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/gpio/consumer.h>
16 #include <linux/pwm.h>
17 #include <linux/platform_data/lm3630a_bl.h>
20 #define REG_BOOST 0x02
21 #define REG_CONFIG 0x01
22 #define REG_BRT_A 0x03
23 #define REG_BRT_B 0x04
26 #define REG_INT_STATUS 0x09
27 #define REG_INT_EN 0x0A
28 #define REG_FAULT 0x0B
29 #define REG_PWM_OUTLOW 0x12
30 #define REG_PWM_OUTHIGH 0x13
31 #define REG_FILTER_STRENGTH 0x50
34 #define INT_DEBOUNCE_MSEC 10
36 #define LM3630A_BANK_0 0
37 #define LM3630A_BANK_1 1
39 #define LM3630A_NUM_SINKS 2
40 #define LM3630A_SINK_0 0
41 #define LM3630A_SINK_1 1
45 struct delayed_work work
;
48 struct workqueue_struct
*irqthread
;
49 struct lm3630a_platform_data
*pdata
;
50 struct backlight_device
*bleda
;
51 struct backlight_device
*bledb
;
52 struct gpio_desc
*enable_gpio
;
53 struct regmap
*regmap
;
54 struct pwm_device
*pwmd
;
55 struct pwm_state pwmd_state
;
59 static int lm3630a_read(struct lm3630a_chip
*pchip
, unsigned int reg
)
64 rval
= regmap_read(pchip
->regmap
, reg
, ®_val
);
67 return reg_val
& 0xFF;
70 static int lm3630a_write(struct lm3630a_chip
*pchip
,
71 unsigned int reg
, unsigned int data
)
73 return regmap_write(pchip
->regmap
, reg
, data
);
76 static int lm3630a_update(struct lm3630a_chip
*pchip
,
77 unsigned int reg
, unsigned int mask
,
80 return regmap_update_bits(pchip
->regmap
, reg
, mask
, data
);
84 static int lm3630a_chip_init(struct lm3630a_chip
*pchip
)
87 struct lm3630a_platform_data
*pdata
= pchip
->pdata
;
89 usleep_range(1000, 2000);
90 /* set Filter Strength Register */
91 rval
= lm3630a_write(pchip
, REG_FILTER_STRENGTH
, 0x03);
92 /* set Cofig. register */
93 rval
|= lm3630a_update(pchip
, REG_CONFIG
, 0x07, pdata
->pwm_ctrl
);
94 /* set boost control */
95 rval
|= lm3630a_write(pchip
, REG_BOOST
, 0x38);
97 rval
|= lm3630a_update(pchip
, REG_I_A
, 0x1F, 0x1F);
99 rval
|= lm3630a_write(pchip
, REG_I_B
, 0x1F);
101 rval
|= lm3630a_update(pchip
, REG_CTRL
, 0x14, pdata
->leda_ctrl
);
102 rval
|= lm3630a_update(pchip
, REG_CTRL
, 0x0B, pdata
->ledb_ctrl
);
103 usleep_range(1000, 2000);
104 /* set brightness A and B */
105 rval
|= lm3630a_write(pchip
, REG_BRT_A
, pdata
->leda_init_brt
);
106 rval
|= lm3630a_write(pchip
, REG_BRT_B
, pdata
->ledb_init_brt
);
109 dev_err(pchip
->dev
, "i2c failed to access register\n");
113 /* interrupt handling */
114 static void lm3630a_delayed_func(struct work_struct
*work
)
117 struct lm3630a_chip
*pchip
;
119 pchip
= container_of(work
, struct lm3630a_chip
, work
.work
);
121 rval
= lm3630a_read(pchip
, REG_INT_STATUS
);
124 "i2c failed to access REG_INT_STATUS Register\n");
128 dev_info(pchip
->dev
, "REG_INT_STATUS Register is 0x%x\n", rval
);
131 static irqreturn_t
lm3630a_isr_func(int irq
, void *chip
)
134 struct lm3630a_chip
*pchip
= chip
;
135 unsigned long delay
= msecs_to_jiffies(INT_DEBOUNCE_MSEC
);
137 queue_delayed_work(pchip
->irqthread
, &pchip
->work
, delay
);
139 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
141 dev_err(pchip
->dev
, "i2c failed to access register\n");
147 static int lm3630a_intr_config(struct lm3630a_chip
*pchip
)
151 rval
= lm3630a_write(pchip
, REG_INT_EN
, 0x87);
155 INIT_DELAYED_WORK(&pchip
->work
, lm3630a_delayed_func
);
156 pchip
->irqthread
= create_singlethread_workqueue("lm3630a-irqthd");
157 if (!pchip
->irqthread
) {
158 dev_err(pchip
->dev
, "create irq thread fail\n");
161 if (request_threaded_irq
162 (pchip
->irq
, NULL
, lm3630a_isr_func
,
163 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
, "lm3630a_irq", pchip
)) {
164 dev_err(pchip
->dev
, "request threaded irq fail\n");
165 destroy_workqueue(pchip
->irqthread
);
171 static int lm3630a_pwm_ctrl(struct lm3630a_chip
*pchip
, int br
, int br_max
)
175 pchip
->pwmd_state
.period
= pchip
->pdata
->pwm_period
;
177 err
= pwm_set_relative_duty_cycle(&pchip
->pwmd_state
, br
, br_max
);
181 pchip
->pwmd_state
.enabled
= pchip
->pwmd_state
.duty_cycle
? true : false;
183 return pwm_apply_might_sleep(pchip
->pwmd
, &pchip
->pwmd_state
);
186 /* update and get brightness */
187 static int lm3630a_bank_a_update_status(struct backlight_device
*bl
)
190 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
191 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
192 int brightness
= backlight_get_brightness(bl
);
195 if ((pwm_ctrl
& LM3630A_PWM_BANK_A
) != 0)
196 return lm3630a_pwm_ctrl(pchip
, brightness
,
197 bl
->props
.max_brightness
);
200 ret
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
203 usleep_range(1000, 2000);
204 /* minimum brightness is 0x04 */
205 ret
= lm3630a_write(pchip
, REG_BRT_A
, brightness
);
207 if (brightness
< 0x4)
208 /* turn the string off */
209 ret
|= lm3630a_update(pchip
, REG_CTRL
, LM3630A_LEDA_ENABLE
, 0);
211 ret
|= lm3630a_update(pchip
, REG_CTRL
,
212 LM3630A_LEDA_ENABLE
, LM3630A_LEDA_ENABLE
);
218 dev_err(pchip
->dev
, "i2c failed to access (%pe)\n", ERR_PTR(ret
));
222 static int lm3630a_bank_a_get_brightness(struct backlight_device
*bl
)
224 int brightness
, rval
;
225 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
226 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
228 if ((pwm_ctrl
& LM3630A_PWM_BANK_A
) != 0) {
229 rval
= lm3630a_read(pchip
, REG_PWM_OUTHIGH
);
232 brightness
= (rval
& 0x01) << 8;
233 rval
= lm3630a_read(pchip
, REG_PWM_OUTLOW
);
241 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
244 usleep_range(1000, 2000);
245 rval
= lm3630a_read(pchip
, REG_BRT_A
);
251 dev_err(pchip
->dev
, "i2c failed to access register\n");
255 static const struct backlight_ops lm3630a_bank_a_ops
= {
256 .options
= BL_CORE_SUSPENDRESUME
,
257 .update_status
= lm3630a_bank_a_update_status
,
258 .get_brightness
= lm3630a_bank_a_get_brightness
,
261 /* update and get brightness */
262 static int lm3630a_bank_b_update_status(struct backlight_device
*bl
)
265 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
266 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
267 int brightness
= backlight_get_brightness(bl
);
270 if ((pwm_ctrl
& LM3630A_PWM_BANK_B
) != 0)
271 return lm3630a_pwm_ctrl(pchip
, brightness
,
272 bl
->props
.max_brightness
);
275 ret
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
278 usleep_range(1000, 2000);
279 /* minimum brightness is 0x04 */
280 ret
= lm3630a_write(pchip
, REG_BRT_B
, brightness
);
282 if (brightness
< 0x4)
283 /* turn the string off */
284 ret
|= lm3630a_update(pchip
, REG_CTRL
, LM3630A_LEDB_ENABLE
, 0);
286 ret
|= lm3630a_update(pchip
, REG_CTRL
,
287 LM3630A_LEDB_ENABLE
, LM3630A_LEDB_ENABLE
);
293 dev_err(pchip
->dev
, "i2c failed to access (%pe)\n", ERR_PTR(ret
));
297 static int lm3630a_bank_b_get_brightness(struct backlight_device
*bl
)
299 int brightness
, rval
;
300 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
301 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
303 if ((pwm_ctrl
& LM3630A_PWM_BANK_B
) != 0) {
304 rval
= lm3630a_read(pchip
, REG_PWM_OUTHIGH
);
307 brightness
= (rval
& 0x01) << 8;
308 rval
= lm3630a_read(pchip
, REG_PWM_OUTLOW
);
316 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
319 usleep_range(1000, 2000);
320 rval
= lm3630a_read(pchip
, REG_BRT_B
);
326 dev_err(pchip
->dev
, "i2c failed to access register\n");
330 static const struct backlight_ops lm3630a_bank_b_ops
= {
331 .options
= BL_CORE_SUSPENDRESUME
,
332 .update_status
= lm3630a_bank_b_update_status
,
333 .get_brightness
= lm3630a_bank_b_get_brightness
,
336 static int lm3630a_backlight_register(struct lm3630a_chip
*pchip
)
338 struct lm3630a_platform_data
*pdata
= pchip
->pdata
;
339 struct backlight_properties props
;
342 memset(&props
, 0, sizeof(struct backlight_properties
));
343 props
.type
= BACKLIGHT_RAW
;
344 if (pdata
->leda_ctrl
!= LM3630A_LEDA_DISABLE
) {
345 props
.brightness
= pdata
->leda_init_brt
;
346 props
.max_brightness
= pdata
->leda_max_brt
;
347 label
= pdata
->leda_label
? pdata
->leda_label
: "lm3630a_leda";
349 devm_backlight_device_register(pchip
->dev
, label
,
351 &lm3630a_bank_a_ops
, &props
);
352 if (IS_ERR(pchip
->bleda
))
353 return PTR_ERR(pchip
->bleda
);
356 if ((pdata
->ledb_ctrl
!= LM3630A_LEDB_DISABLE
) &&
357 (pdata
->ledb_ctrl
!= LM3630A_LEDB_ON_A
)) {
358 props
.brightness
= pdata
->ledb_init_brt
;
359 props
.max_brightness
= pdata
->ledb_max_brt
;
360 label
= pdata
->ledb_label
? pdata
->ledb_label
: "lm3630a_ledb";
362 devm_backlight_device_register(pchip
->dev
, label
,
364 &lm3630a_bank_b_ops
, &props
);
365 if (IS_ERR(pchip
->bledb
))
366 return PTR_ERR(pchip
->bledb
);
371 static const struct regmap_config lm3630a_regmap
= {
374 .max_register
= REG_MAX
,
377 static int lm3630a_parse_led_sources(struct fwnode_handle
*node
,
378 int default_led_sources
)
380 u32 sources
[LM3630A_NUM_SINKS
];
381 int ret
, num_sources
, i
;
383 num_sources
= fwnode_property_count_u32(node
, "led-sources");
385 return default_led_sources
;
386 else if (num_sources
> ARRAY_SIZE(sources
))
389 ret
= fwnode_property_read_u32_array(node
, "led-sources", sources
,
394 for (i
= 0; i
< num_sources
; i
++) {
395 if (sources
[i
] != LM3630A_SINK_0
&& sources
[i
] != LM3630A_SINK_1
)
398 ret
|= BIT(sources
[i
]);
404 static int lm3630a_parse_bank(struct lm3630a_platform_data
*pdata
,
405 struct fwnode_handle
*node
, int *seen_led_sources
)
407 int led_sources
, ret
;
412 ret
= fwnode_property_read_u32(node
, "reg", &bank
);
416 if (bank
!= LM3630A_BANK_0
&& bank
!= LM3630A_BANK_1
)
419 led_sources
= lm3630a_parse_led_sources(node
, BIT(bank
));
423 if (*seen_led_sources
& led_sources
)
426 *seen_led_sources
|= led_sources
;
428 linear
= fwnode_property_read_bool(node
,
429 "ti,linear-mapping-mode");
431 if (led_sources
& BIT(LM3630A_SINK_0
) ||
432 !(led_sources
& BIT(LM3630A_SINK_1
)))
435 pdata
->ledb_ctrl
= linear
?
436 LM3630A_LEDB_ENABLE_LINEAR
:
439 if (!(led_sources
& BIT(LM3630A_SINK_0
)))
442 pdata
->leda_ctrl
= linear
?
443 LM3630A_LEDA_ENABLE_LINEAR
:
446 if (led_sources
& BIT(LM3630A_SINK_1
))
447 pdata
->ledb_ctrl
= LM3630A_LEDB_ON_A
;
450 ret
= fwnode_property_read_string(node
, "label", &label
);
453 pdata
->ledb_label
= label
;
455 pdata
->leda_label
= label
;
458 ret
= fwnode_property_read_u32(node
, "default-brightness",
462 pdata
->ledb_init_brt
= val
;
464 pdata
->leda_init_brt
= val
;
467 ret
= fwnode_property_read_u32(node
, "max-brightness", &val
);
470 pdata
->ledb_max_brt
= val
;
472 pdata
->leda_max_brt
= val
;
478 static int lm3630a_parse_node(struct lm3630a_chip
*pchip
,
479 struct lm3630a_platform_data
*pdata
)
481 int ret
= -ENODEV
, seen_led_sources
= 0;
482 struct fwnode_handle
*node
;
484 device_for_each_child_node(pchip
->dev
, node
) {
485 ret
= lm3630a_parse_bank(pdata
, node
, &seen_led_sources
);
487 fwnode_handle_put(node
);
495 static int lm3630a_probe(struct i2c_client
*client
)
497 struct lm3630a_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
498 struct lm3630a_chip
*pchip
;
501 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
502 dev_err(&client
->dev
, "fail : i2c functionality check\n");
506 pchip
= devm_kzalloc(&client
->dev
, sizeof(struct lm3630a_chip
),
510 pchip
->dev
= &client
->dev
;
512 pchip
->regmap
= devm_regmap_init_i2c(client
, &lm3630a_regmap
);
513 if (IS_ERR(pchip
->regmap
)) {
514 rval
= PTR_ERR(pchip
->regmap
);
515 dev_err(&client
->dev
, "fail : allocate reg. map: %d\n", rval
);
519 i2c_set_clientdata(client
, pchip
);
521 pdata
= devm_kzalloc(pchip
->dev
,
522 sizeof(struct lm3630a_platform_data
),
528 pdata
->leda_max_brt
= LM3630A_MAX_BRIGHTNESS
;
529 pdata
->ledb_max_brt
= LM3630A_MAX_BRIGHTNESS
;
530 pdata
->leda_init_brt
= LM3630A_MAX_BRIGHTNESS
;
531 pdata
->ledb_init_brt
= LM3630A_MAX_BRIGHTNESS
;
533 rval
= lm3630a_parse_node(pchip
, pdata
);
535 dev_err(&client
->dev
, "fail : parse node\n");
539 pchip
->pdata
= pdata
;
541 pchip
->enable_gpio
= devm_gpiod_get_optional(&client
->dev
, "enable",
543 if (IS_ERR(pchip
->enable_gpio
))
544 return PTR_ERR(pchip
->enable_gpio
);
546 /* chip initialize */
547 rval
= lm3630a_chip_init(pchip
);
549 dev_err(&client
->dev
, "fail : init chip\n");
552 /* backlight register */
553 rval
= lm3630a_backlight_register(pchip
);
555 dev_err(&client
->dev
, "fail : backlight register.\n");
559 if (pdata
->pwm_ctrl
!= LM3630A_PWM_DISABLE
) {
560 pchip
->pwmd
= devm_pwm_get(pchip
->dev
, "lm3630a-pwm");
561 if (IS_ERR(pchip
->pwmd
))
562 return dev_err_probe(&client
->dev
, PTR_ERR(pchip
->pwmd
),
563 "fail : get pwm device\n");
565 pwm_init_state(pchip
->pwmd
, &pchip
->pwmd_state
);
568 /* interrupt enable : irq 0 is not allowed */
569 pchip
->irq
= client
->irq
;
571 rval
= lm3630a_intr_config(pchip
);
575 dev_info(&client
->dev
, "LM3630A backlight register OK.\n");
579 static void lm3630a_remove(struct i2c_client
*client
)
582 struct lm3630a_chip
*pchip
= i2c_get_clientdata(client
);
584 rval
= lm3630a_write(pchip
, REG_BRT_A
, 0);
586 dev_err(pchip
->dev
, "i2c failed to access register\n");
588 rval
= lm3630a_write(pchip
, REG_BRT_B
, 0);
590 dev_err(pchip
->dev
, "i2c failed to access register\n");
593 free_irq(pchip
->irq
, pchip
);
594 destroy_workqueue(pchip
->irqthread
);
598 static const struct i2c_device_id lm3630a_id
[] = {
603 MODULE_DEVICE_TABLE(i2c
, lm3630a_id
);
605 static const struct of_device_id lm3630a_match_table
[] = {
606 { .compatible
= "ti,lm3630a", },
610 MODULE_DEVICE_TABLE(of
, lm3630a_match_table
);
612 static struct i2c_driver lm3630a_i2c_driver
= {
614 .name
= LM3630A_NAME
,
615 .of_match_table
= lm3630a_match_table
,
617 .probe
= lm3630a_probe
,
618 .remove
= lm3630a_remove
,
619 .id_table
= lm3630a_id
,
622 module_i2c_driver(lm3630a_i2c_driver
);
624 MODULE_DESCRIPTION("Texas Instruments Backlight driver for LM3630A");
625 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
626 MODULE_AUTHOR("LDD MLP <ldd-mlp@list.ti.com>");
627 MODULE_LICENSE("GPL v2");