aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/gpio/gpioiic.c
blob: 6d62a6d04d95df65b91a479cdde4ff3eaf185662 (plain) (blame)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
 * Copyright (c) 2010 Luiz Otavio O Souza
 * Copyright (c) 2019 Ian Lepore <ian@freebsd.org>
 *
 * 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 AUTHOR 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 AUTHOR 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.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_platform.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/gpio.h>
#include <sys/kernel.h>
#include <sys/module.h>

#include <dev/gpio/gpiobusvar.h>
#include <dev/iicbus/iiconf.h>

#include "gpiobus_if.h"
#include "iicbb_if.h"

#define	GPIOIIC_SCL_DFLT	0
#define	GPIOIIC_SDA_DFLT	1
#define	GPIOIIC_MIN_PINS	2

struct gpioiic_softc 
{
	device_t	dev;
	gpio_pin_t	sclpin;
	gpio_pin_t	sdapin;
};

#ifdef FDT

#include <dev/ofw/ofw_bus.h>

static struct ofw_compat_data compat_data[] = {
	{"i2c-gpio",  true}, /* Standard devicetree compat string */
	{"gpioiic",   true}, /* Deprecated old freebsd compat string */
	{NULL,        false}
};
OFWBUS_PNP_INFO(compat_data);
SIMPLEBUS_PNP_INFO(compat_data);

static phandle_t
gpioiic_get_node(device_t bus, device_t dev)
{

	/* Share our fdt node with iicbus so it can find its child nodes. */
	return (ofw_bus_get_node(bus));
}

static int
gpioiic_setup_fdt_pins(struct gpioiic_softc *sc)
{
	phandle_t node;
	int err;

	node = ofw_bus_get_node(sc->dev);

	/*
	 * Historically, we used the first two array elements of the gpios
	 * property.  The modern bindings specify separate scl-gpios and
	 * sda-gpios properties.  We cope with whichever is present.
	 */
	if (OF_hasprop(node, "gpios")) {
		if ((err = gpio_pin_get_by_ofw_idx(sc->dev, node,
		    GPIOIIC_SCL_DFLT, &sc->sclpin)) != 0) {
			device_printf(sc->dev, "invalid gpios property\n");
			return (err);
		}
		if ((err = gpio_pin_get_by_ofw_idx(sc->dev, node,
		    GPIOIIC_SDA_DFLT, &sc->sdapin)) != 0) {
			device_printf(sc->dev, "ivalid gpios property\n");
			return (err);
		}
	} else {
		if ((err = gpio_pin_get_by_ofw_property(sc->dev, node,
		    "scl-gpios", &sc->sclpin)) != 0) {
			device_printf(sc->dev, "missing scl-gpios property\n");
			return (err);
		}
		if ((err = gpio_pin_get_by_ofw_property(sc->dev, node,
		    "sda-gpios", &sc->sdapin)) != 0) {
			device_printf(sc->dev, "missing sda-gpios property\n");
			return (err);
		}
	}
	return (0);
}
#endif /* FDT */

