tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Partial driver for HP DriveGuard



Hi.

I noticed the other day that one laptop of mine contains an accelerometer
designed to protect the hard drive. So I quickly hacked a driver for it. The
functionality is similar to that of aps(4), but the chip is fully
documented.

This is incomplete as I was unable to get the interrupt mode to work (the
test machine has no SMBus controller and I couldn't figure out how the chip
is wired). As a result the driver now just reports values to the envsys(4)
framework, and everything is passed through ACPICA, which is obviously
undesirable.

So if someone has too much free time and a HP laptop, feel free to continue
from where I left. After all, 3D accelerometers are cool -- I hacked also a
version that attached to wsmouse(9) and thus provided sort of a bigger "Wii"
:-).
  
Cheers,

Jukka.

- - -

/*      $NetBSD: acel_acpi.c,v 0.1 2009/02/25 15:21:18 $        */

/*-
 * Copyright (c) 2009 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jukka Ruohonen.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * An ACPI driver for Hewlett-Packard 3D DriveGuard accelerometer.
 *
 * The supported chipset is LIS3LV02DL from STMicroelectronics:
 *
 *    http://www.st.com/stonline/products/literature/anp/12441.pdf
 *
 *    (Obtained on Sat Apr 25 00:32:04 EEST 2009.)
 *
 * The chip is a three axes digital output linear accelerometer
 * that is controllable through I2C / SPI serial interface. This
 * implementation however supports only indirect connection through
 * ACPI. Other chips from the same family, such as LIS3LV02DQ, may
 * also work with the driver, provided that there is a suitable DSDT.
 *
 * The chip can generate wake-up, direction detection and free-fall
 * interrupts. The latter could be used to evoke emergency action.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acel_acpi.c,v 0.1 2009/02/25 15:21:18 $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/sysctl.h>

#include <dev/acpi/acpivar.h>
#include <dev/acpi/acel_acpireg.h>

#include <dev/sysmon/sysmonvar.h>

enum acel_sensors {
        ACEL_SENSOR_X = 0,
        ACEL_SENSOR_Y,
        ACEL_SENSOR_Z,
        ACEL_SENSOR_NUM
};

struct acel_reg {
        uint8_t               whoami;
        uint8_t               ctrl[3];
        envsys_data_t         xyz[ACEL_SENSOR_NUM];
        int                   xyz_invert[ACEL_SENSOR_NUM];
};

struct acel_softc {
        device_t              sc_dev;
        struct acpi_devnode  *sc_node;
        struct sysmon_envsys *sc_sme;
        struct sysctllog     *sc_log;
        struct acel_reg       sc_reg;
        uint32_t              sc_irq;
        uint8_t               sc_state;
};

static int         acel_match(device_t, cfdata_t, void *);
static void        acel_attach(device_t, device_t, void *);

#ifdef ACEL_IRQ
static ACPI_STATUS acel_get_irq(ACPI_RESOURCE *, void *);
#endif

static ACPI_STATUS acel_reg_init(ACPI_HANDLE, struct acel_reg *);
static ACPI_STATUS acel_reg_info(ACPI_HANDLE, struct acel_reg *);
static ACPI_STATUS acel_reg_xyz(ACPI_HANDLE, const int, int16_t *);
static ACPI_STATUS acel_reg_read(ACPI_HANDLE, ACPI_INTEGER, uint8_t *);
static ACPI_STATUS acel_reg_write(ACPI_HANDLE, ACPI_INTEGER, uint8_t);
static bool        acel_set_state(struct acel_softc *, uint8_t);
static void        acel_sensor_init(device_t);
static void        acel_sensor_refresh(struct sysmon_envsys *,envsys_data_t *);
static void        acel_sysctl_init(device_t);
static int         acel_sysctl_state(SYSCTLFN_PROTO);
static int         acel_sysctl_invert_xyz(SYSCTLFN_PROTO);
static bool        acel_suspend(device_t PMF_FN_PROTO);

const char * const acel_acpi_ids[] = {
        "HPQ0004",
        NULL
};

CFATTACH_DECL_NEW(acel, sizeof(struct acel_softc),
    acel_match, acel_attach, NULL, NULL);

static int
acel_match(device_t parent, cfdata_t match, void *aux)
{
        struct acpi_attach_args *aa = aux;

        if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
                return 0;

        return acpi_match_hid(aa->aa_node->ad_devinfo, acel_acpi_ids);
}

static void
acel_attach(device_t parent, device_t self, void *aux)
{
        struct acel_softc *sc = device_private(self);
        struct acpi_attach_args *aa = aux;
        ACPI_STATUS rv;

        sc->sc_irq = 0;
        sc->sc_state = 0;

        sc->sc_dev = self;
        sc->sc_node = aa->aa_node;

#ifdef ACEL_IRQ
        rv = AcpiWalkResources(sc->sc_node->ad_handle, "_CRS",
            acel_get_irq, &sc->sc_irq);

        if (ACPI_FAILURE(rv) || sc->sc_irq == 0) {
                aprint_error_dev(self, "failed to parse _CRS: %s\n",
                    AcpiFormatException(rv));
                return;
        }
#endif
        rv = acel_reg_init(sc->sc_node->ad_handle, &sc->sc_reg);

        if (ACPI_FAILURE(rv)) {
                aprint_error_dev(self, "failed to initialize device: %s\n",
                    AcpiFormatException(rv));
                return;
        }

        aprint_naive(": Accelerometer\n");
        aprint_normal(": HP 3D DriveGuard (rev. 0x%X)\n", sc->sc_reg.whoami);

        if (sc->sc_reg.whoami != LIS3LV02DL_ID)
                aprint_error_dev(self, "warning: unsupported chip\n");

        acel_sysctl_init(self);

        if (pmf_device_register(sc->sc_dev, acel_suspend, NULL) != true)
                aprint_error_dev(self, "failed to register power handler\n");
}

#ifdef ACEL_IRQ
/*
 * XXX: The _CRS reports only a value for extended
 *      IRQ, and it is unclear how the chip is wired.
 */
