863-gpiommc.patch 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. --- /dev/null
  2. +++ b/drivers/mmc/host/gpiommc.c
  3. @@ -0,0 +1,609 @@
  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/module.h>
  15. +#include <linux/mmc/gpiommc.h>
  16. +#include <linux/platform_device.h>
  17. +#include <linux/list.h>
  18. +#include <linux/mutex.h>
  19. +#include <linux/spi/spi_gpio_old.h>
  20. +#include <linux/configfs.h>
  21. +#include <linux/gpio.h>
  22. +#include <asm/atomic.h>
  23. +
  24. +
  25. +#define PFX "gpio-mmc: "
  26. +
  27. +
  28. +struct gpiommc_device {
  29. + struct platform_device *pdev;
  30. + struct platform_device *spi_pdev;
  31. + struct spi_board_info boardinfo;
  32. +};
  33. +
  34. +
  35. +MODULE_DESCRIPTION("GPIO based MMC driver");
  36. +MODULE_AUTHOR("Michael Buesch");
  37. +MODULE_LICENSE("GPL");
  38. +
  39. +
  40. +static int gpiommc_boardinfo_setup(struct spi_board_info *bi,
  41. + struct spi_master *master,
  42. + void *data)
  43. +{
  44. + struct gpiommc_device *d = data;
  45. + struct gpiommc_platform_data *pdata = d->pdev->dev.platform_data;
  46. +
  47. + /* Bind the SPI master to the MMC-SPI host driver. */
  48. + strlcpy(bi->modalias, "mmc_spi", sizeof(bi->modalias));
  49. +
  50. + bi->max_speed_hz = pdata->max_bus_speed;
  51. + bi->bus_num = master->bus_num;
  52. + bi->mode = pdata->mode;
  53. +
  54. + return 0;
  55. +}
  56. +
  57. +static int gpiommc_probe(struct platform_device *pdev)
  58. +{
  59. + struct gpiommc_platform_data *mmc_pdata = pdev->dev.platform_data;
  60. + struct spi_gpio_platform_data spi_pdata;
  61. + struct gpiommc_device *d;
  62. + int err;
  63. +
  64. + err = -ENXIO;
  65. + if (!mmc_pdata)
  66. + goto error;
  67. +
  68. +#ifdef CONFIG_MMC_SPI_MODULE
  69. + err = request_module("mmc_spi");
  70. + if (err) {
  71. + printk(KERN_WARNING PFX
  72. + "Failed to request mmc_spi module.\n");
  73. + }
  74. +#endif /* CONFIG_MMC_SPI_MODULE */
  75. +
  76. + /* Allocate the GPIO-MMC device */
  77. + err = -ENOMEM;
  78. + d = kzalloc(sizeof(*d), GFP_KERNEL);
  79. + if (!d)
  80. + goto error;
  81. + d->pdev = pdev;
  82. +
  83. + /* Create the SPI-GPIO device */
  84. + d->spi_pdev = platform_device_alloc(SPI_GPIO_PLATDEV_NAME,
  85. + spi_gpio_next_id());
  86. + if (!d->spi_pdev)
  87. + goto err_free_d;
  88. +
  89. + memset(&spi_pdata, 0, sizeof(spi_pdata));
  90. + spi_pdata.pin_clk = mmc_pdata->pins.gpio_clk;
  91. + spi_pdata.pin_miso = mmc_pdata->pins.gpio_do;
  92. + spi_pdata.pin_mosi = mmc_pdata->pins.gpio_di;
  93. + spi_pdata.pin_cs = mmc_pdata->pins.gpio_cs;
  94. + spi_pdata.cs_activelow = mmc_pdata->pins.cs_activelow;
  95. + spi_pdata.no_spi_delay = mmc_pdata->no_spi_delay;
  96. + spi_pdata.boardinfo_setup = gpiommc_boardinfo_setup;
  97. + spi_pdata.boardinfo_setup_data = d;
  98. +
  99. + err = platform_device_add_data(d->spi_pdev, &spi_pdata,
  100. + sizeof(spi_pdata));
  101. + if (err)
  102. + goto err_free_pdev;
  103. + err = platform_device_add(d->spi_pdev);
  104. + if (err)
  105. + goto err_free_pdata;
  106. + platform_set_drvdata(pdev, d);
  107. +
  108. + printk(KERN_INFO PFX "MMC-Card \"%s\" "
  109. + "attached to GPIO pins di=%u, do=%u, clk=%u, cs=%u\n",
  110. + mmc_pdata->name, mmc_pdata->pins.gpio_di,
  111. + mmc_pdata->pins.gpio_do,
  112. + mmc_pdata->pins.gpio_clk,
  113. + mmc_pdata->pins.gpio_cs);
  114. +
  115. + return 0;
  116. +
  117. +err_free_pdata:
  118. + kfree(d->spi_pdev->dev.platform_data);
  119. + d->spi_pdev->dev.platform_data = NULL;
  120. +err_free_pdev:
  121. + platform_device_put(d->spi_pdev);
  122. +err_free_d:
  123. + kfree(d);
  124. +error:
  125. + return err;
  126. +}
  127. +
  128. +static int gpiommc_remove(struct platform_device *pdev)
  129. +{
  130. + struct gpiommc_device *d = platform_get_drvdata(pdev);
  131. + struct gpiommc_platform_data *pdata = d->pdev->dev.platform_data;
  132. +
  133. + platform_device_unregister(d->spi_pdev);
  134. + printk(KERN_INFO PFX "GPIO based MMC-Card \"%s\" removed\n",
  135. + pdata->name);
  136. + platform_device_put(d->spi_pdev);
  137. +
  138. + return 0;
  139. +}
  140. +
  141. +#ifdef CONFIG_GPIOMMC_CONFIGFS
  142. +
  143. +/* A device that was created through configfs */
  144. +struct gpiommc_configfs_device {
  145. + struct config_item item;
  146. + /* The platform device, after registration. */
  147. + struct platform_device *pdev;
  148. + /* The configuration */
  149. + struct gpiommc_platform_data pdata;
  150. +};
  151. +
  152. +#define GPIO_INVALID -1
  153. +
  154. +static inline bool gpiommc_is_registered(struct gpiommc_configfs_device *dev)
  155. +{
  156. + return (dev->pdev != NULL);
  157. +}
  158. +
  159. +static inline struct gpiommc_configfs_device *ci_to_gpiommc(struct config_item *item)
  160. +{
  161. + return item ? container_of(item, struct gpiommc_configfs_device, item) : NULL;
  162. +}
  163. +
  164. +static struct configfs_attribute gpiommc_attr_DI = {
  165. + .ca_owner = THIS_MODULE,
  166. + .ca_name = "gpio_data_in",
  167. + .ca_mode = S_IRUGO | S_IWUSR,
  168. +};
  169. +
  170. +static struct configfs_attribute gpiommc_attr_DO = {
  171. + .ca_owner = THIS_MODULE,
  172. + .ca_name = "gpio_data_out",
  173. + .ca_mode = S_IRUGO | S_IWUSR,
  174. +};
  175. +
  176. +static struct configfs_attribute gpiommc_attr_CLK = {
  177. + .ca_owner = THIS_MODULE,
  178. + .ca_name = "gpio_clock",
  179. + .ca_mode = S_IRUGO | S_IWUSR,
  180. +};
  181. +
  182. +static struct configfs_attribute gpiommc_attr_CS = {
  183. + .ca_owner = THIS_MODULE,
  184. + .ca_name = "gpio_chipselect",
  185. + .ca_mode = S_IRUGO | S_IWUSR,
  186. +};
  187. +
  188. +static struct configfs_attribute gpiommc_attr_CS_activelow = {
  189. + .ca_owner = THIS_MODULE,
  190. + .ca_name = "gpio_chipselect_activelow",
  191. + .ca_mode = S_IRUGO | S_IWUSR,
  192. +};
  193. +
  194. +static struct configfs_attribute gpiommc_attr_spimode = {
  195. + .ca_owner = THIS_MODULE,
  196. + .ca_name = "spi_mode",
  197. + .ca_mode = S_IRUGO | S_IWUSR,
  198. +};
  199. +
  200. +static struct configfs_attribute gpiommc_attr_spidelay = {
  201. + .ca_owner = THIS_MODULE,
  202. + .ca_name = "spi_delay",
  203. + .ca_mode = S_IRUGO | S_IWUSR,
  204. +};
  205. +
  206. +static struct configfs_attribute gpiommc_attr_max_bus_speed = {
  207. + .ca_owner = THIS_MODULE,
  208. + .ca_name = "max_bus_speed",
  209. + .ca_mode = S_IRUGO | S_IWUSR,
  210. +};
  211. +
  212. +static struct configfs_attribute gpiommc_attr_register = {
  213. + .ca_owner = THIS_MODULE,
  214. + .ca_name = "register",
  215. + .ca_mode = S_IRUGO | S_IWUSR,
  216. +};
  217. +
  218. +static struct configfs_attribute *gpiommc_config_attrs[] = {
  219. + &gpiommc_attr_DI,
  220. + &gpiommc_attr_DO,
  221. + &gpiommc_attr_CLK,
  222. + &gpiommc_attr_CS,
  223. + &gpiommc_attr_CS_activelow,
  224. + &gpiommc_attr_spimode,
  225. + &gpiommc_attr_spidelay,
  226. + &gpiommc_attr_max_bus_speed,
  227. + &gpiommc_attr_register,
  228. + NULL,
  229. +};
  230. +
  231. +static ssize_t gpiommc_config_attr_show(struct config_item *item,
  232. + struct configfs_attribute *attr,
  233. + char *page)
  234. +{
  235. + struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
  236. + ssize_t count = 0;
  237. + unsigned int gpio;
  238. + int err = 0;
  239. +
  240. + if (attr == &gpiommc_attr_DI) {
  241. + gpio = dev->pdata.pins.gpio_di;
  242. + if (gpio == GPIO_INVALID)
  243. + count = snprintf(page, PAGE_SIZE, "not configured\n");
  244. + else
  245. + count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
  246. + goto out;
  247. + }
  248. + if (attr == &gpiommc_attr_DO) {
  249. + gpio = dev->pdata.pins.gpio_do;
  250. + if (gpio == GPIO_INVALID)
  251. + count = snprintf(page, PAGE_SIZE, "not configured\n");
  252. + else
  253. + count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
  254. + goto out;
  255. + }
  256. + if (attr == &gpiommc_attr_CLK) {
  257. + gpio = dev->pdata.pins.gpio_clk;
  258. + if (gpio == GPIO_INVALID)
  259. + count = snprintf(page, PAGE_SIZE, "not configured\n");
  260. + else
  261. + count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
  262. + goto out;
  263. + }
  264. + if (attr == &gpiommc_attr_CS) {
  265. + gpio = dev->pdata.pins.gpio_cs;
  266. + if (gpio == GPIO_INVALID)
  267. + count = snprintf(page, PAGE_SIZE, "not configured\n");
  268. + else
  269. + count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
  270. + goto out;
  271. + }
  272. + if (attr == &gpiommc_attr_CS_activelow) {
  273. + count = snprintf(page, PAGE_SIZE, "%u\n",
  274. + dev->pdata.pins.cs_activelow);
  275. + goto out;
  276. + }
  277. + if (attr == &gpiommc_attr_spimode) {
  278. + count = snprintf(page, PAGE_SIZE, "%u\n",
  279. + dev->pdata.mode);
  280. + goto out;
  281. + }
  282. + if (attr == &gpiommc_attr_spidelay) {
  283. + count = snprintf(page, PAGE_SIZE, "%u\n",
  284. + !dev->pdata.no_spi_delay);
  285. + goto out;
  286. + }
  287. + if (attr == &gpiommc_attr_max_bus_speed) {
  288. + count = snprintf(page, PAGE_SIZE, "%u\n",
  289. + dev->pdata.max_bus_speed);
  290. + goto out;
  291. + }
  292. + if (attr == &gpiommc_attr_register) {
  293. + count = snprintf(page, PAGE_SIZE, "%u\n",
  294. + gpiommc_is_registered(dev));
  295. + goto out;
  296. + }
  297. + WARN_ON(1);
  298. + err = -ENOSYS;
  299. +out:
  300. + return err ? err : count;
  301. +}
  302. +
  303. +static int gpiommc_do_register(struct gpiommc_configfs_device *dev,
  304. + const char *name)
  305. +{
  306. + int err;
  307. +
  308. + if (gpiommc_is_registered(dev))
  309. + return 0;
  310. +
  311. + if (!gpio_is_valid(dev->pdata.pins.gpio_di) ||
  312. + !gpio_is_valid(dev->pdata.pins.gpio_do) ||
  313. + !gpio_is_valid(dev->pdata.pins.gpio_clk) ||
  314. + !gpio_is_valid(dev->pdata.pins.gpio_cs)) {
  315. + printk(KERN_ERR PFX
  316. + "configfs: Invalid GPIO pin number(s)\n");
  317. + return -EINVAL;
  318. + }
  319. +
  320. + strlcpy(dev->pdata.name, name,
  321. + sizeof(dev->pdata.name));
  322. +
  323. + dev->pdev = platform_device_alloc(GPIOMMC_PLATDEV_NAME,
  324. + gpiommc_next_id());
  325. + if (!dev->pdev)
  326. + return -ENOMEM;
  327. + err = platform_device_add_data(dev->pdev, &dev->pdata,
  328. + sizeof(dev->pdata));
  329. + if (err) {
  330. + platform_device_put(dev->pdev);
  331. + return err;
  332. + }
  333. + err = platform_device_add(dev->pdev);
  334. + if (err) {
  335. + platform_device_put(dev->pdev);
  336. + return err;
  337. + }
  338. +
  339. + return 0;
  340. +}
  341. +
  342. +static void gpiommc_do_unregister(struct gpiommc_configfs_device *dev)
  343. +{
  344. + if (!gpiommc_is_registered(dev))
  345. + return;
  346. +
  347. + platform_device_unregister(dev->pdev);
  348. + dev->pdev = NULL;
  349. +}
  350. +
  351. +static ssize_t gpiommc_config_attr_store(struct config_item *item,
  352. + struct configfs_attribute *attr,
  353. + const char *page, size_t count)
  354. +{
  355. + struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
  356. + int err = -EINVAL;
  357. + unsigned long data;
  358. +
  359. + if (attr == &gpiommc_attr_register) {
  360. + err = kstrtoul(page, 10, &data);
  361. + if (err)
  362. + goto out;
  363. + err = -EINVAL;
  364. + if (data == 1)
  365. + err = gpiommc_do_register(dev, item->ci_name);
  366. + if (data == 0) {
  367. + gpiommc_do_unregister(dev);
  368. + err = 0;
  369. + }
  370. + goto out;
  371. + }
  372. +
  373. + if (gpiommc_is_registered(dev)) {
  374. + /* The rest of the config parameters can only be set
  375. + * as long as the device is not registered, yet. */
  376. + err = -EBUSY;
  377. + goto out;
  378. + }
  379. +
  380. + if (attr == &gpiommc_attr_DI) {
  381. + err = kstrtoul(page, 10, &data);
  382. + if (err)
  383. + goto out;
  384. + err = -EINVAL;
  385. + if (!gpio_is_valid(data))
  386. + goto out;
  387. + dev->pdata.pins.gpio_di = data;
  388. + err = 0;
  389. + goto out;
  390. + }
  391. + if (attr == &gpiommc_attr_DO) {
  392. + err = kstrtoul(page, 10, &data);
  393. + if (err)
  394. + goto out;
  395. + err = -EINVAL;
  396. + if (!gpio_is_valid(data))
  397. + goto out;
  398. + dev->pdata.pins.gpio_do = data;
  399. + err = 0;
  400. + goto out;
  401. + }
  402. + if (attr == &gpiommc_attr_CLK) {
  403. + err = kstrtoul(page, 10, &data);
  404. + if (err)
  405. + goto out;
  406. + err = -EINVAL;
  407. + if (!gpio_is_valid(data))
  408. + goto out;
  409. + dev->pdata.pins.gpio_clk = data;
  410. + err = 0;
  411. + goto out;
  412. + }
  413. + if (attr == &gpiommc_attr_CS) {
  414. + err = kstrtoul(page, 10, &data);
  415. + if (err)
  416. + goto out;
  417. + err = -EINVAL;
  418. + if (!gpio_is_valid(data))
  419. + goto out;
  420. + dev->pdata.pins.gpio_cs = data;
  421. + err = 0;
  422. + goto out;
  423. + }
  424. + if (attr == &gpiommc_attr_CS_activelow) {
  425. + err = kstrtoul(page, 10, &data);
  426. + if (err)
  427. + goto out;
  428. + err = -EINVAL;
  429. + if (data != 0 && data != 1)
  430. + goto out;
  431. + dev->pdata.pins.cs_activelow = data;
  432. + err = 0;
  433. + goto out;
  434. + }
  435. + if (attr == &gpiommc_attr_spimode) {
  436. + err = kstrtoul(page, 10, &data);
  437. + if (err)
  438. + goto out;
  439. + err = -EINVAL;
  440. + switch (data) {
  441. + case 0:
  442. + dev->pdata.mode = SPI_MODE_0;
  443. + break;
  444. + case 1:
  445. + dev->pdata.mode = SPI_MODE_1;
  446. + break;
  447. + case 2:
  448. + dev->pdata.mode = SPI_MODE_2;
  449. + break;
  450. + case 3:
  451. + dev->pdata.mode = SPI_MODE_3;
  452. + break;
  453. + default:
  454. + goto out;
  455. + }
  456. + err = 0;
  457. + goto out;
  458. + }
  459. + if (attr == &gpiommc_attr_spidelay) {
  460. + err = kstrtoul(page, 10, &data);
  461. + if (err)
  462. + goto out;
  463. + err = -EINVAL;
  464. + if (data != 0 && data != 1)
  465. + goto out;
  466. + dev->pdata.no_spi_delay = !data;
  467. + err = 0;
  468. + goto out;
  469. + }
  470. + if (attr == &gpiommc_attr_max_bus_speed) {
  471. + err = kstrtoul(page, 10, &data);
  472. + if (err)
  473. + goto out;
  474. + err = -EINVAL;
  475. + if (data > UINT_MAX)
  476. + goto out;
  477. + dev->pdata.max_bus_speed = data;
  478. + err = 0;
  479. + goto out;
  480. + }
  481. + WARN_ON(1);
  482. + err = -ENOSYS;
  483. +out:
  484. + return err ? err : count;
  485. +}
  486. +
  487. +static void gpiommc_config_item_release(struct config_item *item)
  488. +{
  489. + struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
  490. +
  491. + kfree(dev);
  492. +}
  493. +
  494. +static struct configfs_item_operations gpiommc_config_item_ops = {
  495. + .release = gpiommc_config_item_release,
  496. + .show_attribute = gpiommc_config_attr_show,
  497. + .store_attribute = gpiommc_config_attr_store,
  498. +};
  499. +
  500. +static struct config_item_type gpiommc_dev_ci_type = {
  501. + .ct_item_ops = &gpiommc_config_item_ops,
  502. + .ct_attrs = gpiommc_config_attrs,
  503. + .ct_owner = THIS_MODULE,
  504. +};
  505. +
  506. +static struct config_item *gpiommc_make_item(struct config_group *group,
  507. + const char *name)
  508. +{
  509. + struct gpiommc_configfs_device *dev;
  510. +
  511. + if (strlen(name) > GPIOMMC_MAX_NAMELEN) {
  512. + printk(KERN_ERR PFX "configfs: device name too long\n");
  513. + return NULL;
  514. + }
  515. +
  516. + dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  517. + if (!dev)
  518. + return NULL;
  519. +
  520. + config_item_init_type_name(&dev->item, name,
  521. + &gpiommc_dev_ci_type);
  522. +
  523. + /* Assign default configuration */
  524. + dev->pdata.pins.gpio_di = GPIO_INVALID;
  525. + dev->pdata.pins.gpio_do = GPIO_INVALID;
  526. + dev->pdata.pins.gpio_clk = GPIO_INVALID;
  527. + dev->pdata.pins.gpio_cs = GPIO_INVALID;
  528. + dev->pdata.pins.cs_activelow = 1;
  529. + dev->pdata.mode = SPI_MODE_0;
  530. + dev->pdata.no_spi_delay = 0;
  531. + dev->pdata.max_bus_speed = 5000000; /* 5 MHz */
  532. +
  533. + return &(dev->item);
  534. +}
  535. +
  536. +static void gpiommc_drop_item(struct config_group *group,
  537. + struct config_item *item)
  538. +{
  539. + struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
  540. +
  541. + gpiommc_do_unregister(dev);
  542. + kfree(dev);
  543. +}
  544. +
  545. +static struct configfs_group_operations gpiommc_ct_group_ops = {
  546. + .make_item = gpiommc_make_item,
  547. + .drop_item = gpiommc_drop_item,
  548. +};
  549. +
  550. +static struct config_item_type gpiommc_ci_type = {
  551. + .ct_group_ops = &gpiommc_ct_group_ops,
  552. + .ct_owner = THIS_MODULE,
  553. +};
  554. +
  555. +static struct configfs_subsystem gpiommc_subsys = {
  556. + .su_group = {
  557. + .cg_item = {
  558. + .ci_namebuf = GPIOMMC_PLATDEV_NAME,
  559. + .ci_type = &gpiommc_ci_type,
  560. + },
  561. + },
  562. + .su_mutex = __MUTEX_INITIALIZER(gpiommc_subsys.su_mutex),
  563. +};
  564. +
  565. +#endif /* CONFIG_GPIOMMC_CONFIGFS */
  566. +
  567. +static struct platform_driver gpiommc_plat_driver = {
  568. + .probe = gpiommc_probe,
  569. + .remove = gpiommc_remove,
  570. + .driver = {
  571. + .name = GPIOMMC_PLATDEV_NAME,
  572. + .owner = THIS_MODULE,
  573. + },
  574. +};
  575. +
  576. +int gpiommc_next_id(void)
  577. +{
  578. + static atomic_t counter = ATOMIC_INIT(-1);
  579. +
  580. + return atomic_inc_return(&counter);
  581. +}
  582. +EXPORT_SYMBOL(gpiommc_next_id);
  583. +
  584. +static int __init gpiommc_modinit(void)
  585. +{
  586. + int err;
  587. +
  588. + err = platform_driver_register(&gpiommc_plat_driver);
  589. + if (err)
  590. + return err;
  591. +
  592. +#ifdef CONFIG_GPIOMMC_CONFIGFS
  593. + config_group_init(&gpiommc_subsys.su_group);
  594. + err = configfs_register_subsystem(&gpiommc_subsys);
  595. + if (err) {
  596. + platform_driver_unregister(&gpiommc_plat_driver);
  597. + return err;
  598. + }
  599. +#endif /* CONFIG_GPIOMMC_CONFIGFS */
  600. +
  601. + return 0;
  602. +}
  603. +module_init(gpiommc_modinit);
  604. +
  605. +static void __exit gpiommc_modexit(void)
  606. +{
  607. +#ifdef CONFIG_GPIOMMC_CONFIGFS
  608. + configfs_unregister_subsystem(&gpiommc_subsys);
  609. +#endif
  610. + platform_driver_unregister(&gpiommc_plat_driver);
  611. +}
  612. +module_exit(gpiommc_modexit);
  613. --- a/drivers/mmc/host/Kconfig
  614. +++ b/drivers/mmc/host/Kconfig
  615. @@ -556,6 +556,31 @@ config MMC_SDHI
  616. This provides support for the SDHI SD/SDIO controller found in
  617. SuperH and ARM SH-Mobile SoCs
  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. @@ -40,6 +40,7 @@ tmio_mmc_core-$(subst m,y,$(CONFIG_MMC_S
  649. obj-$(CONFIG_MMC_SDHI) += sh_mobile_sdhi.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. obj-$(CONFIG_SDH_BFIN) += bfin_sdh.o
  654. obj-$(CONFIG_MMC_DW) += dw_mmc.o
  655. obj-$(CONFIG_MMC_DW_PLTFM) += dw_mmc-pltfm.o
  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. @@ -4541,6 +4541,11 @@ T: git git://linuxtv.org/anttip/media_tr
  833. S: Maintained
  834. F: drivers/media/usb/hackrf/
  835. +GPIOMMC DRIVER
  836. +P: Michael Buesch
  837. +M: mb@bu3sch.de
  838. +S: Maintained
  839. +
  840. HARDWARE MONITORING
  841. M: Jean Delvare <jdelvare@suse.de>
  842. M: Guenter Roeck <linux@roeck-us.net>