mmc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. *
  3. * arch/arm/mach-u300/mmc.c
  4. *
  5. *
  6. * Copyright (C) 2009 ST-Ericsson AB
  7. * License terms: GNU General Public License (GPL) version 2
  8. *
  9. * Author: Linus Walleij <linus.walleij@stericsson.com>
  10. * Author: Johan Lundin <johan.lundin@stericsson.com>
  11. * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
  12. */
  13. #include <linux/device.h>
  14. #include <linux/amba/bus.h>
  15. #include <linux/mmc/host.h>
  16. #include <linux/input.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/delay.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/gpio.h>
  22. #include <asm/mach/mmc.h>
  23. #include "mmc.h"
  24. struct mmci_card_event {
  25. struct input_dev *mmc_input;
  26. int mmc_inserted;
  27. struct work_struct workq;
  28. struct mmc_platform_data mmc0_plat_data;
  29. };
  30. static unsigned int mmc_status(struct device *dev)
  31. {
  32. struct mmci_card_event *mmci_card = container_of(
  33. dev->platform_data,
  34. struct mmci_card_event, mmc0_plat_data);
  35. return mmci_card->mmc_inserted;
  36. }
  37. /*
  38. * Here follows a large chunk of code which will only be enabled if you
  39. * have both the AB3100 chip mounted and the MMC subsystem activated.
  40. */
  41. static u32 mmc_translate_vdd(struct device *dev, unsigned int voltage)
  42. {
  43. int v;
  44. /*
  45. * MMC Spec:
  46. * bit 7: 1.70 - 1.95V
  47. * bit 8 - 14: 2.0 - 2.6V
  48. * bit 15 - 23: 2.7 - 3.6V
  49. *
  50. * ab3100 voltages:
  51. * 000 - 2.85V
  52. * 001 - 2.75V
  53. * 010 - 1.8V
  54. * 011 - 1.5V
  55. */
  56. switch (voltage) {
  57. case 8:
  58. v = 3;
  59. break;
  60. case 9:
  61. case 10:
  62. case 11:
  63. case 12:
  64. case 13:
  65. case 14:
  66. case 15:
  67. v = 1;
  68. break;
  69. case 16:
  70. v = 1;
  71. break;
  72. case 17:
  73. case 18:
  74. case 19:
  75. case 20:
  76. case 21:
  77. case 22:
  78. case 23:
  79. case 24:
  80. v = 0;
  81. break;
  82. default:
  83. v = 0;
  84. break;
  85. }
  86. /* PL180 voltage register bits */
  87. return v << 2;
  88. }
  89. static int mmci_callback(void *data)
  90. {
  91. struct mmci_card_event *mmci_card = data;
  92. disable_irq_on_gpio_pin(U300_GPIO_PIN_MMC_CD);
  93. schedule_work(&mmci_card->workq);
  94. return 0;
  95. }
  96. static ssize_t gpio_show(struct device *dev, struct device_attribute *attr,
  97. char *buf)
  98. {
  99. struct mmci_card_event *mmci_card = container_of(
  100. dev->platform_data,
  101. struct mmci_card_event, mmc0_plat_data);
  102. return sprintf(buf, "%d\n", !mmci_card->mmc_inserted);
  103. }
  104. static DEVICE_ATTR(mmc_inserted, S_IRUGO, gpio_show, NULL);
  105. static void _mmci_callback(struct work_struct *ws)
  106. {
  107. struct mmci_card_event *mmci_card = container_of(
  108. ws,
  109. struct mmci_card_event, workq);
  110. mdelay(20);
  111. mmci_card->mmc_inserted = !!gpio_get_value(U300_GPIO_PIN_MMC_CD);
  112. input_report_switch(mmci_card->mmc_input, KEY_INSERT,
  113. !mmci_card->mmc_inserted);
  114. input_sync(mmci_card->mmc_input);
  115. pr_debug("MMC/SD card was %s\n",
  116. mmci_card->mmc_inserted ? "removed" : "inserted");
  117. enable_irq_on_gpio_pin(U300_GPIO_PIN_MMC_CD, !mmci_card->mmc_inserted);
  118. }
  119. int __devinit mmc_init(struct amba_device *adev)
  120. {
  121. struct mmci_card_event *mmci_card;
  122. struct device *mmcsd_device = &adev->dev;
  123. int ret = 0;
  124. mmci_card = kzalloc(sizeof(struct mmci_card_event), GFP_KERNEL);
  125. if (!mmci_card)
  126. return -ENOMEM;
  127. /* Nominally 2.85V on our platform */
  128. mmci_card->mmc0_plat_data.ocr_mask = MMC_VDD_28_29;
  129. mmci_card->mmc0_plat_data.translate_vdd = mmc_translate_vdd;
  130. mmci_card->mmc0_plat_data.status = mmc_status;
  131. mmci_card->mmc0_plat_data.gpio_wp = -1;
  132. mmci_card->mmc0_plat_data.gpio_cd = -1;
  133. mmcsd_device->platform_data = (void *) &mmci_card->mmc0_plat_data;
  134. INIT_WORK(&mmci_card->workq, _mmci_callback);
  135. ret = gpio_request(U300_GPIO_PIN_MMC_CD, "MMC card detection");
  136. if (ret) {
  137. printk(KERN_CRIT "Could not allocate MMC card detection " \
  138. "GPIO pin\n");
  139. goto out;
  140. }
  141. ret = gpio_direction_input(U300_GPIO_PIN_MMC_CD);
  142. if (ret) {
  143. printk(KERN_CRIT "Invalid GPIO pin requested\n");
  144. goto out;
  145. }
  146. ret = sysfs_create_file(&mmcsd_device->kobj,
  147. &dev_attr_mmc_inserted.attr);
  148. if (ret)
  149. goto out;
  150. mmci_card->mmc_input = input_allocate_device();
  151. if (!mmci_card->mmc_input) {
  152. printk(KERN_CRIT "Could not allocate MMC input device\n");
  153. return -ENOMEM;
  154. }
  155. mmci_card->mmc_input->name = "MMC insert notification";
  156. mmci_card->mmc_input->id.bustype = BUS_HOST;
  157. mmci_card->mmc_input->id.vendor = 0;
  158. mmci_card->mmc_input->id.product = 0;
  159. mmci_card->mmc_input->id.version = 0x0100;
  160. mmci_card->mmc_input->dev.parent = mmcsd_device;
  161. input_set_capability(mmci_card->mmc_input, EV_SW, KEY_INSERT);
  162. /*
  163. * Since this must always be compiled into the kernel, this input
  164. * is never unregistered or free:ed.
  165. */
  166. ret = input_register_device(mmci_card->mmc_input);
  167. if (ret) {
  168. input_free_device(mmci_card->mmc_input);
  169. goto out;
  170. }
  171. input_set_drvdata(mmci_card->mmc_input, mmci_card);
  172. ret = gpio_register_callback(U300_GPIO_PIN_MMC_CD, mmci_callback,
  173. mmci_card);
  174. schedule_work(&mmci_card->workq);
  175. printk(KERN_INFO "Registered MMC insert/remove notification\n");
  176. out:
  177. return ret;
  178. }