summaryrefslogtreecommitdiff
path: root/driver/ioexpander/pcal6408.c
blob: 287e0506d02f517063f8addd9639877b94be6401 (plain)
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
347
348
349
350
351
352
353
354
/* Copyright 2020 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.
 *
 * NXP PCA(L)6408 I/O expander
 */
#include "common.h"
#include "console.h"
#include "math_util.h"
#include "gpio.h"
#include "i2c.h"
#include "ioexpander.h"
#include "pcal6408.h"

#define CPRINTF(format, args...) cprintf(CC_GPIO, format, ## args)
#define CPRINTS(format, args...) cprints(CC_GPIO, format, ## args)

/*
 * Store interrupt mask registers locally. In this way,
 * we don't have to read it via i2c transaction every time.
 * Default value of interrupt mask register is 0xff.
 */
uint8_t pcal6408_int_mask[] = {
	[0 ... (CONFIG_IO_EXPANDER_PORT_COUNT - 1)] =  0xff };


static int pcal6408_read(int ioex, int reg, int *data)
{
	int rv;
	struct ioexpander_config_t *ioex_p = &ioex_config[ioex];

	rv = i2c_read8(ioex_p->i2c_host_port, ioex_p->i2c_addr_flags,
			reg, data);

	return rv;
}

static int pcal6408_write(int ioex, int reg, int data)
{
	int rv;
	struct ioexpander_config_t *ioex_p = &ioex_config[ioex];

	rv = i2c_write8(ioex_p->i2c_host_port, ioex_p->i2c_addr_flags,
			reg, data);

	return rv;
}

static int pcal6408_ioex_check_is_valid(int port, int mask)
{
	if (port != 0)
		return EC_ERROR_INVAL;

	if (mask & ~PCAL6408_VALID_GPIO_MASK) {
		CPRINTF("GPIO%02d is not support in PCAL6408\n",
						__fls(mask));
		return EC_ERROR_INVAL;
	}

	return EC_SUCCESS;
}

static int pcal6408_ioex_init(int ioex)
{
	/* It seems that we have nothing to do here.
	 * This chip has not a chip id to be identified.
	 */
	return EC_SUCCESS;
}

static int pcal6408_ioex_get_level(int ioex, int port, int mask, int *val)
{
	int rv;

	rv = pcal6408_ioex_check_is_valid(port, mask);
	if (rv != EC_SUCCESS)
		return rv;

	rv = pcal6408_read(ioex, PCAL6408_REG_INPUT, val);
	if (rv != EC_SUCCESS)
		return rv;

	*val = !!(*val & mask);

	return EC_SUCCESS;
}

static int pcal6408_ioex_set_level(int ioex, int port, int mask, int value)
{
	int rv, val;

	rv = pcal6408_ioex_check_is_valid(port, mask);
	if (rv != EC_SUCCESS)
		return rv;

	rv = pcal6408_read(ioex, PCAL6408_REG_OUTPUT, &val);
	if (rv != EC_SUCCESS)
		return rv;

	if (value)
		val |= mask;
	else
		val &= ~mask;

	return pcal6408_write(ioex, PCAL6408_REG_OUTPUT, val);
}

static int pcal6408_ioex_get_flags_by_mask(int ioex, int port, int mask,
					int *flags)
{
	int rv, val;

	rv = pcal6408_ioex_check_is_valid(port, mask);
	if (rv != EC_SUCCESS)
		return rv;

	*flags = GPIO_FLAG_NONE;

	rv = pcal6408_read(ioex, PCAL6408_REG_CONFIG, &val);
	if (rv != EC_SUCCESS)
		return rv;

	if (val & mask)
		*flags |= GPIO_INPUT;
	else
		*flags |= GPIO_OUTPUT;

	rv = pcal6408_read(ioex, PCAL6408_REG_INPUT, &val);
	if (rv != EC_SUCCESS)
		return rv;

	if (val & mask)
		*flags |= GPIO_HIGH;
	else
		*flags |= GPIO_LOW;

	rv = pcal6408_read(ioex, PCAL6408_REG_OUT_CONFIG, &val);
	if (rv != EC_SUCCESS)
		return rv;

	if (val & PCAL6408_OUT_CONFIG_OPEN_DRAIN)
		*flags |= GPIO_OPEN_DRAIN;

	rv = pcal6408_read(ioex, PCAL6408_REG_PULL_ENABLE, &val);
	if (rv != EC_SUCCESS)
		return rv;

	if (val & mask) {
		rv = pcal6408_read(ioex, PCAL6408_REG_PULL_UP_DOWN, &val);
		if (rv != EC_SUCCESS)
			return rv;

		if (val & mask)
			*flags |= GPIO_PULL_UP;
		else
			*flags |= GPIO_PULL_DOWN;
	}

	rv = pcal6408_read(ioex, PCAL6408_REG_INT_MASK, &val);
	if (rv != EC_SUCCESS)
		return rv;

	if ((!!(val & mask) == 0) && ((*flags) & GPIO_INPUT))
		*flags |= GPIO_INT_BOTH;

	return rv;
}

