mmc: of_mmc_spi: Need to include irq.h and of_irq.h
[~shefty/rdma-dev.git] / drivers / mmc / host / of_mmc_spi.c
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>
17 #include <linux/slab.h>
18 #include <linux/irq.h>
19 #include <linux/gpio.h>
20 #include <linux/of.h>
21 #include <linux/of_gpio.h>
22 #include <linux/of_irq.h>
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
28 MODULE_LICENSE("GPL");
29
30 enum {
31         CD_GPIO = 0,
32         WP_GPIO,
33         NUM_GPIOS,
34 };
35
36 struct of_mmc_spi {
37         int gpios[NUM_GPIOS];
38         bool alow_gpios[NUM_GPIOS];
39         int detect_irq;
40         struct mmc_spi_platform_data pdata;
41 };
42
43 static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
44 {
45         return container_of(dev->platform_data, struct of_mmc_spi, pdata);
46 }
47
48 static int of_mmc_spi_read_gpio(struct device *dev, int gpio_num)
49 {
50         struct of_mmc_spi *oms = to_of_mmc_spi(dev);
51         bool active_low = oms->alow_gpios[gpio_num];
52         bool value = gpio_get_value(oms->gpios[gpio_num]);
53
54         return active_low ^ value;
55 }
56
57 static int of_mmc_spi_get_cd(struct device *dev)
58 {
59         return of_mmc_spi_read_gpio(dev, CD_GPIO);
60 }
61
62 static int of_mmc_spi_get_ro(struct device *dev)
63 {
64         return of_mmc_spi_read_gpio(dev, WP_GPIO);
65 }
66
67 static int of_mmc_spi_init(struct device *dev,
68                            irqreturn_t (*irqhandler)(int, void *), void *mmc)
69 {
70         struct of_mmc_spi *oms = to_of_mmc_spi(dev);
71
72         return request_threaded_irq(oms->detect_irq, NULL, irqhandler, 0,
73                                     dev_name(dev), mmc);
74 }
75
76 static void of_mmc_spi_exit(struct device *dev, void *mmc)
77 {
78         struct of_mmc_spi *oms = to_of_mmc_spi(dev);
79
80         free_irq(oms->detect_irq, mmc);
81 }
82
83 struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
84 {
85         struct device *dev = &spi->dev;
86         struct device_node *np = dev->of_node;
87         struct of_mmc_spi *oms;
88         const u32 *voltage_ranges;
89         int num_ranges;
90         int i;
91         int ret = -EINVAL;
92
93         if (dev->platform_data || !np)
94                 return dev->platform_data;
95
96         oms = kzalloc(sizeof(*oms), GFP_KERNEL);
97         if (!oms)
98                 return NULL;
99
100         voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
101         num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
102         if (!voltage_ranges || !num_ranges) {
103                 dev_err(dev, "OF: voltage-ranges unspecified\n");
104                 goto err_ocr;
105         }
106
107         for (i = 0; i < num_ranges; i++) {
108                 const int j = i * 2;
109                 u32 mask;
110
111                 mask = mmc_vddrange_to_ocrmask(voltage_ranges[j],
112                                                voltage_ranges[j + 1]);
113                 if (!mask) {
114                         ret = -EINVAL;
115                         dev_err(dev, "OF: voltage-range #%d is invalid\n", i);
116                         goto err_ocr;
117                 }
118                 oms->pdata.ocr_mask |= mask;
119         }
120
121         for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
122                 enum of_gpio_flags gpio_flags;
123
124                 oms->gpios[i] = of_get_gpio_flags(np, i, &gpio_flags);
125                 if (!gpio_is_valid(oms->gpios[i]))
126                         continue;
127
128                 ret = gpio_request(oms->gpios[i], dev_name(dev));
129                 if (ret < 0) {
130                         oms->gpios[i] = -EINVAL;
131                         continue;
132                 }
133
134                 if (gpio_flags & OF_GPIO_ACTIVE_LOW)
135                         oms->alow_gpios[i] = true;
136         }
137
138         if (gpio_is_valid(oms->gpios[CD_GPIO]))
139                 oms->pdata.get_cd = of_mmc_spi_get_cd;
140         if (gpio_is_valid(oms->gpios[WP_GPIO]))
141                 oms->pdata.get_ro = of_mmc_spi_get_ro;
142
143         oms->detect_irq = irq_of_parse_and_map(np, 0);
144         if (oms->detect_irq != NO_IRQ) {
145                 oms->pdata.init = of_mmc_spi_init;
146                 oms->pdata.exit = of_mmc_spi_exit;
147         } else {
148                 oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
149         }
150
151         dev->platform_data = &oms->pdata;
152         return dev->platform_data;
153 err_ocr:
154         kfree(oms);
155         return NULL;
156 }
157 EXPORT_SYMBOL(mmc_spi_get_pdata);
158
159 void mmc_spi_put_pdata(struct spi_device *spi)
160 {
161         struct device *dev = &spi->dev;
162         struct device_node *np = dev->of_node;
163         struct of_mmc_spi *oms = to_of_mmc_spi(dev);
164         int i;
165
166         if (!dev->platform_data || !np)
167                 return;
168
169         for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
170                 if (gpio_is_valid(oms->gpios[i]))
171                         gpio_free(oms->gpios[i]);
172         }
173         kfree(oms);
174         dev->platform_data = NULL;
175 }
176 EXPORT_SYMBOL(mmc_spi_put_pdata);