mmc-twl4030.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * linux/arch/arm/mach-omap2/mmc-twl4030.c
  3. *
  4. * Copyright (C) 2007-2008 Texas Instruments
  5. * Copyright (C) 2008 Nokia Corporation
  6. * Author: Texas Instruments
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/delay.h>
  18. #include <linux/gpio.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <mach/hardware.h>
  22. #include <mach/control.h>
  23. #include <mach/mmc.h>
  24. #include <mach/board.h>
  25. #include "mmc-twl4030.h"
  26. #if defined(CONFIG_REGULATOR) && \
  27. (defined(CONFIG_MMC_OMAP_HS) || defined(CONFIG_MMC_OMAP_HS_MODULE))
  28. static u16 control_pbias_offset;
  29. static u16 control_devconf1_offset;
  30. #define HSMMC_NAME_LEN 9
  31. static struct twl_mmc_controller {
  32. struct omap_mmc_platform_data *mmc;
  33. /* Vcc == configured supply
  34. * Vcc_alt == optional
  35. * - MMC1, supply for DAT4..DAT7
  36. * - MMC2/MMC2, external level shifter voltage supply, for
  37. * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
  38. */
  39. struct regulator *vcc;
  40. struct regulator *vcc_aux;
  41. char name[HSMMC_NAME_LEN + 1];
  42. } hsmmc[OMAP34XX_NR_MMC];
  43. static int twl_mmc_card_detect(int irq)
  44. {
  45. unsigned i;
  46. for (i = 0; i < ARRAY_SIZE(hsmmc); i++) {
  47. struct omap_mmc_platform_data *mmc;
  48. mmc = hsmmc[i].mmc;
  49. if (!mmc)
  50. continue;
  51. if (irq != mmc->slots[0].card_detect_irq)
  52. continue;
  53. /* NOTE: assumes card detect signal is active-low */
  54. return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
  55. }
  56. return -ENOSYS;
  57. }
  58. static int twl_mmc_get_ro(struct device *dev, int slot)
  59. {
  60. struct omap_mmc_platform_data *mmc = dev->platform_data;
  61. /* NOTE: assumes write protect signal is active-high */
  62. return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
  63. }
  64. static int twl_mmc_get_cover_state(struct device *dev, int slot)
  65. {
  66. struct omap_mmc_platform_data *mmc = dev->platform_data;
  67. /* NOTE: assumes card detect signal is active-low */
  68. return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
  69. }
  70. /*
  71. * MMC Slot Initialization.
  72. */
  73. static int twl_mmc_late_init(struct device *dev)
  74. {
  75. struct omap_mmc_platform_data *mmc = dev->platform_data;
  76. int ret = 0;
  77. int i;
  78. /* MMC/SD/SDIO doesn't require a card detect switch */
  79. if (gpio_is_valid(mmc->slots[0].switch_pin)) {
  80. ret = gpio_request(mmc->slots[0].switch_pin, "mmc_cd");
  81. if (ret)
  82. goto done;
  83. ret = gpio_direction_input(mmc->slots[0].switch_pin);
  84. if (ret)
  85. goto err;
  86. }
  87. /* require at least main regulator */
  88. for (i = 0; i < ARRAY_SIZE(hsmmc); i++) {
  89. if (hsmmc[i].name == mmc->slots[0].name) {
  90. struct regulator *reg;
  91. hsmmc[i].mmc = mmc;
  92. reg = regulator_get(dev, "vmmc");
  93. if (IS_ERR(reg)) {
  94. dev_dbg(dev, "vmmc regulator missing\n");
  95. /* HACK: until fixed.c regulator is usable,
  96. * we don't require a main regulator
  97. * for MMC2 or MMC3
  98. */
  99. if (i != 0)
  100. break;
  101. ret = PTR_ERR(reg);
  102. goto err;
  103. }
  104. hsmmc[i].vcc = reg;
  105. mmc->slots[0].ocr_mask = mmc_regulator_get_ocrmask(reg);
  106. /* allow an aux regulator */
  107. reg = regulator_get(dev, "vmmc_aux");
  108. hsmmc[i].vcc_aux = IS_ERR(reg) ? NULL : reg;
  109. /* UGLY HACK: workaround regulator framework bugs.
  110. * When the bootloader leaves a supply active, it's
  111. * initialized with zero usecount ... and we can't
  112. * disable it without first enabling it. Until the
  113. * framework is fixed, we need a workaround like this
  114. * (which is safe for MMC, but not in general).
  115. */
  116. if (regulator_is_enabled(hsmmc[i].vcc) > 0) {
  117. regulator_enable(hsmmc[i].vcc);
  118. regulator_disable(hsmmc[i].vcc);
  119. }
  120. if (hsmmc[i].vcc_aux) {
  121. if (regulator_is_enabled(reg) > 0) {
  122. regulator_enable(reg);
  123. regulator_disable(reg);
  124. }
  125. }
  126. break;
  127. }
  128. }
  129. return 0;
  130. err:
  131. gpio_free(mmc->slots[0].switch_pin);
  132. done:
  133. mmc->slots[0].card_detect_irq = 0;
  134. mmc->slots[0].card_detect = NULL;
  135. dev_err(dev, "err %d configuring card detect\n", ret);
  136. return ret;
  137. }
  138. static void twl_mmc_cleanup(struct device *dev)
  139. {
  140. struct omap_mmc_platform_data *mmc = dev->platform_data;
  141. gpio_free(mmc->slots[0].switch_pin);
  142. }
  143. #ifdef CONFIG_PM
  144. static int twl_mmc_suspend(struct device *dev, int slot)
  145. {
  146. struct omap_mmc_platform_data *mmc = dev->platform_data;
  147. disable_irq(mmc->slots[0].card_detect_irq);
  148. return 0;
  149. }
  150. static int twl_mmc_resume(struct device *dev, int slot)
  151. {
  152. struct omap_mmc_platform_data *mmc = dev->platform_data;
  153. enable_irq(mmc->slots[0].card_detect_irq);
  154. return 0;
  155. }
  156. #else
  157. #define twl_mmc_suspend NULL
  158. #define twl_mmc_resume NULL
  159. #endif
  160. static int twl_mmc1_set_power(struct device *dev, int slot, int power_on,
  161. int vdd)
  162. {
  163. u32 reg;
  164. int ret = 0;
  165. struct twl_mmc_controller *c = &hsmmc[0];
  166. struct omap_mmc_platform_data *mmc = dev->platform_data;
  167. /*
  168. * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
  169. * card with Vcc regulator (from twl4030 or whatever). OMAP has both
  170. * 1.8V and 3.0V modes, controlled by the PBIAS register.
  171. *
  172. * In 8-bit modes, OMAP VMMC1A (for DAT4..7) needs a supply, which
  173. * is most naturally TWL VSIM; those pins also use PBIAS.
  174. *
  175. * FIXME handle VMMC1A as needed ...
  176. */
  177. if (power_on) {
  178. if (cpu_is_omap2430()) {
  179. reg = omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1);
  180. if ((1 << vdd) >= MMC_VDD_30_31)
  181. reg |= OMAP243X_MMC1_ACTIVE_OVERWRITE;
  182. else
  183. reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
  184. omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
  185. }
  186. if (mmc->slots[0].internal_clock) {
  187. reg = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0);
  188. reg |= OMAP2_MMCSDIO1ADPCLKISEL;
  189. omap_ctrl_writel(reg, OMAP2_CONTROL_DEVCONF0);
  190. }
  191. reg = omap_ctrl_readl(control_pbias_offset);
  192. reg |= OMAP2_PBIASSPEEDCTRL0;
  193. reg &= ~OMAP2_PBIASLITEPWRDNZ0;
  194. omap_ctrl_writel(reg, control_pbias_offset);
  195. ret = mmc_regulator_set_ocr(c->vcc, vdd);
  196. /* 100ms delay required for PBIAS configuration */
  197. msleep(100);
  198. reg = omap_ctrl_readl(control_pbias_offset);
  199. reg |= (OMAP2_PBIASLITEPWRDNZ0 | OMAP2_PBIASSPEEDCTRL0);
  200. if ((1 << vdd) <= MMC_VDD_165_195)
  201. reg &= ~OMAP2_PBIASLITEVMODE0;
  202. else
  203. reg |= OMAP2_PBIASLITEVMODE0;
  204. omap_ctrl_writel(reg, control_pbias_offset);
  205. } else {
  206. reg = omap_ctrl_readl(control_pbias_offset);
  207. reg &= ~OMAP2_PBIASLITEPWRDNZ0;
  208. omap_ctrl_writel(reg, control_pbias_offset);
  209. ret = mmc_regulator_set_ocr(c->vcc, 0);
  210. /* 100ms delay required for PBIAS configuration */
  211. msleep(100);
  212. reg = omap_ctrl_readl(control_pbias_offset);
  213. reg |= (OMAP2_PBIASSPEEDCTRL0 | OMAP2_PBIASLITEPWRDNZ0 |
  214. OMAP2_PBIASLITEVMODE0);
  215. omap_ctrl_writel(reg, control_pbias_offset);
  216. }
  217. return ret;
  218. }
  219. static int twl_mmc23_set_power(struct device *dev, int slot, int power_on, int vdd)
  220. {
  221. int ret = 0;
  222. struct twl_mmc_controller *c = NULL;
  223. struct omap_mmc_platform_data *mmc = dev->platform_data;
  224. int i;
  225. for (i = 1; i < ARRAY_SIZE(hsmmc); i++) {
  226. if (mmc == hsmmc[i].mmc) {
  227. c = &hsmmc[i];
  228. break;
  229. }
  230. }
  231. if (c == NULL)
  232. return -ENODEV;
  233. /* If we don't see a Vcc regulator, assume it's a fixed
  234. * voltage always-on regulator.
  235. */
  236. if (!c->vcc)
  237. return 0;
  238. /*
  239. * Assume Vcc regulator is used only to power the card ... OMAP
  240. * VDDS is used to power the pins, optionally with a transceiver to
  241. * support cards using voltages other than VDDS (1.8V nominal). When a
  242. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  243. *
  244. * In some cases this regulator won't support enable/disable;
  245. * e.g. it's a fixed rail for a WLAN chip.
  246. *
  247. * In other cases vcc_aux switches interface power. Example, for
  248. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  249. * chips/cards need an interface voltage rail too.
  250. */
  251. if (power_on) {
  252. /* only MMC2 supports a CLKIN */
  253. if (mmc->slots[0].internal_clock) {
  254. u32 reg;
  255. reg = omap_ctrl_readl(control_devconf1_offset);
  256. reg |= OMAP2_MMCSDIO2ADPCLKISEL;
  257. omap_ctrl_writel(reg, control_devconf1_offset);
  258. }
  259. ret = mmc_regulator_set_ocr(c->vcc, vdd);
  260. /* enable interface voltage rail, if needed */
  261. if (ret == 0 && c->vcc_aux) {
  262. ret = regulator_enable(c->vcc_aux);
  263. if (ret < 0)
  264. ret = mmc_regulator_set_ocr(c->vcc, 0);
  265. }
  266. } else {
  267. if (c->vcc_aux && (ret = regulator_is_enabled(c->vcc_aux)) > 0)
  268. ret = regulator_disable(c->vcc_aux);
  269. if (ret == 0)
  270. ret = mmc_regulator_set_ocr(c->vcc, 0);
  271. }
  272. return ret;
  273. }
  274. static struct omap_mmc_platform_data *hsmmc_data[OMAP34XX_NR_MMC] __initdata;
  275. void __init twl4030_mmc_init(struct twl4030_hsmmc_info *controllers)
  276. {
  277. struct twl4030_hsmmc_info *c;
  278. int nr_hsmmc = ARRAY_SIZE(hsmmc_data);
  279. if (cpu_is_omap2430()) {
  280. control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
  281. control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
  282. nr_hsmmc = 2;
  283. } else {
  284. control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
  285. control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
  286. }
  287. for (c = controllers; c->mmc; c++) {
  288. struct twl_mmc_controller *twl = hsmmc + c->mmc - 1;
  289. struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
  290. if (!c->mmc || c->mmc > nr_hsmmc) {
  291. pr_debug("MMC%d: no such controller\n", c->mmc);
  292. continue;
  293. }
  294. if (mmc) {
  295. pr_debug("MMC%d: already configured\n", c->mmc);
  296. continue;
  297. }
  298. mmc = kzalloc(sizeof(struct omap_mmc_platform_data), GFP_KERNEL);
  299. if (!mmc) {
  300. pr_err("Cannot allocate memory for mmc device!\n");
  301. return;
  302. }
  303. if (c->name)
  304. strncpy(twl->name, c->name, HSMMC_NAME_LEN);
  305. else
  306. snprintf(twl->name, ARRAY_SIZE(twl->name),
  307. "mmc%islot%i", c->mmc, 1);
  308. mmc->slots[0].name = twl->name;
  309. mmc->nr_slots = 1;
  310. mmc->slots[0].wires = c->wires;
  311. mmc->slots[0].internal_clock = !c->ext_clock;
  312. mmc->dma_mask = 0xffffffff;
  313. mmc->init = twl_mmc_late_init;
  314. /* note: twl4030 card detect GPIOs can disable VMMCx ... */
  315. if (gpio_is_valid(c->gpio_cd)) {
  316. mmc->cleanup = twl_mmc_cleanup;
  317. mmc->suspend = twl_mmc_suspend;
  318. mmc->resume = twl_mmc_resume;
  319. mmc->slots[0].switch_pin = c->gpio_cd;
  320. mmc->slots[0].card_detect_irq = gpio_to_irq(c->gpio_cd);
  321. if (c->cover_only)
  322. mmc->slots[0].get_cover_state = twl_mmc_get_cover_state;
  323. else
  324. mmc->slots[0].card_detect = twl_mmc_card_detect;
  325. } else
  326. mmc->slots[0].switch_pin = -EINVAL;
  327. /* write protect normally uses an OMAP gpio */
  328. if (gpio_is_valid(c->gpio_wp)) {
  329. gpio_request(c->gpio_wp, "mmc_wp");
  330. gpio_direction_input(c->gpio_wp);
  331. mmc->slots[0].gpio_wp = c->gpio_wp;
  332. mmc->slots[0].get_ro = twl_mmc_get_ro;
  333. } else
  334. mmc->slots[0].gpio_wp = -EINVAL;
  335. /* NOTE: MMC slots should have a Vcc regulator set up.
  336. * This may be from a TWL4030-family chip, another
  337. * controllable regulator, or a fixed supply.
  338. *
  339. * temporary HACK: ocr_mask instead of fixed supply
  340. */
  341. mmc->slots[0].ocr_mask = c->ocr_mask;
  342. switch (c->mmc) {
  343. case 1:
  344. /* on-chip level shifting via PBIAS0/PBIAS1 */
  345. mmc->slots[0].set_power = twl_mmc1_set_power;
  346. break;
  347. case 2:
  348. if (c->ext_clock)
  349. c->transceiver = 1;
  350. if (c->transceiver && c->wires > 4)
  351. c->wires = 4;
  352. /* FALLTHROUGH */
  353. case 3:
  354. /* off-chip level shifting, or none */
  355. mmc->slots[0].set_power = twl_mmc23_set_power;
  356. break;
  357. default:
  358. pr_err("MMC%d configuration not supported!\n", c->mmc);
  359. kfree(mmc);
  360. continue;
  361. }
  362. hsmmc_data[c->mmc - 1] = mmc;
  363. }
  364. omap2_init_mmc(hsmmc_data, OMAP34XX_NR_MMC);
  365. /* pass the device nodes back to board setup code */
  366. for (c = controllers; c->mmc; c++) {
  367. struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
  368. if (!c->mmc || c->mmc > nr_hsmmc)
  369. continue;
  370. c->dev = mmc->dev;
  371. }
  372. }
  373. #endif