adds rt_tables to files installed by ip ipkg
[openwrt.git] / package / mmc_over_gpio / src / mmc_over_spigpio.c
1 /*
2 * Driver for driving an MMC card over a bitbanging GPIO SPI bus.
3 *
4 * Copyright 2008 Michael Buesch <mb@bu3sch.de>
5 *
6 * Licensed under the GNU/GPL. See COPYING for details.
7 */
8
9 #include <linux/platform_device.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/spi/spi_gpio.h>
13
14
15 /* This is the maximum speed in Hz */
16 #define GPIOMMC_MAXSPEED 5000000 /* Hz */
17
18
19 #define DRIVER_NAME "spi-gpio-mmc"
20 #define PFX DRIVER_NAME ": "
21
22
23 #define GPIOMMC_MAX_NAMELEN 15
24 #define GPIOMMC_MAX_NAMELEN_STR __stringify(GPIOMMC_MAX_NAMELEN)
25
26 struct gpiommc_pins {
27 unsigned int gpio_di; /* Card DI pin */
28 unsigned int gpio_do; /* Card DO pin */
29 unsigned int gpio_clk; /* Card CLK pin */
30 unsigned int gpio_cs; /* Card CS pin */
31 };
32
33 struct gpiommc_device {
34 char name[GPIOMMC_MAX_NAMELEN + 1];
35 struct platform_device *pdev;
36 struct platform_device *spi_pdev;
37 struct gpiommc_pins pins;
38 u8 mode; /* SPI_MODE_X */
39 struct spi_board_info boardinfo;
40
41 struct list_head list;
42 };
43
44
45 static LIST_HEAD(gpiommc_devices_list);
46 static DEFINE_MUTEX(gpiommc_mutex);
47
48
49 MODULE_DESCRIPTION("SPI-GPIO based MMC driver");
50 MODULE_AUTHOR("Michael Buesch");
51 MODULE_LICENSE("GPL");
52
53
54 static int gpiommc_boardinfo_setup(struct spi_board_info *bi,
55 struct spi_master *master,
56 void *data)
57 {
58 struct gpiommc_device *d = data;
59
60 /* Bind the SPI master to the MMC-SPI host driver. */
61 strlcpy(bi->modalias, "mmc_spi", sizeof(bi->modalias));
62
63 bi->max_speed_hz = GPIOMMC_MAXSPEED;
64 bi->bus_num = master->bus_num;
65 bi->mode = d->mode;
66
67 return 0;
68 }
69
70 static int gpiommc_probe(struct platform_device *pdev)
71 {
72 static int instance;
73 struct gpiommc_device *d = platform_get_drvdata(pdev);
74 struct spi_gpio_platform_data pdata;
75 int err = -ENOMEM;
76
77 d->spi_pdev = platform_device_alloc("spi-gpio", instance++);
78 if (!d->spi_pdev)
79 goto out;
80
81 memset(&pdata, 0, sizeof(pdata));
82 pdata.pin_clk = d->pins.gpio_clk;
83 pdata.pin_miso = d->pins.gpio_do;
84 pdata.pin_mosi = d->pins.gpio_di;
85 pdata.pin_cs = d->pins.gpio_cs;
86 pdata.cs_activelow = 1;
87 pdata.no_spi_delay = 1;
88 pdata.boardinfo_setup = gpiommc_boardinfo_setup;
89 pdata.boardinfo_setup_data = d;
90
91 err = platform_device_add_data(d->spi_pdev, &pdata, sizeof(pdata));
92 if (err)
93 goto err_free_pdev;
94 err = platform_device_register(d->spi_pdev);
95 if (err)
96 goto err_free_pdata;
97
98 printk(KERN_INFO PFX "MMC-Card \"%s\" "
99 "attached to GPIO pins %u,%u,%u,%u\n",
100 d->name, d->pins.gpio_di, d->pins.gpio_do,
101 d->pins.gpio_clk, d->pins.gpio_cs);
102 out:
103 return err;
104
105 err_free_pdata:
106 kfree(d->spi_pdev->dev.platform_data);
107 d->spi_pdev->dev.platform_data = NULL;
108 err_free_pdev:
109 platform_device_put(d->spi_pdev);
110 return err;
111 }
112
113 static int gpiommc_remove(struct platform_device *pdev)
114 {
115 struct gpiommc_device *d = platform_get_drvdata(pdev);
116
117 platform_device_unregister(d->spi_pdev);
118 printk(KERN_INFO PFX "MMC-Card \"%s\" removed\n", d->name);
119
120 return 0;
121 }
122
123 static void gpiommc_free(struct gpiommc_device *d)
124 {
125 kfree(d);
126 }
127
128 static struct gpiommc_device * gpiommc_alloc(struct platform_device *pdev,
129 const char *name,
130 const struct gpiommc_pins *pins,
131 u8 mode)
132 {
133 struct gpiommc_device *d;
134
135 d = kmalloc(sizeof(*d), GFP_KERNEL);
136 if (!d)
137 return NULL;
138
139 strcpy(d->name, name);
140 memcpy(&d->pins, pins, sizeof(d->pins));
141 d->mode = mode;
142 INIT_LIST_HEAD(&d->list);
143
144 return d;
145 }
146
147 /* List must be locked. */
148 static struct gpiommc_device * gpiommc_find_device(const char *name)
149 {
150 struct gpiommc_device *d;
151
152 list_for_each_entry(d, &gpiommc_devices_list, list) {
153 if (strcmp(d->name, name) == 0)
154 return d;
155 }
156
157 return NULL;
158 }
159
160 static void gpiommc_do_destroy_device(struct gpiommc_device *d)
161 {
162 list_del(&d->list);
163 platform_device_unregister(d->pdev);
164 gpiommc_free(d);
165 }
166
167 static int gpiommc_destroy_device(const char *name)
168 {
169 struct gpiommc_device *d;
170 int err = -ENODEV;
171
172 mutex_lock(&gpiommc_mutex);
173 d = gpiommc_find_device(name);
174 if (!d)
175 goto out_unlock;
176 gpiommc_do_destroy_device(d);
177 err = 0;
178 out_unlock:
179 mutex_unlock(&gpiommc_mutex);
180
181 return err;
182 }
183
184 static int gpiommc_create_device(const char *name,
185 const struct gpiommc_pins *pins,
186 u8 mode)
187 {
188 static int instance;
189 struct platform_device *pdev;
190 struct gpiommc_device *d;
191 int err;
192
193 mutex_lock(&gpiommc_mutex);
194 err = -EEXIST;
195 if (gpiommc_find_device(name))
196 goto out_unlock;
197 err = -ENOMEM;
198 pdev = platform_device_alloc(DRIVER_NAME, instance++);
199 if (!pdev)
200 goto out_unlock;
201 d = gpiommc_alloc(pdev, name, pins, mode);
202 if (!d)
203 goto err_free_pdev;
204 platform_set_drvdata(pdev, d);
205 d->pdev = pdev;
206 err = platform_device_register(pdev);
207 if (err)
208 goto err_free_mdev;
209 list_add(&d->list, &gpiommc_devices_list);
210
211 err = 0;
212 out_unlock:
213 mutex_unlock(&gpiommc_mutex);
214
215 return err;
216
217 err_free_mdev:
218 gpiommc_free(d);
219 err_free_pdev:
220 platform_device_put(pdev);
221 goto out_unlock;
222 }
223
224 static ssize_t gpiommc_add_show(struct device_driver *drv,
225 char *buf)
226 {
227 return snprintf(buf, PAGE_SIZE, "NAME DI_pin,DO_pin,CLK_pin,CS_pin [MODE]\n");
228 }
229
230 static ssize_t gpiommc_add_store(struct device_driver *drv,
231 const char *buf, size_t count)
232 {
233 int res, err;
234 char name[GPIOMMC_MAX_NAMELEN + 1];
235 struct gpiommc_pins pins;
236 unsigned int mode;
237
238 res = sscanf(buf, "%" GPIOMMC_MAX_NAMELEN_STR "s %u,%u,%u,%u %u",
239 name, &pins.gpio_di, &pins.gpio_do,
240 &pins.gpio_clk, &pins.gpio_cs, &mode);
241 if (res == 5)
242 mode = 0;
243 else if (res != 6)
244 return -EINVAL;
245 switch (mode) {
246 case 0:
247 mode = SPI_MODE_0;
248 break;
249 case 1:
250 mode = SPI_MODE_1;
251 break;
252 case 2:
253 mode = SPI_MODE_2;
254 break;
255 case 3:
256 mode = SPI_MODE_3;
257 break;
258 default:
259 return -EINVAL;
260 }
261 err = gpiommc_create_device(name, &pins, mode);
262
263 return err ? err : count;
264 }
265
266 static ssize_t gpiommc_remove_show(struct device_driver *drv,
267 char *buf)
268 {
269 return snprintf(buf, PAGE_SIZE, "write device-name to remove the device\n");
270 }
271
272 static ssize_t gpiommc_remove_store(struct device_driver *drv,
273 const char *buf, size_t count)
274 {
275 int err;
276
277 err = gpiommc_destroy_device(buf);
278
279 return err ? err : count;
280 }
281
282 static DRIVER_ATTR(add, 0600,
283 gpiommc_add_show, gpiommc_add_store);
284 static DRIVER_ATTR(remove, 0600,
285 gpiommc_remove_show, gpiommc_remove_store);
286
287 static struct platform_driver gpiommc_plat_driver = {
288 .probe = gpiommc_probe,
289 .remove = gpiommc_remove,
290 .driver = {
291 .name = DRIVER_NAME,
292 .owner = THIS_MODULE,
293 },
294 };
295
296 static int __init gpiommc_modinit(void)
297 {
298 int err;
299
300 err = platform_driver_register(&gpiommc_plat_driver);
301 if (err)
302 return err;
303 err = driver_create_file(&gpiommc_plat_driver.driver,
304 &driver_attr_add);
305 if (err)
306 goto err_drv_unreg;
307 err = driver_create_file(&gpiommc_plat_driver.driver,
308 &driver_attr_remove);
309 if (err)
310 goto err_remove_add;
311
312 return 0;
313
314 err_remove_add:
315 driver_remove_file(&gpiommc_plat_driver.driver,
316 &driver_attr_add);
317 err_drv_unreg:
318 platform_driver_unregister(&gpiommc_plat_driver);
319 return err;
320 }
321 module_init(gpiommc_modinit);
322
323 static void __exit gpiommc_modexit(void)
324 {
325 struct gpiommc_device *d, *tmp;
326
327 driver_remove_file(&gpiommc_plat_driver.driver,
328 &driver_attr_remove);
329 driver_remove_file(&gpiommc_plat_driver.driver,
330 &driver_attr_add);
331
332 mutex_lock(&gpiommc_mutex);
333 list_for_each_entry_safe(d, tmp, &gpiommc_devices_list, list)
334 gpiommc_do_destroy_device(d);
335 mutex_unlock(&gpiommc_mutex);
336
337 platform_driver_unregister(&gpiommc_plat_driver);
338 }
339 module_exit(gpiommc_modexit);
This page took 0.057704 seconds and 5 git commands to generate.