wm831x-i2c.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/mfd/wm831x/core.h>
  21. #include <linux/mfd/wm831x/pdata.h>
  22. static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
  23. int bytes, void *dest)
  24. {
  25. struct i2c_client *i2c = wm831x->control_data;
  26. int ret;
  27. u16 r = cpu_to_be16(reg);
  28. ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
  29. if (ret < 0)
  30. return ret;
  31. if (ret != 2)
  32. return -EIO;
  33. ret = i2c_master_recv(i2c, dest, bytes);
  34. if (ret < 0)
  35. return ret;
  36. if (ret != bytes)
  37. return -EIO;
  38. return 0;
  39. }
  40. /* Currently we allocate the write buffer on the stack; this is OK for
  41. * small writes - if we need to do large writes this will need to be
  42. * revised.
  43. */
  44. static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
  45. int bytes, void *src)
  46. {
  47. struct i2c_client *i2c = wm831x->control_data;
  48. struct i2c_msg xfer[2];
  49. int ret;
  50. reg = cpu_to_be16(reg);
  51. xfer[0].addr = i2c->addr;
  52. xfer[0].flags = 0;
  53. xfer[0].len = 2;
  54. xfer[0].buf = (char *)&reg;
  55. xfer[1].addr = i2c->addr;
  56. xfer[1].flags = I2C_M_NOSTART;
  57. xfer[1].len = bytes;
  58. xfer[1].buf = (char *)src;
  59. ret = i2c_transfer(i2c->adapter, xfer, 2);
  60. if (ret < 0)
  61. return ret;
  62. if (ret != 2)
  63. return -EIO;
  64. return 0;
  65. }
  66. static int wm831x_i2c_probe(struct i2c_client *i2c,
  67. const struct i2c_device_id *id)
  68. {
  69. struct wm831x *wm831x;
  70. wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
  71. if (wm831x == NULL)
  72. return -ENOMEM;
  73. i2c_set_clientdata(i2c, wm831x);
  74. wm831x->dev = &i2c->dev;
  75. wm831x->control_data = i2c;
  76. wm831x->read_dev = wm831x_i2c_read_device;
  77. wm831x->write_dev = wm831x_i2c_write_device;
  78. return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
  79. }
  80. static int wm831x_i2c_remove(struct i2c_client *i2c)
  81. {
  82. struct wm831x *wm831x = i2c_get_clientdata(i2c);
  83. wm831x_device_exit(wm831x);
  84. return 0;
  85. }
  86. static int wm831x_i2c_suspend(struct device *dev)
  87. {
  88. struct wm831x *wm831x = dev_get_drvdata(dev);
  89. return wm831x_device_suspend(wm831x);
  90. }
  91. static const struct i2c_device_id wm831x_i2c_id[] = {
  92. { "wm8310", WM8310 },
  93. { "wm8311", WM8311 },
  94. { "wm8312", WM8312 },
  95. { "wm8320", WM8320 },
  96. { "wm8321", WM8321 },
  97. { "wm8325", WM8325 },
  98. { "wm8326", WM8326 },
  99. { }
  100. };
  101. MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
  102. static const struct dev_pm_ops wm831x_pm_ops = {
  103. .suspend = wm831x_i2c_suspend,
  104. };
  105. static struct i2c_driver wm831x_i2c_driver = {
  106. .driver = {
  107. .name = "wm831x",
  108. .owner = THIS_MODULE,
  109. .pm = &wm831x_pm_ops,
  110. },
  111. .probe = wm831x_i2c_probe,
  112. .remove = wm831x_i2c_remove,
  113. .id_table = wm831x_i2c_id,
  114. };
  115. static int __init wm831x_i2c_init(void)
  116. {
  117. int ret;
  118. ret = i2c_add_driver(&wm831x_i2c_driver);
  119. if (ret != 0)
  120. pr_err("Failed to register wm831x I2C driver: %d\n", ret);
  121. return ret;
  122. }
  123. subsys_initcall(wm831x_i2c_init);
  124. static void __exit wm831x_i2c_exit(void)
  125. {
  126. i2c_del_driver(&wm831x_i2c_driver);
  127. }
  128. module_exit(wm831x_i2c_exit);