blob: fda8054d530872d32816639b98794396d9787797 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Peter Tysere92739d2008-12-17 16:36:21 -06002/*
3 * Copyright 2008 Extreme Engineering Solutions, Inc.
Peter Tysere92739d2008-12-17 16:36:21 -06004 */
5
6/*
Chris Packham5dec49c2010-12-19 10:12:13 +00007 * Driver for NXP's 4, 8 and 16 bit I2C gpio expanders (eg pca9537, pca9557,
8 * pca9539, etc)
Peter Tysere92739d2008-12-17 16:36:21 -06009 */
10
11#include <common.h>
12#include <i2c.h>
13#include <pca953x.h>
14
15/* Default to an address that hopefully won't corrupt other i2c devices */
16#ifndef CONFIG_SYS_I2C_PCA953X_ADDR
17#define CONFIG_SYS_I2C_PCA953X_ADDR (~0)
18#endif
19
20enum {
21 PCA953X_CMD_INFO,
22 PCA953X_CMD_DEVICE,
23 PCA953X_CMD_OUTPUT,
24 PCA953X_CMD_INPUT,
25 PCA953X_CMD_INVERT,
26};
27
Chris Packham5dec49c2010-12-19 10:12:13 +000028#ifdef CONFIG_SYS_I2C_PCA953X_WIDTH
29struct pca953x_chip_ngpio {
30 uint8_t chip;
31 uint8_t ngpio;
32};
33
34static struct pca953x_chip_ngpio pca953x_chip_ngpios[] =
35 CONFIG_SYS_I2C_PCA953X_WIDTH;
36
Chris Packham5dec49c2010-12-19 10:12:13 +000037/*
38 * Determine the number of GPIO pins supported. If we don't know we assume
39 * 8 pins.
40 */
41static int pca953x_ngpio(uint8_t chip)
42{
43 int i;
44
Axel Linf2187612013-06-22 18:22:48 +080045 for (i = 0; i < ARRAY_SIZE(pca953x_chip_ngpios); i++)
Chris Packham5dec49c2010-12-19 10:12:13 +000046 if (pca953x_chip_ngpios[i].chip == chip)
47 return pca953x_chip_ngpios[i].ngpio;
48
49 return 8;
50}
51#else
52static int pca953x_ngpio(uint8_t chip)
53{
54 return 8;
55}
56#endif
57
Peter Tysere92739d2008-12-17 16:36:21 -060058/*
59 * Modify masked bits in register
60 */
61static int pca953x_reg_write(uint8_t chip, uint addr, uint mask, uint data)
62{
Chris Packham5dec49c2010-12-19 10:12:13 +000063 uint8_t valb;
64 uint16_t valw;
Peter Tysere92739d2008-12-17 16:36:21 -060065
Chris Packham5dec49c2010-12-19 10:12:13 +000066 if (pca953x_ngpio(chip) <= 8) {
67 if (i2c_read(chip, addr, 1, &valb, 1))
68 return -1;
Peter Tysere92739d2008-12-17 16:36:21 -060069
Chris Packham5dec49c2010-12-19 10:12:13 +000070 valb &= ~mask;
71 valb |= data;
Peter Tysere92739d2008-12-17 16:36:21 -060072
Chris Packham5dec49c2010-12-19 10:12:13 +000073 return i2c_write(chip, addr, 1, &valb, 1);
74 } else {
75 if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
76 return -1;
77
Dirk Eibachdaa75b32015-10-29 13:51:27 +010078 valw = le16_to_cpu(valw);
Chris Packham5dec49c2010-12-19 10:12:13 +000079 valw &= ~mask;
80 valw |= data;
Dirk Eibachdaa75b32015-10-29 13:51:27 +010081 valw = cpu_to_le16(valw);
Chris Packham5dec49c2010-12-19 10:12:13 +000082
83 return i2c_write(chip, addr << 1, 1, (u8*)&valw, 2);
84 }
85}
86
87static int pca953x_reg_read(uint8_t chip, uint addr, uint *data)
88{
89 uint8_t valb;
90 uint16_t valw;
91
92 if (pca953x_ngpio(chip) <= 8) {
93 if (i2c_read(chip, addr, 1, &valb, 1))
94 return -1;
95 *data = (int)valb;
96 } else {
97 if (i2c_read(chip, addr << 1, 1, (u8*)&valw, 2))
98 return -1;
Dirk Eibachdaa75b32015-10-29 13:51:27 +010099 *data = (uint)le16_to_cpu(valw);
Chris Packham5dec49c2010-12-19 10:12:13 +0000100 }
101 return 0;
Peter Tysere92739d2008-12-17 16:36:21 -0600102}
103
104/*
105 * Set output value of IO pins in 'mask' to corresponding value in 'data'
106 * 0 = low, 1 = high
107 */
108int pca953x_set_val(uint8_t chip, uint mask, uint data)
109{
110 return pca953x_reg_write(chip, PCA953X_OUT, mask, data);
111}
112
113/*
114 * Set read polarity of IO pins in 'mask' to corresponding value in 'data'
115 * 0 = read pin value, 1 = read inverted pin value
116 */
117int pca953x_set_pol(uint8_t chip, uint mask, uint data)
118{
119 return pca953x_reg_write(chip, PCA953X_POL, mask, data);
120}
121
122/*
123 * Set direction of IO pins in 'mask' to corresponding value in 'data'
124 * 0 = output, 1 = input
125 */
126int pca953x_set_dir(uint8_t chip, uint mask, uint data)
127{
128 return pca953x_reg_write(chip, PCA953X_CONF, mask, data);
129}
130
131/*
132 * Read current logic level of all IO pins
133 */
134int pca953x_get_val(uint8_t chip)
135{
Chris Packham5dec49c2010-12-19 10:12:13 +0000136 uint val;
Peter Tysere92739d2008-12-17 16:36:21 -0600137
Chris Packham5dec49c2010-12-19 10:12:13 +0000138 if (pca953x_reg_read(chip, PCA953X_IN, &val) < 0)
Peter Tysere92739d2008-12-17 16:36:21 -0600139 return -1;
140
141 return (int)val;
142}
143
Tom Rini0d097b72018-01-03 09:23:14 -0500144#if defined(CONFIG_CMD_PCA953X) && !defined(CONFIG_SPL_BUILD)
Peter Tysere92739d2008-12-17 16:36:21 -0600145/*
146 * Display pca953x information
147 */
148static int pca953x_info(uint8_t chip)
149{
150 int i;
Chris Packham5dec49c2010-12-19 10:12:13 +0000151 uint data;
152 int nr_gpio = pca953x_ngpio(chip);
153 int msb = nr_gpio - 1;
Peter Tysere92739d2008-12-17 16:36:21 -0600154
Chris Packham5dec49c2010-12-19 10:12:13 +0000155 printf("pca953x@ 0x%x (%d pins):\n\n", chip, nr_gpio);
156 printf("gpio pins: ");
157 for (i = msb; i >= 0; i--)
158 printf("%x", i);
159 printf("\n");
160 for (i = 11 + nr_gpio; i > 0; i--)
161 printf("-");
162 printf("\n");
Peter Tysere92739d2008-12-17 16:36:21 -0600163
Chris Packham5dec49c2010-12-19 10:12:13 +0000164 if (pca953x_reg_read(chip, PCA953X_CONF, &data) < 0)
Peter Tysere92739d2008-12-17 16:36:21 -0600165 return -1;
166 printf("conf: ");
Chris Packham5dec49c2010-12-19 10:12:13 +0000167 for (i = msb; i >= 0; i--)
Peter Tysere92739d2008-12-17 16:36:21 -0600168 printf("%c", data & (1 << i) ? 'i' : 'o');
169 printf("\n");
170
Chris Packham5dec49c2010-12-19 10:12:13 +0000171 if (pca953x_reg_read(chip, PCA953X_POL, &data) < 0)
Peter Tysere92739d2008-12-17 16:36:21 -0600172 return -1;
173 printf("invert: ");
Chris Packham5dec49c2010-12-19 10:12:13 +0000174 for (i = msb; i >= 0; i--)
Peter Tysere92739d2008-12-17 16:36:21 -0600175 printf("%c", data & (1 << i) ? '1' : '0');
176 printf("\n");
177
Chris Packham5dec49c2010-12-19 10:12:13 +0000178 if (pca953x_reg_read(chip, PCA953X_IN, &data) < 0)
Peter Tysere92739d2008-12-17 16:36:21 -0600179 return -1;
180 printf("input: ");
Chris Packham5dec49c2010-12-19 10:12:13 +0000181 for (i = msb; i >= 0; i--)
Peter Tysere92739d2008-12-17 16:36:21 -0600182 printf("%c", data & (1 << i) ? '1' : '0');
183 printf("\n");
184
Chris Packham5dec49c2010-12-19 10:12:13 +0000185 if (pca953x_reg_read(chip, PCA953X_OUT, &data) < 0)
Peter Tysere92739d2008-12-17 16:36:21 -0600186 return -1;
187 printf("output: ");
Chris Packham5dec49c2010-12-19 10:12:13 +0000188 for (i = msb; i >= 0; i--)
Peter Tysere92739d2008-12-17 16:36:21 -0600189 printf("%c", data & (1 << i) ? '1' : '0');
190 printf("\n");
191
192 return 0;
193}
Peter Tysere92739d2008-12-17 16:36:21 -0600194
Tom Rini0d097b72018-01-03 09:23:14 -0500195static cmd_tbl_t cmd_pca953x[] = {
Peter Tysere92739d2008-12-17 16:36:21 -0600196 U_BOOT_CMD_MKENT(device, 3, 0, (void *)PCA953X_CMD_DEVICE, "", ""),
197 U_BOOT_CMD_MKENT(output, 4, 0, (void *)PCA953X_CMD_OUTPUT, "", ""),
198 U_BOOT_CMD_MKENT(input, 3, 0, (void *)PCA953X_CMD_INPUT, "", ""),
199 U_BOOT_CMD_MKENT(invert, 4, 0, (void *)PCA953X_CMD_INVERT, "", ""),
Peter Tysere92739d2008-12-17 16:36:21 -0600200 U_BOOT_CMD_MKENT(info, 2, 0, (void *)PCA953X_CMD_INFO, "", ""),
Peter Tysere92739d2008-12-17 16:36:21 -0600201};
202
Tom Rini0d097b72018-01-03 09:23:14 -0500203static int do_pca953x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tysere92739d2008-12-17 16:36:21 -0600204{
205 static uint8_t chip = CONFIG_SYS_I2C_PCA953X_ADDR;
Laurence Withers633efe92012-07-25 03:55:48 +0000206 int ret = CMD_RET_USAGE, val;
Peter Tysere92739d2008-12-17 16:36:21 -0600207 ulong ul_arg2 = 0;
208 ulong ul_arg3 = 0;
209 cmd_tbl_t *c;
210
211 c = find_cmd_tbl(argv[1], cmd_pca953x, ARRAY_SIZE(cmd_pca953x));
212
213 /* All commands but "device" require 'maxargs' arguments */
214 if (!c || !((argc == (c->maxargs)) ||
Michal Simek01b2a692016-04-11 13:51:26 +0200215 (((long)c->cmd == PCA953X_CMD_DEVICE) &&
Peter Tysere92739d2008-12-17 16:36:21 -0600216 (argc == (c->maxargs - 1))))) {
Laurence Withers633efe92012-07-25 03:55:48 +0000217 return CMD_RET_USAGE;
Peter Tysere92739d2008-12-17 16:36:21 -0600218 }
219
220 /* arg2 used as chip number or pin number */
221 if (argc > 2)
222 ul_arg2 = simple_strtoul(argv[2], NULL, 16);
223
224 /* arg3 used as pin or invert value */
225 if (argc > 3)
226 ul_arg3 = simple_strtoul(argv[3], NULL, 16) & 0x1;
227
Michal Simek01b2a692016-04-11 13:51:26 +0200228 switch ((long)c->cmd) {
Peter Tysere92739d2008-12-17 16:36:21 -0600229 case PCA953X_CMD_INFO:
Laurence Withers633efe92012-07-25 03:55:48 +0000230 ret = pca953x_info(chip);
231 if (ret)
232 ret = CMD_RET_FAILURE;
233 break;
Laurence Withers633efe92012-07-25 03:55:48 +0000234
Peter Tysere92739d2008-12-17 16:36:21 -0600235 case PCA953X_CMD_DEVICE:
236 if (argc == 3)
237 chip = (uint8_t)ul_arg2;
238 printf("Current device address: 0x%x\n", chip);
Laurence Withers633efe92012-07-25 03:55:48 +0000239 ret = CMD_RET_SUCCESS;
240 break;
241
Peter Tysere92739d2008-12-17 16:36:21 -0600242 case PCA953X_CMD_INPUT:
Laurence Withers633efe92012-07-25 03:55:48 +0000243 ret = pca953x_set_dir(chip, (1 << ul_arg2),
Peter Tysere92739d2008-12-17 16:36:21 -0600244 PCA953X_DIR_IN << ul_arg2);
245 val = (pca953x_get_val(chip) & (1 << ul_arg2)) != 0;
246
Laurence Withers633efe92012-07-25 03:55:48 +0000247 if (ret)
248 ret = CMD_RET_FAILURE;
249 else
250 printf("chip 0x%02x, pin 0x%lx = %d\n", chip, ul_arg2,
251 val);
252 break;
253
Peter Tysere92739d2008-12-17 16:36:21 -0600254 case PCA953X_CMD_OUTPUT:
Laurence Withers633efe92012-07-25 03:55:48 +0000255 ret = pca953x_set_dir(chip, (1 << ul_arg2),
Peter Tysere92739d2008-12-17 16:36:21 -0600256 (PCA953X_DIR_OUT << ul_arg2));
Laurence Withers633efe92012-07-25 03:55:48 +0000257 if (!ret)
258 ret = pca953x_set_val(chip, (1 << ul_arg2),
259 (ul_arg3 << ul_arg2));
260 if (ret)
261 ret = CMD_RET_FAILURE;
262 break;
263
Peter Tysere92739d2008-12-17 16:36:21 -0600264 case PCA953X_CMD_INVERT:
Laurence Withers633efe92012-07-25 03:55:48 +0000265 ret = pca953x_set_pol(chip, (1 << ul_arg2),
Peter Tysere92739d2008-12-17 16:36:21 -0600266 (ul_arg3 << ul_arg2));
Laurence Withers633efe92012-07-25 03:55:48 +0000267 if (ret)
268 ret = CMD_RET_FAILURE;
269 break;
Peter Tysere92739d2008-12-17 16:36:21 -0600270 }
Laurence Withers633efe92012-07-25 03:55:48 +0000271
272 if (ret == CMD_RET_FAILURE)
273 eprintf("Error talking to chip at 0x%x\n", chip);
274
275 return ret;
Peter Tysere92739d2008-12-17 16:36:21 -0600276}
277
278U_BOOT_CMD(
279 pca953x, 5, 1, do_pca953x,
Peter Tyser2fb26042009-01-27 18:03:12 -0600280 "pca953x gpio access",
Peter Tysere92739d2008-12-17 16:36:21 -0600281 "device [dev]\n"
282 " - show or set current device address\n"
Peter Tysere92739d2008-12-17 16:36:21 -0600283 "pca953x info\n"
284 " - display info for current chip\n"
Peter Tysere92739d2008-12-17 16:36:21 -0600285 "pca953x output pin 0|1\n"
286 " - set pin as output and drive low or high\n"
287 "pca953x invert pin 0|1\n"
288 " - disable/enable polarity inversion for reads\n"
Laurence Withersd75bc032012-07-25 03:55:47 +0000289 "pca953x input pin\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200290 " - set pin as input and read value"
Peter Tysere92739d2008-12-17 16:36:21 -0600291);
292
293#endif /* CONFIG_CMD_PCA953X */