xref: /freebsd/sys/dev/rtwn/if_rtwn_calib.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
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 
53 static void
54 rtwn_temp_calib(struct rtwn_softc *sc)
55 {
56 	uint8_t temp;
57 
58 	RTWN_ASSERT_LOCKED(sc);
59 
60 	if (!(sc->sc_flags & RTWN_TEMP_MEASURED)) {
61 		/* Start measuring temperature. */
62 		RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
63 		    "%s: start measuring temperature\n", __func__);
64 		rtwn_temp_measure(sc);
65 		sc->sc_flags |= RTWN_TEMP_MEASURED;
66 		return;
67 	}
68 	sc->sc_flags &= ~RTWN_TEMP_MEASURED;
69 
70 	/* Read measured temperature. */
71 	temp = rtwn_temp_read(sc);
72 	if (temp == 0) {	/* Read failed, skip. */
73 		RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
74 		    "%s: temperature read failed, skipping\n", __func__);
75 		return;
76 	}
77 
78 	RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
79 	    "temperature: previous %u, current %u\n",
80 	    sc->thcal_temp, temp);
81 
82 	/*
83 	 * Redo LC/IQ calibration if temperature changed significantly since
84 	 * last calibration.
85 	 */
86 	if (sc->thcal_temp == 0xff) {
87 		/* efuse value is absent; do LCK at initial status. */
88 		rtwn_lc_calib(sc);
89 
90 		sc->thcal_temp = temp;
91 	} else if (abs(temp - sc->thcal_temp) > sc->temp_delta) {
92 		RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
93 		    "%s: LC/IQ calib triggered by temp: %u -> %u\n",
94 		    __func__, sc->thcal_temp, temp);
95 
96 		rtwn_lc_calib(sc);
97 		rtwn_iq_calib(sc);
98 
99 		/* Record temperature of last calibration. */
100 		sc->thcal_temp = temp;
101 	}
102 }
103 
104 static void
105 rtwn_calib_cb(struct rtwn_softc *sc, union sec_param *data)
106 {
107 	/* Do temperature compensation. */
108 	rtwn_temp_calib(sc);
109 
110 #ifndef RTWN_WITHOUT_UCODE
111 	if (sc->sc_ratectl == RTWN_RATECTL_FW) {
112 		/* Refresh per-node RSSI. */
113 		rtwn_set_rssi(sc);
114 	}
115 #endif
116 
117 	if (sc->vaps_running > sc->monvaps_running)
118 		callout_reset(&sc->sc_calib_to, 2*hz, rtwn_calib_to, sc);
119 }
120 
121 void
122 rtwn_calib_to(void *arg)
123 {
124 	struct rtwn_softc *sc = arg;
125 
126 	/* Do it in a process context. */
127 	rtwn_cmd_sleepable(sc, NULL, 0, rtwn_calib_cb);
128 }
129