spi.c 7.3 KB

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