leds-adp5520.c 4.8 KB

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