wm8350-i2c.c 2.7 KB

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