1 // SPDX-License-Identifier: GPL-2.0
2 // TI LM3697 LED chip family driver
3 // Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
5 #include <linux/gpio/consumer.h>
8 #include <linux/of_gpio.h>
9 #include <linux/regulator/consumer.h>
10 #include <linux/leds-ti-lmu-common.h>
12 #define LM3697_REV 0x0
13 #define LM3697_RESET 0x1
14 #define LM3697_OUTPUT_CONFIG 0x10
15 #define LM3697_CTRL_A_RAMP 0x11
16 #define LM3697_CTRL_B_RAMP 0x12
17 #define LM3697_CTRL_A_B_RT_RAMP 0x13
18 #define LM3697_CTRL_A_B_RAMP_CFG 0x14
19 #define LM3697_CTRL_A_B_BRT_CFG 0x16
20 #define LM3697_CTRL_A_FS_CURR_CFG 0x17
21 #define LM3697_CTRL_B_FS_CURR_CFG 0x18
22 #define LM3697_PWM_CFG 0x1c
23 #define LM3697_CTRL_A_BRT_LSB 0x20
24 #define LM3697_CTRL_A_BRT_MSB 0x21
25 #define LM3697_CTRL_B_BRT_LSB 0x22
26 #define LM3697_CTRL_B_BRT_MSB 0x23
27 #define LM3697_CTRL_ENABLE 0x24
29 #define LM3697_SW_RESET BIT(0)
31 #define LM3697_CTRL_A_EN BIT(0)
32 #define LM3697_CTRL_B_EN BIT(1)
33 #define LM3697_CTRL_A_B_EN (LM3697_CTRL_A_EN | LM3697_CTRL_B_EN)
35 #define LM3697_MAX_LED_STRINGS 3
37 #define LM3697_CONTROL_A 0
38 #define LM3697_CONTROL_B 1
39 #define LM3697_MAX_CONTROL_BANKS 2
43 * @hvled_strings: Array of LED strings associated with a control bank
45 * @led_dev: LED class device
46 * @priv: Pointer to the device struct
47 * @lmu_data: Register and setting values for common code
48 * @control_bank: Control bank the LED is associated to. 0 is control bank A
52 u32 hvled_strings
[LM3697_MAX_LED_STRINGS
];
53 char label
[LED_MAX_NAME_SIZE
];
54 struct led_classdev led_dev
;
56 struct ti_lmu_bank lmu_data
;
64 * @enable_gpio: Hardware enable gpio
65 * @regulator: LED supply regulator pointer
66 * @client: Pointer to the I2C client
67 * @regmap: Devices register map
68 * @dev: Pointer to the devices device struct
69 * @lock: Lock for reading/writing the device
70 * @leds: Array of LED strings
73 struct gpio_desc
*enable_gpio
;
74 struct regulator
*regulator
;
75 struct i2c_client
*client
;
76 struct regmap
*regmap
;
82 struct lm3697_led leds
[];
85 static const struct reg_default lm3697_reg_defs
[] = {
86 {LM3697_OUTPUT_CONFIG
, 0x6},
87 {LM3697_CTRL_A_RAMP
, 0x0},
88 {LM3697_CTRL_B_RAMP
, 0x0},
89 {LM3697_CTRL_A_B_RT_RAMP
, 0x0},
90 {LM3697_CTRL_A_B_RAMP_CFG
, 0x0},
91 {LM3697_CTRL_A_B_BRT_CFG
, 0x0},
92 {LM3697_CTRL_A_FS_CURR_CFG
, 0x13},
93 {LM3697_CTRL_B_FS_CURR_CFG
, 0x13},
94 {LM3697_PWM_CFG
, 0xc},
95 {LM3697_CTRL_A_BRT_LSB
, 0x0},
96 {LM3697_CTRL_A_BRT_MSB
, 0x0},
97 {LM3697_CTRL_B_BRT_LSB
, 0x0},
98 {LM3697_CTRL_B_BRT_MSB
, 0x0},
99 {LM3697_CTRL_ENABLE
, 0x0},
102 static const struct regmap_config lm3697_regmap_config
= {
106 .max_register
= LM3697_CTRL_ENABLE
,
107 .reg_defaults
= lm3697_reg_defs
,
108 .num_reg_defaults
= ARRAY_SIZE(lm3697_reg_defs
),
109 .cache_type
= REGCACHE_FLAT
,
112 static int lm3697_brightness_set(struct led_classdev
*led_cdev
,
113 enum led_brightness brt_val
)
115 struct lm3697_led
*led
= container_of(led_cdev
, struct lm3697_led
,
117 int ctrl_en_val
= (1 << led
->control_bank
);
120 mutex_lock(&led
->priv
->lock
);
122 if (brt_val
== LED_OFF
) {
123 ret
= regmap_update_bits(led
->priv
->regmap
, LM3697_CTRL_ENABLE
,
124 ctrl_en_val
, ~ctrl_en_val
);
126 dev_err(&led
->priv
->client
->dev
, "Cannot write ctrl register\n");
130 led
->enabled
= LED_OFF
;
132 ret
= ti_lmu_common_set_brightness(&led
->lmu_data
, brt_val
);
134 dev_err(&led
->priv
->client
->dev
,
135 "Cannot write brightness\n");
140 ret
= regmap_update_bits(led
->priv
->regmap
,
142 ctrl_en_val
, ctrl_en_val
);
144 dev_err(&led
->priv
->client
->dev
,
145 "Cannot enable the device\n");
149 led
->enabled
= brt_val
;
154 mutex_unlock(&led
->priv
->lock
);
158 static int lm3697_init(struct lm3697
*priv
)
160 struct lm3697_led
*led
;
163 if (priv
->enable_gpio
) {
164 gpiod_direction_output(priv
->enable_gpio
, 1);
166 ret
= regmap_write(priv
->regmap
, LM3697_RESET
, LM3697_SW_RESET
);
168 dev_err(&priv
->client
->dev
, "Cannot reset the device\n");
173 ret
= regmap_write(priv
->regmap
, LM3697_CTRL_ENABLE
, 0x0);
175 dev_err(&priv
->client
->dev
, "Cannot write ctrl enable\n");
179 ret
= regmap_write(priv
->regmap
, LM3697_OUTPUT_CONFIG
, priv
->bank_cfg
);
181 dev_err(&priv
->client
->dev
, "Cannot write OUTPUT config\n");
183 for (i
= 0; i
< LM3697_MAX_CONTROL_BANKS
; i
++) {
184 led
= &priv
->leds
[i
];
185 ret
= ti_lmu_common_set_ramp(&led
->lmu_data
);
187 dev_err(&priv
->client
->dev
, "Setting the ramp rate failed\n");
193 static int lm3697_probe_dt(struct lm3697
*priv
)
195 struct fwnode_handle
*child
= NULL
;
196 struct lm3697_led
*led
;
203 priv
->enable_gpio
= devm_gpiod_get_optional(&priv
->client
->dev
,
204 "enable", GPIOD_OUT_LOW
);
205 if (IS_ERR(priv
->enable_gpio
)) {
206 ret
= PTR_ERR(priv
->enable_gpio
);
207 dev_err(&priv
->client
->dev
, "Failed to get enable gpio: %d\n",
212 priv
->regulator
= devm_regulator_get(&priv
->client
->dev
, "vled");
213 if (IS_ERR(priv
->regulator
))
214 priv
->regulator
= NULL
;
216 device_for_each_child_node(priv
->dev
, child
) {
217 ret
= fwnode_property_read_u32(child
, "reg", &control_bank
);
219 dev_err(&priv
->client
->dev
, "reg property missing\n");
220 fwnode_handle_put(child
);
224 if (control_bank
> LM3697_CONTROL_B
) {
225 dev_err(&priv
->client
->dev
, "reg property is invalid\n");
227 fwnode_handle_put(child
);
231 led
= &priv
->leds
[i
];
233 ret
= ti_lmu_common_get_brt_res(&priv
->client
->dev
,
234 child
, &led
->lmu_data
);
236 dev_warn(&priv
->client
->dev
, "brightness resolution property missing\n");
238 led
->control_bank
= control_bank
;
239 led
->lmu_data
.regmap
= priv
->regmap
;
240 led
->lmu_data
.runtime_ramp_reg
= LM3697_CTRL_A_RAMP
+
242 led
->lmu_data
.msb_brightness_reg
= LM3697_CTRL_A_BRT_MSB
+
243 led
->control_bank
* 2;
244 led
->lmu_data
.lsb_brightness_reg
= LM3697_CTRL_A_BRT_LSB
+
245 led
->control_bank
* 2;
247 led
->num_leds
= fwnode_property_count_u32(child
, "led-sources");
248 if (led
->num_leds
> LM3697_MAX_LED_STRINGS
) {
249 dev_err(&priv
->client
->dev
, "Too many LED strings defined\n");
253 ret
= fwnode_property_read_u32_array(child
, "led-sources",
257 dev_err(&priv
->client
->dev
, "led-sources property missing\n");
258 fwnode_handle_put(child
);
262 for (j
= 0; j
< led
->num_leds
; j
++)
264 (led
->control_bank
<< led
->hvled_strings
[j
]);
266 ret
= ti_lmu_common_get_ramp_params(&priv
->client
->dev
,
267 child
, &led
->lmu_data
);
269 dev_warn(&priv
->client
->dev
, "runtime-ramp properties missing\n");
271 fwnode_property_read_string(child
, "linux,default-trigger",
272 &led
->led_dev
.default_trigger
);
274 ret
= fwnode_property_read_string(child
, "label", &name
);
276 snprintf(led
->label
, sizeof(led
->label
),
277 "%s::", priv
->client
->name
);
279 snprintf(led
->label
, sizeof(led
->label
),
280 "%s:%s", priv
->client
->name
, name
);
283 led
->led_dev
.name
= led
->label
;
284 led
->led_dev
.max_brightness
= led
->lmu_data
.max_brightness
;
285 led
->led_dev
.brightness_set_blocking
= lm3697_brightness_set
;
287 ret
= devm_led_classdev_register(priv
->dev
, &led
->led_dev
);
289 dev_err(&priv
->client
->dev
, "led register err: %d\n",
291 fwnode_handle_put(child
);
302 static int lm3697_probe(struct i2c_client
*client
,
303 const struct i2c_device_id
*id
)
309 count
= device_get_child_node_count(&client
->dev
);
311 dev_err(&client
->dev
, "LEDs are not defined in device tree!");
315 led
= devm_kzalloc(&client
->dev
, struct_size(led
, leds
, count
),
320 mutex_init(&led
->lock
);
321 i2c_set_clientdata(client
, led
);
323 led
->client
= client
;
324 led
->dev
= &client
->dev
;
325 led
->regmap
= devm_regmap_init_i2c(client
, &lm3697_regmap_config
);
326 if (IS_ERR(led
->regmap
)) {
327 ret
= PTR_ERR(led
->regmap
);
328 dev_err(&client
->dev
, "Failed to allocate register map: %d\n",
333 ret
= lm3697_probe_dt(led
);
337 return lm3697_init(led
);
340 static int lm3697_remove(struct i2c_client
*client
)
342 struct lm3697
*led
= i2c_get_clientdata(client
);
345 ret
= regmap_update_bits(led
->regmap
, LM3697_CTRL_ENABLE
,
346 LM3697_CTRL_A_B_EN
, 0);
348 dev_err(&led
->client
->dev
, "Failed to disable the device\n");
352 if (led
->enable_gpio
)
353 gpiod_direction_output(led
->enable_gpio
, 0);
355 if (led
->regulator
) {
356 ret
= regulator_disable(led
->regulator
);
358 dev_err(&led
->client
->dev
,
359 "Failed to disable regulator\n");
362 mutex_destroy(&led
->lock
);
367 static const struct i2c_device_id lm3697_id
[] = {
371 MODULE_DEVICE_TABLE(i2c
, lm3697_id
);
373 static const struct of_device_id of_lm3697_leds_match
[] = {
374 { .compatible
= "ti,lm3697", },
377 MODULE_DEVICE_TABLE(of
, of_lm3697_leds_match
);
379 static struct i2c_driver lm3697_driver
= {
382 .of_match_table
= of_lm3697_leds_match
,
384 .probe
= lm3697_probe
,
385 .remove
= lm3697_remove
,
386 .id_table
= lm3697_id
,
388 module_i2c_driver(lm3697_driver
);
390 MODULE_DESCRIPTION("Texas Instruments LM3697 LED driver");
391 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
392 MODULE_LICENSE("GPL v2");