wm831x-i2c.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * wm831x-i2c.c -- I2C access for Wolfson WM831x PMICs
  3. *
  4. * Copyright 2009,2010 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/delay.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/slab.h>
  20. #include <linux/err.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/mfd/wm831x/core.h>
  25. #include <linux/mfd/wm831x/pdata.h>
  26. static int wm831x_i2c_probe(struct i2c_client *i2c,
  27. const struct i2c_device_id *id)
  28. {
  29. struct wm831x_pdata *pdata = dev_get_platdata(&i2c->dev);
  30. const struct of_device_id *of_id;
  31. struct wm831x *wm831x;
  32. enum wm831x_parent type;
  33. int ret;
  34. if (i2c->dev.of_node) {
  35. of_id = of_match_device(wm831x_of_match, &i2c->dev);
  36. if (!of_id) {
  37. dev_err(&i2c->dev, "Failed to match device\n");
  38. return -ENODEV;
  39. }
  40. type = (enum wm831x_parent)of_id->data;
  41. } else {
  42. type = (enum wm831x_parent)id->driver_data;
  43. }
  44. wm831x = devm_kzalloc(&i2c->dev, sizeof(struct wm831x), GFP_KERNEL);
  45. if (wm831x == NULL)
  46. return -ENOMEM;
  47. i2c_set_clientdata(i2c, wm831x);
  48. wm831x->dev = &i2c->dev;
  49. wm831x->type = type;
  50. wm831x->regmap = devm_regmap_init_i2c(i2c, &wm831x_regmap_config);
  51. if (IS_ERR(wm831x->regmap)) {
  52. ret = PTR_ERR(wm831x->regmap);
  53. dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
  54. ret);
  55. return ret;
  56. }
  57. if (pdata)
  58. memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
  59. return wm831x_device_init(wm831x, i2c->irq);
  60. }
  61. static int wm831x_i2c_remove(struct i2c_client *i2c)
  62. {
  63. struct wm831x *wm831x = i2c_get_clientdata(i2c);
  64. wm831x_device_exit(wm831x);
  65. return 0;
  66. }
  67. static int wm831x_i2c_suspend(struct device *dev)
  68. {
  69. struct wm831x *wm831x = dev_get_drvdata(dev);
  70. return wm831x_device_suspend(wm831x);
  71. }
  72. static int wm831x_i2c_poweroff(struct device *dev)
  73. {
  74. struct wm831x *wm831x = dev_get_drvdata(dev);
  75. wm831x_device_shutdown(wm831x);
  76. return 0;
  77. }
  78. static const struct i2c_device_id wm831x_i2c_id[] = {
  79. { "wm8310", WM8310 },
  80. { "wm8311", WM8311 },
  81. { "wm8312", WM8312 },
  82. { "wm8320", WM8320 },
  83. { "wm8321", WM8321 },
  84. { "wm8325", WM8325 },
  85. { "wm8326", WM8326 },
  86. { }
  87. };
  88. MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
  89. static const struct dev_pm_ops wm831x_pm_ops = {
  90. .suspend = wm831x_i2c_suspend,
  91. .poweroff = wm831x_i2c_poweroff,
  92. };
  93. static struct i2c_driver wm831x_i2c_driver = {
  94. .driver = {
  95. .name = "wm831x",
  96. .pm = &wm831x_pm_ops,
  97. .of_match_table = of_match_ptr(wm831x_of_match),
  98. },
  99. .probe = wm831x_i2c_probe,
  100. .remove = wm831x_i2c_remove,
  101. .id_table = wm831x_i2c_id,
  102. };
  103. static int __init wm831x_i2c_init(void)
  104. {
  105. int ret;
  106. ret = i2c_add_driver(&wm831x_i2c_driver);
  107. if (ret != 0)
  108. pr_err("Failed to register wm831x I2C driver: %d\n", ret);
  109. return ret;
  110. }
  111. subsys_initcall(wm831x_i2c_init);
  112. static void __exit wm831x_i2c_exit(void)
  113. {
  114. i2c_del_driver(&wm831x_i2c_driver);
  115. }
  116. module_exit(wm831x_i2c_exit);