ktd253-backlight.c 6.4 KB

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