103773439SPeng Fan /*
203773439SPeng Fan * Take linux kernel driver drivers/gpio/gpio-pca953x.c for reference.
303773439SPeng Fan *
403773439SPeng Fan * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com>
503773439SPeng Fan *
603773439SPeng Fan * SPDX-License-Identifier: GPL-2.0+
703773439SPeng Fan *
803773439SPeng Fan */
903773439SPeng Fan
1003773439SPeng Fan /*
1103773439SPeng Fan * Note:
1203773439SPeng Fan * The driver's compatible table is borrowed from Linux Kernel,
1303773439SPeng Fan * but now max supported gpio pins is 24 and only PCA953X_TYPE
1403773439SPeng Fan * is supported. PCA957X_TYPE is not supported now.
1503773439SPeng Fan * Also the Polarity Inversion feature is not supported now.
1603773439SPeng Fan *
1703773439SPeng Fan * TODO:
1803773439SPeng Fan * 1. Support PCA957X_TYPE
1971db3270Smario.six@gdsys.cc * 2. Support 24 gpio pins
2071db3270Smario.six@gdsys.cc * 3. Support Polarity Inversion
2103773439SPeng Fan */
2203773439SPeng Fan
2303773439SPeng Fan #include <common.h>
2403773439SPeng Fan #include <errno.h>
2503773439SPeng Fan #include <dm.h>
2603773439SPeng Fan #include <fdtdec.h>
2703773439SPeng Fan #include <i2c.h>
2803773439SPeng Fan #include <malloc.h>
2903773439SPeng Fan #include <asm/gpio.h>
3003773439SPeng Fan #include <asm/io.h>
3103773439SPeng Fan #include <dt-bindings/gpio/gpio.h>
3203773439SPeng Fan
3303773439SPeng Fan #define PCA953X_INPUT 0
3403773439SPeng Fan #define PCA953X_OUTPUT 1
3503773439SPeng Fan #define PCA953X_INVERT 2
3603773439SPeng Fan #define PCA953X_DIRECTION 3
3703773439SPeng Fan
3803773439SPeng Fan #define PCA_GPIO_MASK 0x00FF
3903773439SPeng Fan #define PCA_INT 0x0100
4003773439SPeng Fan #define PCA953X_TYPE 0x1000
4103773439SPeng Fan #define PCA957X_TYPE 0x2000
4203773439SPeng Fan #define PCA_TYPE_MASK 0xF000
4303773439SPeng Fan #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
4403773439SPeng Fan
4503773439SPeng Fan enum {
4603773439SPeng Fan PCA953X_DIRECTION_IN,
4703773439SPeng Fan PCA953X_DIRECTION_OUT,
4803773439SPeng Fan };
4903773439SPeng Fan
5071db3270Smario.six@gdsys.cc #define MAX_BANK 5
5103773439SPeng Fan #define BANK_SZ 8
5203773439SPeng Fan
5303773439SPeng Fan DECLARE_GLOBAL_DATA_PTR;
5403773439SPeng Fan
5503773439SPeng Fan /*
5603773439SPeng Fan * struct pca953x_info - Data for pca953x
5703773439SPeng Fan *
5803773439SPeng Fan * @dev: udevice structure for the device
5903773439SPeng Fan * @addr: i2c slave address
6003773439SPeng Fan * @invert: Polarity inversion or not
6103773439SPeng Fan * @gpio_count: the number of gpio pins that the device supports
6203773439SPeng Fan * @chip_type: indicate the chip type,PCA953X or PCA957X
6303773439SPeng Fan * @bank_count: the number of banks that the device supports
6403773439SPeng Fan * @reg_output: array to hold the value of output registers
6503773439SPeng Fan * @reg_direction: array to hold the value of direction registers
6603773439SPeng Fan */
6703773439SPeng Fan struct pca953x_info {
6803773439SPeng Fan struct udevice *dev;
6903773439SPeng Fan int addr;
7003773439SPeng Fan int invert;
7103773439SPeng Fan int gpio_count;
7203773439SPeng Fan int chip_type;
7303773439SPeng Fan int bank_count;
7403773439SPeng Fan u8 reg_output[MAX_BANK];
7503773439SPeng Fan u8 reg_direction[MAX_BANK];
7603773439SPeng Fan };
7703773439SPeng Fan
pca953x_write_single(struct udevice * dev,int reg,u8 val,int offset)7803773439SPeng Fan static int pca953x_write_single(struct udevice *dev, int reg, u8 val,
7903773439SPeng Fan int offset)
8003773439SPeng Fan {
8103773439SPeng Fan struct pca953x_info *info = dev_get_platdata(dev);
8203773439SPeng Fan int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
8303773439SPeng Fan int off = offset / BANK_SZ;
8403773439SPeng Fan int ret = 0;
8503773439SPeng Fan
8603773439SPeng Fan ret = dm_i2c_write(dev, (reg << bank_shift) + off, &val, 1);
8703773439SPeng Fan if (ret) {
8803773439SPeng Fan dev_err(dev, "%s error\n", __func__);
8903773439SPeng Fan return ret;
9003773439SPeng Fan }
9103773439SPeng Fan
9203773439SPeng Fan return 0;
9303773439SPeng Fan }
9403773439SPeng Fan
pca953x_read_single(struct udevice * dev,int reg,u8 * val,int offset)9503773439SPeng Fan static int pca953x_read_single(struct udevice *dev, int reg, u8 *val,
9603773439SPeng Fan int offset)
9703773439SPeng Fan {
9803773439SPeng Fan struct pca953x_info *info = dev_get_platdata(dev);
9903773439SPeng Fan int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
10003773439SPeng Fan int off = offset / BANK_SZ;
10103773439SPeng Fan int ret;
10203773439SPeng Fan u8 byte;
10303773439SPeng Fan
10403773439SPeng Fan ret = dm_i2c_read(dev, (reg << bank_shift) + off, &byte, 1);
10503773439SPeng Fan if (ret) {
10603773439SPeng Fan dev_err(dev, "%s error\n", __func__);
10703773439SPeng Fan return ret;
10803773439SPeng Fan }
10903773439SPeng Fan
11003773439SPeng Fan *val = byte;
11103773439SPeng Fan
11203773439SPeng Fan return 0;
11303773439SPeng Fan }
11403773439SPeng Fan
pca953x_read_regs(struct udevice * dev,int reg,u8 * val)11503773439SPeng Fan static int pca953x_read_regs(struct udevice *dev, int reg, u8 *val)
11603773439SPeng Fan {
11703773439SPeng Fan struct pca953x_info *info = dev_get_platdata(dev);
11803773439SPeng Fan int ret = 0;
11903773439SPeng Fan
12003773439SPeng Fan if (info->gpio_count <= 8) {
12103773439SPeng Fan ret = dm_i2c_read(dev, reg, val, 1);
12203773439SPeng Fan } else if (info->gpio_count <= 16) {
12303773439SPeng Fan ret = dm_i2c_read(dev, reg << 1, val, info->bank_count);
12471db3270Smario.six@gdsys.cc } else if (info->gpio_count == 40) {
12571db3270Smario.six@gdsys.cc /* Auto increment */
12671db3270Smario.six@gdsys.cc ret = dm_i2c_read(dev, (reg << 3) | 0x80, val, info->bank_count);
12703773439SPeng Fan } else {
12803773439SPeng Fan dev_err(dev, "Unsupported now\n");
12903773439SPeng Fan return -EINVAL;
13003773439SPeng Fan }
13103773439SPeng Fan
13203773439SPeng Fan return ret;
13303773439SPeng Fan }
13403773439SPeng Fan
pca953x_is_output(struct udevice * dev,int offset)13503773439SPeng Fan static int pca953x_is_output(struct udevice *dev, int offset)
13603773439SPeng Fan {
13703773439SPeng Fan struct pca953x_info *info = dev_get_platdata(dev);
13803773439SPeng Fan
13903773439SPeng Fan int bank = offset / BANK_SZ;
14003773439SPeng Fan int off = offset % BANK_SZ;
14103773439SPeng Fan
14203773439SPeng Fan /*0: output; 1: input */
14303773439SPeng Fan return !(info->reg_direction[bank] & (1 << off));
14403773439SPeng Fan }
14503773439SPeng Fan
pca953x_get_value(struct udevice * dev,unsigned offset)14603773439SPeng Fan static int pca953x_get_value(struct udevice *dev, unsigned offset)
14703773439SPeng Fan {
14803773439SPeng Fan int ret;
14903773439SPeng Fan u8 val = 0;
15003773439SPeng Fan
151fc76b698Smario.six@gdsys.cc int off = offset % BANK_SZ;
152fc76b698Smario.six@gdsys.cc
15303773439SPeng Fan ret = pca953x_read_single(dev, PCA953X_INPUT, &val, offset);
15403773439SPeng Fan if (ret)
15503773439SPeng Fan return ret;
15603773439SPeng Fan
157fc76b698Smario.six@gdsys.cc return (val >> off) & 0x1;
15803773439SPeng Fan }
15903773439SPeng Fan
pca953x_set_value(struct udevice * dev,unsigned offset,int value)16003773439SPeng Fan static int pca953x_set_value(struct udevice *dev, unsigned offset,
16103773439SPeng Fan int value)
16203773439SPeng Fan {
16303773439SPeng Fan struct pca953x_info *info = dev_get_platdata(dev);
16403773439SPeng Fan int bank = offset / BANK_SZ;
16503773439SPeng Fan int off = offset % BANK_SZ;
16603773439SPeng Fan u8 val;
16703773439SPeng Fan int ret;
16803773439SPeng Fan
16903773439SPeng Fan if (value)
17003773439SPeng Fan val = info->reg_output[bank] | (1 << off);
17103773439SPeng Fan else
17203773439SPeng Fan val = info->reg_output[bank] & ~(1 << off);
17303773439SPeng Fan
17403773439SPeng Fan ret = pca953x_write_single(dev, PCA953X_OUTPUT, val, offset);
17503773439SPeng Fan if (ret)
17603773439SPeng Fan return ret;
17703773439SPeng Fan
17803773439SPeng Fan info->reg_output[bank] = val;
17903773439SPeng Fan
18003773439SPeng Fan return 0;
18103773439SPeng Fan }
18203773439SPeng Fan
pca953x_set_direction(struct udevice * dev,unsigned offset,int dir)18303773439SPeng Fan static int pca953x_set_direction(struct udevice *dev, unsigned offset, int dir)
18403773439SPeng Fan {
18503773439SPeng Fan struct pca953x_info *info = dev_get_platdata(dev);
18603773439SPeng Fan int bank = offset / BANK_SZ;
18703773439SPeng Fan int off = offset % BANK_SZ;
18803773439SPeng Fan u8 val;
18903773439SPeng Fan int ret;
19003773439SPeng Fan
19103773439SPeng Fan if (dir == PCA953X_DIRECTION_IN)
19203773439SPeng Fan val = info->reg_direction[bank] | (1 << off);
19303773439SPeng Fan else
19403773439SPeng Fan val = info->reg_direction[bank] & ~(1 << off);
19503773439SPeng Fan
19603773439SPeng Fan ret = pca953x_write_single(dev, PCA953X_DIRECTION, val, offset);
19703773439SPeng Fan if (ret)
19803773439SPeng Fan return ret;
19903773439SPeng Fan
20003773439SPeng Fan info->reg_direction[bank] = val;
20103773439SPeng Fan
20203773439SPeng Fan return 0;
20303773439SPeng Fan }
20403773439SPeng Fan
pca953x_direction_input(struct udevice * dev,unsigned offset)20503773439SPeng Fan static int pca953x_direction_input(struct udevice *dev, unsigned offset)
20603773439SPeng Fan {
20703773439SPeng Fan return pca953x_set_direction(dev, offset, PCA953X_DIRECTION_IN);
20803773439SPeng Fan }
20903773439SPeng Fan
pca953x_direction_output(struct udevice * dev,unsigned offset,int value)21003773439SPeng Fan static int pca953x_direction_output(struct udevice *dev, unsigned offset,
21103773439SPeng Fan int value)
21203773439SPeng Fan {
21303773439SPeng Fan /* Configure output value. */
21403773439SPeng Fan pca953x_set_value(dev, offset, value);
21503773439SPeng Fan
21603773439SPeng Fan /* Configure direction as output. */
21703773439SPeng Fan pca953x_set_direction(dev, offset, PCA953X_DIRECTION_OUT);
21803773439SPeng Fan
21903773439SPeng Fan return 0;
22003773439SPeng Fan }
22103773439SPeng Fan
pca953x_get_function(struct udevice * dev,unsigned offset)22203773439SPeng Fan static int pca953x_get_function(struct udevice *dev, unsigned offset)
22303773439SPeng Fan {
22403773439SPeng Fan if (pca953x_is_output(dev, offset))
22503773439SPeng Fan return GPIOF_OUTPUT;
22603773439SPeng Fan else
22703773439SPeng Fan return GPIOF_INPUT;
22803773439SPeng Fan }
22903773439SPeng Fan
pca953x_xlate(struct udevice * dev,struct gpio_desc * desc,struct ofnode_phandle_args * args)23003773439SPeng Fan static int pca953x_xlate(struct udevice *dev, struct gpio_desc *desc,
231*3a57123eSSimon Glass struct ofnode_phandle_args *args)
23203773439SPeng Fan {
23303773439SPeng Fan desc->offset = args->args[0];
23403773439SPeng Fan desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
23503773439SPeng Fan
23603773439SPeng Fan return 0;
23703773439SPeng Fan }
23803773439SPeng Fan
23903773439SPeng Fan static const struct dm_gpio_ops pca953x_ops = {
24003773439SPeng Fan .direction_input = pca953x_direction_input,
24103773439SPeng Fan .direction_output = pca953x_direction_output,
24203773439SPeng Fan .get_value = pca953x_get_value,
24303773439SPeng Fan .set_value = pca953x_set_value,
24403773439SPeng Fan .get_function = pca953x_get_function,
24503773439SPeng Fan .xlate = pca953x_xlate,
24603773439SPeng Fan };
24703773439SPeng Fan
pca953x_probe(struct udevice * dev)24803773439SPeng Fan static int pca953x_probe(struct udevice *dev)
24903773439SPeng Fan {
25003773439SPeng Fan struct pca953x_info *info = dev_get_platdata(dev);
25103773439SPeng Fan struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
25203773439SPeng Fan struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
25303773439SPeng Fan char name[32], *str;
25403773439SPeng Fan int addr;
25503773439SPeng Fan ulong driver_data;
25603773439SPeng Fan int ret;
25703773439SPeng Fan
25803773439SPeng Fan if (!info) {
25903773439SPeng Fan dev_err(dev, "platdata not ready\n");
26003773439SPeng Fan return -ENOMEM;
26103773439SPeng Fan }
26203773439SPeng Fan
26303773439SPeng Fan if (!chip) {
26403773439SPeng Fan dev_err(dev, "i2c not ready\n");
26503773439SPeng Fan return -ENODEV;
26603773439SPeng Fan }
26703773439SPeng Fan
268e160f7d4SSimon Glass addr = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", 0);
26903773439SPeng Fan if (addr == 0)
27003773439SPeng Fan return -ENODEV;
27103773439SPeng Fan
27203773439SPeng Fan info->addr = addr;
27303773439SPeng Fan
27403773439SPeng Fan driver_data = dev_get_driver_data(dev);
27503773439SPeng Fan
27603773439SPeng Fan info->gpio_count = driver_data & PCA_GPIO_MASK;
27703773439SPeng Fan if (info->gpio_count > MAX_BANK * BANK_SZ) {
27803773439SPeng Fan dev_err(dev, "Max support %d pins now\n", MAX_BANK * BANK_SZ);
27903773439SPeng Fan return -EINVAL;
28003773439SPeng Fan }
28103773439SPeng Fan
28203773439SPeng Fan info->chip_type = PCA_CHIP_TYPE(driver_data);
28303773439SPeng Fan if (info->chip_type != PCA953X_TYPE) {
28403773439SPeng Fan dev_err(dev, "Only support PCA953X chip type now.\n");
28503773439SPeng Fan return -EINVAL;
28603773439SPeng Fan }
28703773439SPeng Fan
28803773439SPeng Fan info->bank_count = DIV_ROUND_UP(info->gpio_count, BANK_SZ);
28903773439SPeng Fan
29003773439SPeng Fan ret = pca953x_read_regs(dev, PCA953X_OUTPUT, info->reg_output);
29103773439SPeng Fan if (ret) {
29203773439SPeng Fan dev_err(dev, "Error reading output register\n");
29303773439SPeng Fan return ret;
29403773439SPeng Fan }
29503773439SPeng Fan
29603773439SPeng Fan ret = pca953x_read_regs(dev, PCA953X_DIRECTION, info->reg_direction);
29703773439SPeng Fan if (ret) {
29803773439SPeng Fan dev_err(dev, "Error reading direction register\n");
29903773439SPeng Fan return ret;
30003773439SPeng Fan }
30103773439SPeng Fan
30203773439SPeng Fan snprintf(name, sizeof(name), "gpio@%x_", info->addr);
30303773439SPeng Fan str = strdup(name);
30403773439SPeng Fan if (!str)
30503773439SPeng Fan return -ENOMEM;
30603773439SPeng Fan uc_priv->bank_name = str;
30703773439SPeng Fan uc_priv->gpio_count = info->gpio_count;
30803773439SPeng Fan
30903773439SPeng Fan dev_dbg(dev, "%s is ready\n", str);
31003773439SPeng Fan
31103773439SPeng Fan return 0;
31203773439SPeng Fan }
31303773439SPeng Fan
31403773439SPeng Fan #define OF_953X(__nrgpio, __int) (ulong)(__nrgpio | PCA953X_TYPE | __int)
31503773439SPeng Fan #define OF_957X(__nrgpio, __int) (ulong)(__nrgpio | PCA957X_TYPE | __int)
31603773439SPeng Fan
31703773439SPeng Fan static const struct udevice_id pca953x_ids[] = {
31803773439SPeng Fan { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
31903773439SPeng Fan { .compatible = "nxp,pca9534", .data = OF_953X(8, PCA_INT), },
32003773439SPeng Fan { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
32103773439SPeng Fan { .compatible = "nxp,pca9536", .data = OF_953X(4, 0), },
32203773439SPeng Fan { .compatible = "nxp,pca9537", .data = OF_953X(4, PCA_INT), },
32303773439SPeng Fan { .compatible = "nxp,pca9538", .data = OF_953X(8, PCA_INT), },
32403773439SPeng Fan { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
32503773439SPeng Fan { .compatible = "nxp,pca9554", .data = OF_953X(8, PCA_INT), },
32603773439SPeng Fan { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
32703773439SPeng Fan { .compatible = "nxp,pca9556", .data = OF_953X(8, 0), },
32803773439SPeng Fan { .compatible = "nxp,pca9557", .data = OF_953X(8, 0), },
32903773439SPeng Fan { .compatible = "nxp,pca9574", .data = OF_957X(8, PCA_INT), },
33003773439SPeng Fan { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
33103773439SPeng Fan { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
33203773439SPeng Fan
33303773439SPeng Fan { .compatible = "maxim,max7310", .data = OF_953X(8, 0), },
33403773439SPeng Fan { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
33503773439SPeng Fan { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
33603773439SPeng Fan { .compatible = "maxim,max7315", .data = OF_953X(8, PCA_INT), },
33703773439SPeng Fan
33803773439SPeng Fan { .compatible = "ti,pca6107", .data = OF_953X(8, PCA_INT), },
33903773439SPeng Fan { .compatible = "ti,tca6408", .data = OF_953X(8, PCA_INT), },
34003773439SPeng Fan { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
34103773439SPeng Fan { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
34203773439SPeng Fan
34303773439SPeng Fan { .compatible = "onsemi,pca9654", .data = OF_953X(8, PCA_INT), },
34403773439SPeng Fan
34503773439SPeng Fan { .compatible = "exar,xra1202", .data = OF_953X(8, 0), },
34603773439SPeng Fan { }
34703773439SPeng Fan };
34803773439SPeng Fan
34903773439SPeng Fan U_BOOT_DRIVER(pca953x) = {
35003773439SPeng Fan .name = "pca953x",
35103773439SPeng Fan .id = UCLASS_GPIO,
35203773439SPeng Fan .ops = &pca953x_ops,
35303773439SPeng Fan .probe = pca953x_probe,
35403773439SPeng Fan .platdata_auto_alloc_size = sizeof(struct pca953x_info),
35503773439SPeng Fan .of_match = pca953x_ids,
35603773439SPeng Fan };
357