Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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) // TI LM3697 LED chip family driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/of_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/leds-ti-lmu-common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #define LM3697_REV			0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define LM3697_RESET			0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #define LM3697_OUTPUT_CONFIG		0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define LM3697_CTRL_A_RAMP		0x11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define LM3697_CTRL_B_RAMP		0x12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define LM3697_CTRL_A_B_RT_RAMP		0x13
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define LM3697_CTRL_A_B_RAMP_CFG	0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define LM3697_CTRL_A_B_BRT_CFG		0x16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define LM3697_CTRL_A_FS_CURR_CFG	0x17
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define LM3697_CTRL_B_FS_CURR_CFG	0x18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define LM3697_PWM_CFG			0x1c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define LM3697_CTRL_A_BRT_LSB		0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define LM3697_CTRL_A_BRT_MSB		0x21
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define LM3697_CTRL_B_BRT_LSB		0x22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define LM3697_CTRL_B_BRT_MSB		0x23
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define LM3697_CTRL_ENABLE		0x24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define LM3697_SW_RESET		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define LM3697_CTRL_A_EN	BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define LM3697_CTRL_B_EN	BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define LM3697_CTRL_A_B_EN	(LM3697_CTRL_A_EN | LM3697_CTRL_B_EN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define LM3697_MAX_LED_STRINGS	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define LM3697_CONTROL_A	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define LM3697_CONTROL_B	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define LM3697_MAX_CONTROL_BANKS 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * struct lm3697_led -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * @hvled_strings: Array of LED strings associated with a control bank
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * @label: LED label
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * @led_dev: LED class device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * @priv: Pointer to the device struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * @lmu_data: Register and setting values for common code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * @control_bank: Control bank the LED is associated to. 0 is control bank A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *		   1 is control bank B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) struct lm3697_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u32 hvled_strings[LM3697_MAX_LED_STRINGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	char label[LED_MAX_NAME_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct led_classdev led_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct lm3697 *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct ti_lmu_bank lmu_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	int control_bank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	int num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * struct lm3697 -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * @enable_gpio: Hardware enable gpio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * @regulator: LED supply regulator pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * @client: Pointer to the I2C client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * @regmap: Devices register map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * @dev: Pointer to the devices device struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * @lock: Lock for reading/writing the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * @leds: Array of LED strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) struct lm3697 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct gpio_desc *enable_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct regulator *regulator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int bank_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int num_banks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct lm3697_led leds[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static const struct reg_default lm3697_reg_defs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	{LM3697_OUTPUT_CONFIG, 0x6},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	{LM3697_CTRL_A_RAMP, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	{LM3697_CTRL_B_RAMP, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	{LM3697_CTRL_A_B_RT_RAMP, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	{LM3697_CTRL_A_B_RAMP_CFG, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	{LM3697_CTRL_A_B_BRT_CFG, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	{LM3697_CTRL_A_FS_CURR_CFG, 0x13},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	{LM3697_CTRL_B_FS_CURR_CFG, 0x13},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	{LM3697_PWM_CFG, 0xc},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	{LM3697_CTRL_A_BRT_LSB, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	{LM3697_CTRL_A_BRT_MSB, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	{LM3697_CTRL_B_BRT_LSB, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	{LM3697_CTRL_B_BRT_MSB, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	{LM3697_CTRL_ENABLE, 0x0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static const struct regmap_config lm3697_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	.max_register = LM3697_CTRL_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	.reg_defaults = lm3697_reg_defs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	.num_reg_defaults = ARRAY_SIZE(lm3697_reg_defs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	.cache_type = REGCACHE_FLAT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int lm3697_brightness_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 				enum led_brightness brt_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct lm3697_led *led = container_of(led_cdev, struct lm3697_led,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 					      led_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int ctrl_en_val = (1 << led->control_bank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct device *dev = led->priv->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	mutex_lock(&led->priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (brt_val == LED_OFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		ret = regmap_update_bits(led->priv->regmap, LM3697_CTRL_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 					 ctrl_en_val, ~ctrl_en_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			dev_err(dev, "Cannot write ctrl register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			goto brightness_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		led->enabled = LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		ret = ti_lmu_common_set_brightness(&led->lmu_data, brt_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			dev_err(dev, "Cannot write brightness\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			goto brightness_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		if (!led->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			ret = regmap_update_bits(led->priv->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 						 LM3697_CTRL_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 						 ctrl_en_val, ctrl_en_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 				dev_err(dev, "Cannot enable the device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				goto brightness_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			led->enabled = brt_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) brightness_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	mutex_unlock(&led->priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static int lm3697_init(struct lm3697 *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct device *dev = priv->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	struct lm3697_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (priv->enable_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		gpiod_direction_output(priv->enable_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		ret = regmap_write(priv->regmap, LM3697_RESET, LM3697_SW_RESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			dev_err(dev, "Cannot reset the device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	ret = regmap_write(priv->regmap, LM3697_CTRL_ENABLE, 0x0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		dev_err(dev, "Cannot write ctrl enable\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	ret = regmap_write(priv->regmap, LM3697_OUTPUT_CONFIG, priv->bank_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		dev_err(dev, "Cannot write OUTPUT config\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	for (i = 0; i < priv->num_banks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		led = &priv->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		ret = ti_lmu_common_set_ramp(&led->lmu_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			dev_err(dev, "Setting the ramp rate failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int lm3697_probe_dt(struct lm3697 *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct fwnode_handle *child = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct device *dev = priv->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct lm3697_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	int control_bank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	size_t i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	priv->enable_gpio = devm_gpiod_get_optional(dev, "enable",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 						    GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (IS_ERR(priv->enable_gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		return dev_err_probe(dev, PTR_ERR(priv->enable_gpio),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 					  "Failed to get enable GPIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	priv->regulator = devm_regulator_get(dev, "vled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (IS_ERR(priv->regulator))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		priv->regulator = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	device_for_each_child_node(dev, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		struct led_init_data init_data = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		ret = fwnode_property_read_u32(child, "reg", &control_bank);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			dev_err(dev, "reg property missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			goto child_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		if (control_bank > LM3697_CONTROL_B) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			dev_err(dev, "reg property is invalid\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			goto child_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		led = &priv->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		ret = ti_lmu_common_get_brt_res(dev, child, &led->lmu_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			dev_warn(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 				 "brightness resolution property missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		led->control_bank = control_bank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		led->lmu_data.regmap = priv->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		led->lmu_data.runtime_ramp_reg = LM3697_CTRL_A_RAMP +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 						 control_bank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		led->lmu_data.msb_brightness_reg = LM3697_CTRL_A_BRT_MSB +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 						   led->control_bank * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		led->lmu_data.lsb_brightness_reg = LM3697_CTRL_A_BRT_LSB +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 						   led->control_bank * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		led->num_leds = fwnode_property_count_u32(child, "led-sources");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (led->num_leds > LM3697_MAX_LED_STRINGS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			dev_err(dev, "Too many LED strings defined\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		ret = fwnode_property_read_u32_array(child, "led-sources",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 						    led->hvled_strings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 						    led->num_leds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			dev_err(dev, "led-sources property missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			goto child_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		for (j = 0; j < led->num_leds; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			priv->bank_cfg |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 				(led->control_bank << led->hvled_strings[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		ret = ti_lmu_common_get_ramp_params(dev, child, &led->lmu_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			dev_warn(dev, "runtime-ramp properties missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		init_data.fwnode = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		init_data.devicename = priv->client->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		/* for backwards compatibility if `label` is not present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		init_data.default_label = ":";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		led->priv = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		led->led_dev.max_brightness = led->lmu_data.max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		led->led_dev.brightness_set_blocking = lm3697_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		ret = devm_led_classdev_register_ext(dev, &led->led_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 						     &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			dev_err(dev, "led register err: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			goto child_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) child_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int lm3697_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct lm3697 *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	count = device_get_child_node_count(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (!count || count > LM3697_MAX_CONTROL_BANKS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		dev_err(dev, "Strange device tree!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	led = devm_kzalloc(dev, struct_size(led, leds, count), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (!led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	mutex_init(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	i2c_set_clientdata(client, led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	led->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	led->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	led->num_banks = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	led->regmap = devm_regmap_init_i2c(client, &lm3697_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (IS_ERR(led->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		ret = PTR_ERR(led->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		dev_err(dev, "Failed to allocate register map: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	ret = lm3697_probe_dt(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	return lm3697_init(led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static int lm3697_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	struct lm3697 *led = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	struct device *dev = &led->client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	ret = regmap_update_bits(led->regmap, LM3697_CTRL_ENABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				 LM3697_CTRL_A_B_EN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		dev_err(dev, "Failed to disable the device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (led->enable_gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		gpiod_direction_output(led->enable_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (led->regulator) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		ret = regulator_disable(led->regulator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			dev_err(dev, "Failed to disable regulator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	mutex_destroy(&led->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static const struct i2c_device_id lm3697_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	{ "lm3697", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) MODULE_DEVICE_TABLE(i2c, lm3697_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static const struct of_device_id of_lm3697_leds_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	{ .compatible = "ti,lm3697", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) MODULE_DEVICE_TABLE(of, of_lm3697_leds_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static struct i2c_driver lm3697_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		.name	= "lm3697",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		.of_match_table = of_lm3697_leds_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	.probe		= lm3697_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	.remove		= lm3697_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	.id_table	= lm3697_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) module_i2c_driver(lm3697_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) MODULE_DESCRIPTION("Texas Instruments LM3697 LED driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) MODULE_LICENSE("GPL v2");