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