gpioiic.c revision 228258
150472Speter/*-
250199Srgrimes * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
396856Sru * Copyright (c) 2010 Luiz Otavio O Souza
496855Sru * All rights reserved.
550199Srgrimes *
650199Srgrimes * Redistribution and use in source and binary forms, with or without
750199Srgrimes * modification, are permitted provided that the following conditions
850199Srgrimes * are met:
950199Srgrimes * 1. Redistributions of source code must retain the above copyright
1050199Srgrimes *    notice, this list of conditions and the following disclaimer.
1150199Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
1250199Srgrimes *    notice, this list of conditions and the following disclaimer in the
1350199Srgrimes *    documentation and/or other materials provided with the distribution.
1450199Srgrimes *
1550199Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1650199Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1750199Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1850199Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1950199Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2070923Sdougb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2150199Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2250199Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2350203Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2450199Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2550199Srgrimes * SUCH DAMAGE.
2650199Srgrimes */
2750199Srgrimes
2855797Srgrimes#include <sys/cdefs.h>
29172421Sru__FBSDID("$FreeBSD: head/sys/dev/gpio/gpioiic.c 228258 2011-12-04 12:10:24Z adrian $");
3050199Srgrimes
3150199Srgrimes#include <sys/param.h>
3250199Srgrimes#include <sys/systm.h>
3350199Srgrimes#include <sys/bio.h>
3450199Srgrimes#include <sys/bus.h>
3550199Srgrimes#include <sys/conf.h>
3650199Srgrimes#include <sys/kernel.h>
3750199Srgrimes#include <sys/kthread.h>
3850199Srgrimes#include <sys/lock.h>
39217596Suqs#include <sys/malloc.h>
40217596Suqs#include <sys/module.h>
41217596Suqs#include <sys/mutex.h>
42217596Suqs
4350199Srgrimes#include <sys/gpio.h>
4450199Srgrimes#include "gpiobus_if.h"
4550199Srgrimes
4650199Srgrimes#include <dev/iicbus/iiconf.h>
4750199Srgrimes#include <dev/iicbus/iicbus.h>
4850199Srgrimes
4950199Srgrimes#include "iicbb_if.h"
5050199Srgrimes
51#define	SCL_PIN_DEFAULT	0	/* default index of SCL pin on gpiobus */
52#define	SDA_PIN_DEFAULT	1
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	int		scl_pin;
61	int		sda_pin;
62};
63
64static int gpioiic_probe(device_t);
65static int gpioiic_attach(device_t);
66
67/* iicbb interface */
68static void gpioiic_reset_bus(device_t);
69static int gpioiic_callback(device_t, int, caddr_t);
70static void gpioiic_setsda(device_t, int);
71static void gpioiic_setscl(device_t, int);
72static int gpioiic_getsda(device_t);
73static int gpioiic_getscl(device_t);
74static int gpioiic_reset(device_t, u_char, u_char, u_char *);
75
76
77static int
78gpioiic_probe(device_t dev)
79{
80
81	device_set_desc(dev, "GPIO I2C bit-banging driver");
82	return (0);
83}
84
85static int
86gpioiic_attach(device_t dev)
87{
88	struct gpioiic_softc	*sc = device_get_softc(dev);
89	device_t		bitbang;
90
91	sc->sc_dev = dev;
92	sc->sc_busdev = device_get_parent(dev);
93	if (resource_int_value(device_get_name(dev),
94		device_get_unit(dev), "scl", &sc->scl_pin))
95		sc->scl_pin = SCL_PIN_DEFAULT;
96	if (resource_int_value(device_get_name(dev),
97		device_get_unit(dev), "sda", &sc->sda_pin))
98		sc->sda_pin = SDA_PIN_DEFAULT;
99
100	/* add generic bit-banging code */
101	bitbang = device_add_child(dev, "iicbb", -1);
102	device_probe_and_attach(bitbang);
103
104	return (0);
105}
106
107/*
108 * Reset bus by setting SDA first and then SCL.
109 * Must always be called with gpio bus locked.
110 */
111static void
112gpioiic_reset_bus(device_t dev)
113{
114	struct gpioiic_softc		*sc = device_get_softc(dev);
115
116	GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
117	    GPIO_PIN_INPUT);
118	GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
119	    GPIO_PIN_INPUT);
120}
121
122static int
123gpioiic_callback(device_t dev, int index, caddr_t data)
124{
125	struct gpioiic_softc	*sc = device_get_softc(dev);
126	int			error = 0;
127
128	switch (index) {
129	case IIC_REQUEST_BUS:
130		GPIOBUS_LOCK_BUS(sc->sc_busdev);
131		GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
132		GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
133		break;
134	case IIC_RELEASE_BUS:
135		GPIOBUS_LOCK_BUS(sc->sc_busdev);
136		GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
137		GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
138		break;
139	default:
140		error = EINVAL;
141	}
142
143	return(error);
144}
145
146static void
147gpioiic_setsda(device_t dev, int val)
148{
149	struct gpioiic_softc		*sc = device_get_softc(dev);
150
151	GPIOBUS_LOCK_BUS(sc->sc_busdev);
152	if (val == 0) {
153		GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 0);
154		GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
155		    GPIO_PIN_OUTPUT);
156	} else {
157		GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
158		    GPIO_PIN_INPUT);
159	}
160	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
161}
162
163static void
164gpioiic_setscl(device_t dev, int val)
165{
166	struct gpioiic_softc		*sc = device_get_softc(dev);
167
168	GPIOBUS_LOCK_BUS(sc->sc_busdev);
169	if (val == 0) {
170		GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 0);
171		GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
172		    GPIO_PIN_OUTPUT);
173	} else {
174		GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
175		    GPIO_PIN_INPUT);
176	}
177	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
178}
179
180static int
181gpioiic_getscl(device_t dev)
182{
183	struct gpioiic_softc		*sc = device_get_softc(dev);
184	unsigned int			val;
185
186	GPIOBUS_LOCK_BUS(sc->sc_busdev);
187	GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
188	    GPIO_PIN_INPUT);
189	GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, &val);
190	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
191
192	return ((int)val);
193}
194
195static int
196gpioiic_getsda(device_t dev)
197{
198	struct gpioiic_softc		*sc = device_get_softc(dev);
199	unsigned int			val;
200
201	GPIOBUS_LOCK_BUS(sc->sc_busdev);
202	GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
203	    GPIO_PIN_INPUT);
204	GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, &val);
205	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
206
207	return ((int)val);
208}
209
210static int
211gpioiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
212{
213	struct gpioiic_softc		*sc = device_get_softc(dev);
214
215	GPIOBUS_LOCK_BUS(sc->sc_busdev);
216	GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
217
218	gpioiic_reset_bus(sc->sc_dev);
219
220	GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
221	GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
222
223	return (IIC_ENOADDR);
224}
225
226static devclass_t gpioiic_devclass;
227
228static device_method_t gpioiic_methods[] = {
229	/* Device interface */
230	DEVMETHOD(device_probe,		gpioiic_probe),
231	DEVMETHOD(device_attach,	gpioiic_attach),
232	DEVMETHOD(device_detach,	bus_generic_detach),
233
234	/* iicbb interface */
235	DEVMETHOD(iicbb_callback,	gpioiic_callback),
236	DEVMETHOD(iicbb_setsda,		gpioiic_setsda),
237	DEVMETHOD(iicbb_setscl,		gpioiic_setscl),
238	DEVMETHOD(iicbb_getsda,		gpioiic_getsda),
239	DEVMETHOD(iicbb_getscl,		gpioiic_getscl),
240	DEVMETHOD(iicbb_reset,		gpioiic_reset),
241
242	{ 0, 0 }
243};
244
245static driver_t gpioiic_driver = {
246	"gpioiic",
247	gpioiic_methods,
248	sizeof(struct gpioiic_softc),
249};
250
251DRIVER_MODULE(gpioiic, gpiobus, gpioiic_driver, gpioiic_devclass, 0, 0);
252DRIVER_MODULE(iicbb, gpioiic, iicbb_driver, iicbb_devclass, 0, 0);
253MODULE_DEPEND(gpioiic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
254MODULE_DEPEND(gpioiic, gpiobus, 1, 1, 1);
255