blob: 93986f879b09ef2a1e46a8ef4932bd8e0c608070 [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)
Felix Fietkau28f76042019-02-10 00:38:25 +0100789 gpiod_set_value_cansleep(spi->cs_gpiod,
790 !enable);
Linus Walleijf3186dd2019-01-07 16:51:50 +0100791 else
Felix Fietkau28f76042019-02-10 00:38:25 +0100792 gpio_set_value_cansleep(spi->cs_gpio, !enable);
Linus Walleijf3186dd2019-01-07 16:51:50 +0100793 }
Thor Thayer8eee6b92016-10-10 09:25:24 -0500794 /* Some SPI masters need both GPIO CS & slave_select */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200795 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
796 spi->controller->set_cs)
797 spi->controller->set_cs(spi, !enable);
798 } else if (spi->controller->set_cs) {
799 spi->controller->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500800 }
Mark Brownb1589352013-10-05 11:50:40 +0100801}
802
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200803#ifdef CONFIG_HAS_DMA
Boris Brezillon46336962018-04-22 20:35:14 +0200804int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
805 struct sg_table *sgt, void *buf, size_t len,
806 enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000807{
808 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200809 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530810#ifdef CONFIG_HIGHMEM
811 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
812 (unsigned long)buf < (PKMAP_BASE +
813 (LAST_PKMAP * PAGE_SIZE)));
814#else
815 const bool kmap_buf = false;
816#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500817 int desc_len;
818 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000819 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600820 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000821 void *sg_buf;
822 size_t min;
823 int i, ret;
824
Vignesh Rb1b81532016-08-17 15:22:36 +0530825 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200826 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500827 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530828 } else if (virt_addr_valid(buf)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200829 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500830 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530831 } else {
832 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500833 }
834
Mark Brown6ad45a22014-02-02 13:47:47 +0000835 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
836 if (ret != 0)
837 return ret;
838
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600839 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000840 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000841
Vignesh Rb1b81532016-08-17 15:22:36 +0530842 if (vmalloced_buf || kmap_buf) {
Maxime Chevallierce993192018-03-02 15:55:09 +0100843 /*
844 * Next scatterlist entry size is the minimum between
845 * the desc_len and the remaining buffer length that
846 * fits in a page.
847 */
848 min = min_t(size_t, desc_len,
849 min_t(size_t, len,
850 PAGE_SIZE - offset_in_page(buf)));
Vignesh Rb1b81532016-08-17 15:22:36 +0530851 if (vmalloced_buf)
852 vm_page = vmalloc_to_page(buf);
853 else
854 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000855 if (!vm_page) {
856 sg_free_table(sgt);
857 return -ENOMEM;
858 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600859 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000860 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000861 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500862 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000863 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600864 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000865 }
866
Mark Brown6ad45a22014-02-02 13:47:47 +0000867 buf += min;
868 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600869 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000870 }
871
872 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200873 if (!ret)
874 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000875 if (ret < 0) {
876 sg_free_table(sgt);
877 return ret;
878 }
879
880 sgt->nents = ret;
881
882 return 0;
883}
884
Boris Brezillon46336962018-04-22 20:35:14 +0200885void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
886 struct sg_table *sgt, enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000887{
888 if (sgt->orig_nents) {
889 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
890 sg_free_table(sgt);
891 }
892}
893
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200894static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000895{
Mark Brown99adef32014-01-16 12:22:43 +0000896 struct device *tx_dev, *rx_dev;
897 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000898 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000899
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200900 if (!ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000901 return 0;
902
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200903 if (ctlr->dma_tx)
904 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800905 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200906 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800907
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200908 if (ctlr->dma_rx)
909 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800910 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200911 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000912
913 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200914 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000915 continue;
916
917 if (xfer->tx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200918 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000919 (void *)xfer->tx_buf, xfer->len,
920 DMA_TO_DEVICE);
921 if (ret != 0)
922 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000923 }
924
925 if (xfer->rx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200926 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000927 xfer->rx_buf, xfer->len,
928 DMA_FROM_DEVICE);
929 if (ret != 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200930 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000931 DMA_TO_DEVICE);
932 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000933 }
934 }
935 }
936
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200937 ctlr->cur_msg_mapped = true;
Mark Brown99adef32014-01-16 12:22:43 +0000938
939 return 0;
940}
941
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200942static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000943{
944 struct spi_transfer *xfer;
945 struct device *tx_dev, *rx_dev;
946
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200947 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000948 return 0;
949
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200950 if (ctlr->dma_tx)
951 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800952 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200953 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800954
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200955 if (ctlr->dma_rx)
956 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800957 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200958 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000959
960 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200961 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000962 continue;
963
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200964 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
965 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000966 }
967
968 return 0;
969}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200970#else /* !CONFIG_HAS_DMA */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200971static inline int __spi_map_msg(struct spi_controller *ctlr,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200972 struct spi_message *msg)
973{
974 return 0;
975}
976
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200977static inline int __spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000978 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200979{
980 return 0;
981}
982#endif /* !CONFIG_HAS_DMA */
983
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200984static inline int spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000985 struct spi_message *msg)
986{
987 struct spi_transfer *xfer;
988
989 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
990 /*
991 * Restore the original value of tx_buf or rx_buf if they are
992 * NULL.
993 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200994 if (xfer->tx_buf == ctlr->dummy_tx)
Martin Sperl4b786452015-05-25 10:13:10 +0000995 xfer->tx_buf = NULL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200996 if (xfer->rx_buf == ctlr->dummy_rx)
Martin Sperl4b786452015-05-25 10:13:10 +0000997 xfer->rx_buf = NULL;
998 }
999
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001000 return __spi_unmap_msg(ctlr, msg);
Martin Sperl4b786452015-05-25 10:13:10 +00001001}
1002
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001003static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001004{
1005 struct spi_transfer *xfer;
1006 void *tmp;
1007 unsigned int max_tx, max_rx;
1008
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001009 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001010 max_tx = 0;
1011 max_rx = 0;
1012
1013 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001014 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001015 !xfer->tx_buf)
1016 max_tx = max(xfer->len, max_tx);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001017 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001018 !xfer->rx_buf)
1019 max_rx = max(xfer->len, max_rx);
1020 }
1021
1022 if (max_tx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001023 tmp = krealloc(ctlr->dummy_tx, max_tx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001024 GFP_KERNEL | GFP_DMA);
1025 if (!tmp)
1026 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001027 ctlr->dummy_tx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001028 memset(tmp, 0, max_tx);
1029 }
1030
1031 if (max_rx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001032 tmp = krealloc(ctlr->dummy_rx, max_rx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001033 GFP_KERNEL | GFP_DMA);
1034 if (!tmp)
1035 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001036 ctlr->dummy_rx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001037 }
1038
1039 if (max_tx || max_rx) {
1040 list_for_each_entry(xfer, &msg->transfers,
1041 transfer_list) {
1042 if (!xfer->tx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001043 xfer->tx_buf = ctlr->dummy_tx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001044 if (!xfer->rx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001045 xfer->rx_buf = ctlr->dummy_rx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001046 }
1047 }
1048 }
1049
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001050 return __spi_map_msg(ctlr, msg);
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001051}
Mark Brown99adef32014-01-16 12:22:43 +00001052
Lubomir Rintel810923f2018-11-13 11:22:24 +01001053static int spi_transfer_wait(struct spi_controller *ctlr,
1054 struct spi_message *msg,
1055 struct spi_transfer *xfer)
1056{
1057 struct spi_statistics *statm = &ctlr->statistics;
1058 struct spi_statistics *stats = &msg->spi->statistics;
1059 unsigned long long ms = 1;
1060
1061 if (spi_controller_is_slave(ctlr)) {
1062 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1063 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1064 return -EINTR;
1065 }
1066 } else {
1067 ms = 8LL * 1000LL * xfer->len;
1068 do_div(ms, xfer->speed_hz);
1069 ms += ms + 200; /* some tolerance */
1070
1071 if (ms > UINT_MAX)
1072 ms = UINT_MAX;
1073
1074 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1075 msecs_to_jiffies(ms));
1076
1077 if (ms == 0) {
1078 SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1079 SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1080 dev_err(&msg->spi->dev,
1081 "SPI transfer timed out\n");
1082 return -ETIMEDOUT;
1083 }
1084 }
1085
1086 return 0;
1087}
1088
Mark Brownb1589352013-10-05 11:50:40 +01001089/*
1090 * spi_transfer_one_message - Default implementation of transfer_one_message()
1091 *
1092 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -07001093 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +01001094 * standard handling of delays and chip select management.
1095 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001096static int spi_transfer_one_message(struct spi_controller *ctlr,
Mark Brownb1589352013-10-05 11:50:40 +01001097 struct spi_message *msg)
1098{
1099 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +01001100 bool keep_cs = false;
1101 int ret = 0;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001102 struct spi_statistics *statm = &ctlr->statistics;
Martin Sperleca2ebc2015-06-22 13:00:36 +00001103 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +01001104
1105 spi_set_cs(msg->spi, true);
1106
Martin Sperleca2ebc2015-06-22 13:00:36 +00001107 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1108 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1109
Mark Brownb1589352013-10-05 11:50:40 +01001110 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1111 trace_spi_transfer_start(msg, xfer);
1112
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001113 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1114 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
Martin Sperleca2ebc2015-06-22 13:00:36 +00001115
Mark Brown38ec10f2014-08-16 16:27:41 +01001116 if (xfer->tx_buf || xfer->rx_buf) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001117 reinit_completion(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001118
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001119 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
Mark Brown38ec10f2014-08-16 16:27:41 +01001120 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001121 SPI_STATISTICS_INCREMENT_FIELD(statm,
1122 errors);
1123 SPI_STATISTICS_INCREMENT_FIELD(stats,
1124 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001125 dev_err(&msg->spi->dev,
1126 "SPI transfer failed: %d\n", ret);
1127 goto out;
1128 }
Mark Brownb1589352013-10-05 11:50:40 +01001129
Mark Brownd57e7962018-11-15 16:08:32 -08001130 if (ret > 0) {
1131 ret = spi_transfer_wait(ctlr, msg, xfer);
1132 if (ret < 0)
1133 msg->status = ret;
1134 }
Mark Brown38ec10f2014-08-16 16:27:41 +01001135 } else {
1136 if (xfer->len)
1137 dev_err(&msg->spi->dev,
1138 "Bufferless transfer has length %u\n",
1139 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001140 }
Mark Brownb1589352013-10-05 11:50:40 +01001141
1142 trace_spi_transfer_stop(msg, xfer);
1143
1144 if (msg->status != -EINPROGRESS)
1145 goto out;
1146
Daniel Kurtz8244bd32016-10-07 18:55:47 +08001147 if (xfer->delay_usecs) {
1148 u16 us = xfer->delay_usecs;
1149
1150 if (us <= 10)
1151 udelay(us);
1152 else
1153 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1154 }
Mark Brownb1589352013-10-05 11:50:40 +01001155
1156 if (xfer->cs_change) {
1157 if (list_is_last(&xfer->transfer_list,
1158 &msg->transfers)) {
1159 keep_cs = true;
1160 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001161 spi_set_cs(msg->spi, false);
1162 udelay(10);
1163 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001164 }
1165 }
1166
1167 msg->actual_length += xfer->len;
1168 }
1169
1170out:
1171 if (ret != 0 || !keep_cs)
1172 spi_set_cs(msg->spi, false);
1173
1174 if (msg->status == -EINPROGRESS)
1175 msg->status = ret;
1176
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001177 if (msg->status && ctlr->handle_err)
1178 ctlr->handle_err(ctlr, msg);
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001179
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001180 spi_res_release(ctlr, msg);
Martin Sperld780c372015-12-14 15:20:18 +00001181
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001182 spi_finalize_current_message(ctlr);
Mark Brownb1589352013-10-05 11:50:40 +01001183
1184 return ret;
1185}
1186
1187/**
1188 * spi_finalize_current_transfer - report completion of a transfer
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001189 * @ctlr: the controller reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001190 *
1191 * Called by SPI drivers using the core transfer_one_message()
1192 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001193 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001194 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001195void spi_finalize_current_transfer(struct spi_controller *ctlr)
Mark Brownb1589352013-10-05 11:50:40 +01001196{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001197 complete(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001198}
1199EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1200
Linus Walleijffbbdd212012-02-22 10:05:38 +01001201/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001202 * __spi_pump_messages - function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001203 * @ctlr: controller to process queue for
Mark Brownfc9e0f72014-12-10 13:46:33 +00001204 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001205 *
1206 * This function checks if there is any spi message in the queue that
1207 * needs processing and if so call out to the driver to initialize hardware
1208 * and transfer each message.
1209 *
Mark Brown0461a412014-12-09 21:38:05 +00001210 * Note that it is called both from the kthread itself and also from
1211 * inside spi_sync(); the queue extraction handling at the top of the
1212 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001213 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001214static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001215{
Linus Walleijffbbdd212012-02-22 10:05:38 +01001216 unsigned long flags;
1217 bool was_busy = false;
1218 int ret;
1219
Mark Brown983aee52014-12-09 19:46:56 +00001220 /* Lock queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001221 spin_lock_irqsave(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001222
1223 /* Make sure we are not already running a message */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001224 if (ctlr->cur_msg) {
1225 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001226 return;
1227 }
1228
Mark Brownf0125f12019-01-23 17:29:53 +00001229 /* If another context is idling the device then defer */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001230 if (ctlr->idling) {
1231 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1232 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001233 return;
1234 }
1235
Mark Brown983aee52014-12-09 19:46:56 +00001236 /* Check if the queue is idle */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001237 if (list_empty(&ctlr->queue) || !ctlr->running) {
1238 if (!ctlr->busy) {
1239 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -07001240 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001241 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001242
Mark Brownf0125f12019-01-23 17:29:53 +00001243 /* Only do teardown in the thread */
1244 if (!in_kthread) {
1245 kthread_queue_work(&ctlr->kworker,
1246 &ctlr->pump_messages);
1247 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1248 return;
1249 }
1250
1251 ctlr->busy = false;
1252 ctlr->idling = true;
1253 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1254
1255 kfree(ctlr->dummy_rx);
1256 ctlr->dummy_rx = NULL;
1257 kfree(ctlr->dummy_tx);
1258 ctlr->dummy_tx = NULL;
1259 if (ctlr->unprepare_transfer_hardware &&
1260 ctlr->unprepare_transfer_hardware(ctlr))
1261 dev_err(&ctlr->dev,
1262 "failed to unprepare transfer hardware\n");
1263 if (ctlr->auto_runtime_pm) {
1264 pm_runtime_mark_last_busy(ctlr->dev.parent);
1265 pm_runtime_put_autosuspend(ctlr->dev.parent);
1266 }
1267 trace_spi_controller_idle(ctlr);
1268
1269 spin_lock_irqsave(&ctlr->queue_lock, flags);
1270 ctlr->idling = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001271 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001272 return;
1273 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001274
Linus Walleijffbbdd212012-02-22 10:05:38 +01001275 /* Extract head of queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001276 ctlr->cur_msg =
1277 list_first_entry(&ctlr->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001278
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001279 list_del_init(&ctlr->cur_msg->queue);
1280 if (ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001281 was_busy = true;
1282 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001283 ctlr->busy = true;
1284 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001285
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001286 mutex_lock(&ctlr->io_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001287
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001288 if (!was_busy && ctlr->auto_runtime_pm) {
1289 ret = pm_runtime_get_sync(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001290 if (ret < 0) {
Tony Lindgren7e48e232018-05-18 10:30:07 -07001291 pm_runtime_put_noidle(ctlr->dev.parent);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001292 dev_err(&ctlr->dev, "Failed to power device: %d\n",
Mark Brown49834de2013-07-28 14:47:02 +01001293 ret);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001294 mutex_unlock(&ctlr->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001295 return;
1296 }
1297 }
1298
Mark Brown56ec1972013-10-07 19:33:53 +01001299 if (!was_busy)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001300 trace_spi_controller_busy(ctlr);
Mark Brown56ec1972013-10-07 19:33:53 +01001301
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001302 if (!was_busy && ctlr->prepare_transfer_hardware) {
1303 ret = ctlr->prepare_transfer_hardware(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001304 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001305 dev_err(&ctlr->dev,
Linus Walleijffbbdd212012-02-22 10:05:38 +01001306 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001307
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001308 if (ctlr->auto_runtime_pm)
1309 pm_runtime_put(ctlr->dev.parent);
1310 mutex_unlock(&ctlr->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001311 return;
1312 }
1313 }
1314
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001315 trace_spi_message_start(ctlr->cur_msg);
Mark Brown56ec1972013-10-07 19:33:53 +01001316
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001317 if (ctlr->prepare_message) {
1318 ret = ctlr->prepare_message(ctlr, ctlr->cur_msg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001319 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001320 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1321 ret);
1322 ctlr->cur_msg->status = ret;
1323 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001324 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001325 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001326 ctlr->cur_msg_prepared = true;
Mark Brown2841a5f2013-10-05 00:23:12 +01001327 }
1328
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001329 ret = spi_map_msg(ctlr, ctlr->cur_msg);
Mark Brown99adef32014-01-16 12:22:43 +00001330 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001331 ctlr->cur_msg->status = ret;
1332 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001333 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001334 }
1335
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001336 ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001337 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001338 dev_err(&ctlr->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001339 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001340 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001341 }
Jon Hunter49023d22016-03-08 12:28:20 +00001342
1343out:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001344 mutex_unlock(&ctlr->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001345
1346 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001347 if (!ret)
1348 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001349}
1350
Mark Brownfc9e0f72014-12-10 13:46:33 +00001351/**
1352 * spi_pump_messages - kthread work function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001353 * @work: pointer to kthread work struct contained in the controller struct
Mark Brownfc9e0f72014-12-10 13:46:33 +00001354 */
1355static void spi_pump_messages(struct kthread_work *work)
1356{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001357 struct spi_controller *ctlr =
1358 container_of(work, struct spi_controller, pump_messages);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001359
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001360 __spi_pump_messages(ctlr, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001361}
1362
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001363static int spi_init_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001364{
1365 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1366
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001367 ctlr->running = false;
1368 ctlr->busy = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001369
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001370 kthread_init_worker(&ctlr->kworker);
1371 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1372 "%s", dev_name(&ctlr->dev));
1373 if (IS_ERR(ctlr->kworker_task)) {
1374 dev_err(&ctlr->dev, "failed to create message pump task\n");
1375 return PTR_ERR(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001376 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001377 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
Mark Brownf0125f12019-01-23 17:29:53 +00001378
Linus Walleijffbbdd212012-02-22 10:05:38 +01001379 /*
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001380 * Controller config will indicate if this controller should run the
Linus Walleijffbbdd212012-02-22 10:05:38 +01001381 * message pump with high (realtime) priority to reduce the transfer
1382 * latency on the bus by minimising the delay between a transfer
1383 * request and the scheduling of the message pump thread. Without this
1384 * setting the message pump thread will remain at default priority.
1385 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001386 if (ctlr->rt) {
1387 dev_info(&ctlr->dev,
Linus Walleijffbbdd212012-02-22 10:05:38 +01001388 "will run message pump with realtime priority\n");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001389 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001390 }
1391
1392 return 0;
1393}
1394
1395/**
1396 * spi_get_next_queued_message() - called by driver to check for queued
1397 * messages
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001398 * @ctlr: the controller to check for queued messages
Linus Walleijffbbdd212012-02-22 10:05:38 +01001399 *
1400 * If there are more messages in the queue, the next message is returned from
1401 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001402 *
1403 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001404 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001405struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001406{
1407 struct spi_message *next;
1408 unsigned long flags;
1409
1410 /* get a pointer to the next message, if any */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001411 spin_lock_irqsave(&ctlr->queue_lock, flags);
1412 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
Axel Lin1cfd97f2014-01-02 15:16:40 +08001413 queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001414 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001415
1416 return next;
1417}
1418EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1419
1420/**
1421 * spi_finalize_current_message() - the current message is complete
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001422 * @ctlr: the controller to return the message to
Linus Walleijffbbdd212012-02-22 10:05:38 +01001423 *
1424 * Called by the driver to notify the core that the message in the front of the
1425 * queue is complete and can be removed from the queue.
1426 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001427void spi_finalize_current_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001428{
1429 struct spi_message *mesg;
1430 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001431 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001432
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001433 spin_lock_irqsave(&ctlr->queue_lock, flags);
1434 mesg = ctlr->cur_msg;
1435 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001436
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001437 spi_unmap_msg(ctlr, mesg);
Mark Brown99adef32014-01-16 12:22:43 +00001438
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001439 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1440 ret = ctlr->unprepare_message(ctlr, mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001441 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001442 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1443 ret);
Mark Brown2841a5f2013-10-05 00:23:12 +01001444 }
1445 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001446
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001447 spin_lock_irqsave(&ctlr->queue_lock, flags);
1448 ctlr->cur_msg = NULL;
1449 ctlr->cur_msg_prepared = false;
Mark Brownf0125f12019-01-23 17:29:53 +00001450 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001451 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001452
1453 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001454
Linus Walleijffbbdd212012-02-22 10:05:38 +01001455 mesg->state = NULL;
1456 if (mesg->complete)
1457 mesg->complete(mesg->context);
1458}
1459EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1460
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001461static int spi_start_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001462{
1463 unsigned long flags;
1464
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001465 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001466
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001467 if (ctlr->running || ctlr->busy) {
1468 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001469 return -EBUSY;
1470 }
1471
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001472 ctlr->running = true;
1473 ctlr->cur_msg = NULL;
1474 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001475
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001476 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001477
1478 return 0;
1479}
1480
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001481static int spi_stop_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001482{
1483 unsigned long flags;
1484 unsigned limit = 500;
1485 int ret = 0;
1486
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001487 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001488
1489 /*
1490 * This is a bit lame, but is optimized for the common execution path.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001491 * A wait_queue on the ctlr->busy could be used, but then the common
Linus Walleijffbbdd212012-02-22 10:05:38 +01001492 * execution path (pump_messages) would be required to call wake_up or
1493 * friends on every SPI message. Do this instead.
1494 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001495 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1496 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001497 usleep_range(10000, 11000);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001498 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001499 }
1500
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001501 if (!list_empty(&ctlr->queue) || ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001502 ret = -EBUSY;
1503 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001504 ctlr->running = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001505
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001506 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001507
1508 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001509 dev_warn(&ctlr->dev, "could not stop message queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001510 return ret;
1511 }
1512 return ret;
1513}
1514
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001515static int spi_destroy_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001516{
1517 int ret;
1518
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001519 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001520
1521 /*
Petr Mladek39891442016-10-11 13:55:20 -07001522 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001523 * If the reason that stop_queue timed out is that the work will never
1524 * finish, then it does no good to call flush/stop thread, so
1525 * return anyway.
1526 */
1527 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001528 dev_err(&ctlr->dev, "problem destroying queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001529 return ret;
1530 }
1531
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001532 kthread_flush_worker(&ctlr->kworker);
1533 kthread_stop(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001534
1535 return 0;
1536}
1537
Mark Brown0461a412014-12-09 21:38:05 +00001538static int __spi_queued_transfer(struct spi_device *spi,
1539 struct spi_message *msg,
1540 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001541{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001542 struct spi_controller *ctlr = spi->controller;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001543 unsigned long flags;
1544
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001545 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001546
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001547 if (!ctlr->running) {
1548 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001549 return -ESHUTDOWN;
1550 }
1551 msg->actual_length = 0;
1552 msg->status = -EINPROGRESS;
1553
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001554 list_add_tail(&msg->queue, &ctlr->queue);
Mark Brownf0125f12019-01-23 17:29:53 +00001555 if (!ctlr->busy && need_pump)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001556 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001557
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001558 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001559 return 0;
1560}
1561
Mark Brown0461a412014-12-09 21:38:05 +00001562/**
1563 * spi_queued_transfer - transfer function for queued transfers
1564 * @spi: spi device which is requesting transfer
1565 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001566 *
1567 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001568 */
1569static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1570{
1571 return __spi_queued_transfer(spi, msg, true);
1572}
1573
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001574static int spi_controller_initialize_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001575{
1576 int ret;
1577
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001578 ctlr->transfer = spi_queued_transfer;
1579 if (!ctlr->transfer_one_message)
1580 ctlr->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001581
1582 /* Initialize and start queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001583 ret = spi_init_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001584 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001585 dev_err(&ctlr->dev, "problem initializing queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001586 goto err_init_queue;
1587 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001588 ctlr->queued = true;
1589 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001590 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001591 dev_err(&ctlr->dev, "problem starting queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001592 goto err_start_queue;
1593 }
1594
1595 return 0;
1596
1597err_start_queue:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001598 spi_destroy_queue(ctlr);
Mark Brownc3676d52014-05-01 10:47:52 -07001599err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001600 return ret;
1601}
1602
Boris Brezillon988f2592018-04-22 20:35:15 +02001603/**
1604 * spi_flush_queue - Send all pending messages in the queue from the callers'
1605 * context
1606 * @ctlr: controller to process queue for
1607 *
1608 * This should be used when one wants to ensure all pending messages have been
1609 * sent before doing something. Is used by the spi-mem code to make sure SPI
1610 * memory operations do not preempt regular SPI transfers that have been queued
1611 * before the spi-mem operation.
1612 */
1613void spi_flush_queue(struct spi_controller *ctlr)
1614{
1615 if (ctlr->transfer == spi_queued_transfer)
1616 __spi_pump_messages(ctlr, false);
1617}
1618
Linus Walleijffbbdd212012-02-22 10:05:38 +01001619/*-------------------------------------------------------------------------*/
1620
Andreas Larsson7cb94362012-12-04 15:09:38 +01001621#if defined(CONFIG_OF)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001622static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001623 struct device_node *nc)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001624{
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001625 u32 value;
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001626 int rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001627
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001628 /* Mode (clock phase/polarity/etc.) */
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001629 if (of_property_read_bool(nc, "spi-cpha"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001630 spi->mode |= SPI_CPHA;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001631 if (of_property_read_bool(nc, "spi-cpol"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001632 spi->mode |= SPI_CPOL;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001633 if (of_property_read_bool(nc, "spi-3wire"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001634 spi->mode |= SPI_3WIRE;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001635 if (of_property_read_bool(nc, "spi-lsb-first"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001636 spi->mode |= SPI_LSB_FIRST;
1637
Linus Walleijf3186dd2019-01-07 16:51:50 +01001638 /*
1639 * For descriptors associated with the device, polarity inversion is
1640 * handled in the gpiolib, so all chip selects are "active high" in
1641 * the logical sense, the gpiolib will invert the line if need be.
1642 */
1643 if (ctlr->use_gpio_descriptors)
1644 spi->mode |= SPI_CS_HIGH;
1645 else if (of_property_read_bool(nc, "spi-cs-high"))
1646 spi->mode |= SPI_CS_HIGH;
1647
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001648 /* Device DUAL/QUAD mode */
1649 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1650 switch (value) {
1651 case 1:
1652 break;
1653 case 2:
1654 spi->mode |= SPI_TX_DUAL;
1655 break;
1656 case 4:
1657 spi->mode |= SPI_TX_QUAD;
1658 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001659 case 8:
1660 spi->mode |= SPI_TX_OCTAL;
1661 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001662 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001663 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001664 "spi-tx-bus-width %d not supported\n",
1665 value);
1666 break;
1667 }
1668 }
1669
1670 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1671 switch (value) {
1672 case 1:
1673 break;
1674 case 2:
1675 spi->mode |= SPI_RX_DUAL;
1676 break;
1677 case 4:
1678 spi->mode |= SPI_RX_QUAD;
1679 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001680 case 8:
1681 spi->mode |= SPI_RX_OCTAL;
1682 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001683 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001684 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001685 "spi-rx-bus-width %d not supported\n",
1686 value);
1687 break;
1688 }
1689 }
1690
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001691 if (spi_controller_is_slave(ctlr)) {
Rob Herring194276b2018-12-05 13:50:41 -06001692 if (!of_node_name_eq(nc, "slave")) {
Rob Herring25c56c82017-07-18 16:43:31 -05001693 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
1694 nc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001695 return -EINVAL;
1696 }
1697 return 0;
1698 }
1699
1700 /* Device address */
1701 rc = of_property_read_u32(nc, "reg", &value);
1702 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05001703 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
1704 nc, rc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001705 return rc;
1706 }
1707 spi->chip_select = value;
1708
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001709 /* Device speed */
1710 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1711 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001712 dev_err(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05001713 "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001714 return rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001715 }
1716 spi->max_speed_hz = value;
1717
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001718 return 0;
1719}
1720
1721static struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001722of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001723{
1724 struct spi_device *spi;
1725 int rc;
1726
1727 /* Alloc an spi_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001728 spi = spi_alloc_device(ctlr);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001729 if (!spi) {
Rob Herring25c56c82017-07-18 16:43:31 -05001730 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001731 rc = -ENOMEM;
1732 goto err_out;
1733 }
1734
1735 /* Select device driver */
1736 rc = of_modalias_node(nc, spi->modalias,
1737 sizeof(spi->modalias));
1738 if (rc < 0) {
Rob Herring25c56c82017-07-18 16:43:31 -05001739 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001740 goto err_out;
1741 }
1742
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001743 rc = of_spi_parse_dt(ctlr, spi, nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001744 if (rc)
1745 goto err_out;
1746
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001747 /* Store a pointer to the node in the device structure */
1748 of_node_get(nc);
1749 spi->dev.of_node = nc;
1750
1751 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001752 rc = spi_add_device(spi);
1753 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05001754 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
Johan Hovold83241472017-01-30 17:47:05 +01001755 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001756 }
1757
1758 return spi;
1759
Johan Hovold83241472017-01-30 17:47:05 +01001760err_of_node_put:
1761 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001762err_out:
1763 spi_dev_put(spi);
1764 return ERR_PTR(rc);
1765}
1766
Grant Likelyd57a4282012-04-07 14:16:53 -06001767/**
1768 * of_register_spi_devices() - Register child devices onto the SPI bus
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001769 * @ctlr: Pointer to spi_controller device
Grant Likelyd57a4282012-04-07 14:16:53 -06001770 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001771 * Registers an spi_device for each child node of controller node which
1772 * represents a valid SPI slave.
Grant Likelyd57a4282012-04-07 14:16:53 -06001773 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001774static void of_register_spi_devices(struct spi_controller *ctlr)
Grant Likelyd57a4282012-04-07 14:16:53 -06001775{
1776 struct spi_device *spi;
1777 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001778
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001779 if (!ctlr->dev.of_node)
Grant Likelyd57a4282012-04-07 14:16:53 -06001780 return;
1781
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001782 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01001783 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1784 continue;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001785 spi = of_register_spi_device(ctlr, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001786 if (IS_ERR(spi)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001787 dev_warn(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05001788 "Failed to create SPI device for %pOF\n", nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001789 of_node_clear_flag(nc, OF_POPULATED);
1790 }
Grant Likelyd57a4282012-04-07 14:16:53 -06001791 }
1792}
1793#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001794static void of_register_spi_devices(struct spi_controller *ctlr) { }
Grant Likelyd57a4282012-04-07 14:16:53 -06001795#endif
1796
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001797#ifdef CONFIG_ACPI
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001798static void acpi_spi_parse_apple_properties(struct spi_device *spi)
1799{
1800 struct acpi_device *dev = ACPI_COMPANION(&spi->dev);
1801 const union acpi_object *obj;
1802
1803 if (!x86_apple_machine)
1804 return;
1805
1806 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
1807 && obj->buffer.length >= 4)
1808 spi->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
1809
1810 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
1811 && obj->buffer.length == 8)
1812 spi->bits_per_word = *(u64 *)obj->buffer.pointer;
1813
1814 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
1815 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
1816 spi->mode |= SPI_LSB_FIRST;
1817
1818 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
1819 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1820 spi->mode |= SPI_CPOL;
1821
1822 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
1823 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1824 spi->mode |= SPI_CPHA;
1825}
1826
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001827static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1828{
1829 struct spi_device *spi = data;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001830 struct spi_controller *ctlr = spi->controller;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001831
1832 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1833 struct acpi_resource_spi_serialbus *sb;
1834
1835 sb = &ares->data.spi_serial_bus;
1836 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Mika Westerberga0a90712016-02-08 17:14:28 +02001837 /*
1838 * ACPI DeviceSelection numbering is handled by the
1839 * host controller driver in Windows and can vary
1840 * from driver to driver. In Linux we always expect
1841 * 0 .. max - 1 so we need to ask the driver to
1842 * translate between the two schemes.
1843 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001844 if (ctlr->fw_translate_cs) {
1845 int cs = ctlr->fw_translate_cs(ctlr,
Mika Westerberga0a90712016-02-08 17:14:28 +02001846 sb->device_selection);
1847 if (cs < 0)
1848 return cs;
1849 spi->chip_select = cs;
1850 } else {
1851 spi->chip_select = sb->device_selection;
1852 }
1853
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001854 spi->max_speed_hz = sb->connection_speed;
1855
1856 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1857 spi->mode |= SPI_CPHA;
1858 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1859 spi->mode |= SPI_CPOL;
1860 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1861 spi->mode |= SPI_CS_HIGH;
1862 }
1863 } else if (spi->irq < 0) {
1864 struct resource r;
1865
1866 if (acpi_dev_resource_interrupt(ares, 0, &r))
1867 spi->irq = r.start;
1868 }
1869
1870 /* Always tell the ACPI core to skip this resource */
1871 return 1;
1872}
1873
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001874static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
Octavian Purdila7f244672016-07-08 19:13:11 +03001875 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001876{
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001877 struct list_head resource_list;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001878 struct spi_device *spi;
1879 int ret;
1880
Octavian Purdila7f244672016-07-08 19:13:11 +03001881 if (acpi_bus_get_status(adev) || !adev->status.present ||
1882 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001883 return AE_OK;
1884
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001885 spi = spi_alloc_device(ctlr);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001886 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001887 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001888 dev_name(&adev->dev));
1889 return AE_NO_MEMORY;
1890 }
1891
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001892 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001893 spi->irq = -1;
1894
1895 INIT_LIST_HEAD(&resource_list);
1896 ret = acpi_dev_get_resources(adev, &resource_list,
1897 acpi_spi_add_resource, spi);
1898 acpi_dev_free_resource_list(&resource_list);
1899
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001900 acpi_spi_parse_apple_properties(spi);
1901
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001902 if (ret < 0 || !spi->max_speed_hz) {
1903 spi_dev_put(spi);
1904 return AE_OK;
1905 }
1906
Dan O'Donovan0c6543f2017-02-05 16:30:14 +00001907 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
1908 sizeof(spi->modalias));
1909
Christophe RICARD33ada672015-12-23 23:25:35 +01001910 if (spi->irq < 0)
1911 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1912
Octavian Purdila7f244672016-07-08 19:13:11 +03001913 acpi_device_set_enumerated(adev);
1914
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001915 adev->power.flags.ignore_parent = true;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001916 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001917 adev->power.flags.ignore_parent = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001918 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001919 dev_name(&adev->dev));
1920 spi_dev_put(spi);
1921 }
1922
1923 return AE_OK;
1924}
1925
Octavian Purdila7f244672016-07-08 19:13:11 +03001926static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1927 void *data, void **return_value)
1928{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001929 struct spi_controller *ctlr = data;
Octavian Purdila7f244672016-07-08 19:13:11 +03001930 struct acpi_device *adev;
1931
1932 if (acpi_bus_get_device(handle, &adev))
1933 return AE_OK;
1934
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001935 return acpi_register_spi_device(ctlr, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03001936}
1937
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001938static void acpi_register_spi_devices(struct spi_controller *ctlr)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001939{
1940 acpi_status status;
1941 acpi_handle handle;
1942
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001943 handle = ACPI_HANDLE(ctlr->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001944 if (!handle)
1945 return;
1946
1947 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001948 acpi_spi_add_device, NULL, ctlr, NULL);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001949 if (ACPI_FAILURE(status))
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001950 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001951}
1952#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001953static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001954#endif /* CONFIG_ACPI */
1955
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001956static void spi_controller_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001957{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001958 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08001959
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001960 ctlr = container_of(dev, struct spi_controller, dev);
1961 kfree(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08001962}
1963
1964static struct class spi_master_class = {
1965 .name = "spi_master",
1966 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001967 .dev_release = spi_controller_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00001968 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08001969};
1970
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001971#ifdef CONFIG_SPI_SLAVE
1972/**
1973 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
1974 * controller
1975 * @spi: device used for the current transfer
1976 */
1977int spi_slave_abort(struct spi_device *spi)
1978{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001979 struct spi_controller *ctlr = spi->controller;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001980
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001981 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
1982 return ctlr->slave_abort(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001983
1984 return -ENOTSUPP;
1985}
1986EXPORT_SYMBOL_GPL(spi_slave_abort);
1987
1988static int match_true(struct device *dev, void *data)
1989{
1990 return 1;
1991}
1992
1993static ssize_t spi_slave_show(struct device *dev,
1994 struct device_attribute *attr, char *buf)
1995{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001996 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1997 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001998 struct device *child;
1999
2000 child = device_find_child(&ctlr->dev, NULL, match_true);
2001 return sprintf(buf, "%s\n",
2002 child ? to_spi_device(child)->modalias : NULL);
2003}
2004
2005static ssize_t spi_slave_store(struct device *dev,
2006 struct device_attribute *attr, const char *buf,
2007 size_t count)
2008{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002009 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2010 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002011 struct spi_device *spi;
2012 struct device *child;
2013 char name[32];
2014 int rc;
2015
2016 rc = sscanf(buf, "%31s", name);
2017 if (rc != 1 || !name[0])
2018 return -EINVAL;
2019
2020 child = device_find_child(&ctlr->dev, NULL, match_true);
2021 if (child) {
2022 /* Remove registered slave */
2023 device_unregister(child);
2024 put_device(child);
2025 }
2026
2027 if (strcmp(name, "(null)")) {
2028 /* Register new slave */
2029 spi = spi_alloc_device(ctlr);
2030 if (!spi)
2031 return -ENOMEM;
2032
2033 strlcpy(spi->modalias, name, sizeof(spi->modalias));
2034
2035 rc = spi_add_device(spi);
2036 if (rc) {
2037 spi_dev_put(spi);
2038 return rc;
2039 }
2040 }
2041
2042 return count;
2043}
2044
2045static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store);
2046
2047static struct attribute *spi_slave_attrs[] = {
2048 &dev_attr_slave.attr,
2049 NULL,
2050};
2051
2052static const struct attribute_group spi_slave_group = {
2053 .attrs = spi_slave_attrs,
2054};
2055
2056static const struct attribute_group *spi_slave_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002057 &spi_controller_statistics_group,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002058 &spi_slave_group,
2059 NULL,
2060};
2061
2062static struct class spi_slave_class = {
2063 .name = "spi_slave",
2064 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002065 .dev_release = spi_controller_release,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002066 .dev_groups = spi_slave_groups,
2067};
2068#else
2069extern struct class spi_slave_class; /* dummy */
2070#endif
David Brownell8ae12a02006-01-08 13:34:19 -08002071
2072/**
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002073 * __spi_alloc_controller - allocate an SPI master or slave controller
David Brownell8ae12a02006-01-08 13:34:19 -08002074 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07002075 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07002076 * memory is in the driver_data field of the returned device,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002077 * accessible with spi_controller_get_devdata().
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002078 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2079 * slave (true) controller
David Brownell33e34dc2007-05-08 00:32:21 -07002080 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002081 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002082 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002083 * only ones directly touching chip registers. It's how they allocate
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002084 * an spi_controller structure, prior to calling spi_register_controller().
David Brownell8ae12a02006-01-08 13:34:19 -08002085 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002086 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08002087 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002088 * The caller is responsible for assigning the bus number and initializing the
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002089 * controller's methods before calling spi_register_controller(); and (after
2090 * errors adding the device) calling spi_controller_put() to prevent a memory
2091 * leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002092 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002093 * Return: the SPI controller structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002094 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002095struct spi_controller *__spi_alloc_controller(struct device *dev,
2096 unsigned int size, bool slave)
David Brownell8ae12a02006-01-08 13:34:19 -08002097{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002098 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002099
David Brownell0c8684612006-01-08 13:34:25 -08002100 if (!dev)
2101 return NULL;
2102
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002103 ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL);
2104 if (!ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002105 return NULL;
2106
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002107 device_initialize(&ctlr->dev);
2108 ctlr->bus_num = -1;
2109 ctlr->num_chipselect = 1;
2110 ctlr->slave = slave;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002111 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002112 ctlr->dev.class = &spi_slave_class;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002113 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002114 ctlr->dev.class = &spi_master_class;
2115 ctlr->dev.parent = dev;
2116 pm_suspend_ignore_children(&ctlr->dev, true);
2117 spi_controller_set_devdata(ctlr, &ctlr[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08002118
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002119 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002120}
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002121EXPORT_SYMBOL_GPL(__spi_alloc_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002122
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002123#ifdef CONFIG_OF
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002124static int of_spi_register_master(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002125{
Grant Likelye80beb22013-02-12 17:48:37 +00002126 int nb, i, *cs;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002127 struct device_node *np = ctlr->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002128
2129 if (!np)
2130 return 0;
2131
2132 nb = of_gpio_named_count(np, "cs-gpios");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002133 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002134
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002135 /* Return error only for an incorrectly formed cs-gpios property */
2136 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002137 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002138 else if (nb < 0)
2139 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002140
Kees Cooka86854d2018-06-12 14:07:58 -07002141 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002142 GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002143 ctlr->cs_gpios = cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002144
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002145 if (!ctlr->cs_gpios)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002146 return -ENOMEM;
2147
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002148 for (i = 0; i < ctlr->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01002149 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002150
2151 for (i = 0; i < nb; i++)
2152 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2153
2154 return 0;
2155}
2156#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002157static int of_spi_register_master(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002158{
2159 return 0;
2160}
2161#endif
2162
Linus Walleijf3186dd2019-01-07 16:51:50 +01002163/**
2164 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2165 * @ctlr: The SPI master to grab GPIO descriptors for
2166 */
2167static int spi_get_gpio_descs(struct spi_controller *ctlr)
2168{
2169 int nb, i;
2170 struct gpio_desc **cs;
2171 struct device *dev = &ctlr->dev;
2172
2173 nb = gpiod_count(dev, "cs");
2174 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2175
2176 /* No GPIOs at all is fine, else return the error */
2177 if (nb == 0 || nb == -ENOENT)
2178 return 0;
2179 else if (nb < 0)
2180 return nb;
2181
2182 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
2183 GFP_KERNEL);
2184 if (!cs)
2185 return -ENOMEM;
2186 ctlr->cs_gpiods = cs;
2187
2188 for (i = 0; i < nb; i++) {
2189 /*
2190 * Most chipselects are active low, the inverted
2191 * semantics are handled by special quirks in gpiolib,
2192 * so initializing them GPIOD_OUT_LOW here means
2193 * "unasserted", in most cases this will drive the physical
2194 * line high.
2195 */
2196 cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
2197 GPIOD_OUT_LOW);
2198
2199 if (cs[i]) {
2200 /*
2201 * If we find a CS GPIO, name it after the device and
2202 * chip select line.
2203 */
2204 char *gpioname;
2205
2206 gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
2207 dev_name(dev), i);
2208 if (!gpioname)
2209 return -ENOMEM;
2210 gpiod_set_consumer_name(cs[i], gpioname);
2211 }
2212 }
2213
2214 return 0;
2215}
2216
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002217static int spi_controller_check_ops(struct spi_controller *ctlr)
2218{
2219 /*
Boris Brezillonb5932f52018-04-26 18:18:15 +02002220 * The controller may implement only the high-level SPI-memory like
2221 * operations if it does not support regular SPI transfers, and this is
2222 * valid use case.
2223 * If ->mem_ops is NULL, we request that at least one of the
2224 * ->transfer_xxx() method be implemented.
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002225 */
Boris Brezillonb5932f52018-04-26 18:18:15 +02002226 if (ctlr->mem_ops) {
2227 if (!ctlr->mem_ops->exec_op)
2228 return -EINVAL;
2229 } else if (!ctlr->transfer && !ctlr->transfer_one &&
2230 !ctlr->transfer_one_message) {
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002231 return -EINVAL;
Boris Brezillonb5932f52018-04-26 18:18:15 +02002232 }
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002233
2234 return 0;
2235}
2236
David Brownell8ae12a02006-01-08 13:34:19 -08002237/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002238 * spi_register_controller - register SPI master or slave controller
2239 * @ctlr: initialized master, originally from spi_alloc_master() or
2240 * spi_alloc_slave()
David Brownell33e34dc2007-05-08 00:32:21 -07002241 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002242 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002243 * SPI controllers connect to their drivers using some non-SPI bus,
David Brownell8ae12a02006-01-08 13:34:19 -08002244 * such as the platform bus. The final stage of probe() in that code
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002245 * includes calling spi_register_controller() to hook up to this SPI bus glue.
David Brownell8ae12a02006-01-08 13:34:19 -08002246 *
2247 * SPI controllers use board specific (often SOC specific) bus numbers,
2248 * and board-specific addressing for SPI devices combines those numbers
2249 * with chip select numbers. Since SPI does not directly support dynamic
2250 * device identification, boards need configuration tables telling which
2251 * chip is at which address.
2252 *
2253 * This must be called from context that can sleep. It returns zero on
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002254 * success, else a negative error code (dropping the controller's refcount).
David Brownell0c8684612006-01-08 13:34:25 -08002255 * After a successful return, the caller is responsible for calling
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002256 * spi_unregister_controller().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002257 *
2258 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002259 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002260int spi_register_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002261{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002262 struct device *dev = ctlr->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08002263 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08002264 int status = -ENODEV;
Lucas Stach42bdd702017-10-16 12:27:58 +02002265 int id, first_dynamic;
David Brownell8ae12a02006-01-08 13:34:19 -08002266
David Brownell0c8684612006-01-08 13:34:25 -08002267 if (!dev)
2268 return -ENODEV;
2269
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002270 /*
2271 * Make sure all necessary hooks are implemented before registering
2272 * the SPI controller.
2273 */
2274 status = spi_controller_check_ops(ctlr);
2275 if (status)
2276 return status;
2277
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002278 if (!spi_controller_is_slave(ctlr)) {
Linus Walleijf3186dd2019-01-07 16:51:50 +01002279 if (ctlr->use_gpio_descriptors) {
2280 status = spi_get_gpio_descs(ctlr);
2281 if (status)
2282 return status;
Linus Walleij2df201e2019-01-16 09:21:07 +01002283 /*
2284 * A controller using GPIO descriptors always
2285 * supports SPI_CS_HIGH if need be.
2286 */
2287 ctlr->mode_bits |= SPI_CS_HIGH;
Linus Walleijf3186dd2019-01-07 16:51:50 +01002288 } else {
2289 /* Legacy code path for GPIOs from DT */
2290 status = of_spi_register_master(ctlr);
2291 if (status)
2292 return status;
2293 }
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002294 }
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002295
David Brownell082c8cb2007-07-31 00:39:45 -07002296 /* even if it's just one always-selected device, there must
2297 * be at least one chipselect
2298 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002299 if (ctlr->num_chipselect == 0)
David Brownell082c8cb2007-07-31 00:39:45 -07002300 return -EINVAL;
Geert Uytterhoeven04b2d032018-08-21 11:53:03 +02002301 if (ctlr->bus_num >= 0) {
2302 /* devices with a fixed bus num must check-in with the num */
2303 mutex_lock(&board_lock);
2304 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2305 ctlr->bus_num + 1, GFP_KERNEL);
2306 mutex_unlock(&board_lock);
2307 if (WARN(id < 0, "couldn't get idr"))
2308 return id == -ENOSPC ? -EBUSY : id;
2309 ctlr->bus_num = id;
2310 } else if (ctlr->dev.of_node) {
2311 /* allocate dynamic bus number using Linux idr */
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302312 id = of_alias_get_id(ctlr->dev.of_node, "spi");
2313 if (id >= 0) {
2314 ctlr->bus_num = id;
2315 mutex_lock(&board_lock);
2316 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2317 ctlr->bus_num + 1, GFP_KERNEL);
2318 mutex_unlock(&board_lock);
2319 if (WARN(id < 0, "couldn't get idr"))
2320 return id == -ENOSPC ? -EBUSY : id;
2321 }
2322 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002323 if (ctlr->bus_num < 0) {
Lucas Stach42bdd702017-10-16 12:27:58 +02002324 first_dynamic = of_alias_get_highest_id("spi");
2325 if (first_dynamic < 0)
2326 first_dynamic = 0;
2327 else
2328 first_dynamic++;
2329
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302330 mutex_lock(&board_lock);
Lucas Stach42bdd702017-10-16 12:27:58 +02002331 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
2332 0, GFP_KERNEL);
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302333 mutex_unlock(&board_lock);
2334 if (WARN(id < 0, "couldn't get idr"))
2335 return id;
2336 ctlr->bus_num = id;
David Brownell8ae12a02006-01-08 13:34:19 -08002337 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002338 INIT_LIST_HEAD(&ctlr->queue);
2339 spin_lock_init(&ctlr->queue_lock);
2340 spin_lock_init(&ctlr->bus_lock_spinlock);
2341 mutex_init(&ctlr->bus_lock_mutex);
2342 mutex_init(&ctlr->io_mutex);
2343 ctlr->bus_lock_flag = 0;
2344 init_completion(&ctlr->xfer_completion);
2345 if (!ctlr->max_dma_len)
2346 ctlr->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002347
David Brownell8ae12a02006-01-08 13:34:19 -08002348 /* register the device, then userspace will see it.
2349 * registration fails if the bus ID is in use.
2350 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002351 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
2352 status = device_add(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302353 if (status < 0) {
2354 /* free bus id */
2355 mutex_lock(&board_lock);
2356 idr_remove(&spi_master_idr, ctlr->bus_num);
2357 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002358 goto done;
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302359 }
2360 dev_dbg(dev, "registered %s %s\n",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002361 spi_controller_is_slave(ctlr) ? "slave" : "master",
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302362 dev_name(&ctlr->dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002363
Boris Brezillonb5932f52018-04-26 18:18:15 +02002364 /*
2365 * If we're using a queued driver, start the queue. Note that we don't
2366 * need the queueing logic if the driver is only supporting high-level
2367 * memory operations.
2368 */
2369 if (ctlr->transfer) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002370 dev_info(dev, "controller is unqueued, this is deprecated\n");
Boris Brezillonb5932f52018-04-26 18:18:15 +02002371 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002372 status = spi_controller_initialize_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002373 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002374 device_del(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302375 /* free bus id */
2376 mutex_lock(&board_lock);
2377 idr_remove(&spi_master_idr, ctlr->bus_num);
2378 mutex_unlock(&board_lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002379 goto done;
2380 }
2381 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00002382 /* add statistics */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002383 spin_lock_init(&ctlr->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002384
Feng Tang2b9603a2010-08-02 15:52:15 +08002385 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002386 list_add_tail(&ctlr->list, &spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002387 list_for_each_entry(bi, &board_list, list)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002388 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +08002389 mutex_unlock(&board_lock);
2390
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002391 /* Register devices from the device tree and ACPI */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002392 of_register_spi_devices(ctlr);
2393 acpi_register_spi_devices(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002394done:
2395 return status;
2396}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002397EXPORT_SYMBOL_GPL(spi_register_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002398
Mark Brown666d5b42013-08-31 18:50:52 +01002399static void devm_spi_unregister(struct device *dev, void *res)
2400{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002401 spi_unregister_controller(*(struct spi_controller **)res);
Mark Brown666d5b42013-08-31 18:50:52 +01002402}
2403
2404/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002405 * devm_spi_register_controller - register managed SPI master or slave
2406 * controller
2407 * @dev: device managing SPI controller
2408 * @ctlr: initialized controller, originally from spi_alloc_master() or
2409 * spi_alloc_slave()
Mark Brown666d5b42013-08-31 18:50:52 +01002410 * Context: can sleep
2411 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002412 * Register a SPI device as with spi_register_controller() which will
Johan Hovold68b892f2017-10-30 11:35:26 +01002413 * automatically be unregistered and freed.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002414 *
2415 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01002416 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002417int devm_spi_register_controller(struct device *dev,
2418 struct spi_controller *ctlr)
Mark Brown666d5b42013-08-31 18:50:52 +01002419{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002420 struct spi_controller **ptr;
Mark Brown666d5b42013-08-31 18:50:52 +01002421 int ret;
2422
2423 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2424 if (!ptr)
2425 return -ENOMEM;
2426
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002427 ret = spi_register_controller(ctlr);
Stephen Warren4b928942013-11-21 16:11:15 -07002428 if (!ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002429 *ptr = ctlr;
Mark Brown666d5b42013-08-31 18:50:52 +01002430 devres_add(dev, ptr);
2431 } else {
2432 devres_free(ptr);
2433 }
2434
2435 return ret;
2436}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002437EXPORT_SYMBOL_GPL(devm_spi_register_controller);
Mark Brown666d5b42013-08-31 18:50:52 +01002438
David Lamparter34860082010-08-30 23:54:17 +02002439static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002440{
David Lamparter34860082010-08-30 23:54:17 +02002441 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002442 return 0;
2443}
2444
2445/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002446 * spi_unregister_controller - unregister SPI master or slave controller
2447 * @ctlr: the controller being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002448 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002449 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002450 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002451 * only ones directly touching chip registers.
2452 *
2453 * This must be called from context that can sleep.
Johan Hovold68b892f2017-10-30 11:35:26 +01002454 *
2455 * Note that this function also drops a reference to the controller.
David Brownell8ae12a02006-01-08 13:34:19 -08002456 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002457void spi_unregister_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002458{
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302459 struct spi_controller *found;
Johan Hovold67f7b272017-10-30 11:35:25 +01002460 int id = ctlr->bus_num;
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002461 int dummy;
2462
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302463 /* First make sure that this controller was ever added */
2464 mutex_lock(&board_lock);
Johan Hovold67f7b272017-10-30 11:35:25 +01002465 found = idr_find(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302466 mutex_unlock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002467 if (ctlr->queued) {
2468 if (spi_destroy_queue(ctlr))
2469 dev_err(&ctlr->dev, "queue remove failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002470 }
Feng Tang2b9603a2010-08-02 15:52:15 +08002471 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002472 list_del(&ctlr->list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002473 mutex_unlock(&board_lock);
2474
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002475 dummy = device_for_each_child(&ctlr->dev, NULL, __unregister);
2476 device_unregister(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302477 /* free bus id */
2478 mutex_lock(&board_lock);
Jarkko Nikula613bd1e2018-03-20 10:27:50 +02002479 if (found == ctlr)
2480 idr_remove(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302481 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002482}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002483EXPORT_SYMBOL_GPL(spi_unregister_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002484
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002485int spi_controller_suspend(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002486{
2487 int ret;
2488
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002489 /* Basically no-ops for non-queued controllers */
2490 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002491 return 0;
2492
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002493 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002494 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002495 dev_err(&ctlr->dev, "queue stop failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002496
2497 return ret;
2498}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002499EXPORT_SYMBOL_GPL(spi_controller_suspend);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002500
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002501int spi_controller_resume(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002502{
2503 int ret;
2504
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002505 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002506 return 0;
2507
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002508 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002509 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002510 dev_err(&ctlr->dev, "queue restart failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002511
2512 return ret;
2513}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002514EXPORT_SYMBOL_GPL(spi_controller_resume);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002515
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002516static int __spi_controller_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002517{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002518 struct spi_controller *ctlr;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002519 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002520
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002521 ctlr = container_of(dev, struct spi_controller, dev);
2522 return ctlr->bus_num == *bus_num;
Dave Young5ed2c832008-01-22 15:14:18 +08002523}
2524
David Brownell8ae12a02006-01-08 13:34:19 -08002525/**
2526 * spi_busnum_to_master - look up master associated with bus_num
2527 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002528 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002529 *
2530 * This call may be used with devices that are registered after
2531 * arch init time. It returns a refcounted pointer to the relevant
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002532 * spi_controller (which the caller must release), or NULL if there is
David Brownell8ae12a02006-01-08 13:34:19 -08002533 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002534 *
2535 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002536 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002537struct spi_controller *spi_busnum_to_master(u16 bus_num)
David Brownell8ae12a02006-01-08 13:34:19 -08002538{
Tony Jones49dce682007-10-16 01:27:48 -07002539 struct device *dev;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002540 struct spi_controller *ctlr = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002541
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002542 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002543 __spi_controller_match);
Dave Young5ed2c832008-01-22 15:14:18 +08002544 if (dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002545 ctlr = container_of(dev, struct spi_controller, dev);
Dave Young5ed2c832008-01-22 15:14:18 +08002546 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002547 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002548}
2549EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2550
Martin Sperld780c372015-12-14 15:20:18 +00002551/*-------------------------------------------------------------------------*/
2552
2553/* Core methods for SPI resource management */
2554
2555/**
2556 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2557 * during the processing of a spi_message while using
2558 * spi_transfer_one
2559 * @spi: the spi device for which we allocate memory
2560 * @release: the release code to execute for this resource
2561 * @size: size to alloc and return
2562 * @gfp: GFP allocation flags
2563 *
2564 * Return: the pointer to the allocated data
2565 *
2566 * This may get enhanced in the future to allocate from a memory pool
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002567 * of the @spi_device or @spi_controller to avoid repeated allocations.
Martin Sperld780c372015-12-14 15:20:18 +00002568 */
2569void *spi_res_alloc(struct spi_device *spi,
2570 spi_res_release_t release,
2571 size_t size, gfp_t gfp)
2572{
2573 struct spi_res *sres;
2574
2575 sres = kzalloc(sizeof(*sres) + size, gfp);
2576 if (!sres)
2577 return NULL;
2578
2579 INIT_LIST_HEAD(&sres->entry);
2580 sres->release = release;
2581
2582 return sres->data;
2583}
2584EXPORT_SYMBOL_GPL(spi_res_alloc);
2585
2586/**
2587 * spi_res_free - free an spi resource
2588 * @res: pointer to the custom data of a resource
2589 *
2590 */
2591void spi_res_free(void *res)
2592{
2593 struct spi_res *sres = container_of(res, struct spi_res, data);
2594
2595 if (!res)
2596 return;
2597
2598 WARN_ON(!list_empty(&sres->entry));
2599 kfree(sres);
2600}
2601EXPORT_SYMBOL_GPL(spi_res_free);
2602
2603/**
2604 * spi_res_add - add a spi_res to the spi_message
2605 * @message: the spi message
2606 * @res: the spi_resource
2607 */
2608void spi_res_add(struct spi_message *message, void *res)
2609{
2610 struct spi_res *sres = container_of(res, struct spi_res, data);
2611
2612 WARN_ON(!list_empty(&sres->entry));
2613 list_add_tail(&sres->entry, &message->resources);
2614}
2615EXPORT_SYMBOL_GPL(spi_res_add);
2616
2617/**
2618 * spi_res_release - release all spi resources for this message
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002619 * @ctlr: the @spi_controller
Martin Sperld780c372015-12-14 15:20:18 +00002620 * @message: the @spi_message
2621 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002622void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
Martin Sperld780c372015-12-14 15:20:18 +00002623{
2624 struct spi_res *res;
2625
2626 while (!list_empty(&message->resources)) {
2627 res = list_last_entry(&message->resources,
2628 struct spi_res, entry);
2629
2630 if (res->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002631 res->release(ctlr, message, res->data);
Martin Sperld780c372015-12-14 15:20:18 +00002632
2633 list_del(&res->entry);
2634
2635 kfree(res);
2636 }
2637}
2638EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002639
2640/*-------------------------------------------------------------------------*/
2641
Martin Sperl523baf5a2015-12-14 15:20:19 +00002642/* Core methods for spi_message alterations */
2643
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002644static void __spi_replace_transfers_release(struct spi_controller *ctlr,
Martin Sperl523baf5a2015-12-14 15:20:19 +00002645 struct spi_message *msg,
2646 void *res)
2647{
2648 struct spi_replaced_transfers *rxfer = res;
2649 size_t i;
2650
2651 /* call extra callback if requested */
2652 if (rxfer->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002653 rxfer->release(ctlr, msg, res);
Martin Sperl523baf5a2015-12-14 15:20:19 +00002654
2655 /* insert replaced transfers back into the message */
2656 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2657
2658 /* remove the formerly inserted entries */
2659 for (i = 0; i < rxfer->inserted; i++)
2660 list_del(&rxfer->inserted_transfers[i].transfer_list);
2661}
2662
2663/**
2664 * spi_replace_transfers - replace transfers with several transfers
2665 * and register change with spi_message.resources
2666 * @msg: the spi_message we work upon
2667 * @xfer_first: the first spi_transfer we want to replace
2668 * @remove: number of transfers to remove
2669 * @insert: the number of transfers we want to insert instead
2670 * @release: extra release code necessary in some circumstances
2671 * @extradatasize: extra data to allocate (with alignment guarantees
2672 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002673 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002674 *
2675 * Returns: pointer to @spi_replaced_transfers,
2676 * PTR_ERR(...) in case of errors.
2677 */
2678struct spi_replaced_transfers *spi_replace_transfers(
2679 struct spi_message *msg,
2680 struct spi_transfer *xfer_first,
2681 size_t remove,
2682 size_t insert,
2683 spi_replaced_release_t release,
2684 size_t extradatasize,
2685 gfp_t gfp)
2686{
2687 struct spi_replaced_transfers *rxfer;
2688 struct spi_transfer *xfer;
2689 size_t i;
2690
2691 /* allocate the structure using spi_res */
2692 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2693 insert * sizeof(struct spi_transfer)
2694 + sizeof(struct spi_replaced_transfers)
2695 + extradatasize,
2696 gfp);
2697 if (!rxfer)
2698 return ERR_PTR(-ENOMEM);
2699
2700 /* the release code to invoke before running the generic release */
2701 rxfer->release = release;
2702
2703 /* assign extradata */
2704 if (extradatasize)
2705 rxfer->extradata =
2706 &rxfer->inserted_transfers[insert];
2707
2708 /* init the replaced_transfers list */
2709 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2710
2711 /* assign the list_entry after which we should reinsert
2712 * the @replaced_transfers - it may be spi_message.messages!
2713 */
2714 rxfer->replaced_after = xfer_first->transfer_list.prev;
2715
2716 /* remove the requested number of transfers */
2717 for (i = 0; i < remove; i++) {
2718 /* if the entry after replaced_after it is msg->transfers
2719 * then we have been requested to remove more transfers
2720 * than are in the list
2721 */
2722 if (rxfer->replaced_after->next == &msg->transfers) {
2723 dev_err(&msg->spi->dev,
2724 "requested to remove more spi_transfers than are available\n");
2725 /* insert replaced transfers back into the message */
2726 list_splice(&rxfer->replaced_transfers,
2727 rxfer->replaced_after);
2728
2729 /* free the spi_replace_transfer structure */
2730 spi_res_free(rxfer);
2731
2732 /* and return with an error */
2733 return ERR_PTR(-EINVAL);
2734 }
2735
2736 /* remove the entry after replaced_after from list of
2737 * transfers and add it to list of replaced_transfers
2738 */
2739 list_move_tail(rxfer->replaced_after->next,
2740 &rxfer->replaced_transfers);
2741 }
2742
2743 /* create copy of the given xfer with identical settings
2744 * based on the first transfer to get removed
2745 */
2746 for (i = 0; i < insert; i++) {
2747 /* we need to run in reverse order */
2748 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2749
2750 /* copy all spi_transfer data */
2751 memcpy(xfer, xfer_first, sizeof(*xfer));
2752
2753 /* add to list */
2754 list_add(&xfer->transfer_list, rxfer->replaced_after);
2755
2756 /* clear cs_change and delay_usecs for all but the last */
2757 if (i) {
2758 xfer->cs_change = false;
2759 xfer->delay_usecs = 0;
2760 }
2761 }
2762
2763 /* set up inserted */
2764 rxfer->inserted = insert;
2765
2766 /* and register it with spi_res/spi_message */
2767 spi_res_add(msg, rxfer);
2768
2769 return rxfer;
2770}
2771EXPORT_SYMBOL_GPL(spi_replace_transfers);
2772
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002773static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
Fabio Estevam08933412016-02-14 13:33:50 -02002774 struct spi_message *msg,
2775 struct spi_transfer **xferp,
2776 size_t maxsize,
2777 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00002778{
2779 struct spi_transfer *xfer = *xferp, *xfers;
2780 struct spi_replaced_transfers *srt;
2781 size_t offset;
2782 size_t count, i;
2783
2784 /* warn once about this fact that we are splitting a transfer */
2785 dev_warn_once(&msg->spi->dev,
Fabio Estevam7d62f512016-02-17 15:42:27 -02002786 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
Martin Sperld9f12122015-12-14 15:20:20 +00002787 xfer->len, maxsize);
2788
2789 /* calculate how many we have to replace */
2790 count = DIV_ROUND_UP(xfer->len, maxsize);
2791
2792 /* create replacement */
2793 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03002794 if (IS_ERR(srt))
2795 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00002796 xfers = srt->inserted_transfers;
2797
2798 /* now handle each of those newly inserted spi_transfers
2799 * note that the replacements spi_transfers all are preset
2800 * to the same values as *xferp, so tx_buf, rx_buf and len
2801 * are all identical (as well as most others)
2802 * so we just have to fix up len and the pointers.
2803 *
2804 * this also includes support for the depreciated
2805 * spi_message.is_dma_mapped interface
2806 */
2807
2808 /* the first transfer just needs the length modified, so we
2809 * run it outside the loop
2810 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02002811 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00002812
2813 /* all the others need rx_buf/tx_buf also set */
2814 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2815 /* update rx_buf, tx_buf and dma */
2816 if (xfers[i].rx_buf)
2817 xfers[i].rx_buf += offset;
2818 if (xfers[i].rx_dma)
2819 xfers[i].rx_dma += offset;
2820 if (xfers[i].tx_buf)
2821 xfers[i].tx_buf += offset;
2822 if (xfers[i].tx_dma)
2823 xfers[i].tx_dma += offset;
2824
2825 /* update length */
2826 xfers[i].len = min(maxsize, xfers[i].len - offset);
2827 }
2828
2829 /* we set up xferp to the last entry we have inserted,
2830 * so that we skip those already split transfers
2831 */
2832 *xferp = &xfers[count - 1];
2833
2834 /* increment statistics counters */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002835 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperld9f12122015-12-14 15:20:20 +00002836 transfers_split_maxsize);
2837 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2838 transfers_split_maxsize);
2839
2840 return 0;
2841}
2842
2843/**
2844 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2845 * when an individual transfer exceeds a
2846 * certain size
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002847 * @ctlr: the @spi_controller for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09002848 * @msg: the @spi_message to transform
2849 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03002850 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00002851 *
2852 * Return: status of transformation
2853 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002854int spi_split_transfers_maxsize(struct spi_controller *ctlr,
Martin Sperld9f12122015-12-14 15:20:20 +00002855 struct spi_message *msg,
2856 size_t maxsize,
2857 gfp_t gfp)
2858{
2859 struct spi_transfer *xfer;
2860 int ret;
2861
2862 /* iterate over the transfer_list,
2863 * but note that xfer is advanced to the last transfer inserted
2864 * to avoid checking sizes again unnecessarily (also xfer does
2865 * potentiall belong to a different list by the time the
2866 * replacement has happened
2867 */
2868 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2869 if (xfer->len > maxsize) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002870 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
2871 maxsize, gfp);
Martin Sperld9f12122015-12-14 15:20:20 +00002872 if (ret)
2873 return ret;
2874 }
2875 }
2876
2877 return 0;
2878}
2879EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08002880
2881/*-------------------------------------------------------------------------*/
2882
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002883/* Core methods for SPI controller protocol drivers. Some of the
David Brownell7d077192009-06-17 16:26:03 -07002884 * other core methods are currently defined as inline functions.
2885 */
2886
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002887static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
2888 u8 bits_per_word)
Stefan Brüns63ab6452015-08-23 16:06:30 +02002889{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002890 if (ctlr->bits_per_word_mask) {
Stefan Brüns63ab6452015-08-23 16:06:30 +02002891 /* Only 32 bits fit in the mask */
2892 if (bits_per_word > 32)
2893 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002894 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
Stefan Brüns63ab6452015-08-23 16:06:30 +02002895 return -EINVAL;
2896 }
2897
2898 return 0;
2899}
2900
David Brownell7d077192009-06-17 16:26:03 -07002901/**
2902 * spi_setup - setup SPI mode and clock rate
2903 * @spi: the device whose settings are being modified
2904 * Context: can sleep, and no requests are queued to the device
2905 *
2906 * SPI protocol drivers may need to update the transfer mode if the
2907 * device doesn't work with its default. They may likewise need
2908 * to update clock rates or word sizes from initial values. This function
2909 * changes those settings, and must be called from a context that can sleep.
2910 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
2911 * effect the next time the device is selected and data is transferred to
2912 * or from it. When this function returns, the spi device is deselected.
2913 *
2914 * Note that this call will fail if the protocol driver specifies an option
2915 * that the underlying controller or its driver does not support. For
2916 * example, not all hardware supports wire transfers using nine bit words,
2917 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002918 *
2919 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07002920 */
2921int spi_setup(struct spi_device *spi)
2922{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002923 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002924 int status;
David Brownell7d077192009-06-17 16:26:03 -07002925
wangyuhangf477b7f2013-08-11 18:15:17 +08002926 /* check mode to prevent that DUAL and QUAD set at the same time
2927 */
2928 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
2929 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
2930 dev_err(&spi->dev,
2931 "setup: can not select dual and quad at the same time\n");
2932 return -EINVAL;
2933 }
2934 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
2935 */
2936 if ((spi->mode & SPI_3WIRE) && (spi->mode &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00002937 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
2938 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
wangyuhangf477b7f2013-08-11 18:15:17 +08002939 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07002940 /* help drivers fail *cleanly* when they need options
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002941 * that aren't supported with their current controller
David Lechnercbaa62e2018-09-12 19:39:18 -05002942 * SPI_CS_WORD has a fallback software implementation,
2943 * so it is ignored here.
David Brownelle7db06b2009-06-17 16:26:04 -07002944 */
David Lechnercbaa62e2018-09-12 19:39:18 -05002945 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002946 ugly_bits = bad_bits &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00002947 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
2948 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002949 if (ugly_bits) {
2950 dev_warn(&spi->dev,
2951 "setup: ignoring unsupported mode bits %x\n",
2952 ugly_bits);
2953 spi->mode &= ~ugly_bits;
2954 bad_bits &= ~ugly_bits;
2955 }
David Brownelle7db06b2009-06-17 16:26:04 -07002956 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02002957 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07002958 bad_bits);
2959 return -EINVAL;
2960 }
2961
David Brownell7d077192009-06-17 16:26:03 -07002962 if (!spi->bits_per_word)
2963 spi->bits_per_word = 8;
2964
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002965 status = __spi_validate_bits_per_word(spi->controller,
2966 spi->bits_per_word);
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002967 if (status)
2968 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02002969
Axel Lin052eb2d2014-02-10 00:08:05 +08002970 if (!spi->max_speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002971 spi->max_speed_hz = spi->controller->max_speed_hz;
Axel Lin052eb2d2014-02-10 00:08:05 +08002972
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002973 if (spi->controller->setup)
2974 status = spi->controller->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07002975
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05002976 spi_set_cs(spi, false);
2977
Jingoo Han5fe5f052013-10-14 10:31:51 +09002978 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 -07002979 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2980 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2981 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2982 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2983 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2984 spi->bits_per_word, spi->max_speed_hz,
2985 status);
2986
2987 return status;
2988}
2989EXPORT_SYMBOL_GPL(spi_setup);
2990
Mark Brown90808732013-11-13 23:44:15 +00002991static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002992{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002993 struct spi_controller *ctlr = spi->controller;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302994 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002995 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002996
Mark Brown24a00132013-07-10 15:05:40 +01002997 if (list_empty(&message->transfers))
2998 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01002999
David Lechnercbaa62e2018-09-12 19:39:18 -05003000 /* If an SPI controller does not support toggling the CS line on each
David Lechner71388b212018-09-18 12:08:48 -05003001 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
3002 * for the CS line, we can emulate the CS-per-word hardware function by
David Lechnercbaa62e2018-09-12 19:39:18 -05003003 * splitting transfers into one-word transfers and ensuring that
3004 * cs_change is set for each transfer.
3005 */
David Lechner71388b212018-09-18 12:08:48 -05003006 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
Linus Walleijf3186dd2019-01-07 16:51:50 +01003007 spi->cs_gpiod ||
David Lechner71388b212018-09-18 12:08:48 -05003008 gpio_is_valid(spi->cs_gpio))) {
David Lechnercbaa62e2018-09-12 19:39:18 -05003009 size_t maxsize;
3010 int ret;
3011
3012 maxsize = (spi->bits_per_word + 7) / 8;
3013
3014 /* spi_split_transfers_maxsize() requires message->spi */
3015 message->spi = spi;
3016
3017 ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
3018 GFP_KERNEL);
3019 if (ret)
3020 return ret;
3021
3022 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3023 /* don't change cs_change on the last entry in the list */
3024 if (list_is_last(&xfer->transfer_list, &message->transfers))
3025 break;
3026 xfer->cs_change = 1;
3027 }
3028 }
3029
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003030 /* Half-duplex links include original MicroWire, and ones with
3031 * only one data pin like SPI_3WIRE (switches direction) or where
3032 * either MOSI or MISO is missing. They can also be caused by
3033 * software limitations.
3034 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003035 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
3036 (spi->mode & SPI_3WIRE)) {
3037 unsigned flags = ctlr->flags;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003038
3039 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3040 if (xfer->rx_buf && xfer->tx_buf)
3041 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003042 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003043 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003044 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003045 return -EINVAL;
3046 }
3047 }
3048
Laxman Dewangane6811d12012-11-09 14:36:45 +05303049 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303050 * Set transfer bits_per_word and max speed as spi device default if
3051 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08003052 * Set transfer tx_nbits and rx_nbits as single transfer default
3053 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Jonas Bonnb7bb3672019-01-30 09:40:04 +01003054 * Ensure transfer word_delay is at least as long as that required by
3055 * device itself.
Laxman Dewangane6811d12012-11-09 14:36:45 +05303056 */
Martin Sperl77e80582015-11-27 12:31:09 +00003057 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303058 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05303059 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303060 if (!xfer->bits_per_word)
3061 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08003062
3063 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303064 xfer->speed_hz = spi->max_speed_hz;
Mark Brown7dc9fbc2015-08-20 11:52:18 -07003065 if (!xfer->speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003066 xfer->speed_hz = ctlr->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08003067
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003068 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
3069 xfer->speed_hz = ctlr->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02003070
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003071 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
Stefan Brüns63ab6452015-08-23 16:06:30 +02003072 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01003073
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003074 /*
3075 * SPI transfer length should be multiple of SPI word size
3076 * where SPI word size should be power-of-two multiple
3077 */
3078 if (xfer->bits_per_word <= 8)
3079 w_size = 1;
3080 else if (xfer->bits_per_word <= 16)
3081 w_size = 2;
3082 else
3083 w_size = 4;
3084
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003085 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09003086 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003087 return -EINVAL;
3088
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003089 if (xfer->speed_hz && ctlr->min_speed_hz &&
3090 xfer->speed_hz < ctlr->min_speed_hz)
Mark Browna2fd4f92013-07-10 14:57:26 +01003091 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08003092
3093 if (xfer->tx_buf && !xfer->tx_nbits)
3094 xfer->tx_nbits = SPI_NBITS_SINGLE;
3095 if (xfer->rx_buf && !xfer->rx_nbits)
3096 xfer->rx_nbits = SPI_NBITS_SINGLE;
3097 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01003098 * 1. check the value matches one of single, dual and quad
3099 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08003100 */
Sourav Poddardb90a442013-08-22 21:20:48 +05303101 if (xfer->tx_buf) {
3102 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
3103 xfer->tx_nbits != SPI_NBITS_DUAL &&
3104 xfer->tx_nbits != SPI_NBITS_QUAD)
3105 return -EINVAL;
3106 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
3107 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3108 return -EINVAL;
3109 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
3110 !(spi->mode & SPI_TX_QUAD))
3111 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303112 }
wangyuhangf477b7f2013-08-11 18:15:17 +08003113 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05303114 if (xfer->rx_buf) {
3115 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
3116 xfer->rx_nbits != SPI_NBITS_DUAL &&
3117 xfer->rx_nbits != SPI_NBITS_QUAD)
3118 return -EINVAL;
3119 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
3120 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3121 return -EINVAL;
3122 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
3123 !(spi->mode & SPI_RX_QUAD))
3124 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303125 }
Jonas Bonnb7bb3672019-01-30 09:40:04 +01003126
3127 if (xfer->word_delay_usecs < spi->word_delay_usecs)
3128 xfer->word_delay_usecs = spi->word_delay_usecs;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303129 }
3130
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003131 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00003132
3133 return 0;
3134}
3135
3136static int __spi_async(struct spi_device *spi, struct spi_message *message)
3137{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003138 struct spi_controller *ctlr = spi->controller;
Mark Brown90808732013-11-13 23:44:15 +00003139
Boris Brezillonb5932f52018-04-26 18:18:15 +02003140 /*
3141 * Some controllers do not support doing regular SPI transfers. Return
3142 * ENOTSUPP when this is the case.
3143 */
3144 if (!ctlr->transfer)
3145 return -ENOTSUPP;
3146
Mark Brown90808732013-11-13 23:44:15 +00003147 message->spi = spi;
3148
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003149 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003150 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3151
Mark Brown90808732013-11-13 23:44:15 +00003152 trace_spi_message_submit(message);
3153
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003154 return ctlr->transfer(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003155}
3156
David Brownell568d0692009-09-22 16:46:18 -07003157/**
3158 * spi_async - asynchronous SPI transfer
3159 * @spi: device with which data will be exchanged
3160 * @message: describes the data transfers, including completion callback
3161 * Context: any (irqs may be blocked, etc)
3162 *
3163 * This call may be used in_irq and other contexts which can't sleep,
3164 * as well as from task contexts which can sleep.
3165 *
3166 * The completion callback is invoked in a context which can't sleep.
3167 * Before that invocation, the value of message->status is undefined.
3168 * When the callback is issued, message->status holds either zero (to
3169 * indicate complete success) or a negative error code. After that
3170 * callback returns, the driver which issued the transfer request may
3171 * deallocate the associated memory; it's no longer in use by any SPI
3172 * core or controller driver code.
3173 *
3174 * Note that although all messages to a spi_device are handled in
3175 * FIFO order, messages may go to different devices in other orders.
3176 * Some device might be higher priority, or have various "hard" access
3177 * time requirements, for example.
3178 *
3179 * On detection of any fault during the transfer, processing of
3180 * the entire message is aborted, and the device is deselected.
3181 * Until returning from the associated message completion callback,
3182 * no other spi_message queued to that device will be processed.
3183 * (This rule applies equally to all the synchronous transfer calls,
3184 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003185 *
3186 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07003187 */
3188int spi_async(struct spi_device *spi, struct spi_message *message)
3189{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003190 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003191 int ret;
3192 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07003193
Mark Brown90808732013-11-13 23:44:15 +00003194 ret = __spi_validate(spi, message);
3195 if (ret != 0)
3196 return ret;
3197
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003198 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07003199
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003200 if (ctlr->bus_lock_flag)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003201 ret = -EBUSY;
3202 else
3203 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07003204
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003205 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003206
3207 return ret;
David Brownell568d0692009-09-22 16:46:18 -07003208}
3209EXPORT_SYMBOL_GPL(spi_async);
3210
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003211/**
3212 * spi_async_locked - version of spi_async with exclusive bus usage
3213 * @spi: device with which data will be exchanged
3214 * @message: describes the data transfers, including completion callback
3215 * Context: any (irqs may be blocked, etc)
3216 *
3217 * This call may be used in_irq and other contexts which can't sleep,
3218 * as well as from task contexts which can sleep.
3219 *
3220 * The completion callback is invoked in a context which can't sleep.
3221 * Before that invocation, the value of message->status is undefined.
3222 * When the callback is issued, message->status holds either zero (to
3223 * indicate complete success) or a negative error code. After that
3224 * callback returns, the driver which issued the transfer request may
3225 * deallocate the associated memory; it's no longer in use by any SPI
3226 * core or controller driver code.
3227 *
3228 * Note that although all messages to a spi_device are handled in
3229 * FIFO order, messages may go to different devices in other orders.
3230 * Some device might be higher priority, or have various "hard" access
3231 * time requirements, for example.
3232 *
3233 * On detection of any fault during the transfer, processing of
3234 * the entire message is aborted, and the device is deselected.
3235 * Until returning from the associated message completion callback,
3236 * no other spi_message queued to that device will be processed.
3237 * (This rule applies equally to all the synchronous transfer calls,
3238 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003239 *
3240 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003241 */
3242int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3243{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003244 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003245 int ret;
3246 unsigned long flags;
3247
Mark Brown90808732013-11-13 23:44:15 +00003248 ret = __spi_validate(spi, message);
3249 if (ret != 0)
3250 return ret;
3251
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003252 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003253
3254 ret = __spi_async(spi, message);
3255
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003256 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003257
3258 return ret;
3259
3260}
3261EXPORT_SYMBOL_GPL(spi_async_locked);
3262
David Brownell7d077192009-06-17 16:26:03 -07003263/*-------------------------------------------------------------------------*/
3264
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003265/* Utility methods for SPI protocol drivers, layered on
David Brownell7d077192009-06-17 16:26:03 -07003266 * top of the core. Some other utility methods are defined as
3267 * inline functions.
3268 */
3269
Andrew Morton5d870c82006-01-11 11:23:49 -08003270static void spi_complete(void *arg)
3271{
3272 complete(arg);
3273}
3274
Mark Brownef4d96e2016-07-21 23:53:31 +01003275static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003276{
3277 DECLARE_COMPLETION_ONSTACK(done);
3278 int status;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003279 struct spi_controller *ctlr = spi->controller;
Mark Brown0461a412014-12-09 21:38:05 +00003280 unsigned long flags;
3281
3282 status = __spi_validate(spi, message);
3283 if (status != 0)
3284 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003285
3286 message->complete = spi_complete;
3287 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00003288 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003289
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003290 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003291 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3292
Mark Brown0461a412014-12-09 21:38:05 +00003293 /* If we're not using the legacy transfer method then we will
3294 * try to transfer in the calling context so special case.
3295 * This code would be less tricky if we could remove the
3296 * support for driver implemented message queues.
3297 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003298 if (ctlr->transfer == spi_queued_transfer) {
3299 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003300
3301 trace_spi_message_submit(message);
3302
3303 status = __spi_queued_transfer(spi, message, false);
3304
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003305 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003306 } else {
3307 status = spi_async_locked(spi, message);
3308 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003309
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003310 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00003311 /* Push out the messages in the calling context if we
3312 * can.
3313 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003314 if (ctlr->transfer == spi_queued_transfer) {
3315 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperleca2ebc2015-06-22 13:00:36 +00003316 spi_sync_immediate);
3317 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3318 spi_sync_immediate);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003319 __spi_pump_messages(ctlr, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003320 }
Mark Brown0461a412014-12-09 21:38:05 +00003321
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003322 wait_for_completion(&done);
3323 status = message->status;
3324 }
3325 message->context = NULL;
3326 return status;
3327}
3328
David Brownell8ae12a02006-01-08 13:34:19 -08003329/**
3330 * spi_sync - blocking/synchronous SPI data transfers
3331 * @spi: device with which data will be exchanged
3332 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07003333 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003334 *
3335 * This call may only be used from a context that may sleep. The sleep
3336 * is non-interruptible, and has no timeout. Low-overhead controller
3337 * drivers may DMA directly into and out of the message buffers.
3338 *
3339 * Note that the SPI device's chip select is active during the message,
3340 * and then is normally disabled between messages. Drivers for some
3341 * frequently-used devices may want to minimize costs of selecting a chip,
3342 * by leaving it selected in anticipation that the next message will go
3343 * to the same chip. (That may increase power usage.)
3344 *
David Brownell0c8684612006-01-08 13:34:25 -08003345 * Also, the caller is guaranteeing that the memory associated with the
3346 * message will not be freed before this call returns.
3347 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003348 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003349 */
3350int spi_sync(struct spi_device *spi, struct spi_message *message)
3351{
Mark Brownef4d96e2016-07-21 23:53:31 +01003352 int ret;
3353
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003354 mutex_lock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003355 ret = __spi_sync(spi, message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003356 mutex_unlock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003357
3358 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08003359}
3360EXPORT_SYMBOL_GPL(spi_sync);
3361
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003362/**
3363 * spi_sync_locked - version of spi_sync with exclusive bus usage
3364 * @spi: device with which data will be exchanged
3365 * @message: describes the data transfers
3366 * Context: can sleep
3367 *
3368 * This call may only be used from a context that may sleep. The sleep
3369 * is non-interruptible, and has no timeout. Low-overhead controller
3370 * drivers may DMA directly into and out of the message buffers.
3371 *
3372 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003373 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003374 * be released by a spi_bus_unlock call when the exclusive access is over.
3375 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003376 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003377 */
3378int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3379{
Mark Brownef4d96e2016-07-21 23:53:31 +01003380 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003381}
3382EXPORT_SYMBOL_GPL(spi_sync_locked);
3383
3384/**
3385 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003386 * @ctlr: SPI bus master that should be locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003387 * Context: can sleep
3388 *
3389 * This call may only be used from a context that may sleep. The sleep
3390 * is non-interruptible, and has no timeout.
3391 *
3392 * This call should be used by drivers that require exclusive access to the
3393 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3394 * exclusive access is over. Data transfer must be done by spi_sync_locked
3395 * and spi_async_locked calls when the SPI bus lock is held.
3396 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003397 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003398 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003399int spi_bus_lock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003400{
3401 unsigned long flags;
3402
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003403 mutex_lock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003404
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003405 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3406 ctlr->bus_lock_flag = 1;
3407 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003408
3409 /* mutex remains locked until spi_bus_unlock is called */
3410
3411 return 0;
3412}
3413EXPORT_SYMBOL_GPL(spi_bus_lock);
3414
3415/**
3416 * spi_bus_unlock - release the lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003417 * @ctlr: SPI bus master that was locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003418 * Context: can sleep
3419 *
3420 * This call may only be used from a context that may sleep. The sleep
3421 * is non-interruptible, and has no timeout.
3422 *
3423 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3424 * call.
3425 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003426 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003427 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003428int spi_bus_unlock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003429{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003430 ctlr->bus_lock_flag = 0;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003431
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003432 mutex_unlock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003433
3434 return 0;
3435}
3436EXPORT_SYMBOL_GPL(spi_bus_unlock);
3437
David Brownella9948b62006-04-02 10:37:40 -08003438/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003439#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003440
3441static u8 *buf;
3442
3443/**
3444 * spi_write_then_read - SPI synchronous write followed by read
3445 * @spi: device with which data will be exchanged
3446 * @txbuf: data to be written (need not be dma-safe)
3447 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003448 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3449 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003450 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003451 *
3452 * This performs a half duplex MicroWire style transaction with the
3453 * device, sending txbuf and then reading rxbuf. The return value
3454 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003455 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003456 *
David Brownell0c8684612006-01-08 13:34:25 -08003457 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003458 * portable code should never use this for more than 32 bytes.
3459 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c8684612006-01-08 13:34:25 -08003460 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003461 *
3462 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003463 */
3464int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003465 const void *txbuf, unsigned n_tx,
3466 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003467{
David Brownell068f4072007-12-04 23:45:09 -08003468 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003469
3470 int status;
3471 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003472 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003473 u8 *local_buf;
3474
Mark Brownb3a223e2012-12-02 12:54:25 +09003475 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3476 * copying here, (as a pure convenience thing), but we can
3477 * keep heap costs out of the hot path unless someone else is
3478 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003479 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003480 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003481 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3482 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003483 if (!local_buf)
3484 return -ENOMEM;
3485 } else {
3486 local_buf = buf;
3487 }
David Brownell8ae12a02006-01-08 13:34:19 -08003488
Vitaly Wool8275c642006-01-08 13:34:28 -08003489 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003490 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003491 if (n_tx) {
3492 x[0].len = n_tx;
3493 spi_message_add_tail(&x[0], &message);
3494 }
3495 if (n_rx) {
3496 x[1].len = n_rx;
3497 spi_message_add_tail(&x[1], &message);
3498 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003499
David Brownell8ae12a02006-01-08 13:34:19 -08003500 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003501 x[0].tx_buf = local_buf;
3502 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003503
3504 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003505 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003506 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003507 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003508
David Brownellbdff5492009-04-13 14:39:57 -07003509 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003510 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003511 else
3512 kfree(local_buf);
3513
3514 return status;
3515}
3516EXPORT_SYMBOL_GPL(spi_write_then_read);
3517
3518/*-------------------------------------------------------------------------*/
3519
Marco Felsch5f143af2018-09-25 11:42:29 +02003520#if IS_ENABLED(CONFIG_OF)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003521static int __spi_of_device_match(struct device *dev, void *data)
3522{
3523 return dev->of_node == data;
3524}
3525
3526/* must call put_device() when done with returned spi_device device */
Marco Felsch5f143af2018-09-25 11:42:29 +02003527struct spi_device *of_find_spi_device_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003528{
3529 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3530 __spi_of_device_match);
3531 return dev ? to_spi_device(dev) : NULL;
3532}
Marco Felsch5f143af2018-09-25 11:42:29 +02003533EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
3534#endif /* IS_ENABLED(CONFIG_OF) */
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003535
Marco Felsch5f143af2018-09-25 11:42:29 +02003536#if IS_ENABLED(CONFIG_OF_DYNAMIC)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003537static int __spi_of_controller_match(struct device *dev, const void *data)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003538{
3539 return dev->of_node == data;
3540}
3541
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003542/* the spi controllers are not using spi_bus, so we find it with another way */
3543static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003544{
3545 struct device *dev;
3546
3547 dev = class_find_device(&spi_master_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003548 __spi_of_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003549 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3550 dev = class_find_device(&spi_slave_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003551 __spi_of_controller_match);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003552 if (!dev)
3553 return NULL;
3554
3555 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003556 return container_of(dev, struct spi_controller, dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003557}
3558
3559static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3560 void *arg)
3561{
3562 struct of_reconfig_data *rd = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003563 struct spi_controller *ctlr;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003564 struct spi_device *spi;
3565
3566 switch (of_reconfig_get_state_change(action, arg)) {
3567 case OF_RECONFIG_CHANGE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003568 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3569 if (ctlr == NULL)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003570 return NOTIFY_OK; /* not for us */
3571
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003572 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003573 put_device(&ctlr->dev);
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003574 return NOTIFY_OK;
3575 }
3576
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003577 spi = of_register_spi_device(ctlr, rd->dn);
3578 put_device(&ctlr->dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003579
3580 if (IS_ERR(spi)) {
Rob Herring25c56c82017-07-18 16:43:31 -05003581 pr_err("%s: failed to create for '%pOF'\n",
3582 __func__, rd->dn);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003583 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003584 return notifier_from_errno(PTR_ERR(spi));
3585 }
3586 break;
3587
3588 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003589 /* already depopulated? */
3590 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3591 return NOTIFY_OK;
3592
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003593 /* find our device by node */
3594 spi = of_find_spi_device_by_node(rd->dn);
3595 if (spi == NULL)
3596 return NOTIFY_OK; /* no? not meant for us */
3597
3598 /* unregister takes one ref away */
3599 spi_unregister_device(spi);
3600
3601 /* and put the reference of the find */
3602 put_device(&spi->dev);
3603 break;
3604 }
3605
3606 return NOTIFY_OK;
3607}
3608
3609static struct notifier_block spi_of_notifier = {
3610 .notifier_call = of_spi_notify,
3611};
3612#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3613extern struct notifier_block spi_of_notifier;
3614#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3615
Octavian Purdila7f244672016-07-08 19:13:11 +03003616#if IS_ENABLED(CONFIG_ACPI)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003617static int spi_acpi_controller_match(struct device *dev, const void *data)
Octavian Purdila7f244672016-07-08 19:13:11 +03003618{
3619 return ACPI_COMPANION(dev->parent) == data;
3620}
3621
3622static int spi_acpi_device_match(struct device *dev, void *data)
3623{
3624 return ACPI_COMPANION(dev) == data;
3625}
3626
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003627static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
Octavian Purdila7f244672016-07-08 19:13:11 +03003628{
3629 struct device *dev;
3630
3631 dev = class_find_device(&spi_master_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003632 spi_acpi_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003633 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3634 dev = class_find_device(&spi_slave_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003635 spi_acpi_controller_match);
Octavian Purdila7f244672016-07-08 19:13:11 +03003636 if (!dev)
3637 return NULL;
3638
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003639 return container_of(dev, struct spi_controller, dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003640}
3641
3642static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3643{
3644 struct device *dev;
3645
3646 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3647
3648 return dev ? to_spi_device(dev) : NULL;
3649}
3650
3651static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3652 void *arg)
3653{
3654 struct acpi_device *adev = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003655 struct spi_controller *ctlr;
Octavian Purdila7f244672016-07-08 19:13:11 +03003656 struct spi_device *spi;
3657
3658 switch (value) {
3659 case ACPI_RECONFIG_DEVICE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003660 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
3661 if (!ctlr)
Octavian Purdila7f244672016-07-08 19:13:11 +03003662 break;
3663
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003664 acpi_register_spi_device(ctlr, adev);
3665 put_device(&ctlr->dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003666 break;
3667 case ACPI_RECONFIG_DEVICE_REMOVE:
3668 if (!acpi_device_enumerated(adev))
3669 break;
3670
3671 spi = acpi_spi_find_device_by_adev(adev);
3672 if (!spi)
3673 break;
3674
3675 spi_unregister_device(spi);
3676 put_device(&spi->dev);
3677 break;
3678 }
3679
3680 return NOTIFY_OK;
3681}
3682
3683static struct notifier_block spi_acpi_notifier = {
3684 .notifier_call = acpi_spi_notify,
3685};
3686#else
3687extern struct notifier_block spi_acpi_notifier;
3688#endif
3689
David Brownell8ae12a02006-01-08 13:34:19 -08003690static int __init spi_init(void)
3691{
David Brownellb8852442006-01-08 13:34:23 -08003692 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08003693
Christoph Lametere94b1762006-12-06 20:33:17 -08003694 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08003695 if (!buf) {
3696 status = -ENOMEM;
3697 goto err0;
3698 }
3699
3700 status = bus_register(&spi_bus_type);
3701 if (status < 0)
3702 goto err1;
3703
3704 status = class_register(&spi_master_class);
3705 if (status < 0)
3706 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003707
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003708 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
3709 status = class_register(&spi_slave_class);
3710 if (status < 0)
3711 goto err3;
3712 }
3713
Fabio Estevam52677202014-11-26 20:13:57 -02003714 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003715 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03003716 if (IS_ENABLED(CONFIG_ACPI))
3717 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003718
David Brownell8ae12a02006-01-08 13:34:19 -08003719 return 0;
David Brownellb8852442006-01-08 13:34:23 -08003720
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003721err3:
3722 class_unregister(&spi_master_class);
David Brownellb8852442006-01-08 13:34:23 -08003723err2:
3724 bus_unregister(&spi_bus_type);
3725err1:
3726 kfree(buf);
3727 buf = NULL;
3728err0:
3729 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08003730}
David Brownellb8852442006-01-08 13:34:23 -08003731
David Brownell8ae12a02006-01-08 13:34:19 -08003732/* board_info is normally registered in arch_initcall(),
3733 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08003734 *
3735 * REVISIT only boardinfo really needs static linking. the rest (device and
3736 * driver registration) _could_ be dynamically linked (modular) ... costs
3737 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08003738 */
David Brownell673c0c02008-10-15 22:02:46 -07003739postcore_initcall(spi_init);
Mark Brownf0125f12019-01-23 17:29:53 +00003740