leds-lt3593.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/init.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/leds.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/delay.h>
  24. #include <linux/gpio.h>
  25. #include <linux/slab.h>
  26. #include <linux/module.h>
  27. struct lt3593_led_data {
  28. struct led_classdev cdev;
  29. unsigned gpio;
  30. struct work_struct work;
  31. u8 new_level;
  32. };
  33. static void lt3593_led_work(struct work_struct *work)
  34. {
  35. int pulses;
  36. struct lt3593_led_data *led_dat =
  37. container_of(work, struct lt3593_led_data, work);
  38. /*
  39. * The LT3593 resets its internal current level register to the maximum
  40. * level on the first falling edge on the control pin. Each following
  41. * falling edge decreases the current level by 625uA. Up to 32 pulses
  42. * can be sent, so the maximum power reduction is 20mA.
  43. * After a timeout of 128us, the value is taken from the register and
  44. * applied is to the output driver.
  45. */
  46. if (led_dat->new_level == 0) {
  47. gpio_set_value_cansleep(led_dat->gpio, 0);
  48. return;
  49. }
  50. pulses = 32 - (led_dat->new_level * 32) / 255;
  51. if (pulses == 0) {
  52. gpio_set_value_cansleep(led_dat->gpio, 0);
  53. mdelay(1);
  54. gpio_set_value_cansleep(led_dat->gpio, 1);
  55. return;
  56. }
  57. gpio_set_value_cansleep(led_dat->gpio, 1);
  58. while (pulses--) {
  59. gpio_set_value_cansleep(led_dat->gpio, 0);
  60. udelay(1);
  61. gpio_set_value_cansleep(led_dat->gpio, 1);
  62. udelay(1);
  63. }
  64. }
  65. static void lt3593_led_set(struct led_classdev *led_cdev,
  66. enum led_brightness value)
  67. {
  68. struct lt3593_led_data *led_dat =
  69. container_of(led_cdev, struct lt3593_led_data, cdev);
  70. led_dat->new_level = value;
  71. schedule_work(&led_dat->work);
  72. }
  73. static int __devinit create_lt3593_led(const struct gpio_led *template,
  74. struct lt3593_led_data *led_dat, struct device *parent)
  75. {
  76. int ret, state;
  77. /* skip leds on GPIOs that aren't available */
  78. if (!gpio_is_valid(template->gpio)) {
  79. printk(KERN_INFO "%s: skipping unavailable LT3593 LED at gpio %d (%s)\n",
  80. KBUILD_MODNAME, template->gpio, template->name);
  81. return 0;
  82. }
  83. ret = gpio_request(template->gpio, template->name);
  84. if (ret < 0)
  85. return ret;
  86. led_dat->cdev.name = template->name;
  87. led_dat->cdev.default_trigger = template->default_trigger;
  88. led_dat->gpio = template->gpio;
  89. led_dat->cdev.brightness_set = lt3593_led_set;
  90. state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
  91. led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
  92. if (!template->retain_state_suspended)
  93. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  94. ret = gpio_direction_output(led_dat->gpio, state);
  95. if (ret < 0)
  96. goto err;
  97. INIT_WORK(&led_dat->work, lt3593_led_work);
  98. ret = led_classdev_register(parent, &led_dat->cdev);
  99. if (ret < 0)
  100. goto err;
  101. printk(KERN_INFO "%s: registered LT3593 LED '%s' at GPIO %d\n",
  102. KBUILD_MODNAME, template->name, template->gpio);
  103. return 0;
  104. err:
  105. gpio_free(led_dat->gpio);
  106. return ret;
  107. }
  108. static void delete_lt3593_led(struct lt3593_led_data *led)
  109. {
  110. if (!gpio_is_valid(led->gpio))
  111. return;
  112. led_classdev_unregister(&led->cdev);
  113. cancel_work_sync(&led->work);
  114. gpio_free(led->gpio);
  115. }
  116. static int __devinit lt3593_led_probe(struct platform_device *pdev)
  117. {
  118. struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
  119. struct lt3593_led_data *leds_data;
  120. int i, ret = 0;
  121. if (!pdata)
  122. return -EBUSY;
  123. leds_data = kzalloc(sizeof(struct lt3593_led_data) * pdata->num_leds,
  124. GFP_KERNEL);
  125. if (!leds_data)
  126. return -ENOMEM;
  127. for (i = 0; i < pdata->num_leds; i++) {
  128. ret = create_lt3593_led(&pdata->leds[i], &leds_data[i],
  129. &pdev->dev);
  130. if (ret < 0)
  131. goto err;
  132. }
  133. platform_set_drvdata(pdev, leds_data);
  134. return 0;
  135. err:
  136. for (i = i - 1; i >= 0; i--)
  137. delete_lt3593_led(&leds_data[i]);
  138. kfree(leds_data);
  139. return ret;
  140. }
  141. static int __devexit lt3593_led_remove(struct platform_device *pdev)
  142. {
  143. int i;
  144. struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
  145. struct lt3593_led_data *leds_data;
  146. leds_data = platform_get_drvdata(pdev);
  147. for (i = 0; i < pdata->num_leds; i++)
  148. delete_lt3593_led(&leds_data[i]);
  149. kfree(leds_data);
  150. return 0;
  151. }
  152. static struct platform_driver lt3593_led_driver = {
  153. .probe = lt3593_led_probe,
  154. .remove = __devexit_p(lt3593_led_remove),
  155. .driver = {
  156. .name = "leds-lt3593",
  157. .owner = THIS_MODULE,
  158. },
  159. };
  160. module_platform_driver(lt3593_led_driver);
  161. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  162. MODULE_DESCRIPTION("LED driver for LT3593 controllers");
  163. MODULE_LICENSE("GPL");
  164. MODULE_ALIAS("platform:leds-lt3593");