leds-mc13783.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * LEDs driver for Freescale MC13783/MC13892/MC34708
  3. *
  4. * Copyright (C) 2010 Philippe Rétornaz
  5. *
  6. * Based on leds-da903x:
  7. * Copyright (C) 2008 Compulab, Ltd.
  8. * Mike Rapoport <mike@compulab.co.il>
  9. *
  10. * Copyright (C) 2006-2008 Marvell International Ltd.
  11. * Eric Miao <eric.miao@marvell.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/leds.h>
  21. #include <linux/of.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/mfd/mc13xxx.h>
  24. struct mc13xxx_led_devtype {
  25. int led_min;
  26. int led_max;
  27. int num_regs;
  28. u32 ledctrl_base;
  29. };
  30. struct mc13xxx_led {
  31. struct led_classdev cdev;
  32. struct work_struct work;
  33. enum led_brightness new_brightness;
  34. int id;
  35. struct mc13xxx_leds *leds;
  36. };
  37. struct mc13xxx_leds {
  38. struct mc13xxx *master;
  39. struct mc13xxx_led_devtype *devtype;
  40. int num_leds;
  41. struct mc13xxx_led *led;
  42. };
  43. static unsigned int mc13xxx_max_brightness(int id)
  44. {
  45. if (id >= MC13783_LED_MD && id <= MC13783_LED_KP)
  46. return 0x0f;
  47. else if (id >= MC13783_LED_R1 && id <= MC13783_LED_B3)
  48. return 0x1f;
  49. return 0x3f;
  50. }
  51. static void mc13xxx_led_work(struct work_struct *work)
  52. {
  53. struct mc13xxx_led *led = container_of(work, struct mc13xxx_led, work);
  54. struct mc13xxx_leds *leds = led->leds;
  55. unsigned int reg, bank, off, shift;
  56. switch (led->id) {
  57. case MC13783_LED_MD:
  58. case MC13783_LED_AD:
  59. case MC13783_LED_KP:
  60. reg = 2;
  61. shift = 9 + (led->id - MC13783_LED_MD) * 4;
  62. break;
  63. case MC13783_LED_R1:
  64. case MC13783_LED_G1:
  65. case MC13783_LED_B1:
  66. case MC13783_LED_R2:
  67. case MC13783_LED_G2:
  68. case MC13783_LED_B2:
  69. case MC13783_LED_R3:
  70. case MC13783_LED_G3:
  71. case MC13783_LED_B3:
  72. off = led->id - MC13783_LED_R1;
  73. bank = off / 3;
  74. reg = 3 + bank;
  75. shift = (off - bank * 3) * 5 + 6;
  76. break;
  77. case MC13892_LED_MD:
  78. case MC13892_LED_AD:
  79. case MC13892_LED_KP:
  80. reg = (led->id - MC13892_LED_MD) / 2;
  81. shift = 3 + (led->id - MC13892_LED_MD) * 12;
  82. break;
  83. case MC13892_LED_R:
  84. case MC13892_LED_G:
  85. case MC13892_LED_B:
  86. off = led->id - MC13892_LED_R;
  87. bank = off / 2;
  88. reg = 2 + bank;
  89. shift = (off - bank * 2) * 12 + 3;
  90. break;
  91. case MC34708_LED_R:
  92. case MC34708_LED_G:
  93. reg = 0;
  94. shift = 3 + (led->id - MC34708_LED_R) * 12;
  95. break;
  96. default:
  97. BUG();
  98. }
  99. mc13xxx_reg_rmw(leds->master, leds->devtype->ledctrl_base + reg,
  100. mc13xxx_max_brightness(led->id) << shift,
  101. led->new_brightness << shift);
  102. }
  103. static void mc13xxx_led_set(struct led_classdev *led_cdev,
  104. enum led_brightness value)
  105. {
  106. struct mc13xxx_led *led =
  107. container_of(led_cdev, struct mc13xxx_led, cdev);
  108. led->new_brightness = value;
  109. schedule_work(&led->work);
  110. }
  111. #ifdef CONFIG_OF
  112. static struct mc13xxx_leds_platform_data __init *mc13xxx_led_probe_dt(
  113. struct platform_device *pdev)
  114. {
  115. struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
  116. struct mc13xxx_leds_platform_data *pdata;
  117. struct device_node *parent, *child;
  118. struct device *dev = &pdev->dev;
  119. int i = 0, ret = -ENODATA;
  120. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  121. if (!pdata)
  122. return ERR_PTR(-ENOMEM);
  123. parent = of_get_child_by_name(dev->parent->of_node, "leds");
  124. if (!parent)
  125. goto out_node_put;
  126. ret = of_property_read_u32_array(parent, "led-control",
  127. pdata->led_control,
  128. leds->devtype->num_regs);
  129. if (ret)
  130. goto out_node_put;
  131. pdata->num_leds = of_get_child_count(parent);
  132. pdata->led = devm_kzalloc(dev, pdata->num_leds * sizeof(*pdata->led),
  133. GFP_KERNEL);
  134. if (!pdata->led) {
  135. ret = -ENOMEM;
  136. goto out_node_put;
  137. }
  138. for_each_child_of_node(parent, child) {
  139. const char *str;
  140. u32 tmp;
  141. if (of_property_read_u32(child, "reg", &tmp))
  142. continue;
  143. pdata->led[i].id = leds->devtype->led_min + tmp;
  144. if (!of_property_read_string(child, "label", &str))
  145. pdata->led[i].name = str;
  146. if (!of_property_read_string(child, "linux,default-trigger",
  147. &str))
  148. pdata->led[i].default_trigger = str;
  149. i++;
  150. }
  151. pdata->num_leds = i;
  152. ret = i > 0 ? 0 : -ENODATA;
  153. out_node_put:
  154. of_node_put(parent);
  155. return ret ? ERR_PTR(ret) : pdata;
  156. }
  157. #else
  158. static inline struct mc13xxx_leds_platform_data __init *mc13xxx_led_probe_dt(
  159. struct platform_device *pdev)
  160. {
  161. return ERR_PTR(-ENOSYS);
  162. }
  163. #endif
  164. static int __init mc13xxx_led_probe(struct platform_device *pdev)
  165. {
  166. struct device *dev = &pdev->dev;
  167. struct mc13xxx_leds_platform_data *pdata = dev_get_platdata(dev);
  168. struct mc13xxx *mcdev = dev_get_drvdata(dev->parent);
  169. struct mc13xxx_led_devtype *devtype =
  170. (struct mc13xxx_led_devtype *)pdev->id_entry->driver_data;
  171. struct mc13xxx_leds *leds;
  172. int i, id, ret = -ENODATA;
  173. u32 init_led = 0;
  174. leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
  175. if (!leds)
  176. return -ENOMEM;
  177. leds->devtype = devtype;
  178. leds->master = mcdev;
  179. platform_set_drvdata(pdev, leds);
  180. if (dev->parent->of_node) {
  181. pdata = mc13xxx_led_probe_dt(pdev);
  182. if (IS_ERR(pdata))
  183. return PTR_ERR(pdata);
  184. } else if (!pdata)
  185. return -ENODATA;
  186. leds->num_leds = pdata->num_leds;
  187. if ((leds->num_leds < 1) ||
  188. (leds->num_leds > (devtype->led_max - devtype->led_min + 1))) {
  189. dev_err(dev, "Invalid LED count %d\n", leds->num_leds);
  190. return -EINVAL;
  191. }
  192. leds->led = devm_kzalloc(dev, leds->num_leds * sizeof(*leds->led),
  193. GFP_KERNEL);
  194. if (!leds->led)
  195. return -ENOMEM;
  196. for (i = 0; i < devtype->num_regs; i++) {
  197. ret = mc13xxx_reg_write(mcdev, leds->devtype->ledctrl_base + i,
  198. pdata->led_control[i]);
  199. if (ret)
  200. return ret;
  201. }
  202. for (i = 0; i < leds->num_leds; i++) {
  203. const char *name, *trig;
  204. ret = -EINVAL;
  205. id = pdata->led[i].id;
  206. name = pdata->led[i].name;
  207. trig = pdata->led[i].default_trigger;
  208. if ((id > devtype->led_max) || (id < devtype->led_min)) {
  209. dev_err(dev, "Invalid ID %i\n", id);
  210. break;
  211. }
  212. if (init_led & (1 << id)) {
  213. dev_warn(dev, "LED %i already initialized\n", id);
  214. break;
  215. }
  216. init_led |= 1 << id;
  217. leds->led[i].id = id;
  218. leds->led[i].leds = leds;
  219. leds->led[i].cdev.name = name;
  220. leds->led[i].cdev.default_trigger = trig;
  221. leds->led[i].cdev.flags = LED_CORE_SUSPENDRESUME;
  222. leds->led[i].cdev.brightness_set = mc13xxx_led_set;
  223. leds->led[i].cdev.max_brightness = mc13xxx_max_brightness(id);
  224. INIT_WORK(&leds->led[i].work, mc13xxx_led_work);
  225. ret = led_classdev_register(dev->parent, &leds->led[i].cdev);
  226. if (ret) {
  227. dev_err(dev, "Failed to register LED %i\n", id);
  228. break;
  229. }
  230. }
  231. if (ret)
  232. while (--i >= 0) {
  233. led_classdev_unregister(&leds->led[i].cdev);
  234. cancel_work_sync(&leds->led[i].work);
  235. }
  236. return ret;
  237. }
  238. static int mc13xxx_led_remove(struct platform_device *pdev)
  239. {
  240. struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
  241. int i;
  242. for (i = 0; i < leds->num_leds; i++) {
  243. led_classdev_unregister(&leds->led[i].cdev);
  244. cancel_work_sync(&leds->led[i].work);
  245. }
  246. return 0;
  247. }
  248. static const struct mc13xxx_led_devtype mc13783_led_devtype = {
  249. .led_min = MC13783_LED_MD,
  250. .led_max = MC13783_LED_B3,
  251. .num_regs = 6,
  252. .ledctrl_base = 51,
  253. };
  254. static const struct mc13xxx_led_devtype mc13892_led_devtype = {
  255. .led_min = MC13892_LED_MD,
  256. .led_max = MC13892_LED_B,
  257. .num_regs = 4,
  258. .ledctrl_base = 51,
  259. };
  260. static const struct mc13xxx_led_devtype mc34708_led_devtype = {
  261. .led_min = MC34708_LED_R,
  262. .led_max = MC34708_LED_G,
  263. .num_regs = 1,
  264. .ledctrl_base = 54,
  265. };
  266. static const struct platform_device_id mc13xxx_led_id_table[] = {
  267. { "mc13783-led", (kernel_ulong_t)&mc13783_led_devtype, },
  268. { "mc13892-led", (kernel_ulong_t)&mc13892_led_devtype, },
  269. { "mc34708-led", (kernel_ulong_t)&mc34708_led_devtype, },
  270. { }
  271. };
  272. MODULE_DEVICE_TABLE(platform, mc13xxx_led_id_table);
  273. static struct platform_driver mc13xxx_led_driver = {
  274. .driver = {
  275. .name = "mc13xxx-led",
  276. },
  277. .remove = mc13xxx_led_remove,
  278. .id_table = mc13xxx_led_id_table,
  279. };
  280. module_platform_driver_probe(mc13xxx_led_driver, mc13xxx_led_probe);
  281. MODULE_DESCRIPTION("LEDs driver for Freescale MC13XXX PMIC");
  282. MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
  283. MODULE_LICENSE("GPL");