• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
lm3692x_fault_check(struct lm3692x_led * led)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 
lm3692x_brightness_set(struct led_classdev * led_cdev,enum led_brightness brt_val)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\n");
178 		goto out;
179 	}
180 
181 	ret = regmap_write(led->regmap, LM3692X_BRT_MSB, brt_val);
182 	if (ret) {
183 		dev_err(&led->client->dev, "Cannot write MSB\n");
184 		goto out;
185 	}
186 
187 	ret = regmap_write(led->regmap, LM3692X_BRT_LSB, led_brightness_lsb);
188 	if (ret) {
189 		dev_err(&led->client->dev, "Cannot write LSB\n");
190 		goto out;
191 	}
192 out:
193 	mutex_unlock(&led->lock);
194 	return ret;
195 }
196 
lm3692x_init(struct lm3692x_led * led)197 static int lm3692x_init(struct lm3692x_led *led)
198 {
199 	int enable_state;
200 	int ret;
201 
202 	if (led->regulator) {
203 		ret = regulator_enable(led->regulator);
204 		if (ret) {
205 			dev_err(&led->client->dev,
206 				"Failed to enable regulator\n");
207 			return ret;
208 		}
209 	}
210 
211 	if (led->enable_gpio)
212 		gpiod_direction_output(led->enable_gpio, 1);
213 
214 	ret = lm3692x_fault_check(led);
215 	if (ret) {
216 		dev_err(&led->client->dev, "Cannot read/clear faults\n");
217 		goto out;
218 	}
219 
220 	ret = regmap_write(led->regmap, LM3692X_BRT_CTRL, 0x00);
221 	if (ret)
222 		goto out;
223 
224 	/*
225 	 * For glitch free operation, the following data should
226 	 * only be written while LEDx enable bits are 0 and the device enable
227 	 * bit is set to 1.
228 	 * per Section 7.5.14 of the data sheet
229 	 */
230 	ret = regmap_write(led->regmap, LM3692X_EN, LM3692X_DEVICE_EN);
231 	if (ret)
232 		goto out;
233 
234 	/* Set the brightness to 0 so when enabled the LEDs do not come
235 	 * on with full brightness.
236 	 */
237 	ret = regmap_write(led->regmap, LM3692X_BRT_MSB, 0);
238 	if (ret)
239 		goto out;
240 
241 	ret = regmap_write(led->regmap, LM3692X_BRT_LSB, 0);
242 	if (ret)
243 		goto out;
244 
245 	ret = regmap_write(led->regmap, LM3692X_PWM_CTRL,
246 		LM3692X_PWM_FILTER_100 | LM3692X_PWM_SAMP_24MHZ);
247 	if (ret)
248 		goto out;
249 
250 	ret = regmap_write(led->regmap, LM3692X_BOOST_CTRL,
251 			LM3692X_BRHT_MODE_RAMP_MULTI |
252 			LM3692X_BL_ADJ_POL |
253 			LM3692X_RAMP_RATE_250us);
254 	if (ret)
255 		goto out;
256 
257 	ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_HI, 0x00);
258 	if (ret)
259 		goto out;
260 
261 	ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_LO, 0x00);
262 	if (ret)
263 		goto out;
264 
265 	ret = regmap_write(led->regmap, LM3692X_BL_ADJ_THRESH, 0x00);
266 	if (ret)
267 		goto out;
268 
269 	ret = regmap_write(led->regmap, LM3692X_BRT_CTRL,
270 			LM3692X_BL_ADJ_POL | LM3692X_PWM_HYSTER_4LSB);
271 	if (ret)
272 		goto out;
273 
274 	switch (led->led_enable) {
275 	case 0:
276 	default:
277 		if (led->model_id == LM36923_MODEL)
278 			enable_state = LM3692X_LED1_EN | LM3692X_LED2_EN |
279 			       LM36923_LED3_EN;
280 		else
281 			enable_state = LM3692X_LED1_EN | LM3692X_LED2_EN;
282 
283 		break;
284 	case 1:
285 		enable_state = LM3692X_LED1_EN;
286 		break;
287 	case 2:
288 		enable_state = LM3692X_LED2_EN;
289 		break;
290 
291 	case 3:
292 		if (led->model_id == LM36923_MODEL) {
293 			enable_state = LM36923_LED3_EN;
294 			break;
295 		}
296 
297 		ret = -EINVAL;
298 		dev_err(&led->client->dev,
299 			"LED3 sync not available on this device\n");
300 		goto out;
301 	}
302 
303 	ret = regmap_update_bits(led->regmap, LM3692X_EN, LM3692X_ENABLE_MASK,
304 				 enable_state | LM3692X_DEVICE_EN);
305 
306 	return ret;
307 out:
308 	dev_err(&led->client->dev, "Fail writing initialization values\n");
309 
310 	if (led->enable_gpio)
311 		gpiod_direction_output(led->enable_gpio, 0);
312 
313 	if (led->regulator) {
314 		ret = regulator_disable(led->regulator);
315 		if (ret)
316 			dev_err(&led->client->dev,
317 				"Failed to disable regulator\n");
318 	}
319 
320 	return ret;
321 }
lm3692x_probe_dt(struct lm3692x_led * led)322 static int lm3692x_probe_dt(struct lm3692x_led *led)
323 {
324 	struct fwnode_handle *child = NULL;
325 	struct led_init_data init_data = {};
326 	int ret;
327 
328 	led->enable_gpio = devm_gpiod_get_optional(&led->client->dev,
329 						   "enable", GPIOD_OUT_LOW);
330 	if (IS_ERR(led->enable_gpio)) {
331 		ret = PTR_ERR(led->enable_gpio);
332 		dev_err(&led->client->dev, "Failed to get enable gpio: %d\n",
333 			ret);
334 		return ret;
335 	}
336 
337 	led->regulator = devm_regulator_get_optional(&led->client->dev, "vled");
338 	if (IS_ERR(led->regulator)) {
339 		ret = PTR_ERR(led->regulator);
340 		if (ret != -ENODEV) {
341 			if (ret != -EPROBE_DEFER)
342 				dev_err(&led->client->dev,
343 					"Failed to get vled regulator: %d\n",
344 					ret);
345 			return ret;
346 		}
347 		led->regulator = NULL;
348 	}
349 
350 	child = device_get_next_child_node(&led->client->dev, child);
351 	if (!child) {
352 		dev_err(&led->client->dev, "No LED Child node\n");
353 		return -ENODEV;
354 	}
355 
356 	fwnode_property_read_string(child, "linux,default-trigger",
357 				    &led->led_dev.default_trigger);
358 
359 	ret = fwnode_property_read_u32(child, "reg", &led->led_enable);
360 	if (ret) {
361 		dev_err(&led->client->dev, "reg DT property missing\n");
362 		return ret;
363 	}
364 
365 	init_data.fwnode = child;
366 	init_data.devicename = led->client->name;
367 	init_data.default_label = ":";
368 
369 	ret = devm_led_classdev_register_ext(&led->client->dev, &led->led_dev,
370 					     &init_data);
371 	if (ret) {
372 		dev_err(&led->client->dev, "led register err: %d\n", ret);
373 		return ret;
374 	}
375 
376 	return 0;
377 }
378 
lm3692x_probe(struct i2c_client * client,const struct i2c_device_id * id)379 static int lm3692x_probe(struct i2c_client *client,
380 			const struct i2c_device_id *id)
381 {
382 	struct lm3692x_led *led;
383 	int ret;
384 
385 	led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL);
386 	if (!led)
387 		return -ENOMEM;
388 
389 	mutex_init(&led->lock);
390 	led->client = client;
391 	led->led_dev.brightness_set_blocking = lm3692x_brightness_set;
392 	led->model_id = id->driver_data;
393 	i2c_set_clientdata(client, led);
394 
395 	led->regmap = devm_regmap_init_i2c(client, &lm3692x_regmap_config);
396 	if (IS_ERR(led->regmap)) {
397 		ret = PTR_ERR(led->regmap);
398 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
399 			ret);
400 		return ret;
401 	}
402 
403 	ret = lm3692x_probe_dt(led);
404 	if (ret)
405 		return ret;
406 
407 	ret = lm3692x_init(led);
408 	if (ret)
409 		return ret;
410 
411 	return 0;
412 }
413 
lm3692x_remove(struct i2c_client * client)414 static int lm3692x_remove(struct i2c_client *client)
415 {
416 	struct lm3692x_led *led = i2c_get_clientdata(client);
417 	int ret;
418 
419 	ret = regmap_update_bits(led->regmap, LM3692X_EN, LM3692X_DEVICE_EN, 0);
420 	if (ret) {
421 		dev_err(&led->client->dev, "Failed to disable regulator\n");
422 		return ret;
423 	}
424 
425 	if (led->enable_gpio)
426 		gpiod_direction_output(led->enable_gpio, 0);
427 
428 	if (led->regulator) {
429 		ret = regulator_disable(led->regulator);
430 		if (ret)
431 			dev_err(&led->client->dev,
432 				"Failed to disable regulator\n");
433 	}
434 
435 	mutex_destroy(&led->lock);
436 
437 	return 0;
438 }
439 
440 static const struct i2c_device_id lm3692x_id[] = {
441 	{ "lm36922", LM36922_MODEL },
442 	{ "lm36923", LM36923_MODEL },
443 	{ }
444 };
445 MODULE_DEVICE_TABLE(i2c, lm3692x_id);
446 
447 static const struct of_device_id of_lm3692x_leds_match[] = {
448 	{ .compatible = "ti,lm36922", },
449 	{ .compatible = "ti,lm36923", },
450 	{},
451 };
452 MODULE_DEVICE_TABLE(of, of_lm3692x_leds_match);
453 
454 static struct i2c_driver lm3692x_driver = {
455 	.driver = {
456 		.name	= "lm3692x",
457 		.of_match_table = of_lm3692x_leds_match,
458 	},
459 	.probe		= lm3692x_probe,
460 	.remove		= lm3692x_remove,
461 	.id_table	= lm3692x_id,
462 };
463 module_i2c_driver(lm3692x_driver);
464 
465 MODULE_DESCRIPTION("Texas Instruments LM3692X LED driver");
466 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
467 MODULE_LICENSE("GPL v2");
468