spi_s3c24xx_gpio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/gpio.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/spi/spi_bitbang.h>
  22. #include <mach/regs-gpio.h>
  23. #include <mach/spi-gpio.h>
  24. #include <mach/hardware.h>
  25. struct s3c2410_spigpio {
  26. struct spi_bitbang bitbang;
  27. struct s3c2410_spigpio_info *info;
  28. struct platform_device *dev;
  29. };
  30. static inline struct s3c2410_spigpio *spidev_to_sg(struct spi_device *spi)
  31. {
  32. return spi_master_get_devdata(spi->master);
  33. }
  34. static inline void setsck(struct spi_device *dev, int on)
  35. {
  36. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  37. s3c2410_gpio_setpin(sg->info->pin_clk, on ? 1 : 0);
  38. }
  39. static inline void setmosi(struct spi_device *dev, int on)
  40. {
  41. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  42. s3c2410_gpio_setpin(sg->info->pin_mosi, on ? 1 : 0);
  43. }
  44. static inline u32 getmiso(struct spi_device *dev)
  45. {
  46. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  47. return s3c2410_gpio_getpin(sg->info->pin_miso) ? 1 : 0;
  48. }
  49. #define spidelay(x) ndelay(x)
  50. #define EXPAND_BITBANG_TXRX
  51. #include <linux/spi/spi_bitbang.h>
  52. static u32 s3c2410_spigpio_txrx_mode0(struct spi_device *spi,
  53. unsigned nsecs, u32 word, u8 bits)
  54. {
  55. return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
  56. }
  57. static u32 s3c2410_spigpio_txrx_mode1(struct spi_device *spi,
  58. unsigned nsecs, u32 word, u8 bits)
  59. {
  60. return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
  61. }
  62. static u32 s3c2410_spigpio_txrx_mode2(struct spi_device *spi,
  63. unsigned nsecs, u32 word, u8 bits)
  64. {
  65. return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
  66. }
  67. static u32 s3c2410_spigpio_txrx_mode3(struct spi_device *spi,
  68. unsigned nsecs, u32 word, u8 bits)
  69. {
  70. return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
  71. }
  72. static void s3c2410_spigpio_chipselect(struct spi_device *dev, int value)
  73. {
  74. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  75. if (sg->info && sg->info->chip_select)
  76. (sg->info->chip_select)(sg->info, value);
  77. }
  78. static int s3c2410_spigpio_probe(struct platform_device *dev)
  79. {
  80. struct s3c2410_spigpio_info *info;
  81. struct spi_master *master;
  82. struct s3c2410_spigpio *sp;
  83. int ret;
  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.master->num_chipselect = info->num_chipselect;
  98. sp->bitbang.chipselect = s3c2410_spigpio_chipselect;
  99. sp->bitbang.txrx_word[SPI_MODE_0] = s3c2410_spigpio_txrx_mode0;
  100. sp->bitbang.txrx_word[SPI_MODE_1] = s3c2410_spigpio_txrx_mode1;
  101. sp->bitbang.txrx_word[SPI_MODE_2] = s3c2410_spigpio_txrx_mode2;
  102. sp->bitbang.txrx_word[SPI_MODE_3] = s3c2410_spigpio_txrx_mode3;
  103. /* set state of spi pins, always assume that the clock is
  104. * available, but do check the MOSI and MISO. */
  105. s3c2410_gpio_setpin(info->pin_clk, 0);
  106. s3c2410_gpio_cfgpin(info->pin_clk, S3C2410_GPIO_OUTPUT);
  107. if (info->pin_mosi < S3C2410_GPH10) {
  108. s3c2410_gpio_setpin(info->pin_mosi, 0);
  109. s3c2410_gpio_cfgpin(info->pin_mosi, S3C2410_GPIO_OUTPUT);
  110. }
  111. if (info->pin_miso != S3C2410_GPA0 && info->pin_miso < S3C2410_GPH10)
  112. s3c2410_gpio_cfgpin(info->pin_miso, S3C2410_GPIO_INPUT);
  113. ret = spi_bitbang_start(&sp->bitbang);
  114. if (ret)
  115. goto err_no_bitbang;
  116. return 0;
  117. err_no_bitbang:
  118. spi_master_put(sp->bitbang.master);
  119. err:
  120. return ret;
  121. }
  122. static int s3c2410_spigpio_remove(struct platform_device *dev)
  123. {
  124. struct s3c2410_spigpio *sp = platform_get_drvdata(dev);
  125. spi_bitbang_stop(&sp->bitbang);
  126. spi_master_put(sp->bitbang.master);
  127. return 0;
  128. }
  129. /* all gpio should be held over suspend/resume, so we should
  130. * not need to deal with this
  131. */
  132. #define s3c2410_spigpio_suspend NULL
  133. #define s3c2410_spigpio_resume NULL
  134. /* work with hotplug and coldplug */
  135. MODULE_ALIAS("platform:spi_s3c24xx_gpio");
  136. static struct platform_driver s3c2410_spigpio_drv = {
  137. .probe = s3c2410_spigpio_probe,
  138. .remove = s3c2410_spigpio_remove,
  139. .suspend = s3c2410_spigpio_suspend,
  140. .resume = s3c2410_spigpio_resume,
  141. .driver = {
  142. .name = "spi_s3c24xx_gpio",
  143. .owner = THIS_MODULE,
  144. },
  145. };
  146. static int __init s3c2410_spigpio_init(void)
  147. {
  148. return platform_driver_register(&s3c2410_spigpio_drv);
  149. }
  150. static void __exit s3c2410_spigpio_exit(void)
  151. {
  152. platform_driver_unregister(&s3c2410_spigpio_drv);
  153. }
  154. module_init(s3c2410_spigpio_init);
  155. module_exit(s3c2410_spigpio_exit);
  156. MODULE_DESCRIPTION("S3C24XX SPI Driver");
  157. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  158. MODULE_LICENSE("GPL");