sdio.c 9.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * This file is part of wl1271
 *
 * Copyright (C) 2009-2010 Nokia Corporation
 *
 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/irq.h>
#include <linux/module.h>
#include <linux/vmalloc.h>
27
#include <linux/platform_device.h>
28 29 30
#include <linux/mmc/sdio_func.h>
#include <linux/mmc/sdio_ids.h>
#include <linux/mmc/card.h>
31
#include <linux/mmc/host.h>
32
#include <linux/gpio.h>
33
#include <linux/wl12xx.h>
O
Ohad Ben-Cohen 已提交
34
#include <linux/pm_runtime.h>
35

S
Shahar Levi 已提交
36
#include "wl12xx.h"
37
#include "wl12xx_80211.h"
S
Shahar Levi 已提交
38
#include "io.h"
39 40 41 42 43 44 45 46 47

#ifndef SDIO_VENDOR_ID_TI
#define SDIO_VENDOR_ID_TI		0x0097
#endif

#ifndef SDIO_DEVICE_ID_TI_WL1271
#define SDIO_DEVICE_ID_TI_WL1271	0x4076
#endif

48 49
struct wl12xx_sdio_glue {
	struct device *dev;
50
	struct platform_device *core;
51 52
};

53
static const struct sdio_device_id wl1271_devices[] __devinitconst = {
54 55 56 57 58
	{ SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
	{}
};
MODULE_DEVICE_TABLE(sdio, wl1271_devices);

59 60
static void wl1271_sdio_set_block_size(struct device *child,
				       unsigned int blksz)
61
{
62 63
	struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
	struct sdio_func *func = dev_to_sdio_func(glue->dev);
64

65 66 67
	sdio_claim_host(func);
	sdio_set_block_size(func, blksz);
	sdio_release_host(func);
68 69
}

70
static void wl12xx_sdio_raw_read(struct device *child, int addr, void *buf,
T
Teemu Paasikivi 已提交
71
				 size_t len, bool fixed)
72 73
{
	int ret;
74
	struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
75
	struct sdio_func *func = dev_to_sdio_func(glue->dev);
76

77 78
	sdio_claim_host(func);

79 80
	if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
		((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
81 82
		dev_dbg(child->parent, "sdio read 52 addr 0x%x, byte 0x%02x\n",
			addr, ((u8 *)buf)[0]);
83 84 85 86 87 88
	} else {
		if (fixed)
			ret = sdio_readsb(func, buf, addr, len);
		else
			ret = sdio_memcpy_fromio(func, buf, addr, len);

89 90
		dev_dbg(child->parent, "sdio read 53 addr 0x%x, %zu bytes\n",
			addr, len);
91 92
	}

93 94
	sdio_release_host(func);

95
	if (ret)
96
		dev_err(child->parent, "sdio read failed (%d)\n", ret);
97 98
}

99
static void wl12xx_sdio_raw_write(struct device *child, int addr, void *buf,
T
Teemu Paasikivi 已提交
100
				  size_t len, bool fixed)
101 102
{
	int ret;
103
	struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
104
	struct sdio_func *func = dev_to_sdio_func(glue->dev);
105

106 107
	sdio_claim_host(func);

108 109
	if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
		sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
110 111
		dev_dbg(child->parent, "sdio write 52 addr 0x%x, byte 0x%02x\n",
			addr, ((u8 *)buf)[0]);
112
	} else {
113 114
		dev_dbg(child->parent, "sdio write 53 addr 0x%x, %zu bytes\n",
			addr, len);
115 116 117 118 119 120

		if (fixed)
			ret = sdio_writesb(func, addr, buf, len);
		else
			ret = sdio_memcpy_toio(func, addr, buf, len);
	}
121

122 123
	sdio_release_host(func);

124
	if (ret)
125
		dev_err(child->parent, "sdio write failed (%d)\n", ret);
126 127
}

128
static int wl12xx_sdio_power_on(struct wl12xx_sdio_glue *glue)
129
{
O
Ohad Ben-Cohen 已提交
130
	int ret;
131
	struct sdio_func *func = dev_to_sdio_func(glue->dev);
O
Ohad Ben-Cohen 已提交
132

133 134 135
	/* If enabled, tell runtime PM not to power off the card */
	if (pm_runtime_enabled(&func->dev)) {
		ret = pm_runtime_get_sync(&func->dev);
136
		if (ret < 0)
137
			goto out;
138 139 140 141 142
	} else {
		/* Runtime PM is disabled: power up the card manually */
		ret = mmc_power_restore_host(func->card->host);
		if (ret < 0)
			goto out;
143
	}
144

145 146
	sdio_claim_host(func);
	sdio_enable_func(func);
147
	sdio_release_host(func);
148

O
Ohad Ben-Cohen 已提交
149 150
out:
	return ret;
151 152
}

153
static int wl12xx_sdio_power_off(struct wl12xx_sdio_glue *glue)
154
{
155
	int ret;
156
	struct sdio_func *func = dev_to_sdio_func(glue->dev);
157

158
	sdio_claim_host(func);
159 160
	sdio_disable_func(func);
	sdio_release_host(func);
161

162
	/* Power off the card manually, even if runtime PM is enabled. */
163 164 165 166
	ret = mmc_power_save_host(func->card->host);
	if (ret < 0)
		return ret;

167 168 169 170 171
	/* If enabled, let runtime PM know the card is powered off */
	if (pm_runtime_enabled(&func->dev))
		ret = pm_runtime_put_sync(&func->dev);

	return ret;
172 173
}

174
static int wl12xx_sdio_set_power(struct device *child, bool enable)
175
{
176 177
	struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);

178
	if (enable)
179
		return wl12xx_sdio_power_on(glue);
180
	else
181
		return wl12xx_sdio_power_off(glue);
182 183
}

