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