Merge branch 'sh/dwarf-unwinder'
[pandora-kernel.git] / arch / arm / mach-u300 / mmc.c
1 /*
2  *
3  * arch/arm/mach-u300/mmc.c
4  *
5  *
6  * Copyright (C) 2009 ST-Ericsson AB
7  * License terms: GNU General Public License (GPL) version 2
8  *
9  * Author: Linus Walleij <linus.walleij@stericsson.com>
10  * Author: Johan Lundin <johan.lundin@stericsson.com>
11  * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
12  */
13 #include <linux/device.h>
14 #include <linux/amba/bus.h>
15 #include <linux/mmc/host.h>
16 #include <linux/input.h>
17 #include <linux/workqueue.h>
18 #include <linux/delay.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/regulator/machine.h>
21 #include <linux/gpio.h>
22 #include <linux/amba/mmci.h>
23
24 #include "mmc.h"
25 #include "padmux.h"
26
27 struct mmci_card_event {
28         struct input_dev *mmc_input;
29         int mmc_inserted;
30         struct work_struct workq;
31         struct mmci_platform_data mmc0_plat_data;
32 };
33
34 static unsigned int mmc_status(struct device *dev)
35 {
36         struct mmci_card_event *mmci_card = container_of(
37                 dev->platform_data,
38                 struct mmci_card_event, mmc0_plat_data);
39
40         return mmci_card->mmc_inserted;
41 }
42
43 /*
44  * Here follows a large chunk of code which will only be enabled if you
45  * have both the AB3100 chip mounted and the MMC subsystem activated.
46  */
47
48 static u32 mmc_translate_vdd(struct device *dev, unsigned int voltage)
49 {
50         int v;
51
52         /*
53          * MMC Spec:
54          * bit 7:       1.70 - 1.95V
55          * bit 8 - 14:  2.0 - 2.6V
56          * bit 15 - 23: 2.7 - 3.6V
57          *
58          * ab3100 voltages:
59          * 000 - 2.85V
60          * 001 - 2.75V
61          * 010 - 1.8V
62          * 011 - 1.5V
63          */
64         switch (voltage) {
65         case 8:
66                 v = 3;
67                 break;
68         case 9:
69         case 10:
70         case 11:
71         case 12:
72         case 13:
73         case 14:
74         case 15:
75                 v = 1;
76                 break;
77         case 16:
78                 v = 1;
79                 break;
80         case 17:
81         case 18:
82         case 19:
83         case 20:
84         case 21:
85         case 22:
86         case 23:
87         case 24:
88                 v = 0;
89                 break;
90         default:
91                 v = 0;
92                 break;
93         }
94
95         /* PL180 voltage register bits */
96         return v << 2;
97 }
98
99
100
101 static int mmci_callback(void *data)
102 {
103         struct mmci_card_event *mmci_card = data;
104
105         disable_irq_on_gpio_pin(U300_GPIO_PIN_MMC_CD);
106         schedule_work(&mmci_card->workq);
107
108         return 0;
109 }
110
111
112 static ssize_t gpio_show(struct device *dev, struct device_attribute *attr,
113                   char *buf)
114 {
115         struct mmci_card_event *mmci_card = container_of(
116                 dev->platform_data,
117                 struct mmci_card_event, mmc0_plat_data);
118
119
120         return sprintf(buf, "%d\n", !mmci_card->mmc_inserted);
121 }
122
123 static DEVICE_ATTR(mmc_inserted, S_IRUGO, gpio_show, NULL);
124
125 static void _mmci_callback(struct work_struct *ws)
126 {
127
128         struct mmci_card_event *mmci_card = container_of(
129                 ws,
130                 struct mmci_card_event, workq);
131
132         mdelay(20);
133
134         mmci_card->mmc_inserted = !!gpio_get_value(U300_GPIO_PIN_MMC_CD);
135
136         input_report_switch(mmci_card->mmc_input, KEY_INSERT,
137                             !mmci_card->mmc_inserted);
138         input_sync(mmci_card->mmc_input);
139
140         pr_debug("MMC/SD card was %s\n",
141                  mmci_card->mmc_inserted ? "removed" : "inserted");
142
143         enable_irq_on_gpio_pin(U300_GPIO_PIN_MMC_CD, !mmci_card->mmc_inserted);
144 }
145
146 int __devinit mmc_init(struct amba_device *adev)
147 {
148         struct mmci_card_event *mmci_card;
149         struct device *mmcsd_device = &adev->dev;
150         struct pmx *pmx;
151         int ret = 0;
152
153         mmci_card = kzalloc(sizeof(struct mmci_card_event), GFP_KERNEL);
154         if (!mmci_card)
155                 return -ENOMEM;
156
157         /* Nominally 2.85V on our platform */
158         mmci_card->mmc0_plat_data.ocr_mask = MMC_VDD_28_29;
159         mmci_card->mmc0_plat_data.translate_vdd = mmc_translate_vdd;
160         mmci_card->mmc0_plat_data.status = mmc_status;
161         mmci_card->mmc0_plat_data.gpio_wp = -1;
162         mmci_card->mmc0_plat_data.gpio_cd = -1;
163         mmci_card->mmc0_plat_data.capabilities = MMC_CAP_MMC_HIGHSPEED |
164                 MMC_CAP_SD_HIGHSPEED | MMC_CAP_4_BIT_DATA;
165
166         mmcsd_device->platform_data = (void *) &mmci_card->mmc0_plat_data;
167
168         INIT_WORK(&mmci_card->workq, _mmci_callback);
169
170         ret = gpio_request(U300_GPIO_PIN_MMC_CD, "MMC card detection");
171         if (ret) {
172                 printk(KERN_CRIT "Could not allocate MMC card detection " \
173                        "GPIO pin\n");
174                 goto out;
175         }
176
177         ret = gpio_direction_input(U300_GPIO_PIN_MMC_CD);
178         if (ret) {
179                 printk(KERN_CRIT "Invalid GPIO pin requested\n");
180                 goto out;
181         }
182
183         ret = sysfs_create_file(&mmcsd_device->kobj,
184                                &dev_attr_mmc_inserted.attr);
185         if (ret)
186                 goto out;
187
188         mmci_card->mmc_input = input_allocate_device();
189         if (!mmci_card->mmc_input) {
190                 printk(KERN_CRIT "Could not allocate MMC input device\n");
191                 return -ENOMEM;
192         }
193
194         mmci_card->mmc_input->name = "MMC insert notification";
195         mmci_card->mmc_input->id.bustype = BUS_HOST;
196         mmci_card->mmc_input->id.vendor = 0;
197         mmci_card->mmc_input->id.product = 0;
198         mmci_card->mmc_input->id.version = 0x0100;
199         mmci_card->mmc_input->dev.parent = mmcsd_device;
200         input_set_capability(mmci_card->mmc_input, EV_SW, KEY_INSERT);
201
202         /*
203          * Since this must always be compiled into the kernel, this input
204          * is never unregistered or free:ed.
205          */
206         ret = input_register_device(mmci_card->mmc_input);
207         if (ret) {
208                 input_free_device(mmci_card->mmc_input);
209                 goto out;
210         }
211
212         input_set_drvdata(mmci_card->mmc_input, mmci_card);
213
214         /*
215          * Setup padmuxing for MMC. Since this must always be
216          * compiled into the kernel, pmx is never released.
217          */
218         pmx = pmx_get(mmcsd_device, U300_APP_PMX_MMC_SETTING);
219
220         if (IS_ERR(pmx))
221                 pr_warning("Could not get padmux handle\n");
222         else {
223                 ret = pmx_activate(mmcsd_device, pmx);
224                 if (IS_ERR_VALUE(ret))
225                         pr_warning("Could not activate padmuxing\n");
226         }
227
228         ret = gpio_register_callback(U300_GPIO_PIN_MMC_CD, mmci_callback,
229                                      mmci_card);
230
231         schedule_work(&mmci_card->workq);
232
233         printk(KERN_INFO "Registered MMC insert/remove notification\n");
234 out:
235         return ret;
236 }