blob: 292f26807b414432d2ebba695d64ab57565ec867 [file] [log] [blame]
Marco Felschb445bfc2018-09-25 11:42:28 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Brown787f4882018-11-29 16:24:37 +00002// SPI init/core code
3//
4// Copyright (C) 2005 David Brownell
5// Copyright (C) 2008 Secret Lab Technologies Ltd.
David Brownell8ae12a02006-01-08 13:34:19 -08006
David Brownell8ae12a02006-01-08 13:34:19 -08007#include <linux/kernel.h>
8#include <linux/device.h>
9#include <linux/init.h>
10#include <linux/cache.h>
Mark Brown99adef32014-01-16 12:22:43 +000011#include <linux/dma-mapping.h>
12#include <linux/dmaengine.h>
Matthias Kaehlcke94040822007-07-17 04:04:16 -070013#include <linux/mutex.h>
Sinan Akman2b7a32f2010-10-02 21:28:29 -060014#include <linux/of_device.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060015#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020016#include <linux/clk/clk-conf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Anton Vorontsove0626e32009-09-22 16:46:08 -070018#include <linux/mod_devicetable.h>
David Brownell8ae12a02006-01-08 13:34:19 -080019#include <linux/spi/spi.h>
Boris Brezillonb5932f52018-04-26 18:18:15 +020020#include <linux/spi/spi-mem.h>
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +010021#include <linux/of_gpio.h>
Linus Walleijf3186dd2019-01-07 16:51:50 +010022#include <linux/gpio/consumer.h>
Mark Brown3ae22e82010-12-25 15:32:27 +010023#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020024#include <linux/pm_domain.h>
Dmitry Torokhov826cf172017-02-28 14:25:18 -080025#include <linux/property.h>
Paul Gortmaker025ed132011-07-10 12:57:55 -040026#include <linux/export.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060027#include <linux/sched/rt.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010028#include <uapi/linux/sched/types.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010029#include <linux/delay.h>
30#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010031#include <linux/ioport.h>
32#include <linux/acpi.h>
Vignesh Rb1b81532016-08-17 15:22:36 +053033#include <linux/highmem.h>
Suniel Mahesh9b61e302017-08-03 10:05:57 +053034#include <linux/idr.h>
Lukas Wunner8a2e4872017-08-01 14:10:41 +020035#include <linux/platform_data/x86/apple.h>
David Brownell8ae12a02006-01-08 13:34:19 -080036
Mark Brown56ec1972013-10-07 19:33:53 +010037#define CREATE_TRACE_POINTS
38#include <trace/events/spi.h>
Arnd Bergmannca1438d2019-03-21 13:42:25 +010039EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);
40EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
Suniel Mahesh9b61e302017-08-03 10:05:57 +053041
Boris Brezillon46336962018-04-22 20:35:14 +020042#include "internals.h"
43
Suniel Mahesh9b61e302017-08-03 10:05:57 +053044static DEFINE_IDR(spi_master_idr);
Mark Brown56ec1972013-10-07 19:33:53 +010045
David Brownell8ae12a02006-01-08 13:34:19 -080046static void spidev_release(struct device *dev)
47{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080048 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080049
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020050 /* spi controllers may cleanup for released devices */
51 if (spi->controller->cleanup)
52 spi->controller->cleanup(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080053
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020054 spi_controller_put(spi->controller);
Trent Piepho50395632018-09-20 19:18:32 +000055 kfree(spi->driver_override);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000056 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080057}
58
59static ssize_t
60modalias_show(struct device *dev, struct device_attribute *a, char *buf)
61{
62 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080063 int len;
64
65 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
66 if (len != -ENODEV)
67 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080068
Grant Likelyd8e328b2012-05-20 00:08:13 -060069 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080070}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070071static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080072
Trent Piepho50395632018-09-20 19:18:32 +000073static ssize_t driver_override_store(struct device *dev,
74 struct device_attribute *a,
75 const char *buf, size_t count)
76{
77 struct spi_device *spi = to_spi_device(dev);
78 const char *end = memchr(buf, '\n', count);
79 const size_t len = end ? end - buf : count;
80 const char *driver_override, *old;
81
82 /* We need to keep extra room for a newline when displaying value */
83 if (len >= (PAGE_SIZE - 1))
84 return -EINVAL;
85
86 driver_override = kstrndup(buf, len, GFP_KERNEL);
87 if (!driver_override)
88 return -ENOMEM;
89
90 device_lock(dev);
91 old = spi->driver_override;
92 if (len) {
93 spi->driver_override = driver_override;
94 } else {
Andy Shevchenkobe73e322019-10-23 15:16:43 +030095 /* Empty string, disable driver override */
Trent Piepho50395632018-09-20 19:18:32 +000096 spi->driver_override = NULL;
97 kfree(driver_override);
98 }
99 device_unlock(dev);
100 kfree(old);
101
102 return count;
103}
104
105static ssize_t driver_override_show(struct device *dev,
106 struct device_attribute *a, char *buf)
107{
108 const struct spi_device *spi = to_spi_device(dev);
109 ssize_t len;
110
111 device_lock(dev);
112 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : "");
113 device_unlock(dev);
114 return len;
115}
116static DEVICE_ATTR_RW(driver_override);
117
Martin Sperleca2ebc2015-06-22 13:00:36 +0000118#define SPI_STATISTICS_ATTRS(field, file) \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200119static ssize_t spi_controller_##field##_show(struct device *dev, \
120 struct device_attribute *attr, \
121 char *buf) \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000122{ \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200123 struct spi_controller *ctlr = container_of(dev, \
124 struct spi_controller, dev); \
125 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000126} \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200127static struct device_attribute dev_attr_spi_controller_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +0200128 .attr = { .name = file, .mode = 0444 }, \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200129 .show = spi_controller_##field##_show, \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000130}; \
131static ssize_t spi_device_##field##_show(struct device *dev, \
132 struct device_attribute *attr, \
133 char *buf) \
134{ \
Geliang Tangd1eba932015-12-23 00:18:41 +0800135 struct spi_device *spi = to_spi_device(dev); \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000136 return spi_statistics_##field##_show(&spi->statistics, buf); \
137} \
138static struct device_attribute dev_attr_spi_device_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +0200139 .attr = { .name = file, .mode = 0444 }, \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000140 .show = spi_device_##field##_show, \
141}
142
143#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
144static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
145 char *buf) \
146{ \
147 unsigned long flags; \
148 ssize_t len; \
149 spin_lock_irqsave(&stat->lock, flags); \
150 len = sprintf(buf, format_string, stat->field); \
151 spin_unlock_irqrestore(&stat->lock, flags); \
152 return len; \
153} \
154SPI_STATISTICS_ATTRS(name, file)
155
156#define SPI_STATISTICS_SHOW(field, format_string) \
157 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
158 field, format_string)
159
160SPI_STATISTICS_SHOW(messages, "%lu");
161SPI_STATISTICS_SHOW(transfers, "%lu");
162SPI_STATISTICS_SHOW(errors, "%lu");
163SPI_STATISTICS_SHOW(timedout, "%lu");
164
165SPI_STATISTICS_SHOW(spi_sync, "%lu");
166SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
167SPI_STATISTICS_SHOW(spi_async, "%lu");
168
169SPI_STATISTICS_SHOW(bytes, "%llu");
170SPI_STATISTICS_SHOW(bytes_rx, "%llu");
171SPI_STATISTICS_SHOW(bytes_tx, "%llu");
172
Martin Sperl6b7bc062015-06-22 13:02:04 +0000173#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
174 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
175 "transfer_bytes_histo_" number, \
176 transfer_bytes_histo[index], "%lu")
177SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
178SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
179SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
180SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
181SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
182SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
183SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
184SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
185SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
186SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
187SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
188SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
189SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
190SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
191SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
192SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
193SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
194
Martin Sperld9f12122015-12-14 15:20:20 +0000195SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
196
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700197static struct attribute *spi_dev_attrs[] = {
198 &dev_attr_modalias.attr,
Trent Piepho50395632018-09-20 19:18:32 +0000199 &dev_attr_driver_override.attr,
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700200 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800201};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000202
203static const struct attribute_group spi_dev_group = {
204 .attrs = spi_dev_attrs,
205};
206
207static struct attribute *spi_device_statistics_attrs[] = {
208 &dev_attr_spi_device_messages.attr,
209 &dev_attr_spi_device_transfers.attr,
210 &dev_attr_spi_device_errors.attr,
211 &dev_attr_spi_device_timedout.attr,
212 &dev_attr_spi_device_spi_sync.attr,
213 &dev_attr_spi_device_spi_sync_immediate.attr,
214 &dev_attr_spi_device_spi_async.attr,
215 &dev_attr_spi_device_bytes.attr,
216 &dev_attr_spi_device_bytes_rx.attr,
217 &dev_attr_spi_device_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000218 &dev_attr_spi_device_transfer_bytes_histo0.attr,
219 &dev_attr_spi_device_transfer_bytes_histo1.attr,
220 &dev_attr_spi_device_transfer_bytes_histo2.attr,
221 &dev_attr_spi_device_transfer_bytes_histo3.attr,
222 &dev_attr_spi_device_transfer_bytes_histo4.attr,
223 &dev_attr_spi_device_transfer_bytes_histo5.attr,
224 &dev_attr_spi_device_transfer_bytes_histo6.attr,
225 &dev_attr_spi_device_transfer_bytes_histo7.attr,
226 &dev_attr_spi_device_transfer_bytes_histo8.attr,
227 &dev_attr_spi_device_transfer_bytes_histo9.attr,
228 &dev_attr_spi_device_transfer_bytes_histo10.attr,
229 &dev_attr_spi_device_transfer_bytes_histo11.attr,
230 &dev_attr_spi_device_transfer_bytes_histo12.attr,
231 &dev_attr_spi_device_transfer_bytes_histo13.attr,
232 &dev_attr_spi_device_transfer_bytes_histo14.attr,
233 &dev_attr_spi_device_transfer_bytes_histo15.attr,
234 &dev_attr_spi_device_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000235 &dev_attr_spi_device_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000236 NULL,
237};
238
239static const struct attribute_group spi_device_statistics_group = {
240 .name = "statistics",
241 .attrs = spi_device_statistics_attrs,
242};
243
244static const struct attribute_group *spi_dev_groups[] = {
245 &spi_dev_group,
246 &spi_device_statistics_group,
247 NULL,
248};
249
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200250static struct attribute *spi_controller_statistics_attrs[] = {
251 &dev_attr_spi_controller_messages.attr,
252 &dev_attr_spi_controller_transfers.attr,
253 &dev_attr_spi_controller_errors.attr,
254 &dev_attr_spi_controller_timedout.attr,
255 &dev_attr_spi_controller_spi_sync.attr,
256 &dev_attr_spi_controller_spi_sync_immediate.attr,
257 &dev_attr_spi_controller_spi_async.attr,
258 &dev_attr_spi_controller_bytes.attr,
259 &dev_attr_spi_controller_bytes_rx.attr,
260 &dev_attr_spi_controller_bytes_tx.attr,
261 &dev_attr_spi_controller_transfer_bytes_histo0.attr,
262 &dev_attr_spi_controller_transfer_bytes_histo1.attr,
263 &dev_attr_spi_controller_transfer_bytes_histo2.attr,
264 &dev_attr_spi_controller_transfer_bytes_histo3.attr,
265 &dev_attr_spi_controller_transfer_bytes_histo4.attr,
266 &dev_attr_spi_controller_transfer_bytes_histo5.attr,
267 &dev_attr_spi_controller_transfer_bytes_histo6.attr,
268 &dev_attr_spi_controller_transfer_bytes_histo7.attr,
269 &dev_attr_spi_controller_transfer_bytes_histo8.attr,
270 &dev_attr_spi_controller_transfer_bytes_histo9.attr,
271 &dev_attr_spi_controller_transfer_bytes_histo10.attr,
272 &dev_attr_spi_controller_transfer_bytes_histo11.attr,
273 &dev_attr_spi_controller_transfer_bytes_histo12.attr,
274 &dev_attr_spi_controller_transfer_bytes_histo13.attr,
275 &dev_attr_spi_controller_transfer_bytes_histo14.attr,
276 &dev_attr_spi_controller_transfer_bytes_histo15.attr,
277 &dev_attr_spi_controller_transfer_bytes_histo16.attr,
278 &dev_attr_spi_controller_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000279 NULL,
280};
281
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200282static const struct attribute_group spi_controller_statistics_group = {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000283 .name = "statistics",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200284 .attrs = spi_controller_statistics_attrs,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000285};
286
287static const struct attribute_group *spi_master_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200288 &spi_controller_statistics_group,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000289 NULL,
290};
291
292void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
293 struct spi_transfer *xfer,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200294 struct spi_controller *ctlr)
Martin Sperleca2ebc2015-06-22 13:00:36 +0000295{
296 unsigned long flags;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000297 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
298
299 if (l2len < 0)
300 l2len = 0;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000301
302 spin_lock_irqsave(&stats->lock, flags);
303
304 stats->transfers++;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000305 stats->transfer_bytes_histo[l2len]++;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000306
307 stats->bytes += xfer->len;
308 if ((xfer->tx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200309 (xfer->tx_buf != ctlr->dummy_tx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000310 stats->bytes_tx += xfer->len;
311 if ((xfer->rx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200312 (xfer->rx_buf != ctlr->dummy_rx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000313 stats->bytes_rx += xfer->len;
314
315 spin_unlock_irqrestore(&stats->lock, flags);
316}
317EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800318
319/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
320 * and the sysfs version makes coldplug work too.
321 */
322
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700323static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
324 const struct spi_device *sdev)
325{
326 while (id->name[0]) {
327 if (!strcmp(sdev->modalias, id->name))
328 return id;
329 id++;
330 }
331 return NULL;
332}
333
334const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
335{
336 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
337
338 return spi_match_id(sdrv->id_table, sdev);
339}
340EXPORT_SYMBOL_GPL(spi_get_device_id);
341
David Brownell8ae12a02006-01-08 13:34:19 -0800342static int spi_match_device(struct device *dev, struct device_driver *drv)
343{
344 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700345 const struct spi_driver *sdrv = to_spi_driver(drv);
346
Trent Piepho50395632018-09-20 19:18:32 +0000347 /* Check override first, and if set, only use the named driver */
348 if (spi->driver_override)
349 return strcmp(spi->driver_override, drv->name) == 0;
350
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600351 /* Attempt an OF style match */
352 if (of_driver_match_device(dev, drv))
353 return 1;
354
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100355 /* Then try ACPI */
356 if (acpi_driver_match_device(dev, drv))
357 return 1;
358
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700359 if (sdrv->id_table)
360 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800361
Kay Sievers35f74fc2009-01-06 10:44:37 -0800362 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800363}
364
Kay Sievers7eff2e72007-08-14 15:15:12 +0200365static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800366{
367 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800368 int rc;
369
370 rc = acpi_device_uevent_modalias(dev, env);
371 if (rc != -ENODEV)
372 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800373
Andy Shevchenko28566702017-07-26 16:14:00 +0300374 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800375}
376
David Brownell8ae12a02006-01-08 13:34:19 -0800377struct bus_type spi_bus_type = {
378 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700379 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800380 .match = spi_match_device,
381 .uevent = spi_uevent,
David Brownell8ae12a02006-01-08 13:34:19 -0800382};
383EXPORT_SYMBOL_GPL(spi_bus_type);
384
David Brownellb8852442006-01-08 13:34:23 -0800385
386static int spi_drv_probe(struct device *dev)
387{
388 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Jon Hunter44af7922015-10-09 15:45:55 +0100389 struct spi_device *spi = to_spi_device(dev);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300390 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800391
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200392 ret = of_clk_set_defaults(dev->of_node, false);
393 if (ret)
394 return ret;
395
Jon Hunter44af7922015-10-09 15:45:55 +0100396 if (dev->of_node) {
397 spi->irq = of_irq_get(dev->of_node, 0);
398 if (spi->irq == -EPROBE_DEFER)
399 return -EPROBE_DEFER;
400 if (spi->irq < 0)
401 spi->irq = 0;
402 }
403
Ulf Hansson676e7c22014-09-19 20:27:41 +0200404 ret = dev_pm_domain_attach(dev, true);
Ulf Hansson71f277a2018-04-26 10:53:10 +0200405 if (ret)
406 return ret;
407
408 ret = sdrv->probe(spi);
409 if (ret)
410 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300411
412 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800413}
414
415static int spi_drv_remove(struct device *dev)
416{
417 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300418 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800419
Jean Delvareaec35f42014-02-13 15:28:41 +0100420 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200421 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300422
423 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800424}
425
426static void spi_drv_shutdown(struct device *dev)
427{
428 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
429
430 sdrv->shutdown(to_spi_device(dev));
431}
432
David Brownell33e34dc2007-05-08 00:32:21 -0700433/**
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500434 * __spi_register_driver - register a SPI driver
Thierry Reding88c93212015-11-10 13:03:04 +0100435 * @owner: owner module of the driver to register
David Brownell33e34dc2007-05-08 00:32:21 -0700436 * @sdrv: the driver to register
437 * Context: can sleep
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200438 *
439 * Return: zero on success, else a negative error code.
David Brownell33e34dc2007-05-08 00:32:21 -0700440 */
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500441int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
David Brownellb8852442006-01-08 13:34:23 -0800442{
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500443 sdrv->driver.owner = owner;
David Brownellb8852442006-01-08 13:34:23 -0800444 sdrv->driver.bus = &spi_bus_type;
445 if (sdrv->probe)
446 sdrv->driver.probe = spi_drv_probe;
447 if (sdrv->remove)
448 sdrv->driver.remove = spi_drv_remove;
449 if (sdrv->shutdown)
450 sdrv->driver.shutdown = spi_drv_shutdown;
451 return driver_register(&sdrv->driver);
452}
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500453EXPORT_SYMBOL_GPL(__spi_register_driver);
David Brownellb8852442006-01-08 13:34:23 -0800454
David Brownell8ae12a02006-01-08 13:34:19 -0800455/*-------------------------------------------------------------------------*/
456
457/* SPI devices should normally not be created by SPI device drivers; that
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200458 * would make them board-specific. Similarly with SPI controller drivers.
David Brownell8ae12a02006-01-08 13:34:19 -0800459 * Device registration normally goes into like arch/.../mach.../board-YYY.c
460 * with other readonly (flashable) information about mainboard devices.
461 */
462
463struct boardinfo {
464 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800465 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800466};
467
468static LIST_HEAD(board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200469static LIST_HEAD(spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800470
471/*
Andy Shevchenkobe73e322019-10-23 15:16:43 +0300472 * Used to protect add/del operation for board_info list and
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200473 * spi_controller list, and their matching process
Suniel Mahesh9a9a0472017-08-17 18:18:22 +0530474 * also used to protect object of type struct idr
Feng Tang2b9603a2010-08-02 15:52:15 +0800475 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700476static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800477
Grant Likelydc87c982008-05-15 16:50:22 -0600478/**
479 * spi_alloc_device - Allocate a new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200480 * @ctlr: Controller to which device is connected
Grant Likelydc87c982008-05-15 16:50:22 -0600481 * Context: can sleep
482 *
483 * Allows a driver to allocate and initialize a spi_device without
484 * registering it immediately. This allows a driver to directly
485 * fill the spi_device with device parameters before calling
486 * spi_add_device() on it.
487 *
488 * Caller is responsible to call spi_add_device() on the returned
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200489 * spi_device structure to add it to the SPI controller. If the caller
Grant Likelydc87c982008-05-15 16:50:22 -0600490 * needs to discard the spi_device without adding it, then it should
491 * call spi_dev_put() on it.
492 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200493 * Return: a pointer to the new device, or NULL.
Grant Likelydc87c982008-05-15 16:50:22 -0600494 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200495struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
Grant Likelydc87c982008-05-15 16:50:22 -0600496{
497 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600498
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200499 if (!spi_controller_get(ctlr))
Grant Likelydc87c982008-05-15 16:50:22 -0600500 return NULL;
501
Jingoo Han5fe5f052013-10-14 10:31:51 +0900502 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600503 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200504 spi_controller_put(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600505 return NULL;
506 }
507
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200508 spi->master = spi->controller = ctlr;
509 spi->dev.parent = &ctlr->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600510 spi->dev.bus = &spi_bus_type;
511 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100512 spi->cs_gpio = -ENOENT;
John Garryea235782020-02-28 23:18:49 +0800513 spi->mode = ctlr->buswidth_override_bits;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000514
515 spin_lock_init(&spi->statistics.lock);
516
Grant Likelydc87c982008-05-15 16:50:22 -0600517 device_initialize(&spi->dev);
518 return spi;
519}
520EXPORT_SYMBOL_GPL(spi_alloc_device);
521
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200522static void spi_dev_set_name(struct spi_device *spi)
523{
524 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
525
526 if (adev) {
527 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
528 return;
529 }
530
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200531 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200532 spi->chip_select);
533}
534
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200535static int spi_dev_check(struct device *dev, void *data)
536{
537 struct spi_device *spi = to_spi_device(dev);
538 struct spi_device *new_spi = data;
539
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200540 if (spi->controller == new_spi->controller &&
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200541 spi->chip_select == new_spi->chip_select)
542 return -EBUSY;
543 return 0;
544}
545
Grant Likelydc87c982008-05-15 16:50:22 -0600546/**
547 * spi_add_device - Add spi_device allocated with spi_alloc_device
548 * @spi: spi_device to register
549 *
550 * Companion function to spi_alloc_device. Devices allocated with
551 * spi_alloc_device can be added onto the spi bus with this function.
552 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200553 * Return: 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600554 */
555int spi_add_device(struct spi_device *spi)
556{
David Brownelle48880e2008-08-15 00:40:44 -0700557 static DEFINE_MUTEX(spi_add_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200558 struct spi_controller *ctlr = spi->controller;
559 struct device *dev = ctlr->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600560 int status;
561
562 /* Chipselects are numbered 0..max; validate. */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200563 if (spi->chip_select >= ctlr->num_chipselect) {
564 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
565 ctlr->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600566 return -EINVAL;
567 }
568
569 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200570 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700571
572 /* We need to make sure there's no other device with this
573 * chipselect **BEFORE** we call setup(), else we'll trash
574 * its configuration. Lock against concurrent add() calls.
575 */
576 mutex_lock(&spi_add_lock);
577
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200578 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
579 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700580 dev_err(dev, "chipselect %d already in use\n",
581 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700582 goto done;
583 }
584
Linus Walleijf3186dd2019-01-07 16:51:50 +0100585 /* Descriptors take precedence */
586 if (ctlr->cs_gpiods)
587 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
588 else if (ctlr->cs_gpios)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200589 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100590
David Brownelle48880e2008-08-15 00:40:44 -0700591 /* Drivers may modify this initial i/o setup, but will
592 * normally rely on the device being setup. Devices
593 * using SPI_CS_HIGH can't coexist well otherwise...
594 */
David Brownell7d077192009-06-17 16:26:03 -0700595 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600596 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200597 dev_err(dev, "can't setup %s, status %d\n",
598 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700599 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600600 }
601
David Brownelle48880e2008-08-15 00:40:44 -0700602 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600603 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700604 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200605 dev_err(dev, "can't add %s, status %d\n",
606 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700607 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800608 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600609
David Brownelle48880e2008-08-15 00:40:44 -0700610done:
611 mutex_unlock(&spi_add_lock);
612 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600613}
614EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800615
David Brownell33e34dc2007-05-08 00:32:21 -0700616/**
617 * spi_new_device - instantiate one new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200618 * @ctlr: Controller to which device is connected
David Brownell33e34dc2007-05-08 00:32:21 -0700619 * @chip: Describes the SPI device
620 * Context: can sleep
621 *
622 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800623 * after board init creates the hard-wired devices. Some development
624 * platforms may not be able to use spi_register_board_info though, and
625 * this is exported so that for example a USB or parport based adapter
626 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700627 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200628 * Return: the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800629 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200630struct spi_device *spi_new_device(struct spi_controller *ctlr,
Adrian Bunke9d5a462007-03-26 21:32:23 -0800631 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800632{
633 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800634 int status;
635
David Brownell082c8cb2007-07-31 00:39:45 -0700636 /* NOTE: caller did any chip->bus_num checks necessary.
637 *
638 * Also, unless we change the return value convention to use
639 * error-or-pointer (not NULL-or-pointer), troubleshootability
640 * suggests syslogged diagnostics are best here (ugh).
641 */
642
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200643 proxy = spi_alloc_device(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600644 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800645 return NULL;
646
Grant Likely102eb972008-07-23 21:29:55 -0700647 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
648
David Brownell8ae12a02006-01-08 13:34:19 -0800649 proxy->chip_select = chip->chip_select;
650 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700651 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800652 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700653 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800654 proxy->dev.platform_data = (void *) chip->platform_data;
655 proxy->controller_data = chip->controller_data;
656 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800657
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800658 if (chip->properties) {
659 status = device_add_properties(&proxy->dev, chip->properties);
660 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200661 dev_err(&ctlr->dev,
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800662 "failed to add properties to '%s': %d\n",
663 chip->modalias, status);
664 goto err_dev_put;
665 }
David Brownell8ae12a02006-01-08 13:34:19 -0800666 }
667
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800668 status = spi_add_device(proxy);
669 if (status < 0)
670 goto err_remove_props;
671
David Brownell8ae12a02006-01-08 13:34:19 -0800672 return proxy;
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800673
674err_remove_props:
675 if (chip->properties)
676 device_remove_properties(&proxy->dev);
677err_dev_put:
678 spi_dev_put(proxy);
679 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800680}
681EXPORT_SYMBOL_GPL(spi_new_device);
682
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100683/**
684 * spi_unregister_device - unregister a single SPI device
685 * @spi: spi_device to unregister
686 *
687 * Start making the passed SPI device vanish. Normally this would be handled
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200688 * by spi_unregister_controller().
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100689 */
690void spi_unregister_device(struct spi_device *spi)
691{
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100692 if (!spi)
693 return;
694
Johan Hovold83241472017-01-30 17:47:05 +0100695 if (spi->dev.of_node) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100696 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
Johan Hovold83241472017-01-30 17:47:05 +0100697 of_node_put(spi->dev.of_node);
698 }
Octavian Purdila7f244672016-07-08 19:13:11 +0300699 if (ACPI_COMPANION(&spi->dev))
700 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100701 device_unregister(&spi->dev);
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100702}
703EXPORT_SYMBOL_GPL(spi_unregister_device);
704
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200705static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
706 struct spi_board_info *bi)
Feng Tang2b9603a2010-08-02 15:52:15 +0800707{
708 struct spi_device *dev;
709
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200710 if (ctlr->bus_num != bi->bus_num)
Feng Tang2b9603a2010-08-02 15:52:15 +0800711 return;
712
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200713 dev = spi_new_device(ctlr, bi);
Feng Tang2b9603a2010-08-02 15:52:15 +0800714 if (!dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200715 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
Feng Tang2b9603a2010-08-02 15:52:15 +0800716 bi->modalias);
717}
718
David Brownell33e34dc2007-05-08 00:32:21 -0700719/**
720 * spi_register_board_info - register SPI devices for a given board
721 * @info: array of chip descriptors
722 * @n: how many descriptors are provided
723 * Context: can sleep
724 *
David Brownell8ae12a02006-01-08 13:34:19 -0800725 * Board-specific early init code calls this (probably during arch_initcall)
726 * with segments of the SPI device table. Any device nodes are created later,
727 * after the relevant parent SPI controller (bus_num) is defined. We keep
728 * this table of devices forever, so that reloading a controller driver will
729 * not make Linux forget about these hard-wired devices.
730 *
731 * Other code can also call this, e.g. a particular add-on board might provide
732 * SPI devices through its expansion connector, so code initializing that board
733 * would naturally declare its SPI devices.
734 *
735 * The board info passed can safely be __initdata ... but be careful of
736 * any embedded pointers (platform_data, etc), they're copied as-is.
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800737 * Device properties are deep-copied though.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200738 *
739 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -0800740 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000741int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800742{
Feng Tang2b9603a2010-08-02 15:52:15 +0800743 struct boardinfo *bi;
744 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800745
Xiubo Lic7908a32014-09-24 14:30:29 +0800746 if (!n)
Dmitry Torokhovf974cf52017-02-28 14:25:19 -0800747 return 0;
Xiubo Lic7908a32014-09-24 14:30:29 +0800748
Markus Elfringf9bdb7f2017-01-13 12:28:04 +0100749 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800750 if (!bi)
751 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800752
Feng Tang2b9603a2010-08-02 15:52:15 +0800753 for (i = 0; i < n; i++, bi++, info++) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200754 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -0800755
Feng Tang2b9603a2010-08-02 15:52:15 +0800756 memcpy(&bi->board_info, info, sizeof(*info));
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800757 if (info->properties) {
758 bi->board_info.properties =
759 property_entries_dup(info->properties);
760 if (IS_ERR(bi->board_info.properties))
761 return PTR_ERR(bi->board_info.properties);
762 }
763
Feng Tang2b9603a2010-08-02 15:52:15 +0800764 mutex_lock(&board_lock);
765 list_add_tail(&bi->list, &board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200766 list_for_each_entry(ctlr, &spi_controller_list, list)
767 spi_match_controller_to_boardinfo(ctlr,
768 &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +0800769 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800770 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800771
772 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800773}
774
775/*-------------------------------------------------------------------------*/
776
Mark Brownb1589352013-10-05 11:50:40 +0100777static void spi_set_cs(struct spi_device *spi, bool enable)
778{
Alexandru Ardelean25093bd2019-09-26 13:51:43 +0300779 bool enable1 = enable;
780
781 if (!spi->controller->set_cs_timing) {
782 if (enable1)
783 spi_delay_exec(&spi->controller->cs_setup, NULL);
784 else
785 spi_delay_exec(&spi->controller->cs_hold, NULL);
786 }
787
Mark Brownb1589352013-10-05 11:50:40 +0100788 if (spi->mode & SPI_CS_HIGH)
789 enable = !enable;
790
Linus Walleijf3186dd2019-01-07 16:51:50 +0100791 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
792 /*
793 * Honour the SPI_NO_CS flag and invert the enable line, as
794 * active low is default for SPI. Execution paths that handle
795 * polarity inversion in gpiolib (such as device tree) will
796 * enforce active high using the SPI_CS_HIGH resulting in a
797 * double inversion through the code above.
798 */
799 if (!(spi->mode & SPI_NO_CS)) {
800 if (spi->cs_gpiod)
Felix Fietkau28f76042019-02-10 00:38:25 +0100801 gpiod_set_value_cansleep(spi->cs_gpiod,
802 !enable);
Linus Walleijf3186dd2019-01-07 16:51:50 +0100803 else
Felix Fietkau28f76042019-02-10 00:38:25 +0100804 gpio_set_value_cansleep(spi->cs_gpio, !enable);
Linus Walleijf3186dd2019-01-07 16:51:50 +0100805 }
Thor Thayer8eee6b92016-10-10 09:25:24 -0500806 /* Some SPI masters need both GPIO CS & slave_select */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200807 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
808 spi->controller->set_cs)
809 spi->controller->set_cs(spi, !enable);
810 } else if (spi->controller->set_cs) {
811 spi->controller->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500812 }
Alexandru Ardelean25093bd2019-09-26 13:51:43 +0300813
814 if (!spi->controller->set_cs_timing) {
815 if (!enable1)
816 spi_delay_exec(&spi->controller->cs_inactive, NULL);
817 }
Mark Brownb1589352013-10-05 11:50:40 +0100818}
819
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200820#ifdef CONFIG_HAS_DMA
Boris Brezillon46336962018-04-22 20:35:14 +0200821int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
822 struct sg_table *sgt, void *buf, size_t len,
823 enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000824{
825 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200826 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530827#ifdef CONFIG_HIGHMEM
828 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
829 (unsigned long)buf < (PKMAP_BASE +
830 (LAST_PKMAP * PAGE_SIZE)));
831#else
832 const bool kmap_buf = false;
833#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500834 int desc_len;
835 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000836 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600837 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000838 void *sg_buf;
839 size_t min;
840 int i, ret;
841
Vignesh Rb1b81532016-08-17 15:22:36 +0530842 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200843 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500844 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530845 } else if (virt_addr_valid(buf)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200846 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500847 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530848 } else {
849 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500850 }
851
Mark Brown6ad45a22014-02-02 13:47:47 +0000852 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
853 if (ret != 0)
854 return ret;
855
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600856 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000857 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000858
Vignesh Rb1b81532016-08-17 15:22:36 +0530859 if (vmalloced_buf || kmap_buf) {
Maxime Chevallierce993192018-03-02 15:55:09 +0100860 /*
861 * Next scatterlist entry size is the minimum between
862 * the desc_len and the remaining buffer length that
863 * fits in a page.
864 */
865 min = min_t(size_t, desc_len,
866 min_t(size_t, len,
867 PAGE_SIZE - offset_in_page(buf)));
Vignesh Rb1b81532016-08-17 15:22:36 +0530868 if (vmalloced_buf)
869 vm_page = vmalloc_to_page(buf);
870 else
871 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000872 if (!vm_page) {
873 sg_free_table(sgt);
874 return -ENOMEM;
875 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600876 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000877 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000878 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500879 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000880 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600881 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000882 }
883
Mark Brown6ad45a22014-02-02 13:47:47 +0000884 buf += min;
885 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600886 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000887 }
888
889 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200890 if (!ret)
891 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000892 if (ret < 0) {
893 sg_free_table(sgt);
894 return ret;
895 }
896
897 sgt->nents = ret;
898
899 return 0;
900}
901
Boris Brezillon46336962018-04-22 20:35:14 +0200902void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
903 struct sg_table *sgt, enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000904{
905 if (sgt->orig_nents) {
906 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
907 sg_free_table(sgt);
908 }
909}
910
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200911static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000912{
Mark Brown99adef32014-01-16 12:22:43 +0000913 struct device *tx_dev, *rx_dev;
914 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000915 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000916
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200917 if (!ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000918 return 0;
919
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200920 if (ctlr->dma_tx)
921 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800922 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200923 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800924
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200925 if (ctlr->dma_rx)
926 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800927 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200928 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000929
930 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200931 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000932 continue;
933
934 if (xfer->tx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200935 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000936 (void *)xfer->tx_buf, xfer->len,
937 DMA_TO_DEVICE);
938 if (ret != 0)
939 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000940 }
941
942 if (xfer->rx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200943 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000944 xfer->rx_buf, xfer->len,
945 DMA_FROM_DEVICE);
946 if (ret != 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200947 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000948 DMA_TO_DEVICE);
949 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000950 }
951 }
952 }
953
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200954 ctlr->cur_msg_mapped = true;
Mark Brown99adef32014-01-16 12:22:43 +0000955
956 return 0;
957}
958
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200959static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000960{
961 struct spi_transfer *xfer;
962 struct device *tx_dev, *rx_dev;
963
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200964 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000965 return 0;
966
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200967 if (ctlr->dma_tx)
968 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800969 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200970 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800971
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200972 if (ctlr->dma_rx)
973 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800974 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200975 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000976
977 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200978 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000979 continue;
980
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200981 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
982 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000983 }
984
985 return 0;
986}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200987#else /* !CONFIG_HAS_DMA */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200988static inline int __spi_map_msg(struct spi_controller *ctlr,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200989 struct spi_message *msg)
990{
991 return 0;
992}
993
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200994static inline int __spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000995 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200996{
997 return 0;
998}
999#endif /* !CONFIG_HAS_DMA */
1000
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001001static inline int spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +00001002 struct spi_message *msg)
1003{
1004 struct spi_transfer *xfer;
1005
1006 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1007 /*
1008 * Restore the original value of tx_buf or rx_buf if they are
1009 * NULL.
1010 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001011 if (xfer->tx_buf == ctlr->dummy_tx)
Martin Sperl4b786452015-05-25 10:13:10 +00001012 xfer->tx_buf = NULL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001013 if (xfer->rx_buf == ctlr->dummy_rx)
Martin Sperl4b786452015-05-25 10:13:10 +00001014 xfer->rx_buf = NULL;
1015 }
1016
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001017 return __spi_unmap_msg(ctlr, msg);
Martin Sperl4b786452015-05-25 10:13:10 +00001018}
1019
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001020static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001021{
1022 struct spi_transfer *xfer;
1023 void *tmp;
1024 unsigned int max_tx, max_rx;
1025
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001026 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001027 max_tx = 0;
1028 max_rx = 0;
1029
1030 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001031 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001032 !xfer->tx_buf)
1033 max_tx = max(xfer->len, max_tx);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001034 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001035 !xfer->rx_buf)
1036 max_rx = max(xfer->len, max_rx);
1037 }
1038
1039 if (max_tx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001040 tmp = krealloc(ctlr->dummy_tx, max_tx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001041 GFP_KERNEL | GFP_DMA);
1042 if (!tmp)
1043 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001044 ctlr->dummy_tx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001045 memset(tmp, 0, max_tx);
1046 }
1047
1048 if (max_rx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001049 tmp = krealloc(ctlr->dummy_rx, max_rx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001050 GFP_KERNEL | GFP_DMA);
1051 if (!tmp)
1052 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001053 ctlr->dummy_rx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001054 }
1055
1056 if (max_tx || max_rx) {
1057 list_for_each_entry(xfer, &msg->transfers,
1058 transfer_list) {
Chris Lesiak5442dca2019-03-07 20:39:00 +00001059 if (!xfer->len)
1060 continue;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001061 if (!xfer->tx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001062 xfer->tx_buf = ctlr->dummy_tx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001063 if (!xfer->rx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001064 xfer->rx_buf = ctlr->dummy_rx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001065 }
1066 }
1067 }
1068
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001069 return __spi_map_msg(ctlr, msg);
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001070}
Mark Brown99adef32014-01-16 12:22:43 +00001071
Lubomir Rintel810923f2018-11-13 11:22:24 +01001072static int spi_transfer_wait(struct spi_controller *ctlr,
1073 struct spi_message *msg,
1074 struct spi_transfer *xfer)
1075{
1076 struct spi_statistics *statm = &ctlr->statistics;
1077 struct spi_statistics *stats = &msg->spi->statistics;
1078 unsigned long long ms = 1;
1079
1080 if (spi_controller_is_slave(ctlr)) {
1081 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1082 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1083 return -EINTR;
1084 }
1085 } else {
1086 ms = 8LL * 1000LL * xfer->len;
1087 do_div(ms, xfer->speed_hz);
1088 ms += ms + 200; /* some tolerance */
1089
1090 if (ms > UINT_MAX)
1091 ms = UINT_MAX;
1092
1093 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1094 msecs_to_jiffies(ms));
1095
1096 if (ms == 0) {
1097 SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1098 SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1099 dev_err(&msg->spi->dev,
1100 "SPI transfer timed out\n");
1101 return -ETIMEDOUT;
1102 }
1103 }
1104
1105 return 0;
1106}
1107
Martin Sperl0ff2de82019-02-23 08:49:48 +00001108static void _spi_transfer_delay_ns(u32 ns)
1109{
1110 if (!ns)
1111 return;
1112 if (ns <= 1000) {
1113 ndelay(ns);
1114 } else {
1115 u32 us = DIV_ROUND_UP(ns, 1000);
1116
1117 if (us <= 10)
1118 udelay(us);
1119 else
1120 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1121 }
1122}
1123
Alexandru Ardelean3984d392019-09-26 13:51:44 +03001124int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
Martin Sperl0ff2de82019-02-23 08:49:48 +00001125{
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001126 u32 delay = _delay->value;
1127 u32 unit = _delay->unit;
Martin Sperld5864e52019-02-23 08:49:50 +00001128 u32 hz;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001129
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001130 if (!delay)
1131 return 0;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001132
1133 switch (unit) {
1134 case SPI_DELAY_UNIT_USECS:
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001135 delay *= 1000;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001136 break;
1137 case SPI_DELAY_UNIT_NSECS: /* nothing to do here */
1138 break;
Martin Sperld5864e52019-02-23 08:49:50 +00001139 case SPI_DELAY_UNIT_SCK:
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001140 /* clock cycles need to be obtained from spi_transfer */
1141 if (!xfer)
1142 return -EINVAL;
Martin Sperld5864e52019-02-23 08:49:50 +00001143 /* if there is no effective speed know, then approximate
1144 * by underestimating with half the requested hz
1145 */
1146 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001147 if (!hz)
1148 return -EINVAL;
Martin Sperld5864e52019-02-23 08:49:50 +00001149 delay *= DIV_ROUND_UP(1000000000, hz);
1150 break;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001151 default:
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001152 return -EINVAL;
1153 }
1154
1155 return delay;
1156}
Alexandru Ardelean3984d392019-09-26 13:51:44 +03001157EXPORT_SYMBOL_GPL(spi_delay_to_ns);
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001158
1159int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
1160{
1161 int delay;
1162
1163 if (!_delay)
1164 return -EINVAL;
1165
Alexandru Ardelean3984d392019-09-26 13:51:44 +03001166 delay = spi_delay_to_ns(_delay, xfer);
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001167 if (delay < 0)
1168 return delay;
1169
1170 _spi_transfer_delay_ns(delay);
1171
1172 return 0;
1173}
1174EXPORT_SYMBOL_GPL(spi_delay_exec);
1175
Martin Sperl0ff2de82019-02-23 08:49:48 +00001176static void _spi_transfer_cs_change_delay(struct spi_message *msg,
1177 struct spi_transfer *xfer)
1178{
Alexandru Ardelean329f0da2019-09-26 13:51:31 +03001179 u32 delay = xfer->cs_change_delay.value;
1180 u32 unit = xfer->cs_change_delay.unit;
1181 int ret;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001182
1183 /* return early on "fast" mode - for everything but USECS */
Alexandru Ardelean6b3f2362019-09-26 13:51:29 +03001184 if (!delay) {
1185 if (unit == SPI_DELAY_UNIT_USECS)
1186 _spi_transfer_delay_ns(10000);
Martin Sperl0ff2de82019-02-23 08:49:48 +00001187 return;
Alexandru Ardelean6b3f2362019-09-26 13:51:29 +03001188 }
Martin Sperl0ff2de82019-02-23 08:49:48 +00001189
Alexandru Ardelean329f0da2019-09-26 13:51:31 +03001190 ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1191 if (ret) {
Martin Sperl0ff2de82019-02-23 08:49:48 +00001192 dev_err_once(&msg->spi->dev,
1193 "Use of unsupported delay unit %i, using default of 10us\n",
Alexandru Ardelean329f0da2019-09-26 13:51:31 +03001194 unit);
1195 _spi_transfer_delay_ns(10000);
Martin Sperl0ff2de82019-02-23 08:49:48 +00001196 }
Martin Sperl0ff2de82019-02-23 08:49:48 +00001197}
1198
Mark Brownb1589352013-10-05 11:50:40 +01001199/*
1200 * spi_transfer_one_message - Default implementation of transfer_one_message()
1201 *
1202 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -07001203 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +01001204 * standard handling of delays and chip select management.
1205 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001206static int spi_transfer_one_message(struct spi_controller *ctlr,
Mark Brownb1589352013-10-05 11:50:40 +01001207 struct spi_message *msg)
1208{
1209 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +01001210 bool keep_cs = false;
1211 int ret = 0;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001212 struct spi_statistics *statm = &ctlr->statistics;
Martin Sperleca2ebc2015-06-22 13:00:36 +00001213 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +01001214
1215 spi_set_cs(msg->spi, true);
1216
Martin Sperleca2ebc2015-06-22 13:00:36 +00001217 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1218 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1219
Mark Brownb1589352013-10-05 11:50:40 +01001220 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1221 trace_spi_transfer_start(msg, xfer);
1222
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001223 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1224 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
Martin Sperleca2ebc2015-06-22 13:00:36 +00001225
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001226 if (!ctlr->ptp_sts_supported) {
1227 xfer->ptp_sts_word_pre = 0;
1228 ptp_read_system_prets(xfer->ptp_sts);
1229 }
1230
Mark Brown38ec10f2014-08-16 16:27:41 +01001231 if (xfer->tx_buf || xfer->rx_buf) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001232 reinit_completion(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001233
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001234 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
Mark Brown38ec10f2014-08-16 16:27:41 +01001235 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001236 SPI_STATISTICS_INCREMENT_FIELD(statm,
1237 errors);
1238 SPI_STATISTICS_INCREMENT_FIELD(stats,
1239 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001240 dev_err(&msg->spi->dev,
1241 "SPI transfer failed: %d\n", ret);
1242 goto out;
1243 }
Mark Brownb1589352013-10-05 11:50:40 +01001244
Mark Brownd57e7962018-11-15 16:08:32 -08001245 if (ret > 0) {
1246 ret = spi_transfer_wait(ctlr, msg, xfer);
1247 if (ret < 0)
1248 msg->status = ret;
1249 }
Mark Brown38ec10f2014-08-16 16:27:41 +01001250 } else {
1251 if (xfer->len)
1252 dev_err(&msg->spi->dev,
1253 "Bufferless transfer has length %u\n",
1254 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001255 }
Mark Brownb1589352013-10-05 11:50:40 +01001256
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001257 if (!ctlr->ptp_sts_supported) {
1258 ptp_read_system_postts(xfer->ptp_sts);
1259 xfer->ptp_sts_word_post = xfer->len;
1260 }
1261
Mark Brownb1589352013-10-05 11:50:40 +01001262 trace_spi_transfer_stop(msg, xfer);
1263
1264 if (msg->status != -EINPROGRESS)
1265 goto out;
1266
Alexandru Ardeleanbebcfd22019-09-26 13:51:36 +03001267 spi_transfer_delay_exec(xfer);
Mark Brownb1589352013-10-05 11:50:40 +01001268
1269 if (xfer->cs_change) {
1270 if (list_is_last(&xfer->transfer_list,
1271 &msg->transfers)) {
1272 keep_cs = true;
1273 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001274 spi_set_cs(msg->spi, false);
Martin Sperl0ff2de82019-02-23 08:49:48 +00001275 _spi_transfer_cs_change_delay(msg, xfer);
Mark Brown0b73aa62014-03-29 23:48:07 +00001276 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001277 }
1278 }
1279
1280 msg->actual_length += xfer->len;
1281 }
1282
1283out:
1284 if (ret != 0 || !keep_cs)
1285 spi_set_cs(msg->spi, false);
1286
1287 if (msg->status == -EINPROGRESS)
1288 msg->status = ret;
1289
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001290 if (msg->status && ctlr->handle_err)
1291 ctlr->handle_err(ctlr, msg);
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001292
Noralf Trønnesc9ba7a12019-04-13 20:24:13 +02001293 spi_res_release(ctlr, msg);
1294
Mark Brown0ed56252019-05-09 11:27:17 +09001295 spi_finalize_current_message(ctlr);
1296
Mark Brownb1589352013-10-05 11:50:40 +01001297 return ret;
1298}
1299
1300/**
1301 * spi_finalize_current_transfer - report completion of a transfer
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001302 * @ctlr: the controller reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001303 *
1304 * Called by SPI drivers using the core transfer_one_message()
1305 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001306 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001307 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001308void spi_finalize_current_transfer(struct spi_controller *ctlr)
Mark Brownb1589352013-10-05 11:50:40 +01001309{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001310 complete(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001311}
1312EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1313
Linus Walleijffbbdd212012-02-22 10:05:38 +01001314/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001315 * __spi_pump_messages - function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001316 * @ctlr: controller to process queue for
Mark Brownfc9e0f72014-12-10 13:46:33 +00001317 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001318 *
1319 * This function checks if there is any spi message in the queue that
1320 * needs processing and if so call out to the driver to initialize hardware
1321 * and transfer each message.
1322 *
Mark Brown0461a412014-12-09 21:38:05 +00001323 * Note that it is called both from the kthread itself and also from
1324 * inside spi_sync(); the queue extraction handling at the top of the
1325 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001326 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001327static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001328{
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001329 struct spi_transfer *xfer;
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001330 struct spi_message *msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001331 bool was_busy = false;
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001332 unsigned long flags;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001333 int ret;
1334
Mark Brown983aee52014-12-09 19:46:56 +00001335 /* Lock queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001336 spin_lock_irqsave(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001337
1338 /* Make sure we are not already running a message */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001339 if (ctlr->cur_msg) {
1340 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001341 return;
1342 }
1343
Mark Brownf0125f12019-01-23 17:29:53 +00001344 /* If another context is idling the device then defer */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001345 if (ctlr->idling) {
1346 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1347 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001348 return;
1349 }
1350
Mark Brown983aee52014-12-09 19:46:56 +00001351 /* Check if the queue is idle */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001352 if (list_empty(&ctlr->queue) || !ctlr->running) {
1353 if (!ctlr->busy) {
1354 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -07001355 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001356 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001357
Mark Brownf0125f12019-01-23 17:29:53 +00001358 /* Only do teardown in the thread */
1359 if (!in_kthread) {
1360 kthread_queue_work(&ctlr->kworker,
1361 &ctlr->pump_messages);
1362 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1363 return;
1364 }
1365
1366 ctlr->busy = false;
1367 ctlr->idling = true;
1368 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1369
1370 kfree(ctlr->dummy_rx);
1371 ctlr->dummy_rx = NULL;
1372 kfree(ctlr->dummy_tx);
1373 ctlr->dummy_tx = NULL;
1374 if (ctlr->unprepare_transfer_hardware &&
1375 ctlr->unprepare_transfer_hardware(ctlr))
1376 dev_err(&ctlr->dev,
1377 "failed to unprepare transfer hardware\n");
1378 if (ctlr->auto_runtime_pm) {
1379 pm_runtime_mark_last_busy(ctlr->dev.parent);
1380 pm_runtime_put_autosuspend(ctlr->dev.parent);
1381 }
1382 trace_spi_controller_idle(ctlr);
1383
1384 spin_lock_irqsave(&ctlr->queue_lock, flags);
1385 ctlr->idling = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001386 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001387 return;
1388 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001389
Linus Walleijffbbdd212012-02-22 10:05:38 +01001390 /* Extract head of queue */
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001391 msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1392 ctlr->cur_msg = msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001393
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001394 list_del_init(&msg->queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001395 if (ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001396 was_busy = true;
1397 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001398 ctlr->busy = true;
1399 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001400
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001401 mutex_lock(&ctlr->io_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001402
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001403 if (!was_busy && ctlr->auto_runtime_pm) {
1404 ret = pm_runtime_get_sync(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001405 if (ret < 0) {
Tony Lindgren7e48e232018-05-18 10:30:07 -07001406 pm_runtime_put_noidle(ctlr->dev.parent);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001407 dev_err(&ctlr->dev, "Failed to power device: %d\n",
Mark Brown49834de2013-07-28 14:47:02 +01001408 ret);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001409 mutex_unlock(&ctlr->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001410 return;
1411 }
1412 }
1413
Mark Brown56ec1972013-10-07 19:33:53 +01001414 if (!was_busy)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001415 trace_spi_controller_busy(ctlr);
Mark Brown56ec1972013-10-07 19:33:53 +01001416
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001417 if (!was_busy && ctlr->prepare_transfer_hardware) {
1418 ret = ctlr->prepare_transfer_hardware(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001419 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001420 dev_err(&ctlr->dev,
Super Liuf3440d92019-05-22 14:30:14 +08001421 "failed to prepare transfer hardware: %d\n",
1422 ret);
Mark Brown49834de2013-07-28 14:47:02 +01001423
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001424 if (ctlr->auto_runtime_pm)
1425 pm_runtime_put(ctlr->dev.parent);
Super Liuf3440d92019-05-22 14:30:14 +08001426
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001427 msg->status = ret;
Super Liuf3440d92019-05-22 14:30:14 +08001428 spi_finalize_current_message(ctlr);
1429
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001430 mutex_unlock(&ctlr->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001431 return;
1432 }
1433 }
1434
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001435 trace_spi_message_start(msg);
Mark Brown56ec1972013-10-07 19:33:53 +01001436
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001437 if (ctlr->prepare_message) {
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001438 ret = ctlr->prepare_message(ctlr, msg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001439 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001440 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1441 ret);
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001442 msg->status = ret;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001443 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001444 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001445 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001446 ctlr->cur_msg_prepared = true;
Mark Brown2841a5f2013-10-05 00:23:12 +01001447 }
1448
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001449 ret = spi_map_msg(ctlr, msg);
Mark Brown99adef32014-01-16 12:22:43 +00001450 if (ret) {
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001451 msg->status = ret;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001452 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001453 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001454 }
1455
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001456 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1457 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1458 xfer->ptp_sts_word_pre = 0;
1459 ptp_read_system_prets(xfer->ptp_sts);
1460 }
1461 }
1462
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001463 ret = ctlr->transfer_one_message(ctlr, msg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001464 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001465 dev_err(&ctlr->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001466 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001467 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001468 }
Jon Hunter49023d22016-03-08 12:28:20 +00001469
1470out:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001471 mutex_unlock(&ctlr->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001472
1473 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001474 if (!ret)
1475 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001476}
1477
Mark Brownfc9e0f72014-12-10 13:46:33 +00001478/**
1479 * spi_pump_messages - kthread work function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001480 * @work: pointer to kthread work struct contained in the controller struct
Mark Brownfc9e0f72014-12-10 13:46:33 +00001481 */
1482static void spi_pump_messages(struct kthread_work *work)
1483{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001484 struct spi_controller *ctlr =
1485 container_of(work, struct spi_controller, pump_messages);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001486
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001487 __spi_pump_messages(ctlr, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001488}
1489
Douglas Anderson924b5862019-05-15 09:48:12 -07001490/**
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001491 * spi_take_timestamp_pre - helper for drivers to collect the beginning of the
1492 * TX timestamp for the requested byte from the SPI
1493 * transfer. The frequency with which this function
1494 * must be called (once per word, once for the whole
1495 * transfer, once per batch of words etc) is arbitrary
1496 * as long as the @tx buffer offset is greater than or
1497 * equal to the requested byte at the time of the
1498 * call. The timestamp is only taken once, at the
1499 * first such call. It is assumed that the driver
1500 * advances its @tx buffer pointer monotonically.
1501 * @ctlr: Pointer to the spi_controller structure of the driver
1502 * @xfer: Pointer to the transfer being timestamped
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001503 * @progress: How many words (not bytes) have been transferred so far
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001504 * @irqs_off: If true, will disable IRQs and preemption for the duration of the
1505 * transfer, for less jitter in time measurement. Only compatible
1506 * with PIO drivers. If true, must follow up with
1507 * spi_take_timestamp_post or otherwise system will crash.
1508 * WARNING: for fully predictable results, the CPU frequency must
1509 * also be under control (governor).
1510 */
1511void spi_take_timestamp_pre(struct spi_controller *ctlr,
1512 struct spi_transfer *xfer,
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001513 size_t progress, bool irqs_off)
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001514{
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001515 if (!xfer->ptp_sts)
1516 return;
1517
1518 if (xfer->timestamped_pre)
1519 return;
1520
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001521 if (progress < xfer->ptp_sts_word_pre)
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001522 return;
1523
1524 /* Capture the resolution of the timestamp */
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001525 xfer->ptp_sts_word_pre = progress;
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001526
1527 xfer->timestamped_pre = true;
1528
1529 if (irqs_off) {
1530 local_irq_save(ctlr->irq_flags);
1531 preempt_disable();
1532 }
1533
1534 ptp_read_system_prets(xfer->ptp_sts);
1535}
1536EXPORT_SYMBOL_GPL(spi_take_timestamp_pre);
1537
1538/**
1539 * spi_take_timestamp_post - helper for drivers to collect the end of the
1540 * TX timestamp for the requested byte from the SPI
1541 * transfer. Can be called with an arbitrary
1542 * frequency: only the first call where @tx exceeds
1543 * or is equal to the requested word will be
1544 * timestamped.
1545 * @ctlr: Pointer to the spi_controller structure of the driver
1546 * @xfer: Pointer to the transfer being timestamped
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001547 * @progress: How many words (not bytes) have been transferred so far
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001548 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1549 */
1550void spi_take_timestamp_post(struct spi_controller *ctlr,
1551 struct spi_transfer *xfer,
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001552 size_t progress, bool irqs_off)
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001553{
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001554 if (!xfer->ptp_sts)
1555 return;
1556
1557 if (xfer->timestamped_post)
1558 return;
1559
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001560 if (progress < xfer->ptp_sts_word_post)
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001561 return;
1562
1563 ptp_read_system_postts(xfer->ptp_sts);
1564
1565 if (irqs_off) {
1566 local_irq_restore(ctlr->irq_flags);
1567 preempt_enable();
1568 }
1569
1570 /* Capture the resolution of the timestamp */
Vladimir Oltean862dd2a2019-12-27 03:24:17 +02001571 xfer->ptp_sts_word_post = progress;
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001572
1573 xfer->timestamped_post = true;
1574}
1575EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
1576
1577/**
Douglas Anderson924b5862019-05-15 09:48:12 -07001578 * spi_set_thread_rt - set the controller to pump at realtime priority
1579 * @ctlr: controller to boost priority of
1580 *
1581 * This can be called because the controller requested realtime priority
1582 * (by setting the ->rt value before calling spi_register_controller()) or
1583 * because a device on the bus said that its transfers needed realtime
1584 * priority.
1585 *
1586 * NOTE: at the moment if any device on a bus says it needs realtime then
1587 * the thread will be at realtime priority for all transfers on that
1588 * controller. If this eventually becomes a problem we may see if we can
1589 * find a way to boost the priority only temporarily during relevant
1590 * transfers.
1591 */
1592static void spi_set_thread_rt(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001593{
Peter Zijlstra4ff13d02019-08-01 13:13:53 +02001594 struct sched_param param = { .sched_priority = MAX_RT_PRIO / 2 };
Linus Walleijffbbdd212012-02-22 10:05:38 +01001595
Douglas Anderson924b5862019-05-15 09:48:12 -07001596 dev_info(&ctlr->dev,
1597 "will run message pump with realtime priority\n");
1598 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
1599}
1600
1601static int spi_init_queue(struct spi_controller *ctlr)
1602{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001603 ctlr->running = false;
1604 ctlr->busy = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001605
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001606 kthread_init_worker(&ctlr->kworker);
1607 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1608 "%s", dev_name(&ctlr->dev));
1609 if (IS_ERR(ctlr->kworker_task)) {
1610 dev_err(&ctlr->dev, "failed to create message pump task\n");
1611 return PTR_ERR(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001612 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001613 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
Mark Brownf0125f12019-01-23 17:29:53 +00001614
Linus Walleijffbbdd212012-02-22 10:05:38 +01001615 /*
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001616 * Controller config will indicate if this controller should run the
Linus Walleijffbbdd212012-02-22 10:05:38 +01001617 * message pump with high (realtime) priority to reduce the transfer
1618 * latency on the bus by minimising the delay between a transfer
1619 * request and the scheduling of the message pump thread. Without this
1620 * setting the message pump thread will remain at default priority.
1621 */
Douglas Anderson924b5862019-05-15 09:48:12 -07001622 if (ctlr->rt)
1623 spi_set_thread_rt(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001624
1625 return 0;
1626}
1627
1628/**
1629 * spi_get_next_queued_message() - called by driver to check for queued
1630 * messages
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001631 * @ctlr: the controller to check for queued messages
Linus Walleijffbbdd212012-02-22 10:05:38 +01001632 *
1633 * If there are more messages in the queue, the next message is returned from
1634 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001635 *
1636 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001637 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001638struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001639{
1640 struct spi_message *next;
1641 unsigned long flags;
1642
1643 /* get a pointer to the next message, if any */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001644 spin_lock_irqsave(&ctlr->queue_lock, flags);
1645 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
Axel Lin1cfd97f2014-01-02 15:16:40 +08001646 queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001647 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001648
1649 return next;
1650}
1651EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1652
1653/**
1654 * spi_finalize_current_message() - the current message is complete
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001655 * @ctlr: the controller to return the message to
Linus Walleijffbbdd212012-02-22 10:05:38 +01001656 *
1657 * Called by the driver to notify the core that the message in the front of the
1658 * queue is complete and can be removed from the queue.
1659 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001660void spi_finalize_current_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001661{
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001662 struct spi_transfer *xfer;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001663 struct spi_message *mesg;
1664 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001665 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001666
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001667 spin_lock_irqsave(&ctlr->queue_lock, flags);
1668 mesg = ctlr->cur_msg;
1669 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001670
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001671 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1672 list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
1673 ptp_read_system_postts(xfer->ptp_sts);
1674 xfer->ptp_sts_word_post = xfer->len;
1675 }
1676 }
1677
Vladimir Olteanf971a202019-12-27 03:24:44 +02001678 if (unlikely(ctlr->ptp_sts_supported)) {
1679 list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
1680 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped_pre);
1681 WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped_post);
1682 }
1683 }
1684
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001685 spi_unmap_msg(ctlr, mesg);
Mark Brown99adef32014-01-16 12:22:43 +00001686
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001687 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1688 ret = ctlr->unprepare_message(ctlr, mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001689 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001690 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1691 ret);
Mark Brown2841a5f2013-10-05 00:23:12 +01001692 }
1693 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001694
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001695 spin_lock_irqsave(&ctlr->queue_lock, flags);
1696 ctlr->cur_msg = NULL;
1697 ctlr->cur_msg_prepared = false;
Mark Brownf0125f12019-01-23 17:29:53 +00001698 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001699 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001700
1701 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001702
Linus Walleijffbbdd212012-02-22 10:05:38 +01001703 mesg->state = NULL;
1704 if (mesg->complete)
1705 mesg->complete(mesg->context);
1706}
1707EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1708
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001709static int spi_start_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001710{
1711 unsigned long flags;
1712
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001713 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001714
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001715 if (ctlr->running || ctlr->busy) {
1716 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001717 return -EBUSY;
1718 }
1719
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001720 ctlr->running = true;
1721 ctlr->cur_msg = NULL;
1722 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001723
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001724 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001725
1726 return 0;
1727}
1728
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001729static int spi_stop_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001730{
1731 unsigned long flags;
1732 unsigned limit = 500;
1733 int ret = 0;
1734
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001735 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001736
1737 /*
1738 * This is a bit lame, but is optimized for the common execution path.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001739 * A wait_queue on the ctlr->busy could be used, but then the common
Linus Walleijffbbdd212012-02-22 10:05:38 +01001740 * execution path (pump_messages) would be required to call wake_up or
1741 * friends on every SPI message. Do this instead.
1742 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001743 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1744 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001745 usleep_range(10000, 11000);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001746 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001747 }
1748
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001749 if (!list_empty(&ctlr->queue) || ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001750 ret = -EBUSY;
1751 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001752 ctlr->running = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001753
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001754 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001755
1756 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001757 dev_warn(&ctlr->dev, "could not stop message queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001758 return ret;
1759 }
1760 return ret;
1761}
1762
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001763static int spi_destroy_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001764{
1765 int ret;
1766
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001767 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001768
1769 /*
Petr Mladek39891442016-10-11 13:55:20 -07001770 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001771 * If the reason that stop_queue timed out is that the work will never
1772 * finish, then it does no good to call flush/stop thread, so
1773 * return anyway.
1774 */
1775 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001776 dev_err(&ctlr->dev, "problem destroying queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001777 return ret;
1778 }
1779
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001780 kthread_flush_worker(&ctlr->kworker);
1781 kthread_stop(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001782
1783 return 0;
1784}
1785
Mark Brown0461a412014-12-09 21:38:05 +00001786static int __spi_queued_transfer(struct spi_device *spi,
1787 struct spi_message *msg,
1788 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001789{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001790 struct spi_controller *ctlr = spi->controller;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001791 unsigned long flags;
1792
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001793 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001794
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001795 if (!ctlr->running) {
1796 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001797 return -ESHUTDOWN;
1798 }
1799 msg->actual_length = 0;
1800 msg->status = -EINPROGRESS;
1801
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001802 list_add_tail(&msg->queue, &ctlr->queue);
Mark Brownf0125f12019-01-23 17:29:53 +00001803 if (!ctlr->busy && need_pump)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001804 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001805
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001806 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001807 return 0;
1808}
1809
Mark Brown0461a412014-12-09 21:38:05 +00001810/**
1811 * spi_queued_transfer - transfer function for queued transfers
1812 * @spi: spi device which is requesting transfer
1813 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001814 *
1815 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001816 */
1817static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1818{
1819 return __spi_queued_transfer(spi, msg, true);
1820}
1821
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001822static int spi_controller_initialize_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001823{
1824 int ret;
1825
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001826 ctlr->transfer = spi_queued_transfer;
1827 if (!ctlr->transfer_one_message)
1828 ctlr->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001829
1830 /* Initialize and start queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001831 ret = spi_init_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001832 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001833 dev_err(&ctlr->dev, "problem initializing queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001834 goto err_init_queue;
1835 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001836 ctlr->queued = true;
1837 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001838 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001839 dev_err(&ctlr->dev, "problem starting queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001840 goto err_start_queue;
1841 }
1842
1843 return 0;
1844
1845err_start_queue:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001846 spi_destroy_queue(ctlr);
Mark Brownc3676d52014-05-01 10:47:52 -07001847err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001848 return ret;
1849}
1850
Boris Brezillon988f2592018-04-22 20:35:15 +02001851/**
1852 * spi_flush_queue - Send all pending messages in the queue from the callers'
1853 * context
1854 * @ctlr: controller to process queue for
1855 *
1856 * This should be used when one wants to ensure all pending messages have been
1857 * sent before doing something. Is used by the spi-mem code to make sure SPI
1858 * memory operations do not preempt regular SPI transfers that have been queued
1859 * before the spi-mem operation.
1860 */
1861void spi_flush_queue(struct spi_controller *ctlr)
1862{
1863 if (ctlr->transfer == spi_queued_transfer)
1864 __spi_pump_messages(ctlr, false);
1865}
1866
Linus Walleijffbbdd212012-02-22 10:05:38 +01001867/*-------------------------------------------------------------------------*/
1868
Andreas Larsson7cb94362012-12-04 15:09:38 +01001869#if defined(CONFIG_OF)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001870static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001871 struct device_node *nc)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001872{
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001873 u32 value;
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001874 int rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001875
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001876 /* Mode (clock phase/polarity/etc.) */
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001877 if (of_property_read_bool(nc, "spi-cpha"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001878 spi->mode |= SPI_CPHA;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001879 if (of_property_read_bool(nc, "spi-cpol"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001880 spi->mode |= SPI_CPOL;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001881 if (of_property_read_bool(nc, "spi-3wire"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001882 spi->mode |= SPI_3WIRE;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001883 if (of_property_read_bool(nc, "spi-lsb-first"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001884 spi->mode |= SPI_LSB_FIRST;
Gregory CLEMENT3e5ec1d2019-10-18 17:29:29 +02001885 if (of_property_read_bool(nc, "spi-cs-high"))
Linus Walleijf3186dd2019-01-07 16:51:50 +01001886 spi->mode |= SPI_CS_HIGH;
1887
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001888 /* Device DUAL/QUAD mode */
1889 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1890 switch (value) {
1891 case 1:
1892 break;
1893 case 2:
1894 spi->mode |= SPI_TX_DUAL;
1895 break;
1896 case 4:
1897 spi->mode |= SPI_TX_QUAD;
1898 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001899 case 8:
1900 spi->mode |= SPI_TX_OCTAL;
1901 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001902 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001903 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001904 "spi-tx-bus-width %d not supported\n",
1905 value);
1906 break;
1907 }
1908 }
1909
1910 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1911 switch (value) {
1912 case 1:
1913 break;
1914 case 2:
1915 spi->mode |= SPI_RX_DUAL;
1916 break;
1917 case 4:
1918 spi->mode |= SPI_RX_QUAD;
1919 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001920 case 8:
1921 spi->mode |= SPI_RX_OCTAL;
1922 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001923 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001924 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001925 "spi-rx-bus-width %d not supported\n",
1926 value);
1927 break;
1928 }
1929 }
1930
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001931 if (spi_controller_is_slave(ctlr)) {
Rob Herring194276b2018-12-05 13:50:41 -06001932 if (!of_node_name_eq(nc, "slave")) {
Rob Herring25c56c82017-07-18 16:43:31 -05001933 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
1934 nc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001935 return -EINVAL;
1936 }
1937 return 0;
1938 }
1939
1940 /* Device address */
1941 rc = of_property_read_u32(nc, "reg", &value);
1942 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05001943 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
1944 nc, rc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001945 return rc;
1946 }
1947 spi->chip_select = value;
1948
Gregory CLEMENT3e5ec1d2019-10-18 17:29:29 +02001949 /*
1950 * For descriptors associated with the device, polarity inversion is
1951 * handled in the gpiolib, so all gpio chip selects are "active high"
1952 * in the logical sense, the gpiolib will invert the line if need be.
1953 */
Gregory CLEMENT15f794b2019-10-24 16:13:09 +02001954 if ((ctlr->use_gpio_descriptors) && ctlr->cs_gpiods &&
1955 ctlr->cs_gpiods[spi->chip_select])
Gregory CLEMENT3e5ec1d2019-10-18 17:29:29 +02001956 spi->mode |= SPI_CS_HIGH;
1957
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001958 /* Device speed */
1959 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1960 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001961 dev_err(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05001962 "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001963 return rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001964 }
1965 spi->max_speed_hz = value;
1966
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001967 return 0;
1968}
1969
1970static struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001971of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001972{
1973 struct spi_device *spi;
1974 int rc;
1975
1976 /* Alloc an spi_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001977 spi = spi_alloc_device(ctlr);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001978 if (!spi) {
Rob Herring25c56c82017-07-18 16:43:31 -05001979 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001980 rc = -ENOMEM;
1981 goto err_out;
1982 }
1983
1984 /* Select device driver */
1985 rc = of_modalias_node(nc, spi->modalias,
1986 sizeof(spi->modalias));
1987 if (rc < 0) {
Rob Herring25c56c82017-07-18 16:43:31 -05001988 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001989 goto err_out;
1990 }
1991
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001992 rc = of_spi_parse_dt(ctlr, spi, nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001993 if (rc)
1994 goto err_out;
1995
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001996 /* Store a pointer to the node in the device structure */
1997 of_node_get(nc);
1998 spi->dev.of_node = nc;
1999
2000 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002001 rc = spi_add_device(spi);
2002 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05002003 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
Johan Hovold83241472017-01-30 17:47:05 +01002004 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002005 }
2006
2007 return spi;
2008
Johan Hovold83241472017-01-30 17:47:05 +01002009err_of_node_put:
2010 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002011err_out:
2012 spi_dev_put(spi);
2013 return ERR_PTR(rc);
2014}
2015
Grant Likelyd57a4282012-04-07 14:16:53 -06002016/**
2017 * of_register_spi_devices() - Register child devices onto the SPI bus
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002018 * @ctlr: Pointer to spi_controller device
Grant Likelyd57a4282012-04-07 14:16:53 -06002019 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002020 * Registers an spi_device for each child node of controller node which
2021 * represents a valid SPI slave.
Grant Likelyd57a4282012-04-07 14:16:53 -06002022 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002023static void of_register_spi_devices(struct spi_controller *ctlr)
Grant Likelyd57a4282012-04-07 14:16:53 -06002024{
2025 struct spi_device *spi;
2026 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06002027
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002028 if (!ctlr->dev.of_node)
Grant Likelyd57a4282012-04-07 14:16:53 -06002029 return;
2030
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002031 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01002032 if (of_node_test_and_set_flag(nc, OF_POPULATED))
2033 continue;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002034 spi = of_register_spi_device(ctlr, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02002035 if (IS_ERR(spi)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002036 dev_warn(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05002037 "Failed to create SPI device for %pOF\n", nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02002038 of_node_clear_flag(nc, OF_POPULATED);
2039 }
Grant Likelyd57a4282012-04-07 14:16:53 -06002040 }
2041}
2042#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002043static void of_register_spi_devices(struct spi_controller *ctlr) { }
Grant Likelyd57a4282012-04-07 14:16:53 -06002044#endif
2045
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002046#ifdef CONFIG_ACPI
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002047struct acpi_spi_lookup {
2048 struct spi_controller *ctlr;
2049 u32 max_speed_hz;
2050 u32 mode;
2051 int irq;
2052 u8 bits_per_word;
2053 u8 chip_select;
2054};
2055
2056static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
2057 struct acpi_spi_lookup *lookup)
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002058{
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002059 const union acpi_object *obj;
2060
2061 if (!x86_apple_machine)
2062 return;
2063
2064 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
2065 && obj->buffer.length >= 4)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002066 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002067
2068 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
2069 && obj->buffer.length == 8)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002070 lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002071
2072 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
2073 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002074 lookup->mode |= SPI_LSB_FIRST;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002075
2076 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
2077 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002078 lookup->mode |= SPI_CPOL;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002079
2080 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
2081 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002082 lookup->mode |= SPI_CPHA;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002083}
2084
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002085static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
2086{
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002087 struct acpi_spi_lookup *lookup = data;
2088 struct spi_controller *ctlr = lookup->ctlr;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002089
2090 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
2091 struct acpi_resource_spi_serialbus *sb;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002092 acpi_handle parent_handle;
2093 acpi_status status;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002094
2095 sb = &ares->data.spi_serial_bus;
2096 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002097
2098 status = acpi_get_handle(NULL,
2099 sb->resource_source.string_ptr,
2100 &parent_handle);
2101
Ard Biesheuvelb5e3cf42019-06-19 11:52:54 +02002102 if (ACPI_FAILURE(status) ||
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002103 ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
2104 return -ENODEV;
2105
Mika Westerberga0a90712016-02-08 17:14:28 +02002106 /*
2107 * ACPI DeviceSelection numbering is handled by the
2108 * host controller driver in Windows and can vary
2109 * from driver to driver. In Linux we always expect
2110 * 0 .. max - 1 so we need to ask the driver to
2111 * translate between the two schemes.
2112 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002113 if (ctlr->fw_translate_cs) {
2114 int cs = ctlr->fw_translate_cs(ctlr,
Mika Westerberga0a90712016-02-08 17:14:28 +02002115 sb->device_selection);
2116 if (cs < 0)
2117 return cs;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002118 lookup->chip_select = cs;
Mika Westerberga0a90712016-02-08 17:14:28 +02002119 } else {
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002120 lookup->chip_select = sb->device_selection;
Mika Westerberga0a90712016-02-08 17:14:28 +02002121 }
2122
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002123 lookup->max_speed_hz = sb->connection_speed;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002124
2125 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002126 lookup->mode |= SPI_CPHA;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002127 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002128 lookup->mode |= SPI_CPOL;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002129 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002130 lookup->mode |= SPI_CS_HIGH;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002131 }
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002132 } else if (lookup->irq < 0) {
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002133 struct resource r;
2134
2135 if (acpi_dev_resource_interrupt(ares, 0, &r))
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002136 lookup->irq = r.start;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002137 }
2138
2139 /* Always tell the ACPI core to skip this resource */
2140 return 1;
2141}
2142
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002143static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
Octavian Purdila7f244672016-07-08 19:13:11 +03002144 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002145{
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002146 acpi_handle parent_handle = NULL;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002147 struct list_head resource_list;
Ard Biesheuvelb28944c2019-06-20 14:36:49 +02002148 struct acpi_spi_lookup lookup = {};
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002149 struct spi_device *spi;
2150 int ret;
2151
Octavian Purdila7f244672016-07-08 19:13:11 +03002152 if (acpi_bus_get_status(adev) || !adev->status.present ||
2153 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002154 return AE_OK;
2155
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002156 lookup.ctlr = ctlr;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002157 lookup.irq = -1;
2158
2159 INIT_LIST_HEAD(&resource_list);
2160 ret = acpi_dev_get_resources(adev, &resource_list,
2161 acpi_spi_add_resource, &lookup);
2162 acpi_dev_free_resource_list(&resource_list);
2163
2164 if (ret < 0)
2165 /* found SPI in _CRS but it points to another controller */
2166 return AE_OK;
2167
2168 if (!lookup.max_speed_hz &&
2169 !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) &&
2170 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) {
2171 /* Apple does not use _CRS but nested devices for SPI slaves */
2172 acpi_spi_parse_apple_properties(adev, &lookup);
2173 }
2174
2175 if (!lookup.max_speed_hz)
2176 return AE_OK;
2177
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002178 spi = spi_alloc_device(ctlr);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002179 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002180 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002181 dev_name(&adev->dev));
2182 return AE_NO_MEMORY;
2183 }
2184
John Garryea235782020-02-28 23:18:49 +08002185
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01002186 ACPI_COMPANION_SET(&spi->dev, adev);
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002187 spi->max_speed_hz = lookup.max_speed_hz;
John Garryea235782020-02-28 23:18:49 +08002188 spi->mode |= lookup.mode;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002189 spi->irq = lookup.irq;
2190 spi->bits_per_word = lookup.bits_per_word;
2191 spi->chip_select = lookup.chip_select;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002192
Dan O'Donovan0c6543f2017-02-05 16:30:14 +00002193 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
2194 sizeof(spi->modalias));
2195
Christophe RICARD33ada672015-12-23 23:25:35 +01002196 if (spi->irq < 0)
2197 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
2198
Octavian Purdila7f244672016-07-08 19:13:11 +03002199 acpi_device_set_enumerated(adev);
2200
Mika Westerberg33cf00e2013-10-10 13:28:48 +03002201 adev->power.flags.ignore_parent = true;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002202 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03002203 adev->power.flags.ignore_parent = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002204 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002205 dev_name(&adev->dev));
2206 spi_dev_put(spi);
2207 }
2208
2209 return AE_OK;
2210}
2211
Octavian Purdila7f244672016-07-08 19:13:11 +03002212static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
2213 void *data, void **return_value)
2214{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002215 struct spi_controller *ctlr = data;
Octavian Purdila7f244672016-07-08 19:13:11 +03002216 struct acpi_device *adev;
2217
2218 if (acpi_bus_get_device(handle, &adev))
2219 return AE_OK;
2220
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002221 return acpi_register_spi_device(ctlr, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03002222}
2223
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002224#define SPI_ACPI_ENUMERATE_MAX_DEPTH 32
2225
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002226static void acpi_register_spi_devices(struct spi_controller *ctlr)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002227{
2228 acpi_status status;
2229 acpi_handle handle;
2230
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002231 handle = ACPI_HANDLE(ctlr->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002232 if (!handle)
2233 return;
2234
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002235 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
2236 SPI_ACPI_ENUMERATE_MAX_DEPTH,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002237 acpi_spi_add_device, NULL, ctlr, NULL);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002238 if (ACPI_FAILURE(status))
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002239 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002240}
2241#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002242static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002243#endif /* CONFIG_ACPI */
2244
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002245static void spi_controller_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08002246{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002247 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002248
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002249 ctlr = container_of(dev, struct spi_controller, dev);
2250 kfree(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002251}
2252
2253static struct class spi_master_class = {
2254 .name = "spi_master",
2255 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002256 .dev_release = spi_controller_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00002257 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08002258};
2259
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002260#ifdef CONFIG_SPI_SLAVE
2261/**
2262 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2263 * controller
2264 * @spi: device used for the current transfer
2265 */
2266int spi_slave_abort(struct spi_device *spi)
2267{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002268 struct spi_controller *ctlr = spi->controller;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002269
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002270 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
2271 return ctlr->slave_abort(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002272
2273 return -ENOTSUPP;
2274}
2275EXPORT_SYMBOL_GPL(spi_slave_abort);
2276
2277static int match_true(struct device *dev, void *data)
2278{
2279 return 1;
2280}
2281
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002282static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
2283 char *buf)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002284{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002285 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2286 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002287 struct device *child;
2288
2289 child = device_find_child(&ctlr->dev, NULL, match_true);
2290 return sprintf(buf, "%s\n",
2291 child ? to_spi_device(child)->modalias : NULL);
2292}
2293
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002294static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
2295 const char *buf, size_t count)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002296{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002297 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2298 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002299 struct spi_device *spi;
2300 struct device *child;
2301 char name[32];
2302 int rc;
2303
2304 rc = sscanf(buf, "%31s", name);
2305 if (rc != 1 || !name[0])
2306 return -EINVAL;
2307
2308 child = device_find_child(&ctlr->dev, NULL, match_true);
2309 if (child) {
2310 /* Remove registered slave */
2311 device_unregister(child);
2312 put_device(child);
2313 }
2314
2315 if (strcmp(name, "(null)")) {
2316 /* Register new slave */
2317 spi = spi_alloc_device(ctlr);
2318 if (!spi)
2319 return -ENOMEM;
2320
2321 strlcpy(spi->modalias, name, sizeof(spi->modalias));
2322
2323 rc = spi_add_device(spi);
2324 if (rc) {
2325 spi_dev_put(spi);
2326 return rc;
2327 }
2328 }
2329
2330 return count;
2331}
2332
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002333static DEVICE_ATTR_RW(slave);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002334
2335static struct attribute *spi_slave_attrs[] = {
2336 &dev_attr_slave.attr,
2337 NULL,
2338};
2339
2340static const struct attribute_group spi_slave_group = {
2341 .attrs = spi_slave_attrs,
2342};
2343
2344static const struct attribute_group *spi_slave_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002345 &spi_controller_statistics_group,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002346 &spi_slave_group,
2347 NULL,
2348};
2349
2350static struct class spi_slave_class = {
2351 .name = "spi_slave",
2352 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002353 .dev_release = spi_controller_release,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002354 .dev_groups = spi_slave_groups,
2355};
2356#else
2357extern struct class spi_slave_class; /* dummy */
2358#endif
David Brownell8ae12a02006-01-08 13:34:19 -08002359
2360/**
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002361 * __spi_alloc_controller - allocate an SPI master or slave controller
David Brownell8ae12a02006-01-08 13:34:19 -08002362 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07002363 * @size: how much zeroed driver-private data to allocate; the pointer to this
Lukas Wunner229e6af2019-09-11 12:15:30 +02002364 * memory is in the driver_data field of the returned device, accessible
2365 * with spi_controller_get_devdata(); the memory is cacheline aligned;
2366 * drivers granting DMA access to portions of their private data need to
2367 * round up @size using ALIGN(size, dma_get_cache_alignment()).
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002368 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2369 * slave (true) controller
David Brownell33e34dc2007-05-08 00:32:21 -07002370 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002371 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002372 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002373 * only ones directly touching chip registers. It's how they allocate
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002374 * an spi_controller structure, prior to calling spi_register_controller().
David Brownell8ae12a02006-01-08 13:34:19 -08002375 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002376 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08002377 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002378 * The caller is responsible for assigning the bus number and initializing the
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002379 * controller's methods before calling spi_register_controller(); and (after
2380 * errors adding the device) calling spi_controller_put() to prevent a memory
2381 * leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002382 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002383 * Return: the SPI controller structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002384 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002385struct spi_controller *__spi_alloc_controller(struct device *dev,
2386 unsigned int size, bool slave)
David Brownell8ae12a02006-01-08 13:34:19 -08002387{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002388 struct spi_controller *ctlr;
Lukas Wunner229e6af2019-09-11 12:15:30 +02002389 size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
David Brownell8ae12a02006-01-08 13:34:19 -08002390
David Brownell0c8684612006-01-08 13:34:25 -08002391 if (!dev)
2392 return NULL;
2393
Lukas Wunner229e6af2019-09-11 12:15:30 +02002394 ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002395 if (!ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002396 return NULL;
2397
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002398 device_initialize(&ctlr->dev);
2399 ctlr->bus_num = -1;
2400 ctlr->num_chipselect = 1;
2401 ctlr->slave = slave;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002402 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002403 ctlr->dev.class = &spi_slave_class;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002404 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002405 ctlr->dev.class = &spi_master_class;
2406 ctlr->dev.parent = dev;
2407 pm_suspend_ignore_children(&ctlr->dev, true);
Lukas Wunner229e6af2019-09-11 12:15:30 +02002408 spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
David Brownell8ae12a02006-01-08 13:34:19 -08002409
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002410 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002411}
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002412EXPORT_SYMBOL_GPL(__spi_alloc_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002413
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002414#ifdef CONFIG_OF
Linus Walleij43004f32019-08-08 17:03:21 +02002415static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002416{
Grant Likelye80beb22013-02-12 17:48:37 +00002417 int nb, i, *cs;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002418 struct device_node *np = ctlr->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002419
2420 if (!np)
2421 return 0;
2422
2423 nb = of_gpio_named_count(np, "cs-gpios");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002424 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002425
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002426 /* Return error only for an incorrectly formed cs-gpios property */
2427 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002428 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002429 else if (nb < 0)
2430 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002431
Kees Cooka86854d2018-06-12 14:07:58 -07002432 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002433 GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002434 ctlr->cs_gpios = cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002435
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002436 if (!ctlr->cs_gpios)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002437 return -ENOMEM;
2438
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002439 for (i = 0; i < ctlr->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01002440 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002441
2442 for (i = 0; i < nb; i++)
2443 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2444
2445 return 0;
2446}
2447#else
Linus Walleij43004f32019-08-08 17:03:21 +02002448static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002449{
2450 return 0;
2451}
2452#endif
2453
Linus Walleijf3186dd2019-01-07 16:51:50 +01002454/**
2455 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2456 * @ctlr: The SPI master to grab GPIO descriptors for
2457 */
2458static int spi_get_gpio_descs(struct spi_controller *ctlr)
2459{
2460 int nb, i;
2461 struct gpio_desc **cs;
2462 struct device *dev = &ctlr->dev;
Geert Uytterhoeven7d93aec2020-01-02 14:38:17 +01002463 unsigned long native_cs_mask = 0;
2464 unsigned int num_cs_gpios = 0;
Linus Walleijf3186dd2019-01-07 16:51:50 +01002465
2466 nb = gpiod_count(dev, "cs");
2467 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2468
2469 /* No GPIOs at all is fine, else return the error */
2470 if (nb == 0 || nb == -ENOENT)
2471 return 0;
2472 else if (nb < 0)
2473 return nb;
2474
2475 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
2476 GFP_KERNEL);
2477 if (!cs)
2478 return -ENOMEM;
2479 ctlr->cs_gpiods = cs;
2480
2481 for (i = 0; i < nb; i++) {
2482 /*
2483 * Most chipselects are active low, the inverted
2484 * semantics are handled by special quirks in gpiolib,
2485 * so initializing them GPIOD_OUT_LOW here means
2486 * "unasserted", in most cases this will drive the physical
2487 * line high.
2488 */
2489 cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
2490 GPIOD_OUT_LOW);
Geert Uytterhoeven1723fde2019-04-03 16:46:56 +02002491 if (IS_ERR(cs[i]))
2492 return PTR_ERR(cs[i]);
Linus Walleijf3186dd2019-01-07 16:51:50 +01002493
2494 if (cs[i]) {
2495 /*
2496 * If we find a CS GPIO, name it after the device and
2497 * chip select line.
2498 */
2499 char *gpioname;
2500
2501 gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
2502 dev_name(dev), i);
2503 if (!gpioname)
2504 return -ENOMEM;
2505 gpiod_set_consumer_name(cs[i], gpioname);
Geert Uytterhoeven7d93aec2020-01-02 14:38:17 +01002506 num_cs_gpios++;
2507 continue;
Linus Walleijf3186dd2019-01-07 16:51:50 +01002508 }
Geert Uytterhoeven7d93aec2020-01-02 14:38:17 +01002509
2510 if (ctlr->max_native_cs && i >= ctlr->max_native_cs) {
2511 dev_err(dev, "Invalid native chip select %d\n", i);
2512 return -EINVAL;
2513 }
2514 native_cs_mask |= BIT(i);
2515 }
2516
2517 ctlr->unused_native_cs = ffz(native_cs_mask);
2518 if (num_cs_gpios && ctlr->max_native_cs &&
2519 ctlr->unused_native_cs >= ctlr->max_native_cs) {
2520 dev_err(dev, "No unused native chip select available\n");
2521 return -EINVAL;
Linus Walleijf3186dd2019-01-07 16:51:50 +01002522 }
2523
2524 return 0;
2525}
2526
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002527static int spi_controller_check_ops(struct spi_controller *ctlr)
2528{
2529 /*
Boris Brezillonb5932f52018-04-26 18:18:15 +02002530 * The controller may implement only the high-level SPI-memory like
2531 * operations if it does not support regular SPI transfers, and this is
2532 * valid use case.
2533 * If ->mem_ops is NULL, we request that at least one of the
2534 * ->transfer_xxx() method be implemented.
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002535 */
Boris Brezillonb5932f52018-04-26 18:18:15 +02002536 if (ctlr->mem_ops) {
2537 if (!ctlr->mem_ops->exec_op)
2538 return -EINVAL;
2539 } else if (!ctlr->transfer && !ctlr->transfer_one &&
2540 !ctlr->transfer_one_message) {
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002541 return -EINVAL;
Boris Brezillonb5932f52018-04-26 18:18:15 +02002542 }
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002543
2544 return 0;
2545}
2546
David Brownell8ae12a02006-01-08 13:34:19 -08002547/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002548 * spi_register_controller - register SPI master or slave controller
2549 * @ctlr: initialized master, originally from spi_alloc_master() or
2550 * spi_alloc_slave()
David Brownell33e34dc2007-05-08 00:32:21 -07002551 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002552 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002553 * SPI controllers connect to their drivers using some non-SPI bus,
David Brownell8ae12a02006-01-08 13:34:19 -08002554 * such as the platform bus. The final stage of probe() in that code
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002555 * includes calling spi_register_controller() to hook up to this SPI bus glue.
David Brownell8ae12a02006-01-08 13:34:19 -08002556 *
2557 * SPI controllers use board specific (often SOC specific) bus numbers,
2558 * and board-specific addressing for SPI devices combines those numbers
2559 * with chip select numbers. Since SPI does not directly support dynamic
2560 * device identification, boards need configuration tables telling which
2561 * chip is at which address.
2562 *
2563 * This must be called from context that can sleep. It returns zero on
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002564 * success, else a negative error code (dropping the controller's refcount).
David Brownell0c8684612006-01-08 13:34:25 -08002565 * After a successful return, the caller is responsible for calling
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002566 * spi_unregister_controller().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002567 *
2568 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002569 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002570int spi_register_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002571{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002572 struct device *dev = ctlr->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08002573 struct boardinfo *bi;
Sergei Shtylyovb93318a2019-04-05 18:48:50 +03002574 int status;
Lucas Stach42bdd702017-10-16 12:27:58 +02002575 int id, first_dynamic;
David Brownell8ae12a02006-01-08 13:34:19 -08002576
David Brownell0c8684612006-01-08 13:34:25 -08002577 if (!dev)
2578 return -ENODEV;
2579
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002580 /*
2581 * Make sure all necessary hooks are implemented before registering
2582 * the SPI controller.
2583 */
2584 status = spi_controller_check_ops(ctlr);
2585 if (status)
2586 return status;
2587
Geert Uytterhoeven04b2d032018-08-21 11:53:03 +02002588 if (ctlr->bus_num >= 0) {
2589 /* devices with a fixed bus num must check-in with the num */
2590 mutex_lock(&board_lock);
2591 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2592 ctlr->bus_num + 1, GFP_KERNEL);
2593 mutex_unlock(&board_lock);
2594 if (WARN(id < 0, "couldn't get idr"))
2595 return id == -ENOSPC ? -EBUSY : id;
2596 ctlr->bus_num = id;
2597 } else if (ctlr->dev.of_node) {
2598 /* allocate dynamic bus number using Linux idr */
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302599 id = of_alias_get_id(ctlr->dev.of_node, "spi");
2600 if (id >= 0) {
2601 ctlr->bus_num = id;
2602 mutex_lock(&board_lock);
2603 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2604 ctlr->bus_num + 1, GFP_KERNEL);
2605 mutex_unlock(&board_lock);
2606 if (WARN(id < 0, "couldn't get idr"))
2607 return id == -ENOSPC ? -EBUSY : id;
2608 }
2609 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002610 if (ctlr->bus_num < 0) {
Lucas Stach42bdd702017-10-16 12:27:58 +02002611 first_dynamic = of_alias_get_highest_id("spi");
2612 if (first_dynamic < 0)
2613 first_dynamic = 0;
2614 else
2615 first_dynamic++;
2616
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302617 mutex_lock(&board_lock);
Lucas Stach42bdd702017-10-16 12:27:58 +02002618 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
2619 0, GFP_KERNEL);
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302620 mutex_unlock(&board_lock);
2621 if (WARN(id < 0, "couldn't get idr"))
2622 return id;
2623 ctlr->bus_num = id;
David Brownell8ae12a02006-01-08 13:34:19 -08002624 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002625 INIT_LIST_HEAD(&ctlr->queue);
2626 spin_lock_init(&ctlr->queue_lock);
2627 spin_lock_init(&ctlr->bus_lock_spinlock);
2628 mutex_init(&ctlr->bus_lock_mutex);
2629 mutex_init(&ctlr->io_mutex);
2630 ctlr->bus_lock_flag = 0;
2631 init_completion(&ctlr->xfer_completion);
2632 if (!ctlr->max_dma_len)
2633 ctlr->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002634
David Brownell8ae12a02006-01-08 13:34:19 -08002635 /* register the device, then userspace will see it.
2636 * registration fails if the bus ID is in use.
2637 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002638 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
Andrey Smirnov0a919ae2019-04-02 21:01:28 -07002639
2640 if (!spi_controller_is_slave(ctlr)) {
2641 if (ctlr->use_gpio_descriptors) {
2642 status = spi_get_gpio_descs(ctlr);
2643 if (status)
2644 return status;
2645 /*
2646 * A controller using GPIO descriptors always
2647 * supports SPI_CS_HIGH if need be.
2648 */
2649 ctlr->mode_bits |= SPI_CS_HIGH;
2650 } else {
2651 /* Legacy code path for GPIOs from DT */
Linus Walleij43004f32019-08-08 17:03:21 +02002652 status = of_spi_get_gpio_numbers(ctlr);
Andrey Smirnov0a919ae2019-04-02 21:01:28 -07002653 if (status)
2654 return status;
2655 }
2656 }
2657
Tudor Ambarusf9481b02019-06-19 14:38:28 +00002658 /*
2659 * Even if it's just one always-selected device, there must
2660 * be at least one chipselect.
2661 */
2662 if (!ctlr->num_chipselect)
2663 return -EINVAL;
2664
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002665 status = device_add(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302666 if (status < 0) {
2667 /* free bus id */
2668 mutex_lock(&board_lock);
2669 idr_remove(&spi_master_idr, ctlr->bus_num);
2670 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002671 goto done;
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302672 }
2673 dev_dbg(dev, "registered %s %s\n",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002674 spi_controller_is_slave(ctlr) ? "slave" : "master",
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302675 dev_name(&ctlr->dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002676
Boris Brezillonb5932f52018-04-26 18:18:15 +02002677 /*
2678 * If we're using a queued driver, start the queue. Note that we don't
2679 * need the queueing logic if the driver is only supporting high-level
2680 * memory operations.
2681 */
2682 if (ctlr->transfer) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002683 dev_info(dev, "controller is unqueued, this is deprecated\n");
Boris Brezillonb5932f52018-04-26 18:18:15 +02002684 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002685 status = spi_controller_initialize_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002686 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002687 device_del(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302688 /* free bus id */
2689 mutex_lock(&board_lock);
2690 idr_remove(&spi_master_idr, ctlr->bus_num);
2691 mutex_unlock(&board_lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002692 goto done;
2693 }
2694 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00002695 /* add statistics */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002696 spin_lock_init(&ctlr->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002697
Feng Tang2b9603a2010-08-02 15:52:15 +08002698 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002699 list_add_tail(&ctlr->list, &spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002700 list_for_each_entry(bi, &board_list, list)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002701 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +08002702 mutex_unlock(&board_lock);
2703
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002704 /* Register devices from the device tree and ACPI */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002705 of_register_spi_devices(ctlr);
2706 acpi_register_spi_devices(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002707done:
2708 return status;
2709}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002710EXPORT_SYMBOL_GPL(spi_register_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002711
Mark Brown666d5b42013-08-31 18:50:52 +01002712static void devm_spi_unregister(struct device *dev, void *res)
2713{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002714 spi_unregister_controller(*(struct spi_controller **)res);
Mark Brown666d5b42013-08-31 18:50:52 +01002715}
2716
2717/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002718 * devm_spi_register_controller - register managed SPI master or slave
2719 * controller
2720 * @dev: device managing SPI controller
2721 * @ctlr: initialized controller, originally from spi_alloc_master() or
2722 * spi_alloc_slave()
Mark Brown666d5b42013-08-31 18:50:52 +01002723 * Context: can sleep
2724 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002725 * Register a SPI device as with spi_register_controller() which will
Johan Hovold68b892f2017-10-30 11:35:26 +01002726 * automatically be unregistered and freed.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002727 *
2728 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01002729 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002730int devm_spi_register_controller(struct device *dev,
2731 struct spi_controller *ctlr)
Mark Brown666d5b42013-08-31 18:50:52 +01002732{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002733 struct spi_controller **ptr;
Mark Brown666d5b42013-08-31 18:50:52 +01002734 int ret;
2735
2736 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2737 if (!ptr)
2738 return -ENOMEM;
2739
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002740 ret = spi_register_controller(ctlr);
Stephen Warren4b928942013-11-21 16:11:15 -07002741 if (!ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002742 *ptr = ctlr;
Mark Brown666d5b42013-08-31 18:50:52 +01002743 devres_add(dev, ptr);
2744 } else {
2745 devres_free(ptr);
2746 }
2747
2748 return ret;
2749}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002750EXPORT_SYMBOL_GPL(devm_spi_register_controller);
Mark Brown666d5b42013-08-31 18:50:52 +01002751
David Lamparter34860082010-08-30 23:54:17 +02002752static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002753{
David Lamparter34860082010-08-30 23:54:17 +02002754 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002755 return 0;
2756}
2757
2758/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002759 * spi_unregister_controller - unregister SPI master or slave controller
2760 * @ctlr: the controller being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002761 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002762 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002763 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002764 * only ones directly touching chip registers.
2765 *
2766 * This must be called from context that can sleep.
Johan Hovold68b892f2017-10-30 11:35:26 +01002767 *
2768 * Note that this function also drops a reference to the controller.
David Brownell8ae12a02006-01-08 13:34:19 -08002769 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002770void spi_unregister_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002771{
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302772 struct spi_controller *found;
Johan Hovold67f7b272017-10-30 11:35:25 +01002773 int id = ctlr->bus_num;
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002774
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302775 /* First make sure that this controller was ever added */
2776 mutex_lock(&board_lock);
Johan Hovold67f7b272017-10-30 11:35:25 +01002777 found = idr_find(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302778 mutex_unlock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002779 if (ctlr->queued) {
2780 if (spi_destroy_queue(ctlr))
2781 dev_err(&ctlr->dev, "queue remove failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002782 }
Feng Tang2b9603a2010-08-02 15:52:15 +08002783 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002784 list_del(&ctlr->list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002785 mutex_unlock(&board_lock);
2786
Andy Shevchenkoebc37af2019-06-15 20:41:35 +03002787 device_for_each_child(&ctlr->dev, NULL, __unregister);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002788 device_unregister(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302789 /* free bus id */
2790 mutex_lock(&board_lock);
Jarkko Nikula613bd1e2018-03-20 10:27:50 +02002791 if (found == ctlr)
2792 idr_remove(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302793 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002794}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002795EXPORT_SYMBOL_GPL(spi_unregister_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002796
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002797int spi_controller_suspend(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002798{
2799 int ret;
2800
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002801 /* Basically no-ops for non-queued controllers */
2802 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002803 return 0;
2804
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002805 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002806 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002807 dev_err(&ctlr->dev, "queue stop failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002808
2809 return ret;
2810}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002811EXPORT_SYMBOL_GPL(spi_controller_suspend);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002812
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002813int spi_controller_resume(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002814{
2815 int ret;
2816
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002817 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002818 return 0;
2819
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002820 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002821 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002822 dev_err(&ctlr->dev, "queue restart failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002823
2824 return ret;
2825}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002826EXPORT_SYMBOL_GPL(spi_controller_resume);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002827
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002828static int __spi_controller_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002829{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002830 struct spi_controller *ctlr;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002831 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002832
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002833 ctlr = container_of(dev, struct spi_controller, dev);
2834 return ctlr->bus_num == *bus_num;
Dave Young5ed2c832008-01-22 15:14:18 +08002835}
2836
David Brownell8ae12a02006-01-08 13:34:19 -08002837/**
2838 * spi_busnum_to_master - look up master associated with bus_num
2839 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002840 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002841 *
2842 * This call may be used with devices that are registered after
2843 * arch init time. It returns a refcounted pointer to the relevant
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002844 * spi_controller (which the caller must release), or NULL if there is
David Brownell8ae12a02006-01-08 13:34:19 -08002845 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002846 *
2847 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002848 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002849struct spi_controller *spi_busnum_to_master(u16 bus_num)
David Brownell8ae12a02006-01-08 13:34:19 -08002850{
Tony Jones49dce682007-10-16 01:27:48 -07002851 struct device *dev;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002852 struct spi_controller *ctlr = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002853
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002854 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002855 __spi_controller_match);
Dave Young5ed2c832008-01-22 15:14:18 +08002856 if (dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002857 ctlr = container_of(dev, struct spi_controller, dev);
Dave Young5ed2c832008-01-22 15:14:18 +08002858 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002859 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002860}
2861EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2862
Martin Sperld780c372015-12-14 15:20:18 +00002863/*-------------------------------------------------------------------------*/
2864
2865/* Core methods for SPI resource management */
2866
2867/**
2868 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2869 * during the processing of a spi_message while using
2870 * spi_transfer_one
2871 * @spi: the spi device for which we allocate memory
2872 * @release: the release code to execute for this resource
2873 * @size: size to alloc and return
2874 * @gfp: GFP allocation flags
2875 *
2876 * Return: the pointer to the allocated data
2877 *
2878 * This may get enhanced in the future to allocate from a memory pool
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002879 * of the @spi_device or @spi_controller to avoid repeated allocations.
Martin Sperld780c372015-12-14 15:20:18 +00002880 */
2881void *spi_res_alloc(struct spi_device *spi,
2882 spi_res_release_t release,
2883 size_t size, gfp_t gfp)
2884{
2885 struct spi_res *sres;
2886
2887 sres = kzalloc(sizeof(*sres) + size, gfp);
2888 if (!sres)
2889 return NULL;
2890
2891 INIT_LIST_HEAD(&sres->entry);
2892 sres->release = release;
2893
2894 return sres->data;
2895}
2896EXPORT_SYMBOL_GPL(spi_res_alloc);
2897
2898/**
2899 * spi_res_free - free an spi resource
2900 * @res: pointer to the custom data of a resource
2901 *
2902 */
2903void spi_res_free(void *res)
2904{
2905 struct spi_res *sres = container_of(res, struct spi_res, data);
2906
2907 if (!res)
2908 return;
2909
2910 WARN_ON(!list_empty(&sres->entry));
2911 kfree(sres);
2912}
2913EXPORT_SYMBOL_GPL(spi_res_free);
2914
2915/**
2916 * spi_res_add - add a spi_res to the spi_message
2917 * @message: the spi message
2918 * @res: the spi_resource
2919 */
2920void spi_res_add(struct spi_message *message, void *res)
2921{
2922 struct spi_res *sres = container_of(res, struct spi_res, data);
2923
2924 WARN_ON(!list_empty(&sres->entry));
2925 list_add_tail(&sres->entry, &message->resources);
2926}
2927EXPORT_SYMBOL_GPL(spi_res_add);
2928
2929/**
2930 * spi_res_release - release all spi resources for this message
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002931 * @ctlr: the @spi_controller
Martin Sperld780c372015-12-14 15:20:18 +00002932 * @message: the @spi_message
2933 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002934void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
Martin Sperld780c372015-12-14 15:20:18 +00002935{
Vladimir Zapolskiyf5694362019-06-18 19:28:18 +03002936 struct spi_res *res, *tmp;
Martin Sperld780c372015-12-14 15:20:18 +00002937
Vladimir Zapolskiyf5694362019-06-18 19:28:18 +03002938 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
Martin Sperld780c372015-12-14 15:20:18 +00002939 if (res->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002940 res->release(ctlr, message, res->data);
Martin Sperld780c372015-12-14 15:20:18 +00002941
2942 list_del(&res->entry);
2943
2944 kfree(res);
2945 }
2946}
2947EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002948
2949/*-------------------------------------------------------------------------*/
2950
Martin Sperl523baf5a2015-12-14 15:20:19 +00002951/* Core methods for spi_message alterations */
2952
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002953static void __spi_replace_transfers_release(struct spi_controller *ctlr,
Martin Sperl523baf5a2015-12-14 15:20:19 +00002954 struct spi_message *msg,
2955 void *res)
2956{
2957 struct spi_replaced_transfers *rxfer = res;
2958 size_t i;
2959
2960 /* call extra callback if requested */
2961 if (rxfer->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002962 rxfer->release(ctlr, msg, res);
Martin Sperl523baf5a2015-12-14 15:20:19 +00002963
2964 /* insert replaced transfers back into the message */
2965 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2966
2967 /* remove the formerly inserted entries */
2968 for (i = 0; i < rxfer->inserted; i++)
2969 list_del(&rxfer->inserted_transfers[i].transfer_list);
2970}
2971
2972/**
2973 * spi_replace_transfers - replace transfers with several transfers
2974 * and register change with spi_message.resources
2975 * @msg: the spi_message we work upon
2976 * @xfer_first: the first spi_transfer we want to replace
2977 * @remove: number of transfers to remove
2978 * @insert: the number of transfers we want to insert instead
2979 * @release: extra release code necessary in some circumstances
2980 * @extradatasize: extra data to allocate (with alignment guarantees
2981 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002982 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002983 *
2984 * Returns: pointer to @spi_replaced_transfers,
2985 * PTR_ERR(...) in case of errors.
2986 */
2987struct spi_replaced_transfers *spi_replace_transfers(
2988 struct spi_message *msg,
2989 struct spi_transfer *xfer_first,
2990 size_t remove,
2991 size_t insert,
2992 spi_replaced_release_t release,
2993 size_t extradatasize,
2994 gfp_t gfp)
2995{
2996 struct spi_replaced_transfers *rxfer;
2997 struct spi_transfer *xfer;
2998 size_t i;
2999
3000 /* allocate the structure using spi_res */
3001 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
Gustavo A. R. Silvaaef97522019-06-07 13:48:45 -05003002 struct_size(rxfer, inserted_transfers, insert)
Martin Sperl523baf5a2015-12-14 15:20:19 +00003003 + extradatasize,
3004 gfp);
3005 if (!rxfer)
3006 return ERR_PTR(-ENOMEM);
3007
3008 /* the release code to invoke before running the generic release */
3009 rxfer->release = release;
3010
3011 /* assign extradata */
3012 if (extradatasize)
3013 rxfer->extradata =
3014 &rxfer->inserted_transfers[insert];
3015
3016 /* init the replaced_transfers list */
3017 INIT_LIST_HEAD(&rxfer->replaced_transfers);
3018
3019 /* assign the list_entry after which we should reinsert
3020 * the @replaced_transfers - it may be spi_message.messages!
3021 */
3022 rxfer->replaced_after = xfer_first->transfer_list.prev;
3023
3024 /* remove the requested number of transfers */
3025 for (i = 0; i < remove; i++) {
3026 /* if the entry after replaced_after it is msg->transfers
3027 * then we have been requested to remove more transfers
3028 * than are in the list
3029 */
3030 if (rxfer->replaced_after->next == &msg->transfers) {
3031 dev_err(&msg->spi->dev,
3032 "requested to remove more spi_transfers than are available\n");
3033 /* insert replaced transfers back into the message */
3034 list_splice(&rxfer->replaced_transfers,
3035 rxfer->replaced_after);
3036
3037 /* free the spi_replace_transfer structure */
3038 spi_res_free(rxfer);
3039
3040 /* and return with an error */
3041 return ERR_PTR(-EINVAL);
3042 }
3043
3044 /* remove the entry after replaced_after from list of
3045 * transfers and add it to list of replaced_transfers
3046 */
3047 list_move_tail(rxfer->replaced_after->next,
3048 &rxfer->replaced_transfers);
3049 }
3050
3051 /* create copy of the given xfer with identical settings
3052 * based on the first transfer to get removed
3053 */
3054 for (i = 0; i < insert; i++) {
3055 /* we need to run in reverse order */
3056 xfer = &rxfer->inserted_transfers[insert - 1 - i];
3057
3058 /* copy all spi_transfer data */
3059 memcpy(xfer, xfer_first, sizeof(*xfer));
3060
3061 /* add to list */
3062 list_add(&xfer->transfer_list, rxfer->replaced_after);
3063
Alexandru Ardeleanbebcfd22019-09-26 13:51:36 +03003064 /* clear cs_change and delay for all but the last */
Martin Sperl523baf5a2015-12-14 15:20:19 +00003065 if (i) {
3066 xfer->cs_change = false;
3067 xfer->delay_usecs = 0;
Alexandru Ardeleanbebcfd22019-09-26 13:51:36 +03003068 xfer->delay.value = 0;
Martin Sperl523baf5a2015-12-14 15:20:19 +00003069 }
3070 }
3071
3072 /* set up inserted */
3073 rxfer->inserted = insert;
3074
3075 /* and register it with spi_res/spi_message */
3076 spi_res_add(msg, rxfer);
3077
3078 return rxfer;
3079}
3080EXPORT_SYMBOL_GPL(spi_replace_transfers);
3081
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003082static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
Fabio Estevam08933412016-02-14 13:33:50 -02003083 struct spi_message *msg,
3084 struct spi_transfer **xferp,
3085 size_t maxsize,
3086 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00003087{
3088 struct spi_transfer *xfer = *xferp, *xfers;
3089 struct spi_replaced_transfers *srt;
3090 size_t offset;
3091 size_t count, i;
3092
Martin Sperld9f12122015-12-14 15:20:20 +00003093 /* calculate how many we have to replace */
3094 count = DIV_ROUND_UP(xfer->len, maxsize);
3095
3096 /* create replacement */
3097 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03003098 if (IS_ERR(srt))
3099 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00003100 xfers = srt->inserted_transfers;
3101
3102 /* now handle each of those newly inserted spi_transfers
3103 * note that the replacements spi_transfers all are preset
3104 * to the same values as *xferp, so tx_buf, rx_buf and len
3105 * are all identical (as well as most others)
3106 * so we just have to fix up len and the pointers.
3107 *
3108 * this also includes support for the depreciated
3109 * spi_message.is_dma_mapped interface
3110 */
3111
3112 /* the first transfer just needs the length modified, so we
3113 * run it outside the loop
3114 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02003115 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00003116
3117 /* all the others need rx_buf/tx_buf also set */
3118 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
3119 /* update rx_buf, tx_buf and dma */
3120 if (xfers[i].rx_buf)
3121 xfers[i].rx_buf += offset;
3122 if (xfers[i].rx_dma)
3123 xfers[i].rx_dma += offset;
3124 if (xfers[i].tx_buf)
3125 xfers[i].tx_buf += offset;
3126 if (xfers[i].tx_dma)
3127 xfers[i].tx_dma += offset;
3128
3129 /* update length */
3130 xfers[i].len = min(maxsize, xfers[i].len - offset);
3131 }
3132
3133 /* we set up xferp to the last entry we have inserted,
3134 * so that we skip those already split transfers
3135 */
3136 *xferp = &xfers[count - 1];
3137
3138 /* increment statistics counters */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003139 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperld9f12122015-12-14 15:20:20 +00003140 transfers_split_maxsize);
3141 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
3142 transfers_split_maxsize);
3143
3144 return 0;
3145}
3146
3147/**
3148 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
3149 * when an individual transfer exceeds a
3150 * certain size
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003151 * @ctlr: the @spi_controller for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09003152 * @msg: the @spi_message to transform
3153 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03003154 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00003155 *
3156 * Return: status of transformation
3157 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003158int spi_split_transfers_maxsize(struct spi_controller *ctlr,
Martin Sperld9f12122015-12-14 15:20:20 +00003159 struct spi_message *msg,
3160 size_t maxsize,
3161 gfp_t gfp)
3162{
3163 struct spi_transfer *xfer;
3164 int ret;
3165
3166 /* iterate over the transfer_list,
3167 * but note that xfer is advanced to the last transfer inserted
3168 * to avoid checking sizes again unnecessarily (also xfer does
3169 * potentiall belong to a different list by the time the
3170 * replacement has happened
3171 */
3172 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3173 if (xfer->len > maxsize) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003174 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3175 maxsize, gfp);
Martin Sperld9f12122015-12-14 15:20:20 +00003176 if (ret)
3177 return ret;
3178 }
3179 }
3180
3181 return 0;
3182}
3183EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08003184
3185/*-------------------------------------------------------------------------*/
3186
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003187/* Core methods for SPI controller protocol drivers. Some of the
David Brownell7d077192009-06-17 16:26:03 -07003188 * other core methods are currently defined as inline functions.
3189 */
3190
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003191static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
3192 u8 bits_per_word)
Stefan Brüns63ab6452015-08-23 16:06:30 +02003193{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003194 if (ctlr->bits_per_word_mask) {
Stefan Brüns63ab6452015-08-23 16:06:30 +02003195 /* Only 32 bits fit in the mask */
3196 if (bits_per_word > 32)
3197 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003198 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
Stefan Brüns63ab6452015-08-23 16:06:30 +02003199 return -EINVAL;
3200 }
3201
3202 return 0;
3203}
3204
David Brownell7d077192009-06-17 16:26:03 -07003205/**
3206 * spi_setup - setup SPI mode and clock rate
3207 * @spi: the device whose settings are being modified
3208 * Context: can sleep, and no requests are queued to the device
3209 *
3210 * SPI protocol drivers may need to update the transfer mode if the
3211 * device doesn't work with its default. They may likewise need
3212 * to update clock rates or word sizes from initial values. This function
3213 * changes those settings, and must be called from a context that can sleep.
3214 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
3215 * effect the next time the device is selected and data is transferred to
3216 * or from it. When this function returns, the spi device is deselected.
3217 *
3218 * Note that this call will fail if the protocol driver specifies an option
3219 * that the underlying controller or its driver does not support. For
3220 * example, not all hardware supports wire transfers using nine bit words,
3221 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003222 *
3223 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07003224 */
3225int spi_setup(struct spi_device *spi)
3226{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003227 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03003228 int status;
David Brownell7d077192009-06-17 16:26:03 -07003229
wangyuhangf477b7f2013-08-11 18:15:17 +08003230 /* check mode to prevent that DUAL and QUAD set at the same time
3231 */
3232 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
3233 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
3234 dev_err(&spi->dev,
3235 "setup: can not select dual and quad at the same time\n");
3236 return -EINVAL;
3237 }
3238 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
3239 */
3240 if ((spi->mode & SPI_3WIRE) && (spi->mode &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00003241 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3242 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
wangyuhangf477b7f2013-08-11 18:15:17 +08003243 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07003244 /* help drivers fail *cleanly* when they need options
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003245 * that aren't supported with their current controller
David Lechnercbaa62e2018-09-12 19:39:18 -05003246 * SPI_CS_WORD has a fallback software implementation,
3247 * so it is ignored here.
David Brownelle7db06b2009-06-17 16:26:04 -07003248 */
David Lechnercbaa62e2018-09-12 19:39:18 -05003249 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
Serge Semind61ad23c2019-04-26 13:30:07 +03003250 /* nothing prevents from working with active-high CS in case if it
3251 * is driven by GPIO.
3252 */
3253 if (gpio_is_valid(spi->cs_gpio))
3254 bad_bits &= ~SPI_CS_HIGH;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003255 ugly_bits = bad_bits &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00003256 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3257 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003258 if (ugly_bits) {
3259 dev_warn(&spi->dev,
3260 "setup: ignoring unsupported mode bits %x\n",
3261 ugly_bits);
3262 spi->mode &= ~ugly_bits;
3263 bad_bits &= ~ugly_bits;
3264 }
David Brownelle7db06b2009-06-17 16:26:04 -07003265 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02003266 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07003267 bad_bits);
3268 return -EINVAL;
3269 }
3270
David Brownell7d077192009-06-17 16:26:03 -07003271 if (!spi->bits_per_word)
3272 spi->bits_per_word = 8;
3273
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003274 status = __spi_validate_bits_per_word(spi->controller,
3275 spi->bits_per_word);
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03003276 if (status)
3277 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02003278
Axel Lin052eb2d2014-02-10 00:08:05 +08003279 if (!spi->max_speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003280 spi->max_speed_hz = spi->controller->max_speed_hz;
Axel Lin052eb2d2014-02-10 00:08:05 +08003281
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003282 if (spi->controller->setup)
3283 status = spi->controller->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07003284
Luhua Xud948e6c2019-10-30 17:03:54 +08003285 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
3286 status = pm_runtime_get_sync(spi->controller->dev.parent);
3287 if (status < 0) {
3288 pm_runtime_put_noidle(spi->controller->dev.parent);
3289 dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3290 status);
3291 return status;
3292 }
Tony Lindgren57a94602019-11-11 11:53:34 -08003293
3294 /*
3295 * We do not want to return positive value from pm_runtime_get,
3296 * there are many instances of devices calling spi_setup() and
3297 * checking for a non-zero return value instead of a negative
3298 * return value.
3299 */
3300 status = 0;
3301
Luhua Xud948e6c2019-10-30 17:03:54 +08003302 spi_set_cs(spi, false);
3303 pm_runtime_mark_last_busy(spi->controller->dev.parent);
3304 pm_runtime_put_autosuspend(spi->controller->dev.parent);
3305 } else {
3306 spi_set_cs(spi, false);
3307 }
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05003308
Douglas Anderson924b5862019-05-15 09:48:12 -07003309 if (spi->rt && !spi->controller->rt) {
3310 spi->controller->rt = true;
3311 spi_set_thread_rt(spi->controller);
3312 }
3313
Jingoo Han5fe5f052013-10-14 10:31:51 +09003314 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 -07003315 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
3316 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
3317 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
3318 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
3319 (spi->mode & SPI_LOOP) ? "loopback, " : "",
3320 spi->bits_per_word, spi->max_speed_hz,
3321 status);
3322
3323 return status;
3324}
3325EXPORT_SYMBOL_GPL(spi_setup);
3326
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003327/**
3328 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3329 * @spi: the device that requires specific CS timing configuration
Alexandru Ardelean81059362019-09-26 13:51:42 +03003330 * @setup: CS setup time specified via @spi_delay
3331 * @hold: CS hold time specified via @spi_delay
3332 * @inactive: CS inactive delay between transfers specified via @spi_delay
3333 *
3334 * Return: zero on success, else a negative error code.
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003335 */
Alexandru Ardelean81059362019-09-26 13:51:42 +03003336int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup,
3337 struct spi_delay *hold, struct spi_delay *inactive)
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003338{
Alexandru Ardelean25093bd2019-09-26 13:51:43 +03003339 size_t len;
3340
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003341 if (spi->controller->set_cs_timing)
Alexandru Ardelean81059362019-09-26 13:51:42 +03003342 return spi->controller->set_cs_timing(spi, setup, hold,
3343 inactive);
Alexandru Ardelean25093bd2019-09-26 13:51:43 +03003344
3345 if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) ||
3346 (hold && hold->unit == SPI_DELAY_UNIT_SCK) ||
3347 (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) {
3348 dev_err(&spi->dev,
3349 "Clock-cycle delays for CS not supported in SW mode\n");
3350 return -ENOTSUPP;
3351 }
3352
3353 len = sizeof(struct spi_delay);
3354
3355 /* copy delays to controller */
3356 if (setup)
3357 memcpy(&spi->controller->cs_setup, setup, len);
3358 else
3359 memset(&spi->controller->cs_setup, 0, len);
3360
3361 if (hold)
3362 memcpy(&spi->controller->cs_hold, hold, len);
3363 else
3364 memset(&spi->controller->cs_hold, 0, len);
3365
3366 if (inactive)
3367 memcpy(&spi->controller->cs_inactive, inactive, len);
3368 else
3369 memset(&spi->controller->cs_inactive, 0, len);
3370
3371 return 0;
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003372}
3373EXPORT_SYMBOL_GPL(spi_set_cs_timing);
3374
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003375static int _spi_xfer_word_delay_update(struct spi_transfer *xfer,
3376 struct spi_device *spi)
3377{
3378 int delay1, delay2;
3379
Alexandru Ardelean3984d392019-09-26 13:51:44 +03003380 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer);
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003381 if (delay1 < 0)
3382 return delay1;
3383
Alexandru Ardelean3984d392019-09-26 13:51:44 +03003384 delay2 = spi_delay_to_ns(&spi->word_delay, xfer);
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003385 if (delay2 < 0)
3386 return delay2;
3387
3388 if (delay1 < delay2)
3389 memcpy(&xfer->word_delay, &spi->word_delay,
3390 sizeof(xfer->word_delay));
3391
3392 return 0;
3393}
3394
Mark Brown90808732013-11-13 23:44:15 +00003395static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003396{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003397 struct spi_controller *ctlr = spi->controller;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303398 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09003399 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003400
Mark Brown24a00132013-07-10 15:05:40 +01003401 if (list_empty(&message->transfers))
3402 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01003403
David Lechnercbaa62e2018-09-12 19:39:18 -05003404 /* If an SPI controller does not support toggling the CS line on each
David Lechner71388b212018-09-18 12:08:48 -05003405 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
3406 * for the CS line, we can emulate the CS-per-word hardware function by
David Lechnercbaa62e2018-09-12 19:39:18 -05003407 * splitting transfers into one-word transfers and ensuring that
3408 * cs_change is set for each transfer.
3409 */
David Lechner71388b212018-09-18 12:08:48 -05003410 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
Linus Walleijf3186dd2019-01-07 16:51:50 +01003411 spi->cs_gpiod ||
David Lechner71388b212018-09-18 12:08:48 -05003412 gpio_is_valid(spi->cs_gpio))) {
David Lechnercbaa62e2018-09-12 19:39:18 -05003413 size_t maxsize;
3414 int ret;
3415
3416 maxsize = (spi->bits_per_word + 7) / 8;
3417
3418 /* spi_split_transfers_maxsize() requires message->spi */
3419 message->spi = spi;
3420
3421 ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
3422 GFP_KERNEL);
3423 if (ret)
3424 return ret;
3425
3426 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3427 /* don't change cs_change on the last entry in the list */
3428 if (list_is_last(&xfer->transfer_list, &message->transfers))
3429 break;
3430 xfer->cs_change = 1;
3431 }
3432 }
3433
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003434 /* Half-duplex links include original MicroWire, and ones with
3435 * only one data pin like SPI_3WIRE (switches direction) or where
3436 * either MOSI or MISO is missing. They can also be caused by
3437 * software limitations.
3438 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003439 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
3440 (spi->mode & SPI_3WIRE)) {
3441 unsigned flags = ctlr->flags;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003442
3443 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3444 if (xfer->rx_buf && xfer->tx_buf)
3445 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003446 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003447 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003448 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003449 return -EINVAL;
3450 }
3451 }
3452
Laxman Dewangane6811d12012-11-09 14:36:45 +05303453 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303454 * Set transfer bits_per_word and max speed as spi device default if
3455 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08003456 * Set transfer tx_nbits and rx_nbits as single transfer default
3457 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Jonas Bonnb7bb3672019-01-30 09:40:04 +01003458 * Ensure transfer word_delay is at least as long as that required by
3459 * device itself.
Laxman Dewangane6811d12012-11-09 14:36:45 +05303460 */
Martin Sperl77e80582015-11-27 12:31:09 +00003461 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303462 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Martin Sperl5d7e2b52019-02-23 08:49:49 +00003463 xfer->effective_speed_hz = 0;
Sourav Poddar078726c2013-07-18 15:31:25 +05303464 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303465 if (!xfer->bits_per_word)
3466 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08003467
3468 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303469 xfer->speed_hz = spi->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08003470
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003471 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
3472 xfer->speed_hz = ctlr->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02003473
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003474 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
Stefan Brüns63ab6452015-08-23 16:06:30 +02003475 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01003476
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003477 /*
3478 * SPI transfer length should be multiple of SPI word size
3479 * where SPI word size should be power-of-two multiple
3480 */
3481 if (xfer->bits_per_word <= 8)
3482 w_size = 1;
3483 else if (xfer->bits_per_word <= 16)
3484 w_size = 2;
3485 else
3486 w_size = 4;
3487
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003488 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09003489 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003490 return -EINVAL;
3491
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003492 if (xfer->speed_hz && ctlr->min_speed_hz &&
3493 xfer->speed_hz < ctlr->min_speed_hz)
Mark Browna2fd4f92013-07-10 14:57:26 +01003494 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08003495
3496 if (xfer->tx_buf && !xfer->tx_nbits)
3497 xfer->tx_nbits = SPI_NBITS_SINGLE;
3498 if (xfer->rx_buf && !xfer->rx_nbits)
3499 xfer->rx_nbits = SPI_NBITS_SINGLE;
3500 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01003501 * 1. check the value matches one of single, dual and quad
3502 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08003503 */
Sourav Poddardb90a442013-08-22 21:20:48 +05303504 if (xfer->tx_buf) {
3505 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
3506 xfer->tx_nbits != SPI_NBITS_DUAL &&
3507 xfer->tx_nbits != SPI_NBITS_QUAD)
3508 return -EINVAL;
3509 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
3510 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3511 return -EINVAL;
3512 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
3513 !(spi->mode & SPI_TX_QUAD))
3514 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303515 }
wangyuhangf477b7f2013-08-11 18:15:17 +08003516 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05303517 if (xfer->rx_buf) {
3518 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
3519 xfer->rx_nbits != SPI_NBITS_DUAL &&
3520 xfer->rx_nbits != SPI_NBITS_QUAD)
3521 return -EINVAL;
3522 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
3523 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3524 return -EINVAL;
3525 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
3526 !(spi->mode & SPI_RX_QUAD))
3527 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303528 }
Jonas Bonnb7bb3672019-01-30 09:40:04 +01003529
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003530 if (_spi_xfer_word_delay_update(xfer, spi))
3531 return -EINVAL;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303532 }
3533
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003534 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00003535
3536 return 0;
3537}
3538
3539static int __spi_async(struct spi_device *spi, struct spi_message *message)
3540{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003541 struct spi_controller *ctlr = spi->controller;
Vladimir Olteanb42faee2019-09-05 04:01:12 +03003542 struct spi_transfer *xfer;
Mark Brown90808732013-11-13 23:44:15 +00003543
Boris Brezillonb5932f52018-04-26 18:18:15 +02003544 /*
3545 * Some controllers do not support doing regular SPI transfers. Return
3546 * ENOTSUPP when this is the case.
3547 */
3548 if (!ctlr->transfer)
3549 return -ENOTSUPP;
3550
Mark Brown90808732013-11-13 23:44:15 +00003551 message->spi = spi;
3552
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003553 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003554 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3555
Mark Brown90808732013-11-13 23:44:15 +00003556 trace_spi_message_submit(message);
3557
Vladimir Olteanb42faee2019-09-05 04:01:12 +03003558 if (!ctlr->ptp_sts_supported) {
3559 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3560 xfer->ptp_sts_word_pre = 0;
3561 ptp_read_system_prets(xfer->ptp_sts);
3562 }
3563 }
3564
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003565 return ctlr->transfer(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003566}
3567
David Brownell568d0692009-09-22 16:46:18 -07003568/**
3569 * spi_async - asynchronous SPI transfer
3570 * @spi: device with which data will be exchanged
3571 * @message: describes the data transfers, including completion callback
3572 * Context: any (irqs may be blocked, etc)
3573 *
3574 * This call may be used in_irq and other contexts which can't sleep,
3575 * as well as from task contexts which can sleep.
3576 *
3577 * The completion callback is invoked in a context which can't sleep.
3578 * Before that invocation, the value of message->status is undefined.
3579 * When the callback is issued, message->status holds either zero (to
3580 * indicate complete success) or a negative error code. After that
3581 * callback returns, the driver which issued the transfer request may
3582 * deallocate the associated memory; it's no longer in use by any SPI
3583 * core or controller driver code.
3584 *
3585 * Note that although all messages to a spi_device are handled in
3586 * FIFO order, messages may go to different devices in other orders.
3587 * Some device might be higher priority, or have various "hard" access
3588 * time requirements, for example.
3589 *
3590 * On detection of any fault during the transfer, processing of
3591 * the entire message is aborted, and the device is deselected.
3592 * Until returning from the associated message completion callback,
3593 * no other spi_message queued to that device will be processed.
3594 * (This rule applies equally to all the synchronous transfer calls,
3595 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003596 *
3597 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07003598 */
3599int spi_async(struct spi_device *spi, struct spi_message *message)
3600{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003601 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003602 int ret;
3603 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07003604
Mark Brown90808732013-11-13 23:44:15 +00003605 ret = __spi_validate(spi, message);
3606 if (ret != 0)
3607 return ret;
3608
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003609 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07003610
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003611 if (ctlr->bus_lock_flag)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003612 ret = -EBUSY;
3613 else
3614 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07003615
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003616 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003617
3618 return ret;
David Brownell568d0692009-09-22 16:46:18 -07003619}
3620EXPORT_SYMBOL_GPL(spi_async);
3621
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003622/**
3623 * spi_async_locked - version of spi_async with exclusive bus usage
3624 * @spi: device with which data will be exchanged
3625 * @message: describes the data transfers, including completion callback
3626 * Context: any (irqs may be blocked, etc)
3627 *
3628 * This call may be used in_irq and other contexts which can't sleep,
3629 * as well as from task contexts which can sleep.
3630 *
3631 * The completion callback is invoked in a context which can't sleep.
3632 * Before that invocation, the value of message->status is undefined.
3633 * When the callback is issued, message->status holds either zero (to
3634 * indicate complete success) or a negative error code. After that
3635 * callback returns, the driver which issued the transfer request may
3636 * deallocate the associated memory; it's no longer in use by any SPI
3637 * core or controller driver code.
3638 *
3639 * Note that although all messages to a spi_device are handled in
3640 * FIFO order, messages may go to different devices in other orders.
3641 * Some device might be higher priority, or have various "hard" access
3642 * time requirements, for example.
3643 *
3644 * On detection of any fault during the transfer, processing of
3645 * the entire message is aborted, and the device is deselected.
3646 * Until returning from the associated message completion callback,
3647 * no other spi_message queued to that device will be processed.
3648 * (This rule applies equally to all the synchronous transfer calls,
3649 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003650 *
3651 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003652 */
3653int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3654{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003655 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003656 int ret;
3657 unsigned long flags;
3658
Mark Brown90808732013-11-13 23:44:15 +00003659 ret = __spi_validate(spi, message);
3660 if (ret != 0)
3661 return ret;
3662
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003663 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003664
3665 ret = __spi_async(spi, message);
3666
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003667 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003668
3669 return ret;
3670
3671}
3672EXPORT_SYMBOL_GPL(spi_async_locked);
3673
David Brownell7d077192009-06-17 16:26:03 -07003674/*-------------------------------------------------------------------------*/
3675
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003676/* Utility methods for SPI protocol drivers, layered on
David Brownell7d077192009-06-17 16:26:03 -07003677 * top of the core. Some other utility methods are defined as
3678 * inline functions.
3679 */
3680
Andrew Morton5d870c82006-01-11 11:23:49 -08003681static void spi_complete(void *arg)
3682{
3683 complete(arg);
3684}
3685
Mark Brownef4d96e2016-07-21 23:53:31 +01003686static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003687{
3688 DECLARE_COMPLETION_ONSTACK(done);
3689 int status;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003690 struct spi_controller *ctlr = spi->controller;
Mark Brown0461a412014-12-09 21:38:05 +00003691 unsigned long flags;
3692
3693 status = __spi_validate(spi, message);
3694 if (status != 0)
3695 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003696
3697 message->complete = spi_complete;
3698 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00003699 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003700
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003701 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003702 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3703
Mark Brown0461a412014-12-09 21:38:05 +00003704 /* If we're not using the legacy transfer method then we will
3705 * try to transfer in the calling context so special case.
3706 * This code would be less tricky if we could remove the
3707 * support for driver implemented message queues.
3708 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003709 if (ctlr->transfer == spi_queued_transfer) {
3710 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003711
3712 trace_spi_message_submit(message);
3713
3714 status = __spi_queued_transfer(spi, message, false);
3715
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003716 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003717 } else {
3718 status = spi_async_locked(spi, message);
3719 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003720
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003721 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00003722 /* Push out the messages in the calling context if we
3723 * can.
3724 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003725 if (ctlr->transfer == spi_queued_transfer) {
3726 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperleca2ebc2015-06-22 13:00:36 +00003727 spi_sync_immediate);
3728 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3729 spi_sync_immediate);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003730 __spi_pump_messages(ctlr, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003731 }
Mark Brown0461a412014-12-09 21:38:05 +00003732
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003733 wait_for_completion(&done);
3734 status = message->status;
3735 }
3736 message->context = NULL;
3737 return status;
3738}
3739
David Brownell8ae12a02006-01-08 13:34:19 -08003740/**
3741 * spi_sync - blocking/synchronous SPI data transfers
3742 * @spi: device with which data will be exchanged
3743 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07003744 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003745 *
3746 * This call may only be used from a context that may sleep. The sleep
3747 * is non-interruptible, and has no timeout. Low-overhead controller
3748 * drivers may DMA directly into and out of the message buffers.
3749 *
3750 * Note that the SPI device's chip select is active during the message,
3751 * and then is normally disabled between messages. Drivers for some
3752 * frequently-used devices may want to minimize costs of selecting a chip,
3753 * by leaving it selected in anticipation that the next message will go
3754 * to the same chip. (That may increase power usage.)
3755 *
David Brownell0c8684612006-01-08 13:34:25 -08003756 * Also, the caller is guaranteeing that the memory associated with the
3757 * message will not be freed before this call returns.
3758 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003759 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003760 */
3761int spi_sync(struct spi_device *spi, struct spi_message *message)
3762{
Mark Brownef4d96e2016-07-21 23:53:31 +01003763 int ret;
3764
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003765 mutex_lock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003766 ret = __spi_sync(spi, message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003767 mutex_unlock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003768
3769 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08003770}
3771EXPORT_SYMBOL_GPL(spi_sync);
3772
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003773/**
3774 * spi_sync_locked - version of spi_sync with exclusive bus usage
3775 * @spi: device with which data will be exchanged
3776 * @message: describes the data transfers
3777 * Context: can sleep
3778 *
3779 * This call may only be used from a context that may sleep. The sleep
3780 * is non-interruptible, and has no timeout. Low-overhead controller
3781 * drivers may DMA directly into and out of the message buffers.
3782 *
3783 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003784 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003785 * be released by a spi_bus_unlock call when the exclusive access is over.
3786 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003787 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003788 */
3789int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3790{
Mark Brownef4d96e2016-07-21 23:53:31 +01003791 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003792}
3793EXPORT_SYMBOL_GPL(spi_sync_locked);
3794
3795/**
3796 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003797 * @ctlr: SPI bus master that should be locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003798 * Context: can sleep
3799 *
3800 * This call may only be used from a context that may sleep. The sleep
3801 * is non-interruptible, and has no timeout.
3802 *
3803 * This call should be used by drivers that require exclusive access to the
3804 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3805 * exclusive access is over. Data transfer must be done by spi_sync_locked
3806 * and spi_async_locked calls when the SPI bus lock is held.
3807 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003808 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003809 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003810int spi_bus_lock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003811{
3812 unsigned long flags;
3813
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003814 mutex_lock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003815
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003816 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3817 ctlr->bus_lock_flag = 1;
3818 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003819
3820 /* mutex remains locked until spi_bus_unlock is called */
3821
3822 return 0;
3823}
3824EXPORT_SYMBOL_GPL(spi_bus_lock);
3825
3826/**
3827 * spi_bus_unlock - release the lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003828 * @ctlr: SPI bus master that was locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003829 * Context: can sleep
3830 *
3831 * This call may only be used from a context that may sleep. The sleep
3832 * is non-interruptible, and has no timeout.
3833 *
3834 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3835 * call.
3836 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003837 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003838 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003839int spi_bus_unlock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003840{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003841 ctlr->bus_lock_flag = 0;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003842
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003843 mutex_unlock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003844
3845 return 0;
3846}
3847EXPORT_SYMBOL_GPL(spi_bus_unlock);
3848
David Brownella9948b62006-04-02 10:37:40 -08003849/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003850#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003851
3852static u8 *buf;
3853
3854/**
3855 * spi_write_then_read - SPI synchronous write followed by read
3856 * @spi: device with which data will be exchanged
3857 * @txbuf: data to be written (need not be dma-safe)
3858 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003859 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3860 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003861 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003862 *
3863 * This performs a half duplex MicroWire style transaction with the
3864 * device, sending txbuf and then reading rxbuf. The return value
3865 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003866 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003867 *
David Brownell0c8684612006-01-08 13:34:25 -08003868 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003869 * portable code should never use this for more than 32 bytes.
3870 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c8684612006-01-08 13:34:25 -08003871 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003872 *
3873 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003874 */
3875int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003876 const void *txbuf, unsigned n_tx,
3877 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003878{
David Brownell068f4072007-12-04 23:45:09 -08003879 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003880
3881 int status;
3882 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003883 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003884 u8 *local_buf;
3885
Mark Brownb3a223e2012-12-02 12:54:25 +09003886 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3887 * copying here, (as a pure convenience thing), but we can
3888 * keep heap costs out of the hot path unless someone else is
3889 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003890 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003891 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003892 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3893 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003894 if (!local_buf)
3895 return -ENOMEM;
3896 } else {
3897 local_buf = buf;
3898 }
David Brownell8ae12a02006-01-08 13:34:19 -08003899
Vitaly Wool8275c642006-01-08 13:34:28 -08003900 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003901 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003902 if (n_tx) {
3903 x[0].len = n_tx;
3904 spi_message_add_tail(&x[0], &message);
3905 }
3906 if (n_rx) {
3907 x[1].len = n_rx;
3908 spi_message_add_tail(&x[1], &message);
3909 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003910
David Brownell8ae12a02006-01-08 13:34:19 -08003911 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003912 x[0].tx_buf = local_buf;
3913 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003914
3915 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003916 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003917 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003918 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003919
David Brownellbdff5492009-04-13 14:39:57 -07003920 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003921 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003922 else
3923 kfree(local_buf);
3924
3925 return status;
3926}
3927EXPORT_SYMBOL_GPL(spi_write_then_read);
3928
3929/*-------------------------------------------------------------------------*/
3930
Marco Felsch5f143af2018-09-25 11:42:29 +02003931#if IS_ENABLED(CONFIG_OF)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003932/* must call put_device() when done with returned spi_device device */
Marco Felsch5f143af2018-09-25 11:42:29 +02003933struct spi_device *of_find_spi_device_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003934{
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +01003935 struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
3936
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003937 return dev ? to_spi_device(dev) : NULL;
3938}
Marco Felsch5f143af2018-09-25 11:42:29 +02003939EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
3940#endif /* IS_ENABLED(CONFIG_OF) */
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003941
Marco Felsch5f143af2018-09-25 11:42:29 +02003942#if IS_ENABLED(CONFIG_OF_DYNAMIC)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003943/* the spi controllers are not using spi_bus, so we find it with another way */
3944static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003945{
3946 struct device *dev;
3947
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +01003948 dev = class_find_device_by_of_node(&spi_master_class, node);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003949 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +01003950 dev = class_find_device_by_of_node(&spi_slave_class, node);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003951 if (!dev)
3952 return NULL;
3953
3954 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003955 return container_of(dev, struct spi_controller, dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003956}
3957
3958static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3959 void *arg)
3960{
3961 struct of_reconfig_data *rd = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003962 struct spi_controller *ctlr;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003963 struct spi_device *spi;
3964
3965 switch (of_reconfig_get_state_change(action, arg)) {
3966 case OF_RECONFIG_CHANGE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003967 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3968 if (ctlr == NULL)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003969 return NOTIFY_OK; /* not for us */
3970
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003971 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003972 put_device(&ctlr->dev);
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003973 return NOTIFY_OK;
3974 }
3975
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003976 spi = of_register_spi_device(ctlr, rd->dn);
3977 put_device(&ctlr->dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003978
3979 if (IS_ERR(spi)) {
Rob Herring25c56c82017-07-18 16:43:31 -05003980 pr_err("%s: failed to create for '%pOF'\n",
3981 __func__, rd->dn);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003982 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003983 return notifier_from_errno(PTR_ERR(spi));
3984 }
3985 break;
3986
3987 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003988 /* already depopulated? */
3989 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3990 return NOTIFY_OK;
3991
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003992 /* find our device by node */
3993 spi = of_find_spi_device_by_node(rd->dn);
3994 if (spi == NULL)
3995 return NOTIFY_OK; /* no? not meant for us */
3996
3997 /* unregister takes one ref away */
3998 spi_unregister_device(spi);
3999
4000 /* and put the reference of the find */
4001 put_device(&spi->dev);
4002 break;
4003 }
4004
4005 return NOTIFY_OK;
4006}
4007
4008static struct notifier_block spi_of_notifier = {
4009 .notifier_call = of_spi_notify,
4010};
4011#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4012extern struct notifier_block spi_of_notifier;
4013#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
4014
Octavian Purdila7f244672016-07-08 19:13:11 +03004015#if IS_ENABLED(CONFIG_ACPI)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004016static int spi_acpi_controller_match(struct device *dev, const void *data)
Octavian Purdila7f244672016-07-08 19:13:11 +03004017{
4018 return ACPI_COMPANION(dev->parent) == data;
4019}
4020
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004021static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
Octavian Purdila7f244672016-07-08 19:13:11 +03004022{
4023 struct device *dev;
4024
4025 dev = class_find_device(&spi_master_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004026 spi_acpi_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02004027 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
4028 dev = class_find_device(&spi_slave_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004029 spi_acpi_controller_match);
Octavian Purdila7f244672016-07-08 19:13:11 +03004030 if (!dev)
4031 return NULL;
4032
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004033 return container_of(dev, struct spi_controller, dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03004034}
4035
4036static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
4037{
4038 struct device *dev;
4039
Suzuki K Poulose00500142019-07-23 23:18:36 +01004040 dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03004041 return dev ? to_spi_device(dev) : NULL;
4042}
4043
4044static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
4045 void *arg)
4046{
4047 struct acpi_device *adev = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004048 struct spi_controller *ctlr;
Octavian Purdila7f244672016-07-08 19:13:11 +03004049 struct spi_device *spi;
4050
4051 switch (value) {
4052 case ACPI_RECONFIG_DEVICE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004053 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
4054 if (!ctlr)
Octavian Purdila7f244672016-07-08 19:13:11 +03004055 break;
4056
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004057 acpi_register_spi_device(ctlr, adev);
4058 put_device(&ctlr->dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03004059 break;
4060 case ACPI_RECONFIG_DEVICE_REMOVE:
4061 if (!acpi_device_enumerated(adev))
4062 break;
4063
4064 spi = acpi_spi_find_device_by_adev(adev);
4065 if (!spi)
4066 break;
4067
4068 spi_unregister_device(spi);
4069 put_device(&spi->dev);
4070 break;
4071 }
4072
4073 return NOTIFY_OK;
4074}
4075
4076static struct notifier_block spi_acpi_notifier = {
4077 .notifier_call = acpi_spi_notify,
4078};
4079#else
4080extern struct notifier_block spi_acpi_notifier;
4081#endif
4082
David Brownell8ae12a02006-01-08 13:34:19 -08004083static int __init spi_init(void)
4084{
David Brownellb8852442006-01-08 13:34:23 -08004085 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08004086
Christoph Lametere94b1762006-12-06 20:33:17 -08004087 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08004088 if (!buf) {
4089 status = -ENOMEM;
4090 goto err0;
4091 }
4092
4093 status = bus_register(&spi_bus_type);
4094 if (status < 0)
4095 goto err1;
4096
4097 status = class_register(&spi_master_class);
4098 if (status < 0)
4099 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004100
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02004101 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
4102 status = class_register(&spi_slave_class);
4103 if (status < 0)
4104 goto err3;
4105 }
4106
Fabio Estevam52677202014-11-26 20:13:57 -02004107 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004108 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03004109 if (IS_ENABLED(CONFIG_ACPI))
4110 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004111
David Brownell8ae12a02006-01-08 13:34:19 -08004112 return 0;
David Brownellb8852442006-01-08 13:34:23 -08004113
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02004114err3:
4115 class_unregister(&spi_master_class);
David Brownellb8852442006-01-08 13:34:23 -08004116err2:
4117 bus_unregister(&spi_bus_type);
4118err1:
4119 kfree(buf);
4120 buf = NULL;
4121err0:
4122 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08004123}
David Brownellb8852442006-01-08 13:34:23 -08004124
David Brownell8ae12a02006-01-08 13:34:19 -08004125/* board_info is normally registered in arch_initcall(),
4126 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08004127 *
4128 * REVISIT only boardinfo really needs static linking. the rest (device and
4129 * driver registration) _could_ be dynamically linked (modular) ... costs
4130 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08004131 */
David Brownell673c0c02008-10-15 22:02:46 -07004132postcore_initcall(spi_init);