ktd253-backlight.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Backlight driver for the Kinetic KTD253
  4. * Based on code and know-how from the Samsung GT-S7710
  5. * Gareth Phillips <gareth.phillips@samsung.com>
  6. */
  7. #include <linux/backlight.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/limits.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/property.h>
  18. #include <linux/slab.h>
  19. /* Current ratio is n/32 from 1/32 to 32/32 */
  20. #define KTD253_MIN_RATIO 1
  21. #define KTD253_MAX_RATIO 32
  22. #define KTD253_DEFAULT_RATIO 13
  23. #define KTD253_T_LOW_NS (200 + 10) /* Additional 10ns as safety factor */
  24. #define KTD253_T_HIGH_NS (200 + 10) /* Additional 10ns as safety factor */
  25. #define KTD253_T_OFF_CRIT_NS 100000 /* 100 us, now it doesn't look good */
  26. #define KTD253_T_OFF_MS 3
  27. struct ktd253_backlight {
  28. struct device *dev;
  29. struct backlight_device *bl;
  30. struct gpio_desc *gpiod;
  31. u16 ratio;
  32. };
  33. static void ktd253_backlight_set_max_ratio(struct ktd253_backlight *ktd253)
  34. {
  35. gpiod_set_value_cansleep(ktd253->gpiod, 1);
  36. ndelay(KTD253_T_HIGH_NS);
  37. /* We always fall back to this when we power on */
  38. }
  39. static int ktd253_backlight_stepdown(struct ktd253_backlight *ktd253)
  40. {
  41. /*
  42. * These GPIO operations absolutely can NOT sleep so no _cansleep
  43. * suffixes, and no using GPIO expanders on slow buses for this!
  44. *
  45. * The maximum number of cycles of the loop is 32 so the time taken
  46. * should nominally be:
  47. * (T_LOW_NS + T_HIGH_NS + loop_time) * 32
  48. *
  49. * Architectures do not always support ndelay() and we will get a few us
  50. * instead. If we get to a critical time limit an interrupt has likely
  51. * occured in the low part of the loop and we need to restart from the
  52. * top so we have the backlight in a known state.
  53. */
  54. u64 ns;
  55. ns = ktime_get_ns();
  56. gpiod_set_value(ktd253->gpiod, 0);
  57. ndelay(KTD253_T_LOW_NS);
  58. gpiod_set_value(ktd253->gpiod, 1);
  59. ns = ktime_get_ns() - ns;
  60. if (ns >= KTD253_T_OFF_CRIT_NS) {
  61. dev_err(ktd253->dev, "PCM on backlight took too long (%llu ns)\n", ns);
  62. return -EAGAIN;
  63. }
  64. ndelay(KTD253_T_HIGH_NS);
  65. return 0;
  66. }
  67. static int ktd253_backlight_update_status(struct backlight_device *bl)
  68. {
  69. struct ktd253_backlight *ktd253 = bl_get_data(bl);
  70. int brightness = backlight_get_brightness(bl);
  71. u16 target_ratio;
  72. u16 current_ratio = ktd253->ratio;
  73. int ret;
  74. dev_dbg(ktd253->dev, "new brightness/ratio: %d/32\n", brightness);
  75. target_ratio = brightness;
  76. if (target_ratio == current_ratio)
  77. /* This is already right */
  78. return 0;
  79. if (target_ratio == 0) {
  80. gpiod_set_value_cansleep(ktd253->gpiod, 0);
  81. /*
  82. * We need to keep the GPIO low for at least this long
  83. * to actually switch the KTD253 off.
  84. */
  85. msleep(KTD253_T_OFF_MS);
  86. ktd253->ratio = 0;
  87. return 0;
  88. }
  89. if (current_ratio == 0) {
  90. ktd253_backlight_set_max_ratio(ktd253);
  91. current_ratio = KTD253_MAX_RATIO;
  92. }
  93. while (current_ratio != target_ratio) {
  94. /*
  95. * These GPIO operations absolutely can NOT sleep so no
  96. * _cansleep suffixes, and no using GPIO expanders on
  97. * slow buses for this!
  98. */
  99. ret = ktd253_backlight_stepdown(ktd253);
  100. if (ret == -EAGAIN) {
  101. /*
  102. * Something disturbed the backlight setting code when
  103. * running so we need to bring the PWM back to a known
  104. * state. This shouldn't happen too much.
  105. */
  106. gpiod_set_value_cansleep(ktd253->gpiod, 0);
  107. msleep(KTD253_T_OFF_MS);
  108. ktd253_backlight_set_max_ratio(ktd253);
  109. current_ratio = KTD253_MAX_RATIO;
  110. } else if (current_ratio == KTD253_MIN_RATIO) {
  111. /* After 1/32 we loop back to 32/32 */
  112. current_ratio = KTD253_MAX_RATIO;
  113. } else {
  114. current_ratio--;
  115. }
  116. }
  117. ktd253->ratio = current_ratio;
  118. dev_dbg(ktd253->dev, "new ratio set to %d/32\n", target_ratio);
  119. return 0;
  120. }
  121. static const struct backlight_ops ktd253_backlight_ops = {
  122. .options = BL_CORE_SUSPENDRESUME,
  123. .update_status = ktd253_backlight_update_status,
  124. };
  125. static int ktd253_backlight_probe(struct platform_device *pdev)
  126. {
  127. struct device *dev = &pdev->dev;
  128. struct backlight_device *bl;
  129. struct ktd253_backlight *ktd253;
  130. u32 max_brightness;
  131. u32 brightness;
  132. int ret;
  133. ktd253 = devm_kzalloc(dev, sizeof(*ktd253), GFP_KERNEL);
  134. if (!ktd253)
  135. return -ENOMEM;
  136. ktd253->dev = dev;
  137. ret = device_property_read_u32(dev, "max-brightness", &max_brightness);
  138. if (ret)
  139. max_brightness = KTD253_MAX_RATIO;
  140. if (max_brightness > KTD253_MAX_RATIO) {
  141. /* Clamp brightness to hardware max */
  142. dev_err(dev, "illegal max brightness specified\n");
  143. max_brightness = KTD253_MAX_RATIO;
  144. }
  145. ret = device_property_read_u32(dev, "default-brightness", &brightness);
  146. if (ret)
  147. brightness = KTD253_DEFAULT_RATIO;
  148. if (brightness > max_brightness) {
  149. /* Clamp default brightness to max brightness */
  150. dev_err(dev, "default brightness exceeds max brightness\n");
  151. brightness = max_brightness;
  152. }
  153. ktd253->gpiod = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
  154. if (IS_ERR(ktd253->gpiod))
  155. return dev_err_probe(dev, PTR_ERR(ktd253->gpiod),
  156. "gpio line missing or invalid.\n");
  157. gpiod_set_consumer_name(ktd253->gpiod, dev_name(dev));
  158. /* Bring backlight to a known off state */
  159. msleep(KTD253_T_OFF_MS);
  160. bl = devm_backlight_device_register(dev, dev_name(dev), dev, ktd253,
  161. &ktd253_backlight_ops, NULL);
  162. if (IS_ERR(bl)) {
  163. dev_err(dev, "failed to register backlight\n");
  164. return PTR_ERR(bl);
  165. }
  166. bl->props.max_brightness = max_brightness;
  167. /* When we just enable the GPIO line we set max brightness */
  168. if (brightness) {
  169. bl->props.brightness = brightness;
  170. bl->props.power = BACKLIGHT_POWER_ON;
  171. } else {
  172. bl->props.brightness = 0;
  173. bl->props.power = BACKLIGHT_POWER_OFF;
  174. }
  175. ktd253->bl = bl;
  176. platform_set_drvdata(pdev, bl);
  177. backlight_update_status(bl);
  178. return 0;
  179. }
  180. static const struct of_device_id ktd253_backlight_of_match[] = {
  181. { .compatible = "kinetic,ktd253" },
  182. { .compatible = "kinetic,ktd259" },
  183. { /* sentinel */ }
  184. };
  185. MODULE_DEVICE_TABLE(of, ktd253_backlight_of_match);
  186. static struct platform_driver ktd253_backlight_driver = {
  187. .driver = {
  188. .name = "ktd253-backlight",
  189. .of_match_table = ktd253_backlight_of_match,
  190. },
  191. .probe = ktd253_backlight_probe,
  192. };
  193. module_platform_driver(ktd253_backlight_driver);
  194. MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
  195. MODULE_DESCRIPTION("Kinetic KTD253 Backlight Driver");
  196. MODULE_LICENSE("GPL");
  197. MODULE_ALIAS("platform:ktd253-backlight");