Merge git://git.kernel.org/pub/scm/linux/kernel/git/lwfinger/r8192E into staging...
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmsmac / antsel.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
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.
7  *
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.
15  */
16
17 #include <linux/slab.h>
18 #include <net/mac80211.h>
19
20 #include "types.h"
21 #include "main.h"
22 #include "phy_shim.h"
23 #include "antsel.h"
24
25 #define ANT_SELCFG_AUTO         0x80    /* bit indicates antenna sel AUTO */
26 #define ANT_SELCFG_MASK         0x33    /* antenna configuration mask */
27 #define ANT_SELCFG_TX_UNICAST   0       /* unicast tx antenna configuration */
28 #define ANT_SELCFG_RX_UNICAST   1       /* unicast rx antenna configuration */
29 #define ANT_SELCFG_TX_DEF       2       /* default tx antenna configuration */
30 #define ANT_SELCFG_RX_DEF       3       /* default rx antenna configuration */
31
32 /* useful macros */
33 #define BRCMS_ANTSEL_11N_0(ant) ((((ant) & ANT_SELCFG_MASK) >> 4) & 0xf)
34 #define BRCMS_ANTSEL_11N_1(ant) (((ant) & ANT_SELCFG_MASK) & 0xf)
35 #define BRCMS_ANTIDX_11N(ant)   (((BRCMS_ANTSEL_11N_0(ant)) << 2) +\
36                                 (BRCMS_ANTSEL_11N_1(ant)))
37 #define BRCMS_ANT_ISAUTO_11N(ant) (((ant) & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO)
38 #define BRCMS_ANTSEL_11N(ant)   ((ant) & ANT_SELCFG_MASK)
39
40 /* antenna switch */
41 /* defines for no boardlevel antenna diversity */
42 #define ANT_SELCFG_DEF_2x2      0x01    /* default antenna configuration */
43
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 */
47
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 */
51
52 /* static functions */
53 static int brcms_c_antsel_cfgupd(struct antsel_info *asi,
54                                  struct brcms_antselcfg *antsel);
55 static u8 brcms_c_antsel_id2antcfg(struct antsel_info *asi, u8 id);
56 static u16 brcms_c_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg);
57 static void brcms_c_antsel_init_cfg(struct antsel_info *asi,
58                                 struct brcms_antselcfg *antsel,
59                                 bool auto_sel);
60
61 const u16 mimo_2x4_div_antselpat_tbl[] = {
62         0, 0, 0x9, 0xa,         /* ant0: 0 ant1: 2,3 */
63         0, 0, 0x5, 0x6,         /* ant0: 1 ant1: 2,3 */
64         0, 0, 0, 0,             /* n.a.              */
65         0, 0, 0, 0              /* n.a.              */
66 };
67
68 const u8 mimo_2x4_div_antselid_tbl[16] = {
69         0, 0, 0, 0, 0, 2, 3, 0,
70         0, 0, 1, 0, 0, 0, 0, 0  /* pat to antselid */
71 };
72
73 const u16 mimo_2x3_div_antselpat_tbl[] = {
74         16, 0, 1, 16,           /* ant0: 0 ant1: 1,2 */
75         16, 16, 16, 16,         /* n.a.              */
76         16, 2, 16, 16,          /* ant0: 2 ant1: 1   */
77         16, 16, 16, 16          /* n.a.              */
78 };
79
80 const u8 mimo_2x3_div_antselid_tbl[16] = {
81         0, 1, 2, 0, 0, 0, 0, 0,
82         0, 0, 0, 0, 0, 0, 0, 0  /* pat to antselid */
83 };
84
85 struct antsel_info *brcms_c_antsel_attach(struct brcms_c_info *wlc)
86 {
87         struct antsel_info *asi;
88
89         asi = kzalloc(sizeof(struct antsel_info), GFP_ATOMIC);
90         if (!asi) {
91                 wiphy_err(wlc->wiphy, "wl%d: brcms_c_antsel_attach: out of "
92                           "mem\n", wlc->pub->unit);
93                 return NULL;
94         }
95
96         asi->wlc = wlc;
97         asi->pub = wlc->pub;
98         asi->antsel_type = ANTSEL_NA;
99         asi->antsel_avail = false;
100         asi->antsel_antswitch = (u8) getintvar(asi->pub->vars, "antswitch");
101
102         if ((asi->pub->sromrev >= 4) && (asi->antsel_antswitch != 0)) {
103                 switch (asi->antsel_antswitch) {
104                 case ANTSWITCH_TYPE_1:
105                 case ANTSWITCH_TYPE_2:
106                 case ANTSWITCH_TYPE_3:
107                         /* 4321/2 board with 2x3 switch logic */
108                         asi->antsel_type = ANTSEL_2x3;
109                         /* Antenna selection availability */
110                         if (((u16) getintvar(asi->pub->vars, "aa2g") == 7) ||
111                             ((u16) getintvar(asi->pub->vars, "aa5g") == 7)) {
112                                 asi->antsel_avail = true;
113                         } else if (
114                                 (u16) getintvar(asi->pub->vars, "aa2g") == 3 ||
115                                 (u16) getintvar(asi->pub->vars, "aa5g") == 3) {
116                                 asi->antsel_avail = false;
117                         } else {
118                                 asi->antsel_avail = false;
119                                 wiphy_err(wlc->wiphy, "antsel_attach: 2o3 "
120                                           "board cfg invalid\n");
121                         }
122
123                         break;
124                 default:
125                         break;
126                 }
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;
136         }
137
138         /* Set the antenna selection type for the low driver */
139         brcms_b_antsel_type_set(wlc->hw, asi->antsel_type);
140
141         /* Init (auto/manual) antenna selection */
142         brcms_c_antsel_init_cfg(asi, &asi->antcfg_11n, true);
143         brcms_c_antsel_init_cfg(asi, &asi->antcfg_cur, true);
144
145         return asi;
146 }
147
148 void brcms_c_antsel_detach(struct antsel_info *asi)
149 {
150         kfree(asi);
151 }
152
153 void brcms_c_antsel_init(struct antsel_info *asi)
154 {
155         if ((asi->antsel_type == ANTSEL_2x3) ||
156             (asi->antsel_type == ANTSEL_2x4))
157                 brcms_c_antsel_cfgupd(asi, &asi->antcfg_11n);
158 }
159
160 /* boardlevel antenna selection: init antenna selection structure */
161 static void
162 brcms_c_antsel_init_cfg(struct antsel_info *asi, struct brcms_antselcfg *antsel,
163                     bool auto_sel)
164 {
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;
173
174         } else if (asi->antsel_type == ANTSEL_2x4) {
175
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;
181
182         } else {                /* no antenna selection available */
183
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;
189         }
190 }
191
192 void
193 brcms_c_antsel_antcfg_get(struct antsel_info *asi, bool usedef, bool sel,
194                       u8 antselid, u8 fbantselid, u8 *antcfg,
195                       u8 *fbantcfg)
196 {
197         u8 ant;
198
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;
204         }
205
206         if (!sel) {
207                 *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
208                 *fbantcfg = *antcfg;
209
210         } else {
211                 ant = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
212                 if ((ant & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO) {
213                         *antcfg = brcms_c_antsel_id2antcfg(asi, antselid);
214                         *fbantcfg = brcms_c_antsel_id2antcfg(asi, fbantselid);
215                 } else {
216                         *antcfg =
217                             asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
218                         *fbantcfg = *antcfg;
219                 }
220         }
221         return;
222 }
223
224 /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
225 u8 brcms_c_antsel_antsel2id(struct antsel_info *asi, u16 antsel)
226 {
227         u8 antselid = 0;
228
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;
233
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;
238         }
239
240         return antselid;
241 }
242
243 /* boardlevel antenna selection: convert id to ant_cfg */
244 static u8 brcms_c_antsel_id2antcfg(struct antsel_info *asi, u8 id)
245 {
246         u8 antcfg = ANT_SELCFG_DEF_2x2;
247
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;
252
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;
257         }
258
259         return antcfg;
260 }
261
262 /*
263  * boardlevel antenna selection:
264  *   convert ant_cfg to mimo_antsel (ucode interface)
265  */
266 static u16 brcms_c_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg)
267 {
268         u8 idx = BRCMS_ANTIDX_11N(BRCMS_ANTSEL_11N(ant_cfg));
269         u16 mimo_antsel = 0;
270
271         if (asi->antsel_type == ANTSEL_2x4) {
272                 /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
273                 mimo_antsel = (mimo_2x4_div_antselpat_tbl[idx] & 0xf);
274                 return mimo_antsel;
275
276         } else if (asi->antsel_type == ANTSEL_2x3) {
277                 /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
278                 mimo_antsel = (mimo_2x3_div_antselpat_tbl[idx] & 0xf);
279                 return mimo_antsel;
280         }
281
282         return mimo_antsel;
283 }
284
285 /* boardlevel antenna selection: ucode interface control */
286 static int brcms_c_antsel_cfgupd(struct antsel_info *asi,
287                                  struct brcms_antselcfg *antsel)
288 {
289         struct brcms_c_info *wlc = asi->wlc;
290         u8 ant_cfg;
291         u16 mimo_antsel;
292
293         /* 1) Update TX antconfig for all frames that are not unicast data
294          *    (aka default TX)
295          */
296         ant_cfg = antsel->ant_config[ANT_SELCFG_TX_DEF];
297         mimo_antsel = brcms_c_antsel_antcfg2antsel(asi, ant_cfg);
298         brcms_c_write_shm(wlc, M_MIMO_ANTSEL_TXDFLT, mimo_antsel);
299         /*
300          * Update driver stats for currently selected
301          * default tx/rx antenna config
302          */
303         asi->antcfg_cur.ant_config[ANT_SELCFG_TX_DEF] = ant_cfg;
304
305         /* 2) Update RX antconfig for all frames that are not unicast data
306          *    (aka default RX)
307          */
308         ant_cfg = antsel->ant_config[ANT_SELCFG_RX_DEF];
309         mimo_antsel = brcms_c_antsel_antcfg2antsel(asi, ant_cfg);
310         brcms_c_write_shm(wlc, M_MIMO_ANTSEL_RXDFLT, mimo_antsel);
311         /*
312          * Update driver stats for currently selected
313          * default tx/rx antenna config
314          */
315         asi->antcfg_cur.ant_config[ANT_SELCFG_RX_DEF] = ant_cfg;
316
317         return 0;
318 }