2 * linux/arch/arm/mach-omap2/mmc-twl4030.c
4 * Copyright (C) 2007-2008 Texas Instruments
5 * Copyright (C) 2008 Nokia Corporation
6 * Author: Texas Instruments
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.
12 #include <linux/err.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>
22 #include <mach/hardware.h>
23 #include <mach/control.h>
25 #include <mach/board.h>
27 #include "mmc-twl4030.h"
30 #if defined(CONFIG_REGULATOR) && \
31 (defined(CONFIG_MMC_OMAP_HS) || defined(CONFIG_MMC_OMAP_HS_MODULE))
33 static u16 control_pbias_offset;
34 static u16 control_devconf1_offset;
36 #define HSMMC_NAME_LEN 9
38 static struct twl_mmc_controller {
39 struct omap_mmc_platform_data *mmc;
40 /* Vcc == configured supply
42 * - MMC1, supply for DAT4..DAT7
43 * - MMC2/MMC2, external level shifter voltage supply, for
44 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
46 struct regulator *vcc;
47 struct regulator *vcc_aux;
48 char name[HSMMC_NAME_LEN + 1];
49 } hsmmc[OMAP34XX_NR_MMC];
51 static int twl_mmc_card_detect(int irq)
55 for (i = 0; i < ARRAY_SIZE(hsmmc); i++) {
56 struct omap_mmc_platform_data *mmc;
61 if (irq != mmc->slots[0].card_detect_irq)
64 /* NOTE: assumes card detect signal is active-low */
65 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
70 static int twl_mmc_get_ro(struct device *dev, int slot)
72 struct omap_mmc_platform_data *mmc = dev->platform_data;
74 /* NOTE: assumes write protect signal is active-high */
75 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
78 static int twl_mmc_get_cover_state(struct device *dev, int slot)
80 struct omap_mmc_platform_data *mmc = dev->platform_data;
82 /* NOTE: assumes card detect signal is active-low */
83 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
87 * MMC Slot Initialization.
89 static int twl_mmc_late_init(struct device *dev)
91 struct omap_mmc_platform_data *mmc = dev->platform_data;
95 /* MMC/SD/SDIO doesn't require a card detect switch */
96 if (gpio_is_valid(mmc->slots[0].switch_pin)) {
97 ret = gpio_request(mmc->slots[0].switch_pin, "mmc_cd");
100 ret = gpio_direction_input(mmc->slots[0].switch_pin);
105 /* require at least main regulator */
106 for (i = 0; i < ARRAY_SIZE(hsmmc); i++) {
107 if (hsmmc[i].name == mmc->slots[0].name) {
108 struct regulator *reg;
112 reg = regulator_get(dev, "vmmc");
114 dev_dbg(dev, "vmmc regulator missing\n");
115 /* HACK: until fixed.c regulator is usable,
116 * we don't require a main regulator
125 mmc->slots[0].ocr_mask = mmc_regulator_get_ocrmask(reg);
127 /* allow an aux regulator */
128 reg = regulator_get(dev, "vmmc_aux");
129 hsmmc[i].vcc_aux = IS_ERR(reg) ? NULL : reg;
131 /* UGLY HACK: workaround regulator framework bugs.
132 * When the bootloader leaves a supply active, it's
133 * initialized with zero usecount ... and we can't
134 * disable it without first disabling it. Until the
135 * framework is fixed, we need a workaround like this
136 * (which is safe for MMC, but not in general).
138 if (regulator_is_enabled(hsmmc[i].vcc) > 0) {
139 dev_warn(dev, "APPLY REGULATOR HACK for vmmc\n");
140 regulator_enable(hsmmc[i].vcc);
141 regulator_disable(hsmmc[i].vcc);
143 if (hsmmc[i].vcc_aux) {
144 if (regulator_is_enabled(reg) > 0) {
145 dev_warn(dev, "APPLY REGULATOR HACK "
147 regulator_enable(reg);
148 regulator_disable(reg);
159 gpio_free(mmc->slots[0].switch_pin);
161 mmc->slots[0].card_detect_irq = 0;
162 mmc->slots[0].card_detect = NULL;
164 dev_err(dev, "err %d configuring card detect\n", ret);
168 static void twl_mmc_cleanup(struct device *dev)
170 struct omap_mmc_platform_data *mmc = dev->platform_data;
172 gpio_free(mmc->slots[0].switch_pin);
177 static int twl_mmc_suspend(struct device *dev, int slot)
179 struct omap_mmc_platform_data *mmc = dev->platform_data;
181 disable_irq(mmc->slots[0].card_detect_irq);
185 static int twl_mmc_resume(struct device *dev, int slot)
187 struct omap_mmc_platform_data *mmc = dev->platform_data;
189 enable_irq(mmc->slots[0].card_detect_irq);
194 #define twl_mmc_suspend NULL
195 #define twl_mmc_resume NULL
198 static int twl_mmc1_set_power(struct device *dev, int slot, int power_on,
203 struct twl_mmc_controller *c = &hsmmc[0];
204 struct omap_mmc_platform_data *mmc = dev->platform_data;
207 * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
208 * card with Vcc regulator (from twl4030 or whatever). OMAP has both
209 * 1.8V and 3.0V modes, controlled by the PBIAS register.
211 * In 8-bit modes, OMAP VMMC1A (for DAT4..7) needs a supply, which
212 * is most naturally TWL VSIM; those pins also use PBIAS.
214 * FIXME handle VMMC1A as needed ...
217 if (cpu_is_omap2430()) {
218 reg = omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1);
219 if ((1 << vdd) >= MMC_VDD_30_31)
220 reg |= OMAP243X_MMC1_ACTIVE_OVERWRITE;
222 reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
223 omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
226 if (mmc->slots[0].internal_clock) {
227 reg = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0);
228 reg |= OMAP2_MMCSDIO1ADPCLKISEL;
229 omap_ctrl_writel(reg, OMAP2_CONTROL_DEVCONF0);
232 reg = omap_ctrl_readl(control_pbias_offset);
233 reg |= OMAP2_PBIASSPEEDCTRL0;
234 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
235 omap_ctrl_writel(reg, control_pbias_offset);
237 ret = mmc_regulator_set_ocr(c->vcc, vdd);
239 /* 100ms delay required for PBIAS configuration */
241 reg = omap_ctrl_readl(control_pbias_offset);
242 reg |= (OMAP2_PBIASLITEPWRDNZ0 | OMAP2_PBIASSPEEDCTRL0);
243 if ((1 << vdd) <= MMC_VDD_165_195)
244 reg &= ~OMAP2_PBIASLITEVMODE0;
246 reg |= OMAP2_PBIASLITEVMODE0;
247 omap_ctrl_writel(reg, control_pbias_offset);
249 reg = omap_ctrl_readl(control_pbias_offset);
250 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
251 omap_ctrl_writel(reg, control_pbias_offset);
253 ret = mmc_regulator_set_ocr(c->vcc, 0);
255 /* 100ms delay required for PBIAS configuration */
257 reg = omap_ctrl_readl(control_pbias_offset);
258 reg |= (OMAP2_PBIASSPEEDCTRL0 | OMAP2_PBIASLITEPWRDNZ0 |
259 OMAP2_PBIASLITEVMODE0);
260 omap_ctrl_writel(reg, control_pbias_offset);
266 static int twl_mmc23_set_power(struct device *dev, int slot, int power_on, int vdd)
269 struct twl_mmc_controller *c = &hsmmc[1];
270 struct omap_mmc_platform_data *mmc = dev->platform_data;
272 /* If we don't see a Vcc regulator, assume it's a fixed
273 * voltage always-on regulator.
279 * Assume Vcc regulator is used only to power the card ... OMAP
280 * VDDS is used to power the pins, optionally with a transceiver to
281 * support cards using voltages other than VDDS (1.8V nominal). When a
282 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
284 * In some cases this regulator won't support enable/disable;
285 * e.g. it's a fixed rail for a WLAN chip.
287 * In other cases vcc_aux switches interface power. Example, for
288 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
289 * chips/cards need an interface voltage rail too.
292 /* only MMC2 supports a CLKIN */
293 if (mmc->slots[0].internal_clock) {
296 reg = omap_ctrl_readl(control_devconf1_offset);
297 reg |= OMAP2_MMCSDIO2ADPCLKISEL;
298 omap_ctrl_writel(reg, control_devconf1_offset);
300 ret = mmc_regulator_set_ocr(c->vcc, vdd);
301 /* enable interface voltage rail, if needed */
302 if (ret == 0 && c->vcc_aux) {
303 ret = regulator_enable(c->vcc_aux);
305 ret = mmc_regulator_set_ocr(c->vcc, 0);
308 if (c->vcc_aux && (ret = regulator_is_enabled(c->vcc_aux)) > 0)
309 ret = regulator_disable(c->vcc_aux);
311 ret = mmc_regulator_set_ocr(c->vcc, 0);
317 static struct omap_mmc_platform_data *hsmmc_data[OMAP34XX_NR_MMC] __initdata;
319 void __init twl4030_mmc_init(struct twl4030_hsmmc_info *controllers)
321 struct twl4030_hsmmc_info *c;
322 int nr_hsmmc = ARRAY_SIZE(hsmmc_data);
324 if (cpu_is_omap2430()) {
325 control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
326 control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
329 control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
330 control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
333 for (c = controllers; c->mmc; c++) {
334 struct twl_mmc_controller *twl = hsmmc + c->mmc - 1;
335 struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
337 if (!c->mmc || c->mmc > nr_hsmmc) {
338 pr_debug("MMC%d: no such controller\n", c->mmc);
342 pr_debug("MMC%d: already configured\n", c->mmc);
346 mmc = kzalloc(sizeof(struct omap_mmc_platform_data), GFP_KERNEL);
348 pr_err("Cannot allocate memory for mmc device!\n");
353 strncpy(twl->name, c->name, HSMMC_NAME_LEN);
355 sprintf(twl->name, "mmc%islot%i", c->mmc, 1);
356 mmc->slots[0].name = twl->name;
358 mmc->slots[0].wires = c->wires;
359 mmc->slots[0].internal_clock = !c->ext_clock;
360 mmc->dma_mask = 0xffffffff;
361 mmc->init = twl_mmc_late_init;
363 /* note: twl4030 card detect GPIOs can disable VMMCx ... */
364 if (gpio_is_valid(c->gpio_cd)) {
365 mmc->cleanup = twl_mmc_cleanup;
366 mmc->suspend = twl_mmc_suspend;
367 mmc->resume = twl_mmc_resume;
369 mmc->slots[0].switch_pin = c->gpio_cd;
370 mmc->slots[0].card_detect_irq = gpio_to_irq(c->gpio_cd);
372 mmc->slots[0].get_cover_state = twl_mmc_get_cover_state;
374 mmc->slots[0].card_detect = twl_mmc_card_detect;
376 mmc->slots[0].switch_pin = -EINVAL;
378 /* write protect normally uses an OMAP gpio */
379 if (gpio_is_valid(c->gpio_wp)) {
380 gpio_request(c->gpio_wp, "mmc_wp");
381 gpio_direction_input(c->gpio_wp);
383 mmc->slots[0].gpio_wp = c->gpio_wp;
384 mmc->slots[0].get_ro = twl_mmc_get_ro;
386 mmc->slots[0].gpio_wp = -EINVAL;
388 /* NOTE: MMC slots should have a Vcc regulator set up.
389 * This may be from a TWL4030-family chip, another
390 * controllable regulator, or a fixed supply.
392 * temporary HACK: ocr_mask instead of fixed supply
394 mmc->slots[0].ocr_mask = c->ocr_mask;
398 /* on-chip level shifting via PBIAS0/PBIAS1 */
399 mmc->slots[0].set_power = twl_mmc1_set_power;
404 if (c->transceiver && c->wires > 4)
408 /* off-chip level shifting, or none */
409 mmc->slots[0].set_power = twl_mmc23_set_power;
412 pr_err("MMC%d configuration not supported!\n", c->mmc);
416 hsmmc_data[c->mmc - 1] = mmc;
419 omap2_init_mmc(hsmmc_data, OMAP34XX_NR_MMC);
421 /* pass the device nodes back to board setup code */
422 for (c = controllers; c->mmc; c++) {
423 struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
425 if (!c->mmc || c->mmc > nr_hsmmc)