1/*
2 * wl12xx SDIO routines
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 *
18 * Copyright (C) 2005 Texas Instruments Incorporated
19 * Copyright (C) 2008 Google Inc
20 * Copyright (C) 2009 Bob Copeland (me@bobcopeland.com)
21 */
22#include <linux/interrupt.h>
23#include <linux/module.h>
24#include <linux/mod_devicetable.h>
25#include <linux/mmc/sdio_func.h>
26#include <linux/mmc/sdio_ids.h>
27#include <linux/platform_device.h>
28#include <linux/wl12xx.h>
29#include <linux/irq.h>
30#include <linux/pm_runtime.h>
31#include <linux/gpio.h>
32
33#include "wl1251.h"
34
35#ifndef SDIO_VENDOR_ID_TI
36#define SDIO_VENDOR_ID_TI		0x104c
37#endif
38
39#ifndef SDIO_DEVICE_ID_TI_WL1251
40#define SDIO_DEVICE_ID_TI_WL1251	0x9066
41#endif
42
43struct wl1251_sdio {
44	struct sdio_func *func;
45	u32 elp_val;
46};
47
48static struct sdio_func *wl_to_func(struct wl1251 *wl)
49{
50	struct wl1251_sdio *wl_sdio = wl->if_priv;
51	return wl_sdio->func;
52}
53
54static void wl1251_sdio_interrupt(struct sdio_func *func)
55{
56	struct wl1251 *wl = sdio_get_drvdata(func);
57
58	wl1251_debug(DEBUG_IRQ, "IRQ");
59
60	/* FIXME should be synchronous for sdio */
61	ieee80211_queue_work(wl->hw, &wl->irq_work);
62}
63
64static const struct sdio_device_id wl1251_devices[] = {
65	{ SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1251) },
66	{}
67};
68MODULE_DEVICE_TABLE(sdio, wl1251_devices);
69
70
71static void wl1251_sdio_read(struct wl1251 *wl, int addr,
72			     void *buf, size_t len)
73{
74	int ret;
75	struct sdio_func *func = wl_to_func(wl);
76
77	sdio_claim_host(func);
78	ret = sdio_memcpy_fromio(func, buf, addr, len);
79	if (ret)
80		wl1251_error("sdio read failed (%d)", ret);
81	sdio_release_host(func);
82}
83
84static void wl1251_sdio_write(struct wl1251 *wl, int addr,
85			      void *buf, size_t len)
86{
87	int ret;
88	struct sdio_func *func = wl_to_func(wl);
89
90	sdio_claim_host(func);
91	ret = sdio_memcpy_toio(func, addr, buf, len);
92	if (ret)
93		wl1251_error("sdio write failed (%d)", ret);
94	sdio_release_host(func);
95}
96
97static void wl1251_sdio_read_elp(struct wl1251 *wl, int addr, u32 *val)
98{
99	int ret = 0;
100	struct wl1251_sdio *wl_sdio = wl->if_priv;
101	struct sdio_func *func = wl_sdio->func;
102
103	/*
104	 * The hardware only supports RAW (read after write) access for
105	 * reading, regular sdio_readb won't work here (it interprets
106	 * the unused bits of CMD52 as write data even if we send read
107	 * request).
108	 */
109	sdio_claim_host(func);
110	*val = sdio_writeb_readb(func, wl_sdio->elp_val, addr, &ret);
111	sdio_release_host(func);
112
113	if (ret)
114		wl1251_error("sdio_readb failed (%d)", ret);
115}
116
117static void wl1251_sdio_write_elp(struct wl1251 *wl, int addr, u32 val)
118{
119	int ret = 0;
120	struct wl1251_sdio *wl_sdio = wl->if_priv;
121	struct sdio_func *func = wl_sdio->func;
122
123	sdio_claim_host(func);
124	sdio_writeb(func, val, addr, &ret);
125	sdio_release_host(func);
126
127	if (ret)
128		wl1251_error("sdio_writeb failed (%d)", ret);
129	else
130		wl_sdio->elp_val = val;
131}
132
133static void wl1251_sdio_reset(struct wl1251 *wl)
134{
135}
136
137static void wl1251_sdio_enable_irq(struct wl1251 *wl)
138{
139	struct sdio_func *func = wl_to_func(wl);
140
141	sdio_claim_host(func);
142	sdio_claim_irq(func, wl1251_sdio_interrupt);
143	sdio_release_host(func);
144}
145
146static void wl1251_sdio_disable_irq(struct wl1251 *wl)
147{
148	struct sdio_func *func = wl_to_func(wl);
149
150	sdio_claim_host(func);
151	sdio_release_irq(func);
152	sdio_release_host(func);
153}
154
155/* Interrupts when using dedicated WLAN_IRQ pin */
156static irqreturn_t wl1251_line_irq(int irq, void *cookie)
157{
158	struct wl1251 *wl = cookie;
159
160	ieee80211_queue_work(wl->hw, &wl->irq_work);
161
162	return IRQ_HANDLED;
163}
164
165static void wl1251_enable_line_irq(struct wl1251 *wl)
166{
167	return enable_irq(wl->irq);
168}
169
170static void wl1251_disable_line_irq(struct wl1251 *wl)
171{
172	return disable_irq(wl->irq);
173}
174
175static int wl1251_sdio_set_power(struct wl1251 *wl, bool enable)
176{
177	struct sdio_func *func = wl_to_func(wl);
178	int ret;
179
180	if (enable) {
181		/*
182		 * Power is controlled by runtime PM, but we still call board
183		 * callback in case it wants to do any additional setup,
184		 * for example enabling clock buffer for the module.
185		 */
186		if (gpio_is_valid(wl->power_gpio))
187			gpio_set_value(wl->power_gpio, true);
188
189
190		ret = pm_runtime_get_sync(&func->dev);
191		if (ret < 0) {
192			pm_runtime_put_sync(&func->dev);
193			goto out;
194		}
195
196		sdio_claim_host(func);
197		sdio_enable_func(func);
198		sdio_release_host(func);
199	} else {
200		sdio_claim_host(func);
201		sdio_disable_func(func);
202		sdio_release_host(func);
203
204		ret = pm_runtime_put_sync(&func->dev);
205		if (ret < 0)
206			goto out;
207
208		if (gpio_is_valid(wl->power_gpio))
209			gpio_set_value(wl->power_gpio, false);
210	}
211
212out:
213	return ret;
214}
215
216static struct wl1251_if_operations wl1251_sdio_ops = {
217	.read = wl1251_sdio_read,
218	.write = wl1251_sdio_write,
219	.write_elp = wl1251_sdio_write_elp,
220	.read_elp = wl1251_sdio_read_elp,
221	.reset = wl1251_sdio_reset,
222	.power = wl1251_sdio_set_power,
223};
224
225static int wl1251_sdio_probe(struct sdio_func *func,
226			     const struct sdio_device_id *id)
227{
228	int ret;
229	struct wl1251 *wl;
230	struct ieee80211_hw *hw;
231	struct wl1251_sdio *wl_sdio;
232	const struct wl1251_platform_data *wl1251_board_data;
233
234	hw = wl1251_alloc_hw();
235	if (IS_ERR(hw))
236		return PTR_ERR(hw);
237
238	wl = hw->priv;
239
240	wl_sdio = kzalloc(sizeof(*wl_sdio), GFP_KERNEL);
241	if (wl_sdio == NULL) {
242		ret = -ENOMEM;
243		goto out_free_hw;
244	}
245
246	sdio_claim_host(func);
247	ret = sdio_enable_func(func);
248	if (ret)
249		goto release;
250
251	sdio_set_block_size(func, 512);
252	sdio_release_host(func);
253
254	SET_IEEE80211_DEV(hw, &func->dev);
255	wl_sdio->func = func;
256	wl->if_priv = wl_sdio;
257	wl->if_ops = &wl1251_sdio_ops;
258
259	wl1251_board_data = wl1251_get_platform_data();
260	if (!IS_ERR(wl1251_board_data)) {
261		wl->power_gpio = wl1251_board_data->power_gpio;
262		wl->irq = wl1251_board_data->irq;
263		wl->use_eeprom = wl1251_board_data->use_eeprom;
264	}
265
266	if (gpio_is_valid(wl->power_gpio)) {
267		ret = devm_gpio_request(&func->dev, wl->power_gpio,
268								"wl1251 power");
269		if (ret) {
270			wl1251_error("Failed to request gpio: %d\n", ret);
271			goto disable;
272		}
273	}
274
275	if (wl->irq) {
276		irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
277		ret = request_irq(wl->irq, wl1251_line_irq, 0, "wl1251", wl);
278		if (ret < 0) {
279			wl1251_error("request_irq() failed: %d", ret);
280			goto disable;
281		}
282
283		irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
284
285		wl1251_sdio_ops.enable_irq = wl1251_enable_line_irq;
286		wl1251_sdio_ops.disable_irq = wl1251_disable_line_irq;
287
288		wl1251_info("using dedicated interrupt line");
289	} else {
290		wl1251_sdio_ops.enable_irq = wl1251_sdio_enable_irq;
291		wl1251_sdio_ops.disable_irq = wl1251_sdio_disable_irq;
292
293		wl1251_info("using SDIO interrupt");
294	}
295
296	ret = wl1251_init_ieee80211(wl);
297	if (ret)
298		goto out_free_irq;
299
300	sdio_set_drvdata(func, wl);
301
302	/* Tell PM core that we don't need the card to be powered now */
303	pm_runtime_put_noidle(&func->dev);
304
305	return ret;
306
307out_free_irq:
308	if (wl->irq)
309		free_irq(wl->irq, wl);
310disable:
311	sdio_claim_host(func);
312	sdio_disable_func(func);
313release:
314	sdio_release_host(func);
315	kfree(wl_sdio);
316out_free_hw:
317	wl1251_free_hw(wl);
318	return ret;
319}
320
321static void wl1251_sdio_remove(struct sdio_func *func)
322{
323	struct wl1251 *wl = sdio_get_drvdata(func);
324	struct wl1251_sdio *wl_sdio = wl->if_priv;
325
326	/* Undo decrement done above in wl1251_probe */
327	pm_runtime_get_noresume(&func->dev);
328
329	if (wl->irq)
330		free_irq(wl->irq, wl);
331	wl1251_free_hw(wl);
332	kfree(wl_sdio);
333
334	sdio_claim_host(func);
335	sdio_release_irq(func);
336	sdio_disable_func(func);
337	sdio_release_host(func);
338}
339
340static int wl1251_suspend(struct device *dev)
341{
342	/*
343	 * Tell MMC/SDIO core it's OK to power down the card
344	 * (if it isn't already), but not to remove it completely.
345	 */
346	return 0;
347}
348
349static int wl1251_resume(struct device *dev)
350{
351	return 0;
352}
353
354static const struct dev_pm_ops wl1251_sdio_pm_ops = {
355	.suspend        = wl1251_suspend,
356	.resume         = wl1251_resume,
357};
358
359static struct sdio_driver wl1251_sdio_driver = {
360	.name		= "wl1251_sdio",
361	.id_table	= wl1251_devices,
362	.probe		= wl1251_sdio_probe,
363	.remove		= wl1251_sdio_remove,
364	.drv.pm		= &wl1251_sdio_pm_ops,
365};
366
367static int __init wl1251_sdio_init(void)
368{
369	int err;
370
371	err = sdio_register_driver(&wl1251_sdio_driver);
372	if (err)
373		wl1251_error("failed to register sdio driver: %d", err);
374	return err;
375}
376
377static void __exit wl1251_sdio_exit(void)
378{
379	sdio_unregister_driver(&wl1251_sdio_driver);
380	wl1251_notice("unloaded");
381}
382
383module_init(wl1251_sdio_init);
384module_exit(wl1251_sdio_exit);
385
386MODULE_LICENSE("GPL");
387MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
388