blob: 050cfbb536672826bb3a0e27a2225ec0158eaeda [file] [log] [blame]
Kim, Milo7be865a2012-03-23 15:02:01 -07001/*
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>
Kim, Milof7f95052012-07-30 14:40:53 -070017#include <linux/platform_data/lp855x.h>
Kim, Milo8cc97642012-12-17 16:00:43 -080018#include <linux/pwm.h>
Kim, Milo7be865a2012-03-23 15:02:01 -070019
20/* Registers */
Kim, Miloa1fcb2e2012-07-30 14:40:50 -070021#define BRIGHTNESS_CTRL 0x00
22#define DEVICE_CTRL 0x01
23#define EEPROM_START 0xA0
24#define EEPROM_END 0xA7
25#define EPROM_START 0xA0
26#define EPROM_END 0xAF
Kim, Milo7be865a2012-03-23 15:02:01 -070027
28#define BUF_SIZE 20
29#define DEFAULT_BL_NAME "lcd-backlight"
30#define MAX_BRIGHTNESS 255
31
Kim, Milo68853bc2013-02-21 16:44:05 -080032struct lp855x;
33
34/*
35 * struct lp855x_device_config
36 * @pre_init_device: init device function call before updating the brightness
37 * @reg_brightness: register address for brigthenss control
38 * @reg_devicectrl: register address for device control
39 * @post_init_device: late init device function call
40 */
41struct lp855x_device_config {
42 int (*pre_init_device)(struct lp855x *);
43 u8 reg_brightness;
44 u8 reg_devicectrl;
45 int (*post_init_device)(struct lp855x *);
46};
47
Kim, Milo7be865a2012-03-23 15:02:01 -070048struct lp855x {
49 const char *chipname;
50 enum lp855x_chip_id chip_id;
Kim, Milo68853bc2013-02-21 16:44:05 -080051 struct lp855x_device_config *cfg;
Kim, Milo7be865a2012-03-23 15:02:01 -070052 struct i2c_client *client;
53 struct backlight_device *bl;
54 struct device *dev;
Kim, Milo7be865a2012-03-23 15:02:01 -070055 struct lp855x_platform_data *pdata;
Kim, Milo8cc97642012-12-17 16:00:43 -080056 struct pwm_device *pwm;
Kim, Milo7be865a2012-03-23 15:02:01 -070057};
58
59static int lp855x_read_byte(struct lp855x *lp, u8 reg, u8 *data)
60{
61 int ret;
62
Kim, Milo7be865a2012-03-23 15:02:01 -070063 ret = i2c_smbus_read_byte_data(lp->client, reg);
64 if (ret < 0) {
Kim, Milo7be865a2012-03-23 15:02:01 -070065 dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
66 return ret;
67 }
Kim, Milo7be865a2012-03-23 15:02:01 -070068
69 *data = (u8)ret;
70 return 0;
71}
72
73static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
74{
Kim, Miloeaa4d022012-12-17 16:00:45 -080075 return i2c_smbus_write_byte_data(lp->client, reg, data);
Kim, Milo7be865a2012-03-23 15:02:01 -070076}
77
78static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
79{
80 u8 start, end;
81
82 switch (lp->chip_id) {
83 case LP8550:
84 case LP8551:
85 case LP8552:
86 case LP8553:
87 start = EEPROM_START;
88 end = EEPROM_END;
89 break;
90 case LP8556:
91 start = EPROM_START;
92 end = EPROM_END;
93 break;
94 default:
95 return false;
96 }
97
98 return (addr >= start && addr <= end);
99}
100
Kim, Milo68853bc2013-02-21 16:44:05 -0800101static struct lp855x_device_config lp855x_dev_cfg = {
102 .reg_brightness = BRIGHTNESS_CTRL,
103 .reg_devicectrl = DEVICE_CTRL,
104};
105
106/*
107 * Device specific configuration flow
108 *
109 * a) pre_init_device(optional)
110 * b) update the brightness register
111 * c) update device control register
112 * d) update ROM area(optional)
113 * e) post_init_device(optional)
114 *
115 */
116static int lp855x_configure(struct lp855x *lp)
Kim, Milo7be865a2012-03-23 15:02:01 -0700117{
118 u8 val, addr;
119 int i, ret;
120 struct lp855x_platform_data *pd = lp->pdata;
121
Kim, Milo68853bc2013-02-21 16:44:05 -0800122 switch (lp->chip_id) {
123 case LP8550 ... LP8556:
124 lp->cfg = &lp855x_dev_cfg;
125 break;
126 default:
127 return -EINVAL;
128 }
129
130 if (lp->cfg->pre_init_device) {
131 ret = lp->cfg->pre_init_device(lp);
132 if (ret) {
133 dev_err(lp->dev, "pre init device err: %d\n", ret);
134 goto err;
135 }
136 }
137
Kim, Milo7be865a2012-03-23 15:02:01 -0700138 val = pd->initial_brightness;
Kim, Milo68853bc2013-02-21 16:44:05 -0800139 ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
Kim, Milo7be865a2012-03-23 15:02:01 -0700140 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800141 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700142
143 val = pd->device_control;
Kim, Milo68853bc2013-02-21 16:44:05 -0800144 ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
Kim, Milo7be865a2012-03-23 15:02:01 -0700145 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800146 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700147
148 if (pd->load_new_rom_data && pd->size_program) {
149 for (i = 0; i < pd->size_program; i++) {
150 addr = pd->rom_data[i].addr;
151 val = pd->rom_data[i].val;
152 if (!lp855x_is_valid_rom_area(lp, addr))
153 continue;
154
155 ret = lp855x_write_byte(lp, addr, val);
156 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800157 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700158 }
159 }
160
Kim, Milo68853bc2013-02-21 16:44:05 -0800161 if (lp->cfg->post_init_device) {
162 ret = lp->cfg->post_init_device(lp);
163 if (ret) {
164 dev_err(lp->dev, "post init device err: %d\n", ret);
165 goto err;
166 }
167 }
168
169 return 0;
170
171err:
Kim, Milo7be865a2012-03-23 15:02:01 -0700172 return ret;
173}
174
Kim, Milo8cc97642012-12-17 16:00:43 -0800175static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
176{
177 unsigned int period = lp->pdata->period_ns;
178 unsigned int duty = br * period / max_br;
179 struct pwm_device *pwm;
180
181 /* request pwm device with the consumer name */
182 if (!lp->pwm) {
183 pwm = devm_pwm_get(lp->dev, lp->chipname);
184 if (IS_ERR(pwm))
185 return;
186
187 lp->pwm = pwm;
188 }
189
190 pwm_config(lp->pwm, duty, period);
191 if (duty)
192 pwm_enable(lp->pwm);
193 else
194 pwm_disable(lp->pwm);
195}
196
Kim, Milo7be865a2012-03-23 15:02:01 -0700197static int lp855x_bl_update_status(struct backlight_device *bl)
198{
199 struct lp855x *lp = bl_get_data(bl);
200 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
201
202 if (bl->props.state & BL_CORE_SUSPENDED)
203 bl->props.brightness = 0;
204
205 if (mode == PWM_BASED) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700206 int br = bl->props.brightness;
207 int max_br = bl->props.max_brightness;
208
Kim, Milo8cc97642012-12-17 16:00:43 -0800209 lp855x_pwm_ctrl(lp, br, max_br);
Kim, Milo7be865a2012-03-23 15:02:01 -0700210
211 } else if (mode == REGISTER_BASED) {
212 u8 val = bl->props.brightness;
213 lp855x_write_byte(lp, BRIGHTNESS_CTRL, val);
214 }
215
216 return 0;
217}
218
219static int lp855x_bl_get_brightness(struct backlight_device *bl)
220{
221 struct lp855x *lp = bl_get_data(bl);
222 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
223
Kim, Milo8cc97642012-12-17 16:00:43 -0800224 if (mode == REGISTER_BASED) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700225 u8 val = 0;
226
227 lp855x_read_byte(lp, BRIGHTNESS_CTRL, &val);
228 bl->props.brightness = val;
229 }
230
231 return bl->props.brightness;
232}
233
234static const struct backlight_ops lp855x_bl_ops = {
235 .options = BL_CORE_SUSPENDRESUME,
236 .update_status = lp855x_bl_update_status,
237 .get_brightness = lp855x_bl_get_brightness,
238};
239
240static int lp855x_backlight_register(struct lp855x *lp)
241{
242 struct backlight_device *bl;
243 struct backlight_properties props;
244 struct lp855x_platform_data *pdata = lp->pdata;
245 char *name = pdata->name ? : DEFAULT_BL_NAME;
246
247 props.type = BACKLIGHT_PLATFORM;
248 props.max_brightness = MAX_BRIGHTNESS;
249
250 if (pdata->initial_brightness > props.max_brightness)
251 pdata->initial_brightness = props.max_brightness;
252
253 props.brightness = pdata->initial_brightness;
254
255 bl = backlight_device_register(name, lp->dev, lp,
256 &lp855x_bl_ops, &props);
257 if (IS_ERR(bl))
258 return PTR_ERR(bl);
259
260 lp->bl = bl;
261
262 return 0;
263}
264
265static void lp855x_backlight_unregister(struct lp855x *lp)
266{
267 if (lp->bl)
268 backlight_device_unregister(lp->bl);
269}
270
271static ssize_t lp855x_get_chip_id(struct device *dev,
272 struct device_attribute *attr, char *buf)
273{
274 struct lp855x *lp = dev_get_drvdata(dev);
275 return scnprintf(buf, BUF_SIZE, "%s\n", lp->chipname);
276}
277
278static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
279 struct device_attribute *attr, char *buf)
280{
281 struct lp855x *lp = dev_get_drvdata(dev);
282 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
283 char *strmode = NULL;
284
285 if (mode == PWM_BASED)
286 strmode = "pwm based";
287 else if (mode == REGISTER_BASED)
288 strmode = "register based";
289
290 return scnprintf(buf, BUF_SIZE, "%s\n", strmode);
291}
292
293static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
294static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
295
296static struct attribute *lp855x_attributes[] = {
297 &dev_attr_chip_id.attr,
298 &dev_attr_bl_ctl_mode.attr,
299 NULL,
300};
301
302static const struct attribute_group lp855x_attr_group = {
303 .attrs = lp855x_attributes,
304};
305
306static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
307{
308 struct lp855x *lp;
309 struct lp855x_platform_data *pdata = cl->dev.platform_data;
310 enum lp855x_brightness_ctrl_mode mode;
311 int ret;
312
313 if (!pdata) {
314 dev_err(&cl->dev, "no platform data supplied\n");
315 return -EINVAL;
316 }
317
318 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
319 return -EIO;
320
321 lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
322 if (!lp)
323 return -ENOMEM;
324
325 mode = pdata->mode;
326 lp->client = cl;
327 lp->dev = &cl->dev;
328 lp->pdata = pdata;
329 lp->chipname = id->name;
330 lp->chip_id = id->driver_data;
331 i2c_set_clientdata(cl, lp);
332
Kim, Milo68853bc2013-02-21 16:44:05 -0800333 ret = lp855x_configure(lp);
Kim, Milo7be865a2012-03-23 15:02:01 -0700334 if (ret) {
Kim, Milo68853bc2013-02-21 16:44:05 -0800335 dev_err(lp->dev, "device config err: %d", ret);
336 goto err_dev;
Kim, Milo7be865a2012-03-23 15:02:01 -0700337 }
338
339 ret = lp855x_backlight_register(lp);
340 if (ret) {
341 dev_err(lp->dev,
342 "failed to register backlight. err: %d\n", ret);
343 goto err_dev;
344 }
345
346 ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
347 if (ret) {
348 dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
349 goto err_sysfs;
350 }
351
352 backlight_update_status(lp->bl);
353 return 0;
354
355err_sysfs:
356 lp855x_backlight_unregister(lp);
357err_dev:
358 return ret;
359}
360
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500361static int lp855x_remove(struct i2c_client *cl)
Kim, Milo7be865a2012-03-23 15:02:01 -0700362{
363 struct lp855x *lp = i2c_get_clientdata(cl);
364
365 lp->bl->props.brightness = 0;
366 backlight_update_status(lp->bl);
367 sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
368 lp855x_backlight_unregister(lp);
369
370 return 0;
371}
372
373static const struct i2c_device_id lp855x_ids[] = {
374 {"lp8550", LP8550},
375 {"lp8551", LP8551},
376 {"lp8552", LP8552},
377 {"lp8553", LP8553},
378 {"lp8556", LP8556},
379 { }
380};
381MODULE_DEVICE_TABLE(i2c, lp855x_ids);
382
383static struct i2c_driver lp855x_driver = {
384 .driver = {
385 .name = "lp855x",
386 },
387 .probe = lp855x_probe,
Bill Pembertond1723fa2012-11-19 13:21:09 -0500388 .remove = lp855x_remove,
Kim, Milo7be865a2012-03-23 15:02:01 -0700389 .id_table = lp855x_ids,
390};
391
392module_i2c_driver(lp855x_driver);
393
394MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
395MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
396MODULE_LICENSE("GPL");