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/i2c/twl4030.h>
21 #include <mach/hardware.h>
22 #include <mach/control.h>
24 #include <mach/board.h>
26 #include "mmc-twl4030.h"
28 #if defined(CONFIG_MMC_OMAP_HS) || defined(CONFIG_MMC_OMAP_HS_MODULE)
31 #define VSEL_S2_CLR 0x40
33 #define VMMC1_DEV_GRP 0x27
34 #define VMMC1_CLR 0x00
35 #define VMMC1_315V 0x03
36 #define VMMC1_300V 0x02
37 #define VMMC1_285V 0x01
38 #define VMMC1_185V 0x00
39 #define VMMC1_DEDICATED 0x2A
41 #define VMMC2_DEV_GRP 0x2B
42 #define VMMC2_CLR 0x40
43 #define VMMC2_315V 0x0c
44 #define VMMC2_300V 0x0b
45 #define VMMC2_285V 0x0a
46 #define VMMC2_260V 0x08
47 #define VMMC2_185V 0x06
48 #define VMMC2_DEDICATED 0x2E
50 #define VMMC_DEV_GRP_P1 0x20
52 static u16 control_pbias_offset;
54 static struct twl_mmc_controller {
55 u16 control_devconf_offset;
56 u32 devconf_loopback_clock;
58 unsigned card_wp_gpio;
63 .control_devconf_offset = OMAP2_CONTROL_DEVCONF0,
64 .devconf_loopback_clock = OMAP2_MMCSDIO1ADPCLKISEL,
65 .card_detect_gpio = -EINVAL,
66 .twl_vmmc_dev_grp = VMMC1_DEV_GRP,
67 .twl_mmc_dedicated = VMMC1_DEDICATED,
70 /* control_devconf_offset set dynamically */
71 .devconf_loopback_clock = OMAP2_MMCSDIO2ADPCLKISEL,
72 .card_detect_gpio = -EINVAL,
73 .twl_vmmc_dev_grp = VMMC2_DEV_GRP,
74 .twl_mmc_dedicated = VMMC2_DEDICATED,
78 static int twl_mmc1_card_detect(int irq)
80 /* NOTE: assumes card detect signal is active-low */
81 return !gpio_get_value_cansleep(hsmmc[0].card_detect_gpio);
84 static int twl_mmc1_get_ro(struct device *dev, int slot)
86 /* NOTE: assumes write protect signal is active-high */
87 return gpio_get_value_cansleep(hsmmc[0].card_wp_gpio);
91 * MMC Slot Initialization.
93 static int twl_mmc1_late_init(struct device *dev)
95 struct omap_mmc_platform_data *mmc = dev->platform_data;
98 ret = gpio_request(hsmmc[0].card_detect_gpio, "mmc0_cd");
101 ret = gpio_direction_input(hsmmc[0].card_detect_gpio);
105 /* FIXME assumes this uses (a) TWL4030 and (b) GPIO-0 ...
106 * but that's not actually required.
108 ret = twl4030_set_gpio_debounce(0, true);
115 dev_err(dev, "Failed to configure TWL4030 card detect\n");
117 mmc->slots[0].card_detect_irq = 0;
118 mmc->slots[0].card_detect = NULL;
122 static void twl_mmc1_cleanup(struct device *dev)
124 gpio_free(hsmmc[0].card_detect_gpio);
129 static int twl_mmc_suspend(struct device *dev, int slot)
131 struct omap_mmc_platform_data *mmc = dev->platform_data;
133 disable_irq(mmc->slots[0].card_detect_irq);
137 static int twl_mmc_resume(struct device *dev, int slot)
139 struct omap_mmc_platform_data *mmc = dev->platform_data;
141 enable_irq(mmc->slots[0].card_detect_irq);
146 #define twl_mmc_suspend NULL
147 #define twl_mmc_resume NULL
151 * Sets the MMC voltage in twl4030
153 static int twl_mmc_set_voltage(struct twl_mmc_controller *c, int vdd)
156 u8 vmmc, dev_grp_val;
165 if (c->twl_vmmc_dev_grp == VMMC1_DEV_GRP)
171 if (c->twl_vmmc_dev_grp == VMMC1_DEV_GRP)
178 if (c->twl_vmmc_dev_grp == VMMC1_DEV_GRP)
189 if (c->twl_vmmc_dev_grp == VMMC1_DEV_GRP)
194 case MMC_VDD_165_195:
195 if (c->twl_vmmc_dev_grp == VMMC1_DEV_GRP)
206 dev_grp_val = VMMC_DEV_GRP_P1; /* Power up */
208 dev_grp_val = LDO_CLR; /* Power down */
210 ret = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
211 dev_grp_val, c->twl_vmmc_dev_grp);
215 ret = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
216 vmmc, c->twl_mmc_dedicated);
221 static int twl_mmc1_set_power(struct device *dev, int slot, int power_on,
226 struct twl_mmc_controller *c = &hsmmc[0];
229 if (cpu_is_omap2430()) {
230 reg = omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1);
231 if ((1 << vdd) >= MMC_VDD_30_31)
232 reg |= OMAP243X_MMC1_ACTIVE_OVERWRITE;
234 reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
235 omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
238 /* REVISIT: Loop back clock not needed for 2430? */
239 if (!cpu_is_omap2430()) {
240 reg = omap_ctrl_readl(c->control_devconf_offset);
241 reg |= OMAP2_MMCSDIO1ADPCLKISEL;
242 omap_ctrl_writel(reg, c->control_devconf_offset);
245 reg = omap_ctrl_readl(control_pbias_offset);
246 reg |= OMAP2_PBIASSPEEDCTRL0;
247 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
248 omap_ctrl_writel(reg, control_pbias_offset);
250 ret = twl_mmc_set_voltage(c, vdd);
252 /* 100ms delay required for PBIAS configuration */
254 reg = omap_ctrl_readl(control_pbias_offset);
255 reg |= (OMAP2_PBIASLITEPWRDNZ0 | OMAP2_PBIASSPEEDCTRL0);
256 if ((1 << vdd) <= MMC_VDD_165_195)
257 reg &= ~OMAP2_PBIASLITEVMODE0;
259 reg |= OMAP2_PBIASLITEVMODE0;
260 omap_ctrl_writel(reg, control_pbias_offset);
262 reg = omap_ctrl_readl(control_pbias_offset);
263 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
264 omap_ctrl_writel(reg, control_pbias_offset);
266 ret = twl_mmc_set_voltage(c, 0);
268 /* 100ms delay required for PBIAS configuration */
270 reg = omap_ctrl_readl(control_pbias_offset);
271 reg |= (OMAP2_PBIASSPEEDCTRL0 | OMAP2_PBIASLITEPWRDNZ0 |
272 OMAP2_PBIASLITEVMODE0);
273 omap_ctrl_writel(reg, control_pbias_offset);
279 static int twl_mmc2_set_power(struct device *dev, int slot, int power_on, int vdd)
283 struct twl_mmc_controller *c = &hsmmc[1];
288 reg = omap_ctrl_readl(c->control_devconf_offset);
289 reg |= OMAP2_MMCSDIO2ADPCLKISEL;
290 omap_ctrl_writel(reg, c->control_devconf_offset);
291 ret = twl_mmc_set_voltage(c, vdd);
293 ret = twl_mmc_set_voltage(c, 0);
299 static struct omap_mmc_platform_data *hsmmc_data[OMAP34XX_NR_MMC];
301 #define HSMMC_NAME_LEN 9
303 void __init hsmmc_init(struct twl4030_hsmmc_info *controllers)
305 struct twl4030_hsmmc_info *c;
307 if (cpu_is_omap2430()) {
308 control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
309 hsmmc[1].control_devconf_offset = OMAP243X_CONTROL_DEVCONF1;
311 control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
312 hsmmc[1].control_devconf_offset = OMAP343X_CONTROL_DEVCONF1;
315 for (c = controllers; c->mmc; c++) {
316 struct omap_mmc_platform_data *mmc;
319 mmc = kzalloc(sizeof(struct omap_mmc_platform_data), GFP_KERNEL);
321 pr_err("Cannot allocate memory for mmc device!\n");
325 name = kzalloc(HSMMC_NAME_LEN, GFP_KERNEL);
328 pr_err("Cannot allocate memory for mmc name!\n");
332 sprintf(name, "mmc%islot%i", c->mmc, 1);
333 mmc->slots[0].name = name;
335 mmc->slots[0].ocr_mask = MMC_VDD_165_195 |
336 MMC_VDD_26_27 | MMC_VDD_27_28 |
338 MMC_VDD_30_31 | MMC_VDD_31_32;
339 mmc->slots[0].wires = c->wires;
340 mmc->dma_mask = 0xffffffff;
342 /* NOTE: we assume OMAP's MMC1 and MMC2 use
343 * the TWL4030's VMMC1 and VMMC2, respectively;
344 * and that OMAP's MMC3 isn't used.
349 mmc->slots[0].set_power = twl_mmc1_set_power;
350 if (gpio_is_valid(c->gpio_cd)) {
351 mmc->slots[0].card_detect_irq =
352 gpio_to_irq(c->gpio_cd);
353 mmc->suspend = twl_mmc_suspend;
354 mmc->resume = twl_mmc_resume;
356 /* NOTE: hsmmc[0] is hard-wired ... */
357 hsmmc[0].card_detect_gpio = c->gpio_cd;
358 mmc->init = twl_mmc1_late_init;
359 mmc->cleanup = twl_mmc1_cleanup;
360 mmc->slots[0].card_detect =
361 twl_mmc1_card_detect;
363 if (gpio_is_valid(c->gpio_wp)) {
364 gpio_request(c->gpio_wp, "mmc0_wp");
365 gpio_direction_input(c->gpio_wp);
367 /* NOTE: hsmmc[0] is hard-wired ... */
368 hsmmc[0].card_wp_gpio = c->gpio_wp;
369 mmc->slots[0].get_ro = twl_mmc1_get_ro;
374 /* FIXME rework interfaces so that mmc2 (and mmc3) can
375 * be fully functional... hsmmc[] shouldn't hold gpios.
377 mmc->slots[0].set_power = twl_mmc2_set_power;
378 if (gpio_is_valid(c->gpio_cd))
379 pr_warning("MMC2 detect nyet supported!\n");
380 if (gpio_is_valid(c->gpio_wp))
381 pr_warning("MMC2 WP nyet supported!\n");
385 pr_err("MMC%d configuration not supported!\n", c->mmc);
390 omap2_init_mmc(hsmmc_data, OMAP34XX_NR_MMC);