David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1 | /* |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 2 | * SPI init/core code |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2005 David Brownell |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 5 | * Copyright (C) 2008 Secret Lab Technologies Ltd. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 22 | #include <linux/kernel.h> |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 23 | #include <linux/kmod.h> |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 24 | #include <linux/device.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/cache.h> |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 27 | #include <linux/dma-mapping.h> |
| 28 | #include <linux/dmaengine.h> |
Matthias Kaehlcke | 9404082 | 2007-07-17 04:04:16 -0700 | [diff] [blame] | 29 | #include <linux/mutex.h> |
Sinan Akman | 2b7a32f | 2010-10-02 21:28:29 -0600 | [diff] [blame] | 30 | #include <linux/of_device.h> |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 31 | #include <linux/of_irq.h> |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 32 | #include <linux/clk/clk-conf.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 34 | #include <linux/mod_devicetable.h> |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 35 | #include <linux/spi/spi.h> |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 36 | #include <linux/of_gpio.h> |
Mark Brown | 3ae22e8 | 2010-12-25 15:32:27 +0100 | [diff] [blame] | 37 | #include <linux/pm_runtime.h> |
Paul Gortmaker | 025ed13 | 2011-07-10 12:57:55 -0400 | [diff] [blame] | 38 | #include <linux/export.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 39 | #include <linux/sched/rt.h> |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 40 | #include <linux/delay.h> |
| 41 | #include <linux/kthread.h> |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 42 | #include <linux/ioport.h> |
| 43 | #include <linux/acpi.h> |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 44 | |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 45 | #define CREATE_TRACE_POINTS |
| 46 | #include <trace/events/spi.h> |
| 47 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 48 | static void spidev_release(struct device *dev) |
| 49 | { |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 50 | struct spi_device *spi = to_spi_device(dev); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 51 | |
| 52 | /* spi masters may cleanup for released devices */ |
| 53 | if (spi->master->cleanup) |
| 54 | spi->master->cleanup(spi); |
| 55 | |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 56 | spi_master_put(spi->master); |
Roman Tereshonkov | 07a389f | 2010-04-12 09:56:35 +0000 | [diff] [blame] | 57 | kfree(spi); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static ssize_t |
| 61 | modalias_show(struct device *dev, struct device_attribute *a, char *buf) |
| 62 | { |
| 63 | const struct spi_device *spi = to_spi_device(dev); |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 64 | int len; |
| 65 | |
| 66 | len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); |
| 67 | if (len != -ENODEV) |
| 68 | return len; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 69 | |
Grant Likely | d8e328b | 2012-05-20 00:08:13 -0600 | [diff] [blame] | 70 | return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 71 | } |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 72 | static DEVICE_ATTR_RO(modalias); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 73 | |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 74 | static struct attribute *spi_dev_attrs[] = { |
| 75 | &dev_attr_modalias.attr, |
| 76 | NULL, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 77 | }; |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 78 | ATTRIBUTE_GROUPS(spi_dev); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 79 | |
| 80 | /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, |
| 81 | * and the sysfs version makes coldplug work too. |
| 82 | */ |
| 83 | |
Anton Vorontsov | 75368bf | 2009-09-22 16:46:04 -0700 | [diff] [blame] | 84 | static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, |
| 85 | const struct spi_device *sdev) |
| 86 | { |
| 87 | while (id->name[0]) { |
| 88 | if (!strcmp(sdev->modalias, id->name)) |
| 89 | return id; |
| 90 | id++; |
| 91 | } |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) |
| 96 | { |
| 97 | const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); |
| 98 | |
| 99 | return spi_match_id(sdrv->id_table, sdev); |
| 100 | } |
| 101 | EXPORT_SYMBOL_GPL(spi_get_device_id); |
| 102 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 103 | static int spi_match_device(struct device *dev, struct device_driver *drv) |
| 104 | { |
| 105 | const struct spi_device *spi = to_spi_device(dev); |
Anton Vorontsov | 75368bf | 2009-09-22 16:46:04 -0700 | [diff] [blame] | 106 | const struct spi_driver *sdrv = to_spi_driver(drv); |
| 107 | |
Sinan Akman | 2b7a32f | 2010-10-02 21:28:29 -0600 | [diff] [blame] | 108 | /* Attempt an OF style match */ |
| 109 | if (of_driver_match_device(dev, drv)) |
| 110 | return 1; |
| 111 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 112 | /* Then try ACPI */ |
| 113 | if (acpi_driver_match_device(dev, drv)) |
| 114 | return 1; |
| 115 | |
Anton Vorontsov | 75368bf | 2009-09-22 16:46:04 -0700 | [diff] [blame] | 116 | if (sdrv->id_table) |
| 117 | return !!spi_match_id(sdrv->id_table, spi); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 118 | |
Kay Sievers | 35f74fc | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 119 | return strcmp(spi->modalias, drv->name) == 0; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 122 | static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 123 | { |
| 124 | const struct spi_device *spi = to_spi_device(dev); |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 125 | int rc; |
| 126 | |
| 127 | rc = acpi_device_uevent_modalias(dev, env); |
| 128 | if (rc != -ENODEV) |
| 129 | return rc; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 130 | |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 131 | add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
Mark Brown | 3ae22e8 | 2010-12-25 15:32:27 +0100 | [diff] [blame] | 135 | #ifdef CONFIG_PM_SLEEP |
| 136 | static int spi_legacy_suspend(struct device *dev, pm_message_t message) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 137 | { |
David Brownell | 3c72426 | 2008-02-06 01:38:10 -0800 | [diff] [blame] | 138 | int value = 0; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 139 | struct spi_driver *drv = to_spi_driver(dev->driver); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 140 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 141 | /* suspend will stop irqs and dma; no more i/o */ |
David Brownell | 3c72426 | 2008-02-06 01:38:10 -0800 | [diff] [blame] | 142 | if (drv) { |
| 143 | if (drv->suspend) |
| 144 | value = drv->suspend(to_spi_device(dev), message); |
| 145 | else |
| 146 | dev_dbg(dev, "... can't suspend\n"); |
| 147 | } |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 148 | return value; |
| 149 | } |
| 150 | |
Mark Brown | 3ae22e8 | 2010-12-25 15:32:27 +0100 | [diff] [blame] | 151 | static int spi_legacy_resume(struct device *dev) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 152 | { |
David Brownell | 3c72426 | 2008-02-06 01:38:10 -0800 | [diff] [blame] | 153 | int value = 0; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 154 | struct spi_driver *drv = to_spi_driver(dev->driver); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 155 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 156 | /* resume may restart the i/o queue */ |
David Brownell | 3c72426 | 2008-02-06 01:38:10 -0800 | [diff] [blame] | 157 | if (drv) { |
| 158 | if (drv->resume) |
| 159 | value = drv->resume(to_spi_device(dev)); |
| 160 | else |
| 161 | dev_dbg(dev, "... can't resume\n"); |
| 162 | } |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 163 | return value; |
| 164 | } |
| 165 | |
Mark Brown | 3ae22e8 | 2010-12-25 15:32:27 +0100 | [diff] [blame] | 166 | static int spi_pm_suspend(struct device *dev) |
| 167 | { |
| 168 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 169 | |
| 170 | if (pm) |
| 171 | return pm_generic_suspend(dev); |
| 172 | else |
| 173 | return spi_legacy_suspend(dev, PMSG_SUSPEND); |
| 174 | } |
| 175 | |
| 176 | static int spi_pm_resume(struct device *dev) |
| 177 | { |
| 178 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 179 | |
| 180 | if (pm) |
| 181 | return pm_generic_resume(dev); |
| 182 | else |
| 183 | return spi_legacy_resume(dev); |
| 184 | } |
| 185 | |
| 186 | static int spi_pm_freeze(struct device *dev) |
| 187 | { |
| 188 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 189 | |
| 190 | if (pm) |
| 191 | return pm_generic_freeze(dev); |
| 192 | else |
| 193 | return spi_legacy_suspend(dev, PMSG_FREEZE); |
| 194 | } |
| 195 | |
| 196 | static int spi_pm_thaw(struct device *dev) |
| 197 | { |
| 198 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 199 | |
| 200 | if (pm) |
| 201 | return pm_generic_thaw(dev); |
| 202 | else |
| 203 | return spi_legacy_resume(dev); |
| 204 | } |
| 205 | |
| 206 | static int spi_pm_poweroff(struct device *dev) |
| 207 | { |
| 208 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 209 | |
| 210 | if (pm) |
| 211 | return pm_generic_poweroff(dev); |
| 212 | else |
| 213 | return spi_legacy_suspend(dev, PMSG_HIBERNATE); |
| 214 | } |
| 215 | |
| 216 | static int spi_pm_restore(struct device *dev) |
| 217 | { |
| 218 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 219 | |
| 220 | if (pm) |
| 221 | return pm_generic_restore(dev); |
| 222 | else |
| 223 | return spi_legacy_resume(dev); |
| 224 | } |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 225 | #else |
Mark Brown | 3ae22e8 | 2010-12-25 15:32:27 +0100 | [diff] [blame] | 226 | #define spi_pm_suspend NULL |
| 227 | #define spi_pm_resume NULL |
| 228 | #define spi_pm_freeze NULL |
| 229 | #define spi_pm_thaw NULL |
| 230 | #define spi_pm_poweroff NULL |
| 231 | #define spi_pm_restore NULL |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 232 | #endif |
| 233 | |
Mark Brown | 3ae22e8 | 2010-12-25 15:32:27 +0100 | [diff] [blame] | 234 | static const struct dev_pm_ops spi_pm = { |
| 235 | .suspend = spi_pm_suspend, |
| 236 | .resume = spi_pm_resume, |
| 237 | .freeze = spi_pm_freeze, |
| 238 | .thaw = spi_pm_thaw, |
| 239 | .poweroff = spi_pm_poweroff, |
| 240 | .restore = spi_pm_restore, |
| 241 | SET_RUNTIME_PM_OPS( |
| 242 | pm_generic_runtime_suspend, |
| 243 | pm_generic_runtime_resume, |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 244 | NULL |
Mark Brown | 3ae22e8 | 2010-12-25 15:32:27 +0100 | [diff] [blame] | 245 | ) |
| 246 | }; |
| 247 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 248 | struct bus_type spi_bus_type = { |
| 249 | .name = "spi", |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 250 | .dev_groups = spi_dev_groups, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 251 | .match = spi_match_device, |
| 252 | .uevent = spi_uevent, |
Mark Brown | 3ae22e8 | 2010-12-25 15:32:27 +0100 | [diff] [blame] | 253 | .pm = &spi_pm, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 254 | }; |
| 255 | EXPORT_SYMBOL_GPL(spi_bus_type); |
| 256 | |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 257 | |
| 258 | static int spi_drv_probe(struct device *dev) |
| 259 | { |
| 260 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 261 | int ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 262 | |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 263 | ret = of_clk_set_defaults(dev->of_node, false); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | |
Jean Delvare | aec35f4 | 2014-02-13 15:28:41 +0100 | [diff] [blame] | 267 | acpi_dev_pm_attach(dev, true); |
| 268 | ret = sdrv->probe(to_spi_device(dev)); |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 269 | if (ret) |
Jean Delvare | aec35f4 | 2014-02-13 15:28:41 +0100 | [diff] [blame] | 270 | acpi_dev_pm_detach(dev, true); |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 271 | |
| 272 | return ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int spi_drv_remove(struct device *dev) |
| 276 | { |
| 277 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 278 | int ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 279 | |
Jean Delvare | aec35f4 | 2014-02-13 15:28:41 +0100 | [diff] [blame] | 280 | ret = sdrv->remove(to_spi_device(dev)); |
| 281 | acpi_dev_pm_detach(dev, true); |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 282 | |
| 283 | return ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static void spi_drv_shutdown(struct device *dev) |
| 287 | { |
| 288 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
| 289 | |
| 290 | sdrv->shutdown(to_spi_device(dev)); |
| 291 | } |
| 292 | |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 293 | /** |
| 294 | * spi_register_driver - register a SPI driver |
| 295 | * @sdrv: the driver to register |
| 296 | * Context: can sleep |
| 297 | */ |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 298 | int spi_register_driver(struct spi_driver *sdrv) |
| 299 | { |
| 300 | sdrv->driver.bus = &spi_bus_type; |
| 301 | if (sdrv->probe) |
| 302 | sdrv->driver.probe = spi_drv_probe; |
| 303 | if (sdrv->remove) |
| 304 | sdrv->driver.remove = spi_drv_remove; |
| 305 | if (sdrv->shutdown) |
| 306 | sdrv->driver.shutdown = spi_drv_shutdown; |
| 307 | return driver_register(&sdrv->driver); |
| 308 | } |
| 309 | EXPORT_SYMBOL_GPL(spi_register_driver); |
| 310 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 311 | /*-------------------------------------------------------------------------*/ |
| 312 | |
| 313 | /* SPI devices should normally not be created by SPI device drivers; that |
| 314 | * would make them board-specific. Similarly with SPI master drivers. |
| 315 | * Device registration normally goes into like arch/.../mach.../board-YYY.c |
| 316 | * with other readonly (flashable) information about mainboard devices. |
| 317 | */ |
| 318 | |
| 319 | struct boardinfo { |
| 320 | struct list_head list; |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 321 | struct spi_board_info board_info; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | static LIST_HEAD(board_list); |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 325 | static LIST_HEAD(spi_master_list); |
| 326 | |
| 327 | /* |
| 328 | * Used to protect add/del opertion for board_info list and |
| 329 | * spi_master list, and their matching process |
| 330 | */ |
Matthias Kaehlcke | 9404082 | 2007-07-17 04:04:16 -0700 | [diff] [blame] | 331 | static DEFINE_MUTEX(board_lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 332 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 333 | /** |
| 334 | * spi_alloc_device - Allocate a new SPI device |
| 335 | * @master: Controller to which device is connected |
| 336 | * Context: can sleep |
| 337 | * |
| 338 | * Allows a driver to allocate and initialize a spi_device without |
| 339 | * registering it immediately. This allows a driver to directly |
| 340 | * fill the spi_device with device parameters before calling |
| 341 | * spi_add_device() on it. |
| 342 | * |
| 343 | * Caller is responsible to call spi_add_device() on the returned |
| 344 | * spi_device structure to add it to the SPI master. If the caller |
| 345 | * needs to discard the spi_device without adding it, then it should |
| 346 | * call spi_dev_put() on it. |
| 347 | * |
| 348 | * Returns a pointer to the new device, or NULL. |
| 349 | */ |
| 350 | struct spi_device *spi_alloc_device(struct spi_master *master) |
| 351 | { |
| 352 | struct spi_device *spi; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 353 | |
| 354 | if (!spi_master_get(master)) |
| 355 | return NULL; |
| 356 | |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 357 | spi = kzalloc(sizeof(*spi), GFP_KERNEL); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 358 | if (!spi) { |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 359 | spi_master_put(master); |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | spi->master = master; |
Laurent Pinchart | 178db7d | 2011-12-12 01:15:06 +0100 | [diff] [blame] | 364 | spi->dev.parent = &master->dev; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 365 | spi->dev.bus = &spi_bus_type; |
| 366 | spi->dev.release = spidev_release; |
Andreas Larsson | 446411e | 2013-02-13 14:20:25 +0100 | [diff] [blame] | 367 | spi->cs_gpio = -ENOENT; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 368 | device_initialize(&spi->dev); |
| 369 | return spi; |
| 370 | } |
| 371 | EXPORT_SYMBOL_GPL(spi_alloc_device); |
| 372 | |
Jarkko Nikula | e13ac47 | 2013-11-14 14:03:53 +0200 | [diff] [blame] | 373 | static void spi_dev_set_name(struct spi_device *spi) |
| 374 | { |
| 375 | struct acpi_device *adev = ACPI_COMPANION(&spi->dev); |
| 376 | |
| 377 | if (adev) { |
| 378 | dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev), |
| 383 | spi->chip_select); |
| 384 | } |
| 385 | |
Mika Westerberg | b6fb8d3 | 2014-01-09 15:23:55 +0200 | [diff] [blame] | 386 | static int spi_dev_check(struct device *dev, void *data) |
| 387 | { |
| 388 | struct spi_device *spi = to_spi_device(dev); |
| 389 | struct spi_device *new_spi = data; |
| 390 | |
| 391 | if (spi->master == new_spi->master && |
| 392 | spi->chip_select == new_spi->chip_select) |
| 393 | return -EBUSY; |
| 394 | return 0; |
| 395 | } |
| 396 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 397 | /** |
| 398 | * spi_add_device - Add spi_device allocated with spi_alloc_device |
| 399 | * @spi: spi_device to register |
| 400 | * |
| 401 | * Companion function to spi_alloc_device. Devices allocated with |
| 402 | * spi_alloc_device can be added onto the spi bus with this function. |
| 403 | * |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 404 | * Returns 0 on success; negative errno on failure |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 405 | */ |
| 406 | int spi_add_device(struct spi_device *spi) |
| 407 | { |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 408 | static DEFINE_MUTEX(spi_add_lock); |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 409 | struct spi_master *master = spi->master; |
| 410 | struct device *dev = master->dev.parent; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 411 | int status; |
| 412 | |
| 413 | /* Chipselects are numbered 0..max; validate. */ |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 414 | if (spi->chip_select >= master->num_chipselect) { |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 415 | dev_err(dev, "cs%d >= max %d\n", |
| 416 | spi->chip_select, |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 417 | master->num_chipselect); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 418 | return -EINVAL; |
| 419 | } |
| 420 | |
| 421 | /* Set the bus ID string */ |
Jarkko Nikula | e13ac47 | 2013-11-14 14:03:53 +0200 | [diff] [blame] | 422 | spi_dev_set_name(spi); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 423 | |
| 424 | /* We need to make sure there's no other device with this |
| 425 | * chipselect **BEFORE** we call setup(), else we'll trash |
| 426 | * its configuration. Lock against concurrent add() calls. |
| 427 | */ |
| 428 | mutex_lock(&spi_add_lock); |
| 429 | |
Mika Westerberg | b6fb8d3 | 2014-01-09 15:23:55 +0200 | [diff] [blame] | 430 | status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); |
| 431 | if (status) { |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 432 | dev_err(dev, "chipselect %d already in use\n", |
| 433 | spi->chip_select); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 434 | goto done; |
| 435 | } |
| 436 | |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 437 | if (master->cs_gpios) |
| 438 | spi->cs_gpio = master->cs_gpios[spi->chip_select]; |
| 439 | |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 440 | /* Drivers may modify this initial i/o setup, but will |
| 441 | * normally rely on the device being setup. Devices |
| 442 | * using SPI_CS_HIGH can't coexist well otherwise... |
| 443 | */ |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 444 | status = spi_setup(spi); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 445 | if (status < 0) { |
Linus Walleij | eb288a1 | 2010-10-21 21:06:44 +0200 | [diff] [blame] | 446 | dev_err(dev, "can't setup %s, status %d\n", |
| 447 | dev_name(&spi->dev), status); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 448 | goto done; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 449 | } |
| 450 | |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 451 | /* Device may be bound to an active driver when this returns */ |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 452 | status = device_add(&spi->dev); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 453 | if (status < 0) |
Linus Walleij | eb288a1 | 2010-10-21 21:06:44 +0200 | [diff] [blame] | 454 | dev_err(dev, "can't add %s, status %d\n", |
| 455 | dev_name(&spi->dev), status); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 456 | else |
Kay Sievers | 35f74fc | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 457 | dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 458 | |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 459 | done: |
| 460 | mutex_unlock(&spi_add_lock); |
| 461 | return status; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 462 | } |
| 463 | EXPORT_SYMBOL_GPL(spi_add_device); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 464 | |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 465 | /** |
| 466 | * spi_new_device - instantiate one new SPI device |
| 467 | * @master: Controller to which device is connected |
| 468 | * @chip: Describes the SPI device |
| 469 | * Context: can sleep |
| 470 | * |
| 471 | * On typical mainboards, this is purely internal; and it's not needed |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 472 | * after board init creates the hard-wired devices. Some development |
| 473 | * platforms may not be able to use spi_register_board_info though, and |
| 474 | * this is exported so that for example a USB or parport based adapter |
| 475 | * driver could add devices (which it would learn about out-of-band). |
David Brownell | 082c8cb | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 476 | * |
| 477 | * Returns the new device, or NULL. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 478 | */ |
Adrian Bunk | e9d5a46 | 2007-03-26 21:32:23 -0800 | [diff] [blame] | 479 | struct spi_device *spi_new_device(struct spi_master *master, |
| 480 | struct spi_board_info *chip) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 481 | { |
| 482 | struct spi_device *proxy; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 483 | int status; |
| 484 | |
David Brownell | 082c8cb | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 485 | /* NOTE: caller did any chip->bus_num checks necessary. |
| 486 | * |
| 487 | * Also, unless we change the return value convention to use |
| 488 | * error-or-pointer (not NULL-or-pointer), troubleshootability |
| 489 | * suggests syslogged diagnostics are best here (ugh). |
| 490 | */ |
| 491 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 492 | proxy = spi_alloc_device(master); |
| 493 | if (!proxy) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 494 | return NULL; |
| 495 | |
Grant Likely | 102eb97 | 2008-07-23 21:29:55 -0700 | [diff] [blame] | 496 | WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); |
| 497 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 498 | proxy->chip_select = chip->chip_select; |
| 499 | proxy->max_speed_hz = chip->max_speed_hz; |
David Brownell | 980a01c | 2006-06-28 07:47:15 -0700 | [diff] [blame] | 500 | proxy->mode = chip->mode; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 501 | proxy->irq = chip->irq; |
Grant Likely | 102eb97 | 2008-07-23 21:29:55 -0700 | [diff] [blame] | 502 | strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 503 | proxy->dev.platform_data = (void *) chip->platform_data; |
| 504 | proxy->controller_data = chip->controller_data; |
| 505 | proxy->controller_state = NULL; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 506 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 507 | status = spi_add_device(proxy); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 508 | if (status < 0) { |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 509 | spi_dev_put(proxy); |
| 510 | return NULL; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 511 | } |
| 512 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 513 | return proxy; |
| 514 | } |
| 515 | EXPORT_SYMBOL_GPL(spi_new_device); |
| 516 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 517 | static void spi_match_master_to_boardinfo(struct spi_master *master, |
| 518 | struct spi_board_info *bi) |
| 519 | { |
| 520 | struct spi_device *dev; |
| 521 | |
| 522 | if (master->bus_num != bi->bus_num) |
| 523 | return; |
| 524 | |
| 525 | dev = spi_new_device(master, bi); |
| 526 | if (!dev) |
| 527 | dev_err(master->dev.parent, "can't create new device for %s\n", |
| 528 | bi->modalias); |
| 529 | } |
| 530 | |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 531 | /** |
| 532 | * spi_register_board_info - register SPI devices for a given board |
| 533 | * @info: array of chip descriptors |
| 534 | * @n: how many descriptors are provided |
| 535 | * Context: can sleep |
| 536 | * |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 537 | * Board-specific early init code calls this (probably during arch_initcall) |
| 538 | * with segments of the SPI device table. Any device nodes are created later, |
| 539 | * after the relevant parent SPI controller (bus_num) is defined. We keep |
| 540 | * this table of devices forever, so that reloading a controller driver will |
| 541 | * not make Linux forget about these hard-wired devices. |
| 542 | * |
| 543 | * Other code can also call this, e.g. a particular add-on board might provide |
| 544 | * SPI devices through its expansion connector, so code initializing that board |
| 545 | * would naturally declare its SPI devices. |
| 546 | * |
| 547 | * The board info passed can safely be __initdata ... but be careful of |
| 548 | * any embedded pointers (platform_data, etc), they're copied as-is. |
| 549 | */ |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 550 | int spi_register_board_info(struct spi_board_info const *info, unsigned n) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 551 | { |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 552 | struct boardinfo *bi; |
| 553 | int i; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 554 | |
Xiubo Li | c7908a3 | 2014-09-24 14:30:29 +0800 | [diff] [blame^] | 555 | if (!n) |
| 556 | return -EINVAL; |
| 557 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 558 | bi = kzalloc(n * sizeof(*bi), GFP_KERNEL); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 559 | if (!bi) |
| 560 | return -ENOMEM; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 561 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 562 | for (i = 0; i < n; i++, bi++, info++) { |
| 563 | struct spi_master *master; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 564 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 565 | memcpy(&bi->board_info, info, sizeof(*info)); |
| 566 | mutex_lock(&board_lock); |
| 567 | list_add_tail(&bi->list, &board_list); |
| 568 | list_for_each_entry(master, &spi_master_list, list) |
| 569 | spi_match_master_to_boardinfo(master, &bi->board_info); |
| 570 | mutex_unlock(&board_lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 571 | } |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 572 | |
| 573 | return 0; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /*-------------------------------------------------------------------------*/ |
| 577 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 578 | static void spi_set_cs(struct spi_device *spi, bool enable) |
| 579 | { |
| 580 | if (spi->mode & SPI_CS_HIGH) |
| 581 | enable = !enable; |
| 582 | |
| 583 | if (spi->cs_gpio >= 0) |
| 584 | gpio_set_value(spi->cs_gpio, !enable); |
| 585 | else if (spi->master->set_cs) |
| 586 | spi->master->set_cs(spi, !enable); |
| 587 | } |
| 588 | |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 589 | #ifdef CONFIG_HAS_DMA |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 590 | static int spi_map_buf(struct spi_master *master, struct device *dev, |
| 591 | struct sg_table *sgt, void *buf, size_t len, |
| 592 | enum dma_data_direction dir) |
| 593 | { |
| 594 | const bool vmalloced_buf = is_vmalloc_addr(buf); |
| 595 | const int desc_len = vmalloced_buf ? PAGE_SIZE : master->max_dma_len; |
| 596 | const int sgs = DIV_ROUND_UP(len, desc_len); |
| 597 | struct page *vm_page; |
| 598 | void *sg_buf; |
| 599 | size_t min; |
| 600 | int i, ret; |
| 601 | |
| 602 | ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); |
| 603 | if (ret != 0) |
| 604 | return ret; |
| 605 | |
| 606 | for (i = 0; i < sgs; i++) { |
| 607 | min = min_t(size_t, len, desc_len); |
| 608 | |
| 609 | if (vmalloced_buf) { |
| 610 | vm_page = vmalloc_to_page(buf); |
| 611 | if (!vm_page) { |
| 612 | sg_free_table(sgt); |
| 613 | return -ENOMEM; |
| 614 | } |
| 615 | sg_buf = page_address(vm_page) + |
| 616 | ((size_t)buf & ~PAGE_MASK); |
| 617 | } else { |
| 618 | sg_buf = buf; |
| 619 | } |
| 620 | |
| 621 | sg_set_buf(&sgt->sgl[i], sg_buf, min); |
| 622 | |
| 623 | buf += min; |
| 624 | len -= min; |
| 625 | } |
| 626 | |
| 627 | ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); |
Geert Uytterhoeven | 89e4b66 | 2014-07-10 15:29:32 +0200 | [diff] [blame] | 628 | if (!ret) |
| 629 | ret = -ENOMEM; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 630 | if (ret < 0) { |
| 631 | sg_free_table(sgt); |
| 632 | return ret; |
| 633 | } |
| 634 | |
| 635 | sgt->nents = ret; |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static void spi_unmap_buf(struct spi_master *master, struct device *dev, |
| 641 | struct sg_table *sgt, enum dma_data_direction dir) |
| 642 | { |
| 643 | if (sgt->orig_nents) { |
| 644 | dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); |
| 645 | sg_free_table(sgt); |
| 646 | } |
| 647 | } |
| 648 | |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 649 | static int __spi_map_msg(struct spi_master *master, struct spi_message *msg) |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 650 | { |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 651 | struct device *tx_dev, *rx_dev; |
| 652 | struct spi_transfer *xfer; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 653 | int ret; |
Mark Brown | 3a2eba9 | 2014-01-28 20:17:03 +0000 | [diff] [blame] | 654 | |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 655 | if (!master->can_dma) |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 656 | return 0; |
| 657 | |
Geert Uytterhoeven | 3fc2542 | 2014-07-10 15:29:33 +0200 | [diff] [blame] | 658 | tx_dev = master->dma_tx->device->dev; |
| 659 | rx_dev = master->dma_rx->device->dev; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 660 | |
| 661 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 662 | if (!master->can_dma(master, msg->spi, xfer)) |
| 663 | continue; |
| 664 | |
| 665 | if (xfer->tx_buf != NULL) { |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 666 | ret = spi_map_buf(master, tx_dev, &xfer->tx_sg, |
| 667 | (void *)xfer->tx_buf, xfer->len, |
| 668 | DMA_TO_DEVICE); |
| 669 | if (ret != 0) |
| 670 | return ret; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | if (xfer->rx_buf != NULL) { |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 674 | ret = spi_map_buf(master, rx_dev, &xfer->rx_sg, |
| 675 | xfer->rx_buf, xfer->len, |
| 676 | DMA_FROM_DEVICE); |
| 677 | if (ret != 0) { |
| 678 | spi_unmap_buf(master, tx_dev, &xfer->tx_sg, |
| 679 | DMA_TO_DEVICE); |
| 680 | return ret; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | master->cur_msg_mapped = true; |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | static int spi_unmap_msg(struct spi_master *master, struct spi_message *msg) |
| 691 | { |
| 692 | struct spi_transfer *xfer; |
| 693 | struct device *tx_dev, *rx_dev; |
| 694 | |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 695 | if (!master->cur_msg_mapped || !master->can_dma) |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 696 | return 0; |
| 697 | |
Geert Uytterhoeven | 3fc2542 | 2014-07-10 15:29:33 +0200 | [diff] [blame] | 698 | tx_dev = master->dma_tx->device->dev; |
| 699 | rx_dev = master->dma_rx->device->dev; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 700 | |
| 701 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 702 | if (!master->can_dma(master, msg->spi, xfer)) |
| 703 | continue; |
| 704 | |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 705 | spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); |
| 706 | spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | return 0; |
| 710 | } |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 711 | #else /* !CONFIG_HAS_DMA */ |
| 712 | static inline int __spi_map_msg(struct spi_master *master, |
| 713 | struct spi_message *msg) |
| 714 | { |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | static inline int spi_unmap_msg(struct spi_master *master, |
| 719 | struct spi_message *msg) |
| 720 | { |
| 721 | return 0; |
| 722 | } |
| 723 | #endif /* !CONFIG_HAS_DMA */ |
| 724 | |
| 725 | static int spi_map_msg(struct spi_master *master, struct spi_message *msg) |
| 726 | { |
| 727 | struct spi_transfer *xfer; |
| 728 | void *tmp; |
| 729 | unsigned int max_tx, max_rx; |
| 730 | |
| 731 | if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) { |
| 732 | max_tx = 0; |
| 733 | max_rx = 0; |
| 734 | |
| 735 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 736 | if ((master->flags & SPI_MASTER_MUST_TX) && |
| 737 | !xfer->tx_buf) |
| 738 | max_tx = max(xfer->len, max_tx); |
| 739 | if ((master->flags & SPI_MASTER_MUST_RX) && |
| 740 | !xfer->rx_buf) |
| 741 | max_rx = max(xfer->len, max_rx); |
| 742 | } |
| 743 | |
| 744 | if (max_tx) { |
| 745 | tmp = krealloc(master->dummy_tx, max_tx, |
| 746 | GFP_KERNEL | GFP_DMA); |
| 747 | if (!tmp) |
| 748 | return -ENOMEM; |
| 749 | master->dummy_tx = tmp; |
| 750 | memset(tmp, 0, max_tx); |
| 751 | } |
| 752 | |
| 753 | if (max_rx) { |
| 754 | tmp = krealloc(master->dummy_rx, max_rx, |
| 755 | GFP_KERNEL | GFP_DMA); |
| 756 | if (!tmp) |
| 757 | return -ENOMEM; |
| 758 | master->dummy_rx = tmp; |
| 759 | } |
| 760 | |
| 761 | if (max_tx || max_rx) { |
| 762 | list_for_each_entry(xfer, &msg->transfers, |
| 763 | transfer_list) { |
| 764 | if (!xfer->tx_buf) |
| 765 | xfer->tx_buf = master->dummy_tx; |
| 766 | if (!xfer->rx_buf) |
| 767 | xfer->rx_buf = master->dummy_rx; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | return __spi_map_msg(master, msg); |
| 773 | } |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 774 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 775 | /* |
| 776 | * spi_transfer_one_message - Default implementation of transfer_one_message() |
| 777 | * |
| 778 | * This is a standard implementation of transfer_one_message() for |
| 779 | * drivers which impelment a transfer_one() operation. It provides |
| 780 | * standard handling of delays and chip select management. |
| 781 | */ |
| 782 | static int spi_transfer_one_message(struct spi_master *master, |
| 783 | struct spi_message *msg) |
| 784 | { |
| 785 | struct spi_transfer *xfer; |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 786 | bool keep_cs = false; |
| 787 | int ret = 0; |
Mark Brown | 16a0ce4 | 2014-01-30 22:16:41 +0000 | [diff] [blame] | 788 | int ms = 1; |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 789 | |
| 790 | spi_set_cs(msg->spi, true); |
| 791 | |
| 792 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 793 | trace_spi_transfer_start(msg, xfer); |
| 794 | |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 795 | if (xfer->tx_buf || xfer->rx_buf) { |
| 796 | reinit_completion(&master->xfer_completion); |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 797 | |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 798 | ret = master->transfer_one(master, msg->spi, xfer); |
| 799 | if (ret < 0) { |
| 800 | dev_err(&msg->spi->dev, |
| 801 | "SPI transfer failed: %d\n", ret); |
| 802 | goto out; |
| 803 | } |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 804 | |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 805 | if (ret > 0) { |
| 806 | ret = 0; |
| 807 | ms = xfer->len * 8 * 1000 / xfer->speed_hz; |
| 808 | ms += ms + 100; /* some tolerance */ |
Mark Brown | 16a0ce4 | 2014-01-30 22:16:41 +0000 | [diff] [blame] | 809 | |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 810 | ms = wait_for_completion_timeout(&master->xfer_completion, |
| 811 | msecs_to_jiffies(ms)); |
| 812 | } |
Mark Brown | 16a0ce4 | 2014-01-30 22:16:41 +0000 | [diff] [blame] | 813 | |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 814 | if (ms == 0) { |
| 815 | dev_err(&msg->spi->dev, |
| 816 | "SPI transfer timed out\n"); |
| 817 | msg->status = -ETIMEDOUT; |
| 818 | } |
| 819 | } else { |
| 820 | if (xfer->len) |
| 821 | dev_err(&msg->spi->dev, |
| 822 | "Bufferless transfer has length %u\n", |
| 823 | xfer->len); |
Axel Lin | 13a4279 | 2014-01-18 22:05:22 +0800 | [diff] [blame] | 824 | } |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 825 | |
| 826 | trace_spi_transfer_stop(msg, xfer); |
| 827 | |
| 828 | if (msg->status != -EINPROGRESS) |
| 829 | goto out; |
| 830 | |
| 831 | if (xfer->delay_usecs) |
| 832 | udelay(xfer->delay_usecs); |
| 833 | |
| 834 | if (xfer->cs_change) { |
| 835 | if (list_is_last(&xfer->transfer_list, |
| 836 | &msg->transfers)) { |
| 837 | keep_cs = true; |
| 838 | } else { |
Mark Brown | 0b73aa6 | 2014-03-29 23:48:07 +0000 | [diff] [blame] | 839 | spi_set_cs(msg->spi, false); |
| 840 | udelay(10); |
| 841 | spi_set_cs(msg->spi, true); |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
| 845 | msg->actual_length += xfer->len; |
| 846 | } |
| 847 | |
| 848 | out: |
| 849 | if (ret != 0 || !keep_cs) |
| 850 | spi_set_cs(msg->spi, false); |
| 851 | |
| 852 | if (msg->status == -EINPROGRESS) |
| 853 | msg->status = ret; |
| 854 | |
| 855 | spi_finalize_current_message(master); |
| 856 | |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * spi_finalize_current_transfer - report completion of a transfer |
| 862 | * |
| 863 | * Called by SPI drivers using the core transfer_one_message() |
| 864 | * implementation to notify it that the current interrupt driven |
Geert Uytterhoeven | 9e8f488 | 2014-01-21 16:10:05 +0100 | [diff] [blame] | 865 | * transfer has finished and the next one may be scheduled. |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 866 | */ |
| 867 | void spi_finalize_current_transfer(struct spi_master *master) |
| 868 | { |
| 869 | complete(&master->xfer_completion); |
| 870 | } |
| 871 | EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); |
| 872 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 873 | /** |
| 874 | * spi_pump_messages - kthread work function which processes spi message queue |
| 875 | * @work: pointer to kthread work struct contained in the master struct |
| 876 | * |
| 877 | * This function checks if there is any spi message in the queue that |
| 878 | * needs processing and if so call out to the driver to initialize hardware |
| 879 | * and transfer each message. |
| 880 | * |
| 881 | */ |
| 882 | static void spi_pump_messages(struct kthread_work *work) |
| 883 | { |
| 884 | struct spi_master *master = |
| 885 | container_of(work, struct spi_master, pump_messages); |
| 886 | unsigned long flags; |
| 887 | bool was_busy = false; |
| 888 | int ret; |
| 889 | |
| 890 | /* Lock queue and check for queue work */ |
| 891 | spin_lock_irqsave(&master->queue_lock, flags); |
| 892 | if (list_empty(&master->queue) || !master->running) { |
Bryan Freed | b0b36b8 | 2013-03-13 11:17:40 -0700 | [diff] [blame] | 893 | if (!master->busy) { |
| 894 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 895 | return; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 896 | } |
| 897 | master->busy = false; |
| 898 | spin_unlock_irqrestore(&master->queue_lock, flags); |
Mark Brown | 3a2eba9 | 2014-01-28 20:17:03 +0000 | [diff] [blame] | 899 | kfree(master->dummy_rx); |
| 900 | master->dummy_rx = NULL; |
| 901 | kfree(master->dummy_tx); |
| 902 | master->dummy_tx = NULL; |
Bryan Freed | b0b36b8 | 2013-03-13 11:17:40 -0700 | [diff] [blame] | 903 | if (master->unprepare_transfer_hardware && |
| 904 | master->unprepare_transfer_hardware(master)) |
| 905 | dev_err(&master->dev, |
| 906 | "failed to unprepare transfer hardware\n"); |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 907 | if (master->auto_runtime_pm) { |
| 908 | pm_runtime_mark_last_busy(master->dev.parent); |
| 909 | pm_runtime_put_autosuspend(master->dev.parent); |
| 910 | } |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 911 | trace_spi_master_idle(master); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 912 | return; |
| 913 | } |
| 914 | |
| 915 | /* Make sure we are not already running a message */ |
| 916 | if (master->cur_msg) { |
| 917 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 918 | return; |
| 919 | } |
| 920 | /* Extract head of queue */ |
| 921 | master->cur_msg = |
Axel Lin | a89e2d2 | 2014-01-09 16:03:58 +0800 | [diff] [blame] | 922 | list_first_entry(&master->queue, struct spi_message, queue); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 923 | |
| 924 | list_del_init(&master->cur_msg->queue); |
| 925 | if (master->busy) |
| 926 | was_busy = true; |
| 927 | else |
| 928 | master->busy = true; |
| 929 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 930 | |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 931 | if (!was_busy && master->auto_runtime_pm) { |
| 932 | ret = pm_runtime_get_sync(master->dev.parent); |
| 933 | if (ret < 0) { |
| 934 | dev_err(&master->dev, "Failed to power device: %d\n", |
| 935 | ret); |
| 936 | return; |
| 937 | } |
| 938 | } |
| 939 | |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 940 | if (!was_busy) |
| 941 | trace_spi_master_busy(master); |
| 942 | |
Shubhrajyoti D | 7dfd2bd | 2012-05-10 19:20:41 +0530 | [diff] [blame] | 943 | if (!was_busy && master->prepare_transfer_hardware) { |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 944 | ret = master->prepare_transfer_hardware(master); |
| 945 | if (ret) { |
| 946 | dev_err(&master->dev, |
| 947 | "failed to prepare transfer hardware\n"); |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 948 | |
| 949 | if (master->auto_runtime_pm) |
| 950 | pm_runtime_put(master->dev.parent); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 951 | return; |
| 952 | } |
| 953 | } |
| 954 | |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 955 | trace_spi_message_start(master->cur_msg); |
| 956 | |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 957 | if (master->prepare_message) { |
| 958 | ret = master->prepare_message(master, master->cur_msg); |
| 959 | if (ret) { |
| 960 | dev_err(&master->dev, |
| 961 | "failed to prepare message: %d\n", ret); |
| 962 | master->cur_msg->status = ret; |
| 963 | spi_finalize_current_message(master); |
| 964 | return; |
| 965 | } |
| 966 | master->cur_msg_prepared = true; |
| 967 | } |
| 968 | |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 969 | ret = spi_map_msg(master, master->cur_msg); |
| 970 | if (ret) { |
| 971 | master->cur_msg->status = ret; |
| 972 | spi_finalize_current_message(master); |
| 973 | return; |
| 974 | } |
| 975 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 976 | ret = master->transfer_one_message(master, master->cur_msg); |
| 977 | if (ret) { |
| 978 | dev_err(&master->dev, |
Geert Uytterhoeven | 1f802f8 | 2014-01-28 10:33:03 +0100 | [diff] [blame] | 979 | "failed to transfer one message from queue\n"); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 980 | return; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | static int spi_init_queue(struct spi_master *master) |
| 985 | { |
| 986 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; |
| 987 | |
| 988 | INIT_LIST_HEAD(&master->queue); |
| 989 | spin_lock_init(&master->queue_lock); |
| 990 | |
| 991 | master->running = false; |
| 992 | master->busy = false; |
| 993 | |
| 994 | init_kthread_worker(&master->kworker); |
| 995 | master->kworker_task = kthread_run(kthread_worker_fn, |
Kees Cook | f170168 | 2013-07-03 15:04:58 -0700 | [diff] [blame] | 996 | &master->kworker, "%s", |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 997 | dev_name(&master->dev)); |
| 998 | if (IS_ERR(master->kworker_task)) { |
| 999 | dev_err(&master->dev, "failed to create message pump task\n"); |
| 1000 | return -ENOMEM; |
| 1001 | } |
| 1002 | init_kthread_work(&master->pump_messages, spi_pump_messages); |
| 1003 | |
| 1004 | /* |
| 1005 | * Master config will indicate if this controller should run the |
| 1006 | * message pump with high (realtime) priority to reduce the transfer |
| 1007 | * latency on the bus by minimising the delay between a transfer |
| 1008 | * request and the scheduling of the message pump thread. Without this |
| 1009 | * setting the message pump thread will remain at default priority. |
| 1010 | */ |
| 1011 | if (master->rt) { |
| 1012 | dev_info(&master->dev, |
| 1013 | "will run message pump with realtime priority\n"); |
| 1014 | sched_setscheduler(master->kworker_task, SCHED_FIFO, ¶m); |
| 1015 | } |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | /** |
| 1021 | * spi_get_next_queued_message() - called by driver to check for queued |
| 1022 | * messages |
| 1023 | * @master: the master to check for queued messages |
| 1024 | * |
| 1025 | * If there are more messages in the queue, the next message is returned from |
| 1026 | * this call. |
| 1027 | */ |
| 1028 | struct spi_message *spi_get_next_queued_message(struct spi_master *master) |
| 1029 | { |
| 1030 | struct spi_message *next; |
| 1031 | unsigned long flags; |
| 1032 | |
| 1033 | /* get a pointer to the next message, if any */ |
| 1034 | spin_lock_irqsave(&master->queue_lock, flags); |
Axel Lin | 1cfd97f | 2014-01-02 15:16:40 +0800 | [diff] [blame] | 1035 | next = list_first_entry_or_null(&master->queue, struct spi_message, |
| 1036 | queue); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1037 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1038 | |
| 1039 | return next; |
| 1040 | } |
| 1041 | EXPORT_SYMBOL_GPL(spi_get_next_queued_message); |
| 1042 | |
| 1043 | /** |
| 1044 | * spi_finalize_current_message() - the current message is complete |
| 1045 | * @master: the master to return the message to |
| 1046 | * |
| 1047 | * Called by the driver to notify the core that the message in the front of the |
| 1048 | * queue is complete and can be removed from the queue. |
| 1049 | */ |
| 1050 | void spi_finalize_current_message(struct spi_master *master) |
| 1051 | { |
| 1052 | struct spi_message *mesg; |
| 1053 | unsigned long flags; |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1054 | int ret; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1055 | |
| 1056 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1057 | mesg = master->cur_msg; |
| 1058 | master->cur_msg = NULL; |
| 1059 | |
| 1060 | queue_kthread_work(&master->kworker, &master->pump_messages); |
| 1061 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1062 | |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1063 | spi_unmap_msg(master, mesg); |
| 1064 | |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1065 | if (master->cur_msg_prepared && master->unprepare_message) { |
| 1066 | ret = master->unprepare_message(master, mesg); |
| 1067 | if (ret) { |
| 1068 | dev_err(&master->dev, |
| 1069 | "failed to unprepare message: %d\n", ret); |
| 1070 | } |
| 1071 | } |
| 1072 | master->cur_msg_prepared = false; |
| 1073 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1074 | mesg->state = NULL; |
| 1075 | if (mesg->complete) |
| 1076 | mesg->complete(mesg->context); |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 1077 | |
| 1078 | trace_spi_message_done(mesg); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1079 | } |
| 1080 | EXPORT_SYMBOL_GPL(spi_finalize_current_message); |
| 1081 | |
| 1082 | static int spi_start_queue(struct spi_master *master) |
| 1083 | { |
| 1084 | unsigned long flags; |
| 1085 | |
| 1086 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1087 | |
| 1088 | if (master->running || master->busy) { |
| 1089 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1090 | return -EBUSY; |
| 1091 | } |
| 1092 | |
| 1093 | master->running = true; |
| 1094 | master->cur_msg = NULL; |
| 1095 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1096 | |
| 1097 | queue_kthread_work(&master->kworker, &master->pump_messages); |
| 1098 | |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static int spi_stop_queue(struct spi_master *master) |
| 1103 | { |
| 1104 | unsigned long flags; |
| 1105 | unsigned limit = 500; |
| 1106 | int ret = 0; |
| 1107 | |
| 1108 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1109 | |
| 1110 | /* |
| 1111 | * This is a bit lame, but is optimized for the common execution path. |
| 1112 | * A wait_queue on the master->busy could be used, but then the common |
| 1113 | * execution path (pump_messages) would be required to call wake_up or |
| 1114 | * friends on every SPI message. Do this instead. |
| 1115 | */ |
| 1116 | while ((!list_empty(&master->queue) || master->busy) && limit--) { |
| 1117 | spin_unlock_irqrestore(&master->queue_lock, flags); |
Axel Lin | f97b26b | 2014-02-21 09:15:18 +0800 | [diff] [blame] | 1118 | usleep_range(10000, 11000); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1119 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1120 | } |
| 1121 | |
| 1122 | if (!list_empty(&master->queue) || master->busy) |
| 1123 | ret = -EBUSY; |
| 1124 | else |
| 1125 | master->running = false; |
| 1126 | |
| 1127 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1128 | |
| 1129 | if (ret) { |
| 1130 | dev_warn(&master->dev, |
| 1131 | "could not stop message queue\n"); |
| 1132 | return ret; |
| 1133 | } |
| 1134 | return ret; |
| 1135 | } |
| 1136 | |
| 1137 | static int spi_destroy_queue(struct spi_master *master) |
| 1138 | { |
| 1139 | int ret; |
| 1140 | |
| 1141 | ret = spi_stop_queue(master); |
| 1142 | |
| 1143 | /* |
| 1144 | * flush_kthread_worker will block until all work is done. |
| 1145 | * If the reason that stop_queue timed out is that the work will never |
| 1146 | * finish, then it does no good to call flush/stop thread, so |
| 1147 | * return anyway. |
| 1148 | */ |
| 1149 | if (ret) { |
| 1150 | dev_err(&master->dev, "problem destroying queue\n"); |
| 1151 | return ret; |
| 1152 | } |
| 1153 | |
| 1154 | flush_kthread_worker(&master->kworker); |
| 1155 | kthread_stop(master->kworker_task); |
| 1156 | |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | /** |
| 1161 | * spi_queued_transfer - transfer function for queued transfers |
| 1162 | * @spi: spi device which is requesting transfer |
| 1163 | * @msg: spi message which is to handled is queued to driver queue |
| 1164 | */ |
| 1165 | static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) |
| 1166 | { |
| 1167 | struct spi_master *master = spi->master; |
| 1168 | unsigned long flags; |
| 1169 | |
| 1170 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1171 | |
| 1172 | if (!master->running) { |
| 1173 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1174 | return -ESHUTDOWN; |
| 1175 | } |
| 1176 | msg->actual_length = 0; |
| 1177 | msg->status = -EINPROGRESS; |
| 1178 | |
| 1179 | list_add_tail(&msg->queue, &master->queue); |
Axel Lin | 96b3eac | 2013-08-22 23:41:34 +0800 | [diff] [blame] | 1180 | if (!master->busy) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1181 | queue_kthread_work(&master->kworker, &master->pump_messages); |
| 1182 | |
| 1183 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | static int spi_master_initialize_queue(struct spi_master *master) |
| 1188 | { |
| 1189 | int ret; |
| 1190 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1191 | master->transfer = spi_queued_transfer; |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1192 | if (!master->transfer_one_message) |
| 1193 | master->transfer_one_message = spi_transfer_one_message; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1194 | |
| 1195 | /* Initialize and start queue */ |
| 1196 | ret = spi_init_queue(master); |
| 1197 | if (ret) { |
| 1198 | dev_err(&master->dev, "problem initializing queue\n"); |
| 1199 | goto err_init_queue; |
| 1200 | } |
Mark Brown | c3676d5 | 2014-05-01 10:47:52 -0700 | [diff] [blame] | 1201 | master->queued = true; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1202 | ret = spi_start_queue(master); |
| 1203 | if (ret) { |
| 1204 | dev_err(&master->dev, "problem starting queue\n"); |
| 1205 | goto err_start_queue; |
| 1206 | } |
| 1207 | |
| 1208 | return 0; |
| 1209 | |
| 1210 | err_start_queue: |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1211 | spi_destroy_queue(master); |
Mark Brown | c3676d5 | 2014-05-01 10:47:52 -0700 | [diff] [blame] | 1212 | err_init_queue: |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1213 | return ret; |
| 1214 | } |
| 1215 | |
| 1216 | /*-------------------------------------------------------------------------*/ |
| 1217 | |
Andreas Larsson | 7cb9436 | 2012-12-04 15:09:38 +0100 | [diff] [blame] | 1218 | #if defined(CONFIG_OF) |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1219 | /** |
| 1220 | * of_register_spi_devices() - Register child devices onto the SPI bus |
| 1221 | * @master: Pointer to spi_master device |
| 1222 | * |
| 1223 | * Registers an spi_device for each child node of master node which has a 'reg' |
| 1224 | * property. |
| 1225 | */ |
| 1226 | static void of_register_spi_devices(struct spi_master *master) |
| 1227 | { |
| 1228 | struct spi_device *spi; |
| 1229 | struct device_node *nc; |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1230 | int rc; |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1231 | u32 value; |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1232 | |
| 1233 | if (!master->dev.of_node) |
| 1234 | return; |
| 1235 | |
Alexander Sverdlin | f3b6159 | 2012-11-29 08:59:29 +0100 | [diff] [blame] | 1236 | for_each_available_child_of_node(master->dev.of_node, nc) { |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1237 | /* Alloc an spi_device */ |
| 1238 | spi = spi_alloc_device(master); |
| 1239 | if (!spi) { |
| 1240 | dev_err(&master->dev, "spi_device alloc error for %s\n", |
| 1241 | nc->full_name); |
| 1242 | spi_dev_put(spi); |
| 1243 | continue; |
| 1244 | } |
| 1245 | |
| 1246 | /* Select device driver */ |
| 1247 | if (of_modalias_node(nc, spi->modalias, |
| 1248 | sizeof(spi->modalias)) < 0) { |
| 1249 | dev_err(&master->dev, "cannot find modalias for %s\n", |
| 1250 | nc->full_name); |
| 1251 | spi_dev_put(spi); |
| 1252 | continue; |
| 1253 | } |
| 1254 | |
| 1255 | /* Device address */ |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1256 | rc = of_property_read_u32(nc, "reg", &value); |
| 1257 | if (rc) { |
| 1258 | dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n", |
| 1259 | nc->full_name, rc); |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1260 | spi_dev_put(spi); |
| 1261 | continue; |
| 1262 | } |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1263 | spi->chip_select = value; |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1264 | |
| 1265 | /* Mode (clock phase/polarity/etc.) */ |
| 1266 | if (of_find_property(nc, "spi-cpha", NULL)) |
| 1267 | spi->mode |= SPI_CPHA; |
| 1268 | if (of_find_property(nc, "spi-cpol", NULL)) |
| 1269 | spi->mode |= SPI_CPOL; |
| 1270 | if (of_find_property(nc, "spi-cs-high", NULL)) |
| 1271 | spi->mode |= SPI_CS_HIGH; |
Lars-Peter Clausen | c20151d | 2012-12-06 16:55:33 +0100 | [diff] [blame] | 1272 | if (of_find_property(nc, "spi-3wire", NULL)) |
| 1273 | spi->mode |= SPI_3WIRE; |
Zhao Qiang | cd6339e | 2014-04-01 17:10:50 +0800 | [diff] [blame] | 1274 | if (of_find_property(nc, "spi-lsb-first", NULL)) |
| 1275 | spi->mode |= SPI_LSB_FIRST; |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1276 | |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 1277 | /* Device DUAL/QUAD mode */ |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1278 | if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { |
| 1279 | switch (value) { |
| 1280 | case 1: |
Mark Brown | a822e99 | 2013-08-30 23:19:40 +0100 | [diff] [blame] | 1281 | break; |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1282 | case 2: |
Mark Brown | a822e99 | 2013-08-30 23:19:40 +0100 | [diff] [blame] | 1283 | spi->mode |= SPI_TX_DUAL; |
| 1284 | break; |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1285 | case 4: |
Mark Brown | a822e99 | 2013-08-30 23:19:40 +0100 | [diff] [blame] | 1286 | spi->mode |= SPI_TX_QUAD; |
| 1287 | break; |
| 1288 | default: |
Geert Uytterhoeven | 80874d8 | 2014-05-26 14:05:25 +0200 | [diff] [blame] | 1289 | dev_warn(&master->dev, |
| 1290 | "spi-tx-bus-width %d not supported\n", |
| 1291 | value); |
| 1292 | break; |
Mark Brown | a822e99 | 2013-08-30 23:19:40 +0100 | [diff] [blame] | 1293 | } |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 1294 | } |
| 1295 | |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1296 | if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { |
| 1297 | switch (value) { |
| 1298 | case 1: |
Mark Brown | a822e99 | 2013-08-30 23:19:40 +0100 | [diff] [blame] | 1299 | break; |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1300 | case 2: |
Mark Brown | a822e99 | 2013-08-30 23:19:40 +0100 | [diff] [blame] | 1301 | spi->mode |= SPI_RX_DUAL; |
| 1302 | break; |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1303 | case 4: |
Mark Brown | a822e99 | 2013-08-30 23:19:40 +0100 | [diff] [blame] | 1304 | spi->mode |= SPI_RX_QUAD; |
| 1305 | break; |
| 1306 | default: |
Geert Uytterhoeven | 80874d8 | 2014-05-26 14:05:25 +0200 | [diff] [blame] | 1307 | dev_warn(&master->dev, |
| 1308 | "spi-rx-bus-width %d not supported\n", |
| 1309 | value); |
| 1310 | break; |
Mark Brown | a822e99 | 2013-08-30 23:19:40 +0100 | [diff] [blame] | 1311 | } |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 1312 | } |
| 1313 | |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1314 | /* Device speed */ |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1315 | rc = of_property_read_u32(nc, "spi-max-frequency", &value); |
| 1316 | if (rc) { |
| 1317 | dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n", |
| 1318 | nc->full_name, rc); |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1319 | spi_dev_put(spi); |
| 1320 | continue; |
| 1321 | } |
Trent Piepho | 89da429 | 2013-09-27 05:37:25 -0700 | [diff] [blame] | 1322 | spi->max_speed_hz = value; |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1323 | |
| 1324 | /* IRQ */ |
| 1325 | spi->irq = irq_of_parse_and_map(nc, 0); |
| 1326 | |
| 1327 | /* Store a pointer to the node in the device structure */ |
| 1328 | of_node_get(nc); |
| 1329 | spi->dev.of_node = nc; |
| 1330 | |
| 1331 | /* Register the new device */ |
Mathias Krause | 70fac17 | 2013-08-31 20:24:14 +0200 | [diff] [blame] | 1332 | request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias); |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1333 | rc = spi_add_device(spi); |
| 1334 | if (rc) { |
| 1335 | dev_err(&master->dev, "spi_device register error %s\n", |
| 1336 | nc->full_name); |
| 1337 | spi_dev_put(spi); |
| 1338 | } |
| 1339 | |
| 1340 | } |
| 1341 | } |
| 1342 | #else |
| 1343 | static void of_register_spi_devices(struct spi_master *master) { } |
| 1344 | #endif |
| 1345 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1346 | #ifdef CONFIG_ACPI |
| 1347 | static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) |
| 1348 | { |
| 1349 | struct spi_device *spi = data; |
| 1350 | |
| 1351 | if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { |
| 1352 | struct acpi_resource_spi_serialbus *sb; |
| 1353 | |
| 1354 | sb = &ares->data.spi_serial_bus; |
| 1355 | if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { |
| 1356 | spi->chip_select = sb->device_selection; |
| 1357 | spi->max_speed_hz = sb->connection_speed; |
| 1358 | |
| 1359 | if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) |
| 1360 | spi->mode |= SPI_CPHA; |
| 1361 | if (sb->clock_polarity == ACPI_SPI_START_HIGH) |
| 1362 | spi->mode |= SPI_CPOL; |
| 1363 | if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) |
| 1364 | spi->mode |= SPI_CS_HIGH; |
| 1365 | } |
| 1366 | } else if (spi->irq < 0) { |
| 1367 | struct resource r; |
| 1368 | |
| 1369 | if (acpi_dev_resource_interrupt(ares, 0, &r)) |
| 1370 | spi->irq = r.start; |
| 1371 | } |
| 1372 | |
| 1373 | /* Always tell the ACPI core to skip this resource */ |
| 1374 | return 1; |
| 1375 | } |
| 1376 | |
| 1377 | static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, |
| 1378 | void *data, void **return_value) |
| 1379 | { |
| 1380 | struct spi_master *master = data; |
| 1381 | struct list_head resource_list; |
| 1382 | struct acpi_device *adev; |
| 1383 | struct spi_device *spi; |
| 1384 | int ret; |
| 1385 | |
| 1386 | if (acpi_bus_get_device(handle, &adev)) |
| 1387 | return AE_OK; |
| 1388 | if (acpi_bus_get_status(adev) || !adev->status.present) |
| 1389 | return AE_OK; |
| 1390 | |
| 1391 | spi = spi_alloc_device(master); |
| 1392 | if (!spi) { |
| 1393 | dev_err(&master->dev, "failed to allocate SPI device for %s\n", |
| 1394 | dev_name(&adev->dev)); |
| 1395 | return AE_NO_MEMORY; |
| 1396 | } |
| 1397 | |
Rafael J. Wysocki | 7b19981 | 2013-11-11 22:41:56 +0100 | [diff] [blame] | 1398 | ACPI_COMPANION_SET(&spi->dev, adev); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1399 | spi->irq = -1; |
| 1400 | |
| 1401 | INIT_LIST_HEAD(&resource_list); |
| 1402 | ret = acpi_dev_get_resources(adev, &resource_list, |
| 1403 | acpi_spi_add_resource, spi); |
| 1404 | acpi_dev_free_resource_list(&resource_list); |
| 1405 | |
| 1406 | if (ret < 0 || !spi->max_speed_hz) { |
| 1407 | spi_dev_put(spi); |
| 1408 | return AE_OK; |
| 1409 | } |
| 1410 | |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 1411 | adev->power.flags.ignore_parent = true; |
Jarkko Nikula | cf9eb39 | 2013-10-10 17:19:17 +0300 | [diff] [blame] | 1412 | strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias)); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1413 | if (spi_add_device(spi)) { |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 1414 | adev->power.flags.ignore_parent = false; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1415 | dev_err(&master->dev, "failed to add SPI device %s from ACPI\n", |
| 1416 | dev_name(&adev->dev)); |
| 1417 | spi_dev_put(spi); |
| 1418 | } |
| 1419 | |
| 1420 | return AE_OK; |
| 1421 | } |
| 1422 | |
| 1423 | static void acpi_register_spi_devices(struct spi_master *master) |
| 1424 | { |
| 1425 | acpi_status status; |
| 1426 | acpi_handle handle; |
| 1427 | |
Rafael J. Wysocki | 2989617 | 2013-04-01 00:21:08 +0000 | [diff] [blame] | 1428 | handle = ACPI_HANDLE(master->dev.parent); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1429 | if (!handle) |
| 1430 | return; |
| 1431 | |
| 1432 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, |
| 1433 | acpi_spi_add_device, NULL, |
| 1434 | master, NULL); |
| 1435 | if (ACPI_FAILURE(status)) |
| 1436 | dev_warn(&master->dev, "failed to enumerate SPI slaves\n"); |
| 1437 | } |
| 1438 | #else |
| 1439 | static inline void acpi_register_spi_devices(struct spi_master *master) {} |
| 1440 | #endif /* CONFIG_ACPI */ |
| 1441 | |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1442 | static void spi_master_release(struct device *dev) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1443 | { |
| 1444 | struct spi_master *master; |
| 1445 | |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1446 | master = container_of(dev, struct spi_master, dev); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1447 | kfree(master); |
| 1448 | } |
| 1449 | |
| 1450 | static struct class spi_master_class = { |
| 1451 | .name = "spi_master", |
| 1452 | .owner = THIS_MODULE, |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1453 | .dev_release = spi_master_release, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1454 | }; |
| 1455 | |
| 1456 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1457 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1458 | /** |
| 1459 | * spi_alloc_master - allocate SPI master controller |
| 1460 | * @dev: the controller, possibly using the platform_bus |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 1461 | * @size: how much zeroed driver-private data to allocate; the pointer to this |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1462 | * memory is in the driver_data field of the returned device, |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1463 | * accessible with spi_master_get_devdata(). |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 1464 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1465 | * |
| 1466 | * This call is used only by SPI master controller drivers, which are the |
| 1467 | * only ones directly touching chip registers. It's how they allocate |
dmitry pervushin | ba1a051 | 2006-05-20 15:00:14 -0700 | [diff] [blame] | 1468 | * an spi_master structure, prior to calling spi_register_master(). |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1469 | * |
| 1470 | * This must be called from context that can sleep. It returns the SPI |
| 1471 | * master structure on success, else NULL. |
| 1472 | * |
| 1473 | * The caller is responsible for assigning the bus number and initializing |
dmitry pervushin | ba1a051 | 2006-05-20 15:00:14 -0700 | [diff] [blame] | 1474 | * the master's methods before calling spi_register_master(); and (after errors |
Uwe Kleine-König | eb4af0f | 2012-02-23 10:40:14 +0100 | [diff] [blame] | 1475 | * adding the device) calling spi_master_put() and kfree() to prevent a memory |
| 1476 | * leak. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1477 | */ |
Adrian Bunk | e9d5a46 | 2007-03-26 21:32:23 -0800 | [diff] [blame] | 1478 | struct spi_master *spi_alloc_master(struct device *dev, unsigned size) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1479 | { |
| 1480 | struct spi_master *master; |
| 1481 | |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1482 | if (!dev) |
| 1483 | return NULL; |
| 1484 | |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 1485 | master = kzalloc(size + sizeof(*master), GFP_KERNEL); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1486 | if (!master) |
| 1487 | return NULL; |
| 1488 | |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1489 | device_initialize(&master->dev); |
Grant Likely | 1e8a52e | 2012-05-19 23:42:08 -0600 | [diff] [blame] | 1490 | master->bus_num = -1; |
| 1491 | master->num_chipselect = 1; |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1492 | master->dev.class = &spi_master_class; |
| 1493 | master->dev.parent = get_device(dev); |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1494 | spi_master_set_devdata(master, &master[1]); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1495 | |
| 1496 | return master; |
| 1497 | } |
| 1498 | EXPORT_SYMBOL_GPL(spi_alloc_master); |
| 1499 | |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1500 | #ifdef CONFIG_OF |
| 1501 | static int of_spi_register_master(struct spi_master *master) |
| 1502 | { |
Grant Likely | e80beb2 | 2013-02-12 17:48:37 +0000 | [diff] [blame] | 1503 | int nb, i, *cs; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1504 | struct device_node *np = master->dev.of_node; |
| 1505 | |
| 1506 | if (!np) |
| 1507 | return 0; |
| 1508 | |
| 1509 | nb = of_gpio_named_count(np, "cs-gpios"); |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 1510 | master->num_chipselect = max_t(int, nb, master->num_chipselect); |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1511 | |
Andreas Larsson | 8ec5d84 | 2013-02-13 14:23:24 +0100 | [diff] [blame] | 1512 | /* Return error only for an incorrectly formed cs-gpios property */ |
| 1513 | if (nb == 0 || nb == -ENOENT) |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1514 | return 0; |
Andreas Larsson | 8ec5d84 | 2013-02-13 14:23:24 +0100 | [diff] [blame] | 1515 | else if (nb < 0) |
| 1516 | return nb; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1517 | |
| 1518 | cs = devm_kzalloc(&master->dev, |
| 1519 | sizeof(int) * master->num_chipselect, |
| 1520 | GFP_KERNEL); |
| 1521 | master->cs_gpios = cs; |
| 1522 | |
| 1523 | if (!master->cs_gpios) |
| 1524 | return -ENOMEM; |
| 1525 | |
Andreas Larsson | 0da83bb | 2013-01-29 15:53:40 +0100 | [diff] [blame] | 1526 | for (i = 0; i < master->num_chipselect; i++) |
Andreas Larsson | 446411e | 2013-02-13 14:20:25 +0100 | [diff] [blame] | 1527 | cs[i] = -ENOENT; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1528 | |
| 1529 | for (i = 0; i < nb; i++) |
| 1530 | cs[i] = of_get_named_gpio(np, "cs-gpios", i); |
| 1531 | |
| 1532 | return 0; |
| 1533 | } |
| 1534 | #else |
| 1535 | static int of_spi_register_master(struct spi_master *master) |
| 1536 | { |
| 1537 | return 0; |
| 1538 | } |
| 1539 | #endif |
| 1540 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1541 | /** |
| 1542 | * spi_register_master - register SPI master controller |
| 1543 | * @master: initialized master, originally from spi_alloc_master() |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 1544 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1545 | * |
| 1546 | * SPI master controllers connect to their drivers using some non-SPI bus, |
| 1547 | * such as the platform bus. The final stage of probe() in that code |
| 1548 | * includes calling spi_register_master() to hook up to this SPI bus glue. |
| 1549 | * |
| 1550 | * SPI controllers use board specific (often SOC specific) bus numbers, |
| 1551 | * and board-specific addressing for SPI devices combines those numbers |
| 1552 | * with chip select numbers. Since SPI does not directly support dynamic |
| 1553 | * device identification, boards need configuration tables telling which |
| 1554 | * chip is at which address. |
| 1555 | * |
| 1556 | * This must be called from context that can sleep. It returns zero on |
| 1557 | * success, else a negative error code (dropping the master's refcount). |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1558 | * After a successful return, the caller is responsible for calling |
| 1559 | * spi_unregister_master(). |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1560 | */ |
Adrian Bunk | e9d5a46 | 2007-03-26 21:32:23 -0800 | [diff] [blame] | 1561 | int spi_register_master(struct spi_master *master) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1562 | { |
David Brownell | e44a45a | 2007-06-03 13:50:40 -0700 | [diff] [blame] | 1563 | static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1); |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1564 | struct device *dev = master->dev.parent; |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 1565 | struct boardinfo *bi; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1566 | int status = -ENODEV; |
| 1567 | int dynamic = 0; |
| 1568 | |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1569 | if (!dev) |
| 1570 | return -ENODEV; |
| 1571 | |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1572 | status = of_spi_register_master(master); |
| 1573 | if (status) |
| 1574 | return status; |
| 1575 | |
David Brownell | 082c8cb | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 1576 | /* even if it's just one always-selected device, there must |
| 1577 | * be at least one chipselect |
| 1578 | */ |
| 1579 | if (master->num_chipselect == 0) |
| 1580 | return -EINVAL; |
| 1581 | |
Grant Likely | bb29785 | 2012-12-21 19:32:09 +0000 | [diff] [blame] | 1582 | if ((master->bus_num < 0) && master->dev.of_node) |
| 1583 | master->bus_num = of_alias_get_id(master->dev.of_node, "spi"); |
| 1584 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1585 | /* convention: dynamically assigned bus IDs count down from the max */ |
David Brownell | a020ed7 | 2006-04-03 15:49:04 -0700 | [diff] [blame] | 1586 | if (master->bus_num < 0) { |
David Brownell | 082c8cb | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 1587 | /* FIXME switch to an IDR based scheme, something like |
| 1588 | * I2C now uses, so we can't run out of "dynamic" IDs |
| 1589 | */ |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1590 | master->bus_num = atomic_dec_return(&dyn_bus_id); |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 1591 | dynamic = 1; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1592 | } |
| 1593 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1594 | spin_lock_init(&master->bus_lock_spinlock); |
| 1595 | mutex_init(&master->bus_lock_mutex); |
| 1596 | master->bus_lock_flag = 0; |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1597 | init_completion(&master->xfer_completion); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 1598 | if (!master->max_dma_len) |
| 1599 | master->max_dma_len = INT_MAX; |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1600 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1601 | /* register the device, then userspace will see it. |
| 1602 | * registration fails if the bus ID is in use. |
| 1603 | */ |
Kay Sievers | 35f74fc | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 1604 | dev_set_name(&master->dev, "spi%u", master->bus_num); |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1605 | status = device_add(&master->dev); |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 1606 | if (status < 0) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1607 | goto done; |
Kay Sievers | 35f74fc | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 1608 | dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev), |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1609 | dynamic ? " (dynamic)" : ""); |
| 1610 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1611 | /* If we're using a queued driver, start the queue */ |
| 1612 | if (master->transfer) |
| 1613 | dev_info(dev, "master is unqueued, this is deprecated\n"); |
| 1614 | else { |
| 1615 | status = spi_master_initialize_queue(master); |
| 1616 | if (status) { |
Axel Lin | e93b072 | 2013-08-31 20:25:52 +0800 | [diff] [blame] | 1617 | device_del(&master->dev); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1618 | goto done; |
| 1619 | } |
| 1620 | } |
| 1621 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 1622 | mutex_lock(&board_lock); |
| 1623 | list_add_tail(&master->list, &spi_master_list); |
| 1624 | list_for_each_entry(bi, &board_list, list) |
| 1625 | spi_match_master_to_boardinfo(master, &bi->board_info); |
| 1626 | mutex_unlock(&board_lock); |
| 1627 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1628 | /* Register devices from the device tree and ACPI */ |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 1629 | of_register_spi_devices(master); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1630 | acpi_register_spi_devices(master); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1631 | done: |
| 1632 | return status; |
| 1633 | } |
| 1634 | EXPORT_SYMBOL_GPL(spi_register_master); |
| 1635 | |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 1636 | static void devm_spi_unregister(struct device *dev, void *res) |
| 1637 | { |
| 1638 | spi_unregister_master(*(struct spi_master **)res); |
| 1639 | } |
| 1640 | |
| 1641 | /** |
| 1642 | * dev_spi_register_master - register managed SPI master controller |
| 1643 | * @dev: device managing SPI master |
| 1644 | * @master: initialized master, originally from spi_alloc_master() |
| 1645 | * Context: can sleep |
| 1646 | * |
| 1647 | * Register a SPI device as with spi_register_master() which will |
| 1648 | * automatically be unregister |
| 1649 | */ |
| 1650 | int devm_spi_register_master(struct device *dev, struct spi_master *master) |
| 1651 | { |
| 1652 | struct spi_master **ptr; |
| 1653 | int ret; |
| 1654 | |
| 1655 | ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); |
| 1656 | if (!ptr) |
| 1657 | return -ENOMEM; |
| 1658 | |
| 1659 | ret = spi_register_master(master); |
Stephen Warren | 4b92894 | 2013-11-21 16:11:15 -0700 | [diff] [blame] | 1660 | if (!ret) { |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 1661 | *ptr = master; |
| 1662 | devres_add(dev, ptr); |
| 1663 | } else { |
| 1664 | devres_free(ptr); |
| 1665 | } |
| 1666 | |
| 1667 | return ret; |
| 1668 | } |
| 1669 | EXPORT_SYMBOL_GPL(devm_spi_register_master); |
| 1670 | |
David Lamparter | 3486008 | 2010-08-30 23:54:17 +0200 | [diff] [blame] | 1671 | static int __unregister(struct device *dev, void *null) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1672 | { |
David Lamparter | 3486008 | 2010-08-30 23:54:17 +0200 | [diff] [blame] | 1673 | spi_unregister_device(to_spi_device(dev)); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1674 | return 0; |
| 1675 | } |
| 1676 | |
| 1677 | /** |
| 1678 | * spi_unregister_master - unregister SPI master controller |
| 1679 | * @master: the master being unregistered |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 1680 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1681 | * |
| 1682 | * This call is used only by SPI master controller drivers, which are the |
| 1683 | * only ones directly touching chip registers. |
| 1684 | * |
| 1685 | * This must be called from context that can sleep. |
| 1686 | */ |
| 1687 | void spi_unregister_master(struct spi_master *master) |
| 1688 | { |
Jeff Garzik | 89fc9a1 | 2006-12-06 20:35:35 -0800 | [diff] [blame] | 1689 | int dummy; |
| 1690 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1691 | if (master->queued) { |
| 1692 | if (spi_destroy_queue(master)) |
| 1693 | dev_err(&master->dev, "queue remove failed\n"); |
| 1694 | } |
| 1695 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 1696 | mutex_lock(&board_lock); |
| 1697 | list_del(&master->list); |
| 1698 | mutex_unlock(&board_lock); |
| 1699 | |
Sebastian Andrzej Siewior | 97dbf37 | 2010-12-21 17:24:31 -0800 | [diff] [blame] | 1700 | dummy = device_for_each_child(&master->dev, NULL, __unregister); |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1701 | device_unregister(&master->dev); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1702 | } |
| 1703 | EXPORT_SYMBOL_GPL(spi_unregister_master); |
| 1704 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1705 | int spi_master_suspend(struct spi_master *master) |
| 1706 | { |
| 1707 | int ret; |
| 1708 | |
| 1709 | /* Basically no-ops for non-queued masters */ |
| 1710 | if (!master->queued) |
| 1711 | return 0; |
| 1712 | |
| 1713 | ret = spi_stop_queue(master); |
| 1714 | if (ret) |
| 1715 | dev_err(&master->dev, "queue stop failed\n"); |
| 1716 | |
| 1717 | return ret; |
| 1718 | } |
| 1719 | EXPORT_SYMBOL_GPL(spi_master_suspend); |
| 1720 | |
| 1721 | int spi_master_resume(struct spi_master *master) |
| 1722 | { |
| 1723 | int ret; |
| 1724 | |
| 1725 | if (!master->queued) |
| 1726 | return 0; |
| 1727 | |
| 1728 | ret = spi_start_queue(master); |
| 1729 | if (ret) |
| 1730 | dev_err(&master->dev, "queue restart failed\n"); |
| 1731 | |
| 1732 | return ret; |
| 1733 | } |
| 1734 | EXPORT_SYMBOL_GPL(spi_master_resume); |
| 1735 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 1736 | static int __spi_master_match(struct device *dev, const void *data) |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 1737 | { |
| 1738 | struct spi_master *m; |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 1739 | const u16 *bus_num = data; |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 1740 | |
| 1741 | m = container_of(dev, struct spi_master, dev); |
| 1742 | return m->bus_num == *bus_num; |
| 1743 | } |
| 1744 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1745 | /** |
| 1746 | * spi_busnum_to_master - look up master associated with bus_num |
| 1747 | * @bus_num: the master's bus number |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 1748 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1749 | * |
| 1750 | * This call may be used with devices that are registered after |
| 1751 | * arch init time. It returns a refcounted pointer to the relevant |
| 1752 | * spi_master (which the caller must release), or NULL if there is |
| 1753 | * no such master registered. |
| 1754 | */ |
| 1755 | struct spi_master *spi_busnum_to_master(u16 bus_num) |
| 1756 | { |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1757 | struct device *dev; |
Atsushi Nemoto | 1e9a51d | 2007-01-26 00:56:54 -0800 | [diff] [blame] | 1758 | struct spi_master *master = NULL; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1759 | |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 1760 | dev = class_find_device(&spi_master_class, NULL, &bus_num, |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 1761 | __spi_master_match); |
| 1762 | if (dev) |
| 1763 | master = container_of(dev, struct spi_master, dev); |
| 1764 | /* reference got in class_find_device */ |
Atsushi Nemoto | 1e9a51d | 2007-01-26 00:56:54 -0800 | [diff] [blame] | 1765 | return master; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1766 | } |
| 1767 | EXPORT_SYMBOL_GPL(spi_busnum_to_master); |
| 1768 | |
| 1769 | |
| 1770 | /*-------------------------------------------------------------------------*/ |
| 1771 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 1772 | /* Core methods for SPI master protocol drivers. Some of the |
| 1773 | * other core methods are currently defined as inline functions. |
| 1774 | */ |
| 1775 | |
| 1776 | /** |
| 1777 | * spi_setup - setup SPI mode and clock rate |
| 1778 | * @spi: the device whose settings are being modified |
| 1779 | * Context: can sleep, and no requests are queued to the device |
| 1780 | * |
| 1781 | * SPI protocol drivers may need to update the transfer mode if the |
| 1782 | * device doesn't work with its default. They may likewise need |
| 1783 | * to update clock rates or word sizes from initial values. This function |
| 1784 | * changes those settings, and must be called from a context that can sleep. |
| 1785 | * Except for SPI_CS_HIGH, which takes effect immediately, the changes take |
| 1786 | * effect the next time the device is selected and data is transferred to |
| 1787 | * or from it. When this function returns, the spi device is deselected. |
| 1788 | * |
| 1789 | * Note that this call will fail if the protocol driver specifies an option |
| 1790 | * that the underlying controller or its driver does not support. For |
| 1791 | * example, not all hardware supports wire transfers using nine bit words, |
| 1792 | * LSB-first wire encoding, or active-high chipselects. |
| 1793 | */ |
| 1794 | int spi_setup(struct spi_device *spi) |
| 1795 | { |
Geert Uytterhoeven | 83596fb | 2014-04-14 19:39:53 +0200 | [diff] [blame] | 1796 | unsigned bad_bits, ugly_bits; |
Laxman Dewangan | caae070 | 2012-11-09 14:35:22 +0530 | [diff] [blame] | 1797 | int status = 0; |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 1798 | |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 1799 | /* check mode to prevent that DUAL and QUAD set at the same time |
| 1800 | */ |
| 1801 | if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || |
| 1802 | ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { |
| 1803 | dev_err(&spi->dev, |
| 1804 | "setup: can not select dual and quad at the same time\n"); |
| 1805 | return -EINVAL; |
| 1806 | } |
| 1807 | /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden |
| 1808 | */ |
| 1809 | if ((spi->mode & SPI_3WIRE) && (spi->mode & |
| 1810 | (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD))) |
| 1811 | return -EINVAL; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1812 | /* help drivers fail *cleanly* when they need options |
| 1813 | * that aren't supported with their current master |
| 1814 | */ |
| 1815 | bad_bits = spi->mode & ~spi->master->mode_bits; |
Geert Uytterhoeven | 83596fb | 2014-04-14 19:39:53 +0200 | [diff] [blame] | 1816 | ugly_bits = bad_bits & |
| 1817 | (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD); |
| 1818 | if (ugly_bits) { |
| 1819 | dev_warn(&spi->dev, |
| 1820 | "setup: ignoring unsupported mode bits %x\n", |
| 1821 | ugly_bits); |
| 1822 | spi->mode &= ~ugly_bits; |
| 1823 | bad_bits &= ~ugly_bits; |
| 1824 | } |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1825 | if (bad_bits) { |
Linus Walleij | eb288a1 | 2010-10-21 21:06:44 +0200 | [diff] [blame] | 1826 | dev_err(&spi->dev, "setup: unsupported mode bits %x\n", |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1827 | bad_bits); |
| 1828 | return -EINVAL; |
| 1829 | } |
| 1830 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 1831 | if (!spi->bits_per_word) |
| 1832 | spi->bits_per_word = 8; |
| 1833 | |
Axel Lin | 052eb2d4 | 2014-02-10 00:08:05 +0800 | [diff] [blame] | 1834 | if (!spi->max_speed_hz) |
| 1835 | spi->max_speed_hz = spi->master->max_speed_hz; |
| 1836 | |
Laxman Dewangan | caae070 | 2012-11-09 14:35:22 +0530 | [diff] [blame] | 1837 | if (spi->master->setup) |
| 1838 | status = spi->master->setup(spi); |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 1839 | |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 1840 | dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 1841 | (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), |
| 1842 | (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", |
| 1843 | (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", |
| 1844 | (spi->mode & SPI_3WIRE) ? "3wire, " : "", |
| 1845 | (spi->mode & SPI_LOOP) ? "loopback, " : "", |
| 1846 | spi->bits_per_word, spi->max_speed_hz, |
| 1847 | status); |
| 1848 | |
| 1849 | return status; |
| 1850 | } |
| 1851 | EXPORT_SYMBOL_GPL(spi_setup); |
| 1852 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 1853 | static int __spi_validate(struct spi_device *spi, struct spi_message *message) |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1854 | { |
| 1855 | struct spi_master *master = spi->master; |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 1856 | struct spi_transfer *xfer; |
Atsushi Nemoto | 6ea3129 | 2014-02-28 23:03:16 +0900 | [diff] [blame] | 1857 | int w_size; |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1858 | |
Mark Brown | 24a0013 | 2013-07-10 15:05:40 +0100 | [diff] [blame] | 1859 | if (list_empty(&message->transfers)) |
| 1860 | return -EINVAL; |
Mark Brown | 24a0013 | 2013-07-10 15:05:40 +0100 | [diff] [blame] | 1861 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1862 | /* Half-duplex links include original MicroWire, and ones with |
| 1863 | * only one data pin like SPI_3WIRE (switches direction) or where |
| 1864 | * either MOSI or MISO is missing. They can also be caused by |
| 1865 | * software limitations. |
| 1866 | */ |
| 1867 | if ((master->flags & SPI_MASTER_HALF_DUPLEX) |
| 1868 | || (spi->mode & SPI_3WIRE)) { |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1869 | unsigned flags = master->flags; |
| 1870 | |
| 1871 | list_for_each_entry(xfer, &message->transfers, transfer_list) { |
| 1872 | if (xfer->rx_buf && xfer->tx_buf) |
| 1873 | return -EINVAL; |
| 1874 | if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf) |
| 1875 | return -EINVAL; |
| 1876 | if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf) |
| 1877 | return -EINVAL; |
| 1878 | } |
| 1879 | } |
| 1880 | |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 1881 | /** |
Laxman Dewangan | 059b8ff | 2013-01-05 00:17:14 +0530 | [diff] [blame] | 1882 | * Set transfer bits_per_word and max speed as spi device default if |
| 1883 | * it is not set for this transfer. |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 1884 | * Set transfer tx_nbits and rx_nbits as single transfer default |
| 1885 | * (SPI_NBITS_SINGLE) if it is not set for this transfer. |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 1886 | */ |
| 1887 | list_for_each_entry(xfer, &message->transfers, transfer_list) { |
Sourav Poddar | 078726c | 2013-07-18 15:31:25 +0530 | [diff] [blame] | 1888 | message->frame_length += xfer->len; |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 1889 | if (!xfer->bits_per_word) |
| 1890 | xfer->bits_per_word = spi->bits_per_word; |
Axel Lin | a6f87fa | 2014-03-17 10:08:12 +0800 | [diff] [blame] | 1891 | |
| 1892 | if (!xfer->speed_hz) |
Laxman Dewangan | 059b8ff | 2013-01-05 00:17:14 +0530 | [diff] [blame] | 1893 | xfer->speed_hz = spi->max_speed_hz; |
Axel Lin | a6f87fa | 2014-03-17 10:08:12 +0800 | [diff] [blame] | 1894 | |
| 1895 | if (master->max_speed_hz && |
| 1896 | xfer->speed_hz > master->max_speed_hz) |
| 1897 | xfer->speed_hz = master->max_speed_hz; |
Gabor Juhos | 56ede94 | 2013-08-14 10:25:28 +0200 | [diff] [blame] | 1898 | |
Stephen Warren | 543bb25 | 2013-03-26 20:37:57 -0600 | [diff] [blame] | 1899 | if (master->bits_per_word_mask) { |
| 1900 | /* Only 32 bits fit in the mask */ |
| 1901 | if (xfer->bits_per_word > 32) |
| 1902 | return -EINVAL; |
| 1903 | if (!(master->bits_per_word_mask & |
| 1904 | BIT(xfer->bits_per_word - 1))) |
| 1905 | return -EINVAL; |
| 1906 | } |
Mark Brown | a2fd4f9 | 2013-07-10 14:57:26 +0100 | [diff] [blame] | 1907 | |
Ivan T. Ivanov | 4d94bd2 | 2014-02-20 12:02:08 +0200 | [diff] [blame] | 1908 | /* |
| 1909 | * SPI transfer length should be multiple of SPI word size |
| 1910 | * where SPI word size should be power-of-two multiple |
| 1911 | */ |
| 1912 | if (xfer->bits_per_word <= 8) |
| 1913 | w_size = 1; |
| 1914 | else if (xfer->bits_per_word <= 16) |
| 1915 | w_size = 2; |
| 1916 | else |
| 1917 | w_size = 4; |
| 1918 | |
Ivan T. Ivanov | 4d94bd2 | 2014-02-20 12:02:08 +0200 | [diff] [blame] | 1919 | /* No partial transfers accepted */ |
Atsushi Nemoto | 6ea3129 | 2014-02-28 23:03:16 +0900 | [diff] [blame] | 1920 | if (xfer->len % w_size) |
Ivan T. Ivanov | 4d94bd2 | 2014-02-20 12:02:08 +0200 | [diff] [blame] | 1921 | return -EINVAL; |
| 1922 | |
Mark Brown | a2fd4f9 | 2013-07-10 14:57:26 +0100 | [diff] [blame] | 1923 | if (xfer->speed_hz && master->min_speed_hz && |
| 1924 | xfer->speed_hz < master->min_speed_hz) |
| 1925 | return -EINVAL; |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 1926 | |
| 1927 | if (xfer->tx_buf && !xfer->tx_nbits) |
| 1928 | xfer->tx_nbits = SPI_NBITS_SINGLE; |
| 1929 | if (xfer->rx_buf && !xfer->rx_nbits) |
| 1930 | xfer->rx_nbits = SPI_NBITS_SINGLE; |
| 1931 | /* check transfer tx/rx_nbits: |
Geert Uytterhoeven | 1afd998 | 2014-01-12 14:00:29 +0100 | [diff] [blame] | 1932 | * 1. check the value matches one of single, dual and quad |
| 1933 | * 2. check tx/rx_nbits match the mode in spi_device |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 1934 | */ |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 1935 | if (xfer->tx_buf) { |
| 1936 | if (xfer->tx_nbits != SPI_NBITS_SINGLE && |
| 1937 | xfer->tx_nbits != SPI_NBITS_DUAL && |
| 1938 | xfer->tx_nbits != SPI_NBITS_QUAD) |
| 1939 | return -EINVAL; |
| 1940 | if ((xfer->tx_nbits == SPI_NBITS_DUAL) && |
| 1941 | !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) |
| 1942 | return -EINVAL; |
| 1943 | if ((xfer->tx_nbits == SPI_NBITS_QUAD) && |
| 1944 | !(spi->mode & SPI_TX_QUAD)) |
| 1945 | return -EINVAL; |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 1946 | } |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 1947 | /* check transfer rx_nbits */ |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 1948 | if (xfer->rx_buf) { |
| 1949 | if (xfer->rx_nbits != SPI_NBITS_SINGLE && |
| 1950 | xfer->rx_nbits != SPI_NBITS_DUAL && |
| 1951 | xfer->rx_nbits != SPI_NBITS_QUAD) |
| 1952 | return -EINVAL; |
| 1953 | if ((xfer->rx_nbits == SPI_NBITS_DUAL) && |
| 1954 | !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) |
| 1955 | return -EINVAL; |
| 1956 | if ((xfer->rx_nbits == SPI_NBITS_QUAD) && |
| 1957 | !(spi->mode & SPI_RX_QUAD)) |
| 1958 | return -EINVAL; |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 1959 | } |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 1960 | } |
| 1961 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1962 | message->status = -EINPROGRESS; |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 1963 | |
| 1964 | return 0; |
| 1965 | } |
| 1966 | |
| 1967 | static int __spi_async(struct spi_device *spi, struct spi_message *message) |
| 1968 | { |
| 1969 | struct spi_master *master = spi->master; |
| 1970 | |
| 1971 | message->spi = spi; |
| 1972 | |
| 1973 | trace_spi_message_submit(message); |
| 1974 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1975 | return master->transfer(spi, message); |
| 1976 | } |
| 1977 | |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 1978 | /** |
| 1979 | * spi_async - asynchronous SPI transfer |
| 1980 | * @spi: device with which data will be exchanged |
| 1981 | * @message: describes the data transfers, including completion callback |
| 1982 | * Context: any (irqs may be blocked, etc) |
| 1983 | * |
| 1984 | * This call may be used in_irq and other contexts which can't sleep, |
| 1985 | * as well as from task contexts which can sleep. |
| 1986 | * |
| 1987 | * The completion callback is invoked in a context which can't sleep. |
| 1988 | * Before that invocation, the value of message->status is undefined. |
| 1989 | * When the callback is issued, message->status holds either zero (to |
| 1990 | * indicate complete success) or a negative error code. After that |
| 1991 | * callback returns, the driver which issued the transfer request may |
| 1992 | * deallocate the associated memory; it's no longer in use by any SPI |
| 1993 | * core or controller driver code. |
| 1994 | * |
| 1995 | * Note that although all messages to a spi_device are handled in |
| 1996 | * FIFO order, messages may go to different devices in other orders. |
| 1997 | * Some device might be higher priority, or have various "hard" access |
| 1998 | * time requirements, for example. |
| 1999 | * |
| 2000 | * On detection of any fault during the transfer, processing of |
| 2001 | * the entire message is aborted, and the device is deselected. |
| 2002 | * Until returning from the associated message completion callback, |
| 2003 | * no other spi_message queued to that device will be processed. |
| 2004 | * (This rule applies equally to all the synchronous transfer calls, |
| 2005 | * which are wrappers around this core asynchronous primitive.) |
| 2006 | */ |
| 2007 | int spi_async(struct spi_device *spi, struct spi_message *message) |
| 2008 | { |
| 2009 | struct spi_master *master = spi->master; |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2010 | int ret; |
| 2011 | unsigned long flags; |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 2012 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 2013 | ret = __spi_validate(spi, message); |
| 2014 | if (ret != 0) |
| 2015 | return ret; |
| 2016 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2017 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 2018 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2019 | if (master->bus_lock_flag) |
| 2020 | ret = -EBUSY; |
| 2021 | else |
| 2022 | ret = __spi_async(spi, message); |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 2023 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2024 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
| 2025 | |
| 2026 | return ret; |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 2027 | } |
| 2028 | EXPORT_SYMBOL_GPL(spi_async); |
| 2029 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2030 | /** |
| 2031 | * spi_async_locked - version of spi_async with exclusive bus usage |
| 2032 | * @spi: device with which data will be exchanged |
| 2033 | * @message: describes the data transfers, including completion callback |
| 2034 | * Context: any (irqs may be blocked, etc) |
| 2035 | * |
| 2036 | * This call may be used in_irq and other contexts which can't sleep, |
| 2037 | * as well as from task contexts which can sleep. |
| 2038 | * |
| 2039 | * The completion callback is invoked in a context which can't sleep. |
| 2040 | * Before that invocation, the value of message->status is undefined. |
| 2041 | * When the callback is issued, message->status holds either zero (to |
| 2042 | * indicate complete success) or a negative error code. After that |
| 2043 | * callback returns, the driver which issued the transfer request may |
| 2044 | * deallocate the associated memory; it's no longer in use by any SPI |
| 2045 | * core or controller driver code. |
| 2046 | * |
| 2047 | * Note that although all messages to a spi_device are handled in |
| 2048 | * FIFO order, messages may go to different devices in other orders. |
| 2049 | * Some device might be higher priority, or have various "hard" access |
| 2050 | * time requirements, for example. |
| 2051 | * |
| 2052 | * On detection of any fault during the transfer, processing of |
| 2053 | * the entire message is aborted, and the device is deselected. |
| 2054 | * Until returning from the associated message completion callback, |
| 2055 | * no other spi_message queued to that device will be processed. |
| 2056 | * (This rule applies equally to all the synchronous transfer calls, |
| 2057 | * which are wrappers around this core asynchronous primitive.) |
| 2058 | */ |
| 2059 | int spi_async_locked(struct spi_device *spi, struct spi_message *message) |
| 2060 | { |
| 2061 | struct spi_master *master = spi->master; |
| 2062 | int ret; |
| 2063 | unsigned long flags; |
| 2064 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 2065 | ret = __spi_validate(spi, message); |
| 2066 | if (ret != 0) |
| 2067 | return ret; |
| 2068 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2069 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
| 2070 | |
| 2071 | ret = __spi_async(spi, message); |
| 2072 | |
| 2073 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
| 2074 | |
| 2075 | return ret; |
| 2076 | |
| 2077 | } |
| 2078 | EXPORT_SYMBOL_GPL(spi_async_locked); |
| 2079 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 2080 | |
| 2081 | /*-------------------------------------------------------------------------*/ |
| 2082 | |
| 2083 | /* Utility methods for SPI master protocol drivers, layered on |
| 2084 | * top of the core. Some other utility methods are defined as |
| 2085 | * inline functions. |
| 2086 | */ |
| 2087 | |
Andrew Morton | 5d870c8 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 2088 | static void spi_complete(void *arg) |
| 2089 | { |
| 2090 | complete(arg); |
| 2091 | } |
| 2092 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2093 | static int __spi_sync(struct spi_device *spi, struct spi_message *message, |
| 2094 | int bus_locked) |
| 2095 | { |
| 2096 | DECLARE_COMPLETION_ONSTACK(done); |
| 2097 | int status; |
| 2098 | struct spi_master *master = spi->master; |
| 2099 | |
| 2100 | message->complete = spi_complete; |
| 2101 | message->context = &done; |
| 2102 | |
| 2103 | if (!bus_locked) |
| 2104 | mutex_lock(&master->bus_lock_mutex); |
| 2105 | |
| 2106 | status = spi_async_locked(spi, message); |
| 2107 | |
| 2108 | if (!bus_locked) |
| 2109 | mutex_unlock(&master->bus_lock_mutex); |
| 2110 | |
| 2111 | if (status == 0) { |
| 2112 | wait_for_completion(&done); |
| 2113 | status = message->status; |
| 2114 | } |
| 2115 | message->context = NULL; |
| 2116 | return status; |
| 2117 | } |
| 2118 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2119 | /** |
| 2120 | * spi_sync - blocking/synchronous SPI data transfers |
| 2121 | * @spi: device with which data will be exchanged |
| 2122 | * @message: describes the data transfers |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2123 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2124 | * |
| 2125 | * This call may only be used from a context that may sleep. The sleep |
| 2126 | * is non-interruptible, and has no timeout. Low-overhead controller |
| 2127 | * drivers may DMA directly into and out of the message buffers. |
| 2128 | * |
| 2129 | * Note that the SPI device's chip select is active during the message, |
| 2130 | * and then is normally disabled between messages. Drivers for some |
| 2131 | * frequently-used devices may want to minimize costs of selecting a chip, |
| 2132 | * by leaving it selected in anticipation that the next message will go |
| 2133 | * to the same chip. (That may increase power usage.) |
| 2134 | * |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 2135 | * Also, the caller is guaranteeing that the memory associated with the |
| 2136 | * message will not be freed before this call returns. |
| 2137 | * |
Marc Pignat | 9b938b7 | 2007-12-04 23:45:10 -0800 | [diff] [blame] | 2138 | * It returns zero on success, else a negative error code. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2139 | */ |
| 2140 | int spi_sync(struct spi_device *spi, struct spi_message *message) |
| 2141 | { |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2142 | return __spi_sync(spi, message, 0); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2143 | } |
| 2144 | EXPORT_SYMBOL_GPL(spi_sync); |
| 2145 | |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2146 | /** |
| 2147 | * spi_sync_locked - version of spi_sync with exclusive bus usage |
| 2148 | * @spi: device with which data will be exchanged |
| 2149 | * @message: describes the data transfers |
| 2150 | * Context: can sleep |
| 2151 | * |
| 2152 | * This call may only be used from a context that may sleep. The sleep |
| 2153 | * is non-interruptible, and has no timeout. Low-overhead controller |
| 2154 | * drivers may DMA directly into and out of the message buffers. |
| 2155 | * |
| 2156 | * This call should be used by drivers that require exclusive access to the |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2157 | * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must |
Ernst Schwab | cf32b71 | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2158 | * be released by a spi_bus_unlock call when the exclusive access is over. |
| 2159 | * |
| 2160 | * It returns zero on success, else a negative error code. |
| 2161 | */ |
| 2162 | int spi_sync_locked(struct spi_device *spi, struct spi_message *message) |
| 2163 | { |
| 2164 | return __spi_sync(spi, message, 1); |
| 2165 | } |
| 2166 | EXPORT_SYMBOL_GPL(spi_sync_locked); |
| 2167 | |
| 2168 | /** |
| 2169 | * spi_bus_lock - obtain a lock for exclusive SPI bus usage |
| 2170 | * @master: SPI bus master that should be locked for exclusive bus access |
| 2171 | * Context: can sleep |
| 2172 | * |
| 2173 | * This call may only be used from a context that may sleep. The sleep |
| 2174 | * is non-interruptible, and has no timeout. |
| 2175 | * |
| 2176 | * This call should be used by drivers that require exclusive access to the |
| 2177 | * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the |
| 2178 | * exclusive access is over. Data transfer must be done by spi_sync_locked |
| 2179 | * and spi_async_locked calls when the SPI bus lock is held. |
| 2180 | * |
| 2181 | * It returns zero on success, else a negative error code. |
| 2182 | */ |
| 2183 | int spi_bus_lock(struct spi_master *master) |
| 2184 | { |
| 2185 | unsigned long flags; |
| 2186 | |
| 2187 | mutex_lock(&master->bus_lock_mutex); |
| 2188 | |
| 2189 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
| 2190 | master->bus_lock_flag = 1; |
| 2191 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
| 2192 | |
| 2193 | /* mutex remains locked until spi_bus_unlock is called */ |
| 2194 | |
| 2195 | return 0; |
| 2196 | } |
| 2197 | EXPORT_SYMBOL_GPL(spi_bus_lock); |
| 2198 | |
| 2199 | /** |
| 2200 | * spi_bus_unlock - release the lock for exclusive SPI bus usage |
| 2201 | * @master: SPI bus master that was locked for exclusive bus access |
| 2202 | * Context: can sleep |
| 2203 | * |
| 2204 | * This call may only be used from a context that may sleep. The sleep |
| 2205 | * is non-interruptible, and has no timeout. |
| 2206 | * |
| 2207 | * This call releases an SPI bus lock previously obtained by an spi_bus_lock |
| 2208 | * call. |
| 2209 | * |
| 2210 | * It returns zero on success, else a negative error code. |
| 2211 | */ |
| 2212 | int spi_bus_unlock(struct spi_master *master) |
| 2213 | { |
| 2214 | master->bus_lock_flag = 0; |
| 2215 | |
| 2216 | mutex_unlock(&master->bus_lock_mutex); |
| 2217 | |
| 2218 | return 0; |
| 2219 | } |
| 2220 | EXPORT_SYMBOL_GPL(spi_bus_unlock); |
| 2221 | |
David Brownell | a9948b6 | 2006-04-02 10:37:40 -0800 | [diff] [blame] | 2222 | /* portable code must never pass more than 32 bytes */ |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 2223 | #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2224 | |
| 2225 | static u8 *buf; |
| 2226 | |
| 2227 | /** |
| 2228 | * spi_write_then_read - SPI synchronous write followed by read |
| 2229 | * @spi: device with which data will be exchanged |
| 2230 | * @txbuf: data to be written (need not be dma-safe) |
| 2231 | * @n_tx: size of txbuf, in bytes |
Jiri Pirko | 2757049 | 2009-06-17 16:26:06 -0700 | [diff] [blame] | 2232 | * @rxbuf: buffer into which data will be read (need not be dma-safe) |
| 2233 | * @n_rx: size of rxbuf, in bytes |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2234 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2235 | * |
| 2236 | * This performs a half duplex MicroWire style transaction with the |
| 2237 | * device, sending txbuf and then reading rxbuf. The return value |
| 2238 | * is zero for success, else a negative errno status code. |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 2239 | * This call may only be used from a context that may sleep. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2240 | * |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 2241 | * Parameters to this routine are always copied using a small buffer; |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2242 | * portable code should never use this for more than 32 bytes. |
| 2243 | * Performance-sensitive or bulk transfer code should instead use |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 2244 | * spi_{async,sync}() calls with dma-safe buffers. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2245 | */ |
| 2246 | int spi_write_then_read(struct spi_device *spi, |
Mark Brown | 0c4a159 | 2011-05-11 00:09:30 +0200 | [diff] [blame] | 2247 | const void *txbuf, unsigned n_tx, |
| 2248 | void *rxbuf, unsigned n_rx) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2249 | { |
David Brownell | 068f407 | 2007-12-04 23:45:09 -0800 | [diff] [blame] | 2250 | static DEFINE_MUTEX(lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2251 | |
| 2252 | int status; |
| 2253 | struct spi_message message; |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 2254 | struct spi_transfer x[2]; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2255 | u8 *local_buf; |
| 2256 | |
Mark Brown | b3a223e | 2012-12-02 12:54:25 +0900 | [diff] [blame] | 2257 | /* Use preallocated DMA-safe buffer if we can. We can't avoid |
| 2258 | * copying here, (as a pure convenience thing), but we can |
| 2259 | * keep heap costs out of the hot path unless someone else is |
| 2260 | * using the pre-allocated buffer or the transfer is too large. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2261 | */ |
Mark Brown | b3a223e | 2012-12-02 12:54:25 +0900 | [diff] [blame] | 2262 | if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { |
Mark Brown | 2cd94c8 | 2013-01-27 14:35:04 +0800 | [diff] [blame] | 2263 | local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), |
| 2264 | GFP_KERNEL | GFP_DMA); |
Mark Brown | b3a223e | 2012-12-02 12:54:25 +0900 | [diff] [blame] | 2265 | if (!local_buf) |
| 2266 | return -ENOMEM; |
| 2267 | } else { |
| 2268 | local_buf = buf; |
| 2269 | } |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2270 | |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 2271 | spi_message_init(&message); |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 2272 | memset(x, 0, sizeof(x)); |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 2273 | if (n_tx) { |
| 2274 | x[0].len = n_tx; |
| 2275 | spi_message_add_tail(&x[0], &message); |
| 2276 | } |
| 2277 | if (n_rx) { |
| 2278 | x[1].len = n_rx; |
| 2279 | spi_message_add_tail(&x[1], &message); |
| 2280 | } |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 2281 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2282 | memcpy(local_buf, txbuf, n_tx); |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 2283 | x[0].tx_buf = local_buf; |
| 2284 | x[1].rx_buf = local_buf + n_tx; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2285 | |
| 2286 | /* do the i/o */ |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2287 | status = spi_sync(spi, &message); |
Marc Pignat | 9b938b7 | 2007-12-04 23:45:10 -0800 | [diff] [blame] | 2288 | if (status == 0) |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 2289 | memcpy(rxbuf, x[1].rx_buf, n_rx); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2290 | |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 2291 | if (x[0].tx_buf == buf) |
David Brownell | 068f407 | 2007-12-04 23:45:09 -0800 | [diff] [blame] | 2292 | mutex_unlock(&lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2293 | else |
| 2294 | kfree(local_buf); |
| 2295 | |
| 2296 | return status; |
| 2297 | } |
| 2298 | EXPORT_SYMBOL_GPL(spi_write_then_read); |
| 2299 | |
| 2300 | /*-------------------------------------------------------------------------*/ |
| 2301 | |
| 2302 | static int __init spi_init(void) |
| 2303 | { |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 2304 | int status; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2305 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 2306 | buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 2307 | if (!buf) { |
| 2308 | status = -ENOMEM; |
| 2309 | goto err0; |
| 2310 | } |
| 2311 | |
| 2312 | status = bus_register(&spi_bus_type); |
| 2313 | if (status < 0) |
| 2314 | goto err1; |
| 2315 | |
| 2316 | status = class_register(&spi_master_class); |
| 2317 | if (status < 0) |
| 2318 | goto err2; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2319 | return 0; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 2320 | |
| 2321 | err2: |
| 2322 | bus_unregister(&spi_bus_type); |
| 2323 | err1: |
| 2324 | kfree(buf); |
| 2325 | buf = NULL; |
| 2326 | err0: |
| 2327 | return status; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2328 | } |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 2329 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2330 | /* board_info is normally registered in arch_initcall(), |
| 2331 | * but even essential drivers wait till later |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 2332 | * |
| 2333 | * REVISIT only boardinfo really needs static linking. the rest (device and |
| 2334 | * driver registration) _could_ be dynamically linked (modular) ... costs |
| 2335 | * include needing to have boardinfo data structures be much more public. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2336 | */ |
David Brownell | 673c0c0 | 2008-10-15 22:02:46 -0700 | [diff] [blame] | 2337 | postcore_initcall(spi_init); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2338 | |