static int
gpioiic_setup_hinted_pins(struct gpioiic_softc *sc)
{
	device_t busdev;
	const char *busname, *devname;
	int err, numpins, sclnum, sdanum, unit;

	devname = device_get_name(sc->dev);
	unit = device_get_unit(sc->dev);
	busdev = device_get_parent(sc->dev);

	/*
	 * If there is not an "at" hint naming our actual parent, then we
	 * weren't instantiated as a child of gpiobus via hints, and we thus
	 * can't access ivars that only exist for such children.
	 */
	if (resource_string_value(devname, unit, "at", &busname) != 0 ||
	    (strcmp(busname, device_get_nameunit(busdev)) != 0 &&
	     strcmp(busname, device_get_name(busdev)) != 0)) {
		return (ENOENT);
	}

	/* Make sure there were hints for at least two pins. */
	numpins = gpiobus_get_npins(sc->dev);
	if (numpins < GPIOIIC_MIN_PINS) {
#ifdef FDT
		/*
		 * Be silent when there are no hints on FDT systems; the FDT
		 * data will provide the pin config (we'll whine if it doesn't).
		 */
		if (numpins == 0) {
			return (ENOENT);
		}
#endif
		device_printf(sc->dev, 
		    "invalid pins hint; it must contain at least %d pins\n",
		    GPIOIIC_MIN_PINS);
		return (EINVAL);
	}

	/*
	 * Our parent bus has already parsed the pins hint and it will use that
	 * info when we call gpio_pin_get_by_child_index().  But we have to
	 * handle the scl/sda index hints that tell us which of the two pins is
	 * the clock and which is the data.  They're optional, but if present
	 * they must be a valid index (0 <= index < numpins).
	 */
	if ((err = resource_int_value(devname, unit, "scl", &sclnum)) != 0)
		sclnum = GPIOIIC_SCL_DFLT;
	else if (sclnum < 0 || sclnum >= numpins) {
		device_printf(sc->dev, "invalid scl hint %d\n", sclnum);
		return (EINVAL);
	}
	if ((err = resource_int_value(devname, unit, "sda", &sdanum)) != 0)
		sdanum = GPIOIIC_SDA_DFLT;
	else if (sdanum < 0 || sdanum >= numpins) {
		device_printf(sc->dev, "invalid sda hint %d\n", sdanum);
		return (EINVAL);
	}

	/* Allocate gpiobus_pin structs for the pins we found above. */
	if ((err = gpio_pin_get_by_child_index(sc->dev, sclnum,
	    &sc->sclpin)) != 0)
		return (err);
	if ((err = gpio_pin_get_by_child_index(sc->dev, sdanum,
	    &sc->sdapin)) != 0)
		return (err);

	return (0);
}

static void
gpioiic_setsda(device_t dev, int val)
{
	struct gpioiic_softc *sc = device_get_softc(dev);

	if (val) {
		gpio_pin_setflags(sc->sdapin, GPIO_PIN_INPUT);
	} else {
		gpio_pin_setflags(sc->sdapin,
		    GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN);
		gpio_pin_set_active(sc->sdapin, 0);
	}
}

static void
gpioiic_setscl(device_t dev, int val)
{
	struct gpioiic_softc *sc = device_get_softc(dev);

	if (val) {
		gpio_pin_setflags(sc->sclpin, GPIO_PIN_INPUT);
	} else {
		gpio_pin_setflags(sc->sclpin,
		    GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN);
		gpio_pin_set_active(sc->sclpin, 0);
	}
}

static int
gpioiic_getscl(device_t dev)
{
	struct gpioiic_softc *sc = device_get_softc(dev);
	bool val;

	gpio_pin_setflags(sc->sclpin, GPIO_PIN_INPUT);
	gpio_pin_is_active(sc->sclpin, &val);
	return (val);
}

static int
gpioiic_getsda(device_t dev)
{
	struct gpioiic_softc *sc = device_get_softc(dev);
	bool val;

	gpio_pin_setflags(sc->sdapin, GPIO_PIN_INPUT);
	gpio_pin_is_active(sc->sdapin, &val);
	return (val);
}

static int
gpioiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
{
	struct gpioiic_softc *sc = device_get_softc(dev);

	/* Stop driving the bus pins. */
	gpio_pin_setflags(sc->sdapin, GPIO_PIN_INPUT);
	gpio_pin_setflags(sc->sclpin, GPIO_PIN_INPUT);

	/* Indicate that we have no slave address (master mode). */
	return (IIC_ENOADDR);
}

static void
gpioiic_cleanup(struct gpioiic_softc *sc)
{

	device_delete_children(sc->dev);

	if (sc->sclpin != NULL)
		gpio_pin_release(sc->sclpin);

	if (sc->sdapin != NULL)
		gpio_pin_release(sc->sdapin);
}