static ACPI_STATUS
acel_get_irq(ACPI_RESOURCE *res, void *context)
{
        uint32_t *irq;

        if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_IRQ)
                return AE_OK;

        if (res->Data.ExtendedIrq.InterruptCount != 1)
                return AE_BAD_VALUE;

        if (res->Data.ExtendedIrq.ProducerConsumer != ACPI_CONSUMER)
                return AE_BAD_DATA;

        irq = (uint32_t *)context;
        *irq = res->Data.ExtendedIrq.Interrupts[0];

        return AE_OK;
}
#endif

static ACPI_STATUS
acel_reg_init(ACPI_HANDLE hdl, struct acel_reg *reg)
{
        ACPI_STATUS rv;
        uint8_t val;

        rv = AcpiEvaluateObject(hdl, "_INI", NULL, NULL);

        if (ACPI_FAILURE(rv))
                return rv;
        /*
         * Since the "_INI" is practically a
         * black box, it is better to verify
         * the control registers manually.
         */
        if (acel_reg_info(hdl, reg) != AE_OK)
                return AE_ERROR;

        val = reg->ctrl[0];

        if ((reg->ctrl[0] & CTRL1_Xen) == 0)
                val |= CTRL1_Xen;

        if ((reg->ctrl[0] & CTRL1_Yen) == 0)
                val |= CTRL1_Yen;

        if ((reg->ctrl[0] & CTRL1_Zen) == 0)
                val |= CTRL1_Zen;
        /*
         * We will power off the device by default.
         */
        if ((reg->ctrl[0] & (CTRL1_PD0 | CTRL1_PD1)) != 0) {
                val &= ~CTRL1_PD0;
                val &= ~CTRL1_PD1;
        }

        if (val != reg->ctrl[0]) {
                rv = acel_reg_write(hdl, CTRL_REG1, val);

                if (ACPI_FAILURE(rv))
                        return rv;
        }

        val = reg->ctrl[1];

        if ((reg->ctrl[1] & CTRL2_BDU) == 0)
                val |= CTRL2_BDU;

        if ((reg->ctrl[1] & CTRL2_BLE) != 0)
                val &= ~CTRL2_BLE;

        if ((reg->ctrl[1] & CTRL2_DAS) != 0)
                val &= ~CTRL2_DAS;
        /*
         * Given that we use sysmon_envsys(9),
         * there is no need for the data-ready
         * pin. Besides, polling is likely a
         * good idea since there seems to be
         * some constant motion in the sensors.
         */
        if ((reg->ctrl[1] & CTRL2_DRDY) != 0)
                val &= ~CTRL2_DRDY;
        /*
         * XXX: Set this for the interrupt mode.
         */
        if ((reg->ctrl[1] & CTRL2_IEN) != 0)
                val &= ~CTRL2_IEN;

        if (val != reg->ctrl[1]) {
                rv = acel_reg_write(hdl, CTRL_REG2, val);

                if (ACPI_FAILURE(rv))
                        return rv;
        }
        /*
         * Clear possible interrupt setups from
         * the direction-detection register and
         * from the free-fall-wake-up register.
         */
        (void)acel_reg_write(hdl, DD_CFG, 0x00);
        (void)acel_reg_write(hdl, FF_WU_CFG, 0x00);

        (void)acel_reg_info(hdl, reg);

        return AE_OK;
}

static ACPI_STATUS
acel_reg_info(ACPI_HANDLE hdl, struct acel_reg *reg)
{
        int i;

        if (acel_reg_read(hdl, WHO_AM_I, &reg->whoami) != AE_OK)
                return AE_ERROR;

        for (i = 0; i < ACEL_SENSOR_NUM; ++i) {

                if (acel_reg_read(hdl, CTRL_REG1 + i, &reg->ctrl[i]) != AE_OK)
                        return AE_ERROR;
        }

        return AE_OK;
}

static ACPI_STATUS
acel_reg_xyz(ACPI_HANDLE hdl, const int xyz, int16_t *out)
{
        ACPI_INTEGER reg[2];
        ACPI_STATUS rv[2];
        uint8_t hi, lo;

        switch (xyz) {

        case ACEL_SENSOR_X:
                reg[0] = OUTX_L;
                reg[1] = OUTX_H;
                break;

        case ACEL_SENSOR_Y:
                reg[0] = OUTY_L;
                reg[1] = OUTY_H;
                break;

        case ACEL_SENSOR_Z:
                reg[0] = OUTZ_L;
                reg[1] = OUTZ_H;
                break;

        default:
                return AE_BAD_PARAMETER;
        }

        rv[0] = acel_reg_read(hdl, reg[0], &lo);
        rv[1] = acel_reg_read(hdl, reg[1], &hi);

        if (ACPI_FAILURE(rv[0]) || ACPI_FAILURE(rv[1]))
                return AE_ERROR;
        /*
         * These registers are read in "12 bit right
         * justified mode", meaning that the four
         * most significant bits are replaced with
         * the value of bit 12. Note the signed type.
         */
        hi = (hi & 0x10) ? hi | 0xE0 : hi & ~0xE0;

        *out = (hi << 8) | lo;

        return AE_OK;
}

static ACPI_STATUS
acel_reg_read(ACPI_HANDLE hdl, ACPI_INTEGER reg, uint8_t *out)
{
        ACPI_OBJECT_LIST arg;
        ACPI_OBJECT obj, val;
        ACPI_BUFFER buf;
        ACPI_STATUS rv;

        obj.Type = ACPI_TYPE_INTEGER;
        obj.Integer.Value = reg;

        buf.Pointer = &val;
        buf.Length = sizeof(val);

        arg.Count = 1;
        arg.Pointer = &obj;

        rv = AcpiEvaluateObjectTyped(hdl, "ALRD", &arg, &buf,
            ACPI_TYPE_INTEGER);

        if (val.Integer.Value > UINT8_MAX)
                return AE_LIMIT;

        if (ACPI_SUCCESS(rv))
                *out = val.Integer.Value;

        return rv;
}

