spi_s3c24xx_gpio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <mach/regs-gpio.h>
  22. #include <mach/spi-gpio.h>
  23. #include <mach/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. master = spi_alloc_master(&dev->dev, sizeof(struct s3c2410_spigpio));
  84. if (master == NULL) {
  85. dev_err(&dev->dev, "failed to allocate spi master\n");
  86. ret = -ENOMEM;
  87. goto err;
  88. }
  89. sp = spi_master_get_devdata(master);
  90. platform_set_drvdata(dev, sp);
  91. /* copy in the plkatform data */
  92. info = sp->info = dev->dev.platform_data;
  93. /* setup spi bitbang adaptor */
  94. sp->bitbang.master = spi_master_get(master);
  95. sp->bitbang.master->bus_num = info->bus_num;
  96. sp->bitbang.chipselect = s3c2410_spigpio_chipselect;
  97. sp->bitbang.txrx_word[SPI_MODE_0] = s3c2410_spigpio_txrx_mode0;
  98. sp->bitbang.txrx_word[SPI_MODE_1] = s3c2410_spigpio_txrx_mode1;
  99. sp->bitbang.txrx_word[SPI_MODE_2] = s3c2410_spigpio_txrx_mode2;
  100. sp->bitbang.txrx_word[SPI_MODE_3] = s3c2410_spigpio_txrx_mode3;
  101. /* set state of spi pins, always assume that the clock is
  102. * available, but do check the MOSI and MISO. */
  103. s3c2410_gpio_setpin(info->pin_clk, 0);
  104. s3c2410_gpio_cfgpin(info->pin_clk, S3C2410_GPIO_OUTPUT);
  105. if (info->pin_mosi < S3C2410_GPH10) {
  106. s3c2410_gpio_setpin(info->pin_mosi, 0);
  107. s3c2410_gpio_cfgpin(info->pin_mosi, S3C2410_GPIO_OUTPUT);
  108. }
  109. if (info->pin_miso != S3C2410_GPA0 && info->pin_miso < S3C2410_GPH10)
  110. s3c2410_gpio_cfgpin(info->pin_miso, S3C2410_GPIO_INPUT);
  111. ret = spi_bitbang_start(&sp->bitbang);
  112. if (ret)
  113. goto err_no_bitbang;
  114. return 0;
  115. err_no_bitbang:
  116. spi_master_put(sp->bitbang.master);
  117. err:
  118. return ret;
  119. }
  120. static int s3c2410_spigpio_remove(struct platform_device *dev)
  121. {
  122. struct s3c2410_spigpio *sp = platform_get_drvdata(dev);
  123. spi_bitbang_stop(&sp->bitbang);
  124. spi_master_put(sp->bitbang.master);
  125. return 0;
  126. }
  127. /* all gpio should be held over suspend/resume, so we should
  128. * not need to deal with this
  129. */
  130. #define s3c2410_spigpio_suspend NULL
  131. #define s3c2410_spigpio_resume NULL
  132. /* work with hotplug and coldplug */
  133. MODULE_ALIAS("platform:spi_s3c24xx_gpio");
  134. static struct platform_driver s3c2410_spigpio_drv = {
  135. .probe = s3c2410_spigpio_probe,
  136. .remove = s3c2410_spigpio_remove,
  137. .suspend = s3c2410_spigpio_suspend,
  138. .resume = s3c2410_spigpio_resume,
  139. .driver = {
  140. .name = "spi_s3c24xx_gpio",
  141. .owner = THIS_MODULE,
  142. },
  143. };
  144. static int __init s3c2410_spigpio_init(void)
  145. {
  146. return platform_driver_register(&s3c2410_spigpio_drv);
  147. }
  148. static void __exit s3c2410_spigpio_exit(void)
  149. {
  150. platform_driver_unregister(&s3c2410_spigpio_drv);
  151. }
  152. module_init(s3c2410_spigpio_init);
  153. module_exit(s3c2410_spigpio_exit);
  154. MODULE_DESCRIPTION("S3C24XX SPI Driver");
  155. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  156. MODULE_LICENSE("GPL");