Commit | Line | Data |
---|---|---|
b1e6b706 SKS |
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> | |
9b2da53f | 20 | #include <linux/regulator/consumer.h> |
54f4dedb | 21 | #include <linux/module.h> |
b1e6b706 SKS |
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_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 */ | |
40b1445b SKS |
72 | #define LM3530_ALS_ZB_MAX (4) |
73 | #define LM3530_ALS_WINDOW_mV (1000) | |
74 | #define LM3530_ALS_OFFSET_mV (4) | |
b1e6b706 SKS |
75 | |
76 | /* Zone Target Register defaults */ | |
40b1445b SKS |
77 | #define LM3530_DEF_ZT_0 (0x7F) |
78 | #define LM3530_DEF_ZT_1 (0x66) | |
b1e6b706 | 79 | #define LM3530_DEF_ZT_2 (0x4C) |
40b1445b SKS |
80 | #define LM3530_DEF_ZT_3 (0x33) |
81 | #define LM3530_DEF_ZT_4 (0x19) | |
b1e6b706 | 82 | |
bbe64532 KM |
83 | /* 7 bits are used for the brightness : LM3530_BRT_CTRL_REG */ |
84 | #define MAX_BRIGHTNESS (127) | |
85 | ||
b1e6b706 SKS |
86 | struct lm3530_mode_map { |
87 | const char *mode; | |
88 | enum lm3530_mode mode_val; | |
89 | }; | |
90 | ||
91 | static 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 | |
9b2da53f SKS |
103 | * @regulator: regulator |
104 | * @brighness: previous brightness value | |
105 | * @enable: regulator is enabled | |
b1e6b706 SKS |
106 | */ |
107 | struct lm3530_data { | |
108 | struct led_classdev led_dev; | |
109 | struct i2c_client *client; | |
110 | struct lm3530_platform_data *pdata; | |
111 | enum lm3530_mode mode; | |
9b2da53f SKS |
112 | struct regulator *regulator; |
113 | enum led_brightness brightness; | |
114 | bool enable; | |
b1e6b706 SKS |
115 | }; |
116 | ||
117 | static 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 | ||
135 | static 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 | ||
146 | static 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]; | |
40b1445b SKS |
156 | u8 zones[LM3530_ALS_ZB_MAX]; |
157 | u32 als_vmin, als_vmax, als_vstep; | |
b1e6b706 SKS |
158 | 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) { | |
40b1445b | 169 | if (pltfm->als_vmax == 0) { |
ec44fd42 AL |
170 | pltfm->als_vmin = 0; |
171 | pltfm->als_vmax = LM3530_ALS_WINDOW_mV; | |
40b1445b SKS |
172 | } |
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 | ||
b1e6b706 SKS |
189 | 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); | |
40b1445b | 197 | |
b1e6b706 SKS |
198 | } |
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 | ||
9b2da53f SKS |
208 | if (drvdata->brightness) |
209 | brightness = drvdata->brightness; | |
210 | else | |
211 | brightness = drvdata->brightness = pltfm->brt_val; | |
b1e6b706 | 212 | |
bbe64532 KM |
213 | if (brightness > drvdata->led_dev.max_brightness) |
214 | brightness = drvdata->led_dev.max_brightness; | |
215 | ||
b1e6b706 SKS |
216 | 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 */ | |
40b1445b SKS |
222 | 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 */ | |
b1e6b706 SKS |
226 | 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 | ||
9b2da53f SKS |
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 | ||
b1e6b706 SKS |
242 | 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 | ||
252 | static 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 | ||
9b2da53f SKS |
262 | 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 | ||
b1e6b706 SKS |
271 | /* set the brightness in brightness control register*/ |
272 | err = i2c_smbus_write_byte_data(drvdata->client, | |
bbe64532 | 273 | LM3530_BRT_CTRL_REG, brt_val); |
b1e6b706 SKS |
274 | if (err) |
275 | dev_err(&drvdata->client->dev, | |
276 | "Unable to set brightness: %d\n", err); | |
9b2da53f | 277 | else |
bbe64532 | 278 | drvdata->brightness = brt_val; |
9b2da53f SKS |
279 | |
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 | } | |
b1e6b706 SKS |
287 | break; |
288 | case LM3530_BL_MODE_ALS: | |
289 | break; | |
290 | case LM3530_BL_MODE_PWM: | |
291 | break; | |
292 | default: | |
293 | break; | |
294 | } | |
295 | } | |
296 | ||
40b1445b SKS |
297 | static ssize_t lm3530_mode_get(struct device *dev, |
298 | struct device_attribute *attr, char *buf) | |
299 | { | |
300 | struct i2c_client *client = container_of( | |
301 | dev->parent, struct i2c_client, dev); | |
302 | struct lm3530_data *drvdata = i2c_get_clientdata(client); | |
303 | int i, len = 0; | |
304 | ||
305 | 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 | } | |
b1e6b706 SKS |
315 | |
316 | static ssize_t lm3530_mode_set(struct device *dev, struct device_attribute | |
317 | *attr, const char *buf, size_t size) | |
318 | { | |
319 | int err; | |
320 | struct i2c_client *client = container_of( | |
321 | dev->parent, struct i2c_client, dev); | |
322 | struct lm3530_data *drvdata = i2c_get_clientdata(client); | |
323 | int mode; | |
324 | ||
325 | mode = lm3530_get_mode_from_str(buf); | |
326 | if (mode < 0) { | |
327 | dev_err(dev, "Invalid mode\n"); | |
328 | return -EINVAL; | |
329 | } | |
330 | ||
331 | if (mode == LM3530_BL_MODE_MANUAL) | |
332 | drvdata->mode = LM3530_BL_MODE_MANUAL; | |
333 | else if (mode == LM3530_BL_MODE_ALS) | |
334 | drvdata->mode = LM3530_BL_MODE_ALS; | |
335 | else if (mode == LM3530_BL_MODE_PWM) { | |
336 | dev_err(dev, "PWM mode not supported\n"); | |
337 | return -EINVAL; | |
338 | } | |
339 | ||
340 | err = lm3530_init_registers(drvdata); | |
341 | if (err) { | |
342 | dev_err(dev, "Setting %s Mode failed :%d\n", buf, err); | |
343 | return err; | |
344 | } | |
345 | ||
346 | return sizeof(drvdata->mode); | |
347 | } | |
40b1445b | 348 | static DEVICE_ATTR(mode, 0644, lm3530_mode_get, lm3530_mode_set); |
b1e6b706 SKS |
349 | |
350 | static int __devinit lm3530_probe(struct i2c_client *client, | |
351 | const struct i2c_device_id *id) | |
352 | { | |
353 | struct lm3530_platform_data *pdata = client->dev.platform_data; | |
354 | struct lm3530_data *drvdata; | |
355 | int err = 0; | |
356 | ||
357 | if (pdata == NULL) { | |
358 | dev_err(&client->dev, "platform data required\n"); | |
359 | err = -ENODEV; | |
360 | goto err_out; | |
361 | } | |
362 | ||
363 | /* BL mode */ | |
364 | if (pdata->mode > LM3530_BL_MODE_PWM) { | |
365 | dev_err(&client->dev, "Illegal Mode request\n"); | |
366 | err = -EINVAL; | |
367 | goto err_out; | |
368 | } | |
369 | ||
370 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | |
371 | dev_err(&client->dev, "I2C_FUNC_I2C not supported\n"); | |
372 | err = -EIO; | |
373 | goto err_out; | |
374 | } | |
375 | ||
376 | drvdata = kzalloc(sizeof(struct lm3530_data), GFP_KERNEL); | |
377 | if (drvdata == NULL) { | |
378 | err = -ENOMEM; | |
379 | goto err_out; | |
380 | } | |
381 | ||
382 | drvdata->mode = pdata->mode; | |
383 | drvdata->client = client; | |
384 | drvdata->pdata = pdata; | |
9b2da53f SKS |
385 | drvdata->brightness = LED_OFF; |
386 | drvdata->enable = false; | |
b1e6b706 SKS |
387 | drvdata->led_dev.name = LM3530_LED_DEV; |
388 | drvdata->led_dev.brightness_set = lm3530_brightness_set; | |
bbe64532 | 389 | drvdata->led_dev.max_brightness = MAX_BRIGHTNESS; |
b1e6b706 SKS |
390 | |
391 | i2c_set_clientdata(client, drvdata); | |
392 | ||
9b2da53f SKS |
393 | drvdata->regulator = regulator_get(&client->dev, "vin"); |
394 | if (IS_ERR(drvdata->regulator)) { | |
395 | dev_err(&client->dev, "regulator get failed\n"); | |
396 | err = PTR_ERR(drvdata->regulator); | |
397 | drvdata->regulator = NULL; | |
398 | goto err_regulator_get; | |
b1e6b706 SKS |
399 | } |
400 | ||
9b2da53f SKS |
401 | if (drvdata->pdata->brt_val) { |
402 | err = lm3530_init_registers(drvdata); | |
403 | if (err < 0) { | |
404 | dev_err(&client->dev, | |
405 | "Register Init failed: %d\n", err); | |
406 | err = -ENODEV; | |
407 | goto err_reg_init; | |
408 | } | |
409 | } | |
410 | err = led_classdev_register(&client->dev, &drvdata->led_dev); | |
b1e6b706 SKS |
411 | if (err < 0) { |
412 | dev_err(&client->dev, "Register led class failed: %d\n", err); | |
413 | err = -ENODEV; | |
414 | goto err_class_register; | |
415 | } | |
416 | ||
417 | err = device_create_file(drvdata->led_dev.dev, &dev_attr_mode); | |
418 | if (err < 0) { | |
419 | dev_err(&client->dev, "File device creation failed: %d\n", err); | |
420 | err = -ENODEV; | |
421 | goto err_create_file; | |
422 | } | |
423 | ||
424 | return 0; | |
425 | ||
426 | err_create_file: | |
427 | led_classdev_unregister(&drvdata->led_dev); | |
428 | err_class_register: | |
429 | err_reg_init: | |
9b2da53f SKS |
430 | regulator_put(drvdata->regulator); |
431 | err_regulator_get: | |
b1e6b706 SKS |
432 | kfree(drvdata); |
433 | err_out: | |
434 | return err; | |
435 | } | |
436 | ||
437 | static int __devexit lm3530_remove(struct i2c_client *client) | |
438 | { | |
439 | struct lm3530_data *drvdata = i2c_get_clientdata(client); | |
440 | ||
441 | device_remove_file(drvdata->led_dev.dev, &dev_attr_mode); | |
9b2da53f SKS |
442 | |
443 | if (drvdata->enable) | |
444 | regulator_disable(drvdata->regulator); | |
445 | regulator_put(drvdata->regulator); | |
b1e6b706 SKS |
446 | led_classdev_unregister(&drvdata->led_dev); |
447 | kfree(drvdata); | |
448 | return 0; | |
449 | } | |
450 | ||
451 | static const struct i2c_device_id lm3530_id[] = { | |
452 | {LM3530_NAME, 0}, | |
453 | {} | |
454 | }; | |
d5f33d45 | 455 | MODULE_DEVICE_TABLE(i2c, lm3530_id); |
b1e6b706 SKS |
456 | |
457 | static struct i2c_driver lm3530_i2c_driver = { | |
458 | .probe = lm3530_probe, | |
93f53982 | 459 | .remove = __devexit_p(lm3530_remove), |
b1e6b706 SKS |
460 | .id_table = lm3530_id, |
461 | .driver = { | |
462 | .name = LM3530_NAME, | |
463 | .owner = THIS_MODULE, | |
464 | }, | |
465 | }; | |
466 | ||
09a0d183 | 467 | module_i2c_driver(lm3530_i2c_driver); |
b1e6b706 SKS |
468 | |
469 | MODULE_DESCRIPTION("Back Light driver for LM3530"); | |
470 | MODULE_LICENSE("GPL v2"); | |
471 | MODULE_AUTHOR("Shreshtha Kumar SAHU <shreshthakumar.sahu@stericsson.com>"); |