Skip to contentSkip to conversation

leds: leds-pca995x: Add support for NXP PCA9956B

PATCH Superseded Pieterjan Camerlynck 2 participants 4 messages Jul 10, 2024 – Jul 11, 2024

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover leds: leds-pca995x: Add support for NXP PCA9956B

This series adds support for NXP PCA9956B to the pca995x driver. This chip is similar to the others but has 24 instead of 16 outputs and a slightly different register layout. Datasheet available at [1].

[1]: https://www.nxp.com/docs/en/data-sheet/PCA9956B.pdf

---
Changes in v2:
- define seperate const struct pca995x_chipdef for each chip
- remove chip enum
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Pieterjan Camerlynck (2):
dt-bindings: leds: pca995x: Add new nxp,pca9956b compatible
leds: leds-pca995x: Add support for NXP PCA9956B

 .../devicetree/bindings/leds/nxp,pca995x.yaml      |  6 +-
 drivers/leds/leds-pca995x.c                        | 82 ++++++++++++----------
 2 files changed, 50 insertions(+), 38 deletions(-)

---
base-commit: 82d01fe6ee52086035b201cfa1410a3b04384257
change-id: 20240709-pca995x-d7c5f97f6ebc

Best regards,

Signature · 1 line
Pieterjan Camerlynck <[email protected]>
#1 lore raw

PATCH v2 1/2 dt-bindings: leds: pca995x: Add new nxp,pca9956b compatible

Add nxp,pca9956b compatible, which has 24 instead of 16 outputs. Adjust the documentation for the difference.

---
 Documentation/devicetree/bindings/leds/nxp,pca995x.yaml | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/leds/nxp,pca995x.yaml +4 -2
index 654915c1f687..ab8c90cbadb5 100644
@@ -11,19 +11,21 @@ maintainers:
   - Marek Vasut <[email protected]>
 
 description:
-  The NXP PCA9952/PCA9955B are programmable LED controllers connected via I2C
-  that can drive 16 separate lines. Each of them can be individually switched
+  The NXP PCA995x family are programmable LED controllers connected via I2C
+  that can drive separate lines. Each of them can be individually switched
   on and off, and brightness can be controlled via individual PWM.
 
   Datasheets are available at
   https://www.nxp.com/docs/en/data-sheet/PCA9952_PCA9955.pdf
   https://www.nxp.com/docs/en/data-sheet/PCA9955B.pdf
+  https://www.nxp.com/docs/en/data-sheet/PCA9956B.pdf
 
 properties:
   compatible:
     enum:
       - nxp,pca9952
       - nxp,pca9955b
+      - nxp,pca9956b
 
   reg:
     maxItems: 1
Signature · 1 line
2.45.2
#2 lore raw

PATCH v2 2/2 leds: leds-pca995x: Add support for NXP PCA9956B

In reply to Pieterjan Camerlynck

Add support for PCA9956B chip, which belongs to the same family.

This chip features 24 instead of 16 outputs, so add a chipdef struct to deal with the different register layouts.

---
 drivers/leds/leds-pca995x.c | 82 +++++++++++++++++++++++++--------------------
 1 file changed, 46 insertions(+), 36 deletions(-)
drivers/leds/leds-pca995x.c +46 -36
index 78215dff1499..d783d643d7e9 100644
@@ -19,10 +19,6 @@
 #define PCA995X_MODE1			0x00
 #define PCA995X_MODE2			0x01
 #define PCA995X_LEDOUT0			0x02
-#define PCA9955B_PWM0			0x08
-#define PCA9952_PWM0			0x0A
-#define PCA9952_IREFALL			0x43
-#define PCA9955B_IREFALL		0x45
 
 /* Auto-increment disabled. Normal mode */
 #define PCA995X_MODE1_CFG		0x00
@@ -34,17 +30,37 @@
 #define PCA995X_LDRX_MASK		0x3
 #define PCA995X_LDRX_BITS		2
 
-#define PCA995X_MAX_OUTPUTS		16
 #define PCA995X_OUTPUTS_PER_REG		4
 
 #define PCA995X_IREFALL_FULL_CFG	0xFF
 #define PCA995X_IREFALL_HALF_CFG	(PCA995X_IREFALL_FULL_CFG / 2)
 
-#define PCA995X_TYPE_NON_B		0
-#define PCA995X_TYPE_B			1
-
 #define ldev_to_led(c)	container_of(c, struct pca995x_led, ldev)
 
