2 * Take linux kernel driver drivers/gpio/gpio-pca953x.c for reference.
4 * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com>
6 * SPDX-License-Identifier: GPL-2.0+
12 * The driver's compatible table is borrowed from Linux Kernel,
13 * but now max supported gpio pins is 24 and only PCA953X_TYPE
14 * is supported. PCA957X_TYPE is not supported now.
15 * Also the Polarity Inversion feature is not supported now.
18 * 1. Support PCA957X_TYPE
19 * 2. Support 24 gpio pins
20 * 3. Support Polarity Inversion
31 #include <dt-bindings/gpio/gpio.h>
33 #define PCA953X_INPUT 0
34 #define PCA953X_OUTPUT 1
35 #define PCA953X_INVERT 2
36 #define PCA953X_DIRECTION 3
38 #define PCA_GPIO_MASK 0x00FF
39 #define PCA_INT 0x0100
40 #define PCA953X_TYPE 0x1000
41 #define PCA957X_TYPE 0x2000
42 #define PCA_TYPE_MASK 0xF000
43 #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
47 PCA953X_DIRECTION_OUT,
54 * struct pca953x_info - Data for pca953x
56 * @dev: udevice structure for the device
57 * @addr: i2c slave address
58 * @invert: Polarity inversion or not
59 * @gpio_count: the number of gpio pins that the device supports
60 * @chip_type: indicate the chip type,PCA953X or PCA957X
61 * @bank_count: the number of banks that the device supports
62 * @reg_output: array to hold the value of output registers
63 * @reg_direction: array to hold the value of direction registers
72 u8 reg_output[MAX_BANK];
73 u8 reg_direction[MAX_BANK];
76 static int pca953x_write_single(struct udevice *dev, int reg, u8 val,
79 struct pca953x_info *info = dev_get_platdata(dev);
80 int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
81 int off = offset / BANK_SZ;
84 ret = dm_i2c_write(dev, (reg << bank_shift) + off, &val, 1);
86 dev_err(dev, "%s error\n", __func__);
93 static int pca953x_read_single(struct udevice *dev, int reg, u8 *val,
96 struct pca953x_info *info = dev_get_platdata(dev);
97 int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
98 int off = offset / BANK_SZ;
102 ret = dm_i2c_read(dev, (reg << bank_shift) + off, &byte, 1);
104 dev_err(dev, "%s error\n", __func__);
113 static int pca953x_read_regs(struct udevice *dev, int reg, u8 *val)
115 struct pca953x_info *info = dev_get_platdata(dev);
118 if (info->gpio_count <= 8) {
119 ret = dm_i2c_read(dev, reg, val, 1);
120 } else if (info->gpio_count <= 16) {
121 ret = dm_i2c_read(dev, reg << 1, val, info->bank_count);
122 } else if (info->gpio_count == 40) {
124 ret = dm_i2c_read(dev, (reg << 3) | 0x80, val,
127 dev_err(dev, "Unsupported now\n");
134 static int pca953x_is_output(struct udevice *dev, int offset)
136 struct pca953x_info *info = dev_get_platdata(dev);
138 int bank = offset / BANK_SZ;
139 int off = offset % BANK_SZ;
141 /*0: output; 1: input */
142 return !(info->reg_direction[bank] & (1 << off));
145 static int pca953x_get_value(struct udevice *dev, uint offset)
150 int off = offset % BANK_SZ;
152 ret = pca953x_read_single(dev, PCA953X_INPUT, &val, offset);
156 return (val >> off) & 0x1;
159 static int pca953x_set_value(struct udevice *dev, uint offset, int value)
161 struct pca953x_info *info = dev_get_platdata(dev);
162 int bank = offset / BANK_SZ;
163 int off = offset % BANK_SZ;
168 val = info->reg_output[bank] | (1 << off);
170 val = info->reg_output[bank] & ~(1 << off);
172 ret = pca953x_write_single(dev, PCA953X_OUTPUT, val, offset);
176 info->reg_output[bank] = val;
181 static int pca953x_set_direction(struct udevice *dev, uint offset, int dir)
183 struct pca953x_info *info = dev_get_platdata(dev);
184 int bank = offset / BANK_SZ;
185 int off = offset % BANK_SZ;
189 if (dir == PCA953X_DIRECTION_IN)
190 val = info->reg_direction[bank] | (1 << off);
192 val = info->reg_direction[bank] & ~(1 << off);
194 ret = pca953x_write_single(dev, PCA953X_DIRECTION, val, offset);
198 info->reg_direction[bank] = val;
203 static int pca953x_direction_input(struct udevice *dev, uint offset)
205 return pca953x_set_direction(dev, offset, PCA953X_DIRECTION_IN);
208 static int pca953x_direction_output(struct udevice *dev, uint offset, int value)
210 /* Configure output value. */
211 pca953x_set_value(dev, offset, value);
213 /* Configure direction as output. */
214 pca953x_set_direction(dev, offset, PCA953X_DIRECTION_OUT);
219 static int pca953x_get_function(struct udevice *dev, uint offset)
221 if (pca953x_is_output(dev, offset))
227 static int pca953x_xlate(struct udevice *dev, struct gpio_desc *desc,
228 struct ofnode_phandle_args *args)
230 desc->offset = args->args[0];
231 desc->flags = args->args[1] & (GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0);
236 static const struct dm_gpio_ops pca953x_ops = {
237 .direction_input = pca953x_direction_input,
238 .direction_output = pca953x_direction_output,
239 .get_value = pca953x_get_value,
240 .set_value = pca953x_set_value,
241 .get_function = pca953x_get_function,
242 .xlate = pca953x_xlate,
245 static int pca953x_probe(struct udevice *dev)
247 struct pca953x_info *info = dev_get_platdata(dev);
248 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
249 char name[32], label[8], *str;
256 addr = dev_read_addr(dev);
262 driver_data = dev_get_driver_data(dev);
264 info->gpio_count = driver_data & PCA_GPIO_MASK;
265 if (info->gpio_count > MAX_BANK * BANK_SZ) {
266 dev_err(dev, "Max support %d pins now\n", MAX_BANK * BANK_SZ);
270 info->chip_type = PCA_CHIP_TYPE(driver_data);
271 if (info->chip_type != PCA953X_TYPE) {
272 dev_err(dev, "Only support PCA953X chip type now.\n");
276 info->bank_count = DIV_ROUND_UP(info->gpio_count, BANK_SZ);
278 ret = pca953x_read_regs(dev, PCA953X_OUTPUT, info->reg_output);
280 dev_err(dev, "Error reading output register\n");
284 ret = pca953x_read_regs(dev, PCA953X_DIRECTION, info->reg_direction);
286 dev_err(dev, "Error reading direction register\n");
290 tmp = dev_read_prop(dev, "label", &size);
293 memcpy(label, tmp, sizeof(label) - 1);
294 label[sizeof(label) - 1] = '\0';
295 snprintf(name, sizeof(name), "%s@%x_", label, info->addr);
297 snprintf(name, sizeof(name), "gpio@%x_", info->addr);
303 uc_priv->bank_name = str;
304 uc_priv->gpio_count = info->gpio_count;
306 dev_dbg(dev, "%s is ready\n", str);
311 #define OF_953X(__nrgpio, __int) (ulong)(__nrgpio | PCA953X_TYPE | __int)
312 #define OF_957X(__nrgpio, __int) (ulong)(__nrgpio | PCA957X_TYPE | __int)
314 static const struct udevice_id pca953x_ids[] = {
315 { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
316 { .compatible = "nxp,pca9534", .data = OF_953X(8, PCA_INT), },
317 { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
318 { .compatible = "nxp,pca9536", .data = OF_953X(4, 0), },
319 { .compatible = "nxp,pca9537", .data = OF_953X(4, PCA_INT), },
320 { .compatible = "nxp,pca9538", .data = OF_953X(8, PCA_INT), },
321 { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
322 { .compatible = "nxp,pca9554", .data = OF_953X(8, PCA_INT), },
323 { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
324 { .compatible = "nxp,pca9556", .data = OF_953X(8, 0), },
325 { .compatible = "nxp,pca9557", .data = OF_953X(8, 0), },
326 { .compatible = "nxp,pca9574", .data = OF_957X(8, PCA_INT), },
327 { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
328 { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
330 { .compatible = "maxim,max7310", .data = OF_953X(8, 0), },
331 { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
332 { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
333 { .compatible = "maxim,max7315", .data = OF_953X(8, PCA_INT), },
335 { .compatible = "ti,pca6107", .data = OF_953X(8, PCA_INT), },
336 { .compatible = "ti,tca6408", .data = OF_953X(8, PCA_INT), },
337 { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
338 { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
340 { .compatible = "onsemi,pca9654", .data = OF_953X(8, PCA_INT), },
342 { .compatible = "exar,xra1202", .data = OF_953X(8, 0), },
346 U_BOOT_DRIVER(pca953x) = {
350 .probe = pca953x_probe,
351 .platdata_auto_alloc_size = sizeof(struct pca953x_info),
352 .of_match = pca953x_ids,