8253202c098534aaff74f1844f847bb5fbe6ded5
[openwrt/openwrt.git] / target / linux / generic-2.6 / patches-2.6.25 / 922-gpiommc.patch
1 Index: linux-2.6.25.10/drivers/mmc/host/gpiommc.c
2 ===================================================================
3 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4 +++ linux-2.6.25.10/drivers/mmc/host/gpiommc.c 2008-07-18 22:31:00.000000000 +0200
5 @@ -0,0 +1,328 @@
6 +/*
7 + * Driver an MMC/SD card on a bitbanging GPIO SPI bus.
8 + * This module hooks up the mmc_spi and spi_gpio modules and also
9 + * provides a sysfs interface.
10 + *
11 + * Copyright 2008 Michael Buesch <mb@bu3sch.de>
12 + *
13 + * Licensed under the GNU/GPL. See COPYING for details.
14 + */
15 +
16 +#include <linux/mmc/gpiommc.h>
17 +#include <linux/platform_device.h>
18 +#include <linux/list.h>
19 +#include <linux/mutex.h>
20 +#include <linux/spi/spi_gpio.h>
21 +
22 +
23 +#define PFX "gpio-mmc: "
24 +#define GPIOMMC_MAX_NAMELEN_STR __stringify(GPIOMMC_MAX_NAMELEN)
25 +
26 +struct gpiommc_device {
27 + struct platform_device *pdev;
28 + struct platform_device *spi_pdev;
29 + struct spi_board_info boardinfo;
30 +};
31 +
32 +
33 +MODULE_DESCRIPTION("GPIO based MMC driver");
34 +MODULE_AUTHOR("Michael Buesch");
35 +MODULE_LICENSE("GPL");
36 +
37 +
38 +static int gpiommc_boardinfo_setup(struct spi_board_info *bi,
39 + struct spi_master *master,
40 + void *data)
41 +{
42 + struct gpiommc_device *d = data;
43 + struct gpiommc_platform_data *pdata = d->pdev->dev.platform_data;
44 +
45 + /* Bind the SPI master to the MMC-SPI host driver. */
46 + strlcpy(bi->modalias, "mmc_spi", sizeof(bi->modalias));
47 +
48 + bi->max_speed_hz = pdata->max_bus_speed;
49 + bi->bus_num = master->bus_num;
50 + bi->mode = pdata->mode;
51 +
52 + return 0;
53 +}
54 +
55 +static int gpiommc_probe(struct platform_device *pdev)
56 +{
57 + struct gpiommc_platform_data *mmc_pdata = pdev->dev.platform_data;
58 + struct spi_gpio_platform_data spi_pdata;
59 + struct gpiommc_device *d;
60 + int err;
61 +
62 + err = -ENXIO;
63 + if (!mmc_pdata)
64 + goto error;
65 +
66 + /* Allocate the GPIO-MMC device */
67 + err = -ENOMEM;
68 + d = kzalloc(sizeof(*d), GFP_KERNEL);
69 + if (!d)
70 + goto error;
71 + d->pdev = pdev;
72 +
73 + /* Create the SPI-GPIO device */
74 + d->spi_pdev = platform_device_alloc(SPI_GPIO_PLATDEV_NAME,
75 + spi_gpio_next_id());
76 + if (!d->spi_pdev)
77 + goto err_free_d;
78 +
79 + memset(&spi_pdata, 0, sizeof(spi_pdata));
80 + spi_pdata.pin_clk = mmc_pdata->pins.gpio_clk;
81 + spi_pdata.pin_miso = mmc_pdata->pins.gpio_do;
82 + spi_pdata.pin_mosi = mmc_pdata->pins.gpio_di;
83 + spi_pdata.pin_cs = mmc_pdata->pins.gpio_cs;
84 + spi_pdata.cs_activelow = mmc_pdata->pins.cs_activelow;
85 + spi_pdata.no_spi_delay = mmc_pdata->no_spi_delay;
86 + spi_pdata.boardinfo_setup = gpiommc_boardinfo_setup;
87 + spi_pdata.boardinfo_setup_data = d;
88 +
89 + err = platform_device_add_data(d->spi_pdev, &spi_pdata,
90 + sizeof(spi_pdata));
91 + if (err)
92 + goto err_free_pdev;
93 + err = platform_device_add(d->spi_pdev);
94 + if (err)
95 + goto err_free_pdata;
96 + platform_set_drvdata(pdev, d);
97 +
98 + printk(KERN_INFO PFX "MMC-Card \"%s\" "
99 + "attached to GPIO pins di=%u, do=%u, clk=%u, cs=%u\n",
100 + mmc_pdata->name, mmc_pdata->pins.gpio_di,
101 + mmc_pdata->pins.gpio_do,
102 + mmc_pdata->pins.gpio_clk,
103 + mmc_pdata->pins.gpio_cs);
104 +
105 + return 0;
106 +
107 +err_free_pdata:
108 + kfree(d->spi_pdev->dev.platform_data);
109 + d->spi_pdev->dev.platform_data = NULL;
110 +err_free_pdev:
111 + platform_device_put(d->spi_pdev);
112 +err_free_d:
113 + kfree(d);
114 +error:
115 + return err;
116 +}
117 +
118 +static int gpiommc_remove(struct platform_device *pdev)
119 +{
120 + struct gpiommc_device *d = platform_get_drvdata(pdev);
121 + struct gpiommc_platform_data *pdata = d->pdev->dev.platform_data;
122 +
123 + platform_device_unregister(d->spi_pdev);
124 + printk(KERN_INFO PFX "GPIO based MMC-Card \"%s\" removed\n", pdata->name);
125 + platform_device_put(d->spi_pdev);
126 +
127 + return 0;
128 +}
129 +
130 +/* Wrapper for the platform data with context data for the sysfs interface. */
131 +struct gpiommc_sysfs_platform_data {
132 + struct gpiommc_platform_data p; /* Keep as first element */
133 +
134 + /* The platform device that we allocated. */
135 + struct platform_device *pdev;
136 + /* gpiommc_sysfs_list */
137 + struct list_head list;
138 +};
139 +
140 +static LIST_HEAD(gpiommc_sysfs_list);
141 +static DEFINE_MUTEX(gpiommc_sysfs_mutex);
142 +
143 +static struct gpiommc_sysfs_platform_data *gpiommc_sysfs_find_dev(const char *name)
144 +{
145 + struct gpiommc_sysfs_platform_data *pdata;
146 +
147 + list_for_each_entry(pdata, &gpiommc_sysfs_list, list) {
148 + if (strcmp(pdata->p.name, name) == 0)
149 + return pdata;
150 + }
151 +
152 + return NULL;
153 +}
154 +
155 +static ssize_t gpiommc_add_store(struct device_driver *drv,
156 + const char *buf, size_t count)
157 +{
158 + int res, err;
159 + struct gpiommc_sysfs_platform_data pdata_local, *pdata;
160 + struct platform_device *pdev;
161 + unsigned int no_spi_delay = 0, mode = 0, csactivelow = 0;
162 +
163 + mutex_lock(&gpiommc_sysfs_mutex);
164 +
165 + pdata = &pdata_local;
166 + memset(pdata, 0, sizeof(*pdata));
167 +
168 + err = -EINVAL;
169 + res = sscanf(buf, "%" GPIOMMC_MAX_NAMELEN_STR "s %u %u %u %u %u %u %u %u",
170 + pdata->p.name,
171 + &pdata->p.pins.gpio_di,
172 + &pdata->p.pins.gpio_do,
173 + &pdata->p.pins.gpio_clk,
174 + &pdata->p.pins.gpio_cs,
175 + &mode,
176 + &pdata->p.max_bus_speed,
177 + &no_spi_delay,
178 + &csactivelow);
179 + pdata->p.mode = mode;
180 + pdata->p.no_spi_delay = !!no_spi_delay;
181 + pdata->p.pins.cs_activelow = !!csactivelow;
182 + if (res < 9)
183 + pdata->p.pins.cs_activelow = 1; /* Default: CS = activelow */
184 + if (res < 8)
185 + pdata->p.no_spi_delay = 0; /* Default: Delay turned on */
186 + if (res < 7)
187 + pdata->p.max_bus_speed = 5000000; /* Default: 5Mhz */
188 + if (res < 6)
189 + pdata->p.mode = 0; /* Default: SPI mode 0 */
190 + if (res < 5 || res > 9)
191 + goto out; /* First 5 args are mandatory. */
192 +
193 + /* Convert mode so that the SPI subsystem does understand it. */
194 + switch (pdata->p.mode) {
195 + case 0:
196 + pdata->p.mode = SPI_MODE_0;
197 + break;
198 + case 1:
199 + pdata->p.mode = SPI_MODE_1;
200 + break;
201 + case 2:
202 + pdata->p.mode = SPI_MODE_2;
203 + break;
204 + case 3:
205 + pdata->p.mode = SPI_MODE_3;
206 + break;
207 + default:
208 + goto out; /* Invalid mode */
209 + }
210 +
211 + err = -EEXIST;
212 + if (gpiommc_sysfs_find_dev(pdata->p.name))
213 + goto out;
214 +
215 + err = -ENOMEM;
216 + pdev = platform_device_alloc(GPIOMMC_PLATDEV_NAME, gpiommc_next_id());
217 + if (!pdev)
218 + goto out;
219 +
220 + err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
221 + if (err)
222 + goto err_free_pdev;
223 + pdata = pdev->dev.platform_data;
224 +
225 + err = platform_device_add(pdev);
226 + if (err)
227 + goto err_free_pdev;
228 +
229 + pdata->pdev = pdev;
230 + INIT_LIST_HEAD(&pdata->list);
231 + list_add(&pdata->list, &gpiommc_sysfs_list);
232 +
233 + err = 0;
234 +out:
235 + mutex_unlock(&gpiommc_sysfs_mutex);
236 +
237 + return err ? err : count;
238 +
239 +err_free_pdev:
240 + platform_device_put(pdev);
241 + goto out;
242 +}
243 +
244 +static ssize_t gpiommc_remove_store(struct device_driver *drv,
245 + const char *buf, size_t count)
246 +{
247 + struct gpiommc_sysfs_platform_data *pdata;
248 + int err;
249 +
250 + mutex_lock(&gpiommc_sysfs_mutex);
251 +
252 + err = -ENODEV;
253 + pdata = gpiommc_sysfs_find_dev(buf);
254 + if (!pdata)
255 + goto out;
256 +
257 + list_del(&pdata->list);
258 + platform_device_unregister(pdata->pdev);
259 +
260 +out:
261 + mutex_unlock(&gpiommc_sysfs_mutex);
262 +
263 + return err ? err : count;
264 +}
265 +
266 +static DRIVER_ATTR(add, 0200,
267 + NULL, gpiommc_add_store);
268 +static DRIVER_ATTR(remove, 0200,
269 + NULL, gpiommc_remove_store);
270 +
271 +static struct platform_driver gpiommc_plat_driver = {
272 + .probe = gpiommc_probe,
273 + .remove = gpiommc_remove,
274 + .driver = {
275 + .name = GPIOMMC_PLATDEV_NAME,
276 + .owner = THIS_MODULE,
277 + },
278 +};
279 +
280 +int gpiommc_next_id(void)
281 +{
282 + static atomic_t counter = ATOMIC_INIT(-1);
283 +
284 + return atomic_inc_return(&counter);
285 +}
286 +EXPORT_SYMBOL(gpiommc_next_id);
287 +
288 +static int __init gpiommc_modinit(void)
289 +{
290 + int err;
291 +
292 + err = platform_driver_register(&gpiommc_plat_driver);
293 + if (err)
294 + return err;
295 + err = driver_create_file(&gpiommc_plat_driver.driver,
296 + &driver_attr_add);
297 + if (err)
298 + goto err_drv_unreg;
299 + err = driver_create_file(&gpiommc_plat_driver.driver,
300 + &driver_attr_remove);
301 + if (err)
302 + goto err_remove_add;
303 +
304 + return 0;
305 +
306 +err_remove_add:
307 + driver_remove_file(&gpiommc_plat_driver.driver,
308 + &driver_attr_add);
309 +err_drv_unreg:
310 + platform_driver_unregister(&gpiommc_plat_driver);
311 + return err;
312 +}
313 +module_init(gpiommc_modinit);
314 +
315 +static void __exit gpiommc_modexit(void)
316 +{
317 + struct gpiommc_sysfs_platform_data *pdata, *pdata_tmp;
318 +
319 + driver_remove_file(&gpiommc_plat_driver.driver,
320 + &driver_attr_remove);
321 + driver_remove_file(&gpiommc_plat_driver.driver,
322 + &driver_attr_add);
323 +
324 + mutex_lock(&gpiommc_sysfs_mutex);
325 + list_for_each_entry_safe(pdata, pdata_tmp, &gpiommc_sysfs_list, list) {
326 + list_del(&pdata->list);
327 + platform_device_unregister(pdata->pdev);
328 + }
329 + mutex_unlock(&gpiommc_sysfs_mutex);
330 +
331 + platform_driver_unregister(&gpiommc_plat_driver);
332 +}
333 +module_exit(gpiommc_modexit);
334 Index: linux-2.6.25.10/drivers/mmc/host/Kconfig
335 ===================================================================
336 --- linux-2.6.25.10.orig/drivers/mmc/host/Kconfig 2008-07-18 22:30:36.000000000 +0200
337 +++ linux-2.6.25.10/drivers/mmc/host/Kconfig 2008-07-18 22:31:00.000000000 +0200
338 @@ -130,3 +130,23 @@ config MMC_SPI
339
340 If unsure, or if your system has no SPI master driver, say N.
341
342 +config GPIOMMC
343 + tristate "MMC/SD over GPIO-based SPI"
344 + depends on MMC && MMC_SPI && SPI_GPIO
345 + help
346 + This driver hooks up the mmc_spi and spi_gpio modules so that
347 + MMC/SD cards can be used on a GPIO based bus by bitbanging
348 + the SPI protocol in software.
349 +
350 + This driver provides a sysfs interface to dynamically create
351 + and destroy GPIO-based MMC/SD card interfaces. It also provides
352 + a platform device interface API.
353 + See Documentation/gpiommc.txt for details.
354 +
355 + The module will be called gpiommc.
356 +
357 + If unsure, say N.
358 +
359 +config MMC_S3C
360 + tristate "Samsung S3C SD/MMC Card Interface support"
361 + depends on ARCH_S3C2410 && MMC
362 Index: linux-2.6.25.10/drivers/mmc/host/Makefile
363 ===================================================================
364 --- linux-2.6.25.10.orig/drivers/mmc/host/Makefile 2008-07-18 22:30:36.000000000 +0200
365 +++ linux-2.6.25.10/drivers/mmc/host/Makefile 2008-07-18 22:31:20.000000000 +0200
366 @@ -18,3 +18,4 @@ obj-$(CONFIG_MMC_AT91) += at91_mci.o
367 obj-$(CONFIG_MMC_TIFM_SD) += tifm_sd.o
368 obj-$(CONFIG_MMC_SPI) += mmc_spi.o
369
370 +obj-$(CONFIG_GPIOMMC) += gpiommc.o
371 Index: linux-2.6.25.10/include/linux/mmc/gpiommc.h
372 ===================================================================
373 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
374 +++ linux-2.6.25.10/include/linux/mmc/gpiommc.h 2008-07-18 22:31:00.000000000 +0200
375 @@ -0,0 +1,62 @@
376 +/*
377 + * Device driver for MMC/SD cards driven over a GPIO bus.
378 + *
379 + * Copyright (c) 2008 Michael Buesch
380 + *
381 + * Licensed under the GNU/GPL version 2.
382 + */
383 +#ifndef LINUX_GPIOMMC_H_
384 +#define LINUX_GPIOMMC_H_
385 +
386 +#include <linux/types.h>
387 +
388 +
389 +#define GPIOMMC_MAX_NAMELEN 15
390 +
391 +/** struct gpiommc_pins - Hardware pin assignments
392 + * @gpio_di: The GPIO number of the DATA IN pin
393 + * @gpio_do: The GPIO number of the DATA OUT pin
394 + * @gpio_clk: The GPIO number of the CLOCK pin
395 + * @gpio_cs: The GPIO number of the CHIPSELECT pin
396 + * @cs_activelow: If true, the chip is considered selected if @gpio_cs is low.
397 + */
398 +struct gpiommc_pins {
399 + unsigned int gpio_di;
400 + unsigned int gpio_do;
401 + unsigned int gpio_clk;
402 + unsigned int gpio_cs;
403 + bool cs_activelow;
404 +};
405 +
406 +/** struct gpiommc_platform_data - Platform data for a MMC-over-SPI-GPIO device.
407 + * @name: The unique name string of the device.
408 + * @pins: The hardware pin assignments.
409 + * @mode: The hardware mode. This is either SPI_MODE_0,
410 + * SPI_MODE_1, SPI_MODE_2 or SPI_MODE_3. See the SPI documentation.
411 + * @no_spi_delay: Do not use delays in the lowlevel SPI bitbanging code.
412 + * This is not standards compliant, but may be required for some
413 + * embedded machines to gain reasonable speed.
414 + * @max_bus_speed: The maximum speed of the SPI bus, in Hertz.
415 + */
416 +struct gpiommc_platform_data {
417 + char name[GPIOMMC_MAX_NAMELEN + 1];
418 + struct gpiommc_pins pins;
419 + u8 mode;
420 + bool no_spi_delay;
421 + unsigned int max_bus_speed;
422 +};
423 +
424 +/** GPIOMMC_PLATDEV_NAME - The platform device name string.
425 + * The name string that has to be used for platform_device_alloc
426 + * when allocating a gpiommc device.
427 + */
428 +#define GPIOMMC_PLATDEV_NAME "gpiommc"
429 +
430 +/** gpiommc_next_id - Get another platform device ID number.
431 + * This returns the next platform device ID number that has to be used
432 + * for platform_device_alloc. The ID is opaque and should not be used for
433 + * anything else.
434 + */
435 +int gpiommc_next_id(void);
436 +
437 +#endif /* LINUX_GPIOMMC_H_ */
438 Index: linux-2.6.25.10/Documentation/gpiommc.txt
439 ===================================================================
440 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
441 +++ linux-2.6.25.10/Documentation/gpiommc.txt 2008-07-18 22:31:00.000000000 +0200
442 @@ -0,0 +1,96 @@
443 +GPIOMMC - Driver for an MMC/SD card on a bitbanging GPIO SPI bus
444 +================================================================
445 +
446 +The gpiommc module hooks up the mmc_spi and spi_gpio modules for running an
447 +MMC or SD card on GPIO pins.
448 +
449 +Two interfaces for registering a new MMC/SD card device are provided.
450 +A static platform-device based mechanism and a dynamic sysfs based interface.
451 +
452 +
453 +Registering devices via platform-device
454 +=======================================
455 +
456 +The platform-device interface is used for registering MMC/SD devices that are
457 +part of the hardware platform. This is most useful only for embedded machines
458 +with MMC/SD devices statically connected to the platform GPIO bus.
459 +
460 +The data structures are declared in <linux/mmc/gpiommc.h>
461 +
462 +To register a new device, define an instance of struct gpiommc_platform_data.
463 +This structure holds any information about how the device is hooked up to the
464 +GPIO pins and what hardware modes the device supports. See the docbook-style
465 +documentation in the header file for more information on the struct fields.
466 +
467 +Then allocate a new instance of a platform device by doing:
468 +
469 + pdev = platform_device_alloc(GPIOMMC_PLATDEV_NAME, gpiommc_next_id());
470 +
471 +This will allocate the platform device data structures and hook it up to the
472 +gpiommc driver.
473 +Then add the gpiommc_platform_data to the platform device.
474 +
475 + err = platform_device_add_data(pdev, pdata, sizeof(struct gpiommc_platform_data));
476 +
477 +You may free the local instance of struct gpiommc_platform_data now.
478 +Now simply register the platform device.
479 +
480 + err = platform_device_add(pdev);
481 +
482 +Done. The gpiommc probe routine should be called and you should see a dmesg
483 +message for the added device.
484 +
485 +
486 +Registering devices via sysfs
487 +=============================
488 +
489 +MMC/SD cards connected via GPIO often are a pretty dynamic thing. For example
490 +selfmade hacks for soldering an MMC/SD card to standard GPIO pins on embedded
491 +hardware are a common situation.
492 +So we provide a dynamic interface to conveniently handle adding and removing
493 +devices from userspace, without the need to recompile the kernel.
494 +
495 +There are two sysfs files responsible for that:
496 +export ADD=/sys/bus/platform/drivers/gpiommc/add
497 +export REMOVE=/sys/bus/platform/drivers/gpiommc/remove
498 +
499 +To add a new device, simply echo the configuration string to the "add" file.
500 +The config string is composed out of the following elements:
501 +
502 +DEVNAME DIpin DOpin CLKpin CSpin SPIMODE MAXBUSSPEED NO_SPI_DELAY CSACTIVELOW
503 +
504 +DEVNAME is a unique name string for the device.
505 +DIpin is the SPI DI GPIO pin.
506 +DOpin is the SPI DO GPIO pin.
507 +CLKpin is the SPI CLOCK GPIO pin.
508 +CSpin is the SPI CHIPSELECT GPIO pin.
509 +SPIMODE is the hardware mode the device will run at. Can be 0-3.
510 +MAXBUSSPEED is the maximum bus speed in Hertz.
511 +NO_SPI_DELAY can be 1 or 0. If it is 1, then the lowlevel SPI delay
512 +will not be performed. This is not standards compliant, but may be required
513 +to gain reasonable speeds on embedded hardware.
514 +CSACTIVELOW can be 1 or 0. If it is 1, the chip is considered to be selected, if CS
515 +is at a logical 0.
516 +
517 +Note that the elements SPIMODE, MAXBUSSPEED and NO_SPI_DELAY are optional
518 +and can be omitted.
519 +SPIMODE will default to 0.
520 +MAXBUSSSPEED will default to 5Mhz.
521 +NO_SPI_DELAY will default to 0.
522 +CSACTIVELOW will default to 1.
523 +
524 +Example:
525 +
526 + echo -n "my_device 5 4 3 7 0 1000000 1" > $ADD
527 +
528 +This will add a new device called "my_device" with the GPIO pins assigned as
529 +DI=5, DO=4, CLK=3, CS=7
530 +The hardware mode will be SPI_MODE_0.
531 +The maximum bus speed will be 1000000 Hz (1Mhz)
532 +And the explicit SPI delay at the lowlevel bitbang loop will be switched off.
533 +
534 +To remove a device, simply echo the device name string to the "remove" file.
535 +
536 +Example:
537 +
538 + echo -n "my_device" > $REMOVE
539 Index: linux-2.6.25.10/MAINTAINERS
540 ===================================================================
541 --- linux-2.6.25.10.orig/MAINTAINERS 2008-07-18 22:30:41.000000000 +0200
542 +++ linux-2.6.25.10/MAINTAINERS 2008-07-18 22:31:00.000000000 +0200
543 @@ -1736,6 +1736,11 @@ L: gigaset307x-common@lists.sourceforge.
544 W: http://gigaset307x.sourceforge.net/
545 S: Maintained
546
547 +GPIOMMC DRIVER
548 +P: Michael Buesch
549 +M: mb@bu3sch.de
550 +S: Maintained
551 +
552 HARDWARE MONITORING
553 P: Mark M. Hoffman
554 M: mhoffman@lightlink.com