wm8350-i2c.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include <linux/slab.h>
  22. static int wm8350_i2c_read_device(struct wm8350 *wm8350, char reg,
  23. int bytes, void *dest)
  24. {
  25. int ret;
  26. ret = i2c_master_send(wm8350->i2c_client, &reg, 1);
  27. if (ret < 0)
  28. return ret;
  29. ret = i2c_master_recv(wm8350->i2c_client, dest, bytes);
  30. if (ret < 0)
  31. return ret;
  32. if (ret != bytes)
  33. return -EIO;
  34. return 0;
  35. }
  36. static int wm8350_i2c_write_device(struct wm8350 *wm8350, char reg,
  37. int bytes, void *src)
  38. {
  39. /* we add 1 byte for device register */
  40. u8 msg[(WM8350_MAX_REGISTER << 1) + 1];
  41. int ret;
  42. if (bytes > ((WM8350_MAX_REGISTER << 1) + 1))
  43. return -EINVAL;
  44. msg[0] = reg;
  45. memcpy(&msg[1], src, bytes);
  46. ret = i2c_master_send(wm8350->i2c_client, msg, bytes + 1);
  47. if (ret < 0)
  48. return ret;
  49. if (ret != bytes + 1)
  50. return -EIO;
  51. return 0;
  52. }
  53. static int wm8350_i2c_probe(struct i2c_client *i2c,
  54. const struct i2c_device_id *id)
  55. {
  56. struct wm8350 *wm8350;
  57. int ret = 0;
  58. wm8350 = kzalloc(sizeof(struct wm8350), GFP_KERNEL);
  59. if (wm8350 == NULL) {
  60. kfree(i2c);
  61. return -ENOMEM;
  62. }
  63. i2c_set_clientdata(i2c, wm8350);
  64. wm8350->dev = &i2c->dev;
  65. wm8350->i2c_client = i2c;
  66. wm8350->read_dev = wm8350_i2c_read_device;
  67. wm8350->write_dev = wm8350_i2c_write_device;
  68. ret = wm8350_device_init(wm8350, i2c->irq, i2c->dev.platform_data);
  69. if (ret < 0)
  70. goto err;
  71. return ret;
  72. err:
  73. kfree(wm8350);
  74. return ret;
  75. }
  76. static int wm8350_i2c_remove(struct i2c_client *i2c)
  77. {
  78. struct wm8350 *wm8350 = i2c_get_clientdata(i2c);
  79. wm8350_device_exit(wm8350);
  80. kfree(wm8350);
  81. return 0;
  82. }
  83. static const struct i2c_device_id wm8350_i2c_id[] = {
  84. { "wm8350", 0 },
  85. { "wm8351", 0 },
  86. { "wm8352", 0 },
  87. { }
  88. };
  89. MODULE_DEVICE_TABLE(i2c, wm8350_i2c_id);
  90. static struct i2c_driver wm8350_i2c_driver = {
  91. .driver = {
  92. .name = "wm8350",
  93. .owner = THIS_MODULE,
  94. },
  95. .probe = wm8350_i2c_probe,
  96. .remove = wm8350_i2c_remove,
  97. .id_table = wm8350_i2c_id,
  98. };
  99. static int __init wm8350_i2c_init(void)
  100. {
  101. return i2c_add_driver(&wm8350_i2c_driver);
  102. }
  103. /* init early so consumer devices can complete system boot */
  104. subsys_initcall(wm8350_i2c_init);
  105. static void __exit wm8350_i2c_exit(void)
  106. {
  107. i2c_del_driver(&wm8350_i2c_driver);
  108. }
  109. module_exit(wm8350_i2c_exit);
  110. MODULE_DESCRIPTION("I2C support for the WM8350 AudioPlus PMIC");
  111. MODULE_LICENSE("GPL");