WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / wireless / st / cw1200 / cw1200_sdio.c
blobb65ec14136c7ee7c1818710f78cab01b80a59669
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Mac80211 SDIO driver for ST-Ericsson CW1200 device
5 * Copyright (c) 2010, ST-Ericsson
6 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
7 */
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>
20 #include "cw1200.h"
21 #include "hwbus.h"
22 #include <linux/platform_data/net-cw1200.h>
23 #include "hwio.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 = {
33 .ref_clk = 38400,
34 .have_5ghz = false,
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;
46 struct hwbus_priv {
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,
60 unsigned int addr,
61 void *dst, int count)
63 return sdio_memcpy_fromio(self->func, dst, addr, count);
66 static int cw1200_sdio_memcpy_toio(struct hwbus_priv *self,
67 unsigned int addr,
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. */
88 if (self->core)
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;
101 if (self->core) {
102 cw1200_sdio_lock(self);
103 cw1200_irq_handler(self->core);
104 cw1200_sdio_unlock(self);
105 return IRQ_HANDLED;
106 } else {
107 return IRQ_NONE;
111 static int cw1200_request_irq(struct hwbus_priv *self)
113 int ret;
114 u8 cccr;
116 cccr = sdio_f0_readb(self->func, SDIO_CCCR_IENx, &ret);
117 if (WARN_ON(ret))
118 goto err;
120 /* Master interrupt enable ... */
121 cccr |= BIT(0);
123 /* ... for our function */
124 cccr |= BIT(self->func->num);
126 sdio_f0_writeb(self->func, cccr, SDIO_CCCR_IENx, &ret);
127 if (WARN_ON(ret))
128 goto err;
130 ret = enable_irq_wake(self->pdata->irq);
131 if (WARN_ON(ret))
132 goto err;
134 /* Request the IRQ */
135 ret = request_threaded_irq(self->pdata->irq, cw1200_gpio_hardirq,
136 cw1200_gpio_irq,
137 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
138 "cw1200_wlan_irq", self);
139 if (WARN_ON(ret))
140 goto err;
142 return 0;
144 err:
145 return ret;
148 static int cw1200_sdio_irq_subscribe(struct hwbus_priv *self)
150 int ret = 0;
152 pr_debug("SW IRQ subscribe\n");
153 sdio_claim_host(self->func);
154 if (self->pdata->irq)
155 ret = cw1200_request_irq(self);
156 else
157 ret = sdio_claim_irq(self->func, cw1200_sdio_irq_handler);
159 sdio_release_host(self->func);
160 return ret;
163 static int cw1200_sdio_irq_unsubscribe(struct hwbus_priv *self)
165 int ret = 0;
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);
172 } else {
173 sdio_claim_host(self->func);
174 ret = sdio_release_irq(self->func);
175 sdio_release_host(self->func);
177 return ret;
180 static int cw1200_sdio_off(const struct cw1200_platform_data_sdio *pdata)
182 if (pdata->reset) {
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);
190 if (pdata->clk_ctrl)
191 pdata->clk_ctrl(pdata, false);
193 return 0;
196 static int cw1200_sdio_on(const struct cw1200_platform_data_sdio *pdata)
198 /* Ensure I/Os are pulled low */
199 if (pdata->reset) {
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");
214 return -1;
218 /* Enable CLK32K */
219 if (pdata->clk_ctrl) {
220 if (pdata->clk_ctrl(pdata, true)) {
221 pr_err("clk_ctrl() failed!\n");
222 return -1;
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 */
233 if (pdata->reset) {
234 gpio_set_value(pdata->reset, 1);
235 msleep(50); /* Or more..? */
237 return 0;
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);
244 else
245 size = sdio_align_size(self->func, size);
247 return size;
250 static int cw1200_sdio_pm(struct hwbus_priv *self, bool suspend)
252 int ret = 0;
254 if (self->pdata->irq)
255 ret = irq_set_irq_wake(self->pdata->irq, suspend);
256 return ret;
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;
273 int status;
275 pr_info("cw1200_wlan_sdio: Probe called\n");
277 /* We are only able to handle the wlan function */
278 if (func->num != 0x01)
279 return -ENODEV;
281 self = kzalloc(sizeof(*self), GFP_KERNEL);
282 if (!self) {
283 pr_err("Can't allocate SDIO hwbus_priv.\n");
284 return -ENOMEM;
287 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
289 self->pdata = global_plat_data; /* FIXME */
290 self->func = func;
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);
304 if (status) {
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);
310 kfree(self);
313 return status;
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);
323 if (self) {
324 cw1200_sdio_irq_unsubscribe(self);
325 if (self->core) {
326 cw1200_core_release(self->core);
327 self->core = NULL;
329 sdio_claim_host(func);
330 sdio_disable_func(func);
331 sdio_release_host(func);
332 sdio_set_drvdata(func, NULL);
333 kfree(self);
337 #ifdef CONFIG_PM
338 static int cw1200_sdio_suspend(struct device *dev)
340 int ret;
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))
345 return -EAGAIN;
347 /* Notify SDIO that CW1200 will remain powered during suspend */
348 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
349 if (ret)
350 pr_err("Error setting SDIO pm flags: %i\n", ret);
352 return ret;
355 static int cw1200_sdio_resume(struct device *dev)
357 return 0;
360 static const struct dev_pm_ops cw1200_pm_ops = {
361 .suspend = cw1200_sdio_suspend,
362 .resume = cw1200_sdio_resume,
364 #endif
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,
371 #ifdef CONFIG_PM
372 .drv = {
373 .pm = &cw1200_pm_ops,
375 #endif
378 /* Init Module function -> Called by insmod */
379 static int __init cw1200_sdio_init(void)
381 const struct cw1200_platform_data_sdio *pdata;
382 int ret;
384 /* FIXME -- this won't support multiple devices */
385 pdata = global_plat_data;
387 if (cw1200_sdio_on(pdata)) {
388 ret = -1;
389 goto err;
392 ret = sdio_register_driver(&sdio_driver);
393 if (ret)
394 goto err;
396 return 0;
398 err:
399 cw1200_sdio_off(pdata);
400 return ret;
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);