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