leds-ti-lmu-common.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright 2015 Texas Instruments
  3. // Copyright 2018 Sebastian Reichel
  4. // Copyright 2018 Pavel Machek <pavel@ucw.cz>
  5. // TI LMU LED common framework, based on previous work from
  6. // Milo Kim <milo.kim@ti.com>
  7. #include <linux/bitops.h>
  8. #include <linux/err.h>
  9. #include <linux/of_device.h>
  10. #include <linux/leds-ti-lmu-common.h>
  11. static const unsigned int ramp_table[16] = {2048, 262000, 524000, 1049000,
  12. 2090000, 4194000, 8389000, 16780000, 33550000,
  13. 41940000, 50330000, 58720000, 67110000,
  14. 83880000, 100660000, 117440000};
  15. static int ti_lmu_common_update_brightness(struct ti_lmu_bank *lmu_bank,
  16. int brightness)
  17. {
  18. struct regmap *regmap = lmu_bank->regmap;
  19. u8 reg, val;
  20. int ret;
  21. /*
  22. * Brightness register update
  23. *
  24. * 11 bit dimming: update LSB bits and write MSB byte.
  25. * MSB brightness should be shifted.
  26. * 8 bit dimming: write MSB byte.
  27. */
  28. if (lmu_bank->max_brightness == MAX_BRIGHTNESS_11BIT) {
  29. reg = lmu_bank->lsb_brightness_reg;
  30. ret = regmap_update_bits(regmap, reg,
  31. LMU_11BIT_LSB_MASK,
  32. brightness);
  33. if (ret)
  34. return ret;
  35. val = brightness >> LMU_11BIT_MSB_SHIFT;
  36. } else {
  37. val = brightness;
  38. }
  39. reg = lmu_bank->msb_brightness_reg;
  40. return regmap_write(regmap, reg, val);
  41. }
  42. int ti_lmu_common_set_brightness(struct ti_lmu_bank *lmu_bank, int brightness)
  43. {
  44. return ti_lmu_common_update_brightness(lmu_bank, brightness);
  45. }
  46. EXPORT_SYMBOL(ti_lmu_common_set_brightness);
  47. static unsigned int ti_lmu_common_convert_ramp_to_index(unsigned int usec)
  48. {
  49. int size = ARRAY_SIZE(ramp_table);
  50. int i;
  51. if (usec <= ramp_table[0])
  52. return 0;
  53. if (usec > ramp_table[size - 1])
  54. return size - 1;
  55. for (i = 1; i < size; i++) {
  56. if (usec == ramp_table[i])
  57. return i;
  58. /* Find an approximate index by looking up the table */
  59. if (usec > ramp_table[i - 1] && usec < ramp_table[i]) {
  60. if (usec - ramp_table[i - 1] < ramp_table[i] - usec)
  61. return i - 1;
  62. else
  63. return i;
  64. }
  65. }
  66. return 0;
  67. }
  68. int ti_lmu_common_set_ramp(struct ti_lmu_bank *lmu_bank)
  69. {
  70. struct regmap *regmap = lmu_bank->regmap;
  71. u8 ramp, ramp_up, ramp_down;
  72. if (lmu_bank->ramp_up_usec == 0 && lmu_bank->ramp_down_usec == 0) {
  73. ramp_up = 0;
  74. ramp_down = 0;
  75. } else {
  76. ramp_up = ti_lmu_common_convert_ramp_to_index(lmu_bank->ramp_up_usec);
  77. ramp_down = ti_lmu_common_convert_ramp_to_index(lmu_bank->ramp_down_usec);
  78. }
  79. ramp = (ramp_up << 4) | ramp_down;
  80. return regmap_write(regmap, lmu_bank->runtime_ramp_reg, ramp);
  81. }
  82. EXPORT_SYMBOL(ti_lmu_common_set_ramp);
  83. int ti_lmu_common_get_ramp_params(struct device *dev,
  84. struct fwnode_handle *child,
  85. struct ti_lmu_bank *lmu_data)
  86. {
  87. int ret;
  88. ret = fwnode_property_read_u32(child, "ramp-up-us",
  89. &lmu_data->ramp_up_usec);
  90. if (ret)
  91. dev_warn(dev, "ramp-up-us property missing\n");
  92. ret = fwnode_property_read_u32(child, "ramp-down-us",
  93. &lmu_data->ramp_down_usec);
  94. if (ret)
  95. dev_warn(dev, "ramp-down-us property missing\n");
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(ti_lmu_common_get_ramp_params);
  99. int ti_lmu_common_get_brt_res(struct device *dev, struct fwnode_handle *child,
  100. struct ti_lmu_bank *lmu_data)
  101. {
  102. int ret;
  103. ret = device_property_read_u32(dev, "ti,brightness-resolution",
  104. &lmu_data->max_brightness);
  105. if (ret)
  106. ret = fwnode_property_read_u32(child,
  107. "ti,brightness-resolution",
  108. &lmu_data->max_brightness);
  109. if (lmu_data->max_brightness <= 0) {
  110. lmu_data->max_brightness = MAX_BRIGHTNESS_8BIT;
  111. return ret;
  112. }
  113. if (lmu_data->max_brightness > MAX_BRIGHTNESS_11BIT)
  114. lmu_data->max_brightness = MAX_BRIGHTNESS_11BIT;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(ti_lmu_common_get_brt_res);
  118. MODULE_DESCRIPTION("TI LMU common LED framework");
  119. MODULE_AUTHOR("Sebastian Reichel");
  120. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
  121. MODULE_LICENSE("GPL v2");
  122. MODULE_ALIAS("ti-lmu-led-common");