]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/board-sdp-hsmmc.c
ARM: OMAP: HSMMC: enable use as a module
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / board-sdp-hsmmc.c
1 /*
2  * linux/arch/arm/mach-omap2/board-sdp-hsmmc.c
3  *
4  * Copyright (C) 2007 Texas Instruments
5  * Author: Texas Instruments
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/err.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <asm/hardware.h>
17 #include <asm/arch/twl4030.h>
18 #include <asm/arch/mmc.h>
19 #include <asm/arch/board.h>
20 #include <asm/io.h>
21
22 #if defined(CONFIG_MMC_OMAP_HS) || defined(CONFIG_MMC_OMAP_HS_MODULE)
23
24 #define VMMC1_DEV_GRP           0x27
25 #define P1_DEV_GRP              0x20
26 #define VMMC1_DEDICATED         0x2A
27 #define VSEL_3V                 0x02
28 #define VSEL_18V                0x00
29 #define TWL_GPIO_PUPDCTR1       0x13
30 #define TWL_GPIO_IMR1A          0x1C
31 #define TWL_GPIO_ISR1A          0x19
32 #define LDO_CLR                 0x00
33 #define VSEL_S2_CLR             0x40
34 #define GPIO_0_BIT_POS          1 << 0
35 #define MMC1_CD_IRQ             0
36 #define MMC2_CD_IRQ             1
37
38 static int sdp_mmc_card_detect(int irq)
39 {
40         return twl4030_get_gpio_datain(irq - IH_TWL4030_GPIO_BASE);
41 }
42
43 /*
44  * MMC Slot Initialization.
45  */
46 static int sdp_mmc_late_init(struct device *dev)
47 {
48         int ret = 0;
49
50         /*
51          * Configure TWL4030 GPIO parameters for MMC hotplug irq
52          */
53         ret = twl4030_request_gpio(MMC1_CD_IRQ);
54         if (ret != 0)
55                 goto err;
56
57         ret = twl4030_set_gpio_edge_ctrl(MMC1_CD_IRQ,
58                         TWL4030_GPIO_EDGE_RISING | TWL4030_GPIO_EDGE_FALLING);
59         if (ret != 0)
60                 goto err;
61
62         ret = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, 0x02,
63                                                 TWL_GPIO_PUPDCTR1);
64         if (ret != 0)
65                 goto err;
66
67         ret = twl4030_set_gpio_debounce(MMC1_CD_IRQ, TWL4030_GPIO_IS_ENABLE);
68         if (ret != 0)
69                 goto err;
70
71         return ret;
72 err:
73         dev_err(dev, "Failed to configure TWL4030 GPIO IRQ\n");
74
75         return ret;
76 }
77
78 static void sdp_mmc_cleanup(struct device *dev)
79 {
80         int ret = 0;
81
82         ret = twl4030_free_gpio(MMC1_CD_IRQ);
83         if (ret != 0)
84                 dev_err(dev, "Failed to configure TWL4030 GPIO IRQ\n");
85 }
86
87 #ifdef CONFIG_PM
88
89 /*
90  * To mask and unmask MMC Card Detect Interrupt
91  * mask : 1
92  * unmask : 0
93  */
94 static int mask_cd_interrupt(int mask)
95 {
96         u8 reg = 0, ret = 0;
97
98         ret = twl4030_i2c_read_u8(TWL4030_MODULE_GPIO, &reg, TWL_GPIO_IMR1A);
99         if (ret != 0)
100                 goto err;
101
102         reg = (mask == 1) ? (reg | GPIO_0_BIT_POS) : (reg & ~GPIO_0_BIT_POS);
103
104         ret = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, reg, TWL_GPIO_IMR1A);
105         if (ret != 0)
106                 goto err;
107
108         ret = twl4030_i2c_read_u8(TWL4030_MODULE_GPIO, &reg, TWL_GPIO_ISR1A);
109         if (ret != 0)
110                 goto err;
111
112         reg = (mask == 1) ? (reg | GPIO_0_BIT_POS) : (reg & ~GPIO_0_BIT_POS);
113
114         ret = twl4030_i2c_write_u8(TWL4030_MODULE_GPIO, reg, TWL_GPIO_ISR1A);
115         if (ret != 0)
116                 goto err;
117 err:
118         return ret;
119 }
120
121 static int sdp_mmc_suspend(struct device *dev, int slot)
122 {
123         int ret = 0;
124
125         disable_irq(TWL4030_GPIO_IRQ_NO(MMC1_CD_IRQ));
126         ret = mask_cd_interrupt(1);
127
128         return ret;
129 }
130
131 static int sdp_mmc_resume(struct device *dev, int slot)
132 {
133         int ret = 0;
134
135         enable_irq(TWL4030_GPIO_IRQ_NO(MMC1_CD_IRQ));
136         ret = mask_cd_interrupt(0);
137
138         return ret;
139 }
140
141 #endif
142
143 static int sdp_mmc_set_power(struct device *dev, int slot, int power_on,
144                                 int vdd)
145 {
146         u32 vdd_sel = 0, devconf = 0, reg = 0;
147         int ret = 0;
148
149         /* REVISIT: Using address directly till the control.h defines
150          * are settled.
151          */
152 #if defined(CONFIG_ARCH_OMAP2430)
153         #define OMAP2_CONTROL_PBIAS 0x490024A0
154 #else
155         #define OMAP2_CONTROL_PBIAS 0x48002520
156 #endif
157
158         if (power_on == 1) {
159                 if (cpu_is_omap24xx())
160                         devconf = omap_readl(0x490022E8);
161                 else
162                         devconf = omap_readl(0x48002274);
163
164                 switch (1 << vdd) {
165                 case MMC_VDD_33_34:
166                 case MMC_VDD_32_33:
167                         vdd_sel = VSEL_3V;
168                         if (cpu_is_omap24xx())
169                                 devconf = (devconf | (1 << 31));
170                         break;
171                 case MMC_VDD_165_195:
172                         vdd_sel = VSEL_18V;
173                         if (cpu_is_omap24xx())
174                                 devconf = (devconf & ~(1 << 31));
175                 }
176
177                 if (cpu_is_omap24xx())
178                         omap_writel(devconf, 0x490022E8);
179                 else
180                         omap_writel(devconf | 1 << 24, 0x48002274);
181
182                 omap_writel(omap_readl(OMAP2_CONTROL_PBIAS) | 1 << 2,
183                         OMAP2_CONTROL_PBIAS);
184                 omap_writel(omap_readl(OMAP2_CONTROL_PBIAS) & ~(1 << 1),
185                         OMAP2_CONTROL_PBIAS);
186
187                 ret = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
188                                                 P1_DEV_GRP, VMMC1_DEV_GRP);
189                 if (ret != 0)
190                         goto err;
191
192                 ret = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
193                                                 vdd_sel, VMMC1_DEDICATED);
194                 if (ret != 0)
195                         goto err;
196
197                 msleep(100);
198                 reg = omap_readl(OMAP2_CONTROL_PBIAS);
199                 reg = (vdd_sel == VSEL_18V) ? ((reg | 0x6) & ~0x1)
200                                                 : (reg | 0x7);
201                 omap_writel(reg, OMAP2_CONTROL_PBIAS);
202
203                 return ret;
204
205         } else if (power_on == 0) {
206                 /* Power OFF */
207
208                 /* For MMC1, Toggle PBIAS before every power up sequence */
209                 omap_writel(omap_readl(OMAP2_CONTROL_PBIAS) & ~(1 << 1),
210                                         OMAP2_CONTROL_PBIAS);
211                 ret = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
212                                                 LDO_CLR, VMMC1_DEV_GRP);
213                 if (ret != 0)
214                         goto err;
215
216                 ret = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
217                                                 VSEL_S2_CLR, VMMC1_DEDICATED);
218                 if (ret != 0)
219                         goto err;
220
221                 /* 100ms delay required for PBIAS configuration */
222                 msleep(100);
223                 omap_writel(omap_readl(OMAP2_CONTROL_PBIAS) | 0x7,
224                         OMAP2_CONTROL_PBIAS);
225         } else {
226                 ret = -1;
227                 goto err;
228         }
229
230         return 0;
231 err:
232         return 1;
233 }
234
235 static struct omap_mmc_platform_data sdp_mmc_data = {
236         .nr_slots                       = 1,
237         .switch_slot                    = NULL,
238         .init                           = sdp_mmc_late_init,
239         .cleanup                        = sdp_mmc_cleanup,
240 #ifdef CONFIG_PM
241         .suspend                        = sdp_mmc_suspend,
242         .resume                         = sdp_mmc_resume,
243 #endif
244         .slots[0] = {
245                 .set_power              = sdp_mmc_set_power,
246                 .set_bus_mode           = NULL,
247                 .get_ro                 = NULL,
248                 .get_cover_state        = NULL,
249                 .ocr_mask               = MMC_VDD_32_33 | MMC_VDD_33_34 |
250                                                 MMC_VDD_165_195,
251                 .name                   = "first slot",
252
253                 .card_detect_irq        = TWL4030_GPIO_IRQ_NO(MMC1_CD_IRQ),
254                 .card_detect            = sdp_mmc_card_detect,
255         },
256 };
257
258 void __init sdp_mmc_init(void)
259 {
260         omap_set_mmc_info(1, &sdp_mmc_data);
261 }
262
263 #else
264
265 void __init sdp_mmc_init(void)
266 {
267
268 }
269
270 #endif