d14321cfdc86e9be9824a8c976f09979f757aa8f
[openwrt/openwrt.git] / target / linux / generic-2.6 / patches-2.6.28 / 922-gpiommc.patch
1 Index: linux-2.6.28.2/drivers/mmc/host/gpiommc.c
2 ===================================================================
3 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4 +++ linux-2.6.28.2/drivers/mmc/host/gpiommc.c 2009-02-10 17:16:16.000000000 +0100
5 @@ -0,0 +1,608 @@
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 configfs 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_old.h>
21 +#include <linux/configfs.h>
22 +#include <linux/gpio.h>
23 +#include <asm/atomic.h>
24 +
25 +
26 +#define PFX "gpio-mmc: "
27 +
28 +
29 +struct gpiommc_device {
30 + struct platform_device *pdev;
31 + struct platform_device *spi_pdev;
32 + struct spi_board_info boardinfo;
33 +};
34 +
35 +
36 +MODULE_DESCRIPTION("GPIO based MMC driver");
37 +MODULE_AUTHOR("Michael Buesch");
38 +MODULE_LICENSE("GPL");
39 +
40 +
41 +static int gpiommc_boardinfo_setup(struct spi_board_info *bi,
42 + struct spi_master *master,
43 + void *data)
44 +{
45 + struct gpiommc_device *d = data;
46 + struct gpiommc_platform_data *pdata = d->pdev->dev.platform_data;
47 +
48 + /* Bind the SPI master to the MMC-SPI host driver. */
49 + strlcpy(bi->modalias, "mmc_spi", sizeof(bi->modalias));
50 +
51 + bi->max_speed_hz = pdata->max_bus_speed;
52 + bi->bus_num = master->bus_num;
53 + bi->mode = pdata->mode;
54 +
55 + return 0;
56 +}
57 +
58 +static int gpiommc_probe(struct platform_device *pdev)
59 +{
60 + struct gpiommc_platform_data *mmc_pdata = pdev->dev.platform_data;
61 + struct spi_gpio_platform_data spi_pdata;
62 + struct gpiommc_device *d;
63 + int err;
64 +
65 + err = -ENXIO;
66 + if (!mmc_pdata)
67 + goto error;
68 +
69 +#ifdef CONFIG_MMC_SPI_MODULE
70 + err = request_module("mmc_spi");
71 + if (err) {
72 + printk(KERN_WARNING PFX
73 + "Failed to request mmc_spi module.\n");
74 + }
75 +#endif /* CONFIG_MMC_SPI_MODULE */
76 +
77 + /* Allocate the GPIO-MMC device */
78 + err = -ENOMEM;
79 + d = kzalloc(sizeof(*d), GFP_KERNEL);
80 + if (!d)
81 + goto error;
82 + d->pdev = pdev;
83 +
84 + /* Create the SPI-GPIO device */
85 + d->spi_pdev = platform_device_alloc(SPI_GPIO_PLATDEV_NAME,
86 + spi_gpio_next_id());
87 + if (!d->spi_pdev)
88 + goto err_free_d;
89 +
90 + memset(&spi_pdata, 0, sizeof(spi_pdata));
91 + spi_pdata.pin_clk = mmc_pdata->pins.gpio_clk;
92 + spi_pdata.pin_miso = mmc_pdata->pins.gpio_do;
93 + spi_pdata.pin_mosi = mmc_pdata->pins.gpio_di;
94 + spi_pdata.pin_cs = mmc_pdata->pins.gpio_cs;
95 + spi_pdata.cs_activelow = mmc_pdata->pins.cs_activelow;
96 + spi_pdata.no_spi_delay = mmc_pdata->no_spi_delay;
97 + spi_pdata.boardinfo_setup = gpiommc_boardinfo_setup;
98 + spi_pdata.boardinfo_setup_data = d;
99 +
100 + err = platform_device_add_data(d->spi_pdev, &spi_pdata,
101 + sizeof(spi_pdata));
102 + if (err)
103 + goto err_free_pdev;
104 + err = platform_device_add(d->spi_pdev);
105 + if (err)
106 + goto err_free_pdata;
107 + platform_set_drvdata(pdev, d);
108 +
109 + printk(KERN_INFO PFX "MMC-Card \"%s\" "
110 + "attached to GPIO pins di=%u, do=%u, clk=%u, cs=%u\n",
111 + mmc_pdata->name, mmc_pdata->pins.gpio_di,
112 + mmc_pdata->pins.gpio_do,
113 + mmc_pdata->pins.gpio_clk,
114 + mmc_pdata->pins.gpio_cs);
115 +
116 + return 0;
117 +
118 +err_free_pdata:
119 + kfree(d->spi_pdev->dev.platform_data);
120 + d->spi_pdev->dev.platform_data = NULL;
121 +err_free_pdev:
122 + platform_device_put(d->spi_pdev);
123 +err_free_d:
124 + kfree(d);
125 +error:
126 + return err;
127 +}
128 +
129 +static int gpiommc_remove(struct platform_device *pdev)
130 +{
131 + struct gpiommc_device *d = platform_get_drvdata(pdev);
132 + struct gpiommc_platform_data *pdata = d->pdev->dev.platform_data;
133 +
134 + platform_device_unregister(d->spi_pdev);
135 + printk(KERN_INFO PFX "GPIO based MMC-Card \"%s\" removed\n",
136 + pdata->name);
137 + platform_device_put(d->spi_pdev);
138 +
139 + return 0;
140 +}
141 +
142 +#ifdef CONFIG_GPIOMMC_CONFIGFS
143 +
144 +/* A device that was created through configfs */
145 +struct gpiommc_configfs_device {
146 + struct config_item item;
147 + /* The platform device, after registration. */
148 + struct platform_device *pdev;
149 + /* The configuration */
150 + struct gpiommc_platform_data pdata;
151 +};
152 +
153 +#define GPIO_INVALID -1
154 +
155 +static inline bool gpiommc_is_registered(struct gpiommc_configfs_device *dev)
156 +{
157 + return (dev->pdev != NULL);
158 +}
159 +
160 +static inline struct gpiommc_configfs_device *ci_to_gpiommc(struct config_item *item)
161 +{
162 + return item ? container_of(item, struct gpiommc_configfs_device, item) : NULL;
163 +}
164 +
165 +static struct configfs_attribute gpiommc_attr_DI = {
166 + .ca_owner = THIS_MODULE,
167 + .ca_name = "gpio_data_in",
168 + .ca_mode = S_IRUGO | S_IWUSR,
169 +};
170 +
171 +static struct configfs_attribute gpiommc_attr_DO = {
172 + .ca_owner = THIS_MODULE,
173 + .ca_name = "gpio_data_out",
174 + .ca_mode = S_IRUGO | S_IWUSR,
175 +};
176 +
177 +static struct configfs_attribute gpiommc_attr_CLK = {
178 + .ca_owner = THIS_MODULE,
179 + .ca_name = "gpio_clock",
180 + .ca_mode = S_IRUGO | S_IWUSR,
181 +};
182 +
183 +static struct configfs_attribute gpiommc_attr_CS = {
184 + .ca_owner = THIS_MODULE,
185 + .ca_name = "gpio_chipselect",
186 + .ca_mode = S_IRUGO | S_IWUSR,
187 +};
188 +
189 +static struct configfs_attribute gpiommc_attr_CS_activelow = {
190 + .ca_owner = THIS_MODULE,
191 + .ca_name = "gpio_chipselect_activelow",
192 + .ca_mode = S_IRUGO | S_IWUSR,
193 +};
194 +
195 +static struct configfs_attribute gpiommc_attr_spimode = {
196 + .ca_owner = THIS_MODULE,
197 + .ca_name = "spi_mode",
198 + .ca_mode = S_IRUGO | S_IWUSR,
199 +};
200 +
201 +static struct configfs_attribute gpiommc_attr_spidelay = {
202 + .ca_owner = THIS_MODULE,
203 + .ca_name = "spi_delay",
204 + .ca_mode = S_IRUGO | S_IWUSR,
205 +};
206 +
207 +static struct configfs_attribute gpiommc_attr_max_bus_speed = {
208 + .ca_owner = THIS_MODULE,
209 + .ca_name = "max_bus_speed",
210 + .ca_mode = S_IRUGO | S_IWUSR,
211 +};
212 +
213 +static struct configfs_attribute gpiommc_attr_register = {
214 + .ca_owner = THIS_MODULE,
215 + .ca_name = "register",
216 + .ca_mode = S_IRUGO | S_IWUSR,
217 +};
218 +
219 +static struct configfs_attribute *gpiommc_config_attrs[] = {
220 + &gpiommc_attr_DI,
221 + &gpiommc_attr_DO,
222 + &gpiommc_attr_CLK,
223 + &gpiommc_attr_CS,
224 + &gpiommc_attr_CS_activelow,
225 + &gpiommc_attr_spimode,
226 + &gpiommc_attr_spidelay,
227 + &gpiommc_attr_max_bus_speed,
228 + &gpiommc_attr_register,
229 + NULL,
230 +};
231 +
232 +static ssize_t gpiommc_config_attr_show(struct config_item *item,
233 + struct configfs_attribute *attr,
234 + char *page)
235 +{
236 + struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
237 + ssize_t count = 0;
238 + unsigned int gpio;
239 + int err = 0;
240 +
241 + if (attr == &gpiommc_attr_DI) {
242 + gpio = dev->pdata.pins.gpio_di;
243 + if (gpio == GPIO_INVALID)
244 + count = snprintf(page, PAGE_SIZE, "not configured\n");
245 + else
246 + count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
247 + goto out;
248 + }
249 + if (attr == &gpiommc_attr_DO) {
250 + gpio = dev->pdata.pins.gpio_do;
251 + if (gpio == GPIO_INVALID)
252 + count = snprintf(page, PAGE_SIZE, "not configured\n");
253 + else
254 + count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
255 + goto out;
256 + }
257 + if (attr == &gpiommc_attr_CLK) {
258 + gpio = dev->pdata.pins.gpio_clk;
259 + if (gpio == GPIO_INVALID)
260 + count = snprintf(page, PAGE_SIZE, "not configured\n");
261 + else
262 + count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
263 + goto out;
264 + }
265 + if (attr == &gpiommc_attr_CS) {
266 + gpio = dev->pdata.pins.gpio_cs;
267 + if (gpio == GPIO_INVALID)
268 + count = snprintf(page, PAGE_SIZE, "not configured\n");
269 + else
270 + count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
271 + goto out;
272 + }
273 + if (attr == &gpiommc_attr_CS_activelow) {
274 + count = snprintf(page, PAGE_SIZE, "%u\n",
275 + dev->pdata.pins.cs_activelow);
276 + goto out;
277 + }
278 + if (attr == &gpiommc_attr_spimode) {
279 + count = snprintf(page, PAGE_SIZE, "%u\n",
280 + dev->pdata.mode);
281 + goto out;
282 + }
283 + if (attr == &gpiommc_attr_spidelay) {
284 + count = snprintf(page, PAGE_SIZE, "%u\n",
285 + !dev->pdata.no_spi_delay);
286 + goto out;
287 + }
288 + if (attr == &gpiommc_attr_max_bus_speed) {
289 + count = snprintf(page, PAGE_SIZE, "%u\n",
290 + dev->pdata.max_bus_speed);
291 + goto out;
292 + }
293 + if (attr == &gpiommc_attr_register) {
294 + count = snprintf(page, PAGE_SIZE, "%u\n",
295 + gpiommc_is_registered(dev));
296 + goto out;
297 + }
298 + WARN_ON(1);
299 + err = -ENOSYS;
300 +out:
301 + return err ? err : count;
302 +}
303 +
304 +static int gpiommc_do_register(struct gpiommc_configfs_device *dev,
305 + const char *name)
306 +{
307 + int err;
308 +
309 + if (gpiommc_is_registered(dev))
310 + return 0;
311 +
312 + if (!gpio_is_valid(dev->pdata.pins.gpio_di) ||
313 + !gpio_is_valid(dev->pdata.pins.gpio_do) ||
314 + !gpio_is_valid(dev->pdata.pins.gpio_clk) ||
315 + !gpio_is_valid(dev->pdata.pins.gpio_cs)) {
316 + printk(KERN_ERR PFX
317 + "configfs: Invalid GPIO pin number(s)\n");
318 + return -EINVAL;
319 + }
320 +
321 + strlcpy(dev->pdata.name, name,
322 + sizeof(dev->pdata.name));
323 +
324 + dev->pdev = platform_device_alloc(GPIOMMC_PLATDEV_NAME,
325 + gpiommc_next_id());
326 + if (!dev->pdev)
327 + return -ENOMEM;
328 + err = platform_device_add_data(dev->pdev, &dev->pdata,
329 + sizeof(dev->pdata));
330 + if (err) {
331 + platform_device_put(dev->pdev);
332 + return err;
333 + }
334 + err = platform_device_add(dev->pdev);
335 + if (err) {
336 + platform_device_put(dev->pdev);
337 + return err;
338 + }
339 +
340 + return 0;
341 +}
342 +
343 +static void gpiommc_do_unregister(struct gpiommc_configfs_device *dev)
344 +{
345 + if (!gpiommc_is_registered(dev))
346 + return;
347 +
348 + platform_device_unregister(dev->pdev);
349 + dev->pdev = NULL;
350 +}
351 +
352 +static ssize_t gpiommc_config_attr_store(struct config_item *item,
353 + struct configfs_attribute *attr,
354 + const char *page, size_t count)
355 +{
356 + struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
357 + int err = -EINVAL;
358 + unsigned long data;
359 +
360 + if (attr == &gpiommc_attr_register) {
361 + err = strict_strtoul(page, 10, &data);
362 + if (err)
363 + goto out;
364 + err = -EINVAL;
365 + if (data == 1)
366 + err = gpiommc_do_register(dev, item->ci_name);
367 + if (data == 0) {
368 + gpiommc_do_unregister(dev);
369 + err = 0;
370 + }
371 + goto out;
372 + }
373 +
374 + if (gpiommc_is_registered(dev)) {
375 + /* The rest of the config parameters can only be set
376 + * as long as the device is not registered, yet. */
377 + err = -EBUSY;
378 + goto out;
379 + }
380 +
381 + if (attr == &gpiommc_attr_DI) {
382 + err = strict_strtoul(page, 10, &data);
383 + if (err)
384 + goto out;
385 + err = -EINVAL;
386 + if (!gpio_is_valid(data))
387 + goto out;
388 + dev->pdata.pins.gpio_di = data;
389 + err = 0;
390 + goto out;
391 + }
392 + if (attr == &gpiommc_attr_DO) {
393 + err = strict_strtoul(page, 10, &data);
394 + if (err)
395 + goto out;
396 + err = -EINVAL;
397 + if (!gpio_is_valid(data))
398 + goto out;
399 + dev->pdata.pins.gpio_do = data;
400 + err = 0;
401 + goto out;
402 + }
403 + if (attr == &gpiommc_attr_CLK) {
404 + err = strict_strtoul(page, 10, &data);
405 + if (err)
406 + goto out;
407 + err = -EINVAL;
408 + if (!gpio_is_valid(data))
409 + goto out;
410 + dev->pdata.pins.gpio_clk = data;
411 + err = 0;
412 + goto out;
413 + }
414 + if (attr == &gpiommc_attr_CS) {
415 + err = strict_strtoul(page, 10, &data);
416 + if (err)
417 + goto out;
418 + err = -EINVAL;
419 + if (!gpio_is_valid(data))
420 + goto out;
421 + dev->pdata.pins.gpio_cs = data;
422 + err = 0;
423 + goto out;
424 + }
425 + if (attr == &gpiommc_attr_CS_activelow) {
426 + err = strict_strtoul(page, 10, &data);
427 + if (err)
428 + goto out;
429 + err = -EINVAL;
430 + if (data != 0 && data != 1)
431 + goto out;
432 + dev->pdata.pins.cs_activelow = data;
433 + err = 0;
434 + goto out;
435 + }
436 + if (attr == &gpiommc_attr_spimode) {
437 + err = strict_strtoul(page, 10, &data);
438 + if (err)
439 + goto out;
440 + err = -EINVAL;
441 + switch (data) {
442 + case 0:
443 + dev->pdata.mode = SPI_MODE_0;
444 + break;
445 + case 1:
446 + dev->pdata.mode = SPI_MODE_1;
447 + break;
448 + case 2:
449 + dev->pdata.mode = SPI_MODE_2;
450 + break;
451 + case 3:
452 + dev->pdata.mode = SPI_MODE_3;
453 + break;
454 + default:
455 + goto out;
456 + }
457 + err = 0;
458 + goto out;
459 + }
460 + if (attr == &gpiommc_attr_spidelay) {
461 + err = strict_strtoul(page, 10, &data);
462 + if (err)
463 + goto out;
464 + err = -EINVAL;
465 + if (data != 0 && data != 1)
466 + goto out;
467 + dev->pdata.no_spi_delay = !data;
468 + err = 0;
469 + goto out;
470 + }
471 + if (attr == &gpiommc_attr_max_bus_speed) {
472 + err = strict_strtoul(page, 10, &data);
473 + if (err)
474 + goto out;
475 + err = -EINVAL;
476 + if (data > UINT_MAX)
477 + goto out;
478 + dev->pdata.max_bus_speed = data;
479 + err = 0;
480 + goto out;
481 + }
482 + WARN_ON(1);
483 + err = -ENOSYS;
484 +out:
485 + return err ? err : count;
486 +}
487 +
488 +static void gpiommc_config_item_release(struct config_item *item)
489 +{
490 + struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
491 +
492 + kfree(dev);
493 +}
494 +
495 +static struct configfs_item_operations gpiommc_config_item_ops = {
496 + .release = gpiommc_config_item_release,
497 + .show_attribute = gpiommc_config_attr_show,
498 + .store_attribute = gpiommc_config_attr_store,
499 +};
500 +
501 +static struct config_item_type gpiommc_dev_ci_type = {
502 + .ct_item_ops = &gpiommc_config_item_ops,
503 + .ct_attrs = gpiommc_config_attrs,
504 + .ct_owner = THIS_MODULE,
505 +};
506 +
507 +static struct config_item *gpiommc_make_item(struct config_group *group,
508 + const char *name)
509 +{
510 + struct gpiommc_configfs_device *dev;
511 +
512 + if (strlen(name) > GPIOMMC_MAX_NAMELEN) {
513 + printk(KERN_ERR PFX "configfs: device name too long\n");
514 + return NULL;
515 + }
516 +
517 + dev = kzalloc(sizeof(*dev), GFP_KERNEL);
518 + if (!dev)
519 + return NULL;
520 +
521 + config_item_init_type_name(&dev->item, name,
522 + &gpiommc_dev_ci_type);
523 +
524 + /* Assign default configuration */
525 + dev->pdata.pins.gpio_di = GPIO_INVALID;
526 + dev->pdata.pins.gpio_do = GPIO_INVALID;
527 + dev->pdata.pins.gpio_clk = GPIO_INVALID;
528 + dev->pdata.pins.gpio_cs = GPIO_INVALID;
529 + dev->pdata.pins.cs_activelow = 1;
530 + dev->pdata.mode = SPI_MODE_0;
531 + dev->pdata.no_spi_delay = 0;
532 + dev->pdata.max_bus_speed = 5000000; /* 5 MHz */
533 +
534 + return &(dev->item);
535 +}
536 +
537 +static void gpiommc_drop_item(struct config_group *group,
538 + struct config_item *item)
539 +{
540 + struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
541 +
542 + gpiommc_do_unregister(dev);
543 + kfree(dev);
544 +}
545 +
546 +static struct configfs_group_operations gpiommc_ct_group_ops = {
547 + .make_item = gpiommc_make_item,
548 + .drop_item = gpiommc_drop_item,
549 +};
550 +
551 +static struct config_item_type gpiommc_ci_type = {
552 + .ct_group_ops = &gpiommc_ct_group_ops,
553 + .ct_owner = THIS_MODULE,
554 +};
555 +
556 +static struct configfs_subsystem gpiommc_subsys = {
557 + .su_group = {
558 + .cg_item = {
559 + .ci_namebuf = GPIOMMC_PLATDEV_NAME,
560 + .ci_type = &gpiommc_ci_type,
561 + },
562 + },
563 + .su_mutex = __MUTEX_INITIALIZER(gpiommc_subsys.su_mutex),
564 +};
565 +
566 +#endif /* CONFIG_GPIOMMC_CONFIGFS */
567 +
568 +static struct platform_driver gpiommc_plat_driver = {
569 + .probe = gpiommc_probe,
570 + .remove = gpiommc_remove,
571 + .driver = {
572 + .name = GPIOMMC_PLATDEV_NAME,
573 + .owner = THIS_MODULE,
574 + },
575 +};
576 +
577 +int gpiommc_next_id(void)
578 +{
579 + static atomic_t counter = ATOMIC_INIT(-1);
580 +
581 + return atomic_inc_return(&counter);
582 +}
583 +EXPORT_SYMBOL(gpiommc_next_id);
584 +
585 +static int __init gpiommc_modinit(void)
586 +{
587 + int err;
588 +
589 + err = platform_driver_register(&gpiommc_plat_driver);
590 + if (err)
591 + return err;
592 +
593 +#ifdef CONFIG_GPIOMMC_CONFIGFS
594 + config_group_init(&gpiommc_subsys.su_group);
595 + err = configfs_register_subsystem(&gpiommc_subsys);
596 + if (err) {
597 + platform_driver_unregister(&gpiommc_plat_driver);
598 + return err;
599 + }
600 +#endif /* CONFIG_GPIOMMC_CONFIGFS */
601 +
602 + return 0;
603 +}
604 +module_init(gpiommc_modinit);
605 +
606 +static void __exit gpiommc_modexit(void)
607 +{
608 +#ifdef CONFIG_GPIOMMC_CONFIGFS
609 + configfs_unregister_subsystem(&gpiommc_subsys);
610 +#endif
611 + platform_driver_unregister(&gpiommc_plat_driver);
612 +}
613 +module_exit(gpiommc_modexit);
614 Index: linux-2.6.28.2/drivers/mmc/host/Kconfig
615 ===================================================================
616 --- linux-2.6.28.2.orig/drivers/mmc/host/Kconfig 2009-02-10 17:16:15.000000000 +0100
617 +++ linux-2.6.28.2/drivers/mmc/host/Kconfig 2009-02-10 17:16:16.000000000 +0100
618 @@ -192,3 +192,28 @@ config MMC_TMIO
619 help
620 This provides support for the SD/MMC cell found in TC6393XB,
621 T7L66XB and also ipaq ASIC3
622 +
623 +config GPIOMMC
624 + tristate "MMC/SD over GPIO-based SPI"
625 + depends on MMC && MMC_SPI && SPI_GPIO
626 + help
627 + This driver hooks up the mmc_spi and spi_gpio modules so that
628 + MMC/SD cards can be used on a GPIO based bus by bitbanging
629 + the SPI protocol in software.
630 +
631 + This driver provides a configfs interface to dynamically create
632 + and destroy GPIO-based MMC/SD card devices. It also provides
633 + a platform device interface API.
634 + See Documentation/gpiommc.txt for details.
635 +
636 + The module will be called gpiommc.
637 +
638 + If unsure, say N.
639 +
640 +config GPIOMMC_CONFIGFS
641 + bool
642 + depends on GPIOMMC && CONFIGFS_FS
643 + default y
644 + help
645 + This option automatically enables configfs support for gpiommc
646 + if configfs is available.
647 Index: linux-2.6.28.2/drivers/mmc/host/Makefile
648 ===================================================================
649 --- linux-2.6.28.2.orig/drivers/mmc/host/Makefile 2009-02-10 17:16:15.000000000 +0100
650 +++ linux-2.6.28.2/drivers/mmc/host/Makefile 2009-02-10 17:16:16.000000000 +0100
651 @@ -22,4 +22,5 @@ obj-$(CONFIG_MMC_SPI) += mmc_spi.o
652 obj-$(CONFIG_MMC_S3C) += s3cmci.o
653 obj-$(CONFIG_MMC_SDRICOH_CS) += sdricoh_cs.o
654 obj-$(CONFIG_MMC_TMIO) += tmio_mmc.o
655 +obj-$(CONFIG_GPIOMMC) += gpiommc.o
656
657 Index: linux-2.6.28.2/include/linux/mmc/gpiommc.h
658 ===================================================================
659 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
660 +++ linux-2.6.28.2/include/linux/mmc/gpiommc.h 2009-02-10 17:16:16.000000000 +0100
661 @@ -0,0 +1,71 @@
662 +/*
663 + * Device driver for MMC/SD cards driven over a GPIO bus.
664 + *
665 + * Copyright (c) 2008 Michael Buesch
666 + *
667 + * Licensed under the GNU/GPL version 2.
668 + */
669 +#ifndef LINUX_GPIOMMC_H_
670 +#define LINUX_GPIOMMC_H_
671 +
672 +#include <linux/types.h>
673 +
674 +
675 +#define GPIOMMC_MAX_NAMELEN 15
676 +#define GPIOMMC_MAX_NAMELEN_STR __stringify(GPIOMMC_MAX_NAMELEN)
677 +
678 +/**
679 + * struct gpiommc_pins - Hardware pin assignments
680 + *
681 + * @gpio_di: The GPIO number of the DATA IN pin
682 + * @gpio_do: The GPIO number of the DATA OUT pin
683 + * @gpio_clk: The GPIO number of the CLOCK pin
684 + * @gpio_cs: The GPIO number of the CHIPSELECT pin
685 + * @cs_activelow: If true, the chip is considered selected if @gpio_cs is low.
686 + */
687 +struct gpiommc_pins {
688 + unsigned int gpio_di;
689 + unsigned int gpio_do;
690 + unsigned int gpio_clk;
691 + unsigned int gpio_cs;
692 + bool cs_activelow;
693 +};
694 +
695 +/**
696 + * struct gpiommc_platform_data - Platform data for a MMC-over-SPI-GPIO device.
697 + *
698 + * @name: The unique name string of the device.
699 + * @pins: The hardware pin assignments.
700 + * @mode: The hardware mode. This is either SPI_MODE_0,
701 + * SPI_MODE_1, SPI_MODE_2 or SPI_MODE_3. See the SPI documentation.
702 + * @no_spi_delay: Do not use delays in the lowlevel SPI bitbanging code.
703 + * This is not standards compliant, but may be required for some
704 + * embedded machines to gain reasonable speed.
705 + * @max_bus_speed: The maximum speed of the SPI bus, in Hertz.
706 + */
707 +struct gpiommc_platform_data {
708 + char name[GPIOMMC_MAX_NAMELEN + 1];
709 + struct gpiommc_pins pins;
710 + u8 mode;
711 + bool no_spi_delay;
712 + unsigned int max_bus_speed;
713 +};
714 +
715 +/**
716 + * GPIOMMC_PLATDEV_NAME - The platform device name string.
717 + *
718 + * The name string that has to be used for platform_device_alloc
719 + * when allocating a gpiommc device.
720 + */
721 +#define GPIOMMC_PLATDEV_NAME "gpiommc"
722 +
723 +/**
724 + * gpiommc_next_id - Get another platform device ID number.
725 + *
726 + * This returns the next platform device ID number that has to be used
727 + * for platform_device_alloc. The ID is opaque and should not be used for
728 + * anything else.
729 + */
730 +int gpiommc_next_id(void);
731 +
732 +#endif /* LINUX_GPIOMMC_H_ */
733 Index: linux-2.6.28.2/Documentation/gpiommc.txt
734 ===================================================================
735 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
736 +++ linux-2.6.28.2/Documentation/gpiommc.txt 2009-02-10 17:16:16.000000000 +0100
737 @@ -0,0 +1,97 @@
738 +GPIOMMC - Driver for an MMC/SD card on a bitbanging GPIO SPI bus
739 +================================================================
740 +
741 +The gpiommc module hooks up the mmc_spi and spi_gpio modules for running an
742 +MMC or SD card on GPIO pins.
743 +
744 +Two interfaces for registering a new MMC/SD card device are provided:
745 +A static platform-device based mechanism and a dynamic configfs based interface.
746 +
747 +
748 +Registering devices via platform-device
749 +=======================================
750 +
751 +The platform-device interface is used for registering MMC/SD devices that are
752 +part of the hardware platform. This is most useful only for embedded machines
753 +with MMC/SD devices statically connected to the platform GPIO bus.
754 +
755 +The data structures are declared in <linux/mmc/gpiommc.h>.
756 +
757 +To register a new device, define an instance of struct gpiommc_platform_data.
758 +This structure holds any information about how the device is hooked up to the
759 +GPIO pins and what hardware modes the device supports. See the docbook-style
760 +documentation in the header file for more information on the struct fields.
761 +
762 +Then allocate a new instance of a platform device by doing:
763 +
764 + pdev = platform_device_alloc(GPIOMMC_PLATDEV_NAME, gpiommc_next_id());
765 +
766 +This will allocate the platform device data structures and hook it up to the
767 +gpiommc driver.
768 +Then add the gpiommc_platform_data to the platform device.
769 +
770 + err = platform_device_add_data(pdev, pdata, sizeof(struct gpiommc_platform_data));
771 +
772 +You may free the local instance of struct gpiommc_platform_data now. (So the
773 +struct may be allocated on the stack, too).
774 +Now simply register the platform device.
775 +
776 + err = platform_device_add(pdev);
777 +
778 +Done. The gpiommc probe routine will be invoked now and you should see a kernel
779 +log message for the added device.
780 +
781 +
782 +Registering devices via configfs
783 +================================
784 +
785 +MMC/SD cards connected via GPIO often are a pretty dynamic thing, as for example
786 +selfmade hacks for soldering an MMC/SD card to standard GPIO pins on embedded
787 +hardware are a common situation.
788 +So we provide a dynamic interface to conveniently handle adding and removing
789 +devices from userspace, without the need to recompile the kernel.
790 +
791 +The "gpiommc" subdirectory at the configfs mountpoint is used for handling
792 +the dynamic configuration.
793 +
794 +To create a new device, it must first be allocated with mkdir.
795 +The following command will allocate a device named "my_mmc":
796 + mkdir /config/gpiommc/my_mmc
797 +
798 +There are several configuration files available in the new
799 +/config/gpiommc/my_mmc/ directory:
800 +
801 +gpio_data_in = The SPI data-IN GPIO pin number.
802 +gpio_data_out = The SPI data-OUT GPIO pin number.
803 +gpio_clock = The SPI Clock GPIO pin number.
804 +gpio_chipselect = The SPI Chipselect GPIO pin number.
805 +gpio_chipselect_activelow = Boolean. If 0, Chipselect is active-HIGH.
806 + If 1, Chipselect is active-LOW.
807 +spi_mode = The SPI data mode. Can be 0-3.
808 +spi_delay = Enable all delays in the lowlevel bitbanging.
809 +max_bus_speed = The maximum SPI bus speed. In Hertz.
810 +
811 +register = Not a configuration parameter.
812 + Used to register the configured card
813 + with the kernel.
814 +
815 +The device must first get configured and then registered by writing "1" to
816 +the "register" file.
817 +The configuration parameters "gpio_data_in", "gpio_data_out", "gpio_clock"
818 +and "gpio_chipselect" are essential and _must_ be configured before writing
819 +"1" to the "register" file. The registration will fail, otherwise.
820 +
821 +The default values for the other parameters are:
822 +gpio_chipselect_activelow = 1 (CS active-LOW)
823 +spi_mode = 0 (SPI_MODE_0)
824 +spi_delay = 1 (enabled)
825 +max_bus_speed = 5000000 (5 Mhz)
826 +
827 +Configuration values can not be changed after registration. To unregister
828 +the device, write a "0" to the "register" file. The configuration can be
829 +changed again after unregistering.
830 +
831 +To completely remove the device, simply rmdir the directory
832 +(/config/gpiommc/my_mmc in this example).
833 +There's no need to first unregister the device before removing it. That will
834 +be done automatically.
835 Index: linux-2.6.28.2/MAINTAINERS
836 ===================================================================
837 --- linux-2.6.28.2.orig/MAINTAINERS 2009-02-10 17:16:15.000000000 +0100
838 +++ linux-2.6.28.2/MAINTAINERS 2009-02-10 17:16:16.000000000 +0100
839 @@ -1911,6 +1911,11 @@ W: http://moinejf.free.fr
840 L: video4linux-list@redhat.com
841 S: Maintained
842
843 +GPIOMMC DRIVER
844 +P: Michael Buesch
845 +M: mb@bu3sch.de
846 +S: Maintained
847 +
848 HARDWARE MONITORING
849 L: lm-sensors@lm-sensors.org
850 W: http://www.lm-sensors.org/