blob: ef8032d2f5bf56f5d39ad36c5ca1049c9f01325d [file] [log] [blame]
Linus Walleijbb3cee22009-04-23 10:22:13 +01001/*
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>
Linus Walleij6ef297f2009-09-22 14:29:36 +010022#include <linux/amba/mmci.h>
Linus Walleijbb3cee22009-04-23 10:22:13 +010023
Linus Walleijbb3cee22009-04-23 10:22:13 +010024#include "mmc.h"
Linus Walleijdf1e0522009-08-10 12:52:40 +010025#include "padmux.h"
Linus Walleijbb3cee22009-04-23 10:22:13 +010026
27struct mmci_card_event {
28 struct input_dev *mmc_input;
29 int mmc_inserted;
30 struct work_struct workq;
Linus Walleij6ef297f2009-09-22 14:29:36 +010031 struct mmci_platform_data mmc0_plat_data;
Linus Walleijbb3cee22009-04-23 10:22:13 +010032};
33
34static 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
Linus Walleijbb3cee22009-04-23 10:22:13 +010043static int mmci_callback(void *data)
44{
45 struct mmci_card_event *mmci_card = data;
46
47 disable_irq_on_gpio_pin(U300_GPIO_PIN_MMC_CD);
48 schedule_work(&mmci_card->workq);
49
50 return 0;
51}
52
53
54static ssize_t gpio_show(struct device *dev, struct device_attribute *attr,
55 char *buf)
56{
57 struct mmci_card_event *mmci_card = container_of(
58 dev->platform_data,
59 struct mmci_card_event, mmc0_plat_data);
60
61
62 return sprintf(buf, "%d\n", !mmci_card->mmc_inserted);
63}
64
65static DEVICE_ATTR(mmc_inserted, S_IRUGO, gpio_show, NULL);
66
67static void _mmci_callback(struct work_struct *ws)
68{
69
70 struct mmci_card_event *mmci_card = container_of(
71 ws,
72 struct mmci_card_event, workq);
73
74 mdelay(20);
75
76 mmci_card->mmc_inserted = !!gpio_get_value(U300_GPIO_PIN_MMC_CD);
77
78 input_report_switch(mmci_card->mmc_input, KEY_INSERT,
79 !mmci_card->mmc_inserted);
80 input_sync(mmci_card->mmc_input);
81
82 pr_debug("MMC/SD card was %s\n",
83 mmci_card->mmc_inserted ? "removed" : "inserted");
84
85 enable_irq_on_gpio_pin(U300_GPIO_PIN_MMC_CD, !mmci_card->mmc_inserted);
86}
87
88int __devinit mmc_init(struct amba_device *adev)
89{
90 struct mmci_card_event *mmci_card;
91 struct device *mmcsd_device = &adev->dev;
Linus Walleijdf1e0522009-08-10 12:52:40 +010092 struct pmx *pmx;
Linus Walleijbb3cee22009-04-23 10:22:13 +010093 int ret = 0;
94
95 mmci_card = kzalloc(sizeof(struct mmci_card_event), GFP_KERNEL);
96 if (!mmci_card)
97 return -ENOMEM;
98
Linus Walleijf9e8eef2009-09-24 21:42:44 +010099 /*
100 * Do not set ocr_mask or voltage translation function,
101 * we have a regulator we can control instead.
102 */
Linus Walleijbb3cee22009-04-23 10:22:13 +0100103 /* Nominally 2.85V on our platform */
Linus Walleijbb3cee22009-04-23 10:22:13 +0100104 mmci_card->mmc0_plat_data.status = mmc_status;
Russell King7fb2bbf2009-07-09 15:15:12 +0100105 mmci_card->mmc0_plat_data.gpio_wp = -1;
106 mmci_card->mmc0_plat_data.gpio_cd = -1;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100107 mmci_card->mmc0_plat_data.capabilities = MMC_CAP_MMC_HIGHSPEED |
Linus Walleij771dc152010-04-08 07:38:52 +0100108 MMC_CAP_SD_HIGHSPEED | MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
Linus Walleijbb3cee22009-04-23 10:22:13 +0100109
110 mmcsd_device->platform_data = (void *) &mmci_card->mmc0_plat_data;
111
112 INIT_WORK(&mmci_card->workq, _mmci_callback);
113
114 ret = gpio_request(U300_GPIO_PIN_MMC_CD, "MMC card detection");
115 if (ret) {
116 printk(KERN_CRIT "Could not allocate MMC card detection " \
117 "GPIO pin\n");
118 goto out;
119 }
120
121 ret = gpio_direction_input(U300_GPIO_PIN_MMC_CD);
122 if (ret) {
123 printk(KERN_CRIT "Invalid GPIO pin requested\n");
124 goto out;
125 }
126
127 ret = sysfs_create_file(&mmcsd_device->kobj,
128 &dev_attr_mmc_inserted.attr);
129 if (ret)
130 goto out;
131
132 mmci_card->mmc_input = input_allocate_device();
133 if (!mmci_card->mmc_input) {
134 printk(KERN_CRIT "Could not allocate MMC input device\n");
135 return -ENOMEM;
136 }
137
138 mmci_card->mmc_input->name = "MMC insert notification";
139 mmci_card->mmc_input->id.bustype = BUS_HOST;
140 mmci_card->mmc_input->id.vendor = 0;
141 mmci_card->mmc_input->id.product = 0;
142 mmci_card->mmc_input->id.version = 0x0100;
143 mmci_card->mmc_input->dev.parent = mmcsd_device;
144 input_set_capability(mmci_card->mmc_input, EV_SW, KEY_INSERT);
145
146 /*
147 * Since this must always be compiled into the kernel, this input
148 * is never unregistered or free:ed.
149 */
150 ret = input_register_device(mmci_card->mmc_input);
151 if (ret) {
152 input_free_device(mmci_card->mmc_input);
153 goto out;
154 }
155
156 input_set_drvdata(mmci_card->mmc_input, mmci_card);
157
Linus Walleijdf1e0522009-08-10 12:52:40 +0100158 /*
159 * Setup padmuxing for MMC. Since this must always be
160 * compiled into the kernel, pmx is never released.
161 */
162 pmx = pmx_get(mmcsd_device, U300_APP_PMX_MMC_SETTING);
163
164 if (IS_ERR(pmx))
165 pr_warning("Could not get padmux handle\n");
166 else {
167 ret = pmx_activate(mmcsd_device, pmx);
168 if (IS_ERR_VALUE(ret))
169 pr_warning("Could not activate padmuxing\n");
170 }
171
Linus Walleijbb3cee22009-04-23 10:22:13 +0100172 ret = gpio_register_callback(U300_GPIO_PIN_MMC_CD, mmci_callback,
173 mmci_card);
174
175 schedule_work(&mmci_card->workq);
176
177 printk(KERN_INFO "Registered MMC insert/remove notification\n");
178out:
179 return ret;
180}