static int pcal6408_ioex_set_flags_by_mask(int ioex, int port, int mask,
					int flags)
{
	int rv, val;

	rv = pcal6408_ioex_check_is_valid(port, mask);
	if (rv != EC_SUCCESS)
		return rv;

	if (((flags & GPIO_INT_BOTH) == GPIO_INT_RISING) ||
		((flags & GPIO_INT_BOTH) == GPIO_INT_FALLING)) {
		CPRINTF("PCAL6408 only support GPIO_INT_BOTH.\n");
		return EC_ERROR_INVAL;
	}


	if ((flags & (GPIO_INT_F_RISING | GPIO_INT_F_FALLING)) &&
		!(flags & GPIO_INPUT)) {
		CPRINTF("Interrupt pin must be GPIO_INPUT.\n");
		return EC_ERROR_INVAL;
	}

	/* All output gpios share GPIO_OPEN_DRAIN, should be consistent */
	if (flags & GPIO_OPEN_DRAIN)
		val = PCAL6408_OUT_CONFIG_OPEN_DRAIN;
	else
		val = 0;

	rv = pcal6408_write(ioex, PCAL6408_REG_OUT_CONFIG, val);
	if (rv != EC_SUCCESS)
		return rv;

	rv = pcal6408_read(ioex, PCAL6408_REG_CONFIG, &val);
	if (rv != EC_SUCCESS)
		return rv;

	if (flags & GPIO_INPUT)
		val |= mask;
	if (flags & GPIO_OUTPUT)
		val &= ~mask;

	rv = pcal6408_write(ioex, PCAL6408_REG_CONFIG, val);
	if (rv != EC_SUCCESS)
		return rv;

	if (flags & GPIO_OUTPUT) {
		rv = pcal6408_read(ioex, PCAL6408_REG_OUTPUT, &val);
		if (rv != EC_SUCCESS)
			return rv;

		if (flags & GPIO_HIGH)
			val |= mask;
		else if (flags & GPIO_LOW)
			val &= ~mask;

		rv = pcal6408_write(ioex, PCAL6408_REG_OUTPUT, val);
		if (rv != EC_SUCCESS)
			return rv;
	}

	if (!(flags & (GPIO_PULL_UP | GPIO_PULL_DOWN))) {
		rv = pcal6408_read(ioex, PCAL6408_REG_PULL_ENABLE, &val);
		if (rv != EC_SUCCESS)
			return rv;

		val &= ~mask;

		rv = pcal6408_write(ioex, PCAL6408_REG_PULL_ENABLE, val);
		if (rv != EC_SUCCESS)
			return rv;
	} else {
		rv = pcal6408_read(ioex, PCAL6408_REG_PULL_ENABLE, &val);
		if (rv != EC_SUCCESS)
			return rv;

		val |= mask;

		rv = pcal6408_write(ioex, PCAL6408_REG_PULL_ENABLE, val);
		if (rv != EC_SUCCESS)
			return rv;

		rv = pcal6408_read(ioex, PCAL6408_REG_PULL_UP_DOWN, &val);
		if (rv != EC_SUCCESS)
			return rv;

		if (flags & GPIO_PULL_UP)
			val |= mask;
		else if (flags & GPIO_PULL_DOWN)
			val &= ~mask;

		rv = pcal6408_write(ioex, PCAL6408_REG_PULL_UP_DOWN, val);
		if (rv != EC_SUCCESS)
			return rv;
	}

	return rv;
}

static int pcal6408_ioex_enable_interrupt(int ioex, int port, int mask,
					int enable)
{
	int rv, val;

	rv = pcal6408_ioex_check_is_valid(port, mask);
	if (rv != EC_SUCCESS)
		return rv;

	/* Interrupt should be latched */
	rv = pcal6408_read(ioex, PCAL6408_REG_INPUT_LATCH, &val);
	if (rv != EC_SUCCESS)
		return rv;

	if (enable)
		val |= mask;
	else
		val &= ~mask;

	rv = pcal6408_write(ioex, PCAL6408_REG_INPUT_LATCH, val);
	if (rv != EC_SUCCESS)
		return rv;

	/*
	 * Enable or disable interrupt.
	 * In PCAL6408_REG_INT_MASK, 0 = enable interrupt,
	 *                           1 = disable interrupt.
	 */
	if (enable)
		pcal6408_int_mask[ioex] &= ~mask;
	else
		pcal6408_int_mask[ioex] |= mask;

	rv = pcal6408_write(ioex, PCAL6408_REG_INT_MASK,
			pcal6408_int_mask[ioex]);

	return rv;
}

int pcal6408_ioex_event_handler(int ioex)
{
	int int_status, int_mask;
	struct ioexpander_config_t *ioex_p = &ioex_config[ioex];
	int i, rv = 0;
	const struct ioex_info *g;

	int_mask = pcal6408_int_mask[ioex];

	/*
	 * Read input port register will clear the interrupt,
	 * read status register will not.
	 */
	rv = i2c_read8(ioex_p->i2c_host_port, ioex_p->i2c_addr_flags,
				PCAL6408_REG_INT_STATUS, &int_status);
	if (rv != EC_SUCCESS)
		return rv;

	/*
	 * In pcal6408_int_mask[x], 0 = enable interrupt,
	 *                          1 = disable interrupt.
	 */
	int_status = int_status & ~int_mask;

	if (!int_status)
		return EC_SUCCESS;

	for (i = 0, g = ioex_list; i < ioex_ih_count; i++, g++) {

		if (ioex == g->ioex && 0 == g->port &&
					(int_status & g->mask)) {
			ioex_irq_handlers[i](i + IOEX_SIGNAL_START);
			int_status &= ~g->mask;
			if (!int_status)
				break;
		}
	}

	return EC_SUCCESS;
}

const struct ioexpander_drv pcal6408_ioexpander_drv = {
	.init			= &pcal6408_ioex_init,
	.get_level		= &pcal6408_ioex_get_level,
	.set_level		= &pcal6408_ioex_set_level,
	.get_flags_by_mask	= &pcal6408_ioex_get_flags_by_mask,
	.set_flags_by_mask	= &pcal6408_ioex_set_flags_by_mask,
	.enable_interrupt	= &pcal6408_ioex_enable_interrupt,
};