xref: /openbmc/linux/drivers/leds/leds-lm3530.c (revision 9cdb81c7)
1 /*
2  * Copyright (C) 2011 ST-Ericsson SA.
3  * Copyright (C) 2009 Motorola, Inc.
4  *
5  * License Terms: GNU General Public License v2
6  *
7  * Simple driver for National Semiconductor LM3530 Backlight driver chip
8  *
9  * Author: Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>
10  * based on leds-lm3530.c by Dan Murphy <D.Murphy@motorola.com>
11  */
12 
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>
21 #include <linux/module.h>
22 
23 #define LM3530_LED_DEV "lcd-backlight"
24 #define LM3530_NAME "lm3530-led"
25 
26 #define LM3530_GEN_CONFIG		0x10
27 #define LM3530_ALS_CONFIG		0x20
28 #define LM3530_BRT_RAMP_RATE		0x30
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			14
41 
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)
49 
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)
54 
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)
59 
60 #define LM3530_ENABLE_ALS		(3 << LM3530_EN_ALS_SHIFT)
61 
62 /* Brightness Ramp Rate Register */
63 #define LM3530_BRT_RAMP_FALL_SHIFT	(0)
64 #define LM3530_BRT_RAMP_RISE_SHIFT	(3)
65 
66 /* ALS Resistor Select */
67 #define LM3530_ALS1_IMP_SHIFT		(0)
68 #define LM3530_ALS2_IMP_SHIFT		(4)
69 
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)
74 
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)
81 
82 /* 7 bits are used for the brightness : LM3530_BRT_CTRL_REG */
83 #define MAX_BRIGHTNESS			(127)
84 
85 struct lm3530_mode_map {
86 	const char *mode;
87 	enum lm3530_mode mode_val;
88 };
89 
90 static struct lm3530_mode_map mode_map[] = {
91 	{ "man", LM3530_BL_MODE_MANUAL },
92 	{ "als", LM3530_BL_MODE_ALS },
93 	{ "pwm", LM3530_BL_MODE_PWM },
94 };
95 
96 /**
97  * struct lm3530_data
98  * @led_dev: led class device
99  * @client: i2c client
100  * @pdata: LM3530 platform data
101  * @mode: mode of operation - manual, ALS, PWM
102  * @regulator: regulator
103  * @brighness: previous brightness value
104  * @enable: regulator is enabled
105  */
106 struct lm3530_data {
107 	struct led_classdev led_dev;
108 	struct i2c_client *client;
109 	struct lm3530_platform_data *pdata;
110 	enum lm3530_mode mode;
111 	struct regulator *regulator;
112 	enum led_brightness brightness;
113 	bool enable;
114 };
115 
116 static const u8 lm3530_reg[LM3530_REG_MAX] = {
117 	LM3530_GEN_CONFIG,
118 	LM3530_ALS_CONFIG,
119 	LM3530_BRT_RAMP_RATE,
120 	LM3530_ALS_IMP_SELECT,
121 	LM3530_BRT_CTRL_REG,
122 	LM3530_ALS_ZB0_REG,
123 	LM3530_ALS_ZB1_REG,
124 	LM3530_ALS_ZB2_REG,
125 	LM3530_ALS_ZB3_REG,
126 	LM3530_ALS_Z0T_REG,
127 	LM3530_ALS_Z1T_REG,
128 	LM3530_ALS_Z2T_REG,
129 	LM3530_ALS_Z3T_REG,
130 	LM3530_ALS_Z4T_REG,
131 };
132 
133 static int lm3530_get_mode_from_str(const char *str)
134 {
135 	int i;
136 
137 	for (i = 0; i < ARRAY_SIZE(mode_map); i++)
138 		if (sysfs_streq(str, mode_map[i].mode))
139 			return mode_map[i].mode_val;
140 
141 	return -1;
142 }
143 
144 static int lm3530_init_registers(struct lm3530_data *drvdata)
145 {
146 	int ret = 0;
147 	int i;
148 	u8 gen_config;
149 	u8 als_config = 0;
150 	u8 brt_ramp;
151 	u8 als_imp_sel = 0;
152 	u8 brightness;
153 	u8 reg_val[LM3530_REG_MAX];
154 	u8 zones[LM3530_ALS_ZB_MAX];
155 	u32 als_vmin, als_vmax, als_vstep;
156 	struct lm3530_platform_data *pdata = drvdata->pdata;
157 	struct i2c_client *client = drvdata->client;
158 	struct lm3530_pwm_data *pwm = &pdata->pwm_data;
159 
160 	gen_config = (pdata->brt_ramp_law << LM3530_RAMP_LAW_SHIFT) |
161 			((pdata->max_current & 7) << LM3530_MAX_CURR_SHIFT);
162 
163 	switch (drvdata->mode) {
164 	case LM3530_BL_MODE_MANUAL:
165 	case LM3530_BL_MODE_ALS:
166 		gen_config |= LM3530_ENABLE_I2C;
167 		break;
168 	case LM3530_BL_MODE_PWM:
169 		gen_config |= LM3530_ENABLE_PWM | LM3530_ENABLE_PWM_SIMPLE |
170 			      (pdata->pwm_pol_hi << LM3530_PWM_POL_SHIFT);
171 		break;
172 	}
173 
174 	if (drvdata->mode == LM3530_BL_MODE_ALS) {
175 		if (pdata->als_vmax == 0) {
176 			pdata->als_vmin = 0;
177 			pdata->als_vmax = LM3530_ALS_WINDOW_mV;
178 		}
179 
180 		als_vmin = pdata->als_vmin;
181 		als_vmax = pdata->als_vmax;
182 
183 		if ((als_vmax - als_vmin) > LM3530_ALS_WINDOW_mV)
184 			pdata->als_vmax = als_vmax =
185 				als_vmin + LM3530_ALS_WINDOW_mV;
186 
187 		/* n zone boundary makes n+1 zones */
188 		als_vstep = (als_vmax - als_vmin) / (LM3530_ALS_ZB_MAX + 1);
189 
190 		for (i = 0; i < LM3530_ALS_ZB_MAX; i++)
191 			zones[i] = (((als_vmin + LM3530_ALS_OFFSET_mV) +
192 					als_vstep + (i * als_vstep)) * LED_FULL)
193 					/ 1000;
194 
195 		als_config =
196 			(pdata->als_avrg_time << LM3530_ALS_AVG_TIME_SHIFT) |
197 			(LM3530_ENABLE_ALS) |
198 			(pdata->als_input_mode << LM3530_ALS_SEL_SHIFT);
199 
200 		als_imp_sel =
201 			(pdata->als1_resistor_sel << LM3530_ALS1_IMP_SHIFT) |
202 			(pdata->als2_resistor_sel << LM3530_ALS2_IMP_SHIFT);
203 
204 	}
205 
206 	brt_ramp = (pdata->brt_ramp_fall << LM3530_BRT_RAMP_FALL_SHIFT) |
207 			(pdata->brt_ramp_rise << LM3530_BRT_RAMP_RISE_SHIFT);
208 
209 	if (drvdata->brightness)
210 		brightness = drvdata->brightness;
211 	else
212 		brightness = drvdata->brightness = pdata->brt_val;
213 
214 	if (brightness > drvdata->led_dev.max_brightness)
215 		brightness = drvdata->led_dev.max_brightness;
216 
217 	reg_val[0] = gen_config;	/* LM3530_GEN_CONFIG */
218 	reg_val[1] = als_config;	/* LM3530_ALS_CONFIG */
219 	reg_val[2] = brt_ramp;		/* LM3530_BRT_RAMP_RATE */
220 	reg_val[3] = als_imp_sel;	/* LM3530_ALS_IMP_SELECT */
221 	reg_val[4] = brightness;	/* LM3530_BRT_CTRL_REG */
222 	reg_val[5] = zones[0];		/* LM3530_ALS_ZB0_REG */
223 	reg_val[6] = zones[1];		/* LM3530_ALS_ZB1_REG */
224 	reg_val[7] = zones[2];		/* LM3530_ALS_ZB2_REG */
225 	reg_val[8] = zones[3];		/* LM3530_ALS_ZB3_REG */
226 	reg_val[9] = LM3530_DEF_ZT_0;	/* LM3530_ALS_Z0T_REG */
227 	reg_val[10] = LM3530_DEF_ZT_1;	/* LM3530_ALS_Z1T_REG */
228 	reg_val[11] = LM3530_DEF_ZT_2;	/* LM3530_ALS_Z2T_REG */
229 	reg_val[12] = LM3530_DEF_ZT_3;	/* LM3530_ALS_Z3T_REG */
230 	reg_val[13] = LM3530_DEF_ZT_4;	/* LM3530_ALS_Z4T_REG */
231 
232 	if (!drvdata->enable) {
233 		ret = regulator_enable(drvdata->regulator);
234 		if (ret) {
235 			dev_err(&drvdata->client->dev,
236 					"Enable regulator failed\n");
237 			return ret;
238 		}
239 		drvdata->enable = true;
240 	}
241 
242 	for (i = 0; i < LM3530_REG_MAX; i++) {
243 		/* do not update brightness register when pwm mode */
244 		if (lm3530_reg[i] == LM3530_BRT_CTRL_REG &&
245 		    drvdata->mode == LM3530_BL_MODE_PWM) {
246 			if (pwm->pwm_set_intensity)
247 				pwm->pwm_set_intensity(reg_val[i],
248 					drvdata->led_dev.max_brightness);
249 			continue;
250 		}
251 
252 		ret = i2c_smbus_write_byte_data(client,
253 				lm3530_reg[i], reg_val[i]);
254 		if (ret)
255 			break;
256 	}
257 
258 	return ret;
259 }
260 
261 static void lm3530_brightness_set(struct led_classdev *led_cdev,
262 				     enum led_brightness brt_val)
263 {
264 	int err;
265 	struct lm3530_data *drvdata =
266 	    container_of(led_cdev, struct lm3530_data, led_dev);
267 	struct lm3530_platform_data *pdata = drvdata->pdata;
268 	struct lm3530_pwm_data *pwm = &pdata->pwm_data;
269 	u8 max_brightness = led_cdev->max_brightness;
270 
271 	switch (drvdata->mode) {
272 	case LM3530_BL_MODE_MANUAL:
273 
274 		if (!drvdata->enable) {
275 			err = lm3530_init_registers(drvdata);
276 			if (err) {
277 				dev_err(&drvdata->client->dev,
278 					"Register Init failed: %d\n", err);
279 				break;
280 			}
281 		}
282 
283 		/* set the brightness in brightness control register*/
284 		err = i2c_smbus_write_byte_data(drvdata->client,
285 				LM3530_BRT_CTRL_REG, brt_val);
286 		if (err)
287 			dev_err(&drvdata->client->dev,
288 				"Unable to set brightness: %d\n", err);
289 		else
290 			drvdata->brightness = brt_val;
291 
292 		if (brt_val == 0) {
293 			err = regulator_disable(drvdata->regulator);
294 			if (err)
295 				dev_err(&drvdata->client->dev,
296 					"Disable regulator failed\n");
297 			drvdata->enable = false;
298 		}
299 		break;
300 	case LM3530_BL_MODE_ALS:
301 		break;
302 	case LM3530_BL_MODE_PWM:
303 		if (pwm->pwm_set_intensity)
304 			pwm->pwm_set_intensity(brt_val, max_brightness);
305 		break;
306 	default:
307 		break;
308 	}
309 }
310 
311 static ssize_t lm3530_mode_get(struct device *dev,
312 		struct device_attribute *attr, char *buf)
313 {
314 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
315 	struct lm3530_data *drvdata;
316 	int i, len = 0;
317 
318 	drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
319 	for (i = 0; i < ARRAY_SIZE(mode_map); i++)
320 		if (drvdata->mode == mode_map[i].mode_val)
321 			len += sprintf(buf + len, "[%s] ", mode_map[i].mode);
322 		else
323 			len += sprintf(buf + len, "%s ", mode_map[i].mode);
324 
325 	len += sprintf(buf + len, "\n");
326 
327 	return len;
328 }
329 
330 static ssize_t lm3530_mode_set(struct device *dev, struct device_attribute
331 				   *attr, const char *buf, size_t size)
332 {
333 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
334 	struct lm3530_data *drvdata;
335 	struct lm3530_pwm_data *pwm;
336 	u8 max_brightness;
337 	int mode, err;
338 
339 	drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
340 	pwm = &drvdata->pdata->pwm_data;
341 	max_brightness = led_cdev->max_brightness;
342 	mode = lm3530_get_mode_from_str(buf);
343 	if (mode < 0) {
344 		dev_err(dev, "Invalid mode\n");
345 		return -EINVAL;
346 	}
347 
348 	drvdata->mode = mode;
349 
350 	/* set pwm to low if unnecessary */
351 	if (mode != LM3530_BL_MODE_PWM && pwm->pwm_set_intensity)
352 		pwm->pwm_set_intensity(0, max_brightness);
353 
354 	err = lm3530_init_registers(drvdata);
355 	if (err) {
356 		dev_err(dev, "Setting %s Mode failed :%d\n", buf, err);
357 		return err;
358 	}
359 
360 	return sizeof(drvdata->mode);
361 }
362 static DEVICE_ATTR(mode, 0644, lm3530_mode_get, lm3530_mode_set);
363 
364 static int __devinit lm3530_probe(struct i2c_client *client,
365 			   const struct i2c_device_id *id)
366 {
367 	struct lm3530_platform_data *pdata = client->dev.platform_data;
368 	struct lm3530_data *drvdata;
369 	int err = 0;
370 
371 	if (pdata == NULL) {
372 		dev_err(&client->dev, "platform data required\n");
373 		err = -ENODEV;
374 		goto err_out;
375 	}
376 
377 	/* BL mode */
378 	if (pdata->mode > LM3530_BL_MODE_PWM) {
379 		dev_err(&client->dev, "Illegal Mode request\n");
380 		err = -EINVAL;
381 		goto err_out;
382 	}
383 
384 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
385 		dev_err(&client->dev, "I2C_FUNC_I2C not supported\n");
386 		err = -EIO;
387 		goto err_out;
388 	}
389 
390 	drvdata = kzalloc(sizeof(struct lm3530_data), GFP_KERNEL);
391 	if (drvdata == NULL) {
392 		err = -ENOMEM;
393 		goto err_out;
394 	}
395 
396 	drvdata->mode = pdata->mode;
397 	drvdata->client = client;
398 	drvdata->pdata = pdata;
399 	drvdata->brightness = LED_OFF;
400 	drvdata->enable = false;
401 	drvdata->led_dev.name = LM3530_LED_DEV;
402 	drvdata->led_dev.brightness_set = lm3530_brightness_set;
403 	drvdata->led_dev.max_brightness = MAX_BRIGHTNESS;
404 
405 	i2c_set_clientdata(client, drvdata);
406 
407 	drvdata->regulator = regulator_get(&client->dev, "vin");
408 	if (IS_ERR(drvdata->regulator)) {
409 		dev_err(&client->dev, "regulator get failed\n");
410 		err = PTR_ERR(drvdata->regulator);
411 		drvdata->regulator = NULL;
412 		goto err_regulator_get;
413 	}
414 
415 	if (drvdata->pdata->brt_val) {
416 		err = lm3530_init_registers(drvdata);
417 		if (err < 0) {
418 			dev_err(&client->dev,
419 				"Register Init failed: %d\n", err);
420 			err = -ENODEV;
421 			goto err_reg_init;
422 		}
423 	}
424 	err = led_classdev_register(&client->dev, &drvdata->led_dev);
425 	if (err < 0) {
426 		dev_err(&client->dev, "Register led class failed: %d\n", err);
427 		err = -ENODEV;
428 		goto err_class_register;
429 	}
430 
431 	err = device_create_file(drvdata->led_dev.dev, &dev_attr_mode);
432 	if (err < 0) {
433 		dev_err(&client->dev, "File device creation failed: %d\n", err);
434 		err = -ENODEV;
435 		goto err_create_file;
436 	}
437 
438 	return 0;
439 
440 err_create_file:
441 	led_classdev_unregister(&drvdata->led_dev);
442 err_class_register:
443 err_reg_init:
444 	regulator_put(drvdata->regulator);
445 err_regulator_get:
446 	kfree(drvdata);
447 err_out:
448 	return err;
449 }
450 
451 static int __devexit lm3530_remove(struct i2c_client *client)
452 {
453 	struct lm3530_data *drvdata = i2c_get_clientdata(client);
454 
455 	device_remove_file(drvdata->led_dev.dev, &dev_attr_mode);
456 
457 	if (drvdata->enable)
458 		regulator_disable(drvdata->regulator);
459 	regulator_put(drvdata->regulator);
460 	led_classdev_unregister(&drvdata->led_dev);
461 	kfree(drvdata);
462 	return 0;
463 }
464 
465 static const struct i2c_device_id lm3530_id[] = {
466 	{LM3530_NAME, 0},
467 	{}
468 };
469 MODULE_DEVICE_TABLE(i2c, lm3530_id);
470 
471 static struct i2c_driver lm3530_i2c_driver = {
472 	.probe = lm3530_probe,
473 	.remove = __devexit_p(lm3530_remove),
474 	.id_table = lm3530_id,
475 	.driver = {
476 		.name = LM3530_NAME,
477 		.owner = THIS_MODULE,
478 	},
479 };
480 
481 module_i2c_driver(lm3530_i2c_driver);
482 
483 MODULE_DESCRIPTION("Back Light driver for LM3530");
484 MODULE_LICENSE("GPL v2");
485 MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>");
486