sdio.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * wl12xx SDIO routines
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. *
  18. * Copyright (C) 2005 Texas Instruments Incorporated
  19. * Copyright (C) 2008 Google Inc
  20. * Copyright (C) 2009 Bob Copeland (me@bobcopeland.com)
  21. */
  22. #include <linux/module.h>
  23. #include <linux/mod_devicetable.h>
  24. #include <linux/mmc/sdio_func.h>
  25. #include <linux/mmc/sdio_ids.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/wl12xx.h>
  28. #include <linux/irq.h>
  29. #include <linux/pm_runtime.h>
  30. #include "wl1251.h"
  31. #ifndef SDIO_VENDOR_ID_TI
  32. #define SDIO_VENDOR_ID_TI 0x104c
  33. #endif
  34. #ifndef SDIO_DEVICE_ID_TI_WL1251
  35. #define SDIO_DEVICE_ID_TI_WL1251 0x9066
  36. #endif
  37. struct wl1251_sdio {
  38. struct sdio_func *func;
  39. u32 elp_val;
  40. };
  41. static struct sdio_func *wl_to_func(struct wl1251 *wl)
  42. {
  43. struct wl1251_sdio *wl_sdio = wl->if_priv;
  44. return wl_sdio->func;
  45. }
  46. static void wl1251_sdio_interrupt(struct sdio_func *func)
  47. {
  48. struct wl1251 *wl = sdio_get_drvdata(func);
  49. wl1251_debug(DEBUG_IRQ, "IRQ");
  50. /* FIXME should be synchronous for sdio */
  51. ieee80211_queue_work(wl->hw, &wl->irq_work);
  52. }
  53. static const struct sdio_device_id wl1251_devices[] = {
  54. { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1251) },
  55. {}
  56. };
  57. MODULE_DEVICE_TABLE(sdio, wl1251_devices);
  58. static void wl1251_sdio_read(struct wl1251 *wl, int addr,
  59. void *buf, size_t len)
  60. {
  61. int ret;
  62. struct sdio_func *func = wl_to_func(wl);
  63. sdio_claim_host(func);
  64. ret = sdio_memcpy_fromio(func, buf, addr, len);
  65. if (ret)
  66. wl1251_error("sdio read failed (%d)", ret);
  67. sdio_release_host(func);
  68. }
  69. static void wl1251_sdio_write(struct wl1251 *wl, int addr,
  70. void *buf, size_t len)
  71. {
  72. int ret;
  73. struct sdio_func *func = wl_to_func(wl);
  74. sdio_claim_host(func);
  75. ret = sdio_memcpy_toio(func, addr, buf, len);
  76. if (ret)
  77. wl1251_error("sdio write failed (%d)", ret);
  78. sdio_release_host(func);
  79. }
  80. static void wl1251_sdio_read_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. /*
  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. if (ret)
  95. wl1251_error("sdio_readb failed (%d)", ret);
  96. }
  97. static void wl1251_sdio_write_elp(struct wl1251 *wl, int addr, u32 val)
  98. {
  99. int ret = 0;
  100. struct wl1251_sdio *wl_sdio = wl->if_priv;
  101. struct sdio_func *func = wl_sdio->func;
  102. sdio_claim_host(func);
  103. sdio_writeb(func, val, addr, &ret);
  104. sdio_release_host(func);
  105. if (ret)
  106. wl1251_error("sdio_writeb failed (%d)", ret);
  107. else
  108. wl_sdio->elp_val = val;
  109. }
  110. static void wl1251_sdio_reset(struct wl1251 *wl)
  111. {
  112. }
  113. static void wl1251_sdio_enable_irq(struct wl1251 *wl)
  114. {
  115. struct sdio_func *func = wl_to_func(wl);
  116. sdio_claim_host(func);
  117. sdio_claim_irq(func, wl1251_sdio_interrupt);
  118. sdio_release_host(func);
  119. }
  120. static void wl1251_sdio_disable_irq(struct wl1251 *wl)
  121. {
  122. struct sdio_func *func = wl_to_func(wl);
  123. sdio_claim_host(func);
  124. sdio_release_irq(func);
  125. sdio_release_host(func);
  126. }
  127. /* Interrupts when using dedicated WLAN_IRQ pin */
  128. static irqreturn_t wl1251_line_irq(int irq, void *cookie)
  129. {
  130. struct wl1251 *wl = cookie;
  131. ieee80211_queue_work(wl->hw, &wl->irq_work);
  132. return IRQ_HANDLED;
  133. }
  134. static void wl1251_enable_line_irq(struct wl1251 *wl)
  135. {
  136. return enable_irq(wl->irq);
  137. }
  138. static void wl1251_disable_line_irq(struct wl1251 *wl)
  139. {
  140. return disable_irq(wl->irq);
  141. }
  142. static int wl1251_sdio_set_power(struct wl1251 *wl, bool enable)
  143. {
  144. struct sdio_func *func = wl_to_func(wl);
  145. int ret;
  146. if (enable) {
  147. /*
  148. * Power is controlled by runtime PM, but we still call board
  149. * callback in case it wants to do any additional setup,
  150. * for example enabling clock buffer for the module.
  151. */
  152. if (wl->set_power)
  153. wl->set_power(true);
  154. ret = pm_runtime_get_sync(&func->dev);
  155. if (ret < 0)
  156. goto out;
  157. sdio_claim_host(func);
  158. sdio_enable_func(func);
  159. sdio_release_host(func);
  160. } else {
  161. sdio_claim_host(func);
  162. sdio_disable_func(func);
  163. sdio_release_host(func);
  164. ret = pm_runtime_put_sync(&func->dev);
  165. if (ret < 0)
  166. goto out;
  167. if (wl->set_power)
  168. wl->set_power(false);
  169. }
  170. out:
  171. return ret;
  172. }
  173. static struct wl1251_if_operations wl1251_sdio_ops = {
  174. .read = wl1251_sdio_read,
  175. .write = wl1251_sdio_write,
  176. .write_elp = wl1251_sdio_write_elp,
  177. .read_elp = wl1251_sdio_read_elp,
  178. .reset = wl1251_sdio_reset,
  179. .power = wl1251_sdio_set_power,
  180. };
  181. static int wl1251_sdio_probe(struct sdio_func *func,
  182. const struct sdio_device_id *id)
  183. {
  184. int ret;
  185. struct wl1251 *wl;
  186. struct ieee80211_hw *hw;
  187. struct wl1251_sdio *wl_sdio;
  188. const struct wl12xx_platform_data *wl12xx_board_data;
  189. hw = wl1251_alloc_hw();
  190. if (IS_ERR(hw))
  191. return PTR_ERR(hw);
  192. wl = hw->priv;
  193. wl_sdio = kzalloc(sizeof(*wl_sdio), GFP_KERNEL);
  194. if (wl_sdio == NULL) {
  195. ret = -ENOMEM;
  196. goto out_free_hw;
  197. }
  198. sdio_claim_host(func);
  199. ret = sdio_enable_func(func);
  200. if (ret)
  201. goto release;
  202. sdio_set_block_size(func, 512);
  203. sdio_release_host(func);
  204. SET_IEEE80211_DEV(hw, &func->dev);
  205. wl_sdio->func = func;
  206. wl->if_priv = wl_sdio;
  207. wl->if_ops = &wl1251_sdio_ops;
  208. wl12xx_board_data = wl12xx_get_platform_data();
  209. if (wl12xx_board_data != NULL) {
  210. wl->set_power = wl12xx_board_data->set_power;
  211. wl->irq = wl12xx_board_data->irq;
  212. wl->use_eeprom = wl12xx_board_data->use_eeprom;
  213. }
  214. if (wl->irq) {
  215. ret = request_irq(wl->irq, wl1251_line_irq, 0, "wl1251", wl);
  216. if (ret < 0) {
  217. wl1251_error("request_irq() failed: %d", ret);
  218. goto disable;
  219. }
  220. set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  221. disable_irq(wl->irq);
  222. wl1251_sdio_ops.enable_irq = wl1251_enable_line_irq;
  223. wl1251_sdio_ops.disable_irq = wl1251_disable_line_irq;
  224. wl1251_info("using dedicated interrupt line");
  225. } else {
  226. wl1251_sdio_ops.enable_irq = wl1251_sdio_enable_irq;
  227. wl1251_sdio_ops.disable_irq = wl1251_sdio_disable_irq;
  228. wl1251_info("using SDIO interrupt");
  229. }
  230. ret = wl1251_init_ieee80211(wl);
  231. if (ret)
  232. goto out_free_irq;
  233. sdio_set_drvdata(func, wl);
  234. /* Tell PM core that we don't need the card to be powered now */
  235. pm_runtime_put_noidle(&func->dev);
  236. return ret;
  237. out_free_irq:
  238. if (wl->irq)
  239. free_irq(wl->irq, wl);
  240. disable:
  241. sdio_claim_host(func);
  242. sdio_disable_func(func);
  243. release:
  244. sdio_release_host(func);
  245. kfree(wl_sdio);
  246. out_free_hw:
  247. wl1251_free_hw(wl);
  248. return ret;
  249. }
  250. static void __devexit wl1251_sdio_remove(struct sdio_func *func)
  251. {
  252. struct wl1251 *wl = sdio_get_drvdata(func);
  253. struct wl1251_sdio *wl_sdio = wl->if_priv;
  254. /* Undo decrement done above in wl1251_probe */
  255. pm_runtime_get_noresume(&func->dev);
  256. if (wl->irq)
  257. free_irq(wl->irq, wl);
  258. kfree(wl_sdio);
  259. wl1251_free_hw(wl);
  260. sdio_claim_host(func);
  261. sdio_release_irq(func);
  262. sdio_disable_func(func);
  263. sdio_release_host(func);
  264. }
  265. static int wl1251_suspend(struct device *dev)
  266. {
  267. /*
  268. * Tell MMC/SDIO core it's OK to power down the card
  269. * (if it isn't already), but not to remove it completely.
  270. */
  271. return 0;
  272. }
  273. static int wl1251_resume(struct device *dev)
  274. {
  275. return 0;
  276. }
  277. static const struct dev_pm_ops wl1251_sdio_pm_ops = {
  278. .suspend = wl1251_suspend,
  279. .resume = wl1251_resume,
  280. };
  281. static struct sdio_driver wl1251_sdio_driver = {
  282. .name = "wl1251_sdio",
  283. .id_table = wl1251_devices,
  284. .probe = wl1251_sdio_probe,
  285. .remove = __devexit_p(wl1251_sdio_remove),
  286. .drv.pm = &wl1251_sdio_pm_ops,
  287. };
  288. static int __init wl1251_sdio_init(void)
  289. {
  290. int err;
  291. err = sdio_register_driver(&wl1251_sdio_driver);
  292. if (err)
  293. wl1251_error("failed to register sdio driver: %d", err);
  294. return err;
  295. }
  296. static void __exit wl1251_sdio_exit(void)
  297. {
  298. sdio_unregister_driver(&wl1251_sdio_driver);
  299. wl1251_notice("unloaded");
  300. }
  301. module_init(wl1251_sdio_init);
  302. module_exit(wl1251_sdio_exit);
  303. MODULE_LICENSE("GPL");
  304. MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");