static ACPI_STATUS
acel_reg_write(ACPI_HANDLE hdl, ACPI_INTEGER reg, uint8_t val)
{
        ACPI_OBJECT_LIST arg;
        ACPI_OBJECT obj[2];

        obj[0].Type = obj[1].Type = ACPI_TYPE_INTEGER;

        obj[0].Integer.Value = reg;
        obj[1].Integer.Value = val;

        arg.Count = 2;
        arg.Pointer = obj;

        return AcpiEvaluateObject(hdl, "ALWR", &arg, NULL);
}

static bool
acel_set_state(struct acel_softc *sc, uint8_t state)
{
        ACPI_OBJECT_LIST arg;
        ACPI_OBJECT obj;
        int8_t val;

        if (sc->sc_state == state)
                return true;

        val = sc->sc_reg.ctrl[0];
        val = (state != 0) ? val | CTRL1_PD0 : val & ~CTRL1_PD0;

        if (acel_reg_write(sc->sc_node->ad_handle, CTRL_REG1, val) != AE_OK)
                return false;

        sc->sc_reg.ctrl[0] = val;

        if (state != 0)
                acel_sensor_init(sc->sc_dev);
        else {
                KASSERT(sc->sc_sme != NULL);
                (void)sysmon_envsys_unregister(sc->sc_sme);
                sc->sc_sme = NULL;
        }

        obj.Type = ACPI_TYPE_INTEGER;
        obj.Integer.Value = state;

        arg.Count = 1;
        arg.Pointer = &obj;

        (void)AcpiEvaluateObject(sc->sc_node->ad_handle, "ALED", &arg, NULL);

        return true;
}

static void
acel_sensor_init(device_t self)
{
        struct acel_softc *sc = device_private(self);
        const char zyx[ACEL_SENSOR_NUM] = { 'x', 'y', 'z' };
        int i, rv;

        sc->sc_sme = sysmon_envsys_create();

        for (i = 0; i < ACEL_SENSOR_NUM; ++i) {

                sc->sc_reg.xyz[i].units = ENVSYS_INTEGER;
                sc->sc_reg.xyz[i].state = ENVSYS_SINVALID;

                (void)snprintf(sc->sc_reg.xyz[i].desc,
                    ENVSYS_DESCLEN, "%c-axis", zyx[i]);

                rv = sysmon_envsys_sensor_attach(sc->sc_sme,
                    &sc->sc_reg.xyz[i]);

                if (rv != 0) {
                        aprint_error_dev(self, "failed to attach '%s' to "
                            "sysmon (error %d)\n", sc->sc_reg.xyz[i].desc, rv);
                        sysmon_envsys_destroy(sc->sc_sme);
                        return;
                }
        }

        sc->sc_sme->sme_cookie = sc;
        sc->sc_sme->sme_name = device_xname(self);
        sc->sc_sme->sme_refresh = acel_sensor_refresh;

        rv = sysmon_envsys_register(sc->sc_sme);

        if (rv != 0) {
                aprint_error_dev(self, "failed to register with sysmon\n");
                sysmon_envsys_destroy(sc->sc_sme);
        }
}
/*
 * While there would be plenty more information to gather from
 * the chip, each poll cycle will already now make six calls to the
 * abyss of ACPI(CA). This is hopelessly slow way to read registers.
 */
static void
acel_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
        struct acel_softc *sc = sme->sme_cookie;
        ACPI_STATUS rv;
        int16_t i, val;

        KASSERT(sc->sc_state != 0);

        for (i = val = 0; i < ACEL_SENSOR_NUM; ++i) {

                rv = acel_reg_xyz(sc->sc_node->ad_handle, i, &val);

                if (ACPI_SUCCESS(rv)) {

                        if (sc->sc_reg.xyz_invert[i] != 0)
                                val = -val;

                        sc->sc_reg.xyz[i].value_cur = val;
                        sc->sc_reg.xyz[i].state = ENVSYS_SVALID;
                        continue;
                }

                if (sc->sc_reg.xyz[i].state != ENVSYS_SINVALID) {
                        aprint_error_dev(sc->sc_dev,
                            "failed to read state of '%s': %s\n",
                            sc->sc_reg.xyz[i].desc, AcpiFormatException(rv));
                }

                sc->sc_reg.xyz[i].state = ENVSYS_SINVALID;
        }
}

static void
acel_sysctl_init(device_t self)
{
        struct acel_softc *sc = device_private(self);
        const struct sysctlnode *node;
        int root, rv;

        sc->sc_log = NULL;

        rv = sysctl_createv(NULL, 0, NULL, NULL,
            CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
            NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL);

        if (rv != 0)
                goto fail;

        rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
            0, CTLTYPE_NODE, device_xname(self),
            SYSCTL_DESCR("HP 3D DriveGuard controls"),
            NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);

        if (rv != 0)
                goto fail;

        root = node->sysctl_num;

        rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
            CTLFLAG_READWRITE, CTLTYPE_INT, "enable",
            SYSCTL_DESCR("Enables or disables the device"),
            acel_sysctl_state, 0, sc, 0,
            CTL_HW, root, CTL_CREATE, CTL_EOL);

        if (rv != 0)
                goto fail;

        rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
            CTLFLAG_READWRITE, CTLTYPE_INT, "invert_x",
            SYSCTL_DESCR("Inverts X-axis"),
            acel_sysctl_invert_xyz, 0, &sc->sc_reg.xyz_invert[0], 0,
            CTL_HW, root, CTL_CREATE, CTL_EOL);

        if (rv != 0)
                goto fail;

        rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
            CTLFLAG_READWRITE, CTLTYPE_INT, "invert_y",
            SYSCTL_DESCR("Inverts Y-axis"),
            acel_sysctl_invert_xyz, 0, &sc->sc_reg.xyz_invert[1], 0,
            CTL_HW, root, CTL_CREATE, CTL_EOL);

        if (rv != 0)
                goto fail;

        rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
            CTLFLAG_READWRITE, CTLTYPE_INT, "invert_z",
            SYSCTL_DESCR("Inverts Z-axis"),
            acel_sysctl_invert_xyz, 0, &sc->sc_reg.xyz_invert[2], 0,
            CTL_HW, root, CTL_CREATE, CTL_EOL);

        if (rv != 0)
                goto fail;

        return;

