libnvdimm/altmap: Track namespace boundaries in altmap
[linux-2.6-block.git] / drivers / leds / leds-lp8860.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
7a8685ac
DM
2/*
3 * TI LP8860 4-Channel LED Driver
4 *
5 * Copyright (C) 2014 Texas Instruments
6 *
7 * Author: Dan Murphy <dmurphy@ti.com>
7a8685ac
DM
8 */
9
10#include <linux/i2c.h>
11#include <linux/init.h>
12#include <linux/leds.h>
13#include <linux/regmap.h>
14#include <linux/regulator/consumer.h>
15#include <linux/module.h>
16#include <linux/mutex.h>
17#include <linux/of.h>
18#include <linux/of_gpio.h>
19#include <linux/gpio/consumer.h>
20#include <linux/slab.h>
c6b218c9 21#include <uapi/linux/uleds.h>
7a8685ac
DM
22
23#define LP8860_DISP_CL1_BRT_MSB 0x00
24#define LP8860_DISP_CL1_BRT_LSB 0x01
25#define LP8860_DISP_CL1_CURR_MSB 0x02
26#define LP8860_DISP_CL1_CURR_LSB 0x03
27#define LP8860_CL2_BRT_MSB 0x04
28#define LP8860_CL2_BRT_LSB 0x05
29#define LP8860_CL2_CURRENT 0x06
30#define LP8860_CL3_BRT_MSB 0x07
31#define LP8860_CL3_BRT_LSB 0x08
32#define LP8860_CL3_CURRENT 0x09
33#define LP8860_CL4_BRT_MSB 0x0a
34#define LP8860_CL4_BRT_LSB 0x0b
35#define LP8860_CL4_CURRENT 0x0c
36#define LP8860_CONFIG 0x0d
37#define LP8860_STATUS 0x0e
38#define LP8860_FAULT 0x0f
39#define LP8860_LED_FAULT 0x10
40#define LP8860_FAULT_CLEAR 0x11
41#define LP8860_ID 0x12
42#define LP8860_TEMP_MSB 0x13
43#define LP8860_TEMP_LSB 0x14
44#define LP8860_DISP_LED_CURR_MSB 0x15
45#define LP8860_DISP_LED_CURR_LSB 0x16
46#define LP8860_DISP_LED_PWM_MSB 0x17
47#define LP8860_DISP_LED_PWM_LSB 0x18
48#define LP8860_EEPROM_CNTRL 0x19
49#define LP8860_EEPROM_UNLOCK 0x1a
50
51#define LP8860_EEPROM_REG_0 0x60
52#define LP8860_EEPROM_REG_1 0x61
53#define LP8860_EEPROM_REG_2 0x62
54#define LP8860_EEPROM_REG_3 0x63
55#define LP8860_EEPROM_REG_4 0x64
56#define LP8860_EEPROM_REG_5 0x65
57#define LP8860_EEPROM_REG_6 0x66
58#define LP8860_EEPROM_REG_7 0x67
59#define LP8860_EEPROM_REG_8 0x68
60#define LP8860_EEPROM_REG_9 0x69
61#define LP8860_EEPROM_REG_10 0x6a
62#define LP8860_EEPROM_REG_11 0x6b
63#define LP8860_EEPROM_REG_12 0x6c
64#define LP8860_EEPROM_REG_13 0x6d
65#define LP8860_EEPROM_REG_14 0x6e
66#define LP8860_EEPROM_REG_15 0x6f
67#define LP8860_EEPROM_REG_16 0x70
68#define LP8860_EEPROM_REG_17 0x71
69#define LP8860_EEPROM_REG_18 0x72
70#define LP8860_EEPROM_REG_19 0x73
71#define LP8860_EEPROM_REG_20 0x74
72#define LP8860_EEPROM_REG_21 0x75
73#define LP8860_EEPROM_REG_22 0x76
74#define LP8860_EEPROM_REG_23 0x77
75#define LP8860_EEPROM_REG_24 0x78
76
77#define LP8860_LOCK_EEPROM 0x00
78#define LP8860_UNLOCK_EEPROM 0x01
79#define LP8860_PROGRAM_EEPROM 0x02
80#define LP8860_EEPROM_CODE_1 0x08
81#define LP8860_EEPROM_CODE_2 0xba
82#define LP8860_EEPROM_CODE_3 0xef
83
84#define LP8860_CLEAR_FAULTS 0x01
85
7a8685ac
DM
86/**
87 * struct lp8860_led -
88 * @lock - Lock for reading/writing the device
7a8685ac
DM
89 * @client - Pointer to the I2C client
90 * @led_dev - led class device pointer
91 * @regmap - Devices register map
92 * @eeprom_regmap - EEPROM register map
93 * @enable_gpio - VDDIO/EN gpio to enable communication interface
94 * @regulator - LED supply regulator pointer
7a8685ac 95 * @label - LED label
8a7a76c8 96 */
7a8685ac
DM
97struct lp8860_led {
98 struct mutex lock;
7a8685ac
DM
99 struct i2c_client *client;
100 struct led_classdev led_dev;
101 struct regmap *regmap;
102 struct regmap *eeprom_regmap;
103 struct gpio_desc *enable_gpio;
104 struct regulator *regulator;
c6b218c9 105 char label[LED_MAX_NAME_SIZE];
7a8685ac
DM
106};
107
108struct lp8860_eeprom_reg {
109 uint8_t reg;
110 uint8_t value;
111};
112
113static struct lp8860_eeprom_reg lp8860_eeprom_disp_regs[] = {
114 { LP8860_EEPROM_REG_0, 0xed },
115 { LP8860_EEPROM_REG_1, 0xdf },
116 { LP8860_EEPROM_REG_2, 0xdc },
117 { LP8860_EEPROM_REG_3, 0xf0 },
118 { LP8860_EEPROM_REG_4, 0xdf },
119 { LP8860_EEPROM_REG_5, 0xe5 },
120 { LP8860_EEPROM_REG_6, 0xf2 },
121 { LP8860_EEPROM_REG_7, 0x77 },
122 { LP8860_EEPROM_REG_8, 0x77 },
123 { LP8860_EEPROM_REG_9, 0x71 },
124 { LP8860_EEPROM_REG_10, 0x3f },
125 { LP8860_EEPROM_REG_11, 0xb7 },
126 { LP8860_EEPROM_REG_12, 0x17 },
127 { LP8860_EEPROM_REG_13, 0xef },
128 { LP8860_EEPROM_REG_14, 0xb0 },
129 { LP8860_EEPROM_REG_15, 0x87 },
130 { LP8860_EEPROM_REG_16, 0xce },
131 { LP8860_EEPROM_REG_17, 0x72 },
132 { LP8860_EEPROM_REG_18, 0xe5 },
133 { LP8860_EEPROM_REG_19, 0xdf },
134 { LP8860_EEPROM_REG_20, 0x35 },
135 { LP8860_EEPROM_REG_21, 0x06 },
136 { LP8860_EEPROM_REG_22, 0xdc },
137 { LP8860_EEPROM_REG_23, 0x88 },
138 { LP8860_EEPROM_REG_24, 0x3E },
139};
140
141static int lp8860_unlock_eeprom(struct lp8860_led *led, int lock)
142{
143 int ret;
144
145 mutex_lock(&led->lock);
146
147 if (lock == LP8860_UNLOCK_EEPROM) {
148 ret = regmap_write(led->regmap,
149 LP8860_EEPROM_UNLOCK,
150 LP8860_EEPROM_CODE_1);
151 if (ret) {
152 dev_err(&led->client->dev, "EEPROM Unlock failed\n");
153 goto out;
154 }
155
156 ret = regmap_write(led->regmap,
157 LP8860_EEPROM_UNLOCK,
158 LP8860_EEPROM_CODE_2);
159 if (ret) {
160 dev_err(&led->client->dev, "EEPROM Unlock failed\n");
161 goto out;
162 }
163 ret = regmap_write(led->regmap,
164 LP8860_EEPROM_UNLOCK,
165 LP8860_EEPROM_CODE_3);
166 if (ret) {
167 dev_err(&led->client->dev, "EEPROM Unlock failed\n");
168 goto out;
169 }
170 } else {
171 ret = regmap_write(led->regmap,
172 LP8860_EEPROM_UNLOCK,
173 LP8860_LOCK_EEPROM);
174 }
175
176out:
177 mutex_unlock(&led->lock);
178 return ret;
179}
180
181static int lp8860_fault_check(struct lp8860_led *led)
182{
183 int ret, fault;
184 unsigned int read_buf;
185
186 ret = regmap_read(led->regmap, LP8860_LED_FAULT, &read_buf);
187 if (ret)
188 goto out;
189
190 fault = read_buf;
191
192 ret = regmap_read(led->regmap, LP8860_FAULT, &read_buf);
193 if (ret)
194 goto out;
195
196 fault |= read_buf;
197
198 /* Attempt to clear any faults */
199 if (fault)
200 ret = regmap_write(led->regmap, LP8860_FAULT_CLEAR,
201 LP8860_CLEAR_FAULTS);
202out:
203 return ret;
204}
205
7c4b10a2
AL
206static int lp8860_brightness_set(struct led_classdev *led_cdev,
207 enum led_brightness brt_val)
7a8685ac 208{
7c4b10a2
AL
209 struct lp8860_led *led =
210 container_of(led_cdev, struct lp8860_led, led_dev);
211 int disp_brightness = brt_val * 255;
7a8685ac 212 int ret;
7a8685ac
DM
213
214 mutex_lock(&led->lock);
215
216 ret = lp8860_fault_check(led);
217 if (ret) {
218 dev_err(&led->client->dev, "Cannot read/clear faults\n");
219 goto out;
220 }
221
222 ret = regmap_write(led->regmap, LP8860_DISP_CL1_BRT_MSB,
223 (disp_brightness & 0xff00) >> 8);
224 if (ret) {
225 dev_err(&led->client->dev, "Cannot write CL1 MSB\n");
226 goto out;
227 }
228
229 ret = regmap_write(led->regmap, LP8860_DISP_CL1_BRT_LSB,
230 disp_brightness & 0xff);
231 if (ret) {
232 dev_err(&led->client->dev, "Cannot write CL1 LSB\n");
233 goto out;
234 }
235out:
236 mutex_unlock(&led->lock);
7c4b10a2 237 return ret;
7a8685ac
DM
238}
239
240static int lp8860_init(struct lp8860_led *led)
241{
242 unsigned int read_buf;
243 int ret, i, reg_count;
244
b12ef03a
DM
245 if (led->regulator) {
246 ret = regulator_enable(led->regulator);
247 if (ret) {
248 dev_err(&led->client->dev,
249 "Failed to enable regulator\n");
250 return ret;
251 }
252 }
253
7a8685ac
DM
254 if (led->enable_gpio)
255 gpiod_direction_output(led->enable_gpio, 1);
256
257 ret = lp8860_fault_check(led);
258 if (ret)
259 goto out;
260
261 ret = regmap_read(led->regmap, LP8860_STATUS, &read_buf);
262 if (ret)
263 goto out;
264
265 ret = lp8860_unlock_eeprom(led, LP8860_UNLOCK_EEPROM);
266 if (ret) {
267 dev_err(&led->client->dev, "Failed unlocking EEPROM\n");
268 goto out;
269 }
270
271 reg_count = ARRAY_SIZE(lp8860_eeprom_disp_regs) / sizeof(lp8860_eeprom_disp_regs[0]);
272 for (i = 0; i < reg_count; i++) {
273 ret = regmap_write(led->eeprom_regmap,
274 lp8860_eeprom_disp_regs[i].reg,
275 lp8860_eeprom_disp_regs[i].value);
276 if (ret) {
277 dev_err(&led->client->dev, "Failed writing EEPROM\n");
278 goto out;
279 }
280 }
281
282 ret = lp8860_unlock_eeprom(led, LP8860_LOCK_EEPROM);
283 if (ret)
284 goto out;
285
286 ret = regmap_write(led->regmap,
287 LP8860_EEPROM_CNTRL,
288 LP8860_PROGRAM_EEPROM);
b12ef03a 289 if (ret) {
7a8685ac 290 dev_err(&led->client->dev, "Failed programming EEPROM\n");
b12ef03a
DM
291 goto out;
292 }
293
294 return ret;
295
7a8685ac
DM
296out:
297 if (ret)
298 if (led->enable_gpio)
299 gpiod_direction_output(led->enable_gpio, 0);
b12ef03a
DM
300
301 if (led->regulator) {
302 ret = regulator_disable(led->regulator);
303 if (ret)
304 dev_err(&led->client->dev,
305 "Failed to disable regulator\n");
306 }
307
7a8685ac
DM
308 return ret;
309}
310
de88e71a 311static const struct reg_default lp8860_reg_defs[] = {
7a8685ac
DM
312 { LP8860_DISP_CL1_BRT_MSB, 0x00},
313 { LP8860_DISP_CL1_BRT_LSB, 0x00},
314 { LP8860_DISP_CL1_CURR_MSB, 0x00},
315 { LP8860_DISP_CL1_CURR_LSB, 0x00},
316 { LP8860_CL2_BRT_MSB, 0x00},
317 { LP8860_CL2_BRT_LSB, 0x00},
318 { LP8860_CL2_CURRENT, 0x00},
319 { LP8860_CL3_BRT_MSB, 0x00},
320 { LP8860_CL3_BRT_LSB, 0x00},
321 { LP8860_CL3_CURRENT, 0x00},
322 { LP8860_CL4_BRT_MSB, 0x00},
323 { LP8860_CL4_BRT_LSB, 0x00},
324 { LP8860_CL4_CURRENT, 0x00},
325 { LP8860_CONFIG, 0x00},
326 { LP8860_FAULT_CLEAR, 0x00},
327 { LP8860_EEPROM_CNTRL, 0x80},
328 { LP8860_EEPROM_UNLOCK, 0x00},
329};
330
331static const struct regmap_config lp8860_regmap_config = {
332 .reg_bits = 8,
333 .val_bits = 8,
334
335 .max_register = LP8860_EEPROM_UNLOCK,
336 .reg_defaults = lp8860_reg_defs,
337 .num_reg_defaults = ARRAY_SIZE(lp8860_reg_defs),
338 .cache_type = REGCACHE_NONE,
339};
340
de88e71a 341static const struct reg_default lp8860_eeprom_defs[] = {
7a8685ac
DM
342 { LP8860_EEPROM_REG_0, 0x00 },
343 { LP8860_EEPROM_REG_1, 0x00 },
344 { LP8860_EEPROM_REG_2, 0x00 },
345 { LP8860_EEPROM_REG_3, 0x00 },
346 { LP8860_EEPROM_REG_4, 0x00 },
347 { LP8860_EEPROM_REG_5, 0x00 },
348 { LP8860_EEPROM_REG_6, 0x00 },
349 { LP8860_EEPROM_REG_7, 0x00 },
350 { LP8860_EEPROM_REG_8, 0x00 },
351 { LP8860_EEPROM_REG_9, 0x00 },
352 { LP8860_EEPROM_REG_10, 0x00 },
353 { LP8860_EEPROM_REG_11, 0x00 },
354 { LP8860_EEPROM_REG_12, 0x00 },
355 { LP8860_EEPROM_REG_13, 0x00 },
356 { LP8860_EEPROM_REG_14, 0x00 },
357 { LP8860_EEPROM_REG_15, 0x00 },
358 { LP8860_EEPROM_REG_16, 0x00 },
359 { LP8860_EEPROM_REG_17, 0x00 },
360 { LP8860_EEPROM_REG_18, 0x00 },
361 { LP8860_EEPROM_REG_19, 0x00 },
362 { LP8860_EEPROM_REG_20, 0x00 },
363 { LP8860_EEPROM_REG_21, 0x00 },
364 { LP8860_EEPROM_REG_22, 0x00 },
365 { LP8860_EEPROM_REG_23, 0x00 },
366 { LP8860_EEPROM_REG_24, 0x00 },
367};
368
369static const struct regmap_config lp8860_eeprom_regmap_config = {
370 .reg_bits = 8,
371 .val_bits = 8,
372
373 .max_register = LP8860_EEPROM_REG_24,
374 .reg_defaults = lp8860_eeprom_defs,
375 .num_reg_defaults = ARRAY_SIZE(lp8860_eeprom_defs),
376 .cache_type = REGCACHE_NONE,
377};
378
379static int lp8860_probe(struct i2c_client *client,
380 const struct i2c_device_id *id)
381{
382 int ret;
383 struct lp8860_led *led;
384 struct device_node *np = client->dev.of_node;
c6b218c9
DM
385 struct device_node *child_node;
386 const char *name;
7a8685ac
DM
387
388 led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL);
389 if (!led)
390 return -ENOMEM;
391
c6b218c9 392 for_each_available_child_of_node(np, child_node) {
50aa46c4
DM
393 led->led_dev.default_trigger = of_get_property(child_node,
394 "linux,default-trigger",
395 NULL);
396
c6b218c9
DM
397 ret = of_property_read_string(child_node, "label", &name);
398 if (!ret)
399 snprintf(led->label, sizeof(led->label), "%s:%s",
400 id->name, name);
401 else
402 snprintf(led->label, sizeof(led->label),
403 "%s::display_cluster", id->name);
7a8685ac
DM
404 }
405
eb2294c3
UKK
406 led->enable_gpio = devm_gpiod_get_optional(&client->dev,
407 "enable", GPIOD_OUT_LOW);
408 if (IS_ERR(led->enable_gpio)) {
409 ret = PTR_ERR(led->enable_gpio);
410 dev_err(&client->dev, "Failed to get enable gpio: %d\n", ret);
411 return ret;
412 }
7a8685ac
DM
413
414 led->regulator = devm_regulator_get(&client->dev, "vled");
415 if (IS_ERR(led->regulator))
416 led->regulator = NULL;
417
418 led->client = client;
419 led->led_dev.name = led->label;
7c4b10a2 420 led->led_dev.brightness_set_blocking = lp8860_brightness_set;
7a8685ac
DM
421
422 mutex_init(&led->lock);
7a8685ac
DM
423
424 i2c_set_clientdata(client, led);
425
426 led->regmap = devm_regmap_init_i2c(client, &lp8860_regmap_config);
427 if (IS_ERR(led->regmap)) {
428 ret = PTR_ERR(led->regmap);
429 dev_err(&client->dev, "Failed to allocate register map: %d\n",
430 ret);
431 return ret;
432 }
433
434 led->eeprom_regmap = devm_regmap_init_i2c(client, &lp8860_eeprom_regmap_config);
435 if (IS_ERR(led->eeprom_regmap)) {
436 ret = PTR_ERR(led->eeprom_regmap);
437 dev_err(&client->dev, "Failed to allocate register map: %d\n",
438 ret);
439 return ret;
440 }
441
442 ret = lp8860_init(led);
443 if (ret)
444 return ret;
445
a2169c9b 446 ret = devm_led_classdev_register(&client->dev, &led->led_dev);
7a8685ac
DM
447 if (ret) {
448 dev_err(&client->dev, "led register err: %d\n", ret);
449 return ret;
450 }
451
452 return 0;
453}
454
455static int lp8860_remove(struct i2c_client *client)
456{
457 struct lp8860_led *led = i2c_get_clientdata(client);
458 int ret;
459
7a8685ac
DM
460 if (led->enable_gpio)
461 gpiod_direction_output(led->enable_gpio, 0);
462
463 if (led->regulator) {
464 ret = regulator_disable(led->regulator);
465 if (ret)
466 dev_err(&led->client->dev,
467 "Failed to disable regulator\n");
468 }
469
a2169c9b
DM
470 mutex_destroy(&led->lock);
471
7a8685ac
DM
472 return 0;
473}
474
475static const struct i2c_device_id lp8860_id[] = {
476 { "lp8860", 0 },
477 { }
478};
479MODULE_DEVICE_TABLE(i2c, lp8860_id);
480
7a8685ac
DM
481static const struct of_device_id of_lp8860_leds_match[] = {
482 { .compatible = "ti,lp8860", },
483 {},
484};
485MODULE_DEVICE_TABLE(of, of_lp8860_leds_match);
7a8685ac
DM
486
487static struct i2c_driver lp8860_driver = {
488 .driver = {
489 .name = "lp8860",
a2169c9b 490 .of_match_table = of_lp8860_leds_match,
7a8685ac
DM
491 },
492 .probe = lp8860_probe,
493 .remove = lp8860_remove,
494 .id_table = lp8860_id,
495};
496module_i2c_driver(lp8860_driver);
497
5ee047fb 498MODULE_DESCRIPTION("Texas Instruments LP8860 LED driver");
7a8685ac 499MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
a2169c9b 500MODULE_LICENSE("GPL v2");