mmc.c 5.2 KB

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