blob: d07517151340e4e3b0eef1630dec6287b98b7462 [file] [log] [blame]
Marco Felschb445bfc2018-09-25 11:42:28 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Brown787f4882018-11-29 16:24:37 +00002// SPI init/core code
3//
4// Copyright (C) 2005 David Brownell
5// Copyright (C) 2008 Secret Lab Technologies Ltd.
David Brownell8ae12a02006-01-08 13:34:19 -08006
David Brownell8ae12a02006-01-08 13:34:19 -08007#include <linux/kernel.h>
8#include <linux/device.h>
9#include <linux/init.h>
10#include <linux/cache.h>
Mark Brown99adef32014-01-16 12:22:43 +000011#include <linux/dma-mapping.h>
12#include <linux/dmaengine.h>
Matthias Kaehlcke94040822007-07-17 04:04:16 -070013#include <linux/mutex.h>
Sinan Akman2b7a32f2010-10-02 21:28:29 -060014#include <linux/of_device.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060015#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020016#include <linux/clk/clk-conf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Anton Vorontsove0626e32009-09-22 16:46:08 -070018#include <linux/mod_devicetable.h>
David Brownell8ae12a02006-01-08 13:34:19 -080019#include <linux/spi/spi.h>
Boris Brezillonb5932f52018-04-26 18:18:15 +020020#include <linux/spi/spi-mem.h>
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +010021#include <linux/of_gpio.h>
Linus Walleijf3186dd2019-01-07 16:51:50 +010022#include <linux/gpio/consumer.h>
Mark Brown3ae22e82010-12-25 15:32:27 +010023#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020024#include <linux/pm_domain.h>
Dmitry Torokhov826cf172017-02-28 14:25:18 -080025#include <linux/property.h>
Paul Gortmaker025ed132011-07-10 12:57:55 -040026#include <linux/export.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060027#include <linux/sched/rt.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010028#include <uapi/linux/sched/types.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010029#include <linux/delay.h>
30#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010031#include <linux/ioport.h>
32#include <linux/acpi.h>
Vignesh Rb1b81532016-08-17 15:22:36 +053033#include <linux/highmem.h>
Suniel Mahesh9b61e302017-08-03 10:05:57 +053034#include <linux/idr.h>
Lukas Wunner8a2e4872017-08-01 14:10:41 +020035#include <linux/platform_data/x86/apple.h>
David Brownell8ae12a02006-01-08 13:34:19 -080036
Mark Brown56ec1972013-10-07 19:33:53 +010037#define CREATE_TRACE_POINTS
38#include <trace/events/spi.h>
Arnd Bergmannca1438d2019-03-21 13:42:25 +010039EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);
40EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
Suniel Mahesh9b61e302017-08-03 10:05:57 +053041
Boris Brezillon46336962018-04-22 20:35:14 +020042#include "internals.h"
43
Suniel Mahesh9b61e302017-08-03 10:05:57 +053044static DEFINE_IDR(spi_master_idr);
Mark Brown56ec1972013-10-07 19:33:53 +010045
David Brownell8ae12a02006-01-08 13:34:19 -080046static void spidev_release(struct device *dev)
47{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080048 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080049
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020050 /* spi controllers may cleanup for released devices */
51 if (spi->controller->cleanup)
52 spi->controller->cleanup(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080053
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020054 spi_controller_put(spi->controller);
Trent Piepho50395632018-09-20 19:18:32 +000055 kfree(spi->driver_override);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000056 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080057}
58
59static ssize_t
60modalias_show(struct device *dev, struct device_attribute *a, char *buf)
61{
62 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080063 int len;
64
65 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
66 if (len != -ENODEV)
67 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080068
Grant Likelyd8e328b2012-05-20 00:08:13 -060069 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080070}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070071static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080072
Trent Piepho50395632018-09-20 19:18:32 +000073static ssize_t driver_override_store(struct device *dev,
74 struct device_attribute *a,
75 const char *buf, size_t count)
76{
77 struct spi_device *spi = to_spi_device(dev);
78 const char *end = memchr(buf, '\n', count);
79 const size_t len = end ? end - buf : count;
80 const char *driver_override, *old;
81
82 /* We need to keep extra room for a newline when displaying value */
83 if (len >= (PAGE_SIZE - 1))
84 return -EINVAL;
85
86 driver_override = kstrndup(buf, len, GFP_KERNEL);
87 if (!driver_override)
88 return -ENOMEM;
89
90 device_lock(dev);
91 old = spi->driver_override;
92 if (len) {
93 spi->driver_override = driver_override;
94 } else {
95 /* Emptry string, disable driver override */
96 spi->driver_override = NULL;
97 kfree(driver_override);
98 }
99 device_unlock(dev);
100 kfree(old);
101
102 return count;
103}
104
105static ssize_t driver_override_show(struct device *dev,
106 struct device_attribute *a, char *buf)
107{
108 const struct spi_device *spi = to_spi_device(dev);
109 ssize_t len;
110
111 device_lock(dev);
112 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : "");
113 device_unlock(dev);
114 return len;
115}
116static DEVICE_ATTR_RW(driver_override);
117
Martin Sperleca2ebc2015-06-22 13:00:36 +0000118#define SPI_STATISTICS_ATTRS(field, file) \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200119static ssize_t spi_controller_##field##_show(struct device *dev, \
120 struct device_attribute *attr, \
121 char *buf) \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000122{ \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200123 struct spi_controller *ctlr = container_of(dev, \
124 struct spi_controller, dev); \
125 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000126} \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200127static struct device_attribute dev_attr_spi_controller_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +0200128 .attr = { .name = file, .mode = 0444 }, \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200129 .show = spi_controller_##field##_show, \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000130}; \
131static ssize_t spi_device_##field##_show(struct device *dev, \
132 struct device_attribute *attr, \
133 char *buf) \
134{ \
Geliang Tangd1eba932015-12-23 00:18:41 +0800135 struct spi_device *spi = to_spi_device(dev); \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000136 return spi_statistics_##field##_show(&spi->statistics, buf); \
137} \
138static struct device_attribute dev_attr_spi_device_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +0200139 .attr = { .name = file, .mode = 0444 }, \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000140 .show = spi_device_##field##_show, \
141}
142
143#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
144static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
145 char *buf) \
146{ \
147 unsigned long flags; \
148 ssize_t len; \
149 spin_lock_irqsave(&stat->lock, flags); \
150 len = sprintf(buf, format_string, stat->field); \
151 spin_unlock_irqrestore(&stat->lock, flags); \
152 return len; \
153} \
154SPI_STATISTICS_ATTRS(name, file)
155
156#define SPI_STATISTICS_SHOW(field, format_string) \
157 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
158 field, format_string)
159
160SPI_STATISTICS_SHOW(messages, "%lu");
161SPI_STATISTICS_SHOW(transfers, "%lu");
162SPI_STATISTICS_SHOW(errors, "%lu");
163SPI_STATISTICS_SHOW(timedout, "%lu");
164
165SPI_STATISTICS_SHOW(spi_sync, "%lu");
166SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
167SPI_STATISTICS_SHOW(spi_async, "%lu");
168
169SPI_STATISTICS_SHOW(bytes, "%llu");
170SPI_STATISTICS_SHOW(bytes_rx, "%llu");
171SPI_STATISTICS_SHOW(bytes_tx, "%llu");
172
Martin Sperl6b7bc062015-06-22 13:02:04 +0000173#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
174 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
175 "transfer_bytes_histo_" number, \
176 transfer_bytes_histo[index], "%lu")
177SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
178SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
179SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
180SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
181SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
182SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
183SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
184SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
185SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
186SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
187SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
188SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
189SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
190SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
191SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
192SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
193SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
194
Martin Sperld9f12122015-12-14 15:20:20 +0000195SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
196
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700197static struct attribute *spi_dev_attrs[] = {
198 &dev_attr_modalias.attr,
Trent Piepho50395632018-09-20 19:18:32 +0000199 &dev_attr_driver_override.attr,
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700200 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800201};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000202
203static const struct attribute_group spi_dev_group = {
204 .attrs = spi_dev_attrs,
205};
206
207static struct attribute *spi_device_statistics_attrs[] = {
208 &dev_attr_spi_device_messages.attr,
209 &dev_attr_spi_device_transfers.attr,
210 &dev_attr_spi_device_errors.attr,
211 &dev_attr_spi_device_timedout.attr,
212 &dev_attr_spi_device_spi_sync.attr,
213 &dev_attr_spi_device_spi_sync_immediate.attr,
214 &dev_attr_spi_device_spi_async.attr,
215 &dev_attr_spi_device_bytes.attr,
216 &dev_attr_spi_device_bytes_rx.attr,
217 &dev_attr_spi_device_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000218 &dev_attr_spi_device_transfer_bytes_histo0.attr,
219 &dev_attr_spi_device_transfer_bytes_histo1.attr,
220 &dev_attr_spi_device_transfer_bytes_histo2.attr,
221 &dev_attr_spi_device_transfer_bytes_histo3.attr,
222 &dev_attr_spi_device_transfer_bytes_histo4.attr,
223 &dev_attr_spi_device_transfer_bytes_histo5.attr,
224 &dev_attr_spi_device_transfer_bytes_histo6.attr,
225 &dev_attr_spi_device_transfer_bytes_histo7.attr,
226 &dev_attr_spi_device_transfer_bytes_histo8.attr,
227 &dev_attr_spi_device_transfer_bytes_histo9.attr,
228 &dev_attr_spi_device_transfer_bytes_histo10.attr,
229 &dev_attr_spi_device_transfer_bytes_histo11.attr,
230 &dev_attr_spi_device_transfer_bytes_histo12.attr,
231 &dev_attr_spi_device_transfer_bytes_histo13.attr,
232 &dev_attr_spi_device_transfer_bytes_histo14.attr,
233 &dev_attr_spi_device_transfer_bytes_histo15.attr,
234 &dev_attr_spi_device_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000235 &dev_attr_spi_device_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000236 NULL,
237};
238
239static const struct attribute_group spi_device_statistics_group = {
240 .name = "statistics",
241 .attrs = spi_device_statistics_attrs,
242};
243
244static const struct attribute_group *spi_dev_groups[] = {
245 &spi_dev_group,
246 &spi_device_statistics_group,
247 NULL,
248};
249
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200250static struct attribute *spi_controller_statistics_attrs[] = {
251 &dev_attr_spi_controller_messages.attr,
252 &dev_attr_spi_controller_transfers.attr,
253 &dev_attr_spi_controller_errors.attr,
254 &dev_attr_spi_controller_timedout.attr,
255 &dev_attr_spi_controller_spi_sync.attr,
256 &dev_attr_spi_controller_spi_sync_immediate.attr,
257 &dev_attr_spi_controller_spi_async.attr,
258 &dev_attr_spi_controller_bytes.attr,
259 &dev_attr_spi_controller_bytes_rx.attr,
260 &dev_attr_spi_controller_bytes_tx.attr,
261 &dev_attr_spi_controller_transfer_bytes_histo0.attr,
262 &dev_attr_spi_controller_transfer_bytes_histo1.attr,
263 &dev_attr_spi_controller_transfer_bytes_histo2.attr,
264 &dev_attr_spi_controller_transfer_bytes_histo3.attr,
265 &dev_attr_spi_controller_transfer_bytes_histo4.attr,
266 &dev_attr_spi_controller_transfer_bytes_histo5.attr,
267 &dev_attr_spi_controller_transfer_bytes_histo6.attr,
268 &dev_attr_spi_controller_transfer_bytes_histo7.attr,
269 &dev_attr_spi_controller_transfer_bytes_histo8.attr,
270 &dev_attr_spi_controller_transfer_bytes_histo9.attr,
271 &dev_attr_spi_controller_transfer_bytes_histo10.attr,
272 &dev_attr_spi_controller_transfer_bytes_histo11.attr,
273 &dev_attr_spi_controller_transfer_bytes_histo12.attr,
274 &dev_attr_spi_controller_transfer_bytes_histo13.attr,
275 &dev_attr_spi_controller_transfer_bytes_histo14.attr,
276 &dev_attr_spi_controller_transfer_bytes_histo15.attr,
277 &dev_attr_spi_controller_transfer_bytes_histo16.attr,
278 &dev_attr_spi_controller_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000279 NULL,
280};
281
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200282static const struct attribute_group spi_controller_statistics_group = {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000283 .name = "statistics",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200284 .attrs = spi_controller_statistics_attrs,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000285};
286
287static const struct attribute_group *spi_master_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200288 &spi_controller_statistics_group,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000289 NULL,
290};
291
292void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
293 struct spi_transfer *xfer,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200294 struct spi_controller *ctlr)
Martin Sperleca2ebc2015-06-22 13:00:36 +0000295{
296 unsigned long flags;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000297 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
298
299 if (l2len < 0)
300 l2len = 0;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000301
302 spin_lock_irqsave(&stats->lock, flags);
303
304 stats->transfers++;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000305 stats->transfer_bytes_histo[l2len]++;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000306
307 stats->bytes += xfer->len;
308 if ((xfer->tx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200309 (xfer->tx_buf != ctlr->dummy_tx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000310 stats->bytes_tx += xfer->len;
311 if ((xfer->rx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200312 (xfer->rx_buf != ctlr->dummy_rx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000313 stats->bytes_rx += xfer->len;
314
315 spin_unlock_irqrestore(&stats->lock, flags);
316}
317EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800318
319/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
320 * and the sysfs version makes coldplug work too.
321 */
322
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700323static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
324 const struct spi_device *sdev)
325{
326 while (id->name[0]) {
327 if (!strcmp(sdev->modalias, id->name))
328 return id;
329 id++;
330 }
331 return NULL;
332}
333
334const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
335{
336 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
337
338 return spi_match_id(sdrv->id_table, sdev);
339}
340EXPORT_SYMBOL_GPL(spi_get_device_id);
341
David Brownell8ae12a02006-01-08 13:34:19 -0800342static int spi_match_device(struct device *dev, struct device_driver *drv)
343{
344 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700345 const struct spi_driver *sdrv = to_spi_driver(drv);
346
Trent Piepho50395632018-09-20 19:18:32 +0000347 /* Check override first, and if set, only use the named driver */
348 if (spi->driver_override)
349 return strcmp(spi->driver_override, drv->name) == 0;
350
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600351 /* Attempt an OF style match */
352 if (of_driver_match_device(dev, drv))
353 return 1;
354
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100355 /* Then try ACPI */
356 if (acpi_driver_match_device(dev, drv))
357 return 1;
358
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700359 if (sdrv->id_table)
360 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800361
Kay Sievers35f74fc2009-01-06 10:44:37 -0800362 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800363}
364
Kay Sievers7eff2e72007-08-14 15:15:12 +0200365static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800366{
367 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800368 int rc;
369
370 rc = acpi_device_uevent_modalias(dev, env);
371 if (rc != -ENODEV)
372 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800373
Andy Shevchenko28566702017-07-26 16:14:00 +0300374 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800375}
376
David Brownell8ae12a02006-01-08 13:34:19 -0800377struct bus_type spi_bus_type = {
378 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700379 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800380 .match = spi_match_device,
381 .uevent = spi_uevent,
David Brownell8ae12a02006-01-08 13:34:19 -0800382};
383EXPORT_SYMBOL_GPL(spi_bus_type);
384
David Brownellb8852442006-01-08 13:34:23 -0800385
386static int spi_drv_probe(struct device *dev)
387{
388 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Jon Hunter44af7922015-10-09 15:45:55 +0100389 struct spi_device *spi = to_spi_device(dev);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300390 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800391
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200392 ret = of_clk_set_defaults(dev->of_node, false);
393 if (ret)
394 return ret;
395
Jon Hunter44af7922015-10-09 15:45:55 +0100396 if (dev->of_node) {
397 spi->irq = of_irq_get(dev->of_node, 0);
398 if (spi->irq == -EPROBE_DEFER)
399 return -EPROBE_DEFER;
400 if (spi->irq < 0)
401 spi->irq = 0;
402 }
403
Ulf Hansson676e7c22014-09-19 20:27:41 +0200404 ret = dev_pm_domain_attach(dev, true);
Ulf Hansson71f277a2018-04-26 10:53:10 +0200405 if (ret)
406 return ret;
407
408 ret = sdrv->probe(spi);
409 if (ret)
410 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300411
412 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800413}
414
415static int spi_drv_remove(struct device *dev)
416{
417 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300418 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800419
Jean Delvareaec35f42014-02-13 15:28:41 +0100420 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200421 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300422
423 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800424}
425
426static void spi_drv_shutdown(struct device *dev)
427{
428 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
429
430 sdrv->shutdown(to_spi_device(dev));
431}
432
David Brownell33e34dc2007-05-08 00:32:21 -0700433/**
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500434 * __spi_register_driver - register a SPI driver
Thierry Reding88c93212015-11-10 13:03:04 +0100435 * @owner: owner module of the driver to register
David Brownell33e34dc2007-05-08 00:32:21 -0700436 * @sdrv: the driver to register
437 * Context: can sleep
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200438 *
439 * Return: zero on success, else a negative error code.
David Brownell33e34dc2007-05-08 00:32:21 -0700440 */
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500441int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
David Brownellb8852442006-01-08 13:34:23 -0800442{
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500443 sdrv->driver.owner = owner;
David Brownellb8852442006-01-08 13:34:23 -0800444 sdrv->driver.bus = &spi_bus_type;
445 if (sdrv->probe)
446 sdrv->driver.probe = spi_drv_probe;
447 if (sdrv->remove)
448 sdrv->driver.remove = spi_drv_remove;
449 if (sdrv->shutdown)
450 sdrv->driver.shutdown = spi_drv_shutdown;
451 return driver_register(&sdrv->driver);
452}
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500453EXPORT_SYMBOL_GPL(__spi_register_driver);
David Brownellb8852442006-01-08 13:34:23 -0800454
David Brownell8ae12a02006-01-08 13:34:19 -0800455/*-------------------------------------------------------------------------*/
456
457/* SPI devices should normally not be created by SPI device drivers; that
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200458 * would make them board-specific. Similarly with SPI controller drivers.
David Brownell8ae12a02006-01-08 13:34:19 -0800459 * Device registration normally goes into like arch/.../mach.../board-YYY.c
460 * with other readonly (flashable) information about mainboard devices.
461 */
462
463struct boardinfo {
464 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800465 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800466};
467
468static LIST_HEAD(board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200469static LIST_HEAD(spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800470
471/*
472 * Used to protect add/del opertion for board_info list and
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200473 * spi_controller list, and their matching process
Suniel Mahesh9a9a0472017-08-17 18:18:22 +0530474 * also used to protect object of type struct idr
Feng Tang2b9603a2010-08-02 15:52:15 +0800475 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700476static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800477
Grant Likelydc87c982008-05-15 16:50:22 -0600478/**
479 * spi_alloc_device - Allocate a new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200480 * @ctlr: Controller to which device is connected
Grant Likelydc87c982008-05-15 16:50:22 -0600481 * Context: can sleep
482 *
483 * Allows a driver to allocate and initialize a spi_device without
484 * registering it immediately. This allows a driver to directly
485 * fill the spi_device with device parameters before calling
486 * spi_add_device() on it.
487 *
488 * Caller is responsible to call spi_add_device() on the returned
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200489 * spi_device structure to add it to the SPI controller. If the caller
Grant Likelydc87c982008-05-15 16:50:22 -0600490 * needs to discard the spi_device without adding it, then it should
491 * call spi_dev_put() on it.
492 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200493 * Return: a pointer to the new device, or NULL.
Grant Likelydc87c982008-05-15 16:50:22 -0600494 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200495struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
Grant Likelydc87c982008-05-15 16:50:22 -0600496{
497 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600498
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200499 if (!spi_controller_get(ctlr))
Grant Likelydc87c982008-05-15 16:50:22 -0600500 return NULL;
501
Jingoo Han5fe5f052013-10-14 10:31:51 +0900502 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600503 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200504 spi_controller_put(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600505 return NULL;
506 }
507
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200508 spi->master = spi->controller = ctlr;
509 spi->dev.parent = &ctlr->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600510 spi->dev.bus = &spi_bus_type;
511 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100512 spi->cs_gpio = -ENOENT;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000513
514 spin_lock_init(&spi->statistics.lock);
515
Grant Likelydc87c982008-05-15 16:50:22 -0600516 device_initialize(&spi->dev);
517 return spi;
518}
519EXPORT_SYMBOL_GPL(spi_alloc_device);
520
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200521static void spi_dev_set_name(struct spi_device *spi)
522{
523 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
524
525 if (adev) {
526 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
527 return;
528 }
529
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200530 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200531 spi->chip_select);
532}
533
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200534static int spi_dev_check(struct device *dev, void *data)
535{
536 struct spi_device *spi = to_spi_device(dev);
537 struct spi_device *new_spi = data;
538
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200539 if (spi->controller == new_spi->controller &&
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200540 spi->chip_select == new_spi->chip_select)
541 return -EBUSY;
542 return 0;
543}
544
Grant Likelydc87c982008-05-15 16:50:22 -0600545/**
546 * spi_add_device - Add spi_device allocated with spi_alloc_device
547 * @spi: spi_device to register
548 *
549 * Companion function to spi_alloc_device. Devices allocated with
550 * spi_alloc_device can be added onto the spi bus with this function.
551 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200552 * Return: 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600553 */
554int spi_add_device(struct spi_device *spi)
555{
David Brownelle48880e2008-08-15 00:40:44 -0700556 static DEFINE_MUTEX(spi_add_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200557 struct spi_controller *ctlr = spi->controller;
558 struct device *dev = ctlr->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600559 int status;
560
561 /* Chipselects are numbered 0..max; validate. */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200562 if (spi->chip_select >= ctlr->num_chipselect) {
563 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
564 ctlr->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600565 return -EINVAL;
566 }
567
568 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200569 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700570
571 /* We need to make sure there's no other device with this
572 * chipselect **BEFORE** we call setup(), else we'll trash
573 * its configuration. Lock against concurrent add() calls.
574 */
575 mutex_lock(&spi_add_lock);
576
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200577 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
578 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700579 dev_err(dev, "chipselect %d already in use\n",
580 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700581 goto done;
582 }
583
Linus Walleijf3186dd2019-01-07 16:51:50 +0100584 /* Descriptors take precedence */
585 if (ctlr->cs_gpiods)
586 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
587 else if (ctlr->cs_gpios)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200588 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100589
David Brownelle48880e2008-08-15 00:40:44 -0700590 /* Drivers may modify this initial i/o setup, but will
591 * normally rely on the device being setup. Devices
592 * using SPI_CS_HIGH can't coexist well otherwise...
593 */
David Brownell7d077192009-06-17 16:26:03 -0700594 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600595 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200596 dev_err(dev, "can't setup %s, status %d\n",
597 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700598 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600599 }
600
David Brownelle48880e2008-08-15 00:40:44 -0700601 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600602 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700603 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200604 dev_err(dev, "can't add %s, status %d\n",
605 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700606 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800607 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600608
David Brownelle48880e2008-08-15 00:40:44 -0700609done:
610 mutex_unlock(&spi_add_lock);
611 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600612}
613EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800614
David Brownell33e34dc2007-05-08 00:32:21 -0700615/**
616 * spi_new_device - instantiate one new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200617 * @ctlr: Controller to which device is connected
David Brownell33e34dc2007-05-08 00:32:21 -0700618 * @chip: Describes the SPI device
619 * Context: can sleep
620 *
621 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800622 * after board init creates the hard-wired devices. Some development
623 * platforms may not be able to use spi_register_board_info though, and
624 * this is exported so that for example a USB or parport based adapter
625 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700626 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200627 * Return: the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800628 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200629struct spi_device *spi_new_device(struct spi_controller *ctlr,
Adrian Bunke9d5a462007-03-26 21:32:23 -0800630 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800631{
632 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800633 int status;
634
David Brownell082c8cb2007-07-31 00:39:45 -0700635 /* NOTE: caller did any chip->bus_num checks necessary.
636 *
637 * Also, unless we change the return value convention to use
638 * error-or-pointer (not NULL-or-pointer), troubleshootability
639 * suggests syslogged diagnostics are best here (ugh).
640 */
641
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200642 proxy = spi_alloc_device(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600643 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800644 return NULL;
645
Grant Likely102eb972008-07-23 21:29:55 -0700646 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
647
David Brownell8ae12a02006-01-08 13:34:19 -0800648 proxy->chip_select = chip->chip_select;
649 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700650 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800651 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700652 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800653 proxy->dev.platform_data = (void *) chip->platform_data;
654 proxy->controller_data = chip->controller_data;
655 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800656
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800657 if (chip->properties) {
658 status = device_add_properties(&proxy->dev, chip->properties);
659 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200660 dev_err(&ctlr->dev,
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800661 "failed to add properties to '%s': %d\n",
662 chip->modalias, status);
663 goto err_dev_put;
664 }
David Brownell8ae12a02006-01-08 13:34:19 -0800665 }
666
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800667 status = spi_add_device(proxy);
668 if (status < 0)
669 goto err_remove_props;
670
David Brownell8ae12a02006-01-08 13:34:19 -0800671 return proxy;
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800672
673err_remove_props:
674 if (chip->properties)
675 device_remove_properties(&proxy->dev);
676err_dev_put:
677 spi_dev_put(proxy);
678 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800679}
680EXPORT_SYMBOL_GPL(spi_new_device);
681
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100682/**
683 * spi_unregister_device - unregister a single SPI device
684 * @spi: spi_device to unregister
685 *
686 * Start making the passed SPI device vanish. Normally this would be handled
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200687 * by spi_unregister_controller().
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100688 */
689void spi_unregister_device(struct spi_device *spi)
690{
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100691 if (!spi)
692 return;
693
Johan Hovold83241472017-01-30 17:47:05 +0100694 if (spi->dev.of_node) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100695 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
Johan Hovold83241472017-01-30 17:47:05 +0100696 of_node_put(spi->dev.of_node);
697 }
Octavian Purdila7f244672016-07-08 19:13:11 +0300698 if (ACPI_COMPANION(&spi->dev))
699 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100700 device_unregister(&spi->dev);
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100701}
702EXPORT_SYMBOL_GPL(spi_unregister_device);
703
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200704static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
705 struct spi_board_info *bi)
Feng Tang2b9603a2010-08-02 15:52:15 +0800706{
707 struct spi_device *dev;
708
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200709 if (ctlr->bus_num != bi->bus_num)
Feng Tang2b9603a2010-08-02 15:52:15 +0800710 return;
711
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200712 dev = spi_new_device(ctlr, bi);
Feng Tang2b9603a2010-08-02 15:52:15 +0800713 if (!dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200714 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
Feng Tang2b9603a2010-08-02 15:52:15 +0800715 bi->modalias);
716}
717
David Brownell33e34dc2007-05-08 00:32:21 -0700718/**
719 * spi_register_board_info - register SPI devices for a given board
720 * @info: array of chip descriptors
721 * @n: how many descriptors are provided
722 * Context: can sleep
723 *
David Brownell8ae12a02006-01-08 13:34:19 -0800724 * Board-specific early init code calls this (probably during arch_initcall)
725 * with segments of the SPI device table. Any device nodes are created later,
726 * after the relevant parent SPI controller (bus_num) is defined. We keep
727 * this table of devices forever, so that reloading a controller driver will
728 * not make Linux forget about these hard-wired devices.
729 *
730 * Other code can also call this, e.g. a particular add-on board might provide
731 * SPI devices through its expansion connector, so code initializing that board
732 * would naturally declare its SPI devices.
733 *
734 * The board info passed can safely be __initdata ... but be careful of
735 * any embedded pointers (platform_data, etc), they're copied as-is.
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800736 * Device properties are deep-copied though.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200737 *
738 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -0800739 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000740int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800741{
Feng Tang2b9603a2010-08-02 15:52:15 +0800742 struct boardinfo *bi;
743 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800744
Xiubo Lic7908a32014-09-24 14:30:29 +0800745 if (!n)
Dmitry Torokhovf974cf52017-02-28 14:25:19 -0800746 return 0;
Xiubo Lic7908a32014-09-24 14:30:29 +0800747
Markus Elfringf9bdb7f2017-01-13 12:28:04 +0100748 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800749 if (!bi)
750 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800751
Feng Tang2b9603a2010-08-02 15:52:15 +0800752 for (i = 0; i < n; i++, bi++, info++) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200753 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -0800754
Feng Tang2b9603a2010-08-02 15:52:15 +0800755 memcpy(&bi->board_info, info, sizeof(*info));
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800756 if (info->properties) {
757 bi->board_info.properties =
758 property_entries_dup(info->properties);
759 if (IS_ERR(bi->board_info.properties))
760 return PTR_ERR(bi->board_info.properties);
761 }
762
Feng Tang2b9603a2010-08-02 15:52:15 +0800763 mutex_lock(&board_lock);
764 list_add_tail(&bi->list, &board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200765 list_for_each_entry(ctlr, &spi_controller_list, list)
766 spi_match_controller_to_boardinfo(ctlr,
767 &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +0800768 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800769 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800770
771 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800772}
773
774/*-------------------------------------------------------------------------*/
775
Mark Brownb1589352013-10-05 11:50:40 +0100776static void spi_set_cs(struct spi_device *spi, bool enable)
777{
778 if (spi->mode & SPI_CS_HIGH)
779 enable = !enable;
780
Linus Walleijf3186dd2019-01-07 16:51:50 +0100781 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
782 /*
783 * Honour the SPI_NO_CS flag and invert the enable line, as
784 * active low is default for SPI. Execution paths that handle
785 * polarity inversion in gpiolib (such as device tree) will
786 * enforce active high using the SPI_CS_HIGH resulting in a
787 * double inversion through the code above.
788 */
789 if (!(spi->mode & SPI_NO_CS)) {
790 if (spi->cs_gpiod)
Felix Fietkau28f76042019-02-10 00:38:25 +0100791 gpiod_set_value_cansleep(spi->cs_gpiod,
792 !enable);
Linus Walleijf3186dd2019-01-07 16:51:50 +0100793 else
Felix Fietkau28f76042019-02-10 00:38:25 +0100794 gpio_set_value_cansleep(spi->cs_gpio, !enable);
Linus Walleijf3186dd2019-01-07 16:51:50 +0100795 }
Thor Thayer8eee6b92016-10-10 09:25:24 -0500796 /* Some SPI masters need both GPIO CS & slave_select */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200797 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
798 spi->controller->set_cs)
799 spi->controller->set_cs(spi, !enable);
800 } else if (spi->controller->set_cs) {
801 spi->controller->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500802 }
Mark Brownb1589352013-10-05 11:50:40 +0100803}
804
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200805#ifdef CONFIG_HAS_DMA
Boris Brezillon46336962018-04-22 20:35:14 +0200806int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
807 struct sg_table *sgt, void *buf, size_t len,
808 enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000809{
810 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200811 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530812#ifdef CONFIG_HIGHMEM
813 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
814 (unsigned long)buf < (PKMAP_BASE +
815 (LAST_PKMAP * PAGE_SIZE)));
816#else
817 const bool kmap_buf = false;
818#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500819 int desc_len;
820 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000821 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600822 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000823 void *sg_buf;
824 size_t min;
825 int i, ret;
826
Vignesh Rb1b81532016-08-17 15:22:36 +0530827 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200828 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500829 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530830 } else if (virt_addr_valid(buf)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200831 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500832 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530833 } else {
834 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500835 }
836
Mark Brown6ad45a22014-02-02 13:47:47 +0000837 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
838 if (ret != 0)
839 return ret;
840
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600841 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000842 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000843
Vignesh Rb1b81532016-08-17 15:22:36 +0530844 if (vmalloced_buf || kmap_buf) {
Maxime Chevallierce993192018-03-02 15:55:09 +0100845 /*
846 * Next scatterlist entry size is the minimum between
847 * the desc_len and the remaining buffer length that
848 * fits in a page.
849 */
850 min = min_t(size_t, desc_len,
851 min_t(size_t, len,
852 PAGE_SIZE - offset_in_page(buf)));
Vignesh Rb1b81532016-08-17 15:22:36 +0530853 if (vmalloced_buf)
854 vm_page = vmalloc_to_page(buf);
855 else
856 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000857 if (!vm_page) {
858 sg_free_table(sgt);
859 return -ENOMEM;
860 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600861 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000862 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000863 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500864 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000865 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600866 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000867 }
868
Mark Brown6ad45a22014-02-02 13:47:47 +0000869 buf += min;
870 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600871 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000872 }
873
874 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200875 if (!ret)
876 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000877 if (ret < 0) {
878 sg_free_table(sgt);
879 return ret;
880 }
881
882 sgt->nents = ret;
883
884 return 0;
885}
886
Boris Brezillon46336962018-04-22 20:35:14 +0200887void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
888 struct sg_table *sgt, enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000889{
890 if (sgt->orig_nents) {
891 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
892 sg_free_table(sgt);
893 }
894}
895
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200896static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000897{
Mark Brown99adef32014-01-16 12:22:43 +0000898 struct device *tx_dev, *rx_dev;
899 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000900 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000901
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200902 if (!ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000903 return 0;
904
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200905 if (ctlr->dma_tx)
906 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800907 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200908 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800909
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200910 if (ctlr->dma_rx)
911 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800912 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200913 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000914
915 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200916 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000917 continue;
918
919 if (xfer->tx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200920 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000921 (void *)xfer->tx_buf, xfer->len,
922 DMA_TO_DEVICE);
923 if (ret != 0)
924 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000925 }
926
927 if (xfer->rx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200928 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000929 xfer->rx_buf, xfer->len,
930 DMA_FROM_DEVICE);
931 if (ret != 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200932 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000933 DMA_TO_DEVICE);
934 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000935 }
936 }
937 }
938
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200939 ctlr->cur_msg_mapped = true;
Mark Brown99adef32014-01-16 12:22:43 +0000940
941 return 0;
942}
943
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200944static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000945{
946 struct spi_transfer *xfer;
947 struct device *tx_dev, *rx_dev;
948
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200949 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000950 return 0;
951
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200952 if (ctlr->dma_tx)
953 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800954 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200955 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800956
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200957 if (ctlr->dma_rx)
958 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800959 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200960 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000961
962 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200963 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000964 continue;
965
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200966 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
967 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000968 }
969
970 return 0;
971}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200972#else /* !CONFIG_HAS_DMA */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200973static inline int __spi_map_msg(struct spi_controller *ctlr,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200974 struct spi_message *msg)
975{
976 return 0;
977}
978
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200979static inline int __spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000980 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200981{
982 return 0;
983}
984#endif /* !CONFIG_HAS_DMA */
985
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200986static inline int spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000987 struct spi_message *msg)
988{
989 struct spi_transfer *xfer;
990
991 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
992 /*
993 * Restore the original value of tx_buf or rx_buf if they are
994 * NULL.
995 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200996 if (xfer->tx_buf == ctlr->dummy_tx)
Martin Sperl4b786452015-05-25 10:13:10 +0000997 xfer->tx_buf = NULL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200998 if (xfer->rx_buf == ctlr->dummy_rx)
Martin Sperl4b786452015-05-25 10:13:10 +0000999 xfer->rx_buf = NULL;
1000 }
1001
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001002 return __spi_unmap_msg(ctlr, msg);
Martin Sperl4b786452015-05-25 10:13:10 +00001003}
1004
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001005static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001006{
1007 struct spi_transfer *xfer;
1008 void *tmp;
1009 unsigned int max_tx, max_rx;
1010
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001011 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001012 max_tx = 0;
1013 max_rx = 0;
1014
1015 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001016 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001017 !xfer->tx_buf)
1018 max_tx = max(xfer->len, max_tx);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001019 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001020 !xfer->rx_buf)
1021 max_rx = max(xfer->len, max_rx);
1022 }
1023
1024 if (max_tx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001025 tmp = krealloc(ctlr->dummy_tx, max_tx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001026 GFP_KERNEL | GFP_DMA);
1027 if (!tmp)
1028 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001029 ctlr->dummy_tx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001030 memset(tmp, 0, max_tx);
1031 }
1032
1033 if (max_rx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001034 tmp = krealloc(ctlr->dummy_rx, max_rx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001035 GFP_KERNEL | GFP_DMA);
1036 if (!tmp)
1037 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001038 ctlr->dummy_rx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001039 }
1040
1041 if (max_tx || max_rx) {
1042 list_for_each_entry(xfer, &msg->transfers,
1043 transfer_list) {
Chris Lesiak5442dca2019-03-07 20:39:00 +00001044 if (!xfer->len)
1045 continue;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001046 if (!xfer->tx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001047 xfer->tx_buf = ctlr->dummy_tx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001048 if (!xfer->rx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001049 xfer->rx_buf = ctlr->dummy_rx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001050 }
1051 }
1052 }
1053
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001054 return __spi_map_msg(ctlr, msg);
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001055}
Mark Brown99adef32014-01-16 12:22:43 +00001056
Lubomir Rintel810923f2018-11-13 11:22:24 +01001057static int spi_transfer_wait(struct spi_controller *ctlr,
1058 struct spi_message *msg,
1059 struct spi_transfer *xfer)
1060{
1061 struct spi_statistics *statm = &ctlr->statistics;
1062 struct spi_statistics *stats = &msg->spi->statistics;
1063 unsigned long long ms = 1;
1064
1065 if (spi_controller_is_slave(ctlr)) {
1066 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1067 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1068 return -EINTR;
1069 }
1070 } else {
1071 ms = 8LL * 1000LL * xfer->len;
1072 do_div(ms, xfer->speed_hz);
1073 ms += ms + 200; /* some tolerance */
1074
1075 if (ms > UINT_MAX)
1076 ms = UINT_MAX;
1077
1078 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1079 msecs_to_jiffies(ms));
1080
1081 if (ms == 0) {
1082 SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1083 SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1084 dev_err(&msg->spi->dev,
1085 "SPI transfer timed out\n");
1086 return -ETIMEDOUT;
1087 }
1088 }
1089
1090 return 0;
1091}
1092
Martin Sperl0ff2de82019-02-23 08:49:48 +00001093static void _spi_transfer_delay_ns(u32 ns)
1094{
1095 if (!ns)
1096 return;
1097 if (ns <= 1000) {
1098 ndelay(ns);
1099 } else {
1100 u32 us = DIV_ROUND_UP(ns, 1000);
1101
1102 if (us <= 10)
1103 udelay(us);
1104 else
1105 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1106 }
1107}
1108
1109static void _spi_transfer_cs_change_delay(struct spi_message *msg,
1110 struct spi_transfer *xfer)
1111{
1112 u32 delay = xfer->cs_change_delay;
1113 u32 unit = xfer->cs_change_delay_unit;
Martin Sperld5864e52019-02-23 08:49:50 +00001114 u32 hz;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001115
1116 /* return early on "fast" mode - for everything but USECS */
1117 if (!delay && unit != SPI_DELAY_UNIT_USECS)
1118 return;
1119
1120 switch (unit) {
1121 case SPI_DELAY_UNIT_USECS:
1122 /* for compatibility use default of 10us */
1123 if (!delay)
1124 delay = 10000;
1125 else
1126 delay *= 1000;
1127 break;
1128 case SPI_DELAY_UNIT_NSECS: /* nothing to do here */
1129 break;
Martin Sperld5864e52019-02-23 08:49:50 +00001130 case SPI_DELAY_UNIT_SCK:
1131 /* if there is no effective speed know, then approximate
1132 * by underestimating with half the requested hz
1133 */
1134 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1135 delay *= DIV_ROUND_UP(1000000000, hz);
1136 break;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001137 default:
1138 dev_err_once(&msg->spi->dev,
1139 "Use of unsupported delay unit %i, using default of 10us\n",
1140 xfer->cs_change_delay_unit);
1141 delay = 10000;
1142 }
1143 /* now sleep for the requested amount of time */
1144 _spi_transfer_delay_ns(delay);
1145}
1146
Mark Brownb1589352013-10-05 11:50:40 +01001147/*
1148 * spi_transfer_one_message - Default implementation of transfer_one_message()
1149 *
1150 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -07001151 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +01001152 * standard handling of delays and chip select management.
1153 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001154static int spi_transfer_one_message(struct spi_controller *ctlr,
Mark Brownb1589352013-10-05 11:50:40 +01001155 struct spi_message *msg)
1156{
1157 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +01001158 bool keep_cs = false;
1159 int ret = 0;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001160 struct spi_statistics *statm = &ctlr->statistics;
Martin Sperleca2ebc2015-06-22 13:00:36 +00001161 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +01001162
1163 spi_set_cs(msg->spi, true);
1164
Martin Sperleca2ebc2015-06-22 13:00:36 +00001165 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1166 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1167
Mark Brownb1589352013-10-05 11:50:40 +01001168 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1169 trace_spi_transfer_start(msg, xfer);
1170
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001171 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1172 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
Martin Sperleca2ebc2015-06-22 13:00:36 +00001173
Mark Brown38ec10f2014-08-16 16:27:41 +01001174 if (xfer->tx_buf || xfer->rx_buf) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001175 reinit_completion(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001176
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001177 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
Mark Brown38ec10f2014-08-16 16:27:41 +01001178 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001179 SPI_STATISTICS_INCREMENT_FIELD(statm,
1180 errors);
1181 SPI_STATISTICS_INCREMENT_FIELD(stats,
1182 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001183 dev_err(&msg->spi->dev,
1184 "SPI transfer failed: %d\n", ret);
1185 goto out;
1186 }
Mark Brownb1589352013-10-05 11:50:40 +01001187
Mark Brownd57e7962018-11-15 16:08:32 -08001188 if (ret > 0) {
1189 ret = spi_transfer_wait(ctlr, msg, xfer);
1190 if (ret < 0)
1191 msg->status = ret;
1192 }
Mark Brown38ec10f2014-08-16 16:27:41 +01001193 } else {
1194 if (xfer->len)
1195 dev_err(&msg->spi->dev,
1196 "Bufferless transfer has length %u\n",
1197 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001198 }
Mark Brownb1589352013-10-05 11:50:40 +01001199
1200 trace_spi_transfer_stop(msg, xfer);
1201
1202 if (msg->status != -EINPROGRESS)
1203 goto out;
1204
Martin Sperl0ff2de82019-02-23 08:49:48 +00001205 if (xfer->delay_usecs)
1206 _spi_transfer_delay_ns(xfer->delay_usecs * 1000);
Mark Brownb1589352013-10-05 11:50:40 +01001207
1208 if (xfer->cs_change) {
1209 if (list_is_last(&xfer->transfer_list,
1210 &msg->transfers)) {
1211 keep_cs = true;
1212 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001213 spi_set_cs(msg->spi, false);
Martin Sperl0ff2de82019-02-23 08:49:48 +00001214 _spi_transfer_cs_change_delay(msg, xfer);
Mark Brown0b73aa62014-03-29 23:48:07 +00001215 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001216 }
1217 }
1218
1219 msg->actual_length += xfer->len;
1220 }
1221
1222out:
1223 if (ret != 0 || !keep_cs)
1224 spi_set_cs(msg->spi, false);
1225
1226 if (msg->status == -EINPROGRESS)
1227 msg->status = ret;
1228
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001229 if (msg->status && ctlr->handle_err)
1230 ctlr->handle_err(ctlr, msg);
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001231
Noralf Trønnesc9ba7a12019-04-13 20:24:13 +02001232 spi_res_release(ctlr, msg);
1233
Mark Brown0ed56252019-05-09 11:27:17 +09001234 spi_finalize_current_message(ctlr);
1235
Mark Brownb1589352013-10-05 11:50:40 +01001236 return ret;
1237}
1238
1239/**
1240 * spi_finalize_current_transfer - report completion of a transfer
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001241 * @ctlr: the controller reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001242 *
1243 * Called by SPI drivers using the core transfer_one_message()
1244 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001245 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001246 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001247void spi_finalize_current_transfer(struct spi_controller *ctlr)
Mark Brownb1589352013-10-05 11:50:40 +01001248{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001249 complete(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001250}
1251EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1252
Linus Walleijffbbdd212012-02-22 10:05:38 +01001253/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001254 * __spi_pump_messages - function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001255 * @ctlr: controller to process queue for
Mark Brownfc9e0f72014-12-10 13:46:33 +00001256 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001257 *
1258 * This function checks if there is any spi message in the queue that
1259 * needs processing and if so call out to the driver to initialize hardware
1260 * and transfer each message.
1261 *
Mark Brown0461a412014-12-09 21:38:05 +00001262 * Note that it is called both from the kthread itself and also from
1263 * inside spi_sync(); the queue extraction handling at the top of the
1264 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001265 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001266static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001267{
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001268 struct spi_message *msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001269 bool was_busy = false;
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001270 unsigned long flags;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001271 int ret;
1272
Mark Brown983aee52014-12-09 19:46:56 +00001273 /* Lock queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001274 spin_lock_irqsave(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001275
1276 /* Make sure we are not already running a message */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001277 if (ctlr->cur_msg) {
1278 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001279 return;
1280 }
1281
Mark Brownf0125f12019-01-23 17:29:53 +00001282 /* If another context is idling the device then defer */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001283 if (ctlr->idling) {
1284 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1285 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001286 return;
1287 }
1288
Mark Brown983aee52014-12-09 19:46:56 +00001289 /* Check if the queue is idle */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001290 if (list_empty(&ctlr->queue) || !ctlr->running) {
1291 if (!ctlr->busy) {
1292 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -07001293 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001294 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001295
Mark Brownf0125f12019-01-23 17:29:53 +00001296 /* Only do teardown in the thread */
1297 if (!in_kthread) {
1298 kthread_queue_work(&ctlr->kworker,
1299 &ctlr->pump_messages);
1300 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1301 return;
1302 }
1303
1304 ctlr->busy = false;
1305 ctlr->idling = true;
1306 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1307
1308 kfree(ctlr->dummy_rx);
1309 ctlr->dummy_rx = NULL;
1310 kfree(ctlr->dummy_tx);
1311 ctlr->dummy_tx = NULL;
1312 if (ctlr->unprepare_transfer_hardware &&
1313 ctlr->unprepare_transfer_hardware(ctlr))
1314 dev_err(&ctlr->dev,
1315 "failed to unprepare transfer hardware\n");
1316 if (ctlr->auto_runtime_pm) {
1317 pm_runtime_mark_last_busy(ctlr->dev.parent);
1318 pm_runtime_put_autosuspend(ctlr->dev.parent);
1319 }
1320 trace_spi_controller_idle(ctlr);
1321
1322 spin_lock_irqsave(&ctlr->queue_lock, flags);
1323 ctlr->idling = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001324 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001325 return;
1326 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001327
Linus Walleijffbbdd212012-02-22 10:05:38 +01001328 /* Extract head of queue */
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001329 msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1330 ctlr->cur_msg = msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001331
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001332 list_del_init(&msg->queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001333 if (ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001334 was_busy = true;
1335 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001336 ctlr->busy = true;
1337 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001338
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001339 mutex_lock(&ctlr->io_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001340
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001341 if (!was_busy && ctlr->auto_runtime_pm) {
1342 ret = pm_runtime_get_sync(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001343 if (ret < 0) {
Tony Lindgren7e48e232018-05-18 10:30:07 -07001344 pm_runtime_put_noidle(ctlr->dev.parent);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001345 dev_err(&ctlr->dev, "Failed to power device: %d\n",
Mark Brown49834de2013-07-28 14:47:02 +01001346 ret);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001347 mutex_unlock(&ctlr->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001348 return;
1349 }
1350 }
1351
Mark Brown56ec1972013-10-07 19:33:53 +01001352 if (!was_busy)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001353 trace_spi_controller_busy(ctlr);
Mark Brown56ec1972013-10-07 19:33:53 +01001354
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001355 if (!was_busy && ctlr->prepare_transfer_hardware) {
1356 ret = ctlr->prepare_transfer_hardware(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001357 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001358 dev_err(&ctlr->dev,
Super Liuf3440d92019-05-22 14:30:14 +08001359 "failed to prepare transfer hardware: %d\n",
1360 ret);
Mark Brown49834de2013-07-28 14:47:02 +01001361
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001362 if (ctlr->auto_runtime_pm)
1363 pm_runtime_put(ctlr->dev.parent);
Super Liuf3440d92019-05-22 14:30:14 +08001364
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001365 msg->status = ret;
Super Liuf3440d92019-05-22 14:30:14 +08001366 spi_finalize_current_message(ctlr);
1367
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001368 mutex_unlock(&ctlr->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001369 return;
1370 }
1371 }
1372
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001373 trace_spi_message_start(msg);
Mark Brown56ec1972013-10-07 19:33:53 +01001374
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001375 if (ctlr->prepare_message) {
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001376 ret = ctlr->prepare_message(ctlr, msg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001377 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001378 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1379 ret);
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001380 msg->status = ret;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001381 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001382 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001383 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001384 ctlr->cur_msg_prepared = true;
Mark Brown2841a5f2013-10-05 00:23:12 +01001385 }
1386
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001387 ret = spi_map_msg(ctlr, msg);
Mark Brown99adef32014-01-16 12:22:43 +00001388 if (ret) {
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001389 msg->status = ret;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001390 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001391 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001392 }
1393
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001394 ret = ctlr->transfer_one_message(ctlr, msg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001395 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001396 dev_err(&ctlr->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001397 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001398 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001399 }
Jon Hunter49023d22016-03-08 12:28:20 +00001400
1401out:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001402 mutex_unlock(&ctlr->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001403
1404 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001405 if (!ret)
1406 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001407}
1408
Mark Brownfc9e0f72014-12-10 13:46:33 +00001409/**
1410 * spi_pump_messages - kthread work function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001411 * @work: pointer to kthread work struct contained in the controller struct
Mark Brownfc9e0f72014-12-10 13:46:33 +00001412 */
1413static void spi_pump_messages(struct kthread_work *work)
1414{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001415 struct spi_controller *ctlr =
1416 container_of(work, struct spi_controller, pump_messages);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001417
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001418 __spi_pump_messages(ctlr, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001419}
1420
Douglas Anderson924b5862019-05-15 09:48:12 -07001421/**
1422 * spi_set_thread_rt - set the controller to pump at realtime priority
1423 * @ctlr: controller to boost priority of
1424 *
1425 * This can be called because the controller requested realtime priority
1426 * (by setting the ->rt value before calling spi_register_controller()) or
1427 * because a device on the bus said that its transfers needed realtime
1428 * priority.
1429 *
1430 * NOTE: at the moment if any device on a bus says it needs realtime then
1431 * the thread will be at realtime priority for all transfers on that
1432 * controller. If this eventually becomes a problem we may see if we can
1433 * find a way to boost the priority only temporarily during relevant
1434 * transfers.
1435 */
1436static void spi_set_thread_rt(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001437{
Peter Zijlstra4ff13d02019-08-01 13:13:53 +02001438 struct sched_param param = { .sched_priority = MAX_RT_PRIO / 2 };
Linus Walleijffbbdd212012-02-22 10:05:38 +01001439
Douglas Anderson924b5862019-05-15 09:48:12 -07001440 dev_info(&ctlr->dev,
1441 "will run message pump with realtime priority\n");
1442 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
1443}
1444
1445static int spi_init_queue(struct spi_controller *ctlr)
1446{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001447 ctlr->running = false;
1448 ctlr->busy = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001449
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001450 kthread_init_worker(&ctlr->kworker);
1451 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1452 "%s", dev_name(&ctlr->dev));
1453 if (IS_ERR(ctlr->kworker_task)) {
1454 dev_err(&ctlr->dev, "failed to create message pump task\n");
1455 return PTR_ERR(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001456 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001457 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
Mark Brownf0125f12019-01-23 17:29:53 +00001458
Linus Walleijffbbdd212012-02-22 10:05:38 +01001459 /*
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001460 * Controller config will indicate if this controller should run the
Linus Walleijffbbdd212012-02-22 10:05:38 +01001461 * message pump with high (realtime) priority to reduce the transfer
1462 * latency on the bus by minimising the delay between a transfer
1463 * request and the scheduling of the message pump thread. Without this
1464 * setting the message pump thread will remain at default priority.
1465 */
Douglas Anderson924b5862019-05-15 09:48:12 -07001466 if (ctlr->rt)
1467 spi_set_thread_rt(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001468
1469 return 0;
1470}
1471
1472/**
1473 * spi_get_next_queued_message() - called by driver to check for queued
1474 * messages
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001475 * @ctlr: the controller to check for queued messages
Linus Walleijffbbdd212012-02-22 10:05:38 +01001476 *
1477 * If there are more messages in the queue, the next message is returned from
1478 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001479 *
1480 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001481 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001482struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001483{
1484 struct spi_message *next;
1485 unsigned long flags;
1486
1487 /* get a pointer to the next message, if any */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001488 spin_lock_irqsave(&ctlr->queue_lock, flags);
1489 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
Axel Lin1cfd97f2014-01-02 15:16:40 +08001490 queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001491 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001492
1493 return next;
1494}
1495EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1496
1497/**
1498 * spi_finalize_current_message() - the current message is complete
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001499 * @ctlr: the controller to return the message to
Linus Walleijffbbdd212012-02-22 10:05:38 +01001500 *
1501 * Called by the driver to notify the core that the message in the front of the
1502 * queue is complete and can be removed from the queue.
1503 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001504void spi_finalize_current_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001505{
1506 struct spi_message *mesg;
1507 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001508 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001509
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001510 spin_lock_irqsave(&ctlr->queue_lock, flags);
1511 mesg = ctlr->cur_msg;
1512 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001513
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001514 spi_unmap_msg(ctlr, mesg);
Mark Brown99adef32014-01-16 12:22:43 +00001515
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001516 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1517 ret = ctlr->unprepare_message(ctlr, mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001518 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001519 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1520 ret);
Mark Brown2841a5f2013-10-05 00:23:12 +01001521 }
1522 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001523
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001524 spin_lock_irqsave(&ctlr->queue_lock, flags);
1525 ctlr->cur_msg = NULL;
1526 ctlr->cur_msg_prepared = false;
Mark Brownf0125f12019-01-23 17:29:53 +00001527 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001528 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001529
1530 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001531
Linus Walleijffbbdd212012-02-22 10:05:38 +01001532 mesg->state = NULL;
1533 if (mesg->complete)
1534 mesg->complete(mesg->context);
1535}
1536EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1537
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001538static int spi_start_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001539{
1540 unsigned long flags;
1541
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001542 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001543
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001544 if (ctlr->running || ctlr->busy) {
1545 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001546 return -EBUSY;
1547 }
1548
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001549 ctlr->running = true;
1550 ctlr->cur_msg = NULL;
1551 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001552
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001553 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001554
1555 return 0;
1556}
1557
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001558static int spi_stop_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001559{
1560 unsigned long flags;
1561 unsigned limit = 500;
1562 int ret = 0;
1563
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001564 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001565
1566 /*
1567 * This is a bit lame, but is optimized for the common execution path.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001568 * A wait_queue on the ctlr->busy could be used, but then the common
Linus Walleijffbbdd212012-02-22 10:05:38 +01001569 * execution path (pump_messages) would be required to call wake_up or
1570 * friends on every SPI message. Do this instead.
1571 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001572 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1573 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001574 usleep_range(10000, 11000);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001575 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001576 }
1577
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001578 if (!list_empty(&ctlr->queue) || ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001579 ret = -EBUSY;
1580 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001581 ctlr->running = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001582
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001583 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001584
1585 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001586 dev_warn(&ctlr->dev, "could not stop message queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001587 return ret;
1588 }
1589 return ret;
1590}
1591
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001592static int spi_destroy_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001593{
1594 int ret;
1595
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001596 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001597
1598 /*
Petr Mladek39891442016-10-11 13:55:20 -07001599 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001600 * If the reason that stop_queue timed out is that the work will never
1601 * finish, then it does no good to call flush/stop thread, so
1602 * return anyway.
1603 */
1604 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001605 dev_err(&ctlr->dev, "problem destroying queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001606 return ret;
1607 }
1608
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001609 kthread_flush_worker(&ctlr->kworker);
1610 kthread_stop(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001611
1612 return 0;
1613}
1614
Mark Brown0461a412014-12-09 21:38:05 +00001615static int __spi_queued_transfer(struct spi_device *spi,
1616 struct spi_message *msg,
1617 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001618{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001619 struct spi_controller *ctlr = spi->controller;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001620 unsigned long flags;
1621
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001622 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001623
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001624 if (!ctlr->running) {
1625 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001626 return -ESHUTDOWN;
1627 }
1628 msg->actual_length = 0;
1629 msg->status = -EINPROGRESS;
1630
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001631 list_add_tail(&msg->queue, &ctlr->queue);
Mark Brownf0125f12019-01-23 17:29:53 +00001632 if (!ctlr->busy && need_pump)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001633 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001634
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001635 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001636 return 0;
1637}
1638
Mark Brown0461a412014-12-09 21:38:05 +00001639/**
1640 * spi_queued_transfer - transfer function for queued transfers
1641 * @spi: spi device which is requesting transfer
1642 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001643 *
1644 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001645 */
1646static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1647{
1648 return __spi_queued_transfer(spi, msg, true);
1649}
1650
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001651static int spi_controller_initialize_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001652{
1653 int ret;
1654
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001655 ctlr->transfer = spi_queued_transfer;
1656 if (!ctlr->transfer_one_message)
1657 ctlr->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001658
1659 /* Initialize and start queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001660 ret = spi_init_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001661 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001662 dev_err(&ctlr->dev, "problem initializing queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001663 goto err_init_queue;
1664 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001665 ctlr->queued = true;
1666 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001667 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001668 dev_err(&ctlr->dev, "problem starting queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001669 goto err_start_queue;
1670 }
1671
1672 return 0;
1673
1674err_start_queue:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001675 spi_destroy_queue(ctlr);
Mark Brownc3676d52014-05-01 10:47:52 -07001676err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001677 return ret;
1678}
1679
Boris Brezillon988f2592018-04-22 20:35:15 +02001680/**
1681 * spi_flush_queue - Send all pending messages in the queue from the callers'
1682 * context
1683 * @ctlr: controller to process queue for
1684 *
1685 * This should be used when one wants to ensure all pending messages have been
1686 * sent before doing something. Is used by the spi-mem code to make sure SPI
1687 * memory operations do not preempt regular SPI transfers that have been queued
1688 * before the spi-mem operation.
1689 */
1690void spi_flush_queue(struct spi_controller *ctlr)
1691{
1692 if (ctlr->transfer == spi_queued_transfer)
1693 __spi_pump_messages(ctlr, false);
1694}
1695
Linus Walleijffbbdd212012-02-22 10:05:38 +01001696/*-------------------------------------------------------------------------*/
1697
Andreas Larsson7cb94362012-12-04 15:09:38 +01001698#if defined(CONFIG_OF)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001699static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001700 struct device_node *nc)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001701{
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001702 u32 value;
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001703 int rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001704
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001705 /* Mode (clock phase/polarity/etc.) */
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001706 if (of_property_read_bool(nc, "spi-cpha"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001707 spi->mode |= SPI_CPHA;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001708 if (of_property_read_bool(nc, "spi-cpol"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001709 spi->mode |= SPI_CPOL;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001710 if (of_property_read_bool(nc, "spi-3wire"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001711 spi->mode |= SPI_3WIRE;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001712 if (of_property_read_bool(nc, "spi-lsb-first"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001713 spi->mode |= SPI_LSB_FIRST;
Gregory CLEMENT3e5ec1d2019-10-18 17:29:29 +02001714 if (of_property_read_bool(nc, "spi-cs-high"))
Linus Walleijf3186dd2019-01-07 16:51:50 +01001715 spi->mode |= SPI_CS_HIGH;
1716
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001717 /* Device DUAL/QUAD mode */
1718 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1719 switch (value) {
1720 case 1:
1721 break;
1722 case 2:
1723 spi->mode |= SPI_TX_DUAL;
1724 break;
1725 case 4:
1726 spi->mode |= SPI_TX_QUAD;
1727 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001728 case 8:
1729 spi->mode |= SPI_TX_OCTAL;
1730 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001731 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001732 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001733 "spi-tx-bus-width %d not supported\n",
1734 value);
1735 break;
1736 }
1737 }
1738
1739 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1740 switch (value) {
1741 case 1:
1742 break;
1743 case 2:
1744 spi->mode |= SPI_RX_DUAL;
1745 break;
1746 case 4:
1747 spi->mode |= SPI_RX_QUAD;
1748 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001749 case 8:
1750 spi->mode |= SPI_RX_OCTAL;
1751 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001752 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001753 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001754 "spi-rx-bus-width %d not supported\n",
1755 value);
1756 break;
1757 }
1758 }
1759
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001760 if (spi_controller_is_slave(ctlr)) {
Rob Herring194276b2018-12-05 13:50:41 -06001761 if (!of_node_name_eq(nc, "slave")) {
Rob Herring25c56c82017-07-18 16:43:31 -05001762 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
1763 nc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001764 return -EINVAL;
1765 }
1766 return 0;
1767 }
1768
1769 /* Device address */
1770 rc = of_property_read_u32(nc, "reg", &value);
1771 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05001772 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
1773 nc, rc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001774 return rc;
1775 }
1776 spi->chip_select = value;
1777
Gregory CLEMENT3e5ec1d2019-10-18 17:29:29 +02001778 /*
1779 * For descriptors associated with the device, polarity inversion is
1780 * handled in the gpiolib, so all gpio chip selects are "active high"
1781 * in the logical sense, the gpiolib will invert the line if need be.
1782 */
1783 if ((ctlr->use_gpio_descriptors) && ctlr->cs_gpiods[spi->chip_select])
1784 spi->mode |= SPI_CS_HIGH;
1785
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001786 /* Device speed */
1787 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1788 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001789 dev_err(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05001790 "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001791 return rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001792 }
1793 spi->max_speed_hz = value;
1794
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001795 return 0;
1796}
1797
1798static struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001799of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001800{
1801 struct spi_device *spi;
1802 int rc;
1803
1804 /* Alloc an spi_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001805 spi = spi_alloc_device(ctlr);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001806 if (!spi) {
Rob Herring25c56c82017-07-18 16:43:31 -05001807 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001808 rc = -ENOMEM;
1809 goto err_out;
1810 }
1811
1812 /* Select device driver */
1813 rc = of_modalias_node(nc, spi->modalias,
1814 sizeof(spi->modalias));
1815 if (rc < 0) {
Rob Herring25c56c82017-07-18 16:43:31 -05001816 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001817 goto err_out;
1818 }
1819
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001820 rc = of_spi_parse_dt(ctlr, spi, nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001821 if (rc)
1822 goto err_out;
1823
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001824 /* Store a pointer to the node in the device structure */
1825 of_node_get(nc);
1826 spi->dev.of_node = nc;
1827
1828 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001829 rc = spi_add_device(spi);
1830 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05001831 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
Johan Hovold83241472017-01-30 17:47:05 +01001832 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001833 }
1834
1835 return spi;
1836
Johan Hovold83241472017-01-30 17:47:05 +01001837err_of_node_put:
1838 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001839err_out:
1840 spi_dev_put(spi);
1841 return ERR_PTR(rc);
1842}
1843
Grant Likelyd57a4282012-04-07 14:16:53 -06001844/**
1845 * of_register_spi_devices() - Register child devices onto the SPI bus
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001846 * @ctlr: Pointer to spi_controller device
Grant Likelyd57a4282012-04-07 14:16:53 -06001847 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001848 * Registers an spi_device for each child node of controller node which
1849 * represents a valid SPI slave.
Grant Likelyd57a4282012-04-07 14:16:53 -06001850 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001851static void of_register_spi_devices(struct spi_controller *ctlr)
Grant Likelyd57a4282012-04-07 14:16:53 -06001852{
1853 struct spi_device *spi;
1854 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001855
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001856 if (!ctlr->dev.of_node)
Grant Likelyd57a4282012-04-07 14:16:53 -06001857 return;
1858
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001859 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01001860 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1861 continue;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001862 spi = of_register_spi_device(ctlr, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001863 if (IS_ERR(spi)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001864 dev_warn(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05001865 "Failed to create SPI device for %pOF\n", nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001866 of_node_clear_flag(nc, OF_POPULATED);
1867 }
Grant Likelyd57a4282012-04-07 14:16:53 -06001868 }
1869}
1870#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001871static void of_register_spi_devices(struct spi_controller *ctlr) { }
Grant Likelyd57a4282012-04-07 14:16:53 -06001872#endif
1873
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001874#ifdef CONFIG_ACPI
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001875struct acpi_spi_lookup {
1876 struct spi_controller *ctlr;
1877 u32 max_speed_hz;
1878 u32 mode;
1879 int irq;
1880 u8 bits_per_word;
1881 u8 chip_select;
1882};
1883
1884static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
1885 struct acpi_spi_lookup *lookup)
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001886{
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001887 const union acpi_object *obj;
1888
1889 if (!x86_apple_machine)
1890 return;
1891
1892 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
1893 && obj->buffer.length >= 4)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001894 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001895
1896 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
1897 && obj->buffer.length == 8)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001898 lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001899
1900 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
1901 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001902 lookup->mode |= SPI_LSB_FIRST;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001903
1904 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
1905 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001906 lookup->mode |= SPI_CPOL;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001907
1908 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
1909 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001910 lookup->mode |= SPI_CPHA;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001911}
1912
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001913static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1914{
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001915 struct acpi_spi_lookup *lookup = data;
1916 struct spi_controller *ctlr = lookup->ctlr;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001917
1918 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1919 struct acpi_resource_spi_serialbus *sb;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001920 acpi_handle parent_handle;
1921 acpi_status status;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001922
1923 sb = &ares->data.spi_serial_bus;
1924 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001925
1926 status = acpi_get_handle(NULL,
1927 sb->resource_source.string_ptr,
1928 &parent_handle);
1929
Ard Biesheuvelb5e3cf42019-06-19 11:52:54 +02001930 if (ACPI_FAILURE(status) ||
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001931 ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
1932 return -ENODEV;
1933
Mika Westerberga0a90712016-02-08 17:14:28 +02001934 /*
1935 * ACPI DeviceSelection numbering is handled by the
1936 * host controller driver in Windows and can vary
1937 * from driver to driver. In Linux we always expect
1938 * 0 .. max - 1 so we need to ask the driver to
1939 * translate between the two schemes.
1940 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001941 if (ctlr->fw_translate_cs) {
1942 int cs = ctlr->fw_translate_cs(ctlr,
Mika Westerberga0a90712016-02-08 17:14:28 +02001943 sb->device_selection);
1944 if (cs < 0)
1945 return cs;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001946 lookup->chip_select = cs;
Mika Westerberga0a90712016-02-08 17:14:28 +02001947 } else {
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001948 lookup->chip_select = sb->device_selection;
Mika Westerberga0a90712016-02-08 17:14:28 +02001949 }
1950
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001951 lookup->max_speed_hz = sb->connection_speed;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001952
1953 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001954 lookup->mode |= SPI_CPHA;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001955 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001956 lookup->mode |= SPI_CPOL;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001957 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001958 lookup->mode |= SPI_CS_HIGH;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001959 }
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001960 } else if (lookup->irq < 0) {
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001961 struct resource r;
1962
1963 if (acpi_dev_resource_interrupt(ares, 0, &r))
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001964 lookup->irq = r.start;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001965 }
1966
1967 /* Always tell the ACPI core to skip this resource */
1968 return 1;
1969}
1970
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001971static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
Octavian Purdila7f244672016-07-08 19:13:11 +03001972 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001973{
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001974 acpi_handle parent_handle = NULL;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001975 struct list_head resource_list;
Ard Biesheuvelb28944c2019-06-20 14:36:49 +02001976 struct acpi_spi_lookup lookup = {};
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001977 struct spi_device *spi;
1978 int ret;
1979
Octavian Purdila7f244672016-07-08 19:13:11 +03001980 if (acpi_bus_get_status(adev) || !adev->status.present ||
1981 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001982 return AE_OK;
1983
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001984 lookup.ctlr = ctlr;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02001985 lookup.irq = -1;
1986
1987 INIT_LIST_HEAD(&resource_list);
1988 ret = acpi_dev_get_resources(adev, &resource_list,
1989 acpi_spi_add_resource, &lookup);
1990 acpi_dev_free_resource_list(&resource_list);
1991
1992 if (ret < 0)
1993 /* found SPI in _CRS but it points to another controller */
1994 return AE_OK;
1995
1996 if (!lookup.max_speed_hz &&
1997 !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) &&
1998 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) {
1999 /* Apple does not use _CRS but nested devices for SPI slaves */
2000 acpi_spi_parse_apple_properties(adev, &lookup);
2001 }
2002
2003 if (!lookup.max_speed_hz)
2004 return AE_OK;
2005
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002006 spi = spi_alloc_device(ctlr);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002007 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002008 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002009 dev_name(&adev->dev));
2010 return AE_NO_MEMORY;
2011 }
2012
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01002013 ACPI_COMPANION_SET(&spi->dev, adev);
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002014 spi->max_speed_hz = lookup.max_speed_hz;
2015 spi->mode = lookup.mode;
2016 spi->irq = lookup.irq;
2017 spi->bits_per_word = lookup.bits_per_word;
2018 spi->chip_select = lookup.chip_select;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002019
Dan O'Donovan0c6543f2017-02-05 16:30:14 +00002020 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
2021 sizeof(spi->modalias));
2022
Christophe RICARD33ada672015-12-23 23:25:35 +01002023 if (spi->irq < 0)
2024 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
2025
Octavian Purdila7f244672016-07-08 19:13:11 +03002026 acpi_device_set_enumerated(adev);
2027
Mika Westerberg33cf00e2013-10-10 13:28:48 +03002028 adev->power.flags.ignore_parent = true;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002029 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03002030 adev->power.flags.ignore_parent = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002031 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002032 dev_name(&adev->dev));
2033 spi_dev_put(spi);
2034 }
2035
2036 return AE_OK;
2037}
2038
Octavian Purdila7f244672016-07-08 19:13:11 +03002039static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
2040 void *data, void **return_value)
2041{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002042 struct spi_controller *ctlr = data;
Octavian Purdila7f244672016-07-08 19:13:11 +03002043 struct acpi_device *adev;
2044
2045 if (acpi_bus_get_device(handle, &adev))
2046 return AE_OK;
2047
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002048 return acpi_register_spi_device(ctlr, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03002049}
2050
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002051#define SPI_ACPI_ENUMERATE_MAX_DEPTH 32
2052
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002053static void acpi_register_spi_devices(struct spi_controller *ctlr)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002054{
2055 acpi_status status;
2056 acpi_handle handle;
2057
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002058 handle = ACPI_HANDLE(ctlr->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002059 if (!handle)
2060 return;
2061
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002062 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
2063 SPI_ACPI_ENUMERATE_MAX_DEPTH,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002064 acpi_spi_add_device, NULL, ctlr, NULL);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002065 if (ACPI_FAILURE(status))
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002066 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002067}
2068#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002069static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002070#endif /* CONFIG_ACPI */
2071
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002072static void spi_controller_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08002073{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002074 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002075
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002076 ctlr = container_of(dev, struct spi_controller, dev);
2077 kfree(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002078}
2079
2080static struct class spi_master_class = {
2081 .name = "spi_master",
2082 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002083 .dev_release = spi_controller_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00002084 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08002085};
2086
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002087#ifdef CONFIG_SPI_SLAVE
2088/**
2089 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2090 * controller
2091 * @spi: device used for the current transfer
2092 */
2093int spi_slave_abort(struct spi_device *spi)
2094{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002095 struct spi_controller *ctlr = spi->controller;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002096
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002097 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
2098 return ctlr->slave_abort(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002099
2100 return -ENOTSUPP;
2101}
2102EXPORT_SYMBOL_GPL(spi_slave_abort);
2103
2104static int match_true(struct device *dev, void *data)
2105{
2106 return 1;
2107}
2108
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002109static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
2110 char *buf)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002111{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002112 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2113 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002114 struct device *child;
2115
2116 child = device_find_child(&ctlr->dev, NULL, match_true);
2117 return sprintf(buf, "%s\n",
2118 child ? to_spi_device(child)->modalias : NULL);
2119}
2120
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002121static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
2122 const char *buf, size_t count)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002123{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002124 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2125 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002126 struct spi_device *spi;
2127 struct device *child;
2128 char name[32];
2129 int rc;
2130
2131 rc = sscanf(buf, "%31s", name);
2132 if (rc != 1 || !name[0])
2133 return -EINVAL;
2134
2135 child = device_find_child(&ctlr->dev, NULL, match_true);
2136 if (child) {
2137 /* Remove registered slave */
2138 device_unregister(child);
2139 put_device(child);
2140 }
2141
2142 if (strcmp(name, "(null)")) {
2143 /* Register new slave */
2144 spi = spi_alloc_device(ctlr);
2145 if (!spi)
2146 return -ENOMEM;
2147
2148 strlcpy(spi->modalias, name, sizeof(spi->modalias));
2149
2150 rc = spi_add_device(spi);
2151 if (rc) {
2152 spi_dev_put(spi);
2153 return rc;
2154 }
2155 }
2156
2157 return count;
2158}
2159
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002160static DEVICE_ATTR_RW(slave);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002161
2162static struct attribute *spi_slave_attrs[] = {
2163 &dev_attr_slave.attr,
2164 NULL,
2165};
2166
2167static const struct attribute_group spi_slave_group = {
2168 .attrs = spi_slave_attrs,
2169};
2170
2171static const struct attribute_group *spi_slave_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002172 &spi_controller_statistics_group,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002173 &spi_slave_group,
2174 NULL,
2175};
2176
2177static struct class spi_slave_class = {
2178 .name = "spi_slave",
2179 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002180 .dev_release = spi_controller_release,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002181 .dev_groups = spi_slave_groups,
2182};
2183#else
2184extern struct class spi_slave_class; /* dummy */
2185#endif
David Brownell8ae12a02006-01-08 13:34:19 -08002186
2187/**
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002188 * __spi_alloc_controller - allocate an SPI master or slave controller
David Brownell8ae12a02006-01-08 13:34:19 -08002189 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07002190 * @size: how much zeroed driver-private data to allocate; the pointer to this
Lukas Wunner229e6af2019-09-11 12:15:30 +02002191 * memory is in the driver_data field of the returned device, accessible
2192 * with spi_controller_get_devdata(); the memory is cacheline aligned;
2193 * drivers granting DMA access to portions of their private data need to
2194 * round up @size using ALIGN(size, dma_get_cache_alignment()).
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002195 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2196 * slave (true) controller
David Brownell33e34dc2007-05-08 00:32:21 -07002197 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002198 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002199 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002200 * only ones directly touching chip registers. It's how they allocate
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002201 * an spi_controller structure, prior to calling spi_register_controller().
David Brownell8ae12a02006-01-08 13:34:19 -08002202 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002203 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08002204 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002205 * The caller is responsible for assigning the bus number and initializing the
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002206 * controller's methods before calling spi_register_controller(); and (after
2207 * errors adding the device) calling spi_controller_put() to prevent a memory
2208 * leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002209 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002210 * Return: the SPI controller structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002211 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002212struct spi_controller *__spi_alloc_controller(struct device *dev,
2213 unsigned int size, bool slave)
David Brownell8ae12a02006-01-08 13:34:19 -08002214{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002215 struct spi_controller *ctlr;
Lukas Wunner229e6af2019-09-11 12:15:30 +02002216 size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
David Brownell8ae12a02006-01-08 13:34:19 -08002217
David Brownell0c8684612006-01-08 13:34:25 -08002218 if (!dev)
2219 return NULL;
2220
Lukas Wunner229e6af2019-09-11 12:15:30 +02002221 ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002222 if (!ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002223 return NULL;
2224
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002225 device_initialize(&ctlr->dev);
2226 ctlr->bus_num = -1;
2227 ctlr->num_chipselect = 1;
2228 ctlr->slave = slave;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002229 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002230 ctlr->dev.class = &spi_slave_class;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002231 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002232 ctlr->dev.class = &spi_master_class;
2233 ctlr->dev.parent = dev;
2234 pm_suspend_ignore_children(&ctlr->dev, true);
Lukas Wunner229e6af2019-09-11 12:15:30 +02002235 spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
David Brownell8ae12a02006-01-08 13:34:19 -08002236
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002237 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002238}
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002239EXPORT_SYMBOL_GPL(__spi_alloc_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002240
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002241#ifdef CONFIG_OF
Linus Walleij43004f32019-08-08 17:03:21 +02002242static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002243{
Grant Likelye80beb22013-02-12 17:48:37 +00002244 int nb, i, *cs;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002245 struct device_node *np = ctlr->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002246
2247 if (!np)
2248 return 0;
2249
2250 nb = of_gpio_named_count(np, "cs-gpios");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002251 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002252
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002253 /* Return error only for an incorrectly formed cs-gpios property */
2254 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002255 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002256 else if (nb < 0)
2257 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002258
Kees Cooka86854d2018-06-12 14:07:58 -07002259 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002260 GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002261 ctlr->cs_gpios = cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002262
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002263 if (!ctlr->cs_gpios)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002264 return -ENOMEM;
2265
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002266 for (i = 0; i < ctlr->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01002267 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002268
2269 for (i = 0; i < nb; i++)
2270 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2271
2272 return 0;
2273}
2274#else
Linus Walleij43004f32019-08-08 17:03:21 +02002275static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002276{
2277 return 0;
2278}
2279#endif
2280
Linus Walleijf3186dd2019-01-07 16:51:50 +01002281/**
2282 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2283 * @ctlr: The SPI master to grab GPIO descriptors for
2284 */
2285static int spi_get_gpio_descs(struct spi_controller *ctlr)
2286{
2287 int nb, i;
2288 struct gpio_desc **cs;
2289 struct device *dev = &ctlr->dev;
2290
2291 nb = gpiod_count(dev, "cs");
2292 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2293
2294 /* No GPIOs at all is fine, else return the error */
2295 if (nb == 0 || nb == -ENOENT)
2296 return 0;
2297 else if (nb < 0)
2298 return nb;
2299
2300 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
2301 GFP_KERNEL);
2302 if (!cs)
2303 return -ENOMEM;
2304 ctlr->cs_gpiods = cs;
2305
2306 for (i = 0; i < nb; i++) {
2307 /*
2308 * Most chipselects are active low, the inverted
2309 * semantics are handled by special quirks in gpiolib,
2310 * so initializing them GPIOD_OUT_LOW here means
2311 * "unasserted", in most cases this will drive the physical
2312 * line high.
2313 */
2314 cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
2315 GPIOD_OUT_LOW);
Geert Uytterhoeven1723fde2019-04-03 16:46:56 +02002316 if (IS_ERR(cs[i]))
2317 return PTR_ERR(cs[i]);
Linus Walleijf3186dd2019-01-07 16:51:50 +01002318
2319 if (cs[i]) {
2320 /*
2321 * If we find a CS GPIO, name it after the device and
2322 * chip select line.
2323 */
2324 char *gpioname;
2325
2326 gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
2327 dev_name(dev), i);
2328 if (!gpioname)
2329 return -ENOMEM;
2330 gpiod_set_consumer_name(cs[i], gpioname);
2331 }
2332 }
2333
2334 return 0;
2335}
2336
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002337static int spi_controller_check_ops(struct spi_controller *ctlr)
2338{
2339 /*
Boris Brezillonb5932f52018-04-26 18:18:15 +02002340 * The controller may implement only the high-level SPI-memory like
2341 * operations if it does not support regular SPI transfers, and this is
2342 * valid use case.
2343 * If ->mem_ops is NULL, we request that at least one of the
2344 * ->transfer_xxx() method be implemented.
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002345 */
Boris Brezillonb5932f52018-04-26 18:18:15 +02002346 if (ctlr->mem_ops) {
2347 if (!ctlr->mem_ops->exec_op)
2348 return -EINVAL;
2349 } else if (!ctlr->transfer && !ctlr->transfer_one &&
2350 !ctlr->transfer_one_message) {
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002351 return -EINVAL;
Boris Brezillonb5932f52018-04-26 18:18:15 +02002352 }
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002353
2354 return 0;
2355}
2356
David Brownell8ae12a02006-01-08 13:34:19 -08002357/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002358 * spi_register_controller - register SPI master or slave controller
2359 * @ctlr: initialized master, originally from spi_alloc_master() or
2360 * spi_alloc_slave()
David Brownell33e34dc2007-05-08 00:32:21 -07002361 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002362 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002363 * SPI controllers connect to their drivers using some non-SPI bus,
David Brownell8ae12a02006-01-08 13:34:19 -08002364 * such as the platform bus. The final stage of probe() in that code
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002365 * includes calling spi_register_controller() to hook up to this SPI bus glue.
David Brownell8ae12a02006-01-08 13:34:19 -08002366 *
2367 * SPI controllers use board specific (often SOC specific) bus numbers,
2368 * and board-specific addressing for SPI devices combines those numbers
2369 * with chip select numbers. Since SPI does not directly support dynamic
2370 * device identification, boards need configuration tables telling which
2371 * chip is at which address.
2372 *
2373 * This must be called from context that can sleep. It returns zero on
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002374 * success, else a negative error code (dropping the controller's refcount).
David Brownell0c8684612006-01-08 13:34:25 -08002375 * After a successful return, the caller is responsible for calling
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002376 * spi_unregister_controller().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002377 *
2378 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002379 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002380int spi_register_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002381{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002382 struct device *dev = ctlr->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08002383 struct boardinfo *bi;
Sergei Shtylyovb93318a2019-04-05 18:48:50 +03002384 int status;
Lucas Stach42bdd702017-10-16 12:27:58 +02002385 int id, first_dynamic;
David Brownell8ae12a02006-01-08 13:34:19 -08002386
David Brownell0c8684612006-01-08 13:34:25 -08002387 if (!dev)
2388 return -ENODEV;
2389
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002390 /*
2391 * Make sure all necessary hooks are implemented before registering
2392 * the SPI controller.
2393 */
2394 status = spi_controller_check_ops(ctlr);
2395 if (status)
2396 return status;
2397
Geert Uytterhoeven04b2d032018-08-21 11:53:03 +02002398 if (ctlr->bus_num >= 0) {
2399 /* devices with a fixed bus num must check-in with the num */
2400 mutex_lock(&board_lock);
2401 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2402 ctlr->bus_num + 1, GFP_KERNEL);
2403 mutex_unlock(&board_lock);
2404 if (WARN(id < 0, "couldn't get idr"))
2405 return id == -ENOSPC ? -EBUSY : id;
2406 ctlr->bus_num = id;
2407 } else if (ctlr->dev.of_node) {
2408 /* allocate dynamic bus number using Linux idr */
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302409 id = of_alias_get_id(ctlr->dev.of_node, "spi");
2410 if (id >= 0) {
2411 ctlr->bus_num = id;
2412 mutex_lock(&board_lock);
2413 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2414 ctlr->bus_num + 1, GFP_KERNEL);
2415 mutex_unlock(&board_lock);
2416 if (WARN(id < 0, "couldn't get idr"))
2417 return id == -ENOSPC ? -EBUSY : id;
2418 }
2419 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002420 if (ctlr->bus_num < 0) {
Lucas Stach42bdd702017-10-16 12:27:58 +02002421 first_dynamic = of_alias_get_highest_id("spi");
2422 if (first_dynamic < 0)
2423 first_dynamic = 0;
2424 else
2425 first_dynamic++;
2426
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302427 mutex_lock(&board_lock);
Lucas Stach42bdd702017-10-16 12:27:58 +02002428 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
2429 0, GFP_KERNEL);
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302430 mutex_unlock(&board_lock);
2431 if (WARN(id < 0, "couldn't get idr"))
2432 return id;
2433 ctlr->bus_num = id;
David Brownell8ae12a02006-01-08 13:34:19 -08002434 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002435 INIT_LIST_HEAD(&ctlr->queue);
2436 spin_lock_init(&ctlr->queue_lock);
2437 spin_lock_init(&ctlr->bus_lock_spinlock);
2438 mutex_init(&ctlr->bus_lock_mutex);
2439 mutex_init(&ctlr->io_mutex);
2440 ctlr->bus_lock_flag = 0;
2441 init_completion(&ctlr->xfer_completion);
2442 if (!ctlr->max_dma_len)
2443 ctlr->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002444
David Brownell8ae12a02006-01-08 13:34:19 -08002445 /* register the device, then userspace will see it.
2446 * registration fails if the bus ID is in use.
2447 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002448 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
Andrey Smirnov0a919ae2019-04-02 21:01:28 -07002449
2450 if (!spi_controller_is_slave(ctlr)) {
2451 if (ctlr->use_gpio_descriptors) {
2452 status = spi_get_gpio_descs(ctlr);
2453 if (status)
2454 return status;
2455 /*
2456 * A controller using GPIO descriptors always
2457 * supports SPI_CS_HIGH if need be.
2458 */
2459 ctlr->mode_bits |= SPI_CS_HIGH;
2460 } else {
2461 /* Legacy code path for GPIOs from DT */
Linus Walleij43004f32019-08-08 17:03:21 +02002462 status = of_spi_get_gpio_numbers(ctlr);
Andrey Smirnov0a919ae2019-04-02 21:01:28 -07002463 if (status)
2464 return status;
2465 }
2466 }
2467
Tudor Ambarusf9481b02019-06-19 14:38:28 +00002468 /*
2469 * Even if it's just one always-selected device, there must
2470 * be at least one chipselect.
2471 */
2472 if (!ctlr->num_chipselect)
2473 return -EINVAL;
2474
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002475 status = device_add(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302476 if (status < 0) {
2477 /* free bus id */
2478 mutex_lock(&board_lock);
2479 idr_remove(&spi_master_idr, ctlr->bus_num);
2480 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002481 goto done;
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302482 }
2483 dev_dbg(dev, "registered %s %s\n",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002484 spi_controller_is_slave(ctlr) ? "slave" : "master",
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302485 dev_name(&ctlr->dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002486
Boris Brezillonb5932f52018-04-26 18:18:15 +02002487 /*
2488 * If we're using a queued driver, start the queue. Note that we don't
2489 * need the queueing logic if the driver is only supporting high-level
2490 * memory operations.
2491 */
2492 if (ctlr->transfer) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002493 dev_info(dev, "controller is unqueued, this is deprecated\n");
Boris Brezillonb5932f52018-04-26 18:18:15 +02002494 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002495 status = spi_controller_initialize_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002496 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002497 device_del(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302498 /* free bus id */
2499 mutex_lock(&board_lock);
2500 idr_remove(&spi_master_idr, ctlr->bus_num);
2501 mutex_unlock(&board_lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002502 goto done;
2503 }
2504 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00002505 /* add statistics */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002506 spin_lock_init(&ctlr->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002507
Feng Tang2b9603a2010-08-02 15:52:15 +08002508 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002509 list_add_tail(&ctlr->list, &spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002510 list_for_each_entry(bi, &board_list, list)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002511 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +08002512 mutex_unlock(&board_lock);
2513
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002514 /* Register devices from the device tree and ACPI */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002515 of_register_spi_devices(ctlr);
2516 acpi_register_spi_devices(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002517done:
2518 return status;
2519}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002520EXPORT_SYMBOL_GPL(spi_register_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002521
Mark Brown666d5b42013-08-31 18:50:52 +01002522static void devm_spi_unregister(struct device *dev, void *res)
2523{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002524 spi_unregister_controller(*(struct spi_controller **)res);
Mark Brown666d5b42013-08-31 18:50:52 +01002525}
2526
2527/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002528 * devm_spi_register_controller - register managed SPI master or slave
2529 * controller
2530 * @dev: device managing SPI controller
2531 * @ctlr: initialized controller, originally from spi_alloc_master() or
2532 * spi_alloc_slave()
Mark Brown666d5b42013-08-31 18:50:52 +01002533 * Context: can sleep
2534 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002535 * Register a SPI device as with spi_register_controller() which will
Johan Hovold68b892f2017-10-30 11:35:26 +01002536 * automatically be unregistered and freed.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002537 *
2538 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01002539 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002540int devm_spi_register_controller(struct device *dev,
2541 struct spi_controller *ctlr)
Mark Brown666d5b42013-08-31 18:50:52 +01002542{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002543 struct spi_controller **ptr;
Mark Brown666d5b42013-08-31 18:50:52 +01002544 int ret;
2545
2546 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2547 if (!ptr)
2548 return -ENOMEM;
2549
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002550 ret = spi_register_controller(ctlr);
Stephen Warren4b928942013-11-21 16:11:15 -07002551 if (!ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002552 *ptr = ctlr;
Mark Brown666d5b42013-08-31 18:50:52 +01002553 devres_add(dev, ptr);
2554 } else {
2555 devres_free(ptr);
2556 }
2557
2558 return ret;
2559}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002560EXPORT_SYMBOL_GPL(devm_spi_register_controller);
Mark Brown666d5b42013-08-31 18:50:52 +01002561
David Lamparter34860082010-08-30 23:54:17 +02002562static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002563{
David Lamparter34860082010-08-30 23:54:17 +02002564 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002565 return 0;
2566}
2567
2568/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002569 * spi_unregister_controller - unregister SPI master or slave controller
2570 * @ctlr: the controller being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002571 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002572 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002573 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002574 * only ones directly touching chip registers.
2575 *
2576 * This must be called from context that can sleep.
Johan Hovold68b892f2017-10-30 11:35:26 +01002577 *
2578 * Note that this function also drops a reference to the controller.
David Brownell8ae12a02006-01-08 13:34:19 -08002579 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002580void spi_unregister_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002581{
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302582 struct spi_controller *found;
Johan Hovold67f7b272017-10-30 11:35:25 +01002583 int id = ctlr->bus_num;
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002584
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302585 /* First make sure that this controller was ever added */
2586 mutex_lock(&board_lock);
Johan Hovold67f7b272017-10-30 11:35:25 +01002587 found = idr_find(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302588 mutex_unlock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002589 if (ctlr->queued) {
2590 if (spi_destroy_queue(ctlr))
2591 dev_err(&ctlr->dev, "queue remove failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002592 }
Feng Tang2b9603a2010-08-02 15:52:15 +08002593 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002594 list_del(&ctlr->list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002595 mutex_unlock(&board_lock);
2596
Andy Shevchenkoebc37af2019-06-15 20:41:35 +03002597 device_for_each_child(&ctlr->dev, NULL, __unregister);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002598 device_unregister(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302599 /* free bus id */
2600 mutex_lock(&board_lock);
Jarkko Nikula613bd1e2018-03-20 10:27:50 +02002601 if (found == ctlr)
2602 idr_remove(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302603 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002604}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002605EXPORT_SYMBOL_GPL(spi_unregister_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002606
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002607int spi_controller_suspend(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002608{
2609 int ret;
2610
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002611 /* Basically no-ops for non-queued controllers */
2612 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002613 return 0;
2614
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002615 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002616 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002617 dev_err(&ctlr->dev, "queue stop failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002618
2619 return ret;
2620}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002621EXPORT_SYMBOL_GPL(spi_controller_suspend);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002622
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002623int spi_controller_resume(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002624{
2625 int ret;
2626
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002627 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002628 return 0;
2629
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002630 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002631 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002632 dev_err(&ctlr->dev, "queue restart failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002633
2634 return ret;
2635}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002636EXPORT_SYMBOL_GPL(spi_controller_resume);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002637
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002638static int __spi_controller_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002639{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002640 struct spi_controller *ctlr;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002641 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002642
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002643 ctlr = container_of(dev, struct spi_controller, dev);
2644 return ctlr->bus_num == *bus_num;
Dave Young5ed2c832008-01-22 15:14:18 +08002645}
2646
David Brownell8ae12a02006-01-08 13:34:19 -08002647/**
2648 * spi_busnum_to_master - look up master associated with bus_num
2649 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002650 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002651 *
2652 * This call may be used with devices that are registered after
2653 * arch init time. It returns a refcounted pointer to the relevant
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002654 * spi_controller (which the caller must release), or NULL if there is
David Brownell8ae12a02006-01-08 13:34:19 -08002655 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002656 *
2657 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002658 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002659struct spi_controller *spi_busnum_to_master(u16 bus_num)
David Brownell8ae12a02006-01-08 13:34:19 -08002660{
Tony Jones49dce682007-10-16 01:27:48 -07002661 struct device *dev;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002662 struct spi_controller *ctlr = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002663
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002664 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002665 __spi_controller_match);
Dave Young5ed2c832008-01-22 15:14:18 +08002666 if (dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002667 ctlr = container_of(dev, struct spi_controller, dev);
Dave Young5ed2c832008-01-22 15:14:18 +08002668 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002669 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002670}
2671EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2672
Martin Sperld780c372015-12-14 15:20:18 +00002673/*-------------------------------------------------------------------------*/
2674
2675/* Core methods for SPI resource management */
2676
2677/**
2678 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2679 * during the processing of a spi_message while using
2680 * spi_transfer_one
2681 * @spi: the spi device for which we allocate memory
2682 * @release: the release code to execute for this resource
2683 * @size: size to alloc and return
2684 * @gfp: GFP allocation flags
2685 *
2686 * Return: the pointer to the allocated data
2687 *
2688 * This may get enhanced in the future to allocate from a memory pool
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002689 * of the @spi_device or @spi_controller to avoid repeated allocations.
Martin Sperld780c372015-12-14 15:20:18 +00002690 */
2691void *spi_res_alloc(struct spi_device *spi,
2692 spi_res_release_t release,
2693 size_t size, gfp_t gfp)
2694{
2695 struct spi_res *sres;
2696
2697 sres = kzalloc(sizeof(*sres) + size, gfp);
2698 if (!sres)
2699 return NULL;
2700
2701 INIT_LIST_HEAD(&sres->entry);
2702 sres->release = release;
2703
2704 return sres->data;
2705}
2706EXPORT_SYMBOL_GPL(spi_res_alloc);
2707
2708/**
2709 * spi_res_free - free an spi resource
2710 * @res: pointer to the custom data of a resource
2711 *
2712 */
2713void spi_res_free(void *res)
2714{
2715 struct spi_res *sres = container_of(res, struct spi_res, data);
2716
2717 if (!res)
2718 return;
2719
2720 WARN_ON(!list_empty(&sres->entry));
2721 kfree(sres);
2722}
2723EXPORT_SYMBOL_GPL(spi_res_free);
2724
2725/**
2726 * spi_res_add - add a spi_res to the spi_message
2727 * @message: the spi message
2728 * @res: the spi_resource
2729 */
2730void spi_res_add(struct spi_message *message, void *res)
2731{
2732 struct spi_res *sres = container_of(res, struct spi_res, data);
2733
2734 WARN_ON(!list_empty(&sres->entry));
2735 list_add_tail(&sres->entry, &message->resources);
2736}
2737EXPORT_SYMBOL_GPL(spi_res_add);
2738
2739/**
2740 * spi_res_release - release all spi resources for this message
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002741 * @ctlr: the @spi_controller
Martin Sperld780c372015-12-14 15:20:18 +00002742 * @message: the @spi_message
2743 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002744void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
Martin Sperld780c372015-12-14 15:20:18 +00002745{
Vladimir Zapolskiyf5694362019-06-18 19:28:18 +03002746 struct spi_res *res, *tmp;
Martin Sperld780c372015-12-14 15:20:18 +00002747
Vladimir Zapolskiyf5694362019-06-18 19:28:18 +03002748 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
Martin Sperld780c372015-12-14 15:20:18 +00002749 if (res->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002750 res->release(ctlr, message, res->data);
Martin Sperld780c372015-12-14 15:20:18 +00002751
2752 list_del(&res->entry);
2753
2754 kfree(res);
2755 }
2756}
2757EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002758
2759/*-------------------------------------------------------------------------*/
2760
Martin Sperl523baf5a2015-12-14 15:20:19 +00002761/* Core methods for spi_message alterations */
2762
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002763static void __spi_replace_transfers_release(struct spi_controller *ctlr,
Martin Sperl523baf5a2015-12-14 15:20:19 +00002764 struct spi_message *msg,
2765 void *res)
2766{
2767 struct spi_replaced_transfers *rxfer = res;
2768 size_t i;
2769
2770 /* call extra callback if requested */
2771 if (rxfer->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002772 rxfer->release(ctlr, msg, res);
Martin Sperl523baf5a2015-12-14 15:20:19 +00002773
2774 /* insert replaced transfers back into the message */
2775 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2776
2777 /* remove the formerly inserted entries */
2778 for (i = 0; i < rxfer->inserted; i++)
2779 list_del(&rxfer->inserted_transfers[i].transfer_list);
2780}
2781
2782/**
2783 * spi_replace_transfers - replace transfers with several transfers
2784 * and register change with spi_message.resources
2785 * @msg: the spi_message we work upon
2786 * @xfer_first: the first spi_transfer we want to replace
2787 * @remove: number of transfers to remove
2788 * @insert: the number of transfers we want to insert instead
2789 * @release: extra release code necessary in some circumstances
2790 * @extradatasize: extra data to allocate (with alignment guarantees
2791 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002792 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002793 *
2794 * Returns: pointer to @spi_replaced_transfers,
2795 * PTR_ERR(...) in case of errors.
2796 */
2797struct spi_replaced_transfers *spi_replace_transfers(
2798 struct spi_message *msg,
2799 struct spi_transfer *xfer_first,
2800 size_t remove,
2801 size_t insert,
2802 spi_replaced_release_t release,
2803 size_t extradatasize,
2804 gfp_t gfp)
2805{
2806 struct spi_replaced_transfers *rxfer;
2807 struct spi_transfer *xfer;
2808 size_t i;
2809
2810 /* allocate the structure using spi_res */
2811 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
Gustavo A. R. Silvaaef97522019-06-07 13:48:45 -05002812 struct_size(rxfer, inserted_transfers, insert)
Martin Sperl523baf5a2015-12-14 15:20:19 +00002813 + extradatasize,
2814 gfp);
2815 if (!rxfer)
2816 return ERR_PTR(-ENOMEM);
2817
2818 /* the release code to invoke before running the generic release */
2819 rxfer->release = release;
2820
2821 /* assign extradata */
2822 if (extradatasize)
2823 rxfer->extradata =
2824 &rxfer->inserted_transfers[insert];
2825
2826 /* init the replaced_transfers list */
2827 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2828
2829 /* assign the list_entry after which we should reinsert
2830 * the @replaced_transfers - it may be spi_message.messages!
2831 */
2832 rxfer->replaced_after = xfer_first->transfer_list.prev;
2833
2834 /* remove the requested number of transfers */
2835 for (i = 0; i < remove; i++) {
2836 /* if the entry after replaced_after it is msg->transfers
2837 * then we have been requested to remove more transfers
2838 * than are in the list
2839 */
2840 if (rxfer->replaced_after->next == &msg->transfers) {
2841 dev_err(&msg->spi->dev,
2842 "requested to remove more spi_transfers than are available\n");
2843 /* insert replaced transfers back into the message */
2844 list_splice(&rxfer->replaced_transfers,
2845 rxfer->replaced_after);
2846
2847 /* free the spi_replace_transfer structure */
2848 spi_res_free(rxfer);
2849
2850 /* and return with an error */
2851 return ERR_PTR(-EINVAL);
2852 }
2853
2854 /* remove the entry after replaced_after from list of
2855 * transfers and add it to list of replaced_transfers
2856 */
2857 list_move_tail(rxfer->replaced_after->next,
2858 &rxfer->replaced_transfers);
2859 }
2860
2861 /* create copy of the given xfer with identical settings
2862 * based on the first transfer to get removed
2863 */
2864 for (i = 0; i < insert; i++) {
2865 /* we need to run in reverse order */
2866 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2867
2868 /* copy all spi_transfer data */
2869 memcpy(xfer, xfer_first, sizeof(*xfer));
2870
2871 /* add to list */
2872 list_add(&xfer->transfer_list, rxfer->replaced_after);
2873
2874 /* clear cs_change and delay_usecs for all but the last */
2875 if (i) {
2876 xfer->cs_change = false;
2877 xfer->delay_usecs = 0;
2878 }
2879 }
2880
2881 /* set up inserted */
2882 rxfer->inserted = insert;
2883
2884 /* and register it with spi_res/spi_message */
2885 spi_res_add(msg, rxfer);
2886
2887 return rxfer;
2888}
2889EXPORT_SYMBOL_GPL(spi_replace_transfers);
2890
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002891static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
Fabio Estevam08933412016-02-14 13:33:50 -02002892 struct spi_message *msg,
2893 struct spi_transfer **xferp,
2894 size_t maxsize,
2895 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00002896{
2897 struct spi_transfer *xfer = *xferp, *xfers;
2898 struct spi_replaced_transfers *srt;
2899 size_t offset;
2900 size_t count, i;
2901
Martin Sperld9f12122015-12-14 15:20:20 +00002902 /* calculate how many we have to replace */
2903 count = DIV_ROUND_UP(xfer->len, maxsize);
2904
2905 /* create replacement */
2906 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03002907 if (IS_ERR(srt))
2908 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00002909 xfers = srt->inserted_transfers;
2910
2911 /* now handle each of those newly inserted spi_transfers
2912 * note that the replacements spi_transfers all are preset
2913 * to the same values as *xferp, so tx_buf, rx_buf and len
2914 * are all identical (as well as most others)
2915 * so we just have to fix up len and the pointers.
2916 *
2917 * this also includes support for the depreciated
2918 * spi_message.is_dma_mapped interface
2919 */
2920
2921 /* the first transfer just needs the length modified, so we
2922 * run it outside the loop
2923 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02002924 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00002925
2926 /* all the others need rx_buf/tx_buf also set */
2927 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2928 /* update rx_buf, tx_buf and dma */
2929 if (xfers[i].rx_buf)
2930 xfers[i].rx_buf += offset;
2931 if (xfers[i].rx_dma)
2932 xfers[i].rx_dma += offset;
2933 if (xfers[i].tx_buf)
2934 xfers[i].tx_buf += offset;
2935 if (xfers[i].tx_dma)
2936 xfers[i].tx_dma += offset;
2937
2938 /* update length */
2939 xfers[i].len = min(maxsize, xfers[i].len - offset);
2940 }
2941
2942 /* we set up xferp to the last entry we have inserted,
2943 * so that we skip those already split transfers
2944 */
2945 *xferp = &xfers[count - 1];
2946
2947 /* increment statistics counters */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002948 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperld9f12122015-12-14 15:20:20 +00002949 transfers_split_maxsize);
2950 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2951 transfers_split_maxsize);
2952
2953 return 0;
2954}
2955
2956/**
2957 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2958 * when an individual transfer exceeds a
2959 * certain size
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002960 * @ctlr: the @spi_controller for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09002961 * @msg: the @spi_message to transform
2962 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03002963 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00002964 *
2965 * Return: status of transformation
2966 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002967int spi_split_transfers_maxsize(struct spi_controller *ctlr,
Martin Sperld9f12122015-12-14 15:20:20 +00002968 struct spi_message *msg,
2969 size_t maxsize,
2970 gfp_t gfp)
2971{
2972 struct spi_transfer *xfer;
2973 int ret;
2974
2975 /* iterate over the transfer_list,
2976 * but note that xfer is advanced to the last transfer inserted
2977 * to avoid checking sizes again unnecessarily (also xfer does
2978 * potentiall belong to a different list by the time the
2979 * replacement has happened
2980 */
2981 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2982 if (xfer->len > maxsize) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002983 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
2984 maxsize, gfp);
Martin Sperld9f12122015-12-14 15:20:20 +00002985 if (ret)
2986 return ret;
2987 }
2988 }
2989
2990 return 0;
2991}
2992EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08002993
2994/*-------------------------------------------------------------------------*/
2995
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002996/* Core methods for SPI controller protocol drivers. Some of the
David Brownell7d077192009-06-17 16:26:03 -07002997 * other core methods are currently defined as inline functions.
2998 */
2999
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003000static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
3001 u8 bits_per_word)
Stefan Brüns63ab6452015-08-23 16:06:30 +02003002{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003003 if (ctlr->bits_per_word_mask) {
Stefan Brüns63ab6452015-08-23 16:06:30 +02003004 /* Only 32 bits fit in the mask */
3005 if (bits_per_word > 32)
3006 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003007 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
Stefan Brüns63ab6452015-08-23 16:06:30 +02003008 return -EINVAL;
3009 }
3010
3011 return 0;
3012}
3013
David Brownell7d077192009-06-17 16:26:03 -07003014/**
3015 * spi_setup - setup SPI mode and clock rate
3016 * @spi: the device whose settings are being modified
3017 * Context: can sleep, and no requests are queued to the device
3018 *
3019 * SPI protocol drivers may need to update the transfer mode if the
3020 * device doesn't work with its default. They may likewise need
3021 * to update clock rates or word sizes from initial values. This function
3022 * changes those settings, and must be called from a context that can sleep.
3023 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
3024 * effect the next time the device is selected and data is transferred to
3025 * or from it. When this function returns, the spi device is deselected.
3026 *
3027 * Note that this call will fail if the protocol driver specifies an option
3028 * that the underlying controller or its driver does not support. For
3029 * example, not all hardware supports wire transfers using nine bit words,
3030 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003031 *
3032 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07003033 */
3034int spi_setup(struct spi_device *spi)
3035{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003036 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03003037 int status;
David Brownell7d077192009-06-17 16:26:03 -07003038
wangyuhangf477b7f2013-08-11 18:15:17 +08003039 /* check mode to prevent that DUAL and QUAD set at the same time
3040 */
3041 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
3042 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
3043 dev_err(&spi->dev,
3044 "setup: can not select dual and quad at the same time\n");
3045 return -EINVAL;
3046 }
3047 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
3048 */
3049 if ((spi->mode & SPI_3WIRE) && (spi->mode &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00003050 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3051 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
wangyuhangf477b7f2013-08-11 18:15:17 +08003052 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07003053 /* help drivers fail *cleanly* when they need options
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003054 * that aren't supported with their current controller
David Lechnercbaa62e2018-09-12 19:39:18 -05003055 * SPI_CS_WORD has a fallback software implementation,
3056 * so it is ignored here.
David Brownelle7db06b2009-06-17 16:26:04 -07003057 */
David Lechnercbaa62e2018-09-12 19:39:18 -05003058 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
Serge Semind61ad23c2019-04-26 13:30:07 +03003059 /* nothing prevents from working with active-high CS in case if it
3060 * is driven by GPIO.
3061 */
3062 if (gpio_is_valid(spi->cs_gpio))
3063 bad_bits &= ~SPI_CS_HIGH;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003064 ugly_bits = bad_bits &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00003065 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3066 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003067 if (ugly_bits) {
3068 dev_warn(&spi->dev,
3069 "setup: ignoring unsupported mode bits %x\n",
3070 ugly_bits);
3071 spi->mode &= ~ugly_bits;
3072 bad_bits &= ~ugly_bits;
3073 }
David Brownelle7db06b2009-06-17 16:26:04 -07003074 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02003075 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07003076 bad_bits);
3077 return -EINVAL;
3078 }
3079
David Brownell7d077192009-06-17 16:26:03 -07003080 if (!spi->bits_per_word)
3081 spi->bits_per_word = 8;
3082
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003083 status = __spi_validate_bits_per_word(spi->controller,
3084 spi->bits_per_word);
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03003085 if (status)
3086 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02003087
Axel Lin052eb2d2014-02-10 00:08:05 +08003088 if (!spi->max_speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003089 spi->max_speed_hz = spi->controller->max_speed_hz;
Axel Lin052eb2d2014-02-10 00:08:05 +08003090
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003091 if (spi->controller->setup)
3092 status = spi->controller->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07003093
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05003094 spi_set_cs(spi, false);
3095
Douglas Anderson924b5862019-05-15 09:48:12 -07003096 if (spi->rt && !spi->controller->rt) {
3097 spi->controller->rt = true;
3098 spi_set_thread_rt(spi->controller);
3099 }
3100
Jingoo Han5fe5f052013-10-14 10:31:51 +09003101 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
David Brownell7d077192009-06-17 16:26:03 -07003102 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
3103 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
3104 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
3105 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
3106 (spi->mode & SPI_LOOP) ? "loopback, " : "",
3107 spi->bits_per_word, spi->max_speed_hz,
3108 status);
3109
3110 return status;
3111}
3112EXPORT_SYMBOL_GPL(spi_setup);
3113
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003114/**
3115 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3116 * @spi: the device that requires specific CS timing configuration
3117 * @setup: CS setup time in terms of clock count
3118 * @hold: CS hold time in terms of clock count
3119 * @inactive_dly: CS inactive delay between transfers in terms of clock count
3120 */
3121void spi_set_cs_timing(struct spi_device *spi, u8 setup, u8 hold,
3122 u8 inactive_dly)
3123{
3124 if (spi->controller->set_cs_timing)
3125 spi->controller->set_cs_timing(spi, setup, hold, inactive_dly);
3126}
3127EXPORT_SYMBOL_GPL(spi_set_cs_timing);
3128
Mark Brown90808732013-11-13 23:44:15 +00003129static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003130{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003131 struct spi_controller *ctlr = spi->controller;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303132 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09003133 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003134
Mark Brown24a00132013-07-10 15:05:40 +01003135 if (list_empty(&message->transfers))
3136 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01003137
David Lechnercbaa62e2018-09-12 19:39:18 -05003138 /* If an SPI controller does not support toggling the CS line on each
David Lechner71388b212018-09-18 12:08:48 -05003139 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
3140 * for the CS line, we can emulate the CS-per-word hardware function by
David Lechnercbaa62e2018-09-12 19:39:18 -05003141 * splitting transfers into one-word transfers and ensuring that
3142 * cs_change is set for each transfer.
3143 */
David Lechner71388b212018-09-18 12:08:48 -05003144 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
Linus Walleijf3186dd2019-01-07 16:51:50 +01003145 spi->cs_gpiod ||
David Lechner71388b212018-09-18 12:08:48 -05003146 gpio_is_valid(spi->cs_gpio))) {
David Lechnercbaa62e2018-09-12 19:39:18 -05003147 size_t maxsize;
3148 int ret;
3149
3150 maxsize = (spi->bits_per_word + 7) / 8;
3151
3152 /* spi_split_transfers_maxsize() requires message->spi */
3153 message->spi = spi;
3154
3155 ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
3156 GFP_KERNEL);
3157 if (ret)
3158 return ret;
3159
3160 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3161 /* don't change cs_change on the last entry in the list */
3162 if (list_is_last(&xfer->transfer_list, &message->transfers))
3163 break;
3164 xfer->cs_change = 1;
3165 }
3166 }
3167
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003168 /* Half-duplex links include original MicroWire, and ones with
3169 * only one data pin like SPI_3WIRE (switches direction) or where
3170 * either MOSI or MISO is missing. They can also be caused by
3171 * software limitations.
3172 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003173 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
3174 (spi->mode & SPI_3WIRE)) {
3175 unsigned flags = ctlr->flags;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003176
3177 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3178 if (xfer->rx_buf && xfer->tx_buf)
3179 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003180 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003181 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003182 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003183 return -EINVAL;
3184 }
3185 }
3186
Laxman Dewangane6811d12012-11-09 14:36:45 +05303187 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303188 * Set transfer bits_per_word and max speed as spi device default if
3189 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08003190 * Set transfer tx_nbits and rx_nbits as single transfer default
3191 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Jonas Bonnb7bb3672019-01-30 09:40:04 +01003192 * Ensure transfer word_delay is at least as long as that required by
3193 * device itself.
Laxman Dewangane6811d12012-11-09 14:36:45 +05303194 */
Martin Sperl77e80582015-11-27 12:31:09 +00003195 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303196 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Martin Sperl5d7e2b52019-02-23 08:49:49 +00003197 xfer->effective_speed_hz = 0;
Sourav Poddar078726c2013-07-18 15:31:25 +05303198 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303199 if (!xfer->bits_per_word)
3200 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08003201
3202 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303203 xfer->speed_hz = spi->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08003204
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003205 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
3206 xfer->speed_hz = ctlr->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02003207
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003208 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
Stefan Brüns63ab6452015-08-23 16:06:30 +02003209 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01003210
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003211 /*
3212 * SPI transfer length should be multiple of SPI word size
3213 * where SPI word size should be power-of-two multiple
3214 */
3215 if (xfer->bits_per_word <= 8)
3216 w_size = 1;
3217 else if (xfer->bits_per_word <= 16)
3218 w_size = 2;
3219 else
3220 w_size = 4;
3221
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003222 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09003223 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003224 return -EINVAL;
3225
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003226 if (xfer->speed_hz && ctlr->min_speed_hz &&
3227 xfer->speed_hz < ctlr->min_speed_hz)
Mark Browna2fd4f92013-07-10 14:57:26 +01003228 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08003229
3230 if (xfer->tx_buf && !xfer->tx_nbits)
3231 xfer->tx_nbits = SPI_NBITS_SINGLE;
3232 if (xfer->rx_buf && !xfer->rx_nbits)
3233 xfer->rx_nbits = SPI_NBITS_SINGLE;
3234 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01003235 * 1. check the value matches one of single, dual and quad
3236 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08003237 */
Sourav Poddardb90a442013-08-22 21:20:48 +05303238 if (xfer->tx_buf) {
3239 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
3240 xfer->tx_nbits != SPI_NBITS_DUAL &&
3241 xfer->tx_nbits != SPI_NBITS_QUAD)
3242 return -EINVAL;
3243 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
3244 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3245 return -EINVAL;
3246 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
3247 !(spi->mode & SPI_TX_QUAD))
3248 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303249 }
wangyuhangf477b7f2013-08-11 18:15:17 +08003250 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05303251 if (xfer->rx_buf) {
3252 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
3253 xfer->rx_nbits != SPI_NBITS_DUAL &&
3254 xfer->rx_nbits != SPI_NBITS_QUAD)
3255 return -EINVAL;
3256 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
3257 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3258 return -EINVAL;
3259 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
3260 !(spi->mode & SPI_RX_QUAD))
3261 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303262 }
Jonas Bonnb7bb3672019-01-30 09:40:04 +01003263
3264 if (xfer->word_delay_usecs < spi->word_delay_usecs)
3265 xfer->word_delay_usecs = spi->word_delay_usecs;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303266 }
3267
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003268 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00003269
3270 return 0;
3271}
3272
3273static int __spi_async(struct spi_device *spi, struct spi_message *message)
3274{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003275 struct spi_controller *ctlr = spi->controller;
Mark Brown90808732013-11-13 23:44:15 +00003276
Boris Brezillonb5932f52018-04-26 18:18:15 +02003277 /*
3278 * Some controllers do not support doing regular SPI transfers. Return
3279 * ENOTSUPP when this is the case.
3280 */
3281 if (!ctlr->transfer)
3282 return -ENOTSUPP;
3283
Mark Brown90808732013-11-13 23:44:15 +00003284 message->spi = spi;
3285
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003286 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003287 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3288
Mark Brown90808732013-11-13 23:44:15 +00003289 trace_spi_message_submit(message);
3290
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003291 return ctlr->transfer(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003292}
3293
David Brownell568d0692009-09-22 16:46:18 -07003294/**
3295 * spi_async - asynchronous SPI transfer
3296 * @spi: device with which data will be exchanged
3297 * @message: describes the data transfers, including completion callback
3298 * Context: any (irqs may be blocked, etc)
3299 *
3300 * This call may be used in_irq and other contexts which can't sleep,
3301 * as well as from task contexts which can sleep.
3302 *
3303 * The completion callback is invoked in a context which can't sleep.
3304 * Before that invocation, the value of message->status is undefined.
3305 * When the callback is issued, message->status holds either zero (to
3306 * indicate complete success) or a negative error code. After that
3307 * callback returns, the driver which issued the transfer request may
3308 * deallocate the associated memory; it's no longer in use by any SPI
3309 * core or controller driver code.
3310 *
3311 * Note that although all messages to a spi_device are handled in
3312 * FIFO order, messages may go to different devices in other orders.
3313 * Some device might be higher priority, or have various "hard" access
3314 * time requirements, for example.
3315 *
3316 * On detection of any fault during the transfer, processing of
3317 * the entire message is aborted, and the device is deselected.
3318 * Until returning from the associated message completion callback,
3319 * no other spi_message queued to that device will be processed.
3320 * (This rule applies equally to all the synchronous transfer calls,
3321 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003322 *
3323 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07003324 */
3325int spi_async(struct spi_device *spi, struct spi_message *message)
3326{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003327 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003328 int ret;
3329 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07003330
Mark Brown90808732013-11-13 23:44:15 +00003331 ret = __spi_validate(spi, message);
3332 if (ret != 0)
3333 return ret;
3334
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003335 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07003336
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003337 if (ctlr->bus_lock_flag)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003338 ret = -EBUSY;
3339 else
3340 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07003341
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003342 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003343
3344 return ret;
David Brownell568d0692009-09-22 16:46:18 -07003345}
3346EXPORT_SYMBOL_GPL(spi_async);
3347
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003348/**
3349 * spi_async_locked - version of spi_async with exclusive bus usage
3350 * @spi: device with which data will be exchanged
3351 * @message: describes the data transfers, including completion callback
3352 * Context: any (irqs may be blocked, etc)
3353 *
3354 * This call may be used in_irq and other contexts which can't sleep,
3355 * as well as from task contexts which can sleep.
3356 *
3357 * The completion callback is invoked in a context which can't sleep.
3358 * Before that invocation, the value of message->status is undefined.
3359 * When the callback is issued, message->status holds either zero (to
3360 * indicate complete success) or a negative error code. After that
3361 * callback returns, the driver which issued the transfer request may
3362 * deallocate the associated memory; it's no longer in use by any SPI
3363 * core or controller driver code.
3364 *
3365 * Note that although all messages to a spi_device are handled in
3366 * FIFO order, messages may go to different devices in other orders.
3367 * Some device might be higher priority, or have various "hard" access
3368 * time requirements, for example.
3369 *
3370 * On detection of any fault during the transfer, processing of
3371 * the entire message is aborted, and the device is deselected.
3372 * Until returning from the associated message completion callback,
3373 * no other spi_message queued to that device will be processed.
3374 * (This rule applies equally to all the synchronous transfer calls,
3375 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003376 *
3377 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003378 */
3379int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3380{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003381 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003382 int ret;
3383 unsigned long flags;
3384
Mark Brown90808732013-11-13 23:44:15 +00003385 ret = __spi_validate(spi, message);
3386 if (ret != 0)
3387 return ret;
3388
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003389 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003390
3391 ret = __spi_async(spi, message);
3392
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003393 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003394
3395 return ret;
3396
3397}
3398EXPORT_SYMBOL_GPL(spi_async_locked);
3399
David Brownell7d077192009-06-17 16:26:03 -07003400/*-------------------------------------------------------------------------*/
3401
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003402/* Utility methods for SPI protocol drivers, layered on
David Brownell7d077192009-06-17 16:26:03 -07003403 * top of the core. Some other utility methods are defined as
3404 * inline functions.
3405 */
3406
Andrew Morton5d870c82006-01-11 11:23:49 -08003407static void spi_complete(void *arg)
3408{
3409 complete(arg);
3410}
3411
Mark Brownef4d96e2016-07-21 23:53:31 +01003412static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003413{
3414 DECLARE_COMPLETION_ONSTACK(done);
3415 int status;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003416 struct spi_controller *ctlr = spi->controller;
Mark Brown0461a412014-12-09 21:38:05 +00003417 unsigned long flags;
3418
3419 status = __spi_validate(spi, message);
3420 if (status != 0)
3421 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003422
3423 message->complete = spi_complete;
3424 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00003425 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003426
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003427 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003428 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3429
Mark Brown0461a412014-12-09 21:38:05 +00003430 /* If we're not using the legacy transfer method then we will
3431 * try to transfer in the calling context so special case.
3432 * This code would be less tricky if we could remove the
3433 * support for driver implemented message queues.
3434 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003435 if (ctlr->transfer == spi_queued_transfer) {
3436 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003437
3438 trace_spi_message_submit(message);
3439
3440 status = __spi_queued_transfer(spi, message, false);
3441
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003442 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003443 } else {
3444 status = spi_async_locked(spi, message);
3445 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003446
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003447 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00003448 /* Push out the messages in the calling context if we
3449 * can.
3450 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003451 if (ctlr->transfer == spi_queued_transfer) {
3452 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperleca2ebc2015-06-22 13:00:36 +00003453 spi_sync_immediate);
3454 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3455 spi_sync_immediate);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003456 __spi_pump_messages(ctlr, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003457 }
Mark Brown0461a412014-12-09 21:38:05 +00003458
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003459 wait_for_completion(&done);
3460 status = message->status;
3461 }
3462 message->context = NULL;
3463 return status;
3464}
3465
David Brownell8ae12a02006-01-08 13:34:19 -08003466/**
3467 * spi_sync - blocking/synchronous SPI data transfers
3468 * @spi: device with which data will be exchanged
3469 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07003470 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003471 *
3472 * This call may only be used from a context that may sleep. The sleep
3473 * is non-interruptible, and has no timeout. Low-overhead controller
3474 * drivers may DMA directly into and out of the message buffers.
3475 *
3476 * Note that the SPI device's chip select is active during the message,
3477 * and then is normally disabled between messages. Drivers for some
3478 * frequently-used devices may want to minimize costs of selecting a chip,
3479 * by leaving it selected in anticipation that the next message will go
3480 * to the same chip. (That may increase power usage.)
3481 *
David Brownell0c8684612006-01-08 13:34:25 -08003482 * Also, the caller is guaranteeing that the memory associated with the
3483 * message will not be freed before this call returns.
3484 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003485 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003486 */
3487int spi_sync(struct spi_device *spi, struct spi_message *message)
3488{
Mark Brownef4d96e2016-07-21 23:53:31 +01003489 int ret;
3490
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003491 mutex_lock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003492 ret = __spi_sync(spi, message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003493 mutex_unlock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003494
3495 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08003496}
3497EXPORT_SYMBOL_GPL(spi_sync);
3498
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003499/**
3500 * spi_sync_locked - version of spi_sync with exclusive bus usage
3501 * @spi: device with which data will be exchanged
3502 * @message: describes the data transfers
3503 * Context: can sleep
3504 *
3505 * This call may only be used from a context that may sleep. The sleep
3506 * is non-interruptible, and has no timeout. Low-overhead controller
3507 * drivers may DMA directly into and out of the message buffers.
3508 *
3509 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003510 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003511 * be released by a spi_bus_unlock call when the exclusive access is over.
3512 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003513 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003514 */
3515int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3516{
Mark Brownef4d96e2016-07-21 23:53:31 +01003517 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003518}
3519EXPORT_SYMBOL_GPL(spi_sync_locked);
3520
3521/**
3522 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003523 * @ctlr: SPI bus master that should be locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003524 * Context: can sleep
3525 *
3526 * This call may only be used from a context that may sleep. The sleep
3527 * is non-interruptible, and has no timeout.
3528 *
3529 * This call should be used by drivers that require exclusive access to the
3530 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3531 * exclusive access is over. Data transfer must be done by spi_sync_locked
3532 * and spi_async_locked calls when the SPI bus lock is held.
3533 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003534 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003535 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003536int spi_bus_lock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003537{
3538 unsigned long flags;
3539
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003540 mutex_lock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003541
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003542 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3543 ctlr->bus_lock_flag = 1;
3544 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003545
3546 /* mutex remains locked until spi_bus_unlock is called */
3547
3548 return 0;
3549}
3550EXPORT_SYMBOL_GPL(spi_bus_lock);
3551
3552/**
3553 * spi_bus_unlock - release the lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003554 * @ctlr: SPI bus master that was locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003555 * Context: can sleep
3556 *
3557 * This call may only be used from a context that may sleep. The sleep
3558 * is non-interruptible, and has no timeout.
3559 *
3560 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3561 * call.
3562 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003563 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003564 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003565int spi_bus_unlock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003566{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003567 ctlr->bus_lock_flag = 0;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003568
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003569 mutex_unlock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003570
3571 return 0;
3572}
3573EXPORT_SYMBOL_GPL(spi_bus_unlock);
3574
David Brownella9948b62006-04-02 10:37:40 -08003575/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003576#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003577
3578static u8 *buf;
3579
3580/**
3581 * spi_write_then_read - SPI synchronous write followed by read
3582 * @spi: device with which data will be exchanged
3583 * @txbuf: data to be written (need not be dma-safe)
3584 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003585 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3586 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003587 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003588 *
3589 * This performs a half duplex MicroWire style transaction with the
3590 * device, sending txbuf and then reading rxbuf. The return value
3591 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003592 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003593 *
David Brownell0c8684612006-01-08 13:34:25 -08003594 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003595 * portable code should never use this for more than 32 bytes.
3596 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c8684612006-01-08 13:34:25 -08003597 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003598 *
3599 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003600 */
3601int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003602 const void *txbuf, unsigned n_tx,
3603 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003604{
David Brownell068f4072007-12-04 23:45:09 -08003605 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003606
3607 int status;
3608 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003609 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003610 u8 *local_buf;
3611
Mark Brownb3a223e2012-12-02 12:54:25 +09003612 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3613 * copying here, (as a pure convenience thing), but we can
3614 * keep heap costs out of the hot path unless someone else is
3615 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003616 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003617 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003618 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3619 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003620 if (!local_buf)
3621 return -ENOMEM;
3622 } else {
3623 local_buf = buf;
3624 }
David Brownell8ae12a02006-01-08 13:34:19 -08003625
Vitaly Wool8275c642006-01-08 13:34:28 -08003626 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003627 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003628 if (n_tx) {
3629 x[0].len = n_tx;
3630 spi_message_add_tail(&x[0], &message);
3631 }
3632 if (n_rx) {
3633 x[1].len = n_rx;
3634 spi_message_add_tail(&x[1], &message);
3635 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003636
David Brownell8ae12a02006-01-08 13:34:19 -08003637 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003638 x[0].tx_buf = local_buf;
3639 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003640
3641 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003642 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003643 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003644 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003645
David Brownellbdff5492009-04-13 14:39:57 -07003646 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003647 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003648 else
3649 kfree(local_buf);
3650
3651 return status;
3652}
3653EXPORT_SYMBOL_GPL(spi_write_then_read);
3654
3655/*-------------------------------------------------------------------------*/
3656
Marco Felsch5f143af2018-09-25 11:42:29 +02003657#if IS_ENABLED(CONFIG_OF)
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +01003658static int __spi_of_device_match(struct device *dev, const void *data)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003659{
3660 return dev->of_node == data;
3661}
3662
3663/* must call put_device() when done with returned spi_device device */
Marco Felsch5f143af2018-09-25 11:42:29 +02003664struct spi_device *of_find_spi_device_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003665{
3666 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3667 __spi_of_device_match);
3668 return dev ? to_spi_device(dev) : NULL;
3669}
Marco Felsch5f143af2018-09-25 11:42:29 +02003670EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
3671#endif /* IS_ENABLED(CONFIG_OF) */
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003672
Marco Felsch5f143af2018-09-25 11:42:29 +02003673#if IS_ENABLED(CONFIG_OF_DYNAMIC)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003674static int __spi_of_controller_match(struct device *dev, const void *data)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003675{
3676 return dev->of_node == data;
3677}
3678
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003679/* the spi controllers are not using spi_bus, so we find it with another way */
3680static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003681{
3682 struct device *dev;
3683
3684 dev = class_find_device(&spi_master_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003685 __spi_of_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003686 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3687 dev = class_find_device(&spi_slave_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003688 __spi_of_controller_match);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003689 if (!dev)
3690 return NULL;
3691
3692 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003693 return container_of(dev, struct spi_controller, dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003694}
3695
3696static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3697 void *arg)
3698{
3699 struct of_reconfig_data *rd = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003700 struct spi_controller *ctlr;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003701 struct spi_device *spi;
3702
3703 switch (of_reconfig_get_state_change(action, arg)) {
3704 case OF_RECONFIG_CHANGE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003705 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3706 if (ctlr == NULL)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003707 return NOTIFY_OK; /* not for us */
3708
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003709 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003710 put_device(&ctlr->dev);
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003711 return NOTIFY_OK;
3712 }
3713
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003714 spi = of_register_spi_device(ctlr, rd->dn);
3715 put_device(&ctlr->dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003716
3717 if (IS_ERR(spi)) {
Rob Herring25c56c82017-07-18 16:43:31 -05003718 pr_err("%s: failed to create for '%pOF'\n",
3719 __func__, rd->dn);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003720 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003721 return notifier_from_errno(PTR_ERR(spi));
3722 }
3723 break;
3724
3725 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003726 /* already depopulated? */
3727 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3728 return NOTIFY_OK;
3729
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003730 /* find our device by node */
3731 spi = of_find_spi_device_by_node(rd->dn);
3732 if (spi == NULL)
3733 return NOTIFY_OK; /* no? not meant for us */
3734
3735 /* unregister takes one ref away */
3736 spi_unregister_device(spi);
3737
3738 /* and put the reference of the find */
3739 put_device(&spi->dev);
3740 break;
3741 }
3742
3743 return NOTIFY_OK;
3744}
3745
3746static struct notifier_block spi_of_notifier = {
3747 .notifier_call = of_spi_notify,
3748};
3749#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3750extern struct notifier_block spi_of_notifier;
3751#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3752
Octavian Purdila7f244672016-07-08 19:13:11 +03003753#if IS_ENABLED(CONFIG_ACPI)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003754static int spi_acpi_controller_match(struct device *dev, const void *data)
Octavian Purdila7f244672016-07-08 19:13:11 +03003755{
3756 return ACPI_COMPANION(dev->parent) == data;
3757}
3758
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +01003759static int spi_acpi_device_match(struct device *dev, const void *data)
Octavian Purdila7f244672016-07-08 19:13:11 +03003760{
3761 return ACPI_COMPANION(dev) == data;
3762}
3763
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003764static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
Octavian Purdila7f244672016-07-08 19:13:11 +03003765{
3766 struct device *dev;
3767
3768 dev = class_find_device(&spi_master_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003769 spi_acpi_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003770 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3771 dev = class_find_device(&spi_slave_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003772 spi_acpi_controller_match);
Octavian Purdila7f244672016-07-08 19:13:11 +03003773 if (!dev)
3774 return NULL;
3775
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003776 return container_of(dev, struct spi_controller, dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003777}
3778
3779static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3780{
3781 struct device *dev;
3782
3783 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3784
3785 return dev ? to_spi_device(dev) : NULL;
3786}
3787
3788static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3789 void *arg)
3790{
3791 struct acpi_device *adev = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003792 struct spi_controller *ctlr;
Octavian Purdila7f244672016-07-08 19:13:11 +03003793 struct spi_device *spi;
3794
3795 switch (value) {
3796 case ACPI_RECONFIG_DEVICE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003797 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
3798 if (!ctlr)
Octavian Purdila7f244672016-07-08 19:13:11 +03003799 break;
3800
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003801 acpi_register_spi_device(ctlr, adev);
3802 put_device(&ctlr->dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003803 break;
3804 case ACPI_RECONFIG_DEVICE_REMOVE:
3805 if (!acpi_device_enumerated(adev))
3806 break;
3807
3808 spi = acpi_spi_find_device_by_adev(adev);
3809 if (!spi)
3810 break;
3811
3812 spi_unregister_device(spi);
3813 put_device(&spi->dev);
3814 break;
3815 }
3816
3817 return NOTIFY_OK;
3818}
3819
3820static struct notifier_block spi_acpi_notifier = {
3821 .notifier_call = acpi_spi_notify,
3822};
3823#else
3824extern struct notifier_block spi_acpi_notifier;
3825#endif
3826
David Brownell8ae12a02006-01-08 13:34:19 -08003827static int __init spi_init(void)
3828{
David Brownellb8852442006-01-08 13:34:23 -08003829 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08003830
Christoph Lametere94b1762006-12-06 20:33:17 -08003831 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08003832 if (!buf) {
3833 status = -ENOMEM;
3834 goto err0;
3835 }
3836
3837 status = bus_register(&spi_bus_type);
3838 if (status < 0)
3839 goto err1;
3840
3841 status = class_register(&spi_master_class);
3842 if (status < 0)
3843 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003844
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003845 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
3846 status = class_register(&spi_slave_class);
3847 if (status < 0)
3848 goto err3;
3849 }
3850
Fabio Estevam52677202014-11-26 20:13:57 -02003851 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003852 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03003853 if (IS_ENABLED(CONFIG_ACPI))
3854 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003855
David Brownell8ae12a02006-01-08 13:34:19 -08003856 return 0;
David Brownellb8852442006-01-08 13:34:23 -08003857
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003858err3:
3859 class_unregister(&spi_master_class);
David Brownellb8852442006-01-08 13:34:23 -08003860err2:
3861 bus_unregister(&spi_bus_type);
3862err1:
3863 kfree(buf);
3864 buf = NULL;
3865err0:
3866 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08003867}
David Brownellb8852442006-01-08 13:34:23 -08003868
David Brownell8ae12a02006-01-08 13:34:19 -08003869/* board_info is normally registered in arch_initcall(),
3870 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08003871 *
3872 * REVISIT only boardinfo really needs static linking. the rest (device and
3873 * driver registration) _could_ be dynamically linked (modular) ... costs
3874 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08003875 */
David Brownell673c0c02008-10-15 22:02:46 -07003876postcore_initcall(spi_init);