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>
18 #include <linux/gpio.h>
20 #include <linux/of_gpio.h>
21 #include <linux/of_irq.h>
26 struct sdio_func
*func
;
30 static struct sdio_func
*wl_to_func(struct wl1251
*wl
)
32 struct wl1251_sdio
*wl_sdio
= wl
->if_priv
;
36 static void wl1251_sdio_interrupt(struct sdio_func
*func
)
38 struct wl1251
*wl
= sdio_get_drvdata(func
);
40 wl1251_debug(DEBUG_IRQ
, "IRQ");
42 /* FIXME should be synchronous for sdio */
43 ieee80211_queue_work(wl
->hw
, &wl
->irq_work
);
46 static const struct sdio_device_id wl1251_devices
[] = {
47 { SDIO_DEVICE(SDIO_VENDOR_ID_TI_WL1251
, SDIO_DEVICE_ID_TI_WL1251
) },
50 MODULE_DEVICE_TABLE(sdio
, wl1251_devices
);
53 static void wl1251_sdio_read(struct wl1251
*wl
, int addr
,
54 void *buf
, size_t len
)
57 struct sdio_func
*func
= wl_to_func(wl
);
59 sdio_claim_host(func
);
60 ret
= sdio_memcpy_fromio(func
, buf
, addr
, len
);
62 wl1251_error("sdio read failed (%d)", ret
);
63 sdio_release_host(func
);
66 static void wl1251_sdio_write(struct wl1251
*wl
, int addr
,
67 void *buf
, size_t len
)
70 struct sdio_func
*func
= wl_to_func(wl
);
72 sdio_claim_host(func
);
73 ret
= sdio_memcpy_toio(func
, addr
, buf
, len
);
75 wl1251_error("sdio write failed (%d)", ret
);
76 sdio_release_host(func
);
79 static void wl1251_sdio_read_elp(struct wl1251
*wl
, int addr
, u32
*val
)
82 struct wl1251_sdio
*wl_sdio
= wl
->if_priv
;
83 struct sdio_func
*func
= wl_sdio
->func
;
86 * The hardware only supports RAW (read after write) access for
87 * reading, regular sdio_readb won't work here (it interprets
88 * the unused bits of CMD52 as write data even if we send read
91 sdio_claim_host(func
);
92 *val
= sdio_writeb_readb(func
, wl_sdio
->elp_val
, addr
, &ret
);
93 sdio_release_host(func
);
96 wl1251_error("sdio_readb failed (%d)", ret
);
99 static void wl1251_sdio_write_elp(struct wl1251
*wl
, int addr
, u32 val
)
102 struct wl1251_sdio
*wl_sdio
= wl
->if_priv
;
103 struct sdio_func
*func
= wl_sdio
->func
;
105 sdio_claim_host(func
);
106 sdio_writeb(func
, val
, addr
, &ret
);
107 sdio_release_host(func
);
110 wl1251_error("sdio_writeb failed (%d)", ret
);
112 wl_sdio
->elp_val
= val
;
115 static void wl1251_sdio_reset(struct wl1251
*wl
)
119 static void wl1251_sdio_enable_irq(struct wl1251
*wl
)
121 struct sdio_func
*func
= wl_to_func(wl
);
123 sdio_claim_host(func
);
124 sdio_claim_irq(func
, wl1251_sdio_interrupt
);
125 sdio_release_host(func
);
128 static void wl1251_sdio_disable_irq(struct wl1251
*wl
)
130 struct sdio_func
*func
= wl_to_func(wl
);
132 sdio_claim_host(func
);
133 sdio_release_irq(func
);
134 sdio_release_host(func
);
137 /* Interrupts when using dedicated WLAN_IRQ pin */
138 static irqreturn_t
wl1251_line_irq(int irq
, void *cookie
)
140 struct wl1251
*wl
= cookie
;
142 ieee80211_queue_work(wl
->hw
, &wl
->irq_work
);
147 static void wl1251_enable_line_irq(struct wl1251
*wl
)
149 return enable_irq(wl
->irq
);
152 static void wl1251_disable_line_irq(struct wl1251
*wl
)
154 return disable_irq(wl
->irq
);
157 static int wl1251_sdio_set_power(struct wl1251
*wl
, bool enable
)
159 struct sdio_func
*func
= wl_to_func(wl
);
164 * Power is controlled by runtime PM, but we still call board
165 * callback in case it wants to do any additional setup,
166 * for example enabling clock buffer for the module.
168 if (gpio_is_valid(wl
->power_gpio
))
169 gpio_set_value(wl
->power_gpio
, true);
172 ret
= pm_runtime_get_sync(&func
->dev
);
174 pm_runtime_put_sync(&func
->dev
);
178 sdio_claim_host(func
);
179 sdio_enable_func(func
);
180 sdio_release_host(func
);
182 sdio_claim_host(func
);
183 sdio_disable_func(func
);
184 sdio_release_host(func
);
186 ret
= pm_runtime_put_sync(&func
->dev
);
190 if (gpio_is_valid(wl
->power_gpio
))
191 gpio_set_value(wl
->power_gpio
, false);
198 static struct wl1251_if_operations wl1251_sdio_ops
= {
199 .read
= wl1251_sdio_read
,
200 .write
= wl1251_sdio_write
,
201 .write_elp
= wl1251_sdio_write_elp
,
202 .read_elp
= wl1251_sdio_read_elp
,
203 .reset
= wl1251_sdio_reset
,
204 .power
= wl1251_sdio_set_power
,
207 static int wl1251_sdio_probe(struct sdio_func
*func
,
208 const struct sdio_device_id
*id
)
212 struct ieee80211_hw
*hw
;
213 struct wl1251_sdio
*wl_sdio
;
214 const struct wl1251_platform_data
*wl1251_board_data
;
215 struct device_node
*np
= func
->dev
.of_node
;
217 hw
= wl1251_alloc_hw();
223 wl_sdio
= kzalloc(sizeof(*wl_sdio
), GFP_KERNEL
);
224 if (wl_sdio
== NULL
) {
229 sdio_claim_host(func
);
230 ret
= sdio_enable_func(func
);
234 sdio_set_block_size(func
, 512);
235 sdio_release_host(func
);
237 SET_IEEE80211_DEV(hw
, &func
->dev
);
238 wl_sdio
->func
= func
;
239 wl
->if_priv
= wl_sdio
;
240 wl
->if_ops
= &wl1251_sdio_ops
;
242 wl1251_board_data
= wl1251_get_platform_data();
243 if (!IS_ERR(wl1251_board_data
)) {
244 wl
->power_gpio
= wl1251_board_data
->power_gpio
;
245 wl
->irq
= wl1251_board_data
->irq
;
246 wl
->use_eeprom
= wl1251_board_data
->use_eeprom
;
248 wl
->use_eeprom
= of_property_read_bool(np
,
249 "ti,wl1251-has-eeprom");
250 wl
->power_gpio
= of_get_named_gpio(np
, "ti,power-gpio", 0);
251 wl
->irq
= of_irq_get(np
, 0);
253 if (wl
->power_gpio
== -EPROBE_DEFER
||
254 wl
->irq
== -EPROBE_DEFER
) {
260 if (gpio_is_valid(wl
->power_gpio
)) {
261 ret
= devm_gpio_request(&func
->dev
, wl
->power_gpio
,
264 wl1251_error("Failed to request gpio: %d\n", ret
);
270 irq_set_status_flags(wl
->irq
, IRQ_NOAUTOEN
);
271 ret
= request_irq(wl
->irq
, wl1251_line_irq
, 0, "wl1251", wl
);
273 wl1251_error("request_irq() failed: %d", ret
);
277 irq_set_irq_type(wl
->irq
, IRQ_TYPE_EDGE_RISING
);
279 wl1251_sdio_ops
.enable_irq
= wl1251_enable_line_irq
;
280 wl1251_sdio_ops
.disable_irq
= wl1251_disable_line_irq
;
282 wl1251_info("using dedicated interrupt line");
284 wl1251_sdio_ops
.enable_irq
= wl1251_sdio_enable_irq
;
285 wl1251_sdio_ops
.disable_irq
= wl1251_sdio_disable_irq
;
287 wl1251_info("using SDIO interrupt");
290 ret
= wl1251_init_ieee80211(wl
);
294 sdio_set_drvdata(func
, wl
);
296 /* Tell PM core that we don't need the card to be powered now */
297 pm_runtime_put_noidle(&func
->dev
);
303 free_irq(wl
->irq
, wl
);
305 sdio_claim_host(func
);
306 sdio_disable_func(func
);
308 sdio_release_host(func
);
315 static void wl1251_sdio_remove(struct sdio_func
*func
)
317 struct wl1251
*wl
= sdio_get_drvdata(func
);
318 struct wl1251_sdio
*wl_sdio
= wl
->if_priv
;
320 /* Undo decrement done above in wl1251_probe */
321 pm_runtime_get_noresume(&func
->dev
);
324 free_irq(wl
->irq
, wl
);
328 sdio_claim_host(func
);
329 sdio_release_irq(func
);
330 sdio_disable_func(func
);
331 sdio_release_host(func
);
334 static int wl1251_suspend(struct device
*dev
)
337 * Tell MMC/SDIO core it's OK to power down the card
338 * (if it isn't already), but not to remove it completely.
343 static int wl1251_resume(struct device
*dev
)
348 static const struct dev_pm_ops wl1251_sdio_pm_ops
= {
349 .suspend
= wl1251_suspend
,
350 .resume
= wl1251_resume
,
353 static struct sdio_driver wl1251_sdio_driver
= {
354 .name
= "wl1251_sdio",
355 .id_table
= wl1251_devices
,
356 .probe
= wl1251_sdio_probe
,
357 .remove
= wl1251_sdio_remove
,
358 .drv
.pm
= &wl1251_sdio_pm_ops
,
361 static int __init
wl1251_sdio_init(void)
365 err
= sdio_register_driver(&wl1251_sdio_driver
);
367 wl1251_error("failed to register sdio driver: %d", err
);
371 static void __exit
wl1251_sdio_exit(void)
373 sdio_unregister_driver(&wl1251_sdio_driver
);
374 wl1251_notice("unloaded");
377 module_init(wl1251_sdio_init
);
378 module_exit(wl1251_sdio_exit
);
380 MODULE_LICENSE("GPL");
381 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");