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