]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/mmc-twl4030.c
mmc-twl4030 uses regulator framework
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / mmc-twl4030.c
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
22 #include <mach/hardware.h>
23 #include <mach/control.h>
24 #include <mach/mmc.h>
25 #include <mach/board.h>
26
27 #include "mmc-twl4030.h"
28
29
30 #if defined(CONFIG_REGULATOR) && \
31         (defined(CONFIG_MMC_OMAP_HS) || defined(CONFIG_MMC_OMAP_HS_MODULE))
32
33 static u16 control_pbias_offset;
34 static u16 control_devconf1_offset;
35
36 #define HSMMC_NAME_LEN  9
37
38 static struct twl_mmc_controller {
39         struct omap_mmc_platform_data   *mmc;
40         /* Vcc == configured supply
41          * Vcc_alt == optional
42          *   -  MMC1, supply for DAT4..DAT7
43          *   -  MMC2/MMC2, external level shifter voltage supply, for
44          *      chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
45          */
46         struct regulator                *vcc;
47         struct regulator                *vcc_aux;
48         char                            name[HSMMC_NAME_LEN + 1];
49 } hsmmc[OMAP34XX_NR_MMC];
50
51 static int twl_mmc_card_detect(int irq)
52 {
53         unsigned i;
54
55         for (i = 0; i < ARRAY_SIZE(hsmmc); i++) {
56                 struct omap_mmc_platform_data *mmc;
57
58                 mmc = hsmmc[i].mmc;
59                 if (!mmc)
60                         continue;
61                 if (irq != mmc->slots[0].card_detect_irq)
62                         continue;
63
64                 /* NOTE: assumes card detect signal is active-low */
65                 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
66         }
67         return -ENOSYS;
68 }
69
70 static int twl_mmc_get_ro(struct device *dev, int slot)
71 {
72         struct omap_mmc_platform_data *mmc = dev->platform_data;
73
74         /* NOTE: assumes write protect signal is active-high */
75         return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
76 }
77
78 static int twl_mmc_get_cover_state(struct device *dev, int slot)
79 {
80         struct omap_mmc_platform_data *mmc = dev->platform_data;
81
82         /* NOTE: assumes card detect signal is active-low */
83         return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
84 }
85
86 /*
87  * MMC Slot Initialization.
88  */
89 static int twl_mmc_late_init(struct device *dev)
90 {
91         struct omap_mmc_platform_data *mmc = dev->platform_data;
92         int ret = 0;
93         int i;
94
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");
98                 if (ret)
99                         goto done;
100                 ret = gpio_direction_input(mmc->slots[0].switch_pin);
101                 if (ret)
102                         goto err;
103         }
104
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;
109
110                         hsmmc[i].mmc = mmc;
111
112                         reg = regulator_get(dev, "vmmc");
113                         if (IS_ERR(reg)) {
114                                 dev_dbg(dev, "vmmc regulator missing\n");
115                                 /* HACK: until fixed.c regulator is usable,
116                                  * we don't require a main regulator
117                                  * for MMC2 or MMC3
118                                  */
119                                 if (i != 0)
120                                         break;
121                                 ret = PTR_ERR(reg);
122                                 goto err;
123                         }
124                         hsmmc[i].vcc = reg;
125                         mmc->slots[0].ocr_mask = mmc_regulator_get_ocrmask(reg);
126
127                         /* allow an aux regulator */
128                         reg = regulator_get(dev, "vmmc_aux");
129                         hsmmc[i].vcc_aux = IS_ERR(reg) ? NULL : reg;
130
131                         break;
132                 }
133         }
134
135         return 0;
136
137 err:
138         gpio_free(mmc->slots[0].switch_pin);
139 done:
140         mmc->slots[0].card_detect_irq = 0;
141         mmc->slots[0].card_detect = NULL;
142
143         dev_err(dev, "err %d configuring card detect\n", ret);
144         return ret;
145 }
146
147 static void twl_mmc_cleanup(struct device *dev)
148 {
149         struct omap_mmc_platform_data *mmc = dev->platform_data;
150
151         gpio_free(mmc->slots[0].switch_pin);
152 }
153
154 #ifdef CONFIG_PM
155
156 static int twl_mmc_suspend(struct device *dev, int slot)
157 {
158         struct omap_mmc_platform_data *mmc = dev->platform_data;
159
160         disable_irq(mmc->slots[0].card_detect_irq);
161         return 0;
162 }
163
164 static int twl_mmc_resume(struct device *dev, int slot)
165 {
166         struct omap_mmc_platform_data *mmc = dev->platform_data;
167
168         enable_irq(mmc->slots[0].card_detect_irq);
169         return 0;
170 }
171
172 #else
173 #define twl_mmc_suspend NULL
174 #define twl_mmc_resume  NULL
175 #endif
176
177 static int twl_mmc1_set_power(struct device *dev, int slot, int power_on,
178                                 int vdd)
179 {
180         u32 reg;
181         int ret = 0;
182         struct twl_mmc_controller *c = &hsmmc[0];
183         struct omap_mmc_platform_data *mmc = dev->platform_data;
184
185         /*
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.
189          *
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.
192          *
193          * FIXME handle VMMC1A as needed ...
194          */
195         if (power_on) {
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;
200                         else
201                                 reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
202                         omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
203                 }
204
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);
209                 }
210
211                 reg = omap_ctrl_readl(control_pbias_offset);
212                 reg |= OMAP2_PBIASSPEEDCTRL0;
213                 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
214                 omap_ctrl_writel(reg, control_pbias_offset);
215
216                 ret = mmc_regulator_set_ocr(c->vcc, vdd);
217
218                 /* 100ms delay required for PBIAS configuration */
219                 msleep(100);
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;
224                 else
225                         reg |= OMAP2_PBIASLITEVMODE0;
226                 omap_ctrl_writel(reg, control_pbias_offset);
227         } else {
228                 reg = omap_ctrl_readl(control_pbias_offset);
229                 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
230                 omap_ctrl_writel(reg, control_pbias_offset);
231
232                 ret = mmc_regulator_set_ocr(c->vcc, 0);
233
234                 /* 100ms delay required for PBIAS configuration */
235                 msleep(100);
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);
240         }
241
242         return ret;
243 }
244
245 static int twl_mmc23_set_power(struct device *dev, int slot, int power_on, int vdd)
246 {
247         int ret = 0;
248         struct twl_mmc_controller *c = &hsmmc[1];
249         struct omap_mmc_platform_data *mmc = dev->platform_data;
250
251         /* If we don't see a Vcc regulator, assume it's a fixed
252          * voltage always-on regulator.
253          */
254         if (!c->vcc)
255                 return 0;
256
257         /*
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.
262          *
263          * In some cases this regulator won't support enable/disable;
264          * e.g. it's a fixed rail for a WLAN chip.
265          *
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.
269          */
270         if (power_on) {
271                 /* only MMC2 supports a CLKIN */
272                 if (mmc->slots[0].internal_clock) {
273                         u32 reg;
274
275                         reg = omap_ctrl_readl(control_devconf1_offset);
276                         reg |= OMAP2_MMCSDIO2ADPCLKISEL;
277                         omap_ctrl_writel(reg, control_devconf1_offset);
278                 }
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);
283                         if (ret < 0)
284                                 ret = mmc_regulator_set_ocr(c->vcc, 0);
285                 }
286         } else {
287                 if (c->vcc_aux)
288                         ret = regulator_enable(c->vcc_aux);
289                 if (ret == 0)
290                         ret = mmc_regulator_set_ocr(c->vcc, 0);
291         }
292
293         return ret;
294 }
295
296 static struct omap_mmc_platform_data *hsmmc_data[OMAP34XX_NR_MMC] __initdata;
297
298 void __init twl4030_mmc_init(struct twl4030_hsmmc_info *controllers)
299 {
300         struct twl4030_hsmmc_info *c;
301         int nr_hsmmc = ARRAY_SIZE(hsmmc_data);
302
303         if (cpu_is_omap2430()) {
304                 control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
305                 control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
306                 nr_hsmmc = 2;
307         } else {
308                 control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
309                 control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
310         }
311
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];
315
316                 if (!c->mmc || c->mmc > nr_hsmmc) {
317                         pr_debug("MMC%d: no such controller\n", c->mmc);
318                         continue;
319                 }
320                 if (mmc) {
321                         pr_debug("MMC%d: already configured\n", c->mmc);
322                         continue;
323                 }
324
325                 mmc = kzalloc(sizeof(struct omap_mmc_platform_data), GFP_KERNEL);
326                 if (!mmc) {
327                         pr_err("Cannot allocate memory for mmc device!\n");
328                         return;
329                 }
330
331                 if (c->name)
332                         strncpy(twl->name, c->name, HSMMC_NAME_LEN);
333                 else
334                         sprintf(twl->name, "mmc%islot%i", c->mmc, 1);
335                 mmc->slots[0].name = twl->name;
336                 mmc->nr_slots = 1;
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;
341
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;
347
348                         mmc->slots[0].switch_pin = c->gpio_cd;
349                         mmc->slots[0].card_detect_irq = gpio_to_irq(c->gpio_cd);
350                         if (c->cover_only)
351                                 mmc->slots[0].get_cover_state = twl_mmc_get_cover_state;
352                         else
353                                 mmc->slots[0].card_detect = twl_mmc_card_detect;
354                 } else
355                         mmc->slots[0].switch_pin = -EINVAL;
356
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);
361
362                         mmc->slots[0].gpio_wp = c->gpio_wp;
363                         mmc->slots[0].get_ro = twl_mmc_get_ro;
364                 } else
365                         mmc->slots[0].gpio_wp = -EINVAL;
366
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.
370                  *
371                  * temporary HACK: ocr_mask instead of fixed supply
372                  */
373                 mmc->slots[0].ocr_mask = c->ocr_mask;
374
375                 switch (c->mmc) {
376                 case 1:
377                         /* on-chip level shifting via PBIAS0/PBIAS1 */
378                         mmc->slots[0].set_power = twl_mmc1_set_power;
379                         break;
380                 case 2:
381                         if (c->ext_clock)
382                                 c->transceiver = 1;
383                         if (c->transceiver && c->wires > 4)
384                                 c->wires = 4;
385                         /* FALLTHROUGH */
386                 case 3:
387                         /* off-chip level shifting, or none */
388                         mmc->slots[0].set_power = twl_mmc23_set_power;
389                         break;
390                 default:
391                         pr_err("MMC%d configuration not supported!\n", c->mmc);
392                         kfree(mmc);
393                         continue;
394                 }
395                 hsmmc_data[c->mmc - 1] = mmc;
396         }
397
398         omap2_init_mmc(hsmmc_data, OMAP34XX_NR_MMC);
399
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];
403
404                 if (!c->mmc || c->mmc > nr_hsmmc)
405                         continue;
406                 c->dev = mmc->dev;
407         }
408 }
409
410 #endif