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. #include "spi_bitbang_txrx.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, 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, 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, 0, 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, 0, 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.master->num_chipselect = info->num_chipselect;
  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. return 0;
  116. err_no_bitbang:
  117. spi_master_put(sp->bitbang.master);
  118. err:
  119. return ret;
  120. }
  121. static int s3c2410_spigpio_remove(struct platform_device *dev)
  122. {
  123. struct s3c2410_spigpio *sp = platform_get_drvdata(dev);
  124. spi_bitbang_stop(&sp->bitbang);
  125. spi_master_put(sp->bitbang.master);
  126. return 0;
  127. }
  128. /* all gpio should be held over suspend/resume, so we should
  129. * not need to deal with this
  130. */
  131. #define s3c2410_spigpio_suspend NULL
  132. #define s3c2410_spigpio_resume NULL
  133. /* work with hotplug and coldplug */
  134. MODULE_ALIAS("platform:spi_s3c24xx_gpio");
  135. static struct platform_driver s3c2410_spigpio_drv = {
  136. .probe = s3c2410_spigpio_probe,
  137. .remove = s3c2410_spigpio_remove,
  138. .suspend = s3c2410_spigpio_suspend,
  139. .resume = s3c2410_spigpio_resume,
  140. .driver = {
  141. .name = "spi_s3c24xx_gpio",
  142. .owner = THIS_MODULE,
  143. },
  144. };
  145. static int __init s3c2410_spigpio_init(void)
  146. {
  147. return platform_driver_register(&s3c2410_spigpio_drv);
  148. }
  149. static void __exit s3c2410_spigpio_exit(void)
  150. {
  151. platform_driver_unregister(&s3c2410_spigpio_drv);
  152. }
  153. module_init(s3c2410_spigpio_init);
  154. module_exit(s3c2410_spigpio_exit);
  155. MODULE_DESCRIPTION("S3C24XX SPI Driver");
  156. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  157. MODULE_LICENSE("GPL");