1 // SPDX-License-Identifier: GPL-2.0-only
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
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>
21 #include <linux/of_device.h>
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 {
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;
48 const struct pca9532_chip_info *chip_info;
53 static int pca9532_probe(struct i2c_client *client);
54 static void pca9532_remove(struct i2c_client *client);
63 static const struct i2c_device_id pca9532_id[] = {
64 { "pca9530", pca9530 },
65 { "pca9531", pca9531 },
66 { "pca9532", pca9532 },
67 { "pca9533", pca9533 },
71 MODULE_DEVICE_TABLE(i2c, pca9532_id);
73 static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
89 static const struct of_device_id of_pca9532_leds_match[] = {
90 { .compatible = "nxp,pca9530", .data = (void *)pca9530 },
91 { .compatible = "nxp,pca9531", .data = (void *)pca9531 },
92 { .compatible = "nxp,pca9532", .data = (void *)pca9532 },
93 { .compatible = "nxp,pca9533", .data = (void *)pca9533 },
97 MODULE_DEVICE_TABLE(of, of_pca9532_leds_match);
100 static struct i2c_driver pca9532_driver = {
102 .name = "leds-pca953x",
103 .of_match_table = of_match_ptr(of_pca9532_leds_match),
105 .probe_new = pca9532_probe,
106 .remove = pca9532_remove,
107 .id_table = pca9532_id,
110 /* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
111 * the clever Thecus people are using one pwm to drive the beeper. So,
112 * as a compromise we average one pwm to the values requested by all
113 * leds that are not ON/OFF.
115 static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
116 enum led_brightness value)
118 int a = 0, b = 0, i = 0;
119 struct pca9532_data *data = i2c_get_clientdata(client);
120 for (i = 0; i < data->chip_info->num_leds; i++) {
121 if (data->leds[i].type == PCA9532_TYPE_LED &&
122 data->leds[i].state == PCA9532_PWM0+pwm) {
124 b += data->leds[i].ldev.brightness;
128 dev_err(&client->dev,
129 "fear of division by zero %d/%d, wanted %d\n",
137 data->psc[pwm] = blink;
141 static int pca9532_setpwm(struct i2c_client *client, int pwm)
143 struct pca9532_data *data = i2c_get_clientdata(client);
144 u8 maxleds = data->chip_info->num_leds;
146 mutex_lock(&data->update_lock);
147 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
149 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
151 mutex_unlock(&data->update_lock);
155 /* Set LED routing */
156 static void pca9532_setled(struct pca9532_led *led)
158 struct i2c_client *client = led->client;
159 struct pca9532_data *data = i2c_get_clientdata(client);
160 u8 maxleds = data->chip_info->num_leds;
163 mutex_lock(&data->update_lock);
164 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
166 reg = reg & ~LED_MASK(led->id);
167 /* set the new value */
168 reg = reg | (led->state << LED_SHIFT(led->id));
169 i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
170 mutex_unlock(&data->update_lock);
173 static int pca9532_set_brightness(struct led_classdev *led_cdev,
174 enum led_brightness value)
177 struct pca9532_led *led = ldev_to_led(led_cdev);
179 if (value == LED_OFF)
180 led->state = PCA9532_OFF;
181 else if (value == LED_FULL)
182 led->state = PCA9532_ON;
184 led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
185 err = pca9532_calcpwm(led->client, 0, 0, value);
189 if (led->state == PCA9532_PWM0)
190 pca9532_setpwm(led->client, 0);
195 static int pca9532_set_blink(struct led_classdev *led_cdev,
196 unsigned long *delay_on, unsigned long *delay_off)
198 struct pca9532_led *led = ldev_to_led(led_cdev);
199 struct i2c_client *client = led->client;
203 if (*delay_on == 0 && *delay_off == 0) {
204 /* led subsystem ask us for a blink rate */
208 if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
211 /* Thecus specific: only use PSC/PWM 0 */
212 psc = (*delay_on * 152-1)/1000;
213 err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
216 if (led->state == PCA9532_PWM0)
217 pca9532_setpwm(led->client, 0);
223 static int pca9532_event(struct input_dev *dev, unsigned int type,
224 unsigned int code, int value)
226 struct pca9532_data *data = input_get_drvdata(dev);
228 if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
231 /* XXX: allow different kind of beeps with psc/pwm modifications */
232 if (value > 1 && value < 32767)
237 schedule_work(&data->work);
242 static void pca9532_input_work(struct work_struct *work)
244 struct pca9532_data *data =
245 container_of(work, struct pca9532_data, work);
246 u8 maxleds = data->chip_info->num_leds;
248 mutex_lock(&data->update_lock);
249 i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
251 mutex_unlock(&data->update_lock);
254 static enum pca9532_state pca9532_getled(struct pca9532_led *led)
256 struct i2c_client *client = led->client;
257 struct pca9532_data *data = i2c_get_clientdata(client);
258 u8 maxleds = data->chip_info->num_leds;
260 enum pca9532_state ret;
262 mutex_lock(&data->update_lock);
263 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
264 ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
265 mutex_unlock(&data->update_lock);
269 #ifdef CONFIG_LEDS_PCA9532_GPIO
270 static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
272 struct pca9532_data *data = gpiochip_get_data(gc);
273 struct pca9532_led *led = &data->leds[offset];
275 if (led->type == PCA9532_TYPE_GPIO)
281 static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
283 struct pca9532_data *data = gpiochip_get_data(gc);
284 struct pca9532_led *led = &data->leds[offset];
287 led->state = PCA9532_ON;
289 led->state = PCA9532_OFF;
294 static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
296 struct pca9532_data *data = gpiochip_get_data(gc);
299 reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
301 return !!(reg & (1 << (offset % 8)));
304 static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
306 /* To use as input ensure pin is not driven */
307 pca9532_gpio_set_value(gc, offset, 0);
312 static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
314 pca9532_gpio_set_value(gc, offset, val);
318 #endif /* CONFIG_LEDS_PCA9532_GPIO */
320 static void pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
325 switch (data->leds[i].type) {
326 case PCA9532_TYPE_NONE:
327 case PCA9532_TYPE_GPIO:
329 case PCA9532_TYPE_LED:
330 led_classdev_unregister(&data->leds[i].ldev);
332 case PCA9532_TYPE_N2100_BEEP:
333 if (data->idev != NULL) {
334 cancel_work_sync(&data->work);
341 #ifdef CONFIG_LEDS_PCA9532_GPIO
342 if (data->gpio.parent)
343 gpiochip_remove(&data->gpio);
347 static int pca9532_configure(struct i2c_client *client,
348 struct pca9532_data *data, struct pca9532_platform_data *pdata)
352 u8 maxleds = data->chip_info->num_leds;
354 for (i = 0; i < 2; i++) {
355 data->pwm[i] = pdata->pwm[i];
356 data->psc[i] = pdata->psc[i];
357 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
359 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
363 for (i = 0; i < data->chip_info->num_leds; i++) {
364 struct pca9532_led *led = &data->leds[i];
365 struct pca9532_led *pled = &pdata->leds[i];
366 led->client = client;
368 led->type = pled->type;
370 case PCA9532_TYPE_NONE:
372 case PCA9532_TYPE_GPIO:
375 case PCA9532_TYPE_LED:
376 if (pled->state == PCA9532_KEEP)
377 led->state = pca9532_getled(led);
379 led->state = pled->state;
380 led->name = pled->name;
381 led->ldev.name = led->name;
382 led->ldev.default_trigger = pled->default_trigger;
383 led->ldev.brightness = LED_OFF;
384 led->ldev.brightness_set_blocking =
385 pca9532_set_brightness;
386 led->ldev.blink_set = pca9532_set_blink;
387 err = led_classdev_register(&client->dev, &led->ldev);
389 dev_err(&client->dev,
390 "couldn't register LED %s\n",
396 case PCA9532_TYPE_N2100_BEEP:
398 led->state = PCA9532_PWM1;
400 data->idev = devm_input_allocate_device(&client->dev);
401 if (data->idev == NULL) {
405 data->idev->name = pled->name;
406 data->idev->phys = "i2c/pca9532";
407 data->idev->id.bustype = BUS_HOST;
408 data->idev->id.vendor = 0x001f;
409 data->idev->id.product = 0x0001;
410 data->idev->id.version = 0x0100;
411 data->idev->evbit[0] = BIT_MASK(EV_SND);
412 data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
414 data->idev->event = pca9532_event;
415 input_set_drvdata(data->idev, data);
416 INIT_WORK(&data->work, pca9532_input_work);
417 err = input_register_device(data->idev);
419 cancel_work_sync(&data->work);
427 #ifdef CONFIG_LEDS_PCA9532_GPIO
429 data->gpio.label = "gpio-pca9532";
430 data->gpio.direction_input = pca9532_gpio_direction_input;
431 data->gpio.direction_output = pca9532_gpio_direction_output;
432 data->gpio.set = pca9532_gpio_set_value;
433 data->gpio.get = pca9532_gpio_get_value;
434 data->gpio.request = pca9532_gpio_request_pin;
435 data->gpio.can_sleep = 1;
436 data->gpio.base = pdata->gpio_base;
437 data->gpio.ngpio = data->chip_info->num_leds;
438 data->gpio.parent = &client->dev;
439 data->gpio.owner = THIS_MODULE;
441 err = gpiochip_add_data(&data->gpio, data);
443 /* Use data->gpio.dev as a flag for freeing gpiochip */
444 data->gpio.parent = NULL;
445 dev_warn(&client->dev, "could not add gpiochip\n");
447 dev_info(&client->dev, "gpios %i...%i\n",
448 data->gpio.base, data->gpio.base +
449 data->gpio.ngpio - 1);
457 pca9532_destroy_devices(data, i);
461 static struct pca9532_platform_data *
462 pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
464 struct pca9532_platform_data *pdata;
465 struct device_node *child;
470 devid = (int)(uintptr_t)of_device_get_match_data(dev);
471 maxleds = pca9532_chip_info_tbl[devid].num_leds;
473 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
475 return ERR_PTR(-ENOMEM);
477 pdata->gpio_base = -1;
479 of_property_read_u8_array(np, "nxp,pwm", &pdata->pwm[0],
480 ARRAY_SIZE(pdata->pwm));
481 of_property_read_u8_array(np, "nxp,psc", &pdata->psc[0],
482 ARRAY_SIZE(pdata->psc));
484 for_each_available_child_of_node(np, child) {
485 if (of_property_read_string(child, "label",
486 &pdata->leds[i].name))
487 pdata->leds[i].name = child->name;
488 of_property_read_u32(child, "type", &pdata->leds[i].type);
489 of_property_read_string(child, "linux,default-trigger",
490 &pdata->leds[i].default_trigger);
491 if (!of_property_read_string(child, "default-state", &state)) {
492 if (!strcmp(state, "on"))
493 pdata->leds[i].state = PCA9532_ON;
494 else if (!strcmp(state, "keep"))
495 pdata->leds[i].state = PCA9532_KEEP;
497 if (++i >= maxleds) {
506 static int pca9532_probe(struct i2c_client *client)
508 const struct i2c_device_id *id = i2c_client_get_device_id(client);
510 struct pca9532_data *data = i2c_get_clientdata(client);
511 struct pca9532_platform_data *pca9532_pdata =
512 dev_get_platdata(&client->dev);
513 struct device_node *np = dev_of_node(&client->dev);
515 if (!pca9532_pdata) {
518 pca9532_of_populate_pdata(&client->dev, np);
519 if (IS_ERR(pca9532_pdata))
520 return PTR_ERR(pca9532_pdata);
522 dev_err(&client->dev, "no platform data\n");
525 devid = (int)(uintptr_t)of_device_get_match_data(&client->dev);
527 devid = id->driver_data;
530 if (!i2c_check_functionality(client->adapter,
531 I2C_FUNC_SMBUS_BYTE_DATA))
534 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
538 data->chip_info = &pca9532_chip_info_tbl[devid];
540 dev_info(&client->dev, "setting platform data\n");
541 i2c_set_clientdata(client, data);
542 data->client = client;
543 mutex_init(&data->update_lock);
545 return pca9532_configure(client, data, pca9532_pdata);
548 static void pca9532_remove(struct i2c_client *client)
550 struct pca9532_data *data = i2c_get_clientdata(client);
552 pca9532_destroy_devices(data, data->chip_info->num_leds);
555 module_i2c_driver(pca9532_driver);
557 MODULE_AUTHOR("Riku Voipio");
558 MODULE_LICENSE("GPL");
559 MODULE_DESCRIPTION("PCA 9532 LED dimmer");