Lines Matching +full:led +full:- +full:4
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2007-2008 Extreme Engineering Solutions, Inc.
5 * Author: Nate Case <ncase@xes-inc.com>
7 * LED driver for various PCA955x I2C LED drivers
11 * Device Description 7-bit slave address
12 * ------ ----------- -------------------
13 * PCA9550 2-bit driver 0x60 .. 0x61
14 * PCA9551 8-bit driver 0x60 .. 0x67
15 * PCA9552 16-bit driver 0x60 .. 0x67
16 * PCA9553/01 4-bit driver 0x62
17 * PCA9553/02 4-bit driver 0x63
19 * Philips PCA955x LED driver chips follow a register map as shown below:
22 * ---------------- -----------
25 * NUM_INPUT_REGS - 1 Last Input register X
32 * NUM_INPUT_REGS + 4 LED selector 0
33 * NUM_INPUT_REGS + 4
34 * + NUM_LED_REGS - 1 Last LED selector
53 #include <dt-bindings/leds/leds-pca955x.h>
55 /* LED select registers determine the source that drives LED outputs */
57 #define PCA955X_LS_LED_OFF 0x1 /* Output HI-Z */
75 u8 slv_addr; /* 7-bit slave address mask */
101 .bits = 4,
139 * Return an LED selector register value based on an existing one, with
140 * the appropriate 2-bit state value set for the given LED number (0-3).
155 u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + (2 * n); in pca955x_write_psc()
160 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", in pca955x_write_psc()
167 * output. LED is OFF when the count is less than the value of this
168 * register, and ON when it is greater. If PWMx == 0, LED is always OFF.
170 * Duty cycle is (256 - PWMx) / 256
175 u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n); in pca955x_write_pwm()
180 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", in pca955x_write_pwm()
186 * Write to LED selector register, which determines the source that
187 * drives the LED output.
192 u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n; in pca955x_write_ls()
197 dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n", in pca955x_write_ls()
203 * Read the LED selector register, which determines the source that
204 * drives the LED output.
209 u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n; in pca955x_read_ls()
214 dev_err(&client->dev, "%s: reg 0x%x, err %d\n", in pca955x_read_ls()
225 u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n); in pca955x_read_pwm()
230 dev_err(&client->dev, "%s: reg 0x%x, err %d\n", in pca955x_read_pwm()
243 struct pca955x *pca955x = pca955x_led->pca955x; in pca955x_led_get()
247 ret = pca955x_read_ls(pca955x->client, pca955x_led->led_num / 4, &ls); in pca955x_led_get()
251 ls = (ls >> ((pca955x_led->led_num % 4) << 1)) & 0x3; in pca955x_led_get()
263 ret = pca955x_read_pwm(pca955x->client, 1, &pwm); in pca955x_led_get()
266 ret = 255 - pwm; in pca955x_led_get()
279 int chip_ls; /* which LSx to use (0-3 potentially) */ in pca955x_led_set()
280 int ls_led; /* which set of bits within LSx to use (0-3) */ in pca955x_led_set()
284 pca955x = pca955x_led->pca955x; in pca955x_led_set()
286 chip_ls = pca955x_led->led_num / 4; in pca955x_led_set()
287 ls_led = pca955x_led->led_num % 4; in pca955x_led_set()
289 mutex_lock(&pca955x->lock); in pca955x_led_set()
291 ret = pca955x_read_ls(pca955x->client, chip_ls, &ls); in pca955x_led_set()
313 ret = pca955x_write_pwm(pca955x->client, 1, 255 - value); in pca955x_led_set()
320 ret = pca955x_write_ls(pca955x->client, chip_ls, ls); in pca955x_led_set()
323 mutex_unlock(&pca955x->lock); in pca955x_led_set()
337 dev_err(&client->dev, "%s: reg 0x%x, err %d\n", in pca955x_read_input()
350 return test_and_set_bit(offset, &pca955x->active_pins) ? -EBUSY : 0; in pca955x_gpio_request_pin()
357 clear_bit(offset, &pca955x->active_pins); in pca955x_gpio_free_pin()
364 struct pca955x_led *led = &pca955x->leds[offset]; in pca955x_set_value() local
367 return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_HIGH); in pca955x_set_value()
369 return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_LOW); in pca955x_set_value()
381 struct pca955x_led *led = &pca955x->leds[offset]; in pca955x_gpio_get_value() local
385 pca955x_read_input(pca955x->client, led->led_num / 8, ®); in pca955x_gpio_get_value()
387 return !!(reg & (1 << (led->led_num % 8))); in pca955x_gpio_get_value()
394 struct pca955x_led *led = &pca955x->leds[offset]; in pca955x_gpio_direction_input() local
397 return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_INPUT); in pca955x_gpio_direction_input()
411 struct pca955x_led *led; in pca955x_get_pdata() local
415 count = device_get_child_node_count(&client->dev); in pca955x_get_pdata()
416 if (count > chip->bits) in pca955x_get_pdata()
417 return ERR_PTR(-ENODEV); in pca955x_get_pdata()
419 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); in pca955x_get_pdata()
421 return ERR_PTR(-ENOMEM); in pca955x_get_pdata()
423 pdata->leds = devm_kcalloc(&client->dev, in pca955x_get_pdata()
424 chip->bits, sizeof(struct pca955x_led), in pca955x_get_pdata()
426 if (!pdata->leds) in pca955x_get_pdata()
427 return ERR_PTR(-ENOMEM); in pca955x_get_pdata()
429 device_for_each_child_node(&client->dev, child) { in pca955x_get_pdata()
434 if ((res != 0) || (reg >= chip->bits)) in pca955x_get_pdata()
437 led = &pdata->leds[reg]; in pca955x_get_pdata()
438 led->type = PCA955X_TYPE_LED; in pca955x_get_pdata()
439 led->fwnode = child; in pca955x_get_pdata()
440 led->default_state = led_init_default_state_get(child); in pca955x_get_pdata()
442 fwnode_property_read_u32(child, "type", &led->type); in pca955x_get_pdata()
445 pdata->num_leds = chip->bits; in pca955x_get_pdata()
455 struct led_classdev *led; in pca955x_probe() local
466 return dev_err_probe(&client->dev, -ENODEV, "unknown chip\n"); in pca955x_probe()
468 adapter = client->adapter; in pca955x_probe()
469 pdata = dev_get_platdata(&client->dev); in pca955x_probe()
477 if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) != in pca955x_probe()
478 chip->slv_addr) { in pca955x_probe()
479 dev_err(&client->dev, "invalid slave address %02x\n", in pca955x_probe()
480 client->addr); in pca955x_probe()
481 return -ENODEV; in pca955x_probe()
484 dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at " in pca955x_probe()
485 "slave address 0x%02x\n", client->name, chip->bits, in pca955x_probe()
486 client->addr); in pca955x_probe()
489 return -EIO; in pca955x_probe()
491 if (pdata->num_leds != chip->bits) { in pca955x_probe()
492 dev_err(&client->dev, in pca955x_probe()
493 "board info claims %d LEDs on a %d-bit chip\n", in pca955x_probe()
494 pdata->num_leds, chip->bits); in pca955x_probe()
495 return -ENODEV; in pca955x_probe()
498 pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL); in pca955x_probe()
500 return -ENOMEM; in pca955x_probe()
502 pca955x->leds = devm_kcalloc(&client->dev, chip->bits, in pca955x_probe()
504 if (!pca955x->leds) in pca955x_probe()
505 return -ENOMEM; in pca955x_probe()
509 mutex_init(&pca955x->lock); in pca955x_probe()
510 pca955x->client = client; in pca955x_probe()
511 pca955x->chipdef = chip; in pca955x_probe()
516 for (i = 0; i < chip->bits; i++) { in pca955x_probe()
517 pca955x_led = &pca955x->leds[i]; in pca955x_probe()
518 pca955x_led->led_num = i; in pca955x_probe()
519 pca955x_led->pca955x = pca955x; in pca955x_probe()
520 pca955x_led->type = pdata->leds[i].type; in pca955x_probe()
522 switch (pca955x_led->type) { in pca955x_probe()
527 led = &pca955x_led->led_cdev; in pca955x_probe()
528 led->brightness_set_blocking = pca955x_led_set; in pca955x_probe()
529 led->brightness_get = pca955x_led_get; in pca955x_probe()
531 if (pdata->leds[i].default_state == LEDS_DEFSTATE_OFF) { in pca955x_probe()
532 err = pca955x_led_set(led, LED_OFF); in pca955x_probe()
535 } else if (pdata->leds[i].default_state == LEDS_DEFSTATE_ON) { in pca955x_probe()
536 err = pca955x_led_set(led, LED_FULL); in pca955x_probe()
541 init_data.fwnode = pdata->leds[i].fwnode; in pca955x_probe()
544 if (to_of_node(init_data.fwnode)->name[0] == in pca955x_probe()
561 err = devm_led_classdev_register_ext(&client->dev, led, in pca955x_probe()
566 set_bit(i, &pca955x->active_pins); in pca955x_probe()
569 * For default-state == "keep", let the core update the in pca955x_probe()
574 if (pdata->leds[i].default_state == LEDS_DEFSTATE_KEEP) { in pca955x_probe()
575 if (led->brightness != LED_FULL && in pca955x_probe()
576 led->brightness != LED_OFF && in pca955x_probe()
577 led->brightness != LED_HALF) in pca955x_probe()
584 err = pca955x_write_pwm(client, 0, 255 - LED_HALF); in pca955x_probe()
604 pca955x->gpio.label = "gpio-pca955x"; in pca955x_probe()
605 pca955x->gpio.direction_input = pca955x_gpio_direction_input; in pca955x_probe()
606 pca955x->gpio.direction_output = pca955x_gpio_direction_output; in pca955x_probe()
607 pca955x->gpio.set = pca955x_gpio_set_value; in pca955x_probe()
608 pca955x->gpio.get = pca955x_gpio_get_value; in pca955x_probe()
609 pca955x->gpio.request = pca955x_gpio_request_pin; in pca955x_probe()
610 pca955x->gpio.free = pca955x_gpio_free_pin; in pca955x_probe()
611 pca955x->gpio.can_sleep = 1; in pca955x_probe()
612 pca955x->gpio.base = -1; in pca955x_probe()
613 pca955x->gpio.ngpio = chip->bits; in pca955x_probe()
614 pca955x->gpio.parent = &client->dev; in pca955x_probe()
615 pca955x->gpio.owner = THIS_MODULE; in pca955x_probe()
617 err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio, in pca955x_probe()
620 /* Use data->gpio.dev as a flag for freeing gpiochip */ in pca955x_probe()
621 pca955x->gpio.parent = NULL; in pca955x_probe()
622 dev_warn(&client->dev, "could not add gpiochip\n"); in pca955x_probe()
625 dev_info(&client->dev, "gpios %i...%i\n", in pca955x_probe()
626 pca955x->gpio.base, pca955x->gpio.base + in pca955x_probe()
627 pca955x->gpio.ngpio - 1); in pca955x_probe()
637 { "ibm-pca9552", (kernel_ulong_t)&pca955x_chipdefs[ibm_pca9552] },
655 .name = "leds-pca955x",
664 MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
665 MODULE_DESCRIPTION("PCA955x LED driver");