0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/device.h>
0013 #include <linux/slab.h>
0014 #include <linux/irq.h>
0015 #include <linux/of.h>
0016 #include <linux/of_irq.h>
0017 #include <linux/spi/spi.h>
0018 #include <linux/spi/mmc_spi.h>
0019 #include <linux/mmc/core.h>
0020 #include <linux/mmc/host.h>
0021
0022 MODULE_LICENSE("GPL");
0023
0024 struct of_mmc_spi {
0025 struct mmc_spi_platform_data pdata;
0026 int detect_irq;
0027 };
0028
0029 static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
0030 {
0031 return container_of(dev->platform_data, struct of_mmc_spi, pdata);
0032 }
0033
0034 static int of_mmc_spi_init(struct device *dev,
0035 irqreturn_t (*irqhandler)(int, void *), void *mmc)
0036 {
0037 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
0038
0039 return request_threaded_irq(oms->detect_irq, NULL, irqhandler,
0040 IRQF_ONESHOT, dev_name(dev), mmc);
0041 }
0042
0043 static void of_mmc_spi_exit(struct device *dev, void *mmc)
0044 {
0045 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
0046
0047 free_irq(oms->detect_irq, mmc);
0048 }
0049
0050 struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
0051 {
0052 struct mmc_host *mmc = dev_get_drvdata(&spi->dev);
0053 struct device *dev = &spi->dev;
0054 struct of_mmc_spi *oms;
0055
0056 if (dev->platform_data || !dev_fwnode(dev))
0057 return dev->platform_data;
0058
0059 oms = kzalloc(sizeof(*oms), GFP_KERNEL);
0060 if (!oms)
0061 return NULL;
0062
0063 if (mmc_of_parse_voltage(mmc, &oms->pdata.ocr_mask) < 0)
0064 goto err_ocr;
0065
0066 oms->detect_irq = spi->irq;
0067 if (oms->detect_irq > 0) {
0068 oms->pdata.init = of_mmc_spi_init;
0069 oms->pdata.exit = of_mmc_spi_exit;
0070 } else {
0071 oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
0072 }
0073 if (device_property_read_bool(dev, "cap-sd-highspeed"))
0074 oms->pdata.caps |= MMC_CAP_SD_HIGHSPEED;
0075 if (device_property_read_bool(dev, "cap-mmc-highspeed"))
0076 oms->pdata.caps |= MMC_CAP_MMC_HIGHSPEED;
0077
0078 dev->platform_data = &oms->pdata;
0079 return dev->platform_data;
0080 err_ocr:
0081 kfree(oms);
0082 return NULL;
0083 }
0084 EXPORT_SYMBOL(mmc_spi_get_pdata);
0085
0086 void mmc_spi_put_pdata(struct spi_device *spi)
0087 {
0088 struct device *dev = &spi->dev;
0089 struct of_mmc_spi *oms = to_of_mmc_spi(dev);
0090
0091 if (!dev->platform_data || !dev_fwnode(dev))
0092 return;
0093
0094 kfree(oms);
0095 dev->platform_data = NULL;
0096 }
0097 EXPORT_SYMBOL(mmc_spi_put_pdata);