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