spi.c 7.2 KB

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