0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 #include <linux/bitops.h>
0041 #include <linux/ctype.h>
0042 #include <linux/delay.h>
0043 #include <linux/err.h>
0044 #include <linux/gpio/driver.h>
0045 #include <linux/i2c.h>
0046 #include <linux/leds.h>
0047 #include <linux/module.h>
0048 #include <linux/of.h>
0049 #include <linux/property.h>
0050 #include <linux/slab.h>
0051 #include <linux/string.h>
0052
0053 #include <dt-bindings/leds/leds-pca955x.h>
0054
0055
0056 #define PCA955X_LS_LED_ON 0x0
0057 #define PCA955X_LS_LED_OFF 0x1
0058 #define PCA955X_LS_BLINK0 0x2
0059 #define PCA955X_LS_BLINK1 0x3
0060
0061 #define PCA955X_GPIO_INPUT LED_OFF
0062 #define PCA955X_GPIO_HIGH LED_OFF
0063 #define PCA955X_GPIO_LOW LED_FULL
0064
0065 enum pca955x_type {
0066 pca9550,
0067 pca9551,
0068 pca9552,
0069 ibm_pca9552,
0070 pca9553,
0071 };
0072
0073 struct pca955x_chipdef {
0074 int bits;
0075 u8 slv_addr;
0076 int slv_addr_shift;
0077 };
0078
0079 static struct pca955x_chipdef pca955x_chipdefs[] = {
0080 [pca9550] = {
0081 .bits = 2,
0082 .slv_addr = 0x60,
0083 .slv_addr_shift = 1,
0084 },
0085 [pca9551] = {
0086 .bits = 8,
0087 .slv_addr = 0x60,
0088 .slv_addr_shift = 3,
0089 },
0090 [pca9552] = {
0091 .bits = 16,
0092 .slv_addr = 0x60,
0093 .slv_addr_shift = 3,
0094 },
0095 [ibm_pca9552] = {
0096 .bits = 16,
0097 .slv_addr = 0x30,
0098 .slv_addr_shift = 3,
0099 },
0100 [pca9553] = {
0101 .bits = 4,
0102 .slv_addr = 0x62,
0103 .slv_addr_shift = 1,
0104 },
0105 };
0106
0107 static const struct i2c_device_id pca955x_id[] = {
0108 { "pca9550", pca9550 },
0109 { "pca9551", pca9551 },
0110 { "pca9552", pca9552 },
0111 { "ibm-pca9552", ibm_pca9552 },
0112 { "pca9553", pca9553 },
0113 { }
0114 };
0115 MODULE_DEVICE_TABLE(i2c, pca955x_id);
0116
0117 struct pca955x {
0118 struct mutex lock;
0119 struct pca955x_led *leds;
0120 struct pca955x_chipdef *chipdef;
0121 struct i2c_client *client;
0122 unsigned long active_pins;
0123 #ifdef CONFIG_LEDS_PCA955X_GPIO
0124 struct gpio_chip gpio;
0125 #endif
0126 };
0127
0128 struct pca955x_led {
0129 struct pca955x *pca955x;
0130 struct led_classdev led_cdev;
0131 int led_num;
0132 u32 type;
0133 int default_state;
0134 struct fwnode_handle *fwnode;
0135 };
0136
0137 struct pca955x_platform_data {
0138 struct pca955x_led *leds;
0139 int num_leds;
0140 };
0141
0142
0143 static inline int pca95xx_num_input_regs(int bits)
0144 {
0145 return (bits + 7) / 8;
0146 }
0147
0148
0149 static inline int pca95xx_num_led_regs(int bits)
0150 {
0151 return (bits + 3) / 4;
0152 }
0153
0154
0155
0156
0157
0158 static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
0159 {
0160 return (oldval & (~(0x3 << (led_num << 1)))) |
0161 ((state & 0x3) << (led_num << 1));
0162 }
0163
0164
0165
0166
0167
0168 static int pca955x_write_psc(struct i2c_client *client, int n, u8 val)
0169 {
0170 struct pca955x *pca955x = i2c_get_clientdata(client);
0171 u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + (2 * n);
0172 int ret;
0173
0174 ret = i2c_smbus_write_byte_data(client, cmd, val);
0175 if (ret < 0)
0176 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
0177 __func__, n, val, ret);
0178 return ret;
0179 }
0180
0181
0182
0183
0184
0185
0186
0187
0188 static int pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
0189 {
0190 struct pca955x *pca955x = i2c_get_clientdata(client);
0191 u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
0192 int ret;
0193
0194 ret = i2c_smbus_write_byte_data(client, cmd, val);
0195 if (ret < 0)
0196 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
0197 __func__, n, val, ret);
0198 return ret;
0199 }
0200
0201
0202
0203
0204
0205 static int pca955x_write_ls(struct i2c_client *client, int n, u8 val)
0206 {
0207 struct pca955x *pca955x = i2c_get_clientdata(client);
0208 u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n;
0209 int ret;
0210
0211 ret = i2c_smbus_write_byte_data(client, cmd, val);
0212 if (ret < 0)
0213 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
0214 __func__, n, val, ret);
0215 return ret;
0216 }
0217
0218
0219
0220
0221
0222 static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val)
0223 {
0224 struct pca955x *pca955x = i2c_get_clientdata(client);
0225 u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n;
0226 int ret;
0227
0228 ret = i2c_smbus_read_byte_data(client, cmd);
0229 if (ret < 0) {
0230 dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
0231 __func__, n, ret);
0232 return ret;
0233 }
0234 *val = (u8)ret;
0235 return 0;
0236 }
0237
0238 static int pca955x_read_pwm(struct i2c_client *client, int n, u8 *val)
0239 {
0240 struct pca955x *pca955x = i2c_get_clientdata(client);
0241 u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
0242 int ret;
0243
0244 ret = i2c_smbus_read_byte_data(client, cmd);
0245 if (ret < 0) {
0246 dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
0247 __func__, n, ret);
0248 return ret;
0249 }
0250 *val = (u8)ret;
0251 return 0;
0252 }
0253
0254 static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
0255 {
0256 struct pca955x_led *pca955x_led = container_of(led_cdev,
0257 struct pca955x_led,
0258 led_cdev);
0259 struct pca955x *pca955x = pca955x_led->pca955x;
0260 u8 ls, pwm;
0261 int ret;
0262
0263 ret = pca955x_read_ls(pca955x->client, pca955x_led->led_num / 4, &ls);
0264 if (ret)
0265 return ret;
0266
0267 ls = (ls >> ((pca955x_led->led_num % 4) << 1)) & 0x3;
0268 switch (ls) {
0269 case PCA955X_LS_LED_ON:
0270 ret = LED_FULL;
0271 break;
0272 case PCA955X_LS_LED_OFF:
0273 ret = LED_OFF;
0274 break;
0275 case PCA955X_LS_BLINK0:
0276 ret = LED_HALF;
0277 break;
0278 case PCA955X_LS_BLINK1:
0279 ret = pca955x_read_pwm(pca955x->client, 1, &pwm);
0280 if (ret)
0281 return ret;
0282 ret = 255 - pwm;
0283 break;
0284 }
0285
0286 return ret;
0287 }
0288
0289 static int pca955x_led_set(struct led_classdev *led_cdev,
0290 enum led_brightness value)
0291 {
0292 struct pca955x_led *pca955x_led;
0293 struct pca955x *pca955x;
0294 u8 ls;
0295 int chip_ls;
0296 int ls_led;
0297 int ret;
0298
0299 pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev);
0300 pca955x = pca955x_led->pca955x;
0301
0302 chip_ls = pca955x_led->led_num / 4;
0303 ls_led = pca955x_led->led_num % 4;
0304
0305 mutex_lock(&pca955x->lock);
0306
0307 ret = pca955x_read_ls(pca955x->client, chip_ls, &ls);
0308 if (ret)
0309 goto out;
0310
0311 switch (value) {
0312 case LED_FULL:
0313 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
0314 break;
0315 case LED_OFF:
0316 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF);
0317 break;
0318 case LED_HALF:
0319 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0);
0320 break;
0321 default:
0322
0323
0324
0325
0326
0327
0328
0329 ret = pca955x_write_pwm(pca955x->client, 1, 255 - value);
0330 if (ret)
0331 goto out;
0332 ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
0333 break;
0334 }
0335
0336 ret = pca955x_write_ls(pca955x->client, chip_ls, ls);
0337
0338 out:
0339 mutex_unlock(&pca955x->lock);
0340
0341 return ret;
0342 }
0343
0344 #ifdef CONFIG_LEDS_PCA955X_GPIO
0345
0346
0347
0348 static int pca955x_read_input(struct i2c_client *client, int n, u8 *val)
0349 {
0350 int ret = i2c_smbus_read_byte_data(client, n);
0351
0352 if (ret < 0) {
0353 dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
0354 __func__, n, ret);
0355 return ret;
0356 }
0357 *val = (u8)ret;
0358 return 0;
0359
0360 }
0361
0362 static int pca955x_gpio_request_pin(struct gpio_chip *gc, unsigned int offset)
0363 {
0364 struct pca955x *pca955x = gpiochip_get_data(gc);
0365
0366 return test_and_set_bit(offset, &pca955x->active_pins) ? -EBUSY : 0;
0367 }
0368
0369 static void pca955x_gpio_free_pin(struct gpio_chip *gc, unsigned int offset)
0370 {
0371 struct pca955x *pca955x = gpiochip_get_data(gc);
0372
0373 clear_bit(offset, &pca955x->active_pins);
0374 }
0375
0376 static int pca955x_set_value(struct gpio_chip *gc, unsigned int offset,
0377 int val)
0378 {
0379 struct pca955x *pca955x = gpiochip_get_data(gc);
0380 struct pca955x_led *led = &pca955x->leds[offset];
0381
0382 if (val)
0383 return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_HIGH);
0384
0385 return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_LOW);
0386 }
0387
0388 static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset,
0389 int val)
0390 {
0391 pca955x_set_value(gc, offset, val);
0392 }
0393
0394 static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
0395 {
0396 struct pca955x *pca955x = gpiochip_get_data(gc);
0397 struct pca955x_led *led = &pca955x->leds[offset];
0398 u8 reg = 0;
0399
0400
0401 pca955x_read_input(pca955x->client, led->led_num / 8, ®);
0402
0403 return !!(reg & (1 << (led->led_num % 8)));
0404 }
0405
0406 static int pca955x_gpio_direction_input(struct gpio_chip *gc,
0407 unsigned int offset)
0408 {
0409 struct pca955x *pca955x = gpiochip_get_data(gc);
0410 struct pca955x_led *led = &pca955x->leds[offset];
0411
0412
0413 return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_INPUT);
0414 }
0415
0416 static int pca955x_gpio_direction_output(struct gpio_chip *gc,
0417 unsigned int offset, int val)
0418 {
0419 return pca955x_set_value(gc, offset, val);
0420 }
0421 #endif
0422
0423 static struct pca955x_platform_data *
0424 pca955x_get_pdata(struct i2c_client *client, struct pca955x_chipdef *chip)
0425 {
0426 struct pca955x_platform_data *pdata;
0427 struct pca955x_led *led;
0428 struct fwnode_handle *child;
0429 int count;
0430
0431 count = device_get_child_node_count(&client->dev);
0432 if (count > chip->bits)
0433 return ERR_PTR(-ENODEV);
0434
0435 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
0436 if (!pdata)
0437 return ERR_PTR(-ENOMEM);
0438
0439 pdata->leds = devm_kcalloc(&client->dev,
0440 chip->bits, sizeof(struct pca955x_led),
0441 GFP_KERNEL);
0442 if (!pdata->leds)
0443 return ERR_PTR(-ENOMEM);
0444
0445 device_for_each_child_node(&client->dev, child) {
0446 const char *state;
0447 u32 reg;
0448 int res;
0449
0450 res = fwnode_property_read_u32(child, "reg", ®);
0451 if ((res != 0) || (reg >= chip->bits))
0452 continue;
0453
0454 led = &pdata->leds[reg];
0455 led->type = PCA955X_TYPE_LED;
0456 led->fwnode = child;
0457 fwnode_property_read_u32(child, "type", &led->type);
0458
0459 if (!fwnode_property_read_string(child, "default-state",
0460 &state)) {
0461 if (!strcmp(state, "keep"))
0462 led->default_state = LEDS_GPIO_DEFSTATE_KEEP;
0463 else if (!strcmp(state, "on"))
0464 led->default_state = LEDS_GPIO_DEFSTATE_ON;
0465 else
0466 led->default_state = LEDS_GPIO_DEFSTATE_OFF;
0467 } else {
0468 led->default_state = LEDS_GPIO_DEFSTATE_OFF;
0469 }
0470 }
0471
0472 pdata->num_leds = chip->bits;
0473
0474 return pdata;
0475 }
0476
0477 static const struct of_device_id of_pca955x_match[] = {
0478 { .compatible = "nxp,pca9550", .data = (void *)pca9550 },
0479 { .compatible = "nxp,pca9551", .data = (void *)pca9551 },
0480 { .compatible = "nxp,pca9552", .data = (void *)pca9552 },
0481 { .compatible = "ibm,pca9552", .data = (void *)ibm_pca9552 },
0482 { .compatible = "nxp,pca9553", .data = (void *)pca9553 },
0483 {},
0484 };
0485 MODULE_DEVICE_TABLE(of, of_pca955x_match);
0486
0487 static int pca955x_probe(struct i2c_client *client)
0488 {
0489 struct pca955x *pca955x;
0490 struct pca955x_led *pca955x_led;
0491 struct pca955x_chipdef *chip;
0492 struct led_classdev *led;
0493 struct led_init_data init_data;
0494 struct i2c_adapter *adapter;
0495 int i, err;
0496 struct pca955x_platform_data *pdata;
0497 bool set_default_label = false;
0498 bool keep_pwm = false;
0499 char default_label[8];
0500 enum pca955x_type chip_type;
0501 const void *md = device_get_match_data(&client->dev);
0502
0503 if (md) {
0504 chip_type = (enum pca955x_type)md;
0505 } else {
0506 const struct i2c_device_id *id = i2c_match_id(pca955x_id,
0507 client);
0508
0509 if (id) {
0510 chip_type = (enum pca955x_type)id->driver_data;
0511 } else {
0512 dev_err(&client->dev, "unknown chip\n");
0513 return -ENODEV;
0514 }
0515 }
0516
0517 chip = &pca955x_chipdefs[chip_type];
0518 adapter = client->adapter;
0519 pdata = dev_get_platdata(&client->dev);
0520 if (!pdata) {
0521 pdata = pca955x_get_pdata(client, chip);
0522 if (IS_ERR(pdata))
0523 return PTR_ERR(pdata);
0524 }
0525
0526
0527 if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
0528 chip->slv_addr) {
0529 dev_err(&client->dev, "invalid slave address %02x\n",
0530 client->addr);
0531 return -ENODEV;
0532 }
0533
0534 dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at "
0535 "slave address 0x%02x\n", client->name, chip->bits,
0536 client->addr);
0537
0538 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0539 return -EIO;
0540
0541 if (pdata->num_leds != chip->bits) {
0542 dev_err(&client->dev,
0543 "board info claims %d LEDs on a %d-bit chip\n",
0544 pdata->num_leds, chip->bits);
0545 return -ENODEV;
0546 }
0547
0548 pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
0549 if (!pca955x)
0550 return -ENOMEM;
0551
0552 pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
0553 sizeof(*pca955x_led), GFP_KERNEL);
0554 if (!pca955x->leds)
0555 return -ENOMEM;
0556
0557 i2c_set_clientdata(client, pca955x);
0558
0559 mutex_init(&pca955x->lock);
0560 pca955x->client = client;
0561 pca955x->chipdef = chip;
0562
0563 init_data.devname_mandatory = false;
0564 init_data.devicename = "pca955x";
0565
0566 for (i = 0; i < chip->bits; i++) {
0567 pca955x_led = &pca955x->leds[i];
0568 pca955x_led->led_num = i;
0569 pca955x_led->pca955x = pca955x;
0570 pca955x_led->type = pdata->leds[i].type;
0571
0572 switch (pca955x_led->type) {
0573 case PCA955X_TYPE_NONE:
0574 case PCA955X_TYPE_GPIO:
0575 break;
0576 case PCA955X_TYPE_LED:
0577 led = &pca955x_led->led_cdev;
0578 led->brightness_set_blocking = pca955x_led_set;
0579 led->brightness_get = pca955x_led_get;
0580
0581 if (pdata->leds[i].default_state ==
0582 LEDS_GPIO_DEFSTATE_OFF) {
0583 err = pca955x_led_set(led, LED_OFF);
0584 if (err)
0585 return err;
0586 } else if (pdata->leds[i].default_state ==
0587 LEDS_GPIO_DEFSTATE_ON) {
0588 err = pca955x_led_set(led, LED_FULL);
0589 if (err)
0590 return err;
0591 }
0592
0593 init_data.fwnode = pdata->leds[i].fwnode;
0594
0595 if (is_of_node(init_data.fwnode)) {
0596 if (to_of_node(init_data.fwnode)->name[0] ==
0597 '\0')
0598 set_default_label = true;
0599 else
0600 set_default_label = false;
0601 } else {
0602 set_default_label = true;
0603 }
0604
0605 if (set_default_label) {
0606 snprintf(default_label, sizeof(default_label),
0607 "%d", i);
0608 init_data.default_label = default_label;
0609 } else {
0610 init_data.default_label = NULL;
0611 }
0612
0613 err = devm_led_classdev_register_ext(&client->dev, led,
0614 &init_data);
0615 if (err)
0616 return err;
0617
0618 set_bit(i, &pca955x->active_pins);
0619
0620
0621
0622
0623
0624
0625
0626 if (pdata->leds[i].default_state ==
0627 LEDS_GPIO_DEFSTATE_KEEP) {
0628 if (led->brightness != LED_FULL &&
0629 led->brightness != LED_OFF &&
0630 led->brightness != LED_HALF)
0631 keep_pwm = true;
0632 }
0633 }
0634 }
0635
0636
0637 err = pca955x_write_pwm(client, 0, 255 - LED_HALF);
0638 if (err)
0639 return err;
0640
0641 if (!keep_pwm) {
0642
0643 err = pca955x_write_pwm(client, 1, 0);
0644 if (err)
0645 return err;
0646 }
0647
0648
0649 err = pca955x_write_psc(client, 0, 0);
0650 if (err)
0651 return err;
0652 err = pca955x_write_psc(client, 1, 0);
0653 if (err)
0654 return err;
0655
0656 #ifdef CONFIG_LEDS_PCA955X_GPIO
0657 pca955x->gpio.label = "gpio-pca955x";
0658 pca955x->gpio.direction_input = pca955x_gpio_direction_input;
0659 pca955x->gpio.direction_output = pca955x_gpio_direction_output;
0660 pca955x->gpio.set = pca955x_gpio_set_value;
0661 pca955x->gpio.get = pca955x_gpio_get_value;
0662 pca955x->gpio.request = pca955x_gpio_request_pin;
0663 pca955x->gpio.free = pca955x_gpio_free_pin;
0664 pca955x->gpio.can_sleep = 1;
0665 pca955x->gpio.base = -1;
0666 pca955x->gpio.ngpio = chip->bits;
0667 pca955x->gpio.parent = &client->dev;
0668 pca955x->gpio.owner = THIS_MODULE;
0669
0670 err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio,
0671 pca955x);
0672 if (err) {
0673
0674 pca955x->gpio.parent = NULL;
0675 dev_warn(&client->dev, "could not add gpiochip\n");
0676 return err;
0677 }
0678 dev_info(&client->dev, "gpios %i...%i\n",
0679 pca955x->gpio.base, pca955x->gpio.base +
0680 pca955x->gpio.ngpio - 1);
0681 #endif
0682
0683 return 0;
0684 }
0685
0686 static struct i2c_driver pca955x_driver = {
0687 .driver = {
0688 .name = "leds-pca955x",
0689 .of_match_table = of_pca955x_match,
0690 },
0691 .probe_new = pca955x_probe,
0692 .id_table = pca955x_id,
0693 };
0694
0695 module_i2c_driver(pca955x_driver);
0696
0697 MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
0698 MODULE_DESCRIPTION("PCA955x LED driver");
0699 MODULE_LICENSE("GPL v2");