fail:
        if (sc->sc_log != NULL)
                sysctl_teardown(&sc->sc_log);

        aprint_error_dev(sc->sc_dev,
            "failed to initialize sysctl (error %d)\n", rv);
}

static int
acel_sysctl_state(SYSCTLFN_ARGS)
{
        struct sysctlnode node;
        struct acel_softc *sc;
        int error, t;

        sc = (struct acel_softc *)rnode->sysctl_data;
        t = sc->sc_state;

        node = *rnode;
        node.sysctl_data = &t;

        error = sysctl_lookup(SYSCTLFN_CALL(&node));

        if (error || newp == NULL)
                return error;

        if (t != 0 && t != 1)
                return EINVAL;

        if (acel_set_state(sc, t) != true) {
                aprint_error_dev(sc->sc_dev, "failed to %s device\n",
                    (t != 0) ? "enable" : "disable");
                return ENXIO;
        }

        sc->sc_state = t;

        return 0;
}

static int
acel_sysctl_invert_xyz(SYSCTLFN_ARGS)
{
        struct sysctlnode node;
        int error, t;

        node = *rnode;
        t = *(int *)rnode->sysctl_data;
        node.sysctl_data = &t;

        error = sysctl_lookup(SYSCTLFN_CALL(&node));

        if (error || newp == NULL)
                return error;

        if (t != 0 && t != 1)
                return EINVAL;

        *(int *)rnode->sysctl_data = t;

        return 0;
}

static bool
acel_suspend(device_t dv PMF_FN_ARGS)
{
        struct acel_softc *sc = device_private(dv);

        return acel_set_state(sc, 0);
}


---


/*      $NetBSD: acel_acpireg.h,v 0.1 2009/02/25 15:21:18 $     */

/*-
 * Copyright (c) 2009 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jukka Ruohonen.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef ACEL_ACPIREG_H
#define ACEL_ACPIREG_H

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acel_acpireg.h,v 0.1 2009/02/25 15:21:18 $");
/*
 * Partial datasheet for LIS3LV02D.
 */
#define LIS3LV02DL_ID     0x3A

enum lis3_reg {
        WHO_AM_I        = 0x0F, /* r  */
        OFFSET_X        = 0x16, /* rw */
        OFFSET_Y        = 0x17, /* rw */
        OFFSET_Z        = 0x18, /* rw */
        GAIN_X          = 0x19, /* rw */
        GAIN_Y          = 0x1A, /* rw */
        GAIN_Z          = 0x1B, /* rw */
        CTRL_REG1       = 0x20, /* rw */
        CTRL_REG2       = 0x21, /* rw */
        CTRL_REG3       = 0x22, /* rw */
        HP_FILTER_RESET = 0x23, /* r  */
        STATUS_REG      = 0x27, /* rw */
        OUTX_L          = 0x28, /* r  */
        OUTX_H          = 0x29, /* r  */
        OUTY_L          = 0x2A, /* r  */
        OUTY_H          = 0x2B, /* r  */
        OUTZ_L          = 0x2C, /* r  */
        OUTZ_H          = 0x2D, /* r  */
        FF_WU_CFG       = 0x30, /* r  */
        FF_WU_SRC       = 0x31, /* rw */
        FF_WU_ACK       = 0x32, /* r  */
        FF_WU_THS_L     = 0x34, /* rw */
        FF_WU_THS_H     = 0x35, /* rw */
        FF_WU_DURATION  = 0x36, /* rw */
        DD_CFG          = 0x38, /* rw */
        DD_SRC          = 0x39, /* rw */
        DD_ACK          = 0x3A, /* r  */
        DD_THSI_L       = 0x3C, /* rw */
        DD_THSI_H       = 0x3D, /* rw */
        DD_THSE_L       = 0x3E, /* rw */
        DD_THSE_H       = 0x3F  /* rw */
};

enum lis_ctrl1 {
        CTRL1_Xen  = (1 << 0),  /* X-axis enable */
        CTRL1_Yen  = (1 << 1),  /* Y-axis enable */
        CTRL1_Zen  = (1 << 2),  /* Z-axis enable */
        CTRL1_ST   = (1 << 3),  /* Self test enable */
        CTRL1_DF0  = (1 << 4),  /* Decimation factor control */
        CTRL1_DF1  = (1 << 5),  /* Decimation factor control */
        CTRL1_PD0  = (1 << 6),  /* Power down control */
        CTRL1_PD1  = (1 << 7)   /* Power down control */
};

enum lis3_ctrl2 {
        CTRL2_DAS  = (1 << 0),  /* Data alignment selection */
        CTRL2_SIM  = (1 << 1),  /* SPI serial interface mode */
        CTRL2_DRDY = (1 << 2),  /* Enable data-ready generation */
        CTRL2_IEN  = (1 << 3),  /* Enable interrupt mode */
        CTRL2_BOOT = (1 << 4),  /* Reboot memory contents */
        CTRL2_BLE  = (1 << 5),  /* Endian mode */
        CTRL2_BDU  = (1 << 6),  /* Block data update */
        CTRL2_FS   = (1 << 7)   /* Full scale selection */
};

