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>
8 #include <linux/bitops.h>
10 #include <linux/of_device.h>
12 #include <linux/leds-ti-lmu-common.h>
14 const static int ramp_table[16] = {2048, 262000, 524000, 1049000, 2090000,
15 4194000, 8389000, 16780000, 33550000, 41940000,
16 50330000, 58720000, 67110000, 83880000,
17 100660000, 117440000};
19 static int ti_lmu_common_update_brightness(struct ti_lmu_bank *lmu_bank,
22 struct regmap *regmap = lmu_bank->regmap;
27 * Brightness register update
29 * 11 bit dimming: update LSB bits and write MSB byte.
30 * MSB brightness should be shifted.
31 * 8 bit dimming: write MSB byte.
33 if (lmu_bank->max_brightness == MAX_BRIGHTNESS_11BIT) {
34 reg = lmu_bank->lsb_brightness_reg;
35 ret = regmap_update_bits(regmap, reg,
41 val = brightness >> LMU_11BIT_MSB_SHIFT;
46 reg = lmu_bank->msb_brightness_reg;
48 return regmap_write(regmap, reg, val);
51 int ti_lmu_common_set_brightness(struct ti_lmu_bank *lmu_bank, int brightness)
53 return ti_lmu_common_update_brightness(lmu_bank, brightness);
55 EXPORT_SYMBOL(ti_lmu_common_set_brightness);
57 static int ti_lmu_common_convert_ramp_to_index(unsigned int usec)
59 int size = ARRAY_SIZE(ramp_table);
62 if (usec <= ramp_table[0])
65 if (usec > ramp_table[size - 1])
68 for (i = 1; i < size; i++) {
69 if (usec == ramp_table[i])
72 /* Find an approximate index by looking up the table */
73 if (usec > ramp_table[i - 1] && usec < ramp_table[i]) {
74 if (usec - ramp_table[i - 1] < ramp_table[i] - usec)
84 int ti_lmu_common_set_ramp(struct ti_lmu_bank *lmu_bank)
86 struct regmap *regmap = lmu_bank->regmap;
87 u8 ramp, ramp_up, ramp_down;
89 if (lmu_bank->ramp_up_usec == 0 && lmu_bank->ramp_down_usec == 0) {
93 ramp_up = ti_lmu_common_convert_ramp_to_index(lmu_bank->ramp_up_usec);
94 ramp_down = ti_lmu_common_convert_ramp_to_index(lmu_bank->ramp_down_usec);
97 if (ramp_up < 0 || ramp_down < 0)
100 ramp = (ramp_up << 4) | ramp_down;
102 return regmap_write(regmap, lmu_bank->runtime_ramp_reg, ramp);
105 EXPORT_SYMBOL(ti_lmu_common_set_ramp);
107 int ti_lmu_common_get_ramp_params(struct device *dev,
108 struct fwnode_handle *child,
109 struct ti_lmu_bank *lmu_data)
113 ret = fwnode_property_read_u32(child, "ramp-up-us",
114 &lmu_data->ramp_up_usec);
116 dev_warn(dev, "ramp-up-us property missing\n");
119 ret = fwnode_property_read_u32(child, "ramp-down-us",
120 &lmu_data->ramp_down_usec);
122 dev_warn(dev, "ramp-down-us property missing\n");
126 EXPORT_SYMBOL(ti_lmu_common_get_ramp_params);
128 int ti_lmu_common_get_brt_res(struct device *dev, struct fwnode_handle *child,
129 struct ti_lmu_bank *lmu_data)
133 ret = device_property_read_u32(dev, "ti,brightness-resolution",
134 &lmu_data->max_brightness);
136 ret = fwnode_property_read_u32(child,
137 "ti,brightness-resolution",
138 &lmu_data->max_brightness);
139 if (lmu_data->max_brightness <= 0) {
140 lmu_data->max_brightness = MAX_BRIGHTNESS_8BIT;
144 if (lmu_data->max_brightness > MAX_BRIGHTNESS_11BIT)
145 lmu_data->max_brightness = MAX_BRIGHTNESS_11BIT;
150 EXPORT_SYMBOL(ti_lmu_common_get_brt_res);
152 MODULE_DESCRIPTION("TI LMU common LED framework");
153 MODULE_AUTHOR("Sebastian Reichel");
154 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
155 MODULE_LICENSE("GPL v2");
156 MODULE_ALIAS("ti-lmu-led-common");