xref: /openbmc/u-boot/drivers/gpio/pca953x.c (revision e77e65df)
1 /*
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 /*
20  * Driver for NXP's 4, 8 and 16 bit I2C gpio expanders (eg pca9537, pca9557,
21  * pca9539, etc)
22  */
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 
33 enum {
34 	PCA953X_CMD_INFO,
35 	PCA953X_CMD_DEVICE,
36 	PCA953X_CMD_OUTPUT,
37 	PCA953X_CMD_INPUT,
38 	PCA953X_CMD_INVERT,
39 };
40 
41 #ifdef CONFIG_SYS_I2C_PCA953X_WIDTH
42 struct pca953x_chip_ngpio {
43 	uint8_t chip;
44 	uint8_t ngpio;
45 };
46 
47 static struct pca953x_chip_ngpio pca953x_chip_ngpios[] =
48     CONFIG_SYS_I2C_PCA953X_WIDTH;
49 
50 /*
51  * Determine the number of GPIO pins supported. If we don't know we assume
52  * 8 pins.
53  */
54 static int pca953x_ngpio(uint8_t chip)
55 {
56 	int i;
57 
58 	for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++)
59 		if (pca953x_chip_ngpios[i].chip == chip)
60 			return pca953x_chip_ngpios[i].ngpio;
61 
62 	return 8;
63 }
64 #else
65 static int pca953x_ngpio(uint8_t chip)
66 {
67 	return 8;
68 }
69 #endif
70 
71 /*
72  * Modify masked bits in register
73  */
74 static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data)
75 {
76 	uint8_t valb;
77 	uint16_t valw;
78 
79 	if (pca953x_ngpio(chip) <= 8) {
80 		if (i2c_read(chip, addr, 1, &valb, 1))
81 			return -1;
82 
83 		valb &= ~mask;
84 		valb |= data;
85 
86 		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 
98 static 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;
113 }
114 
115 /*
116  * Set output value of IO pins in 'mask' to corresponding value in 'data'
117  * 0 = low, 1 = high
118  */
119 int 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  */
128 int 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  */
137 int 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  */
145 int pca953x_get_val(uint8_t chip)
146 {
147 	uint val;
148 
149 	if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0)
150 		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  */
160 static int pca953x_info(uint8_t chip)
161 {
162 	int i;
163 	uint data;
164 	int nr_gpio = pca953x_ngpio(chip);
165 	int msb = nr_gpio - 1;
166 
167 	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");
175 
176 	if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0)
177 		return -1;
178 	printf("conf:      ");
179 	for (i = msb; i >= 0; i--)
180 		printf("%c", data & (1 << i) ? 'i' : 'o');
181 	printf("\n");
182 
183 	if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0)
184 		return -1;
185 	printf("invert:    ");
186 	for (i = msb; i >= 0; i--)
187 		printf("%c", data & (1 << i) ? '1' : '0');
188 	printf("\n");
189 
190 	if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0)
191 		return -1;
192 	printf("input:     ");
193 	for (i = msb; i >= 0; i--)
194 		printf("%c", data & (1 << i) ? '1' : '0');
195 	printf("\n");
196 
197 	if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0)
198 		return -1;
199 	printf("output:    ");
200 	for (i = msb; i >= 0; i--)
201 		printf("%c", data & (1 << i) ? '1' : '0');
202 	printf("\n");
203 
204 	return 0;
205 }
206 #endif /* CONFIG_CMD_PCA953X_INFO */
207 
208 cmd_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 
218 int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
219 {
220 	static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
221 	int ret = CMD_RET_USAGE, val;
222 	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))))) {
232 		return CMD_RET_USAGE;
233 	}
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:
246 		ret = pca953x_info(chip);
247 		if (ret)
248 			ret = CMD_RET_FAILURE;
249 		break;
250 #endif
251 
252 	case PCA953X_CMD_DEVICE:
253 		if (argc == 3)
254 			chip = (uint8_t)ul_arg2;
255 		printf("Current device address: 0x%x\n", chip);
256 		ret = CMD_RET_SUCCESS;
257 		break;
258 
259 	case PCA953X_CMD_INPUT:
260 		ret = pca953x_set_dir(chip, (1 << ul_arg2),
261 				PCA953X_DIR_IN << ul_arg2);
262 		val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
263 
264 		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 
271 	case PCA953X_CMD_OUTPUT:
272 		ret = pca953x_set_dir(chip, (1 << ul_arg2),
273 				(PCA953X_DIR_OUT << ul_arg2));
274 		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 
281 	case PCA953X_CMD_INVERT:
282 		ret = pca953x_set_pol(chip, (1 << ul_arg2),
283 					(ul_arg3 << ul_arg2));
284 		if (ret)
285 			ret = CMD_RET_FAILURE;
286 		break;
287 	}
288 
289 	if (ret == CMD_RET_FAILURE)
290 		eprintf("Error talking to chip at 0x%x\n", chip);
291 
292 	return ret;
293 }
294 
295 U_BOOT_CMD(
296 	pca953x,	5,	1,	do_pca953x,
297 	"pca953x gpio access",
298 	"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"
308 	"pca953x input pin\n"
309 	"	- set pin as input and read value"
310 );
311 
312 #endif /* CONFIG_CMD_PCA953X */
313