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