leds-lt3593.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * LEDs driver for LT3593 controllers
  3. *
  4. * See the datasheet at http://cds.linear.com/docs/Datasheet/3593f.pdf
  5. *
  6. * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  7. *
  8. * Based on leds-gpio.c,
  9. *
  10. * Copyright (C) 2007 8D Technologies inc.
  11. * Raphael Assenat <raph@8d.com>
  12. * Copyright (C) 2008 Freescale Semiconductor, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/leds.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/delay.h>
  23. #include <linux/gpio.h>
  24. #include <linux/slab.h>
  25. #include <linux/module.h>
  26. struct lt3593_led_data {
  27. struct led_classdev cdev;
  28. unsigned gpio;
  29. struct work_struct work;
  30. u8 new_level;
  31. };
  32. static void lt3593_led_work(struct work_struct *work)
  33. {
  34. int pulses;
  35. struct lt3593_led_data *led_dat =
  36. container_of(work, struct lt3593_led_data, work);
  37. /*
  38. * The LT3593 resets its internal current level register to the maximum
  39. * level on the first falling edge on the control pin. Each following
  40. * falling edge decreases the current level by 625uA. Up to 32 pulses
  41. * can be sent, so the maximum power reduction is 20mA.
  42. * After a timeout of 128us, the value is taken from the register and
  43. * applied is to the output driver.
  44. */
  45. if (led_dat->new_level == 0) {
  46. gpio_set_value_cansleep(led_dat->gpio, 0);
  47. return;
  48. }
  49. pulses = 32 - (led_dat->new_level * 32) / 255;
  50. if (pulses == 0) {
  51. gpio_set_value_cansleep(led_dat->gpio, 0);
  52. mdelay(1);
  53. gpio_set_value_cansleep(led_dat->gpio, 1);
  54. return;
  55. }
  56. gpio_set_value_cansleep(led_dat->gpio, 1);
  57. while (pulses--) {
  58. gpio_set_value_cansleep(led_dat->gpio, 0);
  59. udelay(1);
  60. gpio_set_value_cansleep(led_dat->gpio, 1);
  61. udelay(1);
  62. }
  63. }
  64. static void lt3593_led_set(struct led_classdev *led_cdev,
  65. enum led_brightness value)
  66. {
  67. struct lt3593_led_data *led_dat =
  68. container_of(led_cdev, struct lt3593_led_data, cdev);
  69. led_dat->new_level = value;
  70. schedule_work(&led_dat->work);
  71. }
  72. static int create_lt3593_led(const struct gpio_led *template,
  73. struct lt3593_led_data *led_dat, struct device *parent)
  74. {
  75. int ret, state;
  76. /* skip leds on GPIOs that aren't available */
  77. if (!gpio_is_valid(template->gpio)) {
  78. dev_info(parent, "%s: skipping unavailable LT3593 LED at gpio %d (%s)\n",
  79. KBUILD_MODNAME, template->gpio, template->name);
  80. return 0;
  81. }
  82. led_dat->cdev.name = template->name;
  83. led_dat->cdev.default_trigger = template->default_trigger;
  84. led_dat->gpio = template->gpio;
  85. led_dat->cdev.brightness_set = lt3593_led_set;
  86. state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
  87. led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
  88. if (!template->retain_state_suspended)
  89. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  90. ret = devm_gpio_request_one(parent, template->gpio, state ?
  91. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
  92. template->name);
  93. if (ret < 0)
  94. return ret;
  95. INIT_WORK(&led_dat->work, lt3593_led_work);
  96. ret = led_classdev_register(parent, &led_dat->cdev);
  97. if (ret < 0)
  98. return ret;
  99. dev_info(parent, "%s: registered LT3593 LED '%s' at GPIO %d\n",
  100. KBUILD_MODNAME, template->name, template->gpio);
  101. return 0;
  102. }
  103. static void delete_lt3593_led(struct lt3593_led_data *led)
  104. {
  105. if (!gpio_is_valid(led->gpio))
  106. return;
  107. led_classdev_unregister(&led->cdev);
  108. cancel_work_sync(&led->work);
  109. }
  110. static int lt3593_led_probe(struct platform_device *pdev)
  111. {
  112. struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
  113. struct lt3593_led_data *leds_data;
  114. int i, ret = 0;
  115. if (!pdata)
  116. return -EBUSY;
  117. leds_data = devm_kzalloc(&pdev->dev,
  118. sizeof(struct lt3593_led_data) * pdata->num_leds,
  119. GFP_KERNEL);
  120. if (!leds_data)
  121. return -ENOMEM;
  122. for (i = 0; i < pdata->num_leds; i++) {
  123. ret = create_lt3593_led(&pdata->leds[i], &leds_data[i],
  124. &pdev->dev);
  125. if (ret < 0)
  126. goto err;
  127. }
  128. platform_set_drvdata(pdev, leds_data);
  129. return 0;
  130. err:
  131. for (i = i - 1; i >= 0; i--)
  132. delete_lt3593_led(&leds_data[i]);
  133. return ret;
  134. }
  135. static int lt3593_led_remove(struct platform_device *pdev)
  136. {
  137. int i;
  138. struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
  139. struct lt3593_led_data *leds_data;
  140. leds_data = platform_get_drvdata(pdev);
  141. for (i = 0; i < pdata->num_leds; i++)
  142. delete_lt3593_led(&leds_data[i]);
  143. return 0;
  144. }
  145. static struct platform_driver lt3593_led_driver = {
  146. .probe = lt3593_led_probe,
  147. .remove = lt3593_led_remove,
  148. .driver = {
  149. .name = "leds-lt3593",
  150. },
  151. };
  152. module_platform_driver(lt3593_led_driver);
  153. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  154. MODULE_DESCRIPTION("LED driver for LT3593 controllers");
  155. MODULE_LICENSE("GPL");
  156. MODULE_ALIAS("platform:leds-lt3593");