xref: /linux/drivers/leds/leds-ti-lmu-common.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
13fce8e1eSDan Murphy // SPDX-License-Identifier: GPL-2.0
23fce8e1eSDan Murphy // Copyright 2015 Texas Instruments
33fce8e1eSDan Murphy // Copyright 2018 Sebastian Reichel
43fce8e1eSDan Murphy // Copyright 2018 Pavel Machek <pavel@ucw.cz>
53fce8e1eSDan Murphy // TI LMU LED common framework, based on previous work from
63fce8e1eSDan Murphy // Milo Kim <milo.kim@ti.com>
73fce8e1eSDan Murphy 
83fce8e1eSDan Murphy #include <linux/bitops.h>
93fce8e1eSDan Murphy #include <linux/err.h>
10*3192f141SRob Herring #include <linux/property.h>
113fce8e1eSDan Murphy 
123fce8e1eSDan Murphy #include <linux/leds-ti-lmu-common.h>
133fce8e1eSDan Murphy 
14ba7eb84fSKrzysztof Wilczynski static const unsigned int ramp_table[16] = {2048, 262000, 524000, 1049000,
152637fd43SDan Murphy 				2090000, 4194000, 8389000, 16780000, 33550000,
162637fd43SDan Murphy 				41940000, 50330000, 58720000, 67110000,
172637fd43SDan Murphy 				83880000, 100660000, 117440000};
183fce8e1eSDan Murphy 
ti_lmu_common_update_brightness(struct ti_lmu_bank * lmu_bank,int brightness)193fce8e1eSDan Murphy static int ti_lmu_common_update_brightness(struct ti_lmu_bank *lmu_bank,
203fce8e1eSDan Murphy 					   int brightness)
213fce8e1eSDan Murphy {
223fce8e1eSDan Murphy 	struct regmap *regmap = lmu_bank->regmap;
233fce8e1eSDan Murphy 	u8 reg, val;
243fce8e1eSDan Murphy 	int ret;
253fce8e1eSDan Murphy 
263fce8e1eSDan Murphy 	/*
273fce8e1eSDan Murphy 	 * Brightness register update
283fce8e1eSDan Murphy 	 *
293fce8e1eSDan Murphy 	 * 11 bit dimming: update LSB bits and write MSB byte.
303fce8e1eSDan Murphy 	 *		   MSB brightness should be shifted.
313fce8e1eSDan Murphy 	 *  8 bit dimming: write MSB byte.
323fce8e1eSDan Murphy 	 */
333fce8e1eSDan Murphy 	if (lmu_bank->max_brightness == MAX_BRIGHTNESS_11BIT) {
343fce8e1eSDan Murphy 		reg = lmu_bank->lsb_brightness_reg;
353fce8e1eSDan Murphy 		ret = regmap_update_bits(regmap, reg,
363fce8e1eSDan Murphy 					 LMU_11BIT_LSB_MASK,
373fce8e1eSDan Murphy 					 brightness);
383fce8e1eSDan Murphy 		if (ret)
393fce8e1eSDan Murphy 			return ret;
403fce8e1eSDan Murphy 
413fce8e1eSDan Murphy 		val = brightness >> LMU_11BIT_MSB_SHIFT;
423fce8e1eSDan Murphy 	} else {
433fce8e1eSDan Murphy 		val = brightness;
443fce8e1eSDan Murphy 	}
453fce8e1eSDan Murphy 
463fce8e1eSDan Murphy 	reg = lmu_bank->msb_brightness_reg;
473fce8e1eSDan Murphy 
483fce8e1eSDan Murphy 	return regmap_write(regmap, reg, val);
493fce8e1eSDan Murphy }
503fce8e1eSDan Murphy 
ti_lmu_common_set_brightness(struct ti_lmu_bank * lmu_bank,int brightness)513fce8e1eSDan Murphy int ti_lmu_common_set_brightness(struct ti_lmu_bank *lmu_bank, int brightness)
523fce8e1eSDan Murphy {
533fce8e1eSDan Murphy 	return ti_lmu_common_update_brightness(lmu_bank, brightness);
543fce8e1eSDan Murphy }
553fce8e1eSDan Murphy EXPORT_SYMBOL(ti_lmu_common_set_brightness);
563fce8e1eSDan Murphy 
ti_lmu_common_convert_ramp_to_index(unsigned int usec)572637fd43SDan Murphy static unsigned int ti_lmu_common_convert_ramp_to_index(unsigned int usec)
583fce8e1eSDan Murphy {
593fce8e1eSDan Murphy 	int size = ARRAY_SIZE(ramp_table);
603fce8e1eSDan Murphy 	int i;
613fce8e1eSDan Murphy 
623fce8e1eSDan Murphy 	if (usec <= ramp_table[0])
633fce8e1eSDan Murphy 		return 0;
643fce8e1eSDan Murphy 
653fce8e1eSDan Murphy 	if (usec > ramp_table[size - 1])
663fce8e1eSDan Murphy 		return size - 1;
673fce8e1eSDan Murphy 
683fce8e1eSDan Murphy 	for (i = 1; i < size; i++) {
693fce8e1eSDan Murphy 		if (usec == ramp_table[i])
703fce8e1eSDan Murphy 			return i;
713fce8e1eSDan Murphy 
723fce8e1eSDan Murphy 		/* Find an approximate index by looking up the table */
733fce8e1eSDan Murphy 		if (usec > ramp_table[i - 1] && usec < ramp_table[i]) {
743fce8e1eSDan Murphy 			if (usec - ramp_table[i - 1] < ramp_table[i] - usec)
753fce8e1eSDan Murphy 				return i - 1;
763fce8e1eSDan Murphy 			else
773fce8e1eSDan Murphy 				return i;
783fce8e1eSDan Murphy 		}
793fce8e1eSDan Murphy 	}
803fce8e1eSDan Murphy 
812637fd43SDan Murphy 	return 0;
823fce8e1eSDan Murphy }
833fce8e1eSDan Murphy 
ti_lmu_common_set_ramp(struct ti_lmu_bank * lmu_bank)843fce8e1eSDan Murphy int ti_lmu_common_set_ramp(struct ti_lmu_bank *lmu_bank)
853fce8e1eSDan Murphy {
863fce8e1eSDan Murphy 	struct regmap *regmap = lmu_bank->regmap;
873fce8e1eSDan Murphy 	u8 ramp, ramp_up, ramp_down;
883fce8e1eSDan Murphy 
893fce8e1eSDan Murphy 	if (lmu_bank->ramp_up_usec == 0 && lmu_bank->ramp_down_usec == 0) {
903fce8e1eSDan Murphy 		ramp_up = 0;
913fce8e1eSDan Murphy 		ramp_down = 0;
923fce8e1eSDan Murphy 	} else {
933fce8e1eSDan Murphy 		ramp_up = ti_lmu_common_convert_ramp_to_index(lmu_bank->ramp_up_usec);
943fce8e1eSDan Murphy 		ramp_down = ti_lmu_common_convert_ramp_to_index(lmu_bank->ramp_down_usec);
953fce8e1eSDan Murphy 	}
963fce8e1eSDan Murphy 
973fce8e1eSDan Murphy 	ramp = (ramp_up << 4) | ramp_down;
983fce8e1eSDan Murphy 
993fce8e1eSDan Murphy 	return regmap_write(regmap, lmu_bank->runtime_ramp_reg, ramp);
1003fce8e1eSDan Murphy 
1013fce8e1eSDan Murphy }
1023fce8e1eSDan Murphy EXPORT_SYMBOL(ti_lmu_common_set_ramp);
1033fce8e1eSDan Murphy 
ti_lmu_common_get_ramp_params(struct device * dev,struct fwnode_handle * child,struct ti_lmu_bank * lmu_data)1043fce8e1eSDan Murphy int ti_lmu_common_get_ramp_params(struct device *dev,
1053fce8e1eSDan Murphy 				  struct fwnode_handle *child,
1063fce8e1eSDan Murphy 				  struct ti_lmu_bank *lmu_data)
1073fce8e1eSDan Murphy {
1083fce8e1eSDan Murphy 	int ret;
1093fce8e1eSDan Murphy 
1103fce8e1eSDan Murphy 	ret = fwnode_property_read_u32(child, "ramp-up-us",
1113fce8e1eSDan Murphy 				 &lmu_data->ramp_up_usec);
1123fce8e1eSDan Murphy 	if (ret)
1133fce8e1eSDan Murphy 		dev_warn(dev, "ramp-up-us property missing\n");
1143fce8e1eSDan Murphy 
1153fce8e1eSDan Murphy 
1163fce8e1eSDan Murphy 	ret = fwnode_property_read_u32(child, "ramp-down-us",
1173fce8e1eSDan Murphy 				 &lmu_data->ramp_down_usec);
1183fce8e1eSDan Murphy 	if (ret)
1193fce8e1eSDan Murphy 		dev_warn(dev, "ramp-down-us property missing\n");
1203fce8e1eSDan Murphy 
1213fce8e1eSDan Murphy 	return 0;
1223fce8e1eSDan Murphy }
1233fce8e1eSDan Murphy EXPORT_SYMBOL(ti_lmu_common_get_ramp_params);
1243fce8e1eSDan Murphy 
ti_lmu_common_get_brt_res(struct device * dev,struct fwnode_handle * child,struct ti_lmu_bank * lmu_data)1253fce8e1eSDan Murphy int ti_lmu_common_get_brt_res(struct device *dev, struct fwnode_handle *child,
1263fce8e1eSDan Murphy 				  struct ti_lmu_bank *lmu_data)
1273fce8e1eSDan Murphy {
1283fce8e1eSDan Murphy 	int ret;
1293fce8e1eSDan Murphy 
1303fce8e1eSDan Murphy 	ret = device_property_read_u32(dev, "ti,brightness-resolution",
1313fce8e1eSDan Murphy 				       &lmu_data->max_brightness);
1323fce8e1eSDan Murphy 	if (ret)
1333fce8e1eSDan Murphy 		ret = fwnode_property_read_u32(child,
1343fce8e1eSDan Murphy 					       "ti,brightness-resolution",
1353fce8e1eSDan Murphy 					       &lmu_data->max_brightness);
1363fce8e1eSDan Murphy 	if (lmu_data->max_brightness <= 0) {
1373fce8e1eSDan Murphy 		lmu_data->max_brightness = MAX_BRIGHTNESS_8BIT;
1383fce8e1eSDan Murphy 		return ret;
1393fce8e1eSDan Murphy 	}
1403fce8e1eSDan Murphy 
1413fce8e1eSDan Murphy 	if (lmu_data->max_brightness > MAX_BRIGHTNESS_11BIT)
1423fce8e1eSDan Murphy 			lmu_data->max_brightness = MAX_BRIGHTNESS_11BIT;
1433fce8e1eSDan Murphy 
1443fce8e1eSDan Murphy 
1453fce8e1eSDan Murphy 	return 0;
1463fce8e1eSDan Murphy }
1473fce8e1eSDan Murphy EXPORT_SYMBOL(ti_lmu_common_get_brt_res);
1483fce8e1eSDan Murphy 
1493fce8e1eSDan Murphy MODULE_DESCRIPTION("TI LMU common LED framework");
1503fce8e1eSDan Murphy MODULE_AUTHOR("Sebastian Reichel");
1513fce8e1eSDan Murphy MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
1523fce8e1eSDan Murphy MODULE_LICENSE("GPL v2");
1533fce8e1eSDan Murphy MODULE_ALIAS("ti-lmu-led-common");
154