diff options
author | Mark Kettenis <kettenis@cvs.openbsd.org> | 2017-12-31 13:54:10 +0000 |
---|---|---|
committer | Mark Kettenis <kettenis@cvs.openbsd.org> | 2017-12-31 13:54:10 +0000 |
commit | 79e73cea79408781eeeb1efa8516cbb7cbafdaa8 (patch) | |
tree | bc986312acf66f987b3249779fabb95878d85f32 /sys/dev | |
parent | 95f815577837f44170ee445cac5f03db3c3dc65c (diff) |
Add sxitemp(4), a driver for the temperature sensors on the Allwinner H5 SoC.
Diffstat (limited to 'sys/dev')
-rw-r--r-- | sys/dev/fdt/files.fdt | 6 | ||||
-rw-r--r-- | sys/dev/fdt/sxitemp.c | 162 |
2 files changed, 167 insertions, 1 deletions
diff --git a/sys/dev/fdt/files.fdt b/sys/dev/fdt/files.fdt index 5abe2d9c020..ece9a897294 100644 --- a/sys/dev/fdt/files.fdt +++ b/sys/dev/fdt/files.fdt @@ -1,4 +1,4 @@ -# $OpenBSD: files.fdt,v 1.33 2017/12/31 11:13:59 kettenis Exp $ +# $OpenBSD: files.fdt,v 1.34 2017/12/31 13:54:09 kettenis Exp $ # # Config file and device description for machine-independent FDT code. # Included by ports that need it. @@ -28,6 +28,10 @@ device sximmc: sdmmcbus attach sximmc at fdt file dev/fdt/sximmc.c sximmc +device sxitemp +attach sxitemp at fdt +file dev/fdt/sxitemp.c sxitemp + device sxitwi: i2cbus attach sxitwi at fdt file dev/fdt/sxitwi.c sxitwi diff --git a/sys/dev/fdt/sxitemp.c b/sys/dev/fdt/sxitemp.c new file mode 100644 index 00000000000..62fbf8643f9 --- /dev/null +++ b/sys/dev/fdt/sxitemp.c @@ -0,0 +1,162 @@ +/* $OpenBSD: sxitemp.c,v 1.1 2017/12/31 13:54:09 kettenis Exp $ */ +/* + * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/device.h> +#include <sys/sensors.h> + +#include <machine/intr.h> +#include <machine/bus.h> +#include <machine/fdt.h> + +#include <dev/ofw/openfirm.h> +#include <dev/ofw/ofw_clock.h> +#include <dev/ofw/ofw_misc.h> +#include <dev/ofw/ofw_pinctrl.h> +#include <dev/ofw/fdt.h> + +/* Registers */ +#define THS_CTRL0 0x0000 +#define THS_CTRL0_SENSOR_ACQ(x) ((x) & 0xffff) +#define THS_CTRL2 0x0040 +#define THS_CTRL2_ADC_ACQ(x) (((x) & 0xffff) << 16) +#define THS_CTRL2_SENSE1_EN (1 << 1) +#define THS_CTRL2_SENSE0_EN (1 << 0) +#define THS_INT_CTRL 0x0044 +#define THS_INT_CTRL_THERMAL_PER(x) (((x) & 0xfffff) << 12) +#define THS_FILTER 0x0070 +#define THS_FILTER_EN (1 << 2) +#define THS_FILTER_TYPE(x) ((x) & 0x3) +#define THS0_DATA 0x0080 +#define THS1_DATA 0x0084 + +#define HREAD4(sc, reg) \ + (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))) +#define HWRITE4(sc, reg, val) \ + bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) + +struct sxitemp_softc { + struct device sc_dev; + bus_space_tag_t sc_iot; + bus_space_handle_t sc_ioh; + + struct ksensor sc_sensors[2]; + struct ksensordev sc_sensordev; +}; + +int sxitemp_match(struct device *, void *, void *); +void sxitemp_attach(struct device *, struct device *, void *); + +struct cfattach sxitemp_ca = { + sizeof (struct sxitemp_softc), sxitemp_match, sxitemp_attach +}; + +struct cfdriver sxitemp_cd = { + NULL, "sxitemp", DV_DULL +}; + +void sxitemp_refresh_sensors(void *); + +int +sxitemp_match(struct device *parent, void *match, void *aux) +{ + struct fdt_attach_args *faa = aux; + + return (OF_is_compatible(faa->fa_node, "allwinner,sun50i-h5-ths")); +} + +void +sxitemp_attach(struct device *parent, struct device *self, void *aux) +{ + struct sxitemp_softc *sc = (struct sxitemp_softc *)self; + struct fdt_attach_args *faa = aux; + int node = faa->fa_node; + + if (faa->fa_nreg < 1) { + printf(": no registers\n"); + return; + } + + sc->sc_iot = faa->fa_iot; + if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, + faa->fa_reg[0].size, 0, &sc->sc_ioh)) { + printf(": can't map registers\n"); + return; + } + + printf("\n"); + + pinctrl_byname(node, "default"); + + clock_enable_all(node); + reset_deassert_all(node); + + /* Start data acquisition. */ + HWRITE4(sc, THS_FILTER, THS_FILTER_EN | THS_FILTER_TYPE(1)); + HWRITE4(sc, THS_INT_CTRL, THS_INT_CTRL_THERMAL_PER(800)); + HWRITE4(sc, THS_CTRL0, THS_CTRL0_SENSOR_ACQ(31)); + HWRITE4(sc, THS_CTRL2, THS_CTRL2_ADC_ACQ(31) | + THS_CTRL2_SENSE0_EN | THS_CTRL2_SENSE1_EN); + + /* Register sensors. */ + strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, + sizeof(sc->sc_sensordev.xname)); + strlcpy(sc->sc_sensors[0].desc, "CPU", sizeof(sc->sc_sensors[0].desc)); + sc->sc_sensors[0].type = SENSOR_TEMP; + sc->sc_sensors[0].flags = SENSOR_FINVALID; + sensor_attach(&sc->sc_sensordev, &sc->sc_sensors[0]); + strlcpy(sc->sc_sensors[1].desc, "GPU", sizeof(sc->sc_sensors[1].desc)); + sc->sc_sensors[1].type = SENSOR_TEMP; + sc->sc_sensors[1].flags = SENSOR_FINVALID; + sensor_attach(&sc->sc_sensordev, &sc->sc_sensors[1]); + sensordev_install(&sc->sc_sensordev); + sensor_task_register(sc, sxitemp_refresh_sensors, 5); +} + +uint64_t +sxitemp_calc_temp0(int64_t data) +{ + if (data > 0x500) + return -119100 * data + 223000000; + else + return -145200 * data + 259000000; +} + +uint64_t +sxitemp_calc_temp1(int64_t data) +{ + if (data > 0x500) + return -119100 * data + 223000000; + else + return -159000 * data + 276000000; +} + +void +sxitemp_refresh_sensors(void *arg) +{ + struct sxitemp_softc *sc = arg; + uint32_t data; + + data = HREAD4(sc, THS0_DATA); + sc->sc_sensors[0].value = sxitemp_calc_temp0(data) + 273150000; + sc->sc_sensors[0].flags &= ~SENSOR_FINVALID; + + data = HREAD4(sc, THS1_DATA); + sc->sc_sensors[1].value = sxitemp_calc_temp1(data) + 273150000; + sc->sc_sensors[1].flags &= ~SENSOR_FINVALID; +} |