wm831x-i2c.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. unsigned char msg[bytes + 2];
  49. int ret;
  50. reg = cpu_to_be16(reg);
  51. memcpy(&msg[0], &reg, 2);
  52. memcpy(&msg[2], src, bytes);
  53. ret = i2c_master_send(i2c, msg, bytes + 2);
  54. if (ret < 0)
  55. return ret;
  56. if (ret < bytes + 2)
  57. return -EIO;
  58. return 0;
  59. }
  60. static int wm831x_i2c_probe(struct i2c_client *i2c,
  61. const struct i2c_device_id *id)
  62. {
  63. struct wm831x *wm831x;
  64. wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
  65. if (wm831x == NULL)
  66. return -ENOMEM;
  67. i2c_set_clientdata(i2c, wm831x);
  68. wm831x->dev = &i2c->dev;
  69. wm831x->control_data = i2c;
  70. wm831x->read_dev = wm831x_i2c_read_device;
  71. wm831x->write_dev = wm831x_i2c_write_device;
  72. return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
  73. }
  74. static int wm831x_i2c_remove(struct i2c_client *i2c)
  75. {
  76. struct wm831x *wm831x = i2c_get_clientdata(i2c);
  77. wm831x_device_exit(wm831x);
  78. return 0;
  79. }
  80. static int wm831x_i2c_suspend(struct i2c_client *i2c, pm_message_t mesg)
  81. {
  82. struct wm831x *wm831x = i2c_get_clientdata(i2c);
  83. return wm831x_device_suspend(wm831x);
  84. }
  85. static const struct i2c_device_id wm831x_i2c_id[] = {
  86. { "wm8310", WM8310 },
  87. { "wm8311", WM8311 },
  88. { "wm8312", WM8312 },
  89. { "wm8320", WM8320 },
  90. { "wm8321", WM8321 },
  91. { "wm8325", WM8325 },
  92. { }
  93. };
  94. MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
  95. static struct i2c_driver wm831x_i2c_driver = {
  96. .driver = {
  97. .name = "wm831x",
  98. .owner = THIS_MODULE,
  99. },
  100. .probe = wm831x_i2c_probe,
  101. .remove = wm831x_i2c_remove,
  102. .suspend = wm831x_i2c_suspend,
  103. .id_table = wm831x_i2c_id,
  104. };
  105. static int __init wm831x_i2c_init(void)
  106. {
  107. int ret;
  108. ret = i2c_add_driver(&wm831x_i2c_driver);
  109. if (ret != 0)
  110. pr_err("Failed to register wm831x I2C driver: %d\n", ret);
  111. return ret;
  112. }
  113. subsys_initcall(wm831x_i2c_init);
  114. static void __exit wm831x_i2c_exit(void)
  115. {
  116. i2c_del_driver(&wm831x_i2c_driver);
  117. }
  118. module_exit(wm831x_i2c_exit);