]>
Commit | Line | Data |
---|---|---|
e92739d3 PT |
1 | /* |
2 | * Copyright 2008 Extreme Engineering Solutions, Inc. | |
3 | * | |
5b8031cc | 4 | * SPDX-License-Identifier: GPL-2.0 |
e92739d3 PT |
5 | */ |
6 | ||
7 | /* | |
5dec49ca CP |
8 | * Driver for NXP's 4, 8 and 16 bit I2C gpio expanders (eg pca9537, pca9557, |
9 | * pca9539, etc) | |
e92739d3 PT |
10 | */ |
11 | ||
12 | #include <common.h> | |
13 | #include <i2c.h> | |
14 | #include <pca953x.h> | |
15 | ||
16 | /* Default to an address that hopefully won't corrupt other i2c devices */ | |
17 | #ifndef CONFIG_SYS_I2C_PCA953X_ADDR | |
18 | #define CONFIG_SYS_I2C_PCA953X_ADDR (~0) | |
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 | ||
5dec49ca CP |
29 | #ifdef CONFIG_SYS_I2C_PCA953X_WIDTH |
30 | struct pca953x_chip_ngpio { | |
31 | uint8_t chip; | |
32 | uint8_t ngpio; | |
33 | }; | |
34 | ||
35 | static struct pca953x_chip_ngpio pca953x_chip_ngpios[] = | |
36 | CONFIG_SYS_I2C_PCA953X_WIDTH; | |
37 | ||
5dec49ca CP |
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 | ||
f2187617 | 46 | for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++) |
5dec49ca CP |
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 | ||
e92739d3 PT |
59 | /* |
60 | * Modify masked bits in register | |
61 | */ | |
62 | static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data) | |
63 | { | |
5dec49ca CP |
64 | uint8_t valb; |
65 | uint16_t valw; | |
e92739d3 | 66 | |
5dec49ca CP |
67 | if (pca953x_ngpio(chip) <= 8) { |
68 | if (i2c_read(chip, addr, 1, &valb, 1)) | |
69 | return -1; | |
70 | ||
71 | valb &= ~mask; | |
72 | valb |= data; | |
73 | ||
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 | ||
daa75b34 | 79 | valw = le16_to_cpu(valw); |
5dec49ca CP |
80 | valw &= ~mask; |
81 | valw |= data; | |
daa75b34 | 82 | valw = cpu_to_le16(valw); |
e92739d3 | 83 | |
5dec49ca CP |
84 | return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2); |
85 | } | |
86 | } | |
e92739d3 | 87 | |
5dec49ca CP |
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; | |
daa75b34 | 100 | *data = (uint)le16_to_cpu(valw); |
5dec49ca CP |
101 | } |
102 | return 0; | |
e92739d3 PT |
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 | { | |
5dec49ca | 137 | uint val; |
e92739d3 | 138 | |
5dec49ca | 139 | if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0) |
e92739d3 PT |
140 | return -1; |
141 | ||
142 | return (int)val; | |
143 | } | |
144 | ||
0d097b77 | 145 | #if defined(CONFIG_CMD_PCA953X) && !defined(CONFIG_SPL_BUILD) |
e92739d3 PT |
146 | /* |
147 | * Display pca953x information | |
148 | */ | |
149 | static int pca953x_info(uint8_t chip) | |
150 | { | |
151 | int i; | |
5dec49ca CP |
152 | uint data; |
153 | int nr_gpio = pca953x_ngpio(chip); | |
154 | int msb = nr_gpio - 1; | |
155 | ||
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"); | |
e92739d3 | 164 | |
5dec49ca | 165 | if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0) |
e92739d3 PT |
166 | return -1; |
167 | printf("conf: "); | |
5dec49ca | 168 | for (i = msb; i >= 0; i--) |
e92739d3 PT |
169 | printf("%c", data & (1 << i) ? 'i' : 'o'); |
170 | printf("\n"); | |
171 | ||
5dec49ca | 172 | if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0) |
e92739d3 PT |
173 | return -1; |
174 | printf("invert: "); | |
5dec49ca | 175 | for (i = msb; i >= 0; i--) |
e92739d3 PT |
176 | printf("%c", data & (1 << i) ? '1' : '0'); |
177 | printf("\n"); | |
178 | ||
5dec49ca | 179 | if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0) |
e92739d3 PT |
180 | return -1; |
181 | printf("input: "); | |
5dec49ca | 182 | for (i = msb; i >= 0; i--) |
e92739d3 PT |
183 | printf("%c", data & (1 << i) ? '1' : '0'); |
184 | printf("\n"); | |
185 | ||
5dec49ca | 186 | if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0) |
e92739d3 PT |
187 | return -1; |
188 | printf("output: "); | |
5dec49ca | 189 | for (i = msb; i >= 0; i--) |
e92739d3 PT |
190 | printf("%c", data & (1 << i) ? '1' : '0'); |
191 | printf("\n"); | |
192 | ||
193 | return 0; | |
194 | } | |
e92739d3 | 195 | |
0d097b77 | 196 | static cmd_tbl_t cmd_pca953x[] = { |
e92739d3 PT |
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, "", ""), | |
e92739d3 | 201 | U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""), |
e92739d3 PT |
202 | }; |
203 | ||
0d097b77 | 204 | static int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
e92739d3 PT |
205 | { |
206 | static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR; | |
633efe9c | 207 | int ret = CMD_RET_USAGE, val; |
e92739d3 PT |
208 | ulong ul_arg2 = 0; |
209 | ulong ul_arg3 = 0; | |
210 | cmd_tbl_t *c; | |
211 | ||
212 | c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x)); | |
213 | ||
214 | /* All commands but "device" require 'maxargs' arguments */ | |
215 | if (!c || !((argc == (c->maxargs)) || | |
01b2a699 | 216 | (((long)c->cmd == PCA953X_CMD_DEVICE) && |
e92739d3 | 217 | (argc == (c->maxargs - 1))))) { |
633efe9c | 218 | return CMD_RET_USAGE; |
e92739d3 PT |
219 | } |
220 | ||
221 | /* arg2 used as chip number or pin number */ | |
222 | if (argc > 2) | |
223 | ul_arg2 = simple_strtoul(argv[2], NULL, 16); | |
224 | ||
225 | /* arg3 used as pin or invert value */ | |
226 | if (argc > 3) | |
227 | ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1; | |
228 | ||
01b2a699 | 229 | switch ((long)c->cmd) { |
e92739d3 | 230 | case PCA953X_CMD_INFO: |
633efe9c LW |
231 | ret = pca953x_info(chip); |
232 | if (ret) | |
233 | ret = CMD_RET_FAILURE; | |
234 | break; | |
633efe9c | 235 | |
e92739d3 PT |
236 | case PCA953X_CMD_DEVICE: |
237 | if (argc == 3) | |
238 | chip = (uint8_t)ul_arg2; | |
239 | printf("Current device address: 0x%x\n", chip); | |
633efe9c LW |
240 | ret = CMD_RET_SUCCESS; |
241 | break; | |
242 | ||
e92739d3 | 243 | case PCA953X_CMD_INPUT: |
633efe9c | 244 | ret = pca953x_set_dir(chip, (1 << ul_arg2), |
e92739d3 PT |
245 | PCA953X_DIR_IN << ul_arg2); |
246 | val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0; | |
247 | ||
633efe9c LW |
248 | if (ret) |
249 | ret = CMD_RET_FAILURE; | |
250 | else | |
251 | printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2, | |
252 | val); | |
253 | break; | |
254 | ||
e92739d3 | 255 | case PCA953X_CMD_OUTPUT: |
633efe9c | 256 | ret = pca953x_set_dir(chip, (1 << ul_arg2), |
e92739d3 | 257 | (PCA953X_DIR_OUT << ul_arg2)); |
633efe9c LW |
258 | if (!ret) |
259 | ret = pca953x_set_val(chip, (1 << ul_arg2), | |
260 | (ul_arg3 << ul_arg2)); | |
261 | if (ret) | |
262 | ret = CMD_RET_FAILURE; | |
263 | break; | |
264 | ||
e92739d3 | 265 | case PCA953X_CMD_INVERT: |
633efe9c | 266 | ret = pca953x_set_pol(chip, (1 << ul_arg2), |
e92739d3 | 267 | (ul_arg3 << ul_arg2)); |
633efe9c LW |
268 | if (ret) |
269 | ret = CMD_RET_FAILURE; | |
270 | break; | |
e92739d3 | 271 | } |
633efe9c LW |
272 | |
273 | if (ret == CMD_RET_FAILURE) | |
274 | eprintf("Error talking to chip at 0x%x\n", chip); | |
275 | ||
276 | return ret; | |
e92739d3 PT |
277 | } |
278 | ||
279 | U_BOOT_CMD( | |
280 | pca953x, 5, 1, do_pca953x, | |
2fb2604d | 281 | "pca953x gpio access", |
e92739d3 PT |
282 | "device [dev]\n" |
283 | " - show or set current device address\n" | |
e92739d3 PT |
284 | "pca953x info\n" |
285 | " - display info for current chip\n" | |
e92739d3 PT |
286 | "pca953x output pin 0|1\n" |
287 | " - set pin as output and drive low or high\n" | |
288 | "pca953x invert pin 0|1\n" | |
289 | " - disable/enable polarity inversion for reads\n" | |
d75bc03f | 290 | "pca953x input pin\n" |
a89c33db | 291 | " - set pin as input and read value" |
e92739d3 PT |
292 | ); |
293 | ||
294 | #endif /* CONFIG_CMD_PCA953X */ |