Commit | Line | Data |
---|---|---|
7be865ab KM |
1 | /* |
2 | * TI LP855x Backlight Driver | |
3 | * | |
4 | * Copyright (C) 2011 Texas Instruments | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/i2c.h> | |
15 | #include <linux/backlight.h> | |
16 | #include <linux/err.h> | |
4add0664 | 17 | #include <linux/of.h> |
f7f95056 | 18 | #include <linux/platform_data/lp855x.h> |
8cc9764c | 19 | #include <linux/pwm.h> |
829b030e | 20 | #include <linux/regulator/consumer.h> |
7be865ab | 21 | |
26e8ccc2 KM |
22 | /* LP8550/1/2/3/6 Registers */ |
23 | #define LP855X_BRIGHTNESS_CTRL 0x00 | |
24 | #define LP855X_DEVICE_CTRL 0x01 | |
25 | #define LP855X_EEPROM_START 0xA0 | |
26 | #define LP855X_EEPROM_END 0xA7 | |
27 | #define LP8556_EPROM_START 0xA0 | |
28 | #define LP8556_EPROM_END 0xAF | |
29 | ||
5812c13a | 30 | /* LP8555/7 Registers */ |
26e8ccc2 KM |
31 | #define LP8557_BL_CMD 0x00 |
32 | #define LP8557_BL_MASK 0x01 | |
33 | #define LP8557_BL_ON 0x01 | |
34 | #define LP8557_BL_OFF 0x00 | |
35 | #define LP8557_BRIGHTNESS_CTRL 0x04 | |
36 | #define LP8557_CONFIG 0x10 | |
5812c13a MK |
37 | #define LP8555_EPROM_START 0x10 |
38 | #define LP8555_EPROM_END 0x7A | |
26e8ccc2 KM |
39 | #define LP8557_EPROM_START 0x10 |
40 | #define LP8557_EPROM_END 0x1E | |
7be865ab | 41 | |
7be865ab KM |
42 | #define DEFAULT_BL_NAME "lcd-backlight" |
43 | #define MAX_BRIGHTNESS 255 | |
44 | ||
0b818573 KM |
45 | enum lp855x_brightness_ctrl_mode { |
46 | PWM_BASED = 1, | |
47 | REGISTER_BASED, | |
48 | }; | |
49 | ||
68853bc2 KM |
50 | struct lp855x; |
51 | ||
52 | /* | |
53 | * struct lp855x_device_config | |
54 | * @pre_init_device: init device function call before updating the brightness | |
55 | * @reg_brightness: register address for brigthenss control | |
56 | * @reg_devicectrl: register address for device control | |
57 | * @post_init_device: late init device function call | |
58 | */ | |
59 | struct lp855x_device_config { | |
60 | int (*pre_init_device)(struct lp855x *); | |
61 | u8 reg_brightness; | |
62 | u8 reg_devicectrl; | |
63 | int (*post_init_device)(struct lp855x *); | |
64 | }; | |
65 | ||
7be865ab KM |
66 | struct lp855x { |
67 | const char *chipname; | |
68 | enum lp855x_chip_id chip_id; | |
0b818573 | 69 | enum lp855x_brightness_ctrl_mode mode; |
68853bc2 | 70 | struct lp855x_device_config *cfg; |
7be865ab KM |
71 | struct i2c_client *client; |
72 | struct backlight_device *bl; | |
73 | struct device *dev; | |
7be865ab | 74 | struct lp855x_platform_data *pdata; |
8cc9764c | 75 | struct pwm_device *pwm; |
fe009175 | 76 | struct regulator *supply; /* regulator for VDD input */ |
7be865ab KM |
77 | }; |
78 | ||
7be865ab KM |
79 | static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data) |
80 | { | |
eaa4d02f | 81 | return i2c_smbus_write_byte_data(lp->client, reg, data); |
7be865ab KM |
82 | } |
83 | ||
26e8ccc2 KM |
84 | static int lp855x_update_bit(struct lp855x *lp, u8 reg, u8 mask, u8 data) |
85 | { | |
86 | int ret; | |
87 | u8 tmp; | |
88 | ||
89 | ret = i2c_smbus_read_byte_data(lp->client, reg); | |
90 | if (ret < 0) { | |
91 | dev_err(lp->dev, "failed to read 0x%.2x\n", reg); | |
92 | return ret; | |
93 | } | |
94 | ||
95 | tmp = (u8)ret; | |
96 | tmp &= ~mask; | |
97 | tmp |= data & mask; | |
98 | ||
99 | return lp855x_write_byte(lp, reg, tmp); | |
100 | } | |
101 | ||
7be865ab KM |
102 | static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr) |
103 | { | |
104 | u8 start, end; | |
105 | ||
106 | switch (lp->chip_id) { | |
107 | case LP8550: | |
108 | case LP8551: | |
109 | case LP8552: | |
110 | case LP8553: | |
26e8ccc2 KM |
111 | start = LP855X_EEPROM_START; |
112 | end = LP855X_EEPROM_END; | |
7be865ab KM |
113 | break; |
114 | case LP8556: | |
26e8ccc2 KM |
115 | start = LP8556_EPROM_START; |
116 | end = LP8556_EPROM_END; | |
117 | break; | |
5812c13a MK |
118 | case LP8555: |
119 | start = LP8555_EPROM_START; | |
120 | end = LP8555_EPROM_END; | |
121 | break; | |
26e8ccc2 KM |
122 | case LP8557: |
123 | start = LP8557_EPROM_START; | |
124 | end = LP8557_EPROM_END; | |
7be865ab KM |
125 | break; |
126 | default: | |
127 | return false; | |
128 | } | |
129 | ||
2ce23860 | 130 | return addr >= start && addr <= end; |
7be865ab KM |
131 | } |
132 | ||
26e8ccc2 KM |
133 | static int lp8557_bl_off(struct lp855x *lp) |
134 | { | |
135 | /* BL_ON = 0 before updating EPROM settings */ | |
136 | return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK, | |
137 | LP8557_BL_OFF); | |
138 | } | |
139 | ||
140 | static int lp8557_bl_on(struct lp855x *lp) | |
141 | { | |
142 | /* BL_ON = 1 after updating EPROM settings */ | |
143 | return lp855x_update_bit(lp, LP8557_BL_CMD, LP8557_BL_MASK, | |
144 | LP8557_BL_ON); | |
145 | } | |
146 | ||
68853bc2 | 147 | static struct lp855x_device_config lp855x_dev_cfg = { |
26e8ccc2 KM |
148 | .reg_brightness = LP855X_BRIGHTNESS_CTRL, |
149 | .reg_devicectrl = LP855X_DEVICE_CTRL, | |
150 | }; | |
151 | ||
152 | static struct lp855x_device_config lp8557_dev_cfg = { | |
153 | .reg_brightness = LP8557_BRIGHTNESS_CTRL, | |
154 | .reg_devicectrl = LP8557_CONFIG, | |
155 | .pre_init_device = lp8557_bl_off, | |
156 | .post_init_device = lp8557_bl_on, | |
68853bc2 KM |
157 | }; |
158 | ||
159 | /* | |
160 | * Device specific configuration flow | |
161 | * | |
162 | * a) pre_init_device(optional) | |
163 | * b) update the brightness register | |
164 | * c) update device control register | |
165 | * d) update ROM area(optional) | |
166 | * e) post_init_device(optional) | |
167 | * | |
168 | */ | |
169 | static int lp855x_configure(struct lp855x *lp) | |
7be865ab KM |
170 | { |
171 | u8 val, addr; | |
172 | int i, ret; | |
173 | struct lp855x_platform_data *pd = lp->pdata; | |
174 | ||
68853bc2 | 175 | switch (lp->chip_id) { |
5812c13a MK |
176 | case LP8550: |
177 | case LP8551: | |
178 | case LP8552: | |
179 | case LP8553: | |
180 | case LP8556: | |
68853bc2 KM |
181 | lp->cfg = &lp855x_dev_cfg; |
182 | break; | |
5812c13a | 183 | case LP8555: |
26e8ccc2 KM |
184 | case LP8557: |
185 | lp->cfg = &lp8557_dev_cfg; | |
186 | break; | |
68853bc2 KM |
187 | default: |
188 | return -EINVAL; | |
189 | } | |
190 | ||
191 | if (lp->cfg->pre_init_device) { | |
192 | ret = lp->cfg->pre_init_device(lp); | |
193 | if (ret) { | |
194 | dev_err(lp->dev, "pre init device err: %d\n", ret); | |
195 | goto err; | |
196 | } | |
197 | } | |
198 | ||
7be865ab | 199 | val = pd->initial_brightness; |
68853bc2 | 200 | ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val); |
7be865ab | 201 | if (ret) |
68853bc2 | 202 | goto err; |
7be865ab KM |
203 | |
204 | val = pd->device_control; | |
68853bc2 | 205 | ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val); |
7be865ab | 206 | if (ret) |
68853bc2 | 207 | goto err; |
7be865ab | 208 | |
c365e59d | 209 | if (pd->size_program > 0) { |
7be865ab KM |
210 | for (i = 0; i < pd->size_program; i++) { |
211 | addr = pd->rom_data[i].addr; | |
212 | val = pd->rom_data[i].val; | |
213 | if (!lp855x_is_valid_rom_area(lp, addr)) | |
214 | continue; | |
215 | ||
216 | ret = lp855x_write_byte(lp, addr, val); | |
217 | if (ret) | |
68853bc2 KM |
218 | goto err; |
219 | } | |
220 | } | |
221 | ||
222 | if (lp->cfg->post_init_device) { | |
223 | ret = lp->cfg->post_init_device(lp); | |
224 | if (ret) { | |
225 | dev_err(lp->dev, "post init device err: %d\n", ret); | |
226 | goto err; | |
7be865ab KM |
227 | } |
228 | } | |
229 | ||
68853bc2 KM |
230 | return 0; |
231 | ||
232 | err: | |
7be865ab KM |
233 | return ret; |
234 | } | |
235 | ||
8cc9764c KM |
236 | static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br) |
237 | { | |
238 | unsigned int period = lp->pdata->period_ns; | |
239 | unsigned int duty = br * period / max_br; | |
240 | struct pwm_device *pwm; | |
241 | ||
242 | /* request pwm device with the consumer name */ | |
243 | if (!lp->pwm) { | |
244 | pwm = devm_pwm_get(lp->dev, lp->chipname); | |
245 | if (IS_ERR(pwm)) | |
246 | return; | |
247 | ||
248 | lp->pwm = pwm; | |
249 | } | |
250 | ||
251 | pwm_config(lp->pwm, duty, period); | |
252 | if (duty) | |
253 | pwm_enable(lp->pwm); | |
254 | else | |
255 | pwm_disable(lp->pwm); | |
256 | } | |
257 | ||
7be865ab KM |
258 | static int lp855x_bl_update_status(struct backlight_device *bl) |
259 | { | |
260 | struct lp855x *lp = bl_get_data(bl); | |
61c1c614 | 261 | int brightness = bl->props.brightness; |
7be865ab | 262 | |
9f0a511f | 263 | if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK)) |
61c1c614 | 264 | brightness = 0; |
7be865ab | 265 | |
61c1c614 SP |
266 | if (lp->mode == PWM_BASED) |
267 | lp855x_pwm_ctrl(lp, brightness, bl->props.max_brightness); | |
268 | else if (lp->mode == REGISTER_BASED) | |
269 | lp855x_write_byte(lp, lp->cfg->reg_brightness, (u8)brightness); | |
7be865ab KM |
270 | |
271 | return 0; | |
272 | } | |
273 | ||
7be865ab KM |
274 | static const struct backlight_ops lp855x_bl_ops = { |
275 | .options = BL_CORE_SUSPENDRESUME, | |
276 | .update_status = lp855x_bl_update_status, | |
7be865ab KM |
277 | }; |
278 | ||
279 | static int lp855x_backlight_register(struct lp855x *lp) | |
280 | { | |
281 | struct backlight_device *bl; | |
282 | struct backlight_properties props; | |
283 | struct lp855x_platform_data *pdata = lp->pdata; | |
600ffd33 | 284 | const char *name = pdata->name ? : DEFAULT_BL_NAME; |
7be865ab KM |
285 | |
286 | props.type = BACKLIGHT_PLATFORM; | |
287 | props.max_brightness = MAX_BRIGHTNESS; | |
288 | ||
289 | if (pdata->initial_brightness > props.max_brightness) | |
290 | pdata->initial_brightness = props.max_brightness; | |
291 | ||
292 | props.brightness = pdata->initial_brightness; | |
293 | ||
6255e8e9 | 294 | bl = devm_backlight_device_register(lp->dev, name, lp->dev, lp, |
7be865ab KM |
295 | &lp855x_bl_ops, &props); |
296 | if (IS_ERR(bl)) | |
297 | return PTR_ERR(bl); | |
298 | ||
299 | lp->bl = bl; | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
7be865ab KM |
304 | static ssize_t lp855x_get_chip_id(struct device *dev, |
305 | struct device_attribute *attr, char *buf) | |
306 | { | |
307 | struct lp855x *lp = dev_get_drvdata(dev); | |
a94cb126 | 308 | |
6a7aeb1a | 309 | return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname); |
7be865ab KM |
310 | } |
311 | ||
312 | static ssize_t lp855x_get_bl_ctl_mode(struct device *dev, | |
313 | struct device_attribute *attr, char *buf) | |
314 | { | |
315 | struct lp855x *lp = dev_get_drvdata(dev); | |
7be865ab KM |
316 | char *strmode = NULL; |
317 | ||
0b818573 | 318 | if (lp->mode == PWM_BASED) |
7be865ab | 319 | strmode = "pwm based"; |
0b818573 | 320 | else if (lp->mode == REGISTER_BASED) |
7be865ab KM |
321 | strmode = "register based"; |
322 | ||
6a7aeb1a | 323 | return scnprintf(buf, PAGE_SIZE, "%s\n", strmode); |
7be865ab KM |
324 | } |
325 | ||
326 | static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL); | |
327 | static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL); | |
328 | ||
329 | static struct attribute *lp855x_attributes[] = { | |
330 | &dev_attr_chip_id.attr, | |
331 | &dev_attr_bl_ctl_mode.attr, | |
332 | NULL, | |
333 | }; | |
334 | ||
335 | static const struct attribute_group lp855x_attr_group = { | |
336 | .attrs = lp855x_attributes, | |
337 | }; | |
338 | ||
4add0664 | 339 | #ifdef CONFIG_OF |
47726656 | 340 | static int lp855x_parse_dt(struct lp855x *lp) |
4add0664 | 341 | { |
47726656 SP |
342 | struct device *dev = lp->dev; |
343 | struct device_node *node = dev->of_node; | |
4add0664 KM |
344 | struct lp855x_platform_data *pdata; |
345 | int rom_length; | |
346 | ||
347 | if (!node) { | |
348 | dev_err(dev, "no platform data\n"); | |
349 | return -EINVAL; | |
350 | } | |
351 | ||
352 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); | |
353 | if (!pdata) | |
354 | return -ENOMEM; | |
355 | ||
356 | of_property_read_string(node, "bl-name", &pdata->name); | |
357 | of_property_read_u8(node, "dev-ctrl", &pdata->device_control); | |
358 | of_property_read_u8(node, "init-brt", &pdata->initial_brightness); | |
359 | of_property_read_u32(node, "pwm-period", &pdata->period_ns); | |
360 | ||
361 | /* Fill ROM platform data if defined */ | |
362 | rom_length = of_get_child_count(node); | |
363 | if (rom_length > 0) { | |
364 | struct lp855x_rom_data *rom; | |
365 | struct device_node *child; | |
366 | int i = 0; | |
367 | ||
368 | rom = devm_kzalloc(dev, sizeof(*rom) * rom_length, GFP_KERNEL); | |
369 | if (!rom) | |
370 | return -ENOMEM; | |
371 | ||
372 | for_each_child_of_node(node, child) { | |
373 | of_property_read_u8(child, "rom-addr", &rom[i].addr); | |
374 | of_property_read_u8(child, "rom-val", &rom[i].val); | |
375 | i++; | |
376 | } | |
377 | ||
378 | pdata->size_program = rom_length; | |
379 | pdata->rom_data = &rom[0]; | |
380 | } | |
381 | ||
47726656 | 382 | lp->pdata = pdata; |
4add0664 KM |
383 | |
384 | return 0; | |
385 | } | |
386 | #else | |
47726656 | 387 | static int lp855x_parse_dt(struct lp855x *lp) |
4add0664 KM |
388 | { |
389 | return -EINVAL; | |
390 | } | |
391 | #endif | |
392 | ||
7be865ab KM |
393 | static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id) |
394 | { | |
395 | struct lp855x *lp; | |
7be865ab KM |
396 | int ret; |
397 | ||
7be865ab KM |
398 | if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) |
399 | return -EIO; | |
400 | ||
401 | lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL); | |
402 | if (!lp) | |
403 | return -ENOMEM; | |
404 | ||
7be865ab KM |
405 | lp->client = cl; |
406 | lp->dev = &cl->dev; | |
7be865ab KM |
407 | lp->chipname = id->name; |
408 | lp->chip_id = id->driver_data; | |
47726656 SP |
409 | lp->pdata = dev_get_platdata(&cl->dev); |
410 | ||
411 | if (!lp->pdata) { | |
412 | ret = lp855x_parse_dt(lp); | |
413 | if (ret < 0) | |
414 | return ret; | |
415 | } | |
416 | ||
417 | if (lp->pdata->period_ns > 0) | |
418 | lp->mode = PWM_BASED; | |
419 | else | |
420 | lp->mode = REGISTER_BASED; | |
421 | ||
fe009175 MK |
422 | lp->supply = devm_regulator_get(lp->dev, "power"); |
423 | if (IS_ERR(lp->supply)) { | |
424 | if (PTR_ERR(lp->supply) == -EPROBE_DEFER) | |
425 | return -EPROBE_DEFER; | |
426 | lp->supply = NULL; | |
427 | } | |
428 | ||
429 | if (lp->supply) { | |
430 | ret = regulator_enable(lp->supply); | |
829b030e SP |
431 | if (ret < 0) { |
432 | dev_err(&cl->dev, "failed to enable supply: %d\n", ret); | |
433 | return ret; | |
434 | } | |
435 | } | |
436 | ||
7be865ab KM |
437 | i2c_set_clientdata(cl, lp); |
438 | ||
68853bc2 | 439 | ret = lp855x_configure(lp); |
7be865ab | 440 | if (ret) { |
68853bc2 | 441 | dev_err(lp->dev, "device config err: %d", ret); |
6255e8e9 | 442 | return ret; |
7be865ab KM |
443 | } |
444 | ||
445 | ret = lp855x_backlight_register(lp); | |
446 | if (ret) { | |
447 | dev_err(lp->dev, | |
448 | "failed to register backlight. err: %d\n", ret); | |
6255e8e9 | 449 | return ret; |
7be865ab KM |
450 | } |
451 | ||
452 | ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group); | |
453 | if (ret) { | |
454 | dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret); | |
6255e8e9 | 455 | return ret; |
7be865ab KM |
456 | } |
457 | ||
458 | backlight_update_status(lp->bl); | |
459 | return 0; | |
7be865ab KM |
460 | } |
461 | ||
7e4b9d0b | 462 | static int lp855x_remove(struct i2c_client *cl) |
7be865ab KM |
463 | { |
464 | struct lp855x *lp = i2c_get_clientdata(cl); | |
465 | ||
466 | lp->bl->props.brightness = 0; | |
467 | backlight_update_status(lp->bl); | |
fe009175 MK |
468 | if (lp->supply) |
469 | regulator_disable(lp->supply); | |
7be865ab | 470 | sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group); |
7be865ab KM |
471 | |
472 | return 0; | |
473 | } | |
474 | ||
4add0664 KM |
475 | static const struct of_device_id lp855x_dt_ids[] = { |
476 | { .compatible = "ti,lp8550", }, | |
477 | { .compatible = "ti,lp8551", }, | |
478 | { .compatible = "ti,lp8552", }, | |
479 | { .compatible = "ti,lp8553", }, | |
5812c13a | 480 | { .compatible = "ti,lp8555", }, |
4add0664 KM |
481 | { .compatible = "ti,lp8556", }, |
482 | { .compatible = "ti,lp8557", }, | |
483 | { } | |
484 | }; | |
485 | MODULE_DEVICE_TABLE(of, lp855x_dt_ids); | |
486 | ||
7be865ab KM |
487 | static const struct i2c_device_id lp855x_ids[] = { |
488 | {"lp8550", LP8550}, | |
489 | {"lp8551", LP8551}, | |
490 | {"lp8552", LP8552}, | |
491 | {"lp8553", LP8553}, | |
5812c13a | 492 | {"lp8555", LP8555}, |
7be865ab | 493 | {"lp8556", LP8556}, |
26e8ccc2 | 494 | {"lp8557", LP8557}, |
7be865ab KM |
495 | { } |
496 | }; | |
497 | MODULE_DEVICE_TABLE(i2c, lp855x_ids); | |
498 | ||
499 | static struct i2c_driver lp855x_driver = { | |
500 | .driver = { | |
501 | .name = "lp855x", | |
4add0664 | 502 | .of_match_table = of_match_ptr(lp855x_dt_ids), |
7be865ab KM |
503 | }, |
504 | .probe = lp855x_probe, | |
d1723fa2 | 505 | .remove = lp855x_remove, |
7be865ab KM |
506 | .id_table = lp855x_ids, |
507 | }; | |
508 | ||
509 | module_i2c_driver(lp855x_driver); | |
510 | ||
511 | MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver"); | |
512 | MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>"); | |
513 | MODULE_LICENSE("GPL"); |