1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2005 Texas Instruments Incorporated
6 * Copyright (C) 2008 Google Inc
7 * Copyright (C) 2009 Bob Copeland (me@bobcopeland.com)
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/mmc/sdio_func.h>
13 #include <linux/mmc/sdio_ids.h>
14 #include <linux/platform_device.h>
15 #include <linux/wl12xx.h>
16 #include <linux/irq.h>
17 #include <linux/pm_runtime.h>
19 #include <linux/of_irq.h>
24 struct sdio_func
*func
;
28 static struct sdio_func
*wl_to_func(struct wl1251
*wl
)
30 struct wl1251_sdio
*wl_sdio
= wl
->if_priv
;
34 static void wl1251_sdio_interrupt(struct sdio_func
*func
)
36 struct wl1251
*wl
= sdio_get_drvdata(func
);
38 wl1251_debug(DEBUG_IRQ
, "IRQ");
40 /* FIXME should be synchronous for sdio */
41 ieee80211_queue_work(wl
->hw
, &wl
->irq_work
);
44 static const struct sdio_device_id wl1251_devices
[] = {
45 { SDIO_DEVICE(SDIO_VENDOR_ID_TI_WL1251
, SDIO_DEVICE_ID_TI_WL1251
) },
48 MODULE_DEVICE_TABLE(sdio
, wl1251_devices
);
51 static void wl1251_sdio_read(struct wl1251
*wl
, int addr
,
52 void *buf
, size_t len
)
55 struct sdio_func
*func
= wl_to_func(wl
);
57 sdio_claim_host(func
);
58 ret
= sdio_memcpy_fromio(func
, buf
, addr
, len
);
60 wl1251_error("sdio read failed (%d)", ret
);
61 sdio_release_host(func
);
64 static void wl1251_sdio_write(struct wl1251
*wl
, int addr
,
65 void *buf
, size_t len
)
68 struct sdio_func
*func
= wl_to_func(wl
);
70 sdio_claim_host(func
);
71 ret
= sdio_memcpy_toio(func
, addr
, buf
, len
);
73 wl1251_error("sdio write failed (%d)", ret
);
74 sdio_release_host(func
);
77 static void wl1251_sdio_read_elp(struct wl1251
*wl
, int addr
, u32
*val
)
80 struct wl1251_sdio
*wl_sdio
= wl
->if_priv
;
81 struct sdio_func
*func
= wl_sdio
->func
;
84 * The hardware only supports RAW (read after write) access for
85 * reading, regular sdio_readb won't work here (it interprets
86 * the unused bits of CMD52 as write data even if we send read
89 sdio_claim_host(func
);
90 *val
= sdio_writeb_readb(func
, wl_sdio
->elp_val
, addr
, &ret
);
91 sdio_release_host(func
);
94 wl1251_error("sdio_readb failed (%d)", ret
);
97 static void wl1251_sdio_write_elp(struct wl1251
*wl
, int addr
, u32 val
)
100 struct wl1251_sdio
*wl_sdio
= wl
->if_priv
;
101 struct sdio_func
*func
= wl_sdio
->func
;
103 sdio_claim_host(func
);
104 sdio_writeb(func
, val
, addr
, &ret
);
105 sdio_release_host(func
);
108 wl1251_error("sdio_writeb failed (%d)", ret
);
110 wl_sdio
->elp_val
= val
;
113 static void wl1251_sdio_reset(struct wl1251
*wl
)
117 static void wl1251_sdio_enable_irq(struct wl1251
*wl
)
119 struct sdio_func
*func
= wl_to_func(wl
);
121 sdio_claim_host(func
);
122 sdio_claim_irq(func
, wl1251_sdio_interrupt
);
123 sdio_release_host(func
);
126 static void wl1251_sdio_disable_irq(struct wl1251
*wl
)
128 struct sdio_func
*func
= wl_to_func(wl
);
130 sdio_claim_host(func
);
131 sdio_release_irq(func
);
132 sdio_release_host(func
);
135 /* Interrupts when using dedicated WLAN_IRQ pin */
136 static irqreturn_t
wl1251_line_irq(int irq
, void *cookie
)
138 struct wl1251
*wl
= cookie
;
140 ieee80211_queue_work(wl
->hw
, &wl
->irq_work
);
145 static void wl1251_enable_line_irq(struct wl1251
*wl
)
147 return enable_irq(wl
->irq
);
150 static void wl1251_disable_line_irq(struct wl1251
*wl
)
152 return disable_irq(wl
->irq
);
155 static int wl1251_sdio_set_power(struct wl1251
*wl
, bool enable
)
157 struct sdio_func
*func
= wl_to_func(wl
);
161 ret
= pm_runtime_get_sync(&func
->dev
);
163 pm_runtime_put_sync(&func
->dev
);
167 sdio_claim_host(func
);
168 sdio_enable_func(func
);
169 sdio_release_host(func
);
171 sdio_claim_host(func
);
172 sdio_disable_func(func
);
173 sdio_release_host(func
);
175 ret
= pm_runtime_put_sync(&func
->dev
);
184 static struct wl1251_if_operations wl1251_sdio_ops
= {
185 .read
= wl1251_sdio_read
,
186 .write
= wl1251_sdio_write
,
187 .write_elp
= wl1251_sdio_write_elp
,
188 .read_elp
= wl1251_sdio_read_elp
,
189 .reset
= wl1251_sdio_reset
,
190 .power
= wl1251_sdio_set_power
,
193 static int wl1251_sdio_probe(struct sdio_func
*func
,
194 const struct sdio_device_id
*id
)
198 struct ieee80211_hw
*hw
;
199 struct wl1251_sdio
*wl_sdio
;
200 const struct wl1251_platform_data
*wl1251_board_data
;
201 struct device_node
*np
= func
->dev
.of_node
;
203 hw
= wl1251_alloc_hw();
209 wl_sdio
= kzalloc(sizeof(*wl_sdio
), GFP_KERNEL
);
210 if (wl_sdio
== NULL
) {
215 sdio_claim_host(func
);
216 ret
= sdio_enable_func(func
);
220 sdio_set_block_size(func
, 512);
221 sdio_release_host(func
);
223 SET_IEEE80211_DEV(hw
, &func
->dev
);
224 wl_sdio
->func
= func
;
225 wl
->if_priv
= wl_sdio
;
226 wl
->if_ops
= &wl1251_sdio_ops
;
228 wl1251_board_data
= wl1251_get_platform_data();
229 if (!IS_ERR(wl1251_board_data
)) {
230 wl
->irq
= wl1251_board_data
->irq
;
231 wl
->use_eeprom
= wl1251_board_data
->use_eeprom
;
233 wl
->use_eeprom
= of_property_read_bool(np
, "ti,wl1251-has-eeprom");
234 wl
->irq
= of_irq_get(np
, 0);
235 if (wl
->irq
== -EPROBE_DEFER
) {
242 irq_set_status_flags(wl
->irq
, IRQ_NOAUTOEN
);
243 ret
= request_irq(wl
->irq
, wl1251_line_irq
, 0, "wl1251", wl
);
245 wl1251_error("request_irq() failed: %d", ret
);
249 irq_set_irq_type(wl
->irq
, IRQ_TYPE_EDGE_RISING
);
251 wl1251_sdio_ops
.enable_irq
= wl1251_enable_line_irq
;
252 wl1251_sdio_ops
.disable_irq
= wl1251_disable_line_irq
;
254 wl1251_info("using dedicated interrupt line");
256 wl1251_sdio_ops
.enable_irq
= wl1251_sdio_enable_irq
;
257 wl1251_sdio_ops
.disable_irq
= wl1251_sdio_disable_irq
;
259 wl1251_info("using SDIO interrupt");
262 ret
= wl1251_init_ieee80211(wl
);
266 sdio_set_drvdata(func
, wl
);
268 /* Tell PM core that we don't need the card to be powered now */
269 pm_runtime_put_noidle(&func
->dev
);
275 free_irq(wl
->irq
, wl
);
277 sdio_claim_host(func
);
278 sdio_disable_func(func
);
280 sdio_release_host(func
);
287 static void wl1251_sdio_remove(struct sdio_func
*func
)
289 struct wl1251
*wl
= sdio_get_drvdata(func
);
290 struct wl1251_sdio
*wl_sdio
= wl
->if_priv
;
292 /* Undo decrement done above in wl1251_probe */
293 pm_runtime_get_noresume(&func
->dev
);
296 free_irq(wl
->irq
, wl
);
300 sdio_claim_host(func
);
301 sdio_release_irq(func
);
302 sdio_disable_func(func
);
303 sdio_release_host(func
);
306 static int wl1251_suspend(struct device
*dev
)
309 * Tell MMC/SDIO core it's OK to power down the card
310 * (if it isn't already), but not to remove it completely.
315 static int wl1251_resume(struct device
*dev
)
320 static const struct dev_pm_ops wl1251_sdio_pm_ops
= {
321 .suspend
= wl1251_suspend
,
322 .resume
= wl1251_resume
,
325 static struct sdio_driver wl1251_sdio_driver
= {
326 .name
= "wl1251_sdio",
327 .id_table
= wl1251_devices
,
328 .probe
= wl1251_sdio_probe
,
329 .remove
= wl1251_sdio_remove
,
330 .drv
.pm
= &wl1251_sdio_pm_ops
,
333 static int __init
wl1251_sdio_init(void)
337 err
= sdio_register_driver(&wl1251_sdio_driver
);
339 wl1251_error("failed to register sdio driver: %d", err
);
343 static void __exit
wl1251_sdio_exit(void)
345 sdio_unregister_driver(&wl1251_sdio_driver
);
346 wl1251_notice("unloaded");
349 module_init(wl1251_sdio_init
);
350 module_exit(wl1251_sdio_exit
);
352 MODULE_LICENSE("GPL");
353 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");