xref: /freebsd/sys/dev/rtwn/if_rtwn_calib.c (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1 /*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6  * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/cdefs.h>
22 #include "opt_wlan.h"
23 
24 #include <sys/param.h>
25 #include <sys/lock.h>
26 #include <sys/mutex.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/taskqueue.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 
37 #include <net/if.h>
38 #include <net/ethernet.h>
39 #include <net/if_media.h>
40 
41 #include <net80211/ieee80211_var.h>
42 #include <net80211/ieee80211_radiotap.h>
43 
44 #include <dev/rtwn/if_rtwnvar.h>
45 
46 #include <dev/rtwn/if_rtwn_calib.h>
47 #include <dev/rtwn/if_rtwn_debug.h>
48 #include <dev/rtwn/if_rtwn_task.h>
49 
50 static void
51 rtwn_temp_calib(struct rtwn_softc *sc)
52 {
53 	uint8_t temp;
54 
55 	RTWN_ASSERT_LOCKED(sc);
56 
57 	if (!(sc->sc_flags & RTWN_TEMP_MEASURED)) {
58 		/* Start measuring temperature. */
59 		RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
60 		    "%s: start measuring temperature\n", __func__);
61 		rtwn_temp_measure(sc);
62 		sc->sc_flags |= RTWN_TEMP_MEASURED;
63 		return;
64 	}
65 	sc->sc_flags &= ~RTWN_TEMP_MEASURED;
66 
67 	/* Read measured temperature. */
68 	temp = rtwn_temp_read(sc);
69 	if (temp == 0) {	/* Read failed, skip. */
70 		RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
71 		    "%s: temperature read failed, skipping\n", __func__);
72 		return;
73 	}
74 
75 	RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
76 	    "temperature: previous %u, current %u\n",
77 	    sc->thcal_temp, temp);
78 
79 	/*
80 	 * Redo LC/IQ calibration if temperature changed significantly since
81 	 * last calibration.
82 	 */
83 	if (sc->thcal_temp == 0xff) {
84 		/* efuse value is absent; do LCK at initial status. */
85 		rtwn_lc_calib(sc);
86 
87 		sc->thcal_temp = temp;
88 	} else if (abs(temp - sc->thcal_temp) > sc->temp_delta) {
89 		RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
90 		    "%s: LC/IQ calib triggered by temp: %u -> %u\n",
91 		    __func__, sc->thcal_temp, temp);
92 
93 		rtwn_lc_calib(sc);
94 		rtwn_iq_calib(sc);
95 
96 		/* Record temperature of last calibration. */
97 		sc->thcal_temp = temp;
98 	}
99 }
100 
101 static void
102 rtwn_calib_cb(struct rtwn_softc *sc, union sec_param *data)
103 {
104 	/* Do temperature compensation. */
105 	rtwn_temp_calib(sc);
106 
107 #ifndef RTWN_WITHOUT_UCODE
108 	if (sc->sc_ratectl == RTWN_RATECTL_FW) {
109 		/* Refresh per-node RSSI. */
110 		rtwn_set_rssi(sc);
111 	}
112 #endif
113 
114 	if (sc->vaps_running > sc->monvaps_running)
115 		callout_reset(&sc->sc_calib_to, 2*hz, rtwn_calib_to, sc);
116 }
117 
118 void
119 rtwn_calib_to(void *arg)
120 {
121 	struct rtwn_softc *sc = arg;
122 
123 	/* Do it in a process context. */
124 	rtwn_cmd_sleepable(sc, NULL, 0, rtwn_calib_cb);
125 }
126