wm8350-i2c.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * wm8350-i2c.c -- Generic I2C driver for Wolfson WM8350 PMIC
  3. *
  4. * This driver defines and configures the WM8350 for the Freescale i.MX32ADS.
  5. *
  6. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  7. *
  8. * Author: Liam Girdwood
  9. * linux@wolfsonmicro.com
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/init.h>
  20. #include <linux/i2c.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/mfd/wm8350/core.h>
  23. static int wm8350_i2c_read_device(struct wm8350 *wm8350, char reg,
  24. int bytes, void *dest)
  25. {
  26. int ret;
  27. ret = i2c_master_send(wm8350->i2c_client, &reg, 1);
  28. if (ret < 0)
  29. return ret;
  30. ret = i2c_master_recv(wm8350->i2c_client, dest, bytes);
  31. if (ret < 0)
  32. return ret;
  33. if (ret != bytes)
  34. return -EIO;
  35. return 0;
  36. }
  37. static int wm8350_i2c_write_device(struct wm8350 *wm8350, char reg,
  38. int bytes, void *src)
  39. {
  40. /* we add 1 byte for device register */
  41. u8 msg[(WM8350_MAX_REGISTER << 1) + 1];
  42. int ret;
  43. if (bytes > ((WM8350_MAX_REGISTER << 1) + 1))
  44. return -EINVAL;
  45. msg[0] = reg;
  46. memcpy(&msg[1], src, bytes);
  47. ret = i2c_master_send(wm8350->i2c_client, msg, bytes + 1);
  48. if (ret < 0)
  49. return ret;
  50. if (ret != bytes + 1)
  51. return -EIO;
  52. return 0;
  53. }
  54. static int wm8350_i2c_probe(struct i2c_client *i2c,
  55. const struct i2c_device_id *id)
  56. {
  57. struct wm8350 *wm8350;
  58. int ret = 0;
  59. wm8350 = kzalloc(sizeof(struct wm8350), GFP_KERNEL);
  60. if (wm8350 == NULL) {
  61. kfree(i2c);
  62. return -ENOMEM;
  63. }
  64. i2c_set_clientdata(i2c, wm8350);
  65. wm8350->dev = &i2c->dev;
  66. wm8350->i2c_client = i2c;
  67. wm8350->read_dev = wm8350_i2c_read_device;
  68. wm8350->write_dev = wm8350_i2c_write_device;
  69. ret = wm8350_device_init(wm8350, i2c->irq, i2c->dev.platform_data);
  70. if (ret < 0)
  71. goto err;
  72. return ret;
  73. err:
  74. kfree(wm8350);
  75. return ret;
  76. }
  77. static int wm8350_i2c_remove(struct i2c_client *i2c)
  78. {
  79. struct wm8350 *wm8350 = i2c_get_clientdata(i2c);
  80. wm8350_device_exit(wm8350);
  81. kfree(wm8350);
  82. return 0;
  83. }
  84. static const struct i2c_device_id wm8350_i2c_id[] = {
  85. { "wm8350", 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");