enum lis_ctrl3 {
        CTRL3_CFS0 = (1 << 0),  /* High-pass filter cut-off frequency */
        CTRL3_CFS1 = (1 << 1),  /* High-pass filter cut-off frequency */
        CTRL3_FDS  = (1 << 4),  /* Filtered data selection */
        CTRL3_HPFF = (1 << 5),  /* High pass filter for free-fall */
        CTRL3_HPDD = (1 << 6),  /* High pass filter for DD */
        CTRL3_ECK  = (1 << 7)   /* External clock */
};

#endif  /* def ACEL_ACPIREG_H */
/*      $NetBSD: acel_acpi.c,v 0.1 2009/02/25 15:21:18 $        */

/*-
 * Copyright (c) 2009 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jukka Ruohonen.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * An ACPI driver for Hewlett-Packard 3D DriveGuard accelerometer.
 *
 * The supported chipset is LIS3LV02DL from STMicroelectronics:
 *
 *    http://www.st.com/stonline/products/literature/anp/12441.pdf
 *
 *    (Obtained on Sat Apr 25 00:32:04 EEST 2009.)
 *
 * The chip is a three axes digital output linear accelerometer
 * that is controllable through I2C / SPI serial interface. This
 * implementation however supports only indirect connection through
 * ACPI. Other chips from the same family, such as LIS3LV02DQ, may
 * also work with the driver, provided that there is a suitable DSDT.
 *
 * The chip can generate wake-up, direction detection and free-fall
 * interrupts. The latter could be used to evoke emergency action.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acel_acpi.c,v 0.1 2009/02/25 15:21:18 $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/sysctl.h>

#include <dev/acpi/acpivar.h>
#include <dev/acpi/acel_acpireg.h>

#include <dev/sysmon/sysmonvar.h>

enum acel_sensors {
        ACEL_SENSOR_X = 0,
        ACEL_SENSOR_Y,
        ACEL_SENSOR_Z,
        ACEL_SENSOR_NUM
};

struct acel_reg {
        uint8_t               whoami;
        uint8_t               ctrl[3];
        envsys_data_t         xyz[ACEL_SENSOR_NUM];
        int                   xyz_invert[ACEL_SENSOR_NUM];
};

struct acel_softc {
        device_t              sc_dev;
        struct acpi_devnode  *sc_node;
        struct sysmon_envsys *sc_sme;
        struct sysctllog     *sc_log;
        struct acel_reg       sc_reg;
        uint32_t              sc_irq;
        uint8_t               sc_state;
};

static int         acel_match(device_t, cfdata_t, void *);
static void        acel_attach(device_t, device_t, void *);

#ifdef ACEL_IRQ
static ACPI_STATUS acel_get_irq(ACPI_RESOURCE *, void *);
#endif

static ACPI_STATUS acel_reg_init(ACPI_HANDLE, struct acel_reg *);
static ACPI_STATUS acel_reg_info(ACPI_HANDLE, struct acel_reg *);
static ACPI_STATUS acel_reg_xyz(ACPI_HANDLE, const int, int16_t *);
static ACPI_STATUS acel_reg_read(ACPI_HANDLE, ACPI_INTEGER, uint8_t *);
static ACPI_STATUS acel_reg_write(ACPI_HANDLE, ACPI_INTEGER, uint8_t);
static bool        acel_set_state(struct acel_softc *, uint8_t);
static void        acel_sensor_init(device_t);
static void        acel_sensor_refresh(struct sysmon_envsys *,envsys_data_t *);
static void        acel_sysctl_init(device_t);
static int         acel_sysctl_state(SYSCTLFN_PROTO);
static int         acel_sysctl_invert_xyz(SYSCTLFN_PROTO);
static bool        acel_suspend(device_t PMF_FN_PROTO);

const char * const acel_acpi_ids[] = {
        "HPQ0004",
        NULL
};

CFATTACH_DECL_NEW(acel, sizeof(struct acel_softc),
    acel_match, acel_attach, NULL, NULL);

static int
acel_match(device_t parent, cfdata_t match, void *aux)
{
        struct acpi_attach_args *aa = aux;

        if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
                return 0;

        return acpi_match_hid(aa->aa_node->ad_devinfo, acel_acpi_ids);
}

static void
acel_attach(device_t parent, device_t self, void *aux)
{
        struct acel_softc *sc = device_private(self);
        struct acpi_attach_args *aa = aux;
        ACPI_STATUS rv;

        sc->sc_irq = 0;
        sc->sc_state = 0;

        sc->sc_dev = self;
        sc->sc_node = aa->aa_node;

#ifdef ACEL_IRQ
        rv = AcpiWalkResources(sc->sc_node->ad_handle, "_CRS",
            acel_get_irq, &sc->sc_irq);

        if (ACPI_FAILURE(rv) || sc->sc_irq == 0) {
                aprint_error_dev(self, "failed to parse _CRS: %s\n",
                    AcpiFormatException(rv));
                return;
        }
#endif
        rv = acel_reg_init(sc->sc_node->ad_handle, &sc->sc_reg);

        if (ACPI_FAILURE(rv)) {
                aprint_error_dev(self, "failed to initialize device: %s\n",
                    AcpiFormatException(rv));
                return;
        }

        aprint_naive(": Accelerometer\n");
        aprint_normal(": HP 3D DriveGuard (rev. 0x%X)\n", sc->sc_reg.whoami);

        if (sc->sc_reg.whoami != LIS3LV02DL_ID)
                aprint_error_dev(self, "warning: unsupported chip\n");

        acel_sysctl_init(self);

        if (pmf_device_register(sc->sc_dev, acel_suspend, NULL) != true)
                aprint_error_dev(self, "failed to register power handler\n");
}

#ifdef ACEL_IRQ
/*
 * XXX: The _CRS reports only a value for extended
 *      IRQ, and it is unclear how the chip is wired.
 */
static ACPI_STATUS
acel_get_irq(ACPI_RESOURCE *res, void *context)
{
        uint32_t *irq;

        if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_IRQ)
                return AE_OK;

        if (res->Data.ExtendedIrq.InterruptCount != 1)
                return AE_BAD_VALUE;

        if (res->Data.ExtendedIrq.ProducerConsumer != ACPI_CONSUMER)
                return AE_BAD_DATA;

        irq = (uint32_t *)context;
        *irq = res->Data.ExtendedIrq.Interrupts[0];

        return AE_OK;
}
#endif

