leds-adp5520.c 5.2 KB

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