apple_smc_temp.c revision 1.3 1 1.1 riastrad /* $NetBSD: apple_smc_temp.c,v 1.3 2014/04/01 17:48:52 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Apple System Management Controller: Temperature Sensors
5 1.1 riastrad */
6 1.1 riastrad
7 1.1 riastrad /*-
8 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
9 1.1 riastrad * All rights reserved.
10 1.1 riastrad *
11 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
12 1.1 riastrad * by Taylor R. Campbell.
13 1.1 riastrad *
14 1.1 riastrad * Redistribution and use in source and binary forms, with or without
15 1.1 riastrad * modification, are permitted provided that the following conditions
16 1.1 riastrad * are met:
17 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
18 1.1 riastrad * notice, this list of conditions and the following disclaimer.
19 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
21 1.1 riastrad * documentation and/or other materials provided with the distribution.
22 1.1 riastrad *
23 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
34 1.1 riastrad */
35 1.1 riastrad
36 1.1 riastrad #include <sys/cdefs.h>
37 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: apple_smc_temp.c,v 1.3 2014/04/01 17:48:52 riastradh Exp $");
38 1.1 riastrad
39 1.1 riastrad #include <sys/types.h>
40 1.1 riastrad #include <sys/param.h>
41 1.1 riastrad #include <sys/device.h>
42 1.1 riastrad #include <sys/kmem.h>
43 1.2 riastrad #include <sys/module.h>
44 1.1 riastrad #include <sys/systm.h>
45 1.1 riastrad
46 1.1 riastrad #include <dev/ic/apple_smc.h>
47 1.1 riastrad
48 1.1 riastrad #include <dev/sysmon/sysmonvar.h>
49 1.1 riastrad
50 1.1 riastrad struct apple_smc_temp_softc {
51 1.1 riastrad device_t sc_dev;
52 1.1 riastrad struct apple_smc_tag *sc_smc;
53 1.1 riastrad struct sysmon_envsys *sc_sme;
54 1.1 riastrad struct {
55 1.1 riastrad struct apple_smc_key *sensor_key;
56 1.1 riastrad struct envsys_data sensor_data;
57 1.1 riastrad } *sc_sensors;
58 1.1 riastrad size_t sc_nsensors;
59 1.1 riastrad };
60 1.1 riastrad
61 1.1 riastrad static int apple_smc_temp_match(device_t, cfdata_t, void *);
62 1.1 riastrad static void apple_smc_temp_attach(device_t, device_t, void *);
63 1.1 riastrad static int apple_smc_temp_detach(device_t, int);
64 1.1 riastrad static void apple_smc_temp_refresh(struct sysmon_envsys *,
65 1.1 riastrad struct envsys_data *);
66 1.1 riastrad static int apple_smc_temp_count_sensors(struct apple_smc_tag *,
67 1.1 riastrad uint32_t *);
68 1.1 riastrad static void apple_smc_temp_count_sensors_scanner(struct apple_smc_tag *,
69 1.1 riastrad void *, struct apple_smc_key *);
70 1.1 riastrad static int apple_smc_temp_find_sensors(struct apple_smc_temp_softc *);
71 1.1 riastrad static int apple_smc_temp_find_sensors_init(struct apple_smc_tag *,
72 1.1 riastrad void *, uint32_t);
73 1.1 riastrad static void apple_smc_temp_find_sensors_scanner(struct apple_smc_tag *,
74 1.1 riastrad void *, struct apple_smc_key *);
75 1.1 riastrad static void apple_smc_temp_release_keys(struct apple_smc_temp_softc *);
76 1.1 riastrad static int apple_smc_scan_temp_sensors(struct apple_smc_tag *, void *,
77 1.1 riastrad int (*)(struct apple_smc_tag *, void *, uint32_t),
78 1.1 riastrad void (*)(struct apple_smc_tag *, void *,
79 1.1 riastrad struct apple_smc_key *));
80 1.1 riastrad static int apple_smc_bound_temp_sensors(struct apple_smc_tag *,
81 1.1 riastrad uint32_t *, uint32_t *);
82 1.1 riastrad static bool apple_smc_temp_sensor_p(const struct apple_smc_key *);
83 1.1 riastrad
84 1.1 riastrad CFATTACH_DECL_NEW(apple_smc_temp, sizeof(struct apple_smc_temp_softc),
85 1.1 riastrad apple_smc_temp_match, apple_smc_temp_attach, apple_smc_temp_detach, NULL);
86 1.1 riastrad
87 1.1 riastrad static int
88 1.1 riastrad apple_smc_temp_match(device_t parent, cfdata_t match, void *aux)
89 1.1 riastrad {
90 1.1 riastrad const struct apple_smc_attach_args *asa = aux;
91 1.1 riastrad uint32_t nsensors;
92 1.1 riastrad int error;
93 1.1 riastrad
94 1.1 riastrad error = apple_smc_temp_count_sensors(asa->asa_smc, &nsensors);
95 1.1 riastrad if (error)
96 1.1 riastrad return 0;
97 1.1 riastrad
98 1.1 riastrad if (nsensors == 0)
99 1.1 riastrad return 0;
100 1.1 riastrad
101 1.1 riastrad return 1;
102 1.1 riastrad }
103 1.1 riastrad
104 1.1 riastrad static void
105 1.1 riastrad apple_smc_temp_attach(device_t parent, device_t self, void *aux)
106 1.1 riastrad {
107 1.1 riastrad struct apple_smc_temp_softc *sc = device_private(self);
108 1.1 riastrad const struct apple_smc_attach_args *asa = aux;
109 1.1 riastrad
110 1.1 riastrad sc->sc_dev = self;
111 1.1 riastrad sc->sc_smc = asa->asa_smc;
112 1.1 riastrad sc->sc_sme = sysmon_envsys_create();
113 1.1 riastrad sc->sc_sme->sme_name = device_xname(self);
114 1.1 riastrad sc->sc_sme->sme_cookie = sc;
115 1.1 riastrad sc->sc_sme->sme_refresh = apple_smc_temp_refresh;
116 1.1 riastrad
117 1.1 riastrad (void)apple_smc_temp_find_sensors(sc);
118 1.1 riastrad }
119 1.1 riastrad
120 1.1 riastrad static int
121 1.1 riastrad apple_smc_temp_detach(device_t self, int flags)
122 1.1 riastrad {
123 1.1 riastrad struct apple_smc_temp_softc *sc = device_private(self);
124 1.1 riastrad
125 1.1 riastrad KASSERT(sc->sc_sme != NULL);
126 1.1 riastrad sysmon_envsys_unregister(sc->sc_sme);
127 1.1 riastrad sc->sc_sme = NULL;
128 1.1 riastrad
129 1.1 riastrad if (sc->sc_sensors != NULL) {
130 1.1 riastrad KASSERT(sc->sc_nsensors > 0);
131 1.1 riastrad apple_smc_temp_release_keys(sc);
132 1.1 riastrad kmem_free(sc->sc_sensors,
133 1.1 riastrad (sizeof(sc->sc_sensors[0]) * sc->sc_nsensors));
134 1.1 riastrad sc->sc_sensors = NULL;
135 1.1 riastrad sc->sc_nsensors = 0;
136 1.1 riastrad }
137 1.1 riastrad
138 1.1 riastrad return 0;
139 1.1 riastrad }
140 1.1 riastrad
141 1.1 riastrad static void
142 1.1 riastrad apple_smc_temp_refresh(struct sysmon_envsys *sme, struct envsys_data *edata)
143 1.1 riastrad {
144 1.1 riastrad struct apple_smc_temp_softc *sc = sme->sme_cookie;
145 1.1 riastrad const struct apple_smc_key *key;
146 1.1 riastrad uint16_t utemp16;
147 1.1 riastrad int32_t temp;
148 1.1 riastrad int error;
149 1.1 riastrad
150 1.1 riastrad if (edata->sensor >= sc->sc_nsensors) {
151 1.1 riastrad aprint_error_dev(sc->sc_dev, "unknown sensor %"PRIu32"\n",
152 1.1 riastrad edata->sensor);
153 1.1 riastrad return;
154 1.1 riastrad }
155 1.1 riastrad
156 1.1 riastrad key = sc->sc_sensors[edata->sensor].sensor_key;
157 1.1 riastrad error = apple_smc_read_key_2(sc->sc_smc, key, &utemp16);
158 1.1 riastrad if (error) {
159 1.1 riastrad aprint_error_dev(sc->sc_dev,
160 1.1 riastrad "failed to read temperature sensor %"PRIu32" (%s): %d\n",
161 1.1 riastrad edata->sensor, apple_smc_key_name(key), error);
162 1.1 riastrad edata->state = ENVSYS_SINVALID;
163 1.1 riastrad return;
164 1.1 riastrad }
165 1.1 riastrad
166 1.1 riastrad /* Sign-extend, in case we ever get below freezing... */
167 1.1 riastrad temp = (int16_t)utemp16;
168 1.1 riastrad
169 1.1 riastrad /* Convert to `millicentigrade'. */
170 1.1 riastrad temp *= 250;
171 1.1 riastrad temp >>= 6;
172 1.1 riastrad
173 1.1 riastrad /* Convert to millikelvins. */
174 1.1 riastrad temp += 273150;
175 1.1 riastrad
176 1.1 riastrad /* Finally, convert to microkelvins as sysmon_envsys wants. */
177 1.1 riastrad temp *= 1000;
178 1.1 riastrad
179 1.1 riastrad edata->value_cur = temp;
180 1.1 riastrad edata->state = ENVSYS_SVALID;
181 1.1 riastrad }
182 1.1 riastrad
183 1.1 riastrad static int
184 1.1 riastrad apple_smc_temp_count_sensors(struct apple_smc_tag *smc, uint32_t *nsensors)
185 1.1 riastrad {
186 1.1 riastrad
187 1.1 riastrad *nsensors = 0;
188 1.1 riastrad
189 1.1 riastrad return apple_smc_scan_temp_sensors(smc, nsensors,
190 1.1 riastrad NULL,
191 1.1 riastrad &apple_smc_temp_count_sensors_scanner);
192 1.1 riastrad }
193 1.1 riastrad
194 1.1 riastrad static void
195 1.1 riastrad apple_smc_temp_count_sensors_scanner(struct apple_smc_tag *smc, void *arg,
196 1.1 riastrad struct apple_smc_key *key)
197 1.1 riastrad {
198 1.1 riastrad uint32_t *nsensors = arg;
199 1.1 riastrad
200 1.1 riastrad (*nsensors)++;
201 1.1 riastrad apple_smc_release_key(smc, key);
202 1.1 riastrad }
203 1.1 riastrad
204 1.1 riastrad struct fss { /* Find Sensors State */
205 1.1 riastrad struct apple_smc_temp_softc *fss_sc;
206 1.1 riastrad size_t fss_sensor;
207 1.1 riastrad };
208 1.1 riastrad
209 1.1 riastrad static int
210 1.1 riastrad apple_smc_temp_find_sensors(struct apple_smc_temp_softc *sc)
211 1.1 riastrad {
212 1.1 riastrad struct fss fss;
213 1.1 riastrad int error;
214 1.1 riastrad
215 1.1 riastrad fss.fss_sc = sc;
216 1.1 riastrad fss.fss_sensor = 0;
217 1.1 riastrad
218 1.1 riastrad error = apple_smc_scan_temp_sensors(sc->sc_smc, &fss,
219 1.1 riastrad &apple_smc_temp_find_sensors_init,
220 1.1 riastrad &apple_smc_temp_find_sensors_scanner);
221 1.1 riastrad if (error)
222 1.1 riastrad return error;
223 1.1 riastrad
224 1.1 riastrad /* Shrink the array if we overshot. */
225 1.1 riastrad if (fss.fss_sensor < sc->sc_nsensors) {
226 1.1 riastrad void *sensors = kmem_alloc((fss.fss_sensor *
227 1.1 riastrad sizeof(sc->sc_sensors[0])), KM_SLEEP);
228 1.1 riastrad (void)memcpy(sensors, sc->sc_sensors,
229 1.1 riastrad (fss.fss_sensor * sizeof(sc->sc_sensors[0])));
230 1.1 riastrad kmem_free(sc->sc_sensors, sc->sc_nsensors);
231 1.1 riastrad sc->sc_nsensors = fss.fss_sensor;
232 1.1 riastrad sc->sc_sensors = sensors;
233 1.1 riastrad }
234 1.1 riastrad
235 1.1 riastrad return 0;
236 1.1 riastrad }
237 1.1 riastrad
238 1.1 riastrad static int
239 1.1 riastrad apple_smc_temp_find_sensors_init(struct apple_smc_tag *smc, void *arg,
240 1.1 riastrad uint32_t nsensors)
241 1.1 riastrad {
242 1.1 riastrad struct fss *fss = arg;
243 1.1 riastrad
244 1.1 riastrad fss->fss_sc->sc_nsensors = nsensors;
245 1.1 riastrad if (nsensors == 0)
246 1.1 riastrad fss->fss_sc->sc_sensors = NULL;
247 1.1 riastrad else
248 1.1 riastrad fss->fss_sc->sc_sensors = kmem_alloc((nsensors *
249 1.1 riastrad sizeof(fss->fss_sc->sc_sensors[0])), KM_SLEEP);
250 1.1 riastrad
251 1.1 riastrad return 0;
252 1.1 riastrad }
253 1.1 riastrad
254 1.1 riastrad static void
255 1.1 riastrad apple_smc_temp_find_sensors_scanner(struct apple_smc_tag *smc, void *arg,
256 1.1 riastrad struct apple_smc_key *key)
257 1.1 riastrad {
258 1.1 riastrad struct fss *fss = arg;
259 1.1 riastrad const uint32_t sensor = fss->fss_sensor;
260 1.1 riastrad struct envsys_data *edata =
261 1.1 riastrad &fss->fss_sc->sc_sensors[sensor].sensor_data;
262 1.1 riastrad int error;
263 1.1 riastrad
264 1.1 riastrad edata->units = ENVSYS_STEMP;
265 1.1 riastrad edata->state = ENVSYS_SINVALID;
266 1.1 riastrad edata->flags = ENVSYS_FHAS_ENTROPY;
267 1.1 riastrad
268 1.1 riastrad /*
269 1.1 riastrad * XXX Use a more meaningful name based on a table of known
270 1.1 riastrad * temperature sensors.
271 1.1 riastrad */
272 1.1 riastrad CTASSERT(sizeof(edata->desc) >= 4);
273 1.1 riastrad (void)strlcpy(edata->desc, apple_smc_key_name(key), 4);
274 1.1 riastrad
275 1.1 riastrad error = sysmon_envsys_sensor_attach(fss->fss_sc->sc_sme, edata);
276 1.1 riastrad if (error) {
277 1.1 riastrad aprint_error_dev(fss->fss_sc->sc_dev,
278 1.1 riastrad "failed to attach temperature sensor %s: %d\n",
279 1.1 riastrad apple_smc_key_name(key), error);
280 1.1 riastrad return;
281 1.1 riastrad }
282 1.1 riastrad
283 1.1 riastrad fss->fss_sc->sc_sensors[sensor].sensor_key = key;
284 1.1 riastrad
285 1.1 riastrad fss->fss_sensor++;
286 1.1 riastrad }
287 1.1 riastrad
288 1.1 riastrad static void
289 1.1 riastrad apple_smc_temp_release_keys(struct apple_smc_temp_softc *sc)
290 1.1 riastrad {
291 1.1 riastrad uint32_t sensor;
292 1.1 riastrad
293 1.1 riastrad for (sensor = 0; sensor < sc->sc_nsensors; sensor++) {
294 1.1 riastrad KASSERT(sc->sc_sensors[sensor].sensor_key != NULL);
295 1.1 riastrad apple_smc_release_key(sc->sc_smc,
296 1.1 riastrad sc->sc_sensors[sensor].sensor_key);
297 1.1 riastrad }
298 1.1 riastrad }
299 1.1 riastrad
300 1.1 riastrad static int
301 1.1 riastrad apple_smc_scan_temp_sensors(struct apple_smc_tag *smc, void *arg,
302 1.1 riastrad int (*init)(struct apple_smc_tag *, void *, uint32_t),
303 1.1 riastrad void (*scanner)(struct apple_smc_tag *, void *, struct apple_smc_key *))
304 1.1 riastrad {
305 1.1 riastrad uint32_t tstart, ustart, i;
306 1.1 riastrad struct apple_smc_key *key;
307 1.1 riastrad int error;
308 1.1 riastrad
309 1.1 riastrad error = apple_smc_bound_temp_sensors(smc, &tstart, &ustart);
310 1.1 riastrad if (error)
311 1.1 riastrad return error;
312 1.1 riastrad KASSERT(tstart <= ustart);
313 1.1 riastrad
314 1.1 riastrad if (init != NULL) {
315 1.1 riastrad error = (*init)(smc, arg, (ustart - tstart));
316 1.1 riastrad if (error)
317 1.1 riastrad return error;
318 1.1 riastrad }
319 1.1 riastrad
320 1.1 riastrad for (i = tstart; i < ustart; i++) {
321 1.1 riastrad error = apple_smc_nth_key(smc, i, NULL, &key);
322 1.1 riastrad if (error)
323 1.1 riastrad continue;
324 1.1 riastrad
325 1.1 riastrad if (!apple_smc_temp_sensor_p(key)) {
326 1.1 riastrad apple_smc_release_key(smc, key);
327 1.1 riastrad continue;
328 1.1 riastrad }
329 1.1 riastrad
330 1.1 riastrad (*scanner)(smc, arg, key);
331 1.1 riastrad }
332 1.1 riastrad
333 1.1 riastrad return 0;
334 1.1 riastrad }
335 1.1 riastrad
336 1.1 riastrad static bool
337 1.1 riastrad apple_smc_temp_sensor_p(const struct apple_smc_key *key)
338 1.1 riastrad {
339 1.1 riastrad
340 1.1 riastrad return (0 == memcmp(apple_smc_key_desc(key)->asd_type,
341 1.1 riastrad APPLE_SMC_TYPE_SP78, 4));
342 1.1 riastrad }
343 1.1 riastrad
344 1.1 riastrad static int
345 1.1 riastrad apple_smc_bound_temp_sensors(struct apple_smc_tag *smc, uint32_t *tstart,
346 1.1 riastrad uint32_t *ustart)
347 1.1 riastrad {
348 1.1 riastrad int error;
349 1.1 riastrad
350 1.1 riastrad error = apple_smc_key_search(smc, "T", tstart);
351 1.1 riastrad if (error)
352 1.1 riastrad return error;
353 1.1 riastrad
354 1.1 riastrad error = apple_smc_key_search(smc, "U", ustart);
355 1.1 riastrad if (error)
356 1.1 riastrad return error;
357 1.1 riastrad
358 1.1 riastrad if (!(*tstart <= *ustart))
359 1.1 riastrad return EIO;
360 1.1 riastrad
361 1.1 riastrad return 0;
362 1.1 riastrad }
363 1.2 riastrad
364 1.2 riastrad MODULE(MODULE_CLASS_DRIVER, apple_smc_temp, "apple_smc");
365 1.2 riastrad
366 1.2 riastrad #ifdef _MODULE
367 1.2 riastrad #include "ioconf.c"
368 1.2 riastrad #endif
369 1.2 riastrad
370 1.2 riastrad static int
371 1.2 riastrad apple_smc_temp_modcmd(modcmd_t cmd, void *arg __unused)
372 1.2 riastrad {
373 1.2 riastrad int error;
374 1.2 riastrad
375 1.2 riastrad switch (cmd) {
376 1.2 riastrad case MODULE_CMD_INIT:
377 1.2 riastrad #ifdef _MODULE
378 1.2 riastrad error = config_init_component(cfdriver_ioconf_apple_smc_temp,
379 1.2 riastrad cfattach_ioconf_apple_smc_temp,
380 1.2 riastrad cfdata_ioconf_apple_smc_temp);
381 1.3 riastrad if (error)
382 1.2 riastrad return error;
383 1.2 riastrad #endif
384 1.2 riastrad return 0;
385 1.2 riastrad
386 1.2 riastrad case MODULE_CMD_FINI:
387 1.2 riastrad #ifdef _MODULE
388 1.2 riastrad error = config_fini_component(cfdriver_ioconf_apple_smc_temp,
389 1.2 riastrad cfattach_ioconf_apple_smc_temp,
390 1.2 riastrad cfdata_ioconf_apple_smc_temp);
391 1.2 riastrad if (error)
392 1.2 riastrad return error;
393 1.2 riastrad #endif
394 1.2 riastrad return 0;
395 1.2 riastrad
396 1.2 riastrad default:
397 1.2 riastrad return ENOTTY;
398 1.2 riastrad }
399 1.2 riastrad }
400