gpioiic.c revision 213277
1/*-
2 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * Copyright (c) 2010 Luiz Otavio O Souza
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/dev/gpio/gpioiic.c 213277 2010-09-29 20:53:33Z gonzo $");
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/bio.h>
34#include <sys/bus.h>
35#include <sys/conf.h>
36#include <sys/kernel.h>
37#include <sys/kthread.h>
38#include <sys/lock.h>
39#include <sys/malloc.h>
40#include <sys/module.h>
41#include <sys/mutex.h>
42
43#include <sys/gpio.h>
44#include "gpiobus_if.h"
45
46#include <dev/iicbus/iiconf.h>
47#include <dev/iicbus/iicbus.h>
48
49#include "iicbb_if.h"
50
51#define	SCL_PIN		0	/* gpiobus mapped pin 6 */
52#define	SDA_PIN		1	/* gpiobus mapped pin 7 */
53
54struct gpioiic_softc
55{
56	device_t	sc_dev;
57	device_t	sc_busdev;
58	struct mtx	sc_mtx;
59	struct cdev	*sc_leddev;
60};
61
62static int gpioiic_probe(device_t);
63static int gpioiic_attach(device_t);
64
65/* iicbb interface */
66static void gpioiic_reset_bus(device_t);
67static int gpioiic_callback(device_t, int, caddr_t);
68static void gpioiic_setsda(device_t, int);
69static void gpioiic_setscl(device_t, int);
70static int gpioiic_getsda(device_t);
71static int gpioiic_getscl(device_t);
72static int gpioiic_reset(device_t, u_char, u_char, u_char *);
73
74
75static int
76gpioiic_probe(device_t dev)
77{
78
79	device_set_desc(dev, "GPIO I2C bit-banging driver");
80	return (0);
81}
82
83static int
84gpioiic_attach(device_t dev)
85{
86	struct gpioiic_softc	*sc = device_get_softc(dev);
87	device_t		bitbang;
88
89	sc->sc_dev = dev;
90	sc->sc_busdev = device_get_parent(dev);
91
92	/* add generic bit-banging code */
93	bitbang = device_add_child(dev, "iicbb", -1);
94	device_probe_and_attach(bitbang);
95
96	return (0);
97}
98
99/*
100 * Reset bus by setting SDA first and then SCL.
101 * Must always be called with gpio bus locked.
102 */
103static void
104gpioiic_reset_bus(device_t dev)
105{
106	struct gpioiic_softc		*sc = device_get_softc(dev);
107
108	GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SDA_PIN,
109	    GPIO_PIN_INPUT);
110	GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SCL_PIN,
111	    GPIO_PIN_INPUT);
112}
113
114static int
115gpioiic_callback(device_t dev, int index, caddr_t data)
116{
117	struct gpioiic_softc	*sc = device_get_softc(dev);
118	int			error = 0;
119
120	switch (index) {
121	case IIC_REQUEST_BUS:
122		GPIOBUS_LOCK_BUS(sc->sc_busdev);
123		GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
124		GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
125		break;
126	case IIC_RELEASE_BUS:
127		GPIOBUS_LOCK_BUS(sc->sc_busdev);
128		GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
129		GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
130		break;
131	default:
132		error = EINVAL;
133	}
134
135	return(error);
136}
137
138static void
139gpioiic_setsda(device_t dev, int val)
140{
141	struct gpioiic_softc		*sc = device_get_softc(dev);
142
143	GPIOBUS_LOCK_BUS(sc->sc_busdev);
144	if (val == 0) {
145		GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, SDA_PIN, 0);
146		GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SDA_PIN,
147		    GPIO_PIN_OUTPUT);
148	} else {
149		GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SDA_PIN,
150		    GPIO_PIN_INPUT);
151	}
152	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
153}
154
155static void
156gpioiic_setscl(device_t dev, int val)
157{
158	struct gpioiic_softc		*sc = device_get_softc(dev);
159
160	GPIOBUS_LOCK_BUS(sc->sc_busdev);
161	if (val == 0) {
162		GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, SCL_PIN, 0);
163		GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SCL_PIN,
164		    GPIO_PIN_OUTPUT);
165	} else {
166		GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SCL_PIN,
167		    GPIO_PIN_INPUT);
168	}
169	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
170}
171
172static int
173gpioiic_getscl(device_t dev)
174{
175	struct gpioiic_softc		*sc = device_get_softc(dev);
176	unsigned int			val;
177
178	GPIOBUS_LOCK_BUS(sc->sc_busdev);
179	GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SCL_PIN,
180	    GPIO_PIN_INPUT);
181	GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, SCL_PIN, &val);
182	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
183
184	return ((int)val);
185}
186
187static int
188gpioiic_getsda(device_t dev)
189{
190	struct gpioiic_softc		*sc = device_get_softc(dev);
191	unsigned int			val;
192
193	GPIOBUS_LOCK_BUS(sc->sc_busdev);
194	GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SDA_PIN,
195	    GPIO_PIN_INPUT);
196	GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, SDA_PIN, &val);
197	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
198
199	return ((int)val);
200}
201
202static int
203gpioiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
204{
205	struct gpioiic_softc		*sc = device_get_softc(dev);
206
207	GPIOBUS_LOCK_BUS(sc->sc_busdev);
208	GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
209
210	gpioiic_reset_bus(sc->sc_dev);
211
212	GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
213	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
214
215	return (IIC_ENOADDR);
216}
217
218static devclass_t gpioiic_devclass;
219
220static device_method_t gpioiic_methods[] = {
221	/* Device interface */
222	DEVMETHOD(device_probe,		gpioiic_probe),
223	DEVMETHOD(device_attach,	gpioiic_attach),
224	DEVMETHOD(device_detach,	bus_generic_detach),
225
226	/* iicbb interface */
227	DEVMETHOD(iicbb_callback,	gpioiic_callback),
228	DEVMETHOD(iicbb_setsda,		gpioiic_setsda),
229	DEVMETHOD(iicbb_setscl,		gpioiic_setscl),
230	DEVMETHOD(iicbb_getsda,		gpioiic_getsda),
231	DEVMETHOD(iicbb_getscl,		gpioiic_getscl),
232	DEVMETHOD(iicbb_reset,		gpioiic_reset),
233
234	{ 0, 0 }
235};
236
237static driver_t gpioiic_driver = {
238	"gpioiic",
239	gpioiic_methods,
240	sizeof(struct gpioiic_softc),
241};
242
243DRIVER_MODULE(gpioiic, gpiobus, gpioiic_driver, gpioiic_devclass, 0, 0);
244DRIVER_MODULE(iicbb, gpioiic, iicbb_driver, iicbb_devclass, 0, 0);
245MODULE_DEPEND(gpioiic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
246MODULE_DEPEND(gpioiic, gpiobus, 1, 1, 1);
247