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