gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / wireless / ti / wl1251 / sdio.c
blobc9a4e9a43400114b99d707a8e0e7011c5cf24d2f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * wl12xx SDIO routines
5 * Copyright (C) 2005 Texas Instruments Incorporated
6 * Copyright (C) 2008 Google Inc
7 * Copyright (C) 2009 Bob Copeland (me@bobcopeland.com)
8 */
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/of.h>
19 #include <linux/of_irq.h>
21 #include "wl1251.h"
23 struct wl1251_sdio {
24 struct sdio_func *func;
25 u32 elp_val;
28 static struct sdio_func *wl_to_func(struct wl1251 *wl)
30 struct wl1251_sdio *wl_sdio = wl->if_priv;
31 return wl_sdio->func;
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)
54 int ret;
55 struct sdio_func *func = wl_to_func(wl);
57 sdio_claim_host(func);
58 ret = sdio_memcpy_fromio(func, buf, addr, len);
59 if (ret)
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)
67 int ret;
68 struct sdio_func *func = wl_to_func(wl);
70 sdio_claim_host(func);
71 ret = sdio_memcpy_toio(func, addr, buf, len);
72 if (ret)
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)
79 int ret = 0;
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
87 * request).
89 sdio_claim_host(func);
90 *val = sdio_writeb_readb(func, wl_sdio->elp_val, addr, &ret);
91 sdio_release_host(func);
93 if (ret)
94 wl1251_error("sdio_readb failed (%d)", ret);
97 static void wl1251_sdio_write_elp(struct wl1251 *wl, int addr, u32 val)
99 int ret = 0;
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);
107 if (ret)
108 wl1251_error("sdio_writeb failed (%d)", ret);
109 else
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);
142 return IRQ_HANDLED;
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);
158 int ret;
160 if (enable) {
161 ret = pm_runtime_get_sync(&func->dev);
162 if (ret < 0) {
163 pm_runtime_put_sync(&func->dev);
164 goto out;
167 sdio_claim_host(func);
168 sdio_enable_func(func);
169 sdio_release_host(func);
170 } else {
171 sdio_claim_host(func);
172 sdio_disable_func(func);
173 sdio_release_host(func);
175 ret = pm_runtime_put_sync(&func->dev);
176 if (ret < 0)
177 goto out;
180 out:
181 return ret;
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)
196 int ret;
197 struct wl1251 *wl;
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();
204 if (IS_ERR(hw))
205 return PTR_ERR(hw);
207 wl = hw->priv;
209 wl_sdio = kzalloc(sizeof(*wl_sdio), GFP_KERNEL);
210 if (wl_sdio == NULL) {
211 ret = -ENOMEM;
212 goto out_free_hw;
215 sdio_claim_host(func);
216 ret = sdio_enable_func(func);
217 if (ret)
218 goto release;
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;
232 } else if (np) {
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) {
236 ret = -EPROBE_DEFER;
237 goto disable;
241 if (wl->irq) {
242 irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
243 ret = request_irq(wl->irq, wl1251_line_irq, 0, "wl1251", wl);
244 if (ret < 0) {
245 wl1251_error("request_irq() failed: %d", ret);
246 goto disable;
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");
255 } else {
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);
263 if (ret)
264 goto out_free_irq;
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);
271 return ret;
273 out_free_irq:
274 if (wl->irq)
275 free_irq(wl->irq, wl);
276 disable:
277 sdio_claim_host(func);
278 sdio_disable_func(func);
279 release:
280 sdio_release_host(func);
281 kfree(wl_sdio);
282 out_free_hw:
283 wl1251_free_hw(wl);
284 return ret;
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);
295 if (wl->irq)
296 free_irq(wl->irq, wl);
297 wl1251_free_hw(wl);
298 kfree(wl_sdio);
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.
312 return 0;
315 static int wl1251_resume(struct device *dev)
317 return 0;
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)
335 int err;
337 err = sdio_register_driver(&wl1251_sdio_driver);
338 if (err)
339 wl1251_error("failed to register sdio driver: %d", err);
340 return 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>");