static ACPI_STATUS
acel_reg_init(ACPI_HANDLE hdl, struct acel_reg *reg)
{
        ACPI_STATUS rv;
        uint8_t val;

        rv = AcpiEvaluateObject(hdl, "_INI", NULL, NULL);

        if (ACPI_FAILURE(rv))
                return rv;
        /*
         * Since the "_INI" is practically a
         * black box, it is better to verify
         * the control registers manually.
         */
        if (acel_reg_info(hdl, reg) != AE_OK)
                return AE_ERROR;

        val = reg->ctrl[0];

        if ((reg->ctrl[0] & CTRL1_Xen) == 0)
                val |= CTRL1_Xen;

        if ((reg->ctrl[0] & CTRL1_Yen) == 0)
                val |= CTRL1_Yen;

        if ((reg->ctrl[0] & CTRL1_Zen) == 0)
                val |= CTRL1_Zen;
        /*
         * We will power off the device by default.
         */
        if ((reg->ctrl[0] & (CTRL1_PD0 | CTRL1_PD1)) != 0) {
                val &= ~CTRL1_PD0;
                val &= ~CTRL1_PD1;
        }

        if (val != reg->ctrl[0]) {
                rv = acel_reg_write(hdl, CTRL_REG1, val);

                if (ACPI_FAILURE(rv))
                        return rv;
        }

        val = reg->ctrl[1];

        if ((reg->ctrl[1] & CTRL2_BDU) == 0)
                val |= CTRL2_BDU;

        if ((reg->ctrl[1] & CTRL2_BLE) != 0)
                val &= ~CTRL2_BLE;

        if ((reg->ctrl[1] & CTRL2_DAS) != 0)
                val &= ~CTRL2_DAS;
        /*
         * Given that we use sysmon_envsys(9),
         * there is no need for the data-ready
         * pin. Besides, polling is likely a
         * good idea since there seems to be
         * some constant motion in the sensors.
         */
        if ((reg->ctrl[1] & CTRL2_DRDY) != 0)
                val &= ~CTRL2_DRDY;
        /*
         * XXX: Set this for the interrupt mode.
         */
        if ((reg->ctrl[1] & CTRL2_IEN) != 0)
                val &= ~CTRL2_IEN;

        if (val != reg->ctrl[1]) {
                rv = acel_reg_write(hdl, CTRL_REG2, val);

                if (ACPI_FAILURE(rv))
                        return rv;
        }
        /*
         * Clear possible interrupt setups from
         * the direction-detection register and
         * from the free-fall-wake-up register.
         */
        (void)acel_reg_write(hdl, DD_CFG, 0x00);
        (void)acel_reg_write(hdl, FF_WU_CFG, 0x00);

        (void)acel_reg_info(hdl, reg);

        return AE_OK;
}

static ACPI_STATUS
acel_reg_info(ACPI_HANDLE hdl, struct acel_reg *reg)
{
        int i;

        if (acel_reg_read(hdl, WHO_AM_I, &reg->whoami) != AE_OK)
                return AE_ERROR;

        for (i = 0; i < ACEL_SENSOR_NUM; ++i) {

                if (acel_reg_read(hdl, CTRL_REG1 + i, &reg->ctrl[i]) != AE_OK)
                        return AE_ERROR;
        }

        return AE_OK;
}

static ACPI_STATUS
acel_reg_xyz(ACPI_HANDLE hdl, const int xyz, int16_t *out)
{
        ACPI_INTEGER reg[2];
        ACPI_STATUS rv[2];
        uint8_t hi, lo;

        switch (xyz) {

        case ACEL_SENSOR_X:
                reg[0] = OUTX_L;
                reg[1] = OUTX_H;
                break;

        case ACEL_SENSOR_Y:
                reg[0] = OUTY_L;
                reg[1] = OUTY_H;
                break;

        case ACEL_SENSOR_Z:
                reg[0] = OUTZ_L;
                reg[1] = OUTZ_H;
                break;

        default:
                return AE_BAD_PARAMETER;
        }

        rv[0] = acel_reg_read(hdl, reg[0], &lo);
        rv[1] = acel_reg_read(hdl, reg[1], &hi);

        if (ACPI_FAILURE(rv[0]) || ACPI_FAILURE(rv[1]))
                return AE_ERROR;
        /*
         * These registers are read in "12 bit right
         * justified mode", meaning that the four
         * most significant bits are replaced with
         * the value of bit 12. Note the signed type.
         */
        hi = (hi & 0x10) ? hi | 0xE0 : hi & ~0xE0;

        *out = (hi << 8) | lo;

        return AE_OK;
}

static ACPI_STATUS
acel_reg_read(ACPI_HANDLE hdl, ACPI_INTEGER reg, uint8_t *out)
{
        ACPI_OBJECT_LIST arg;
        ACPI_OBJECT obj, val;
        ACPI_BUFFER buf;
        ACPI_STATUS rv;

        obj.Type = ACPI_TYPE_INTEGER;
        obj.Integer.Value = reg;

        buf.Pointer = &val;
        buf.Length = sizeof(val);

        arg.Count = 1;
        arg.Pointer = &obj;

        rv = AcpiEvaluateObjectTyped(hdl, "ALRD", &arg, &buf,
            ACPI_TYPE_INTEGER);

        if (val.Integer.Value > UINT8_MAX)
                return AE_LIMIT;

        if (ACPI_SUCCESS(rv))
                *out = val.Integer.Value;

        return rv;
}

