1 // SPDX-License-Identifier: GPL-2.0
2 // TI LM3697 LED chip family driver
3 // Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
5 #include <linux/bits.h>
6 #include <linux/gpio/consumer.h>
8 #include <linux/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/property.h>
11 #include <linux/regmap.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/types.h>
15 #include <linux/leds-ti-lmu-common.h>
17 #define LM3697_REV 0x0
18 #define LM3697_RESET 0x1
19 #define LM3697_OUTPUT_CONFIG 0x10
20 #define LM3697_CTRL_A_RAMP 0x11
21 #define LM3697_CTRL_B_RAMP 0x12
22 #define LM3697_CTRL_A_B_RT_RAMP 0x13
23 #define LM3697_CTRL_A_B_RAMP_CFG 0x14
24 #define LM3697_CTRL_A_B_BRT_CFG 0x16
25 #define LM3697_CTRL_A_FS_CURR_CFG 0x17
26 #define LM3697_CTRL_B_FS_CURR_CFG 0x18
27 #define LM3697_PWM_CFG 0x1c
28 #define LM3697_CTRL_A_BRT_LSB 0x20
29 #define LM3697_CTRL_A_BRT_MSB 0x21
30 #define LM3697_CTRL_B_BRT_LSB 0x22
31 #define LM3697_CTRL_B_BRT_MSB 0x23
32 #define LM3697_CTRL_ENABLE 0x24
34 #define LM3697_SW_RESET BIT(0)
36 #define LM3697_CTRL_A_EN BIT(0)
37 #define LM3697_CTRL_B_EN BIT(1)
38 #define LM3697_CTRL_A_B_EN (LM3697_CTRL_A_EN | LM3697_CTRL_B_EN)
40 #define LM3697_MAX_LED_STRINGS 3
42 #define LM3697_CONTROL_A 0
43 #define LM3697_CONTROL_B 1
44 #define LM3697_MAX_CONTROL_BANKS 2
48 * @hvled_strings: Array of LED strings associated with a control bank
50 * @led_dev: LED class device
51 * @priv: Pointer to the device struct
52 * @lmu_data: Register and setting values for common code
53 * @control_bank: Control bank the LED is associated to. 0 is control bank A
55 * @enabled: LED brightness level (or LED_OFF)
56 * @num_leds: Number of LEDs available
59 u32 hvled_strings
[LM3697_MAX_LED_STRINGS
];
60 char label
[LED_MAX_NAME_SIZE
];
61 struct led_classdev led_dev
;
63 struct ti_lmu_bank lmu_data
;
71 * @enable_gpio: Hardware enable gpio
72 * @regulator: LED supply regulator pointer
73 * @client: Pointer to the I2C client
74 * @regmap: Devices register map
75 * @dev: Pointer to the devices device struct
76 * @lock: Lock for reading/writing the device
77 * @leds: Array of LED strings
78 * @bank_cfg: OUTPUT_CONFIG register values
79 * @num_banks: Number of control banks
82 struct gpio_desc
*enable_gpio
;
83 struct regulator
*regulator
;
84 struct i2c_client
*client
;
85 struct regmap
*regmap
;
92 struct lm3697_led leds
[] __counted_by(num_banks
);
95 static const struct reg_default lm3697_reg_defs
[] = {
96 {LM3697_OUTPUT_CONFIG
, 0x6},
97 {LM3697_CTRL_A_RAMP
, 0x0},
98 {LM3697_CTRL_B_RAMP
, 0x0},
99 {LM3697_CTRL_A_B_RT_RAMP
, 0x0},
100 {LM3697_CTRL_A_B_RAMP_CFG
, 0x0},
101 {LM3697_CTRL_A_B_BRT_CFG
, 0x0},
102 {LM3697_CTRL_A_FS_CURR_CFG
, 0x13},
103 {LM3697_CTRL_B_FS_CURR_CFG
, 0x13},
104 {LM3697_PWM_CFG
, 0xc},
105 {LM3697_CTRL_A_BRT_LSB
, 0x0},
106 {LM3697_CTRL_A_BRT_MSB
, 0x0},
107 {LM3697_CTRL_B_BRT_LSB
, 0x0},
108 {LM3697_CTRL_B_BRT_MSB
, 0x0},
109 {LM3697_CTRL_ENABLE
, 0x0},
112 static const struct regmap_config lm3697_regmap_config
= {
116 .max_register
= LM3697_CTRL_ENABLE
,
117 .reg_defaults
= lm3697_reg_defs
,
118 .num_reg_defaults
= ARRAY_SIZE(lm3697_reg_defs
),
119 .cache_type
= REGCACHE_FLAT
,
122 static int lm3697_brightness_set(struct led_classdev
*led_cdev
,
123 enum led_brightness brt_val
)
125 struct lm3697_led
*led
= container_of(led_cdev
, struct lm3697_led
,
127 int ctrl_en_val
= (1 << led
->control_bank
);
128 struct device
*dev
= led
->priv
->dev
;
131 mutex_lock(&led
->priv
->lock
);
133 if (brt_val
== LED_OFF
) {
134 ret
= regmap_update_bits(led
->priv
->regmap
, LM3697_CTRL_ENABLE
,
135 ctrl_en_val
, ~ctrl_en_val
);
137 dev_err(dev
, "Cannot write ctrl register\n");
141 led
->enabled
= LED_OFF
;
143 ret
= ti_lmu_common_set_brightness(&led
->lmu_data
, brt_val
);
145 dev_err(dev
, "Cannot write brightness\n");
150 ret
= regmap_update_bits(led
->priv
->regmap
,
152 ctrl_en_val
, ctrl_en_val
);
154 dev_err(dev
, "Cannot enable the device\n");
158 led
->enabled
= brt_val
;
163 mutex_unlock(&led
->priv
->lock
);
167 static int lm3697_init(struct lm3697
*priv
)
169 struct device
*dev
= priv
->dev
;
170 struct lm3697_led
*led
;
173 if (priv
->enable_gpio
) {
174 gpiod_direction_output(priv
->enable_gpio
, 1);
176 ret
= regmap_write(priv
->regmap
, LM3697_RESET
, LM3697_SW_RESET
);
178 dev_err(dev
, "Cannot reset the device\n");
183 ret
= regmap_write(priv
->regmap
, LM3697_CTRL_ENABLE
, 0x0);
185 dev_err(dev
, "Cannot write ctrl enable\n");
189 ret
= regmap_write(priv
->regmap
, LM3697_OUTPUT_CONFIG
, priv
->bank_cfg
);
191 dev_err(dev
, "Cannot write OUTPUT config\n");
193 for (i
= 0; i
< priv
->num_banks
; i
++) {
194 led
= &priv
->leds
[i
];
195 ret
= ti_lmu_common_set_ramp(&led
->lmu_data
);
197 dev_err(dev
, "Setting the ramp rate failed\n");
203 static int lm3697_probe_dt(struct lm3697
*priv
)
205 struct device
*dev
= priv
->dev
;
206 struct lm3697_led
*led
;
212 priv
->enable_gpio
= devm_gpiod_get_optional(dev
, "enable",
214 if (IS_ERR(priv
->enable_gpio
))
215 return dev_err_probe(dev
, PTR_ERR(priv
->enable_gpio
),
216 "Failed to get enable GPIO\n");
218 priv
->regulator
= devm_regulator_get(dev
, "vled");
219 if (IS_ERR(priv
->regulator
))
220 priv
->regulator
= NULL
;
222 device_for_each_child_node_scoped(dev
, child
) {
223 struct led_init_data init_data
= {};
225 ret
= fwnode_property_read_u32(child
, "reg", &control_bank
);
227 dev_err(dev
, "reg property missing\n");
231 if (control_bank
> LM3697_CONTROL_B
) {
232 dev_err(dev
, "reg property is invalid\n");
236 led
= &priv
->leds
[i
];
238 ret
= ti_lmu_common_get_brt_res(dev
, child
, &led
->lmu_data
);
241 "brightness resolution property missing\n");
243 led
->control_bank
= control_bank
;
244 led
->lmu_data
.regmap
= priv
->regmap
;
245 led
->lmu_data
.runtime_ramp_reg
= LM3697_CTRL_A_RAMP
+
247 led
->lmu_data
.msb_brightness_reg
= LM3697_CTRL_A_BRT_MSB
+
248 led
->control_bank
* 2;
249 led
->lmu_data
.lsb_brightness_reg
= LM3697_CTRL_A_BRT_LSB
+
250 led
->control_bank
* 2;
252 led
->num_leds
= fwnode_property_count_u32(child
, "led-sources");
253 if (led
->num_leds
> LM3697_MAX_LED_STRINGS
) {
254 dev_err(dev
, "Too many LED strings defined\n");
258 ret
= fwnode_property_read_u32_array(child
, "led-sources",
262 dev_err(dev
, "led-sources property missing\n");
266 for (j
= 0; j
< led
->num_leds
; j
++)
268 (led
->control_bank
<< led
->hvled_strings
[j
]);
270 ret
= ti_lmu_common_get_ramp_params(dev
, child
, &led
->lmu_data
);
272 dev_warn(dev
, "runtime-ramp properties missing\n");
274 init_data
.fwnode
= child
;
275 init_data
.devicename
= priv
->client
->name
;
276 /* for backwards compatibility if `label` is not present */
277 init_data
.default_label
= ":";
280 led
->led_dev
.max_brightness
= led
->lmu_data
.max_brightness
;
281 led
->led_dev
.brightness_set_blocking
= lm3697_brightness_set
;
283 ret
= devm_led_classdev_register_ext(dev
, &led
->led_dev
,
286 dev_err(dev
, "led register err: %d\n", ret
);
296 static int lm3697_probe(struct i2c_client
*client
)
298 struct device
*dev
= &client
->dev
;
303 count
= device_get_child_node_count(dev
);
304 if (!count
|| count
> LM3697_MAX_CONTROL_BANKS
) {
305 dev_err(dev
, "Strange device tree!");
309 led
= devm_kzalloc(dev
, struct_size(led
, leds
, count
), GFP_KERNEL
);
313 mutex_init(&led
->lock
);
314 i2c_set_clientdata(client
, led
);
316 led
->client
= client
;
318 led
->num_banks
= count
;
319 led
->regmap
= devm_regmap_init_i2c(client
, &lm3697_regmap_config
);
320 if (IS_ERR(led
->regmap
)) {
321 ret
= PTR_ERR(led
->regmap
);
322 dev_err(dev
, "Failed to allocate register map: %d\n", ret
);
326 ret
= lm3697_probe_dt(led
);
330 return lm3697_init(led
);
333 static void lm3697_remove(struct i2c_client
*client
)
335 struct lm3697
*led
= i2c_get_clientdata(client
);
336 struct device
*dev
= &led
->client
->dev
;
339 ret
= regmap_update_bits(led
->regmap
, LM3697_CTRL_ENABLE
,
340 LM3697_CTRL_A_B_EN
, 0);
342 dev_err(dev
, "Failed to disable the device\n");
344 if (led
->enable_gpio
)
345 gpiod_direction_output(led
->enable_gpio
, 0);
347 if (led
->regulator
) {
348 ret
= regulator_disable(led
->regulator
);
350 dev_err(dev
, "Failed to disable regulator\n");
353 mutex_destroy(&led
->lock
);
356 static const struct i2c_device_id lm3697_id
[] = {
360 MODULE_DEVICE_TABLE(i2c
, lm3697_id
);
362 static const struct of_device_id of_lm3697_leds_match
[] = {
363 { .compatible
= "ti,lm3697", },
366 MODULE_DEVICE_TABLE(of
, of_lm3697_leds_match
);
368 static struct i2c_driver lm3697_driver
= {
371 .of_match_table
= of_lm3697_leds_match
,
373 .probe
= lm3697_probe
,
374 .remove
= lm3697_remove
,
375 .id_table
= lm3697_id
,
377 module_i2c_driver(lm3697_driver
);
379 MODULE_DESCRIPTION("Texas Instruments LM3697 LED driver");
380 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
381 MODULE_LICENSE("GPL v2");