Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 1 | /* |
| 2 | * OpenFirmware bindings for the MMC-over-SPI driver |
| 3 | * |
| 4 | * Copyright (c) MontaVista Software, Inc. 2008. |
| 5 | * |
| 6 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
David Miller | b2fce6a | 2011-03-18 15:00:23 -0700 | [diff] [blame] | 18 | #include <linux/irq.h> |
Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 19 | #include <linux/gpio.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/of_gpio.h> |
David Miller | b2fce6a | 2011-03-18 15:00:23 -0700 | [diff] [blame] | 22 | #include <linux/of_irq.h> |
Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 23 | #include <linux/spi/spi.h> |
| 24 | #include <linux/spi/mmc_spi.h> |
| 25 | #include <linux/mmc/core.h> |
| 26 | #include <linux/mmc/host.h> |
| 27 | |
Wanlong Gao | b9c350a | 2011-05-24 21:03:22 +0800 | [diff] [blame] | 28 | /* For archs that don't support NO_IRQ (such as mips), provide a dummy value */ |
| 29 | #ifndef NO_IRQ |
| 30 | #define NO_IRQ 0 |
| 31 | #endif |
| 32 | |
Grant Likely | 02d9e58 | 2009-11-04 22:43:35 -0700 | [diff] [blame] | 33 | MODULE_LICENSE("GPL"); |
| 34 | |
Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 35 | enum { |
| 36 | CD_GPIO = 0, |
| 37 | WP_GPIO, |
| 38 | NUM_GPIOS, |
| 39 | }; |
| 40 | |
| 41 | struct of_mmc_spi { |
| 42 | int gpios[NUM_GPIOS]; |
| 43 | bool alow_gpios[NUM_GPIOS]; |
Esben Haabendal | 290293e | 2011-03-07 20:45:28 -0700 | [diff] [blame] | 44 | int detect_irq; |
Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 45 | struct mmc_spi_platform_data pdata; |
| 46 | }; |
| 47 | |
| 48 | static struct of_mmc_spi *to_of_mmc_spi(struct device *dev) |
| 49 | { |
| 50 | return container_of(dev->platform_data, struct of_mmc_spi, pdata); |
| 51 | } |
| 52 | |
| 53 | static int of_mmc_spi_read_gpio(struct device *dev, int gpio_num) |
| 54 | { |
| 55 | struct of_mmc_spi *oms = to_of_mmc_spi(dev); |
| 56 | bool active_low = oms->alow_gpios[gpio_num]; |
| 57 | bool value = gpio_get_value(oms->gpios[gpio_num]); |
| 58 | |
| 59 | return active_low ^ value; |
| 60 | } |
| 61 | |
| 62 | static int of_mmc_spi_get_cd(struct device *dev) |
| 63 | { |
| 64 | return of_mmc_spi_read_gpio(dev, CD_GPIO); |
| 65 | } |
| 66 | |
| 67 | static int of_mmc_spi_get_ro(struct device *dev) |
| 68 | { |
| 69 | return of_mmc_spi_read_gpio(dev, WP_GPIO); |
| 70 | } |
| 71 | |
Esben Haabendal | 290293e | 2011-03-07 20:45:28 -0700 | [diff] [blame] | 72 | static int of_mmc_spi_init(struct device *dev, |
| 73 | irqreturn_t (*irqhandler)(int, void *), void *mmc) |
| 74 | { |
| 75 | struct of_mmc_spi *oms = to_of_mmc_spi(dev); |
| 76 | |
| 77 | return request_threaded_irq(oms->detect_irq, NULL, irqhandler, 0, |
| 78 | dev_name(dev), mmc); |
| 79 | } |
| 80 | |
| 81 | static void of_mmc_spi_exit(struct device *dev, void *mmc) |
| 82 | { |
| 83 | struct of_mmc_spi *oms = to_of_mmc_spi(dev); |
| 84 | |
| 85 | free_irq(oms->detect_irq, mmc); |
| 86 | } |
| 87 | |
Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 88 | struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi) |
| 89 | { |
| 90 | struct device *dev = &spi->dev; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 91 | struct device_node *np = dev->of_node; |
Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 92 | struct of_mmc_spi *oms; |
| 93 | const u32 *voltage_ranges; |
| 94 | int num_ranges; |
| 95 | int i; |
| 96 | int ret = -EINVAL; |
| 97 | |
| 98 | if (dev->platform_data || !np) |
| 99 | return dev->platform_data; |
| 100 | |
| 101 | oms = kzalloc(sizeof(*oms), GFP_KERNEL); |
| 102 | if (!oms) |
| 103 | return NULL; |
| 104 | |
| 105 | voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges); |
| 106 | num_ranges = num_ranges / sizeof(*voltage_ranges) / 2; |
| 107 | if (!voltage_ranges || !num_ranges) { |
| 108 | dev_err(dev, "OF: voltage-ranges unspecified\n"); |
| 109 | goto err_ocr; |
| 110 | } |
| 111 | |
| 112 | for (i = 0; i < num_ranges; i++) { |
| 113 | const int j = i * 2; |
| 114 | u32 mask; |
| 115 | |
Jean-Christophe PLAGNIOL-VILLARD | b6bf30d | 2012-01-30 05:15:29 +0100 | [diff] [blame] | 116 | mask = mmc_vddrange_to_ocrmask(be32_to_cpu(voltage_ranges[j]), |
| 117 | be32_to_cpu(voltage_ranges[j + 1])); |
Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 118 | if (!mask) { |
| 119 | ret = -EINVAL; |
| 120 | dev_err(dev, "OF: voltage-range #%d is invalid\n", i); |
| 121 | goto err_ocr; |
| 122 | } |
| 123 | oms->pdata.ocr_mask |= mask; |
| 124 | } |
| 125 | |
| 126 | for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) { |
| 127 | enum of_gpio_flags gpio_flags; |
| 128 | |
| 129 | oms->gpios[i] = of_get_gpio_flags(np, i, &gpio_flags); |
| 130 | if (!gpio_is_valid(oms->gpios[i])) |
| 131 | continue; |
| 132 | |
Kay Sievers | 48f8151 | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 133 | ret = gpio_request(oms->gpios[i], dev_name(dev)); |
Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 134 | if (ret < 0) { |
| 135 | oms->gpios[i] = -EINVAL; |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | if (gpio_flags & OF_GPIO_ACTIVE_LOW) |
| 140 | oms->alow_gpios[i] = true; |
| 141 | } |
| 142 | |
| 143 | if (gpio_is_valid(oms->gpios[CD_GPIO])) |
| 144 | oms->pdata.get_cd = of_mmc_spi_get_cd; |
| 145 | if (gpio_is_valid(oms->gpios[WP_GPIO])) |
| 146 | oms->pdata.get_ro = of_mmc_spi_get_ro; |
| 147 | |
Esben Haabendal | 290293e | 2011-03-07 20:45:28 -0700 | [diff] [blame] | 148 | oms->detect_irq = irq_of_parse_and_map(np, 0); |
Roland Stigge | 073350f | 2013-01-30 18:05:08 +0100 | [diff] [blame^] | 149 | if (oms->detect_irq != 0) { |
Esben Haabendal | 290293e | 2011-03-07 20:45:28 -0700 | [diff] [blame] | 150 | oms->pdata.init = of_mmc_spi_init; |
| 151 | oms->pdata.exit = of_mmc_spi_exit; |
| 152 | } else { |
| 153 | oms->pdata.caps |= MMC_CAP_NEEDS_POLL; |
| 154 | } |
Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 155 | |
| 156 | dev->platform_data = &oms->pdata; |
| 157 | return dev->platform_data; |
| 158 | err_ocr: |
| 159 | kfree(oms); |
| 160 | return NULL; |
| 161 | } |
| 162 | EXPORT_SYMBOL(mmc_spi_get_pdata); |
| 163 | |
| 164 | void mmc_spi_put_pdata(struct spi_device *spi) |
| 165 | { |
| 166 | struct device *dev = &spi->dev; |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 167 | struct device_node *np = dev->of_node; |
Anton Vorontsov | 9c43df5 | 2008-12-30 18:15:28 +0300 | [diff] [blame] | 168 | struct of_mmc_spi *oms = to_of_mmc_spi(dev); |
| 169 | int i; |
| 170 | |
| 171 | if (!dev->platform_data || !np) |
| 172 | return; |
| 173 | |
| 174 | for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) { |
| 175 | if (gpio_is_valid(oms->gpios[i])) |
| 176 | gpio_free(oms->gpios[i]); |
| 177 | } |
| 178 | kfree(oms); |
| 179 | dev->platform_data = NULL; |
| 180 | } |
| 181 | EXPORT_SYMBOL(mmc_spi_put_pdata); |