blob: 109d82c1abc05a9844578df75f72ad2f80af03ee [file] [log] [blame]
Ben Dooks1fc75472006-05-20 15:00:17 -07001/* 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 Dooks1fc75472006-05-20 15:00:17 -070014#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/spinlock.h>
Ben Dooks6d3a25f2006-12-22 01:11:45 -080018#include <linux/workqueue.h>
Ben Dooks1fc75472006-05-20 15:00:17 -070019#include <linux/platform_device.h>
20
21#include <linux/spi/spi.h>
22#include <linux/spi/spi_bitbang.h>
23
24#include <asm/arch/regs-gpio.h>
25#include <asm/arch/spi-gpio.h>
Arnaud Patard (Rtpe2a5d2f2006-12-29 16:49:10 -080026#include <asm/hardware.h>
Ben Dooks1fc75472006-05-20 15:00:17 -070027
28struct s3c2410_spigpio {
29 struct spi_bitbang bitbang;
30
31 struct s3c2410_spigpio_info *info;
32 struct platform_device *dev;
33};
34
35static inline struct s3c2410_spigpio *spidev_to_sg(struct spi_device *spi)
36{
37 return spi->controller_data;
38}
39
40static inline void setsck(struct spi_device *dev, int on)
41{
42 struct s3c2410_spigpio *sg = spidev_to_sg(dev);
43 s3c2410_gpio_setpin(sg->info->pin_clk, on ? 1 : 0);
44}
45
46static inline void setmosi(struct spi_device *dev, int on)
47{
48 struct s3c2410_spigpio *sg = spidev_to_sg(dev);
49 s3c2410_gpio_setpin(sg->info->pin_mosi, on ? 1 : 0);
50}
51
52static inline u32 getmiso(struct spi_device *dev)
53{
54 struct s3c2410_spigpio *sg = spidev_to_sg(dev);
55 return s3c2410_gpio_getpin(sg->info->pin_miso) ? 1 : 0;
56}
57
58#define spidelay(x) ndelay(x)
59
60#define EXPAND_BITBANG_TXRX
61#include <linux/spi/spi_bitbang.h>
62
63
64static u32 s3c2410_spigpio_txrx_mode0(struct spi_device *spi,
65 unsigned nsecs, u32 word, u8 bits)
66{
67 return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
68}
69
70static u32 s3c2410_spigpio_txrx_mode1(struct spi_device *spi,
71 unsigned nsecs, u32 word, u8 bits)
72{
73 return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
74}
75
Harald Welted23c6c22007-02-20 13:58:19 -080076static u32 s3c2410_spigpio_txrx_mode2(struct spi_device *spi,
77 unsigned nsecs, u32 word, u8 bits)
78{
79 return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
80}
81
82static u32 s3c2410_spigpio_txrx_mode3(struct spi_device *spi,
83 unsigned nsecs, u32 word, u8 bits)
84{
85 return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
86}
87
88
Ben Dooks1fc75472006-05-20 15:00:17 -070089static void s3c2410_spigpio_chipselect(struct spi_device *dev, int value)
90{
91 struct s3c2410_spigpio *sg = spidev_to_sg(dev);
92
93 if (sg->info && sg->info->chip_select)
94 (sg->info->chip_select)(sg->info, value);
95}
96
97static int s3c2410_spigpio_probe(struct platform_device *dev)
98{
Ben Dooks438ae1a2007-11-28 16:21:29 -080099 struct s3c2410_spigpio_info *info;
Ben Dooks1fc75472006-05-20 15:00:17 -0700100 struct spi_master *master;
101 struct s3c2410_spigpio *sp;
102 int ret;
103 int i;
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 Dooks438ae1a2007-11-28 16:21:29 -0800117 info = sp->info = dev->dev.platform_data;
Ben Dooks1fc75472006-05-20 15:00:17 -0700118
119 /* setup spi bitbang adaptor */
120 sp->bitbang.master = spi_master_get(master);
David Brownell75d42792007-11-28 16:21:30 -0800121 sp->bitbang.master->bus_num = info->bus_num;
Ben Dooks1fc75472006-05-20 15:00:17 -0700122 sp->bitbang.chipselect = s3c2410_spigpio_chipselect;
123
124 sp->bitbang.txrx_word[SPI_MODE_0] = s3c2410_spigpio_txrx_mode0;
125 sp->bitbang.txrx_word[SPI_MODE_1] = s3c2410_spigpio_txrx_mode1;
Harald Welted23c6c22007-02-20 13:58:19 -0800126 sp->bitbang.txrx_word[SPI_MODE_2] = s3c2410_spigpio_txrx_mode2;
127 sp->bitbang.txrx_word[SPI_MODE_3] = s3c2410_spigpio_txrx_mode3;
Ben Dooks1fc75472006-05-20 15:00:17 -0700128
Ben Dooks438ae1a2007-11-28 16:21:29 -0800129 /* set state of spi pins, always assume that the clock is
130 * available, but do check the MOSI and MISO. */
131 s3c2410_gpio_setpin(info->pin_clk, 0);
132 s3c2410_gpio_cfgpin(info->pin_clk, S3C2410_GPIO_OUTPUT);
Ben Dooks1fc75472006-05-20 15:00:17 -0700133
Ben Dooks438ae1a2007-11-28 16:21:29 -0800134 if (info->pin_mosi < S3C2410_GPH10) {
135 s3c2410_gpio_setpin(info->pin_mosi, 0);
136 s3c2410_gpio_cfgpin(info->pin_mosi, S3C2410_GPIO_OUTPUT);
137 }
138
139 if (info->pin_miso != S3C2410_GPA0 && info->pin_miso < S3C2410_GPH10)
140 s3c2410_gpio_cfgpin(info->pin_miso, S3C2410_GPIO_INPUT);
Ben Dooks1fc75472006-05-20 15:00:17 -0700141
142 ret = spi_bitbang_start(&sp->bitbang);
143 if (ret)
144 goto err_no_bitbang;
145
146 /* register the chips to go with the board */
147
148 for (i = 0; i < sp->info->board_size; i++) {
149 dev_info(&dev->dev, "registering %p: %s\n",
150 &sp->info->board_info[i],
151 sp->info->board_info[i].modalias);
152
153 sp->info->board_info[i].controller_data = sp;
154 spi_new_device(master, sp->info->board_info + i);
155 }
156
157 return 0;
158
159 err_no_bitbang:
160 spi_master_put(sp->bitbang.master);
161 err:
162 return ret;
163
164}
165
166static int s3c2410_spigpio_remove(struct platform_device *dev)
167{
168 struct s3c2410_spigpio *sp = platform_get_drvdata(dev);
169
170 spi_bitbang_stop(&sp->bitbang);
171 spi_master_put(sp->bitbang.master);
172
173 return 0;
174}
175
176/* all gpio should be held over suspend/resume, so we should
177 * not need to deal with this
178*/
179
180#define s3c2410_spigpio_suspend NULL
181#define s3c2410_spigpio_resume NULL
182
183
184static struct platform_driver s3c2410_spigpio_drv = {
185 .probe = s3c2410_spigpio_probe,
186 .remove = s3c2410_spigpio_remove,
187 .suspend = s3c2410_spigpio_suspend,
188 .resume = s3c2410_spigpio_resume,
189 .driver = {
David Brownellfc3ba952007-08-30 23:56:24 -0700190 .name = "spi_s3c24xx_gpio",
Ben Dooks1fc75472006-05-20 15:00:17 -0700191 .owner = THIS_MODULE,
192 },
193};
194
195static int __init s3c2410_spigpio_init(void)
196{
197 return platform_driver_register(&s3c2410_spigpio_drv);
198}
199
200static void __exit s3c2410_spigpio_exit(void)
201{
202 platform_driver_unregister(&s3c2410_spigpio_drv);
203}
204
205module_init(s3c2410_spigpio_init);
206module_exit(s3c2410_spigpio_exit);
207
208MODULE_DESCRIPTION("S3C24XX SPI Driver");
209MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
210MODULE_LICENSE("GPL");