Skip to contentSkip to conversation

gpio: pca953x: Clear the polarity invert register at init

PATCH In review Anatolij Gustschin 2 participants 2 messages Oct 18, 2018 – Nov 17, 2018 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpio: pca953x: Clear the polarity invert register at init

From: Ye Li <[email protected]>

The pca953x_gpio driver uses default value of polarity inversion register. For some devices like PCA9557 and MAX7310, their polarity inversion register default value is 0xf0. So for high 4 ports, when reading their values, the values are inverted as the actual level.

This patch clears the polarity inversion register to 0 at init, so that the port read and write values are aligned.

---
 drivers/gpio/pca953x_gpio.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/gpio/pca953x_gpio.c +28 -0
index 0bb484498a..341527acc5 100644
@@ -130,6 +130,25 @@ static int pca953x_read_regs(struct udevice *dev, int reg, u8 *val)
 	return ret;
 }
 
+static int pca953x_write_regs(struct udevice *dev, int reg, u8 *val)
+{
+	struct pca953x_info *info = dev_get_platdata(dev);
+	int ret = 0;
+
+	if (info->gpio_count <= 8) {
+		ret = dm_i2c_write(dev, reg, val, 1);
+	} else if (info->gpio_count <= 16) {
+		ret = dm_i2c_write(dev, reg << 1, val, info->bank_count);
+	} else if (info->gpio_count == 40) {
+		/* Auto increment */
+		ret = dm_i2c_write(dev, (reg << 3) | 0x80, val, info->bank_count);
+	} else {
+		return -EINVAL;
+	}
+
+	return ret;
+}
+
 static int pca953x_is_output(struct udevice *dev, int offset)
 {
 	struct pca953x_info *info = dev_get_platdata(dev);
@@ -251,6 +270,7 @@ static int pca953x_probe(struct udevice *dev)
 	int ret;
 	int size;
 	const u8 *tmp;
+	u8 val[MAX_BANK];
 
 	addr = dev_read_addr(dev);
 	if (addr == 0)
@@ -296,6 +316,14 @@ static int pca953x_probe(struct udevice *dev)
 		snprintf(name, sizeof(name), "gpio@%x_", info->addr);
 	}
 
+	/* Clear the polarity registers to no invert */
+	memset(val, 0, MAX_BANK);
+	ret = pca953x_write_regs(dev, PCA953X_INVERT, val);
+	if (ret < 0) {
+		dev_err(dev, "Error writing invert register\n");
+		return ret;
+	}
+
 	str = strdup(name);
 	if (!str)
 		return -ENOMEM;
Signature · 1 line
2.17.1
#1 lore raw
Quoted 7 lines · Anatolij Gustschin

From: Ye Li <[email protected]>

The pca953x_gpio driver uses default value of polarity inversion register. For some devices like PCA9557 and MAX7310, their polarity inversion register default value is 0xf0. So for high 4 ports, when reading their values, the values are inverted as the actual level.

Quoted 7 lines

This patch clears the polarity inversion register to 0 at init, so that the port read and write values are aligned.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20181117/94470b1e/attachment.sig>