spi_s3c24xx_gpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 void s3c2410_spigpio_chipselect(struct spi_device *dev, int value)
  62. {
  63. struct s3c2410_spigpio *sg = spidev_to_sg(dev);
  64. if (sg->info && sg->info->chip_select)
  65. (sg->info->chip_select)(sg->info, value);
  66. }
  67. static int s3c2410_spigpio_probe(struct platform_device *dev)
  68. {
  69. struct spi_master *master;
  70. struct s3c2410_spigpio *sp;
  71. int ret;
  72. int i;
  73. master = spi_alloc_master(&dev->dev, sizeof(struct s3c2410_spigpio));
  74. if (master == NULL) {
  75. dev_err(&dev->dev, "failed to allocate spi master\n");
  76. ret = -ENOMEM;
  77. goto err;
  78. }
  79. sp = spi_master_get_devdata(master);
  80. platform_set_drvdata(dev, sp);
  81. /* copy in the plkatform data */
  82. sp->info = dev->dev.platform_data;
  83. /* setup spi bitbang adaptor */
  84. sp->bitbang.master = spi_master_get(master);
  85. sp->bitbang.chipselect = s3c2410_spigpio_chipselect;
  86. sp->bitbang.txrx_word[SPI_MODE_0] = s3c2410_spigpio_txrx_mode0;
  87. sp->bitbang.txrx_word[SPI_MODE_1] = s3c2410_spigpio_txrx_mode1;
  88. /* set state of spi pins */
  89. s3c2410_gpio_setpin(sp->info->pin_clk, 0);
  90. s3c2410_gpio_setpin(sp->info->pin_mosi, 0);
  91. s3c2410_gpio_cfgpin(sp->info->pin_clk, S3C2410_GPIO_OUTPUT);
  92. s3c2410_gpio_cfgpin(sp->info->pin_mosi, S3C2410_GPIO_OUTPUT);
  93. s3c2410_gpio_cfgpin(sp->info->pin_miso, S3C2410_GPIO_INPUT);
  94. ret = spi_bitbang_start(&sp->bitbang);
  95. if (ret)
  96. goto err_no_bitbang;
  97. /* register the chips to go with the board */
  98. for (i = 0; i < sp->info->board_size; i++) {
  99. dev_info(&dev->dev, "registering %p: %s\n",
  100. &sp->info->board_info[i],
  101. sp->info->board_info[i].modalias);
  102. sp->info->board_info[i].controller_data = sp;
  103. spi_new_device(master, sp->info->board_info + i);
  104. }
  105. return 0;
  106. err_no_bitbang:
  107. spi_master_put(sp->bitbang.master);
  108. err:
  109. return ret;
  110. }
  111. static int s3c2410_spigpio_remove(struct platform_device *dev)
  112. {
  113. struct s3c2410_spigpio *sp = platform_get_drvdata(dev);
  114. spi_bitbang_stop(&sp->bitbang);
  115. spi_master_put(sp->bitbang.master);
  116. return 0;
  117. }
  118. /* all gpio should be held over suspend/resume, so we should
  119. * not need to deal with this
  120. */
  121. #define s3c2410_spigpio_suspend NULL
  122. #define s3c2410_spigpio_resume NULL
  123. static struct platform_driver s3c2410_spigpio_drv = {
  124. .probe = s3c2410_spigpio_probe,
  125. .remove = s3c2410_spigpio_remove,
  126. .suspend = s3c2410_spigpio_suspend,
  127. .resume = s3c2410_spigpio_resume,
  128. .driver = {
  129. .name = "s3c24xx-spi-gpio",
  130. .owner = THIS_MODULE,
  131. },
  132. };
  133. static int __init s3c2410_spigpio_init(void)
  134. {
  135. return platform_driver_register(&s3c2410_spigpio_drv);
  136. }
  137. static void __exit s3c2410_spigpio_exit(void)
  138. {
  139. platform_driver_unregister(&s3c2410_spigpio_drv);
  140. }
  141. module_init(s3c2410_spigpio_init);
  142. module_exit(s3c2410_spigpio_exit);
  143. MODULE_DESCRIPTION("S3C24XX SPI Driver");
  144. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  145. MODULE_LICENSE("GPL");