2 * Copyright (C) 2011 ST-Ericsson SA.
3 * Copyright (C) 2009 Motorola, Inc.
5 * License Terms: GNU General Public License v2
7 * Simple driver for National Semiconductor LM3530 Backlight driver chip
9 * Author: Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>
10 * based on leds-lm3530.c by Dan Murphy <D.Murphy@motorola.com>
13 #include <linux/i2c.h>
14 #include <linux/leds.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/input.h>
18 #include <linux/led-lm3530.h>
19 #include <linux/types.h>
20 #include <linux/regulator/consumer.h>
22 #define LM3530_LED_DEV "lcd-backlight"
23 #define LM3530_NAME "lm3530-led"
25 #define LM3530_GEN_CONFIG 0x10
26 #define LM3530_ALS_CONFIG 0x20
27 #define LM3530_BRT_RAMP_RATE 0x30
28 #define LM3530_ALS_ZONE_REG 0x40
29 #define LM3530_ALS_IMP_SELECT 0x41
30 #define LM3530_BRT_CTRL_REG 0xA0
31 #define LM3530_ALS_ZB0_REG 0x60
32 #define LM3530_ALS_ZB1_REG 0x61
33 #define LM3530_ALS_ZB2_REG 0x62
34 #define LM3530_ALS_ZB3_REG 0x63
35 #define LM3530_ALS_Z0T_REG 0x70
36 #define LM3530_ALS_Z1T_REG 0x71
37 #define LM3530_ALS_Z2T_REG 0x72
38 #define LM3530_ALS_Z3T_REG 0x73
39 #define LM3530_ALS_Z4T_REG 0x74
40 #define LM3530_REG_MAX 15
42 /* General Control Register */
43 #define LM3530_EN_I2C_SHIFT (0)
44 #define LM3530_RAMP_LAW_SHIFT (1)
45 #define LM3530_MAX_CURR_SHIFT (2)
46 #define LM3530_EN_PWM_SHIFT (5)
47 #define LM3530_PWM_POL_SHIFT (6)
48 #define LM3530_EN_PWM_SIMPLE_SHIFT (7)
50 #define LM3530_ENABLE_I2C (1 << LM3530_EN_I2C_SHIFT)
51 #define LM3530_ENABLE_PWM (1 << LM3530_EN_PWM_SHIFT)
52 #define LM3530_POL_LOW (1 << LM3530_PWM_POL_SHIFT)
53 #define LM3530_ENABLE_PWM_SIMPLE (1 << LM3530_EN_PWM_SIMPLE_SHIFT)
55 /* ALS Config Register Options */
56 #define LM3530_ALS_AVG_TIME_SHIFT (0)
57 #define LM3530_EN_ALS_SHIFT (3)
58 #define LM3530_ALS_SEL_SHIFT (5)
60 #define LM3530_ENABLE_ALS (3 << LM3530_EN_ALS_SHIFT)
62 /* Brightness Ramp Rate Register */
63 #define LM3530_BRT_RAMP_FALL_SHIFT (0)
64 #define LM3530_BRT_RAMP_RISE_SHIFT (3)
66 /* ALS Resistor Select */
67 #define LM3530_ALS1_IMP_SHIFT (0)
68 #define LM3530_ALS2_IMP_SHIFT (4)
70 /* Zone Boundary Register defaults */
71 #define LM3530_ALS_ZB_MAX (4)
72 #define LM3530_ALS_WINDOW_mV (1000)
73 #define LM3530_ALS_OFFSET_mV (4)
75 /* Zone Target Register defaults */
76 #define LM3530_DEF_ZT_0 (0x7F)
77 #define LM3530_DEF_ZT_1 (0x66)
78 #define LM3530_DEF_ZT_2 (0x4C)
79 #define LM3530_DEF_ZT_3 (0x33)
80 #define LM3530_DEF_ZT_4 (0x19)
82 struct lm3530_mode_map
{
84 enum lm3530_mode mode_val
;
87 static struct lm3530_mode_map mode_map
[] = {
88 { "man", LM3530_BL_MODE_MANUAL
},
89 { "als", LM3530_BL_MODE_ALS
},
90 { "pwm", LM3530_BL_MODE_PWM
},
95 * @led_dev: led class device
97 * @pdata: LM3530 platform data
98 * @mode: mode of operation - manual, ALS, PWM
99 * @regulator: regulator
100 * @brighness: previous brightness value
101 * @enable: regulator is enabled
104 struct led_classdev led_dev
;
105 struct i2c_client
*client
;
106 struct lm3530_platform_data
*pdata
;
107 enum lm3530_mode mode
;
108 struct regulator
*regulator
;
109 enum led_brightness brightness
;
113 static const u8 lm3530_reg
[LM3530_REG_MAX
] = {
116 LM3530_BRT_RAMP_RATE
,
118 LM3530_ALS_IMP_SELECT
,
131 static int lm3530_get_mode_from_str(const char *str
)
135 for (i
= 0; i
< ARRAY_SIZE(mode_map
); i
++)
136 if (sysfs_streq(str
, mode_map
[i
].mode
))
137 return mode_map
[i
].mode_val
;
142 static int lm3530_init_registers(struct lm3530_data
*drvdata
)
151 u8 reg_val
[LM3530_REG_MAX
];
152 u8 zones
[LM3530_ALS_ZB_MAX
];
153 u32 als_vmin
, als_vmax
, als_vstep
;
154 struct lm3530_platform_data
*pltfm
= drvdata
->pdata
;
155 struct i2c_client
*client
= drvdata
->client
;
157 gen_config
= (pltfm
->brt_ramp_law
<< LM3530_RAMP_LAW_SHIFT
) |
158 ((pltfm
->max_current
& 7) << LM3530_MAX_CURR_SHIFT
);
160 if (drvdata
->mode
== LM3530_BL_MODE_MANUAL
||
161 drvdata
->mode
== LM3530_BL_MODE_ALS
)
162 gen_config
|= (LM3530_ENABLE_I2C
);
164 if (drvdata
->mode
== LM3530_BL_MODE_ALS
) {
165 if (pltfm
->als_vmax
== 0) {
166 pltfm
->als_vmin
= als_vmin
= 0;
167 pltfm
->als_vmin
= als_vmax
= LM3530_ALS_WINDOW_mV
;
170 als_vmin
= pltfm
->als_vmin
;
171 als_vmax
= pltfm
->als_vmax
;
173 if ((als_vmax
- als_vmin
) > LM3530_ALS_WINDOW_mV
)
174 pltfm
->als_vmax
= als_vmax
=
175 als_vmin
+ LM3530_ALS_WINDOW_mV
;
177 /* n zone boundary makes n+1 zones */
178 als_vstep
= (als_vmax
- als_vmin
) / (LM3530_ALS_ZB_MAX
+ 1);
180 for (i
= 0; i
< LM3530_ALS_ZB_MAX
; i
++)
181 zones
[i
] = (((als_vmin
+ LM3530_ALS_OFFSET_mV
) +
182 als_vstep
+ (i
* als_vstep
)) * LED_FULL
)
186 (pltfm
->als_avrg_time
<< LM3530_ALS_AVG_TIME_SHIFT
) |
187 (LM3530_ENABLE_ALS
) |
188 (pltfm
->als_input_mode
<< LM3530_ALS_SEL_SHIFT
);
191 (pltfm
->als1_resistor_sel
<< LM3530_ALS1_IMP_SHIFT
) |
192 (pltfm
->als2_resistor_sel
<< LM3530_ALS2_IMP_SHIFT
);
196 if (drvdata
->mode
== LM3530_BL_MODE_PWM
)
197 gen_config
|= (LM3530_ENABLE_PWM
) |
198 (pltfm
->pwm_pol_hi
<< LM3530_PWM_POL_SHIFT
) |
199 (LM3530_ENABLE_PWM_SIMPLE
);
201 brt_ramp
= (pltfm
->brt_ramp_fall
<< LM3530_BRT_RAMP_FALL_SHIFT
) |
202 (pltfm
->brt_ramp_rise
<< LM3530_BRT_RAMP_RISE_SHIFT
);
204 if (drvdata
->brightness
)
205 brightness
= drvdata
->brightness
;
207 brightness
= drvdata
->brightness
= pltfm
->brt_val
;
209 reg_val
[0] = gen_config
; /* LM3530_GEN_CONFIG */
210 reg_val
[1] = als_config
; /* LM3530_ALS_CONFIG */
211 reg_val
[2] = brt_ramp
; /* LM3530_BRT_RAMP_RATE */
212 reg_val
[3] = 0x00; /* LM3530_ALS_ZONE_REG */
213 reg_val
[4] = als_imp_sel
; /* LM3530_ALS_IMP_SELECT */
214 reg_val
[5] = brightness
; /* LM3530_BRT_CTRL_REG */
215 reg_val
[6] = zones
[0]; /* LM3530_ALS_ZB0_REG */
216 reg_val
[7] = zones
[1]; /* LM3530_ALS_ZB1_REG */
217 reg_val
[8] = zones
[2]; /* LM3530_ALS_ZB2_REG */
218 reg_val
[9] = zones
[3]; /* LM3530_ALS_ZB3_REG */
219 reg_val
[10] = LM3530_DEF_ZT_0
; /* LM3530_ALS_Z0T_REG */
220 reg_val
[11] = LM3530_DEF_ZT_1
; /* LM3530_ALS_Z1T_REG */
221 reg_val
[12] = LM3530_DEF_ZT_2
; /* LM3530_ALS_Z2T_REG */
222 reg_val
[13] = LM3530_DEF_ZT_3
; /* LM3530_ALS_Z3T_REG */
223 reg_val
[14] = LM3530_DEF_ZT_4
; /* LM3530_ALS_Z4T_REG */
225 if (!drvdata
->enable
) {
226 ret
= regulator_enable(drvdata
->regulator
);
228 dev_err(&drvdata
->client
->dev
,
229 "Enable regulator failed\n");
232 drvdata
->enable
= true;
235 for (i
= 0; i
< LM3530_REG_MAX
; i
++) {
236 ret
= i2c_smbus_write_byte_data(client
,
237 lm3530_reg
[i
], reg_val
[i
]);
245 static void lm3530_brightness_set(struct led_classdev
*led_cdev
,
246 enum led_brightness brt_val
)
249 struct lm3530_data
*drvdata
=
250 container_of(led_cdev
, struct lm3530_data
, led_dev
);
252 switch (drvdata
->mode
) {
253 case LM3530_BL_MODE_MANUAL
:
255 if (!drvdata
->enable
) {
256 err
= lm3530_init_registers(drvdata
);
258 dev_err(&drvdata
->client
->dev
,
259 "Register Init failed: %d\n", err
);
264 /* set the brightness in brightness control register*/
265 err
= i2c_smbus_write_byte_data(drvdata
->client
,
266 LM3530_BRT_CTRL_REG
, brt_val
/ 2);
268 dev_err(&drvdata
->client
->dev
,
269 "Unable to set brightness: %d\n", err
);
271 drvdata
->brightness
= brt_val
/ 2;
274 err
= regulator_disable(drvdata
->regulator
);
276 dev_err(&drvdata
->client
->dev
,
277 "Disable regulator failed\n");
278 drvdata
->enable
= false;
281 case LM3530_BL_MODE_ALS
:
283 case LM3530_BL_MODE_PWM
:
290 static ssize_t
lm3530_mode_get(struct device
*dev
,
291 struct device_attribute
*attr
, char *buf
)
293 struct i2c_client
*client
= container_of(
294 dev
->parent
, struct i2c_client
, dev
);
295 struct lm3530_data
*drvdata
= i2c_get_clientdata(client
);
298 for (i
= 0; i
< ARRAY_SIZE(mode_map
); i
++)
299 if (drvdata
->mode
== mode_map
[i
].mode_val
)
300 len
+= sprintf(buf
+ len
, "[%s] ", mode_map
[i
].mode
);
302 len
+= sprintf(buf
+ len
, "%s ", mode_map
[i
].mode
);
304 len
+= sprintf(buf
+ len
, "\n");
309 static ssize_t
lm3530_mode_set(struct device
*dev
, struct device_attribute
310 *attr
, const char *buf
, size_t size
)
313 struct i2c_client
*client
= container_of(
314 dev
->parent
, struct i2c_client
, dev
);
315 struct lm3530_data
*drvdata
= i2c_get_clientdata(client
);
318 mode
= lm3530_get_mode_from_str(buf
);
320 dev_err(dev
, "Invalid mode\n");
324 if (mode
== LM3530_BL_MODE_MANUAL
)
325 drvdata
->mode
= LM3530_BL_MODE_MANUAL
;
326 else if (mode
== LM3530_BL_MODE_ALS
)
327 drvdata
->mode
= LM3530_BL_MODE_ALS
;
328 else if (mode
== LM3530_BL_MODE_PWM
) {
329 dev_err(dev
, "PWM mode not supported\n");
333 err
= lm3530_init_registers(drvdata
);
335 dev_err(dev
, "Setting %s Mode failed :%d\n", buf
, err
);
339 return sizeof(drvdata
->mode
);
341 static DEVICE_ATTR(mode
, 0644, lm3530_mode_get
, lm3530_mode_set
);
343 static int __devinit
lm3530_probe(struct i2c_client
*client
,
344 const struct i2c_device_id
*id
)
346 struct lm3530_platform_data
*pdata
= client
->dev
.platform_data
;
347 struct lm3530_data
*drvdata
;
351 dev_err(&client
->dev
, "platform data required\n");
357 if (pdata
->mode
> LM3530_BL_MODE_PWM
) {
358 dev_err(&client
->dev
, "Illegal Mode request\n");
363 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
364 dev_err(&client
->dev
, "I2C_FUNC_I2C not supported\n");
369 drvdata
= kzalloc(sizeof(struct lm3530_data
), GFP_KERNEL
);
370 if (drvdata
== NULL
) {
375 drvdata
->mode
= pdata
->mode
;
376 drvdata
->client
= client
;
377 drvdata
->pdata
= pdata
;
378 drvdata
->brightness
= LED_OFF
;
379 drvdata
->enable
= false;
380 drvdata
->led_dev
.name
= LM3530_LED_DEV
;
381 drvdata
->led_dev
.brightness_set
= lm3530_brightness_set
;
383 i2c_set_clientdata(client
, drvdata
);
385 drvdata
->regulator
= regulator_get(&client
->dev
, "vin");
386 if (IS_ERR(drvdata
->regulator
)) {
387 dev_err(&client
->dev
, "regulator get failed\n");
388 err
= PTR_ERR(drvdata
->regulator
);
389 drvdata
->regulator
= NULL
;
390 goto err_regulator_get
;
393 if (drvdata
->pdata
->brt_val
) {
394 err
= lm3530_init_registers(drvdata
);
396 dev_err(&client
->dev
,
397 "Register Init failed: %d\n", err
);
402 err
= led_classdev_register(&client
->dev
, &drvdata
->led_dev
);
404 dev_err(&client
->dev
, "Register led class failed: %d\n", err
);
406 goto err_class_register
;
409 err
= device_create_file(drvdata
->led_dev
.dev
, &dev_attr_mode
);
411 dev_err(&client
->dev
, "File device creation failed: %d\n", err
);
413 goto err_create_file
;
419 led_classdev_unregister(&drvdata
->led_dev
);
422 regulator_put(drvdata
->regulator
);
424 i2c_set_clientdata(client
, NULL
);
430 static int __devexit
lm3530_remove(struct i2c_client
*client
)
432 struct lm3530_data
*drvdata
= i2c_get_clientdata(client
);
434 device_remove_file(drvdata
->led_dev
.dev
, &dev_attr_mode
);
437 regulator_disable(drvdata
->regulator
);
438 regulator_put(drvdata
->regulator
);
439 led_classdev_unregister(&drvdata
->led_dev
);
444 static const struct i2c_device_id lm3530_id
[] = {
448 MODULE_DEVICE_TABLE(i2c
, lm3530_id
);
450 static struct i2c_driver lm3530_i2c_driver
= {
451 .probe
= lm3530_probe
,
452 .remove
= lm3530_remove
,
453 .id_table
= lm3530_id
,
456 .owner
= THIS_MODULE
,
460 static int __init
lm3530_init(void)
462 return i2c_add_driver(&lm3530_i2c_driver
);
465 static void __exit
lm3530_exit(void)
467 i2c_del_driver(&lm3530_i2c_driver
);
470 module_init(lm3530_init
);
471 module_exit(lm3530_exit
);
473 MODULE_DESCRIPTION("Back Light driver for LM3530");
474 MODULE_LICENSE("GPL v2");
475 MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>");