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/platform_device.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/spi_bitbang.h>
23 #include <asm/arch/regs-gpio.h>
24 #include <asm/arch/spi-gpio.h>
25 #include <asm/arch/hardware.h>
27 struct s3c2410_spigpio
{
28 struct spi_bitbang bitbang
;
30 struct s3c2410_spigpio_info
*info
;
31 struct platform_device
*dev
;
34 static inline struct s3c2410_spigpio
*spidev_to_sg(struct spi_device
*spi
)
36 return spi
->controller_data
;
39 static inline void setsck(struct spi_device
*dev
, int on
)
41 struct s3c2410_spigpio
*sg
= spidev_to_sg(dev
);
42 s3c2410_gpio_setpin(sg
->info
->pin_clk
, on
? 1 : 0);
45 static inline void setmosi(struct spi_device
*dev
, int on
)
47 struct s3c2410_spigpio
*sg
= spidev_to_sg(dev
);
48 s3c2410_gpio_setpin(sg
->info
->pin_mosi
, on
? 1 : 0);
51 static inline u32
getmiso(struct spi_device
*dev
)
53 struct s3c2410_spigpio
*sg
= spidev_to_sg(dev
);
54 return s3c2410_gpio_getpin(sg
->info
->pin_miso
) ? 1 : 0;
57 #define spidelay(x) ndelay(x)
59 #define EXPAND_BITBANG_TXRX
60 #include <linux/spi/spi_bitbang.h>
63 static u32
s3c2410_spigpio_txrx_mode0(struct spi_device
*spi
,
64 unsigned nsecs
, u32 word
, u8 bits
)
66 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, word
, bits
);
69 static u32
s3c2410_spigpio_txrx_mode1(struct spi_device
*spi
,
70 unsigned nsecs
, u32 word
, u8 bits
)
72 return bitbang_txrx_be_cpha1(spi
, nsecs
, 0, word
, bits
);
75 static void s3c2410_spigpio_chipselect(struct spi_device
*dev
, int value
)
77 struct s3c2410_spigpio
*sg
= spidev_to_sg(dev
);
79 if (sg
->info
&& sg
->info
->chip_select
)
80 (sg
->info
->chip_select
)(sg
->info
, value
);
83 static int s3c2410_spigpio_probe(struct platform_device
*dev
)
85 struct spi_master
*master
;
86 struct s3c2410_spigpio
*sp
;
90 master
= spi_alloc_master(&dev
->dev
, sizeof(struct s3c2410_spigpio
));
92 dev_err(&dev
->dev
, "failed to allocate spi master\n");
97 sp
= spi_master_get_devdata(master
);
99 platform_set_drvdata(dev
, sp
);
101 /* copy in the plkatform data */
102 sp
->info
= dev
->dev
.platform_data
;
104 /* setup spi bitbang adaptor */
105 sp
->bitbang
.master
= spi_master_get(master
);
106 sp
->bitbang
.chipselect
= s3c2410_spigpio_chipselect
;
108 sp
->bitbang
.txrx_word
[SPI_MODE_0
] = s3c2410_spigpio_txrx_mode0
;
109 sp
->bitbang
.txrx_word
[SPI_MODE_1
] = s3c2410_spigpio_txrx_mode1
;
111 /* set state of spi pins */
112 s3c2410_gpio_setpin(sp
->info
->pin_clk
, 0);
113 s3c2410_gpio_setpin(sp
->info
->pin_mosi
, 0);
115 s3c2410_gpio_cfgpin(sp
->info
->pin_clk
, S3C2410_GPIO_OUTPUT
);
116 s3c2410_gpio_cfgpin(sp
->info
->pin_mosi
, S3C2410_GPIO_OUTPUT
);
117 s3c2410_gpio_cfgpin(sp
->info
->pin_miso
, S3C2410_GPIO_INPUT
);
119 ret
= spi_bitbang_start(&sp
->bitbang
);
123 /* register the chips to go with the board */
125 for (i
= 0; i
< sp
->info
->board_size
; i
++) {
126 dev_info(&dev
->dev
, "registering %p: %s\n",
127 &sp
->info
->board_info
[i
],
128 sp
->info
->board_info
[i
].modalias
);
130 sp
->info
->board_info
[i
].controller_data
= sp
;
131 spi_new_device(master
, sp
->info
->board_info
+ i
);
137 spi_master_put(sp
->bitbang
.master
);
143 static int s3c2410_spigpio_remove(struct platform_device
*dev
)
145 struct s3c2410_spigpio
*sp
= platform_get_drvdata(dev
);
147 spi_bitbang_stop(&sp
->bitbang
);
148 spi_master_put(sp
->bitbang
.master
);
153 /* all gpio should be held over suspend/resume, so we should
154 * not need to deal with this
157 #define s3c2410_spigpio_suspend NULL
158 #define s3c2410_spigpio_resume NULL
161 static struct platform_driver s3c2410_spigpio_drv
= {
162 .probe
= s3c2410_spigpio_probe
,
163 .remove
= s3c2410_spigpio_remove
,
164 .suspend
= s3c2410_spigpio_suspend
,
165 .resume
= s3c2410_spigpio_resume
,
167 .name
= "s3c24xx-spi-gpio",
168 .owner
= THIS_MODULE
,
172 static int __init
s3c2410_spigpio_init(void)
174 return platform_driver_register(&s3c2410_spigpio_drv
);
177 static void __exit
s3c2410_spigpio_exit(void)
179 platform_driver_unregister(&s3c2410_spigpio_drv
);
182 module_init(s3c2410_spigpio_init
);
183 module_exit(s3c2410_spigpio_exit
);
185 MODULE_DESCRIPTION("S3C24XX SPI Driver");
186 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
187 MODULE_LICENSE("GPL");