]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/amlogic/aml8726/aml8726_i2c.c
MFC r306902:
[FreeBSD/FreeBSD.git] / sys / arm / amlogic / aml8726 / aml8726_i2c.c
1 /*-
2  * Copyright 2013-2015 John Wehle <john@feith.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * Amlogic aml8726 I2C driver.
29  *
30  * Currently this implementation doesn't take full advantage of the hardware.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/mbuf.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/rman.h>
47
48 #include <machine/bus.h>
49
50 #include <dev/fdt/fdt_common.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
53
54 #include <dev/iicbus/iiconf.h>
55 #include <dev/iicbus/iicbus.h>
56
57 #include "iicbb_if.h"
58
59
60 struct aml8726_iic_softc {
61         device_t        dev;
62         struct resource *res[1];
63         struct mtx      mtx;
64         device_t        iicbb;
65 };
66
67 static struct resource_spec aml8726_iic_spec[] = {
68         { SYS_RES_MEMORY, 0, RF_ACTIVE },
69         { -1, 0 }
70 };
71
72 #define AML_I2C_LOCK(sc)                mtx_lock(&(sc)->mtx)
73 #define AML_I2C_UNLOCK(sc)              mtx_unlock(&(sc)->mtx)
74 #define AML_I2C_LOCK_INIT(sc)           \
75     mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev),        \
76     "i2c", MTX_DEF)
77 #define AML_I2C_LOCK_DESTROY(sc)        mtx_destroy(&(sc)->mtx);
78
79 #define AML_I2C_CTRL_REG                0
80 #define AML_I2C_MANUAL_SDA_I            (1 << 26)
81 #define AML_I2C_MANUAL_SCL_I            (1 << 25)
82 #define AML_I2C_MANUAL_SDA_O            (1 << 24)
83 #define AML_I2C_MANUAL_SCL_O            (1 << 23)
84 #define AML_I2C_MANUAL_EN               (1 << 22)
85
86 #define CSR_WRITE_4(sc, reg, val)       bus_write_4((sc)->res[0], reg, (val))
87 #define CSR_READ_4(sc, reg)             bus_read_4((sc)->res[0], reg)
88
89 static int
90 aml8726_iic_probe(device_t dev)
91 {
92
93         if (!ofw_bus_status_okay(dev))
94                 return (ENXIO);
95
96         if (!ofw_bus_is_compatible(dev, "amlogic,meson6-i2c"))
97                 return (ENXIO);
98
99         device_set_desc(dev, "Amlogic aml8726 I2C");
100
101         return (BUS_PROBE_DEFAULT);
102 }
103
104 static int
105 aml8726_iic_attach(device_t dev)
106 {
107         struct aml8726_iic_softc *sc = device_get_softc(dev);
108         int error;
109
110         sc->dev = dev;
111
112         if (bus_alloc_resources(dev, aml8726_iic_spec, sc->res)) {
113                 device_printf(dev, "can not allocate resources for device\n");
114                 return (ENXIO);
115         }
116
117         AML_I2C_LOCK_INIT(sc);
118
119         sc->iicbb = device_add_child(dev, "iicbb", -1);
120
121         if (sc->iicbb == NULL) {
122                 device_printf(dev, "could not add iicbb\n");
123                 error = ENXIO;
124                 goto fail;
125         }
126
127         error = device_probe_and_attach(sc->iicbb);
128
129         if (error) {
130                 device_printf(dev, "could not attach iicbb\n");
131                 goto fail;
132         }
133
134         return (0);
135
136 fail:
137         AML_I2C_LOCK_DESTROY(sc);
138         bus_release_resources(dev, aml8726_iic_spec, sc->res);
139
140         return (error);
141 }
142
143 static int
144 aml8726_iic_detach(device_t dev)
145 {
146         struct aml8726_iic_softc *sc = device_get_softc(dev);
147         device_t child;
148
149         /*
150          * Detach the children before recursively deleting
151          * in case a child has a pointer to a grandchild
152          * which is used by the child's detach routine.
153          *
154          * Remember the child before detaching so we can
155          * delete it (bus_generic_detach indirectly zeroes
156          * sc->child_dev).
157          */
158         child = sc->iicbb;
159         bus_generic_detach(dev);
160         if (child)
161                 device_delete_child(dev, child);
162
163         AML_I2C_LOCK_DESTROY(sc);
164
165         bus_release_resources(dev, aml8726_iic_spec, sc->res);
166
167         return (0);
168 }
169
170 static void
171 aml8726_iic_child_detached(device_t dev, device_t child)
172 {
173         struct aml8726_iic_softc *sc = device_get_softc(dev);
174
175         if (child == sc->iicbb)
176                 sc->iicbb = NULL;
177 }
178
179 static int
180 aml8726_iic_callback(device_t dev, int index, caddr_t data)
181 {
182
183         return (0);
184 }
185
186 static int
187 aml8726_iic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
188 {
189         struct aml8726_iic_softc *sc = device_get_softc(dev);
190
191         AML_I2C_LOCK(sc);
192
193         CSR_WRITE_4(sc, AML_I2C_CTRL_REG,
194             (CSR_READ_4(sc, AML_I2C_CTRL_REG) | AML_I2C_MANUAL_SDA_O |
195             AML_I2C_MANUAL_SCL_O | AML_I2C_MANUAL_EN));
196
197         AML_I2C_UNLOCK(sc);
198
199         /* Wait for 10 usec */
200         DELAY(10);
201
202         return (IIC_ENOADDR);
203 }
204
205 static int
206 aml8726_iic_getscl(device_t dev)
207 {
208         struct aml8726_iic_softc *sc = device_get_softc(dev);
209
210         return (CSR_READ_4(sc, AML_I2C_CTRL_REG) & AML_I2C_MANUAL_SCL_I);
211 }
212
213 static int
214 aml8726_iic_getsda(device_t dev)
215 {
216         struct aml8726_iic_softc *sc = device_get_softc(dev);
217
218         return (CSR_READ_4(sc, AML_I2C_CTRL_REG) & AML_I2C_MANUAL_SDA_I);
219 }
220
221 static void
222 aml8726_iic_setscl(device_t dev, int val)
223 {
224         struct aml8726_iic_softc *sc = device_get_softc(dev);
225
226         AML_I2C_LOCK(sc);
227
228         CSR_WRITE_4(sc, AML_I2C_CTRL_REG, ((CSR_READ_4(sc, AML_I2C_CTRL_REG) &
229             ~AML_I2C_MANUAL_SCL_O) | (val ? AML_I2C_MANUAL_SCL_O : 0) |
230             AML_I2C_MANUAL_EN));
231
232         AML_I2C_UNLOCK(sc);
233 }
234
235 static void
236 aml8726_iic_setsda(device_t dev, int val)
237 {
238         struct aml8726_iic_softc *sc = device_get_softc(dev);
239
240         AML_I2C_LOCK(sc);
241
242         CSR_WRITE_4(sc, AML_I2C_CTRL_REG, ((CSR_READ_4(sc, AML_I2C_CTRL_REG) &
243             ~AML_I2C_MANUAL_SDA_O) | (val ? AML_I2C_MANUAL_SDA_O : 0) |
244             AML_I2C_MANUAL_EN));
245
246         AML_I2C_UNLOCK(sc);
247 }
248
249 static device_method_t aml8726_iic_methods[] = {
250         /* Device interface */
251         DEVMETHOD(device_probe,         aml8726_iic_probe),
252         DEVMETHOD(device_attach,        aml8726_iic_attach),
253         DEVMETHOD(device_detach,        aml8726_iic_detach),
254
255         /* bus interface */
256         DEVMETHOD(bus_child_detached,   aml8726_iic_child_detached),
257         DEVMETHOD(bus_print_child,      bus_generic_print_child),
258         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
259
260         /* IICBB interface */
261         DEVMETHOD(iicbb_callback,       aml8726_iic_callback),
262         DEVMETHOD(iicbb_reset,          aml8726_iic_reset),
263
264         DEVMETHOD(iicbb_getscl,         aml8726_iic_getscl),
265         DEVMETHOD(iicbb_getsda,         aml8726_iic_getsda),
266         DEVMETHOD(iicbb_setscl,         aml8726_iic_setscl),
267         DEVMETHOD(iicbb_setsda,         aml8726_iic_setsda),
268
269         DEVMETHOD_END
270 };
271
272 static driver_t aml8726_iic_driver = {
273         "aml8726_iic",
274         aml8726_iic_methods,
275         sizeof(struct aml8726_iic_softc),
276 };
277
278 static devclass_t aml8726_iic_devclass;
279
280 DRIVER_MODULE(aml8726_iic, simplebus, aml8726_iic_driver,
281     aml8726_iic_devclass, 0, 0);
282 DRIVER_MODULE(iicbb, aml8726_iic, iicbb_driver, iicbb_devclass, 0, 0);
283 MODULE_DEPEND(aml8726_iic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
284 MODULE_VERSION(aml8726_iic, 1);