Merge branch 'drm-core-next' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[pandora-kernel.git] / drivers / net / wireless / wl12xx / sdio.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2009-2010 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/irq.h>
25 #include <linux/module.h>
26 #include <linux/crc7.h>
27 #include <linux/vmalloc.h>
28 #include <linux/mmc/sdio_func.h>
29 #include <linux/mmc/sdio_ids.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mmc/host.h>
32 #include <linux/gpio.h>
33 #include <linux/wl12xx.h>
34 #include <linux/pm_runtime.h>
35
36 #include "wl12xx.h"
37 #include "wl12xx_80211.h"
38 #include "io.h"
39
40 #ifndef SDIO_VENDOR_ID_TI
41 #define SDIO_VENDOR_ID_TI               0x0097
42 #endif
43
44 #ifndef SDIO_DEVICE_ID_TI_WL1271
45 #define SDIO_DEVICE_ID_TI_WL1271        0x4076
46 #endif
47
48 static const struct sdio_device_id wl1271_devices[] = {
49         { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
50         {}
51 };
52 MODULE_DEVICE_TABLE(sdio, wl1271_devices);
53
54 static void wl1271_sdio_set_block_size(struct wl1271 *wl, unsigned int blksz)
55 {
56         sdio_claim_host(wl->if_priv);
57         sdio_set_block_size(wl->if_priv, blksz);
58         sdio_release_host(wl->if_priv);
59 }
60
61 static inline struct sdio_func *wl_to_func(struct wl1271 *wl)
62 {
63         return wl->if_priv;
64 }
65
66 static struct device *wl1271_sdio_wl_to_dev(struct wl1271 *wl)
67 {
68         return &(wl_to_func(wl)->dev);
69 }
70
71 static irqreturn_t wl1271_hardirq(int irq, void *cookie)
72 {
73         struct wl1271 *wl = cookie;
74         unsigned long flags;
75
76         wl1271_debug(DEBUG_IRQ, "IRQ");
77
78         /* complete the ELP completion */
79         spin_lock_irqsave(&wl->wl_lock, flags);
80         set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
81         if (wl->elp_compl) {
82                 complete(wl->elp_compl);
83                 wl->elp_compl = NULL;
84         }
85
86         if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
87                 /* don't enqueue a work right now. mark it as pending */
88                 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
89                 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
90                 disable_irq_nosync(wl->irq);
91                 pm_wakeup_event(wl1271_sdio_wl_to_dev(wl), 0);
92                 spin_unlock_irqrestore(&wl->wl_lock, flags);
93                 return IRQ_HANDLED;
94         }
95         spin_unlock_irqrestore(&wl->wl_lock, flags);
96
97         return IRQ_WAKE_THREAD;
98 }
99
100 static void wl1271_sdio_disable_interrupts(struct wl1271 *wl)
101 {
102         disable_irq(wl->irq);
103 }
104
105 static void wl1271_sdio_enable_interrupts(struct wl1271 *wl)
106 {
107         enable_irq(wl->irq);
108 }
109
110 static void wl1271_sdio_reset(struct wl1271 *wl)
111 {
112 }
113
114 static void wl1271_sdio_init(struct wl1271 *wl)
115 {
116 }
117
118 static void wl1271_sdio_raw_read(struct wl1271 *wl, int addr, void *buf,
119                                  size_t len, bool fixed)
120 {
121         int ret;
122         struct sdio_func *func = wl_to_func(wl);
123
124         if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
125                 ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
126                 wl1271_debug(DEBUG_SDIO, "sdio read 52 addr 0x%x, byte 0x%02x",
127                              addr, ((u8 *)buf)[0]);
128         } else {
129                 if (fixed)
130                         ret = sdio_readsb(func, buf, addr, len);
131                 else
132                         ret = sdio_memcpy_fromio(func, buf, addr, len);
133
134                 wl1271_debug(DEBUG_SDIO, "sdio read 53 addr 0x%x, %zu bytes",
135                              addr, len);
136                 wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
137         }
138
139         if (ret)
140                 wl1271_error("sdio read failed (%d)", ret);
141 }
142
143 static void wl1271_sdio_raw_write(struct wl1271 *wl, int addr, void *buf,
144                                   size_t len, bool fixed)
145 {
146         int ret;
147         struct sdio_func *func = wl_to_func(wl);
148
149         if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
150                 sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
151                 wl1271_debug(DEBUG_SDIO, "sdio write 52 addr 0x%x, byte 0x%02x",
152                              addr, ((u8 *)buf)[0]);
153         } else {
154                 wl1271_debug(DEBUG_SDIO, "sdio write 53 addr 0x%x, %zu bytes",
155                              addr, len);
156                 wl1271_dump_ascii(DEBUG_SDIO, "data: ", buf, len);
157
158                 if (fixed)
159                         ret = sdio_writesb(func, addr, buf, len);
160                 else
161                         ret = sdio_memcpy_toio(func, addr, buf, len);
162         }
163
164         if (ret)
165                 wl1271_error("sdio write failed (%d)", ret);
166 }
167
168 static int wl1271_sdio_power_on(struct wl1271 *wl)
169 {
170         struct sdio_func *func = wl_to_func(wl);
171         int ret;
172
173         /* Make sure the card will not be powered off by runtime PM */
174         ret = pm_runtime_get_sync(&func->dev);
175         if (ret < 0)
176                 goto out;
177
178         /* Runtime PM might be disabled, so power up the card manually */
179         ret = mmc_power_restore_host(func->card->host);
180         if (ret < 0)
181                 goto out;
182
183         sdio_claim_host(func);
184         sdio_enable_func(func);
185
186 out:
187         return ret;
188 }
189
190 static int wl1271_sdio_power_off(struct wl1271 *wl)
191 {
192         struct sdio_func *func = wl_to_func(wl);
193         int ret;
194
195         sdio_disable_func(func);
196         sdio_release_host(func);
197
198         /* Runtime PM might be disabled, so power off the card manually */
199         ret = mmc_power_save_host(func->card->host);
200         if (ret < 0)
201                 return ret;
202
203         /* Let runtime PM know the card is powered off */
204         return pm_runtime_put_sync(&func->dev);
205 }
206
207 static int wl1271_sdio_set_power(struct wl1271 *wl, bool enable)
208 {
209         if (enable)
210                 return wl1271_sdio_power_on(wl);
211         else
212                 return wl1271_sdio_power_off(wl);
213 }
214
215 static struct wl1271_if_operations sdio_ops = {
216         .read           = wl1271_sdio_raw_read,
217         .write          = wl1271_sdio_raw_write,
218         .reset          = wl1271_sdio_reset,
219         .init           = wl1271_sdio_init,
220         .power          = wl1271_sdio_set_power,
221         .dev            = wl1271_sdio_wl_to_dev,
222         .enable_irq     = wl1271_sdio_enable_interrupts,
223         .disable_irq    = wl1271_sdio_disable_interrupts,
224         .set_block_size = wl1271_sdio_set_block_size,
225 };
226
227 static int __devinit wl1271_probe(struct sdio_func *func,
228                                   const struct sdio_device_id *id)
229 {
230         struct ieee80211_hw *hw;
231         const struct wl12xx_platform_data *wlan_data;
232         struct wl1271 *wl;
233         unsigned long irqflags;
234         mmc_pm_flag_t mmcflags;
235         int ret;
236
237         /* We are only able to handle the wlan function */
238         if (func->num != 0x02)
239                 return -ENODEV;
240
241         hw = wl1271_alloc_hw();
242         if (IS_ERR(hw))
243                 return PTR_ERR(hw);
244
245         wl = hw->priv;
246
247         wl->if_priv = func;
248         wl->if_ops = &sdio_ops;
249
250         /* Grab access to FN0 for ELP reg. */
251         func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
252
253         /* Use block mode for transferring over one block size of data */
254         func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
255
256         wlan_data = wl12xx_get_platform_data();
257         if (IS_ERR(wlan_data)) {
258                 ret = PTR_ERR(wlan_data);
259                 wl1271_error("missing wlan platform data: %d", ret);
260                 goto out_free;
261         }
262
263         wl->irq = wlan_data->irq;
264         wl->ref_clock = wlan_data->board_ref_clock;
265         wl->tcxo_clock = wlan_data->board_tcxo_clock;
266         wl->platform_quirks = wlan_data->platform_quirks;
267
268         if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
269                 irqflags = IRQF_TRIGGER_RISING;
270         else
271                 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
272
273         ret = request_threaded_irq(wl->irq, wl1271_hardirq, wl1271_irq,
274                                    irqflags,
275                                    DRIVER_NAME, wl);
276         if (ret < 0) {
277                 wl1271_error("request_irq() failed: %d", ret);
278                 goto out_free;
279         }
280
281         enable_irq_wake(wl->irq);
282         device_init_wakeup(wl1271_sdio_wl_to_dev(wl), 1);
283
284         disable_irq(wl->irq);
285
286         /* if sdio can keep power while host is suspended, enable wow */
287         mmcflags = sdio_get_host_pm_caps(func);
288         wl1271_debug(DEBUG_SDIO, "sdio PM caps = 0x%x", mmcflags);
289
290         if (mmcflags & MMC_PM_KEEP_POWER)
291                 hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
292
293         ret = wl1271_init_ieee80211(wl);
294         if (ret)
295                 goto out_irq;
296
297         ret = wl1271_register_hw(wl);
298         if (ret)
299                 goto out_irq;
300
301         sdio_set_drvdata(func, wl);
302
303         /* Tell PM core that we don't need the card to be powered now */
304         pm_runtime_put_noidle(&func->dev);
305
306         wl1271_notice("initialized");
307
308         return 0;
309
310  out_irq:
311         free_irq(wl->irq, wl);
312
313  out_free:
314         wl1271_free_hw(wl);
315
316         return ret;
317 }
318
319 static void __devexit wl1271_remove(struct sdio_func *func)
320 {
321         struct wl1271 *wl = sdio_get_drvdata(func);
322
323         /* Undo decrement done above in wl1271_probe */
324         pm_runtime_get_noresume(&func->dev);
325
326         wl1271_unregister_hw(wl);
327         device_init_wakeup(wl1271_sdio_wl_to_dev(wl), 0);
328         disable_irq_wake(wl->irq);
329         free_irq(wl->irq, wl);
330         wl1271_free_hw(wl);
331 }
332
333 #ifdef CONFIG_PM
334 static int wl1271_suspend(struct device *dev)
335 {
336         /* Tell MMC/SDIO core it's OK to power down the card
337          * (if it isn't already), but not to remove it completely */
338         struct sdio_func *func = dev_to_sdio_func(dev);
339         struct wl1271 *wl = sdio_get_drvdata(func);
340         mmc_pm_flag_t sdio_flags;
341         int ret = 0;
342
343         wl1271_debug(DEBUG_MAC80211, "wl1271 suspend. wow_enabled: %d",
344                      wl->wow_enabled);
345
346         /* check whether sdio should keep power */
347         if (wl->wow_enabled) {
348                 sdio_flags = sdio_get_host_pm_caps(func);
349
350                 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
351                         wl1271_error("can't keep power while host "
352                                      "is suspended");
353                         ret = -EINVAL;
354                         goto out;
355                 }
356
357                 /* keep power while host suspended */
358                 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
359                 if (ret) {
360                         wl1271_error("error while trying to keep power");
361                         goto out;
362                 }
363
364                 /* release host */
365                 sdio_release_host(func);
366         }
367 out:
368         return ret;
369 }
370
371 static int wl1271_resume(struct device *dev)
372 {
373         struct sdio_func *func = dev_to_sdio_func(dev);
374         struct wl1271 *wl = sdio_get_drvdata(func);
375
376         wl1271_debug(DEBUG_MAC80211, "wl1271 resume");
377         if (wl->wow_enabled) {
378                 /* claim back host */
379                 sdio_claim_host(func);
380         }
381
382         return 0;
383 }
384
385 static const struct dev_pm_ops wl1271_sdio_pm_ops = {
386         .suspend        = wl1271_suspend,
387         .resume         = wl1271_resume,
388 };
389 #endif
390
391 static struct sdio_driver wl1271_sdio_driver = {
392         .name           = "wl1271_sdio",
393         .id_table       = wl1271_devices,
394         .probe          = wl1271_probe,
395         .remove         = __devexit_p(wl1271_remove),
396 #ifdef CONFIG_PM
397         .drv = {
398                 .pm = &wl1271_sdio_pm_ops,
399         },
400 #endif
401 };
402
403 static int __init wl1271_init(void)
404 {
405         int ret;
406
407         ret = sdio_register_driver(&wl1271_sdio_driver);
408         if (ret < 0) {
409                 wl1271_error("failed to register sdio driver: %d", ret);
410                 goto out;
411         }
412
413 out:
414         return ret;
415 }
416
417 static void __exit wl1271_exit(void)
418 {
419         sdio_unregister_driver(&wl1271_sdio_driver);
420
421         wl1271_notice("unloaded");
422 }
423
424 module_init(wl1271_init);
425 module_exit(wl1271_exit);
426
427 MODULE_LICENSE("GPL");
428 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
429 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
430 MODULE_FIRMWARE(WL1271_FW_NAME);
431 MODULE_FIRMWARE(WL128X_FW_NAME);
432 MODULE_FIRMWARE(WL127X_AP_FW_NAME);
433 MODULE_FIRMWARE(WL128X_AP_FW_NAME);