blob: 2b9e2bbbb03e820d6c0286f7af1c541ad9dd6675 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Kim, Milo7be865a2012-03-23 15:02:01 -07002/*
3 * TI LP855x Backlight Driver
4 *
5 * Copyright (C) 2011 Texas Instruments
Kim, Milo7be865a2012-03-23 15:02:01 -07006 */
7
Hans de Goede6202b5d2021-11-02 23:55:04 +01008#include <linux/acpi.h>
Kim, Milo7be865a2012-03-23 15:02:01 -07009#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/i2c.h>
12#include <linux/backlight.h>
Brian Norris60255302016-06-10 12:39:57 -070013#include <linux/delay.h>
Kim, Milo7be865a2012-03-23 15:02:01 -070014#include <linux/err.h>
Kim, Milo4add0662013-04-29 16:18:06 -070015#include <linux/of.h>
Kim, Milof7f95052012-07-30 14:40:53 -070016#include <linux/platform_data/lp855x.h>
Kim, Milo8cc97642012-12-17 16:00:43 -080017#include <linux/pwm.h>
Sean Paul829b0302014-12-02 17:39:12 -080018#include <linux/regulator/consumer.h>
Kim, Milo7be865a2012-03-23 15:02:01 -070019
Kim, Milo26e8ccc2013-02-21 16:44:06 -080020/* 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 Kim5812c132013-11-12 15:08:57 -080028/* LP8555/7 Registers */
Kim, Milo26e8ccc2013-02-21 16:44:06 -080029#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 Kim5812c132013-11-12 15:08:57 -080035#define LP8555_EPROM_START 0x10
36#define LP8555_EPROM_END 0x7A
Kim, Milo26e8ccc2013-02-21 16:44:06 -080037#define LP8557_EPROM_START 0x10
38#define LP8557_EPROM_END 0x1E
Kim, Milo7be865a2012-03-23 15:02:01 -070039
Kim, Milo7be865a2012-03-23 15:02:01 -070040#define DEFAULT_BL_NAME "lcd-backlight"
41#define MAX_BRIGHTNESS 255
42
Kim, Milo0b818572013-04-29 16:18:03 -070043enum lp855x_brightness_ctrl_mode {
44 PWM_BASED = 1,
45 REGISTER_BASED,
46};
47
Kim, Milo68853bc2013-02-21 16:44:05 -080048struct 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 */
57struct 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, Milo7be865a2012-03-23 15:02:01 -070064struct lp855x {
65 const char *chipname;
66 enum lp855x_chip_id chip_id;
Kim, Milo0b818572013-04-29 16:18:03 -070067 enum lp855x_brightness_ctrl_mode mode;
Kim, Milo68853bc2013-02-21 16:44:05 -080068 struct lp855x_device_config *cfg;
Kim, Milo7be865a2012-03-23 15:02:01 -070069 struct i2c_client *client;
70 struct backlight_device *bl;
71 struct device *dev;
Kim, Milo7be865a2012-03-23 15:02:01 -070072 struct lp855x_platform_data *pdata;
Kim, Milo8cc97642012-12-17 16:00:43 -080073 struct pwm_device *pwm;
Milo Kimfe009172015-07-20 15:45:38 +090074 struct regulator *supply; /* regulator for VDD input */
Brian Norris60255302016-06-10 12:39:57 -070075 struct regulator *enable; /* regulator for EN/VDDIO input */
Kim, Milo7be865a2012-03-23 15:02:01 -070076};
77
Kim, Milo7be865a2012-03-23 15:02:01 -070078static int lp855x_write_byte(struct lp855x *lp, u8 reg, u8 data)
79{
Kim, Miloeaa4d022012-12-17 16:00:45 -080080 return i2c_smbus_write_byte_data(lp->client, reg, data);
Kim, Milo7be865a2012-03-23 15:02:01 -070081}
82
Kim, Milo26e8ccc2013-02-21 16:44:06 -080083static 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, Milo7be865a2012-03-23 15:02:01 -0700101static 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, Milo26e8ccc2013-02-21 16:44:06 -0800110 start = LP855X_EEPROM_START;
111 end = LP855X_EEPROM_END;
Kim, Milo7be865a2012-03-23 15:02:01 -0700112 break;
113 case LP8556:
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800114 start = LP8556_EPROM_START;
115 end = LP8556_EPROM_END;
116 break;
Milo Kim5812c132013-11-12 15:08:57 -0800117 case LP8555:
118 start = LP8555_EPROM_START;
119 end = LP8555_EPROM_END;
120 break;
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800121 case LP8557:
122 start = LP8557_EPROM_START;
123 end = LP8557_EPROM_END;
Kim, Milo7be865a2012-03-23 15:02:01 -0700124 break;
125 default:
126 return false;
127 }
128
Jingoo Han2ce23862014-01-23 15:54:32 -0800129 return addr >= start && addr <= end;
Kim, Milo7be865a2012-03-23 15:02:01 -0700130}
131
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800132static 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
139static 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, Milo68853bc2013-02-21 16:44:05 -0800146static struct lp855x_device_config lp855x_dev_cfg = {
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800147 .reg_brightness = LP855X_BRIGHTNESS_CTRL,
148 .reg_devicectrl = LP855X_DEVICE_CTRL,
149};
150
151static 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, Milo68853bc2013-02-21 16:44:05 -0800156};
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 */
168static int lp855x_configure(struct lp855x *lp)
Kim, Milo7be865a2012-03-23 15:02:01 -0700169{
170 u8 val, addr;
171 int i, ret;
172 struct lp855x_platform_data *pd = lp->pdata;
173
Kim, Milo68853bc2013-02-21 16:44:05 -0800174 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, Milo7be865a2012-03-23 15:02:01 -0700182 val = pd->initial_brightness;
Kim, Milo68853bc2013-02-21 16:44:05 -0800183 ret = lp855x_write_byte(lp, lp->cfg->reg_brightness, val);
Kim, Milo7be865a2012-03-23 15:02:01 -0700184 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800185 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700186
187 val = pd->device_control;
Kim, Milo68853bc2013-02-21 16:44:05 -0800188 ret = lp855x_write_byte(lp, lp->cfg->reg_devicectrl, val);
Kim, Milo7be865a2012-03-23 15:02:01 -0700189 if (ret)
Kim, Milo68853bc2013-02-21 16:44:05 -0800190 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700191
Kim, Miloc365e592013-04-29 16:18:05 -0700192 if (pd->size_program > 0) {
Kim, Milo7be865a2012-03-23 15:02:01 -0700193 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, Milo68853bc2013-02-21 16:44:05 -0800201 goto err;
Kim, Milo7be865a2012-03-23 15:02:01 -0700202 }
203 }
204
Kim, Milo68853bc2013-02-21 16:44:05 -0800205 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
215err:
Kim, Milo7be865a2012-03-23 15:02:01 -0700216 return ret;
217}
218
Kim, Milo8cc97642012-12-17 16:00:43 -0800219static 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 Brezillon6fbab832016-04-14 21:17:31 +0200232
233 /*
234 * FIXME: pwm_apply_args() should be removed when switching to
235 * the atomic PWM API.
236 */
237 pwm_apply_args(pwm);
Kim, Milo8cc97642012-12-17 16:00:43 -0800238 }
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, Milo7be865a2012-03-23 15:02:01 -0700247static int lp855x_bl_update_status(struct backlight_device *bl)
248{
249 struct lp855x *lp = bl_get_data(bl);
Sean Paul61c1c612015-05-11 13:32:05 -0700250 int brightness = bl->props.brightness;
Kim, Milo7be865a2012-03-23 15:02:01 -0700251
Shingo Nakao9f0a5112013-07-02 14:15:54 +0200252 if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
Sean Paul61c1c612015-05-11 13:32:05 -0700253 brightness = 0;
Kim, Milo7be865a2012-03-23 15:02:01 -0700254
Sean Paul61c1c612015-05-11 13:32:05 -0700255 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, Milo7be865a2012-03-23 15:02:01 -0700259
260 return 0;
261}
262
Kim, Milo7be865a2012-03-23 15:02:01 -0700263static const struct backlight_ops lp855x_bl_ops = {
264 .options = BL_CORE_SUSPENDRESUME,
265 .update_status = lp855x_bl_update_status,
Kim, Milo7be865a2012-03-23 15:02:01 -0700266};
267
268static 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, Milo600ffd32013-04-29 16:18:02 -0700273 const char *name = pdata->name ? : DEFAULT_BL_NAME;
Kim, Milo7be865a2012-03-23 15:02:01 -0700274
Werner Johansson9f7898f2015-08-27 10:41:15 -0700275 memset(&props, 0, sizeof(props));
Kim, Milo7be865a2012-03-23 15:02:01 -0700276 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 Han6255e8e2013-11-12 15:09:19 -0800284 bl = devm_backlight_device_register(lp->dev, name, lp->dev, lp,
Kim, Milo7be865a2012-03-23 15:02:01 -0700285 &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, Milo7be865a2012-03-23 15:02:01 -0700294static 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 Hana94cb122014-08-27 10:12:53 +0900298
Kim, Milo6a7aeb12013-04-29 16:17:52 -0700299 return scnprintf(buf, PAGE_SIZE, "%s\n", lp->chipname);
Kim, Milo7be865a2012-03-23 15:02:01 -0700300}
301
302static 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, Milo7be865a2012-03-23 15:02:01 -0700306 char *strmode = NULL;
307
Kim, Milo0b818572013-04-29 16:18:03 -0700308 if (lp->mode == PWM_BASED)
Kim, Milo7be865a2012-03-23 15:02:01 -0700309 strmode = "pwm based";
Kim, Milo0b818572013-04-29 16:18:03 -0700310 else if (lp->mode == REGISTER_BASED)
Kim, Milo7be865a2012-03-23 15:02:01 -0700311 strmode = "register based";
312
Kim, Milo6a7aeb12013-04-29 16:17:52 -0700313 return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
Kim, Milo7be865a2012-03-23 15:02:01 -0700314}
315
316static DEVICE_ATTR(chip_id, S_IRUGO, lp855x_get_chip_id, NULL);
317static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp855x_get_bl_ctl_mode, NULL);
318
319static struct attribute *lp855x_attributes[] = {
320 &dev_attr_chip_id.attr,
321 &dev_attr_bl_ctl_mode.attr,
322 NULL,
323};
324
325static const struct attribute_group lp855x_attr_group = {
326 .attrs = lp855x_attributes,
327};
328
Kim, Milo4add0662013-04-29 16:18:06 -0700329#ifdef CONFIG_OF
Sean Paul47726652014-12-02 17:39:11 -0800330static int lp855x_parse_dt(struct lp855x *lp)
Kim, Milo4add0662013-04-29 16:18:06 -0700331{
Sean Paul47726652014-12-02 17:39:11 -0800332 struct device *dev = lp->dev;
333 struct device_node *node = dev->of_node;
Hans de Goede6202b5d2021-11-02 23:55:04 +0100334 struct lp855x_platform_data *pdata = lp->pdata;
Kim, Milo4add0662013-04-29 16:18:06 -0700335 int rom_length;
336
337 if (!node) {
338 dev_err(dev, "no platform data\n");
339 return -EINVAL;
340 }
341
Kim, Milo4add0662013-04-29 16:18:06 -0700342 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 Cooka86854d2018-06-12 14:07:58 -0700354 rom = devm_kcalloc(dev, rom_length, sizeof(*rom), GFP_KERNEL);
Kim, Milo4add0662013-04-29 16:18:06 -0700355 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, Milo4add0662013-04-29 16:18:06 -0700368 return 0;
369}
370#else
Sean Paul47726652014-12-02 17:39:11 -0800371static int lp855x_parse_dt(struct lp855x *lp)
Kim, Milo4add0662013-04-29 16:18:06 -0700372{
373 return -EINVAL;
374}
375#endif
376
Hans de Goede6202b5d2021-11-02 23:55:04 +0100377static 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, Milo7be865a2012-03-23 15:02:01 -0700400static int lp855x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
401{
Hans de Goede6202b5d2021-11-02 23:55:04 +0100402 const struct acpi_device_id *acpi_id = NULL;
Hans de Goede92add942021-11-02 23:55:03 +0100403 struct device *dev = &cl->dev;
Kim, Milo7be865a2012-03-23 15:02:01 -0700404 struct lp855x *lp;
Kim, Milo7be865a2012-03-23 15:02:01 -0700405 int ret;
406
Kim, Milo7be865a2012-03-23 15:02:01 -0700407 if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
408 return -EIO;
409
Hans de Goede92add942021-11-02 23:55:03 +0100410 lp = devm_kzalloc(dev, sizeof(struct lp855x), GFP_KERNEL);
Kim, Milo7be865a2012-03-23 15:02:01 -0700411 if (!lp)
412 return -ENOMEM;
413
Sean Paul47726652014-12-02 17:39:11 -0800414 lp->client = cl;
Hans de Goede92add942021-11-02 23:55:03 +0100415 lp->dev = dev;
Hans de Goede92add942021-11-02 23:55:03 +0100416 lp->pdata = dev_get_platdata(dev);
Sean Paul47726652014-12-02 17:39:11 -0800417
Hans de Goede6202b5d2021-11-02 23:55:04 +0100418 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 Goededec5779e2021-11-02 23:55:02 +0100430 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 Paul47726652014-12-02 17:39:11 -0800446 if (!lp->pdata) {
Hans de Goede6202b5d2021-11-02 23:55:04 +0100447 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 Paul47726652014-12-02 17:39:11 -0800460 }
461
462 if (lp->pdata->period_ns > 0)
Kim, Milo0b818572013-04-29 16:18:03 -0700463 lp->mode = PWM_BASED;
464 else
465 lp->mode = REGISTER_BASED;
466
Hans de Goede92add942021-11-02 23:55:03 +0100467 lp->supply = devm_regulator_get(dev, "power");
Milo Kimfe009172015-07-20 15:45:38 +0900468 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 Goede92add942021-11-02 23:55:03 +0100474 lp->enable = devm_regulator_get_optional(dev, "enable");
Brian Norris60255302016-06-10 12:39:57 -0700475 if (IS_ERR(lp->enable)) {
476 ret = PTR_ERR(lp->enable);
477 if (ret == -ENODEV) {
478 lp->enable = NULL;
479 } else {
Hans de Goede92add942021-11-02 23:55:03 +0100480 return dev_err_probe(dev, ret, "getting enable regulator\n");
Brian Norris60255302016-06-10 12:39:57 -0700481 }
482 }
483
Milo Kimfe009172015-07-20 15:45:38 +0900484 if (lp->supply) {
485 ret = regulator_enable(lp->supply);
Sean Paul829b0302014-12-02 17:39:12 -0800486 if (ret < 0) {
Hans de Goede92add942021-11-02 23:55:03 +0100487 dev_err(dev, "failed to enable supply: %d\n", ret);
Sean Paul829b0302014-12-02 17:39:12 -0800488 return ret;
489 }
490 }
491
Brian Norris60255302016-06-10 12:39:57 -0700492 if (lp->enable) {
493 ret = regulator_enable(lp->enable);
494 if (ret < 0) {
Hans de Goede92add942021-11-02 23:55:03 +0100495 dev_err(dev, "failed to enable vddio: %d\n", ret);
Jon Hunterd8207c12020-02-24 14:07:48 +0000496 goto disable_supply;
Brian Norris60255302016-06-10 12:39:57 -0700497 }
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, Milo7be865a2012-03-23 15:02:01 -0700506 i2c_set_clientdata(cl, lp);
507
Kim, Milo68853bc2013-02-21 16:44:05 -0800508 ret = lp855x_configure(lp);
Kim, Milo7be865a2012-03-23 15:02:01 -0700509 if (ret) {
Hans de Goede92add942021-11-02 23:55:03 +0100510 dev_err(dev, "device config err: %d", ret);
Jon Hunterd8207c12020-02-24 14:07:48 +0000511 goto disable_vddio;
Kim, Milo7be865a2012-03-23 15:02:01 -0700512 }
513
514 ret = lp855x_backlight_register(lp);
515 if (ret) {
Hans de Goede92add942021-11-02 23:55:03 +0100516 dev_err(dev, "failed to register backlight. err: %d\n", ret);
Jon Hunterd8207c12020-02-24 14:07:48 +0000517 goto disable_vddio;
Kim, Milo7be865a2012-03-23 15:02:01 -0700518 }
519
Hans de Goede92add942021-11-02 23:55:03 +0100520 ret = sysfs_create_group(&dev->kobj, &lp855x_attr_group);
Kim, Milo7be865a2012-03-23 15:02:01 -0700521 if (ret) {
Hans de Goede92add942021-11-02 23:55:03 +0100522 dev_err(dev, "failed to register sysfs. err: %d\n", ret);
Jon Hunterd8207c12020-02-24 14:07:48 +0000523 goto disable_vddio;
Kim, Milo7be865a2012-03-23 15:02:01 -0700524 }
525
526 backlight_update_status(lp->bl);
Jon Hunterd8207c12020-02-24 14:07:48 +0000527
Kim, Milo7be865a2012-03-23 15:02:01 -0700528 return 0;
Jon Hunterd8207c12020-02-24 14:07:48 +0000529
530disable_vddio:
531 if (lp->enable)
532 regulator_disable(lp->enable);
533disable_supply:
534 if (lp->supply)
535 regulator_disable(lp->supply);
536
537 return ret;
Kim, Milo7be865a2012-03-23 15:02:01 -0700538}
539
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500540static int lp855x_remove(struct i2c_client *cl)
Kim, Milo7be865a2012-03-23 15:02:01 -0700541{
542 struct lp855x *lp = i2c_get_clientdata(cl);
543
544 lp->bl->props.brightness = 0;
545 backlight_update_status(lp->bl);
Jon Hunterd8207c12020-02-24 14:07:48 +0000546 if (lp->enable)
547 regulator_disable(lp->enable);
Milo Kimfe009172015-07-20 15:45:38 +0900548 if (lp->supply)
549 regulator_disable(lp->supply);
Kim, Milo7be865a2012-03-23 15:02:01 -0700550 sysfs_remove_group(&lp->dev->kobj, &lp855x_attr_group);
Kim, Milo7be865a2012-03-23 15:02:01 -0700551
552 return 0;
553}
554
Kim, Milo4add0662013-04-29 16:18:06 -0700555static 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 Kim5812c132013-11-12 15:08:57 -0800560 { .compatible = "ti,lp8555", },
Kim, Milo4add0662013-04-29 16:18:06 -0700561 { .compatible = "ti,lp8556", },
562 { .compatible = "ti,lp8557", },
563 { }
564};
565MODULE_DEVICE_TABLE(of, lp855x_dt_ids);
566
Kim, Milo7be865a2012-03-23 15:02:01 -0700567static const struct i2c_device_id lp855x_ids[] = {
568 {"lp8550", LP8550},
569 {"lp8551", LP8551},
570 {"lp8552", LP8552},
571 {"lp8553", LP8553},
Milo Kim5812c132013-11-12 15:08:57 -0800572 {"lp8555", LP8555},
Kim, Milo7be865a2012-03-23 15:02:01 -0700573 {"lp8556", LP8556},
Kim, Milo26e8ccc2013-02-21 16:44:06 -0800574 {"lp8557", LP8557},
Kim, Milo7be865a2012-03-23 15:02:01 -0700575 { }
576};
577MODULE_DEVICE_TABLE(i2c, lp855x_ids);
578
Hans de Goede6202b5d2021-11-02 23:55:04 +0100579#ifdef CONFIG_ACPI
580static 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};
585MODULE_DEVICE_TABLE(acpi, lp855x_acpi_match);
586#endif
587
Kim, Milo7be865a2012-03-23 15:02:01 -0700588static struct i2c_driver lp855x_driver = {
589 .driver = {
590 .name = "lp855x",
Kim, Milo4add0662013-04-29 16:18:06 -0700591 .of_match_table = of_match_ptr(lp855x_dt_ids),
Hans de Goede6202b5d2021-11-02 23:55:04 +0100592 .acpi_match_table = ACPI_PTR(lp855x_acpi_match),
Kim, Milo7be865a2012-03-23 15:02:01 -0700593 },
594 .probe = lp855x_probe,
Bill Pembertond1723fa2012-11-19 13:21:09 -0500595 .remove = lp855x_remove,
Kim, Milo7be865a2012-03-23 15:02:01 -0700596 .id_table = lp855x_ids,
597};
598
599module_i2c_driver(lp855x_driver);
600
601MODULE_DESCRIPTION("Texas Instruments LP855x Backlight driver");
602MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
603MODULE_LICENSE("GPL");