summaryrefslogtreecommitdiff
path: root/sys/dev/i2c/i2cmux.c
blob: b47045e35be3f82480b6aec3b944374dffff1507 (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
/*	$NetBSD: i2cmux.c,v 1.7 2021/11/10 15:39:03 msaitoh Exp $	*/

/*-
 * Copyright (c) 2020 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
#include "acpica.h"
#endif

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.7 2021/11/10 15:39:03 msaitoh Exp $");

#include <sys/types.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/kmem.h>

#include <dev/fdt/fdtvar.h>
#include <dev/i2c/i2cvar.h>
#include <dev/i2c/i2cmuxvar.h>

#if NACPICA > 0
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpi_i2c.h>
#endif

/*
 * i2c mux
 *
 * This works by interposing a set of virtual controllers behind the real
 * i2c controller.  We provide our own acquire and release functions that
 * perform the following tasks:
 *
 *	acquire -> acquire parent controller, program mux
 *
 *	release -> idle mux, release parent controller
 *
 * All of the actual I/O operations are transparently passed through.
 *
 * N.B. the locking order; the generic I2C layer has already acquired
 * our virtual controller's mutex before calling our acquire function,
 * and we will then acquire the real controller's mutex when we acquire
 * the bus, so the order is:
 *
 *	mux virtual controller -> parent controller
 *
 * These are common routines used by various i2c mux controller
 * implementations (gpio, pin mux, i2c device, etc.).
 */

/*****************************************************************************/

static int
iicmux_acquire_bus(void * const v, int const flags)
{
	struct iicmux_bus * const bus = v;
	struct iicmux_softc * const sc = bus->mux;
	int error;

	error = iic_acquire_bus(sc->sc_i2c_parent, flags);
	if (error) {
		return error;
	}

	error = sc->sc_config->acquire_bus(bus, flags);
	if (error) {
		iic_release_bus(sc->sc_i2c_parent, flags);
	}

	return error;
}

static void
iicmux_release_bus(void * const v, int const flags)
{
	struct iicmux_bus * const bus = v;
	struct iicmux_softc * const sc = bus->mux;

	sc->sc_config->release_bus(bus, flags);
	iic_release_bus(sc->sc_i2c_parent, flags);
}

static int
iicmux_exec(void * const v, i2c_op_t const op, i2c_addr_t const addr,
    const void * const cmdbuf, size_t const cmdlen, void * const databuf,
    size_t const datalen, int const flags)
{
	struct iicmux_bus * const bus = v;
	struct iicmux_softc * const sc = bus->mux;

	return iic_exec(sc->sc_i2c_parent, op, addr, cmdbuf, cmdlen,
			databuf, datalen, flags);
}

/*****************************************************************************/

static int
iicmux_count_children(struct iicmux_softc * const sc)
{
	char name[32];
	int child, count;

 restart:
	for (child = OF_child(sc->sc_i2c_mux_phandle), count = 0; child;
	     child = OF_peer(child)) {
		if (OF_getprop(child, "name", name, sizeof(name)) <= 0) {
			continue;
		}
		if (strcmp(name, "i2c-mux") == 0) {
			/*
			 * The node we encountered is the actual parent
			 * of the i2c bus children.  Stash its phandle
			 * and restart the enumeration.
			 */
			sc->sc_i2c_mux_phandle = child;
			goto restart;
		}
		count++;
	}

	return count;
}

/* XXX iicbus_print() should be able to do this. */
static int
iicmux_print(void * const aux, const char * const pnp)
{
	i2c_tag_t const tag = aux;
	struct iicmux_bus * const bus = tag->ic_cookie;
	int rv;

	rv = iicbus_print(aux, pnp);
	aprint_normal(" bus %d", bus->busidx);

	return rv;
}

static void
iicmux_attach_bus(struct iicmux_softc * const sc,
    uintptr_t const handle, enum i2c_cookie_type handletype, int const busidx)
{
	struct iicmux_bus * const bus = &sc->sc_busses[busidx];

	bus->mux = sc;
	bus->busidx = busidx;
	bus->handle = handle;
	bus->handletype = handletype;

	bus->bus_data = sc->sc_config->get_bus_info(bus);
	if (bus->bus_data == NULL) {
		aprint_error_dev(sc->sc_dev,
		    "unable to get info for bus %d\n", busidx);
		return;
	}

	iic_tag_init(&bus->controller);
	bus->controller.ic_cookie = bus;
	bus->controller.ic_acquire_bus = iicmux_acquire_bus;
	bus->controller.ic_release_bus = iicmux_release_bus;
	bus->controller.ic_exec = iicmux_exec;

	switch (handletype) {
	case I2C_COOKIE_OF:
		fdtbus_register_i2c_controller(&bus->controller,
		    (int)bus->handle);

		fdtbus_attach_i2cbus(sc->sc_dev, (int)bus->handle,
		    &bus->controller, iicmux_print);
		break;
#if NACPICA > 0
	case I2C_COOKIE_ACPI: {
		struct acpi_devnode *ad = acpi_match_node((ACPI_HANDLE)handle);
		KASSERT(ad != NULL);
		struct i2cbus_attach_args iba = {
			.iba_tag = &bus->controller,
			.iba_child_devices = acpi_enter_i2c_devs(NULL, ad)
		};
		config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
	}	break;
#endif
	default:
		aprint_error_dev(sc->sc_dev, "unknown handle type\n");
		break;
	}
}

static void
iicmux_attach_fdt(struct iicmux_softc * const sc)
{
	/*
	 * We start out assuming that the i2c bus nodes are children of
	 * our own node.  We'll adjust later if we encounter an "i2c-mux"
	 * node when counting our children.  If we encounter such a node,
	 * then it's that node that is the parent of the i2c bus children.
	 */
	sc->sc_i2c_mux_phandle = (int)sc->sc_handle;

	sc->sc_nbusses = iicmux_count_children(sc);
	if (sc->sc_nbusses == 0) {
		return;
	}

	sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
	    KM_SLEEP);

	int child, idx;
	for (child = OF_child(sc->sc_i2c_mux_phandle), idx = 0; child;
	     child = OF_peer(child), idx++) {
		KASSERT(idx < sc->sc_nbusses);
		iicmux_attach_bus(sc, child, I2C_COOKIE_OF, idx);
	}
}

