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