xref: /openbmc/linux/drivers/leds/leds-lm3530.c (revision bb982009d3850759d3f4a4c853f9c456c48b6c2d)
1b1e6b706SShreshtha Kumar Sahu /*
2b1e6b706SShreshtha Kumar Sahu  * Copyright (C) 2011 ST-Ericsson SA.
3b1e6b706SShreshtha Kumar Sahu  * Copyright (C) 2009 Motorola, Inc.
4b1e6b706SShreshtha Kumar Sahu  *
5b1e6b706SShreshtha Kumar Sahu  * License Terms: GNU General Public License v2
6b1e6b706SShreshtha Kumar Sahu  *
7b1e6b706SShreshtha Kumar Sahu  * Simple driver for National Semiconductor LM3530 Backlight driver chip
8b1e6b706SShreshtha Kumar Sahu  *
9b1e6b706SShreshtha Kumar Sahu  * Author: Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>
10b1e6b706SShreshtha Kumar Sahu  * based on leds-lm3530.c by Dan Murphy <D.Murphy@motorola.com>
11b1e6b706SShreshtha Kumar Sahu  */
12b1e6b706SShreshtha Kumar Sahu 
13b1e6b706SShreshtha Kumar Sahu #include <linux/i2c.h>
14b1e6b706SShreshtha Kumar Sahu #include <linux/leds.h>
15b1e6b706SShreshtha Kumar Sahu #include <linux/slab.h>
16b1e6b706SShreshtha Kumar Sahu #include <linux/platform_device.h>
17b1e6b706SShreshtha Kumar Sahu #include <linux/input.h>
18b1e6b706SShreshtha Kumar Sahu #include <linux/led-lm3530.h>
19b1e6b706SShreshtha Kumar Sahu #include <linux/types.h>
209b2da53fSShreshtha Kumar Sahu #include <linux/regulator/consumer.h>
2154f4dedbSPaul Gortmaker #include <linux/module.h>
22b1e6b706SShreshtha Kumar Sahu 
23b1e6b706SShreshtha Kumar Sahu #define LM3530_LED_DEV "lcd-backlight"
24b1e6b706SShreshtha Kumar Sahu #define LM3530_NAME "lm3530-led"
25b1e6b706SShreshtha Kumar Sahu 
26b1e6b706SShreshtha Kumar Sahu #define LM3530_GEN_CONFIG		0x10
27b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_CONFIG		0x20
28b1e6b706SShreshtha Kumar Sahu #define LM3530_BRT_RAMP_RATE		0x30
29b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_ZONE_REG		0x40
30b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_IMP_SELECT		0x41
31b1e6b706SShreshtha Kumar Sahu #define LM3530_BRT_CTRL_REG		0xA0
32b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_ZB0_REG		0x60
33b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_ZB1_REG		0x61
34b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_ZB2_REG		0x62
35b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_ZB3_REG		0x63
36b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_Z0T_REG		0x70
37b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_Z1T_REG		0x71
38b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_Z2T_REG		0x72
39b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_Z3T_REG		0x73
40b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_Z4T_REG		0x74
41b1e6b706SShreshtha Kumar Sahu #define LM3530_REG_MAX			15
42b1e6b706SShreshtha Kumar Sahu 
43b1e6b706SShreshtha Kumar Sahu /* General Control Register */
44b1e6b706SShreshtha Kumar Sahu #define LM3530_EN_I2C_SHIFT		(0)
45b1e6b706SShreshtha Kumar Sahu #define LM3530_RAMP_LAW_SHIFT		(1)
46b1e6b706SShreshtha Kumar Sahu #define LM3530_MAX_CURR_SHIFT		(2)
47b1e6b706SShreshtha Kumar Sahu #define LM3530_EN_PWM_SHIFT		(5)
48b1e6b706SShreshtha Kumar Sahu #define LM3530_PWM_POL_SHIFT		(6)
49b1e6b706SShreshtha Kumar Sahu #define LM3530_EN_PWM_SIMPLE_SHIFT	(7)
50b1e6b706SShreshtha Kumar Sahu 
51b1e6b706SShreshtha Kumar Sahu #define LM3530_ENABLE_I2C		(1 << LM3530_EN_I2C_SHIFT)
52b1e6b706SShreshtha Kumar Sahu #define LM3530_ENABLE_PWM		(1 << LM3530_EN_PWM_SHIFT)
53b1e6b706SShreshtha Kumar Sahu #define LM3530_POL_LOW			(1 << LM3530_PWM_POL_SHIFT)
54b1e6b706SShreshtha Kumar Sahu #define LM3530_ENABLE_PWM_SIMPLE	(1 << LM3530_EN_PWM_SIMPLE_SHIFT)
55b1e6b706SShreshtha Kumar Sahu 
56b1e6b706SShreshtha Kumar Sahu /* ALS Config Register Options */
57b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_AVG_TIME_SHIFT	(0)
58b1e6b706SShreshtha Kumar Sahu #define LM3530_EN_ALS_SHIFT		(3)
59b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS_SEL_SHIFT		(5)
60b1e6b706SShreshtha Kumar Sahu 
61b1e6b706SShreshtha Kumar Sahu #define LM3530_ENABLE_ALS		(3 << LM3530_EN_ALS_SHIFT)
62b1e6b706SShreshtha Kumar Sahu 
63b1e6b706SShreshtha Kumar Sahu /* Brightness Ramp Rate Register */
64b1e6b706SShreshtha Kumar Sahu #define LM3530_BRT_RAMP_FALL_SHIFT	(0)
65b1e6b706SShreshtha Kumar Sahu #define LM3530_BRT_RAMP_RISE_SHIFT	(3)
66b1e6b706SShreshtha Kumar Sahu 
67b1e6b706SShreshtha Kumar Sahu /* ALS Resistor Select */
68b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS1_IMP_SHIFT		(0)
69b1e6b706SShreshtha Kumar Sahu #define LM3530_ALS2_IMP_SHIFT		(4)
70b1e6b706SShreshtha Kumar Sahu 
71b1e6b706SShreshtha Kumar Sahu /* Zone Boundary Register defaults */
7240b1445bSShreshtha Kumar Sahu #define LM3530_ALS_ZB_MAX		(4)
7340b1445bSShreshtha Kumar Sahu #define LM3530_ALS_WINDOW_mV		(1000)
7440b1445bSShreshtha Kumar Sahu #define LM3530_ALS_OFFSET_mV		(4)
75b1e6b706SShreshtha Kumar Sahu 
76b1e6b706SShreshtha Kumar Sahu /* Zone Target Register defaults */
7740b1445bSShreshtha Kumar Sahu #define LM3530_DEF_ZT_0			(0x7F)
7840b1445bSShreshtha Kumar Sahu #define LM3530_DEF_ZT_1			(0x66)
79b1e6b706SShreshtha Kumar Sahu #define LM3530_DEF_ZT_2			(0x4C)
8040b1445bSShreshtha Kumar Sahu #define LM3530_DEF_ZT_3			(0x33)
8140b1445bSShreshtha Kumar Sahu #define LM3530_DEF_ZT_4			(0x19)
82b1e6b706SShreshtha Kumar Sahu 
83bbe64532SKim, Milo /* 7 bits are used for the brightness : LM3530_BRT_CTRL_REG */
84bbe64532SKim, Milo #define MAX_BRIGHTNESS			(127)
85bbe64532SKim, Milo 
86b1e6b706SShreshtha Kumar Sahu struct lm3530_mode_map {
87b1e6b706SShreshtha Kumar Sahu 	const char *mode;
88b1e6b706SShreshtha Kumar Sahu 	enum lm3530_mode mode_val;
89b1e6b706SShreshtha Kumar Sahu };
90b1e6b706SShreshtha Kumar Sahu 
91b1e6b706SShreshtha Kumar Sahu static struct lm3530_mode_map mode_map[] = {
92b1e6b706SShreshtha Kumar Sahu 	{ "man", LM3530_BL_MODE_MANUAL },
93b1e6b706SShreshtha Kumar Sahu 	{ "als", LM3530_BL_MODE_ALS },
94b1e6b706SShreshtha Kumar Sahu 	{ "pwm", LM3530_BL_MODE_PWM },
95b1e6b706SShreshtha Kumar Sahu };
96b1e6b706SShreshtha Kumar Sahu 
97b1e6b706SShreshtha Kumar Sahu /**
98b1e6b706SShreshtha Kumar Sahu  * struct lm3530_data
99b1e6b706SShreshtha Kumar Sahu  * @led_dev: led class device
100b1e6b706SShreshtha Kumar Sahu  * @client: i2c client
101b1e6b706SShreshtha Kumar Sahu  * @pdata: LM3530 platform data
102b1e6b706SShreshtha Kumar Sahu  * @mode: mode of operation - manual, ALS, PWM
1039b2da53fSShreshtha Kumar Sahu  * @regulator: regulator
1049b2da53fSShreshtha Kumar Sahu  * @brighness: previous brightness value
1059b2da53fSShreshtha Kumar Sahu  * @enable: regulator is enabled
106b1e6b706SShreshtha Kumar Sahu  */
107b1e6b706SShreshtha Kumar Sahu struct lm3530_data {
108b1e6b706SShreshtha Kumar Sahu 	struct led_classdev led_dev;
109b1e6b706SShreshtha Kumar Sahu 	struct i2c_client *client;
110b1e6b706SShreshtha Kumar Sahu 	struct lm3530_platform_data *pdata;
111b1e6b706SShreshtha Kumar Sahu 	enum lm3530_mode mode;
1129b2da53fSShreshtha Kumar Sahu 	struct regulator *regulator;
1139b2da53fSShreshtha Kumar Sahu 	enum led_brightness brightness;
1149b2da53fSShreshtha Kumar Sahu 	bool enable;
115b1e6b706SShreshtha Kumar Sahu };
116b1e6b706SShreshtha Kumar Sahu 
117b1e6b706SShreshtha Kumar Sahu static const u8 lm3530_reg[LM3530_REG_MAX] = {
118b1e6b706SShreshtha Kumar Sahu 	LM3530_GEN_CONFIG,
119b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_CONFIG,
120b1e6b706SShreshtha Kumar Sahu 	LM3530_BRT_RAMP_RATE,
121b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_ZONE_REG,
122b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_IMP_SELECT,
123b1e6b706SShreshtha Kumar Sahu 	LM3530_BRT_CTRL_REG,
124b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_ZB0_REG,
125b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_ZB1_REG,
126b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_ZB2_REG,
127b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_ZB3_REG,
128b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_Z0T_REG,
129b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_Z1T_REG,
130b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_Z2T_REG,
131b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_Z3T_REG,
132b1e6b706SShreshtha Kumar Sahu 	LM3530_ALS_Z4T_REG,
133b1e6b706SShreshtha Kumar Sahu };
134b1e6b706SShreshtha Kumar Sahu 
135b1e6b706SShreshtha Kumar Sahu static int lm3530_get_mode_from_str(const char *str)
136b1e6b706SShreshtha Kumar Sahu {
137b1e6b706SShreshtha Kumar Sahu 	int i;
138b1e6b706SShreshtha Kumar Sahu 
139b1e6b706SShreshtha Kumar Sahu 	for (i = 0; i < ARRAY_SIZE(mode_map); i++)
140b1e6b706SShreshtha Kumar Sahu 		if (sysfs_streq(str, mode_map[i].mode))
141b1e6b706SShreshtha Kumar Sahu 			return mode_map[i].mode_val;
142b1e6b706SShreshtha Kumar Sahu 
143b1e6b706SShreshtha Kumar Sahu 	return -1;
144b1e6b706SShreshtha Kumar Sahu }
145b1e6b706SShreshtha Kumar Sahu 
146b1e6b706SShreshtha Kumar Sahu static int lm3530_init_registers(struct lm3530_data *drvdata)
147b1e6b706SShreshtha Kumar Sahu {
148b1e6b706SShreshtha Kumar Sahu 	int ret = 0;
149b1e6b706SShreshtha Kumar Sahu 	int i;
150b1e6b706SShreshtha Kumar Sahu 	u8 gen_config;
151b1e6b706SShreshtha Kumar Sahu 	u8 als_config = 0;
152b1e6b706SShreshtha Kumar Sahu 	u8 brt_ramp;
153b1e6b706SShreshtha Kumar Sahu 	u8 als_imp_sel = 0;
154b1e6b706SShreshtha Kumar Sahu 	u8 brightness;
155b1e6b706SShreshtha Kumar Sahu 	u8 reg_val[LM3530_REG_MAX];
15640b1445bSShreshtha Kumar Sahu 	u8 zones[LM3530_ALS_ZB_MAX];
15740b1445bSShreshtha Kumar Sahu 	u32 als_vmin, als_vmax, als_vstep;
158b1e6b706SShreshtha Kumar Sahu 	struct lm3530_platform_data *pltfm = drvdata->pdata;
159b1e6b706SShreshtha Kumar Sahu 	struct i2c_client *client = drvdata->client;
160*bb982009SKim, Milo 	struct lm3530_pwm_data *pwm = &pltfm->pwm_data;
161b1e6b706SShreshtha Kumar Sahu 
162b1e6b706SShreshtha Kumar Sahu 	gen_config = (pltfm->brt_ramp_law << LM3530_RAMP_LAW_SHIFT) |
163b1e6b706SShreshtha Kumar Sahu 			((pltfm->max_current & 7) << LM3530_MAX_CURR_SHIFT);
164b1e6b706SShreshtha Kumar Sahu 
165b1e6b706SShreshtha Kumar Sahu 	if (drvdata->mode == LM3530_BL_MODE_MANUAL ||
166b1e6b706SShreshtha Kumar Sahu 	    drvdata->mode == LM3530_BL_MODE_ALS)
167b1e6b706SShreshtha Kumar Sahu 		gen_config |= (LM3530_ENABLE_I2C);
168b1e6b706SShreshtha Kumar Sahu 
169b1e6b706SShreshtha Kumar Sahu 	if (drvdata->mode == LM3530_BL_MODE_ALS) {
17040b1445bSShreshtha Kumar Sahu 		if (pltfm->als_vmax == 0) {
171ec44fd42SAxel Lin 			pltfm->als_vmin = 0;
172ec44fd42SAxel Lin 			pltfm->als_vmax = LM3530_ALS_WINDOW_mV;
17340b1445bSShreshtha Kumar Sahu 		}
17440b1445bSShreshtha Kumar Sahu 
17540b1445bSShreshtha Kumar Sahu 		als_vmin = pltfm->als_vmin;
17640b1445bSShreshtha Kumar Sahu 		als_vmax = pltfm->als_vmax;
17740b1445bSShreshtha Kumar Sahu 
17840b1445bSShreshtha Kumar Sahu 		if ((als_vmax - als_vmin) > LM3530_ALS_WINDOW_mV)
17940b1445bSShreshtha Kumar Sahu 			pltfm->als_vmax = als_vmax =
18040b1445bSShreshtha Kumar Sahu 				als_vmin + LM3530_ALS_WINDOW_mV;
18140b1445bSShreshtha Kumar Sahu 
18240b1445bSShreshtha Kumar Sahu 		/* n zone boundary makes n+1 zones */
18340b1445bSShreshtha Kumar Sahu 		als_vstep = (als_vmax - als_vmin) / (LM3530_ALS_ZB_MAX + 1);
18440b1445bSShreshtha Kumar Sahu 
18540b1445bSShreshtha Kumar Sahu 		for (i = 0; i < LM3530_ALS_ZB_MAX; i++)
18640b1445bSShreshtha Kumar Sahu 			zones[i] = (((als_vmin + LM3530_ALS_OFFSET_mV) +
18740b1445bSShreshtha Kumar Sahu 					als_vstep + (i * als_vstep)) * LED_FULL)
18840b1445bSShreshtha Kumar Sahu 					/ 1000;
18940b1445bSShreshtha Kumar Sahu 
190b1e6b706SShreshtha Kumar Sahu 		als_config =
191b1e6b706SShreshtha Kumar Sahu 			(pltfm->als_avrg_time << LM3530_ALS_AVG_TIME_SHIFT) |
192b1e6b706SShreshtha Kumar Sahu 			(LM3530_ENABLE_ALS) |
193b1e6b706SShreshtha Kumar Sahu 			(pltfm->als_input_mode << LM3530_ALS_SEL_SHIFT);
194b1e6b706SShreshtha Kumar Sahu 
195b1e6b706SShreshtha Kumar Sahu 		als_imp_sel =
196b1e6b706SShreshtha Kumar Sahu 			(pltfm->als1_resistor_sel << LM3530_ALS1_IMP_SHIFT) |
197b1e6b706SShreshtha Kumar Sahu 			(pltfm->als2_resistor_sel << LM3530_ALS2_IMP_SHIFT);
19840b1445bSShreshtha Kumar Sahu 
199b1e6b706SShreshtha Kumar Sahu 	}
200b1e6b706SShreshtha Kumar Sahu 
201b1e6b706SShreshtha Kumar Sahu 	if (drvdata->mode == LM3530_BL_MODE_PWM)
202b1e6b706SShreshtha Kumar Sahu 		gen_config |= (LM3530_ENABLE_PWM) |
203b1e6b706SShreshtha Kumar Sahu 				(pltfm->pwm_pol_hi << LM3530_PWM_POL_SHIFT) |
204b1e6b706SShreshtha Kumar Sahu 				(LM3530_ENABLE_PWM_SIMPLE);
205b1e6b706SShreshtha Kumar Sahu 
206b1e6b706SShreshtha Kumar Sahu 	brt_ramp = (pltfm->brt_ramp_fall << LM3530_BRT_RAMP_FALL_SHIFT) |
207b1e6b706SShreshtha Kumar Sahu 			(pltfm->brt_ramp_rise << LM3530_BRT_RAMP_RISE_SHIFT);
208b1e6b706SShreshtha Kumar Sahu 
2099b2da53fSShreshtha Kumar Sahu 	if (drvdata->brightness)
2109b2da53fSShreshtha Kumar Sahu 		brightness = drvdata->brightness;
2119b2da53fSShreshtha Kumar Sahu 	else
2129b2da53fSShreshtha Kumar Sahu 		brightness = drvdata->brightness = pltfm->brt_val;
213b1e6b706SShreshtha Kumar Sahu 
214bbe64532SKim, Milo 	if (brightness > drvdata->led_dev.max_brightness)
215bbe64532SKim, Milo 		brightness = drvdata->led_dev.max_brightness;
216bbe64532SKim, Milo 
217b1e6b706SShreshtha Kumar Sahu 	reg_val[0] = gen_config;	/* LM3530_GEN_CONFIG */
218b1e6b706SShreshtha Kumar Sahu 	reg_val[1] = als_config;	/* LM3530_ALS_CONFIG */
219b1e6b706SShreshtha Kumar Sahu 	reg_val[2] = brt_ramp;		/* LM3530_BRT_RAMP_RATE */
220b1e6b706SShreshtha Kumar Sahu 	reg_val[3] = 0x00;		/* LM3530_ALS_ZONE_REG */
221b1e6b706SShreshtha Kumar Sahu 	reg_val[4] = als_imp_sel;	/* LM3530_ALS_IMP_SELECT */
222b1e6b706SShreshtha Kumar Sahu 	reg_val[5] = brightness;	/* LM3530_BRT_CTRL_REG */
22340b1445bSShreshtha Kumar Sahu 	reg_val[6] = zones[0];		/* LM3530_ALS_ZB0_REG */
22440b1445bSShreshtha Kumar Sahu 	reg_val[7] = zones[1];		/* LM3530_ALS_ZB1_REG */
22540b1445bSShreshtha Kumar Sahu 	reg_val[8] = zones[2];		/* LM3530_ALS_ZB2_REG */
22640b1445bSShreshtha Kumar Sahu 	reg_val[9] = zones[3];		/* LM3530_ALS_ZB3_REG */
227b1e6b706SShreshtha Kumar Sahu 	reg_val[10] = LM3530_DEF_ZT_0;	/* LM3530_ALS_Z0T_REG */
228b1e6b706SShreshtha Kumar Sahu 	reg_val[11] = LM3530_DEF_ZT_1;	/* LM3530_ALS_Z1T_REG */
229b1e6b706SShreshtha Kumar Sahu 	reg_val[12] = LM3530_DEF_ZT_2;	/* LM3530_ALS_Z2T_REG */
230b1e6b706SShreshtha Kumar Sahu 	reg_val[13] = LM3530_DEF_ZT_3;	/* LM3530_ALS_Z3T_REG */
231b1e6b706SShreshtha Kumar Sahu 	reg_val[14] = LM3530_DEF_ZT_4;	/* LM3530_ALS_Z4T_REG */
232b1e6b706SShreshtha Kumar Sahu 
2339b2da53fSShreshtha Kumar Sahu 	if (!drvdata->enable) {
2349b2da53fSShreshtha Kumar Sahu 		ret = regulator_enable(drvdata->regulator);
2359b2da53fSShreshtha Kumar Sahu 		if (ret) {
2369b2da53fSShreshtha Kumar Sahu 			dev_err(&drvdata->client->dev,
2379b2da53fSShreshtha Kumar Sahu 					"Enable regulator failed\n");
2389b2da53fSShreshtha Kumar Sahu 			return ret;
2399b2da53fSShreshtha Kumar Sahu 		}
2409b2da53fSShreshtha Kumar Sahu 		drvdata->enable = true;
2419b2da53fSShreshtha Kumar Sahu 	}
2429b2da53fSShreshtha Kumar Sahu 
243b1e6b706SShreshtha Kumar Sahu 	for (i = 0; i < LM3530_REG_MAX; i++) {
244*bb982009SKim, Milo 		/* do not update brightness register when pwm mode */
245*bb982009SKim, Milo 		if (lm3530_reg[i] == LM3530_BRT_CTRL_REG &&
246*bb982009SKim, Milo 		    drvdata->mode == LM3530_BL_MODE_PWM) {
247*bb982009SKim, Milo 			if (pwm->pwm_set_intensity)
248*bb982009SKim, Milo 				pwm->pwm_set_intensity(reg_val[i],
249*bb982009SKim, Milo 					drvdata->led_dev.max_brightness);
250*bb982009SKim, Milo 			continue;
251*bb982009SKim, Milo 		}
252*bb982009SKim, Milo 
253b1e6b706SShreshtha Kumar Sahu 		ret = i2c_smbus_write_byte_data(client,
254b1e6b706SShreshtha Kumar Sahu 				lm3530_reg[i], reg_val[i]);
255b1e6b706SShreshtha Kumar Sahu 		if (ret)
256b1e6b706SShreshtha Kumar Sahu 			break;
257b1e6b706SShreshtha Kumar Sahu 	}
258b1e6b706SShreshtha Kumar Sahu 
259b1e6b706SShreshtha Kumar Sahu 	return ret;
260b1e6b706SShreshtha Kumar Sahu }
261b1e6b706SShreshtha Kumar Sahu 
262b1e6b706SShreshtha Kumar Sahu static void lm3530_brightness_set(struct led_classdev *led_cdev,
263b1e6b706SShreshtha Kumar Sahu 				     enum led_brightness brt_val)
264b1e6b706SShreshtha Kumar Sahu {
265b1e6b706SShreshtha Kumar Sahu 	int err;
266b1e6b706SShreshtha Kumar Sahu 	struct lm3530_data *drvdata =
267b1e6b706SShreshtha Kumar Sahu 	    container_of(led_cdev, struct lm3530_data, led_dev);
268*bb982009SKim, Milo 	struct lm3530_platform_data *pdata = drvdata->pdata;
269*bb982009SKim, Milo 	struct lm3530_pwm_data *pwm = &pdata->pwm_data;
270*bb982009SKim, Milo 	u8 max_brightness = led_cdev->max_brightness;
271b1e6b706SShreshtha Kumar Sahu 
272b1e6b706SShreshtha Kumar Sahu 	switch (drvdata->mode) {
273b1e6b706SShreshtha Kumar Sahu 	case LM3530_BL_MODE_MANUAL:
274b1e6b706SShreshtha Kumar Sahu 
2759b2da53fSShreshtha Kumar Sahu 		if (!drvdata->enable) {
2769b2da53fSShreshtha Kumar Sahu 			err = lm3530_init_registers(drvdata);
2779b2da53fSShreshtha Kumar Sahu 			if (err) {
2789b2da53fSShreshtha Kumar Sahu 				dev_err(&drvdata->client->dev,
2799b2da53fSShreshtha Kumar Sahu 					"Register Init failed: %d\n", err);
2809b2da53fSShreshtha Kumar Sahu 				break;
2819b2da53fSShreshtha Kumar Sahu 			}
2829b2da53fSShreshtha Kumar Sahu 		}
2839b2da53fSShreshtha Kumar Sahu 
284b1e6b706SShreshtha Kumar Sahu 		/* set the brightness in brightness control register*/
285b1e6b706SShreshtha Kumar Sahu 		err = i2c_smbus_write_byte_data(drvdata->client,
286bbe64532SKim, Milo 				LM3530_BRT_CTRL_REG, brt_val);
287b1e6b706SShreshtha Kumar Sahu 		if (err)
288b1e6b706SShreshtha Kumar Sahu 			dev_err(&drvdata->client->dev,
289b1e6b706SShreshtha Kumar Sahu 				"Unable to set brightness: %d\n", err);
2909b2da53fSShreshtha Kumar Sahu 		else
291bbe64532SKim, Milo 			drvdata->brightness = brt_val;
2929b2da53fSShreshtha Kumar Sahu 
2939b2da53fSShreshtha Kumar Sahu 		if (brt_val == 0) {
2949b2da53fSShreshtha Kumar Sahu 			err = regulator_disable(drvdata->regulator);
2959b2da53fSShreshtha Kumar Sahu 			if (err)
2969b2da53fSShreshtha Kumar Sahu 				dev_err(&drvdata->client->dev,
2979b2da53fSShreshtha Kumar Sahu 					"Disable regulator failed\n");
2989b2da53fSShreshtha Kumar Sahu 			drvdata->enable = false;
2999b2da53fSShreshtha Kumar Sahu 		}
300b1e6b706SShreshtha Kumar Sahu 		break;
301b1e6b706SShreshtha Kumar Sahu 	case LM3530_BL_MODE_ALS:
302b1e6b706SShreshtha Kumar Sahu 		break;
303b1e6b706SShreshtha Kumar Sahu 	case LM3530_BL_MODE_PWM:
304*bb982009SKim, Milo 		if (pwm->pwm_set_intensity)
305*bb982009SKim, Milo 			pwm->pwm_set_intensity(brt_val, max_brightness);
306b1e6b706SShreshtha Kumar Sahu 		break;
307b1e6b706SShreshtha Kumar Sahu 	default:
308b1e6b706SShreshtha Kumar Sahu 		break;
309b1e6b706SShreshtha Kumar Sahu 	}
310b1e6b706SShreshtha Kumar Sahu }
311b1e6b706SShreshtha Kumar Sahu 
31240b1445bSShreshtha Kumar Sahu static ssize_t lm3530_mode_get(struct device *dev,
31340b1445bSShreshtha Kumar Sahu 		struct device_attribute *attr, char *buf)
31440b1445bSShreshtha Kumar Sahu {
315e13d9786SKim, Milo 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
316e13d9786SKim, Milo 	struct lm3530_data *drvdata;
31740b1445bSShreshtha Kumar Sahu 	int i, len = 0;
31840b1445bSShreshtha Kumar Sahu 
319e13d9786SKim, Milo 	drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
32040b1445bSShreshtha Kumar Sahu 	for (i = 0; i < ARRAY_SIZE(mode_map); i++)
32140b1445bSShreshtha Kumar Sahu 		if (drvdata->mode == mode_map[i].mode_val)
32240b1445bSShreshtha Kumar Sahu 			len += sprintf(buf + len, "[%s] ", mode_map[i].mode);
32340b1445bSShreshtha Kumar Sahu 		else
32440b1445bSShreshtha Kumar Sahu 			len += sprintf(buf + len, "%s ", mode_map[i].mode);
32540b1445bSShreshtha Kumar Sahu 
32640b1445bSShreshtha Kumar Sahu 	len += sprintf(buf + len, "\n");
32740b1445bSShreshtha Kumar Sahu 
32840b1445bSShreshtha Kumar Sahu 	return len;
32940b1445bSShreshtha Kumar Sahu }
330b1e6b706SShreshtha Kumar Sahu 
331b1e6b706SShreshtha Kumar Sahu static ssize_t lm3530_mode_set(struct device *dev, struct device_attribute
332b1e6b706SShreshtha Kumar Sahu 				   *attr, const char *buf, size_t size)
333b1e6b706SShreshtha Kumar Sahu {
334e13d9786SKim, Milo 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
335e13d9786SKim, Milo 	struct lm3530_data *drvdata;
336*bb982009SKim, Milo 	struct lm3530_pwm_data *pwm;
337*bb982009SKim, Milo 	u8 max_brightness;
338e13d9786SKim, Milo 	int mode, err;
339b1e6b706SShreshtha Kumar Sahu 
340e13d9786SKim, Milo 	drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
341*bb982009SKim, Milo 	pwm = &drvdata->pdata->pwm_data;
342*bb982009SKim, Milo 	max_brightness = led_cdev->max_brightness;
343b1e6b706SShreshtha Kumar Sahu 	mode = lm3530_get_mode_from_str(buf);
344b1e6b706SShreshtha Kumar Sahu 	if (mode < 0) {
345b1e6b706SShreshtha Kumar Sahu 		dev_err(dev, "Invalid mode\n");
346b1e6b706SShreshtha Kumar Sahu 		return -EINVAL;
347b1e6b706SShreshtha Kumar Sahu 	}
348b1e6b706SShreshtha Kumar Sahu 
349*bb982009SKim, Milo 	drvdata->mode = mode;
350*bb982009SKim, Milo 
351*bb982009SKim, Milo 	/* set pwm to low if unnecessary */
352*bb982009SKim, Milo 	if (mode != LM3530_BL_MODE_PWM && pwm->pwm_set_intensity)
353*bb982009SKim, Milo 		pwm->pwm_set_intensity(0, max_brightness);
354b1e6b706SShreshtha Kumar Sahu 
355b1e6b706SShreshtha Kumar Sahu 	err = lm3530_init_registers(drvdata);
356b1e6b706SShreshtha Kumar Sahu 	if (err) {
357b1e6b706SShreshtha Kumar Sahu 		dev_err(dev, "Setting %s Mode failed :%d\n", buf, err);
358b1e6b706SShreshtha Kumar Sahu 		return err;
359b1e6b706SShreshtha Kumar Sahu 	}
360b1e6b706SShreshtha Kumar Sahu 
361b1e6b706SShreshtha Kumar Sahu 	return sizeof(drvdata->mode);
362b1e6b706SShreshtha Kumar Sahu }
36340b1445bSShreshtha Kumar Sahu static DEVICE_ATTR(mode, 0644, lm3530_mode_get, lm3530_mode_set);
364b1e6b706SShreshtha Kumar Sahu 
365b1e6b706SShreshtha Kumar Sahu static int __devinit lm3530_probe(struct i2c_client *client,
366b1e6b706SShreshtha Kumar Sahu 			   const struct i2c_device_id *id)
367b1e6b706SShreshtha Kumar Sahu {
368b1e6b706SShreshtha Kumar Sahu 	struct lm3530_platform_data *pdata = client->dev.platform_data;
369b1e6b706SShreshtha Kumar Sahu 	struct lm3530_data *drvdata;
370b1e6b706SShreshtha Kumar Sahu 	int err = 0;
371b1e6b706SShreshtha Kumar Sahu 
372b1e6b706SShreshtha Kumar Sahu 	if (pdata == NULL) {
373b1e6b706SShreshtha Kumar Sahu 		dev_err(&client->dev, "platform data required\n");
374b1e6b706SShreshtha Kumar Sahu 		err = -ENODEV;
375b1e6b706SShreshtha Kumar Sahu 		goto err_out;
376b1e6b706SShreshtha Kumar Sahu 	}
377b1e6b706SShreshtha Kumar Sahu 
378b1e6b706SShreshtha Kumar Sahu 	/* BL mode */
379b1e6b706SShreshtha Kumar Sahu 	if (pdata->mode > LM3530_BL_MODE_PWM) {
380b1e6b706SShreshtha Kumar Sahu 		dev_err(&client->dev, "Illegal Mode request\n");
381b1e6b706SShreshtha Kumar Sahu 		err = -EINVAL;
382b1e6b706SShreshtha Kumar Sahu 		goto err_out;
383b1e6b706SShreshtha Kumar Sahu 	}
384b1e6b706SShreshtha Kumar Sahu 
385b1e6b706SShreshtha Kumar Sahu 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
386b1e6b706SShreshtha Kumar Sahu 		dev_err(&client->dev, "I2C_FUNC_I2C not supported\n");
387b1e6b706SShreshtha Kumar Sahu 		err = -EIO;
388b1e6b706SShreshtha Kumar Sahu 		goto err_out;
389b1e6b706SShreshtha Kumar Sahu 	}
390b1e6b706SShreshtha Kumar Sahu 
391b1e6b706SShreshtha Kumar Sahu 	drvdata = kzalloc(sizeof(struct lm3530_data), GFP_KERNEL);
392b1e6b706SShreshtha Kumar Sahu 	if (drvdata == NULL) {
393b1e6b706SShreshtha Kumar Sahu 		err = -ENOMEM;
394b1e6b706SShreshtha Kumar Sahu 		goto err_out;
395b1e6b706SShreshtha Kumar Sahu 	}
396b1e6b706SShreshtha Kumar Sahu 
397b1e6b706SShreshtha Kumar Sahu 	drvdata->mode = pdata->mode;
398b1e6b706SShreshtha Kumar Sahu 	drvdata->client = client;
399b1e6b706SShreshtha Kumar Sahu 	drvdata->pdata = pdata;
4009b2da53fSShreshtha Kumar Sahu 	drvdata->brightness = LED_OFF;
4019b2da53fSShreshtha Kumar Sahu 	drvdata->enable = false;
402b1e6b706SShreshtha Kumar Sahu 	drvdata->led_dev.name = LM3530_LED_DEV;
403b1e6b706SShreshtha Kumar Sahu 	drvdata->led_dev.brightness_set = lm3530_brightness_set;
404bbe64532SKim, Milo 	drvdata->led_dev.max_brightness = MAX_BRIGHTNESS;
405b1e6b706SShreshtha Kumar Sahu 
406b1e6b706SShreshtha Kumar Sahu 	i2c_set_clientdata(client, drvdata);
407b1e6b706SShreshtha Kumar Sahu 
4089b2da53fSShreshtha Kumar Sahu 	drvdata->regulator = regulator_get(&client->dev, "vin");
4099b2da53fSShreshtha Kumar Sahu 	if (IS_ERR(drvdata->regulator)) {
4109b2da53fSShreshtha Kumar Sahu 		dev_err(&client->dev, "regulator get failed\n");
4119b2da53fSShreshtha Kumar Sahu 		err = PTR_ERR(drvdata->regulator);
4129b2da53fSShreshtha Kumar Sahu 		drvdata->regulator = NULL;
4139b2da53fSShreshtha Kumar Sahu 		goto err_regulator_get;
4149b2da53fSShreshtha Kumar Sahu 	}
4159b2da53fSShreshtha Kumar Sahu 
4169b2da53fSShreshtha Kumar Sahu 	if (drvdata->pdata->brt_val) {
417b1e6b706SShreshtha Kumar Sahu 		err = lm3530_init_registers(drvdata);
418b1e6b706SShreshtha Kumar Sahu 		if (err < 0) {
4199b2da53fSShreshtha Kumar Sahu 			dev_err(&client->dev,
4209b2da53fSShreshtha Kumar Sahu 				"Register Init failed: %d\n", err);
421b1e6b706SShreshtha Kumar Sahu 			err = -ENODEV;
422b1e6b706SShreshtha Kumar Sahu 			goto err_reg_init;
423b1e6b706SShreshtha Kumar Sahu 		}
4249b2da53fSShreshtha Kumar Sahu 	}
4259b2da53fSShreshtha Kumar Sahu 	err = led_classdev_register(&client->dev, &drvdata->led_dev);
426b1e6b706SShreshtha Kumar Sahu 	if (err < 0) {
427b1e6b706SShreshtha Kumar Sahu 		dev_err(&client->dev, "Register led class failed: %d\n", err);
428b1e6b706SShreshtha Kumar Sahu 		err = -ENODEV;
429b1e6b706SShreshtha Kumar Sahu 		goto err_class_register;
430b1e6b706SShreshtha Kumar Sahu 	}
431b1e6b706SShreshtha Kumar Sahu 
432b1e6b706SShreshtha Kumar Sahu 	err = device_create_file(drvdata->led_dev.dev, &dev_attr_mode);
433b1e6b706SShreshtha Kumar Sahu 	if (err < 0) {
434b1e6b706SShreshtha Kumar Sahu 		dev_err(&client->dev, "File device creation failed: %d\n", err);
435b1e6b706SShreshtha Kumar Sahu 		err = -ENODEV;
436b1e6b706SShreshtha Kumar Sahu 		goto err_create_file;
437b1e6b706SShreshtha Kumar Sahu 	}
438b1e6b706SShreshtha Kumar Sahu 
439b1e6b706SShreshtha Kumar Sahu 	return 0;
440b1e6b706SShreshtha Kumar Sahu 
441b1e6b706SShreshtha Kumar Sahu err_create_file:
442b1e6b706SShreshtha Kumar Sahu 	led_classdev_unregister(&drvdata->led_dev);
443b1e6b706SShreshtha Kumar Sahu err_class_register:
444b1e6b706SShreshtha Kumar Sahu err_reg_init:
4459b2da53fSShreshtha Kumar Sahu 	regulator_put(drvdata->regulator);
4469b2da53fSShreshtha Kumar Sahu err_regulator_get:
447b1e6b706SShreshtha Kumar Sahu 	kfree(drvdata);
448b1e6b706SShreshtha Kumar Sahu err_out:
449b1e6b706SShreshtha Kumar Sahu 	return err;
450b1e6b706SShreshtha Kumar Sahu }
451b1e6b706SShreshtha Kumar Sahu 
452b1e6b706SShreshtha Kumar Sahu static int __devexit lm3530_remove(struct i2c_client *client)
453b1e6b706SShreshtha Kumar Sahu {
454b1e6b706SShreshtha Kumar Sahu 	struct lm3530_data *drvdata = i2c_get_clientdata(client);
455b1e6b706SShreshtha Kumar Sahu 
456b1e6b706SShreshtha Kumar Sahu 	device_remove_file(drvdata->led_dev.dev, &dev_attr_mode);
4579b2da53fSShreshtha Kumar Sahu 
4589b2da53fSShreshtha Kumar Sahu 	if (drvdata->enable)
4599b2da53fSShreshtha Kumar Sahu 		regulator_disable(drvdata->regulator);
4609b2da53fSShreshtha Kumar Sahu 	regulator_put(drvdata->regulator);
461b1e6b706SShreshtha Kumar Sahu 	led_classdev_unregister(&drvdata->led_dev);
462b1e6b706SShreshtha Kumar Sahu 	kfree(drvdata);
463b1e6b706SShreshtha Kumar Sahu 	return 0;
464b1e6b706SShreshtha Kumar Sahu }
465b1e6b706SShreshtha Kumar Sahu 
466b1e6b706SShreshtha Kumar Sahu static const struct i2c_device_id lm3530_id[] = {
467b1e6b706SShreshtha Kumar Sahu 	{LM3530_NAME, 0},
468b1e6b706SShreshtha Kumar Sahu 	{}
469b1e6b706SShreshtha Kumar Sahu };
470d5f33d45SAxel Lin MODULE_DEVICE_TABLE(i2c, lm3530_id);
471b1e6b706SShreshtha Kumar Sahu 
472b1e6b706SShreshtha Kumar Sahu static struct i2c_driver lm3530_i2c_driver = {
473b1e6b706SShreshtha Kumar Sahu 	.probe = lm3530_probe,
47493f53982SAxel Lin 	.remove = __devexit_p(lm3530_remove),
475b1e6b706SShreshtha Kumar Sahu 	.id_table = lm3530_id,
476b1e6b706SShreshtha Kumar Sahu 	.driver = {
477b1e6b706SShreshtha Kumar Sahu 		.name = LM3530_NAME,
478b1e6b706SShreshtha Kumar Sahu 		.owner = THIS_MODULE,
479b1e6b706SShreshtha Kumar Sahu 	},
480b1e6b706SShreshtha Kumar Sahu };
481b1e6b706SShreshtha Kumar Sahu 
48209a0d183SAxel Lin module_i2c_driver(lm3530_i2c_driver);
483b1e6b706SShreshtha Kumar Sahu 
484b1e6b706SShreshtha Kumar Sahu MODULE_DESCRIPTION("Back Light driver for LM3530");
485b1e6b706SShreshtha Kumar Sahu MODULE_LICENSE("GPL v2");
486b1e6b706SShreshtha Kumar Sahu MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>");
487