2 * Copyright 2008 Extreme Engineering Solutions, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * Version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * Driver for NXP's 4, 8 and 16 bit I2C gpio expanders (eg pca9537, pca9557,
28 /* Default to an address that hopefully won't corrupt other i2c devices */
29 #ifndef CONFIG_SYS_I2C_PCA953X_ADDR
30 #define CONFIG_SYS_I2C_PCA953X_ADDR (~0)
41 #ifdef CONFIG_SYS_I2C_PCA953X_WIDTH
42 struct pca953x_chip_ngpio {
47 static struct pca953x_chip_ngpio pca953x_chip_ngpios[] =
48 CONFIG_SYS_I2C_PCA953X_WIDTH;
51 * Determine the number of GPIO pins supported. If we don't know we assume
54 static int pca953x_ngpio(uint8_t chip)
58 for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++)
59 if (pca953x_chip_ngpios[i].chip == chip)
60 return pca953x_chip_ngpios[i].ngpio;
65 static int pca953x_ngpio(uint8_t chip)
72 * Modify masked bits in register
74 static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data)
79 if (pca953x_ngpio(chip) <= 8) {
80 if (i2c_read(chip, addr, 1, &valb, 1))
86 return i2c_write(chip, addr, 1, &valb, 1);
88 if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
94 return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2);
98 static int pca953x_reg_read(uint8_t chip, uint addr, uint *data)
103 if (pca953x_ngpio(chip) <= 8) {
104 if (i2c_read(chip, addr, 1, &valb, 1))
108 if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
116 * Set output value of IO pins in 'mask' to corresponding value in 'data'
119 int pca953x_set_val(uint8_t chip, uint mask, uint data)
121 return pca953x_reg_write(chip, PCA953X_OUT, mask, data);
125 * Set read polarity of IO pins in 'mask' to corresponding value in 'data'
126 * 0 = read pin value, 1 = read inverted pin value
128 int pca953x_set_pol(uint8_t chip, uint mask, uint data)
130 return pca953x_reg_write(chip, PCA953X_POL, mask, data);
134 * Set direction of IO pins in 'mask' to corresponding value in 'data'
135 * 0 = output, 1 = input
137 int pca953x_set_dir(uint8_t chip, uint mask, uint data)
139 return pca953x_reg_write(chip, PCA953X_CONF, mask, data);
143 * Read current logic level of all IO pins
145 int pca953x_get_val(uint8_t chip)
149 if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0)
155 #ifdef CONFIG_CMD_PCA953X
156 #ifdef CONFIG_CMD_PCA953X_INFO
158 * Display pca953x information
160 static int pca953x_info(uint8_t chip)
164 int nr_gpio = pca953x_ngpio(chip);
165 int msb = nr_gpio - 1;
167 printf("pca953x@ 0x%x (%d pins):\n\n", chip, nr_gpio);
168 printf("gpio pins: ");
169 for (i = msb; i >= 0; i--)
172 for (i = 11 + nr_gpio; i > 0; i--)
176 if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0)
179 for (i = msb; i >= 0; i--)
180 printf("%c", data & (1 << i) ? 'i' : 'o');
183 if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0)
186 for (i = msb; i >= 0; i--)
187 printf("%c", data & (1 << i) ? '1' : '0');
190 if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0)
193 for (i = msb; i >= 0; i--)
194 printf("%c", data & (1 << i) ? '1' : '0');
197 if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0)
200 for (i = msb; i >= 0; i--)
201 printf("%c", data & (1 << i) ? '1' : '0');
206 #endif /* CONFIG_CMD_PCA953X_INFO */
208 cmd_tbl_t cmd_pca953x[] = {
209 U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""),
210 U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""),
211 U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""),
212 U_BOOT_CMD_MKENT(invert, 4, 0, (void *)PCA953X_CMD_INVERT, "", ""),
213 #ifdef CONFIG_CMD_PCA953X_INFO
214 U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""),
218 int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
220 static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
221 int ret = CMD_RET_USAGE, val;
226 c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x));
228 /* All commands but "device" require 'maxargs' arguments */
229 if (!c || !((argc == (c->maxargs)) ||
230 (((int)c->cmd == PCA953X_CMD_DEVICE) &&
231 (argc == (c->maxargs - 1))))) {
232 return CMD_RET_USAGE;
235 /* arg2 used as chip number or pin number */
237 ul_arg2 = simple_strtoul(argv[2], NULL, 16);
239 /* arg3 used as pin or invert value */
241 ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1;
243 switch ((int)c->cmd) {
244 #ifdef CONFIG_CMD_PCA953X_INFO
245 case PCA953X_CMD_INFO:
246 ret = pca953x_info(chip);
248 ret = CMD_RET_FAILURE;
252 case PCA953X_CMD_DEVICE:
254 chip = (uint8_t)ul_arg2;
255 printf("Current device address: 0x%x\n", chip);
256 ret = CMD_RET_SUCCESS;
259 case PCA953X_CMD_INPUT:
260 ret = pca953x_set_dir(chip, (1 << ul_arg2),
261 PCA953X_DIR_IN << ul_arg2);
262 val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
265 ret = CMD_RET_FAILURE;
267 printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2,
271 case PCA953X_CMD_OUTPUT:
272 ret = pca953x_set_dir(chip, (1 << ul_arg2),
273 (PCA953X_DIR_OUT << ul_arg2));
275 ret = pca953x_set_val(chip, (1 << ul_arg2),
276 (ul_arg3 << ul_arg2));
278 ret = CMD_RET_FAILURE;
281 case PCA953X_CMD_INVERT:
282 ret = pca953x_set_pol(chip, (1 << ul_arg2),
283 (ul_arg3 << ul_arg2));
285 ret = CMD_RET_FAILURE;
289 if (ret == CMD_RET_FAILURE)
290 eprintf("Error talking to chip at 0x%x\n", chip);
296 pca953x, 5, 1, do_pca953x,
297 "pca953x gpio access",
299 " - show or set current device address\n"
300 #ifdef CONFIG_CMD_PCA953X_INFO
302 " - display info for current chip\n"
304 "pca953x output pin 0|1\n"
305 " - set pin as output and drive low or high\n"
306 "pca953x invert pin 0|1\n"
307 " - disable/enable polarity inversion for reads\n"
308 "pca953x input pin\n"
309 " - set pin as input and read value"
312 #endif /* CONFIG_CMD_PCA953X */