2 * Copyright 2013-2015 John Wehle <john@feith.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Amlogic aml8726 I2C driver.
30 * Currently this implementation doesn't take full advantage of the hardware.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
48 #include <machine/bus.h>
50 #include <dev/fdt/fdt_common.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
54 #include <dev/iicbus/iiconf.h>
55 #include <dev/iicbus/iicbus.h>
60 struct aml8726_iic_softc {
62 struct resource *res[1];
67 static struct resource_spec aml8726_iic_spec[] = {
68 { SYS_RES_MEMORY, 0, RF_ACTIVE },
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), \
77 #define AML_I2C_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx);
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)
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)
90 aml8726_iic_probe(device_t dev)
93 if (!ofw_bus_status_okay(dev))
96 if (!ofw_bus_is_compatible(dev, "amlogic,meson6-i2c"))
99 device_set_desc(dev, "Amlogic aml8726 I2C");
101 return (BUS_PROBE_DEFAULT);
105 aml8726_iic_attach(device_t dev)
107 struct aml8726_iic_softc *sc = device_get_softc(dev);
112 if (bus_alloc_resources(dev, aml8726_iic_spec, sc->res)) {
113 device_printf(dev, "can not allocate resources for device\n");
117 AML_I2C_LOCK_INIT(sc);
119 sc->iicbb = device_add_child(dev, "iicbb", -1);
121 if (sc->iicbb == NULL) {
122 device_printf(dev, "could not add iicbb\n");
127 error = device_probe_and_attach(sc->iicbb);
130 device_printf(dev, "could not attach iicbb\n");
137 AML_I2C_LOCK_DESTROY(sc);
138 bus_release_resources(dev, aml8726_iic_spec, sc->res);
144 aml8726_iic_detach(device_t dev)
146 struct aml8726_iic_softc *sc = device_get_softc(dev);
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.
154 * Remember the child before detaching so we can
155 * delete it (bus_generic_detach indirectly zeroes
159 bus_generic_detach(dev);
161 device_delete_child(dev, child);
163 AML_I2C_LOCK_DESTROY(sc);
165 bus_release_resources(dev, aml8726_iic_spec, sc->res);
171 aml8726_iic_child_detached(device_t dev, device_t child)
173 struct aml8726_iic_softc *sc = device_get_softc(dev);
175 if (child == sc->iicbb)
180 aml8726_iic_callback(device_t dev, int index, caddr_t data)
187 aml8726_iic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
189 struct aml8726_iic_softc *sc = device_get_softc(dev);
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));
199 /* Wait for 10 usec */
202 return (IIC_ENOADDR);
206 aml8726_iic_getscl(device_t dev)
208 struct aml8726_iic_softc *sc = device_get_softc(dev);
210 return (CSR_READ_4(sc, AML_I2C_CTRL_REG) & AML_I2C_MANUAL_SCL_I);
214 aml8726_iic_getsda(device_t dev)
216 struct aml8726_iic_softc *sc = device_get_softc(dev);
218 return (CSR_READ_4(sc, AML_I2C_CTRL_REG) & AML_I2C_MANUAL_SDA_I);
222 aml8726_iic_setscl(device_t dev, int val)
224 struct aml8726_iic_softc *sc = device_get_softc(dev);
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) |
236 aml8726_iic_setsda(device_t dev, int val)
238 struct aml8726_iic_softc *sc = device_get_softc(dev);
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) |
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),
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),
260 /* IICBB interface */
261 DEVMETHOD(iicbb_callback, aml8726_iic_callback),
262 DEVMETHOD(iicbb_reset, aml8726_iic_reset),
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),
272 static driver_t aml8726_iic_driver = {
275 sizeof(struct aml8726_iic_softc),
278 static devclass_t aml8726_iic_devclass;
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);