1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 ST-Ericsson SA.
4 * Copyright (C) 2009 Motorola, Inc.
6 * Simple driver for National Semiconductor LM3530 Backlight driver chip
8 * Author: Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>
9 * based on leds-lm3530.c by Dan Murphy <D.Murphy@motorola.com>
12 #include <linux/i2c.h>
13 #include <linux/leds.h>
14 #include <linux/slab.h>
15 #include <linux/platform_device.h>
16 #include <linux/input.h>
17 #include <linux/led-lm3530.h>
18 #include <linux/types.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/module.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_IMP_SELECT 0x41
29 #define LM3530_BRT_CTRL_REG 0xA0
30 #define LM3530_ALS_ZB0_REG 0x60
31 #define LM3530_ALS_ZB1_REG 0x61
32 #define LM3530_ALS_ZB2_REG 0x62
33 #define LM3530_ALS_ZB3_REG 0x63
34 #define LM3530_ALS_Z0T_REG 0x70
35 #define LM3530_ALS_Z1T_REG 0x71
36 #define LM3530_ALS_Z2T_REG 0x72
37 #define LM3530_ALS_Z3T_REG 0x73
38 #define LM3530_ALS_Z4T_REG 0x74
39 #define LM3530_REG_MAX 14
41 /* General Control Register */
42 #define LM3530_EN_I2C_SHIFT (0)
43 #define LM3530_RAMP_LAW_SHIFT (1)
44 #define LM3530_MAX_CURR_SHIFT (2)
45 #define LM3530_EN_PWM_SHIFT (5)
46 #define LM3530_PWM_POL_SHIFT (6)
47 #define LM3530_EN_PWM_SIMPLE_SHIFT (7)
49 #define LM3530_ENABLE_I2C (1 << LM3530_EN_I2C_SHIFT)
50 #define LM3530_ENABLE_PWM (1 << LM3530_EN_PWM_SHIFT)
51 #define LM3530_POL_LOW (1 << LM3530_PWM_POL_SHIFT)
52 #define LM3530_ENABLE_PWM_SIMPLE (1 << LM3530_EN_PWM_SIMPLE_SHIFT)
54 /* ALS Config Register Options */
55 #define LM3530_ALS_AVG_TIME_SHIFT (0)
56 #define LM3530_EN_ALS_SHIFT (3)
57 #define LM3530_ALS_SEL_SHIFT (5)
59 #define LM3530_ENABLE_ALS (3 << LM3530_EN_ALS_SHIFT)
61 /* Brightness Ramp Rate Register */
62 #define LM3530_BRT_RAMP_FALL_SHIFT (0)
63 #define LM3530_BRT_RAMP_RISE_SHIFT (3)
65 /* ALS Resistor Select */
66 #define LM3530_ALS1_IMP_SHIFT (0)
67 #define LM3530_ALS2_IMP_SHIFT (4)
69 /* Zone Boundary Register defaults */
70 #define LM3530_ALS_ZB_MAX (4)
71 #define LM3530_ALS_WINDOW_mV (1000)
72 #define LM3530_ALS_OFFSET_mV (4)
74 /* Zone Target Register defaults */
75 #define LM3530_DEF_ZT_0 (0x7F)
76 #define LM3530_DEF_ZT_1 (0x66)
77 #define LM3530_DEF_ZT_2 (0x4C)
78 #define LM3530_DEF_ZT_3 (0x33)
79 #define LM3530_DEF_ZT_4 (0x19)
81 /* 7 bits are used for the brightness : LM3530_BRT_CTRL_REG */
82 #define MAX_BRIGHTNESS (127)
84 struct lm3530_mode_map
{
86 enum lm3530_mode mode_val
;
89 static struct lm3530_mode_map mode_map
[] = {
90 { "man", LM3530_BL_MODE_MANUAL
},
91 { "als", LM3530_BL_MODE_ALS
},
92 { "pwm", LM3530_BL_MODE_PWM
},
97 * @led_dev: led class device
99 * @pdata: LM3530 platform data
100 * @mode: mode of operation - manual, ALS, PWM
101 * @regulator: regulator
102 * @brightness: previous brightness value
103 * @enable: regulator is enabled
106 struct led_classdev led_dev
;
107 struct i2c_client
*client
;
108 struct lm3530_platform_data
*pdata
;
109 enum lm3530_mode mode
;
110 struct regulator
*regulator
;
111 enum led_brightness brightness
;
116 * struct lm3530_als_data
117 * @config : value of ALS configuration register
118 * @imp_sel : value of ALS resistor select register
119 * @zone : values of ALS ZB(Zone Boundary) registers
121 struct lm3530_als_data
{
124 u8 zones
[LM3530_ALS_ZB_MAX
];
127 static const u8 lm3530_reg
[LM3530_REG_MAX
] = {
130 LM3530_BRT_RAMP_RATE
,
131 LM3530_ALS_IMP_SELECT
,
144 static int lm3530_get_mode_from_str(const char *str
)
148 for (i
= 0; i
< ARRAY_SIZE(mode_map
); i
++)
149 if (sysfs_streq(str
, mode_map
[i
].mode
))
150 return mode_map
[i
].mode_val
;
155 static void lm3530_als_configure(struct lm3530_platform_data
*pdata
,
156 struct lm3530_als_data
*als
)
159 u32 als_vmin
, als_vmax
, als_vstep
;
161 if (pdata
->als_vmax
== 0) {
163 pdata
->als_vmax
= LM3530_ALS_WINDOW_mV
;
166 als_vmin
= pdata
->als_vmin
;
167 als_vmax
= pdata
->als_vmax
;
169 if ((als_vmax
- als_vmin
) > LM3530_ALS_WINDOW_mV
)
170 pdata
->als_vmax
= als_vmax
= als_vmin
+ LM3530_ALS_WINDOW_mV
;
172 /* n zone boundary makes n+1 zones */
173 als_vstep
= (als_vmax
- als_vmin
) / (LM3530_ALS_ZB_MAX
+ 1);
175 for (i
= 0; i
< LM3530_ALS_ZB_MAX
; i
++)
176 als
->zones
[i
] = (((als_vmin
+ LM3530_ALS_OFFSET_mV
) +
177 als_vstep
+ (i
* als_vstep
)) * LED_FULL
) / 1000;
180 (pdata
->als_avrg_time
<< LM3530_ALS_AVG_TIME_SHIFT
) |
181 (LM3530_ENABLE_ALS
) |
182 (pdata
->als_input_mode
<< LM3530_ALS_SEL_SHIFT
);
185 (pdata
->als1_resistor_sel
<< LM3530_ALS1_IMP_SHIFT
) |
186 (pdata
->als2_resistor_sel
<< LM3530_ALS2_IMP_SHIFT
);
189 static int lm3530_led_enable(struct lm3530_data
*drvdata
)
196 ret
= regulator_enable(drvdata
->regulator
);
198 dev_err(drvdata
->led_dev
.dev
, "Failed to enable vin:%d\n", ret
);
202 drvdata
->enable
= true;
206 static void lm3530_led_disable(struct lm3530_data
*drvdata
)
210 if (!drvdata
->enable
)
213 ret
= regulator_disable(drvdata
->regulator
);
215 dev_err(drvdata
->led_dev
.dev
, "Failed to disable vin:%d\n",
220 drvdata
->enable
= false;
223 static int lm3530_init_registers(struct lm3530_data
*drvdata
)
230 u8 reg_val
[LM3530_REG_MAX
];
231 struct lm3530_platform_data
*pdata
= drvdata
->pdata
;
232 struct i2c_client
*client
= drvdata
->client
;
233 struct lm3530_pwm_data
*pwm
= &pdata
->pwm_data
;
234 struct lm3530_als_data als
;
236 memset(&als
, 0, sizeof(struct lm3530_als_data
));
238 gen_config
= (pdata
->brt_ramp_law
<< LM3530_RAMP_LAW_SHIFT
) |
239 ((pdata
->max_current
& 7) << LM3530_MAX_CURR_SHIFT
);
241 switch (drvdata
->mode
) {
242 case LM3530_BL_MODE_MANUAL
:
243 gen_config
|= LM3530_ENABLE_I2C
;
245 case LM3530_BL_MODE_ALS
:
246 gen_config
|= LM3530_ENABLE_I2C
;
247 lm3530_als_configure(pdata
, &als
);
249 case LM3530_BL_MODE_PWM
:
250 gen_config
|= LM3530_ENABLE_PWM
| LM3530_ENABLE_PWM_SIMPLE
|
251 (pdata
->pwm_pol_hi
<< LM3530_PWM_POL_SHIFT
);
255 brt_ramp
= (pdata
->brt_ramp_fall
<< LM3530_BRT_RAMP_FALL_SHIFT
) |
256 (pdata
->brt_ramp_rise
<< LM3530_BRT_RAMP_RISE_SHIFT
);
258 if (drvdata
->brightness
)
259 brightness
= drvdata
->brightness
;
261 brightness
= drvdata
->brightness
= pdata
->brt_val
;
263 if (brightness
> drvdata
->led_dev
.max_brightness
)
264 brightness
= drvdata
->led_dev
.max_brightness
;
266 reg_val
[0] = gen_config
; /* LM3530_GEN_CONFIG */
267 reg_val
[1] = als
.config
; /* LM3530_ALS_CONFIG */
268 reg_val
[2] = brt_ramp
; /* LM3530_BRT_RAMP_RATE */
269 reg_val
[3] = als
.imp_sel
; /* LM3530_ALS_IMP_SELECT */
270 reg_val
[4] = brightness
; /* LM3530_BRT_CTRL_REG */
271 reg_val
[5] = als
.zones
[0]; /* LM3530_ALS_ZB0_REG */
272 reg_val
[6] = als
.zones
[1]; /* LM3530_ALS_ZB1_REG */
273 reg_val
[7] = als
.zones
[2]; /* LM3530_ALS_ZB2_REG */
274 reg_val
[8] = als
.zones
[3]; /* LM3530_ALS_ZB3_REG */
275 reg_val
[9] = LM3530_DEF_ZT_0
; /* LM3530_ALS_Z0T_REG */
276 reg_val
[10] = LM3530_DEF_ZT_1
; /* LM3530_ALS_Z1T_REG */
277 reg_val
[11] = LM3530_DEF_ZT_2
; /* LM3530_ALS_Z2T_REG */
278 reg_val
[12] = LM3530_DEF_ZT_3
; /* LM3530_ALS_Z3T_REG */
279 reg_val
[13] = LM3530_DEF_ZT_4
; /* LM3530_ALS_Z4T_REG */
281 ret
= lm3530_led_enable(drvdata
);
285 for (i
= 0; i
< LM3530_REG_MAX
; i
++) {
286 /* do not update brightness register when pwm mode */
287 if (lm3530_reg
[i
] == LM3530_BRT_CTRL_REG
&&
288 drvdata
->mode
== LM3530_BL_MODE_PWM
) {
289 if (pwm
->pwm_set_intensity
)
290 pwm
->pwm_set_intensity(reg_val
[i
],
291 drvdata
->led_dev
.max_brightness
);
295 ret
= i2c_smbus_write_byte_data(client
,
296 lm3530_reg
[i
], reg_val
[i
]);
304 static void lm3530_brightness_set(struct led_classdev
*led_cdev
,
305 enum led_brightness brt_val
)
308 struct lm3530_data
*drvdata
=
309 container_of(led_cdev
, struct lm3530_data
, led_dev
);
310 struct lm3530_platform_data
*pdata
= drvdata
->pdata
;
311 struct lm3530_pwm_data
*pwm
= &pdata
->pwm_data
;
312 u8 max_brightness
= led_cdev
->max_brightness
;
314 switch (drvdata
->mode
) {
315 case LM3530_BL_MODE_MANUAL
:
317 if (!drvdata
->enable
) {
318 err
= lm3530_init_registers(drvdata
);
320 dev_err(&drvdata
->client
->dev
,
321 "Register Init failed: %d\n", err
);
326 /* set the brightness in brightness control register*/
327 err
= i2c_smbus_write_byte_data(drvdata
->client
,
328 LM3530_BRT_CTRL_REG
, brt_val
);
330 dev_err(&drvdata
->client
->dev
,
331 "Unable to set brightness: %d\n", err
);
333 drvdata
->brightness
= brt_val
;
336 lm3530_led_disable(drvdata
);
338 case LM3530_BL_MODE_ALS
:
340 case LM3530_BL_MODE_PWM
:
341 if (pwm
->pwm_set_intensity
)
342 pwm
->pwm_set_intensity(brt_val
, max_brightness
);
349 static ssize_t
mode_show(struct device
*dev
,
350 struct device_attribute
*attr
, char *buf
)
352 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
353 struct lm3530_data
*drvdata
;
356 drvdata
= container_of(led_cdev
, struct lm3530_data
, led_dev
);
357 for (i
= 0; i
< ARRAY_SIZE(mode_map
); i
++)
358 if (drvdata
->mode
== mode_map
[i
].mode_val
)
359 len
+= sprintf(buf
+ len
, "[%s] ", mode_map
[i
].mode
);
361 len
+= sprintf(buf
+ len
, "%s ", mode_map
[i
].mode
);
363 len
+= sprintf(buf
+ len
, "\n");
368 static ssize_t
mode_store(struct device
*dev
, struct device_attribute
369 *attr
, const char *buf
, size_t size
)
371 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
372 struct lm3530_data
*drvdata
;
373 struct lm3530_pwm_data
*pwm
;
377 drvdata
= container_of(led_cdev
, struct lm3530_data
, led_dev
);
378 pwm
= &drvdata
->pdata
->pwm_data
;
379 max_brightness
= led_cdev
->max_brightness
;
380 mode
= lm3530_get_mode_from_str(buf
);
382 dev_err(dev
, "Invalid mode\n");
386 drvdata
->mode
= mode
;
388 /* set pwm to low if unnecessary */
389 if (mode
!= LM3530_BL_MODE_PWM
&& pwm
->pwm_set_intensity
)
390 pwm
->pwm_set_intensity(0, max_brightness
);
392 err
= lm3530_init_registers(drvdata
);
394 dev_err(dev
, "Setting %s Mode failed :%d\n", buf
, err
);
398 return sizeof(drvdata
->mode
);
400 static DEVICE_ATTR_RW(mode
);
402 static struct attribute
*lm3530_attrs
[] = {
406 ATTRIBUTE_GROUPS(lm3530
);
408 static int lm3530_probe(struct i2c_client
*client
)
410 struct lm3530_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
411 struct lm3530_data
*drvdata
;
415 dev_err(&client
->dev
, "platform data required\n");
420 if (pdata
->mode
> LM3530_BL_MODE_PWM
) {
421 dev_err(&client
->dev
, "Illegal Mode request\n");
425 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
426 dev_err(&client
->dev
, "I2C_FUNC_I2C not supported\n");
430 drvdata
= devm_kzalloc(&client
->dev
, sizeof(struct lm3530_data
),
435 drvdata
->mode
= pdata
->mode
;
436 drvdata
->client
= client
;
437 drvdata
->pdata
= pdata
;
438 drvdata
->brightness
= LED_OFF
;
439 drvdata
->enable
= false;
440 drvdata
->led_dev
.name
= LM3530_LED_DEV
;
441 drvdata
->led_dev
.brightness_set
= lm3530_brightness_set
;
442 drvdata
->led_dev
.max_brightness
= MAX_BRIGHTNESS
;
443 drvdata
->led_dev
.groups
= lm3530_groups
;
445 i2c_set_clientdata(client
, drvdata
);
447 drvdata
->regulator
= devm_regulator_get(&client
->dev
, "vin");
448 if (IS_ERR(drvdata
->regulator
)) {
449 dev_err(&client
->dev
, "regulator get failed\n");
450 err
= PTR_ERR(drvdata
->regulator
);
451 drvdata
->regulator
= NULL
;
455 if (drvdata
->pdata
->brt_val
) {
456 err
= lm3530_init_registers(drvdata
);
458 dev_err(&client
->dev
,
459 "Register Init failed: %d\n", err
);
463 err
= led_classdev_register(&client
->dev
, &drvdata
->led_dev
);
465 dev_err(&client
->dev
, "Register led class failed: %d\n", err
);
472 static void lm3530_remove(struct i2c_client
*client
)
474 struct lm3530_data
*drvdata
= i2c_get_clientdata(client
);
476 lm3530_led_disable(drvdata
);
477 led_classdev_unregister(&drvdata
->led_dev
);
480 static const struct i2c_device_id lm3530_id
[] = {
484 MODULE_DEVICE_TABLE(i2c
, lm3530_id
);
486 static struct i2c_driver lm3530_i2c_driver
= {
487 .probe
= lm3530_probe
,
488 .remove
= lm3530_remove
,
489 .id_table
= lm3530_id
,
495 module_i2c_driver(lm3530_i2c_driver
);
497 MODULE_DESCRIPTION("Back Light driver for LM3530");
498 MODULE_LICENSE("GPL v2");
499 MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>");