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;
58 static int lm3630a_read(struct lm3630a_chip *pchip, unsigned int reg)
63 rval = regmap_read(pchip->regmap, reg, ®_val);
66 return reg_val & 0xFF;
69 static int lm3630a_write(struct lm3630a_chip *pchip,
70 unsigned int reg, unsigned int data)
72 return regmap_write(pchip->regmap, reg, data);
75 static int lm3630a_update(struct lm3630a_chip *pchip,
76 unsigned int reg, unsigned int mask,
79 return regmap_update_bits(pchip->regmap, reg, mask, data);
83 static int lm3630a_chip_init(struct lm3630a_chip *pchip)
86 struct lm3630a_platform_data *pdata = pchip->pdata;
88 usleep_range(1000, 2000);
89 /* set Filter Strength Register */
90 rval = lm3630a_write(pchip, REG_FILTER_STRENGTH, 0x03);
91 /* set Cofig. register */
92 rval |= lm3630a_update(pchip, REG_CONFIG, 0x07, pdata->pwm_ctrl);
93 /* set boost control */
94 rval |= lm3630a_write(pchip, REG_BOOST, 0x38);
96 rval |= lm3630a_update(pchip, REG_I_A, 0x1F, 0x1F);
98 rval |= lm3630a_write(pchip, REG_I_B, 0x1F);
100 rval |= lm3630a_update(pchip, REG_CTRL, 0x14, pdata->leda_ctrl);
101 rval |= lm3630a_update(pchip, REG_CTRL, 0x0B, pdata->ledb_ctrl);
102 usleep_range(1000, 2000);
103 /* set brightness A and B */
104 rval |= lm3630a_write(pchip, REG_BRT_A, pdata->leda_init_brt);
105 rval |= lm3630a_write(pchip, REG_BRT_B, pdata->ledb_init_brt);
108 dev_err(pchip->dev, "i2c failed to access register\n");
112 /* interrupt handling */
113 static void lm3630a_delayed_func(struct work_struct *work)
116 struct lm3630a_chip *pchip;
118 pchip = container_of(work, struct lm3630a_chip, work.work);
120 rval = lm3630a_read(pchip, REG_INT_STATUS);
123 "i2c failed to access REG_INT_STATUS Register\n");
127 dev_info(pchip->dev, "REG_INT_STATUS Register is 0x%x\n", rval);
130 static irqreturn_t lm3630a_isr_func(int irq, void *chip)
133 struct lm3630a_chip *pchip = chip;
134 unsigned long delay = msecs_to_jiffies(INT_DEBOUNCE_MSEC);
136 queue_delayed_work(pchip->irqthread, &pchip->work, delay);
138 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
140 dev_err(pchip->dev, "i2c failed to access register\n");
146 static int lm3630a_intr_config(struct lm3630a_chip *pchip)
150 rval = lm3630a_write(pchip, REG_INT_EN, 0x87);
154 INIT_DELAYED_WORK(&pchip->work, lm3630a_delayed_func);
155 pchip->irqthread = create_singlethread_workqueue("lm3630a-irqthd");
156 if (!pchip->irqthread) {
157 dev_err(pchip->dev, "create irq thread fail\n");
160 if (request_threaded_irq
161 (pchip->irq, NULL, lm3630a_isr_func,
162 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lm3630a_irq", pchip)) {
163 dev_err(pchip->dev, "request threaded irq fail\n");
164 destroy_workqueue(pchip->irqthread);
170 static void lm3630a_pwm_ctrl(struct lm3630a_chip *pchip, int br, int br_max)
172 unsigned int period = pchip->pdata->pwm_period;
173 unsigned int duty = br * period / br_max;
175 pwm_config(pchip->pwmd, duty, period);
177 pwm_enable(pchip->pwmd);
179 pwm_disable(pchip->pwmd);
182 /* update and get brightness */
183 static int lm3630a_bank_a_update_status(struct backlight_device *bl)
186 struct lm3630a_chip *pchip = bl_get_data(bl);
187 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
190 if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
191 lm3630a_pwm_ctrl(pchip, bl->props.brightness,
192 bl->props.max_brightness);
197 ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
200 usleep_range(1000, 2000);
201 /* minimum brightness is 0x04 */
202 ret = lm3630a_write(pchip, REG_BRT_A, bl->props.brightness);
203 if (bl->props.brightness < 0x4)
204 ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDA_ENABLE, 0);
206 ret |= lm3630a_update(pchip, REG_CTRL,
207 LM3630A_LEDA_ENABLE, LM3630A_LEDA_ENABLE);
213 dev_err(pchip->dev, "i2c failed to access (%pe)\n", ERR_PTR(ret));
217 static int lm3630a_bank_a_get_brightness(struct backlight_device *bl)
219 int brightness, rval;
220 struct lm3630a_chip *pchip = bl_get_data(bl);
221 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
223 if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
224 rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
227 brightness = (rval & 0x01) << 8;
228 rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
236 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
239 usleep_range(1000, 2000);
240 rval = lm3630a_read(pchip, REG_BRT_A);
246 dev_err(pchip->dev, "i2c failed to access register\n");
250 static const struct backlight_ops lm3630a_bank_a_ops = {
251 .options = BL_CORE_SUSPENDRESUME,
252 .update_status = lm3630a_bank_a_update_status,
253 .get_brightness = lm3630a_bank_a_get_brightness,
256 /* update and get brightness */
257 static int lm3630a_bank_b_update_status(struct backlight_device *bl)
260 struct lm3630a_chip *pchip = bl_get_data(bl);
261 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
264 if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
265 lm3630a_pwm_ctrl(pchip, bl->props.brightness,
266 bl->props.max_brightness);
271 ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
274 usleep_range(1000, 2000);
275 /* minimum brightness is 0x04 */
276 ret = lm3630a_write(pchip, REG_BRT_B, bl->props.brightness);
277 if (bl->props.brightness < 0x4)
278 ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDB_ENABLE, 0);
280 ret |= lm3630a_update(pchip, REG_CTRL,
281 LM3630A_LEDB_ENABLE, LM3630A_LEDB_ENABLE);
287 dev_err(pchip->dev, "i2c failed to access (%pe)\n", ERR_PTR(ret));
291 static int lm3630a_bank_b_get_brightness(struct backlight_device *bl)
293 int brightness, rval;
294 struct lm3630a_chip *pchip = bl_get_data(bl);
295 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
297 if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
298 rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
301 brightness = (rval & 0x01) << 8;
302 rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
310 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
313 usleep_range(1000, 2000);
314 rval = lm3630a_read(pchip, REG_BRT_B);
320 dev_err(pchip->dev, "i2c failed to access register\n");
324 static const struct backlight_ops lm3630a_bank_b_ops = {
325 .options = BL_CORE_SUSPENDRESUME,
326 .update_status = lm3630a_bank_b_update_status,
327 .get_brightness = lm3630a_bank_b_get_brightness,
330 static int lm3630a_backlight_register(struct lm3630a_chip *pchip)
332 struct lm3630a_platform_data *pdata = pchip->pdata;
333 struct backlight_properties props;
336 memset(&props, 0, sizeof(struct backlight_properties));
337 props.type = BACKLIGHT_RAW;
338 if (pdata->leda_ctrl != LM3630A_LEDA_DISABLE) {
339 props.brightness = pdata->leda_init_brt;
340 props.max_brightness = pdata->leda_max_brt;
341 label = pdata->leda_label ? pdata->leda_label : "lm3630a_leda";
343 devm_backlight_device_register(pchip->dev, label,
345 &lm3630a_bank_a_ops, &props);
346 if (IS_ERR(pchip->bleda))
347 return PTR_ERR(pchip->bleda);
350 if ((pdata->ledb_ctrl != LM3630A_LEDB_DISABLE) &&
351 (pdata->ledb_ctrl != LM3630A_LEDB_ON_A)) {
352 props.brightness = pdata->ledb_init_brt;
353 props.max_brightness = pdata->ledb_max_brt;
354 label = pdata->ledb_label ? pdata->ledb_label : "lm3630a_ledb";
356 devm_backlight_device_register(pchip->dev, label,
358 &lm3630a_bank_b_ops, &props);
359 if (IS_ERR(pchip->bledb))
360 return PTR_ERR(pchip->bledb);
365 static const struct regmap_config lm3630a_regmap = {
368 .max_register = REG_MAX,
371 static int lm3630a_parse_led_sources(struct fwnode_handle *node,
372 int default_led_sources)
374 u32 sources[LM3630A_NUM_SINKS];
375 int ret, num_sources, i;
377 num_sources = fwnode_property_count_u32(node, "led-sources");
379 return default_led_sources;
380 else if (num_sources > ARRAY_SIZE(sources))
383 ret = fwnode_property_read_u32_array(node, "led-sources", sources,
388 for (i = 0; i < num_sources; i++) {
389 if (sources[i] != LM3630A_SINK_0 && sources[i] != LM3630A_SINK_1)
392 ret |= BIT(sources[i]);
398 static int lm3630a_parse_bank(struct lm3630a_platform_data *pdata,
399 struct fwnode_handle *node, int *seen_led_sources)
401 int led_sources, ret;
406 ret = fwnode_property_read_u32(node, "reg", &bank);
410 if (bank != LM3630A_BANK_0 && bank != LM3630A_BANK_1)
413 led_sources = lm3630a_parse_led_sources(node, BIT(bank));
417 if (*seen_led_sources & led_sources)
420 *seen_led_sources |= led_sources;
422 linear = fwnode_property_read_bool(node,
423 "ti,linear-mapping-mode");
425 if (led_sources & BIT(LM3630A_SINK_0) ||
426 !(led_sources & BIT(LM3630A_SINK_1)))
429 pdata->ledb_ctrl = linear ?
430 LM3630A_LEDB_ENABLE_LINEAR :
433 if (!(led_sources & BIT(LM3630A_SINK_0)))
436 pdata->leda_ctrl = linear ?
437 LM3630A_LEDA_ENABLE_LINEAR :
440 if (led_sources & BIT(LM3630A_SINK_1))
441 pdata->ledb_ctrl = LM3630A_LEDB_ON_A;
444 ret = fwnode_property_read_string(node, "label", &label);
447 pdata->ledb_label = label;
449 pdata->leda_label = label;
452 ret = fwnode_property_read_u32(node, "default-brightness",
456 pdata->ledb_init_brt = val;
458 pdata->leda_init_brt = val;
461 ret = fwnode_property_read_u32(node, "max-brightness", &val);
464 pdata->ledb_max_brt = val;
466 pdata->leda_max_brt = val;
472 static int lm3630a_parse_node(struct lm3630a_chip *pchip,
473 struct lm3630a_platform_data *pdata)
475 int ret = -ENODEV, seen_led_sources = 0;
476 struct fwnode_handle *node;
478 device_for_each_child_node(pchip->dev, node) {
479 ret = lm3630a_parse_bank(pdata, node, &seen_led_sources);
481 fwnode_handle_put(node);
489 static int lm3630a_probe(struct i2c_client *client,
490 const struct i2c_device_id *id)
492 struct lm3630a_platform_data *pdata = dev_get_platdata(&client->dev);
493 struct lm3630a_chip *pchip;
496 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
497 dev_err(&client->dev, "fail : i2c functionality check\n");
501 pchip = devm_kzalloc(&client->dev, sizeof(struct lm3630a_chip),
505 pchip->dev = &client->dev;
507 pchip->regmap = devm_regmap_init_i2c(client, &lm3630a_regmap);
508 if (IS_ERR(pchip->regmap)) {
509 rval = PTR_ERR(pchip->regmap);
510 dev_err(&client->dev, "fail : allocate reg. map: %d\n", rval);
514 i2c_set_clientdata(client, pchip);
516 pdata = devm_kzalloc(pchip->dev,
517 sizeof(struct lm3630a_platform_data),
523 pdata->leda_max_brt = LM3630A_MAX_BRIGHTNESS;
524 pdata->ledb_max_brt = LM3630A_MAX_BRIGHTNESS;
525 pdata->leda_init_brt = LM3630A_MAX_BRIGHTNESS;
526 pdata->ledb_init_brt = LM3630A_MAX_BRIGHTNESS;
528 rval = lm3630a_parse_node(pchip, pdata);
530 dev_err(&client->dev, "fail : parse node\n");
534 pchip->pdata = pdata;
536 pchip->enable_gpio = devm_gpiod_get_optional(&client->dev, "enable",
538 if (IS_ERR(pchip->enable_gpio)) {
539 rval = PTR_ERR(pchip->enable_gpio);
543 /* chip initialize */
544 rval = lm3630a_chip_init(pchip);
546 dev_err(&client->dev, "fail : init chip\n");
549 /* backlight register */
550 rval = lm3630a_backlight_register(pchip);
552 dev_err(&client->dev, "fail : backlight register.\n");
556 if (pdata->pwm_ctrl != LM3630A_PWM_DISABLE) {
557 pchip->pwmd = devm_pwm_get(pchip->dev, "lm3630a-pwm");
558 if (IS_ERR(pchip->pwmd)) {
559 dev_err(&client->dev, "fail : get pwm device\n");
560 return PTR_ERR(pchip->pwmd);
564 * FIXME: pwm_apply_args() should be removed when switching to
565 * the atomic PWM API.
567 pwm_apply_args(pchip->pwmd);
570 /* interrupt enable : irq 0 is not allowed */
571 pchip->irq = client->irq;
573 rval = lm3630a_intr_config(pchip);
577 dev_info(&client->dev, "LM3630A backlight register OK.\n");
581 static int lm3630a_remove(struct i2c_client *client)
584 struct lm3630a_chip *pchip = i2c_get_clientdata(client);
586 rval = lm3630a_write(pchip, REG_BRT_A, 0);
588 dev_err(pchip->dev, "i2c failed to access register\n");
590 rval = lm3630a_write(pchip, REG_BRT_B, 0);
592 dev_err(pchip->dev, "i2c failed to access register\n");
595 free_irq(pchip->irq, pchip);
596 flush_workqueue(pchip->irqthread);
597 destroy_workqueue(pchip->irqthread);
602 static const struct i2c_device_id lm3630a_id[] = {
607 MODULE_DEVICE_TABLE(i2c, lm3630a_id);
609 static const struct of_device_id lm3630a_match_table[] = {
610 { .compatible = "ti,lm3630a", },
614 MODULE_DEVICE_TABLE(of, lm3630a_match_table);
616 static struct i2c_driver lm3630a_i2c_driver = {
618 .name = LM3630A_NAME,
619 .of_match_table = lm3630a_match_table,
621 .probe = lm3630a_probe,
622 .remove = lm3630a_remove,
623 .id_table = lm3630a_id,
626 module_i2c_driver(lm3630a_i2c_driver);
628 MODULE_DESCRIPTION("Texas Instruments Backlight driver for LM3630A");
629 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
630 MODULE_AUTHOR("LDD MLP <ldd-mlp@list.ti.com>");
631 MODULE_LICENSE("GPL v2");