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