apple_smc_temp.c revision 1.2 1 1.1 riastrad /* $NetBSD: apple_smc_temp.c,v 1.2 2014/04/01 17:48:39 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.2 2014/04/01 17:48:39 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.2 riastrad #define APPLE_SMC_DEVICE "applesmctemp"
51 1.2 riastrad
52 1.1 riastrad struct apple_smc_temp_softc {
53 1.1 riastrad device_t sc_dev;
54 1.1 riastrad struct apple_smc_tag *sc_smc;
55 1.1 riastrad struct sysmon_envsys *sc_sme;
56 1.1 riastrad struct {
57 1.1 riastrad struct apple_smc_key *sensor_key;
58 1.1 riastrad struct envsys_data sensor_data;
59 1.1 riastrad } *sc_sensors;
60 1.1 riastrad size_t sc_nsensors;
61 1.1 riastrad };
62 1.1 riastrad
63 1.1 riastrad static int apple_smc_temp_match(device_t, cfdata_t, void *);
64 1.1 riastrad static void apple_smc_temp_attach(device_t, device_t, void *);
65 1.1 riastrad static int apple_smc_temp_detach(device_t, int);
66 1.1 riastrad static void apple_smc_temp_refresh(struct sysmon_envsys *,
67 1.1 riastrad struct envsys_data *);
68 1.1 riastrad static int apple_smc_temp_count_sensors(struct apple_smc_tag *,
69 1.1 riastrad uint32_t *);
70 1.1 riastrad static void apple_smc_temp_count_sensors_scanner(struct apple_smc_tag *,
71 1.1 riastrad void *, struct apple_smc_key *);
72 1.1 riastrad static int apple_smc_temp_find_sensors(struct apple_smc_temp_softc *);
73 1.1 riastrad static int apple_smc_temp_find_sensors_init(struct apple_smc_tag *,
74 1.1 riastrad void *, uint32_t);
75 1.1 riastrad static void apple_smc_temp_find_sensors_scanner(struct apple_smc_tag *,
76 1.1 riastrad void *, struct apple_smc_key *);
77 1.1 riastrad static void apple_smc_temp_release_keys(struct apple_smc_temp_softc *);
78 1.1 riastrad static int apple_smc_scan_temp_sensors(struct apple_smc_tag *, void *,
79 1.1 riastrad int (*)(struct apple_smc_tag *, void *, uint32_t),
80 1.1 riastrad void (*)(struct apple_smc_tag *, void *,
81 1.1 riastrad struct apple_smc_key *));
82 1.1 riastrad static int apple_smc_bound_temp_sensors(struct apple_smc_tag *,
83 1.1 riastrad uint32_t *, uint32_t *);
84 1.1 riastrad static bool apple_smc_temp_sensor_p(const struct apple_smc_key *);
85 1.1 riastrad
86 1.1 riastrad CFATTACH_DECL_NEW(apple_smc_temp, sizeof(struct apple_smc_temp_softc),
87 1.1 riastrad apple_smc_temp_match, apple_smc_temp_attach, apple_smc_temp_detach, NULL);
88 1.1 riastrad
89 1.1 riastrad static int
90 1.1 riastrad apple_smc_temp_match(device_t parent, cfdata_t match, void *aux)
91 1.1 riastrad {
92 1.1 riastrad const struct apple_smc_attach_args *asa = aux;
93 1.1 riastrad uint32_t nsensors;
94 1.1 riastrad int error;
95 1.1 riastrad
96 1.2 riastrad if (strcmp(asa->asa_device, APPLE_SMC_DEVICE) != 0)
97 1.2 riastrad return 0;
98 1.2 riastrad
99 1.1 riastrad error = apple_smc_temp_count_sensors(asa->asa_smc, &nsensors);
100 1.1 riastrad if (error)
101 1.1 riastrad return 0;
102 1.1 riastrad
103 1.1 riastrad if (nsensors == 0)
104 1.1 riastrad return 0;
105 1.1 riastrad
106 1.1 riastrad return 1;
107 1.1 riastrad }
108 1.1 riastrad
109 1.1 riastrad static void
110 1.1 riastrad apple_smc_temp_attach(device_t parent, device_t self, void *aux)
111 1.1 riastrad {
112 1.1 riastrad struct apple_smc_temp_softc *sc = device_private(self);
113 1.1 riastrad const struct apple_smc_attach_args *asa = aux;
114 1.1 riastrad
115 1.1 riastrad sc->sc_dev = self;
116 1.1 riastrad sc->sc_smc = asa->asa_smc;
117 1.1 riastrad sc->sc_sme = sysmon_envsys_create();
118 1.1 riastrad sc->sc_sme->sme_name = device_xname(self);
119 1.1 riastrad sc->sc_sme->sme_cookie = sc;
120 1.1 riastrad sc->sc_sme->sme_refresh = apple_smc_temp_refresh;
121 1.1 riastrad
122 1.1 riastrad (void)apple_smc_temp_find_sensors(sc);
123 1.1 riastrad }
124 1.1 riastrad
125 1.1 riastrad static int
126 1.1 riastrad apple_smc_temp_detach(device_t self, int flags)
127 1.1 riastrad {
128 1.1 riastrad struct apple_smc_temp_softc *sc = device_private(self);
129 1.1 riastrad
130 1.1 riastrad KASSERT(sc->sc_sme != NULL);
131 1.1 riastrad sysmon_envsys_unregister(sc->sc_sme);
132 1.1 riastrad sc->sc_sme = NULL;
133 1.1 riastrad
134 1.1 riastrad if (sc->sc_sensors != NULL) {
135 1.1 riastrad KASSERT(sc->sc_nsensors > 0);
136 1.1 riastrad apple_smc_temp_release_keys(sc);
137 1.1 riastrad kmem_free(sc->sc_sensors,
138 1.1 riastrad (sizeof(sc->sc_sensors[0]) * sc->sc_nsensors));
139 1.1 riastrad sc->sc_sensors = NULL;
140 1.1 riastrad sc->sc_nsensors = 0;
141 1.1 riastrad }
142 1.1 riastrad
143 1.1 riastrad return 0;
144 1.1 riastrad }
145 1.1 riastrad
146 1.1 riastrad static void
147 1.1 riastrad apple_smc_temp_refresh(struct sysmon_envsys *sme, struct envsys_data *edata)
148 1.1 riastrad {
149 1.1 riastrad struct apple_smc_temp_softc *sc = sme->sme_cookie;
150 1.1 riastrad const struct apple_smc_key *key;
151 1.1 riastrad uint16_t utemp16;
152 1.1 riastrad int32_t temp;
153 1.1 riastrad int error;
154 1.1 riastrad
155 1.1 riastrad if (edata->sensor >= sc->sc_nsensors) {
156 1.1 riastrad aprint_error_dev(sc->sc_dev, "unknown sensor %"PRIu32"\n",
157 1.1 riastrad edata->sensor);
158 1.1 riastrad return;
159 1.1 riastrad }
160 1.1 riastrad
161 1.1 riastrad key = sc->sc_sensors[edata->sensor].sensor_key;
162 1.1 riastrad error = apple_smc_read_key_2(sc->sc_smc, key, &utemp16);
163 1.1 riastrad if (error) {
164 1.1 riastrad aprint_error_dev(sc->sc_dev,
165 1.1 riastrad "failed to read temperature sensor %"PRIu32" (%s): %d\n",
166 1.1 riastrad edata->sensor, apple_smc_key_name(key), error);
167 1.1 riastrad edata->state = ENVSYS_SINVALID;
168 1.1 riastrad return;
169 1.1 riastrad }
170 1.1 riastrad
171 1.1 riastrad /* Sign-extend, in case we ever get below freezing... */
172 1.1 riastrad temp = (int16_t)utemp16;
173 1.1 riastrad
174 1.1 riastrad /* Convert to `millicentigrade'. */
175 1.1 riastrad temp *= 250;
176 1.1 riastrad temp >>= 6;
177 1.1 riastrad
178 1.1 riastrad /* Convert to millikelvins. */
179 1.1 riastrad temp += 273150;
180 1.1 riastrad
181 1.1 riastrad /* Finally, convert to microkelvins as sysmon_envsys wants. */
182 1.1 riastrad temp *= 1000;
183 1.1 riastrad
184 1.1 riastrad edata->value_cur = temp;
185 1.1 riastrad edata->state = ENVSYS_SVALID;
186 1.1 riastrad }
187 1.1 riastrad
188 1.1 riastrad static int
189 1.1 riastrad apple_smc_temp_count_sensors(struct apple_smc_tag *smc, uint32_t *nsensors)
190 1.1 riastrad {
191 1.1 riastrad
192 1.1 riastrad *nsensors = 0;
193 1.1 riastrad
194 1.1 riastrad return apple_smc_scan_temp_sensors(smc, nsensors,
195 1.1 riastrad NULL,
196 1.1 riastrad &apple_smc_temp_count_sensors_scanner);
197 1.1 riastrad }
198 1.1 riastrad
199 1.1 riastrad static void
200 1.1 riastrad apple_smc_temp_count_sensors_scanner(struct apple_smc_tag *smc, void *arg,
201 1.1 riastrad struct apple_smc_key *key)
202 1.1 riastrad {
203 1.1 riastrad uint32_t *nsensors = arg;
204 1.1 riastrad
205 1.1 riastrad (*nsensors)++;
206 1.1 riastrad apple_smc_release_key(smc, key);
207 1.1 riastrad }
208 1.1 riastrad
209 1.1 riastrad struct fss { /* Find Sensors State */
210 1.1 riastrad struct apple_smc_temp_softc *fss_sc;
211 1.1 riastrad size_t fss_sensor;
212 1.1 riastrad };
213 1.1 riastrad
214 1.1 riastrad static int
215 1.1 riastrad apple_smc_temp_find_sensors(struct apple_smc_temp_softc *sc)
216 1.1 riastrad {
217 1.1 riastrad struct fss fss;
218 1.1 riastrad int error;
219 1.1 riastrad
220 1.1 riastrad fss.fss_sc = sc;
221 1.1 riastrad fss.fss_sensor = 0;
222 1.1 riastrad
223 1.1 riastrad error = apple_smc_scan_temp_sensors(sc->sc_smc, &fss,
224 1.1 riastrad &apple_smc_temp_find_sensors_init,
225 1.1 riastrad &apple_smc_temp_find_sensors_scanner);
226 1.1 riastrad if (error)
227 1.1 riastrad return error;
228 1.1 riastrad
229 1.1 riastrad /* Shrink the array if we overshot. */
230 1.1 riastrad if (fss.fss_sensor < sc->sc_nsensors) {
231 1.1 riastrad void *sensors = kmem_alloc((fss.fss_sensor *
232 1.1 riastrad sizeof(sc->sc_sensors[0])), KM_SLEEP);
233 1.1 riastrad (void)memcpy(sensors, sc->sc_sensors,
234 1.1 riastrad (fss.fss_sensor * sizeof(sc->sc_sensors[0])));
235 1.1 riastrad kmem_free(sc->sc_sensors, sc->sc_nsensors);
236 1.1 riastrad sc->sc_nsensors = fss.fss_sensor;
237 1.1 riastrad sc->sc_sensors = sensors;
238 1.1 riastrad }
239 1.1 riastrad
240 1.1 riastrad return 0;
241 1.1 riastrad }
242 1.1 riastrad
243 1.1 riastrad static int
244 1.1 riastrad apple_smc_temp_find_sensors_init(struct apple_smc_tag *smc, void *arg,
245 1.1 riastrad uint32_t nsensors)
246 1.1 riastrad {
247 1.1 riastrad struct fss *fss = arg;
248 1.1 riastrad
249 1.1 riastrad fss->fss_sc->sc_nsensors = nsensors;
250 1.1 riastrad if (nsensors == 0)
251 1.1 riastrad fss->fss_sc->sc_sensors = NULL;
252 1.1 riastrad else
253 1.1 riastrad fss->fss_sc->sc_sensors = kmem_alloc((nsensors *
254 1.1 riastrad sizeof(fss->fss_sc->sc_sensors[0])), KM_SLEEP);
255 1.1 riastrad
256 1.1 riastrad return 0;
257 1.1 riastrad }
258 1.1 riastrad
259 1.1 riastrad static void
260 1.1 riastrad apple_smc_temp_find_sensors_scanner(struct apple_smc_tag *smc, void *arg,
261 1.1 riastrad struct apple_smc_key *key)
262 1.1 riastrad {
263 1.1 riastrad struct fss *fss = arg;
264 1.1 riastrad const uint32_t sensor = fss->fss_sensor;
265 1.1 riastrad struct envsys_data *edata =
266 1.1 riastrad &fss->fss_sc->sc_sensors[sensor].sensor_data;
267 1.1 riastrad int error;
268 1.1 riastrad
269 1.1 riastrad edata->units = ENVSYS_STEMP;
270 1.1 riastrad edata->state = ENVSYS_SINVALID;
271 1.1 riastrad edata->flags = ENVSYS_FHAS_ENTROPY;
272 1.1 riastrad
273 1.1 riastrad /*
274 1.1 riastrad * XXX Use a more meaningful name based on a table of known
275 1.1 riastrad * temperature sensors.
276 1.1 riastrad */
277 1.1 riastrad CTASSERT(sizeof(edata->desc) >= 4);
278 1.1 riastrad (void)strlcpy(edata->desc, apple_smc_key_name(key), 4);
279 1.1 riastrad
280 1.1 riastrad error = sysmon_envsys_sensor_attach(fss->fss_sc->sc_sme, edata);
281 1.1 riastrad if (error) {
282 1.1 riastrad aprint_error_dev(fss->fss_sc->sc_dev,
283 1.1 riastrad "failed to attach temperature sensor %s: %d\n",
284 1.1 riastrad apple_smc_key_name(key), error);
285 1.1 riastrad return;
286 1.1 riastrad }
287 1.1 riastrad
288 1.1 riastrad fss->fss_sc->sc_sensors[sensor].sensor_key = key;
289 1.1 riastrad
290 1.1 riastrad fss->fss_sensor++;
291 1.1 riastrad }
292 1.1 riastrad
293 1.1 riastrad static void
294 1.1 riastrad apple_smc_temp_release_keys(struct apple_smc_temp_softc *sc)
295 1.1 riastrad {
296 1.1 riastrad uint32_t sensor;
297 1.1 riastrad
298 1.1 riastrad for (sensor = 0; sensor < sc->sc_nsensors; sensor++) {
299 1.1 riastrad KASSERT(sc->sc_sensors[sensor].sensor_key != NULL);
300 1.1 riastrad apple_smc_release_key(sc->sc_smc,
301 1.1 riastrad sc->sc_sensors[sensor].sensor_key);
302 1.1 riastrad }
303 1.1 riastrad }
304 1.1 riastrad
305 1.1 riastrad static int
306 1.1 riastrad apple_smc_scan_temp_sensors(struct apple_smc_tag *smc, void *arg,
307 1.1 riastrad int (*init)(struct apple_smc_tag *, void *, uint32_t),
308 1.1 riastrad void (*scanner)(struct apple_smc_tag *, void *, struct apple_smc_key *))
309 1.1 riastrad {
310 1.1 riastrad uint32_t tstart, ustart, i;
311 1.1 riastrad struct apple_smc_key *key;
312 1.1 riastrad int error;
313 1.1 riastrad
314 1.1 riastrad error = apple_smc_bound_temp_sensors(smc, &tstart, &ustart);
315 1.1 riastrad if (error)
316 1.1 riastrad return error;
317 1.1 riastrad KASSERT(tstart <= ustart);
318 1.1 riastrad
319 1.1 riastrad if (init != NULL) {
320 1.1 riastrad error = (*init)(smc, arg, (ustart - tstart));
321 1.1 riastrad if (error)
322 1.1 riastrad return error;
323 1.1 riastrad }
324 1.1 riastrad
325 1.1 riastrad for (i = tstart; i < ustart; i++) {
326 1.1 riastrad error = apple_smc_nth_key(smc, i, NULL, &key);
327 1.1 riastrad if (error)
328 1.1 riastrad continue;
329 1.1 riastrad
330 1.1 riastrad if (!apple_smc_temp_sensor_p(key)) {
331 1.1 riastrad apple_smc_release_key(smc, key);
332 1.1 riastrad continue;
333 1.1 riastrad }
334 1.1 riastrad
335 1.1 riastrad (*scanner)(smc, arg, key);
336 1.1 riastrad }
337 1.1 riastrad
338 1.1 riastrad return 0;
339 1.1 riastrad }
340 1.1 riastrad
341 1.1 riastrad static bool
342 1.1 riastrad apple_smc_temp_sensor_p(const struct apple_smc_key *key)
343 1.1 riastrad {
344 1.1 riastrad
345 1.1 riastrad return (0 == memcmp(apple_smc_key_desc(key)->asd_type,
346 1.1 riastrad APPLE_SMC_TYPE_SP78, 4));
347 1.1 riastrad }
348 1.1 riastrad
349 1.1 riastrad static int
350 1.1 riastrad apple_smc_bound_temp_sensors(struct apple_smc_tag *smc, uint32_t *tstart,
351 1.1 riastrad uint32_t *ustart)
352 1.1 riastrad {
353 1.1 riastrad int error;
354 1.1 riastrad
355 1.1 riastrad error = apple_smc_key_search(smc, "T", tstart);
356 1.1 riastrad if (error)
357 1.1 riastrad return error;
358 1.1 riastrad
359 1.1 riastrad error = apple_smc_key_search(smc, "U", ustart);
360 1.1 riastrad if (error)
361 1.1 riastrad return error;
362 1.1 riastrad
363 1.1 riastrad if (!(*tstart <= *ustart))
364 1.1 riastrad return EIO;
365 1.1 riastrad
366 1.1 riastrad return 0;
367 1.1 riastrad }
368 1.2 riastrad
369 1.2 riastrad MODULE(MODULE_CLASS_DRIVER, apple_smc_temp, "apple_smc");
370 1.2 riastrad
371 1.2 riastrad #ifdef _MODULE
372 1.2 riastrad #include "ioconf.c"
373 1.2 riastrad #endif
374 1.2 riastrad
375 1.2 riastrad static int
376 1.2 riastrad apple_smc_temp_modcmd(modcmd_t cmd, void *arg __unused)
377 1.2 riastrad {
378 1.2 riastrad int error;
379 1.2 riastrad
380 1.2 riastrad switch (cmd) {
381 1.2 riastrad case MODULE_CMD_INIT:
382 1.2 riastrad error = apple_smc_register_device(APPLE_SMC_DEVICE);
383 1.2 riastrad if (error)
384 1.2 riastrad return error;
385 1.2 riastrad #ifdef _MODULE
386 1.2 riastrad error = config_init_component(cfdriver_ioconf_apple_smc_temp,
387 1.2 riastrad cfattach_ioconf_apple_smc_temp,
388 1.2 riastrad cfdata_ioconf_apple_smc_temp);
389 1.2 riastrad if (error) {
390 1.2 riastrad apple_smc_unregister_device(APPLE_SMC_DEVICE);
391 1.2 riastrad return error;
392 1.2 riastrad }
393 1.2 riastrad #endif
394 1.2 riastrad return 0;
395 1.2 riastrad
396 1.2 riastrad case MODULE_CMD_FINI:
397 1.2 riastrad #ifdef _MODULE
398 1.2 riastrad error = config_fini_component(cfdriver_ioconf_apple_smc_temp,
399 1.2 riastrad cfattach_ioconf_apple_smc_temp,
400 1.2 riastrad cfdata_ioconf_apple_smc_temp);
401 1.2 riastrad if (error)
402 1.2 riastrad return error;
403 1.2 riastrad #endif
404 1.2 riastrad apple_smc_unregister_device(APPLE_SMC_DEVICE);
405 1.2 riastrad return 0;
406 1.2 riastrad
407 1.2 riastrad default:
408 1.2 riastrad return ENOTTY;
409 1.2 riastrad }
410 1.2 riastrad }
411