blob: 623e084c3b536c40b504aa7963759ffe59f21ffc [file] [log] [blame]
Dan Murphy5c1d8242019-05-06 14:16:14 -05001// SPDX-License-Identifier: GPL-2.0
2// TI LM3697 LED chip family driver
Alexander A. Klimovc5437332020-07-13 16:51:15 +02003// Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
Dan Murphy5c1d8242019-05-06 14:16:14 -05004
5#include <linux/gpio/consumer.h>
6#include <linux/i2c.h>
7#include <linux/of.h>
8#include <linux/of_gpio.h>
9#include <linux/regulator/consumer.h>
10#include <linux/leds-ti-lmu-common.h>
11
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
28
29#define LM3697_SW_RESET BIT(0)
30
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)
34
35#define LM3697_MAX_LED_STRINGS 3
36
37#define LM3697_CONTROL_A 0
38#define LM3697_CONTROL_B 1
39#define LM3697_MAX_CONTROL_BANKS 2
40
41/**
42 * struct lm3697_led -
43 * @hvled_strings: Array of LED strings associated with a control bank
44 * @label: LED label
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
49 * 1 is control bank B
50 */
51struct lm3697_led {
52 u32 hvled_strings[LM3697_MAX_LED_STRINGS];
53 char label[LED_MAX_NAME_SIZE];
54 struct led_classdev led_dev;
55 struct lm3697 *priv;
56 struct ti_lmu_bank lmu_data;
57 int control_bank;
58 int enabled;
59 int num_leds;
60};
61
62/**
63 * struct lm3697 -
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
71 */
72struct lm3697 {
73 struct gpio_desc *enable_gpio;
74 struct regulator *regulator;
75 struct i2c_client *client;
76 struct regmap *regmap;
77 struct device *dev;
78 struct mutex lock;
79
80 int bank_cfg;
81
82 struct lm3697_led leds[];
83};
84
85static 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},
100};
101
102static const struct regmap_config lm3697_regmap_config = {
103 .reg_bits = 8,
104 .val_bits = 8,
105
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,
110};
111
112static int lm3697_brightness_set(struct led_classdev *led_cdev,
113 enum led_brightness brt_val)
114{
115 struct lm3697_led *led = container_of(led_cdev, struct lm3697_led,
116 led_dev);
117 int ctrl_en_val = (1 << led->control_bank);
118 int ret;
119
120 mutex_lock(&led->priv->lock);
121
122 if (brt_val == LED_OFF) {
123 ret = regmap_update_bits(led->priv->regmap, LM3697_CTRL_ENABLE,
124 ctrl_en_val, ~ctrl_en_val);
125 if (ret) {
126 dev_err(&led->priv->client->dev, "Cannot write ctrl register\n");
127 goto brightness_out;
128 }
129
130 led->enabled = LED_OFF;
131 } else {
132 ret = ti_lmu_common_set_brightness(&led->lmu_data, brt_val);
133 if (ret) {
134 dev_err(&led->priv->client->dev,
135 "Cannot write brightness\n");
136 goto brightness_out;
137 }
138
139 if (!led->enabled) {
140 ret = regmap_update_bits(led->priv->regmap,
141 LM3697_CTRL_ENABLE,
142 ctrl_en_val, ctrl_en_val);
143 if (ret) {
144 dev_err(&led->priv->client->dev,
145 "Cannot enable the device\n");
146 goto brightness_out;
147 }
148
149 led->enabled = brt_val;
150 }
151 }
152
153brightness_out:
154 mutex_unlock(&led->priv->lock);
155 return ret;
156}
157
158static int lm3697_init(struct lm3697 *priv)
159{
160 struct lm3697_led *led;
161 int i, ret;
162
163 if (priv->enable_gpio) {
164 gpiod_direction_output(priv->enable_gpio, 1);
165 } else {
166 ret = regmap_write(priv->regmap, LM3697_RESET, LM3697_SW_RESET);
167 if (ret) {
168 dev_err(&priv->client->dev, "Cannot reset the device\n");
169 goto out;
170 }
171 }
172
173 ret = regmap_write(priv->regmap, LM3697_CTRL_ENABLE, 0x0);
174 if (ret) {
175 dev_err(&priv->client->dev, "Cannot write ctrl enable\n");
176 goto out;
177 }
178
179 ret = regmap_write(priv->regmap, LM3697_OUTPUT_CONFIG, priv->bank_cfg);
180 if (ret)
181 dev_err(&priv->client->dev, "Cannot write OUTPUT config\n");
182
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);
186 if (ret)
187 dev_err(&priv->client->dev, "Setting the ramp rate failed\n");
188 }
189out:
190 return ret;
191}
192
193static int lm3697_probe_dt(struct lm3697 *priv)
194{
195 struct fwnode_handle *child = NULL;
196 struct lm3697_led *led;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500197 int control_bank;
198 size_t i = 0;
199 int ret = -EINVAL;
200 int j;
201
202 priv->enable_gpio = devm_gpiod_get_optional(&priv->client->dev,
203 "enable", GPIOD_OUT_LOW);
204 if (IS_ERR(priv->enable_gpio)) {
205 ret = PTR_ERR(priv->enable_gpio);
206 dev_err(&priv->client->dev, "Failed to get enable gpio: %d\n",
207 ret);
208 return ret;
209 }
210
211 priv->regulator = devm_regulator_get(&priv->client->dev, "vled");
212 if (IS_ERR(priv->regulator))
213 priv->regulator = NULL;
214
215 device_for_each_child_node(priv->dev, child) {
Marek BehĂșn3a953dc2020-09-18 00:33:02 +0200216 struct led_init_data init_data = {};
217
Dan Murphy5c1d8242019-05-06 14:16:14 -0500218 ret = fwnode_property_read_u32(child, "reg", &control_bank);
219 if (ret) {
220 dev_err(&priv->client->dev, "reg property missing\n");
221 fwnode_handle_put(child);
222 goto child_out;
223 }
224
225 if (control_bank > LM3697_CONTROL_B) {
226 dev_err(&priv->client->dev, "reg property is invalid\n");
227 ret = -EINVAL;
228 fwnode_handle_put(child);
229 goto child_out;
230 }
231
232 led = &priv->leds[i];
233
234 ret = ti_lmu_common_get_brt_res(&priv->client->dev,
235 child, &led->lmu_data);
236 if (ret)
237 dev_warn(&priv->client->dev, "brightness resolution property missing\n");
238
239 led->control_bank = control_bank;
240 led->lmu_data.regmap = priv->regmap;
241 led->lmu_data.runtime_ramp_reg = LM3697_CTRL_A_RAMP +
242 control_bank;
243 led->lmu_data.msb_brightness_reg = LM3697_CTRL_A_BRT_MSB +
244 led->control_bank * 2;
245 led->lmu_data.lsb_brightness_reg = LM3697_CTRL_A_BRT_LSB +
246 led->control_bank * 2;
247
Andy Shevchenko246eab592019-07-23 23:14:59 +0300248 led->num_leds = fwnode_property_count_u32(child, "led-sources");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500249 if (led->num_leds > LM3697_MAX_LED_STRINGS) {
Colin Ian Kingcc9c0772020-01-22 23:37:49 +0000250 dev_err(&priv->client->dev, "Too many LED strings defined\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500251 continue;
252 }
253
254 ret = fwnode_property_read_u32_array(child, "led-sources",
255 led->hvled_strings,
256 led->num_leds);
257 if (ret) {
258 dev_err(&priv->client->dev, "led-sources property missing\n");
259 fwnode_handle_put(child);
260 goto child_out;
261 }
262
263 for (j = 0; j < led->num_leds; j++)
264 priv->bank_cfg |=
265 (led->control_bank << led->hvled_strings[j]);
266
267 ret = ti_lmu_common_get_ramp_params(&priv->client->dev,
268 child, &led->lmu_data);
269 if (ret)
270 dev_warn(&priv->client->dev, "runtime-ramp properties missing\n");
271
272 fwnode_property_read_string(child, "linux,default-trigger",
273 &led->led_dev.default_trigger);
274
Marek BehĂșn3a953dc2020-09-18 00:33:02 +0200275 init_data.fwnode = child;
276 init_data.devicename = priv->client->name;
277 /* for backwards compatibility if `label` is not present */
278 init_data.default_label = ":";
Dan Murphy5c1d8242019-05-06 14:16:14 -0500279
280 led->priv = priv;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500281 led->led_dev.max_brightness = led->lmu_data.max_brightness;
282 led->led_dev.brightness_set_blocking = lm3697_brightness_set;
283
Marek BehĂșn3a953dc2020-09-18 00:33:02 +0200284 ret = devm_led_classdev_register_ext(priv->dev, &led->led_dev,
285 &init_data);
Dan Murphy5c1d8242019-05-06 14:16:14 -0500286 if (ret) {
287 dev_err(&priv->client->dev, "led register err: %d\n",
288 ret);
289 fwnode_handle_put(child);
290 goto child_out;
291 }
292
293 i++;
294 }
295
296child_out:
297 return ret;
298}
299
300static int lm3697_probe(struct i2c_client *client,
301 const struct i2c_device_id *id)
302{
303 struct lm3697 *led;
304 int count;
305 int ret;
306
307 count = device_get_child_node_count(&client->dev);
308 if (!count) {
309 dev_err(&client->dev, "LEDs are not defined in device tree!");
310 return -ENODEV;
311 }
312
313 led = devm_kzalloc(&client->dev, struct_size(led, leds, count),
314 GFP_KERNEL);
315 if (!led)
316 return -ENOMEM;
317
318 mutex_init(&led->lock);
319 i2c_set_clientdata(client, led);
320
321 led->client = client;
322 led->dev = &client->dev;
323 led->regmap = devm_regmap_init_i2c(client, &lm3697_regmap_config);
324 if (IS_ERR(led->regmap)) {
325 ret = PTR_ERR(led->regmap);
326 dev_err(&client->dev, "Failed to allocate register map: %d\n",
327 ret);
328 return ret;
329 }
330
331 ret = lm3697_probe_dt(led);
332 if (ret)
333 return ret;
334
335 return lm3697_init(led);
336}
337
338static int lm3697_remove(struct i2c_client *client)
339{
340 struct lm3697 *led = i2c_get_clientdata(client);
341 int ret;
342
343 ret = regmap_update_bits(led->regmap, LM3697_CTRL_ENABLE,
344 LM3697_CTRL_A_B_EN, 0);
345 if (ret) {
346 dev_err(&led->client->dev, "Failed to disable the device\n");
347 return ret;
348 }
349
350 if (led->enable_gpio)
351 gpiod_direction_output(led->enable_gpio, 0);
352
353 if (led->regulator) {
354 ret = regulator_disable(led->regulator);
355 if (ret)
356 dev_err(&led->client->dev,
357 "Failed to disable regulator\n");
358 }
359
360 mutex_destroy(&led->lock);
361
362 return 0;
363}
364
365static const struct i2c_device_id lm3697_id[] = {
366 { "lm3697", 0 },
367 { }
368};
369MODULE_DEVICE_TABLE(i2c, lm3697_id);
370
371static const struct of_device_id of_lm3697_leds_match[] = {
372 { .compatible = "ti,lm3697", },
373 {},
374};
375MODULE_DEVICE_TABLE(of, of_lm3697_leds_match);
376
377static struct i2c_driver lm3697_driver = {
378 .driver = {
379 .name = "lm3697",
380 .of_match_table = of_lm3697_leds_match,
381 },
382 .probe = lm3697_probe,
383 .remove = lm3697_remove,
384 .id_table = lm3697_id,
385};
386module_i2c_driver(lm3697_driver);
387
388MODULE_DESCRIPTION("Texas Instruments LM3697 LED driver");
389MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
390MODULE_LICENSE("GPL v2");