Marco Felsch | b445bfc | 2018-09-25 11:42:28 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mark Brown | 787f488 | 2018-11-29 16:24:37 +0000 | [diff] [blame] | 2 | // SPI init/core code |
| 3 | // |
| 4 | // Copyright (C) 2005 David Brownell |
| 5 | // Copyright (C) 2008 Secret Lab Technologies Ltd. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 6 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/device.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/cache.h> |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 11 | #include <linux/dma-mapping.h> |
| 12 | #include <linux/dmaengine.h> |
Matthias Kaehlcke | 9404082 | 2007-07-17 04:04:16 -0700 | [diff] [blame] | 13 | #include <linux/mutex.h> |
Sinan Akman | 2b7a32f | 2010-10-02 21:28:29 -0600 | [diff] [blame] | 14 | #include <linux/of_device.h> |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 15 | #include <linux/of_irq.h> |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 16 | #include <linux/clk/clk-conf.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 18 | #include <linux/mod_devicetable.h> |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 19 | #include <linux/spi/spi.h> |
Boris Brezillon | b5932f5 | 2018-04-26 18:18:15 +0200 | [diff] [blame] | 20 | #include <linux/spi/spi-mem.h> |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 21 | #include <linux/of_gpio.h> |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 22 | #include <linux/gpio/consumer.h> |
Mark Brown | 3ae22e8 | 2010-12-25 15:32:27 +0100 | [diff] [blame] | 23 | #include <linux/pm_runtime.h> |
Ulf Hansson | f48c767 | 2014-09-29 13:58:47 +0200 | [diff] [blame] | 24 | #include <linux/pm_domain.h> |
Dmitry Torokhov | 826cf17 | 2017-02-28 14:25:18 -0800 | [diff] [blame] | 25 | #include <linux/property.h> |
Paul Gortmaker | 025ed13 | 2011-07-10 12:57:55 -0400 | [diff] [blame] | 26 | #include <linux/export.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 27 | #include <linux/sched/rt.h> |
Ingo Molnar | ae7e81c | 2017-02-01 18:07:51 +0100 | [diff] [blame] | 28 | #include <uapi/linux/sched/types.h> |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 29 | #include <linux/delay.h> |
| 30 | #include <linux/kthread.h> |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 31 | #include <linux/ioport.h> |
| 32 | #include <linux/acpi.h> |
Vignesh R | b1b8153 | 2016-08-17 15:22:36 +0530 | [diff] [blame] | 33 | #include <linux/highmem.h> |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 34 | #include <linux/idr.h> |
Lukas Wunner | 8a2e487 | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 35 | #include <linux/platform_data/x86/apple.h> |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 36 | |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 37 | #define CREATE_TRACE_POINTS |
| 38 | #include <trace/events/spi.h> |
Arnd Bergmann | ca1438d | 2019-03-21 13:42:25 +0100 | [diff] [blame] | 39 | EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start); |
| 40 | EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop); |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 41 | |
Boris Brezillon | 4633696 | 2018-04-22 20:35:14 +0200 | [diff] [blame] | 42 | #include "internals.h" |
| 43 | |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 44 | static DEFINE_IDR(spi_master_idr); |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 45 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 46 | static void spidev_release(struct device *dev) |
| 47 | { |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 48 | struct spi_device *spi = to_spi_device(dev); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 49 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 50 | spi_controller_put(spi->controller); |
Trent Piepho | 5039563 | 2018-09-20 19:18:32 +0000 | [diff] [blame] | 51 | kfree(spi->driver_override); |
Roman Tereshonkov | 07a389f | 2010-04-12 09:56:35 +0000 | [diff] [blame] | 52 | kfree(spi); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static ssize_t |
| 56 | modalias_show(struct device *dev, struct device_attribute *a, char *buf) |
| 57 | { |
| 58 | const struct spi_device *spi = to_spi_device(dev); |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 59 | int len; |
| 60 | |
| 61 | len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); |
| 62 | if (len != -ENODEV) |
| 63 | return len; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 64 | |
Grant Likely | d8e328b | 2012-05-20 00:08:13 -0600 | [diff] [blame] | 65 | return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 66 | } |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 67 | static DEVICE_ATTR_RO(modalias); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 68 | |
Trent Piepho | 5039563 | 2018-09-20 19:18:32 +0000 | [diff] [blame] | 69 | static ssize_t driver_override_store(struct device *dev, |
| 70 | struct device_attribute *a, |
| 71 | const char *buf, size_t count) |
| 72 | { |
| 73 | struct spi_device *spi = to_spi_device(dev); |
| 74 | const char *end = memchr(buf, '\n', count); |
| 75 | const size_t len = end ? end - buf : count; |
| 76 | const char *driver_override, *old; |
| 77 | |
| 78 | /* We need to keep extra room for a newline when displaying value */ |
| 79 | if (len >= (PAGE_SIZE - 1)) |
| 80 | return -EINVAL; |
| 81 | |
| 82 | driver_override = kstrndup(buf, len, GFP_KERNEL); |
| 83 | if (!driver_override) |
| 84 | return -ENOMEM; |
| 85 | |
| 86 | device_lock(dev); |
| 87 | old = spi->driver_override; |
| 88 | if (len) { |
| 89 | spi->driver_override = driver_override; |
| 90 | } else { |
Andy Shevchenko | be73e32 | 2019-10-23 15:16:43 +0300 | [diff] [blame] | 91 | /* Empty string, disable driver override */ |
Trent Piepho | 5039563 | 2018-09-20 19:18:32 +0000 | [diff] [blame] | 92 | spi->driver_override = NULL; |
| 93 | kfree(driver_override); |
| 94 | } |
| 95 | device_unlock(dev); |
| 96 | kfree(old); |
| 97 | |
| 98 | return count; |
| 99 | } |
| 100 | |
| 101 | static ssize_t driver_override_show(struct device *dev, |
| 102 | struct device_attribute *a, char *buf) |
| 103 | { |
| 104 | const struct spi_device *spi = to_spi_device(dev); |
| 105 | ssize_t len; |
| 106 | |
| 107 | device_lock(dev); |
| 108 | len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : ""); |
| 109 | device_unlock(dev); |
| 110 | return len; |
| 111 | } |
| 112 | static DEVICE_ATTR_RW(driver_override); |
| 113 | |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 114 | #define SPI_STATISTICS_ATTRS(field, file) \ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 115 | static ssize_t spi_controller_##field##_show(struct device *dev, \ |
| 116 | struct device_attribute *attr, \ |
| 117 | char *buf) \ |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 118 | { \ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 119 | struct spi_controller *ctlr = container_of(dev, \ |
| 120 | struct spi_controller, dev); \ |
| 121 | return spi_statistics_##field##_show(&ctlr->statistics, buf); \ |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 122 | } \ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 123 | static struct device_attribute dev_attr_spi_controller_##field = { \ |
Geert Uytterhoeven | ad25c92 | 2017-05-04 16:29:56 +0200 | [diff] [blame] | 124 | .attr = { .name = file, .mode = 0444 }, \ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 125 | .show = spi_controller_##field##_show, \ |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 126 | }; \ |
| 127 | static ssize_t spi_device_##field##_show(struct device *dev, \ |
| 128 | struct device_attribute *attr, \ |
| 129 | char *buf) \ |
| 130 | { \ |
Geliang Tang | d1eba93 | 2015-12-23 00:18:41 +0800 | [diff] [blame] | 131 | struct spi_device *spi = to_spi_device(dev); \ |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 132 | return spi_statistics_##field##_show(&spi->statistics, buf); \ |
| 133 | } \ |
| 134 | static struct device_attribute dev_attr_spi_device_##field = { \ |
Geert Uytterhoeven | ad25c92 | 2017-05-04 16:29:56 +0200 | [diff] [blame] | 135 | .attr = { .name = file, .mode = 0444 }, \ |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 136 | .show = spi_device_##field##_show, \ |
| 137 | } |
| 138 | |
| 139 | #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \ |
| 140 | static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ |
| 141 | char *buf) \ |
| 142 | { \ |
| 143 | unsigned long flags; \ |
| 144 | ssize_t len; \ |
| 145 | spin_lock_irqsave(&stat->lock, flags); \ |
| 146 | len = sprintf(buf, format_string, stat->field); \ |
| 147 | spin_unlock_irqrestore(&stat->lock, flags); \ |
| 148 | return len; \ |
| 149 | } \ |
| 150 | SPI_STATISTICS_ATTRS(name, file) |
| 151 | |
| 152 | #define SPI_STATISTICS_SHOW(field, format_string) \ |
| 153 | SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ |
| 154 | field, format_string) |
| 155 | |
| 156 | SPI_STATISTICS_SHOW(messages, "%lu"); |
| 157 | SPI_STATISTICS_SHOW(transfers, "%lu"); |
| 158 | SPI_STATISTICS_SHOW(errors, "%lu"); |
| 159 | SPI_STATISTICS_SHOW(timedout, "%lu"); |
| 160 | |
| 161 | SPI_STATISTICS_SHOW(spi_sync, "%lu"); |
| 162 | SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu"); |
| 163 | SPI_STATISTICS_SHOW(spi_async, "%lu"); |
| 164 | |
| 165 | SPI_STATISTICS_SHOW(bytes, "%llu"); |
| 166 | SPI_STATISTICS_SHOW(bytes_rx, "%llu"); |
| 167 | SPI_STATISTICS_SHOW(bytes_tx, "%llu"); |
| 168 | |
Martin Sperl | 6b7bc06 | 2015-06-22 13:02:04 +0000 | [diff] [blame] | 169 | #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ |
| 170 | SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ |
| 171 | "transfer_bytes_histo_" number, \ |
| 172 | transfer_bytes_histo[index], "%lu") |
| 173 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); |
| 174 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); |
| 175 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); |
| 176 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); |
| 177 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); |
| 178 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); |
| 179 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); |
| 180 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); |
| 181 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); |
| 182 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); |
| 183 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); |
| 184 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); |
| 185 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); |
| 186 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); |
| 187 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); |
| 188 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); |
| 189 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); |
| 190 | |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 191 | SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu"); |
| 192 | |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 193 | static struct attribute *spi_dev_attrs[] = { |
| 194 | &dev_attr_modalias.attr, |
Trent Piepho | 5039563 | 2018-09-20 19:18:32 +0000 | [diff] [blame] | 195 | &dev_attr_driver_override.attr, |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 196 | NULL, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 197 | }; |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 198 | |
| 199 | static const struct attribute_group spi_dev_group = { |
| 200 | .attrs = spi_dev_attrs, |
| 201 | }; |
| 202 | |
| 203 | static struct attribute *spi_device_statistics_attrs[] = { |
| 204 | &dev_attr_spi_device_messages.attr, |
| 205 | &dev_attr_spi_device_transfers.attr, |
| 206 | &dev_attr_spi_device_errors.attr, |
| 207 | &dev_attr_spi_device_timedout.attr, |
| 208 | &dev_attr_spi_device_spi_sync.attr, |
| 209 | &dev_attr_spi_device_spi_sync_immediate.attr, |
| 210 | &dev_attr_spi_device_spi_async.attr, |
| 211 | &dev_attr_spi_device_bytes.attr, |
| 212 | &dev_attr_spi_device_bytes_rx.attr, |
| 213 | &dev_attr_spi_device_bytes_tx.attr, |
Martin Sperl | 6b7bc06 | 2015-06-22 13:02:04 +0000 | [diff] [blame] | 214 | &dev_attr_spi_device_transfer_bytes_histo0.attr, |
| 215 | &dev_attr_spi_device_transfer_bytes_histo1.attr, |
| 216 | &dev_attr_spi_device_transfer_bytes_histo2.attr, |
| 217 | &dev_attr_spi_device_transfer_bytes_histo3.attr, |
| 218 | &dev_attr_spi_device_transfer_bytes_histo4.attr, |
| 219 | &dev_attr_spi_device_transfer_bytes_histo5.attr, |
| 220 | &dev_attr_spi_device_transfer_bytes_histo6.attr, |
| 221 | &dev_attr_spi_device_transfer_bytes_histo7.attr, |
| 222 | &dev_attr_spi_device_transfer_bytes_histo8.attr, |
| 223 | &dev_attr_spi_device_transfer_bytes_histo9.attr, |
| 224 | &dev_attr_spi_device_transfer_bytes_histo10.attr, |
| 225 | &dev_attr_spi_device_transfer_bytes_histo11.attr, |
| 226 | &dev_attr_spi_device_transfer_bytes_histo12.attr, |
| 227 | &dev_attr_spi_device_transfer_bytes_histo13.attr, |
| 228 | &dev_attr_spi_device_transfer_bytes_histo14.attr, |
| 229 | &dev_attr_spi_device_transfer_bytes_histo15.attr, |
| 230 | &dev_attr_spi_device_transfer_bytes_histo16.attr, |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 231 | &dev_attr_spi_device_transfers_split_maxsize.attr, |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 232 | NULL, |
| 233 | }; |
| 234 | |
| 235 | static const struct attribute_group spi_device_statistics_group = { |
| 236 | .name = "statistics", |
| 237 | .attrs = spi_device_statistics_attrs, |
| 238 | }; |
| 239 | |
| 240 | static const struct attribute_group *spi_dev_groups[] = { |
| 241 | &spi_dev_group, |
| 242 | &spi_device_statistics_group, |
| 243 | NULL, |
| 244 | }; |
| 245 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 246 | static struct attribute *spi_controller_statistics_attrs[] = { |
| 247 | &dev_attr_spi_controller_messages.attr, |
| 248 | &dev_attr_spi_controller_transfers.attr, |
| 249 | &dev_attr_spi_controller_errors.attr, |
| 250 | &dev_attr_spi_controller_timedout.attr, |
| 251 | &dev_attr_spi_controller_spi_sync.attr, |
| 252 | &dev_attr_spi_controller_spi_sync_immediate.attr, |
| 253 | &dev_attr_spi_controller_spi_async.attr, |
| 254 | &dev_attr_spi_controller_bytes.attr, |
| 255 | &dev_attr_spi_controller_bytes_rx.attr, |
| 256 | &dev_attr_spi_controller_bytes_tx.attr, |
| 257 | &dev_attr_spi_controller_transfer_bytes_histo0.attr, |
| 258 | &dev_attr_spi_controller_transfer_bytes_histo1.attr, |
| 259 | &dev_attr_spi_controller_transfer_bytes_histo2.attr, |
| 260 | &dev_attr_spi_controller_transfer_bytes_histo3.attr, |
| 261 | &dev_attr_spi_controller_transfer_bytes_histo4.attr, |
| 262 | &dev_attr_spi_controller_transfer_bytes_histo5.attr, |
| 263 | &dev_attr_spi_controller_transfer_bytes_histo6.attr, |
| 264 | &dev_attr_spi_controller_transfer_bytes_histo7.attr, |
| 265 | &dev_attr_spi_controller_transfer_bytes_histo8.attr, |
| 266 | &dev_attr_spi_controller_transfer_bytes_histo9.attr, |
| 267 | &dev_attr_spi_controller_transfer_bytes_histo10.attr, |
| 268 | &dev_attr_spi_controller_transfer_bytes_histo11.attr, |
| 269 | &dev_attr_spi_controller_transfer_bytes_histo12.attr, |
| 270 | &dev_attr_spi_controller_transfer_bytes_histo13.attr, |
| 271 | &dev_attr_spi_controller_transfer_bytes_histo14.attr, |
| 272 | &dev_attr_spi_controller_transfer_bytes_histo15.attr, |
| 273 | &dev_attr_spi_controller_transfer_bytes_histo16.attr, |
| 274 | &dev_attr_spi_controller_transfers_split_maxsize.attr, |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 275 | NULL, |
| 276 | }; |
| 277 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 278 | static const struct attribute_group spi_controller_statistics_group = { |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 279 | .name = "statistics", |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 280 | .attrs = spi_controller_statistics_attrs, |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | static const struct attribute_group *spi_master_groups[] = { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 284 | &spi_controller_statistics_group, |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 285 | NULL, |
| 286 | }; |
| 287 | |
| 288 | void spi_statistics_add_transfer_stats(struct spi_statistics *stats, |
| 289 | struct spi_transfer *xfer, |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 290 | struct spi_controller *ctlr) |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 291 | { |
| 292 | unsigned long flags; |
Martin Sperl | 6b7bc06 | 2015-06-22 13:02:04 +0000 | [diff] [blame] | 293 | int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; |
| 294 | |
| 295 | if (l2len < 0) |
| 296 | l2len = 0; |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 297 | |
| 298 | spin_lock_irqsave(&stats->lock, flags); |
| 299 | |
| 300 | stats->transfers++; |
Martin Sperl | 6b7bc06 | 2015-06-22 13:02:04 +0000 | [diff] [blame] | 301 | stats->transfer_bytes_histo[l2len]++; |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 302 | |
| 303 | stats->bytes += xfer->len; |
| 304 | if ((xfer->tx_buf) && |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 305 | (xfer->tx_buf != ctlr->dummy_tx)) |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 306 | stats->bytes_tx += xfer->len; |
| 307 | if ((xfer->rx_buf) && |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 308 | (xfer->rx_buf != ctlr->dummy_rx)) |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 309 | stats->bytes_rx += xfer->len; |
| 310 | |
| 311 | spin_unlock_irqrestore(&stats->lock, flags); |
| 312 | } |
| 313 | EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 314 | |
| 315 | /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, |
| 316 | * and the sysfs version makes coldplug work too. |
| 317 | */ |
| 318 | |
Anton Vorontsov | 75368bf | 2009-09-22 16:46:04 -0700 | [diff] [blame] | 319 | static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, |
| 320 | const struct spi_device *sdev) |
| 321 | { |
| 322 | while (id->name[0]) { |
| 323 | if (!strcmp(sdev->modalias, id->name)) |
| 324 | return id; |
| 325 | id++; |
| 326 | } |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) |
| 331 | { |
| 332 | const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); |
| 333 | |
| 334 | return spi_match_id(sdrv->id_table, sdev); |
| 335 | } |
| 336 | EXPORT_SYMBOL_GPL(spi_get_device_id); |
| 337 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 338 | static int spi_match_device(struct device *dev, struct device_driver *drv) |
| 339 | { |
| 340 | const struct spi_device *spi = to_spi_device(dev); |
Anton Vorontsov | 75368bf | 2009-09-22 16:46:04 -0700 | [diff] [blame] | 341 | const struct spi_driver *sdrv = to_spi_driver(drv); |
| 342 | |
Trent Piepho | 5039563 | 2018-09-20 19:18:32 +0000 | [diff] [blame] | 343 | /* Check override first, and if set, only use the named driver */ |
| 344 | if (spi->driver_override) |
| 345 | return strcmp(spi->driver_override, drv->name) == 0; |
| 346 | |
Sinan Akman | 2b7a32f | 2010-10-02 21:28:29 -0600 | [diff] [blame] | 347 | /* Attempt an OF style match */ |
| 348 | if (of_driver_match_device(dev, drv)) |
| 349 | return 1; |
| 350 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 351 | /* Then try ACPI */ |
| 352 | if (acpi_driver_match_device(dev, drv)) |
| 353 | return 1; |
| 354 | |
Anton Vorontsov | 75368bf | 2009-09-22 16:46:04 -0700 | [diff] [blame] | 355 | if (sdrv->id_table) |
| 356 | return !!spi_match_id(sdrv->id_table, spi); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 357 | |
Kay Sievers | 35f74fc | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 358 | return strcmp(spi->modalias, drv->name) == 0; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 361 | static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 362 | { |
| 363 | const struct spi_device *spi = to_spi_device(dev); |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 364 | int rc; |
| 365 | |
| 366 | rc = acpi_device_uevent_modalias(dev, env); |
| 367 | if (rc != -ENODEV) |
| 368 | return rc; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 369 | |
Andy Shevchenko | 2856670 | 2017-07-26 16:14:00 +0300 | [diff] [blame] | 370 | 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] | 371 | } |
| 372 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 373 | struct bus_type spi_bus_type = { |
| 374 | .name = "spi", |
Greg Kroah-Hartman | aa7da56 | 2013-10-07 18:27:38 -0700 | [diff] [blame] | 375 | .dev_groups = spi_dev_groups, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 376 | .match = spi_match_device, |
| 377 | .uevent = spi_uevent, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 378 | }; |
| 379 | EXPORT_SYMBOL_GPL(spi_bus_type); |
| 380 | |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 381 | |
| 382 | static int spi_drv_probe(struct device *dev) |
| 383 | { |
| 384 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
Jon Hunter | 44af792 | 2015-10-09 15:45:55 +0100 | [diff] [blame] | 385 | struct spi_device *spi = to_spi_device(dev); |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 386 | int ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 387 | |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 388 | ret = of_clk_set_defaults(dev->of_node, false); |
| 389 | if (ret) |
| 390 | return ret; |
| 391 | |
Jon Hunter | 44af792 | 2015-10-09 15:45:55 +0100 | [diff] [blame] | 392 | if (dev->of_node) { |
| 393 | spi->irq = of_irq_get(dev->of_node, 0); |
| 394 | if (spi->irq == -EPROBE_DEFER) |
| 395 | return -EPROBE_DEFER; |
| 396 | if (spi->irq < 0) |
| 397 | spi->irq = 0; |
| 398 | } |
| 399 | |
Ulf Hansson | 676e7c2 | 2014-09-19 20:27:41 +0200 | [diff] [blame] | 400 | ret = dev_pm_domain_attach(dev, true); |
Ulf Hansson | 71f277a | 2018-04-26 10:53:10 +0200 | [diff] [blame] | 401 | if (ret) |
| 402 | return ret; |
| 403 | |
Uwe Kleine-König | ddbed6d | 2020-11-19 17:16:02 +0100 | [diff] [blame] | 404 | if (sdrv->probe) { |
| 405 | ret = sdrv->probe(spi); |
| 406 | if (ret) |
| 407 | dev_pm_domain_detach(dev, true); |
| 408 | } |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 409 | |
| 410 | return ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static int spi_drv_remove(struct device *dev) |
| 414 | { |
| 415 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
Uwe Kleine-König | ddbed6d | 2020-11-19 17:16:02 +0100 | [diff] [blame] | 416 | int ret = 0; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 417 | |
Uwe Kleine-König | ddbed6d | 2020-11-19 17:16:02 +0100 | [diff] [blame] | 418 | if (sdrv->remove) |
| 419 | ret = sdrv->remove(to_spi_device(dev)); |
Ulf Hansson | 676e7c2 | 2014-09-19 20:27:41 +0200 | [diff] [blame] | 420 | dev_pm_domain_detach(dev, true); |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 421 | |
| 422 | return ret; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static void spi_drv_shutdown(struct device *dev) |
| 426 | { |
| 427 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); |
| 428 | |
| 429 | sdrv->shutdown(to_spi_device(dev)); |
| 430 | } |
| 431 | |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 432 | /** |
Andrew F. Davis | ca5d248 | 2015-10-23 08:59:10 -0500 | [diff] [blame] | 433 | * __spi_register_driver - register a SPI driver |
Thierry Reding | 88c9321 | 2015-11-10 13:03:04 +0100 | [diff] [blame] | 434 | * @owner: owner module of the driver to register |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 435 | * @sdrv: the driver to register |
| 436 | * Context: can sleep |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 437 | * |
| 438 | * Return: zero on success, else a negative error code. |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 439 | */ |
Andrew F. Davis | ca5d248 | 2015-10-23 08:59:10 -0500 | [diff] [blame] | 440 | int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 441 | { |
Andrew F. Davis | ca5d248 | 2015-10-23 08:59:10 -0500 | [diff] [blame] | 442 | sdrv->driver.owner = owner; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 443 | sdrv->driver.bus = &spi_bus_type; |
Uwe Kleine-König | ddbed6d | 2020-11-19 17:16:02 +0100 | [diff] [blame] | 444 | sdrv->driver.probe = spi_drv_probe; |
| 445 | sdrv->driver.remove = spi_drv_remove; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 446 | if (sdrv->shutdown) |
| 447 | sdrv->driver.shutdown = spi_drv_shutdown; |
| 448 | return driver_register(&sdrv->driver); |
| 449 | } |
Andrew F. Davis | ca5d248 | 2015-10-23 08:59:10 -0500 | [diff] [blame] | 450 | EXPORT_SYMBOL_GPL(__spi_register_driver); |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 451 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 452 | /*-------------------------------------------------------------------------*/ |
| 453 | |
| 454 | /* SPI devices should normally not be created by SPI device drivers; that |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 455 | * would make them board-specific. Similarly with SPI controller drivers. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 456 | * Device registration normally goes into like arch/.../mach.../board-YYY.c |
| 457 | * with other readonly (flashable) information about mainboard devices. |
| 458 | */ |
| 459 | |
| 460 | struct boardinfo { |
| 461 | struct list_head list; |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 462 | struct spi_board_info board_info; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | static LIST_HEAD(board_list); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 466 | static LIST_HEAD(spi_controller_list); |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 467 | |
| 468 | /* |
Andy Shevchenko | be73e32 | 2019-10-23 15:16:43 +0300 | [diff] [blame] | 469 | * Used to protect add/del operation for board_info list and |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 470 | * spi_controller list, and their matching process |
Suniel Mahesh | 9a9a047 | 2017-08-17 18:18:22 +0530 | [diff] [blame] | 471 | * also used to protect object of type struct idr |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 472 | */ |
Matthias Kaehlcke | 9404082 | 2007-07-17 04:04:16 -0700 | [diff] [blame] | 473 | static DEFINE_MUTEX(board_lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 474 | |
Lukas Wunner | ddf75be | 2020-08-03 13:09:01 +0200 | [diff] [blame] | 475 | /* |
| 476 | * Prevents addition of devices with same chip select and |
| 477 | * addition of devices below an unregistering controller. |
| 478 | */ |
| 479 | static DEFINE_MUTEX(spi_add_lock); |
| 480 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 481 | /** |
| 482 | * spi_alloc_device - Allocate a new SPI device |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 483 | * @ctlr: Controller to which device is connected |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 484 | * Context: can sleep |
| 485 | * |
| 486 | * Allows a driver to allocate and initialize a spi_device without |
| 487 | * registering it immediately. This allows a driver to directly |
| 488 | * fill the spi_device with device parameters before calling |
| 489 | * spi_add_device() on it. |
| 490 | * |
| 491 | * Caller is responsible to call spi_add_device() on the returned |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 492 | * 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] | 493 | * needs to discard the spi_device without adding it, then it should |
| 494 | * call spi_dev_put() on it. |
| 495 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 496 | * Return: a pointer to the new device, or NULL. |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 497 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 498 | struct spi_device *spi_alloc_device(struct spi_controller *ctlr) |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 499 | { |
| 500 | struct spi_device *spi; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 501 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 502 | if (!spi_controller_get(ctlr)) |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 503 | return NULL; |
| 504 | |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 505 | spi = kzalloc(sizeof(*spi), GFP_KERNEL); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 506 | if (!spi) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 507 | spi_controller_put(ctlr); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 508 | return NULL; |
| 509 | } |
| 510 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 511 | spi->master = spi->controller = ctlr; |
| 512 | spi->dev.parent = &ctlr->dev; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 513 | spi->dev.bus = &spi_bus_type; |
| 514 | spi->dev.release = spidev_release; |
Andreas Larsson | 446411e | 2013-02-13 14:20:25 +0100 | [diff] [blame] | 515 | spi->cs_gpio = -ENOENT; |
John Garry | ea23578 | 2020-02-28 23:18:49 +0800 | [diff] [blame] | 516 | spi->mode = ctlr->buswidth_override_bits; |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 517 | |
| 518 | spin_lock_init(&spi->statistics.lock); |
| 519 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 520 | device_initialize(&spi->dev); |
| 521 | return spi; |
| 522 | } |
| 523 | EXPORT_SYMBOL_GPL(spi_alloc_device); |
| 524 | |
Jarkko Nikula | e13ac47 | 2013-11-14 14:03:53 +0200 | [diff] [blame] | 525 | static void spi_dev_set_name(struct spi_device *spi) |
| 526 | { |
| 527 | struct acpi_device *adev = ACPI_COMPANION(&spi->dev); |
| 528 | |
| 529 | if (adev) { |
| 530 | dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); |
| 531 | return; |
| 532 | } |
| 533 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 534 | dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), |
Jarkko Nikula | e13ac47 | 2013-11-14 14:03:53 +0200 | [diff] [blame] | 535 | spi->chip_select); |
| 536 | } |
| 537 | |
Mika Westerberg | b6fb8d3 | 2014-01-09 15:23:55 +0200 | [diff] [blame] | 538 | static int spi_dev_check(struct device *dev, void *data) |
| 539 | { |
| 540 | struct spi_device *spi = to_spi_device(dev); |
| 541 | struct spi_device *new_spi = data; |
| 542 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 543 | if (spi->controller == new_spi->controller && |
Mika Westerberg | b6fb8d3 | 2014-01-09 15:23:55 +0200 | [diff] [blame] | 544 | spi->chip_select == new_spi->chip_select) |
| 545 | return -EBUSY; |
| 546 | return 0; |
| 547 | } |
| 548 | |
Saravana Kannan | 4b8b7bc | 2021-04-26 16:56:38 -0700 | [diff] [blame] | 549 | static void spi_cleanup(struct spi_device *spi) |
| 550 | { |
| 551 | if (spi->controller->cleanup) |
| 552 | spi->controller->cleanup(spi); |
| 553 | } |
| 554 | |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 555 | /** |
| 556 | * spi_add_device - Add spi_device allocated with spi_alloc_device |
| 557 | * @spi: spi_device to register |
| 558 | * |
| 559 | * Companion function to spi_alloc_device. Devices allocated with |
| 560 | * spi_alloc_device can be added onto the spi bus with this function. |
| 561 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 562 | * Return: 0 on success; negative errno on failure |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 563 | */ |
| 564 | int spi_add_device(struct spi_device *spi) |
| 565 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 566 | struct spi_controller *ctlr = spi->controller; |
| 567 | struct device *dev = ctlr->dev.parent; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 568 | int status; |
| 569 | |
| 570 | /* Chipselects are numbered 0..max; validate. */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 571 | if (spi->chip_select >= ctlr->num_chipselect) { |
| 572 | dev_err(dev, "cs%d >= max %d\n", spi->chip_select, |
| 573 | ctlr->num_chipselect); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 574 | return -EINVAL; |
| 575 | } |
| 576 | |
| 577 | /* Set the bus ID string */ |
Jarkko Nikula | e13ac47 | 2013-11-14 14:03:53 +0200 | [diff] [blame] | 578 | spi_dev_set_name(spi); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 579 | |
| 580 | /* We need to make sure there's no other device with this |
| 581 | * chipselect **BEFORE** we call setup(), else we'll trash |
| 582 | * its configuration. Lock against concurrent add() calls. |
| 583 | */ |
| 584 | mutex_lock(&spi_add_lock); |
| 585 | |
Mika Westerberg | b6fb8d3 | 2014-01-09 15:23:55 +0200 | [diff] [blame] | 586 | status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); |
| 587 | if (status) { |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 588 | dev_err(dev, "chipselect %d already in use\n", |
| 589 | spi->chip_select); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 590 | goto done; |
| 591 | } |
| 592 | |
Lukas Wunner | ddf75be | 2020-08-03 13:09:01 +0200 | [diff] [blame] | 593 | /* Controller may unregister concurrently */ |
| 594 | if (IS_ENABLED(CONFIG_SPI_DYNAMIC) && |
| 595 | !device_is_registered(&ctlr->dev)) { |
| 596 | status = -ENODEV; |
| 597 | goto done; |
| 598 | } |
| 599 | |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 600 | /* Descriptors take precedence */ |
| 601 | if (ctlr->cs_gpiods) |
| 602 | spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; |
| 603 | else if (ctlr->cs_gpios) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 604 | spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 605 | |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 606 | /* Drivers may modify this initial i/o setup, but will |
| 607 | * normally rely on the device being setup. Devices |
| 608 | * using SPI_CS_HIGH can't coexist well otherwise... |
| 609 | */ |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 610 | status = spi_setup(spi); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 611 | if (status < 0) { |
Linus Walleij | eb288a1 | 2010-10-21 21:06:44 +0200 | [diff] [blame] | 612 | dev_err(dev, "can't setup %s, status %d\n", |
| 613 | dev_name(&spi->dev), status); |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 614 | goto done; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 615 | } |
| 616 | |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 617 | /* Device may be bound to an active driver when this returns */ |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 618 | status = device_add(&spi->dev); |
Saravana Kannan | 4b8b7bc | 2021-04-26 16:56:38 -0700 | [diff] [blame] | 619 | if (status < 0) { |
Linus Walleij | eb288a1 | 2010-10-21 21:06:44 +0200 | [diff] [blame] | 620 | dev_err(dev, "can't add %s, status %d\n", |
| 621 | dev_name(&spi->dev), status); |
Saravana Kannan | 4b8b7bc | 2021-04-26 16:56:38 -0700 | [diff] [blame] | 622 | spi_cleanup(spi); |
| 623 | } else { |
Kay Sievers | 35f74fc | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 624 | dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); |
Saravana Kannan | 4b8b7bc | 2021-04-26 16:56:38 -0700 | [diff] [blame] | 625 | } |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 626 | |
David Brownell | e48880e | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 627 | done: |
| 628 | mutex_unlock(&spi_add_lock); |
| 629 | return status; |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 630 | } |
| 631 | EXPORT_SYMBOL_GPL(spi_add_device); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 632 | |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 633 | /** |
| 634 | * spi_new_device - instantiate one new SPI device |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 635 | * @ctlr: Controller to which device is connected |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 636 | * @chip: Describes the SPI device |
| 637 | * Context: can sleep |
| 638 | * |
| 639 | * On typical mainboards, this is purely internal; and it's not needed |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 640 | * after board init creates the hard-wired devices. Some development |
| 641 | * platforms may not be able to use spi_register_board_info though, and |
| 642 | * this is exported so that for example a USB or parport based adapter |
| 643 | * driver could add devices (which it would learn about out-of-band). |
David Brownell | 082c8cb | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 644 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 645 | * Return: the new device, or NULL. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 646 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 647 | struct spi_device *spi_new_device(struct spi_controller *ctlr, |
Adrian Bunk | e9d5a46 | 2007-03-26 21:32:23 -0800 | [diff] [blame] | 648 | struct spi_board_info *chip) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 649 | { |
| 650 | struct spi_device *proxy; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 651 | int status; |
| 652 | |
David Brownell | 082c8cb | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 653 | /* NOTE: caller did any chip->bus_num checks necessary. |
| 654 | * |
| 655 | * Also, unless we change the return value convention to use |
| 656 | * error-or-pointer (not NULL-or-pointer), troubleshootability |
| 657 | * suggests syslogged diagnostics are best here (ugh). |
| 658 | */ |
| 659 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 660 | proxy = spi_alloc_device(ctlr); |
Grant Likely | dc87c98 | 2008-05-15 16:50:22 -0600 | [diff] [blame] | 661 | if (!proxy) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 662 | return NULL; |
| 663 | |
Grant Likely | 102eb97 | 2008-07-23 21:29:55 -0700 | [diff] [blame] | 664 | WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); |
| 665 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 666 | proxy->chip_select = chip->chip_select; |
| 667 | proxy->max_speed_hz = chip->max_speed_hz; |
David Brownell | 980a01c | 2006-06-28 07:47:15 -0700 | [diff] [blame] | 668 | proxy->mode = chip->mode; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 669 | proxy->irq = chip->irq; |
Grant Likely | 102eb97 | 2008-07-23 21:29:55 -0700 | [diff] [blame] | 670 | strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 671 | proxy->dev.platform_data = (void *) chip->platform_data; |
| 672 | proxy->controller_data = chip->controller_data; |
| 673 | proxy->controller_state = NULL; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 674 | |
Dmitry Torokhov | 826cf17 | 2017-02-28 14:25:18 -0800 | [diff] [blame] | 675 | if (chip->properties) { |
| 676 | status = device_add_properties(&proxy->dev, chip->properties); |
| 677 | if (status) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 678 | dev_err(&ctlr->dev, |
Dmitry Torokhov | 826cf17 | 2017-02-28 14:25:18 -0800 | [diff] [blame] | 679 | "failed to add properties to '%s': %d\n", |
| 680 | chip->modalias, status); |
| 681 | goto err_dev_put; |
| 682 | } |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 683 | } |
| 684 | |
Dmitry Torokhov | 826cf17 | 2017-02-28 14:25:18 -0800 | [diff] [blame] | 685 | status = spi_add_device(proxy); |
| 686 | if (status < 0) |
| 687 | goto err_remove_props; |
| 688 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 689 | return proxy; |
Dmitry Torokhov | 826cf17 | 2017-02-28 14:25:18 -0800 | [diff] [blame] | 690 | |
| 691 | err_remove_props: |
| 692 | if (chip->properties) |
| 693 | device_remove_properties(&proxy->dev); |
| 694 | err_dev_put: |
| 695 | spi_dev_put(proxy); |
| 696 | return NULL; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 697 | } |
| 698 | EXPORT_SYMBOL_GPL(spi_new_device); |
| 699 | |
Geert Uytterhoeven | 3b1884c | 2015-11-30 15:28:06 +0100 | [diff] [blame] | 700 | /** |
| 701 | * spi_unregister_device - unregister a single SPI device |
| 702 | * @spi: spi_device to unregister |
| 703 | * |
| 704 | * Start making the passed SPI device vanish. Normally this would be handled |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 705 | * by spi_unregister_controller(). |
Geert Uytterhoeven | 3b1884c | 2015-11-30 15:28:06 +0100 | [diff] [blame] | 706 | */ |
| 707 | void spi_unregister_device(struct spi_device *spi) |
| 708 | { |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 709 | if (!spi) |
| 710 | return; |
| 711 | |
Johan Hovold | 8324147 | 2017-01-30 17:47:05 +0100 | [diff] [blame] | 712 | if (spi->dev.of_node) { |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 713 | of_node_clear_flag(spi->dev.of_node, OF_POPULATED); |
Johan Hovold | 8324147 | 2017-01-30 17:47:05 +0100 | [diff] [blame] | 714 | of_node_put(spi->dev.of_node); |
| 715 | } |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 716 | if (ACPI_COMPANION(&spi->dev)) |
| 717 | acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); |
Saravana Kannan | 0c4d4de | 2021-05-05 09:47:34 -0700 | [diff] [blame] | 718 | device_del(&spi->dev); |
| 719 | spi_cleanup(spi); |
| 720 | put_device(&spi->dev); |
Geert Uytterhoeven | 3b1884c | 2015-11-30 15:28:06 +0100 | [diff] [blame] | 721 | } |
| 722 | EXPORT_SYMBOL_GPL(spi_unregister_device); |
| 723 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 724 | static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, |
| 725 | struct spi_board_info *bi) |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 726 | { |
| 727 | struct spi_device *dev; |
| 728 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 729 | if (ctlr->bus_num != bi->bus_num) |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 730 | return; |
| 731 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 732 | dev = spi_new_device(ctlr, bi); |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 733 | if (!dev) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 734 | 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] | 735 | bi->modalias); |
| 736 | } |
| 737 | |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 738 | /** |
| 739 | * spi_register_board_info - register SPI devices for a given board |
| 740 | * @info: array of chip descriptors |
| 741 | * @n: how many descriptors are provided |
| 742 | * Context: can sleep |
| 743 | * |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 744 | * Board-specific early init code calls this (probably during arch_initcall) |
| 745 | * with segments of the SPI device table. Any device nodes are created later, |
| 746 | * after the relevant parent SPI controller (bus_num) is defined. We keep |
| 747 | * this table of devices forever, so that reloading a controller driver will |
| 748 | * not make Linux forget about these hard-wired devices. |
| 749 | * |
| 750 | * Other code can also call this, e.g. a particular add-on board might provide |
| 751 | * SPI devices through its expansion connector, so code initializing that board |
| 752 | * would naturally declare its SPI devices. |
| 753 | * |
| 754 | * The board info passed can safely be __initdata ... but be careful of |
| 755 | * any embedded pointers (platform_data, etc), they're copied as-is. |
Dmitry Torokhov | 826cf17 | 2017-02-28 14:25:18 -0800 | [diff] [blame] | 756 | * Device properties are deep-copied though. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 757 | * |
| 758 | * Return: zero on success, else a negative error code. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 759 | */ |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 760 | 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] | 761 | { |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 762 | struct boardinfo *bi; |
| 763 | int i; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 764 | |
Xiubo Li | c7908a3 | 2014-09-24 14:30:29 +0800 | [diff] [blame] | 765 | if (!n) |
Dmitry Torokhov | f974cf5 | 2017-02-28 14:25:19 -0800 | [diff] [blame] | 766 | return 0; |
Xiubo Li | c7908a3 | 2014-09-24 14:30:29 +0800 | [diff] [blame] | 767 | |
Markus Elfring | f9bdb7f | 2017-01-13 12:28:04 +0100 | [diff] [blame] | 768 | bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 769 | if (!bi) |
| 770 | return -ENOMEM; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 771 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 772 | for (i = 0; i < n; i++, bi++, info++) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 773 | struct spi_controller *ctlr; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 774 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 775 | memcpy(&bi->board_info, info, sizeof(*info)); |
Dmitry Torokhov | 826cf17 | 2017-02-28 14:25:18 -0800 | [diff] [blame] | 776 | if (info->properties) { |
| 777 | bi->board_info.properties = |
| 778 | property_entries_dup(info->properties); |
| 779 | if (IS_ERR(bi->board_info.properties)) |
| 780 | return PTR_ERR(bi->board_info.properties); |
| 781 | } |
| 782 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 783 | mutex_lock(&board_lock); |
| 784 | list_add_tail(&bi->list, &board_list); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 785 | list_for_each_entry(ctlr, &spi_controller_list, list) |
| 786 | spi_match_controller_to_boardinfo(ctlr, |
| 787 | &bi->board_info); |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 788 | mutex_unlock(&board_lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 789 | } |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 790 | |
| 791 | return 0; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /*-------------------------------------------------------------------------*/ |
| 795 | |
David Bauer | c5aa9ea | 2021-04-16 21:59:56 +0200 | [diff] [blame] | 796 | static void spi_set_cs(struct spi_device *spi, bool enable, bool force) |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 797 | { |
Alexandru Ardelean | 25093bd | 2019-09-26 13:51:43 +0300 | [diff] [blame] | 798 | bool enable1 = enable; |
| 799 | |
Douglas Anderson | d40f0b6 | 2020-06-29 16:41:06 -0700 | [diff] [blame] | 800 | /* |
| 801 | * Avoid calling into the driver (or doing delays) if the chip select |
| 802 | * isn't actually changing from the last time this was called. |
| 803 | */ |
David Bauer | c5aa9ea | 2021-04-16 21:59:56 +0200 | [diff] [blame] | 804 | if (!force && (spi->controller->last_cs_enable == enable) && |
Douglas Anderson | d40f0b6 | 2020-06-29 16:41:06 -0700 | [diff] [blame] | 805 | (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) |
| 806 | return; |
| 807 | |
| 808 | spi->controller->last_cs_enable = enable; |
| 809 | spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; |
| 810 | |
Alexandru Ardelean | 25093bd | 2019-09-26 13:51:43 +0300 | [diff] [blame] | 811 | if (!spi->controller->set_cs_timing) { |
| 812 | if (enable1) |
| 813 | spi_delay_exec(&spi->controller->cs_setup, NULL); |
| 814 | else |
| 815 | spi_delay_exec(&spi->controller->cs_hold, NULL); |
| 816 | } |
| 817 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 818 | if (spi->mode & SPI_CS_HIGH) |
| 819 | enable = !enable; |
| 820 | |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 821 | if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 822 | if (!(spi->mode & SPI_NO_CS)) { |
Andy Shevchenko | f5da082 | 2021-05-11 17:09:12 +0300 | [diff] [blame] | 823 | if (spi->cs_gpiod) { |
| 824 | /* |
| 825 | * Historically ACPI has no means of the GPIO polarity and |
| 826 | * thus the SPISerialBus() resource defines it on the per-chip |
| 827 | * basis. In order to avoid a chain of negations, the GPIO |
| 828 | * polarity is considered being Active High. Even for the cases |
| 829 | * when _DSD() is involved (in the updated versions of ACPI) |
| 830 | * the GPIO CS polarity must be defined Active High to avoid |
| 831 | * ambiguity. That's why we use enable, that takes SPI_CS_HIGH |
| 832 | * into account. |
| 833 | */ |
| 834 | if (has_acpi_companion(&spi->dev)) |
| 835 | gpiod_set_value_cansleep(spi->cs_gpiod, !enable); |
| 836 | else |
| 837 | /* Polarity handled by GPIO library */ |
| 838 | gpiod_set_value_cansleep(spi->cs_gpiod, enable1); |
| 839 | } else { |
Sven Van Asbroeck | 766c6b6 | 2020-11-06 10:07:06 -0500 | [diff] [blame] | 840 | /* |
| 841 | * invert the enable line, as active low is |
| 842 | * default for SPI. |
| 843 | */ |
Felix Fietkau | 28f7604 | 2019-02-10 00:38:25 +0100 | [diff] [blame] | 844 | gpio_set_value_cansleep(spi->cs_gpio, !enable); |
Andy Shevchenko | f5da082 | 2021-05-11 17:09:12 +0300 | [diff] [blame] | 845 | } |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 846 | } |
Thor Thayer | 8eee6b9 | 2016-10-10 09:25:24 -0500 | [diff] [blame] | 847 | /* Some SPI masters need both GPIO CS & slave_select */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 848 | if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && |
| 849 | spi->controller->set_cs) |
| 850 | spi->controller->set_cs(spi, !enable); |
| 851 | } else if (spi->controller->set_cs) { |
| 852 | spi->controller->set_cs(spi, !enable); |
Thor Thayer | 8eee6b9 | 2016-10-10 09:25:24 -0500 | [diff] [blame] | 853 | } |
Alexandru Ardelean | 25093bd | 2019-09-26 13:51:43 +0300 | [diff] [blame] | 854 | |
| 855 | if (!spi->controller->set_cs_timing) { |
| 856 | if (!enable1) |
| 857 | spi_delay_exec(&spi->controller->cs_inactive, NULL); |
| 858 | } |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 859 | } |
| 860 | |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 861 | #ifdef CONFIG_HAS_DMA |
Boris Brezillon | 4633696 | 2018-04-22 20:35:14 +0200 | [diff] [blame] | 862 | int spi_map_buf(struct spi_controller *ctlr, struct device *dev, |
| 863 | struct sg_table *sgt, void *buf, size_t len, |
| 864 | enum dma_data_direction dir) |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 865 | { |
| 866 | const bool vmalloced_buf = is_vmalloc_addr(buf); |
Andy Shevchenko | df88e91 | 2016-03-09 11:20:00 +0200 | [diff] [blame] | 867 | unsigned int max_seg_size = dma_get_max_seg_size(dev); |
Vignesh R | b1b8153 | 2016-08-17 15:22:36 +0530 | [diff] [blame] | 868 | #ifdef CONFIG_HIGHMEM |
| 869 | const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && |
| 870 | (unsigned long)buf < (PKMAP_BASE + |
| 871 | (LAST_PKMAP * PAGE_SIZE))); |
| 872 | #else |
| 873 | const bool kmap_buf = false; |
| 874 | #endif |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 875 | int desc_len; |
| 876 | int sgs; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 877 | struct page *vm_page; |
Juan Gutierrez | 8dd4a01 | 2016-11-21 16:50:03 -0600 | [diff] [blame] | 878 | struct scatterlist *sg; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 879 | void *sg_buf; |
| 880 | size_t min; |
| 881 | int i, ret; |
| 882 | |
Vignesh R | b1b8153 | 2016-08-17 15:22:36 +0530 | [diff] [blame] | 883 | if (vmalloced_buf || kmap_buf) { |
Biju Das | 21680aa | 2022-03-16 17:53:17 +0000 | [diff] [blame] | 884 | desc_len = min_t(unsigned long, max_seg_size, PAGE_SIZE); |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 885 | sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); |
Vignesh R | 0569a88 | 2016-04-25 15:14:00 +0530 | [diff] [blame] | 886 | } else if (virt_addr_valid(buf)) { |
Biju Das | 21680aa | 2022-03-16 17:53:17 +0000 | [diff] [blame] | 887 | desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len); |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 888 | sgs = DIV_ROUND_UP(len, desc_len); |
Vignesh R | 0569a88 | 2016-04-25 15:14:00 +0530 | [diff] [blame] | 889 | } else { |
| 890 | return -EINVAL; |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 891 | } |
| 892 | |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 893 | ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); |
| 894 | if (ret != 0) |
| 895 | return ret; |
| 896 | |
Juan Gutierrez | 8dd4a01 | 2016-11-21 16:50:03 -0600 | [diff] [blame] | 897 | sg = &sgt->sgl[0]; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 898 | for (i = 0; i < sgs; i++) { |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 899 | |
Vignesh R | b1b8153 | 2016-08-17 15:22:36 +0530 | [diff] [blame] | 900 | if (vmalloced_buf || kmap_buf) { |
Maxime Chevallier | ce99319 | 2018-03-02 15:55:09 +0100 | [diff] [blame] | 901 | /* |
| 902 | * Next scatterlist entry size is the minimum between |
| 903 | * the desc_len and the remaining buffer length that |
| 904 | * fits in a page. |
| 905 | */ |
| 906 | min = min_t(size_t, desc_len, |
| 907 | min_t(size_t, len, |
| 908 | PAGE_SIZE - offset_in_page(buf))); |
Vignesh R | b1b8153 | 2016-08-17 15:22:36 +0530 | [diff] [blame] | 909 | if (vmalloced_buf) |
| 910 | vm_page = vmalloc_to_page(buf); |
| 911 | else |
| 912 | vm_page = kmap_to_page(buf); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 913 | if (!vm_page) { |
| 914 | sg_free_table(sgt); |
| 915 | return -ENOMEM; |
| 916 | } |
Juan Gutierrez | 8dd4a01 | 2016-11-21 16:50:03 -0600 | [diff] [blame] | 917 | sg_set_page(sg, vm_page, |
Charles Keepax | c1aefbd | 2014-11-17 09:14:31 +0000 | [diff] [blame] | 918 | min, offset_in_page(buf)); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 919 | } else { |
Andrew Gabbasov | 65598c1 | 2015-06-30 10:48:37 -0500 | [diff] [blame] | 920 | min = min_t(size_t, len, desc_len); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 921 | sg_buf = buf; |
Juan Gutierrez | 8dd4a01 | 2016-11-21 16:50:03 -0600 | [diff] [blame] | 922 | sg_set_buf(sg, sg_buf, min); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 925 | buf += min; |
| 926 | len -= min; |
Juan Gutierrez | 8dd4a01 | 2016-11-21 16:50:03 -0600 | [diff] [blame] | 927 | sg = sg_next(sg); |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); |
Geert Uytterhoeven | 89e4b66 | 2014-07-10 15:29:32 +0200 | [diff] [blame] | 931 | if (!ret) |
| 932 | ret = -ENOMEM; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 933 | if (ret < 0) { |
| 934 | sg_free_table(sgt); |
| 935 | return ret; |
| 936 | } |
| 937 | |
| 938 | sgt->nents = ret; |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
Boris Brezillon | 4633696 | 2018-04-22 20:35:14 +0200 | [diff] [blame] | 943 | void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, |
| 944 | struct sg_table *sgt, enum dma_data_direction dir) |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 945 | { |
| 946 | if (sgt->orig_nents) { |
| 947 | dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); |
| 948 | sg_free_table(sgt); |
| 949 | } |
| 950 | } |
| 951 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 952 | 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] | 953 | { |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 954 | struct device *tx_dev, *rx_dev; |
| 955 | struct spi_transfer *xfer; |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 956 | int ret; |
Mark Brown | 3a2eba9 | 2014-01-28 20:17:03 +0000 | [diff] [blame] | 957 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 958 | if (!ctlr->can_dma) |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 959 | return 0; |
| 960 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 961 | if (ctlr->dma_tx) |
| 962 | tx_dev = ctlr->dma_tx->device->dev; |
Leilk Liu | c37f45b | 2015-07-23 17:10:40 +0800 | [diff] [blame] | 963 | else |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 964 | tx_dev = ctlr->dev.parent; |
Leilk Liu | c37f45b | 2015-07-23 17:10:40 +0800 | [diff] [blame] | 965 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 966 | if (ctlr->dma_rx) |
| 967 | rx_dev = ctlr->dma_rx->device->dev; |
Leilk Liu | c37f45b | 2015-07-23 17:10:40 +0800 | [diff] [blame] | 968 | else |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 969 | rx_dev = ctlr->dev.parent; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 970 | |
| 971 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 972 | if (!ctlr->can_dma(ctlr, msg->spi, xfer)) |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 973 | continue; |
| 974 | |
| 975 | if (xfer->tx_buf != NULL) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 976 | ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 977 | (void *)xfer->tx_buf, xfer->len, |
| 978 | DMA_TO_DEVICE); |
| 979 | if (ret != 0) |
| 980 | return ret; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | if (xfer->rx_buf != NULL) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 984 | ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 985 | xfer->rx_buf, xfer->len, |
| 986 | DMA_FROM_DEVICE); |
| 987 | if (ret != 0) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 988 | spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, |
Mark Brown | 6ad45a2 | 2014-02-02 13:47:47 +0000 | [diff] [blame] | 989 | DMA_TO_DEVICE); |
| 990 | return ret; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 995 | ctlr->cur_msg_mapped = true; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 996 | |
| 997 | return 0; |
| 998 | } |
| 999 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1000 | 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] | 1001 | { |
| 1002 | struct spi_transfer *xfer; |
| 1003 | struct device *tx_dev, *rx_dev; |
| 1004 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1005 | if (!ctlr->cur_msg_mapped || !ctlr->can_dma) |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1006 | return 0; |
| 1007 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1008 | if (ctlr->dma_tx) |
| 1009 | tx_dev = ctlr->dma_tx->device->dev; |
Leilk Liu | c37f45b | 2015-07-23 17:10:40 +0800 | [diff] [blame] | 1010 | else |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1011 | tx_dev = ctlr->dev.parent; |
Leilk Liu | c37f45b | 2015-07-23 17:10:40 +0800 | [diff] [blame] | 1012 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1013 | if (ctlr->dma_rx) |
| 1014 | rx_dev = ctlr->dma_rx->device->dev; |
Leilk Liu | c37f45b | 2015-07-23 17:10:40 +0800 | [diff] [blame] | 1015 | else |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1016 | rx_dev = ctlr->dev.parent; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1017 | |
| 1018 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1019 | if (!ctlr->can_dma(ctlr, msg->spi, xfer)) |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1020 | continue; |
| 1021 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1022 | spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); |
| 1023 | spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Robin Gong | 809b1b0 | 2020-06-17 06:42:08 +0800 | [diff] [blame] | 1026 | ctlr->cur_msg_mapped = false; |
| 1027 | |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1028 | return 0; |
| 1029 | } |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1030 | #else /* !CONFIG_HAS_DMA */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1031 | static inline int __spi_map_msg(struct spi_controller *ctlr, |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1032 | struct spi_message *msg) |
| 1033 | { |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1037 | static inline int __spi_unmap_msg(struct spi_controller *ctlr, |
Martin Sperl | 4b78645 | 2015-05-25 10:13:10 +0000 | [diff] [blame] | 1038 | struct spi_message *msg) |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1039 | { |
| 1040 | return 0; |
| 1041 | } |
| 1042 | #endif /* !CONFIG_HAS_DMA */ |
| 1043 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1044 | static inline int spi_unmap_msg(struct spi_controller *ctlr, |
Martin Sperl | 4b78645 | 2015-05-25 10:13:10 +0000 | [diff] [blame] | 1045 | struct spi_message *msg) |
| 1046 | { |
| 1047 | struct spi_transfer *xfer; |
| 1048 | |
| 1049 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 1050 | /* |
| 1051 | * Restore the original value of tx_buf or rx_buf if they are |
| 1052 | * NULL. |
| 1053 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1054 | if (xfer->tx_buf == ctlr->dummy_tx) |
Martin Sperl | 4b78645 | 2015-05-25 10:13:10 +0000 | [diff] [blame] | 1055 | xfer->tx_buf = NULL; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1056 | if (xfer->rx_buf == ctlr->dummy_rx) |
Martin Sperl | 4b78645 | 2015-05-25 10:13:10 +0000 | [diff] [blame] | 1057 | xfer->rx_buf = NULL; |
| 1058 | } |
| 1059 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1060 | return __spi_unmap_msg(ctlr, msg); |
Martin Sperl | 4b78645 | 2015-05-25 10:13:10 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1063 | 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] | 1064 | { |
| 1065 | struct spi_transfer *xfer; |
| 1066 | void *tmp; |
| 1067 | unsigned int max_tx, max_rx; |
| 1068 | |
dillon min | aee67fe | 2020-05-25 11:45:48 +0800 | [diff] [blame] | 1069 | if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) |
| 1070 | && !(msg->spi->mode & SPI_3WIRE)) { |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1071 | max_tx = 0; |
| 1072 | max_rx = 0; |
| 1073 | |
| 1074 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1075 | if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1076 | !xfer->tx_buf) |
| 1077 | max_tx = max(xfer->len, max_tx); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1078 | if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1079 | !xfer->rx_buf) |
| 1080 | max_rx = max(xfer->len, max_rx); |
| 1081 | } |
| 1082 | |
| 1083 | if (max_tx) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1084 | tmp = krealloc(ctlr->dummy_tx, max_tx, |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1085 | GFP_KERNEL | GFP_DMA); |
| 1086 | if (!tmp) |
| 1087 | return -ENOMEM; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1088 | ctlr->dummy_tx = tmp; |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1089 | memset(tmp, 0, max_tx); |
| 1090 | } |
| 1091 | |
| 1092 | if (max_rx) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1093 | tmp = krealloc(ctlr->dummy_rx, max_rx, |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1094 | GFP_KERNEL | GFP_DMA); |
| 1095 | if (!tmp) |
| 1096 | return -ENOMEM; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1097 | ctlr->dummy_rx = tmp; |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | if (max_tx || max_rx) { |
| 1101 | list_for_each_entry(xfer, &msg->transfers, |
| 1102 | transfer_list) { |
Chris Lesiak | 5442dca | 2019-03-07 20:39:00 +0000 | [diff] [blame] | 1103 | if (!xfer->len) |
| 1104 | continue; |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1105 | if (!xfer->tx_buf) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1106 | xfer->tx_buf = ctlr->dummy_tx; |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1107 | if (!xfer->rx_buf) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1108 | xfer->rx_buf = ctlr->dummy_rx; |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1113 | return __spi_map_msg(ctlr, msg); |
Geert Uytterhoeven | 2de440f | 2014-05-02 06:29:34 +0200 | [diff] [blame] | 1114 | } |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1115 | |
Lubomir Rintel | 810923f | 2018-11-13 11:22:24 +0100 | [diff] [blame] | 1116 | static int spi_transfer_wait(struct spi_controller *ctlr, |
| 1117 | struct spi_message *msg, |
| 1118 | struct spi_transfer *xfer) |
| 1119 | { |
| 1120 | struct spi_statistics *statm = &ctlr->statistics; |
| 1121 | struct spi_statistics *stats = &msg->spi->statistics; |
Xu Yilun | 501e187 | 2021-01-04 09:29:09 +0800 | [diff] [blame] | 1122 | u32 speed_hz = xfer->speed_hz; |
Colin Ian King | 49686df | 2020-04-10 13:23:15 +0100 | [diff] [blame] | 1123 | unsigned long long ms; |
Lubomir Rintel | 810923f | 2018-11-13 11:22:24 +0100 | [diff] [blame] | 1124 | |
| 1125 | if (spi_controller_is_slave(ctlr)) { |
| 1126 | if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { |
| 1127 | dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); |
| 1128 | return -EINTR; |
| 1129 | } |
| 1130 | } else { |
Xu Yilun | 501e187 | 2021-01-04 09:29:09 +0800 | [diff] [blame] | 1131 | if (!speed_hz) |
| 1132 | speed_hz = 100000; |
| 1133 | |
Lubomir Rintel | 810923f | 2018-11-13 11:22:24 +0100 | [diff] [blame] | 1134 | ms = 8LL * 1000LL * xfer->len; |
Xu Yilun | 501e187 | 2021-01-04 09:29:09 +0800 | [diff] [blame] | 1135 | do_div(ms, speed_hz); |
Lubomir Rintel | 810923f | 2018-11-13 11:22:24 +0100 | [diff] [blame] | 1136 | ms += ms + 200; /* some tolerance */ |
| 1137 | |
| 1138 | if (ms > UINT_MAX) |
| 1139 | ms = UINT_MAX; |
| 1140 | |
| 1141 | ms = wait_for_completion_timeout(&ctlr->xfer_completion, |
| 1142 | msecs_to_jiffies(ms)); |
| 1143 | |
| 1144 | if (ms == 0) { |
| 1145 | SPI_STATISTICS_INCREMENT_FIELD(statm, timedout); |
| 1146 | SPI_STATISTICS_INCREMENT_FIELD(stats, timedout); |
| 1147 | dev_err(&msg->spi->dev, |
| 1148 | "SPI transfer timed out\n"); |
| 1149 | return -ETIMEDOUT; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1156 | static void _spi_transfer_delay_ns(u32 ns) |
| 1157 | { |
| 1158 | if (!ns) |
| 1159 | return; |
| 1160 | if (ns <= 1000) { |
| 1161 | ndelay(ns); |
| 1162 | } else { |
| 1163 | u32 us = DIV_ROUND_UP(ns, 1000); |
| 1164 | |
| 1165 | if (us <= 10) |
| 1166 | udelay(us); |
| 1167 | else |
| 1168 | usleep_range(us, us + DIV_ROUND_UP(us, 10)); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Alexandru Ardelean | 3984d39 | 2019-09-26 13:51:44 +0300 | [diff] [blame] | 1172 | int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer) |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1173 | { |
Alexandru Ardelean | b2c9815 | 2019-09-26 13:51:30 +0300 | [diff] [blame] | 1174 | u32 delay = _delay->value; |
| 1175 | u32 unit = _delay->unit; |
Martin Sperl | d5864e5 | 2019-02-23 08:49:50 +0000 | [diff] [blame] | 1176 | u32 hz; |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1177 | |
Alexandru Ardelean | b2c9815 | 2019-09-26 13:51:30 +0300 | [diff] [blame] | 1178 | if (!delay) |
| 1179 | return 0; |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1180 | |
| 1181 | switch (unit) { |
| 1182 | case SPI_DELAY_UNIT_USECS: |
Alexandru Ardelean | b2c9815 | 2019-09-26 13:51:30 +0300 | [diff] [blame] | 1183 | delay *= 1000; |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1184 | break; |
| 1185 | case SPI_DELAY_UNIT_NSECS: /* nothing to do here */ |
| 1186 | break; |
Martin Sperl | d5864e5 | 2019-02-23 08:49:50 +0000 | [diff] [blame] | 1187 | case SPI_DELAY_UNIT_SCK: |
Alexandru Ardelean | b2c9815 | 2019-09-26 13:51:30 +0300 | [diff] [blame] | 1188 | /* clock cycles need to be obtained from spi_transfer */ |
| 1189 | if (!xfer) |
| 1190 | return -EINVAL; |
Martin Sperl | d5864e5 | 2019-02-23 08:49:50 +0000 | [diff] [blame] | 1191 | /* if there is no effective speed know, then approximate |
| 1192 | * by underestimating with half the requested hz |
| 1193 | */ |
| 1194 | hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; |
Alexandru Ardelean | b2c9815 | 2019-09-26 13:51:30 +0300 | [diff] [blame] | 1195 | if (!hz) |
| 1196 | return -EINVAL; |
Martin Sperl | d5864e5 | 2019-02-23 08:49:50 +0000 | [diff] [blame] | 1197 | delay *= DIV_ROUND_UP(1000000000, hz); |
| 1198 | break; |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1199 | default: |
Alexandru Ardelean | b2c9815 | 2019-09-26 13:51:30 +0300 | [diff] [blame] | 1200 | return -EINVAL; |
| 1201 | } |
| 1202 | |
| 1203 | return delay; |
| 1204 | } |
Alexandru Ardelean | 3984d39 | 2019-09-26 13:51:44 +0300 | [diff] [blame] | 1205 | EXPORT_SYMBOL_GPL(spi_delay_to_ns); |
Alexandru Ardelean | b2c9815 | 2019-09-26 13:51:30 +0300 | [diff] [blame] | 1206 | |
| 1207 | int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer) |
| 1208 | { |
| 1209 | int delay; |
| 1210 | |
Mark Brown | 8fede89 | 2020-05-22 16:50:05 +0100 | [diff] [blame] | 1211 | might_sleep(); |
| 1212 | |
Alexandru Ardelean | b2c9815 | 2019-09-26 13:51:30 +0300 | [diff] [blame] | 1213 | if (!_delay) |
| 1214 | return -EINVAL; |
| 1215 | |
Alexandru Ardelean | 3984d39 | 2019-09-26 13:51:44 +0300 | [diff] [blame] | 1216 | delay = spi_delay_to_ns(_delay, xfer); |
Alexandru Ardelean | b2c9815 | 2019-09-26 13:51:30 +0300 | [diff] [blame] | 1217 | if (delay < 0) |
| 1218 | return delay; |
| 1219 | |
| 1220 | _spi_transfer_delay_ns(delay); |
| 1221 | |
| 1222 | return 0; |
| 1223 | } |
| 1224 | EXPORT_SYMBOL_GPL(spi_delay_exec); |
| 1225 | |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1226 | static void _spi_transfer_cs_change_delay(struct spi_message *msg, |
| 1227 | struct spi_transfer *xfer) |
| 1228 | { |
Alexandru Ardelean | 329f0da | 2019-09-26 13:51:31 +0300 | [diff] [blame] | 1229 | u32 delay = xfer->cs_change_delay.value; |
| 1230 | u32 unit = xfer->cs_change_delay.unit; |
| 1231 | int ret; |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1232 | |
| 1233 | /* return early on "fast" mode - for everything but USECS */ |
Alexandru Ardelean | 6b3f236 | 2019-09-26 13:51:29 +0300 | [diff] [blame] | 1234 | if (!delay) { |
| 1235 | if (unit == SPI_DELAY_UNIT_USECS) |
| 1236 | _spi_transfer_delay_ns(10000); |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1237 | return; |
Alexandru Ardelean | 6b3f236 | 2019-09-26 13:51:29 +0300 | [diff] [blame] | 1238 | } |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1239 | |
Alexandru Ardelean | 329f0da | 2019-09-26 13:51:31 +0300 | [diff] [blame] | 1240 | ret = spi_delay_exec(&xfer->cs_change_delay, xfer); |
| 1241 | if (ret) { |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1242 | dev_err_once(&msg->spi->dev, |
| 1243 | "Use of unsupported delay unit %i, using default of 10us\n", |
Alexandru Ardelean | 329f0da | 2019-09-26 13:51:31 +0300 | [diff] [blame] | 1244 | unit); |
| 1245 | _spi_transfer_delay_ns(10000); |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1246 | } |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1249 | /* |
| 1250 | * spi_transfer_one_message - Default implementation of transfer_one_message() |
| 1251 | * |
| 1252 | * This is a standard implementation of transfer_one_message() for |
Moritz Fischer | 8ba811a | 2016-05-03 11:59:30 -0700 | [diff] [blame] | 1253 | * drivers which implement a transfer_one() operation. It provides |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1254 | * standard handling of delays and chip select management. |
| 1255 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1256 | static int spi_transfer_one_message(struct spi_controller *ctlr, |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1257 | struct spi_message *msg) |
| 1258 | { |
| 1259 | struct spi_transfer *xfer; |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1260 | bool keep_cs = false; |
| 1261 | int ret = 0; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1262 | struct spi_statistics *statm = &ctlr->statistics; |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 1263 | struct spi_statistics *stats = &msg->spi->statistics; |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1264 | |
David Bauer | c5aa9ea | 2021-04-16 21:59:56 +0200 | [diff] [blame] | 1265 | spi_set_cs(msg->spi, true, false); |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1266 | |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 1267 | SPI_STATISTICS_INCREMENT_FIELD(statm, messages); |
| 1268 | SPI_STATISTICS_INCREMENT_FIELD(stats, messages); |
| 1269 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1270 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 1271 | trace_spi_transfer_start(msg, xfer); |
| 1272 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1273 | spi_statistics_add_transfer_stats(statm, xfer, ctlr); |
| 1274 | spi_statistics_add_transfer_stats(stats, xfer, ctlr); |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 1275 | |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1276 | if (!ctlr->ptp_sts_supported) { |
| 1277 | xfer->ptp_sts_word_pre = 0; |
| 1278 | ptp_read_system_prets(xfer->ptp_sts); |
| 1279 | } |
| 1280 | |
Nicolas Saenz Julienne | 86f5b09 | 2021-02-11 19:08:20 +0100 | [diff] [blame] | 1281 | if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1282 | reinit_completion(&ctlr->xfer_completion); |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1283 | |
Robin Gong | 809b1b0 | 2020-06-17 06:42:08 +0800 | [diff] [blame] | 1284 | fallback_pio: |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1285 | ret = ctlr->transfer_one(ctlr, msg->spi, xfer); |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1286 | if (ret < 0) { |
Robin Gong | 809b1b0 | 2020-06-17 06:42:08 +0800 | [diff] [blame] | 1287 | if (ctlr->cur_msg_mapped && |
| 1288 | (xfer->error & SPI_TRANS_FAIL_NO_START)) { |
| 1289 | __spi_unmap_msg(ctlr, msg); |
| 1290 | ctlr->fallback = true; |
| 1291 | xfer->error &= ~SPI_TRANS_FAIL_NO_START; |
| 1292 | goto fallback_pio; |
| 1293 | } |
| 1294 | |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 1295 | SPI_STATISTICS_INCREMENT_FIELD(statm, |
| 1296 | errors); |
| 1297 | SPI_STATISTICS_INCREMENT_FIELD(stats, |
| 1298 | errors); |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1299 | dev_err(&msg->spi->dev, |
| 1300 | "SPI transfer failed: %d\n", ret); |
| 1301 | goto out; |
| 1302 | } |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1303 | |
Mark Brown | d57e796 | 2018-11-15 16:08:32 -0800 | [diff] [blame] | 1304 | if (ret > 0) { |
| 1305 | ret = spi_transfer_wait(ctlr, msg, xfer); |
| 1306 | if (ret < 0) |
| 1307 | msg->status = ret; |
| 1308 | } |
Mark Brown | 38ec10f | 2014-08-16 16:27:41 +0100 | [diff] [blame] | 1309 | } else { |
| 1310 | if (xfer->len) |
| 1311 | dev_err(&msg->spi->dev, |
| 1312 | "Bufferless transfer has length %u\n", |
| 1313 | xfer->len); |
Axel Lin | 13a4279 | 2014-01-18 22:05:22 +0800 | [diff] [blame] | 1314 | } |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1315 | |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1316 | if (!ctlr->ptp_sts_supported) { |
| 1317 | ptp_read_system_postts(xfer->ptp_sts); |
| 1318 | xfer->ptp_sts_word_post = xfer->len; |
| 1319 | } |
| 1320 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1321 | trace_spi_transfer_stop(msg, xfer); |
| 1322 | |
| 1323 | if (msg->status != -EINPROGRESS) |
| 1324 | goto out; |
| 1325 | |
Alexandru Ardelean | bebcfd2 | 2019-09-26 13:51:36 +0300 | [diff] [blame] | 1326 | spi_transfer_delay_exec(xfer); |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1327 | |
| 1328 | if (xfer->cs_change) { |
| 1329 | if (list_is_last(&xfer->transfer_list, |
| 1330 | &msg->transfers)) { |
| 1331 | keep_cs = true; |
| 1332 | } else { |
David Bauer | c5aa9ea | 2021-04-16 21:59:56 +0200 | [diff] [blame] | 1333 | spi_set_cs(msg->spi, false, false); |
Martin Sperl | 0ff2de8 | 2019-02-23 08:49:48 +0000 | [diff] [blame] | 1334 | _spi_transfer_cs_change_delay(msg, xfer); |
David Bauer | c5aa9ea | 2021-04-16 21:59:56 +0200 | [diff] [blame] | 1335 | spi_set_cs(msg->spi, true, false); |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | msg->actual_length += xfer->len; |
| 1340 | } |
| 1341 | |
| 1342 | out: |
| 1343 | if (ret != 0 || !keep_cs) |
David Bauer | c5aa9ea | 2021-04-16 21:59:56 +0200 | [diff] [blame] | 1344 | spi_set_cs(msg->spi, false, false); |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1345 | |
| 1346 | if (msg->status == -EINPROGRESS) |
| 1347 | msg->status = ret; |
| 1348 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1349 | if (msg->status && ctlr->handle_err) |
| 1350 | ctlr->handle_err(ctlr, msg); |
Andy Shevchenko | b716c4f | 2015-02-27 17:34:15 +0200 | [diff] [blame] | 1351 | |
Mark Brown | 0ed5625 | 2019-05-09 11:27:17 +0900 | [diff] [blame] | 1352 | spi_finalize_current_message(ctlr); |
| 1353 | |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1354 | return ret; |
| 1355 | } |
| 1356 | |
| 1357 | /** |
| 1358 | * spi_finalize_current_transfer - report completion of a transfer |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1359 | * @ctlr: the controller reporting completion |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1360 | * |
| 1361 | * Called by SPI drivers using the core transfer_one_message() |
| 1362 | * implementation to notify it that the current interrupt driven |
Geert Uytterhoeven | 9e8f488 | 2014-01-21 16:10:05 +0100 | [diff] [blame] | 1363 | * transfer has finished and the next one may be scheduled. |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1364 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1365 | void spi_finalize_current_transfer(struct spi_controller *ctlr) |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1366 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1367 | complete(&ctlr->xfer_completion); |
Mark Brown | b158935 | 2013-10-05 11:50:40 +0100 | [diff] [blame] | 1368 | } |
| 1369 | EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); |
| 1370 | |
Mark Brown | e126859 | 2020-07-15 17:36:10 +0100 | [diff] [blame] | 1371 | static void spi_idle_runtime_pm(struct spi_controller *ctlr) |
| 1372 | { |
| 1373 | if (ctlr->auto_runtime_pm) { |
| 1374 | pm_runtime_mark_last_busy(ctlr->dev.parent); |
| 1375 | pm_runtime_put_autosuspend(ctlr->dev.parent); |
| 1376 | } |
| 1377 | } |
| 1378 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1379 | /** |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1380 | * __spi_pump_messages - function which processes spi message queue |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1381 | * @ctlr: controller to process queue for |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1382 | * @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] | 1383 | * |
| 1384 | * This function checks if there is any spi message in the queue that |
| 1385 | * needs processing and if so call out to the driver to initialize hardware |
| 1386 | * and transfer each message. |
| 1387 | * |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1388 | * Note that it is called both from the kthread itself and also from |
| 1389 | * inside spi_sync(); the queue extraction handling at the top of the |
| 1390 | * function should deal with this safely. |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1391 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1392 | static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1393 | { |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1394 | struct spi_transfer *xfer; |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1395 | struct spi_message *msg; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1396 | bool was_busy = false; |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1397 | unsigned long flags; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1398 | int ret; |
| 1399 | |
Mark Brown | 983aee5 | 2014-12-09 19:46:56 +0000 | [diff] [blame] | 1400 | /* Lock queue */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1401 | spin_lock_irqsave(&ctlr->queue_lock, flags); |
Mark Brown | 983aee5 | 2014-12-09 19:46:56 +0000 | [diff] [blame] | 1402 | |
| 1403 | /* Make sure we are not already running a message */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1404 | if (ctlr->cur_msg) { |
| 1405 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Mark Brown | 983aee5 | 2014-12-09 19:46:56 +0000 | [diff] [blame] | 1406 | return; |
| 1407 | } |
| 1408 | |
Mark Brown | f0125f1 | 2019-01-23 17:29:53 +0000 | [diff] [blame] | 1409 | /* If another context is idling the device then defer */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1410 | if (ctlr->idling) { |
Marek Szyprowski | 60a883d | 2020-07-09 08:50:07 +0200 | [diff] [blame] | 1411 | kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1412 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1413 | return; |
| 1414 | } |
| 1415 | |
Mark Brown | 983aee5 | 2014-12-09 19:46:56 +0000 | [diff] [blame] | 1416 | /* Check if the queue is idle */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1417 | if (list_empty(&ctlr->queue) || !ctlr->running) { |
| 1418 | if (!ctlr->busy) { |
| 1419 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Bryan Freed | b0b36b8 | 2013-03-13 11:17:40 -0700 | [diff] [blame] | 1420 | return; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1421 | } |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1422 | |
Mark Brown | e126859 | 2020-07-15 17:36:10 +0100 | [diff] [blame] | 1423 | /* Defer any non-atomic teardown to the thread */ |
Mark Brown | f0125f1 | 2019-01-23 17:29:53 +0000 | [diff] [blame] | 1424 | if (!in_kthread) { |
Mark Brown | e126859 | 2020-07-15 17:36:10 +0100 | [diff] [blame] | 1425 | if (!ctlr->dummy_rx && !ctlr->dummy_tx && |
| 1426 | !ctlr->unprepare_transfer_hardware) { |
| 1427 | spi_idle_runtime_pm(ctlr); |
| 1428 | ctlr->busy = false; |
| 1429 | trace_spi_controller_idle(ctlr); |
| 1430 | } else { |
| 1431 | kthread_queue_work(ctlr->kworker, |
| 1432 | &ctlr->pump_messages); |
| 1433 | } |
Mark Brown | f0125f1 | 2019-01-23 17:29:53 +0000 | [diff] [blame] | 1434 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
| 1435 | return; |
| 1436 | } |
| 1437 | |
| 1438 | ctlr->busy = false; |
| 1439 | ctlr->idling = true; |
| 1440 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
| 1441 | |
| 1442 | kfree(ctlr->dummy_rx); |
| 1443 | ctlr->dummy_rx = NULL; |
| 1444 | kfree(ctlr->dummy_tx); |
| 1445 | ctlr->dummy_tx = NULL; |
| 1446 | if (ctlr->unprepare_transfer_hardware && |
| 1447 | ctlr->unprepare_transfer_hardware(ctlr)) |
| 1448 | dev_err(&ctlr->dev, |
| 1449 | "failed to unprepare transfer hardware\n"); |
Mark Brown | e126859 | 2020-07-15 17:36:10 +0100 | [diff] [blame] | 1450 | spi_idle_runtime_pm(ctlr); |
Mark Brown | f0125f1 | 2019-01-23 17:29:53 +0000 | [diff] [blame] | 1451 | trace_spi_controller_idle(ctlr); |
| 1452 | |
| 1453 | spin_lock_irqsave(&ctlr->queue_lock, flags); |
| 1454 | ctlr->idling = false; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1455 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1456 | return; |
| 1457 | } |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1458 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1459 | /* Extract head of queue */ |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1460 | msg = list_first_entry(&ctlr->queue, struct spi_message, queue); |
| 1461 | ctlr->cur_msg = msg; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1462 | |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1463 | list_del_init(&msg->queue); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1464 | if (ctlr->busy) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1465 | was_busy = true; |
| 1466 | else |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1467 | ctlr->busy = true; |
| 1468 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1469 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1470 | mutex_lock(&ctlr->io_mutex); |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 1471 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1472 | if (!was_busy && ctlr->auto_runtime_pm) { |
| 1473 | ret = pm_runtime_get_sync(ctlr->dev.parent); |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 1474 | if (ret < 0) { |
Tony Lindgren | 7e48e23 | 2018-05-18 10:30:07 -0700 | [diff] [blame] | 1475 | pm_runtime_put_noidle(ctlr->dev.parent); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1476 | dev_err(&ctlr->dev, "Failed to power device: %d\n", |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 1477 | ret); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1478 | mutex_unlock(&ctlr->io_mutex); |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 1479 | return; |
| 1480 | } |
| 1481 | } |
| 1482 | |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 1483 | if (!was_busy) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1484 | trace_spi_controller_busy(ctlr); |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 1485 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1486 | if (!was_busy && ctlr->prepare_transfer_hardware) { |
| 1487 | ret = ctlr->prepare_transfer_hardware(ctlr); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1488 | if (ret) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1489 | dev_err(&ctlr->dev, |
Super Liu | f3440d9 | 2019-05-22 14:30:14 +0800 | [diff] [blame] | 1490 | "failed to prepare transfer hardware: %d\n", |
| 1491 | ret); |
Mark Brown | 49834de | 2013-07-28 14:47:02 +0100 | [diff] [blame] | 1492 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1493 | if (ctlr->auto_runtime_pm) |
| 1494 | pm_runtime_put(ctlr->dev.parent); |
Super Liu | f3440d9 | 2019-05-22 14:30:14 +0800 | [diff] [blame] | 1495 | |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1496 | msg->status = ret; |
Super Liu | f3440d9 | 2019-05-22 14:30:14 +0800 | [diff] [blame] | 1497 | spi_finalize_current_message(ctlr); |
| 1498 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1499 | mutex_unlock(&ctlr->io_mutex); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1500 | return; |
| 1501 | } |
| 1502 | } |
| 1503 | |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1504 | trace_spi_message_start(msg); |
Mark Brown | 56ec197 | 2013-10-07 19:33:53 +0100 | [diff] [blame] | 1505 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1506 | if (ctlr->prepare_message) { |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1507 | ret = ctlr->prepare_message(ctlr, msg); |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1508 | if (ret) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1509 | dev_err(&ctlr->dev, "failed to prepare message: %d\n", |
| 1510 | ret); |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1511 | msg->status = ret; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1512 | spi_finalize_current_message(ctlr); |
Jon Hunter | 49023d2 | 2016-03-08 12:28:20 +0000 | [diff] [blame] | 1513 | goto out; |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1514 | } |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1515 | ctlr->cur_msg_prepared = true; |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1516 | } |
| 1517 | |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1518 | ret = spi_map_msg(ctlr, msg); |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1519 | if (ret) { |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1520 | msg->status = ret; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1521 | spi_finalize_current_message(ctlr); |
Jon Hunter | 49023d2 | 2016-03-08 12:28:20 +0000 | [diff] [blame] | 1522 | goto out; |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1525 | if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { |
| 1526 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 1527 | xfer->ptp_sts_word_pre = 0; |
| 1528 | ptp_read_system_prets(xfer->ptp_sts); |
| 1529 | } |
| 1530 | } |
| 1531 | |
Vladimir Oltean | d1c44c9 | 2019-09-05 04:01:11 +0300 | [diff] [blame] | 1532 | ret = ctlr->transfer_one_message(ctlr, msg); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1533 | if (ret) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1534 | dev_err(&ctlr->dev, |
Geert Uytterhoeven | 1f802f8 | 2014-01-28 10:33:03 +0100 | [diff] [blame] | 1535 | "failed to transfer one message from queue\n"); |
Jon Hunter | 49023d2 | 2016-03-08 12:28:20 +0000 | [diff] [blame] | 1536 | goto out; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1537 | } |
Jon Hunter | 49023d2 | 2016-03-08 12:28:20 +0000 | [diff] [blame] | 1538 | |
| 1539 | out: |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1540 | mutex_unlock(&ctlr->io_mutex); |
Mark Brown | 6282697 | 2016-02-15 18:28:02 +0000 | [diff] [blame] | 1541 | |
| 1542 | /* Prod the scheduler in case transfer_one() was busy waiting */ |
Jon Hunter | 49023d2 | 2016-03-08 12:28:20 +0000 | [diff] [blame] | 1543 | if (!ret) |
| 1544 | cond_resched(); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1545 | } |
| 1546 | |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1547 | /** |
| 1548 | * spi_pump_messages - kthread work function which processes spi message queue |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1549 | * @work: pointer to kthread work struct contained in the controller struct |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1550 | */ |
| 1551 | static void spi_pump_messages(struct kthread_work *work) |
| 1552 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1553 | struct spi_controller *ctlr = |
| 1554 | container_of(work, struct spi_controller, pump_messages); |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1555 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1556 | __spi_pump_messages(ctlr, true); |
Mark Brown | fc9e0f7 | 2014-12-10 13:46:33 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Douglas Anderson | 924b586 | 2019-05-15 09:48:12 -0700 | [diff] [blame] | 1559 | /** |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1560 | * spi_take_timestamp_pre - helper for drivers to collect the beginning of the |
| 1561 | * TX timestamp for the requested byte from the SPI |
| 1562 | * transfer. The frequency with which this function |
| 1563 | * must be called (once per word, once for the whole |
| 1564 | * transfer, once per batch of words etc) is arbitrary |
| 1565 | * as long as the @tx buffer offset is greater than or |
| 1566 | * equal to the requested byte at the time of the |
| 1567 | * call. The timestamp is only taken once, at the |
| 1568 | * first such call. It is assumed that the driver |
| 1569 | * advances its @tx buffer pointer monotonically. |
| 1570 | * @ctlr: Pointer to the spi_controller structure of the driver |
| 1571 | * @xfer: Pointer to the transfer being timestamped |
Vladimir Oltean | 862dd2a | 2019-12-27 03:24:17 +0200 | [diff] [blame] | 1572 | * @progress: How many words (not bytes) have been transferred so far |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1573 | * @irqs_off: If true, will disable IRQs and preemption for the duration of the |
| 1574 | * transfer, for less jitter in time measurement. Only compatible |
| 1575 | * with PIO drivers. If true, must follow up with |
| 1576 | * spi_take_timestamp_post or otherwise system will crash. |
| 1577 | * WARNING: for fully predictable results, the CPU frequency must |
| 1578 | * also be under control (governor). |
| 1579 | */ |
| 1580 | void spi_take_timestamp_pre(struct spi_controller *ctlr, |
| 1581 | struct spi_transfer *xfer, |
Vladimir Oltean | 862dd2a | 2019-12-27 03:24:17 +0200 | [diff] [blame] | 1582 | size_t progress, bool irqs_off) |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1583 | { |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1584 | if (!xfer->ptp_sts) |
| 1585 | return; |
| 1586 | |
Vladimir Oltean | 6a72682 | 2020-03-05 00:00:39 +0200 | [diff] [blame] | 1587 | if (xfer->timestamped) |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1588 | return; |
| 1589 | |
Vladimir Oltean | 6a72682 | 2020-03-05 00:00:39 +0200 | [diff] [blame] | 1590 | if (progress > xfer->ptp_sts_word_pre) |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1591 | return; |
| 1592 | |
| 1593 | /* Capture the resolution of the timestamp */ |
Vladimir Oltean | 862dd2a | 2019-12-27 03:24:17 +0200 | [diff] [blame] | 1594 | xfer->ptp_sts_word_pre = progress; |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1595 | |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1596 | if (irqs_off) { |
| 1597 | local_irq_save(ctlr->irq_flags); |
| 1598 | preempt_disable(); |
| 1599 | } |
| 1600 | |
| 1601 | ptp_read_system_prets(xfer->ptp_sts); |
| 1602 | } |
| 1603 | EXPORT_SYMBOL_GPL(spi_take_timestamp_pre); |
| 1604 | |
| 1605 | /** |
| 1606 | * spi_take_timestamp_post - helper for drivers to collect the end of the |
| 1607 | * TX timestamp for the requested byte from the SPI |
| 1608 | * transfer. Can be called with an arbitrary |
| 1609 | * frequency: only the first call where @tx exceeds |
| 1610 | * or is equal to the requested word will be |
| 1611 | * timestamped. |
| 1612 | * @ctlr: Pointer to the spi_controller structure of the driver |
| 1613 | * @xfer: Pointer to the transfer being timestamped |
Vladimir Oltean | 862dd2a | 2019-12-27 03:24:17 +0200 | [diff] [blame] | 1614 | * @progress: How many words (not bytes) have been transferred so far |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1615 | * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU. |
| 1616 | */ |
| 1617 | void spi_take_timestamp_post(struct spi_controller *ctlr, |
| 1618 | struct spi_transfer *xfer, |
Vladimir Oltean | 862dd2a | 2019-12-27 03:24:17 +0200 | [diff] [blame] | 1619 | size_t progress, bool irqs_off) |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1620 | { |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1621 | if (!xfer->ptp_sts) |
| 1622 | return; |
| 1623 | |
Vladimir Oltean | 6a72682 | 2020-03-05 00:00:39 +0200 | [diff] [blame] | 1624 | if (xfer->timestamped) |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1625 | return; |
| 1626 | |
Vladimir Oltean | 862dd2a | 2019-12-27 03:24:17 +0200 | [diff] [blame] | 1627 | if (progress < xfer->ptp_sts_word_post) |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1628 | return; |
| 1629 | |
| 1630 | ptp_read_system_postts(xfer->ptp_sts); |
| 1631 | |
| 1632 | if (irqs_off) { |
| 1633 | local_irq_restore(ctlr->irq_flags); |
| 1634 | preempt_enable(); |
| 1635 | } |
| 1636 | |
| 1637 | /* Capture the resolution of the timestamp */ |
Vladimir Oltean | 862dd2a | 2019-12-27 03:24:17 +0200 | [diff] [blame] | 1638 | xfer->ptp_sts_word_post = progress; |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1639 | |
Vladimir Oltean | 6a72682 | 2020-03-05 00:00:39 +0200 | [diff] [blame] | 1640 | xfer->timestamped = true; |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1641 | } |
| 1642 | EXPORT_SYMBOL_GPL(spi_take_timestamp_post); |
| 1643 | |
| 1644 | /** |
Douglas Anderson | 924b586 | 2019-05-15 09:48:12 -0700 | [diff] [blame] | 1645 | * spi_set_thread_rt - set the controller to pump at realtime priority |
| 1646 | * @ctlr: controller to boost priority of |
| 1647 | * |
| 1648 | * This can be called because the controller requested realtime priority |
| 1649 | * (by setting the ->rt value before calling spi_register_controller()) or |
| 1650 | * because a device on the bus said that its transfers needed realtime |
| 1651 | * priority. |
| 1652 | * |
| 1653 | * NOTE: at the moment if any device on a bus says it needs realtime then |
| 1654 | * the thread will be at realtime priority for all transfers on that |
| 1655 | * controller. If this eventually becomes a problem we may see if we can |
| 1656 | * find a way to boost the priority only temporarily during relevant |
| 1657 | * transfers. |
| 1658 | */ |
| 1659 | static void spi_set_thread_rt(struct spi_controller *ctlr) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1660 | { |
Douglas Anderson | 924b586 | 2019-05-15 09:48:12 -0700 | [diff] [blame] | 1661 | dev_info(&ctlr->dev, |
| 1662 | "will run message pump with realtime priority\n"); |
Linus Torvalds | 6d2b84a | 2020-08-06 11:55:43 -0700 | [diff] [blame] | 1663 | sched_set_fifo(ctlr->kworker->task); |
Douglas Anderson | 924b586 | 2019-05-15 09:48:12 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | static int spi_init_queue(struct spi_controller *ctlr) |
| 1667 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1668 | ctlr->running = false; |
| 1669 | ctlr->busy = false; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1670 | |
Marek Szyprowski | 60a883d | 2020-07-09 08:50:07 +0200 | [diff] [blame] | 1671 | ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); |
| 1672 | if (IS_ERR(ctlr->kworker)) { |
| 1673 | dev_err(&ctlr->dev, "failed to create message pump kworker\n"); |
| 1674 | return PTR_ERR(ctlr->kworker); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1675 | } |
Marek Szyprowski | 60a883d | 2020-07-09 08:50:07 +0200 | [diff] [blame] | 1676 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1677 | kthread_init_work(&ctlr->pump_messages, spi_pump_messages); |
Mark Brown | f0125f1 | 2019-01-23 17:29:53 +0000 | [diff] [blame] | 1678 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1679 | /* |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1680 | * Controller config will indicate if this controller should run the |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1681 | * message pump with high (realtime) priority to reduce the transfer |
| 1682 | * latency on the bus by minimising the delay between a transfer |
| 1683 | * request and the scheduling of the message pump thread. Without this |
| 1684 | * setting the message pump thread will remain at default priority. |
| 1685 | */ |
Douglas Anderson | 924b586 | 2019-05-15 09:48:12 -0700 | [diff] [blame] | 1686 | if (ctlr->rt) |
| 1687 | spi_set_thread_rt(ctlr); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1688 | |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | /** |
| 1693 | * spi_get_next_queued_message() - called by driver to check for queued |
| 1694 | * messages |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1695 | * @ctlr: the controller to check for queued messages |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1696 | * |
| 1697 | * If there are more messages in the queue, the next message is returned from |
| 1698 | * this call. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 1699 | * |
| 1700 | * 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] | 1701 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1702 | struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1703 | { |
| 1704 | struct spi_message *next; |
| 1705 | unsigned long flags; |
| 1706 | |
| 1707 | /* get a pointer to the next message, if any */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1708 | spin_lock_irqsave(&ctlr->queue_lock, flags); |
| 1709 | next = list_first_entry_or_null(&ctlr->queue, struct spi_message, |
Axel Lin | 1cfd97f | 2014-01-02 15:16:40 +0800 | [diff] [blame] | 1710 | queue); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1711 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1712 | |
| 1713 | return next; |
| 1714 | } |
| 1715 | EXPORT_SYMBOL_GPL(spi_get_next_queued_message); |
| 1716 | |
| 1717 | /** |
| 1718 | * spi_finalize_current_message() - the current message is complete |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1719 | * @ctlr: the controller to return the message to |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1720 | * |
| 1721 | * Called by the driver to notify the core that the message in the front of the |
| 1722 | * queue is complete and can be removed from the queue. |
| 1723 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1724 | void spi_finalize_current_message(struct spi_controller *ctlr) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1725 | { |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1726 | struct spi_transfer *xfer; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1727 | struct spi_message *mesg; |
| 1728 | unsigned long flags; |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1729 | int ret; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1730 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1731 | spin_lock_irqsave(&ctlr->queue_lock, flags); |
| 1732 | mesg = ctlr->cur_msg; |
| 1733 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1734 | |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 1735 | if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { |
| 1736 | list_for_each_entry(xfer, &mesg->transfers, transfer_list) { |
| 1737 | ptp_read_system_postts(xfer->ptp_sts); |
| 1738 | xfer->ptp_sts_word_post = xfer->len; |
| 1739 | } |
| 1740 | } |
| 1741 | |
Vladimir Oltean | 6a72682 | 2020-03-05 00:00:39 +0200 | [diff] [blame] | 1742 | if (unlikely(ctlr->ptp_sts_supported)) |
| 1743 | list_for_each_entry(xfer, &mesg->transfers, transfer_list) |
| 1744 | WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); |
Vladimir Oltean | f971a20 | 2019-12-27 03:24:44 +0200 | [diff] [blame] | 1745 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1746 | spi_unmap_msg(ctlr, mesg); |
Mark Brown | 99adef3 | 2014-01-16 12:22:43 +0000 | [diff] [blame] | 1747 | |
Gustav Wiklander | b59a7ca | 2020-09-08 17:11:29 +0200 | [diff] [blame] | 1748 | /* In the prepare_messages callback the spi bus has the opportunity to |
| 1749 | * split a transfer to smaller chunks. |
| 1750 | * Release splited transfers here since spi_map_msg is done on the |
| 1751 | * splited transfers. |
| 1752 | */ |
| 1753 | spi_res_release(ctlr, mesg); |
| 1754 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1755 | if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { |
| 1756 | ret = ctlr->unprepare_message(ctlr, mesg); |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1757 | if (ret) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1758 | dev_err(&ctlr->dev, "failed to unprepare message: %d\n", |
| 1759 | ret); |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1760 | } |
| 1761 | } |
Uwe Kleine-König | 391949b | 2015-03-18 11:27:28 +0100 | [diff] [blame] | 1762 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1763 | spin_lock_irqsave(&ctlr->queue_lock, flags); |
| 1764 | ctlr->cur_msg = NULL; |
| 1765 | ctlr->cur_msg_prepared = false; |
Robin Gong | 809b1b0 | 2020-06-17 06:42:08 +0800 | [diff] [blame] | 1766 | ctlr->fallback = false; |
Marek Szyprowski | 60a883d | 2020-07-09 08:50:07 +0200 | [diff] [blame] | 1767 | kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1768 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Martin Sperl | 8e76ef8 | 2015-05-10 07:50:45 +0000 | [diff] [blame] | 1769 | |
| 1770 | trace_spi_message_done(mesg); |
Mark Brown | 2841a5f | 2013-10-05 00:23:12 +0100 | [diff] [blame] | 1771 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1772 | mesg->state = NULL; |
| 1773 | if (mesg->complete) |
| 1774 | mesg->complete(mesg->context); |
| 1775 | } |
| 1776 | EXPORT_SYMBOL_GPL(spi_finalize_current_message); |
| 1777 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1778 | static int spi_start_queue(struct spi_controller *ctlr) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1779 | { |
| 1780 | unsigned long flags; |
| 1781 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1782 | spin_lock_irqsave(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1783 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1784 | if (ctlr->running || ctlr->busy) { |
| 1785 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1786 | return -EBUSY; |
| 1787 | } |
| 1788 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1789 | ctlr->running = true; |
| 1790 | ctlr->cur_msg = NULL; |
| 1791 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1792 | |
Marek Szyprowski | 60a883d | 2020-07-09 08:50:07 +0200 | [diff] [blame] | 1793 | kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1794 | |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1798 | static int spi_stop_queue(struct spi_controller *ctlr) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1799 | { |
| 1800 | unsigned long flags; |
| 1801 | unsigned limit = 500; |
| 1802 | int ret = 0; |
| 1803 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1804 | spin_lock_irqsave(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1805 | |
| 1806 | /* |
| 1807 | * 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] | 1808 | * 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] | 1809 | * execution path (pump_messages) would be required to call wake_up or |
| 1810 | * friends on every SPI message. Do this instead. |
| 1811 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1812 | while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { |
| 1813 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Axel Lin | f97b26b | 2014-02-21 09:15:18 +0800 | [diff] [blame] | 1814 | usleep_range(10000, 11000); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1815 | spin_lock_irqsave(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1816 | } |
| 1817 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1818 | if (!list_empty(&ctlr->queue) || ctlr->busy) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1819 | ret = -EBUSY; |
| 1820 | else |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1821 | ctlr->running = false; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1822 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1823 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1824 | |
| 1825 | if (ret) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1826 | dev_warn(&ctlr->dev, "could not stop message queue\n"); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1827 | return ret; |
| 1828 | } |
| 1829 | return ret; |
| 1830 | } |
| 1831 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1832 | static int spi_destroy_queue(struct spi_controller *ctlr) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1833 | { |
| 1834 | int ret; |
| 1835 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1836 | ret = spi_stop_queue(ctlr); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1837 | |
| 1838 | /* |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 1839 | * kthread_flush_worker will block until all work is done. |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1840 | * If the reason that stop_queue timed out is that the work will never |
| 1841 | * finish, then it does no good to call flush/stop thread, so |
| 1842 | * return anyway. |
| 1843 | */ |
| 1844 | if (ret) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1845 | dev_err(&ctlr->dev, "problem destroying queue\n"); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1846 | return ret; |
| 1847 | } |
| 1848 | |
Marek Szyprowski | 60a883d | 2020-07-09 08:50:07 +0200 | [diff] [blame] | 1849 | kthread_destroy_worker(ctlr->kworker); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1850 | |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1854 | static int __spi_queued_transfer(struct spi_device *spi, |
| 1855 | struct spi_message *msg, |
| 1856 | bool need_pump) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1857 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1858 | struct spi_controller *ctlr = spi->controller; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1859 | unsigned long flags; |
| 1860 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1861 | spin_lock_irqsave(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1862 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1863 | if (!ctlr->running) { |
| 1864 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1865 | return -ESHUTDOWN; |
| 1866 | } |
| 1867 | msg->actual_length = 0; |
| 1868 | msg->status = -EINPROGRESS; |
| 1869 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1870 | list_add_tail(&msg->queue, &ctlr->queue); |
Mark Brown | f0125f1 | 2019-01-23 17:29:53 +0000 | [diff] [blame] | 1871 | if (!ctlr->busy && need_pump) |
Marek Szyprowski | 60a883d | 2020-07-09 08:50:07 +0200 | [diff] [blame] | 1872 | kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1873 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1874 | spin_unlock_irqrestore(&ctlr->queue_lock, flags); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1875 | return 0; |
| 1876 | } |
| 1877 | |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1878 | /** |
| 1879 | * spi_queued_transfer - transfer function for queued transfers |
| 1880 | * @spi: spi device which is requesting transfer |
| 1881 | * @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] | 1882 | * |
| 1883 | * Return: zero on success, else a negative error code. |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 1884 | */ |
| 1885 | static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) |
| 1886 | { |
| 1887 | return __spi_queued_transfer(spi, msg, true); |
| 1888 | } |
| 1889 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1890 | static int spi_controller_initialize_queue(struct spi_controller *ctlr) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1891 | { |
| 1892 | int ret; |
| 1893 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1894 | ctlr->transfer = spi_queued_transfer; |
| 1895 | if (!ctlr->transfer_one_message) |
| 1896 | ctlr->transfer_one_message = spi_transfer_one_message; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1897 | |
| 1898 | /* Initialize and start queue */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1899 | ret = spi_init_queue(ctlr); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1900 | if (ret) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1901 | dev_err(&ctlr->dev, "problem initializing queue\n"); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1902 | goto err_init_queue; |
| 1903 | } |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1904 | ctlr->queued = true; |
| 1905 | ret = spi_start_queue(ctlr); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1906 | if (ret) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1907 | dev_err(&ctlr->dev, "problem starting queue\n"); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1908 | goto err_start_queue; |
| 1909 | } |
| 1910 | |
| 1911 | return 0; |
| 1912 | |
| 1913 | err_start_queue: |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1914 | spi_destroy_queue(ctlr); |
Mark Brown | c3676d5 | 2014-05-01 10:47:52 -0700 | [diff] [blame] | 1915 | err_init_queue: |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1916 | return ret; |
| 1917 | } |
| 1918 | |
Boris Brezillon | 988f259 | 2018-04-22 20:35:15 +0200 | [diff] [blame] | 1919 | /** |
| 1920 | * spi_flush_queue - Send all pending messages in the queue from the callers' |
| 1921 | * context |
| 1922 | * @ctlr: controller to process queue for |
| 1923 | * |
| 1924 | * This should be used when one wants to ensure all pending messages have been |
| 1925 | * sent before doing something. Is used by the spi-mem code to make sure SPI |
| 1926 | * memory operations do not preempt regular SPI transfers that have been queued |
| 1927 | * before the spi-mem operation. |
| 1928 | */ |
| 1929 | void spi_flush_queue(struct spi_controller *ctlr) |
| 1930 | { |
| 1931 | if (ctlr->transfer == spi_queued_transfer) |
| 1932 | __spi_pump_messages(ctlr, false); |
| 1933 | } |
| 1934 | |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 1935 | /*-------------------------------------------------------------------------*/ |
| 1936 | |
Andreas Larsson | 7cb9436 | 2012-12-04 15:09:38 +0100 | [diff] [blame] | 1937 | #if defined(CONFIG_OF) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1938 | 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] | 1939 | struct device_node *nc) |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1940 | { |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1941 | u32 value; |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 1942 | int rc; |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1943 | |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1944 | /* Mode (clock phase/polarity/etc.) */ |
Sergei Shtylyov | e0bcb68 | 2017-08-06 23:15:31 +0300 | [diff] [blame] | 1945 | if (of_property_read_bool(nc, "spi-cpha")) |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1946 | spi->mode |= SPI_CPHA; |
Sergei Shtylyov | e0bcb68 | 2017-08-06 23:15:31 +0300 | [diff] [blame] | 1947 | if (of_property_read_bool(nc, "spi-cpol")) |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1948 | spi->mode |= SPI_CPOL; |
Sergei Shtylyov | e0bcb68 | 2017-08-06 23:15:31 +0300 | [diff] [blame] | 1949 | if (of_property_read_bool(nc, "spi-3wire")) |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1950 | spi->mode |= SPI_3WIRE; |
Sergei Shtylyov | e0bcb68 | 2017-08-06 23:15:31 +0300 | [diff] [blame] | 1951 | if (of_property_read_bool(nc, "spi-lsb-first")) |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1952 | spi->mode |= SPI_LSB_FIRST; |
Gregory CLEMENT | 3e5ec1d | 2019-10-18 17:29:29 +0200 | [diff] [blame] | 1953 | if (of_property_read_bool(nc, "spi-cs-high")) |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 1954 | spi->mode |= SPI_CS_HIGH; |
| 1955 | |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1956 | /* Device DUAL/QUAD mode */ |
| 1957 | if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { |
| 1958 | switch (value) { |
| 1959 | case 1: |
| 1960 | break; |
| 1961 | case 2: |
| 1962 | spi->mode |= SPI_TX_DUAL; |
| 1963 | break; |
| 1964 | case 4: |
| 1965 | spi->mode |= SPI_TX_QUAD; |
| 1966 | break; |
Yogesh Narayan Gaur | 6b03061 | 2018-12-03 08:39:06 +0000 | [diff] [blame] | 1967 | case 8: |
| 1968 | spi->mode |= SPI_TX_OCTAL; |
| 1969 | break; |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1970 | default: |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1971 | dev_warn(&ctlr->dev, |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1972 | "spi-tx-bus-width %d not supported\n", |
| 1973 | value); |
| 1974 | break; |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { |
| 1979 | switch (value) { |
| 1980 | case 1: |
| 1981 | break; |
| 1982 | case 2: |
| 1983 | spi->mode |= SPI_RX_DUAL; |
| 1984 | break; |
| 1985 | case 4: |
| 1986 | spi->mode |= SPI_RX_QUAD; |
| 1987 | break; |
Yogesh Narayan Gaur | 6b03061 | 2018-12-03 08:39:06 +0000 | [diff] [blame] | 1988 | case 8: |
| 1989 | spi->mode |= SPI_RX_OCTAL; |
| 1990 | break; |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1991 | default: |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1992 | dev_warn(&ctlr->dev, |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 1993 | "spi-rx-bus-width %d not supported\n", |
| 1994 | value); |
| 1995 | break; |
| 1996 | } |
| 1997 | } |
| 1998 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 1999 | if (spi_controller_is_slave(ctlr)) { |
Rob Herring | 194276b | 2018-12-05 13:50:41 -0600 | [diff] [blame] | 2000 | if (!of_node_name_eq(nc, "slave")) { |
Rob Herring | 25c56c8 | 2017-07-18 16:43:31 -0500 | [diff] [blame] | 2001 | dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", |
| 2002 | nc); |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2003 | return -EINVAL; |
| 2004 | } |
| 2005 | return 0; |
| 2006 | } |
| 2007 | |
| 2008 | /* Device address */ |
| 2009 | rc = of_property_read_u32(nc, "reg", &value); |
| 2010 | if (rc) { |
Rob Herring | 25c56c8 | 2017-07-18 16:43:31 -0500 | [diff] [blame] | 2011 | dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", |
| 2012 | nc, rc); |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2013 | return rc; |
| 2014 | } |
| 2015 | spi->chip_select = value; |
| 2016 | |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 2017 | /* Device speed */ |
Chuanhong Guo | 671c3bf | 2020-03-06 16:50:49 +0800 | [diff] [blame] | 2018 | if (!of_property_read_u32(nc, "spi-max-frequency", &value)) |
| 2019 | spi->max_speed_hz = value; |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 2020 | |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 2021 | return 0; |
| 2022 | } |
| 2023 | |
| 2024 | static struct spi_device * |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2025 | of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 2026 | { |
| 2027 | struct spi_device *spi; |
| 2028 | int rc; |
| 2029 | |
| 2030 | /* Alloc an spi_device */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2031 | spi = spi_alloc_device(ctlr); |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 2032 | if (!spi) { |
Rob Herring | 25c56c8 | 2017-07-18 16:43:31 -0500 | [diff] [blame] | 2033 | dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 2034 | rc = -ENOMEM; |
| 2035 | goto err_out; |
| 2036 | } |
| 2037 | |
| 2038 | /* Select device driver */ |
| 2039 | rc = of_modalias_node(nc, spi->modalias, |
| 2040 | sizeof(spi->modalias)); |
| 2041 | if (rc < 0) { |
Rob Herring | 25c56c8 | 2017-07-18 16:43:31 -0500 | [diff] [blame] | 2042 | dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 2043 | goto err_out; |
| 2044 | } |
| 2045 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2046 | rc = of_spi_parse_dt(ctlr, spi, nc); |
Geert Uytterhoeven | c2e51ac | 2016-09-12 22:50:41 +0200 | [diff] [blame] | 2047 | if (rc) |
| 2048 | goto err_out; |
| 2049 | |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 2050 | /* Store a pointer to the node in the device structure */ |
| 2051 | of_node_get(nc); |
| 2052 | spi->dev.of_node = nc; |
Charles Keepax | 61a7a63 | 2021-04-21 11:14:02 +0100 | [diff] [blame] | 2053 | spi->dev.fwnode = of_fwnode_handle(nc); |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 2054 | |
| 2055 | /* Register the new device */ |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 2056 | rc = spi_add_device(spi); |
| 2057 | if (rc) { |
Rob Herring | 25c56c8 | 2017-07-18 16:43:31 -0500 | [diff] [blame] | 2058 | dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); |
Johan Hovold | 8324147 | 2017-01-30 17:47:05 +0100 | [diff] [blame] | 2059 | goto err_of_node_put; |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | return spi; |
| 2063 | |
Johan Hovold | 8324147 | 2017-01-30 17:47:05 +0100 | [diff] [blame] | 2064 | err_of_node_put: |
| 2065 | of_node_put(nc); |
Pantelis Antoniou | aff5e3f | 2014-10-29 10:40:37 +0200 | [diff] [blame] | 2066 | err_out: |
| 2067 | spi_dev_put(spi); |
| 2068 | return ERR_PTR(rc); |
| 2069 | } |
| 2070 | |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 2071 | /** |
| 2072 | * of_register_spi_devices() - Register child devices onto the SPI bus |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2073 | * @ctlr: Pointer to spi_controller device |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 2074 | * |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2075 | * Registers an spi_device for each child node of controller node which |
| 2076 | * represents a valid SPI slave. |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 2077 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2078 | static void of_register_spi_devices(struct spi_controller *ctlr) |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 2079 | { |
| 2080 | struct spi_device *spi; |
| 2081 | struct device_node *nc; |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 2082 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2083 | if (!ctlr->dev.of_node) |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 2084 | return; |
| 2085 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2086 | for_each_available_child_of_node(ctlr->dev.of_node, nc) { |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 2087 | if (of_node_test_and_set_flag(nc, OF_POPULATED)) |
| 2088 | continue; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2089 | spi = of_register_spi_device(ctlr, nc); |
Ralf Ramsauer | e0af98a | 2016-10-17 15:59:56 +0200 | [diff] [blame] | 2090 | if (IS_ERR(spi)) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2091 | dev_warn(&ctlr->dev, |
Rob Herring | 25c56c8 | 2017-07-18 16:43:31 -0500 | [diff] [blame] | 2092 | "Failed to create SPI device for %pOF\n", nc); |
Ralf Ramsauer | e0af98a | 2016-10-17 15:59:56 +0200 | [diff] [blame] | 2093 | of_node_clear_flag(nc, OF_POPULATED); |
| 2094 | } |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 2095 | } |
| 2096 | } |
| 2097 | #else |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2098 | static void of_register_spi_devices(struct spi_controller *ctlr) { } |
Grant Likely | d57a428 | 2012-04-07 14:16:53 -0600 | [diff] [blame] | 2099 | #endif |
| 2100 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2101 | #ifdef CONFIG_ACPI |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2102 | struct acpi_spi_lookup { |
| 2103 | struct spi_controller *ctlr; |
| 2104 | u32 max_speed_hz; |
| 2105 | u32 mode; |
| 2106 | int irq; |
| 2107 | u8 bits_per_word; |
| 2108 | u8 chip_select; |
| 2109 | }; |
| 2110 | |
| 2111 | static void acpi_spi_parse_apple_properties(struct acpi_device *dev, |
| 2112 | struct acpi_spi_lookup *lookup) |
Lukas Wunner | 8a2e487 | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 2113 | { |
Lukas Wunner | 8a2e487 | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 2114 | const union acpi_object *obj; |
| 2115 | |
| 2116 | if (!x86_apple_machine) |
| 2117 | return; |
| 2118 | |
| 2119 | if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) |
| 2120 | && obj->buffer.length >= 4) |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2121 | lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; |
Lukas Wunner | 8a2e487 | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 2122 | |
| 2123 | if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) |
| 2124 | && obj->buffer.length == 8) |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2125 | lookup->bits_per_word = *(u64 *)obj->buffer.pointer; |
Lukas Wunner | 8a2e487 | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 2126 | |
| 2127 | if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) |
| 2128 | && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2129 | lookup->mode |= SPI_LSB_FIRST; |
Lukas Wunner | 8a2e487 | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 2130 | |
| 2131 | if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) |
| 2132 | && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2133 | lookup->mode |= SPI_CPOL; |
Lukas Wunner | 8a2e487 | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 2134 | |
| 2135 | if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) |
| 2136 | && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2137 | lookup->mode |= SPI_CPHA; |
Lukas Wunner | 8a2e487 | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 2138 | } |
| 2139 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2140 | static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) |
| 2141 | { |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2142 | struct acpi_spi_lookup *lookup = data; |
| 2143 | struct spi_controller *ctlr = lookup->ctlr; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2144 | |
| 2145 | if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { |
| 2146 | struct acpi_resource_spi_serialbus *sb; |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2147 | acpi_handle parent_handle; |
| 2148 | acpi_status status; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2149 | |
| 2150 | sb = &ares->data.spi_serial_bus; |
| 2151 | if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2152 | |
| 2153 | status = acpi_get_handle(NULL, |
| 2154 | sb->resource_source.string_ptr, |
| 2155 | &parent_handle); |
| 2156 | |
Ard Biesheuvel | b5e3cf4 | 2019-06-19 11:52:54 +0200 | [diff] [blame] | 2157 | if (ACPI_FAILURE(status) || |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2158 | ACPI_HANDLE(ctlr->dev.parent) != parent_handle) |
| 2159 | return -ENODEV; |
| 2160 | |
Mika Westerberg | a0a9071 | 2016-02-08 17:14:28 +0200 | [diff] [blame] | 2161 | /* |
| 2162 | * ACPI DeviceSelection numbering is handled by the |
| 2163 | * host controller driver in Windows and can vary |
| 2164 | * from driver to driver. In Linux we always expect |
| 2165 | * 0 .. max - 1 so we need to ask the driver to |
| 2166 | * translate between the two schemes. |
| 2167 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2168 | if (ctlr->fw_translate_cs) { |
| 2169 | int cs = ctlr->fw_translate_cs(ctlr, |
Mika Westerberg | a0a9071 | 2016-02-08 17:14:28 +0200 | [diff] [blame] | 2170 | sb->device_selection); |
| 2171 | if (cs < 0) |
| 2172 | return cs; |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2173 | lookup->chip_select = cs; |
Mika Westerberg | a0a9071 | 2016-02-08 17:14:28 +0200 | [diff] [blame] | 2174 | } else { |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2175 | lookup->chip_select = sb->device_selection; |
Mika Westerberg | a0a9071 | 2016-02-08 17:14:28 +0200 | [diff] [blame] | 2176 | } |
| 2177 | |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2178 | lookup->max_speed_hz = sb->connection_speed; |
Andy Shevchenko | 0dadde3 | 2020-04-13 21:04:06 +0300 | [diff] [blame] | 2179 | lookup->bits_per_word = sb->data_bit_length; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2180 | |
| 2181 | if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2182 | lookup->mode |= SPI_CPHA; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2183 | if (sb->clock_polarity == ACPI_SPI_START_HIGH) |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2184 | lookup->mode |= SPI_CPOL; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2185 | if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2186 | lookup->mode |= SPI_CS_HIGH; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2187 | } |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2188 | } else if (lookup->irq < 0) { |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2189 | struct resource r; |
| 2190 | |
| 2191 | if (acpi_dev_resource_interrupt(ares, 0, &r)) |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2192 | lookup->irq = r.start; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | /* Always tell the ACPI core to skip this resource */ |
| 2196 | return 1; |
| 2197 | } |
| 2198 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2199 | static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 2200 | struct acpi_device *adev) |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2201 | { |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2202 | acpi_handle parent_handle = NULL; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2203 | struct list_head resource_list; |
Ard Biesheuvel | b28944c | 2019-06-20 14:36:49 +0200 | [diff] [blame] | 2204 | struct acpi_spi_lookup lookup = {}; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2205 | struct spi_device *spi; |
| 2206 | int ret; |
| 2207 | |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 2208 | if (acpi_bus_get_status(adev) || !adev->status.present || |
| 2209 | acpi_device_enumerated(adev)) |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2210 | return AE_OK; |
| 2211 | |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2212 | lookup.ctlr = ctlr; |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2213 | lookup.irq = -1; |
| 2214 | |
| 2215 | INIT_LIST_HEAD(&resource_list); |
| 2216 | ret = acpi_dev_get_resources(adev, &resource_list, |
| 2217 | acpi_spi_add_resource, &lookup); |
| 2218 | acpi_dev_free_resource_list(&resource_list); |
| 2219 | |
| 2220 | if (ret < 0) |
| 2221 | /* found SPI in _CRS but it points to another controller */ |
| 2222 | return AE_OK; |
| 2223 | |
| 2224 | if (!lookup.max_speed_hz && |
| 2225 | !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) && |
| 2226 | ACPI_HANDLE(ctlr->dev.parent) == parent_handle) { |
| 2227 | /* Apple does not use _CRS but nested devices for SPI slaves */ |
| 2228 | acpi_spi_parse_apple_properties(adev, &lookup); |
| 2229 | } |
| 2230 | |
| 2231 | if (!lookup.max_speed_hz) |
| 2232 | return AE_OK; |
| 2233 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2234 | spi = spi_alloc_device(ctlr); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2235 | if (!spi) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2236 | dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2237 | dev_name(&adev->dev)); |
| 2238 | return AE_NO_MEMORY; |
| 2239 | } |
| 2240 | |
John Garry | ea23578 | 2020-02-28 23:18:49 +0800 | [diff] [blame] | 2241 | |
Rafael J. Wysocki | 7b19981 | 2013-11-11 22:41:56 +0100 | [diff] [blame] | 2242 | ACPI_COMPANION_SET(&spi->dev, adev); |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2243 | spi->max_speed_hz = lookup.max_speed_hz; |
John Garry | ea23578 | 2020-02-28 23:18:49 +0800 | [diff] [blame] | 2244 | spi->mode |= lookup.mode; |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2245 | spi->irq = lookup.irq; |
| 2246 | spi->bits_per_word = lookup.bits_per_word; |
| 2247 | spi->chip_select = lookup.chip_select; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2248 | |
Dan O'Donovan | 0c6543f | 2017-02-05 16:30:14 +0000 | [diff] [blame] | 2249 | acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, |
| 2250 | sizeof(spi->modalias)); |
| 2251 | |
Christophe RICARD | 33ada67 | 2015-12-23 23:25:35 +0100 | [diff] [blame] | 2252 | if (spi->irq < 0) |
| 2253 | spi->irq = acpi_dev_gpio_irq_get(adev, 0); |
| 2254 | |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 2255 | acpi_device_set_enumerated(adev); |
| 2256 | |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 2257 | adev->power.flags.ignore_parent = true; |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2258 | if (spi_add_device(spi)) { |
Mika Westerberg | 33cf00e | 2013-10-10 13:28:48 +0300 | [diff] [blame] | 2259 | adev->power.flags.ignore_parent = false; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2260 | 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] | 2261 | dev_name(&adev->dev)); |
| 2262 | spi_dev_put(spi); |
| 2263 | } |
| 2264 | |
| 2265 | return AE_OK; |
| 2266 | } |
| 2267 | |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 2268 | static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, |
| 2269 | void *data, void **return_value) |
| 2270 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2271 | struct spi_controller *ctlr = data; |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 2272 | struct acpi_device *adev; |
| 2273 | |
| 2274 | if (acpi_bus_get_device(handle, &adev)) |
| 2275 | return AE_OK; |
| 2276 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2277 | return acpi_register_spi_device(ctlr, adev); |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 2278 | } |
| 2279 | |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2280 | #define SPI_ACPI_ENUMERATE_MAX_DEPTH 32 |
| 2281 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2282 | static void acpi_register_spi_devices(struct spi_controller *ctlr) |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2283 | { |
| 2284 | acpi_status status; |
| 2285 | acpi_handle handle; |
| 2286 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2287 | handle = ACPI_HANDLE(ctlr->dev.parent); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2288 | if (!handle) |
| 2289 | return; |
| 2290 | |
Ard Biesheuvel | 4c3c595 | 2019-05-30 13:16:34 +0200 | [diff] [blame] | 2291 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
| 2292 | SPI_ACPI_ENUMERATE_MAX_DEPTH, |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2293 | acpi_spi_add_device, NULL, ctlr, NULL); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2294 | if (ACPI_FAILURE(status)) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2295 | dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2296 | } |
| 2297 | #else |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2298 | static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2299 | #endif /* CONFIG_ACPI */ |
| 2300 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2301 | static void spi_controller_release(struct device *dev) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2302 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2303 | struct spi_controller *ctlr; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2304 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2305 | ctlr = container_of(dev, struct spi_controller, dev); |
| 2306 | kfree(ctlr); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | static struct class spi_master_class = { |
| 2310 | .name = "spi_master", |
| 2311 | .owner = THIS_MODULE, |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2312 | .dev_release = spi_controller_release, |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 2313 | .dev_groups = spi_master_groups, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2314 | }; |
| 2315 | |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2316 | #ifdef CONFIG_SPI_SLAVE |
| 2317 | /** |
| 2318 | * spi_slave_abort - abort the ongoing transfer request on an SPI slave |
| 2319 | * controller |
| 2320 | * @spi: device used for the current transfer |
| 2321 | */ |
| 2322 | int spi_slave_abort(struct spi_device *spi) |
| 2323 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2324 | struct spi_controller *ctlr = spi->controller; |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2325 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2326 | if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) |
| 2327 | return ctlr->slave_abort(ctlr); |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2328 | |
| 2329 | return -ENOTSUPP; |
| 2330 | } |
| 2331 | EXPORT_SYMBOL_GPL(spi_slave_abort); |
| 2332 | |
| 2333 | static int match_true(struct device *dev, void *data) |
| 2334 | { |
| 2335 | return 1; |
| 2336 | } |
| 2337 | |
Geert Uytterhoeven | cc8b465 | 2019-07-31 14:47:38 +0200 | [diff] [blame] | 2338 | static ssize_t slave_show(struct device *dev, struct device_attribute *attr, |
| 2339 | char *buf) |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2340 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2341 | struct spi_controller *ctlr = container_of(dev, struct spi_controller, |
| 2342 | dev); |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2343 | struct device *child; |
| 2344 | |
| 2345 | child = device_find_child(&ctlr->dev, NULL, match_true); |
| 2346 | return sprintf(buf, "%s\n", |
| 2347 | child ? to_spi_device(child)->modalias : NULL); |
| 2348 | } |
| 2349 | |
Geert Uytterhoeven | cc8b465 | 2019-07-31 14:47:38 +0200 | [diff] [blame] | 2350 | static ssize_t slave_store(struct device *dev, struct device_attribute *attr, |
| 2351 | const char *buf, size_t count) |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2352 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2353 | struct spi_controller *ctlr = container_of(dev, struct spi_controller, |
| 2354 | dev); |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2355 | struct spi_device *spi; |
| 2356 | struct device *child; |
| 2357 | char name[32]; |
| 2358 | int rc; |
| 2359 | |
| 2360 | rc = sscanf(buf, "%31s", name); |
| 2361 | if (rc != 1 || !name[0]) |
| 2362 | return -EINVAL; |
| 2363 | |
| 2364 | child = device_find_child(&ctlr->dev, NULL, match_true); |
| 2365 | if (child) { |
| 2366 | /* Remove registered slave */ |
| 2367 | device_unregister(child); |
| 2368 | put_device(child); |
| 2369 | } |
| 2370 | |
| 2371 | if (strcmp(name, "(null)")) { |
| 2372 | /* Register new slave */ |
| 2373 | spi = spi_alloc_device(ctlr); |
| 2374 | if (!spi) |
| 2375 | return -ENOMEM; |
| 2376 | |
| 2377 | strlcpy(spi->modalias, name, sizeof(spi->modalias)); |
| 2378 | |
| 2379 | rc = spi_add_device(spi); |
| 2380 | if (rc) { |
| 2381 | spi_dev_put(spi); |
| 2382 | return rc; |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | return count; |
| 2387 | } |
| 2388 | |
Geert Uytterhoeven | cc8b465 | 2019-07-31 14:47:38 +0200 | [diff] [blame] | 2389 | static DEVICE_ATTR_RW(slave); |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2390 | |
| 2391 | static struct attribute *spi_slave_attrs[] = { |
| 2392 | &dev_attr_slave.attr, |
| 2393 | NULL, |
| 2394 | }; |
| 2395 | |
| 2396 | static const struct attribute_group spi_slave_group = { |
| 2397 | .attrs = spi_slave_attrs, |
| 2398 | }; |
| 2399 | |
| 2400 | static const struct attribute_group *spi_slave_groups[] = { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2401 | &spi_controller_statistics_group, |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2402 | &spi_slave_group, |
| 2403 | NULL, |
| 2404 | }; |
| 2405 | |
| 2406 | static struct class spi_slave_class = { |
| 2407 | .name = "spi_slave", |
| 2408 | .owner = THIS_MODULE, |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2409 | .dev_release = spi_controller_release, |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2410 | .dev_groups = spi_slave_groups, |
| 2411 | }; |
| 2412 | #else |
| 2413 | extern struct class spi_slave_class; /* dummy */ |
| 2414 | #endif |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2415 | |
| 2416 | /** |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2417 | * __spi_alloc_controller - allocate an SPI master or slave controller |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2418 | * @dev: the controller, possibly using the platform_bus |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2419 | * @size: how much zeroed driver-private data to allocate; the pointer to this |
Lukas Wunner | 229e6af | 2019-09-11 12:15:30 +0200 | [diff] [blame] | 2420 | * memory is in the driver_data field of the returned device, accessible |
| 2421 | * with spi_controller_get_devdata(); the memory is cacheline aligned; |
| 2422 | * drivers granting DMA access to portions of their private data need to |
| 2423 | * round up @size using ALIGN(size, dma_get_cache_alignment()). |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2424 | * @slave: flag indicating whether to allocate an SPI master (false) or SPI |
| 2425 | * slave (true) controller |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2426 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2427 | * |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2428 | * This call is used only by SPI controller drivers, which are the |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2429 | * only ones directly touching chip registers. It's how they allocate |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2430 | * an spi_controller structure, prior to calling spi_register_controller(). |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2431 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2432 | * This must be called from context that can sleep. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2433 | * |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2434 | * The caller is responsible for assigning the bus number and initializing the |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2435 | * controller's methods before calling spi_register_controller(); and (after |
| 2436 | * errors adding the device) calling spi_controller_put() to prevent a memory |
| 2437 | * leak. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2438 | * |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2439 | * Return: the SPI controller structure on success, else NULL. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2440 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2441 | struct spi_controller *__spi_alloc_controller(struct device *dev, |
| 2442 | unsigned int size, bool slave) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2443 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2444 | struct spi_controller *ctlr; |
Lukas Wunner | 229e6af | 2019-09-11 12:15:30 +0200 | [diff] [blame] | 2445 | size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment()); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2446 | |
David Brownell | 0c868461 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 2447 | if (!dev) |
| 2448 | return NULL; |
| 2449 | |
Lukas Wunner | 229e6af | 2019-09-11 12:15:30 +0200 | [diff] [blame] | 2450 | ctlr = kzalloc(size + ctlr_size, GFP_KERNEL); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2451 | if (!ctlr) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2452 | return NULL; |
| 2453 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2454 | device_initialize(&ctlr->dev); |
| 2455 | ctlr->bus_num = -1; |
| 2456 | ctlr->num_chipselect = 1; |
| 2457 | ctlr->slave = slave; |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2458 | if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2459 | ctlr->dev.class = &spi_slave_class; |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2460 | else |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2461 | ctlr->dev.class = &spi_master_class; |
| 2462 | ctlr->dev.parent = dev; |
| 2463 | pm_suspend_ignore_children(&ctlr->dev, true); |
Lukas Wunner | 229e6af | 2019-09-11 12:15:30 +0200 | [diff] [blame] | 2464 | spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2465 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2466 | return ctlr; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2467 | } |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 2468 | EXPORT_SYMBOL_GPL(__spi_alloc_controller); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2469 | |
Lukas Wunner | 5e844cc | 2020-11-11 20:07:10 +0100 | [diff] [blame] | 2470 | static void devm_spi_release_controller(struct device *dev, void *ctlr) |
| 2471 | { |
| 2472 | spi_controller_put(*(struct spi_controller **)ctlr); |
| 2473 | } |
| 2474 | |
| 2475 | /** |
| 2476 | * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller() |
| 2477 | * @dev: physical device of SPI controller |
| 2478 | * @size: how much zeroed driver-private data to allocate |
| 2479 | * @slave: whether to allocate an SPI master (false) or SPI slave (true) |
| 2480 | * Context: can sleep |
| 2481 | * |
| 2482 | * Allocate an SPI controller and automatically release a reference on it |
| 2483 | * when @dev is unbound from its driver. Drivers are thus relieved from |
| 2484 | * having to call spi_controller_put(). |
| 2485 | * |
| 2486 | * The arguments to this function are identical to __spi_alloc_controller(). |
| 2487 | * |
| 2488 | * Return: the SPI controller structure on success, else NULL. |
| 2489 | */ |
| 2490 | struct spi_controller *__devm_spi_alloc_controller(struct device *dev, |
| 2491 | unsigned int size, |
| 2492 | bool slave) |
| 2493 | { |
| 2494 | struct spi_controller **ptr, *ctlr; |
| 2495 | |
| 2496 | ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr), |
| 2497 | GFP_KERNEL); |
| 2498 | if (!ptr) |
| 2499 | return NULL; |
| 2500 | |
| 2501 | ctlr = __spi_alloc_controller(dev, size, slave); |
| 2502 | if (ctlr) { |
William A. Kennington III | c7fabe3 | 2021-04-07 02:55:27 -0700 | [diff] [blame] | 2503 | ctlr->devm_allocated = true; |
Lukas Wunner | 5e844cc | 2020-11-11 20:07:10 +0100 | [diff] [blame] | 2504 | *ptr = ctlr; |
| 2505 | devres_add(dev, ptr); |
| 2506 | } else { |
| 2507 | devres_free(ptr); |
| 2508 | } |
| 2509 | |
| 2510 | return ctlr; |
| 2511 | } |
| 2512 | EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller); |
| 2513 | |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2514 | #ifdef CONFIG_OF |
Linus Walleij | 43004f3 | 2019-08-08 17:03:21 +0200 | [diff] [blame] | 2515 | static int of_spi_get_gpio_numbers(struct spi_controller *ctlr) |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2516 | { |
Grant Likely | e80beb2 | 2013-02-12 17:48:37 +0000 | [diff] [blame] | 2517 | int nb, i, *cs; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2518 | struct device_node *np = ctlr->dev.of_node; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2519 | |
| 2520 | if (!np) |
| 2521 | return 0; |
| 2522 | |
| 2523 | nb = of_gpio_named_count(np, "cs-gpios"); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2524 | ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2525 | |
Andreas Larsson | 8ec5d84 | 2013-02-13 14:23:24 +0100 | [diff] [blame] | 2526 | /* Return error only for an incorrectly formed cs-gpios property */ |
| 2527 | if (nb == 0 || nb == -ENOENT) |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2528 | return 0; |
Andreas Larsson | 8ec5d84 | 2013-02-13 14:23:24 +0100 | [diff] [blame] | 2529 | else if (nb < 0) |
| 2530 | return nb; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2531 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 2532 | cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2533 | GFP_KERNEL); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2534 | ctlr->cs_gpios = cs; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2535 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2536 | if (!ctlr->cs_gpios) |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2537 | return -ENOMEM; |
| 2538 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2539 | for (i = 0; i < ctlr->num_chipselect; i++) |
Andreas Larsson | 446411e | 2013-02-13 14:20:25 +0100 | [diff] [blame] | 2540 | cs[i] = -ENOENT; |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2541 | |
| 2542 | for (i = 0; i < nb; i++) |
| 2543 | cs[i] = of_get_named_gpio(np, "cs-gpios", i); |
| 2544 | |
| 2545 | return 0; |
| 2546 | } |
| 2547 | #else |
Linus Walleij | 43004f3 | 2019-08-08 17:03:21 +0200 | [diff] [blame] | 2548 | static int of_spi_get_gpio_numbers(struct spi_controller *ctlr) |
Jean-Christophe PLAGNIOL-VILLARD | 7431798 | 2012-11-15 20:19:57 +0100 | [diff] [blame] | 2549 | { |
| 2550 | return 0; |
| 2551 | } |
| 2552 | #endif |
| 2553 | |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 2554 | /** |
| 2555 | * spi_get_gpio_descs() - grab chip select GPIOs for the master |
| 2556 | * @ctlr: The SPI master to grab GPIO descriptors for |
| 2557 | */ |
| 2558 | static int spi_get_gpio_descs(struct spi_controller *ctlr) |
| 2559 | { |
| 2560 | int nb, i; |
| 2561 | struct gpio_desc **cs; |
| 2562 | struct device *dev = &ctlr->dev; |
Geert Uytterhoeven | 7d93aec | 2020-01-02 14:38:17 +0100 | [diff] [blame] | 2563 | unsigned long native_cs_mask = 0; |
| 2564 | unsigned int num_cs_gpios = 0; |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 2565 | |
| 2566 | nb = gpiod_count(dev, "cs"); |
| 2567 | ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); |
| 2568 | |
| 2569 | /* No GPIOs at all is fine, else return the error */ |
| 2570 | if (nb == 0 || nb == -ENOENT) |
| 2571 | return 0; |
| 2572 | else if (nb < 0) |
| 2573 | return nb; |
| 2574 | |
| 2575 | cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), |
| 2576 | GFP_KERNEL); |
| 2577 | if (!cs) |
| 2578 | return -ENOMEM; |
| 2579 | ctlr->cs_gpiods = cs; |
| 2580 | |
| 2581 | for (i = 0; i < nb; i++) { |
| 2582 | /* |
| 2583 | * Most chipselects are active low, the inverted |
| 2584 | * semantics are handled by special quirks in gpiolib, |
| 2585 | * so initializing them GPIOD_OUT_LOW here means |
| 2586 | * "unasserted", in most cases this will drive the physical |
| 2587 | * line high. |
| 2588 | */ |
| 2589 | cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, |
| 2590 | GPIOD_OUT_LOW); |
Geert Uytterhoeven | 1723fde | 2019-04-03 16:46:56 +0200 | [diff] [blame] | 2591 | if (IS_ERR(cs[i])) |
| 2592 | return PTR_ERR(cs[i]); |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 2593 | |
| 2594 | if (cs[i]) { |
| 2595 | /* |
| 2596 | * If we find a CS GPIO, name it after the device and |
| 2597 | * chip select line. |
| 2598 | */ |
| 2599 | char *gpioname; |
| 2600 | |
| 2601 | gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", |
| 2602 | dev_name(dev), i); |
| 2603 | if (!gpioname) |
| 2604 | return -ENOMEM; |
| 2605 | gpiod_set_consumer_name(cs[i], gpioname); |
Geert Uytterhoeven | 7d93aec | 2020-01-02 14:38:17 +0100 | [diff] [blame] | 2606 | num_cs_gpios++; |
| 2607 | continue; |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 2608 | } |
Geert Uytterhoeven | 7d93aec | 2020-01-02 14:38:17 +0100 | [diff] [blame] | 2609 | |
| 2610 | if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { |
| 2611 | dev_err(dev, "Invalid native chip select %d\n", i); |
| 2612 | return -EINVAL; |
| 2613 | } |
| 2614 | native_cs_mask |= BIT(i); |
| 2615 | } |
| 2616 | |
Andy Shevchenko | 08d0aa1 | 2021-04-20 19:44:25 +0300 | [diff] [blame] | 2617 | ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; |
Andy Shevchenko | db5a7e2 | 2021-04-20 19:44:24 +0300 | [diff] [blame] | 2618 | |
| 2619 | if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && |
| 2620 | ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { |
Geert Uytterhoeven | 7d93aec | 2020-01-02 14:38:17 +0100 | [diff] [blame] | 2621 | dev_err(dev, "No unused native chip select available\n"); |
| 2622 | return -EINVAL; |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | return 0; |
| 2626 | } |
| 2627 | |
Boris Brezillon | bdf3a3b | 2018-04-11 00:44:30 +0200 | [diff] [blame] | 2628 | static int spi_controller_check_ops(struct spi_controller *ctlr) |
| 2629 | { |
| 2630 | /* |
Boris Brezillon | b5932f5 | 2018-04-26 18:18:15 +0200 | [diff] [blame] | 2631 | * The controller may implement only the high-level SPI-memory like |
| 2632 | * operations if it does not support regular SPI transfers, and this is |
| 2633 | * valid use case. |
| 2634 | * If ->mem_ops is NULL, we request that at least one of the |
| 2635 | * ->transfer_xxx() method be implemented. |
Boris Brezillon | bdf3a3b | 2018-04-11 00:44:30 +0200 | [diff] [blame] | 2636 | */ |
Boris Brezillon | b5932f5 | 2018-04-26 18:18:15 +0200 | [diff] [blame] | 2637 | if (ctlr->mem_ops) { |
| 2638 | if (!ctlr->mem_ops->exec_op) |
| 2639 | return -EINVAL; |
| 2640 | } else if (!ctlr->transfer && !ctlr->transfer_one && |
| 2641 | !ctlr->transfer_one_message) { |
Boris Brezillon | bdf3a3b | 2018-04-11 00:44:30 +0200 | [diff] [blame] | 2642 | return -EINVAL; |
Boris Brezillon | b5932f5 | 2018-04-26 18:18:15 +0200 | [diff] [blame] | 2643 | } |
Boris Brezillon | bdf3a3b | 2018-04-11 00:44:30 +0200 | [diff] [blame] | 2644 | |
| 2645 | return 0; |
| 2646 | } |
| 2647 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2648 | /** |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2649 | * spi_register_controller - register SPI master or slave controller |
| 2650 | * @ctlr: initialized master, originally from spi_alloc_master() or |
| 2651 | * spi_alloc_slave() |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2652 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2653 | * |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2654 | * SPI controllers connect to their drivers using some non-SPI bus, |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2655 | * 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] | 2656 | * 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] | 2657 | * |
| 2658 | * SPI controllers use board specific (often SOC specific) bus numbers, |
| 2659 | * and board-specific addressing for SPI devices combines those numbers |
| 2660 | * with chip select numbers. Since SPI does not directly support dynamic |
| 2661 | * device identification, boards need configuration tables telling which |
| 2662 | * chip is at which address. |
| 2663 | * |
| 2664 | * 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] | 2665 | * success, else a negative error code (dropping the controller's refcount). |
David Brownell | 0c868461 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 2666 | * After a successful return, the caller is responsible for calling |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2667 | * spi_unregister_controller(). |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2668 | * |
| 2669 | * Return: zero on success, else a negative error code. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2670 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2671 | int spi_register_controller(struct spi_controller *ctlr) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2672 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2673 | struct device *dev = ctlr->dev.parent; |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 2674 | struct boardinfo *bi; |
Sergei Shtylyov | b93318a | 2019-04-05 18:48:50 +0300 | [diff] [blame] | 2675 | int status; |
Lucas Stach | 42bdd70 | 2017-10-16 12:27:58 +0200 | [diff] [blame] | 2676 | int id, first_dynamic; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2677 | |
David Brownell | 0c868461 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 2678 | if (!dev) |
| 2679 | return -ENODEV; |
| 2680 | |
Boris Brezillon | bdf3a3b | 2018-04-11 00:44:30 +0200 | [diff] [blame] | 2681 | /* |
| 2682 | * Make sure all necessary hooks are implemented before registering |
| 2683 | * the SPI controller. |
| 2684 | */ |
| 2685 | status = spi_controller_check_ops(ctlr); |
| 2686 | if (status) |
| 2687 | return status; |
| 2688 | |
Geert Uytterhoeven | 04b2d03 | 2018-08-21 11:53:03 +0200 | [diff] [blame] | 2689 | if (ctlr->bus_num >= 0) { |
| 2690 | /* devices with a fixed bus num must check-in with the num */ |
| 2691 | mutex_lock(&board_lock); |
| 2692 | id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, |
| 2693 | ctlr->bus_num + 1, GFP_KERNEL); |
| 2694 | mutex_unlock(&board_lock); |
| 2695 | if (WARN(id < 0, "couldn't get idr")) |
| 2696 | return id == -ENOSPC ? -EBUSY : id; |
| 2697 | ctlr->bus_num = id; |
| 2698 | } else if (ctlr->dev.of_node) { |
| 2699 | /* allocate dynamic bus number using Linux idr */ |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 2700 | id = of_alias_get_id(ctlr->dev.of_node, "spi"); |
| 2701 | if (id >= 0) { |
| 2702 | ctlr->bus_num = id; |
| 2703 | mutex_lock(&board_lock); |
| 2704 | id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, |
| 2705 | ctlr->bus_num + 1, GFP_KERNEL); |
| 2706 | mutex_unlock(&board_lock); |
| 2707 | if (WARN(id < 0, "couldn't get idr")) |
| 2708 | return id == -ENOSPC ? -EBUSY : id; |
| 2709 | } |
| 2710 | } |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2711 | if (ctlr->bus_num < 0) { |
Lucas Stach | 42bdd70 | 2017-10-16 12:27:58 +0200 | [diff] [blame] | 2712 | first_dynamic = of_alias_get_highest_id("spi"); |
| 2713 | if (first_dynamic < 0) |
| 2714 | first_dynamic = 0; |
| 2715 | else |
| 2716 | first_dynamic++; |
| 2717 | |
Suniel Mahesh | 9a9a047 | 2017-08-17 18:18:22 +0530 | [diff] [blame] | 2718 | mutex_lock(&board_lock); |
Lucas Stach | 42bdd70 | 2017-10-16 12:27:58 +0200 | [diff] [blame] | 2719 | id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, |
| 2720 | 0, GFP_KERNEL); |
Suniel Mahesh | 9a9a047 | 2017-08-17 18:18:22 +0530 | [diff] [blame] | 2721 | mutex_unlock(&board_lock); |
| 2722 | if (WARN(id < 0, "couldn't get idr")) |
| 2723 | return id; |
| 2724 | ctlr->bus_num = id; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2725 | } |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2726 | INIT_LIST_HEAD(&ctlr->queue); |
| 2727 | spin_lock_init(&ctlr->queue_lock); |
| 2728 | spin_lock_init(&ctlr->bus_lock_spinlock); |
| 2729 | mutex_init(&ctlr->bus_lock_mutex); |
| 2730 | mutex_init(&ctlr->io_mutex); |
| 2731 | ctlr->bus_lock_flag = 0; |
| 2732 | init_completion(&ctlr->xfer_completion); |
| 2733 | if (!ctlr->max_dma_len) |
| 2734 | ctlr->max_dma_len = INT_MAX; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 2735 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2736 | /* register the device, then userspace will see it. |
| 2737 | * registration fails if the bus ID is in use. |
| 2738 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2739 | dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); |
Andrey Smirnov | 0a919ae | 2019-04-02 21:01:28 -0700 | [diff] [blame] | 2740 | |
| 2741 | if (!spi_controller_is_slave(ctlr)) { |
| 2742 | if (ctlr->use_gpio_descriptors) { |
| 2743 | status = spi_get_gpio_descs(ctlr); |
| 2744 | if (status) |
Aaro Koskinen | f9981d4 | 2020-03-04 13:17:40 +0200 | [diff] [blame] | 2745 | goto free_bus_id; |
Andrey Smirnov | 0a919ae | 2019-04-02 21:01:28 -0700 | [diff] [blame] | 2746 | /* |
| 2747 | * A controller using GPIO descriptors always |
| 2748 | * supports SPI_CS_HIGH if need be. |
| 2749 | */ |
| 2750 | ctlr->mode_bits |= SPI_CS_HIGH; |
| 2751 | } else { |
| 2752 | /* Legacy code path for GPIOs from DT */ |
Linus Walleij | 43004f3 | 2019-08-08 17:03:21 +0200 | [diff] [blame] | 2753 | status = of_spi_get_gpio_numbers(ctlr); |
Andrey Smirnov | 0a919ae | 2019-04-02 21:01:28 -0700 | [diff] [blame] | 2754 | if (status) |
Aaro Koskinen | f9981d4 | 2020-03-04 13:17:40 +0200 | [diff] [blame] | 2755 | goto free_bus_id; |
Andrey Smirnov | 0a919ae | 2019-04-02 21:01:28 -0700 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | |
Tudor Ambarus | f9481b0 | 2019-06-19 14:38:28 +0000 | [diff] [blame] | 2759 | /* |
| 2760 | * Even if it's just one always-selected device, there must |
| 2761 | * be at least one chipselect. |
| 2762 | */ |
Aaro Koskinen | f9981d4 | 2020-03-04 13:17:40 +0200 | [diff] [blame] | 2763 | if (!ctlr->num_chipselect) { |
| 2764 | status = -EINVAL; |
| 2765 | goto free_bus_id; |
| 2766 | } |
Tudor Ambarus | f9481b0 | 2019-06-19 14:38:28 +0000 | [diff] [blame] | 2767 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2768 | status = device_add(&ctlr->dev); |
Aaro Koskinen | f9981d4 | 2020-03-04 13:17:40 +0200 | [diff] [blame] | 2769 | if (status < 0) |
| 2770 | goto free_bus_id; |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 2771 | dev_dbg(dev, "registered %s %s\n", |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2772 | spi_controller_is_slave(ctlr) ? "slave" : "master", |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 2773 | dev_name(&ctlr->dev)); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2774 | |
Boris Brezillon | b5932f5 | 2018-04-26 18:18:15 +0200 | [diff] [blame] | 2775 | /* |
| 2776 | * If we're using a queued driver, start the queue. Note that we don't |
| 2777 | * need the queueing logic if the driver is only supporting high-level |
| 2778 | * memory operations. |
| 2779 | */ |
| 2780 | if (ctlr->transfer) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2781 | dev_info(dev, "controller is unqueued, this is deprecated\n"); |
Boris Brezillon | b5932f5 | 2018-04-26 18:18:15 +0200 | [diff] [blame] | 2782 | } else if (ctlr->transfer_one || ctlr->transfer_one_message) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2783 | status = spi_controller_initialize_queue(ctlr); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2784 | if (status) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2785 | device_del(&ctlr->dev); |
Aaro Koskinen | f9981d4 | 2020-03-04 13:17:40 +0200 | [diff] [blame] | 2786 | goto free_bus_id; |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2787 | } |
| 2788 | } |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 2789 | /* add statistics */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2790 | spin_lock_init(&ctlr->statistics.lock); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2791 | |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 2792 | mutex_lock(&board_lock); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2793 | list_add_tail(&ctlr->list, &spi_controller_list); |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 2794 | list_for_each_entry(bi, &board_list, list) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2795 | spi_match_controller_to_boardinfo(ctlr, &bi->board_info); |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 2796 | mutex_unlock(&board_lock); |
| 2797 | |
Mika Westerberg | 64bee4d | 2012-11-30 12:37:53 +0100 | [diff] [blame] | 2798 | /* Register devices from the device tree and ACPI */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2799 | of_register_spi_devices(ctlr); |
| 2800 | acpi_register_spi_devices(ctlr); |
Aaro Koskinen | f9981d4 | 2020-03-04 13:17:40 +0200 | [diff] [blame] | 2801 | return status; |
| 2802 | |
| 2803 | free_bus_id: |
| 2804 | mutex_lock(&board_lock); |
| 2805 | idr_remove(&spi_master_idr, ctlr->bus_num); |
| 2806 | mutex_unlock(&board_lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2807 | return status; |
| 2808 | } |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2809 | EXPORT_SYMBOL_GPL(spi_register_controller); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2810 | |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 2811 | static void devm_spi_unregister(struct device *dev, void *res) |
| 2812 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2813 | spi_unregister_controller(*(struct spi_controller **)res); |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | /** |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2817 | * devm_spi_register_controller - register managed SPI master or slave |
| 2818 | * controller |
| 2819 | * @dev: device managing SPI controller |
| 2820 | * @ctlr: initialized controller, originally from spi_alloc_master() or |
| 2821 | * spi_alloc_slave() |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 2822 | * Context: can sleep |
| 2823 | * |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2824 | * Register a SPI device as with spi_register_controller() which will |
Johan Hovold | 68b892f | 2017-10-30 11:35:26 +0100 | [diff] [blame] | 2825 | * automatically be unregistered and freed. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2826 | * |
| 2827 | * Return: zero on success, else a negative error code. |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 2828 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2829 | int devm_spi_register_controller(struct device *dev, |
| 2830 | struct spi_controller *ctlr) |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 2831 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2832 | struct spi_controller **ptr; |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 2833 | int ret; |
| 2834 | |
| 2835 | ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); |
| 2836 | if (!ptr) |
| 2837 | return -ENOMEM; |
| 2838 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2839 | ret = spi_register_controller(ctlr); |
Stephen Warren | 4b92894 | 2013-11-21 16:11:15 -0700 | [diff] [blame] | 2840 | if (!ret) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2841 | *ptr = ctlr; |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 2842 | devres_add(dev, ptr); |
| 2843 | } else { |
| 2844 | devres_free(ptr); |
| 2845 | } |
| 2846 | |
| 2847 | return ret; |
| 2848 | } |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2849 | EXPORT_SYMBOL_GPL(devm_spi_register_controller); |
Mark Brown | 666d5b4 | 2013-08-31 18:50:52 +0100 | [diff] [blame] | 2850 | |
David Lamparter | 3486008 | 2010-08-30 23:54:17 +0200 | [diff] [blame] | 2851 | static int __unregister(struct device *dev, void *null) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2852 | { |
David Lamparter | 3486008 | 2010-08-30 23:54:17 +0200 | [diff] [blame] | 2853 | spi_unregister_device(to_spi_device(dev)); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2854 | return 0; |
| 2855 | } |
| 2856 | |
| 2857 | /** |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2858 | * spi_unregister_controller - unregister SPI master or slave controller |
| 2859 | * @ctlr: the controller being unregistered |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2860 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2861 | * |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2862 | * This call is used only by SPI controller drivers, which are the |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2863 | * only ones directly touching chip registers. |
| 2864 | * |
| 2865 | * This must be called from context that can sleep. |
Johan Hovold | 68b892f | 2017-10-30 11:35:26 +0100 | [diff] [blame] | 2866 | * |
| 2867 | * Note that this function also drops a reference to the controller. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2868 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2869 | void spi_unregister_controller(struct spi_controller *ctlr) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2870 | { |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 2871 | struct spi_controller *found; |
Johan Hovold | 67f7b27 | 2017-10-30 11:35:25 +0100 | [diff] [blame] | 2872 | int id = ctlr->bus_num; |
Jeff Garzik | 89fc9a1 | 2006-12-06 20:35:35 -0800 | [diff] [blame] | 2873 | |
Lukas Wunner | ddf75be | 2020-08-03 13:09:01 +0200 | [diff] [blame] | 2874 | /* Prevent addition of new devices, unregister existing ones */ |
| 2875 | if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) |
| 2876 | mutex_lock(&spi_add_lock); |
| 2877 | |
Lukas Wunner | 8485567 | 2020-05-15 17:58:01 +0200 | [diff] [blame] | 2878 | device_for_each_child(&ctlr->dev, NULL, __unregister); |
| 2879 | |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 2880 | /* First make sure that this controller was ever added */ |
| 2881 | mutex_lock(&board_lock); |
Johan Hovold | 67f7b27 | 2017-10-30 11:35:25 +0100 | [diff] [blame] | 2882 | found = idr_find(&spi_master_idr, id); |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 2883 | mutex_unlock(&board_lock); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2884 | if (ctlr->queued) { |
| 2885 | if (spi_destroy_queue(ctlr)) |
| 2886 | dev_err(&ctlr->dev, "queue remove failed\n"); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2887 | } |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 2888 | mutex_lock(&board_lock); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2889 | list_del(&ctlr->list); |
Feng Tang | 2b9603a | 2010-08-02 15:52:15 +0800 | [diff] [blame] | 2890 | mutex_unlock(&board_lock); |
| 2891 | |
Lukas Wunner | 5e844cc | 2020-11-11 20:07:10 +0100 | [diff] [blame] | 2892 | device_del(&ctlr->dev); |
| 2893 | |
| 2894 | /* Release the last reference on the controller if its driver |
| 2895 | * has not yet been converted to devm_spi_alloc_master/slave(). |
| 2896 | */ |
William A. Kennington III | c7fabe3 | 2021-04-07 02:55:27 -0700 | [diff] [blame] | 2897 | if (!ctlr->devm_allocated) |
Lukas Wunner | 5e844cc | 2020-11-11 20:07:10 +0100 | [diff] [blame] | 2898 | put_device(&ctlr->dev); |
| 2899 | |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 2900 | /* free bus id */ |
| 2901 | mutex_lock(&board_lock); |
Jarkko Nikula | 613bd1e | 2018-03-20 10:27:50 +0200 | [diff] [blame] | 2902 | if (found == ctlr) |
| 2903 | idr_remove(&spi_master_idr, id); |
Suniel Mahesh | 9b61e30 | 2017-08-03 10:05:57 +0530 | [diff] [blame] | 2904 | mutex_unlock(&board_lock); |
Lukas Wunner | ddf75be | 2020-08-03 13:09:01 +0200 | [diff] [blame] | 2905 | |
| 2906 | if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) |
| 2907 | mutex_unlock(&spi_add_lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2908 | } |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2909 | EXPORT_SYMBOL_GPL(spi_unregister_controller); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2910 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2911 | int spi_controller_suspend(struct spi_controller *ctlr) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2912 | { |
| 2913 | int ret; |
| 2914 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2915 | /* Basically no-ops for non-queued controllers */ |
| 2916 | if (!ctlr->queued) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2917 | return 0; |
| 2918 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2919 | ret = spi_stop_queue(ctlr); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2920 | if (ret) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2921 | dev_err(&ctlr->dev, "queue stop failed\n"); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2922 | |
| 2923 | return ret; |
| 2924 | } |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2925 | EXPORT_SYMBOL_GPL(spi_controller_suspend); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2926 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2927 | int spi_controller_resume(struct spi_controller *ctlr) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2928 | { |
| 2929 | int ret; |
| 2930 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2931 | if (!ctlr->queued) |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2932 | return 0; |
| 2933 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2934 | ret = spi_start_queue(ctlr); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2935 | if (ret) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2936 | dev_err(&ctlr->dev, "queue restart failed\n"); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2937 | |
| 2938 | return ret; |
| 2939 | } |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2940 | EXPORT_SYMBOL_GPL(spi_controller_resume); |
Linus Walleij | ffbbdd21 | 2012-02-22 10:05:38 +0100 | [diff] [blame] | 2941 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2942 | static int __spi_controller_match(struct device *dev, const void *data) |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 2943 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2944 | struct spi_controller *ctlr; |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 2945 | const u16 *bus_num = data; |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 2946 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2947 | ctlr = container_of(dev, struct spi_controller, dev); |
| 2948 | return ctlr->bus_num == *bus_num; |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 2949 | } |
| 2950 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2951 | /** |
| 2952 | * spi_busnum_to_master - look up master associated with bus_num |
| 2953 | * @bus_num: the master's bus number |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 2954 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2955 | * |
| 2956 | * This call may be used with devices that are registered after |
| 2957 | * arch init time. It returns a refcounted pointer to the relevant |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2958 | * spi_controller (which the caller must release), or NULL if there is |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2959 | * no such master registered. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 2960 | * |
| 2961 | * Return: the SPI master structure on success, else NULL. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2962 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2963 | struct spi_controller *spi_busnum_to_master(u16 bus_num) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2964 | { |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 2965 | struct device *dev; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2966 | struct spi_controller *ctlr = NULL; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2967 | |
Greg Kroah-Hartman | 695794a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 2968 | dev = class_find_device(&spi_master_class, NULL, &bus_num, |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2969 | __spi_controller_match); |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 2970 | if (dev) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2971 | ctlr = container_of(dev, struct spi_controller, dev); |
Dave Young | 5ed2c83 | 2008-01-22 15:14:18 +0800 | [diff] [blame] | 2972 | /* reference got in class_find_device */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 2973 | return ctlr; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 2974 | } |
| 2975 | EXPORT_SYMBOL_GPL(spi_busnum_to_master); |
| 2976 | |
Martin Sperl | d780c37 | 2015-12-14 15:20:18 +0000 | [diff] [blame] | 2977 | /*-------------------------------------------------------------------------*/ |
| 2978 | |
| 2979 | /* Core methods for SPI resource management */ |
| 2980 | |
| 2981 | /** |
| 2982 | * spi_res_alloc - allocate a spi resource that is life-cycle managed |
| 2983 | * during the processing of a spi_message while using |
| 2984 | * spi_transfer_one |
| 2985 | * @spi: the spi device for which we allocate memory |
| 2986 | * @release: the release code to execute for this resource |
| 2987 | * @size: size to alloc and return |
| 2988 | * @gfp: GFP allocation flags |
| 2989 | * |
| 2990 | * Return: the pointer to the allocated data |
| 2991 | * |
| 2992 | * 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] | 2993 | * of the @spi_device or @spi_controller to avoid repeated allocations. |
Martin Sperl | d780c37 | 2015-12-14 15:20:18 +0000 | [diff] [blame] | 2994 | */ |
| 2995 | void *spi_res_alloc(struct spi_device *spi, |
| 2996 | spi_res_release_t release, |
| 2997 | size_t size, gfp_t gfp) |
| 2998 | { |
| 2999 | struct spi_res *sres; |
| 3000 | |
| 3001 | sres = kzalloc(sizeof(*sres) + size, gfp); |
| 3002 | if (!sres) |
| 3003 | return NULL; |
| 3004 | |
| 3005 | INIT_LIST_HEAD(&sres->entry); |
| 3006 | sres->release = release; |
| 3007 | |
| 3008 | return sres->data; |
| 3009 | } |
| 3010 | EXPORT_SYMBOL_GPL(spi_res_alloc); |
| 3011 | |
| 3012 | /** |
| 3013 | * spi_res_free - free an spi resource |
| 3014 | * @res: pointer to the custom data of a resource |
| 3015 | * |
| 3016 | */ |
| 3017 | void spi_res_free(void *res) |
| 3018 | { |
| 3019 | struct spi_res *sres = container_of(res, struct spi_res, data); |
| 3020 | |
| 3021 | if (!res) |
| 3022 | return; |
| 3023 | |
| 3024 | WARN_ON(!list_empty(&sres->entry)); |
| 3025 | kfree(sres); |
| 3026 | } |
| 3027 | EXPORT_SYMBOL_GPL(spi_res_free); |
| 3028 | |
| 3029 | /** |
| 3030 | * spi_res_add - add a spi_res to the spi_message |
| 3031 | * @message: the spi message |
| 3032 | * @res: the spi_resource |
| 3033 | */ |
| 3034 | void spi_res_add(struct spi_message *message, void *res) |
| 3035 | { |
| 3036 | struct spi_res *sres = container_of(res, struct spi_res, data); |
| 3037 | |
| 3038 | WARN_ON(!list_empty(&sres->entry)); |
| 3039 | list_add_tail(&sres->entry, &message->resources); |
| 3040 | } |
| 3041 | EXPORT_SYMBOL_GPL(spi_res_add); |
| 3042 | |
| 3043 | /** |
| 3044 | * spi_res_release - release all spi resources for this message |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3045 | * @ctlr: the @spi_controller |
Martin Sperl | d780c37 | 2015-12-14 15:20:18 +0000 | [diff] [blame] | 3046 | * @message: the @spi_message |
| 3047 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3048 | void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) |
Martin Sperl | d780c37 | 2015-12-14 15:20:18 +0000 | [diff] [blame] | 3049 | { |
Vladimir Zapolskiy | f569436 | 2019-06-18 19:28:18 +0300 | [diff] [blame] | 3050 | struct spi_res *res, *tmp; |
Martin Sperl | d780c37 | 2015-12-14 15:20:18 +0000 | [diff] [blame] | 3051 | |
Vladimir Zapolskiy | f569436 | 2019-06-18 19:28:18 +0300 | [diff] [blame] | 3052 | list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { |
Martin Sperl | d780c37 | 2015-12-14 15:20:18 +0000 | [diff] [blame] | 3053 | if (res->release) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3054 | res->release(ctlr, message, res->data); |
Martin Sperl | d780c37 | 2015-12-14 15:20:18 +0000 | [diff] [blame] | 3055 | |
| 3056 | list_del(&res->entry); |
| 3057 | |
| 3058 | kfree(res); |
| 3059 | } |
| 3060 | } |
| 3061 | EXPORT_SYMBOL_GPL(spi_res_release); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3062 | |
| 3063 | /*-------------------------------------------------------------------------*/ |
| 3064 | |
Martin Sperl | 523baf5a | 2015-12-14 15:20:19 +0000 | [diff] [blame] | 3065 | /* Core methods for spi_message alterations */ |
| 3066 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3067 | static void __spi_replace_transfers_release(struct spi_controller *ctlr, |
Martin Sperl | 523baf5a | 2015-12-14 15:20:19 +0000 | [diff] [blame] | 3068 | struct spi_message *msg, |
| 3069 | void *res) |
| 3070 | { |
| 3071 | struct spi_replaced_transfers *rxfer = res; |
| 3072 | size_t i; |
| 3073 | |
| 3074 | /* call extra callback if requested */ |
| 3075 | if (rxfer->release) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3076 | rxfer->release(ctlr, msg, res); |
Martin Sperl | 523baf5a | 2015-12-14 15:20:19 +0000 | [diff] [blame] | 3077 | |
| 3078 | /* insert replaced transfers back into the message */ |
| 3079 | list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); |
| 3080 | |
| 3081 | /* remove the formerly inserted entries */ |
| 3082 | for (i = 0; i < rxfer->inserted; i++) |
| 3083 | list_del(&rxfer->inserted_transfers[i].transfer_list); |
| 3084 | } |
| 3085 | |
| 3086 | /** |
| 3087 | * spi_replace_transfers - replace transfers with several transfers |
| 3088 | * and register change with spi_message.resources |
| 3089 | * @msg: the spi_message we work upon |
| 3090 | * @xfer_first: the first spi_transfer we want to replace |
| 3091 | * @remove: number of transfers to remove |
| 3092 | * @insert: the number of transfers we want to insert instead |
| 3093 | * @release: extra release code necessary in some circumstances |
| 3094 | * @extradatasize: extra data to allocate (with alignment guarantees |
| 3095 | * of struct @spi_transfer) |
Martin Sperl | 0588539 | 2016-02-18 15:53:11 +0000 | [diff] [blame] | 3096 | * @gfp: gfp flags |
Martin Sperl | 523baf5a | 2015-12-14 15:20:19 +0000 | [diff] [blame] | 3097 | * |
| 3098 | * Returns: pointer to @spi_replaced_transfers, |
| 3099 | * PTR_ERR(...) in case of errors. |
| 3100 | */ |
| 3101 | struct spi_replaced_transfers *spi_replace_transfers( |
| 3102 | struct spi_message *msg, |
| 3103 | struct spi_transfer *xfer_first, |
| 3104 | size_t remove, |
| 3105 | size_t insert, |
| 3106 | spi_replaced_release_t release, |
| 3107 | size_t extradatasize, |
| 3108 | gfp_t gfp) |
| 3109 | { |
| 3110 | struct spi_replaced_transfers *rxfer; |
| 3111 | struct spi_transfer *xfer; |
| 3112 | size_t i; |
| 3113 | |
| 3114 | /* allocate the structure using spi_res */ |
| 3115 | rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, |
Gustavo A. R. Silva | aef9752 | 2019-06-07 13:48:45 -0500 | [diff] [blame] | 3116 | struct_size(rxfer, inserted_transfers, insert) |
Martin Sperl | 523baf5a | 2015-12-14 15:20:19 +0000 | [diff] [blame] | 3117 | + extradatasize, |
| 3118 | gfp); |
| 3119 | if (!rxfer) |
| 3120 | return ERR_PTR(-ENOMEM); |
| 3121 | |
| 3122 | /* the release code to invoke before running the generic release */ |
| 3123 | rxfer->release = release; |
| 3124 | |
| 3125 | /* assign extradata */ |
| 3126 | if (extradatasize) |
| 3127 | rxfer->extradata = |
| 3128 | &rxfer->inserted_transfers[insert]; |
| 3129 | |
| 3130 | /* init the replaced_transfers list */ |
| 3131 | INIT_LIST_HEAD(&rxfer->replaced_transfers); |
| 3132 | |
| 3133 | /* assign the list_entry after which we should reinsert |
| 3134 | * the @replaced_transfers - it may be spi_message.messages! |
| 3135 | */ |
| 3136 | rxfer->replaced_after = xfer_first->transfer_list.prev; |
| 3137 | |
| 3138 | /* remove the requested number of transfers */ |
| 3139 | for (i = 0; i < remove; i++) { |
| 3140 | /* if the entry after replaced_after it is msg->transfers |
| 3141 | * then we have been requested to remove more transfers |
| 3142 | * than are in the list |
| 3143 | */ |
| 3144 | if (rxfer->replaced_after->next == &msg->transfers) { |
| 3145 | dev_err(&msg->spi->dev, |
| 3146 | "requested to remove more spi_transfers than are available\n"); |
| 3147 | /* insert replaced transfers back into the message */ |
| 3148 | list_splice(&rxfer->replaced_transfers, |
| 3149 | rxfer->replaced_after); |
| 3150 | |
| 3151 | /* free the spi_replace_transfer structure */ |
| 3152 | spi_res_free(rxfer); |
| 3153 | |
| 3154 | /* and return with an error */ |
| 3155 | return ERR_PTR(-EINVAL); |
| 3156 | } |
| 3157 | |
| 3158 | /* remove the entry after replaced_after from list of |
| 3159 | * transfers and add it to list of replaced_transfers |
| 3160 | */ |
| 3161 | list_move_tail(rxfer->replaced_after->next, |
| 3162 | &rxfer->replaced_transfers); |
| 3163 | } |
| 3164 | |
| 3165 | /* create copy of the given xfer with identical settings |
| 3166 | * based on the first transfer to get removed |
| 3167 | */ |
| 3168 | for (i = 0; i < insert; i++) { |
| 3169 | /* we need to run in reverse order */ |
| 3170 | xfer = &rxfer->inserted_transfers[insert - 1 - i]; |
| 3171 | |
| 3172 | /* copy all spi_transfer data */ |
| 3173 | memcpy(xfer, xfer_first, sizeof(*xfer)); |
| 3174 | |
| 3175 | /* add to list */ |
| 3176 | list_add(&xfer->transfer_list, rxfer->replaced_after); |
| 3177 | |
Alexandru Ardelean | bebcfd2 | 2019-09-26 13:51:36 +0300 | [diff] [blame] | 3178 | /* clear cs_change and delay for all but the last */ |
Martin Sperl | 523baf5a | 2015-12-14 15:20:19 +0000 | [diff] [blame] | 3179 | if (i) { |
| 3180 | xfer->cs_change = false; |
| 3181 | xfer->delay_usecs = 0; |
Alexandru Ardelean | bebcfd2 | 2019-09-26 13:51:36 +0300 | [diff] [blame] | 3182 | xfer->delay.value = 0; |
Martin Sperl | 523baf5a | 2015-12-14 15:20:19 +0000 | [diff] [blame] | 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | /* set up inserted */ |
| 3187 | rxfer->inserted = insert; |
| 3188 | |
| 3189 | /* and register it with spi_res/spi_message */ |
| 3190 | spi_res_add(msg, rxfer); |
| 3191 | |
| 3192 | return rxfer; |
| 3193 | } |
| 3194 | EXPORT_SYMBOL_GPL(spi_replace_transfers); |
| 3195 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3196 | static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, |
Fabio Estevam | 0893341 | 2016-02-14 13:33:50 -0200 | [diff] [blame] | 3197 | struct spi_message *msg, |
| 3198 | struct spi_transfer **xferp, |
| 3199 | size_t maxsize, |
| 3200 | gfp_t gfp) |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 3201 | { |
| 3202 | struct spi_transfer *xfer = *xferp, *xfers; |
| 3203 | struct spi_replaced_transfers *srt; |
| 3204 | size_t offset; |
| 3205 | size_t count, i; |
| 3206 | |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 3207 | /* calculate how many we have to replace */ |
| 3208 | count = DIV_ROUND_UP(xfer->len, maxsize); |
| 3209 | |
| 3210 | /* create replacement */ |
| 3211 | srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); |
Dan Carpenter | 657d32e | 2016-02-12 09:38:33 +0300 | [diff] [blame] | 3212 | if (IS_ERR(srt)) |
| 3213 | return PTR_ERR(srt); |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 3214 | xfers = srt->inserted_transfers; |
| 3215 | |
| 3216 | /* now handle each of those newly inserted spi_transfers |
| 3217 | * note that the replacements spi_transfers all are preset |
| 3218 | * to the same values as *xferp, so tx_buf, rx_buf and len |
| 3219 | * are all identical (as well as most others) |
| 3220 | * so we just have to fix up len and the pointers. |
| 3221 | * |
| 3222 | * this also includes support for the depreciated |
| 3223 | * spi_message.is_dma_mapped interface |
| 3224 | */ |
| 3225 | |
| 3226 | /* the first transfer just needs the length modified, so we |
| 3227 | * run it outside the loop |
| 3228 | */ |
Fabio Estevam | c8dab77 | 2016-02-17 15:42:28 -0200 | [diff] [blame] | 3229 | xfers[0].len = min_t(size_t, maxsize, xfer[0].len); |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 3230 | |
| 3231 | /* all the others need rx_buf/tx_buf also set */ |
| 3232 | for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { |
| 3233 | /* update rx_buf, tx_buf and dma */ |
| 3234 | if (xfers[i].rx_buf) |
| 3235 | xfers[i].rx_buf += offset; |
| 3236 | if (xfers[i].rx_dma) |
| 3237 | xfers[i].rx_dma += offset; |
| 3238 | if (xfers[i].tx_buf) |
| 3239 | xfers[i].tx_buf += offset; |
| 3240 | if (xfers[i].tx_dma) |
| 3241 | xfers[i].tx_dma += offset; |
| 3242 | |
| 3243 | /* update length */ |
| 3244 | xfers[i].len = min(maxsize, xfers[i].len - offset); |
| 3245 | } |
| 3246 | |
| 3247 | /* we set up xferp to the last entry we have inserted, |
| 3248 | * so that we skip those already split transfers |
| 3249 | */ |
| 3250 | *xferp = &xfers[count - 1]; |
| 3251 | |
| 3252 | /* increment statistics counters */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3253 | SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 3254 | transfers_split_maxsize); |
| 3255 | SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, |
| 3256 | transfers_split_maxsize); |
| 3257 | |
| 3258 | return 0; |
| 3259 | } |
| 3260 | |
| 3261 | /** |
| 3262 | * spi_split_tranfers_maxsize - split spi transfers into multiple transfers |
| 3263 | * when an individual transfer exceeds a |
| 3264 | * certain size |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3265 | * @ctlr: the @spi_controller for this transfer |
Masanari Iida | 3700ce9 | 2016-02-22 20:33:44 +0900 | [diff] [blame] | 3266 | * @msg: the @spi_message to transform |
| 3267 | * @maxsize: the maximum when to apply this |
Javier Martinez Canillas | 10f11a2 | 2016-03-10 15:01:14 -0300 | [diff] [blame] | 3268 | * @gfp: GFP allocation flags |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 3269 | * |
| 3270 | * Return: status of transformation |
| 3271 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3272 | int spi_split_transfers_maxsize(struct spi_controller *ctlr, |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 3273 | struct spi_message *msg, |
| 3274 | size_t maxsize, |
| 3275 | gfp_t gfp) |
| 3276 | { |
| 3277 | struct spi_transfer *xfer; |
| 3278 | int ret; |
| 3279 | |
| 3280 | /* iterate over the transfer_list, |
| 3281 | * but note that xfer is advanced to the last transfer inserted |
| 3282 | * to avoid checking sizes again unnecessarily (also xfer does |
| 3283 | * potentiall belong to a different list by the time the |
| 3284 | * replacement has happened |
| 3285 | */ |
| 3286 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 3287 | if (xfer->len > maxsize) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3288 | ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, |
| 3289 | maxsize, gfp); |
Martin Sperl | d9f1212 | 2015-12-14 15:20:20 +0000 | [diff] [blame] | 3290 | if (ret) |
| 3291 | return ret; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | return 0; |
| 3296 | } |
| 3297 | EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3298 | |
| 3299 | /*-------------------------------------------------------------------------*/ |
| 3300 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3301 | /* Core methods for SPI controller protocol drivers. Some of the |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 3302 | * other core methods are currently defined as inline functions. |
| 3303 | */ |
| 3304 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3305 | static int __spi_validate_bits_per_word(struct spi_controller *ctlr, |
| 3306 | u8 bits_per_word) |
Stefan Brüns | 63ab645 | 2015-08-23 16:06:30 +0200 | [diff] [blame] | 3307 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3308 | if (ctlr->bits_per_word_mask) { |
Stefan Brüns | 63ab645 | 2015-08-23 16:06:30 +0200 | [diff] [blame] | 3309 | /* Only 32 bits fit in the mask */ |
| 3310 | if (bits_per_word > 32) |
| 3311 | return -EINVAL; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3312 | 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] | 3313 | return -EINVAL; |
| 3314 | } |
| 3315 | |
| 3316 | return 0; |
| 3317 | } |
| 3318 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 3319 | /** |
| 3320 | * spi_setup - setup SPI mode and clock rate |
| 3321 | * @spi: the device whose settings are being modified |
| 3322 | * Context: can sleep, and no requests are queued to the device |
| 3323 | * |
| 3324 | * SPI protocol drivers may need to update the transfer mode if the |
| 3325 | * device doesn't work with its default. They may likewise need |
| 3326 | * to update clock rates or word sizes from initial values. This function |
| 3327 | * changes those settings, and must be called from a context that can sleep. |
| 3328 | * Except for SPI_CS_HIGH, which takes effect immediately, the changes take |
| 3329 | * effect the next time the device is selected and data is transferred to |
| 3330 | * or from it. When this function returns, the spi device is deselected. |
| 3331 | * |
| 3332 | * Note that this call will fail if the protocol driver specifies an option |
| 3333 | * that the underlying controller or its driver does not support. For |
| 3334 | * example, not all hardware supports wire transfers using nine bit words, |
| 3335 | * LSB-first wire encoding, or active-high chipselects. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 3336 | * |
| 3337 | * Return: zero on success, else a negative error code. |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 3338 | */ |
| 3339 | int spi_setup(struct spi_device *spi) |
| 3340 | { |
Geert Uytterhoeven | 83596fb | 2014-04-14 19:39:53 +0200 | [diff] [blame] | 3341 | unsigned bad_bits, ugly_bits; |
Andy Shevchenko | 5ab8d26 | 2015-10-14 22:43:07 +0300 | [diff] [blame] | 3342 | int status; |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 3343 | |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 3344 | /* check mode to prevent that DUAL and QUAD set at the same time |
| 3345 | */ |
| 3346 | if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || |
| 3347 | ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { |
| 3348 | dev_err(&spi->dev, |
| 3349 | "setup: can not select dual and quad at the same time\n"); |
| 3350 | return -EINVAL; |
| 3351 | } |
| 3352 | /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden |
| 3353 | */ |
| 3354 | if ((spi->mode & SPI_3WIRE) && (spi->mode & |
Yogesh Narayan Gaur | 6b03061 | 2018-12-03 08:39:06 +0000 | [diff] [blame] | 3355 | (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | |
| 3356 | SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 3357 | return -EINVAL; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 3358 | /* help drivers fail *cleanly* when they need options |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3359 | * that aren't supported with their current controller |
David Lechner | cbaa62e | 2018-09-12 19:39:18 -0500 | [diff] [blame] | 3360 | * SPI_CS_WORD has a fallback software implementation, |
| 3361 | * so it is ignored here. |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 3362 | */ |
David Lechner | cbaa62e | 2018-09-12 19:39:18 -0500 | [diff] [blame] | 3363 | bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); |
Serge Semin | d61ad23c | 2019-04-26 13:30:07 +0300 | [diff] [blame] | 3364 | /* nothing prevents from working with active-high CS in case if it |
| 3365 | * is driven by GPIO. |
| 3366 | */ |
| 3367 | if (gpio_is_valid(spi->cs_gpio)) |
| 3368 | bad_bits &= ~SPI_CS_HIGH; |
Geert Uytterhoeven | 83596fb | 2014-04-14 19:39:53 +0200 | [diff] [blame] | 3369 | ugly_bits = bad_bits & |
Yogesh Narayan Gaur | 6b03061 | 2018-12-03 08:39:06 +0000 | [diff] [blame] | 3370 | (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | |
| 3371 | SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); |
Geert Uytterhoeven | 83596fb | 2014-04-14 19:39:53 +0200 | [diff] [blame] | 3372 | if (ugly_bits) { |
| 3373 | dev_warn(&spi->dev, |
| 3374 | "setup: ignoring unsupported mode bits %x\n", |
| 3375 | ugly_bits); |
| 3376 | spi->mode &= ~ugly_bits; |
| 3377 | bad_bits &= ~ugly_bits; |
| 3378 | } |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 3379 | if (bad_bits) { |
Linus Walleij | eb288a1 | 2010-10-21 21:06:44 +0200 | [diff] [blame] | 3380 | dev_err(&spi->dev, "setup: unsupported mode bits %x\n", |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 3381 | bad_bits); |
| 3382 | return -EINVAL; |
| 3383 | } |
| 3384 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 3385 | if (!spi->bits_per_word) |
| 3386 | spi->bits_per_word = 8; |
| 3387 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3388 | status = __spi_validate_bits_per_word(spi->controller, |
| 3389 | spi->bits_per_word); |
Andy Shevchenko | 5ab8d26 | 2015-10-14 22:43:07 +0300 | [diff] [blame] | 3390 | if (status) |
| 3391 | return status; |
Stefan Brüns | 63ab645 | 2015-08-23 16:06:30 +0200 | [diff] [blame] | 3392 | |
Axel Lin | 052eb2d | 2014-02-10 00:08:05 +0800 | [diff] [blame] | 3393 | if (!spi->max_speed_hz) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3394 | spi->max_speed_hz = spi->controller->max_speed_hz; |
Axel Lin | 052eb2d | 2014-02-10 00:08:05 +0800 | [diff] [blame] | 3395 | |
Serge Semin | 4fae3a5 | 2020-11-17 12:45:17 +0300 | [diff] [blame] | 3396 | mutex_lock(&spi->controller->io_mutex); |
| 3397 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3398 | if (spi->controller->setup) |
| 3399 | status = spi->controller->setup(spi); |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 3400 | |
Luhua Xu | d948e6c | 2019-10-30 17:03:54 +0800 | [diff] [blame] | 3401 | if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { |
| 3402 | status = pm_runtime_get_sync(spi->controller->dev.parent); |
| 3403 | if (status < 0) { |
Serge Semin | 4fae3a5 | 2020-11-17 12:45:17 +0300 | [diff] [blame] | 3404 | mutex_unlock(&spi->controller->io_mutex); |
Luhua Xu | d948e6c | 2019-10-30 17:03:54 +0800 | [diff] [blame] | 3405 | pm_runtime_put_noidle(spi->controller->dev.parent); |
| 3406 | dev_err(&spi->controller->dev, "Failed to power device: %d\n", |
| 3407 | status); |
| 3408 | return status; |
| 3409 | } |
Tony Lindgren | 57a9460 | 2019-11-11 11:53:34 -0800 | [diff] [blame] | 3410 | |
| 3411 | /* |
| 3412 | * We do not want to return positive value from pm_runtime_get, |
| 3413 | * there are many instances of devices calling spi_setup() and |
| 3414 | * checking for a non-zero return value instead of a negative |
| 3415 | * return value. |
| 3416 | */ |
| 3417 | status = 0; |
| 3418 | |
David Bauer | c5aa9ea | 2021-04-16 21:59:56 +0200 | [diff] [blame] | 3419 | spi_set_cs(spi, false, true); |
Luhua Xu | d948e6c | 2019-10-30 17:03:54 +0800 | [diff] [blame] | 3420 | pm_runtime_mark_last_busy(spi->controller->dev.parent); |
| 3421 | pm_runtime_put_autosuspend(spi->controller->dev.parent); |
| 3422 | } else { |
David Bauer | c5aa9ea | 2021-04-16 21:59:56 +0200 | [diff] [blame] | 3423 | spi_set_cs(spi, false, true); |
Luhua Xu | d948e6c | 2019-10-30 17:03:54 +0800 | [diff] [blame] | 3424 | } |
Franklin S Cooper Jr | abeedb0 | 2015-10-16 10:29:03 -0500 | [diff] [blame] | 3425 | |
Serge Semin | 4fae3a5 | 2020-11-17 12:45:17 +0300 | [diff] [blame] | 3426 | mutex_unlock(&spi->controller->io_mutex); |
| 3427 | |
Douglas Anderson | 924b586 | 2019-05-15 09:48:12 -0700 | [diff] [blame] | 3428 | if (spi->rt && !spi->controller->rt) { |
| 3429 | spi->controller->rt = true; |
| 3430 | spi_set_thread_rt(spi->controller); |
| 3431 | } |
| 3432 | |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 3433 | 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] | 3434 | (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), |
| 3435 | (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", |
| 3436 | (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", |
| 3437 | (spi->mode & SPI_3WIRE) ? "3wire, " : "", |
| 3438 | (spi->mode & SPI_LOOP) ? "loopback, " : "", |
| 3439 | spi->bits_per_word, spi->max_speed_hz, |
| 3440 | status); |
| 3441 | |
| 3442 | return status; |
| 3443 | } |
| 3444 | EXPORT_SYMBOL_GPL(spi_setup); |
| 3445 | |
Sowjanya Komatineni | f1ca999 | 2019-04-04 17:14:16 -0700 | [diff] [blame] | 3446 | /** |
| 3447 | * spi_set_cs_timing - configure CS setup, hold, and inactive delays |
| 3448 | * @spi: the device that requires specific CS timing configuration |
Alexandru Ardelean | 8105936 | 2019-09-26 13:51:42 +0300 | [diff] [blame] | 3449 | * @setup: CS setup time specified via @spi_delay |
| 3450 | * @hold: CS hold time specified via @spi_delay |
| 3451 | * @inactive: CS inactive delay between transfers specified via @spi_delay |
| 3452 | * |
| 3453 | * Return: zero on success, else a negative error code. |
Sowjanya Komatineni | f1ca999 | 2019-04-04 17:14:16 -0700 | [diff] [blame] | 3454 | */ |
Alexandru Ardelean | 8105936 | 2019-09-26 13:51:42 +0300 | [diff] [blame] | 3455 | int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup, |
| 3456 | struct spi_delay *hold, struct spi_delay *inactive) |
Sowjanya Komatineni | f1ca999 | 2019-04-04 17:14:16 -0700 | [diff] [blame] | 3457 | { |
Alexandru Ardelean | 25093bd | 2019-09-26 13:51:43 +0300 | [diff] [blame] | 3458 | size_t len; |
| 3459 | |
Sowjanya Komatineni | f1ca999 | 2019-04-04 17:14:16 -0700 | [diff] [blame] | 3460 | if (spi->controller->set_cs_timing) |
Alexandru Ardelean | 8105936 | 2019-09-26 13:51:42 +0300 | [diff] [blame] | 3461 | return spi->controller->set_cs_timing(spi, setup, hold, |
| 3462 | inactive); |
Alexandru Ardelean | 25093bd | 2019-09-26 13:51:43 +0300 | [diff] [blame] | 3463 | |
| 3464 | if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) || |
| 3465 | (hold && hold->unit == SPI_DELAY_UNIT_SCK) || |
| 3466 | (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) { |
| 3467 | dev_err(&spi->dev, |
| 3468 | "Clock-cycle delays for CS not supported in SW mode\n"); |
| 3469 | return -ENOTSUPP; |
| 3470 | } |
| 3471 | |
| 3472 | len = sizeof(struct spi_delay); |
| 3473 | |
| 3474 | /* copy delays to controller */ |
| 3475 | if (setup) |
| 3476 | memcpy(&spi->controller->cs_setup, setup, len); |
| 3477 | else |
| 3478 | memset(&spi->controller->cs_setup, 0, len); |
| 3479 | |
| 3480 | if (hold) |
| 3481 | memcpy(&spi->controller->cs_hold, hold, len); |
| 3482 | else |
| 3483 | memset(&spi->controller->cs_hold, 0, len); |
| 3484 | |
| 3485 | if (inactive) |
| 3486 | memcpy(&spi->controller->cs_inactive, inactive, len); |
| 3487 | else |
| 3488 | memset(&spi->controller->cs_inactive, 0, len); |
| 3489 | |
| 3490 | return 0; |
Sowjanya Komatineni | f1ca999 | 2019-04-04 17:14:16 -0700 | [diff] [blame] | 3491 | } |
| 3492 | EXPORT_SYMBOL_GPL(spi_set_cs_timing); |
| 3493 | |
Alexandru Ardelean | 6c613f6 | 2019-09-26 13:51:35 +0300 | [diff] [blame] | 3494 | static int _spi_xfer_word_delay_update(struct spi_transfer *xfer, |
| 3495 | struct spi_device *spi) |
| 3496 | { |
| 3497 | int delay1, delay2; |
| 3498 | |
Alexandru Ardelean | 3984d39 | 2019-09-26 13:51:44 +0300 | [diff] [blame] | 3499 | delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); |
Alexandru Ardelean | 6c613f6 | 2019-09-26 13:51:35 +0300 | [diff] [blame] | 3500 | if (delay1 < 0) |
| 3501 | return delay1; |
| 3502 | |
Alexandru Ardelean | 3984d39 | 2019-09-26 13:51:44 +0300 | [diff] [blame] | 3503 | delay2 = spi_delay_to_ns(&spi->word_delay, xfer); |
Alexandru Ardelean | 6c613f6 | 2019-09-26 13:51:35 +0300 | [diff] [blame] | 3504 | if (delay2 < 0) |
| 3505 | return delay2; |
| 3506 | |
| 3507 | if (delay1 < delay2) |
| 3508 | memcpy(&xfer->word_delay, &spi->word_delay, |
| 3509 | sizeof(xfer->word_delay)); |
| 3510 | |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 3514 | static int __spi_validate(struct spi_device *spi, struct spi_message *message) |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3515 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3516 | struct spi_controller *ctlr = spi->controller; |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 3517 | struct spi_transfer *xfer; |
Atsushi Nemoto | 6ea3129 | 2014-02-28 23:03:16 +0900 | [diff] [blame] | 3518 | int w_size; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3519 | |
Mark Brown | 24a0013 | 2013-07-10 15:05:40 +0100 | [diff] [blame] | 3520 | if (list_empty(&message->transfers)) |
| 3521 | return -EINVAL; |
Mark Brown | 24a0013 | 2013-07-10 15:05:40 +0100 | [diff] [blame] | 3522 | |
David Lechner | cbaa62e | 2018-09-12 19:39:18 -0500 | [diff] [blame] | 3523 | /* If an SPI controller does not support toggling the CS line on each |
David Lechner | 71388b21 | 2018-09-18 12:08:48 -0500 | [diff] [blame] | 3524 | * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO |
| 3525 | * for the CS line, we can emulate the CS-per-word hardware function by |
David Lechner | cbaa62e | 2018-09-12 19:39:18 -0500 | [diff] [blame] | 3526 | * splitting transfers into one-word transfers and ensuring that |
| 3527 | * cs_change is set for each transfer. |
| 3528 | */ |
David Lechner | 71388b21 | 2018-09-18 12:08:48 -0500 | [diff] [blame] | 3529 | if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || |
Linus Walleij | f3186dd | 2019-01-07 16:51:50 +0100 | [diff] [blame] | 3530 | spi->cs_gpiod || |
David Lechner | 71388b21 | 2018-09-18 12:08:48 -0500 | [diff] [blame] | 3531 | gpio_is_valid(spi->cs_gpio))) { |
David Lechner | cbaa62e | 2018-09-12 19:39:18 -0500 | [diff] [blame] | 3532 | size_t maxsize; |
| 3533 | int ret; |
| 3534 | |
| 3535 | maxsize = (spi->bits_per_word + 7) / 8; |
| 3536 | |
| 3537 | /* spi_split_transfers_maxsize() requires message->spi */ |
| 3538 | message->spi = spi; |
| 3539 | |
| 3540 | ret = spi_split_transfers_maxsize(ctlr, message, maxsize, |
| 3541 | GFP_KERNEL); |
| 3542 | if (ret) |
| 3543 | return ret; |
| 3544 | |
| 3545 | list_for_each_entry(xfer, &message->transfers, transfer_list) { |
| 3546 | /* don't change cs_change on the last entry in the list */ |
| 3547 | if (list_is_last(&xfer->transfer_list, &message->transfers)) |
| 3548 | break; |
| 3549 | xfer->cs_change = 1; |
| 3550 | } |
| 3551 | } |
| 3552 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3553 | /* Half-duplex links include original MicroWire, and ones with |
| 3554 | * only one data pin like SPI_3WIRE (switches direction) or where |
| 3555 | * either MOSI or MISO is missing. They can also be caused by |
| 3556 | * software limitations. |
| 3557 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3558 | if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || |
| 3559 | (spi->mode & SPI_3WIRE)) { |
| 3560 | unsigned flags = ctlr->flags; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3561 | |
| 3562 | list_for_each_entry(xfer, &message->transfers, transfer_list) { |
| 3563 | if (xfer->rx_buf && xfer->tx_buf) |
| 3564 | return -EINVAL; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3565 | if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3566 | return -EINVAL; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3567 | if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3568 | return -EINVAL; |
| 3569 | } |
| 3570 | } |
| 3571 | |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 3572 | /** |
Laxman Dewangan | 059b8ff | 2013-01-05 00:17:14 +0530 | [diff] [blame] | 3573 | * Set transfer bits_per_word and max speed as spi device default if |
| 3574 | * it is not set for this transfer. |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 3575 | * Set transfer tx_nbits and rx_nbits as single transfer default |
| 3576 | * (SPI_NBITS_SINGLE) if it is not set for this transfer. |
Jonas Bonn | b7bb367 | 2019-01-30 09:40:04 +0100 | [diff] [blame] | 3577 | * Ensure transfer word_delay is at least as long as that required by |
| 3578 | * device itself. |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 3579 | */ |
Martin Sperl | 77e8058 | 2015-11-27 12:31:09 +0000 | [diff] [blame] | 3580 | message->frame_length = 0; |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 3581 | list_for_each_entry(xfer, &message->transfers, transfer_list) { |
Martin Sperl | 5d7e2b5 | 2019-02-23 08:49:49 +0000 | [diff] [blame] | 3582 | xfer->effective_speed_hz = 0; |
Sourav Poddar | 078726c | 2013-07-18 15:31:25 +0530 | [diff] [blame] | 3583 | message->frame_length += xfer->len; |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 3584 | if (!xfer->bits_per_word) |
| 3585 | xfer->bits_per_word = spi->bits_per_word; |
Axel Lin | a6f87fa | 2014-03-17 10:08:12 +0800 | [diff] [blame] | 3586 | |
| 3587 | if (!xfer->speed_hz) |
Laxman Dewangan | 059b8ff | 2013-01-05 00:17:14 +0530 | [diff] [blame] | 3588 | xfer->speed_hz = spi->max_speed_hz; |
Axel Lin | a6f87fa | 2014-03-17 10:08:12 +0800 | [diff] [blame] | 3589 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3590 | if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) |
| 3591 | xfer->speed_hz = ctlr->max_speed_hz; |
Gabor Juhos | 56ede94 | 2013-08-14 10:25:28 +0200 | [diff] [blame] | 3592 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3593 | if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) |
Stefan Brüns | 63ab645 | 2015-08-23 16:06:30 +0200 | [diff] [blame] | 3594 | return -EINVAL; |
Mark Brown | a2fd4f9 | 2013-07-10 14:57:26 +0100 | [diff] [blame] | 3595 | |
Ivan T. Ivanov | 4d94bd2 | 2014-02-20 12:02:08 +0200 | [diff] [blame] | 3596 | /* |
| 3597 | * SPI transfer length should be multiple of SPI word size |
| 3598 | * where SPI word size should be power-of-two multiple |
| 3599 | */ |
| 3600 | if (xfer->bits_per_word <= 8) |
| 3601 | w_size = 1; |
| 3602 | else if (xfer->bits_per_word <= 16) |
| 3603 | w_size = 2; |
| 3604 | else |
| 3605 | w_size = 4; |
| 3606 | |
Ivan T. Ivanov | 4d94bd2 | 2014-02-20 12:02:08 +0200 | [diff] [blame] | 3607 | /* No partial transfers accepted */ |
Atsushi Nemoto | 6ea3129 | 2014-02-28 23:03:16 +0900 | [diff] [blame] | 3608 | if (xfer->len % w_size) |
Ivan T. Ivanov | 4d94bd2 | 2014-02-20 12:02:08 +0200 | [diff] [blame] | 3609 | return -EINVAL; |
| 3610 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3611 | if (xfer->speed_hz && ctlr->min_speed_hz && |
| 3612 | xfer->speed_hz < ctlr->min_speed_hz) |
Mark Brown | a2fd4f9 | 2013-07-10 14:57:26 +0100 | [diff] [blame] | 3613 | return -EINVAL; |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 3614 | |
| 3615 | if (xfer->tx_buf && !xfer->tx_nbits) |
| 3616 | xfer->tx_nbits = SPI_NBITS_SINGLE; |
| 3617 | if (xfer->rx_buf && !xfer->rx_nbits) |
| 3618 | xfer->rx_nbits = SPI_NBITS_SINGLE; |
| 3619 | /* check transfer tx/rx_nbits: |
Geert Uytterhoeven | 1afd998 | 2014-01-12 14:00:29 +0100 | [diff] [blame] | 3620 | * 1. check the value matches one of single, dual and quad |
| 3621 | * 2. check tx/rx_nbits match the mode in spi_device |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 3622 | */ |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 3623 | if (xfer->tx_buf) { |
| 3624 | if (xfer->tx_nbits != SPI_NBITS_SINGLE && |
| 3625 | xfer->tx_nbits != SPI_NBITS_DUAL && |
| 3626 | xfer->tx_nbits != SPI_NBITS_QUAD) |
| 3627 | return -EINVAL; |
| 3628 | if ((xfer->tx_nbits == SPI_NBITS_DUAL) && |
| 3629 | !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) |
| 3630 | return -EINVAL; |
| 3631 | if ((xfer->tx_nbits == SPI_NBITS_QUAD) && |
| 3632 | !(spi->mode & SPI_TX_QUAD)) |
| 3633 | return -EINVAL; |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 3634 | } |
wangyuhang | f477b7f | 2013-08-11 18:15:17 +0800 | [diff] [blame] | 3635 | /* check transfer rx_nbits */ |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 3636 | if (xfer->rx_buf) { |
| 3637 | if (xfer->rx_nbits != SPI_NBITS_SINGLE && |
| 3638 | xfer->rx_nbits != SPI_NBITS_DUAL && |
| 3639 | xfer->rx_nbits != SPI_NBITS_QUAD) |
| 3640 | return -EINVAL; |
| 3641 | if ((xfer->rx_nbits == SPI_NBITS_DUAL) && |
| 3642 | !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) |
| 3643 | return -EINVAL; |
| 3644 | if ((xfer->rx_nbits == SPI_NBITS_QUAD) && |
| 3645 | !(spi->mode & SPI_RX_QUAD)) |
| 3646 | return -EINVAL; |
Sourav Poddar | db90a44 | 2013-08-22 21:20:48 +0530 | [diff] [blame] | 3647 | } |
Jonas Bonn | b7bb367 | 2019-01-30 09:40:04 +0100 | [diff] [blame] | 3648 | |
Alexandru Ardelean | 6c613f6 | 2019-09-26 13:51:35 +0300 | [diff] [blame] | 3649 | if (_spi_xfer_word_delay_update(xfer, spi)) |
| 3650 | return -EINVAL; |
Laxman Dewangan | e6811d1 | 2012-11-09 14:36:45 +0530 | [diff] [blame] | 3651 | } |
| 3652 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3653 | message->status = -EINPROGRESS; |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 3654 | |
| 3655 | return 0; |
| 3656 | } |
| 3657 | |
| 3658 | static int __spi_async(struct spi_device *spi, struct spi_message *message) |
| 3659 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3660 | struct spi_controller *ctlr = spi->controller; |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 3661 | struct spi_transfer *xfer; |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 3662 | |
Boris Brezillon | b5932f5 | 2018-04-26 18:18:15 +0200 | [diff] [blame] | 3663 | /* |
| 3664 | * Some controllers do not support doing regular SPI transfers. Return |
| 3665 | * ENOTSUPP when this is the case. |
| 3666 | */ |
| 3667 | if (!ctlr->transfer) |
| 3668 | return -ENOTSUPP; |
| 3669 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 3670 | message->spi = spi; |
| 3671 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3672 | SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 3673 | SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); |
| 3674 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 3675 | trace_spi_message_submit(message); |
| 3676 | |
Vladimir Oltean | b42faee | 2019-09-05 04:01:12 +0300 | [diff] [blame] | 3677 | if (!ctlr->ptp_sts_supported) { |
| 3678 | list_for_each_entry(xfer, &message->transfers, transfer_list) { |
| 3679 | xfer->ptp_sts_word_pre = 0; |
| 3680 | ptp_read_system_prets(xfer->ptp_sts); |
| 3681 | } |
| 3682 | } |
| 3683 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3684 | return ctlr->transfer(spi, message); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3685 | } |
| 3686 | |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 3687 | /** |
| 3688 | * spi_async - asynchronous SPI transfer |
| 3689 | * @spi: device with which data will be exchanged |
| 3690 | * @message: describes the data transfers, including completion callback |
| 3691 | * Context: any (irqs may be blocked, etc) |
| 3692 | * |
| 3693 | * This call may be used in_irq and other contexts which can't sleep, |
| 3694 | * as well as from task contexts which can sleep. |
| 3695 | * |
| 3696 | * The completion callback is invoked in a context which can't sleep. |
| 3697 | * Before that invocation, the value of message->status is undefined. |
| 3698 | * When the callback is issued, message->status holds either zero (to |
| 3699 | * indicate complete success) or a negative error code. After that |
| 3700 | * callback returns, the driver which issued the transfer request may |
| 3701 | * deallocate the associated memory; it's no longer in use by any SPI |
| 3702 | * core or controller driver code. |
| 3703 | * |
| 3704 | * Note that although all messages to a spi_device are handled in |
| 3705 | * FIFO order, messages may go to different devices in other orders. |
| 3706 | * Some device might be higher priority, or have various "hard" access |
| 3707 | * time requirements, for example. |
| 3708 | * |
| 3709 | * On detection of any fault during the transfer, processing of |
| 3710 | * the entire message is aborted, and the device is deselected. |
| 3711 | * Until returning from the associated message completion callback, |
| 3712 | * no other spi_message queued to that device will be processed. |
| 3713 | * (This rule applies equally to all the synchronous transfer calls, |
| 3714 | * which are wrappers around this core asynchronous primitive.) |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 3715 | * |
| 3716 | * Return: zero on success, else a negative error code. |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 3717 | */ |
| 3718 | int spi_async(struct spi_device *spi, struct spi_message *message) |
| 3719 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3720 | struct spi_controller *ctlr = spi->controller; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3721 | int ret; |
| 3722 | unsigned long flags; |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 3723 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 3724 | ret = __spi_validate(spi, message); |
| 3725 | if (ret != 0) |
| 3726 | return ret; |
| 3727 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3728 | spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 3729 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3730 | if (ctlr->bus_lock_flag) |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3731 | ret = -EBUSY; |
| 3732 | else |
| 3733 | ret = __spi_async(spi, message); |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 3734 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3735 | spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3736 | |
| 3737 | return ret; |
David Brownell | 568d069 | 2009-09-22 16:46:18 -0700 | [diff] [blame] | 3738 | } |
| 3739 | EXPORT_SYMBOL_GPL(spi_async); |
| 3740 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3741 | /** |
| 3742 | * spi_async_locked - version of spi_async with exclusive bus usage |
| 3743 | * @spi: device with which data will be exchanged |
| 3744 | * @message: describes the data transfers, including completion callback |
| 3745 | * Context: any (irqs may be blocked, etc) |
| 3746 | * |
| 3747 | * This call may be used in_irq and other contexts which can't sleep, |
| 3748 | * as well as from task contexts which can sleep. |
| 3749 | * |
| 3750 | * The completion callback is invoked in a context which can't sleep. |
| 3751 | * Before that invocation, the value of message->status is undefined. |
| 3752 | * When the callback is issued, message->status holds either zero (to |
| 3753 | * indicate complete success) or a negative error code. After that |
| 3754 | * callback returns, the driver which issued the transfer request may |
| 3755 | * deallocate the associated memory; it's no longer in use by any SPI |
| 3756 | * core or controller driver code. |
| 3757 | * |
| 3758 | * Note that although all messages to a spi_device are handled in |
| 3759 | * FIFO order, messages may go to different devices in other orders. |
| 3760 | * Some device might be higher priority, or have various "hard" access |
| 3761 | * time requirements, for example. |
| 3762 | * |
| 3763 | * On detection of any fault during the transfer, processing of |
| 3764 | * the entire message is aborted, and the device is deselected. |
| 3765 | * Until returning from the associated message completion callback, |
| 3766 | * no other spi_message queued to that device will be processed. |
| 3767 | * (This rule applies equally to all the synchronous transfer calls, |
| 3768 | * which are wrappers around this core asynchronous primitive.) |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 3769 | * |
| 3770 | * Return: zero on success, else a negative error code. |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3771 | */ |
| 3772 | int spi_async_locked(struct spi_device *spi, struct spi_message *message) |
| 3773 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3774 | struct spi_controller *ctlr = spi->controller; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3775 | int ret; |
| 3776 | unsigned long flags; |
| 3777 | |
Mark Brown | 9080873 | 2013-11-13 23:44:15 +0000 | [diff] [blame] | 3778 | ret = __spi_validate(spi, message); |
| 3779 | if (ret != 0) |
| 3780 | return ret; |
| 3781 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3782 | spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3783 | |
| 3784 | ret = __spi_async(spi, message); |
| 3785 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3786 | spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3787 | |
| 3788 | return ret; |
| 3789 | |
| 3790 | } |
| 3791 | EXPORT_SYMBOL_GPL(spi_async_locked); |
| 3792 | |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 3793 | /*-------------------------------------------------------------------------*/ |
| 3794 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3795 | /* Utility methods for SPI protocol drivers, layered on |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 3796 | * top of the core. Some other utility methods are defined as |
| 3797 | * inline functions. |
| 3798 | */ |
| 3799 | |
Andrew Morton | 5d870c8 | 2006-01-11 11:23:49 -0800 | [diff] [blame] | 3800 | static void spi_complete(void *arg) |
| 3801 | { |
| 3802 | complete(arg); |
| 3803 | } |
| 3804 | |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 3805 | static int __spi_sync(struct spi_device *spi, struct spi_message *message) |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3806 | { |
| 3807 | DECLARE_COMPLETION_ONSTACK(done); |
| 3808 | int status; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3809 | struct spi_controller *ctlr = spi->controller; |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 3810 | unsigned long flags; |
| 3811 | |
| 3812 | status = __spi_validate(spi, message); |
| 3813 | if (status != 0) |
| 3814 | return status; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3815 | |
| 3816 | message->complete = spi_complete; |
| 3817 | message->context = &done; |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 3818 | message->spi = spi; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3819 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3820 | SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 3821 | SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); |
| 3822 | |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 3823 | /* If we're not using the legacy transfer method then we will |
| 3824 | * try to transfer in the calling context so special case. |
| 3825 | * This code would be less tricky if we could remove the |
| 3826 | * support for driver implemented message queues. |
| 3827 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3828 | if (ctlr->transfer == spi_queued_transfer) { |
| 3829 | spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 3830 | |
| 3831 | trace_spi_message_submit(message); |
| 3832 | |
| 3833 | status = __spi_queued_transfer(spi, message, false); |
| 3834 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3835 | spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 3836 | } else { |
| 3837 | status = spi_async_locked(spi, message); |
| 3838 | } |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3839 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3840 | if (status == 0) { |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 3841 | /* Push out the messages in the calling context if we |
| 3842 | * can. |
| 3843 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3844 | if (ctlr->transfer == spi_queued_transfer) { |
| 3845 | SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 3846 | spi_sync_immediate); |
| 3847 | SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, |
| 3848 | spi_sync_immediate); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3849 | __spi_pump_messages(ctlr, false); |
Martin Sperl | eca2ebc | 2015-06-22 13:00:36 +0000 | [diff] [blame] | 3850 | } |
Mark Brown | 0461a41 | 2014-12-09 21:38:05 +0000 | [diff] [blame] | 3851 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3852 | wait_for_completion(&done); |
| 3853 | status = message->status; |
| 3854 | } |
| 3855 | message->context = NULL; |
| 3856 | return status; |
| 3857 | } |
| 3858 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3859 | /** |
| 3860 | * spi_sync - blocking/synchronous SPI data transfers |
| 3861 | * @spi: device with which data will be exchanged |
| 3862 | * @message: describes the data transfers |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 3863 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3864 | * |
| 3865 | * This call may only be used from a context that may sleep. The sleep |
| 3866 | * is non-interruptible, and has no timeout. Low-overhead controller |
| 3867 | * drivers may DMA directly into and out of the message buffers. |
| 3868 | * |
| 3869 | * Note that the SPI device's chip select is active during the message, |
| 3870 | * and then is normally disabled between messages. Drivers for some |
| 3871 | * frequently-used devices may want to minimize costs of selecting a chip, |
| 3872 | * by leaving it selected in anticipation that the next message will go |
| 3873 | * to the same chip. (That may increase power usage.) |
| 3874 | * |
David Brownell | 0c868461 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 3875 | * Also, the caller is guaranteeing that the memory associated with the |
| 3876 | * message will not be freed before this call returns. |
| 3877 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 3878 | * Return: zero on success, else a negative error code. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3879 | */ |
| 3880 | int spi_sync(struct spi_device *spi, struct spi_message *message) |
| 3881 | { |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 3882 | int ret; |
| 3883 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3884 | mutex_lock(&spi->controller->bus_lock_mutex); |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 3885 | ret = __spi_sync(spi, message); |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3886 | mutex_unlock(&spi->controller->bus_lock_mutex); |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 3887 | |
| 3888 | return ret; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3889 | } |
| 3890 | EXPORT_SYMBOL_GPL(spi_sync); |
| 3891 | |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3892 | /** |
| 3893 | * spi_sync_locked - version of spi_sync with exclusive bus usage |
| 3894 | * @spi: device with which data will be exchanged |
| 3895 | * @message: describes the data transfers |
| 3896 | * Context: can sleep |
| 3897 | * |
| 3898 | * This call may only be used from a context that may sleep. The sleep |
| 3899 | * is non-interruptible, and has no timeout. Low-overhead controller |
| 3900 | * drivers may DMA directly into and out of the message buffers. |
| 3901 | * |
| 3902 | * 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] | 3903 | * 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] | 3904 | * be released by a spi_bus_unlock call when the exclusive access is over. |
| 3905 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 3906 | * Return: zero on success, else a negative error code. |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3907 | */ |
| 3908 | int spi_sync_locked(struct spi_device *spi, struct spi_message *message) |
| 3909 | { |
Mark Brown | ef4d96e | 2016-07-21 23:53:31 +0100 | [diff] [blame] | 3910 | return __spi_sync(spi, message); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3911 | } |
| 3912 | EXPORT_SYMBOL_GPL(spi_sync_locked); |
| 3913 | |
| 3914 | /** |
| 3915 | * spi_bus_lock - obtain a lock for exclusive SPI bus usage |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3916 | * @ctlr: SPI bus master that should be locked for exclusive bus access |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3917 | * Context: can sleep |
| 3918 | * |
| 3919 | * This call may only be used from a context that may sleep. The sleep |
| 3920 | * is non-interruptible, and has no timeout. |
| 3921 | * |
| 3922 | * This call should be used by drivers that require exclusive access to the |
| 3923 | * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the |
| 3924 | * exclusive access is over. Data transfer must be done by spi_sync_locked |
| 3925 | * and spi_async_locked calls when the SPI bus lock is held. |
| 3926 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 3927 | * Return: always zero. |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3928 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3929 | int spi_bus_lock(struct spi_controller *ctlr) |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3930 | { |
| 3931 | unsigned long flags; |
| 3932 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3933 | mutex_lock(&ctlr->bus_lock_mutex); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3934 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3935 | spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); |
| 3936 | ctlr->bus_lock_flag = 1; |
| 3937 | spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3938 | |
| 3939 | /* mutex remains locked until spi_bus_unlock is called */ |
| 3940 | |
| 3941 | return 0; |
| 3942 | } |
| 3943 | EXPORT_SYMBOL_GPL(spi_bus_lock); |
| 3944 | |
| 3945 | /** |
| 3946 | * spi_bus_unlock - release the lock for exclusive SPI bus usage |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3947 | * @ctlr: SPI bus master that was locked for exclusive bus access |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3948 | * Context: can sleep |
| 3949 | * |
| 3950 | * This call may only be used from a context that may sleep. The sleep |
| 3951 | * is non-interruptible, and has no timeout. |
| 3952 | * |
| 3953 | * This call releases an SPI bus lock previously obtained by an spi_bus_lock |
| 3954 | * call. |
| 3955 | * |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 3956 | * Return: always zero. |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3957 | */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3958 | int spi_bus_unlock(struct spi_controller *ctlr) |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3959 | { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3960 | ctlr->bus_lock_flag = 0; |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3961 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 3962 | mutex_unlock(&ctlr->bus_lock_mutex); |
Ernst Schwab | cf32b71e | 2010-06-28 17:49:29 -0700 | [diff] [blame] | 3963 | |
| 3964 | return 0; |
| 3965 | } |
| 3966 | EXPORT_SYMBOL_GPL(spi_bus_unlock); |
| 3967 | |
David Brownell | a9948b6 | 2006-04-02 10:37:40 -0800 | [diff] [blame] | 3968 | /* portable code must never pass more than 32 bytes */ |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 3969 | #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3970 | |
| 3971 | static u8 *buf; |
| 3972 | |
| 3973 | /** |
| 3974 | * spi_write_then_read - SPI synchronous write followed by read |
| 3975 | * @spi: device with which data will be exchanged |
| 3976 | * @txbuf: data to be written (need not be dma-safe) |
| 3977 | * @n_tx: size of txbuf, in bytes |
Jiri Pirko | 2757049 | 2009-06-17 16:26:06 -0700 | [diff] [blame] | 3978 | * @rxbuf: buffer into which data will be read (need not be dma-safe) |
| 3979 | * @n_rx: size of rxbuf, in bytes |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 3980 | * Context: can sleep |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3981 | * |
| 3982 | * This performs a half duplex MicroWire style transaction with the |
| 3983 | * device, sending txbuf and then reading rxbuf. The return value |
| 3984 | * is zero for success, else a negative errno status code. |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 3985 | * This call may only be used from a context that may sleep. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3986 | * |
Mark Brown | c373643 | 2020-05-25 14:31:20 +0100 | [diff] [blame] | 3987 | * Parameters to this routine are always copied using a small buffer. |
David Brownell | 33e34dc | 2007-05-08 00:32:21 -0700 | [diff] [blame] | 3988 | * Performance-sensitive or bulk transfer code should instead use |
David Brownell | 0c868461 | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 3989 | * spi_{async,sync}() calls with dma-safe buffers. |
Javier Martinez Canillas | 97d56dc | 2015-10-22 18:59:23 +0200 | [diff] [blame] | 3990 | * |
| 3991 | * Return: zero on success, else a negative error code. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3992 | */ |
| 3993 | int spi_write_then_read(struct spi_device *spi, |
Mark Brown | 0c4a159 | 2011-05-11 00:09:30 +0200 | [diff] [blame] | 3994 | const void *txbuf, unsigned n_tx, |
| 3995 | void *rxbuf, unsigned n_rx) |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3996 | { |
David Brownell | 068f407 | 2007-12-04 23:45:09 -0800 | [diff] [blame] | 3997 | static DEFINE_MUTEX(lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 3998 | |
| 3999 | int status; |
| 4000 | struct spi_message message; |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 4001 | struct spi_transfer x[2]; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4002 | u8 *local_buf; |
| 4003 | |
Mark Brown | b3a223e | 2012-12-02 12:54:25 +0900 | [diff] [blame] | 4004 | /* Use preallocated DMA-safe buffer if we can. We can't avoid |
| 4005 | * copying here, (as a pure convenience thing), but we can |
| 4006 | * keep heap costs out of the hot path unless someone else is |
| 4007 | * using the pre-allocated buffer or the transfer is too large. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4008 | */ |
Mark Brown | b3a223e | 2012-12-02 12:54:25 +0900 | [diff] [blame] | 4009 | if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { |
Mark Brown | 2cd94c8 | 2013-01-27 14:35:04 +0800 | [diff] [blame] | 4010 | local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), |
| 4011 | GFP_KERNEL | GFP_DMA); |
Mark Brown | b3a223e | 2012-12-02 12:54:25 +0900 | [diff] [blame] | 4012 | if (!local_buf) |
| 4013 | return -ENOMEM; |
| 4014 | } else { |
| 4015 | local_buf = buf; |
| 4016 | } |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4017 | |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 4018 | spi_message_init(&message); |
Jingoo Han | 5fe5f05 | 2013-10-14 10:31:51 +0900 | [diff] [blame] | 4019 | memset(x, 0, sizeof(x)); |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 4020 | if (n_tx) { |
| 4021 | x[0].len = n_tx; |
| 4022 | spi_message_add_tail(&x[0], &message); |
| 4023 | } |
| 4024 | if (n_rx) { |
| 4025 | x[1].len = n_rx; |
| 4026 | spi_message_add_tail(&x[1], &message); |
| 4027 | } |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 4028 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4029 | memcpy(local_buf, txbuf, n_tx); |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 4030 | x[0].tx_buf = local_buf; |
| 4031 | x[1].rx_buf = local_buf + n_tx; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4032 | |
| 4033 | /* do the i/o */ |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4034 | status = spi_sync(spi, &message); |
Marc Pignat | 9b938b7 | 2007-12-04 23:45:10 -0800 | [diff] [blame] | 4035 | if (status == 0) |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 4036 | memcpy(rxbuf, x[1].rx_buf, n_rx); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4037 | |
David Brownell | bdff549 | 2009-04-13 14:39:57 -0700 | [diff] [blame] | 4038 | if (x[0].tx_buf == buf) |
David Brownell | 068f407 | 2007-12-04 23:45:09 -0800 | [diff] [blame] | 4039 | mutex_unlock(&lock); |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4040 | else |
| 4041 | kfree(local_buf); |
| 4042 | |
| 4043 | return status; |
| 4044 | } |
| 4045 | EXPORT_SYMBOL_GPL(spi_write_then_read); |
| 4046 | |
| 4047 | /*-------------------------------------------------------------------------*/ |
| 4048 | |
Marco Felsch | 5f143af | 2018-09-25 11:42:29 +0200 | [diff] [blame] | 4049 | #if IS_ENABLED(CONFIG_OF) |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4050 | /* must call put_device() when done with returned spi_device device */ |
Marco Felsch | 5f143af | 2018-09-25 11:42:29 +0200 | [diff] [blame] | 4051 | struct spi_device *of_find_spi_device_by_node(struct device_node *node) |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4052 | { |
Suzuki K Poulose | cfba5de | 2019-07-23 23:18:33 +0100 | [diff] [blame] | 4053 | struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); |
| 4054 | |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4055 | return dev ? to_spi_device(dev) : NULL; |
| 4056 | } |
Marco Felsch | 5f143af | 2018-09-25 11:42:29 +0200 | [diff] [blame] | 4057 | EXPORT_SYMBOL_GPL(of_find_spi_device_by_node); |
| 4058 | #endif /* IS_ENABLED(CONFIG_OF) */ |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4059 | |
Marco Felsch | 5f143af | 2018-09-25 11:42:29 +0200 | [diff] [blame] | 4060 | #if IS_ENABLED(CONFIG_OF_DYNAMIC) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4061 | /* the spi controllers are not using spi_bus, so we find it with another way */ |
| 4062 | 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] | 4063 | { |
| 4064 | struct device *dev; |
| 4065 | |
Suzuki K Poulose | cfba5de | 2019-07-23 23:18:33 +0100 | [diff] [blame] | 4066 | dev = class_find_device_by_of_node(&spi_master_class, node); |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 4067 | if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) |
Suzuki K Poulose | cfba5de | 2019-07-23 23:18:33 +0100 | [diff] [blame] | 4068 | dev = class_find_device_by_of_node(&spi_slave_class, node); |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4069 | if (!dev) |
| 4070 | return NULL; |
| 4071 | |
| 4072 | /* reference got in class_find_device */ |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4073 | return container_of(dev, struct spi_controller, dev); |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4074 | } |
| 4075 | |
| 4076 | static int of_spi_notify(struct notifier_block *nb, unsigned long action, |
| 4077 | void *arg) |
| 4078 | { |
| 4079 | struct of_reconfig_data *rd = arg; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4080 | struct spi_controller *ctlr; |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4081 | struct spi_device *spi; |
| 4082 | |
| 4083 | switch (of_reconfig_get_state_change(action, arg)) { |
| 4084 | case OF_RECONFIG_CHANGE_ADD: |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4085 | ctlr = of_find_spi_controller_by_node(rd->dn->parent); |
| 4086 | if (ctlr == NULL) |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4087 | return NOTIFY_OK; /* not for us */ |
| 4088 | |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 4089 | if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4090 | put_device(&ctlr->dev); |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 4091 | return NOTIFY_OK; |
| 4092 | } |
| 4093 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4094 | spi = of_register_spi_device(ctlr, rd->dn); |
| 4095 | put_device(&ctlr->dev); |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4096 | |
| 4097 | if (IS_ERR(spi)) { |
Rob Herring | 25c56c8 | 2017-07-18 16:43:31 -0500 | [diff] [blame] | 4098 | pr_err("%s: failed to create for '%pOF'\n", |
| 4099 | __func__, rd->dn); |
Ralf Ramsauer | e0af98a | 2016-10-17 15:59:56 +0200 | [diff] [blame] | 4100 | of_node_clear_flag(rd->dn, OF_POPULATED); |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4101 | return notifier_from_errno(PTR_ERR(spi)); |
| 4102 | } |
| 4103 | break; |
| 4104 | |
| 4105 | case OF_RECONFIG_CHANGE_REMOVE: |
Geert Uytterhoeven | bd6c164 | 2015-11-30 15:28:07 +0100 | [diff] [blame] | 4106 | /* already depopulated? */ |
| 4107 | if (!of_node_check_flag(rd->dn, OF_POPULATED)) |
| 4108 | return NOTIFY_OK; |
| 4109 | |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4110 | /* find our device by node */ |
| 4111 | spi = of_find_spi_device_by_node(rd->dn); |
| 4112 | if (spi == NULL) |
| 4113 | return NOTIFY_OK; /* no? not meant for us */ |
| 4114 | |
| 4115 | /* unregister takes one ref away */ |
| 4116 | spi_unregister_device(spi); |
| 4117 | |
| 4118 | /* and put the reference of the find */ |
| 4119 | put_device(&spi->dev); |
| 4120 | break; |
| 4121 | } |
| 4122 | |
| 4123 | return NOTIFY_OK; |
| 4124 | } |
| 4125 | |
| 4126 | static struct notifier_block spi_of_notifier = { |
| 4127 | .notifier_call = of_spi_notify, |
| 4128 | }; |
| 4129 | #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ |
| 4130 | extern struct notifier_block spi_of_notifier; |
| 4131 | #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ |
| 4132 | |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 4133 | #if IS_ENABLED(CONFIG_ACPI) |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4134 | static int spi_acpi_controller_match(struct device *dev, const void *data) |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 4135 | { |
| 4136 | return ACPI_COMPANION(dev->parent) == data; |
| 4137 | } |
| 4138 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4139 | 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] | 4140 | { |
| 4141 | struct device *dev; |
| 4142 | |
| 4143 | dev = class_find_device(&spi_master_class, NULL, adev, |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4144 | spi_acpi_controller_match); |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 4145 | if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) |
| 4146 | dev = class_find_device(&spi_slave_class, NULL, adev, |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4147 | spi_acpi_controller_match); |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 4148 | if (!dev) |
| 4149 | return NULL; |
| 4150 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4151 | return container_of(dev, struct spi_controller, dev); |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) |
| 4155 | { |
| 4156 | struct device *dev; |
| 4157 | |
Suzuki K Poulose | 0050014 | 2019-07-23 23:18:36 +0100 | [diff] [blame] | 4158 | dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev); |
Wolfram Sang | 5b16668 | 2020-03-12 14:45:07 +0100 | [diff] [blame] | 4159 | return to_spi_device(dev); |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, |
| 4163 | void *arg) |
| 4164 | { |
| 4165 | struct acpi_device *adev = arg; |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4166 | struct spi_controller *ctlr; |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 4167 | struct spi_device *spi; |
| 4168 | |
| 4169 | switch (value) { |
| 4170 | case ACPI_RECONFIG_DEVICE_ADD: |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4171 | ctlr = acpi_spi_find_controller_by_adev(adev->parent); |
| 4172 | if (!ctlr) |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 4173 | break; |
| 4174 | |
Geert Uytterhoeven | 8caab75 | 2017-06-13 13:23:52 +0200 | [diff] [blame] | 4175 | acpi_register_spi_device(ctlr, adev); |
| 4176 | put_device(&ctlr->dev); |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 4177 | break; |
| 4178 | case ACPI_RECONFIG_DEVICE_REMOVE: |
| 4179 | if (!acpi_device_enumerated(adev)) |
| 4180 | break; |
| 4181 | |
| 4182 | spi = acpi_spi_find_device_by_adev(adev); |
| 4183 | if (!spi) |
| 4184 | break; |
| 4185 | |
| 4186 | spi_unregister_device(spi); |
| 4187 | put_device(&spi->dev); |
| 4188 | break; |
| 4189 | } |
| 4190 | |
| 4191 | return NOTIFY_OK; |
| 4192 | } |
| 4193 | |
| 4194 | static struct notifier_block spi_acpi_notifier = { |
| 4195 | .notifier_call = acpi_spi_notify, |
| 4196 | }; |
| 4197 | #else |
| 4198 | extern struct notifier_block spi_acpi_notifier; |
| 4199 | #endif |
| 4200 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4201 | static int __init spi_init(void) |
| 4202 | { |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 4203 | int status; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4204 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 4205 | buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 4206 | if (!buf) { |
| 4207 | status = -ENOMEM; |
| 4208 | goto err0; |
| 4209 | } |
| 4210 | |
| 4211 | status = bus_register(&spi_bus_type); |
| 4212 | if (status < 0) |
| 4213 | goto err1; |
| 4214 | |
| 4215 | status = class_register(&spi_master_class); |
| 4216 | if (status < 0) |
| 4217 | goto err2; |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4218 | |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 4219 | if (IS_ENABLED(CONFIG_SPI_SLAVE)) { |
| 4220 | status = class_register(&spi_slave_class); |
| 4221 | if (status < 0) |
| 4222 | goto err3; |
| 4223 | } |
| 4224 | |
Fabio Estevam | 5267720 | 2014-11-26 20:13:57 -0200 | [diff] [blame] | 4225 | if (IS_ENABLED(CONFIG_OF_DYNAMIC)) |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4226 | WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); |
Octavian Purdila | 7f24467 | 2016-07-08 19:13:11 +0300 | [diff] [blame] | 4227 | if (IS_ENABLED(CONFIG_ACPI)) |
| 4228 | WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); |
Pantelis Antoniou | ce79d54 | 2014-10-28 22:36:05 +0200 | [diff] [blame] | 4229 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4230 | return 0; |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 4231 | |
Geert Uytterhoeven | 6c36406 | 2017-05-22 15:11:41 +0200 | [diff] [blame] | 4232 | err3: |
| 4233 | class_unregister(&spi_master_class); |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 4234 | err2: |
| 4235 | bus_unregister(&spi_bus_type); |
| 4236 | err1: |
| 4237 | kfree(buf); |
| 4238 | buf = NULL; |
| 4239 | err0: |
| 4240 | return status; |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4241 | } |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 4242 | |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4243 | /* board_info is normally registered in arch_initcall(), |
| 4244 | * but even essential drivers wait till later |
David Brownell | b885244 | 2006-01-08 13:34:23 -0800 | [diff] [blame] | 4245 | * |
| 4246 | * REVISIT only boardinfo really needs static linking. the rest (device and |
| 4247 | * driver registration) _could_ be dynamically linked (modular) ... costs |
| 4248 | * include needing to have boardinfo data structures be much more public. |
David Brownell | 8ae12a0 | 2006-01-08 13:34:19 -0800 | [diff] [blame] | 4249 | */ |
David Brownell | 673c0c0 | 2008-10-15 22:02:46 -0700 | [diff] [blame] | 4250 | postcore_initcall(spi_init); |