powerpc/mm: Drop the unnecessary region check
[linux-2.6-block.git] / drivers / spi / spi.c
CommitLineData
b445bfcb 1// SPDX-License-Identifier: GPL-2.0-or-later
787f4889
MB
2// SPI init/core code
3//
4// Copyright (C) 2005 David Brownell
5// Copyright (C) 2008 Secret Lab Technologies Ltd.
8ae12a0d 6
8ae12a0d
DB
7#include <linux/kernel.h>
8#include <linux/device.h>
9#include <linux/init.h>
10#include <linux/cache.h>
99adef31
MB
11#include <linux/dma-mapping.h>
12#include <linux/dmaengine.h>
94040828 13#include <linux/mutex.h>
2b7a32f7 14#include <linux/of_device.h>
d57a4282 15#include <linux/of_irq.h>
86be408b 16#include <linux/clk/clk-conf.h>
5a0e3ad6 17#include <linux/slab.h>
e0626e38 18#include <linux/mod_devicetable.h>
8ae12a0d 19#include <linux/spi/spi.h>
b5932f5c 20#include <linux/spi/spi-mem.h>
74317984 21#include <linux/of_gpio.h>
f3186dd8 22#include <linux/gpio/consumer.h>
3ae22e8c 23#include <linux/pm_runtime.h>
f48c767c 24#include <linux/pm_domain.h>
826cf175 25#include <linux/property.h>
025ed130 26#include <linux/export.h>
8bd75c77 27#include <linux/sched/rt.h>
ae7e81c0 28#include <uapi/linux/sched/types.h>
ffbbdd21
LW
29#include <linux/delay.h>
30#include <linux/kthread.h>
64bee4d2
MW
31#include <linux/ioport.h>
32#include <linux/acpi.h>
b1b8153c 33#include <linux/highmem.h>
9b61e302 34#include <linux/idr.h>
8a2e487e 35#include <linux/platform_data/x86/apple.h>
8ae12a0d 36
56ec1978
MB
37#define CREATE_TRACE_POINTS
38#include <trace/events/spi.h>
9b61e302 39
46336966
BB
40#include "internals.h"
41
9b61e302 42static DEFINE_IDR(spi_master_idr);
56ec1978 43
8ae12a0d
DB
44static void spidev_release(struct device *dev)
45{
0ffa0285 46 struct spi_device *spi = to_spi_device(dev);
8ae12a0d 47
8caab75f
GU
48 /* spi controllers may cleanup for released devices */
49 if (spi->controller->cleanup)
50 spi->controller->cleanup(spi);
8ae12a0d 51
8caab75f 52 spi_controller_put(spi->controller);
5039563e 53 kfree(spi->driver_override);
07a389fe 54 kfree(spi);
8ae12a0d
DB
55}
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);
8c4ff6d0
ZR
61 int len;
62
63 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
64 if (len != -ENODEV)
65 return len;
8ae12a0d 66
d8e328b3 67 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
8ae12a0d 68}
aa7da564 69static DEVICE_ATTR_RO(modalias);
8ae12a0d 70
5039563e
TP
71static 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
eca2ebc7 116#define SPI_STATISTICS_ATTRS(field, file) \
8caab75f
GU
117static ssize_t spi_controller_##field##_show(struct device *dev, \
118 struct device_attribute *attr, \
119 char *buf) \
eca2ebc7 120{ \
8caab75f
GU
121 struct spi_controller *ctlr = container_of(dev, \
122 struct spi_controller, dev); \
123 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
eca2ebc7 124} \
8caab75f 125static struct device_attribute dev_attr_spi_controller_##field = { \
ad25c92e 126 .attr = { .name = file, .mode = 0444 }, \
8caab75f 127 .show = spi_controller_##field##_show, \
eca2ebc7
MS
128}; \
129static ssize_t spi_device_##field##_show(struct device *dev, \
130 struct device_attribute *attr, \
131 char *buf) \
132{ \
d1eba93b 133 struct spi_device *spi = to_spi_device(dev); \
eca2ebc7
MS
134 return spi_statistics_##field##_show(&spi->statistics, buf); \
135} \
136static struct device_attribute dev_attr_spi_device_##field = { \
ad25c92e 137 .attr = { .name = file, .mode = 0444 }, \
eca2ebc7
MS
138 .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
6b7bc061
MS
171#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
d9f12122
MS
193SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
194
aa7da564
GKH
195static struct attribute *spi_dev_attrs[] = {
196 &dev_attr_modalias.attr,
5039563e 197 &dev_attr_driver_override.attr,
aa7da564 198 NULL,
8ae12a0d 199};
eca2ebc7
MS
200
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,
6b7bc061
MS
216 &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,
d9f12122 233 &dev_attr_spi_device_transfers_split_maxsize.attr,
eca2ebc7
MS
234 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
8caab75f
GU
248static 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,
eca2ebc7
MS
277 NULL,
278};
279
8caab75f 280static const struct attribute_group spi_controller_statistics_group = {
eca2ebc7 281 .name = "statistics",
8caab75f 282 .attrs = spi_controller_statistics_attrs,
eca2ebc7
MS
283};
284
285static const struct attribute_group *spi_master_groups[] = {
8caab75f 286 &spi_controller_statistics_group,
eca2ebc7
MS
287 NULL,
288};
289
290void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
291 struct spi_transfer *xfer,
8caab75f 292 struct spi_controller *ctlr)
eca2ebc7
MS
293{
294 unsigned long flags;
6b7bc061
MS
295 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
296
297 if (l2len < 0)
298 l2len = 0;
eca2ebc7
MS
299
300 spin_lock_irqsave(&stats->lock, flags);
301
302 stats->transfers++;
6b7bc061 303 stats->transfer_bytes_histo[l2len]++;
eca2ebc7
MS
304
305 stats->bytes += xfer->len;
306 if ((xfer->tx_buf) &&
8caab75f 307 (xfer->tx_buf != ctlr->dummy_tx))
eca2ebc7
MS
308 stats->bytes_tx += xfer->len;
309 if ((xfer->rx_buf) &&
8caab75f 310 (xfer->rx_buf != ctlr->dummy_rx))
eca2ebc7
MS
311 stats->bytes_rx += xfer->len;
312
313 spin_unlock_irqrestore(&stats->lock, flags);
314}
315EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
8ae12a0d
DB
316
317/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
318 * and the sysfs version makes coldplug work too.
319 */
320
75368bf6
AV
321static 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
8ae12a0d
DB
340static int spi_match_device(struct device *dev, struct device_driver *drv)
341{
342 const struct spi_device *spi = to_spi_device(dev);
75368bf6
AV
343 const struct spi_driver *sdrv = to_spi_driver(drv);
344
5039563e
TP
345 /* 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
2b7a32f7
SA
349 /* Attempt an OF style match */
350 if (of_driver_match_device(dev, drv))
351 return 1;
352
64bee4d2
MW
353 /* Then try ACPI */
354 if (acpi_driver_match_device(dev, drv))
355 return 1;
356
75368bf6
AV
357 if (sdrv->id_table)
358 return !!spi_match_id(sdrv->id_table, spi);
8ae12a0d 359
35f74fca 360 return strcmp(spi->modalias, drv->name) == 0;
8ae12a0d
DB
361}
362
7eff2e7a 363static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
8ae12a0d
DB
364{
365 const struct spi_device *spi = to_spi_device(dev);
8c4ff6d0
ZR
366 int rc;
367
368 rc = acpi_device_uevent_modalias(dev, env);
369 if (rc != -ENODEV)
370 return rc;
8ae12a0d 371
2856670f 372 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
8ae12a0d
DB
373}
374
8ae12a0d
DB
375struct bus_type spi_bus_type = {
376 .name = "spi",
aa7da564 377 .dev_groups = spi_dev_groups,
8ae12a0d
DB
378 .match = spi_match_device,
379 .uevent = spi_uevent,
8ae12a0d
DB
380};
381EXPORT_SYMBOL_GPL(spi_bus_type);
382
b885244e
DB
383
384static int spi_drv_probe(struct device *dev)
385{
386 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
44af7927 387 struct spi_device *spi = to_spi_device(dev);
33cf00e5
MW
388 int ret;
389
86be408b
SN
390 ret = of_clk_set_defaults(dev->of_node, false);
391 if (ret)
392 return ret;
393
44af7927
JH
394 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
676e7c25 402 ret = dev_pm_domain_attach(dev, true);
71f277a7
UH
403 if (ret)
404 return ret;
405
406 ret = sdrv->probe(spi);
407 if (ret)
408 dev_pm_domain_detach(dev, true);
b885244e 409
33cf00e5 410 return ret;
b885244e
DB
411}
412
413static int spi_drv_remove(struct device *dev)
414{
415 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
33cf00e5
MW
416 int ret;
417
aec35f4e 418 ret = sdrv->remove(to_spi_device(dev));
676e7c25 419 dev_pm_domain_detach(dev, true);
b885244e 420
33cf00e5 421 return ret;
b885244e
DB
422}
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
33e34dc6 431/**
ca5d2485 432 * __spi_register_driver - register a SPI driver
88c9321d 433 * @owner: owner module of the driver to register
33e34dc6
DB
434 * @sdrv: the driver to register
435 * Context: can sleep
97d56dc6
JMC
436 *
437 * Return: zero on success, else a negative error code.
33e34dc6 438 */
ca5d2485 439int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
b885244e 440{
ca5d2485 441 sdrv->driver.owner = owner;
b885244e
DB
442 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}
ca5d2485 451EXPORT_SYMBOL_GPL(__spi_register_driver);
b885244e 452
8ae12a0d
DB
453/*-------------------------------------------------------------------------*/
454
455/* SPI devices should normally not be created by SPI device drivers; that
8caab75f 456 * would make them board-specific. Similarly with SPI controller drivers.
8ae12a0d
DB
457 * 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;
2b9603a0 463 struct spi_board_info board_info;
8ae12a0d
DB
464};
465
466static LIST_HEAD(board_list);
8caab75f 467static LIST_HEAD(spi_controller_list);
2b9603a0
FT
468
469/*
470 * Used to protect add/del opertion for board_info list and
8caab75f 471 * spi_controller list, and their matching process
9a9a047a 472 * also used to protect object of type struct idr
2b9603a0 473 */
94040828 474static DEFINE_MUTEX(board_lock);
8ae12a0d 475
dc87c98e
GL
476/**
477 * spi_alloc_device - Allocate a new SPI device
8caab75f 478 * @ctlr: Controller to which device is connected
dc87c98e
GL
479 * 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
8caab75f 487 * spi_device structure to add it to the SPI controller. If the caller
dc87c98e
GL
488 * needs to discard the spi_device without adding it, then it should
489 * call spi_dev_put() on it.
490 *
97d56dc6 491 * Return: a pointer to the new device, or NULL.
dc87c98e 492 */
8caab75f 493struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
dc87c98e
GL
494{
495 struct spi_device *spi;
dc87c98e 496
8caab75f 497 if (!spi_controller_get(ctlr))
dc87c98e
GL
498 return NULL;
499
5fe5f05e 500 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
dc87c98e 501 if (!spi) {
8caab75f 502 spi_controller_put(ctlr);
dc87c98e
GL
503 return NULL;
504 }
505
8caab75f
GU
506 spi->master = spi->controller = ctlr;
507 spi->dev.parent = &ctlr->dev;
dc87c98e
GL
508 spi->dev.bus = &spi_bus_type;
509 spi->dev.release = spidev_release;
446411e1 510 spi->cs_gpio = -ENOENT;
eca2ebc7
MS
511
512 spin_lock_init(&spi->statistics.lock);
513
dc87c98e
GL
514 device_initialize(&spi->dev);
515 return spi;
516}
517EXPORT_SYMBOL_GPL(spi_alloc_device);
518
e13ac47b
JN
519static 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
8caab75f 528 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
e13ac47b
JN
529 spi->chip_select);
530}
531
b6fb8d3a
MW
532static 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
8caab75f 537 if (spi->controller == new_spi->controller &&
b6fb8d3a
MW
538 spi->chip_select == new_spi->chip_select)
539 return -EBUSY;
540 return 0;
541}
542
dc87c98e
GL
543/**
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 *
97d56dc6 550 * Return: 0 on success; negative errno on failure
dc87c98e
GL
551 */
552int spi_add_device(struct spi_device *spi)
553{
e48880e0 554 static DEFINE_MUTEX(spi_add_lock);
8caab75f
GU
555 struct spi_controller *ctlr = spi->controller;
556 struct device *dev = ctlr->dev.parent;
dc87c98e
GL
557 int status;
558
559 /* Chipselects are numbered 0..max; validate. */
8caab75f
GU
560 if (spi->chip_select >= ctlr->num_chipselect) {
561 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
562 ctlr->num_chipselect);
dc87c98e
GL
563 return -EINVAL;
564 }
565
566 /* Set the bus ID string */
e13ac47b 567 spi_dev_set_name(spi);
e48880e0
DB
568
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
b6fb8d3a
MW
575 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
576 if (status) {
e48880e0
DB
577 dev_err(dev, "chipselect %d already in use\n",
578 spi->chip_select);
e48880e0
DB
579 goto done;
580 }
581
f3186dd8
LW
582 /* Descriptors take precedence */
583 if (ctlr->cs_gpiods)
584 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
585 else if (ctlr->cs_gpios)
8caab75f 586 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
74317984 587
e48880e0
DB
588 /* 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 */
7d077197 592 status = spi_setup(spi);
dc87c98e 593 if (status < 0) {
eb288a1f
LW
594 dev_err(dev, "can't setup %s, status %d\n",
595 dev_name(&spi->dev), status);
e48880e0 596 goto done;
dc87c98e
GL
597 }
598
e48880e0 599 /* Device may be bound to an active driver when this returns */
dc87c98e 600 status = device_add(&spi->dev);
e48880e0 601 if (status < 0)
eb288a1f
LW
602 dev_err(dev, "can't add %s, status %d\n",
603 dev_name(&spi->dev), status);
e48880e0 604 else
35f74fca 605 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
dc87c98e 606
e48880e0
DB
607done:
608 mutex_unlock(&spi_add_lock);
609 return status;
dc87c98e
GL
610}
611EXPORT_SYMBOL_GPL(spi_add_device);
8ae12a0d 612
33e34dc6
DB
613/**
614 * spi_new_device - instantiate one new SPI device
8caab75f 615 * @ctlr: Controller to which device is connected
33e34dc6
DB
616 * @chip: Describes the SPI device
617 * Context: can sleep
618 *
619 * On typical mainboards, this is purely internal; and it's not needed
8ae12a0d
DB
620 * 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).
082c8cb4 624 *
97d56dc6 625 * Return: the new device, or NULL.
8ae12a0d 626 */
8caab75f 627struct spi_device *spi_new_device(struct spi_controller *ctlr,
e9d5a461 628 struct spi_board_info *chip)
8ae12a0d
DB
629{
630 struct spi_device *proxy;
8ae12a0d
DB
631 int status;
632
082c8cb4
DB
633 /* 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
8caab75f 640 proxy = spi_alloc_device(ctlr);
dc87c98e 641 if (!proxy)
8ae12a0d
DB
642 return NULL;
643
102eb975
GL
644 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
645
8ae12a0d
DB
646 proxy->chip_select = chip->chip_select;
647 proxy->max_speed_hz = chip->max_speed_hz;
980a01c9 648 proxy->mode = chip->mode;
8ae12a0d 649 proxy->irq = chip->irq;
102eb975 650 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
8ae12a0d
DB
651 proxy->dev.platform_data = (void *) chip->platform_data;
652 proxy->controller_data = chip->controller_data;
653 proxy->controller_state = NULL;
8ae12a0d 654
826cf175
DT
655 if (chip->properties) {
656 status = device_add_properties(&proxy->dev, chip->properties);
657 if (status) {
8caab75f 658 dev_err(&ctlr->dev,
826cf175
DT
659 "failed to add properties to '%s': %d\n",
660 chip->modalias, status);
661 goto err_dev_put;
662 }
8ae12a0d
DB
663 }
664
826cf175
DT
665 status = spi_add_device(proxy);
666 if (status < 0)
667 goto err_remove_props;
668
8ae12a0d 669 return proxy;
826cf175
DT
670
671err_remove_props:
672 if (chip->properties)
673 device_remove_properties(&proxy->dev);
674err_dev_put:
675 spi_dev_put(proxy);
676 return NULL;
8ae12a0d
DB
677}
678EXPORT_SYMBOL_GPL(spi_new_device);
679
3b1884c2
GU
680/**
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
8caab75f 685 * by spi_unregister_controller().
3b1884c2
GU
686 */
687void spi_unregister_device(struct spi_device *spi)
688{
bd6c1644
GU
689 if (!spi)
690 return;
691
8324147f 692 if (spi->dev.of_node) {
bd6c1644 693 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
8324147f
JH
694 of_node_put(spi->dev.of_node);
695 }
7f24467f
OP
696 if (ACPI_COMPANION(&spi->dev))
697 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
bd6c1644 698 device_unregister(&spi->dev);
3b1884c2
GU
699}
700EXPORT_SYMBOL_GPL(spi_unregister_device);
701
8caab75f
GU
702static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
703 struct spi_board_info *bi)
2b9603a0
FT
704{
705 struct spi_device *dev;
706
8caab75f 707 if (ctlr->bus_num != bi->bus_num)
2b9603a0
FT
708 return;
709
8caab75f 710 dev = spi_new_device(ctlr, bi);
2b9603a0 711 if (!dev)
8caab75f 712 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
2b9603a0
FT
713 bi->modalias);
714}
715
33e34dc6
DB
716/**
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 *
8ae12a0d
DB
722 * 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.
826cf175 734 * Device properties are deep-copied though.
97d56dc6
JMC
735 *
736 * Return: zero on success, else a negative error code.
8ae12a0d 737 */
fd4a319b 738int spi_register_board_info(struct spi_board_info const *info, unsigned n)
8ae12a0d 739{
2b9603a0
FT
740 struct boardinfo *bi;
741 int i;
8ae12a0d 742
c7908a37 743 if (!n)
f974cf57 744 return 0;
c7908a37 745
f9bdb7fd 746 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
8ae12a0d
DB
747 if (!bi)
748 return -ENOMEM;
8ae12a0d 749
2b9603a0 750 for (i = 0; i < n; i++, bi++, info++) {
8caab75f 751 struct spi_controller *ctlr;
8ae12a0d 752
2b9603a0 753 memcpy(&bi->board_info, info, sizeof(*info));
826cf175
DT
754 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
2b9603a0
FT
761 mutex_lock(&board_lock);
762 list_add_tail(&bi->list, &board_list);
8caab75f
GU
763 list_for_each_entry(ctlr, &spi_controller_list, list)
764 spi_match_controller_to_boardinfo(ctlr,
765 &bi->board_info);
2b9603a0 766 mutex_unlock(&board_lock);
8ae12a0d 767 }
2b9603a0
FT
768
769 return 0;
8ae12a0d
DB
770}
771
772/*-------------------------------------------------------------------------*/
773
b158935f
MB
774static void spi_set_cs(struct spi_device *spi, bool enable)
775{
776 if (spi->mode & SPI_CS_HIGH)
777 enable = !enable;
778
f3186dd8
LW
779 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)
28f7604f
FF
789 gpiod_set_value_cansleep(spi->cs_gpiod,
790 !enable);
f3186dd8 791 else
28f7604f 792 gpio_set_value_cansleep(spi->cs_gpio, !enable);
f3186dd8 793 }
8eee6b9d 794 /* Some SPI masters need both GPIO CS & slave_select */
8caab75f
GU
795 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);
8eee6b9d 800 }
b158935f
MB
801}
802
2de440f5 803#ifdef CONFIG_HAS_DMA
46336966
BB
804int 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)
6ad45a27
MB
807{
808 const bool vmalloced_buf = is_vmalloc_addr(buf);
df88e91b 809 unsigned int max_seg_size = dma_get_max_seg_size(dev);
b1b8153c
V
810#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
65598c13
AG
817 int desc_len;
818 int sgs;
6ad45a27 819 struct page *vm_page;
8dd4a016 820 struct scatterlist *sg;
6ad45a27
MB
821 void *sg_buf;
822 size_t min;
823 int i, ret;
824
b1b8153c 825 if (vmalloced_buf || kmap_buf) {
df88e91b 826 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
65598c13 827 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
0569a88f 828 } else if (virt_addr_valid(buf)) {
8caab75f 829 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
65598c13 830 sgs = DIV_ROUND_UP(len, desc_len);
0569a88f
V
831 } else {
832 return -EINVAL;
65598c13
AG
833 }
834
6ad45a27
MB
835 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
836 if (ret != 0)
837 return ret;
838
8dd4a016 839 sg = &sgt->sgl[0];
6ad45a27 840 for (i = 0; i < sgs; i++) {
6ad45a27 841
b1b8153c 842 if (vmalloced_buf || kmap_buf) {
ce99319a
MC
843 /*
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)));
b1b8153c
V
851 if (vmalloced_buf)
852 vm_page = vmalloc_to_page(buf);
853 else
854 vm_page = kmap_to_page(buf);
6ad45a27
MB
855 if (!vm_page) {
856 sg_free_table(sgt);
857 return -ENOMEM;
858 }
8dd4a016 859 sg_set_page(sg, vm_page,
c1aefbdd 860 min, offset_in_page(buf));
6ad45a27 861 } else {
65598c13 862 min = min_t(size_t, len, desc_len);
6ad45a27 863 sg_buf = buf;
8dd4a016 864 sg_set_buf(sg, sg_buf, min);
6ad45a27
MB
865 }
866
6ad45a27
MB
867 buf += min;
868 len -= min;
8dd4a016 869 sg = sg_next(sg);
6ad45a27
MB
870 }
871
872 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
89e4b66a
GU
873 if (!ret)
874 ret = -ENOMEM;
6ad45a27
MB
875 if (ret < 0) {
876 sg_free_table(sgt);
877 return ret;
878 }
879
880 sgt->nents = ret;
881
882 return 0;
883}
884
46336966
BB
885void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
886 struct sg_table *sgt, enum dma_data_direction dir)
6ad45a27
MB
887{
888 if (sgt->orig_nents) {
889 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
890 sg_free_table(sgt);
891 }
892}
893
8caab75f 894static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
99adef31 895{
99adef31
MB
896 struct device *tx_dev, *rx_dev;
897 struct spi_transfer *xfer;
6ad45a27 898 int ret;
3a2eba9b 899
8caab75f 900 if (!ctlr->can_dma)
99adef31
MB
901 return 0;
902
8caab75f
GU
903 if (ctlr->dma_tx)
904 tx_dev = ctlr->dma_tx->device->dev;
c37f45b5 905 else
8caab75f 906 tx_dev = ctlr->dev.parent;
c37f45b5 907
8caab75f
GU
908 if (ctlr->dma_rx)
909 rx_dev = ctlr->dma_rx->device->dev;
c37f45b5 910 else
8caab75f 911 rx_dev = ctlr->dev.parent;
99adef31
MB
912
913 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
8caab75f 914 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
99adef31
MB
915 continue;
916
917 if (xfer->tx_buf != NULL) {
8caab75f 918 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
6ad45a27
MB
919 (void *)xfer->tx_buf, xfer->len,
920 DMA_TO_DEVICE);
921 if (ret != 0)
922 return ret;
99adef31
MB
923 }
924
925 if (xfer->rx_buf != NULL) {
8caab75f 926 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
6ad45a27
MB
927 xfer->rx_buf, xfer->len,
928 DMA_FROM_DEVICE);
929 if (ret != 0) {
8caab75f 930 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
6ad45a27
MB
931 DMA_TO_DEVICE);
932 return ret;
99adef31
MB
933 }
934 }
935 }
936
8caab75f 937 ctlr->cur_msg_mapped = true;
99adef31
MB
938
939 return 0;
940}
941
8caab75f 942static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
99adef31
MB
943{
944 struct spi_transfer *xfer;
945 struct device *tx_dev, *rx_dev;
946
8caab75f 947 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
99adef31
MB
948 return 0;
949
8caab75f
GU
950 if (ctlr->dma_tx)
951 tx_dev = ctlr->dma_tx->device->dev;
c37f45b5 952 else
8caab75f 953 tx_dev = ctlr->dev.parent;
c37f45b5 954
8caab75f
GU
955 if (ctlr->dma_rx)
956 rx_dev = ctlr->dma_rx->device->dev;
c37f45b5 957 else
8caab75f 958 rx_dev = ctlr->dev.parent;
99adef31
MB
959
960 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
8caab75f 961 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
99adef31
MB
962 continue;
963
8caab75f
GU
964 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);
99adef31
MB
966 }
967
968 return 0;
969}
2de440f5 970#else /* !CONFIG_HAS_DMA */
8caab75f 971static inline int __spi_map_msg(struct spi_controller *ctlr,
2de440f5
GU
972 struct spi_message *msg)
973{
974 return 0;
975}
976
8caab75f 977static inline int __spi_unmap_msg(struct spi_controller *ctlr,
4b786458 978 struct spi_message *msg)
2de440f5
GU
979{
980 return 0;
981}
982#endif /* !CONFIG_HAS_DMA */
983
8caab75f 984static inline int spi_unmap_msg(struct spi_controller *ctlr,
4b786458
MS
985 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 */
8caab75f 994 if (xfer->tx_buf == ctlr->dummy_tx)
4b786458 995 xfer->tx_buf = NULL;
8caab75f 996 if (xfer->rx_buf == ctlr->dummy_rx)
4b786458
MS
997 xfer->rx_buf = NULL;
998 }
999
8caab75f 1000 return __spi_unmap_msg(ctlr, msg);
4b786458
MS
1001}
1002
8caab75f 1003static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
2de440f5
GU
1004{
1005 struct spi_transfer *xfer;
1006 void *tmp;
1007 unsigned int max_tx, max_rx;
1008
8caab75f 1009 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
2de440f5
GU
1010 max_tx = 0;
1011 max_rx = 0;
1012
1013 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
8caab75f 1014 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
2de440f5
GU
1015 !xfer->tx_buf)
1016 max_tx = max(xfer->len, max_tx);
8caab75f 1017 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
2de440f5
GU
1018 !xfer->rx_buf)
1019 max_rx = max(xfer->len, max_rx);
1020 }
1021
1022 if (max_tx) {
8caab75f 1023 tmp = krealloc(ctlr->dummy_tx, max_tx,
2de440f5
GU
1024 GFP_KERNEL | GFP_DMA);
1025 if (!tmp)
1026 return -ENOMEM;
8caab75f 1027 ctlr->dummy_tx = tmp;
2de440f5
GU
1028 memset(tmp, 0, max_tx);
1029 }
1030
1031 if (max_rx) {
8caab75f 1032 tmp = krealloc(ctlr->dummy_rx, max_rx,
2de440f5
GU
1033 GFP_KERNEL | GFP_DMA);
1034 if (!tmp)
1035 return -ENOMEM;
8caab75f 1036 ctlr->dummy_rx = tmp;
2de440f5
GU
1037 }
1038
1039 if (max_tx || max_rx) {
1040 list_for_each_entry(xfer, &msg->transfers,
1041 transfer_list) {
1042 if (!xfer->tx_buf)
8caab75f 1043 xfer->tx_buf = ctlr->dummy_tx;
2de440f5 1044 if (!xfer->rx_buf)
8caab75f 1045 xfer->rx_buf = ctlr->dummy_rx;
2de440f5
GU
1046 }
1047 }
1048 }
1049
8caab75f 1050 return __spi_map_msg(ctlr, msg);
2de440f5 1051}
99adef31 1052
810923f3
LR
1053static 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
b158935f
MB
1089/*
1090 * spi_transfer_one_message - Default implementation of transfer_one_message()
1091 *
1092 * This is a standard implementation of transfer_one_message() for
8ba811a7 1093 * drivers which implement a transfer_one() operation. It provides
b158935f
MB
1094 * standard handling of delays and chip select management.
1095 */
8caab75f 1096static int spi_transfer_one_message(struct spi_controller *ctlr,
b158935f
MB
1097 struct spi_message *msg)
1098{
1099 struct spi_transfer *xfer;
b158935f
MB
1100 bool keep_cs = false;
1101 int ret = 0;
8caab75f 1102 struct spi_statistics *statm = &ctlr->statistics;
eca2ebc7 1103 struct spi_statistics *stats = &msg->spi->statistics;
b158935f
MB
1104
1105 spi_set_cs(msg->spi, true);
1106
eca2ebc7
MS
1107 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1108 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1109
b158935f
MB
1110 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1111 trace_spi_transfer_start(msg, xfer);
1112
8caab75f
GU
1113 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1114 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
eca2ebc7 1115
38ec10f6 1116 if (xfer->tx_buf || xfer->rx_buf) {
8caab75f 1117 reinit_completion(&ctlr->xfer_completion);
b158935f 1118
8caab75f 1119 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
38ec10f6 1120 if (ret < 0) {
eca2ebc7
MS
1121 SPI_STATISTICS_INCREMENT_FIELD(statm,
1122 errors);
1123 SPI_STATISTICS_INCREMENT_FIELD(stats,
1124 errors);
38ec10f6
MB
1125 dev_err(&msg->spi->dev,
1126 "SPI transfer failed: %d\n", ret);
1127 goto out;
1128 }
b158935f 1129
d57e7960
MB
1130 if (ret > 0) {
1131 ret = spi_transfer_wait(ctlr, msg, xfer);
1132 if (ret < 0)
1133 msg->status = ret;
1134 }
38ec10f6
MB
1135 } else {
1136 if (xfer->len)
1137 dev_err(&msg->spi->dev,
1138 "Bufferless transfer has length %u\n",
1139 xfer->len);
13a42798 1140 }
b158935f
MB
1141
1142 trace_spi_transfer_stop(msg, xfer);
1143
1144 if (msg->status != -EINPROGRESS)
1145 goto out;
1146
8244bd3a
DK
1147 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 }
b158935f
MB
1155
1156 if (xfer->cs_change) {
1157 if (list_is_last(&xfer->transfer_list,
1158 &msg->transfers)) {
1159 keep_cs = true;
1160 } else {
0b73aa63
MB
1161 spi_set_cs(msg->spi, false);
1162 udelay(10);
1163 spi_set_cs(msg->spi, true);
b158935f
MB
1164 }
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
8caab75f
GU
1177 if (msg->status && ctlr->handle_err)
1178 ctlr->handle_err(ctlr, msg);
b716c4ff 1179
8caab75f 1180 spi_res_release(ctlr, msg);
d780c371 1181
8caab75f 1182 spi_finalize_current_message(ctlr);
b158935f
MB
1183
1184 return ret;
1185}
1186
1187/**
1188 * spi_finalize_current_transfer - report completion of a transfer
8caab75f 1189 * @ctlr: the controller reporting completion
b158935f
MB
1190 *
1191 * Called by SPI drivers using the core transfer_one_message()
1192 * implementation to notify it that the current interrupt driven
9e8f4882 1193 * transfer has finished and the next one may be scheduled.
b158935f 1194 */
8caab75f 1195void spi_finalize_current_transfer(struct spi_controller *ctlr)
b158935f 1196{
8caab75f 1197 complete(&ctlr->xfer_completion);
b158935f
MB
1198}
1199EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1200
ffbbdd21 1201/**
fc9e0f71 1202 * __spi_pump_messages - function which processes spi message queue
8caab75f 1203 * @ctlr: controller to process queue for
fc9e0f71 1204 * @in_kthread: true if we are in the context of the message pump thread
ffbbdd21
LW
1205 *
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 *
0461a414
MB
1210 * 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.
ffbbdd21 1213 */
8caab75f 1214static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
ffbbdd21 1215{
ffbbdd21
LW
1216 unsigned long flags;
1217 bool was_busy = false;
1218 int ret;
1219
983aee5d 1220 /* Lock queue */
8caab75f 1221 spin_lock_irqsave(&ctlr->queue_lock, flags);
983aee5d
MB
1222
1223 /* Make sure we are not already running a message */
8caab75f
GU
1224 if (ctlr->cur_msg) {
1225 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
983aee5d
MB
1226 return;
1227 }
1228
f0125f1a 1229 /* If another context is idling the device then defer */
8caab75f
GU
1230 if (ctlr->idling) {
1231 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1232 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
0461a414
MB
1233 return;
1234 }
1235
983aee5d 1236 /* Check if the queue is idle */
8caab75f
GU
1237 if (list_empty(&ctlr->queue) || !ctlr->running) {
1238 if (!ctlr->busy) {
1239 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
b0b36b86 1240 return;
ffbbdd21 1241 }
fc9e0f71 1242
f0125f1a
MB
1243 /* 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;
8caab75f 1271 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
ffbbdd21
LW
1272 return;
1273 }
ffbbdd21 1274
ffbbdd21 1275 /* Extract head of queue */
8caab75f
GU
1276 ctlr->cur_msg =
1277 list_first_entry(&ctlr->queue, struct spi_message, queue);
ffbbdd21 1278
8caab75f
GU
1279 list_del_init(&ctlr->cur_msg->queue);
1280 if (ctlr->busy)
ffbbdd21
LW
1281 was_busy = true;
1282 else
8caab75f
GU
1283 ctlr->busy = true;
1284 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
ffbbdd21 1285
8caab75f 1286 mutex_lock(&ctlr->io_mutex);
ef4d96ec 1287
8caab75f
GU
1288 if (!was_busy && ctlr->auto_runtime_pm) {
1289 ret = pm_runtime_get_sync(ctlr->dev.parent);
49834de2 1290 if (ret < 0) {
7e48e23a 1291 pm_runtime_put_noidle(ctlr->dev.parent);
8caab75f 1292 dev_err(&ctlr->dev, "Failed to power device: %d\n",
49834de2 1293 ret);
8caab75f 1294 mutex_unlock(&ctlr->io_mutex);
49834de2
MB
1295 return;
1296 }
1297 }
1298
56ec1978 1299 if (!was_busy)
8caab75f 1300 trace_spi_controller_busy(ctlr);
56ec1978 1301
8caab75f
GU
1302 if (!was_busy && ctlr->prepare_transfer_hardware) {
1303 ret = ctlr->prepare_transfer_hardware(ctlr);
ffbbdd21 1304 if (ret) {
8caab75f 1305 dev_err(&ctlr->dev,
ffbbdd21 1306 "failed to prepare transfer hardware\n");
49834de2 1307
8caab75f
GU
1308 if (ctlr->auto_runtime_pm)
1309 pm_runtime_put(ctlr->dev.parent);
1310 mutex_unlock(&ctlr->io_mutex);
ffbbdd21
LW
1311 return;
1312 }
1313 }
1314
8caab75f 1315 trace_spi_message_start(ctlr->cur_msg);
56ec1978 1316
8caab75f
GU
1317 if (ctlr->prepare_message) {
1318 ret = ctlr->prepare_message(ctlr, ctlr->cur_msg);
2841a5fc 1319 if (ret) {
8caab75f
GU
1320 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1321 ret);
1322 ctlr->cur_msg->status = ret;
1323 spi_finalize_current_message(ctlr);
49023d2e 1324 goto out;
2841a5fc 1325 }
8caab75f 1326 ctlr->cur_msg_prepared = true;
2841a5fc
MB
1327 }
1328
8caab75f 1329 ret = spi_map_msg(ctlr, ctlr->cur_msg);
99adef31 1330 if (ret) {
8caab75f
GU
1331 ctlr->cur_msg->status = ret;
1332 spi_finalize_current_message(ctlr);
49023d2e 1333 goto out;
99adef31
MB
1334 }
1335
8caab75f 1336 ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg);
ffbbdd21 1337 if (ret) {
8caab75f 1338 dev_err(&ctlr->dev,
1f802f82 1339 "failed to transfer one message from queue\n");
49023d2e 1340 goto out;
ffbbdd21 1341 }
49023d2e
JH
1342
1343out:
8caab75f 1344 mutex_unlock(&ctlr->io_mutex);
62826970
MB
1345
1346 /* Prod the scheduler in case transfer_one() was busy waiting */
49023d2e
JH
1347 if (!ret)
1348 cond_resched();
ffbbdd21
LW
1349}
1350
fc9e0f71
MB
1351/**
1352 * spi_pump_messages - kthread work function which processes spi message queue
8caab75f 1353 * @work: pointer to kthread work struct contained in the controller struct
fc9e0f71
MB
1354 */
1355static void spi_pump_messages(struct kthread_work *work)
1356{
8caab75f
GU
1357 struct spi_controller *ctlr =
1358 container_of(work, struct spi_controller, pump_messages);
fc9e0f71 1359
8caab75f 1360 __spi_pump_messages(ctlr, true);
fc9e0f71
MB
1361}
1362
8caab75f 1363static int spi_init_queue(struct spi_controller *ctlr)
ffbbdd21
LW
1364{
1365 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1366
8caab75f
GU
1367 ctlr->running = false;
1368 ctlr->busy = false;
ffbbdd21 1369
8caab75f
GU
1370 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);
ffbbdd21 1376 }
8caab75f 1377 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
f0125f1a 1378
ffbbdd21 1379 /*
8caab75f 1380 * Controller config will indicate if this controller should run the
ffbbdd21
LW
1381 * 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 */
8caab75f
GU
1386 if (ctlr->rt) {
1387 dev_info(&ctlr->dev,
ffbbdd21 1388 "will run message pump with realtime priority\n");
8caab75f 1389 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
ffbbdd21
LW
1390 }
1391
1392 return 0;
1393}
1394
1395/**
1396 * spi_get_next_queued_message() - called by driver to check for queued
1397 * messages
8caab75f 1398 * @ctlr: the controller to check for queued messages
ffbbdd21
LW
1399 *
1400 * If there are more messages in the queue, the next message is returned from
1401 * this call.
97d56dc6
JMC
1402 *
1403 * Return: the next message in the queue, else NULL if the queue is empty.
ffbbdd21 1404 */
8caab75f 1405struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
ffbbdd21
LW
1406{
1407 struct spi_message *next;
1408 unsigned long flags;
1409
1410 /* get a pointer to the next message, if any */
8caab75f
GU
1411 spin_lock_irqsave(&ctlr->queue_lock, flags);
1412 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
1cfd97f9 1413 queue);
8caab75f 1414 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
ffbbdd21
LW
1415
1416 return next;
1417}
1418EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1419
1420/**
1421 * spi_finalize_current_message() - the current message is complete
8caab75f 1422 * @ctlr: the controller to return the message to
ffbbdd21
LW
1423 *
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 */
8caab75f 1427void spi_finalize_current_message(struct spi_controller *ctlr)
ffbbdd21
LW
1428{
1429 struct spi_message *mesg;
1430 unsigned long flags;
2841a5fc 1431 int ret;
ffbbdd21 1432
8caab75f
GU
1433 spin_lock_irqsave(&ctlr->queue_lock, flags);
1434 mesg = ctlr->cur_msg;
1435 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
ffbbdd21 1436
8caab75f 1437 spi_unmap_msg(ctlr, mesg);
99adef31 1438
8caab75f
GU
1439 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1440 ret = ctlr->unprepare_message(ctlr, mesg);
2841a5fc 1441 if (ret) {
8caab75f
GU
1442 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1443 ret);
2841a5fc
MB
1444 }
1445 }
391949b6 1446
8caab75f
GU
1447 spin_lock_irqsave(&ctlr->queue_lock, flags);
1448 ctlr->cur_msg = NULL;
1449 ctlr->cur_msg_prepared = false;
f0125f1a 1450 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
8caab75f 1451 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
8e76ef88
MS
1452
1453 trace_spi_message_done(mesg);
2841a5fc 1454
ffbbdd21
LW
1455 mesg->state = NULL;
1456 if (mesg->complete)
1457 mesg->complete(mesg->context);
1458}
1459EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1460
8caab75f 1461static int spi_start_queue(struct spi_controller *ctlr)
ffbbdd21
LW
1462{
1463 unsigned long flags;
1464
8caab75f 1465 spin_lock_irqsave(&ctlr->queue_lock, flags);
ffbbdd21 1466
8caab75f
GU
1467 if (ctlr->running || ctlr->busy) {
1468 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
ffbbdd21
LW
1469 return -EBUSY;
1470 }
1471
8caab75f
GU
1472 ctlr->running = true;
1473 ctlr->cur_msg = NULL;
1474 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
ffbbdd21 1475
8caab75f 1476 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
ffbbdd21
LW
1477
1478 return 0;
1479}
1480
8caab75f 1481static int spi_stop_queue(struct spi_controller *ctlr)
ffbbdd21
LW
1482{
1483 unsigned long flags;
1484 unsigned limit = 500;
1485 int ret = 0;
1486
8caab75f 1487 spin_lock_irqsave(&ctlr->queue_lock, flags);
ffbbdd21
LW
1488
1489 /*
1490 * This is a bit lame, but is optimized for the common execution path.
8caab75f 1491 * A wait_queue on the ctlr->busy could be used, but then the common
ffbbdd21
LW
1492 * execution path (pump_messages) would be required to call wake_up or
1493 * friends on every SPI message. Do this instead.
1494 */
8caab75f
GU
1495 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1496 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
f97b26b0 1497 usleep_range(10000, 11000);
8caab75f 1498 spin_lock_irqsave(&ctlr->queue_lock, flags);
ffbbdd21
LW
1499 }
1500
8caab75f 1501 if (!list_empty(&ctlr->queue) || ctlr->busy)
ffbbdd21
LW
1502 ret = -EBUSY;
1503 else
8caab75f 1504 ctlr->running = false;
ffbbdd21 1505
8caab75f 1506 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
ffbbdd21
LW
1507
1508 if (ret) {
8caab75f 1509 dev_warn(&ctlr->dev, "could not stop message queue\n");
ffbbdd21
LW
1510 return ret;
1511 }
1512 return ret;
1513}
1514
8caab75f 1515static int spi_destroy_queue(struct spi_controller *ctlr)
ffbbdd21
LW
1516{
1517 int ret;
1518
8caab75f 1519 ret = spi_stop_queue(ctlr);
ffbbdd21
LW
1520
1521 /*
3989144f 1522 * kthread_flush_worker will block until all work is done.
ffbbdd21
LW
1523 * 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) {
8caab75f 1528 dev_err(&ctlr->dev, "problem destroying queue\n");
ffbbdd21
LW
1529 return ret;
1530 }
1531
8caab75f
GU
1532 kthread_flush_worker(&ctlr->kworker);
1533 kthread_stop(ctlr->kworker_task);
ffbbdd21
LW
1534
1535 return 0;
1536}
1537
0461a414
MB
1538static int __spi_queued_transfer(struct spi_device *spi,
1539 struct spi_message *msg,
1540 bool need_pump)
ffbbdd21 1541{
8caab75f 1542 struct spi_controller *ctlr = spi->controller;
ffbbdd21
LW
1543 unsigned long flags;
1544
8caab75f 1545 spin_lock_irqsave(&ctlr->queue_lock, flags);
ffbbdd21 1546
8caab75f
GU
1547 if (!ctlr->running) {
1548 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
ffbbdd21
LW
1549 return -ESHUTDOWN;
1550 }
1551 msg->actual_length = 0;
1552 msg->status = -EINPROGRESS;
1553
8caab75f 1554 list_add_tail(&msg->queue, &ctlr->queue);
f0125f1a 1555 if (!ctlr->busy && need_pump)
8caab75f 1556 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
ffbbdd21 1557
8caab75f 1558 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
ffbbdd21
LW
1559 return 0;
1560}
1561
0461a414
MB
1562/**
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
97d56dc6
JMC
1566 *
1567 * Return: zero on success, else a negative error code.
0461a414
MB
1568 */
1569static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1570{
1571 return __spi_queued_transfer(spi, msg, true);
1572}
1573
8caab75f 1574static int spi_controller_initialize_queue(struct spi_controller *ctlr)
ffbbdd21
LW
1575{
1576 int ret;
1577
8caab75f
GU
1578 ctlr->transfer = spi_queued_transfer;
1579 if (!ctlr->transfer_one_message)
1580 ctlr->transfer_one_message = spi_transfer_one_message;
ffbbdd21
LW
1581
1582 /* Initialize and start queue */
8caab75f 1583 ret = spi_init_queue(ctlr);
ffbbdd21 1584 if (ret) {
8caab75f 1585 dev_err(&ctlr->dev, "problem initializing queue\n");
ffbbdd21
LW
1586 goto err_init_queue;
1587 }
8caab75f
GU
1588 ctlr->queued = true;
1589 ret = spi_start_queue(ctlr);
ffbbdd21 1590 if (ret) {
8caab75f 1591 dev_err(&ctlr->dev, "problem starting queue\n");
ffbbdd21
LW
1592 goto err_start_queue;
1593 }
1594
1595 return 0;
1596
1597err_start_queue:
8caab75f 1598 spi_destroy_queue(ctlr);
c3676d5c 1599err_init_queue:
ffbbdd21
LW
1600 return ret;
1601}
1602
988f259b
BB
1603/**
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
ffbbdd21
LW
1619/*-------------------------------------------------------------------------*/
1620
7cb94361 1621#if defined(CONFIG_OF)
8caab75f 1622static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
c2e51ac3 1623 struct device_node *nc)
aff5e3f8 1624{
aff5e3f8 1625 u32 value;
c2e51ac3 1626 int rc;
aff5e3f8 1627
aff5e3f8 1628 /* Mode (clock phase/polarity/etc.) */
e0bcb680 1629 if (of_property_read_bool(nc, "spi-cpha"))
aff5e3f8 1630 spi->mode |= SPI_CPHA;
e0bcb680 1631 if (of_property_read_bool(nc, "spi-cpol"))
aff5e3f8 1632 spi->mode |= SPI_CPOL;
e0bcb680 1633 if (of_property_read_bool(nc, "spi-3wire"))
aff5e3f8 1634 spi->mode |= SPI_3WIRE;
e0bcb680 1635 if (of_property_read_bool(nc, "spi-lsb-first"))
aff5e3f8
PA
1636 spi->mode |= SPI_LSB_FIRST;
1637
f3186dd8
LW
1638 /*
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
aff5e3f8
PA
1648 /* 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;
6b03061f
YNG
1659 case 8:
1660 spi->mode |= SPI_TX_OCTAL;
1661 break;
aff5e3f8 1662 default:
8caab75f 1663 dev_warn(&ctlr->dev,
aff5e3f8
PA
1664 "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;
6b03061f
YNG
1680 case 8:
1681 spi->mode |= SPI_RX_OCTAL;
1682 break;
aff5e3f8 1683 default:
8caab75f 1684 dev_warn(&ctlr->dev,
aff5e3f8
PA
1685 "spi-rx-bus-width %d not supported\n",
1686 value);
1687 break;
1688 }
1689 }
1690
8caab75f 1691 if (spi_controller_is_slave(ctlr)) {
194276b0 1692 if (!of_node_name_eq(nc, "slave")) {
25c56c88
RH
1693 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
1694 nc);
6c364062
GU
1695 return -EINVAL;
1696 }
1697 return 0;
1698 }
1699
1700 /* Device address */
1701 rc = of_property_read_u32(nc, "reg", &value);
1702 if (rc) {
25c56c88
RH
1703 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
1704 nc, rc);
6c364062
GU
1705 return rc;
1706 }
1707 spi->chip_select = value;
1708
aff5e3f8
PA
1709 /* Device speed */
1710 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1711 if (rc) {
8caab75f 1712 dev_err(&ctlr->dev,
25c56c88 1713 "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
c2e51ac3 1714 return rc;
aff5e3f8
PA
1715 }
1716 spi->max_speed_hz = value;
1717
c2e51ac3
GU
1718 return 0;
1719}
1720
1721static struct spi_device *
8caab75f 1722of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
c2e51ac3
GU
1723{
1724 struct spi_device *spi;
1725 int rc;
1726
1727 /* Alloc an spi_device */
8caab75f 1728 spi = spi_alloc_device(ctlr);
c2e51ac3 1729 if (!spi) {
25c56c88 1730 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
c2e51ac3
GU
1731 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) {
25c56c88 1739 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
c2e51ac3
GU
1740 goto err_out;
1741 }
1742
8caab75f 1743 rc = of_spi_parse_dt(ctlr, spi, nc);
c2e51ac3
GU
1744 if (rc)
1745 goto err_out;
1746
aff5e3f8
PA
1747 /* 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 */
aff5e3f8
PA
1752 rc = spi_add_device(spi);
1753 if (rc) {
25c56c88 1754 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
8324147f 1755 goto err_of_node_put;
aff5e3f8
PA
1756 }
1757
1758 return spi;
1759
8324147f
JH
1760err_of_node_put:
1761 of_node_put(nc);
aff5e3f8
PA
1762err_out:
1763 spi_dev_put(spi);
1764 return ERR_PTR(rc);
1765}
1766
d57a4282
GL
1767/**
1768 * of_register_spi_devices() - Register child devices onto the SPI bus
8caab75f 1769 * @ctlr: Pointer to spi_controller device
d57a4282 1770 *
6c364062
GU
1771 * Registers an spi_device for each child node of controller node which
1772 * represents a valid SPI slave.
d57a4282 1773 */
8caab75f 1774static void of_register_spi_devices(struct spi_controller *ctlr)
d57a4282
GL
1775{
1776 struct spi_device *spi;
1777 struct device_node *nc;
d57a4282 1778
8caab75f 1779 if (!ctlr->dev.of_node)
d57a4282
GL
1780 return;
1781
8caab75f 1782 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
bd6c1644
GU
1783 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1784 continue;
8caab75f 1785 spi = of_register_spi_device(ctlr, nc);
e0af98a7 1786 if (IS_ERR(spi)) {
8caab75f 1787 dev_warn(&ctlr->dev,
25c56c88 1788 "Failed to create SPI device for %pOF\n", nc);
e0af98a7
RR
1789 of_node_clear_flag(nc, OF_POPULATED);
1790 }
d57a4282
GL
1791 }
1792}
1793#else
8caab75f 1794static void of_register_spi_devices(struct spi_controller *ctlr) { }
d57a4282
GL
1795#endif
1796
64bee4d2 1797#ifdef CONFIG_ACPI
8a2e487e
LW
1798static 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
64bee4d2
MW
1827static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1828{
1829 struct spi_device *spi = data;
8caab75f 1830 struct spi_controller *ctlr = spi->controller;
64bee4d2
MW
1831
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) {
a0a90718
MW
1837 /*
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 */
8caab75f
GU
1844 if (ctlr->fw_translate_cs) {
1845 int cs = ctlr->fw_translate_cs(ctlr,
a0a90718
MW
1846 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
64bee4d2
MW
1854 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
8caab75f 1874static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
7f24467f 1875 struct acpi_device *adev)
64bee4d2 1876{
64bee4d2 1877 struct list_head resource_list;
64bee4d2
MW
1878 struct spi_device *spi;
1879 int ret;
1880
7f24467f
OP
1881 if (acpi_bus_get_status(adev) || !adev->status.present ||
1882 acpi_device_enumerated(adev))
64bee4d2
MW
1883 return AE_OK;
1884
8caab75f 1885 spi = spi_alloc_device(ctlr);
64bee4d2 1886 if (!spi) {
8caab75f 1887 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
64bee4d2
MW
1888 dev_name(&adev->dev));
1889 return AE_NO_MEMORY;
1890 }
1891
7b199811 1892 ACPI_COMPANION_SET(&spi->dev, adev);
64bee4d2
MW
1893 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
8a2e487e
LW
1900 acpi_spi_parse_apple_properties(spi);
1901
64bee4d2
MW
1902 if (ret < 0 || !spi->max_speed_hz) {
1903 spi_dev_put(spi);
1904 return AE_OK;
1905 }
1906
0c6543f6
DD
1907 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
1908 sizeof(spi->modalias));
1909
33ada67d
CR
1910 if (spi->irq < 0)
1911 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1912
7f24467f
OP
1913 acpi_device_set_enumerated(adev);
1914
33cf00e5 1915 adev->power.flags.ignore_parent = true;
64bee4d2 1916 if (spi_add_device(spi)) {
33cf00e5 1917 adev->power.flags.ignore_parent = false;
8caab75f 1918 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
64bee4d2
MW
1919 dev_name(&adev->dev));
1920 spi_dev_put(spi);
1921 }
1922
1923 return AE_OK;
1924}
1925
7f24467f
OP
1926static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1927 void *data, void **return_value)
1928{
8caab75f 1929 struct spi_controller *ctlr = data;
7f24467f
OP
1930 struct acpi_device *adev;
1931
1932 if (acpi_bus_get_device(handle, &adev))
1933 return AE_OK;
1934
8caab75f 1935 return acpi_register_spi_device(ctlr, adev);
7f24467f
OP
1936}
1937
8caab75f 1938static void acpi_register_spi_devices(struct spi_controller *ctlr)
64bee4d2
MW
1939{
1940 acpi_status status;
1941 acpi_handle handle;
1942
8caab75f 1943 handle = ACPI_HANDLE(ctlr->dev.parent);
64bee4d2
MW
1944 if (!handle)
1945 return;
1946
1947 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
8caab75f 1948 acpi_spi_add_device, NULL, ctlr, NULL);
64bee4d2 1949 if (ACPI_FAILURE(status))
8caab75f 1950 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
64bee4d2
MW
1951}
1952#else
8caab75f 1953static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
64bee4d2
MW
1954#endif /* CONFIG_ACPI */
1955
8caab75f 1956static void spi_controller_release(struct device *dev)
8ae12a0d 1957{
8caab75f 1958 struct spi_controller *ctlr;
8ae12a0d 1959
8caab75f
GU
1960 ctlr = container_of(dev, struct spi_controller, dev);
1961 kfree(ctlr);
8ae12a0d
DB
1962}
1963
1964static struct class spi_master_class = {
1965 .name = "spi_master",
1966 .owner = THIS_MODULE,
8caab75f 1967 .dev_release = spi_controller_release,
eca2ebc7 1968 .dev_groups = spi_master_groups,
8ae12a0d
DB
1969};
1970
6c364062
GU
1971#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{
8caab75f 1979 struct spi_controller *ctlr = spi->controller;
6c364062 1980
8caab75f
GU
1981 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
1982 return ctlr->slave_abort(ctlr);
6c364062
GU
1983
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{
8caab75f
GU
1996 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1997 dev);
6c364062
GU
1998 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{
8caab75f
GU
2009 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2010 dev);
6c364062
GU
2011 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[] = {
8caab75f 2057 &spi_controller_statistics_group,
6c364062
GU
2058 &spi_slave_group,
2059 NULL,
2060};
2061
2062static struct class spi_slave_class = {
2063 .name = "spi_slave",
2064 .owner = THIS_MODULE,
8caab75f 2065 .dev_release = spi_controller_release,
6c364062
GU
2066 .dev_groups = spi_slave_groups,
2067};
2068#else
2069extern struct class spi_slave_class; /* dummy */
2070#endif
8ae12a0d
DB
2071
2072/**
6c364062 2073 * __spi_alloc_controller - allocate an SPI master or slave controller
8ae12a0d 2074 * @dev: the controller, possibly using the platform_bus
33e34dc6 2075 * @size: how much zeroed driver-private data to allocate; the pointer to this
49dce689 2076 * memory is in the driver_data field of the returned device,
8caab75f 2077 * accessible with spi_controller_get_devdata().
6c364062
GU
2078 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2079 * slave (true) controller
33e34dc6 2080 * Context: can sleep
8ae12a0d 2081 *
6c364062 2082 * This call is used only by SPI controller drivers, which are the
8ae12a0d 2083 * only ones directly touching chip registers. It's how they allocate
8caab75f 2084 * an spi_controller structure, prior to calling spi_register_controller().
8ae12a0d 2085 *
97d56dc6 2086 * This must be called from context that can sleep.
8ae12a0d 2087 *
6c364062 2088 * The caller is responsible for assigning the bus number and initializing the
8caab75f
GU
2089 * 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.
97d56dc6 2092 *
6c364062 2093 * Return: the SPI controller structure on success, else NULL.
8ae12a0d 2094 */
8caab75f
GU
2095struct spi_controller *__spi_alloc_controller(struct device *dev,
2096 unsigned int size, bool slave)
8ae12a0d 2097{
8caab75f 2098 struct spi_controller *ctlr;
8ae12a0d 2099
0c868461
DB
2100 if (!dev)
2101 return NULL;
2102
8caab75f
GU
2103 ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL);
2104 if (!ctlr)
8ae12a0d
DB
2105 return NULL;
2106
8caab75f
GU
2107 device_initialize(&ctlr->dev);
2108 ctlr->bus_num = -1;
2109 ctlr->num_chipselect = 1;
2110 ctlr->slave = slave;
6c364062 2111 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
8caab75f 2112 ctlr->dev.class = &spi_slave_class;
6c364062 2113 else
8caab75f
GU
2114 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]);
8ae12a0d 2118
8caab75f 2119 return ctlr;
8ae12a0d 2120}
6c364062 2121EXPORT_SYMBOL_GPL(__spi_alloc_controller);
8ae12a0d 2122
74317984 2123#ifdef CONFIG_OF
8caab75f 2124static int of_spi_register_master(struct spi_controller *ctlr)
74317984 2125{
e80beb27 2126 int nb, i, *cs;
8caab75f 2127 struct device_node *np = ctlr->dev.of_node;
74317984
JCPV
2128
2129 if (!np)
2130 return 0;
2131
2132 nb = of_gpio_named_count(np, "cs-gpios");
8caab75f 2133 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
74317984 2134
8ec5d84e
AL
2135 /* Return error only for an incorrectly formed cs-gpios property */
2136 if (nb == 0 || nb == -ENOENT)
74317984 2137 return 0;
8ec5d84e
AL
2138 else if (nb < 0)
2139 return nb;
74317984 2140
a86854d0 2141 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
74317984 2142 GFP_KERNEL);
8caab75f 2143 ctlr->cs_gpios = cs;
74317984 2144
8caab75f 2145 if (!ctlr->cs_gpios)
74317984
JCPV
2146 return -ENOMEM;
2147
8caab75f 2148 for (i = 0; i < ctlr->num_chipselect; i++)
446411e1 2149 cs[i] = -ENOENT;
74317984
JCPV
2150
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
8caab75f 2157static int of_spi_register_master(struct spi_controller *ctlr)
74317984
JCPV
2158{
2159 return 0;
2160}
2161#endif
2162
f3186dd8
LW
2163/**
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
bdf3a3b5
BB
2217static int spi_controller_check_ops(struct spi_controller *ctlr)
2218{
2219 /*
b5932f5c
BB
2220 * 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.
bdf3a3b5 2225 */
b5932f5c
BB
2226 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) {
bdf3a3b5 2231 return -EINVAL;
b5932f5c 2232 }
bdf3a3b5
BB
2233
2234 return 0;
2235}
2236
8ae12a0d 2237/**
8caab75f
GU
2238 * spi_register_controller - register SPI master or slave controller
2239 * @ctlr: initialized master, originally from spi_alloc_master() or
2240 * spi_alloc_slave()
33e34dc6 2241 * Context: can sleep
8ae12a0d 2242 *
8caab75f 2243 * SPI controllers connect to their drivers using some non-SPI bus,
8ae12a0d 2244 * such as the platform bus. The final stage of probe() in that code
8caab75f 2245 * includes calling spi_register_controller() to hook up to this SPI bus glue.
8ae12a0d
DB
2246 *
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
8caab75f 2254 * success, else a negative error code (dropping the controller's refcount).
0c868461 2255 * After a successful return, the caller is responsible for calling
8caab75f 2256 * spi_unregister_controller().
97d56dc6
JMC
2257 *
2258 * Return: zero on success, else a negative error code.
8ae12a0d 2259 */
8caab75f 2260int spi_register_controller(struct spi_controller *ctlr)
8ae12a0d 2261{
8caab75f 2262 struct device *dev = ctlr->dev.parent;
2b9603a0 2263 struct boardinfo *bi;
8ae12a0d 2264 int status = -ENODEV;
42bdd706 2265 int id, first_dynamic;
8ae12a0d 2266
0c868461
DB
2267 if (!dev)
2268 return -ENODEV;
2269
bdf3a3b5
BB
2270 /*
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
8caab75f 2278 if (!spi_controller_is_slave(ctlr)) {
f3186dd8
LW
2279 if (ctlr->use_gpio_descriptors) {
2280 status = spi_get_gpio_descs(ctlr);
2281 if (status)
2282 return status;
2df201e0
LW
2283 /*
2284 * A controller using GPIO descriptors always
2285 * supports SPI_CS_HIGH if need be.
2286 */
2287 ctlr->mode_bits |= SPI_CS_HIGH;
f3186dd8
LW
2288 } else {
2289 /* Legacy code path for GPIOs from DT */
2290 status = of_spi_register_master(ctlr);
2291 if (status)
2292 return status;
2293 }
6c364062 2294 }
74317984 2295
082c8cb4
DB
2296 /* even if it's just one always-selected device, there must
2297 * be at least one chipselect
2298 */
8caab75f 2299 if (ctlr->num_chipselect == 0)
082c8cb4 2300 return -EINVAL;
04b2d03a
GU
2301 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 */
9b61e302
SM
2312 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 }
8caab75f 2323 if (ctlr->bus_num < 0) {
42bdd706
LS
2324 first_dynamic = of_alias_get_highest_id("spi");
2325 if (first_dynamic < 0)
2326 first_dynamic = 0;
2327 else
2328 first_dynamic++;
2329
9a9a047a 2330 mutex_lock(&board_lock);
42bdd706
LS
2331 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
2332 0, GFP_KERNEL);
9a9a047a
SM
2333 mutex_unlock(&board_lock);
2334 if (WARN(id < 0, "couldn't get idr"))
2335 return id;
2336 ctlr->bus_num = id;
8ae12a0d 2337 }
8caab75f
GU
2338 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;
cf32b71e 2347
8ae12a0d
DB
2348 /* register the device, then userspace will see it.
2349 * registration fails if the bus ID is in use.
2350 */
8caab75f
GU
2351 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
2352 status = device_add(&ctlr->dev);
9b61e302
SM
2353 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);
8ae12a0d 2358 goto done;
9b61e302
SM
2359 }
2360 dev_dbg(dev, "registered %s %s\n",
8caab75f 2361 spi_controller_is_slave(ctlr) ? "slave" : "master",
9b61e302 2362 dev_name(&ctlr->dev));
8ae12a0d 2363
b5932f5c
BB
2364 /*
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) {
8caab75f 2370 dev_info(dev, "controller is unqueued, this is deprecated\n");
b5932f5c 2371 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
8caab75f 2372 status = spi_controller_initialize_queue(ctlr);
ffbbdd21 2373 if (status) {
8caab75f 2374 device_del(&ctlr->dev);
9b61e302
SM
2375 /* free bus id */
2376 mutex_lock(&board_lock);
2377 idr_remove(&spi_master_idr, ctlr->bus_num);
2378 mutex_unlock(&board_lock);
ffbbdd21
LW
2379 goto done;
2380 }
2381 }
eca2ebc7 2382 /* add statistics */
8caab75f 2383 spin_lock_init(&ctlr->statistics.lock);
ffbbdd21 2384
2b9603a0 2385 mutex_lock(&board_lock);
8caab75f 2386 list_add_tail(&ctlr->list, &spi_controller_list);
2b9603a0 2387 list_for_each_entry(bi, &board_list, list)
8caab75f 2388 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
2b9603a0
FT
2389 mutex_unlock(&board_lock);
2390
64bee4d2 2391 /* Register devices from the device tree and ACPI */
8caab75f
GU
2392 of_register_spi_devices(ctlr);
2393 acpi_register_spi_devices(ctlr);
8ae12a0d
DB
2394done:
2395 return status;
2396}
8caab75f 2397EXPORT_SYMBOL_GPL(spi_register_controller);
8ae12a0d 2398
666d5b4c
MB
2399static void devm_spi_unregister(struct device *dev, void *res)
2400{
8caab75f 2401 spi_unregister_controller(*(struct spi_controller **)res);
666d5b4c
MB
2402}
2403
2404/**
8caab75f
GU
2405 * 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()
666d5b4c
MB
2410 * Context: can sleep
2411 *
8caab75f 2412 * Register a SPI device as with spi_register_controller() which will
68b892f1 2413 * automatically be unregistered and freed.
97d56dc6
JMC
2414 *
2415 * Return: zero on success, else a negative error code.
666d5b4c 2416 */
8caab75f
GU
2417int devm_spi_register_controller(struct device *dev,
2418 struct spi_controller *ctlr)
666d5b4c 2419{
8caab75f 2420 struct spi_controller **ptr;
666d5b4c
MB
2421 int ret;
2422
2423 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2424 if (!ptr)
2425 return -ENOMEM;
2426
8caab75f 2427 ret = spi_register_controller(ctlr);
4b92894e 2428 if (!ret) {
8caab75f 2429 *ptr = ctlr;
666d5b4c
MB
2430 devres_add(dev, ptr);
2431 } else {
2432 devres_free(ptr);
2433 }
2434
2435 return ret;
2436}
8caab75f 2437EXPORT_SYMBOL_GPL(devm_spi_register_controller);
666d5b4c 2438
34860089 2439static int __unregister(struct device *dev, void *null)
8ae12a0d 2440{
34860089 2441 spi_unregister_device(to_spi_device(dev));
8ae12a0d
DB
2442 return 0;
2443}
2444
2445/**
8caab75f
GU
2446 * spi_unregister_controller - unregister SPI master or slave controller
2447 * @ctlr: the controller being unregistered
33e34dc6 2448 * Context: can sleep
8ae12a0d 2449 *
8caab75f 2450 * This call is used only by SPI controller drivers, which are the
8ae12a0d
DB
2451 * only ones directly touching chip registers.
2452 *
2453 * This must be called from context that can sleep.
68b892f1
JH
2454 *
2455 * Note that this function also drops a reference to the controller.
8ae12a0d 2456 */
8caab75f 2457void spi_unregister_controller(struct spi_controller *ctlr)
8ae12a0d 2458{
9b61e302 2459 struct spi_controller *found;
67f7b278 2460 int id = ctlr->bus_num;
89fc9a1a
JG
2461 int dummy;
2462
9b61e302
SM
2463 /* First make sure that this controller was ever added */
2464 mutex_lock(&board_lock);
67f7b278 2465 found = idr_find(&spi_master_idr, id);
9b61e302 2466 mutex_unlock(&board_lock);
8caab75f
GU
2467 if (ctlr->queued) {
2468 if (spi_destroy_queue(ctlr))
2469 dev_err(&ctlr->dev, "queue remove failed\n");
ffbbdd21 2470 }
2b9603a0 2471 mutex_lock(&board_lock);
8caab75f 2472 list_del(&ctlr->list);
2b9603a0
FT
2473 mutex_unlock(&board_lock);
2474
8caab75f
GU
2475 dummy = device_for_each_child(&ctlr->dev, NULL, __unregister);
2476 device_unregister(&ctlr->dev);
9b61e302
SM
2477 /* free bus id */
2478 mutex_lock(&board_lock);
613bd1ea
JN
2479 if (found == ctlr)
2480 idr_remove(&spi_master_idr, id);
9b61e302 2481 mutex_unlock(&board_lock);
8ae12a0d 2482}
8caab75f 2483EXPORT_SYMBOL_GPL(spi_unregister_controller);
8ae12a0d 2484
8caab75f 2485int spi_controller_suspend(struct spi_controller *ctlr)
ffbbdd21
LW
2486{
2487 int ret;
2488
8caab75f
GU
2489 /* Basically no-ops for non-queued controllers */
2490 if (!ctlr->queued)
ffbbdd21
LW
2491 return 0;
2492
8caab75f 2493 ret = spi_stop_queue(ctlr);
ffbbdd21 2494 if (ret)
8caab75f 2495 dev_err(&ctlr->dev, "queue stop failed\n");
ffbbdd21
LW
2496
2497 return ret;
2498}
8caab75f 2499EXPORT_SYMBOL_GPL(spi_controller_suspend);
ffbbdd21 2500
8caab75f 2501int spi_controller_resume(struct spi_controller *ctlr)
ffbbdd21
LW
2502{
2503 int ret;
2504
8caab75f 2505 if (!ctlr->queued)
ffbbdd21
LW
2506 return 0;
2507
8caab75f 2508 ret = spi_start_queue(ctlr);
ffbbdd21 2509 if (ret)
8caab75f 2510 dev_err(&ctlr->dev, "queue restart failed\n");
ffbbdd21
LW
2511
2512 return ret;
2513}
8caab75f 2514EXPORT_SYMBOL_GPL(spi_controller_resume);
ffbbdd21 2515
8caab75f 2516static int __spi_controller_match(struct device *dev, const void *data)
5ed2c832 2517{
8caab75f 2518 struct spi_controller *ctlr;
9f3b795a 2519 const u16 *bus_num = data;
5ed2c832 2520
8caab75f
GU
2521 ctlr = container_of(dev, struct spi_controller, dev);
2522 return ctlr->bus_num == *bus_num;
5ed2c832
DY
2523}
2524
8ae12a0d
DB
2525/**
2526 * spi_busnum_to_master - look up master associated with bus_num
2527 * @bus_num: the master's bus number
33e34dc6 2528 * Context: can sleep
8ae12a0d
DB
2529 *
2530 * This call may be used with devices that are registered after
2531 * arch init time. It returns a refcounted pointer to the relevant
8caab75f 2532 * spi_controller (which the caller must release), or NULL if there is
8ae12a0d 2533 * no such master registered.
97d56dc6
JMC
2534 *
2535 * Return: the SPI master structure on success, else NULL.
8ae12a0d 2536 */
8caab75f 2537struct spi_controller *spi_busnum_to_master(u16 bus_num)
8ae12a0d 2538{
49dce689 2539 struct device *dev;
8caab75f 2540 struct spi_controller *ctlr = NULL;
5ed2c832 2541
695794ae 2542 dev = class_find_device(&spi_master_class, NULL, &bus_num,
8caab75f 2543 __spi_controller_match);
5ed2c832 2544 if (dev)
8caab75f 2545 ctlr = container_of(dev, struct spi_controller, dev);
5ed2c832 2546 /* reference got in class_find_device */
8caab75f 2547 return ctlr;
8ae12a0d
DB
2548}
2549EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2550
d780c371
MS
2551/*-------------------------------------------------------------------------*/
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
8caab75f 2567 * of the @spi_device or @spi_controller to avoid repeated allocations.
d780c371
MS
2568 */
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
8caab75f 2619 * @ctlr: the @spi_controller
d780c371
MS
2620 * @message: the @spi_message
2621 */
8caab75f 2622void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
d780c371
MS
2623{
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)
8caab75f 2631 res->release(ctlr, message, res->data);
d780c371
MS
2632
2633 list_del(&res->entry);
2634
2635 kfree(res);
2636 }
2637}
2638EXPORT_SYMBOL_GPL(spi_res_release);
8ae12a0d
DB
2639
2640/*-------------------------------------------------------------------------*/
2641
523baf5a
MS
2642/* Core methods for spi_message alterations */
2643
8caab75f 2644static void __spi_replace_transfers_release(struct spi_controller *ctlr,
523baf5a
MS
2645 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)
8caab75f 2653 rxfer->release(ctlr, msg, res);
523baf5a
MS
2654
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)
05885397 2673 * @gfp: gfp flags
523baf5a
MS
2674 *
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
8caab75f 2773static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
08933418
FE
2774 struct spi_message *msg,
2775 struct spi_transfer **xferp,
2776 size_t maxsize,
2777 gfp_t gfp)
d9f12122
MS
2778{
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,
7d62f51e 2786 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
d9f12122
MS
2787 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);
657d32ef
DC
2794 if (IS_ERR(srt))
2795 return PTR_ERR(srt);
d9f12122
MS
2796 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 */
c8dab77a 2811 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
d9f12122
MS
2812
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 */
8caab75f 2835 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
d9f12122
MS
2836 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
8caab75f 2847 * @ctlr: the @spi_controller for this transfer
3700ce95
MI
2848 * @msg: the @spi_message to transform
2849 * @maxsize: the maximum when to apply this
10f11a22 2850 * @gfp: GFP allocation flags
d9f12122
MS
2851 *
2852 * Return: status of transformation
2853 */
8caab75f 2854int spi_split_transfers_maxsize(struct spi_controller *ctlr,
d9f12122
MS
2855 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) {
8caab75f
GU
2870 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
2871 maxsize, gfp);
d9f12122
MS
2872 if (ret)
2873 return ret;
2874 }
2875 }
2876
2877 return 0;
2878}
2879EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
8ae12a0d
DB
2880
2881/*-------------------------------------------------------------------------*/
2882
8caab75f 2883/* Core methods for SPI controller protocol drivers. Some of the
7d077197
DB
2884 * other core methods are currently defined as inline functions.
2885 */
2886
8caab75f
GU
2887static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
2888 u8 bits_per_word)
63ab645f 2889{
8caab75f 2890 if (ctlr->bits_per_word_mask) {
63ab645f
SB
2891 /* Only 32 bits fit in the mask */
2892 if (bits_per_word > 32)
2893 return -EINVAL;
8caab75f 2894 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
63ab645f
SB
2895 return -EINVAL;
2896 }
2897
2898 return 0;
2899}
2900
7d077197
DB
2901/**
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.
97d56dc6
JMC
2918 *
2919 * Return: zero on success, else a negative error code.
7d077197
DB
2920 */
2921int spi_setup(struct spi_device *spi)
2922{
83596fbe 2923 unsigned bad_bits, ugly_bits;
5ab8d262 2924 int status;
7d077197 2925
f477b7fb 2926 /* 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 &
6b03061f
YNG
2937 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
2938 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
f477b7fb 2939 return -EINVAL;
e7db06b5 2940 /* help drivers fail *cleanly* when they need options
8caab75f 2941 * that aren't supported with their current controller
cbaa62e0
DL
2942 * SPI_CS_WORD has a fallback software implementation,
2943 * so it is ignored here.
e7db06b5 2944 */
cbaa62e0 2945 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
83596fbe 2946 ugly_bits = bad_bits &
6b03061f
YNG
2947 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
2948 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
83596fbe
GU
2949 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 }
e7db06b5 2956 if (bad_bits) {
eb288a1f 2957 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
e7db06b5
DB
2958 bad_bits);
2959 return -EINVAL;
2960 }
2961
7d077197
DB
2962 if (!spi->bits_per_word)
2963 spi->bits_per_word = 8;
2964
8caab75f
GU
2965 status = __spi_validate_bits_per_word(spi->controller,
2966 spi->bits_per_word);
5ab8d262
AS
2967 if (status)
2968 return status;
63ab645f 2969
052eb2d4 2970 if (!spi->max_speed_hz)
8caab75f 2971 spi->max_speed_hz = spi->controller->max_speed_hz;
052eb2d4 2972
8caab75f
GU
2973 if (spi->controller->setup)
2974 status = spi->controller->setup(spi);
7d077197 2975
abeedb01
FCJ
2976 spi_set_cs(spi, false);
2977
5fe5f05e 2978 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
7d077197
DB
2979 (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
90808738 2991static int __spi_validate(struct spi_device *spi, struct spi_message *message)
cf32b71e 2992{
8caab75f 2993 struct spi_controller *ctlr = spi->controller;
e6811d1d 2994 struct spi_transfer *xfer;
6ea31293 2995 int w_size;
cf32b71e 2996
24a0013a
MB
2997 if (list_empty(&message->transfers))
2998 return -EINVAL;
24a0013a 2999
cbaa62e0 3000 /* If an SPI controller does not support toggling the CS line on each
71388b21
DL
3001 * 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
cbaa62e0
DL
3003 * splitting transfers into one-word transfers and ensuring that
3004 * cs_change is set for each transfer.
3005 */
71388b21 3006 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
f3186dd8 3007 spi->cs_gpiod ||
71388b21 3008 gpio_is_valid(spi->cs_gpio))) {
cbaa62e0
DL
3009 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
cf32b71e
ES
3030 /* 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 */
8caab75f
GU
3035 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
3036 (spi->mode & SPI_3WIRE)) {
3037 unsigned flags = ctlr->flags;
cf32b71e
ES
3038
3039 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3040 if (xfer->rx_buf && xfer->tx_buf)
3041 return -EINVAL;
8caab75f 3042 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
cf32b71e 3043 return -EINVAL;
8caab75f 3044 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
cf32b71e
ES
3045 return -EINVAL;
3046 }
3047 }
3048
e6811d1d 3049 /**
059b8ffe
LD
3050 * Set transfer bits_per_word and max speed as spi device default if
3051 * it is not set for this transfer.
f477b7fb 3052 * Set transfer tx_nbits and rx_nbits as single transfer default
3053 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
b7bb367a
JB
3054 * Ensure transfer word_delay is at least as long as that required by
3055 * device itself.
e6811d1d 3056 */
77e80588 3057 message->frame_length = 0;
e6811d1d 3058 list_for_each_entry(xfer, &message->transfers, transfer_list) {
078726ce 3059 message->frame_length += xfer->len;
e6811d1d
LD
3060 if (!xfer->bits_per_word)
3061 xfer->bits_per_word = spi->bits_per_word;
a6f87fad
AL
3062
3063 if (!xfer->speed_hz)
059b8ffe 3064 xfer->speed_hz = spi->max_speed_hz;
7dc9fbc3 3065 if (!xfer->speed_hz)
8caab75f 3066 xfer->speed_hz = ctlr->max_speed_hz;
a6f87fad 3067
8caab75f
GU
3068 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
3069 xfer->speed_hz = ctlr->max_speed_hz;
56ede94a 3070
8caab75f 3071 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
63ab645f 3072 return -EINVAL;
a2fd4f9f 3073
4d94bd21
II
3074 /*
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
4d94bd21 3085 /* No partial transfers accepted */
6ea31293 3086 if (xfer->len % w_size)
4d94bd21
II
3087 return -EINVAL;
3088
8caab75f
GU
3089 if (xfer->speed_hz && ctlr->min_speed_hz &&
3090 xfer->speed_hz < ctlr->min_speed_hz)
a2fd4f9f 3091 return -EINVAL;
f477b7fb 3092
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:
1afd9989
GU
3098 * 1. check the value matches one of single, dual and quad
3099 * 2. check tx/rx_nbits match the mode in spi_device
f477b7fb 3100 */
db90a441
SP
3101 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;
db90a441 3112 }
f477b7fb 3113 /* check transfer rx_nbits */
db90a441
SP
3114 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;
db90a441 3125 }
b7bb367a
JB
3126
3127 if (xfer->word_delay_usecs < spi->word_delay_usecs)
3128 xfer->word_delay_usecs = spi->word_delay_usecs;
e6811d1d
LD
3129 }
3130
cf32b71e 3131 message->status = -EINPROGRESS;
90808738
MB
3132
3133 return 0;
3134}
3135
3136static int __spi_async(struct spi_device *spi, struct spi_message *message)
3137{
8caab75f 3138 struct spi_controller *ctlr = spi->controller;
90808738 3139
b5932f5c
BB
3140 /*
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
90808738
MB
3147 message->spi = spi;
3148
8caab75f 3149 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
eca2ebc7
MS
3150 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3151
90808738
MB
3152 trace_spi_message_submit(message);
3153
8caab75f 3154 return ctlr->transfer(spi, message);
cf32b71e
ES
3155}
3156
568d0697
DB
3157/**
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.)
97d56dc6
JMC
3185 *
3186 * Return: zero on success, else a negative error code.
568d0697
DB
3187 */
3188int spi_async(struct spi_device *spi, struct spi_message *message)
3189{
8caab75f 3190 struct spi_controller *ctlr = spi->controller;
cf32b71e
ES
3191 int ret;
3192 unsigned long flags;
568d0697 3193
90808738
MB
3194 ret = __spi_validate(spi, message);
3195 if (ret != 0)
3196 return ret;
3197
8caab75f 3198 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
568d0697 3199
8caab75f 3200 if (ctlr->bus_lock_flag)
cf32b71e
ES
3201 ret = -EBUSY;
3202 else
3203 ret = __spi_async(spi, message);
568d0697 3204
8caab75f 3205 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
cf32b71e
ES
3206
3207 return ret;
568d0697
DB
3208}
3209EXPORT_SYMBOL_GPL(spi_async);
3210
cf32b71e
ES
3211/**
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.)
97d56dc6
JMC
3239 *
3240 * Return: zero on success, else a negative error code.
cf32b71e
ES
3241 */
3242int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3243{
8caab75f 3244 struct spi_controller *ctlr = spi->controller;
cf32b71e
ES
3245 int ret;
3246 unsigned long flags;
3247
90808738
MB
3248 ret = __spi_validate(spi, message);
3249 if (ret != 0)
3250 return ret;
3251
8caab75f 3252 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
cf32b71e
ES
3253
3254 ret = __spi_async(spi, message);
3255
8caab75f 3256 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
cf32b71e
ES
3257
3258 return ret;
3259
3260}
3261EXPORT_SYMBOL_GPL(spi_async_locked);
3262
7d077197
DB
3263/*-------------------------------------------------------------------------*/
3264
8caab75f 3265/* Utility methods for SPI protocol drivers, layered on
7d077197
DB
3266 * top of the core. Some other utility methods are defined as
3267 * inline functions.
3268 */
3269
5d870c8e
AM
3270static void spi_complete(void *arg)
3271{
3272 complete(arg);
3273}
3274
ef4d96ec 3275static int __spi_sync(struct spi_device *spi, struct spi_message *message)
cf32b71e
ES
3276{
3277 DECLARE_COMPLETION_ONSTACK(done);
3278 int status;
8caab75f 3279 struct spi_controller *ctlr = spi->controller;
0461a414
MB
3280 unsigned long flags;
3281
3282 status = __spi_validate(spi, message);
3283 if (status != 0)
3284 return status;
cf32b71e
ES
3285
3286 message->complete = spi_complete;
3287 message->context = &done;
0461a414 3288 message->spi = spi;
cf32b71e 3289
8caab75f 3290 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
eca2ebc7
MS
3291 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3292
0461a414
MB
3293 /* 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 */
8caab75f
GU
3298 if (ctlr->transfer == spi_queued_transfer) {
3299 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
0461a414
MB
3300
3301 trace_spi_message_submit(message);
3302
3303 status = __spi_queued_transfer(spi, message, false);
3304
8caab75f 3305 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
0461a414
MB
3306 } else {
3307 status = spi_async_locked(spi, message);
3308 }
cf32b71e 3309
cf32b71e 3310 if (status == 0) {
0461a414
MB
3311 /* Push out the messages in the calling context if we
3312 * can.
3313 */
8caab75f
GU
3314 if (ctlr->transfer == spi_queued_transfer) {
3315 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
eca2ebc7
MS
3316 spi_sync_immediate);
3317 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3318 spi_sync_immediate);
8caab75f 3319 __spi_pump_messages(ctlr, false);
eca2ebc7 3320 }
0461a414 3321
cf32b71e
ES
3322 wait_for_completion(&done);
3323 status = message->status;
3324 }
3325 message->context = NULL;
3326 return status;
3327}
3328
8ae12a0d
DB
3329/**
3330 * spi_sync - blocking/synchronous SPI data transfers
3331 * @spi: device with which data will be exchanged
3332 * @message: describes the data transfers
33e34dc6 3333 * Context: can sleep
8ae12a0d
DB
3334 *
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 *
0c868461
DB
3345 * Also, the caller is guaranteeing that the memory associated with the
3346 * message will not be freed before this call returns.
3347 *
97d56dc6 3348 * Return: zero on success, else a negative error code.
8ae12a0d
DB
3349 */
3350int spi_sync(struct spi_device *spi, struct spi_message *message)
3351{
ef4d96ec
MB
3352 int ret;
3353
8caab75f 3354 mutex_lock(&spi->controller->bus_lock_mutex);
ef4d96ec 3355 ret = __spi_sync(spi, message);
8caab75f 3356 mutex_unlock(&spi->controller->bus_lock_mutex);
ef4d96ec
MB
3357
3358 return ret;
8ae12a0d
DB
3359}
3360EXPORT_SYMBOL_GPL(spi_sync);
3361
cf32b71e
ES
3362/**
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
25985edc 3373 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
cf32b71e
ES
3374 * be released by a spi_bus_unlock call when the exclusive access is over.
3375 *
97d56dc6 3376 * Return: zero on success, else a negative error code.
cf32b71e
ES
3377 */
3378int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3379{
ef4d96ec 3380 return __spi_sync(spi, message);
cf32b71e
ES
3381}
3382EXPORT_SYMBOL_GPL(spi_sync_locked);
3383
3384/**
3385 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
8caab75f 3386 * @ctlr: SPI bus master that should be locked for exclusive bus access
cf32b71e
ES
3387 * 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 *
97d56dc6 3397 * Return: always zero.
cf32b71e 3398 */
8caab75f 3399int spi_bus_lock(struct spi_controller *ctlr)
cf32b71e
ES
3400{
3401 unsigned long flags;
3402
8caab75f 3403 mutex_lock(&ctlr->bus_lock_mutex);
cf32b71e 3404
8caab75f
GU
3405 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3406 ctlr->bus_lock_flag = 1;
3407 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
cf32b71e
ES
3408
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
8caab75f 3417 * @ctlr: SPI bus master that was locked for exclusive bus access
cf32b71e
ES
3418 * 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 *
97d56dc6 3426 * Return: always zero.
cf32b71e 3427 */
8caab75f 3428int spi_bus_unlock(struct spi_controller *ctlr)
cf32b71e 3429{
8caab75f 3430 ctlr->bus_lock_flag = 0;
cf32b71e 3431
8caab75f 3432 mutex_unlock(&ctlr->bus_lock_mutex);
cf32b71e
ES
3433
3434 return 0;
3435}
3436EXPORT_SYMBOL_GPL(spi_bus_unlock);
3437
a9948b61 3438/* portable code must never pass more than 32 bytes */
5fe5f05e 3439#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
8ae12a0d
DB
3440
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
27570497
JP
3448 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3449 * @n_rx: size of rxbuf, in bytes
33e34dc6 3450 * Context: can sleep
8ae12a0d
DB
3451 *
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.
b885244e 3455 * This call may only be used from a context that may sleep.
8ae12a0d 3456 *
0c868461 3457 * Parameters to this routine are always copied using a small buffer;
33e34dc6
DB
3458 * portable code should never use this for more than 32 bytes.
3459 * Performance-sensitive or bulk transfer code should instead use
0c868461 3460 * spi_{async,sync}() calls with dma-safe buffers.
97d56dc6
JMC
3461 *
3462 * Return: zero on success, else a negative error code.
8ae12a0d
DB
3463 */
3464int spi_write_then_read(struct spi_device *spi,
0c4a1590
MB
3465 const void *txbuf, unsigned n_tx,
3466 void *rxbuf, unsigned n_rx)
8ae12a0d 3467{
068f4070 3468 static DEFINE_MUTEX(lock);
8ae12a0d
DB
3469
3470 int status;
3471 struct spi_message message;
bdff549e 3472 struct spi_transfer x[2];
8ae12a0d
DB
3473 u8 *local_buf;
3474
b3a223ee
MB
3475 /* 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.
8ae12a0d 3479 */
b3a223ee 3480 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
2cd94c8a
MB
3481 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3482 GFP_KERNEL | GFP_DMA);
b3a223ee
MB
3483 if (!local_buf)
3484 return -ENOMEM;
3485 } else {
3486 local_buf = buf;
3487 }
8ae12a0d 3488
8275c642 3489 spi_message_init(&message);
5fe5f05e 3490 memset(x, 0, sizeof(x));
bdff549e
DB
3491 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 }
8275c642 3499
8ae12a0d 3500 memcpy(local_buf, txbuf, n_tx);
bdff549e
DB
3501 x[0].tx_buf = local_buf;
3502 x[1].rx_buf = local_buf + n_tx;
8ae12a0d
DB
3503
3504 /* do the i/o */
8ae12a0d 3505 status = spi_sync(spi, &message);
9b938b74 3506 if (status == 0)
bdff549e 3507 memcpy(rxbuf, x[1].rx_buf, n_rx);
8ae12a0d 3508
bdff549e 3509 if (x[0].tx_buf == buf)
068f4070 3510 mutex_unlock(&lock);
8ae12a0d
DB
3511 else
3512 kfree(local_buf);
3513
3514 return status;
3515}
3516EXPORT_SYMBOL_GPL(spi_write_then_read);
3517
3518/*-------------------------------------------------------------------------*/
3519
5f143af7 3520#if IS_ENABLED(CONFIG_OF)
ce79d54a
PA
3521static 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 */
5f143af7 3527struct spi_device *of_find_spi_device_by_node(struct device_node *node)
ce79d54a
PA
3528{
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}
5f143af7
MF
3533EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
3534#endif /* IS_ENABLED(CONFIG_OF) */
ce79d54a 3535
5f143af7 3536#if IS_ENABLED(CONFIG_OF_DYNAMIC)
8caab75f 3537static int __spi_of_controller_match(struct device *dev, const void *data)
ce79d54a
PA
3538{
3539 return dev->of_node == data;
3540}
3541
8caab75f
GU
3542/* 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)
ce79d54a
PA
3544{
3545 struct device *dev;
3546
3547 dev = class_find_device(&spi_master_class, NULL, node,
8caab75f 3548 __spi_of_controller_match);
6c364062
GU
3549 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3550 dev = class_find_device(&spi_slave_class, NULL, node,
8caab75f 3551 __spi_of_controller_match);
ce79d54a
PA
3552 if (!dev)
3553 return NULL;
3554
3555 /* reference got in class_find_device */
8caab75f 3556 return container_of(dev, struct spi_controller, dev);
ce79d54a
PA
3557}
3558
3559static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3560 void *arg)
3561{
3562 struct of_reconfig_data *rd = arg;
8caab75f 3563 struct spi_controller *ctlr;
ce79d54a
PA
3564 struct spi_device *spi;
3565
3566 switch (of_reconfig_get_state_change(action, arg)) {
3567 case OF_RECONFIG_CHANGE_ADD:
8caab75f
GU
3568 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3569 if (ctlr == NULL)
ce79d54a
PA
3570 return NOTIFY_OK; /* not for us */
3571
bd6c1644 3572 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
8caab75f 3573 put_device(&ctlr->dev);
bd6c1644
GU
3574 return NOTIFY_OK;
3575 }
3576
8caab75f
GU
3577 spi = of_register_spi_device(ctlr, rd->dn);
3578 put_device(&ctlr->dev);
ce79d54a
PA
3579
3580 if (IS_ERR(spi)) {
25c56c88
RH
3581 pr_err("%s: failed to create for '%pOF'\n",
3582 __func__, rd->dn);
e0af98a7 3583 of_node_clear_flag(rd->dn, OF_POPULATED);
ce79d54a
PA
3584 return notifier_from_errno(PTR_ERR(spi));
3585 }
3586 break;
3587
3588 case OF_RECONFIG_CHANGE_REMOVE:
bd6c1644
GU
3589 /* already depopulated? */
3590 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3591 return NOTIFY_OK;
3592
ce79d54a
PA
3593 /* 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
7f24467f 3616#if IS_ENABLED(CONFIG_ACPI)
8caab75f 3617static int spi_acpi_controller_match(struct device *dev, const void *data)
7f24467f
OP
3618{
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
8caab75f 3627static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
7f24467f
OP
3628{
3629 struct device *dev;
3630
3631 dev = class_find_device(&spi_master_class, NULL, adev,
8caab75f 3632 spi_acpi_controller_match);
6c364062
GU
3633 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3634 dev = class_find_device(&spi_slave_class, NULL, adev,
8caab75f 3635 spi_acpi_controller_match);
7f24467f
OP
3636 if (!dev)
3637 return NULL;
3638
8caab75f 3639 return container_of(dev, struct spi_controller, dev);
7f24467f
OP
3640}
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;
8caab75f 3655 struct spi_controller *ctlr;
7f24467f
OP
3656 struct spi_device *spi;
3657
3658 switch (value) {
3659 case ACPI_RECONFIG_DEVICE_ADD:
8caab75f
GU
3660 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
3661 if (!ctlr)
7f24467f
OP
3662 break;
3663
8caab75f
GU
3664 acpi_register_spi_device(ctlr, adev);
3665 put_device(&ctlr->dev);
7f24467f
OP
3666 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
8ae12a0d
DB
3690static int __init spi_init(void)
3691{
b885244e
DB
3692 int status;
3693
e94b1766 3694 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
b885244e
DB
3695 if (!buf) {
3696 status = -ENOMEM;
3697 goto err0;
3698 }
3699
3700 status = bus_register(&spi_bus_type);
3701 if (status < 0)
3702 goto err1;
8ae12a0d 3703
b885244e
DB
3704 status = class_register(&spi_master_class);
3705 if (status < 0)
3706 goto err2;
ce79d54a 3707
6c364062
GU
3708 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
3709 status = class_register(&spi_slave_class);
3710 if (status < 0)
3711 goto err3;
3712 }
3713
5267720e 3714 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
ce79d54a 3715 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
7f24467f
OP
3716 if (IS_ENABLED(CONFIG_ACPI))
3717 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
ce79d54a 3718
8ae12a0d 3719 return 0;
b885244e 3720
6c364062
GU
3721err3:
3722 class_unregister(&spi_master_class);
b885244e
DB
3723err2:
3724 bus_unregister(&spi_bus_type);
3725err1:
3726 kfree(buf);
3727 buf = NULL;
3728err0:
3729 return status;
8ae12a0d 3730}
b885244e 3731
8ae12a0d
DB
3732/* board_info is normally registered in arch_initcall(),
3733 * but even essential drivers wait till later
b885244e
DB
3734 *
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.
8ae12a0d 3738 */
673c0c00 3739postcore_initcall(spi_init);
f0125f1a 3740