sdio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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/interrupt.h>
  23. #include <linux/module.h>
  24. #include <linux/mod_devicetable.h>
  25. #include <linux/mmc/sdio_func.h>
  26. #include <linux/mmc/sdio_ids.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/wl12xx.h>
  29. #include <linux/irq.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/gpio.h>
  32. #include "wl1251.h"
  33. #ifndef SDIO_VENDOR_ID_TI
  34. #define SDIO_VENDOR_ID_TI 0x104c
  35. #endif
  36. #ifndef SDIO_DEVICE_ID_TI_WL1251
  37. #define SDIO_DEVICE_ID_TI_WL1251 0x9066
  38. #endif
  39. struct wl1251_sdio {
  40. struct sdio_func *func;
  41. u32 elp_val;
  42. };
  43. static struct sdio_func *wl_to_func(struct wl1251 *wl)
  44. {
  45. struct wl1251_sdio *wl_sdio = wl->if_priv;
  46. return wl_sdio->func;
  47. }
  48. static void wl1251_sdio_interrupt(struct sdio_func *func)
  49. {
  50. struct wl1251 *wl = sdio_get_drvdata(func);
  51. wl1251_debug(DEBUG_IRQ, "IRQ");
  52. /* FIXME should be synchronous for sdio */
  53. ieee80211_queue_work(wl->hw, &wl->irq_work);
  54. }
  55. static const struct sdio_device_id wl1251_devices[] = {
  56. { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1251) },
  57. {}
  58. };
  59. MODULE_DEVICE_TABLE(sdio, wl1251_devices);
  60. static void wl1251_sdio_read(struct wl1251 *wl, int addr,
  61. void *buf, size_t len)
  62. {
  63. int ret;
  64. struct sdio_func *func = wl_to_func(wl);
  65. sdio_claim_host(func);
  66. ret = sdio_memcpy_fromio(func, buf, addr, len);
  67. if (ret)
  68. wl1251_error("sdio read failed (%d)", ret);
  69. sdio_release_host(func);
  70. }
  71. static void wl1251_sdio_write(struct wl1251 *wl, int addr,
  72. void *buf, size_t len)
  73. {
  74. int ret;
  75. struct sdio_func *func = wl_to_func(wl);
  76. sdio_claim_host(func);
  77. ret = sdio_memcpy_toio(func, addr, buf, len);
  78. if (ret)
  79. wl1251_error("sdio write failed (%d)", ret);
  80. sdio_release_host(func);
  81. }
  82. static void wl1251_sdio_read_elp(struct wl1251 *wl, int addr, u32 *val)
  83. {
  84. int ret = 0;
  85. struct wl1251_sdio *wl_sdio = wl->if_priv;
  86. struct sdio_func *func = wl_sdio->func;
  87. /*
  88. * The hardware only supports RAW (read after write) access for
  89. * reading, regular sdio_readb won't work here (it interprets
  90. * the unused bits of CMD52 as write data even if we send read
  91. * request).
  92. */
  93. sdio_claim_host(func);
  94. *val = sdio_writeb_readb(func, wl_sdio->elp_val, addr, &ret);
  95. sdio_release_host(func);
  96. if (ret)
  97. wl1251_error("sdio_readb failed (%d)", ret);
  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. sdio_claim_host(func);
  105. sdio_writeb(func, val, addr, &ret);
  106. sdio_release_host(func);
  107. if (ret)
  108. wl1251_error("sdio_writeb failed (%d)", ret);
  109. else
  110. wl_sdio->elp_val = val;
  111. }
  112. static void wl1251_sdio_reset(struct wl1251 *wl)
  113. {
  114. }
  115. static void wl1251_sdio_enable_irq(struct wl1251 *wl)
  116. {
  117. struct sdio_func *func = wl_to_func(wl);
  118. sdio_claim_host(func);
  119. sdio_claim_irq(func, wl1251_sdio_interrupt);
  120. sdio_release_host(func);
  121. }
  122. static void wl1251_sdio_disable_irq(struct wl1251 *wl)
  123. {
  124. struct sdio_func *func = wl_to_func(wl);
  125. sdio_claim_host(func);
  126. sdio_release_irq(func);
  127. sdio_release_host(func);
  128. }
  129. /* Interrupts when using dedicated WLAN_IRQ pin */
  130. static irqreturn_t wl1251_line_irq(int irq, void *cookie)
  131. {
  132. struct wl1251 *wl = cookie;
  133. ieee80211_queue_work(wl->hw, &wl->irq_work);
  134. return IRQ_HANDLED;
  135. }
  136. static void wl1251_enable_line_irq(struct wl1251 *wl)
  137. {
  138. return enable_irq(wl->irq);
  139. }
  140. static void wl1251_disable_line_irq(struct wl1251 *wl)
  141. {
  142. return disable_irq(wl->irq);
  143. }
  144. static int wl1251_sdio_set_power(struct wl1251 *wl, bool enable)
  145. {
  146. struct sdio_func *func = wl_to_func(wl);
  147. int ret;
  148. if (enable) {
  149. /*
  150. * Power is controlled by runtime PM, but we still call board
  151. * callback in case it wants to do any additional setup,
  152. * for example enabling clock buffer for the module.
  153. */
  154. if (gpio_is_valid(wl->power_gpio))
  155. gpio_set_value(wl->power_gpio, true);
  156. ret = pm_runtime_get_sync(&func->dev);
  157. if (ret < 0) {
  158. pm_runtime_put_sync(&func->dev);
  159. goto out;
  160. }
  161. sdio_claim_host(func);
  162. sdio_enable_func(func);
  163. sdio_release_host(func);
  164. } else {
  165. sdio_claim_host(func);
  166. sdio_disable_func(func);
  167. sdio_release_host(func);
  168. ret = pm_runtime_put_sync(&func->dev);
  169. if (ret < 0)
  170. goto out;
  171. if (gpio_is_valid(wl->power_gpio))
  172. gpio_set_value(wl->power_gpio, false);
  173. }
  174. out:
  175. return ret;
  176. }
  177. static struct wl1251_if_operations wl1251_sdio_ops = {
  178. .read = wl1251_sdio_read,
  179. .write = wl1251_sdio_write,
  180. .write_elp = wl1251_sdio_write_elp,
  181. .read_elp = wl1251_sdio_read_elp,
  182. .reset = wl1251_sdio_reset,
  183. .power = wl1251_sdio_set_power,
  184. };
  185. static int wl1251_sdio_probe(struct sdio_func *func,
  186. const struct sdio_device_id *id)
  187. {
  188. int ret;
  189. struct wl1251 *wl;
  190. struct ieee80211_hw *hw;
  191. struct wl1251_sdio *wl_sdio;
  192. const struct wl1251_platform_data *wl1251_board_data;
  193. hw = wl1251_alloc_hw();
  194. if (IS_ERR(hw))
  195. return PTR_ERR(hw);
  196. wl = hw->priv;
  197. wl_sdio = kzalloc(sizeof(*wl_sdio), GFP_KERNEL);
  198. if (wl_sdio == NULL) {
  199. ret = -ENOMEM;
  200. goto out_free_hw;
  201. }
  202. sdio_claim_host(func);
  203. ret = sdio_enable_func(func);
  204. if (ret)
  205. goto release;
  206. sdio_set_block_size(func, 512);
  207. sdio_release_host(func);
  208. SET_IEEE80211_DEV(hw, &func->dev);
  209. wl_sdio->func = func;
  210. wl->if_priv = wl_sdio;
  211. wl->if_ops = &wl1251_sdio_ops;
  212. wl1251_board_data = wl1251_get_platform_data();
  213. if (!IS_ERR(wl1251_board_data)) {
  214. wl->power_gpio = wl1251_board_data->power_gpio;
  215. wl->irq = wl1251_board_data->irq;
  216. wl->use_eeprom = wl1251_board_data->use_eeprom;
  217. }
  218. if (gpio_is_valid(wl->power_gpio)) {
  219. ret = devm_gpio_request(&func->dev, wl->power_gpio,
  220. "wl1251 power");
  221. if (ret) {
  222. wl1251_error("Failed to request gpio: %d\n", ret);
  223. goto disable;
  224. }
  225. }
  226. if (wl->irq) {
  227. irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
  228. ret = request_irq(wl->irq, wl1251_line_irq, 0, "wl1251", wl);
  229. if (ret < 0) {
  230. wl1251_error("request_irq() failed: %d", ret);
  231. goto disable;
  232. }
  233. irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  234. wl1251_sdio_ops.enable_irq = wl1251_enable_line_irq;
  235. wl1251_sdio_ops.disable_irq = wl1251_disable_line_irq;
  236. wl1251_info("using dedicated interrupt line");
  237. } else {
  238. wl1251_sdio_ops.enable_irq = wl1251_sdio_enable_irq;
  239. wl1251_sdio_ops.disable_irq = wl1251_sdio_disable_irq;
  240. wl1251_info("using SDIO interrupt");
  241. }
  242. ret = wl1251_init_ieee80211(wl);
  243. if (ret)
  244. goto out_free_irq;
  245. sdio_set_drvdata(func, wl);
  246. /* Tell PM core that we don't need the card to be powered now */
  247. pm_runtime_put_noidle(&func->dev);
  248. return ret;
  249. out_free_irq:
  250. if (wl->irq)
  251. free_irq(wl->irq, wl);
  252. disable:
  253. sdio_claim_host(func);
  254. sdio_disable_func(func);
  255. release:
  256. sdio_release_host(func);
  257. kfree(wl_sdio);
  258. out_free_hw:
  259. wl1251_free_hw(wl);
  260. return ret;
  261. }
  262. static void wl1251_sdio_remove(struct sdio_func *func)
  263. {
  264. struct wl1251 *wl = sdio_get_drvdata(func);
  265. struct wl1251_sdio *wl_sdio = wl->if_priv;
  266. /* Undo decrement done above in wl1251_probe */
  267. pm_runtime_get_noresume(&func->dev);
  268. if (wl->irq)
  269. free_irq(wl->irq, wl);
  270. wl1251_free_hw(wl);
  271. kfree(wl_sdio);
  272. sdio_claim_host(func);
  273. sdio_release_irq(func);
  274. sdio_disable_func(func);
  275. sdio_release_host(func);
  276. }
  277. static int wl1251_suspend(struct device *dev)
  278. {
  279. /*
  280. * Tell MMC/SDIO core it's OK to power down the card
  281. * (if it isn't already), but not to remove it completely.
  282. */
  283. return 0;
  284. }
  285. static int wl1251_resume(struct device *dev)
  286. {
  287. return 0;
  288. }
  289. static const struct dev_pm_ops wl1251_sdio_pm_ops = {
  290. .suspend = wl1251_suspend,
  291. .resume = wl1251_resume,
  292. };
  293. static struct sdio_driver wl1251_sdio_driver = {
  294. .name = "wl1251_sdio",
  295. .id_table = wl1251_devices,
  296. .probe = wl1251_sdio_probe,
  297. .remove = wl1251_sdio_remove,
  298. .drv.pm = &wl1251_sdio_pm_ops,
  299. };
  300. static int __init wl1251_sdio_init(void)
  301. {
  302. int err;
  303. err = sdio_register_driver(&wl1251_sdio_driver);
  304. if (err)
  305. wl1251_error("failed to register sdio driver: %d", err);
  306. return err;
  307. }
  308. static void __exit wl1251_sdio_exit(void)
  309. {
  310. sdio_unregister_driver(&wl1251_sdio_driver);
  311. wl1251_notice("unloaded");
  312. }
  313. module_init(wl1251_sdio_init);
  314. module_exit(wl1251_sdio_exit);
  315. MODULE_LICENSE("GPL");
  316. MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");