#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/uio.h>
#include <bus/ppbus/ppbconf.h>
#include "ppbus_if.h"
#include <bus/ppbus/ppbio.h>
#include <bus/iicbus/iiconf.h>
#include <bus/iicbus/iicbus.h>
#include "iicbb_if.h"
static int lpbb_detect(device_t dev);
static void
lpbb_identify(driver_t *driver, device_t parent)
{
device_t dev;
dev = device_find_child(parent, "lpbb", 0);
if (!dev)
BUS_ADD_CHILD(parent, parent, 0, "lpbb", -1);
}
static int
lpbb_probe(device_t dev)
{
if (!lpbb_detect(dev))
return (ENXIO);
device_set_desc(dev, "Parallel I2C bit-banging interface");
return (0);
}
static int
lpbb_attach(device_t dev)
{
device_t bitbang;
bitbang = device_add_child(dev, "iicbb", -1);
device_probe_and_attach(bitbang);
return (0);
}
static int
lpbb_callback(device_t dev, int index, caddr_t *data)
{
device_t ppbus = device_get_parent(dev);
int error = 0;
int how;
switch (index) {
case IIC_REQUEST_BUS:
how = *(int *)data;
error = ppb_request_bus(ppbus, dev, how);
break;
case IIC_RELEASE_BUS:
error = ppb_release_bus(ppbus, dev);
break;
default:
error = EINVAL;
}
return (error);
}
#define SDA_out 0x80
#define SCL_out 0x08
#define SDA_in 0x80
#define SCL_in 0x08
#define ALIM 0x20
#define I2CKEY 0x50
static int
lpbb_getscl(device_t dev)
{
return ((ppb_rstr(device_get_parent(dev)) & SCL_in) == SCL_in);
}
static int
lpbb_getsda(device_t dev)
{
return ((ppb_rstr(device_get_parent(dev)) & SDA_in) == SDA_in);
}
static void
lpbb_setsda(device_t dev, char val)
{
device_t ppbus = device_get_parent(dev);
if(val==0)
ppb_wdtr(ppbus, (u_char)SDA_out);
else
ppb_wdtr(ppbus, (u_char)~SDA_out);
}
static void
lpbb_setscl(device_t dev, unsigned char val)
{
device_t ppbus = device_get_parent(dev);
if(val==0)
ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)&~SCL_out));
else
ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)|SCL_out));
}
static int lpbb_detect(device_t dev)
{
device_t ppbus = device_get_parent(dev);
if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
device_printf(dev, "can't allocate ppbus\n");
return (0);
}
lpbb_setsda(dev, 1);
lpbb_setscl(dev, 1);
if ((ppb_rstr(ppbus) & I2CKEY) ||
((ppb_rstr(ppbus) & ALIM) != ALIM)) {
ppb_release_bus(ppbus, dev);
return (0);
}
ppb_release_bus(ppbus, dev);
return (1);
}
static int
lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
{
device_t ppbus = device_get_parent(dev);
if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
device_printf(dev, "can't allocate ppbus\n");
return (0);
}
lpbb_setsda(dev, 1);
lpbb_setscl(dev, 1);
ppb_release_bus(ppbus, dev);
return (IIC_ENOADDR);
}
static devclass_t lpbb_devclass;
static device_method_t lpbb_methods[] = {
DEVMETHOD(device_identify, lpbb_identify),
DEVMETHOD(device_probe, lpbb_probe),
DEVMETHOD(device_attach, lpbb_attach),
DEVMETHOD(bus_print_child, bus_generic_print_child),
DEVMETHOD(iicbb_callback, lpbb_callback),
DEVMETHOD(iicbb_setsda, lpbb_setsda),
DEVMETHOD(iicbb_setscl, lpbb_setscl),
DEVMETHOD(iicbb_getsda, lpbb_getsda),
DEVMETHOD(iicbb_getscl, lpbb_getscl),
DEVMETHOD(iicbb_reset, lpbb_reset),
DEVMETHOD_END
};
static driver_t lpbb_driver = {
"lpbb",
lpbb_methods,
1,
};
DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, NULL, NULL);
MODULE_DEPEND(lpbb, ppbus, 1, 1, 1);
MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
MODULE_VERSION(lpbb, 1);