leds-wm8350.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * LED driver for WM8350 driven LEDS.
  3. *
  4. * Copyright(C) 2007, 2008 Wolfson Microelectronics PLC.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/leds.h>
  15. #include <linux/err.h>
  16. #include <linux/mfd/wm8350/pmic.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/slab.h>
  19. /* Microamps */
  20. static const int isink_cur[] = {
  21. 4,
  22. 5,
  23. 6,
  24. 7,
  25. 8,
  26. 10,
  27. 11,
  28. 14,
  29. 16,
  30. 19,
  31. 23,
  32. 27,
  33. 32,
  34. 39,
  35. 46,
  36. 54,
  37. 65,
  38. 77,
  39. 92,
  40. 109,
  41. 130,
  42. 154,
  43. 183,
  44. 218,
  45. 259,
  46. 308,
  47. 367,
  48. 436,
  49. 518,
  50. 616,
  51. 733,
  52. 872,
  53. 1037,
  54. 1233,
  55. 1466,
  56. 1744,
  57. 2073,
  58. 2466,
  59. 2933,
  60. 3487,
  61. 4147,
  62. 4932,
  63. 5865,
  64. 6975,
  65. 8294,
  66. 9864,
  67. 11730,
  68. 13949,
  69. 16589,
  70. 19728,
  71. 23460,
  72. 27899,
  73. 33178,
  74. 39455,
  75. 46920,
  76. 55798,
  77. 66355,
  78. 78910,
  79. 93840,
  80. 111596,
  81. 132710,
  82. 157820,
  83. 187681,
  84. 223191
  85. };
  86. #define to_wm8350_led(led_cdev) \
  87. container_of(led_cdev, struct wm8350_led, cdev)
  88. static void wm8350_led_enable(struct wm8350_led *led)
  89. {
  90. int ret;
  91. if (led->enabled)
  92. return;
  93. ret = regulator_enable(led->isink);
  94. if (ret != 0) {
  95. dev_err(led->cdev.dev, "Failed to enable ISINK: %d\n", ret);
  96. return;
  97. }
  98. ret = regulator_enable(led->dcdc);
  99. if (ret != 0) {
  100. dev_err(led->cdev.dev, "Failed to enable DCDC: %d\n", ret);
  101. regulator_disable(led->isink);
  102. return;
  103. }
  104. led->enabled = 1;
  105. }
  106. static void wm8350_led_disable(struct wm8350_led *led)
  107. {
  108. int ret;
  109. if (!led->enabled)
  110. return;
  111. ret = regulator_disable(led->dcdc);
  112. if (ret != 0) {
  113. dev_err(led->cdev.dev, "Failed to disable DCDC: %d\n", ret);
  114. return;
  115. }
  116. ret = regulator_disable(led->isink);
  117. if (ret != 0) {
  118. dev_err(led->cdev.dev, "Failed to disable ISINK: %d\n", ret);
  119. regulator_enable(led->dcdc);
  120. return;
  121. }
  122. led->enabled = 0;
  123. }
  124. static void led_work(struct work_struct *work)
  125. {
  126. struct wm8350_led *led = container_of(work, struct wm8350_led, work);
  127. int ret;
  128. int uA;
  129. unsigned long flags;
  130. mutex_lock(&led->mutex);
  131. spin_lock_irqsave(&led->value_lock, flags);
  132. if (led->value == LED_OFF) {
  133. spin_unlock_irqrestore(&led->value_lock, flags);
  134. wm8350_led_disable(led);
  135. goto out;
  136. }
  137. /* This scales linearly into the index of valid current
  138. * settings which results in a linear scaling of perceived
  139. * brightness due to the non-linear current settings provided
  140. * by the hardware.
  141. */
  142. uA = (led->max_uA_index * led->value) / LED_FULL;
  143. spin_unlock_irqrestore(&led->value_lock, flags);
  144. BUG_ON(uA >= ARRAY_SIZE(isink_cur));
  145. ret = regulator_set_current_limit(led->isink, isink_cur[uA],
  146. isink_cur[uA]);
  147. if (ret != 0)
  148. dev_err(led->cdev.dev, "Failed to set %duA: %d\n",
  149. isink_cur[uA], ret);
  150. wm8350_led_enable(led);
  151. out:
  152. mutex_unlock(&led->mutex);
  153. }
  154. static void wm8350_led_set(struct led_classdev *led_cdev,
  155. enum led_brightness value)
  156. {
  157. struct wm8350_led *led = to_wm8350_led(led_cdev);
  158. unsigned long flags;
  159. spin_lock_irqsave(&led->value_lock, flags);
  160. led->value = value;
  161. schedule_work(&led->work);
  162. spin_unlock_irqrestore(&led->value_lock, flags);
  163. }
  164. static void wm8350_led_shutdown(struct platform_device *pdev)
  165. {
  166. struct wm8350_led *led = platform_get_drvdata(pdev);
  167. mutex_lock(&led->mutex);
  168. led->value = LED_OFF;
  169. wm8350_led_disable(led);
  170. mutex_unlock(&led->mutex);
  171. }
  172. static int wm8350_led_probe(struct platform_device *pdev)
  173. {
  174. struct regulator *isink, *dcdc;
  175. struct wm8350_led *led;
  176. struct wm8350_led_platform_data *pdata = pdev->dev.platform_data;
  177. int ret, i;
  178. if (pdata == NULL) {
  179. dev_err(&pdev->dev, "no platform data\n");
  180. return -ENODEV;
  181. }
  182. if (pdata->max_uA < isink_cur[0]) {
  183. dev_err(&pdev->dev, "Invalid maximum current %duA\n",
  184. pdata->max_uA);
  185. return -EINVAL;
  186. }
  187. isink = regulator_get(&pdev->dev, "led_isink");
  188. if (IS_ERR(isink)) {
  189. printk(KERN_ERR "%s: can't get ISINK\n", __func__);
  190. return PTR_ERR(isink);
  191. }
  192. dcdc = regulator_get(&pdev->dev, "led_vcc");
  193. if (IS_ERR(dcdc)) {
  194. printk(KERN_ERR "%s: can't get DCDC\n", __func__);
  195. ret = PTR_ERR(dcdc);
  196. goto err_isink;
  197. }
  198. led = kzalloc(sizeof(*led), GFP_KERNEL);
  199. if (led == NULL) {
  200. ret = -ENOMEM;
  201. goto err_dcdc;
  202. }
  203. led->cdev.brightness_set = wm8350_led_set;
  204. led->cdev.default_trigger = pdata->default_trigger;
  205. led->cdev.name = pdata->name;
  206. led->cdev.flags |= LED_CORE_SUSPENDRESUME;
  207. led->enabled = regulator_is_enabled(isink);
  208. led->isink = isink;
  209. led->dcdc = dcdc;
  210. for (i = 0; i < ARRAY_SIZE(isink_cur) - 1; i++)
  211. if (isink_cur[i] >= pdata->max_uA)
  212. break;
  213. led->max_uA_index = i;
  214. if (pdata->max_uA != isink_cur[i])
  215. dev_warn(&pdev->dev,
  216. "Maximum current %duA is not directly supported,"
  217. " check platform data\n",
  218. pdata->max_uA);
  219. spin_lock_init(&led->value_lock);
  220. mutex_init(&led->mutex);
  221. INIT_WORK(&led->work, led_work);
  222. led->value = LED_OFF;
  223. platform_set_drvdata(pdev, led);
  224. ret = led_classdev_register(&pdev->dev, &led->cdev);
  225. if (ret < 0)
  226. goto err_led;
  227. return 0;
  228. err_led:
  229. kfree(led);
  230. err_dcdc:
  231. regulator_put(dcdc);
  232. err_isink:
  233. regulator_put(isink);
  234. return ret;
  235. }
  236. static int wm8350_led_remove(struct platform_device *pdev)
  237. {
  238. struct wm8350_led *led = platform_get_drvdata(pdev);
  239. led_classdev_unregister(&led->cdev);
  240. flush_work_sync(&led->work);
  241. wm8350_led_disable(led);
  242. regulator_put(led->dcdc);
  243. regulator_put(led->isink);
  244. kfree(led);
  245. return 0;
  246. }
  247. static struct platform_driver wm8350_led_driver = {
  248. .driver = {
  249. .name = "wm8350-led",
  250. .owner = THIS_MODULE,
  251. },
  252. .probe = wm8350_led_probe,
  253. .remove = wm8350_led_remove,
  254. .shutdown = wm8350_led_shutdown,
  255. };
  256. static int __devinit wm8350_led_init(void)
  257. {
  258. return platform_driver_register(&wm8350_led_driver);
  259. }
  260. module_init(wm8350_led_init);
  261. static void wm8350_led_exit(void)
  262. {
  263. platform_driver_unregister(&wm8350_led_driver);
  264. }
  265. module_exit(wm8350_led_exit);
  266. MODULE_AUTHOR("Mark Brown");
  267. MODULE_DESCRIPTION("WM8350 LED driver");
  268. MODULE_LICENSE("GPL");
  269. MODULE_ALIAS("platform:wm8350-led");