2 * Bitbanging SPI bus driver using GPIO API
4 * Copyright (c) 2008 Piotr Skamruk
5 * Copyright (c) 2008 Michael Buesch
7 * based on spi_s3c2410_gpio.c
8 * Copyright (c) 2006 Ben Dooks
9 * Copyright (c) 2006 Simtec Electronics
11 * Copyright (C) 2007 Atmel Corporation
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/spinlock.h>
22 #include <linux/workqueue.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/spi/spi.h>
26 #include <linux/spi/spi_bitbang.h>
27 #include "linux/spi/spi_gpio.h" //XXX
32 struct spi_bitbang bitbang
;
33 struct spi_gpio_platform_data
*info
;
34 struct platform_device
*pdev
;
35 struct spi_board_info bi
;
39 static inline struct spi_gpio
*spidev_to_sg(struct spi_device
*dev
)
41 return dev
->controller_data
;
44 static inline void setsck(struct spi_device
*dev
, int val
)
46 struct spi_gpio
*sp
= spidev_to_sg(dev
);
47 gpio_set_value(sp
->info
->pin_clk
, val
? 1 : 0);
50 static inline void setmosi(struct spi_device
*dev
, int val
)
52 struct spi_gpio
*sp
= spidev_to_sg(dev
);
53 gpio_set_value(sp
->info
->pin_mosi
, val
? 1 : 0);
56 static inline u32
getmiso(struct spi_device
*dev
)
58 struct spi_gpio
*sp
= spidev_to_sg(dev
);
59 return gpio_get_value(sp
->info
->pin_miso
) ? 1 : 0;
62 static inline void do_spidelay(struct spi_device
*dev
, unsigned nsecs
)
64 struct spi_gpio
*sp
= spidev_to_sg(dev
);
66 if (!sp
->info
->no_spi_delay
)
70 #define spidelay(nsecs) do { \
71 /* Steal the spi_device pointer from our caller. \
72 * The bitbang-API should probably get fixed here... */ \
73 do_spidelay(spi, nsecs); \
76 #define EXPAND_BITBANG_TXRX
77 #include <linux/spi/spi_bitbang.h>
79 static u32
spi_gpio_txrx_mode0(struct spi_device
*spi
,
80 unsigned nsecs
, u32 word
, u8 bits
)
82 return bitbang_txrx_be_cpha0(spi
, nsecs
, 0, word
, bits
);
85 static u32
spi_gpio_txrx_mode1(struct spi_device
*spi
,
86 unsigned nsecs
, u32 word
, u8 bits
)
88 return bitbang_txrx_be_cpha1(spi
, nsecs
, 0, word
, bits
);
91 static u32
spi_gpio_txrx_mode2(struct spi_device
*spi
,
92 unsigned nsecs
, u32 word
, u8 bits
)
94 return bitbang_txrx_be_cpha0(spi
, nsecs
, 1, word
, bits
);
97 static u32
spi_gpio_txrx_mode3(struct spi_device
*spi
,
98 unsigned nsecs
, u32 word
, u8 bits
)
100 return bitbang_txrx_be_cpha1(spi
, nsecs
, 1, word
, bits
);
103 static void spi_gpio_chipselect(struct spi_device
*dev
, int on
)
105 struct spi_gpio
*sp
= spidev_to_sg(dev
);
107 if (sp
->info
->cs_activelow
)
109 gpio_set_value(sp
->info
->pin_cs
, on
? 1 : 0);
112 static int spi_gpio_probe(struct platform_device
*pdev
)
114 struct spi_master
*master
;
115 struct spi_gpio_platform_data
*pdata
;
117 struct spi_device
*spidev
;
120 pdata
= pdev
->dev
.platform_data
;
125 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct spi_gpio
));
127 goto err_alloc_master
;
129 sp
= spi_master_get_devdata(master
);
130 platform_set_drvdata(pdev
, sp
);
133 err
= gpio_request(pdata
->pin_clk
, "spi_clock");
135 goto err_request_clk
;
136 err
= gpio_request(pdata
->pin_mosi
, "spi_mosi");
138 goto err_request_mosi
;
139 err
= gpio_request(pdata
->pin_miso
, "spi_miso");
141 goto err_request_miso
;
142 err
= gpio_request(pdata
->pin_cs
, "spi_cs");
146 sp
->bitbang
.master
= spi_master_get(master
);
147 sp
->bitbang
.master
->bus_num
= -1;
148 sp
->bitbang
.master
->num_chipselect
= 1;
149 sp
->bitbang
.chipselect
= spi_gpio_chipselect
;
150 sp
->bitbang
.txrx_word
[SPI_MODE_0
] = spi_gpio_txrx_mode0
;
151 sp
->bitbang
.txrx_word
[SPI_MODE_1
] = spi_gpio_txrx_mode1
;
152 sp
->bitbang
.txrx_word
[SPI_MODE_2
] = spi_gpio_txrx_mode2
;
153 sp
->bitbang
.txrx_word
[SPI_MODE_3
] = spi_gpio_txrx_mode3
;
155 gpio_direction_output(pdata
->pin_clk
, 0);
156 gpio_direction_output(pdata
->pin_mosi
, 0);
157 gpio_direction_output(pdata
->pin_cs
,
158 pdata
->cs_activelow
? 1 : 0);
159 gpio_direction_input(pdata
->pin_miso
);
161 err
= spi_bitbang_start(&sp
->bitbang
);
164 err
= pdata
->boardinfo_setup(&sp
->bi
, master
,
165 pdata
->boardinfo_setup_data
);
168 sp
->bi
.controller_data
= sp
;
169 spidev
= spi_new_device(master
, &sp
->bi
);
177 spi_bitbang_stop(&sp
->bitbang
);
179 spi_master_put(sp
->bitbang
.master
);
180 gpio_free(pdata
->pin_cs
);
182 gpio_free(pdata
->pin_miso
);
184 gpio_free(pdata
->pin_mosi
);
186 gpio_free(pdata
->pin_clk
);
194 static int __devexit
spi_gpio_remove(struct platform_device
*pdev
)
197 struct spi_gpio_platform_data
*pdata
;
199 pdata
= pdev
->dev
.platform_data
;
200 sp
= platform_get_drvdata(pdev
);
202 gpio_free(pdata
->pin_clk
);
203 gpio_free(pdata
->pin_mosi
);
204 gpio_free(pdata
->pin_miso
);
205 gpio_free(pdata
->pin_cs
);
206 spi_bitbang_stop(&sp
->bitbang
);
207 spi_master_put(sp
->bitbang
.master
);
212 static struct platform_driver spi_gpio_driver
= {
215 .owner
= THIS_MODULE
,
217 .probe
= spi_gpio_probe
,
218 .remove
= __devexit_p(spi_gpio_remove
),
221 static int __init
spi_gpio_init(void)
225 err
= platform_driver_register(&spi_gpio_driver
);
227 printk(KERN_ERR
"spi-gpio: register failed: %d\n", err
);
231 module_init(spi_gpio_init
);
233 static void __exit
spi_gpio_exit(void)
235 platform_driver_unregister(&spi_gpio_driver
);
237 module_exit(spi_gpio_exit
);
239 MODULE_AUTHOR("Piot Skamruk <piotr.skamruk at gmail.com>");
240 MODULE_AUTHOR("Michael Buesch");
241 MODULE_DESCRIPTION("Platform independent GPIO bitbangling SPI driver");
242 MODULE_LICENSE("GPL v2");