cw1200_spi.c 10 KB

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