+// SPDX-License-Identifier: GPL-2.0+
/*
* Take linux kernel driver drivers/gpio/gpio-pca953x.c for reference.
*
* Copyright (C) 2016 Peng Fan <van.freenix@gmail.com>
*
- * SPDX-License-Identifier: GPL-2.0+
- *
*/
/*
*
* TODO:
* 1. Support PCA957X_TYPE
- * 2. Support max 40 gpio pins
- * 3. Support Plolarity Inversion
+ * 2. Support Polarity Inversion
*/
#include <common.h>
#include <malloc.h>
#include <asm/gpio.h>
#include <asm/io.h>
+#include <dm/device_compat.h>
#include <dt-bindings/gpio/gpio.h>
#define PCA953X_INPUT 0
PCA953X_DIRECTION_OUT,
};
-#define MAX_BANK 3
+#define MAX_BANK 5
#define BANK_SZ 8
-DECLARE_GLOBAL_DATA_PTR;
-
/*
* struct pca953x_info - Data for pca953x
*
ret = dm_i2c_read(dev, reg, val, 1);
} else if (info->gpio_count <= 16) {
ret = dm_i2c_read(dev, reg << 1, val, info->bank_count);
+ } else if (info->gpio_count <= 24) {
+ /* Auto increment */
+ ret = dm_i2c_read(dev, (reg << 2) | 0x80, val,
+ info->bank_count);
+ } else if (info->gpio_count == 40) {
+ /* Auto increment */
+ ret = dm_i2c_read(dev, (reg << 3) | 0x80, val,
+ info->bank_count);
} else {
dev_err(dev, "Unsupported now\n");
return -EINVAL;
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 <= 24) {
+ /* Auto increment */
+ ret = dm_i2c_write(dev, (reg << 2) | 0x80, 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);
return !(info->reg_direction[bank] & (1 << off));
}
-static int pca953x_get_value(struct udevice *dev, unsigned offset)
+static int pca953x_get_value(struct udevice *dev, uint offset)
{
int ret;
u8 val = 0;
+ int off = offset % BANK_SZ;
+
ret = pca953x_read_single(dev, PCA953X_INPUT, &val, offset);
if (ret)
return ret;
- return (val >> offset) & 0x1;
+ return (val >> off) & 0x1;
}
-static int pca953x_set_value(struct udevice *dev, unsigned offset,
- int value)
+static int pca953x_set_value(struct udevice *dev, uint offset, int value)
{
struct pca953x_info *info = dev_get_platdata(dev);
int bank = offset / BANK_SZ;
return 0;
}
-static int pca953x_set_direction(struct udevice *dev, unsigned offset, int dir)
+static int pca953x_set_direction(struct udevice *dev, uint offset, int dir)
{
struct pca953x_info *info = dev_get_platdata(dev);
int bank = offset / BANK_SZ;
return 0;
}
-static int pca953x_direction_input(struct udevice *dev, unsigned offset)
+static int pca953x_direction_input(struct udevice *dev, uint offset)
{
return pca953x_set_direction(dev, offset, PCA953X_DIRECTION_IN);
}
-static int pca953x_direction_output(struct udevice *dev, unsigned offset,
- int value)
+static int pca953x_direction_output(struct udevice *dev, uint offset, int value)
{
/* Configure output value. */
pca953x_set_value(dev, offset, value);
return 0;
}
-static int pca953x_get_function(struct udevice *dev, unsigned offset)
+static int pca953x_get_function(struct udevice *dev, uint offset)
{
if (pca953x_is_output(dev, offset))
return GPIOF_OUTPUT;
}
static int pca953x_xlate(struct udevice *dev, struct gpio_desc *desc,
- struct fdtdec_phandle_args *args)
+ struct ofnode_phandle_args *args)
{
desc->offset = args->args[0];
desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
{
struct pca953x_info *info = dev_get_platdata(dev);
struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
- struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
- char name[32], *str;
+ char name[32], label[8], *str;
int addr;
ulong driver_data;
int ret;
+ int size;
+ const u8 *tmp;
+ u8 val[MAX_BANK];
- if (!info) {
- dev_err(dev, "platdata not ready\n");
- return -ENOMEM;
- }
-
- if (!chip) {
- dev_err(dev, "i2c not ready\n");
- return -ENODEV;
- }
-
- addr = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", 0);
+ addr = dev_read_addr(dev);
if (addr == 0)
return -ENODEV;
return ret;
}
- snprintf(name, sizeof(name), "gpio@%x_", info->addr);
+ tmp = dev_read_prop(dev, "label", &size);
+
+ if (tmp) {
+ memcpy(label, tmp, sizeof(label) - 1);
+ label[sizeof(label) - 1] = '\0';
+ snprintf(name, sizeof(name), "%s@%x_", label, info->addr);
+ } else {
+ 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;
{ .compatible = "ti,tca6408", .data = OF_953X(8, PCA_INT), },
{ .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
{ .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
+ { .compatible = "ti,tca9539", .data = OF_953X(16, PCA_INT), },
{ .compatible = "onsemi,pca9654", .data = OF_953X(8, PCA_INT), },