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 = "/*(DEBLOBBED)*/",
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 */ },
56 MODULE_DEVICE_TABLE(sdio, cw1200_sdio_ids);
58 /* hwbus_ops implemetation */
60 static int cw1200_sdio_memcpy_fromio(struct hwbus_priv *self,
64 return sdio_memcpy_fromio(self->func, dst, addr, count);
67 static int cw1200_sdio_memcpy_toio(struct hwbus_priv *self,
69 const void *src, int count)
71 return sdio_memcpy_toio(self->func, addr, (void *)src, count);
74 static void cw1200_sdio_lock(struct hwbus_priv *self)
76 sdio_claim_host(self->func);
79 static void cw1200_sdio_unlock(struct hwbus_priv *self)
81 sdio_release_host(self->func);
84 static void cw1200_sdio_irq_handler(struct sdio_func *func)
86 struct hwbus_priv *self = sdio_get_drvdata(func);
88 /* note: sdio_host already claimed here. */
90 cw1200_irq_handler(self->core);
93 static irqreturn_t cw1200_gpio_hardirq(int irq, void *dev_id)
95 return IRQ_WAKE_THREAD;
98 static irqreturn_t cw1200_gpio_irq(int irq, void *dev_id)
100 struct hwbus_priv *self = dev_id;
103 cw1200_sdio_lock(self);
104 cw1200_irq_handler(self->core);
105 cw1200_sdio_unlock(self);
112 static int cw1200_request_irq(struct hwbus_priv *self)
117 cccr = sdio_f0_readb(self->func, SDIO_CCCR_IENx, &ret);
121 /* Master interrupt enable ... */
124 /* ... for our function */
125 cccr |= BIT(self->func->num);
127 sdio_f0_writeb(self->func, cccr, SDIO_CCCR_IENx, &ret);
131 ret = enable_irq_wake(self->pdata->irq);
135 /* Request the IRQ */
136 ret = request_threaded_irq(self->pdata->irq, cw1200_gpio_hardirq,
138 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
139 "cw1200_wlan_irq", self);
149 static int cw1200_sdio_irq_subscribe(struct hwbus_priv *self)
153 pr_debug("SW IRQ subscribe\n");
154 sdio_claim_host(self->func);
155 if (self->pdata->irq)
156 ret = cw1200_request_irq(self);
158 ret = sdio_claim_irq(self->func, cw1200_sdio_irq_handler);
160 sdio_release_host(self->func);
164 static int cw1200_sdio_irq_unsubscribe(struct hwbus_priv *self)
168 pr_debug("SW IRQ unsubscribe\n");
170 if (self->pdata->irq) {
171 disable_irq_wake(self->pdata->irq);
172 free_irq(self->pdata->irq, self);
174 sdio_claim_host(self->func);
175 ret = sdio_release_irq(self->func);
176 sdio_release_host(self->func);
181 static int cw1200_sdio_off(const struct cw1200_platform_data_sdio *pdata)
184 gpio_set_value(pdata->reset, 0);
185 msleep(30); /* Min is 2 * CLK32K cycles */
186 gpio_free(pdata->reset);
189 if (pdata->power_ctrl)
190 pdata->power_ctrl(pdata, false);
192 pdata->clk_ctrl(pdata, false);
197 static int cw1200_sdio_on(const struct cw1200_platform_data_sdio *pdata)
199 /* Ensure I/Os are pulled low */
201 gpio_request(pdata->reset, "cw1200_wlan_reset");
202 gpio_direction_output(pdata->reset, 0);
204 if (pdata->powerup) {
205 gpio_request(pdata->powerup, "cw1200_wlan_powerup");
206 gpio_direction_output(pdata->powerup, 0);
208 if (pdata->reset || pdata->powerup)
209 msleep(10); /* Settle time? */
211 /* Enable 3v3 and 1v8 to hardware */
212 if (pdata->power_ctrl) {
213 if (pdata->power_ctrl(pdata, true)) {
214 pr_err("power_ctrl() failed!\n");
220 if (pdata->clk_ctrl) {
221 if (pdata->clk_ctrl(pdata, true)) {
222 pr_err("clk_ctrl() failed!\n");
225 msleep(10); /* Delay until clock is stable for 2 cycles */
228 /* Enable POWERUP signal */
229 if (pdata->powerup) {
230 gpio_set_value(pdata->powerup, 1);
231 msleep(250); /* or more..? */
233 /* Enable RSTn signal */
235 gpio_set_value(pdata->reset, 1);
236 msleep(50); /* Or more..? */
241 static size_t cw1200_sdio_align_size(struct hwbus_priv *self, size_t size)
243 if (self->pdata->no_nptb)
244 size = round_up(size, SDIO_BLOCK_SIZE);
246 size = sdio_align_size(self->func, size);
251 static int cw1200_sdio_pm(struct hwbus_priv *self, bool suspend)
255 if (self->pdata->irq)
256 ret = irq_set_irq_wake(self->pdata->irq, suspend);
260 static const struct hwbus_ops cw1200_sdio_hwbus_ops = {
261 .hwbus_memcpy_fromio = cw1200_sdio_memcpy_fromio,
262 .hwbus_memcpy_toio = cw1200_sdio_memcpy_toio,
263 .lock = cw1200_sdio_lock,
264 .unlock = cw1200_sdio_unlock,
265 .align_size = cw1200_sdio_align_size,
266 .power_mgmt = cw1200_sdio_pm,
269 /* Probe Function to be called by SDIO stack when device is discovered */
270 static int cw1200_sdio_probe(struct sdio_func *func,
271 const struct sdio_device_id *id)
273 struct hwbus_priv *self;
276 pr_info("cw1200_wlan_sdio: Probe called\n");
278 /* We are only able to handle the wlan function */
279 if (func->num != 0x01)
282 self = kzalloc(sizeof(*self), GFP_KERNEL);
284 pr_err("Can't allocate SDIO hwbus_priv.\n");
288 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
290 self->pdata = global_plat_data; /* FIXME */
292 sdio_set_drvdata(func, self);
293 sdio_claim_host(func);
294 sdio_enable_func(func);
295 sdio_release_host(func);
297 status = cw1200_sdio_irq_subscribe(self);
299 status = cw1200_core_probe(&cw1200_sdio_hwbus_ops,
300 self, &func->dev, &self->core,
301 self->pdata->ref_clk,
302 self->pdata->macaddr,
303 self->pdata->sdd_file,
304 self->pdata->have_5ghz);
306 cw1200_sdio_irq_unsubscribe(self);
307 sdio_claim_host(func);
308 sdio_disable_func(func);
309 sdio_release_host(func);
310 sdio_set_drvdata(func, NULL);
317 /* Disconnect Function to be called by SDIO stack when
318 * device is disconnected
320 static void cw1200_sdio_disconnect(struct sdio_func *func)
322 struct hwbus_priv *self = sdio_get_drvdata(func);
325 cw1200_sdio_irq_unsubscribe(self);
327 cw1200_core_release(self->core);
330 sdio_claim_host(func);
331 sdio_disable_func(func);
332 sdio_release_host(func);
333 sdio_set_drvdata(func, NULL);
339 static int cw1200_sdio_suspend(struct device *dev)
342 struct sdio_func *func = dev_to_sdio_func(dev);
343 struct hwbus_priv *self = sdio_get_drvdata(func);
345 if (!cw1200_can_suspend(self->core))
348 /* Notify SDIO that CW1200 will remain powered during suspend */
349 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
351 pr_err("Error setting SDIO pm flags: %i\n", ret);
356 static int cw1200_sdio_resume(struct device *dev)
361 static const struct dev_pm_ops cw1200_pm_ops = {
362 .suspend = cw1200_sdio_suspend,
363 .resume = cw1200_sdio_resume,
367 static struct sdio_driver sdio_driver = {
368 .name = "cw1200_wlan_sdio",
369 .id_table = cw1200_sdio_ids,
370 .probe = cw1200_sdio_probe,
371 .remove = cw1200_sdio_disconnect,
374 .pm = &cw1200_pm_ops,
379 /* Init Module function -> Called by insmod */
380 static int __init cw1200_sdio_init(void)
382 const struct cw1200_platform_data_sdio *pdata;
385 /* FIXME -- this won't support multiple devices */
386 pdata = global_plat_data;
388 if (cw1200_sdio_on(pdata)) {
393 ret = sdio_register_driver(&sdio_driver);
400 cw1200_sdio_off(pdata);
404 /* Called at Driver Unloading */
405 static void __exit cw1200_sdio_exit(void)
407 const struct cw1200_platform_data_sdio *pdata;
409 /* FIXME -- this won't support multiple devices */
410 pdata = global_plat_data;
411 sdio_unregister_driver(&sdio_driver);
412 cw1200_sdio_off(pdata);
416 module_init(cw1200_sdio_init);
417 module_exit(cw1200_sdio_exit);