blob: 85bbebfde02e4896b6dda821ac86694237c3fa80 [file] [log] [blame]
Anton Vorontsov9c43df52008-12-30 18:15:28 +03001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Millerb2fce6a2011-03-18 15:00:23 -070018#include <linux/irq.h>
Anton Vorontsov9c43df52008-12-30 18:15:28 +030019#include <linux/gpio.h>
20#include <linux/of.h>
21#include <linux/of_gpio.h>
David Millerb2fce6a2011-03-18 15:00:23 -070022#include <linux/of_irq.h>
Anton Vorontsov9c43df52008-12-30 18:15:28 +030023#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 Gaob9c350a2011-05-24 21:03:22 +080028/* 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 Likely02d9e582009-11-04 22:43:35 -070033MODULE_LICENSE("GPL");
34
Anton Vorontsov9c43df52008-12-30 18:15:28 +030035enum {
36 CD_GPIO = 0,
37 WP_GPIO,
38 NUM_GPIOS,
39};
40
41struct of_mmc_spi {
42 int gpios[NUM_GPIOS];
43 bool alow_gpios[NUM_GPIOS];
Esben Haabendal290293e2011-03-07 20:45:28 -070044 int detect_irq;
Anton Vorontsov9c43df52008-12-30 18:15:28 +030045 struct mmc_spi_platform_data pdata;
46};
47
48static 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
Esben Haabendal290293e2011-03-07 20:45:28 -070053static int of_mmc_spi_init(struct device *dev,
54 irqreturn_t (*irqhandler)(int, void *), void *mmc)
55{
56 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
57
saurabh60b71f62015-11-25 23:56:24 +053058 return request_threaded_irq(oms->detect_irq, NULL, irqhandler,
59 IRQF_ONESHOT, dev_name(dev), mmc);
Esben Haabendal290293e2011-03-07 20:45:28 -070060}
61
62static void of_mmc_spi_exit(struct device *dev, void *mmc)
63{
64 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
65
66 free_irq(oms->detect_irq, mmc);
67}
68
Anton Vorontsov9c43df52008-12-30 18:15:28 +030069struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
70{
71 struct device *dev = &spi->dev;
Grant Likely61c7a082010-04-13 16:12:29 -070072 struct device_node *np = dev->of_node;
Anton Vorontsov9c43df52008-12-30 18:15:28 +030073 struct of_mmc_spi *oms;
74 const u32 *voltage_ranges;
75 int num_ranges;
76 int i;
Anton Vorontsov9c43df52008-12-30 18:15:28 +030077
78 if (dev->platform_data || !np)
79 return dev->platform_data;
80
81 oms = kzalloc(sizeof(*oms), GFP_KERNEL);
82 if (!oms)
83 return NULL;
84
85 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
86 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
87 if (!voltage_ranges || !num_ranges) {
88 dev_err(dev, "OF: voltage-ranges unspecified\n");
89 goto err_ocr;
90 }
91
92 for (i = 0; i < num_ranges; i++) {
93 const int j = i * 2;
94 u32 mask;
95
Jean-Christophe PLAGNIOL-VILLARDb6bf30d2012-01-30 05:15:29 +010096 mask = mmc_vddrange_to_ocrmask(be32_to_cpu(voltage_ranges[j]),
97 be32_to_cpu(voltage_ranges[j + 1]));
Anton Vorontsov9c43df52008-12-30 18:15:28 +030098 if (!mask) {
Anton Vorontsov9c43df52008-12-30 18:15:28 +030099 dev_err(dev, "OF: voltage-range #%d is invalid\n", i);
100 goto err_ocr;
101 }
102 oms->pdata.ocr_mask |= mask;
103 }
104
105 for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
106 enum of_gpio_flags gpio_flags;
107
108 oms->gpios[i] = of_get_gpio_flags(np, i, &gpio_flags);
109 if (!gpio_is_valid(oms->gpios[i]))
110 continue;
111
Anton Vorontsov9c43df52008-12-30 18:15:28 +0300112 if (gpio_flags & OF_GPIO_ACTIVE_LOW)
113 oms->alow_gpios[i] = true;
114 }
115
Laurent Pinchartbf287a92013-08-08 12:38:32 +0200116 if (gpio_is_valid(oms->gpios[CD_GPIO])) {
117 oms->pdata.cd_gpio = oms->gpios[CD_GPIO];
118 oms->pdata.flags |= MMC_SPI_USE_CD_GPIO;
119 if (!oms->alow_gpios[CD_GPIO])
120 oms->pdata.caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
121 }
122 if (gpio_is_valid(oms->gpios[WP_GPIO])) {
123 oms->pdata.ro_gpio = oms->gpios[WP_GPIO];
124 oms->pdata.flags |= MMC_SPI_USE_RO_GPIO;
125 if (!oms->alow_gpios[WP_GPIO])
126 oms->pdata.caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
127 }
Anton Vorontsov9c43df52008-12-30 18:15:28 +0300128
Esben Haabendal290293e2011-03-07 20:45:28 -0700129 oms->detect_irq = irq_of_parse_and_map(np, 0);
Roland Stigge073350f2013-01-30 18:05:08 +0100130 if (oms->detect_irq != 0) {
Esben Haabendal290293e2011-03-07 20:45:28 -0700131 oms->pdata.init = of_mmc_spi_init;
132 oms->pdata.exit = of_mmc_spi_exit;
133 } else {
134 oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
135 }
Anton Vorontsov9c43df52008-12-30 18:15:28 +0300136
137 dev->platform_data = &oms->pdata;
138 return dev->platform_data;
139err_ocr:
140 kfree(oms);
141 return NULL;
142}
143EXPORT_SYMBOL(mmc_spi_get_pdata);
144
145void mmc_spi_put_pdata(struct spi_device *spi)
146{
147 struct device *dev = &spi->dev;
Grant Likely61c7a082010-04-13 16:12:29 -0700148 struct device_node *np = dev->of_node;
Anton Vorontsov9c43df52008-12-30 18:15:28 +0300149 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
Anton Vorontsov9c43df52008-12-30 18:15:28 +0300150
151 if (!dev->platform_data || !np)
152 return;
153
Anton Vorontsov9c43df52008-12-30 18:15:28 +0300154 kfree(oms);
155 dev->platform_data = NULL;
156}
157EXPORT_SYMBOL(mmc_spi_put_pdata);