184
static struct wl1271_if_operations sdio_ops = {
185 186 187
	.read		= wl12xx_sdio_raw_read,
	.write		= wl12xx_sdio_raw_write,
	.power		= wl12xx_sdio_set_power,
188
	.set_block_size = wl1271_sdio_set_block_size,
189 190 191 192 193
};

static int __devinit wl1271_probe(struct sdio_func *func,
				  const struct sdio_device_id *id)
{
194
	struct wl12xx_platform_data *wlan_data;
195
	struct wl12xx_sdio_glue *glue;
196
	struct resource res[1];
197
	mmc_pm_flag_t mmcflags;
198
	int ret = -ENOMEM;
199 200 201 202 203

	/* We are only able to handle the wlan function */
	if (func->num != 0x02)
		return -ENODEV;

204 205
	glue = kzalloc(sizeof(*glue), GFP_KERNEL);
	if (!glue) {
206
		dev_err(&func->dev, "can't allocate glue\n");
207 208 209 210
		goto out;
	}

	glue->dev = &func->dev;
211 212 213 214

	/* Grab access to FN0 for ELP reg. */
	func->card->quirks |= MMC_QUIRK_LENIENT_FN0;

215 216 217
	/* Use block mode for transferring over one block size of data */
	func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;

218 219 220
	wlan_data = wl12xx_get_platform_data();
	if (IS_ERR(wlan_data)) {
		ret = PTR_ERR(wlan_data);
221
		dev_err(glue->dev, "missing wlan platform data: %d\n", ret);
222
		goto out_free_glue;
223 224
	}

225 226
	/* if sdio can keep power while host is suspended, enable wow */
	mmcflags = sdio_get_host_pm_caps(func);
227
	dev_dbg(glue->dev, "sdio PM caps = 0x%x\n", mmcflags);
228

229 230
	if (mmcflags & MMC_PM_KEEP_POWER)
		wlan_data->pwr_in_suspend = true;
231

232
	wlan_data->ops = &sdio_ops;
233

234
	sdio_set_drvdata(func, glue);
235

O
Ohad Ben-Cohen 已提交
236 237 238
	/* Tell PM core that we don't need the card to be powered now */
	pm_runtime_put_noidle(&func->dev);

239
	glue->core = platform_device_alloc("wl12xx", -1);
240
	if (!glue->core) {
241
		dev_err(glue->dev, "can't allocate platform_device");
242
		ret = -ENOMEM;
243
		goto out_free_glue;
244 245 246 247 248 249 250 251 252 253 254 255
	}

	glue->core->dev.parent = &func->dev;

	memset(res, 0x00, sizeof(res));

	res[0].start = wlan_data->irq;
	res[0].flags = IORESOURCE_IRQ;
	res[0].name = "irq";

	ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
	if (ret) {
256
		dev_err(glue->dev, "can't add resources\n");
257 258 259 260 261 262
		goto out_dev_put;
	}

	ret = platform_device_add_data(glue->core, wlan_data,
				       sizeof(*wlan_data));
	if (ret) {
263
		dev_err(glue->dev, "can't add platform data\n");
264 265 266 267 268
		goto out_dev_put;
	}

	ret = platform_device_add(glue->core);
	if (ret) {
269
		dev_err(glue->dev, "can't add platform device\n");
270 271
		goto out_dev_put;
	}
272 273
	return 0;

274 275 276
out_dev_put:
	platform_device_put(glue->core);

277 278
out_free_glue:
	kfree(glue);
279

280
out:
281 282 283 284 285
	return ret;
}

