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