blob: b437541555ffbc1db13ddb75e68de46d6c0109e5 [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
32struct lp855x {
33 const char *chipname;
34 enum lp855x_chip_id chip_id;
35 struct i2c_client *client;
36 struct backlight_device *bl;
37 struct device *dev;
38 struct mutex xfer_lock;
39 struct lp855x_platform_data *pdata;
Kim, Milo8cc97642012-12-17 16:00:43 -080040 struct pwm_device *pwm;
Kim, Milo7be865a2012-03-23 15:02:01 -070041};
42
43static int lp855x_read_byte(struct lp855x *lp, u8 reg, u8 *data)
44{
45 int ret;
46
47 mutex_lock(&lp->xfer_lock);
48 ret = i2c_smbus_read_byte_data(lp->client, reg);
49 if (ret < 0) {
50 mutex_unlock(&lp->xfer_lock);
51 dev_err(lp->dev, "failed to read 0x%.2x\n", reg);
52 return ret;
53 }
54 mutex_unlock(&lp->xfer_lock);
55
56 *data = (u8)ret;
57 return 0;
58}
59
60static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
61{
62 int ret;
63
64 mutex_lock(&lp->xfer_lock);
65 ret = i2c_smbus_write_byte_data(lp->client, reg, data);
66 mutex_unlock(&lp->xfer_lock);
67
68 return ret;
69}
70
71static bool lp855x_is_valid_rom_area(struct lp855x *lp, u8 addr)
72{
73 u8 start, end;
74
75 switch (lp->chip_id) {
76 case LP8550:
77 case LP8551:
78 case LP8552:
79 case LP8553:
80 start = EEPROM_START;
81 end = EEPROM_END;
82 break;
83 case LP8556:
84 start = EPROM_START;
85 end = EPROM_END;
86 break;
87 default:
88 return false;
89 }
90
91 return (addr >= start && addr <= end);
92}
93
94static int lp855x_init_registers(struct lp855x *lp)
95{
96 u8 val, addr;
97 int i, ret;
98 struct lp855x_platform_data *pd = lp->pdata;
99
100 val = pd->initial_brightness;
101 ret = lp855x_write_byte(lp, BRIGHTNESS_CTRL, val);
102 if (ret)
103 return ret;
104
105 val = pd->device_control;
106 ret = lp855x_write_byte(lp, DEVICE_CTRL, val);
107 if (ret)
108 return ret;
109
110 if (pd->load_new_rom_data && pd->size_program) {
111 for (i = 0; i < pd->size_program; i++) {
112 addr = pd->rom_data[i].addr;
113 val = pd->rom_data[i].val;
114 if (!lp855x_is_valid_rom_area(lp, addr))
115 continue;
116
117 ret = lp855x_write_byte(lp, addr, val);
118 if (ret)
119 return ret;
120 }
121 }
122
123 return ret;
124}
125
Kim, Milo8cc97642012-12-17 16:00:43 -0800126static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br)
127{
128 unsigned int period = lp->pdata->period_ns;
129 unsigned int duty = br * period / max_br;
130 struct pwm_device *pwm;
131
132 /* request pwm device with the consumer name */
133 if (!lp->pwm) {
134 pwm = devm_pwm_get(lp->dev, lp->chipname);
135 if (IS_ERR(pwm))
136 return;
137
138 lp->pwm = pwm;
139 }
140
141 pwm_config(lp->pwm, duty, period);
142 if (duty)
143 pwm_enable(lp->pwm);
144 else
145 pwm_disable(lp->pwm);
146}
147
Kim, Milo7be865a2012-03-23 15:02:01 -0700148static int lp855x_bl_update_status(struct backlight_device *bl)
149{
150 struct lp855x *lp = bl_get_data(bl);
151 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
152
153 if (bl->props.state & BL_CORE_SUSPENDED)
154 bl->props.brightness = 0;
155
156 if (mode == PWM_BASED) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700157 int br = bl->props.brightness;
158 int max_br = bl->props.max_brightness;
159
Kim, Milo8cc97642012-12-17 16:00:43 -0800160 lp855x_pwm_ctrl(lp, br, max_br);
Kim, Milo7be865a2012-03-23 15:02:01 -0700161
162 } else if (mode == REGISTER_BASED) {
163 u8 val = bl->props.brightness;
164 lp855x_write_byte(lp, BRIGHTNESS_CTRL, val);
165 }
166
167 return 0;
168}
169
170static int lp855x_bl_get_brightness(struct backlight_device *bl)
171{
172 struct lp855x *lp = bl_get_data(bl);
173 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
174
Kim, Milo8cc97642012-12-17 16:00:43 -0800175 if (mode == REGISTER_BASED) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700176 u8 val = 0;
177
178 lp855x_read_byte(lp, BRIGHTNESS_CTRL, &val);
179 bl->props.brightness = val;
180 }
181
182 return bl->props.brightness;
183}
184
185static const struct backlight_ops lp855x_bl_ops = {
186 .options = BL_CORE_SUSPENDRESUME,
187 .update_status = lp855x_bl_update_status,
188 .get_brightness = lp855x_bl_get_brightness,
189};
190
191static int lp855x_backlight_register(struct lp855x *lp)
192{
193 struct backlight_device *bl;
194 struct backlight_properties props;
195 struct lp855x_platform_data *pdata = lp->pdata;
196 char *name = pdata->name ? : DEFAULT_BL_NAME;
197
198 props.type = BACKLIGHT_PLATFORM;
199 props.max_brightness = MAX_BRIGHTNESS;
200
201 if (pdata->initial_brightness > props.max_brightness)
202 pdata->initial_brightness = props.max_brightness;
203
204 props.brightness = pdata->initial_brightness;
205
206 bl = backlight_device_register(name, lp->dev, lp,
207 &lp855x_bl_ops, &props);
208 if (IS_ERR(bl))
209 return PTR_ERR(bl);
210
211 lp->bl = bl;
212
213 return 0;
214}
215
216static void lp855x_backlight_unregister(struct lp855x *lp)
217{
218 if (lp->bl)
219 backlight_device_unregister(lp->bl);
220}
221
222static ssize_t lp855x_get_chip_id(struct device *dev,
223 struct device_attribute *attr, char *buf)
224{
225 struct lp855x *lp = dev_get_drvdata(dev);
226 return scnprintf(buf, BUF_SIZE, "%s\n", lp->chipname);
227}
228
229static ssize_t lp855x_get_bl_ctl_mode(struct device *dev,
230 struct device_attribute *attr, char *buf)
231{
232 struct lp855x *lp = dev_get_drvdata(dev);
233 enum lp855x_brightness_ctrl_mode mode = lp->pdata->mode;
234 char *strmode = NULL;
235
236 if (mode == PWM_BASED)
237 strmode = "pwm based";
238 else if (mode == REGISTER_BASED)
239 strmode = "register based";
240
241 return scnprintf(buf, BUF_SIZE, "%s\n", strmode);
242}
243
244static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
245static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
246
247static struct attribute *lp855x_attributes[] = {
248 &dev_attr_chip_id.attr,
249 &dev_attr_bl_ctl_mode.attr,
250 NULL,
251};
252
253static const struct attribute_group lp855x_attr_group = {
254 .attrs = lp855x_attributes,
255};
256
257static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
258{
259 struct lp855x *lp;
260 struct lp855x_platform_data *pdata = cl->dev.platform_data;
261 enum lp855x_brightness_ctrl_mode mode;
262 int ret;
263
264 if (!pdata) {
265 dev_err(&cl->dev, "no platform data supplied\n");
266 return -EINVAL;
267 }
268
269 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
270 return -EIO;
271
272 lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL);
273 if (!lp)
274 return -ENOMEM;
275
276 mode = pdata->mode;
277 lp->client = cl;
278 lp->dev = &cl->dev;
279 lp->pdata = pdata;
280 lp->chipname = id->name;
281 lp->chip_id = id->driver_data;
282 i2c_set_clientdata(cl, lp);
283
284 mutex_init(&lp->xfer_lock);
285
286 ret = lp855x_init_registers(lp);
287 if (ret) {
288 dev_err(lp->dev, "i2c communication err: %d", ret);
289 if (mode == REGISTER_BASED)
290 goto err_dev;
291 }
292
293 ret = lp855x_backlight_register(lp);
294 if (ret) {
295 dev_err(lp->dev,
296 "failed to register backlight. err: %d\n", ret);
297 goto err_dev;
298 }
299
300 ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group);
301 if (ret) {
302 dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret);
303 goto err_sysfs;
304 }
305
306 backlight_update_status(lp->bl);
307 return 0;
308
309err_sysfs:
310 lp855x_backlight_unregister(lp);
311err_dev:
312 return ret;
313}
314
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500315static int lp855x_remove(struct i2c_client *cl)
Kim, Milo7be865a2012-03-23 15:02:01 -0700316{
317 struct lp855x *lp = i2c_get_clientdata(cl);
318
319 lp->bl->props.brightness = 0;
320 backlight_update_status(lp->bl);
321 sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
322 lp855x_backlight_unregister(lp);
323
324 return 0;
325}
326
327static const struct i2c_device_id lp855x_ids[] = {
328 {"lp8550", LP8550},
329 {"lp8551", LP8551},
330 {"lp8552", LP8552},
331 {"lp8553", LP8553},
332 {"lp8556", LP8556},
333 { }
334};
335MODULE_DEVICE_TABLE(i2c, lp855x_ids);
336
337static struct i2c_driver lp855x_driver = {
338 .driver = {
339 .name = "lp855x",
340 },
341 .probe = lp855x_probe,
Bill Pembertond1723fa2012-11-19 13:21:09 -0500342 .remove = lp855x_remove,
Kim, Milo7be865a2012-03-23 15:02:01 -0700343 .id_table = lp855x_ids,
344};
345
346module_i2c_driver(lp855x_driver);
347
348MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
349MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
350MODULE_LICENSE("GPL");