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
34 #define REG_FILTER_STRENGTH 0x50
37 #define INT_DEBOUNCE_MSEC 10
40 struct delayed_work work
;
43 struct workqueue_struct
*irqthread
;
44 struct lm3630a_platform_data
*pdata
;
45 struct backlight_device
*bleda
;
46 struct backlight_device
*bledb
;
47 struct regmap
*regmap
;
48 struct pwm_device
*pwmd
;
52 static int lm3630a_read(struct lm3630a_chip
*pchip
, unsigned int reg
)
57 rval
= regmap_read(pchip
->regmap
, reg
, ®_val
);
60 return reg_val
& 0xFF;
63 static int lm3630a_write(struct lm3630a_chip
*pchip
,
64 unsigned int reg
, unsigned int data
)
66 return regmap_write(pchip
->regmap
, reg
, data
);
69 static int lm3630a_update(struct lm3630a_chip
*pchip
,
70 unsigned int reg
, unsigned int mask
,
73 return regmap_update_bits(pchip
->regmap
, reg
, mask
, data
);
77 static int lm3630a_chip_init(struct lm3630a_chip
*pchip
)
80 struct lm3630a_platform_data
*pdata
= pchip
->pdata
;
82 usleep_range(1000, 2000);
83 /* set Filter Strength Register */
84 rval
= lm3630a_write(pchip
, REG_FILTER_STRENGTH
, 0x03);
85 /* set Cofig. register */
86 rval
|= lm3630a_update(pchip
, REG_CONFIG
, 0x07, pdata
->pwm_ctrl
);
87 /* set boost control */
88 rval
|= lm3630a_write(pchip
, REG_BOOST
, 0x38);
90 rval
|= lm3630a_update(pchip
, REG_I_A
, 0x1F, 0x1F);
92 rval
|= lm3630a_write(pchip
, REG_I_B
, 0x1F);
94 rval
|= lm3630a_update(pchip
, REG_CTRL
, 0x14, pdata
->leda_ctrl
);
95 rval
|= lm3630a_update(pchip
, REG_CTRL
, 0x0B, pdata
->ledb_ctrl
);
96 usleep_range(1000, 2000);
97 /* set brightness A and B */
98 rval
|= lm3630a_write(pchip
, REG_BRT_A
, pdata
->leda_init_brt
);
99 rval
|= lm3630a_write(pchip
, REG_BRT_B
, pdata
->ledb_init_brt
);
102 dev_err(pchip
->dev
, "i2c failed to access register\n");
106 /* interrupt handling */
107 static void lm3630a_delayed_func(struct work_struct
*work
)
110 struct lm3630a_chip
*pchip
;
112 pchip
= container_of(work
, struct lm3630a_chip
, work
.work
);
114 rval
= lm3630a_read(pchip
, REG_INT_STATUS
);
117 "i2c failed to access REG_INT_STATUS Register\n");
121 dev_info(pchip
->dev
, "REG_INT_STATUS Register is 0x%x\n", rval
);
124 static irqreturn_t
lm3630a_isr_func(int irq
, void *chip
)
127 struct lm3630a_chip
*pchip
= chip
;
128 unsigned long delay
= msecs_to_jiffies(INT_DEBOUNCE_MSEC
);
130 queue_delayed_work(pchip
->irqthread
, &pchip
->work
, delay
);
132 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
134 dev_err(pchip
->dev
, "i2c failed to access register\n");
140 static int lm3630a_intr_config(struct lm3630a_chip
*pchip
)
144 rval
= lm3630a_write(pchip
, REG_INT_EN
, 0x87);
148 INIT_DELAYED_WORK(&pchip
->work
, lm3630a_delayed_func
);
149 pchip
->irqthread
= create_singlethread_workqueue("lm3630a-irqthd");
150 if (!pchip
->irqthread
) {
151 dev_err(pchip
->dev
, "create irq thread fail\n");
154 if (request_threaded_irq
155 (pchip
->irq
, NULL
, lm3630a_isr_func
,
156 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
, "lm3630a_irq", pchip
)) {
157 dev_err(pchip
->dev
, "request threaded irq fail\n");
158 destroy_workqueue(pchip
->irqthread
);
164 static void lm3630a_pwm_ctrl(struct lm3630a_chip
*pchip
, int br
, int br_max
)
166 unsigned int period
= pchip
->pdata
->pwm_period
;
167 unsigned int duty
= br
* period
/ br_max
;
169 pwm_config(pchip
->pwmd
, duty
, period
);
171 pwm_enable(pchip
->pwmd
);
173 pwm_disable(pchip
->pwmd
);
176 /* update and get brightness */
177 static int lm3630a_bank_a_update_status(struct backlight_device
*bl
)
180 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
181 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
184 if ((pwm_ctrl
& LM3630A_PWM_BANK_A
) != 0) {
185 lm3630a_pwm_ctrl(pchip
, bl
->props
.brightness
,
186 bl
->props
.max_brightness
);
187 return bl
->props
.brightness
;
191 ret
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
194 usleep_range(1000, 2000);
195 /* minimum brightness is 0x04 */
196 ret
= lm3630a_write(pchip
, REG_BRT_A
, bl
->props
.brightness
);
197 if (bl
->props
.brightness
< 0x4)
198 ret
|= lm3630a_update(pchip
, REG_CTRL
, LM3630A_LEDA_ENABLE
, 0);
200 ret
|= lm3630a_update(pchip
, REG_CTRL
,
201 LM3630A_LEDA_ENABLE
, LM3630A_LEDA_ENABLE
);
204 return bl
->props
.brightness
;
207 dev_err(pchip
->dev
, "i2c failed to access\n");
208 return bl
->props
.brightness
;
211 static int lm3630a_bank_a_get_brightness(struct backlight_device
*bl
)
213 int brightness
, rval
;
214 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
215 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
217 if ((pwm_ctrl
& LM3630A_PWM_BANK_A
) != 0) {
218 rval
= lm3630a_read(pchip
, REG_PWM_OUTHIGH
);
221 brightness
= (rval
& 0x01) << 8;
222 rval
= lm3630a_read(pchip
, REG_PWM_OUTLOW
);
230 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
233 usleep_range(1000, 2000);
234 rval
= lm3630a_read(pchip
, REG_BRT_A
);
240 bl
->props
.brightness
= brightness
;
241 return bl
->props
.brightness
;
243 dev_err(pchip
->dev
, "i2c failed to access register\n");
247 static const struct backlight_ops lm3630a_bank_a_ops
= {
248 .options
= BL_CORE_SUSPENDRESUME
,
249 .update_status
= lm3630a_bank_a_update_status
,
250 .get_brightness
= lm3630a_bank_a_get_brightness
,
253 /* update and get brightness */
254 static int lm3630a_bank_b_update_status(struct backlight_device
*bl
)
257 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
258 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
261 if ((pwm_ctrl
& LM3630A_PWM_BANK_B
) != 0) {
262 lm3630a_pwm_ctrl(pchip
, bl
->props
.brightness
,
263 bl
->props
.max_brightness
);
264 return bl
->props
.brightness
;
268 ret
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
271 usleep_range(1000, 2000);
272 /* minimum brightness is 0x04 */
273 ret
= lm3630a_write(pchip
, REG_BRT_B
, bl
->props
.brightness
);
274 if (bl
->props
.brightness
< 0x4)
275 ret
|= lm3630a_update(pchip
, REG_CTRL
, LM3630A_LEDB_ENABLE
, 0);
277 ret
|= lm3630a_update(pchip
, REG_CTRL
,
278 LM3630A_LEDB_ENABLE
, LM3630A_LEDB_ENABLE
);
281 return bl
->props
.brightness
;
284 dev_err(pchip
->dev
, "i2c failed to access REG_CTRL\n");
285 return bl
->props
.brightness
;
288 static int lm3630a_bank_b_get_brightness(struct backlight_device
*bl
)
290 int brightness
, rval
;
291 struct lm3630a_chip
*pchip
= bl_get_data(bl
);
292 enum lm3630a_pwm_ctrl pwm_ctrl
= pchip
->pdata
->pwm_ctrl
;
294 if ((pwm_ctrl
& LM3630A_PWM_BANK_B
) != 0) {
295 rval
= lm3630a_read(pchip
, REG_PWM_OUTHIGH
);
298 brightness
= (rval
& 0x01) << 8;
299 rval
= lm3630a_read(pchip
, REG_PWM_OUTLOW
);
307 rval
= lm3630a_update(pchip
, REG_CTRL
, 0x80, 0x00);
310 usleep_range(1000, 2000);
311 rval
= lm3630a_read(pchip
, REG_BRT_B
);
317 bl
->props
.brightness
= brightness
;
318 return bl
->props
.brightness
;
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 backlight_properties props
;
333 struct lm3630a_platform_data
*pdata
= pchip
->pdata
;
335 props
.type
= BACKLIGHT_RAW
;
336 if (pdata
->leda_ctrl
!= LM3630A_LEDA_DISABLE
) {
337 props
.brightness
= pdata
->leda_init_brt
;
338 props
.max_brightness
= pdata
->leda_max_brt
;
340 devm_backlight_device_register(pchip
->dev
, "lm3630a_leda",
342 &lm3630a_bank_a_ops
, &props
);
343 if (IS_ERR(pchip
->bleda
))
344 return PTR_ERR(pchip
->bleda
);
347 if ((pdata
->ledb_ctrl
!= LM3630A_LEDB_DISABLE
) &&
348 (pdata
->ledb_ctrl
!= LM3630A_LEDB_ON_A
)) {
349 props
.brightness
= pdata
->ledb_init_brt
;
350 props
.max_brightness
= pdata
->ledb_max_brt
;
352 devm_backlight_device_register(pchip
->dev
, "lm3630a_ledb",
354 &lm3630a_bank_b_ops
, &props
);
355 if (IS_ERR(pchip
->bledb
))
356 return PTR_ERR(pchip
->bledb
);
361 static const struct regmap_config lm3630a_regmap
= {
364 .max_register
= REG_MAX
,
367 static int lm3630a_probe(struct i2c_client
*client
,
368 const struct i2c_device_id
*id
)
370 struct lm3630a_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
371 struct lm3630a_chip
*pchip
;
374 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
375 dev_err(&client
->dev
, "fail : i2c functionality check\n");
379 pchip
= devm_kzalloc(&client
->dev
, sizeof(struct lm3630a_chip
),
383 pchip
->dev
= &client
->dev
;
385 pchip
->regmap
= devm_regmap_init_i2c(client
, &lm3630a_regmap
);
386 if (IS_ERR(pchip
->regmap
)) {
387 rval
= PTR_ERR(pchip
->regmap
);
388 dev_err(&client
->dev
, "fail : allocate reg. map: %d\n", rval
);
392 i2c_set_clientdata(client
, pchip
);
394 pdata
= devm_kzalloc(pchip
->dev
,
395 sizeof(struct lm3630a_platform_data
),
400 pdata
->leda_ctrl
= LM3630A_LEDA_ENABLE
;
401 pdata
->ledb_ctrl
= LM3630A_LEDB_ENABLE
;
402 pdata
->leda_max_brt
= LM3630A_MAX_BRIGHTNESS
;
403 pdata
->ledb_max_brt
= LM3630A_MAX_BRIGHTNESS
;
404 pdata
->leda_init_brt
= LM3630A_MAX_BRIGHTNESS
;
405 pdata
->ledb_init_brt
= LM3630A_MAX_BRIGHTNESS
;
407 pchip
->pdata
= pdata
;
409 /* chip initialize */
410 rval
= lm3630a_chip_init(pchip
);
412 dev_err(&client
->dev
, "fail : init chip\n");
415 /* backlight register */
416 rval
= lm3630a_backlight_register(pchip
);
418 dev_err(&client
->dev
, "fail : backlight register.\n");
422 if (pdata
->pwm_ctrl
!= LM3630A_PWM_DISABLE
) {
423 pchip
->pwmd
= devm_pwm_get(pchip
->dev
, "lm3630a-pwm");
424 if (IS_ERR(pchip
->pwmd
)) {
425 dev_err(&client
->dev
, "fail : get pwm device\n");
426 return PTR_ERR(pchip
->pwmd
);
430 * FIXME: pwm_apply_args() should be removed when switching to
431 * the atomic PWM API.
433 pwm_apply_args(pchip
->pwmd
);
436 /* interrupt enable : irq 0 is not allowed */
437 pchip
->irq
= client
->irq
;
439 rval
= lm3630a_intr_config(pchip
);
443 dev_info(&client
->dev
, "LM3630A backlight register OK.\n");
447 static int lm3630a_remove(struct i2c_client
*client
)
450 struct lm3630a_chip
*pchip
= i2c_get_clientdata(client
);
452 rval
= lm3630a_write(pchip
, REG_BRT_A
, 0);
454 dev_err(pchip
->dev
, "i2c failed to access register\n");
456 rval
= lm3630a_write(pchip
, REG_BRT_B
, 0);
458 dev_err(pchip
->dev
, "i2c failed to access register\n");
461 free_irq(pchip
->irq
, pchip
);
462 flush_workqueue(pchip
->irqthread
);
463 destroy_workqueue(pchip
->irqthread
);
468 static const struct i2c_device_id lm3630a_id
[] = {
473 MODULE_DEVICE_TABLE(i2c
, lm3630a_id
);
475 static struct i2c_driver lm3630a_i2c_driver
= {
477 .name
= LM3630A_NAME
,
479 .probe
= lm3630a_probe
,
480 .remove
= lm3630a_remove
,
481 .id_table
= lm3630a_id
,
484 module_i2c_driver(lm3630a_i2c_driver
);
486 MODULE_DESCRIPTION("Texas Instruments Backlight driver for LM3630A");
487 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
488 MODULE_AUTHOR("LDD MLP <ldd-mlp@list.ti.com>");
489 MODULE_LICENSE("GPL v2");