X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=drivers%2Fgpio%2Fpca953x.c;h=238e02805cb6673d8eb681f281fb8bafb74a8947;hb=4096f350d5218830ffedc56631e4382df478a370;hp=359fdeea23246b8285a5382b50b5e8885bd826af;hpb=aad813a342aca1a8127a283c64813e4ae4464d9c;p=u-boot diff --git a/drivers/gpio/pca953x.c b/drivers/gpio/pca953x.c index 359fdeea23..238e02805c 100644 --- a/drivers/gpio/pca953x.c +++ b/drivers/gpio/pca953x.c @@ -1,19 +1,7 @@ /* * Copyright 2008 Extreme Engineering Solutions, Inc. * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * Version 2 as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, - * MA 02111-1307 USA + * SPDX-License-Identifier: GPL-2.0 */ /* @@ -47,9 +35,6 @@ struct pca953x_chip_ngpio { static struct pca953x_chip_ngpio pca953x_chip_ngpios[] = CONFIG_SYS_I2C_PCA953X_WIDTH; -#define NUM_CHIP_GPIOS (sizeof(pca953x_chip_ngpios) / \ - sizeof(struct pca953x_chip_ngpio)) - /* * Determine the number of GPIO pins supported. If we don't know we assume * 8 pins. @@ -58,7 +43,7 @@ static int pca953x_ngpio(uint8_t chip) { int i; - for (i = 0; i < NUM_CHIP_GPIOS; i++) + for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++) if (pca953x_chip_ngpios[i].chip == chip) return pca953x_chip_ngpios[i].ngpio; @@ -91,8 +76,10 @@ static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data) if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2)) return -1; + valw = le16_to_cpu(valw); valw &= ~mask; valw |= data; + valw = cpu_to_le16(valw); return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2); } @@ -110,7 +97,7 @@ static int pca953x_reg_read(uint8_t chip, uint addr, uint *data) } else { if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2)) return -1; - *data = (int)valw; + *data = (uint)le16_to_cpu(valw); } return 0; } @@ -221,7 +208,7 @@ cmd_tbl_t cmd_pca953x[] = { int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR; - int val; + int ret = CMD_RET_USAGE, val; ulong ul_arg2 = 0; ulong ul_arg3 = 0; cmd_tbl_t *c; @@ -230,9 +217,9 @@ int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) /* All commands but "device" require 'maxargs' arguments */ if (!c || !((argc == (c->maxargs)) || - (((int)c->cmd == PCA953X_CMD_DEVICE) && + (((long)c->cmd == PCA953X_CMD_DEVICE) && (argc == (c->maxargs - 1))))) { - return cmd_usage(cmdtp); + return CMD_RET_USAGE; } /* arg2 used as chip number or pin number */ @@ -243,35 +230,56 @@ int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) if (argc > 3) ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1; - switch ((int)c->cmd) { + switch ((long)c->cmd) { #ifdef CONFIG_CMD_PCA953X_INFO case PCA953X_CMD_INFO: - return pca953x_info(chip); + ret = pca953x_info(chip); + if (ret) + ret = CMD_RET_FAILURE; + break; #endif + case PCA953X_CMD_DEVICE: if (argc == 3) chip = (uint8_t)ul_arg2; printf("Current device address: 0x%x\n", chip); - return 0; + ret = CMD_RET_SUCCESS; + break; + case PCA953X_CMD_INPUT: - pca953x_set_dir(chip, (1 << ul_arg2), + ret = pca953x_set_dir(chip, (1 << ul_arg2), PCA953X_DIR_IN << ul_arg2); val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0; - printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2, val); - return val; + if (ret) + ret = CMD_RET_FAILURE; + else + printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2, + val); + break; + case PCA953X_CMD_OUTPUT: - pca953x_set_dir(chip, (1 << ul_arg2), + ret = pca953x_set_dir(chip, (1 << ul_arg2), (PCA953X_DIR_OUT << ul_arg2)); - return pca953x_set_val(chip, (1 << ul_arg2), - (ul_arg3 << ul_arg2)); + if (!ret) + ret = pca953x_set_val(chip, (1 << ul_arg2), + (ul_arg3 << ul_arg2)); + if (ret) + ret = CMD_RET_FAILURE; + break; + case PCA953X_CMD_INVERT: - return pca953x_set_pol(chip, (1 << ul_arg2), + ret = pca953x_set_pol(chip, (1 << ul_arg2), (ul_arg3 << ul_arg2)); - default: - /* We should never get here */ - return 1; + if (ret) + ret = CMD_RET_FAILURE; + break; } + + if (ret == CMD_RET_FAILURE) + eprintf("Error talking to chip at 0x%x\n", chip); + + return ret; } U_BOOT_CMD( @@ -287,7 +295,7 @@ U_BOOT_CMD( " - set pin as output and drive low or high\n" "pca953x invert pin 0|1\n" " - disable/enable polarity inversion for reads\n" - "pca953x intput pin\n" + "pca953x input pin\n" " - set pin as input and read value" );