wm8350-i2c.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * wm8350-i2c.c -- Generic I2C driver for Wolfson WM8350 PMIC
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Liam Girdwood
  7. * linux@wolfsonmicro.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/init.h>
  18. #include <linux/i2c.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mfd/wm8350/core.h>
  21. static int wm8350_i2c_read_device(struct wm8350 *wm8350, char reg,
  22. int bytes, void *dest)
  23. {
  24. int ret;
  25. ret = i2c_master_send(wm8350->i2c_client, &reg, 1);
  26. if (ret < 0)
  27. return ret;
  28. ret = i2c_master_recv(wm8350->i2c_client, dest, bytes);
  29. if (ret < 0)
  30. return ret;
  31. if (ret != bytes)
  32. return -EIO;
  33. return 0;
  34. }
  35. static int wm8350_i2c_write_device(struct wm8350 *wm8350, char reg,
  36. int bytes, void *src)
  37. {
  38. /* we add 1 byte for device register */
  39. u8 msg[(WM8350_MAX_REGISTER << 1) + 1];
  40. int ret;
  41. if (bytes > ((WM8350_MAX_REGISTER << 1) + 1))
  42. return -EINVAL;
  43. msg[0] = reg;
  44. memcpy(&msg[1], src, bytes);
  45. ret = i2c_master_send(wm8350->i2c_client, msg, bytes + 1);
  46. if (ret < 0)
  47. return ret;
  48. if (ret != bytes + 1)
  49. return -EIO;
  50. return 0;
  51. }
  52. static int wm8350_i2c_probe(struct i2c_client *i2c,
  53. const struct i2c_device_id *id)
  54. {
  55. struct wm8350 *wm8350;
  56. int ret = 0;
  57. wm8350 = kzalloc(sizeof(struct wm8350), GFP_KERNEL);
  58. if (wm8350 == NULL) {
  59. kfree(i2c);
  60. return -ENOMEM;
  61. }
  62. i2c_set_clientdata(i2c, wm8350);
  63. wm8350->dev = &i2c->dev;
  64. wm8350->i2c_client = i2c;
  65. wm8350->read_dev = wm8350_i2c_read_device;
  66. wm8350->write_dev = wm8350_i2c_write_device;
  67. ret = wm8350_device_init(wm8350, i2c->irq, i2c->dev.platform_data);
  68. if (ret < 0)
  69. goto err;
  70. return ret;
  71. err:
  72. kfree(wm8350);
  73. return ret;
  74. }
  75. static int wm8350_i2c_remove(struct i2c_client *i2c)
  76. {
  77. struct wm8350 *wm8350 = i2c_get_clientdata(i2c);
  78. wm8350_device_exit(wm8350);
  79. kfree(wm8350);
  80. return 0;
  81. }
  82. static const struct i2c_device_id wm8350_i2c_id[] = {
  83. { "wm8350", 0 },
  84. { "wm8351", 0 },
  85. { "wm8352", 0 },
  86. { }
  87. };
  88. MODULE_DEVICE_TABLE(i2c, wm8350_i2c_id);
  89. static struct i2c_driver wm8350_i2c_driver = {
  90. .driver = {
  91. .name = "wm8350",
  92. .owner = THIS_MODULE,
  93. },
  94. .probe = wm8350_i2c_probe,
  95. .remove = wm8350_i2c_remove,
  96. .id_table = wm8350_i2c_id,
  97. };
  98. static int __init wm8350_i2c_init(void)
  99. {
  100. return i2c_add_driver(&wm8350_i2c_driver);
  101. }
  102. /* init early so consumer devices can complete system boot */
  103. subsys_initcall(wm8350_i2c_init);
  104. static void __exit wm8350_i2c_exit(void)
  105. {
  106. i2c_del_driver(&wm8350_i2c_driver);
  107. }
  108. module_exit(wm8350_i2c_exit);
  109. MODULE_DESCRIPTION("I2C support for the WM8350 AudioPlus PMIC");
  110. MODULE_LICENSE("GPL");