xref: /openbmc/u-boot/drivers/gpio/pca953x.c (revision 23ff8633)
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 = le16_to_cpu(valw);
92 		valw &= ~mask;
93 		valw |= data;
94 		valw = cpu_to_le16(valw);
95 
96 		return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2);
97 	}
98 }
99 
100 static int pca953x_reg_read(uint8_t chip, uint addr, uint *data)
101 {
102 	uint8_t valb;
103 	uint16_t valw;
104 
105 	if (pca953x_ngpio(chip) <= 8) {
106 		if (i2c_read(chip, addr, 1, &valb, 1))
107 			return -1;
108 		*data = (int)valb;
109 	} else {
110 		if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
111 			return -1;
112 		*data = (uint)le16_to_cpu(valw);
113 	}
114 	return 0;
115 }
116 
117 /*
118  * Set output value of IO pins in 'mask' to corresponding value in 'data'
119  * 0 = low, 1 = high
120  */
121 int pca953x_set_val(uint8_t chip, uint mask, uint data)
122 {
123 	return pca953x_reg_write(chip, PCA953X_OUT, mask, data);
124 }
125 
126 /*
127  * Set read polarity of IO pins in 'mask' to corresponding value in 'data'
128  * 0 = read pin value, 1 = read inverted pin value
129  */
130 int pca953x_set_pol(uint8_t chip, uint mask, uint data)
131 {
132 	return pca953x_reg_write(chip, PCA953X_POL, mask, data);
133 }
134 
135 /*
136  * Set direction of IO pins in 'mask' to corresponding value in 'data'
137  * 0 = output, 1 = input
138  */
139 int pca953x_set_dir(uint8_t chip, uint mask, uint data)
140 {
141 	return pca953x_reg_write(chip, PCA953X_CONF, mask, data);
142 }
143 
144 /*
145  * Read current logic level of all IO pins
146  */
147 int pca953x_get_val(uint8_t chip)
148 {
149 	uint val;
150 
151 	if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0)
152 		return -1;
153 
154 	return (int)val;
155 }
156 
157 #ifdef CONFIG_CMD_PCA953X
158 #ifdef CONFIG_CMD_PCA953X_INFO
159 /*
160  * Display pca953x information
161  */
162 static int pca953x_info(uint8_t chip)
163 {
164 	int i;
165 	uint data;
166 	int nr_gpio = pca953x_ngpio(chip);
167 	int msb = nr_gpio - 1;
168 
169 	printf("pca953x@ 0x%x (%d pins):\n\n", chip, nr_gpio);
170 	printf("gpio pins: ");
171 	for (i = msb; i >= 0; i--)
172 		printf("%x", i);
173 	printf("\n");
174 	for (i = 11 + nr_gpio; i > 0; i--)
175 		printf("-");
176 	printf("\n");
177 
178 	if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0)
179 		return -1;
180 	printf("conf:      ");
181 	for (i = msb; i >= 0; i--)
182 		printf("%c", data & (1 << i) ? 'i' : 'o');
183 	printf("\n");
184 
185 	if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0)
186 		return -1;
187 	printf("invert:    ");
188 	for (i = msb; i >= 0; i--)
189 		printf("%c", data & (1 << i) ? '1' : '0');
190 	printf("\n");
191 
192 	if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0)
193 		return -1;
194 	printf("input:     ");
195 	for (i = msb; i >= 0; i--)
196 		printf("%c", data & (1 << i) ? '1' : '0');
197 	printf("\n");
198 
199 	if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0)
200 		return -1;
201 	printf("output:    ");
202 	for (i = msb; i >= 0; i--)
203 		printf("%c", data & (1 << i) ? '1' : '0');
204 	printf("\n");
205 
206 	return 0;
207 }
208 #endif /* CONFIG_CMD_PCA953X_INFO */
209 
210 cmd_tbl_t cmd_pca953x[] = {
211 	U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""),
212 	U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""),
213 	U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""),
214 	U_BOOT_CMD_MKENT(invert, 4, 0, (void *)PCA953X_CMD_INVERT, "", ""),
215 #ifdef CONFIG_CMD_PCA953X_INFO
216 	U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""),
217 #endif
218 };
219 
220 int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
221 {
222 	static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
223 	int ret = CMD_RET_USAGE, val;
224 	ulong ul_arg2 = 0;
225 	ulong ul_arg3 = 0;
226 	cmd_tbl_t *c;
227 
228 	c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x));
229 
230 	/* All commands but "device" require 'maxargs' arguments */
231 	if (!c || !((argc == (c->maxargs)) ||
232 		(((int)c->cmd == PCA953X_CMD_DEVICE) &&
233 		 (argc == (c->maxargs - 1))))) {
234 		return CMD_RET_USAGE;
235 	}
236 
237 	/* arg2 used as chip number or pin number */
238 	if (argc > 2)
239 		ul_arg2 = simple_strtoul(argv[2], NULL, 16);
240 
241 	/* arg3 used as pin or invert value */
242 	if (argc > 3)
243 		ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1;
244 
245 	switch ((int)c->cmd) {
246 #ifdef CONFIG_CMD_PCA953X_INFO
247 	case PCA953X_CMD_INFO:
248 		ret = pca953x_info(chip);
249 		if (ret)
250 			ret = CMD_RET_FAILURE;
251 		break;
252 #endif
253 
254 	case PCA953X_CMD_DEVICE:
255 		if (argc == 3)
256 			chip = (uint8_t)ul_arg2;
257 		printf("Current device address: 0x%x\n", chip);
258 		ret = CMD_RET_SUCCESS;
259 		break;
260 
261 	case PCA953X_CMD_INPUT:
262 		ret = pca953x_set_dir(chip, (1 << ul_arg2),
263 				PCA953X_DIR_IN << ul_arg2);
264 		val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
265 
266 		if (ret)
267 			ret = CMD_RET_FAILURE;
268 		else
269 			printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2,
270 									val);
271 		break;
272 
273 	case PCA953X_CMD_OUTPUT:
274 		ret = pca953x_set_dir(chip, (1 << ul_arg2),
275 				(PCA953X_DIR_OUT << ul_arg2));
276 		if (!ret)
277 			ret = pca953x_set_val(chip, (1 << ul_arg2),
278 						(ul_arg3 << ul_arg2));
279 		if (ret)
280 			ret = CMD_RET_FAILURE;
281 		break;
282 
283 	case PCA953X_CMD_INVERT:
284 		ret = pca953x_set_pol(chip, (1 << ul_arg2),
285 					(ul_arg3 << ul_arg2));
286 		if (ret)
287 			ret = CMD_RET_FAILURE;
288 		break;
289 	}
290 
291 	if (ret == CMD_RET_FAILURE)
292 		eprintf("Error talking to chip at 0x%x\n", chip);
293 
294 	return ret;
295 }
296 
297 U_BOOT_CMD(
298 	pca953x,	5,	1,	do_pca953x,
299 	"pca953x gpio access",
300 	"device [dev]\n"
301 	"	- show or set current device address\n"
302 #ifdef CONFIG_CMD_PCA953X_INFO
303 	"pca953x info\n"
304 	"	- display info for current chip\n"
305 #endif
306 	"pca953x output pin 0|1\n"
307 	"	- set pin as output and drive low or high\n"
308 	"pca953x invert pin 0|1\n"
309 	"	- disable/enable polarity inversion for reads\n"
310 	"pca953x input pin\n"
311 	"	- set pin as input and read value"
312 );
313 
314 #endif /* CONFIG_CMD_PCA953X */
315