#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: hpacel_acpi.c,v 1.6 2021/01/29 15:49:55 thorpej Exp $");
#include <sys/param.h>
#include <sys/module.h>
#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpi_power.h>
#include <dev/sysmon/sysmonvar.h>
#define _COMPONENT ACPI_RESOURCE_COMPONENT
ACPI_MODULE_NAME ("hpacel_acpi")
enum {
HPACEL_SENSOR_X = 0,
HPACEL_SENSOR_Y,
HPACEL_SENSOR_Z,
HPACEL_SENSOR_COUNT
};
#define LIS3LV02DL_ID 0x3A
enum lis3lv02dl_reg {
WHO_AM_I = 0x0F,
OFFSET_X = 0x16,
OFFSET_Y = 0x17,
OFFSET_Z = 0x18,
GAIN_X = 0x19,
GAIN_Y = 0x1A,
GAIN_Z = 0x1B,
CTRL_REG1 = 0x20,
CTRL_REG2 = 0x21,
CTRL_REG3 = 0x22,
HP_FILTER_RESET = 0x23,
STATUS_REG = 0x27,
OUTX_L = 0x28,
OUTX_H = 0x29,
OUTY_L = 0x2A,
OUTY_H = 0x2B,
OUTZ_L = 0x2C,
OUTZ_H = 0x2D,
FF_WU_CFG = 0x30,
FF_WU_SRC = 0x31,
FF_WU_ACK = 0x32,
FF_WU_THS_L = 0x34,
FF_WU_THS_H = 0x35,
FF_WU_DURATION = 0x36,
DD_CFG = 0x38,
DD_SRC = 0x39,
DD_ACK = 0x3A,
DD_THSI_L = 0x3C,
DD_THSI_H = 0x3D,
DD_THSE_L = 0x3E,
DD_THSE_H = 0x3F
};
enum lis3lv02dl_ctrl1 {
CTRL1_Xen = (1 << 0),
CTRL1_Yen = (1 << 1),
CTRL1_Zen = (1 << 2),
CTRL1_ST = (1 << 3),
CTRL1_DF0 = (1 << 4),
CTRL1_DF1 = (1 << 5),
CTRL1_PD0 = (1 << 6),
CTRL1_PD1 = (1 << 7)
};
enum lis3lv02dl_ctrl2 {
CTRL2_DAS = (1 << 0),
CTRL2_SIM = (1 << 1),
CTRL2_DRDY = (1 << 2),
CTRL2_IEN = (1 << 3),
CTRL2_BOOT = (1 << 4),
CTRL2_BLE = (1 << 5),
CTRL2_BDU = (1 << 6),
CTRL2_FS = (1 << 7)
};
enum lis3lv02dl_ctrl3 {
CTRL3_CFS0 = (1 << 0),
CTRL3_CFS1 = (1 << 1),
CTRL3_FDS = (1 << 4),
CTRL3_HPFF = (1 << 5),
CTRL3_HPDD = (1 << 6),
CTRL3_ECK = (1 << 7)
};
struct hpacel_softc {
device_t sc_dev;
struct acpi_devnode *sc_node;
struct sysmon_envsys *sc_sme;
bool sc_state;
uint8_t sc_whoami;
uint8_t sc_ctrl[3];
envsys_data_t sc_sensor[HPACEL_SENSOR_COUNT];
};
static const struct device_compatible_entry compat_data[] = {
{ .compat = "HPQ0004" },
DEVICE_COMPAT_EOL
};
static int hpacel_match(device_t, cfdata_t, void *);
static void hpacel_attach(device_t, device_t, void *);
static int hpacel_detach(device_t, int);
static bool hpacel_reg_init(device_t);
static bool hpacel_suspend(device_t, const pmf_qual_t *);
static bool hpacel_resume(device_t, const pmf_qual_t *);
static ACPI_STATUS hpacel_reg_info(device_t);
static ACPI_STATUS hpacel_reg_read(ACPI_HANDLE, ACPI_INTEGER, uint8_t *);
static ACPI_STATUS hpacel_reg_write(ACPI_HANDLE, ACPI_INTEGER, uint8_t);
static ACPI_STATUS hpacel_reg_xyz(ACPI_HANDLE, const int, int16_t *);
static ACPI_STATUS hpacel_power(device_t, bool);
static bool hpacel_sensor_init(device_t);
static void hpacel_sensor_refresh(struct sysmon_envsys *,
envsys_data_t *);
CFATTACH_DECL_NEW(hpacel, sizeof(struct hpacel_softc),
hpacel_match, hpacel_attach, hpacel_detach, NULL);
static int
hpacel_match(device_t parent, cfdata_t match, void *aux)
{
struct acpi_attach_args *aa = aux;
return acpi_compatible_match(aa, compat_data);
}
static void
hpacel_attach(device_t parent, device_t self, void *aux)
{
struct hpacel_softc *sc = device_private(self);
struct acpi_attach_args *aa = aux;
sc->sc_sme = NULL;
sc->sc_dev = self;
sc->sc_state = false;
sc->sc_node = aa->aa_node;
aprint_naive("\n");
aprint_normal(": HP 3D DriveGuard accelerometer\n");
if (hpacel_reg_init(self) != true)
return;
(void)pmf_device_register(self, hpacel_suspend, hpacel_resume);
if (hpacel_sensor_init(self) != false)
(void)hpacel_power(self, true);
sc->sc_state = true;
}
static int
hpacel_detach(device_t self, int flags)
{
struct hpacel_softc *sc = device_private(self);
if (sc->sc_state != false)
(void)hpacel_power(self, false);
if (sc->sc_sme != NULL)
sysmon_envsys_unregister(sc->sc_sme);
return 0;
}
static bool
hpacel_suspend(device_t self, const pmf_qual_t *qual)
{
struct hpacel_softc *sc = device_private(self);
if (sc->sc_state != false)
(void)hpacel_power(self, false);
return true;
}
static bool
hpacel_resume(device_t self, const pmf_qual_t *qual)
{
struct hpacel_softc *sc = device_private(self);
if (sc->sc_state != false)
(void)hpacel_power(self, true);
return true;
}
static bool
hpacel_reg_init(device_t self)
{
struct hpacel_softc *sc = device_private(self);
ACPI_HANDLE hdl = sc->sc_node->ad_handle;
ACPI_STATUS rv;
uint8_t val;
rv = AcpiEvaluateObject(hdl, "_INI", NULL, NULL);
if (ACPI_FAILURE(rv))
goto out;
rv = hpacel_reg_info(self);
if (ACPI_FAILURE(rv))
goto out;
val = sc->sc_ctrl[0];
if ((sc->sc_ctrl[0] & CTRL1_Xen) == 0)
val |= CTRL1_Xen;
if ((sc->sc_ctrl[0] & CTRL1_Yen) == 0)
val |= CTRL1_Yen;
if ((sc->sc_ctrl[0] & CTRL1_Zen) == 0)
val |= CTRL1_Zen;
if (val != sc->sc_ctrl[0]) {
rv = hpacel_reg_write(hdl, CTRL_REG1, val);
if (ACPI_FAILURE(rv))
return rv;
}
val = sc->sc_ctrl[1];
if ((sc->sc_ctrl[1] & CTRL2_BDU) == 0)
val |= CTRL2_BDU;
if ((sc->sc_ctrl[1] & CTRL2_BLE) != 0)
val &= ~CTRL2_BLE;
if ((sc->sc_ctrl[1] & CTRL2_DAS) != 0)
val &= ~CTRL2_DAS;
if ((sc->sc_ctrl[1] & CTRL2_DRDY) != 0)
val &= ~CTRL2_DRDY;
if ((sc->sc_ctrl[1] & CTRL2_IEN) != 0)
val &= ~CTRL2_IEN;
if (val != sc->sc_ctrl[1]) {
rv = hpacel_reg_write(hdl, CTRL_REG2, val);
if (ACPI_FAILURE(rv))
return rv;
}
(void)hpacel_reg_write(hdl, DD_CFG, 0x00);
(void)hpacel_reg_write(hdl, FF_WU_CFG, 0x00);
(void)hpacel_reg_info(self);
out:
if (ACPI_FAILURE(rv))
aprint_error_dev(self, "failed to initialize "
"device: %s\n", AcpiFormatException(rv));
return (rv != AE_OK) ? false : true;
}
static ACPI_STATUS
hpacel_reg_info(device_t self)
{
struct hpacel_softc *sc = device_private(self);
ACPI_HANDLE hdl = sc->sc_node->ad_handle;
ACPI_STATUS rv;
size_t i;
rv = hpacel_reg_read(hdl, WHO_AM_I, &sc->sc_whoami);
if (ACPI_FAILURE(rv))
return rv;
for (i = 0; i < __arraycount(sc->sc_sensor); i++) {
rv = hpacel_reg_read(hdl, CTRL_REG1 + i, &sc->sc_ctrl[i]);
if (ACPI_FAILURE(rv))
return rv;
}
return AE_OK;
}
static ACPI_STATUS
hpacel_reg_read(ACPI_HANDLE hdl, ACPI_INTEGER reg, uint8_t *valp)
{
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 (ACPI_FAILURE(rv))
return rv;
if (val.Integer.Value > UINT8_MAX)
return AE_AML_NUMERIC_OVERFLOW;
*valp = val.Integer.Value;
return AE_OK;
}
static ACPI_STATUS
hpacel_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 ACPI_STATUS
hpacel_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 HPACEL_SENSOR_X:
reg[0] = OUTX_L;
reg[1] = OUTX_H;
break;
case HPACEL_SENSOR_Y:
reg[0] = OUTY_L;
reg[1] = OUTY_H;
break;
case HPACEL_SENSOR_Z:
reg[0] = OUTZ_L;
reg[1] = OUTZ_H;
break;
default:
return AE_BAD_PARAMETER;
}
rv[0] = hpacel_reg_read(hdl, reg[0], &lo);
rv[1] = hpacel_reg_read(hdl, reg[1], &hi);
if (ACPI_FAILURE(rv[0]) || ACPI_FAILURE(rv[1]))
return AE_ERROR;
hi = (hi & 0x10) ? hi | 0xE0 : hi & ~0xE0;
*out = (hi << 8) | lo;
return AE_OK;
}
static ACPI_STATUS
hpacel_power(device_t self, bool enable)
{
struct hpacel_softc *sc = device_private(self);
ACPI_HANDLE hdl = sc->sc_node->ad_handle;
ACPI_OBJECT_LIST arg;
ACPI_OBJECT obj;
ACPI_STATUS rv;
uint8_t val;
rv = hpacel_reg_info(self);
if (ACPI_FAILURE(rv))
return rv;
val = sc->sc_ctrl[0];
if (enable != false)
val |= CTRL1_PD0 | CTRL1_PD1;
else {
val &= ~(CTRL1_PD0 | CTRL1_PD1);
}
if (val != sc->sc_ctrl[0]) {
rv = hpacel_reg_write(hdl, CTRL_REG1, val);
if (ACPI_FAILURE(rv))
return rv;
}
obj.Type = ACPI_TYPE_INTEGER;
obj.Integer.Value = enable;
arg.Count = 1;
arg.Pointer = &obj;
(void)AcpiEvaluateObject(hdl, "ALED", &arg, NULL);
return rv;
}
static bool
hpacel_sensor_init(device_t self)
{
const char zyx[HPACEL_SENSOR_COUNT] = { 'x', 'y', 'z' };
struct hpacel_softc *sc = device_private(self);
size_t i;
int rv;
CTASSERT(HPACEL_SENSOR_X == 0);
CTASSERT(HPACEL_SENSOR_Y == 1);
CTASSERT(HPACEL_SENSOR_Z == 2);
sc->sc_sme = sysmon_envsys_create();
for (i = 0; i < __arraycount(sc->sc_sensor); i++) {
sc->sc_sensor[i].units = ENVSYS_INTEGER;
sc->sc_sensor[i].state = ENVSYS_SINVALID;
sc->sc_sensor[i].flags = ENVSYS_FHAS_ENTROPY;
(void)snprintf(sc->sc_sensor[i].desc,
ENVSYS_DESCLEN, "%c-acceleration", zyx[i]);
rv = sysmon_envsys_sensor_attach(sc->sc_sme,&sc->sc_sensor[i]);
if (rv != 0)
goto fail;
}
sc->sc_sme->sme_cookie = sc;
sc->sc_sme->sme_flags = SME_POLL_ONLY;
sc->sc_sme->sme_name = device_xname(self);
sc->sc_sme->sme_refresh = hpacel_sensor_refresh;
rv = sysmon_envsys_register(sc->sc_sme);
if (rv != 0)
goto fail;
return true;
fail:
aprint_error_dev(self, "failed to initialize sensors\n");
sysmon_envsys_destroy(sc->sc_sme);
sc->sc_sme = NULL;
return false;
}
static void
hpacel_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
struct hpacel_softc *sc = sme->sme_cookie;
ACPI_STATUS rv;
int16_t val;
size_t i;
for (i = 0; i < __arraycount(sc->sc_sensor); i++) {
rv = hpacel_reg_xyz(sc->sc_node->ad_handle, i, &val);
if (ACPI_SUCCESS(rv)) {
sc->sc_sensor[i].value_cur = val;
sc->sc_sensor[i].state = ENVSYS_SVALID;
continue;
}
sc->sc_sensor[i].state = ENVSYS_SINVALID;
}
}
MODULE(MODULE_CLASS_DRIVER, hpacel, "sysmon_envsys");
#ifdef _MODULE
#include "ioconf.c"
#endif
static int
hpacel_modcmd(modcmd_t cmd, void *aux)
{
int rv = 0;
switch (cmd) {
case MODULE_CMD_INIT:
#ifdef _MODULE
rv = config_init_component(cfdriver_ioconf_hpacel,
cfattach_ioconf_hpacel, cfdata_ioconf_hpacel);
#endif
break;
case MODULE_CMD_FINI:
#ifdef _MODULE
rv = config_fini_component(cfdriver_ioconf_hpacel,
cfattach_ioconf_hpacel, cfdata_ioconf_hpacel);
#endif
break;
default:
rv = ENOTTY;
}
return rv;
}