Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 2 | /* |
| 3 | * TI LP855x Backlight Driver |
| 4 | * |
| 5 | * Copyright (C) 2011 Texas Instruments |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Hans de Goede | 6202b5d | 2021-11-02 23:55:04 +0100 | [diff] [blame] | 8 | #include <linux/acpi.h> |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/i2c.h> |
| 12 | #include <linux/backlight.h> |
Brian Norris | 6025530 | 2016-06-10 12:39:57 -0700 | [diff] [blame] | 13 | #include <linux/delay.h> |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 14 | #include <linux/err.h> |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 15 | #include <linux/of.h> |
Kim, Milo | f7f9505 | 2012-07-30 14:40:53 -0700 | [diff] [blame] | 16 | #include <linux/platform_data/lp855x.h> |
Kim, Milo | 8cc9764 | 2012-12-17 16:00:43 -0800 | [diff] [blame] | 17 | #include <linux/pwm.h> |
Sean Paul | 829b030 | 2014-12-02 17:39:12 -0800 | [diff] [blame] | 18 | #include <linux/regulator/consumer.h> |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 19 | |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 20 | /* LP8550/1/2/3/6 Registers */ |
| 21 | #define LP855X_BRIGHTNESS_CTRL 0x00 |
| 22 | #define LP855X_DEVICE_CTRL 0x01 |
| 23 | #define LP855X_EEPROM_START 0xA0 |
| 24 | #define LP855X_EEPROM_END 0xA7 |
| 25 | #define LP8556_EPROM_START 0xA0 |
| 26 | #define LP8556_EPROM_END 0xAF |
| 27 | |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 28 | /* LP8555/7 Registers */ |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 29 | #define LP8557_BL_CMD 0x00 |
| 30 | #define LP8557_BL_MASK 0x01 |
| 31 | #define LP8557_BL_ON 0x01 |
| 32 | #define LP8557_BL_OFF 0x00 |
| 33 | #define LP8557_BRIGHTNESS_CTRL 0x04 |
| 34 | #define LP8557_CONFIG 0x10 |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 35 | #define LP8555_EPROM_START 0x10 |
| 36 | #define LP8555_EPROM_END 0x7A |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 37 | #define LP8557_EPROM_START 0x10 |
| 38 | #define LP8557_EPROM_END 0x1E |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 39 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 40 | #define DEFAULT_BL_NAME "lcd-backlight" |
| 41 | #define MAX_BRIGHTNESS 255 |
| 42 | |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 43 | enum lp855x_brightness_ctrl_mode { |
| 44 | PWM_BASED = 1, |
| 45 | REGISTER_BASED, |
| 46 | }; |
| 47 | |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 48 | struct lp855x; |
| 49 | |
| 50 | /* |
| 51 | * struct lp855x_device_config |
| 52 | * @pre_init_device: init device function call before updating the brightness |
| 53 | * @reg_brightness: register address for brigthenss control |
| 54 | * @reg_devicectrl: register address for device control |
| 55 | * @post_init_device: late init device function call |
| 56 | */ |
| 57 | struct lp855x_device_config { |
| 58 | int (*pre_init_device)(struct lp855x *); |
| 59 | u8 reg_brightness; |
| 60 | u8 reg_devicectrl; |
| 61 | int (*post_init_device)(struct lp855x *); |
| 62 | }; |
| 63 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 64 | struct lp855x { |
| 65 | const char *chipname; |
| 66 | enum lp855x_chip_id chip_id; |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 67 | enum lp855x_brightness_ctrl_mode mode; |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 68 | struct lp855x_device_config *cfg; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 69 | struct i2c_client *client; |
| 70 | struct backlight_device *bl; |
| 71 | struct device *dev; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 72 | struct lp855x_platform_data *pdata; |
Kim, Milo | 8cc9764 | 2012-12-17 16:00:43 -0800 | [diff] [blame] | 73 | struct pwm_device *pwm; |
Milo Kim | fe00917 | 2015-07-20 15:45:38 +0900 | [diff] [blame] | 74 | struct regulator *supply; /* regulator for VDD input */ |
Brian Norris | 6025530 | 2016-06-10 12:39:57 -0700 | [diff] [blame] | 75 | struct regulator *enable; /* regulator for EN/VDDIO input */ |
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 | if (lp->cfg->pre_init_device) { |
| 175 | ret = lp->cfg->pre_init_device(lp); |
| 176 | if (ret) { |
| 177 | dev_err(lp->dev, "pre init device err: %d\n", ret); |
| 178 | goto err; |
| 179 | } |
| 180 | } |
| 181 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 182 | val = pd->initial_brightness; |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 183 | ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 184 | if (ret) |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 185 | goto err; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 186 | |
| 187 | val = pd->device_control; |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 188 | ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 189 | if (ret) |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 190 | goto err; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 191 | |
Kim, Milo | c365e59 | 2013-04-29 16:18:05 -0700 | [diff] [blame] | 192 | if (pd->size_program > 0) { |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 193 | for (i = 0; i < pd->size_program; i++) { |
| 194 | addr = pd->rom_data[i].addr; |
| 195 | val = pd->rom_data[i].val; |
| 196 | if (!lp855x_is_valid_rom_area(lp, addr)) |
| 197 | continue; |
| 198 | |
| 199 | ret = lp855x_write_byte(lp, addr, val); |
| 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 | } |
| 204 | |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 205 | if (lp->cfg->post_init_device) { |
| 206 | ret = lp->cfg->post_init_device(lp); |
| 207 | if (ret) { |
| 208 | dev_err(lp->dev, "post init device err: %d\n", ret); |
| 209 | goto err; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | |
| 215 | err: |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 216 | return ret; |
| 217 | } |
| 218 | |
Kim, Milo | 8cc9764 | 2012-12-17 16:00:43 -0800 | [diff] [blame] | 219 | static void lp855x_pwm_ctrl(struct lp855x *lp, int br, int max_br) |
| 220 | { |
| 221 | unsigned int period = lp->pdata->period_ns; |
| 222 | unsigned int duty = br * period / max_br; |
| 223 | struct pwm_device *pwm; |
| 224 | |
| 225 | /* request pwm device with the consumer name */ |
| 226 | if (!lp->pwm) { |
| 227 | pwm = devm_pwm_get(lp->dev, lp->chipname); |
| 228 | if (IS_ERR(pwm)) |
| 229 | return; |
| 230 | |
| 231 | lp->pwm = pwm; |
Boris Brezillon | 6fbab83 | 2016-04-14 21:17:31 +0200 | [diff] [blame] | 232 | |
| 233 | /* |
| 234 | * FIXME: pwm_apply_args() should be removed when switching to |
| 235 | * the atomic PWM API. |
| 236 | */ |
| 237 | pwm_apply_args(pwm); |
Kim, Milo | 8cc9764 | 2012-12-17 16:00:43 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | pwm_config(lp->pwm, duty, period); |
| 241 | if (duty) |
| 242 | pwm_enable(lp->pwm); |
| 243 | else |
| 244 | pwm_disable(lp->pwm); |
| 245 | } |
| 246 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 247 | static int lp855x_bl_update_status(struct backlight_device *bl) |
| 248 | { |
| 249 | struct lp855x *lp = bl_get_data(bl); |
Sean Paul | 61c1c61 | 2015-05-11 13:32:05 -0700 | [diff] [blame] | 250 | int brightness = bl->props.brightness; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 251 | |
Shingo Nakao | 9f0a511 | 2013-07-02 14:15:54 +0200 | [diff] [blame] | 252 | if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK)) |
Sean Paul | 61c1c61 | 2015-05-11 13:32:05 -0700 | [diff] [blame] | 253 | brightness = 0; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 254 | |
Sean Paul | 61c1c61 | 2015-05-11 13:32:05 -0700 | [diff] [blame] | 255 | if (lp->mode == PWM_BASED) |
| 256 | lp855x_pwm_ctrl(lp, brightness, bl->props.max_brightness); |
| 257 | else if (lp->mode == REGISTER_BASED) |
| 258 | lp855x_write_byte(lp, lp->cfg->reg_brightness, (u8)brightness); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 263 | static const struct backlight_ops lp855x_bl_ops = { |
| 264 | .options = BL_CORE_SUSPENDRESUME, |
| 265 | .update_status = lp855x_bl_update_status, |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | static int lp855x_backlight_register(struct lp855x *lp) |
| 269 | { |
| 270 | struct backlight_device *bl; |
| 271 | struct backlight_properties props; |
| 272 | struct lp855x_platform_data *pdata = lp->pdata; |
Kim, Milo | 600ffd3 | 2013-04-29 16:18:02 -0700 | [diff] [blame] | 273 | const char *name = pdata->name ? : DEFAULT_BL_NAME; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 274 | |
Werner Johansson | 9f7898f | 2015-08-27 10:41:15 -0700 | [diff] [blame] | 275 | memset(&props, 0, sizeof(props)); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 276 | props.type = BACKLIGHT_PLATFORM; |
| 277 | props.max_brightness = MAX_BRIGHTNESS; |
| 278 | |
| 279 | if (pdata->initial_brightness > props.max_brightness) |
| 280 | pdata->initial_brightness = props.max_brightness; |
| 281 | |
| 282 | props.brightness = pdata->initial_brightness; |
| 283 | |
Jingoo Han | 6255e8e | 2013-11-12 15:09:19 -0800 | [diff] [blame] | 284 | bl = devm_backlight_device_register(lp->dev, name, lp->dev, lp, |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 285 | &lp855x_bl_ops, &props); |
| 286 | if (IS_ERR(bl)) |
| 287 | return PTR_ERR(bl); |
| 288 | |
| 289 | lp->bl = bl; |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 294 | static ssize_t lp855x_get_chip_id(struct device *dev, |
| 295 | struct device_attribute *attr, char *buf) |
| 296 | { |
| 297 | struct lp855x *lp = dev_get_drvdata(dev); |
Jingoo Han | a94cb12 | 2014-08-27 10:12:53 +0900 | [diff] [blame] | 298 | |
Kim, Milo | 6a7aeb1 | 2013-04-29 16:17:52 -0700 | [diff] [blame] | 299 | return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static ssize_t lp855x_get_bl_ctl_mode(struct device *dev, |
| 303 | struct device_attribute *attr, char *buf) |
| 304 | { |
| 305 | struct lp855x *lp = dev_get_drvdata(dev); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 306 | char *strmode = NULL; |
| 307 | |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 308 | if (lp->mode == PWM_BASED) |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 309 | strmode = "pwm based"; |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 310 | else if (lp->mode == REGISTER_BASED) |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 311 | strmode = "register based"; |
| 312 | |
Kim, Milo | 6a7aeb1 | 2013-04-29 16:17:52 -0700 | [diff] [blame] | 313 | return scnprintf(buf, PAGE_SIZE, "%s\n", strmode); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL); |
| 317 | static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL); |
| 318 | |
| 319 | static struct attribute *lp855x_attributes[] = { |
| 320 | &dev_attr_chip_id.attr, |
| 321 | &dev_attr_bl_ctl_mode.attr, |
| 322 | NULL, |
| 323 | }; |
| 324 | |
| 325 | static const struct attribute_group lp855x_attr_group = { |
| 326 | .attrs = lp855x_attributes, |
| 327 | }; |
| 328 | |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 329 | #ifdef CONFIG_OF |
Sean Paul | 4772665 | 2014-12-02 17:39:11 -0800 | [diff] [blame] | 330 | static int lp855x_parse_dt(struct lp855x *lp) |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 331 | { |
Sean Paul | 4772665 | 2014-12-02 17:39:11 -0800 | [diff] [blame] | 332 | struct device *dev = lp->dev; |
| 333 | struct device_node *node = dev->of_node; |
Hans de Goede | 6202b5d | 2021-11-02 23:55:04 +0100 | [diff] [blame] | 334 | struct lp855x_platform_data *pdata = lp->pdata; |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 335 | int rom_length; |
| 336 | |
| 337 | if (!node) { |
| 338 | dev_err(dev, "no platform data\n"); |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 342 | of_property_read_string(node, "bl-name", &pdata->name); |
| 343 | of_property_read_u8(node, "dev-ctrl", &pdata->device_control); |
| 344 | of_property_read_u8(node, "init-brt", &pdata->initial_brightness); |
| 345 | of_property_read_u32(node, "pwm-period", &pdata->period_ns); |
| 346 | |
| 347 | /* Fill ROM platform data if defined */ |
| 348 | rom_length = of_get_child_count(node); |
| 349 | if (rom_length > 0) { |
| 350 | struct lp855x_rom_data *rom; |
| 351 | struct device_node *child; |
| 352 | int i = 0; |
| 353 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 354 | rom = devm_kcalloc(dev, rom_length, sizeof(*rom), GFP_KERNEL); |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 355 | if (!rom) |
| 356 | return -ENOMEM; |
| 357 | |
| 358 | for_each_child_of_node(node, child) { |
| 359 | of_property_read_u8(child, "rom-addr", &rom[i].addr); |
| 360 | of_property_read_u8(child, "rom-val", &rom[i].val); |
| 361 | i++; |
| 362 | } |
| 363 | |
| 364 | pdata->size_program = rom_length; |
| 365 | pdata->rom_data = &rom[0]; |
| 366 | } |
| 367 | |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | #else |
Sean Paul | 4772665 | 2014-12-02 17:39:11 -0800 | [diff] [blame] | 371 | static int lp855x_parse_dt(struct lp855x *lp) |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 372 | { |
| 373 | return -EINVAL; |
| 374 | } |
| 375 | #endif |
| 376 | |
Hans de Goede | 6202b5d | 2021-11-02 23:55:04 +0100 | [diff] [blame] | 377 | static int lp855x_parse_acpi(struct lp855x *lp) |
| 378 | { |
| 379 | int ret; |
| 380 | |
| 381 | /* |
| 382 | * On ACPI the device has already been initialized by the firmware |
| 383 | * and is in register mode, so we can read back the settings from |
| 384 | * the registers. |
| 385 | */ |
| 386 | ret = i2c_smbus_read_byte_data(lp->client, lp->cfg->reg_brightness); |
| 387 | if (ret < 0) |
| 388 | return ret; |
| 389 | |
| 390 | lp->pdata->initial_brightness = ret; |
| 391 | |
| 392 | ret = i2c_smbus_read_byte_data(lp->client, lp->cfg->reg_devicectrl); |
| 393 | if (ret < 0) |
| 394 | return ret; |
| 395 | |
| 396 | lp->pdata->device_control = ret; |
| 397 | return 0; |
| 398 | } |
| 399 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 400 | static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id) |
| 401 | { |
Hans de Goede | 6202b5d | 2021-11-02 23:55:04 +0100 | [diff] [blame] | 402 | const struct acpi_device_id *acpi_id = NULL; |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 403 | struct device *dev = &cl->dev; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 404 | struct lp855x *lp; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 405 | int ret; |
| 406 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 407 | if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) |
| 408 | return -EIO; |
| 409 | |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 410 | lp = devm_kzalloc(dev, sizeof(struct lp855x), GFP_KERNEL); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 411 | if (!lp) |
| 412 | return -ENOMEM; |
| 413 | |
Sean Paul | 4772665 | 2014-12-02 17:39:11 -0800 | [diff] [blame] | 414 | lp->client = cl; |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 415 | lp->dev = dev; |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 416 | lp->pdata = dev_get_platdata(dev); |
Sean Paul | 4772665 | 2014-12-02 17:39:11 -0800 | [diff] [blame] | 417 | |
Hans de Goede | 6202b5d | 2021-11-02 23:55:04 +0100 | [diff] [blame] | 418 | if (id) { |
| 419 | lp->chipname = id->name; |
| 420 | lp->chip_id = id->driver_data; |
| 421 | } else { |
| 422 | acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev); |
| 423 | if (!acpi_id) |
| 424 | return -ENODEV; |
| 425 | |
| 426 | lp->chipname = acpi_id->id; |
| 427 | lp->chip_id = acpi_id->driver_data; |
| 428 | } |
| 429 | |
Hans de Goede | dec5779e | 2021-11-02 23:55:02 +0100 | [diff] [blame] | 430 | switch (lp->chip_id) { |
| 431 | case LP8550: |
| 432 | case LP8551: |
| 433 | case LP8552: |
| 434 | case LP8553: |
| 435 | case LP8556: |
| 436 | lp->cfg = &lp855x_dev_cfg; |
| 437 | break; |
| 438 | case LP8555: |
| 439 | case LP8557: |
| 440 | lp->cfg = &lp8557_dev_cfg; |
| 441 | break; |
| 442 | default: |
| 443 | return -EINVAL; |
| 444 | } |
| 445 | |
Sean Paul | 4772665 | 2014-12-02 17:39:11 -0800 | [diff] [blame] | 446 | if (!lp->pdata) { |
Hans de Goede | 6202b5d | 2021-11-02 23:55:04 +0100 | [diff] [blame] | 447 | lp->pdata = devm_kzalloc(dev, sizeof(*lp->pdata), GFP_KERNEL); |
| 448 | if (!lp->pdata) |
| 449 | return -ENOMEM; |
| 450 | |
| 451 | if (id) { |
| 452 | ret = lp855x_parse_dt(lp); |
| 453 | if (ret < 0) |
| 454 | return ret; |
| 455 | } else { |
| 456 | ret = lp855x_parse_acpi(lp); |
| 457 | if (ret < 0) |
| 458 | return ret; |
| 459 | } |
Sean Paul | 4772665 | 2014-12-02 17:39:11 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | if (lp->pdata->period_ns > 0) |
Kim, Milo | 0b81857 | 2013-04-29 16:18:03 -0700 | [diff] [blame] | 463 | lp->mode = PWM_BASED; |
| 464 | else |
| 465 | lp->mode = REGISTER_BASED; |
| 466 | |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 467 | lp->supply = devm_regulator_get(dev, "power"); |
Milo Kim | fe00917 | 2015-07-20 15:45:38 +0900 | [diff] [blame] | 468 | if (IS_ERR(lp->supply)) { |
| 469 | if (PTR_ERR(lp->supply) == -EPROBE_DEFER) |
| 470 | return -EPROBE_DEFER; |
| 471 | lp->supply = NULL; |
| 472 | } |
| 473 | |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 474 | lp->enable = devm_regulator_get_optional(dev, "enable"); |
Brian Norris | 6025530 | 2016-06-10 12:39:57 -0700 | [diff] [blame] | 475 | if (IS_ERR(lp->enable)) { |
| 476 | ret = PTR_ERR(lp->enable); |
| 477 | if (ret == -ENODEV) { |
| 478 | lp->enable = NULL; |
| 479 | } else { |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 480 | return dev_err_probe(dev, ret, "getting enable regulator\n"); |
Brian Norris | 6025530 | 2016-06-10 12:39:57 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
Milo Kim | fe00917 | 2015-07-20 15:45:38 +0900 | [diff] [blame] | 484 | if (lp->supply) { |
| 485 | ret = regulator_enable(lp->supply); |
Sean Paul | 829b030 | 2014-12-02 17:39:12 -0800 | [diff] [blame] | 486 | if (ret < 0) { |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 487 | dev_err(dev, "failed to enable supply: %d\n", ret); |
Sean Paul | 829b030 | 2014-12-02 17:39:12 -0800 | [diff] [blame] | 488 | return ret; |
| 489 | } |
| 490 | } |
| 491 | |
Brian Norris | 6025530 | 2016-06-10 12:39:57 -0700 | [diff] [blame] | 492 | if (lp->enable) { |
| 493 | ret = regulator_enable(lp->enable); |
| 494 | if (ret < 0) { |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 495 | dev_err(dev, "failed to enable vddio: %d\n", ret); |
Jon Hunter | d8207c1 | 2020-02-24 14:07:48 +0000 | [diff] [blame] | 496 | goto disable_supply; |
Brian Norris | 6025530 | 2016-06-10 12:39:57 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /* |
| 500 | * LP8555 datasheet says t_RESPONSE (time between VDDIO and |
| 501 | * I2C) is 1ms. |
| 502 | */ |
| 503 | usleep_range(1000, 2000); |
| 504 | } |
| 505 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 506 | i2c_set_clientdata(cl, lp); |
| 507 | |
Kim, Milo | 68853bc | 2013-02-21 16:44:05 -0800 | [diff] [blame] | 508 | ret = lp855x_configure(lp); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 509 | if (ret) { |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 510 | dev_err(dev, "device config err: %d", ret); |
Jon Hunter | d8207c1 | 2020-02-24 14:07:48 +0000 | [diff] [blame] | 511 | goto disable_vddio; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | ret = lp855x_backlight_register(lp); |
| 515 | if (ret) { |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 516 | dev_err(dev, "failed to register backlight. err: %d\n", ret); |
Jon Hunter | d8207c1 | 2020-02-24 14:07:48 +0000 | [diff] [blame] | 517 | goto disable_vddio; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 520 | ret = sysfs_create_group(&dev->kobj, &lp855x_attr_group); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 521 | if (ret) { |
Hans de Goede | 92add94 | 2021-11-02 23:55:03 +0100 | [diff] [blame] | 522 | dev_err(dev, "failed to register sysfs. err: %d\n", ret); |
Jon Hunter | d8207c1 | 2020-02-24 14:07:48 +0000 | [diff] [blame] | 523 | goto disable_vddio; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | backlight_update_status(lp->bl); |
Jon Hunter | d8207c1 | 2020-02-24 14:07:48 +0000 | [diff] [blame] | 527 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 528 | return 0; |
Jon Hunter | d8207c1 | 2020-02-24 14:07:48 +0000 | [diff] [blame] | 529 | |
| 530 | disable_vddio: |
| 531 | if (lp->enable) |
| 532 | regulator_disable(lp->enable); |
| 533 | disable_supply: |
| 534 | if (lp->supply) |
| 535 | regulator_disable(lp->supply); |
| 536 | |
| 537 | return ret; |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Bill Pemberton | 7e4b9d0 | 2012-11-19 13:26:34 -0500 | [diff] [blame] | 540 | static int lp855x_remove(struct i2c_client *cl) |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 541 | { |
| 542 | struct lp855x *lp = i2c_get_clientdata(cl); |
| 543 | |
| 544 | lp->bl->props.brightness = 0; |
| 545 | backlight_update_status(lp->bl); |
Jon Hunter | d8207c1 | 2020-02-24 14:07:48 +0000 | [diff] [blame] | 546 | if (lp->enable) |
| 547 | regulator_disable(lp->enable); |
Milo Kim | fe00917 | 2015-07-20 15:45:38 +0900 | [diff] [blame] | 548 | if (lp->supply) |
| 549 | regulator_disable(lp->supply); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 550 | sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group); |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 555 | static const struct of_device_id lp855x_dt_ids[] = { |
| 556 | { .compatible = "ti,lp8550", }, |
| 557 | { .compatible = "ti,lp8551", }, |
| 558 | { .compatible = "ti,lp8552", }, |
| 559 | { .compatible = "ti,lp8553", }, |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 560 | { .compatible = "ti,lp8555", }, |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 561 | { .compatible = "ti,lp8556", }, |
| 562 | { .compatible = "ti,lp8557", }, |
| 563 | { } |
| 564 | }; |
| 565 | MODULE_DEVICE_TABLE(of, lp855x_dt_ids); |
| 566 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 567 | static const struct i2c_device_id lp855x_ids[] = { |
| 568 | {"lp8550", LP8550}, |
| 569 | {"lp8551", LP8551}, |
| 570 | {"lp8552", LP8552}, |
| 571 | {"lp8553", LP8553}, |
Milo Kim | 5812c13 | 2013-11-12 15:08:57 -0800 | [diff] [blame] | 572 | {"lp8555", LP8555}, |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 573 | {"lp8556", LP8556}, |
Kim, Milo | 26e8ccc | 2013-02-21 16:44:06 -0800 | [diff] [blame] | 574 | {"lp8557", LP8557}, |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 575 | { } |
| 576 | }; |
| 577 | MODULE_DEVICE_TABLE(i2c, lp855x_ids); |
| 578 | |
Hans de Goede | 6202b5d | 2021-11-02 23:55:04 +0100 | [diff] [blame] | 579 | #ifdef CONFIG_ACPI |
| 580 | static const struct acpi_device_id lp855x_acpi_match[] = { |
| 581 | /* Xiaomi specific HID used for the LP8556 on the Mi Pad 2 */ |
| 582 | { "XMCC0001", LP8556 }, |
| 583 | { } |
| 584 | }; |
| 585 | MODULE_DEVICE_TABLE(acpi, lp855x_acpi_match); |
| 586 | #endif |
| 587 | |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 588 | static struct i2c_driver lp855x_driver = { |
| 589 | .driver = { |
| 590 | .name = "lp855x", |
Kim, Milo | 4add066 | 2013-04-29 16:18:06 -0700 | [diff] [blame] | 591 | .of_match_table = of_match_ptr(lp855x_dt_ids), |
Hans de Goede | 6202b5d | 2021-11-02 23:55:04 +0100 | [diff] [blame] | 592 | .acpi_match_table = ACPI_PTR(lp855x_acpi_match), |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 593 | }, |
| 594 | .probe = lp855x_probe, |
Bill Pemberton | d1723fa | 2012-11-19 13:21:09 -0500 | [diff] [blame] | 595 | .remove = lp855x_remove, |
Kim, Milo | 7be865a | 2012-03-23 15:02:01 -0700 | [diff] [blame] | 596 | .id_table = lp855x_ids, |
| 597 | }; |
| 598 | |
| 599 | module_i2c_driver(lp855x_driver); |
| 600 | |
| 601 | MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver"); |
| 602 | MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>"); |
| 603 | MODULE_LICENSE("GPL"); |