2 * Copyright 2013 Texas Instruments, Inc.
3 * Author: Dan Murphy <dmurphy@ti.com>
5 * Derived work from the pca953x.c driver
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 /* tca642x register address definitions */
29 struct tca642x_bank_info tca642x_regs[] = {
33 .configuration_reg = 0x0c },
37 .configuration_reg = 0x0d },
41 .configuration_reg = 0x0e },
45 * Modify masked bits in register
47 static int tca642x_reg_write(uchar chip, uint8_t addr,
48 uint8_t reg_bit, uint8_t data)
54 org_bus_num = i2c_get_bus_num();
55 i2c_set_bus_num(CONFIG_SYS_I2C_TCA642X_BUS_NUM);
57 if (i2c_read(chip, addr, 1, (uint8_t *)&valw, 1)) {
58 printf("Could not read before writing\n");
65 ret = i2c_write(chip, addr, 1, (u8 *)&valw, 1);
68 i2c_set_bus_num(org_bus_num);
72 static int tca642x_reg_read(uchar chip, uint8_t addr, uint8_t *data)
78 org_bus_num = i2c_get_bus_num();
79 i2c_set_bus_num(CONFIG_SYS_I2C_TCA642X_BUS_NUM);
80 if (i2c_read(chip, addr, 1, (u8 *)&valw, 1)) {
88 i2c_set_bus_num(org_bus_num);
93 * Set output value of IO pins in 'reg_bit' to corresponding value in 'data'
96 int tca642x_set_val(uchar chip, uint8_t gpio_bank,
97 uint8_t reg_bit, uint8_t data)
99 uint8_t out_reg = tca642x_regs[gpio_bank].output_reg;
101 return tca642x_reg_write(chip, out_reg, reg_bit, data);
105 * Set read polarity of IO pins in 'reg_bit' to corresponding value in 'data'
106 * 0 = read pin value, 1 = read inverted pin value
108 int tca642x_set_pol(uchar chip, uint8_t gpio_bank,
109 uint8_t reg_bit, uint8_t data)
111 uint8_t pol_reg = tca642x_regs[gpio_bank].polarity_reg;
113 return tca642x_reg_write(chip, pol_reg, reg_bit, data);
117 * Set direction of IO pins in 'reg_bit' to corresponding value in 'data'
118 * 0 = output, 1 = input
120 int tca642x_set_dir(uchar chip, uint8_t gpio_bank,
121 uint8_t reg_bit, uint8_t data)
123 uint8_t config_reg = tca642x_regs[gpio_bank].configuration_reg;
125 return tca642x_reg_write(chip, config_reg, reg_bit, data);
129 * Read current logic level of all IO pins
131 int tca642x_get_val(uchar chip, uint8_t gpio_bank)
134 uint8_t in_reg = tca642x_regs[gpio_bank].input_reg;
136 if (tca642x_reg_read(chip, in_reg, &val) < 0)
143 * Set the inital register states for the tca642x gpio expander
145 int tca642x_set_inital_state(uchar chip, struct tca642x_bank_info init_data[])
149 uint8_t polarity_reg;
152 for (i = 0; i < 3; i++) {
153 config_reg = tca642x_regs[i].configuration_reg;
154 ret = tca642x_reg_write(chip, config_reg, 0xff,
155 init_data[i].configuration_reg);
156 polarity_reg = tca642x_regs[i].polarity_reg;
157 ret = tca642x_reg_write(chip, polarity_reg, 0xff,
158 init_data[i].polarity_reg);
159 output_reg = tca642x_regs[i].output_reg;
160 ret = tca642x_reg_write(chip, output_reg, 0xff,
161 init_data[i].output_reg);
167 #if defined(CONFIG_CMD_TCA642X) && !defined(CONFIG_SPL_BUILD)
169 * Display tca642x information
171 static int tca642x_info(uchar chip)
176 printf("tca642x@ 0x%x (%d pins):\n", chip, 24);
177 for (i = 0; i < 3; i++) {
178 printf("Bank %i\n", i);
179 if (tca642x_reg_read(chip,
180 tca642x_regs[i].configuration_reg,
183 printf("\tConfiguration: ");
184 for (j = 7; j >= 0; j--)
185 printf("%c", data & (1 << j) ? 'i' : 'o');
188 if (tca642x_reg_read(chip,
189 tca642x_regs[i].polarity_reg, &data) < 0)
191 printf("\tPolarity: ");
192 for (j = 7; j >= 0; j--)
193 printf("%c", data & (1 << j) ? '1' : '0');
196 if (tca642x_reg_read(chip,
197 tca642x_regs[i].input_reg, &data) < 0)
199 printf("\tInput value: ");
200 for (j = 7; j >= 0; j--)
201 printf("%c", data & (1 << j) ? '1' : '0');
204 if (tca642x_reg_read(chip,
205 tca642x_regs[i].output_reg, &data) < 0)
207 printf("\tOutput value: ");
208 for (j = 7; j >= 0; j--)
209 printf("%c", data & (1 << j) ? '1' : '0');
216 static int tca642x_get_bank(int pin)
222 } else if ((pin >= 10) && (pin <= 17)) {
224 } else if ((pin >= 20) && (pin <= 27)) {
227 printf("Requested pin is not available\n");
234 static struct cmd_tbl cmd_tca642x[] = {
235 U_BOOT_CMD_MKENT(device, 3, 0, (void *)TCA642X_CMD_DEVICE, "", ""),
236 U_BOOT_CMD_MKENT(output, 4, 0, (void *)TCA642X_CMD_OUTPUT, "", ""),
237 U_BOOT_CMD_MKENT(input, 3, 0, (void *)TCA642X_CMD_INPUT, "", ""),
238 U_BOOT_CMD_MKENT(invert, 4, 0, (void *)TCA642X_CMD_INVERT, "", ""),
239 U_BOOT_CMD_MKENT(info, 2, 0, (void *)TCA642X_CMD_INFO, "", ""),
242 static int do_tca642x(struct cmd_tbl *cmdtp, int flag, int argc,
245 static uchar chip = CONFIG_SYS_I2C_TCA642X_ADDR;
246 int ret = CMD_RET_USAGE, val;
253 c = find_cmd_tbl(argv[1], cmd_tca642x, ARRAY_SIZE(cmd_tca642x));
255 /* All commands but "device" require 'maxargs' arguments */
257 !((argc == (c->maxargs)) ||
258 (((int)c->cmd == TCA642X_CMD_DEVICE) &&
259 (argc == (c->maxargs - 1))))) {
260 return CMD_RET_USAGE;
263 /* arg2 used as chip number or pin number */
265 ul_arg2 = simple_strtoul(argv[2], NULL, 10);
267 /* arg3 used as pin or invert value */
269 ul_arg3 = simple_strtoul(argv[3], NULL, 10) & 0x1;
271 switch ((int)c->cmd) {
272 case TCA642X_CMD_INFO:
273 ret = tca642x_info(chip);
275 ret = CMD_RET_FAILURE;
278 case TCA642X_CMD_DEVICE:
280 chip = (uint8_t)ul_arg2;
281 printf("Current device address: 0x%x\n", chip);
282 ret = CMD_RET_SUCCESS;
285 case TCA642X_CMD_INPUT:
286 gpio_bank = tca642x_get_bank(ul_arg2);
288 ret = CMD_RET_FAILURE;
291 bank_shift = ul_arg2 - (gpio_bank * 10);
292 ret = tca642x_set_dir(chip, gpio_bank, (1 << bank_shift),
293 TCA642X_DIR_IN << bank_shift);
294 val = (tca642x_get_val(chip, gpio_bank) &
295 (1 << bank_shift)) != 0;
298 ret = CMD_RET_FAILURE;
300 printf("chip 0x%02x, pin 0x%lx = %d\n", chip,
304 case TCA642X_CMD_OUTPUT:
305 gpio_bank = tca642x_get_bank(ul_arg2);
307 ret = CMD_RET_FAILURE;
310 bank_shift = ul_arg2 - (gpio_bank * 10);
311 ret = tca642x_set_dir(chip, gpio_bank, (1 << bank_shift),
312 (TCA642X_DIR_OUT << bank_shift));
314 ret = tca642x_set_val(chip,
315 gpio_bank, (1 << bank_shift),
316 (ul_arg3 << bank_shift));
318 ret = CMD_RET_FAILURE;
321 case TCA642X_CMD_INVERT:
322 gpio_bank = tca642x_get_bank(ul_arg2);
324 ret = CMD_RET_FAILURE;
327 bank_shift = ul_arg2 - (gpio_bank * 10);
328 ret = tca642x_set_pol(chip, gpio_bank, (1 << bank_shift),
329 (ul_arg3 << bank_shift));
331 ret = CMD_RET_FAILURE;
335 if (ret == CMD_RET_FAILURE)
336 eprintf("Error talking to chip at 0x%x\n", chip);
342 tca642x, 5, 1, do_tca642x,
343 "tca642x gpio access",
345 " - show or set current device address\n"
347 " - display info for current chip\n"
348 "tca642x output pin 0|1\n"
349 " - set pin as output and drive low or high\n"
350 "tca642x invert pin 0|1\n"
351 " - disable/enable polarity inversion for reads\n"
352 "tca642x input pin\n"
353 " - set pin as input and read value"
356 #endif /* CONFIG_CMD_TCA642X */