leds-mc13783.c 7.5 KB

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