spi_s3c24xx_gpio.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* linux/drivers/spi/spi_s3c24xx_gpio.c
  2. *
  3. * Copyright (c) 2006 Ben Dooks
  4. * Copyright (c) 2006 Simtec Electronics
  5. *
  6. * S3C24XX GPIO based SPI driver
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/spi/spi_bitbang.h>
  21. #include <asm/arch/regs-gpio.h>
  22. #include <asm/arch/spi-gpio.h>
  23. #include <asm/hardware.h>
  24. struct s3c2410_spigpio {
  25. struct spi_bitbang bitbang;
  26. struct s3c2410_spigpio_info *info;
  27. struct platform_device *dev;
  28. };
  29. static inline struct s3c2410_spigpio *spidev_to_sg(struct spi_device *spi)
  30. {
  31. return spi->controller_data;
  32. }
  33. static inline void setsck(struct spi_device *dev, int on)
  34. {
  35. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  36. s3c2410_gpio_setpin(sg->info->pin_clk, on ? 1 : 0);
  37. }
  38. static inline void setmosi(struct spi_device *dev, int on)
  39. {
  40. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  41. s3c2410_gpio_setpin(sg->info->pin_mosi, on ? 1 : 0);
  42. }
  43. static inline u32 getmiso(struct spi_device *dev)
  44. {
  45. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  46. return s3c2410_gpio_getpin(sg->info->pin_miso) ? 1 : 0;
  47. }
  48. #define spidelay(x) ndelay(x)
  49. #define EXPAND_BITBANG_TXRX
  50. #include <linux/spi/spi_bitbang.h>
  51. static u32 s3c2410_spigpio_txrx_mode0(struct spi_device *spi,
  52. unsigned nsecs, u32 word, u8 bits)
  53. {
  54. return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
  55. }
  56. static u32 s3c2410_spigpio_txrx_mode1(struct spi_device *spi,
  57. unsigned nsecs, u32 word, u8 bits)
  58. {
  59. return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
  60. }
  61. static u32 s3c2410_spigpio_txrx_mode2(struct spi_device *spi,
  62. unsigned nsecs, u32 word, u8 bits)
  63. {
  64. return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
  65. }
  66. static u32 s3c2410_spigpio_txrx_mode3(struct spi_device *spi,
  67. unsigned nsecs, u32 word, u8 bits)
  68. {
  69. return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
  70. }
  71. static void s3c2410_spigpio_chipselect(struct spi_device *dev, int value)
  72. {
  73. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  74. if (sg->info && sg->info->chip_select)
  75. (sg->info->chip_select)(sg->info, value);
  76. }
  77. static int s3c2410_spigpio_probe(struct platform_device *dev)
  78. {
  79. struct s3c2410_spigpio_info *info;
  80. struct spi_master *master;
  81. struct s3c2410_spigpio *sp;
  82. int ret;
  83. int i;
  84. master = spi_alloc_master(&dev->dev, sizeof(struct s3c2410_spigpio));
  85. if (master == NULL) {
  86. dev_err(&dev->dev, "failed to allocate spi master\n");
  87. ret = -ENOMEM;
  88. goto err;
  89. }
  90. sp = spi_master_get_devdata(master);
  91. platform_set_drvdata(dev, sp);
  92. /* copy in the plkatform data */
  93. info = sp->info = dev->dev.platform_data;
  94. /* setup spi bitbang adaptor */
  95. sp->bitbang.master = spi_master_get(master);
  96. sp->bitbang.master->bus_num = info->bus_num;
  97. sp->bitbang.chipselect = s3c2410_spigpio_chipselect;
  98. sp->bitbang.txrx_word[SPI_MODE_0] = s3c2410_spigpio_txrx_mode0;
  99. sp->bitbang.txrx_word[SPI_MODE_1] = s3c2410_spigpio_txrx_mode1;
  100. sp->bitbang.txrx_word[SPI_MODE_2] = s3c2410_spigpio_txrx_mode2;
  101. sp->bitbang.txrx_word[SPI_MODE_3] = s3c2410_spigpio_txrx_mode3;
  102. /* set state of spi pins, always assume that the clock is
  103. * available, but do check the MOSI and MISO. */
  104. s3c2410_gpio_setpin(info->pin_clk, 0);
  105. s3c2410_gpio_cfgpin(info->pin_clk, S3C2410_GPIO_OUTPUT);
  106. if (info->pin_mosi < S3C2410_GPH10) {
  107. s3c2410_gpio_setpin(info->pin_mosi, 0);
  108. s3c2410_gpio_cfgpin(info->pin_mosi, S3C2410_GPIO_OUTPUT);
  109. }
  110. if (info->pin_miso != S3C2410_GPA0 && info->pin_miso < S3C2410_GPH10)
  111. s3c2410_gpio_cfgpin(info->pin_miso, S3C2410_GPIO_INPUT);
  112. ret = spi_bitbang_start(&sp->bitbang);
  113. if (ret)
  114. goto err_no_bitbang;
  115. /* register the chips to go with the board */
  116. for (i = 0; i < sp->info->board_size; i++) {
  117. dev_info(&dev->dev, "registering %p: %s\n",
  118. &sp->info->board_info[i],
  119. sp->info->board_info[i].modalias);
  120. sp->info->board_info[i].controller_data = sp;
  121. spi_new_device(master, sp->info->board_info + i);
  122. }
  123. return 0;
  124. err_no_bitbang:
  125. spi_master_put(sp->bitbang.master);
  126. err:
  127. return ret;
  128. }
  129. static int s3c2410_spigpio_remove(struct platform_device *dev)
  130. {
  131. struct s3c2410_spigpio *sp = platform_get_drvdata(dev);
  132. spi_bitbang_stop(&sp->bitbang);
  133. spi_master_put(sp->bitbang.master);
  134. return 0;
  135. }
  136. /* all gpio should be held over suspend/resume, so we should
  137. * not need to deal with this
  138. */
  139. #define s3c2410_spigpio_suspend NULL
  140. #define s3c2410_spigpio_resume NULL
  141. static struct platform_driver s3c2410_spigpio_drv = {
  142. .probe = s3c2410_spigpio_probe,
  143. .remove = s3c2410_spigpio_remove,
  144. .suspend = s3c2410_spigpio_suspend,
  145. .resume = s3c2410_spigpio_resume,
  146. .driver = {
  147. .name = "spi_s3c24xx_gpio",
  148. .owner = THIS_MODULE,
  149. },
  150. };
  151. static int __init s3c2410_spigpio_init(void)
  152. {
  153. return platform_driver_register(&s3c2410_spigpio_drv);
  154. }
  155. static void __exit s3c2410_spigpio_exit(void)
  156. {
  157. platform_driver_unregister(&s3c2410_spigpio_drv);
  158. }
  159. module_init(s3c2410_spigpio_init);
  160. module_exit(s3c2410_spigpio_exit);
  161. MODULE_DESCRIPTION("S3C24XX SPI Driver");
  162. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  163. MODULE_LICENSE("GPL");