1 // SPDX-License-Identifier: GPL-2.0+
3 * Take linux kernel driver drivers/gpio/gpio-pca953x.c for reference.
5 * Copyright (C) 2016 Peng Fan <van.freenix@gmail.com>
11 * The driver's compatible table is borrowed from Linux Kernel,
12 * but now max supported gpio pins is 24 and only PCA953X_TYPE
13 * is supported. PCA957X_TYPE is not supported now.
14 * Also the Polarity Inversion feature is not supported now.
17 * 1. Support PCA957X_TYPE
18 * 2. Support Polarity Inversion
29 #include <dm/device_compat.h>
30 #include <dt-bindings/gpio/gpio.h>
31 #include <linux/bitops.h>
33 #define PCA953X_INPUT 0
34 #define PCA953X_OUTPUT 1
35 #define PCA953X_INVERT 2
36 #define PCA953X_DIRECTION 3
38 #define PCA957X_INPUT 0
39 #define PCA957X_OUTPUT 5
40 #define PCA957X_INVERT 1
41 #define PCA957X_DIRECTION 4
44 #define PCA_GPIO_MASK 0x00FF
45 #define PCA_INT 0x0100
46 #define PCA_PCAL BIT(9)
47 #define PCA_LATCH_INT (PCA_PCAL | PCA_INT)
48 #define PCA953X_TYPE 0x1000
49 #define PCA957X_TYPE 0x2000
50 #define PCA_TYPE_MASK 0xF000
51 #define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
55 PCA953X_DIRECTION_OUT,
68 static const struct pca95xx_reg pca953x_regs = {
69 .direction = PCA953X_DIRECTION,
70 .output = PCA953X_OUTPUT,
71 .input = PCA953X_INPUT,
72 .invert = PCA953X_INVERT,
75 static const struct pca95xx_reg pca957x_regs = {
76 .direction = PCA957X_DIRECTION,
77 .output = PCA957X_OUTPUT,
78 .input = PCA957X_INPUT,
79 .invert = PCA957X_INVERT,
83 * struct pca953x_info - Data for pca953x/pca957x
85 * @dev: udevice structure for the device
86 * @addr: i2c slave address
87 * @invert: Polarity inversion or not
88 * @gpio_count: the number of gpio pins that the device supports
89 * @chip_type: indicate the chip type,PCA953X or PCA957X
90 * @bank_count: the number of banks that the device supports
91 * @reg_output: array to hold the value of output registers
92 * @reg_direction: array to hold the value of direction registers
93 * @regs: struct to hold the registers addresses
102 u8 reg_output[MAX_BANK];
103 u8 reg_direction[MAX_BANK];
104 const struct pca95xx_reg *regs;
107 static int pca953x_write_single(struct udevice *dev, int reg, u8 val,
110 struct pca953x_info *info = dev_get_plat(dev);
111 int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
112 int off = offset / BANK_SZ;
115 ret = dm_i2c_write(dev, (reg << bank_shift) + off, &val, 1);
117 dev_err(dev, "%s error\n", __func__);
124 static int pca953x_read_single(struct udevice *dev, int reg, u8 *val,
127 struct pca953x_info *info = dev_get_plat(dev);
128 int bank_shift = fls((info->gpio_count - 1) / BANK_SZ);
129 int off = offset / BANK_SZ;
133 ret = dm_i2c_read(dev, (reg << bank_shift) + off, &byte, 1);
135 dev_err(dev, "%s error\n", __func__);
144 static int pca953x_read_regs(struct udevice *dev, int reg, u8 *val)
146 struct pca953x_info *info = dev_get_plat(dev);
149 if (info->gpio_count <= 8) {
150 ret = dm_i2c_read(dev, reg, val, 1);
151 } else if (info->gpio_count <= 16) {
152 ret = dm_i2c_read(dev, reg << 1, val, info->bank_count);
153 } else if (info->gpio_count <= 24) {
155 ret = dm_i2c_read(dev, (reg << 2) | 0x80, val,
157 } else if (info->gpio_count == 40) {
159 ret = dm_i2c_read(dev, (reg << 3) | 0x80, val,
162 dev_err(dev, "Unsupported now\n");
169 static int pca953x_write_regs(struct udevice *dev, int reg, u8 *val)
171 struct pca953x_info *info = dev_get_plat(dev);
174 if (info->gpio_count <= 8) {
175 ret = dm_i2c_write(dev, reg, val, 1);
176 } else if (info->gpio_count <= 16) {
177 ret = dm_i2c_write(dev, reg << 1, val, info->bank_count);
178 } else if (info->gpio_count <= 24) {
180 ret = dm_i2c_write(dev, (reg << 2) | 0x80, val,
182 } else if (info->gpio_count == 40) {
184 ret = dm_i2c_write(dev, (reg << 3) | 0x80, val, info->bank_count);
192 static int pca953x_is_output(struct udevice *dev, int offset)
194 struct pca953x_info *info = dev_get_plat(dev);
196 int bank = offset / BANK_SZ;
197 int off = offset % BANK_SZ;
199 /*0: output; 1: input */
200 return !(info->reg_direction[bank] & (1 << off));
203 static int pca953x_get_value(struct udevice *dev, uint offset)
205 struct pca953x_info *info = dev_get_plat(dev);
209 int off = offset % BANK_SZ;
211 ret = pca953x_read_single(dev, info->regs->input, &val, offset);
215 return (val >> off) & 0x1;
218 static int pca953x_set_value(struct udevice *dev, uint offset, int value)
220 struct pca953x_info *info = dev_get_plat(dev);
221 int bank = offset / BANK_SZ;
222 int off = offset % BANK_SZ;
227 val = info->reg_output[bank] | (1 << off);
229 val = info->reg_output[bank] & ~(1 << off);
231 ret = pca953x_write_single(dev, info->regs->output, val, offset);
235 info->reg_output[bank] = val;
240 static int pca953x_set_direction(struct udevice *dev, uint offset, int dir)
242 struct pca953x_info *info = dev_get_plat(dev);
243 int bank = offset / BANK_SZ;
244 int off = offset % BANK_SZ;
248 if (dir == PCA953X_DIRECTION_IN)
249 val = info->reg_direction[bank] | (1 << off);
251 val = info->reg_direction[bank] & ~(1 << off);
253 ret = pca953x_write_single(dev, info->regs->direction, val, offset);
257 info->reg_direction[bank] = val;
262 static int pca953x_direction_input(struct udevice *dev, uint offset)
264 return pca953x_set_direction(dev, offset, PCA953X_DIRECTION_IN);
267 static int pca953x_direction_output(struct udevice *dev, uint offset, int value)
269 /* Configure output value. */
270 pca953x_set_value(dev, offset, value);
272 /* Configure direction as output. */
273 pca953x_set_direction(dev, offset, PCA953X_DIRECTION_OUT);
278 static int pca953x_get_function(struct udevice *dev, uint offset)
280 if (pca953x_is_output(dev, offset))
286 static int pca953x_xlate(struct udevice *dev, struct gpio_desc *desc,
287 struct ofnode_phandle_args *args)
289 desc->offset = args->args[0];
290 desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0;
295 static const struct dm_gpio_ops pca953x_ops = {
296 .direction_input = pca953x_direction_input,
297 .direction_output = pca953x_direction_output,
298 .get_value = pca953x_get_value,
299 .set_value = pca953x_set_value,
300 .get_function = pca953x_get_function,
301 .xlate = pca953x_xlate,
304 static int pca953x_probe(struct udevice *dev)
306 struct pca953x_info *info = dev_get_plat(dev);
307 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
308 char name[32], label[8], *str;
316 addr = dev_read_addr(dev);
322 driver_data = dev_get_driver_data(dev);
324 info->gpio_count = driver_data & PCA_GPIO_MASK;
325 if (info->gpio_count > MAX_BANK * BANK_SZ) {
326 dev_err(dev, "Max support %d pins now\n", MAX_BANK * BANK_SZ);
330 info->chip_type = PCA_CHIP_TYPE(driver_data);
331 if (info->chip_type == PCA953X_TYPE)
332 info->regs = &pca953x_regs;
334 info->regs = &pca957x_regs;
336 info->bank_count = DIV_ROUND_UP(info->gpio_count, BANK_SZ);
338 ret = pca953x_read_regs(dev, info->regs->output, info->reg_output);
340 dev_err(dev, "Error reading output register\n");
344 ret = pca953x_read_regs(dev, PCA953X_DIRECTION, info->reg_direction);
346 dev_err(dev, "Error reading direction register\n");
350 tmp = dev_read_prop(dev, "label", &size);
353 memcpy(label, tmp, sizeof(label) - 1);
354 label[sizeof(label) - 1] = '\0';
355 snprintf(name, sizeof(name), "%s@%x_", label, info->addr);
357 snprintf(name, sizeof(name), "gpio@%x_", info->addr);
360 /* Clear the polarity registers to no invert */
361 memset(val, 0, MAX_BANK);
362 ret = pca953x_write_regs(dev, info->regs->invert, val);
364 dev_err(dev, "Error writing invert register\n");
371 uc_priv->bank_name = str;
372 uc_priv->gpio_count = info->gpio_count;
374 dev_dbg(dev, "%s is ready\n", str);
379 #define OF_953X(__nrgpio, __int) (ulong)(__nrgpio | PCA953X_TYPE | __int)
380 #define OF_957X(__nrgpio, __int) (ulong)(__nrgpio | PCA957X_TYPE | __int)
382 static const struct udevice_id pca953x_ids[] = {
383 { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
384 { .compatible = "nxp,pca9534", .data = OF_953X(8, PCA_INT), },
385 { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
386 { .compatible = "nxp,pca9536", .data = OF_953X(4, 0), },
387 { .compatible = "nxp,pca9537", .data = OF_953X(4, PCA_INT), },
388 { .compatible = "nxp,pca9538", .data = OF_953X(8, PCA_INT), },
389 { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
390 { .compatible = "nxp,pca9554", .data = OF_953X(8, PCA_INT), },
391 { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
392 { .compatible = "nxp,pca9556", .data = OF_953X(8, 0), },
393 { .compatible = "nxp,pca9557", .data = OF_953X(8, 0), },
394 { .compatible = "nxp,pca9574", .data = OF_957X(8, PCA_INT), },
395 { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
396 { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
398 { .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), },
400 { .compatible = "maxim,max7310", .data = OF_953X(8, 0), },
401 { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
402 { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
403 { .compatible = "maxim,max7315", .data = OF_953X(8, PCA_INT), },
405 { .compatible = "ti,pca6107", .data = OF_953X(8, PCA_INT), },
406 { .compatible = "ti,tca6408", .data = OF_953X(8, PCA_INT), },
407 { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
408 { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
409 { .compatible = "ti,tca9539", .data = OF_953X(16, PCA_INT), },
411 { .compatible = "onsemi,pca9654", .data = OF_953X(8, PCA_INT), },
413 { .compatible = "exar,xra1202", .data = OF_953X(8, 0), },
417 U_BOOT_DRIVER(pca953x) = {
421 .probe = pca953x_probe,
422 .plat_auto = sizeof(struct pca953x_info),
423 .of_match = pca953x_ids,