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,
15 /* Default to an address that hopefully won't corrupt other i2c devices */
16 #ifndef CONFIG_SYS_I2C_PCA953X_ADDR
17 #define CONFIG_SYS_I2C_PCA953X_ADDR (~0)
28 #ifdef CONFIG_SYS_I2C_PCA953X_WIDTH
29 struct pca953x_chip_ngpio {
34 static struct pca953x_chip_ngpio pca953x_chip_ngpios[] =
35 CONFIG_SYS_I2C_PCA953X_WIDTH;
38 * Determine the number of GPIO pins supported. If we don't know we assume
41 static int pca953x_ngpio(uint8_t chip)
45 for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++)
46 if (pca953x_chip_ngpios[i].chip == chip)
47 return pca953x_chip_ngpios[i].ngpio;
52 static int pca953x_ngpio(uint8_t chip)
59 * Modify masked bits in register
61 static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data)
66 if (pca953x_ngpio(chip) <= 8) {
67 if (i2c_read(chip, addr, 1, &valb, 1))
73 return i2c_write(chip, addr, 1, &valb, 1);
75 if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
78 valw = le16_to_cpu(valw);
81 valw = cpu_to_le16(valw);
83 return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2);
87 static int pca953x_reg_read(uint8_t chip, uint addr, uint *data)
92 if (pca953x_ngpio(chip) <= 8) {
93 if (i2c_read(chip, addr, 1, &valb, 1))
97 if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
99 *data = (uint)le16_to_cpu(valw);
105 * Set output value of IO pins in 'mask' to corresponding value in 'data'
108 int pca953x_set_val(uint8_t chip, uint mask, uint data)
110 return pca953x_reg_write(chip, PCA953X_OUT, mask, data);
114 * Set read polarity of IO pins in 'mask' to corresponding value in 'data'
115 * 0 = read pin value, 1 = read inverted pin value
117 int pca953x_set_pol(uint8_t chip, uint mask, uint data)
119 return pca953x_reg_write(chip, PCA953X_POL, mask, data);
123 * Set direction of IO pins in 'mask' to corresponding value in 'data'
124 * 0 = output, 1 = input
126 int pca953x_set_dir(uint8_t chip, uint mask, uint data)
128 return pca953x_reg_write(chip, PCA953X_CONF, mask, data);
132 * Read current logic level of all IO pins
134 int pca953x_get_val(uint8_t chip)
138 if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0)
144 #if defined(CONFIG_CMD_PCA953X) && !defined(CONFIG_SPL_BUILD)
146 * Display pca953x information
148 static int pca953x_info(uint8_t chip)
152 int nr_gpio = pca953x_ngpio(chip);
153 int msb = nr_gpio - 1;
155 printf("pca953x@ 0x%x (%d pins):\n\n", chip, nr_gpio);
156 printf("gpio pins: ");
157 for (i = msb; i >= 0; i--)
160 for (i = 11 + nr_gpio; i > 0; i--)
164 if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0)
167 for (i = msb; i >= 0; i--)
168 printf("%c", data & (1 << i) ? 'i' : 'o');
171 if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0)
174 for (i = msb; i >= 0; i--)
175 printf("%c", data & (1 << i) ? '1' : '0');
178 if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0)
181 for (i = msb; i >= 0; i--)
182 printf("%c", data & (1 << i) ? '1' : '0');
185 if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0)
188 for (i = msb; i >= 0; i--)
189 printf("%c", data & (1 << i) ? '1' : '0');
195 static cmd_tbl_t cmd_pca953x[] = {
196 U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""),
197 U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""),
198 U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""),
199 U_BOOT_CMD_MKENT(invert, 4, 0, (void *)PCA953X_CMD_INVERT, "", ""),
200 U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""),
203 static int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
205 static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
206 int ret = CMD_RET_USAGE, val;
211 c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x));
213 /* All commands but "device" require 'maxargs' arguments */
214 if (!c || !((argc == (c->maxargs)) ||
215 (((long)c->cmd == PCA953X_CMD_DEVICE) &&
216 (argc == (c->maxargs - 1))))) {
217 return CMD_RET_USAGE;
220 /* arg2 used as chip number or pin number */
222 ul_arg2 = simple_strtoul(argv[2], NULL, 16);
224 /* arg3 used as pin or invert value */
226 ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1;
228 switch ((long)c->cmd) {
229 case PCA953X_CMD_INFO:
230 ret = pca953x_info(chip);
232 ret = CMD_RET_FAILURE;
235 case PCA953X_CMD_DEVICE:
237 chip = (uint8_t)ul_arg2;
238 printf("Current device address: 0x%x\n", chip);
239 ret = CMD_RET_SUCCESS;
242 case PCA953X_CMD_INPUT:
243 ret = pca953x_set_dir(chip, (1 << ul_arg2),
244 PCA953X_DIR_IN << ul_arg2);
245 val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
248 ret = CMD_RET_FAILURE;
250 printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2,
254 case PCA953X_CMD_OUTPUT:
255 ret = pca953x_set_dir(chip, (1 << ul_arg2),
256 (PCA953X_DIR_OUT << ul_arg2));
258 ret = pca953x_set_val(chip, (1 << ul_arg2),
259 (ul_arg3 << ul_arg2));
261 ret = CMD_RET_FAILURE;
264 case PCA953X_CMD_INVERT:
265 ret = pca953x_set_pol(chip, (1 << ul_arg2),
266 (ul_arg3 << ul_arg2));
268 ret = CMD_RET_FAILURE;
272 if (ret == CMD_RET_FAILURE)
273 eprintf("Error talking to chip at 0x%x\n", chip);
279 pca953x, 5, 1, do_pca953x,
280 "pca953x gpio access",
282 " - show or set current device address\n"
284 " - display info for current chip\n"
285 "pca953x output pin 0|1\n"
286 " - set pin as output and drive low or high\n"
287 "pca953x invert pin 0|1\n"
288 " - disable/enable polarity inversion for reads\n"
289 "pca953x input pin\n"
290 " - set pin as input and read value"
293 #endif /* CONFIG_CMD_PCA953X */