Merge current mainline tree into linux-omap tree
[pandora-kernel.git] / arch / arm / mach-omap2 / board-n800-mmc.c
1 /*
2  * linux/arch/arm/mach-omap2/board-n800-mmc.c
3  *
4  * Copyright (C) 2006 Nokia Corporation
5  * Author: Juha Yrjola
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
12 #include <linux/delay.h>
13 #include <linux/platform_device.h>
14 #include <linux/i2c/menelaus.h>
15
16 #include <asm/mach-types.h>
17
18 #include <mach/mmc.h>
19 #include <mach/gpio.h>
20
21 #if defined(CONFIG_MMC_OMAP) || defined(CONFIG_MMC_OMAP_MODULE)
22
23 static const int slot_switch_gpio = 96;
24
25 static const int n810_slot2_pw_vddf = 23;
26 static const int n810_slot2_pw_vdd = 9;
27
28 static int slot1_cover_open;
29 static int slot2_cover_open;
30 static struct device *mmc_device;
31
32 /*
33  * VMMC   --> slot 1 (N800 & N810)
34  * VDCDC3_APE, VMCS2_APE --> slot 2 on N800
35  * GPIO96 --> Menelaus GPIO2
36  * GPIO23 --> controls slot2 VSD    (N810 only)
37  * GPIO9  --> controls slot2 VIO_SD (N810 only)
38  */
39
40 static int n800_mmc_switch_slot(struct device *dev, int slot)
41 {
42 #ifdef CONFIG_MMC_DEBUG
43         dev_dbg(dev, "Choose slot %d\n", slot + 1);
44 #endif
45         if (slot == 0)
46                 omap_set_gpio_dataout(slot_switch_gpio, 0);
47         else
48                 omap_set_gpio_dataout(slot_switch_gpio, 1);
49         return 0;
50 }
51
52 static int n800_mmc_set_power_menelaus(struct device *dev, int slot,
53                                         int power_on, int vdd)
54 {
55         int mV;
56
57 #ifdef CONFIG_MMC_DEBUG
58         dev_dbg(dev, "Set slot %d power: %s (vdd %d)\n", slot + 1,
59                 power_on ? "on" : "off", vdd);
60 #endif
61         if (slot == 0) {
62                 if (!power_on)
63                         return menelaus_set_vmmc(0);
64                 switch (1 << vdd) {
65                 case MMC_VDD_33_34:
66                 case MMC_VDD_32_33:
67                 case MMC_VDD_31_32:
68                         mV = 3100;
69                         break;
70                 case MMC_VDD_30_31:
71                         mV = 3000;
72                         break;
73                 case MMC_VDD_28_29:
74                         mV = 2800;
75                         break;
76                 case MMC_VDD_165_195:
77                         mV = 1850;
78                         break;
79                 default:
80                         BUG();
81                 }
82                 return menelaus_set_vmmc(mV);
83         } else {
84                 if (!power_on)
85                         return menelaus_set_vdcdc(3, 0);
86                 switch (1 << vdd) {
87                 case MMC_VDD_33_34:
88                 case MMC_VDD_32_33:
89                         mV = 3300;
90                         break;
91                 case MMC_VDD_30_31:
92                 case MMC_VDD_29_30:
93                         mV = 3000;
94                         break;
95                 case MMC_VDD_28_29:
96                 case MMC_VDD_27_28:
97                         mV = 2800;
98                         break;
99                 case MMC_VDD_24_25:
100                 case MMC_VDD_23_24:
101                         mV = 2400;
102                         break;
103                 case MMC_VDD_22_23:
104                 case MMC_VDD_21_22:
105                         mV = 2200;
106                         break;
107                 case MMC_VDD_20_21:
108                         mV = 2000;
109                         break;
110                 case MMC_VDD_165_195:
111                         mV = 1800;
112                         break;
113                 default:
114                         BUG();
115                 }
116                 return menelaus_set_vdcdc(3, mV);
117         }
118         return 0;
119 }
120
121 static void nokia_mmc_set_power_internal(struct device *dev,
122                                          int power_on)
123 {
124         dev_dbg(dev, "Set internal slot power %s\n",
125                 power_on ? "on" : "off");
126
127         if (power_on) {
128                 omap_set_gpio_dataout(n810_slot2_pw_vddf, 1);
129                 udelay(30);
130                 omap_set_gpio_dataout(n810_slot2_pw_vdd, 1);
131                 udelay(100);
132         } else {
133                 omap_set_gpio_dataout(n810_slot2_pw_vdd, 0);
134                 msleep(50);
135                 omap_set_gpio_dataout(n810_slot2_pw_vddf, 0);
136                 msleep(50);
137         }
138 }
139
140 static int n800_mmc_set_power(struct device *dev, int slot, int power_on,
141                               int vdd)
142 {
143         if (machine_is_nokia_n800() || slot == 0)
144                 return n800_mmc_set_power_menelaus(dev, slot, power_on, vdd);
145
146         nokia_mmc_set_power_internal(dev, power_on);
147
148         return 0;
149 }
150
151 static int n800_mmc_set_bus_mode(struct device *dev, int slot, int bus_mode)
152 {
153         int r;
154
155         dev_dbg(dev, "Set slot %d bus mode %s\n", slot + 1,
156                 bus_mode == MMC_BUSMODE_OPENDRAIN ? "open-drain" : "push-pull");
157         BUG_ON(slot != 0 && slot != 1);
158         slot++;
159         switch (bus_mode) {
160         case MMC_BUSMODE_OPENDRAIN:
161                 r = menelaus_set_mmc_opendrain(slot, 1);
162                 break;
163         case MMC_BUSMODE_PUSHPULL:
164                 r = menelaus_set_mmc_opendrain(slot, 0);
165                 break;
166         default:
167                 BUG();
168         }
169         if (r != 0 && printk_ratelimit())
170                 dev_err(dev, "MMC: unable to set bus mode for slot %d\n",
171                         slot);
172         return r;
173 }
174
175 static int n800_mmc_get_cover_state(struct device *dev, int slot)
176 {
177         slot++;
178         BUG_ON(slot != 1 && slot != 2);
179         if (slot == 1)
180                 return slot1_cover_open;
181         else
182                 return slot2_cover_open;
183 }
184
185 static void n800_mmc_callback(void *data, u8 card_mask)
186 {
187         int bit, *openp, index;
188
189         if (machine_is_nokia_n800()) {
190                 bit = 1 << 1;
191                 openp = &slot2_cover_open;
192                 index = 1;
193         } else {
194                 bit = 1;
195                 openp = &slot1_cover_open;
196                 index = 0;
197         }
198
199         if (card_mask & bit)
200                 *openp = 1;
201         else
202                 *openp = 0;
203
204         omap_mmc_notify_cover_event(mmc_device, index, *openp);
205 }
206
207 void n800_mmc_slot1_cover_handler(void *arg, int closed_state)
208 {
209         if (mmc_device == NULL)
210                 return;
211
212         slot1_cover_open = !closed_state;
213         omap_mmc_notify_cover_event(mmc_device, 0, closed_state);
214 }
215
216 static int n800_mmc_late_init(struct device *dev)
217 {
218         int r, bit, *openp;
219         int vs2sel;
220
221         mmc_device = dev;
222
223         r = menelaus_set_slot_sel(1);
224         if (r < 0)
225                 return r;
226
227         if (machine_is_nokia_n800())
228                 vs2sel = 0;
229         else
230                 vs2sel = 2;
231
232         r = menelaus_set_mmc_slot(2, 0, vs2sel, 1);
233         if (r < 0)
234                 return r;
235
236         n800_mmc_set_power(dev, 0, MMC_POWER_ON, 16); /* MMC_VDD_28_29 */
237         n800_mmc_set_power(dev, 1, MMC_POWER_ON, 16);
238
239         r = menelaus_set_mmc_slot(1, 1, 0, 1);
240         if (r < 0)
241                 return r;
242         r = menelaus_set_mmc_slot(2, 1, vs2sel, 1);
243         if (r < 0)
244                 return r;
245
246         r = menelaus_get_slot_pin_states();
247         if (r < 0)
248                 return r;
249
250         if (machine_is_nokia_n800()) {
251                 bit = 1 << 1;
252                 openp = &slot2_cover_open;
253         } else {
254                 bit = 1;
255                 openp = &slot1_cover_open;
256                 slot2_cover_open = 0;
257         }
258
259         /* All slot pin bits seem to be inversed until first swith change */
260         if (r == 0xf || r == (0xf & ~bit))
261                 r = ~r;
262
263         if (r & bit)
264                 *openp = 1;
265         else
266                 *openp = 0;
267
268         r = menelaus_register_mmc_callback(n800_mmc_callback, NULL);
269
270         return r;
271 }
272
273 static void n800_mmc_shutdown(struct device *dev)
274 {
275         int vs2sel;
276
277         if (machine_is_nokia_n800())
278                 vs2sel = 0;
279         else
280                 vs2sel = 2;
281
282         menelaus_set_mmc_slot(1, 0, 0, 0);
283         menelaus_set_mmc_slot(2, 0, vs2sel, 0);
284 }
285
286 static void n800_mmc_cleanup(struct device *dev)
287 {
288         menelaus_unregister_mmc_callback();
289
290         omap_free_gpio(slot_switch_gpio);
291
292         if (machine_is_nokia_n810()) {
293                 omap_free_gpio(n810_slot2_pw_vddf);
294                 omap_free_gpio(n810_slot2_pw_vdd);
295         }
296 }
297
298 /*
299  * MMC controller1 has two slots that are multiplexed via I2C.
300  * MMC controller2 is not in use.
301  */
302 static struct omap_mmc_platform_data mmc1_data = {
303         .nr_slots               = 2,
304         .switch_slot            = n800_mmc_switch_slot,
305         .init                   = n800_mmc_late_init,
306         .cleanup                = n800_mmc_cleanup,
307         .shutdown               = n800_mmc_shutdown,
308         .max_freq               = 24000000,
309         .dma_mask               = 0xffffffff,
310         .slots[0] = {
311                 .wire4          = 1,
312                 .set_power      = n800_mmc_set_power,
313                 .set_bus_mode   = n800_mmc_set_bus_mode,
314                 .get_cover_state= n800_mmc_get_cover_state,
315                 .ocr_mask       = MMC_VDD_165_195 | MMC_VDD_30_31 |
316                                   MMC_VDD_32_33   | MMC_VDD_33_34,
317                 .name           = "internal",
318         },
319         .slots[1] = {
320                 .set_power      = n800_mmc_set_power,
321                 .set_bus_mode   = n800_mmc_set_bus_mode,
322                 .get_cover_state= n800_mmc_get_cover_state,
323                 .ocr_mask       = MMC_VDD_165_195 | MMC_VDD_20_21 |
324                                   MMC_VDD_21_22 | MMC_VDD_22_23 | MMC_VDD_23_24 |
325                                   MMC_VDD_24_25 | MMC_VDD_27_28 | MMC_VDD_28_29 |
326                                   MMC_VDD_29_30 | MMC_VDD_30_31 | MMC_VDD_32_33 |
327                                   MMC_VDD_33_34,
328                 .name           = "external",
329         },
330 };
331
332 static struct omap_mmc_platform_data *mmc_data[OMAP24XX_NR_MMC];
333
334 void __init n800_mmc_init(void)
335
336 {
337         if (machine_is_nokia_n810()) {
338                 mmc1_data.slots[0].name = "external";
339
340                 /*
341                  * Some Samsung Movinand chips do not like open-ended
342                  * multi-block reads and fall to braind-dead state
343                  * while doing so. Reducing the number of blocks in
344                  * the transfer or delays in clock disable do not help
345                  */
346                 mmc1_data.slots[1].name = "internal";
347                 mmc1_data.slots[1].ban_openended = 1;
348         }
349
350         if (omap_request_gpio(slot_switch_gpio) < 0)
351                 BUG();
352         omap_set_gpio_dataout(slot_switch_gpio, 0);
353         omap_set_gpio_direction(slot_switch_gpio, 0);
354
355         if (machine_is_nokia_n810()) {
356                 if (omap_request_gpio(n810_slot2_pw_vddf) < 0)
357                         BUG();
358                 omap_set_gpio_dataout(n810_slot2_pw_vddf, 0);
359                 omap_set_gpio_direction(n810_slot2_pw_vddf, 0);
360
361                 if (omap_request_gpio(n810_slot2_pw_vdd) < 0)
362                         BUG();
363                 omap_set_gpio_dataout(n810_slot2_pw_vdd, 0);
364                 omap_set_gpio_direction(n810_slot2_pw_vdd, 0);
365         }
366
367         mmc_data[0] = &mmc1_data;
368         omap2_init_mmc(mmc_data, OMAP24XX_NR_MMC);
369 }
370 #else
371
372 void __init n800_mmc_init(void)
373 {
374 }
375
376 void n800_mmc_slot1_cover_handler(void *arg, int state)
377 {
378 }
379
380 #endif