staging: brcm80211: replaced wlc_ by brcms_c_
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / brcm80211 / brcmsmac / antsel.c
blob7e12f605be68ae249642ba1e873b9ce6b1dc9b3a
1 /*
2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/slab.h>
18 #include <net/mac80211.h>
20 #include "types.h"
21 #include "bmac.h"
22 #include "main.h"
23 #include "phy_shim.h"
24 #include "antsel.h"
26 #define ANT_SELCFG_AUTO 0x80 /* bit indicates antenna sel AUTO */
27 #define ANT_SELCFG_MASK 0x33 /* antenna configuration mask */
28 #define ANT_SELCFG_TX_UNICAST 0 /* unicast tx antenna configuration */
29 #define ANT_SELCFG_RX_UNICAST 1 /* unicast rx antenna configuration */
30 #define ANT_SELCFG_TX_DEF 2 /* default tx antenna configuration */
31 #define ANT_SELCFG_RX_DEF 3 /* default rx antenna configuration */
33 /* useful macros */
34 #define WLC_ANTSEL_11N_0(ant) ((((ant) & ANT_SELCFG_MASK) >> 4) & 0xf)
35 #define WLC_ANTSEL_11N_1(ant) (((ant) & ANT_SELCFG_MASK) & 0xf)
36 #define WLC_ANTIDX_11N(ant) (((WLC_ANTSEL_11N_0(ant)) << 2) + (WLC_ANTSEL_11N_1(ant)))
37 #define WLC_ANT_ISAUTO_11N(ant) (((ant) & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO)
38 #define WLC_ANTSEL_11N(ant) ((ant) & ANT_SELCFG_MASK)
40 /* antenna switch */
41 /* defines for no boardlevel antenna diversity */
42 #define ANT_SELCFG_DEF_2x2 0x01 /* default antenna configuration */
44 /* 2x3 antdiv defines and tables for GPIO communication */
45 #define ANT_SELCFG_NUM_2x3 3
46 #define ANT_SELCFG_DEF_2x3 0x01 /* default antenna configuration */
48 /* 2x4 antdiv rev4 defines and tables for GPIO communication */
49 #define ANT_SELCFG_NUM_2x4 4
50 #define ANT_SELCFG_DEF_2x4 0x02 /* default antenna configuration */
52 /* static functions */
53 static int wlc_antsel_cfgupd(struct antsel_info *asi, wlc_antselcfg_t *antsel);
54 static u8 wlc_antsel_id2antcfg(struct antsel_info *asi, u8 id);
55 static u16 wlc_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg);
56 static void wlc_antsel_init_cfg(struct antsel_info *asi,
57 wlc_antselcfg_t *antsel,
58 bool auto_sel);
60 const u16 mimo_2x4_div_antselpat_tbl[] = {
61 0, 0, 0x9, 0xa, /* ant0: 0 ant1: 2,3 */
62 0, 0, 0x5, 0x6, /* ant0: 1 ant1: 2,3 */
63 0, 0, 0, 0, /* n.a. */
64 0, 0, 0, 0 /* n.a. */
67 const u8 mimo_2x4_div_antselid_tbl[16] = {
68 0, 0, 0, 0, 0, 2, 3, 0,
69 0, 0, 1, 0, 0, 0, 0, 0 /* pat to antselid */
72 const u16 mimo_2x3_div_antselpat_tbl[] = {
73 16, 0, 1, 16, /* ant0: 0 ant1: 1,2 */
74 16, 16, 16, 16, /* n.a. */
75 16, 2, 16, 16, /* ant0: 2 ant1: 1 */
76 16, 16, 16, 16 /* n.a. */
79 const u8 mimo_2x3_div_antselid_tbl[16] = {
80 0, 1, 2, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0 /* pat to antselid */
84 struct antsel_info *wlc_antsel_attach(struct brcms_c_info *wlc)
86 struct antsel_info *asi;
88 asi = kzalloc(sizeof(struct antsel_info), GFP_ATOMIC);
89 if (!asi) {
90 wiphy_err(wlc->wiphy, "wl%d: wlc_antsel_attach: out of mem\n",
91 wlc->pub->unit);
92 return NULL;
95 asi->wlc = wlc;
96 asi->pub = wlc->pub;
97 asi->antsel_type = ANTSEL_NA;
98 asi->antsel_avail = false;
99 asi->antsel_antswitch = (u8) getintvar(asi->pub->vars, "antswitch");
101 if ((asi->pub->sromrev >= 4) && (asi->antsel_antswitch != 0)) {
102 switch (asi->antsel_antswitch) {
103 case ANTSWITCH_TYPE_1:
104 case ANTSWITCH_TYPE_2:
105 case ANTSWITCH_TYPE_3:
106 /* 4321/2 board with 2x3 switch logic */
107 asi->antsel_type = ANTSEL_2x3;
108 /* Antenna selection availability */
109 if (((u16) getintvar(asi->pub->vars, "aa2g") == 7) ||
110 ((u16) getintvar(asi->pub->vars, "aa5g") == 7)) {
111 asi->antsel_avail = true;
112 } else
113 if (((u16) getintvar(asi->pub->vars, "aa2g") ==
115 || ((u16) getintvar(asi->pub->vars, "aa5g")
116 == 3)) {
117 asi->antsel_avail = false;
118 } else {
119 asi->antsel_avail = false;
120 wiphy_err(wlc->wiphy, "wlc_antsel_attach: 2o3 "
121 "board cfg invalid\n");
123 break;
124 default:
125 break;
127 } else if ((asi->pub->sromrev == 4) &&
128 ((u16) getintvar(asi->pub->vars, "aa2g") == 7) &&
129 ((u16) getintvar(asi->pub->vars, "aa5g") == 0)) {
130 /* hack to match old 4321CB2 cards with 2of3 antenna switch */
131 asi->antsel_type = ANTSEL_2x3;
132 asi->antsel_avail = true;
133 } else if (asi->pub->boardflags2 & BFL2_2X4_DIV) {
134 asi->antsel_type = ANTSEL_2x4;
135 asi->antsel_avail = true;
138 /* Set the antenna selection type for the low driver */
139 brcms_b_antsel_type_set(wlc->hw, asi->antsel_type);
141 /* Init (auto/manual) antenna selection */
142 wlc_antsel_init_cfg(asi, &asi->antcfg_11n, true);
143 wlc_antsel_init_cfg(asi, &asi->antcfg_cur, true);
145 return asi;
148 void wlc_antsel_detach(struct antsel_info *asi)
150 kfree(asi);
153 void wlc_antsel_init(struct antsel_info *asi)
155 if ((asi->antsel_type == ANTSEL_2x3) ||
156 (asi->antsel_type == ANTSEL_2x4))
157 wlc_antsel_cfgupd(asi, &asi->antcfg_11n);
160 /* boardlevel antenna selection: init antenna selection structure */
161 static void
162 wlc_antsel_init_cfg(struct antsel_info *asi, wlc_antselcfg_t *antsel,
163 bool auto_sel)
165 if (asi->antsel_type == ANTSEL_2x3) {
166 u8 antcfg_def = ANT_SELCFG_DEF_2x3 |
167 ((asi->antsel_avail && auto_sel) ? ANT_SELCFG_AUTO : 0);
168 antsel->ant_config[ANT_SELCFG_TX_DEF] = antcfg_def;
169 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = antcfg_def;
170 antsel->ant_config[ANT_SELCFG_RX_DEF] = antcfg_def;
171 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = antcfg_def;
172 antsel->num_antcfg = ANT_SELCFG_NUM_2x3;
174 } else if (asi->antsel_type == ANTSEL_2x4) {
176 antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x4;
177 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x4;
178 antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x4;
179 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x4;
180 antsel->num_antcfg = ANT_SELCFG_NUM_2x4;
182 } else { /* no antenna selection available */
184 antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x2;
185 antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x2;
186 antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x2;
187 antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x2;
188 antsel->num_antcfg = 0;
192 void
193 wlc_antsel_antcfg_get(struct antsel_info *asi, bool usedef, bool sel,
194 u8 antselid, u8 fbantselid, u8 *antcfg,
195 u8 *fbantcfg)
197 u8 ant;
199 /* if use default, assign it and return */
200 if (usedef) {
201 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_DEF];
202 *fbantcfg = *antcfg;
203 return;
206 if (!sel) {
207 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
208 *fbantcfg = *antcfg;
210 } else {
211 ant = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
212 if ((ant & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO) {
213 *antcfg = wlc_antsel_id2antcfg(asi, antselid);
214 *fbantcfg = wlc_antsel_id2antcfg(asi, fbantselid);
215 } else {
216 *antcfg =
217 asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
218 *fbantcfg = *antcfg;
221 return;
224 /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
225 u8 wlc_antsel_antsel2id(struct antsel_info *asi, u16 antsel)
227 u8 antselid = 0;
229 if (asi->antsel_type == ANTSEL_2x4) {
230 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
231 antselid = mimo_2x4_div_antselid_tbl[(antsel & 0xf)];
232 return antselid;
234 } else if (asi->antsel_type == ANTSEL_2x3) {
235 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
236 antselid = mimo_2x3_div_antselid_tbl[(antsel & 0xf)];
237 return antselid;
240 return antselid;
243 /* boardlevel antenna selection: convert id to ant_cfg */
244 static u8 wlc_antsel_id2antcfg(struct antsel_info *asi, u8 id)
246 u8 antcfg = ANT_SELCFG_DEF_2x2;
248 if (asi->antsel_type == ANTSEL_2x4) {
249 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
250 antcfg = (((id & 0x2) << 3) | ((id & 0x1) + 2));
251 return antcfg;
253 } else if (asi->antsel_type == ANTSEL_2x3) {
254 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
255 antcfg = (((id & 0x02) << 4) | ((id & 0x1) + 1));
256 return antcfg;
259 return antcfg;
262 /* boardlevel antenna selection: convert ant_cfg to mimo_antsel (ucode interface) */
263 static u16 wlc_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg)
265 u8 idx = WLC_ANTIDX_11N(WLC_ANTSEL_11N(ant_cfg));
266 u16 mimo_antsel = 0;
268 if (asi->antsel_type == ANTSEL_2x4) {
269 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
270 mimo_antsel = (mimo_2x4_div_antselpat_tbl[idx] & 0xf);
271 return mimo_antsel;
273 } else if (asi->antsel_type == ANTSEL_2x3) {
274 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
275 mimo_antsel = (mimo_2x3_div_antselpat_tbl[idx] & 0xf);
276 return mimo_antsel;
279 return mimo_antsel;
282 /* boardlevel antenna selection: ucode interface control */
283 static int wlc_antsel_cfgupd(struct antsel_info *asi, wlc_antselcfg_t *antsel)
285 struct brcms_c_info *wlc = asi->wlc;
286 u8 ant_cfg;
287 u16 mimo_antsel;
289 /* 1) Update TX antconfig for all frames that are not unicast data
290 * (aka default TX)
292 ant_cfg = antsel->ant_config[ANT_SELCFG_TX_DEF];
293 mimo_antsel = wlc_antsel_antcfg2antsel(asi, ant_cfg);
294 brcms_c_write_shm(wlc, M_MIMO_ANTSEL_TXDFLT, mimo_antsel);
295 /* Update driver stats for currently selected default tx/rx antenna config */
296 asi->antcfg_cur.ant_config[ANT_SELCFG_TX_DEF] = ant_cfg;
298 /* 2) Update RX antconfig for all frames that are not unicast data
299 * (aka default RX)
301 ant_cfg = antsel->ant_config[ANT_SELCFG_RX_DEF];
302 mimo_antsel = wlc_antsel_antcfg2antsel(asi, ant_cfg);
303 brcms_c_write_shm(wlc, M_MIMO_ANTSEL_RXDFLT, mimo_antsel);
304 /* Update driver stats for currently selected default tx/rx antenna config */
305 asi->antcfg_cur.ant_config[ANT_SELCFG_RX_DEF] = ant_cfg;
307 return 0;