2 * Simple driver for Texas Instruments LM3630A Backlight driver chip
3 * Copyright (C) 2012 Texas Instruments
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/backlight.h>
14 #include <linux/err.h>
15 #include <linux/delay.h>
16 #include <linux/uaccess.h>
17 #include <linux/interrupt.h>
18 #include <linux/regmap.h>
19 #include <linux/pwm.h>
20 #include <linux/platform_data/lm3630a_bl.h>
23 #define REG_BOOST 0x02
24 #define REG_CONFIG 0x01
25 #define REG_BRT_A 0x03
26 #define REG_BRT_B 0x04
29 #define REG_INT_STATUS 0x09
30 #define REG_INT_EN 0x0A
31 #define REG_FAULT 0x0B
32 #define REG_PWM_OUTLOW 0x12
33 #define REG_PWM_OUTHIGH 0x13
36 #define INT_DEBOUNCE_MSEC 10
39 struct delayed_work work
;
42 struct workqueue_struct
*irqthread
;
43 struct lm3630a_platform_data
*pdata
;
44 struct backlight_device
*bleda
;
45 struct backlight_device
*bledb
;
46 struct regmap
*regmap
;
47 struct pwm_device
*pwmd
;
51 static int lm3630a_read(struct lm3630a_chip
*pchip
, unsigned int reg
)
56 rval
= regmap_read(pchip
->regmap
, reg
, ®_val
);
59 return reg_val
& 0xFF;
62 static int lm3630a_write(struct lm3630a_chip
*pchip
,
63 unsigned int reg
, unsigned int data
)
65 return regmap_write(pchip
->regmap
, reg
, data
);
68 static int lm3630a_update(struct lm3630a_chip
*pchip
,
69 unsigned int reg
, unsigned int mask
,
72 return regmap_update_bits(pchip
->regmap
, reg
, mask
, data
);
76 static int lm3630a_chip_init(struct lm3630a_chip
*pchip
)
79 struct lm3630a_platform_data
*pdata
= pchip
->pdata
;
81 usleep_range(1000, 2000);
82 /* set Filter Strength Register */
83 rval
= lm3630a_write(pchip
, 0x50, 0x03);
84 /* set Cofig. register */
85 rval
|= lm3630a_update(pchip
, REG_CONFIG
, 0x07, pdata
->pwm_ctrl
);
86 /* set boost control */
87 rval
|= lm3630a_write(pchip
, REG_BOOST
, 0x38);
89 rval
|= lm3630a_update(pchip
, REG_I_A
, 0x1F, 0x1F);
91 rval
|= lm3630a_write(pchip
, REG_I_B
, 0x1F);
93 rval
|= lm3630a_update(pchip
, REG_CTRL
, 0x14, pdata
->leda_ctrl
);
94 rval
|= lm3630a_update(pchip
, REG_CTRL
, 0x0B, pdata
->ledb_ctrl
);
95 usleep_range(1000, 2000);
96 /* set brightness A and B */
97 rval
|= lm3630a_write(pchip
, REG_BRT_A
, pdata
->leda_init_brt
);
98 rval
|= lm3630a_write(pchip
, REG_BRT_B
, pdata
->ledb_init_brt
);
101 dev_err(pchip
->dev
, "i2c failed to access register\n");
105 /* interrupt handling */
106 static void lm3630a_delayed_func(struct work_struct
*work
)
109 struct lm3630a_chip
*pchip
;
111 pchip
= container_of(work
, struct lm3630a_chip
, work
.work
);
113 rval
= lm3630a_read(pchip
, REG_INT_STATUS
);
116 "i2c failed to access REG_INT_STATUS Register\n");
120 dev_info(pchip
->dev
, "REG_INT_STATUS Register is 0x%x\n", rval
);
123 static irqreturn_t
lm3630a_isr_func(int irq
, void *chip
)
126 struct lm3630a_chip
*pchip
= chip
;
127 unsigned long delay
= msecs_to_jiffies(INT_DEBOUNCE_MSEC
);
129 queue_delayed_work(pchip
->irqthread
, &pchip
->work
, delay
);
131 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
133 dev_err(pchip
->dev
, "i2c failed to access register\n");
139 static int lm3630a_intr_config(struct lm3630a_chip
*pchip
)
143 rval
= lm3630a_write(pchip
, REG_INT_EN
, 0x87);
147 INIT_DELAYED_WORK(&pchip
->work
, lm3630a_delayed_func
);
148 pchip
->irqthread
= create_singlethread_workqueue("lm3630a-irqthd");
149 if (!pchip
->irqthread
) {
150 dev_err(pchip
->dev
, "create irq thread fail\n");
153 if (request_threaded_irq
154 (pchip
->irq
, NULL
, lm3630a_isr_func
,
155 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
, "lm3630a_irq", pchip
)) {
156 dev_err(pchip
->dev
, "request threaded irq fail\n");
162 static void lm3630a_pwm_ctrl(struct lm3630a_chip
*pchip
, int br
, int br_max
)
164 unsigned int period
= pwm_get_period(pchip
->pwmd
);
165 unsigned int duty
= br
* period
/ br_max
;
167 pwm_config(pchip
->pwmd
, duty
, period
);
169 pwm_enable(pchip
->pwmd
);
171 pwm_disable(pchip
->pwmd
);
174 /* update and get brightness */
175 static int lm3630a_bank_a_update_status(struct backlight_device
*bl
)
178 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
179 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
182 if ((pwm_ctrl
& LM3630A_PWM_BANK_A
) != 0) {
183 lm3630a_pwm_ctrl(pchip
, bl
->props
.brightness
,
184 bl
->props
.max_brightness
);
185 return bl
->props
.brightness
;
189 ret
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
192 usleep_range(1000, 2000);
193 /* minimum brightness is 0x04 */
194 ret
= lm3630a_write(pchip
, REG_BRT_A
, bl
->props
.brightness
);
195 if (bl
->props
.brightness
< 0x4)
196 ret
|= lm3630a_update(pchip
, REG_CTRL
, LM3630A_LEDA_ENABLE
, 0);
198 ret
|= lm3630a_update(pchip
, REG_CTRL
,
199 LM3630A_LEDA_ENABLE
, LM3630A_LEDA_ENABLE
);
202 return bl
->props
.brightness
;
205 dev_err(pchip
->dev
, "i2c failed to access\n");
206 return bl
->props
.brightness
;
209 static int lm3630a_bank_a_get_brightness(struct backlight_device
*bl
)
211 int brightness
, rval
;
212 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
213 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
215 if ((pwm_ctrl
& LM3630A_PWM_BANK_A
) != 0) {
216 rval
= lm3630a_read(pchip
, REG_PWM_OUTHIGH
);
219 brightness
= (rval
& 0x01) << 8;
220 rval
= lm3630a_read(pchip
, REG_PWM_OUTLOW
);
228 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
231 usleep_range(1000, 2000);
232 rval
= lm3630a_read(pchip
, REG_BRT_A
);
238 bl
->props
.brightness
= brightness
;
239 return bl
->props
.brightness
;
241 dev_err(pchip
->dev
, "i2c failed to access register\n");
245 static const struct backlight_ops lm3630a_bank_a_ops
= {
246 .options
= BL_CORE_SUSPENDRESUME
,
247 .update_status
= lm3630a_bank_a_update_status
,
248 .get_brightness
= lm3630a_bank_a_get_brightness
,
251 /* update and get brightness */
252 static int lm3630a_bank_b_update_status(struct backlight_device
*bl
)
255 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
256 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
259 if ((pwm_ctrl
& LM3630A_PWM_BANK_B
) != 0) {
260 lm3630a_pwm_ctrl(pchip
, bl
->props
.brightness
,
261 bl
->props
.max_brightness
);
262 return bl
->props
.brightness
;
266 ret
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
269 usleep_range(1000, 2000);
270 /* minimum brightness is 0x04 */
271 ret
= lm3630a_write(pchip
, REG_BRT_B
, bl
->props
.brightness
);
272 if (bl
->props
.brightness
< 0x4)
273 ret
|= lm3630a_update(pchip
, REG_CTRL
, LM3630A_LEDB_ENABLE
, 0);
275 ret
|= lm3630a_update(pchip
, REG_CTRL
,
276 LM3630A_LEDB_ENABLE
, LM3630A_LEDB_ENABLE
);
279 return bl
->props
.brightness
;
282 dev_err(pchip
->dev
, "i2c failed to access REG_CTRL\n");
283 return bl
->props
.brightness
;
286 static int lm3630a_bank_b_get_brightness(struct backlight_device
*bl
)
288 int brightness
, rval
;
289 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
290 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
292 if ((pwm_ctrl
& LM3630A_PWM_BANK_B
) != 0) {
293 rval
= lm3630a_read(pchip
, REG_PWM_OUTHIGH
);
296 brightness
= (rval
& 0x01) << 8;
297 rval
= lm3630a_read(pchip
, REG_PWM_OUTLOW
);
305 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
308 usleep_range(1000, 2000);
309 rval
= lm3630a_read(pchip
, REG_BRT_B
);
315 bl
->props
.brightness
= brightness
;
316 return bl
->props
.brightness
;
318 dev_err(pchip
->dev
, "i2c failed to access register\n");
322 static const struct backlight_ops lm3630a_bank_b_ops
= {
323 .options
= BL_CORE_SUSPENDRESUME
,
324 .update_status
= lm3630a_bank_b_update_status
,
325 .get_brightness
= lm3630a_bank_b_get_brightness
,
328 static int lm3630a_backlight_register(struct lm3630a_chip
*pchip
)
330 struct backlight_properties props
;
331 struct lm3630a_platform_data
*pdata
= pchip
->pdata
;
333 props
.type
= BACKLIGHT_RAW
;
334 if (pdata
->leda_ctrl
!= LM3630A_LEDA_DISABLE
) {
335 props
.brightness
= pdata
->leda_init_brt
;
336 props
.max_brightness
= pdata
->leda_max_brt
;
338 devm_backlight_device_register(pchip
->dev
, "lm3630a_leda",
340 &lm3630a_bank_a_ops
, &props
);
341 if (IS_ERR(pchip
->bleda
))
342 return PTR_ERR(pchip
->bleda
);
345 if ((pdata
->ledb_ctrl
!= LM3630A_LEDB_DISABLE
) &&
346 (pdata
->ledb_ctrl
!= LM3630A_LEDB_ON_A
)) {
347 props
.brightness
= pdata
->ledb_init_brt
;
348 props
.max_brightness
= pdata
->ledb_max_brt
;
350 devm_backlight_device_register(pchip
->dev
, "lm3630a_ledb",
352 &lm3630a_bank_b_ops
, &props
);
353 if (IS_ERR(pchip
->bledb
))
354 return PTR_ERR(pchip
->bledb
);
359 static const struct regmap_config lm3630a_regmap
= {
362 .max_register
= REG_MAX
,
365 static int lm3630a_probe(struct i2c_client
*client
,
366 const struct i2c_device_id
*id
)
368 struct lm3630a_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
369 struct lm3630a_chip
*pchip
;
372 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
373 dev_err(&client
->dev
, "fail : i2c functionality check\n");
377 pchip
= devm_kzalloc(&client
->dev
, sizeof(struct lm3630a_chip
),
381 pchip
->dev
= &client
->dev
;
383 pchip
->regmap
= devm_regmap_init_i2c(client
, &lm3630a_regmap
);
384 if (IS_ERR(pchip
->regmap
)) {
385 rval
= PTR_ERR(pchip
->regmap
);
386 dev_err(&client
->dev
, "fail : allocate reg. map: %d\n", rval
);
390 i2c_set_clientdata(client
, pchip
);
392 pdata
= devm_kzalloc(pchip
->dev
,
393 sizeof(struct lm3630a_platform_data
),
398 pdata
->leda_ctrl
= LM3630A_LEDA_ENABLE
;
399 pdata
->ledb_ctrl
= LM3630A_LEDB_ENABLE
;
400 pdata
->leda_max_brt
= LM3630A_MAX_BRIGHTNESS
;
401 pdata
->ledb_max_brt
= LM3630A_MAX_BRIGHTNESS
;
402 pdata
->leda_init_brt
= LM3630A_MAX_BRIGHTNESS
;
403 pdata
->ledb_init_brt
= LM3630A_MAX_BRIGHTNESS
;
405 pchip
->pdata
= pdata
;
407 /* chip initialize */
408 rval
= lm3630a_chip_init(pchip
);
410 dev_err(&client
->dev
, "fail : init chip\n");
413 /* backlight register */
414 rval
= lm3630a_backlight_register(pchip
);
416 dev_err(&client
->dev
, "fail : backlight register.\n");
420 if (pdata
->pwm_ctrl
!= LM3630A_PWM_DISABLE
) {
421 pchip
->pwmd
= devm_pwm_get(pchip
->dev
, "lm3630a-pwm");
422 if (IS_ERR(pchip
->pwmd
)) {
423 dev_err(&client
->dev
, "fail : get pwm device\n");
424 return PTR_ERR(pchip
->pwmd
);
427 pchip
->pwmd
->period
= pdata
->pwm_period
;
429 /* interrupt enable : irq 0 is not allowed */
430 pchip
->irq
= client
->irq
;
432 rval
= lm3630a_intr_config(pchip
);
436 dev_info(&client
->dev
, "LM3630A backlight register OK.\n");
440 static int lm3630a_remove(struct i2c_client
*client
)
443 struct lm3630a_chip
*pchip
= i2c_get_clientdata(client
);
445 rval
= lm3630a_write(pchip
, REG_BRT_A
, 0);
447 dev_err(pchip
->dev
, "i2c failed to access register\n");
449 rval
= lm3630a_write(pchip
, REG_BRT_B
, 0);
451 dev_err(pchip
->dev
, "i2c failed to access register\n");
454 free_irq(pchip
->irq
, pchip
);
455 flush_workqueue(pchip
->irqthread
);
456 destroy_workqueue(pchip
->irqthread
);
461 static const struct i2c_device_id lm3630a_id
[] = {
466 MODULE_DEVICE_TABLE(i2c
, lm3630a_id
);
468 static struct i2c_driver lm3630a_i2c_driver
= {
470 .name
= LM3630A_NAME
,
472 .probe
= lm3630a_probe
,
473 .remove
= lm3630a_remove
,
474 .id_table
= lm3630a_id
,
477 module_i2c_driver(lm3630a_i2c_driver
);
479 MODULE_DESCRIPTION("Texas Instruments Backlight driver for LM3630A");
480 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
481 MODULE_AUTHOR("LDD MLP <ldd-mlp@list.ti.com>");
482 MODULE_LICENSE("GPL v2");