Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 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> |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 17 | #include <linux/of.h> |
Kim, Milo | f7f9505 | 2012-07-30 14:40:53 -0700 | [diff] [blame] | 18 | #include <linux/platform_data/lp855x.h> |
Kim, Milo | 8cc9764 | 2012-12-17 16:00:43 -0800 | [diff] [blame] | 19 | #include <linux/pwm.h> |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 20 | |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 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 | |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 29 | /* LP8555/7 Registers */ |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 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 |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 36 | #define LP8555_EPROM_START 0x10 |
| 37 | #define LP8555_EPROM_END 0x7A |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 38 | #define LP8557_EPROM_START 0x10 |
| 39 | #define LP8557_EPROM_END 0x1E |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 40 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 41 | #define DEFAULT_BL_NAME "lcd-backlight" |
| 42 | #define MAX_BRIGHTNESS 255 |
| 43 | |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 44 | enum lp855x_brightness_ctrl_mode { |
| 45 | PWM_BASED = 1, |
| 46 | REGISTER_BASED, |
| 47 | }; |
| 48 | |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 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 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 65 | struct lp855x { |
| 66 | const char *chipname; |
| 67 | enum lp855x_chip_id chip_id; |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 68 | enum lp855x_brightness_ctrl_mode mode; |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 69 | struct lp855x_device_config *cfg; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 70 | struct i2c_client *client; |
| 71 | struct backlight_device *bl; |
| 72 | struct device *dev; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 73 | struct lp855x_platform_data *pdata; |
Kim, Milo | 8cc9764 | 2012-12-17 16:00:43 -0800 | [diff] [blame] | 74 | struct pwm_device *pwm; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 77 | static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data) |
| 78 | { |
Kim, Milo | eaa4d02 | 2012-12-17 16:00:45 -0800 | [diff] [blame] | 79 | return i2c_smbus_write_byte_data(lp->client, reg, data); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 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 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 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: |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 109 | start = LP855X_EEPROM_START; |
| 110 | end = LP855X_EEPROM_END; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 111 | break; |
| 112 | case LP8556: |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 113 | start = LP8556_EPROM_START; |
| 114 | end = LP8556_EPROM_END; |
| 115 | break; |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 116 | case LP8555: |
| 117 | start = LP8555_EPROM_START; |
| 118 | end = LP8555_EPROM_END; |
| 119 | break; |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 120 | case LP8557: |
| 121 | start = LP8557_EPROM_START; |
| 122 | end = LP8557_EPROM_END; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 123 | break; |
| 124 | default: |
| 125 | return false; |
| 126 | } |
| 127 | |
Jingoo Han | 2ce2386 | 2014-01-23 15:54:32 -0800 | [diff] [blame] | 128 | return addr >= start && addr <= end; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 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 | |
| 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 | |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 145 | static struct lp855x_device_config lp855x_dev_cfg = { |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 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, |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 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 | */ |
| 167 | static int lp855x_configure(struct lp855x *lp) |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 168 | { |
| 169 | u8 val, addr; |
| 170 | int i, ret; |
| 171 | struct lp855x_platform_data *pd = lp->pdata; |
| 172 | |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 173 | switch (lp->chip_id) { |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 174 | case LP8550: |
| 175 | case LP8551: |
| 176 | case LP8552: |
| 177 | case LP8553: |
| 178 | case LP8556: |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 179 | lp->cfg = &lp855x_dev_cfg; |
| 180 | break; |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 181 | case LP8555: |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 182 | case LP8557: |
| 183 | lp->cfg = &lp8557_dev_cfg; |
| 184 | break; |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 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 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 197 | val = pd->initial_brightness; |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 198 | ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 199 | if (ret) |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 200 | goto err; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 201 | |
| 202 | val = pd->device_control; |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 203 | ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 204 | if (ret) |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 205 | goto err; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 206 | |
Kim, Milo | c365e59 | 2013-04-29 16:18:05 -0700 | [diff] [blame] | 207 | if (pd->size_program > 0) { |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 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) |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 216 | goto err; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 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: |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 231 | return ret; |
| 232 | } |
| 233 | |
Kim, Milo | 8cc9764 | 2012-12-17 16:00:43 -0800 | [diff] [blame] | 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 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 256 | static int lp855x_bl_update_status(struct backlight_device *bl) |
| 257 | { |
| 258 | struct lp855x *lp = bl_get_data(bl); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 259 | |
Shingo Nakao | 9f0a511 | 2013-07-02 14:15:54 +0200 | [diff] [blame] | 260 | if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK)) |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 261 | bl->props.brightness = 0; |
| 262 | |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 263 | if (lp->mode == PWM_BASED) { |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 264 | int br = bl->props.brightness; |
| 265 | int max_br = bl->props.max_brightness; |
| 266 | |
Kim, Milo | 8cc9764 | 2012-12-17 16:00:43 -0800 | [diff] [blame] | 267 | lp855x_pwm_ctrl(lp, br, max_br); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 268 | |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 269 | } else if (lp->mode == REGISTER_BASED) { |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 270 | u8 val = bl->props.brightness; |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 271 | lp855x_write_byte(lp, lp->cfg->reg_brightness, val); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 277 | static const struct backlight_ops lp855x_bl_ops = { |
| 278 | .options = BL_CORE_SUSPENDRESUME, |
| 279 | .update_status = lp855x_bl_update_status, |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | static int lp855x_backlight_register(struct lp855x *lp) |
| 283 | { |
| 284 | struct backlight_device *bl; |
| 285 | struct backlight_properties props; |
| 286 | struct lp855x_platform_data *pdata = lp->pdata; |
Kim, Milo | 600ffd3 | 2013-04-29 16:18:02 -0700 | [diff] [blame] | 287 | const char *name = pdata->name ? : DEFAULT_BL_NAME; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 288 | |
| 289 | props.type = BACKLIGHT_PLATFORM; |
| 290 | props.max_brightness = MAX_BRIGHTNESS; |
| 291 | |
| 292 | if (pdata->initial_brightness > props.max_brightness) |
| 293 | pdata->initial_brightness = props.max_brightness; |
| 294 | |
| 295 | props.brightness = pdata->initial_brightness; |
| 296 | |
Jingoo Han | 6255e8e | 2013-11-12 15:09:19 -0800 | [diff] [blame] | 297 | bl = devm_backlight_device_register(lp->dev, name, lp->dev, lp, |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 298 | &lp855x_bl_ops, &props); |
| 299 | if (IS_ERR(bl)) |
| 300 | return PTR_ERR(bl); |
| 301 | |
| 302 | lp->bl = bl; |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 307 | static ssize_t lp855x_get_chip_id(struct device *dev, |
| 308 | struct device_attribute *attr, char *buf) |
| 309 | { |
| 310 | struct lp855x *lp = dev_get_drvdata(dev); |
Kim, Milo | 6a7aeb1 | 2013-04-29 16:17:52 -0700 | [diff] [blame] | 311 | return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static ssize_t lp855x_get_bl_ctl_mode(struct device *dev, |
| 315 | struct device_attribute *attr, char *buf) |
| 316 | { |
| 317 | struct lp855x *lp = dev_get_drvdata(dev); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 318 | char *strmode = NULL; |
| 319 | |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 320 | if (lp->mode == PWM_BASED) |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 321 | strmode = "pwm based"; |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 322 | else if (lp->mode == REGISTER_BASED) |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 323 | strmode = "register based"; |
| 324 | |
Kim, Milo | 6a7aeb1 | 2013-04-29 16:17:52 -0700 | [diff] [blame] | 325 | return scnprintf(buf, PAGE_SIZE, "%s\n", strmode); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL); |
| 329 | static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL); |
| 330 | |
| 331 | static struct attribute *lp855x_attributes[] = { |
| 332 | &dev_attr_chip_id.attr, |
| 333 | &dev_attr_bl_ctl_mode.attr, |
| 334 | NULL, |
| 335 | }; |
| 336 | |
| 337 | static const struct attribute_group lp855x_attr_group = { |
| 338 | .attrs = lp855x_attributes, |
| 339 | }; |
| 340 | |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 341 | #ifdef CONFIG_OF |
| 342 | static int lp855x_parse_dt(struct device *dev, struct device_node *node) |
| 343 | { |
| 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 | |
| 382 | dev->platform_data = pdata; |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | #else |
| 387 | static int lp855x_parse_dt(struct device *dev, struct device_node *node) |
| 388 | { |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | #endif |
| 392 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 393 | static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id) |
| 394 | { |
| 395 | struct lp855x *lp; |
Jingoo Han | c512794c | 2013-11-12 15:09:04 -0800 | [diff] [blame] | 396 | struct lp855x_platform_data *pdata = dev_get_platdata(&cl->dev); |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 397 | struct device_node *node = cl->dev.of_node; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 398 | int ret; |
| 399 | |
| 400 | if (!pdata) { |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 401 | ret = lp855x_parse_dt(&cl->dev, node); |
| 402 | if (ret < 0) |
| 403 | return ret; |
| 404 | |
Jingoo Han | c512794c | 2013-11-12 15:09:04 -0800 | [diff] [blame] | 405 | pdata = dev_get_platdata(&cl->dev); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) |
| 409 | return -EIO; |
| 410 | |
| 411 | lp = devm_kzalloc(&cl->dev, sizeof(struct lp855x), GFP_KERNEL); |
| 412 | if (!lp) |
| 413 | return -ENOMEM; |
| 414 | |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 415 | if (pdata->period_ns > 0) |
| 416 | lp->mode = PWM_BASED; |
| 417 | else |
| 418 | lp->mode = REGISTER_BASED; |
| 419 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 420 | lp->client = cl; |
| 421 | lp->dev = &cl->dev; |
| 422 | lp->pdata = pdata; |
| 423 | lp->chipname = id->name; |
| 424 | lp->chip_id = id->driver_data; |
| 425 | i2c_set_clientdata(cl, lp); |
| 426 | |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 427 | ret = lp855x_configure(lp); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 428 | if (ret) { |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 429 | dev_err(lp->dev, "device config err: %d", ret); |
Jingoo Han | 6255e8e | 2013-11-12 15:09:19 -0800 | [diff] [blame] | 430 | return ret; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | ret = lp855x_backlight_register(lp); |
| 434 | if (ret) { |
| 435 | dev_err(lp->dev, |
| 436 | "failed to register backlight. err: %d\n", ret); |
Jingoo Han | 6255e8e | 2013-11-12 15:09:19 -0800 | [diff] [blame] | 437 | return ret; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | ret = sysfs_create_group(&lp->dev->kobj, &lp855x_attr_group); |
| 441 | if (ret) { |
| 442 | dev_err(lp->dev, "failed to register sysfs. err: %d\n", ret); |
Jingoo Han | 6255e8e | 2013-11-12 15:09:19 -0800 | [diff] [blame] | 443 | return ret; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | backlight_update_status(lp->bl); |
| 447 | return 0; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Bill Pemberton | 7e4b9d0 | 2012-11-19 13:26:34 -0500 | [diff] [blame] | 450 | static int lp855x_remove(struct i2c_client *cl) |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 451 | { |
| 452 | struct lp855x *lp = i2c_get_clientdata(cl); |
| 453 | |
| 454 | lp->bl->props.brightness = 0; |
| 455 | backlight_update_status(lp->bl); |
| 456 | sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 461 | static const struct of_device_id lp855x_dt_ids[] = { |
| 462 | { .compatible = "ti,lp8550", }, |
| 463 | { .compatible = "ti,lp8551", }, |
| 464 | { .compatible = "ti,lp8552", }, |
| 465 | { .compatible = "ti,lp8553", }, |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 466 | { .compatible = "ti,lp8555", }, |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 467 | { .compatible = "ti,lp8556", }, |
| 468 | { .compatible = "ti,lp8557", }, |
| 469 | { } |
| 470 | }; |
| 471 | MODULE_DEVICE_TABLE(of, lp855x_dt_ids); |
| 472 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 473 | static const struct i2c_device_id lp855x_ids[] = { |
| 474 | {"lp8550", LP8550}, |
| 475 | {"lp8551", LP8551}, |
| 476 | {"lp8552", LP8552}, |
| 477 | {"lp8553", LP8553}, |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 478 | {"lp8555", LP8555}, |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 479 | {"lp8556", LP8556}, |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 480 | {"lp8557", LP8557}, |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 481 | { } |
| 482 | }; |
| 483 | MODULE_DEVICE_TABLE(i2c, lp855x_ids); |
| 484 | |
| 485 | static struct i2c_driver lp855x_driver = { |
| 486 | .driver = { |
| 487 | .name = "lp855x", |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 488 | .of_match_table = of_match_ptr(lp855x_dt_ids), |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 489 | }, |
| 490 | .probe = lp855x_probe, |
Bill Pemberton | d1723fa | 2012-11-19 13:21:09 -0500 | [diff] [blame] | 491 | .remove = lp855x_remove, |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 492 | .id_table = lp855x_ids, |
| 493 | }; |
| 494 | |
| 495 | module_i2c_driver(lp855x_driver); |
| 496 | |
| 497 | MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver"); |
| 498 | MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>"); |
| 499 | MODULE_LICENSE("GPL"); |