spi_s3c24xx_gpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/platform_device.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/spi_bitbang.h>
  20. #include <asm/arch/regs-gpio.h>
  21. #include <asm/arch/spi-gpio.h>
  22. #include <asm/arch/hardware.h>
  23. struct s3c2410_spigpio {
  24. struct spi_bitbang bitbang;
  25. struct s3c2410_spigpio_info *info;
  26. struct platform_device *dev;
  27. };
  28. static inline struct s3c2410_spigpio *spidev_to_sg(struct spi_device *spi)
  29. {
  30. return spi->controller_data;
  31. }
  32. static inline void setsck(struct spi_device *dev, int on)
  33. {
  34. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  35. s3c2410_gpio_setpin(sg->info->pin_clk, on ? 1 : 0);
  36. }
  37. static inline void setmosi(struct spi_device *dev, int on)
  38. {
  39. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  40. s3c2410_gpio_setpin(sg->info->pin_mosi, on ? 1 : 0);
  41. }
  42. static inline u32 getmiso(struct spi_device *dev)
  43. {
  44. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  45. return s3c2410_gpio_getpin(sg->info->pin_miso) ? 1 : 0;
  46. }
  47. #define spidelay(x) ndelay(x)
  48. #define EXPAND_BITBANG_TXRX
  49. #include <linux/spi/spi_bitbang.h>
  50. static u32 s3c2410_spigpio_txrx_mode0(struct spi_device *spi,
  51. unsigned nsecs, u32 word, u8 bits)
  52. {
  53. return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
  54. }
  55. static u32 s3c2410_spigpio_txrx_mode1(struct spi_device *spi,
  56. unsigned nsecs, u32 word, u8 bits)
  57. {
  58. return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
  59. }
  60. static void s3c2410_spigpio_chipselect(struct spi_device *dev, int value)
  61. {
  62. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  63. if (sg->info && sg->info->chip_select)
  64. (sg->info->chip_select)(sg->info, value);
  65. }
  66. static int s3c2410_spigpio_probe(struct platform_device *dev)
  67. {
  68. struct spi_master *master;
  69. struct s3c2410_spigpio *sp;
  70. int ret;
  71. int i;
  72. master = spi_alloc_master(&dev->dev, sizeof(struct s3c2410_spigpio));
  73. if (master == NULL) {
  74. dev_err(&dev->dev, "failed to allocate spi master\n");
  75. ret = -ENOMEM;
  76. goto err;
  77. }
  78. sp = spi_master_get_devdata(master);
  79. platform_set_drvdata(dev, sp);
  80. /* copy in the plkatform data */
  81. sp->info = dev->dev.platform_data;
  82. /* setup spi bitbang adaptor */
  83. sp->bitbang.master = spi_master_get(master);
  84. sp->bitbang.chipselect = s3c2410_spigpio_chipselect;
  85. sp->bitbang.txrx_word[SPI_MODE_0] = s3c2410_spigpio_txrx_mode0;
  86. sp->bitbang.txrx_word[SPI_MODE_1] = s3c2410_spigpio_txrx_mode1;
  87. /* set state of spi pins */
  88. s3c2410_gpio_setpin(sp->info->pin_clk, 0);
  89. s3c2410_gpio_setpin(sp->info->pin_mosi, 0);
  90. s3c2410_gpio_cfgpin(sp->info->pin_clk, S3C2410_GPIO_OUTPUT);
  91. s3c2410_gpio_cfgpin(sp->info->pin_mosi, S3C2410_GPIO_OUTPUT);
  92. s3c2410_gpio_cfgpin(sp->info->pin_miso, S3C2410_GPIO_INPUT);
  93. ret = spi_bitbang_start(&sp->bitbang);
  94. if (ret)
  95. goto err_no_bitbang;
  96. /* register the chips to go with the board */
  97. for (i = 0; i < sp->info->board_size; i++) {
  98. dev_info(&dev->dev, "registering %p: %s\n",
  99. &sp->info->board_info[i],
  100. sp->info->board_info[i].modalias);
  101. sp->info->board_info[i].controller_data = sp;
  102. spi_new_device(master, sp->info->board_info + i);
  103. }
  104. return 0;
  105. err_no_bitbang:
  106. spi_master_put(sp->bitbang.master);
  107. err:
  108. return ret;
  109. }
  110. static int s3c2410_spigpio_remove(struct platform_device *dev)
  111. {
  112. struct s3c2410_spigpio *sp = platform_get_drvdata(dev);
  113. spi_bitbang_stop(&sp->bitbang);
  114. spi_master_put(sp->bitbang.master);
  115. return 0;
  116. }
  117. /* all gpio should be held over suspend/resume, so we should
  118. * not need to deal with this
  119. */
  120. #define s3c2410_spigpio_suspend NULL
  121. #define s3c2410_spigpio_resume NULL
  122. static struct platform_driver s3c2410_spigpio_drv = {
  123. .probe = s3c2410_spigpio_probe,
  124. .remove = s3c2410_spigpio_remove,
  125. .suspend = s3c2410_spigpio_suspend,
  126. .resume = s3c2410_spigpio_resume,
  127. .driver = {
  128. .name = "s3c24xx-spi-gpio",
  129. .owner = THIS_MODULE,
  130. },
  131. };
  132. static int __init s3c2410_spigpio_init(void)
  133. {
  134. return platform_driver_register(&s3c2410_spigpio_drv);
  135. }
  136. static void __exit s3c2410_spigpio_exit(void)
  137. {
  138. platform_driver_unregister(&s3c2410_spigpio_drv);
  139. }
  140. module_init(s3c2410_spigpio_init);
  141. module_exit(s3c2410_spigpio_exit);
  142. MODULE_DESCRIPTION("S3C24XX SPI Driver");
  143. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  144. MODULE_LICENSE("GPL");