static int
gpioiic_probe(device_t dev)
{
	int rv;

	/*
	 * By default we only bid to attach if specifically added by our parent
	 * (usually via hint.gpioiic.#.at=busname).  On FDT systems we bid as
	 * the default driver based on being configured in the FDT data.
	 */
	rv = BUS_PROBE_NOWILDCARD;

#ifdef FDT
	if (ofw_bus_status_okay(dev) &&
	    ofw_bus_search_compatible(dev, compat_data)->ocd_data)
                rv = BUS_PROBE_DEFAULT;
#endif

	device_set_desc(dev, "GPIO I2C");

	return (rv);
}

static int
gpioiic_attach(device_t dev)
{
	struct gpioiic_softc *sc = device_get_softc(dev);
	int err;

	sc->dev = dev;

	/* Acquire our gpio pins. */
	err = gpioiic_setup_hinted_pins(sc);
#ifdef FDT
	if (err != 0)
		err = gpioiic_setup_fdt_pins(sc);
#endif
	if (err != 0) {
		device_printf(sc->dev, "no pins configured\n");
		gpioiic_cleanup(sc);
		return (ENXIO);
	}

	/*
	 * Say what we came up with for pin config.
	 * NB: in the !FDT case the controller driver might not be set up enough
	 * for GPIO_GET_BUS() to work.  Also, our parent is the only gpiobus
	 * that can provide our pins.
	 */
	device_printf(dev, "SCL pin: %s:%d, SDA pin: %s:%d\n",
#ifdef FDT
	    device_get_nameunit(GPIO_GET_BUS(sc->sclpin->dev)), sc->sclpin->pin,
	    device_get_nameunit(GPIO_GET_BUS(sc->sdapin->dev)), sc->sdapin->pin);
#else
	    device_get_nameunit(device_get_parent(dev)), sc->sclpin->pin,
	    device_get_nameunit(device_get_parent(dev)), sc->sdapin->pin);
#endif

	/* Add the bitbang driver as our only child; it will add iicbus. */
	device_add_child(sc->dev, "iicbb", -1);
	return (bus_generic_attach(dev));
}

static int
gpioiic_detach(device_t dev)
{
	struct gpioiic_softc *sc = device_get_softc(dev);
	int err;

	if ((err = bus_generic_detach(dev)) != 0)
		return (err);

	gpioiic_cleanup(sc);

	return (0);
}

static devclass_t gpioiic_devclass;

static device_method_t gpioiic_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		gpioiic_probe),
	DEVMETHOD(device_attach,	gpioiic_attach),
	DEVMETHOD(device_detach,	gpioiic_detach),

	/* iicbb interface */
	DEVMETHOD(iicbb_setsda,		gpioiic_setsda),
	DEVMETHOD(iicbb_setscl,		gpioiic_setscl),
	DEVMETHOD(iicbb_getsda,		gpioiic_getsda),
	DEVMETHOD(iicbb_getscl,		gpioiic_getscl),
	DEVMETHOD(iicbb_reset,		gpioiic_reset),

#ifdef FDT
	/* OFW bus interface */
	DEVMETHOD(ofw_bus_get_node,	gpioiic_get_node),
#endif

	DEVMETHOD_END
};

static driver_t gpioiic_driver = {
	"gpioiic",
	gpioiic_methods,
	sizeof(struct gpioiic_softc),
};

DRIVER_MODULE(gpioiic, gpiobus, gpioiic_driver, gpioiic_devclass, 0, 0);
DRIVER_MODULE(gpioiic, simplebus, gpioiic_driver, gpioiic_devclass, 0, 0);
DRIVER_MODULE(iicbb, gpioiic, iicbb_driver, iicbb_devclass, 0, 0);
MODULE_DEPEND(gpioiic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
MODULE_DEPEND(gpioiic, gpiobus, 1, 1, 1);