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 const __be32 *voltage_ranges; 65 int num_ranges; 66 int i; 67 68 if (dev->platform_data || !np) 69 return dev->platform_data; 70 71 oms = kzalloc(sizeof(*oms), GFP_KERNEL); 72 if (!oms) 73 return NULL; 74 75 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges); 76 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2; 77 if (!voltage_ranges || !num_ranges) { 78 dev_err(dev, "OF: voltage-ranges unspecified\n"); 79 goto err_ocr; 80 } 81 82 for (i = 0; i < num_ranges; i++) { 83 const int j = i * 2; 84 u32 mask; 85 86 mask = mmc_vddrange_to_ocrmask(be32_to_cpu(voltage_ranges[j]), 87 be32_to_cpu(voltage_ranges[j + 1])); 88 if (!mask) { 89 dev_err(dev, "OF: voltage-range #%d is invalid\n", i); 90 goto err_ocr; 91 } 92 oms->pdata.ocr_mask |= mask; 93 } 94 95 oms->detect_irq = irq_of_parse_and_map(np, 0); 96 if (oms->detect_irq != 0) { 97 oms->pdata.init = of_mmc_spi_init; 98 oms->pdata.exit = of_mmc_spi_exit; 99 } else { 100 oms->pdata.caps |= MMC_CAP_NEEDS_POLL; 101 } 102 103 dev->platform_data = &oms->pdata; 104 return dev->platform_data; 105 err_ocr: 106 kfree(oms); 107 return NULL; 108 } 109 EXPORT_SYMBOL(mmc_spi_get_pdata); 110 111 void mmc_spi_put_pdata(struct spi_device *spi) 112 { 113 struct device *dev = &spi->dev; 114 struct device_node *np = dev->of_node; 115 struct of_mmc_spi *oms = to_of_mmc_spi(dev); 116 117 if (!dev->platform_data || !np) 118 return; 119 120 kfree(oms); 121 dev->platform_data = NULL; 122 } 123 EXPORT_SYMBOL(mmc_spi_put_pdata); 124