xref: /freebsd/sys/arm/allwinner/aw_thermal.c (revision 7447ca0eb235974642312b9555caec00b57d8fc1)
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * Allwinner thermal sensor controller
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/module.h>
43 #include <machine/bus.h>
44 
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include <arm/allwinner/aw_sid.h>
49 
50 #define	THS_CTRL0		0x00
51 #define	THS_CTRL2		0x40
52 #define	 SENSOR_ACQ1_SHIFT	16
53 #define	 SENSOR2_EN		(1 << 2)
54 #define	 SENSOR1_EN		(1 << 1)
55 #define	 SENSOR0_EN		(1 << 0)
56 #define	THS_INTC		0x44
57 #define	THS_INTS		0x48
58 #define	THS_FILTER		0x70
59 #define	 FILTER_EN		(1 << 2)
60 #define	THS_CALIB0		0x74
61 #define	THS_CALIB1		0x78
62 #define	THS_DATA0		0x80
63 #define	THS_DATA1		0x84
64 #define	THS_DATA2		0x88
65 #define	 DATA_MASK		0xfff
66 
67 #define	TEMP_BASE		2719
68 #define	TEMP_MUL		1000
69 #define	TEMP_DIV		14186
70 #define	TEMP_TO_K		273
71 #define	ADC_ACQUIRE_TIME	(24 - 1)
72 #define	SENSOR_ENABLE_ALL	(SENSOR0_EN|SENSOR1_EN|SENSOR2_EN)
73 
74 enum aw_thermal_sensor {
75 	THS_SENSOR_CPU_CLUSTER0,
76 	THS_SENSOR_CPU_CLUSTER1,
77 	THS_SENSOR_GPU,
78 	THS_SENSOR_END = -1
79 };
80 
81 struct aw_thermal_sensor_config {
82 	enum aw_thermal_sensor	sensor;
83 	const char		*name;
84 	const char		*desc;
85 };
86 
87 static const struct aw_thermal_sensor_config a83t_sensor_config[] = {
88 	{ .sensor = THS_SENSOR_CPU_CLUSTER0,
89 	  .name = "cluster0",	.desc = "CPU cluster 0 temperature" },
90 	{ .sensor = THS_SENSOR_CPU_CLUSTER1,
91 	  .name = "cluster1",	.desc = "CPU cluster 1 temperature" },
92 	{ .sensor = THS_SENSOR_GPU,
93 	  .name = "gpu",	.desc = "GPU temperature" },
94 	{ .sensor = THS_SENSOR_END }
95 };
96 
97 static struct ofw_compat_data compat_data[] = {
98 	{ "allwinner,sun8i-a83t-ts",	(uintptr_t)&a83t_sensor_config },
99 	{ NULL,				(uintptr_t)NULL }
100 };
101 
102 #define	THS_CONF(d)		\
103 	(void *)ofw_bus_search_compatible((d), compat_data)->ocd_data
104 
105 struct aw_thermal_softc {
106 	struct resource			*res;
107 	struct aw_thermal_sensor_config	*conf;
108 };
109 
110 static struct resource_spec aw_thermal_spec[] = {
111 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
112 	{ -1, 0 }
113 };
114 
115 #define	RD4(sc, reg)		bus_read_4((sc)->res, (reg))
116 #define	WR4(sc, reg, val)	bus_write_4((sc)->res, (reg), (val))
117 
118 static int
119 aw_thermal_init(struct aw_thermal_softc *sc)
120 {
121 	uint32_t calib0, calib1;
122 	int error;
123 
124 	/* Read calibration settings from SRAM */
125 	error = aw_sid_read_tscalib(&calib0, &calib1);
126 	if (error != 0)
127 		return (error);
128 
129 	/* Write calibration settings to thermal controller */
130 	WR4(sc, THS_CALIB0, calib0);
131 	WR4(sc, THS_CALIB1, calib1);
132 
133 	/* Configure ADC acquire time (CLK_IN/(N+1)) and enable sensors */
134 	WR4(sc, THS_CTRL0, ADC_ACQUIRE_TIME);
135 	WR4(sc, THS_CTRL2, (ADC_ACQUIRE_TIME << SENSOR_ACQ1_SHIFT) |
136 	    SENSOR_ENABLE_ALL);
137 
138 	/* Disable interrupts */
139 	WR4(sc, THS_INTC, 0);
140 	WR4(sc, THS_INTS, RD4(sc, THS_INTS));
141 
142 	/* Enable average filter */
143 	WR4(sc, THS_FILTER, RD4(sc, THS_FILTER) | FILTER_EN);
144 
145 	return (0);
146 }
147 
148 static int
149 aw_thermal_gettemp(uint32_t val)
150 {
151 	int raw;
152 
153 	raw = val & DATA_MASK;
154 	return (((TEMP_BASE - raw) * TEMP_MUL) / TEMP_DIV) + TEMP_TO_K;
155 }
156 
157 static int
158 aw_thermal_sysctl(SYSCTL_HANDLER_ARGS)
159 {
160 	struct aw_thermal_softc *sc;
161 	enum aw_thermal_sensor sensor;
162 	int val;
163 
164 	sc = arg1;
165 	sensor = arg2;
166 
167 	val = aw_thermal_gettemp(RD4(sc, THS_DATA0 + (sensor * 4)));
168 
169 	return sysctl_handle_opaque(oidp, &val, sizeof(val), req);
170 }
171 
172 static int
173 aw_thermal_probe(device_t dev)
174 {
175 	if (!ofw_bus_status_okay(dev))
176 		return (ENXIO);
177 
178 	if (THS_CONF(dev) == NULL)
179 		return (ENXIO);
180 
181 	device_set_desc(dev, "Allwinner Thermal Sensor Controller");
182 	return (BUS_PROBE_DEFAULT);
183 }
184 
185 static int
186 aw_thermal_attach(device_t dev)
187 {
188 	struct aw_thermal_softc *sc;
189 	int i;
190 
191 	sc = device_get_softc(dev);
192 
193 	sc->conf = THS_CONF(dev);
194 
195 	if (bus_alloc_resources(dev, aw_thermal_spec, &sc->res) != 0) {
196 		device_printf(dev, "cannot allocate resources for device\n");
197 		return (ENXIO);
198 	}
199 
200 	if (aw_thermal_init(sc) != 0)
201 		return (ENXIO);
202 
203 	for (i = 0; sc->conf[i].sensor != THS_SENSOR_END; i++)
204 		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
205 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
206 		    OID_AUTO, sc->conf[i].name,
207 		    CTLTYPE_INT | CTLFLAG_RD,
208 		    sc, sc->conf[i].sensor, aw_thermal_sysctl, "IK0",
209 		    sc->conf[i].desc);
210 
211 	return (0);
212 }
213 
214 static device_method_t aw_thermal_methods[] = {
215 	/* Device interface */
216 	DEVMETHOD(device_probe,		aw_thermal_probe),
217 	DEVMETHOD(device_attach,	aw_thermal_attach),
218 
219 	DEVMETHOD_END
220 };
221 
222 static driver_t aw_thermal_driver = {
223 	"aw_thermal",
224 	aw_thermal_methods,
225 	sizeof(struct aw_thermal_softc),
226 };
227 
228 static devclass_t aw_thermal_devclass;
229 
230 DRIVER_MODULE(aw_thermal, simplebus, aw_thermal_driver, aw_thermal_devclass,
231     0, 0);
232 MODULE_VERSION(aw_thermal, 1);
233