cw1200_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. int irq_disabled;
  40. };
  41. #define SDIO_TO_SPI_ADDR(addr) ((addr & 0x1f)>>2)
  42. #define SET_WRITE 0x7FFF /* usage: and operation */
  43. #define SET_READ 0x8000 /* usage: or operation */
  44. /* Notes on byte ordering:
  45. LE: B0 B1 B2 B3
  46. BE: B3 B2 B1 B0
  47. Hardware expects 32-bit data to be written as 16-bit BE words:
  48. B1 B0 B3 B2
  49. */
  50. static int cw1200_spi_memcpy_fromio(struct hwbus_priv *self,
  51. unsigned int addr,
  52. void *dst, int count)
  53. {
  54. int ret, i;
  55. u16 regaddr;
  56. struct spi_message m;
  57. struct spi_transfer t_addr = {
  58. .tx_buf = &regaddr,
  59. .len = sizeof(regaddr),
  60. };
  61. struct spi_transfer t_msg = {
  62. .rx_buf = dst,
  63. .len = count,
  64. };
  65. regaddr = (SDIO_TO_SPI_ADDR(addr))<<12;
  66. regaddr |= SET_READ;
  67. regaddr |= (count>>1);
  68. #ifdef SPI_DEBUG
  69. pr_info("READ : %04d from 0x%02x (%04x)\n", count, addr, regaddr);
  70. #endif
  71. /* Header is LE16 */
  72. regaddr = cpu_to_le16(regaddr);
  73. /* We have to byteswap if the SPI bus is limited to 8b operation
  74. or we are running on a Big Endian system
  75. */
  76. #if defined(__LITTLE_ENDIAN)
  77. if (self->func->bits_per_word == 8)
  78. #endif
  79. regaddr = swab16(regaddr);
  80. spi_message_init(&m);
  81. spi_message_add_tail(&t_addr, &m);
  82. spi_message_add_tail(&t_msg, &m);
  83. ret = spi_sync(self->func, &m);
  84. #ifdef SPI_DEBUG
  85. pr_info("READ : ");
  86. for (i = 0; i < t_addr.len; i++)
  87. printk("%02x ", ((u8 *)t_addr.tx_buf)[i]);
  88. printk(" : ");
  89. for (i = 0; i < t_msg.len; i++)
  90. printk("%02x ", ((u8 *)t_msg.rx_buf)[i]);
  91. printk("\n");
  92. #endif
  93. /* We have to byteswap if the SPI bus is limited to 8b operation
  94. or we are running on a Big Endian system
  95. */
  96. #if defined(__LITTLE_ENDIAN)
  97. if (self->func->bits_per_word == 8)
  98. #endif
  99. {
  100. uint16_t *buf = (uint16_t *)dst;
  101. for (i = 0; i < ((count + 1) >> 1); i++)
  102. buf[i] = swab16(buf[i]);
  103. }
  104. return ret;
  105. }
  106. static int cw1200_spi_memcpy_toio(struct hwbus_priv *self,
  107. unsigned int addr,
  108. const void *src, int count)
  109. {
  110. int rval, i;
  111. u16 regaddr;
  112. struct spi_transfer t_addr = {
  113. .tx_buf = &regaddr,
  114. .len = sizeof(regaddr),
  115. };
  116. struct spi_transfer t_msg = {
  117. .tx_buf = src,
  118. .len = count,
  119. };
  120. struct spi_message m;
  121. regaddr = (SDIO_TO_SPI_ADDR(addr))<<12;
  122. regaddr &= SET_WRITE;
  123. regaddr |= (count>>1);
  124. #ifdef SPI_DEBUG
  125. pr_info("WRITE: %04d to 0x%02x (%04x)\n", count, addr, regaddr);
  126. #endif
  127. /* Header is LE16 */
  128. regaddr = cpu_to_le16(regaddr);
  129. /* We have to byteswap if the SPI bus is limited to 8b operation
  130. or we are running on a Big Endian system
  131. */
  132. #if defined(__LITTLE_ENDIAN)
  133. if (self->func->bits_per_word == 8)
  134. #endif
  135. {
  136. uint16_t *buf = (uint16_t *)src;
  137. regaddr = swab16(regaddr);
  138. for (i = 0; i < ((count + 1) >> 1); i++)
  139. buf[i] = swab16(buf[i]);
  140. }
  141. #ifdef SPI_DEBUG
  142. pr_info("WRITE: ");
  143. for (i = 0; i < t_addr.len; i++)
  144. printk("%02x ", ((u8 *)t_addr.tx_buf)[i]);
  145. printk(" : ");
  146. for (i = 0; i < t_msg.len; i++)
  147. printk("%02x ", ((u8 *)t_msg.tx_buf)[i]);
  148. printk("\n");
  149. #endif
  150. spi_message_init(&m);
  151. spi_message_add_tail(&t_addr, &m);
  152. spi_message_add_tail(&t_msg, &m);
  153. rval = spi_sync(self->func, &m);
  154. #ifdef SPI_DEBUG
  155. pr_info("WROTE: %d\n", m.actual_length);
  156. #endif
  157. #if defined(__LITTLE_ENDIAN)
  158. /* We have to byteswap if the SPI bus is limited to 8b operation */
  159. if (self->func->bits_per_word == 8)
  160. #endif
  161. {
  162. uint16_t *buf = (uint16_t *)src;
  163. for (i = 0; i < ((count + 1) >> 1); i++)
  164. buf[i] = swab16(buf[i]);
  165. }
  166. return rval;
  167. }
  168. static void cw1200_spi_lock(struct hwbus_priv *self)
  169. {
  170. unsigned long flags;
  171. DECLARE_WAITQUEUE(wait, current);
  172. might_sleep();
  173. add_wait_queue(&self->wq, &wait);
  174. spin_lock_irqsave(&self->lock, flags);
  175. while (1) {
  176. set_current_state(TASK_UNINTERRUPTIBLE);
  177. if (!self->claimed)
  178. break;
  179. spin_unlock_irqrestore(&self->lock, flags);
  180. schedule();
  181. spin_lock_irqsave(&self->lock, flags);
  182. }
  183. set_current_state(TASK_RUNNING);
  184. self->claimed = 1;
  185. spin_unlock_irqrestore(&self->lock, flags);
  186. remove_wait_queue(&self->wq, &wait);
  187. return;
  188. }
  189. static void cw1200_spi_unlock(struct hwbus_priv *self)
  190. {
  191. unsigned long flags;
  192. spin_lock_irqsave(&self->lock, flags);
  193. self->claimed = 0;
  194. spin_unlock_irqrestore(&self->lock, flags);
  195. wake_up(&self->wq);
  196. return;
  197. }
  198. static irqreturn_t cw1200_spi_irq_handler(int irq, void *dev_id)
  199. {
  200. struct hwbus_priv *self = dev_id;
  201. if (self->core) {
  202. disable_irq_nosync(self->func->irq);
  203. self->irq_disabled = 1;
  204. cw1200_irq_handler(self->core);
  205. return IRQ_HANDLED;
  206. } else {
  207. return IRQ_NONE;
  208. }
  209. }
  210. static int cw1200_spi_irq_subscribe(struct hwbus_priv *self)
  211. {
  212. int ret;
  213. pr_debug("SW IRQ subscribe\n");
  214. ret = request_any_context_irq(self->func->irq, cw1200_spi_irq_handler,
  215. IRQF_TRIGGER_HIGH,
  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. pr_debug("SW IRQ unsubscribe\n");
  231. disable_irq_wake(self->func->irq);
  232. free_irq(self->func->irq, self);
  233. return 0;
  234. }
  235. static int cw1200_spi_irq_enable(struct hwbus_priv *self, int enable)
  236. {
  237. /* Disables are handled by the interrupt handler */
  238. if (enable && self->irq_disabled) {
  239. enable_irq(self->func->irq);
  240. self->irq_disabled = 0;
  241. }
  242. return 0;
  243. }
  244. static int cw1200_spi_off(const struct cw1200_platform_data_spi *pdata)
  245. {
  246. if (pdata->reset) {
  247. gpio_set_value(pdata->reset, 0);
  248. msleep(30); /* Min is 2 * CLK32K cycles */
  249. gpio_free(pdata->reset);
  250. }
  251. if (pdata->power_ctrl)
  252. pdata->power_ctrl(pdata, false);
  253. if (pdata->clk_ctrl)
  254. pdata->clk_ctrl(pdata, false);
  255. return 0;
  256. }
  257. static int cw1200_spi_on(const struct cw1200_platform_data_spi *pdata)
  258. {
  259. /* Ensure I/Os are pulled low */
  260. if (pdata->reset) {
  261. gpio_request(pdata->reset, "cw1200_wlan_reset");
  262. gpio_direction_output(pdata->reset, 0);
  263. }
  264. if (pdata->powerup) {
  265. gpio_request(pdata->powerup, "cw1200_wlan_powerup");
  266. gpio_direction_output(pdata->powerup, 0);
  267. }
  268. if (pdata->reset || pdata->powerup)
  269. msleep(10); /* Settle time? */
  270. /* Enable 3v3 and 1v8 to hardware */
  271. if (pdata->power_ctrl) {
  272. if (pdata->power_ctrl(pdata, true)) {
  273. pr_err("power_ctrl() failed!\n");
  274. return -1;
  275. }
  276. }
  277. /* Enable CLK32K */
  278. if (pdata->clk_ctrl) {
  279. if (pdata->clk_ctrl(pdata, true)) {
  280. pr_err("clk_ctrl() failed!\n");
  281. return -1;
  282. }
  283. msleep(10); /* Delay until clock is stable for 2 cycles */
  284. }
  285. /* Enable POWERUP signal */
  286. if (pdata->powerup) {
  287. gpio_set_value(pdata->powerup, 1);
  288. msleep(250); /* or more..? */
  289. }
  290. /* Enable RSTn signal */
  291. if (pdata->reset) {
  292. gpio_set_value(pdata->reset, 1);
  293. msleep(50); /* Or more..? */
  294. }
  295. return 0;
  296. }
  297. static size_t cw1200_spi_align_size(struct hwbus_priv *self, size_t size)
  298. {
  299. return size & 1 ? size + 1 : size;
  300. }
  301. static int cw1200_spi_pm(struct hwbus_priv *self, bool suspend)
  302. {
  303. return irq_set_irq_wake(self->func->irq, suspend);
  304. }
  305. static struct hwbus_ops cw1200_spi_hwbus_ops = {
  306. .hwbus_memcpy_fromio = cw1200_spi_memcpy_fromio,
  307. .hwbus_memcpy_toio = cw1200_spi_memcpy_toio,
  308. .lock = cw1200_spi_lock,
  309. .unlock = cw1200_spi_unlock,
  310. .align_size = cw1200_spi_align_size,
  311. .power_mgmt = cw1200_spi_pm,
  312. .irq_enable = cw1200_spi_irq_enable,
  313. };
  314. /* Probe Function to be called by SPI stack when device is discovered */
  315. static int cw1200_spi_probe(struct spi_device *func)
  316. {
  317. const struct cw1200_platform_data_spi *plat_data =
  318. dev_get_platdata(&func->dev);
  319. struct hwbus_priv *self;
  320. int status;
  321. /* Sanity check speed */
  322. if (func->max_speed_hz > 52000000)
  323. func->max_speed_hz = 52000000;
  324. if (func->max_speed_hz < 1000000)
  325. func->max_speed_hz = 1000000;
  326. /* Fix up transfer size */
  327. if (plat_data->spi_bits_per_word)
  328. func->bits_per_word = plat_data->spi_bits_per_word;
  329. if (!func->bits_per_word)
  330. func->bits_per_word = 16;
  331. /* And finally.. */
  332. func->mode = SPI_MODE_0;
  333. pr_info("cw1200_wlan_spi: Probe called (CS %d M %d BPW %d CLK %d)\n",
  334. func->chip_select, func->mode, func->bits_per_word,
  335. func->max_speed_hz);
  336. if (cw1200_spi_on(plat_data)) {
  337. pr_err("spi_on() failed!\n");
  338. return -1;
  339. }
  340. if (spi_setup(func)) {
  341. pr_err("spi_setup() failed!\n");
  342. return -1;
  343. }
  344. self = kzalloc(sizeof(*self), GFP_KERNEL);
  345. if (!self) {
  346. pr_err("Can't allocate SPI hwbus_priv.");
  347. return -ENOMEM;
  348. }
  349. self->pdata = plat_data;
  350. self->func = func;
  351. spin_lock_init(&self->lock);
  352. spi_set_drvdata(func, self);
  353. init_waitqueue_head(&self->wq);
  354. status = cw1200_spi_irq_subscribe(self);
  355. status = cw1200_core_probe(&cw1200_spi_hwbus_ops,
  356. self, &func->dev, &self->core,
  357. self->pdata->ref_clk,
  358. self->pdata->macaddr,
  359. self->pdata->sdd_file,
  360. self->pdata->have_5ghz);
  361. if (status) {
  362. cw1200_spi_irq_unsubscribe(self);
  363. cw1200_spi_off(plat_data);
  364. kfree(self);
  365. }
  366. return status;
  367. }
  368. /* Disconnect Function to be called by SPI stack when device is disconnected */
  369. static int cw1200_spi_disconnect(struct spi_device *func)
  370. {
  371. struct hwbus_priv *self = spi_get_drvdata(func);
  372. if (self) {
  373. cw1200_spi_irq_unsubscribe(self);
  374. if (self->core) {
  375. cw1200_core_release(self->core);
  376. self->core = NULL;
  377. }
  378. kfree(self);
  379. }
  380. cw1200_spi_off(dev_get_platdata(&func->dev));
  381. return 0;
  382. }
  383. #ifdef CONFIG_PM
  384. static int cw1200_spi_suspend(struct device *dev, pm_message_t state)
  385. {
  386. struct hwbus_priv *self = spi_get_drvdata(to_spi_device(dev));
  387. if (!cw1200_can_suspend(self->core))
  388. return -EAGAIN;
  389. /* XXX notify host that we have to keep CW1200 powered on? */
  390. return 0;
  391. }
  392. static int cw1200_spi_resume(struct device *dev)
  393. {
  394. return 0;
  395. }
  396. #endif
  397. static struct spi_driver spi_driver = {
  398. .probe = cw1200_spi_probe,
  399. .remove = cw1200_spi_disconnect,
  400. .driver = {
  401. .name = "cw1200_wlan_spi",
  402. .bus = &spi_bus_type,
  403. .owner = THIS_MODULE,
  404. #ifdef CONFIG_PM
  405. .suspend = cw1200_spi_suspend,
  406. .resume = cw1200_spi_resume,
  407. #endif
  408. },
  409. };
  410. module_spi_driver(spi_driver);