sxitemp.c revision 1.1
1/*	$OpenBSD: sxitemp.c,v 1.1 2017/12/31 13:54:09 kettenis Exp $	*/
2/*
3 * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/param.h>
19#include <sys/systm.h>
20#include <sys/device.h>
21#include <sys/sensors.h>
22
23#include <machine/intr.h>
24#include <machine/bus.h>
25#include <machine/fdt.h>
26
27#include <dev/ofw/openfirm.h>
28#include <dev/ofw/ofw_clock.h>
29#include <dev/ofw/ofw_misc.h>
30#include <dev/ofw/ofw_pinctrl.h>
31#include <dev/ofw/fdt.h>
32
33/* Registers */
34#define THS_CTRL0			0x0000
35#define  THS_CTRL0_SENSOR_ACQ(x)	((x) & 0xffff)
36#define THS_CTRL2			0x0040
37#define  THS_CTRL2_ADC_ACQ(x)		(((x) & 0xffff) << 16)
38#define  THS_CTRL2_SENSE1_EN		(1 << 1)
39#define  THS_CTRL2_SENSE0_EN		(1 << 0)
40#define THS_INT_CTRL			0x0044
41#define  THS_INT_CTRL_THERMAL_PER(x)	(((x) & 0xfffff) << 12)
42#define THS_FILTER			0x0070
43#define  THS_FILTER_EN			(1 << 2)
44#define  THS_FILTER_TYPE(x)		((x) & 0x3)
45#define THS0_DATA			0x0080
46#define THS1_DATA			0x0084
47
48#define HREAD4(sc, reg)							\
49	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
50#define HWRITE4(sc, reg, val)						\
51	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
52
53struct sxitemp_softc {
54	struct device		sc_dev;
55	bus_space_tag_t		sc_iot;
56	bus_space_handle_t	sc_ioh;
57
58	struct ksensor		sc_sensors[2];
59	struct ksensordev	sc_sensordev;
60};
61
62int	sxitemp_match(struct device *, void *, void *);
63void	sxitemp_attach(struct device *, struct device *, void *);
64
65struct cfattach	sxitemp_ca = {
66	sizeof (struct sxitemp_softc), sxitemp_match, sxitemp_attach
67};
68
69struct cfdriver sxitemp_cd = {
70	NULL, "sxitemp", DV_DULL
71};
72
73void	sxitemp_refresh_sensors(void *);
74
75int
76sxitemp_match(struct device *parent, void *match, void *aux)
77{
78	struct fdt_attach_args *faa = aux;
79
80	return (OF_is_compatible(faa->fa_node, "allwinner,sun50i-h5-ths"));
81}
82
83void
84sxitemp_attach(struct device *parent, struct device *self, void *aux)
85{
86	struct sxitemp_softc *sc = (struct sxitemp_softc *)self;
87	struct fdt_attach_args *faa = aux;
88	int node = faa->fa_node;
89
90	if (faa->fa_nreg < 1) {
91		printf(": no registers\n");
92		return;
93	}
94
95	sc->sc_iot = faa->fa_iot;
96	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
97	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
98		printf(": can't map registers\n");
99		return;
100	}
101
102	printf("\n");
103
104	pinctrl_byname(node, "default");
105
106	clock_enable_all(node);
107	reset_deassert_all(node);
108
109	/* Start data acquisition. */
110	HWRITE4(sc, THS_FILTER, THS_FILTER_EN | THS_FILTER_TYPE(1));
111	HWRITE4(sc, THS_INT_CTRL, THS_INT_CTRL_THERMAL_PER(800));
112	HWRITE4(sc, THS_CTRL0, THS_CTRL0_SENSOR_ACQ(31));
113	HWRITE4(sc, THS_CTRL2, THS_CTRL2_ADC_ACQ(31) |
114	    THS_CTRL2_SENSE0_EN | THS_CTRL2_SENSE1_EN);
115
116	/* Register sensors. */
117	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
118	    sizeof(sc->sc_sensordev.xname));
119	strlcpy(sc->sc_sensors[0].desc, "CPU", sizeof(sc->sc_sensors[0].desc));
120	sc->sc_sensors[0].type = SENSOR_TEMP;
121	sc->sc_sensors[0].flags = SENSOR_FINVALID;
122	sensor_attach(&sc->sc_sensordev, &sc->sc_sensors[0]);
123	strlcpy(sc->sc_sensors[1].desc, "GPU", sizeof(sc->sc_sensors[1].desc));
124	sc->sc_sensors[1].type = SENSOR_TEMP;
125	sc->sc_sensors[1].flags = SENSOR_FINVALID;
126	sensor_attach(&sc->sc_sensordev, &sc->sc_sensors[1]);
127	sensordev_install(&sc->sc_sensordev);
128	sensor_task_register(sc, sxitemp_refresh_sensors, 5);
129}
130
131uint64_t
132sxitemp_calc_temp0(int64_t data)
133{
134	if (data > 0x500)
135		return -119100 * data + 223000000;
136	else
137		return -145200 * data + 259000000;
138}
139
140uint64_t
141sxitemp_calc_temp1(int64_t data)
142{
143	if (data > 0x500)
144		return -119100 * data + 223000000;
145	else
146		return -159000 * data + 276000000;
147}
148
149void
150sxitemp_refresh_sensors(void *arg)
151{
152	struct sxitemp_softc *sc = arg;
153	uint32_t data;
154
155	data = HREAD4(sc, THS0_DATA);
156	sc->sc_sensors[0].value = sxitemp_calc_temp0(data) + 273150000;
157	sc->sc_sensors[0].flags &= ~SENSOR_FINVALID;
158
159	data = HREAD4(sc, THS1_DATA);
160	sc->sc_sensors[1].value = sxitemp_calc_temp1(data) + 273150000;
161	sc->sc_sensors[1].flags &= ~SENSOR_FINVALID;
162}
163