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