spi.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. *
  20. */
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/swab.h>
  26. #include <linux/crc7.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/wl12xx.h>
  29. #include <linux/gpio.h>
  30. #include <linux/of.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/regulator/consumer.h>
  33. #include "wl1251.h"
  34. #include "reg.h"
  35. #include "spi.h"
  36. static irqreturn_t wl1251_irq(int irq, void *cookie)
  37. {
  38. struct wl1251 *wl;
  39. wl1251_debug(DEBUG_IRQ, "IRQ");
  40. wl = cookie;
  41. ieee80211_queue_work(wl->hw, &wl->irq_work);
  42. return IRQ_HANDLED;
  43. }
  44. static struct spi_device *wl_to_spi(struct wl1251 *wl)
  45. {
  46. return wl->if_priv;
  47. }
  48. static void wl1251_spi_reset(struct wl1251 *wl)
  49. {
  50. u8 *cmd;
  51. struct spi_transfer t;
  52. struct spi_message m;
  53. cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  54. if (!cmd) {
  55. wl1251_error("could not allocate cmd for spi reset");
  56. return;
  57. }
  58. memset(&t, 0, sizeof(t));
  59. spi_message_init(&m);
  60. memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
  61. t.tx_buf = cmd;
  62. t.len = WSPI_INIT_CMD_LEN;
  63. spi_message_add_tail(&t, &m);
  64. spi_sync(wl_to_spi(wl), &m);
  65. wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
  66. kfree(cmd);
  67. }
  68. static void wl1251_spi_wake(struct wl1251 *wl)
  69. {
  70. struct spi_transfer t;
  71. struct spi_message m;
  72. u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
  73. if (!cmd) {
  74. wl1251_error("could not allocate cmd for spi init");
  75. return;
  76. }
  77. memset(&t, 0, sizeof(t));
  78. spi_message_init(&m);
  79. /* Set WSPI_INIT_COMMAND
  80. * the data is being send from the MSB to LSB
  81. */
  82. cmd[0] = 0xff;
  83. cmd[1] = 0xff;
  84. cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
  85. cmd[3] = 0;
  86. cmd[4] = 0;
  87. cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
  88. cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
  89. cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
  90. | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
  91. if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
  92. cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
  93. else
  94. cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
  95. cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END;
  96. /*
  97. * The above is the logical order; it must actually be stored
  98. * in the buffer byte-swapped.
  99. */
  100. __swab32s((u32 *)cmd);
  101. __swab32s((u32 *)cmd+1);
  102. t.tx_buf = cmd;
  103. t.len = WSPI_INIT_CMD_LEN;
  104. spi_message_add_tail(&t, &m);
  105. spi_sync(wl_to_spi(wl), &m);
  106. wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
  107. kfree(cmd);
  108. }
  109. static void wl1251_spi_reset_wake(struct wl1251 *wl)
  110. {
  111. wl1251_spi_reset(wl);
  112. wl1251_spi_wake(wl);
  113. }
  114. static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
  115. size_t len)
  116. {
  117. struct spi_transfer t[3];
  118. struct spi_message m;
  119. u8 *busy_buf;
  120. u32 *cmd;
  121. cmd = &wl->buffer_cmd;
  122. busy_buf = wl->buffer_busyword;
  123. *cmd = 0;
  124. *cmd |= WSPI_CMD_READ;
  125. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  126. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  127. spi_message_init(&m);
  128. memset(t, 0, sizeof(t));
  129. t[0].tx_buf = cmd;
  130. t[0].len = 4;
  131. spi_message_add_tail(&t[0], &m);
  132. /* Busy and non busy words read */
  133. t[1].rx_buf = busy_buf;
  134. t[1].len = WL1251_BUSY_WORD_LEN;
  135. spi_message_add_tail(&t[1], &m);
  136. t[2].rx_buf = buf;
  137. t[2].len = len;
  138. spi_message_add_tail(&t[2], &m);
  139. spi_sync(wl_to_spi(wl), &m);
  140. /* FIXME: check busy words */
  141. wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
  142. wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
  143. }
  144. static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
  145. size_t len)
  146. {
  147. struct spi_transfer t[2];
  148. struct spi_message m;
  149. u32 *cmd;
  150. cmd = &wl->buffer_cmd;
  151. *cmd = 0;
  152. *cmd |= WSPI_CMD_WRITE;
  153. *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
  154. *cmd |= addr & WSPI_CMD_BYTE_ADDR;
  155. spi_message_init(&m);
  156. memset(t, 0, sizeof(t));
  157. t[0].tx_buf = cmd;
  158. t[0].len = sizeof(*cmd);
  159. spi_message_add_tail(&t[0], &m);
  160. t[1].tx_buf = buf;
  161. t[1].len = len;
  162. spi_message_add_tail(&t[1], &m);
  163. spi_sync(wl_to_spi(wl), &m);
  164. wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
  165. wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
  166. }
  167. static void wl1251_spi_enable_irq(struct wl1251 *wl)
  168. {
  169. return enable_irq(wl->irq);
  170. }
  171. static void wl1251_spi_disable_irq(struct wl1251 *wl)
  172. {
  173. return disable_irq(wl->irq);
  174. }
  175. static int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
  176. {
  177. if (gpio_is_valid(wl->power_gpio))
  178. gpio_set_value(wl->power_gpio, enable);
  179. return 0;
  180. }
  181. static const struct wl1251_if_operations wl1251_spi_ops = {
  182. .read = wl1251_spi_read,
  183. .write = wl1251_spi_write,
  184. .reset = wl1251_spi_reset_wake,
  185. .enable_irq = wl1251_spi_enable_irq,
  186. .disable_irq = wl1251_spi_disable_irq,
  187. .power = wl1251_spi_set_power,
  188. };
  189. static int wl1251_spi_probe(struct spi_device *spi)
  190. {
  191. struct wl1251_platform_data *pdata = dev_get_platdata(&spi->dev);
  192. struct device_node *np = spi->dev.of_node;
  193. struct ieee80211_hw *hw;
  194. struct wl1251 *wl;
  195. int ret;
  196. if (!np && !pdata) {
  197. wl1251_error("no platform data");
  198. return -ENODEV;
  199. }
  200. hw = wl1251_alloc_hw();
  201. if (IS_ERR(hw))
  202. return PTR_ERR(hw);
  203. wl = hw->priv;
  204. SET_IEEE80211_DEV(hw, &spi->dev);
  205. spi_set_drvdata(spi, wl);
  206. wl->if_priv = spi;
  207. wl->if_ops = &wl1251_spi_ops;
  208. /* This is the only SPI value that we need to set here, the rest
  209. * comes from the board-peripherals file
  210. */
  211. spi->bits_per_word = 32;
  212. ret = spi_setup(spi);
  213. if (ret < 0) {
  214. wl1251_error("spi_setup failed");
  215. goto out_free;
  216. }
  217. if (np) {
  218. wl->use_eeprom = of_property_read_bool(np, "ti,wl1251-has-eeprom");
  219. wl->power_gpio = of_get_named_gpio(np, "ti,power-gpio", 0);
  220. } else if (pdata) {
  221. wl->power_gpio = pdata->power_gpio;
  222. wl->use_eeprom = pdata->use_eeprom;
  223. }
  224. if (wl->power_gpio == -EPROBE_DEFER) {
  225. ret = -EPROBE_DEFER;
  226. goto out_free;
  227. }
  228. if (gpio_is_valid(wl->power_gpio)) {
  229. ret = devm_gpio_request_one(&spi->dev, wl->power_gpio,
  230. GPIOF_OUT_INIT_LOW, "wl1251 power");
  231. if (ret) {
  232. wl1251_error("Failed to request gpio: %d\n", ret);
  233. goto out_free;
  234. }
  235. } else {
  236. wl1251_error("set power gpio missing in platform data");
  237. ret = -ENODEV;
  238. goto out_free;
  239. }
  240. wl->irq = spi->irq;
  241. if (wl->irq < 0) {
  242. wl1251_error("irq missing in platform data");
  243. ret = -ENODEV;
  244. goto out_free;
  245. }
  246. irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
  247. ret = devm_request_irq(&spi->dev, wl->irq, wl1251_irq, 0,
  248. DRIVER_NAME, wl);
  249. if (ret < 0) {
  250. wl1251_error("request_irq() failed: %d", ret);
  251. goto out_free;
  252. }
  253. irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
  254. wl->vio = devm_regulator_get(&spi->dev, "vio");
  255. if (IS_ERR(wl->vio)) {
  256. ret = PTR_ERR(wl->vio);
  257. wl1251_error("vio regulator missing: %d", ret);
  258. goto out_free;
  259. }
  260. ret = regulator_enable(wl->vio);
  261. if (ret)
  262. goto out_free;
  263. ret = wl1251_init_ieee80211(wl);
  264. if (ret)
  265. goto disable_regulator;
  266. return 0;
  267. disable_regulator:
  268. regulator_disable(wl->vio);
  269. out_free:
  270. ieee80211_free_hw(hw);
  271. return ret;
  272. }
  273. static int wl1251_spi_remove(struct spi_device *spi)
  274. {
  275. struct wl1251 *wl = spi_get_drvdata(spi);
  276. wl1251_free_hw(wl);
  277. regulator_disable(wl->vio);
  278. return 0;
  279. }
  280. static struct spi_driver wl1251_spi_driver = {
  281. .driver = {
  282. .name = DRIVER_NAME,
  283. },
  284. .probe = wl1251_spi_probe,
  285. .remove = wl1251_spi_remove,
  286. };
  287. module_spi_driver(wl1251_spi_driver);
  288. MODULE_LICENSE("GPL");
  289. MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
  290. MODULE_ALIAS("spi:wl1251");