1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2008 Extreme Engineering Solutions, Inc.
7 * Driver for NXP's 4, 8 and 16 bit I2C gpio expanders (eg pca9537, pca9557,
16 /* Default to an address that hopefully won't corrupt other i2c devices */
17 #ifndef CONFIG_SYS_I2C_PCA953X_ADDR
18 #define CONFIG_SYS_I2C_PCA953X_ADDR (~0)
29 #ifdef CONFIG_SYS_I2C_PCA953X_WIDTH
30 struct pca953x_chip_ngpio {
35 static struct pca953x_chip_ngpio pca953x_chip_ngpios[] =
36 CONFIG_SYS_I2C_PCA953X_WIDTH;
39 * Determine the number of GPIO pins supported. If we don't know we assume
42 static int pca953x_ngpio(uint8_t chip)
46 for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++)
47 if (pca953x_chip_ngpios[i].chip == chip)
48 return pca953x_chip_ngpios[i].ngpio;
53 static int pca953x_ngpio(uint8_t chip)
60 * Modify masked bits in register
62 static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data)
67 if (pca953x_ngpio(chip) <= 8) {
68 if (i2c_read(chip, addr, 1, &valb, 1))
74 return i2c_write(chip, addr, 1, &valb, 1);
76 if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
79 valw = le16_to_cpu(valw);
82 valw = cpu_to_le16(valw);
84 return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2);
88 static int pca953x_reg_read(uint8_t chip, uint addr, uint *data)
93 if (pca953x_ngpio(chip) <= 8) {
94 if (i2c_read(chip, addr, 1, &valb, 1))
98 if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
100 *data = (uint)le16_to_cpu(valw);
106 * Set output value of IO pins in 'mask' to corresponding value in 'data'
109 int pca953x_set_val(uint8_t chip, uint mask, uint data)
111 return pca953x_reg_write(chip, PCA953X_OUT, mask, data);
115 * Set read polarity of IO pins in 'mask' to corresponding value in 'data'
116 * 0 = read pin value, 1 = read inverted pin value
118 int pca953x_set_pol(uint8_t chip, uint mask, uint data)
120 return pca953x_reg_write(chip, PCA953X_POL, mask, data);
124 * Set direction of IO pins in 'mask' to corresponding value in 'data'
125 * 0 = output, 1 = input
127 int pca953x_set_dir(uint8_t chip, uint mask, uint data)
129 return pca953x_reg_write(chip, PCA953X_CONF, mask, data);
133 * Read current logic level of all IO pins
135 int pca953x_get_val(uint8_t chip)
139 if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0)
145 #if defined(CONFIG_CMD_PCA953X) && !defined(CONFIG_SPL_BUILD)
147 * Display pca953x information
149 static int pca953x_info(uint8_t chip)
153 int nr_gpio = pca953x_ngpio(chip);
154 int msb = nr_gpio - 1;
156 printf("pca953x@ 0x%x (%d pins):\n\n", chip, nr_gpio);
157 printf("gpio pins: ");
158 for (i = msb; i >= 0; i--)
161 for (i = 11 + nr_gpio; i > 0; i--)
165 if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0)
168 for (i = msb; i >= 0; i--)
169 printf("%c", data & (1 << i) ? 'i' : 'o');
172 if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0)
175 for (i = msb; i >= 0; i--)
176 printf("%c", data & (1 << i) ? '1' : '0');
179 if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0)
182 for (i = msb; i >= 0; i--)
183 printf("%c", data & (1 << i) ? '1' : '0');
186 if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0)
189 for (i = msb; i >= 0; i--)
190 printf("%c", data & (1 << i) ? '1' : '0');
196 static struct cmd_tbl cmd_pca953x[] = {
197 U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""),
198 U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""),
199 U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""),
200 U_BOOT_CMD_MKENT(invert, 4, 0, (void *)PCA953X_CMD_INVERT, "", ""),
201 U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""),
204 static int do_pca953x(struct cmd_tbl *cmdtp, int flag, int argc,
207 static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
208 int ret = CMD_RET_USAGE, val;
213 c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x));
215 /* All commands but "device" require 'maxargs' arguments */
216 if (!c || !((argc == (c->maxargs)) ||
217 (((long)c->cmd == PCA953X_CMD_DEVICE) &&
218 (argc == (c->maxargs - 1))))) {
219 return CMD_RET_USAGE;
222 /* arg2 used as chip number or pin number */
224 ul_arg2 = hextoul(argv[2], NULL);
226 /* arg3 used as pin or invert value */
228 ul_arg3 = hextoul(argv[3], NULL) & 0x1;
230 switch ((long)c->cmd) {
231 case PCA953X_CMD_INFO:
232 ret = pca953x_info(chip);
234 ret = CMD_RET_FAILURE;
237 case PCA953X_CMD_DEVICE:
239 chip = (uint8_t)ul_arg2;
240 printf("Current device address: 0x%x\n", chip);
241 ret = CMD_RET_SUCCESS;
244 case PCA953X_CMD_INPUT:
245 ret = pca953x_set_dir(chip, (1 << ul_arg2),
246 PCA953X_DIR_IN << ul_arg2);
247 val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
250 ret = CMD_RET_FAILURE;
252 printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2,
256 case PCA953X_CMD_OUTPUT:
257 ret = pca953x_set_dir(chip, (1 << ul_arg2),
258 (PCA953X_DIR_OUT << ul_arg2));
260 ret = pca953x_set_val(chip, (1 << ul_arg2),
261 (ul_arg3 << ul_arg2));
263 ret = CMD_RET_FAILURE;
266 case PCA953X_CMD_INVERT:
267 ret = pca953x_set_pol(chip, (1 << ul_arg2),
268 (ul_arg3 << ul_arg2));
270 ret = CMD_RET_FAILURE;
274 if (ret == CMD_RET_FAILURE)
275 eprintf("Error talking to chip at 0x%x\n", chip);
281 pca953x, 5, 1, do_pca953x,
282 "pca953x gpio access",
284 " - show or set current device address\n"
286 " - display info for current chip\n"
287 "pca953x output pin 0|1\n"
288 " - set pin as output and drive low or high\n"
289 "pca953x invert pin 0|1\n"
290 " - disable/enable polarity inversion for reads\n"
291 "pca953x input pin\n"
292 " - set pin as input and read value"
295 #endif /* CONFIG_CMD_PCA953X */