Peter Tyser | 9902e42 | 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 | 9b38320 | 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 | 9902e42 | 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 | 9b38320 | 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 | 9b38320 | 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 | e977043 | 2013-06-22 18:22:48 +0800 | [diff] [blame] | 58 | for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++) |
Chris Packham | 9b38320 | 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 | 9902e42 | 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 | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 76 | uint8_t valb; |
| 77 | uint16_t valw; |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 78 | |
Chris Packham | 9b38320 | 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; |
| 82 | |
| 83 | valb &= ~mask; |
| 84 | valb |= data; |
| 85 | |
| 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; |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 90 | |
Dirk Eibach | 4f515fb | 2015-10-29 13:51:27 +0100 | [diff] [blame] | 91 | valw = le16_to_cpu(valw); |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 92 | valw &= ~mask; |
| 93 | valw |= data; |
Dirk Eibach | 4f515fb | 2015-10-29 13:51:27 +0100 | [diff] [blame] | 94 | valw = cpu_to_le16(valw); |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 95 | |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 96 | return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2); |
| 97 | } |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 98 | } |
| 99 | |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 100 | static int pca953x_reg_read(uint8_t chip, uint addr, uint *data) |
| 101 | { |
| 102 | uint8_t valb; |
| 103 | uint16_t valw; |
| 104 | |
| 105 | if (pca953x_ngpio(chip) <= 8) { |
| 106 | if (i2c_read(chip, addr, 1, &valb, 1)) |
| 107 | return -1; |
| 108 | *data = (int)valb; |
| 109 | } else { |
| 110 | if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2)) |
| 111 | return -1; |
Dirk Eibach | 4f515fb | 2015-10-29 13:51:27 +0100 | [diff] [blame] | 112 | *data = (uint)le16_to_cpu(valw); |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 117 | /* |
| 118 | * Set output value of IO pins in 'mask' to corresponding value in 'data' |
| 119 | * 0 = low, 1 = high |
| 120 | */ |
| 121 | int pca953x_set_val(uint8_t chip, uint mask, uint data) |
| 122 | { |
| 123 | return pca953x_reg_write(chip, PCA953X_OUT, mask, data); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Set read polarity of IO pins in 'mask' to corresponding value in 'data' |
| 128 | * 0 = read pin value, 1 = read inverted pin value |
| 129 | */ |
| 130 | int pca953x_set_pol(uint8_t chip, uint mask, uint data) |
| 131 | { |
| 132 | return pca953x_reg_write(chip, PCA953X_POL, mask, data); |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Set direction of IO pins in 'mask' to corresponding value in 'data' |
| 137 | * 0 = output, 1 = input |
| 138 | */ |
| 139 | int pca953x_set_dir(uint8_t chip, uint mask, uint data) |
| 140 | { |
| 141 | return pca953x_reg_write(chip, PCA953X_CONF, mask, data); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Read current logic level of all IO pins |
| 146 | */ |
| 147 | int pca953x_get_val(uint8_t chip) |
| 148 | { |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 149 | uint val; |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 150 | |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 151 | if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0) |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 152 | return -1; |
| 153 | |
| 154 | return (int)val; |
| 155 | } |
| 156 | |
| 157 | #ifdef CONFIG_CMD_PCA953X |
| 158 | #ifdef CONFIG_CMD_PCA953X_INFO |
| 159 | /* |
| 160 | * Display pca953x information |
| 161 | */ |
| 162 | static int pca953x_info(uint8_t chip) |
| 163 | { |
| 164 | int i; |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 165 | uint data; |
| 166 | int nr_gpio = pca953x_ngpio(chip); |
| 167 | int msb = nr_gpio - 1; |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 168 | |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 169 | printf("pca953x@ 0x%x (%d pins):\n\n", chip, nr_gpio); |
| 170 | printf("gpio pins: "); |
| 171 | for (i = msb; i >= 0; i--) |
| 172 | printf("%x", i); |
| 173 | printf("\n"); |
| 174 | for (i = 11 + nr_gpio; i > 0; i--) |
| 175 | printf("-"); |
| 176 | printf("\n"); |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 177 | |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 178 | if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0) |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 179 | return -1; |
| 180 | printf("conf: "); |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 181 | for (i = msb; i >= 0; i--) |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 182 | printf("%c", data & (1 << i) ? 'i' : 'o'); |
| 183 | printf("\n"); |
| 184 | |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 185 | if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0) |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 186 | return -1; |
| 187 | printf("invert: "); |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 188 | for (i = msb; i >= 0; i--) |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 189 | printf("%c", data & (1 << i) ? '1' : '0'); |
| 190 | printf("\n"); |
| 191 | |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 192 | if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0) |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 193 | return -1; |
| 194 | printf("input: "); |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 195 | for (i = msb; i >= 0; i--) |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 196 | printf("%c", data & (1 << i) ? '1' : '0'); |
| 197 | printf("\n"); |
| 198 | |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 199 | if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0) |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 200 | return -1; |
| 201 | printf("output: "); |
Chris Packham | 9b38320 | 2010-12-19 10:12:13 +0000 | [diff] [blame] | 202 | for (i = msb; i >= 0; i--) |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 203 | printf("%c", data & (1 << i) ? '1' : '0'); |
| 204 | printf("\n"); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | #endif /* CONFIG_CMD_PCA953X_INFO */ |
| 209 | |
| 210 | cmd_tbl_t cmd_pca953x[] = { |
| 211 | U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""), |
| 212 | U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""), |
| 213 | U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""), |
| 214 | U_BOOT_CMD_MKENT(invert, 4, 0, (void *)PCA953X_CMD_INVERT, "", ""), |
| 215 | #ifdef CONFIG_CMD_PCA953X_INFO |
| 216 | U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""), |
| 217 | #endif |
| 218 | }; |
| 219 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 220 | int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 221 | { |
| 222 | static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR; |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 223 | int ret = CMD_RET_USAGE, val; |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 224 | ulong ul_arg2 = 0; |
| 225 | ulong ul_arg3 = 0; |
| 226 | cmd_tbl_t *c; |
| 227 | |
| 228 | c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x)); |
| 229 | |
| 230 | /* All commands but "device" require 'maxargs' arguments */ |
| 231 | if (!c || !((argc == (c->maxargs)) || |
| 232 | (((int)c->cmd == PCA953X_CMD_DEVICE) && |
| 233 | (argc == (c->maxargs - 1))))) { |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 234 | return CMD_RET_USAGE; |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* arg2 used as chip number or pin number */ |
| 238 | if (argc > 2) |
| 239 | ul_arg2 = simple_strtoul(argv[2], NULL, 16); |
| 240 | |
| 241 | /* arg3 used as pin or invert value */ |
| 242 | if (argc > 3) |
| 243 | ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1; |
| 244 | |
| 245 | switch ((int)c->cmd) { |
| 246 | #ifdef CONFIG_CMD_PCA953X_INFO |
| 247 | case PCA953X_CMD_INFO: |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 248 | ret = pca953x_info(chip); |
| 249 | if (ret) |
| 250 | ret = CMD_RET_FAILURE; |
| 251 | break; |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 252 | #endif |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 253 | |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 254 | case PCA953X_CMD_DEVICE: |
| 255 | if (argc == 3) |
| 256 | chip = (uint8_t)ul_arg2; |
| 257 | printf("Current device address: 0x%x\n", chip); |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 258 | ret = CMD_RET_SUCCESS; |
| 259 | break; |
| 260 | |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 261 | case PCA953X_CMD_INPUT: |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 262 | ret = pca953x_set_dir(chip, (1 << ul_arg2), |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 263 | PCA953X_DIR_IN << ul_arg2); |
| 264 | val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0; |
| 265 | |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 266 | if (ret) |
| 267 | ret = CMD_RET_FAILURE; |
| 268 | else |
| 269 | printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2, |
| 270 | val); |
| 271 | break; |
| 272 | |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 273 | case PCA953X_CMD_OUTPUT: |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 274 | ret = pca953x_set_dir(chip, (1 << ul_arg2), |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 275 | (PCA953X_DIR_OUT << ul_arg2)); |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 276 | if (!ret) |
| 277 | ret = pca953x_set_val(chip, (1 << ul_arg2), |
| 278 | (ul_arg3 << ul_arg2)); |
| 279 | if (ret) |
| 280 | ret = CMD_RET_FAILURE; |
| 281 | break; |
| 282 | |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 283 | case PCA953X_CMD_INVERT: |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 284 | ret = pca953x_set_pol(chip, (1 << ul_arg2), |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 285 | (ul_arg3 << ul_arg2)); |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 286 | if (ret) |
| 287 | ret = CMD_RET_FAILURE; |
| 288 | break; |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 289 | } |
Laurence Withers | 46a409d | 2012-07-25 03:55:48 +0000 | [diff] [blame] | 290 | |
| 291 | if (ret == CMD_RET_FAILURE) |
| 292 | eprintf("Error talking to chip at 0x%x\n", chip); |
| 293 | |
| 294 | return ret; |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | U_BOOT_CMD( |
| 298 | pca953x, 5, 1, do_pca953x, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 299 | "pca953x gpio access", |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 300 | "device [dev]\n" |
| 301 | " - show or set current device address\n" |
| 302 | #ifdef CONFIG_CMD_PCA953X_INFO |
| 303 | "pca953x info\n" |
| 304 | " - display info for current chip\n" |
| 305 | #endif |
| 306 | "pca953x output pin 0|1\n" |
| 307 | " - set pin as output and drive low or high\n" |
| 308 | "pca953x invert pin 0|1\n" |
| 309 | " - disable/enable polarity inversion for reads\n" |
Laurence Withers | 7a63c65 | 2012-07-25 03:55:47 +0000 | [diff] [blame] | 310 | "pca953x input pin\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 311 | " - set pin as input and read value" |
Peter Tyser | 9902e42 | 2008-12-17 16:36:21 -0600 | [diff] [blame] | 312 | ); |
| 313 | |
| 314 | #endif /* CONFIG_CMD_PCA953X */ |