leds-adp5520.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * LEDs driver for Analog Devices ADP5520/ADP5501 MFD PMICs
  3. *
  4. * Copyright 2009 Analog Devices Inc.
  5. *
  6. * Loosely derived from 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. * Licensed under the GPL-2 or later.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/leds.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/mfd/adp5520.h>
  22. #include <linux/slab.h>
  23. struct adp5520_led {
  24. struct led_classdev cdev;
  25. struct work_struct work;
  26. struct device *master;
  27. enum led_brightness new_brightness;
  28. int id;
  29. int flags;
  30. };
  31. static void adp5520_led_work(struct work_struct *work)
  32. {
  33. struct adp5520_led *led = container_of(work, struct adp5520_led, work);
  34. adp5520_write(led->master, ADP5520_LED1_CURRENT + led->id - 1,
  35. led->new_brightness >> 2);
  36. }
  37. static void adp5520_led_set(struct led_classdev *led_cdev,
  38. enum led_brightness value)
  39. {
  40. struct adp5520_led *led;
  41. led = container_of(led_cdev, struct adp5520_led, cdev);
  42. led->new_brightness = value;
  43. schedule_work(&led->work);
  44. }
  45. static int adp5520_led_setup(struct adp5520_led *led)
  46. {
  47. struct device *dev = led->master;
  48. int flags = led->flags;
  49. int ret = 0;
  50. switch (led->id) {
  51. case FLAG_ID_ADP5520_LED1_ADP5501_LED0:
  52. ret |= adp5520_set_bits(dev, ADP5520_LED_TIME,
  53. (flags >> ADP5520_FLAG_OFFT_SHIFT) &
  54. ADP5520_FLAG_OFFT_MASK);
  55. ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
  56. ADP5520_LED1_EN);
  57. break;
  58. case FLAG_ID_ADP5520_LED2_ADP5501_LED1:
  59. ret |= adp5520_set_bits(dev, ADP5520_LED_TIME,
  60. ((flags >> ADP5520_FLAG_OFFT_SHIFT) &
  61. ADP5520_FLAG_OFFT_MASK) << 2);
  62. ret |= adp5520_clr_bits(dev, ADP5520_LED_CONTROL,
  63. ADP5520_R3_MODE);
  64. ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
  65. ADP5520_LED2_EN);
  66. break;
  67. case FLAG_ID_ADP5520_LED3_ADP5501_LED2:
  68. ret |= adp5520_set_bits(dev, ADP5520_LED_TIME,
  69. ((flags >> ADP5520_FLAG_OFFT_SHIFT) &
  70. ADP5520_FLAG_OFFT_MASK) << 4);
  71. ret |= adp5520_clr_bits(dev, ADP5520_LED_CONTROL,
  72. ADP5520_C3_MODE);
  73. ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
  74. ADP5520_LED3_EN);
  75. break;
  76. }
  77. return ret;
  78. }
  79. static int __devinit adp5520_led_prepare(struct platform_device *pdev)
  80. {
  81. struct adp5520_leds_platform_data *pdata = pdev->dev.platform_data;
  82. struct device *dev = pdev->dev.parent;
  83. int ret = 0;
  84. ret |= adp5520_write(dev, ADP5520_LED1_CURRENT, 0);
  85. ret |= adp5520_write(dev, ADP5520_LED2_CURRENT, 0);
  86. ret |= adp5520_write(dev, ADP5520_LED3_CURRENT, 0);
  87. ret |= adp5520_write(dev, ADP5520_LED_TIME, pdata->led_on_time << 6);
  88. ret |= adp5520_write(dev, ADP5520_LED_FADE, FADE_VAL(pdata->fade_in,
  89. pdata->fade_out));
  90. return ret;
  91. }
  92. static int __devinit adp5520_led_probe(struct platform_device *pdev)
  93. {
  94. struct adp5520_leds_platform_data *pdata = pdev->dev.platform_data;
  95. struct adp5520_led *led, *led_dat;
  96. struct led_info *cur_led;
  97. int ret, i;
  98. if (pdata == NULL) {
  99. dev_err(&pdev->dev, "missing platform data\n");
  100. return -ENODEV;
  101. }
  102. if (pdata->num_leds > ADP5520_01_MAXLEDS) {
  103. dev_err(&pdev->dev, "can't handle more than %d LEDS\n",
  104. ADP5520_01_MAXLEDS);
  105. return -EFAULT;
  106. }
  107. led = kzalloc(sizeof(*led) * pdata->num_leds, GFP_KERNEL);
  108. if (led == NULL) {
  109. dev_err(&pdev->dev, "failed to alloc memory\n");
  110. return -ENOMEM;
  111. }
  112. ret = adp5520_led_prepare(pdev);
  113. if (ret) {
  114. dev_err(&pdev->dev, "failed to write\n");
  115. goto err_free;
  116. }
  117. for (i = 0; i < pdata->num_leds; ++i) {
  118. cur_led = &pdata->leds[i];
  119. led_dat = &led[i];
  120. led_dat->cdev.name = cur_led->name;
  121. led_dat->cdev.default_trigger = cur_led->default_trigger;
  122. led_dat->cdev.brightness_set = adp5520_led_set;
  123. led_dat->cdev.brightness = LED_OFF;
  124. if (cur_led->flags & ADP5520_FLAG_LED_MASK)
  125. led_dat->flags = cur_led->flags;
  126. else
  127. led_dat->flags = i + 1;
  128. led_dat->id = led_dat->flags & ADP5520_FLAG_LED_MASK;
  129. led_dat->master = pdev->dev.parent;
  130. led_dat->new_brightness = LED_OFF;
  131. INIT_WORK(&led_dat->work, adp5520_led_work);
  132. ret = led_classdev_register(led_dat->master, &led_dat->cdev);
  133. if (ret) {
  134. dev_err(&pdev->dev, "failed to register LED %d\n",
  135. led_dat->id);
  136. goto err;
  137. }
  138. ret = adp5520_led_setup(led_dat);
  139. if (ret) {
  140. dev_err(&pdev->dev, "failed to write\n");
  141. i++;
  142. goto err;
  143. }
  144. }
  145. platform_set_drvdata(pdev, led);
  146. return 0;
  147. err:
  148. if (i > 0) {
  149. for (i = i - 1; i >= 0; i--) {
  150. led_classdev_unregister(&led[i].cdev);
  151. cancel_work_sync(&led[i].work);
  152. }
  153. }
  154. err_free:
  155. kfree(led);
  156. return ret;
  157. }
  158. static int __devexit adp5520_led_remove(struct platform_device *pdev)
  159. {
  160. struct adp5520_leds_platform_data *pdata = pdev->dev.platform_data;
  161. struct adp5520_led *led;
  162. int i;
  163. led = platform_get_drvdata(pdev);
  164. adp5520_clr_bits(led->master, ADP5520_LED_CONTROL,
  165. ADP5520_LED1_EN | ADP5520_LED2_EN | ADP5520_LED3_EN);
  166. for (i = 0; i < pdata->num_leds; i++) {
  167. led_classdev_unregister(&led[i].cdev);
  168. cancel_work_sync(&led[i].work);
  169. }
  170. kfree(led);
  171. return 0;
  172. }
  173. static struct platform_driver adp5520_led_driver = {
  174. .driver = {
  175. .name = "adp5520-led",
  176. .owner = THIS_MODULE,
  177. },
  178. .probe = adp5520_led_probe,
  179. .remove = __devexit_p(adp5520_led_remove),
  180. };
  181. static int __init adp5520_led_init(void)
  182. {
  183. return platform_driver_register(&adp5520_led_driver);
  184. }
  185. module_init(adp5520_led_init);
  186. static void __exit adp5520_led_exit(void)
  187. {
  188. platform_driver_unregister(&adp5520_led_driver);
  189. }
  190. module_exit(adp5520_led_exit);
  191. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  192. MODULE_DESCRIPTION("LEDS ADP5520(01) Driver");
  193. MODULE_LICENSE("GPL");
  194. MODULE_ALIAS("platform:adp5520-led");