wlcore: add device-tree support
[deliverable/linux.git] / drivers / net / wireless / ti / wlcore / sdio.c
CommitLineData
5129dffe
TP
1/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2009-2010 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/irq.h>
25#include <linux/module.h>
5129dffe 26#include <linux/vmalloc.h>
025aef8f 27#include <linux/platform_device.h>
197c6283 28#include <linux/mmc/sdio.h>
5129dffe
TP
29#include <linux/mmc/sdio_func.h>
30#include <linux/mmc/sdio_ids.h>
31#include <linux/mmc/card.h>
11251e7e 32#include <linux/mmc/host.h>
19b87173 33#include <linux/gpio.h>
09cecc34 34#include <linux/wl12xx.h>
00cbb3c5 35#include <linux/pm_runtime.h>
df472894 36#include <linux/printk.h>
5ea5c518
EP
37#include <linux/of.h>
38#include <linux/of_irq.h>
5129dffe 39
c31be25a 40#include "wlcore.h"
5129dffe 41#include "wl12xx_80211.h"
00d20100 42#include "io.h"
5129dffe 43
5129dffe
TP
44#ifndef SDIO_VENDOR_ID_TI
45#define SDIO_VENDOR_ID_TI 0x0097
46#endif
47
48#ifndef SDIO_DEVICE_ID_TI_WL1271
49#define SDIO_DEVICE_ID_TI_WL1271 0x4076
50#endif
51
df472894
LC
52static bool dump = false;
53
fbe936bc
FB
54struct wl12xx_sdio_glue {
55 struct device *dev;
025aef8f 56 struct platform_device *core;
fbe936bc
FB
57};
58
b74324d1 59static const struct sdio_device_id wl1271_devices[] = {
5129dffe
TP
60 { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
61 {}
62};
63MODULE_DEVICE_TABLE(sdio, wl1271_devices);
64
a390e85c
FB
65static void wl1271_sdio_set_block_size(struct device *child,
66 unsigned int blksz)
a81159ed 67{
a390e85c
FB
68 struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
69 struct sdio_func *func = dev_to_sdio_func(glue->dev);
5129dffe 70
a390e85c
FB
71 sdio_claim_host(func);
72 sdio_set_block_size(func, blksz);
73 sdio_release_host(func);
5129dffe
TP
74}
75
f1a26e63
IY
76static int __must_check wl12xx_sdio_raw_read(struct device *child, int addr,
77 void *buf, size_t len, bool fixed)
5129dffe
TP
78{
79 int ret;
a390e85c 80 struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
fbe936bc 81 struct sdio_func *func = dev_to_sdio_func(glue->dev);
5129dffe 82
b4748306
EP
83 sdio_claim_host(func);
84
df472894
LC
85 if (unlikely(dump)) {
86 printk(KERN_DEBUG "wlcore_sdio: READ from 0x%04x\n", addr);
87 print_hex_dump(KERN_DEBUG, "wlcore_sdio: READ ",
88 DUMP_PREFIX_OFFSET, 16, 1,
89 buf, len, false);
90 }
91
00782136 92 if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG)) {
5129dffe 93 ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
3c4d3868
LC
94 dev_dbg(child->parent, "sdio read 52 addr 0x%x, byte 0x%02x\n",
95 addr, ((u8 *)buf)[0]);
5129dffe
TP
96 } else {
97 if (fixed)
98 ret = sdio_readsb(func, buf, addr, len);
99 else
100 ret = sdio_memcpy_fromio(func, buf, addr, len);
101
3c4d3868
LC
102 dev_dbg(child->parent, "sdio read 53 addr 0x%x, %zu bytes\n",
103 addr, len);
5129dffe
TP
104 }
105
b4748306
EP
106 sdio_release_host(func);
107
02eb1d9d 108 if (WARN_ON(ret))
3c4d3868 109 dev_err(child->parent, "sdio read failed (%d)\n", ret);
02eb1d9d
IY
110
111 return ret;
5129dffe
TP
112}
113
f1a26e63
IY
114static int __must_check wl12xx_sdio_raw_write(struct device *child, int addr,
115 void *buf, size_t len, bool fixed)
5129dffe
TP
116{
117 int ret;
a390e85c 118 struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
fbe936bc 119 struct sdio_func *func = dev_to_sdio_func(glue->dev);
5129dffe 120
b4748306
EP
121 sdio_claim_host(func);
122
df472894
LC
123 if (unlikely(dump)) {
124 printk(KERN_DEBUG "wlcore_sdio: WRITE to 0x%04x\n", addr);
125 print_hex_dump(KERN_DEBUG, "wlcore_sdio: WRITE ",
126 DUMP_PREFIX_OFFSET, 16, 1,
127 buf, len, false);
128 }
129
00782136 130 if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG)) {
5129dffe 131 sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
3c4d3868
LC
132 dev_dbg(child->parent, "sdio write 52 addr 0x%x, byte 0x%02x\n",
133 addr, ((u8 *)buf)[0]);
5129dffe 134 } else {
3c4d3868
LC
135 dev_dbg(child->parent, "sdio write 53 addr 0x%x, %zu bytes\n",
136 addr, len);
5129dffe
TP
137
138 if (fixed)
139 ret = sdio_writesb(func, addr, buf, len);
140 else
141 ret = sdio_memcpy_toio(func, addr, buf, len);
142 }
49063a0d 143
b4748306
EP
144 sdio_release_host(func);
145
02eb1d9d 146 if (WARN_ON(ret))
3c4d3868 147 dev_err(child->parent, "sdio write failed (%d)\n", ret);
02eb1d9d
IY
148
149 return ret;
49063a0d
OBC
150}
151
a390e85c 152static int wl12xx_sdio_power_on(struct wl12xx_sdio_glue *glue)
49063a0d 153{
00cbb3c5 154 int ret;
fbe936bc 155 struct sdio_func *func = dev_to_sdio_func(glue->dev);
645865fc
IY
156 struct mmc_card *card = func->card;
157
158 ret = pm_runtime_get_sync(&card->dev);
159 if (ret) {
160 /*
161 * Runtime PM might be temporarily disabled, or the device
162 * might have a positive reference counter. Make sure it is
163 * really powered on.
164 */
165 ret = mmc_power_restore_host(card->host);
166 if (ret < 0) {
167 pm_runtime_put_sync(&card->dev);
b5d6e5f6 168 goto out;
645865fc 169 }
86046da4 170 }
5129dffe 171
49063a0d
OBC
172 sdio_claim_host(func);
173 sdio_enable_func(func);
b4748306 174 sdio_release_host(func);
2cc78ff7 175
00cbb3c5
OBC
176out:
177 return ret;
49063a0d
OBC
178}
179
a390e85c 180static int wl12xx_sdio_power_off(struct wl12xx_sdio_glue *glue)
49063a0d 181{
11251e7e 182 int ret;
fbe936bc 183 struct sdio_func *func = dev_to_sdio_func(glue->dev);
645865fc 184 struct mmc_card *card = func->card;
49063a0d 185
b4748306 186 sdio_claim_host(func);
49063a0d
OBC
187 sdio_disable_func(func);
188 sdio_release_host(func);
2cc78ff7 189
645865fc
IY
190 /* Power off the card manually in case it wasn't powered off above */
191 ret = mmc_power_save_host(card->host);
11251e7e 192 if (ret < 0)
645865fc 193 goto out;
11251e7e 194
645865fc
IY
195 /* Let runtime PM know the card is powered off */
196 pm_runtime_put_sync(&card->dev);
86046da4 197
645865fc 198out:
86046da4 199 return ret;
5129dffe
TP
200}
201
a390e85c 202static int wl12xx_sdio_set_power(struct device *child, bool enable)
becd551c 203{
a390e85c
FB
204 struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
205
49063a0d 206 if (enable)
a390e85c 207 return wl12xx_sdio_power_on(glue);
49063a0d 208 else
a390e85c 209 return wl12xx_sdio_power_off(glue);
becd551c
TP
210}
211
5129dffe 212static struct wl1271_if_operations sdio_ops = {
a390e85c
FB
213 .read = wl12xx_sdio_raw_read,
214 .write = wl12xx_sdio_raw_write,
215 .power = wl12xx_sdio_set_power,
a81159ed 216 .set_block_size = wl1271_sdio_set_block_size,
5129dffe
TP
217};
218
5ea5c518
EP
219#ifdef CONFIG_OF
220static const struct of_device_id wlcore_sdio_of_match_table[] = {
221 { .compatible = "ti,wl1271" },
222 { .compatible = "ti,wl1273" },
223 { .compatible = "ti,wl1281" },
224 { .compatible = "ti,wl1283" },
225 { .compatible = "ti,wl1801" },
226 { .compatible = "ti,wl1805" },
227 { .compatible = "ti,wl1807" },
228 { .compatible = "ti,wl1831" },
229 { .compatible = "ti,wl1835" },
230 { .compatible = "ti,wl1837" },
231 { }
232};
233
234static struct wl12xx_platform_data *wlcore_probe_of(struct device *dev)
235{
236 struct device_node *np = dev->of_node;
237 struct wl12xx_platform_data *pdata;
238
239 if (!np || !of_match_node(wlcore_sdio_of_match_table, np))
240 return NULL;
241
242 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
243 if (!pdata)
244 return NULL;
245
246 pdata->irq = irq_of_parse_and_map(np, 0);
247 if (!pdata->irq) {
248 dev_err(dev, "No irq in platform data\n");
249 kfree(pdata);
250 return NULL;
251 }
252
253 pdata->irq_trigger =
254 irqd_get_trigger_type(irq_get_irq_data(pdata->irq));
255
256 /* optional clock frequency params */
257 of_property_read_u32(np, "ref-clock-frequency",
258 &pdata->ref_clock_freq);
259 of_property_read_u32(np, "tcxo-clock-frequency",
260 &pdata->tcxo_clock_freq);
261
262 return pdata;
263}
264#else
265static struct wl12xx_platform_data *wlcore_probe_of(struct device *dev)
266{
267 return NULL;
268}
269#endif
270
271static struct wl12xx_platform_data *
272wlcore_get_platform_data(struct device *dev)
273{
274 struct wl12xx_platform_data *pdata;
275
276 /* first, look for DT data */
277 pdata = wlcore_probe_of(dev);
278 if (pdata)
279 return pdata;
280
281 /* if not found - fallback to static platform data */
282 pdata = wl12xx_get_platform_data();
283 if (!IS_ERR(pdata))
284 return kmemdup(pdata, sizeof(*pdata), GFP_KERNEL);
285
286 dev_err(dev, "No platform data set\n");
287 return NULL;
288}
289
290static void wlcore_del_platform_data(struct wl12xx_platform_data *pdata)
291{
292 kfree(pdata);
293}
294
b74324d1 295static int wl1271_probe(struct sdio_func *func,
5129dffe
TP
296 const struct sdio_device_id *id)
297{
4c104162 298 struct wlcore_platdev_data pdev_data;
fbe936bc 299 struct wl12xx_sdio_glue *glue;
025aef8f 300 struct resource res[1];
f795ea8b 301 mmc_pm_flag_t mmcflags;
fbe936bc 302 int ret = -ENOMEM;
197c6283 303 const char *chip_family;
5129dffe
TP
304
305 /* We are only able to handle the wlan function */
306 if (func->num != 0x02)
307 return -ENODEV;
308
4c104162
CE
309 memset(&pdev_data, 0x00, sizeof(pdev_data));
310 pdev_data.if_ops = &sdio_ops;
afb43e6d 311
fbe936bc
FB
312 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
313 if (!glue) {
3c4d3868 314 dev_err(&func->dev, "can't allocate glue\n");
4c104162 315 goto out;
fbe936bc
FB
316 }
317
fbe936bc 318 glue->dev = &func->dev;
5129dffe 319
5129dffe
TP
320 /* Grab access to FN0 for ELP reg. */
321 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
322
1d732e8c
AN
323 /* Use block mode for transferring over one block size of data */
324 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
325
5ea5c518
EP
326 pdev_data.pdata = wlcore_get_platform_data(&func->dev);
327 if (!pdev_data.pdata)
a390e85c 328 goto out_free_glue;
5129dffe 329
a390e85c
FB
330 /* if sdio can keep power while host is suspended, enable wow */
331 mmcflags = sdio_get_host_pm_caps(func);
3c4d3868 332 dev_dbg(glue->dev, "sdio PM caps = 0x%x\n", mmcflags);
2c0f2463 333
a390e85c 334 if (mmcflags & MMC_PM_KEEP_POWER)
4c104162 335 pdev_data.pdata->pwr_in_suspend = true;
f795ea8b 336
fbe936bc 337 sdio_set_drvdata(func, glue);
49d7f6d8 338
00cbb3c5
OBC
339 /* Tell PM core that we don't need the card to be powered now */
340 pm_runtime_put_noidle(&func->dev);
341
197c6283
LC
342 /*
343 * Due to a hardware bug, we can't differentiate wl18xx from
344 * wl12xx, because both report the same device ID. The only
345 * way to differentiate is by checking the SDIO revision,
346 * which is 3.00 on the wl18xx chips.
347 */
348 if (func->card->cccr.sdio_vsn == SDIO_SDIO_REV_3_00)
349 chip_family = "wl18xx";
350 else
351 chip_family = "wl12xx";
352
06ab4058 353 glue->core = platform_device_alloc(chip_family, PLATFORM_DEVID_AUTO);
025aef8f 354 if (!glue->core) {
3c4d3868 355 dev_err(glue->dev, "can't allocate platform_device");
025aef8f 356 ret = -ENOMEM;
5ea5c518 357 goto out_free_pdata;
025aef8f
FB
358 }
359
360 glue->core->dev.parent = &func->dev;
361
362 memset(res, 0x00, sizeof(res));
363
4c104162 364 res[0].start = pdev_data.pdata->irq;
6f921fab 365 res[0].flags = IORESOURCE_IRQ | pdev_data.pdata->irq_trigger;
025aef8f
FB
366 res[0].name = "irq";
367
368 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
369 if (ret) {
3c4d3868 370 dev_err(glue->dev, "can't add resources\n");
025aef8f
FB
371 goto out_dev_put;
372 }
373
4c104162
CE
374 ret = platform_device_add_data(glue->core, &pdev_data,
375 sizeof(pdev_data));
025aef8f 376 if (ret) {
3c4d3868 377 dev_err(glue->dev, "can't add platform data\n");
025aef8f
FB
378 goto out_dev_put;
379 }
380
381 ret = platform_device_add(glue->core);
382 if (ret) {
3c4d3868 383 dev_err(glue->dev, "can't add platform device\n");
025aef8f
FB
384 goto out_dev_put;
385 }
5129dffe
TP
386 return 0;
387
025aef8f
FB
388out_dev_put:
389 platform_device_put(glue->core);
390
5ea5c518
EP
391out_free_pdata:
392 wlcore_del_platform_data(pdev_data.pdata);
393
fbe936bc
FB
394out_free_glue:
395 kfree(glue);
a390e85c 396
fbe936bc 397out:
5129dffe
TP
398 return ret;
399}
400
b74324d1 401static void wl1271_remove(struct sdio_func *func)
5129dffe 402{
fbe936bc 403 struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
5ea5c518
EP
404 struct wlcore_platdev_data *pdev_data = glue->core->dev.platform_data;
405 struct wl12xx_platform_data *pdata = pdev_data->pdata;
5129dffe 406
00cbb3c5
OBC
407 /* Undo decrement done above in wl1271_probe */
408 pm_runtime_get_noresume(&func->dev);
409
200e9326 410 platform_device_unregister(glue->core);
5ea5c518 411 wlcore_del_platform_data(pdata);
fbe936bc 412 kfree(glue);
5129dffe
TP
413}
414
f634a4e7 415#ifdef CONFIG_PM
674f3058
OBC
416static int wl1271_suspend(struct device *dev)
417{
418 /* Tell MMC/SDIO core it's OK to power down the card
419 * (if it isn't already), but not to remove it completely */
039bdb14 420 struct sdio_func *func = dev_to_sdio_func(dev);
b6932894
ES
421 struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
422 struct wl1271 *wl = platform_get_drvdata(glue->core);
039bdb14
EP
423 mmc_pm_flag_t sdio_flags;
424 int ret = 0;
425
3c4d3868
LC
426 dev_dbg(dev, "wl1271 suspend. wow_enabled: %d\n",
427 wl->wow_enabled);
039bdb14
EP
428
429 /* check whether sdio should keep power */
430 if (wl->wow_enabled) {
431 sdio_flags = sdio_get_host_pm_caps(func);
432
433 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
3c4d3868
LC
434 dev_err(dev, "can't keep power while host "
435 "is suspended\n");
039bdb14
EP
436 ret = -EINVAL;
437 goto out;
438 }
439
440 /* keep power while host suspended */
441 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
442 if (ret) {
3c4d3868 443 dev_err(dev, "error while trying to keep power\n");
039bdb14
EP
444 goto out;
445 }
446 }
447out:
448 return ret;
674f3058
OBC
449}
450
451static int wl1271_resume(struct device *dev)
452{
3c4d3868 453 dev_dbg(dev, "wl1271 resume\n");
f44e5868 454
674f3058
OBC
455 return 0;
456}
457
458static const struct dev_pm_ops wl1271_sdio_pm_ops = {
459 .suspend = wl1271_suspend,
460 .resume = wl1271_resume,
461};
f634a4e7 462#endif
674f3058 463
5129dffe 464static struct sdio_driver wl1271_sdio_driver = {
f4b5d8d8 465 .name = "wl1271_sdio",
5129dffe
TP
466 .id_table = wl1271_devices,
467 .probe = wl1271_probe,
b74324d1 468 .remove = wl1271_remove,
f634a4e7 469#ifdef CONFIG_PM
674f3058
OBC
470 .drv = {
471 .pm = &wl1271_sdio_pm_ops,
472 },
f634a4e7 473#endif
5129dffe
TP
474};
475
476static int __init wl1271_init(void)
477{
6bdaf796 478 return sdio_register_driver(&wl1271_sdio_driver);
5129dffe
TP
479}
480
481static void __exit wl1271_exit(void)
482{
483 sdio_unregister_driver(&wl1271_sdio_driver);
5129dffe
TP
484}
485
486module_init(wl1271_init);
487module_exit(wl1271_exit);
488
df472894
LC
489module_param(dump, bool, S_IRUSR | S_IWUSR);
490MODULE_PARM_DESC(dump, "Enable sdio read/write dumps.");
491
5129dffe 492MODULE_LICENSE("GPL");
5245e3a9 493MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
5129dffe 494MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
This page took 0.579939 seconds and 5 git commands to generate.