Commit | Line | Data |
---|---|---|
a910e4a9 SP |
1 | /* |
2 | * Mac80211 SDIO driver for ST-Ericsson CW1200 device | |
3 | * | |
4 | * Copyright (c) 2010, ST-Ericsson | |
5 | * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no> | |
6 | * | |
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. | |
10 | */ | |
11 | ||
a910e4a9 SP |
12 | #include <linux/module.h> |
13 | #include <linux/gpio.h> | |
14 | #include <linux/delay.h> | |
15 | #include <linux/mmc/host.h> | |
16 | #include <linux/mmc/sdio_func.h> | |
17 | #include <linux/mmc/card.h> | |
18 | #include <linux/mmc/sdio.h> | |
19 | #include <net/mac80211.h> | |
20 | ||
21 | #include "cw1200.h" | |
911373cc | 22 | #include "hwbus.h" |
c9922198 | 23 | #include <linux/platform_data/cw1200_platform.h> |
a910e4a9 SP |
24 | #include "hwio.h" |
25 | ||
26 | MODULE_AUTHOR("Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>"); | |
27 | MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SDIO driver"); | |
28 | MODULE_LICENSE("GPL"); | |
29 | ||
30 | #define SDIO_BLOCK_SIZE (512) | |
31 | ||
911373cc | 32 | struct hwbus_priv { |
a910e4a9 SP |
33 | struct sdio_func *func; |
34 | struct cw1200_common *core; | |
35 | const struct cw1200_platform_data_sdio *pdata; | |
36 | }; | |
37 | ||
38 | #ifndef SDIO_VENDOR_ID_STE | |
39 | #define SDIO_VENDOR_ID_STE 0x0020 | |
40 | #endif | |
41 | ||
42 | #ifndef SDIO_DEVICE_ID_STE_CW1200 | |
43 | #define SDIO_DEVICE_ID_STE_CW1200 0x2280 | |
44 | #endif | |
45 | ||
46 | static const struct sdio_device_id cw1200_sdio_ids[] = { | |
47 | { SDIO_DEVICE(SDIO_VENDOR_ID_STE, SDIO_DEVICE_ID_STE_CW1200) }, | |
48 | { /* end: all zeroes */ }, | |
49 | }; | |
50 | ||
911373cc | 51 | /* hwbus_ops implemetation */ |
a910e4a9 | 52 | |
911373cc | 53 | static int cw1200_sdio_memcpy_fromio(struct hwbus_priv *self, |
a910e4a9 SP |
54 | unsigned int addr, |
55 | void *dst, int count) | |
56 | { | |
57 | return sdio_memcpy_fromio(self->func, dst, addr, count); | |
58 | } | |
59 | ||
911373cc | 60 | static int cw1200_sdio_memcpy_toio(struct hwbus_priv *self, |
a910e4a9 SP |
61 | unsigned int addr, |
62 | const void *src, int count) | |
63 | { | |
64 | return sdio_memcpy_toio(self->func, addr, (void *)src, count); | |
65 | } | |
66 | ||
911373cc | 67 | static void cw1200_sdio_lock(struct hwbus_priv *self) |
a910e4a9 SP |
68 | { |
69 | sdio_claim_host(self->func); | |
70 | } | |
71 | ||
911373cc | 72 | static void cw1200_sdio_unlock(struct hwbus_priv *self) |
a910e4a9 SP |
73 | { |
74 | sdio_release_host(self->func); | |
75 | } | |
76 | ||
77 | static void cw1200_sdio_irq_handler(struct sdio_func *func) | |
78 | { | |
911373cc | 79 | struct hwbus_priv *self = sdio_get_drvdata(func); |
a910e4a9 SP |
80 | |
81 | /* note: sdio_host already claimed here. */ | |
82 | if (self->core) | |
83 | cw1200_irq_handler(self->core); | |
84 | } | |
85 | ||
86 | static irqreturn_t cw1200_gpio_hardirq(int irq, void *dev_id) | |
87 | { | |
88 | return IRQ_WAKE_THREAD; | |
89 | } | |
90 | ||
91 | static irqreturn_t cw1200_gpio_irq(int irq, void *dev_id) | |
92 | { | |
911373cc | 93 | struct hwbus_priv *self = dev_id; |
a910e4a9 SP |
94 | |
95 | if (self->core) { | |
96 | sdio_claim_host(self->func); | |
97 | cw1200_irq_handler(self->core); | |
98 | sdio_release_host(self->func); | |
99 | return IRQ_HANDLED; | |
100 | } else { | |
101 | return IRQ_NONE; | |
102 | } | |
103 | } | |
104 | ||
911373cc | 105 | static int cw1200_request_irq(struct hwbus_priv *self) |
a910e4a9 SP |
106 | { |
107 | int ret; | |
108 | const struct resource *irq = self->pdata->irq; | |
109 | u8 cccr; | |
110 | ||
111 | cccr = sdio_f0_readb(self->func, SDIO_CCCR_IENx, &ret); | |
112 | if (WARN_ON(ret)) | |
113 | goto err; | |
114 | ||
115 | /* Master interrupt enable ... */ | |
116 | cccr |= BIT(0); | |
117 | ||
118 | /* ... for our function */ | |
119 | cccr |= BIT(self->func->num); | |
120 | ||
121 | sdio_f0_writeb(self->func, cccr, SDIO_CCCR_IENx, &ret); | |
122 | if (WARN_ON(ret)) | |
123 | goto err; | |
124 | ||
125 | ret = enable_irq_wake(irq->start); | |
126 | if (WARN_ON(ret)) | |
127 | goto err; | |
128 | ||
129 | /* Request the IRQ */ | |
130 | ret = request_threaded_irq(irq->start, cw1200_gpio_hardirq, | |
131 | cw1200_gpio_irq, | |
132 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, | |
133 | irq->name, self); | |
134 | if (WARN_ON(ret)) | |
135 | goto err; | |
136 | ||
137 | return 0; | |
138 | ||
139 | err: | |
140 | return ret; | |
141 | } | |
142 | ||
911373cc | 143 | static int cw1200_sdio_irq_subscribe(struct hwbus_priv *self) |
a910e4a9 SP |
144 | { |
145 | int ret = 0; | |
146 | ||
147 | pr_debug("SW IRQ subscribe\n"); | |
148 | sdio_claim_host(self->func); | |
149 | if (self->pdata->irq) | |
150 | ret = cw1200_request_irq(self); | |
151 | else | |
152 | ret = sdio_claim_irq(self->func, cw1200_sdio_irq_handler); | |
153 | ||
154 | sdio_release_host(self->func); | |
155 | return ret; | |
156 | } | |
157 | ||
911373cc | 158 | static int cw1200_sdio_irq_unsubscribe(struct hwbus_priv *self) |
a910e4a9 SP |
159 | { |
160 | int ret = 0; | |
161 | ||
162 | pr_debug("SW IRQ unsubscribe\n"); | |
163 | ||
164 | if (self->pdata->irq) { | |
165 | disable_irq_wake(self->pdata->irq->start); | |
166 | free_irq(self->pdata->irq->start, self); | |
167 | } else { | |
168 | sdio_claim_host(self->func); | |
169 | ret = sdio_release_irq(self->func); | |
170 | sdio_release_host(self->func); | |
171 | } | |
172 | return ret; | |
173 | } | |
174 | ||
175 | static int cw1200_sdio_off(const struct cw1200_platform_data_sdio *pdata) | |
176 | { | |
177 | const struct resource *reset = pdata->reset; | |
178 | ||
179 | if (reset) { | |
180 | gpio_set_value(reset->start, 0); | |
181 | msleep(30); /* Min is 2 * CLK32K cycles */ | |
182 | gpio_free(reset->start); | |
183 | } | |
184 | ||
185 | if (pdata->power_ctrl) | |
186 | pdata->power_ctrl(pdata, false); | |
187 | if (pdata->clk_ctrl) | |
188 | pdata->clk_ctrl(pdata, false); | |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
193 | static int cw1200_sdio_on(const struct cw1200_platform_data_sdio *pdata) | |
194 | { | |
195 | const struct resource *reset = pdata->reset; | |
7b19bc2c | 196 | const struct resource *powerup = pdata->powerup; |
a910e4a9 SP |
197 | |
198 | /* Ensure I/Os are pulled low */ | |
199 | if (reset) { | |
200 | gpio_request(reset->start, reset->name); | |
201 | gpio_direction_output(reset->start, 0); | |
202 | } | |
203 | if (powerup) { | |
204 | gpio_request(powerup->start, powerup->name); | |
205 | gpio_direction_output(powerup->start, 0); | |
206 | } | |
207 | if (reset || powerup) | |
208 | msleep(50); /* Settle time */ | |
209 | ||
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; | |
215 | } | |
216 | } | |
217 | ||
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; | |
223 | } | |
224 | msleep(10); /* Delay until clock is stable for 2 cycles */ | |
225 | } | |
226 | ||
227 | /* Enable POWERUP signal */ | |
228 | if (powerup) { | |
229 | gpio_set_value(powerup->start, 1); | |
230 | msleep(250); /* or more..? */ | |
231 | } | |
232 | /* Enable RSTn signal */ | |
233 | if (reset) { | |
234 | gpio_set_value(reset->start, 1); | |
235 | msleep(50); /* Or more..? */ | |
236 | } | |
237 | return 0; | |
238 | } | |
239 | ||
911373cc | 240 | static size_t cw1200_sdio_align_size(struct hwbus_priv *self, size_t size) |
a910e4a9 SP |
241 | { |
242 | if (self->pdata->no_nptb) | |
243 | size = round_up(size, SDIO_BLOCK_SIZE); | |
244 | else | |
245 | size = sdio_align_size(self->func, size); | |
246 | ||
247 | return size; | |
248 | } | |
249 | ||
911373cc | 250 | static int cw1200_sdio_pm(struct hwbus_priv *self, bool suspend) |
a910e4a9 SP |
251 | { |
252 | int ret = 0; | |
253 | ||
254 | if (self->pdata->irq) | |
255 | ret = irq_set_irq_wake(self->pdata->irq->start, suspend); | |
256 | return ret; | |
257 | } | |
258 | ||
911373cc SP |
259 | static struct hwbus_ops cw1200_sdio_hwbus_ops = { |
260 | .hwbus_memcpy_fromio = cw1200_sdio_memcpy_fromio, | |
261 | .hwbus_memcpy_toio = cw1200_sdio_memcpy_toio, | |
a910e4a9 SP |
262 | .lock = cw1200_sdio_lock, |
263 | .unlock = cw1200_sdio_unlock, | |
264 | .align_size = cw1200_sdio_align_size, | |
265 | .power_mgmt = cw1200_sdio_pm, | |
266 | }; | |
267 | ||
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) | |
271 | { | |
911373cc | 272 | struct hwbus_priv *self; |
a910e4a9 SP |
273 | int status; |
274 | ||
275 | pr_info("cw1200_wlan_sdio: Probe called\n"); | |
276 | ||
277 | /* We are only able to handle the wlan function */ | |
278 | if (func->num != 0x01) | |
279 | return -ENODEV; | |
280 | ||
281 | self = kzalloc(sizeof(*self), GFP_KERNEL); | |
282 | if (!self) { | |
911373cc | 283 | pr_err("Can't allocate SDIO hwbus_priv.\n"); |
a910e4a9 SP |
284 | return -ENOMEM; |
285 | } | |
286 | ||
287 | func->card->quirks |= MMC_QUIRK_LENIENT_FN0; | |
288 | ||
289 | self->pdata = cw1200_get_platform_data(); | |
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); | |
295 | ||
296 | status = cw1200_sdio_irq_subscribe(self); | |
297 | ||
911373cc | 298 | status = cw1200_core_probe(&cw1200_sdio_hwbus_ops, |
a910e4a9 SP |
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); | |
311 | } | |
312 | ||
313 | return status; | |
314 | } | |
315 | ||
316 | /* Disconnect Function to be called by SDIO stack when | |
317 | * device is disconnected */ | |
318 | static void cw1200_sdio_disconnect(struct sdio_func *func) | |
319 | { | |
911373cc | 320 | struct hwbus_priv *self = sdio_get_drvdata(func); |
a910e4a9 SP |
321 | |
322 | if (self) { | |
323 | cw1200_sdio_irq_unsubscribe(self); | |
324 | if (self->core) { | |
325 | cw1200_core_release(self->core); | |
326 | self->core = NULL; | |
327 | } | |
328 | sdio_claim_host(func); | |
329 | sdio_disable_func(func); | |
330 | sdio_release_host(func); | |
331 | sdio_set_drvdata(func, NULL); | |
332 | kfree(self); | |
333 | } | |
334 | } | |
335 | ||
4e17b87e | 336 | #ifdef CONFIG_PM |
a910e4a9 SP |
337 | static int cw1200_sdio_suspend(struct device *dev) |
338 | { | |
339 | int ret; | |
340 | struct sdio_func *func = dev_to_sdio_func(dev); | |
911373cc | 341 | struct hwbus_priv *self = sdio_get_drvdata(func); |
a910e4a9 SP |
342 | |
343 | if (!cw1200_can_suspend(self->core)) | |
344 | return -EAGAIN; | |
345 | ||
346 | /* Notify SDIO that CW1200 will remain powered during suspend */ | |
347 | ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER); | |
348 | if (ret) | |
349 | pr_err("Error setting SDIO pm flags: %i\n", ret); | |
350 | ||
351 | return ret; | |
352 | } | |
353 | ||
354 | static int cw1200_sdio_resume(struct device *dev) | |
355 | { | |
356 | return 0; | |
357 | } | |
358 | ||
359 | static const struct dev_pm_ops cw1200_pm_ops = { | |
360 | .suspend = cw1200_sdio_suspend, | |
361 | .resume = cw1200_sdio_resume, | |
362 | }; | |
4e17b87e | 363 | #endif |
a910e4a9 SP |
364 | |
365 | static struct sdio_driver sdio_driver = { | |
366 | .name = "cw1200_wlan_sdio", | |
367 | .id_table = cw1200_sdio_ids, | |
368 | .probe = cw1200_sdio_probe, | |
369 | .remove = cw1200_sdio_disconnect, | |
4e17b87e | 370 | #ifdef CONFIG_PM |
a910e4a9 SP |
371 | .drv = { |
372 | .pm = &cw1200_pm_ops, | |
373 | } | |
4e17b87e | 374 | #endif |
a910e4a9 SP |
375 | }; |
376 | ||
377 | /* Init Module function -> Called by insmod */ | |
378 | static int __init cw1200_sdio_init(void) | |
379 | { | |
380 | const struct cw1200_platform_data_sdio *pdata; | |
381 | int ret; | |
382 | ||
383 | pdata = cw1200_get_platform_data(); | |
384 | ||
385 | if (cw1200_sdio_on(pdata)) { | |
386 | ret = -1; | |
387 | goto err; | |
388 | } | |
389 | ||
390 | ret = sdio_register_driver(&sdio_driver); | |
391 | if (ret) | |
392 | goto err; | |
393 | ||
394 | return 0; | |
395 | ||
396 | err: | |
397 | cw1200_sdio_off(pdata); | |
398 | return ret; | |
399 | } | |
400 | ||
401 | /* Called at Driver Unloading */ | |
402 | static void __exit cw1200_sdio_exit(void) | |
403 | { | |
404 | const struct cw1200_platform_data_sdio *pdata; | |
405 | pdata = cw1200_get_platform_data(); | |
406 | sdio_unregister_driver(&sdio_driver); | |
407 | cw1200_sdio_off(pdata); | |
408 | } | |
409 | ||
410 | ||
411 | module_init(cw1200_sdio_init); | |
412 | module_exit(cw1200_sdio_exit); |