xref: /openbmc/linux/drivers/net/wireless/ti/wl1251/sdio.c (revision 7b73a9c8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * wl12xx SDIO routines
4  *
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>
22 
23 #include "wl1251.h"
24 
25 struct wl1251_sdio {
26 	struct sdio_func *func;
27 	u32 elp_val;
28 };
29 
30 static struct sdio_func *wl_to_func(struct wl1251 *wl)
31 {
32 	struct wl1251_sdio *wl_sdio = wl->if_priv;
33 	return wl_sdio->func;
34 }
35 
36 static void wl1251_sdio_interrupt(struct sdio_func *func)
37 {
38 	struct wl1251 *wl = sdio_get_drvdata(func);
39 
40 	wl1251_debug(DEBUG_IRQ, "IRQ");
41 
42 	/* FIXME should be synchronous for sdio */
43 	ieee80211_queue_work(wl->hw, &wl->irq_work);
44 }
45 
46 static const struct sdio_device_id wl1251_devices[] = {
47 	{ SDIO_DEVICE(SDIO_VENDOR_ID_TI_WL1251, SDIO_DEVICE_ID_TI_WL1251) },
48 	{}
49 };
50 MODULE_DEVICE_TABLE(sdio, wl1251_devices);
51 
52 
53 static void wl1251_sdio_read(struct wl1251 *wl, int addr,
54 			     void *buf, size_t len)
55 {
56 	int ret;
57 	struct sdio_func *func = wl_to_func(wl);
58 
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);
64 }
65 
66 static void wl1251_sdio_write(struct wl1251 *wl, int addr,
67 			      void *buf, size_t len)
68 {
69 	int ret;
70 	struct sdio_func *func = wl_to_func(wl);
71 
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);
77 }
78 
79 static void wl1251_sdio_read_elp(struct wl1251 *wl, int addr, u32 *val)
80 {
81 	int ret = 0;
82 	struct wl1251_sdio *wl_sdio = wl->if_priv;
83 	struct sdio_func *func = wl_sdio->func;
84 
85 	/*
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).
90 	 */
91 	sdio_claim_host(func);
92 	*val = sdio_writeb_readb(func, wl_sdio->elp_val, addr, &ret);
93 	sdio_release_host(func);
94 
95 	if (ret)
96 		wl1251_error("sdio_readb failed (%d)", ret);
97 }
98 
99 static void wl1251_sdio_write_elp(struct wl1251 *wl, int addr, u32 val)
100 {
101 	int ret = 0;
102 	struct wl1251_sdio *wl_sdio = wl->if_priv;
103 	struct sdio_func *func = wl_sdio->func;
104 
105 	sdio_claim_host(func);
106 	sdio_writeb(func, val, addr, &ret);
107 	sdio_release_host(func);
108 
109 	if (ret)
110 		wl1251_error("sdio_writeb failed (%d)", ret);
111 	else
112 		wl_sdio->elp_val = val;
113 }
114 
115 static void wl1251_sdio_reset(struct wl1251 *wl)
116 {
117 }
118 
119 static void wl1251_sdio_enable_irq(struct wl1251 *wl)
120 {
121 	struct sdio_func *func = wl_to_func(wl);
122 
123 	sdio_claim_host(func);
124 	sdio_claim_irq(func, wl1251_sdio_interrupt);
125 	sdio_release_host(func);
126 }
127 
128 static void wl1251_sdio_disable_irq(struct wl1251 *wl)
129 {
130 	struct sdio_func *func = wl_to_func(wl);
131 
132 	sdio_claim_host(func);
133 	sdio_release_irq(func);
134 	sdio_release_host(func);
135 }
136 
137 /* Interrupts when using dedicated WLAN_IRQ pin */
138 static irqreturn_t wl1251_line_irq(int irq, void *cookie)
139 {
140 	struct wl1251 *wl = cookie;
141 
142 	ieee80211_queue_work(wl->hw, &wl->irq_work);
143 
144 	return IRQ_HANDLED;
145 }
146 
147 static void wl1251_enable_line_irq(struct wl1251 *wl)
148 {
149 	return enable_irq(wl->irq);
150 }
151 
152 static void wl1251_disable_line_irq(struct wl1251 *wl)
153 {
154 	return disable_irq(wl->irq);
155 }
156 
157 static int wl1251_sdio_set_power(struct wl1251 *wl, bool enable)
158 {
159 	struct sdio_func *func = wl_to_func(wl);
160 	int ret;
161 
162 	if (enable) {
163 		/*
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.
167 		 */
168 		if (gpio_is_valid(wl->power_gpio))
169 			gpio_set_value(wl->power_gpio, true);
170 
171 
172 		ret = pm_runtime_get_sync(&func->dev);
173 		if (ret < 0) {
174 			pm_runtime_put_sync(&func->dev);
175 			goto out;
176 		}
177 
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);
185 
186 		ret = pm_runtime_put_sync(&func->dev);
187 		if (ret < 0)
188 			goto out;
189 
190 		if (gpio_is_valid(wl->power_gpio))
191 			gpio_set_value(wl->power_gpio, false);
192 	}
193 
194 out:
195 	return ret;
196 }
197 
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,
205 };
206 
207 static int wl1251_sdio_probe(struct sdio_func *func,
208 			     const struct sdio_device_id *id)
209 {
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;
216 
217 	hw = wl1251_alloc_hw();
218 	if (IS_ERR(hw))
219 		return PTR_ERR(hw);
220 
221 	wl = hw->priv;
222 
223 	wl_sdio = kzalloc(sizeof(*wl_sdio), GFP_KERNEL);
224 	if (wl_sdio == NULL) {
225 		ret = -ENOMEM;
226 		goto out_free_hw;
227 	}
228 
229 	sdio_claim_host(func);
230 	ret = sdio_enable_func(func);
231 	if (ret)
232 		goto release;
233 
234 	sdio_set_block_size(func, 512);
235 	sdio_release_host(func);
236 
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;
241 
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);
252 
253 		if (wl->power_gpio == -EPROBE_DEFER ||
254 		    wl->irq == -EPROBE_DEFER) {
255 			ret = -EPROBE_DEFER;
256 			goto disable;
257 		}
258 	}
259 
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;
266 		}
267 	}
268 
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;
275 		}
276 
277 		irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
278 
279 		wl1251_sdio_ops.enable_irq = wl1251_enable_line_irq;
280 		wl1251_sdio_ops.disable_irq = wl1251_disable_line_irq;
281 
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;
286 
287 		wl1251_info("using SDIO interrupt");
288 	}
289 
290 	ret = wl1251_init_ieee80211(wl);
291 	if (ret)
292 		goto out_free_irq;
293 
294 	sdio_set_drvdata(func, wl);
295 
296 	/* Tell PM core that we don't need the card to be powered now */
297 	pm_runtime_put_noidle(&func->dev);
298 
299 	return ret;
300 
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;
313 }
314 
315 static void wl1251_sdio_remove(struct sdio_func *func)
316 {
317 	struct wl1251 *wl = sdio_get_drvdata(func);
318 	struct wl1251_sdio *wl_sdio = wl->if_priv;
319 
320 	/* Undo decrement done above in wl1251_probe */
321 	pm_runtime_get_noresume(&func->dev);
322 
323 	if (wl->irq)
324 		free_irq(wl->irq, wl);
325 	wl1251_free_hw(wl);
326 	kfree(wl_sdio);
327 
328 	sdio_claim_host(func);
329 	sdio_release_irq(func);
330 	sdio_disable_func(func);
331 	sdio_release_host(func);
332 }
333 
334 static int wl1251_suspend(struct device *dev)
335 {
336 	/*
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.
339 	 */
340 	return 0;
341 }
342 
343 static int wl1251_resume(struct device *dev)
344 {
345 	return 0;
346 }
347 
348 static const struct dev_pm_ops wl1251_sdio_pm_ops = {
349 	.suspend        = wl1251_suspend,
350 	.resume         = wl1251_resume,
351 };
352 
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,
359 };
360 
361 static int __init wl1251_sdio_init(void)
362 {
363 	int err;
364 
365 	err = sdio_register_driver(&wl1251_sdio_driver);
366 	if (err)
367 		wl1251_error("failed to register sdio driver: %d", err);
368 	return err;
369 }
370 
371 static void __exit wl1251_sdio_exit(void)
372 {
373 	sdio_unregister_driver(&wl1251_sdio_driver);
374 	wl1251_notice("unloaded");
375 }
376 
377 module_init(wl1251_sdio_init);
378 module_exit(wl1251_sdio_exit);
379 
380 MODULE_LICENSE("GPL");
381 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
382