sdio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/irq.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/of.h>
  18. #include <linux/of_irq.h>
  19. #include "wl1251.h"
  20. struct wl1251_sdio {
  21. struct sdio_func *func;
  22. u32 elp_val;
  23. };
  24. static struct sdio_func *wl_to_func(struct wl1251 *wl)
  25. {
  26. struct wl1251_sdio *wl_sdio = wl->if_priv;
  27. return wl_sdio->func;
  28. }
  29. static void wl1251_sdio_interrupt(struct sdio_func *func)
  30. {
  31. struct wl1251 *wl = sdio_get_drvdata(func);
  32. wl1251_debug(DEBUG_IRQ, "IRQ");
  33. /* FIXME should be synchronous for sdio */
  34. ieee80211_queue_work(wl->hw, &wl->irq_work);
  35. }
  36. static const struct sdio_device_id wl1251_devices[] = {
  37. { SDIO_DEVICE(SDIO_VENDOR_ID_TI_WL1251, SDIO_DEVICE_ID_TI_WL1251) },
  38. {}
  39. };
  40. MODULE_DEVICE_TABLE(sdio, wl1251_devices);
  41. static void wl1251_sdio_read(struct wl1251 *wl, int addr,
  42. void *buf, size_t len)
  43. {
  44. int ret;
  45. struct sdio_func *func = wl_to_func(wl);
  46. sdio_claim_host(func);
  47. ret = sdio_memcpy_fromio(func, buf, addr, len);
  48. if (ret)
  49. wl1251_error("sdio read failed (%d)", ret);
  50. sdio_release_host(func);
  51. }
  52. static void wl1251_sdio_write(struct wl1251 *wl, int addr,
  53. void *buf, size_t len)
  54. {
  55. int ret;
  56. struct sdio_func *func = wl_to_func(wl);
  57. sdio_claim_host(func);
  58. ret = sdio_memcpy_toio(func, addr, buf, len);
  59. if (ret)
  60. wl1251_error("sdio write failed (%d)", ret);
  61. sdio_release_host(func);
  62. }
  63. static void wl1251_sdio_read_elp(struct wl1251 *wl, int addr, u32 *val)
  64. {
  65. int ret = 0;
  66. struct wl1251_sdio *wl_sdio = wl->if_priv;
  67. struct sdio_func *func = wl_sdio->func;
  68. /*
  69. * The hardware only supports RAW (read after write) access for
  70. * reading, regular sdio_readb won't work here (it interprets
  71. * the unused bits of CMD52 as write data even if we send read
  72. * request).
  73. */
  74. sdio_claim_host(func);
  75. *val = sdio_writeb_readb(func, wl_sdio->elp_val, addr, &ret);
  76. sdio_release_host(func);
  77. if (ret)
  78. wl1251_error("sdio_readb failed (%d)", ret);
  79. }
  80. static void wl1251_sdio_write_elp(struct wl1251 *wl, int addr, u32 val)
  81. {
  82. int ret = 0;
  83. struct wl1251_sdio *wl_sdio = wl->if_priv;
  84. struct sdio_func *func = wl_sdio->func;
  85. sdio_claim_host(func);
  86. sdio_writeb(func, val, addr, &ret);
  87. sdio_release_host(func);
  88. if (ret)
  89. wl1251_error("sdio_writeb failed (%d)", ret);
  90. else
  91. wl_sdio->elp_val = val;
  92. }
  93. static void wl1251_sdio_reset(struct wl1251 *wl)
  94. {
  95. }
  96. static void wl1251_sdio_enable_irq(struct wl1251 *wl)
  97. {
  98. struct sdio_func *func = wl_to_func(wl);
  99. sdio_claim_host(func);
  100. sdio_claim_irq(func, wl1251_sdio_interrupt);
  101. sdio_release_host(func);
  102. }
  103. static void wl1251_sdio_disable_irq(struct wl1251 *wl)
  104. {
  105. struct sdio_func *func = wl_to_func(wl);
  106. sdio_claim_host(func);
  107. sdio_release_irq(func);
  108. sdio_release_host(func);
  109. }
  110. /* Interrupts when using dedicated WLAN_IRQ pin */
  111. static irqreturn_t wl1251_line_irq(int irq, void *cookie)
  112. {
  113. struct wl1251 *wl = cookie;
  114. ieee80211_queue_work(wl->hw, &wl->irq_work);
  115. return IRQ_HANDLED;
  116. }
  117. static void wl1251_enable_line_irq(struct wl1251 *wl)
  118. {
  119. return enable_irq(wl->irq);
  120. }
  121. static void wl1251_disable_line_irq(struct wl1251 *wl)
  122. {
  123. return disable_irq(wl->irq);
  124. }
  125. static int wl1251_sdio_set_power(struct wl1251 *wl, bool enable)
  126. {
  127. struct sdio_func *func = wl_to_func(wl);
  128. int ret;
  129. if (enable) {
  130. ret = pm_runtime_get_sync(&func->dev);
  131. if (ret < 0) {
  132. pm_runtime_put_sync(&func->dev);
  133. goto out;
  134. }
  135. sdio_claim_host(func);
  136. sdio_enable_func(func);
  137. sdio_release_host(func);
  138. } else {
  139. sdio_claim_host(func);
  140. sdio_disable_func(func);
  141. sdio_release_host(func);
  142. ret = pm_runtime_put_sync(&func->dev);
  143. if (ret < 0)
  144. goto out;
  145. }
  146. out:
  147. return ret;
  148. }
  149. static struct wl1251_if_operations wl1251_sdio_ops = {
  150. .read = wl1251_sdio_read,
  151. .write = wl1251_sdio_write,
  152. .write_elp = wl1251_sdio_write_elp,
  153. .read_elp = wl1251_sdio_read_elp,
  154. .reset = wl1251_sdio_reset,
  155. .power = wl1251_sdio_set_power,
  156. };
  157. static int wl1251_sdio_probe(struct sdio_func *func,
  158. const struct sdio_device_id *id)
  159. {
  160. int ret;
  161. struct wl1251 *wl;
  162. struct ieee80211_hw *hw;
  163. struct wl1251_sdio *wl_sdio;
  164. struct device_node *np = func->dev.of_node;
  165. hw = wl1251_alloc_hw();
  166. if (IS_ERR(hw))
  167. return PTR_ERR(hw);
  168. wl = hw->priv;
  169. wl_sdio = kzalloc_obj(*wl_sdio);
  170. if (wl_sdio == NULL) {
  171. ret = -ENOMEM;
  172. goto out_free_hw;
  173. }
  174. sdio_claim_host(func);
  175. ret = sdio_enable_func(func);
  176. if (ret)
  177. goto release;
  178. sdio_set_block_size(func, 512);
  179. sdio_release_host(func);
  180. SET_IEEE80211_DEV(hw, &func->dev);
  181. wl_sdio->func = func;
  182. wl->if_priv = wl_sdio;
  183. wl->if_ops = &wl1251_sdio_ops;
  184. if (np) {
  185. wl->use_eeprom = of_property_read_bool(np, "ti,wl1251-has-eeprom");
  186. wl->irq = of_irq_get(np, 0);
  187. if (wl->irq == -EPROBE_DEFER) {
  188. ret = -EPROBE_DEFER;
  189. goto disable;
  190. }
  191. }
  192. if (wl->irq) {
  193. ret = request_irq(wl->irq, wl1251_line_irq, IRQF_NO_AUTOEN,
  194. "wl1251", wl);
  195. if (ret < 0) {
  196. wl1251_error("request_irq() failed: %d", ret);
  197. goto disable;
  198. }
  199. irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  200. wl1251_sdio_ops.enable_irq = wl1251_enable_line_irq;
  201. wl1251_sdio_ops.disable_irq = wl1251_disable_line_irq;
  202. wl1251_info("using dedicated interrupt line");
  203. } else {
  204. wl1251_sdio_ops.enable_irq = wl1251_sdio_enable_irq;
  205. wl1251_sdio_ops.disable_irq = wl1251_sdio_disable_irq;
  206. wl1251_info("using SDIO interrupt");
  207. }
  208. ret = wl1251_init_ieee80211(wl);
  209. if (ret)
  210. goto out_free_irq;
  211. sdio_set_drvdata(func, wl);
  212. /* Tell PM core that we don't need the card to be powered now */
  213. pm_runtime_put_noidle(&func->dev);
  214. return ret;
  215. out_free_irq:
  216. if (wl->irq)
  217. free_irq(wl->irq, wl);
  218. disable:
  219. sdio_claim_host(func);
  220. sdio_disable_func(func);
  221. release:
  222. sdio_release_host(func);
  223. kfree(wl_sdio);
  224. out_free_hw:
  225. wl1251_free_hw(wl);
  226. return ret;
  227. }
  228. static void wl1251_sdio_remove(struct sdio_func *func)
  229. {
  230. struct wl1251 *wl = sdio_get_drvdata(func);
  231. struct wl1251_sdio *wl_sdio = wl->if_priv;
  232. /* Undo decrement done above in wl1251_probe */
  233. pm_runtime_get_noresume(&func->dev);
  234. if (wl->irq)
  235. free_irq(wl->irq, wl);
  236. wl1251_free_hw(wl);
  237. kfree(wl_sdio);
  238. sdio_claim_host(func);
  239. sdio_release_irq(func);
  240. sdio_disable_func(func);
  241. sdio_release_host(func);
  242. }
  243. static int wl1251_suspend(struct device *dev)
  244. {
  245. /*
  246. * Tell MMC/SDIO core it's OK to power down the card
  247. * (if it isn't already), but not to remove it completely.
  248. */
  249. return 0;
  250. }
  251. static int wl1251_resume(struct device *dev)
  252. {
  253. return 0;
  254. }
  255. static const struct dev_pm_ops wl1251_sdio_pm_ops = {
  256. .suspend = wl1251_suspend,
  257. .resume = wl1251_resume,
  258. };
  259. static struct sdio_driver wl1251_sdio_driver = {
  260. .name = "wl1251_sdio",
  261. .id_table = wl1251_devices,
  262. .probe = wl1251_sdio_probe,
  263. .remove = wl1251_sdio_remove,
  264. .drv.pm = &wl1251_sdio_pm_ops,
  265. };
  266. module_sdio_driver(wl1251_sdio_driver);
  267. MODULE_DESCRIPTION("TI WL1251 SDIO helpers");
  268. MODULE_LICENSE("GPL");
  269. MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");