leds-lt3593.c 4.3 KB

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