xref: /freebsd/sys/dev/rtwn/if_rtwn_calib.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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 __FBSDID("$FreeBSD$");
23 
24 #include "opt_wlan.h"
25 
26 #include <sys/param.h>
27 #include <sys/lock.h>
28 #include <sys/mutex.h>
29 #include <sys/mbuf.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/queue.h>
35 #include <sys/taskqueue.h>
36 #include <sys/bus.h>
37 #include <sys/endian.h>
38 
39 #include <net/if.h>
40 #include <net/ethernet.h>
41 #include <net/if_media.h>
42 
43 #include <net80211/ieee80211_var.h>
44 #include <net80211/ieee80211_radiotap.h>
45 
46 #include <dev/rtwn/if_rtwnvar.h>
47 
48 #include <dev/rtwn/if_rtwn_calib.h>
49 #include <dev/rtwn/if_rtwn_debug.h>
50 #include <dev/rtwn/if_rtwn_task.h>
51 
52 static void
53 rtwn_temp_calib(struct rtwn_softc *sc)
54 {
55 	uint8_t temp;
56 
57 	RTWN_ASSERT_LOCKED(sc);
58 
59 	if (!(sc->sc_flags & RTWN_TEMP_MEASURED)) {
60 		/* Start measuring temperature. */
61 		RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
62 		    "%s: start measuring temperature\n", __func__);
63 		rtwn_temp_measure(sc);
64 		sc->sc_flags |= RTWN_TEMP_MEASURED;
65 		return;
66 	}
67 	sc->sc_flags &= ~RTWN_TEMP_MEASURED;
68 
69 	/* Read measured temperature. */
70 	temp = rtwn_temp_read(sc);
71 	if (temp == 0) {	/* Read failed, skip. */
72 		RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
73 		    "%s: temperature read failed, skipping\n", __func__);
74 		return;
75 	}
76 
77 	RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
78 	    "temperature: previous %u, current %u\n",
79 	    sc->thcal_temp, temp);
80 
81 	/*
82 	 * Redo LC/IQ calibration if temperature changed significantly since
83 	 * last calibration.
84 	 */
85 	if (sc->thcal_temp == 0xff) {
86 		/* efuse value is absent; do LCK at initial status. */
87 		rtwn_lc_calib(sc);
88 
89 		sc->thcal_temp = temp;
90 	} else if (abs(temp - sc->thcal_temp) > sc->temp_delta) {
91 		RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
92 		    "%s: LC/IQ calib triggered by temp: %u -> %u\n",
93 		    __func__, sc->thcal_temp, temp);
94 
95 		rtwn_lc_calib(sc);
96 		rtwn_iq_calib(sc);
97 
98 		/* Record temperature of last calibration. */
99 		sc->thcal_temp = temp;
100 	}
101 }
102 
103 static void
104 rtwn_calib_cb(struct rtwn_softc *sc, union sec_param *data)
105 {
106 	/* Do temperature compensation. */
107 	rtwn_temp_calib(sc);
108 
109 #ifndef RTWN_WITHOUT_UCODE
110 	if (sc->sc_ratectl == RTWN_RATECTL_FW) {
111 		/* Refresh per-node RSSI. */
112 		rtwn_set_rssi(sc);
113 	}
114 #endif
115 
116 	if (sc->vaps_running > sc->monvaps_running)
117 		callout_reset(&sc->sc_calib_to, 2*hz, rtwn_calib_to, sc);
118 }
119 
120 void
121 rtwn_calib_to(void *arg)
122 {
123 	struct rtwn_softc *sc = arg;
124 
125 	/* Do it in a process context. */
126 	rtwn_cmd_sleepable(sc, NULL, 0, rtwn_calib_cb);
127 }
128