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>
23 #define PCA9532_REG_INPUT(i) ((i) >> 3)
24 #define PCA9532_REG_OFFSET(m) ((m) >> 4)
25 #define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
26 #define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
27 #define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
28 #define LED_NUM(led) (led & 0x3)
29 #define LED_SHIFT(led) (LED_NUM(led) * 2)
30 #define LED_MASK(led) (0x3 << LED_SHIFT(led))
32 #define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
34 struct pca9532_chip_info {
39 struct i2c_client *client;
40 struct pca9532_led leds[16];
41 struct mutex update_lock;
42 struct input_dev *idev;
43 struct work_struct work;
44 #ifdef CONFIG_LEDS_PCA9532_GPIO
45 struct gpio_chip gpio;
47 const struct pca9532_chip_info *chip_info;
52 static int pca9532_probe(struct i2c_client *client);
53 static void pca9532_remove(struct i2c_client *client);
62 static const struct i2c_device_id pca9532_id[] = {
63 { "pca9530", pca9530 },
64 { "pca9531", pca9531 },
65 { "pca9532", pca9532 },
66 { "pca9533", pca9533 },
70 MODULE_DEVICE_TABLE(i2c, pca9532_id);
72 static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
88 static const struct of_device_id of_pca9532_leds_match[] = {
89 { .compatible = "nxp,pca9530", .data = (void *)pca9530 },
90 { .compatible = "nxp,pca9531", .data = (void *)pca9531 },
91 { .compatible = "nxp,pca9532", .data = (void *)pca9532 },
92 { .compatible = "nxp,pca9533", .data = (void *)pca9533 },
96 MODULE_DEVICE_TABLE(of, of_pca9532_leds_match);
99 static struct i2c_driver pca9532_driver = {
101 .name = "leds-pca953x",
102 .of_match_table = of_match_ptr(of_pca9532_leds_match),
104 .probe = pca9532_probe,
105 .remove = pca9532_remove,
106 .id_table = pca9532_id,
109 /* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
110 * the clever Thecus people are using one pwm to drive the beeper. So,
111 * as a compromise we average one pwm to the values requested by all
112 * leds that are not ON/OFF.
114 static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
115 enum led_brightness value)
117 int a = 0, b = 0, i = 0;
118 struct pca9532_data *data = i2c_get_clientdata(client);
119 for (i = 0; i < data->chip_info->num_leds; i++) {
120 if (data->leds[i].type == PCA9532_TYPE_LED &&
121 data->leds[i].state == PCA9532_PWM0+pwm) {
123 b += data->leds[i].ldev.brightness;
127 dev_err(&client->dev,
128 "fear of division by zero %d/%d, wanted %d\n",
136 data->psc[pwm] = blink;
140 static int pca9532_setpwm(struct i2c_client *client, int pwm)
142 struct pca9532_data *data = i2c_get_clientdata(client);
143 u8 maxleds = data->chip_info->num_leds;
145 mutex_lock(&data->update_lock);
146 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
148 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
150 mutex_unlock(&data->update_lock);
154 /* Set LED routing */
155 static void pca9532_setled(struct pca9532_led *led)
157 struct i2c_client *client = led->client;
158 struct pca9532_data *data = i2c_get_clientdata(client);
159 u8 maxleds = data->chip_info->num_leds;
162 mutex_lock(&data->update_lock);
163 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
165 reg = reg & ~LED_MASK(led->id);
166 /* set the new value */
167 reg = reg | (led->state << LED_SHIFT(led->id));
168 i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
169 mutex_unlock(&data->update_lock);
172 static int pca9532_set_brightness(struct led_classdev *led_cdev,
173 enum led_brightness value)
176 struct pca9532_led *led = ldev_to_led(led_cdev);
178 if (value == LED_OFF)
179 led->state = PCA9532_OFF;
180 else if (value == LED_FULL)
181 led->state = PCA9532_ON;
183 led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
184 err = pca9532_calcpwm(led->client, 0, 0, value);
188 if (led->state == PCA9532_PWM0)
189 pca9532_setpwm(led->client, 0);
194 static int pca9532_set_blink(struct led_classdev *led_cdev,
195 unsigned long *delay_on, unsigned long *delay_off)
197 struct pca9532_led *led = ldev_to_led(led_cdev);
198 struct i2c_client *client = led->client;
202 if (*delay_on == 0 && *delay_off == 0) {
203 /* led subsystem ask us for a blink rate */
207 if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
210 /* Thecus specific: only use PSC/PWM 0 */
211 psc = (*delay_on * 152-1)/1000;
212 err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
215 if (led->state == PCA9532_PWM0)
216 pca9532_setpwm(led->client, 0);
222 static int pca9532_event(struct input_dev *dev, unsigned int type,
223 unsigned int code, int value)
225 struct pca9532_data *data = input_get_drvdata(dev);
227 if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
230 /* XXX: allow different kind of beeps with psc/pwm modifications */
231 if (value > 1 && value < 32767)
236 schedule_work(&data->work);
241 static void pca9532_input_work(struct work_struct *work)
243 struct pca9532_data *data =
244 container_of(work, struct pca9532_data, work);
245 u8 maxleds = data->chip_info->num_leds;
247 mutex_lock(&data->update_lock);
248 i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
250 mutex_unlock(&data->update_lock);
253 static enum pca9532_state pca9532_getled(struct pca9532_led *led)
255 struct i2c_client *client = led->client;
256 struct pca9532_data *data = i2c_get_clientdata(client);
257 u8 maxleds = data->chip_info->num_leds;
259 enum pca9532_state ret;
261 mutex_lock(&data->update_lock);
262 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
263 ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
264 mutex_unlock(&data->update_lock);
268 #ifdef CONFIG_LEDS_PCA9532_GPIO
269 static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
271 struct pca9532_data *data = gpiochip_get_data(gc);
272 struct pca9532_led *led = &data->leds[offset];
274 if (led->type == PCA9532_TYPE_GPIO)
280 static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
282 struct pca9532_data *data = gpiochip_get_data(gc);
283 struct pca9532_led *led = &data->leds[offset];
286 led->state = PCA9532_ON;
288 led->state = PCA9532_OFF;
293 static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
295 struct pca9532_data *data = gpiochip_get_data(gc);
298 reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
300 return !!(reg & (1 << (offset % 8)));
303 static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
305 /* To use as input ensure pin is not driven */
306 pca9532_gpio_set_value(gc, offset, 0);
311 static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
313 pca9532_gpio_set_value(gc, offset, val);
317 #endif /* CONFIG_LEDS_PCA9532_GPIO */
319 static void pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
324 switch (data->leds[i].type) {
325 case PCA9532_TYPE_NONE:
326 case PCA9532_TYPE_GPIO:
328 case PCA9532_TYPE_LED:
329 led_classdev_unregister(&data->leds[i].ldev);
331 case PCA9532_TYPE_N2100_BEEP:
332 if (data->idev != NULL) {
333 cancel_work_sync(&data->work);
340 #ifdef CONFIG_LEDS_PCA9532_GPIO
341 if (data->gpio.parent)
342 gpiochip_remove(&data->gpio);
346 static int pca9532_configure(struct i2c_client *client,
347 struct pca9532_data *data, struct pca9532_platform_data *pdata)
351 u8 maxleds = data->chip_info->num_leds;
353 for (i = 0; i < 2; i++) {
354 data->pwm[i] = pdata->pwm[i];
355 data->psc[i] = pdata->psc[i];
356 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
358 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
362 for (i = 0; i < data->chip_info->num_leds; i++) {
363 struct pca9532_led *led = &data->leds[i];
364 struct pca9532_led *pled = &pdata->leds[i];
365 led->client = client;
367 led->type = pled->type;
369 case PCA9532_TYPE_NONE:
371 case PCA9532_TYPE_GPIO:
374 case PCA9532_TYPE_LED:
375 if (pled->state == PCA9532_KEEP)
376 led->state = pca9532_getled(led);
378 led->state = pled->state;
379 led->name = pled->name;
380 led->ldev.name = led->name;
381 led->ldev.default_trigger = pled->default_trigger;
382 led->ldev.brightness = LED_OFF;
383 led->ldev.brightness_set_blocking =
384 pca9532_set_brightness;
385 led->ldev.blink_set = pca9532_set_blink;
386 err = led_classdev_register(&client->dev, &led->ldev);
388 dev_err(&client->dev,
389 "couldn't register LED %s\n",
395 case PCA9532_TYPE_N2100_BEEP:
397 led->state = PCA9532_PWM1;
399 data->idev = devm_input_allocate_device(&client->dev);
400 if (data->idev == NULL) {
404 data->idev->name = pled->name;
405 data->idev->phys = "i2c/pca9532";
406 data->idev->id.bustype = BUS_HOST;
407 data->idev->id.vendor = 0x001f;
408 data->idev->id.product = 0x0001;
409 data->idev->id.version = 0x0100;
410 data->idev->evbit[0] = BIT_MASK(EV_SND);
411 data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
413 data->idev->event = pca9532_event;
414 input_set_drvdata(data->idev, data);
415 INIT_WORK(&data->work, pca9532_input_work);
416 err = input_register_device(data->idev);
418 cancel_work_sync(&data->work);
426 #ifdef CONFIG_LEDS_PCA9532_GPIO
428 data->gpio.label = "gpio-pca9532";
429 data->gpio.direction_input = pca9532_gpio_direction_input;
430 data->gpio.direction_output = pca9532_gpio_direction_output;
431 data->gpio.set = pca9532_gpio_set_value;
432 data->gpio.get = pca9532_gpio_get_value;
433 data->gpio.request = pca9532_gpio_request_pin;
434 data->gpio.can_sleep = 1;
435 data->gpio.base = pdata->gpio_base;
436 data->gpio.ngpio = data->chip_info->num_leds;
437 data->gpio.parent = &client->dev;
438 data->gpio.owner = THIS_MODULE;
440 err = gpiochip_add_data(&data->gpio, data);
442 /* Use data->gpio.dev as a flag for freeing gpiochip */
443 data->gpio.parent = NULL;
444 dev_warn(&client->dev, "could not add gpiochip\n");
446 dev_info(&client->dev, "gpios %i...%i\n",
447 data->gpio.base, data->gpio.base +
448 data->gpio.ngpio - 1);
456 pca9532_destroy_devices(data, i);
460 static struct pca9532_platform_data *
461 pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
463 struct pca9532_platform_data *pdata;
464 struct device_node *child;
469 devid = (int)(uintptr_t)of_device_get_match_data(dev);
470 maxleds = pca9532_chip_info_tbl[devid].num_leds;
472 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
474 return ERR_PTR(-ENOMEM);
476 pdata->gpio_base = -1;
478 of_property_read_u8_array(np, "nxp,pwm", &pdata->pwm[0],
479 ARRAY_SIZE(pdata->pwm));
480 of_property_read_u8_array(np, "nxp,psc", &pdata->psc[0],
481 ARRAY_SIZE(pdata->psc));
483 for_each_available_child_of_node(np, child) {
484 if (of_property_read_string(child, "label",
485 &pdata->leds[i].name))
486 pdata->leds[i].name = child->name;
487 of_property_read_u32(child, "type", &pdata->leds[i].type);
488 of_property_read_string(child, "linux,default-trigger",
489 &pdata->leds[i].default_trigger);
490 if (!of_property_read_string(child, "default-state", &state)) {
491 if (!strcmp(state, "on"))
492 pdata->leds[i].state = PCA9532_ON;
493 else if (!strcmp(state, "keep"))
494 pdata->leds[i].state = PCA9532_KEEP;
496 if (++i >= maxleds) {
505 static int pca9532_probe(struct i2c_client *client)
507 const struct i2c_device_id *id = i2c_client_get_device_id(client);
509 struct pca9532_data *data = i2c_get_clientdata(client);
510 struct pca9532_platform_data *pca9532_pdata =
511 dev_get_platdata(&client->dev);
512 struct device_node *np = dev_of_node(&client->dev);
514 if (!pca9532_pdata) {
517 pca9532_of_populate_pdata(&client->dev, np);
518 if (IS_ERR(pca9532_pdata))
519 return PTR_ERR(pca9532_pdata);
521 dev_err(&client->dev, "no platform data\n");
524 devid = (int)(uintptr_t)of_device_get_match_data(&client->dev);
526 devid = id->driver_data;
529 if (!i2c_check_functionality(client->adapter,
530 I2C_FUNC_SMBUS_BYTE_DATA))
533 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
537 data->chip_info = &pca9532_chip_info_tbl[devid];
539 dev_info(&client->dev, "setting platform data\n");
540 i2c_set_clientdata(client, data);
541 data->client = client;
542 mutex_init(&data->update_lock);
544 return pca9532_configure(client, data, pca9532_pdata);
547 static void pca9532_remove(struct i2c_client *client)
549 struct pca9532_data *data = i2c_get_clientdata(client);
551 pca9532_destroy_devices(data, data->chip_info->num_leds);
554 module_i2c_driver(pca9532_driver);
556 MODULE_AUTHOR("Riku Voipio");
557 MODULE_LICENSE("GPL");
558 MODULE_DESCRIPTION("PCA 9532 LED dimmer");