#include <sys/cdefs.h>
#include "opt_platform.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/gpio.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <dev/gpio/gpiobusvar.h>
#include <dev/ow/owll.h>
#ifdef FDT
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
static struct ofw_compat_data compat_data[] = {
{"w1-gpio", true},
{NULL, false}
};
OFWBUS_PNP_INFO(compat_data);
SIMPLEBUS_PNP_INFO(compat_data);
#endif
#define OW_PIN 0
#define OWC_GPIOBUS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
#define OWC_GPIOBUS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
#define OWC_GPIOBUS_LOCK_INIT(_sc) \
mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
"owc_gpiobus", MTX_DEF)
#define OWC_GPIOBUS_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
struct owc_gpiobus_softc
{
device_t sc_dev;
gpio_pin_t sc_pin;
struct mtx sc_mtx;
};
static int owc_gpiobus_probe(device_t);
static int owc_gpiobus_attach(device_t);
static int owc_gpiobus_detach(device_t);
static int
owc_gpiobus_probe(device_t dev)
{
int rv;
rv = BUS_PROBE_NOWILDCARD;
#ifdef FDT
if (ofw_bus_status_okay(dev) &&
ofw_bus_search_compatible(dev, compat_data)->ocd_data)
rv = BUS_PROBE_DEFAULT;
#endif
device_set_desc(dev, "GPIO one-wire bus");
return (rv);
}
static int
owc_gpiobus_attach(device_t dev)
{
struct owc_gpiobus_softc *sc;
int err;
sc = device_get_softc(dev);
sc->sc_dev = dev;
#ifdef FDT
err = gpio_pin_get_by_ofw_idx(dev, ofw_bus_get_node(dev), OW_PIN,
&sc->sc_pin);
#else
err = ENOENT;
#endif
if (err != 0 &&
strcmp("gpiobus", device_get_name(device_get_parent(dev))) == 0)
err = gpio_pin_get_by_child_index(dev, OW_PIN, &sc->sc_pin);
if (err != 0) {
device_printf(sc->sc_dev,
"cannot acquire gpio pin (config error)\n");
return (err);
}
OWC_GPIOBUS_LOCK_INIT(sc);
device_add_child(sc->sc_dev, "ow", DEVICE_UNIT_ANY);
bus_delayed_attach_children(dev);
return (0);
}
static int
owc_gpiobus_detach(device_t dev)
{
struct owc_gpiobus_softc *sc;
int err;
sc = device_get_softc(dev);
if ((err = bus_generic_detach(dev)) != 0)
return (err);
gpio_pin_release(sc->sc_pin);
OWC_GPIOBUS_LOCK_DESTROY(sc);
return (0);
}
#define OUTPIN(sc) gpio_pin_setflags((sc)->sc_pin, GPIO_PIN_OUTPUT)
#define INPIN(sc) gpio_pin_setflags((sc)->sc_pin, GPIO_PIN_INPUT)
#define GETPIN(sc, bp) gpio_pin_is_active((sc)->sc_pin, (bp))
#define LOW(sc) gpio_pin_set_active((sc)->sc_pin, false)
static int
owc_gpiobus_write_one(device_t dev, struct ow_timing *t)
{
struct owc_gpiobus_softc *sc;
sc = device_get_softc(dev);
critical_enter();
OUTPIN(sc);
LOW(sc);
DELAY(t->t_low1);
INPIN(sc);
DELAY(t->t_slot - t->t_low1 + t->t_rec);
critical_exit();
return (0);
}
static int
owc_gpiobus_write_zero(device_t dev, struct ow_timing *t)
{
struct owc_gpiobus_softc *sc;
sc = device_get_softc(dev);
critical_enter();
OUTPIN(sc);
LOW(sc);
DELAY(t->t_low0);
INPIN(sc);
DELAY(t->t_slot - t->t_low0 + t->t_rec);
critical_exit();
return (0);
}
static int
owc_gpiobus_read_data(device_t dev, struct ow_timing *t, int *bit)
{
struct owc_gpiobus_softc *sc;
bool sample;
sbintime_t then, now;
sc = device_get_softc(dev);
critical_enter();
then = sbinuptime();
OUTPIN(sc);
LOW(sc);
DELAY(t->t_lowr);
INPIN(sc);
do {
now = sbinuptime();
GETPIN(sc, &sample);
} while (now - then < (t->t_rdv + 2) * SBT_1US && sample == false);
critical_exit();
if (now - then < t->t_rdv * SBT_1US)
*bit = 1;
else
*bit = 0;
do {
now = sbinuptime();
} while (now - then < (t->t_slot + t->t_rec) * SBT_1US);
return (0);
}
static int
owc_gpiobus_reset_and_presence(device_t dev, struct ow_timing *t, int *bit)
{
struct owc_gpiobus_softc *sc;
bool sample;
sc = device_get_softc(dev);
INPIN(sc);
GETPIN(sc, &sample);
if (sample == false) {
*bit = -1;
return (EIO);
}
critical_enter();
OUTPIN(sc);
LOW(sc);
DELAY(t->t_rstl);
INPIN(sc);
DELAY(t->t_pdh + t->t_pdl / 2);
GETPIN(sc, &sample);
*bit = sample;
critical_exit();
DELAY(t->t_rsth - (t->t_pdh + t->t_pdl / 2));
GETPIN(sc, &sample);
if (sample == false) {
*bit = -1;
return (EIO);
}
return (0);
}
static device_method_t owc_gpiobus_methods[] = {
DEVMETHOD(device_probe, owc_gpiobus_probe),
DEVMETHOD(device_attach, owc_gpiobus_attach),
DEVMETHOD(device_detach, owc_gpiobus_detach),
DEVMETHOD(owll_write_one, owc_gpiobus_write_one),
DEVMETHOD(owll_write_zero, owc_gpiobus_write_zero),
DEVMETHOD(owll_read_data, owc_gpiobus_read_data),
DEVMETHOD(owll_reset_and_presence, owc_gpiobus_reset_and_presence),
DEVMETHOD_END
};
static driver_t owc_gpiobus_driver = {
"owc",
owc_gpiobus_methods,
sizeof(struct owc_gpiobus_softc),
};
#ifdef FDT
DRIVER_MODULE(owc_gpiobus, simplebus, owc_gpiobus_driver, 0, 0);
#endif
DRIVER_MODULE(owc_gpiobus, gpiobus, owc_gpiobus_driver, 0, 0);
MODULE_DEPEND(owc_gpiobus, ow, 1, 1, 1);
MODULE_DEPEND(owc_gpiobus, gpiobus, 1, 1, 1);
MODULE_VERSION(owc_gpiobus, 1);