blob: a7779ce931ab30e67d50a857bfecdc987a20593a [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);
Marek Behún0b9e3572020-09-18 00:33:03 +0200118 struct device *dev = led->priv->dev;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500119 int ret;
120
121 mutex_lock(&led->priv->lock);
122
123 if (brt_val == LED_OFF) {
124 ret = regmap_update_bits(led->priv->regmap, LM3697_CTRL_ENABLE,
125 ctrl_en_val, ~ctrl_en_val);
126 if (ret) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200127 dev_err(dev, "Cannot write ctrl register\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500128 goto brightness_out;
129 }
130
131 led->enabled = LED_OFF;
132 } else {
133 ret = ti_lmu_common_set_brightness(&led->lmu_data, brt_val);
134 if (ret) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200135 dev_err(dev, "Cannot write brightness\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500136 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) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200144 dev_err(dev, "Cannot enable the device\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500145 goto brightness_out;
146 }
147
148 led->enabled = brt_val;
149 }
150 }
151
152brightness_out:
153 mutex_unlock(&led->priv->lock);
154 return ret;
155}
156
157static int lm3697_init(struct lm3697 *priv)
158{
Marek Behún0b9e3572020-09-18 00:33:03 +0200159 struct device *dev = priv->dev;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500160 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) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200168 dev_err(dev, "Cannot reset the device\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500169 goto out;
170 }
171 }
172
173 ret = regmap_write(priv->regmap, LM3697_CTRL_ENABLE, 0x0);
174 if (ret) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200175 dev_err(dev, "Cannot write ctrl enable\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500176 goto out;
177 }
178
179 ret = regmap_write(priv->regmap, LM3697_OUTPUT_CONFIG, priv->bank_cfg);
180 if (ret)
Marek Behún0b9e3572020-09-18 00:33:03 +0200181 dev_err(dev, "Cannot write OUTPUT config\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500182
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)
Marek Behún0b9e3572020-09-18 00:33:03 +0200187 dev_err(dev, "Setting the ramp rate failed\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500188 }
189out:
190 return ret;
191}
192
193static int lm3697_probe_dt(struct lm3697 *priv)
194{
195 struct fwnode_handle *child = NULL;
Marek Behún0b9e3572020-09-18 00:33:03 +0200196 struct device *dev = priv->dev;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500197 struct lm3697_led *led;
Marek Behún0b9e3572020-09-18 00:33:03 +0200198 int ret = -EINVAL;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500199 int control_bank;
200 size_t i = 0;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500201 int j;
202
Marek Behún0b9e3572020-09-18 00:33:03 +0200203 priv->enable_gpio = devm_gpiod_get_optional(dev, "enable",
204 GPIOD_OUT_LOW);
Dan Murphy5c1d8242019-05-06 14:16:14 -0500205 if (IS_ERR(priv->enable_gpio)) {
206 ret = PTR_ERR(priv->enable_gpio);
Marek Behún0b9e3572020-09-18 00:33:03 +0200207 dev_err(dev, "Failed to get enable gpio: %d\n", ret);
Dan Murphy5c1d8242019-05-06 14:16:14 -0500208 return ret;
209 }
210
Marek Behún0b9e3572020-09-18 00:33:03 +0200211 priv->regulator = devm_regulator_get(dev, "vled");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500212 if (IS_ERR(priv->regulator))
213 priv->regulator = NULL;
214
Marek Behún0b9e3572020-09-18 00:33:03 +0200215 device_for_each_child_node(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) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200220 dev_err(dev, "reg property missing\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500221 fwnode_handle_put(child);
222 goto child_out;
223 }
224
225 if (control_bank > LM3697_CONTROL_B) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200226 dev_err(dev, "reg property is invalid\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500227 ret = -EINVAL;
228 fwnode_handle_put(child);
229 goto child_out;
230 }
231
232 led = &priv->leds[i];
233
Marek Behún0b9e3572020-09-18 00:33:03 +0200234 ret = ti_lmu_common_get_brt_res(dev, child, &led->lmu_data);
Dan Murphy5c1d8242019-05-06 14:16:14 -0500235 if (ret)
Marek Behún0b9e3572020-09-18 00:33:03 +0200236 dev_warn(dev,
237 "brightness resolution property missing\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500238
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) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200250 dev_err(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) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200258 dev_err(dev, "led-sources property missing\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500259 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
Marek Behún0b9e3572020-09-18 00:33:03 +0200267 ret = ti_lmu_common_get_ramp_params(dev, child, &led->lmu_data);
Dan Murphy5c1d8242019-05-06 14:16:14 -0500268 if (ret)
Marek Behún0b9e3572020-09-18 00:33:03 +0200269 dev_warn(dev, "runtime-ramp properties missing\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500270
271 fwnode_property_read_string(child, "linux,default-trigger",
272 &led->led_dev.default_trigger);
273
Marek Behún3a953dc2020-09-18 00:33:02 +0200274 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 = ":";
Dan Murphy5c1d8242019-05-06 14:16:14 -0500278
279 led->priv = priv;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500280 led->led_dev.max_brightness = led->lmu_data.max_brightness;
281 led->led_dev.brightness_set_blocking = lm3697_brightness_set;
282
Marek Behún0b9e3572020-09-18 00:33:03 +0200283 ret = devm_led_classdev_register_ext(dev, &led->led_dev,
Marek Behún3a953dc2020-09-18 00:33:02 +0200284 &init_data);
Dan Murphy5c1d8242019-05-06 14:16:14 -0500285 if (ret) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200286 dev_err(dev, "led register err: %d\n", ret);
Dan Murphy5c1d8242019-05-06 14:16:14 -0500287 fwnode_handle_put(child);
288 goto child_out;
289 }
290
291 i++;
292 }
293
294child_out:
295 return ret;
296}
297
298static int lm3697_probe(struct i2c_client *client,
299 const struct i2c_device_id *id)
300{
Marek Behún0b9e3572020-09-18 00:33:03 +0200301 struct device *dev = &client->dev;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500302 struct lm3697 *led;
303 int count;
304 int ret;
305
Marek Behún0b9e3572020-09-18 00:33:03 +0200306 count = device_get_child_node_count(dev);
Dan Murphy5c1d8242019-05-06 14:16:14 -0500307 if (!count) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200308 dev_err(dev, "LEDs are not defined in device tree!");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500309 return -ENODEV;
310 }
311
Marek Behún0b9e3572020-09-18 00:33:03 +0200312 led = devm_kzalloc(dev, struct_size(led, leds, count), GFP_KERNEL);
Dan Murphy5c1d8242019-05-06 14:16:14 -0500313 if (!led)
314 return -ENOMEM;
315
316 mutex_init(&led->lock);
317 i2c_set_clientdata(client, led);
318
319 led->client = client;
Marek Behún0b9e3572020-09-18 00:33:03 +0200320 led->dev = dev;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500321 led->regmap = devm_regmap_init_i2c(client, &lm3697_regmap_config);
322 if (IS_ERR(led->regmap)) {
323 ret = PTR_ERR(led->regmap);
Marek Behún0b9e3572020-09-18 00:33:03 +0200324 dev_err(dev, "Failed to allocate register map: %d\n", ret);
Dan Murphy5c1d8242019-05-06 14:16:14 -0500325 return ret;
326 }
327
328 ret = lm3697_probe_dt(led);
329 if (ret)
330 return ret;
331
332 return lm3697_init(led);
333}
334
335static int lm3697_remove(struct i2c_client *client)
336{
337 struct lm3697 *led = i2c_get_clientdata(client);
Marek Behún0b9e3572020-09-18 00:33:03 +0200338 struct device *dev = &led->client->dev;
Dan Murphy5c1d8242019-05-06 14:16:14 -0500339 int ret;
340
341 ret = regmap_update_bits(led->regmap, LM3697_CTRL_ENABLE,
342 LM3697_CTRL_A_B_EN, 0);
343 if (ret) {
Marek Behún0b9e3572020-09-18 00:33:03 +0200344 dev_err(dev, "Failed to disable the device\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500345 return ret;
346 }
347
348 if (led->enable_gpio)
349 gpiod_direction_output(led->enable_gpio, 0);
350
351 if (led->regulator) {
352 ret = regulator_disable(led->regulator);
353 if (ret)
Marek Behún0b9e3572020-09-18 00:33:03 +0200354 dev_err(dev, "Failed to disable regulator\n");
Dan Murphy5c1d8242019-05-06 14:16:14 -0500355 }
356
357 mutex_destroy(&led->lock);
358
359 return 0;
360}
361
362static const struct i2c_device_id lm3697_id[] = {
363 { "lm3697", 0 },
364 { }
365};
366MODULE_DEVICE_TABLE(i2c, lm3697_id);
367
368static const struct of_device_id of_lm3697_leds_match[] = {
369 { .compatible = "ti,lm3697", },
370 {},
371};
372MODULE_DEVICE_TABLE(of, of_lm3697_leds_match);
373
374static struct i2c_driver lm3697_driver = {
375 .driver = {
376 .name = "lm3697",
377 .of_match_table = of_lm3697_leds_match,
378 },
379 .probe = lm3697_probe,
380 .remove = lm3697_remove,
381 .id_table = lm3697_id,
382};
383module_i2c_driver(lm3697_driver);
384
385MODULE_DESCRIPTION("Texas Instruments LM3697 LED driver");
386MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
387MODULE_LICENSE("GPL v2");