1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* IO Expander Controller Common Code */
#include "console.h"
#include "gpio.h"
#include "hooks.h"
#include "ioexpander.h"
#include "system.h"
#include "util.h"
#define CPRINTF(format, args...) cprintf(CC_GPIO, format, ## args)
#define CPRINTS(format, args...) cprints(CC_GPIO, format, ## args)
static uint8_t last_val[(IOEX_COUNT + 7) / 8];
static int last_val_changed(enum ioex_signal signal, int v)
{
const int i = signal - IOEX_SIGNAL_START;
ASSERT(signal_is_ioex(signal));
if (v && !(last_val[i / 8] & (BIT(i % 8)))) {
last_val[i / 8] |= BIT(i % 8);
return 1;
} else if (!v && last_val[i / 8] & (BIT(i % 8))) {
last_val[i / 8] &= ~(BIT(i % 8));
return 1;
} else {
return 0;
}
}
int signal_is_ioex(int signal)
{
return ((signal >= IOEX_SIGNAL_START) && (signal < IOEX_SIGNAL_END));
}
static const struct ioex_info *ioex_get_signal_info(enum ioex_signal signal)
{
const struct ioex_info *g;
ASSERT(signal_is_ioex(signal));
g = ioex_list + signal - IOEX_SIGNAL_START;
if (!(ioex_config[g->ioex].flags & IOEX_FLAGS_INITIALIZED)) {
CPRINTS("ioex %s disabled", g->name);
return NULL;
}
return g;
}
static int ioex_is_valid_interrupt_signal(enum ioex_signal signal)
{
const struct ioexpander_drv *drv;
const struct ioex_info *g = ioex_get_signal_info(signal);
if (g == NULL)
return EC_ERROR_BUSY;
/* Fail if no interrupt handler */
if (signal - IOEX_SIGNAL_START >= ioex_ih_count)
return EC_ERROR_PARAM1;
drv = ioex_config[g->ioex].drv;
/*
* Not every IOEX chip can support interrupt, check it before enabling
* the interrupt function
*/
if (drv->enable_interrupt == NULL) {
CPRINTS("IOEX chip port %d doesn't support INT", g->ioex);
return EC_ERROR_UNIMPLEMENTED;
}
return EC_SUCCESS;
}
int ioex_enable_interrupt(enum ioex_signal signal)
{
int rv;
const struct ioex_info *g = ioex_get_signal_info(signal);
const struct ioexpander_drv *drv;
rv = ioex_is_valid_interrupt_signal(signal);
if (rv != EC_SUCCESS)
return rv;
drv = ioex_config[g->ioex].drv;
return drv->enable_interrupt(g->ioex, g->port, g->mask, 1);
}
int ioex_disable_interrupt(enum ioex_signal signal)
{
int rv;
const struct ioexpander_drv *drv;
const struct ioex_info *g = ioex_get_signal_info(signal);
rv = ioex_is_valid_interrupt_signal(signal);
if (rv != EC_SUCCESS)
return rv;
drv = ioex_config[g->ioex].drv;
return drv->enable_interrupt(g->ioex, g->port, g->mask, 0);
}
int ioex_get_flags(enum ioex_signal signal, int *flags)
{
const struct ioex_info *g = ioex_get_signal_info(signal);
if (g == NULL)
return EC_ERROR_BUSY;
return ioex_config[g->ioex].drv->get_flags_by_mask(g->ioex,
g->port, g->mask, flags);
}
int ioex_set_flags(enum ioex_signal signal, int flags)
{
const struct ioex_info *g = ioex_get_signal_info(signal);
if (g == NULL)
return EC_ERROR_BUSY;
return ioex_config[g->ioex].drv->set_flags_by_mask(g->ioex,
g->port, g->mask, flags);
}
int ioex_get_level(enum ioex_signal signal, int *val)
{
const struct ioex_info *g = ioex_get_signal_info(signal);
if (g == NULL)
return EC_ERROR_BUSY;
return ioex_config[g->ioex].drv->get_level(g->ioex, g->port,
g->mask, val);
}
int ioex_set_level(enum ioex_signal signal, int value)
{
const struct ioex_info *g = ioex_get_signal_info(signal);
if (g == NULL)
return EC_ERROR_BUSY;
return ioex_config[g->ioex].drv->set_level(g->ioex, g->port,
g->mask, value);
}
#ifdef CONFIG_IO_EXPANDER_SUPPORT_GET_PORT
int ioex_get_port(int ioex, int port, int *val)
{
if (ioex_config[ioex].drv->get_port == NULL)
return EC_ERROR_UNIMPLEMENTED;
return ioex_config[ioex].drv->get_port(ioex, port, val);
}
#endif
int ioex_init(int ioex)
{
const struct ioex_info *g = ioex_list;
const struct ioexpander_drv *drv = ioex_config[ioex].drv;
int rv;
int i;
if (drv->init != NULL) {
rv = drv->init(ioex);
if (rv != EC_SUCCESS)
return rv;
}
/*
* Set all IO expander GPIOs to default flags according to the setting
* in gpio.inc
*/
for (i = 0; i < IOEX_COUNT; i++, g++) {
int flags = g->flags;
if (g->ioex == ioex && g->mask && !(flags & GPIO_DEFAULT)) {
/* Late-sysJump should not set the output levels */
if (system_jumped_late())
flags &= ~(GPIO_LOW | GPIO_HIGH);
drv->set_flags_by_mask(g->ioex, g->port,
g->mask, flags);
}
}
ioex_config[ioex].flags &= ~IOEX_FLAGS_DEFAULT_INIT_DISABLED;
ioex_config[ioex].flags |= IOEX_FLAGS_INITIALIZED;
return EC_SUCCESS;
}
static void ioex_init_default(void)
{
int i;
for (i = 0; i < CONFIG_IO_EXPANDER_PORT_COUNT; i++) {
/*
* If the IO Expander has been initialized or if the default
* initialization is disabled, skip initializing.
*/
if (ioex_config[i].flags & (IOEX_FLAGS_INITIALIZED |
IOEX_FLAGS_DEFAULT_INIT_DISABLED))
continue;
ioex_init(i);
}
}
DECLARE_HOOK(HOOK_INIT, ioex_init_default, HOOK_PRIO_INIT_I2C + 1);
const char *ioex_get_name(enum ioex_signal signal)
{
const struct ioex_info *g = ioex_list + signal - IOEX_SIGNAL_START;
return g->name;
}
static void print_ioex_info(enum ioex_signal signal)
{
int changed, v, val;
int flags = 0;
const struct ioex_info *g = ioex_list + signal - IOEX_SIGNAL_START;
if (!(ioex_config[g->ioex].flags & IOEX_FLAGS_INITIALIZED)) {
ccprintf(" DISABLED %s\n", ioex_get_name(signal));
return;
}
v = ioex_get_level(signal, &val);
if (v) {
ccprintf("Fail to get %s level\n", ioex_get_name(signal));
return;
}
v = ioex_get_flags(signal, &flags);
if (v) {
ccprintf("Fail to get %s flags\n", ioex_get_name(signal));
return;
}
changed = last_val_changed(signal, val);
ccprintf(" %d%c %s%s%s%s%s%s\n", val,
(changed ? '*' : ' '),
(flags & GPIO_INPUT ? "I " : ""),
(flags & GPIO_OUTPUT ? "O " : ""),
(flags & GPIO_LOW ? "L " : ""),
(flags & GPIO_HIGH ? "H " : ""),
(flags & GPIO_OPEN_DRAIN ? "ODR " : ""),
ioex_get_name(signal));
/* Flush console to avoid truncating output */
cflush();
}
static int ioex_get_default_flags(enum ioex_signal signal)
{
const struct ioex_info *g = ioex_get_signal_info(signal);
if (g == NULL)
return 0;
return g->flags;
}
/* IO expander commands */
static enum ioex_signal find_ioex_by_name(const char *name)
{
enum ioex_signal signal;
if (!name)
return IOEX_SIGNAL_END;
for (signal = IOEX_SIGNAL_START; signal < IOEX_SIGNAL_END; signal++) {
if (!strcasecmp(name, ioex_get_name(signal)))
return signal;
}
return IOEX_SIGNAL_END;
}
static enum ec_error_list ioex_set(const char *name, int value)
{
enum ioex_signal signal = find_ioex_by_name(name);
if (!signal_is_ioex(signal))
return EC_ERROR_INVAL;
if (!(ioex_get_default_flags(signal) & GPIO_OUTPUT))
return EC_ERROR_INVAL;
return ioex_set_level(signal, value);
}
static int command_ioex_set(int argc, char **argv)
{
char *e;
int v;
if (argc < 3)
return EC_ERROR_PARAM_COUNT;
v = strtoi(argv[2], &e, 0);
if (*e)
return EC_ERROR_PARAM2;
if (ioex_set(argv[1], v) != EC_SUCCESS)
return EC_ERROR_PARAM1;
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(ioexset, command_ioex_set,
"name <0 | 1>",
"Set level of a IO expander IO");
static int command_ioex_get(int argc, char **argv)
{
enum ioex_signal signal;
/* If a signal is specified, print only that one */
if (argc == 2) {
signal = find_ioex_by_name(argv[1]);
if (!signal_is_ioex(signal))
return EC_ERROR_PARAM1;
print_ioex_info(signal);
return EC_SUCCESS;
}
/* Otherwise print them all */
for (signal = IOEX_SIGNAL_START; signal < IOEX_SIGNAL_END; signal++)
print_ioex_info(signal);
return EC_SUCCESS;
}
DECLARE_SAFE_CONSOLE_COMMAND(ioexget, command_ioex_get,
"[name]",
"Read level of IO expander pin(s)");
|