2 * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
44 #include <machine/bus.h>
46 #include <dev/extres/regulator/regulator.h>
47 #include <dev/fdt/fdt_pinctrl.h>
48 #include <dev/gpio/gpiobusvar.h>
49 #include <dev/iicbus/iiconf.h>
50 #include <dev/iicbus/iicbus.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
54 #include <gnu/dts/include/dt-bindings/mfd/as3722.h>
57 #include "regdev_if.h"
61 static struct ofw_compat_data compat_data[] = {
66 #define LOCK(_sc) sx_xlock(&(_sc)->lock)
67 #define UNLOCK(_sc) sx_xunlock(&(_sc)->lock)
68 #define LOCK_INIT(_sc) sx_init(&(_sc)->lock, "as3722")
69 #define LOCK_DESTROY(_sc) sx_destroy(&(_sc)->lock);
70 #define ASSERT_LOCKED(_sc) sx_assert(&(_sc)->lock, SA_XLOCKED);
71 #define ASSERT_UNLOCKED(_sc) sx_assert(&(_sc)->lock, SA_UNLOCKED);
73 #define AS3722_DEVICE_ID 0x0C
76 * Raw register access function.
79 as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val)
83 struct iic_msg msgs[2] = {
84 {0, IIC_M_WR, 1, &addr},
85 {0, IIC_M_RD, 1, val},
88 msgs[0].slave = sc->bus_addr;
89 msgs[1].slave = sc->bus_addr;
92 rv = iicbus_transfer(sc->dev, msgs, 2);
94 device_printf(sc->dev,
95 "Error when reading reg 0x%02X, rv: %d\n", reg, rv);
102 int as3722_read_buf(struct as3722_softc *sc, uint8_t reg, uint8_t *buf,
107 struct iic_msg msgs[2] = {
108 {0, IIC_M_WR, 1, &addr},
109 {0, IIC_M_RD, size, buf},
112 msgs[0].slave = sc->bus_addr;
113 msgs[1].slave = sc->bus_addr;
116 rv = iicbus_transfer(sc->dev, msgs, 2);
118 device_printf(sc->dev,
119 "Error when reading reg 0x%02X, rv: %d\n", reg, rv);
127 as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val)
132 struct iic_msg msgs[1] = {
133 {0, IIC_M_WR, 2, data},
136 msgs[0].slave = sc->bus_addr;
140 rv = iicbus_transfer(sc->dev, msgs, 1);
142 device_printf(sc->dev,
143 "Error when writing reg 0x%02X, rv: %d\n", reg, rv);
149 int as3722_write_buf(struct as3722_softc *sc, uint8_t reg, uint8_t *buf,
154 struct iic_msg msgs[2] = {
155 {0, IIC_M_WR, 1, data},
156 {0, IIC_M_WR | IIC_M_NOSTART, size, buf},
159 msgs[0].slave = sc->bus_addr;
160 msgs[1].slave = sc->bus_addr;
163 rv = iicbus_transfer(sc->dev, msgs, 2);
165 device_printf(sc->dev,
166 "Error when writing reg 0x%02X, rv: %d\n", reg, rv);
173 as3722_modify(struct as3722_softc *sc, uint8_t reg, uint8_t clear, uint8_t set)
178 rv = as3722_read(sc, reg, &val);
185 rv = as3722_write(sc, reg, val);
193 as3722_get_version(struct as3722_softc *sc)
198 /* Verify AS3722 ID and version. */
199 rv = RD1(sc, AS3722_ASIC_ID1, ®);
203 if (reg != AS3722_DEVICE_ID) {
204 device_printf(sc->dev, "Invalid chip ID is 0x%x\n", reg);
208 rv = RD1(sc, AS3722_ASIC_ID2, &sc->chip_rev);
213 device_printf(sc->dev, "AS3722 rev: 0x%x\n", sc->chip_rev);
218 as3722_init(struct as3722_softc *sc)
225 reg |= AS3722_INT_PULL_UP;
227 reg |= AS3722_I2C_PULL_UP;
229 rv = RM1(sc, AS3722_IO_VOLTAGE,
230 AS3722_INT_PULL_UP | AS3722_I2C_PULL_UP, reg);
234 /* mask interrupts */
235 rv = WR1(sc, AS3722_INTERRUPT_MASK1, 0);
238 rv = WR1(sc, AS3722_INTERRUPT_MASK2, 0);
241 rv = WR1(sc, AS3722_INTERRUPT_MASK3, 0);
244 rv = WR1(sc, AS3722_INTERRUPT_MASK4, 0);
251 as3722_parse_fdt(struct as3722_softc *sc, phandle_t node)
255 OF_hasprop(node, "ams,enable-internal-int-pullup") ? 1 : 0;
257 OF_hasprop(node, "ams,enable-internal-i2c-pullup") ? 1 : 0;
262 as3722_intr(void *arg)
264 struct as3722_softc *sc;
266 sc = (struct as3722_softc *)arg;
267 /* XXX Finish temperature alarms. */
271 as3722_probe(device_t dev)
274 if (!ofw_bus_status_okay(dev))
277 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
280 device_set_desc(dev, "AS3722 PMIC");
281 return (BUS_PROBE_DEFAULT);
285 as3722_attach(device_t dev)
287 struct as3722_softc *sc;
292 sc = device_get_softc(dev);
294 sc->bus_addr = iicbus_get_addr(dev);
295 node = ofw_bus_get_node(sc->dev);
296 dname = device_get_name(dev);
297 dunit = device_get_unit(dev);
302 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
304 if (sc->irq_res == NULL) {
305 device_printf(dev, "Cannot allocate interrupt.\n");
310 rv = as3722_parse_fdt(sc, node);
313 rv = as3722_get_version(sc);
316 rv = as3722_init(sc);
319 rv = as3722_regulator_attach(sc, node);
322 rv = as3722_gpio_attach(sc, node);
325 rv = as3722_rtc_attach(sc, node);
329 fdt_pinctrl_register(dev, NULL);
330 fdt_pinctrl_configure_by_name(dev, "default");
332 /* Setup interrupt. */
333 rv = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
334 NULL, as3722_intr, sc, &sc->irq_h);
336 device_printf(dev, "Cannot setup interrupt.\n");
339 return (bus_generic_attach(dev));
342 if (sc->irq_h != NULL)
343 bus_teardown_intr(dev, sc->irq_res, sc->irq_h);
344 if (sc->irq_res != NULL)
345 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
351 as3722_detach(device_t dev)
353 struct as3722_softc *sc;
355 sc = device_get_softc(dev);
356 if (sc->irq_h != NULL)
357 bus_teardown_intr(dev, sc->irq_res, sc->irq_h);
358 if (sc->irq_res != NULL)
359 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
362 return (bus_generic_detach(dev));
366 as3722_gpio_get_node(device_t bus, device_t dev)
369 /* We only have one child, the GPIO bus, which needs our own node. */
370 return (ofw_bus_get_node(bus));
373 static device_method_t as3722_methods[] = {
374 /* Device interface */
375 DEVMETHOD(device_probe, as3722_probe),
376 DEVMETHOD(device_attach, as3722_attach),
377 DEVMETHOD(device_detach, as3722_detach),
379 /* Regdev interface */
380 DEVMETHOD(regdev_map, as3722_regulator_map),
383 DEVMETHOD(clock_gettime, as3722_rtc_gettime),
384 DEVMETHOD(clock_settime, as3722_rtc_settime),
386 /* GPIO protocol interface */
387 DEVMETHOD(gpio_get_bus, as3722_gpio_get_bus),
388 DEVMETHOD(gpio_pin_max, as3722_gpio_pin_max),
389 DEVMETHOD(gpio_pin_getname, as3722_gpio_pin_getname),
390 DEVMETHOD(gpio_pin_getflags, as3722_gpio_pin_getflags),
391 DEVMETHOD(gpio_pin_getcaps, as3722_gpio_pin_getcaps),
392 DEVMETHOD(gpio_pin_setflags, as3722_gpio_pin_setflags),
393 DEVMETHOD(gpio_pin_get, as3722_gpio_pin_get),
394 DEVMETHOD(gpio_pin_set, as3722_gpio_pin_set),
395 DEVMETHOD(gpio_pin_toggle, as3722_gpio_pin_toggle),
396 DEVMETHOD(gpio_map_gpios, as3722_gpio_map_gpios),
398 /* fdt_pinctrl interface */
399 DEVMETHOD(fdt_pinctrl_configure, as3722_pinmux_configure),
401 /* ofw_bus interface */
402 DEVMETHOD(ofw_bus_get_node, as3722_gpio_get_node),
407 static devclass_t as3722_devclass;
408 DEFINE_CLASS_0(gpio, as3722_driver, as3722_methods,
409 sizeof(struct as3722_softc));
410 EARLY_DRIVER_MODULE(as3722, iicbus, as3722_driver, as3722_devclass,