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