#if NACPICA > 0
static void
iicmux_attach_acpi(struct iicmux_softc * const sc)
{
	ACPI_HANDLE hdl = (ACPI_HANDLE)sc->sc_handle;
	struct acpi_devnode *devnode, *ad;
	int idx;

	devnode = acpi_match_node(hdl);
	KASSERT(devnode != NULL);

	/* Count child busses */
	sc->sc_nbusses = 0;
	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
		    !acpi_device_present(ad->ad_handle)) {
			continue;
		}
		sc->sc_nbusses++;
	}

	sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
	    KM_SLEEP);

	/* Attach child busses */
	idx = 0;
	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
		    !acpi_device_present(ad->ad_handle)) {
			continue;
		}
		iicmux_attach_bus(sc, (uintptr_t)ad->ad_handle,
		    I2C_COOKIE_ACPI, idx);
		idx++;
	}
}
#endif

void
iicmux_attach(struct iicmux_softc * const sc)
{
	/*
	 * We expect sc->sc_handle, sc->sc_config, and sc->sc_i2c_parent
	 * to be initialized by the front-end.
	 */
	KASSERT(sc->sc_handle > 0);
	KASSERT(sc->sc_config != NULL);
	KASSERT(sc->sc_i2c_parent != NULL);

	/*
	 * Gather up all of the various bits of information needed
	 * for this particular type of i2c mux.
	 */
	sc->sc_mux_data = sc->sc_config->get_mux_info(sc);
	if (sc->sc_mux_data == NULL) {
		aprint_error_dev(sc->sc_dev, "unable to get info for mux\n");
		return;
	}

	/*
	 * Do configuration method (OF, ACPI) specific setup.
	 */
	switch (sc->sc_handletype) {
	case I2C_COOKIE_OF:
		iicmux_attach_fdt(sc);
		break;
#if NACPICA > 0
	case I2C_COOKIE_ACPI:
		iicmux_attach_acpi(sc);
		break;
#endif
	default:
		aprint_error_dev(sc->sc_dev, "could not configure mux: "
		    "handle type %u not supported\n", sc->sc_handletype);
		break;
	}
}