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 <net/mac80211.h>
21 #include <linux/platform_data/net-cw1200.h>
24 MODULE_AUTHOR("Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>");
25 MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SDIO driver");
26 MODULE_LICENSE("GPL");
28 #define SDIO_BLOCK_SIZE (512)
30 /* Default platform data for Sagrad modules */
31 static struct cw1200_platform_data_sdio sagrad_109x_evk_platform_data
= {
34 .sdd_file
= "sdd_sagrad_1091_1098.bin",
37 /* Allow platform data to be overridden */
38 static struct cw1200_platform_data_sdio
*global_plat_data
= &sagrad_109x_evk_platform_data
;
40 void __init
cw1200_sdio_set_platform_data(struct cw1200_platform_data_sdio
*pdata
)
42 global_plat_data
= pdata
;
46 struct sdio_func
*func
;
47 struct cw1200_common
*core
;
48 const struct cw1200_platform_data_sdio
*pdata
;
51 #ifndef SDIO_VENDOR_ID_STE
52 #define SDIO_VENDOR_ID_STE 0x0020
55 #ifndef SDIO_DEVICE_ID_STE_CW1200
56 #define SDIO_DEVICE_ID_STE_CW1200 0x2280
59 static const struct sdio_device_id cw1200_sdio_ids
[] = {
60 { SDIO_DEVICE(SDIO_VENDOR_ID_STE
, SDIO_DEVICE_ID_STE_CW1200
) },
61 { /* end: all zeroes */ },
64 /* hwbus_ops implemetation */
66 static int cw1200_sdio_memcpy_fromio(struct hwbus_priv
*self
,
70 return sdio_memcpy_fromio(self
->func
, dst
, addr
, count
);
73 static int cw1200_sdio_memcpy_toio(struct hwbus_priv
*self
,
75 const void *src
, int count
)
77 return sdio_memcpy_toio(self
->func
, addr
, (void *)src
, count
);
80 static void cw1200_sdio_lock(struct hwbus_priv
*self
)
82 sdio_claim_host(self
->func
);
85 static void cw1200_sdio_unlock(struct hwbus_priv
*self
)
87 sdio_release_host(self
->func
);
90 static void cw1200_sdio_irq_handler(struct sdio_func
*func
)
92 struct hwbus_priv
*self
= sdio_get_drvdata(func
);
94 /* note: sdio_host already claimed here. */
96 cw1200_irq_handler(self
->core
);
99 static irqreturn_t
cw1200_gpio_hardirq(int irq
, void *dev_id
)
101 return IRQ_WAKE_THREAD
;
104 static irqreturn_t
cw1200_gpio_irq(int irq
, void *dev_id
)
106 struct hwbus_priv
*self
= dev_id
;
109 cw1200_sdio_lock(self
);
110 cw1200_irq_handler(self
->core
);
111 cw1200_sdio_unlock(self
);
118 static int cw1200_request_irq(struct hwbus_priv
*self
)
123 cccr
= sdio_f0_readb(self
->func
, SDIO_CCCR_IENx
, &ret
);
127 /* Master interrupt enable ... */
130 /* ... for our function */
131 cccr
|= BIT(self
->func
->num
);
133 sdio_f0_writeb(self
->func
, cccr
, SDIO_CCCR_IENx
, &ret
);
137 ret
= enable_irq_wake(self
->pdata
->irq
);
141 /* Request the IRQ */
142 ret
= request_threaded_irq(self
->pdata
->irq
, cw1200_gpio_hardirq
,
144 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
145 "cw1200_wlan_irq", self
);
155 static int cw1200_sdio_irq_subscribe(struct hwbus_priv
*self
)
159 pr_debug("SW IRQ subscribe\n");
160 sdio_claim_host(self
->func
);
161 if (self
->pdata
->irq
)
162 ret
= cw1200_request_irq(self
);
164 ret
= sdio_claim_irq(self
->func
, cw1200_sdio_irq_handler
);
166 sdio_release_host(self
->func
);
170 static int cw1200_sdio_irq_unsubscribe(struct hwbus_priv
*self
)
174 pr_debug("SW IRQ unsubscribe\n");
176 if (self
->pdata
->irq
) {
177 disable_irq_wake(self
->pdata
->irq
);
178 free_irq(self
->pdata
->irq
, self
);
180 sdio_claim_host(self
->func
);
181 ret
= sdio_release_irq(self
->func
);
182 sdio_release_host(self
->func
);
187 static int cw1200_sdio_off(const struct cw1200_platform_data_sdio
*pdata
)
190 gpio_set_value(pdata
->reset
, 0);
191 msleep(30); /* Min is 2 * CLK32K cycles */
192 gpio_free(pdata
->reset
);
195 if (pdata
->power_ctrl
)
196 pdata
->power_ctrl(pdata
, false);
198 pdata
->clk_ctrl(pdata
, false);
203 static int cw1200_sdio_on(const struct cw1200_platform_data_sdio
*pdata
)
205 /* Ensure I/Os are pulled low */
207 gpio_request(pdata
->reset
, "cw1200_wlan_reset");
208 gpio_direction_output(pdata
->reset
, 0);
210 if (pdata
->powerup
) {
211 gpio_request(pdata
->powerup
, "cw1200_wlan_powerup");
212 gpio_direction_output(pdata
->powerup
, 0);
214 if (pdata
->reset
|| pdata
->powerup
)
215 msleep(10); /* Settle time? */
217 /* Enable 3v3 and 1v8 to hardware */
218 if (pdata
->power_ctrl
) {
219 if (pdata
->power_ctrl(pdata
, true)) {
220 pr_err("power_ctrl() failed!\n");
226 if (pdata
->clk_ctrl
) {
227 if (pdata
->clk_ctrl(pdata
, true)) {
228 pr_err("clk_ctrl() failed!\n");
231 msleep(10); /* Delay until clock is stable for 2 cycles */
234 /* Enable POWERUP signal */
235 if (pdata
->powerup
) {
236 gpio_set_value(pdata
->powerup
, 1);
237 msleep(250); /* or more..? */
239 /* Enable RSTn signal */
241 gpio_set_value(pdata
->reset
, 1);
242 msleep(50); /* Or more..? */
247 static size_t cw1200_sdio_align_size(struct hwbus_priv
*self
, size_t size
)
249 if (self
->pdata
->no_nptb
)
250 size
= round_up(size
, SDIO_BLOCK_SIZE
);
252 size
= sdio_align_size(self
->func
, size
);
257 static int cw1200_sdio_pm(struct hwbus_priv
*self
, bool suspend
)
261 if (self
->pdata
->irq
)
262 ret
= irq_set_irq_wake(self
->pdata
->irq
, suspend
);
266 static const struct hwbus_ops cw1200_sdio_hwbus_ops
= {
267 .hwbus_memcpy_fromio
= cw1200_sdio_memcpy_fromio
,
268 .hwbus_memcpy_toio
= cw1200_sdio_memcpy_toio
,
269 .lock
= cw1200_sdio_lock
,
270 .unlock
= cw1200_sdio_unlock
,
271 .align_size
= cw1200_sdio_align_size
,
272 .power_mgmt
= cw1200_sdio_pm
,
275 /* Probe Function to be called by SDIO stack when device is discovered */
276 static int cw1200_sdio_probe(struct sdio_func
*func
,
277 const struct sdio_device_id
*id
)
279 struct hwbus_priv
*self
;
282 pr_info("cw1200_wlan_sdio: Probe called\n");
284 /* We are only able to handle the wlan function */
285 if (func
->num
!= 0x01)
288 self
= kzalloc(sizeof(*self
), GFP_KERNEL
);
290 pr_err("Can't allocate SDIO hwbus_priv.\n");
294 func
->card
->quirks
|= MMC_QUIRK_LENIENT_FN0
;
296 self
->pdata
= global_plat_data
; /* FIXME */
298 sdio_set_drvdata(func
, self
);
299 sdio_claim_host(func
);
300 sdio_enable_func(func
);
301 sdio_release_host(func
);
303 status
= cw1200_sdio_irq_subscribe(self
);
305 status
= cw1200_core_probe(&cw1200_sdio_hwbus_ops
,
306 self
, &func
->dev
, &self
->core
,
307 self
->pdata
->ref_clk
,
308 self
->pdata
->macaddr
,
309 self
->pdata
->sdd_file
,
310 self
->pdata
->have_5ghz
);
312 cw1200_sdio_irq_unsubscribe(self
);
313 sdio_claim_host(func
);
314 sdio_disable_func(func
);
315 sdio_release_host(func
);
316 sdio_set_drvdata(func
, NULL
);
323 /* Disconnect Function to be called by SDIO stack when
324 * device is disconnected
326 static void cw1200_sdio_disconnect(struct sdio_func
*func
)
328 struct hwbus_priv
*self
= sdio_get_drvdata(func
);
331 cw1200_sdio_irq_unsubscribe(self
);
333 cw1200_core_release(self
->core
);
336 sdio_claim_host(func
);
337 sdio_disable_func(func
);
338 sdio_release_host(func
);
339 sdio_set_drvdata(func
, NULL
);
345 static int cw1200_sdio_suspend(struct device
*dev
)
348 struct sdio_func
*func
= dev_to_sdio_func(dev
);
349 struct hwbus_priv
*self
= sdio_get_drvdata(func
);
351 if (!cw1200_can_suspend(self
->core
))
354 /* Notify SDIO that CW1200 will remain powered during suspend */
355 ret
= sdio_set_host_pm_flags(func
, MMC_PM_KEEP_POWER
);
357 pr_err("Error setting SDIO pm flags: %i\n", ret
);
362 static int cw1200_sdio_resume(struct device
*dev
)
367 static const struct dev_pm_ops cw1200_pm_ops
= {
368 .suspend
= cw1200_sdio_suspend
,
369 .resume
= cw1200_sdio_resume
,
373 static struct sdio_driver sdio_driver
= {
374 .name
= "cw1200_wlan_sdio",
375 .id_table
= cw1200_sdio_ids
,
376 .probe
= cw1200_sdio_probe
,
377 .remove
= cw1200_sdio_disconnect
,
380 .pm
= &cw1200_pm_ops
,
385 /* Init Module function -> Called by insmod */
386 static int __init
cw1200_sdio_init(void)
388 const struct cw1200_platform_data_sdio
*pdata
;
391 /* FIXME -- this won't support multiple devices */
392 pdata
= global_plat_data
;
394 if (cw1200_sdio_on(pdata
)) {
399 ret
= sdio_register_driver(&sdio_driver
);
406 cw1200_sdio_off(pdata
);
410 /* Called at Driver Unloading */
411 static void __exit
cw1200_sdio_exit(void)
413 const struct cw1200_platform_data_sdio
*pdata
;
415 /* FIXME -- this won't support multiple devices */
416 pdata
= global_plat_data
;
417 sdio_unregister_driver(&sdio_driver
);
418 cw1200_sdio_off(pdata
);
422 module_init(cw1200_sdio_init
);
423 module_exit(cw1200_sdio_exit
);