xref: /linux/drivers/leds/leds-lm3692x.c (revision 4f797f56c3786e2c6bc542b3f80e9a599b073976)
1 // SPDX-License-Identifier: GPL-2.0
2 // TI LM3692x LED chip family driver
3 // Copyright (C) 2017-18 Texas Instruments Incorporated - http://www.ti.com/
4 
5 #include <linux/gpio/consumer.h>
6 #include <linux/i2c.h>
7 #include <linux/init.h>
8 #include <linux/leds.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/of.h>
12 #include <linux/of_gpio.h>
13 #include <linux/regmap.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/slab.h>
16 
17 #define LM36922_MODEL	0
18 #define LM36923_MODEL	1
19 
20 #define LM3692X_REV		0x0
21 #define LM3692X_RESET		0x1
22 #define LM3692X_EN		0x10
23 #define LM3692X_BRT_CTRL	0x11
24 #define LM3692X_PWM_CTRL	0x12
25 #define LM3692X_BOOST_CTRL	0x13
26 #define LM3692X_AUTO_FREQ_HI	0x15
27 #define LM3692X_AUTO_FREQ_LO	0x16
28 #define LM3692X_BL_ADJ_THRESH	0x17
29 #define LM3692X_BRT_LSB		0x18
30 #define LM3692X_BRT_MSB		0x19
31 #define LM3692X_FAULT_CTRL	0x1e
32 #define LM3692X_FAULT_FLAGS	0x1f
33 
34 #define LM3692X_SW_RESET	BIT(0)
35 #define LM3692X_DEVICE_EN	BIT(0)
36 #define LM3692X_LED1_EN		BIT(1)
37 #define LM3692X_LED2_EN		BIT(2)
38 #define LM36923_LED3_EN		BIT(3)
39 #define LM3692X_ENABLE_MASK	(LM3692X_DEVICE_EN | LM3692X_LED1_EN | \
40 				 LM3692X_LED2_EN | LM36923_LED3_EN)
41 
42 /* Brightness Control Bits */
43 #define LM3692X_BL_ADJ_POL	BIT(0)
44 #define LM3692X_RAMP_RATE_125us	0x00
45 #define LM3692X_RAMP_RATE_250us	BIT(1)
46 #define LM3692X_RAMP_RATE_500us BIT(2)
47 #define LM3692X_RAMP_RATE_1ms	(BIT(1) | BIT(2))
48 #define LM3692X_RAMP_RATE_2ms	BIT(3)
49 #define LM3692X_RAMP_RATE_4ms	(BIT(3) | BIT(1))
50 #define LM3692X_RAMP_RATE_8ms	(BIT(2) | BIT(3))
51 #define LM3692X_RAMP_RATE_16ms	(BIT(1) | BIT(2) | BIT(3))
52 #define LM3692X_RAMP_EN		BIT(4)
53 #define LM3692X_BRHT_MODE_REG	0x00
54 #define LM3692X_BRHT_MODE_PWM	BIT(5)
55 #define LM3692X_BRHT_MODE_MULTI_RAMP BIT(6)
56 #define LM3692X_BRHT_MODE_RAMP_MULTI (BIT(5) | BIT(6))
57 #define LM3692X_MAP_MODE_EXP	BIT(7)
58 
59 /* PWM Register Bits */
60 #define LM3692X_PWM_FILTER_100	BIT(0)
61 #define LM3692X_PWM_FILTER_150	BIT(1)
62 #define LM3692X_PWM_FILTER_200	(BIT(0) | BIT(1))
63 #define LM3692X_PWM_HYSTER_1LSB BIT(2)
64 #define LM3692X_PWM_HYSTER_2LSB	BIT(3)
65 #define LM3692X_PWM_HYSTER_3LSB (BIT(3) | BIT(2))
66 #define LM3692X_PWM_HYSTER_4LSB BIT(4)
67 #define LM3692X_PWM_HYSTER_5LSB (BIT(4) | BIT(2))
68 #define LM3692X_PWM_HYSTER_6LSB (BIT(4) | BIT(3))
69 #define LM3692X_PWM_POLARITY	BIT(5)
70 #define LM3692X_PWM_SAMP_4MHZ	BIT(6)
71 #define LM3692X_PWM_SAMP_24MHZ	BIT(7)
72 
73 /* Boost Control Bits */
74 #define LM3692X_OCP_PROT_1A	BIT(0)
75 #define LM3692X_OCP_PROT_1_25A	BIT(1)
76 #define LM3692X_OCP_PROT_1_5A	(BIT(0) | BIT(1))
77 #define LM3692X_OVP_21V		BIT(2)
78 #define LM3692X_OVP_25V		BIT(3)
79 #define LM3692X_OVP_29V		(BIT(2) | BIT(3))
80 #define LM3692X_MIN_IND_22UH	BIT(4)
81 #define LM3692X_BOOST_SW_1MHZ	BIT(5)
82 #define LM3692X_BOOST_SW_NO_SHIFT	BIT(6)
83 
84 /* Fault Control Bits */
85 #define LM3692X_FAULT_CTRL_OVP BIT(0)
86 #define LM3692X_FAULT_CTRL_OCP BIT(1)
87 #define LM3692X_FAULT_CTRL_TSD BIT(2)
88 #define LM3692X_FAULT_CTRL_OPEN BIT(3)
89 
90 /* Fault Flag Bits */
91 #define LM3692X_FAULT_FLAG_OVP BIT(0)
92 #define LM3692X_FAULT_FLAG_OCP BIT(1)
93 #define LM3692X_FAULT_FLAG_TSD BIT(2)
94 #define LM3692X_FAULT_FLAG_SHRT BIT(3)
95 #define LM3692X_FAULT_FLAG_OPEN BIT(4)
96 
97 /**
98  * struct lm3692x_led -
99  * @lock - Lock for reading/writing the device
100  * @client - Pointer to the I2C client
101  * @led_dev - LED class device pointer
102  * @regmap - Devices register map
103  * @enable_gpio - VDDIO/EN gpio to enable communication interface
104  * @regulator - LED supply regulator pointer
105  * @led_enable - LED sync to be enabled
106  * @model_id - Current device model ID enumerated
107  */
108 struct lm3692x_led {
109 	struct mutex lock;
110 	struct i2c_client *client;
111 	struct led_classdev led_dev;
112 	struct regmap *regmap;
113 	struct gpio_desc *enable_gpio;
114 	struct regulator *regulator;
115 	int led_enable;
116 	int model_id;
117 };
118 
119 static const struct reg_default lm3692x_reg_defs[] = {
120 	{LM3692X_EN, 0xf},
121 	{LM3692X_BRT_CTRL, 0x61},
122 	{LM3692X_PWM_CTRL, 0x73},
123 	{LM3692X_BOOST_CTRL, 0x6f},
124 	{LM3692X_AUTO_FREQ_HI, 0x0},
125 	{LM3692X_AUTO_FREQ_LO, 0x0},
126 	{LM3692X_BL_ADJ_THRESH, 0x0},
127 	{LM3692X_BRT_LSB, 0x7},
128 	{LM3692X_BRT_MSB, 0xff},
129 	{LM3692X_FAULT_CTRL, 0x7},
130 };
131 
132 static const struct regmap_config lm3692x_regmap_config = {
133 	.reg_bits = 8,
134 	.val_bits = 8,
135 
136 	.max_register = LM3692X_FAULT_FLAGS,
137 	.reg_defaults = lm3692x_reg_defs,
138 	.num_reg_defaults = ARRAY_SIZE(lm3692x_reg_defs),
139 	.cache_type = REGCACHE_RBTREE,
140 };
141 
142 static int lm3692x_fault_check(struct lm3692x_led *led)
143 {
144 	int ret;
145 	unsigned int read_buf;
146 
147 	ret = regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf);
148 	if (ret)
149 		return ret;
150 
151 	if (read_buf)
152 		dev_err(&led->client->dev, "Detected a fault 0x%X\n", read_buf);
153 
154 	/* The first read may clear the fault.  Check again to see if the fault
155 	 * still exits and return that value.
156 	 */
157 	regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf);
158 	if (read_buf)
159 		dev_err(&led->client->dev, "Second read of fault flags 0x%X\n",
160 			read_buf);
161 
162 	return read_buf;
163 }
164 
165 static int lm3692x_brightness_set(struct led_classdev *led_cdev,
166 				enum led_brightness brt_val)
167 {
168 	struct lm3692x_led *led =
169 			container_of(led_cdev, struct lm3692x_led, led_dev);
170 	int ret;
171 	int led_brightness_lsb = (brt_val >> 5);
172 
173 	mutex_lock(&led->lock);
174 
175 	ret = lm3692x_fault_check(led);
176 	if (ret) {
177 		dev_err(&led->client->dev, "Cannot read/clear faults: %d\n",
178 			ret);
179 		goto out;
180 	}
181 
182 	ret = regmap_write(led->regmap, LM3692X_BRT_MSB, brt_val);
183 	if (ret) {
184 		dev_err(&led->client->dev, "Cannot write MSB: %d\n", ret);
185 		goto out;
186 	}
187 
188 	ret = regmap_write(led->regmap, LM3692X_BRT_LSB, led_brightness_lsb);
189 	if (ret) {
190 		dev_err(&led->client->dev, "Cannot write LSB: %d\n", ret);
191 		goto out;
192 	}
193 out:
194 	mutex_unlock(&led->lock);
195 	return ret;
196 }
197 
198 static int lm3692x_init(struct lm3692x_led *led)
199 {
200 	int enable_state;
201 	int ret, reg_ret;
202 
203 	if (led->regulator) {
204 		ret = regulator_enable(led->regulator);
205 		if (ret) {
206 			dev_err(&led->client->dev,
207 				"Failed to enable regulator: %d\n", ret);
208 			return ret;
209 		}
210 	}
211 
212 	if (led->enable_gpio)
213 		gpiod_direction_output(led->enable_gpio, 1);
214 
215 	ret = lm3692x_fault_check(led);
216 	if (ret) {
217 		dev_err(&led->client->dev, "Cannot read/clear faults: %d\n",
218 			ret);
219 		goto out;
220 	}
221 
222 	ret = regmap_write(led->regmap, LM3692X_BRT_CTRL, 0x00);
223 	if (ret)
224 		goto out;
225 
226 	/*
227 	 * For glitch free operation, the following data should
228 	 * only be written while LEDx enable bits are 0 and the device enable
229 	 * bit is set to 1.
230 	 * per Section 7.5.14 of the data sheet
231 	 */
232 	ret = regmap_write(led->regmap, LM3692X_EN, LM3692X_DEVICE_EN);
233 	if (ret)
234 		goto out;
235 
236 	/* Set the brightness to 0 so when enabled the LEDs do not come
237 	 * on with full brightness.
238 	 */
239 	ret = regmap_write(led->regmap, LM3692X_BRT_MSB, 0);
240 	if (ret)
241 		goto out;
242 
243 	ret = regmap_write(led->regmap, LM3692X_BRT_LSB, 0);
244 	if (ret)
245 		goto out;
246 
247 	ret = regmap_write(led->regmap, LM3692X_PWM_CTRL,
248 		LM3692X_PWM_FILTER_100 | LM3692X_PWM_SAMP_24MHZ);
249 	if (ret)
250 		goto out;
251 
252 	ret = regmap_write(led->regmap, LM3692X_BOOST_CTRL,
253 			LM3692X_BOOST_SW_1MHZ |
254 			LM3692X_BOOST_SW_NO_SHIFT |
255 			LM3692X_OCP_PROT_1_5A);
256 	if (ret)
257 		goto out;
258 
259 	ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_HI, 0x00);
260 	if (ret)
261 		goto out;
262 
263 	ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_LO, 0x00);
264 	if (ret)
265 		goto out;
266 
267 	ret = regmap_write(led->regmap, LM3692X_BL_ADJ_THRESH, 0x00);
268 	if (ret)
269 		goto out;
270 
271 	ret = regmap_write(led->regmap, LM3692X_BRT_CTRL,
272 			LM3692X_BL_ADJ_POL | LM3692X_RAMP_EN);
273 	if (ret)
274 		goto out;
275 
276 	switch (led->led_enable) {
277 	case 0:
278 	default:
279 		if (led->model_id == LM36923_MODEL)
280 			enable_state = LM3692X_LED1_EN | LM3692X_LED2_EN |
281 			       LM36923_LED3_EN;
282 		else
283 			enable_state = LM3692X_LED1_EN | LM3692X_LED2_EN;
284 
285 		break;
286 	case 1:
287 		enable_state = LM3692X_LED1_EN;
288 		break;
289 	case 2:
290 		enable_state = LM3692X_LED2_EN;
291 		break;
292 
293 	case 3:
294 		if (led->model_id == LM36923_MODEL) {
295 			enable_state = LM36923_LED3_EN;
296 			break;
297 		}
298 
299 		ret = -EINVAL;
300 		dev_err(&led->client->dev,
301 			"LED3 sync not available on this device\n");
302 		goto out;
303 	}
304 
305 	ret = regmap_update_bits(led->regmap, LM3692X_EN, LM3692X_ENABLE_MASK,
306 				 enable_state | LM3692X_DEVICE_EN);
307 
308 	return ret;
309 out:
310 	dev_err(&led->client->dev, "Fail writing initialization values\n");
311 
312 	if (led->enable_gpio)
313 		gpiod_direction_output(led->enable_gpio, 0);
314 
315 	if (led->regulator) {
316 		reg_ret = regulator_disable(led->regulator);
317 		if (reg_ret)
318 			dev_err(&led->client->dev,
319 				"Failed to disable regulator: %d\n", reg_ret);
320 	}
321 
322 	return ret;
323 }
324 
325 static int lm3692x_probe_dt(struct lm3692x_led *led)
326 {
327 	struct fwnode_handle *child = NULL;
328 	struct led_init_data init_data = {};
329 	int ret;
330 
331 	led->enable_gpio = devm_gpiod_get_optional(&led->client->dev,
332 						   "enable", GPIOD_OUT_LOW);
333 	if (IS_ERR(led->enable_gpio)) {
334 		ret = PTR_ERR(led->enable_gpio);
335 		dev_err(&led->client->dev, "Failed to get enable gpio: %d\n",
336 			ret);
337 		return ret;
338 	}
339 
340 	led->regulator = devm_regulator_get_optional(&led->client->dev, "vled");
341 	if (IS_ERR(led->regulator)) {
342 		ret = PTR_ERR(led->regulator);
343 		if (ret != -ENODEV) {
344 			if (ret != -EPROBE_DEFER)
345 				dev_err(&led->client->dev,
346 					"Failed to get vled regulator: %d\n",
347 					ret);
348 			return ret;
349 		}
350 		led->regulator = NULL;
351 	}
352 
353 	child = device_get_next_child_node(&led->client->dev, child);
354 	if (!child) {
355 		dev_err(&led->client->dev, "No LED Child node\n");
356 		return -ENODEV;
357 	}
358 
359 	fwnode_property_read_string(child, "linux,default-trigger",
360 				    &led->led_dev.default_trigger);
361 
362 	ret = fwnode_property_read_u32(child, "reg", &led->led_enable);
363 	if (ret) {
364 		dev_err(&led->client->dev, "reg DT property missing\n");
365 		return ret;
366 	}
367 
368 	init_data.fwnode = child;
369 	init_data.devicename = led->client->name;
370 	init_data.default_label = ":";
371 
372 	ret = devm_led_classdev_register_ext(&led->client->dev, &led->led_dev,
373 					     &init_data);
374 	if (ret) {
375 		dev_err(&led->client->dev, "led register err: %d\n", ret);
376 		return ret;
377 	}
378 
379 	return 0;
380 }
381 
382 static int lm3692x_probe(struct i2c_client *client,
383 			const struct i2c_device_id *id)
384 {
385 	struct lm3692x_led *led;
386 	int ret;
387 
388 	led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL);
389 	if (!led)
390 		return -ENOMEM;
391 
392 	mutex_init(&led->lock);
393 	led->client = client;
394 	led->led_dev.brightness_set_blocking = lm3692x_brightness_set;
395 	led->model_id = id->driver_data;
396 	i2c_set_clientdata(client, led);
397 
398 	led->regmap = devm_regmap_init_i2c(client, &lm3692x_regmap_config);
399 	if (IS_ERR(led->regmap)) {
400 		ret = PTR_ERR(led->regmap);
401 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
402 			ret);
403 		return ret;
404 	}
405 
406 	ret = lm3692x_probe_dt(led);
407 	if (ret)
408 		return ret;
409 
410 	ret = lm3692x_init(led);
411 	if (ret)
412 		return ret;
413 
414 	return 0;
415 }
416 
417 static int lm3692x_remove(struct i2c_client *client)
418 {
419 	struct lm3692x_led *led = i2c_get_clientdata(client);
420 	int ret;
421 
422 	ret = regmap_update_bits(led->regmap, LM3692X_EN, LM3692X_DEVICE_EN, 0);
423 	if (ret) {
424 		dev_err(&led->client->dev, "Failed to disable regulator: %d\n",
425 			ret);
426 		return ret;
427 	}
428 
429 	if (led->enable_gpio)
430 		gpiod_direction_output(led->enable_gpio, 0);
431 
432 	if (led->regulator) {
433 		ret = regulator_disable(led->regulator);
434 		if (ret)
435 			dev_err(&led->client->dev,
436 				"Failed to disable regulator: %d\n", ret);
437 	}
438 
439 	mutex_destroy(&led->lock);
440 
441 	return 0;
442 }
443 
444 static const struct i2c_device_id lm3692x_id[] = {
445 	{ "lm36922", LM36922_MODEL },
446 	{ "lm36923", LM36923_MODEL },
447 	{ }
448 };
449 MODULE_DEVICE_TABLE(i2c, lm3692x_id);
450 
451 static const struct of_device_id of_lm3692x_leds_match[] = {
452 	{ .compatible = "ti,lm36922", },
453 	{ .compatible = "ti,lm36923", },
454 	{},
455 };
456 MODULE_DEVICE_TABLE(of, of_lm3692x_leds_match);
457 
458 static struct i2c_driver lm3692x_driver = {
459 	.driver = {
460 		.name	= "lm3692x",
461 		.of_match_table = of_lm3692x_leds_match,
462 	},
463 	.probe		= lm3692x_probe,
464 	.remove		= lm3692x_remove,
465 	.id_table	= lm3692x_id,
466 };
467 module_i2c_driver(lm3692x_driver);
468 
469 MODULE_DESCRIPTION("Texas Instruments LM3692X LED driver");
470 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
471 MODULE_LICENSE("GPL v2");
472