blob: 7371cd4a87fdd7b7a64afbb39ed4fd80e402d6cc [file] [log] [blame]
Peter Tysere92739d2008-12-17 16:36:21 -06001/*
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 Packham5dec49c2010-12-19 10:12:13 +000020 * Driver for NXP's 4, 8 and 16 bit I2C gpio expanders (eg pca9537, pca9557,
21 * pca9539, etc)
Peter Tysere92739d2008-12-17 16:36:21 -060022 */
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
33enum {
34 PCA953X_CMD_INFO,
35 PCA953X_CMD_DEVICE,
36 PCA953X_CMD_OUTPUT,
37 PCA953X_CMD_INPUT,
38 PCA953X_CMD_INVERT,
39};
40
Chris Packham5dec49c2010-12-19 10:12:13 +000041#ifdef CONFIG_SYS_I2C_PCA953X_WIDTH
42struct pca953x_chip_ngpio {
43 uint8_t chip;
44 uint8_t ngpio;
45};
46
47static struct pca953x_chip_ngpio pca953x_chip_ngpios[] =
48 CONFIG_SYS_I2C_PCA953X_WIDTH;
49
Chris Packham5dec49c2010-12-19 10:12:13 +000050/*
51 * Determine the number of GPIO pins supported. If we don't know we assume
52 * 8 pins.
53 */
54static int pca953x_ngpio(uint8_t chip)
55{
56 int i;
57
Axel Linf2187612013-06-22 18:22:48 +080058 for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++)
Chris Packham5dec49c2010-12-19 10:12:13 +000059 if (pca953x_chip_ngpios[i].chip == chip)
60 return pca953x_chip_ngpios[i].ngpio;
61
62 return 8;
63}
64#else
65static int pca953x_ngpio(uint8_t chip)
66{
67 return 8;
68}
69#endif
70
Peter Tysere92739d2008-12-17 16:36:21 -060071/*
72 * Modify masked bits in register
73 */
74static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data)
75{
Chris Packham5dec49c2010-12-19 10:12:13 +000076 uint8_t valb;
77 uint16_t valw;
Peter Tysere92739d2008-12-17 16:36:21 -060078
Chris Packham5dec49c2010-12-19 10:12:13 +000079 if (pca953x_ngpio(chip) <= 8) {
80 if (i2c_read(chip, addr, 1, &valb, 1))
81 return -1;
Peter Tysere92739d2008-12-17 16:36:21 -060082
Chris Packham5dec49c2010-12-19 10:12:13 +000083 valb &= ~mask;
84 valb |= data;
Peter Tysere92739d2008-12-17 16:36:21 -060085
Chris Packham5dec49c2010-12-19 10:12:13 +000086 return i2c_write(chip, addr, 1, &valb, 1);
87 } else {
88 if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
89 return -1;
90
91 valw &= ~mask;
92 valw |= data;
93
94 return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2);
95 }
96}
97
98static int pca953x_reg_read(uint8_t chip, uint addr, uint *data)
99{
100 uint8_t valb;
101 uint16_t valw;
102
103 if (pca953x_ngpio(chip) <= 8) {
104 if (i2c_read(chip, addr, 1, &valb, 1))
105 return -1;
106 *data = (int)valb;
107 } else {
108 if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
109 return -1;
110 *data = (int)valw;
111 }
112 return 0;
Peter Tysere92739d2008-12-17 16:36:21 -0600113}
114
115/*
116 * Set output value of IO pins in 'mask' to corresponding value in 'data'
117 * 0 = low, 1 = high
118 */
119int pca953x_set_val(uint8_t chip, uint mask, uint data)
120{
121 return pca953x_reg_write(chip, PCA953X_OUT, mask, data);
122}
123
124/*
125 * Set read polarity of IO pins in 'mask' to corresponding value in 'data'
126 * 0 = read pin value, 1 = read inverted pin value
127 */
128int pca953x_set_pol(uint8_t chip, uint mask, uint data)
129{
130 return pca953x_reg_write(chip, PCA953X_POL, mask, data);
131}
132
133/*
134 * Set direction of IO pins in 'mask' to corresponding value in 'data'
135 * 0 = output, 1 = input
136 */
137int pca953x_set_dir(uint8_t chip, uint mask, uint data)
138{
139 return pca953x_reg_write(chip, PCA953X_CONF, mask, data);
140}
141
142/*
143 * Read current logic level of all IO pins
144 */
145int pca953x_get_val(uint8_t chip)
146{
Chris Packham5dec49c2010-12-19 10:12:13 +0000147 uint val;
Peter Tysere92739d2008-12-17 16:36:21 -0600148
Chris Packham5dec49c2010-12-19 10:12:13 +0000149 if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0)
Peter Tysere92739d2008-12-17 16:36:21 -0600150 return -1;
151
152 return (int)val;
153}
154
155#ifdef CONFIG_CMD_PCA953X
156#ifdef CONFIG_CMD_PCA953X_INFO
157/*
158 * Display pca953x information
159 */
160static int pca953x_info(uint8_t chip)
161{
162 int i;
Chris Packham5dec49c2010-12-19 10:12:13 +0000163 uint data;
164 int nr_gpio = pca953x_ngpio(chip);
165 int msb = nr_gpio - 1;
Peter Tysere92739d2008-12-17 16:36:21 -0600166
Chris Packham5dec49c2010-12-19 10:12:13 +0000167 printf("pca953x@ 0x%x (%d pins):\n\n", chip, nr_gpio);
168 printf("gpio pins: ");
169 for (i = msb; i >= 0; i--)
170 printf("%x", i);
171 printf("\n");
172 for (i = 11 + nr_gpio; i > 0; i--)
173 printf("-");
174 printf("\n");
Peter Tysere92739d2008-12-17 16:36:21 -0600175
Chris Packham5dec49c2010-12-19 10:12:13 +0000176 if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0)
Peter Tysere92739d2008-12-17 16:36:21 -0600177 return -1;
178 printf("conf: ");
Chris Packham5dec49c2010-12-19 10:12:13 +0000179 for (i = msb; i >= 0; i--)
Peter Tysere92739d2008-12-17 16:36:21 -0600180 printf("%c", data & (1 << i) ? 'i' : 'o');
181 printf("\n");
182
Chris Packham5dec49c2010-12-19 10:12:13 +0000183 if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0)
Peter Tysere92739d2008-12-17 16:36:21 -0600184 return -1;
185 printf("invert: ");
Chris Packham5dec49c2010-12-19 10:12:13 +0000186 for (i = msb; i >= 0; i--)
Peter Tysere92739d2008-12-17 16:36:21 -0600187 printf("%c", data & (1 << i) ? '1' : '0');
188 printf("\n");
189
Chris Packham5dec49c2010-12-19 10:12:13 +0000190 if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0)
Peter Tysere92739d2008-12-17 16:36:21 -0600191 return -1;
192 printf("input: ");
Chris Packham5dec49c2010-12-19 10:12:13 +0000193 for (i = msb; i >= 0; i--)
Peter Tysere92739d2008-12-17 16:36:21 -0600194 printf("%c", data & (1 << i) ? '1' : '0');
195 printf("\n");
196
Chris Packham5dec49c2010-12-19 10:12:13 +0000197 if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0)
Peter Tysere92739d2008-12-17 16:36:21 -0600198 return -1;
199 printf("output: ");
Chris Packham5dec49c2010-12-19 10:12:13 +0000200 for (i = msb; i >= 0; i--)
Peter Tysere92739d2008-12-17 16:36:21 -0600201 printf("%c", data & (1 << i) ? '1' : '0');
202 printf("\n");
203
204 return 0;
205}
206#endif /* CONFIG_CMD_PCA953X_INFO */
207
208cmd_tbl_t cmd_pca953x[] = {
209 U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""),
210 U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""),
211 U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""),
212 U_BOOT_CMD_MKENT(invert, 4, 0, (void *)PCA953X_CMD_INVERT, "", ""),
213#ifdef CONFIG_CMD_PCA953X_INFO
214 U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""),
215#endif
216};
217
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200218int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tysere92739d2008-12-17 16:36:21 -0600219{
220 static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
Laurence Withers633efe92012-07-25 03:55:48 +0000221 int ret = CMD_RET_USAGE, val;
Peter Tysere92739d2008-12-17 16:36:21 -0600222 ulong ul_arg2 = 0;
223 ulong ul_arg3 = 0;
224 cmd_tbl_t *c;
225
226 c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x));
227
228 /* All commands but "device" require 'maxargs' arguments */
229 if (!c || !((argc == (c->maxargs)) ||
230 (((int)c->cmd == PCA953X_CMD_DEVICE) &&
231 (argc == (c->maxargs - 1))))) {
Laurence Withers633efe92012-07-25 03:55:48 +0000232 return CMD_RET_USAGE;
Peter Tysere92739d2008-12-17 16:36:21 -0600233 }
234
235 /* arg2 used as chip number or pin number */
236 if (argc > 2)
237 ul_arg2 = simple_strtoul(argv[2], NULL, 16);
238
239 /* arg3 used as pin or invert value */
240 if (argc > 3)
241 ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1;
242
243 switch ((int)c->cmd) {
244#ifdef CONFIG_CMD_PCA953X_INFO
245 case PCA953X_CMD_INFO:
Laurence Withers633efe92012-07-25 03:55:48 +0000246 ret = pca953x_info(chip);
247 if (ret)
248 ret = CMD_RET_FAILURE;
249 break;
Peter Tysere92739d2008-12-17 16:36:21 -0600250#endif
Laurence Withers633efe92012-07-25 03:55:48 +0000251
Peter Tysere92739d2008-12-17 16:36:21 -0600252 case PCA953X_CMD_DEVICE:
253 if (argc == 3)
254 chip = (uint8_t)ul_arg2;
255 printf("Current device address: 0x%x\n", chip);
Laurence Withers633efe92012-07-25 03:55:48 +0000256 ret = CMD_RET_SUCCESS;
257 break;
258
Peter Tysere92739d2008-12-17 16:36:21 -0600259 case PCA953X_CMD_INPUT:
Laurence Withers633efe92012-07-25 03:55:48 +0000260 ret = pca953x_set_dir(chip, (1 << ul_arg2),
Peter Tysere92739d2008-12-17 16:36:21 -0600261 PCA953X_DIR_IN << ul_arg2);
262 val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
263
Laurence Withers633efe92012-07-25 03:55:48 +0000264 if (ret)
265 ret = CMD_RET_FAILURE;
266 else
267 printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2,
268 val);
269 break;
270
Peter Tysere92739d2008-12-17 16:36:21 -0600271 case PCA953X_CMD_OUTPUT:
Laurence Withers633efe92012-07-25 03:55:48 +0000272 ret = pca953x_set_dir(chip, (1 << ul_arg2),
Peter Tysere92739d2008-12-17 16:36:21 -0600273 (PCA953X_DIR_OUT << ul_arg2));
Laurence Withers633efe92012-07-25 03:55:48 +0000274 if (!ret)
275 ret = pca953x_set_val(chip, (1 << ul_arg2),
276 (ul_arg3 << ul_arg2));
277 if (ret)
278 ret = CMD_RET_FAILURE;
279 break;
280
Peter Tysere92739d2008-12-17 16:36:21 -0600281 case PCA953X_CMD_INVERT:
Laurence Withers633efe92012-07-25 03:55:48 +0000282 ret = pca953x_set_pol(chip, (1 << ul_arg2),
Peter Tysere92739d2008-12-17 16:36:21 -0600283 (ul_arg3 << ul_arg2));
Laurence Withers633efe92012-07-25 03:55:48 +0000284 if (ret)
285 ret = CMD_RET_FAILURE;
286 break;
Peter Tysere92739d2008-12-17 16:36:21 -0600287 }
Laurence Withers633efe92012-07-25 03:55:48 +0000288
289 if (ret == CMD_RET_FAILURE)
290 eprintf("Error talking to chip at 0x%x\n", chip);
291
292 return ret;
Peter Tysere92739d2008-12-17 16:36:21 -0600293}
294
295U_BOOT_CMD(
296 pca953x, 5, 1, do_pca953x,
Peter Tyser2fb26042009-01-27 18:03:12 -0600297 "pca953x gpio access",
Peter Tysere92739d2008-12-17 16:36:21 -0600298 "device [dev]\n"
299 " - show or set current device address\n"
300#ifdef CONFIG_CMD_PCA953X_INFO
301 "pca953x info\n"
302 " - display info for current chip\n"
303#endif
304 "pca953x output pin 0|1\n"
305 " - set pin as output and drive low or high\n"
306 "pca953x invert pin 0|1\n"
307 " - disable/enable polarity inversion for reads\n"
Laurence Withersd75bc032012-07-25 03:55:47 +0000308 "pca953x input pin\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200309 " - set pin as input and read value"
Peter Tysere92739d2008-12-17 16:36:21 -0600310);
311
312#endif /* CONFIG_CMD_PCA953X */