cw1200_sdio.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Mac80211 SDIO driver for ST-Ericsson CW1200 device
  3. *
  4. * Copyright (c) 2010, ST-Ericsson
  5. * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/gpio.h>
  13. #include <linux/delay.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/sdio_func.h>
  16. #include <linux/mmc/card.h>
  17. #include <linux/mmc/sdio.h>
  18. #include <net/mac80211.h>
  19. #include "cw1200.h"
  20. #include "hwbus.h"
  21. #include <linux/platform_data/cw1200_platform.h>
  22. #include "hwio.h"
  23. MODULE_AUTHOR("Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>");
  24. MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SDIO driver");
  25. MODULE_LICENSE("GPL");
  26. #define SDIO_BLOCK_SIZE (512)
  27. struct hwbus_priv {
  28. struct sdio_func *func;
  29. struct cw1200_common *core;
  30. const struct cw1200_platform_data_sdio *pdata;
  31. };
  32. #ifndef SDIO_VENDOR_ID_STE
  33. #define SDIO_VENDOR_ID_STE 0x0020
  34. #endif
  35. #ifndef SDIO_DEVICE_ID_STE_CW1200
  36. #define SDIO_DEVICE_ID_STE_CW1200 0x2280
  37. #endif
  38. static const struct sdio_device_id cw1200_sdio_ids[] = {
  39. { SDIO_DEVICE(SDIO_VENDOR_ID_STE, SDIO_DEVICE_ID_STE_CW1200) },
  40. { /* end: all zeroes */ },
  41. };
  42. /* hwbus_ops implemetation */
  43. static int cw1200_sdio_memcpy_fromio(struct hwbus_priv *self,
  44. unsigned int addr,
  45. void *dst, int count)
  46. {
  47. return sdio_memcpy_fromio(self->func, dst, addr, count);
  48. }
  49. static int cw1200_sdio_memcpy_toio(struct hwbus_priv *self,
  50. unsigned int addr,
  51. const void *src, int count)
  52. {
  53. return sdio_memcpy_toio(self->func, addr, (void *)src, count);
  54. }
  55. static void cw1200_sdio_lock(struct hwbus_priv *self)
  56. {
  57. sdio_claim_host(self->func);
  58. }
  59. static void cw1200_sdio_unlock(struct hwbus_priv *self)
  60. {
  61. sdio_release_host(self->func);
  62. }
  63. static void cw1200_sdio_irq_handler(struct sdio_func *func)
  64. {
  65. struct hwbus_priv *self = sdio_get_drvdata(func);
  66. /* note: sdio_host already claimed here. */
  67. if (self->core)
  68. cw1200_irq_handler(self->core);
  69. }
  70. static irqreturn_t cw1200_gpio_hardirq(int irq, void *dev_id)
  71. {
  72. return IRQ_WAKE_THREAD;
  73. }
  74. static irqreturn_t cw1200_gpio_irq(int irq, void *dev_id)
  75. {
  76. struct hwbus_priv *self = dev_id;
  77. if (self->core) {
  78. sdio_claim_host(self->func);
  79. cw1200_irq_handler(self->core);
  80. sdio_release_host(self->func);
  81. return IRQ_HANDLED;
  82. } else {
  83. return IRQ_NONE;
  84. }
  85. }
  86. static int cw1200_request_irq(struct hwbus_priv *self)
  87. {
  88. int ret;
  89. const struct resource *irq = self->pdata->irq;
  90. u8 cccr;
  91. cccr = sdio_f0_readb(self->func, SDIO_CCCR_IENx, &ret);
  92. if (WARN_ON(ret))
  93. goto err;
  94. /* Master interrupt enable ... */
  95. cccr |= BIT(0);
  96. /* ... for our function */
  97. cccr |= BIT(self->func->num);
  98. sdio_f0_writeb(self->func, cccr, SDIO_CCCR_IENx, &ret);
  99. if (WARN_ON(ret))
  100. goto err;
  101. ret = enable_irq_wake(irq->start);
  102. if (WARN_ON(ret))
  103. goto err;
  104. /* Request the IRQ */
  105. ret = request_threaded_irq(irq->start, cw1200_gpio_hardirq,
  106. cw1200_gpio_irq,
  107. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  108. irq->name, self);
  109. if (WARN_ON(ret))
  110. goto err;
  111. return 0;
  112. err:
  113. return ret;
  114. }
  115. static int cw1200_sdio_irq_subscribe(struct hwbus_priv *self)
  116. {
  117. int ret = 0;
  118. pr_debug("SW IRQ subscribe\n");
  119. sdio_claim_host(self->func);
  120. if (self->pdata->irq)
  121. ret = cw1200_request_irq(self);
  122. else
  123. ret = sdio_claim_irq(self->func, cw1200_sdio_irq_handler);
  124. sdio_release_host(self->func);
  125. return ret;
  126. }
  127. static int cw1200_sdio_irq_unsubscribe(struct hwbus_priv *self)
  128. {
  129. int ret = 0;
  130. pr_debug("SW IRQ unsubscribe\n");
  131. if (self->pdata->irq) {
  132. disable_irq_wake(self->pdata->irq->start);
  133. free_irq(self->pdata->irq->start, self);
  134. } else {
  135. sdio_claim_host(self->func);
  136. ret = sdio_release_irq(self->func);
  137. sdio_release_host(self->func);
  138. }
  139. return ret;
  140. }
  141. static int cw1200_sdio_off(const struct cw1200_platform_data_sdio *pdata)
  142. {
  143. const struct resource *reset = pdata->reset;
  144. if (reset) {
  145. gpio_set_value(reset->start, 0);
  146. msleep(30); /* Min is 2 * CLK32K cycles */
  147. gpio_free(reset->start);
  148. }
  149. if (pdata->power_ctrl)
  150. pdata->power_ctrl(pdata, false);
  151. if (pdata->clk_ctrl)
  152. pdata->clk_ctrl(pdata, false);
  153. return 0;
  154. }
  155. static int cw1200_sdio_on(const struct cw1200_platform_data_sdio *pdata)
  156. {
  157. const struct resource *reset = pdata->reset;
  158. const struct resource *powerup = pdata->powerup;
  159. /* Ensure I/Os are pulled low */
  160. if (reset) {
  161. gpio_request(reset->start, reset->name);
  162. gpio_direction_output(reset->start, 0);
  163. }
  164. if (powerup) {
  165. gpio_request(powerup->start, powerup->name);
  166. gpio_direction_output(powerup->start, 0);
  167. }
  168. if (reset || powerup)
  169. msleep(50); /* Settle time */
  170. /* Enable 3v3 and 1v8 to hardware */
  171. if (pdata->power_ctrl) {
  172. if (pdata->power_ctrl(pdata, true)) {
  173. pr_err("power_ctrl() failed!\n");
  174. return -1;
  175. }
  176. }
  177. /* Enable CLK32K */
  178. if (pdata->clk_ctrl) {
  179. if (pdata->clk_ctrl(pdata, true)) {
  180. pr_err("clk_ctrl() failed!\n");
  181. return -1;
  182. }
  183. msleep(10); /* Delay until clock is stable for 2 cycles */
  184. }
  185. /* Enable POWERUP signal */
  186. if (powerup) {
  187. gpio_set_value(powerup->start, 1);
  188. msleep(250); /* or more..? */
  189. }
  190. /* Enable RSTn signal */
  191. if (reset) {
  192. gpio_set_value(reset->start, 1);
  193. msleep(50); /* Or more..? */
  194. }
  195. return 0;
  196. }
  197. static size_t cw1200_sdio_align_size(struct hwbus_priv *self, size_t size)
  198. {
  199. if (self->pdata->no_nptb)
  200. size = round_up(size, SDIO_BLOCK_SIZE);
  201. else
  202. size = sdio_align_size(self->func, size);
  203. return size;
  204. }
  205. static int cw1200_sdio_pm(struct hwbus_priv *self, bool suspend)
  206. {
  207. int ret = 0;
  208. if (self->pdata->irq)
  209. ret = irq_set_irq_wake(self->pdata->irq->start, suspend);
  210. return ret;
  211. }
  212. static struct hwbus_ops cw1200_sdio_hwbus_ops = {
  213. .hwbus_memcpy_fromio = cw1200_sdio_memcpy_fromio,
  214. .hwbus_memcpy_toio = cw1200_sdio_memcpy_toio,
  215. .lock = cw1200_sdio_lock,
  216. .unlock = cw1200_sdio_unlock,
  217. .align_size = cw1200_sdio_align_size,
  218. .power_mgmt = cw1200_sdio_pm,
  219. };
  220. /* Probe Function to be called by SDIO stack when device is discovered */
  221. static int cw1200_sdio_probe(struct sdio_func *func,
  222. const struct sdio_device_id *id)
  223. {
  224. struct hwbus_priv *self;
  225. int status;
  226. pr_info("cw1200_wlan_sdio: Probe called\n");
  227. /* We are only able to handle the wlan function */
  228. if (func->num != 0x01)
  229. return -ENODEV;
  230. self = kzalloc(sizeof(*self), GFP_KERNEL);
  231. if (!self) {
  232. pr_err("Can't allocate SDIO hwbus_priv.\n");
  233. return -ENOMEM;
  234. }
  235. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  236. self->pdata = cw1200_get_platform_data();
  237. self->func = func;
  238. sdio_set_drvdata(func, self);
  239. sdio_claim_host(func);
  240. sdio_enable_func(func);
  241. sdio_release_host(func);
  242. status = cw1200_sdio_irq_subscribe(self);
  243. status = cw1200_core_probe(&cw1200_sdio_hwbus_ops,
  244. self, &func->dev, &self->core,
  245. self->pdata->ref_clk,
  246. self->pdata->macaddr,
  247. self->pdata->sdd_file,
  248. self->pdata->have_5ghz);
  249. if (status) {
  250. cw1200_sdio_irq_unsubscribe(self);
  251. sdio_claim_host(func);
  252. sdio_disable_func(func);
  253. sdio_release_host(func);
  254. sdio_set_drvdata(func, NULL);
  255. kfree(self);
  256. }
  257. return status;
  258. }
  259. /* Disconnect Function to be called by SDIO stack when
  260. * device is disconnected */
  261. static void cw1200_sdio_disconnect(struct sdio_func *func)
  262. {
  263. struct hwbus_priv *self = sdio_get_drvdata(func);
  264. if (self) {
  265. cw1200_sdio_irq_unsubscribe(self);
  266. if (self->core) {
  267. cw1200_core_release(self->core);
  268. self->core = NULL;
  269. }
  270. sdio_claim_host(func);
  271. sdio_disable_func(func);
  272. sdio_release_host(func);
  273. sdio_set_drvdata(func, NULL);
  274. kfree(self);
  275. }
  276. }
  277. #ifdef CONFIG_PM
  278. static int cw1200_sdio_suspend(struct device *dev)
  279. {
  280. int ret;
  281. struct sdio_func *func = dev_to_sdio_func(dev);
  282. struct hwbus_priv *self = sdio_get_drvdata(func);
  283. if (!cw1200_can_suspend(self->core))
  284. return -EAGAIN;
  285. /* Notify SDIO that CW1200 will remain powered during suspend */
  286. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  287. if (ret)
  288. pr_err("Error setting SDIO pm flags: %i\n", ret);
  289. return ret;
  290. }
  291. static int cw1200_sdio_resume(struct device *dev)
  292. {
  293. return 0;
  294. }
  295. static const struct dev_pm_ops cw1200_pm_ops = {
  296. .suspend = cw1200_sdio_suspend,
  297. .resume = cw1200_sdio_resume,
  298. };
  299. #endif
  300. static struct sdio_driver sdio_driver = {
  301. .name = "cw1200_wlan_sdio",
  302. .id_table = cw1200_sdio_ids,
  303. .probe = cw1200_sdio_probe,
  304. .remove = cw1200_sdio_disconnect,
  305. #ifdef CONFIG_PM
  306. .drv = {
  307. .pm = &cw1200_pm_ops,
  308. }
  309. #endif
  310. };
  311. /* Init Module function -> Called by insmod */
  312. static int __init cw1200_sdio_init(void)
  313. {
  314. const struct cw1200_platform_data_sdio *pdata;
  315. int ret;
  316. pdata = cw1200_get_platform_data();
  317. if (cw1200_sdio_on(pdata)) {
  318. ret = -1;
  319. goto err;
  320. }
  321. ret = sdio_register_driver(&sdio_driver);
  322. if (ret)
  323. goto err;
  324. return 0;
  325. err:
  326. cw1200_sdio_off(pdata);
  327. return ret;
  328. }
  329. /* Called at Driver Unloading */
  330. static void __exit cw1200_sdio_exit(void)
  331. {
  332. const struct cw1200_platform_data_sdio *pdata;
  333. pdata = cw1200_get_platform_data();
  334. sdio_unregister_driver(&sdio_driver);
  335. cw1200_sdio_off(pdata);
  336. }
  337. module_init(cw1200_sdio_init);
  338. module_exit(cw1200_sdio_exit);