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