spi_s3c24xx.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* linux/drivers/spi/spi_s3c24xx.c
  2. *
  3. * Copyright (c) 2006 Ben Dooks
  4. * Copyright (c) 2006 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/errno.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/gpio.h>
  22. #include <linux/io.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/spi/spi_bitbang.h>
  25. #include <plat/regs-spi.h>
  26. #include <mach/spi.h>
  27. struct s3c24xx_spi {
  28. /* bitbang has to be first */
  29. struct spi_bitbang bitbang;
  30. struct completion done;
  31. void __iomem *regs;
  32. int irq;
  33. int len;
  34. int count;
  35. void (*set_cs)(struct s3c2410_spi_info *spi,
  36. int cs, int pol);
  37. /* data buffers */
  38. const unsigned char *tx;
  39. unsigned char *rx;
  40. struct clk *clk;
  41. struct resource *ioarea;
  42. struct spi_master *master;
  43. struct spi_device *curdev;
  44. struct device *dev;
  45. struct s3c2410_spi_info *pdata;
  46. };
  47. #define SPCON_DEFAULT (S3C2410_SPCON_MSTR | S3C2410_SPCON_SMOD_INT)
  48. #define SPPIN_DEFAULT (S3C2410_SPPIN_KEEP)
  49. static inline struct s3c24xx_spi *to_hw(struct spi_device *sdev)
  50. {
  51. return spi_master_get_devdata(sdev->master);
  52. }
  53. static void s3c24xx_spi_gpiocs(struct s3c2410_spi_info *spi, int cs, int pol)
  54. {
  55. gpio_set_value(spi->pin_cs, pol);
  56. }
  57. static void s3c24xx_spi_chipsel(struct spi_device *spi, int value)
  58. {
  59. struct s3c24xx_spi *hw = to_hw(spi);
  60. unsigned int cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;
  61. unsigned int spcon;
  62. switch (value) {
  63. case BITBANG_CS_INACTIVE:
  64. hw->set_cs(hw->pdata, spi->chip_select, cspol^1);
  65. break;
  66. case BITBANG_CS_ACTIVE:
  67. spcon = readb(hw->regs + S3C2410_SPCON);
  68. if (spi->mode & SPI_CPHA)
  69. spcon |= S3C2410_SPCON_CPHA_FMTB;
  70. else
  71. spcon &= ~S3C2410_SPCON_CPHA_FMTB;
  72. if (spi->mode & SPI_CPOL)
  73. spcon |= S3C2410_SPCON_CPOL_HIGH;
  74. else
  75. spcon &= ~S3C2410_SPCON_CPOL_HIGH;
  76. spcon |= S3C2410_SPCON_ENSCK;
  77. /* write new configration */
  78. writeb(spcon, hw->regs + S3C2410_SPCON);
  79. hw->set_cs(hw->pdata, spi->chip_select, cspol);
  80. break;
  81. }
  82. }
  83. static int s3c24xx_spi_setupxfer(struct spi_device *spi,
  84. struct spi_transfer *t)
  85. {
  86. struct s3c24xx_spi *hw = to_hw(spi);
  87. unsigned int bpw;
  88. unsigned int hz;
  89. unsigned int div;
  90. unsigned long clk;
  91. bpw = t ? t->bits_per_word : spi->bits_per_word;
  92. hz = t ? t->speed_hz : spi->max_speed_hz;
  93. if (!bpw)
  94. bpw = 8;
  95. if (!hz)
  96. hz = spi->max_speed_hz;
  97. if (bpw != 8) {
  98. dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
  99. return -EINVAL;
  100. }
  101. clk = clk_get_rate(hw->clk);
  102. div = DIV_ROUND_UP(clk, hz * 2) - 1;
  103. if (div > 255)
  104. div = 255;
  105. dev_dbg(&spi->dev, "setting pre-scaler to %d (wanted %d, got %ld)\n",
  106. div, hz, clk / (2 * (div + 1)));
  107. writeb(div, hw->regs + S3C2410_SPPRE);
  108. spin_lock(&hw->bitbang.lock);
  109. if (!hw->bitbang.busy) {
  110. hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
  111. /* need to ndelay for 0.5 clocktick ? */
  112. }
  113. spin_unlock(&hw->bitbang.lock);
  114. return 0;
  115. }
  116. static int s3c24xx_spi_setup(struct spi_device *spi)
  117. {
  118. int ret;
  119. ret = s3c24xx_spi_setupxfer(spi, NULL);
  120. if (ret < 0) {
  121. dev_err(&spi->dev, "setupxfer returned %d\n", ret);
  122. return ret;
  123. }
  124. return 0;
  125. }
  126. static inline unsigned int hw_txbyte(struct s3c24xx_spi *hw, int count)
  127. {
  128. return hw->tx ? hw->tx[count] : 0;
  129. }
  130. static int s3c24xx_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  131. {
  132. struct s3c24xx_spi *hw = to_hw(spi);
  133. dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
  134. t->tx_buf, t->rx_buf, t->len);
  135. hw->tx = t->tx_buf;
  136. hw->rx = t->rx_buf;
  137. hw->len = t->len;
  138. hw->count = 0;
  139. init_completion(&hw->done);
  140. /* send the first byte */
  141. writeb(hw_txbyte(hw, 0), hw->regs + S3C2410_SPTDAT);
  142. wait_for_completion(&hw->done);
  143. return hw->count;
  144. }
  145. static irqreturn_t s3c24xx_spi_irq(int irq, void *dev)
  146. {
  147. struct s3c24xx_spi *hw = dev;
  148. unsigned int spsta = readb(hw->regs + S3C2410_SPSTA);
  149. unsigned int count = hw->count;
  150. if (spsta & S3C2410_SPSTA_DCOL) {
  151. dev_dbg(hw->dev, "data-collision\n");
  152. complete(&hw->done);
  153. goto irq_done;
  154. }
  155. if (!(spsta & S3C2410_SPSTA_READY)) {
  156. dev_dbg(hw->dev, "spi not ready for tx?\n");
  157. complete(&hw->done);
  158. goto irq_done;
  159. }
  160. hw->count++;
  161. if (hw->rx)
  162. hw->rx[count] = readb(hw->regs + S3C2410_SPRDAT);
  163. count++;
  164. if (count < hw->len)
  165. writeb(hw_txbyte(hw, count), hw->regs + S3C2410_SPTDAT);
  166. else
  167. complete(&hw->done);
  168. irq_done:
  169. return IRQ_HANDLED;
  170. }
  171. static void s3c24xx_spi_initialsetup(struct s3c24xx_spi *hw)
  172. {
  173. /* for the moment, permanently enable the clock */
  174. clk_enable(hw->clk);
  175. /* program defaults into the registers */
  176. writeb(0xff, hw->regs + S3C2410_SPPRE);
  177. writeb(SPPIN_DEFAULT, hw->regs + S3C2410_SPPIN);
  178. writeb(SPCON_DEFAULT, hw->regs + S3C2410_SPCON);
  179. if (hw->pdata) {
  180. if (hw->set_cs == s3c24xx_spi_gpiocs)
  181. gpio_direction_output(hw->pdata->pin_cs, 1);
  182. if (hw->pdata->gpio_setup)
  183. hw->pdata->gpio_setup(hw->pdata, 1);
  184. }
  185. }
  186. static int __init s3c24xx_spi_probe(struct platform_device *pdev)
  187. {
  188. struct s3c2410_spi_info *pdata;
  189. struct s3c24xx_spi *hw;
  190. struct spi_master *master;
  191. struct resource *res;
  192. int err = 0;
  193. master = spi_alloc_master(&pdev->dev, sizeof(struct s3c24xx_spi));
  194. if (master == NULL) {
  195. dev_err(&pdev->dev, "No memory for spi_master\n");
  196. err = -ENOMEM;
  197. goto err_nomem;
  198. }
  199. hw = spi_master_get_devdata(master);
  200. memset(hw, 0, sizeof(struct s3c24xx_spi));
  201. hw->master = spi_master_get(master);
  202. hw->pdata = pdata = pdev->dev.platform_data;
  203. hw->dev = &pdev->dev;
  204. if (pdata == NULL) {
  205. dev_err(&pdev->dev, "No platform data supplied\n");
  206. err = -ENOENT;
  207. goto err_no_pdata;
  208. }
  209. platform_set_drvdata(pdev, hw);
  210. init_completion(&hw->done);
  211. /* setup the master state. */
  212. /* the spi->mode bits understood by this driver: */
  213. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  214. master->num_chipselect = hw->pdata->num_cs;
  215. master->bus_num = pdata->bus_num;
  216. /* setup the state for the bitbang driver */
  217. hw->bitbang.master = hw->master;
  218. hw->bitbang.setup_transfer = s3c24xx_spi_setupxfer;
  219. hw->bitbang.chipselect = s3c24xx_spi_chipsel;
  220. hw->bitbang.txrx_bufs = s3c24xx_spi_txrx;
  221. hw->bitbang.master->setup = s3c24xx_spi_setup;
  222. dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);
  223. /* find and map our resources */
  224. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  225. if (res == NULL) {
  226. dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
  227. err = -ENOENT;
  228. goto err_no_iores;
  229. }
  230. hw->ioarea = request_mem_region(res->start, resource_size(res),
  231. pdev->name);
  232. if (hw->ioarea == NULL) {
  233. dev_err(&pdev->dev, "Cannot reserve region\n");
  234. err = -ENXIO;
  235. goto err_no_iores;
  236. }
  237. hw->regs = ioremap(res->start, resource_size(res));
  238. if (hw->regs == NULL) {
  239. dev_err(&pdev->dev, "Cannot map IO\n");
  240. err = -ENXIO;
  241. goto err_no_iomap;
  242. }
  243. hw->irq = platform_get_irq(pdev, 0);
  244. if (hw->irq < 0) {
  245. dev_err(&pdev->dev, "No IRQ specified\n");
  246. err = -ENOENT;
  247. goto err_no_irq;
  248. }
  249. err = request_irq(hw->irq, s3c24xx_spi_irq, 0, pdev->name, hw);
  250. if (err) {
  251. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  252. goto err_no_irq;
  253. }
  254. hw->clk = clk_get(&pdev->dev, "spi");
  255. if (IS_ERR(hw->clk)) {
  256. dev_err(&pdev->dev, "No clock for device\n");
  257. err = PTR_ERR(hw->clk);
  258. goto err_no_clk;
  259. }
  260. /* setup any gpio we can */
  261. if (!pdata->set_cs) {
  262. if (pdata->pin_cs < 0) {
  263. dev_err(&pdev->dev, "No chipselect pin\n");
  264. goto err_register;
  265. }
  266. err = gpio_request(pdata->pin_cs, dev_name(&pdev->dev));
  267. if (err) {
  268. dev_err(&pdev->dev, "Failed to get gpio for cs\n");
  269. goto err_register;
  270. }
  271. hw->set_cs = s3c24xx_spi_gpiocs;
  272. gpio_direction_output(pdata->pin_cs, 1);
  273. } else
  274. hw->set_cs = pdata->set_cs;
  275. s3c24xx_spi_initialsetup(hw);
  276. /* register our spi controller */
  277. err = spi_bitbang_start(&hw->bitbang);
  278. if (err) {
  279. dev_err(&pdev->dev, "Failed to register SPI master\n");
  280. goto err_register;
  281. }
  282. return 0;
  283. err_register:
  284. if (hw->set_cs == s3c24xx_spi_gpiocs)
  285. gpio_free(pdata->pin_cs);
  286. clk_disable(hw->clk);
  287. clk_put(hw->clk);
  288. err_no_clk:
  289. free_irq(hw->irq, hw);
  290. err_no_irq:
  291. iounmap(hw->regs);
  292. err_no_iomap:
  293. release_resource(hw->ioarea);
  294. kfree(hw->ioarea);
  295. err_no_iores:
  296. err_no_pdata:
  297. spi_master_put(hw->master);
  298. err_nomem:
  299. return err;
  300. }
  301. static int __exit s3c24xx_spi_remove(struct platform_device *dev)
  302. {
  303. struct s3c24xx_spi *hw = platform_get_drvdata(dev);
  304. platform_set_drvdata(dev, NULL);
  305. spi_unregister_master(hw->master);
  306. clk_disable(hw->clk);
  307. clk_put(hw->clk);
  308. free_irq(hw->irq, hw);
  309. iounmap(hw->regs);
  310. if (hw->set_cs == s3c24xx_spi_gpiocs)
  311. gpio_free(hw->pdata->pin_cs);
  312. release_resource(hw->ioarea);
  313. kfree(hw->ioarea);
  314. spi_master_put(hw->master);
  315. return 0;
  316. }
  317. #ifdef CONFIG_PM
  318. static int s3c24xx_spi_suspend(struct device *dev)
  319. {
  320. struct s3c24xx_spi *hw = platform_get_drvdata(to_platform_device(dev));
  321. if (hw->pdata && hw->pdata->gpio_setup)
  322. hw->pdata->gpio_setup(hw->pdata, 0);
  323. clk_disable(hw->clk);
  324. return 0;
  325. }
  326. static int s3c24xx_spi_resume(struct device *dev)
  327. {
  328. struct s3c24xx_spi *hw = platform_get_drvdata(to_platform_device(dev));
  329. s3c24xx_spi_initialsetup(hw);
  330. return 0;
  331. }
  332. static struct dev_pm_ops s3c24xx_spi_pmops = {
  333. .suspend = s3c24xx_spi_suspend,
  334. .resume = s3c24xx_spi_resume,
  335. };
  336. #define S3C24XX_SPI_PMOPS &s3c24xx_spi_pmops
  337. #else
  338. #define S3C24XX_SPI_PMOPS NULL
  339. #endif /* CONFIG_PM */
  340. MODULE_ALIAS("platform:s3c2410-spi");
  341. static struct platform_driver s3c24xx_spi_driver = {
  342. .remove = __exit_p(s3c24xx_spi_remove),
  343. .driver = {
  344. .name = "s3c2410-spi",
  345. .owner = THIS_MODULE,
  346. .pm = S3C24XX_SPI_PMOPS,
  347. },
  348. };
  349. static int __init s3c24xx_spi_init(void)
  350. {
  351. return platform_driver_probe(&s3c24xx_spi_driver, s3c24xx_spi_probe);
  352. }
  353. static void __exit s3c24xx_spi_exit(void)
  354. {
  355. platform_driver_unregister(&s3c24xx_spi_driver);
  356. }
  357. module_init(s3c24xx_spi_init);
  358. module_exit(s3c24xx_spi_exit);
  359. MODULE_DESCRIPTION("S3C24XX SPI Driver");
  360. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  361. MODULE_LICENSE("GPL");