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> |
Ingo Molnar | ae7e81c | 2017-02-01 18:07:51 +0100 | [diff] [blame] | 36 | #include <uapi/linux/sched/types.h> |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 37 | #include <linux/delay.h> |
| 38 | #include <linux/kthread.h> |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 39 | #include <linux/ioport.h> |
| 40 | #include <linux/acpi.h> |
Vignesh R | b1b8153 | 2016-08-17 15:22:36 +0530 | [diff] [blame] | 41 | #include <linux/highmem.h> |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 42 | |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 43 | #define CREATE_TRACE_POINTS |
| 44 | #include <trace/events/spi.h> |
| 45 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 46 | static void spidev_release(struct device *dev) |
| 47 | { |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 48 | struct spi_device *spi = to_spi_device(dev); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 49 | |
| 50 | /* spi masters may cleanup for released devices */ |
| 51 | if (spi->master->cleanup) |
| 52 | spi->master->cleanup(spi); |
| 53 | |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 54 | spi_master_put(spi->master); |
Roman Tereshonkov | 07a389f | 2010-04-12 09:56:35 +0000 | [diff] [blame] | 55 | kfree(spi); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static ssize_t |
| 59 | modalias_show(struct device *dev, struct device_attribute *a, char *buf) |
| 60 | { |
| 61 | const struct spi_device *spi = to_spi_device(dev); |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 62 | int len; |
| 63 | |
| 64 | len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); |
| 65 | if (len != -ENODEV) |
| 66 | return len; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 67 | |
Grant Likely | d8e328b | 2012-05-20 00:08:13 -0600 | [diff] [blame] | 68 | return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 69 | } |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 70 | static DEVICE_ATTR_RO(modalias); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 71 | |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 72 | #define SPI_STATISTICS_ATTRS(field, file) \ |
| 73 | static ssize_t spi_master_##field##_show(struct device *dev, \ |
| 74 | struct device_attribute *attr, \ |
| 75 | char *buf) \ |
| 76 | { \ |
| 77 | struct spi_master *master = container_of(dev, \ |
| 78 | struct spi_master, dev); \ |
| 79 | return spi_statistics_##field##_show(&master->statistics, buf); \ |
| 80 | } \ |
| 81 | static struct device_attribute dev_attr_spi_master_##field = { \ |
| 82 | .attr = { .name = file, .mode = S_IRUGO }, \ |
| 83 | .show = spi_master_##field##_show, \ |
| 84 | }; \ |
| 85 | static ssize_t spi_device_##field##_show(struct device *dev, \ |
| 86 | struct device_attribute *attr, \ |
| 87 | char *buf) \ |
| 88 | { \ |
Geliang Tang | d1eba93 | 2015-12-23 00:18:41 +0800 | [diff] [blame] | 89 | struct spi_device *spi = to_spi_device(dev); \ |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 90 | return spi_statistics_##field##_show(&spi->statistics, buf); \ |
| 91 | } \ |
| 92 | static struct device_attribute dev_attr_spi_device_##field = { \ |
| 93 | .attr = { .name = file, .mode = S_IRUGO }, \ |
| 94 | .show = spi_device_##field##_show, \ |
| 95 | } |
| 96 | |
| 97 | #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \ |
| 98 | static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ |
| 99 | char *buf) \ |
| 100 | { \ |
| 101 | unsigned long flags; \ |
| 102 | ssize_t len; \ |
| 103 | spin_lock_irqsave(&stat->lock, flags); \ |
| 104 | len = sprintf(buf, format_string, stat->field); \ |
| 105 | spin_unlock_irqrestore(&stat->lock, flags); \ |
| 106 | return len; \ |
| 107 | } \ |
| 108 | SPI_STATISTICS_ATTRS(name, file) |
| 109 | |
| 110 | #define SPI_STATISTICS_SHOW(field, format_string) \ |
| 111 | SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ |
| 112 | field, format_string) |
| 113 | |
| 114 | SPI_STATISTICS_SHOW(messages, "%lu"); |
| 115 | SPI_STATISTICS_SHOW(transfers, "%lu"); |
| 116 | SPI_STATISTICS_SHOW(errors, "%lu"); |
| 117 | SPI_STATISTICS_SHOW(timedout, "%lu"); |
| 118 | |
| 119 | SPI_STATISTICS_SHOW(spi_sync, "%lu"); |
| 120 | SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu"); |
| 121 | SPI_STATISTICS_SHOW(spi_async, "%lu"); |
| 122 | |
| 123 | SPI_STATISTICS_SHOW(bytes, "%llu"); |
| 124 | SPI_STATISTICS_SHOW(bytes_rx, "%llu"); |
| 125 | SPI_STATISTICS_SHOW(bytes_tx, "%llu"); |
| 126 | |
Martin Sperl | 6b7bc06 | 2015-06-22 13:02:04 +0000 | [diff] [blame] | 127 | #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ |
| 128 | SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ |
| 129 | "transfer_bytes_histo_" number, \ |
| 130 | transfer_bytes_histo[index], "%lu") |
| 131 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); |
| 132 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); |
| 133 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); |
| 134 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); |
| 135 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); |
| 136 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); |
| 137 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); |
| 138 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); |
| 139 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); |
| 140 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); |
| 141 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); |
| 142 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); |
| 143 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); |
| 144 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); |
| 145 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); |
| 146 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); |
| 147 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); |
| 148 | |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 149 | SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu"); |
| 150 | |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 151 | static struct attribute *spi_dev_attrs[] = { |
| 152 | &dev_attr_modalias.attr, |
| 153 | NULL, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 154 | }; |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 155 | |
| 156 | static const struct attribute_group spi_dev_group = { |
| 157 | .attrs = spi_dev_attrs, |
| 158 | }; |
| 159 | |
| 160 | static struct attribute *spi_device_statistics_attrs[] = { |
| 161 | &dev_attr_spi_device_messages.attr, |
| 162 | &dev_attr_spi_device_transfers.attr, |
| 163 | &dev_attr_spi_device_errors.attr, |
| 164 | &dev_attr_spi_device_timedout.attr, |
| 165 | &dev_attr_spi_device_spi_sync.attr, |
| 166 | &dev_attr_spi_device_spi_sync_immediate.attr, |
| 167 | &dev_attr_spi_device_spi_async.attr, |
| 168 | &dev_attr_spi_device_bytes.attr, |
| 169 | &dev_attr_spi_device_bytes_rx.attr, |
| 170 | &dev_attr_spi_device_bytes_tx.attr, |
Martin Sperl | 6b7bc06 | 2015-06-22 13:02:04 +0000 | [diff] [blame] | 171 | &dev_attr_spi_device_transfer_bytes_histo0.attr, |
| 172 | &dev_attr_spi_device_transfer_bytes_histo1.attr, |
| 173 | &dev_attr_spi_device_transfer_bytes_histo2.attr, |
| 174 | &dev_attr_spi_device_transfer_bytes_histo3.attr, |
| 175 | &dev_attr_spi_device_transfer_bytes_histo4.attr, |
| 176 | &dev_attr_spi_device_transfer_bytes_histo5.attr, |
| 177 | &dev_attr_spi_device_transfer_bytes_histo6.attr, |
| 178 | &dev_attr_spi_device_transfer_bytes_histo7.attr, |
| 179 | &dev_attr_spi_device_transfer_bytes_histo8.attr, |
| 180 | &dev_attr_spi_device_transfer_bytes_histo9.attr, |
| 181 | &dev_attr_spi_device_transfer_bytes_histo10.attr, |
| 182 | &dev_attr_spi_device_transfer_bytes_histo11.attr, |
| 183 | &dev_attr_spi_device_transfer_bytes_histo12.attr, |
| 184 | &dev_attr_spi_device_transfer_bytes_histo13.attr, |
| 185 | &dev_attr_spi_device_transfer_bytes_histo14.attr, |
| 186 | &dev_attr_spi_device_transfer_bytes_histo15.attr, |
| 187 | &dev_attr_spi_device_transfer_bytes_histo16.attr, |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 188 | &dev_attr_spi_device_transfers_split_maxsize.attr, |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 189 | NULL, |
| 190 | }; |
| 191 | |
| 192 | static const struct attribute_group spi_device_statistics_group = { |
| 193 | .name = "statistics", |
| 194 | .attrs = spi_device_statistics_attrs, |
| 195 | }; |
| 196 | |
| 197 | static const struct attribute_group *spi_dev_groups[] = { |
| 198 | &spi_dev_group, |
| 199 | &spi_device_statistics_group, |
| 200 | NULL, |
| 201 | }; |
| 202 | |
| 203 | static struct attribute *spi_master_statistics_attrs[] = { |
| 204 | &dev_attr_spi_master_messages.attr, |
| 205 | &dev_attr_spi_master_transfers.attr, |
| 206 | &dev_attr_spi_master_errors.attr, |
| 207 | &dev_attr_spi_master_timedout.attr, |
| 208 | &dev_attr_spi_master_spi_sync.attr, |
| 209 | &dev_attr_spi_master_spi_sync_immediate.attr, |
| 210 | &dev_attr_spi_master_spi_async.attr, |
| 211 | &dev_attr_spi_master_bytes.attr, |
| 212 | &dev_attr_spi_master_bytes_rx.attr, |
| 213 | &dev_attr_spi_master_bytes_tx.attr, |
Martin Sperl | 6b7bc06 | 2015-06-22 13:02:04 +0000 | [diff] [blame] | 214 | &dev_attr_spi_master_transfer_bytes_histo0.attr, |
| 215 | &dev_attr_spi_master_transfer_bytes_histo1.attr, |
| 216 | &dev_attr_spi_master_transfer_bytes_histo2.attr, |
| 217 | &dev_attr_spi_master_transfer_bytes_histo3.attr, |
| 218 | &dev_attr_spi_master_transfer_bytes_histo4.attr, |
| 219 | &dev_attr_spi_master_transfer_bytes_histo5.attr, |
| 220 | &dev_attr_spi_master_transfer_bytes_histo6.attr, |
| 221 | &dev_attr_spi_master_transfer_bytes_histo7.attr, |
| 222 | &dev_attr_spi_master_transfer_bytes_histo8.attr, |
| 223 | &dev_attr_spi_master_transfer_bytes_histo9.attr, |
| 224 | &dev_attr_spi_master_transfer_bytes_histo10.attr, |
| 225 | &dev_attr_spi_master_transfer_bytes_histo11.attr, |
| 226 | &dev_attr_spi_master_transfer_bytes_histo12.attr, |
| 227 | &dev_attr_spi_master_transfer_bytes_histo13.attr, |
| 228 | &dev_attr_spi_master_transfer_bytes_histo14.attr, |
| 229 | &dev_attr_spi_master_transfer_bytes_histo15.attr, |
| 230 | &dev_attr_spi_master_transfer_bytes_histo16.attr, |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 231 | &dev_attr_spi_master_transfers_split_maxsize.attr, |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 232 | NULL, |
| 233 | }; |
| 234 | |
| 235 | static const struct attribute_group spi_master_statistics_group = { |
| 236 | .name = "statistics", |
| 237 | .attrs = spi_master_statistics_attrs, |
| 238 | }; |
| 239 | |
| 240 | static const struct attribute_group *spi_master_groups[] = { |
| 241 | &spi_master_statistics_group, |
| 242 | NULL, |
| 243 | }; |
| 244 | |
| 245 | void spi_statistics_add_transfer_stats(struct spi_statistics *stats, |
| 246 | struct spi_transfer *xfer, |
| 247 | struct spi_master *master) |
| 248 | { |
| 249 | unsigned long flags; |
Martin Sperl | 6b7bc06 | 2015-06-22 13:02:04 +0000 | [diff] [blame] | 250 | int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; |
| 251 | |
| 252 | if (l2len < 0) |
| 253 | l2len = 0; |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 254 | |
| 255 | spin_lock_irqsave(&stats->lock, flags); |
| 256 | |
| 257 | stats->transfers++; |
Martin Sperl | 6b7bc06 | 2015-06-22 13:02:04 +0000 | [diff] [blame] | 258 | stats->transfer_bytes_histo[l2len]++; |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 259 | |
| 260 | stats->bytes += xfer->len; |
| 261 | if ((xfer->tx_buf) && |
| 262 | (xfer->tx_buf != master->dummy_tx)) |
| 263 | stats->bytes_tx += xfer->len; |
| 264 | if ((xfer->rx_buf) && |
| 265 | (xfer->rx_buf != master->dummy_rx)) |
| 266 | stats->bytes_rx += xfer->len; |
| 267 | |
| 268 | spin_unlock_irqrestore(&stats->lock, flags); |
| 269 | } |
| 270 | EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 271 | |
| 272 | /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, |
| 273 | * and the sysfs version makes coldplug work too. |
| 274 | */ |
| 275 | |
Anton Vorontsov | 75368bf | 2009-09-22 16:46:04 -0700 | [diff] [blame] | 276 | static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, |
| 277 | const struct spi_device *sdev) |
| 278 | { |
| 279 | while (id->name[0]) { |
| 280 | if (!strcmp(sdev->modalias, id->name)) |
| 281 | return id; |
| 282 | id++; |
| 283 | } |
| 284 | return NULL; |
| 285 | } |
| 286 | |
| 287 | const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) |
| 288 | { |
| 289 | const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); |
| 290 | |
| 291 | return spi_match_id(sdrv->id_table, sdev); |
| 292 | } |
| 293 | EXPORT_SYMBOL_GPL(spi_get_device_id); |
| 294 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 295 | static int spi_match_device(struct device *dev, struct device_driver *drv) |
| 296 | { |
| 297 | const struct spi_device *spi = to_spi_device(dev); |
Anton Vorontsov | 75368bf | 2009-09-22 16:46:04 -0700 | [diff] [blame] | 298 | const struct spi_driver *sdrv = to_spi_driver(drv); |
| 299 | |
Sinan Akman | 2b7a32f | 2010-10-02 21:28:29 -0600 | [diff] [blame] | 300 | /* Attempt an OF style match */ |
| 301 | if (of_driver_match_device(dev, drv)) |
| 302 | return 1; |
| 303 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 304 | /* Then try ACPI */ |
| 305 | if (acpi_driver_match_device(dev, drv)) |
| 306 | return 1; |
| 307 | |
Anton Vorontsov | 75368bf | 2009-09-22 16:46:04 -0700 | [diff] [blame] | 308 | if (sdrv->id_table) |
| 309 | return !!spi_match_id(sdrv->id_table, spi); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 310 | |
Kay Sievers | 35f74fc | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 311 | return strcmp(spi->modalias, drv->name) == 0; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 314 | static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 315 | { |
| 316 | const struct spi_device *spi = to_spi_device(dev); |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 317 | int rc; |
| 318 | |
| 319 | rc = acpi_device_uevent_modalias(dev, env); |
| 320 | if (rc != -ENODEV) |
| 321 | return rc; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 322 | |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 323 | add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 327 | struct bus_type spi_bus_type = { |
| 328 | .name = "spi", |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 329 | .dev_groups = spi_dev_groups, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 330 | .match = spi_match_device, |
| 331 | .uevent = spi_uevent, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 332 | }; |
| 333 | EXPORT_SYMBOL_GPL(spi_bus_type); |
| 334 | |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 335 | |
| 336 | static int spi_drv_probe(struct device *dev) |
| 337 | { |
| 338 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
Jon Hunter | 44af792 | 2015-10-09 15:45:55 +0100 | [diff] [blame] | 339 | struct spi_device *spi = to_spi_device(dev); |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 340 | int ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 341 | |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 342 | ret = of_clk_set_defaults(dev->of_node, false); |
| 343 | if (ret) |
| 344 | return ret; |
| 345 | |
Jon Hunter | 44af792 | 2015-10-09 15:45:55 +0100 | [diff] [blame] | 346 | if (dev->of_node) { |
| 347 | spi->irq = of_irq_get(dev->of_node, 0); |
| 348 | if (spi->irq == -EPROBE_DEFER) |
| 349 | return -EPROBE_DEFER; |
| 350 | if (spi->irq < 0) |
| 351 | spi->irq = 0; |
| 352 | } |
| 353 | |
Ulf Hansson | 676e7c2 | 2014-09-19 20:27:41 +0200 | [diff] [blame] | 354 | ret = dev_pm_domain_attach(dev, true); |
| 355 | if (ret != -EPROBE_DEFER) { |
Jon Hunter | 44af792 | 2015-10-09 15:45:55 +0100 | [diff] [blame] | 356 | ret = sdrv->probe(spi); |
Ulf Hansson | 676e7c2 | 2014-09-19 20:27:41 +0200 | [diff] [blame] | 357 | if (ret) |
| 358 | dev_pm_domain_detach(dev, true); |
| 359 | } |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 360 | |
| 361 | return ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static int spi_drv_remove(struct device *dev) |
| 365 | { |
| 366 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 367 | int ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 368 | |
Jean Delvare | aec35f4 | 2014-02-13 15:28:41 +0100 | [diff] [blame] | 369 | ret = sdrv->remove(to_spi_device(dev)); |
Ulf Hansson | 676e7c2 | 2014-09-19 20:27:41 +0200 | [diff] [blame] | 370 | dev_pm_domain_detach(dev, true); |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 371 | |
| 372 | return ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void spi_drv_shutdown(struct device *dev) |
| 376 | { |
| 377 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
| 378 | |
| 379 | sdrv->shutdown(to_spi_device(dev)); |
| 380 | } |
| 381 | |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 382 | /** |
Andrew F. Davis | ca5d248 | 2015-10-23 08:59:10 -0500 | [diff] [blame] | 383 | * __spi_register_driver - register a SPI driver |
Thierry Reding | 88c9321 | 2015-11-10 13:03:04 +0100 | [diff] [blame] | 384 | * @owner: owner module of the driver to register |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 385 | * @sdrv: the driver to register |
| 386 | * Context: can sleep |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 387 | * |
| 388 | * Return: zero on success, else a negative error code. |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 389 | */ |
Andrew F. Davis | ca5d248 | 2015-10-23 08:59:10 -0500 | [diff] [blame] | 390 | int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 391 | { |
Andrew F. Davis | ca5d248 | 2015-10-23 08:59:10 -0500 | [diff] [blame] | 392 | sdrv->driver.owner = owner; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 393 | sdrv->driver.bus = &spi_bus_type; |
| 394 | if (sdrv->probe) |
| 395 | sdrv->driver.probe = spi_drv_probe; |
| 396 | if (sdrv->remove) |
| 397 | sdrv->driver.remove = spi_drv_remove; |
| 398 | if (sdrv->shutdown) |
| 399 | sdrv->driver.shutdown = spi_drv_shutdown; |
| 400 | return driver_register(&sdrv->driver); |
| 401 | } |
Andrew F. Davis | ca5d248 | 2015-10-23 08:59:10 -0500 | [diff] [blame] | 402 | EXPORT_SYMBOL_GPL(__spi_register_driver); |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 403 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 404 | /*-------------------------------------------------------------------------*/ |
| 405 | |
| 406 | /* SPI devices should normally not be created by SPI device drivers; that |
| 407 | * would make them board-specific. Similarly with SPI master drivers. |
| 408 | * Device registration normally goes into like arch/.../mach.../board-YYY.c |
| 409 | * with other readonly (flashable) information about mainboard devices. |
| 410 | */ |
| 411 | |
| 412 | struct boardinfo { |
| 413 | struct list_head list; |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 414 | struct spi_board_info board_info; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | static LIST_HEAD(board_list); |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 418 | static LIST_HEAD(spi_master_list); |
| 419 | |
| 420 | /* |
| 421 | * Used to protect add/del opertion for board_info list and |
| 422 | * spi_master list, and their matching process |
| 423 | */ |
Matthias Kaehlcke | 9404082 | 2007-07-17 04:04:16 -0700 | [diff] [blame] | 424 | static DEFINE_MUTEX(board_lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 425 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 426 | /** |
| 427 | * spi_alloc_device - Allocate a new SPI device |
| 428 | * @master: Controller to which device is connected |
| 429 | * Context: can sleep |
| 430 | * |
| 431 | * Allows a driver to allocate and initialize a spi_device without |
| 432 | * registering it immediately. This allows a driver to directly |
| 433 | * fill the spi_device with device parameters before calling |
| 434 | * spi_add_device() on it. |
| 435 | * |
| 436 | * Caller is responsible to call spi_add_device() on the returned |
| 437 | * spi_device structure to add it to the SPI master. If the caller |
| 438 | * needs to discard the spi_device without adding it, then it should |
| 439 | * call spi_dev_put() on it. |
| 440 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 441 | * Return: a pointer to the new device, or NULL. |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 442 | */ |
| 443 | struct spi_device *spi_alloc_device(struct spi_master *master) |
| 444 | { |
| 445 | struct spi_device *spi; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 446 | |
| 447 | if (!spi_master_get(master)) |
| 448 | return NULL; |
| 449 | |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 450 | spi = kzalloc(sizeof(*spi), GFP_KERNEL); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 451 | if (!spi) { |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 452 | spi_master_put(master); |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | spi->master = master; |
Laurent Pinchart | 178db7d | 2011-12-12 01:15:06 +0100 | [diff] [blame] | 457 | spi->dev.parent = &master->dev; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 458 | spi->dev.bus = &spi_bus_type; |
| 459 | spi->dev.release = spidev_release; |
Andreas Larsson | 446411e | 2013-02-13 14:20:25 +0100 | [diff] [blame] | 460 | spi->cs_gpio = -ENOENT; |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 461 | |
| 462 | spin_lock_init(&spi->statistics.lock); |
| 463 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 464 | device_initialize(&spi->dev); |
| 465 | return spi; |
| 466 | } |
| 467 | EXPORT_SYMBOL_GPL(spi_alloc_device); |
| 468 | |
Jarkko Nikula | e13ac47 | 2013-11-14 14:03:53 +0200 | [diff] [blame] | 469 | static void spi_dev_set_name(struct spi_device *spi) |
| 470 | { |
| 471 | struct acpi_device *adev = ACPI_COMPANION(&spi->dev); |
| 472 | |
| 473 | if (adev) { |
| 474 | dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev), |
| 479 | spi->chip_select); |
| 480 | } |
| 481 | |
Mika Westerberg | b6fb8d3 | 2014-01-09 15:23:55 +0200 | [diff] [blame] | 482 | static int spi_dev_check(struct device *dev, void *data) |
| 483 | { |
| 484 | struct spi_device *spi = to_spi_device(dev); |
| 485 | struct spi_device *new_spi = data; |
| 486 | |
| 487 | if (spi->master == new_spi->master && |
| 488 | spi->chip_select == new_spi->chip_select) |
| 489 | return -EBUSY; |
| 490 | return 0; |
| 491 | } |
| 492 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 493 | /** |
| 494 | * spi_add_device - Add spi_device allocated with spi_alloc_device |
| 495 | * @spi: spi_device to register |
| 496 | * |
| 497 | * Companion function to spi_alloc_device. Devices allocated with |
| 498 | * spi_alloc_device can be added onto the spi bus with this function. |
| 499 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 500 | * Return: 0 on success; negative errno on failure |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 501 | */ |
| 502 | int spi_add_device(struct spi_device *spi) |
| 503 | { |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 504 | static DEFINE_MUTEX(spi_add_lock); |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 505 | struct spi_master *master = spi->master; |
| 506 | struct device *dev = master->dev.parent; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 507 | int status; |
| 508 | |
| 509 | /* Chipselects are numbered 0..max; validate. */ |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 510 | if (spi->chip_select >= master->num_chipselect) { |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 511 | dev_err(dev, "cs%d >= max %d\n", |
| 512 | spi->chip_select, |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 513 | master->num_chipselect); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 514 | return -EINVAL; |
| 515 | } |
| 516 | |
| 517 | /* Set the bus ID string */ |
Jarkko Nikula | e13ac47 | 2013-11-14 14:03:53 +0200 | [diff] [blame] | 518 | spi_dev_set_name(spi); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 519 | |
| 520 | /* We need to make sure there's no other device with this |
| 521 | * chipselect **BEFORE** we call setup(), else we'll trash |
| 522 | * its configuration. Lock against concurrent add() calls. |
| 523 | */ |
| 524 | mutex_lock(&spi_add_lock); |
| 525 | |
Mika Westerberg | b6fb8d3 | 2014-01-09 15:23:55 +0200 | [diff] [blame] | 526 | status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); |
| 527 | if (status) { |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 528 | dev_err(dev, "chipselect %d already in use\n", |
| 529 | spi->chip_select); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 530 | goto done; |
| 531 | } |
| 532 | |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 533 | if (master->cs_gpios) |
| 534 | spi->cs_gpio = master->cs_gpios[spi->chip_select]; |
| 535 | |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 536 | /* Drivers may modify this initial i/o setup, but will |
| 537 | * normally rely on the device being setup. Devices |
| 538 | * using SPI_CS_HIGH can't coexist well otherwise... |
| 539 | */ |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 540 | status = spi_setup(spi); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 541 | if (status < 0) { |
Linus Walleij | eb288a1 | 2010-10-21 21:06:44 +0200 | [diff] [blame] | 542 | dev_err(dev, "can't setup %s, status %d\n", |
| 543 | dev_name(&spi->dev), status); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 544 | goto done; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 545 | } |
| 546 | |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 547 | /* Device may be bound to an active driver when this returns */ |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 548 | status = device_add(&spi->dev); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 549 | if (status < 0) |
Linus Walleij | eb288a1 | 2010-10-21 21:06:44 +0200 | [diff] [blame] | 550 | dev_err(dev, "can't add %s, status %d\n", |
| 551 | dev_name(&spi->dev), status); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 552 | else |
Kay Sievers | 35f74fc | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 553 | dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 554 | |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 555 | done: |
| 556 | mutex_unlock(&spi_add_lock); |
| 557 | return status; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 558 | } |
| 559 | EXPORT_SYMBOL_GPL(spi_add_device); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 560 | |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 561 | /** |
| 562 | * spi_new_device - instantiate one new SPI device |
| 563 | * @master: Controller to which device is connected |
| 564 | * @chip: Describes the SPI device |
| 565 | * Context: can sleep |
| 566 | * |
| 567 | * On typical mainboards, this is purely internal; and it's not needed |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 568 | * after board init creates the hard-wired devices. Some development |
| 569 | * platforms may not be able to use spi_register_board_info though, and |
| 570 | * this is exported so that for example a USB or parport based adapter |
| 571 | * driver could add devices (which it would learn about out-of-band). |
David Brownell | 082c8cb | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 572 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 573 | * Return: the new device, or NULL. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 574 | */ |
Adrian Bunk | e9d5a46 | 2007-03-26 21:32:23 -0800 | [diff] [blame] | 575 | struct spi_device *spi_new_device(struct spi_master *master, |
| 576 | struct spi_board_info *chip) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 577 | { |
| 578 | struct spi_device *proxy; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 579 | int status; |
| 580 | |
David Brownell | 082c8cb | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 581 | /* NOTE: caller did any chip->bus_num checks necessary. |
| 582 | * |
| 583 | * Also, unless we change the return value convention to use |
| 584 | * error-or-pointer (not NULL-or-pointer), troubleshootability |
| 585 | * suggests syslogged diagnostics are best here (ugh). |
| 586 | */ |
| 587 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 588 | proxy = spi_alloc_device(master); |
| 589 | if (!proxy) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 590 | return NULL; |
| 591 | |
Grant Likely | 102eb97 | 2008-07-23 21:29:55 -0700 | [diff] [blame] | 592 | WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); |
| 593 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 594 | proxy->chip_select = chip->chip_select; |
| 595 | proxy->max_speed_hz = chip->max_speed_hz; |
David Brownell | 980a01c | 2006-06-28 07:47:15 -0700 | [diff] [blame] | 596 | proxy->mode = chip->mode; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 597 | proxy->irq = chip->irq; |
Grant Likely | 102eb97 | 2008-07-23 21:29:55 -0700 | [diff] [blame] | 598 | strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 599 | proxy->dev.platform_data = (void *) chip->platform_data; |
| 600 | proxy->controller_data = chip->controller_data; |
| 601 | proxy->controller_state = NULL; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 602 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 603 | status = spi_add_device(proxy); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 604 | if (status < 0) { |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 605 | spi_dev_put(proxy); |
| 606 | return NULL; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 607 | } |
| 608 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 609 | return proxy; |
| 610 | } |
| 611 | EXPORT_SYMBOL_GPL(spi_new_device); |
| 612 | |
Geert Uytterhoeven | 3b1884c | 2015-11-30 15:28:06 +0100 | [diff] [blame] | 613 | /** |
| 614 | * spi_unregister_device - unregister a single SPI device |
| 615 | * @spi: spi_device to unregister |
| 616 | * |
| 617 | * Start making the passed SPI device vanish. Normally this would be handled |
| 618 | * by spi_unregister_master(). |
| 619 | */ |
| 620 | void spi_unregister_device(struct spi_device *spi) |
| 621 | { |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 622 | if (!spi) |
| 623 | return; |
| 624 | |
Johan Hovold | 8324147 | 2017-01-30 17:47:05 +0100 | [diff] [blame] | 625 | if (spi->dev.of_node) { |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 626 | of_node_clear_flag(spi->dev.of_node, OF_POPULATED); |
Johan Hovold | 8324147 | 2017-01-30 17:47:05 +0100 | [diff] [blame] | 627 | of_node_put(spi->dev.of_node); |
| 628 | } |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 629 | if (ACPI_COMPANION(&spi->dev)) |
| 630 | acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 631 | device_unregister(&spi->dev); |
Geert Uytterhoeven | 3b1884c | 2015-11-30 15:28:06 +0100 | [diff] [blame] | 632 | } |
| 633 | EXPORT_SYMBOL_GPL(spi_unregister_device); |
| 634 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 635 | static void spi_match_master_to_boardinfo(struct spi_master *master, |
| 636 | struct spi_board_info *bi) |
| 637 | { |
| 638 | struct spi_device *dev; |
| 639 | |
| 640 | if (master->bus_num != bi->bus_num) |
| 641 | return; |
| 642 | |
| 643 | dev = spi_new_device(master, bi); |
| 644 | if (!dev) |
| 645 | dev_err(master->dev.parent, "can't create new device for %s\n", |
| 646 | bi->modalias); |
| 647 | } |
| 648 | |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 649 | /** |
| 650 | * spi_register_board_info - register SPI devices for a given board |
| 651 | * @info: array of chip descriptors |
| 652 | * @n: how many descriptors are provided |
| 653 | * Context: can sleep |
| 654 | * |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 655 | * Board-specific early init code calls this (probably during arch_initcall) |
| 656 | * with segments of the SPI device table. Any device nodes are created later, |
| 657 | * after the relevant parent SPI controller (bus_num) is defined. We keep |
| 658 | * this table of devices forever, so that reloading a controller driver will |
| 659 | * not make Linux forget about these hard-wired devices. |
| 660 | * |
| 661 | * Other code can also call this, e.g. a particular add-on board might provide |
| 662 | * SPI devices through its expansion connector, so code initializing that board |
| 663 | * would naturally declare its SPI devices. |
| 664 | * |
| 665 | * The board info passed can safely be __initdata ... but be careful of |
| 666 | * any embedded pointers (platform_data, etc), they're copied as-is. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 667 | * |
| 668 | * Return: zero on success, else a negative error code. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 669 | */ |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 670 | 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] | 671 | { |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 672 | struct boardinfo *bi; |
| 673 | int i; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 674 | |
Xiubo Li | c7908a3 | 2014-09-24 14:30:29 +0800 | [diff] [blame] | 675 | if (!n) |
| 676 | return -EINVAL; |
| 677 | |
Markus Elfring | f9bdb7f | 2017-01-13 12:28:04 +0100 | [diff] [blame] | 678 | bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 679 | if (!bi) |
| 680 | return -ENOMEM; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 681 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 682 | for (i = 0; i < n; i++, bi++, info++) { |
| 683 | struct spi_master *master; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 684 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 685 | memcpy(&bi->board_info, info, sizeof(*info)); |
| 686 | mutex_lock(&board_lock); |
| 687 | list_add_tail(&bi->list, &board_list); |
| 688 | list_for_each_entry(master, &spi_master_list, list) |
| 689 | spi_match_master_to_boardinfo(master, &bi->board_info); |
| 690 | mutex_unlock(&board_lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 691 | } |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 692 | |
| 693 | return 0; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /*-------------------------------------------------------------------------*/ |
| 697 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 698 | static void spi_set_cs(struct spi_device *spi, bool enable) |
| 699 | { |
| 700 | if (spi->mode & SPI_CS_HIGH) |
| 701 | enable = !enable; |
| 702 | |
Thor Thayer | 8eee6b9 | 2016-10-10 09:25:24 -0500 | [diff] [blame] | 703 | if (gpio_is_valid(spi->cs_gpio)) { |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 704 | gpio_set_value(spi->cs_gpio, !enable); |
Thor Thayer | 8eee6b9 | 2016-10-10 09:25:24 -0500 | [diff] [blame] | 705 | /* Some SPI masters need both GPIO CS & slave_select */ |
| 706 | if ((spi->master->flags & SPI_MASTER_GPIO_SS) && |
| 707 | spi->master->set_cs) |
| 708 | spi->master->set_cs(spi, !enable); |
| 709 | } else if (spi->master->set_cs) { |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 710 | spi->master->set_cs(spi, !enable); |
Thor Thayer | 8eee6b9 | 2016-10-10 09:25:24 -0500 | [diff] [blame] | 711 | } |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 714 | #ifdef CONFIG_HAS_DMA |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 715 | static int spi_map_buf(struct spi_master *master, struct device *dev, |
| 716 | struct sg_table *sgt, void *buf, size_t len, |
| 717 | enum dma_data_direction dir) |
| 718 | { |
| 719 | const bool vmalloced_buf = is_vmalloc_addr(buf); |
Andy Shevchenko | df88e91 | 2016-03-09 11:20:00 +0200 | [diff] [blame] | 720 | unsigned int max_seg_size = dma_get_max_seg_size(dev); |
Vignesh R | b1b8153 | 2016-08-17 15:22:36 +0530 | [diff] [blame] | 721 | #ifdef CONFIG_HIGHMEM |
| 722 | const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && |
| 723 | (unsigned long)buf < (PKMAP_BASE + |
| 724 | (LAST_PKMAP * PAGE_SIZE))); |
| 725 | #else |
| 726 | const bool kmap_buf = false; |
| 727 | #endif |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 728 | int desc_len; |
| 729 | int sgs; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 730 | struct page *vm_page; |
Juan Gutierrez | 8dd4a01 | 2016-11-21 16:50:03 -0600 | [diff] [blame] | 731 | struct scatterlist *sg; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 732 | void *sg_buf; |
| 733 | size_t min; |
| 734 | int i, ret; |
| 735 | |
Vignesh R | b1b8153 | 2016-08-17 15:22:36 +0530 | [diff] [blame] | 736 | if (vmalloced_buf || kmap_buf) { |
Andy Shevchenko | df88e91 | 2016-03-09 11:20:00 +0200 | [diff] [blame] | 737 | desc_len = min_t(int, max_seg_size, PAGE_SIZE); |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 738 | sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); |
Vignesh R | 0569a88 | 2016-04-25 15:14:00 +0530 | [diff] [blame] | 739 | } else if (virt_addr_valid(buf)) { |
Andy Shevchenko | df88e91 | 2016-03-09 11:20:00 +0200 | [diff] [blame] | 740 | desc_len = min_t(int, max_seg_size, master->max_dma_len); |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 741 | sgs = DIV_ROUND_UP(len, desc_len); |
Vignesh R | 0569a88 | 2016-04-25 15:14:00 +0530 | [diff] [blame] | 742 | } else { |
| 743 | return -EINVAL; |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 744 | } |
| 745 | |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 746 | ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); |
| 747 | if (ret != 0) |
| 748 | return ret; |
| 749 | |
Juan Gutierrez | 8dd4a01 | 2016-11-21 16:50:03 -0600 | [diff] [blame] | 750 | sg = &sgt->sgl[0]; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 751 | for (i = 0; i < sgs; i++) { |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 752 | |
Vignesh R | b1b8153 | 2016-08-17 15:22:36 +0530 | [diff] [blame] | 753 | if (vmalloced_buf || kmap_buf) { |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 754 | min = min_t(size_t, |
| 755 | len, desc_len - offset_in_page(buf)); |
Vignesh R | b1b8153 | 2016-08-17 15:22:36 +0530 | [diff] [blame] | 756 | if (vmalloced_buf) |
| 757 | vm_page = vmalloc_to_page(buf); |
| 758 | else |
| 759 | vm_page = kmap_to_page(buf); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 760 | if (!vm_page) { |
| 761 | sg_free_table(sgt); |
| 762 | return -ENOMEM; |
| 763 | } |
Juan Gutierrez | 8dd4a01 | 2016-11-21 16:50:03 -0600 | [diff] [blame] | 764 | sg_set_page(sg, vm_page, |
Charles Keepax | c1aefbd | 2014-11-17 09:14:31 +0000 | [diff] [blame] | 765 | min, offset_in_page(buf)); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 766 | } else { |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 767 | min = min_t(size_t, len, desc_len); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 768 | sg_buf = buf; |
Juan Gutierrez | 8dd4a01 | 2016-11-21 16:50:03 -0600 | [diff] [blame] | 769 | sg_set_buf(sg, sg_buf, min); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 772 | buf += min; |
| 773 | len -= min; |
Juan Gutierrez | 8dd4a01 | 2016-11-21 16:50:03 -0600 | [diff] [blame] | 774 | sg = sg_next(sg); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); |
Geert Uytterhoeven | 89e4b66 | 2014-07-10 15:29:32 +0200 | [diff] [blame] | 778 | if (!ret) |
| 779 | ret = -ENOMEM; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 780 | if (ret < 0) { |
| 781 | sg_free_table(sgt); |
| 782 | return ret; |
| 783 | } |
| 784 | |
| 785 | sgt->nents = ret; |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static void spi_unmap_buf(struct spi_master *master, struct device *dev, |
| 791 | struct sg_table *sgt, enum dma_data_direction dir) |
| 792 | { |
| 793 | if (sgt->orig_nents) { |
| 794 | dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); |
| 795 | sg_free_table(sgt); |
| 796 | } |
| 797 | } |
| 798 | |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 799 | 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] | 800 | { |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 801 | struct device *tx_dev, *rx_dev; |
| 802 | struct spi_transfer *xfer; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 803 | int ret; |
Mark Brown | 3a2eba9 | 2014-01-28 20:17:03 +0000 | [diff] [blame] | 804 | |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 805 | if (!master->can_dma) |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 806 | return 0; |
| 807 | |
Leilk Liu | c37f45b | 2015-07-23 17:10:40 +0800 | [diff] [blame] | 808 | if (master->dma_tx) |
| 809 | tx_dev = master->dma_tx->device->dev; |
| 810 | else |
Daniel Kurtz | 88b0aa5 | 2017-01-27 00:21:53 +0800 | [diff] [blame] | 811 | tx_dev = master->dev.parent; |
Leilk Liu | c37f45b | 2015-07-23 17:10:40 +0800 | [diff] [blame] | 812 | |
| 813 | if (master->dma_rx) |
| 814 | rx_dev = master->dma_rx->device->dev; |
| 815 | else |
Daniel Kurtz | 88b0aa5 | 2017-01-27 00:21:53 +0800 | [diff] [blame] | 816 | rx_dev = master->dev.parent; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 817 | |
| 818 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 819 | if (!master->can_dma(master, msg->spi, xfer)) |
| 820 | continue; |
| 821 | |
| 822 | if (xfer->tx_buf != NULL) { |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 823 | ret = spi_map_buf(master, tx_dev, &xfer->tx_sg, |
| 824 | (void *)xfer->tx_buf, xfer->len, |
| 825 | DMA_TO_DEVICE); |
| 826 | if (ret != 0) |
| 827 | return ret; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (xfer->rx_buf != NULL) { |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 831 | ret = spi_map_buf(master, rx_dev, &xfer->rx_sg, |
| 832 | xfer->rx_buf, xfer->len, |
| 833 | DMA_FROM_DEVICE); |
| 834 | if (ret != 0) { |
| 835 | spi_unmap_buf(master, tx_dev, &xfer->tx_sg, |
| 836 | DMA_TO_DEVICE); |
| 837 | return ret; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | master->cur_msg_mapped = true; |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | |
Martin Sperl | 4b78645 | 2015-05-25 10:13:10 +0000 | [diff] [blame] | 847 | 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] | 848 | { |
| 849 | struct spi_transfer *xfer; |
| 850 | struct device *tx_dev, *rx_dev; |
| 851 | |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 852 | if (!master->cur_msg_mapped || !master->can_dma) |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 853 | return 0; |
| 854 | |
Leilk Liu | c37f45b | 2015-07-23 17:10:40 +0800 | [diff] [blame] | 855 | if (master->dma_tx) |
| 856 | tx_dev = master->dma_tx->device->dev; |
| 857 | else |
Daniel Kurtz | 88b0aa5 | 2017-01-27 00:21:53 +0800 | [diff] [blame] | 858 | tx_dev = master->dev.parent; |
Leilk Liu | c37f45b | 2015-07-23 17:10:40 +0800 | [diff] [blame] | 859 | |
| 860 | if (master->dma_rx) |
| 861 | rx_dev = master->dma_rx->device->dev; |
| 862 | else |
Daniel Kurtz | 88b0aa5 | 2017-01-27 00:21:53 +0800 | [diff] [blame] | 863 | rx_dev = master->dev.parent; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 864 | |
| 865 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 866 | if (!master->can_dma(master, msg->spi, xfer)) |
| 867 | continue; |
| 868 | |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 869 | spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); |
| 870 | spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | return 0; |
| 874 | } |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 875 | #else /* !CONFIG_HAS_DMA */ |
Vignesh R | f4502dd | 2016-06-08 12:18:31 +0530 | [diff] [blame] | 876 | static inline int spi_map_buf(struct spi_master *master, |
| 877 | struct device *dev, struct sg_table *sgt, |
| 878 | void *buf, size_t len, |
| 879 | enum dma_data_direction dir) |
| 880 | { |
| 881 | return -EINVAL; |
| 882 | } |
| 883 | |
| 884 | static inline void spi_unmap_buf(struct spi_master *master, |
| 885 | struct device *dev, struct sg_table *sgt, |
| 886 | enum dma_data_direction dir) |
| 887 | { |
| 888 | } |
| 889 | |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 890 | static inline int __spi_map_msg(struct spi_master *master, |
| 891 | struct spi_message *msg) |
| 892 | { |
| 893 | return 0; |
| 894 | } |
| 895 | |
Martin Sperl | 4b78645 | 2015-05-25 10:13:10 +0000 | [diff] [blame] | 896 | static inline int __spi_unmap_msg(struct spi_master *master, |
| 897 | struct spi_message *msg) |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 898 | { |
| 899 | return 0; |
| 900 | } |
| 901 | #endif /* !CONFIG_HAS_DMA */ |
| 902 | |
Martin Sperl | 4b78645 | 2015-05-25 10:13:10 +0000 | [diff] [blame] | 903 | static inline int spi_unmap_msg(struct spi_master *master, |
| 904 | struct spi_message *msg) |
| 905 | { |
| 906 | struct spi_transfer *xfer; |
| 907 | |
| 908 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 909 | /* |
| 910 | * Restore the original value of tx_buf or rx_buf if they are |
| 911 | * NULL. |
| 912 | */ |
| 913 | if (xfer->tx_buf == master->dummy_tx) |
| 914 | xfer->tx_buf = NULL; |
| 915 | if (xfer->rx_buf == master->dummy_rx) |
| 916 | xfer->rx_buf = NULL; |
| 917 | } |
| 918 | |
| 919 | return __spi_unmap_msg(master, msg); |
| 920 | } |
| 921 | |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 922 | static int spi_map_msg(struct spi_master *master, struct spi_message *msg) |
| 923 | { |
| 924 | struct spi_transfer *xfer; |
| 925 | void *tmp; |
| 926 | unsigned int max_tx, max_rx; |
| 927 | |
| 928 | if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) { |
| 929 | max_tx = 0; |
| 930 | max_rx = 0; |
| 931 | |
| 932 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 933 | if ((master->flags & SPI_MASTER_MUST_TX) && |
| 934 | !xfer->tx_buf) |
| 935 | max_tx = max(xfer->len, max_tx); |
| 936 | if ((master->flags & SPI_MASTER_MUST_RX) && |
| 937 | !xfer->rx_buf) |
| 938 | max_rx = max(xfer->len, max_rx); |
| 939 | } |
| 940 | |
| 941 | if (max_tx) { |
| 942 | tmp = krealloc(master->dummy_tx, max_tx, |
| 943 | GFP_KERNEL | GFP_DMA); |
| 944 | if (!tmp) |
| 945 | return -ENOMEM; |
| 946 | master->dummy_tx = tmp; |
| 947 | memset(tmp, 0, max_tx); |
| 948 | } |
| 949 | |
| 950 | if (max_rx) { |
| 951 | tmp = krealloc(master->dummy_rx, max_rx, |
| 952 | GFP_KERNEL | GFP_DMA); |
| 953 | if (!tmp) |
| 954 | return -ENOMEM; |
| 955 | master->dummy_rx = tmp; |
| 956 | } |
| 957 | |
| 958 | if (max_tx || max_rx) { |
| 959 | list_for_each_entry(xfer, &msg->transfers, |
| 960 | transfer_list) { |
| 961 | if (!xfer->tx_buf) |
| 962 | xfer->tx_buf = master->dummy_tx; |
| 963 | if (!xfer->rx_buf) |
| 964 | xfer->rx_buf = master->dummy_rx; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | return __spi_map_msg(master, msg); |
| 970 | } |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 971 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 972 | /* |
| 973 | * spi_transfer_one_message - Default implementation of transfer_one_message() |
| 974 | * |
| 975 | * This is a standard implementation of transfer_one_message() for |
Moritz Fischer | 8ba811a | 2016-05-03 11:59:30 -0700 | [diff] [blame] | 976 | * drivers which implement a transfer_one() operation. It provides |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 977 | * standard handling of delays and chip select management. |
| 978 | */ |
| 979 | static int spi_transfer_one_message(struct spi_master *master, |
| 980 | struct spi_message *msg) |
| 981 | { |
| 982 | struct spi_transfer *xfer; |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 983 | bool keep_cs = false; |
| 984 | int ret = 0; |
Sien Wu | d0716dd | 2016-09-01 18:24:29 -0500 | [diff] [blame] | 985 | unsigned long long ms = 1; |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 986 | struct spi_statistics *statm = &master->statistics; |
| 987 | struct spi_statistics *stats = &msg->spi->statistics; |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 988 | |
| 989 | spi_set_cs(msg->spi, true); |
| 990 | |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 991 | SPI_STATISTICS_INCREMENT_FIELD(statm, messages); |
| 992 | SPI_STATISTICS_INCREMENT_FIELD(stats, messages); |
| 993 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 994 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 995 | trace_spi_transfer_start(msg, xfer); |
| 996 | |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 997 | spi_statistics_add_transfer_stats(statm, xfer, master); |
| 998 | spi_statistics_add_transfer_stats(stats, xfer, master); |
| 999 | |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1000 | if (xfer->tx_buf || xfer->rx_buf) { |
| 1001 | reinit_completion(&master->xfer_completion); |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1002 | |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1003 | ret = master->transfer_one(master, msg->spi, xfer); |
| 1004 | if (ret < 0) { |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 1005 | SPI_STATISTICS_INCREMENT_FIELD(statm, |
| 1006 | errors); |
| 1007 | SPI_STATISTICS_INCREMENT_FIELD(stats, |
| 1008 | errors); |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1009 | dev_err(&msg->spi->dev, |
| 1010 | "SPI transfer failed: %d\n", ret); |
| 1011 | goto out; |
| 1012 | } |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1013 | |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1014 | if (ret > 0) { |
| 1015 | ret = 0; |
Sien Wu | d0716dd | 2016-09-01 18:24:29 -0500 | [diff] [blame] | 1016 | ms = 8LL * 1000LL * xfer->len; |
| 1017 | do_div(ms, xfer->speed_hz); |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1018 | ms += ms + 100; /* some tolerance */ |
Mark Brown | 16a0ce4 | 2014-01-30 22:16:41 +0000 | [diff] [blame] | 1019 | |
Sien Wu | d0716dd | 2016-09-01 18:24:29 -0500 | [diff] [blame] | 1020 | if (ms > UINT_MAX) |
| 1021 | ms = UINT_MAX; |
| 1022 | |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1023 | ms = wait_for_completion_timeout(&master->xfer_completion, |
| 1024 | msecs_to_jiffies(ms)); |
| 1025 | } |
Mark Brown | 16a0ce4 | 2014-01-30 22:16:41 +0000 | [diff] [blame] | 1026 | |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1027 | if (ms == 0) { |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 1028 | SPI_STATISTICS_INCREMENT_FIELD(statm, |
| 1029 | timedout); |
| 1030 | SPI_STATISTICS_INCREMENT_FIELD(stats, |
| 1031 | timedout); |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1032 | dev_err(&msg->spi->dev, |
| 1033 | "SPI transfer timed out\n"); |
| 1034 | msg->status = -ETIMEDOUT; |
| 1035 | } |
| 1036 | } else { |
| 1037 | if (xfer->len) |
| 1038 | dev_err(&msg->spi->dev, |
| 1039 | "Bufferless transfer has length %u\n", |
| 1040 | xfer->len); |
Axel Lin | 13a4279 | 2014-01-18 22:05:22 +0800 | [diff] [blame] | 1041 | } |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1042 | |
| 1043 | trace_spi_transfer_stop(msg, xfer); |
| 1044 | |
| 1045 | if (msg->status != -EINPROGRESS) |
| 1046 | goto out; |
| 1047 | |
Daniel Kurtz | 8244bd3 | 2016-10-07 18:55:47 +0800 | [diff] [blame] | 1048 | if (xfer->delay_usecs) { |
| 1049 | u16 us = xfer->delay_usecs; |
| 1050 | |
| 1051 | if (us <= 10) |
| 1052 | udelay(us); |
| 1053 | else |
| 1054 | usleep_range(us, us + DIV_ROUND_UP(us, 10)); |
| 1055 | } |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1056 | |
| 1057 | if (xfer->cs_change) { |
| 1058 | if (list_is_last(&xfer->transfer_list, |
| 1059 | &msg->transfers)) { |
| 1060 | keep_cs = true; |
| 1061 | } else { |
Mark Brown | 0b73aa6 | 2014-03-29 23:48:07 +0000 | [diff] [blame] | 1062 | spi_set_cs(msg->spi, false); |
| 1063 | udelay(10); |
| 1064 | spi_set_cs(msg->spi, true); |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | msg->actual_length += xfer->len; |
| 1069 | } |
| 1070 | |
| 1071 | out: |
| 1072 | if (ret != 0 || !keep_cs) |
| 1073 | spi_set_cs(msg->spi, false); |
| 1074 | |
| 1075 | if (msg->status == -EINPROGRESS) |
| 1076 | msg->status = ret; |
| 1077 | |
Geert Uytterhoeven | ff61eb4 | 2015-04-07 20:39:19 +0200 | [diff] [blame] | 1078 | if (msg->status && master->handle_err) |
Andy Shevchenko | b716c4f | 2015-02-27 17:34:15 +0200 | [diff] [blame] | 1079 | master->handle_err(master, msg); |
| 1080 | |
Martin Sperl | d780c37 | 2015-12-14 15:20:18 +0000 | [diff] [blame] | 1081 | spi_res_release(master, msg); |
| 1082 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1083 | spi_finalize_current_message(master); |
| 1084 | |
| 1085 | return ret; |
| 1086 | } |
| 1087 | |
| 1088 | /** |
| 1089 | * spi_finalize_current_transfer - report completion of a transfer |
Thierry Reding | 2c67568 | 2014-08-08 13:02:36 +0200 | [diff] [blame] | 1090 | * @master: the master reporting completion |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1091 | * |
| 1092 | * Called by SPI drivers using the core transfer_one_message() |
| 1093 | * implementation to notify it that the current interrupt driven |
Geert Uytterhoeven | 9e8f488 | 2014-01-21 16:10:05 +0100 | [diff] [blame] | 1094 | * transfer has finished and the next one may be scheduled. |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1095 | */ |
| 1096 | void spi_finalize_current_transfer(struct spi_master *master) |
| 1097 | { |
| 1098 | complete(&master->xfer_completion); |
| 1099 | } |
| 1100 | EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); |
| 1101 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1102 | /** |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1103 | * __spi_pump_messages - function which processes spi message queue |
| 1104 | * @master: master to process queue for |
| 1105 | * @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] | 1106 | * |
| 1107 | * This function checks if there is any spi message in the queue that |
| 1108 | * needs processing and if so call out to the driver to initialize hardware |
| 1109 | * and transfer each message. |
| 1110 | * |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1111 | * Note that it is called both from the kthread itself and also from |
| 1112 | * inside spi_sync(); the queue extraction handling at the top of the |
| 1113 | * function should deal with this safely. |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1114 | */ |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 1115 | static void __spi_pump_messages(struct spi_master *master, bool in_kthread) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1116 | { |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1117 | unsigned long flags; |
| 1118 | bool was_busy = false; |
| 1119 | int ret; |
| 1120 | |
Mark Brown | 983aee5 | 2014-12-09 19:46:56 +0000 | [diff] [blame] | 1121 | /* Lock queue */ |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1122 | spin_lock_irqsave(&master->queue_lock, flags); |
Mark Brown | 983aee5 | 2014-12-09 19:46:56 +0000 | [diff] [blame] | 1123 | |
| 1124 | /* Make sure we are not already running a message */ |
| 1125 | if (master->cur_msg) { |
| 1126 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1127 | return; |
| 1128 | } |
| 1129 | |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1130 | /* If another context is idling the device then defer */ |
| 1131 | if (master->idling) { |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 1132 | kthread_queue_work(&master->kworker, &master->pump_messages); |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1133 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1134 | return; |
| 1135 | } |
| 1136 | |
Mark Brown | 983aee5 | 2014-12-09 19:46:56 +0000 | [diff] [blame] | 1137 | /* Check if the queue is idle */ |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1138 | if (list_empty(&master->queue) || !master->running) { |
Bryan Freed | b0b36b8 | 2013-03-13 11:17:40 -0700 | [diff] [blame] | 1139 | if (!master->busy) { |
| 1140 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1141 | return; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1142 | } |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1143 | |
| 1144 | /* Only do teardown in the thread */ |
| 1145 | if (!in_kthread) { |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 1146 | kthread_queue_work(&master->kworker, |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1147 | &master->pump_messages); |
| 1148 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1152 | master->busy = false; |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1153 | master->idling = true; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1154 | spin_unlock_irqrestore(&master->queue_lock, flags); |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1155 | |
Mark Brown | 3a2eba9 | 2014-01-28 20:17:03 +0000 | [diff] [blame] | 1156 | kfree(master->dummy_rx); |
| 1157 | master->dummy_rx = NULL; |
| 1158 | kfree(master->dummy_tx); |
| 1159 | master->dummy_tx = NULL; |
Bryan Freed | b0b36b8 | 2013-03-13 11:17:40 -0700 | [diff] [blame] | 1160 | if (master->unprepare_transfer_hardware && |
| 1161 | master->unprepare_transfer_hardware(master)) |
| 1162 | dev_err(&master->dev, |
| 1163 | "failed to unprepare transfer hardware\n"); |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 1164 | if (master->auto_runtime_pm) { |
| 1165 | pm_runtime_mark_last_busy(master->dev.parent); |
| 1166 | pm_runtime_put_autosuspend(master->dev.parent); |
| 1167 | } |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 1168 | trace_spi_master_idle(master); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1169 | |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1170 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1171 | master->idling = false; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1172 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1173 | return; |
| 1174 | } |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1175 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1176 | /* Extract head of queue */ |
| 1177 | master->cur_msg = |
Axel Lin | a89e2d2 | 2014-01-09 16:03:58 +0800 | [diff] [blame] | 1178 | list_first_entry(&master->queue, struct spi_message, queue); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1179 | |
| 1180 | list_del_init(&master->cur_msg->queue); |
| 1181 | if (master->busy) |
| 1182 | was_busy = true; |
| 1183 | else |
| 1184 | master->busy = true; |
| 1185 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1186 | |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 1187 | mutex_lock(&master->io_mutex); |
| 1188 | |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 1189 | if (!was_busy && master->auto_runtime_pm) { |
| 1190 | ret = pm_runtime_get_sync(master->dev.parent); |
| 1191 | if (ret < 0) { |
| 1192 | dev_err(&master->dev, "Failed to power device: %d\n", |
| 1193 | ret); |
Mark Brown | 764f216 | 2016-08-09 17:45:33 +0100 | [diff] [blame] | 1194 | mutex_unlock(&master->io_mutex); |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 1195 | return; |
| 1196 | } |
| 1197 | } |
| 1198 | |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 1199 | if (!was_busy) |
| 1200 | trace_spi_master_busy(master); |
| 1201 | |
Shubhrajyoti D | 7dfd2bd | 2012-05-10 19:20:41 +0530 | [diff] [blame] | 1202 | if (!was_busy && master->prepare_transfer_hardware) { |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1203 | ret = master->prepare_transfer_hardware(master); |
| 1204 | if (ret) { |
| 1205 | dev_err(&master->dev, |
| 1206 | "failed to prepare transfer hardware\n"); |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 1207 | |
| 1208 | if (master->auto_runtime_pm) |
| 1209 | pm_runtime_put(master->dev.parent); |
Mark Brown | 764f216 | 2016-08-09 17:45:33 +0100 | [diff] [blame] | 1210 | mutex_unlock(&master->io_mutex); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1211 | return; |
| 1212 | } |
| 1213 | } |
| 1214 | |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 1215 | trace_spi_message_start(master->cur_msg); |
| 1216 | |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1217 | if (master->prepare_message) { |
| 1218 | ret = master->prepare_message(master, master->cur_msg); |
| 1219 | if (ret) { |
| 1220 | dev_err(&master->dev, |
| 1221 | "failed to prepare message: %d\n", ret); |
| 1222 | master->cur_msg->status = ret; |
| 1223 | spi_finalize_current_message(master); |
Jon Hunter | 49023d2 | 2016-03-08 12:28:20 +0000 | [diff] [blame] | 1224 | goto out; |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1225 | } |
| 1226 | master->cur_msg_prepared = true; |
| 1227 | } |
| 1228 | |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1229 | ret = spi_map_msg(master, master->cur_msg); |
| 1230 | if (ret) { |
| 1231 | master->cur_msg->status = ret; |
| 1232 | spi_finalize_current_message(master); |
Jon Hunter | 49023d2 | 2016-03-08 12:28:20 +0000 | [diff] [blame] | 1233 | goto out; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1236 | ret = master->transfer_one_message(master, master->cur_msg); |
| 1237 | if (ret) { |
| 1238 | dev_err(&master->dev, |
Geert Uytterhoeven | 1f802f8 | 2014-01-28 10:33:03 +0100 | [diff] [blame] | 1239 | "failed to transfer one message from queue\n"); |
Jon Hunter | 49023d2 | 2016-03-08 12:28:20 +0000 | [diff] [blame] | 1240 | goto out; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1241 | } |
Jon Hunter | 49023d2 | 2016-03-08 12:28:20 +0000 | [diff] [blame] | 1242 | |
| 1243 | out: |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 1244 | mutex_unlock(&master->io_mutex); |
Mark Brown | 6282697 | 2016-02-15 18:28:02 +0000 | [diff] [blame] | 1245 | |
| 1246 | /* Prod the scheduler in case transfer_one() was busy waiting */ |
Jon Hunter | 49023d2 | 2016-03-08 12:28:20 +0000 | [diff] [blame] | 1247 | if (!ret) |
| 1248 | cond_resched(); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1249 | } |
| 1250 | |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1251 | /** |
| 1252 | * spi_pump_messages - kthread work function which processes spi message queue |
| 1253 | * @work: pointer to kthread work struct contained in the master struct |
| 1254 | */ |
| 1255 | static void spi_pump_messages(struct kthread_work *work) |
| 1256 | { |
| 1257 | struct spi_master *master = |
| 1258 | container_of(work, struct spi_master, pump_messages); |
| 1259 | |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 1260 | __spi_pump_messages(master, true); |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1263 | static int spi_init_queue(struct spi_master *master) |
| 1264 | { |
| 1265 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; |
| 1266 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1267 | master->running = false; |
| 1268 | master->busy = false; |
| 1269 | |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 1270 | kthread_init_worker(&master->kworker); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1271 | master->kworker_task = kthread_run(kthread_worker_fn, |
Kees Cook | f170168 | 2013-07-03 15:04:58 -0700 | [diff] [blame] | 1272 | &master->kworker, "%s", |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1273 | dev_name(&master->dev)); |
| 1274 | if (IS_ERR(master->kworker_task)) { |
| 1275 | dev_err(&master->dev, "failed to create message pump task\n"); |
Jarkko Nikula | 98a8f5a | 2014-12-04 11:02:25 +0200 | [diff] [blame] | 1276 | return PTR_ERR(master->kworker_task); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1277 | } |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 1278 | kthread_init_work(&master->pump_messages, spi_pump_messages); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1279 | |
| 1280 | /* |
| 1281 | * Master config will indicate if this controller should run the |
| 1282 | * message pump with high (realtime) priority to reduce the transfer |
| 1283 | * latency on the bus by minimising the delay between a transfer |
| 1284 | * request and the scheduling of the message pump thread. Without this |
| 1285 | * setting the message pump thread will remain at default priority. |
| 1286 | */ |
| 1287 | if (master->rt) { |
| 1288 | dev_info(&master->dev, |
| 1289 | "will run message pump with realtime priority\n"); |
| 1290 | sched_setscheduler(master->kworker_task, SCHED_FIFO, ¶m); |
| 1291 | } |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | /** |
| 1297 | * spi_get_next_queued_message() - called by driver to check for queued |
| 1298 | * messages |
| 1299 | * @master: the master to check for queued messages |
| 1300 | * |
| 1301 | * If there are more messages in the queue, the next message is returned from |
| 1302 | * this call. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 1303 | * |
| 1304 | * Return: the next message in the queue, else NULL if the queue is empty. |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1305 | */ |
| 1306 | struct spi_message *spi_get_next_queued_message(struct spi_master *master) |
| 1307 | { |
| 1308 | struct spi_message *next; |
| 1309 | unsigned long flags; |
| 1310 | |
| 1311 | /* get a pointer to the next message, if any */ |
| 1312 | spin_lock_irqsave(&master->queue_lock, flags); |
Axel Lin | 1cfd97f | 2014-01-02 15:16:40 +0800 | [diff] [blame] | 1313 | next = list_first_entry_or_null(&master->queue, struct spi_message, |
| 1314 | queue); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1315 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1316 | |
| 1317 | return next; |
| 1318 | } |
| 1319 | EXPORT_SYMBOL_GPL(spi_get_next_queued_message); |
| 1320 | |
| 1321 | /** |
| 1322 | * spi_finalize_current_message() - the current message is complete |
| 1323 | * @master: the master to return the message to |
| 1324 | * |
| 1325 | * Called by the driver to notify the core that the message in the front of the |
| 1326 | * queue is complete and can be removed from the queue. |
| 1327 | */ |
| 1328 | void spi_finalize_current_message(struct spi_master *master) |
| 1329 | { |
| 1330 | struct spi_message *mesg; |
| 1331 | unsigned long flags; |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1332 | int ret; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1333 | |
| 1334 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1335 | mesg = master->cur_msg; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1336 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1337 | |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1338 | spi_unmap_msg(master, mesg); |
| 1339 | |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1340 | if (master->cur_msg_prepared && master->unprepare_message) { |
| 1341 | ret = master->unprepare_message(master, mesg); |
| 1342 | if (ret) { |
| 1343 | dev_err(&master->dev, |
| 1344 | "failed to unprepare message: %d\n", ret); |
| 1345 | } |
| 1346 | } |
Uwe Kleine-König | 391949b | 2015-03-18 11:27:28 +0100 | [diff] [blame] | 1347 | |
Martin Sperl | 8e76ef8 | 2015-05-10 07:50:45 +0000 | [diff] [blame] | 1348 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1349 | master->cur_msg = NULL; |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1350 | master->cur_msg_prepared = false; |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 1351 | kthread_queue_work(&master->kworker, &master->pump_messages); |
Martin Sperl | 8e76ef8 | 2015-05-10 07:50:45 +0000 | [diff] [blame] | 1352 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1353 | |
| 1354 | trace_spi_message_done(mesg); |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1355 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1356 | mesg->state = NULL; |
| 1357 | if (mesg->complete) |
| 1358 | mesg->complete(mesg->context); |
| 1359 | } |
| 1360 | EXPORT_SYMBOL_GPL(spi_finalize_current_message); |
| 1361 | |
| 1362 | static int spi_start_queue(struct spi_master *master) |
| 1363 | { |
| 1364 | unsigned long flags; |
| 1365 | |
| 1366 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1367 | |
| 1368 | if (master->running || master->busy) { |
| 1369 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1370 | return -EBUSY; |
| 1371 | } |
| 1372 | |
| 1373 | master->running = true; |
| 1374 | master->cur_msg = NULL; |
| 1375 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1376 | |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 1377 | kthread_queue_work(&master->kworker, &master->pump_messages); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1378 | |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | static int spi_stop_queue(struct spi_master *master) |
| 1383 | { |
| 1384 | unsigned long flags; |
| 1385 | unsigned limit = 500; |
| 1386 | int ret = 0; |
| 1387 | |
| 1388 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1389 | |
| 1390 | /* |
| 1391 | * This is a bit lame, but is optimized for the common execution path. |
| 1392 | * A wait_queue on the master->busy could be used, but then the common |
| 1393 | * execution path (pump_messages) would be required to call wake_up or |
| 1394 | * friends on every SPI message. Do this instead. |
| 1395 | */ |
| 1396 | while ((!list_empty(&master->queue) || master->busy) && limit--) { |
| 1397 | spin_unlock_irqrestore(&master->queue_lock, flags); |
Axel Lin | f97b26b | 2014-02-21 09:15:18 +0800 | [diff] [blame] | 1398 | usleep_range(10000, 11000); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1399 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1400 | } |
| 1401 | |
| 1402 | if (!list_empty(&master->queue) || master->busy) |
| 1403 | ret = -EBUSY; |
| 1404 | else |
| 1405 | master->running = false; |
| 1406 | |
| 1407 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1408 | |
| 1409 | if (ret) { |
| 1410 | dev_warn(&master->dev, |
| 1411 | "could not stop message queue\n"); |
| 1412 | return ret; |
| 1413 | } |
| 1414 | return ret; |
| 1415 | } |
| 1416 | |
| 1417 | static int spi_destroy_queue(struct spi_master *master) |
| 1418 | { |
| 1419 | int ret; |
| 1420 | |
| 1421 | ret = spi_stop_queue(master); |
| 1422 | |
| 1423 | /* |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 1424 | * kthread_flush_worker will block until all work is done. |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1425 | * If the reason that stop_queue timed out is that the work will never |
| 1426 | * finish, then it does no good to call flush/stop thread, so |
| 1427 | * return anyway. |
| 1428 | */ |
| 1429 | if (ret) { |
| 1430 | dev_err(&master->dev, "problem destroying queue\n"); |
| 1431 | return ret; |
| 1432 | } |
| 1433 | |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 1434 | kthread_flush_worker(&master->kworker); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1435 | kthread_stop(master->kworker_task); |
| 1436 | |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1440 | static int __spi_queued_transfer(struct spi_device *spi, |
| 1441 | struct spi_message *msg, |
| 1442 | bool need_pump) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1443 | { |
| 1444 | struct spi_master *master = spi->master; |
| 1445 | unsigned long flags; |
| 1446 | |
| 1447 | spin_lock_irqsave(&master->queue_lock, flags); |
| 1448 | |
| 1449 | if (!master->running) { |
| 1450 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1451 | return -ESHUTDOWN; |
| 1452 | } |
| 1453 | msg->actual_length = 0; |
| 1454 | msg->status = -EINPROGRESS; |
| 1455 | |
| 1456 | list_add_tail(&msg->queue, &master->queue); |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1457 | if (!master->busy && need_pump) |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 1458 | kthread_queue_work(&master->kworker, &master->pump_messages); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1459 | |
| 1460 | spin_unlock_irqrestore(&master->queue_lock, flags); |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1464 | /** |
| 1465 | * spi_queued_transfer - transfer function for queued transfers |
| 1466 | * @spi: spi device which is requesting transfer |
| 1467 | * @msg: spi message which is to handled is queued to driver queue |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 1468 | * |
| 1469 | * Return: zero on success, else a negative error code. |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1470 | */ |
| 1471 | static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) |
| 1472 | { |
| 1473 | return __spi_queued_transfer(spi, msg, true); |
| 1474 | } |
| 1475 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1476 | static int spi_master_initialize_queue(struct spi_master *master) |
| 1477 | { |
| 1478 | int ret; |
| 1479 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1480 | master->transfer = spi_queued_transfer; |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1481 | if (!master->transfer_one_message) |
| 1482 | master->transfer_one_message = spi_transfer_one_message; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1483 | |
| 1484 | /* Initialize and start queue */ |
| 1485 | ret = spi_init_queue(master); |
| 1486 | if (ret) { |
| 1487 | dev_err(&master->dev, "problem initializing queue\n"); |
| 1488 | goto err_init_queue; |
| 1489 | } |
Mark Brown | c3676d5 | 2014-05-01 10:47:52 -0700 | [diff] [blame] | 1490 | master->queued = true; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1491 | ret = spi_start_queue(master); |
| 1492 | if (ret) { |
| 1493 | dev_err(&master->dev, "problem starting queue\n"); |
| 1494 | goto err_start_queue; |
| 1495 | } |
| 1496 | |
| 1497 | return 0; |
| 1498 | |
| 1499 | err_start_queue: |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1500 | spi_destroy_queue(master); |
Mark Brown | c3676d5 | 2014-05-01 10:47:52 -0700 | [diff] [blame] | 1501 | err_init_queue: |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1502 | return ret; |
| 1503 | } |
| 1504 | |
| 1505 | /*-------------------------------------------------------------------------*/ |
| 1506 | |
Andreas Larsson | 7cb9436 | 2012-12-04 15:09:38 +0100 | [diff] [blame] | 1507 | #if defined(CONFIG_OF) |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 1508 | static int of_spi_parse_dt(struct spi_master *master, struct spi_device *spi, |
| 1509 | struct device_node *nc) |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1510 | { |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1511 | u32 value; |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 1512 | int rc; |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1513 | |
| 1514 | /* Device address */ |
| 1515 | rc = of_property_read_u32(nc, "reg", &value); |
| 1516 | if (rc) { |
| 1517 | dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n", |
| 1518 | nc->full_name, rc); |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 1519 | return rc; |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1520 | } |
| 1521 | spi->chip_select = value; |
| 1522 | |
| 1523 | /* Mode (clock phase/polarity/etc.) */ |
| 1524 | if (of_find_property(nc, "spi-cpha", NULL)) |
| 1525 | spi->mode |= SPI_CPHA; |
| 1526 | if (of_find_property(nc, "spi-cpol", NULL)) |
| 1527 | spi->mode |= SPI_CPOL; |
| 1528 | if (of_find_property(nc, "spi-cs-high", NULL)) |
| 1529 | spi->mode |= SPI_CS_HIGH; |
| 1530 | if (of_find_property(nc, "spi-3wire", NULL)) |
| 1531 | spi->mode |= SPI_3WIRE; |
| 1532 | if (of_find_property(nc, "spi-lsb-first", NULL)) |
| 1533 | spi->mode |= SPI_LSB_FIRST; |
| 1534 | |
| 1535 | /* Device DUAL/QUAD mode */ |
| 1536 | if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { |
| 1537 | switch (value) { |
| 1538 | case 1: |
| 1539 | break; |
| 1540 | case 2: |
| 1541 | spi->mode |= SPI_TX_DUAL; |
| 1542 | break; |
| 1543 | case 4: |
| 1544 | spi->mode |= SPI_TX_QUAD; |
| 1545 | break; |
| 1546 | default: |
| 1547 | dev_warn(&master->dev, |
| 1548 | "spi-tx-bus-width %d not supported\n", |
| 1549 | value); |
| 1550 | break; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { |
| 1555 | switch (value) { |
| 1556 | case 1: |
| 1557 | break; |
| 1558 | case 2: |
| 1559 | spi->mode |= SPI_RX_DUAL; |
| 1560 | break; |
| 1561 | case 4: |
| 1562 | spi->mode |= SPI_RX_QUAD; |
| 1563 | break; |
| 1564 | default: |
| 1565 | dev_warn(&master->dev, |
| 1566 | "spi-rx-bus-width %d not supported\n", |
| 1567 | value); |
| 1568 | break; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | /* Device speed */ |
| 1573 | rc = of_property_read_u32(nc, "spi-max-frequency", &value); |
| 1574 | if (rc) { |
| 1575 | dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n", |
| 1576 | nc->full_name, rc); |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 1577 | return rc; |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1578 | } |
| 1579 | spi->max_speed_hz = value; |
| 1580 | |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 1581 | return 0; |
| 1582 | } |
| 1583 | |
| 1584 | static struct spi_device * |
| 1585 | of_register_spi_device(struct spi_master *master, struct device_node *nc) |
| 1586 | { |
| 1587 | struct spi_device *spi; |
| 1588 | int rc; |
| 1589 | |
| 1590 | /* Alloc an spi_device */ |
| 1591 | spi = spi_alloc_device(master); |
| 1592 | if (!spi) { |
| 1593 | dev_err(&master->dev, "spi_device alloc error for %s\n", |
| 1594 | nc->full_name); |
| 1595 | rc = -ENOMEM; |
| 1596 | goto err_out; |
| 1597 | } |
| 1598 | |
| 1599 | /* Select device driver */ |
| 1600 | rc = of_modalias_node(nc, spi->modalias, |
| 1601 | sizeof(spi->modalias)); |
| 1602 | if (rc < 0) { |
| 1603 | dev_err(&master->dev, "cannot find modalias for %s\n", |
| 1604 | nc->full_name); |
| 1605 | goto err_out; |
| 1606 | } |
| 1607 | |
| 1608 | rc = of_spi_parse_dt(master, spi, nc); |
| 1609 | if (rc) |
| 1610 | goto err_out; |
| 1611 | |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1612 | /* Store a pointer to the node in the device structure */ |
| 1613 | of_node_get(nc); |
| 1614 | spi->dev.of_node = nc; |
| 1615 | |
| 1616 | /* Register the new device */ |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1617 | rc = spi_add_device(spi); |
| 1618 | if (rc) { |
| 1619 | dev_err(&master->dev, "spi_device register error %s\n", |
| 1620 | nc->full_name); |
Johan Hovold | 8324147 | 2017-01-30 17:47:05 +0100 | [diff] [blame] | 1621 | goto err_of_node_put; |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | return spi; |
| 1625 | |
Johan Hovold | 8324147 | 2017-01-30 17:47:05 +0100 | [diff] [blame] | 1626 | err_of_node_put: |
| 1627 | of_node_put(nc); |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1628 | err_out: |
| 1629 | spi_dev_put(spi); |
| 1630 | return ERR_PTR(rc); |
| 1631 | } |
| 1632 | |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1633 | /** |
| 1634 | * of_register_spi_devices() - Register child devices onto the SPI bus |
| 1635 | * @master: Pointer to spi_master device |
| 1636 | * |
| 1637 | * Registers an spi_device for each child node of master node which has a 'reg' |
| 1638 | * property. |
| 1639 | */ |
| 1640 | static void of_register_spi_devices(struct spi_master *master) |
| 1641 | { |
| 1642 | struct spi_device *spi; |
| 1643 | struct device_node *nc; |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1644 | |
| 1645 | if (!master->dev.of_node) |
| 1646 | return; |
| 1647 | |
Alexander Sverdlin | f3b6159 | 2012-11-29 08:59:29 +0100 | [diff] [blame] | 1648 | for_each_available_child_of_node(master->dev.of_node, nc) { |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 1649 | if (of_node_test_and_set_flag(nc, OF_POPULATED)) |
| 1650 | continue; |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1651 | spi = of_register_spi_device(master, nc); |
Ralf Ramsauer | e0af98a | 2016-10-17 15:59:56 +0200 | [diff] [blame] | 1652 | if (IS_ERR(spi)) { |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1653 | dev_warn(&master->dev, "Failed to create SPI device for %s\n", |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1654 | nc->full_name); |
Ralf Ramsauer | e0af98a | 2016-10-17 15:59:56 +0200 | [diff] [blame] | 1655 | of_node_clear_flag(nc, OF_POPULATED); |
| 1656 | } |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 1657 | } |
| 1658 | } |
| 1659 | #else |
| 1660 | static void of_register_spi_devices(struct spi_master *master) { } |
| 1661 | #endif |
| 1662 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1663 | #ifdef CONFIG_ACPI |
| 1664 | static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) |
| 1665 | { |
| 1666 | struct spi_device *spi = data; |
Mika Westerberg | a0a9071 | 2016-02-08 17:14:28 +0200 | [diff] [blame] | 1667 | struct spi_master *master = spi->master; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1668 | |
| 1669 | if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { |
| 1670 | struct acpi_resource_spi_serialbus *sb; |
| 1671 | |
| 1672 | sb = &ares->data.spi_serial_bus; |
| 1673 | if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { |
Mika Westerberg | a0a9071 | 2016-02-08 17:14:28 +0200 | [diff] [blame] | 1674 | /* |
| 1675 | * ACPI DeviceSelection numbering is handled by the |
| 1676 | * host controller driver in Windows and can vary |
| 1677 | * from driver to driver. In Linux we always expect |
| 1678 | * 0 .. max - 1 so we need to ask the driver to |
| 1679 | * translate between the two schemes. |
| 1680 | */ |
| 1681 | if (master->fw_translate_cs) { |
| 1682 | int cs = master->fw_translate_cs(master, |
| 1683 | sb->device_selection); |
| 1684 | if (cs < 0) |
| 1685 | return cs; |
| 1686 | spi->chip_select = cs; |
| 1687 | } else { |
| 1688 | spi->chip_select = sb->device_selection; |
| 1689 | } |
| 1690 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1691 | spi->max_speed_hz = sb->connection_speed; |
| 1692 | |
| 1693 | if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) |
| 1694 | spi->mode |= SPI_CPHA; |
| 1695 | if (sb->clock_polarity == ACPI_SPI_START_HIGH) |
| 1696 | spi->mode |= SPI_CPOL; |
| 1697 | if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) |
| 1698 | spi->mode |= SPI_CS_HIGH; |
| 1699 | } |
| 1700 | } else if (spi->irq < 0) { |
| 1701 | struct resource r; |
| 1702 | |
| 1703 | if (acpi_dev_resource_interrupt(ares, 0, &r)) |
| 1704 | spi->irq = r.start; |
| 1705 | } |
| 1706 | |
| 1707 | /* Always tell the ACPI core to skip this resource */ |
| 1708 | return 1; |
| 1709 | } |
| 1710 | |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 1711 | static acpi_status acpi_register_spi_device(struct spi_master *master, |
| 1712 | struct acpi_device *adev) |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1713 | { |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1714 | struct list_head resource_list; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1715 | struct spi_device *spi; |
| 1716 | int ret; |
| 1717 | |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 1718 | if (acpi_bus_get_status(adev) || !adev->status.present || |
| 1719 | acpi_device_enumerated(adev)) |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1720 | return AE_OK; |
| 1721 | |
| 1722 | spi = spi_alloc_device(master); |
| 1723 | if (!spi) { |
| 1724 | dev_err(&master->dev, "failed to allocate SPI device for %s\n", |
| 1725 | dev_name(&adev->dev)); |
| 1726 | return AE_NO_MEMORY; |
| 1727 | } |
| 1728 | |
Rafael J. Wysocki | 7b19981 | 2013-11-11 22:41:56 +0100 | [diff] [blame] | 1729 | ACPI_COMPANION_SET(&spi->dev, adev); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1730 | spi->irq = -1; |
| 1731 | |
| 1732 | INIT_LIST_HEAD(&resource_list); |
| 1733 | ret = acpi_dev_get_resources(adev, &resource_list, |
| 1734 | acpi_spi_add_resource, spi); |
| 1735 | acpi_dev_free_resource_list(&resource_list); |
| 1736 | |
| 1737 | if (ret < 0 || !spi->max_speed_hz) { |
| 1738 | spi_dev_put(spi); |
| 1739 | return AE_OK; |
| 1740 | } |
| 1741 | |
Dan O'Donovan | 0c6543f | 2017-02-05 16:30:14 +0000 | [diff] [blame] | 1742 | acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, |
| 1743 | sizeof(spi->modalias)); |
| 1744 | |
Christophe RICARD | 33ada67 | 2015-12-23 23:25:35 +0100 | [diff] [blame] | 1745 | if (spi->irq < 0) |
| 1746 | spi->irq = acpi_dev_gpio_irq_get(adev, 0); |
| 1747 | |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 1748 | acpi_device_set_enumerated(adev); |
| 1749 | |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 1750 | adev->power.flags.ignore_parent = true; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1751 | if (spi_add_device(spi)) { |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 1752 | adev->power.flags.ignore_parent = false; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1753 | dev_err(&master->dev, "failed to add SPI device %s from ACPI\n", |
| 1754 | dev_name(&adev->dev)); |
| 1755 | spi_dev_put(spi); |
| 1756 | } |
| 1757 | |
| 1758 | return AE_OK; |
| 1759 | } |
| 1760 | |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 1761 | static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, |
| 1762 | void *data, void **return_value) |
| 1763 | { |
| 1764 | struct spi_master *master = data; |
| 1765 | struct acpi_device *adev; |
| 1766 | |
| 1767 | if (acpi_bus_get_device(handle, &adev)) |
| 1768 | return AE_OK; |
| 1769 | |
| 1770 | return acpi_register_spi_device(master, adev); |
| 1771 | } |
| 1772 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1773 | static void acpi_register_spi_devices(struct spi_master *master) |
| 1774 | { |
| 1775 | acpi_status status; |
| 1776 | acpi_handle handle; |
| 1777 | |
Rafael J. Wysocki | 2989617 | 2013-04-01 00:21:08 +0000 | [diff] [blame] | 1778 | handle = ACPI_HANDLE(master->dev.parent); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1779 | if (!handle) |
| 1780 | return; |
| 1781 | |
| 1782 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, |
| 1783 | acpi_spi_add_device, NULL, |
| 1784 | master, NULL); |
| 1785 | if (ACPI_FAILURE(status)) |
| 1786 | dev_warn(&master->dev, "failed to enumerate SPI slaves\n"); |
| 1787 | } |
| 1788 | #else |
| 1789 | static inline void acpi_register_spi_devices(struct spi_master *master) {} |
| 1790 | #endif /* CONFIG_ACPI */ |
| 1791 | |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1792 | static void spi_master_release(struct device *dev) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1793 | { |
| 1794 | struct spi_master *master; |
| 1795 | |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1796 | master = container_of(dev, struct spi_master, dev); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1797 | kfree(master); |
| 1798 | } |
| 1799 | |
| 1800 | static struct class spi_master_class = { |
| 1801 | .name = "spi_master", |
| 1802 | .owner = THIS_MODULE, |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1803 | .dev_release = spi_master_release, |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 1804 | .dev_groups = spi_master_groups, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1805 | }; |
| 1806 | |
| 1807 | |
| 1808 | /** |
| 1809 | * spi_alloc_master - allocate SPI master controller |
| 1810 | * @dev: the controller, possibly using the platform_bus |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 1811 | * @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] | 1812 | * memory is in the driver_data field of the returned device, |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1813 | * accessible with spi_master_get_devdata(). |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 1814 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1815 | * |
| 1816 | * This call is used only by SPI master controller drivers, which are the |
| 1817 | * only ones directly touching chip registers. It's how they allocate |
dmitry pervushin | ba1a051 | 2006-05-20 15:00:14 -0700 | [diff] [blame] | 1818 | * an spi_master structure, prior to calling spi_register_master(). |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1819 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 1820 | * This must be called from context that can sleep. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1821 | * |
| 1822 | * The caller is responsible for assigning the bus number and initializing |
dmitry pervushin | ba1a051 | 2006-05-20 15:00:14 -0700 | [diff] [blame] | 1823 | * the master's methods before calling spi_register_master(); and (after errors |
Guenter Roeck | a394d63 | 2015-09-06 01:46:54 +0300 | [diff] [blame] | 1824 | * adding the device) calling spi_master_put() to prevent a memory leak. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 1825 | * |
| 1826 | * Return: the SPI master structure on success, else NULL. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1827 | */ |
Adrian Bunk | e9d5a46 | 2007-03-26 21:32:23 -0800 | [diff] [blame] | 1828 | struct spi_master *spi_alloc_master(struct device *dev, unsigned size) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1829 | { |
| 1830 | struct spi_master *master; |
| 1831 | |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1832 | if (!dev) |
| 1833 | return NULL; |
| 1834 | |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 1835 | master = kzalloc(size + sizeof(*master), GFP_KERNEL); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1836 | if (!master) |
| 1837 | return NULL; |
| 1838 | |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1839 | device_initialize(&master->dev); |
Grant Likely | 1e8a52e | 2012-05-19 23:42:08 -0600 | [diff] [blame] | 1840 | master->bus_num = -1; |
| 1841 | master->num_chipselect = 1; |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1842 | master->dev.class = &spi_master_class; |
Johan Hovold | 157f38f | 2015-12-14 16:16:19 +0100 | [diff] [blame] | 1843 | master->dev.parent = dev; |
Linus Walleij | d7e2ee2 | 2016-04-11 13:51:03 +0200 | [diff] [blame] | 1844 | pm_suspend_ignore_children(&master->dev, true); |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1845 | spi_master_set_devdata(master, &master[1]); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1846 | |
| 1847 | return master; |
| 1848 | } |
| 1849 | EXPORT_SYMBOL_GPL(spi_alloc_master); |
| 1850 | |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1851 | #ifdef CONFIG_OF |
| 1852 | static int of_spi_register_master(struct spi_master *master) |
| 1853 | { |
Grant Likely | e80beb2 | 2013-02-12 17:48:37 +0000 | [diff] [blame] | 1854 | int nb, i, *cs; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1855 | struct device_node *np = master->dev.of_node; |
| 1856 | |
| 1857 | if (!np) |
| 1858 | return 0; |
| 1859 | |
| 1860 | nb = of_gpio_named_count(np, "cs-gpios"); |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 1861 | master->num_chipselect = max_t(int, nb, master->num_chipselect); |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1862 | |
Andreas Larsson | 8ec5d84 | 2013-02-13 14:23:24 +0100 | [diff] [blame] | 1863 | /* Return error only for an incorrectly formed cs-gpios property */ |
| 1864 | if (nb == 0 || nb == -ENOENT) |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1865 | return 0; |
Andreas Larsson | 8ec5d84 | 2013-02-13 14:23:24 +0100 | [diff] [blame] | 1866 | else if (nb < 0) |
| 1867 | return nb; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1868 | |
| 1869 | cs = devm_kzalloc(&master->dev, |
| 1870 | sizeof(int) * master->num_chipselect, |
| 1871 | GFP_KERNEL); |
| 1872 | master->cs_gpios = cs; |
| 1873 | |
| 1874 | if (!master->cs_gpios) |
| 1875 | return -ENOMEM; |
| 1876 | |
Andreas Larsson | 0da83bb | 2013-01-29 15:53:40 +0100 | [diff] [blame] | 1877 | for (i = 0; i < master->num_chipselect; i++) |
Andreas Larsson | 446411e | 2013-02-13 14:20:25 +0100 | [diff] [blame] | 1878 | cs[i] = -ENOENT; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1879 | |
| 1880 | for (i = 0; i < nb; i++) |
| 1881 | cs[i] = of_get_named_gpio(np, "cs-gpios", i); |
| 1882 | |
| 1883 | return 0; |
| 1884 | } |
| 1885 | #else |
| 1886 | static int of_spi_register_master(struct spi_master *master) |
| 1887 | { |
| 1888 | return 0; |
| 1889 | } |
| 1890 | #endif |
| 1891 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1892 | /** |
| 1893 | * spi_register_master - register SPI master controller |
| 1894 | * @master: initialized master, originally from spi_alloc_master() |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 1895 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1896 | * |
| 1897 | * SPI master controllers connect to their drivers using some non-SPI bus, |
| 1898 | * such as the platform bus. The final stage of probe() in that code |
| 1899 | * includes calling spi_register_master() to hook up to this SPI bus glue. |
| 1900 | * |
| 1901 | * SPI controllers use board specific (often SOC specific) bus numbers, |
| 1902 | * and board-specific addressing for SPI devices combines those numbers |
| 1903 | * with chip select numbers. Since SPI does not directly support dynamic |
| 1904 | * device identification, boards need configuration tables telling which |
| 1905 | * chip is at which address. |
| 1906 | * |
| 1907 | * This must be called from context that can sleep. It returns zero on |
| 1908 | * success, else a negative error code (dropping the master's refcount). |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1909 | * After a successful return, the caller is responsible for calling |
| 1910 | * spi_unregister_master(). |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 1911 | * |
| 1912 | * Return: zero on success, else a negative error code. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1913 | */ |
Adrian Bunk | e9d5a46 | 2007-03-26 21:32:23 -0800 | [diff] [blame] | 1914 | int spi_register_master(struct spi_master *master) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1915 | { |
David Brownell | e44a45a | 2007-06-03 13:50:40 -0700 | [diff] [blame] | 1916 | static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1); |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1917 | struct device *dev = master->dev.parent; |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 1918 | struct boardinfo *bi; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1919 | int status = -ENODEV; |
| 1920 | int dynamic = 0; |
| 1921 | |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1922 | if (!dev) |
| 1923 | return -ENODEV; |
| 1924 | |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 1925 | status = of_spi_register_master(master); |
| 1926 | if (status) |
| 1927 | return status; |
| 1928 | |
David Brownell | 082c8cb | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 1929 | /* even if it's just one always-selected device, there must |
| 1930 | * be at least one chipselect |
| 1931 | */ |
| 1932 | if (master->num_chipselect == 0) |
| 1933 | return -EINVAL; |
| 1934 | |
Grant Likely | bb29785 | 2012-12-21 19:32:09 +0000 | [diff] [blame] | 1935 | if ((master->bus_num < 0) && master->dev.of_node) |
| 1936 | master->bus_num = of_alias_get_id(master->dev.of_node, "spi"); |
| 1937 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1938 | /* convention: dynamically assigned bus IDs count down from the max */ |
David Brownell | a020ed7 | 2006-04-03 15:49:04 -0700 | [diff] [blame] | 1939 | if (master->bus_num < 0) { |
David Brownell | 082c8cb | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 1940 | /* FIXME switch to an IDR based scheme, something like |
| 1941 | * I2C now uses, so we can't run out of "dynamic" IDs |
| 1942 | */ |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1943 | master->bus_num = atomic_dec_return(&dyn_bus_id); |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 1944 | dynamic = 1; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1945 | } |
| 1946 | |
Mark Brown | 5424d43 | 2014-12-10 17:40:53 +0000 | [diff] [blame] | 1947 | INIT_LIST_HEAD(&master->queue); |
| 1948 | spin_lock_init(&master->queue_lock); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1949 | spin_lock_init(&master->bus_lock_spinlock); |
| 1950 | mutex_init(&master->bus_lock_mutex); |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 1951 | mutex_init(&master->io_mutex); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1952 | master->bus_lock_flag = 0; |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1953 | init_completion(&master->xfer_completion); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 1954 | if (!master->max_dma_len) |
| 1955 | master->max_dma_len = INT_MAX; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 1956 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1957 | /* register the device, then userspace will see it. |
| 1958 | * registration fails if the bus ID is in use. |
| 1959 | */ |
Kay Sievers | 35f74fc | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 1960 | dev_set_name(&master->dev, "spi%u", master->bus_num); |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1961 | status = device_add(&master->dev); |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 1962 | if (status < 0) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1963 | goto done; |
Kay Sievers | 35f74fc | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 1964 | dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev), |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1965 | dynamic ? " (dynamic)" : ""); |
| 1966 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1967 | /* If we're using a queued driver, start the queue */ |
| 1968 | if (master->transfer) |
| 1969 | dev_info(dev, "master is unqueued, this is deprecated\n"); |
| 1970 | else { |
| 1971 | status = spi_master_initialize_queue(master); |
| 1972 | if (status) { |
Axel Lin | e93b072 | 2013-08-31 20:25:52 +0800 | [diff] [blame] | 1973 | device_del(&master->dev); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1974 | goto done; |
| 1975 | } |
| 1976 | } |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 1977 | /* add statistics */ |
| 1978 | spin_lock_init(&master->statistics.lock); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1979 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 1980 | mutex_lock(&board_lock); |
| 1981 | list_add_tail(&master->list, &spi_master_list); |
| 1982 | list_for_each_entry(bi, &board_list, list) |
| 1983 | spi_match_master_to_boardinfo(master, &bi->board_info); |
| 1984 | mutex_unlock(&board_lock); |
| 1985 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1986 | /* Register devices from the device tree and ACPI */ |
Anatolij Gustschin | 12b15e8 | 2010-07-27 22:35:58 +0200 | [diff] [blame] | 1987 | of_register_spi_devices(master); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 1988 | acpi_register_spi_devices(master); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 1989 | done: |
| 1990 | return status; |
| 1991 | } |
| 1992 | EXPORT_SYMBOL_GPL(spi_register_master); |
| 1993 | |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 1994 | static void devm_spi_unregister(struct device *dev, void *res) |
| 1995 | { |
| 1996 | spi_unregister_master(*(struct spi_master **)res); |
| 1997 | } |
| 1998 | |
| 1999 | /** |
| 2000 | * dev_spi_register_master - register managed SPI master controller |
| 2001 | * @dev: device managing SPI master |
| 2002 | * @master: initialized master, originally from spi_alloc_master() |
| 2003 | * Context: can sleep |
| 2004 | * |
| 2005 | * Register a SPI device as with spi_register_master() which will |
| 2006 | * automatically be unregister |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2007 | * |
| 2008 | * Return: zero on success, else a negative error code. |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 2009 | */ |
| 2010 | int devm_spi_register_master(struct device *dev, struct spi_master *master) |
| 2011 | { |
| 2012 | struct spi_master **ptr; |
| 2013 | int ret; |
| 2014 | |
| 2015 | ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); |
| 2016 | if (!ptr) |
| 2017 | return -ENOMEM; |
| 2018 | |
| 2019 | ret = spi_register_master(master); |
Stephen Warren | 4b92894 | 2013-11-21 16:11:15 -0700 | [diff] [blame] | 2020 | if (!ret) { |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 2021 | *ptr = master; |
| 2022 | devres_add(dev, ptr); |
| 2023 | } else { |
| 2024 | devres_free(ptr); |
| 2025 | } |
| 2026 | |
| 2027 | return ret; |
| 2028 | } |
| 2029 | EXPORT_SYMBOL_GPL(devm_spi_register_master); |
| 2030 | |
David Lamparter | 3486008 | 2010-08-30 23:54:17 +0200 | [diff] [blame] | 2031 | static int __unregister(struct device *dev, void *null) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2032 | { |
David Lamparter | 3486008 | 2010-08-30 23:54:17 +0200 | [diff] [blame] | 2033 | spi_unregister_device(to_spi_device(dev)); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2034 | return 0; |
| 2035 | } |
| 2036 | |
| 2037 | /** |
| 2038 | * spi_unregister_master - unregister SPI master controller |
| 2039 | * @master: the master being unregistered |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2040 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2041 | * |
| 2042 | * This call is used only by SPI master controller drivers, which are the |
| 2043 | * only ones directly touching chip registers. |
| 2044 | * |
| 2045 | * This must be called from context that can sleep. |
| 2046 | */ |
| 2047 | void spi_unregister_master(struct spi_master *master) |
| 2048 | { |
Jeff Garzik | 89fc9a1 | 2006-12-06 20:35:35 -0800 | [diff] [blame] | 2049 | int dummy; |
| 2050 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2051 | if (master->queued) { |
| 2052 | if (spi_destroy_queue(master)) |
| 2053 | dev_err(&master->dev, "queue remove failed\n"); |
| 2054 | } |
| 2055 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 2056 | mutex_lock(&board_lock); |
| 2057 | list_del(&master->list); |
| 2058 | mutex_unlock(&board_lock); |
| 2059 | |
Sebastian Andrzej Siewior | 97dbf37 | 2010-12-21 17:24:31 -0800 | [diff] [blame] | 2060 | dummy = device_for_each_child(&master->dev, NULL, __unregister); |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 2061 | device_unregister(&master->dev); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2062 | } |
| 2063 | EXPORT_SYMBOL_GPL(spi_unregister_master); |
| 2064 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2065 | int spi_master_suspend(struct spi_master *master) |
| 2066 | { |
| 2067 | int ret; |
| 2068 | |
| 2069 | /* Basically no-ops for non-queued masters */ |
| 2070 | if (!master->queued) |
| 2071 | return 0; |
| 2072 | |
| 2073 | ret = spi_stop_queue(master); |
| 2074 | if (ret) |
| 2075 | dev_err(&master->dev, "queue stop failed\n"); |
| 2076 | |
| 2077 | return ret; |
| 2078 | } |
| 2079 | EXPORT_SYMBOL_GPL(spi_master_suspend); |
| 2080 | |
| 2081 | int spi_master_resume(struct spi_master *master) |
| 2082 | { |
| 2083 | int ret; |
| 2084 | |
| 2085 | if (!master->queued) |
| 2086 | return 0; |
| 2087 | |
| 2088 | ret = spi_start_queue(master); |
| 2089 | if (ret) |
| 2090 | dev_err(&master->dev, "queue restart failed\n"); |
| 2091 | |
| 2092 | return ret; |
| 2093 | } |
| 2094 | EXPORT_SYMBOL_GPL(spi_master_resume); |
| 2095 | |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 2096 | static int __spi_master_match(struct device *dev, const void *data) |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 2097 | { |
| 2098 | struct spi_master *m; |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 2099 | const u16 *bus_num = data; |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 2100 | |
| 2101 | m = container_of(dev, struct spi_master, dev); |
| 2102 | return m->bus_num == *bus_num; |
| 2103 | } |
| 2104 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2105 | /** |
| 2106 | * spi_busnum_to_master - look up master associated with bus_num |
| 2107 | * @bus_num: the master's bus number |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2108 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2109 | * |
| 2110 | * This call may be used with devices that are registered after |
| 2111 | * arch init time. It returns a refcounted pointer to the relevant |
| 2112 | * spi_master (which the caller must release), or NULL if there is |
| 2113 | * no such master registered. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2114 | * |
| 2115 | * Return: the SPI master structure on success, else NULL. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2116 | */ |
| 2117 | struct spi_master *spi_busnum_to_master(u16 bus_num) |
| 2118 | { |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 2119 | struct device *dev; |
Atsushi Nemoto | 1e9a51d | 2007-01-26 00:56:54 -0800 | [diff] [blame] | 2120 | struct spi_master *master = NULL; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2121 | |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 2122 | dev = class_find_device(&spi_master_class, NULL, &bus_num, |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 2123 | __spi_master_match); |
| 2124 | if (dev) |
| 2125 | master = container_of(dev, struct spi_master, dev); |
| 2126 | /* reference got in class_find_device */ |
Atsushi Nemoto | 1e9a51d | 2007-01-26 00:56:54 -0800 | [diff] [blame] | 2127 | return master; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2128 | } |
| 2129 | EXPORT_SYMBOL_GPL(spi_busnum_to_master); |
| 2130 | |
Martin Sperl | d780c37 | 2015-12-14 15:20:18 +0000 | [diff] [blame] | 2131 | /*-------------------------------------------------------------------------*/ |
| 2132 | |
| 2133 | /* Core methods for SPI resource management */ |
| 2134 | |
| 2135 | /** |
| 2136 | * spi_res_alloc - allocate a spi resource that is life-cycle managed |
| 2137 | * during the processing of a spi_message while using |
| 2138 | * spi_transfer_one |
| 2139 | * @spi: the spi device for which we allocate memory |
| 2140 | * @release: the release code to execute for this resource |
| 2141 | * @size: size to alloc and return |
| 2142 | * @gfp: GFP allocation flags |
| 2143 | * |
| 2144 | * Return: the pointer to the allocated data |
| 2145 | * |
| 2146 | * This may get enhanced in the future to allocate from a memory pool |
| 2147 | * of the @spi_device or @spi_master to avoid repeated allocations. |
| 2148 | */ |
| 2149 | void *spi_res_alloc(struct spi_device *spi, |
| 2150 | spi_res_release_t release, |
| 2151 | size_t size, gfp_t gfp) |
| 2152 | { |
| 2153 | struct spi_res *sres; |
| 2154 | |
| 2155 | sres = kzalloc(sizeof(*sres) + size, gfp); |
| 2156 | if (!sres) |
| 2157 | return NULL; |
| 2158 | |
| 2159 | INIT_LIST_HEAD(&sres->entry); |
| 2160 | sres->release = release; |
| 2161 | |
| 2162 | return sres->data; |
| 2163 | } |
| 2164 | EXPORT_SYMBOL_GPL(spi_res_alloc); |
| 2165 | |
| 2166 | /** |
| 2167 | * spi_res_free - free an spi resource |
| 2168 | * @res: pointer to the custom data of a resource |
| 2169 | * |
| 2170 | */ |
| 2171 | void spi_res_free(void *res) |
| 2172 | { |
| 2173 | struct spi_res *sres = container_of(res, struct spi_res, data); |
| 2174 | |
| 2175 | if (!res) |
| 2176 | return; |
| 2177 | |
| 2178 | WARN_ON(!list_empty(&sres->entry)); |
| 2179 | kfree(sres); |
| 2180 | } |
| 2181 | EXPORT_SYMBOL_GPL(spi_res_free); |
| 2182 | |
| 2183 | /** |
| 2184 | * spi_res_add - add a spi_res to the spi_message |
| 2185 | * @message: the spi message |
| 2186 | * @res: the spi_resource |
| 2187 | */ |
| 2188 | void spi_res_add(struct spi_message *message, void *res) |
| 2189 | { |
| 2190 | struct spi_res *sres = container_of(res, struct spi_res, data); |
| 2191 | |
| 2192 | WARN_ON(!list_empty(&sres->entry)); |
| 2193 | list_add_tail(&sres->entry, &message->resources); |
| 2194 | } |
| 2195 | EXPORT_SYMBOL_GPL(spi_res_add); |
| 2196 | |
| 2197 | /** |
| 2198 | * spi_res_release - release all spi resources for this message |
| 2199 | * @master: the @spi_master |
| 2200 | * @message: the @spi_message |
| 2201 | */ |
| 2202 | void spi_res_release(struct spi_master *master, |
| 2203 | struct spi_message *message) |
| 2204 | { |
| 2205 | struct spi_res *res; |
| 2206 | |
| 2207 | while (!list_empty(&message->resources)) { |
| 2208 | res = list_last_entry(&message->resources, |
| 2209 | struct spi_res, entry); |
| 2210 | |
| 2211 | if (res->release) |
| 2212 | res->release(master, message, res->data); |
| 2213 | |
| 2214 | list_del(&res->entry); |
| 2215 | |
| 2216 | kfree(res); |
| 2217 | } |
| 2218 | } |
| 2219 | EXPORT_SYMBOL_GPL(spi_res_release); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2220 | |
| 2221 | /*-------------------------------------------------------------------------*/ |
| 2222 | |
Martin Sperl | 523baf5a | 2015-12-14 15:20:19 +0000 | [diff] [blame] | 2223 | /* Core methods for spi_message alterations */ |
| 2224 | |
| 2225 | static void __spi_replace_transfers_release(struct spi_master *master, |
| 2226 | struct spi_message *msg, |
| 2227 | void *res) |
| 2228 | { |
| 2229 | struct spi_replaced_transfers *rxfer = res; |
| 2230 | size_t i; |
| 2231 | |
| 2232 | /* call extra callback if requested */ |
| 2233 | if (rxfer->release) |
| 2234 | rxfer->release(master, msg, res); |
| 2235 | |
| 2236 | /* insert replaced transfers back into the message */ |
| 2237 | list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); |
| 2238 | |
| 2239 | /* remove the formerly inserted entries */ |
| 2240 | for (i = 0; i < rxfer->inserted; i++) |
| 2241 | list_del(&rxfer->inserted_transfers[i].transfer_list); |
| 2242 | } |
| 2243 | |
| 2244 | /** |
| 2245 | * spi_replace_transfers - replace transfers with several transfers |
| 2246 | * and register change with spi_message.resources |
| 2247 | * @msg: the spi_message we work upon |
| 2248 | * @xfer_first: the first spi_transfer we want to replace |
| 2249 | * @remove: number of transfers to remove |
| 2250 | * @insert: the number of transfers we want to insert instead |
| 2251 | * @release: extra release code necessary in some circumstances |
| 2252 | * @extradatasize: extra data to allocate (with alignment guarantees |
| 2253 | * of struct @spi_transfer) |
Martin Sperl | 0588539 | 2016-02-18 15:53:11 +0000 | [diff] [blame] | 2254 | * @gfp: gfp flags |
Martin Sperl | 523baf5a | 2015-12-14 15:20:19 +0000 | [diff] [blame] | 2255 | * |
| 2256 | * Returns: pointer to @spi_replaced_transfers, |
| 2257 | * PTR_ERR(...) in case of errors. |
| 2258 | */ |
| 2259 | struct spi_replaced_transfers *spi_replace_transfers( |
| 2260 | struct spi_message *msg, |
| 2261 | struct spi_transfer *xfer_first, |
| 2262 | size_t remove, |
| 2263 | size_t insert, |
| 2264 | spi_replaced_release_t release, |
| 2265 | size_t extradatasize, |
| 2266 | gfp_t gfp) |
| 2267 | { |
| 2268 | struct spi_replaced_transfers *rxfer; |
| 2269 | struct spi_transfer *xfer; |
| 2270 | size_t i; |
| 2271 | |
| 2272 | /* allocate the structure using spi_res */ |
| 2273 | rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, |
| 2274 | insert * sizeof(struct spi_transfer) |
| 2275 | + sizeof(struct spi_replaced_transfers) |
| 2276 | + extradatasize, |
| 2277 | gfp); |
| 2278 | if (!rxfer) |
| 2279 | return ERR_PTR(-ENOMEM); |
| 2280 | |
| 2281 | /* the release code to invoke before running the generic release */ |
| 2282 | rxfer->release = release; |
| 2283 | |
| 2284 | /* assign extradata */ |
| 2285 | if (extradatasize) |
| 2286 | rxfer->extradata = |
| 2287 | &rxfer->inserted_transfers[insert]; |
| 2288 | |
| 2289 | /* init the replaced_transfers list */ |
| 2290 | INIT_LIST_HEAD(&rxfer->replaced_transfers); |
| 2291 | |
| 2292 | /* assign the list_entry after which we should reinsert |
| 2293 | * the @replaced_transfers - it may be spi_message.messages! |
| 2294 | */ |
| 2295 | rxfer->replaced_after = xfer_first->transfer_list.prev; |
| 2296 | |
| 2297 | /* remove the requested number of transfers */ |
| 2298 | for (i = 0; i < remove; i++) { |
| 2299 | /* if the entry after replaced_after it is msg->transfers |
| 2300 | * then we have been requested to remove more transfers |
| 2301 | * than are in the list |
| 2302 | */ |
| 2303 | if (rxfer->replaced_after->next == &msg->transfers) { |
| 2304 | dev_err(&msg->spi->dev, |
| 2305 | "requested to remove more spi_transfers than are available\n"); |
| 2306 | /* insert replaced transfers back into the message */ |
| 2307 | list_splice(&rxfer->replaced_transfers, |
| 2308 | rxfer->replaced_after); |
| 2309 | |
| 2310 | /* free the spi_replace_transfer structure */ |
| 2311 | spi_res_free(rxfer); |
| 2312 | |
| 2313 | /* and return with an error */ |
| 2314 | return ERR_PTR(-EINVAL); |
| 2315 | } |
| 2316 | |
| 2317 | /* remove the entry after replaced_after from list of |
| 2318 | * transfers and add it to list of replaced_transfers |
| 2319 | */ |
| 2320 | list_move_tail(rxfer->replaced_after->next, |
| 2321 | &rxfer->replaced_transfers); |
| 2322 | } |
| 2323 | |
| 2324 | /* create copy of the given xfer with identical settings |
| 2325 | * based on the first transfer to get removed |
| 2326 | */ |
| 2327 | for (i = 0; i < insert; i++) { |
| 2328 | /* we need to run in reverse order */ |
| 2329 | xfer = &rxfer->inserted_transfers[insert - 1 - i]; |
| 2330 | |
| 2331 | /* copy all spi_transfer data */ |
| 2332 | memcpy(xfer, xfer_first, sizeof(*xfer)); |
| 2333 | |
| 2334 | /* add to list */ |
| 2335 | list_add(&xfer->transfer_list, rxfer->replaced_after); |
| 2336 | |
| 2337 | /* clear cs_change and delay_usecs for all but the last */ |
| 2338 | if (i) { |
| 2339 | xfer->cs_change = false; |
| 2340 | xfer->delay_usecs = 0; |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | /* set up inserted */ |
| 2345 | rxfer->inserted = insert; |
| 2346 | |
| 2347 | /* and register it with spi_res/spi_message */ |
| 2348 | spi_res_add(msg, rxfer); |
| 2349 | |
| 2350 | return rxfer; |
| 2351 | } |
| 2352 | EXPORT_SYMBOL_GPL(spi_replace_transfers); |
| 2353 | |
Fabio Estevam | 0893341 | 2016-02-14 13:33:50 -0200 | [diff] [blame] | 2354 | static int __spi_split_transfer_maxsize(struct spi_master *master, |
| 2355 | struct spi_message *msg, |
| 2356 | struct spi_transfer **xferp, |
| 2357 | size_t maxsize, |
| 2358 | gfp_t gfp) |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 2359 | { |
| 2360 | struct spi_transfer *xfer = *xferp, *xfers; |
| 2361 | struct spi_replaced_transfers *srt; |
| 2362 | size_t offset; |
| 2363 | size_t count, i; |
| 2364 | |
| 2365 | /* warn once about this fact that we are splitting a transfer */ |
| 2366 | dev_warn_once(&msg->spi->dev, |
Fabio Estevam | 7d62f51 | 2016-02-17 15:42:27 -0200 | [diff] [blame] | 2367 | "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n", |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 2368 | xfer->len, maxsize); |
| 2369 | |
| 2370 | /* calculate how many we have to replace */ |
| 2371 | count = DIV_ROUND_UP(xfer->len, maxsize); |
| 2372 | |
| 2373 | /* create replacement */ |
| 2374 | srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); |
Dan Carpenter | 657d32e | 2016-02-12 09:38:33 +0300 | [diff] [blame] | 2375 | if (IS_ERR(srt)) |
| 2376 | return PTR_ERR(srt); |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 2377 | xfers = srt->inserted_transfers; |
| 2378 | |
| 2379 | /* now handle each of those newly inserted spi_transfers |
| 2380 | * note that the replacements spi_transfers all are preset |
| 2381 | * to the same values as *xferp, so tx_buf, rx_buf and len |
| 2382 | * are all identical (as well as most others) |
| 2383 | * so we just have to fix up len and the pointers. |
| 2384 | * |
| 2385 | * this also includes support for the depreciated |
| 2386 | * spi_message.is_dma_mapped interface |
| 2387 | */ |
| 2388 | |
| 2389 | /* the first transfer just needs the length modified, so we |
| 2390 | * run it outside the loop |
| 2391 | */ |
Fabio Estevam | c8dab77 | 2016-02-17 15:42:28 -0200 | [diff] [blame] | 2392 | xfers[0].len = min_t(size_t, maxsize, xfer[0].len); |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 2393 | |
| 2394 | /* all the others need rx_buf/tx_buf also set */ |
| 2395 | for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { |
| 2396 | /* update rx_buf, tx_buf and dma */ |
| 2397 | if (xfers[i].rx_buf) |
| 2398 | xfers[i].rx_buf += offset; |
| 2399 | if (xfers[i].rx_dma) |
| 2400 | xfers[i].rx_dma += offset; |
| 2401 | if (xfers[i].tx_buf) |
| 2402 | xfers[i].tx_buf += offset; |
| 2403 | if (xfers[i].tx_dma) |
| 2404 | xfers[i].tx_dma += offset; |
| 2405 | |
| 2406 | /* update length */ |
| 2407 | xfers[i].len = min(maxsize, xfers[i].len - offset); |
| 2408 | } |
| 2409 | |
| 2410 | /* we set up xferp to the last entry we have inserted, |
| 2411 | * so that we skip those already split transfers |
| 2412 | */ |
| 2413 | *xferp = &xfers[count - 1]; |
| 2414 | |
| 2415 | /* increment statistics counters */ |
| 2416 | SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, |
| 2417 | transfers_split_maxsize); |
| 2418 | SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, |
| 2419 | transfers_split_maxsize); |
| 2420 | |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
| 2424 | /** |
| 2425 | * spi_split_tranfers_maxsize - split spi transfers into multiple transfers |
| 2426 | * when an individual transfer exceeds a |
| 2427 | * certain size |
| 2428 | * @master: the @spi_master for this transfer |
Masanari Iida | 3700ce9 | 2016-02-22 20:33:44 +0900 | [diff] [blame] | 2429 | * @msg: the @spi_message to transform |
| 2430 | * @maxsize: the maximum when to apply this |
Javier Martinez Canillas | 10f11a2 | 2016-03-10 15:01:14 -0300 | [diff] [blame] | 2431 | * @gfp: GFP allocation flags |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 2432 | * |
| 2433 | * Return: status of transformation |
| 2434 | */ |
| 2435 | int spi_split_transfers_maxsize(struct spi_master *master, |
| 2436 | struct spi_message *msg, |
| 2437 | size_t maxsize, |
| 2438 | gfp_t gfp) |
| 2439 | { |
| 2440 | struct spi_transfer *xfer; |
| 2441 | int ret; |
| 2442 | |
| 2443 | /* iterate over the transfer_list, |
| 2444 | * but note that xfer is advanced to the last transfer inserted |
| 2445 | * to avoid checking sizes again unnecessarily (also xfer does |
| 2446 | * potentiall belong to a different list by the time the |
| 2447 | * replacement has happened |
| 2448 | */ |
| 2449 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 2450 | if (xfer->len > maxsize) { |
| 2451 | ret = __spi_split_transfer_maxsize( |
| 2452 | master, msg, &xfer, maxsize, gfp); |
| 2453 | if (ret) |
| 2454 | return ret; |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | return 0; |
| 2459 | } |
| 2460 | EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2461 | |
| 2462 | /*-------------------------------------------------------------------------*/ |
| 2463 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 2464 | /* Core methods for SPI master protocol drivers. Some of the |
| 2465 | * other core methods are currently defined as inline functions. |
| 2466 | */ |
| 2467 | |
Stefan Brüns | 63ab645 | 2015-08-23 16:06:30 +0200 | [diff] [blame] | 2468 | static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word) |
| 2469 | { |
| 2470 | if (master->bits_per_word_mask) { |
| 2471 | /* Only 32 bits fit in the mask */ |
| 2472 | if (bits_per_word > 32) |
| 2473 | return -EINVAL; |
| 2474 | if (!(master->bits_per_word_mask & |
| 2475 | SPI_BPW_MASK(bits_per_word))) |
| 2476 | return -EINVAL; |
| 2477 | } |
| 2478 | |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 2482 | /** |
| 2483 | * spi_setup - setup SPI mode and clock rate |
| 2484 | * @spi: the device whose settings are being modified |
| 2485 | * Context: can sleep, and no requests are queued to the device |
| 2486 | * |
| 2487 | * SPI protocol drivers may need to update the transfer mode if the |
| 2488 | * device doesn't work with its default. They may likewise need |
| 2489 | * to update clock rates or word sizes from initial values. This function |
| 2490 | * changes those settings, and must be called from a context that can sleep. |
| 2491 | * Except for SPI_CS_HIGH, which takes effect immediately, the changes take |
| 2492 | * effect the next time the device is selected and data is transferred to |
| 2493 | * or from it. When this function returns, the spi device is deselected. |
| 2494 | * |
| 2495 | * Note that this call will fail if the protocol driver specifies an option |
| 2496 | * that the underlying controller or its driver does not support. For |
| 2497 | * example, not all hardware supports wire transfers using nine bit words, |
| 2498 | * LSB-first wire encoding, or active-high chipselects. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2499 | * |
| 2500 | * Return: zero on success, else a negative error code. |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 2501 | */ |
| 2502 | int spi_setup(struct spi_device *spi) |
| 2503 | { |
Geert Uytterhoeven | 83596fb | 2014-04-14 19:39:53 +0200 | [diff] [blame] | 2504 | unsigned bad_bits, ugly_bits; |
Andy Shevchenko | 5ab8d26 | 2015-10-14 22:43:07 +0300 | [diff] [blame] | 2505 | int status; |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 2506 | |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 2507 | /* check mode to prevent that DUAL and QUAD set at the same time |
| 2508 | */ |
| 2509 | if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || |
| 2510 | ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { |
| 2511 | dev_err(&spi->dev, |
| 2512 | "setup: can not select dual and quad at the same time\n"); |
| 2513 | return -EINVAL; |
| 2514 | } |
| 2515 | /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden |
| 2516 | */ |
| 2517 | if ((spi->mode & SPI_3WIRE) && (spi->mode & |
| 2518 | (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD))) |
| 2519 | return -EINVAL; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 2520 | /* help drivers fail *cleanly* when they need options |
| 2521 | * that aren't supported with their current master |
| 2522 | */ |
| 2523 | bad_bits = spi->mode & ~spi->master->mode_bits; |
Geert Uytterhoeven | 83596fb | 2014-04-14 19:39:53 +0200 | [diff] [blame] | 2524 | ugly_bits = bad_bits & |
| 2525 | (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD); |
| 2526 | if (ugly_bits) { |
| 2527 | dev_warn(&spi->dev, |
| 2528 | "setup: ignoring unsupported mode bits %x\n", |
| 2529 | ugly_bits); |
| 2530 | spi->mode &= ~ugly_bits; |
| 2531 | bad_bits &= ~ugly_bits; |
| 2532 | } |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 2533 | if (bad_bits) { |
Linus Walleij | eb288a1 | 2010-10-21 21:06:44 +0200 | [diff] [blame] | 2534 | dev_err(&spi->dev, "setup: unsupported mode bits %x\n", |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 2535 | bad_bits); |
| 2536 | return -EINVAL; |
| 2537 | } |
| 2538 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 2539 | if (!spi->bits_per_word) |
| 2540 | spi->bits_per_word = 8; |
| 2541 | |
Andy Shevchenko | 5ab8d26 | 2015-10-14 22:43:07 +0300 | [diff] [blame] | 2542 | status = __spi_validate_bits_per_word(spi->master, spi->bits_per_word); |
| 2543 | if (status) |
| 2544 | return status; |
Stefan Brüns | 63ab645 | 2015-08-23 16:06:30 +0200 | [diff] [blame] | 2545 | |
Axel Lin | 052eb2d | 2014-02-10 00:08:05 +0800 | [diff] [blame] | 2546 | if (!spi->max_speed_hz) |
| 2547 | spi->max_speed_hz = spi->master->max_speed_hz; |
| 2548 | |
Laxman Dewangan | caae070 | 2012-11-09 14:35:22 +0530 | [diff] [blame] | 2549 | if (spi->master->setup) |
| 2550 | status = spi->master->setup(spi); |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 2551 | |
Franklin S Cooper Jr | abeedb0 | 2015-10-16 10:29:03 -0500 | [diff] [blame] | 2552 | spi_set_cs(spi, false); |
| 2553 | |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 2554 | 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] | 2555 | (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), |
| 2556 | (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", |
| 2557 | (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", |
| 2558 | (spi->mode & SPI_3WIRE) ? "3wire, " : "", |
| 2559 | (spi->mode & SPI_LOOP) ? "loopback, " : "", |
| 2560 | spi->bits_per_word, spi->max_speed_hz, |
| 2561 | status); |
| 2562 | |
| 2563 | return status; |
| 2564 | } |
| 2565 | EXPORT_SYMBOL_GPL(spi_setup); |
| 2566 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 2567 | static int __spi_validate(struct spi_device *spi, struct spi_message *message) |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2568 | { |
| 2569 | struct spi_master *master = spi->master; |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 2570 | struct spi_transfer *xfer; |
Atsushi Nemoto | 6ea3129 | 2014-02-28 23:03:16 +0900 | [diff] [blame] | 2571 | int w_size; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2572 | |
Mark Brown | 24a0013 | 2013-07-10 15:05:40 +0100 | [diff] [blame] | 2573 | if (list_empty(&message->transfers)) |
| 2574 | return -EINVAL; |
Mark Brown | 24a0013 | 2013-07-10 15:05:40 +0100 | [diff] [blame] | 2575 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2576 | /* Half-duplex links include original MicroWire, and ones with |
| 2577 | * only one data pin like SPI_3WIRE (switches direction) or where |
| 2578 | * either MOSI or MISO is missing. They can also be caused by |
| 2579 | * software limitations. |
| 2580 | */ |
| 2581 | if ((master->flags & SPI_MASTER_HALF_DUPLEX) |
| 2582 | || (spi->mode & SPI_3WIRE)) { |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2583 | unsigned flags = master->flags; |
| 2584 | |
| 2585 | list_for_each_entry(xfer, &message->transfers, transfer_list) { |
| 2586 | if (xfer->rx_buf && xfer->tx_buf) |
| 2587 | return -EINVAL; |
| 2588 | if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf) |
| 2589 | return -EINVAL; |
| 2590 | if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf) |
| 2591 | return -EINVAL; |
| 2592 | } |
| 2593 | } |
| 2594 | |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 2595 | /** |
Laxman Dewangan | 059b8ff | 2013-01-05 00:17:14 +0530 | [diff] [blame] | 2596 | * Set transfer bits_per_word and max speed as spi device default if |
| 2597 | * it is not set for this transfer. |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 2598 | * Set transfer tx_nbits and rx_nbits as single transfer default |
| 2599 | * (SPI_NBITS_SINGLE) if it is not set for this transfer. |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 2600 | */ |
Martin Sperl | 77e8058 | 2015-11-27 12:31:09 +0000 | [diff] [blame] | 2601 | message->frame_length = 0; |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 2602 | list_for_each_entry(xfer, &message->transfers, transfer_list) { |
Sourav Poddar | 078726c | 2013-07-18 15:31:25 +0530 | [diff] [blame] | 2603 | message->frame_length += xfer->len; |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 2604 | if (!xfer->bits_per_word) |
| 2605 | xfer->bits_per_word = spi->bits_per_word; |
Axel Lin | a6f87fa | 2014-03-17 10:08:12 +0800 | [diff] [blame] | 2606 | |
| 2607 | if (!xfer->speed_hz) |
Laxman Dewangan | 059b8ff | 2013-01-05 00:17:14 +0530 | [diff] [blame] | 2608 | xfer->speed_hz = spi->max_speed_hz; |
Mark Brown | 7dc9fbc | 2015-08-20 11:52:18 -0700 | [diff] [blame] | 2609 | if (!xfer->speed_hz) |
| 2610 | xfer->speed_hz = master->max_speed_hz; |
Axel Lin | a6f87fa | 2014-03-17 10:08:12 +0800 | [diff] [blame] | 2611 | |
| 2612 | if (master->max_speed_hz && |
| 2613 | xfer->speed_hz > master->max_speed_hz) |
| 2614 | xfer->speed_hz = master->max_speed_hz; |
Gabor Juhos | 56ede94 | 2013-08-14 10:25:28 +0200 | [diff] [blame] | 2615 | |
Stefan Brüns | 63ab645 | 2015-08-23 16:06:30 +0200 | [diff] [blame] | 2616 | if (__spi_validate_bits_per_word(master, xfer->bits_per_word)) |
| 2617 | return -EINVAL; |
Mark Brown | a2fd4f9 | 2013-07-10 14:57:26 +0100 | [diff] [blame] | 2618 | |
Ivan T. Ivanov | 4d94bd2 | 2014-02-20 12:02:08 +0200 | [diff] [blame] | 2619 | /* |
| 2620 | * SPI transfer length should be multiple of SPI word size |
| 2621 | * where SPI word size should be power-of-two multiple |
| 2622 | */ |
| 2623 | if (xfer->bits_per_word <= 8) |
| 2624 | w_size = 1; |
| 2625 | else if (xfer->bits_per_word <= 16) |
| 2626 | w_size = 2; |
| 2627 | else |
| 2628 | w_size = 4; |
| 2629 | |
Ivan T. Ivanov | 4d94bd2 | 2014-02-20 12:02:08 +0200 | [diff] [blame] | 2630 | /* No partial transfers accepted */ |
Atsushi Nemoto | 6ea3129 | 2014-02-28 23:03:16 +0900 | [diff] [blame] | 2631 | if (xfer->len % w_size) |
Ivan T. Ivanov | 4d94bd2 | 2014-02-20 12:02:08 +0200 | [diff] [blame] | 2632 | return -EINVAL; |
| 2633 | |
Mark Brown | a2fd4f9 | 2013-07-10 14:57:26 +0100 | [diff] [blame] | 2634 | if (xfer->speed_hz && master->min_speed_hz && |
| 2635 | xfer->speed_hz < master->min_speed_hz) |
| 2636 | return -EINVAL; |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 2637 | |
| 2638 | if (xfer->tx_buf && !xfer->tx_nbits) |
| 2639 | xfer->tx_nbits = SPI_NBITS_SINGLE; |
| 2640 | if (xfer->rx_buf && !xfer->rx_nbits) |
| 2641 | xfer->rx_nbits = SPI_NBITS_SINGLE; |
| 2642 | /* check transfer tx/rx_nbits: |
Geert Uytterhoeven | 1afd998 | 2014-01-12 14:00:29 +0100 | [diff] [blame] | 2643 | * 1. check the value matches one of single, dual and quad |
| 2644 | * 2. check tx/rx_nbits match the mode in spi_device |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 2645 | */ |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 2646 | if (xfer->tx_buf) { |
| 2647 | if (xfer->tx_nbits != SPI_NBITS_SINGLE && |
| 2648 | xfer->tx_nbits != SPI_NBITS_DUAL && |
| 2649 | xfer->tx_nbits != SPI_NBITS_QUAD) |
| 2650 | return -EINVAL; |
| 2651 | if ((xfer->tx_nbits == SPI_NBITS_DUAL) && |
| 2652 | !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) |
| 2653 | return -EINVAL; |
| 2654 | if ((xfer->tx_nbits == SPI_NBITS_QUAD) && |
| 2655 | !(spi->mode & SPI_TX_QUAD)) |
| 2656 | return -EINVAL; |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 2657 | } |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 2658 | /* check transfer rx_nbits */ |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 2659 | if (xfer->rx_buf) { |
| 2660 | if (xfer->rx_nbits != SPI_NBITS_SINGLE && |
| 2661 | xfer->rx_nbits != SPI_NBITS_DUAL && |
| 2662 | xfer->rx_nbits != SPI_NBITS_QUAD) |
| 2663 | return -EINVAL; |
| 2664 | if ((xfer->rx_nbits == SPI_NBITS_DUAL) && |
| 2665 | !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) |
| 2666 | return -EINVAL; |
| 2667 | if ((xfer->rx_nbits == SPI_NBITS_QUAD) && |
| 2668 | !(spi->mode & SPI_RX_QUAD)) |
| 2669 | return -EINVAL; |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 2670 | } |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 2671 | } |
| 2672 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2673 | message->status = -EINPROGRESS; |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 2674 | |
| 2675 | return 0; |
| 2676 | } |
| 2677 | |
| 2678 | static int __spi_async(struct spi_device *spi, struct spi_message *message) |
| 2679 | { |
| 2680 | struct spi_master *master = spi->master; |
| 2681 | |
| 2682 | message->spi = spi; |
| 2683 | |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 2684 | SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async); |
| 2685 | SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); |
| 2686 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 2687 | trace_spi_message_submit(message); |
| 2688 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2689 | return master->transfer(spi, message); |
| 2690 | } |
| 2691 | |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 2692 | /** |
| 2693 | * spi_async - asynchronous SPI transfer |
| 2694 | * @spi: device with which data will be exchanged |
| 2695 | * @message: describes the data transfers, including completion callback |
| 2696 | * Context: any (irqs may be blocked, etc) |
| 2697 | * |
| 2698 | * This call may be used in_irq and other contexts which can't sleep, |
| 2699 | * as well as from task contexts which can sleep. |
| 2700 | * |
| 2701 | * The completion callback is invoked in a context which can't sleep. |
| 2702 | * Before that invocation, the value of message->status is undefined. |
| 2703 | * When the callback is issued, message->status holds either zero (to |
| 2704 | * indicate complete success) or a negative error code. After that |
| 2705 | * callback returns, the driver which issued the transfer request may |
| 2706 | * deallocate the associated memory; it's no longer in use by any SPI |
| 2707 | * core or controller driver code. |
| 2708 | * |
| 2709 | * Note that although all messages to a spi_device are handled in |
| 2710 | * FIFO order, messages may go to different devices in other orders. |
| 2711 | * Some device might be higher priority, or have various "hard" access |
| 2712 | * time requirements, for example. |
| 2713 | * |
| 2714 | * On detection of any fault during the transfer, processing of |
| 2715 | * the entire message is aborted, and the device is deselected. |
| 2716 | * Until returning from the associated message completion callback, |
| 2717 | * no other spi_message queued to that device will be processed. |
| 2718 | * (This rule applies equally to all the synchronous transfer calls, |
| 2719 | * which are wrappers around this core asynchronous primitive.) |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2720 | * |
| 2721 | * Return: zero on success, else a negative error code. |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 2722 | */ |
| 2723 | int spi_async(struct spi_device *spi, struct spi_message *message) |
| 2724 | { |
| 2725 | struct spi_master *master = spi->master; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2726 | int ret; |
| 2727 | unsigned long flags; |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 2728 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 2729 | ret = __spi_validate(spi, message); |
| 2730 | if (ret != 0) |
| 2731 | return ret; |
| 2732 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2733 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 2734 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2735 | if (master->bus_lock_flag) |
| 2736 | ret = -EBUSY; |
| 2737 | else |
| 2738 | ret = __spi_async(spi, message); |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 2739 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2740 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
| 2741 | |
| 2742 | return ret; |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 2743 | } |
| 2744 | EXPORT_SYMBOL_GPL(spi_async); |
| 2745 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2746 | /** |
| 2747 | * spi_async_locked - version of spi_async with exclusive bus usage |
| 2748 | * @spi: device with which data will be exchanged |
| 2749 | * @message: describes the data transfers, including completion callback |
| 2750 | * Context: any (irqs may be blocked, etc) |
| 2751 | * |
| 2752 | * This call may be used in_irq and other contexts which can't sleep, |
| 2753 | * as well as from task contexts which can sleep. |
| 2754 | * |
| 2755 | * The completion callback is invoked in a context which can't sleep. |
| 2756 | * Before that invocation, the value of message->status is undefined. |
| 2757 | * When the callback is issued, message->status holds either zero (to |
| 2758 | * indicate complete success) or a negative error code. After that |
| 2759 | * callback returns, the driver which issued the transfer request may |
| 2760 | * deallocate the associated memory; it's no longer in use by any SPI |
| 2761 | * core or controller driver code. |
| 2762 | * |
| 2763 | * Note that although all messages to a spi_device are handled in |
| 2764 | * FIFO order, messages may go to different devices in other orders. |
| 2765 | * Some device might be higher priority, or have various "hard" access |
| 2766 | * time requirements, for example. |
| 2767 | * |
| 2768 | * On detection of any fault during the transfer, processing of |
| 2769 | * the entire message is aborted, and the device is deselected. |
| 2770 | * Until returning from the associated message completion callback, |
| 2771 | * no other spi_message queued to that device will be processed. |
| 2772 | * (This rule applies equally to all the synchronous transfer calls, |
| 2773 | * which are wrappers around this core asynchronous primitive.) |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2774 | * |
| 2775 | * Return: zero on success, else a negative error code. |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2776 | */ |
| 2777 | int spi_async_locked(struct spi_device *spi, struct spi_message *message) |
| 2778 | { |
| 2779 | struct spi_master *master = spi->master; |
| 2780 | int ret; |
| 2781 | unsigned long flags; |
| 2782 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 2783 | ret = __spi_validate(spi, message); |
| 2784 | if (ret != 0) |
| 2785 | return ret; |
| 2786 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2787 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
| 2788 | |
| 2789 | ret = __spi_async(spi, message); |
| 2790 | |
| 2791 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
| 2792 | |
| 2793 | return ret; |
| 2794 | |
| 2795 | } |
| 2796 | EXPORT_SYMBOL_GPL(spi_async_locked); |
| 2797 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 2798 | |
Vignesh R | 556351f | 2015-12-11 09:39:56 +0530 | [diff] [blame] | 2799 | int spi_flash_read(struct spi_device *spi, |
| 2800 | struct spi_flash_read_message *msg) |
| 2801 | |
| 2802 | { |
| 2803 | struct spi_master *master = spi->master; |
Vignesh R | f4502dd | 2016-06-08 12:18:31 +0530 | [diff] [blame] | 2804 | struct device *rx_dev = NULL; |
Vignesh R | 556351f | 2015-12-11 09:39:56 +0530 | [diff] [blame] | 2805 | int ret; |
| 2806 | |
| 2807 | if ((msg->opcode_nbits == SPI_NBITS_DUAL || |
| 2808 | msg->addr_nbits == SPI_NBITS_DUAL) && |
| 2809 | !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) |
| 2810 | return -EINVAL; |
| 2811 | if ((msg->opcode_nbits == SPI_NBITS_QUAD || |
| 2812 | msg->addr_nbits == SPI_NBITS_QUAD) && |
| 2813 | !(spi->mode & SPI_TX_QUAD)) |
| 2814 | return -EINVAL; |
| 2815 | if (msg->data_nbits == SPI_NBITS_DUAL && |
| 2816 | !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) |
| 2817 | return -EINVAL; |
| 2818 | if (msg->data_nbits == SPI_NBITS_QUAD && |
| 2819 | !(spi->mode & SPI_RX_QUAD)) |
| 2820 | return -EINVAL; |
| 2821 | |
| 2822 | if (master->auto_runtime_pm) { |
| 2823 | ret = pm_runtime_get_sync(master->dev.parent); |
| 2824 | if (ret < 0) { |
| 2825 | dev_err(&master->dev, "Failed to power device: %d\n", |
| 2826 | ret); |
| 2827 | return ret; |
| 2828 | } |
| 2829 | } |
Vignesh R | f4502dd | 2016-06-08 12:18:31 +0530 | [diff] [blame] | 2830 | |
Vignesh R | 556351f | 2015-12-11 09:39:56 +0530 | [diff] [blame] | 2831 | mutex_lock(&master->bus_lock_mutex); |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 2832 | mutex_lock(&master->io_mutex); |
Vignesh R | f4502dd | 2016-06-08 12:18:31 +0530 | [diff] [blame] | 2833 | if (master->dma_rx) { |
| 2834 | rx_dev = master->dma_rx->device->dev; |
| 2835 | ret = spi_map_buf(master, rx_dev, &msg->rx_sg, |
| 2836 | msg->buf, msg->len, |
| 2837 | DMA_FROM_DEVICE); |
| 2838 | if (!ret) |
| 2839 | msg->cur_msg_mapped = true; |
| 2840 | } |
Vignesh R | 556351f | 2015-12-11 09:39:56 +0530 | [diff] [blame] | 2841 | ret = master->spi_flash_read(spi, msg); |
Vignesh R | f4502dd | 2016-06-08 12:18:31 +0530 | [diff] [blame] | 2842 | if (msg->cur_msg_mapped) |
| 2843 | spi_unmap_buf(master, rx_dev, &msg->rx_sg, |
| 2844 | DMA_FROM_DEVICE); |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 2845 | mutex_unlock(&master->io_mutex); |
Vignesh R | 556351f | 2015-12-11 09:39:56 +0530 | [diff] [blame] | 2846 | mutex_unlock(&master->bus_lock_mutex); |
Vignesh R | f4502dd | 2016-06-08 12:18:31 +0530 | [diff] [blame] | 2847 | |
Vignesh R | 556351f | 2015-12-11 09:39:56 +0530 | [diff] [blame] | 2848 | if (master->auto_runtime_pm) |
| 2849 | pm_runtime_put(master->dev.parent); |
| 2850 | |
| 2851 | return ret; |
| 2852 | } |
| 2853 | EXPORT_SYMBOL_GPL(spi_flash_read); |
| 2854 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 2855 | /*-------------------------------------------------------------------------*/ |
| 2856 | |
| 2857 | /* Utility methods for SPI master protocol drivers, layered on |
| 2858 | * top of the core. Some other utility methods are defined as |
| 2859 | * inline functions. |
| 2860 | */ |
| 2861 | |
Andrew Morton | 5d870c8 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 2862 | static void spi_complete(void *arg) |
| 2863 | { |
| 2864 | complete(arg); |
| 2865 | } |
| 2866 | |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 2867 | static int __spi_sync(struct spi_device *spi, struct spi_message *message) |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2868 | { |
| 2869 | DECLARE_COMPLETION_ONSTACK(done); |
| 2870 | int status; |
| 2871 | struct spi_master *master = spi->master; |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 2872 | unsigned long flags; |
| 2873 | |
| 2874 | status = __spi_validate(spi, message); |
| 2875 | if (status != 0) |
| 2876 | return status; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2877 | |
| 2878 | message->complete = spi_complete; |
| 2879 | message->context = &done; |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 2880 | message->spi = spi; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2881 | |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 2882 | SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync); |
| 2883 | SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); |
| 2884 | |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 2885 | /* If we're not using the legacy transfer method then we will |
| 2886 | * try to transfer in the calling context so special case. |
| 2887 | * This code would be less tricky if we could remove the |
| 2888 | * support for driver implemented message queues. |
| 2889 | */ |
| 2890 | if (master->transfer == spi_queued_transfer) { |
| 2891 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
| 2892 | |
| 2893 | trace_spi_message_submit(message); |
| 2894 | |
| 2895 | status = __spi_queued_transfer(spi, message, false); |
| 2896 | |
| 2897 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
| 2898 | } else { |
| 2899 | status = spi_async_locked(spi, message); |
| 2900 | } |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2901 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2902 | if (status == 0) { |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 2903 | /* Push out the messages in the calling context if we |
| 2904 | * can. |
| 2905 | */ |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 2906 | if (master->transfer == spi_queued_transfer) { |
| 2907 | SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, |
| 2908 | spi_sync_immediate); |
| 2909 | SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, |
| 2910 | spi_sync_immediate); |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 2911 | __spi_pump_messages(master, false); |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 2912 | } |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 2913 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2914 | wait_for_completion(&done); |
| 2915 | status = message->status; |
| 2916 | } |
| 2917 | message->context = NULL; |
| 2918 | return status; |
| 2919 | } |
| 2920 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2921 | /** |
| 2922 | * spi_sync - blocking/synchronous SPI data transfers |
| 2923 | * @spi: device with which data will be exchanged |
| 2924 | * @message: describes the data transfers |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2925 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2926 | * |
| 2927 | * This call may only be used from a context that may sleep. The sleep |
| 2928 | * is non-interruptible, and has no timeout. Low-overhead controller |
| 2929 | * drivers may DMA directly into and out of the message buffers. |
| 2930 | * |
| 2931 | * Note that the SPI device's chip select is active during the message, |
| 2932 | * and then is normally disabled between messages. Drivers for some |
| 2933 | * frequently-used devices may want to minimize costs of selecting a chip, |
| 2934 | * by leaving it selected in anticipation that the next message will go |
| 2935 | * to the same chip. (That may increase power usage.) |
| 2936 | * |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 2937 | * Also, the caller is guaranteeing that the memory associated with the |
| 2938 | * message will not be freed before this call returns. |
| 2939 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2940 | * Return: zero on success, else a negative error code. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2941 | */ |
| 2942 | int spi_sync(struct spi_device *spi, struct spi_message *message) |
| 2943 | { |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 2944 | int ret; |
| 2945 | |
| 2946 | mutex_lock(&spi->master->bus_lock_mutex); |
| 2947 | ret = __spi_sync(spi, message); |
| 2948 | mutex_unlock(&spi->master->bus_lock_mutex); |
| 2949 | |
| 2950 | return ret; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2951 | } |
| 2952 | EXPORT_SYMBOL_GPL(spi_sync); |
| 2953 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2954 | /** |
| 2955 | * spi_sync_locked - version of spi_sync with exclusive bus usage |
| 2956 | * @spi: device with which data will be exchanged |
| 2957 | * @message: describes the data transfers |
| 2958 | * Context: can sleep |
| 2959 | * |
| 2960 | * This call may only be used from a context that may sleep. The sleep |
| 2961 | * is non-interruptible, and has no timeout. Low-overhead controller |
| 2962 | * drivers may DMA directly into and out of the message buffers. |
| 2963 | * |
| 2964 | * 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] | 2965 | * 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] | 2966 | * be released by a spi_bus_unlock call when the exclusive access is over. |
| 2967 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2968 | * Return: zero on success, else a negative error code. |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2969 | */ |
| 2970 | int spi_sync_locked(struct spi_device *spi, struct spi_message *message) |
| 2971 | { |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 2972 | return __spi_sync(spi, message); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2973 | } |
| 2974 | EXPORT_SYMBOL_GPL(spi_sync_locked); |
| 2975 | |
| 2976 | /** |
| 2977 | * spi_bus_lock - obtain a lock for exclusive SPI bus usage |
| 2978 | * @master: SPI bus master that should be locked for exclusive bus access |
| 2979 | * Context: can sleep |
| 2980 | * |
| 2981 | * This call may only be used from a context that may sleep. The sleep |
| 2982 | * is non-interruptible, and has no timeout. |
| 2983 | * |
| 2984 | * This call should be used by drivers that require exclusive access to the |
| 2985 | * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the |
| 2986 | * exclusive access is over. Data transfer must be done by spi_sync_locked |
| 2987 | * and spi_async_locked calls when the SPI bus lock is held. |
| 2988 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2989 | * Return: always zero. |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2990 | */ |
| 2991 | int spi_bus_lock(struct spi_master *master) |
| 2992 | { |
| 2993 | unsigned long flags; |
| 2994 | |
| 2995 | mutex_lock(&master->bus_lock_mutex); |
| 2996 | |
| 2997 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
| 2998 | master->bus_lock_flag = 1; |
| 2999 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
| 3000 | |
| 3001 | /* mutex remains locked until spi_bus_unlock is called */ |
| 3002 | |
| 3003 | return 0; |
| 3004 | } |
| 3005 | EXPORT_SYMBOL_GPL(spi_bus_lock); |
| 3006 | |
| 3007 | /** |
| 3008 | * spi_bus_unlock - release the lock for exclusive SPI bus usage |
| 3009 | * @master: SPI bus master that was locked for exclusive bus access |
| 3010 | * Context: can sleep |
| 3011 | * |
| 3012 | * This call may only be used from a context that may sleep. The sleep |
| 3013 | * is non-interruptible, and has no timeout. |
| 3014 | * |
| 3015 | * This call releases an SPI bus lock previously obtained by an spi_bus_lock |
| 3016 | * call. |
| 3017 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 3018 | * Return: always zero. |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3019 | */ |
| 3020 | int spi_bus_unlock(struct spi_master *master) |
| 3021 | { |
| 3022 | master->bus_lock_flag = 0; |
| 3023 | |
| 3024 | mutex_unlock(&master->bus_lock_mutex); |
| 3025 | |
| 3026 | return 0; |
| 3027 | } |
| 3028 | EXPORT_SYMBOL_GPL(spi_bus_unlock); |
| 3029 | |
David Brownell | a9948b6 | 2006-04-02 10:37:40 -0800 | [diff] [blame] | 3030 | /* portable code must never pass more than 32 bytes */ |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 3031 | #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3032 | |
| 3033 | static u8 *buf; |
| 3034 | |
| 3035 | /** |
| 3036 | * spi_write_then_read - SPI synchronous write followed by read |
| 3037 | * @spi: device with which data will be exchanged |
| 3038 | * @txbuf: data to be written (need not be dma-safe) |
| 3039 | * @n_tx: size of txbuf, in bytes |
Jiri Pirko | 2757049 | 2009-06-17 16:26:06 -0700 | [diff] [blame] | 3040 | * @rxbuf: buffer into which data will be read (need not be dma-safe) |
| 3041 | * @n_rx: size of rxbuf, in bytes |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 3042 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3043 | * |
| 3044 | * This performs a half duplex MicroWire style transaction with the |
| 3045 | * device, sending txbuf and then reading rxbuf. The return value |
| 3046 | * is zero for success, else a negative errno status code. |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 3047 | * This call may only be used from a context that may sleep. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3048 | * |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 3049 | * Parameters to this routine are always copied using a small buffer; |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 3050 | * portable code should never use this for more than 32 bytes. |
| 3051 | * Performance-sensitive or bulk transfer code should instead use |
David Brownell | 0c86846 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 3052 | * spi_{async,sync}() calls with dma-safe buffers. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 3053 | * |
| 3054 | * Return: zero on success, else a negative error code. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3055 | */ |
| 3056 | int spi_write_then_read(struct spi_device *spi, |
Mark Brown | 0c4a159 | 2011-05-11 00:09:30 +0200 | [diff] [blame] | 3057 | const void *txbuf, unsigned n_tx, |
| 3058 | void *rxbuf, unsigned n_rx) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3059 | { |
David Brownell | 068f407 | 2007-12-04 23:45:09 -0800 | [diff] [blame] | 3060 | static DEFINE_MUTEX(lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3061 | |
| 3062 | int status; |
| 3063 | struct spi_message message; |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 3064 | struct spi_transfer x[2]; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3065 | u8 *local_buf; |
| 3066 | |
Mark Brown | b3a223e | 2012-12-02 12:54:25 +0900 | [diff] [blame] | 3067 | /* Use preallocated DMA-safe buffer if we can. We can't avoid |
| 3068 | * copying here, (as a pure convenience thing), but we can |
| 3069 | * keep heap costs out of the hot path unless someone else is |
| 3070 | * using the pre-allocated buffer or the transfer is too large. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3071 | */ |
Mark Brown | b3a223e | 2012-12-02 12:54:25 +0900 | [diff] [blame] | 3072 | if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { |
Mark Brown | 2cd94c8 | 2013-01-27 14:35:04 +0800 | [diff] [blame] | 3073 | local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), |
| 3074 | GFP_KERNEL | GFP_DMA); |
Mark Brown | b3a223e | 2012-12-02 12:54:25 +0900 | [diff] [blame] | 3075 | if (!local_buf) |
| 3076 | return -ENOMEM; |
| 3077 | } else { |
| 3078 | local_buf = buf; |
| 3079 | } |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3080 | |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 3081 | spi_message_init(&message); |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 3082 | memset(x, 0, sizeof(x)); |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 3083 | if (n_tx) { |
| 3084 | x[0].len = n_tx; |
| 3085 | spi_message_add_tail(&x[0], &message); |
| 3086 | } |
| 3087 | if (n_rx) { |
| 3088 | x[1].len = n_rx; |
| 3089 | spi_message_add_tail(&x[1], &message); |
| 3090 | } |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 3091 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3092 | memcpy(local_buf, txbuf, n_tx); |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 3093 | x[0].tx_buf = local_buf; |
| 3094 | x[1].rx_buf = local_buf + n_tx; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3095 | |
| 3096 | /* do the i/o */ |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3097 | status = spi_sync(spi, &message); |
Marc Pignat | 9b938b7 | 2007-12-04 23:45:10 -0800 | [diff] [blame] | 3098 | if (status == 0) |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 3099 | memcpy(rxbuf, x[1].rx_buf, n_rx); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3100 | |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 3101 | if (x[0].tx_buf == buf) |
David Brownell | 068f407 | 2007-12-04 23:45:09 -0800 | [diff] [blame] | 3102 | mutex_unlock(&lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3103 | else |
| 3104 | kfree(local_buf); |
| 3105 | |
| 3106 | return status; |
| 3107 | } |
| 3108 | EXPORT_SYMBOL_GPL(spi_write_then_read); |
| 3109 | |
| 3110 | /*-------------------------------------------------------------------------*/ |
| 3111 | |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 3112 | #if IS_ENABLED(CONFIG_OF_DYNAMIC) |
| 3113 | static int __spi_of_device_match(struct device *dev, void *data) |
| 3114 | { |
| 3115 | return dev->of_node == data; |
| 3116 | } |
| 3117 | |
| 3118 | /* must call put_device() when done with returned spi_device device */ |
| 3119 | static struct spi_device *of_find_spi_device_by_node(struct device_node *node) |
| 3120 | { |
| 3121 | struct device *dev = bus_find_device(&spi_bus_type, NULL, node, |
| 3122 | __spi_of_device_match); |
| 3123 | return dev ? to_spi_device(dev) : NULL; |
| 3124 | } |
| 3125 | |
| 3126 | static int __spi_of_master_match(struct device *dev, const void *data) |
| 3127 | { |
| 3128 | return dev->of_node == data; |
| 3129 | } |
| 3130 | |
| 3131 | /* the spi masters are not using spi_bus, so we find it with another way */ |
| 3132 | static struct spi_master *of_find_spi_master_by_node(struct device_node *node) |
| 3133 | { |
| 3134 | struct device *dev; |
| 3135 | |
| 3136 | dev = class_find_device(&spi_master_class, NULL, node, |
| 3137 | __spi_of_master_match); |
| 3138 | if (!dev) |
| 3139 | return NULL; |
| 3140 | |
| 3141 | /* reference got in class_find_device */ |
| 3142 | return container_of(dev, struct spi_master, dev); |
| 3143 | } |
| 3144 | |
| 3145 | static int of_spi_notify(struct notifier_block *nb, unsigned long action, |
| 3146 | void *arg) |
| 3147 | { |
| 3148 | struct of_reconfig_data *rd = arg; |
| 3149 | struct spi_master *master; |
| 3150 | struct spi_device *spi; |
| 3151 | |
| 3152 | switch (of_reconfig_get_state_change(action, arg)) { |
| 3153 | case OF_RECONFIG_CHANGE_ADD: |
| 3154 | master = of_find_spi_master_by_node(rd->dn->parent); |
| 3155 | if (master == NULL) |
| 3156 | return NOTIFY_OK; /* not for us */ |
| 3157 | |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 3158 | if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { |
| 3159 | put_device(&master->dev); |
| 3160 | return NOTIFY_OK; |
| 3161 | } |
| 3162 | |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 3163 | spi = of_register_spi_device(master, rd->dn); |
| 3164 | put_device(&master->dev); |
| 3165 | |
| 3166 | if (IS_ERR(spi)) { |
| 3167 | pr_err("%s: failed to create for '%s'\n", |
| 3168 | __func__, rd->dn->full_name); |
Ralf Ramsauer | e0af98a | 2016-10-17 15:59:56 +0200 | [diff] [blame] | 3169 | of_node_clear_flag(rd->dn, OF_POPULATED); |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 3170 | return notifier_from_errno(PTR_ERR(spi)); |
| 3171 | } |
| 3172 | break; |
| 3173 | |
| 3174 | case OF_RECONFIG_CHANGE_REMOVE: |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 3175 | /* already depopulated? */ |
| 3176 | if (!of_node_check_flag(rd->dn, OF_POPULATED)) |
| 3177 | return NOTIFY_OK; |
| 3178 | |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 3179 | /* find our device by node */ |
| 3180 | spi = of_find_spi_device_by_node(rd->dn); |
| 3181 | if (spi == NULL) |
| 3182 | return NOTIFY_OK; /* no? not meant for us */ |
| 3183 | |
| 3184 | /* unregister takes one ref away */ |
| 3185 | spi_unregister_device(spi); |
| 3186 | |
| 3187 | /* and put the reference of the find */ |
| 3188 | put_device(&spi->dev); |
| 3189 | break; |
| 3190 | } |
| 3191 | |
| 3192 | return NOTIFY_OK; |
| 3193 | } |
| 3194 | |
| 3195 | static struct notifier_block spi_of_notifier = { |
| 3196 | .notifier_call = of_spi_notify, |
| 3197 | }; |
| 3198 | #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ |
| 3199 | extern struct notifier_block spi_of_notifier; |
| 3200 | #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ |
| 3201 | |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 3202 | #if IS_ENABLED(CONFIG_ACPI) |
| 3203 | static int spi_acpi_master_match(struct device *dev, const void *data) |
| 3204 | { |
| 3205 | return ACPI_COMPANION(dev->parent) == data; |
| 3206 | } |
| 3207 | |
| 3208 | static int spi_acpi_device_match(struct device *dev, void *data) |
| 3209 | { |
| 3210 | return ACPI_COMPANION(dev) == data; |
| 3211 | } |
| 3212 | |
| 3213 | static struct spi_master *acpi_spi_find_master_by_adev(struct acpi_device *adev) |
| 3214 | { |
| 3215 | struct device *dev; |
| 3216 | |
| 3217 | dev = class_find_device(&spi_master_class, NULL, adev, |
| 3218 | spi_acpi_master_match); |
| 3219 | if (!dev) |
| 3220 | return NULL; |
| 3221 | |
| 3222 | return container_of(dev, struct spi_master, dev); |
| 3223 | } |
| 3224 | |
| 3225 | static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) |
| 3226 | { |
| 3227 | struct device *dev; |
| 3228 | |
| 3229 | dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match); |
| 3230 | |
| 3231 | return dev ? to_spi_device(dev) : NULL; |
| 3232 | } |
| 3233 | |
| 3234 | static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, |
| 3235 | void *arg) |
| 3236 | { |
| 3237 | struct acpi_device *adev = arg; |
| 3238 | struct spi_master *master; |
| 3239 | struct spi_device *spi; |
| 3240 | |
| 3241 | switch (value) { |
| 3242 | case ACPI_RECONFIG_DEVICE_ADD: |
| 3243 | master = acpi_spi_find_master_by_adev(adev->parent); |
| 3244 | if (!master) |
| 3245 | break; |
| 3246 | |
| 3247 | acpi_register_spi_device(master, adev); |
| 3248 | put_device(&master->dev); |
| 3249 | break; |
| 3250 | case ACPI_RECONFIG_DEVICE_REMOVE: |
| 3251 | if (!acpi_device_enumerated(adev)) |
| 3252 | break; |
| 3253 | |
| 3254 | spi = acpi_spi_find_device_by_adev(adev); |
| 3255 | if (!spi) |
| 3256 | break; |
| 3257 | |
| 3258 | spi_unregister_device(spi); |
| 3259 | put_device(&spi->dev); |
| 3260 | break; |
| 3261 | } |
| 3262 | |
| 3263 | return NOTIFY_OK; |
| 3264 | } |
| 3265 | |
| 3266 | static struct notifier_block spi_acpi_notifier = { |
| 3267 | .notifier_call = acpi_spi_notify, |
| 3268 | }; |
| 3269 | #else |
| 3270 | extern struct notifier_block spi_acpi_notifier; |
| 3271 | #endif |
| 3272 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3273 | static int __init spi_init(void) |
| 3274 | { |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 3275 | int status; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3276 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 3277 | buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 3278 | if (!buf) { |
| 3279 | status = -ENOMEM; |
| 3280 | goto err0; |
| 3281 | } |
| 3282 | |
| 3283 | status = bus_register(&spi_bus_type); |
| 3284 | if (status < 0) |
| 3285 | goto err1; |
| 3286 | |
| 3287 | status = class_register(&spi_master_class); |
| 3288 | if (status < 0) |
| 3289 | goto err2; |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 3290 | |
Fabio Estevam | 5267720 | 2014-11-26 20:13:57 -0200 | [diff] [blame] | 3291 | if (IS_ENABLED(CONFIG_OF_DYNAMIC)) |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 3292 | WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 3293 | if (IS_ENABLED(CONFIG_ACPI)) |
| 3294 | WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 3295 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3296 | return 0; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 3297 | |
| 3298 | err2: |
| 3299 | bus_unregister(&spi_bus_type); |
| 3300 | err1: |
| 3301 | kfree(buf); |
| 3302 | buf = NULL; |
| 3303 | err0: |
| 3304 | return status; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3305 | } |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 3306 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3307 | /* board_info is normally registered in arch_initcall(), |
| 3308 | * but even essential drivers wait till later |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 3309 | * |
| 3310 | * REVISIT only boardinfo really needs static linking. the rest (device and |
| 3311 | * driver registration) _could_ be dynamically linked (modular) ... costs |
| 3312 | * include needing to have boardinfo data structures be much more public. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3313 | */ |
David Brownell | 673c0c0 | 2008-10-15 22:02:46 -0700 | [diff] [blame] | 3314 | postcore_initcall(spi_init); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3315 | |