blob: ce79523a4d108409e2060c60b700261a9220d59a [file] [log] [blame]
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -07001/*
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>
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -070020#include <linux/regulator/consumer.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040021#include <linux/module.h>
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070022
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_ZONE_REG 0x40
30#define LM3530_ALS_IMP_SELECT 0x41
31#define LM3530_BRT_CTRL_REG 0xA0
32#define LM3530_ALS_ZB0_REG 0x60
33#define LM3530_ALS_ZB1_REG 0x61
34#define LM3530_ALS_ZB2_REG 0x62
35#define LM3530_ALS_ZB3_REG 0x63
36#define LM3530_ALS_Z0T_REG 0x70
37#define LM3530_ALS_Z1T_REG 0x71
38#define LM3530_ALS_Z2T_REG 0x72
39#define LM3530_ALS_Z3T_REG 0x73
40#define LM3530_ALS_Z4T_REG 0x74
41#define LM3530_REG_MAX 15
42
43/* General Control Register */
44#define LM3530_EN_I2C_SHIFT (0)
45#define LM3530_RAMP_LAW_SHIFT (1)
46#define LM3530_MAX_CURR_SHIFT (2)
47#define LM3530_EN_PWM_SHIFT (5)
48#define LM3530_PWM_POL_SHIFT (6)
49#define LM3530_EN_PWM_SIMPLE_SHIFT (7)
50
51#define LM3530_ENABLE_I2C (1 << LM3530_EN_I2C_SHIFT)
52#define LM3530_ENABLE_PWM (1 << LM3530_EN_PWM_SHIFT)
53#define LM3530_POL_LOW (1 << LM3530_PWM_POL_SHIFT)
54#define LM3530_ENABLE_PWM_SIMPLE (1 << LM3530_EN_PWM_SIMPLE_SHIFT)
55
56/* ALS Config Register Options */
57#define LM3530_ALS_AVG_TIME_SHIFT (0)
58#define LM3530_EN_ALS_SHIFT (3)
59#define LM3530_ALS_SEL_SHIFT (5)
60
61#define LM3530_ENABLE_ALS (3 << LM3530_EN_ALS_SHIFT)
62
63/* Brightness Ramp Rate Register */
64#define LM3530_BRT_RAMP_FALL_SHIFT (0)
65#define LM3530_BRT_RAMP_RISE_SHIFT (3)
66
67/* ALS Resistor Select */
68#define LM3530_ALS1_IMP_SHIFT (0)
69#define LM3530_ALS2_IMP_SHIFT (4)
70
71/* Zone Boundary Register defaults */
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -070072#define LM3530_ALS_ZB_MAX (4)
73#define LM3530_ALS_WINDOW_mV (1000)
74#define LM3530_ALS_OFFSET_mV (4)
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070075
76/* Zone Target Register defaults */
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -070077#define LM3530_DEF_ZT_0 (0x7F)
78#define LM3530_DEF_ZT_1 (0x66)
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070079#define LM3530_DEF_ZT_2 (0x4C)
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -070080#define LM3530_DEF_ZT_3 (0x33)
81#define LM3530_DEF_ZT_4 (0x19)
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070082
Kim, Milobbe64532012-03-23 15:02:11 -070083/* 7 bits are used for the brightness : LM3530_BRT_CTRL_REG */
84#define MAX_BRIGHTNESS (127)
85
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -070086struct lm3530_mode_map {
87 const char *mode;
88 enum lm3530_mode mode_val;
89};
90
91static struct lm3530_mode_map mode_map[] = {
92 { "man", LM3530_BL_MODE_MANUAL },
93 { "als", LM3530_BL_MODE_ALS },
94 { "pwm", LM3530_BL_MODE_PWM },
95};
96
97/**
98 * struct lm3530_data
99 * @led_dev: led class device
100 * @client: i2c client
101 * @pdata: LM3530 platform data
102 * @mode: mode of operation - manual, ALS, PWM
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700103 * @regulator: regulator
104 * @brighness: previous brightness value
105 * @enable: regulator is enabled
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700106 */
107struct lm3530_data {
108 struct led_classdev led_dev;
109 struct i2c_client *client;
110 struct lm3530_platform_data *pdata;
111 enum lm3530_mode mode;
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700112 struct regulator *regulator;
113 enum led_brightness brightness;
114 bool enable;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700115};
116
117static const u8 lm3530_reg[LM3530_REG_MAX] = {
118 LM3530_GEN_CONFIG,
119 LM3530_ALS_CONFIG,
120 LM3530_BRT_RAMP_RATE,
121 LM3530_ALS_ZONE_REG,
122 LM3530_ALS_IMP_SELECT,
123 LM3530_BRT_CTRL_REG,
124 LM3530_ALS_ZB0_REG,
125 LM3530_ALS_ZB1_REG,
126 LM3530_ALS_ZB2_REG,
127 LM3530_ALS_ZB3_REG,
128 LM3530_ALS_Z0T_REG,
129 LM3530_ALS_Z1T_REG,
130 LM3530_ALS_Z2T_REG,
131 LM3530_ALS_Z3T_REG,
132 LM3530_ALS_Z4T_REG,
133};
134
135static int lm3530_get_mode_from_str(const char *str)
136{
137 int i;
138
139 for (i = 0; i < ARRAY_SIZE(mode_map); i++)
140 if (sysfs_streq(str, mode_map[i].mode))
141 return mode_map[i].mode_val;
142
143 return -1;
144}
145
146static int lm3530_init_registers(struct lm3530_data *drvdata)
147{
148 int ret = 0;
149 int i;
150 u8 gen_config;
151 u8 als_config = 0;
152 u8 brt_ramp;
153 u8 als_imp_sel = 0;
154 u8 brightness;
155 u8 reg_val[LM3530_REG_MAX];
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700156 u8 zones[LM3530_ALS_ZB_MAX];
157 u32 als_vmin, als_vmax, als_vstep;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700158 struct lm3530_platform_data *pltfm = drvdata->pdata;
159 struct i2c_client *client = drvdata->client;
160
161 gen_config = (pltfm->brt_ramp_law << LM3530_RAMP_LAW_SHIFT) |
162 ((pltfm->max_current & 7) << LM3530_MAX_CURR_SHIFT);
163
164 if (drvdata->mode == LM3530_BL_MODE_MANUAL ||
165 drvdata->mode == LM3530_BL_MODE_ALS)
166 gen_config |= (LM3530_ENABLE_I2C);
167
168 if (drvdata->mode == LM3530_BL_MODE_ALS) {
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700169 if (pltfm->als_vmax == 0) {
Axel Linec44fd42012-02-08 17:13:39 -0800170 pltfm->als_vmin = 0;
171 pltfm->als_vmax = LM3530_ALS_WINDOW_mV;
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700172 }
173
174 als_vmin = pltfm->als_vmin;
175 als_vmax = pltfm->als_vmax;
176
177 if ((als_vmax - als_vmin) > LM3530_ALS_WINDOW_mV)
178 pltfm->als_vmax = als_vmax =
179 als_vmin + LM3530_ALS_WINDOW_mV;
180
181 /* n zone boundary makes n+1 zones */
182 als_vstep = (als_vmax - als_vmin) / (LM3530_ALS_ZB_MAX + 1);
183
184 for (i = 0; i < LM3530_ALS_ZB_MAX; i++)
185 zones[i] = (((als_vmin + LM3530_ALS_OFFSET_mV) +
186 als_vstep + (i * als_vstep)) * LED_FULL)
187 / 1000;
188
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700189 als_config =
190 (pltfm->als_avrg_time << LM3530_ALS_AVG_TIME_SHIFT) |
191 (LM3530_ENABLE_ALS) |
192 (pltfm->als_input_mode << LM3530_ALS_SEL_SHIFT);
193
194 als_imp_sel =
195 (pltfm->als1_resistor_sel << LM3530_ALS1_IMP_SHIFT) |
196 (pltfm->als2_resistor_sel << LM3530_ALS2_IMP_SHIFT);
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700197
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700198 }
199
200 if (drvdata->mode == LM3530_BL_MODE_PWM)
201 gen_config |= (LM3530_ENABLE_PWM) |
202 (pltfm->pwm_pol_hi << LM3530_PWM_POL_SHIFT) |
203 (LM3530_ENABLE_PWM_SIMPLE);
204
205 brt_ramp = (pltfm->brt_ramp_fall << LM3530_BRT_RAMP_FALL_SHIFT) |
206 (pltfm->brt_ramp_rise << LM3530_BRT_RAMP_RISE_SHIFT);
207
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700208 if (drvdata->brightness)
209 brightness = drvdata->brightness;
210 else
211 brightness = drvdata->brightness = pltfm->brt_val;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700212
Kim, Milobbe64532012-03-23 15:02:11 -0700213 if (brightness > drvdata->led_dev.max_brightness)
214 brightness = drvdata->led_dev.max_brightness;
215
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700216 reg_val[0] = gen_config; /* LM3530_GEN_CONFIG */
217 reg_val[1] = als_config; /* LM3530_ALS_CONFIG */
218 reg_val[2] = brt_ramp; /* LM3530_BRT_RAMP_RATE */
219 reg_val[3] = 0x00; /* LM3530_ALS_ZONE_REG */
220 reg_val[4] = als_imp_sel; /* LM3530_ALS_IMP_SELECT */
221 reg_val[5] = brightness; /* LM3530_BRT_CTRL_REG */
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700222 reg_val[6] = zones[0]; /* LM3530_ALS_ZB0_REG */
223 reg_val[7] = zones[1]; /* LM3530_ALS_ZB1_REG */
224 reg_val[8] = zones[2]; /* LM3530_ALS_ZB2_REG */
225 reg_val[9] = zones[3]; /* LM3530_ALS_ZB3_REG */
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700226 reg_val[10] = LM3530_DEF_ZT_0; /* LM3530_ALS_Z0T_REG */
227 reg_val[11] = LM3530_DEF_ZT_1; /* LM3530_ALS_Z1T_REG */
228 reg_val[12] = LM3530_DEF_ZT_2; /* LM3530_ALS_Z2T_REG */
229 reg_val[13] = LM3530_DEF_ZT_3; /* LM3530_ALS_Z3T_REG */
230 reg_val[14] = LM3530_DEF_ZT_4; /* LM3530_ALS_Z4T_REG */
231
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700232 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
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700242 for (i = 0; i < LM3530_REG_MAX; i++) {
243 ret = i2c_smbus_write_byte_data(client,
244 lm3530_reg[i], reg_val[i]);
245 if (ret)
246 break;
247 }
248
249 return ret;
250}
251
252static void lm3530_brightness_set(struct led_classdev *led_cdev,
253 enum led_brightness brt_val)
254{
255 int err;
256 struct lm3530_data *drvdata =
257 container_of(led_cdev, struct lm3530_data, led_dev);
258
259 switch (drvdata->mode) {
260 case LM3530_BL_MODE_MANUAL:
261
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700262 if (!drvdata->enable) {
263 err = lm3530_init_registers(drvdata);
264 if (err) {
265 dev_err(&drvdata->client->dev,
266 "Register Init failed: %d\n", err);
267 break;
268 }
269 }
270
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700271 /* set the brightness in brightness control register*/
272 err = i2c_smbus_write_byte_data(drvdata->client,
Kim, Milobbe64532012-03-23 15:02:11 -0700273 LM3530_BRT_CTRL_REG, brt_val);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700274 if (err)
275 dev_err(&drvdata->client->dev,
276 "Unable to set brightness: %d\n", err);
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700277 else
Kim, Milobbe64532012-03-23 15:02:11 -0700278 drvdata->brightness = brt_val;
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700279
280 if (brt_val == 0) {
281 err = regulator_disable(drvdata->regulator);
282 if (err)
283 dev_err(&drvdata->client->dev,
284 "Disable regulator failed\n");
285 drvdata->enable = false;
286 }
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700287 break;
288 case LM3530_BL_MODE_ALS:
289 break;
290 case LM3530_BL_MODE_PWM:
291 break;
292 default:
293 break;
294 }
295}
296
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700297static ssize_t lm3530_mode_get(struct device *dev,
298 struct device_attribute *attr, char *buf)
299{
Kim, Miloe13d9782012-03-23 15:02:11 -0700300 struct led_classdev *led_cdev = dev_get_drvdata(dev);
301 struct lm3530_data *drvdata;
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700302 int i, len = 0;
303
Kim, Miloe13d9782012-03-23 15:02:11 -0700304 drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700305 for (i = 0; i < ARRAY_SIZE(mode_map); i++)
306 if (drvdata->mode == mode_map[i].mode_val)
307 len += sprintf(buf + len, "[%s] ", mode_map[i].mode);
308 else
309 len += sprintf(buf + len, "%s ", mode_map[i].mode);
310
311 len += sprintf(buf + len, "\n");
312
313 return len;
314}
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700315
316static ssize_t lm3530_mode_set(struct device *dev, struct device_attribute
317 *attr, const char *buf, size_t size)
318{
Kim, Miloe13d9782012-03-23 15:02:11 -0700319 struct led_classdev *led_cdev = dev_get_drvdata(dev);
320 struct lm3530_data *drvdata;
321 int mode, err;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700322
Kim, Miloe13d9782012-03-23 15:02:11 -0700323 drvdata = container_of(led_cdev, struct lm3530_data, led_dev);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700324 mode = lm3530_get_mode_from_str(buf);
325 if (mode < 0) {
326 dev_err(dev, "Invalid mode\n");
327 return -EINVAL;
328 }
329
330 if (mode == LM3530_BL_MODE_MANUAL)
331 drvdata->mode = LM3530_BL_MODE_MANUAL;
332 else if (mode == LM3530_BL_MODE_ALS)
333 drvdata->mode = LM3530_BL_MODE_ALS;
334 else if (mode == LM3530_BL_MODE_PWM) {
335 dev_err(dev, "PWM mode not supported\n");
336 return -EINVAL;
337 }
338
339 err = lm3530_init_registers(drvdata);
340 if (err) {
341 dev_err(dev, "Setting %s Mode failed :%d\n", buf, err);
342 return err;
343 }
344
345 return sizeof(drvdata->mode);
346}
Shreshtha Kumar Sahu40b14452011-07-25 17:13:17 -0700347static DEVICE_ATTR(mode, 0644, lm3530_mode_get, lm3530_mode_set);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700348
349static int __devinit lm3530_probe(struct i2c_client *client,
350 const struct i2c_device_id *id)
351{
352 struct lm3530_platform_data *pdata = client->dev.platform_data;
353 struct lm3530_data *drvdata;
354 int err = 0;
355
356 if (pdata == NULL) {
357 dev_err(&client->dev, "platform data required\n");
358 err = -ENODEV;
359 goto err_out;
360 }
361
362 /* BL mode */
363 if (pdata->mode > LM3530_BL_MODE_PWM) {
364 dev_err(&client->dev, "Illegal Mode request\n");
365 err = -EINVAL;
366 goto err_out;
367 }
368
369 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
370 dev_err(&client->dev, "I2C_FUNC_I2C not supported\n");
371 err = -EIO;
372 goto err_out;
373 }
374
375 drvdata = kzalloc(sizeof(struct lm3530_data), GFP_KERNEL);
376 if (drvdata == NULL) {
377 err = -ENOMEM;
378 goto err_out;
379 }
380
381 drvdata->mode = pdata->mode;
382 drvdata->client = client;
383 drvdata->pdata = pdata;
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700384 drvdata->brightness = LED_OFF;
385 drvdata->enable = false;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700386 drvdata->led_dev.name = LM3530_LED_DEV;
387 drvdata->led_dev.brightness_set = lm3530_brightness_set;
Kim, Milobbe64532012-03-23 15:02:11 -0700388 drvdata->led_dev.max_brightness = MAX_BRIGHTNESS;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700389
390 i2c_set_clientdata(client, drvdata);
391
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700392 drvdata->regulator = regulator_get(&client->dev, "vin");
393 if (IS_ERR(drvdata->regulator)) {
394 dev_err(&client->dev, "regulator get failed\n");
395 err = PTR_ERR(drvdata->regulator);
396 drvdata->regulator = NULL;
397 goto err_regulator_get;
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700398 }
399
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700400 if (drvdata->pdata->brt_val) {
401 err = lm3530_init_registers(drvdata);
402 if (err < 0) {
403 dev_err(&client->dev,
404 "Register Init failed: %d\n", err);
405 err = -ENODEV;
406 goto err_reg_init;
407 }
408 }
409 err = led_classdev_register(&client->dev, &drvdata->led_dev);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700410 if (err < 0) {
411 dev_err(&client->dev, "Register led class failed: %d\n", err);
412 err = -ENODEV;
413 goto err_class_register;
414 }
415
416 err = device_create_file(drvdata->led_dev.dev, &dev_attr_mode);
417 if (err < 0) {
418 dev_err(&client->dev, "File device creation failed: %d\n", err);
419 err = -ENODEV;
420 goto err_create_file;
421 }
422
423 return 0;
424
425err_create_file:
426 led_classdev_unregister(&drvdata->led_dev);
427err_class_register:
428err_reg_init:
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700429 regulator_put(drvdata->regulator);
430err_regulator_get:
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700431 kfree(drvdata);
432err_out:
433 return err;
434}
435
436static int __devexit lm3530_remove(struct i2c_client *client)
437{
438 struct lm3530_data *drvdata = i2c_get_clientdata(client);
439
440 device_remove_file(drvdata->led_dev.dev, &dev_attr_mode);
Shreshtha Kumar Sahu9b2da532011-05-24 17:13:28 -0700441
442 if (drvdata->enable)
443 regulator_disable(drvdata->regulator);
444 regulator_put(drvdata->regulator);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700445 led_classdev_unregister(&drvdata->led_dev);
446 kfree(drvdata);
447 return 0;
448}
449
450static const struct i2c_device_id lm3530_id[] = {
451 {LM3530_NAME, 0},
452 {}
453};
Axel Lind5f33d42011-05-17 15:44:09 -0700454MODULE_DEVICE_TABLE(i2c, lm3530_id);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700455
456static struct i2c_driver lm3530_i2c_driver = {
457 .probe = lm3530_probe,
Axel Lin93f53982011-10-31 17:12:15 -0700458 .remove = __devexit_p(lm3530_remove),
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700459 .id_table = lm3530_id,
460 .driver = {
461 .name = LM3530_NAME,
462 .owner = THIS_MODULE,
463 },
464};
465
Axel Lin09a0d182012-01-10 15:09:27 -0800466module_i2c_driver(lm3530_i2c_driver);
Shreshtha Kumar Sahub1e6b702011-03-22 16:30:16 -0700467
468MODULE_DESCRIPTION("Back Light driver for LM3530");
469MODULE_LICENSE("GPL v2");
470MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>");