cw1200_spi.c 10 KB

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