Linux 2.6.21-rc3
[linux/fpc-iii.git] / drivers / spi / spi_s3c24xx_gpio.c
blob611ac22b7cdc2c404e69c07d8dfbcd4e73b1db7c
1 /* linux/drivers/spi/spi_s3c24xx_gpio.c
3 * Copyright (c) 2006 Ben Dooks
4 * Copyright (c) 2006 Simtec Electronics
6 * S3C24XX GPIO based SPI driver
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.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/spinlock.h>
18 #include <linux/workqueue.h>
19 #include <linux/platform_device.h>
21 #include <linux/spi/spi.h>
22 #include <linux/spi/spi_bitbang.h>
24 #include <asm/arch/regs-gpio.h>
25 #include <asm/arch/spi-gpio.h>
26 #include <asm/hardware.h>
28 struct s3c2410_spigpio {
29 struct spi_bitbang bitbang;
31 struct s3c2410_spigpio_info *info;
32 struct platform_device *dev;
35 static inline struct s3c2410_spigpio *spidev_to_sg(struct spi_device *spi)
37 return spi->controller_data;
40 static inline void setsck(struct spi_device *dev, int on)
42 struct s3c2410_spigpio *sg = spidev_to_sg(dev);
43 s3c2410_gpio_setpin(sg->info->pin_clk, on ? 1 : 0);
46 static inline void setmosi(struct spi_device *dev, int on)
48 struct s3c2410_spigpio *sg = spidev_to_sg(dev);
49 s3c2410_gpio_setpin(sg->info->pin_mosi, on ? 1 : 0);
52 static inline u32 getmiso(struct spi_device *dev)
54 struct s3c2410_spigpio *sg = spidev_to_sg(dev);
55 return s3c2410_gpio_getpin(sg->info->pin_miso) ? 1 : 0;
58 #define spidelay(x) ndelay(x)
60 #define EXPAND_BITBANG_TXRX
61 #include <linux/spi/spi_bitbang.h>
64 static u32 s3c2410_spigpio_txrx_mode0(struct spi_device *spi,
65 unsigned nsecs, u32 word, u8 bits)
67 return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
70 static u32 s3c2410_spigpio_txrx_mode1(struct spi_device *spi,
71 unsigned nsecs, u32 word, u8 bits)
73 return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
76 static u32 s3c2410_spigpio_txrx_mode2(struct spi_device *spi,
77 unsigned nsecs, u32 word, u8 bits)
79 return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
82 static u32 s3c2410_spigpio_txrx_mode3(struct spi_device *spi,
83 unsigned nsecs, u32 word, u8 bits)
85 return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
89 static void s3c2410_spigpio_chipselect(struct spi_device *dev, int value)
91 struct s3c2410_spigpio *sg = spidev_to_sg(dev);
93 if (sg->info && sg->info->chip_select)
94 (sg->info->chip_select)(sg->info, value);
97 static int s3c2410_spigpio_probe(struct platform_device *dev)
99 struct spi_master *master;
100 struct s3c2410_spigpio *sp;
101 int ret;
102 int i;
104 master = spi_alloc_master(&dev->dev, sizeof(struct s3c2410_spigpio));
105 if (master == NULL) {
106 dev_err(&dev->dev, "failed to allocate spi master\n");
107 ret = -ENOMEM;
108 goto err;
111 sp = spi_master_get_devdata(master);
113 platform_set_drvdata(dev, sp);
115 /* copy in the plkatform data */
116 sp->info = dev->dev.platform_data;
118 /* setup spi bitbang adaptor */
119 sp->bitbang.master = spi_master_get(master);
120 sp->bitbang.chipselect = s3c2410_spigpio_chipselect;
122 sp->bitbang.txrx_word[SPI_MODE_0] = s3c2410_spigpio_txrx_mode0;
123 sp->bitbang.txrx_word[SPI_MODE_1] = s3c2410_spigpio_txrx_mode1;
124 sp->bitbang.txrx_word[SPI_MODE_2] = s3c2410_spigpio_txrx_mode2;
125 sp->bitbang.txrx_word[SPI_MODE_3] = s3c2410_spigpio_txrx_mode3;
127 /* set state of spi pins */
128 s3c2410_gpio_setpin(sp->info->pin_clk, 0);
129 s3c2410_gpio_setpin(sp->info->pin_mosi, 0);
131 s3c2410_gpio_cfgpin(sp->info->pin_clk, S3C2410_GPIO_OUTPUT);
132 s3c2410_gpio_cfgpin(sp->info->pin_mosi, S3C2410_GPIO_OUTPUT);
133 s3c2410_gpio_cfgpin(sp->info->pin_miso, S3C2410_GPIO_INPUT);
135 ret = spi_bitbang_start(&sp->bitbang);
136 if (ret)
137 goto err_no_bitbang;
139 /* register the chips to go with the board */
141 for (i = 0; i < sp->info->board_size; i++) {
142 dev_info(&dev->dev, "registering %p: %s\n",
143 &sp->info->board_info[i],
144 sp->info->board_info[i].modalias);
146 sp->info->board_info[i].controller_data = sp;
147 spi_new_device(master, sp->info->board_info + i);
150 return 0;
152 err_no_bitbang:
153 spi_master_put(sp->bitbang.master);
154 err:
155 return ret;
159 static int s3c2410_spigpio_remove(struct platform_device *dev)
161 struct s3c2410_spigpio *sp = platform_get_drvdata(dev);
163 spi_bitbang_stop(&sp->bitbang);
164 spi_master_put(sp->bitbang.master);
166 return 0;
169 /* all gpio should be held over suspend/resume, so we should
170 * not need to deal with this
173 #define s3c2410_spigpio_suspend NULL
174 #define s3c2410_spigpio_resume NULL
177 static struct platform_driver s3c2410_spigpio_drv = {
178 .probe = s3c2410_spigpio_probe,
179 .remove = s3c2410_spigpio_remove,
180 .suspend = s3c2410_spigpio_suspend,
181 .resume = s3c2410_spigpio_resume,
182 .driver = {
183 .name = "s3c24xx-spi-gpio",
184 .owner = THIS_MODULE,
188 static int __init s3c2410_spigpio_init(void)
190 return platform_driver_register(&s3c2410_spigpio_drv);
193 static void __exit s3c2410_spigpio_exit(void)
195 platform_driver_unregister(&s3c2410_spigpio_drv);
198 module_init(s3c2410_spigpio_init);
199 module_exit(s3c2410_spigpio_exit);
201 MODULE_DESCRIPTION("S3C24XX SPI Driver");
202 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
203 MODULE_LICENSE("GPL");