wm831x-spi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * wm831x-spi.c -- SPI access for Wolfson WM831x PMICs
  3. *
  4. * Copyright 2009,2010 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/pm.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/mfd/wm831x/core.h>
  19. static int wm831x_spi_read_device(struct wm831x *wm831x, unsigned short reg,
  20. int bytes, void *dest)
  21. {
  22. u16 tx_val;
  23. u16 *d = dest;
  24. int r, ret;
  25. /* Go register at a time */
  26. for (r = reg; r < reg + (bytes / 2); r++) {
  27. tx_val = r | 0x8000;
  28. ret = spi_write_then_read(wm831x->control_data,
  29. (u8 *)&tx_val, 2, (u8 *)d, 2);
  30. if (ret != 0)
  31. return ret;
  32. *d = be16_to_cpu(*d);
  33. d++;
  34. }
  35. return 0;
  36. }
  37. static int wm831x_spi_write_device(struct wm831x *wm831x, unsigned short reg,
  38. int bytes, void *src)
  39. {
  40. struct spi_device *spi = wm831x->control_data;
  41. u16 *s = src;
  42. u16 data[2];
  43. int ret, r;
  44. /* Go register at a time */
  45. for (r = reg; r < reg + (bytes / 2); r++) {
  46. data[0] = r;
  47. data[1] = *s++;
  48. ret = spi_write(spi, (char *)&data, sizeof(data));
  49. if (ret != 0)
  50. return ret;
  51. }
  52. return 0;
  53. }
  54. static int __devinit wm831x_spi_probe(struct spi_device *spi)
  55. {
  56. struct wm831x *wm831x;
  57. enum wm831x_parent type;
  58. /* Currently SPI support for ID tables is unmerged, we're faking it */
  59. if (strcmp(spi->modalias, "wm8310") == 0)
  60. type = WM8310;
  61. else if (strcmp(spi->modalias, "wm8311") == 0)
  62. type = WM8311;
  63. else if (strcmp(spi->modalias, "wm8312") == 0)
  64. type = WM8312;
  65. else if (strcmp(spi->modalias, "wm8320") == 0)
  66. type = WM8320;
  67. else if (strcmp(spi->modalias, "wm8321") == 0)
  68. type = WM8321;
  69. else if (strcmp(spi->modalias, "wm8325") == 0)
  70. type = WM8325;
  71. else if (strcmp(spi->modalias, "wm8326") == 0)
  72. type = WM8326;
  73. else {
  74. dev_err(&spi->dev, "Unknown device type\n");
  75. return -EINVAL;
  76. }
  77. wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
  78. if (wm831x == NULL)
  79. return -ENOMEM;
  80. spi->bits_per_word = 16;
  81. spi->mode = SPI_MODE_0;
  82. dev_set_drvdata(&spi->dev, wm831x);
  83. wm831x->dev = &spi->dev;
  84. wm831x->control_data = spi;
  85. wm831x->read_dev = wm831x_spi_read_device;
  86. wm831x->write_dev = wm831x_spi_write_device;
  87. return wm831x_device_init(wm831x, type, spi->irq);
  88. }
  89. static int __devexit wm831x_spi_remove(struct spi_device *spi)
  90. {
  91. struct wm831x *wm831x = dev_get_drvdata(&spi->dev);
  92. wm831x_device_exit(wm831x);
  93. return 0;
  94. }
  95. static int wm831x_spi_suspend(struct device *dev)
  96. {
  97. struct wm831x *wm831x = dev_get_drvdata(dev);
  98. return wm831x_device_suspend(wm831x);
  99. }
  100. static const struct dev_pm_ops wm831x_spi_pm = {
  101. .freeze = wm831x_spi_suspend,
  102. .suspend = wm831x_spi_suspend,
  103. };
  104. static struct spi_driver wm8310_spi_driver = {
  105. .driver = {
  106. .name = "wm8310",
  107. .bus = &spi_bus_type,
  108. .owner = THIS_MODULE,
  109. .pm = &wm831x_spi_pm,
  110. },
  111. .probe = wm831x_spi_probe,
  112. .remove = __devexit_p(wm831x_spi_remove),
  113. };
  114. static struct spi_driver wm8311_spi_driver = {
  115. .driver = {
  116. .name = "wm8311",
  117. .bus = &spi_bus_type,
  118. .owner = THIS_MODULE,
  119. .pm = &wm831x_spi_pm,
  120. },
  121. .probe = wm831x_spi_probe,
  122. .remove = __devexit_p(wm831x_spi_remove),
  123. };
  124. static struct spi_driver wm8312_spi_driver = {
  125. .driver = {
  126. .name = "wm8312",
  127. .bus = &spi_bus_type,
  128. .owner = THIS_MODULE,
  129. .pm = &wm831x_spi_pm,
  130. },
  131. .probe = wm831x_spi_probe,
  132. .remove = __devexit_p(wm831x_spi_remove),
  133. };
  134. static struct spi_driver wm8320_spi_driver = {
  135. .driver = {
  136. .name = "wm8320",
  137. .bus = &spi_bus_type,
  138. .owner = THIS_MODULE,
  139. .pm = &wm831x_spi_pm,
  140. },
  141. .probe = wm831x_spi_probe,
  142. .remove = __devexit_p(wm831x_spi_remove),
  143. };
  144. static struct spi_driver wm8321_spi_driver = {
  145. .driver = {
  146. .name = "wm8321",
  147. .bus = &spi_bus_type,
  148. .owner = THIS_MODULE,
  149. .pm = &wm831x_spi_pm,
  150. },
  151. .probe = wm831x_spi_probe,
  152. .remove = __devexit_p(wm831x_spi_remove),
  153. };
  154. static struct spi_driver wm8325_spi_driver = {
  155. .driver = {
  156. .name = "wm8325",
  157. .bus = &spi_bus_type,
  158. .owner = THIS_MODULE,
  159. .pm = &wm831x_spi_pm,
  160. },
  161. .probe = wm831x_spi_probe,
  162. .remove = __devexit_p(wm831x_spi_remove),
  163. };
  164. static struct spi_driver wm8326_spi_driver = {
  165. .driver = {
  166. .name = "wm8326",
  167. .bus = &spi_bus_type,
  168. .owner = THIS_MODULE,
  169. .pm = &wm831x_spi_pm,
  170. },
  171. .probe = wm831x_spi_probe,
  172. .remove = __devexit_p(wm831x_spi_remove),
  173. };
  174. static int __init wm831x_spi_init(void)
  175. {
  176. int ret;
  177. ret = spi_register_driver(&wm8310_spi_driver);
  178. if (ret != 0)
  179. pr_err("Failed to register WM8310 SPI driver: %d\n", ret);
  180. ret = spi_register_driver(&wm8311_spi_driver);
  181. if (ret != 0)
  182. pr_err("Failed to register WM8311 SPI driver: %d\n", ret);
  183. ret = spi_register_driver(&wm8312_spi_driver);
  184. if (ret != 0)
  185. pr_err("Failed to register WM8312 SPI driver: %d\n", ret);
  186. ret = spi_register_driver(&wm8320_spi_driver);
  187. if (ret != 0)
  188. pr_err("Failed to register WM8320 SPI driver: %d\n", ret);
  189. ret = spi_register_driver(&wm8321_spi_driver);
  190. if (ret != 0)
  191. pr_err("Failed to register WM8321 SPI driver: %d\n", ret);
  192. ret = spi_register_driver(&wm8325_spi_driver);
  193. if (ret != 0)
  194. pr_err("Failed to register WM8325 SPI driver: %d\n", ret);
  195. ret = spi_register_driver(&wm8326_spi_driver);
  196. if (ret != 0)
  197. pr_err("Failed to register WM8326 SPI driver: %d\n", ret);
  198. return 0;
  199. }
  200. subsys_initcall(wm831x_spi_init);
  201. static void __exit wm831x_spi_exit(void)
  202. {
  203. spi_unregister_driver(&wm8326_spi_driver);
  204. spi_unregister_driver(&wm8325_spi_driver);
  205. spi_unregister_driver(&wm8321_spi_driver);
  206. spi_unregister_driver(&wm8320_spi_driver);
  207. spi_unregister_driver(&wm8312_spi_driver);
  208. spi_unregister_driver(&wm8311_spi_driver);
  209. spi_unregister_driver(&wm8310_spi_driver);
  210. }
  211. module_exit(wm831x_spi_exit);
  212. MODULE_DESCRIPTION("SPI support for WM831x/2x AudioPlus PMICs");
  213. MODULE_LICENSE("GPL");
  214. MODULE_AUTHOR("Mark Brown");