fix the route to the pptp server when using pptp as wan interface
[openwrt.git] / package / mmc_over_gpio / src / spi_gpio.c
1 /*
2 * Bitbanging SPI bus driver using GPIO API
3 *
4 * Copyright (c) 2008 Piotr Skamruk
5 * Copyright (c) 2008 Michael Buesch
6 *
7 * based on spi_s3c2410_gpio.c
8 * Copyright (c) 2006 Ben Dooks
9 * Copyright (c) 2006 Simtec Electronics
10 * and on i2c-gpio.c
11 * Copyright (C) 2007 Atmel Corporation
12 *
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.
16 */
17
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
28 #include <asm/gpio.h>
29
30
31 struct spi_gpio {
32 struct spi_bitbang bitbang;
33 struct spi_gpio_platform_data *info;
34 struct platform_device *pdev;
35 struct spi_board_info bi;
36 };
37
38
39 static inline struct spi_gpio *spidev_to_sg(struct spi_device *dev)
40 {
41 return dev->controller_data;
42 }
43
44 static inline void setsck(struct spi_device *dev, int val)
45 {
46 struct spi_gpio *sp = spidev_to_sg(dev);
47 gpio_set_value(sp->info->pin_clk, val ? 1 : 0);
48 }
49
50 static inline void setmosi(struct spi_device *dev, int val )
51 {
52 struct spi_gpio *sp = spidev_to_sg(dev);
53 gpio_set_value(sp->info->pin_mosi, val ? 1 : 0);
54 }
55
56 static inline u32 getmiso(struct spi_device *dev)
57 {
58 struct spi_gpio *sp = spidev_to_sg(dev);
59 return gpio_get_value(sp->info->pin_miso) ? 1 : 0;
60 }
61
62 static inline void do_spidelay(struct spi_device *dev, unsigned nsecs)
63 {
64 struct spi_gpio *sp = spidev_to_sg(dev);
65
66 if (!sp->info->no_spi_delay)
67 ndelay(nsecs);
68 }
69
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); \
74 } while (0)
75
76 #define EXPAND_BITBANG_TXRX
77 #include <linux/spi/spi_bitbang.h>
78
79 static u32 spi_gpio_txrx_mode0(struct spi_device *spi,
80 unsigned nsecs, u32 word, u8 bits)
81 {
82 return bitbang_txrx_be_cpha0(spi, nsecs, 0, word, bits);
83 }
84
85 static u32 spi_gpio_txrx_mode1(struct spi_device *spi,
86 unsigned nsecs, u32 word, u8 bits)
87 {
88 return bitbang_txrx_be_cpha1(spi, nsecs, 0, word, bits);
89 }
90
91 static u32 spi_gpio_txrx_mode2(struct spi_device *spi,
92 unsigned nsecs, u32 word, u8 bits)
93 {
94 return bitbang_txrx_be_cpha0(spi, nsecs, 1, word, bits);
95 }
96
97 static u32 spi_gpio_txrx_mode3(struct spi_device *spi,
98 unsigned nsecs, u32 word, u8 bits)
99 {
100 return bitbang_txrx_be_cpha1(spi, nsecs, 1, word, bits);
101 }
102
103 static void spi_gpio_chipselect(struct spi_device *dev, int on)
104 {
105 struct spi_gpio *sp = spidev_to_sg(dev);
106
107 if (sp->info->cs_activelow)
108 on = !on;
109 gpio_set_value(sp->info->pin_cs, on ? 1 : 0);
110 }
111
112 static int spi_gpio_probe(struct platform_device *pdev)
113 {
114 struct spi_master *master;
115 struct spi_gpio_platform_data *pdata;
116 struct spi_gpio *sp;
117 struct spi_device *spidev;
118 int err;
119
120 pdata = pdev->dev.platform_data;
121 if (!pdata)
122 return -ENXIO;
123
124 err = -ENOMEM;
125 master = spi_alloc_master(&pdev->dev, sizeof(struct spi_gpio));
126 if (!master)
127 goto err_alloc_master;
128
129 sp = spi_master_get_devdata(master);
130 platform_set_drvdata(pdev, sp);
131 sp->info = pdata;
132
133 err = gpio_request(pdata->pin_clk, "spi_clock");
134 if (err)
135 goto err_request_clk;
136 err = gpio_request(pdata->pin_mosi, "spi_mosi");
137 if (err)
138 goto err_request_mosi;
139 err = gpio_request(pdata->pin_miso, "spi_miso");
140 if (err)
141 goto err_request_miso;
142 err = gpio_request(pdata->pin_cs, "spi_cs");
143 if (err)
144 goto err_request_cs;
145
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;
154
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);
160
161 err = spi_bitbang_start(&sp->bitbang);
162 if (err)
163 goto err_no_bitbang;
164 err = pdata->boardinfo_setup(&sp->bi, master,
165 pdata->boardinfo_setup_data);
166 if (err)
167 goto err_bi_setup;
168 sp->bi.controller_data = sp;
169 spidev = spi_new_device(master, &sp->bi);
170 if (!spidev)
171 goto err_new_dev;
172
173 return 0;
174
175 err_new_dev:
176 err_bi_setup:
177 spi_bitbang_stop(&sp->bitbang);
178 err_no_bitbang:
179 spi_master_put(sp->bitbang.master);
180 gpio_free(pdata->pin_cs);
181 err_request_cs:
182 gpio_free(pdata->pin_miso);
183 err_request_miso:
184 gpio_free(pdata->pin_mosi);
185 err_request_mosi:
186 gpio_free(pdata->pin_clk);
187 err_request_clk:
188 kfree(master);
189
190 err_alloc_master:
191 return err;
192 }
193
194 static int __devexit spi_gpio_remove(struct platform_device *pdev)
195 {
196 struct spi_gpio *sp;
197 struct spi_gpio_platform_data *pdata;
198
199 pdata = pdev->dev.platform_data;
200 sp = platform_get_drvdata(pdev);
201
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);
208
209 return 0;
210 }
211
212 static struct platform_driver spi_gpio_driver = {
213 .driver = {
214 .name = "spi-gpio",
215 .owner = THIS_MODULE,
216 },
217 .probe = spi_gpio_probe,
218 .remove = __devexit_p(spi_gpio_remove),
219 };
220
221 static int __init spi_gpio_init(void)
222 {
223 int err;
224
225 err = platform_driver_register(&spi_gpio_driver);
226 if (err)
227 printk(KERN_ERR "spi-gpio: register failed: %d\n", err);
228
229 return err;
230 }
231 module_init(spi_gpio_init);
232
233 static void __exit spi_gpio_exit(void)
234 {
235 platform_driver_unregister(&spi_gpio_driver);
236 }
237 module_exit(spi_gpio_exit);
238
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");
This page took 0.071874 seconds and 5 git commands to generate.