spi_s3c24xx.c 11 KB

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