| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 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 |  | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> | 
 | 15 | #include <linux/init.h> | 
 | 16 | #include <linux/delay.h> | 
 | 17 | #include <linux/spinlock.h> | 
| Ben Dooks | 6d3a25f | 2006-12-22 01:11:45 -0800 | [diff] [blame] | 18 | #include <linux/workqueue.h> | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 19 | #include <linux/platform_device.h> | 
| Ben Dooks | ec976d6 | 2009-05-13 22:52:24 +0100 | [diff] [blame] | 20 | #include <linux/gpio.h> | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 21 |  | 
 | 22 | #include <linux/spi/spi.h> | 
 | 23 | #include <linux/spi/spi_bitbang.h> | 
 | 24 |  | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 25 | #include <mach/regs-gpio.h> | 
 | 26 | #include <mach/spi-gpio.h> | 
 | 27 | #include <mach/hardware.h> | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 28 |  | 
 | 29 | struct s3c2410_spigpio { | 
 | 30 | 	struct spi_bitbang		 bitbang; | 
 | 31 |  | 
 | 32 | 	struct s3c2410_spigpio_info	*info; | 
 | 33 | 	struct platform_device		*dev; | 
 | 34 | }; | 
 | 35 |  | 
 | 36 | static inline struct s3c2410_spigpio *spidev_to_sg(struct spi_device *spi) | 
 | 37 | { | 
| Ben Dooks | e39ea8a | 2008-12-01 13:13:56 -0800 | [diff] [blame] | 38 | 	return spi_master_get_devdata(spi->master); | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 39 | } | 
 | 40 |  | 
 | 41 | static inline void setsck(struct spi_device *dev, int on) | 
 | 42 | { | 
 | 43 | 	struct s3c2410_spigpio *sg = spidev_to_sg(dev); | 
 | 44 | 	s3c2410_gpio_setpin(sg->info->pin_clk, on ? 1 : 0); | 
 | 45 | } | 
 | 46 |  | 
 | 47 | static inline void setmosi(struct spi_device *dev, int on) | 
 | 48 | { | 
 | 49 | 	struct s3c2410_spigpio *sg = spidev_to_sg(dev); | 
 | 50 | 	s3c2410_gpio_setpin(sg->info->pin_mosi, on ? 1 : 0); | 
 | 51 | } | 
 | 52 |  | 
 | 53 | static inline u32 getmiso(struct spi_device *dev) | 
 | 54 | { | 
 | 55 | 	struct s3c2410_spigpio *sg = spidev_to_sg(dev); | 
 | 56 | 	return s3c2410_gpio_getpin(sg->info->pin_miso) ? 1 : 0; | 
 | 57 | } | 
 | 58 |  | 
 | 59 | #define spidelay(x) ndelay(x) | 
 | 60 |  | 
 | 61 | #define	EXPAND_BITBANG_TXRX | 
 | 62 | #include <linux/spi/spi_bitbang.h> | 
 | 63 |  | 
 | 64 |  | 
 | 65 | static u32 s3c2410_spigpio_txrx_mode0(struct spi_device *spi, | 
 | 66 | 				      unsigned nsecs, u32 word, u8 bits) | 
 | 67 | { | 
 | 68 | 	return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits); | 
 | 69 | } | 
 | 70 |  | 
 | 71 | static u32 s3c2410_spigpio_txrx_mode1(struct spi_device *spi, | 
 | 72 | 				      unsigned nsecs, u32 word, u8 bits) | 
 | 73 | { | 
 | 74 | 	return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits); | 
 | 75 | } | 
 | 76 |  | 
