Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Extreme Engineering Solutions, Inc. |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 12 | * |
| 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, |
| 16 | * MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | /* |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 20 | * Driver for NXP's 4, 8 and 16 bit I2C gpio expanders (eg pca9537, pca9557, |
| 21 | * pca9539, etc) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <i2c.h> |
| 26 | #include <pca953x.h> |
| 27 | |
| 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) |
| 31 | #endif |
| 32 | |
| 33 | enum { |
| 34 | PCA953X_CMD_INFO, |
| 35 | PCA953X_CMD_DEVICE, |
| 36 | PCA953X_CMD_OUTPUT, |
| 37 | PCA953X_CMD_INPUT, |
| 38 | PCA953X_CMD_INVERT, |
| 39 | }; |
| 40 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 41 | #ifdef CONFIG_SYS_I2C_PCA953X_WIDTH |
| 42 | struct pca953x_chip_ngpio { |
| 43 | uint8_t chip; |
| 44 | uint8_t ngpio; |
| 45 | }; |
| 46 | |
| 47 | static struct pca953x_chip_ngpio pca953x_chip_ngpios[] = |
| 48 | CONFIG_SYS_I2C_PCA953X_WIDTH; |
| 49 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 50 | /* |
| 51 | * Determine the number of GPIO pins supported. If we don't know we assume |
| 52 | * 8 pins. |
| 53 | */ |
| 54 | static int pca953x_ngpio(uint8_t chip) |
| 55 | { |
| 56 | int i; |
| 57 | |
Axel Lin | f218761 | 2013-06-22 18:22:48 +0800 | [diff] [blame] | 58 | for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++) |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 59 | if (pca953x_chip_ngpios[i].chip == chip) |
| 60 | return pca953x_chip_ngpios[i].ngpio; |
| 61 | |
| 62 | return 8; |
| 63 | } |
| 64 | #else |
| 65 | static int pca953x_ngpio(uint8_t chip) |
| 66 | { |
| 67 | return 8; |
| 68 | } |
| 69 | #endif |
| 70 | |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 71 | /* |
| 72 | * Modify masked bits in register |
| 73 | */ |
| 74 | static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data) |
| 75 | { |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 76 | uint8_t valb; |
| 77 | uint16_t valw; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 78 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 79 | if (pca953x_ngpio(chip) <= 8) { |
| 80 | if (i2c_read(chip, addr, 1, &valb, 1)) |
| 81 | return -1; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 82 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 83 | valb &= ~mask; |
| 84 | valb |= data; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 85 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 86 | return i2c_write(chip, addr, 1, &valb, 1); |
| 87 | } else { |
| 88 | if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2)) |
| 89 | return -1; |
| 90 | |
| 91 | valw &= ~mask; |
| 92 | valw |= data; |
| 93 | |
| 94 | return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static int pca953x_reg_read(uint8_t chip, uint addr, uint *data) |
| 99 | { |
| 100 | uint8_t valb; |
| 101 | uint16_t valw; |
| 102 | |
| 103 | if (pca953x_ngpio(chip) <= 8) { |
| 104 | if (i2c_read(chip, addr, 1, &valb, 1)) |
| 105 | return -1; |
| 106 | *data = (int)valb; |
| 107 | } else { |
| 108 | if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2)) |
| 109 | return -1; |
| 110 | *data = (int)valw; |
| 111 | } |
| 112 | return 0; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Set output value of IO pins in 'mask' to corresponding value in 'data' |
| 117 | * 0 = low, 1 = high |
| 118 | */ |
| 119 | int pca953x_set_val(uint8_t chip, uint mask, uint data) |
| 120 | { |
| 121 | return pca953x_reg_write(chip, PCA953X_OUT, mask, data); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Set read polarity of IO pins in 'mask' to corresponding value in 'data' |
| 126 | * 0 = read pin value, 1 = read inverted pin value |
| 127 | */ |
| 128 | int pca953x_set_pol(uint8_t chip, uint mask, uint data) |
| 129 | { |
| 130 | return pca953x_reg_write(chip, PCA953X_POL, mask, data); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Set direction of IO pins in 'mask' to corresponding value in 'data' |
| 135 | * 0 = output, 1 = input |
| 136 | */ |
| 137 | int pca953x_set_dir(uint8_t chip, uint mask, uint data) |
| 138 | { |
| 139 | return pca953x_reg_write(chip, PCA953X_CONF, mask, data); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Read current logic level of all IO pins |
| 144 | */ |
| 145 | int pca953x_get_val(uint8_t chip) |
| 146 | { |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 147 | uint val; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 148 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 149 | if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 150 | return -1; |
| 151 | |
| 152 | return (int)val; |
| 153 | } |
| 154 | |
| 155 | #ifdef CONFIG_CMD_PCA953X |
| 156 | #ifdef CONFIG_CMD_PCA953X_INFO |
| 157 | /* |
| 158 | * Display pca953x information |
| 159 | */ |
| 160 | static int pca953x_info(uint8_t chip) |
| 161 | { |
| 162 | int i; |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 163 | uint data; |
| 164 | int nr_gpio = pca953x_ngpio(chip); |
| 165 | int msb = nr_gpio - 1; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 166 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 167 | printf("pca953x@ 0x%x (%d pins):\n\n", chip, nr_gpio); |
| 168 | printf("gpio pins: "); |
| 169 | for (i = msb; i >= 0; i--) |
| 170 | printf("%x", i); |
| 171 | printf("\n"); |
| 172 | for (i = 11 + nr_gpio; i > 0; i--) |
| 173 | printf("-"); |
| 174 | printf("\n"); |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 175 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 176 | if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 177 | return -1; |
| 178 | printf("conf: "); |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 179 | for (i = msb; i >= 0; i--) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 180 | printf("%c", data & (1 << i) ? 'i' : 'o'); |
| 181 | printf("\n"); |
| 182 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 183 | if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 184 | return -1; |
| 185 | printf("invert: "); |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 186 | for (i = msb; i >= 0; i--) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 187 | printf("%c", data & (1 << i) ? '1' : '0'); |
| 188 | printf("\n"); |
| 189 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 190 | if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 191 | return -1; |
| 192 | printf("input: "); |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 193 | for (i = msb; i >= 0; i--) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 194 | printf("%c", data & (1 << i) ? '1' : '0'); |
| 195 | printf("\n"); |
| 196 | |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 197 | if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 198 | return -1; |
| 199 | printf("output: "); |
Chris Packham | 5dec49c | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 200 | for (i = msb; i >= 0; i--) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 201 | printf("%c", data & (1 << i) ? '1' : '0'); |
| 202 | printf("\n"); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | #endif /* CONFIG_CMD_PCA953X_INFO */ |
| 207 | |
| 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, "", ""), |
| 215 | #endif |
| 216 | }; |
| 217 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 218 | int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 219 | { |
| 220 | static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR; |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 221 | int ret = CMD_RET_USAGE, val; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 222 | ulong ul_arg2 = 0; |
| 223 | ulong ul_arg3 = 0; |
| 224 | cmd_tbl_t *c; |
| 225 | |
| 226 | c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x)); |
| 227 | |
| 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))))) { |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 232 | return CMD_RET_USAGE; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* arg2 used as chip number or pin number */ |
| 236 | if (argc > 2) |
| 237 | ul_arg2 = simple_strtoul(argv[2], NULL, 16); |
| 238 | |
| 239 | /* arg3 used as pin or invert value */ |
| 240 | if (argc > 3) |
| 241 | ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1; |
| 242 | |
| 243 | switch ((int)c->cmd) { |
| 244 | #ifdef CONFIG_CMD_PCA953X_INFO |
| 245 | case PCA953X_CMD_INFO: |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 246 | ret = pca953x_info(chip); |
| 247 | if (ret) |
| 248 | ret = CMD_RET_FAILURE; |
| 249 | break; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 250 | #endif |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 251 | |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 252 | case PCA953X_CMD_DEVICE: |
| 253 | if (argc == 3) |
| 254 | chip = (uint8_t)ul_arg2; |
| 255 | printf("Current device address: 0x%x\n", chip); |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 256 | ret = CMD_RET_SUCCESS; |
| 257 | break; |
| 258 | |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 259 | case PCA953X_CMD_INPUT: |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 260 | ret = pca953x_set_dir(chip, (1 << ul_arg2), |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 261 | PCA953X_DIR_IN << ul_arg2); |
| 262 | val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0; |
| 263 | |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 264 | if (ret) |
| 265 | ret = CMD_RET_FAILURE; |
| 266 | else |
| 267 | printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2, |
| 268 | val); |
| 269 | break; |
| 270 | |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 271 | case PCA953X_CMD_OUTPUT: |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 272 | ret = pca953x_set_dir(chip, (1 << ul_arg2), |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 273 | (PCA953X_DIR_OUT << ul_arg2)); |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 274 | if (!ret) |
| 275 | ret = pca953x_set_val(chip, (1 << ul_arg2), |
| 276 | (ul_arg3 << ul_arg2)); |
| 277 | if (ret) |
| 278 | ret = CMD_RET_FAILURE; |
| 279 | break; |
| 280 | |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 281 | case PCA953X_CMD_INVERT: |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 282 | ret = pca953x_set_pol(chip, (1 << ul_arg2), |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 283 | (ul_arg3 << ul_arg2)); |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 284 | if (ret) |
| 285 | ret = CMD_RET_FAILURE; |
| 286 | break; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 287 | } |
Laurence Withers | 633efe9 | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 288 | |
| 289 | if (ret == CMD_RET_FAILURE) |
| 290 | eprintf("Error talking to chip at 0x%x\n", chip); |
| 291 | |
| 292 | return ret; |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | U_BOOT_CMD( |
| 296 | pca953x, 5, 1, do_pca953x, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 297 | "pca953x gpio access", |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 298 | "device [dev]\n" |
| 299 | " - show or set current device address\n" |
| 300 | #ifdef CONFIG_CMD_PCA953X_INFO |
| 301 | "pca953x info\n" |
| 302 | " - display info for current chip\n" |
| 303 | #endif |
| 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" |
Laurence Withers | d75bc03 | 2012-07-25 03:55:47 +0000 | [diff] [blame] | 308 | "pca953x input pin\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 309 | " - set pin as input and read value" |
Peter Tyser | e92739d | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 310 | ); |
| 311 | |
| 312 | #endif /* CONFIG_CMD_PCA953X */ |