static ACPI_STATUS
acel_reg_write(ACPI_HANDLE hdl, ACPI_INTEGER reg, uint8_t val)
{
        ACPI_OBJECT_LIST arg;
        ACPI_OBJECT obj[2];

        obj[0].Type = obj[1].Type = ACPI_TYPE_INTEGER;

        obj[0].Integer.Value = reg;
        obj[1].Integer.Value = val;

        arg.Count = 2;
        arg.Pointer = obj;

        return AcpiEvaluateObject(hdl, "ALWR", &arg, NULL);
}

static bool
acel_set_state(struct acel_softc *sc, uint8_t state)
{
        ACPI_OBJECT_LIST arg;
        ACPI_OBJECT obj;
        int8_t val;

        if (sc->sc_state == state)
                return true;

        val = sc->sc_reg.ctrl[0];
        val = (state != 0) ? val | CTRL1_PD0 : val & ~CTRL1_PD0;

        if (acel_reg_write(sc->sc_node->ad_handle, CTRL_REG1, val) != AE_OK)
                return false;

        sc->sc_reg.ctrl[0] = val;

        if (state != 0)
                acel_sensor_init(sc->sc_dev);
        else {
                KASSERT(sc->sc_sme != NULL);
                (void)sysmon_envsys_unregister(sc->sc_sme);
                sc->sc_sme = NULL;
        }

        obj.Type = ACPI_TYPE_INTEGER;
        obj.Integer.Value = state;

        arg.Count = 1;
        arg.Pointer = &obj;

        (void)AcpiEvaluateObject(sc->sc_node->ad_handle, "ALED", &arg, NULL);

        return true;
}

static void
acel_sensor_init(device_t self)
{
        struct acel_softc *sc = device_private(self);
        const char zyx[ACEL_SENSOR_NUM] = { 'x', 'y', 'z' };
        int i, rv;

        sc->sc_sme = sysmon_envsys_create();

        for (i = 0; i < ACEL_SENSOR_NUM; ++i) {

                sc->sc_reg.xyz[i].units = ENVSYS_INTEGER;
                sc->sc_reg.xyz[i].state = ENVSYS_SINVALID;

                (void)snprintf(sc->sc_reg.xyz[i].desc,
                    ENVSYS_DESCLEN, "%c-axis", zyx[i]);

                rv = sysmon_envsys_sensor_attach(sc->sc_sme,
                    &sc->sc_reg.xyz[i]);

                if (rv != 0) {
                        aprint_error_dev(self, "failed to attach '%s' to "
                            "sysmon (error %d)\n", sc->sc_reg.xyz[i].desc, rv);
                        sysmon_envsys_destroy(sc->sc_sme);
                        return;
                }
        }

        sc->sc_sme->sme_cookie = sc;
        sc->sc_sme->sme_name = device_xname(self);
        sc->sc_sme->sme_refresh = acel_sensor_refresh;

        rv = sysmon_envsys_register(sc->sc_sme);

        if (rv != 0) {
                aprint_error_dev(self, "failed to register with sysmon\n");
                sysmon_envsys_destroy(sc->sc_sme);
        }
}
/*
 * While there would be plenty more information to gather from
 * the chip, each poll cycle will already now make six calls to the
 * abyss of ACPI(CA). This is hopelessly slow way to read registers.
 */
static void
acel_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
        struct acel_softc *sc = sme->sme_cookie;
        ACPI_STATUS rv;
        int16_t i, val;

        KASSERT(sc->sc_state != 0);

        for (i = val = 0; i < ACEL_SENSOR_NUM; ++i) {

                rv = acel_reg_xyz(sc->sc_node->ad_handle, i, &val);

                if (ACPI_SUCCESS(rv)) {

                        if (sc->sc_reg.xyz_invert[i] != 0)
                                val = -val;

                        sc->sc_reg.xyz[i].value_cur = val;
                        sc->sc_reg.xyz[i].state = ENVSYS_SVALID;
                        continue;
                }

                if (sc->sc_reg.xyz[i].state != ENVSYS_SINVALID) {
                        aprint_error_dev(sc->sc_dev,
                            "failed to read state of '%s': %s\n",
                            sc->sc_reg.xyz[i].desc, AcpiFormatException(rv));
                }

                sc->sc_reg.xyz[i].state = ENVSYS_SINVALID;
        }
}

static void
acel_sysctl_init(device_t self)
{
        struct acel_softc *sc = device_private(self);
        const struct sysctlnode *node;
        int root, rv;

        sc->sc_log = NULL;

        rv = sysctl_createv(NULL, 0, NULL, NULL,
            CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
            NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL);

        if (rv != 0)
                goto fail;

        rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
            0, CTLTYPE_NODE, device_xname(self),
            SYSCTL_DESCR("HP 3D DriveGuard controls"),
            NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);

        if (rv != 0)
                goto fail;

        root = node->sysctl_num;

        rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
            CTLFLAG_READWRITE, CTLTYPE_INT, "enable",
            SYSCTL_DESCR("Enables or disables the device"),
            acel_sysctl_state, 0, sc, 0,
            CTL_HW, root, CTL_CREATE, CTL_EOL);

        if (rv != 0)
                goto fail;

        rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
            CTLFLAG_READWRITE, CTLTYPE_INT, "invert_x",
            SYSCTL_DESCR("Inverts X-axis"),
            acel_sysctl_invert_xyz, 0, &sc->sc_reg.xyz_invert[0], 0,
            CTL_HW, root, CTL_CREATE, CTL_EOL);

        if (rv != 0)
                goto fail;

        rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
            CTLFLAG_READWRITE, CTLTYPE_INT, "invert_y",
            SYSCTL_DESCR("Inverts Y-axis"),
            acel_sysctl_invert_xyz, 0, &sc->sc_reg.xyz_invert[1], 0,
            CTL_HW, root, CTL_CREATE, CTL_EOL);

        if (rv != 0)
                goto fail;

        rv = sysctl_createv(&sc->sc_log, 0, NULL, &node,
            CTLFLAG_READWRITE, CTLTYPE_INT, "invert_z",
            SYSCTL_DESCR("Inverts Z-axis"),
            acel_sysctl_invert_xyz, 0, &sc->sc_reg.xyz_invert[2], 0,
            CTL_HW, root, CTL_CREATE, CTL_EOL);

        if (rv != 0)
                goto fail;

        return;

