1
0

leds-nu801.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * LED driver for NU801
  3. *
  4. * Kevin Paul Herbert
  5. * Copyright (c) 2012, Meraki, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/leds.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/delay.h>
  20. #include <linux/leds-nu801.h>
  21. #include <linux/gpio.h>
  22. #include <linux/of_gpio.h>
  23. #define MAX_NAME_LENGTH 24
  24. #define NUM_COLORS 3
  25. static const char * const led_nu801_colors[] = { "blue", "green", "red" };
  26. struct led_nu801_led_data {
  27. struct led_classdev cdev;
  28. struct led_nu801_data *controller;
  29. enum led_brightness level;
  30. char name[MAX_NAME_LENGTH];
  31. };
  32. struct led_nu801_data {
  33. unsigned cki;
  34. unsigned sdi;
  35. int lei;
  36. struct delayed_work work;
  37. struct led_nu801_led_data *led_chain;
  38. int num_leds;
  39. const char *device_name;
  40. const char *name;
  41. u32 ndelay;
  42. atomic_t pending;
  43. };
  44. static void led_nu801_work(struct work_struct *work)
  45. {
  46. struct led_nu801_data *controller =
  47. container_of(work, struct led_nu801_data, work.work);
  48. struct led_nu801_led_data *led;
  49. u16 bit;
  50. u16 brightness;
  51. int index;
  52. for (index = 0; index < controller->num_leds; index++) {
  53. led = &controller->led_chain[index];
  54. brightness = led->level << 8; /* To do: gamma correction */
  55. for (bit = 0x8000; bit; bit = bit >> 1) {
  56. gpio_set_value(controller->sdi,
  57. (brightness & bit) != 0);
  58. gpio_set_value(controller->cki, 1);
  59. if (unlikely(((index == (controller->num_leds - 1)) &&
  60. (bit == 1) &&
  61. (controller->lei < 0)))) {
  62. udelay(600);
  63. } else {
  64. ndelay(controller->ndelay);
  65. }
  66. gpio_set_value(controller->cki, 0);
  67. ndelay(controller->ndelay);
  68. }
  69. }
  70. if (controller->lei >= 0) {
  71. gpio_set_value(controller->lei, 1);
  72. ndelay(controller->ndelay);
  73. gpio_set_value(controller->lei, 0);
  74. }
  75. atomic_set(&controller->pending, 1);
  76. }
  77. static void led_nu801_set(struct led_classdev *led_cdev,
  78. enum led_brightness value)
  79. {
  80. struct led_nu801_led_data *led_dat =
  81. container_of(led_cdev, struct led_nu801_led_data, cdev);
  82. struct led_nu801_data *controller = led_dat->controller;
  83. if (led_dat->level != value) {
  84. led_dat->level = value;
  85. if (atomic_dec_and_test(&controller->pending))
  86. schedule_delayed_work(&led_dat->controller->work,
  87. (HZ/1000) + 1);
  88. }
  89. }
  90. static int __init led_nu801_create(struct led_nu801_data *controller,
  91. struct device *parent,
  92. int index,
  93. enum led_brightness brightness,
  94. #ifdef CONFIG_LEDS_TRIGGERS
  95. const char *default_trigger,
  96. #endif
  97. const char *color)
  98. {
  99. struct led_nu801_led_data *led = &controller->led_chain[index];
  100. int ret;
  101. scnprintf(led->name, sizeof(led->name), "%s:%s:%s%d",
  102. controller->device_name, color, controller->name,
  103. (controller->num_leds - (index + 1)) / NUM_COLORS);
  104. led->cdev.name = led->name;
  105. led->cdev.brightness_set = led_nu801_set;
  106. #ifdef CONFIG_LEDS_TRIGGERS
  107. led->cdev.default_trigger = default_trigger;
  108. #endif
  109. led->level = brightness;
  110. led->controller = controller;
  111. ret = led_classdev_register(parent, &led->cdev);
  112. if (ret < 0)
  113. goto err;
  114. return 0;
  115. err:
  116. kfree(led);
  117. return ret;
  118. }
  119. static int __init
  120. led_nu801_create_chain(const struct led_nu801_template *template,
  121. struct led_nu801_data *controller,
  122. struct device *parent)
  123. {
  124. int ret;
  125. int index;
  126. controller->cki = template->cki;
  127. controller->sdi = template->sdi;
  128. controller->lei = template->lei;
  129. controller->num_leds = template->num_leds * 3;
  130. controller->device_name = template->device_name;
  131. controller->name = template->name;
  132. controller->ndelay = template->ndelay;
  133. atomic_set(&controller->pending, 1);
  134. controller->led_chain = kzalloc(sizeof(struct led_nu801_led_data) *
  135. controller->num_leds, GFP_KERNEL);
  136. if (!controller->led_chain)
  137. return -ENOMEM;
  138. ret = gpio_request(controller->cki, template->name);
  139. if (ret < 0)
  140. goto err_free_chain;
  141. ret = gpio_request(controller->sdi, template->name);
  142. if (ret < 0)
  143. goto err_ret_cki;
  144. if (controller->lei >= 0) {
  145. ret = gpio_request(controller->lei, template->name);
  146. if (ret < 0)
  147. goto err_ret_sdi;
  148. ret = gpio_direction_output(controller->lei, 0);
  149. if (ret < 0)
  150. goto err_ret_lei;
  151. }
  152. ret = gpio_direction_output(controller->cki, 0);
  153. if (ret < 0)
  154. goto err_ret_lei;
  155. ret = gpio_direction_output(controller->sdi, 0);
  156. if (ret < 0)
  157. goto err_ret_lei;
  158. for (index = 0; index < controller->num_leds; index++) {
  159. ret = led_nu801_create(controller, parent, index,
  160. template->init_brightness
  161. [index % NUM_COLORS],
  162. #ifdef CONFIG_LEDS_TRIGGERS
  163. template->default_trigger,
  164. #endif
  165. template->led_colors[index % NUM_COLORS] ?
  166. template->led_colors[index % NUM_COLORS] :
  167. led_nu801_colors[index % NUM_COLORS]);
  168. if (ret < 0)
  169. goto err_ret_sdi;
  170. }
  171. INIT_DELAYED_WORK(&controller->work, led_nu801_work);
  172. schedule_delayed_work(&controller->work, 0);
  173. return 0;
  174. err_ret_lei:
  175. if (controller->lei >= 0)
  176. gpio_free(controller->lei);
  177. err_ret_sdi:
  178. gpio_free(controller->sdi);
  179. err_ret_cki:
  180. gpio_free(controller->cki);
  181. err_free_chain:
  182. kfree(controller->led_chain);
  183. return ret;
  184. }
  185. static void led_nu801_delete_chain(struct led_nu801_data *controller)
  186. {
  187. struct led_nu801_led_data *led_chain;
  188. struct led_nu801_led_data *led;
  189. int index;
  190. int num_leds;
  191. led_chain = controller->led_chain;
  192. controller->led_chain = 0;
  193. num_leds = controller->num_leds;
  194. controller->num_leds = 0;
  195. cancel_delayed_work_sync(&controller->work);
  196. for (index = 0; index < num_leds; index++) {
  197. led = &led_chain[index];
  198. led_classdev_unregister(&led->cdev);
  199. }
  200. gpio_free(controller->cki);
  201. gpio_free(controller->sdi);
  202. if (controller->lei >= 0)
  203. gpio_free(controller->lei);
  204. kfree(led_chain);
  205. }
  206. static struct led_nu801_data * __init
  207. leds_nu801_create_of(struct platform_device *pdev)
  208. {
  209. struct device_node *np = pdev->dev.of_node, *child;
  210. struct led_nu801_data *controllers;
  211. int count = 0, ret;
  212. int i = 0;
  213. for_each_child_of_node(np, child)
  214. count++;
  215. if (!count)
  216. return NULL;
  217. controllers = kzalloc(sizeof(struct led_nu801_data) * count,
  218. GFP_KERNEL);
  219. if (!controllers)
  220. return NULL;
  221. for_each_child_of_node(np, child) {
  222. const char *state;
  223. struct led_nu801_template template = {};
  224. struct device_node *colors;
  225. int jj;
  226. template.cki = of_get_named_gpio_flags(child, "cki", 0, NULL);
  227. template.sdi = of_get_named_gpio_flags(child, "sdi", 0, NULL);
  228. if (of_find_property(child, "lei", NULL)) {
  229. template.lei = of_get_named_gpio_flags(child, "lei",
  230. 0, NULL);
  231. } else {
  232. template.lei = -1;
  233. }
  234. of_property_read_u32(child, "ndelay", &template.ndelay);
  235. of_property_read_u32(child, "num_leds", &template.num_leds);
  236. template.name = of_get_property(child, "label", NULL) ? :
  237. child->name;
  238. template.default_trigger = of_get_property(child,
  239. "default-trigger", NULL);
  240. jj = 0;
  241. for_each_child_of_node(child, colors) {
  242. template.led_colors[jj] = of_get_property(colors,
  243. "label", NULL);
  244. state = of_get_property(colors, "state", NULL);
  245. if (!strncmp(state, "off", 3))
  246. template.init_brightness[jj] = LED_OFF;
  247. else if (!strncmp(state, "half", 4))
  248. template.init_brightness[jj] = LED_HALF;
  249. else if (!strncmp(state, "full", 4))
  250. template.init_brightness[jj] = LED_FULL;
  251. jj++;
  252. }
  253. ret = led_nu801_create_chain(&template,
  254. &controllers[i],
  255. &pdev->dev);
  256. if (ret < 0)
  257. goto err;
  258. i++;
  259. }
  260. return controllers;
  261. err:
  262. for (i = i - 1; i >= 0; i--)
  263. led_nu801_delete_chain(&controllers[i]);
  264. kfree(controllers);
  265. return NULL;
  266. }
  267. static int __init led_nu801_probe(struct platform_device *pdev)
  268. {
  269. struct led_nu801_platform_data *pdata = pdev->dev.platform_data;
  270. struct led_nu801_data *controllers;
  271. int i, ret = 0;
  272. if (!(pdata && pdata->num_controllers)) {
  273. controllers = leds_nu801_create_of(pdev);
  274. if (!controllers)
  275. return -ENODEV;
  276. }
  277. controllers = kzalloc(sizeof(struct led_nu801_data) *
  278. pdata->num_controllers, GFP_KERNEL);
  279. if (!controllers)
  280. return -ENOMEM;
  281. for (i = 0; i < pdata->num_controllers; i++) {
  282. ret = led_nu801_create_chain(&pdata->template[i],
  283. &controllers[i],
  284. &pdev->dev);
  285. if (ret < 0)
  286. goto err;
  287. }
  288. platform_set_drvdata(pdev, controllers);
  289. return 0;
  290. err:
  291. for (i = i - 1; i >= 0; i--)
  292. led_nu801_delete_chain(&controllers[i]);
  293. kfree(controllers);
  294. return ret;
  295. }
  296. static int led_nu801_remove(struct platform_device *pdev)
  297. {
  298. int i;
  299. struct led_nu801_platform_data *pdata = pdev->dev.platform_data;
  300. struct led_nu801_data *controllers;
  301. controllers = platform_get_drvdata(pdev);
  302. for (i = 0; i < pdata->num_controllers; i++)
  303. led_nu801_delete_chain(&controllers[i]);
  304. kfree(controllers);
  305. return 0;
  306. }
  307. static const struct of_device_id of_numen_leds_match[] = {
  308. { .compatible = "numen,leds-nu801", },
  309. {},
  310. };
  311. MODULE_DEVICE_TABLE(of, of_pwm_leds_match);
  312. static struct platform_driver led_nu801_driver = {
  313. .probe = led_nu801_probe,
  314. .remove = led_nu801_remove,
  315. .driver = {
  316. .name = "leds-nu801",
  317. .owner = THIS_MODULE,
  318. .of_match_table = of_numen_leds_match,
  319. },
  320. };
  321. static int __init led_nu801_init(void)
  322. {
  323. return platform_driver_register(&led_nu801_driver);
  324. }
  325. static void __exit led_nu801_exit(void)
  326. {
  327. platform_driver_unregister(&led_nu801_driver);
  328. }
  329. module_init(led_nu801_init);
  330. module_exit(led_nu801_exit);
  331. MODULE_AUTHOR("Kevin Paul Herbert <kph@meraki.net>");
  332. MODULE_DESCRIPTION("NU801 LED driver");
  333. MODULE_LICENSE("GPL v2");
  334. MODULE_ALIAS("platform:leds-nu801");