blob: d43421400e9d4d66b6d431c694b2cd26eaacb759 [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>
19#include <linux/i2c/twl4030.h>
David Brownell01971f62009-03-23 18:23:47 -070020#include <linux/regulator/machine.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
29#if defined(CONFIG_TWL4030_CORE) && \
30 (defined(CONFIG_MMC_OMAP_HS) || defined(CONFIG_MMC_OMAP_HS_MODULE))
31
32#define LDO_CLR 0x00
33#define VSEL_S2_CLR 0x40
34
35#define VMMC1_DEV_GRP 0x27
36#define VMMC1_CLR 0x00
37#define VMMC1_315V 0x03
38#define VMMC1_300V 0x02
39#define VMMC1_285V 0x01
40#define VMMC1_185V 0x00
41#define VMMC1_DEDICATED 0x2A
42
43#define VMMC2_DEV_GRP 0x2B
44#define VMMC2_CLR 0x40
45#define VMMC2_315V 0x0c
46#define VMMC2_300V 0x0b
47#define VMMC2_285V 0x0a
David Brownell0329c372009-03-23 18:23:47 -070048#define VMMC2_280V 0x09
Tony Lindgren90c62bf2008-12-10 17:37:17 -080049#define VMMC2_260V 0x08
50#define VMMC2_185V 0x06
51#define VMMC2_DEDICATED 0x2E
52
53#define VMMC_DEV_GRP_P1 0x20
54
55static u16 control_pbias_offset;
56static u16 control_devconf1_offset;
57
58#define HSMMC_NAME_LEN 9
59
60static struct twl_mmc_controller {
61 struct omap_mmc_platform_data *mmc;
62 u8 twl_vmmc_dev_grp;
63 u8 twl_mmc_dedicated;
Adrian Hunter84660322009-03-23 18:23:46 -070064 char name[HSMMC_NAME_LEN + 1];
Grazvydas Ignotas07d83cc2009-03-23 18:23:47 -070065} hsmmc[OMAP34XX_NR_MMC] = {
Tony Lindgren90c62bf2008-12-10 17:37:17 -080066 {
67 .twl_vmmc_dev_grp = VMMC1_DEV_GRP,
68 .twl_mmc_dedicated = VMMC1_DEDICATED,
69 },
70 {
71 .twl_vmmc_dev_grp = VMMC2_DEV_GRP,
72 .twl_mmc_dedicated = VMMC2_DEDICATED,
73 },
74};
75
76static int twl_mmc_card_detect(int irq)
77{
78 unsigned i;
79
80 for (i = 0; i < ARRAY_SIZE(hsmmc); i++) {
81 struct omap_mmc_platform_data *mmc;
82
83 mmc = hsmmc[i].mmc;
84 if (!mmc)
85 continue;
86 if (irq != mmc->slots[0].card_detect_irq)
87 continue;
88
89 /* NOTE: assumes card detect signal is active-low */
90 return !gpio_get_value_cansleep(mmc->slots[0].switch_pin);
91 }
92 return -ENOSYS;
93}
94
95static int twl_mmc_get_ro(struct device *dev, int slot)
96{
97 struct omap_mmc_platform_data *mmc = dev->platform_data;
98
99 /* NOTE: assumes write protect signal is active-high */
100 return gpio_get_value_cansleep(mmc->slots[0].gpio_wp);
101}
102
103/*
104 * MMC Slot Initialization.
105 */
106static int twl_mmc_late_init(struct device *dev)
107{
108 struct omap_mmc_platform_data *mmc = dev->platform_data;
109 int ret = 0;
110 int i;
111
112 ret = gpio_request(mmc->slots[0].switch_pin, "mmc_cd");
113 if (ret)
114 goto done;
115 ret = gpio_direction_input(mmc->slots[0].switch_pin);
116 if (ret)
117 goto err;
118
119 for (i = 0; i < ARRAY_SIZE(hsmmc); i++) {
120 if (hsmmc[i].name == mmc->slots[0].name) {
121 hsmmc[i].mmc = mmc;
122 break;
123 }
124 }
125
126 return 0;
127
128err:
129 gpio_free(mmc->slots[0].switch_pin);
130done:
131 mmc->slots[0].card_detect_irq = 0;
132 mmc->slots[0].card_detect = NULL;
133
134 dev_err(dev, "err %d configuring card detect\n", ret);
135 return ret;
136}
137
138static void twl_mmc_cleanup(struct device *dev)
139{
140 struct omap_mmc_platform_data *mmc = dev->platform_data;
141
142 gpio_free(mmc->slots[0].switch_pin);
143}
144
145#ifdef CONFIG_PM
146
147static int twl_mmc_suspend(struct device *dev, int slot)
148{
149 struct omap_mmc_platform_data *mmc = dev->platform_data;
150
151 disable_irq(mmc->slots[0].card_detect_irq);
152 return 0;
153}
154
155static int twl_mmc_resume(struct device *dev, int slot)
156{
157 struct omap_mmc_platform_data *mmc = dev->platform_data;
158
159 enable_irq(mmc->slots[0].card_detect_irq);
160 return 0;
161}
162
163#else
164#define twl_mmc_suspend NULL
165#define twl_mmc_resume NULL
166#endif
167
168/*
169 * Sets the MMC voltage in twl4030
170 */
David Brownell0329c372009-03-23 18:23:47 -0700171
172#define MMC1_OCR (MMC_VDD_165_195 \
173 |MMC_VDD_28_29|MMC_VDD_29_30|MMC_VDD_30_31|MMC_VDD_31_32)
174#define MMC2_OCR (MMC_VDD_165_195 \
175 |MMC_VDD_25_26|MMC_VDD_26_27|MMC_VDD_27_28 \
176 |MMC_VDD_28_29|MMC_VDD_29_30|MMC_VDD_30_31|MMC_VDD_31_32)
177
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800178static int twl_mmc_set_voltage(struct twl_mmc_controller *c, int vdd)
179{
180 int ret;
David Brownell034ae7b2009-03-23 18:23:48 -0700181 u8 vmmc = 0, dev_grp_val;
182
183 if (!vdd)
184 goto doit;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800185
David Brownell0329c372009-03-23 18:23:47 -0700186 if (c->twl_vmmc_dev_grp == VMMC1_DEV_GRP) {
187 /* VMMC1: max 220 mA. And for 8-bit mode,
188 * VSIM: max 50 mA
189 */
190 switch (1 << vdd) {
191 case MMC_VDD_165_195:
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800192 vmmc = VMMC1_185V;
David Brownell0329c372009-03-23 18:23:47 -0700193 /* and VSIM_180V */
194 break;
195 case MMC_VDD_28_29:
196 vmmc = VMMC1_285V;
197 /* and VSIM_280V */
198 break;
199 case MMC_VDD_29_30:
200 case MMC_VDD_30_31:
201 vmmc = VMMC1_300V;
202 /* and VSIM_300V */
203 break;
204 case MMC_VDD_31_32:
205 vmmc = VMMC1_315V;
206 /* error if VSIM needed */
207 break;
208 default:
David Brownell034ae7b2009-03-23 18:23:48 -0700209 return -EINVAL;
David Brownell0329c372009-03-23 18:23:47 -0700210 }
211 } else if (c->twl_vmmc_dev_grp == VMMC2_DEV_GRP) {
212 /* VMMC2: max 100 mA */
213 switch (1 << vdd) {
214 case MMC_VDD_165_195:
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800215 vmmc = VMMC2_185V;
David Brownell0329c372009-03-23 18:23:47 -0700216 break;
217 case MMC_VDD_25_26:
218 case MMC_VDD_26_27:
219 vmmc = VMMC2_260V;
220 break;
221 case MMC_VDD_27_28:
222 vmmc = VMMC2_280V;
223 break;
224 case MMC_VDD_28_29:
225 vmmc = VMMC2_285V;
226 break;
227 case MMC_VDD_29_30:
228 case MMC_VDD_30_31:
229 vmmc = VMMC2_300V;
230 break;
231 case MMC_VDD_31_32:
232 vmmc = VMMC2_315V;
233 break;
234 default:
David Brownell034ae7b2009-03-23 18:23:48 -0700235 return -EINVAL;
David Brownell0329c372009-03-23 18:23:47 -0700236 }
237 } else {
David Brownell034ae7b2009-03-23 18:23:48 -0700238 return -EINVAL;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800239 }
240
David Brownell034ae7b2009-03-23 18:23:48 -0700241doit:
242 if (vdd)
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800243 dev_grp_val = VMMC_DEV_GRP_P1; /* Power up */
244 else
245 dev_grp_val = LDO_CLR; /* Power down */
246
247 ret = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
248 dev_grp_val, c->twl_vmmc_dev_grp);
David Brownell034ae7b2009-03-23 18:23:48 -0700249 if (ret || !vdd)
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800250 return ret;
251
252 ret = twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
253 vmmc, c->twl_mmc_dedicated);
254
255 return ret;
256}
257
258static int twl_mmc1_set_power(struct device *dev, int slot, int power_on,
259 int vdd)
260{
261 u32 reg;
262 int ret = 0;
263 struct twl_mmc_controller *c = &hsmmc[0];
264 struct omap_mmc_platform_data *mmc = dev->platform_data;
265
David Brownell0329c372009-03-23 18:23:47 -0700266 /*
267 * Assume we power both OMAP VMMC1 (for CMD, CLK, DAT0..3) and the
268 * card using the same TWL VMMC1 supply (hsmmc[0]); OMAP has both
269 * 1.8V and 3.0V modes, controlled by the PBIAS register.
270 *
271 * In 8-bit modes, OMAP VMMC1A (for DAT4..7) needs a supply, which
272 * is most naturally TWL VSIM; those pins also use PBIAS.
273 */
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800274 if (power_on) {
275 if (cpu_is_omap2430()) {
276 reg = omap_ctrl_readl(OMAP243X_CONTROL_DEVCONF1);
277 if ((1 << vdd) >= MMC_VDD_30_31)
278 reg |= OMAP243X_MMC1_ACTIVE_OVERWRITE;
279 else
280 reg &= ~OMAP243X_MMC1_ACTIVE_OVERWRITE;
281 omap_ctrl_writel(reg, OMAP243X_CONTROL_DEVCONF1);
282 }
283
284 if (mmc->slots[0].internal_clock) {
285 reg = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0);
286 reg |= OMAP2_MMCSDIO1ADPCLKISEL;
287 omap_ctrl_writel(reg, OMAP2_CONTROL_DEVCONF0);
288 }
289
290 reg = omap_ctrl_readl(control_pbias_offset);
291 reg |= OMAP2_PBIASSPEEDCTRL0;
292 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
293 omap_ctrl_writel(reg, control_pbias_offset);
294
295 ret = twl_mmc_set_voltage(c, vdd);
296
297 /* 100ms delay required for PBIAS configuration */
298 msleep(100);
299 reg = omap_ctrl_readl(control_pbias_offset);
300 reg |= (OMAP2_PBIASLITEPWRDNZ0 | OMAP2_PBIASSPEEDCTRL0);
301 if ((1 << vdd) <= MMC_VDD_165_195)
302 reg &= ~OMAP2_PBIASLITEVMODE0;
303 else
304 reg |= OMAP2_PBIASLITEVMODE0;
305 omap_ctrl_writel(reg, control_pbias_offset);
306 } else {
307 reg = omap_ctrl_readl(control_pbias_offset);
308 reg &= ~OMAP2_PBIASLITEPWRDNZ0;
309 omap_ctrl_writel(reg, control_pbias_offset);
310
311 ret = twl_mmc_set_voltage(c, 0);
312
313 /* 100ms delay required for PBIAS configuration */
314 msleep(100);
315 reg = omap_ctrl_readl(control_pbias_offset);
316 reg |= (OMAP2_PBIASSPEEDCTRL0 | OMAP2_PBIASLITEPWRDNZ0 |
317 OMAP2_PBIASLITEVMODE0);
318 omap_ctrl_writel(reg, control_pbias_offset);
319 }
320
321 return ret;
322}
323
324static int twl_mmc2_set_power(struct device *dev, int slot, int power_on, int vdd)
325{
326 int ret;
327 struct twl_mmc_controller *c = &hsmmc[1];
328 struct omap_mmc_platform_data *mmc = dev->platform_data;
329
David Brownell0329c372009-03-23 18:23:47 -0700330 /*
331 * Assume TWL VMMC2 (hsmmc[1]) is used only to power the card ... OMAP
332 * VDDS is used to power the pins, optionally with a transceiver to
333 * support cards using voltages other than VDDS (1.8V nominal). When a
334 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
335 */
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800336 if (power_on) {
337 if (mmc->slots[0].internal_clock) {
338 u32 reg;
339
340 reg = omap_ctrl_readl(control_devconf1_offset);
341 reg |= OMAP2_MMCSDIO2ADPCLKISEL;
342 omap_ctrl_writel(reg, control_devconf1_offset);
343 }
344 ret = twl_mmc_set_voltage(c, vdd);
345 } else {
346 ret = twl_mmc_set_voltage(c, 0);
347 }
348
349 return ret;
350}
351
Grazvydas Ignotas07d83cc2009-03-23 18:23:47 -0700352static int twl_mmc3_set_power(struct device *dev, int slot, int power_on,
353 int vdd)
354{
355 /*
356 * Assume MMC3 has self-powered device connected, for example on-board
357 * chip with external power source.
358 */
359 return 0;
360}
361
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800362static struct omap_mmc_platform_data *hsmmc_data[OMAP34XX_NR_MMC] __initdata;
363
364void __init twl4030_mmc_init(struct twl4030_hsmmc_info *controllers)
365{
366 struct twl4030_hsmmc_info *c;
367 int nr_hsmmc = ARRAY_SIZE(hsmmc_data);
368
369 if (cpu_is_omap2430()) {
370 control_pbias_offset = OMAP243X_CONTROL_PBIAS_LITE;
371 control_devconf1_offset = OMAP243X_CONTROL_DEVCONF1;
372 nr_hsmmc = 2;
373 } else {
374 control_pbias_offset = OMAP343X_CONTROL_PBIAS_LITE;
375 control_devconf1_offset = OMAP343X_CONTROL_DEVCONF1;
376 }
377
378 for (c = controllers; c->mmc; c++) {
379 struct twl_mmc_controller *twl = hsmmc + c->mmc - 1;
380 struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
381
382 if (!c->mmc || c->mmc > nr_hsmmc) {
383 pr_debug("MMC%d: no such controller\n", c->mmc);
384 continue;
385 }
386 if (mmc) {
387 pr_debug("MMC%d: already configured\n", c->mmc);
388 continue;
389 }
390
391 mmc = kzalloc(sizeof(struct omap_mmc_platform_data), GFP_KERNEL);
392 if (!mmc) {
393 pr_err("Cannot allocate memory for mmc device!\n");
394 return;
395 }
396
Adrian Hunter84660322009-03-23 18:23:46 -0700397 snprintf(twl->name, ARRAY_SIZE(twl->name), "mmc%islot%i",
398 c->mmc, 1);
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800399 mmc->slots[0].name = twl->name;
400 mmc->nr_slots = 1;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800401 mmc->slots[0].wires = c->wires;
402 mmc->slots[0].internal_clock = !c->ext_clock;
403 mmc->dma_mask = 0xffffffff;
404
405 /* note: twl4030 card detect GPIOs normally switch VMMCx ... */
406 if (gpio_is_valid(c->gpio_cd)) {
407 mmc->init = twl_mmc_late_init;
408 mmc->cleanup = twl_mmc_cleanup;
409 mmc->suspend = twl_mmc_suspend;
410 mmc->resume = twl_mmc_resume;
411
412 mmc->slots[0].switch_pin = c->gpio_cd;
413 mmc->slots[0].card_detect_irq = gpio_to_irq(c->gpio_cd);
414 mmc->slots[0].card_detect = twl_mmc_card_detect;
415 } else
416 mmc->slots[0].switch_pin = -EINVAL;
417
418 /* write protect normally uses an OMAP gpio */
419 if (gpio_is_valid(c->gpio_wp)) {
420 gpio_request(c->gpio_wp, "mmc_wp");
421 gpio_direction_input(c->gpio_wp);
422
423 mmc->slots[0].gpio_wp = c->gpio_wp;
424 mmc->slots[0].get_ro = twl_mmc_get_ro;
425 } else
426 mmc->slots[0].gpio_wp = -EINVAL;
427
428 /* NOTE: we assume OMAP's MMC1 and MMC2 use
429 * the TWL4030's VMMC1 and VMMC2, respectively;
Grazvydas Ignotas07d83cc2009-03-23 18:23:47 -0700430 * and that MMC3 device has it's own power source.
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800431 */
432
433 switch (c->mmc) {
434 case 1:
435 mmc->slots[0].set_power = twl_mmc1_set_power;
David Brownell0329c372009-03-23 18:23:47 -0700436 mmc->slots[0].ocr_mask = MMC1_OCR;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800437 break;
438 case 2:
439 mmc->slots[0].set_power = twl_mmc2_set_power;
David Brownell0329c372009-03-23 18:23:47 -0700440 if (c->transceiver)
441 mmc->slots[0].ocr_mask = MMC2_OCR;
442 else
443 mmc->slots[0].ocr_mask = MMC_VDD_165_195;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800444 break;
Grazvydas Ignotas07d83cc2009-03-23 18:23:47 -0700445 case 3:
446 mmc->slots[0].set_power = twl_mmc3_set_power;
447 mmc->slots[0].ocr_mask = MMC_VDD_165_195;
448 break;
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800449 default:
450 pr_err("MMC%d configuration not supported!\n", c->mmc);
Grazvydas Ignotas07d83cc2009-03-23 18:23:47 -0700451 kfree(mmc);
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800452 continue;
453 }
454 hsmmc_data[c->mmc - 1] = mmc;
455 }
456
457 omap2_init_mmc(hsmmc_data, OMAP34XX_NR_MMC);
David Brownell01971f62009-03-23 18:23:47 -0700458
459 /* pass the device nodes back to board setup code */
460 for (c = controllers; c->mmc; c++) {
461 struct omap_mmc_platform_data *mmc = hsmmc_data[c->mmc - 1];
462
463 if (!c->mmc || c->mmc > nr_hsmmc)
464 continue;
465 c->dev = mmc->dev;
466 }
Tony Lindgren90c62bf2008-12-10 17:37:17 -0800467}
468
469#endif