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