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. #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. return -ENOMEM;
  61. i2c_set_clientdata(i2c, wm8350);
  62. wm8350->dev = &i2c->dev;
  63. wm8350->i2c_client = i2c;
  64. wm8350->read_dev = wm8350_i2c_read_device;
  65. wm8350->write_dev = wm8350_i2c_write_device;
  66. ret = wm8350_device_init(wm8350, i2c->irq, i2c->dev.platform_data);
  67. if (ret < 0)
  68. goto err;
  69. return ret;
  70. err:
  71. kfree(wm8350);
  72. return ret;
  73. }
  74. static int wm8350_i2c_remove(struct i2c_client *i2c)
  75. {
  76. struct wm8350 *wm8350 = i2c_get_clientdata(i2c);
  77. wm8350_device_exit(wm8350);
  78. kfree(wm8350);
  79. return 0;
  80. }
  81. static const struct i2c_device_id wm8350_i2c_id[] = {
  82. { "wm8350", 0 },
  83. { "wm8351", 0 },
  84. { "wm8352", 0 },
  85. { }
  86. };
  87. MODULE_DEVICE_TABLE(i2c, wm8350_i2c_id);
  88. static struct i2c_driver wm8350_i2c_driver = {
  89. .driver = {
  90. .name = "wm8350",
  91. .owner = THIS_MODULE,
  92. },
  93. .probe = wm8350_i2c_probe,
  94. .remove = wm8350_i2c_remove,
  95. .id_table = wm8350_i2c_id,
  96. };
  97. static int __init wm8350_i2c_init(void)
  98. {
  99. return i2c_add_driver(&wm8350_i2c_driver);
  100. }
  101. /* init early so consumer devices can complete system boot */
  102. subsys_initcall(wm8350_i2c_init);
  103. static void __exit wm8350_i2c_exit(void)
  104. {
  105. i2c_del_driver(&wm8350_i2c_driver);
  106. }
  107. module_exit(wm8350_i2c_exit);
  108. MODULE_DESCRIPTION("I2C support for the WM8350 AudioPlus PMIC");
  109. MODULE_LICENSE("GPL");