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
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.
11 + * Copyright 2008 Michael Buesch <mb@bu3sch.de>
13 + * Licensed under the GNU/GPL. See COPYING for details.
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>
23 +#define PFX "gpio-mmc: "
24 +#define GPIOMMC_MAX_NAMELEN_STR __stringify(GPIOMMC_MAX_NAMELEN)
26 +struct gpiommc_device {
27 + struct platform_device *pdev;
28 + struct platform_device *spi_pdev;
29 + struct spi_board_info boardinfo;
33 +MODULE_DESCRIPTION("GPIO based MMC driver");
34 +MODULE_AUTHOR("Michael Buesch");
35 +MODULE_LICENSE("GPL");
38 +static int gpiommc_boardinfo_setup(struct spi_board_info *bi,
39 + struct spi_master *master,
42 + struct gpiommc_device *d = data;
43 + struct gpiommc_platform_data *pdata = d->pdev->dev.platform_data;
45 + /* Bind the SPI master to the MMC-SPI host driver. */
46 + strlcpy(bi->modalias, "mmc_spi", sizeof(bi->modalias));
48 + bi->max_speed_hz = pdata->max_bus_speed;
49 + bi->bus_num = master->bus_num;
50 + bi->mode = pdata->mode;
55 +static int gpiommc_probe(struct platform_device *pdev)
57 + struct gpiommc_platform_data *mmc_pdata = pdev->dev.platform_data;
58 + struct spi_gpio_platform_data spi_pdata;
59 + struct gpiommc_device *d;
66 + /* Allocate the GPIO-MMC device */
68 + d = kzalloc(sizeof(*d), GFP_KERNEL);
73 + /* Create the SPI-GPIO device */
74 + d->spi_pdev = platform_device_alloc(SPI_GPIO_PLATDEV_NAME,
75 + spi_gpio_next_id());
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;
89 + err = platform_device_add_data(d->spi_pdev, &spi_pdata,
93 + err = platform_device_add(d->spi_pdev);
95 + goto err_free_pdata;
96 + platform_set_drvdata(pdev, d);
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);
108 + kfree(d->spi_pdev->dev.platform_data);
109 + d->spi_pdev->dev.platform_data = NULL;
111 + platform_device_put(d->spi_pdev);
118 +static int gpiommc_remove(struct platform_device *pdev)
120 + struct gpiommc_device *d = platform_get_drvdata(pdev);
121 + struct gpiommc_platform_data *pdata = d->pdev->dev.platform_data;
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);
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 */
134 + /* The platform device that we allocated. */
135 + struct platform_device *pdev;
136 + /* gpiommc_sysfs_list */
137 + struct list_head list;
140 +static LIST_HEAD(gpiommc_sysfs_list);
141 +static DEFINE_MUTEX(gpiommc_sysfs_mutex);
143 +static struct gpiommc_sysfs_platform_data *gpiommc_sysfs_find_dev(const char *name)
145 + struct gpiommc_sysfs_platform_data *pdata;
147 + list_for_each_entry(pdata, &gpiommc_sysfs_list, list) {
148 + if (strcmp(pdata->p.name, name) == 0)
155 +static ssize_t gpiommc_add_store(struct device_driver *drv,
156 + const char *buf, size_t count)
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;
163 + mutex_lock(&gpiommc_sysfs_mutex);
165 + pdata = &pdata_local;
166 + memset(pdata, 0, sizeof(*pdata));
169 + res = sscanf(buf, "%" GPIOMMC_MAX_NAMELEN_STR "s %u %u %u %u %u %u %u %u",
171 + &pdata->p.pins.gpio_di,
172 + &pdata->p.pins.gpio_do,
173 + &pdata->p.pins.gpio_clk,
174 + &pdata->p.pins.gpio_cs,
176 + &pdata->p.max_bus_speed,
179 + pdata->p.mode = mode;
180 + pdata->p.no_spi_delay = !!no_spi_delay;
181 + pdata->p.pins.cs_activelow = !!csactivelow;
183 + pdata->p.pins.cs_activelow = 1; /* Default: CS = activelow */
185 + pdata->p.no_spi_delay = 0; /* Default: Delay turned on */
187 + pdata->p.max_bus_speed = 5000000; /* Default: 5Mhz */
189 + pdata->p.mode = 0; /* Default: SPI mode 0 */
190 + if (res < 5 || res > 9)
191 + goto out; /* First 5 args are mandatory. */
193 + /* Convert mode so that the SPI subsystem does understand it. */
194 + switch (pdata->p.mode) {
196 + pdata->p.mode = SPI_MODE_0;
199 + pdata->p.mode = SPI_MODE_1;
202 + pdata->p.mode = SPI_MODE_2;
205 + pdata->p.mode = SPI_MODE_3;
208 + goto out; /* Invalid mode */
212 + if (gpiommc_sysfs_find_dev(pdata->p.name))
216 + pdev = platform_device_alloc(GPIOMMC_PLATDEV_NAME, gpiommc_next_id());
220 + err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
222 + goto err_free_pdev;
223 + pdata = pdev->dev.platform_data;
225 + err = platform_device_add(pdev);
227 + goto err_free_pdev;
229 + pdata->pdev = pdev;
230 + INIT_LIST_HEAD(&pdata->list);
231 + list_add(&pdata->list, &gpiommc_sysfs_list);
235 + mutex_unlock(&gpiommc_sysfs_mutex);
237 + return err ? err : count;
240 + platform_device_put(pdev);
244 +static ssize_t gpiommc_remove_store(struct device_driver *drv,
245 + const char *buf, size_t count)
247 + struct gpiommc_sysfs_platform_data *pdata;
250 + mutex_lock(&gpiommc_sysfs_mutex);
253 + pdata = gpiommc_sysfs_find_dev(buf);
257 + list_del(&pdata->list);
258 + platform_device_unregister(pdata->pdev);
261 + mutex_unlock(&gpiommc_sysfs_mutex);
263 + return err ? err : count;
266 +static DRIVER_ATTR(add, 0200,
267 + NULL, gpiommc_add_store);
268 +static DRIVER_ATTR(remove, 0200,
269 + NULL, gpiommc_remove_store);
271 +static struct platform_driver gpiommc_plat_driver = {
272 + .probe = gpiommc_probe,
273 + .remove = gpiommc_remove,
275 + .name = GPIOMMC_PLATDEV_NAME,
276 + .owner = THIS_MODULE,
280 +int gpiommc_next_id(void)
282 + static atomic_t counter = ATOMIC_INIT(-1);
284 + return atomic_inc_return(&counter);
286 +EXPORT_SYMBOL(gpiommc_next_id);
288 +static int __init gpiommc_modinit(void)
292 + err = platform_driver_register(&gpiommc_plat_driver);
295 + err = driver_create_file(&gpiommc_plat_driver.driver,
298 + goto err_drv_unreg;
299 + err = driver_create_file(&gpiommc_plat_driver.driver,
300 + &driver_attr_remove);
302 + goto err_remove_add;
307 + driver_remove_file(&gpiommc_plat_driver.driver,
310 + platform_driver_unregister(&gpiommc_plat_driver);
313 +module_init(gpiommc_modinit);
315 +static void __exit gpiommc_modexit(void)
317 + struct gpiommc_sysfs_platform_data *pdata, *pdata_tmp;
319 + driver_remove_file(&gpiommc_plat_driver.driver,
320 + &driver_attr_remove);
321 + driver_remove_file(&gpiommc_plat_driver.driver,
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);
329 + mutex_unlock(&gpiommc_sysfs_mutex);
331 + platform_driver_unregister(&gpiommc_plat_driver);
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
340 If unsure, or if your system has no SPI master driver, say N.
343 + tristate "MMC/SD over GPIO-based SPI"
344 + depends on MMC && MMC_SPI && SPI_GPIO
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.
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.
355 + The module will be called gpiommc.
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
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
377 + * Device driver for MMC/SD cards driven over a GPIO bus.
379 + * Copyright (c) 2008 Michael Buesch
381 + * Licensed under the GNU/GPL version 2.
383 +#ifndef LINUX_GPIOMMC_H_
384 +#define LINUX_GPIOMMC_H_
386 +#include <linux/types.h>
389 +#define GPIOMMC_MAX_NAMELEN 15
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.
398 +struct gpiommc_pins {
399 + unsigned int gpio_di;
400 + unsigned int gpio_do;
401 + unsigned int gpio_clk;
402 + unsigned int gpio_cs;
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.
416 +struct gpiommc_platform_data {
417 + char name[GPIOMMC_MAX_NAMELEN + 1];
418 + struct gpiommc_pins pins;
421 + unsigned int max_bus_speed;
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.
428 +#define GPIOMMC_PLATDEV_NAME "gpiommc"
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
435 +int gpiommc_next_id(void);
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
443 +GPIOMMC - Driver for an MMC/SD card on a bitbanging GPIO SPI bus
444 +================================================================
446 +The gpiommc module hooks up the mmc_spi and spi_gpio modules for running an
447 +MMC or SD card on GPIO pins.
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.
453 +Registering devices via platform-device
454 +=======================================
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.
460 +The data structures are declared in <linux/mmc/gpiommc.h>
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.
467 +Then allocate a new instance of a platform device by doing:
469 + pdev = platform_device_alloc(GPIOMMC_PLATDEV_NAME, gpiommc_next_id());
471 +This will allocate the platform device data structures and hook it up to the
473 +Then add the gpiommc_platform_data to the platform device.
475 + err = platform_device_add_data(pdev, pdata, sizeof(struct gpiommc_platform_data));
477 +You may free the local instance of struct gpiommc_platform_data now.
478 +Now simply register the platform device.
480 + err = platform_device_add(pdev);
482 +Done. The gpiommc probe routine should be called and you should see a dmesg
483 +message for the added device.
486 +Registering devices via sysfs
487 +=============================
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.
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
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:
502 +DEVNAME DIpin DOpin CLKpin CSpin SPIMODE MAXBUSSPEED NO_SPI_DELAY CSACTIVELOW
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
517 +Note that the elements SPIMODE, MAXBUSSPEED and NO_SPI_DELAY are optional
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.
526 + echo -n "my_device 5 4 3 7 0 1000000 1" > $ADD
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.
534 +To remove a device, simply echo the device name string to the "remove" file.
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/
554 M: mhoffman@lightlink.com