fail:
        if (sc->sc_log != NULL)
                sysctl_teardown(&sc->sc_log);

        aprint_error_dev(sc->sc_dev,
            "failed to initialize sysctl (error %d)\n", rv);
}

static int
acel_sysctl_state(SYSCTLFN_ARGS)
{
        struct sysctlnode node;
        struct acel_softc *sc;
        int error, t;

        sc = (struct acel_softc *)rnode->sysctl_data;
        t = sc->sc_state;

        node = *rnode;
        node.sysctl_data = &t;

        error = sysctl_lookup(SYSCTLFN_CALL(&node));

        if (error || newp == NULL)
                return error;

        if (t != 0 && t != 1)
                return EINVAL;

        if (acel_set_state(sc, t) != true) {
                aprint_error_dev(sc->sc_dev, "failed to %s device\n",
                    (t != 0) ? "enable" : "disable");
                return ENXIO;
        }

        sc->sc_state = t;

        return 0;
}

static int
acel_sysctl_invert_xyz(SYSCTLFN_ARGS)
{
        struct sysctlnode node;
        int error, t;

        node = *rnode;
        t = *(int *)rnode->sysctl_data;
        node.sysctl_data = &t;

        error = sysctl_lookup(SYSCTLFN_CALL(&node));

        if (error || newp == NULL)
                return error;

        if (t != 0 && t != 1)
                return EINVAL;

        *(int *)rnode->sysctl_data = t;

        return 0;
}

static bool
acel_suspend(device_t dv PMF_FN_ARGS)
{
        struct acel_softc *sc = device_private(dv);

        return acel_set_state(sc, 0);
}
/*      $NetBSD: acel_acpireg.h,v 0.1 2009/02/25 15:21:18 $     */

/*-
 * Copyright (c) 2009 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jukka Ruohonen.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef ACEL_ACPIREG_H
#define ACEL_ACPIREG_H

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acel_acpireg.h,v 0.1 2009/02/25 15:21:18 $");
/*
 * Partial datasheet for LIS3LV02D.
 */
#define LIS3LV02DL_ID     0x3A

enum lis3_reg {
        WHO_AM_I        = 0x0F, /* r  */
        OFFSET_X        = 0x16, /* rw */
        OFFSET_Y        = 0x17, /* rw */
        OFFSET_Z        = 0x18, /* rw */
        GAIN_X          = 0x19, /* rw */
        GAIN_Y          = 0x1A, /* rw */
        GAIN_Z          = 0x1B, /* rw */
        CTRL_REG1       = 0x20, /* rw */
        CTRL_REG2       = 0x21, /* rw */
        CTRL_REG3       = 0x22, /* rw */
        HP_FILTER_RESET = 0x23, /* r  */
        STATUS_REG      = 0x27, /* rw */
        OUTX_L          = 0x28, /* r  */
        OUTX_H          = 0x29, /* r  */
        OUTY_L          = 0x2A, /* r  */
        OUTY_H          = 0x2B, /* r  */
        OUTZ_L          = 0x2C, /* r  */
        OUTZ_H          = 0x2D, /* r  */
        FF_WU_CFG       = 0x30, /* r  */
        FF_WU_SRC       = 0x31, /* rw */
        FF_WU_ACK       = 0x32, /* r  */
        FF_WU_THS_L     = 0x34, /* rw */
        FF_WU_THS_H     = 0x35, /* rw */
        FF_WU_DURATION  = 0x36, /* rw */
        DD_CFG          = 0x38, /* rw */
        DD_SRC          = 0x39, /* rw */
        DD_ACK          = 0x3A, /* r  */
        DD_THSI_L       = 0x3C, /* rw */
        DD_THSI_H       = 0x3D, /* rw */
        DD_THSE_L       = 0x3E, /* rw */
        DD_THSE_H       = 0x3F  /* rw */
};

enum lis_ctrl1 {
        CTRL1_Xen  = (1 << 0),  /* X-axis enable */
        CTRL1_Yen  = (1 << 1),  /* Y-axis enable */
        CTRL1_Zen  = (1 << 2),  /* Z-axis enable */
        CTRL1_ST   = (1 << 3),  /* Self test enable */
        CTRL1_DF0  = (1 << 4),  /* Decimation factor control */
        CTRL1_DF1  = (1 << 5),  /* Decimation factor control */
        CTRL1_PD0  = (1 << 6),  /* Power down control */
        CTRL1_PD1  = (1 << 7)   /* Power down control */
};

enum lis3_ctrl2 {
        CTRL2_DAS  = (1 << 0),  /* Data alignment selection */
        CTRL2_SIM  = (1 << 1),  /* SPI serial interface mode */
        CTRL2_DRDY = (1 << 2),  /* Enable data-ready generation */
        CTRL2_IEN  = (1 << 3),  /* Enable interrupt mode */
        CTRL2_BOOT = (1 << 4),  /* Reboot memory contents */
        CTRL2_BLE  = (1 << 5),  /* Endian mode */
        CTRL2_BDU  = (1 << 6),  /* Block data update */
        CTRL2_FS   = (1 << 7)   /* Full scale selection */
};

enum lis_ctrl3 {
        CTRL3_CFS0 = (1 << 0),  /* High-pass filter cut-off frequency */
        CTRL3_CFS1 = (1 << 1),  /* High-pass filter cut-off frequency */
        CTRL3_FDS  = (1 << 4),  /* Filtered data selection */
        CTRL3_HPFF = (1 << 5),  /* High pass filter for free-fall */
        CTRL3_HPDD = (1 << 6),  /* High pass filter for DD */
        CTRL3_ECK  = (1 << 7)   /* External clock */
};

#endif  /* def ACEL_ACPIREG_H */


Home | Main Index | Thread Index | Old Index