xref: /linux/drivers/net/wireless/st/cw1200/cw1200_sdio.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Mac80211 SDIO driver for ST-Ericsson CW1200 device
4  *
5  * Copyright (c) 2010, ST-Ericsson
6  * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/delay.h>
13 #include <linux/mmc/host.h>
14 #include <linux/mmc/sdio_func.h>
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/sdio.h>
17 #include <linux/mmc/sdio_ids.h>
18 #include <net/mac80211.h>
19 
20 #include "cw1200.h"
21 #include "hwbus.h"
22 #include <linux/platform_data/net-cw1200.h>
23 #include "hwio.h"
24 
25 MODULE_AUTHOR("Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>");
26 MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SDIO driver");
27 MODULE_LICENSE("GPL");
28 
29 #define SDIO_BLOCK_SIZE (512)
30 
31 /* Default platform data for Sagrad modules */
32 static struct cw1200_platform_data_sdio sagrad_109x_evk_platform_data = {
33 	.ref_clk = 38400,
34 	.have_5ghz = false,
35 	.sdd_file = "sdd_sagrad_1091_1098.bin",
36 };
37 
38 /* Allow platform data to be overridden */
39 static struct cw1200_platform_data_sdio *global_plat_data = &sagrad_109x_evk_platform_data;
40 
cw1200_sdio_set_platform_data(struct cw1200_platform_data_sdio * pdata)41 void __init cw1200_sdio_set_platform_data(struct cw1200_platform_data_sdio *pdata)
42 {
43 	global_plat_data = pdata;
44 }
45 
46 struct hwbus_priv {
47 	struct sdio_func	*func;
48 	struct cw1200_common	*core;
49 	const struct cw1200_platform_data_sdio *pdata;
50 };
51 
52 static const struct sdio_device_id cw1200_sdio_ids[] = {
53 	{ SDIO_DEVICE(SDIO_VENDOR_ID_STE, SDIO_DEVICE_ID_STE_CW1200) },
54 	{ /* end: all zeroes */			},
55 };
56 MODULE_DEVICE_TABLE(sdio, cw1200_sdio_ids);
57 
58 /* hwbus_ops implemetation */
59 
cw1200_sdio_memcpy_fromio(struct hwbus_priv * self,unsigned int addr,void * dst,int count)60 static int cw1200_sdio_memcpy_fromio(struct hwbus_priv *self,
61 				     unsigned int addr,
62 				     void *dst, int count)
63 {
64 	return sdio_memcpy_fromio(self->func, dst, addr, count);
65 }
66 
cw1200_sdio_memcpy_toio(struct hwbus_priv * self,unsigned int addr,const void * src,int count)67 static int cw1200_sdio_memcpy_toio(struct hwbus_priv *self,
68 				   unsigned int addr,
69 				   const void *src, int count)
70 {
71 	return sdio_memcpy_toio(self->func, addr, (void *)src, count);
72 }
73 
cw1200_sdio_lock(struct hwbus_priv * self)74 static void cw1200_sdio_lock(struct hwbus_priv *self)
75 {
76 	sdio_claim_host(self->func);
77 }
78 
cw1200_sdio_unlock(struct hwbus_priv * self)79 static void cw1200_sdio_unlock(struct hwbus_priv *self)
80 {
81 	sdio_release_host(self->func);
82 }
83 
cw1200_sdio_irq_handler(struct sdio_func * func)84 static void cw1200_sdio_irq_handler(struct sdio_func *func)
85 {
86 	struct hwbus_priv *self = sdio_get_drvdata(func);
87 
88 	/* note:  sdio_host already claimed here. */
89 	if (self->core)
90 		cw1200_irq_handler(self->core);
91 }
92 
cw1200_gpio_hardirq(int irq,void * dev_id)93 static irqreturn_t cw1200_gpio_hardirq(int irq, void *dev_id)
94 {
95 	return IRQ_WAKE_THREAD;
96 }
97 
cw1200_gpio_irq(int irq,void * dev_id)98 static irqreturn_t cw1200_gpio_irq(int irq, void *dev_id)
99 {
100 	struct hwbus_priv *self = dev_id;
101 
102 	if (self->core) {
103 		cw1200_sdio_lock(self);
104 		cw1200_irq_handler(self->core);
105 		cw1200_sdio_unlock(self);
106 		return IRQ_HANDLED;
107 	} else {
108 		return IRQ_NONE;
109 	}
110 }
111 
cw1200_request_irq(struct hwbus_priv * self)112 static int cw1200_request_irq(struct hwbus_priv *self)
113 {
114 	int ret;
115 	u8 cccr;
116 
117 	cccr = sdio_f0_readb(self->func, SDIO_CCCR_IENx, &ret);
118 	if (WARN_ON(ret))
119 		goto err;
120 
121 	/* Master interrupt enable ... */
122 	cccr |= BIT(0);
123 
124 	/* ... for our function */
125 	cccr |= BIT(self->func->num);
126 
127 	sdio_f0_writeb(self->func, cccr, SDIO_CCCR_IENx, &ret);
128 	if (WARN_ON(ret))
129 		goto err;
130 
131 	ret = enable_irq_wake(self->pdata->irq);
132 	if (WARN_ON(ret))
133 		goto err;
134 
135 	/* Request the IRQ */
136 	ret =  request_threaded_irq(self->pdata->irq, cw1200_gpio_hardirq,
137 				    cw1200_gpio_irq,
138 				    IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
139 				    "cw1200_wlan_irq", self);
140 	if (WARN_ON(ret))
141 		goto err;
142 
143 	return 0;
144 
145 err:
146 	return ret;
147 }
148 
cw1200_sdio_irq_subscribe(struct hwbus_priv * self)149 static int cw1200_sdio_irq_subscribe(struct hwbus_priv *self)
150 {
151 	int ret = 0;
152 
153 	pr_debug("SW IRQ subscribe\n");
154 	sdio_claim_host(self->func);
155 	if (self->pdata->irq)
156 		ret = cw1200_request_irq(self);
157 	else
158 		ret = sdio_claim_irq(self->func, cw1200_sdio_irq_handler);
159 
160 	sdio_release_host(self->func);
161 	return ret;
162 }
163 
cw1200_sdio_irq_unsubscribe(struct hwbus_priv * self)164 static int cw1200_sdio_irq_unsubscribe(struct hwbus_priv *self)
165 {
166 	int ret = 0;
167 
168 	pr_debug("SW IRQ unsubscribe\n");
169 
170 	if (self->pdata->irq) {
171 		disable_irq_wake(self->pdata->irq);
172 		free_irq(self->pdata->irq, self);
173 	} else {
174 		sdio_claim_host(self->func);
175 		ret = sdio_release_irq(self->func);
176 		sdio_release_host(self->func);
177 	}
178 	return ret;
179 }
180 
181 /* Like the rest of the driver, this only supports one device per system */
182 static struct gpio_desc *cw1200_reset;
183 static struct gpio_desc *cw1200_powerup;
184 
cw1200_sdio_off(const struct cw1200_platform_data_sdio * pdata)185 static int cw1200_sdio_off(const struct cw1200_platform_data_sdio *pdata)
186 {
187 	if (cw1200_reset) {
188 		gpiod_set_value(cw1200_reset, 0);
189 		msleep(30); /* Min is 2 * CLK32K cycles */
190 	}
191 
192 	if (pdata->power_ctrl)
193 		pdata->power_ctrl(pdata, false);
194 	if (pdata->clk_ctrl)
195 		pdata->clk_ctrl(pdata, false);
196 
197 	return 0;
198 }
199 
cw1200_sdio_on(const struct cw1200_platform_data_sdio * pdata)200 static int cw1200_sdio_on(const struct cw1200_platform_data_sdio *pdata)
201 {
202 	/* Ensure I/Os are pulled low (reset is active low) */
203 	cw1200_reset = devm_gpiod_get_optional(NULL, "reset", GPIOD_OUT_HIGH);
204 	if (IS_ERR(cw1200_reset)) {
205 		pr_err("could not get CW1200 SDIO reset GPIO\n");
206 		return PTR_ERR(cw1200_reset);
207 	}
208 	gpiod_set_consumer_name(cw1200_reset, "cw1200_wlan_reset");
209 	cw1200_powerup = devm_gpiod_get_optional(NULL, "powerup", GPIOD_OUT_LOW);
210 	if (IS_ERR(cw1200_powerup)) {
211 		pr_err("could not get CW1200 SDIO powerup GPIO\n");
212 		return PTR_ERR(cw1200_powerup);
213 	}
214 	gpiod_set_consumer_name(cw1200_powerup, "cw1200_wlan_powerup");
215 
216 	if (cw1200_reset || cw1200_powerup)
217 		msleep(10); /* Settle time? */
218 
219 	/* Enable 3v3 and 1v8 to hardware */
220 	if (pdata->power_ctrl) {
221 		if (pdata->power_ctrl(pdata, true)) {
222 			pr_err("power_ctrl() failed!\n");
223 			return -1;
224 		}
225 	}
226 
227 	/* Enable CLK32K */
228 	if (pdata->clk_ctrl) {
229 		if (pdata->clk_ctrl(pdata, true)) {
230 			pr_err("clk_ctrl() failed!\n");
231 			return -1;
232 		}
233 		msleep(10); /* Delay until clock is stable for 2 cycles */
234 	}
235 
236 	/* Enable POWERUP signal */
237 	if (cw1200_powerup) {
238 		gpiod_set_value(cw1200_powerup, 1);
239 		msleep(250); /* or more..? */
240 	}
241 	/* Deassert RSTn signal, note active low */
242 	if (cw1200_reset) {
243 		gpiod_set_value(cw1200_reset, 0);
244 		msleep(50); /* Or more..? */
245 	}
246 	return 0;
247 }
248 
cw1200_sdio_align_size(struct hwbus_priv * self,size_t size)249 static size_t cw1200_sdio_align_size(struct hwbus_priv *self, size_t size)
250 {
251 	if (self->pdata->no_nptb)
252 		size = round_up(size, SDIO_BLOCK_SIZE);
253 	else
254 		size = sdio_align_size(self->func, size);
255 
256 	return size;
257 }
258 
cw1200_sdio_pm(struct hwbus_priv * self,bool suspend)259 static int cw1200_sdio_pm(struct hwbus_priv *self, bool suspend)
260 {
261 	int ret = 0;
262 
263 	if (self->pdata->irq)
264 		ret = irq_set_irq_wake(self->pdata->irq, suspend);
265 	return ret;
266 }
267 
268 static const struct hwbus_ops cw1200_sdio_hwbus_ops = {
269 	.hwbus_memcpy_fromio	= cw1200_sdio_memcpy_fromio,
270 	.hwbus_memcpy_toio	= cw1200_sdio_memcpy_toio,
271 	.lock			= cw1200_sdio_lock,
272 	.unlock			= cw1200_sdio_unlock,
273 	.align_size		= cw1200_sdio_align_size,
274 	.power_mgmt		= cw1200_sdio_pm,
275 };
276 
277 /* Probe Function to be called by SDIO stack when device is discovered */
cw1200_sdio_probe(struct sdio_func * func,const struct sdio_device_id * id)278 static int cw1200_sdio_probe(struct sdio_func *func,
279 			     const struct sdio_device_id *id)
280 {
281 	struct hwbus_priv *self;
282 	int status;
283 
284 	pr_info("cw1200_wlan_sdio: Probe called\n");
285 
286 	/* We are only able to handle the wlan function */
287 	if (func->num != 0x01)
288 		return -ENODEV;
289 
290 	self = kzalloc(sizeof(*self), GFP_KERNEL);
291 	if (!self) {
292 		pr_err("Can't allocate SDIO hwbus_priv.\n");
293 		return -ENOMEM;
294 	}
295 
296 	func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
297 
298 	self->pdata = global_plat_data; /* FIXME */
299 	self->func = func;
300 	sdio_set_drvdata(func, self);
301 	sdio_claim_host(func);
302 	sdio_enable_func(func);
303 	sdio_release_host(func);
304 
305 	status = cw1200_sdio_irq_subscribe(self);
306 
307 	status = cw1200_core_probe(&cw1200_sdio_hwbus_ops,
308 				   self, &func->dev, &self->core,
309 				   self->pdata->ref_clk,
310 				   self->pdata->macaddr,
311 				   self->pdata->sdd_file,
312 				   self->pdata->have_5ghz);
313 	if (status) {
314 		cw1200_sdio_irq_unsubscribe(self);
315 		sdio_claim_host(func);
316 		sdio_disable_func(func);
317 		sdio_release_host(func);
318 		sdio_set_drvdata(func, NULL);
319 		kfree(self);
320 	}
321 
322 	return status;
323 }
324 
325 /* Disconnect Function to be called by SDIO stack when
326  * device is disconnected
327  */
cw1200_sdio_disconnect(struct sdio_func * func)328 static void cw1200_sdio_disconnect(struct sdio_func *func)
329 {
330 	struct hwbus_priv *self = sdio_get_drvdata(func);
331 
332 	if (self) {
333 		cw1200_sdio_irq_unsubscribe(self);
334 		if (self->core) {
335 			cw1200_core_release(self->core);
336 			self->core = NULL;
337 		}
338 		sdio_claim_host(func);
339 		sdio_disable_func(func);
340 		sdio_release_host(func);
341 		sdio_set_drvdata(func, NULL);
342 		kfree(self);
343 	}
344 }
345 
346 #ifdef CONFIG_PM
cw1200_sdio_suspend(struct device * dev)347 static int cw1200_sdio_suspend(struct device *dev)
348 {
349 	int ret;
350 	struct sdio_func *func = dev_to_sdio_func(dev);
351 	struct hwbus_priv *self = sdio_get_drvdata(func);
352 
353 	if (!cw1200_can_suspend(self->core))
354 		return -EAGAIN;
355 
356 	/* Notify SDIO that CW1200 will remain powered during suspend */
357 	ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
358 	if (ret)
359 		pr_err("Error setting SDIO pm flags: %i\n", ret);
360 
361 	return ret;
362 }
363 
cw1200_sdio_resume(struct device * dev)364 static int cw1200_sdio_resume(struct device *dev)
365 {
366 	return 0;
367 }
368 
369 static const struct dev_pm_ops cw1200_pm_ops = {
370 	.suspend = cw1200_sdio_suspend,
371 	.resume = cw1200_sdio_resume,
372 };
373 #endif
374 
375 static struct sdio_driver sdio_driver = {
376 	.name		= "cw1200_wlan_sdio",
377 	.id_table	= cw1200_sdio_ids,
378 	.probe		= cw1200_sdio_probe,
379 	.remove		= cw1200_sdio_disconnect,
380 #ifdef CONFIG_PM
381 	.drv = {
382 		.pm = &cw1200_pm_ops,
383 	}
384 #endif
385 };
386 
387 /* Init Module function -> Called by insmod */
cw1200_sdio_init(void)388 static int __init cw1200_sdio_init(void)
389 {
390 	const struct cw1200_platform_data_sdio *pdata;
391 	int ret;
392 
393 	/* FIXME -- this won't support multiple devices */
394 	pdata = global_plat_data;
395 
396 	if (cw1200_sdio_on(pdata)) {
397 		ret = -1;
398 		goto err;
399 	}
400 
401 	ret = sdio_register_driver(&sdio_driver);
402 	if (ret)
403 		goto err;
404 
405 	return 0;
406 
407 err:
408 	cw1200_sdio_off(pdata);
409 	return ret;
410 }
411 
412 /* Called at Driver Unloading */
cw1200_sdio_exit(void)413 static void __exit cw1200_sdio_exit(void)
414 {
415 	const struct cw1200_platform_data_sdio *pdata;
416 
417 	/* FIXME -- this won't support multiple devices */
418 	pdata = global_plat_data;
419 	sdio_unregister_driver(&sdio_driver);
420 	cw1200_sdio_off(pdata);
421 }
422 
423 
424 module_init(cw1200_sdio_init);
425 module_exit(cw1200_sdio_exit);
426