1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simple driver for Texas Instruments LM3630A Backlight driver chip
4 * Copyright (C) 2012 Texas Instruments
6 #include <linux/module.h>
7 #include <linux/slab.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>
19 #define REG_BOOST 0x02
20 #define REG_CONFIG 0x01
21 #define REG_BRT_A 0x03
22 #define REG_BRT_B 0x04
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
33 #define INT_DEBOUNCE_MSEC 10
35 #define LM3630A_BANK_0 0
36 #define LM3630A_BANK_1 1
38 #define LM3630A_NUM_SINKS 2
39 #define LM3630A_SINK_0 0
40 #define LM3630A_SINK_1 1
44 struct delayed_work work
;
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
;
56 static int lm3630a_read(struct lm3630a_chip
*pchip
, unsigned int reg
)
61 rval
= regmap_read(pchip
->regmap
, reg
, ®_val
);
64 return reg_val
& 0xFF;
67 static int lm3630a_write(struct lm3630a_chip
*pchip
,
68 unsigned int reg
, unsigned int data
)
70 return regmap_write(pchip
->regmap
, reg
, data
);
73 static int lm3630a_update(struct lm3630a_chip
*pchip
,
74 unsigned int reg
, unsigned int mask
,
77 return regmap_update_bits(pchip
->regmap
, reg
, mask
, data
);
81 static int lm3630a_chip_init(struct lm3630a_chip
*pchip
)
84 struct lm3630a_platform_data
*pdata
= pchip
->pdata
;
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);
94 rval
|= lm3630a_update(pchip
, REG_I_A
, 0x1F, 0x1F);
96 rval
|= lm3630a_write(pchip
, REG_I_B
, 0x1F);
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
);
106 dev_err(pchip
->dev
, "i2c failed to access register\n");
110 /* interrupt handling */
111 static void lm3630a_delayed_func(struct work_struct
*work
)
114 struct lm3630a_chip
*pchip
;
116 pchip
= container_of(work
, struct lm3630a_chip
, work
.work
);
118 rval
= lm3630a_read(pchip
, REG_INT_STATUS
);
121 "i2c failed to access REG_INT_STATUS Register\n");
125 dev_info(pchip
->dev
, "REG_INT_STATUS Register is 0x%x\n", rval
);
128 static irqreturn_t
lm3630a_isr_func(int irq
, void *chip
)
131 struct lm3630a_chip
*pchip
= chip
;
132 unsigned long delay
= msecs_to_jiffies(INT_DEBOUNCE_MSEC
);
134 queue_delayed_work(pchip
->irqthread
, &pchip
->work
, delay
);
136 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
138 dev_err(pchip
->dev
, "i2c failed to access register\n");
144 static int lm3630a_intr_config(struct lm3630a_chip
*pchip
)
148 rval
= lm3630a_write(pchip
, REG_INT_EN
, 0x87);
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");
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
);
168 static void lm3630a_pwm_ctrl(struct lm3630a_chip
*pchip
, int br
, int br_max
)
170 unsigned int period
= pchip
->pdata
->pwm_period
;
171 unsigned int duty
= br
* period
/ br_max
;
173 pwm_config(pchip
->pwmd
, duty
, period
);
175 pwm_enable(pchip
->pwmd
);
177 pwm_disable(pchip
->pwmd
);
180 /* update and get brightness */
181 static int lm3630a_bank_a_update_status(struct backlight_device
*bl
)
184 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
185 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
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
;
195 ret
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
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);
204 ret
|= lm3630a_update(pchip
, REG_CTRL
,
205 LM3630A_LEDA_ENABLE
, LM3630A_LEDA_ENABLE
);
211 dev_err(pchip
->dev
, "i2c failed to access\n");
212 return bl
->props
.brightness
;
215 static int lm3630a_bank_a_get_brightness(struct backlight_device
*bl
)
217 int brightness
, rval
;
218 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
219 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
221 if ((pwm_ctrl
& LM3630A_PWM_BANK_A
) != 0) {
222 rval
= lm3630a_read(pchip
, REG_PWM_OUTHIGH
);
225 brightness
= (rval
& 0x01) << 8;
226 rval
= lm3630a_read(pchip
, REG_PWM_OUTLOW
);
234 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
237 usleep_range(1000, 2000);
238 rval
= lm3630a_read(pchip
, REG_BRT_A
);
244 bl
->props
.brightness
= brightness
;
245 return bl
->props
.brightness
;
247 dev_err(pchip
->dev
, "i2c failed to access register\n");
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
,
257 /* update and get brightness */
258 static int lm3630a_bank_b_update_status(struct backlight_device
*bl
)
261 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
262 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
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
;
272 ret
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
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);
281 ret
|= lm3630a_update(pchip
, REG_CTRL
,
282 LM3630A_LEDB_ENABLE
, LM3630A_LEDB_ENABLE
);
288 dev_err(pchip
->dev
, "i2c failed to access REG_CTRL\n");
289 return bl
->props
.brightness
;
292 static int lm3630a_bank_b_get_brightness(struct backlight_device
*bl
)
294 int brightness
, rval
;
295 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
296 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
298 if ((pwm_ctrl
& LM3630A_PWM_BANK_B
) != 0) {
299 rval
= lm3630a_read(pchip
, REG_PWM_OUTHIGH
);
302 brightness
= (rval
& 0x01) << 8;
303 rval
= lm3630a_read(pchip
, REG_PWM_OUTLOW
);
311 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
314 usleep_range(1000, 2000);
315 rval
= lm3630a_read(pchip
, REG_BRT_B
);
321 bl
->props
.brightness
= brightness
;
322 return bl
->props
.brightness
;
324 dev_err(pchip
->dev
, "i2c failed to access register\n");
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
,
334 static int lm3630a_backlight_register(struct lm3630a_chip
*pchip
)
336 struct lm3630a_platform_data
*pdata
= pchip
->pdata
;
337 struct backlight_properties props
;
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";
346 devm_backlight_device_register(pchip
->dev
, label
,
348 &lm3630a_bank_a_ops
, &props
);
349 if (IS_ERR(pchip
->bleda
))
350 return PTR_ERR(pchip
->bleda
);
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";
359 devm_backlight_device_register(pchip
->dev
, label
,
361 &lm3630a_bank_b_ops
, &props
);
362 if (IS_ERR(pchip
->bledb
))
363 return PTR_ERR(pchip
->bledb
);
368 static const struct regmap_config lm3630a_regmap
= {
371 .max_register
= REG_MAX
,
374 static int lm3630a_parse_led_sources(struct fwnode_handle
*node
,
375 int default_led_sources
)
377 u32 sources
[LM3630A_NUM_SINKS
];
378 int ret
, num_sources
, i
;
380 num_sources
= fwnode_property_read_u32_array(node
, "led-sources", NULL
,
383 return default_led_sources
;
384 else if (num_sources
> ARRAY_SIZE(sources
))
387 ret
= fwnode_property_read_u32_array(node
, "led-sources", sources
,
392 for (i
= 0; i
< num_sources
; i
++) {
393 if (sources
[i
] < LM3630A_SINK_0
|| sources
[i
] > LM3630A_SINK_1
)
396 ret
|= BIT(sources
[i
]);
402 static int lm3630a_parse_bank(struct lm3630a_platform_data
*pdata
,
403 struct fwnode_handle
*node
, int *seen_led_sources
)
405 int led_sources
, ret
;
410 ret
= fwnode_property_read_u32(node
, "reg", &bank
);
414 if (bank
< LM3630A_BANK_0
|| bank
> LM3630A_BANK_1
)
417 led_sources
= lm3630a_parse_led_sources(node
, BIT(bank
));
421 if (*seen_led_sources
& led_sources
)
424 *seen_led_sources
|= led_sources
;
426 linear
= fwnode_property_read_bool(node
,
427 "ti,linear-mapping-mode");
429 if (led_sources
& BIT(LM3630A_SINK_0
) ||
430 !(led_sources
& BIT(LM3630A_SINK_1
)))
433 pdata
->ledb_ctrl
= linear
?
434 LM3630A_LEDB_ENABLE_LINEAR
:
437 if (!(led_sources
& BIT(LM3630A_SINK_0
)))
440 pdata
->leda_ctrl
= linear
?
441 LM3630A_LEDA_ENABLE_LINEAR
:
444 if (led_sources
& BIT(LM3630A_SINK_1
))
445 pdata
->ledb_ctrl
= LM3630A_LEDB_ON_A
;
448 ret
= fwnode_property_read_string(node
, "label", &label
);
451 pdata
->ledb_label
= label
;
453 pdata
->leda_label
= label
;
456 ret
= fwnode_property_read_u32(node
, "default-brightness",
460 pdata
->ledb_init_brt
= val
;
462 pdata
->leda_init_brt
= val
;
465 ret
= fwnode_property_read_u32(node
, "max-brightness", &val
);
468 pdata
->ledb_max_brt
= val
;
470 pdata
->leda_max_brt
= val
;
476 static int lm3630a_parse_node(struct lm3630a_chip
*pchip
,
477 struct lm3630a_platform_data
*pdata
)
479 int ret
= -ENODEV
, seen_led_sources
= 0;
480 struct fwnode_handle
*node
;
482 device_for_each_child_node(pchip
->dev
, node
) {
483 ret
= lm3630a_parse_bank(pdata
, node
, &seen_led_sources
);
491 static int lm3630a_probe(struct i2c_client
*client
,
492 const struct i2c_device_id
*id
)
494 struct lm3630a_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
495 struct lm3630a_chip
*pchip
;
498 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
499 dev_err(&client
->dev
, "fail : i2c functionality check\n");
503 pchip
= devm_kzalloc(&client
->dev
, sizeof(struct lm3630a_chip
),
507 pchip
->dev
= &client
->dev
;
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
);
516 i2c_set_clientdata(client
, pchip
);
518 pdata
= devm_kzalloc(pchip
->dev
,
519 sizeof(struct lm3630a_platform_data
),
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
;
530 rval
= lm3630a_parse_node(pchip
, pdata
);
532 dev_err(&client
->dev
, "fail : parse node\n");
536 pchip
->pdata
= pdata
;
538 /* chip initialize */
539 rval
= lm3630a_chip_init(pchip
);
541 dev_err(&client
->dev
, "fail : init chip\n");
544 /* backlight register */
545 rval
= lm3630a_backlight_register(pchip
);
547 dev_err(&client
->dev
, "fail : backlight register.\n");
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
);
559 * FIXME: pwm_apply_args() should be removed when switching to
560 * the atomic PWM API.
562 pwm_apply_args(pchip
->pwmd
);
565 /* interrupt enable : irq 0 is not allowed */
566 pchip
->irq
= client
->irq
;
568 rval
= lm3630a_intr_config(pchip
);
572 dev_info(&client
->dev
, "LM3630A backlight register OK.\n");
576 static int lm3630a_remove(struct i2c_client
*client
)
579 struct lm3630a_chip
*pchip
= i2c_get_clientdata(client
);
581 rval
= lm3630a_write(pchip
, REG_BRT_A
, 0);
583 dev_err(pchip
->dev
, "i2c failed to access register\n");
585 rval
= lm3630a_write(pchip
, REG_BRT_B
, 0);
587 dev_err(pchip
->dev
, "i2c failed to access register\n");
590 free_irq(pchip
->irq
, pchip
);
591 flush_workqueue(pchip
->irqthread
);
592 destroy_workqueue(pchip
->irqthread
);
597 static const struct i2c_device_id lm3630a_id
[] = {
602 static const struct of_device_id lm3630a_match_table
[] = {
603 { .compatible
= "ti,lm3630a", },
607 MODULE_DEVICE_TABLE(i2c
, lm3630a_id
);
609 static struct i2c_driver lm3630a_i2c_driver
= {
611 .name
= LM3630A_NAME
,
612 .of_match_table
= lm3630a_match_table
,
614 .probe
= lm3630a_probe
,
615 .remove
= lm3630a_remove
,
616 .id_table
= lm3630a_id
,
619 module_i2c_driver(lm3630a_i2c_driver
);
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");