blob: 3c4d950a4755353236a4a78b3f906e73d796e0ac [file] [log] [blame]
Andy Shevchenkoaad5f192019-03-27 20:01:10 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Anton Vorontsov9c43df52008-12-30 18:15:28 +03002/*
3 * OpenFirmware bindings for the MMC-over-SPI driver
4 *
5 * Copyright (c) MontaVista Software, Inc. 2008.
6 *
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
Anton Vorontsov9c43df52008-12-30 18:15:28 +03008 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Millerb2fce6a2011-03-18 15:00:23 -070014#include <linux/irq.h>
Anton Vorontsov9c43df52008-12-30 18:15:28 +030015#include <linux/of.h>
David Millerb2fce6a2011-03-18 15:00:23 -070016#include <linux/of_irq.h>
Anton Vorontsov9c43df52008-12-30 18:15:28 +030017#include <linux/spi/spi.h>
18#include <linux/spi/mmc_spi.h>
19#include <linux/mmc/core.h>
20#include <linux/mmc/host.h>
21
Wanlong Gaob9c350a2011-05-24 21:03:22 +080022/* For archs that don't support NO_IRQ (such as mips), provide a dummy value */
23#ifndef NO_IRQ
24#define NO_IRQ 0
25#endif
26
Grant Likely02d9e582009-11-04 22:43:35 -070027MODULE_LICENSE("GPL");
28
Anton Vorontsov9c43df52008-12-30 18:15:28 +030029struct of_mmc_spi {
Esben Haabendal290293e2011-03-07 20:45:28 -070030 int detect_irq;
Anton Vorontsov9c43df52008-12-30 18:15:28 +030031 struct mmc_spi_platform_data pdata;
32};
33
34static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
35{
36 return container_of(dev->platform_data, struct of_mmc_spi, pdata);
37}
38
Esben Haabendal290293e2011-03-07 20:45:28 -070039static int of_mmc_spi_init(struct device *dev,
40 irqreturn_t (*irqhandler)(int, void *), void *mmc)
41{
42 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
43
saurabh60b71f62015-11-25 23:56:24 +053044 return request_threaded_irq(oms->detect_irq, NULL, irqhandler,
45 IRQF_ONESHOT, dev_name(dev), mmc);
Esben Haabendal290293e2011-03-07 20:45:28 -070046}
47
48static void of_mmc_spi_exit(struct device *dev, void *mmc)
49{
50 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
51
52 free_irq(oms->detect_irq, mmc);
53}
54
Anton Vorontsov9c43df52008-12-30 18:15:28 +030055struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
56{
57 struct device *dev = &spi->dev;
Grant Likely61c7a082010-04-13 16:12:29 -070058 struct device_node *np = dev->of_node;
Anton Vorontsov9c43df52008-12-30 18:15:28 +030059 struct of_mmc_spi *oms;
Anton Vorontsov9c43df52008-12-30 18:15:28 +030060
61 if (dev->platform_data || !np)
62 return dev->platform_data;
63
64 oms = kzalloc(sizeof(*oms), GFP_KERNEL);
65 if (!oms)
66 return NULL;
67
Ulf Hansson64310862019-02-13 16:57:21 +010068 if (mmc_of_parse_voltage(np, &oms->pdata.ocr_mask) <= 0)
Anton Vorontsov9c43df52008-12-30 18:15:28 +030069 goto err_ocr;
Anton Vorontsov9c43df52008-12-30 18:15:28 +030070
Esben Haabendal290293e2011-03-07 20:45:28 -070071 oms->detect_irq = irq_of_parse_and_map(np, 0);
Roland Stigge073350f2013-01-30 18:05:08 +010072 if (oms->detect_irq != 0) {
Esben Haabendal290293e2011-03-07 20:45:28 -070073 oms->pdata.init = of_mmc_spi_init;
74 oms->pdata.exit = of_mmc_spi_exit;
75 } else {
76 oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
77 }
Anton Vorontsov9c43df52008-12-30 18:15:28 +030078
79 dev->platform_data = &oms->pdata;
80 return dev->platform_data;
81err_ocr:
82 kfree(oms);
83 return NULL;
84}
85EXPORT_SYMBOL(mmc_spi_get_pdata);
86
87void mmc_spi_put_pdata(struct spi_device *spi)
88{
89 struct device *dev = &spi->dev;
Grant Likely61c7a082010-04-13 16:12:29 -070090 struct device_node *np = dev->of_node;
Anton Vorontsov9c43df52008-12-30 18:15:28 +030091 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
Anton Vorontsov9c43df52008-12-30 18:15:28 +030092
93 if (!dev->platform_data || !np)
94 return;
95
Anton Vorontsov9c43df52008-12-30 18:15:28 +030096 kfree(oms);
97 dev->platform_data = NULL;
98}
99EXPORT_SYMBOL(mmc_spi_put_pdata);