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/gpio.h> 18 #include <linux/of.h> 19 #include <linux/of_gpio.h> 20 #include <linux/spi/spi.h> 21 #include <linux/spi/mmc_spi.h> 22 #include <linux/mmc/core.h> 23 #include <linux/mmc/host.h> 24 25 MODULE_LICENSE("GPL"); 26 27 enum { 28 CD_GPIO = 0, 29 WP_GPIO, 30 NUM_GPIOS, 31 }; 32 33 struct of_mmc_spi { 34 int gpios[NUM_GPIOS]; 35 bool alow_gpios[NUM_GPIOS]; 36 struct mmc_spi_platform_data pdata; 37 }; 38 39 static struct of_mmc_spi *to_of_mmc_spi(struct device *dev) 40 { 41 return container_of(dev->platform_data, struct of_mmc_spi, pdata); 42 } 43 44 static int of_mmc_spi_read_gpio(struct device *dev, int gpio_num) 45 { 46 struct of_mmc_spi *oms = to_of_mmc_spi(dev); 47 bool active_low = oms->alow_gpios[gpio_num]; 48 bool value = gpio_get_value(oms->gpios[gpio_num]); 49 50 return active_low ^ value; 51 } 52 53 static int of_mmc_spi_get_cd(struct device *dev) 54 { 55 return of_mmc_spi_read_gpio(dev, CD_GPIO); 56 } 57 58 static int of_mmc_spi_get_ro(struct device *dev) 59 { 60 return of_mmc_spi_read_gpio(dev, WP_GPIO); 61 } 62 63 struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi) 64 { 65 struct device *dev = &spi->dev; 66 struct device_node *np = dev_archdata_get_node(&dev->archdata); 67 struct of_mmc_spi *oms; 68 const u32 *voltage_ranges; 69 int num_ranges; 70 int i; 71 int ret = -EINVAL; 72 73 if (dev->platform_data || !np) 74 return dev->platform_data; 75 76 oms = kzalloc(sizeof(*oms), GFP_KERNEL); 77 if (!oms) 78 return NULL; 79 80 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges); 81 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2; 82 if (!voltage_ranges || !num_ranges) { 83 dev_err(dev, "OF: voltage-ranges unspecified\n"); 84 goto err_ocr; 85 } 86 87 for (i = 0; i < num_ranges; i++) { 88 const int j = i * 2; 89 u32 mask; 90 91 mask = mmc_vddrange_to_ocrmask(voltage_ranges[j], 92 voltage_ranges[j + 1]); 93 if (!mask) { 94 ret = -EINVAL; 95 dev_err(dev, "OF: voltage-range #%d is invalid\n", i); 96 goto err_ocr; 97 } 98 oms->pdata.ocr_mask |= mask; 99 } 100 101 for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) { 102 enum of_gpio_flags gpio_flags; 103 104 oms->gpios[i] = of_get_gpio_flags(np, i, &gpio_flags); 105 if (!gpio_is_valid(oms->gpios[i])) 106 continue; 107 108 ret = gpio_request(oms->gpios[i], dev_name(dev)); 109 if (ret < 0) { 110 oms->gpios[i] = -EINVAL; 111 continue; 112 } 113 114 if (gpio_flags & OF_GPIO_ACTIVE_LOW) 115 oms->alow_gpios[i] = true; 116 } 117 118 if (gpio_is_valid(oms->gpios[CD_GPIO])) 119 oms->pdata.get_cd = of_mmc_spi_get_cd; 120 if (gpio_is_valid(oms->gpios[WP_GPIO])) 121 oms->pdata.get_ro = of_mmc_spi_get_ro; 122 123 /* We don't support interrupts yet, let's poll. */ 124 oms->pdata.caps |= MMC_CAP_NEEDS_POLL; 125 126 dev->platform_data = &oms->pdata; 127 return dev->platform_data; 128 err_ocr: 129 kfree(oms); 130 return NULL; 131 } 132 EXPORT_SYMBOL(mmc_spi_get_pdata); 133 134 void mmc_spi_put_pdata(struct spi_device *spi) 135 { 136 struct device *dev = &spi->dev; 137 struct device_node *np = dev_archdata_get_node(&dev->archdata); 138 struct of_mmc_spi *oms = to_of_mmc_spi(dev); 139 int i; 140 141 if (!dev->platform_data || !np) 142 return; 143 144 for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) { 145 if (gpio_is_valid(oms->gpios[i])) 146 gpio_free(oms->gpios[i]); 147 } 148 kfree(oms); 149 dev->platform_data = NULL; 150 } 151 EXPORT_SYMBOL(mmc_spi_put_pdata); 152