2 * Copyright 2007-2008 Extreme Engineering Solutions, Inc.
4 * Author: Nate Case <ncase@xes-inc.com>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * LED driver for various PCA955x I2C LED drivers
14 * Device Description 7-bit slave address
15 * ------ ----------- -------------------
16 * PCA9550 2-bit driver 0x60 .. 0x61
17 * PCA9551 8-bit driver 0x60 .. 0x67
18 * PCA9552 16-bit driver 0x60 .. 0x67
19 * PCA9553/01 4-bit driver 0x62
20 * PCA9553/02 4-bit driver 0x63
22 * Philips PCA955x LED driver chips follow a register map as shown below:
24 * Control Register Description
25 * ---------------- -----------
26 * 0x0 Input register 0
28 * NUM_INPUT_REGS - 1 Last Input register X
30 * NUM_INPUT_REGS Frequency prescaler 0
31 * NUM_INPUT_REGS + 1 PWM register 0
32 * NUM_INPUT_REGS + 2 Frequency prescaler 1
33 * NUM_INPUT_REGS + 3 PWM register 1
35 * NUM_INPUT_REGS + 4 LED selector 0
37 * + NUM_LED_REGS - 1 Last LED selector
39 * where NUM_INPUT_REGS and NUM_LED_REGS vary depending on how many
40 * bits the chip supports.
43 #include <linux/acpi.h>
44 #include <linux/ctype.h>
45 #include <linux/delay.h>
46 #include <linux/err.h>
47 #include <linux/gpio.h>
48 #include <linux/i2c.h>
49 #include <linux/leds.h>
50 #include <linux/module.h>
51 #include <linux/of_device.h>
53 #include <linux/slab.h>
54 #include <linux/string.h>
56 #include <dt-bindings/leds/leds-pca955x.h>
58 /* LED select registers determine the source that drives LED outputs */
59 #define PCA955X_LS_LED_ON 0x0 /* Output LOW */
60 #define PCA955X_LS_LED_OFF 0x1 /* Output HI-Z */
61 #define PCA955X_LS_BLINK0 0x2 /* Blink at PWM0 rate */
62 #define PCA955X_LS_BLINK1 0x3 /* Blink at PWM1 rate */
71 struct pca955x_chipdef {
73 u8 slv_addr; /* 7-bit slave address mask */
74 int slv_addr_shift; /* Number of bits to ignore */
77 static struct pca955x_chipdef pca955x_chipdefs[] = {
80 .slv_addr = /* 110000x */ 0x60,
85 .slv_addr = /* 1100xxx */ 0x60,
90 .slv_addr = /* 1100xxx */ 0x60,
95 .slv_addr = /* 110001x */ 0x62,
100 static const struct i2c_device_id pca955x_id[] = {
101 { "pca9550", pca9550 },
102 { "pca9551", pca9551 },
103 { "pca9552", pca9552 },
104 { "pca9553", pca9553 },
107 MODULE_DEVICE_TABLE(i2c, pca955x_id);
109 static const struct acpi_device_id pca955x_acpi_ids[] = {
110 { "PCA9550", pca9550 },
111 { "PCA9551", pca9551 },
112 { "PCA9552", pca9552 },
113 { "PCA9553", pca9553 },
116 MODULE_DEVICE_TABLE(acpi, pca955x_acpi_ids);
120 struct pca955x_led *leds;
121 struct pca955x_chipdef *chipdef;
122 struct i2c_client *client;
123 #ifdef CONFIG_LEDS_PCA955X_GPIO
124 struct gpio_chip gpio;
129 struct pca955x *pca955x;
130 struct led_classdev led_cdev;
131 int led_num; /* 0 .. 15 potentially */
134 const char *default_trigger;
137 struct pca955x_platform_data {
138 struct pca955x_led *leds;
142 /* 8 bits per input register */
143 static inline int pca95xx_num_input_regs(int bits)
145 return (bits + 7) / 8;
148 /* 4 bits per LED selector register */
149 static inline int pca95xx_num_led_regs(int bits)
151 return (bits + 3) / 4;
155 * Return an LED selector register value based on an existing one, with
156 * the appropriate 2-bit state value set for the given LED number (0-3).
158 static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
160 return (oldval & (~(0x3 << (led_num << 1)))) |
161 ((state & 0x3) << (led_num << 1));
165 * Write to frequency prescaler register, used to program the
166 * period of the PWM output. period = (PSCx + 1) / 38
168 static int pca955x_write_psc(struct i2c_client *client, int n, u8 val)
170 struct pca955x *pca955x = i2c_get_clientdata(client);
173 ret = i2c_smbus_write_byte_data(client,
174 pca95xx_num_input_regs(pca955x->chipdef->bits) + 2*n,
177 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
178 __func__, n, val, ret);
183 * Write to PWM register, which determines the duty cycle of the
184 * output. LED is OFF when the count is less than the value of this
185 * register, and ON when it is greater. If PWMx == 0, LED is always OFF.
187 * Duty cycle is (256 - PWMx) / 256
189 static int pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
191 struct pca955x *pca955x = i2c_get_clientdata(client);
194 ret = i2c_smbus_write_byte_data(client,
195 pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + 2*n,
198 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
199 __func__, n, val, ret);
204 * Write to LED selector register, which determines the source that
205 * drives the LED output.
207 static int pca955x_write_ls(struct i2c_client *client, int n, u8 val)
209 struct pca955x *pca955x = i2c_get_clientdata(client);
212 ret = i2c_smbus_write_byte_data(client,
213 pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n,
216 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
217 __func__, n, val, ret);
222 * Read the LED selector register, which determines the source that
223 * drives the LED output.
225 static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val)
227 struct pca955x *pca955x = i2c_get_clientdata(client);
230 ret = i2c_smbus_read_byte_data(client,
231 pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n);
233 dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
241 static int pca955x_led_set(struct led_classdev *led_cdev,
242 enum led_brightness value)
244 struct pca955x_led *pca955x_led;
245 struct pca955x *pca955x;
247 int chip_ls; /* which LSx to use (0-3 potentially) */
248 int ls_led; /* which set of bits within LSx to use (0-3) */
251 pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev);
252 pca955x = pca955x_led->pca955x;
254 chip_ls = pca955x_led->led_num / 4;
255 ls_led = pca955x_led->led_num % 4;
257 mutex_lock(&pca955x->lock);
259 ret = pca955x_read_ls(pca955x->client, chip_ls, &ls);
265 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
268 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF);
271 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0);
275 * Use PWM1 for all other values. This has the unwanted
276 * side effect of making all LEDs on the chip share the
277 * same brightness level if set to a value other than
278 * OFF, HALF, or FULL. But, this is probably better than
279 * just turning off for all other values.
281 ret = pca955x_write_pwm(pca955x->client, 1, 255 - value);
284 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
288 ret = pca955x_write_ls(pca955x->client, chip_ls, ls);
291 mutex_unlock(&pca955x->lock);
296 #ifdef CONFIG_LEDS_PCA955X_GPIO
298 * Read the INPUT register, which contains the state of LEDs.
300 static int pca955x_read_input(struct i2c_client *client, int n, u8 *val)
302 int ret = i2c_smbus_read_byte_data(client, n);
305 dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
314 static int pca955x_gpio_request_pin(struct gpio_chip *gc, unsigned int offset)
316 struct pca955x *pca955x = gpiochip_get_data(gc);
317 struct pca955x_led *led = &pca955x->leds[offset];
319 if (led->type == PCA955X_TYPE_GPIO)
325 static int pca955x_set_value(struct gpio_chip *gc, unsigned int offset,
328 struct pca955x *pca955x = gpiochip_get_data(gc);
329 struct pca955x_led *led = &pca955x->leds[offset];
332 return pca955x_led_set(&led->led_cdev, LED_FULL);
334 return pca955x_led_set(&led->led_cdev, LED_OFF);
337 static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset,
340 pca955x_set_value(gc, offset, val);
343 static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
345 struct pca955x *pca955x = gpiochip_get_data(gc);
346 struct pca955x_led *led = &pca955x->leds[offset];
349 /* There is nothing we can do about errors */
350 pca955x_read_input(pca955x->client, led->led_num / 8, ®);
352 return !!(reg & (1 << (led->led_num % 8)));
355 static int pca955x_gpio_direction_input(struct gpio_chip *gc,
358 /* To use as input ensure pin is not driven */
359 return pca955x_set_value(gc, offset, 0);
362 static int pca955x_gpio_direction_output(struct gpio_chip *gc,
363 unsigned int offset, int val)
365 return pca955x_set_value(gc, offset, val);
367 #endif /* CONFIG_LEDS_PCA955X_GPIO */
369 #if IS_ENABLED(CONFIG_OF)
370 static struct pca955x_platform_data *
371 pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip)
373 struct device_node *np = client->dev.of_node;
374 struct device_node *child;
375 struct pca955x_platform_data *pdata;
378 count = of_get_child_count(np);
379 if (!count || count > chip->bits)
380 return ERR_PTR(-ENODEV);
382 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
384 return ERR_PTR(-ENOMEM);
386 pdata->leds = devm_kzalloc(&client->dev,
387 sizeof(struct pca955x_led) * chip->bits,
390 return ERR_PTR(-ENOMEM);
392 for_each_child_of_node(np, child) {
397 res = of_property_read_u32(child, "reg", ®);
398 if ((res != 0) || (reg >= chip->bits))
401 if (of_property_read_string(child, "label", &name))
404 snprintf(pdata->leds[reg].name, sizeof(pdata->leds[reg].name),
407 pdata->leds[reg].type = PCA955X_TYPE_LED;
408 of_property_read_u32(child, "type", &pdata->leds[reg].type);
409 of_property_read_string(child, "linux,default-trigger",
410 &pdata->leds[reg].default_trigger);
413 pdata->num_leds = chip->bits;
418 static const struct of_device_id of_pca955x_match[] = {
419 { .compatible = "nxp,pca9550", .data = (void *)pca9550 },
420 { .compatible = "nxp,pca9551", .data = (void *)pca9551 },
421 { .compatible = "nxp,pca9552", .data = (void *)pca9552 },
422 { .compatible = "nxp,pca9553", .data = (void *)pca9553 },
426 MODULE_DEVICE_TABLE(of, of_pca955x_match);
428 static struct pca955x_platform_data *
429 pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip)
431 return ERR_PTR(-ENODEV);
435 static int pca955x_probe(struct i2c_client *client,
436 const struct i2c_device_id *id)
438 struct pca955x *pca955x;
439 struct pca955x_led *pca955x_led;
440 struct pca955x_chipdef *chip;
441 struct i2c_adapter *adapter;
443 struct pca955x_platform_data *pdata;
447 chip = &pca955x_chipdefs[id->driver_data];
449 const struct acpi_device_id *acpi_id;
451 acpi_id = acpi_match_device(pca955x_acpi_ids, &client->dev);
454 chip = &pca955x_chipdefs[acpi_id->driver_data];
456 adapter = to_i2c_adapter(client->dev.parent);
457 pdata = dev_get_platdata(&client->dev);
459 pdata = pca955x_pdata_of_init(client, chip);
461 return PTR_ERR(pdata);
464 /* Make sure the slave address / chip type combo given is possible */
465 if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
467 dev_err(&client->dev, "invalid slave address %02x\n",
472 dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at "
473 "slave address 0x%02x\n",
474 client->name, chip->bits, client->addr);
476 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
479 if (pdata->num_leds != chip->bits) {
480 dev_err(&client->dev,
481 "board info claims %d LEDs on a %d-bit chip\n",
482 pdata->num_leds, chip->bits);
486 pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
490 pca955x->leds = devm_kzalloc(&client->dev,
491 sizeof(*pca955x_led) * chip->bits, GFP_KERNEL);
495 i2c_set_clientdata(client, pca955x);
497 mutex_init(&pca955x->lock);
498 pca955x->client = client;
499 pca955x->chipdef = chip;
501 for (i = 0; i < chip->bits; i++) {
502 pca955x_led = &pca955x->leds[i];
503 pca955x_led->led_num = i;
504 pca955x_led->pca955x = pca955x;
505 pca955x_led->type = pdata->leds[i].type;
507 switch (pca955x_led->type) {
508 case PCA955X_TYPE_NONE:
510 case PCA955X_TYPE_GPIO:
513 case PCA955X_TYPE_LED:
515 * Platform data can specify LED names and
518 if (pdata->leds[i].name[0] == '\0')
519 snprintf(pdata->leds[i].name,
520 sizeof(pdata->leds[i].name), "%d", i);
522 snprintf(pca955x_led->name,
523 sizeof(pca955x_led->name), "pca955x:%s",
524 pdata->leds[i].name);
526 if (pdata->leds[i].default_trigger)
527 pca955x_led->led_cdev.default_trigger =
528 pdata->leds[i].default_trigger;
530 pca955x_led->led_cdev.name = pca955x_led->name;
531 pca955x_led->led_cdev.brightness_set_blocking =
534 err = devm_led_classdev_register(&client->dev,
535 &pca955x_led->led_cdev);
540 err = pca955x_led_set(&pca955x_led->led_cdev, LED_OFF);
546 /* PWM0 is used for half brightness or 50% duty cycle */
547 err = pca955x_write_pwm(client, 0, 255 - LED_HALF);
551 /* PWM1 is used for variable brightness, default to OFF */
552 err = pca955x_write_pwm(client, 1, 0);
556 /* Set to fast frequency so we do not see flashing */
557 err = pca955x_write_psc(client, 0, 0);
560 err = pca955x_write_psc(client, 1, 0);
564 #ifdef CONFIG_LEDS_PCA955X_GPIO
566 pca955x->gpio.label = "gpio-pca955x";
567 pca955x->gpio.direction_input = pca955x_gpio_direction_input;
568 pca955x->gpio.direction_output = pca955x_gpio_direction_output;
569 pca955x->gpio.set = pca955x_gpio_set_value;
570 pca955x->gpio.get = pca955x_gpio_get_value;
571 pca955x->gpio.request = pca955x_gpio_request_pin;
572 pca955x->gpio.can_sleep = 1;
573 pca955x->gpio.base = -1;
574 pca955x->gpio.ngpio = ngpios;
575 pca955x->gpio.parent = &client->dev;
576 pca955x->gpio.owner = THIS_MODULE;
578 err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio,
581 /* Use data->gpio.dev as a flag for freeing gpiochip */
582 pca955x->gpio.parent = NULL;
583 dev_warn(&client->dev, "could not add gpiochip\n");
586 dev_info(&client->dev, "gpios %i...%i\n",
587 pca955x->gpio.base, pca955x->gpio.base +
588 pca955x->gpio.ngpio - 1);
595 static struct i2c_driver pca955x_driver = {
597 .name = "leds-pca955x",
598 .acpi_match_table = ACPI_PTR(pca955x_acpi_ids),
599 .of_match_table = of_match_ptr(of_pca955x_match),
601 .probe = pca955x_probe,
602 .id_table = pca955x_id,
605 module_i2c_driver(pca955x_driver);
607 MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
608 MODULE_DESCRIPTION("PCA955x LED driver");
609 MODULE_LICENSE("GPL v2");