xref: /linux/drivers/video/backlight/lm3630a_bl.c (revision b8d312aa075f33282565467662c4628dae0a2aff)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Simple driver for Texas Instruments LM3630A Backlight driver chip
4 * Copyright (C) 2012 Texas Instruments
5 */
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/i2c.h>
9 #include <linux/backlight.h>
10 #include <linux/err.h>
11 #include <linux/delay.h>
12 #include <linux/uaccess.h>
13 #include <linux/interrupt.h>
14 #include <linux/regmap.h>
15 #include <linux/pwm.h>
16 #include <linux/platform_data/lm3630a_bl.h>
17 
18 #define REG_CTRL	0x00
19 #define REG_BOOST	0x02
20 #define REG_CONFIG	0x01
21 #define REG_BRT_A	0x03
22 #define REG_BRT_B	0x04
23 #define REG_I_A		0x05
24 #define REG_I_B		0x06
25 #define REG_INT_STATUS	0x09
26 #define REG_INT_EN	0x0A
27 #define REG_FAULT	0x0B
28 #define REG_PWM_OUTLOW	0x12
29 #define REG_PWM_OUTHIGH	0x13
30 #define REG_FILTER_STRENGTH	0x50
31 #define REG_MAX		0x50
32 
33 #define INT_DEBOUNCE_MSEC	10
34 
35 #define LM3630A_BANK_0		0
36 #define LM3630A_BANK_1		1
37 
38 #define LM3630A_NUM_SINKS	2
39 #define LM3630A_SINK_0		0
40 #define LM3630A_SINK_1		1
41 
42 struct lm3630a_chip {
43 	struct device *dev;
44 	struct delayed_work work;
45 
46 	int irq;
47 	struct workqueue_struct *irqthread;
48 	struct lm3630a_platform_data *pdata;
49 	struct backlight_device *bleda;
50 	struct backlight_device *bledb;
51 	struct regmap *regmap;
52 	struct pwm_device *pwmd;
53 };
54 
55 /* i2c access */
56 static int lm3630a_read(struct lm3630a_chip *pchip, unsigned int reg)
57 {
58 	int rval;
59 	unsigned int reg_val;
60 
61 	rval = regmap_read(pchip->regmap, reg, &reg_val);
62 	if (rval < 0)
63 		return rval;
64 	return reg_val & 0xFF;
65 }
66 
67 static int lm3630a_write(struct lm3630a_chip *pchip,
68 			 unsigned int reg, unsigned int data)
69 {
70 	return regmap_write(pchip->regmap, reg, data);
71 }
72 
73 static int lm3630a_update(struct lm3630a_chip *pchip,
74 			  unsigned int reg, unsigned int mask,
75 			  unsigned int data)
76 {
77 	return regmap_update_bits(pchip->regmap, reg, mask, data);
78 }
79 
80 /* initialize chip */
81 static int lm3630a_chip_init(struct lm3630a_chip *pchip)
82 {
83 	int rval;
84 	struct lm3630a_platform_data *pdata = pchip->pdata;
85 
86 	usleep_range(1000, 2000);
87 	/* set Filter Strength Register */
88 	rval = lm3630a_write(pchip, REG_FILTER_STRENGTH, 0x03);
89 	/* set Cofig. register */
90 	rval |= lm3630a_update(pchip, REG_CONFIG, 0x07, pdata->pwm_ctrl);
91 	/* set boost control */
92 	rval |= lm3630a_write(pchip, REG_BOOST, 0x38);
93 	/* set current A */
94 	rval |= lm3630a_update(pchip, REG_I_A, 0x1F, 0x1F);
95 	/* set current B */
96 	rval |= lm3630a_write(pchip, REG_I_B, 0x1F);
97 	/* set control */
98 	rval |= lm3630a_update(pchip, REG_CTRL, 0x14, pdata->leda_ctrl);
99 	rval |= lm3630a_update(pchip, REG_CTRL, 0x0B, pdata->ledb_ctrl);
100 	usleep_range(1000, 2000);
101 	/* set brightness A and B */
102 	rval |= lm3630a_write(pchip, REG_BRT_A, pdata->leda_init_brt);
103 	rval |= lm3630a_write(pchip, REG_BRT_B, pdata->ledb_init_brt);
104 
105 	if (rval < 0)
106 		dev_err(pchip->dev, "i2c failed to access register\n");
107 	return rval;
108 }
109 
110 /* interrupt handling */
111 static void lm3630a_delayed_func(struct work_struct *work)
112 {
113 	int rval;
114 	struct lm3630a_chip *pchip;
115 
116 	pchip = container_of(work, struct lm3630a_chip, work.work);
117 
118 	rval = lm3630a_read(pchip, REG_INT_STATUS);
119 	if (rval < 0) {
120 		dev_err(pchip->dev,
121 			"i2c failed to access REG_INT_STATUS Register\n");
122 		return;
123 	}
124 
125 	dev_info(pchip->dev, "REG_INT_STATUS Register is 0x%x\n", rval);
126 }
127 
128 static irqreturn_t lm3630a_isr_func(int irq, void *chip)
129 {
130 	int rval;
131 	struct lm3630a_chip *pchip = chip;
132 	unsigned long delay = msecs_to_jiffies(INT_DEBOUNCE_MSEC);
133 
134 	queue_delayed_work(pchip->irqthread, &pchip->work, delay);
135 
136 	rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
137 	if (rval < 0) {
138 		dev_err(pchip->dev, "i2c failed to access register\n");
139 		return IRQ_NONE;
140 	}
141 	return IRQ_HANDLED;
142 }
143 
144 static int lm3630a_intr_config(struct lm3630a_chip *pchip)
145 {
146 	int rval;
147 
148 	rval = lm3630a_write(pchip, REG_INT_EN, 0x87);
149 	if (rval < 0)
150 		return rval;
151 
152 	INIT_DELAYED_WORK(&pchip->work, lm3630a_delayed_func);
153 	pchip->irqthread = create_singlethread_workqueue("lm3630a-irqthd");
154 	if (!pchip->irqthread) {
155 		dev_err(pchip->dev, "create irq thread fail\n");
156 		return -ENOMEM;
157 	}
158 	if (request_threaded_irq
159 	    (pchip->irq, NULL, lm3630a_isr_func,
160 	     IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lm3630a_irq", pchip)) {
161 		dev_err(pchip->dev, "request threaded irq fail\n");
162 		destroy_workqueue(pchip->irqthread);
163 		return -ENOMEM;
164 	}
165 	return rval;
166 }
167 
168 static void lm3630a_pwm_ctrl(struct lm3630a_chip *pchip, int br, int br_max)
169 {
170 	unsigned int period = pchip->pdata->pwm_period;
171 	unsigned int duty = br * period / br_max;
172 
173 	pwm_config(pchip->pwmd, duty, period);
174 	if (duty)
175 		pwm_enable(pchip->pwmd);
176 	else
177 		pwm_disable(pchip->pwmd);
178 }
179 
180 /* update and get brightness */
181 static int lm3630a_bank_a_update_status(struct backlight_device *bl)
182 {
183 	int ret;
184 	struct lm3630a_chip *pchip = bl_get_data(bl);
185 	enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
186 
187 	/* pwm control */
188 	if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
189 		lm3630a_pwm_ctrl(pchip, bl->props.brightness,
190 				 bl->props.max_brightness);
191 		return bl->props.brightness;
192 	}
193 
194 	/* disable sleep */
195 	ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
196 	if (ret < 0)
197 		goto out_i2c_err;
198 	usleep_range(1000, 2000);
199 	/* minimum brightness is 0x04 */
200 	ret = lm3630a_write(pchip, REG_BRT_A, bl->props.brightness);
201 	if (bl->props.brightness < 0x4)
202 		ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDA_ENABLE, 0);
203 	else
204 		ret |= lm3630a_update(pchip, REG_CTRL,
205 				      LM3630A_LEDA_ENABLE, LM3630A_LEDA_ENABLE);
206 	if (ret < 0)
207 		goto out_i2c_err;
208 	return 0;
209 
210 out_i2c_err:
211 	dev_err(pchip->dev, "i2c failed to access\n");
212 	return bl->props.brightness;
213 }
214 
215 static int lm3630a_bank_a_get_brightness(struct backlight_device *bl)
216 {
217 	int brightness, rval;
218 	struct lm3630a_chip *pchip = bl_get_data(bl);
219 	enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
220 
221 	if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
222 		rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
223 		if (rval < 0)
224 			goto out_i2c_err;
225 		brightness = (rval & 0x01) << 8;
226 		rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
227 		if (rval < 0)
228 			goto out_i2c_err;
229 		brightness |= rval;
230 		goto out;
231 	}
232 
233 	/* disable sleep */
234 	rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
235 	if (rval < 0)
236 		goto out_i2c_err;
237 	usleep_range(1000, 2000);
238 	rval = lm3630a_read(pchip, REG_BRT_A);
239 	if (rval < 0)
240 		goto out_i2c_err;
241 	brightness = rval;
242 
243 out:
244 	bl->props.brightness = brightness;
245 	return bl->props.brightness;
246 out_i2c_err:
247 	dev_err(pchip->dev, "i2c failed to access register\n");
248 	return 0;
249 }
250 
251 static const struct backlight_ops lm3630a_bank_a_ops = {
252 	.options = BL_CORE_SUSPENDRESUME,
253 	.update_status = lm3630a_bank_a_update_status,
254 	.get_brightness = lm3630a_bank_a_get_brightness,
255 };
256 
257 /* update and get brightness */
258 static int lm3630a_bank_b_update_status(struct backlight_device *bl)
259 {
260 	int ret;
261 	struct lm3630a_chip *pchip = bl_get_data(bl);
262 	enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
263 
264 	/* pwm control */
265 	if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
266 		lm3630a_pwm_ctrl(pchip, bl->props.brightness,
267 				 bl->props.max_brightness);
268 		return bl->props.brightness;
269 	}
270 
271 	/* disable sleep */
272 	ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
273 	if (ret < 0)
274 		goto out_i2c_err;
275 	usleep_range(1000, 2000);
276 	/* minimum brightness is 0x04 */
277 	ret = lm3630a_write(pchip, REG_BRT_B, bl->props.brightness);
278 	if (bl->props.brightness < 0x4)
279 		ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDB_ENABLE, 0);
280 	else
281 		ret |= lm3630a_update(pchip, REG_CTRL,
282 				      LM3630A_LEDB_ENABLE, LM3630A_LEDB_ENABLE);
283 	if (ret < 0)
284 		goto out_i2c_err;
285 	return 0;
286 
287 out_i2c_err:
288 	dev_err(pchip->dev, "i2c failed to access REG_CTRL\n");
289 	return bl->props.brightness;
290 }
291 
292 static int lm3630a_bank_b_get_brightness(struct backlight_device *bl)
293 {
294 	int brightness, rval;
295 	struct lm3630a_chip *pchip = bl_get_data(bl);
296 	enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
297 
298 	if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
299 		rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
300 		if (rval < 0)
301 			goto out_i2c_err;
302 		brightness = (rval & 0x01) << 8;
303 		rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
304 		if (rval < 0)
305 			goto out_i2c_err;
306 		brightness |= rval;
307 		goto out;
308 	}
309 
310 	/* disable sleep */
311 	rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
312 	if (rval < 0)
313 		goto out_i2c_err;
314 	usleep_range(1000, 2000);
315 	rval = lm3630a_read(pchip, REG_BRT_B);
316 	if (rval < 0)
317 		goto out_i2c_err;
318 	brightness = rval;
319 
320 out:
321 	bl->props.brightness = brightness;
322 	return bl->props.brightness;
323 out_i2c_err:
324 	dev_err(pchip->dev, "i2c failed to access register\n");
325 	return 0;
326 }
327 
328 static const struct backlight_ops lm3630a_bank_b_ops = {
329 	.options = BL_CORE_SUSPENDRESUME,
330 	.update_status = lm3630a_bank_b_update_status,
331 	.get_brightness = lm3630a_bank_b_get_brightness,
332 };
333 
334 static int lm3630a_backlight_register(struct lm3630a_chip *pchip)
335 {
336 	struct lm3630a_platform_data *pdata = pchip->pdata;
337 	struct backlight_properties props;
338 	const char *label;
339 
340 	props.type = BACKLIGHT_RAW;
341 	if (pdata->leda_ctrl != LM3630A_LEDA_DISABLE) {
342 		props.brightness = pdata->leda_init_brt;
343 		props.max_brightness = pdata->leda_max_brt;
344 		label = pdata->leda_label ? pdata->leda_label : "lm3630a_leda";
345 		pchip->bleda =
346 		    devm_backlight_device_register(pchip->dev, label,
347 						   pchip->dev, pchip,
348 						   &lm3630a_bank_a_ops, &props);
349 		if (IS_ERR(pchip->bleda))
350 			return PTR_ERR(pchip->bleda);
351 	}
352 
353 	if ((pdata->ledb_ctrl != LM3630A_LEDB_DISABLE) &&
354 	    (pdata->ledb_ctrl != LM3630A_LEDB_ON_A)) {
355 		props.brightness = pdata->ledb_init_brt;
356 		props.max_brightness = pdata->ledb_max_brt;
357 		label = pdata->ledb_label ? pdata->ledb_label : "lm3630a_ledb";
358 		pchip->bledb =
359 		    devm_backlight_device_register(pchip->dev, label,
360 						   pchip->dev, pchip,
361 						   &lm3630a_bank_b_ops, &props);
362 		if (IS_ERR(pchip->bledb))
363 			return PTR_ERR(pchip->bledb);
364 	}
365 	return 0;
366 }
367 
368 static const struct regmap_config lm3630a_regmap = {
369 	.reg_bits = 8,
370 	.val_bits = 8,
371 	.max_register = REG_MAX,
372 };
373 
374 static int lm3630a_parse_led_sources(struct fwnode_handle *node,
375 				     int default_led_sources)
376 {
377 	u32 sources[LM3630A_NUM_SINKS];
378 	int ret, num_sources, i;
379 
380 	num_sources = fwnode_property_read_u32_array(node, "led-sources", NULL,
381 						     0);
382 	if (num_sources < 0)
383 		return default_led_sources;
384 	else if (num_sources > ARRAY_SIZE(sources))
385 		return -EINVAL;
386 
387 	ret = fwnode_property_read_u32_array(node, "led-sources", sources,
388 					     num_sources);
389 	if (ret)
390 		return ret;
391 
392 	for (i = 0; i < num_sources; i++) {
393 		if (sources[i] < LM3630A_SINK_0 || sources[i] > LM3630A_SINK_1)
394 			return -EINVAL;
395 
396 		ret |= BIT(sources[i]);
397 	}
398 
399 	return ret;
400 }
401 
402 static int lm3630a_parse_bank(struct lm3630a_platform_data *pdata,
403 			      struct fwnode_handle *node, int *seen_led_sources)
404 {
405 	int led_sources, ret;
406 	const char *label;
407 	u32 bank, val;
408 	bool linear;
409 
410 	ret = fwnode_property_read_u32(node, "reg", &bank);
411 	if (ret)
412 		return ret;
413 
414 	if (bank < LM3630A_BANK_0 || bank > LM3630A_BANK_1)
415 		return -EINVAL;
416 
417 	led_sources = lm3630a_parse_led_sources(node, BIT(bank));
418 	if (led_sources < 0)
419 		return led_sources;
420 
421 	if (*seen_led_sources & led_sources)
422 		return -EINVAL;
423 
424 	*seen_led_sources |= led_sources;
425 
426 	linear = fwnode_property_read_bool(node,
427 					   "ti,linear-mapping-mode");
428 	if (bank) {
429 		if (led_sources & BIT(LM3630A_SINK_0) ||
430 		    !(led_sources & BIT(LM3630A_SINK_1)))
431 			return -EINVAL;
432 
433 		pdata->ledb_ctrl = linear ?
434 			LM3630A_LEDB_ENABLE_LINEAR :
435 			LM3630A_LEDB_ENABLE;
436 	} else {
437 		if (!(led_sources & BIT(LM3630A_SINK_0)))
438 			return -EINVAL;
439 
440 		pdata->leda_ctrl = linear ?
441 			LM3630A_LEDA_ENABLE_LINEAR :
442 			LM3630A_LEDA_ENABLE;
443 
444 		if (led_sources & BIT(LM3630A_SINK_1))
445 			pdata->ledb_ctrl = LM3630A_LEDB_ON_A;
446 	}
447 
448 	ret = fwnode_property_read_string(node, "label", &label);
449 	if (!ret) {
450 		if (bank)
451 			pdata->ledb_label = label;
452 		else
453 			pdata->leda_label = label;
454 	}
455 
456 	ret = fwnode_property_read_u32(node, "default-brightness",
457 				       &val);
458 	if (!ret) {
459 		if (bank)
460 			pdata->ledb_init_brt = val;
461 		else
462 			pdata->leda_init_brt = val;
463 	}
464 
465 	ret = fwnode_property_read_u32(node, "max-brightness", &val);
466 	if (!ret) {
467 		if (bank)
468 			pdata->ledb_max_brt = val;
469 		else
470 			pdata->leda_max_brt = val;
471 	}
472 
473 	return 0;
474 }
475 
476 static int lm3630a_parse_node(struct lm3630a_chip *pchip,
477 			      struct lm3630a_platform_data *pdata)
478 {
479 	int ret = -ENODEV, seen_led_sources = 0;
480 	struct fwnode_handle *node;
481 
482 	device_for_each_child_node(pchip->dev, node) {
483 		ret = lm3630a_parse_bank(pdata, node, &seen_led_sources);
484 		if (ret)
485 			return ret;
486 	}
487 
488 	return ret;
489 }
490 
491 static int lm3630a_probe(struct i2c_client *client,
492 			 const struct i2c_device_id *id)
493 {
494 	struct lm3630a_platform_data *pdata = dev_get_platdata(&client->dev);
495 	struct lm3630a_chip *pchip;
496 	int rval;
497 
498 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
499 		dev_err(&client->dev, "fail : i2c functionality check\n");
500 		return -EOPNOTSUPP;
501 	}
502 
503 	pchip = devm_kzalloc(&client->dev, sizeof(struct lm3630a_chip),
504 			     GFP_KERNEL);
505 	if (!pchip)
506 		return -ENOMEM;
507 	pchip->dev = &client->dev;
508 
509 	pchip->regmap = devm_regmap_init_i2c(client, &lm3630a_regmap);
510 	if (IS_ERR(pchip->regmap)) {
511 		rval = PTR_ERR(pchip->regmap);
512 		dev_err(&client->dev, "fail : allocate reg. map: %d\n", rval);
513 		return rval;
514 	}
515 
516 	i2c_set_clientdata(client, pchip);
517 	if (pdata == NULL) {
518 		pdata = devm_kzalloc(pchip->dev,
519 				     sizeof(struct lm3630a_platform_data),
520 				     GFP_KERNEL);
521 		if (pdata == NULL)
522 			return -ENOMEM;
523 
524 		/* default values */
525 		pdata->leda_max_brt = LM3630A_MAX_BRIGHTNESS;
526 		pdata->ledb_max_brt = LM3630A_MAX_BRIGHTNESS;
527 		pdata->leda_init_brt = LM3630A_MAX_BRIGHTNESS;
528 		pdata->ledb_init_brt = LM3630A_MAX_BRIGHTNESS;
529 
530 		rval = lm3630a_parse_node(pchip, pdata);
531 		if (rval) {
532 			dev_err(&client->dev, "fail : parse node\n");
533 			return rval;
534 		}
535 	}
536 	pchip->pdata = pdata;
537 
538 	/* chip initialize */
539 	rval = lm3630a_chip_init(pchip);
540 	if (rval < 0) {
541 		dev_err(&client->dev, "fail : init chip\n");
542 		return rval;
543 	}
544 	/* backlight register */
545 	rval = lm3630a_backlight_register(pchip);
546 	if (rval < 0) {
547 		dev_err(&client->dev, "fail : backlight register.\n");
548 		return rval;
549 	}
550 	/* pwm */
551 	if (pdata->pwm_ctrl != LM3630A_PWM_DISABLE) {
552 		pchip->pwmd = devm_pwm_get(pchip->dev, "lm3630a-pwm");
553 		if (IS_ERR(pchip->pwmd)) {
554 			dev_err(&client->dev, "fail : get pwm device\n");
555 			return PTR_ERR(pchip->pwmd);
556 		}
557 
558 		/*
559 		 * FIXME: pwm_apply_args() should be removed when switching to
560 		 * the atomic PWM API.
561 		 */
562 		pwm_apply_args(pchip->pwmd);
563 	}
564 
565 	/* interrupt enable  : irq 0 is not allowed */
566 	pchip->irq = client->irq;
567 	if (pchip->irq) {
568 		rval = lm3630a_intr_config(pchip);
569 		if (rval < 0)
570 			return rval;
571 	}
572 	dev_info(&client->dev, "LM3630A backlight register OK.\n");
573 	return 0;
574 }
575 
576 static int lm3630a_remove(struct i2c_client *client)
577 {
578 	int rval;
579 	struct lm3630a_chip *pchip = i2c_get_clientdata(client);
580 
581 	rval = lm3630a_write(pchip, REG_BRT_A, 0);
582 	if (rval < 0)
583 		dev_err(pchip->dev, "i2c failed to access register\n");
584 
585 	rval = lm3630a_write(pchip, REG_BRT_B, 0);
586 	if (rval < 0)
587 		dev_err(pchip->dev, "i2c failed to access register\n");
588 
589 	if (pchip->irq) {
590 		free_irq(pchip->irq, pchip);
591 		flush_workqueue(pchip->irqthread);
592 		destroy_workqueue(pchip->irqthread);
593 	}
594 	return 0;
595 }
596 
597 static const struct i2c_device_id lm3630a_id[] = {
598 	{LM3630A_NAME, 0},
599 	{}
600 };
601 
602 static const struct of_device_id lm3630a_match_table[] = {
603 	{ .compatible = "ti,lm3630a", },
604 	{ },
605 };
606 
607 MODULE_DEVICE_TABLE(i2c, lm3630a_id);
608 
609 static struct i2c_driver lm3630a_i2c_driver = {
610 	.driver = {
611 		   .name = LM3630A_NAME,
612 		   .of_match_table = lm3630a_match_table,
613 		   },
614 	.probe = lm3630a_probe,
615 	.remove = lm3630a_remove,
616 	.id_table = lm3630a_id,
617 };
618 
619 module_i2c_driver(lm3630a_i2c_driver);
620 
621 MODULE_DESCRIPTION("Texas Instruments Backlight driver for LM3630A");
622 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
623 MODULE_AUTHOR("LDD MLP <ldd-mlp@list.ti.com>");
624 MODULE_LICENSE("GPL v2");
625