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
;
138 gpio_free(mmc
->slots
[0].switch_pin
);
140 mmc
->slots
[0].card_detect_irq
= 0;
141 mmc
->slots
[0].card_detect
= NULL
;
143 dev_err(dev
, "err %d configuring card detect\n", ret
);
147 static void twl_mmc_cleanup(struct device
*dev
)
149 struct omap_mmc_platform_data
*mmc
= dev
->platform_data
;
151 gpio_free(mmc
->slots
[0].switch_pin
);
156 static int twl_mmc_suspend(struct device
*dev
, int slot
)
158 struct omap_mmc_platform_data
*mmc
= dev
->platform_data
;
160 disable_irq(mmc
->slots
[0].card_detect_irq
);
164 static int twl_mmc_resume(struct device
*dev
, int slot
)
166 struct omap_mmc_platform_data
*mmc
= dev
->platform_data
;
168 enable_irq(mmc
->slots
[0].card_detect_irq
);
173 #define twl_mmc_suspend NULL
174 #define twl_mmc_resume NULL
177 static int twl_mmc1_set_power(struct device
*dev
, int slot
, int power_on
,
182 struct twl_mmc_controller
*c
= &hsmmc
[0];
183 struct omap_mmc_platform_data
*mmc
= dev
->platform_data
;
186 * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
187 * card with Vcc regulator (from twl4030 or whatever). OMAP has both
188 * 1.8V and 3.0V modes, controlled by the PBIAS register.
190 * In 8-bit modes, OMAP VMMC1A (for DAT4..7) needs a supply, which
191 * is most naturally TWL VSIM; those pins also use PBIAS.
193 * FIXME handle VMMC1A as needed ...
196 if (cpu_is_omap2430()) {
197 reg
= omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1
);
198 if ((1 << vdd
) >= MMC_VDD_30_31
)
199 reg
|= OMAP243X_MMC1_ACTIVE_OVERWRITE
;
201 reg
&= ~OMAP243X_MMC1_ACTIVE_OVERWRITE
;
202 omap_ctrl_writel(reg
, OMAP243X_CONTROL_DEVCONF1
);
205 if (mmc
->slots
[0].internal_clock
) {
206 reg
= omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0
);
207 reg
|= OMAP2_MMCSDIO1ADPCLKISEL
;
208 omap_ctrl_writel(reg
, OMAP2_CONTROL_DEVCONF0
);
211 reg
= omap_ctrl_readl(control_pbias_offset
);
212 reg
|= OMAP2_PBIASSPEEDCTRL0
;
213 reg
&= ~OMAP2_PBIASLITEPWRDNZ0
;
214 omap_ctrl_writel(reg
, control_pbias_offset
);
216 ret
= mmc_regulator_set_ocr(c
->vcc
, vdd
);
218 /* 100ms delay required for PBIAS configuration */
220 reg
= omap_ctrl_readl(control_pbias_offset
);
221 reg
|= (OMAP2_PBIASLITEPWRDNZ0
| OMAP2_PBIASSPEEDCTRL0
);
222 if ((1 << vdd
) <= MMC_VDD_165_195
)
223 reg
&= ~OMAP2_PBIASLITEVMODE0
;
225 reg
|= OMAP2_PBIASLITEVMODE0
;
226 omap_ctrl_writel(reg
, control_pbias_offset
);
228 reg
= omap_ctrl_readl(control_pbias_offset
);
229 reg
&= ~OMAP2_PBIASLITEPWRDNZ0
;
230 omap_ctrl_writel(reg
, control_pbias_offset
);
232 ret
= mmc_regulator_set_ocr(c
->vcc
, 0);
234 /* 100ms delay required for PBIAS configuration */
236 reg
= omap_ctrl_readl(control_pbias_offset
);
237 reg
|= (OMAP2_PBIASSPEEDCTRL0
| OMAP2_PBIASLITEPWRDNZ0
|
238 OMAP2_PBIASLITEVMODE0
);
239 omap_ctrl_writel(reg
, control_pbias_offset
);
245 static int twl_mmc23_set_power(struct device
*dev
, int slot
, int power_on
, int vdd
)
248 struct twl_mmc_controller
*c
= &hsmmc
[1];
249 struct omap_mmc_platform_data
*mmc
= dev
->platform_data
;
251 /* If we don't see a Vcc regulator, assume it's a fixed
252 * voltage always-on regulator.
258 * Assume Vcc regulator is used only to power the card ... OMAP
259 * VDDS is used to power the pins, optionally with a transceiver to
260 * support cards using voltages other than VDDS (1.8V nominal). When a
261 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
263 * In some cases this regulator won't support enable/disable;
264 * e.g. it's a fixed rail for a WLAN chip.
266 * In other cases vcc_aux switches interface power. Example, for
267 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
268 * chips/cards need an interface voltage rail too.
271 /* only MMC2 supports a CLKIN */
272 if (mmc
->slots
[0].internal_clock
) {
275 reg
= omap_ctrl_readl(control_devconf1_offset
);
276 reg
|= OMAP2_MMCSDIO2ADPCLKISEL
;
277 omap_ctrl_writel(reg
, control_devconf1_offset
);
279 ret
= mmc_regulator_set_ocr(c
->vcc
, vdd
);
280 /* enable interface voltage rail, if needed */
281 if (ret
== 0 && c
->vcc_aux
) {
282 ret
= regulator_enable(c
->vcc_aux
);
284 ret
= mmc_regulator_set_ocr(c
->vcc
, 0);
288 ret
= regulator_enable(c
->vcc_aux
);
290 ret
= mmc_regulator_set_ocr(c
->vcc
, 0);
296 static struct omap_mmc_platform_data
*hsmmc_data
[OMAP34XX_NR_MMC
] __initdata
;
298 void __init
twl4030_mmc_init(struct twl4030_hsmmc_info
*controllers
)
300 struct twl4030_hsmmc_info
*c
;
301 int nr_hsmmc
= ARRAY_SIZE(hsmmc_data
);
303 if (cpu_is_omap2430()) {
304 control_pbias_offset
= OMAP243X_CONTROL_PBIAS_LITE
;
305 control_devconf1_offset
= OMAP243X_CONTROL_DEVCONF1
;
308 control_pbias_offset
= OMAP343X_CONTROL_PBIAS_LITE
;
309 control_devconf1_offset
= OMAP343X_CONTROL_DEVCONF1
;
312 for (c
= controllers
; c
->mmc
; c
++) {
313 struct twl_mmc_controller
*twl
= hsmmc
+ c
->mmc
- 1;
314 struct omap_mmc_platform_data
*mmc
= hsmmc_data
[c
->mmc
- 1];
316 if (!c
->mmc
|| c
->mmc
> nr_hsmmc
) {
317 pr_debug("MMC%d: no such controller\n", c
->mmc
);
321 pr_debug("MMC%d: already configured\n", c
->mmc
);
325 mmc
= kzalloc(sizeof(struct omap_mmc_platform_data
), GFP_KERNEL
);
327 pr_err("Cannot allocate memory for mmc device!\n");
332 strncpy(twl
->name
, c
->name
, HSMMC_NAME_LEN
);
334 sprintf(twl
->name
, "mmc%islot%i", c
->mmc
, 1);
335 mmc
->slots
[0].name
= twl
->name
;
337 mmc
->slots
[0].wires
= c
->wires
;
338 mmc
->slots
[0].internal_clock
= !c
->ext_clock
;
339 mmc
->dma_mask
= 0xffffffff;
340 mmc
->init
= twl_mmc_late_init
;
342 /* note: twl4030 card detect GPIOs can disable VMMCx ... */
343 if (gpio_is_valid(c
->gpio_cd
)) {
344 mmc
->cleanup
= twl_mmc_cleanup
;
345 mmc
->suspend
= twl_mmc_suspend
;
346 mmc
->resume
= twl_mmc_resume
;
348 mmc
->slots
[0].switch_pin
= c
->gpio_cd
;
349 mmc
->slots
[0].card_detect_irq
= gpio_to_irq(c
->gpio_cd
);
351 mmc
->slots
[0].get_cover_state
= twl_mmc_get_cover_state
;
353 mmc
->slots
[0].card_detect
= twl_mmc_card_detect
;
355 mmc
->slots
[0].switch_pin
= -EINVAL
;
357 /* write protect normally uses an OMAP gpio */
358 if (gpio_is_valid(c
->gpio_wp
)) {
359 gpio_request(c
->gpio_wp
, "mmc_wp");
360 gpio_direction_input(c
->gpio_wp
);
362 mmc
->slots
[0].gpio_wp
= c
->gpio_wp
;
363 mmc
->slots
[0].get_ro
= twl_mmc_get_ro
;
365 mmc
->slots
[0].gpio_wp
= -EINVAL
;
367 /* NOTE: MMC slots should have a Vcc regulator set up.
368 * This may be from a TWL4030-family chip, another
369 * controllable regulator, or a fixed supply.
371 * temporary HACK: ocr_mask instead of fixed supply
373 mmc
->slots
[0].ocr_mask
= c
->ocr_mask
;
377 /* on-chip level shifting via PBIAS0/PBIAS1 */
378 mmc
->slots
[0].set_power
= twl_mmc1_set_power
;
383 if (c
->transceiver
&& c
->wires
> 4)
387 /* off-chip level shifting, or none */
388 mmc
->slots
[0].set_power
= twl_mmc23_set_power
;
391 pr_err("MMC%d configuration not supported!\n", c
->mmc
);
395 hsmmc_data
[c
->mmc
- 1] = mmc
;
398 omap2_init_mmc(hsmmc_data
, OMAP34XX_NR_MMC
);
400 /* pass the device nodes back to board setup code */
401 for (c
= controllers
; c
->mmc
; c
++) {
402 struct omap_mmc_platform_data
*mmc
= hsmmc_data
[c
->mmc
- 1];
404 if (!c
->mmc
|| c
->mmc
> nr_hsmmc
)