static void __devexit wl1271_remove(struct sdio_func *func)
{
286
	struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
287

O
Ohad Ben-Cohen 已提交
288 289 290
	/* Undo decrement done above in wl1271_probe */
	pm_runtime_get_noresume(&func->dev);

291 292
	platform_device_del(glue->core);
	platform_device_put(glue->core);
293
	kfree(glue);
294 295
}

296
#ifdef CONFIG_PM
297 298 299 300
static int wl1271_suspend(struct device *dev)
{
	/* Tell MMC/SDIO core it's OK to power down the card
	 * (if it isn't already), but not to remove it completely */
301
	struct sdio_func *func = dev_to_sdio_func(dev);
E
Eyal Shapira 已提交
302 303
	struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
	struct wl1271 *wl = platform_get_drvdata(glue->core);
304 305 306
	mmc_pm_flag_t sdio_flags;
	int ret = 0;

307 308
	dev_dbg(dev, "wl1271 suspend. wow_enabled: %d\n",
		wl->wow_enabled);
309 310 311 312 313 314

	/* check whether sdio should keep power */
	if (wl->wow_enabled) {
		sdio_flags = sdio_get_host_pm_caps(func);

		if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
315 316
			dev_err(dev, "can't keep power while host "
				     "is suspended\n");
317 318 319 320 321 322 323
			ret = -EINVAL;
			goto out;
		}

		/* keep power while host suspended */
		ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
		if (ret) {
324
			dev_err(dev, "error while trying to keep power\n");
325 326 327 328 329
			goto out;
		}
	}
out:
	return ret;
330 331 332 333
}

static int wl1271_resume(struct device *dev)
{
334
	dev_dbg(dev, "wl1271 resume\n");
335

336 337 338 339 340 341 342
	return 0;
}

static const struct dev_pm_ops wl1271_sdio_pm_ops = {
	.suspend	= wl1271_suspend,
	.resume		= wl1271_resume,
};
343
#endif
344

345
static struct sdio_driver wl1271_sdio_driver = {
346
	.name		= "wl1271_sdio",
347 348 349
	.id_table	= wl1271_devices,
	.probe		= wl1271_probe,
	.remove		= __devexit_p(wl1271_remove),
350
#ifdef CONFIG_PM
351 352 353
	.drv = {
		.pm = &wl1271_sdio_pm_ops,
	},
354
#endif
355 356 357 358
};

static int __init wl1271_init(void)
{
359
	return sdio_register_driver(&wl1271_sdio_driver);
360 361 362 363 364 365 366 367 368 369 370
}

static void __exit wl1271_exit(void)
{
	sdio_unregister_driver(&wl1271_sdio_driver);
}

module_init(wl1271_init);
module_exit(wl1271_exit);

MODULE_LICENSE("GPL");
371
MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
372
MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
373 374
MODULE_FIRMWARE(WL127X_FW_NAME_SINGLE);
MODULE_FIRMWARE(WL127X_FW_NAME_MULTI);
375
MODULE_FIRMWARE(WL127X_PLT_FW_NAME);
376 377
MODULE_FIRMWARE(WL128X_FW_NAME_SINGLE);
MODULE_FIRMWARE(WL128X_FW_NAME_MULTI);
378
MODULE_FIRMWARE(WL128X_PLT_FW_NAME);