sdio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/irq.h>
  24. #include <linux/module.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mmc/sdio_func.h>
  28. #include <linux/mmc/sdio_ids.h>
  29. #include <linux/mmc/card.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/gpio.h>
  32. #include <linux/wl12xx.h>
  33. #include <linux/pm_runtime.h>
  34. #include "wl12xx.h"
  35. #include "wl12xx_80211.h"
  36. #include "io.h"
  37. #ifndef SDIO_VENDOR_ID_TI
  38. #define SDIO_VENDOR_ID_TI 0x0097
  39. #endif
  40. #ifndef SDIO_DEVICE_ID_TI_WL1271
  41. #define SDIO_DEVICE_ID_TI_WL1271 0x4076
  42. #endif
  43. struct wl12xx_sdio_glue {
  44. struct device *dev;
  45. struct platform_device *core;
  46. };
  47. static const struct sdio_device_id wl1271_devices[] __devinitconst = {
  48. { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
  49. {}
  50. };
  51. MODULE_DEVICE_TABLE(sdio, wl1271_devices);
  52. static void wl1271_sdio_set_block_size(struct device *child,
  53. unsigned int blksz)
  54. {
  55. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  56. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  57. sdio_claim_host(func);
  58. sdio_set_block_size(func, blksz);
  59. sdio_release_host(func);
  60. }
  61. static void wl12xx_sdio_raw_read(struct device *child, int addr, void *buf,
  62. size_t len, bool fixed)
  63. {
  64. int ret;
  65. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  66. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  67. sdio_claim_host(func);
  68. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
  69. ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
  70. dev_dbg(child->parent, "sdio read 52 addr 0x%x, byte 0x%02x\n",
  71. addr, ((u8 *)buf)[0]);
  72. } else {
  73. if (fixed)
  74. ret = sdio_readsb(func, buf, addr, len);
  75. else
  76. ret = sdio_memcpy_fromio(func, buf, addr, len);
  77. dev_dbg(child->parent, "sdio read 53 addr 0x%x, %zu bytes\n",
  78. addr, len);
  79. }
  80. sdio_release_host(func);
  81. if (ret)
  82. dev_err(child->parent, "sdio read failed (%d)\n", ret);
  83. }
  84. static void wl12xx_sdio_raw_write(struct device *child, int addr, void *buf,
  85. size_t len, bool fixed)
  86. {
  87. int ret;
  88. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  89. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  90. sdio_claim_host(func);
  91. if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG_ADDR)) {
  92. sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
  93. dev_dbg(child->parent, "sdio write 52 addr 0x%x, byte 0x%02x\n",
  94. addr, ((u8 *)buf)[0]);
  95. } else {
  96. dev_dbg(child->parent, "sdio write 53 addr 0x%x, %zu bytes\n",
  97. addr, len);
  98. if (fixed)
  99. ret = sdio_writesb(func, addr, buf, len);
  100. else
  101. ret = sdio_memcpy_toio(func, addr, buf, len);
  102. }
  103. sdio_release_host(func);
  104. if (ret)
  105. dev_err(child->parent, "sdio write failed (%d)\n", ret);
  106. }
  107. static int wl12xx_sdio_power_on(struct wl12xx_sdio_glue *glue)
  108. {
  109. int ret;
  110. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  111. /* If enabled, tell runtime PM not to power off the card */
  112. if (pm_runtime_enabled(&func->dev)) {
  113. ret = pm_runtime_get_sync(&func->dev);
  114. if (ret < 0)
  115. goto out;
  116. } else {
  117. /* Runtime PM is disabled: power up the card manually */
  118. ret = mmc_power_restore_host(func->card->host);
  119. if (ret < 0)
  120. goto out;
  121. }
  122. sdio_claim_host(func);
  123. sdio_enable_func(func);
  124. sdio_release_host(func);
  125. out:
  126. return ret;
  127. }
  128. static int wl12xx_sdio_power_off(struct wl12xx_sdio_glue *glue)
  129. {
  130. int ret;
  131. struct sdio_func *func = dev_to_sdio_func(glue->dev);
  132. sdio_claim_host(func);
  133. sdio_disable_func(func);
  134. sdio_release_host(func);
  135. /* Power off the card manually, even if runtime PM is enabled. */
  136. ret = mmc_power_save_host(func->card->host);
  137. if (ret < 0)
  138. return ret;
  139. /* If enabled, let runtime PM know the card is powered off */
  140. if (pm_runtime_enabled(&func->dev))
  141. ret = pm_runtime_put_sync(&func->dev);
  142. return ret;
  143. }
  144. static int wl12xx_sdio_set_power(struct device *child, bool enable)
  145. {
  146. struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
  147. if (enable)
  148. return wl12xx_sdio_power_on(glue);
  149. else
  150. return wl12xx_sdio_power_off(glue);
  151. }
  152. static struct wl1271_if_operations sdio_ops = {
  153. .read = wl12xx_sdio_raw_read,
  154. .write = wl12xx_sdio_raw_write,
  155. .power = wl12xx_sdio_set_power,
  156. .set_block_size = wl1271_sdio_set_block_size,
  157. };
  158. static int __devinit wl1271_probe(struct sdio_func *func,
  159. const struct sdio_device_id *id)
  160. {
  161. struct wl12xx_platform_data *wlan_data;
  162. struct wl12xx_sdio_glue *glue;
  163. struct resource res[1];
  164. mmc_pm_flag_t mmcflags;
  165. int ret = -ENOMEM;
  166. /* We are only able to handle the wlan function */
  167. if (func->num != 0x02)
  168. return -ENODEV;
  169. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  170. if (!glue) {
  171. dev_err(&func->dev, "can't allocate glue\n");
  172. goto out;
  173. }
  174. glue->dev = &func->dev;
  175. /* Grab access to FN0 for ELP reg. */
  176. func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
  177. /* Use block mode for transferring over one block size of data */
  178. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  179. wlan_data = wl12xx_get_platform_data();
  180. if (IS_ERR(wlan_data)) {
  181. ret = PTR_ERR(wlan_data);
  182. dev_err(glue->dev, "missing wlan platform data: %d\n", ret);
  183. goto out_free_glue;
  184. }
  185. /* if sdio can keep power while host is suspended, enable wow */
  186. mmcflags = sdio_get_host_pm_caps(func);
  187. dev_dbg(glue->dev, "sdio PM caps = 0x%x\n", mmcflags);
  188. if (mmcflags & MMC_PM_KEEP_POWER)
  189. wlan_data->pwr_in_suspend = true;
  190. wlan_data->ops = &sdio_ops;
  191. sdio_set_drvdata(func, glue);
  192. /* Tell PM core that we don't need the card to be powered now */
  193. pm_runtime_put_noidle(&func->dev);
  194. glue->core = platform_device_alloc("wl12xx", -1);
  195. if (!glue->core) {
  196. dev_err(glue->dev, "can't allocate platform_device");
  197. ret = -ENOMEM;
  198. goto out_free_glue;
  199. }
  200. glue->core->dev.parent = &func->dev;
  201. memset(res, 0x00, sizeof(res));
  202. res[0].start = wlan_data->irq;
  203. res[0].flags = IORESOURCE_IRQ;
  204. res[0].name = "irq";
  205. ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
  206. if (ret) {
  207. dev_err(glue->dev, "can't add resources\n");
  208. goto out_dev_put;
  209. }
  210. ret = platform_device_add_data(glue->core, wlan_data,
  211. sizeof(*wlan_data));
  212. if (ret) {
  213. dev_err(glue->dev, "can't add platform data\n");
  214. goto out_dev_put;
  215. }
  216. ret = platform_device_add(glue->core);
  217. if (ret) {
  218. dev_err(glue->dev, "can't add platform device\n");
  219. goto out_dev_put;
  220. }
  221. return 0;
  222. out_dev_put:
  223. platform_device_put(glue->core);
  224. out_free_glue:
  225. kfree(glue);
  226. out:
  227. return ret;
  228. }
  229. static void __devexit wl1271_remove(struct sdio_func *func)
  230. {
  231. struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
  232. /* Undo decrement done above in wl1271_probe */
  233. pm_runtime_get_noresume(&func->dev);
  234. platform_device_del(glue->core);
  235. platform_device_put(glue->core);
  236. kfree(glue);
  237. }
  238. #ifdef CONFIG_PM
  239. static int wl1271_suspend(struct device *dev)
  240. {
  241. /* Tell MMC/SDIO core it's OK to power down the card
  242. * (if it isn't already), but not to remove it completely */
  243. struct sdio_func *func = dev_to_sdio_func(dev);
  244. struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
  245. struct wl1271 *wl = platform_get_drvdata(glue->core);
  246. mmc_pm_flag_t sdio_flags;
  247. int ret = 0;
  248. dev_dbg(dev, "wl1271 suspend. wow_enabled: %d\n",
  249. wl->wow_enabled);
  250. /* check whether sdio should keep power */
  251. if (wl->wow_enabled) {
  252. sdio_flags = sdio_get_host_pm_caps(func);
  253. if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
  254. dev_err(dev, "can't keep power while host "
  255. "is suspended\n");
  256. ret = -EINVAL;
  257. goto out;
  258. }
  259. /* keep power while host suspended */
  260. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  261. if (ret) {
  262. dev_err(dev, "error while trying to keep power\n");
  263. goto out;
  264. }
  265. }
  266. out:
  267. return ret;
  268. }
  269. static int wl1271_resume(struct device *dev)
  270. {
  271. dev_dbg(dev, "wl1271 resume\n");
  272. return 0;
  273. }
  274. static const struct dev_pm_ops wl1271_sdio_pm_ops = {
  275. .suspend = wl1271_suspend,
  276. .resume = wl1271_resume,
  277. };
  278. #endif
  279. static struct sdio_driver wl1271_sdio_driver = {
  280. .name = "wl1271_sdio",
  281. .id_table = wl1271_devices,
  282. .probe = wl1271_probe,
  283. .remove = __devexit_p(wl1271_remove),
  284. #ifdef CONFIG_PM
  285. .drv = {
  286. .pm = &wl1271_sdio_pm_ops,
  287. },
  288. #endif
  289. };
  290. static int __init wl1271_init(void)
  291. {
  292. return sdio_register_driver(&wl1271_sdio_driver);
  293. }
  294. static void __exit wl1271_exit(void)
  295. {
  296. sdio_unregister_driver(&wl1271_sdio_driver);
  297. }
  298. module_init(wl1271_init);
  299. module_exit(wl1271_exit);
  300. MODULE_LICENSE("GPL");
  301. MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
  302. MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
  303. MODULE_FIRMWARE(WL127X_FW_NAME_SINGLE);
  304. MODULE_FIRMWARE(WL127X_FW_NAME_MULTI);
  305. MODULE_FIRMWARE(WL127X_PLT_FW_NAME);
  306. MODULE_FIRMWARE(WL128X_FW_NAME_SINGLE);
  307. MODULE_FIRMWARE(WL128X_FW_NAME_MULTI);
  308. MODULE_FIRMWARE(WL128X_PLT_FW_NAME);