wm831x-i2c.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * wm831x-i2c.c -- I2C access for Wolfson WM831x PMICs
  4. *
  5. * Copyright 2009,2010 Wolfson Microelectronics PLC.
  6. *
  7. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/i2c.h>
  12. #include <linux/delay.h>
  13. #include <linux/mfd/core.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/mfd/wm831x/core.h>
  20. #include <linux/mfd/wm831x/pdata.h>
  21. static int wm831x_i2c_probe(struct i2c_client *i2c,
  22. const struct i2c_device_id *id)
  23. {
  24. struct wm831x_pdata *pdata = dev_get_platdata(&i2c->dev);
  25. const struct of_device_id *of_id;
  26. struct wm831x *wm831x;
  27. enum wm831x_parent type;
  28. int ret;
  29. if (i2c->dev.of_node) {
  30. of_id = of_match_device(wm831x_of_match, &i2c->dev);
  31. if (!of_id) {
  32. dev_err(&i2c->dev, "Failed to match device\n");
  33. return -ENODEV;
  34. }
  35. type = (enum wm831x_parent)of_id->data;
  36. } else {
  37. type = (enum wm831x_parent)id->driver_data;
  38. }
  39. wm831x = devm_kzalloc(&i2c->dev, sizeof(struct wm831x), GFP_KERNEL);
  40. if (wm831x == NULL)
  41. return -ENOMEM;
  42. i2c_set_clientdata(i2c, wm831x);
  43. wm831x->dev = &i2c->dev;
  44. wm831x->type = type;
  45. wm831x->regmap = devm_regmap_init_i2c(i2c, &wm831x_regmap_config);
  46. if (IS_ERR(wm831x->regmap)) {
  47. ret = PTR_ERR(wm831x->regmap);
  48. dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
  49. ret);
  50. return ret;
  51. }
  52. if (pdata)
  53. memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
  54. return wm831x_device_init(wm831x, i2c->irq);
  55. }
  56. static int wm831x_i2c_suspend(struct device *dev)
  57. {
  58. struct wm831x *wm831x = dev_get_drvdata(dev);
  59. return wm831x_device_suspend(wm831x);
  60. }
  61. static int wm831x_i2c_poweroff(struct device *dev)
  62. {
  63. struct wm831x *wm831x = dev_get_drvdata(dev);
  64. wm831x_device_shutdown(wm831x);
  65. return 0;
  66. }
  67. static const struct i2c_device_id wm831x_i2c_id[] = {
  68. { "wm8310", WM8310 },
  69. { "wm8311", WM8311 },
  70. { "wm8312", WM8312 },
  71. { "wm8320", WM8320 },
  72. { "wm8321", WM8321 },
  73. { "wm8325", WM8325 },
  74. { "wm8326", WM8326 },
  75. { }
  76. };
  77. static const struct dev_pm_ops wm831x_pm_ops = {
  78. .suspend = wm831x_i2c_suspend,
  79. .poweroff = wm831x_i2c_poweroff,
  80. };
  81. static struct i2c_driver wm831x_i2c_driver = {
  82. .driver = {
  83. .name = "wm831x",
  84. .pm = &wm831x_pm_ops,
  85. .of_match_table = of_match_ptr(wm831x_of_match),
  86. .suppress_bind_attrs = true,
  87. },
  88. .probe = wm831x_i2c_probe,
  89. .id_table = wm831x_i2c_id,
  90. };
  91. static int __init wm831x_i2c_init(void)
  92. {
  93. int ret;
  94. ret = i2c_add_driver(&wm831x_i2c_driver);
  95. if (ret != 0)
  96. pr_err("Failed to register wm831x I2C driver: %d\n", ret);
  97. return ret;
  98. }
  99. subsys_initcall(wm831x_i2c_init);