1 /* 2 * Copyright 2007-2008 Extreme Engineering Solutions, Inc. 3 * 4 * Author: Nate Case <ncase@xes-inc.com> 5 * 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. 9 * 10 * LED driver for various PCA955x I2C LED drivers 11 * 12 * Supported devices: 13 * 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 21 * 22 * Philips PCA955x LED driver chips follow a register map as shown below: 23 * 24 * Control Register Description 25 * ---------------- ----------- 26 * 0x0 Input register 0 27 * .. 28 * NUM_INPUT_REGS - 1 Last Input register X 29 * 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 34 * 35 * NUM_INPUT_REGS + 4 LED selector 0 36 * NUM_INPUT_REGS + 4 37 * + NUM_LED_REGS - 1 Last LED selector 38 * 39 * where NUM_INPUT_REGS and NUM_LED_REGS vary depending on how many 40 * bits the chip supports. 41 */ 42 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> 52 #include <linux/of.h> 53 #include <linux/slab.h> 54 #include <linux/string.h> 55 56 #include <dt-bindings/leds/leds-pca955x.h> 57 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 */ 63 64 enum pca955x_type { 65 pca9550, 66 pca9551, 67 pca9552, 68 pca9553, 69 }; 70 71 struct pca955x_chipdef { 72 int bits; 73 u8 slv_addr; /* 7-bit slave address mask */ 74 int slv_addr_shift; /* Number of bits to ignore */ 75 }; 76 77 static struct pca955x_chipdef pca955x_chipdefs[] = { 78 [pca9550] = { 79 .bits = 2, 80 .slv_addr = /* 110000x */ 0x60, 81 .slv_addr_shift = 1, 82 }, 83 [pca9551] = { 84 .bits = 8, 85 .slv_addr = /* 1100xxx */ 0x60, 86 .slv_addr_shift = 3, 87 }, 88 [pca9552] = { 89 .bits = 16, 90 .slv_addr = /* 1100xxx */ 0x60, 91 .slv_addr_shift = 3, 92 }, 93 [pca9553] = { 94 .bits = 4, 95 .slv_addr = /* 110001x */ 0x62, 96 .slv_addr_shift = 1, 97 }, 98 }; 99 100 static const struct i2c_device_id pca955x_id[] = { 101 { "pca9550", pca9550 }, 102 { "pca9551", pca9551 }, 103 { "pca9552", pca9552 }, 104 { "pca9553", pca9553 }, 105 { } 106 }; 107 MODULE_DEVICE_TABLE(i2c, pca955x_id); 108 109 static const struct acpi_device_id pca955x_acpi_ids[] = { 110 { "PCA9550", pca9550 }, 111 { "PCA9551", pca9551 }, 112 { "PCA9552", pca9552 }, 113 { "PCA9553", pca9553 }, 114 { } 115 }; 116 MODULE_DEVICE_TABLE(acpi, pca955x_acpi_ids); 117 118 struct pca955x { 119 struct mutex lock; 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; 125 #endif 126 }; 127 128 struct pca955x_led { 129 struct pca955x *pca955x; 130 struct led_classdev led_cdev; 131 int led_num; /* 0 .. 15 potentially */ 132 char name[32]; 133 u32 type; 134 const char *default_trigger; 135 }; 136 137 struct pca955x_platform_data { 138 struct pca955x_led *leds; 139 int num_leds; 140 }; 141 142 /* 8 bits per input register */ 143 static inline int pca95xx_num_input_regs(int bits) 144 { 145 return (bits + 7) / 8; 146 } 147 148 /* 4 bits per LED selector register */ 149 static inline int pca95xx_num_led_regs(int bits) 150 { 151 return (bits + 3) / 4; 152 } 153 154 /* 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). 157 */ 158 static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state) 159 { 160 return (oldval & (~(0x3 << (led_num << 1)))) | 161 ((state & 0x3) << (led_num << 1)); 162 } 163 164 /* 165 * Write to frequency prescaler register, used to program the 166 * period of the PWM output. period = (PSCx + 1) / 38 167 */ 168 static int pca955x_write_psc(struct i2c_client *client, int n, u8 val) 169 { 170 struct pca955x *pca955x = i2c_get_clientdata(client); 171 int ret; 172 173 ret = i2c_smbus_write_byte_data(client, 174 pca95xx_num_input_regs(pca955x->chipdef->bits) + 2*n, 175 val); 176 if (ret < 0) 177 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", 178 __func__, n, val, ret); 179 return ret; 180 } 181 182 /* 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. 186 * 187 * Duty cycle is (256 - PWMx) / 256 188 */ 189 static int pca955x_write_pwm(struct i2c_client *client, int n, u8 val) 190 { 191 struct pca955x *pca955x = i2c_get_clientdata(client); 192 int ret; 193 194 ret = i2c_smbus_write_byte_data(client, 195 pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + 2*n, 196 val); 197 if (ret < 0) 198 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", 199 __func__, n, val, ret); 200 return ret; 201 } 202 203 /* 204 * Write to LED selector register, which determines the source that 205 * drives the LED output. 206 */ 207 static int pca955x_write_ls(struct i2c_client *client, int n, u8 val) 208 { 209 struct pca955x *pca955x = i2c_get_clientdata(client); 210 int ret; 211 212 ret = i2c_smbus_write_byte_data(client, 213 pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n, 214 val); 215 if (ret < 0) 216 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", 217 __func__, n, val, ret); 218 return ret; 219 } 220 221 /* 222 * Read the LED selector register, which determines the source that 223 * drives the LED output. 224 */ 225 static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val) 226 { 227 struct pca955x *pca955x = i2c_get_clientdata(client); 228 int ret; 229 230 ret = i2c_smbus_read_byte_data(client, 231 pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n); 232 if (ret < 0) { 233 dev_err(&client->dev, "%s: reg 0x%x, err %d\n", 234 __func__, n, ret); 235 return ret; 236 } 237 *val = (u8)ret; 238 return 0; 239 } 240 241 static int pca955x_led_set(struct led_classdev *led_cdev, 242 enum led_brightness value) 243 { 244 struct pca955x_led *pca955x_led; 245 struct pca955x *pca955x; 246 u8 ls; 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) */ 249 int ret; 250 251 pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev); 252 pca955x = pca955x_led->pca955x; 253 254 chip_ls = pca955x_led->led_num / 4; 255 ls_led = pca955x_led->led_num % 4; 256 257 mutex_lock(&pca955x->lock); 258 259 ret = pca955x_read_ls(pca955x->client, chip_ls, &ls); 260 if (ret) 261 goto out; 262 263 switch (value) { 264 case LED_FULL: 265 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON); 266 break; 267 case LED_OFF: 268 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF); 269 break; 270 case LED_HALF: 271 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0); 272 break; 273 default: 274 /* 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. 280 */ 281 ret = pca955x_write_pwm(pca955x->client, 1, 255 - value); 282 if (ret) 283 goto out; 284 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1); 285 break; 286 } 287 288 ret = pca955x_write_ls(pca955x->client, chip_ls, ls); 289 290 out: 291 mutex_unlock(&pca955x->lock); 292 293 return ret; 294 } 295 296 #ifdef CONFIG_LEDS_PCA955X_GPIO 297 /* 298 * Read the INPUT register, which contains the state of LEDs. 299 */ 300 static int pca955x_read_input(struct i2c_client *client, int n, u8 *val) 301 { 302 int ret = i2c_smbus_read_byte_data(client, n); 303 304 if (ret < 0) { 305 dev_err(&client->dev, "%s: reg 0x%x, err %d\n", 306 __func__, n, ret); 307 return ret; 308 } 309 *val = (u8)ret; 310 return 0; 311 312 } 313 314 static int pca955x_gpio_request_pin(struct gpio_chip *gc, unsigned int offset) 315 { 316 struct pca955x *pca955x = gpiochip_get_data(gc); 317 struct pca955x_led *led = &pca955x->leds[offset]; 318 319 if (led->type == PCA955X_TYPE_GPIO) 320 return 0; 321 322 return -EBUSY; 323 } 324 325 static int pca955x_set_value(struct gpio_chip *gc, unsigned int offset, 326 int val) 327 { 328 struct pca955x *pca955x = gpiochip_get_data(gc); 329 struct pca955x_led *led = &pca955x->leds[offset]; 330 331 if (val) 332 return pca955x_led_set(&led->led_cdev, LED_FULL); 333 else 334 return pca955x_led_set(&led->led_cdev, LED_OFF); 335 } 336 337 static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset, 338 int val) 339 { 340 pca955x_set_value(gc, offset, val); 341 } 342 343 static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset) 344 { 345 struct pca955x *pca955x = gpiochip_get_data(gc); 346 struct pca955x_led *led = &pca955x->leds[offset]; 347 u8 reg = 0; 348 349 /* There is nothing we can do about errors */ 350 pca955x_read_input(pca955x->client, led->led_num / 8, ®); 351 352 return !!(reg & (1 << (led->led_num % 8))); 353 } 354 355 static int pca955x_gpio_direction_input(struct gpio_chip *gc, 356 unsigned int offset) 357 { 358 /* To use as input ensure pin is not driven */ 359 return pca955x_set_value(gc, offset, 0); 360 } 361 362 static int pca955x_gpio_direction_output(struct gpio_chip *gc, 363 unsigned int offset, int val) 364 { 365 return pca955x_set_value(gc, offset, val); 366 } 367 #endif /* CONFIG_LEDS_PCA955X_GPIO */ 368 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) 372 { 373 struct device_node *np = client->dev.of_node; 374 struct device_node *child; 375 struct pca955x_platform_data *pdata; 376 int count; 377 378 count = of_get_child_count(np); 379 if (!count || count > chip->bits) 380 return ERR_PTR(-ENODEV); 381 382 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); 383 if (!pdata) 384 return ERR_PTR(-ENOMEM); 385 386 pdata->leds = devm_kzalloc(&client->dev, 387 sizeof(struct pca955x_led) * chip->bits, 388 GFP_KERNEL); 389 if (!pdata->leds) 390 return ERR_PTR(-ENOMEM); 391 392 for_each_child_of_node(np, child) { 393 const char *name; 394 u32 reg; 395 int res; 396 397 res = of_property_read_u32(child, "reg", ®); 398 if ((res != 0) || (reg >= chip->bits)) 399 continue; 400 401 if (of_property_read_string(child, "label", &name)) 402 name = child->name; 403 404 snprintf(pdata->leds[reg].name, sizeof(pdata->leds[reg].name), 405 "%s", name); 406 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); 411 } 412 413 pdata->num_leds = chip->bits; 414 415 return pdata; 416 } 417 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 }, 423 {}, 424 }; 425 426 MODULE_DEVICE_TABLE(of, of_pca955x_match); 427 #else 428 static struct pca955x_platform_data * 429 pca955x_pdata_of_init(struct i2c_client *client, struct pca955x_chipdef *chip) 430 { 431 return ERR_PTR(-ENODEV); 432 } 433 #endif 434 435 static int pca955x_probe(struct i2c_client *client, 436 const struct i2c_device_id *id) 437 { 438 struct pca955x *pca955x; 439 struct pca955x_led *pca955x_led; 440 struct pca955x_chipdef *chip; 441 struct i2c_adapter *adapter; 442 int i, err; 443 struct pca955x_platform_data *pdata; 444 int ngpios = 0; 445 446 if (id) { 447 chip = &pca955x_chipdefs[id->driver_data]; 448 } else { 449 const struct acpi_device_id *acpi_id; 450 451 acpi_id = acpi_match_device(pca955x_acpi_ids, &client->dev); 452 if (!acpi_id) 453 return -ENODEV; 454 chip = &pca955x_chipdefs[acpi_id->driver_data]; 455 } 456 adapter = to_i2c_adapter(client->dev.parent); 457 pdata = dev_get_platdata(&client->dev); 458 if (!pdata) { 459 pdata = pca955x_pdata_of_init(client, chip); 460 if (IS_ERR(pdata)) 461 return PTR_ERR(pdata); 462 } 463 464 /* Make sure the slave address / chip type combo given is possible */ 465 if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) != 466 chip->slv_addr) { 467 dev_err(&client->dev, "invalid slave address %02x\n", 468 client->addr); 469 return -ENODEV; 470 } 471 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); 475 476 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 477 return -EIO; 478 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); 483 return -ENODEV; 484 } 485 486 pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL); 487 if (!pca955x) 488 return -ENOMEM; 489 490 pca955x->leds = devm_kzalloc(&client->dev, 491 sizeof(*pca955x_led) * chip->bits, GFP_KERNEL); 492 if (!pca955x->leds) 493 return -ENOMEM; 494 495 i2c_set_clientdata(client, pca955x); 496 497 mutex_init(&pca955x->lock); 498 pca955x->client = client; 499 pca955x->chipdef = chip; 500 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; 506 507 switch (pca955x_led->type) { 508 case PCA955X_TYPE_NONE: 509 break; 510 case PCA955X_TYPE_GPIO: 511 ngpios++; 512 break; 513 case PCA955X_TYPE_LED: 514 /* 515 * Platform data can specify LED names and 516 * default triggers 517 */ 518 if (pdata->leds[i].name[0] == '\0') 519 snprintf(pdata->leds[i].name, 520 sizeof(pdata->leds[i].name), "%d", i); 521 522 snprintf(pca955x_led->name, 523 sizeof(pca955x_led->name), "pca955x:%s", 524 pdata->leds[i].name); 525 526 if (pdata->leds[i].default_trigger) 527 pca955x_led->led_cdev.default_trigger = 528 pdata->leds[i].default_trigger; 529 530 pca955x_led->led_cdev.name = pca955x_led->name; 531 pca955x_led->led_cdev.brightness_set_blocking = 532 pca955x_led_set; 533 534 err = devm_led_classdev_register(&client->dev, 535 &pca955x_led->led_cdev); 536 if (err) 537 return err; 538 539 /* Turn off LED */ 540 err = pca955x_led_set(&pca955x_led->led_cdev, LED_OFF); 541 if (err) 542 return err; 543 } 544 } 545 546 /* PWM0 is used for half brightness or 50% duty cycle */ 547 err = pca955x_write_pwm(client, 0, 255 - LED_HALF); 548 if (err) 549 return err; 550 551 /* PWM1 is used for variable brightness, default to OFF */ 552 err = pca955x_write_pwm(client, 1, 0); 553 if (err) 554 return err; 555 556 /* Set to fast frequency so we do not see flashing */ 557 err = pca955x_write_psc(client, 0, 0); 558 if (err) 559 return err; 560 err = pca955x_write_psc(client, 1, 0); 561 if (err) 562 return err; 563 564 #ifdef CONFIG_LEDS_PCA955X_GPIO 565 if (ngpios) { 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; 577 578 err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio, 579 pca955x); 580 if (err) { 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"); 584 return err; 585 } 586 dev_info(&client->dev, "gpios %i...%i\n", 587 pca955x->gpio.base, pca955x->gpio.base + 588 pca955x->gpio.ngpio - 1); 589 } 590 #endif 591 592 return 0; 593 } 594 595 static struct i2c_driver pca955x_driver = { 596 .driver = { 597 .name = "leds-pca955x", 598 .acpi_match_table = ACPI_PTR(pca955x_acpi_ids), 599 .of_match_table = of_match_ptr(of_pca955x_match), 600 }, 601 .probe = pca955x_probe, 602 .id_table = pca955x_id, 603 }; 604 605 module_i2c_driver(pca955x_driver); 606 607 MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>"); 608 MODULE_DESCRIPTION("PCA955x LED driver"); 609 MODULE_LICENSE("GPL v2"); 610