Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / leds / leds-pca9532.c
blob27d027165472502658f52e0ea890449a9180d9e1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * pca9532.c - 16-bit Led dimmer
5 * Copyright (C) 2011 Jan Weitzel
6 * Copyright (C) 2008 Riku Voipio
8 * Datasheet: http://www.nxp.com/documents/data_sheet/PCA9532.pdf
9 */
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/leds.h>
15 #include <linux/input.h>
16 #include <linux/mutex.h>
17 #include <linux/workqueue.h>
18 #include <linux/leds-pca9532.h>
19 #include <linux/gpio/driver.h>
20 #include <linux/of.h>
21 #include <linux/of_device.h>
23 /* m = num_leds*/
24 #define PCA9532_REG_INPUT(i) ((i) >> 3)
25 #define PCA9532_REG_OFFSET(m) ((m) >> 4)
26 #define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
27 #define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
28 #define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
29 #define LED_NUM(led) (led & 0x3)
30 #define LED_SHIFT(led) (LED_NUM(led) * 2)
31 #define LED_MASK(led) (0x3 << LED_SHIFT(led))
33 #define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
35 struct pca9532_chip_info {
36 u8 num_leds;
39 struct pca9532_data {
40 struct i2c_client *client;
41 struct pca9532_led leds[16];
42 struct mutex update_lock;
43 struct input_dev *idev;
44 struct work_struct work;
45 #ifdef CONFIG_LEDS_PCA9532_GPIO
46 struct gpio_chip gpio;
47 #endif
48 const struct pca9532_chip_info *chip_info;
49 u8 pwm[2];
50 u8 psc[2];
53 static int pca9532_probe(struct i2c_client *client,
54 const struct i2c_device_id *id);
55 static int pca9532_remove(struct i2c_client *client);
57 enum {
58 pca9530,
59 pca9531,
60 pca9532,
61 pca9533,
64 static const struct i2c_device_id pca9532_id[] = {
65 { "pca9530", pca9530 },
66 { "pca9531", pca9531 },
67 { "pca9532", pca9532 },
68 { "pca9533", pca9533 },
69 { }
72 MODULE_DEVICE_TABLE(i2c, pca9532_id);
74 static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
75 [pca9530] = {
76 .num_leds = 2,
78 [pca9531] = {
79 .num_leds = 8,
81 [pca9532] = {
82 .num_leds = 16,
84 [pca9533] = {
85 .num_leds = 4,
89 #ifdef CONFIG_OF
90 static const struct of_device_id of_pca9532_leds_match[] = {
91 { .compatible = "nxp,pca9530", .data = (void *)pca9530 },
92 { .compatible = "nxp,pca9531", .data = (void *)pca9531 },
93 { .compatible = "nxp,pca9532", .data = (void *)pca9532 },
94 { .compatible = "nxp,pca9533", .data = (void *)pca9533 },
95 {},
98 MODULE_DEVICE_TABLE(of, of_pca9532_leds_match);
99 #endif
101 static struct i2c_driver pca9532_driver = {
102 .driver = {
103 .name = "leds-pca953x",
104 .of_match_table = of_match_ptr(of_pca9532_leds_match),
106 .probe = pca9532_probe,
107 .remove = pca9532_remove,
108 .id_table = pca9532_id,
111 /* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
112 * the clever Thecus people are using one pwm to drive the beeper. So,
113 * as a compromise we average one pwm to the values requested by all
114 * leds that are not ON/OFF.
115 * */
116 static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
117 enum led_brightness value)
119 int a = 0, b = 0, i = 0;
120 struct pca9532_data *data = i2c_get_clientdata(client);
121 for (i = 0; i < data->chip_info->num_leds; i++) {
122 if (data->leds[i].type == PCA9532_TYPE_LED &&
123 data->leds[i].state == PCA9532_PWM0+pwm) {
124 a++;
125 b += data->leds[i].ldev.brightness;
128 if (a == 0) {
129 dev_err(&client->dev,
130 "fear of division by zero %d/%d, wanted %d\n",
131 b, a, value);
132 return -EINVAL;
134 b = b/a;
135 if (b > 0xFF)
136 return -EINVAL;
137 data->pwm[pwm] = b;
138 data->psc[pwm] = blink;
139 return 0;
142 static int pca9532_setpwm(struct i2c_client *client, int pwm)
144 struct pca9532_data *data = i2c_get_clientdata(client);
145 u8 maxleds = data->chip_info->num_leds;
147 mutex_lock(&data->update_lock);
148 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
149 data->pwm[pwm]);
150 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
151 data->psc[pwm]);
152 mutex_unlock(&data->update_lock);
153 return 0;
156 /* Set LED routing */
157 static void pca9532_setled(struct pca9532_led *led)
159 struct i2c_client *client = led->client;
160 struct pca9532_data *data = i2c_get_clientdata(client);
161 u8 maxleds = data->chip_info->num_leds;
162 char reg;
164 mutex_lock(&data->update_lock);
165 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
166 /* zero led bits */
167 reg = reg & ~LED_MASK(led->id);
168 /* set the new value */
169 reg = reg | (led->state << LED_SHIFT(led->id));
170 i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
171 mutex_unlock(&data->update_lock);
174 static int pca9532_set_brightness(struct led_classdev *led_cdev,
175 enum led_brightness value)
177 int err = 0;
178 struct pca9532_led *led = ldev_to_led(led_cdev);
180 if (value == LED_OFF)
181 led->state = PCA9532_OFF;
182 else if (value == LED_FULL)
183 led->state = PCA9532_ON;
184 else {
185 led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
186 err = pca9532_calcpwm(led->client, 0, 0, value);
187 if (err)
188 return err;
190 if (led->state == PCA9532_PWM0)
191 pca9532_setpwm(led->client, 0);
192 pca9532_setled(led);
193 return err;
196 static int pca9532_set_blink(struct led_classdev *led_cdev,
197 unsigned long *delay_on, unsigned long *delay_off)
199 struct pca9532_led *led = ldev_to_led(led_cdev);
200 struct i2c_client *client = led->client;
201 int psc;
202 int err = 0;
204 if (*delay_on == 0 && *delay_off == 0) {
205 /* led subsystem ask us for a blink rate */
206 *delay_on = 1000;
207 *delay_off = 1000;
209 if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
210 return -EINVAL;
212 /* Thecus specific: only use PSC/PWM 0 */
213 psc = (*delay_on * 152-1)/1000;
214 err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
215 if (err)
216 return err;
217 if (led->state == PCA9532_PWM0)
218 pca9532_setpwm(led->client, 0);
219 pca9532_setled(led);
221 return 0;
224 static int pca9532_event(struct input_dev *dev, unsigned int type,
225 unsigned int code, int value)
227 struct pca9532_data *data = input_get_drvdata(dev);
229 if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
230 return -1;
232 /* XXX: allow different kind of beeps with psc/pwm modifications */
233 if (value > 1 && value < 32767)
234 data->pwm[1] = 127;
235 else
236 data->pwm[1] = 0;
238 schedule_work(&data->work);
240 return 0;
243 static void pca9532_input_work(struct work_struct *work)
245 struct pca9532_data *data =
246 container_of(work, struct pca9532_data, work);
247 u8 maxleds = data->chip_info->num_leds;
249 mutex_lock(&data->update_lock);
250 i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
251 data->pwm[1]);
252 mutex_unlock(&data->update_lock);
255 static enum pca9532_state pca9532_getled(struct pca9532_led *led)
257 struct i2c_client *client = led->client;
258 struct pca9532_data *data = i2c_get_clientdata(client);
259 u8 maxleds = data->chip_info->num_leds;
260 char reg;
261 enum pca9532_state ret;
263 mutex_lock(&data->update_lock);
264 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
265 ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
266 mutex_unlock(&data->update_lock);
267 return ret;
270 #ifdef CONFIG_LEDS_PCA9532_GPIO
271 static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
273 struct pca9532_data *data = gpiochip_get_data(gc);
274 struct pca9532_led *led = &data->leds[offset];
276 if (led->type == PCA9532_TYPE_GPIO)
277 return 0;
279 return -EBUSY;
282 static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
284 struct pca9532_data *data = gpiochip_get_data(gc);
285 struct pca9532_led *led = &data->leds[offset];
287 if (val)
288 led->state = PCA9532_ON;
289 else
290 led->state = PCA9532_OFF;
292 pca9532_setled(led);
295 static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
297 struct pca9532_data *data = gpiochip_get_data(gc);
298 unsigned char reg;
300 reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
302 return !!(reg & (1 << (offset % 8)));
305 static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
307 /* To use as input ensure pin is not driven */
308 pca9532_gpio_set_value(gc, offset, 0);
310 return 0;
313 static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
315 pca9532_gpio_set_value(gc, offset, val);
317 return 0;
319 #endif /* CONFIG_LEDS_PCA9532_GPIO */
321 static int pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
323 int i = n_devs;
325 if (!data)
326 return -EINVAL;
328 while (--i >= 0) {
329 switch (data->leds[i].type) {
330 case PCA9532_TYPE_NONE:
331 case PCA9532_TYPE_GPIO:
332 break;
333 case PCA9532_TYPE_LED:
334 led_classdev_unregister(&data->leds[i].ldev);
335 break;
336 case PCA9532_TYPE_N2100_BEEP:
337 if (data->idev != NULL) {
338 cancel_work_sync(&data->work);
339 data->idev = NULL;
341 break;
345 #ifdef CONFIG_LEDS_PCA9532_GPIO
346 if (data->gpio.parent)
347 gpiochip_remove(&data->gpio);
348 #endif
350 return 0;
353 static int pca9532_configure(struct i2c_client *client,
354 struct pca9532_data *data, struct pca9532_platform_data *pdata)
356 int i, err = 0;
357 int gpios = 0;
358 u8 maxleds = data->chip_info->num_leds;
360 for (i = 0; i < 2; i++) {
361 data->pwm[i] = pdata->pwm[i];
362 data->psc[i] = pdata->psc[i];
363 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
364 data->pwm[i]);
365 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
366 data->psc[i]);
369 for (i = 0; i < data->chip_info->num_leds; i++) {
370 struct pca9532_led *led = &data->leds[i];
371 struct pca9532_led *pled = &pdata->leds[i];
372 led->client = client;
373 led->id = i;
374 led->type = pled->type;
375 switch (led->type) {
376 case PCA9532_TYPE_NONE:
377 break;
378 case PCA9532_TYPE_GPIO:
379 gpios++;
380 break;
381 case PCA9532_TYPE_LED:
382 if (pled->state == PCA9532_KEEP)
383 led->state = pca9532_getled(led);
384 else
385 led->state = pled->state;
386 led->name = pled->name;
387 led->ldev.name = led->name;
388 led->ldev.default_trigger = pled->default_trigger;
389 led->ldev.brightness = LED_OFF;
390 led->ldev.brightness_set_blocking =
391 pca9532_set_brightness;
392 led->ldev.blink_set = pca9532_set_blink;
393 err = led_classdev_register(&client->dev, &led->ldev);
394 if (err < 0) {
395 dev_err(&client->dev,
396 "couldn't register LED %s\n",
397 led->name);
398 goto exit;
400 pca9532_setled(led);
401 break;
402 case PCA9532_TYPE_N2100_BEEP:
403 BUG_ON(data->idev);
404 led->state = PCA9532_PWM1;
405 pca9532_setled(led);
406 data->idev = devm_input_allocate_device(&client->dev);
407 if (data->idev == NULL) {
408 err = -ENOMEM;
409 goto exit;
411 data->idev->name = pled->name;
412 data->idev->phys = "i2c/pca9532";
413 data->idev->id.bustype = BUS_HOST;
414 data->idev->id.vendor = 0x001f;
415 data->idev->id.product = 0x0001;
416 data->idev->id.version = 0x0100;
417 data->idev->evbit[0] = BIT_MASK(EV_SND);
418 data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
419 BIT_MASK(SND_TONE);
420 data->idev->event = pca9532_event;
421 input_set_drvdata(data->idev, data);
422 INIT_WORK(&data->work, pca9532_input_work);
423 err = input_register_device(data->idev);
424 if (err) {
425 cancel_work_sync(&data->work);
426 data->idev = NULL;
427 goto exit;
429 break;
433 #ifdef CONFIG_LEDS_PCA9532_GPIO
434 if (gpios) {
435 data->gpio.label = "gpio-pca9532";
436 data->gpio.direction_input = pca9532_gpio_direction_input;
437 data->gpio.direction_output = pca9532_gpio_direction_output;
438 data->gpio.set = pca9532_gpio_set_value;
439 data->gpio.get = pca9532_gpio_get_value;
440 data->gpio.request = pca9532_gpio_request_pin;
441 data->gpio.can_sleep = 1;
442 data->gpio.base = pdata->gpio_base;
443 data->gpio.ngpio = data->chip_info->num_leds;
444 data->gpio.parent = &client->dev;
445 data->gpio.owner = THIS_MODULE;
447 err = gpiochip_add_data(&data->gpio, data);
448 if (err) {
449 /* Use data->gpio.dev as a flag for freeing gpiochip */
450 data->gpio.parent = NULL;
451 dev_warn(&client->dev, "could not add gpiochip\n");
452 } else {
453 dev_info(&client->dev, "gpios %i...%i\n",
454 data->gpio.base, data->gpio.base +
455 data->gpio.ngpio - 1);
458 #endif
460 return 0;
462 exit:
463 pca9532_destroy_devices(data, i);
464 return err;
467 static struct pca9532_platform_data *
468 pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
470 struct pca9532_platform_data *pdata;
471 struct device_node *child;
472 int devid, maxleds;
473 int i = 0;
474 const char *state;
476 devid = (int)(uintptr_t)of_device_get_match_data(dev);
477 maxleds = pca9532_chip_info_tbl[devid].num_leds;
479 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
480 if (!pdata)
481 return ERR_PTR(-ENOMEM);
483 of_property_read_u8_array(np, "nxp,pwm", &pdata->pwm[0],
484 ARRAY_SIZE(pdata->pwm));
485 of_property_read_u8_array(np, "nxp,psc", &pdata->psc[0],
486 ARRAY_SIZE(pdata->psc));
488 for_each_available_child_of_node(np, child) {
489 if (of_property_read_string(child, "label",
490 &pdata->leds[i].name))
491 pdata->leds[i].name = child->name;
492 of_property_read_u32(child, "type", &pdata->leds[i].type);
493 of_property_read_string(child, "linux,default-trigger",
494 &pdata->leds[i].default_trigger);
495 if (!of_property_read_string(child, "default-state", &state)) {
496 if (!strcmp(state, "on"))
497 pdata->leds[i].state = PCA9532_ON;
498 else if (!strcmp(state, "keep"))
499 pdata->leds[i].state = PCA9532_KEEP;
501 if (++i >= maxleds) {
502 of_node_put(child);
503 break;
507 return pdata;
510 static int pca9532_probe(struct i2c_client *client,
511 const struct i2c_device_id *id)
513 int devid;
514 struct pca9532_data *data = i2c_get_clientdata(client);
515 struct pca9532_platform_data *pca9532_pdata =
516 dev_get_platdata(&client->dev);
517 struct device_node *np = dev_of_node(&client->dev);
519 if (!pca9532_pdata) {
520 if (np) {
521 pca9532_pdata =
522 pca9532_of_populate_pdata(&client->dev, np);
523 if (IS_ERR(pca9532_pdata))
524 return PTR_ERR(pca9532_pdata);
525 } else {
526 dev_err(&client->dev, "no platform data\n");
527 return -EINVAL;
529 devid = (int)(uintptr_t)of_device_get_match_data(&client->dev);
530 } else {
531 devid = id->driver_data;
534 if (!i2c_check_functionality(client->adapter,
535 I2C_FUNC_SMBUS_BYTE_DATA))
536 return -EIO;
538 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
539 if (!data)
540 return -ENOMEM;
542 data->chip_info = &pca9532_chip_info_tbl[devid];
544 dev_info(&client->dev, "setting platform data\n");
545 i2c_set_clientdata(client, data);
546 data->client = client;
547 mutex_init(&data->update_lock);
549 return pca9532_configure(client, data, pca9532_pdata);
552 static int pca9532_remove(struct i2c_client *client)
554 struct pca9532_data *data = i2c_get_clientdata(client);
556 return pca9532_destroy_devices(data, data->chip_info->num_leds);
559 module_i2c_driver(pca9532_driver);
561 MODULE_AUTHOR("Riku Voipio");
562 MODULE_LICENSE("GPL");
563 MODULE_DESCRIPTION("PCA 9532 LED dimmer");