blob: c8c863765b6db72b86c2e6c468c518c3168b259f [file] [log] [blame]
Peter Tyser9902e422008-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 Packham9b383202010-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 Tyser9902e422008-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 Packham9b383202010-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 Packham9b383202010-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 Line9770432013-06-22 18:22:48 +080058 for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++)
Chris Packham9b383202010-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 Tyser9902e422008-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 Packham9b383202010-12-19 10:12:13 +000076 uint8_t valb;
77 uint16_t valw;
Peter Tyser9902e422008-12-17 16:36:21 -060078
Chris Packham9b383202010-12-19 10:12:13 +000079 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;
Peter Tyser9902e422008-12-17 16:36:21 -060090
Dirk Eibach4f515fb2015-10-29 13:51:27 +010091 valw = le16_to_cpu(valw);
Chris Packham9b383202010-12-19 10:12:13 +000092 valw &= ~mask;
93 valw |= data;
Dirk Eibach4f515fb2015-10-29 13:51:27 +010094 valw = cpu_to_le16(valw);
Peter Tyser9902e422008-12-17 16:36:21 -060095
Chris Packham9b383202010-12-19 10:12:13 +000096 return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2);
97 }
Peter Tyser9902e422008-12-17 16:36:21 -060098}
99
Chris Packham9b383202010-12-19 10:12:13 +0000100static 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;
Dirk Eibach4f515fb2015-10-29 13:51:27 +0100112 *data = (uint)le16_to_cpu(valw);
Chris Packham9b383202010-12-19 10:12:13 +0000113 }
114 return 0;
115}
116
Peter Tyser9902e422008-12-17 16:36:21 -0600117/*
118 * Set output value of IO pins in 'mask' to corresponding value in 'data'
119 * 0 = low, 1 = high
120 */
121int 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 */
130int 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 */
139int 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 */
147int pca953x_get_val(uint8_t chip)
148{
Chris Packham9b383202010-12-19 10:12:13 +0000149 uint val;
Peter Tyser9902e422008-12-17 16:36:21 -0600150
Chris Packham9b383202010-12-19 10:12:13 +0000151 if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0)
Peter Tyser9902e422008-12-17 16:36:21 -0600152 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 */
162static int pca953x_info(uint8_t chip)
163{
164 int i;
Chris Packham9b383202010-12-19 10:12:13 +0000165 uint data;
166 int nr_gpio = pca953x_ngpio(chip);
167 int msb = nr_gpio - 1;
Peter Tyser9902e422008-12-17 16:36:21 -0600168
Chris Packham9b383202010-12-19 10:12:13 +0000169 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");
Peter Tyser9902e422008-12-17 16:36:21 -0600177
Chris Packham9b383202010-12-19 10:12:13 +0000178 if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0)
Peter Tyser9902e422008-12-17 16:36:21 -0600179 return -1;
180 printf("conf: ");
Chris Packham9b383202010-12-19 10:12:13 +0000181 for (i = msb; i >= 0; i--)
Peter Tyser9902e422008-12-17 16:36:21 -0600182 printf("%c", data & (1 << i) ? 'i' : 'o');
183 printf("\n");
184
Chris Packham9b383202010-12-19 10:12:13 +0000185 if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0)
Peter Tyser9902e422008-12-17 16:36:21 -0600186 return -1;
187 printf("invert: ");
Chris Packham9b383202010-12-19 10:12:13 +0000188 for (i = msb; i >= 0; i--)
Peter Tyser9902e422008-12-17 16:36:21 -0600189 printf("%c", data & (1 << i) ? '1' : '0');
190 printf("\n");
191
Chris Packham9b383202010-12-19 10:12:13 +0000192 if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0)
Peter Tyser9902e422008-12-17 16:36:21 -0600193 return -1;
194 printf("input: ");
Chris Packham9b383202010-12-19 10:12:13 +0000195 for (i = msb; i >= 0; i--)
Peter Tyser9902e422008-12-17 16:36:21 -0600196 printf("%c", data & (1 << i) ? '1' : '0');
197 printf("\n");
198
Chris Packham9b383202010-12-19 10:12:13 +0000199 if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0)
Peter Tyser9902e422008-12-17 16:36:21 -0600200 return -1;
201 printf("output: ");
Chris Packham9b383202010-12-19 10:12:13 +0000202 for (i = msb; i >= 0; i--)
Peter Tyser9902e422008-12-17 16:36:21 -0600203 printf("%c", data & (1 << i) ? '1' : '0');
204 printf("\n");
205
206 return 0;
207}
208#endif /* CONFIG_CMD_PCA953X_INFO */
209
210cmd_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
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200220int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tyser9902e422008-12-17 16:36:21 -0600221{
222 static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
Laurence Withers46a409d2012-07-25 03:55:48 +0000223 int ret = CMD_RET_USAGE, val;
Peter Tyser9902e422008-12-17 16:36:21 -0600224 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))))) {
Laurence Withers46a409d2012-07-25 03:55:48 +0000234 return CMD_RET_USAGE;
Peter Tyser9902e422008-12-17 16:36:21 -0600235 }
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:
Laurence Withers46a409d2012-07-25 03:55:48 +0000248 ret = pca953x_info(chip);
249 if (ret)
250 ret = CMD_RET_FAILURE;
251 break;
Peter Tyser9902e422008-12-17 16:36:21 -0600252#endif
Laurence Withers46a409d2012-07-25 03:55:48 +0000253
Peter Tyser9902e422008-12-17 16:36:21 -0600254 case PCA953X_CMD_DEVICE:
255 if (argc == 3)
256 chip = (uint8_t)ul_arg2;
257 printf("Current device address: 0x%x\n", chip);
Laurence Withers46a409d2012-07-25 03:55:48 +0000258 ret = CMD_RET_SUCCESS;
259 break;
260
Peter Tyser9902e422008-12-17 16:36:21 -0600261 case PCA953X_CMD_INPUT:
Laurence Withers46a409d2012-07-25 03:55:48 +0000262 ret = pca953x_set_dir(chip, (1 << ul_arg2),
Peter Tyser9902e422008-12-17 16:36:21 -0600263 PCA953X_DIR_IN << ul_arg2);
264 val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
265
Laurence Withers46a409d2012-07-25 03:55:48 +0000266 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
Peter Tyser9902e422008-12-17 16:36:21 -0600273 case PCA953X_CMD_OUTPUT:
Laurence Withers46a409d2012-07-25 03:55:48 +0000274 ret = pca953x_set_dir(chip, (1 << ul_arg2),
Peter Tyser9902e422008-12-17 16:36:21 -0600275 (PCA953X_DIR_OUT << ul_arg2));
Laurence Withers46a409d2012-07-25 03:55:48 +0000276 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
Peter Tyser9902e422008-12-17 16:36:21 -0600283 case PCA953X_CMD_INVERT:
Laurence Withers46a409d2012-07-25 03:55:48 +0000284 ret = pca953x_set_pol(chip, (1 << ul_arg2),
Peter Tyser9902e422008-12-17 16:36:21 -0600285 (ul_arg3 << ul_arg2));
Laurence Withers46a409d2012-07-25 03:55:48 +0000286 if (ret)
287 ret = CMD_RET_FAILURE;
288 break;
Peter Tyser9902e422008-12-17 16:36:21 -0600289 }
Laurence Withers46a409d2012-07-25 03:55:48 +0000290
291 if (ret == CMD_RET_FAILURE)
292 eprintf("Error talking to chip at 0x%x\n", chip);
293
294 return ret;
Peter Tyser9902e422008-12-17 16:36:21 -0600295}
296
297U_BOOT_CMD(
298 pca953x, 5, 1, do_pca953x,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600299 "pca953x gpio access",
Peter Tyser9902e422008-12-17 16:36:21 -0600300 "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"
Laurence Withers7a63c652012-07-25 03:55:47 +0000310 "pca953x input pin\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200311 " - set pin as input and read value"
Peter Tyser9902e422008-12-17 16:36:21 -0600312);
313
314#endif /* CONFIG_CMD_PCA953X */