mmc-twl4030.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. hsmmc[i].vcc = NULL;
  103. goto err;
  104. }
  105. hsmmc[i].vcc = reg;
  106. mmc->slots[0].ocr_mask = mmc_regulator_get_ocrmask(reg);
  107. /* allow an aux regulator */
  108. reg = regulator_get(dev, "vmmc_aux");
  109. hsmmc[i].vcc_aux = IS_ERR(reg) ? NULL : reg;
  110. /* UGLY HACK: workaround regulator framework bugs.
  111. * When the bootloader leaves a supply active, it's
  112. * initialized with zero usecount ... and we can't
  113. * disable it without first enabling it. Until the
  114. * framework is fixed, we need a workaround like this
  115. * (which is safe for MMC, but not in general).
  116. */
  117. if (regulator_is_enabled(hsmmc[i].vcc) > 0) {
  118. regulator_enable(hsmmc[i].vcc);
  119. regulator_disable(hsmmc[i].vcc);
  120. }
  121. if (hsmmc[i].vcc_aux) {
  122. if (regulator_is_enabled(reg) > 0) {
  123. regulator_enable(reg);
  124. regulator_disable(reg);
  125. }
  126. }
  127. break;
  128. }
  129. }
  130. return 0;
  131. err:
  132. gpio_free(mmc->slots[0].switch_pin);
  133. done:
  134. mmc->slots[0].card_detect_irq = 0;
  135. mmc->slots[0].card_detect = NULL;
  136. dev_err(dev, "err %d configuring card detect\n", ret);
  137. return ret;
  138. }
  139. static void twl_mmc_cleanup(struct device *dev)
  140. {
  141. struct omap_mmc_platform_data *mmc = dev->platform_data;
  142. int i;
  143. gpio_free(mmc->slots[0].switch_pin);
  144. for(i = 0; i < ARRAY_SIZE(hsmmc); i++) {
  145. regulator_put(hsmmc[i].vcc);
  146. regulator_put(hsmmc[i].vcc_aux);
  147. }
  148. }
  149. #ifdef CONFIG_PM
  150. static int twl_mmc_suspend(struct device *dev, int slot)
  151. {
  152. struct omap_mmc_platform_data *mmc = dev->platform_data;
  153. disable_irq(mmc->slots[0].card_detect_irq);
  154. return 0;
  155. }
  156. static int twl_mmc_resume(struct device *dev, int slot)
  157. {
  158. struct omap_mmc_platform_data *mmc = dev->platform_data;
  159. enable_irq(mmc->slots[0].card_detect_irq);
  160. return 0;
  161. }
  162. #else
  163. #define twl_mmc_suspend NULL
  164. #define twl_mmc_resume NULL
  165. #endif
  166. static int twl_mmc1_set_power(struct device *dev, int slot, int power_on,
  167. int vdd)
  168. {
  169. u32 reg;
  170. int ret = 0;
  171. struct twl_mmc_controller *c = &hsmmc[0];
  172. struct omap_mmc_platform_data *mmc = dev->platform_data;
  173. /*
  174. * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
  175. * card with Vcc regulator (from twl4030 or whatever). OMAP has both
  176. * 1.8V and 3.0V modes, controlled by the PBIAS register.
  177. *
  178. * In 8-bit modes, OMAP VMMC1A (for DAT4..7) needs a supply, which
  179. * is most naturally TWL VSIM; those pins also use PBIAS.
  180. *
  181. * FIXME handle VMMC1A as needed ...
  182. */
  183. if (power_on) {
  184. if (cpu_is_omap2430()) {
  185. reg = omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1);
  186. if ((1 << vdd) >= MMC_VDD_30_31)
  187. reg |= OMAP243X_MMC1_ACTIVE_OVERWRITE;
  188. else
  189. reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
  190. omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
  191. }
  192. if (mmc->slots[0].internal_clock) {
  193. reg = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0);
  194. reg |= OMAP2_MMCSDIO1ADPCLKISEL;
  195. omap_ctrl_writel(reg, OMAP2_CONTROL_DEVCONF0);
  196. }
  197. reg = omap_ctrl_readl(control_pbias_offset);
  198. reg |= OMAP2_PBIASSPEEDCTRL0;
  199. reg &= ~OMAP2_PBIASLITEPWRDNZ0;
  200. omap_ctrl_writel(reg, control_pbias_offset);
  201. ret = mmc_regulator_set_ocr(c->vcc, vdd);
  202. /* 100ms delay required for PBIAS configuration */
  203. msleep(100);
  204. reg = omap_ctrl_readl(control_pbias_offset);
  205. reg |= (OMAP2_PBIASLITEPWRDNZ0 | OMAP2_PBIASSPEEDCTRL0);
  206. if ((1 << vdd) <= MMC_VDD_165_195)
  207. reg &= ~OMAP2_PBIASLITEVMODE0;
  208. else
  209. reg |= OMAP2_PBIASLITEVMODE0;
  210. omap_ctrl_writel(reg, control_pbias_offset);
  211. } else {
  212. reg = omap_ctrl_readl(control_pbias_offset);
  213. reg &= ~OMAP2_PBIASLITEPWRDNZ0;
  214. omap_ctrl_writel(reg, control_pbias_offset);
  215. ret = mmc_regulator_set_ocr(c->vcc, 0);
  216. /* 100ms delay required for PBIAS configuration */
  217. msleep(100);
  218. reg = omap_ctrl_readl(control_pbias_offset);
  219. reg |= (OMAP2_PBIASSPEEDCTRL0 | OMAP2_PBIASLITEPWRDNZ0 |
  220. OMAP2_PBIASLITEVMODE0);
  221. omap_ctrl_writel(reg, control_pbias_offset);
  222. }
  223. return ret;
  224. }
  225. static int twl_mmc23_set_power(struct device *dev, int slot, int power_on, int vdd)
  226. {
  227. int ret = 0;
  228. struct twl_mmc_controller *c = NULL;
  229. struct omap_mmc_platform_data *mmc = dev->platform_data;
  230. int i;
  231. for (i = 1; i < ARRAY_SIZE(hsmmc); i++) {
  232. if (mmc == hsmmc[i].mmc) {
  233. c = &hsmmc[i];
  234. break;
  235. }
  236. }
  237. if (c == NULL)
  238. return -ENODEV;
  239. /* If we don't see a Vcc regulator, assume it's a fixed
  240. * voltage always-on regulator.
  241. */
  242. if (!c->vcc)
  243. return 0;
  244. /*
  245. * Assume Vcc regulator is used only to power the card ... OMAP
  246. * VDDS is used to power the pins, optionally with a transceiver to
  247. * support cards using voltages other than VDDS (1.8V nominal). When a
  248. * transceiver is used, DAT3..7 are muxed as transceiver control pins.
  249. *
  250. * In some cases this regulator won't support enable/disable;
  251. * e.g. it's a fixed rail for a WLAN chip.
  252. *
  253. * In other cases vcc_aux switches interface power. Example, for
  254. * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
  255. * chips/cards need an interface voltage rail too.
  256. */
  257. if (power_on) {
  258. /* only MMC2 supports a CLKIN */
  259. if (mmc->slots[0].internal_clock) {
  260. u32 reg;
  261. reg = omap_ctrl_readl(control_devconf1_offset);
  262. reg |= OMAP2_MMCSDIO2ADPCLKISEL;
  263. omap_ctrl_writel(reg, control_devconf1_offset);
  264. }
  265. ret = mmc_regulator_set_ocr(c->vcc, vdd);
  266. /* enable interface voltage rail, if needed */
  267. if (ret == 0 && c->vcc_aux) {
  268. ret = regulator_enable(c->vcc_aux);
  269. if (ret < 0)
  270. ret = mmc_regulator_set_ocr(c->vcc, 0);
  271. }
  272. } else {
  273. if (c->vcc_aux && (ret = regulator_is_enabled(c->vcc_aux)) > 0)
  274. ret = regulator_disable(c->vcc_aux);
  275. if (ret == 0)
  276. ret = mmc_regulator_set_ocr(c->vcc, 0);
  277. }
  278. return ret;
  279. }
  280. static struct omap_mmc_platform_data *hsmmc_data[OMAP34XX_NR_MMC] __initdata;
  281. void __init twl4030_mmc_init(struct twl4030_hsmmc_info *controllers)
  282. {
  283. struct twl4030_hsmmc_info *c;
  284. int nr_hsmmc = ARRAY_SIZE(hsmmc_data);
  285. if (cpu_is_omap2430()) {
  286. control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
  287. control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
  288. nr_hsmmc = 2;
  289. } else {
  290. control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
  291. control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
  292. }
  293. for (c = controllers; c->mmc; c++) {
  294. struct twl_mmc_controller *twl = hsmmc + c->mmc - 1;
  295. struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
  296. if (!c->mmc || c->mmc > nr_hsmmc) {
  297. pr_debug("MMC%d: no such controller\n", c->mmc);
  298. continue;
  299. }
  300. if (mmc) {
  301. pr_debug("MMC%d: already configured\n", c->mmc);
  302. continue;
  303. }
  304. mmc = kzalloc(sizeof(struct omap_mmc_platform_data), GFP_KERNEL);
  305. if (!mmc) {
  306. pr_err("Cannot allocate memory for mmc device!\n");
  307. return;
  308. }
  309. if (c->name)
  310. strncpy(twl->name, c->name, HSMMC_NAME_LEN);
  311. else
  312. snprintf(twl->name, ARRAY_SIZE(twl->name),
  313. "mmc%islot%i", c->mmc, 1);
  314. mmc->slots[0].name = twl->name;
  315. mmc->nr_slots = 1;
  316. mmc->slots[0].wires = c->wires;
  317. mmc->slots[0].internal_clock = !c->ext_clock;
  318. mmc->dma_mask = 0xffffffff;
  319. mmc->init = twl_mmc_late_init;
  320. /* note: twl4030 card detect GPIOs can disable VMMCx ... */
  321. if (gpio_is_valid(c->gpio_cd)) {
  322. mmc->cleanup = twl_mmc_cleanup;
  323. mmc->suspend = twl_mmc_suspend;
  324. mmc->resume = twl_mmc_resume;
  325. mmc->slots[0].switch_pin = c->gpio_cd;
  326. mmc->slots[0].card_detect_irq = gpio_to_irq(c->gpio_cd);
  327. if (c->cover_only)
  328. mmc->slots[0].get_cover_state = twl_mmc_get_cover_state;
  329. else
  330. mmc->slots[0].card_detect = twl_mmc_card_detect;
  331. } else
  332. mmc->slots[0].switch_pin = -EINVAL;
  333. /* write protect normally uses an OMAP gpio */
  334. if (gpio_is_valid(c->gpio_wp)) {
  335. gpio_request(c->gpio_wp, "mmc_wp");
  336. gpio_direction_input(c->gpio_wp);
  337. mmc->slots[0].gpio_wp = c->gpio_wp;
  338. mmc->slots[0].get_ro = twl_mmc_get_ro;
  339. } else
  340. mmc->slots[0].gpio_wp = -EINVAL;
  341. /* NOTE: MMC slots should have a Vcc regulator set up.
  342. * This may be from a TWL4030-family chip, another
  343. * controllable regulator, or a fixed supply.
  344. *
  345. * temporary HACK: ocr_mask instead of fixed supply
  346. */
  347. mmc->slots[0].ocr_mask = c->ocr_mask;
  348. switch (c->mmc) {
  349. case 1:
  350. /* on-chip level shifting via PBIAS0/PBIAS1 */
  351. mmc->slots[0].set_power = twl_mmc1_set_power;
  352. break;
  353. case 2:
  354. if (c->ext_clock)
  355. c->transceiver = 1;
  356. if (c->transceiver && c->wires > 4)
  357. c->wires = 4;
  358. /* FALLTHROUGH */
  359. case 3:
  360. /* off-chip level shifting, or none */
  361. mmc->slots[0].set_power = twl_mmc23_set_power;
  362. break;
  363. default:
  364. pr_err("MMC%d configuration not supported!\n", c->mmc);
  365. kfree(mmc);
  366. continue;
  367. }
  368. hsmmc_data[c->mmc - 1] = mmc;
  369. }
  370. omap2_init_mmc(hsmmc_data, OMAP34XX_NR_MMC);
  371. /* pass the device nodes back to board setup code */
  372. for (c = controllers; c->mmc; c++) {
  373. struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
  374. if (!c->mmc || c->mmc > nr_hsmmc)
  375. continue;
  376. c->dev = mmc->dev;
  377. }
  378. }
  379. #endif