#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/rman.h>
#include <sys/sysctl.h>
#include <machine/bus.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/clk/clk.h>
#include <dev/hwreset/hwreset.h>
#include <dev/evdev/input.h>
#include <dev/evdev/evdev.h>
#define READ(_sc, _r) bus_read_4((_sc)->res[0], (_r))
#define WRITE(_sc, _r, _v) bus_write_4((_sc)->res[0], (_r), (_v))
#define AW_IR_CTL 0x00
#define AW_IR_CTL_GEN (1 << 0)
#define AW_IR_CTL_RXEN (1 << 1)
#define AW_IR_CTL_MD (1 << 4) | (1 << 5)
#define AW_IR_RXCTL 0x10
#define AW_IR_RXCTL_RPPI (1 << 2)
#define AW_IR_RXFIFO 0x20
#define AW_IR_RXINT 0x2C
#define AW_IR_RXINT_ROI_EN (1 << 0)
#define AW_IR_RXINT_RPEI_EN (1 << 1)
#define AW_IR_RXINT_RAI_EN (1 << 4)
#define AW_IR_RXINT_RAL(val) ((val) << 8)
#define AW_IR_RXSTA 0x30
#define AW_IR_RXSTA_COUNTER(val) (((val) >> 8) & (sc->fifo_size * 2 - 1))
#define AW_IR_RXSTA_CLEARALL 0xff
#define AW_IR_CIR 0x34
#define AW_IR_RXFILT_VAL (((8) & 0x3f) << 2)
#define AW_IR_RXIDLE_VAL (((2) & 0xff) << 8)
#define VAL_MASK 0x80
#define PERIOD_MASK 0x7f
#define AW_IR_BASE_CLK 3000000
#define AW_IR_SAMPLE_64 (0 << 0)
#define AW_IR_SAMPLE_128 (1 << 0)
#define AW_IR_ERROR_CODE 0xffffffff
#define AW_IR_REPEAT_CODE 0x0
#define AW_IR_L1_MIN 80
#define AW_IR_L0_MIN 40
#define AW_IR_PMAX 26
#define AW_IR_DMID 26
#define AW_IR_DMAX 53
#define AW_IR_ACTIVE_T_VAL AW_IR_L1_MIN
#define AW_IR_ACTIVE_T (((AW_IR_ACTIVE_T_VAL - 1) & 0xff) << 16)
#define AW_IR_ACTIVE_T_C_VAL 0
#define AW_IR_ACTIVE_T_C ((AW_IR_ACTIVE_T_C_VAL & 0xff) << 23)
#define CODE_MASK 0x00ff00ff
#define INV_CODE_MASK 0xff00ff00
#define VALID_CODE_MASK 0x00ff0000
enum {
A10_IR = 1,
A13_IR,
A31_IR,
};
#define AW_IR_RAW_BUF_SIZE 128
SYSCTL_NODE(_hw, OID_AUTO, aw_cir, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
"aw_cir driver");
static int aw_cir_debug = 0;
SYSCTL_INT(_hw_aw_cir, OID_AUTO, debug, CTLFLAG_RWTUN, &aw_cir_debug, 0,
"Debug 1=on 0=off");
struct aw_ir_softc {
device_t dev;
struct resource *res[2];
void * intrhand;
int fifo_size;
int dcnt;
unsigned char buf[AW_IR_RAW_BUF_SIZE];
struct evdev_dev *sc_evdev;
};
static struct resource_spec aw_ir_spec[] = {
{ SYS_RES_MEMORY, 0, RF_ACTIVE },
{ SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
{ -1, 0 }
};
static struct ofw_compat_data compat_data[] = {
{ "allwinner,sun4i-a10-ir", A10_IR },
{ "allwinner,sun5i-a13-ir", A13_IR },
{ "allwinner,sun6i-a31-ir", A31_IR },
{ NULL, 0 }
};
static void
aw_ir_buf_reset(struct aw_ir_softc *sc)
{
sc->dcnt = 0;
}
static void
aw_ir_buf_write(struct aw_ir_softc *sc, unsigned char data)
{
if (sc->dcnt < AW_IR_RAW_BUF_SIZE)
sc->buf[sc->dcnt++] = data;
else
if (bootverbose)
device_printf(sc->dev, "IR RX Buffer Full!\n");
}
static int
aw_ir_buf_full(struct aw_ir_softc *sc)
{
return (sc->dcnt >= AW_IR_RAW_BUF_SIZE);
}
static unsigned char
aw_ir_read_data(struct aw_ir_softc *sc)
{
return (unsigned char)(READ(sc, AW_IR_RXFIFO) & 0xff);
}
static unsigned long
aw_ir_decode_packets(struct aw_ir_softc *sc)
{
unsigned int len, code;
unsigned int active_delay;
unsigned char val, last;
int i, bitcount;
if (bootverbose && __predict_false(aw_cir_debug) != 0)
device_printf(sc->dev, "sc->dcnt = %d\n", sc->dcnt);
active_delay = AW_IR_ACTIVE_T_VAL *
(AW_IR_ACTIVE_T_C_VAL != 0 ? 128 : 1);
len = active_delay;
if (bootverbose && __predict_false(aw_cir_debug) != 0)
device_printf(sc->dev, "Initial len: %d\n", len);
for (i = 0; i < sc->dcnt; i++) {
val = sc->buf[i];
if (val & VAL_MASK)
len += (val & PERIOD_MASK) + 1;
else {
if (len > AW_IR_L1_MIN)
break;
len = 0;
}
}
if (bootverbose && __predict_false(aw_cir_debug) != 0)
device_printf(sc->dev, "len = %d\n", len);
if ((val & VAL_MASK) || (len <= AW_IR_L1_MIN)) {
if (bootverbose && __predict_false(aw_cir_debug) != 0)
device_printf(sc->dev, "Bit separator error\n");
goto error_code;
}
len = 0;
for (; i < sc->dcnt; i++) {
val = sc->buf[i];
if (val & VAL_MASK) {
if(len > AW_IR_L0_MIN)
break;
len = 0;
} else
len += (val & PERIOD_MASK) + 1;
}
if ((!(val & VAL_MASK)) || (len <= AW_IR_L0_MIN)) {
if (bootverbose && __predict_false(aw_cir_debug) != 0)
device_printf(sc->dev, "Bit length error\n");
goto error_code;
}
code = 0;
bitcount = 0;
last = 1;
len = 0;
for (; i < sc->dcnt; i++) {
val = sc->buf[i];
if (last) {
if (val & VAL_MASK)
len += (val & PERIOD_MASK) + 1;
else {
if (len > AW_IR_PMAX) {
if (bootverbose)
device_printf(sc->dev,
"Pulse error, len=%d\n",
len);
goto error_code;
}
last = 0;
len = (val & PERIOD_MASK) + 1;
}
} else {
if (val & VAL_MASK) {
if (len > AW_IR_DMAX) {
if (bootverbose)
device_printf(sc->dev,
"Distance error, len=%d\n",
len);
goto error_code;
} else {
if (len > AW_IR_DMID) {
code |= 1 << bitcount;
}
bitcount++;
if (bitcount == 32)
break;
}
last = 1;
len = (val & PERIOD_MASK) + 1;
} else
len += (val & PERIOD_MASK) + 1;
}
}
return (code);
error_code:
return (AW_IR_ERROR_CODE);
}
static int
aw_ir_validate_code(unsigned long code)
{
unsigned long v1, v2;
v1 = code & CODE_MASK;
v2 = (code & INV_CODE_MASK) >> 8;
if (((v1 ^ v2) & VALID_CODE_MASK) == VALID_CODE_MASK)
return (0);
else
return (1);
}
static void
aw_ir_intr(void *arg)
{
struct aw_ir_softc *sc;
uint32_t val;
int i, dcnt;
unsigned long ir_code;
int stat;
sc = (struct aw_ir_softc *)arg;
val = READ(sc, AW_IR_RXSTA);
if (bootverbose && __predict_false(aw_cir_debug) != 0)
device_printf(sc->dev, "RX interrupt status: %x\n", val);
WRITE(sc, AW_IR_RXSTA, val | AW_IR_RXSTA_CLEARALL);
if (val & (AW_IR_RXINT_RAI_EN | AW_IR_RXINT_RPEI_EN)) {
if (bootverbose && __predict_false(aw_cir_debug) != 0)
device_printf(sc->dev,
"RX FIFO Data available or Packet end\n");
dcnt = AW_IR_RXSTA_COUNTER(val);
for (i = 0; i < dcnt; i++) {
if (aw_ir_buf_full(sc)) {
if (bootverbose)
device_printf(sc->dev,
"raw buffer full\n");
break;
} else
aw_ir_buf_write(sc, aw_ir_read_data(sc));
}
}
if (val & AW_IR_RXINT_RPEI_EN) {
if (bootverbose && __predict_false(aw_cir_debug) != 0)
device_printf(sc->dev, "RX Packet end\n");
ir_code = aw_ir_decode_packets(sc);
stat = aw_ir_validate_code(ir_code);
if (stat == 0) {
evdev_push_event(sc->sc_evdev,
EV_MSC, MSC_SCAN, ir_code);
evdev_sync(sc->sc_evdev);
}
if (bootverbose && __predict_false(aw_cir_debug) != 0) {
device_printf(sc->dev, "Final IR code: %lx\n",
ir_code);
device_printf(sc->dev, "IR code status: %d\n",
stat);
}
aw_ir_buf_reset(sc);
}
if (val & AW_IR_RXINT_ROI_EN) {
if (bootverbose)
device_printf(sc->dev, "RX FIFO overflow\n");
aw_ir_buf_reset(sc);
}
}
static int
aw_ir_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
return (ENXIO);
device_set_desc(dev, "Allwinner CIR controller");
return (BUS_PROBE_DEFAULT);
}
static int
aw_ir_attach(device_t dev)
{
struct aw_ir_softc *sc;
hwreset_t rst_apb;
clk_t clk_ir, clk_gate;
int err;
uint32_t val = 0;
clk_ir = clk_gate = NULL;
rst_apb = NULL;
sc = device_get_softc(dev);
sc->dev = dev;
if (bus_alloc_resources(dev, aw_ir_spec, sc->res) != 0) {
device_printf(dev, "could not allocate memory resource\n");
return (ENXIO);
}
switch (ofw_bus_search_compatible(dev, compat_data)->ocd_data) {
case A10_IR:
sc->fifo_size = 16;
break;
case A13_IR:
case A31_IR:
sc->fifo_size = 64;
break;
}
if (hwreset_get_by_ofw_idx(dev, 0, 0, &rst_apb) == 0) {
err = hwreset_deassert(rst_apb);
if (err != 0) {
device_printf(dev, "cannot de-assert reset\n");
goto error;
}
}
aw_ir_buf_reset(sc);
err = clk_get_by_ofw_name(dev, 0, "apb", &clk_gate);
if (err != 0) {
device_printf(dev, "Cannot get gate clock\n");
goto error;
}
err = clk_get_by_ofw_name(dev, 0, "ir", &clk_ir);
if (err != 0) {
device_printf(dev, "Cannot get IR clock\n");
goto error;
}
err = clk_set_freq(clk_ir, AW_IR_BASE_CLK, 0);
if (err != 0) {
device_printf(dev, "cannot set IR clock rate\n");
goto error;
}
err = clk_enable(clk_gate);
if (err != 0) {
device_printf(dev, "Cannot enable clk gate\n");
goto error;
}
err = clk_enable(clk_ir);
if (err != 0) {
device_printf(dev, "Cannot enable IR clock\n");
goto error;
}
if (bus_setup_intr(dev, sc->res[1],
INTR_TYPE_MISC | INTR_MPSAFE, NULL, aw_ir_intr, sc,
&sc->intrhand)) {
bus_release_resources(dev, aw_ir_spec, sc->res);
device_printf(dev, "cannot setup interrupt handler\n");
err = ENXIO;
goto error;
}
WRITE(sc, AW_IR_CTL, AW_IR_CTL_MD);
val = AW_IR_SAMPLE_128;
val |= (AW_IR_RXFILT_VAL | AW_IR_RXIDLE_VAL);
val |= (AW_IR_ACTIVE_T | AW_IR_ACTIVE_T_C);
WRITE(sc, AW_IR_CIR, val);
WRITE(sc, AW_IR_RXCTL, AW_IR_RXCTL_RPPI);
WRITE(sc, AW_IR_RXSTA, AW_IR_RXSTA_CLEARALL);
WRITE(sc, AW_IR_RXINT, AW_IR_RXINT_ROI_EN | AW_IR_RXINT_RPEI_EN |
AW_IR_RXINT_RAI_EN | AW_IR_RXINT_RAL((sc->fifo_size >> 1) - 1));
val = READ(sc, AW_IR_CTL);
WRITE(sc, AW_IR_CTL, val | AW_IR_CTL_GEN | AW_IR_CTL_RXEN);
sc->sc_evdev = evdev_alloc();
evdev_set_name(sc->sc_evdev, device_get_desc(sc->dev));
evdev_set_phys(sc->sc_evdev, device_get_nameunit(sc->dev));
evdev_set_id(sc->sc_evdev, BUS_HOST, 0, 0, 0);
evdev_support_event(sc->sc_evdev, EV_SYN);
evdev_support_event(sc->sc_evdev, EV_MSC);
evdev_support_msc(sc->sc_evdev, MSC_SCAN);
err = evdev_register(sc->sc_evdev);
if (err) {
device_printf(dev,
"failed to register evdev: error=%d\n", err);
goto error;
}
return (0);
error:
if (clk_gate != NULL)
clk_release(clk_gate);
if (clk_ir != NULL)
clk_release(clk_ir);
if (rst_apb != NULL)
hwreset_release(rst_apb);
evdev_free(sc->sc_evdev);
sc->sc_evdev = NULL;
bus_release_resources(dev, aw_ir_spec, sc->res);
return (ENXIO);
}
static device_method_t aw_ir_methods[] = {
DEVMETHOD(device_probe, aw_ir_probe),
DEVMETHOD(device_attach, aw_ir_attach),
DEVMETHOD_END
};
static driver_t aw_ir_driver = {
"aw_ir",
aw_ir_methods,
sizeof(struct aw_ir_softc),
};
DRIVER_MODULE(aw_ir, simplebus, aw_ir_driver, 0, 0);
MODULE_DEPEND(aw_ir, evdev, 1, 1, 1);