blob: 9756a878fd90263c5ddd4fcfd7a1d6dc60228c77 [file] [log] [blame]
Tony Lindgren90c62bf2008-12-10 17:37:17 -08001/*
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>
David Brownellb583f262009-05-28 14:04:03 -070019#include <linux/mmc/host.h>
20#include <linux/regulator/consumer.h>
Tony Lindgren90c62bf2008-12-10 17:37:17 -080021
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
David Brownellb583f262009-05-28 14:04:03 -070029
30#if defined(CONFIG_REGULATOR) && \
Tony Lindgren90c62bf2008-12-10 17:37:17 -080031 (defined(CONFIG_MMC_OMAP_HS) || defined(CONFIG_MMC_OMAP_HS_MODULE))
32
Tony Lindgren90c62bf2008-12-10 17:37:17 -080033static u16 control_pbias_offset;
34static u16 control_devconf1_offset;
35
36#define HSMMC_NAME_LEN 9
37
38static struct twl_mmc_controller {
39 struct omap_mmc_platform_data *mmc;
David Brownellb583f262009-05-28 14:04:03 -070040 /* 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];
Tony Lindgren90c62bf2008-12-10 17:37:17 -080050
51static 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
70static 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
Adrian Hunter8d75e982009-03-23 18:23:48 -070078static 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
Tony Lindgren90c62bf2008-12-10 17:37:17 -080086/*
87 * MMC Slot Initialization.
88 */
89static 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
David Brownellb583f262009-05-28 14:04:03 -070095 /* 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 }
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800104
David Brownellb583f262009-05-28 14:04:03 -0700105 /* require at least main regulator */
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800106 for (i = 0; i < ARRAY_SIZE(hsmmc); i++) {
107 if (hsmmc[i].name == mmc->slots[0].name) {
David Brownellb583f262009-05-28 14:04:03 -0700108 struct regulator *reg;
109
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800110 hsmmc[i].mmc = mmc;
David Brownellb583f262009-05-28 14:04:03 -0700111
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 /* 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 enabling it. Until the
135 * framework is fixed, we need a workaround like this
136 * (which is safe for MMC, but not in general).
137 */
138 if (regulator_is_enabled(hsmmc[i].vcc) > 0) {
139 regulator_enable(hsmmc[i].vcc);
140 regulator_disable(hsmmc[i].vcc);
141 }
142 if (hsmmc[i].vcc_aux) {
143 if (regulator_is_enabled(reg) > 0) {
144 regulator_enable(reg);
145 regulator_disable(reg);
146 }
147 }
148
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800149 break;
150 }
151 }
152
153 return 0;
154
155err:
156 gpio_free(mmc->slots[0].switch_pin);
157done:
158 mmc->slots[0].card_detect_irq = 0;
159 mmc->slots[0].card_detect = NULL;
160
161 dev_err(dev, "err %d configuring card detect\n", ret);
162 return ret;
163}
164
165static void twl_mmc_cleanup(struct device *dev)
166{
167 struct omap_mmc_platform_data *mmc = dev->platform_data;
168
169 gpio_free(mmc->slots[0].switch_pin);
170}
171
172#ifdef CONFIG_PM
173
174static int twl_mmc_suspend(struct device *dev, int slot)
175{
176 struct omap_mmc_platform_data *mmc = dev->platform_data;
177
178 disable_irq(mmc->slots[0].card_detect_irq);
179 return 0;
180}
181
182static int twl_mmc_resume(struct device *dev, int slot)
183{
184 struct omap_mmc_platform_data *mmc = dev->platform_data;
185
186 enable_irq(mmc->slots[0].card_detect_irq);
187 return 0;
188}
189
190#else
191#define twl_mmc_suspend NULL
192#define twl_mmc_resume NULL
193#endif
194
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800195static int twl_mmc1_set_power(struct device *dev, int slot, int power_on,
196 int vdd)
197{
198 u32 reg;
199 int ret = 0;
200 struct twl_mmc_controller *c = &hsmmc[0];
201 struct omap_mmc_platform_data *mmc = dev->platform_data;
202
David Brownell0329c372009-03-23 18:23:47 -0700203 /*
204 * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
David Brownellb583f262009-05-28 14:04:03 -0700205 * card with Vcc regulator (from twl4030 or whatever). OMAP has both
David Brownell0329c372009-03-23 18:23:47 -0700206 * 1.8V and 3.0V modes, controlled by the PBIAS register.
207 *
208 * In 8-bit modes, OMAP VMMC1A (for DAT4..7) needs a supply, which
209 * is most naturally TWL VSIM; those pins also use PBIAS.
David Brownellb583f262009-05-28 14:04:03 -0700210 *
211 * FIXME handle VMMC1A as needed ...
David Brownell0329c372009-03-23 18:23:47 -0700212 */
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800213 if (power_on) {
214 if (cpu_is_omap2430()) {
215 reg = omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1);
216 if ((1 << vdd) >= MMC_VDD_30_31)
217 reg |= OMAP243X_MMC1_ACTIVE_OVERWRITE;
218 else
219 reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
220 omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
221 }
222
223 if (mmc->slots[0].internal_clock) {
224 reg = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0);
225 reg |= OMAP2_MMCSDIO1ADPCLKISEL;
226 omap_ctrl_writel(reg, OMAP2_CONTROL_DEVCONF0);
227 }
228
229 reg = omap_ctrl_readl(control_pbias_offset);
230 reg |= OMAP2_PBIASSPEEDCTRL0;
231 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
232 omap_ctrl_writel(reg, control_pbias_offset);
233
David Brownellb583f262009-05-28 14:04:03 -0700234 ret = mmc_regulator_set_ocr(c->vcc, vdd);
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800235
236 /* 100ms delay required for PBIAS configuration */
237 msleep(100);
238 reg = omap_ctrl_readl(control_pbias_offset);
239 reg |= (OMAP2_PBIASLITEPWRDNZ0 | OMAP2_PBIASSPEEDCTRL0);
240 if ((1 << vdd) <= MMC_VDD_165_195)
241 reg &= ~OMAP2_PBIASLITEVMODE0;
242 else
243 reg |= OMAP2_PBIASLITEVMODE0;
244 omap_ctrl_writel(reg, control_pbias_offset);
245 } else {
246 reg = omap_ctrl_readl(control_pbias_offset);
247 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
248 omap_ctrl_writel(reg, control_pbias_offset);
249
David Brownellb583f262009-05-28 14:04:03 -0700250 ret = mmc_regulator_set_ocr(c->vcc, 0);
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800251
252 /* 100ms delay required for PBIAS configuration */
253 msleep(100);
254 reg = omap_ctrl_readl(control_pbias_offset);
255 reg |= (OMAP2_PBIASSPEEDCTRL0 | OMAP2_PBIASLITEPWRDNZ0 |
256 OMAP2_PBIASLITEVMODE0);
257 omap_ctrl_writel(reg, control_pbias_offset);
258 }
259
260 return ret;
261}
262
David Brownellb583f262009-05-28 14:04:03 -0700263static int twl_mmc23_set_power(struct device *dev, int slot, int power_on, int vdd)
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800264{
David Brownellb583f262009-05-28 14:04:03 -0700265 int ret = 0;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800266 struct twl_mmc_controller *c = &hsmmc[1];
267 struct omap_mmc_platform_data *mmc = dev->platform_data;
268
David Brownellb583f262009-05-28 14:04:03 -0700269 /* If we don't see a Vcc regulator, assume it's a fixed
270 * voltage always-on regulator.
271 */
272 if (!c->vcc)
273 return 0;
274
David Brownell0329c372009-03-23 18:23:47 -0700275 /*
David Brownellb583f262009-05-28 14:04:03 -0700276 * Assume Vcc regulator is used only to power the card ... OMAP
David Brownell0329c372009-03-23 18:23:47 -0700277 * VDDS is used to power the pins, optionally with a transceiver to
278 * support cards using voltages other than VDDS (1.8V nominal). When a
279 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
David Brownellb583f262009-05-28 14:04:03 -0700280 *
281 * In some cases this regulator won't support enable/disable;
282 * e.g. it's a fixed rail for a WLAN chip.
283 *
284 * In other cases vcc_aux switches interface power. Example, for
285 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
286 * chips/cards need an interface voltage rail too.
David Brownell0329c372009-03-23 18:23:47 -0700287 */
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800288 if (power_on) {
David Brownellb583f262009-05-28 14:04:03 -0700289 /* only MMC2 supports a CLKIN */
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800290 if (mmc->slots[0].internal_clock) {
291 u32 reg;
292
293 reg = omap_ctrl_readl(control_devconf1_offset);
294 reg |= OMAP2_MMCSDIO2ADPCLKISEL;
295 omap_ctrl_writel(reg, control_devconf1_offset);
296 }
David Brownellb583f262009-05-28 14:04:03 -0700297 ret = mmc_regulator_set_ocr(c->vcc, vdd);
298 /* enable interface voltage rail, if needed */
299 if (ret == 0 && c->vcc_aux) {
300 ret = regulator_enable(c->vcc_aux);
301 if (ret < 0)
302 ret = mmc_regulator_set_ocr(c->vcc, 0);
303 }
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800304 } else {
David Brownellb583f262009-05-28 14:04:03 -0700305 if (c->vcc_aux && (ret = regulator_is_enabled(c->vcc_aux)) > 0)
306 ret = regulator_disable(c->vcc_aux);
307 if (ret == 0)
308 ret = mmc_regulator_set_ocr(c->vcc, 0);
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800309 }
310
311 return ret;
312}
313
314static struct omap_mmc_platform_data *hsmmc_data[OMAP34XX_NR_MMC] __initdata;
315
316void __init twl4030_mmc_init(struct twl4030_hsmmc_info *controllers)
317{
318 struct twl4030_hsmmc_info *c;
319 int nr_hsmmc = ARRAY_SIZE(hsmmc_data);
320
321 if (cpu_is_omap2430()) {
322 control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
323 control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
324 nr_hsmmc = 2;
325 } else {
326 control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
327 control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
328 }
329
330 for (c = controllers; c->mmc; c++) {
331 struct twl_mmc_controller *twl = hsmmc + c->mmc - 1;
332 struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
333
334 if (!c->mmc || c->mmc > nr_hsmmc) {
335 pr_debug("MMC%d: no such controller\n", c->mmc);
336 continue;
337 }
338 if (mmc) {
339 pr_debug("MMC%d: already configured\n", c->mmc);
340 continue;
341 }
342
343 mmc = kzalloc(sizeof(struct omap_mmc_platform_data), GFP_KERNEL);
344 if (!mmc) {
345 pr_err("Cannot allocate memory for mmc device!\n");
346 return;
347 }
348
Adrian Huntere51151a2009-03-23 18:23:48 -0700349 if (c->name)
350 strncpy(twl->name, c->name, HSMMC_NAME_LEN);
351 else
352 snprintf(twl->name, ARRAY_SIZE(twl->name),
353 "mmc%islot%i", c->mmc, 1);
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800354 mmc->slots[0].name = twl->name;
355 mmc->nr_slots = 1;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800356 mmc->slots[0].wires = c->wires;
357 mmc->slots[0].internal_clock = !c->ext_clock;
358 mmc->dma_mask = 0xffffffff;
David Brownellb583f262009-05-28 14:04:03 -0700359 mmc->init = twl_mmc_late_init;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800360
David Brownellb583f262009-05-28 14:04:03 -0700361 /* note: twl4030 card detect GPIOs can disable VMMCx ... */
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800362 if (gpio_is_valid(c->gpio_cd)) {
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800363 mmc->cleanup = twl_mmc_cleanup;
364 mmc->suspend = twl_mmc_suspend;
365 mmc->resume = twl_mmc_resume;
366
367 mmc->slots[0].switch_pin = c->gpio_cd;
368 mmc->slots[0].card_detect_irq = gpio_to_irq(c->gpio_cd);
Adrian Hunter8d75e982009-03-23 18:23:48 -0700369 if (c->cover_only)
370 mmc->slots[0].get_cover_state = twl_mmc_get_cover_state;
371 else
372 mmc->slots[0].card_detect = twl_mmc_card_detect;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800373 } else
374 mmc->slots[0].switch_pin = -EINVAL;
375
376 /* write protect normally uses an OMAP gpio */
377 if (gpio_is_valid(c->gpio_wp)) {
378 gpio_request(c->gpio_wp, "mmc_wp");
379 gpio_direction_input(c->gpio_wp);
380
381 mmc->slots[0].gpio_wp = c->gpio_wp;
382 mmc->slots[0].get_ro = twl_mmc_get_ro;
383 } else
384 mmc->slots[0].gpio_wp = -EINVAL;
385
David Brownellb583f262009-05-28 14:04:03 -0700386 /* NOTE: MMC slots should have a Vcc regulator set up.
387 * This may be from a TWL4030-family chip, another
388 * controllable regulator, or a fixed supply.
389 *
390 * temporary HACK: ocr_mask instead of fixed supply
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800391 */
David Brownellb583f262009-05-28 14:04:03 -0700392 mmc->slots[0].ocr_mask = c->ocr_mask;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800393
394 switch (c->mmc) {
395 case 1:
David Brownellb583f262009-05-28 14:04:03 -0700396 /* on-chip level shifting via PBIAS0/PBIAS1 */
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800397 mmc->slots[0].set_power = twl_mmc1_set_power;
398 break;
399 case 2:
David Brownellb583f262009-05-28 14:04:03 -0700400 if (c->ext_clock)
401 c->transceiver = 1;
402 if (c->transceiver && c->wires > 4)
403 c->wires = 4;
404 /* FALLTHROUGH */
Grazvydas Ignotas07d83cc2009-03-23 18:23:47 -0700405 case 3:
David Brownellb583f262009-05-28 14:04:03 -0700406 /* off-chip level shifting, or none */
407 mmc->slots[0].set_power = twl_mmc23_set_power;
Grazvydas Ignotas07d83cc2009-03-23 18:23:47 -0700408 break;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800409 default:
410 pr_err("MMC%d configuration not supported!\n", c->mmc);
Grazvydas Ignotas07d83cc2009-03-23 18:23:47 -0700411 kfree(mmc);
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800412 continue;
413 }
414 hsmmc_data[c->mmc - 1] = mmc;
415 }
416
417 omap2_init_mmc(hsmmc_data, OMAP34XX_NR_MMC);
David Brownell01971f62009-03-23 18:23:47 -0700418
419 /* pass the device nodes back to board setup code */
420 for (c = controllers; c->mmc; c++) {
421 struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
422
423 if (!c->mmc || c->mmc > nr_hsmmc)
424 continue;
425 c->dev = mmc->dev;
426 }
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800427}
428
429#endif