]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/spi/spi.c
Merge tag 'efi-urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/efi/efi into...
[mirror_ubuntu-artful-kernel.git] / drivers / spi / spi.c
CommitLineData
8ae12a0d 1/*
ca632f55 2 * SPI init/core code
8ae12a0d
DB
3 *
4 * Copyright (C) 2005 David Brownell
d57a4282 5 * Copyright (C) 2008 Secret Lab Technologies Ltd.
8ae12a0d
DB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
8ae12a0d
DB
16 */
17
8ae12a0d
DB
18#include <linux/kernel.h>
19#include <linux/device.h>
20#include <linux/init.h>
21#include <linux/cache.h>
99adef31
MB
22#include <linux/dma-mapping.h>
23#include <linux/dmaengine.h>
94040828 24#include <linux/mutex.h>
2b7a32f7 25#include <linux/of_device.h>
d57a4282 26#include <linux/of_irq.h>
86be408b 27#include <linux/clk/clk-conf.h>
5a0e3ad6 28#include <linux/slab.h>
e0626e38 29#include <linux/mod_devicetable.h>
8ae12a0d 30#include <linux/spi/spi.h>
74317984 31#include <linux/of_gpio.h>
3ae22e8c 32#include <linux/pm_runtime.h>
f48c767c 33#include <linux/pm_domain.h>
025ed130 34#include <linux/export.h>
8bd75c77 35#include <linux/sched/rt.h>
ae7e81c0 36#include <uapi/linux/sched/types.h>
ffbbdd21
LW
37#include <linux/delay.h>
38#include <linux/kthread.h>
64bee4d2
MW
39#include <linux/ioport.h>
40#include <linux/acpi.h>
b1b8153c 41#include <linux/highmem.h>
8ae12a0d 42
56ec1978
MB
43#define CREATE_TRACE_POINTS
44#include <trace/events/spi.h>
45
8ae12a0d
DB
46static void spidev_release(struct device *dev)
47{
0ffa0285 48 struct spi_device *spi = to_spi_device(dev);
8ae12a0d
DB
49
50 /* spi masters may cleanup for released devices */
51 if (spi->master->cleanup)
52 spi->master->cleanup(spi);
53
0c868461 54 spi_master_put(spi->master);
07a389fe 55 kfree(spi);
8ae12a0d
DB
56}
57
58static ssize_t
59modalias_show(struct device *dev, struct device_attribute *a, char *buf)
60{
61 const struct spi_device *spi = to_spi_device(dev);
8c4ff6d0
ZR
62 int len;
63
64 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
65 if (len != -ENODEV)
66 return len;
8ae12a0d 67
d8e328b3 68 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
8ae12a0d 69}
aa7da564 70static DEVICE_ATTR_RO(modalias);
8ae12a0d 71
eca2ebc7
MS
72#define SPI_STATISTICS_ATTRS(field, file) \
73static ssize_t spi_master_##field##_show(struct device *dev, \
74 struct device_attribute *attr, \
75 char *buf) \
76{ \
77 struct spi_master *master = container_of(dev, \
78 struct spi_master, dev); \
79 return spi_statistics_##field##_show(&master->statistics, buf); \
80} \
81static struct device_attribute dev_attr_spi_master_##field = { \
82 .attr = { .name = file, .mode = S_IRUGO }, \
83 .show = spi_master_##field##_show, \
84}; \
85static ssize_t spi_device_##field##_show(struct device *dev, \
86 struct device_attribute *attr, \
87 char *buf) \
88{ \
d1eba93b 89 struct spi_device *spi = to_spi_device(dev); \
eca2ebc7
MS
90 return spi_statistics_##field##_show(&spi->statistics, buf); \
91} \
92static struct device_attribute dev_attr_spi_device_##field = { \
93 .attr = { .name = file, .mode = S_IRUGO }, \
94 .show = spi_device_##field##_show, \
95}
96
97#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
98static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
99 char *buf) \
100{ \
101 unsigned long flags; \
102 ssize_t len; \
103 spin_lock_irqsave(&stat->lock, flags); \
104 len = sprintf(buf, format_string, stat->field); \
105 spin_unlock_irqrestore(&stat->lock, flags); \
106 return len; \
107} \
108SPI_STATISTICS_ATTRS(name, file)
109
110#define SPI_STATISTICS_SHOW(field, format_string) \
111 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
112 field, format_string)
113
114SPI_STATISTICS_SHOW(messages, "%lu");
115SPI_STATISTICS_SHOW(transfers, "%lu");
116SPI_STATISTICS_SHOW(errors, "%lu");
117SPI_STATISTICS_SHOW(timedout, "%lu");
118
119SPI_STATISTICS_SHOW(spi_sync, "%lu");
120SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
121SPI_STATISTICS_SHOW(spi_async, "%lu");
122
123SPI_STATISTICS_SHOW(bytes, "%llu");
124SPI_STATISTICS_SHOW(bytes_rx, "%llu");
125SPI_STATISTICS_SHOW(bytes_tx, "%llu");
126
6b7bc061
MS
127#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
128 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
129 "transfer_bytes_histo_" number, \
130 transfer_bytes_histo[index], "%lu")
131SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
132SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
133SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
134SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
135SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
136SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
137SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
138SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
139SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
140SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
141SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
142SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
143SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
144SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
145SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
146SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
147SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
148
d9f12122
MS
149SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
150
aa7da564
GKH
151static struct attribute *spi_dev_attrs[] = {
152 &dev_attr_modalias.attr,
153 NULL,
8ae12a0d 154};
eca2ebc7
MS
155
156static const struct attribute_group spi_dev_group = {
157 .attrs = spi_dev_attrs,
158};
159
160static struct attribute *spi_device_statistics_attrs[] = {
161 &dev_attr_spi_device_messages.attr,
162 &dev_attr_spi_device_transfers.attr,
163 &dev_attr_spi_device_errors.attr,
164 &dev_attr_spi_device_timedout.attr,
165 &dev_attr_spi_device_spi_sync.attr,
166 &dev_attr_spi_device_spi_sync_immediate.attr,
167 &dev_attr_spi_device_spi_async.attr,
168 &dev_attr_spi_device_bytes.attr,
169 &dev_attr_spi_device_bytes_rx.attr,
170 &dev_attr_spi_device_bytes_tx.attr,
6b7bc061
MS
171 &dev_attr_spi_device_transfer_bytes_histo0.attr,
172 &dev_attr_spi_device_transfer_bytes_histo1.attr,
173 &dev_attr_spi_device_transfer_bytes_histo2.attr,
174 &dev_attr_spi_device_transfer_bytes_histo3.attr,
175 &dev_attr_spi_device_transfer_bytes_histo4.attr,
176 &dev_attr_spi_device_transfer_bytes_histo5.attr,
177 &dev_attr_spi_device_transfer_bytes_histo6.attr,
178 &dev_attr_spi_device_transfer_bytes_histo7.attr,
179 &dev_attr_spi_device_transfer_bytes_histo8.attr,
180 &dev_attr_spi_device_transfer_bytes_histo9.attr,
181 &dev_attr_spi_device_transfer_bytes_histo10.attr,
182 &dev_attr_spi_device_transfer_bytes_histo11.attr,
183 &dev_attr_spi_device_transfer_bytes_histo12.attr,
184 &dev_attr_spi_device_transfer_bytes_histo13.attr,
185 &dev_attr_spi_device_transfer_bytes_histo14.attr,
186 &dev_attr_spi_device_transfer_bytes_histo15.attr,
187 &dev_attr_spi_device_transfer_bytes_histo16.attr,
d9f12122 188 &dev_attr_spi_device_transfers_split_maxsize.attr,
eca2ebc7
MS
189 NULL,
190};
191
192static const struct attribute_group spi_device_statistics_group = {
193 .name = "statistics",
194 .attrs = spi_device_statistics_attrs,
195};
196
197static const struct attribute_group *spi_dev_groups[] = {
198 &spi_dev_group,
199 &spi_device_statistics_group,
200 NULL,
201};
202
203static struct attribute *spi_master_statistics_attrs[] = {
204 &dev_attr_spi_master_messages.attr,
205 &dev_attr_spi_master_transfers.attr,
206 &dev_attr_spi_master_errors.attr,
207 &dev_attr_spi_master_timedout.attr,
208 &dev_attr_spi_master_spi_sync.attr,
209 &dev_attr_spi_master_spi_sync_immediate.attr,
210 &dev_attr_spi_master_spi_async.attr,
211 &dev_attr_spi_master_bytes.attr,
212 &dev_attr_spi_master_bytes_rx.attr,
213 &dev_attr_spi_master_bytes_tx.attr,
6b7bc061
MS
214 &dev_attr_spi_master_transfer_bytes_histo0.attr,
215 &dev_attr_spi_master_transfer_bytes_histo1.attr,
216 &dev_attr_spi_master_transfer_bytes_histo2.attr,
217 &dev_attr_spi_master_transfer_bytes_histo3.attr,
218 &dev_attr_spi_master_transfer_bytes_histo4.attr,
219 &dev_attr_spi_master_transfer_bytes_histo5.attr,
220 &dev_attr_spi_master_transfer_bytes_histo6.attr,
221 &dev_attr_spi_master_transfer_bytes_histo7.attr,
222 &dev_attr_spi_master_transfer_bytes_histo8.attr,
223 &dev_attr_spi_master_transfer_bytes_histo9.attr,
224 &dev_attr_spi_master_transfer_bytes_histo10.attr,
225 &dev_attr_spi_master_transfer_bytes_histo11.attr,
226 &dev_attr_spi_master_transfer_bytes_histo12.attr,
227 &dev_attr_spi_master_transfer_bytes_histo13.attr,
228 &dev_attr_spi_master_transfer_bytes_histo14.attr,
229 &dev_attr_spi_master_transfer_bytes_histo15.attr,
230 &dev_attr_spi_master_transfer_bytes_histo16.attr,
d9f12122 231 &dev_attr_spi_master_transfers_split_maxsize.attr,
eca2ebc7
MS
232 NULL,
233};
234
235static const struct attribute_group spi_master_statistics_group = {
236 .name = "statistics",
237 .attrs = spi_master_statistics_attrs,
238};
239
240static const struct attribute_group *spi_master_groups[] = {
241 &spi_master_statistics_group,
242 NULL,
243};
244
245void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
246 struct spi_transfer *xfer,
247 struct spi_master *master)
248{
249 unsigned long flags;
6b7bc061
MS
250 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
251
252 if (l2len < 0)
253 l2len = 0;
eca2ebc7
MS
254
255 spin_lock_irqsave(&stats->lock, flags);
256
257 stats->transfers++;
6b7bc061 258 stats->transfer_bytes_histo[l2len]++;
eca2ebc7
MS
259
260 stats->bytes += xfer->len;
261 if ((xfer->tx_buf) &&
262 (xfer->tx_buf != master->dummy_tx))
263 stats->bytes_tx += xfer->len;
264 if ((xfer->rx_buf) &&
265 (xfer->rx_buf != master->dummy_rx))
266 stats->bytes_rx += xfer->len;
267
268 spin_unlock_irqrestore(&stats->lock, flags);
269}
270EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
8ae12a0d
DB
271
272/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
273 * and the sysfs version makes coldplug work too.
274 */
275
75368bf6
AV
276static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
277 const struct spi_device *sdev)
278{
279 while (id->name[0]) {
280 if (!strcmp(sdev->modalias, id->name))
281 return id;
282 id++;
283 }
284 return NULL;
285}
286
287const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
288{
289 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
290
291 return spi_match_id(sdrv->id_table, sdev);
292}
293EXPORT_SYMBOL_GPL(spi_get_device_id);
294
8ae12a0d
DB
295static int spi_match_device(struct device *dev, struct device_driver *drv)
296{
297 const struct spi_device *spi = to_spi_device(dev);
75368bf6
AV
298 const struct spi_driver *sdrv = to_spi_driver(drv);
299
2b7a32f7
SA
300 /* Attempt an OF style match */
301 if (of_driver_match_device(dev, drv))
302 return 1;
303
64bee4d2
MW
304 /* Then try ACPI */
305 if (acpi_driver_match_device(dev, drv))
306 return 1;
307
75368bf6
AV
308 if (sdrv->id_table)
309 return !!spi_match_id(sdrv->id_table, spi);
8ae12a0d 310
35f74fca 311 return strcmp(spi->modalias, drv->name) == 0;
8ae12a0d
DB
312}
313
7eff2e7a 314static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
8ae12a0d
DB
315{
316 const struct spi_device *spi = to_spi_device(dev);
8c4ff6d0
ZR
317 int rc;
318
319 rc = acpi_device_uevent_modalias(dev, env);
320 if (rc != -ENODEV)
321 return rc;
8ae12a0d 322
e0626e38 323 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
8ae12a0d
DB
324 return 0;
325}
326
8ae12a0d
DB
327struct bus_type spi_bus_type = {
328 .name = "spi",
aa7da564 329 .dev_groups = spi_dev_groups,
8ae12a0d
DB
330 .match = spi_match_device,
331 .uevent = spi_uevent,
8ae12a0d
DB
332};
333EXPORT_SYMBOL_GPL(spi_bus_type);
334
b885244e
DB
335
336static int spi_drv_probe(struct device *dev)
337{
338 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
44af7927 339 struct spi_device *spi = to_spi_device(dev);
33cf00e5
MW
340 int ret;
341
86be408b
SN
342 ret = of_clk_set_defaults(dev->of_node, false);
343 if (ret)
344 return ret;
345
44af7927
JH
346 if (dev->of_node) {
347 spi->irq = of_irq_get(dev->of_node, 0);
348 if (spi->irq == -EPROBE_DEFER)
349 return -EPROBE_DEFER;
350 if (spi->irq < 0)
351 spi->irq = 0;
352 }
353
676e7c25
UH
354 ret = dev_pm_domain_attach(dev, true);
355 if (ret != -EPROBE_DEFER) {
44af7927 356 ret = sdrv->probe(spi);
676e7c25
UH
357 if (ret)
358 dev_pm_domain_detach(dev, true);
359 }
b885244e 360
33cf00e5 361 return ret;
b885244e
DB
362}
363
364static int spi_drv_remove(struct device *dev)
365{
366 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
33cf00e5
MW
367 int ret;
368
aec35f4e 369 ret = sdrv->remove(to_spi_device(dev));
676e7c25 370 dev_pm_domain_detach(dev, true);
b885244e 371
33cf00e5 372 return ret;
b885244e
DB
373}
374
375static void spi_drv_shutdown(struct device *dev)
376{
377 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
378
379 sdrv->shutdown(to_spi_device(dev));
380}
381
33e34dc6 382/**
ca5d2485 383 * __spi_register_driver - register a SPI driver
88c9321d 384 * @owner: owner module of the driver to register
33e34dc6
DB
385 * @sdrv: the driver to register
386 * Context: can sleep
97d56dc6
JMC
387 *
388 * Return: zero on success, else a negative error code.
33e34dc6 389 */
ca5d2485 390int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
b885244e 391{
ca5d2485 392 sdrv->driver.owner = owner;
b885244e
DB
393 sdrv->driver.bus = &spi_bus_type;
394 if (sdrv->probe)
395 sdrv->driver.probe = spi_drv_probe;
396 if (sdrv->remove)
397 sdrv->driver.remove = spi_drv_remove;
398 if (sdrv->shutdown)
399 sdrv->driver.shutdown = spi_drv_shutdown;
400 return driver_register(&sdrv->driver);
401}
ca5d2485 402EXPORT_SYMBOL_GPL(__spi_register_driver);
b885244e 403
8ae12a0d
DB
404/*-------------------------------------------------------------------------*/
405
406/* SPI devices should normally not be created by SPI device drivers; that
407 * would make them board-specific. Similarly with SPI master drivers.
408 * Device registration normally goes into like arch/.../mach.../board-YYY.c
409 * with other readonly (flashable) information about mainboard devices.
410 */
411
412struct boardinfo {
413 struct list_head list;
2b9603a0 414 struct spi_board_info board_info;
8ae12a0d
DB
415};
416
417static LIST_HEAD(board_list);
2b9603a0
FT
418static LIST_HEAD(spi_master_list);
419
420/*
421 * Used to protect add/del opertion for board_info list and
422 * spi_master list, and their matching process
423 */
94040828 424static DEFINE_MUTEX(board_lock);
8ae12a0d 425
dc87c98e
GL
426/**
427 * spi_alloc_device - Allocate a new SPI device
428 * @master: Controller to which device is connected
429 * Context: can sleep
430 *
431 * Allows a driver to allocate and initialize a spi_device without
432 * registering it immediately. This allows a driver to directly
433 * fill the spi_device with device parameters before calling
434 * spi_add_device() on it.
435 *
436 * Caller is responsible to call spi_add_device() on the returned
437 * spi_device structure to add it to the SPI master. If the caller
438 * needs to discard the spi_device without adding it, then it should
439 * call spi_dev_put() on it.
440 *
97d56dc6 441 * Return: a pointer to the new device, or NULL.
dc87c98e
GL
442 */
443struct spi_device *spi_alloc_device(struct spi_master *master)
444{
445 struct spi_device *spi;
dc87c98e
GL
446
447 if (!spi_master_get(master))
448 return NULL;
449
5fe5f05e 450 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
dc87c98e 451 if (!spi) {
dc87c98e
GL
452 spi_master_put(master);
453 return NULL;
454 }
455
456 spi->master = master;
178db7d3 457 spi->dev.parent = &master->dev;
dc87c98e
GL
458 spi->dev.bus = &spi_bus_type;
459 spi->dev.release = spidev_release;
446411e1 460 spi->cs_gpio = -ENOENT;
eca2ebc7
MS
461
462 spin_lock_init(&spi->statistics.lock);
463
dc87c98e
GL
464 device_initialize(&spi->dev);
465 return spi;
466}
467EXPORT_SYMBOL_GPL(spi_alloc_device);
468
e13ac47b
JN
469static void spi_dev_set_name(struct spi_device *spi)
470{
471 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
472
473 if (adev) {
474 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
475 return;
476 }
477
478 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
479 spi->chip_select);
480}
481
b6fb8d3a
MW
482static int spi_dev_check(struct device *dev, void *data)
483{
484 struct spi_device *spi = to_spi_device(dev);
485 struct spi_device *new_spi = data;
486
487 if (spi->master == new_spi->master &&
488 spi->chip_select == new_spi->chip_select)
489 return -EBUSY;
490 return 0;
491}
492
dc87c98e
GL
493/**
494 * spi_add_device - Add spi_device allocated with spi_alloc_device
495 * @spi: spi_device to register
496 *
497 * Companion function to spi_alloc_device. Devices allocated with
498 * spi_alloc_device can be added onto the spi bus with this function.
499 *
97d56dc6 500 * Return: 0 on success; negative errno on failure
dc87c98e
GL
501 */
502int spi_add_device(struct spi_device *spi)
503{
e48880e0 504 static DEFINE_MUTEX(spi_add_lock);
74317984
JCPV
505 struct spi_master *master = spi->master;
506 struct device *dev = master->dev.parent;
dc87c98e
GL
507 int status;
508
509 /* Chipselects are numbered 0..max; validate. */
74317984 510 if (spi->chip_select >= master->num_chipselect) {
dc87c98e
GL
511 dev_err(dev, "cs%d >= max %d\n",
512 spi->chip_select,
74317984 513 master->num_chipselect);
dc87c98e
GL
514 return -EINVAL;
515 }
516
517 /* Set the bus ID string */
e13ac47b 518 spi_dev_set_name(spi);
e48880e0
DB
519
520 /* We need to make sure there's no other device with this
521 * chipselect **BEFORE** we call setup(), else we'll trash
522 * its configuration. Lock against concurrent add() calls.
523 */
524 mutex_lock(&spi_add_lock);
525
b6fb8d3a
MW
526 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
527 if (status) {
e48880e0
DB
528 dev_err(dev, "chipselect %d already in use\n",
529 spi->chip_select);
e48880e0
DB
530 goto done;
531 }
532
74317984
JCPV
533 if (master->cs_gpios)
534 spi->cs_gpio = master->cs_gpios[spi->chip_select];
535
e48880e0
DB
536 /* Drivers may modify this initial i/o setup, but will
537 * normally rely on the device being setup. Devices
538 * using SPI_CS_HIGH can't coexist well otherwise...
539 */
7d077197 540 status = spi_setup(spi);
dc87c98e 541 if (status < 0) {
eb288a1f
LW
542 dev_err(dev, "can't setup %s, status %d\n",
543 dev_name(&spi->dev), status);
e48880e0 544 goto done;
dc87c98e
GL
545 }
546
e48880e0 547 /* Device may be bound to an active driver when this returns */
dc87c98e 548 status = device_add(&spi->dev);
e48880e0 549 if (status < 0)
eb288a1f
LW
550 dev_err(dev, "can't add %s, status %d\n",
551 dev_name(&spi->dev), status);
e48880e0 552 else
35f74fca 553 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
dc87c98e 554
e48880e0
DB
555done:
556 mutex_unlock(&spi_add_lock);
557 return status;
dc87c98e
GL
558}
559EXPORT_SYMBOL_GPL(spi_add_device);
8ae12a0d 560
33e34dc6
DB
561/**
562 * spi_new_device - instantiate one new SPI device
563 * @master: Controller to which device is connected
564 * @chip: Describes the SPI device
565 * Context: can sleep
566 *
567 * On typical mainboards, this is purely internal; and it's not needed
8ae12a0d
DB
568 * after board init creates the hard-wired devices. Some development
569 * platforms may not be able to use spi_register_board_info though, and
570 * this is exported so that for example a USB or parport based adapter
571 * driver could add devices (which it would learn about out-of-band).
082c8cb4 572 *
97d56dc6 573 * Return: the new device, or NULL.
8ae12a0d 574 */
e9d5a461
AB
575struct spi_device *spi_new_device(struct spi_master *master,
576 struct spi_board_info *chip)
8ae12a0d
DB
577{
578 struct spi_device *proxy;
8ae12a0d
DB
579 int status;
580
082c8cb4
DB
581 /* NOTE: caller did any chip->bus_num checks necessary.
582 *
583 * Also, unless we change the return value convention to use
584 * error-or-pointer (not NULL-or-pointer), troubleshootability
585 * suggests syslogged diagnostics are best here (ugh).
586 */
587
dc87c98e
GL
588 proxy = spi_alloc_device(master);
589 if (!proxy)
8ae12a0d
DB
590 return NULL;
591
102eb975
GL
592 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
593
8ae12a0d
DB
594 proxy->chip_select = chip->chip_select;
595 proxy->max_speed_hz = chip->max_speed_hz;
980a01c9 596 proxy->mode = chip->mode;
8ae12a0d 597 proxy->irq = chip->irq;
102eb975 598 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
8ae12a0d
DB
599 proxy->dev.platform_data = (void *) chip->platform_data;
600 proxy->controller_data = chip->controller_data;
601 proxy->controller_state = NULL;
8ae12a0d 602
dc87c98e 603 status = spi_add_device(proxy);
8ae12a0d 604 if (status < 0) {
dc87c98e
GL
605 spi_dev_put(proxy);
606 return NULL;
8ae12a0d
DB
607 }
608
8ae12a0d
DB
609 return proxy;
610}
611EXPORT_SYMBOL_GPL(spi_new_device);
612
3b1884c2
GU
613/**
614 * spi_unregister_device - unregister a single SPI device
615 * @spi: spi_device to unregister
616 *
617 * Start making the passed SPI device vanish. Normally this would be handled
618 * by spi_unregister_master().
619 */
620void spi_unregister_device(struct spi_device *spi)
621{
bd6c1644
GU
622 if (!spi)
623 return;
624
8324147f 625 if (spi->dev.of_node) {
bd6c1644 626 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
8324147f
JH
627 of_node_put(spi->dev.of_node);
628 }
7f24467f
OP
629 if (ACPI_COMPANION(&spi->dev))
630 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
bd6c1644 631 device_unregister(&spi->dev);
3b1884c2
GU
632}
633EXPORT_SYMBOL_GPL(spi_unregister_device);
634
2b9603a0
FT
635static void spi_match_master_to_boardinfo(struct spi_master *master,
636 struct spi_board_info *bi)
637{
638 struct spi_device *dev;
639
640 if (master->bus_num != bi->bus_num)
641 return;
642
643 dev = spi_new_device(master, bi);
644 if (!dev)
645 dev_err(master->dev.parent, "can't create new device for %s\n",
646 bi->modalias);
647}
648
33e34dc6
DB
649/**
650 * spi_register_board_info - register SPI devices for a given board
651 * @info: array of chip descriptors
652 * @n: how many descriptors are provided
653 * Context: can sleep
654 *
8ae12a0d
DB
655 * Board-specific early init code calls this (probably during arch_initcall)
656 * with segments of the SPI device table. Any device nodes are created later,
657 * after the relevant parent SPI controller (bus_num) is defined. We keep
658 * this table of devices forever, so that reloading a controller driver will
659 * not make Linux forget about these hard-wired devices.
660 *
661 * Other code can also call this, e.g. a particular add-on board might provide
662 * SPI devices through its expansion connector, so code initializing that board
663 * would naturally declare its SPI devices.
664 *
665 * The board info passed can safely be __initdata ... but be careful of
666 * any embedded pointers (platform_data, etc), they're copied as-is.
97d56dc6
JMC
667 *
668 * Return: zero on success, else a negative error code.
8ae12a0d 669 */
fd4a319b 670int spi_register_board_info(struct spi_board_info const *info, unsigned n)
8ae12a0d 671{
2b9603a0
FT
672 struct boardinfo *bi;
673 int i;
8ae12a0d 674
c7908a37
XL
675 if (!n)
676 return -EINVAL;
677
f9bdb7fd 678 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
8ae12a0d
DB
679 if (!bi)
680 return -ENOMEM;
8ae12a0d 681
2b9603a0
FT
682 for (i = 0; i < n; i++, bi++, info++) {
683 struct spi_master *master;
8ae12a0d 684
2b9603a0
FT
685 memcpy(&bi->board_info, info, sizeof(*info));
686 mutex_lock(&board_lock);
687 list_add_tail(&bi->list, &board_list);
688 list_for_each_entry(master, &spi_master_list, list)
689 spi_match_master_to_boardinfo(master, &bi->board_info);
690 mutex_unlock(&board_lock);
8ae12a0d 691 }
2b9603a0
FT
692
693 return 0;
8ae12a0d
DB
694}
695
696/*-------------------------------------------------------------------------*/
697
b158935f
MB
698static void spi_set_cs(struct spi_device *spi, bool enable)
699{
700 if (spi->mode & SPI_CS_HIGH)
701 enable = !enable;
702
8eee6b9d 703 if (gpio_is_valid(spi->cs_gpio)) {
b158935f 704 gpio_set_value(spi->cs_gpio, !enable);
8eee6b9d
TT
705 /* Some SPI masters need both GPIO CS & slave_select */
706 if ((spi->master->flags & SPI_MASTER_GPIO_SS) &&
707 spi->master->set_cs)
708 spi->master->set_cs(spi, !enable);
709 } else if (spi->master->set_cs) {
b158935f 710 spi->master->set_cs(spi, !enable);
8eee6b9d 711 }
b158935f
MB
712}
713
2de440f5 714#ifdef CONFIG_HAS_DMA
6ad45a27
MB
715static int spi_map_buf(struct spi_master *master, struct device *dev,
716 struct sg_table *sgt, void *buf, size_t len,
717 enum dma_data_direction dir)
718{
719 const bool vmalloced_buf = is_vmalloc_addr(buf);
df88e91b 720 unsigned int max_seg_size = dma_get_max_seg_size(dev);
b1b8153c
V
721#ifdef CONFIG_HIGHMEM
722 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
723 (unsigned long)buf < (PKMAP_BASE +
724 (LAST_PKMAP * PAGE_SIZE)));
725#else
726 const bool kmap_buf = false;
727#endif
65598c13
AG
728 int desc_len;
729 int sgs;
6ad45a27 730 struct page *vm_page;
8dd4a016 731 struct scatterlist *sg;
6ad45a27
MB
732 void *sg_buf;
733 size_t min;
734 int i, ret;
735
b1b8153c 736 if (vmalloced_buf || kmap_buf) {
df88e91b 737 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
65598c13 738 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
0569a88f 739 } else if (virt_addr_valid(buf)) {
df88e91b 740 desc_len = min_t(int, max_seg_size, master->max_dma_len);
65598c13 741 sgs = DIV_ROUND_UP(len, desc_len);
0569a88f
V
742 } else {
743 return -EINVAL;
65598c13
AG
744 }
745
6ad45a27
MB
746 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
747 if (ret != 0)
748 return ret;
749
8dd4a016 750 sg = &sgt->sgl[0];
6ad45a27 751 for (i = 0; i < sgs; i++) {
6ad45a27 752
b1b8153c 753 if (vmalloced_buf || kmap_buf) {
65598c13
AG
754 min = min_t(size_t,
755 len, desc_len - offset_in_page(buf));
b1b8153c
V
756 if (vmalloced_buf)
757 vm_page = vmalloc_to_page(buf);
758 else
759 vm_page = kmap_to_page(buf);
6ad45a27
MB
760 if (!vm_page) {
761 sg_free_table(sgt);
762 return -ENOMEM;
763 }
8dd4a016 764 sg_set_page(sg, vm_page,
c1aefbdd 765 min, offset_in_page(buf));
6ad45a27 766 } else {
65598c13 767 min = min_t(size_t, len, desc_len);
6ad45a27 768 sg_buf = buf;
8dd4a016 769 sg_set_buf(sg, sg_buf, min);
6ad45a27
MB
770 }
771
6ad45a27
MB
772 buf += min;
773 len -= min;
8dd4a016 774 sg = sg_next(sg);
6ad45a27
MB
775 }
776
777 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
89e4b66a
GU
778 if (!ret)
779 ret = -ENOMEM;
6ad45a27
MB
780 if (ret < 0) {
781 sg_free_table(sgt);
782 return ret;
783 }
784
785 sgt->nents = ret;
786
787 return 0;
788}
789
790static void spi_unmap_buf(struct spi_master *master, struct device *dev,
791 struct sg_table *sgt, enum dma_data_direction dir)
792{
793 if (sgt->orig_nents) {
794 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
795 sg_free_table(sgt);
796 }
797}
798
2de440f5 799static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
99adef31 800{
99adef31
MB
801 struct device *tx_dev, *rx_dev;
802 struct spi_transfer *xfer;
6ad45a27 803 int ret;
3a2eba9b 804
6ad45a27 805 if (!master->can_dma)
99adef31
MB
806 return 0;
807
c37f45b5
LL
808 if (master->dma_tx)
809 tx_dev = master->dma_tx->device->dev;
810 else
88b0aa54 811 tx_dev = master->dev.parent;
c37f45b5
LL
812
813 if (master->dma_rx)
814 rx_dev = master->dma_rx->device->dev;
815 else
88b0aa54 816 rx_dev = master->dev.parent;
99adef31
MB
817
818 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
819 if (!master->can_dma(master, msg->spi, xfer))
820 continue;
821
822 if (xfer->tx_buf != NULL) {
6ad45a27
MB
823 ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
824 (void *)xfer->tx_buf, xfer->len,
825 DMA_TO_DEVICE);
826 if (ret != 0)
827 return ret;
99adef31
MB
828 }
829
830 if (xfer->rx_buf != NULL) {
6ad45a27
MB
831 ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
832 xfer->rx_buf, xfer->len,
833 DMA_FROM_DEVICE);
834 if (ret != 0) {
835 spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
836 DMA_TO_DEVICE);
837 return ret;
99adef31
MB
838 }
839 }
840 }
841
842 master->cur_msg_mapped = true;
843
844 return 0;
845}
846
4b786458 847static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
99adef31
MB
848{
849 struct spi_transfer *xfer;
850 struct device *tx_dev, *rx_dev;
851
6ad45a27 852 if (!master->cur_msg_mapped || !master->can_dma)
99adef31
MB
853 return 0;
854
c37f45b5
LL
855 if (master->dma_tx)
856 tx_dev = master->dma_tx->device->dev;
857 else
88b0aa54 858 tx_dev = master->dev.parent;
c37f45b5
LL
859
860 if (master->dma_rx)
861 rx_dev = master->dma_rx->device->dev;
862 else
88b0aa54 863 rx_dev = master->dev.parent;
99adef31
MB
864
865 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
866 if (!master->can_dma(master, msg->spi, xfer))
867 continue;
868
6ad45a27
MB
869 spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
870 spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
99adef31
MB
871 }
872
873 return 0;
874}
2de440f5 875#else /* !CONFIG_HAS_DMA */
f4502dd1
V
876static inline int spi_map_buf(struct spi_master *master,
877 struct device *dev, struct sg_table *sgt,
878 void *buf, size_t len,
879 enum dma_data_direction dir)
880{
881 return -EINVAL;
882}
883
884static inline void spi_unmap_buf(struct spi_master *master,
885 struct device *dev, struct sg_table *sgt,
886 enum dma_data_direction dir)
887{
888}
889
2de440f5
GU
890static inline int __spi_map_msg(struct spi_master *master,
891 struct spi_message *msg)
892{
893 return 0;
894}
895
4b786458
MS
896static inline int __spi_unmap_msg(struct spi_master *master,
897 struct spi_message *msg)
2de440f5
GU
898{
899 return 0;
900}
901#endif /* !CONFIG_HAS_DMA */
902
4b786458
MS
903static inline int spi_unmap_msg(struct spi_master *master,
904 struct spi_message *msg)
905{
906 struct spi_transfer *xfer;
907
908 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
909 /*
910 * Restore the original value of tx_buf or rx_buf if they are
911 * NULL.
912 */
913 if (xfer->tx_buf == master->dummy_tx)
914 xfer->tx_buf = NULL;
915 if (xfer->rx_buf == master->dummy_rx)
916 xfer->rx_buf = NULL;
917 }
918
919 return __spi_unmap_msg(master, msg);
920}
921
2de440f5
GU
922static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
923{
924 struct spi_transfer *xfer;
925 void *tmp;
926 unsigned int max_tx, max_rx;
927
928 if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
929 max_tx = 0;
930 max_rx = 0;
931
932 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
933 if ((master->flags & SPI_MASTER_MUST_TX) &&
934 !xfer->tx_buf)
935 max_tx = max(xfer->len, max_tx);
936 if ((master->flags & SPI_MASTER_MUST_RX) &&
937 !xfer->rx_buf)
938 max_rx = max(xfer->len, max_rx);
939 }
940
941 if (max_tx) {
942 tmp = krealloc(master->dummy_tx, max_tx,
943 GFP_KERNEL | GFP_DMA);
944 if (!tmp)
945 return -ENOMEM;
946 master->dummy_tx = tmp;
947 memset(tmp, 0, max_tx);
948 }
949
950 if (max_rx) {
951 tmp = krealloc(master->dummy_rx, max_rx,
952 GFP_KERNEL | GFP_DMA);
953 if (!tmp)
954 return -ENOMEM;
955 master->dummy_rx = tmp;
956 }
957
958 if (max_tx || max_rx) {
959 list_for_each_entry(xfer, &msg->transfers,
960 transfer_list) {
961 if (!xfer->tx_buf)
962 xfer->tx_buf = master->dummy_tx;
963 if (!xfer->rx_buf)
964 xfer->rx_buf = master->dummy_rx;
965 }
966 }
967 }
968
969 return __spi_map_msg(master, msg);
970}
99adef31 971
b158935f
MB
972/*
973 * spi_transfer_one_message - Default implementation of transfer_one_message()
974 *
975 * This is a standard implementation of transfer_one_message() for
8ba811a7 976 * drivers which implement a transfer_one() operation. It provides
b158935f
MB
977 * standard handling of delays and chip select management.
978 */
979static int spi_transfer_one_message(struct spi_master *master,
980 struct spi_message *msg)
981{
982 struct spi_transfer *xfer;
b158935f
MB
983 bool keep_cs = false;
984 int ret = 0;
d0716dde 985 unsigned long long ms = 1;
eca2ebc7
MS
986 struct spi_statistics *statm = &master->statistics;
987 struct spi_statistics *stats = &msg->spi->statistics;
b158935f
MB
988
989 spi_set_cs(msg->spi, true);
990
eca2ebc7
MS
991 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
992 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
993
b158935f
MB
994 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
995 trace_spi_transfer_start(msg, xfer);
996
eca2ebc7
MS
997 spi_statistics_add_transfer_stats(statm, xfer, master);
998 spi_statistics_add_transfer_stats(stats, xfer, master);
999
38ec10f6
MB
1000 if (xfer->tx_buf || xfer->rx_buf) {
1001 reinit_completion(&master->xfer_completion);
b158935f 1002
38ec10f6
MB
1003 ret = master->transfer_one(master, msg->spi, xfer);
1004 if (ret < 0) {
eca2ebc7
MS
1005 SPI_STATISTICS_INCREMENT_FIELD(statm,
1006 errors);
1007 SPI_STATISTICS_INCREMENT_FIELD(stats,
1008 errors);
38ec10f6
MB
1009 dev_err(&msg->spi->dev,
1010 "SPI transfer failed: %d\n", ret);
1011 goto out;
1012 }
b158935f 1013
38ec10f6
MB
1014 if (ret > 0) {
1015 ret = 0;
d0716dde
SW
1016 ms = 8LL * 1000LL * xfer->len;
1017 do_div(ms, xfer->speed_hz);
38ec10f6 1018 ms += ms + 100; /* some tolerance */
16a0ce4e 1019
d0716dde
SW
1020 if (ms > UINT_MAX)
1021 ms = UINT_MAX;
1022
38ec10f6
MB
1023 ms = wait_for_completion_timeout(&master->xfer_completion,
1024 msecs_to_jiffies(ms));
1025 }
16a0ce4e 1026
38ec10f6 1027 if (ms == 0) {
eca2ebc7
MS
1028 SPI_STATISTICS_INCREMENT_FIELD(statm,
1029 timedout);
1030 SPI_STATISTICS_INCREMENT_FIELD(stats,
1031 timedout);
38ec10f6
MB
1032 dev_err(&msg->spi->dev,
1033 "SPI transfer timed out\n");
1034 msg->status = -ETIMEDOUT;
1035 }
1036 } else {
1037 if (xfer->len)
1038 dev_err(&msg->spi->dev,
1039 "Bufferless transfer has length %u\n",
1040 xfer->len);
13a42798 1041 }
b158935f
MB
1042
1043 trace_spi_transfer_stop(msg, xfer);
1044
1045 if (msg->status != -EINPROGRESS)
1046 goto out;
1047
8244bd3a
DK
1048 if (xfer->delay_usecs) {
1049 u16 us = xfer->delay_usecs;
1050
1051 if (us <= 10)
1052 udelay(us);
1053 else
1054 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1055 }
b158935f
MB
1056
1057 if (xfer->cs_change) {
1058 if (list_is_last(&xfer->transfer_list,
1059 &msg->transfers)) {
1060 keep_cs = true;
1061 } else {
0b73aa63
MB
1062 spi_set_cs(msg->spi, false);
1063 udelay(10);
1064 spi_set_cs(msg->spi, true);
b158935f
MB
1065 }
1066 }
1067
1068 msg->actual_length += xfer->len;
1069 }
1070
1071out:
1072 if (ret != 0 || !keep_cs)
1073 spi_set_cs(msg->spi, false);
1074
1075 if (msg->status == -EINPROGRESS)
1076 msg->status = ret;
1077
ff61eb42 1078 if (msg->status && master->handle_err)
b716c4ff
AS
1079 master->handle_err(master, msg);
1080
d780c371
MS
1081 spi_res_release(master, msg);
1082
b158935f
MB
1083 spi_finalize_current_message(master);
1084
1085 return ret;
1086}
1087
1088/**
1089 * spi_finalize_current_transfer - report completion of a transfer
2c675689 1090 * @master: the master reporting completion
b158935f
MB
1091 *
1092 * Called by SPI drivers using the core transfer_one_message()
1093 * implementation to notify it that the current interrupt driven
9e8f4882 1094 * transfer has finished and the next one may be scheduled.
b158935f
MB
1095 */
1096void spi_finalize_current_transfer(struct spi_master *master)
1097{
1098 complete(&master->xfer_completion);
1099}
1100EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1101
ffbbdd21 1102/**
fc9e0f71
MB
1103 * __spi_pump_messages - function which processes spi message queue
1104 * @master: master to process queue for
1105 * @in_kthread: true if we are in the context of the message pump thread
ffbbdd21
LW
1106 *
1107 * This function checks if there is any spi message in the queue that
1108 * needs processing and if so call out to the driver to initialize hardware
1109 * and transfer each message.
1110 *
0461a414
MB
1111 * Note that it is called both from the kthread itself and also from
1112 * inside spi_sync(); the queue extraction handling at the top of the
1113 * function should deal with this safely.
ffbbdd21 1114 */
ef4d96ec 1115static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
ffbbdd21 1116{
ffbbdd21
LW
1117 unsigned long flags;
1118 bool was_busy = false;
1119 int ret;
1120
983aee5d 1121 /* Lock queue */
ffbbdd21 1122 spin_lock_irqsave(&master->queue_lock, flags);
983aee5d
MB
1123
1124 /* Make sure we are not already running a message */
1125 if (master->cur_msg) {
1126 spin_unlock_irqrestore(&master->queue_lock, flags);
1127 return;
1128 }
1129
0461a414
MB
1130 /* If another context is idling the device then defer */
1131 if (master->idling) {
3989144f 1132 kthread_queue_work(&master->kworker, &master->pump_messages);
0461a414
MB
1133 spin_unlock_irqrestore(&master->queue_lock, flags);
1134 return;
1135 }
1136
983aee5d 1137 /* Check if the queue is idle */
ffbbdd21 1138 if (list_empty(&master->queue) || !master->running) {
b0b36b86
BF
1139 if (!master->busy) {
1140 spin_unlock_irqrestore(&master->queue_lock, flags);
1141 return;
ffbbdd21 1142 }
fc9e0f71
MB
1143
1144 /* Only do teardown in the thread */
1145 if (!in_kthread) {
3989144f 1146 kthread_queue_work(&master->kworker,
fc9e0f71
MB
1147 &master->pump_messages);
1148 spin_unlock_irqrestore(&master->queue_lock, flags);
1149 return;
1150 }
1151
ffbbdd21 1152 master->busy = false;
0461a414 1153 master->idling = true;
ffbbdd21 1154 spin_unlock_irqrestore(&master->queue_lock, flags);
0461a414 1155
3a2eba9b
MB
1156 kfree(master->dummy_rx);
1157 master->dummy_rx = NULL;
1158 kfree(master->dummy_tx);
1159 master->dummy_tx = NULL;
b0b36b86
BF
1160 if (master->unprepare_transfer_hardware &&
1161 master->unprepare_transfer_hardware(master))
1162 dev_err(&master->dev,
1163 "failed to unprepare transfer hardware\n");
49834de2
MB
1164 if (master->auto_runtime_pm) {
1165 pm_runtime_mark_last_busy(master->dev.parent);
1166 pm_runtime_put_autosuspend(master->dev.parent);
1167 }
56ec1978 1168 trace_spi_master_idle(master);
ffbbdd21 1169
0461a414
MB
1170 spin_lock_irqsave(&master->queue_lock, flags);
1171 master->idling = false;
ffbbdd21
LW
1172 spin_unlock_irqrestore(&master->queue_lock, flags);
1173 return;
1174 }
ffbbdd21 1175
ffbbdd21
LW
1176 /* Extract head of queue */
1177 master->cur_msg =
a89e2d27 1178 list_first_entry(&master->queue, struct spi_message, queue);
ffbbdd21
LW
1179
1180 list_del_init(&master->cur_msg->queue);
1181 if (master->busy)
1182 was_busy = true;
1183 else
1184 master->busy = true;
1185 spin_unlock_irqrestore(&master->queue_lock, flags);
1186
ef4d96ec
MB
1187 mutex_lock(&master->io_mutex);
1188
49834de2
MB
1189 if (!was_busy && master->auto_runtime_pm) {
1190 ret = pm_runtime_get_sync(master->dev.parent);
1191 if (ret < 0) {
1192 dev_err(&master->dev, "Failed to power device: %d\n",
1193 ret);
764f2166 1194 mutex_unlock(&master->io_mutex);
49834de2
MB
1195 return;
1196 }
1197 }
1198
56ec1978
MB
1199 if (!was_busy)
1200 trace_spi_master_busy(master);
1201
7dfd2bd7 1202 if (!was_busy && master->prepare_transfer_hardware) {
ffbbdd21
LW
1203 ret = master->prepare_transfer_hardware(master);
1204 if (ret) {
1205 dev_err(&master->dev,
1206 "failed to prepare transfer hardware\n");
49834de2
MB
1207
1208 if (master->auto_runtime_pm)
1209 pm_runtime_put(master->dev.parent);
764f2166 1210 mutex_unlock(&master->io_mutex);
ffbbdd21
LW
1211 return;
1212 }
1213 }
1214
56ec1978
MB
1215 trace_spi_message_start(master->cur_msg);
1216
2841a5fc
MB
1217 if (master->prepare_message) {
1218 ret = master->prepare_message(master, master->cur_msg);
1219 if (ret) {
1220 dev_err(&master->dev,
1221 "failed to prepare message: %d\n", ret);
1222 master->cur_msg->status = ret;
1223 spi_finalize_current_message(master);
49023d2e 1224 goto out;
2841a5fc
MB
1225 }
1226 master->cur_msg_prepared = true;
1227 }
1228
99adef31
MB
1229 ret = spi_map_msg(master, master->cur_msg);
1230 if (ret) {
1231 master->cur_msg->status = ret;
1232 spi_finalize_current_message(master);
49023d2e 1233 goto out;
99adef31
MB
1234 }
1235
ffbbdd21
LW
1236 ret = master->transfer_one_message(master, master->cur_msg);
1237 if (ret) {
1238 dev_err(&master->dev,
1f802f82 1239 "failed to transfer one message from queue\n");
49023d2e 1240 goto out;
ffbbdd21 1241 }
49023d2e
JH
1242
1243out:
ef4d96ec 1244 mutex_unlock(&master->io_mutex);
62826970
MB
1245
1246 /* Prod the scheduler in case transfer_one() was busy waiting */
49023d2e
JH
1247 if (!ret)
1248 cond_resched();
ffbbdd21
LW
1249}
1250
fc9e0f71
MB
1251/**
1252 * spi_pump_messages - kthread work function which processes spi message queue
1253 * @work: pointer to kthread work struct contained in the master struct
1254 */
1255static void spi_pump_messages(struct kthread_work *work)
1256{
1257 struct spi_master *master =
1258 container_of(work, struct spi_master, pump_messages);
1259
ef4d96ec 1260 __spi_pump_messages(master, true);
fc9e0f71
MB
1261}
1262
ffbbdd21
LW
1263static int spi_init_queue(struct spi_master *master)
1264{
1265 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1266
ffbbdd21
LW
1267 master->running = false;
1268 master->busy = false;
1269
3989144f 1270 kthread_init_worker(&master->kworker);
ffbbdd21 1271 master->kworker_task = kthread_run(kthread_worker_fn,
f170168b 1272 &master->kworker, "%s",
ffbbdd21
LW
1273 dev_name(&master->dev));
1274 if (IS_ERR(master->kworker_task)) {
1275 dev_err(&master->dev, "failed to create message pump task\n");
98a8f5a0 1276 return PTR_ERR(master->kworker_task);
ffbbdd21 1277 }
3989144f 1278 kthread_init_work(&master->pump_messages, spi_pump_messages);
ffbbdd21
LW
1279
1280 /*
1281 * Master config will indicate if this controller should run the
1282 * message pump with high (realtime) priority to reduce the transfer
1283 * latency on the bus by minimising the delay between a transfer
1284 * request and the scheduling of the message pump thread. Without this
1285 * setting the message pump thread will remain at default priority.
1286 */
1287 if (master->rt) {
1288 dev_info(&master->dev,
1289 "will run message pump with realtime priority\n");
1290 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
1291 }
1292
1293 return 0;
1294}
1295
1296/**
1297 * spi_get_next_queued_message() - called by driver to check for queued
1298 * messages
1299 * @master: the master to check for queued messages
1300 *
1301 * If there are more messages in the queue, the next message is returned from
1302 * this call.
97d56dc6
JMC
1303 *
1304 * Return: the next message in the queue, else NULL if the queue is empty.
ffbbdd21
LW
1305 */
1306struct spi_message *spi_get_next_queued_message(struct spi_master *master)
1307{
1308 struct spi_message *next;
1309 unsigned long flags;
1310
1311 /* get a pointer to the next message, if any */
1312 spin_lock_irqsave(&master->queue_lock, flags);
1cfd97f9
AL
1313 next = list_first_entry_or_null(&master->queue, struct spi_message,
1314 queue);
ffbbdd21
LW
1315 spin_unlock_irqrestore(&master->queue_lock, flags);
1316
1317 return next;
1318}
1319EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1320
1321/**
1322 * spi_finalize_current_message() - the current message is complete
1323 * @master: the master to return the message to
1324 *
1325 * Called by the driver to notify the core that the message in the front of the
1326 * queue is complete and can be removed from the queue.
1327 */
1328void spi_finalize_current_message(struct spi_master *master)
1329{
1330 struct spi_message *mesg;
1331 unsigned long flags;
2841a5fc 1332 int ret;
ffbbdd21
LW
1333
1334 spin_lock_irqsave(&master->queue_lock, flags);
1335 mesg = master->cur_msg;
ffbbdd21
LW
1336 spin_unlock_irqrestore(&master->queue_lock, flags);
1337
99adef31
MB
1338 spi_unmap_msg(master, mesg);
1339
2841a5fc
MB
1340 if (master->cur_msg_prepared && master->unprepare_message) {
1341 ret = master->unprepare_message(master, mesg);
1342 if (ret) {
1343 dev_err(&master->dev,
1344 "failed to unprepare message: %d\n", ret);
1345 }
1346 }
391949b6 1347
8e76ef88
MS
1348 spin_lock_irqsave(&master->queue_lock, flags);
1349 master->cur_msg = NULL;
2841a5fc 1350 master->cur_msg_prepared = false;
3989144f 1351 kthread_queue_work(&master->kworker, &master->pump_messages);
8e76ef88
MS
1352 spin_unlock_irqrestore(&master->queue_lock, flags);
1353
1354 trace_spi_message_done(mesg);
2841a5fc 1355
ffbbdd21
LW
1356 mesg->state = NULL;
1357 if (mesg->complete)
1358 mesg->complete(mesg->context);
1359}
1360EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1361
1362static int spi_start_queue(struct spi_master *master)
1363{
1364 unsigned long flags;
1365
1366 spin_lock_irqsave(&master->queue_lock, flags);
1367
1368 if (master->running || master->busy) {
1369 spin_unlock_irqrestore(&master->queue_lock, flags);
1370 return -EBUSY;
1371 }
1372
1373 master->running = true;
1374 master->cur_msg = NULL;
1375 spin_unlock_irqrestore(&master->queue_lock, flags);
1376
3989144f 1377 kthread_queue_work(&master->kworker, &master->pump_messages);
ffbbdd21
LW
1378
1379 return 0;
1380}
1381
1382static int spi_stop_queue(struct spi_master *master)
1383{
1384 unsigned long flags;
1385 unsigned limit = 500;
1386 int ret = 0;
1387
1388 spin_lock_irqsave(&master->queue_lock, flags);
1389
1390 /*
1391 * This is a bit lame, but is optimized for the common execution path.
1392 * A wait_queue on the master->busy could be used, but then the common
1393 * execution path (pump_messages) would be required to call wake_up or
1394 * friends on every SPI message. Do this instead.
1395 */
1396 while ((!list_empty(&master->queue) || master->busy) && limit--) {
1397 spin_unlock_irqrestore(&master->queue_lock, flags);
f97b26b0 1398 usleep_range(10000, 11000);
ffbbdd21
LW
1399 spin_lock_irqsave(&master->queue_lock, flags);
1400 }
1401
1402 if (!list_empty(&master->queue) || master->busy)
1403 ret = -EBUSY;
1404 else
1405 master->running = false;
1406
1407 spin_unlock_irqrestore(&master->queue_lock, flags);
1408
1409 if (ret) {
1410 dev_warn(&master->dev,
1411 "could not stop message queue\n");
1412 return ret;
1413 }
1414 return ret;
1415}
1416
1417static int spi_destroy_queue(struct spi_master *master)
1418{
1419 int ret;
1420
1421 ret = spi_stop_queue(master);
1422
1423 /*
3989144f 1424 * kthread_flush_worker will block until all work is done.
ffbbdd21
LW
1425 * If the reason that stop_queue timed out is that the work will never
1426 * finish, then it does no good to call flush/stop thread, so
1427 * return anyway.
1428 */
1429 if (ret) {
1430 dev_err(&master->dev, "problem destroying queue\n");
1431 return ret;
1432 }
1433
3989144f 1434 kthread_flush_worker(&master->kworker);
ffbbdd21
LW
1435 kthread_stop(master->kworker_task);
1436
1437 return 0;
1438}
1439
0461a414
MB
1440static int __spi_queued_transfer(struct spi_device *spi,
1441 struct spi_message *msg,
1442 bool need_pump)
ffbbdd21
LW
1443{
1444 struct spi_master *master = spi->master;
1445 unsigned long flags;
1446
1447 spin_lock_irqsave(&master->queue_lock, flags);
1448
1449 if (!master->running) {
1450 spin_unlock_irqrestore(&master->queue_lock, flags);
1451 return -ESHUTDOWN;
1452 }
1453 msg->actual_length = 0;
1454 msg->status = -EINPROGRESS;
1455
1456 list_add_tail(&msg->queue, &master->queue);
0461a414 1457 if (!master->busy && need_pump)
3989144f 1458 kthread_queue_work(&master->kworker, &master->pump_messages);
ffbbdd21
LW
1459
1460 spin_unlock_irqrestore(&master->queue_lock, flags);
1461 return 0;
1462}
1463
0461a414
MB
1464/**
1465 * spi_queued_transfer - transfer function for queued transfers
1466 * @spi: spi device which is requesting transfer
1467 * @msg: spi message which is to handled is queued to driver queue
97d56dc6
JMC
1468 *
1469 * Return: zero on success, else a negative error code.
0461a414
MB
1470 */
1471static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1472{
1473 return __spi_queued_transfer(spi, msg, true);
1474}
1475
ffbbdd21
LW
1476static int spi_master_initialize_queue(struct spi_master *master)
1477{
1478 int ret;
1479
ffbbdd21 1480 master->transfer = spi_queued_transfer;
b158935f
MB
1481 if (!master->transfer_one_message)
1482 master->transfer_one_message = spi_transfer_one_message;
ffbbdd21
LW
1483
1484 /* Initialize and start queue */
1485 ret = spi_init_queue(master);
1486 if (ret) {
1487 dev_err(&master->dev, "problem initializing queue\n");
1488 goto err_init_queue;
1489 }
c3676d5c 1490 master->queued = true;
ffbbdd21
LW
1491 ret = spi_start_queue(master);
1492 if (ret) {
1493 dev_err(&master->dev, "problem starting queue\n");
1494 goto err_start_queue;
1495 }
1496
1497 return 0;
1498
1499err_start_queue:
ffbbdd21 1500 spi_destroy_queue(master);
c3676d5c 1501err_init_queue:
ffbbdd21
LW
1502 return ret;
1503}
1504
1505/*-------------------------------------------------------------------------*/
1506
7cb94361 1507#if defined(CONFIG_OF)
c2e51ac3
GU
1508static int of_spi_parse_dt(struct spi_master *master, struct spi_device *spi,
1509 struct device_node *nc)
aff5e3f8 1510{
aff5e3f8 1511 u32 value;
c2e51ac3 1512 int rc;
aff5e3f8
PA
1513
1514 /* Device address */
1515 rc = of_property_read_u32(nc, "reg", &value);
1516 if (rc) {
1517 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1518 nc->full_name, rc);
c2e51ac3 1519 return rc;
aff5e3f8
PA
1520 }
1521 spi->chip_select = value;
1522
1523 /* Mode (clock phase/polarity/etc.) */
1524 if (of_find_property(nc, "spi-cpha", NULL))
1525 spi->mode |= SPI_CPHA;
1526 if (of_find_property(nc, "spi-cpol", NULL))
1527 spi->mode |= SPI_CPOL;
1528 if (of_find_property(nc, "spi-cs-high", NULL))
1529 spi->mode |= SPI_CS_HIGH;
1530 if (of_find_property(nc, "spi-3wire", NULL))
1531 spi->mode |= SPI_3WIRE;
1532 if (of_find_property(nc, "spi-lsb-first", NULL))
1533 spi->mode |= SPI_LSB_FIRST;
1534
1535 /* Device DUAL/QUAD mode */
1536 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1537 switch (value) {
1538 case 1:
1539 break;
1540 case 2:
1541 spi->mode |= SPI_TX_DUAL;
1542 break;
1543 case 4:
1544 spi->mode |= SPI_TX_QUAD;
1545 break;
1546 default:
1547 dev_warn(&master->dev,
1548 "spi-tx-bus-width %d not supported\n",
1549 value);
1550 break;
1551 }
1552 }
1553
1554 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1555 switch (value) {
1556 case 1:
1557 break;
1558 case 2:
1559 spi->mode |= SPI_RX_DUAL;
1560 break;
1561 case 4:
1562 spi->mode |= SPI_RX_QUAD;
1563 break;
1564 default:
1565 dev_warn(&master->dev,
1566 "spi-rx-bus-width %d not supported\n",
1567 value);
1568 break;
1569 }
1570 }
1571
1572 /* Device speed */
1573 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1574 if (rc) {
1575 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1576 nc->full_name, rc);
c2e51ac3 1577 return rc;
aff5e3f8
PA
1578 }
1579 spi->max_speed_hz = value;
1580
c2e51ac3
GU
1581 return 0;
1582}
1583
1584static struct spi_device *
1585of_register_spi_device(struct spi_master *master, struct device_node *nc)
1586{
1587 struct spi_device *spi;
1588 int rc;
1589
1590 /* Alloc an spi_device */
1591 spi = spi_alloc_device(master);
1592 if (!spi) {
1593 dev_err(&master->dev, "spi_device alloc error for %s\n",
1594 nc->full_name);
1595 rc = -ENOMEM;
1596 goto err_out;
1597 }
1598
1599 /* Select device driver */
1600 rc = of_modalias_node(nc, spi->modalias,
1601 sizeof(spi->modalias));
1602 if (rc < 0) {
1603 dev_err(&master->dev, "cannot find modalias for %s\n",
1604 nc->full_name);
1605 goto err_out;
1606 }
1607
1608 rc = of_spi_parse_dt(master, spi, nc);
1609 if (rc)
1610 goto err_out;
1611
aff5e3f8
PA
1612 /* Store a pointer to the node in the device structure */
1613 of_node_get(nc);
1614 spi->dev.of_node = nc;
1615
1616 /* Register the new device */
aff5e3f8
PA
1617 rc = spi_add_device(spi);
1618 if (rc) {
1619 dev_err(&master->dev, "spi_device register error %s\n",
1620 nc->full_name);
8324147f 1621 goto err_of_node_put;
aff5e3f8
PA
1622 }
1623
1624 return spi;
1625
8324147f
JH
1626err_of_node_put:
1627 of_node_put(nc);
aff5e3f8
PA
1628err_out:
1629 spi_dev_put(spi);
1630 return ERR_PTR(rc);
1631}
1632
d57a4282
GL
1633/**
1634 * of_register_spi_devices() - Register child devices onto the SPI bus
1635 * @master: Pointer to spi_master device
1636 *
1637 * Registers an spi_device for each child node of master node which has a 'reg'
1638 * property.
1639 */
1640static void of_register_spi_devices(struct spi_master *master)
1641{
1642 struct spi_device *spi;
1643 struct device_node *nc;
d57a4282
GL
1644
1645 if (!master->dev.of_node)
1646 return;
1647
f3b6159e 1648 for_each_available_child_of_node(master->dev.of_node, nc) {
bd6c1644
GU
1649 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1650 continue;
aff5e3f8 1651 spi = of_register_spi_device(master, nc);
e0af98a7 1652 if (IS_ERR(spi)) {
aff5e3f8 1653 dev_warn(&master->dev, "Failed to create SPI device for %s\n",
d57a4282 1654 nc->full_name);
e0af98a7
RR
1655 of_node_clear_flag(nc, OF_POPULATED);
1656 }
d57a4282
GL
1657 }
1658}
1659#else
1660static void of_register_spi_devices(struct spi_master *master) { }
1661#endif
1662
64bee4d2
MW
1663#ifdef CONFIG_ACPI
1664static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1665{
1666 struct spi_device *spi = data;
a0a90718 1667 struct spi_master *master = spi->master;
64bee4d2
MW
1668
1669 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1670 struct acpi_resource_spi_serialbus *sb;
1671
1672 sb = &ares->data.spi_serial_bus;
1673 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
a0a90718
MW
1674 /*
1675 * ACPI DeviceSelection numbering is handled by the
1676 * host controller driver in Windows and can vary
1677 * from driver to driver. In Linux we always expect
1678 * 0 .. max - 1 so we need to ask the driver to
1679 * translate between the two schemes.
1680 */
1681 if (master->fw_translate_cs) {
1682 int cs = master->fw_translate_cs(master,
1683 sb->device_selection);
1684 if (cs < 0)
1685 return cs;
1686 spi->chip_select = cs;
1687 } else {
1688 spi->chip_select = sb->device_selection;
1689 }
1690
64bee4d2
MW
1691 spi->max_speed_hz = sb->connection_speed;
1692
1693 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1694 spi->mode |= SPI_CPHA;
1695 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1696 spi->mode |= SPI_CPOL;
1697 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1698 spi->mode |= SPI_CS_HIGH;
1699 }
1700 } else if (spi->irq < 0) {
1701 struct resource r;
1702
1703 if (acpi_dev_resource_interrupt(ares, 0, &r))
1704 spi->irq = r.start;
1705 }
1706
1707 /* Always tell the ACPI core to skip this resource */
1708 return 1;
1709}
1710
7f24467f
OP
1711static acpi_status acpi_register_spi_device(struct spi_master *master,
1712 struct acpi_device *adev)
64bee4d2 1713{
64bee4d2 1714 struct list_head resource_list;
64bee4d2
MW
1715 struct spi_device *spi;
1716 int ret;
1717
7f24467f
OP
1718 if (acpi_bus_get_status(adev) || !adev->status.present ||
1719 acpi_device_enumerated(adev))
64bee4d2
MW
1720 return AE_OK;
1721
1722 spi = spi_alloc_device(master);
1723 if (!spi) {
1724 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1725 dev_name(&adev->dev));
1726 return AE_NO_MEMORY;
1727 }
1728
7b199811 1729 ACPI_COMPANION_SET(&spi->dev, adev);
64bee4d2
MW
1730 spi->irq = -1;
1731
1732 INIT_LIST_HEAD(&resource_list);
1733 ret = acpi_dev_get_resources(adev, &resource_list,
1734 acpi_spi_add_resource, spi);
1735 acpi_dev_free_resource_list(&resource_list);
1736
1737 if (ret < 0 || !spi->max_speed_hz) {
1738 spi_dev_put(spi);
1739 return AE_OK;
1740 }
1741
0c6543f6
DD
1742 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
1743 sizeof(spi->modalias));
1744
33ada67d
CR
1745 if (spi->irq < 0)
1746 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1747
7f24467f
OP
1748 acpi_device_set_enumerated(adev);
1749
33cf00e5 1750 adev->power.flags.ignore_parent = true;
64bee4d2 1751 if (spi_add_device(spi)) {
33cf00e5 1752 adev->power.flags.ignore_parent = false;
64bee4d2
MW
1753 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1754 dev_name(&adev->dev));
1755 spi_dev_put(spi);
1756 }
1757
1758 return AE_OK;
1759}
1760
7f24467f
OP
1761static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1762 void *data, void **return_value)
1763{
1764 struct spi_master *master = data;
1765 struct acpi_device *adev;
1766
1767 if (acpi_bus_get_device(handle, &adev))
1768 return AE_OK;
1769
1770 return acpi_register_spi_device(master, adev);
1771}
1772
64bee4d2
MW
1773static void acpi_register_spi_devices(struct spi_master *master)
1774{
1775 acpi_status status;
1776 acpi_handle handle;
1777
29896178 1778 handle = ACPI_HANDLE(master->dev.parent);
64bee4d2
MW
1779 if (!handle)
1780 return;
1781
1782 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1783 acpi_spi_add_device, NULL,
1784 master, NULL);
1785 if (ACPI_FAILURE(status))
1786 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1787}
1788#else
1789static inline void acpi_register_spi_devices(struct spi_master *master) {}
1790#endif /* CONFIG_ACPI */
1791
49dce689 1792static void spi_master_release(struct device *dev)
8ae12a0d
DB
1793{
1794 struct spi_master *master;
1795
49dce689 1796 master = container_of(dev, struct spi_master, dev);
8ae12a0d
DB
1797 kfree(master);
1798}
1799
1800static struct class spi_master_class = {
1801 .name = "spi_master",
1802 .owner = THIS_MODULE,
49dce689 1803 .dev_release = spi_master_release,
eca2ebc7 1804 .dev_groups = spi_master_groups,
8ae12a0d
DB
1805};
1806
1807
1808/**
1809 * spi_alloc_master - allocate SPI master controller
1810 * @dev: the controller, possibly using the platform_bus
33e34dc6 1811 * @size: how much zeroed driver-private data to allocate; the pointer to this
49dce689 1812 * memory is in the driver_data field of the returned device,
0c868461 1813 * accessible with spi_master_get_devdata().
33e34dc6 1814 * Context: can sleep
8ae12a0d
DB
1815 *
1816 * This call is used only by SPI master controller drivers, which are the
1817 * only ones directly touching chip registers. It's how they allocate
ba1a0513 1818 * an spi_master structure, prior to calling spi_register_master().
8ae12a0d 1819 *
97d56dc6 1820 * This must be called from context that can sleep.
8ae12a0d
DB
1821 *
1822 * The caller is responsible for assigning the bus number and initializing
ba1a0513 1823 * the master's methods before calling spi_register_master(); and (after errors
a394d635 1824 * adding the device) calling spi_master_put() to prevent a memory leak.
97d56dc6
JMC
1825 *
1826 * Return: the SPI master structure on success, else NULL.
8ae12a0d 1827 */
e9d5a461 1828struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
8ae12a0d
DB
1829{
1830 struct spi_master *master;
1831
0c868461
DB
1832 if (!dev)
1833 return NULL;
1834
5fe5f05e 1835 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
8ae12a0d
DB
1836 if (!master)
1837 return NULL;
1838
49dce689 1839 device_initialize(&master->dev);
1e8a52e1
GL
1840 master->bus_num = -1;
1841 master->num_chipselect = 1;
49dce689 1842 master->dev.class = &spi_master_class;
157f38f9 1843 master->dev.parent = dev;
d7e2ee25 1844 pm_suspend_ignore_children(&master->dev, true);
0c868461 1845 spi_master_set_devdata(master, &master[1]);
8ae12a0d
DB
1846
1847 return master;
1848}
1849EXPORT_SYMBOL_GPL(spi_alloc_master);
1850
74317984
JCPV
1851#ifdef CONFIG_OF
1852static int of_spi_register_master(struct spi_master *master)
1853{
e80beb27 1854 int nb, i, *cs;
74317984
JCPV
1855 struct device_node *np = master->dev.of_node;
1856
1857 if (!np)
1858 return 0;
1859
1860 nb = of_gpio_named_count(np, "cs-gpios");
5fe5f05e 1861 master->num_chipselect = max_t(int, nb, master->num_chipselect);
74317984 1862
8ec5d84e
AL
1863 /* Return error only for an incorrectly formed cs-gpios property */
1864 if (nb == 0 || nb == -ENOENT)
74317984 1865 return 0;
8ec5d84e
AL
1866 else if (nb < 0)
1867 return nb;
74317984
JCPV
1868
1869 cs = devm_kzalloc(&master->dev,
1870 sizeof(int) * master->num_chipselect,
1871 GFP_KERNEL);
1872 master->cs_gpios = cs;
1873
1874 if (!master->cs_gpios)
1875 return -ENOMEM;
1876
0da83bb1 1877 for (i = 0; i < master->num_chipselect; i++)
446411e1 1878 cs[i] = -ENOENT;
74317984
JCPV
1879
1880 for (i = 0; i < nb; i++)
1881 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1882
1883 return 0;
1884}
1885#else
1886static int of_spi_register_master(struct spi_master *master)
1887{
1888 return 0;
1889}
1890#endif
1891
8ae12a0d
DB
1892/**
1893 * spi_register_master - register SPI master controller
1894 * @master: initialized master, originally from spi_alloc_master()
33e34dc6 1895 * Context: can sleep
8ae12a0d
DB
1896 *
1897 * SPI master controllers connect to their drivers using some non-SPI bus,
1898 * such as the platform bus. The final stage of probe() in that code
1899 * includes calling spi_register_master() to hook up to this SPI bus glue.
1900 *
1901 * SPI controllers use board specific (often SOC specific) bus numbers,
1902 * and board-specific addressing for SPI devices combines those numbers
1903 * with chip select numbers. Since SPI does not directly support dynamic
1904 * device identification, boards need configuration tables telling which
1905 * chip is at which address.
1906 *
1907 * This must be called from context that can sleep. It returns zero on
1908 * success, else a negative error code (dropping the master's refcount).
0c868461
DB
1909 * After a successful return, the caller is responsible for calling
1910 * spi_unregister_master().
97d56dc6
JMC
1911 *
1912 * Return: zero on success, else a negative error code.
8ae12a0d 1913 */
e9d5a461 1914int spi_register_master(struct spi_master *master)
8ae12a0d 1915{
e44a45ae 1916 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
49dce689 1917 struct device *dev = master->dev.parent;
2b9603a0 1918 struct boardinfo *bi;
8ae12a0d
DB
1919 int status = -ENODEV;
1920 int dynamic = 0;
1921
0c868461
DB
1922 if (!dev)
1923 return -ENODEV;
1924
74317984
JCPV
1925 status = of_spi_register_master(master);
1926 if (status)
1927 return status;
1928
082c8cb4
DB
1929 /* even if it's just one always-selected device, there must
1930 * be at least one chipselect
1931 */
1932 if (master->num_chipselect == 0)
1933 return -EINVAL;
1934
bb29785e
GL
1935 if ((master->bus_num < 0) && master->dev.of_node)
1936 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1937
8ae12a0d 1938 /* convention: dynamically assigned bus IDs count down from the max */
a020ed75 1939 if (master->bus_num < 0) {
082c8cb4
DB
1940 /* FIXME switch to an IDR based scheme, something like
1941 * I2C now uses, so we can't run out of "dynamic" IDs
1942 */
8ae12a0d 1943 master->bus_num = atomic_dec_return(&dyn_bus_id);
b885244e 1944 dynamic = 1;
8ae12a0d
DB
1945 }
1946
5424d43e
MB
1947 INIT_LIST_HEAD(&master->queue);
1948 spin_lock_init(&master->queue_lock);
cf32b71e
ES
1949 spin_lock_init(&master->bus_lock_spinlock);
1950 mutex_init(&master->bus_lock_mutex);
ef4d96ec 1951 mutex_init(&master->io_mutex);
cf32b71e 1952 master->bus_lock_flag = 0;
b158935f 1953 init_completion(&master->xfer_completion);
6ad45a27
MB
1954 if (!master->max_dma_len)
1955 master->max_dma_len = INT_MAX;
cf32b71e 1956
8ae12a0d
DB
1957 /* register the device, then userspace will see it.
1958 * registration fails if the bus ID is in use.
1959 */
35f74fca 1960 dev_set_name(&master->dev, "spi%u", master->bus_num);
49dce689 1961 status = device_add(&master->dev);
b885244e 1962 if (status < 0)
8ae12a0d 1963 goto done;
35f74fca 1964 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
8ae12a0d
DB
1965 dynamic ? " (dynamic)" : "");
1966
ffbbdd21
LW
1967 /* If we're using a queued driver, start the queue */
1968 if (master->transfer)
1969 dev_info(dev, "master is unqueued, this is deprecated\n");
1970 else {
1971 status = spi_master_initialize_queue(master);
1972 if (status) {
e93b0724 1973 device_del(&master->dev);
ffbbdd21
LW
1974 goto done;
1975 }
1976 }
eca2ebc7
MS
1977 /* add statistics */
1978 spin_lock_init(&master->statistics.lock);
ffbbdd21 1979
2b9603a0
FT
1980 mutex_lock(&board_lock);
1981 list_add_tail(&master->list, &spi_master_list);
1982 list_for_each_entry(bi, &board_list, list)
1983 spi_match_master_to_boardinfo(master, &bi->board_info);
1984 mutex_unlock(&board_lock);
1985
64bee4d2 1986 /* Register devices from the device tree and ACPI */
12b15e83 1987 of_register_spi_devices(master);
64bee4d2 1988 acpi_register_spi_devices(master);
8ae12a0d
DB
1989done:
1990 return status;
1991}
1992EXPORT_SYMBOL_GPL(spi_register_master);
1993
666d5b4c
MB
1994static void devm_spi_unregister(struct device *dev, void *res)
1995{
1996 spi_unregister_master(*(struct spi_master **)res);
1997}
1998
1999/**
2000 * dev_spi_register_master - register managed SPI master controller
2001 * @dev: device managing SPI master
2002 * @master: initialized master, originally from spi_alloc_master()
2003 * Context: can sleep
2004 *
2005 * Register a SPI device as with spi_register_master() which will
2006 * automatically be unregister
97d56dc6
JMC
2007 *
2008 * Return: zero on success, else a negative error code.
666d5b4c
MB
2009 */
2010int devm_spi_register_master(struct device *dev, struct spi_master *master)
2011{
2012 struct spi_master **ptr;
2013 int ret;
2014
2015 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2016 if (!ptr)
2017 return -ENOMEM;
2018
2019 ret = spi_register_master(master);
4b92894e 2020 if (!ret) {
666d5b4c
MB
2021 *ptr = master;
2022 devres_add(dev, ptr);
2023 } else {
2024 devres_free(ptr);
2025 }
2026
2027 return ret;
2028}
2029EXPORT_SYMBOL_GPL(devm_spi_register_master);
2030
34860089 2031static int __unregister(struct device *dev, void *null)
8ae12a0d 2032{
34860089 2033 spi_unregister_device(to_spi_device(dev));
8ae12a0d
DB
2034 return 0;
2035}
2036
2037/**
2038 * spi_unregister_master - unregister SPI master controller
2039 * @master: the master being unregistered
33e34dc6 2040 * Context: can sleep
8ae12a0d
DB
2041 *
2042 * This call is used only by SPI master controller drivers, which are the
2043 * only ones directly touching chip registers.
2044 *
2045 * This must be called from context that can sleep.
2046 */
2047void spi_unregister_master(struct spi_master *master)
2048{
89fc9a1a
JG
2049 int dummy;
2050
ffbbdd21
LW
2051 if (master->queued) {
2052 if (spi_destroy_queue(master))
2053 dev_err(&master->dev, "queue remove failed\n");
2054 }
2055
2b9603a0
FT
2056 mutex_lock(&board_lock);
2057 list_del(&master->list);
2058 mutex_unlock(&board_lock);
2059
97dbf37d 2060 dummy = device_for_each_child(&master->dev, NULL, __unregister);
49dce689 2061 device_unregister(&master->dev);
8ae12a0d
DB
2062}
2063EXPORT_SYMBOL_GPL(spi_unregister_master);
2064
ffbbdd21
LW
2065int spi_master_suspend(struct spi_master *master)
2066{
2067 int ret;
2068
2069 /* Basically no-ops for non-queued masters */
2070 if (!master->queued)
2071 return 0;
2072
2073 ret = spi_stop_queue(master);
2074 if (ret)
2075 dev_err(&master->dev, "queue stop failed\n");
2076
2077 return ret;
2078}
2079EXPORT_SYMBOL_GPL(spi_master_suspend);
2080
2081int spi_master_resume(struct spi_master *master)
2082{
2083 int ret;
2084
2085 if (!master->queued)
2086 return 0;
2087
2088 ret = spi_start_queue(master);
2089 if (ret)
2090 dev_err(&master->dev, "queue restart failed\n");
2091
2092 return ret;
2093}
2094EXPORT_SYMBOL_GPL(spi_master_resume);
2095
9f3b795a 2096static int __spi_master_match(struct device *dev, const void *data)
5ed2c832
DY
2097{
2098 struct spi_master *m;
9f3b795a 2099 const u16 *bus_num = data;
5ed2c832
DY
2100
2101 m = container_of(dev, struct spi_master, dev);
2102 return m->bus_num == *bus_num;
2103}
2104
8ae12a0d
DB
2105/**
2106 * spi_busnum_to_master - look up master associated with bus_num
2107 * @bus_num: the master's bus number
33e34dc6 2108 * Context: can sleep
8ae12a0d
DB
2109 *
2110 * This call may be used with devices that are registered after
2111 * arch init time. It returns a refcounted pointer to the relevant
2112 * spi_master (which the caller must release), or NULL if there is
2113 * no such master registered.
97d56dc6
JMC
2114 *
2115 * Return: the SPI master structure on success, else NULL.
8ae12a0d
DB
2116 */
2117struct spi_master *spi_busnum_to_master(u16 bus_num)
2118{
49dce689 2119 struct device *dev;
1e9a51dc 2120 struct spi_master *master = NULL;
5ed2c832 2121
695794ae 2122 dev = class_find_device(&spi_master_class, NULL, &bus_num,
5ed2c832
DY
2123 __spi_master_match);
2124 if (dev)
2125 master = container_of(dev, struct spi_master, dev);
2126 /* reference got in class_find_device */
1e9a51dc 2127 return master;
8ae12a0d
DB
2128}
2129EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2130
d780c371
MS
2131/*-------------------------------------------------------------------------*/
2132
2133/* Core methods for SPI resource management */
2134
2135/**
2136 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2137 * during the processing of a spi_message while using
2138 * spi_transfer_one
2139 * @spi: the spi device for which we allocate memory
2140 * @release: the release code to execute for this resource
2141 * @size: size to alloc and return
2142 * @gfp: GFP allocation flags
2143 *
2144 * Return: the pointer to the allocated data
2145 *
2146 * This may get enhanced in the future to allocate from a memory pool
2147 * of the @spi_device or @spi_master to avoid repeated allocations.
2148 */
2149void *spi_res_alloc(struct spi_device *spi,
2150 spi_res_release_t release,
2151 size_t size, gfp_t gfp)
2152{
2153 struct spi_res *sres;
2154
2155 sres = kzalloc(sizeof(*sres) + size, gfp);
2156 if (!sres)
2157 return NULL;
2158
2159 INIT_LIST_HEAD(&sres->entry);
2160 sres->release = release;
2161
2162 return sres->data;
2163}
2164EXPORT_SYMBOL_GPL(spi_res_alloc);
2165
2166/**
2167 * spi_res_free - free an spi resource
2168 * @res: pointer to the custom data of a resource
2169 *
2170 */
2171void spi_res_free(void *res)
2172{
2173 struct spi_res *sres = container_of(res, struct spi_res, data);
2174
2175 if (!res)
2176 return;
2177
2178 WARN_ON(!list_empty(&sres->entry));
2179 kfree(sres);
2180}
2181EXPORT_SYMBOL_GPL(spi_res_free);
2182
2183/**
2184 * spi_res_add - add a spi_res to the spi_message
2185 * @message: the spi message
2186 * @res: the spi_resource
2187 */
2188void spi_res_add(struct spi_message *message, void *res)
2189{
2190 struct spi_res *sres = container_of(res, struct spi_res, data);
2191
2192 WARN_ON(!list_empty(&sres->entry));
2193 list_add_tail(&sres->entry, &message->resources);
2194}
2195EXPORT_SYMBOL_GPL(spi_res_add);
2196
2197/**
2198 * spi_res_release - release all spi resources for this message
2199 * @master: the @spi_master
2200 * @message: the @spi_message
2201 */
2202void spi_res_release(struct spi_master *master,
2203 struct spi_message *message)
2204{
2205 struct spi_res *res;
2206
2207 while (!list_empty(&message->resources)) {
2208 res = list_last_entry(&message->resources,
2209 struct spi_res, entry);
2210
2211 if (res->release)
2212 res->release(master, message, res->data);
2213
2214 list_del(&res->entry);
2215
2216 kfree(res);
2217 }
2218}
2219EXPORT_SYMBOL_GPL(spi_res_release);
8ae12a0d
DB
2220
2221/*-------------------------------------------------------------------------*/
2222
523baf5a
MS
2223/* Core methods for spi_message alterations */
2224
2225static void __spi_replace_transfers_release(struct spi_master *master,
2226 struct spi_message *msg,
2227 void *res)
2228{
2229 struct spi_replaced_transfers *rxfer = res;
2230 size_t i;
2231
2232 /* call extra callback if requested */
2233 if (rxfer->release)
2234 rxfer->release(master, msg, res);
2235
2236 /* insert replaced transfers back into the message */
2237 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2238
2239 /* remove the formerly inserted entries */
2240 for (i = 0; i < rxfer->inserted; i++)
2241 list_del(&rxfer->inserted_transfers[i].transfer_list);
2242}
2243
2244/**
2245 * spi_replace_transfers - replace transfers with several transfers
2246 * and register change with spi_message.resources
2247 * @msg: the spi_message we work upon
2248 * @xfer_first: the first spi_transfer we want to replace
2249 * @remove: number of transfers to remove
2250 * @insert: the number of transfers we want to insert instead
2251 * @release: extra release code necessary in some circumstances
2252 * @extradatasize: extra data to allocate (with alignment guarantees
2253 * of struct @spi_transfer)
05885397 2254 * @gfp: gfp flags
523baf5a
MS
2255 *
2256 * Returns: pointer to @spi_replaced_transfers,
2257 * PTR_ERR(...) in case of errors.
2258 */
2259struct spi_replaced_transfers *spi_replace_transfers(
2260 struct spi_message *msg,
2261 struct spi_transfer *xfer_first,
2262 size_t remove,
2263 size_t insert,
2264 spi_replaced_release_t release,
2265 size_t extradatasize,
2266 gfp_t gfp)
2267{
2268 struct spi_replaced_transfers *rxfer;
2269 struct spi_transfer *xfer;
2270 size_t i;
2271
2272 /* allocate the structure using spi_res */
2273 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2274 insert * sizeof(struct spi_transfer)
2275 + sizeof(struct spi_replaced_transfers)
2276 + extradatasize,
2277 gfp);
2278 if (!rxfer)
2279 return ERR_PTR(-ENOMEM);
2280
2281 /* the release code to invoke before running the generic release */
2282 rxfer->release = release;
2283
2284 /* assign extradata */
2285 if (extradatasize)
2286 rxfer->extradata =
2287 &rxfer->inserted_transfers[insert];
2288
2289 /* init the replaced_transfers list */
2290 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2291
2292 /* assign the list_entry after which we should reinsert
2293 * the @replaced_transfers - it may be spi_message.messages!
2294 */
2295 rxfer->replaced_after = xfer_first->transfer_list.prev;
2296
2297 /* remove the requested number of transfers */
2298 for (i = 0; i < remove; i++) {
2299 /* if the entry after replaced_after it is msg->transfers
2300 * then we have been requested to remove more transfers
2301 * than are in the list
2302 */
2303 if (rxfer->replaced_after->next == &msg->transfers) {
2304 dev_err(&msg->spi->dev,
2305 "requested to remove more spi_transfers than are available\n");
2306 /* insert replaced transfers back into the message */
2307 list_splice(&rxfer->replaced_transfers,
2308 rxfer->replaced_after);
2309
2310 /* free the spi_replace_transfer structure */
2311 spi_res_free(rxfer);
2312
2313 /* and return with an error */
2314 return ERR_PTR(-EINVAL);
2315 }
2316
2317 /* remove the entry after replaced_after from list of
2318 * transfers and add it to list of replaced_transfers
2319 */
2320 list_move_tail(rxfer->replaced_after->next,
2321 &rxfer->replaced_transfers);
2322 }
2323
2324 /* create copy of the given xfer with identical settings
2325 * based on the first transfer to get removed
2326 */
2327 for (i = 0; i < insert; i++) {
2328 /* we need to run in reverse order */
2329 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2330
2331 /* copy all spi_transfer data */
2332 memcpy(xfer, xfer_first, sizeof(*xfer));
2333
2334 /* add to list */
2335 list_add(&xfer->transfer_list, rxfer->replaced_after);
2336
2337 /* clear cs_change and delay_usecs for all but the last */
2338 if (i) {
2339 xfer->cs_change = false;
2340 xfer->delay_usecs = 0;
2341 }
2342 }
2343
2344 /* set up inserted */
2345 rxfer->inserted = insert;
2346
2347 /* and register it with spi_res/spi_message */
2348 spi_res_add(msg, rxfer);
2349
2350 return rxfer;
2351}
2352EXPORT_SYMBOL_GPL(spi_replace_transfers);
2353
08933418
FE
2354static int __spi_split_transfer_maxsize(struct spi_master *master,
2355 struct spi_message *msg,
2356 struct spi_transfer **xferp,
2357 size_t maxsize,
2358 gfp_t gfp)
d9f12122
MS
2359{
2360 struct spi_transfer *xfer = *xferp, *xfers;
2361 struct spi_replaced_transfers *srt;
2362 size_t offset;
2363 size_t count, i;
2364
2365 /* warn once about this fact that we are splitting a transfer */
2366 dev_warn_once(&msg->spi->dev,
7d62f51e 2367 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
d9f12122
MS
2368 xfer->len, maxsize);
2369
2370 /* calculate how many we have to replace */
2371 count = DIV_ROUND_UP(xfer->len, maxsize);
2372
2373 /* create replacement */
2374 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
657d32ef
DC
2375 if (IS_ERR(srt))
2376 return PTR_ERR(srt);
d9f12122
MS
2377 xfers = srt->inserted_transfers;
2378
2379 /* now handle each of those newly inserted spi_transfers
2380 * note that the replacements spi_transfers all are preset
2381 * to the same values as *xferp, so tx_buf, rx_buf and len
2382 * are all identical (as well as most others)
2383 * so we just have to fix up len and the pointers.
2384 *
2385 * this also includes support for the depreciated
2386 * spi_message.is_dma_mapped interface
2387 */
2388
2389 /* the first transfer just needs the length modified, so we
2390 * run it outside the loop
2391 */
c8dab77a 2392 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
d9f12122
MS
2393
2394 /* all the others need rx_buf/tx_buf also set */
2395 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2396 /* update rx_buf, tx_buf and dma */
2397 if (xfers[i].rx_buf)
2398 xfers[i].rx_buf += offset;
2399 if (xfers[i].rx_dma)
2400 xfers[i].rx_dma += offset;
2401 if (xfers[i].tx_buf)
2402 xfers[i].tx_buf += offset;
2403 if (xfers[i].tx_dma)
2404 xfers[i].tx_dma += offset;
2405
2406 /* update length */
2407 xfers[i].len = min(maxsize, xfers[i].len - offset);
2408 }
2409
2410 /* we set up xferp to the last entry we have inserted,
2411 * so that we skip those already split transfers
2412 */
2413 *xferp = &xfers[count - 1];
2414
2415 /* increment statistics counters */
2416 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2417 transfers_split_maxsize);
2418 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2419 transfers_split_maxsize);
2420
2421 return 0;
2422}
2423
2424/**
2425 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2426 * when an individual transfer exceeds a
2427 * certain size
2428 * @master: the @spi_master for this transfer
3700ce95
MI
2429 * @msg: the @spi_message to transform
2430 * @maxsize: the maximum when to apply this
10f11a22 2431 * @gfp: GFP allocation flags
d9f12122
MS
2432 *
2433 * Return: status of transformation
2434 */
2435int spi_split_transfers_maxsize(struct spi_master *master,
2436 struct spi_message *msg,
2437 size_t maxsize,
2438 gfp_t gfp)
2439{
2440 struct spi_transfer *xfer;
2441 int ret;
2442
2443 /* iterate over the transfer_list,
2444 * but note that xfer is advanced to the last transfer inserted
2445 * to avoid checking sizes again unnecessarily (also xfer does
2446 * potentiall belong to a different list by the time the
2447 * replacement has happened
2448 */
2449 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2450 if (xfer->len > maxsize) {
2451 ret = __spi_split_transfer_maxsize(
2452 master, msg, &xfer, maxsize, gfp);
2453 if (ret)
2454 return ret;
2455 }
2456 }
2457
2458 return 0;
2459}
2460EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
8ae12a0d
DB
2461
2462/*-------------------------------------------------------------------------*/
2463
7d077197
DB
2464/* Core methods for SPI master protocol drivers. Some of the
2465 * other core methods are currently defined as inline functions.
2466 */
2467
63ab645f
SB
2468static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
2469{
2470 if (master->bits_per_word_mask) {
2471 /* Only 32 bits fit in the mask */
2472 if (bits_per_word > 32)
2473 return -EINVAL;
2474 if (!(master->bits_per_word_mask &
2475 SPI_BPW_MASK(bits_per_word)))
2476 return -EINVAL;
2477 }
2478
2479 return 0;
2480}
2481
7d077197
DB
2482/**
2483 * spi_setup - setup SPI mode and clock rate
2484 * @spi: the device whose settings are being modified
2485 * Context: can sleep, and no requests are queued to the device
2486 *
2487 * SPI protocol drivers may need to update the transfer mode if the
2488 * device doesn't work with its default. They may likewise need
2489 * to update clock rates or word sizes from initial values. This function
2490 * changes those settings, and must be called from a context that can sleep.
2491 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
2492 * effect the next time the device is selected and data is transferred to
2493 * or from it. When this function returns, the spi device is deselected.
2494 *
2495 * Note that this call will fail if the protocol driver specifies an option
2496 * that the underlying controller or its driver does not support. For
2497 * example, not all hardware supports wire transfers using nine bit words,
2498 * LSB-first wire encoding, or active-high chipselects.
97d56dc6
JMC
2499 *
2500 * Return: zero on success, else a negative error code.
7d077197
DB
2501 */
2502int spi_setup(struct spi_device *spi)
2503{
83596fbe 2504 unsigned bad_bits, ugly_bits;
5ab8d262 2505 int status;
7d077197 2506
f477b7fb 2507 /* check mode to prevent that DUAL and QUAD set at the same time
2508 */
2509 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
2510 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
2511 dev_err(&spi->dev,
2512 "setup: can not select dual and quad at the same time\n");
2513 return -EINVAL;
2514 }
2515 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
2516 */
2517 if ((spi->mode & SPI_3WIRE) && (spi->mode &
2518 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
2519 return -EINVAL;
e7db06b5
DB
2520 /* help drivers fail *cleanly* when they need options
2521 * that aren't supported with their current master
2522 */
2523 bad_bits = spi->mode & ~spi->master->mode_bits;
83596fbe
GU
2524 ugly_bits = bad_bits &
2525 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
2526 if (ugly_bits) {
2527 dev_warn(&spi->dev,
2528 "setup: ignoring unsupported mode bits %x\n",
2529 ugly_bits);
2530 spi->mode &= ~ugly_bits;
2531 bad_bits &= ~ugly_bits;
2532 }
e7db06b5 2533 if (bad_bits) {
eb288a1f 2534 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
e7db06b5
DB
2535 bad_bits);
2536 return -EINVAL;
2537 }
2538
7d077197
DB
2539 if (!spi->bits_per_word)
2540 spi->bits_per_word = 8;
2541
5ab8d262
AS
2542 status = __spi_validate_bits_per_word(spi->master, spi->bits_per_word);
2543 if (status)
2544 return status;
63ab645f 2545
052eb2d4
AL
2546 if (!spi->max_speed_hz)
2547 spi->max_speed_hz = spi->master->max_speed_hz;
2548
caae070c
LD
2549 if (spi->master->setup)
2550 status = spi->master->setup(spi);
7d077197 2551
abeedb01
FCJ
2552 spi_set_cs(spi, false);
2553
5fe5f05e 2554 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
7d077197
DB
2555 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2556 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2557 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2558 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2559 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2560 spi->bits_per_word, spi->max_speed_hz,
2561 status);
2562
2563 return status;
2564}
2565EXPORT_SYMBOL_GPL(spi_setup);
2566
90808738 2567static int __spi_validate(struct spi_device *spi, struct spi_message *message)
cf32b71e
ES
2568{
2569 struct spi_master *master = spi->master;
e6811d1d 2570 struct spi_transfer *xfer;
6ea31293 2571 int w_size;
cf32b71e 2572
24a0013a
MB
2573 if (list_empty(&message->transfers))
2574 return -EINVAL;
24a0013a 2575
cf32b71e
ES
2576 /* Half-duplex links include original MicroWire, and ones with
2577 * only one data pin like SPI_3WIRE (switches direction) or where
2578 * either MOSI or MISO is missing. They can also be caused by
2579 * software limitations.
2580 */
2581 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
2582 || (spi->mode & SPI_3WIRE)) {
cf32b71e
ES
2583 unsigned flags = master->flags;
2584
2585 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2586 if (xfer->rx_buf && xfer->tx_buf)
2587 return -EINVAL;
2588 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
2589 return -EINVAL;
2590 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
2591 return -EINVAL;
2592 }
2593 }
2594
e6811d1d 2595 /**
059b8ffe
LD
2596 * Set transfer bits_per_word and max speed as spi device default if
2597 * it is not set for this transfer.
f477b7fb 2598 * Set transfer tx_nbits and rx_nbits as single transfer default
2599 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
e6811d1d 2600 */
77e80588 2601 message->frame_length = 0;
e6811d1d 2602 list_for_each_entry(xfer, &message->transfers, transfer_list) {
078726ce 2603 message->frame_length += xfer->len;
e6811d1d
LD
2604 if (!xfer->bits_per_word)
2605 xfer->bits_per_word = spi->bits_per_word;
a6f87fad
AL
2606
2607 if (!xfer->speed_hz)
059b8ffe 2608 xfer->speed_hz = spi->max_speed_hz;
7dc9fbc3
MB
2609 if (!xfer->speed_hz)
2610 xfer->speed_hz = master->max_speed_hz;
a6f87fad
AL
2611
2612 if (master->max_speed_hz &&
2613 xfer->speed_hz > master->max_speed_hz)
2614 xfer->speed_hz = master->max_speed_hz;
56ede94a 2615
63ab645f
SB
2616 if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
2617 return -EINVAL;
a2fd4f9f 2618
4d94bd21
II
2619 /*
2620 * SPI transfer length should be multiple of SPI word size
2621 * where SPI word size should be power-of-two multiple
2622 */
2623 if (xfer->bits_per_word <= 8)
2624 w_size = 1;
2625 else if (xfer->bits_per_word <= 16)
2626 w_size = 2;
2627 else
2628 w_size = 4;
2629
4d94bd21 2630 /* No partial transfers accepted */
6ea31293 2631 if (xfer->len % w_size)
4d94bd21
II
2632 return -EINVAL;
2633
a2fd4f9f
MB
2634 if (xfer->speed_hz && master->min_speed_hz &&
2635 xfer->speed_hz < master->min_speed_hz)
2636 return -EINVAL;
f477b7fb 2637
2638 if (xfer->tx_buf && !xfer->tx_nbits)
2639 xfer->tx_nbits = SPI_NBITS_SINGLE;
2640 if (xfer->rx_buf && !xfer->rx_nbits)
2641 xfer->rx_nbits = SPI_NBITS_SINGLE;
2642 /* check transfer tx/rx_nbits:
1afd9989
GU
2643 * 1. check the value matches one of single, dual and quad
2644 * 2. check tx/rx_nbits match the mode in spi_device
f477b7fb 2645 */
db90a441
SP
2646 if (xfer->tx_buf) {
2647 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2648 xfer->tx_nbits != SPI_NBITS_DUAL &&
2649 xfer->tx_nbits != SPI_NBITS_QUAD)
2650 return -EINVAL;
2651 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2652 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2653 return -EINVAL;
2654 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2655 !(spi->mode & SPI_TX_QUAD))
2656 return -EINVAL;
db90a441 2657 }
f477b7fb 2658 /* check transfer rx_nbits */
db90a441
SP
2659 if (xfer->rx_buf) {
2660 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2661 xfer->rx_nbits != SPI_NBITS_DUAL &&
2662 xfer->rx_nbits != SPI_NBITS_QUAD)
2663 return -EINVAL;
2664 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2665 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2666 return -EINVAL;
2667 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2668 !(spi->mode & SPI_RX_QUAD))
2669 return -EINVAL;
db90a441 2670 }
e6811d1d
LD
2671 }
2672
cf32b71e 2673 message->status = -EINPROGRESS;
90808738
MB
2674
2675 return 0;
2676}
2677
2678static int __spi_async(struct spi_device *spi, struct spi_message *message)
2679{
2680 struct spi_master *master = spi->master;
2681
2682 message->spi = spi;
2683
eca2ebc7
MS
2684 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
2685 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2686
90808738
MB
2687 trace_spi_message_submit(message);
2688
cf32b71e
ES
2689 return master->transfer(spi, message);
2690}
2691
568d0697
DB
2692/**
2693 * spi_async - asynchronous SPI transfer
2694 * @spi: device with which data will be exchanged
2695 * @message: describes the data transfers, including completion callback
2696 * Context: any (irqs may be blocked, etc)
2697 *
2698 * This call may be used in_irq and other contexts which can't sleep,
2699 * as well as from task contexts which can sleep.
2700 *
2701 * The completion callback is invoked in a context which can't sleep.
2702 * Before that invocation, the value of message->status is undefined.
2703 * When the callback is issued, message->status holds either zero (to
2704 * indicate complete success) or a negative error code. After that
2705 * callback returns, the driver which issued the transfer request may
2706 * deallocate the associated memory; it's no longer in use by any SPI
2707 * core or controller driver code.
2708 *
2709 * Note that although all messages to a spi_device are handled in
2710 * FIFO order, messages may go to different devices in other orders.
2711 * Some device might be higher priority, or have various "hard" access
2712 * time requirements, for example.
2713 *
2714 * On detection of any fault during the transfer, processing of
2715 * the entire message is aborted, and the device is deselected.
2716 * Until returning from the associated message completion callback,
2717 * no other spi_message queued to that device will be processed.
2718 * (This rule applies equally to all the synchronous transfer calls,
2719 * which are wrappers around this core asynchronous primitive.)
97d56dc6
JMC
2720 *
2721 * Return: zero on success, else a negative error code.
568d0697
DB
2722 */
2723int spi_async(struct spi_device *spi, struct spi_message *message)
2724{
2725 struct spi_master *master = spi->master;
cf32b71e
ES
2726 int ret;
2727 unsigned long flags;
568d0697 2728
90808738
MB
2729 ret = __spi_validate(spi, message);
2730 if (ret != 0)
2731 return ret;
2732
cf32b71e 2733 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
568d0697 2734
cf32b71e
ES
2735 if (master->bus_lock_flag)
2736 ret = -EBUSY;
2737 else
2738 ret = __spi_async(spi, message);
568d0697 2739
cf32b71e
ES
2740 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2741
2742 return ret;
568d0697
DB
2743}
2744EXPORT_SYMBOL_GPL(spi_async);
2745
cf32b71e
ES
2746/**
2747 * spi_async_locked - version of spi_async with exclusive bus usage
2748 * @spi: device with which data will be exchanged
2749 * @message: describes the data transfers, including completion callback
2750 * Context: any (irqs may be blocked, etc)
2751 *
2752 * This call may be used in_irq and other contexts which can't sleep,
2753 * as well as from task contexts which can sleep.
2754 *
2755 * The completion callback is invoked in a context which can't sleep.
2756 * Before that invocation, the value of message->status is undefined.
2757 * When the callback is issued, message->status holds either zero (to
2758 * indicate complete success) or a negative error code. After that
2759 * callback returns, the driver which issued the transfer request may
2760 * deallocate the associated memory; it's no longer in use by any SPI
2761 * core or controller driver code.
2762 *
2763 * Note that although all messages to a spi_device are handled in
2764 * FIFO order, messages may go to different devices in other orders.
2765 * Some device might be higher priority, or have various "hard" access
2766 * time requirements, for example.
2767 *
2768 * On detection of any fault during the transfer, processing of
2769 * the entire message is aborted, and the device is deselected.
2770 * Until returning from the associated message completion callback,
2771 * no other spi_message queued to that device will be processed.
2772 * (This rule applies equally to all the synchronous transfer calls,
2773 * which are wrappers around this core asynchronous primitive.)
97d56dc6
JMC
2774 *
2775 * Return: zero on success, else a negative error code.
cf32b71e
ES
2776 */
2777int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2778{
2779 struct spi_master *master = spi->master;
2780 int ret;
2781 unsigned long flags;
2782
90808738
MB
2783 ret = __spi_validate(spi, message);
2784 if (ret != 0)
2785 return ret;
2786
cf32b71e
ES
2787 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2788
2789 ret = __spi_async(spi, message);
2790
2791 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2792
2793 return ret;
2794
2795}
2796EXPORT_SYMBOL_GPL(spi_async_locked);
2797
7d077197 2798
556351f1
V
2799int spi_flash_read(struct spi_device *spi,
2800 struct spi_flash_read_message *msg)
2801
2802{
2803 struct spi_master *master = spi->master;
f4502dd1 2804 struct device *rx_dev = NULL;
556351f1
V
2805 int ret;
2806
2807 if ((msg->opcode_nbits == SPI_NBITS_DUAL ||
2808 msg->addr_nbits == SPI_NBITS_DUAL) &&
2809 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2810 return -EINVAL;
2811 if ((msg->opcode_nbits == SPI_NBITS_QUAD ||
2812 msg->addr_nbits == SPI_NBITS_QUAD) &&
2813 !(spi->mode & SPI_TX_QUAD))
2814 return -EINVAL;
2815 if (msg->data_nbits == SPI_NBITS_DUAL &&
2816 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2817 return -EINVAL;
2818 if (msg->data_nbits == SPI_NBITS_QUAD &&
2819 !(spi->mode & SPI_RX_QUAD))
2820 return -EINVAL;
2821
2822 if (master->auto_runtime_pm) {
2823 ret = pm_runtime_get_sync(master->dev.parent);
2824 if (ret < 0) {
2825 dev_err(&master->dev, "Failed to power device: %d\n",
2826 ret);
2827 return ret;
2828 }
2829 }
f4502dd1 2830
556351f1 2831 mutex_lock(&master->bus_lock_mutex);
ef4d96ec 2832 mutex_lock(&master->io_mutex);
f4502dd1
V
2833 if (master->dma_rx) {
2834 rx_dev = master->dma_rx->device->dev;
2835 ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
2836 msg->buf, msg->len,
2837 DMA_FROM_DEVICE);
2838 if (!ret)
2839 msg->cur_msg_mapped = true;
2840 }
556351f1 2841 ret = master->spi_flash_read(spi, msg);
f4502dd1
V
2842 if (msg->cur_msg_mapped)
2843 spi_unmap_buf(master, rx_dev, &msg->rx_sg,
2844 DMA_FROM_DEVICE);
ef4d96ec 2845 mutex_unlock(&master->io_mutex);
556351f1 2846 mutex_unlock(&master->bus_lock_mutex);
f4502dd1 2847
556351f1
V
2848 if (master->auto_runtime_pm)
2849 pm_runtime_put(master->dev.parent);
2850
2851 return ret;
2852}
2853EXPORT_SYMBOL_GPL(spi_flash_read);
2854
7d077197
DB
2855/*-------------------------------------------------------------------------*/
2856
2857/* Utility methods for SPI master protocol drivers, layered on
2858 * top of the core. Some other utility methods are defined as
2859 * inline functions.
2860 */
2861
5d870c8e
AM
2862static void spi_complete(void *arg)
2863{
2864 complete(arg);
2865}
2866
ef4d96ec 2867static int __spi_sync(struct spi_device *spi, struct spi_message *message)
cf32b71e
ES
2868{
2869 DECLARE_COMPLETION_ONSTACK(done);
2870 int status;
2871 struct spi_master *master = spi->master;
0461a414
MB
2872 unsigned long flags;
2873
2874 status = __spi_validate(spi, message);
2875 if (status != 0)
2876 return status;
cf32b71e
ES
2877
2878 message->complete = spi_complete;
2879 message->context = &done;
0461a414 2880 message->spi = spi;
cf32b71e 2881
eca2ebc7
MS
2882 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
2883 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
2884
0461a414
MB
2885 /* If we're not using the legacy transfer method then we will
2886 * try to transfer in the calling context so special case.
2887 * This code would be less tricky if we could remove the
2888 * support for driver implemented message queues.
2889 */
2890 if (master->transfer == spi_queued_transfer) {
2891 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2892
2893 trace_spi_message_submit(message);
2894
2895 status = __spi_queued_transfer(spi, message, false);
2896
2897 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2898 } else {
2899 status = spi_async_locked(spi, message);
2900 }
cf32b71e 2901
cf32b71e 2902 if (status == 0) {
0461a414
MB
2903 /* Push out the messages in the calling context if we
2904 * can.
2905 */
eca2ebc7
MS
2906 if (master->transfer == spi_queued_transfer) {
2907 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2908 spi_sync_immediate);
2909 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
2910 spi_sync_immediate);
ef4d96ec 2911 __spi_pump_messages(master, false);
eca2ebc7 2912 }
0461a414 2913
cf32b71e
ES
2914 wait_for_completion(&done);
2915 status = message->status;
2916 }
2917 message->context = NULL;
2918 return status;
2919}
2920
8ae12a0d
DB
2921/**
2922 * spi_sync - blocking/synchronous SPI data transfers
2923 * @spi: device with which data will be exchanged
2924 * @message: describes the data transfers
33e34dc6 2925 * Context: can sleep
8ae12a0d
DB
2926 *
2927 * This call may only be used from a context that may sleep. The sleep
2928 * is non-interruptible, and has no timeout. Low-overhead controller
2929 * drivers may DMA directly into and out of the message buffers.
2930 *
2931 * Note that the SPI device's chip select is active during the message,
2932 * and then is normally disabled between messages. Drivers for some
2933 * frequently-used devices may want to minimize costs of selecting a chip,
2934 * by leaving it selected in anticipation that the next message will go
2935 * to the same chip. (That may increase power usage.)
2936 *
0c868461
DB
2937 * Also, the caller is guaranteeing that the memory associated with the
2938 * message will not be freed before this call returns.
2939 *
97d56dc6 2940 * Return: zero on success, else a negative error code.
8ae12a0d
DB
2941 */
2942int spi_sync(struct spi_device *spi, struct spi_message *message)
2943{
ef4d96ec
MB
2944 int ret;
2945
2946 mutex_lock(&spi->master->bus_lock_mutex);
2947 ret = __spi_sync(spi, message);
2948 mutex_unlock(&spi->master->bus_lock_mutex);
2949
2950 return ret;
8ae12a0d
DB
2951}
2952EXPORT_SYMBOL_GPL(spi_sync);
2953
cf32b71e
ES
2954/**
2955 * spi_sync_locked - version of spi_sync with exclusive bus usage
2956 * @spi: device with which data will be exchanged
2957 * @message: describes the data transfers
2958 * Context: can sleep
2959 *
2960 * This call may only be used from a context that may sleep. The sleep
2961 * is non-interruptible, and has no timeout. Low-overhead controller
2962 * drivers may DMA directly into and out of the message buffers.
2963 *
2964 * This call should be used by drivers that require exclusive access to the
25985edc 2965 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
cf32b71e
ES
2966 * be released by a spi_bus_unlock call when the exclusive access is over.
2967 *
97d56dc6 2968 * Return: zero on success, else a negative error code.
cf32b71e
ES
2969 */
2970int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2971{
ef4d96ec 2972 return __spi_sync(spi, message);
cf32b71e
ES
2973}
2974EXPORT_SYMBOL_GPL(spi_sync_locked);
2975
2976/**
2977 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
2978 * @master: SPI bus master that should be locked for exclusive bus access
2979 * Context: can sleep
2980 *
2981 * This call may only be used from a context that may sleep. The sleep
2982 * is non-interruptible, and has no timeout.
2983 *
2984 * This call should be used by drivers that require exclusive access to the
2985 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
2986 * exclusive access is over. Data transfer must be done by spi_sync_locked
2987 * and spi_async_locked calls when the SPI bus lock is held.
2988 *
97d56dc6 2989 * Return: always zero.
cf32b71e
ES
2990 */
2991int spi_bus_lock(struct spi_master *master)
2992{
2993 unsigned long flags;
2994
2995 mutex_lock(&master->bus_lock_mutex);
2996
2997 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2998 master->bus_lock_flag = 1;
2999 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
3000
3001 /* mutex remains locked until spi_bus_unlock is called */
3002
3003 return 0;
3004}
3005EXPORT_SYMBOL_GPL(spi_bus_lock);
3006
3007/**
3008 * spi_bus_unlock - release the lock for exclusive SPI bus usage
3009 * @master: SPI bus master that was locked for exclusive bus access
3010 * Context: can sleep
3011 *
3012 * This call may only be used from a context that may sleep. The sleep
3013 * is non-interruptible, and has no timeout.
3014 *
3015 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3016 * call.
3017 *
97d56dc6 3018 * Return: always zero.
cf32b71e
ES
3019 */
3020int spi_bus_unlock(struct spi_master *master)
3021{
3022 master->bus_lock_flag = 0;
3023
3024 mutex_unlock(&master->bus_lock_mutex);
3025
3026 return 0;
3027}
3028EXPORT_SYMBOL_GPL(spi_bus_unlock);
3029
a9948b61 3030/* portable code must never pass more than 32 bytes */
5fe5f05e 3031#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
8ae12a0d
DB
3032
3033static u8 *buf;
3034
3035/**
3036 * spi_write_then_read - SPI synchronous write followed by read
3037 * @spi: device with which data will be exchanged
3038 * @txbuf: data to be written (need not be dma-safe)
3039 * @n_tx: size of txbuf, in bytes
27570497
JP
3040 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3041 * @n_rx: size of rxbuf, in bytes
33e34dc6 3042 * Context: can sleep
8ae12a0d
DB
3043 *
3044 * This performs a half duplex MicroWire style transaction with the
3045 * device, sending txbuf and then reading rxbuf. The return value
3046 * is zero for success, else a negative errno status code.
b885244e 3047 * This call may only be used from a context that may sleep.
8ae12a0d 3048 *
0c868461 3049 * Parameters to this routine are always copied using a small buffer;
33e34dc6
DB
3050 * portable code should never use this for more than 32 bytes.
3051 * Performance-sensitive or bulk transfer code should instead use
0c868461 3052 * spi_{async,sync}() calls with dma-safe buffers.
97d56dc6
JMC
3053 *
3054 * Return: zero on success, else a negative error code.
8ae12a0d
DB
3055 */
3056int spi_write_then_read(struct spi_device *spi,
0c4a1590
MB
3057 const void *txbuf, unsigned n_tx,
3058 void *rxbuf, unsigned n_rx)
8ae12a0d 3059{
068f4070 3060 static DEFINE_MUTEX(lock);
8ae12a0d
DB
3061
3062 int status;
3063 struct spi_message message;
bdff549e 3064 struct spi_transfer x[2];
8ae12a0d
DB
3065 u8 *local_buf;
3066
b3a223ee
MB
3067 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3068 * copying here, (as a pure convenience thing), but we can
3069 * keep heap costs out of the hot path unless someone else is
3070 * using the pre-allocated buffer or the transfer is too large.
8ae12a0d 3071 */
b3a223ee 3072 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
2cd94c8a
MB
3073 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3074 GFP_KERNEL | GFP_DMA);
b3a223ee
MB
3075 if (!local_buf)
3076 return -ENOMEM;
3077 } else {
3078 local_buf = buf;
3079 }
8ae12a0d 3080
8275c642 3081 spi_message_init(&message);
5fe5f05e 3082 memset(x, 0, sizeof(x));
bdff549e
DB
3083 if (n_tx) {
3084 x[0].len = n_tx;
3085 spi_message_add_tail(&x[0], &message);
3086 }
3087 if (n_rx) {
3088 x[1].len = n_rx;
3089 spi_message_add_tail(&x[1], &message);
3090 }
8275c642 3091
8ae12a0d 3092 memcpy(local_buf, txbuf, n_tx);
bdff549e
DB
3093 x[0].tx_buf = local_buf;
3094 x[1].rx_buf = local_buf + n_tx;
8ae12a0d
DB
3095
3096 /* do the i/o */
8ae12a0d 3097 status = spi_sync(spi, &message);
9b938b74 3098 if (status == 0)
bdff549e 3099 memcpy(rxbuf, x[1].rx_buf, n_rx);
8ae12a0d 3100
bdff549e 3101 if (x[0].tx_buf == buf)
068f4070 3102 mutex_unlock(&lock);
8ae12a0d
DB
3103 else
3104 kfree(local_buf);
3105
3106 return status;
3107}
3108EXPORT_SYMBOL_GPL(spi_write_then_read);
3109
3110/*-------------------------------------------------------------------------*/
3111
ce79d54a
PA
3112#if IS_ENABLED(CONFIG_OF_DYNAMIC)
3113static int __spi_of_device_match(struct device *dev, void *data)
3114{
3115 return dev->of_node == data;
3116}
3117
3118/* must call put_device() when done with returned spi_device device */
3119static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
3120{
3121 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3122 __spi_of_device_match);
3123 return dev ? to_spi_device(dev) : NULL;
3124}
3125
3126static int __spi_of_master_match(struct device *dev, const void *data)
3127{
3128 return dev->of_node == data;
3129}
3130
3131/* the spi masters are not using spi_bus, so we find it with another way */
3132static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
3133{
3134 struct device *dev;
3135
3136 dev = class_find_device(&spi_master_class, NULL, node,
3137 __spi_of_master_match);
3138 if (!dev)
3139 return NULL;
3140
3141 /* reference got in class_find_device */
3142 return container_of(dev, struct spi_master, dev);
3143}
3144
3145static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3146 void *arg)
3147{
3148 struct of_reconfig_data *rd = arg;
3149 struct spi_master *master;
3150 struct spi_device *spi;
3151
3152 switch (of_reconfig_get_state_change(action, arg)) {
3153 case OF_RECONFIG_CHANGE_ADD:
3154 master = of_find_spi_master_by_node(rd->dn->parent);
3155 if (master == NULL)
3156 return NOTIFY_OK; /* not for us */
3157
bd6c1644
GU
3158 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
3159 put_device(&master->dev);
3160 return NOTIFY_OK;
3161 }
3162
ce79d54a
PA
3163 spi = of_register_spi_device(master, rd->dn);
3164 put_device(&master->dev);
3165
3166 if (IS_ERR(spi)) {
3167 pr_err("%s: failed to create for '%s'\n",
3168 __func__, rd->dn->full_name);
e0af98a7 3169 of_node_clear_flag(rd->dn, OF_POPULATED);
ce79d54a
PA
3170 return notifier_from_errno(PTR_ERR(spi));
3171 }
3172 break;
3173
3174 case OF_RECONFIG_CHANGE_REMOVE:
bd6c1644
GU
3175 /* already depopulated? */
3176 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3177 return NOTIFY_OK;
3178
ce79d54a
PA
3179 /* find our device by node */
3180 spi = of_find_spi_device_by_node(rd->dn);
3181 if (spi == NULL)
3182 return NOTIFY_OK; /* no? not meant for us */
3183
3184 /* unregister takes one ref away */
3185 spi_unregister_device(spi);
3186
3187 /* and put the reference of the find */
3188 put_device(&spi->dev);
3189 break;
3190 }
3191
3192 return NOTIFY_OK;
3193}
3194
3195static struct notifier_block spi_of_notifier = {
3196 .notifier_call = of_spi_notify,
3197};
3198#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3199extern struct notifier_block spi_of_notifier;
3200#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3201
7f24467f
OP
3202#if IS_ENABLED(CONFIG_ACPI)
3203static int spi_acpi_master_match(struct device *dev, const void *data)
3204{
3205 return ACPI_COMPANION(dev->parent) == data;
3206}
3207
3208static int spi_acpi_device_match(struct device *dev, void *data)
3209{
3210 return ACPI_COMPANION(dev) == data;
3211}
3212
3213static struct spi_master *acpi_spi_find_master_by_adev(struct acpi_device *adev)
3214{
3215 struct device *dev;
3216
3217 dev = class_find_device(&spi_master_class, NULL, adev,
3218 spi_acpi_master_match);
3219 if (!dev)
3220 return NULL;
3221
3222 return container_of(dev, struct spi_master, dev);
3223}
3224
3225static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3226{
3227 struct device *dev;
3228
3229 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3230
3231 return dev ? to_spi_device(dev) : NULL;
3232}
3233
3234static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3235 void *arg)
3236{
3237 struct acpi_device *adev = arg;
3238 struct spi_master *master;
3239 struct spi_device *spi;
3240
3241 switch (value) {
3242 case ACPI_RECONFIG_DEVICE_ADD:
3243 master = acpi_spi_find_master_by_adev(adev->parent);
3244 if (!master)
3245 break;
3246
3247 acpi_register_spi_device(master, adev);
3248 put_device(&master->dev);
3249 break;
3250 case ACPI_RECONFIG_DEVICE_REMOVE:
3251 if (!acpi_device_enumerated(adev))
3252 break;
3253
3254 spi = acpi_spi_find_device_by_adev(adev);
3255 if (!spi)
3256 break;
3257
3258 spi_unregister_device(spi);
3259 put_device(&spi->dev);
3260 break;
3261 }
3262
3263 return NOTIFY_OK;
3264}
3265
3266static struct notifier_block spi_acpi_notifier = {
3267 .notifier_call = acpi_spi_notify,
3268};
3269#else
3270extern struct notifier_block spi_acpi_notifier;
3271#endif
3272
8ae12a0d
DB
3273static int __init spi_init(void)
3274{
b885244e
DB
3275 int status;
3276
e94b1766 3277 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
b885244e
DB
3278 if (!buf) {
3279 status = -ENOMEM;
3280 goto err0;
3281 }
3282
3283 status = bus_register(&spi_bus_type);
3284 if (status < 0)
3285 goto err1;
8ae12a0d 3286
b885244e
DB
3287 status = class_register(&spi_master_class);
3288 if (status < 0)
3289 goto err2;
ce79d54a 3290
5267720e 3291 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
ce79d54a 3292 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
7f24467f
OP
3293 if (IS_ENABLED(CONFIG_ACPI))
3294 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
ce79d54a 3295
8ae12a0d 3296 return 0;
b885244e
DB
3297
3298err2:
3299 bus_unregister(&spi_bus_type);
3300err1:
3301 kfree(buf);
3302 buf = NULL;
3303err0:
3304 return status;
8ae12a0d 3305}
b885244e 3306
8ae12a0d
DB
3307/* board_info is normally registered in arch_initcall(),
3308 * but even essential drivers wait till later
b885244e
DB
3309 *
3310 * REVISIT only boardinfo really needs static linking. the rest (device and
3311 * driver registration) _could_ be dynamically linked (modular) ... costs
3312 * include needing to have boardinfo data structures be much more public.
8ae12a0d 3313 */
673c0c00 3314postcore_initcall(spi_init);
8ae12a0d 3315