summaryrefslogtreecommitdiff
path: root/sys/dev/i2c/pca9548.c
blob: 1fe6328f5c28d8bbc2e568c9e9a97cf5565aa6e8 (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
/*	$OpenBSD: pca9548.c,v 1.2 2020/09/29 13:50:54 patrick Exp $	*/

/*
 * Copyright (c) 2020 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <machine/bus.h>

#define _I2C_PRIVATE
#include <dev/i2c/i2cvar.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_misc.h>

#define PCA9548_NUM_CHANNELS	8

struct pcamux_bus {
	struct pcamux_softc	*pb_sc;
	int			pb_node;
	int			pb_channel;
	struct i2c_controller	pb_ic;
	struct i2c_bus		pb_ib;
};

struct pcamux_softc {
	struct device		sc_dev;
	i2c_tag_t		sc_tag;
	i2c_addr_t		sc_addr;
	
	int			sc_node;
	int			sc_channel;
	struct pcamux_bus	sc_bus[PCA9548_NUM_CHANNELS];
	struct rwlock		sc_lock;
};

int	pcamux_match(struct device *, void *, void *);
void	pcamux_attach(struct device *, struct device *, void *);

struct cfattach pcamux_ca = {
	sizeof(struct pcamux_softc), pcamux_match, pcamux_attach
};

struct cfdriver pcamux_cd = {
	NULL, "pcamux", DV_DULL
};

int	pcamux_acquire_bus(void *, int);
void	pcamux_release_bus(void *, int);
int	pcamux_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
	    void *, size_t, int);
void	pcamux_bus_scan(struct device *, struct i2cbus_attach_args *, void *);

int
pcamux_match(struct device *parent, void *match, void *aux)
{
	struct i2c_attach_args *ia = aux;

	if (strcmp(ia->ia_name, "nxp,pca9548") == 0)
		return (1);
	return (0);
}

void
pcamux_attach(struct device *parent, struct device *self, void *aux)
{
	struct pcamux_softc *sc = (struct pcamux_softc *)self;
	struct i2c_attach_args *ia = aux;
	int node = *(int *)ia->ia_cookie;

	sc->sc_tag = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;

	sc->sc_node = node;
	sc->sc_channel = -1;	/* unknown */
	rw_init(&sc->sc_lock, sc->sc_dev.dv_xname);

	printf("\n");

	for (node = OF_child(node); node; node = OF_peer(node)) {
		struct i2cbus_attach_args iba;
		struct pcamux_bus *pb;
		uint32_t channel;

		channel = OF_getpropint(node, "reg", -1);
		if (channel >= PCA9548_NUM_CHANNELS)
			continue;

		pb = &sc->sc_bus[channel];
		pb->pb_sc = sc;
		pb->pb_node = node;
		pb->pb_channel = channel;
		pb->pb_ic.ic_cookie = pb;
		pb->pb_ic.ic_acquire_bus = pcamux_acquire_bus;
		pb->pb_ic.ic_release_bus = pcamux_release_bus;
		pb->pb_ic.ic_exec = pcamux_exec;

		/* Configure the child busses. */
		memset(&iba, 0, sizeof(iba));
		iba.iba_name = "iic";
		iba.iba_tag = &pb->pb_ic;
		iba.iba_bus_scan = pcamux_bus_scan;
		iba.iba_bus_scan_arg = &pb->pb_node;

		config_found(&sc->sc_dev, &iba, iicbus_print);

		pb->pb_ib.ib_node = node;
		pb->pb_ib.ib_ic = &pb->pb_ic;
		i2c_register(&pb->pb_ib);
	}
}

int
pcamux_set_channel(struct pcamux_softc *sc, int channel, int flags)
{
	uint8_t data;
	int error;

	if (channel < -1 || channel >= PCA9548_NUM_CHANNELS)
		return ENXIO;

	if (sc->sc_channel == channel)
		return 0;

	data = (channel == -1) ? 0 : (1 << channel);
	error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
	    sc->sc_addr, NULL, 0, &data, sizeof data, flags);

	return error;
}

int
pcamux_acquire_bus(void *cookie, int flags)
{
	struct pcamux_bus *pb = cookie;
	struct pcamux_softc *sc = pb->pb_sc;
	int rwflags = RW_WRITE;
	int error;

	if (flags & I2C_F_POLL)
		rwflags |= RW_NOSLEEP;

	error = rw_enter(&sc->sc_lock, rwflags);
	if (error)
		return error;

	/* Acquire parent bus. */
	error = iic_acquire_bus(sc->sc_tag, flags);
	if (error) {
		rw_exit_write(&sc->sc_lock);
		return error;
	}

	error = pcamux_set_channel(sc, pb->pb_channel, flags);
	if (error) {
		iic_release_bus(sc->sc_tag, flags);
		rw_exit_write(&sc->sc_lock);
		return error;
	}

	return 0;
}

void
pcamux_release_bus(void *cookie, int flags)
{
	struct pcamux_bus *pb = cookie;
	struct pcamux_softc *sc = pb->pb_sc;

	/* Release parent bus. */
	iic_release_bus(sc->sc_tag, flags);
	rw_exit_write(&sc->sc_lock);
}

int
pcamux_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmd,
    size_t cmdlen, void *buf, size_t buflen, int flags)
{
	struct pcamux_bus *pb = cookie;
	struct pcamux_softc *sc = pb->pb_sc;

	rw_assert_wrlock(&sc->sc_lock);

	/* Issue the transaction on the parent bus. */
	return iic_exec(sc->sc_tag, op, addr, cmd, cmdlen, buf, buflen, flags);
}

void
pcamux_bus_scan(struct device *self, struct i2cbus_attach_args *iba, void *arg)
{
	int iba_node = *(int *)arg;
	struct i2c_attach_args ia;
	char name[32];
	uint32_t reg[1];
	int node;

	for (node = OF_child(iba_node); node; node = OF_peer(node)) {
		memset(name, 0, sizeof(name));
		memset(reg, 0, sizeof(reg));

		if (OF_getprop(node, "compatible", name, sizeof(name)) == -1)
			continue;
		if (name[0] == '\0')
			continue;

		if (OF_getprop(node, "reg", &reg, sizeof(reg)) != sizeof(reg))
			continue;

		memset(&ia, 0, sizeof(ia));
		ia.ia_tag = iba->iba_tag;
		ia.ia_addr = bemtoh32(&reg[0]);
		ia.ia_name = name;
		ia.ia_cookie = &node;
		config_found(self, &ia, iic_print);
	}
}