blob: 6ea7b286c80c250fc1649b710f0e5056be1617bf [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_controller_put(spi->controller);
Trent Piepho50395632018-09-20 19:18:32 +000051 kfree(spi->driver_override);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000052 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080053}
54
55static ssize_t
56modalias_show(struct device *dev, struct device_attribute *a, char *buf)
57{
58 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080059 int len;
60
61 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
62 if (len != -ENODEV)
63 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080064
Grant Likelyd8e328b2012-05-20 00:08:13 -060065 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080066}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070067static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080068
Trent Piepho50395632018-09-20 19:18:32 +000069static ssize_t driver_override_store(struct device *dev,
70 struct device_attribute *a,
71 const char *buf, size_t count)
72{
73 struct spi_device *spi = to_spi_device(dev);
74 const char *end = memchr(buf, '\n', count);
75 const size_t len = end ? end - buf : count;
76 const char *driver_override, *old;
77
78 /* We need to keep extra room for a newline when displaying value */
79 if (len >= (PAGE_SIZE - 1))
80 return -EINVAL;
81
82 driver_override = kstrndup(buf, len, GFP_KERNEL);
83 if (!driver_override)
84 return -ENOMEM;
85
86 device_lock(dev);
87 old = spi->driver_override;
88 if (len) {
89 spi->driver_override = driver_override;
90 } else {
Andy Shevchenkobe73e322019-10-23 15:16:43 +030091 /* Empty string, disable driver override */
Trent Piepho50395632018-09-20 19:18:32 +000092 spi->driver_override = NULL;
93 kfree(driver_override);
94 }
95 device_unlock(dev);
96 kfree(old);
97
98 return count;
99}
100
101static ssize_t driver_override_show(struct device *dev,
102 struct device_attribute *a, char *buf)
103{
104 const struct spi_device *spi = to_spi_device(dev);
105 ssize_t len;
106
107 device_lock(dev);
108 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : "");
109 device_unlock(dev);
110 return len;
111}
112static DEVICE_ATTR_RW(driver_override);
113
Martin Sperleca2ebc2015-06-22 13:00:36 +0000114#define SPI_STATISTICS_ATTRS(field, file) \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200115static ssize_t spi_controller_##field##_show(struct device *dev, \
116 struct device_attribute *attr, \
117 char *buf) \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000118{ \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200119 struct spi_controller *ctlr = container_of(dev, \
120 struct spi_controller, dev); \
121 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000122} \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200123static struct device_attribute dev_attr_spi_controller_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +0200124 .attr = { .name = file, .mode = 0444 }, \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200125 .show = spi_controller_##field##_show, \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000126}; \
127static ssize_t spi_device_##field##_show(struct device *dev, \
128 struct device_attribute *attr, \
129 char *buf) \
130{ \
Geliang Tangd1eba932015-12-23 00:18:41 +0800131 struct spi_device *spi = to_spi_device(dev); \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000132 return spi_statistics_##field##_show(&spi->statistics, buf); \
133} \
134static struct device_attribute dev_attr_spi_device_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +0200135 .attr = { .name = file, .mode = 0444 }, \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000136 .show = spi_device_##field##_show, \
137}
138
139#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
140static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
141 char *buf) \
142{ \
143 unsigned long flags; \
144 ssize_t len; \
145 spin_lock_irqsave(&stat->lock, flags); \
146 len = sprintf(buf, format_string, stat->field); \
147 spin_unlock_irqrestore(&stat->lock, flags); \
148 return len; \
149} \
150SPI_STATISTICS_ATTRS(name, file)
151
152#define SPI_STATISTICS_SHOW(field, format_string) \
153 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
154 field, format_string)
155
156SPI_STATISTICS_SHOW(messages, "%lu");
157SPI_STATISTICS_SHOW(transfers, "%lu");
158SPI_STATISTICS_SHOW(errors, "%lu");
159SPI_STATISTICS_SHOW(timedout, "%lu");
160
161SPI_STATISTICS_SHOW(spi_sync, "%lu");
162SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
163SPI_STATISTICS_SHOW(spi_async, "%lu");
164
165SPI_STATISTICS_SHOW(bytes, "%llu");
166SPI_STATISTICS_SHOW(bytes_rx, "%llu");
167SPI_STATISTICS_SHOW(bytes_tx, "%llu");
168
Martin Sperl6b7bc062015-06-22 13:02:04 +0000169#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
170 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
171 "transfer_bytes_histo_" number, \
172 transfer_bytes_histo[index], "%lu")
173SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
174SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
175SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
176SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
177SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
178SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
179SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
180SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
181SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
182SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
183SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
184SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
185SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
186SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
187SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
188SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
189SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
190
Martin Sperld9f12122015-12-14 15:20:20 +0000191SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
192
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700193static struct attribute *spi_dev_attrs[] = {
194 &dev_attr_modalias.attr,
Trent Piepho50395632018-09-20 19:18:32 +0000195 &dev_attr_driver_override.attr,
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700196 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800197};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000198
199static const struct attribute_group spi_dev_group = {
200 .attrs = spi_dev_attrs,
201};
202
203static struct attribute *spi_device_statistics_attrs[] = {
204 &dev_attr_spi_device_messages.attr,
205 &dev_attr_spi_device_transfers.attr,
206 &dev_attr_spi_device_errors.attr,
207 &dev_attr_spi_device_timedout.attr,
208 &dev_attr_spi_device_spi_sync.attr,
209 &dev_attr_spi_device_spi_sync_immediate.attr,
210 &dev_attr_spi_device_spi_async.attr,
211 &dev_attr_spi_device_bytes.attr,
212 &dev_attr_spi_device_bytes_rx.attr,
213 &dev_attr_spi_device_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000214 &dev_attr_spi_device_transfer_bytes_histo0.attr,
215 &dev_attr_spi_device_transfer_bytes_histo1.attr,
216 &dev_attr_spi_device_transfer_bytes_histo2.attr,
217 &dev_attr_spi_device_transfer_bytes_histo3.attr,
218 &dev_attr_spi_device_transfer_bytes_histo4.attr,
219 &dev_attr_spi_device_transfer_bytes_histo5.attr,
220 &dev_attr_spi_device_transfer_bytes_histo6.attr,
221 &dev_attr_spi_device_transfer_bytes_histo7.attr,
222 &dev_attr_spi_device_transfer_bytes_histo8.attr,
223 &dev_attr_spi_device_transfer_bytes_histo9.attr,
224 &dev_attr_spi_device_transfer_bytes_histo10.attr,
225 &dev_attr_spi_device_transfer_bytes_histo11.attr,
226 &dev_attr_spi_device_transfer_bytes_histo12.attr,
227 &dev_attr_spi_device_transfer_bytes_histo13.attr,
228 &dev_attr_spi_device_transfer_bytes_histo14.attr,
229 &dev_attr_spi_device_transfer_bytes_histo15.attr,
230 &dev_attr_spi_device_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000231 &dev_attr_spi_device_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000232 NULL,
233};
234
235static const struct attribute_group spi_device_statistics_group = {
236 .name = "statistics",
237 .attrs = spi_device_statistics_attrs,
238};
239
240static const struct attribute_group *spi_dev_groups[] = {
241 &spi_dev_group,
242 &spi_device_statistics_group,
243 NULL,
244};
245
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200246static struct attribute *spi_controller_statistics_attrs[] = {
247 &dev_attr_spi_controller_messages.attr,
248 &dev_attr_spi_controller_transfers.attr,
249 &dev_attr_spi_controller_errors.attr,
250 &dev_attr_spi_controller_timedout.attr,
251 &dev_attr_spi_controller_spi_sync.attr,
252 &dev_attr_spi_controller_spi_sync_immediate.attr,
253 &dev_attr_spi_controller_spi_async.attr,
254 &dev_attr_spi_controller_bytes.attr,
255 &dev_attr_spi_controller_bytes_rx.attr,
256 &dev_attr_spi_controller_bytes_tx.attr,
257 &dev_attr_spi_controller_transfer_bytes_histo0.attr,
258 &dev_attr_spi_controller_transfer_bytes_histo1.attr,
259 &dev_attr_spi_controller_transfer_bytes_histo2.attr,
260 &dev_attr_spi_controller_transfer_bytes_histo3.attr,
261 &dev_attr_spi_controller_transfer_bytes_histo4.attr,
262 &dev_attr_spi_controller_transfer_bytes_histo5.attr,
263 &dev_attr_spi_controller_transfer_bytes_histo6.attr,
264 &dev_attr_spi_controller_transfer_bytes_histo7.attr,
265 &dev_attr_spi_controller_transfer_bytes_histo8.attr,
266 &dev_attr_spi_controller_transfer_bytes_histo9.attr,
267 &dev_attr_spi_controller_transfer_bytes_histo10.attr,
268 &dev_attr_spi_controller_transfer_bytes_histo11.attr,
269 &dev_attr_spi_controller_transfer_bytes_histo12.attr,
270 &dev_attr_spi_controller_transfer_bytes_histo13.attr,
271 &dev_attr_spi_controller_transfer_bytes_histo14.attr,
272 &dev_attr_spi_controller_transfer_bytes_histo15.attr,
273 &dev_attr_spi_controller_transfer_bytes_histo16.attr,
274 &dev_attr_spi_controller_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000275 NULL,
276};
277
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200278static const struct attribute_group spi_controller_statistics_group = {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000279 .name = "statistics",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200280 .attrs = spi_controller_statistics_attrs,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000281};
282
283static const struct attribute_group *spi_master_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200284 &spi_controller_statistics_group,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000285 NULL,
286};
287
288void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
289 struct spi_transfer *xfer,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200290 struct spi_controller *ctlr)
Martin Sperleca2ebc2015-06-22 13:00:36 +0000291{
292 unsigned long flags;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000293 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
294
295 if (l2len < 0)
296 l2len = 0;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000297
298 spin_lock_irqsave(&stats->lock, flags);
299
300 stats->transfers++;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000301 stats->transfer_bytes_histo[l2len]++;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000302
303 stats->bytes += xfer->len;
304 if ((xfer->tx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200305 (xfer->tx_buf != ctlr->dummy_tx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000306 stats->bytes_tx += xfer->len;
307 if ((xfer->rx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200308 (xfer->rx_buf != ctlr->dummy_rx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000309 stats->bytes_rx += xfer->len;
310
311 spin_unlock_irqrestore(&stats->lock, flags);
312}
313EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800314
315/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
316 * and the sysfs version makes coldplug work too.
317 */
318
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700319static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
320 const struct spi_device *sdev)
321{
322 while (id->name[0]) {
323 if (!strcmp(sdev->modalias, id->name))
324 return id;
325 id++;
326 }
327 return NULL;
328}
329
330const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
331{
332 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
333
334 return spi_match_id(sdrv->id_table, sdev);
335}
336EXPORT_SYMBOL_GPL(spi_get_device_id);
337
David Brownell8ae12a02006-01-08 13:34:19 -0800338static int spi_match_device(struct device *dev, struct device_driver *drv)
339{
340 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700341 const struct spi_driver *sdrv = to_spi_driver(drv);
342
Trent Piepho50395632018-09-20 19:18:32 +0000343 /* Check override first, and if set, only use the named driver */
344 if (spi->driver_override)
345 return strcmp(spi->driver_override, drv->name) == 0;
346
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600347 /* Attempt an OF style match */
348 if (of_driver_match_device(dev, drv))
349 return 1;
350
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100351 /* Then try ACPI */
352 if (acpi_driver_match_device(dev, drv))
353 return 1;
354
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700355 if (sdrv->id_table)
356 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800357
Kay Sievers35f74fc2009-01-06 10:44:37 -0800358 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800359}
360
Kay Sievers7eff2e72007-08-14 15:15:12 +0200361static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800362{
363 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800364 int rc;
365
366 rc = acpi_device_uevent_modalias(dev, env);
367 if (rc != -ENODEV)
368 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800369
Andy Shevchenko28566702017-07-26 16:14:00 +0300370 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800371}
372
David Brownell8ae12a02006-01-08 13:34:19 -0800373struct bus_type spi_bus_type = {
374 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700375 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800376 .match = spi_match_device,
377 .uevent = spi_uevent,
David Brownell8ae12a02006-01-08 13:34:19 -0800378};
379EXPORT_SYMBOL_GPL(spi_bus_type);
380
David Brownellb8852442006-01-08 13:34:23 -0800381
382static int spi_drv_probe(struct device *dev)
383{
384 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Jon Hunter44af7922015-10-09 15:45:55 +0100385 struct spi_device *spi = to_spi_device(dev);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300386 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800387
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200388 ret = of_clk_set_defaults(dev->of_node, false);
389 if (ret)
390 return ret;
391
Jon Hunter44af7922015-10-09 15:45:55 +0100392 if (dev->of_node) {
393 spi->irq = of_irq_get(dev->of_node, 0);
394 if (spi->irq == -EPROBE_DEFER)
395 return -EPROBE_DEFER;
396 if (spi->irq < 0)
397 spi->irq = 0;
398 }
399
Ulf Hansson676e7c22014-09-19 20:27:41 +0200400 ret = dev_pm_domain_attach(dev, true);
Ulf Hansson71f277a2018-04-26 10:53:10 +0200401 if (ret)
402 return ret;
403
Uwe Kleine-Königddbed6d2020-11-19 17:16:02 +0100404 if (sdrv->probe) {
405 ret = sdrv->probe(spi);
406 if (ret)
407 dev_pm_domain_detach(dev, true);
408 }
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300409
410 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800411}
412
413static int spi_drv_remove(struct device *dev)
414{
415 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Uwe Kleine-Königddbed6d2020-11-19 17:16:02 +0100416 int ret = 0;
David Brownellb8852442006-01-08 13:34:23 -0800417
Uwe Kleine-Königddbed6d2020-11-19 17:16:02 +0100418 if (sdrv->remove)
419 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200420 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300421
422 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800423}
424
425static void spi_drv_shutdown(struct device *dev)
426{
427 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
428
429 sdrv->shutdown(to_spi_device(dev));
430}
431
David Brownell33e34dc2007-05-08 00:32:21 -0700432/**
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500433 * __spi_register_driver - register a SPI driver
Thierry Reding88c93212015-11-10 13:03:04 +0100434 * @owner: owner module of the driver to register
David Brownell33e34dc2007-05-08 00:32:21 -0700435 * @sdrv: the driver to register
436 * Context: can sleep
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200437 *
438 * Return: zero on success, else a negative error code.
David Brownell33e34dc2007-05-08 00:32:21 -0700439 */
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500440int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
David Brownellb8852442006-01-08 13:34:23 -0800441{
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500442 sdrv->driver.owner = owner;
David Brownellb8852442006-01-08 13:34:23 -0800443 sdrv->driver.bus = &spi_bus_type;
Uwe Kleine-Königddbed6d2020-11-19 17:16:02 +0100444 sdrv->driver.probe = spi_drv_probe;
445 sdrv->driver.remove = spi_drv_remove;
David Brownellb8852442006-01-08 13:34:23 -0800446 if (sdrv->shutdown)
447 sdrv->driver.shutdown = spi_drv_shutdown;
448 return driver_register(&sdrv->driver);
449}
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500450EXPORT_SYMBOL_GPL(__spi_register_driver);
David Brownellb8852442006-01-08 13:34:23 -0800451
David Brownell8ae12a02006-01-08 13:34:19 -0800452/*-------------------------------------------------------------------------*/
453
454/* SPI devices should normally not be created by SPI device drivers; that
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200455 * would make them board-specific. Similarly with SPI controller drivers.
David Brownell8ae12a02006-01-08 13:34:19 -0800456 * Device registration normally goes into like arch/.../mach.../board-YYY.c
457 * with other readonly (flashable) information about mainboard devices.
458 */
459
460struct boardinfo {
461 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800462 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800463};
464
465static LIST_HEAD(board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200466static LIST_HEAD(spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800467
468/*
Andy Shevchenkobe73e322019-10-23 15:16:43 +0300469 * Used to protect add/del operation for board_info list and
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200470 * spi_controller list, and their matching process
Suniel Mahesh9a9a0472017-08-17 18:18:22 +0530471 * also used to protect object of type struct idr
Feng Tang2b9603a2010-08-02 15:52:15 +0800472 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700473static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800474
Lukas Wunnerddf75be2020-08-03 13:09:01 +0200475/*
476 * Prevents addition of devices with same chip select and
477 * addition of devices below an unregistering controller.
478 */
479static DEFINE_MUTEX(spi_add_lock);
480
Grant Likelydc87c982008-05-15 16:50:22 -0600481/**
482 * spi_alloc_device - Allocate a new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200483 * @ctlr: Controller to which device is connected
Grant Likelydc87c982008-05-15 16:50:22 -0600484 * Context: can sleep
485 *
486 * Allows a driver to allocate and initialize a spi_device without
487 * registering it immediately. This allows a driver to directly
488 * fill the spi_device with device parameters before calling
489 * spi_add_device() on it.
490 *
491 * Caller is responsible to call spi_add_device() on the returned
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200492 * spi_device structure to add it to the SPI controller. If the caller
Grant Likelydc87c982008-05-15 16:50:22 -0600493 * needs to discard the spi_device without adding it, then it should
494 * call spi_dev_put() on it.
495 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200496 * Return: a pointer to the new device, or NULL.
Grant Likelydc87c982008-05-15 16:50:22 -0600497 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200498struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
Grant Likelydc87c982008-05-15 16:50:22 -0600499{
500 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600501
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200502 if (!spi_controller_get(ctlr))
Grant Likelydc87c982008-05-15 16:50:22 -0600503 return NULL;
504
Jingoo Han5fe5f052013-10-14 10:31:51 +0900505 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600506 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200507 spi_controller_put(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600508 return NULL;
509 }
510
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200511 spi->master = spi->controller = ctlr;
512 spi->dev.parent = &ctlr->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600513 spi->dev.bus = &spi_bus_type;
514 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100515 spi->cs_gpio = -ENOENT;
John Garryea235782020-02-28 23:18:49 +0800516 spi->mode = ctlr->buswidth_override_bits;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000517
518 spin_lock_init(&spi->statistics.lock);
519
Grant Likelydc87c982008-05-15 16:50:22 -0600520 device_initialize(&spi->dev);
521 return spi;
522}
523EXPORT_SYMBOL_GPL(spi_alloc_device);
524
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200525static void spi_dev_set_name(struct spi_device *spi)
526{
527 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
528
529 if (adev) {
530 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
531 return;
532 }
533
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200534 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200535 spi->chip_select);
536}
537
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200538static int spi_dev_check(struct device *dev, void *data)
539{
540 struct spi_device *spi = to_spi_device(dev);
541 struct spi_device *new_spi = data;
542
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200543 if (spi->controller == new_spi->controller &&
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200544 spi->chip_select == new_spi->chip_select)
545 return -EBUSY;
546 return 0;
547}
548
Saravana Kannan4b8b7bc2021-04-26 16:56:38 -0700549static void spi_cleanup(struct spi_device *spi)
550{
551 if (spi->controller->cleanup)
552 spi->controller->cleanup(spi);
553}
554
Grant Likelydc87c982008-05-15 16:50:22 -0600555/**
556 * spi_add_device - Add spi_device allocated with spi_alloc_device
557 * @spi: spi_device to register
558 *
559 * Companion function to spi_alloc_device. Devices allocated with
560 * spi_alloc_device can be added onto the spi bus with this function.
561 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200562 * Return: 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600563 */
564int spi_add_device(struct spi_device *spi)
565{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200566 struct spi_controller *ctlr = spi->controller;
567 struct device *dev = ctlr->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600568 int status;
569
570 /* Chipselects are numbered 0..max; validate. */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200571 if (spi->chip_select >= ctlr->num_chipselect) {
572 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
573 ctlr->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600574 return -EINVAL;
575 }
576
577 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200578 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700579
580 /* We need to make sure there's no other device with this
581 * chipselect **BEFORE** we call setup(), else we'll trash
582 * its configuration. Lock against concurrent add() calls.
583 */
584 mutex_lock(&spi_add_lock);
585
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200586 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
587 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700588 dev_err(dev, "chipselect %d already in use\n",
589 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700590 goto done;
591 }
592
Lukas Wunnerddf75be2020-08-03 13:09:01 +0200593 /* Controller may unregister concurrently */
594 if (IS_ENABLED(CONFIG_SPI_DYNAMIC) &&
595 !device_is_registered(&ctlr->dev)) {
596 status = -ENODEV;
597 goto done;
598 }
599
Linus Walleijf3186dd2019-01-07 16:51:50 +0100600 /* Descriptors take precedence */
601 if (ctlr->cs_gpiods)
602 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
603 else if (ctlr->cs_gpios)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200604 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100605
David Brownelle48880e2008-08-15 00:40:44 -0700606 /* Drivers may modify this initial i/o setup, but will
607 * normally rely on the device being setup. Devices
608 * using SPI_CS_HIGH can't coexist well otherwise...
609 */
David Brownell7d077192009-06-17 16:26:03 -0700610 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600611 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200612 dev_err(dev, "can't setup %s, status %d\n",
613 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700614 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600615 }
616
David Brownelle48880e2008-08-15 00:40:44 -0700617 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600618 status = device_add(&spi->dev);
Saravana Kannan4b8b7bc2021-04-26 16:56:38 -0700619 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200620 dev_err(dev, "can't add %s, status %d\n",
621 dev_name(&spi->dev), status);
Saravana Kannan4b8b7bc2021-04-26 16:56:38 -0700622 spi_cleanup(spi);
623 } else {
Kay Sievers35f74fc2009-01-06 10:44:37 -0800624 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Saravana Kannan4b8b7bc2021-04-26 16:56:38 -0700625 }
Grant Likelydc87c982008-05-15 16:50:22 -0600626
David Brownelle48880e2008-08-15 00:40:44 -0700627done:
628 mutex_unlock(&spi_add_lock);
629 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600630}
631EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800632
David Brownell33e34dc2007-05-08 00:32:21 -0700633/**
634 * spi_new_device - instantiate one new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200635 * @ctlr: Controller to which device is connected
David Brownell33e34dc2007-05-08 00:32:21 -0700636 * @chip: Describes the SPI device
637 * Context: can sleep
638 *
639 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800640 * after board init creates the hard-wired devices. Some development
641 * platforms may not be able to use spi_register_board_info though, and
642 * this is exported so that for example a USB or parport based adapter
643 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700644 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200645 * Return: the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800646 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200647struct spi_device *spi_new_device(struct spi_controller *ctlr,
Adrian Bunke9d5a462007-03-26 21:32:23 -0800648 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800649{
650 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800651 int status;
652
David Brownell082c8cb2007-07-31 00:39:45 -0700653 /* NOTE: caller did any chip->bus_num checks necessary.
654 *
655 * Also, unless we change the return value convention to use
656 * error-or-pointer (not NULL-or-pointer), troubleshootability
657 * suggests syslogged diagnostics are best here (ugh).
658 */
659
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200660 proxy = spi_alloc_device(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600661 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800662 return NULL;
663
Grant Likely102eb972008-07-23 21:29:55 -0700664 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
665
David Brownell8ae12a02006-01-08 13:34:19 -0800666 proxy->chip_select = chip->chip_select;
667 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700668 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800669 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700670 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800671 proxy->dev.platform_data = (void *) chip->platform_data;
672 proxy->controller_data = chip->controller_data;
673 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800674
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800675 if (chip->properties) {
676 status = device_add_properties(&proxy->dev, chip->properties);
677 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200678 dev_err(&ctlr->dev,
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800679 "failed to add properties to '%s': %d\n",
680 chip->modalias, status);
681 goto err_dev_put;
682 }
David Brownell8ae12a02006-01-08 13:34:19 -0800683 }
684
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800685 status = spi_add_device(proxy);
686 if (status < 0)
687 goto err_remove_props;
688
David Brownell8ae12a02006-01-08 13:34:19 -0800689 return proxy;
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800690
691err_remove_props:
692 if (chip->properties)
693 device_remove_properties(&proxy->dev);
694err_dev_put:
695 spi_dev_put(proxy);
696 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800697}
698EXPORT_SYMBOL_GPL(spi_new_device);
699
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100700/**
701 * spi_unregister_device - unregister a single SPI device
702 * @spi: spi_device to unregister
703 *
704 * Start making the passed SPI device vanish. Normally this would be handled
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200705 * by spi_unregister_controller().
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100706 */
707void spi_unregister_device(struct spi_device *spi)
708{
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100709 if (!spi)
710 return;
711
Johan Hovold83241472017-01-30 17:47:05 +0100712 if (spi->dev.of_node) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100713 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
Johan Hovold83241472017-01-30 17:47:05 +0100714 of_node_put(spi->dev.of_node);
715 }
Octavian Purdila7f244672016-07-08 19:13:11 +0300716 if (ACPI_COMPANION(&spi->dev))
717 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
Saravana Kannan0c4d4de2021-05-05 09:47:34 -0700718 device_del(&spi->dev);
719 spi_cleanup(spi);
720 put_device(&spi->dev);
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100721}
722EXPORT_SYMBOL_GPL(spi_unregister_device);
723
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200724static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
725 struct spi_board_info *bi)
Feng Tang2b9603a2010-08-02 15:52:15 +0800726{
727 struct spi_device *dev;
728
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200729 if (ctlr->bus_num != bi->bus_num)
Feng Tang2b9603a2010-08-02 15:52:15 +0800730 return;
731
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200732 dev = spi_new_device(ctlr, bi);
Feng Tang2b9603a2010-08-02 15:52:15 +0800733 if (!dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200734 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
Feng Tang2b9603a2010-08-02 15:52:15 +0800735 bi->modalias);
736}
737
David Brownell33e34dc2007-05-08 00:32:21 -0700738/**
739 * spi_register_board_info - register SPI devices for a given board
740 * @info: array of chip descriptors
741 * @n: how many descriptors are provided
742 * Context: can sleep
743 *
David Brownell8ae12a02006-01-08 13:34:19 -0800744 * Board-specific early init code calls this (probably during arch_initcall)
745 * with segments of the SPI device table. Any device nodes are created later,
746 * after the relevant parent SPI controller (bus_num) is defined. We keep
747 * this table of devices forever, so that reloading a controller driver will
748 * not make Linux forget about these hard-wired devices.
749 *
750 * Other code can also call this, e.g. a particular add-on board might provide
751 * SPI devices through its expansion connector, so code initializing that board
752 * would naturally declare its SPI devices.
753 *
754 * The board info passed can safely be __initdata ... but be careful of
755 * any embedded pointers (platform_data, etc), they're copied as-is.
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800756 * Device properties are deep-copied though.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200757 *
758 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -0800759 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000760int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800761{
Feng Tang2b9603a2010-08-02 15:52:15 +0800762 struct boardinfo *bi;
763 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800764
Xiubo Lic7908a32014-09-24 14:30:29 +0800765 if (!n)
Dmitry Torokhovf974cf52017-02-28 14:25:19 -0800766 return 0;
Xiubo Lic7908a32014-09-24 14:30:29 +0800767
Markus Elfringf9bdb7f2017-01-13 12:28:04 +0100768 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800769 if (!bi)
770 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800771
Feng Tang2b9603a2010-08-02 15:52:15 +0800772 for (i = 0; i < n; i++, bi++, info++) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200773 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -0800774
Feng Tang2b9603a2010-08-02 15:52:15 +0800775 memcpy(&bi->board_info, info, sizeof(*info));
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800776 if (info->properties) {
777 bi->board_info.properties =
778 property_entries_dup(info->properties);
779 if (IS_ERR(bi->board_info.properties))
780 return PTR_ERR(bi->board_info.properties);
781 }
782
Feng Tang2b9603a2010-08-02 15:52:15 +0800783 mutex_lock(&board_lock);
784 list_add_tail(&bi->list, &board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200785 list_for_each_entry(ctlr, &spi_controller_list, list)
786 spi_match_controller_to_boardinfo(ctlr,
787 &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +0800788 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800789 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800790
791 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800792}
793
794/*-------------------------------------------------------------------------*/
795
David Bauerc5aa9ea2021-04-16 21:59:56 +0200796static void spi_set_cs(struct spi_device *spi, bool enable, bool force)
Mark Brownb1589352013-10-05 11:50:40 +0100797{
Alexandru Ardelean25093bd2019-09-26 13:51:43 +0300798 bool enable1 = enable;
799
Douglas Andersond40f0b62020-06-29 16:41:06 -0700800 /*
801 * Avoid calling into the driver (or doing delays) if the chip select
802 * isn't actually changing from the last time this was called.
803 */
David Bauerc5aa9ea2021-04-16 21:59:56 +0200804 if (!force && (spi->controller->last_cs_enable == enable) &&
Douglas Andersond40f0b62020-06-29 16:41:06 -0700805 (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH)))
806 return;
807
808 spi->controller->last_cs_enable = enable;
809 spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH;
810
Alexandru Ardelean25093bd2019-09-26 13:51:43 +0300811 if (!spi->controller->set_cs_timing) {
812 if (enable1)
813 spi_delay_exec(&spi->controller->cs_setup, NULL);
814 else
815 spi_delay_exec(&spi->controller->cs_hold, NULL);
816 }
817
Mark Brownb1589352013-10-05 11:50:40 +0100818 if (spi->mode & SPI_CS_HIGH)
819 enable = !enable;
820
Linus Walleijf3186dd2019-01-07 16:51:50 +0100821 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
Linus Walleijf3186dd2019-01-07 16:51:50 +0100822 if (!(spi->mode & SPI_NO_CS)) {
Andy Shevchenkof5da0822021-05-11 17:09:12 +0300823 if (spi->cs_gpiod) {
824 /*
825 * Historically ACPI has no means of the GPIO polarity and
826 * thus the SPISerialBus() resource defines it on the per-chip
827 * basis. In order to avoid a chain of negations, the GPIO
828 * polarity is considered being Active High. Even for the cases
829 * when _DSD() is involved (in the updated versions of ACPI)
830 * the GPIO CS polarity must be defined Active High to avoid
831 * ambiguity. That's why we use enable, that takes SPI_CS_HIGH
832 * into account.
833 */
834 if (has_acpi_companion(&spi->dev))
835 gpiod_set_value_cansleep(spi->cs_gpiod, !enable);
836 else
837 /* Polarity handled by GPIO library */
838 gpiod_set_value_cansleep(spi->cs_gpiod, enable1);
839 } else {
Sven Van Asbroeck766c6b62020-11-06 10:07:06 -0500840 /*
841 * invert the enable line, as active low is
842 * default for SPI.
843 */
Felix Fietkau28f76042019-02-10 00:38:25 +0100844 gpio_set_value_cansleep(spi->cs_gpio, !enable);
Andy Shevchenkof5da0822021-05-11 17:09:12 +0300845 }
Linus Walleijf3186dd2019-01-07 16:51:50 +0100846 }
Thor Thayer8eee6b92016-10-10 09:25:24 -0500847 /* Some SPI masters need both GPIO CS & slave_select */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200848 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
849 spi->controller->set_cs)
850 spi->controller->set_cs(spi, !enable);
851 } else if (spi->controller->set_cs) {
852 spi->controller->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500853 }
Alexandru Ardelean25093bd2019-09-26 13:51:43 +0300854
855 if (!spi->controller->set_cs_timing) {
856 if (!enable1)
857 spi_delay_exec(&spi->controller->cs_inactive, NULL);
858 }
Mark Brownb1589352013-10-05 11:50:40 +0100859}
860
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200861#ifdef CONFIG_HAS_DMA
Boris Brezillon46336962018-04-22 20:35:14 +0200862int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
863 struct sg_table *sgt, void *buf, size_t len,
864 enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000865{
866 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200867 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530868#ifdef CONFIG_HIGHMEM
869 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
870 (unsigned long)buf < (PKMAP_BASE +
871 (LAST_PKMAP * PAGE_SIZE)));
872#else
873 const bool kmap_buf = false;
874#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500875 int desc_len;
876 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000877 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600878 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000879 void *sg_buf;
880 size_t min;
881 int i, ret;
882
Vignesh Rb1b81532016-08-17 15:22:36 +0530883 if (vmalloced_buf || kmap_buf) {
Biju Das21680aa2022-03-16 17:53:17 +0000884 desc_len = min_t(unsigned long, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500885 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530886 } else if (virt_addr_valid(buf)) {
Biju Das21680aa2022-03-16 17:53:17 +0000887 desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500888 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530889 } else {
890 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500891 }
892
Mark Brown6ad45a22014-02-02 13:47:47 +0000893 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
894 if (ret != 0)
895 return ret;
896
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600897 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000898 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000899
Vignesh Rb1b81532016-08-17 15:22:36 +0530900 if (vmalloced_buf || kmap_buf) {
Maxime Chevallierce993192018-03-02 15:55:09 +0100901 /*
902 * Next scatterlist entry size is the minimum between
903 * the desc_len and the remaining buffer length that
904 * fits in a page.
905 */
906 min = min_t(size_t, desc_len,
907 min_t(size_t, len,
908 PAGE_SIZE - offset_in_page(buf)));
Vignesh Rb1b81532016-08-17 15:22:36 +0530909 if (vmalloced_buf)
910 vm_page = vmalloc_to_page(buf);
911 else
912 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000913 if (!vm_page) {
914 sg_free_table(sgt);
915 return -ENOMEM;
916 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600917 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000918 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000919 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500920 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000921 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600922 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000923 }
924
Mark Brown6ad45a22014-02-02 13:47:47 +0000925 buf += min;
926 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600927 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000928 }
929
930 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200931 if (!ret)
932 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000933 if (ret < 0) {
934 sg_free_table(sgt);
935 return ret;
936 }
937
938 sgt->nents = ret;
939
940 return 0;
941}
942
Boris Brezillon46336962018-04-22 20:35:14 +0200943void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
944 struct sg_table *sgt, enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000945{
946 if (sgt->orig_nents) {
947 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
948 sg_free_table(sgt);
949 }
950}
951
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200952static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000953{
Mark Brown99adef32014-01-16 12:22:43 +0000954 struct device *tx_dev, *rx_dev;
955 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000956 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000957
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200958 if (!ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000959 return 0;
960
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200961 if (ctlr->dma_tx)
962 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800963 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200964 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800965
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200966 if (ctlr->dma_rx)
967 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800968 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200969 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000970
971 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200972 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000973 continue;
974
975 if (xfer->tx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200976 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000977 (void *)xfer->tx_buf, xfer->len,
978 DMA_TO_DEVICE);
979 if (ret != 0)
980 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000981 }
982
983 if (xfer->rx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200984 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000985 xfer->rx_buf, xfer->len,
986 DMA_FROM_DEVICE);
987 if (ret != 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200988 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000989 DMA_TO_DEVICE);
990 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000991 }
992 }
993 }
994
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200995 ctlr->cur_msg_mapped = true;
Mark Brown99adef32014-01-16 12:22:43 +0000996
997 return 0;
998}
999
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001000static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +00001001{
1002 struct spi_transfer *xfer;
1003 struct device *tx_dev, *rx_dev;
1004
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001005 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +00001006 return 0;
1007
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001008 if (ctlr->dma_tx)
1009 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +08001010 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001011 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +08001012
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001013 if (ctlr->dma_rx)
1014 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +08001015 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001016 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +00001017
1018 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001019 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +00001020 continue;
1021
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001022 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
1023 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +00001024 }
1025
Robin Gong809b1b02020-06-17 06:42:08 +08001026 ctlr->cur_msg_mapped = false;
1027
Mark Brown99adef32014-01-16 12:22:43 +00001028 return 0;
1029}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001030#else /* !CONFIG_HAS_DMA */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001031static inline int __spi_map_msg(struct spi_controller *ctlr,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001032 struct spi_message *msg)
1033{
1034 return 0;
1035}
1036
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001037static inline int __spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +00001038 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001039{
1040 return 0;
1041}
1042#endif /* !CONFIG_HAS_DMA */
1043
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001044static inline int spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +00001045 struct spi_message *msg)
1046{
1047 struct spi_transfer *xfer;
1048
1049 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1050 /*
1051 * Restore the original value of tx_buf or rx_buf if they are
1052 * NULL.
1053 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001054 if (xfer->tx_buf == ctlr->dummy_tx)
Martin Sperl4b786452015-05-25 10:13:10 +00001055 xfer->tx_buf = NULL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001056 if (xfer->rx_buf == ctlr->dummy_rx)
Martin Sperl4b786452015-05-25 10:13:10 +00001057 xfer->rx_buf = NULL;
1058 }
1059
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001060 return __spi_unmap_msg(ctlr, msg);
Martin Sperl4b786452015-05-25 10:13:10 +00001061}
1062
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001063static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001064{
1065 struct spi_transfer *xfer;
1066 void *tmp;
1067 unsigned int max_tx, max_rx;
1068
dillon minaee67fe2020-05-25 11:45:48 +08001069 if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX))
1070 && !(msg->spi->mode & SPI_3WIRE)) {
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001071 max_tx = 0;
1072 max_rx = 0;
1073
1074 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001075 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001076 !xfer->tx_buf)
1077 max_tx = max(xfer->len, max_tx);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001078 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001079 !xfer->rx_buf)
1080 max_rx = max(xfer->len, max_rx);
1081 }
1082
1083 if (max_tx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001084 tmp = krealloc(ctlr->dummy_tx, max_tx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001085 GFP_KERNEL | GFP_DMA);
1086 if (!tmp)
1087 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001088 ctlr->dummy_tx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001089 memset(tmp, 0, max_tx);
1090 }
1091
1092 if (max_rx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001093 tmp = krealloc(ctlr->dummy_rx, max_rx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001094 GFP_KERNEL | GFP_DMA);
1095 if (!tmp)
1096 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001097 ctlr->dummy_rx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001098 }
1099
1100 if (max_tx || max_rx) {
1101 list_for_each_entry(xfer, &msg->transfers,
1102 transfer_list) {
Chris Lesiak5442dca2019-03-07 20:39:00 +00001103 if (!xfer->len)
1104 continue;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001105 if (!xfer->tx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001106 xfer->tx_buf = ctlr->dummy_tx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001107 if (!xfer->rx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001108 xfer->rx_buf = ctlr->dummy_rx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001109 }
1110 }
1111 }
1112
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001113 return __spi_map_msg(ctlr, msg);
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001114}
Mark Brown99adef32014-01-16 12:22:43 +00001115
Lubomir Rintel810923f2018-11-13 11:22:24 +01001116static int spi_transfer_wait(struct spi_controller *ctlr,
1117 struct spi_message *msg,
1118 struct spi_transfer *xfer)
1119{
1120 struct spi_statistics *statm = &ctlr->statistics;
1121 struct spi_statistics *stats = &msg->spi->statistics;
Xu Yilun501e1872021-01-04 09:29:09 +08001122 u32 speed_hz = xfer->speed_hz;
Colin Ian King49686df2020-04-10 13:23:15 +01001123 unsigned long long ms;
Lubomir Rintel810923f2018-11-13 11:22:24 +01001124
1125 if (spi_controller_is_slave(ctlr)) {
1126 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1127 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1128 return -EINTR;
1129 }
1130 } else {
Xu Yilun501e1872021-01-04 09:29:09 +08001131 if (!speed_hz)
1132 speed_hz = 100000;
1133
Lubomir Rintel810923f2018-11-13 11:22:24 +01001134 ms = 8LL * 1000LL * xfer->len;
Xu Yilun501e1872021-01-04 09:29:09 +08001135 do_div(ms, speed_hz);
Lubomir Rintel810923f2018-11-13 11:22:24 +01001136 ms += ms + 200; /* some tolerance */
1137
1138 if (ms > UINT_MAX)
1139 ms = UINT_MAX;
1140
1141 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1142 msecs_to_jiffies(ms));
1143
1144 if (ms == 0) {
1145 SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1146 SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1147 dev_err(&msg->spi->dev,
1148 "SPI transfer timed out\n");
1149 return -ETIMEDOUT;
1150 }
1151 }
1152
1153 return 0;
1154}
1155
Martin Sperl0ff2de82019-02-23 08:49:48 +00001156static void _spi_transfer_delay_ns(u32 ns)
1157{
1158 if (!ns)
1159 return;
1160 if (ns <= 1000) {
1161 ndelay(ns);
1162 } else {
1163 u32 us = DIV_ROUND_UP(ns, 1000);
1164
1165 if (us <= 10)
1166 udelay(us);
1167 else
1168 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1169 }
1170}
1171
Alexandru Ardelean3984d392019-09-26 13:51:44 +03001172int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
Martin Sperl0ff2de82019-02-23 08:49:48 +00001173{
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001174 u32 delay = _delay->value;
1175 u32 unit = _delay->unit;
Martin Sperld5864e52019-02-23 08:49:50 +00001176 u32 hz;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001177
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001178 if (!delay)
1179 return 0;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001180
1181 switch (unit) {
1182 case SPI_DELAY_UNIT_USECS:
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001183 delay *= 1000;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001184 break;
1185 case SPI_DELAY_UNIT_NSECS: /* nothing to do here */
1186 break;
Martin Sperld5864e52019-02-23 08:49:50 +00001187 case SPI_DELAY_UNIT_SCK:
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001188 /* clock cycles need to be obtained from spi_transfer */
1189 if (!xfer)
1190 return -EINVAL;
Martin Sperld5864e52019-02-23 08:49:50 +00001191 /* if there is no effective speed know, then approximate
1192 * by underestimating with half the requested hz
1193 */
1194 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001195 if (!hz)
1196 return -EINVAL;
Martin Sperld5864e52019-02-23 08:49:50 +00001197 delay *= DIV_ROUND_UP(1000000000, hz);
1198 break;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001199 default:
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001200 return -EINVAL;
1201 }
1202
1203 return delay;
1204}
Alexandru Ardelean3984d392019-09-26 13:51:44 +03001205EXPORT_SYMBOL_GPL(spi_delay_to_ns);
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001206
1207int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
1208{
1209 int delay;
1210
Mark Brown8fede892020-05-22 16:50:05 +01001211 might_sleep();
1212
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001213 if (!_delay)
1214 return -EINVAL;
1215
Alexandru Ardelean3984d392019-09-26 13:51:44 +03001216 delay = spi_delay_to_ns(_delay, xfer);
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001217 if (delay < 0)
1218 return delay;
1219
1220 _spi_transfer_delay_ns(delay);
1221
1222 return 0;
1223}
1224EXPORT_SYMBOL_GPL(spi_delay_exec);
1225
Martin Sperl0ff2de82019-02-23 08:49:48 +00001226static void _spi_transfer_cs_change_delay(struct spi_message *msg,
1227 struct spi_transfer *xfer)
1228{
Alexandru Ardelean329f0da2019-09-26 13:51:31 +03001229 u32 delay = xfer->cs_change_delay.value;
1230 u32 unit = xfer->cs_change_delay.unit;
1231 int ret;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001232
1233 /* return early on "fast" mode - for everything but USECS */
Alexandru Ardelean6b3f2362019-09-26 13:51:29 +03001234 if (!delay) {
1235 if (unit == SPI_DELAY_UNIT_USECS)
1236 _spi_transfer_delay_ns(10000);
Martin Sperl0ff2de82019-02-23 08:49:48 +00001237 return;
Alexandru Ardelean6b3f2362019-09-26 13:51:29 +03001238 }
Martin Sperl0ff2de82019-02-23 08:49:48 +00001239
Alexandru Ardelean329f0da2019-09-26 13:51:31 +03001240 ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1241 if (ret) {
Martin Sperl0ff2de82019-02-23 08:49:48 +00001242 dev_err_once(&msg->spi->dev,
1243 "Use of unsupported delay unit %i, using default of 10us\n",
Alexandru Ardelean329f0da2019-09-26 13:51:31 +03001244 unit);
1245 _spi_transfer_delay_ns(10000);
Martin Sperl0ff2de82019-02-23 08:49:48 +00001246 }
Martin Sperl0ff2de82019-02-23 08:49:48 +00001247}
1248
Mark Brownb1589352013-10-05 11:50:40 +01001249/*
1250 * spi_transfer_one_message - Default implementation of transfer_one_message()
1251 *
1252 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -07001253 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +01001254 * standard handling of delays and chip select management.
1255 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001256static int spi_transfer_one_message(struct spi_controller *ctlr,
Mark Brownb1589352013-10-05 11:50:40 +01001257 struct spi_message *msg)
1258{
1259 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +01001260 bool keep_cs = false;
1261 int ret = 0;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001262 struct spi_statistics *statm = &ctlr->statistics;
Martin Sperleca2ebc2015-06-22 13:00:36 +00001263 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +01001264
David Bauerc5aa9ea2021-04-16 21:59:56 +02001265 spi_set_cs(msg->spi, true, false);
Mark Brownb1589352013-10-05 11:50:40 +01001266
Martin Sperleca2ebc2015-06-22 13:00:36 +00001267 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1268 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1269
Mark Brownb1589352013-10-05 11:50:40 +01001270 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1271 trace_spi_transfer_start(msg, xfer);
1272
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001273 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1274 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
Martin Sperleca2ebc2015-06-22 13:00:36 +00001275
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001276 if (!ctlr->ptp_sts_supported) {
1277 xfer->ptp_sts_word_pre = 0;
1278 ptp_read_system_prets(xfer->ptp_sts);
1279 }
1280
Nicolas Saenz Julienne86f5b092021-02-11 19:08:20 +01001281 if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001282 reinit_completion(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001283
Robin Gong809b1b02020-06-17 06:42:08 +08001284fallback_pio:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001285 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
Mark Brown38ec10f2014-08-16 16:27:41 +01001286 if (ret < 0) {
Robin Gong809b1b02020-06-17 06:42:08 +08001287 if (ctlr->cur_msg_mapped &&
1288 (xfer->error & SPI_TRANS_FAIL_NO_START)) {
1289 __spi_unmap_msg(ctlr, msg);
1290 ctlr->fallback = true;
1291 xfer->error &= ~SPI_TRANS_FAIL_NO_START;
1292 goto fallback_pio;
1293 }
1294
Martin Sperleca2ebc2015-06-22 13:00:36 +00001295 SPI_STATISTICS_INCREMENT_FIELD(statm,
1296 errors);
1297 SPI_STATISTICS_INCREMENT_FIELD(stats,
1298 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001299 dev_err(&msg->spi->dev,
1300 "SPI transfer failed: %d\n", ret);
1301 goto out;
1302 }
Mark Brownb1589352013-10-05 11:50:40 +01001303
Mark Brownd57e7962018-11-15 16:08:32 -08001304 if (ret > 0) {
1305 ret = spi_transfer_wait(ctlr, msg, xfer);
1306 if (ret < 0)
1307 msg->status = ret;
1308 }
Mark Brown38ec10f2014-08-16 16:27:41 +01001309 } else {
1310 if (xfer->len)
1311 dev_err(&msg->spi->dev,
1312 "Bufferless transfer has length %u\n",
1313 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001314 }
Mark Brownb1589352013-10-05 11:50:40 +01001315
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001316 if (!ctlr->ptp_sts_supported) {
1317 ptp_read_system_postts(xfer->ptp_sts);
1318 xfer->ptp_sts_word_post = xfer->len;
1319 }
1320
Mark Brownb1589352013-10-05 11:50:40 +01001321 trace_spi_transfer_stop(msg, xfer);
1322
1323 if (msg->status != -EINPROGRESS)
1324 goto out;
1325
Alexandru Ardeleanbebcfd22019-09-26 13:51:36 +03001326 spi_transfer_delay_exec(xfer);
Mark Brownb1589352013-10-05 11:50:40 +01001327
1328 if (xfer->cs_change) {
1329 if (list_is_last(&xfer->transfer_list,
1330 &msg->transfers)) {
1331 keep_cs = true;
1332 } else {
David Bauerc5aa9ea2021-04-16 21:59:56 +02001333 spi_set_cs(msg->spi, false, false);
Martin Sperl0ff2de82019-02-23 08:49:48 +00001334 _spi_transfer_cs_change_delay(msg, xfer);
David Bauerc5aa9ea2021-04-16 21:59:56 +02001335 spi_set_cs(msg->spi, true, false);
Mark Brownb1589352013-10-05 11:50:40 +01001336 }
1337 }
1338
1339 msg->actual_length += xfer->len;
1340 }
1341
1342out:
1343 if (ret != 0 || !keep_cs)
David Bauerc5aa9ea2021-04-16 21:59:56 +02001344 spi_set_cs(msg->spi, false, false);
Mark Brownb1589352013-10-05 11:50:40 +01001345
1346 if (msg->status == -EINPROGRESS)
1347 msg->status = ret;
1348
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001349 if (msg->status && ctlr->handle_err)
1350 ctlr->handle_err(ctlr, msg);
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001351
Mark Brown0ed56252019-05-09 11:27:17 +09001352 spi_finalize_current_message(ctlr);
1353
Mark Brownb1589352013-10-05 11:50:40 +01001354 return ret;
1355}
1356
1357/**
1358 * spi_finalize_current_transfer - report completion of a transfer
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001359 * @ctlr: the controller reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001360 *
1361 * Called by SPI drivers using the core transfer_one_message()
1362 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001363 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001364 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001365void spi_finalize_current_transfer(struct spi_controller *ctlr)
Mark Brownb1589352013-10-05 11:50:40 +01001366{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001367 complete(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001368}
1369EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1370
Mark Browne1268592020-07-15 17:36:10 +01001371static void spi_idle_runtime_pm(struct spi_controller *ctlr)
1372{
1373 if (ctlr->auto_runtime_pm) {
1374 pm_runtime_mark_last_busy(ctlr->dev.parent);
1375 pm_runtime_put_autosuspend(ctlr->dev.parent);
1376 }
1377}
1378
Linus Walleijffbbdd212012-02-22 10:05:38 +01001379/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001380 * __spi_pump_messages - function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001381 * @ctlr: controller to process queue for
Mark Brownfc9e0f72014-12-10 13:46:33 +00001382 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001383 *
1384 * This function checks if there is any spi message in the queue that
1385 * needs processing and if so call out to the driver to initialize hardware
1386 * and transfer each message.
1387 *
Mark Brown0461a412014-12-09 21:38:05 +00001388 * Note that it is called both from the kthread itself and also from
1389 * inside spi_sync(); the queue extraction handling at the top of the
1390 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001391 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001392static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001393{
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001394 struct spi_transfer *xfer;
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001395 struct spi_message *msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001396 bool was_busy = false;
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001397 unsigned long flags;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001398 int ret;
1399
Mark Brown983aee52014-12-09 19:46:56 +00001400 /* Lock queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001401 spin_lock_irqsave(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001402
1403 /* Make sure we are not already running a message */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001404 if (ctlr->cur_msg) {
1405 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001406 return;
1407 }
1408
Mark Brownf0125f12019-01-23 17:29:53 +00001409 /* If another context is idling the device then defer */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001410 if (ctlr->idling) {
Marek Szyprowski60a883d2020-07-09 08:50:07 +02001411 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001412 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001413 return;
1414 }
1415
Mark Brown983aee52014-12-09 19:46:56 +00001416 /* Check if the queue is idle */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001417 if (list_empty(&ctlr->queue) || !ctlr->running) {
1418 if (!ctlr->busy) {
1419 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -07001420 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001421 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001422
Mark Browne1268592020-07-15 17:36:10 +01001423 /* Defer any non-atomic teardown to the thread */
Mark Brownf0125f12019-01-23 17:29:53 +00001424 if (!in_kthread) {
Mark Browne1268592020-07-15 17:36:10 +01001425 if (!ctlr->dummy_rx && !ctlr->dummy_tx &&
1426 !ctlr->unprepare_transfer_hardware) {
1427 spi_idle_runtime_pm(ctlr);
1428 ctlr->busy = false;
1429 trace_spi_controller_idle(ctlr);
1430 } else {
1431 kthread_queue_work(ctlr->kworker,
1432 &ctlr->pump_messages);
1433 }
Mark Brownf0125f12019-01-23 17:29:53 +00001434 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1435 return;
1436 }
1437
1438 ctlr->busy = false;
1439 ctlr->idling = true;
1440 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1441
1442 kfree(ctlr->dummy_rx);
1443 ctlr->dummy_rx = NULL;
1444 kfree(ctlr->dummy_tx);
1445 ctlr->dummy_tx = NULL;
1446 if (ctlr->unprepare_transfer_hardware &&
1447 ctlr->unprepare_transfer_hardware(ctlr))
1448 dev_err(&ctlr->dev,
1449 "failed to unprepare transfer hardware\n");
Mark Browne1268592020-07-15 17:36:10 +01001450 spi_idle_runtime_pm(ctlr);
Mark Brownf0125f12019-01-23 17:29:53 +00001451 trace_spi_controller_idle(ctlr);
1452
1453 spin_lock_irqsave(&ctlr->queue_lock, flags);
1454 ctlr->idling = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001455 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001456 return;
1457 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001458
Linus Walleijffbbdd212012-02-22 10:05:38 +01001459 /* Extract head of queue */
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001460 msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1461 ctlr->cur_msg = msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001462
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001463 list_del_init(&msg->queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001464 if (ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001465 was_busy = true;
1466 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001467 ctlr->busy = true;
1468 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001469
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001470 mutex_lock(&ctlr->io_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001471
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001472 if (!was_busy && ctlr->auto_runtime_pm) {
1473 ret = pm_runtime_get_sync(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001474 if (ret < 0) {
Tony Lindgren7e48e232018-05-18 10:30:07 -07001475 pm_runtime_put_noidle(ctlr->dev.parent);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001476 dev_err(&ctlr->dev, "Failed to power device: %d\n",
Mark Brown49834de2013-07-28 14:47:02 +01001477 ret);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001478 mutex_unlock(&ctlr->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001479 return;
1480 }
1481 }
1482
Mark Brown56ec1972013-10-07 19:33:53 +01001483 if (!was_busy)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001484 trace_spi_controller_busy(ctlr);
Mark Brown56ec1972013-10-07 19:33:53 +01001485
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001486 if (!was_busy && ctlr->prepare_transfer_hardware) {
1487 ret = ctlr->prepare_transfer_hardware(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001488 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001489 dev_err(&ctlr->dev,
Super Liuf3440d92019-05-22 14:30:14 +08001490 "failed to prepare transfer hardware: %d\n",
1491 ret);
Mark Brown49834de2013-07-28 14:47:02 +01001492
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001493 if (ctlr->auto_runtime_pm)
1494 pm_runtime_put(ctlr->dev.parent);
Super Liuf3440d92019-05-22 14:30:14 +08001495
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001496 msg->status = ret;
Super Liuf3440d92019-05-22 14:30:14 +08001497 spi_finalize_current_message(ctlr);
1498
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001499 mutex_unlock(&ctlr->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001500 return;
1501 }
1502 }
1503
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001504 trace_spi_message_start(msg);
Mark Brown56ec1972013-10-07 19:33:53 +01001505
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001506 if (ctlr->prepare_message) {
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001507 ret = ctlr->prepare_message(ctlr, msg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001508 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001509 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1510 ret);
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001511 msg->status = ret;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001512 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001513 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001514 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001515 ctlr->cur_msg_prepared = true;
Mark Brown2841a5f2013-10-05 00:23:12 +01001516 }
1517
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001518 ret = spi_map_msg(ctlr, msg);
Mark Brown99adef32014-01-16 12:22:43 +00001519 if (ret) {
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001520 msg->status = ret;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001521 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001522 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001523 }
1524
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001525 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1526 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1527 xfer->ptp_sts_word_pre = 0;
1528 ptp_read_system_prets(xfer->ptp_sts);
1529 }
1530 }
1531
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001532 ret = ctlr->transfer_one_message(ctlr, msg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001533 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001534 dev_err(&ctlr->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001535 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001536 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001537 }
Jon Hunter49023d22016-03-08 12:28:20 +00001538
1539out:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001540 mutex_unlock(&ctlr->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001541
1542 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001543 if (!ret)
1544 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001545}
1546
Mark Brownfc9e0f72014-12-10 13:46:33 +00001547/**
1548 * spi_pump_messages - kthread work function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001549 * @work: pointer to kthread work struct contained in the controller struct
Mark Brownfc9e0f72014-12-10 13:46:33 +00001550 */
1551static void spi_pump_messages(struct kthread_work *work)
1552{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001553 struct spi_controller *ctlr =
1554 container_of(work, struct spi_controller, pump_messages);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001555
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001556 __spi_pump_messages(ctlr, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001557}
1558
Douglas Anderson924b5862019-05-15 09:48:12 -07001559/**
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001560 * spi_take_timestamp_pre - helper for drivers to collect the beginning of the
1561 * TX timestamp for the requested byte from the SPI
1562 * transfer. The frequency with which this function
1563 * must be called (once per word, once for the whole
1564 * transfer, once per batch of words etc) is arbitrary
1565 * as long as the @tx buffer offset is greater than or
1566 * equal to the requested byte at the time of the
1567 * call. The timestamp is only taken once, at the
1568 * first such call. It is assumed that the driver
1569 * advances its @tx buffer pointer monotonically.
1570 * @ctlr: Pointer to the spi_controller structure of the driver
1571 * @xfer: Pointer to the transfer being timestamped
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001572 * @progress: How many words (not bytes) have been transferred so far
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001573 * @irqs_off: If true, will disable IRQs and preemption for the duration of the
1574 * transfer, for less jitter in time measurement. Only compatible
1575 * with PIO drivers. If true, must follow up with
1576 * spi_take_timestamp_post or otherwise system will crash.
1577 * WARNING: for fully predictable results, the CPU frequency must
1578 * also be under control (governor).
1579 */
1580void spi_take_timestamp_pre(struct spi_controller *ctlr,
1581 struct spi_transfer *xfer,
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001582 size_t progress, bool irqs_off)
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001583{
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001584 if (!xfer->ptp_sts)
1585 return;
1586
Vladimir Oltean6a726822020-03-05 00:00:39 +02001587 if (xfer->timestamped)
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001588 return;
1589
Vladimir Oltean6a726822020-03-05 00:00:39 +02001590 if (progress > xfer->ptp_sts_word_pre)
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001591 return;
1592
1593 /* Capture the resolution of the timestamp */
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001594 xfer->ptp_sts_word_pre = progress;
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001595
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001596 if (irqs_off) {
1597 local_irq_save(ctlr->irq_flags);
1598 preempt_disable();
1599 }
1600
1601 ptp_read_system_prets(xfer->ptp_sts);
1602}
1603EXPORT_SYMBOL_GPL(spi_take_timestamp_pre);
1604
1605/**
1606 * spi_take_timestamp_post - helper for drivers to collect the end of the
1607 * TX timestamp for the requested byte from the SPI
1608 * transfer. Can be called with an arbitrary
1609 * frequency: only the first call where @tx exceeds
1610 * or is equal to the requested word will be
1611 * timestamped.
1612 * @ctlr: Pointer to the spi_controller structure of the driver
1613 * @xfer: Pointer to the transfer being timestamped
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001614 * @progress: How many words (not bytes) have been transferred so far
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001615 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1616 */
1617void spi_take_timestamp_post(struct spi_controller *ctlr,
1618 struct spi_transfer *xfer,
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001619 size_t progress, bool irqs_off)
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001620{
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001621 if (!xfer->ptp_sts)
1622 return;
1623
Vladimir Oltean6a726822020-03-05 00:00:39 +02001624 if (xfer->timestamped)
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001625 return;
1626
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001627 if (progress < xfer->ptp_sts_word_post)
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001628 return;
1629
1630 ptp_read_system_postts(xfer->ptp_sts);
1631
1632 if (irqs_off) {
1633 local_irq_restore(ctlr->irq_flags);
1634 preempt_enable();
1635 }
1636
1637 /* Capture the resolution of the timestamp */
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001638 xfer->ptp_sts_word_post = progress;
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001639
Vladimir Oltean6a726822020-03-05 00:00:39 +02001640 xfer->timestamped = true;
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001641}
1642EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
1643
1644/**
Douglas Anderson924b5862019-05-15 09:48:12 -07001645 * spi_set_thread_rt - set the controller to pump at realtime priority
1646 * @ctlr: controller to boost priority of
1647 *
1648 * This can be called because the controller requested realtime priority
1649 * (by setting the ->rt value before calling spi_register_controller()) or
1650 * because a device on the bus said that its transfers needed realtime
1651 * priority.
1652 *
1653 * NOTE: at the moment if any device on a bus says it needs realtime then
1654 * the thread will be at realtime priority for all transfers on that
1655 * controller. If this eventually becomes a problem we may see if we can
1656 * find a way to boost the priority only temporarily during relevant
1657 * transfers.
1658 */
1659static void spi_set_thread_rt(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001660{
Douglas Anderson924b5862019-05-15 09:48:12 -07001661 dev_info(&ctlr->dev,
1662 "will run message pump with realtime priority\n");
Linus Torvalds6d2b84a2020-08-06 11:55:43 -07001663 sched_set_fifo(ctlr->kworker->task);
Douglas Anderson924b5862019-05-15 09:48:12 -07001664}
1665
1666static int spi_init_queue(struct spi_controller *ctlr)
1667{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001668 ctlr->running = false;
1669 ctlr->busy = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001670
Marek Szyprowski60a883d2020-07-09 08:50:07 +02001671 ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev));
1672 if (IS_ERR(ctlr->kworker)) {
1673 dev_err(&ctlr->dev, "failed to create message pump kworker\n");
1674 return PTR_ERR(ctlr->kworker);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001675 }
Marek Szyprowski60a883d2020-07-09 08:50:07 +02001676
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001677 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
Mark Brownf0125f12019-01-23 17:29:53 +00001678
Linus Walleijffbbdd212012-02-22 10:05:38 +01001679 /*
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001680 * Controller config will indicate if this controller should run the
Linus Walleijffbbdd212012-02-22 10:05:38 +01001681 * message pump with high (realtime) priority to reduce the transfer
1682 * latency on the bus by minimising the delay between a transfer
1683 * request and the scheduling of the message pump thread. Without this
1684 * setting the message pump thread will remain at default priority.
1685 */
Douglas Anderson924b5862019-05-15 09:48:12 -07001686 if (ctlr->rt)
1687 spi_set_thread_rt(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001688
1689 return 0;
1690}
1691
1692/**
1693 * spi_get_next_queued_message() - called by driver to check for queued
1694 * messages
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001695 * @ctlr: the controller to check for queued messages
Linus Walleijffbbdd212012-02-22 10:05:38 +01001696 *
1697 * If there are more messages in the queue, the next message is returned from
1698 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001699 *
1700 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001701 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001702struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001703{
1704 struct spi_message *next;
1705 unsigned long flags;
1706
1707 /* get a pointer to the next message, if any */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001708 spin_lock_irqsave(&ctlr->queue_lock, flags);
1709 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
Axel Lin1cfd97f2014-01-02 15:16:40 +08001710 queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001711 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001712
1713 return next;
1714}
1715EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1716
1717/**
1718 * spi_finalize_current_message() - the current message is complete
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001719 * @ctlr: the controller to return the message to
Linus Walleijffbbdd212012-02-22 10:05:38 +01001720 *
1721 * Called by the driver to notify the core that the message in the front of the
1722 * queue is complete and can be removed from the queue.
1723 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001724void spi_finalize_current_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001725{
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001726 struct spi_transfer *xfer;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001727 struct spi_message *mesg;
1728 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001729 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001730
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001731 spin_lock_irqsave(&ctlr->queue_lock, flags);
1732 mesg = ctlr->cur_msg;
1733 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001734
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001735 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1736 list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
1737 ptp_read_system_postts(xfer->ptp_sts);
1738 xfer->ptp_sts_word_post = xfer->len;
1739 }
1740 }
1741
Vladimir Oltean6a726822020-03-05 00:00:39 +02001742 if (unlikely(ctlr->ptp_sts_supported))
1743 list_for_each_entry(xfer, &mesg->transfers, transfer_list)
1744 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped);
Vladimir Olteanf971a202019-12-27 03:24:44 +02001745
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001746 spi_unmap_msg(ctlr, mesg);
Mark Brown99adef32014-01-16 12:22:43 +00001747
Gustav Wiklanderb59a7ca2020-09-08 17:11:29 +02001748 /* In the prepare_messages callback the spi bus has the opportunity to
1749 * split a transfer to smaller chunks.
1750 * Release splited transfers here since spi_map_msg is done on the
1751 * splited transfers.
1752 */
1753 spi_res_release(ctlr, mesg);
1754
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001755 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1756 ret = ctlr->unprepare_message(ctlr, mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001757 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001758 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1759 ret);
Mark Brown2841a5f2013-10-05 00:23:12 +01001760 }
1761 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001762
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001763 spin_lock_irqsave(&ctlr->queue_lock, flags);
1764 ctlr->cur_msg = NULL;
1765 ctlr->cur_msg_prepared = false;
Robin Gong809b1b02020-06-17 06:42:08 +08001766 ctlr->fallback = false;
Marek Szyprowski60a883d2020-07-09 08:50:07 +02001767 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001768 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001769
1770 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001771
Linus Walleijffbbdd212012-02-22 10:05:38 +01001772 mesg->state = NULL;
1773 if (mesg->complete)
1774 mesg->complete(mesg->context);
1775}
1776EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1777
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001778static int spi_start_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001779{
1780 unsigned long flags;
1781
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001782 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001783
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001784 if (ctlr->running || ctlr->busy) {
1785 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001786 return -EBUSY;
1787 }
1788
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001789 ctlr->running = true;
1790 ctlr->cur_msg = NULL;
1791 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001792
Marek Szyprowski60a883d2020-07-09 08:50:07 +02001793 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001794
1795 return 0;
1796}
1797
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001798static int spi_stop_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001799{
1800 unsigned long flags;
1801 unsigned limit = 500;
1802 int ret = 0;
1803
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001804 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001805
1806 /*
1807 * This is a bit lame, but is optimized for the common execution path.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001808 * A wait_queue on the ctlr->busy could be used, but then the common
Linus Walleijffbbdd212012-02-22 10:05:38 +01001809 * execution path (pump_messages) would be required to call wake_up or
1810 * friends on every SPI message. Do this instead.
1811 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001812 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1813 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001814 usleep_range(10000, 11000);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001815 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001816 }
1817
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001818 if (!list_empty(&ctlr->queue) || ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001819 ret = -EBUSY;
1820 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001821 ctlr->running = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001822
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001823 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001824
1825 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001826 dev_warn(&ctlr->dev, "could not stop message queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001827 return ret;
1828 }
1829 return ret;
1830}
1831
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001832static int spi_destroy_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001833{
1834 int ret;
1835
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001836 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001837
1838 /*
Petr Mladek39891442016-10-11 13:55:20 -07001839 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001840 * If the reason that stop_queue timed out is that the work will never
1841 * finish, then it does no good to call flush/stop thread, so
1842 * return anyway.
1843 */
1844 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001845 dev_err(&ctlr->dev, "problem destroying queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001846 return ret;
1847 }
1848
Marek Szyprowski60a883d2020-07-09 08:50:07 +02001849 kthread_destroy_worker(ctlr->kworker);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001850
1851 return 0;
1852}
1853
Mark Brown0461a412014-12-09 21:38:05 +00001854static int __spi_queued_transfer(struct spi_device *spi,
1855 struct spi_message *msg,
1856 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001857{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001858 struct spi_controller *ctlr = spi->controller;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001859 unsigned long flags;
1860
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001861 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001862
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001863 if (!ctlr->running) {
1864 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001865 return -ESHUTDOWN;
1866 }
1867 msg->actual_length = 0;
1868 msg->status = -EINPROGRESS;
1869
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001870 list_add_tail(&msg->queue, &ctlr->queue);
Mark Brownf0125f12019-01-23 17:29:53 +00001871 if (!ctlr->busy && need_pump)
Marek Szyprowski60a883d2020-07-09 08:50:07 +02001872 kthread_queue_work(ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001873
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001874 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001875 return 0;
1876}
1877
Mark Brown0461a412014-12-09 21:38:05 +00001878/**
1879 * spi_queued_transfer - transfer function for queued transfers
1880 * @spi: spi device which is requesting transfer
1881 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001882 *
1883 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001884 */
1885static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1886{
1887 return __spi_queued_transfer(spi, msg, true);
1888}
1889
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001890static int spi_controller_initialize_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001891{
1892 int ret;
1893
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001894 ctlr->transfer = spi_queued_transfer;
1895 if (!ctlr->transfer_one_message)
1896 ctlr->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001897
1898 /* Initialize and start queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001899 ret = spi_init_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001900 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001901 dev_err(&ctlr->dev, "problem initializing queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001902 goto err_init_queue;
1903 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001904 ctlr->queued = true;
1905 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001906 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001907 dev_err(&ctlr->dev, "problem starting queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001908 goto err_start_queue;
1909 }
1910
1911 return 0;
1912
1913err_start_queue:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001914 spi_destroy_queue(ctlr);
Mark Brownc3676d52014-05-01 10:47:52 -07001915err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001916 return ret;
1917}
1918
Boris Brezillon988f2592018-04-22 20:35:15 +02001919/**
1920 * spi_flush_queue - Send all pending messages in the queue from the callers'
1921 * context
1922 * @ctlr: controller to process queue for
1923 *
1924 * This should be used when one wants to ensure all pending messages have been
1925 * sent before doing something. Is used by the spi-mem code to make sure SPI
1926 * memory operations do not preempt regular SPI transfers that have been queued
1927 * before the spi-mem operation.
1928 */
1929void spi_flush_queue(struct spi_controller *ctlr)
1930{
1931 if (ctlr->transfer == spi_queued_transfer)
1932 __spi_pump_messages(ctlr, false);
1933}
1934
Linus Walleijffbbdd212012-02-22 10:05:38 +01001935/*-------------------------------------------------------------------------*/
1936
Andreas Larsson7cb94362012-12-04 15:09:38 +01001937#if defined(CONFIG_OF)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001938static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001939 struct device_node *nc)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001940{
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001941 u32 value;
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001942 int rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001943
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001944 /* Mode (clock phase/polarity/etc.) */
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001945 if (of_property_read_bool(nc, "spi-cpha"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001946 spi->mode |= SPI_CPHA;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001947 if (of_property_read_bool(nc, "spi-cpol"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001948 spi->mode |= SPI_CPOL;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001949 if (of_property_read_bool(nc, "spi-3wire"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001950 spi->mode |= SPI_3WIRE;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001951 if (of_property_read_bool(nc, "spi-lsb-first"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001952 spi->mode |= SPI_LSB_FIRST;
Gregory CLEMENT3e5ec1d2019-10-18 17:29:29 +02001953 if (of_property_read_bool(nc, "spi-cs-high"))
Linus Walleijf3186dd2019-01-07 16:51:50 +01001954 spi->mode |= SPI_CS_HIGH;
1955
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001956 /* Device DUAL/QUAD mode */
1957 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1958 switch (value) {
1959 case 1:
1960 break;
1961 case 2:
1962 spi->mode |= SPI_TX_DUAL;
1963 break;
1964 case 4:
1965 spi->mode |= SPI_TX_QUAD;
1966 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001967 case 8:
1968 spi->mode |= SPI_TX_OCTAL;
1969 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001970 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001971 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001972 "spi-tx-bus-width %d not supported\n",
1973 value);
1974 break;
1975 }
1976 }
1977
1978 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1979 switch (value) {
1980 case 1:
1981 break;
1982 case 2:
1983 spi->mode |= SPI_RX_DUAL;
1984 break;
1985 case 4:
1986 spi->mode |= SPI_RX_QUAD;
1987 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001988 case 8:
1989 spi->mode |= SPI_RX_OCTAL;
1990 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001991 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001992 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001993 "spi-rx-bus-width %d not supported\n",
1994 value);
1995 break;
1996 }
1997 }
1998
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001999 if (spi_controller_is_slave(ctlr)) {
Rob Herring194276b2018-12-05 13:50:41 -06002000 if (!of_node_name_eq(nc, "slave")) {
Rob Herring25c56c82017-07-18 16:43:31 -05002001 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
2002 nc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002003 return -EINVAL;
2004 }
2005 return 0;
2006 }
2007
2008 /* Device address */
2009 rc = of_property_read_u32(nc, "reg", &value);
2010 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05002011 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
2012 nc, rc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002013 return rc;
2014 }
2015 spi->chip_select = value;
2016
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002017 /* Device speed */
Chuanhong Guo671c3bf2020-03-06 16:50:49 +08002018 if (!of_property_read_u32(nc, "spi-max-frequency", &value))
2019 spi->max_speed_hz = value;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002020
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02002021 return 0;
2022}
2023
2024static struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002025of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02002026{
2027 struct spi_device *spi;
2028 int rc;
2029
2030 /* Alloc an spi_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002031 spi = spi_alloc_device(ctlr);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02002032 if (!spi) {
Rob Herring25c56c82017-07-18 16:43:31 -05002033 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02002034 rc = -ENOMEM;
2035 goto err_out;
2036 }
2037
2038 /* Select device driver */
2039 rc = of_modalias_node(nc, spi->modalias,
2040 sizeof(spi->modalias));
2041 if (rc < 0) {
Rob Herring25c56c82017-07-18 16:43:31 -05002042 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02002043 goto err_out;
2044 }
2045
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002046 rc = of_spi_parse_dt(ctlr, spi, nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02002047 if (rc)
2048 goto err_out;
2049
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002050 /* Store a pointer to the node in the device structure */
2051 of_node_get(nc);
2052 spi->dev.of_node = nc;
Charles Keepax61a7a632021-04-21 11:14:02 +01002053 spi->dev.fwnode = of_fwnode_handle(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002054
2055 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002056 rc = spi_add_device(spi);
2057 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05002058 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
Johan Hovold83241472017-01-30 17:47:05 +01002059 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002060 }
2061
2062 return spi;
2063
Johan Hovold83241472017-01-30 17:47:05 +01002064err_of_node_put:
2065 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002066err_out:
2067 spi_dev_put(spi);
2068 return ERR_PTR(rc);
2069}
2070
Grant Likelyd57a4282012-04-07 14:16:53 -06002071/**
2072 * of_register_spi_devices() - Register child devices onto the SPI bus
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002073 * @ctlr: Pointer to spi_controller device
Grant Likelyd57a4282012-04-07 14:16:53 -06002074 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002075 * Registers an spi_device for each child node of controller node which
2076 * represents a valid SPI slave.
Grant Likelyd57a4282012-04-07 14:16:53 -06002077 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002078static void of_register_spi_devices(struct spi_controller *ctlr)
Grant Likelyd57a4282012-04-07 14:16:53 -06002079{
2080 struct spi_device *spi;
2081 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06002082
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002083 if (!ctlr->dev.of_node)
Grant Likelyd57a4282012-04-07 14:16:53 -06002084 return;
2085
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002086 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01002087 if (of_node_test_and_set_flag(nc, OF_POPULATED))
2088 continue;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002089 spi = of_register_spi_device(ctlr, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02002090 if (IS_ERR(spi)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002091 dev_warn(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05002092 "Failed to create SPI device for %pOF\n", nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02002093 of_node_clear_flag(nc, OF_POPULATED);
2094 }
Grant Likelyd57a4282012-04-07 14:16:53 -06002095 }
2096}
2097#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002098static void of_register_spi_devices(struct spi_controller *ctlr) { }
Grant Likelyd57a4282012-04-07 14:16:53 -06002099#endif
2100
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002101#ifdef CONFIG_ACPI
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002102struct acpi_spi_lookup {
2103 struct spi_controller *ctlr;
2104 u32 max_speed_hz;
2105 u32 mode;
2106 int irq;
2107 u8 bits_per_word;
2108 u8 chip_select;
2109};
2110
2111static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
2112 struct acpi_spi_lookup *lookup)
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002113{
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002114 const union acpi_object *obj;
2115
2116 if (!x86_apple_machine)
2117 return;
2118
2119 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
2120 && obj->buffer.length >= 4)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002121 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002122
2123 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
2124 && obj->buffer.length == 8)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002125 lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002126
2127 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
2128 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002129 lookup->mode |= SPI_LSB_FIRST;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002130
2131 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
2132 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002133 lookup->mode |= SPI_CPOL;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002134
2135 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
2136 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002137 lookup->mode |= SPI_CPHA;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002138}
2139
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002140static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
2141{
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002142 struct acpi_spi_lookup *lookup = data;
2143 struct spi_controller *ctlr = lookup->ctlr;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002144
2145 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
2146 struct acpi_resource_spi_serialbus *sb;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002147 acpi_handle parent_handle;
2148 acpi_status status;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002149
2150 sb = &ares->data.spi_serial_bus;
2151 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002152
2153 status = acpi_get_handle(NULL,
2154 sb->resource_source.string_ptr,
2155 &parent_handle);
2156
Ard Biesheuvelb5e3cf42019-06-19 11:52:54 +02002157 if (ACPI_FAILURE(status) ||
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002158 ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
2159 return -ENODEV;
2160
Mika Westerberga0a90712016-02-08 17:14:28 +02002161 /*
2162 * ACPI DeviceSelection numbering is handled by the
2163 * host controller driver in Windows and can vary
2164 * from driver to driver. In Linux we always expect
2165 * 0 .. max - 1 so we need to ask the driver to
2166 * translate between the two schemes.
2167 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002168 if (ctlr->fw_translate_cs) {
2169 int cs = ctlr->fw_translate_cs(ctlr,
Mika Westerberga0a90712016-02-08 17:14:28 +02002170 sb->device_selection);
2171 if (cs < 0)
2172 return cs;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002173 lookup->chip_select = cs;
Mika Westerberga0a90712016-02-08 17:14:28 +02002174 } else {
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002175 lookup->chip_select = sb->device_selection;
Mika Westerberga0a90712016-02-08 17:14:28 +02002176 }
2177
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002178 lookup->max_speed_hz = sb->connection_speed;
Andy Shevchenko0dadde32020-04-13 21:04:06 +03002179 lookup->bits_per_word = sb->data_bit_length;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002180
2181 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002182 lookup->mode |= SPI_CPHA;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002183 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002184 lookup->mode |= SPI_CPOL;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002185 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002186 lookup->mode |= SPI_CS_HIGH;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002187 }
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002188 } else if (lookup->irq < 0) {
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002189 struct resource r;
2190
2191 if (acpi_dev_resource_interrupt(ares, 0, &r))
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002192 lookup->irq = r.start;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002193 }
2194
2195 /* Always tell the ACPI core to skip this resource */
2196 return 1;
2197}
2198
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002199static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
Octavian Purdila7f244672016-07-08 19:13:11 +03002200 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002201{
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002202 acpi_handle parent_handle = NULL;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002203 struct list_head resource_list;
Ard Biesheuvelb28944c2019-06-20 14:36:49 +02002204 struct acpi_spi_lookup lookup = {};
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002205 struct spi_device *spi;
2206 int ret;
2207
Octavian Purdila7f244672016-07-08 19:13:11 +03002208 if (acpi_bus_get_status(adev) || !adev->status.present ||
2209 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002210 return AE_OK;
2211
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002212 lookup.ctlr = ctlr;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002213 lookup.irq = -1;
2214
2215 INIT_LIST_HEAD(&resource_list);
2216 ret = acpi_dev_get_resources(adev, &resource_list,
2217 acpi_spi_add_resource, &lookup);
2218 acpi_dev_free_resource_list(&resource_list);
2219
2220 if (ret < 0)
2221 /* found SPI in _CRS but it points to another controller */
2222 return AE_OK;
2223
2224 if (!lookup.max_speed_hz &&
2225 !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) &&
2226 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) {
2227 /* Apple does not use _CRS but nested devices for SPI slaves */
2228 acpi_spi_parse_apple_properties(adev, &lookup);
2229 }
2230
2231 if (!lookup.max_speed_hz)
2232 return AE_OK;
2233
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002234 spi = spi_alloc_device(ctlr);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002235 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002236 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002237 dev_name(&adev->dev));
2238 return AE_NO_MEMORY;
2239 }
2240
John Garryea235782020-02-28 23:18:49 +08002241
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01002242 ACPI_COMPANION_SET(&spi->dev, adev);
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002243 spi->max_speed_hz = lookup.max_speed_hz;
John Garryea235782020-02-28 23:18:49 +08002244 spi->mode |= lookup.mode;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002245 spi->irq = lookup.irq;
2246 spi->bits_per_word = lookup.bits_per_word;
2247 spi->chip_select = lookup.chip_select;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002248
Dan O'Donovan0c6543f2017-02-05 16:30:14 +00002249 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
2250 sizeof(spi->modalias));
2251
Christophe RICARD33ada672015-12-23 23:25:35 +01002252 if (spi->irq < 0)
2253 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
2254
Octavian Purdila7f244672016-07-08 19:13:11 +03002255 acpi_device_set_enumerated(adev);
2256
Mika Westerberg33cf00e2013-10-10 13:28:48 +03002257 adev->power.flags.ignore_parent = true;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002258 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03002259 adev->power.flags.ignore_parent = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002260 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002261 dev_name(&adev->dev));
2262 spi_dev_put(spi);
2263 }
2264
2265 return AE_OK;
2266}
2267
Octavian Purdila7f244672016-07-08 19:13:11 +03002268static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
2269 void *data, void **return_value)
2270{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002271 struct spi_controller *ctlr = data;
Octavian Purdila7f244672016-07-08 19:13:11 +03002272 struct acpi_device *adev;
2273
2274 if (acpi_bus_get_device(handle, &adev))
2275 return AE_OK;
2276
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002277 return acpi_register_spi_device(ctlr, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03002278}
2279
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002280#define SPI_ACPI_ENUMERATE_MAX_DEPTH 32
2281
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002282static void acpi_register_spi_devices(struct spi_controller *ctlr)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002283{
2284 acpi_status status;
2285 acpi_handle handle;
2286
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002287 handle = ACPI_HANDLE(ctlr->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002288 if (!handle)
2289 return;
2290
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002291 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
2292 SPI_ACPI_ENUMERATE_MAX_DEPTH,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002293 acpi_spi_add_device, NULL, ctlr, NULL);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002294 if (ACPI_FAILURE(status))
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002295 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002296}
2297#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002298static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002299#endif /* CONFIG_ACPI */
2300
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002301static void spi_controller_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08002302{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002303 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002304
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002305 ctlr = container_of(dev, struct spi_controller, dev);
2306 kfree(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002307}
2308
2309static struct class spi_master_class = {
2310 .name = "spi_master",
2311 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002312 .dev_release = spi_controller_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00002313 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08002314};
2315
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002316#ifdef CONFIG_SPI_SLAVE
2317/**
2318 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2319 * controller
2320 * @spi: device used for the current transfer
2321 */
2322int spi_slave_abort(struct spi_device *spi)
2323{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002324 struct spi_controller *ctlr = spi->controller;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002325
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002326 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
2327 return ctlr->slave_abort(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002328
2329 return -ENOTSUPP;
2330}
2331EXPORT_SYMBOL_GPL(spi_slave_abort);
2332
2333static int match_true(struct device *dev, void *data)
2334{
2335 return 1;
2336}
2337
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002338static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
2339 char *buf)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002340{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002341 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2342 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002343 struct device *child;
2344
2345 child = device_find_child(&ctlr->dev, NULL, match_true);
2346 return sprintf(buf, "%s\n",
2347 child ? to_spi_device(child)->modalias : NULL);
2348}
2349
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002350static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
2351 const char *buf, size_t count)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002352{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002353 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2354 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002355 struct spi_device *spi;
2356 struct device *child;
2357 char name[32];
2358 int rc;
2359
2360 rc = sscanf(buf, "%31s", name);
2361 if (rc != 1 || !name[0])
2362 return -EINVAL;
2363
2364 child = device_find_child(&ctlr->dev, NULL, match_true);
2365 if (child) {
2366 /* Remove registered slave */
2367 device_unregister(child);
2368 put_device(child);
2369 }
2370
2371 if (strcmp(name, "(null)")) {
2372 /* Register new slave */
2373 spi = spi_alloc_device(ctlr);
2374 if (!spi)
2375 return -ENOMEM;
2376
2377 strlcpy(spi->modalias, name, sizeof(spi->modalias));
2378
2379 rc = spi_add_device(spi);
2380 if (rc) {
2381 spi_dev_put(spi);
2382 return rc;
2383 }
2384 }
2385
2386 return count;
2387}
2388
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002389static DEVICE_ATTR_RW(slave);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002390
2391static struct attribute *spi_slave_attrs[] = {
2392 &dev_attr_slave.attr,
2393 NULL,
2394};
2395
2396static const struct attribute_group spi_slave_group = {
2397 .attrs = spi_slave_attrs,
2398};
2399
2400static const struct attribute_group *spi_slave_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002401 &spi_controller_statistics_group,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002402 &spi_slave_group,
2403 NULL,
2404};
2405
2406static struct class spi_slave_class = {
2407 .name = "spi_slave",
2408 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002409 .dev_release = spi_controller_release,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002410 .dev_groups = spi_slave_groups,
2411};
2412#else
2413extern struct class spi_slave_class; /* dummy */
2414#endif
David Brownell8ae12a02006-01-08 13:34:19 -08002415
2416/**
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002417 * __spi_alloc_controller - allocate an SPI master or slave controller
David Brownell8ae12a02006-01-08 13:34:19 -08002418 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07002419 * @size: how much zeroed driver-private data to allocate; the pointer to this
Lukas Wunner229e6af2019-09-11 12:15:30 +02002420 * memory is in the driver_data field of the returned device, accessible
2421 * with spi_controller_get_devdata(); the memory is cacheline aligned;
2422 * drivers granting DMA access to portions of their private data need to
2423 * round up @size using ALIGN(size, dma_get_cache_alignment()).
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002424 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2425 * slave (true) controller
David Brownell33e34dc2007-05-08 00:32:21 -07002426 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002427 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002428 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002429 * only ones directly touching chip registers. It's how they allocate
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002430 * an spi_controller structure, prior to calling spi_register_controller().
David Brownell8ae12a02006-01-08 13:34:19 -08002431 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002432 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08002433 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002434 * The caller is responsible for assigning the bus number and initializing the
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002435 * controller's methods before calling spi_register_controller(); and (after
2436 * errors adding the device) calling spi_controller_put() to prevent a memory
2437 * leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002438 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002439 * Return: the SPI controller structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002440 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002441struct spi_controller *__spi_alloc_controller(struct device *dev,
2442 unsigned int size, bool slave)
David Brownell8ae12a02006-01-08 13:34:19 -08002443{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002444 struct spi_controller *ctlr;
Lukas Wunner229e6af2019-09-11 12:15:30 +02002445 size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
David Brownell8ae12a02006-01-08 13:34:19 -08002446
David Brownell0c8684612006-01-08 13:34:25 -08002447 if (!dev)
2448 return NULL;
2449
Lukas Wunner229e6af2019-09-11 12:15:30 +02002450 ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002451 if (!ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002452 return NULL;
2453
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002454 device_initialize(&ctlr->dev);
2455 ctlr->bus_num = -1;
2456 ctlr->num_chipselect = 1;
2457 ctlr->slave = slave;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002458 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002459 ctlr->dev.class = &spi_slave_class;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002460 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002461 ctlr->dev.class = &spi_master_class;
2462 ctlr->dev.parent = dev;
2463 pm_suspend_ignore_children(&ctlr->dev, true);
Lukas Wunner229e6af2019-09-11 12:15:30 +02002464 spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
David Brownell8ae12a02006-01-08 13:34:19 -08002465
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002466 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002467}
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002468EXPORT_SYMBOL_GPL(__spi_alloc_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002469
Lukas Wunner5e844cc2020-11-11 20:07:10 +01002470static void devm_spi_release_controller(struct device *dev, void *ctlr)
2471{
2472 spi_controller_put(*(struct spi_controller **)ctlr);
2473}
2474
2475/**
2476 * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller()
2477 * @dev: physical device of SPI controller
2478 * @size: how much zeroed driver-private data to allocate
2479 * @slave: whether to allocate an SPI master (false) or SPI slave (true)
2480 * Context: can sleep
2481 *
2482 * Allocate an SPI controller and automatically release a reference on it
2483 * when @dev is unbound from its driver. Drivers are thus relieved from
2484 * having to call spi_controller_put().
2485 *
2486 * The arguments to this function are identical to __spi_alloc_controller().
2487 *
2488 * Return: the SPI controller structure on success, else NULL.
2489 */
2490struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
2491 unsigned int size,
2492 bool slave)
2493{
2494 struct spi_controller **ptr, *ctlr;
2495
2496 ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr),
2497 GFP_KERNEL);
2498 if (!ptr)
2499 return NULL;
2500
2501 ctlr = __spi_alloc_controller(dev, size, slave);
2502 if (ctlr) {
William A. Kennington IIIc7fabe32021-04-07 02:55:27 -07002503 ctlr->devm_allocated = true;
Lukas Wunner5e844cc2020-11-11 20:07:10 +01002504 *ptr = ctlr;
2505 devres_add(dev, ptr);
2506 } else {
2507 devres_free(ptr);
2508 }
2509
2510 return ctlr;
2511}
2512EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller);
2513
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002514#ifdef CONFIG_OF
Linus Walleij43004f32019-08-08 17:03:21 +02002515static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002516{
Grant Likelye80beb22013-02-12 17:48:37 +00002517 int nb, i, *cs;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002518 struct device_node *np = ctlr->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002519
2520 if (!np)
2521 return 0;
2522
2523 nb = of_gpio_named_count(np, "cs-gpios");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002524 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002525
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002526 /* Return error only for an incorrectly formed cs-gpios property */
2527 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002528 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002529 else if (nb < 0)
2530 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002531
Kees Cooka86854d2018-06-12 14:07:58 -07002532 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002533 GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002534 ctlr->cs_gpios = cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002535
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002536 if (!ctlr->cs_gpios)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002537 return -ENOMEM;
2538
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002539 for (i = 0; i < ctlr->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01002540 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002541
2542 for (i = 0; i < nb; i++)
2543 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2544
2545 return 0;
2546}
2547#else
Linus Walleij43004f32019-08-08 17:03:21 +02002548static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002549{
2550 return 0;
2551}
2552#endif
2553
Linus Walleijf3186dd2019-01-07 16:51:50 +01002554/**
2555 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2556 * @ctlr: The SPI master to grab GPIO descriptors for
2557 */
2558static int spi_get_gpio_descs(struct spi_controller *ctlr)
2559{
2560 int nb, i;
2561 struct gpio_desc **cs;
2562 struct device *dev = &ctlr->dev;
Geert Uytterhoeven7d93aec2020-01-02 14:38:17 +01002563 unsigned long native_cs_mask = 0;
2564 unsigned int num_cs_gpios = 0;
Linus Walleijf3186dd2019-01-07 16:51:50 +01002565
2566 nb = gpiod_count(dev, "cs");
2567 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2568
2569 /* No GPIOs at all is fine, else return the error */
2570 if (nb == 0 || nb == -ENOENT)
2571 return 0;
2572 else if (nb < 0)
2573 return nb;
2574
2575 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
2576 GFP_KERNEL);
2577 if (!cs)
2578 return -ENOMEM;
2579 ctlr->cs_gpiods = cs;
2580
2581 for (i = 0; i < nb; i++) {
2582 /*
2583 * Most chipselects are active low, the inverted
2584 * semantics are handled by special quirks in gpiolib,
2585 * so initializing them GPIOD_OUT_LOW here means
2586 * "unasserted", in most cases this will drive the physical
2587 * line high.
2588 */
2589 cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
2590 GPIOD_OUT_LOW);
Geert Uytterhoeven1723fde2019-04-03 16:46:56 +02002591 if (IS_ERR(cs[i]))
2592 return PTR_ERR(cs[i]);
Linus Walleijf3186dd2019-01-07 16:51:50 +01002593
2594 if (cs[i]) {
2595 /*
2596 * If we find a CS GPIO, name it after the device and
2597 * chip select line.
2598 */
2599 char *gpioname;
2600
2601 gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
2602 dev_name(dev), i);
2603 if (!gpioname)
2604 return -ENOMEM;
2605 gpiod_set_consumer_name(cs[i], gpioname);
Geert Uytterhoeven7d93aec2020-01-02 14:38:17 +01002606 num_cs_gpios++;
2607 continue;
Linus Walleijf3186dd2019-01-07 16:51:50 +01002608 }
Geert Uytterhoeven7d93aec2020-01-02 14:38:17 +01002609
2610 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) {
2611 dev_err(dev, "Invalid native chip select %d\n", i);
2612 return -EINVAL;
2613 }
2614 native_cs_mask |= BIT(i);
2615 }
2616
Andy Shevchenko08d0aa12021-04-20 19:44:25 +03002617 ctlr->unused_native_cs = ffs(~native_cs_mask) - 1;
Andy Shevchenkodb5a7e22021-04-20 19:44:24 +03002618
2619 if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios &&
2620 ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) {
Geert Uytterhoeven7d93aec2020-01-02 14:38:17 +01002621 dev_err(dev, "No unused native chip select available\n");
2622 return -EINVAL;
Linus Walleijf3186dd2019-01-07 16:51:50 +01002623 }
2624
2625 return 0;
2626}
2627
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002628static int spi_controller_check_ops(struct spi_controller *ctlr)
2629{
2630 /*
Boris Brezillonb5932f52018-04-26 18:18:15 +02002631 * The controller may implement only the high-level SPI-memory like
2632 * operations if it does not support regular SPI transfers, and this is
2633 * valid use case.
2634 * If ->mem_ops is NULL, we request that at least one of the
2635 * ->transfer_xxx() method be implemented.
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002636 */
Boris Brezillonb5932f52018-04-26 18:18:15 +02002637 if (ctlr->mem_ops) {
2638 if (!ctlr->mem_ops->exec_op)
2639 return -EINVAL;
2640 } else if (!ctlr->transfer && !ctlr->transfer_one &&
2641 !ctlr->transfer_one_message) {
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002642 return -EINVAL;
Boris Brezillonb5932f52018-04-26 18:18:15 +02002643 }
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002644
2645 return 0;
2646}
2647
David Brownell8ae12a02006-01-08 13:34:19 -08002648/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002649 * spi_register_controller - register SPI master or slave controller
2650 * @ctlr: initialized master, originally from spi_alloc_master() or
2651 * spi_alloc_slave()
David Brownell33e34dc2007-05-08 00:32:21 -07002652 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002653 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002654 * SPI controllers connect to their drivers using some non-SPI bus,
David Brownell8ae12a02006-01-08 13:34:19 -08002655 * such as the platform bus. The final stage of probe() in that code
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002656 * includes calling spi_register_controller() to hook up to this SPI bus glue.
David Brownell8ae12a02006-01-08 13:34:19 -08002657 *
2658 * SPI controllers use board specific (often SOC specific) bus numbers,
2659 * and board-specific addressing for SPI devices combines those numbers
2660 * with chip select numbers. Since SPI does not directly support dynamic
2661 * device identification, boards need configuration tables telling which
2662 * chip is at which address.
2663 *
2664 * This must be called from context that can sleep. It returns zero on
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002665 * success, else a negative error code (dropping the controller's refcount).
David Brownell0c8684612006-01-08 13:34:25 -08002666 * After a successful return, the caller is responsible for calling
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002667 * spi_unregister_controller().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002668 *
2669 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002670 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002671int spi_register_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002672{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002673 struct device *dev = ctlr->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08002674 struct boardinfo *bi;
Sergei Shtylyovb93318a2019-04-05 18:48:50 +03002675 int status;
Lucas Stach42bdd702017-10-16 12:27:58 +02002676 int id, first_dynamic;
David Brownell8ae12a02006-01-08 13:34:19 -08002677
David Brownell0c8684612006-01-08 13:34:25 -08002678 if (!dev)
2679 return -ENODEV;
2680
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002681 /*
2682 * Make sure all necessary hooks are implemented before registering
2683 * the SPI controller.
2684 */
2685 status = spi_controller_check_ops(ctlr);
2686 if (status)
2687 return status;
2688
Geert Uytterhoeven04b2d032018-08-21 11:53:03 +02002689 if (ctlr->bus_num >= 0) {
2690 /* devices with a fixed bus num must check-in with the num */
2691 mutex_lock(&board_lock);
2692 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2693 ctlr->bus_num + 1, GFP_KERNEL);
2694 mutex_unlock(&board_lock);
2695 if (WARN(id < 0, "couldn't get idr"))
2696 return id == -ENOSPC ? -EBUSY : id;
2697 ctlr->bus_num = id;
2698 } else if (ctlr->dev.of_node) {
2699 /* allocate dynamic bus number using Linux idr */
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302700 id = of_alias_get_id(ctlr->dev.of_node, "spi");
2701 if (id >= 0) {
2702 ctlr->bus_num = id;
2703 mutex_lock(&board_lock);
2704 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2705 ctlr->bus_num + 1, GFP_KERNEL);
2706 mutex_unlock(&board_lock);
2707 if (WARN(id < 0, "couldn't get idr"))
2708 return id == -ENOSPC ? -EBUSY : id;
2709 }
2710 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002711 if (ctlr->bus_num < 0) {
Lucas Stach42bdd702017-10-16 12:27:58 +02002712 first_dynamic = of_alias_get_highest_id("spi");
2713 if (first_dynamic < 0)
2714 first_dynamic = 0;
2715 else
2716 first_dynamic++;
2717
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302718 mutex_lock(&board_lock);
Lucas Stach42bdd702017-10-16 12:27:58 +02002719 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
2720 0, GFP_KERNEL);
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302721 mutex_unlock(&board_lock);
2722 if (WARN(id < 0, "couldn't get idr"))
2723 return id;
2724 ctlr->bus_num = id;
David Brownell8ae12a02006-01-08 13:34:19 -08002725 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002726 INIT_LIST_HEAD(&ctlr->queue);
2727 spin_lock_init(&ctlr->queue_lock);
2728 spin_lock_init(&ctlr->bus_lock_spinlock);
2729 mutex_init(&ctlr->bus_lock_mutex);
2730 mutex_init(&ctlr->io_mutex);
2731 ctlr->bus_lock_flag = 0;
2732 init_completion(&ctlr->xfer_completion);
2733 if (!ctlr->max_dma_len)
2734 ctlr->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002735
David Brownell8ae12a02006-01-08 13:34:19 -08002736 /* register the device, then userspace will see it.
2737 * registration fails if the bus ID is in use.
2738 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002739 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
Andrey Smirnov0a919ae2019-04-02 21:01:28 -07002740
2741 if (!spi_controller_is_slave(ctlr)) {
2742 if (ctlr->use_gpio_descriptors) {
2743 status = spi_get_gpio_descs(ctlr);
2744 if (status)
Aaro Koskinenf9981d42020-03-04 13:17:40 +02002745 goto free_bus_id;
Andrey Smirnov0a919ae2019-04-02 21:01:28 -07002746 /*
2747 * A controller using GPIO descriptors always
2748 * supports SPI_CS_HIGH if need be.
2749 */
2750 ctlr->mode_bits |= SPI_CS_HIGH;
2751 } else {
2752 /* Legacy code path for GPIOs from DT */
Linus Walleij43004f32019-08-08 17:03:21 +02002753 status = of_spi_get_gpio_numbers(ctlr);
Andrey Smirnov0a919ae2019-04-02 21:01:28 -07002754 if (status)
Aaro Koskinenf9981d42020-03-04 13:17:40 +02002755 goto free_bus_id;
Andrey Smirnov0a919ae2019-04-02 21:01:28 -07002756 }
2757 }
2758
Tudor Ambarusf9481b02019-06-19 14:38:28 +00002759 /*
2760 * Even if it's just one always-selected device, there must
2761 * be at least one chipselect.
2762 */
Aaro Koskinenf9981d42020-03-04 13:17:40 +02002763 if (!ctlr->num_chipselect) {
2764 status = -EINVAL;
2765 goto free_bus_id;
2766 }
Tudor Ambarusf9481b02019-06-19 14:38:28 +00002767
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002768 status = device_add(&ctlr->dev);
Aaro Koskinenf9981d42020-03-04 13:17:40 +02002769 if (status < 0)
2770 goto free_bus_id;
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302771 dev_dbg(dev, "registered %s %s\n",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002772 spi_controller_is_slave(ctlr) ? "slave" : "master",
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302773 dev_name(&ctlr->dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002774
Boris Brezillonb5932f52018-04-26 18:18:15 +02002775 /*
2776 * If we're using a queued driver, start the queue. Note that we don't
2777 * need the queueing logic if the driver is only supporting high-level
2778 * memory operations.
2779 */
2780 if (ctlr->transfer) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002781 dev_info(dev, "controller is unqueued, this is deprecated\n");
Boris Brezillonb5932f52018-04-26 18:18:15 +02002782 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002783 status = spi_controller_initialize_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002784 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002785 device_del(&ctlr->dev);
Aaro Koskinenf9981d42020-03-04 13:17:40 +02002786 goto free_bus_id;
Linus Walleijffbbdd212012-02-22 10:05:38 +01002787 }
2788 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00002789 /* add statistics */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002790 spin_lock_init(&ctlr->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002791
Feng Tang2b9603a2010-08-02 15:52:15 +08002792 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002793 list_add_tail(&ctlr->list, &spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002794 list_for_each_entry(bi, &board_list, list)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002795 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +08002796 mutex_unlock(&board_lock);
2797
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002798 /* Register devices from the device tree and ACPI */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002799 of_register_spi_devices(ctlr);
2800 acpi_register_spi_devices(ctlr);
Aaro Koskinenf9981d42020-03-04 13:17:40 +02002801 return status;
2802
2803free_bus_id:
2804 mutex_lock(&board_lock);
2805 idr_remove(&spi_master_idr, ctlr->bus_num);
2806 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002807 return status;
2808}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002809EXPORT_SYMBOL_GPL(spi_register_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002810
Mark Brown666d5b42013-08-31 18:50:52 +01002811static void devm_spi_unregister(struct device *dev, void *res)
2812{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002813 spi_unregister_controller(*(struct spi_controller **)res);
Mark Brown666d5b42013-08-31 18:50:52 +01002814}
2815
2816/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002817 * devm_spi_register_controller - register managed SPI master or slave
2818 * controller
2819 * @dev: device managing SPI controller
2820 * @ctlr: initialized controller, originally from spi_alloc_master() or
2821 * spi_alloc_slave()
Mark Brown666d5b42013-08-31 18:50:52 +01002822 * Context: can sleep
2823 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002824 * Register a SPI device as with spi_register_controller() which will
Johan Hovold68b892f2017-10-30 11:35:26 +01002825 * automatically be unregistered and freed.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002826 *
2827 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01002828 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002829int devm_spi_register_controller(struct device *dev,
2830 struct spi_controller *ctlr)
Mark Brown666d5b42013-08-31 18:50:52 +01002831{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002832 struct spi_controller **ptr;
Mark Brown666d5b42013-08-31 18:50:52 +01002833 int ret;
2834
2835 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2836 if (!ptr)
2837 return -ENOMEM;
2838
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002839 ret = spi_register_controller(ctlr);
Stephen Warren4b928942013-11-21 16:11:15 -07002840 if (!ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002841 *ptr = ctlr;
Mark Brown666d5b42013-08-31 18:50:52 +01002842 devres_add(dev, ptr);
2843 } else {
2844 devres_free(ptr);
2845 }
2846
2847 return ret;
2848}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002849EXPORT_SYMBOL_GPL(devm_spi_register_controller);
Mark Brown666d5b42013-08-31 18:50:52 +01002850
David Lamparter34860082010-08-30 23:54:17 +02002851static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002852{
David Lamparter34860082010-08-30 23:54:17 +02002853 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002854 return 0;
2855}
2856
2857/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002858 * spi_unregister_controller - unregister SPI master or slave controller
2859 * @ctlr: the controller being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002860 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002861 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002862 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002863 * only ones directly touching chip registers.
2864 *
2865 * This must be called from context that can sleep.
Johan Hovold68b892f2017-10-30 11:35:26 +01002866 *
2867 * Note that this function also drops a reference to the controller.
David Brownell8ae12a02006-01-08 13:34:19 -08002868 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002869void spi_unregister_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002870{
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302871 struct spi_controller *found;
Johan Hovold67f7b272017-10-30 11:35:25 +01002872 int id = ctlr->bus_num;
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002873
Lukas Wunnerddf75be2020-08-03 13:09:01 +02002874 /* Prevent addition of new devices, unregister existing ones */
2875 if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
2876 mutex_lock(&spi_add_lock);
2877
Lukas Wunner84855672020-05-15 17:58:01 +02002878 device_for_each_child(&ctlr->dev, NULL, __unregister);
2879
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302880 /* First make sure that this controller was ever added */
2881 mutex_lock(&board_lock);
Johan Hovold67f7b272017-10-30 11:35:25 +01002882 found = idr_find(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302883 mutex_unlock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002884 if (ctlr->queued) {
2885 if (spi_destroy_queue(ctlr))
2886 dev_err(&ctlr->dev, "queue remove failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002887 }
Feng Tang2b9603a2010-08-02 15:52:15 +08002888 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002889 list_del(&ctlr->list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002890 mutex_unlock(&board_lock);
2891
Lukas Wunner5e844cc2020-11-11 20:07:10 +01002892 device_del(&ctlr->dev);
2893
2894 /* Release the last reference on the controller if its driver
2895 * has not yet been converted to devm_spi_alloc_master/slave().
2896 */
William A. Kennington IIIc7fabe32021-04-07 02:55:27 -07002897 if (!ctlr->devm_allocated)
Lukas Wunner5e844cc2020-11-11 20:07:10 +01002898 put_device(&ctlr->dev);
2899
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302900 /* free bus id */
2901 mutex_lock(&board_lock);
Jarkko Nikula613bd1e2018-03-20 10:27:50 +02002902 if (found == ctlr)
2903 idr_remove(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302904 mutex_unlock(&board_lock);
Lukas Wunnerddf75be2020-08-03 13:09:01 +02002905
2906 if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
2907 mutex_unlock(&spi_add_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002908}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002909EXPORT_SYMBOL_GPL(spi_unregister_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002910
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002911int spi_controller_suspend(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002912{
2913 int ret;
2914
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002915 /* Basically no-ops for non-queued controllers */
2916 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002917 return 0;
2918
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002919 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002920 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002921 dev_err(&ctlr->dev, "queue stop failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002922
2923 return ret;
2924}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002925EXPORT_SYMBOL_GPL(spi_controller_suspend);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002926
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002927int spi_controller_resume(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002928{
2929 int ret;
2930
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002931 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002932 return 0;
2933
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002934 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002935 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002936 dev_err(&ctlr->dev, "queue restart failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002937
2938 return ret;
2939}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002940EXPORT_SYMBOL_GPL(spi_controller_resume);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002941
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002942static int __spi_controller_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002943{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002944 struct spi_controller *ctlr;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002945 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002946
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002947 ctlr = container_of(dev, struct spi_controller, dev);
2948 return ctlr->bus_num == *bus_num;
Dave Young5ed2c832008-01-22 15:14:18 +08002949}
2950
David Brownell8ae12a02006-01-08 13:34:19 -08002951/**
2952 * spi_busnum_to_master - look up master associated with bus_num
2953 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002954 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002955 *
2956 * This call may be used with devices that are registered after
2957 * arch init time. It returns a refcounted pointer to the relevant
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002958 * spi_controller (which the caller must release), or NULL if there is
David Brownell8ae12a02006-01-08 13:34:19 -08002959 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002960 *
2961 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002962 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002963struct spi_controller *spi_busnum_to_master(u16 bus_num)
David Brownell8ae12a02006-01-08 13:34:19 -08002964{
Tony Jones49dce682007-10-16 01:27:48 -07002965 struct device *dev;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002966 struct spi_controller *ctlr = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002967
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002968 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002969 __spi_controller_match);
Dave Young5ed2c832008-01-22 15:14:18 +08002970 if (dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002971 ctlr = container_of(dev, struct spi_controller, dev);
Dave Young5ed2c832008-01-22 15:14:18 +08002972 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002973 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002974}
2975EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2976
Martin Sperld780c372015-12-14 15:20:18 +00002977/*-------------------------------------------------------------------------*/
2978
2979/* Core methods for SPI resource management */
2980
2981/**
2982 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2983 * during the processing of a spi_message while using
2984 * spi_transfer_one
2985 * @spi: the spi device for which we allocate memory
2986 * @release: the release code to execute for this resource
2987 * @size: size to alloc and return
2988 * @gfp: GFP allocation flags
2989 *
2990 * Return: the pointer to the allocated data
2991 *
2992 * This may get enhanced in the future to allocate from a memory pool
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002993 * of the @spi_device or @spi_controller to avoid repeated allocations.
Martin Sperld780c372015-12-14 15:20:18 +00002994 */
2995void *spi_res_alloc(struct spi_device *spi,
2996 spi_res_release_t release,
2997 size_t size, gfp_t gfp)
2998{
2999 struct spi_res *sres;
3000
3001 sres = kzalloc(sizeof(*sres) + size, gfp);
3002 if (!sres)
3003 return NULL;
3004
3005 INIT_LIST_HEAD(&sres->entry);
3006 sres->release = release;
3007
3008 return sres->data;
3009}
3010EXPORT_SYMBOL_GPL(spi_res_alloc);
3011
3012/**
3013 * spi_res_free - free an spi resource
3014 * @res: pointer to the custom data of a resource
3015 *
3016 */
3017void spi_res_free(void *res)
3018{
3019 struct spi_res *sres = container_of(res, struct spi_res, data);
3020
3021 if (!res)
3022 return;
3023
3024 WARN_ON(!list_empty(&sres->entry));
3025 kfree(sres);
3026}
3027EXPORT_SYMBOL_GPL(spi_res_free);
3028
3029/**
3030 * spi_res_add - add a spi_res to the spi_message
3031 * @message: the spi message
3032 * @res: the spi_resource
3033 */
3034void spi_res_add(struct spi_message *message, void *res)
3035{
3036 struct spi_res *sres = container_of(res, struct spi_res, data);
3037
3038 WARN_ON(!list_empty(&sres->entry));
3039 list_add_tail(&sres->entry, &message->resources);
3040}
3041EXPORT_SYMBOL_GPL(spi_res_add);
3042
3043/**
3044 * spi_res_release - release all spi resources for this message
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003045 * @ctlr: the @spi_controller
Martin Sperld780c372015-12-14 15:20:18 +00003046 * @message: the @spi_message
3047 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003048void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
Martin Sperld780c372015-12-14 15:20:18 +00003049{
Vladimir Zapolskiyf5694362019-06-18 19:28:18 +03003050 struct spi_res *res, *tmp;
Martin Sperld780c372015-12-14 15:20:18 +00003051
Vladimir Zapolskiyf5694362019-06-18 19:28:18 +03003052 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
Martin Sperld780c372015-12-14 15:20:18 +00003053 if (res->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003054 res->release(ctlr, message, res->data);
Martin Sperld780c372015-12-14 15:20:18 +00003055
3056 list_del(&res->entry);
3057
3058 kfree(res);
3059 }
3060}
3061EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08003062
3063/*-------------------------------------------------------------------------*/
3064
Martin Sperl523baf5a2015-12-14 15:20:19 +00003065/* Core methods for spi_message alterations */
3066
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003067static void __spi_replace_transfers_release(struct spi_controller *ctlr,
Martin Sperl523baf5a2015-12-14 15:20:19 +00003068 struct spi_message *msg,
3069 void *res)
3070{
3071 struct spi_replaced_transfers *rxfer = res;
3072 size_t i;
3073
3074 /* call extra callback if requested */
3075 if (rxfer->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003076 rxfer->release(ctlr, msg, res);
Martin Sperl523baf5a2015-12-14 15:20:19 +00003077
3078 /* insert replaced transfers back into the message */
3079 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
3080
3081 /* remove the formerly inserted entries */
3082 for (i = 0; i < rxfer->inserted; i++)
3083 list_del(&rxfer->inserted_transfers[i].transfer_list);
3084}
3085
3086/**
3087 * spi_replace_transfers - replace transfers with several transfers
3088 * and register change with spi_message.resources
3089 * @msg: the spi_message we work upon
3090 * @xfer_first: the first spi_transfer we want to replace
3091 * @remove: number of transfers to remove
3092 * @insert: the number of transfers we want to insert instead
3093 * @release: extra release code necessary in some circumstances
3094 * @extradatasize: extra data to allocate (with alignment guarantees
3095 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00003096 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00003097 *
3098 * Returns: pointer to @spi_replaced_transfers,
3099 * PTR_ERR(...) in case of errors.
3100 */
3101struct spi_replaced_transfers *spi_replace_transfers(
3102 struct spi_message *msg,
3103 struct spi_transfer *xfer_first,
3104 size_t remove,
3105 size_t insert,
3106 spi_replaced_release_t release,
3107 size_t extradatasize,
3108 gfp_t gfp)
3109{
3110 struct spi_replaced_transfers *rxfer;
3111 struct spi_transfer *xfer;
3112 size_t i;
3113
3114 /* allocate the structure using spi_res */
3115 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
Gustavo A. R. Silvaaef97522019-06-07 13:48:45 -05003116 struct_size(rxfer, inserted_transfers, insert)
Martin Sperl523baf5a2015-12-14 15:20:19 +00003117 + extradatasize,
3118 gfp);
3119 if (!rxfer)
3120 return ERR_PTR(-ENOMEM);
3121
3122 /* the release code to invoke before running the generic release */
3123 rxfer->release = release;
3124
3125 /* assign extradata */
3126 if (extradatasize)
3127 rxfer->extradata =
3128 &rxfer->inserted_transfers[insert];
3129
3130 /* init the replaced_transfers list */
3131 INIT_LIST_HEAD(&rxfer->replaced_transfers);
3132
3133 /* assign the list_entry after which we should reinsert
3134 * the @replaced_transfers - it may be spi_message.messages!
3135 */
3136 rxfer->replaced_after = xfer_first->transfer_list.prev;
3137
3138 /* remove the requested number of transfers */
3139 for (i = 0; i < remove; i++) {
3140 /* if the entry after replaced_after it is msg->transfers
3141 * then we have been requested to remove more transfers
3142 * than are in the list
3143 */
3144 if (rxfer->replaced_after->next == &msg->transfers) {
3145 dev_err(&msg->spi->dev,
3146 "requested to remove more spi_transfers than are available\n");
3147 /* insert replaced transfers back into the message */
3148 list_splice(&rxfer->replaced_transfers,
3149 rxfer->replaced_after);
3150
3151 /* free the spi_replace_transfer structure */
3152 spi_res_free(rxfer);
3153
3154 /* and return with an error */
3155 return ERR_PTR(-EINVAL);
3156 }
3157
3158 /* remove the entry after replaced_after from list of
3159 * transfers and add it to list of replaced_transfers
3160 */
3161 list_move_tail(rxfer->replaced_after->next,
3162 &rxfer->replaced_transfers);
3163 }
3164
3165 /* create copy of the given xfer with identical settings
3166 * based on the first transfer to get removed
3167 */
3168 for (i = 0; i < insert; i++) {
3169 /* we need to run in reverse order */
3170 xfer = &rxfer->inserted_transfers[insert - 1 - i];
3171
3172 /* copy all spi_transfer data */
3173 memcpy(xfer, xfer_first, sizeof(*xfer));
3174
3175 /* add to list */
3176 list_add(&xfer->transfer_list, rxfer->replaced_after);
3177
Alexandru Ardeleanbebcfd22019-09-26 13:51:36 +03003178 /* clear cs_change and delay for all but the last */
Martin Sperl523baf5a2015-12-14 15:20:19 +00003179 if (i) {
3180 xfer->cs_change = false;
3181 xfer->delay_usecs = 0;
Alexandru Ardeleanbebcfd22019-09-26 13:51:36 +03003182 xfer->delay.value = 0;
Martin Sperl523baf5a2015-12-14 15:20:19 +00003183 }
3184 }
3185
3186 /* set up inserted */
3187 rxfer->inserted = insert;
3188
3189 /* and register it with spi_res/spi_message */
3190 spi_res_add(msg, rxfer);
3191
3192 return rxfer;
3193}
3194EXPORT_SYMBOL_GPL(spi_replace_transfers);
3195
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003196static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
Fabio Estevam08933412016-02-14 13:33:50 -02003197 struct spi_message *msg,
3198 struct spi_transfer **xferp,
3199 size_t maxsize,
3200 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00003201{
3202 struct spi_transfer *xfer = *xferp, *xfers;
3203 struct spi_replaced_transfers *srt;
3204 size_t offset;
3205 size_t count, i;
3206
Martin Sperld9f12122015-12-14 15:20:20 +00003207 /* calculate how many we have to replace */
3208 count = DIV_ROUND_UP(xfer->len, maxsize);
3209
3210 /* create replacement */
3211 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03003212 if (IS_ERR(srt))
3213 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00003214 xfers = srt->inserted_transfers;
3215
3216 /* now handle each of those newly inserted spi_transfers
3217 * note that the replacements spi_transfers all are preset
3218 * to the same values as *xferp, so tx_buf, rx_buf and len
3219 * are all identical (as well as most others)
3220 * so we just have to fix up len and the pointers.
3221 *
3222 * this also includes support for the depreciated
3223 * spi_message.is_dma_mapped interface
3224 */
3225
3226 /* the first transfer just needs the length modified, so we
3227 * run it outside the loop
3228 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02003229 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00003230
3231 /* all the others need rx_buf/tx_buf also set */
3232 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
3233 /* update rx_buf, tx_buf and dma */
3234 if (xfers[i].rx_buf)
3235 xfers[i].rx_buf += offset;
3236 if (xfers[i].rx_dma)
3237 xfers[i].rx_dma += offset;
3238 if (xfers[i].tx_buf)
3239 xfers[i].tx_buf += offset;
3240 if (xfers[i].tx_dma)
3241 xfers[i].tx_dma += offset;
3242
3243 /* update length */
3244 xfers[i].len = min(maxsize, xfers[i].len - offset);
3245 }
3246
3247 /* we set up xferp to the last entry we have inserted,
3248 * so that we skip those already split transfers
3249 */
3250 *xferp = &xfers[count - 1];
3251
3252 /* increment statistics counters */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003253 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperld9f12122015-12-14 15:20:20 +00003254 transfers_split_maxsize);
3255 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
3256 transfers_split_maxsize);
3257
3258 return 0;
3259}
3260
3261/**
3262 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
3263 * when an individual transfer exceeds a
3264 * certain size
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003265 * @ctlr: the @spi_controller for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09003266 * @msg: the @spi_message to transform
3267 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03003268 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00003269 *
3270 * Return: status of transformation
3271 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003272int spi_split_transfers_maxsize(struct spi_controller *ctlr,
Martin Sperld9f12122015-12-14 15:20:20 +00003273 struct spi_message *msg,
3274 size_t maxsize,
3275 gfp_t gfp)
3276{
3277 struct spi_transfer *xfer;
3278 int ret;
3279
3280 /* iterate over the transfer_list,
3281 * but note that xfer is advanced to the last transfer inserted
3282 * to avoid checking sizes again unnecessarily (also xfer does
3283 * potentiall belong to a different list by the time the
3284 * replacement has happened
3285 */
3286 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3287 if (xfer->len > maxsize) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003288 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3289 maxsize, gfp);
Martin Sperld9f12122015-12-14 15:20:20 +00003290 if (ret)
3291 return ret;
3292 }
3293 }
3294
3295 return 0;
3296}
3297EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08003298
3299/*-------------------------------------------------------------------------*/
3300
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003301/* Core methods for SPI controller protocol drivers. Some of the
David Brownell7d077192009-06-17 16:26:03 -07003302 * other core methods are currently defined as inline functions.
3303 */
3304
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003305static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
3306 u8 bits_per_word)
Stefan Brüns63ab6452015-08-23 16:06:30 +02003307{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003308 if (ctlr->bits_per_word_mask) {
Stefan Brüns63ab6452015-08-23 16:06:30 +02003309 /* Only 32 bits fit in the mask */
3310 if (bits_per_word > 32)
3311 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003312 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
Stefan Brüns63ab6452015-08-23 16:06:30 +02003313 return -EINVAL;
3314 }
3315
3316 return 0;
3317}
3318
David Brownell7d077192009-06-17 16:26:03 -07003319/**
3320 * spi_setup - setup SPI mode and clock rate
3321 * @spi: the device whose settings are being modified
3322 * Context: can sleep, and no requests are queued to the device
3323 *
3324 * SPI protocol drivers may need to update the transfer mode if the
3325 * device doesn't work with its default. They may likewise need
3326 * to update clock rates or word sizes from initial values. This function
3327 * changes those settings, and must be called from a context that can sleep.
3328 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
3329 * effect the next time the device is selected and data is transferred to
3330 * or from it. When this function returns, the spi device is deselected.
3331 *
3332 * Note that this call will fail if the protocol driver specifies an option
3333 * that the underlying controller or its driver does not support. For
3334 * example, not all hardware supports wire transfers using nine bit words,
3335 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003336 *
3337 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07003338 */
3339int spi_setup(struct spi_device *spi)
3340{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003341 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03003342 int status;
David Brownell7d077192009-06-17 16:26:03 -07003343
wangyuhangf477b7f2013-08-11 18:15:17 +08003344 /* check mode to prevent that DUAL and QUAD set at the same time
3345 */
3346 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
3347 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
3348 dev_err(&spi->dev,
3349 "setup: can not select dual and quad at the same time\n");
3350 return -EINVAL;
3351 }
3352 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
3353 */
3354 if ((spi->mode & SPI_3WIRE) && (spi->mode &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00003355 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3356 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
wangyuhangf477b7f2013-08-11 18:15:17 +08003357 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07003358 /* help drivers fail *cleanly* when they need options
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003359 * that aren't supported with their current controller
David Lechnercbaa62e2018-09-12 19:39:18 -05003360 * SPI_CS_WORD has a fallback software implementation,
3361 * so it is ignored here.
David Brownelle7db06b2009-06-17 16:26:04 -07003362 */
David Lechnercbaa62e2018-09-12 19:39:18 -05003363 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
Serge Semind61ad23c2019-04-26 13:30:07 +03003364 /* nothing prevents from working with active-high CS in case if it
3365 * is driven by GPIO.
3366 */
3367 if (gpio_is_valid(spi->cs_gpio))
3368 bad_bits &= ~SPI_CS_HIGH;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003369 ugly_bits = bad_bits &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00003370 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3371 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003372 if (ugly_bits) {
3373 dev_warn(&spi->dev,
3374 "setup: ignoring unsupported mode bits %x\n",
3375 ugly_bits);
3376 spi->mode &= ~ugly_bits;
3377 bad_bits &= ~ugly_bits;
3378 }
David Brownelle7db06b2009-06-17 16:26:04 -07003379 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02003380 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07003381 bad_bits);
3382 return -EINVAL;
3383 }
3384
David Brownell7d077192009-06-17 16:26:03 -07003385 if (!spi->bits_per_word)
3386 spi->bits_per_word = 8;
3387
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003388 status = __spi_validate_bits_per_word(spi->controller,
3389 spi->bits_per_word);
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03003390 if (status)
3391 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02003392
Axel Lin052eb2d2014-02-10 00:08:05 +08003393 if (!spi->max_speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003394 spi->max_speed_hz = spi->controller->max_speed_hz;
Axel Lin052eb2d2014-02-10 00:08:05 +08003395
Serge Semin4fae3a52020-11-17 12:45:17 +03003396 mutex_lock(&spi->controller->io_mutex);
3397
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003398 if (spi->controller->setup)
3399 status = spi->controller->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07003400
Luhua Xud948e6c2019-10-30 17:03:54 +08003401 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
3402 status = pm_runtime_get_sync(spi->controller->dev.parent);
3403 if (status < 0) {
Serge Semin4fae3a52020-11-17 12:45:17 +03003404 mutex_unlock(&spi->controller->io_mutex);
Luhua Xud948e6c2019-10-30 17:03:54 +08003405 pm_runtime_put_noidle(spi->controller->dev.parent);
3406 dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3407 status);
3408 return status;
3409 }
Tony Lindgren57a94602019-11-11 11:53:34 -08003410
3411 /*
3412 * We do not want to return positive value from pm_runtime_get,
3413 * there are many instances of devices calling spi_setup() and
3414 * checking for a non-zero return value instead of a negative
3415 * return value.
3416 */
3417 status = 0;
3418
David Bauerc5aa9ea2021-04-16 21:59:56 +02003419 spi_set_cs(spi, false, true);
Luhua Xud948e6c2019-10-30 17:03:54 +08003420 pm_runtime_mark_last_busy(spi->controller->dev.parent);
3421 pm_runtime_put_autosuspend(spi->controller->dev.parent);
3422 } else {
David Bauerc5aa9ea2021-04-16 21:59:56 +02003423 spi_set_cs(spi, false, true);
Luhua Xud948e6c2019-10-30 17:03:54 +08003424 }
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05003425
Serge Semin4fae3a52020-11-17 12:45:17 +03003426 mutex_unlock(&spi->controller->io_mutex);
3427
Douglas Anderson924b5862019-05-15 09:48:12 -07003428 if (spi->rt && !spi->controller->rt) {
3429 spi->controller->rt = true;
3430 spi_set_thread_rt(spi->controller);
3431 }
3432
Jingoo Han5fe5f052013-10-14 10:31:51 +09003433 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 -07003434 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
3435 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
3436 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
3437 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
3438 (spi->mode & SPI_LOOP) ? "loopback, " : "",
3439 spi->bits_per_word, spi->max_speed_hz,
3440 status);
3441
3442 return status;
3443}
3444EXPORT_SYMBOL_GPL(spi_setup);
3445
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003446/**
3447 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3448 * @spi: the device that requires specific CS timing configuration
Alexandru Ardelean81059362019-09-26 13:51:42 +03003449 * @setup: CS setup time specified via @spi_delay
3450 * @hold: CS hold time specified via @spi_delay
3451 * @inactive: CS inactive delay between transfers specified via @spi_delay
3452 *
3453 * Return: zero on success, else a negative error code.
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003454 */
Alexandru Ardelean81059362019-09-26 13:51:42 +03003455int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup,
3456 struct spi_delay *hold, struct spi_delay *inactive)
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003457{
Alexandru Ardelean25093bd2019-09-26 13:51:43 +03003458 size_t len;
3459
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003460 if (spi->controller->set_cs_timing)
Alexandru Ardelean81059362019-09-26 13:51:42 +03003461 return spi->controller->set_cs_timing(spi, setup, hold,
3462 inactive);
Alexandru Ardelean25093bd2019-09-26 13:51:43 +03003463
3464 if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) ||
3465 (hold && hold->unit == SPI_DELAY_UNIT_SCK) ||
3466 (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) {
3467 dev_err(&spi->dev,
3468 "Clock-cycle delays for CS not supported in SW mode\n");
3469 return -ENOTSUPP;
3470 }
3471
3472 len = sizeof(struct spi_delay);
3473
3474 /* copy delays to controller */
3475 if (setup)
3476 memcpy(&spi->controller->cs_setup, setup, len);
3477 else
3478 memset(&spi->controller->cs_setup, 0, len);
3479
3480 if (hold)
3481 memcpy(&spi->controller->cs_hold, hold, len);
3482 else
3483 memset(&spi->controller->cs_hold, 0, len);
3484
3485 if (inactive)
3486 memcpy(&spi->controller->cs_inactive, inactive, len);
3487 else
3488 memset(&spi->controller->cs_inactive, 0, len);
3489
3490 return 0;
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003491}
3492EXPORT_SYMBOL_GPL(spi_set_cs_timing);
3493
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003494static int _spi_xfer_word_delay_update(struct spi_transfer *xfer,
3495 struct spi_device *spi)
3496{
3497 int delay1, delay2;
3498
Alexandru Ardelean3984d392019-09-26 13:51:44 +03003499 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer);
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003500 if (delay1 < 0)
3501 return delay1;
3502
Alexandru Ardelean3984d392019-09-26 13:51:44 +03003503 delay2 = spi_delay_to_ns(&spi->word_delay, xfer);
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003504 if (delay2 < 0)
3505 return delay2;
3506
3507 if (delay1 < delay2)
3508 memcpy(&xfer->word_delay, &spi->word_delay,
3509 sizeof(xfer->word_delay));
3510
3511 return 0;
3512}
3513
Mark Brown90808732013-11-13 23:44:15 +00003514static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003515{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003516 struct spi_controller *ctlr = spi->controller;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303517 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09003518 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003519
Mark Brown24a00132013-07-10 15:05:40 +01003520 if (list_empty(&message->transfers))
3521 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01003522
David Lechnercbaa62e2018-09-12 19:39:18 -05003523 /* If an SPI controller does not support toggling the CS line on each
David Lechner71388b212018-09-18 12:08:48 -05003524 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
3525 * for the CS line, we can emulate the CS-per-word hardware function by
David Lechnercbaa62e2018-09-12 19:39:18 -05003526 * splitting transfers into one-word transfers and ensuring that
3527 * cs_change is set for each transfer.
3528 */
David Lechner71388b212018-09-18 12:08:48 -05003529 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
Linus Walleijf3186dd2019-01-07 16:51:50 +01003530 spi->cs_gpiod ||
David Lechner71388b212018-09-18 12:08:48 -05003531 gpio_is_valid(spi->cs_gpio))) {
David Lechnercbaa62e2018-09-12 19:39:18 -05003532 size_t maxsize;
3533 int ret;
3534
3535 maxsize = (spi->bits_per_word + 7) / 8;
3536
3537 /* spi_split_transfers_maxsize() requires message->spi */
3538 message->spi = spi;
3539
3540 ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
3541 GFP_KERNEL);
3542 if (ret)
3543 return ret;
3544
3545 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3546 /* don't change cs_change on the last entry in the list */
3547 if (list_is_last(&xfer->transfer_list, &message->transfers))
3548 break;
3549 xfer->cs_change = 1;
3550 }
3551 }
3552
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003553 /* Half-duplex links include original MicroWire, and ones with
3554 * only one data pin like SPI_3WIRE (switches direction) or where
3555 * either MOSI or MISO is missing. They can also be caused by
3556 * software limitations.
3557 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003558 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
3559 (spi->mode & SPI_3WIRE)) {
3560 unsigned flags = ctlr->flags;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003561
3562 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3563 if (xfer->rx_buf && xfer->tx_buf)
3564 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003565 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003566 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003567 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003568 return -EINVAL;
3569 }
3570 }
3571
Laxman Dewangane6811d12012-11-09 14:36:45 +05303572 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303573 * Set transfer bits_per_word and max speed as spi device default if
3574 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08003575 * Set transfer tx_nbits and rx_nbits as single transfer default
3576 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Jonas Bonnb7bb3672019-01-30 09:40:04 +01003577 * Ensure transfer word_delay is at least as long as that required by
3578 * device itself.
Laxman Dewangane6811d12012-11-09 14:36:45 +05303579 */
Martin Sperl77e80582015-11-27 12:31:09 +00003580 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303581 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Martin Sperl5d7e2b52019-02-23 08:49:49 +00003582 xfer->effective_speed_hz = 0;
Sourav Poddar078726c2013-07-18 15:31:25 +05303583 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303584 if (!xfer->bits_per_word)
3585 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08003586
3587 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303588 xfer->speed_hz = spi->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08003589
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003590 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
3591 xfer->speed_hz = ctlr->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02003592
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003593 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
Stefan Brüns63ab6452015-08-23 16:06:30 +02003594 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01003595
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003596 /*
3597 * SPI transfer length should be multiple of SPI word size
3598 * where SPI word size should be power-of-two multiple
3599 */
3600 if (xfer->bits_per_word <= 8)
3601 w_size = 1;
3602 else if (xfer->bits_per_word <= 16)
3603 w_size = 2;
3604 else
3605 w_size = 4;
3606
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003607 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09003608 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003609 return -EINVAL;
3610
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003611 if (xfer->speed_hz && ctlr->min_speed_hz &&
3612 xfer->speed_hz < ctlr->min_speed_hz)
Mark Browna2fd4f92013-07-10 14:57:26 +01003613 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08003614
3615 if (xfer->tx_buf && !xfer->tx_nbits)
3616 xfer->tx_nbits = SPI_NBITS_SINGLE;
3617 if (xfer->rx_buf && !xfer->rx_nbits)
3618 xfer->rx_nbits = SPI_NBITS_SINGLE;
3619 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01003620 * 1. check the value matches one of single, dual and quad
3621 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08003622 */
Sourav Poddardb90a442013-08-22 21:20:48 +05303623 if (xfer->tx_buf) {
3624 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
3625 xfer->tx_nbits != SPI_NBITS_DUAL &&
3626 xfer->tx_nbits != SPI_NBITS_QUAD)
3627 return -EINVAL;
3628 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
3629 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3630 return -EINVAL;
3631 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
3632 !(spi->mode & SPI_TX_QUAD))
3633 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303634 }
wangyuhangf477b7f2013-08-11 18:15:17 +08003635 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05303636 if (xfer->rx_buf) {
3637 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
3638 xfer->rx_nbits != SPI_NBITS_DUAL &&
3639 xfer->rx_nbits != SPI_NBITS_QUAD)
3640 return -EINVAL;
3641 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
3642 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3643 return -EINVAL;
3644 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
3645 !(spi->mode & SPI_RX_QUAD))
3646 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303647 }
Jonas Bonnb7bb3672019-01-30 09:40:04 +01003648
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003649 if (_spi_xfer_word_delay_update(xfer, spi))
3650 return -EINVAL;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303651 }
3652
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003653 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00003654
3655 return 0;
3656}
3657
3658static int __spi_async(struct spi_device *spi, struct spi_message *message)
3659{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003660 struct spi_controller *ctlr = spi->controller;
Vladimir Olteanb42faee2019-09-05 04:01:12 +03003661 struct spi_transfer *xfer;
Mark Brown90808732013-11-13 23:44:15 +00003662
Boris Brezillonb5932f52018-04-26 18:18:15 +02003663 /*
3664 * Some controllers do not support doing regular SPI transfers. Return
3665 * ENOTSUPP when this is the case.
3666 */
3667 if (!ctlr->transfer)
3668 return -ENOTSUPP;
3669
Mark Brown90808732013-11-13 23:44:15 +00003670 message->spi = spi;
3671
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003672 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003673 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3674
Mark Brown90808732013-11-13 23:44:15 +00003675 trace_spi_message_submit(message);
3676
Vladimir Olteanb42faee2019-09-05 04:01:12 +03003677 if (!ctlr->ptp_sts_supported) {
3678 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3679 xfer->ptp_sts_word_pre = 0;
3680 ptp_read_system_prets(xfer->ptp_sts);
3681 }
3682 }
3683
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003684 return ctlr->transfer(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003685}
3686
David Brownell568d0692009-09-22 16:46:18 -07003687/**
3688 * spi_async - asynchronous SPI transfer
3689 * @spi: device with which data will be exchanged
3690 * @message: describes the data transfers, including completion callback
3691 * Context: any (irqs may be blocked, etc)
3692 *
3693 * This call may be used in_irq and other contexts which can't sleep,
3694 * as well as from task contexts which can sleep.
3695 *
3696 * The completion callback is invoked in a context which can't sleep.
3697 * Before that invocation, the value of message->status is undefined.
3698 * When the callback is issued, message->status holds either zero (to
3699 * indicate complete success) or a negative error code. After that
3700 * callback returns, the driver which issued the transfer request may
3701 * deallocate the associated memory; it's no longer in use by any SPI
3702 * core or controller driver code.
3703 *
3704 * Note that although all messages to a spi_device are handled in
3705 * FIFO order, messages may go to different devices in other orders.
3706 * Some device might be higher priority, or have various "hard" access
3707 * time requirements, for example.
3708 *
3709 * On detection of any fault during the transfer, processing of
3710 * the entire message is aborted, and the device is deselected.
3711 * Until returning from the associated message completion callback,
3712 * no other spi_message queued to that device will be processed.
3713 * (This rule applies equally to all the synchronous transfer calls,
3714 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003715 *
3716 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07003717 */
3718int spi_async(struct spi_device *spi, struct spi_message *message)
3719{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003720 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003721 int ret;
3722 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07003723
Mark Brown90808732013-11-13 23:44:15 +00003724 ret = __spi_validate(spi, message);
3725 if (ret != 0)
3726 return ret;
3727
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003728 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07003729
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003730 if (ctlr->bus_lock_flag)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003731 ret = -EBUSY;
3732 else
3733 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07003734
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003735 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003736
3737 return ret;
David Brownell568d0692009-09-22 16:46:18 -07003738}
3739EXPORT_SYMBOL_GPL(spi_async);
3740
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003741/**
3742 * spi_async_locked - version of spi_async with exclusive bus usage
3743 * @spi: device with which data will be exchanged
3744 * @message: describes the data transfers, including completion callback
3745 * Context: any (irqs may be blocked, etc)
3746 *
3747 * This call may be used in_irq and other contexts which can't sleep,
3748 * as well as from task contexts which can sleep.
3749 *
3750 * The completion callback is invoked in a context which can't sleep.
3751 * Before that invocation, the value of message->status is undefined.
3752 * When the callback is issued, message->status holds either zero (to
3753 * indicate complete success) or a negative error code. After that
3754 * callback returns, the driver which issued the transfer request may
3755 * deallocate the associated memory; it's no longer in use by any SPI
3756 * core or controller driver code.
3757 *
3758 * Note that although all messages to a spi_device are handled in
3759 * FIFO order, messages may go to different devices in other orders.
3760 * Some device might be higher priority, or have various "hard" access
3761 * time requirements, for example.
3762 *
3763 * On detection of any fault during the transfer, processing of
3764 * the entire message is aborted, and the device is deselected.
3765 * Until returning from the associated message completion callback,
3766 * no other spi_message queued to that device will be processed.
3767 * (This rule applies equally to all the synchronous transfer calls,
3768 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003769 *
3770 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003771 */
3772int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3773{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003774 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003775 int ret;
3776 unsigned long flags;
3777
Mark Brown90808732013-11-13 23:44:15 +00003778 ret = __spi_validate(spi, message);
3779 if (ret != 0)
3780 return ret;
3781
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003782 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003783
3784 ret = __spi_async(spi, message);
3785
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003786 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003787
3788 return ret;
3789
3790}
3791EXPORT_SYMBOL_GPL(spi_async_locked);
3792
David Brownell7d077192009-06-17 16:26:03 -07003793/*-------------------------------------------------------------------------*/
3794
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003795/* Utility methods for SPI protocol drivers, layered on
David Brownell7d077192009-06-17 16:26:03 -07003796 * top of the core. Some other utility methods are defined as
3797 * inline functions.
3798 */
3799
Andrew Morton5d870c82006-01-11 11:23:49 -08003800static void spi_complete(void *arg)
3801{
3802 complete(arg);
3803}
3804
Mark Brownef4d96e2016-07-21 23:53:31 +01003805static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003806{
3807 DECLARE_COMPLETION_ONSTACK(done);
3808 int status;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003809 struct spi_controller *ctlr = spi->controller;
Mark Brown0461a412014-12-09 21:38:05 +00003810 unsigned long flags;
3811
3812 status = __spi_validate(spi, message);
3813 if (status != 0)
3814 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003815
3816 message->complete = spi_complete;
3817 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00003818 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003819
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003820 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003821 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3822
Mark Brown0461a412014-12-09 21:38:05 +00003823 /* If we're not using the legacy transfer method then we will
3824 * try to transfer in the calling context so special case.
3825 * This code would be less tricky if we could remove the
3826 * support for driver implemented message queues.
3827 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003828 if (ctlr->transfer == spi_queued_transfer) {
3829 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003830
3831 trace_spi_message_submit(message);
3832
3833 status = __spi_queued_transfer(spi, message, false);
3834
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003835 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003836 } else {
3837 status = spi_async_locked(spi, message);
3838 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003839
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003840 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00003841 /* Push out the messages in the calling context if we
3842 * can.
3843 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003844 if (ctlr->transfer == spi_queued_transfer) {
3845 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperleca2ebc2015-06-22 13:00:36 +00003846 spi_sync_immediate);
3847 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3848 spi_sync_immediate);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003849 __spi_pump_messages(ctlr, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003850 }
Mark Brown0461a412014-12-09 21:38:05 +00003851
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003852 wait_for_completion(&done);
3853 status = message->status;
3854 }
3855 message->context = NULL;
3856 return status;
3857}
3858
David Brownell8ae12a02006-01-08 13:34:19 -08003859/**
3860 * spi_sync - blocking/synchronous SPI data transfers
3861 * @spi: device with which data will be exchanged
3862 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07003863 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003864 *
3865 * This call may only be used from a context that may sleep. The sleep
3866 * is non-interruptible, and has no timeout. Low-overhead controller
3867 * drivers may DMA directly into and out of the message buffers.
3868 *
3869 * Note that the SPI device's chip select is active during the message,
3870 * and then is normally disabled between messages. Drivers for some
3871 * frequently-used devices may want to minimize costs of selecting a chip,
3872 * by leaving it selected in anticipation that the next message will go
3873 * to the same chip. (That may increase power usage.)
3874 *
David Brownell0c8684612006-01-08 13:34:25 -08003875 * Also, the caller is guaranteeing that the memory associated with the
3876 * message will not be freed before this call returns.
3877 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003878 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003879 */
3880int spi_sync(struct spi_device *spi, struct spi_message *message)
3881{
Mark Brownef4d96e2016-07-21 23:53:31 +01003882 int ret;
3883
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003884 mutex_lock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003885 ret = __spi_sync(spi, message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003886 mutex_unlock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003887
3888 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08003889}
3890EXPORT_SYMBOL_GPL(spi_sync);
3891
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003892/**
3893 * spi_sync_locked - version of spi_sync with exclusive bus usage
3894 * @spi: device with which data will be exchanged
3895 * @message: describes the data transfers
3896 * Context: can sleep
3897 *
3898 * This call may only be used from a context that may sleep. The sleep
3899 * is non-interruptible, and has no timeout. Low-overhead controller
3900 * drivers may DMA directly into and out of the message buffers.
3901 *
3902 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003903 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003904 * be released by a spi_bus_unlock call when the exclusive access is over.
3905 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003906 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003907 */
3908int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3909{
Mark Brownef4d96e2016-07-21 23:53:31 +01003910 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003911}
3912EXPORT_SYMBOL_GPL(spi_sync_locked);
3913
3914/**
3915 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003916 * @ctlr: SPI bus master that should be locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003917 * Context: can sleep
3918 *
3919 * This call may only be used from a context that may sleep. The sleep
3920 * is non-interruptible, and has no timeout.
3921 *
3922 * This call should be used by drivers that require exclusive access to the
3923 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3924 * exclusive access is over. Data transfer must be done by spi_sync_locked
3925 * and spi_async_locked calls when the SPI bus lock is held.
3926 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003927 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003928 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003929int spi_bus_lock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003930{
3931 unsigned long flags;
3932
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003933 mutex_lock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003934
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003935 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3936 ctlr->bus_lock_flag = 1;
3937 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003938
3939 /* mutex remains locked until spi_bus_unlock is called */
3940
3941 return 0;
3942}
3943EXPORT_SYMBOL_GPL(spi_bus_lock);
3944
3945/**
3946 * spi_bus_unlock - release the lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003947 * @ctlr: SPI bus master that was locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003948 * Context: can sleep
3949 *
3950 * This call may only be used from a context that may sleep. The sleep
3951 * is non-interruptible, and has no timeout.
3952 *
3953 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3954 * call.
3955 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003956 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003957 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003958int spi_bus_unlock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003959{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003960 ctlr->bus_lock_flag = 0;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003961
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003962 mutex_unlock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003963
3964 return 0;
3965}
3966EXPORT_SYMBOL_GPL(spi_bus_unlock);
3967
David Brownella9948b62006-04-02 10:37:40 -08003968/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003969#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003970
3971static u8 *buf;
3972
3973/**
3974 * spi_write_then_read - SPI synchronous write followed by read
3975 * @spi: device with which data will be exchanged
3976 * @txbuf: data to be written (need not be dma-safe)
3977 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003978 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3979 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003980 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003981 *
3982 * This performs a half duplex MicroWire style transaction with the
3983 * device, sending txbuf and then reading rxbuf. The return value
3984 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003985 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003986 *
Mark Brownc3736432020-05-25 14:31:20 +01003987 * Parameters to this routine are always copied using a small buffer.
David Brownell33e34dc2007-05-08 00:32:21 -07003988 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c8684612006-01-08 13:34:25 -08003989 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003990 *
3991 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003992 */
3993int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003994 const void *txbuf, unsigned n_tx,
3995 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003996{
David Brownell068f4072007-12-04 23:45:09 -08003997 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003998
3999 int status;
4000 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07004001 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08004002 u8 *local_buf;
4003
Mark Brownb3a223e2012-12-02 12:54:25 +09004004 /* Use preallocated DMA-safe buffer if we can. We can't avoid
4005 * copying here, (as a pure convenience thing), but we can
4006 * keep heap costs out of the hot path unless someone else is
4007 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08004008 */
Mark Brownb3a223e2012-12-02 12:54:25 +09004009 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08004010 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
4011 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09004012 if (!local_buf)
4013 return -ENOMEM;
4014 } else {
4015 local_buf = buf;
4016 }
David Brownell8ae12a02006-01-08 13:34:19 -08004017
Vitaly Wool8275c642006-01-08 13:34:28 -08004018 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09004019 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07004020 if (n_tx) {
4021 x[0].len = n_tx;
4022 spi_message_add_tail(&x[0], &message);
4023 }
4024 if (n_rx) {
4025 x[1].len = n_rx;
4026 spi_message_add_tail(&x[1], &message);
4027 }
Vitaly Wool8275c642006-01-08 13:34:28 -08004028
David Brownell8ae12a02006-01-08 13:34:19 -08004029 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07004030 x[0].tx_buf = local_buf;
4031 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08004032
4033 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08004034 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08004035 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07004036 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08004037
David Brownellbdff5492009-04-13 14:39:57 -07004038 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08004039 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08004040 else
4041 kfree(local_buf);
4042
4043 return status;
4044}
4045EXPORT_SYMBOL_GPL(spi_write_then_read);
4046
4047/*-------------------------------------------------------------------------*/
4048
Marco Felsch5f143af2018-09-25 11:42:29 +02004049#if IS_ENABLED(CONFIG_OF)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004050/* must call put_device() when done with returned spi_device device */
Marco Felsch5f143af2018-09-25 11:42:29 +02004051struct spi_device *of_find_spi_device_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004052{
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +01004053 struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
4054
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004055 return dev ? to_spi_device(dev) : NULL;
4056}
Marco Felsch5f143af2018-09-25 11:42:29 +02004057EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
4058#endif /* IS_ENABLED(CONFIG_OF) */
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004059
Marco Felsch5f143af2018-09-25 11:42:29 +02004060#if IS_ENABLED(CONFIG_OF_DYNAMIC)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004061/* the spi controllers are not using spi_bus, so we find it with another way */
4062static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004063{
4064 struct device *dev;
4065
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +01004066 dev = class_find_device_by_of_node(&spi_master_class, node);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02004067 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +01004068 dev = class_find_device_by_of_node(&spi_slave_class, node);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004069 if (!dev)
4070 return NULL;
4071
4072 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004073 return container_of(dev, struct spi_controller, dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004074}
4075
4076static int of_spi_notify(struct notifier_block *nb, unsigned long action,
4077 void *arg)
4078{
4079 struct of_reconfig_data *rd = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004080 struct spi_controller *ctlr;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004081 struct spi_device *spi;
4082
4083 switch (of_reconfig_get_state_change(action, arg)) {
4084 case OF_RECONFIG_CHANGE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004085 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
4086 if (ctlr == NULL)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004087 return NOTIFY_OK; /* not for us */
4088
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01004089 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004090 put_device(&ctlr->dev);
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01004091 return NOTIFY_OK;
4092 }
4093
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004094 spi = of_register_spi_device(ctlr, rd->dn);
4095 put_device(&ctlr->dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004096
4097 if (IS_ERR(spi)) {
Rob Herring25c56c82017-07-18 16:43:31 -05004098 pr_err("%s: failed to create for '%pOF'\n",
4099 __func__, rd->dn);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02004100 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004101 return notifier_from_errno(PTR_ERR(spi));
4102 }
4103 break;
4104
4105 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01004106 /* already depopulated? */
4107 if (!of_node_check_flag(rd->dn, OF_POPULATED))
4108 return NOTIFY_OK;
4109
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004110 /* find our device by node */
4111 spi = of_find_spi_device_by_node(rd->dn);
4112 if (spi == NULL)
4113 return NOTIFY_OK; /* no? not meant for us */
4114
4115 /* unregister takes one ref away */
4116 spi_unregister_device(spi);
4117
4118 /* and put the reference of the find */
4119 put_device(&spi->dev);
4120 break;
4121 }
4122
4123 return NOTIFY_OK;
4124}
4125
4126static struct notifier_block spi_of_notifier = {
4127 .notifier_call = of_spi_notify,
4128};
4129#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4130extern struct notifier_block spi_of_notifier;
4131#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4132
Octavian Purdila7f244672016-07-08 19:13:11 +03004133#if IS_ENABLED(CONFIG_ACPI)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004134static int spi_acpi_controller_match(struct device *dev, const void *data)
Octavian Purdila7f244672016-07-08 19:13:11 +03004135{
4136 return ACPI_COMPANION(dev->parent) == data;
4137}
4138
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004139static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
Octavian Purdila7f244672016-07-08 19:13:11 +03004140{
4141 struct device *dev;
4142
4143 dev = class_find_device(&spi_master_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004144 spi_acpi_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02004145 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
4146 dev = class_find_device(&spi_slave_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004147 spi_acpi_controller_match);
Octavian Purdila7f244672016-07-08 19:13:11 +03004148 if (!dev)
4149 return NULL;
4150
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004151 return container_of(dev, struct spi_controller, dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03004152}
4153
4154static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
4155{
4156 struct device *dev;
4157
Suzuki K Poulose00500142019-07-23 23:18:36 +01004158 dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
Wolfram Sang5b166682020-03-12 14:45:07 +01004159 return to_spi_device(dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03004160}
4161
4162static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
4163 void *arg)
4164{
4165 struct acpi_device *adev = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004166 struct spi_controller *ctlr;
Octavian Purdila7f244672016-07-08 19:13:11 +03004167 struct spi_device *spi;
4168
4169 switch (value) {
4170 case ACPI_RECONFIG_DEVICE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004171 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
4172 if (!ctlr)
Octavian Purdila7f244672016-07-08 19:13:11 +03004173 break;
4174
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004175 acpi_register_spi_device(ctlr, adev);
4176 put_device(&ctlr->dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03004177 break;
4178 case ACPI_RECONFIG_DEVICE_REMOVE:
4179 if (!acpi_device_enumerated(adev))
4180 break;
4181
4182 spi = acpi_spi_find_device_by_adev(adev);
4183 if (!spi)
4184 break;
4185
4186 spi_unregister_device(spi);
4187 put_device(&spi->dev);
4188 break;
4189 }
4190
4191 return NOTIFY_OK;
4192}
4193
4194static struct notifier_block spi_acpi_notifier = {
4195 .notifier_call = acpi_spi_notify,
4196};
4197#else
4198extern struct notifier_block spi_acpi_notifier;
4199#endif
4200
David Brownell8ae12a02006-01-08 13:34:19 -08004201static int __init spi_init(void)
4202{
David Brownellb8852442006-01-08 13:34:23 -08004203 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08004204
Christoph Lametere94b1762006-12-06 20:33:17 -08004205 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08004206 if (!buf) {
4207 status = -ENOMEM;
4208 goto err0;
4209 }
4210
4211 status = bus_register(&spi_bus_type);
4212 if (status < 0)
4213 goto err1;
4214
4215 status = class_register(&spi_master_class);
4216 if (status < 0)
4217 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004218
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02004219 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
4220 status = class_register(&spi_slave_class);
4221 if (status < 0)
4222 goto err3;
4223 }
4224
Fabio Estevam52677202014-11-26 20:13:57 -02004225 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004226 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03004227 if (IS_ENABLED(CONFIG_ACPI))
4228 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004229
David Brownell8ae12a02006-01-08 13:34:19 -08004230 return 0;
David Brownellb8852442006-01-08 13:34:23 -08004231
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02004232err3:
4233 class_unregister(&spi_master_class);
David Brownellb8852442006-01-08 13:34:23 -08004234err2:
4235 bus_unregister(&spi_bus_type);
4236err1:
4237 kfree(buf);
4238 buf = NULL;
4239err0:
4240 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08004241}
David Brownellb8852442006-01-08 13:34:23 -08004242
David Brownell8ae12a02006-01-08 13:34:19 -08004243/* board_info is normally registered in arch_initcall(),
4244 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08004245 *
4246 * REVISIT only boardinfo really needs static linking. the rest (device and
4247 * driver registration) _could_ be dynamically linked (modular) ... costs
4248 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08004249 */
David Brownell673c0c02008-10-15 22:02:46 -07004250postcore_initcall(spi_init);