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