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