cw1200_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Mac80211 SPI driver for ST-Ericsson CW1200 device
  3. *
  4. * Copyright (c) 2011, Sagrad Inc.
  5. * Author: Solomon Peachy <speachy@sagrad.com>
  6. *
  7. * Based on cw1200_sdio.c
  8. * Copyright (c) 2010, ST-Ericsson
  9. * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/gpio.h>
  17. #include <linux/delay.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/interrupt.h>
  20. #include <net/mac80211.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/device.h>
  23. #include "cw1200.h"
  24. #include "hwbus.h"
  25. #include <linux/platform_data/net-cw1200.h>
  26. #include "hwio.h"
  27. MODULE_AUTHOR("Solomon Peachy <speachy@sagrad.com>");
  28. MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SPI driver");
  29. MODULE_LICENSE("GPL");
  30. MODULE_ALIAS("spi:cw1200_wlan_spi");
  31. /* #define SPI_DEBUG */
  32. struct hwbus_priv {
  33. struct spi_device *func;
  34. struct cw1200_common *core;
  35. const struct cw1200_platform_data_spi *pdata;
  36. spinlock_t lock; /* Serialize all bus operations */
  37. wait_queue_head_t wq;
  38. int claimed;
  39. };
  40. #define SDIO_TO_SPI_ADDR(addr) ((addr & 0x1f)>>2)
  41. #define SET_WRITE 0x7FFF /* usage: and operation */
  42. #define SET_READ 0x8000 /* usage: or operation */
  43. /* Notes on byte ordering:
  44. LE: B0 B1 B2 B3
  45. BE: B3 B2 B1 B0
  46. Hardware expects 32-bit data to be written as 16-bit BE words:
  47. B1 B0 B3 B2
  48. */
  49. static int cw1200_spi_memcpy_fromio(struct hwbus_priv *self,
  50. unsigned int addr,
  51. void *dst, int count)
  52. {
  53. int ret, i;
  54. u16 regaddr;
  55. struct spi_message m;
  56. struct spi_transfer t_addr = {
  57. .tx_buf = &regaddr,
  58. .len = sizeof(regaddr),
  59. };
  60. struct spi_transfer t_msg = {
  61. .rx_buf = dst,
  62. .len = count,
  63. };
  64. regaddr = (SDIO_TO_SPI_ADDR(addr))<<12;
  65. regaddr |= SET_READ;
  66. regaddr |= (count>>1);
  67. #ifdef SPI_DEBUG
  68. pr_info("READ : %04d from 0x%02x (%04x)\n", count, addr, regaddr);
  69. #endif
  70. /* Header is LE16 */
  71. regaddr = cpu_to_le16(regaddr);
  72. /* We have to byteswap if the SPI bus is limited to 8b operation
  73. or we are running on a Big Endian system
  74. */
  75. #if defined(__LITTLE_ENDIAN)
  76. if (self->func->bits_per_word == 8)
  77. #endif
  78. regaddr = swab16(regaddr);
  79. spi_message_init(&m);
  80. spi_message_add_tail(&t_addr, &m);
  81. spi_message_add_tail(&t_msg, &m);
  82. ret = spi_sync(self->func, &m);
  83. #ifdef SPI_DEBUG
  84. pr_info("READ : ");
  85. for (i = 0; i < t_addr.len; i++)
  86. printk("%02x ", ((u8 *)t_addr.tx_buf)[i]);
  87. printk(" : ");
  88. for (i = 0; i < t_msg.len; i++)
  89. printk("%02x ", ((u8 *)t_msg.rx_buf)[i]);
  90. printk("\n");
  91. #endif
  92. /* We have to byteswap if the SPI bus is limited to 8b operation
  93. or we are running on a Big Endian system
  94. */
  95. #if defined(__LITTLE_ENDIAN)
  96. if (self->func->bits_per_word == 8)
  97. #endif
  98. {
  99. uint16_t *buf = (uint16_t *)dst;
  100. for (i = 0; i < ((count + 1) >> 1); i++)
  101. buf[i] = swab16(buf[i]);
  102. }
  103. return ret;
  104. }
  105. static int cw1200_spi_memcpy_toio(struct hwbus_priv *self,
  106. unsigned int addr,
  107. const void *src, int count)
  108. {
  109. int rval, i;
  110. u16 regaddr;
  111. struct spi_transfer t_addr = {
  112. .tx_buf = &regaddr,
  113. .len = sizeof(regaddr),
  114. };
  115. struct spi_transfer t_msg = {
  116. .tx_buf = src,
  117. .len = count,
  118. };
  119. struct spi_message m;
  120. regaddr = (SDIO_TO_SPI_ADDR(addr))<<12;
  121. regaddr &= SET_WRITE;
  122. regaddr |= (count>>1);
  123. #ifdef SPI_DEBUG
  124. pr_info("WRITE: %04d to 0x%02x (%04x)\n", count, addr, regaddr);
  125. #endif
  126. /* Header is LE16 */
  127. regaddr = cpu_to_le16(regaddr);
  128. /* We have to byteswap if the SPI bus is limited to 8b operation
  129. or we are running on a Big Endian system
  130. */
  131. #if defined(__LITTLE_ENDIAN)
  132. if (self->func->bits_per_word == 8)
  133. #endif
  134. {
  135. uint16_t *buf = (uint16_t *)src;
  136. regaddr = swab16(regaddr);
  137. for (i = 0; i < ((count + 1) >> 1); i++)
  138. buf[i] = swab16(buf[i]);
  139. }
  140. #ifdef SPI_DEBUG
  141. pr_info("WRITE: ");
  142. for (i = 0; i < t_addr.len; i++)
  143. printk("%02x ", ((u8 *)t_addr.tx_buf)[i]);
  144. printk(" : ");
  145. for (i = 0; i < t_msg.len; i++)
  146. printk("%02x ", ((u8 *)t_msg.tx_buf)[i]);
  147. printk("\n");
  148. #endif
  149. spi_message_init(&m);
  150. spi_message_add_tail(&t_addr, &m);
  151. spi_message_add_tail(&t_msg, &m);
  152. rval = spi_sync(self->func, &m);
  153. #ifdef SPI_DEBUG
  154. pr_info("WROTE: %d\n", m.actual_length);
  155. #endif
  156. #if defined(__LITTLE_ENDIAN)
  157. /* We have to byteswap if the SPI bus is limited to 8b operation */
  158. if (self->func->bits_per_word == 8)
  159. #endif
  160. {
  161. uint16_t *buf = (uint16_t *)src;
  162. for (i = 0; i < ((count + 1) >> 1); i++)
  163. buf[i] = swab16(buf[i]);
  164. }
  165. return rval;
  166. }
  167. static void cw1200_spi_lock(struct hwbus_priv *self)
  168. {
  169. unsigned long flags;
  170. DECLARE_WAITQUEUE(wait, current);
  171. might_sleep();
  172. add_wait_queue(&self->wq, &wait);
  173. spin_lock_irqsave(&self->lock, flags);
  174. while (1) {
  175. set_current_state(TASK_UNINTERRUPTIBLE);
  176. if (!self->claimed)
  177. break;
  178. spin_unlock_irqrestore(&self->lock, flags);
  179. schedule();
  180. spin_lock_irqsave(&self->lock, flags);
  181. }
  182. set_current_state(TASK_RUNNING);
  183. self->claimed = 1;
  184. spin_unlock_irqrestore(&self->lock, flags);
  185. remove_wait_queue(&self->wq, &wait);
  186. return;
  187. }
  188. static void cw1200_spi_unlock(struct hwbus_priv *self)
  189. {
  190. unsigned long flags;
  191. spin_lock_irqsave(&self->lock, flags);
  192. self->claimed = 0;
  193. spin_unlock_irqrestore(&self->lock, flags);
  194. wake_up(&self->wq);
  195. return;
  196. }
  197. static irqreturn_t cw1200_spi_irq_handler(int irq, void *dev_id)
  198. {
  199. struct hwbus_priv *self = dev_id;
  200. if (self->core) {
  201. cw1200_spi_lock(self);
  202. cw1200_irq_handler(self->core);
  203. cw1200_spi_unlock(self);
  204. return IRQ_HANDLED;
  205. } else {
  206. return IRQ_NONE;
  207. }
  208. }
  209. static int cw1200_spi_irq_subscribe(struct hwbus_priv *self)
  210. {
  211. int ret;
  212. pr_debug("SW IRQ subscribe\n");
  213. ret = request_threaded_irq(self->func->irq, NULL,
  214. cw1200_spi_irq_handler,
  215. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  216. "cw1200_wlan_irq", self);
  217. if (WARN_ON(ret < 0))
  218. goto exit;
  219. ret = enable_irq_wake(self->func->irq);
  220. if (WARN_ON(ret))
  221. goto free_irq;
  222. return 0;
  223. free_irq:
  224. free_irq(self->func->irq, self);
  225. exit:
  226. return ret;
  227. }
  228. static int cw1200_spi_irq_unsubscribe(struct hwbus_priv *self)
  229. {
  230. int ret = 0;
  231. pr_debug("SW IRQ unsubscribe\n");
  232. disable_irq_wake(self->func->irq);
  233. free_irq(self->func->irq, self);
  234. return ret;
  235. }
  236. static int cw1200_spi_off(const struct cw1200_platform_data_spi *pdata)
  237. {
  238. if (pdata->reset) {
  239. gpio_set_value(pdata->reset, 0);
  240. msleep(30); /* Min is 2 * CLK32K cycles */
  241. gpio_free(pdata->reset);
  242. }
  243. if (pdata->power_ctrl)
  244. pdata->power_ctrl(pdata, false);
  245. if (pdata->clk_ctrl)
  246. pdata->clk_ctrl(pdata, false);
  247. return 0;
  248. }
  249. static int cw1200_spi_on(const struct cw1200_platform_data_spi *pdata)
  250. {
  251. /* Ensure I/Os are pulled low */
  252. if (pdata->reset) {
  253. gpio_request(pdata->reset, "cw1200_wlan_reset");
  254. gpio_direction_output(pdata->reset, 0);
  255. }
  256. if (pdata->powerup) {
  257. gpio_request(pdata->powerup, "cw1200_wlan_powerup");
  258. gpio_direction_output(pdata->powerup, 0);
  259. }
  260. if (pdata->reset || pdata->powerup)
  261. msleep(10); /* Settle time? */
  262. /* Enable 3v3 and 1v8 to hardware */
  263. if (pdata->power_ctrl) {
  264. if (pdata->power_ctrl(pdata, true)) {
  265. pr_err("power_ctrl() failed!\n");
  266. return -1;
  267. }
  268. }
  269. /* Enable CLK32K */
  270. if (pdata->clk_ctrl) {
  271. if (pdata->clk_ctrl(pdata, true)) {
  272. pr_err("clk_ctrl() failed!\n");
  273. return -1;
  274. }
  275. msleep(10); /* Delay until clock is stable for 2 cycles */
  276. }
  277. /* Enable POWERUP signal */
  278. if (pdata->powerup) {
  279. gpio_set_value(pdata->powerup, 1);
  280. msleep(250); /* or more..? */
  281. }
  282. /* Enable RSTn signal */
  283. if (pdata->reset) {
  284. gpio_set_value(pdata->reset, 1);
  285. msleep(50); /* Or more..? */
  286. }
  287. return 0;
  288. }
  289. static size_t cw1200_spi_align_size(struct hwbus_priv *self, size_t size)
  290. {
  291. return size & 1 ? size + 1 : size;
  292. }
  293. static int cw1200_spi_pm(struct hwbus_priv *self, bool suspend)
  294. {
  295. return irq_set_irq_wake(self->func->irq, suspend);
  296. }
  297. static struct hwbus_ops cw1200_spi_hwbus_ops = {
  298. .hwbus_memcpy_fromio = cw1200_spi_memcpy_fromio,
  299. .hwbus_memcpy_toio = cw1200_spi_memcpy_toio,
  300. .lock = cw1200_spi_lock,
  301. .unlock = cw1200_spi_unlock,
  302. .align_size = cw1200_spi_align_size,
  303. .power_mgmt = cw1200_spi_pm,
  304. };
  305. /* Probe Function to be called by SPI stack when device is discovered */
  306. static int cw1200_spi_probe(struct spi_device *func)
  307. {
  308. const struct cw1200_platform_data_spi *plat_data =
  309. dev_get_platdata(&func->dev);
  310. struct hwbus_priv *self;
  311. int status;
  312. /* Sanity check speed */
  313. if (func->max_speed_hz > 52000000)
  314. func->max_speed_hz = 52000000;
  315. if (func->max_speed_hz < 1000000)
  316. func->max_speed_hz = 1000000;
  317. /* Fix up transfer size */
  318. if (plat_data->spi_bits_per_word)
  319. func->bits_per_word = plat_data->spi_bits_per_word;
  320. if (!func->bits_per_word)
  321. func->bits_per_word = 16;
  322. /* And finally.. */
  323. func->mode = SPI_MODE_0;
  324. pr_info("cw1200_wlan_spi: Probe called (CS %d M %d BPW %d CLK %d)\n",
  325. func->chip_select, func->mode, func->bits_per_word,
  326. func->max_speed_hz);
  327. if (cw1200_spi_on(plat_data)) {
  328. pr_err("spi_on() failed!\n");
  329. return -1;
  330. }
  331. if (spi_setup(func)) {
  332. pr_err("spi_setup() failed!\n");
  333. return -1;
  334. }
  335. self = devm_kzalloc(&func->dev, sizeof(*self), GFP_KERNEL);
  336. if (!self) {
  337. pr_err("Can't allocate SPI hwbus_priv.");
  338. return -ENOMEM;
  339. }
  340. self->pdata = plat_data;
  341. self->func = func;
  342. spin_lock_init(&self->lock);
  343. spi_set_drvdata(func, self);
  344. init_waitqueue_head(&self->wq);
  345. status = cw1200_spi_irq_subscribe(self);
  346. status = cw1200_core_probe(&cw1200_spi_hwbus_ops,
  347. self, &func->dev, &self->core,
  348. self->pdata->ref_clk,
  349. self->pdata->macaddr,
  350. self->pdata->sdd_file,
  351. self->pdata->have_5ghz);
  352. if (status) {
  353. cw1200_spi_irq_unsubscribe(self);
  354. cw1200_spi_off(plat_data);
  355. }
  356. return status;
  357. }
  358. /* Disconnect Function to be called by SPI stack when device is disconnected */
  359. static int cw1200_spi_disconnect(struct spi_device *func)
  360. {
  361. struct hwbus_priv *self = spi_get_drvdata(func);
  362. if (self) {
  363. cw1200_spi_irq_unsubscribe(self);
  364. if (self->core) {
  365. cw1200_core_release(self->core);
  366. self->core = NULL;
  367. }
  368. }
  369. cw1200_spi_off(dev_get_platdata(&func->dev));
  370. return 0;
  371. }
  372. static int __maybe_unused cw1200_spi_suspend(struct device *dev)
  373. {
  374. struct hwbus_priv *self = spi_get_drvdata(to_spi_device(dev));
  375. if (!cw1200_can_suspend(self->core))
  376. return -EAGAIN;
  377. /* XXX notify host that we have to keep CW1200 powered on? */
  378. return 0;
  379. }
  380. static SIMPLE_DEV_PM_OPS(cw1200_pm_ops, cw1200_spi_suspend, NULL);
  381. static struct spi_driver spi_driver = {
  382. .probe = cw1200_spi_probe,
  383. .remove = cw1200_spi_disconnect,
  384. .driver = {
  385. .name = "cw1200_wlan_spi",
  386. .pm = IS_ENABLED(CONFIG_PM) ? &cw1200_pm_ops : NULL,
  387. },
  388. };
  389. module_spi_driver(spi_driver);