spi: Kill superfluous cast in spi_w8r16()
[deliverable/linux.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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
8ae12a0d 22#include <linux/kernel.h>
d57a4282 23#include <linux/kmod.h>
8ae12a0d
DB
24#include <linux/device.h>
25#include <linux/init.h>
26#include <linux/cache.h>
94040828 27#include <linux/mutex.h>
2b7a32f7 28#include <linux/of_device.h>
d57a4282 29#include <linux/of_irq.h>
5a0e3ad6 30#include <linux/slab.h>
e0626e38 31#include <linux/mod_devicetable.h>
8ae12a0d 32#include <linux/spi/spi.h>
74317984 33#include <linux/of_gpio.h>
3ae22e8c 34#include <linux/pm_runtime.h>
025ed130 35#include <linux/export.h>
8bd75c77 36#include <linux/sched/rt.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>
8ae12a0d 41
56ec1978
MB
42#define CREATE_TRACE_POINTS
43#include <trace/events/spi.h>
44
8ae12a0d
DB
45static void spidev_release(struct device *dev)
46{
0ffa0285 47 struct spi_device *spi = to_spi_device(dev);
8ae12a0d
DB
48
49 /* spi masters may cleanup for released devices */
50 if (spi->master->cleanup)
51 spi->master->cleanup(spi);
52
0c868461 53 spi_master_put(spi->master);
07a389fe 54 kfree(spi);
8ae12a0d
DB
55}
56
57static ssize_t
58modalias_show(struct device *dev, struct device_attribute *a, char *buf)
59{
60 const struct spi_device *spi = to_spi_device(dev);
61
d8e328b3 62 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
8ae12a0d 63}
aa7da564 64static DEVICE_ATTR_RO(modalias);
8ae12a0d 65
aa7da564
GKH
66static struct attribute *spi_dev_attrs[] = {
67 &dev_attr_modalias.attr,
68 NULL,
8ae12a0d 69};
aa7da564 70ATTRIBUTE_GROUPS(spi_dev);
8ae12a0d
DB
71
72/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
73 * and the sysfs version makes coldplug work too.
74 */
75
75368bf6
AV
76static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
77 const struct spi_device *sdev)
78{
79 while (id->name[0]) {
80 if (!strcmp(sdev->modalias, id->name))
81 return id;
82 id++;
83 }
84 return NULL;
85}
86
87const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
88{
89 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
90
91 return spi_match_id(sdrv->id_table, sdev);
92}
93EXPORT_SYMBOL_GPL(spi_get_device_id);
94
8ae12a0d
DB
95static int spi_match_device(struct device *dev, struct device_driver *drv)
96{
97 const struct spi_device *spi = to_spi_device(dev);
75368bf6
AV
98 const struct spi_driver *sdrv = to_spi_driver(drv);
99
2b7a32f7
SA
100 /* Attempt an OF style match */
101 if (of_driver_match_device(dev, drv))
102 return 1;
103
64bee4d2
MW
104 /* Then try ACPI */
105 if (acpi_driver_match_device(dev, drv))
106 return 1;
107
75368bf6
AV
108 if (sdrv->id_table)
109 return !!spi_match_id(sdrv->id_table, spi);
8ae12a0d 110
35f74fca 111 return strcmp(spi->modalias, drv->name) == 0;
8ae12a0d
DB
112}
113
7eff2e7a 114static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
8ae12a0d
DB
115{
116 const struct spi_device *spi = to_spi_device(dev);
117
e0626e38 118 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
8ae12a0d
DB
119 return 0;
120}
121
3ae22e8c
MB
122#ifdef CONFIG_PM_SLEEP
123static int spi_legacy_suspend(struct device *dev, pm_message_t message)
8ae12a0d 124{
3c72426f 125 int value = 0;
b885244e 126 struct spi_driver *drv = to_spi_driver(dev->driver);
8ae12a0d 127
8ae12a0d 128 /* suspend will stop irqs and dma; no more i/o */
3c72426f
DB
129 if (drv) {
130 if (drv->suspend)
131 value = drv->suspend(to_spi_device(dev), message);
132 else
133 dev_dbg(dev, "... can't suspend\n");
134 }
8ae12a0d
DB
135 return value;
136}
137
3ae22e8c 138static int spi_legacy_resume(struct device *dev)
8ae12a0d 139{
3c72426f 140 int value = 0;
b885244e 141 struct spi_driver *drv = to_spi_driver(dev->driver);
8ae12a0d 142
8ae12a0d 143 /* resume may restart the i/o queue */
3c72426f
DB
144 if (drv) {
145 if (drv->resume)
146 value = drv->resume(to_spi_device(dev));
147 else
148 dev_dbg(dev, "... can't resume\n");
149 }
8ae12a0d
DB
150 return value;
151}
152
3ae22e8c
MB
153static int spi_pm_suspend(struct device *dev)
154{
155 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
156
157 if (pm)
158 return pm_generic_suspend(dev);
159 else
160 return spi_legacy_suspend(dev, PMSG_SUSPEND);
161}
162
163static int spi_pm_resume(struct device *dev)
164{
165 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
166
167 if (pm)
168 return pm_generic_resume(dev);
169 else
170 return spi_legacy_resume(dev);
171}
172
173static int spi_pm_freeze(struct device *dev)
174{
175 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
176
177 if (pm)
178 return pm_generic_freeze(dev);
179 else
180 return spi_legacy_suspend(dev, PMSG_FREEZE);
181}
182
183static int spi_pm_thaw(struct device *dev)
184{
185 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
186
187 if (pm)
188 return pm_generic_thaw(dev);
189 else
190 return spi_legacy_resume(dev);
191}
192
193static int spi_pm_poweroff(struct device *dev)
194{
195 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
196
197 if (pm)
198 return pm_generic_poweroff(dev);
199 else
200 return spi_legacy_suspend(dev, PMSG_HIBERNATE);
201}
202
203static int spi_pm_restore(struct device *dev)
204{
205 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
206
207 if (pm)
208 return pm_generic_restore(dev);
209 else
210 return spi_legacy_resume(dev);
211}
8ae12a0d 212#else
3ae22e8c
MB
213#define spi_pm_suspend NULL
214#define spi_pm_resume NULL
215#define spi_pm_freeze NULL
216#define spi_pm_thaw NULL
217#define spi_pm_poweroff NULL
218#define spi_pm_restore NULL
8ae12a0d
DB
219#endif
220
3ae22e8c
MB
221static const struct dev_pm_ops spi_pm = {
222 .suspend = spi_pm_suspend,
223 .resume = spi_pm_resume,
224 .freeze = spi_pm_freeze,
225 .thaw = spi_pm_thaw,
226 .poweroff = spi_pm_poweroff,
227 .restore = spi_pm_restore,
228 SET_RUNTIME_PM_OPS(
229 pm_generic_runtime_suspend,
230 pm_generic_runtime_resume,
45f0a85c 231 NULL
3ae22e8c
MB
232 )
233};
234
8ae12a0d
DB
235struct bus_type spi_bus_type = {
236 .name = "spi",
aa7da564 237 .dev_groups = spi_dev_groups,
8ae12a0d
DB
238 .match = spi_match_device,
239 .uevent = spi_uevent,
3ae22e8c 240 .pm = &spi_pm,
8ae12a0d
DB
241};
242EXPORT_SYMBOL_GPL(spi_bus_type);
243
b885244e
DB
244
245static int spi_drv_probe(struct device *dev)
246{
247 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
33cf00e5
MW
248 struct spi_device *spi = to_spi_device(dev);
249 int ret;
250
251 acpi_dev_pm_attach(&spi->dev, true);
252 ret = sdrv->probe(spi);
253 if (ret)
254 acpi_dev_pm_detach(&spi->dev, true);
b885244e 255
33cf00e5 256 return ret;
b885244e
DB
257}
258
259static int spi_drv_remove(struct device *dev)
260{
261 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
33cf00e5
MW
262 struct spi_device *spi = to_spi_device(dev);
263 int ret;
264
265 ret = sdrv->remove(spi);
266 acpi_dev_pm_detach(&spi->dev, true);
b885244e 267
33cf00e5 268 return ret;
b885244e
DB
269}
270
271static void spi_drv_shutdown(struct device *dev)
272{
273 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
274
275 sdrv->shutdown(to_spi_device(dev));
276}
277
33e34dc6
DB
278/**
279 * spi_register_driver - register a SPI driver
280 * @sdrv: the driver to register
281 * Context: can sleep
282 */
b885244e
DB
283int spi_register_driver(struct spi_driver *sdrv)
284{
285 sdrv->driver.bus = &spi_bus_type;
286 if (sdrv->probe)
287 sdrv->driver.probe = spi_drv_probe;
288 if (sdrv->remove)
289 sdrv->driver.remove = spi_drv_remove;
290 if (sdrv->shutdown)
291 sdrv->driver.shutdown = spi_drv_shutdown;
292 return driver_register(&sdrv->driver);
293}
294EXPORT_SYMBOL_GPL(spi_register_driver);
295
8ae12a0d
DB
296/*-------------------------------------------------------------------------*/
297
298/* SPI devices should normally not be created by SPI device drivers; that
299 * would make them board-specific. Similarly with SPI master drivers.
300 * Device registration normally goes into like arch/.../mach.../board-YYY.c
301 * with other readonly (flashable) information about mainboard devices.
302 */
303
304struct boardinfo {
305 struct list_head list;
2b9603a0 306 struct spi_board_info board_info;
8ae12a0d
DB
307};
308
309static LIST_HEAD(board_list);
2b9603a0
FT
310static LIST_HEAD(spi_master_list);
311
312/*
313 * Used to protect add/del opertion for board_info list and
314 * spi_master list, and their matching process
315 */
94040828 316static DEFINE_MUTEX(board_lock);
8ae12a0d 317
dc87c98e
GL
318/**
319 * spi_alloc_device - Allocate a new SPI device
320 * @master: Controller to which device is connected
321 * Context: can sleep
322 *
323 * Allows a driver to allocate and initialize a spi_device without
324 * registering it immediately. This allows a driver to directly
325 * fill the spi_device with device parameters before calling
326 * spi_add_device() on it.
327 *
328 * Caller is responsible to call spi_add_device() on the returned
329 * spi_device structure to add it to the SPI master. If the caller
330 * needs to discard the spi_device without adding it, then it should
331 * call spi_dev_put() on it.
332 *
333 * Returns a pointer to the new device, or NULL.
334 */
335struct spi_device *spi_alloc_device(struct spi_master *master)
336{
337 struct spi_device *spi;
338 struct device *dev = master->dev.parent;
339
340 if (!spi_master_get(master))
341 return NULL;
342
5fe5f05e 343 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
dc87c98e
GL
344 if (!spi) {
345 dev_err(dev, "cannot alloc spi_device\n");
346 spi_master_put(master);
347 return NULL;
348 }
349
350 spi->master = master;
178db7d3 351 spi->dev.parent = &master->dev;
dc87c98e
GL
352 spi->dev.bus = &spi_bus_type;
353 spi->dev.release = spidev_release;
446411e1 354 spi->cs_gpio = -ENOENT;
dc87c98e
GL
355 device_initialize(&spi->dev);
356 return spi;
357}
358EXPORT_SYMBOL_GPL(spi_alloc_device);
359
e13ac47b
JN
360static void spi_dev_set_name(struct spi_device *spi)
361{
362 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
363
364 if (adev) {
365 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
366 return;
367 }
368
369 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
370 spi->chip_select);
371}
372
dc87c98e
GL
373/**
374 * spi_add_device - Add spi_device allocated with spi_alloc_device
375 * @spi: spi_device to register
376 *
377 * Companion function to spi_alloc_device. Devices allocated with
378 * spi_alloc_device can be added onto the spi bus with this function.
379 *
e48880e0 380 * Returns 0 on success; negative errno on failure
dc87c98e
GL
381 */
382int spi_add_device(struct spi_device *spi)
383{
e48880e0 384 static DEFINE_MUTEX(spi_add_lock);
74317984
JCPV
385 struct spi_master *master = spi->master;
386 struct device *dev = master->dev.parent;
8ec130a0 387 struct device *d;
dc87c98e
GL
388 int status;
389
390 /* Chipselects are numbered 0..max; validate. */
74317984 391 if (spi->chip_select >= master->num_chipselect) {
dc87c98e
GL
392 dev_err(dev, "cs%d >= max %d\n",
393 spi->chip_select,
74317984 394 master->num_chipselect);
dc87c98e
GL
395 return -EINVAL;
396 }
397
398 /* Set the bus ID string */
e13ac47b 399 spi_dev_set_name(spi);
e48880e0
DB
400
401 /* We need to make sure there's no other device with this
402 * chipselect **BEFORE** we call setup(), else we'll trash
403 * its configuration. Lock against concurrent add() calls.
404 */
405 mutex_lock(&spi_add_lock);
406
8ec130a0
RT
407 d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
408 if (d != NULL) {
e48880e0
DB
409 dev_err(dev, "chipselect %d already in use\n",
410 spi->chip_select);
8ec130a0 411 put_device(d);
e48880e0
DB
412 status = -EBUSY;
413 goto done;
414 }
415
74317984
JCPV
416 if (master->cs_gpios)
417 spi->cs_gpio = master->cs_gpios[spi->chip_select];
418
e48880e0
DB
419 /* Drivers may modify this initial i/o setup, but will
420 * normally rely on the device being setup. Devices
421 * using SPI_CS_HIGH can't coexist well otherwise...
422 */
7d077197 423 status = spi_setup(spi);
dc87c98e 424 if (status < 0) {
eb288a1f
LW
425 dev_err(dev, "can't setup %s, status %d\n",
426 dev_name(&spi->dev), status);
e48880e0 427 goto done;
dc87c98e
GL
428 }
429
e48880e0 430 /* Device may be bound to an active driver when this returns */
dc87c98e 431 status = device_add(&spi->dev);
e48880e0 432 if (status < 0)
eb288a1f
LW
433 dev_err(dev, "can't add %s, status %d\n",
434 dev_name(&spi->dev), status);
e48880e0 435 else
35f74fca 436 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
dc87c98e 437
e48880e0
DB
438done:
439 mutex_unlock(&spi_add_lock);
440 return status;
dc87c98e
GL
441}
442EXPORT_SYMBOL_GPL(spi_add_device);
8ae12a0d 443
33e34dc6
DB
444/**
445 * spi_new_device - instantiate one new SPI device
446 * @master: Controller to which device is connected
447 * @chip: Describes the SPI device
448 * Context: can sleep
449 *
450 * On typical mainboards, this is purely internal; and it's not needed
8ae12a0d
DB
451 * after board init creates the hard-wired devices. Some development
452 * platforms may not be able to use spi_register_board_info though, and
453 * this is exported so that for example a USB or parport based adapter
454 * driver could add devices (which it would learn about out-of-band).
082c8cb4
DB
455 *
456 * Returns the new device, or NULL.
8ae12a0d 457 */
e9d5a461
AB
458struct spi_device *spi_new_device(struct spi_master *master,
459 struct spi_board_info *chip)
8ae12a0d
DB
460{
461 struct spi_device *proxy;
8ae12a0d
DB
462 int status;
463
082c8cb4
DB
464 /* NOTE: caller did any chip->bus_num checks necessary.
465 *
466 * Also, unless we change the return value convention to use
467 * error-or-pointer (not NULL-or-pointer), troubleshootability
468 * suggests syslogged diagnostics are best here (ugh).
469 */
470
dc87c98e
GL
471 proxy = spi_alloc_device(master);
472 if (!proxy)
8ae12a0d
DB
473 return NULL;
474
102eb975
GL
475 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
476
8ae12a0d
DB
477 proxy->chip_select = chip->chip_select;
478 proxy->max_speed_hz = chip->max_speed_hz;
980a01c9 479 proxy->mode = chip->mode;
8ae12a0d 480 proxy->irq = chip->irq;
102eb975 481 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
8ae12a0d
DB
482 proxy->dev.platform_data = (void *) chip->platform_data;
483 proxy->controller_data = chip->controller_data;
484 proxy->controller_state = NULL;
8ae12a0d 485
dc87c98e 486 status = spi_add_device(proxy);
8ae12a0d 487 if (status < 0) {
dc87c98e
GL
488 spi_dev_put(proxy);
489 return NULL;
8ae12a0d
DB
490 }
491
8ae12a0d
DB
492 return proxy;
493}
494EXPORT_SYMBOL_GPL(spi_new_device);
495
2b9603a0
FT
496static void spi_match_master_to_boardinfo(struct spi_master *master,
497 struct spi_board_info *bi)
498{
499 struct spi_device *dev;
500
501 if (master->bus_num != bi->bus_num)
502 return;
503
504 dev = spi_new_device(master, bi);
505 if (!dev)
506 dev_err(master->dev.parent, "can't create new device for %s\n",
507 bi->modalias);
508}
509
33e34dc6
DB
510/**
511 * spi_register_board_info - register SPI devices for a given board
512 * @info: array of chip descriptors
513 * @n: how many descriptors are provided
514 * Context: can sleep
515 *
8ae12a0d
DB
516 * Board-specific early init code calls this (probably during arch_initcall)
517 * with segments of the SPI device table. Any device nodes are created later,
518 * after the relevant parent SPI controller (bus_num) is defined. We keep
519 * this table of devices forever, so that reloading a controller driver will
520 * not make Linux forget about these hard-wired devices.
521 *
522 * Other code can also call this, e.g. a particular add-on board might provide
523 * SPI devices through its expansion connector, so code initializing that board
524 * would naturally declare its SPI devices.
525 *
526 * The board info passed can safely be __initdata ... but be careful of
527 * any embedded pointers (platform_data, etc), they're copied as-is.
528 */
fd4a319b 529int spi_register_board_info(struct spi_board_info const *info, unsigned n)
8ae12a0d 530{
2b9603a0
FT
531 struct boardinfo *bi;
532 int i;
8ae12a0d 533
2b9603a0 534 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
8ae12a0d
DB
535 if (!bi)
536 return -ENOMEM;
8ae12a0d 537
2b9603a0
FT
538 for (i = 0; i < n; i++, bi++, info++) {
539 struct spi_master *master;
8ae12a0d 540
2b9603a0
FT
541 memcpy(&bi->board_info, info, sizeof(*info));
542 mutex_lock(&board_lock);
543 list_add_tail(&bi->list, &board_list);
544 list_for_each_entry(master, &spi_master_list, list)
545 spi_match_master_to_boardinfo(master, &bi->board_info);
546 mutex_unlock(&board_lock);
8ae12a0d 547 }
2b9603a0
FT
548
549 return 0;
8ae12a0d
DB
550}
551
552/*-------------------------------------------------------------------------*/
553
b158935f
MB
554static void spi_set_cs(struct spi_device *spi, bool enable)
555{
556 if (spi->mode & SPI_CS_HIGH)
557 enable = !enable;
558
559 if (spi->cs_gpio >= 0)
560 gpio_set_value(spi->cs_gpio, !enable);
561 else if (spi->master->set_cs)
562 spi->master->set_cs(spi, !enable);
563}
564
565/*
566 * spi_transfer_one_message - Default implementation of transfer_one_message()
567 *
568 * This is a standard implementation of transfer_one_message() for
569 * drivers which impelment a transfer_one() operation. It provides
570 * standard handling of delays and chip select management.
571 */
572static int spi_transfer_one_message(struct spi_master *master,
573 struct spi_message *msg)
574{
575 struct spi_transfer *xfer;
576 bool cur_cs = true;
577 bool keep_cs = false;
578 int ret = 0;
579
580 spi_set_cs(msg->spi, true);
581
582 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
583 trace_spi_transfer_start(msg, xfer);
584
16735d02 585 reinit_completion(&master->xfer_completion);
b158935f
MB
586
587 ret = master->transfer_one(master, msg->spi, xfer);
588 if (ret < 0) {
589 dev_err(&msg->spi->dev,
590 "SPI transfer failed: %d\n", ret);
591 goto out;
592 }
593
594 if (ret > 0)
595 wait_for_completion(&master->xfer_completion);
596
597 trace_spi_transfer_stop(msg, xfer);
598
599 if (msg->status != -EINPROGRESS)
600 goto out;
601
602 if (xfer->delay_usecs)
603 udelay(xfer->delay_usecs);
604
605 if (xfer->cs_change) {
606 if (list_is_last(&xfer->transfer_list,
607 &msg->transfers)) {
608 keep_cs = true;
609 } else {
610 cur_cs = !cur_cs;
611 spi_set_cs(msg->spi, cur_cs);
612 }
613 }
614
615 msg->actual_length += xfer->len;
616 }
617
618out:
619 if (ret != 0 || !keep_cs)
620 spi_set_cs(msg->spi, false);
621
622 if (msg->status == -EINPROGRESS)
623 msg->status = ret;
624
625 spi_finalize_current_message(master);
626
627 return ret;
628}
629
630/**
631 * spi_finalize_current_transfer - report completion of a transfer
632 *
633 * Called by SPI drivers using the core transfer_one_message()
634 * implementation to notify it that the current interrupt driven
635 * transfer has finised and the next one may be scheduled.
636 */
637void spi_finalize_current_transfer(struct spi_master *master)
638{
639 complete(&master->xfer_completion);
640}
641EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
642
ffbbdd21
LW
643/**
644 * spi_pump_messages - kthread work function which processes spi message queue
645 * @work: pointer to kthread work struct contained in the master struct
646 *
647 * This function checks if there is any spi message in the queue that
648 * needs processing and if so call out to the driver to initialize hardware
649 * and transfer each message.
650 *
651 */
652static void spi_pump_messages(struct kthread_work *work)
653{
654 struct spi_master *master =
655 container_of(work, struct spi_master, pump_messages);
656 unsigned long flags;
657 bool was_busy = false;
658 int ret;
659
660 /* Lock queue and check for queue work */
661 spin_lock_irqsave(&master->queue_lock, flags);
662 if (list_empty(&master->queue) || !master->running) {
b0b36b86
BF
663 if (!master->busy) {
664 spin_unlock_irqrestore(&master->queue_lock, flags);
665 return;
ffbbdd21
LW
666 }
667 master->busy = false;
668 spin_unlock_irqrestore(&master->queue_lock, flags);
b0b36b86
BF
669 if (master->unprepare_transfer_hardware &&
670 master->unprepare_transfer_hardware(master))
671 dev_err(&master->dev,
672 "failed to unprepare transfer hardware\n");
49834de2
MB
673 if (master->auto_runtime_pm) {
674 pm_runtime_mark_last_busy(master->dev.parent);
675 pm_runtime_put_autosuspend(master->dev.parent);
676 }
56ec1978 677 trace_spi_master_idle(master);
ffbbdd21
LW
678 return;
679 }
680
681 /* Make sure we are not already running a message */
682 if (master->cur_msg) {
683 spin_unlock_irqrestore(&master->queue_lock, flags);
684 return;
685 }
686 /* Extract head of queue */
687 master->cur_msg =
a89e2d27 688 list_first_entry(&master->queue, struct spi_message, queue);
ffbbdd21
LW
689
690 list_del_init(&master->cur_msg->queue);
691 if (master->busy)
692 was_busy = true;
693 else
694 master->busy = true;
695 spin_unlock_irqrestore(&master->queue_lock, flags);
696
49834de2
MB
697 if (!was_busy && master->auto_runtime_pm) {
698 ret = pm_runtime_get_sync(master->dev.parent);
699 if (ret < 0) {
700 dev_err(&master->dev, "Failed to power device: %d\n",
701 ret);
702 return;
703 }
704 }
705
56ec1978
MB
706 if (!was_busy)
707 trace_spi_master_busy(master);
708
7dfd2bd7 709 if (!was_busy && master->prepare_transfer_hardware) {
ffbbdd21
LW
710 ret = master->prepare_transfer_hardware(master);
711 if (ret) {
712 dev_err(&master->dev,
713 "failed to prepare transfer hardware\n");
49834de2
MB
714
715 if (master->auto_runtime_pm)
716 pm_runtime_put(master->dev.parent);
ffbbdd21
LW
717 return;
718 }
719 }
720
56ec1978
MB
721 trace_spi_message_start(master->cur_msg);
722
2841a5fc
MB
723 if (master->prepare_message) {
724 ret = master->prepare_message(master, master->cur_msg);
725 if (ret) {
726 dev_err(&master->dev,
727 "failed to prepare message: %d\n", ret);
728 master->cur_msg->status = ret;
729 spi_finalize_current_message(master);
730 return;
731 }
732 master->cur_msg_prepared = true;
733 }
734
ffbbdd21
LW
735 ret = master->transfer_one_message(master, master->cur_msg);
736 if (ret) {
737 dev_err(&master->dev,
738 "failed to transfer one message from queue\n");
739 return;
740 }
741}
742
743static int spi_init_queue(struct spi_master *master)
744{
745 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
746
747 INIT_LIST_HEAD(&master->queue);
748 spin_lock_init(&master->queue_lock);
749
750 master->running = false;
751 master->busy = false;
752
753 init_kthread_worker(&master->kworker);
754 master->kworker_task = kthread_run(kthread_worker_fn,
f170168b 755 &master->kworker, "%s",
ffbbdd21
LW
756 dev_name(&master->dev));
757 if (IS_ERR(master->kworker_task)) {
758 dev_err(&master->dev, "failed to create message pump task\n");
759 return -ENOMEM;
760 }
761 init_kthread_work(&master->pump_messages, spi_pump_messages);
762
763 /*
764 * Master config will indicate if this controller should run the
765 * message pump with high (realtime) priority to reduce the transfer
766 * latency on the bus by minimising the delay between a transfer
767 * request and the scheduling of the message pump thread. Without this
768 * setting the message pump thread will remain at default priority.
769 */
770 if (master->rt) {
771 dev_info(&master->dev,
772 "will run message pump with realtime priority\n");
773 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
774 }
775
776 return 0;
777}
778
779/**
780 * spi_get_next_queued_message() - called by driver to check for queued
781 * messages
782 * @master: the master to check for queued messages
783 *
784 * If there are more messages in the queue, the next message is returned from
785 * this call.
786 */
787struct spi_message *spi_get_next_queued_message(struct spi_master *master)
788{
789 struct spi_message *next;
790 unsigned long flags;
791
792 /* get a pointer to the next message, if any */
793 spin_lock_irqsave(&master->queue_lock, flags);
1cfd97f9
AL
794 next = list_first_entry_or_null(&master->queue, struct spi_message,
795 queue);
ffbbdd21
LW
796 spin_unlock_irqrestore(&master->queue_lock, flags);
797
798 return next;
799}
800EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
801
802/**
803 * spi_finalize_current_message() - the current message is complete
804 * @master: the master to return the message to
805 *
806 * Called by the driver to notify the core that the message in the front of the
807 * queue is complete and can be removed from the queue.
808 */
809void spi_finalize_current_message(struct spi_master *master)
810{
811 struct spi_message *mesg;
812 unsigned long flags;
2841a5fc 813 int ret;
ffbbdd21
LW
814
815 spin_lock_irqsave(&master->queue_lock, flags);
816 mesg = master->cur_msg;
817 master->cur_msg = NULL;
818
819 queue_kthread_work(&master->kworker, &master->pump_messages);
820 spin_unlock_irqrestore(&master->queue_lock, flags);
821
2841a5fc
MB
822 if (master->cur_msg_prepared && master->unprepare_message) {
823 ret = master->unprepare_message(master, mesg);
824 if (ret) {
825 dev_err(&master->dev,
826 "failed to unprepare message: %d\n", ret);
827 }
828 }
829 master->cur_msg_prepared = false;
830
ffbbdd21
LW
831 mesg->state = NULL;
832 if (mesg->complete)
833 mesg->complete(mesg->context);
56ec1978
MB
834
835 trace_spi_message_done(mesg);
ffbbdd21
LW
836}
837EXPORT_SYMBOL_GPL(spi_finalize_current_message);
838
839static int spi_start_queue(struct spi_master *master)
840{
841 unsigned long flags;
842
843 spin_lock_irqsave(&master->queue_lock, flags);
844
845 if (master->running || master->busy) {
846 spin_unlock_irqrestore(&master->queue_lock, flags);
847 return -EBUSY;
848 }
849
850 master->running = true;
851 master->cur_msg = NULL;
852 spin_unlock_irqrestore(&master->queue_lock, flags);
853
854 queue_kthread_work(&master->kworker, &master->pump_messages);
855
856 return 0;
857}
858
859static int spi_stop_queue(struct spi_master *master)
860{
861 unsigned long flags;
862 unsigned limit = 500;
863 int ret = 0;
864
865 spin_lock_irqsave(&master->queue_lock, flags);
866
867 /*
868 * This is a bit lame, but is optimized for the common execution path.
869 * A wait_queue on the master->busy could be used, but then the common
870 * execution path (pump_messages) would be required to call wake_up or
871 * friends on every SPI message. Do this instead.
872 */
873 while ((!list_empty(&master->queue) || master->busy) && limit--) {
874 spin_unlock_irqrestore(&master->queue_lock, flags);
875 msleep(10);
876 spin_lock_irqsave(&master->queue_lock, flags);
877 }
878
879 if (!list_empty(&master->queue) || master->busy)
880 ret = -EBUSY;
881 else
882 master->running = false;
883
884 spin_unlock_irqrestore(&master->queue_lock, flags);
885
886 if (ret) {
887 dev_warn(&master->dev,
888 "could not stop message queue\n");
889 return ret;
890 }
891 return ret;
892}
893
894static int spi_destroy_queue(struct spi_master *master)
895{
896 int ret;
897
898 ret = spi_stop_queue(master);
899
900 /*
901 * flush_kthread_worker will block until all work is done.
902 * If the reason that stop_queue timed out is that the work will never
903 * finish, then it does no good to call flush/stop thread, so
904 * return anyway.
905 */
906 if (ret) {
907 dev_err(&master->dev, "problem destroying queue\n");
908 return ret;
909 }
910
911 flush_kthread_worker(&master->kworker);
912 kthread_stop(master->kworker_task);
913
914 return 0;
915}
916
917/**
918 * spi_queued_transfer - transfer function for queued transfers
919 * @spi: spi device which is requesting transfer
920 * @msg: spi message which is to handled is queued to driver queue
921 */
922static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
923{
924 struct spi_master *master = spi->master;
925 unsigned long flags;
926
927 spin_lock_irqsave(&master->queue_lock, flags);
928
929 if (!master->running) {
930 spin_unlock_irqrestore(&master->queue_lock, flags);
931 return -ESHUTDOWN;
932 }
933 msg->actual_length = 0;
934 msg->status = -EINPROGRESS;
935
936 list_add_tail(&msg->queue, &master->queue);
96b3eace 937 if (!master->busy)
ffbbdd21
LW
938 queue_kthread_work(&master->kworker, &master->pump_messages);
939
940 spin_unlock_irqrestore(&master->queue_lock, flags);
941 return 0;
942}
943
944static int spi_master_initialize_queue(struct spi_master *master)
945{
946 int ret;
947
948 master->queued = true;
949 master->transfer = spi_queued_transfer;
b158935f
MB
950 if (!master->transfer_one_message)
951 master->transfer_one_message = spi_transfer_one_message;
ffbbdd21
LW
952
953 /* Initialize and start queue */
954 ret = spi_init_queue(master);
955 if (ret) {
956 dev_err(&master->dev, "problem initializing queue\n");
957 goto err_init_queue;
958 }
959 ret = spi_start_queue(master);
960 if (ret) {
961 dev_err(&master->dev, "problem starting queue\n");
962 goto err_start_queue;
963 }
964
965 return 0;
966
967err_start_queue:
968err_init_queue:
969 spi_destroy_queue(master);
970 return ret;
971}
972
973/*-------------------------------------------------------------------------*/
974
7cb94361 975#if defined(CONFIG_OF)
d57a4282
GL
976/**
977 * of_register_spi_devices() - Register child devices onto the SPI bus
978 * @master: Pointer to spi_master device
979 *
980 * Registers an spi_device for each child node of master node which has a 'reg'
981 * property.
982 */
983static void of_register_spi_devices(struct spi_master *master)
984{
985 struct spi_device *spi;
986 struct device_node *nc;
d57a4282 987 int rc;
89da4293 988 u32 value;
d57a4282
GL
989
990 if (!master->dev.of_node)
991 return;
992
f3b6159e 993 for_each_available_child_of_node(master->dev.of_node, nc) {
d57a4282
GL
994 /* Alloc an spi_device */
995 spi = spi_alloc_device(master);
996 if (!spi) {
997 dev_err(&master->dev, "spi_device alloc error for %s\n",
998 nc->full_name);
999 spi_dev_put(spi);
1000 continue;
1001 }
1002
1003 /* Select device driver */
1004 if (of_modalias_node(nc, spi->modalias,
1005 sizeof(spi->modalias)) < 0) {
1006 dev_err(&master->dev, "cannot find modalias for %s\n",
1007 nc->full_name);
1008 spi_dev_put(spi);
1009 continue;
1010 }
1011
1012 /* Device address */
89da4293
TP
1013 rc = of_property_read_u32(nc, "reg", &value);
1014 if (rc) {
1015 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1016 nc->full_name, rc);
d57a4282
GL
1017 spi_dev_put(spi);
1018 continue;
1019 }
89da4293 1020 spi->chip_select = value;
d57a4282
GL
1021
1022 /* Mode (clock phase/polarity/etc.) */
1023 if (of_find_property(nc, "spi-cpha", NULL))
1024 spi->mode |= SPI_CPHA;
1025 if (of_find_property(nc, "spi-cpol", NULL))
1026 spi->mode |= SPI_CPOL;
1027 if (of_find_property(nc, "spi-cs-high", NULL))
1028 spi->mode |= SPI_CS_HIGH;
c20151df
LPC
1029 if (of_find_property(nc, "spi-3wire", NULL))
1030 spi->mode |= SPI_3WIRE;
d57a4282 1031
f477b7fb 1032 /* Device DUAL/QUAD mode */
89da4293
TP
1033 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1034 switch (value) {
1035 case 1:
a822e99c 1036 break;
89da4293 1037 case 2:
a822e99c
MB
1038 spi->mode |= SPI_TX_DUAL;
1039 break;
89da4293 1040 case 4:
a822e99c
MB
1041 spi->mode |= SPI_TX_QUAD;
1042 break;
1043 default:
1044 dev_err(&master->dev,
a110f93d 1045 "spi-tx-bus-width %d not supported\n",
89da4293 1046 value);
a822e99c
MB
1047 spi_dev_put(spi);
1048 continue;
1049 }
f477b7fb 1050 }
1051
89da4293
TP
1052 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1053 switch (value) {
1054 case 1:
a822e99c 1055 break;
89da4293 1056 case 2:
a822e99c
MB
1057 spi->mode |= SPI_RX_DUAL;
1058 break;
89da4293 1059 case 4:
a822e99c
MB
1060 spi->mode |= SPI_RX_QUAD;
1061 break;
1062 default:
1063 dev_err(&master->dev,
a110f93d 1064 "spi-rx-bus-width %d not supported\n",
89da4293 1065 value);
a822e99c
MB
1066 spi_dev_put(spi);
1067 continue;
1068 }
f477b7fb 1069 }
1070
d57a4282 1071 /* Device speed */
89da4293
TP
1072 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1073 if (rc) {
1074 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1075 nc->full_name, rc);
d57a4282
GL
1076 spi_dev_put(spi);
1077 continue;
1078 }
89da4293 1079 spi->max_speed_hz = value;
d57a4282
GL
1080
1081 /* IRQ */
1082 spi->irq = irq_of_parse_and_map(nc, 0);
1083
1084 /* Store a pointer to the node in the device structure */
1085 of_node_get(nc);
1086 spi->dev.of_node = nc;
1087
1088 /* Register the new device */
70fac17c 1089 request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
d57a4282
GL
1090 rc = spi_add_device(spi);
1091 if (rc) {
1092 dev_err(&master->dev, "spi_device register error %s\n",
1093 nc->full_name);
1094 spi_dev_put(spi);
1095 }
1096
1097 }
1098}
1099#else
1100static void of_register_spi_devices(struct spi_master *master) { }
1101#endif
1102
64bee4d2
MW
1103#ifdef CONFIG_ACPI
1104static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1105{
1106 struct spi_device *spi = data;
1107
1108 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1109 struct acpi_resource_spi_serialbus *sb;
1110
1111 sb = &ares->data.spi_serial_bus;
1112 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1113 spi->chip_select = sb->device_selection;
1114 spi->max_speed_hz = sb->connection_speed;
1115
1116 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1117 spi->mode |= SPI_CPHA;
1118 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1119 spi->mode |= SPI_CPOL;
1120 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1121 spi->mode |= SPI_CS_HIGH;
1122 }
1123 } else if (spi->irq < 0) {
1124 struct resource r;
1125
1126 if (acpi_dev_resource_interrupt(ares, 0, &r))
1127 spi->irq = r.start;
1128 }
1129
1130 /* Always tell the ACPI core to skip this resource */
1131 return 1;
1132}
1133
1134static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1135 void *data, void **return_value)
1136{
1137 struct spi_master *master = data;
1138 struct list_head resource_list;
1139 struct acpi_device *adev;
1140 struct spi_device *spi;
1141 int ret;
1142
1143 if (acpi_bus_get_device(handle, &adev))
1144 return AE_OK;
1145 if (acpi_bus_get_status(adev) || !adev->status.present)
1146 return AE_OK;
1147
1148 spi = spi_alloc_device(master);
1149 if (!spi) {
1150 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1151 dev_name(&adev->dev));
1152 return AE_NO_MEMORY;
1153 }
1154
7b199811 1155 ACPI_COMPANION_SET(&spi->dev, adev);
64bee4d2
MW
1156 spi->irq = -1;
1157
1158 INIT_LIST_HEAD(&resource_list);
1159 ret = acpi_dev_get_resources(adev, &resource_list,
1160 acpi_spi_add_resource, spi);
1161 acpi_dev_free_resource_list(&resource_list);
1162
1163 if (ret < 0 || !spi->max_speed_hz) {
1164 spi_dev_put(spi);
1165 return AE_OK;
1166 }
1167
33cf00e5 1168 adev->power.flags.ignore_parent = true;
cf9eb39c 1169 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
64bee4d2 1170 if (spi_add_device(spi)) {
33cf00e5 1171 adev->power.flags.ignore_parent = false;
64bee4d2
MW
1172 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1173 dev_name(&adev->dev));
1174 spi_dev_put(spi);
1175 }
1176
1177 return AE_OK;
1178}
1179
1180static void acpi_register_spi_devices(struct spi_master *master)
1181{
1182 acpi_status status;
1183 acpi_handle handle;
1184
29896178 1185 handle = ACPI_HANDLE(master->dev.parent);
64bee4d2
MW
1186 if (!handle)
1187 return;
1188
1189 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1190 acpi_spi_add_device, NULL,
1191 master, NULL);
1192 if (ACPI_FAILURE(status))
1193 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1194}
1195#else
1196static inline void acpi_register_spi_devices(struct spi_master *master) {}
1197#endif /* CONFIG_ACPI */
1198
49dce689 1199static void spi_master_release(struct device *dev)
8ae12a0d
DB
1200{
1201 struct spi_master *master;
1202
49dce689 1203 master = container_of(dev, struct spi_master, dev);
8ae12a0d
DB
1204 kfree(master);
1205}
1206
1207static struct class spi_master_class = {
1208 .name = "spi_master",
1209 .owner = THIS_MODULE,
49dce689 1210 .dev_release = spi_master_release,
8ae12a0d
DB
1211};
1212
1213
ffbbdd21 1214
8ae12a0d
DB
1215/**
1216 * spi_alloc_master - allocate SPI master controller
1217 * @dev: the controller, possibly using the platform_bus
33e34dc6 1218 * @size: how much zeroed driver-private data to allocate; the pointer to this
49dce689 1219 * memory is in the driver_data field of the returned device,
0c868461 1220 * accessible with spi_master_get_devdata().
33e34dc6 1221 * Context: can sleep
8ae12a0d
DB
1222 *
1223 * This call is used only by SPI master controller drivers, which are the
1224 * only ones directly touching chip registers. It's how they allocate
ba1a0513 1225 * an spi_master structure, prior to calling spi_register_master().
8ae12a0d
DB
1226 *
1227 * This must be called from context that can sleep. It returns the SPI
1228 * master structure on success, else NULL.
1229 *
1230 * The caller is responsible for assigning the bus number and initializing
ba1a0513 1231 * the master's methods before calling spi_register_master(); and (after errors
eb4af0f5
UKK
1232 * adding the device) calling spi_master_put() and kfree() to prevent a memory
1233 * leak.
8ae12a0d 1234 */
e9d5a461 1235struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
8ae12a0d
DB
1236{
1237 struct spi_master *master;
1238
0c868461
DB
1239 if (!dev)
1240 return NULL;
1241
5fe5f05e 1242 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
8ae12a0d
DB
1243 if (!master)
1244 return NULL;
1245
49dce689 1246 device_initialize(&master->dev);
1e8a52e1
GL
1247 master->bus_num = -1;
1248 master->num_chipselect = 1;
49dce689
TJ
1249 master->dev.class = &spi_master_class;
1250 master->dev.parent = get_device(dev);
0c868461 1251 spi_master_set_devdata(master, &master[1]);
8ae12a0d
DB
1252
1253 return master;
1254}
1255EXPORT_SYMBOL_GPL(spi_alloc_master);
1256
74317984
JCPV
1257#ifdef CONFIG_OF
1258static int of_spi_register_master(struct spi_master *master)
1259{
e80beb27 1260 int nb, i, *cs;
74317984
JCPV
1261 struct device_node *np = master->dev.of_node;
1262
1263 if (!np)
1264 return 0;
1265
1266 nb = of_gpio_named_count(np, "cs-gpios");
5fe5f05e 1267 master->num_chipselect = max_t(int, nb, master->num_chipselect);
74317984 1268
8ec5d84e
AL
1269 /* Return error only for an incorrectly formed cs-gpios property */
1270 if (nb == 0 || nb == -ENOENT)
74317984 1271 return 0;
8ec5d84e
AL
1272 else if (nb < 0)
1273 return nb;
74317984
JCPV
1274
1275 cs = devm_kzalloc(&master->dev,
1276 sizeof(int) * master->num_chipselect,
1277 GFP_KERNEL);
1278 master->cs_gpios = cs;
1279
1280 if (!master->cs_gpios)
1281 return -ENOMEM;
1282
0da83bb1 1283 for (i = 0; i < master->num_chipselect; i++)
446411e1 1284 cs[i] = -ENOENT;
74317984
JCPV
1285
1286 for (i = 0; i < nb; i++)
1287 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1288
1289 return 0;
1290}
1291#else
1292static int of_spi_register_master(struct spi_master *master)
1293{
1294 return 0;
1295}
1296#endif
1297
8ae12a0d
DB
1298/**
1299 * spi_register_master - register SPI master controller
1300 * @master: initialized master, originally from spi_alloc_master()
33e34dc6 1301 * Context: can sleep
8ae12a0d
DB
1302 *
1303 * SPI master controllers connect to their drivers using some non-SPI bus,
1304 * such as the platform bus. The final stage of probe() in that code
1305 * includes calling spi_register_master() to hook up to this SPI bus glue.
1306 *
1307 * SPI controllers use board specific (often SOC specific) bus numbers,
1308 * and board-specific addressing for SPI devices combines those numbers
1309 * with chip select numbers. Since SPI does not directly support dynamic
1310 * device identification, boards need configuration tables telling which
1311 * chip is at which address.
1312 *
1313 * This must be called from context that can sleep. It returns zero on
1314 * success, else a negative error code (dropping the master's refcount).
0c868461
DB
1315 * After a successful return, the caller is responsible for calling
1316 * spi_unregister_master().
8ae12a0d 1317 */
e9d5a461 1318int spi_register_master(struct spi_master *master)
8ae12a0d 1319{
e44a45ae 1320 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
49dce689 1321 struct device *dev = master->dev.parent;
2b9603a0 1322 struct boardinfo *bi;
8ae12a0d
DB
1323 int status = -ENODEV;
1324 int dynamic = 0;
1325
0c868461
DB
1326 if (!dev)
1327 return -ENODEV;
1328
74317984
JCPV
1329 status = of_spi_register_master(master);
1330 if (status)
1331 return status;
1332
082c8cb4
DB
1333 /* even if it's just one always-selected device, there must
1334 * be at least one chipselect
1335 */
1336 if (master->num_chipselect == 0)
1337 return -EINVAL;
1338
bb29785e
GL
1339 if ((master->bus_num < 0) && master->dev.of_node)
1340 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1341
8ae12a0d 1342 /* convention: dynamically assigned bus IDs count down from the max */
a020ed75 1343 if (master->bus_num < 0) {
082c8cb4
DB
1344 /* FIXME switch to an IDR based scheme, something like
1345 * I2C now uses, so we can't run out of "dynamic" IDs
1346 */
8ae12a0d 1347 master->bus_num = atomic_dec_return(&dyn_bus_id);
b885244e 1348 dynamic = 1;
8ae12a0d
DB
1349 }
1350
cf32b71e
ES
1351 spin_lock_init(&master->bus_lock_spinlock);
1352 mutex_init(&master->bus_lock_mutex);
1353 master->bus_lock_flag = 0;
b158935f 1354 init_completion(&master->xfer_completion);
cf32b71e 1355
8ae12a0d
DB
1356 /* register the device, then userspace will see it.
1357 * registration fails if the bus ID is in use.
1358 */
35f74fca 1359 dev_set_name(&master->dev, "spi%u", master->bus_num);
49dce689 1360 status = device_add(&master->dev);
b885244e 1361 if (status < 0)
8ae12a0d 1362 goto done;
35f74fca 1363 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
8ae12a0d
DB
1364 dynamic ? " (dynamic)" : "");
1365
ffbbdd21
LW
1366 /* If we're using a queued driver, start the queue */
1367 if (master->transfer)
1368 dev_info(dev, "master is unqueued, this is deprecated\n");
1369 else {
1370 status = spi_master_initialize_queue(master);
1371 if (status) {
e93b0724 1372 device_del(&master->dev);
ffbbdd21
LW
1373 goto done;
1374 }
1375 }
1376
2b9603a0
FT
1377 mutex_lock(&board_lock);
1378 list_add_tail(&master->list, &spi_master_list);
1379 list_for_each_entry(bi, &board_list, list)
1380 spi_match_master_to_boardinfo(master, &bi->board_info);
1381 mutex_unlock(&board_lock);
1382
64bee4d2 1383 /* Register devices from the device tree and ACPI */
12b15e83 1384 of_register_spi_devices(master);
64bee4d2 1385 acpi_register_spi_devices(master);
8ae12a0d
DB
1386done:
1387 return status;
1388}
1389EXPORT_SYMBOL_GPL(spi_register_master);
1390
666d5b4c
MB
1391static void devm_spi_unregister(struct device *dev, void *res)
1392{
1393 spi_unregister_master(*(struct spi_master **)res);
1394}
1395
1396/**
1397 * dev_spi_register_master - register managed SPI master controller
1398 * @dev: device managing SPI master
1399 * @master: initialized master, originally from spi_alloc_master()
1400 * Context: can sleep
1401 *
1402 * Register a SPI device as with spi_register_master() which will
1403 * automatically be unregister
1404 */
1405int devm_spi_register_master(struct device *dev, struct spi_master *master)
1406{
1407 struct spi_master **ptr;
1408 int ret;
1409
1410 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1411 if (!ptr)
1412 return -ENOMEM;
1413
1414 ret = spi_register_master(master);
1415 if (ret != 0) {
1416 *ptr = master;
1417 devres_add(dev, ptr);
1418 } else {
1419 devres_free(ptr);
1420 }
1421
1422 return ret;
1423}
1424EXPORT_SYMBOL_GPL(devm_spi_register_master);
1425
34860089 1426static int __unregister(struct device *dev, void *null)
8ae12a0d 1427{
34860089 1428 spi_unregister_device(to_spi_device(dev));
8ae12a0d
DB
1429 return 0;
1430}
1431
1432/**
1433 * spi_unregister_master - unregister SPI master controller
1434 * @master: the master being unregistered
33e34dc6 1435 * Context: can sleep
8ae12a0d
DB
1436 *
1437 * This call is used only by SPI master controller drivers, which are the
1438 * only ones directly touching chip registers.
1439 *
1440 * This must be called from context that can sleep.
1441 */
1442void spi_unregister_master(struct spi_master *master)
1443{
89fc9a1a
JG
1444 int dummy;
1445
ffbbdd21
LW
1446 if (master->queued) {
1447 if (spi_destroy_queue(master))
1448 dev_err(&master->dev, "queue remove failed\n");
1449 }
1450
2b9603a0
FT
1451 mutex_lock(&board_lock);
1452 list_del(&master->list);
1453 mutex_unlock(&board_lock);
1454
97dbf37d 1455 dummy = device_for_each_child(&master->dev, NULL, __unregister);
49dce689 1456 device_unregister(&master->dev);
8ae12a0d
DB
1457}
1458EXPORT_SYMBOL_GPL(spi_unregister_master);
1459
ffbbdd21
LW
1460int spi_master_suspend(struct spi_master *master)
1461{
1462 int ret;
1463
1464 /* Basically no-ops for non-queued masters */
1465 if (!master->queued)
1466 return 0;
1467
1468 ret = spi_stop_queue(master);
1469 if (ret)
1470 dev_err(&master->dev, "queue stop failed\n");
1471
1472 return ret;
1473}
1474EXPORT_SYMBOL_GPL(spi_master_suspend);
1475
1476int spi_master_resume(struct spi_master *master)
1477{
1478 int ret;
1479
1480 if (!master->queued)
1481 return 0;
1482
1483 ret = spi_start_queue(master);
1484 if (ret)
1485 dev_err(&master->dev, "queue restart failed\n");
1486
1487 return ret;
1488}
1489EXPORT_SYMBOL_GPL(spi_master_resume);
1490
9f3b795a 1491static int __spi_master_match(struct device *dev, const void *data)
5ed2c832
DY
1492{
1493 struct spi_master *m;
9f3b795a 1494 const u16 *bus_num = data;
5ed2c832
DY
1495
1496 m = container_of(dev, struct spi_master, dev);
1497 return m->bus_num == *bus_num;
1498}
1499
8ae12a0d
DB
1500/**
1501 * spi_busnum_to_master - look up master associated with bus_num
1502 * @bus_num: the master's bus number
33e34dc6 1503 * Context: can sleep
8ae12a0d
DB
1504 *
1505 * This call may be used with devices that are registered after
1506 * arch init time. It returns a refcounted pointer to the relevant
1507 * spi_master (which the caller must release), or NULL if there is
1508 * no such master registered.
1509 */
1510struct spi_master *spi_busnum_to_master(u16 bus_num)
1511{
49dce689 1512 struct device *dev;
1e9a51dc 1513 struct spi_master *master = NULL;
5ed2c832 1514
695794ae 1515 dev = class_find_device(&spi_master_class, NULL, &bus_num,
5ed2c832
DY
1516 __spi_master_match);
1517 if (dev)
1518 master = container_of(dev, struct spi_master, dev);
1519 /* reference got in class_find_device */
1e9a51dc 1520 return master;
8ae12a0d
DB
1521}
1522EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1523
1524
1525/*-------------------------------------------------------------------------*/
1526
7d077197
DB
1527/* Core methods for SPI master protocol drivers. Some of the
1528 * other core methods are currently defined as inline functions.
1529 */
1530
1531/**
1532 * spi_setup - setup SPI mode and clock rate
1533 * @spi: the device whose settings are being modified
1534 * Context: can sleep, and no requests are queued to the device
1535 *
1536 * SPI protocol drivers may need to update the transfer mode if the
1537 * device doesn't work with its default. They may likewise need
1538 * to update clock rates or word sizes from initial values. This function
1539 * changes those settings, and must be called from a context that can sleep.
1540 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
1541 * effect the next time the device is selected and data is transferred to
1542 * or from it. When this function returns, the spi device is deselected.
1543 *
1544 * Note that this call will fail if the protocol driver specifies an option
1545 * that the underlying controller or its driver does not support. For
1546 * example, not all hardware supports wire transfers using nine bit words,
1547 * LSB-first wire encoding, or active-high chipselects.
1548 */
1549int spi_setup(struct spi_device *spi)
1550{
e7db06b5 1551 unsigned bad_bits;
caae070c 1552 int status = 0;
7d077197 1553
f477b7fb 1554 /* check mode to prevent that DUAL and QUAD set at the same time
1555 */
1556 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1557 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1558 dev_err(&spi->dev,
1559 "setup: can not select dual and quad at the same time\n");
1560 return -EINVAL;
1561 }
1562 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1563 */
1564 if ((spi->mode & SPI_3WIRE) && (spi->mode &
1565 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1566 return -EINVAL;
e7db06b5
DB
1567 /* help drivers fail *cleanly* when they need options
1568 * that aren't supported with their current master
1569 */
1570 bad_bits = spi->mode & ~spi->master->mode_bits;
1571 if (bad_bits) {
eb288a1f 1572 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
e7db06b5
DB
1573 bad_bits);
1574 return -EINVAL;
1575 }
1576
7d077197
DB
1577 if (!spi->bits_per_word)
1578 spi->bits_per_word = 8;
1579
caae070c
LD
1580 if (spi->master->setup)
1581 status = spi->master->setup(spi);
7d077197 1582
5fe5f05e 1583 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
7d077197
DB
1584 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
1585 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
1586 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
1587 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
1588 (spi->mode & SPI_LOOP) ? "loopback, " : "",
1589 spi->bits_per_word, spi->max_speed_hz,
1590 status);
1591
1592 return status;
1593}
1594EXPORT_SYMBOL_GPL(spi_setup);
1595
90808738 1596static int __spi_validate(struct spi_device *spi, struct spi_message *message)
cf32b71e
ES
1597{
1598 struct spi_master *master = spi->master;
e6811d1d 1599 struct spi_transfer *xfer;
cf32b71e 1600
24a0013a
MB
1601 if (list_empty(&message->transfers))
1602 return -EINVAL;
1603 if (!message->complete)
1604 return -EINVAL;
1605
cf32b71e
ES
1606 /* Half-duplex links include original MicroWire, and ones with
1607 * only one data pin like SPI_3WIRE (switches direction) or where
1608 * either MOSI or MISO is missing. They can also be caused by
1609 * software limitations.
1610 */
1611 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
1612 || (spi->mode & SPI_3WIRE)) {
cf32b71e
ES
1613 unsigned flags = master->flags;
1614
1615 list_for_each_entry(xfer, &message->transfers, transfer_list) {
1616 if (xfer->rx_buf && xfer->tx_buf)
1617 return -EINVAL;
1618 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
1619 return -EINVAL;
1620 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
1621 return -EINVAL;
1622 }
1623 }
1624
e6811d1d 1625 /**
059b8ffe
LD
1626 * Set transfer bits_per_word and max speed as spi device default if
1627 * it is not set for this transfer.
f477b7fb 1628 * Set transfer tx_nbits and rx_nbits as single transfer default
1629 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
e6811d1d
LD
1630 */
1631 list_for_each_entry(xfer, &message->transfers, transfer_list) {
078726ce 1632 message->frame_length += xfer->len;
e6811d1d
LD
1633 if (!xfer->bits_per_word)
1634 xfer->bits_per_word = spi->bits_per_word;
56ede94a 1635 if (!xfer->speed_hz) {
059b8ffe 1636 xfer->speed_hz = spi->max_speed_hz;
56ede94a
GJ
1637 if (master->max_speed_hz &&
1638 xfer->speed_hz > master->max_speed_hz)
1639 xfer->speed_hz = master->max_speed_hz;
1640 }
1641
543bb255
SW
1642 if (master->bits_per_word_mask) {
1643 /* Only 32 bits fit in the mask */
1644 if (xfer->bits_per_word > 32)
1645 return -EINVAL;
1646 if (!(master->bits_per_word_mask &
1647 BIT(xfer->bits_per_word - 1)))
1648 return -EINVAL;
1649 }
a2fd4f9f
MB
1650
1651 if (xfer->speed_hz && master->min_speed_hz &&
1652 xfer->speed_hz < master->min_speed_hz)
1653 return -EINVAL;
1654 if (xfer->speed_hz && master->max_speed_hz &&
1655 xfer->speed_hz > master->max_speed_hz)
d5ee722a 1656 return -EINVAL;
f477b7fb 1657
1658 if (xfer->tx_buf && !xfer->tx_nbits)
1659 xfer->tx_nbits = SPI_NBITS_SINGLE;
1660 if (xfer->rx_buf && !xfer->rx_nbits)
1661 xfer->rx_nbits = SPI_NBITS_SINGLE;
1662 /* check transfer tx/rx_nbits:
1663 * 1. keep the value is not out of single, dual and quad
1664 * 2. keep tx/rx_nbits is contained by mode in spi_device
1665 * 3. if SPI_3WIRE, tx/rx_nbits should be in single
1666 */
db90a441
SP
1667 if (xfer->tx_buf) {
1668 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
1669 xfer->tx_nbits != SPI_NBITS_DUAL &&
1670 xfer->tx_nbits != SPI_NBITS_QUAD)
1671 return -EINVAL;
1672 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
1673 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
1674 return -EINVAL;
1675 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
1676 !(spi->mode & SPI_TX_QUAD))
1677 return -EINVAL;
db90a441 1678 }
f477b7fb 1679 /* check transfer rx_nbits */
db90a441
SP
1680 if (xfer->rx_buf) {
1681 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
1682 xfer->rx_nbits != SPI_NBITS_DUAL &&
1683 xfer->rx_nbits != SPI_NBITS_QUAD)
1684 return -EINVAL;
1685 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
1686 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
1687 return -EINVAL;
1688 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
1689 !(spi->mode & SPI_RX_QUAD))
1690 return -EINVAL;
db90a441 1691 }
e6811d1d
LD
1692 }
1693
cf32b71e 1694 message->status = -EINPROGRESS;
90808738
MB
1695
1696 return 0;
1697}
1698
1699static int __spi_async(struct spi_device *spi, struct spi_message *message)
1700{
1701 struct spi_master *master = spi->master;
1702
1703 message->spi = spi;
1704
1705 trace_spi_message_submit(message);
1706
cf32b71e
ES
1707 return master->transfer(spi, message);
1708}
1709
568d0697
DB
1710/**
1711 * spi_async - asynchronous SPI transfer
1712 * @spi: device with which data will be exchanged
1713 * @message: describes the data transfers, including completion callback
1714 * Context: any (irqs may be blocked, etc)
1715 *
1716 * This call may be used in_irq and other contexts which can't sleep,
1717 * as well as from task contexts which can sleep.
1718 *
1719 * The completion callback is invoked in a context which can't sleep.
1720 * Before that invocation, the value of message->status is undefined.
1721 * When the callback is issued, message->status holds either zero (to
1722 * indicate complete success) or a negative error code. After that
1723 * callback returns, the driver which issued the transfer request may
1724 * deallocate the associated memory; it's no longer in use by any SPI
1725 * core or controller driver code.
1726 *
1727 * Note that although all messages to a spi_device are handled in
1728 * FIFO order, messages may go to different devices in other orders.
1729 * Some device might be higher priority, or have various "hard" access
1730 * time requirements, for example.
1731 *
1732 * On detection of any fault during the transfer, processing of
1733 * the entire message is aborted, and the device is deselected.
1734 * Until returning from the associated message completion callback,
1735 * no other spi_message queued to that device will be processed.
1736 * (This rule applies equally to all the synchronous transfer calls,
1737 * which are wrappers around this core asynchronous primitive.)
1738 */
1739int spi_async(struct spi_device *spi, struct spi_message *message)
1740{
1741 struct spi_master *master = spi->master;
cf32b71e
ES
1742 int ret;
1743 unsigned long flags;
568d0697 1744
90808738
MB
1745 ret = __spi_validate(spi, message);
1746 if (ret != 0)
1747 return ret;
1748
cf32b71e 1749 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
568d0697 1750
cf32b71e
ES
1751 if (master->bus_lock_flag)
1752 ret = -EBUSY;
1753 else
1754 ret = __spi_async(spi, message);
568d0697 1755
cf32b71e
ES
1756 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1757
1758 return ret;
568d0697
DB
1759}
1760EXPORT_SYMBOL_GPL(spi_async);
1761
cf32b71e
ES
1762/**
1763 * spi_async_locked - version of spi_async with exclusive bus usage
1764 * @spi: device with which data will be exchanged
1765 * @message: describes the data transfers, including completion callback
1766 * Context: any (irqs may be blocked, etc)
1767 *
1768 * This call may be used in_irq and other contexts which can't sleep,
1769 * as well as from task contexts which can sleep.
1770 *
1771 * The completion callback is invoked in a context which can't sleep.
1772 * Before that invocation, the value of message->status is undefined.
1773 * When the callback is issued, message->status holds either zero (to
1774 * indicate complete success) or a negative error code. After that
1775 * callback returns, the driver which issued the transfer request may
1776 * deallocate the associated memory; it's no longer in use by any SPI
1777 * core or controller driver code.
1778 *
1779 * Note that although all messages to a spi_device are handled in
1780 * FIFO order, messages may go to different devices in other orders.
1781 * Some device might be higher priority, or have various "hard" access
1782 * time requirements, for example.
1783 *
1784 * On detection of any fault during the transfer, processing of
1785 * the entire message is aborted, and the device is deselected.
1786 * Until returning from the associated message completion callback,
1787 * no other spi_message queued to that device will be processed.
1788 * (This rule applies equally to all the synchronous transfer calls,
1789 * which are wrappers around this core asynchronous primitive.)
1790 */
1791int spi_async_locked(struct spi_device *spi, struct spi_message *message)
1792{
1793 struct spi_master *master = spi->master;
1794 int ret;
1795 unsigned long flags;
1796
90808738
MB
1797 ret = __spi_validate(spi, message);
1798 if (ret != 0)
1799 return ret;
1800
cf32b71e
ES
1801 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1802
1803 ret = __spi_async(spi, message);
1804
1805 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1806
1807 return ret;
1808
1809}
1810EXPORT_SYMBOL_GPL(spi_async_locked);
1811
7d077197
DB
1812
1813/*-------------------------------------------------------------------------*/
1814
1815/* Utility methods for SPI master protocol drivers, layered on
1816 * top of the core. Some other utility methods are defined as
1817 * inline functions.
1818 */
1819
5d870c8e
AM
1820static void spi_complete(void *arg)
1821{
1822 complete(arg);
1823}
1824
cf32b71e
ES
1825static int __spi_sync(struct spi_device *spi, struct spi_message *message,
1826 int bus_locked)
1827{
1828 DECLARE_COMPLETION_ONSTACK(done);
1829 int status;
1830 struct spi_master *master = spi->master;
1831
1832 message->complete = spi_complete;
1833 message->context = &done;
1834
1835 if (!bus_locked)
1836 mutex_lock(&master->bus_lock_mutex);
1837
1838 status = spi_async_locked(spi, message);
1839
1840 if (!bus_locked)
1841 mutex_unlock(&master->bus_lock_mutex);
1842
1843 if (status == 0) {
1844 wait_for_completion(&done);
1845 status = message->status;
1846 }
1847 message->context = NULL;
1848 return status;
1849}
1850
8ae12a0d
DB
1851/**
1852 * spi_sync - blocking/synchronous SPI data transfers
1853 * @spi: device with which data will be exchanged
1854 * @message: describes the data transfers
33e34dc6 1855 * Context: can sleep
8ae12a0d
DB
1856 *
1857 * This call may only be used from a context that may sleep. The sleep
1858 * is non-interruptible, and has no timeout. Low-overhead controller
1859 * drivers may DMA directly into and out of the message buffers.
1860 *
1861 * Note that the SPI device's chip select is active during the message,
1862 * and then is normally disabled between messages. Drivers for some
1863 * frequently-used devices may want to minimize costs of selecting a chip,
1864 * by leaving it selected in anticipation that the next message will go
1865 * to the same chip. (That may increase power usage.)
1866 *
0c868461
DB
1867 * Also, the caller is guaranteeing that the memory associated with the
1868 * message will not be freed before this call returns.
1869 *
9b938b74 1870 * It returns zero on success, else a negative error code.
8ae12a0d
DB
1871 */
1872int spi_sync(struct spi_device *spi, struct spi_message *message)
1873{
cf32b71e 1874 return __spi_sync(spi, message, 0);
8ae12a0d
DB
1875}
1876EXPORT_SYMBOL_GPL(spi_sync);
1877
cf32b71e
ES
1878/**
1879 * spi_sync_locked - version of spi_sync with exclusive bus usage
1880 * @spi: device with which data will be exchanged
1881 * @message: describes the data transfers
1882 * Context: can sleep
1883 *
1884 * This call may only be used from a context that may sleep. The sleep
1885 * is non-interruptible, and has no timeout. Low-overhead controller
1886 * drivers may DMA directly into and out of the message buffers.
1887 *
1888 * This call should be used by drivers that require exclusive access to the
25985edc 1889 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
cf32b71e
ES
1890 * be released by a spi_bus_unlock call when the exclusive access is over.
1891 *
1892 * It returns zero on success, else a negative error code.
1893 */
1894int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
1895{
1896 return __spi_sync(spi, message, 1);
1897}
1898EXPORT_SYMBOL_GPL(spi_sync_locked);
1899
1900/**
1901 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
1902 * @master: SPI bus master that should be locked for exclusive bus access
1903 * Context: can sleep
1904 *
1905 * This call may only be used from a context that may sleep. The sleep
1906 * is non-interruptible, and has no timeout.
1907 *
1908 * This call should be used by drivers that require exclusive access to the
1909 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
1910 * exclusive access is over. Data transfer must be done by spi_sync_locked
1911 * and spi_async_locked calls when the SPI bus lock is held.
1912 *
1913 * It returns zero on success, else a negative error code.
1914 */
1915int spi_bus_lock(struct spi_master *master)
1916{
1917 unsigned long flags;
1918
1919 mutex_lock(&master->bus_lock_mutex);
1920
1921 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1922 master->bus_lock_flag = 1;
1923 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1924
1925 /* mutex remains locked until spi_bus_unlock is called */
1926
1927 return 0;
1928}
1929EXPORT_SYMBOL_GPL(spi_bus_lock);
1930
1931/**
1932 * spi_bus_unlock - release the lock for exclusive SPI bus usage
1933 * @master: SPI bus master that was locked for exclusive bus access
1934 * Context: can sleep
1935 *
1936 * This call may only be used from a context that may sleep. The sleep
1937 * is non-interruptible, and has no timeout.
1938 *
1939 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
1940 * call.
1941 *
1942 * It returns zero on success, else a negative error code.
1943 */
1944int spi_bus_unlock(struct spi_master *master)
1945{
1946 master->bus_lock_flag = 0;
1947
1948 mutex_unlock(&master->bus_lock_mutex);
1949
1950 return 0;
1951}
1952EXPORT_SYMBOL_GPL(spi_bus_unlock);
1953
a9948b61 1954/* portable code must never pass more than 32 bytes */
5fe5f05e 1955#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
8ae12a0d
DB
1956
1957static u8 *buf;
1958
1959/**
1960 * spi_write_then_read - SPI synchronous write followed by read
1961 * @spi: device with which data will be exchanged
1962 * @txbuf: data to be written (need not be dma-safe)
1963 * @n_tx: size of txbuf, in bytes
27570497
JP
1964 * @rxbuf: buffer into which data will be read (need not be dma-safe)
1965 * @n_rx: size of rxbuf, in bytes
33e34dc6 1966 * Context: can sleep
8ae12a0d
DB
1967 *
1968 * This performs a half duplex MicroWire style transaction with the
1969 * device, sending txbuf and then reading rxbuf. The return value
1970 * is zero for success, else a negative errno status code.
b885244e 1971 * This call may only be used from a context that may sleep.
8ae12a0d 1972 *
0c868461 1973 * Parameters to this routine are always copied using a small buffer;
33e34dc6
DB
1974 * portable code should never use this for more than 32 bytes.
1975 * Performance-sensitive or bulk transfer code should instead use
0c868461 1976 * spi_{async,sync}() calls with dma-safe buffers.
8ae12a0d
DB
1977 */
1978int spi_write_then_read(struct spi_device *spi,
0c4a1590
MB
1979 const void *txbuf, unsigned n_tx,
1980 void *rxbuf, unsigned n_rx)
8ae12a0d 1981{
068f4070 1982 static DEFINE_MUTEX(lock);
8ae12a0d
DB
1983
1984 int status;
1985 struct spi_message message;
bdff549e 1986 struct spi_transfer x[2];
8ae12a0d
DB
1987 u8 *local_buf;
1988
b3a223ee
MB
1989 /* Use preallocated DMA-safe buffer if we can. We can't avoid
1990 * copying here, (as a pure convenience thing), but we can
1991 * keep heap costs out of the hot path unless someone else is
1992 * using the pre-allocated buffer or the transfer is too large.
8ae12a0d 1993 */
b3a223ee 1994 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
2cd94c8a
MB
1995 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
1996 GFP_KERNEL | GFP_DMA);
b3a223ee
MB
1997 if (!local_buf)
1998 return -ENOMEM;
1999 } else {
2000 local_buf = buf;
2001 }
8ae12a0d 2002
8275c642 2003 spi_message_init(&message);
5fe5f05e 2004 memset(x, 0, sizeof(x));
bdff549e
DB
2005 if (n_tx) {
2006 x[0].len = n_tx;
2007 spi_message_add_tail(&x[0], &message);
2008 }
2009 if (n_rx) {
2010 x[1].len = n_rx;
2011 spi_message_add_tail(&x[1], &message);
2012 }
8275c642 2013
8ae12a0d 2014 memcpy(local_buf, txbuf, n_tx);
bdff549e
DB
2015 x[0].tx_buf = local_buf;
2016 x[1].rx_buf = local_buf + n_tx;
8ae12a0d
DB
2017
2018 /* do the i/o */
8ae12a0d 2019 status = spi_sync(spi, &message);
9b938b74 2020 if (status == 0)
bdff549e 2021 memcpy(rxbuf, x[1].rx_buf, n_rx);
8ae12a0d 2022
bdff549e 2023 if (x[0].tx_buf == buf)
068f4070 2024 mutex_unlock(&lock);
8ae12a0d
DB
2025 else
2026 kfree(local_buf);
2027
2028 return status;
2029}
2030EXPORT_SYMBOL_GPL(spi_write_then_read);
2031
2032/*-------------------------------------------------------------------------*/
2033
2034static int __init spi_init(void)
2035{
b885244e
DB
2036 int status;
2037
e94b1766 2038 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
b885244e
DB
2039 if (!buf) {
2040 status = -ENOMEM;
2041 goto err0;
2042 }
2043
2044 status = bus_register(&spi_bus_type);
2045 if (status < 0)
2046 goto err1;
8ae12a0d 2047
b885244e
DB
2048 status = class_register(&spi_master_class);
2049 if (status < 0)
2050 goto err2;
8ae12a0d 2051 return 0;
b885244e
DB
2052
2053err2:
2054 bus_unregister(&spi_bus_type);
2055err1:
2056 kfree(buf);
2057 buf = NULL;
2058err0:
2059 return status;
8ae12a0d 2060}
b885244e 2061
8ae12a0d
DB
2062/* board_info is normally registered in arch_initcall(),
2063 * but even essential drivers wait till later
b885244e
DB
2064 *
2065 * REVISIT only boardinfo really needs static linking. the rest (device and
2066 * driver registration) _could_ be dynamically linked (modular) ... costs
2067 * include needing to have boardinfo data structures be much more public.
8ae12a0d 2068 */
673c0c00 2069postcore_initcall(spi_init);
8ae12a0d 2070
This page took 0.913772 seconds and 5 git commands to generate.