2 * Mac80211 SDIO driver for ST-Ericsson CW1200 device
4 * Copyright (c) 2010, ST-Ericsson
5 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/gpio.h>
15 #include <linux/delay.h>
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/sdio_func.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/sdio.h>
20 #include <net/mac80211.h>
24 #include <linux/platform_data/net-cw1200.h>
27 MODULE_AUTHOR("Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>");
28 MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SDIO driver");
29 MODULE_LICENSE("GPL");
31 #define SDIO_BLOCK_SIZE (512)
33 /* Default platform data for Sagrad modules */
34 static struct cw1200_platform_data_sdio sagrad_109x_evk_platform_data
= {
37 .sdd_file
= "sdd_sagrad_1091_1098.bin",
40 /* Allow platform data to be overridden */
41 static struct cw1200_platform_data_sdio
*global_plat_data
= &sagrad_109x_evk_platform_data
;
43 void __init
cw1200_sdio_set_platform_data(struct cw1200_platform_data_sdio
*pdata
)
45 global_plat_data
= pdata
;
49 struct sdio_func
*func
;
50 struct cw1200_common
*core
;
51 const struct cw1200_platform_data_sdio
*pdata
;
54 #ifndef SDIO_VENDOR_ID_STE
55 #define SDIO_VENDOR_ID_STE 0x0020
58 #ifndef SDIO_DEVICE_ID_STE_CW1200
59 #define SDIO_DEVICE_ID_STE_CW1200 0x2280
62 static const struct sdio_device_id cw1200_sdio_ids
[] = {
63 { SDIO_DEVICE(SDIO_VENDOR_ID_STE
, SDIO_DEVICE_ID_STE_CW1200
) },
64 { /* end: all zeroes */ },
67 /* hwbus_ops implemetation */
69 static int cw1200_sdio_memcpy_fromio(struct hwbus_priv
*self
,
73 return sdio_memcpy_fromio(self
->func
, dst
, addr
, count
);
76 static int cw1200_sdio_memcpy_toio(struct hwbus_priv
*self
,
78 const void *src
, int count
)
80 return sdio_memcpy_toio(self
->func
, addr
, (void *)src
, count
);
83 static void cw1200_sdio_lock(struct hwbus_priv
*self
)
85 sdio_claim_host(self
->func
);
88 static void cw1200_sdio_unlock(struct hwbus_priv
*self
)
90 sdio_release_host(self
->func
);
93 static void cw1200_sdio_irq_handler(struct sdio_func
*func
)
95 struct hwbus_priv
*self
= sdio_get_drvdata(func
);
97 /* note: sdio_host already claimed here. */
99 cw1200_irq_handler(self
->core
);
102 static irqreturn_t
cw1200_gpio_hardirq(int irq
, void *dev_id
)
104 return IRQ_WAKE_THREAD
;
107 static irqreturn_t
cw1200_gpio_irq(int irq
, void *dev_id
)
109 struct hwbus_priv
*self
= dev_id
;
112 cw1200_sdio_lock(self
);
113 cw1200_irq_handler(self
->core
);
114 cw1200_sdio_unlock(self
);
121 static int cw1200_request_irq(struct hwbus_priv
*self
)
126 cccr
= sdio_f0_readb(self
->func
, SDIO_CCCR_IENx
, &ret
);
130 /* Master interrupt enable ... */
133 /* ... for our function */
134 cccr
|= BIT(self
->func
->num
);
136 sdio_f0_writeb(self
->func
, cccr
, SDIO_CCCR_IENx
, &ret
);
140 ret
= enable_irq_wake(self
->pdata
->irq
);
144 /* Request the IRQ */
145 ret
= request_threaded_irq(self
->pdata
->irq
, cw1200_gpio_hardirq
,
147 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
148 "cw1200_wlan_irq", self
);
158 static int cw1200_sdio_irq_subscribe(struct hwbus_priv
*self
)
162 pr_debug("SW IRQ subscribe\n");
163 sdio_claim_host(self
->func
);
164 if (self
->pdata
->irq
)
165 ret
= cw1200_request_irq(self
);
167 ret
= sdio_claim_irq(self
->func
, cw1200_sdio_irq_handler
);
169 sdio_release_host(self
->func
);
173 static int cw1200_sdio_irq_unsubscribe(struct hwbus_priv
*self
)
177 pr_debug("SW IRQ unsubscribe\n");
179 if (self
->pdata
->irq
) {
180 disable_irq_wake(self
->pdata
->irq
);
181 free_irq(self
->pdata
->irq
, self
);
183 sdio_claim_host(self
->func
);
184 ret
= sdio_release_irq(self
->func
);
185 sdio_release_host(self
->func
);
190 static int cw1200_sdio_off(const struct cw1200_platform_data_sdio
*pdata
)
193 gpio_set_value(pdata
->reset
, 0);
194 msleep(30); /* Min is 2 * CLK32K cycles */
195 gpio_free(pdata
->reset
);
198 if (pdata
->power_ctrl
)
199 pdata
->power_ctrl(pdata
, false);
201 pdata
->clk_ctrl(pdata
, false);
206 static int cw1200_sdio_on(const struct cw1200_platform_data_sdio
*pdata
)
208 /* Ensure I/Os are pulled low */
210 gpio_request(pdata
->reset
, "cw1200_wlan_reset");
211 gpio_direction_output(pdata
->reset
, 0);
213 if (pdata
->powerup
) {
214 gpio_request(pdata
->powerup
, "cw1200_wlan_powerup");
215 gpio_direction_output(pdata
->powerup
, 0);
217 if (pdata
->reset
|| pdata
->powerup
)
218 msleep(10); /* Settle time? */
220 /* Enable 3v3 and 1v8 to hardware */
221 if (pdata
->power_ctrl
) {
222 if (pdata
->power_ctrl(pdata
, true)) {
223 pr_err("power_ctrl() failed!\n");
229 if (pdata
->clk_ctrl
) {
230 if (pdata
->clk_ctrl(pdata
, true)) {
231 pr_err("clk_ctrl() failed!\n");
234 msleep(10); /* Delay until clock is stable for 2 cycles */
237 /* Enable POWERUP signal */
238 if (pdata
->powerup
) {
239 gpio_set_value(pdata
->powerup
, 1);
240 msleep(250); /* or more..? */
242 /* Enable RSTn signal */
244 gpio_set_value(pdata
->reset
, 1);
245 msleep(50); /* Or more..? */
250 static size_t cw1200_sdio_align_size(struct hwbus_priv
*self
, size_t size
)
252 if (self
->pdata
->no_nptb
)
253 size
= round_up(size
, SDIO_BLOCK_SIZE
);
255 size
= sdio_align_size(self
->func
, size
);
260 static int cw1200_sdio_pm(struct hwbus_priv
*self
, bool suspend
)
264 if (self
->pdata
->irq
)
265 ret
= irq_set_irq_wake(self
->pdata
->irq
, suspend
);
269 static const struct hwbus_ops cw1200_sdio_hwbus_ops
= {
270 .hwbus_memcpy_fromio
= cw1200_sdio_memcpy_fromio
,
271 .hwbus_memcpy_toio
= cw1200_sdio_memcpy_toio
,
272 .lock
= cw1200_sdio_lock
,
273 .unlock
= cw1200_sdio_unlock
,
274 .align_size
= cw1200_sdio_align_size
,
275 .power_mgmt
= cw1200_sdio_pm
,
278 /* Probe Function to be called by SDIO stack when device is discovered */
279 static int cw1200_sdio_probe(struct sdio_func
*func
,
280 const struct sdio_device_id
*id
)
282 struct hwbus_priv
*self
;
285 pr_info("cw1200_wlan_sdio: Probe called\n");
287 /* We are only able to handle the wlan function */
288 if (func
->num
!= 0x01)
291 self
= kzalloc(sizeof(*self
), GFP_KERNEL
);
293 pr_err("Can't allocate SDIO hwbus_priv.\n");
297 func
->card
->quirks
|= MMC_QUIRK_LENIENT_FN0
;
299 self
->pdata
= global_plat_data
; /* FIXME */
301 sdio_set_drvdata(func
, self
);
302 sdio_claim_host(func
);
303 sdio_enable_func(func
);
304 sdio_release_host(func
);
306 status
= cw1200_sdio_irq_subscribe(self
);
308 status
= cw1200_core_probe(&cw1200_sdio_hwbus_ops
,
309 self
, &func
->dev
, &self
->core
,
310 self
->pdata
->ref_clk
,
311 self
->pdata
->macaddr
,
312 self
->pdata
->sdd_file
,
313 self
->pdata
->have_5ghz
);
315 cw1200_sdio_irq_unsubscribe(self
);
316 sdio_claim_host(func
);
317 sdio_disable_func(func
);
318 sdio_release_host(func
);
319 sdio_set_drvdata(func
, NULL
);
326 /* Disconnect Function to be called by SDIO stack when
327 * device is disconnected
329 static void cw1200_sdio_disconnect(struct sdio_func
*func
)
331 struct hwbus_priv
*self
= sdio_get_drvdata(func
);
334 cw1200_sdio_irq_unsubscribe(self
);
336 cw1200_core_release(self
->core
);
339 sdio_claim_host(func
);
340 sdio_disable_func(func
);
341 sdio_release_host(func
);
342 sdio_set_drvdata(func
, NULL
);
348 static int cw1200_sdio_suspend(struct device
*dev
)
351 struct sdio_func
*func
= dev_to_sdio_func(dev
);
352 struct hwbus_priv
*self
= sdio_get_drvdata(func
);
354 if (!cw1200_can_suspend(self
->core
))
357 /* Notify SDIO that CW1200 will remain powered during suspend */
358 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
360 pr_err("Error setting SDIO pm flags: %i\n", ret
);
365 static int cw1200_sdio_resume(struct device
*dev
)
370 static const struct dev_pm_ops cw1200_pm_ops
= {
371 .suspend
= cw1200_sdio_suspend
,
372 .resume
= cw1200_sdio_resume
,
376 static struct sdio_driver sdio_driver
= {
377 .name
= "cw1200_wlan_sdio",
378 .id_table
= cw1200_sdio_ids
,
379 .probe
= cw1200_sdio_probe
,
380 .remove
= cw1200_sdio_disconnect
,
383 .pm
= &cw1200_pm_ops
,
388 /* Init Module function -> Called by insmod */
389 static int __init
cw1200_sdio_init(void)
391 const struct cw1200_platform_data_sdio
*pdata
;
394 /* FIXME -- this won't support multiple devices */
395 pdata
= global_plat_data
;
397 if (cw1200_sdio_on(pdata
)) {
402 ret
= sdio_register_driver(&sdio_driver
);
409 cw1200_sdio_off(pdata
);
413 /* Called at Driver Unloading */
414 static void __exit
cw1200_sdio_exit(void)
416 const struct cw1200_platform_data_sdio
*pdata
;
418 /* FIXME -- this won't support multiple devices */
419 pdata
= global_plat_data
;
420 sdio_unregister_driver(&sdio_driver
);
421 cw1200_sdio_off(pdata
);
425 module_init(cw1200_sdio_init
);
426 module_exit(cw1200_sdio_exit
);