treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / wireless / ti / wl1251 / sdio.c
blob94569cd695c81822ed9bd496ca5f45a88e7a0c44
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/gpio.h>
19 #include <linux/of.h>
20 #include <linux/of_gpio.h>
21 #include <linux/of_irq.h>
23 #include "wl1251.h"
25 struct wl1251_sdio {
26 struct sdio_func *func;
27 u32 elp_val;
30 static struct sdio_func *wl_to_func(struct wl1251 *wl)
32 struct wl1251_sdio *wl_sdio = wl->if_priv;
33 return wl_sdio->func;
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)
56 int ret;
57 struct sdio_func *func = wl_to_func(wl);
59 sdio_claim_host(func);
60 ret = sdio_memcpy_fromio(func, buf, addr, len);
61 if (ret)
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)
69 int ret;
70 struct sdio_func *func = wl_to_func(wl);
72 sdio_claim_host(func);
73 ret = sdio_memcpy_toio(func, addr, buf, len);
74 if (ret)
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)
81 int ret = 0;
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
89 * request).
91 sdio_claim_host(func);
92 *val = sdio_writeb_readb(func, wl_sdio->elp_val, addr, &ret);
93 sdio_release_host(func);
95 if (ret)
96 wl1251_error("sdio_readb failed (%d)", ret);
99 static void wl1251_sdio_write_elp(struct wl1251 *wl, int addr, u32 val)
101 int ret = 0;
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);
109 if (ret)
110 wl1251_error("sdio_writeb failed (%d)", ret);
111 else
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);
144 return IRQ_HANDLED;
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);
160 int ret;
162 if (enable) {
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);
173 if (ret < 0) {
174 pm_runtime_put_sync(&func->dev);
175 goto out;
178 sdio_claim_host(func);
179 sdio_enable_func(func);
180 sdio_release_host(func);
181 } else {
182 sdio_claim_host(func);
183 sdio_disable_func(func);
184 sdio_release_host(func);
186 ret = pm_runtime_put_sync(&func->dev);
187 if (ret < 0)
188 goto out;
190 if (gpio_is_valid(wl->power_gpio))
191 gpio_set_value(wl->power_gpio, false);
194 out:
195 return ret;
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)
210 int ret;
211 struct wl1251 *wl;
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();
218 if (IS_ERR(hw))
219 return PTR_ERR(hw);
221 wl = hw->priv;
223 wl_sdio = kzalloc(sizeof(*wl_sdio), GFP_KERNEL);
224 if (wl_sdio == NULL) {
225 ret = -ENOMEM;
226 goto out_free_hw;
229 sdio_claim_host(func);
230 ret = sdio_enable_func(func);
231 if (ret)
232 goto release;
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;
247 } else if (np) {
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) {
255 ret = -EPROBE_DEFER;
256 goto disable;
260 if (gpio_is_valid(wl->power_gpio)) {
261 ret = devm_gpio_request(&func->dev, wl->power_gpio,
262 "wl1251 power");
263 if (ret) {
264 wl1251_error("Failed to request gpio: %d\n", ret);
265 goto disable;
269 if (wl->irq) {
270 irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
271 ret = request_irq(wl->irq, wl1251_line_irq, 0, "wl1251", wl);
272 if (ret < 0) {
273 wl1251_error("request_irq() failed: %d", ret);
274 goto disable;
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");
283 } else {
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);
291 if (ret)
292 goto out_free_irq;
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);
299 return ret;
301 out_free_irq:
302 if (wl->irq)
303 free_irq(wl->irq, wl);
304 disable:
305 sdio_claim_host(func);
306 sdio_disable_func(func);
307 release:
308 sdio_release_host(func);
309 kfree(wl_sdio);
310 out_free_hw:
311 wl1251_free_hw(wl);
312 return ret;
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);
323 if (wl->irq)
324 free_irq(wl->irq, wl);
325 wl1251_free_hw(wl);
326 kfree(wl_sdio);
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.
340 return 0;
343 static int wl1251_resume(struct device *dev)
345 return 0;
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)
363 int err;
365 err = sdio_register_driver(&wl1251_sdio_driver);
366 if (err)
367 wl1251_error("failed to register sdio driver: %d", err);
368 return 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>");