1 // SPDX-License-Identifier: GPL-2.0-only
3 * Mac80211 SDIO driver for ST-Ericsson CW1200 device
5 * Copyright (c) 2010, ST-Ericsson
6 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/gpio.h>
12 #include <linux/delay.h>
13 #include <linux/mmc/host.h>
14 #include <linux/mmc/sdio_func.h>
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/sdio.h>
17 #include <linux/mmc/sdio_ids.h>
18 #include <net/mac80211.h>
22 #include <linux/platform_data/net-cw1200.h>
25 MODULE_AUTHOR("Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>");
26 MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SDIO driver");
27 MODULE_LICENSE("GPL");
29 #define SDIO_BLOCK_SIZE (512)
31 /* Default platform data for Sagrad modules */
32 static struct cw1200_platform_data_sdio sagrad_109x_evk_platform_data
= {
35 .sdd_file
= "sdd_sagrad_1091_1098.bin",
38 /* Allow platform data to be overridden */
39 static struct cw1200_platform_data_sdio
*global_plat_data
= &sagrad_109x_evk_platform_data
;
41 void __init
cw1200_sdio_set_platform_data(struct cw1200_platform_data_sdio
*pdata
)
43 global_plat_data
= pdata
;
47 struct sdio_func
*func
;
48 struct cw1200_common
*core
;
49 const struct cw1200_platform_data_sdio
*pdata
;
52 static const struct sdio_device_id cw1200_sdio_ids
[] = {
53 { SDIO_DEVICE(SDIO_VENDOR_ID_STE
, SDIO_DEVICE_ID_STE_CW1200
) },
54 { /* end: all zeroes */ },
57 /* hwbus_ops implemetation */
59 static int cw1200_sdio_memcpy_fromio(struct hwbus_priv
*self
,
63 return sdio_memcpy_fromio(self
->func
, dst
, addr
, count
);
66 static int cw1200_sdio_memcpy_toio(struct hwbus_priv
*self
,
68 const void *src
, int count
)
70 return sdio_memcpy_toio(self
->func
, addr
, (void *)src
, count
);
73 static void cw1200_sdio_lock(struct hwbus_priv
*self
)
75 sdio_claim_host(self
->func
);
78 static void cw1200_sdio_unlock(struct hwbus_priv
*self
)
80 sdio_release_host(self
->func
);
83 static void cw1200_sdio_irq_handler(struct sdio_func
*func
)
85 struct hwbus_priv
*self
= sdio_get_drvdata(func
);
87 /* note: sdio_host already claimed here. */
89 cw1200_irq_handler(self
->core
);
92 static irqreturn_t
cw1200_gpio_hardirq(int irq
, void *dev_id
)
94 return IRQ_WAKE_THREAD
;
97 static irqreturn_t
cw1200_gpio_irq(int irq
, void *dev_id
)
99 struct hwbus_priv
*self
= dev_id
;
102 cw1200_sdio_lock(self
);
103 cw1200_irq_handler(self
->core
);
104 cw1200_sdio_unlock(self
);
111 static int cw1200_request_irq(struct hwbus_priv
*self
)
116 cccr
= sdio_f0_readb(self
->func
, SDIO_CCCR_IENx
, &ret
);
120 /* Master interrupt enable ... */
123 /* ... for our function */
124 cccr
|= BIT(self
->func
->num
);
126 sdio_f0_writeb(self
->func
, cccr
, SDIO_CCCR_IENx
, &ret
);
130 ret
= enable_irq_wake(self
->pdata
->irq
);
134 /* Request the IRQ */
135 ret
= request_threaded_irq(self
->pdata
->irq
, cw1200_gpio_hardirq
,
137 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
138 "cw1200_wlan_irq", self
);
148 static int cw1200_sdio_irq_subscribe(struct hwbus_priv
*self
)
152 pr_debug("SW IRQ subscribe\n");
153 sdio_claim_host(self
->func
);
154 if (self
->pdata
->irq
)
155 ret
= cw1200_request_irq(self
);
157 ret
= sdio_claim_irq(self
->func
, cw1200_sdio_irq_handler
);
159 sdio_release_host(self
->func
);
163 static int cw1200_sdio_irq_unsubscribe(struct hwbus_priv
*self
)
167 pr_debug("SW IRQ unsubscribe\n");
169 if (self
->pdata
->irq
) {
170 disable_irq_wake(self
->pdata
->irq
);
171 free_irq(self
->pdata
->irq
, self
);
173 sdio_claim_host(self
->func
);
174 ret
= sdio_release_irq(self
->func
);
175 sdio_release_host(self
->func
);
180 static int cw1200_sdio_off(const struct cw1200_platform_data_sdio
*pdata
)
183 gpio_set_value(pdata
->reset
, 0);
184 msleep(30); /* Min is 2 * CLK32K cycles */
185 gpio_free(pdata
->reset
);
188 if (pdata
->power_ctrl
)
189 pdata
->power_ctrl(pdata
, false);
191 pdata
->clk_ctrl(pdata
, false);
196 static int cw1200_sdio_on(const struct cw1200_platform_data_sdio
*pdata
)
198 /* Ensure I/Os are pulled low */
200 gpio_request(pdata
->reset
, "cw1200_wlan_reset");
201 gpio_direction_output(pdata
->reset
, 0);
203 if (pdata
->powerup
) {
204 gpio_request(pdata
->powerup
, "cw1200_wlan_powerup");
205 gpio_direction_output(pdata
->powerup
, 0);
207 if (pdata
->reset
|| pdata
->powerup
)
208 msleep(10); /* Settle time? */
210 /* Enable 3v3 and 1v8 to hardware */
211 if (pdata
->power_ctrl
) {
212 if (pdata
->power_ctrl(pdata
, true)) {
213 pr_err("power_ctrl() failed!\n");
219 if (pdata
->clk_ctrl
) {
220 if (pdata
->clk_ctrl(pdata
, true)) {
221 pr_err("clk_ctrl() failed!\n");
224 msleep(10); /* Delay until clock is stable for 2 cycles */
227 /* Enable POWERUP signal */
228 if (pdata
->powerup
) {
229 gpio_set_value(pdata
->powerup
, 1);
230 msleep(250); /* or more..? */
232 /* Enable RSTn signal */
234 gpio_set_value(pdata
->reset
, 1);
235 msleep(50); /* Or more..? */
240 static size_t cw1200_sdio_align_size(struct hwbus_priv
*self
, size_t size
)
242 if (self
->pdata
->no_nptb
)
243 size
= round_up(size
, SDIO_BLOCK_SIZE
);
245 size
= sdio_align_size(self
->func
, size
);
250 static int cw1200_sdio_pm(struct hwbus_priv
*self
, bool suspend
)
254 if (self
->pdata
->irq
)
255 ret
= irq_set_irq_wake(self
->pdata
->irq
, suspend
);
259 static const struct hwbus_ops cw1200_sdio_hwbus_ops
= {
260 .hwbus_memcpy_fromio
= cw1200_sdio_memcpy_fromio
,
261 .hwbus_memcpy_toio
= cw1200_sdio_memcpy_toio
,
262 .lock
= cw1200_sdio_lock
,
263 .unlock
= cw1200_sdio_unlock
,
264 .align_size
= cw1200_sdio_align_size
,
265 .power_mgmt
= cw1200_sdio_pm
,
268 /* Probe Function to be called by SDIO stack when device is discovered */
269 static int cw1200_sdio_probe(struct sdio_func
*func
,
270 const struct sdio_device_id
*id
)
272 struct hwbus_priv
*self
;
275 pr_info("cw1200_wlan_sdio: Probe called\n");
277 /* We are only able to handle the wlan function */
278 if (func
->num
!= 0x01)
281 self
= kzalloc(sizeof(*self
), GFP_KERNEL
);
283 pr_err("Can't allocate SDIO hwbus_priv.\n");
287 func
->card
->quirks
|= MMC_QUIRK_LENIENT_FN0
;
289 self
->pdata
= global_plat_data
; /* FIXME */
291 sdio_set_drvdata(func
, self
);
292 sdio_claim_host(func
);
293 sdio_enable_func(func
);
294 sdio_release_host(func
);
296 status
= cw1200_sdio_irq_subscribe(self
);
298 status
= cw1200_core_probe(&cw1200_sdio_hwbus_ops
,
299 self
, &func
->dev
, &self
->core
,
300 self
->pdata
->ref_clk
,
301 self
->pdata
->macaddr
,
302 self
->pdata
->sdd_file
,
303 self
->pdata
->have_5ghz
);
305 cw1200_sdio_irq_unsubscribe(self
);
306 sdio_claim_host(func
);
307 sdio_disable_func(func
);
308 sdio_release_host(func
);
309 sdio_set_drvdata(func
, NULL
);
316 /* Disconnect Function to be called by SDIO stack when
317 * device is disconnected
319 static void cw1200_sdio_disconnect(struct sdio_func
*func
)
321 struct hwbus_priv
*self
= sdio_get_drvdata(func
);
324 cw1200_sdio_irq_unsubscribe(self
);
326 cw1200_core_release(self
->core
);
329 sdio_claim_host(func
);
330 sdio_disable_func(func
);
331 sdio_release_host(func
);
332 sdio_set_drvdata(func
, NULL
);
338 static int cw1200_sdio_suspend(struct device
*dev
)
341 struct sdio_func
*func
= dev_to_sdio_func(dev
);
342 struct hwbus_priv
*self
= sdio_get_drvdata(func
);
344 if (!cw1200_can_suspend(self
->core
))
347 /* Notify SDIO that CW1200 will remain powered during suspend */
348 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
350 pr_err("Error setting SDIO pm flags: %i\n", ret
);
355 static int cw1200_sdio_resume(struct device
*dev
)
360 static const struct dev_pm_ops cw1200_pm_ops
= {
361 .suspend
= cw1200_sdio_suspend
,
362 .resume
= cw1200_sdio_resume
,
366 static struct sdio_driver sdio_driver
= {
367 .name
= "cw1200_wlan_sdio",
368 .id_table
= cw1200_sdio_ids
,
369 .probe
= cw1200_sdio_probe
,
370 .remove
= cw1200_sdio_disconnect
,
373 .pm
= &cw1200_pm_ops
,
378 /* Init Module function -> Called by insmod */
379 static int __init
cw1200_sdio_init(void)
381 const struct cw1200_platform_data_sdio
*pdata
;
384 /* FIXME -- this won't support multiple devices */
385 pdata
= global_plat_data
;
387 if (cw1200_sdio_on(pdata
)) {
392 ret
= sdio_register_driver(&sdio_driver
);
399 cw1200_sdio_off(pdata
);
403 /* Called at Driver Unloading */
404 static void __exit
cw1200_sdio_exit(void)
406 const struct cw1200_platform_data_sdio
*pdata
;
408 /* FIXME -- this won't support multiple devices */
409 pdata
= global_plat_data
;
410 sdio_unregister_driver(&sdio_driver
);
411 cw1200_sdio_off(pdata
);
415 module_init(cw1200_sdio_init
);
416 module_exit(cw1200_sdio_exit
);