xref: /openbmc/linux/drivers/mmc/host/of_mmc_spi.c (revision d003d772)
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/of.h>
20 #include <linux/of_irq.h>
21 #include <linux/spi/spi.h>
22 #include <linux/spi/mmc_spi.h>
23 #include <linux/mmc/core.h>
24 #include <linux/mmc/host.h>
25 
26 /* For archs that don't support NO_IRQ (such as mips), provide a dummy value */
27 #ifndef NO_IRQ
28 #define NO_IRQ 0
29 #endif
30 
31 MODULE_LICENSE("GPL");
32 
33 struct of_mmc_spi {
34 	int detect_irq;
35 	struct mmc_spi_platform_data pdata;
36 };
37 
38 static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
39 {
40 	return container_of(dev->platform_data, struct of_mmc_spi, pdata);
41 }
42 
43 static int of_mmc_spi_init(struct device *dev,
44 			   irqreturn_t (*irqhandler)(int, void *), void *mmc)
45 {
46 	struct of_mmc_spi *oms = to_of_mmc_spi(dev);
47 
48 	return request_threaded_irq(oms->detect_irq, NULL, irqhandler,
49 					IRQF_ONESHOT, dev_name(dev), mmc);
50 }
51 
52 static void of_mmc_spi_exit(struct device *dev, void *mmc)
53 {
54 	struct of_mmc_spi *oms = to_of_mmc_spi(dev);
55 
56 	free_irq(oms->detect_irq, mmc);
57 }
58 
59 struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
60 {
61 	struct device *dev = &spi->dev;
62 	struct device_node *np = dev->of_node;
63 	struct of_mmc_spi *oms;
64 
65 	if (dev->platform_data || !np)
66 		return dev->platform_data;
67 
68 	oms = kzalloc(sizeof(*oms), GFP_KERNEL);
69 	if (!oms)
70 		return NULL;
71 
72 	if (mmc_of_parse_voltage(np, &oms->pdata.ocr_mask) <= 0)
73 		goto err_ocr;
74 
75 	oms->detect_irq = irq_of_parse_and_map(np, 0);
76 	if (oms->detect_irq != 0) {
77 		oms->pdata.init = of_mmc_spi_init;
78 		oms->pdata.exit = of_mmc_spi_exit;
79 	} else {
80 		oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
81 	}
82 
83 	dev->platform_data = &oms->pdata;
84 	return dev->platform_data;
85 err_ocr:
86 	kfree(oms);
87 	return NULL;
88 }
89 EXPORT_SYMBOL(mmc_spi_get_pdata);
90 
91 void mmc_spi_put_pdata(struct spi_device *spi)
92 {
93 	struct device *dev = &spi->dev;
94 	struct device_node *np = dev->of_node;
95 	struct of_mmc_spi *oms = to_of_mmc_spi(dev);
96 
97 	if (!dev->platform_data || !np)
98 		return;
99 
100 	kfree(oms);
101 	dev->platform_data = NULL;
102 }
103 EXPORT_SYMBOL(mmc_spi_put_pdata);
104