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