]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/gpio/gpioiic.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / dev / gpio / gpioiic.c
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$");
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_DEFAULT 0       /* default index of SCL pin on gpiobus */
52 #define SDA_PIN_DEFAULT 1
53
54 struct gpioiic_softc 
55 {
56         device_t        sc_dev;
57         device_t        sc_busdev;
58         struct cdev     *sc_leddev;
59         int             scl_pin;
60         int             sda_pin;
61 };
62
63 static int gpioiic_probe(device_t);
64 static int gpioiic_attach(device_t);
65
66 /* iicbb interface */
67 static void gpioiic_reset_bus(device_t);
68 static int gpioiic_callback(device_t, int, caddr_t);
69 static void gpioiic_setsda(device_t, int);
70 static void gpioiic_setscl(device_t, int);
71 static int gpioiic_getsda(device_t);
72 static int gpioiic_getscl(device_t);
73 static int gpioiic_reset(device_t, u_char, u_char, u_char *);
74
75
76 static int
77 gpioiic_probe(device_t dev)
78 {
79
80         device_set_desc(dev, "GPIO I2C bit-banging driver");
81         return (0);
82 }
83
84 static int
85 gpioiic_attach(device_t dev)
86 {
87         struct gpioiic_softc    *sc = device_get_softc(dev);
88         device_t                bitbang;
89
90         sc->sc_dev = dev;
91         sc->sc_busdev = device_get_parent(dev);
92         if (resource_int_value(device_get_name(dev),
93                 device_get_unit(dev), "scl", &sc->scl_pin))
94                 sc->scl_pin = SCL_PIN_DEFAULT;
95         if (resource_int_value(device_get_name(dev),
96                 device_get_unit(dev), "sda", &sc->sda_pin))
97                 sc->sda_pin = SDA_PIN_DEFAULT;
98
99         /* add generic bit-banging code */
100         bitbang = device_add_child(dev, "iicbb", -1);
101         device_probe_and_attach(bitbang);
102
103         return (0);
104 }
105
106 /*
107  * Reset bus by setting SDA first and then SCL. 
108  * Must always be called with gpio bus locked.
109  */
110 static void
111 gpioiic_reset_bus(device_t dev)
112 {
113         struct gpioiic_softc            *sc = device_get_softc(dev);
114
115         GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
116             GPIO_PIN_INPUT);
117         GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
118             GPIO_PIN_INPUT);
119 }
120
121 static int
122 gpioiic_callback(device_t dev, int index, caddr_t data)
123 {
124         struct gpioiic_softc    *sc = device_get_softc(dev);
125         int                     error = 0;
126
127         switch (index) {
128         case IIC_REQUEST_BUS:
129                 GPIOBUS_LOCK_BUS(sc->sc_busdev);
130                 GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
131                 GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
132                 break;
133         case IIC_RELEASE_BUS:
134                 GPIOBUS_LOCK_BUS(sc->sc_busdev);
135                 GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
136                 GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
137                 break;
138         default:
139                 error = EINVAL;
140         }
141
142         return(error);
143 }
144
145 static void
146 gpioiic_setsda(device_t dev, int val)
147 {
148         struct gpioiic_softc            *sc = device_get_softc(dev);
149
150         if (val == 0) {
151                 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 0);
152                 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
153                     GPIO_PIN_OUTPUT);
154         } else {
155                 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
156                     GPIO_PIN_INPUT);
157         }
158 }
159
160 static void
161 gpioiic_setscl(device_t dev, int val)
162 {
163         struct gpioiic_softc            *sc = device_get_softc(dev);
164
165         if (val == 0) {
166                 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 0);
167                 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
168                     GPIO_PIN_OUTPUT);
169         } else {
170                 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
171                     GPIO_PIN_INPUT);
172         }
173 }
174
175 static int
176 gpioiic_getscl(device_t dev)
177 {
178         struct gpioiic_softc            *sc = device_get_softc(dev);
179         unsigned int                    val;
180
181         GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
182             GPIO_PIN_INPUT);
183         GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, &val);
184
185         return ((int)val);
186 }
187
188 static int
189 gpioiic_getsda(device_t dev)
190 {
191         struct gpioiic_softc            *sc = device_get_softc(dev);
192         unsigned int                    val;
193
194         GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
195             GPIO_PIN_INPUT);
196         GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, &val);
197
198         return ((int)val);
199 }
200
201 static int
202 gpioiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
203 {
204         struct gpioiic_softc            *sc = device_get_softc(dev);
205
206         GPIOBUS_LOCK_BUS(sc->sc_busdev);
207         GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
208
209         gpioiic_reset_bus(sc->sc_dev);
210
211         GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
212         GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
213
214         return (IIC_ENOADDR);
215 }
216
217 static devclass_t gpioiic_devclass;
218
219 static device_method_t gpioiic_methods[] = {
220         /* Device interface */
221         DEVMETHOD(device_probe,         gpioiic_probe),
222         DEVMETHOD(device_attach,        gpioiic_attach),
223         DEVMETHOD(device_detach,        bus_generic_detach),
224
225         /* iicbb interface */
226         DEVMETHOD(iicbb_callback,       gpioiic_callback),
227         DEVMETHOD(iicbb_setsda,         gpioiic_setsda),
228         DEVMETHOD(iicbb_setscl,         gpioiic_setscl),
229         DEVMETHOD(iicbb_getsda,         gpioiic_getsda),
230         DEVMETHOD(iicbb_getscl,         gpioiic_getscl),
231         DEVMETHOD(iicbb_reset,          gpioiic_reset),
232
233         { 0, 0 }
234 };
235
236 static driver_t gpioiic_driver = {
237         "gpioiic",
238         gpioiic_methods,
239         sizeof(struct gpioiic_softc),
240 };
241
242 DRIVER_MODULE(gpioiic, gpiobus, gpioiic_driver, gpioiic_devclass, 0, 0);
243 DRIVER_MODULE(iicbb, gpioiic, iicbb_driver, iicbb_devclass, 0, 0);
244 MODULE_DEPEND(gpioiic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
245 MODULE_DEPEND(gpioiic, gpiobus, 1, 1, 1);