2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Michal Meloun <mmel@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #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/iicbus/iiconf.h>
49 #include <dev/iicbus/iicbus.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
53 #include <dev/iicbus/pmic/act8846.h>
55 #include "regdev_if.h"
57 static struct ofw_compat_data compat_data[] = {
58 {"active-semi,act8846", 1},
62 #define LOCK(_sc) sx_xlock(&(_sc)->lock)
63 #define UNLOCK(_sc) sx_xunlock(&(_sc)->lock)
64 #define LOCK_INIT(_sc) sx_init(&(_sc)->lock, "act8846")
65 #define LOCK_DESTROY(_sc) sx_destroy(&(_sc)->lock);
66 #define ASSERT_LOCKED(_sc) sx_assert(&(_sc)->lock, SA_XLOCKED);
67 #define ASSERT_UNLOCKED(_sc) sx_assert(&(_sc)->lock, SA_UNLOCKED);
71 * Raw register access function.
74 act8846_read(struct act8846_softc *sc, uint8_t reg, uint8_t *val)
78 struct iic_msg msgs[2] = {
79 {0, IIC_M_WR, 1, &addr},
80 {0, IIC_M_RD, 1, val},
83 msgs[0].slave = sc->bus_addr;
84 msgs[1].slave = sc->bus_addr;
87 rv = iicbus_transfer_excl(sc->dev, msgs, 2, IIC_INTRWAIT);
89 device_printf(sc->dev,
90 "Error when reading reg 0x%02X, rv: %d\n", reg, rv);
97 int act8846_read_buf(struct act8846_softc *sc, uint8_t reg, uint8_t *buf,
102 struct iic_msg msgs[2] = {
103 {0, IIC_M_WR, 1, &addr},
104 {0, IIC_M_RD, size, buf},
107 msgs[0].slave = sc->bus_addr;
108 msgs[1].slave = sc->bus_addr;
111 rv = iicbus_transfer_excl(sc->dev, msgs, 2, IIC_INTRWAIT);
113 device_printf(sc->dev,
114 "Error when reading reg 0x%02X, rv: %d\n", reg, rv);
122 act8846_write(struct act8846_softc *sc, uint8_t reg, uint8_t val)
127 struct iic_msg msgs[1] = {
128 {0, IIC_M_WR, 2, data},
131 msgs[0].slave = sc->bus_addr;
135 rv = iicbus_transfer_excl(sc->dev, msgs, 1, IIC_INTRWAIT);
137 device_printf(sc->dev,
138 "Error when writing reg 0x%02X, rv: %d\n", reg, rv);
144 int act8846_write_buf(struct act8846_softc *sc, uint8_t reg, uint8_t *buf,
149 struct iic_msg msgs[2] = {
150 {0, IIC_M_WR, 1, data},
151 {0, IIC_M_WR | IIC_M_NOSTART, size, buf},
154 msgs[0].slave = sc->bus_addr;
155 msgs[1].slave = sc->bus_addr;
158 rv = iicbus_transfer_excl(sc->dev, msgs, 2, IIC_INTRWAIT);
160 device_printf(sc->dev,
161 "Error when writing reg 0x%02X, rv: %d\n", reg, rv);
168 act8846_modify(struct act8846_softc *sc, uint8_t reg, uint8_t clear, uint8_t set)
173 rv = act8846_read(sc, reg, &val);
180 rv = act8846_write(sc, reg, val);
188 act8846_probe(device_t dev)
191 if (!ofw_bus_status_okay(dev))
194 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
197 device_set_desc(dev, "ACT8846 PMIC");
198 return (BUS_PROBE_DEFAULT);
202 act8846_attach(device_t dev)
204 struct act8846_softc *sc;
208 sc = device_get_softc(dev);
210 sc->bus_addr = iicbus_get_addr(dev);
211 node = ofw_bus_get_node(sc->dev);
216 rv = act8846_regulator_attach(sc, node);
220 return (bus_generic_attach(dev));
228 act8846_detach(device_t dev)
230 struct act8846_softc *sc;
232 sc = device_get_softc(dev);
235 return (bus_generic_detach(dev));
238 static device_method_t act8846_methods[] = {
239 /* Device interface */
240 DEVMETHOD(device_probe, act8846_probe),
241 DEVMETHOD(device_attach, act8846_attach),
242 DEVMETHOD(device_detach, act8846_detach),
244 /* Regdev interface */
245 DEVMETHOD(regdev_map, act8846_regulator_map),
250 static devclass_t act8846_devclass;
251 static DEFINE_CLASS_0(act8846_pmu, act8846_driver, act8846_methods,
252 sizeof(struct act8846_softc));
253 EARLY_DRIVER_MODULE(act8846_pmic, iicbus, act8846_driver, act8846_devclass,
254 NULL, NULL, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
255 MODULE_VERSION(act8846_pmic, 1);
256 MODULE_DEPEND(act8846_pmic, iicbus, IICBUS_MINVER, IICBUS_PREFVER,
258 IICBUS_FDT_PNP_INFO(compat_data);