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