blob: 13f447a67d67a404cc72c77204560dc2038d4dff [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>
Suniel Mahesh9b61e302017-08-03 10:05:57 +053039
Boris Brezillon46336962018-04-22 20:35:14 +020040#include "internals.h"
41
Suniel Mahesh9b61e302017-08-03 10:05:57 +053042static DEFINE_IDR(spi_master_idr);
Mark Brown56ec1972013-10-07 19:33:53 +010043
David Brownell8ae12a02006-01-08 13:34:19 -080044static void spidev_release(struct device *dev)
45{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080046 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080047
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020048 /* spi controllers may cleanup for released devices */
49 if (spi->controller->cleanup)
50 spi->controller->cleanup(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080051
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020052 spi_controller_put(spi->controller);
Trent Piepho50395632018-09-20 19:18:32 +000053 kfree(spi->driver_override);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000054 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080055}
56
57static ssize_t
58modalias_show(struct device *dev, struct device_attribute *a, char *buf)
59{
60 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080061 int len;
62
63 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
64 if (len != -ENODEV)
65 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080066
Grant Likelyd8e328b2012-05-20 00:08:13 -060067 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080068}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070069static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080070
Trent Piepho50395632018-09-20 19:18:32 +000071static ssize_t driver_override_store(struct device *dev,
72 struct device_attribute *a,
73 const char *buf, size_t count)
74{
75 struct spi_device *spi = to_spi_device(dev);
76 const char *end = memchr(buf, '\n', count);
77 const size_t len = end ? end - buf : count;
78 const char *driver_override, *old;
79
80 /* We need to keep extra room for a newline when displaying value */
81 if (len >= (PAGE_SIZE - 1))
82 return -EINVAL;
83
84 driver_override = kstrndup(buf, len, GFP_KERNEL);
85 if (!driver_override)
86 return -ENOMEM;
87
88 device_lock(dev);
89 old = spi->driver_override;
90 if (len) {
91 spi->driver_override = driver_override;
92 } else {
93 /* Emptry string, disable driver override */
94 spi->driver_override = NULL;
95 kfree(driver_override);
96 }
97 device_unlock(dev);
98 kfree(old);
99
100 return count;
101}
102
103static ssize_t driver_override_show(struct device *dev,
104 struct device_attribute *a, char *buf)
105{
106 const struct spi_device *spi = to_spi_device(dev);
107 ssize_t len;
108
109 device_lock(dev);
110 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : "");
111 device_unlock(dev);
112 return len;
113}
114static DEVICE_ATTR_RW(driver_override);
115
Martin Sperleca2ebc2015-06-22 13:00:36 +0000116#define SPI_STATISTICS_ATTRS(field, file) \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200117static ssize_t spi_controller_##field##_show(struct device *dev, \
118 struct device_attribute *attr, \
119 char *buf) \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000120{ \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200121 struct spi_controller *ctlr = container_of(dev, \
122 struct spi_controller, dev); \
123 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000124} \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200125static struct device_attribute dev_attr_spi_controller_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +0200126 .attr = { .name = file, .mode = 0444 }, \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200127 .show = spi_controller_##field##_show, \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000128}; \
129static ssize_t spi_device_##field##_show(struct device *dev, \
130 struct device_attribute *attr, \
131 char *buf) \
132{ \
Geliang Tangd1eba932015-12-23 00:18:41 +0800133 struct spi_device *spi = to_spi_device(dev); \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000134 return spi_statistics_##field##_show(&spi->statistics, buf); \
135} \
136static struct device_attribute dev_attr_spi_device_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +0200137 .attr = { .name = file, .mode = 0444 }, \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000138 .show = spi_device_##field##_show, \
139}
140
141#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
142static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
143 char *buf) \
144{ \
145 unsigned long flags; \
146 ssize_t len; \
147 spin_lock_irqsave(&stat->lock, flags); \
148 len = sprintf(buf, format_string, stat->field); \
149 spin_unlock_irqrestore(&stat->lock, flags); \
150 return len; \
151} \
152SPI_STATISTICS_ATTRS(name, file)
153
154#define SPI_STATISTICS_SHOW(field, format_string) \
155 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
156 field, format_string)
157
158SPI_STATISTICS_SHOW(messages, "%lu");
159SPI_STATISTICS_SHOW(transfers, "%lu");
160SPI_STATISTICS_SHOW(errors, "%lu");
161SPI_STATISTICS_SHOW(timedout, "%lu");
162
163SPI_STATISTICS_SHOW(spi_sync, "%lu");
164SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
165SPI_STATISTICS_SHOW(spi_async, "%lu");
166
167SPI_STATISTICS_SHOW(bytes, "%llu");
168SPI_STATISTICS_SHOW(bytes_rx, "%llu");
169SPI_STATISTICS_SHOW(bytes_tx, "%llu");
170
Martin Sperl6b7bc062015-06-22 13:02:04 +0000171#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
172 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
173 "transfer_bytes_histo_" number, \
174 transfer_bytes_histo[index], "%lu")
175SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
176SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
177SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
178SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
179SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
180SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
181SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
182SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
183SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
184SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
185SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
186SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
187SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
188SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
189SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
190SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
191SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
192
Martin Sperld9f12122015-12-14 15:20:20 +0000193SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
194
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700195static struct attribute *spi_dev_attrs[] = {
196 &dev_attr_modalias.attr,
Trent Piepho50395632018-09-20 19:18:32 +0000197 &dev_attr_driver_override.attr,
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700198 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800199};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000200
201static const struct attribute_group spi_dev_group = {
202 .attrs = spi_dev_attrs,
203};
204
205static struct attribute *spi_device_statistics_attrs[] = {
206 &dev_attr_spi_device_messages.attr,
207 &dev_attr_spi_device_transfers.attr,
208 &dev_attr_spi_device_errors.attr,
209 &dev_attr_spi_device_timedout.attr,
210 &dev_attr_spi_device_spi_sync.attr,
211 &dev_attr_spi_device_spi_sync_immediate.attr,
212 &dev_attr_spi_device_spi_async.attr,
213 &dev_attr_spi_device_bytes.attr,
214 &dev_attr_spi_device_bytes_rx.attr,
215 &dev_attr_spi_device_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000216 &dev_attr_spi_device_transfer_bytes_histo0.attr,
217 &dev_attr_spi_device_transfer_bytes_histo1.attr,
218 &dev_attr_spi_device_transfer_bytes_histo2.attr,
219 &dev_attr_spi_device_transfer_bytes_histo3.attr,
220 &dev_attr_spi_device_transfer_bytes_histo4.attr,
221 &dev_attr_spi_device_transfer_bytes_histo5.attr,
222 &dev_attr_spi_device_transfer_bytes_histo6.attr,
223 &dev_attr_spi_device_transfer_bytes_histo7.attr,
224 &dev_attr_spi_device_transfer_bytes_histo8.attr,
225 &dev_attr_spi_device_transfer_bytes_histo9.attr,
226 &dev_attr_spi_device_transfer_bytes_histo10.attr,
227 &dev_attr_spi_device_transfer_bytes_histo11.attr,
228 &dev_attr_spi_device_transfer_bytes_histo12.attr,
229 &dev_attr_spi_device_transfer_bytes_histo13.attr,
230 &dev_attr_spi_device_transfer_bytes_histo14.attr,
231 &dev_attr_spi_device_transfer_bytes_histo15.attr,
232 &dev_attr_spi_device_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000233 &dev_attr_spi_device_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000234 NULL,
235};
236
237static const struct attribute_group spi_device_statistics_group = {
238 .name = "statistics",
239 .attrs = spi_device_statistics_attrs,
240};
241
242static const struct attribute_group *spi_dev_groups[] = {
243 &spi_dev_group,
244 &spi_device_statistics_group,
245 NULL,
246};
247
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200248static struct attribute *spi_controller_statistics_attrs[] = {
249 &dev_attr_spi_controller_messages.attr,
250 &dev_attr_spi_controller_transfers.attr,
251 &dev_attr_spi_controller_errors.attr,
252 &dev_attr_spi_controller_timedout.attr,
253 &dev_attr_spi_controller_spi_sync.attr,
254 &dev_attr_spi_controller_spi_sync_immediate.attr,
255 &dev_attr_spi_controller_spi_async.attr,
256 &dev_attr_spi_controller_bytes.attr,
257 &dev_attr_spi_controller_bytes_rx.attr,
258 &dev_attr_spi_controller_bytes_tx.attr,
259 &dev_attr_spi_controller_transfer_bytes_histo0.attr,
260 &dev_attr_spi_controller_transfer_bytes_histo1.attr,
261 &dev_attr_spi_controller_transfer_bytes_histo2.attr,
262 &dev_attr_spi_controller_transfer_bytes_histo3.attr,
263 &dev_attr_spi_controller_transfer_bytes_histo4.attr,
264 &dev_attr_spi_controller_transfer_bytes_histo5.attr,
265 &dev_attr_spi_controller_transfer_bytes_histo6.attr,
266 &dev_attr_spi_controller_transfer_bytes_histo7.attr,
267 &dev_attr_spi_controller_transfer_bytes_histo8.attr,
268 &dev_attr_spi_controller_transfer_bytes_histo9.attr,
269 &dev_attr_spi_controller_transfer_bytes_histo10.attr,
270 &dev_attr_spi_controller_transfer_bytes_histo11.attr,
271 &dev_attr_spi_controller_transfer_bytes_histo12.attr,
272 &dev_attr_spi_controller_transfer_bytes_histo13.attr,
273 &dev_attr_spi_controller_transfer_bytes_histo14.attr,
274 &dev_attr_spi_controller_transfer_bytes_histo15.attr,
275 &dev_attr_spi_controller_transfer_bytes_histo16.attr,
276 &dev_attr_spi_controller_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000277 NULL,
278};
279
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200280static const struct attribute_group spi_controller_statistics_group = {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000281 .name = "statistics",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200282 .attrs = spi_controller_statistics_attrs,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000283};
284
285static const struct attribute_group *spi_master_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200286 &spi_controller_statistics_group,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000287 NULL,
288};
289
290void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
291 struct spi_transfer *xfer,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200292 struct spi_controller *ctlr)
Martin Sperleca2ebc2015-06-22 13:00:36 +0000293{
294 unsigned long flags;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000295 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
296
297 if (l2len < 0)
298 l2len = 0;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000299
300 spin_lock_irqsave(&stats->lock, flags);
301
302 stats->transfers++;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000303 stats->transfer_bytes_histo[l2len]++;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000304
305 stats->bytes += xfer->len;
306 if ((xfer->tx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200307 (xfer->tx_buf != ctlr->dummy_tx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000308 stats->bytes_tx += xfer->len;
309 if ((xfer->rx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200310 (xfer->rx_buf != ctlr->dummy_rx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000311 stats->bytes_rx += xfer->len;
312
313 spin_unlock_irqrestore(&stats->lock, flags);
314}
315EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800316
317/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
318 * and the sysfs version makes coldplug work too.
319 */
320
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700321static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
322 const struct spi_device *sdev)
323{
324 while (id->name[0]) {
325 if (!strcmp(sdev->modalias, id->name))
326 return id;
327 id++;
328 }
329 return NULL;
330}
331
332const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
333{
334 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
335
336 return spi_match_id(sdrv->id_table, sdev);
337}
338EXPORT_SYMBOL_GPL(spi_get_device_id);
339
David Brownell8ae12a02006-01-08 13:34:19 -0800340static int spi_match_device(struct device *dev, struct device_driver *drv)
341{
342 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700343 const struct spi_driver *sdrv = to_spi_driver(drv);
344
Trent Piepho50395632018-09-20 19:18:32 +0000345 /* Check override first, and if set, only use the named driver */
346 if (spi->driver_override)
347 return strcmp(spi->driver_override, drv->name) == 0;
348
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600349 /* Attempt an OF style match */
350 if (of_driver_match_device(dev, drv))
351 return 1;
352
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100353 /* Then try ACPI */
354 if (acpi_driver_match_device(dev, drv))
355 return 1;
356
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700357 if (sdrv->id_table)
358 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800359
Kay Sievers35f74fc2009-01-06 10:44:37 -0800360 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800361}
362
Kay Sievers7eff2e72007-08-14 15:15:12 +0200363static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800364{
365 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800366 int rc;
367
368 rc = acpi_device_uevent_modalias(dev, env);
369 if (rc != -ENODEV)
370 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800371
Andy Shevchenko28566702017-07-26 16:14:00 +0300372 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800373}
374
David Brownell8ae12a02006-01-08 13:34:19 -0800375struct bus_type spi_bus_type = {
376 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700377 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800378 .match = spi_match_device,
379 .uevent = spi_uevent,
David Brownell8ae12a02006-01-08 13:34:19 -0800380};
381EXPORT_SYMBOL_GPL(spi_bus_type);
382
David Brownellb8852442006-01-08 13:34:23 -0800383
384static int spi_drv_probe(struct device *dev)
385{
386 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Jon Hunter44af7922015-10-09 15:45:55 +0100387 struct spi_device *spi = to_spi_device(dev);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300388 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800389
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200390 ret = of_clk_set_defaults(dev->of_node, false);
391 if (ret)
392 return ret;
393
Jon Hunter44af7922015-10-09 15:45:55 +0100394 if (dev->of_node) {
395 spi->irq = of_irq_get(dev->of_node, 0);
396 if (spi->irq == -EPROBE_DEFER)
397 return -EPROBE_DEFER;
398 if (spi->irq < 0)
399 spi->irq = 0;
400 }
401
Ulf Hansson676e7c22014-09-19 20:27:41 +0200402 ret = dev_pm_domain_attach(dev, true);
Ulf Hansson71f277a2018-04-26 10:53:10 +0200403 if (ret)
404 return ret;
405
406 ret = sdrv->probe(spi);
407 if (ret)
408 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300409
410 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800411}
412
413static int spi_drv_remove(struct device *dev)
414{
415 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300416 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800417
Jean Delvareaec35f42014-02-13 15:28:41 +0100418 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200419 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300420
421 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800422}
423
424static void spi_drv_shutdown(struct device *dev)
425{
426 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
427
428 sdrv->shutdown(to_spi_device(dev));
429}
430
David Brownell33e34dc2007-05-08 00:32:21 -0700431/**
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500432 * __spi_register_driver - register a SPI driver
Thierry Reding88c93212015-11-10 13:03:04 +0100433 * @owner: owner module of the driver to register
David Brownell33e34dc2007-05-08 00:32:21 -0700434 * @sdrv: the driver to register
435 * Context: can sleep
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200436 *
437 * Return: zero on success, else a negative error code.
David Brownell33e34dc2007-05-08 00:32:21 -0700438 */
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500439int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
David Brownellb8852442006-01-08 13:34:23 -0800440{
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500441 sdrv->driver.owner = owner;
David Brownellb8852442006-01-08 13:34:23 -0800442 sdrv->driver.bus = &spi_bus_type;
443 if (sdrv->probe)
444 sdrv->driver.probe = spi_drv_probe;
445 if (sdrv->remove)
446 sdrv->driver.remove = spi_drv_remove;
447 if (sdrv->shutdown)
448 sdrv->driver.shutdown = spi_drv_shutdown;
449 return driver_register(&sdrv->driver);
450}
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500451EXPORT_SYMBOL_GPL(__spi_register_driver);
David Brownellb8852442006-01-08 13:34:23 -0800452
David Brownell8ae12a02006-01-08 13:34:19 -0800453/*-------------------------------------------------------------------------*/
454
455/* SPI devices should normally not be created by SPI device drivers; that
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200456 * would make them board-specific. Similarly with SPI controller drivers.
David Brownell8ae12a02006-01-08 13:34:19 -0800457 * Device registration normally goes into like arch/.../mach.../board-YYY.c
458 * with other readonly (flashable) information about mainboard devices.
459 */
460
461struct boardinfo {
462 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800463 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800464};
465
466static LIST_HEAD(board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200467static LIST_HEAD(spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800468
469/*
470 * Used to protect add/del opertion for board_info list and
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200471 * spi_controller list, and their matching process
Suniel Mahesh9a9a0472017-08-17 18:18:22 +0530472 * also used to protect object of type struct idr
Feng Tang2b9603a2010-08-02 15:52:15 +0800473 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700474static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800475
Grant Likelydc87c982008-05-15 16:50:22 -0600476/**
477 * spi_alloc_device - Allocate a new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200478 * @ctlr: Controller to which device is connected
Grant Likelydc87c982008-05-15 16:50:22 -0600479 * Context: can sleep
480 *
481 * Allows a driver to allocate and initialize a spi_device without
482 * registering it immediately. This allows a driver to directly
483 * fill the spi_device with device parameters before calling
484 * spi_add_device() on it.
485 *
486 * Caller is responsible to call spi_add_device() on the returned
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200487 * spi_device structure to add it to the SPI controller. If the caller
Grant Likelydc87c982008-05-15 16:50:22 -0600488 * needs to discard the spi_device without adding it, then it should
489 * call spi_dev_put() on it.
490 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200491 * Return: a pointer to the new device, or NULL.
Grant Likelydc87c982008-05-15 16:50:22 -0600492 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200493struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
Grant Likelydc87c982008-05-15 16:50:22 -0600494{
495 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600496
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200497 if (!spi_controller_get(ctlr))
Grant Likelydc87c982008-05-15 16:50:22 -0600498 return NULL;
499
Jingoo Han5fe5f052013-10-14 10:31:51 +0900500 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600501 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200502 spi_controller_put(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600503 return NULL;
504 }
505
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200506 spi->master = spi->controller = ctlr;
507 spi->dev.parent = &ctlr->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600508 spi->dev.bus = &spi_bus_type;
509 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100510 spi->cs_gpio = -ENOENT;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000511
512 spin_lock_init(&spi->statistics.lock);
513
Grant Likelydc87c982008-05-15 16:50:22 -0600514 device_initialize(&spi->dev);
515 return spi;
516}
517EXPORT_SYMBOL_GPL(spi_alloc_device);
518
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200519static void spi_dev_set_name(struct spi_device *spi)
520{
521 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
522
523 if (adev) {
524 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
525 return;
526 }
527
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200528 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200529 spi->chip_select);
530}
531
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200532static int spi_dev_check(struct device *dev, void *data)
533{
534 struct spi_device *spi = to_spi_device(dev);
535 struct spi_device *new_spi = data;
536
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200537 if (spi->controller == new_spi->controller &&
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200538 spi->chip_select == new_spi->chip_select)
539 return -EBUSY;
540 return 0;
541}
542
Grant Likelydc87c982008-05-15 16:50:22 -0600543/**
544 * spi_add_device - Add spi_device allocated with spi_alloc_device
545 * @spi: spi_device to register
546 *
547 * Companion function to spi_alloc_device. Devices allocated with
548 * spi_alloc_device can be added onto the spi bus with this function.
549 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200550 * Return: 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600551 */
552int spi_add_device(struct spi_device *spi)
553{
David Brownelle48880e2008-08-15 00:40:44 -0700554 static DEFINE_MUTEX(spi_add_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200555 struct spi_controller *ctlr = spi->controller;
556 struct device *dev = ctlr->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600557 int status;
558
559 /* Chipselects are numbered 0..max; validate. */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200560 if (spi->chip_select >= ctlr->num_chipselect) {
561 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
562 ctlr->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600563 return -EINVAL;
564 }
565
566 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200567 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700568
569 /* We need to make sure there's no other device with this
570 * chipselect **BEFORE** we call setup(), else we'll trash
571 * its configuration. Lock against concurrent add() calls.
572 */
573 mutex_lock(&spi_add_lock);
574
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200575 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
576 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700577 dev_err(dev, "chipselect %d already in use\n",
578 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700579 goto done;
580 }
581
Linus Walleijf3186dd2019-01-07 16:51:50 +0100582 /* Descriptors take precedence */
583 if (ctlr->cs_gpiods)
584 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
585 else if (ctlr->cs_gpios)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200586 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100587
David Brownelle48880e2008-08-15 00:40:44 -0700588 /* Drivers may modify this initial i/o setup, but will
589 * normally rely on the device being setup. Devices
590 * using SPI_CS_HIGH can't coexist well otherwise...
591 */
David Brownell7d077192009-06-17 16:26:03 -0700592 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600593 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200594 dev_err(dev, "can't setup %s, status %d\n",
595 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700596 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600597 }
598
David Brownelle48880e2008-08-15 00:40:44 -0700599 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600600 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700601 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200602 dev_err(dev, "can't add %s, status %d\n",
603 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700604 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800605 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600606
David Brownelle48880e2008-08-15 00:40:44 -0700607done:
608 mutex_unlock(&spi_add_lock);
609 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600610}
611EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800612
David Brownell33e34dc2007-05-08 00:32:21 -0700613/**
614 * spi_new_device - instantiate one new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200615 * @ctlr: Controller to which device is connected
David Brownell33e34dc2007-05-08 00:32:21 -0700616 * @chip: Describes the SPI device
617 * Context: can sleep
618 *
619 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800620 * after board init creates the hard-wired devices. Some development
621 * platforms may not be able to use spi_register_board_info though, and
622 * this is exported so that for example a USB or parport based adapter
623 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700624 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200625 * Return: the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800626 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200627struct spi_device *spi_new_device(struct spi_controller *ctlr,
Adrian Bunke9d5a462007-03-26 21:32:23 -0800628 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800629{
630 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800631 int status;
632
David Brownell082c8cb2007-07-31 00:39:45 -0700633 /* NOTE: caller did any chip->bus_num checks necessary.
634 *
635 * Also, unless we change the return value convention to use
636 * error-or-pointer (not NULL-or-pointer), troubleshootability
637 * suggests syslogged diagnostics are best here (ugh).
638 */
639
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200640 proxy = spi_alloc_device(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600641 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800642 return NULL;
643
Grant Likely102eb972008-07-23 21:29:55 -0700644 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
645
David Brownell8ae12a02006-01-08 13:34:19 -0800646 proxy->chip_select = chip->chip_select;
647 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700648 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800649 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700650 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800651 proxy->dev.platform_data = (void *) chip->platform_data;
652 proxy->controller_data = chip->controller_data;
653 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800654
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800655 if (chip->properties) {
656 status = device_add_properties(&proxy->dev, chip->properties);
657 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200658 dev_err(&ctlr->dev,
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800659 "failed to add properties to '%s': %d\n",
660 chip->modalias, status);
661 goto err_dev_put;
662 }
David Brownell8ae12a02006-01-08 13:34:19 -0800663 }
664
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800665 status = spi_add_device(proxy);
666 if (status < 0)
667 goto err_remove_props;
668
David Brownell8ae12a02006-01-08 13:34:19 -0800669 return proxy;
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800670
671err_remove_props:
672 if (chip->properties)
673 device_remove_properties(&proxy->dev);
674err_dev_put:
675 spi_dev_put(proxy);
676 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800677}
678EXPORT_SYMBOL_GPL(spi_new_device);
679
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100680/**
681 * spi_unregister_device - unregister a single SPI device
682 * @spi: spi_device to unregister
683 *
684 * Start making the passed SPI device vanish. Normally this would be handled
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200685 * by spi_unregister_controller().
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100686 */
687void spi_unregister_device(struct spi_device *spi)
688{
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100689 if (!spi)
690 return;
691
Johan Hovold83241472017-01-30 17:47:05 +0100692 if (spi->dev.of_node) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100693 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
Johan Hovold83241472017-01-30 17:47:05 +0100694 of_node_put(spi->dev.of_node);
695 }
Octavian Purdila7f244672016-07-08 19:13:11 +0300696 if (ACPI_COMPANION(&spi->dev))
697 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100698 device_unregister(&spi->dev);
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100699}
700EXPORT_SYMBOL_GPL(spi_unregister_device);
701
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200702static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
703 struct spi_board_info *bi)
Feng Tang2b9603a2010-08-02 15:52:15 +0800704{
705 struct spi_device *dev;
706
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200707 if (ctlr->bus_num != bi->bus_num)
Feng Tang2b9603a2010-08-02 15:52:15 +0800708 return;
709
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200710 dev = spi_new_device(ctlr, bi);
Feng Tang2b9603a2010-08-02 15:52:15 +0800711 if (!dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200712 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
Feng Tang2b9603a2010-08-02 15:52:15 +0800713 bi->modalias);
714}
715
David Brownell33e34dc2007-05-08 00:32:21 -0700716/**
717 * spi_register_board_info - register SPI devices for a given board
718 * @info: array of chip descriptors
719 * @n: how many descriptors are provided
720 * Context: can sleep
721 *
David Brownell8ae12a02006-01-08 13:34:19 -0800722 * Board-specific early init code calls this (probably during arch_initcall)
723 * with segments of the SPI device table. Any device nodes are created later,
724 * after the relevant parent SPI controller (bus_num) is defined. We keep
725 * this table of devices forever, so that reloading a controller driver will
726 * not make Linux forget about these hard-wired devices.
727 *
728 * Other code can also call this, e.g. a particular add-on board might provide
729 * SPI devices through its expansion connector, so code initializing that board
730 * would naturally declare its SPI devices.
731 *
732 * The board info passed can safely be __initdata ... but be careful of
733 * any embedded pointers (platform_data, etc), they're copied as-is.
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800734 * Device properties are deep-copied though.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200735 *
736 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -0800737 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000738int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800739{
Feng Tang2b9603a2010-08-02 15:52:15 +0800740 struct boardinfo *bi;
741 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800742
Xiubo Lic7908a32014-09-24 14:30:29 +0800743 if (!n)
Dmitry Torokhovf974cf52017-02-28 14:25:19 -0800744 return 0;
Xiubo Lic7908a32014-09-24 14:30:29 +0800745
Markus Elfringf9bdb7f2017-01-13 12:28:04 +0100746 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800747 if (!bi)
748 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800749
Feng Tang2b9603a2010-08-02 15:52:15 +0800750 for (i = 0; i < n; i++, bi++, info++) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200751 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -0800752
Feng Tang2b9603a2010-08-02 15:52:15 +0800753 memcpy(&bi->board_info, info, sizeof(*info));
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800754 if (info->properties) {
755 bi->board_info.properties =
756 property_entries_dup(info->properties);
757 if (IS_ERR(bi->board_info.properties))
758 return PTR_ERR(bi->board_info.properties);
759 }
760
Feng Tang2b9603a2010-08-02 15:52:15 +0800761 mutex_lock(&board_lock);
762 list_add_tail(&bi->list, &board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200763 list_for_each_entry(ctlr, &spi_controller_list, list)
764 spi_match_controller_to_boardinfo(ctlr,
765 &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +0800766 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800767 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800768
769 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800770}
771
772/*-------------------------------------------------------------------------*/
773
Mark Brownb1589352013-10-05 11:50:40 +0100774static void spi_set_cs(struct spi_device *spi, bool enable)
775{
776 if (spi->mode & SPI_CS_HIGH)
777 enable = !enable;
778
Linus Walleijf3186dd2019-01-07 16:51:50 +0100779 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
780 /*
781 * Honour the SPI_NO_CS flag and invert the enable line, as
782 * active low is default for SPI. Execution paths that handle
783 * polarity inversion in gpiolib (such as device tree) will
784 * enforce active high using the SPI_CS_HIGH resulting in a
785 * double inversion through the code above.
786 */
787 if (!(spi->mode & SPI_NO_CS)) {
788 if (spi->cs_gpiod)
789 gpiod_set_value(spi->cs_gpiod, !enable);
790 else
791 gpio_set_value(spi->cs_gpio, !enable);
792 }
Thor Thayer8eee6b92016-10-10 09:25:24 -0500793 /* Some SPI masters need both GPIO CS & slave_select */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200794 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
795 spi->controller->set_cs)
796 spi->controller->set_cs(spi, !enable);
797 } else if (spi->controller->set_cs) {
798 spi->controller->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500799 }
Mark Brownb1589352013-10-05 11:50:40 +0100800}
801
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200802#ifdef CONFIG_HAS_DMA
Boris Brezillon46336962018-04-22 20:35:14 +0200803int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
804 struct sg_table *sgt, void *buf, size_t len,
805 enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000806{
807 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200808 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530809#ifdef CONFIG_HIGHMEM
810 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
811 (unsigned long)buf < (PKMAP_BASE +
812 (LAST_PKMAP * PAGE_SIZE)));
813#else
814 const bool kmap_buf = false;
815#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500816 int desc_len;
817 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000818 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600819 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000820 void *sg_buf;
821 size_t min;
822 int i, ret;
823
Vignesh Rb1b81532016-08-17 15:22:36 +0530824 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200825 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500826 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530827 } else if (virt_addr_valid(buf)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200828 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500829 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530830 } else {
831 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500832 }
833
Mark Brown6ad45a22014-02-02 13:47:47 +0000834 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
835 if (ret != 0)
836 return ret;
837
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600838 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000839 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000840
Vignesh Rb1b81532016-08-17 15:22:36 +0530841 if (vmalloced_buf || kmap_buf) {
Maxime Chevallierce993192018-03-02 15:55:09 +0100842 /*
843 * Next scatterlist entry size is the minimum between
844 * the desc_len and the remaining buffer length that
845 * fits in a page.
846 */
847 min = min_t(size_t, desc_len,
848 min_t(size_t, len,
849 PAGE_SIZE - offset_in_page(buf)));
Vignesh Rb1b81532016-08-17 15:22:36 +0530850 if (vmalloced_buf)
851 vm_page = vmalloc_to_page(buf);
852 else
853 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000854 if (!vm_page) {
855 sg_free_table(sgt);
856 return -ENOMEM;
857 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600858 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000859 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000860 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500861 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000862 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600863 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000864 }
865
Mark Brown6ad45a22014-02-02 13:47:47 +0000866 buf += min;
867 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600868 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000869 }
870
871 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200872 if (!ret)
873 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000874 if (ret < 0) {
875 sg_free_table(sgt);
876 return ret;
877 }
878
879 sgt->nents = ret;
880
881 return 0;
882}
883
Boris Brezillon46336962018-04-22 20:35:14 +0200884void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
885 struct sg_table *sgt, enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000886{
887 if (sgt->orig_nents) {
888 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
889 sg_free_table(sgt);
890 }
891}
892
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200893static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000894{
Mark Brown99adef32014-01-16 12:22:43 +0000895 struct device *tx_dev, *rx_dev;
896 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000897 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000898
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200899 if (!ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000900 return 0;
901
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200902 if (ctlr->dma_tx)
903 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800904 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200905 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800906
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200907 if (ctlr->dma_rx)
908 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800909 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200910 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000911
912 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200913 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000914 continue;
915
916 if (xfer->tx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200917 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000918 (void *)xfer->tx_buf, xfer->len,
919 DMA_TO_DEVICE);
920 if (ret != 0)
921 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000922 }
923
924 if (xfer->rx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200925 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000926 xfer->rx_buf, xfer->len,
927 DMA_FROM_DEVICE);
928 if (ret != 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200929 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000930 DMA_TO_DEVICE);
931 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000932 }
933 }
934 }
935
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200936 ctlr->cur_msg_mapped = true;
Mark Brown99adef32014-01-16 12:22:43 +0000937
938 return 0;
939}
940
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200941static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000942{
943 struct spi_transfer *xfer;
944 struct device *tx_dev, *rx_dev;
945
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200946 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000947 return 0;
948
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200949 if (ctlr->dma_tx)
950 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800951 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200952 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800953
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200954 if (ctlr->dma_rx)
955 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800956 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200957 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000958
959 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200960 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000961 continue;
962
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200963 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
964 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000965 }
966
967 return 0;
968}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200969#else /* !CONFIG_HAS_DMA */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200970static inline int __spi_map_msg(struct spi_controller *ctlr,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200971 struct spi_message *msg)
972{
973 return 0;
974}
975
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200976static inline int __spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000977 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200978{
979 return 0;
980}
981#endif /* !CONFIG_HAS_DMA */
982
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200983static inline int spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000984 struct spi_message *msg)
985{
986 struct spi_transfer *xfer;
987
988 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
989 /*
990 * Restore the original value of tx_buf or rx_buf if they are
991 * NULL.
992 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200993 if (xfer->tx_buf == ctlr->dummy_tx)
Martin Sperl4b786452015-05-25 10:13:10 +0000994 xfer->tx_buf = NULL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200995 if (xfer->rx_buf == ctlr->dummy_rx)
Martin Sperl4b786452015-05-25 10:13:10 +0000996 xfer->rx_buf = NULL;
997 }
998
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200999 return __spi_unmap_msg(ctlr, msg);
Martin Sperl4b786452015-05-25 10:13:10 +00001000}
1001
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001002static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001003{
1004 struct spi_transfer *xfer;
1005 void *tmp;
1006 unsigned int max_tx, max_rx;
1007
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001008 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001009 max_tx = 0;
1010 max_rx = 0;
1011
1012 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001013 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001014 !xfer->tx_buf)
1015 max_tx = max(xfer->len, max_tx);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001016 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001017 !xfer->rx_buf)
1018 max_rx = max(xfer->len, max_rx);
1019 }
1020
1021 if (max_tx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001022 tmp = krealloc(ctlr->dummy_tx, max_tx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001023 GFP_KERNEL | GFP_DMA);
1024 if (!tmp)
1025 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001026 ctlr->dummy_tx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001027 memset(tmp, 0, max_tx);
1028 }
1029
1030 if (max_rx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001031 tmp = krealloc(ctlr->dummy_rx, max_rx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001032 GFP_KERNEL | GFP_DMA);
1033 if (!tmp)
1034 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001035 ctlr->dummy_rx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001036 }
1037
1038 if (max_tx || max_rx) {
1039 list_for_each_entry(xfer, &msg->transfers,
1040 transfer_list) {
1041 if (!xfer->tx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001042 xfer->tx_buf = ctlr->dummy_tx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001043 if (!xfer->rx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001044 xfer->rx_buf = ctlr->dummy_rx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001045 }
1046 }
1047 }
1048
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001049 return __spi_map_msg(ctlr, msg);
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001050}
Mark Brown99adef32014-01-16 12:22:43 +00001051
Lubomir Rintel810923f2018-11-13 11:22:24 +01001052static int spi_transfer_wait(struct spi_controller *ctlr,
1053 struct spi_message *msg,
1054 struct spi_transfer *xfer)
1055{
1056 struct spi_statistics *statm = &ctlr->statistics;
1057 struct spi_statistics *stats = &msg->spi->statistics;
1058 unsigned long long ms = 1;
1059
1060 if (spi_controller_is_slave(ctlr)) {
1061 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1062 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1063 return -EINTR;
1064 }
1065 } else {
1066 ms = 8LL * 1000LL * xfer->len;
1067 do_div(ms, xfer->speed_hz);
1068 ms += ms + 200; /* some tolerance */
1069
1070 if (ms > UINT_MAX)
1071 ms = UINT_MAX;
1072
1073 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1074 msecs_to_jiffies(ms));
1075
1076 if (ms == 0) {
1077 SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1078 SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1079 dev_err(&msg->spi->dev,
1080 "SPI transfer timed out\n");
1081 return -ETIMEDOUT;
1082 }
1083 }
1084
1085 return 0;
1086}
1087
Mark Brownb1589352013-10-05 11:50:40 +01001088/*
1089 * spi_transfer_one_message - Default implementation of transfer_one_message()
1090 *
1091 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -07001092 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +01001093 * standard handling of delays and chip select management.
1094 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001095static int spi_transfer_one_message(struct spi_controller *ctlr,
Mark Brownb1589352013-10-05 11:50:40 +01001096 struct spi_message *msg)
1097{
1098 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +01001099 bool keep_cs = false;
1100 int ret = 0;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001101 struct spi_statistics *statm = &ctlr->statistics;
Martin Sperleca2ebc2015-06-22 13:00:36 +00001102 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +01001103
1104 spi_set_cs(msg->spi, true);
1105
Martin Sperleca2ebc2015-06-22 13:00:36 +00001106 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1107 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1108
Mark Brownb1589352013-10-05 11:50:40 +01001109 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1110 trace_spi_transfer_start(msg, xfer);
1111
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001112 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1113 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
Martin Sperleca2ebc2015-06-22 13:00:36 +00001114
Mark Brown38ec10f2014-08-16 16:27:41 +01001115 if (xfer->tx_buf || xfer->rx_buf) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001116 reinit_completion(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001117
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001118 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
Mark Brown38ec10f2014-08-16 16:27:41 +01001119 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001120 SPI_STATISTICS_INCREMENT_FIELD(statm,
1121 errors);
1122 SPI_STATISTICS_INCREMENT_FIELD(stats,
1123 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001124 dev_err(&msg->spi->dev,
1125 "SPI transfer failed: %d\n", ret);
1126 goto out;
1127 }
Mark Brownb1589352013-10-05 11:50:40 +01001128
Mark Brownd57e7962018-11-15 16:08:32 -08001129 if (ret > 0) {
1130 ret = spi_transfer_wait(ctlr, msg, xfer);
1131 if (ret < 0)
1132 msg->status = ret;
1133 }
Mark Brown38ec10f2014-08-16 16:27:41 +01001134 } else {
1135 if (xfer->len)
1136 dev_err(&msg->spi->dev,
1137 "Bufferless transfer has length %u\n",
1138 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001139 }
Mark Brownb1589352013-10-05 11:50:40 +01001140
1141 trace_spi_transfer_stop(msg, xfer);
1142
1143 if (msg->status != -EINPROGRESS)
1144 goto out;
1145
Daniel Kurtz8244bd32016-10-07 18:55:47 +08001146 if (xfer->delay_usecs) {
1147 u16 us = xfer->delay_usecs;
1148
1149 if (us <= 10)
1150 udelay(us);
1151 else
1152 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1153 }
Mark Brownb1589352013-10-05 11:50:40 +01001154
1155 if (xfer->cs_change) {
1156 if (list_is_last(&xfer->transfer_list,
1157 &msg->transfers)) {
1158 keep_cs = true;
1159 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001160 spi_set_cs(msg->spi, false);
1161 udelay(10);
1162 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001163 }
1164 }
1165
1166 msg->actual_length += xfer->len;
1167 }
1168
1169out:
1170 if (ret != 0 || !keep_cs)
1171 spi_set_cs(msg->spi, false);
1172
1173 if (msg->status == -EINPROGRESS)
1174 msg->status = ret;
1175
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001176 if (msg->status && ctlr->handle_err)
1177 ctlr->handle_err(ctlr, msg);
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001178
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001179 spi_res_release(ctlr, msg);
Martin Sperld780c372015-12-14 15:20:18 +00001180
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001181 spi_finalize_current_message(ctlr);
Mark Brownb1589352013-10-05 11:50:40 +01001182
1183 return ret;
1184}
1185
1186/**
1187 * spi_finalize_current_transfer - report completion of a transfer
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001188 * @ctlr: the controller reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001189 *
1190 * Called by SPI drivers using the core transfer_one_message()
1191 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001192 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001193 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001194void spi_finalize_current_transfer(struct spi_controller *ctlr)
Mark Brownb1589352013-10-05 11:50:40 +01001195{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001196 complete(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001197}
1198EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1199
Linus Walleijffbbdd212012-02-22 10:05:38 +01001200/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001201 * __spi_pump_messages - function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001202 * @ctlr: controller to process queue for
Mark Brownfc9e0f72014-12-10 13:46:33 +00001203 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001204 *
1205 * This function checks if there is any spi message in the queue that
1206 * needs processing and if so call out to the driver to initialize hardware
1207 * and transfer each message.
1208 *
Mark Brown0461a412014-12-09 21:38:05 +00001209 * Note that it is called both from the kthread itself and also from
1210 * inside spi_sync(); the queue extraction handling at the top of the
1211 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001212 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001213static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001214{
Linus Walleijffbbdd212012-02-22 10:05:38 +01001215 unsigned long flags;
1216 bool was_busy = false;
1217 int ret;
1218
Mark Brown983aee52014-12-09 19:46:56 +00001219 /* Lock queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001220 spin_lock_irqsave(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001221
1222 /* Make sure we are not already running a message */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001223 if (ctlr->cur_msg) {
1224 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001225 return;
1226 }
1227
Mark Brownf0125f12019-01-23 17:29:53 +00001228 /* If another context is idling the device then defer */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001229 if (ctlr->idling) {
1230 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1231 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001232 return;
1233 }
1234
Mark Brown983aee52014-12-09 19:46:56 +00001235 /* Check if the queue is idle */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001236 if (list_empty(&ctlr->queue) || !ctlr->running) {
1237 if (!ctlr->busy) {
1238 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -07001239 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001240 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001241
Mark Brownf0125f12019-01-23 17:29:53 +00001242 /* Only do teardown in the thread */
1243 if (!in_kthread) {
1244 kthread_queue_work(&ctlr->kworker,
1245 &ctlr->pump_messages);
1246 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1247 return;
1248 }
1249
1250 ctlr->busy = false;
1251 ctlr->idling = true;
1252 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1253
1254 kfree(ctlr->dummy_rx);
1255 ctlr->dummy_rx = NULL;
1256 kfree(ctlr->dummy_tx);
1257 ctlr->dummy_tx = NULL;
1258 if (ctlr->unprepare_transfer_hardware &&
1259 ctlr->unprepare_transfer_hardware(ctlr))
1260 dev_err(&ctlr->dev,
1261 "failed to unprepare transfer hardware\n");
1262 if (ctlr->auto_runtime_pm) {
1263 pm_runtime_mark_last_busy(ctlr->dev.parent);
1264 pm_runtime_put_autosuspend(ctlr->dev.parent);
1265 }
1266 trace_spi_controller_idle(ctlr);
1267
1268 spin_lock_irqsave(&ctlr->queue_lock, flags);
1269 ctlr->idling = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001270 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001271 return;
1272 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001273
Linus Walleijffbbdd212012-02-22 10:05:38 +01001274 /* Extract head of queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001275 ctlr->cur_msg =
1276 list_first_entry(&ctlr->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001277
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001278 list_del_init(&ctlr->cur_msg->queue);
1279 if (ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001280 was_busy = true;
1281 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001282 ctlr->busy = true;
1283 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001284
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001285 mutex_lock(&ctlr->io_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001286
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001287 if (!was_busy && ctlr->auto_runtime_pm) {
1288 ret = pm_runtime_get_sync(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001289 if (ret < 0) {
Tony Lindgren7e48e232018-05-18 10:30:07 -07001290 pm_runtime_put_noidle(ctlr->dev.parent);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001291 dev_err(&ctlr->dev, "Failed to power device: %d\n",
Mark Brown49834de2013-07-28 14:47:02 +01001292 ret);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001293 mutex_unlock(&ctlr->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001294 return;
1295 }
1296 }
1297
Mark Brown56ec1972013-10-07 19:33:53 +01001298 if (!was_busy)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001299 trace_spi_controller_busy(ctlr);
Mark Brown56ec1972013-10-07 19:33:53 +01001300
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001301 if (!was_busy && ctlr->prepare_transfer_hardware) {
1302 ret = ctlr->prepare_transfer_hardware(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001303 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001304 dev_err(&ctlr->dev,
Linus Walleijffbbdd212012-02-22 10:05:38 +01001305 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001306
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001307 if (ctlr->auto_runtime_pm)
1308 pm_runtime_put(ctlr->dev.parent);
1309 mutex_unlock(&ctlr->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001310 return;
1311 }
1312 }
1313
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001314 trace_spi_message_start(ctlr->cur_msg);
Mark Brown56ec1972013-10-07 19:33:53 +01001315
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001316 if (ctlr->prepare_message) {
1317 ret = ctlr->prepare_message(ctlr, ctlr->cur_msg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001318 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001319 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1320 ret);
1321 ctlr->cur_msg->status = ret;
1322 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001323 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001324 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001325 ctlr->cur_msg_prepared = true;
Mark Brown2841a5f2013-10-05 00:23:12 +01001326 }
1327
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001328 ret = spi_map_msg(ctlr, ctlr->cur_msg);
Mark Brown99adef32014-01-16 12:22:43 +00001329 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001330 ctlr->cur_msg->status = ret;
1331 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001332 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001333 }
1334
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001335 ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001336 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001337 dev_err(&ctlr->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001338 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001339 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001340 }
Jon Hunter49023d22016-03-08 12:28:20 +00001341
1342out:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001343 mutex_unlock(&ctlr->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001344
1345 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001346 if (!ret)
1347 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001348}
1349
Mark Brownfc9e0f72014-12-10 13:46:33 +00001350/**
1351 * spi_pump_messages - kthread work function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001352 * @work: pointer to kthread work struct contained in the controller struct
Mark Brownfc9e0f72014-12-10 13:46:33 +00001353 */
1354static void spi_pump_messages(struct kthread_work *work)
1355{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001356 struct spi_controller *ctlr =
1357 container_of(work, struct spi_controller, pump_messages);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001358
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001359 __spi_pump_messages(ctlr, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001360}
1361
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001362static int spi_init_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001363{
1364 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1365
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001366 ctlr->running = false;
1367 ctlr->busy = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001368
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001369 kthread_init_worker(&ctlr->kworker);
1370 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1371 "%s", dev_name(&ctlr->dev));
1372 if (IS_ERR(ctlr->kworker_task)) {
1373 dev_err(&ctlr->dev, "failed to create message pump task\n");
1374 return PTR_ERR(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001375 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001376 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
Mark Brownf0125f12019-01-23 17:29:53 +00001377
Linus Walleijffbbdd212012-02-22 10:05:38 +01001378 /*
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001379 * Controller config will indicate if this controller should run the
Linus Walleijffbbdd212012-02-22 10:05:38 +01001380 * message pump with high (realtime) priority to reduce the transfer
1381 * latency on the bus by minimising the delay between a transfer
1382 * request and the scheduling of the message pump thread. Without this
1383 * setting the message pump thread will remain at default priority.
1384 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001385 if (ctlr->rt) {
1386 dev_info(&ctlr->dev,
Linus Walleijffbbdd212012-02-22 10:05:38 +01001387 "will run message pump with realtime priority\n");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001388 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001389 }
1390
1391 return 0;
1392}
1393
1394/**
1395 * spi_get_next_queued_message() - called by driver to check for queued
1396 * messages
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001397 * @ctlr: the controller to check for queued messages
Linus Walleijffbbdd212012-02-22 10:05:38 +01001398 *
1399 * If there are more messages in the queue, the next message is returned from
1400 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001401 *
1402 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001403 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001404struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001405{
1406 struct spi_message *next;
1407 unsigned long flags;
1408
1409 /* get a pointer to the next message, if any */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001410 spin_lock_irqsave(&ctlr->queue_lock, flags);
1411 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
Axel Lin1cfd97f2014-01-02 15:16:40 +08001412 queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001413 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001414
1415 return next;
1416}
1417EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1418
1419/**
1420 * spi_finalize_current_message() - the current message is complete
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001421 * @ctlr: the controller to return the message to
Linus Walleijffbbdd212012-02-22 10:05:38 +01001422 *
1423 * Called by the driver to notify the core that the message in the front of the
1424 * queue is complete and can be removed from the queue.
1425 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001426void spi_finalize_current_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001427{
1428 struct spi_message *mesg;
1429 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001430 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001431
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001432 spin_lock_irqsave(&ctlr->queue_lock, flags);
1433 mesg = ctlr->cur_msg;
1434 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001435
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001436 spi_unmap_msg(ctlr, mesg);
Mark Brown99adef32014-01-16 12:22:43 +00001437
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001438 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1439 ret = ctlr->unprepare_message(ctlr, mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001440 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001441 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1442 ret);
Mark Brown2841a5f2013-10-05 00:23:12 +01001443 }
1444 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001445
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001446 spin_lock_irqsave(&ctlr->queue_lock, flags);
1447 ctlr->cur_msg = NULL;
1448 ctlr->cur_msg_prepared = false;
Mark Brownf0125f12019-01-23 17:29:53 +00001449 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001450 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001451
1452 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001453
Linus Walleijffbbdd212012-02-22 10:05:38 +01001454 mesg->state = NULL;
1455 if (mesg->complete)
1456 mesg->complete(mesg->context);
1457}
1458EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1459
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001460static int spi_start_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001461{
1462 unsigned long flags;
1463
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001464 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001465
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001466 if (ctlr->running || ctlr->busy) {
1467 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001468 return -EBUSY;
1469 }
1470
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001471 ctlr->running = true;
1472 ctlr->cur_msg = NULL;
1473 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001474
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001475 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001476
1477 return 0;
1478}
1479
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001480static int spi_stop_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001481{
1482 unsigned long flags;
1483 unsigned limit = 500;
1484 int ret = 0;
1485
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001486 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001487
1488 /*
1489 * This is a bit lame, but is optimized for the common execution path.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001490 * A wait_queue on the ctlr->busy could be used, but then the common
Linus Walleijffbbdd212012-02-22 10:05:38 +01001491 * execution path (pump_messages) would be required to call wake_up or
1492 * friends on every SPI message. Do this instead.
1493 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001494 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1495 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001496 usleep_range(10000, 11000);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001497 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001498 }
1499
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001500 if (!list_empty(&ctlr->queue) || ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001501 ret = -EBUSY;
1502 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001503 ctlr->running = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001504
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001505 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001506
1507 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001508 dev_warn(&ctlr->dev, "could not stop message queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001509 return ret;
1510 }
1511 return ret;
1512}
1513
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001514static int spi_destroy_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001515{
1516 int ret;
1517
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001518 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001519
1520 /*
Petr Mladek39891442016-10-11 13:55:20 -07001521 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001522 * If the reason that stop_queue timed out is that the work will never
1523 * finish, then it does no good to call flush/stop thread, so
1524 * return anyway.
1525 */
1526 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001527 dev_err(&ctlr->dev, "problem destroying queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001528 return ret;
1529 }
1530
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001531 kthread_flush_worker(&ctlr->kworker);
1532 kthread_stop(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001533
1534 return 0;
1535}
1536
Mark Brown0461a412014-12-09 21:38:05 +00001537static int __spi_queued_transfer(struct spi_device *spi,
1538 struct spi_message *msg,
1539 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001540{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001541 struct spi_controller *ctlr = spi->controller;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001542 unsigned long flags;
1543
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001544 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001545
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001546 if (!ctlr->running) {
1547 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001548 return -ESHUTDOWN;
1549 }
1550 msg->actual_length = 0;
1551 msg->status = -EINPROGRESS;
1552
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001553 list_add_tail(&msg->queue, &ctlr->queue);
Mark Brownf0125f12019-01-23 17:29:53 +00001554 if (!ctlr->busy && need_pump)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001555 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001556
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001557 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001558 return 0;
1559}
1560
Mark Brown0461a412014-12-09 21:38:05 +00001561/**
1562 * spi_queued_transfer - transfer function for queued transfers
1563 * @spi: spi device which is requesting transfer
1564 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001565 *
1566 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001567 */
1568static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1569{
1570 return __spi_queued_transfer(spi, msg, true);
1571}
1572
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001573static int spi_controller_initialize_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001574{
1575 int ret;
1576
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001577 ctlr->transfer = spi_queued_transfer;
1578 if (!ctlr->transfer_one_message)
1579 ctlr->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001580
1581 /* Initialize and start queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001582 ret = spi_init_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001583 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001584 dev_err(&ctlr->dev, "problem initializing queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001585 goto err_init_queue;
1586 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001587 ctlr->queued = true;
1588 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001589 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001590 dev_err(&ctlr->dev, "problem starting queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001591 goto err_start_queue;
1592 }
1593
1594 return 0;
1595
1596err_start_queue:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001597 spi_destroy_queue(ctlr);
Mark Brownc3676d52014-05-01 10:47:52 -07001598err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001599 return ret;
1600}
1601
Boris Brezillon988f2592018-04-22 20:35:15 +02001602/**
1603 * spi_flush_queue - Send all pending messages in the queue from the callers'
1604 * context
1605 * @ctlr: controller to process queue for
1606 *
1607 * This should be used when one wants to ensure all pending messages have been
1608 * sent before doing something. Is used by the spi-mem code to make sure SPI
1609 * memory operations do not preempt regular SPI transfers that have been queued
1610 * before the spi-mem operation.
1611 */
1612void spi_flush_queue(struct spi_controller *ctlr)
1613{
1614 if (ctlr->transfer == spi_queued_transfer)
1615 __spi_pump_messages(ctlr, false);
1616}
1617
Linus Walleijffbbdd212012-02-22 10:05:38 +01001618/*-------------------------------------------------------------------------*/
1619
Andreas Larsson7cb94362012-12-04 15:09:38 +01001620#if defined(CONFIG_OF)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001621static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001622 struct device_node *nc)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001623{
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001624 u32 value;
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001625 int rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001626
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001627 /* Mode (clock phase/polarity/etc.) */
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001628 if (of_property_read_bool(nc, "spi-cpha"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001629 spi->mode |= SPI_CPHA;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001630 if (of_property_read_bool(nc, "spi-cpol"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001631 spi->mode |= SPI_CPOL;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001632 if (of_property_read_bool(nc, "spi-3wire"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001633 spi->mode |= SPI_3WIRE;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001634 if (of_property_read_bool(nc, "spi-lsb-first"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001635 spi->mode |= SPI_LSB_FIRST;
1636
Linus Walleijf3186dd2019-01-07 16:51:50 +01001637 /*
1638 * For descriptors associated with the device, polarity inversion is
1639 * handled in the gpiolib, so all chip selects are "active high" in
1640 * the logical sense, the gpiolib will invert the line if need be.
1641 */
1642 if (ctlr->use_gpio_descriptors)
1643 spi->mode |= SPI_CS_HIGH;
1644 else if (of_property_read_bool(nc, "spi-cs-high"))
1645 spi->mode |= SPI_CS_HIGH;
1646
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001647 /* Device DUAL/QUAD mode */
1648 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1649 switch (value) {
1650 case 1:
1651 break;
1652 case 2:
1653 spi->mode |= SPI_TX_DUAL;
1654 break;
1655 case 4:
1656 spi->mode |= SPI_TX_QUAD;
1657 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001658 case 8:
1659 spi->mode |= SPI_TX_OCTAL;
1660 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001661 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001662 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001663 "spi-tx-bus-width %d not supported\n",
1664 value);
1665 break;
1666 }
1667 }
1668
1669 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1670 switch (value) {
1671 case 1:
1672 break;
1673 case 2:
1674 spi->mode |= SPI_RX_DUAL;
1675 break;
1676 case 4:
1677 spi->mode |= SPI_RX_QUAD;
1678 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001679 case 8:
1680 spi->mode |= SPI_RX_OCTAL;
1681 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001682 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001683 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001684 "spi-rx-bus-width %d not supported\n",
1685 value);
1686 break;
1687 }
1688 }
1689
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001690 if (spi_controller_is_slave(ctlr)) {
Rob Herring194276b2018-12-05 13:50:41 -06001691 if (!of_node_name_eq(nc, "slave")) {
Rob Herring25c56c82017-07-18 16:43:31 -05001692 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
1693 nc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001694 return -EINVAL;
1695 }
1696 return 0;
1697 }
1698
1699 /* Device address */
1700 rc = of_property_read_u32(nc, "reg", &value);
1701 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05001702 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
1703 nc, rc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001704 return rc;
1705 }
1706 spi->chip_select = value;
1707
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001708 /* Device speed */
1709 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1710 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001711 dev_err(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05001712 "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001713 return rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001714 }
1715 spi->max_speed_hz = value;
1716
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001717 return 0;
1718}
1719
1720static struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001721of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001722{
1723 struct spi_device *spi;
1724 int rc;
1725
1726 /* Alloc an spi_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001727 spi = spi_alloc_device(ctlr);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001728 if (!spi) {
Rob Herring25c56c82017-07-18 16:43:31 -05001729 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001730 rc = -ENOMEM;
1731 goto err_out;
1732 }
1733
1734 /* Select device driver */
1735 rc = of_modalias_node(nc, spi->modalias,
1736 sizeof(spi->modalias));
1737 if (rc < 0) {
Rob Herring25c56c82017-07-18 16:43:31 -05001738 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001739 goto err_out;
1740 }
1741
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001742 rc = of_spi_parse_dt(ctlr, spi, nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001743 if (rc)
1744 goto err_out;
1745
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001746 /* Store a pointer to the node in the device structure */
1747 of_node_get(nc);
1748 spi->dev.of_node = nc;
1749
1750 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001751 rc = spi_add_device(spi);
1752 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05001753 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
Johan Hovold83241472017-01-30 17:47:05 +01001754 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001755 }
1756
1757 return spi;
1758
Johan Hovold83241472017-01-30 17:47:05 +01001759err_of_node_put:
1760 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001761err_out:
1762 spi_dev_put(spi);
1763 return ERR_PTR(rc);
1764}
1765
Grant Likelyd57a4282012-04-07 14:16:53 -06001766/**
1767 * of_register_spi_devices() - Register child devices onto the SPI bus
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001768 * @ctlr: Pointer to spi_controller device
Grant Likelyd57a4282012-04-07 14:16:53 -06001769 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001770 * Registers an spi_device for each child node of controller node which
1771 * represents a valid SPI slave.
Grant Likelyd57a4282012-04-07 14:16:53 -06001772 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001773static void of_register_spi_devices(struct spi_controller *ctlr)
Grant Likelyd57a4282012-04-07 14:16:53 -06001774{
1775 struct spi_device *spi;
1776 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001777
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001778 if (!ctlr->dev.of_node)
Grant Likelyd57a4282012-04-07 14:16:53 -06001779 return;
1780
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001781 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01001782 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1783 continue;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001784 spi = of_register_spi_device(ctlr, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001785 if (IS_ERR(spi)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001786 dev_warn(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05001787 "Failed to create SPI device for %pOF\n", nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001788 of_node_clear_flag(nc, OF_POPULATED);
1789 }
Grant Likelyd57a4282012-04-07 14:16:53 -06001790 }
1791}
1792#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001793static void of_register_spi_devices(struct spi_controller *ctlr) { }
Grant Likelyd57a4282012-04-07 14:16:53 -06001794#endif
1795
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001796#ifdef CONFIG_ACPI
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001797static void acpi_spi_parse_apple_properties(struct spi_device *spi)
1798{
1799 struct acpi_device *dev = ACPI_COMPANION(&spi->dev);
1800 const union acpi_object *obj;
1801
1802 if (!x86_apple_machine)
1803 return;
1804
1805 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
1806 && obj->buffer.length >= 4)
1807 spi->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
1808
1809 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
1810 && obj->buffer.length == 8)
1811 spi->bits_per_word = *(u64 *)obj->buffer.pointer;
1812
1813 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
1814 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
1815 spi->mode |= SPI_LSB_FIRST;
1816
1817 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
1818 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1819 spi->mode |= SPI_CPOL;
1820
1821 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
1822 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1823 spi->mode |= SPI_CPHA;
1824}
1825
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001826static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1827{
1828 struct spi_device *spi = data;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001829 struct spi_controller *ctlr = spi->controller;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001830
1831 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1832 struct acpi_resource_spi_serialbus *sb;
1833
1834 sb = &ares->data.spi_serial_bus;
1835 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Mika Westerberga0a90712016-02-08 17:14:28 +02001836 /*
1837 * ACPI DeviceSelection numbering is handled by the
1838 * host controller driver in Windows and can vary
1839 * from driver to driver. In Linux we always expect
1840 * 0 .. max - 1 so we need to ask the driver to
1841 * translate between the two schemes.
1842 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001843 if (ctlr->fw_translate_cs) {
1844 int cs = ctlr->fw_translate_cs(ctlr,
Mika Westerberga0a90712016-02-08 17:14:28 +02001845 sb->device_selection);
1846 if (cs < 0)
1847 return cs;
1848 spi->chip_select = cs;
1849 } else {
1850 spi->chip_select = sb->device_selection;
1851 }
1852
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001853 spi->max_speed_hz = sb->connection_speed;
1854
1855 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1856 spi->mode |= SPI_CPHA;
1857 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1858 spi->mode |= SPI_CPOL;
1859 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1860 spi->mode |= SPI_CS_HIGH;
1861 }
1862 } else if (spi->irq < 0) {
1863 struct resource r;
1864
1865 if (acpi_dev_resource_interrupt(ares, 0, &r))
1866 spi->irq = r.start;
1867 }
1868
1869 /* Always tell the ACPI core to skip this resource */
1870 return 1;
1871}
1872
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001873static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
Octavian Purdila7f244672016-07-08 19:13:11 +03001874 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001875{
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001876 struct list_head resource_list;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001877 struct spi_device *spi;
1878 int ret;
1879
Octavian Purdila7f244672016-07-08 19:13:11 +03001880 if (acpi_bus_get_status(adev) || !adev->status.present ||
1881 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001882 return AE_OK;
1883
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001884 spi = spi_alloc_device(ctlr);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001885 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001886 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001887 dev_name(&adev->dev));
1888 return AE_NO_MEMORY;
1889 }
1890
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001891 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001892 spi->irq = -1;
1893
1894 INIT_LIST_HEAD(&resource_list);
1895 ret = acpi_dev_get_resources(adev, &resource_list,
1896 acpi_spi_add_resource, spi);
1897 acpi_dev_free_resource_list(&resource_list);
1898
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001899 acpi_spi_parse_apple_properties(spi);
1900
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001901 if (ret < 0 || !spi->max_speed_hz) {
1902 spi_dev_put(spi);
1903 return AE_OK;
1904 }
1905
Dan O'Donovan0c6543f2017-02-05 16:30:14 +00001906 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
1907 sizeof(spi->modalias));
1908
Christophe RICARD33ada672015-12-23 23:25:35 +01001909 if (spi->irq < 0)
1910 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1911
Octavian Purdila7f244672016-07-08 19:13:11 +03001912 acpi_device_set_enumerated(adev);
1913
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001914 adev->power.flags.ignore_parent = true;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001915 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001916 adev->power.flags.ignore_parent = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001917 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001918 dev_name(&adev->dev));
1919 spi_dev_put(spi);
1920 }
1921
1922 return AE_OK;
1923}
1924
Octavian Purdila7f244672016-07-08 19:13:11 +03001925static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1926 void *data, void **return_value)
1927{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001928 struct spi_controller *ctlr = data;
Octavian Purdila7f244672016-07-08 19:13:11 +03001929 struct acpi_device *adev;
1930
1931 if (acpi_bus_get_device(handle, &adev))
1932 return AE_OK;
1933
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001934 return acpi_register_spi_device(ctlr, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03001935}
1936
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001937static void acpi_register_spi_devices(struct spi_controller *ctlr)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001938{
1939 acpi_status status;
1940 acpi_handle handle;
1941
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001942 handle = ACPI_HANDLE(ctlr->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001943 if (!handle)
1944 return;
1945
1946 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001947 acpi_spi_add_device, NULL, ctlr, NULL);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001948 if (ACPI_FAILURE(status))
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001949 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001950}
1951#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001952static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001953#endif /* CONFIG_ACPI */
1954
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001955static void spi_controller_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001956{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001957 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08001958
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001959 ctlr = container_of(dev, struct spi_controller, dev);
1960 kfree(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08001961}
1962
1963static struct class spi_master_class = {
1964 .name = "spi_master",
1965 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001966 .dev_release = spi_controller_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00001967 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08001968};
1969
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001970#ifdef CONFIG_SPI_SLAVE
1971/**
1972 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
1973 * controller
1974 * @spi: device used for the current transfer
1975 */
1976int spi_slave_abort(struct spi_device *spi)
1977{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001978 struct spi_controller *ctlr = spi->controller;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001979
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001980 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
1981 return ctlr->slave_abort(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001982
1983 return -ENOTSUPP;
1984}
1985EXPORT_SYMBOL_GPL(spi_slave_abort);
1986
1987static int match_true(struct device *dev, void *data)
1988{
1989 return 1;
1990}
1991
1992static ssize_t spi_slave_show(struct device *dev,
1993 struct device_attribute *attr, char *buf)
1994{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001995 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1996 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001997 struct device *child;
1998
1999 child = device_find_child(&ctlr->dev, NULL, match_true);
2000 return sprintf(buf, "%s\n",
2001 child ? to_spi_device(child)->modalias : NULL);
2002}
2003
2004static ssize_t spi_slave_store(struct device *dev,
2005 struct device_attribute *attr, const char *buf,
2006 size_t count)
2007{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002008 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2009 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002010 struct spi_device *spi;
2011 struct device *child;
2012 char name[32];
2013 int rc;
2014
2015 rc = sscanf(buf, "%31s", name);
2016 if (rc != 1 || !name[0])
2017 return -EINVAL;
2018
2019 child = device_find_child(&ctlr->dev, NULL, match_true);
2020 if (child) {
2021 /* Remove registered slave */
2022 device_unregister(child);
2023 put_device(child);
2024 }
2025
2026 if (strcmp(name, "(null)")) {
2027 /* Register new slave */
2028 spi = spi_alloc_device(ctlr);
2029 if (!spi)
2030 return -ENOMEM;
2031
2032 strlcpy(spi->modalias, name, sizeof(spi->modalias));
2033
2034 rc = spi_add_device(spi);
2035 if (rc) {
2036 spi_dev_put(spi);
2037 return rc;
2038 }
2039 }
2040
2041 return count;
2042}
2043
2044static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store);
2045
2046static struct attribute *spi_slave_attrs[] = {
2047 &dev_attr_slave.attr,
2048 NULL,
2049};
2050
2051static const struct attribute_group spi_slave_group = {
2052 .attrs = spi_slave_attrs,
2053};
2054
2055static const struct attribute_group *spi_slave_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002056 &spi_controller_statistics_group,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002057 &spi_slave_group,
2058 NULL,
2059};
2060
2061static struct class spi_slave_class = {
2062 .name = "spi_slave",
2063 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002064 .dev_release = spi_controller_release,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002065 .dev_groups = spi_slave_groups,
2066};
2067#else
2068extern struct class spi_slave_class; /* dummy */
2069#endif
David Brownell8ae12a02006-01-08 13:34:19 -08002070
2071/**
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002072 * __spi_alloc_controller - allocate an SPI master or slave controller
David Brownell8ae12a02006-01-08 13:34:19 -08002073 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07002074 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07002075 * memory is in the driver_data field of the returned device,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002076 * accessible with spi_controller_get_devdata().
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002077 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2078 * slave (true) controller
David Brownell33e34dc2007-05-08 00:32:21 -07002079 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002080 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002081 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002082 * only ones directly touching chip registers. It's how they allocate
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002083 * an spi_controller structure, prior to calling spi_register_controller().
David Brownell8ae12a02006-01-08 13:34:19 -08002084 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002085 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08002086 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002087 * The caller is responsible for assigning the bus number and initializing the
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002088 * controller's methods before calling spi_register_controller(); and (after
2089 * errors adding the device) calling spi_controller_put() to prevent a memory
2090 * leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002091 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002092 * Return: the SPI controller structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002093 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002094struct spi_controller *__spi_alloc_controller(struct device *dev,
2095 unsigned int size, bool slave)
David Brownell8ae12a02006-01-08 13:34:19 -08002096{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002097 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002098
David Brownell0c8684612006-01-08 13:34:25 -08002099 if (!dev)
2100 return NULL;
2101
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002102 ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL);
2103 if (!ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002104 return NULL;
2105
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002106 device_initialize(&ctlr->dev);
2107 ctlr->bus_num = -1;
2108 ctlr->num_chipselect = 1;
2109 ctlr->slave = slave;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002110 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002111 ctlr->dev.class = &spi_slave_class;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002112 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002113 ctlr->dev.class = &spi_master_class;
2114 ctlr->dev.parent = dev;
2115 pm_suspend_ignore_children(&ctlr->dev, true);
2116 spi_controller_set_devdata(ctlr, &ctlr[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08002117
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002118 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002119}
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002120EXPORT_SYMBOL_GPL(__spi_alloc_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002121
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002122#ifdef CONFIG_OF
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002123static int of_spi_register_master(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002124{
Grant Likelye80beb22013-02-12 17:48:37 +00002125 int nb, i, *cs;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002126 struct device_node *np = ctlr->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002127
2128 if (!np)
2129 return 0;
2130
2131 nb = of_gpio_named_count(np, "cs-gpios");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002132 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002133
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002134 /* Return error only for an incorrectly formed cs-gpios property */
2135 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002136 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002137 else if (nb < 0)
2138 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002139
Kees Cooka86854d2018-06-12 14:07:58 -07002140 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002141 GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002142 ctlr->cs_gpios = cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002143
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002144 if (!ctlr->cs_gpios)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002145 return -ENOMEM;
2146
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002147 for (i = 0; i < ctlr->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01002148 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002149
2150 for (i = 0; i < nb; i++)
2151 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2152
2153 return 0;
2154}
2155#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002156static int of_spi_register_master(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002157{
2158 return 0;
2159}
2160#endif
2161
Linus Walleijf3186dd2019-01-07 16:51:50 +01002162/**
2163 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2164 * @ctlr: The SPI master to grab GPIO descriptors for
2165 */
2166static int spi_get_gpio_descs(struct spi_controller *ctlr)
2167{
2168 int nb, i;
2169 struct gpio_desc **cs;
2170 struct device *dev = &ctlr->dev;
2171
2172 nb = gpiod_count(dev, "cs");
2173 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2174
2175 /* No GPIOs at all is fine, else return the error */
2176 if (nb == 0 || nb == -ENOENT)
2177 return 0;
2178 else if (nb < 0)
2179 return nb;
2180
2181 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
2182 GFP_KERNEL);
2183 if (!cs)
2184 return -ENOMEM;
2185 ctlr->cs_gpiods = cs;
2186
2187 for (i = 0; i < nb; i++) {
2188 /*
2189 * Most chipselects are active low, the inverted
2190 * semantics are handled by special quirks in gpiolib,
2191 * so initializing them GPIOD_OUT_LOW here means
2192 * "unasserted", in most cases this will drive the physical
2193 * line high.
2194 */
2195 cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
2196 GPIOD_OUT_LOW);
2197
2198 if (cs[i]) {
2199 /*
2200 * If we find a CS GPIO, name it after the device and
2201 * chip select line.
2202 */
2203 char *gpioname;
2204
2205 gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
2206 dev_name(dev), i);
2207 if (!gpioname)
2208 return -ENOMEM;
2209 gpiod_set_consumer_name(cs[i], gpioname);
2210 }
2211 }
2212
2213 return 0;
2214}
2215
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002216static int spi_controller_check_ops(struct spi_controller *ctlr)
2217{
2218 /*
Boris Brezillonb5932f52018-04-26 18:18:15 +02002219 * The controller may implement only the high-level SPI-memory like
2220 * operations if it does not support regular SPI transfers, and this is
2221 * valid use case.
2222 * If ->mem_ops is NULL, we request that at least one of the
2223 * ->transfer_xxx() method be implemented.
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002224 */
Boris Brezillonb5932f52018-04-26 18:18:15 +02002225 if (ctlr->mem_ops) {
2226 if (!ctlr->mem_ops->exec_op)
2227 return -EINVAL;
2228 } else if (!ctlr->transfer && !ctlr->transfer_one &&
2229 !ctlr->transfer_one_message) {
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002230 return -EINVAL;
Boris Brezillonb5932f52018-04-26 18:18:15 +02002231 }
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002232
2233 return 0;
2234}
2235
David Brownell8ae12a02006-01-08 13:34:19 -08002236/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002237 * spi_register_controller - register SPI master or slave controller
2238 * @ctlr: initialized master, originally from spi_alloc_master() or
2239 * spi_alloc_slave()
David Brownell33e34dc2007-05-08 00:32:21 -07002240 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002241 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002242 * SPI controllers connect to their drivers using some non-SPI bus,
David Brownell8ae12a02006-01-08 13:34:19 -08002243 * such as the platform bus. The final stage of probe() in that code
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002244 * includes calling spi_register_controller() to hook up to this SPI bus glue.
David Brownell8ae12a02006-01-08 13:34:19 -08002245 *
2246 * SPI controllers use board specific (often SOC specific) bus numbers,
2247 * and board-specific addressing for SPI devices combines those numbers
2248 * with chip select numbers. Since SPI does not directly support dynamic
2249 * device identification, boards need configuration tables telling which
2250 * chip is at which address.
2251 *
2252 * This must be called from context that can sleep. It returns zero on
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002253 * success, else a negative error code (dropping the controller's refcount).
David Brownell0c8684612006-01-08 13:34:25 -08002254 * After a successful return, the caller is responsible for calling
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002255 * spi_unregister_controller().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002256 *
2257 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002258 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002259int spi_register_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002260{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002261 struct device *dev = ctlr->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08002262 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08002263 int status = -ENODEV;
Lucas Stach42bdd702017-10-16 12:27:58 +02002264 int id, first_dynamic;
David Brownell8ae12a02006-01-08 13:34:19 -08002265
David Brownell0c8684612006-01-08 13:34:25 -08002266 if (!dev)
2267 return -ENODEV;
2268
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002269 /*
2270 * Make sure all necessary hooks are implemented before registering
2271 * the SPI controller.
2272 */
2273 status = spi_controller_check_ops(ctlr);
2274 if (status)
2275 return status;
2276
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002277 if (!spi_controller_is_slave(ctlr)) {
Linus Walleijf3186dd2019-01-07 16:51:50 +01002278 if (ctlr->use_gpio_descriptors) {
2279 status = spi_get_gpio_descs(ctlr);
2280 if (status)
2281 return status;
2282 } else {
2283 /* Legacy code path for GPIOs from DT */
2284 status = of_spi_register_master(ctlr);
2285 if (status)
2286 return status;
2287 }
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002288 }
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002289
David Brownell082c8cb2007-07-31 00:39:45 -07002290 /* even if it's just one always-selected device, there must
2291 * be at least one chipselect
2292 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002293 if (ctlr->num_chipselect == 0)
David Brownell082c8cb2007-07-31 00:39:45 -07002294 return -EINVAL;
Geert Uytterhoeven04b2d032018-08-21 11:53:03 +02002295 if (ctlr->bus_num >= 0) {
2296 /* devices with a fixed bus num must check-in with the num */
2297 mutex_lock(&board_lock);
2298 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2299 ctlr->bus_num + 1, GFP_KERNEL);
2300 mutex_unlock(&board_lock);
2301 if (WARN(id < 0, "couldn't get idr"))
2302 return id == -ENOSPC ? -EBUSY : id;
2303 ctlr->bus_num = id;
2304 } else if (ctlr->dev.of_node) {
2305 /* allocate dynamic bus number using Linux idr */
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302306 id = of_alias_get_id(ctlr->dev.of_node, "spi");
2307 if (id >= 0) {
2308 ctlr->bus_num = id;
2309 mutex_lock(&board_lock);
2310 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2311 ctlr->bus_num + 1, GFP_KERNEL);
2312 mutex_unlock(&board_lock);
2313 if (WARN(id < 0, "couldn't get idr"))
2314 return id == -ENOSPC ? -EBUSY : id;
2315 }
2316 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002317 if (ctlr->bus_num < 0) {
Lucas Stach42bdd702017-10-16 12:27:58 +02002318 first_dynamic = of_alias_get_highest_id("spi");
2319 if (first_dynamic < 0)
2320 first_dynamic = 0;
2321 else
2322 first_dynamic++;
2323
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302324 mutex_lock(&board_lock);
Lucas Stach42bdd702017-10-16 12:27:58 +02002325 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
2326 0, GFP_KERNEL);
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302327 mutex_unlock(&board_lock);
2328 if (WARN(id < 0, "couldn't get idr"))
2329 return id;
2330 ctlr->bus_num = id;
David Brownell8ae12a02006-01-08 13:34:19 -08002331 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002332 INIT_LIST_HEAD(&ctlr->queue);
2333 spin_lock_init(&ctlr->queue_lock);
2334 spin_lock_init(&ctlr->bus_lock_spinlock);
2335 mutex_init(&ctlr->bus_lock_mutex);
2336 mutex_init(&ctlr->io_mutex);
2337 ctlr->bus_lock_flag = 0;
2338 init_completion(&ctlr->xfer_completion);
2339 if (!ctlr->max_dma_len)
2340 ctlr->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002341
David Brownell8ae12a02006-01-08 13:34:19 -08002342 /* register the device, then userspace will see it.
2343 * registration fails if the bus ID is in use.
2344 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002345 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
2346 status = device_add(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302347 if (status < 0) {
2348 /* free bus id */
2349 mutex_lock(&board_lock);
2350 idr_remove(&spi_master_idr, ctlr->bus_num);
2351 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002352 goto done;
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302353 }
2354 dev_dbg(dev, "registered %s %s\n",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002355 spi_controller_is_slave(ctlr) ? "slave" : "master",
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302356 dev_name(&ctlr->dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002357
Boris Brezillonb5932f52018-04-26 18:18:15 +02002358 /*
2359 * If we're using a queued driver, start the queue. Note that we don't
2360 * need the queueing logic if the driver is only supporting high-level
2361 * memory operations.
2362 */
2363 if (ctlr->transfer) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002364 dev_info(dev, "controller is unqueued, this is deprecated\n");
Boris Brezillonb5932f52018-04-26 18:18:15 +02002365 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002366 status = spi_controller_initialize_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002367 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002368 device_del(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302369 /* free bus id */
2370 mutex_lock(&board_lock);
2371 idr_remove(&spi_master_idr, ctlr->bus_num);
2372 mutex_unlock(&board_lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002373 goto done;
2374 }
2375 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00002376 /* add statistics */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002377 spin_lock_init(&ctlr->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002378
Feng Tang2b9603a2010-08-02 15:52:15 +08002379 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002380 list_add_tail(&ctlr->list, &spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002381 list_for_each_entry(bi, &board_list, list)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002382 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +08002383 mutex_unlock(&board_lock);
2384
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002385 /* Register devices from the device tree and ACPI */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002386 of_register_spi_devices(ctlr);
2387 acpi_register_spi_devices(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002388done:
2389 return status;
2390}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002391EXPORT_SYMBOL_GPL(spi_register_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002392
Mark Brown666d5b42013-08-31 18:50:52 +01002393static void devm_spi_unregister(struct device *dev, void *res)
2394{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002395 spi_unregister_controller(*(struct spi_controller **)res);
Mark Brown666d5b42013-08-31 18:50:52 +01002396}
2397
2398/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002399 * devm_spi_register_controller - register managed SPI master or slave
2400 * controller
2401 * @dev: device managing SPI controller
2402 * @ctlr: initialized controller, originally from spi_alloc_master() or
2403 * spi_alloc_slave()
Mark Brown666d5b42013-08-31 18:50:52 +01002404 * Context: can sleep
2405 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002406 * Register a SPI device as with spi_register_controller() which will
Johan Hovold68b892f2017-10-30 11:35:26 +01002407 * automatically be unregistered and freed.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002408 *
2409 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01002410 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002411int devm_spi_register_controller(struct device *dev,
2412 struct spi_controller *ctlr)
Mark Brown666d5b42013-08-31 18:50:52 +01002413{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002414 struct spi_controller **ptr;
Mark Brown666d5b42013-08-31 18:50:52 +01002415 int ret;
2416
2417 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2418 if (!ptr)
2419 return -ENOMEM;
2420
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002421 ret = spi_register_controller(ctlr);
Stephen Warren4b928942013-11-21 16:11:15 -07002422 if (!ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002423 *ptr = ctlr;
Mark Brown666d5b42013-08-31 18:50:52 +01002424 devres_add(dev, ptr);
2425 } else {
2426 devres_free(ptr);
2427 }
2428
2429 return ret;
2430}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002431EXPORT_SYMBOL_GPL(devm_spi_register_controller);
Mark Brown666d5b42013-08-31 18:50:52 +01002432
David Lamparter34860082010-08-30 23:54:17 +02002433static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002434{
David Lamparter34860082010-08-30 23:54:17 +02002435 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002436 return 0;
2437}
2438
2439/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002440 * spi_unregister_controller - unregister SPI master or slave controller
2441 * @ctlr: the controller being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002442 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002443 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002444 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002445 * only ones directly touching chip registers.
2446 *
2447 * This must be called from context that can sleep.
Johan Hovold68b892f2017-10-30 11:35:26 +01002448 *
2449 * Note that this function also drops a reference to the controller.
David Brownell8ae12a02006-01-08 13:34:19 -08002450 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002451void spi_unregister_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002452{
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302453 struct spi_controller *found;
Johan Hovold67f7b272017-10-30 11:35:25 +01002454 int id = ctlr->bus_num;
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002455 int dummy;
2456
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302457 /* First make sure that this controller was ever added */
2458 mutex_lock(&board_lock);
Johan Hovold67f7b272017-10-30 11:35:25 +01002459 found = idr_find(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302460 mutex_unlock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002461 if (ctlr->queued) {
2462 if (spi_destroy_queue(ctlr))
2463 dev_err(&ctlr->dev, "queue remove failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002464 }
Feng Tang2b9603a2010-08-02 15:52:15 +08002465 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002466 list_del(&ctlr->list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002467 mutex_unlock(&board_lock);
2468
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002469 dummy = device_for_each_child(&ctlr->dev, NULL, __unregister);
2470 device_unregister(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302471 /* free bus id */
2472 mutex_lock(&board_lock);
Jarkko Nikula613bd1e2018-03-20 10:27:50 +02002473 if (found == ctlr)
2474 idr_remove(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302475 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002476}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002477EXPORT_SYMBOL_GPL(spi_unregister_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002478
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002479int spi_controller_suspend(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002480{
2481 int ret;
2482
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002483 /* Basically no-ops for non-queued controllers */
2484 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002485 return 0;
2486
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002487 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002488 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002489 dev_err(&ctlr->dev, "queue stop failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002490
2491 return ret;
2492}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002493EXPORT_SYMBOL_GPL(spi_controller_suspend);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002494
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002495int spi_controller_resume(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002496{
2497 int ret;
2498
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002499 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002500 return 0;
2501
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002502 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002503 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002504 dev_err(&ctlr->dev, "queue restart failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002505
2506 return ret;
2507}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002508EXPORT_SYMBOL_GPL(spi_controller_resume);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002509
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002510static int __spi_controller_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002511{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002512 struct spi_controller *ctlr;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002513 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002514
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002515 ctlr = container_of(dev, struct spi_controller, dev);
2516 return ctlr->bus_num == *bus_num;
Dave Young5ed2c832008-01-22 15:14:18 +08002517}
2518
David Brownell8ae12a02006-01-08 13:34:19 -08002519/**
2520 * spi_busnum_to_master - look up master associated with bus_num
2521 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002522 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002523 *
2524 * This call may be used with devices that are registered after
2525 * arch init time. It returns a refcounted pointer to the relevant
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002526 * spi_controller (which the caller must release), or NULL if there is
David Brownell8ae12a02006-01-08 13:34:19 -08002527 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002528 *
2529 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002530 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002531struct spi_controller *spi_busnum_to_master(u16 bus_num)
David Brownell8ae12a02006-01-08 13:34:19 -08002532{
Tony Jones49dce682007-10-16 01:27:48 -07002533 struct device *dev;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002534 struct spi_controller *ctlr = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002535
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002536 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002537 __spi_controller_match);
Dave Young5ed2c832008-01-22 15:14:18 +08002538 if (dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002539 ctlr = container_of(dev, struct spi_controller, dev);
Dave Young5ed2c832008-01-22 15:14:18 +08002540 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002541 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002542}
2543EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2544
Martin Sperld780c372015-12-14 15:20:18 +00002545/*-------------------------------------------------------------------------*/
2546
2547/* Core methods for SPI resource management */
2548
2549/**
2550 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2551 * during the processing of a spi_message while using
2552 * spi_transfer_one
2553 * @spi: the spi device for which we allocate memory
2554 * @release: the release code to execute for this resource
2555 * @size: size to alloc and return
2556 * @gfp: GFP allocation flags
2557 *
2558 * Return: the pointer to the allocated data
2559 *
2560 * This may get enhanced in the future to allocate from a memory pool
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002561 * of the @spi_device or @spi_controller to avoid repeated allocations.
Martin Sperld780c372015-12-14 15:20:18 +00002562 */
2563void *spi_res_alloc(struct spi_device *spi,
2564 spi_res_release_t release,
2565 size_t size, gfp_t gfp)
2566{
2567 struct spi_res *sres;
2568
2569 sres = kzalloc(sizeof(*sres) + size, gfp);
2570 if (!sres)
2571 return NULL;
2572
2573 INIT_LIST_HEAD(&sres->entry);
2574 sres->release = release;
2575
2576 return sres->data;
2577}
2578EXPORT_SYMBOL_GPL(spi_res_alloc);
2579
2580/**
2581 * spi_res_free - free an spi resource
2582 * @res: pointer to the custom data of a resource
2583 *
2584 */
2585void spi_res_free(void *res)
2586{
2587 struct spi_res *sres = container_of(res, struct spi_res, data);
2588
2589 if (!res)
2590 return;
2591
2592 WARN_ON(!list_empty(&sres->entry));
2593 kfree(sres);
2594}
2595EXPORT_SYMBOL_GPL(spi_res_free);
2596
2597/**
2598 * spi_res_add - add a spi_res to the spi_message
2599 * @message: the spi message
2600 * @res: the spi_resource
2601 */
2602void spi_res_add(struct spi_message *message, void *res)
2603{
2604 struct spi_res *sres = container_of(res, struct spi_res, data);
2605
2606 WARN_ON(!list_empty(&sres->entry));
2607 list_add_tail(&sres->entry, &message->resources);
2608}
2609EXPORT_SYMBOL_GPL(spi_res_add);
2610
2611/**
2612 * spi_res_release - release all spi resources for this message
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002613 * @ctlr: the @spi_controller
Martin Sperld780c372015-12-14 15:20:18 +00002614 * @message: the @spi_message
2615 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002616void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
Martin Sperld780c372015-12-14 15:20:18 +00002617{
2618 struct spi_res *res;
2619
2620 while (!list_empty(&message->resources)) {
2621 res = list_last_entry(&message->resources,
2622 struct spi_res, entry);
2623
2624 if (res->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002625 res->release(ctlr, message, res->data);
Martin Sperld780c372015-12-14 15:20:18 +00002626
2627 list_del(&res->entry);
2628
2629 kfree(res);
2630 }
2631}
2632EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002633
2634/*-------------------------------------------------------------------------*/
2635
Martin Sperl523baf5a2015-12-14 15:20:19 +00002636/* Core methods for spi_message alterations */
2637
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002638static void __spi_replace_transfers_release(struct spi_controller *ctlr,
Martin Sperl523baf5a2015-12-14 15:20:19 +00002639 struct spi_message *msg,
2640 void *res)
2641{
2642 struct spi_replaced_transfers *rxfer = res;
2643 size_t i;
2644
2645 /* call extra callback if requested */
2646 if (rxfer->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002647 rxfer->release(ctlr, msg, res);
Martin Sperl523baf5a2015-12-14 15:20:19 +00002648
2649 /* insert replaced transfers back into the message */
2650 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2651
2652 /* remove the formerly inserted entries */
2653 for (i = 0; i < rxfer->inserted; i++)
2654 list_del(&rxfer->inserted_transfers[i].transfer_list);
2655}
2656
2657/**
2658 * spi_replace_transfers - replace transfers with several transfers
2659 * and register change with spi_message.resources
2660 * @msg: the spi_message we work upon
2661 * @xfer_first: the first spi_transfer we want to replace
2662 * @remove: number of transfers to remove
2663 * @insert: the number of transfers we want to insert instead
2664 * @release: extra release code necessary in some circumstances
2665 * @extradatasize: extra data to allocate (with alignment guarantees
2666 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002667 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002668 *
2669 * Returns: pointer to @spi_replaced_transfers,
2670 * PTR_ERR(...) in case of errors.
2671 */
2672struct spi_replaced_transfers *spi_replace_transfers(
2673 struct spi_message *msg,
2674 struct spi_transfer *xfer_first,
2675 size_t remove,
2676 size_t insert,
2677 spi_replaced_release_t release,
2678 size_t extradatasize,
2679 gfp_t gfp)
2680{
2681 struct spi_replaced_transfers *rxfer;
2682 struct spi_transfer *xfer;
2683 size_t i;
2684
2685 /* allocate the structure using spi_res */
2686 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2687 insert * sizeof(struct spi_transfer)
2688 + sizeof(struct spi_replaced_transfers)
2689 + extradatasize,
2690 gfp);
2691 if (!rxfer)
2692 return ERR_PTR(-ENOMEM);
2693
2694 /* the release code to invoke before running the generic release */
2695 rxfer->release = release;
2696
2697 /* assign extradata */
2698 if (extradatasize)
2699 rxfer->extradata =
2700 &rxfer->inserted_transfers[insert];
2701
2702 /* init the replaced_transfers list */
2703 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2704
2705 /* assign the list_entry after which we should reinsert
2706 * the @replaced_transfers - it may be spi_message.messages!
2707 */
2708 rxfer->replaced_after = xfer_first->transfer_list.prev;
2709
2710 /* remove the requested number of transfers */
2711 for (i = 0; i < remove; i++) {
2712 /* if the entry after replaced_after it is msg->transfers
2713 * then we have been requested to remove more transfers
2714 * than are in the list
2715 */
2716 if (rxfer->replaced_after->next == &msg->transfers) {
2717 dev_err(&msg->spi->dev,
2718 "requested to remove more spi_transfers than are available\n");
2719 /* insert replaced transfers back into the message */
2720 list_splice(&rxfer->replaced_transfers,
2721 rxfer->replaced_after);
2722
2723 /* free the spi_replace_transfer structure */
2724 spi_res_free(rxfer);
2725
2726 /* and return with an error */
2727 return ERR_PTR(-EINVAL);
2728 }
2729
2730 /* remove the entry after replaced_after from list of
2731 * transfers and add it to list of replaced_transfers
2732 */
2733 list_move_tail(rxfer->replaced_after->next,
2734 &rxfer->replaced_transfers);
2735 }
2736
2737 /* create copy of the given xfer with identical settings
2738 * based on the first transfer to get removed
2739 */
2740 for (i = 0; i < insert; i++) {
2741 /* we need to run in reverse order */
2742 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2743
2744 /* copy all spi_transfer data */
2745 memcpy(xfer, xfer_first, sizeof(*xfer));
2746
2747 /* add to list */
2748 list_add(&xfer->transfer_list, rxfer->replaced_after);
2749
2750 /* clear cs_change and delay_usecs for all but the last */
2751 if (i) {
2752 xfer->cs_change = false;
2753 xfer->delay_usecs = 0;
2754 }
2755 }
2756
2757 /* set up inserted */
2758 rxfer->inserted = insert;
2759
2760 /* and register it with spi_res/spi_message */
2761 spi_res_add(msg, rxfer);
2762
2763 return rxfer;
2764}
2765EXPORT_SYMBOL_GPL(spi_replace_transfers);
2766
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002767static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
Fabio Estevam08933412016-02-14 13:33:50 -02002768 struct spi_message *msg,
2769 struct spi_transfer **xferp,
2770 size_t maxsize,
2771 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00002772{
2773 struct spi_transfer *xfer = *xferp, *xfers;
2774 struct spi_replaced_transfers *srt;
2775 size_t offset;
2776 size_t count, i;
2777
2778 /* warn once about this fact that we are splitting a transfer */
2779 dev_warn_once(&msg->spi->dev,
Fabio Estevam7d62f512016-02-17 15:42:27 -02002780 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
Martin Sperld9f12122015-12-14 15:20:20 +00002781 xfer->len, maxsize);
2782
2783 /* calculate how many we have to replace */
2784 count = DIV_ROUND_UP(xfer->len, maxsize);
2785
2786 /* create replacement */
2787 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03002788 if (IS_ERR(srt))
2789 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00002790 xfers = srt->inserted_transfers;
2791
2792 /* now handle each of those newly inserted spi_transfers
2793 * note that the replacements spi_transfers all are preset
2794 * to the same values as *xferp, so tx_buf, rx_buf and len
2795 * are all identical (as well as most others)
2796 * so we just have to fix up len and the pointers.
2797 *
2798 * this also includes support for the depreciated
2799 * spi_message.is_dma_mapped interface
2800 */
2801
2802 /* the first transfer just needs the length modified, so we
2803 * run it outside the loop
2804 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02002805 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00002806
2807 /* all the others need rx_buf/tx_buf also set */
2808 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2809 /* update rx_buf, tx_buf and dma */
2810 if (xfers[i].rx_buf)
2811 xfers[i].rx_buf += offset;
2812 if (xfers[i].rx_dma)
2813 xfers[i].rx_dma += offset;
2814 if (xfers[i].tx_buf)
2815 xfers[i].tx_buf += offset;
2816 if (xfers[i].tx_dma)
2817 xfers[i].tx_dma += offset;
2818
2819 /* update length */
2820 xfers[i].len = min(maxsize, xfers[i].len - offset);
2821 }
2822
2823 /* we set up xferp to the last entry we have inserted,
2824 * so that we skip those already split transfers
2825 */
2826 *xferp = &xfers[count - 1];
2827
2828 /* increment statistics counters */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002829 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperld9f12122015-12-14 15:20:20 +00002830 transfers_split_maxsize);
2831 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2832 transfers_split_maxsize);
2833
2834 return 0;
2835}
2836
2837/**
2838 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2839 * when an individual transfer exceeds a
2840 * certain size
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002841 * @ctlr: the @spi_controller for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09002842 * @msg: the @spi_message to transform
2843 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03002844 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00002845 *
2846 * Return: status of transformation
2847 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002848int spi_split_transfers_maxsize(struct spi_controller *ctlr,
Martin Sperld9f12122015-12-14 15:20:20 +00002849 struct spi_message *msg,
2850 size_t maxsize,
2851 gfp_t gfp)
2852{
2853 struct spi_transfer *xfer;
2854 int ret;
2855
2856 /* iterate over the transfer_list,
2857 * but note that xfer is advanced to the last transfer inserted
2858 * to avoid checking sizes again unnecessarily (also xfer does
2859 * potentiall belong to a different list by the time the
2860 * replacement has happened
2861 */
2862 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2863 if (xfer->len > maxsize) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002864 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
2865 maxsize, gfp);
Martin Sperld9f12122015-12-14 15:20:20 +00002866 if (ret)
2867 return ret;
2868 }
2869 }
2870
2871 return 0;
2872}
2873EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08002874
2875/*-------------------------------------------------------------------------*/
2876
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002877/* Core methods for SPI controller protocol drivers. Some of the
David Brownell7d077192009-06-17 16:26:03 -07002878 * other core methods are currently defined as inline functions.
2879 */
2880
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002881static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
2882 u8 bits_per_word)
Stefan Brüns63ab6452015-08-23 16:06:30 +02002883{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002884 if (ctlr->bits_per_word_mask) {
Stefan Brüns63ab6452015-08-23 16:06:30 +02002885 /* Only 32 bits fit in the mask */
2886 if (bits_per_word > 32)
2887 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002888 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
Stefan Brüns63ab6452015-08-23 16:06:30 +02002889 return -EINVAL;
2890 }
2891
2892 return 0;
2893}
2894
David Brownell7d077192009-06-17 16:26:03 -07002895/**
2896 * spi_setup - setup SPI mode and clock rate
2897 * @spi: the device whose settings are being modified
2898 * Context: can sleep, and no requests are queued to the device
2899 *
2900 * SPI protocol drivers may need to update the transfer mode if the
2901 * device doesn't work with its default. They may likewise need
2902 * to update clock rates or word sizes from initial values. This function
2903 * changes those settings, and must be called from a context that can sleep.
2904 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
2905 * effect the next time the device is selected and data is transferred to
2906 * or from it. When this function returns, the spi device is deselected.
2907 *
2908 * Note that this call will fail if the protocol driver specifies an option
2909 * that the underlying controller or its driver does not support. For
2910 * example, not all hardware supports wire transfers using nine bit words,
2911 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002912 *
2913 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07002914 */
2915int spi_setup(struct spi_device *spi)
2916{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002917 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002918 int status;
David Brownell7d077192009-06-17 16:26:03 -07002919
wangyuhangf477b7f2013-08-11 18:15:17 +08002920 /* check mode to prevent that DUAL and QUAD set at the same time
2921 */
2922 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
2923 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
2924 dev_err(&spi->dev,
2925 "setup: can not select dual and quad at the same time\n");
2926 return -EINVAL;
2927 }
2928 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
2929 */
2930 if ((spi->mode & SPI_3WIRE) && (spi->mode &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00002931 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
2932 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
wangyuhangf477b7f2013-08-11 18:15:17 +08002933 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07002934 /* help drivers fail *cleanly* when they need options
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002935 * that aren't supported with their current controller
David Lechnercbaa62e2018-09-12 19:39:18 -05002936 * SPI_CS_WORD has a fallback software implementation,
2937 * so it is ignored here.
David Brownelle7db06b2009-06-17 16:26:04 -07002938 */
David Lechnercbaa62e2018-09-12 19:39:18 -05002939 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002940 ugly_bits = bad_bits &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00002941 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
2942 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002943 if (ugly_bits) {
2944 dev_warn(&spi->dev,
2945 "setup: ignoring unsupported mode bits %x\n",
2946 ugly_bits);
2947 spi->mode &= ~ugly_bits;
2948 bad_bits &= ~ugly_bits;
2949 }
David Brownelle7db06b2009-06-17 16:26:04 -07002950 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02002951 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07002952 bad_bits);
2953 return -EINVAL;
2954 }
2955
David Brownell7d077192009-06-17 16:26:03 -07002956 if (!spi->bits_per_word)
2957 spi->bits_per_word = 8;
2958
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002959 status = __spi_validate_bits_per_word(spi->controller,
2960 spi->bits_per_word);
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002961 if (status)
2962 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02002963
Axel Lin052eb2d2014-02-10 00:08:05 +08002964 if (!spi->max_speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002965 spi->max_speed_hz = spi->controller->max_speed_hz;
Axel Lin052eb2d2014-02-10 00:08:05 +08002966
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002967 if (spi->controller->setup)
2968 status = spi->controller->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07002969
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05002970 spi_set_cs(spi, false);
2971
Jingoo Han5fe5f052013-10-14 10:31:51 +09002972 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 -07002973 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2974 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2975 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2976 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2977 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2978 spi->bits_per_word, spi->max_speed_hz,
2979 status);
2980
2981 return status;
2982}
2983EXPORT_SYMBOL_GPL(spi_setup);
2984
Mark Brown90808732013-11-13 23:44:15 +00002985static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002986{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002987 struct spi_controller *ctlr = spi->controller;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302988 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002989 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002990
Mark Brown24a00132013-07-10 15:05:40 +01002991 if (list_empty(&message->transfers))
2992 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01002993
David Lechnercbaa62e2018-09-12 19:39:18 -05002994 /* If an SPI controller does not support toggling the CS line on each
David Lechner71388b212018-09-18 12:08:48 -05002995 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
2996 * for the CS line, we can emulate the CS-per-word hardware function by
David Lechnercbaa62e2018-09-12 19:39:18 -05002997 * splitting transfers into one-word transfers and ensuring that
2998 * cs_change is set for each transfer.
2999 */
David Lechner71388b212018-09-18 12:08:48 -05003000 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
Linus Walleijf3186dd2019-01-07 16:51:50 +01003001 spi->cs_gpiod ||
David Lechner71388b212018-09-18 12:08:48 -05003002 gpio_is_valid(spi->cs_gpio))) {
David Lechnercbaa62e2018-09-12 19:39:18 -05003003 size_t maxsize;
3004 int ret;
3005
3006 maxsize = (spi->bits_per_word + 7) / 8;
3007
3008 /* spi_split_transfers_maxsize() requires message->spi */
3009 message->spi = spi;
3010
3011 ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
3012 GFP_KERNEL);
3013 if (ret)
3014 return ret;
3015
3016 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3017 /* don't change cs_change on the last entry in the list */
3018 if (list_is_last(&xfer->transfer_list, &message->transfers))
3019 break;
3020 xfer->cs_change = 1;
3021 }
3022 }
3023
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003024 /* Half-duplex links include original MicroWire, and ones with
3025 * only one data pin like SPI_3WIRE (switches direction) or where
3026 * either MOSI or MISO is missing. They can also be caused by
3027 * software limitations.
3028 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003029 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
3030 (spi->mode & SPI_3WIRE)) {
3031 unsigned flags = ctlr->flags;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003032
3033 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3034 if (xfer->rx_buf && xfer->tx_buf)
3035 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003036 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003037 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003038 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003039 return -EINVAL;
3040 }
3041 }
3042
Laxman Dewangane6811d12012-11-09 14:36:45 +05303043 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303044 * Set transfer bits_per_word and max speed as spi device default if
3045 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08003046 * Set transfer tx_nbits and rx_nbits as single transfer default
3047 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05303048 */
Martin Sperl77e80582015-11-27 12:31:09 +00003049 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303050 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05303051 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303052 if (!xfer->bits_per_word)
3053 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08003054
3055 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303056 xfer->speed_hz = spi->max_speed_hz;
Mark Brown7dc9fbc2015-08-20 11:52:18 -07003057 if (!xfer->speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003058 xfer->speed_hz = ctlr->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08003059
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003060 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
3061 xfer->speed_hz = ctlr->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02003062
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003063 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
Stefan Brüns63ab6452015-08-23 16:06:30 +02003064 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01003065
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003066 /*
3067 * SPI transfer length should be multiple of SPI word size
3068 * where SPI word size should be power-of-two multiple
3069 */
3070 if (xfer->bits_per_word <= 8)
3071 w_size = 1;
3072 else if (xfer->bits_per_word <= 16)
3073 w_size = 2;
3074 else
3075 w_size = 4;
3076
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003077 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09003078 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003079 return -EINVAL;
3080
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003081 if (xfer->speed_hz && ctlr->min_speed_hz &&
3082 xfer->speed_hz < ctlr->min_speed_hz)
Mark Browna2fd4f92013-07-10 14:57:26 +01003083 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08003084
3085 if (xfer->tx_buf && !xfer->tx_nbits)
3086 xfer->tx_nbits = SPI_NBITS_SINGLE;
3087 if (xfer->rx_buf && !xfer->rx_nbits)
3088 xfer->rx_nbits = SPI_NBITS_SINGLE;
3089 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01003090 * 1. check the value matches one of single, dual and quad
3091 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08003092 */
Sourav Poddardb90a442013-08-22 21:20:48 +05303093 if (xfer->tx_buf) {
3094 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
3095 xfer->tx_nbits != SPI_NBITS_DUAL &&
3096 xfer->tx_nbits != SPI_NBITS_QUAD)
3097 return -EINVAL;
3098 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
3099 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3100 return -EINVAL;
3101 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
3102 !(spi->mode & SPI_TX_QUAD))
3103 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303104 }
wangyuhangf477b7f2013-08-11 18:15:17 +08003105 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05303106 if (xfer->rx_buf) {
3107 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
3108 xfer->rx_nbits != SPI_NBITS_DUAL &&
3109 xfer->rx_nbits != SPI_NBITS_QUAD)
3110 return -EINVAL;
3111 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
3112 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3113 return -EINVAL;
3114 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
3115 !(spi->mode & SPI_RX_QUAD))
3116 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303117 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05303118 }
3119
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003120 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00003121
3122 return 0;
3123}
3124
3125static int __spi_async(struct spi_device *spi, struct spi_message *message)
3126{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003127 struct spi_controller *ctlr = spi->controller;
Mark Brown90808732013-11-13 23:44:15 +00003128
Boris Brezillonb5932f52018-04-26 18:18:15 +02003129 /*
3130 * Some controllers do not support doing regular SPI transfers. Return
3131 * ENOTSUPP when this is the case.
3132 */
3133 if (!ctlr->transfer)
3134 return -ENOTSUPP;
3135
Mark Brown90808732013-11-13 23:44:15 +00003136 message->spi = spi;
3137
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003138 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003139 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3140
Mark Brown90808732013-11-13 23:44:15 +00003141 trace_spi_message_submit(message);
3142
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003143 return ctlr->transfer(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003144}
3145
David Brownell568d0692009-09-22 16:46:18 -07003146/**
3147 * spi_async - asynchronous SPI transfer
3148 * @spi: device with which data will be exchanged
3149 * @message: describes the data transfers, including completion callback
3150 * Context: any (irqs may be blocked, etc)
3151 *
3152 * This call may be used in_irq and other contexts which can't sleep,
3153 * as well as from task contexts which can sleep.
3154 *
3155 * The completion callback is invoked in a context which can't sleep.
3156 * Before that invocation, the value of message->status is undefined.
3157 * When the callback is issued, message->status holds either zero (to
3158 * indicate complete success) or a negative error code. After that
3159 * callback returns, the driver which issued the transfer request may
3160 * deallocate the associated memory; it's no longer in use by any SPI
3161 * core or controller driver code.
3162 *
3163 * Note that although all messages to a spi_device are handled in
3164 * FIFO order, messages may go to different devices in other orders.
3165 * Some device might be higher priority, or have various "hard" access
3166 * time requirements, for example.
3167 *
3168 * On detection of any fault during the transfer, processing of
3169 * the entire message is aborted, and the device is deselected.
3170 * Until returning from the associated message completion callback,
3171 * no other spi_message queued to that device will be processed.
3172 * (This rule applies equally to all the synchronous transfer calls,
3173 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003174 *
3175 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07003176 */
3177int spi_async(struct spi_device *spi, struct spi_message *message)
3178{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003179 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003180 int ret;
3181 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07003182
Mark Brown90808732013-11-13 23:44:15 +00003183 ret = __spi_validate(spi, message);
3184 if (ret != 0)
3185 return ret;
3186
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003187 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07003188
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003189 if (ctlr->bus_lock_flag)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003190 ret = -EBUSY;
3191 else
3192 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07003193
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003194 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003195
3196 return ret;
David Brownell568d0692009-09-22 16:46:18 -07003197}
3198EXPORT_SYMBOL_GPL(spi_async);
3199
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003200/**
3201 * spi_async_locked - version of spi_async with exclusive bus usage
3202 * @spi: device with which data will be exchanged
3203 * @message: describes the data transfers, including completion callback
3204 * Context: any (irqs may be blocked, etc)
3205 *
3206 * This call may be used in_irq and other contexts which can't sleep,
3207 * as well as from task contexts which can sleep.
3208 *
3209 * The completion callback is invoked in a context which can't sleep.
3210 * Before that invocation, the value of message->status is undefined.
3211 * When the callback is issued, message->status holds either zero (to
3212 * indicate complete success) or a negative error code. After that
3213 * callback returns, the driver which issued the transfer request may
3214 * deallocate the associated memory; it's no longer in use by any SPI
3215 * core or controller driver code.
3216 *
3217 * Note that although all messages to a spi_device are handled in
3218 * FIFO order, messages may go to different devices in other orders.
3219 * Some device might be higher priority, or have various "hard" access
3220 * time requirements, for example.
3221 *
3222 * On detection of any fault during the transfer, processing of
3223 * the entire message is aborted, and the device is deselected.
3224 * Until returning from the associated message completion callback,
3225 * no other spi_message queued to that device will be processed.
3226 * (This rule applies equally to all the synchronous transfer calls,
3227 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003228 *
3229 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003230 */
3231int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3232{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003233 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003234 int ret;
3235 unsigned long flags;
3236
Mark Brown90808732013-11-13 23:44:15 +00003237 ret = __spi_validate(spi, message);
3238 if (ret != 0)
3239 return ret;
3240
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003241 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003242
3243 ret = __spi_async(spi, message);
3244
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003245 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003246
3247 return ret;
3248
3249}
3250EXPORT_SYMBOL_GPL(spi_async_locked);
3251
David Brownell7d077192009-06-17 16:26:03 -07003252/*-------------------------------------------------------------------------*/
3253
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003254/* Utility methods for SPI protocol drivers, layered on
David Brownell7d077192009-06-17 16:26:03 -07003255 * top of the core. Some other utility methods are defined as
3256 * inline functions.
3257 */
3258
Andrew Morton5d870c82006-01-11 11:23:49 -08003259static void spi_complete(void *arg)
3260{
3261 complete(arg);
3262}
3263
Mark Brownef4d96e2016-07-21 23:53:31 +01003264static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003265{
3266 DECLARE_COMPLETION_ONSTACK(done);
3267 int status;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003268 struct spi_controller *ctlr = spi->controller;
Mark Brown0461a412014-12-09 21:38:05 +00003269 unsigned long flags;
3270
3271 status = __spi_validate(spi, message);
3272 if (status != 0)
3273 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003274
3275 message->complete = spi_complete;
3276 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00003277 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003278
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003279 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003280 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3281
Mark Brown0461a412014-12-09 21:38:05 +00003282 /* If we're not using the legacy transfer method then we will
3283 * try to transfer in the calling context so special case.
3284 * This code would be less tricky if we could remove the
3285 * support for driver implemented message queues.
3286 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003287 if (ctlr->transfer == spi_queued_transfer) {
3288 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003289
3290 trace_spi_message_submit(message);
3291
3292 status = __spi_queued_transfer(spi, message, false);
3293
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003294 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003295 } else {
3296 status = spi_async_locked(spi, message);
3297 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003298
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003299 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00003300 /* Push out the messages in the calling context if we
3301 * can.
3302 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003303 if (ctlr->transfer == spi_queued_transfer) {
3304 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperleca2ebc2015-06-22 13:00:36 +00003305 spi_sync_immediate);
3306 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3307 spi_sync_immediate);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003308 __spi_pump_messages(ctlr, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003309 }
Mark Brown0461a412014-12-09 21:38:05 +00003310
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003311 wait_for_completion(&done);
3312 status = message->status;
3313 }
3314 message->context = NULL;
3315 return status;
3316}
3317
David Brownell8ae12a02006-01-08 13:34:19 -08003318/**
3319 * spi_sync - blocking/synchronous SPI data transfers
3320 * @spi: device with which data will be exchanged
3321 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07003322 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003323 *
3324 * This call may only be used from a context that may sleep. The sleep
3325 * is non-interruptible, and has no timeout. Low-overhead controller
3326 * drivers may DMA directly into and out of the message buffers.
3327 *
3328 * Note that the SPI device's chip select is active during the message,
3329 * and then is normally disabled between messages. Drivers for some
3330 * frequently-used devices may want to minimize costs of selecting a chip,
3331 * by leaving it selected in anticipation that the next message will go
3332 * to the same chip. (That may increase power usage.)
3333 *
David Brownell0c8684612006-01-08 13:34:25 -08003334 * Also, the caller is guaranteeing that the memory associated with the
3335 * message will not be freed before this call returns.
3336 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003337 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003338 */
3339int spi_sync(struct spi_device *spi, struct spi_message *message)
3340{
Mark Brownef4d96e2016-07-21 23:53:31 +01003341 int ret;
3342
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003343 mutex_lock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003344 ret = __spi_sync(spi, message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003345 mutex_unlock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003346
3347 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08003348}
3349EXPORT_SYMBOL_GPL(spi_sync);
3350
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003351/**
3352 * spi_sync_locked - version of spi_sync with exclusive bus usage
3353 * @spi: device with which data will be exchanged
3354 * @message: describes the data transfers
3355 * Context: can sleep
3356 *
3357 * This call may only be used from a context that may sleep. The sleep
3358 * is non-interruptible, and has no timeout. Low-overhead controller
3359 * drivers may DMA directly into and out of the message buffers.
3360 *
3361 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003362 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003363 * be released by a spi_bus_unlock call when the exclusive access is over.
3364 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003365 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003366 */
3367int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3368{
Mark Brownef4d96e2016-07-21 23:53:31 +01003369 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003370}
3371EXPORT_SYMBOL_GPL(spi_sync_locked);
3372
3373/**
3374 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003375 * @ctlr: SPI bus master that should be locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003376 * Context: can sleep
3377 *
3378 * This call may only be used from a context that may sleep. The sleep
3379 * is non-interruptible, and has no timeout.
3380 *
3381 * This call should be used by drivers that require exclusive access to the
3382 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3383 * exclusive access is over. Data transfer must be done by spi_sync_locked
3384 * and spi_async_locked calls when the SPI bus lock is held.
3385 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003386 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003387 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003388int spi_bus_lock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003389{
3390 unsigned long flags;
3391
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003392 mutex_lock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003393
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003394 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3395 ctlr->bus_lock_flag = 1;
3396 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003397
3398 /* mutex remains locked until spi_bus_unlock is called */
3399
3400 return 0;
3401}
3402EXPORT_SYMBOL_GPL(spi_bus_lock);
3403
3404/**
3405 * spi_bus_unlock - release the lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003406 * @ctlr: SPI bus master that was locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003407 * Context: can sleep
3408 *
3409 * This call may only be used from a context that may sleep. The sleep
3410 * is non-interruptible, and has no timeout.
3411 *
3412 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3413 * call.
3414 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003415 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003416 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003417int spi_bus_unlock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003418{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003419 ctlr->bus_lock_flag = 0;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003420
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003421 mutex_unlock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003422
3423 return 0;
3424}
3425EXPORT_SYMBOL_GPL(spi_bus_unlock);
3426
David Brownella9948b62006-04-02 10:37:40 -08003427/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003428#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003429
3430static u8 *buf;
3431
3432/**
3433 * spi_write_then_read - SPI synchronous write followed by read
3434 * @spi: device with which data will be exchanged
3435 * @txbuf: data to be written (need not be dma-safe)
3436 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003437 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3438 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003439 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003440 *
3441 * This performs a half duplex MicroWire style transaction with the
3442 * device, sending txbuf and then reading rxbuf. The return value
3443 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003444 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003445 *
David Brownell0c8684612006-01-08 13:34:25 -08003446 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003447 * portable code should never use this for more than 32 bytes.
3448 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c8684612006-01-08 13:34:25 -08003449 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003450 *
3451 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003452 */
3453int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003454 const void *txbuf, unsigned n_tx,
3455 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003456{
David Brownell068f4072007-12-04 23:45:09 -08003457 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003458
3459 int status;
3460 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003461 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003462 u8 *local_buf;
3463
Mark Brownb3a223e2012-12-02 12:54:25 +09003464 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3465 * copying here, (as a pure convenience thing), but we can
3466 * keep heap costs out of the hot path unless someone else is
3467 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003468 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003469 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003470 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3471 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003472 if (!local_buf)
3473 return -ENOMEM;
3474 } else {
3475 local_buf = buf;
3476 }
David Brownell8ae12a02006-01-08 13:34:19 -08003477
Vitaly Wool8275c642006-01-08 13:34:28 -08003478 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003479 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003480 if (n_tx) {
3481 x[0].len = n_tx;
3482 spi_message_add_tail(&x[0], &message);
3483 }
3484 if (n_rx) {
3485 x[1].len = n_rx;
3486 spi_message_add_tail(&x[1], &message);
3487 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003488
David Brownell8ae12a02006-01-08 13:34:19 -08003489 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003490 x[0].tx_buf = local_buf;
3491 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003492
3493 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003494 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003495 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003496 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003497
David Brownellbdff5492009-04-13 14:39:57 -07003498 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003499 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003500 else
3501 kfree(local_buf);
3502
3503 return status;
3504}
3505EXPORT_SYMBOL_GPL(spi_write_then_read);
3506
3507/*-------------------------------------------------------------------------*/
3508
Marco Felsch5f143af2018-09-25 11:42:29 +02003509#if IS_ENABLED(CONFIG_OF)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003510static int __spi_of_device_match(struct device *dev, void *data)
3511{
3512 return dev->of_node == data;
3513}
3514
3515/* must call put_device() when done with returned spi_device device */
Marco Felsch5f143af2018-09-25 11:42:29 +02003516struct spi_device *of_find_spi_device_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003517{
3518 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3519 __spi_of_device_match);
3520 return dev ? to_spi_device(dev) : NULL;
3521}
Marco Felsch5f143af2018-09-25 11:42:29 +02003522EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
3523#endif /* IS_ENABLED(CONFIG_OF) */
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003524
Marco Felsch5f143af2018-09-25 11:42:29 +02003525#if IS_ENABLED(CONFIG_OF_DYNAMIC)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003526static int __spi_of_controller_match(struct device *dev, const void *data)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003527{
3528 return dev->of_node == data;
3529}
3530
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003531/* the spi controllers are not using spi_bus, so we find it with another way */
3532static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003533{
3534 struct device *dev;
3535
3536 dev = class_find_device(&spi_master_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003537 __spi_of_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003538 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3539 dev = class_find_device(&spi_slave_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003540 __spi_of_controller_match);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003541 if (!dev)
3542 return NULL;
3543
3544 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003545 return container_of(dev, struct spi_controller, dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003546}
3547
3548static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3549 void *arg)
3550{
3551 struct of_reconfig_data *rd = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003552 struct spi_controller *ctlr;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003553 struct spi_device *spi;
3554
3555 switch (of_reconfig_get_state_change(action, arg)) {
3556 case OF_RECONFIG_CHANGE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003557 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3558 if (ctlr == NULL)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003559 return NOTIFY_OK; /* not for us */
3560
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003561 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003562 put_device(&ctlr->dev);
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003563 return NOTIFY_OK;
3564 }
3565
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003566 spi = of_register_spi_device(ctlr, rd->dn);
3567 put_device(&ctlr->dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003568
3569 if (IS_ERR(spi)) {
Rob Herring25c56c82017-07-18 16:43:31 -05003570 pr_err("%s: failed to create for '%pOF'\n",
3571 __func__, rd->dn);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003572 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003573 return notifier_from_errno(PTR_ERR(spi));
3574 }
3575 break;
3576
3577 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003578 /* already depopulated? */
3579 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3580 return NOTIFY_OK;
3581
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003582 /* find our device by node */
3583 spi = of_find_spi_device_by_node(rd->dn);
3584 if (spi == NULL)
3585 return NOTIFY_OK; /* no? not meant for us */
3586
3587 /* unregister takes one ref away */
3588 spi_unregister_device(spi);
3589
3590 /* and put the reference of the find */
3591 put_device(&spi->dev);
3592 break;
3593 }
3594
3595 return NOTIFY_OK;
3596}
3597
3598static struct notifier_block spi_of_notifier = {
3599 .notifier_call = of_spi_notify,
3600};
3601#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3602extern struct notifier_block spi_of_notifier;
3603#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3604
Octavian Purdila7f244672016-07-08 19:13:11 +03003605#if IS_ENABLED(CONFIG_ACPI)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003606static int spi_acpi_controller_match(struct device *dev, const void *data)
Octavian Purdila7f244672016-07-08 19:13:11 +03003607{
3608 return ACPI_COMPANION(dev->parent) == data;
3609}
3610
3611static int spi_acpi_device_match(struct device *dev, void *data)
3612{
3613 return ACPI_COMPANION(dev) == data;
3614}
3615
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003616static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
Octavian Purdila7f244672016-07-08 19:13:11 +03003617{
3618 struct device *dev;
3619
3620 dev = class_find_device(&spi_master_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003621 spi_acpi_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003622 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3623 dev = class_find_device(&spi_slave_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003624 spi_acpi_controller_match);
Octavian Purdila7f244672016-07-08 19:13:11 +03003625 if (!dev)
3626 return NULL;
3627
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003628 return container_of(dev, struct spi_controller, dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003629}
3630
3631static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3632{
3633 struct device *dev;
3634
3635 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3636
3637 return dev ? to_spi_device(dev) : NULL;
3638}
3639
3640static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3641 void *arg)
3642{
3643 struct acpi_device *adev = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003644 struct spi_controller *ctlr;
Octavian Purdila7f244672016-07-08 19:13:11 +03003645 struct spi_device *spi;
3646
3647 switch (value) {
3648 case ACPI_RECONFIG_DEVICE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003649 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
3650 if (!ctlr)
Octavian Purdila7f244672016-07-08 19:13:11 +03003651 break;
3652
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003653 acpi_register_spi_device(ctlr, adev);
3654 put_device(&ctlr->dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003655 break;
3656 case ACPI_RECONFIG_DEVICE_REMOVE:
3657 if (!acpi_device_enumerated(adev))
3658 break;
3659
3660 spi = acpi_spi_find_device_by_adev(adev);
3661 if (!spi)
3662 break;
3663
3664 spi_unregister_device(spi);
3665 put_device(&spi->dev);
3666 break;
3667 }
3668
3669 return NOTIFY_OK;
3670}
3671
3672static struct notifier_block spi_acpi_notifier = {
3673 .notifier_call = acpi_spi_notify,
3674};
3675#else
3676extern struct notifier_block spi_acpi_notifier;
3677#endif
3678
David Brownell8ae12a02006-01-08 13:34:19 -08003679static int __init spi_init(void)
3680{
David Brownellb8852442006-01-08 13:34:23 -08003681 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08003682
Christoph Lametere94b1762006-12-06 20:33:17 -08003683 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08003684 if (!buf) {
3685 status = -ENOMEM;
3686 goto err0;
3687 }
3688
3689 status = bus_register(&spi_bus_type);
3690 if (status < 0)
3691 goto err1;
3692
3693 status = class_register(&spi_master_class);
3694 if (status < 0)
3695 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003696
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003697 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
3698 status = class_register(&spi_slave_class);
3699 if (status < 0)
3700 goto err3;
3701 }
3702
Fabio Estevam52677202014-11-26 20:13:57 -02003703 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003704 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03003705 if (IS_ENABLED(CONFIG_ACPI))
3706 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003707
David Brownell8ae12a02006-01-08 13:34:19 -08003708 return 0;
David Brownellb8852442006-01-08 13:34:23 -08003709
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003710err3:
3711 class_unregister(&spi_master_class);
David Brownellb8852442006-01-08 13:34:23 -08003712err2:
3713 bus_unregister(&spi_bus_type);
3714err1:
3715 kfree(buf);
3716 buf = NULL;
3717err0:
3718 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08003719}
David Brownellb8852442006-01-08 13:34:23 -08003720
David Brownell8ae12a02006-01-08 13:34:19 -08003721/* board_info is normally registered in arch_initcall(),
3722 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08003723 *
3724 * REVISIT only boardinfo really needs static linking. the rest (device and
3725 * driver registration) _could_ be dynamically linked (modular) ... costs
3726 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08003727 */
David Brownell673c0c02008-10-15 22:02:46 -07003728postcore_initcall(spi_init);
Mark Brownf0125f12019-01-23 17:29:53 +00003729