| Harald Welte | d23c6c2 | 2007-02-20 13:58:19 -0800 | [diff] [blame] | 77 | static u32 s3c2410_spigpio_txrx_mode2(struct spi_device *spi, | 
 | 78 | 				      unsigned nsecs, u32 word, u8 bits) | 
 | 79 | { | 
 | 80 | 	return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits); | 
 | 81 | } | 
 | 82 |  | 
 | 83 | static u32 s3c2410_spigpio_txrx_mode3(struct spi_device *spi, | 
 | 84 | 				      unsigned nsecs, u32 word, u8 bits) | 
 | 85 | { | 
 | 86 | 	return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits); | 
 | 87 | } | 
 | 88 |  | 
 | 89 |  | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 90 | static void s3c2410_spigpio_chipselect(struct spi_device *dev, int value) | 
 | 91 | { | 
 | 92 | 	struct s3c2410_spigpio *sg = spidev_to_sg(dev); | 
 | 93 |  | 
 | 94 | 	if (sg->info && sg->info->chip_select) | 
 | 95 | 		(sg->info->chip_select)(sg->info, value); | 
 | 96 | } | 
 | 97 |  | 
 | 98 | static int s3c2410_spigpio_probe(struct platform_device *dev) | 
 | 99 | { | 
| Ben Dooks | 438ae1a | 2007-11-28 16:21:29 -0800 | [diff] [blame] | 100 | 	struct s3c2410_spigpio_info *info; | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 101 | 	struct spi_master	*master; | 
 | 102 | 	struct s3c2410_spigpio  *sp; | 
 | 103 | 	int ret; | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 104 |  | 
 | 105 | 	master = spi_alloc_master(&dev->dev, sizeof(struct s3c2410_spigpio)); | 
 | 106 | 	if (master == NULL) { | 
 | 107 | 		dev_err(&dev->dev, "failed to allocate spi master\n"); | 
 | 108 | 		ret = -ENOMEM; | 
 | 109 | 		goto err; | 
 | 110 | 	} | 
 | 111 |  | 
 | 112 | 	sp = spi_master_get_devdata(master); | 
 | 113 |  | 
 | 114 | 	platform_set_drvdata(dev, sp); | 
 | 115 |  | 
 | 116 | 	/* copy in the plkatform data */ | 
| Ben Dooks | 438ae1a | 2007-11-28 16:21:29 -0800 | [diff] [blame] | 117 | 	info = sp->info = dev->dev.platform_data; | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 118 |  | 
 | 119 | 	/* setup spi bitbang adaptor */ | 
 | 120 | 	sp->bitbang.master = spi_master_get(master); | 
| David Brownell | 75d4279 | 2007-11-28 16:21:30 -0800 | [diff] [blame] | 121 | 	sp->bitbang.master->bus_num = info->bus_num; | 
| Ben Dooks | b93c35f | 2008-12-01 13:13:57 -0800 | [diff] [blame] | 122 | 	sp->bitbang.master->num_chipselect = info->num_chipselect; | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 123 | 	sp->bitbang.chipselect = s3c2410_spigpio_chipselect; | 
 | 124 |  | 
 | 125 | 	sp->bitbang.txrx_word[SPI_MODE_0] = s3c2410_spigpio_txrx_mode0; | 
 | 126 | 	sp->bitbang.txrx_word[SPI_MODE_1] = s3c2410_spigpio_txrx_mode1; | 
| Harald Welte | d23c6c2 | 2007-02-20 13:58:19 -0800 | [diff] [blame] | 127 | 	sp->bitbang.txrx_word[SPI_MODE_2] = s3c2410_spigpio_txrx_mode2; | 
 | 128 | 	sp->bitbang.txrx_word[SPI_MODE_3] = s3c2410_spigpio_txrx_mode3; | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 129 |  | 
| Ben Dooks | 438ae1a | 2007-11-28 16:21:29 -0800 | [diff] [blame] | 130 | 	/* set state of spi pins, always assume that the clock is | 
 | 131 | 	 * available, but do check the MOSI and MISO. */ | 
 | 132 | 	s3c2410_gpio_setpin(info->pin_clk, 0); | 
 | 133 | 	s3c2410_gpio_cfgpin(info->pin_clk, S3C2410_GPIO_OUTPUT); | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 134 |  | 
| Ben Dooks | 438ae1a | 2007-11-28 16:21:29 -0800 | [diff] [blame] | 135 | 	if (info->pin_mosi < S3C2410_GPH10) { | 
 | 136 | 		s3c2410_gpio_setpin(info->pin_mosi, 0); | 
 | 137 | 		s3c2410_gpio_cfgpin(info->pin_mosi, S3C2410_GPIO_OUTPUT); | 
 | 138 | 	} | 
 | 139 |  | 
 | 140 | 	if (info->pin_miso != S3C2410_GPA0 && info->pin_miso < S3C2410_GPH10) | 
 | 141 | 		s3c2410_gpio_cfgpin(info->pin_miso, S3C2410_GPIO_INPUT); | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 142 |  | 
 | 143 | 	ret = spi_bitbang_start(&sp->bitbang); | 
 | 144 | 	if (ret) | 
 | 145 | 		goto err_no_bitbang; | 
 | 146 |  | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 147 | 	return 0; | 
 | 148 |  | 
 | 149 |  err_no_bitbang: | 
 | 150 | 	spi_master_put(sp->bitbang.master); | 
 | 151 |  err: | 
 | 152 | 	return ret; | 
 | 153 |  | 
 | 154 | } | 
 | 155 |  | 
 | 156 | static int s3c2410_spigpio_remove(struct platform_device *dev) | 
 | 157 | { | 
 | 158 | 	struct s3c2410_spigpio *sp = platform_get_drvdata(dev); | 
 | 159 |  | 
 | 160 | 	spi_bitbang_stop(&sp->bitbang); | 
 | 161 | 	spi_master_put(sp->bitbang.master); | 
 | 162 |  | 
 | 163 | 	return 0; | 
 | 164 | } | 
 | 165 |  | 
 | 166 | /* all gpio should be held over suspend/resume, so we should | 
 | 167 |  * not need to deal with this | 
 | 168 | */ | 
 | 169 |  | 
 | 170 | #define s3c2410_spigpio_suspend NULL | 
 | 171 | #define s3c2410_spigpio_resume NULL | 
 | 172 |  | 
| Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 173 | /* work with hotplug and coldplug */ | 
 | 174 | MODULE_ALIAS("platform:spi_s3c24xx_gpio"); | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 175 |  | 
 | 176 | static struct platform_driver s3c2410_spigpio_drv = { | 
 | 177 | 	.probe		= s3c2410_spigpio_probe, | 
 | 178 |         .remove		= s3c2410_spigpio_remove, | 
 | 179 |         .suspend	= s3c2410_spigpio_suspend, | 
 | 180 |         .resume		= s3c2410_spigpio_resume, | 
 | 181 |         .driver		= { | 
| David Brownell | fc3ba95 | 2007-08-30 23:56:24 -0700 | [diff] [blame] | 182 | 		.name	= "spi_s3c24xx_gpio", | 
| Ben Dooks | 1fc7547 | 2006-05-20 15:00:17 -0700 | [diff] [blame] | 183 | 		.owner	= THIS_MODULE, | 
 | 184 |         }, | 
 | 185 | }; | 
 | 186 |  | 
 | 187 | static int __init s3c2410_spigpio_init(void) | 
 | 188 | { | 
 | 189 |         return platform_driver_register(&s3c2410_spigpio_drv); | 
 | 190 | } | 
 | 191 |  | 
 | 192 | static void __exit s3c2410_spigpio_exit(void) | 
 | 193 | { | 
 | 194 |         platform_driver_unregister(&s3c2410_spigpio_drv); | 
 | 195 | } | 
 | 196 |  | 
 | 197 | module_init(s3c2410_spigpio_init); | 
 | 198 | module_exit(s3c2410_spigpio_exit); | 
 | 199 |  | 
 | 200 | MODULE_DESCRIPTION("S3C24XX SPI Driver"); | 
 | 201 | MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); | 
 | 202 | MODULE_LICENSE("GPL"); |