+struct pca995x_chipdef {
+	unsigned int num_leds;
+	u8 pwm_base;
+	u8 irefall;
+};
+
+static const struct pca995x_chipdef pca9952_chipdef = {
+	.num_leds	= 16,
+	.pwm_base	= 0x0a,
+	.irefall	= 0x43,
+};
+
+static const struct pca995x_chipdef pca9955b_chipdef = {
+	.num_leds	= 16,
+	.pwm_base	= 0x08,
+	.irefall	= 0x45,
+};
+
+static const struct pca995x_chipdef pca9956b_chipdef = {
+	.num_leds	= 24,
+	.pwm_base	= 0x0a,
+	.irefall	= 0x40,
+};
+
 struct pca995x_led {
 	unsigned int led_no;
 	struct led_classdev ldev;
@@ -52,9 +68,9 @@ struct pca995x_led {
 };
 
 struct pca995x_chip {
+	const struct pca995x_chipdef *chipdef;
 	struct regmap *regmap;
-	struct pca995x_led leds[PCA995X_MAX_OUTPUTS];
-	int btype;
+	struct pca995x_led leds[];
 };
 
 static int pca995x_brightness_set(struct led_classdev *led_cdev,
@@ -62,10 +78,11 @@ static int pca995x_brightness_set(struct led_classdev *led_cdev,
 {
 	struct pca995x_led *led = ldev_to_led(led_cdev);
 	struct pca995x_chip *chip = led->chip;
+	const struct pca995x_chipdef *chipdef = chip->chipdef;
 	u8 ledout_addr, pwmout_addr;
 	int shift, ret;
 
-	pwmout_addr = (chip->btype ? PCA9955B_PWM0 : PCA9952_PWM0) + led->led_no;
+	pwmout_addr = (chipdef->pwm_base) + led->led_no;
 	ledout_addr = PCA995X_LEDOUT0 + (led->led_no / PCA995X_OUTPUTS_PER_REG);
 	shift = PCA995X_LDRX_BITS * (led->led_no % PCA995X_OUTPUTS_PER_REG);
 
@@ -101,24 +118,24 @@ static const struct regmap_config pca995x_regmap = {
 
 static int pca995x_probe(struct i2c_client *client)
 {
-	struct fwnode_handle *led_fwnodes[PCA995X_MAX_OUTPUTS] = { 0 };
 	struct fwnode_handle *np, *child;
 	struct device *dev = &client->dev;
+	const struct pca995x_chipdef *chipdef;
 	struct pca995x_chip *chip;
 	struct pca995x_led *led;
-	int i, btype, reg, ret;
+	int reg, ret;
 
-	btype = (unsigned long)device_get_match_data(&client->dev);
+	chipdef = device_get_match_data(&client->dev);
 
 	np = dev_fwnode(dev);
 	if (!np)
 		return -ENODEV;
 
-	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
+	chip = devm_kzalloc(dev, struct_size(chip, leds, chipdef->num_leds), GFP_KERNEL);
 	if (!chip)
 		return -ENOMEM;
 
-	chip->btype = btype;
+	chip->chipdef = chipdef;
 	chip->regmap = devm_regmap_init_i2c(client, &pca995x_regmap);
 	if (IS_ERR(chip->regmap))
 		return PTR_ERR(chip->regmap);
@@ -126,41 +143,34 @@ static int pca995x_probe(struct i2c_client *client)
 	i2c_set_clientdata(client, chip);
 
 	fwnode_for_each_available_child_node(np, child) {
+		struct led_init_data init_data = {};
+
 		ret = fwnode_property_read_u32(child, "reg", &reg);
 		if (ret) {
 			fwnode_handle_put(child);
 			return ret;
 		}
 
-		if (reg < 0 || reg >= PCA995X_MAX_OUTPUTS || led_fwnodes[reg]) {
+		if (reg < 0 || reg >= chipdef->num_leds) {
 			fwnode_handle_put(child);
 			return -EINVAL;
 		}
 
 		led = &chip->leds[reg];
-		led_fwnodes[reg] = child;
 		led->chip = chip;
 		led->led_no = reg;
 		led->ldev.brightness_set_blocking = pca995x_brightness_set;
 		led->ldev.max_brightness = 255;
-	}
-
-	for (i = 0; i < PCA995X_MAX_OUTPUTS; i++) {
-		struct led_init_data init_data = {};
-
-		if (!led_fwnodes[i])
-			continue;
-
-		init_data.fwnode = led_fwnodes[i];
+		init_data.fwnode = child;
 
 		ret = devm_led_classdev_register_ext(dev,
-						     &chip->leds[i].ldev,
+						     &led->ldev,
 						     &init_data);
 		if (ret < 0) {
 			fwnode_handle_put(child);
 			return dev_err_probe(dev, ret,
 					     "Could not register LED %s\n",
-					     chip->leds[i].ldev.name);
+					     led->ldev.name);
 		}
 	}
 
@@ -170,21 +180,21 @@ static int pca995x_probe(struct i2c_client *client)
 		return ret;
 
 	/* IREF Output current value for all LEDn outputs */
-	return regmap_write(chip->regmap,
-			    btype ? PCA9955B_IREFALL : PCA9952_IREFALL,
-			    PCA995X_IREFALL_HALF_CFG);
+	return regmap_write(chip->regmap, chipdef->irefall, PCA995X_IREFALL_HALF_CFG);
 }
 
 static const struct i2c_device_id pca995x_id[] = {
-	{ "pca9952", .driver_data = (kernel_ulong_t)PCA995X_TYPE_NON_B },
-	{ "pca9955b", .driver_data = (kernel_ulong_t)PCA995X_TYPE_B },
+	{ "pca9952", .driver_data = (kernel_ulong_t)&pca9952_chipdef },
+	{ "pca9955b", .driver_data = (kernel_ulong_t)&pca9955b_chipdef },
+	{ "pca9956b", .driver_data = (kernel_ulong_t)&pca9956b_chipdef },
 	{}
 };
 MODULE_DEVICE_TABLE(i2c, pca995x_id);
 
 static const struct of_device_id pca995x_of_match[] = {
-	{ .compatible = "nxp,pca9952",  .data = (void *)PCA995X_TYPE_NON_B },
-	{ .compatible = "nxp,pca9955b", .data = (void *)PCA995X_TYPE_B },
+	{ .compatible = "nxp,pca9952", .data = &pca9952_chipdef },
+	{ .compatible = "nxp,pca9955b", . data = &pca9955b_chipdef },
+	{ .compatible = "nxp,pca9956b", .data = &pca9956b_chipdef },
 	{},
 };
 MODULE_DEVICE_TABLE(of, pca995x_of_match);
Signature · 1 line
2.45.2
#3 lore raw

On 7/10/24 4:32 PM, Pieterjan Camerlynck via B4 Relay wrote:

[...]

drivers/leds/leds-pca995x.c struct pca995x_led { · line 73

Quoted 9 lines
drivers/leds/leds-pca995x.c +2 -2
@@ -52,9 +68,9 @@ struct pca995x_led {
  };
 
  struct pca995x_chip {
+	const struct pca995x_chipdef *chipdef;
  	struct regmap *regmap;
-	struct pca995x_led leds[PCA995X_MAX_OUTPUTS];
-	int btype;
+	struct pca995x_led leds[];

Please increase PCA995X_MAX_OUTPUTS to 24 and avoid this variable length array at the end.

drivers/leds/leds-pca995x.c static int pca995x_brightness_set(struct led_classdev *led_cdev, · line 85

Quoted 13 lines
drivers/leds/leds-pca995x.c +2 -1
  };
 
  static int pca995x_brightness_set(struct led_classdev *led_cdev,
@@ -62,10 +78,11 @@ static int pca995x_brightness_set(struct led_classdev *led_cdev,
  {
  	struct pca995x_led *led = ldev_to_led(led_cdev);
  	struct pca995x_chip *chip = led->chip;
+	const struct pca995x_chipdef *chipdef = chip->chipdef;
  	u8 ledout_addr, pwmout_addr;
  	int shift, ret;
 
-	pwmout_addr = (chip->btype ? PCA9955B_PWM0 : PCA9952_PWM0) + led->led_no;
+	pwmout_addr = (chipdef->pwm_base) + led->led_no;

Parenthesis around (chipdef->pwm_base) not needed.

Quoted 15 lines
drivers/leds/leds-pca995x.c +2 -2
  	ledout_addr = PCA995X_LEDOUT0 + (led->led_no / PCA995X_OUTPUTS_PER_REG);
  	shift = PCA995X_LDRX_BITS * (led->led_no % PCA995X_OUTPUTS_PER_REG);
 
@@ -101,24 +118,24 @@ static const struct regmap_config pca995x_regmap = {
 
  static int pca995x_probe(struct i2c_client *client)
  {
-	struct fwnode_handle *led_fwnodes[PCA995X_MAX_OUTPUTS] = { 0 };
  	struct fwnode_handle *np, *child;
  	struct device *dev = &client->dev;
+	const struct pca995x_chipdef *chipdef;
  	struct pca995x_chip *chip;
  	struct pca995x_led *led;
-	int i, btype, reg, ret;
+	int reg, ret;

drivers/leds/leds-pca995x.c static const struct regmap_config pca995x_regmap = { · line 134

Quoted 10 lines
drivers/leds/leds-pca995x.c +2 -2
 
-	btype = (unsigned long)device_get_match_data(&client->dev);
+	chipdef = device_get_match_data(&client->dev);
 
  	np = dev_fwnode(dev);
  	if (!np)
  		return -ENODEV;
 
-	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
+	chip = devm_kzalloc(dev, struct_size(chip, leds, chipdef->num_leds), GFP_KERNEL);

This won't be needed once you fix up PCA995X_MAX_OUTPUTS above.

drivers/leds/leds-pca995x.c static const struct regmap_config pca995x_regmap = { · line 141

Quoted 8 lines
drivers/leds/leds-pca995x.c +1 -1
  	if (!chip)
  		return -ENOMEM;
 
-	chip->btype = btype;
+	chip->chipdef = chipdef;
  	chip->regmap = devm_regmap_init_i2c(client, &pca995x_regmap);
  	if (IS_ERR(chip->regmap))
  		return PTR_ERR(chip->regmap);

It is starting to look pretty good, thanks !