checkpatch: handle missing #if open in context
[linux-2.6/mini2440.git] / drivers / mmc / host / of_mmc_spi.c
blobfb2921f8099dc82910bfb6cc992eddeeae5eda15
1 /*
2 * OpenFirmware bindings for the MMC-over-SPI driver
4 * Copyright (c) MontaVista Software, Inc. 2008.
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
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.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/gpio.h>
18 #include <linux/of.h>
19 #include <linux/of_gpio.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/mmc_spi.h>
22 #include <linux/mmc/core.h>
23 #include <linux/mmc/host.h>
25 enum {
26 CD_GPIO = 0,
27 WP_GPIO,
28 NUM_GPIOS,
31 struct of_mmc_spi {
32 int gpios[NUM_GPIOS];
33 bool alow_gpios[NUM_GPIOS];
34 struct mmc_spi_platform_data pdata;
37 static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
39 return container_of(dev->platform_data, struct of_mmc_spi, pdata);
42 static int of_mmc_spi_read_gpio(struct device *dev, int gpio_num)
44 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
45 bool active_low = oms->alow_gpios[gpio_num];
46 bool value = gpio_get_value(oms->gpios[gpio_num]);
48 return active_low ^ value;
51 static int of_mmc_spi_get_cd(struct device *dev)
53 return of_mmc_spi_read_gpio(dev, CD_GPIO);
56 static int of_mmc_spi_get_ro(struct device *dev)
58 return of_mmc_spi_read_gpio(dev, WP_GPIO);
61 struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
63 struct device *dev = &spi->dev;
64 struct device_node *np = dev_archdata_get_node(&dev->archdata);
65 struct of_mmc_spi *oms;
66 const u32 *voltage_ranges;
67 int num_ranges;
68 int i;
69 int ret = -EINVAL;
71 if (dev->platform_data || !np)
72 return dev->platform_data;
74 oms = kzalloc(sizeof(*oms), GFP_KERNEL);
75 if (!oms)
76 return NULL;
78 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
79 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
80 if (!voltage_ranges || !num_ranges) {
81 dev_err(dev, "OF: voltage-ranges unspecified\n");
82 goto err_ocr;
85 for (i = 0; i < num_ranges; i++) {
86 const int j = i * 2;
87 u32 mask;
89 mask = mmc_vddrange_to_ocrmask(voltage_ranges[j],
90 voltage_ranges[j + 1]);
91 if (!mask) {
92 ret = -EINVAL;
93 dev_err(dev, "OF: voltage-range #%d is invalid\n", i);
94 goto err_ocr;
96 oms->pdata.ocr_mask |= mask;
99 for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
100 enum of_gpio_flags gpio_flags;
102 oms->gpios[i] = of_get_gpio_flags(np, i, &gpio_flags);
103 if (!gpio_is_valid(oms->gpios[i]))
104 continue;
106 ret = gpio_request(oms->gpios[i], dev->bus_id);
107 if (ret < 0) {
108 oms->gpios[i] = -EINVAL;
109 continue;
112 if (gpio_flags & OF_GPIO_ACTIVE_LOW)
113 oms->alow_gpios[i] = true;
116 if (gpio_is_valid(oms->gpios[CD_GPIO]))
117 oms->pdata.get_cd = of_mmc_spi_get_cd;
118 if (gpio_is_valid(oms->gpios[WP_GPIO]))
119 oms->pdata.get_ro = of_mmc_spi_get_ro;
121 /* We don't support interrupts yet, let's poll. */
122 oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
124 dev->platform_data = &oms->pdata;
125 return dev->platform_data;
126 err_ocr:
127 kfree(oms);
128 return NULL;
130 EXPORT_SYMBOL(mmc_spi_get_pdata);
132 void mmc_spi_put_pdata(struct spi_device *spi)
134 struct device *dev = &spi->dev;
135 struct device_node *np = dev_archdata_get_node(&dev->archdata);
136 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
137 int i;
139 if (!dev->platform_data || !np)
140 return;
142 for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
143 if (gpio_is_valid(oms->gpios[i]))
144 gpio_free(oms->gpios[i]);
146 kfree(oms);
147 dev->platform_data = NULL;
149 EXPORT_SYMBOL(mmc_spi_put_pdata);