blob: 9136f30a9fbd6db8c5e440ea50875e50deed6285 [file] [log] [blame]
David Brownell8ae12a02006-01-08 13:34:19 -08001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * SPI init/core code
David Brownell8ae12a02006-01-08 13:34:19 -08003 *
4 * Copyright (C) 2005 David Brownell
Grant Likelyd57a4282012-04-07 14:16:53 -06005 * Copyright (C) 2008 Secret Lab Technologies Ltd.
David Brownell8ae12a02006-01-08 13:34:19 -08006 *
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.
David Brownell8ae12a02006-01-08 13:34:19 -080016 */
17
David Brownell8ae12a02006-01-08 13:34:19 -080018#include <linux/kernel.h>
19#include <linux/device.h>
20#include <linux/init.h>
21#include <linux/cache.h>
Mark Brown99adef32014-01-16 12:22:43 +000022#include <linux/dma-mapping.h>
23#include <linux/dmaengine.h>
Matthias Kaehlcke94040822007-07-17 04:04:16 -070024#include <linux/mutex.h>
Sinan Akman2b7a32f2010-10-02 21:28:29 -060025#include <linux/of_device.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060026#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020027#include <linux/clk/clk-conf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Anton Vorontsove0626e32009-09-22 16:46:08 -070029#include <linux/mod_devicetable.h>
David Brownell8ae12a02006-01-08 13:34:19 -080030#include <linux/spi/spi.h>
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +010031#include <linux/of_gpio.h>
Mark Brown3ae22e82010-12-25 15:32:27 +010032#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020033#include <linux/pm_domain.h>
Paul Gortmaker025ed132011-07-10 12:57:55 -040034#include <linux/export.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060035#include <linux/sched/rt.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010036#include <linux/delay.h>
37#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010038#include <linux/ioport.h>
39#include <linux/acpi.h>
David Brownell8ae12a02006-01-08 13:34:19 -080040
Mark Brown56ec1972013-10-07 19:33:53 +010041#define CREATE_TRACE_POINTS
42#include <trace/events/spi.h>
43
David Brownell8ae12a02006-01-08 13:34:19 -080044static void spidev_release(struct device *dev)
45{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080046 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080047
48 /* spi masters may cleanup for released devices */
49 if (spi->master->cleanup)
50 spi->master->cleanup(spi);
51
David Brownell0c8684612006-01-08 13:34:25 -080052 spi_master_put(spi->master);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000053 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080054}
55
56static ssize_t
57modalias_show(struct device *dev, struct device_attribute *a, char *buf)
58{
59 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080060 int len;
61
62 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
63 if (len != -ENODEV)
64 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080065
Grant Likelyd8e328b2012-05-20 00:08:13 -060066 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080067}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070068static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080069
Martin Sperleca2ebc2015-06-22 13:00:36 +000070#define SPI_STATISTICS_ATTRS(field, file) \
71static ssize_t spi_master_##field##_show(struct device *dev, \
72 struct device_attribute *attr, \
73 char *buf) \
74{ \
75 struct spi_master *master = container_of(dev, \
76 struct spi_master, dev); \
77 return spi_statistics_##field##_show(&master->statistics, buf); \
78} \
79static struct device_attribute dev_attr_spi_master_##field = { \
80 .attr = { .name = file, .mode = S_IRUGO }, \
81 .show = spi_master_##field##_show, \
82}; \
83static ssize_t spi_device_##field##_show(struct device *dev, \
84 struct device_attribute *attr, \
85 char *buf) \
86{ \
87 struct spi_device *spi = container_of(dev, \
88 struct spi_device, dev); \
89 return spi_statistics_##field##_show(&spi->statistics, buf); \
90} \
91static struct device_attribute dev_attr_spi_device_##field = { \
92 .attr = { .name = file, .mode = S_IRUGO }, \
93 .show = spi_device_##field##_show, \
94}
95
96#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
97static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
98 char *buf) \
99{ \
100 unsigned long flags; \
101 ssize_t len; \
102 spin_lock_irqsave(&stat->lock, flags); \
103 len = sprintf(buf, format_string, stat->field); \
104 spin_unlock_irqrestore(&stat->lock, flags); \
105 return len; \
106} \
107SPI_STATISTICS_ATTRS(name, file)
108
109#define SPI_STATISTICS_SHOW(field, format_string) \
110 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
111 field, format_string)
112
113SPI_STATISTICS_SHOW(messages, "%lu");
114SPI_STATISTICS_SHOW(transfers, "%lu");
115SPI_STATISTICS_SHOW(errors, "%lu");
116SPI_STATISTICS_SHOW(timedout, "%lu");
117
118SPI_STATISTICS_SHOW(spi_sync, "%lu");
119SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
120SPI_STATISTICS_SHOW(spi_async, "%lu");
121
122SPI_STATISTICS_SHOW(bytes, "%llu");
123SPI_STATISTICS_SHOW(bytes_rx, "%llu");
124SPI_STATISTICS_SHOW(bytes_tx, "%llu");
125
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700126static struct attribute *spi_dev_attrs[] = {
127 &dev_attr_modalias.attr,
128 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800129};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000130
131static const struct attribute_group spi_dev_group = {
132 .attrs = spi_dev_attrs,
133};
134
135static struct attribute *spi_device_statistics_attrs[] = {
136 &dev_attr_spi_device_messages.attr,
137 &dev_attr_spi_device_transfers.attr,
138 &dev_attr_spi_device_errors.attr,
139 &dev_attr_spi_device_timedout.attr,
140 &dev_attr_spi_device_spi_sync.attr,
141 &dev_attr_spi_device_spi_sync_immediate.attr,
142 &dev_attr_spi_device_spi_async.attr,
143 &dev_attr_spi_device_bytes.attr,
144 &dev_attr_spi_device_bytes_rx.attr,
145 &dev_attr_spi_device_bytes_tx.attr,
146 NULL,
147};
148
149static const struct attribute_group spi_device_statistics_group = {
150 .name = "statistics",
151 .attrs = spi_device_statistics_attrs,
152};
153
154static const struct attribute_group *spi_dev_groups[] = {
155 &spi_dev_group,
156 &spi_device_statistics_group,
157 NULL,
158};
159
160static struct attribute *spi_master_statistics_attrs[] = {
161 &dev_attr_spi_master_messages.attr,
162 &dev_attr_spi_master_transfers.attr,
163 &dev_attr_spi_master_errors.attr,
164 &dev_attr_spi_master_timedout.attr,
165 &dev_attr_spi_master_spi_sync.attr,
166 &dev_attr_spi_master_spi_sync_immediate.attr,
167 &dev_attr_spi_master_spi_async.attr,
168 &dev_attr_spi_master_bytes.attr,
169 &dev_attr_spi_master_bytes_rx.attr,
170 &dev_attr_spi_master_bytes_tx.attr,
171 NULL,
172};
173
174static const struct attribute_group spi_master_statistics_group = {
175 .name = "statistics",
176 .attrs = spi_master_statistics_attrs,
177};
178
179static const struct attribute_group *spi_master_groups[] = {
180 &spi_master_statistics_group,
181 NULL,
182};
183
184void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
185 struct spi_transfer *xfer,
186 struct spi_master *master)
187{
188 unsigned long flags;
189
190 spin_lock_irqsave(&stats->lock, flags);
191
192 stats->transfers++;
193
194 stats->bytes += xfer->len;
195 if ((xfer->tx_buf) &&
196 (xfer->tx_buf != master->dummy_tx))
197 stats->bytes_tx += xfer->len;
198 if ((xfer->rx_buf) &&
199 (xfer->rx_buf != master->dummy_rx))
200 stats->bytes_rx += xfer->len;
201
202 spin_unlock_irqrestore(&stats->lock, flags);
203}
204EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800205
206/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
207 * and the sysfs version makes coldplug work too.
208 */
209
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700210static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
211 const struct spi_device *sdev)
212{
213 while (id->name[0]) {
214 if (!strcmp(sdev->modalias, id->name))
215 return id;
216 id++;
217 }
218 return NULL;
219}
220
221const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
222{
223 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
224
225 return spi_match_id(sdrv->id_table, sdev);
226}
227EXPORT_SYMBOL_GPL(spi_get_device_id);
228
David Brownell8ae12a02006-01-08 13:34:19 -0800229static int spi_match_device(struct device *dev, struct device_driver *drv)
230{
231 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700232 const struct spi_driver *sdrv = to_spi_driver(drv);
233
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600234 /* Attempt an OF style match */
235 if (of_driver_match_device(dev, drv))
236 return 1;
237
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100238 /* Then try ACPI */
239 if (acpi_driver_match_device(dev, drv))
240 return 1;
241
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700242 if (sdrv->id_table)
243 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800244
Kay Sievers35f74fc2009-01-06 10:44:37 -0800245 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800246}
247
Kay Sievers7eff2e72007-08-14 15:15:12 +0200248static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800249{
250 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800251 int rc;
252
253 rc = acpi_device_uevent_modalias(dev, env);
254 if (rc != -ENODEV)
255 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800256
Anton Vorontsove0626e32009-09-22 16:46:08 -0700257 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800258 return 0;
259}
260
David Brownell8ae12a02006-01-08 13:34:19 -0800261struct bus_type spi_bus_type = {
262 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700263 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800264 .match = spi_match_device,
265 .uevent = spi_uevent,
David Brownell8ae12a02006-01-08 13:34:19 -0800266};
267EXPORT_SYMBOL_GPL(spi_bus_type);
268
David Brownellb8852442006-01-08 13:34:23 -0800269
270static int spi_drv_probe(struct device *dev)
271{
272 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Jon Hunter44af7922015-10-09 15:45:55 +0100273 struct spi_device *spi = to_spi_device(dev);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300274 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800275
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200276 ret = of_clk_set_defaults(dev->of_node, false);
277 if (ret)
278 return ret;
279
Jon Hunter44af7922015-10-09 15:45:55 +0100280 if (dev->of_node) {
281 spi->irq = of_irq_get(dev->of_node, 0);
282 if (spi->irq == -EPROBE_DEFER)
283 return -EPROBE_DEFER;
284 if (spi->irq < 0)
285 spi->irq = 0;
286 }
287
Ulf Hansson676e7c22014-09-19 20:27:41 +0200288 ret = dev_pm_domain_attach(dev, true);
289 if (ret != -EPROBE_DEFER) {
Jon Hunter44af7922015-10-09 15:45:55 +0100290 ret = sdrv->probe(spi);
Ulf Hansson676e7c22014-09-19 20:27:41 +0200291 if (ret)
292 dev_pm_domain_detach(dev, true);
293 }
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300294
295 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800296}
297
298static int spi_drv_remove(struct device *dev)
299{
300 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300301 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800302
Jean Delvareaec35f42014-02-13 15:28:41 +0100303 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200304 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300305
306 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800307}
308
309static void spi_drv_shutdown(struct device *dev)
310{
311 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
312
313 sdrv->shutdown(to_spi_device(dev));
314}
315
David Brownell33e34dc2007-05-08 00:32:21 -0700316/**
317 * spi_register_driver - register a SPI driver
318 * @sdrv: the driver to register
319 * Context: can sleep
320 */
David Brownellb8852442006-01-08 13:34:23 -0800321int spi_register_driver(struct spi_driver *sdrv)
322{
323 sdrv->driver.bus = &spi_bus_type;
324 if (sdrv->probe)
325 sdrv->driver.probe = spi_drv_probe;
326 if (sdrv->remove)
327 sdrv->driver.remove = spi_drv_remove;
328 if (sdrv->shutdown)
329 sdrv->driver.shutdown = spi_drv_shutdown;
330 return driver_register(&sdrv->driver);
331}
332EXPORT_SYMBOL_GPL(spi_register_driver);
333
David Brownell8ae12a02006-01-08 13:34:19 -0800334/*-------------------------------------------------------------------------*/
335
336/* SPI devices should normally not be created by SPI device drivers; that
337 * would make them board-specific. Similarly with SPI master drivers.
338 * Device registration normally goes into like arch/.../mach.../board-YYY.c
339 * with other readonly (flashable) information about mainboard devices.
340 */
341
342struct boardinfo {
343 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800344 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800345};
346
347static LIST_HEAD(board_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800348static LIST_HEAD(spi_master_list);
349
350/*
351 * Used to protect add/del opertion for board_info list and
352 * spi_master list, and their matching process
353 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700354static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800355
Grant Likelydc87c982008-05-15 16:50:22 -0600356/**
357 * spi_alloc_device - Allocate a new SPI device
358 * @master: Controller to which device is connected
359 * Context: can sleep
360 *
361 * Allows a driver to allocate and initialize a spi_device without
362 * registering it immediately. This allows a driver to directly
363 * fill the spi_device with device parameters before calling
364 * spi_add_device() on it.
365 *
366 * Caller is responsible to call spi_add_device() on the returned
367 * spi_device structure to add it to the SPI master. If the caller
368 * needs to discard the spi_device without adding it, then it should
369 * call spi_dev_put() on it.
370 *
371 * Returns a pointer to the new device, or NULL.
372 */
373struct spi_device *spi_alloc_device(struct spi_master *master)
374{
375 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600376
377 if (!spi_master_get(master))
378 return NULL;
379
Jingoo Han5fe5f052013-10-14 10:31:51 +0900380 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600381 if (!spi) {
Grant Likelydc87c982008-05-15 16:50:22 -0600382 spi_master_put(master);
383 return NULL;
384 }
385
386 spi->master = master;
Laurent Pinchart178db7d2011-12-12 01:15:06 +0100387 spi->dev.parent = &master->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600388 spi->dev.bus = &spi_bus_type;
389 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100390 spi->cs_gpio = -ENOENT;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000391
392 spin_lock_init(&spi->statistics.lock);
393
Grant Likelydc87c982008-05-15 16:50:22 -0600394 device_initialize(&spi->dev);
395 return spi;
396}
397EXPORT_SYMBOL_GPL(spi_alloc_device);
398
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200399static void spi_dev_set_name(struct spi_device *spi)
400{
401 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
402
403 if (adev) {
404 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
405 return;
406 }
407
408 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
409 spi->chip_select);
410}
411
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200412static int spi_dev_check(struct device *dev, void *data)
413{
414 struct spi_device *spi = to_spi_device(dev);
415 struct spi_device *new_spi = data;
416
417 if (spi->master == new_spi->master &&
418 spi->chip_select == new_spi->chip_select)
419 return -EBUSY;
420 return 0;
421}
422
Grant Likelydc87c982008-05-15 16:50:22 -0600423/**
424 * spi_add_device - Add spi_device allocated with spi_alloc_device
425 * @spi: spi_device to register
426 *
427 * Companion function to spi_alloc_device. Devices allocated with
428 * spi_alloc_device can be added onto the spi bus with this function.
429 *
David Brownelle48880e2008-08-15 00:40:44 -0700430 * Returns 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600431 */
432int spi_add_device(struct spi_device *spi)
433{
David Brownelle48880e2008-08-15 00:40:44 -0700434 static DEFINE_MUTEX(spi_add_lock);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100435 struct spi_master *master = spi->master;
436 struct device *dev = master->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600437 int status;
438
439 /* Chipselects are numbered 0..max; validate. */
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100440 if (spi->chip_select >= master->num_chipselect) {
Grant Likelydc87c982008-05-15 16:50:22 -0600441 dev_err(dev, "cs%d >= max %d\n",
442 spi->chip_select,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100443 master->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600444 return -EINVAL;
445 }
446
447 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200448 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700449
450 /* We need to make sure there's no other device with this
451 * chipselect **BEFORE** we call setup(), else we'll trash
452 * its configuration. Lock against concurrent add() calls.
453 */
454 mutex_lock(&spi_add_lock);
455
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200456 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
457 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700458 dev_err(dev, "chipselect %d already in use\n",
459 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700460 goto done;
461 }
462
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100463 if (master->cs_gpios)
464 spi->cs_gpio = master->cs_gpios[spi->chip_select];
465
David Brownelle48880e2008-08-15 00:40:44 -0700466 /* Drivers may modify this initial i/o setup, but will
467 * normally rely on the device being setup. Devices
468 * using SPI_CS_HIGH can't coexist well otherwise...
469 */
David Brownell7d077192009-06-17 16:26:03 -0700470 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600471 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200472 dev_err(dev, "can't setup %s, status %d\n",
473 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700474 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600475 }
476
David Brownelle48880e2008-08-15 00:40:44 -0700477 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600478 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700479 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200480 dev_err(dev, "can't add %s, status %d\n",
481 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700482 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800483 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600484
David Brownelle48880e2008-08-15 00:40:44 -0700485done:
486 mutex_unlock(&spi_add_lock);
487 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600488}
489EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800490
David Brownell33e34dc2007-05-08 00:32:21 -0700491/**
492 * spi_new_device - instantiate one new SPI device
493 * @master: Controller to which device is connected
494 * @chip: Describes the SPI device
495 * Context: can sleep
496 *
497 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800498 * after board init creates the hard-wired devices. Some development
499 * platforms may not be able to use spi_register_board_info though, and
500 * this is exported so that for example a USB or parport based adapter
501 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700502 *
503 * Returns the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800504 */
Adrian Bunke9d5a462007-03-26 21:32:23 -0800505struct spi_device *spi_new_device(struct spi_master *master,
506 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800507{
508 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800509 int status;
510
David Brownell082c8cb2007-07-31 00:39:45 -0700511 /* NOTE: caller did any chip->bus_num checks necessary.
512 *
513 * Also, unless we change the return value convention to use
514 * error-or-pointer (not NULL-or-pointer), troubleshootability
515 * suggests syslogged diagnostics are best here (ugh).
516 */
517
Grant Likelydc87c982008-05-15 16:50:22 -0600518 proxy = spi_alloc_device(master);
519 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800520 return NULL;
521
Grant Likely102eb972008-07-23 21:29:55 -0700522 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
523
David Brownell8ae12a02006-01-08 13:34:19 -0800524 proxy->chip_select = chip->chip_select;
525 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700526 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800527 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700528 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800529 proxy->dev.platform_data = (void *) chip->platform_data;
530 proxy->controller_data = chip->controller_data;
531 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800532
Grant Likelydc87c982008-05-15 16:50:22 -0600533 status = spi_add_device(proxy);
David Brownell8ae12a02006-01-08 13:34:19 -0800534 if (status < 0) {
Grant Likelydc87c982008-05-15 16:50:22 -0600535 spi_dev_put(proxy);
536 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800537 }
538
David Brownell8ae12a02006-01-08 13:34:19 -0800539 return proxy;
540}
541EXPORT_SYMBOL_GPL(spi_new_device);
542
Feng Tang2b9603a2010-08-02 15:52:15 +0800543static void spi_match_master_to_boardinfo(struct spi_master *master,
544 struct spi_board_info *bi)
545{
546 struct spi_device *dev;
547
548 if (master->bus_num != bi->bus_num)
549 return;
550
551 dev = spi_new_device(master, bi);
552 if (!dev)
553 dev_err(master->dev.parent, "can't create new device for %s\n",
554 bi->modalias);
555}
556
David Brownell33e34dc2007-05-08 00:32:21 -0700557/**
558 * spi_register_board_info - register SPI devices for a given board
559 * @info: array of chip descriptors
560 * @n: how many descriptors are provided
561 * Context: can sleep
562 *
David Brownell8ae12a02006-01-08 13:34:19 -0800563 * Board-specific early init code calls this (probably during arch_initcall)
564 * with segments of the SPI device table. Any device nodes are created later,
565 * after the relevant parent SPI controller (bus_num) is defined. We keep
566 * this table of devices forever, so that reloading a controller driver will
567 * not make Linux forget about these hard-wired devices.
568 *
569 * Other code can also call this, e.g. a particular add-on board might provide
570 * SPI devices through its expansion connector, so code initializing that board
571 * would naturally declare its SPI devices.
572 *
573 * The board info passed can safely be __initdata ... but be careful of
574 * any embedded pointers (platform_data, etc), they're copied as-is.
575 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000576int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800577{
Feng Tang2b9603a2010-08-02 15:52:15 +0800578 struct boardinfo *bi;
579 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800580
Xiubo Lic7908a32014-09-24 14:30:29 +0800581 if (!n)
582 return -EINVAL;
583
Feng Tang2b9603a2010-08-02 15:52:15 +0800584 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800585 if (!bi)
586 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800587
Feng Tang2b9603a2010-08-02 15:52:15 +0800588 for (i = 0; i < n; i++, bi++, info++) {
589 struct spi_master *master;
David Brownell8ae12a02006-01-08 13:34:19 -0800590
Feng Tang2b9603a2010-08-02 15:52:15 +0800591 memcpy(&bi->board_info, info, sizeof(*info));
592 mutex_lock(&board_lock);
593 list_add_tail(&bi->list, &board_list);
594 list_for_each_entry(master, &spi_master_list, list)
595 spi_match_master_to_boardinfo(master, &bi->board_info);
596 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800597 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800598
599 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800600}
601
602/*-------------------------------------------------------------------------*/
603
Mark Brownb1589352013-10-05 11:50:40 +0100604static void spi_set_cs(struct spi_device *spi, bool enable)
605{
606 if (spi->mode & SPI_CS_HIGH)
607 enable = !enable;
608
Andy Shevchenko243f07b2015-10-20 12:28:29 +0300609 if (gpio_is_valid(spi->cs_gpio))
Mark Brownb1589352013-10-05 11:50:40 +0100610 gpio_set_value(spi->cs_gpio, !enable);
611 else if (spi->master->set_cs)
612 spi->master->set_cs(spi, !enable);
613}
614
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200615#ifdef CONFIG_HAS_DMA
Mark Brown6ad45a22014-02-02 13:47:47 +0000616static int spi_map_buf(struct spi_master *master, struct device *dev,
617 struct sg_table *sgt, void *buf, size_t len,
618 enum dma_data_direction dir)
619{
620 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500621 int desc_len;
622 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000623 struct page *vm_page;
624 void *sg_buf;
625 size_t min;
626 int i, ret;
627
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500628 if (vmalloced_buf) {
629 desc_len = PAGE_SIZE;
630 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
631 } else {
632 desc_len = master->max_dma_len;
633 sgs = DIV_ROUND_UP(len, desc_len);
634 }
635
Mark Brown6ad45a22014-02-02 13:47:47 +0000636 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
637 if (ret != 0)
638 return ret;
639
640 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000641
642 if (vmalloced_buf) {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500643 min = min_t(size_t,
644 len, desc_len - offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000645 vm_page = vmalloc_to_page(buf);
646 if (!vm_page) {
647 sg_free_table(sgt);
648 return -ENOMEM;
649 }
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000650 sg_set_page(&sgt->sgl[i], vm_page,
651 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000652 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500653 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000654 sg_buf = buf;
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000655 sg_set_buf(&sgt->sgl[i], sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000656 }
657
Mark Brown6ad45a22014-02-02 13:47:47 +0000658
659 buf += min;
660 len -= min;
661 }
662
663 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200664 if (!ret)
665 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000666 if (ret < 0) {
667 sg_free_table(sgt);
668 return ret;
669 }
670
671 sgt->nents = ret;
672
673 return 0;
674}
675
676static void spi_unmap_buf(struct spi_master *master, struct device *dev,
677 struct sg_table *sgt, enum dma_data_direction dir)
678{
679 if (sgt->orig_nents) {
680 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
681 sg_free_table(sgt);
682 }
683}
684
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200685static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000686{
Mark Brown99adef32014-01-16 12:22:43 +0000687 struct device *tx_dev, *rx_dev;
688 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000689 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000690
Mark Brown6ad45a22014-02-02 13:47:47 +0000691 if (!master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000692 return 0;
693
Leilk Liuc37f45b2015-07-23 17:10:40 +0800694 if (master->dma_tx)
695 tx_dev = master->dma_tx->device->dev;
696 else
697 tx_dev = &master->dev;
698
699 if (master->dma_rx)
700 rx_dev = master->dma_rx->device->dev;
701 else
702 rx_dev = &master->dev;
Mark Brown99adef32014-01-16 12:22:43 +0000703
704 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
705 if (!master->can_dma(master, msg->spi, xfer))
706 continue;
707
708 if (xfer->tx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000709 ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
710 (void *)xfer->tx_buf, xfer->len,
711 DMA_TO_DEVICE);
712 if (ret != 0)
713 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000714 }
715
716 if (xfer->rx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000717 ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
718 xfer->rx_buf, xfer->len,
719 DMA_FROM_DEVICE);
720 if (ret != 0) {
721 spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
722 DMA_TO_DEVICE);
723 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000724 }
725 }
726 }
727
728 master->cur_msg_mapped = true;
729
730 return 0;
731}
732
Martin Sperl4b786452015-05-25 10:13:10 +0000733static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000734{
735 struct spi_transfer *xfer;
736 struct device *tx_dev, *rx_dev;
737
Mark Brown6ad45a22014-02-02 13:47:47 +0000738 if (!master->cur_msg_mapped || !master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000739 return 0;
740
Leilk Liuc37f45b2015-07-23 17:10:40 +0800741 if (master->dma_tx)
742 tx_dev = master->dma_tx->device->dev;
743 else
744 tx_dev = &master->dev;
745
746 if (master->dma_rx)
747 rx_dev = master->dma_rx->device->dev;
748 else
749 rx_dev = &master->dev;
Mark Brown99adef32014-01-16 12:22:43 +0000750
751 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
752 if (!master->can_dma(master, msg->spi, xfer))
753 continue;
754
Mark Brown6ad45a22014-02-02 13:47:47 +0000755 spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
756 spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000757 }
758
759 return 0;
760}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200761#else /* !CONFIG_HAS_DMA */
762static inline int __spi_map_msg(struct spi_master *master,
763 struct spi_message *msg)
764{
765 return 0;
766}
767
Martin Sperl4b786452015-05-25 10:13:10 +0000768static inline int __spi_unmap_msg(struct spi_master *master,
769 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200770{
771 return 0;
772}
773#endif /* !CONFIG_HAS_DMA */
774
Martin Sperl4b786452015-05-25 10:13:10 +0000775static inline int spi_unmap_msg(struct spi_master *master,
776 struct spi_message *msg)
777{
778 struct spi_transfer *xfer;
779
780 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
781 /*
782 * Restore the original value of tx_buf or rx_buf if they are
783 * NULL.
784 */
785 if (xfer->tx_buf == master->dummy_tx)
786 xfer->tx_buf = NULL;
787 if (xfer->rx_buf == master->dummy_rx)
788 xfer->rx_buf = NULL;
789 }
790
791 return __spi_unmap_msg(master, msg);
792}
793
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200794static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
795{
796 struct spi_transfer *xfer;
797 void *tmp;
798 unsigned int max_tx, max_rx;
799
800 if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
801 max_tx = 0;
802 max_rx = 0;
803
804 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
805 if ((master->flags & SPI_MASTER_MUST_TX) &&
806 !xfer->tx_buf)
807 max_tx = max(xfer->len, max_tx);
808 if ((master->flags & SPI_MASTER_MUST_RX) &&
809 !xfer->rx_buf)
810 max_rx = max(xfer->len, max_rx);
811 }
812
813 if (max_tx) {
814 tmp = krealloc(master->dummy_tx, max_tx,
815 GFP_KERNEL | GFP_DMA);
816 if (!tmp)
817 return -ENOMEM;
818 master->dummy_tx = tmp;
819 memset(tmp, 0, max_tx);
820 }
821
822 if (max_rx) {
823 tmp = krealloc(master->dummy_rx, max_rx,
824 GFP_KERNEL | GFP_DMA);
825 if (!tmp)
826 return -ENOMEM;
827 master->dummy_rx = tmp;
828 }
829
830 if (max_tx || max_rx) {
831 list_for_each_entry(xfer, &msg->transfers,
832 transfer_list) {
833 if (!xfer->tx_buf)
834 xfer->tx_buf = master->dummy_tx;
835 if (!xfer->rx_buf)
836 xfer->rx_buf = master->dummy_rx;
837 }
838 }
839 }
840
841 return __spi_map_msg(master, msg);
842}
Mark Brown99adef32014-01-16 12:22:43 +0000843
Mark Brownb1589352013-10-05 11:50:40 +0100844/*
845 * spi_transfer_one_message - Default implementation of transfer_one_message()
846 *
847 * This is a standard implementation of transfer_one_message() for
848 * drivers which impelment a transfer_one() operation. It provides
849 * standard handling of delays and chip select management.
850 */
851static int spi_transfer_one_message(struct spi_master *master,
852 struct spi_message *msg)
853{
854 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +0100855 bool keep_cs = false;
856 int ret = 0;
Nicholas Mc Guire682a71b2015-02-02 03:30:32 -0500857 unsigned long ms = 1;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000858 struct spi_statistics *statm = &master->statistics;
859 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +0100860
861 spi_set_cs(msg->spi, true);
862
Martin Sperleca2ebc2015-06-22 13:00:36 +0000863 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
864 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
865
Mark Brownb1589352013-10-05 11:50:40 +0100866 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
867 trace_spi_transfer_start(msg, xfer);
868
Martin Sperleca2ebc2015-06-22 13:00:36 +0000869 spi_statistics_add_transfer_stats(statm, xfer, master);
870 spi_statistics_add_transfer_stats(stats, xfer, master);
871
Mark Brown38ec10f2014-08-16 16:27:41 +0100872 if (xfer->tx_buf || xfer->rx_buf) {
873 reinit_completion(&master->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +0100874
Mark Brown38ec10f2014-08-16 16:27:41 +0100875 ret = master->transfer_one(master, msg->spi, xfer);
876 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000877 SPI_STATISTICS_INCREMENT_FIELD(statm,
878 errors);
879 SPI_STATISTICS_INCREMENT_FIELD(stats,
880 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +0100881 dev_err(&msg->spi->dev,
882 "SPI transfer failed: %d\n", ret);
883 goto out;
884 }
Mark Brownb1589352013-10-05 11:50:40 +0100885
Mark Brown38ec10f2014-08-16 16:27:41 +0100886 if (ret > 0) {
887 ret = 0;
888 ms = xfer->len * 8 * 1000 / xfer->speed_hz;
889 ms += ms + 100; /* some tolerance */
Mark Brown16a0ce42014-01-30 22:16:41 +0000890
Mark Brown38ec10f2014-08-16 16:27:41 +0100891 ms = wait_for_completion_timeout(&master->xfer_completion,
892 msecs_to_jiffies(ms));
893 }
Mark Brown16a0ce42014-01-30 22:16:41 +0000894
Mark Brown38ec10f2014-08-16 16:27:41 +0100895 if (ms == 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000896 SPI_STATISTICS_INCREMENT_FIELD(statm,
897 timedout);
898 SPI_STATISTICS_INCREMENT_FIELD(stats,
899 timedout);
Mark Brown38ec10f2014-08-16 16:27:41 +0100900 dev_err(&msg->spi->dev,
901 "SPI transfer timed out\n");
902 msg->status = -ETIMEDOUT;
903 }
904 } else {
905 if (xfer->len)
906 dev_err(&msg->spi->dev,
907 "Bufferless transfer has length %u\n",
908 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +0800909 }
Mark Brownb1589352013-10-05 11:50:40 +0100910
911 trace_spi_transfer_stop(msg, xfer);
912
913 if (msg->status != -EINPROGRESS)
914 goto out;
915
916 if (xfer->delay_usecs)
917 udelay(xfer->delay_usecs);
918
919 if (xfer->cs_change) {
920 if (list_is_last(&xfer->transfer_list,
921 &msg->transfers)) {
922 keep_cs = true;
923 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +0000924 spi_set_cs(msg->spi, false);
925 udelay(10);
926 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +0100927 }
928 }
929
930 msg->actual_length += xfer->len;
931 }
932
933out:
934 if (ret != 0 || !keep_cs)
935 spi_set_cs(msg->spi, false);
936
937 if (msg->status == -EINPROGRESS)
938 msg->status = ret;
939
Geert Uytterhoevenff61eb42015-04-07 20:39:19 +0200940 if (msg->status && master->handle_err)
Andy Shevchenkob716c4f2015-02-27 17:34:15 +0200941 master->handle_err(master, msg);
942
Mark Brownb1589352013-10-05 11:50:40 +0100943 spi_finalize_current_message(master);
944
945 return ret;
946}
947
948/**
949 * spi_finalize_current_transfer - report completion of a transfer
Thierry Reding2c675682014-08-08 13:02:36 +0200950 * @master: the master reporting completion
Mark Brownb1589352013-10-05 11:50:40 +0100951 *
952 * Called by SPI drivers using the core transfer_one_message()
953 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +0100954 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +0100955 */
956void spi_finalize_current_transfer(struct spi_master *master)
957{
958 complete(&master->xfer_completion);
959}
960EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
961
Linus Walleijffbbdd212012-02-22 10:05:38 +0100962/**
Mark Brownfc9e0f72014-12-10 13:46:33 +0000963 * __spi_pump_messages - function which processes spi message queue
964 * @master: master to process queue for
965 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +0100966 *
967 * This function checks if there is any spi message in the queue that
968 * needs processing and if so call out to the driver to initialize hardware
969 * and transfer each message.
970 *
Mark Brown0461a412014-12-09 21:38:05 +0000971 * Note that it is called both from the kthread itself and also from
972 * inside spi_sync(); the queue extraction handling at the top of the
973 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +0100974 */
Mark Brownfc9e0f72014-12-10 13:46:33 +0000975static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +0100976{
Linus Walleijffbbdd212012-02-22 10:05:38 +0100977 unsigned long flags;
978 bool was_busy = false;
979 int ret;
980
Mark Brown983aee52014-12-09 19:46:56 +0000981 /* Lock queue */
Linus Walleijffbbdd212012-02-22 10:05:38 +0100982 spin_lock_irqsave(&master->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +0000983
984 /* Make sure we are not already running a message */
985 if (master->cur_msg) {
986 spin_unlock_irqrestore(&master->queue_lock, flags);
987 return;
988 }
989
Mark Brown0461a412014-12-09 21:38:05 +0000990 /* If another context is idling the device then defer */
991 if (master->idling) {
992 queue_kthread_work(&master->kworker, &master->pump_messages);
993 spin_unlock_irqrestore(&master->queue_lock, flags);
994 return;
995 }
996
Mark Brown983aee52014-12-09 19:46:56 +0000997 /* Check if the queue is idle */
Linus Walleijffbbdd212012-02-22 10:05:38 +0100998 if (list_empty(&master->queue) || !master->running) {
Bryan Freedb0b36b82013-03-13 11:17:40 -0700999 if (!master->busy) {
1000 spin_unlock_irqrestore(&master->queue_lock, flags);
1001 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001002 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001003
1004 /* Only do teardown in the thread */
1005 if (!in_kthread) {
1006 queue_kthread_work(&master->kworker,
1007 &master->pump_messages);
1008 spin_unlock_irqrestore(&master->queue_lock, flags);
1009 return;
1010 }
1011
Linus Walleijffbbdd212012-02-22 10:05:38 +01001012 master->busy = false;
Mark Brown0461a412014-12-09 21:38:05 +00001013 master->idling = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001014 spin_unlock_irqrestore(&master->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001015
Mark Brown3a2eba92014-01-28 20:17:03 +00001016 kfree(master->dummy_rx);
1017 master->dummy_rx = NULL;
1018 kfree(master->dummy_tx);
1019 master->dummy_tx = NULL;
Bryan Freedb0b36b82013-03-13 11:17:40 -07001020 if (master->unprepare_transfer_hardware &&
1021 master->unprepare_transfer_hardware(master))
1022 dev_err(&master->dev,
1023 "failed to unprepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001024 if (master->auto_runtime_pm) {
1025 pm_runtime_mark_last_busy(master->dev.parent);
1026 pm_runtime_put_autosuspend(master->dev.parent);
1027 }
Mark Brown56ec1972013-10-07 19:33:53 +01001028 trace_spi_master_idle(master);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001029
Mark Brown0461a412014-12-09 21:38:05 +00001030 spin_lock_irqsave(&master->queue_lock, flags);
1031 master->idling = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001032 spin_unlock_irqrestore(&master->queue_lock, flags);
1033 return;
1034 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001035
Linus Walleijffbbdd212012-02-22 10:05:38 +01001036 /* Extract head of queue */
1037 master->cur_msg =
Axel Lina89e2d22014-01-09 16:03:58 +08001038 list_first_entry(&master->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001039
1040 list_del_init(&master->cur_msg->queue);
1041 if (master->busy)
1042 was_busy = true;
1043 else
1044 master->busy = true;
1045 spin_unlock_irqrestore(&master->queue_lock, flags);
1046
Mark Brown49834de2013-07-28 14:47:02 +01001047 if (!was_busy && master->auto_runtime_pm) {
1048 ret = pm_runtime_get_sync(master->dev.parent);
1049 if (ret < 0) {
1050 dev_err(&master->dev, "Failed to power device: %d\n",
1051 ret);
1052 return;
1053 }
1054 }
1055
Mark Brown56ec1972013-10-07 19:33:53 +01001056 if (!was_busy)
1057 trace_spi_master_busy(master);
1058
Shubhrajyoti D7dfd2bd2012-05-10 19:20:41 +05301059 if (!was_busy && master->prepare_transfer_hardware) {
Linus Walleijffbbdd212012-02-22 10:05:38 +01001060 ret = master->prepare_transfer_hardware(master);
1061 if (ret) {
1062 dev_err(&master->dev,
1063 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001064
1065 if (master->auto_runtime_pm)
1066 pm_runtime_put(master->dev.parent);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001067 return;
1068 }
1069 }
1070
Mark Brown56ec1972013-10-07 19:33:53 +01001071 trace_spi_message_start(master->cur_msg);
1072
Mark Brown2841a5f2013-10-05 00:23:12 +01001073 if (master->prepare_message) {
1074 ret = master->prepare_message(master, master->cur_msg);
1075 if (ret) {
1076 dev_err(&master->dev,
1077 "failed to prepare message: %d\n", ret);
1078 master->cur_msg->status = ret;
1079 spi_finalize_current_message(master);
1080 return;
1081 }
1082 master->cur_msg_prepared = true;
1083 }
1084
Mark Brown99adef32014-01-16 12:22:43 +00001085 ret = spi_map_msg(master, master->cur_msg);
1086 if (ret) {
1087 master->cur_msg->status = ret;
1088 spi_finalize_current_message(master);
1089 return;
1090 }
1091
Linus Walleijffbbdd212012-02-22 10:05:38 +01001092 ret = master->transfer_one_message(master, master->cur_msg);
1093 if (ret) {
1094 dev_err(&master->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001095 "failed to transfer one message from queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001096 return;
1097 }
1098}
1099
Mark Brownfc9e0f72014-12-10 13:46:33 +00001100/**
1101 * spi_pump_messages - kthread work function which processes spi message queue
1102 * @work: pointer to kthread work struct contained in the master struct
1103 */
1104static void spi_pump_messages(struct kthread_work *work)
1105{
1106 struct spi_master *master =
1107 container_of(work, struct spi_master, pump_messages);
1108
1109 __spi_pump_messages(master, true);
1110}
1111
Linus Walleijffbbdd212012-02-22 10:05:38 +01001112static int spi_init_queue(struct spi_master *master)
1113{
1114 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1115
Linus Walleijffbbdd212012-02-22 10:05:38 +01001116 master->running = false;
1117 master->busy = false;
1118
1119 init_kthread_worker(&master->kworker);
1120 master->kworker_task = kthread_run(kthread_worker_fn,
Kees Cookf1701682013-07-03 15:04:58 -07001121 &master->kworker, "%s",
Linus Walleijffbbdd212012-02-22 10:05:38 +01001122 dev_name(&master->dev));
1123 if (IS_ERR(master->kworker_task)) {
1124 dev_err(&master->dev, "failed to create message pump task\n");
Jarkko Nikula98a8f5a2014-12-04 11:02:25 +02001125 return PTR_ERR(master->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001126 }
1127 init_kthread_work(&master->pump_messages, spi_pump_messages);
1128
1129 /*
1130 * Master config will indicate if this controller should run the
1131 * message pump with high (realtime) priority to reduce the transfer
1132 * latency on the bus by minimising the delay between a transfer
1133 * request and the scheduling of the message pump thread. Without this
1134 * setting the message pump thread will remain at default priority.
1135 */
1136 if (master->rt) {
1137 dev_info(&master->dev,
1138 "will run message pump with realtime priority\n");
1139 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
1140 }
1141
1142 return 0;
1143}
1144
1145/**
1146 * spi_get_next_queued_message() - called by driver to check for queued
1147 * messages
1148 * @master: the master to check for queued messages
1149 *
1150 * If there are more messages in the queue, the next message is returned from
1151 * this call.
1152 */
1153struct spi_message *spi_get_next_queued_message(struct spi_master *master)
1154{
1155 struct spi_message *next;
1156 unsigned long flags;
1157
1158 /* get a pointer to the next message, if any */
1159 spin_lock_irqsave(&master->queue_lock, flags);
Axel Lin1cfd97f2014-01-02 15:16:40 +08001160 next = list_first_entry_or_null(&master->queue, struct spi_message,
1161 queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001162 spin_unlock_irqrestore(&master->queue_lock, flags);
1163
1164 return next;
1165}
1166EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1167
1168/**
1169 * spi_finalize_current_message() - the current message is complete
1170 * @master: the master to return the message to
1171 *
1172 * Called by the driver to notify the core that the message in the front of the
1173 * queue is complete and can be removed from the queue.
1174 */
1175void spi_finalize_current_message(struct spi_master *master)
1176{
1177 struct spi_message *mesg;
1178 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001179 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001180
1181 spin_lock_irqsave(&master->queue_lock, flags);
1182 mesg = master->cur_msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001183 spin_unlock_irqrestore(&master->queue_lock, flags);
1184
Mark Brown99adef32014-01-16 12:22:43 +00001185 spi_unmap_msg(master, mesg);
1186
Mark Brown2841a5f2013-10-05 00:23:12 +01001187 if (master->cur_msg_prepared && master->unprepare_message) {
1188 ret = master->unprepare_message(master, mesg);
1189 if (ret) {
1190 dev_err(&master->dev,
1191 "failed to unprepare message: %d\n", ret);
1192 }
1193 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001194
Martin Sperl8e76ef82015-05-10 07:50:45 +00001195 spin_lock_irqsave(&master->queue_lock, flags);
1196 master->cur_msg = NULL;
Mark Brown2841a5f2013-10-05 00:23:12 +01001197 master->cur_msg_prepared = false;
Martin Sperl8e76ef82015-05-10 07:50:45 +00001198 queue_kthread_work(&master->kworker, &master->pump_messages);
1199 spin_unlock_irqrestore(&master->queue_lock, flags);
1200
1201 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001202
Linus Walleijffbbdd212012-02-22 10:05:38 +01001203 mesg->state = NULL;
1204 if (mesg->complete)
1205 mesg->complete(mesg->context);
1206}
1207EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1208
1209static int spi_start_queue(struct spi_master *master)
1210{
1211 unsigned long flags;
1212
1213 spin_lock_irqsave(&master->queue_lock, flags);
1214
1215 if (master->running || master->busy) {
1216 spin_unlock_irqrestore(&master->queue_lock, flags);
1217 return -EBUSY;
1218 }
1219
1220 master->running = true;
1221 master->cur_msg = NULL;
1222 spin_unlock_irqrestore(&master->queue_lock, flags);
1223
1224 queue_kthread_work(&master->kworker, &master->pump_messages);
1225
1226 return 0;
1227}
1228
1229static int spi_stop_queue(struct spi_master *master)
1230{
1231 unsigned long flags;
1232 unsigned limit = 500;
1233 int ret = 0;
1234
1235 spin_lock_irqsave(&master->queue_lock, flags);
1236
1237 /*
1238 * This is a bit lame, but is optimized for the common execution path.
1239 * A wait_queue on the master->busy could be used, but then the common
1240 * execution path (pump_messages) would be required to call wake_up or
1241 * friends on every SPI message. Do this instead.
1242 */
1243 while ((!list_empty(&master->queue) || master->busy) && limit--) {
1244 spin_unlock_irqrestore(&master->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001245 usleep_range(10000, 11000);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001246 spin_lock_irqsave(&master->queue_lock, flags);
1247 }
1248
1249 if (!list_empty(&master->queue) || master->busy)
1250 ret = -EBUSY;
1251 else
1252 master->running = false;
1253
1254 spin_unlock_irqrestore(&master->queue_lock, flags);
1255
1256 if (ret) {
1257 dev_warn(&master->dev,
1258 "could not stop message queue\n");
1259 return ret;
1260 }
1261 return ret;
1262}
1263
1264static int spi_destroy_queue(struct spi_master *master)
1265{
1266 int ret;
1267
1268 ret = spi_stop_queue(master);
1269
1270 /*
1271 * flush_kthread_worker will block until all work is done.
1272 * If the reason that stop_queue timed out is that the work will never
1273 * finish, then it does no good to call flush/stop thread, so
1274 * return anyway.
1275 */
1276 if (ret) {
1277 dev_err(&master->dev, "problem destroying queue\n");
1278 return ret;
1279 }
1280
1281 flush_kthread_worker(&master->kworker);
1282 kthread_stop(master->kworker_task);
1283
1284 return 0;
1285}
1286
Mark Brown0461a412014-12-09 21:38:05 +00001287static int __spi_queued_transfer(struct spi_device *spi,
1288 struct spi_message *msg,
1289 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001290{
1291 struct spi_master *master = spi->master;
1292 unsigned long flags;
1293
1294 spin_lock_irqsave(&master->queue_lock, flags);
1295
1296 if (!master->running) {
1297 spin_unlock_irqrestore(&master->queue_lock, flags);
1298 return -ESHUTDOWN;
1299 }
1300 msg->actual_length = 0;
1301 msg->status = -EINPROGRESS;
1302
1303 list_add_tail(&msg->queue, &master->queue);
Mark Brown0461a412014-12-09 21:38:05 +00001304 if (!master->busy && need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001305 queue_kthread_work(&master->kworker, &master->pump_messages);
1306
1307 spin_unlock_irqrestore(&master->queue_lock, flags);
1308 return 0;
1309}
1310
Mark Brown0461a412014-12-09 21:38:05 +00001311/**
1312 * spi_queued_transfer - transfer function for queued transfers
1313 * @spi: spi device which is requesting transfer
1314 * @msg: spi message which is to handled is queued to driver queue
1315 */
1316static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1317{
1318 return __spi_queued_transfer(spi, msg, true);
1319}
1320
Linus Walleijffbbdd212012-02-22 10:05:38 +01001321static int spi_master_initialize_queue(struct spi_master *master)
1322{
1323 int ret;
1324
Linus Walleijffbbdd212012-02-22 10:05:38 +01001325 master->transfer = spi_queued_transfer;
Mark Brownb1589352013-10-05 11:50:40 +01001326 if (!master->transfer_one_message)
1327 master->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001328
1329 /* Initialize and start queue */
1330 ret = spi_init_queue(master);
1331 if (ret) {
1332 dev_err(&master->dev, "problem initializing queue\n");
1333 goto err_init_queue;
1334 }
Mark Brownc3676d52014-05-01 10:47:52 -07001335 master->queued = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001336 ret = spi_start_queue(master);
1337 if (ret) {
1338 dev_err(&master->dev, "problem starting queue\n");
1339 goto err_start_queue;
1340 }
1341
1342 return 0;
1343
1344err_start_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001345 spi_destroy_queue(master);
Mark Brownc3676d52014-05-01 10:47:52 -07001346err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001347 return ret;
1348}
1349
1350/*-------------------------------------------------------------------------*/
1351
Andreas Larsson7cb94362012-12-04 15:09:38 +01001352#if defined(CONFIG_OF)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001353static struct spi_device *
1354of_register_spi_device(struct spi_master *master, struct device_node *nc)
1355{
1356 struct spi_device *spi;
1357 int rc;
1358 u32 value;
1359
1360 /* Alloc an spi_device */
1361 spi = spi_alloc_device(master);
1362 if (!spi) {
1363 dev_err(&master->dev, "spi_device alloc error for %s\n",
1364 nc->full_name);
1365 rc = -ENOMEM;
1366 goto err_out;
1367 }
1368
1369 /* Select device driver */
1370 rc = of_modalias_node(nc, spi->modalias,
1371 sizeof(spi->modalias));
1372 if (rc < 0) {
1373 dev_err(&master->dev, "cannot find modalias for %s\n",
1374 nc->full_name);
1375 goto err_out;
1376 }
1377
1378 /* Device address */
1379 rc = of_property_read_u32(nc, "reg", &value);
1380 if (rc) {
1381 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1382 nc->full_name, rc);
1383 goto err_out;
1384 }
1385 spi->chip_select = value;
1386
1387 /* Mode (clock phase/polarity/etc.) */
1388 if (of_find_property(nc, "spi-cpha", NULL))
1389 spi->mode |= SPI_CPHA;
1390 if (of_find_property(nc, "spi-cpol", NULL))
1391 spi->mode |= SPI_CPOL;
1392 if (of_find_property(nc, "spi-cs-high", NULL))
1393 spi->mode |= SPI_CS_HIGH;
1394 if (of_find_property(nc, "spi-3wire", NULL))
1395 spi->mode |= SPI_3WIRE;
1396 if (of_find_property(nc, "spi-lsb-first", NULL))
1397 spi->mode |= SPI_LSB_FIRST;
1398
1399 /* Device DUAL/QUAD mode */
1400 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1401 switch (value) {
1402 case 1:
1403 break;
1404 case 2:
1405 spi->mode |= SPI_TX_DUAL;
1406 break;
1407 case 4:
1408 spi->mode |= SPI_TX_QUAD;
1409 break;
1410 default:
1411 dev_warn(&master->dev,
1412 "spi-tx-bus-width %d not supported\n",
1413 value);
1414 break;
1415 }
1416 }
1417
1418 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1419 switch (value) {
1420 case 1:
1421 break;
1422 case 2:
1423 spi->mode |= SPI_RX_DUAL;
1424 break;
1425 case 4:
1426 spi->mode |= SPI_RX_QUAD;
1427 break;
1428 default:
1429 dev_warn(&master->dev,
1430 "spi-rx-bus-width %d not supported\n",
1431 value);
1432 break;
1433 }
1434 }
1435
1436 /* Device speed */
1437 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1438 if (rc) {
1439 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1440 nc->full_name, rc);
1441 goto err_out;
1442 }
1443 spi->max_speed_hz = value;
1444
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001445 /* Store a pointer to the node in the device structure */
1446 of_node_get(nc);
1447 spi->dev.of_node = nc;
1448
1449 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001450 rc = spi_add_device(spi);
1451 if (rc) {
1452 dev_err(&master->dev, "spi_device register error %s\n",
1453 nc->full_name);
1454 goto err_out;
1455 }
1456
1457 return spi;
1458
1459err_out:
1460 spi_dev_put(spi);
1461 return ERR_PTR(rc);
1462}
1463
Grant Likelyd57a4282012-04-07 14:16:53 -06001464/**
1465 * of_register_spi_devices() - Register child devices onto the SPI bus
1466 * @master: Pointer to spi_master device
1467 *
1468 * Registers an spi_device for each child node of master node which has a 'reg'
1469 * property.
1470 */
1471static void of_register_spi_devices(struct spi_master *master)
1472{
1473 struct spi_device *spi;
1474 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001475
1476 if (!master->dev.of_node)
1477 return;
1478
Alexander Sverdlinf3b61592012-11-29 08:59:29 +01001479 for_each_available_child_of_node(master->dev.of_node, nc) {
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001480 spi = of_register_spi_device(master, nc);
1481 if (IS_ERR(spi))
1482 dev_warn(&master->dev, "Failed to create SPI device for %s\n",
Grant Likelyd57a4282012-04-07 14:16:53 -06001483 nc->full_name);
Grant Likelyd57a4282012-04-07 14:16:53 -06001484 }
1485}
1486#else
1487static void of_register_spi_devices(struct spi_master *master) { }
1488#endif
1489
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001490#ifdef CONFIG_ACPI
1491static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1492{
1493 struct spi_device *spi = data;
1494
1495 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1496 struct acpi_resource_spi_serialbus *sb;
1497
1498 sb = &ares->data.spi_serial_bus;
1499 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1500 spi->chip_select = sb->device_selection;
1501 spi->max_speed_hz = sb->connection_speed;
1502
1503 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1504 spi->mode |= SPI_CPHA;
1505 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1506 spi->mode |= SPI_CPOL;
1507 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1508 spi->mode |= SPI_CS_HIGH;
1509 }
1510 } else if (spi->irq < 0) {
1511 struct resource r;
1512
1513 if (acpi_dev_resource_interrupt(ares, 0, &r))
1514 spi->irq = r.start;
1515 }
1516
1517 /* Always tell the ACPI core to skip this resource */
1518 return 1;
1519}
1520
1521static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1522 void *data, void **return_value)
1523{
1524 struct spi_master *master = data;
1525 struct list_head resource_list;
1526 struct acpi_device *adev;
1527 struct spi_device *spi;
1528 int ret;
1529
1530 if (acpi_bus_get_device(handle, &adev))
1531 return AE_OK;
1532 if (acpi_bus_get_status(adev) || !adev->status.present)
1533 return AE_OK;
1534
1535 spi = spi_alloc_device(master);
1536 if (!spi) {
1537 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1538 dev_name(&adev->dev));
1539 return AE_NO_MEMORY;
1540 }
1541
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001542 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001543 spi->irq = -1;
1544
1545 INIT_LIST_HEAD(&resource_list);
1546 ret = acpi_dev_get_resources(adev, &resource_list,
1547 acpi_spi_add_resource, spi);
1548 acpi_dev_free_resource_list(&resource_list);
1549
1550 if (ret < 0 || !spi->max_speed_hz) {
1551 spi_dev_put(spi);
1552 return AE_OK;
1553 }
1554
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001555 adev->power.flags.ignore_parent = true;
Jarkko Nikulacf9eb392013-10-10 17:19:17 +03001556 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001557 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001558 adev->power.flags.ignore_parent = false;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001559 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1560 dev_name(&adev->dev));
1561 spi_dev_put(spi);
1562 }
1563
1564 return AE_OK;
1565}
1566
1567static void acpi_register_spi_devices(struct spi_master *master)
1568{
1569 acpi_status status;
1570 acpi_handle handle;
1571
Rafael J. Wysocki29896172013-04-01 00:21:08 +00001572 handle = ACPI_HANDLE(master->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001573 if (!handle)
1574 return;
1575
1576 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1577 acpi_spi_add_device, NULL,
1578 master, NULL);
1579 if (ACPI_FAILURE(status))
1580 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1581}
1582#else
1583static inline void acpi_register_spi_devices(struct spi_master *master) {}
1584#endif /* CONFIG_ACPI */
1585
Tony Jones49dce682007-10-16 01:27:48 -07001586static void spi_master_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001587{
1588 struct spi_master *master;
1589
Tony Jones49dce682007-10-16 01:27:48 -07001590 master = container_of(dev, struct spi_master, dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001591 kfree(master);
1592}
1593
1594static struct class spi_master_class = {
1595 .name = "spi_master",
1596 .owner = THIS_MODULE,
Tony Jones49dce682007-10-16 01:27:48 -07001597 .dev_release = spi_master_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00001598 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08001599};
1600
1601
1602/**
1603 * spi_alloc_master - allocate SPI master controller
1604 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001605 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001606 * memory is in the driver_data field of the returned device,
David Brownell0c8684612006-01-08 13:34:25 -08001607 * accessible with spi_master_get_devdata().
David Brownell33e34dc2007-05-08 00:32:21 -07001608 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001609 *
1610 * This call is used only by SPI master controller drivers, which are the
1611 * only ones directly touching chip registers. It's how they allocate
dmitry pervushinba1a0512006-05-20 15:00:14 -07001612 * an spi_master structure, prior to calling spi_register_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001613 *
1614 * This must be called from context that can sleep. It returns the SPI
1615 * master structure on success, else NULL.
1616 *
1617 * The caller is responsible for assigning the bus number and initializing
dmitry pervushinba1a0512006-05-20 15:00:14 -07001618 * the master's methods before calling spi_register_master(); and (after errors
Uwe Kleine-Königeb4af0f2012-02-23 10:40:14 +01001619 * adding the device) calling spi_master_put() and kfree() to prevent a memory
1620 * leak.
David Brownell8ae12a02006-01-08 13:34:19 -08001621 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001622struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
David Brownell8ae12a02006-01-08 13:34:19 -08001623{
1624 struct spi_master *master;
1625
David Brownell0c8684612006-01-08 13:34:25 -08001626 if (!dev)
1627 return NULL;
1628
Jingoo Han5fe5f052013-10-14 10:31:51 +09001629 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -08001630 if (!master)
1631 return NULL;
1632
Tony Jones49dce682007-10-16 01:27:48 -07001633 device_initialize(&master->dev);
Grant Likely1e8a52e2012-05-19 23:42:08 -06001634 master->bus_num = -1;
1635 master->num_chipselect = 1;
Tony Jones49dce682007-10-16 01:27:48 -07001636 master->dev.class = &spi_master_class;
1637 master->dev.parent = get_device(dev);
David Brownell0c8684612006-01-08 13:34:25 -08001638 spi_master_set_devdata(master, &master[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001639
1640 return master;
1641}
1642EXPORT_SYMBOL_GPL(spi_alloc_master);
1643
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001644#ifdef CONFIG_OF
1645static int of_spi_register_master(struct spi_master *master)
1646{
Grant Likelye80beb22013-02-12 17:48:37 +00001647 int nb, i, *cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001648 struct device_node *np = master->dev.of_node;
1649
1650 if (!np)
1651 return 0;
1652
1653 nb = of_gpio_named_count(np, "cs-gpios");
Jingoo Han5fe5f052013-10-14 10:31:51 +09001654 master->num_chipselect = max_t(int, nb, master->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001655
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001656 /* Return error only for an incorrectly formed cs-gpios property */
1657 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001658 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001659 else if (nb < 0)
1660 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001661
1662 cs = devm_kzalloc(&master->dev,
1663 sizeof(int) * master->num_chipselect,
1664 GFP_KERNEL);
1665 master->cs_gpios = cs;
1666
1667 if (!master->cs_gpios)
1668 return -ENOMEM;
1669
Andreas Larsson0da83bb2013-01-29 15:53:40 +01001670 for (i = 0; i < master->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01001671 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001672
1673 for (i = 0; i < nb; i++)
1674 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1675
1676 return 0;
1677}
1678#else
1679static int of_spi_register_master(struct spi_master *master)
1680{
1681 return 0;
1682}
1683#endif
1684
David Brownell8ae12a02006-01-08 13:34:19 -08001685/**
1686 * spi_register_master - register SPI master controller
1687 * @master: initialized master, originally from spi_alloc_master()
David Brownell33e34dc2007-05-08 00:32:21 -07001688 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001689 *
1690 * SPI master controllers connect to their drivers using some non-SPI bus,
1691 * such as the platform bus. The final stage of probe() in that code
1692 * includes calling spi_register_master() to hook up to this SPI bus glue.
1693 *
1694 * SPI controllers use board specific (often SOC specific) bus numbers,
1695 * and board-specific addressing for SPI devices combines those numbers
1696 * with chip select numbers. Since SPI does not directly support dynamic
1697 * device identification, boards need configuration tables telling which
1698 * chip is at which address.
1699 *
1700 * This must be called from context that can sleep. It returns zero on
1701 * success, else a negative error code (dropping the master's refcount).
David Brownell0c8684612006-01-08 13:34:25 -08001702 * After a successful return, the caller is responsible for calling
1703 * spi_unregister_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001704 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001705int spi_register_master(struct spi_master *master)
David Brownell8ae12a02006-01-08 13:34:19 -08001706{
David Brownelle44a45a2007-06-03 13:50:40 -07001707 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Tony Jones49dce682007-10-16 01:27:48 -07001708 struct device *dev = master->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08001709 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08001710 int status = -ENODEV;
1711 int dynamic = 0;
1712
David Brownell0c8684612006-01-08 13:34:25 -08001713 if (!dev)
1714 return -ENODEV;
1715
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001716 status = of_spi_register_master(master);
1717 if (status)
1718 return status;
1719
David Brownell082c8cb2007-07-31 00:39:45 -07001720 /* even if it's just one always-selected device, there must
1721 * be at least one chipselect
1722 */
1723 if (master->num_chipselect == 0)
1724 return -EINVAL;
1725
Grant Likelybb297852012-12-21 19:32:09 +00001726 if ((master->bus_num < 0) && master->dev.of_node)
1727 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1728
David Brownell8ae12a02006-01-08 13:34:19 -08001729 /* convention: dynamically assigned bus IDs count down from the max */
David Brownella020ed72006-04-03 15:49:04 -07001730 if (master->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07001731 /* FIXME switch to an IDR based scheme, something like
1732 * I2C now uses, so we can't run out of "dynamic" IDs
1733 */
David Brownell8ae12a02006-01-08 13:34:19 -08001734 master->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08001735 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08001736 }
1737
Mark Brown5424d432014-12-10 17:40:53 +00001738 INIT_LIST_HEAD(&master->queue);
1739 spin_lock_init(&master->queue_lock);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07001740 spin_lock_init(&master->bus_lock_spinlock);
1741 mutex_init(&master->bus_lock_mutex);
1742 master->bus_lock_flag = 0;
Mark Brownb1589352013-10-05 11:50:40 +01001743 init_completion(&master->xfer_completion);
Mark Brown6ad45a22014-02-02 13:47:47 +00001744 if (!master->max_dma_len)
1745 master->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07001746
David Brownell8ae12a02006-01-08 13:34:19 -08001747 /* register the device, then userspace will see it.
1748 * registration fails if the bus ID is in use.
1749 */
Kay Sievers35f74fc2009-01-06 10:44:37 -08001750 dev_set_name(&master->dev, "spi%u", master->bus_num);
Tony Jones49dce682007-10-16 01:27:48 -07001751 status = device_add(&master->dev);
David Brownellb8852442006-01-08 13:34:23 -08001752 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08001753 goto done;
Kay Sievers35f74fc2009-01-06 10:44:37 -08001754 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
David Brownell8ae12a02006-01-08 13:34:19 -08001755 dynamic ? " (dynamic)" : "");
1756
Linus Walleijffbbdd212012-02-22 10:05:38 +01001757 /* If we're using a queued driver, start the queue */
1758 if (master->transfer)
1759 dev_info(dev, "master is unqueued, this is deprecated\n");
1760 else {
1761 status = spi_master_initialize_queue(master);
1762 if (status) {
Axel Line93b0722013-08-31 20:25:52 +08001763 device_del(&master->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001764 goto done;
1765 }
1766 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00001767 /* add statistics */
1768 spin_lock_init(&master->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001769
Feng Tang2b9603a2010-08-02 15:52:15 +08001770 mutex_lock(&board_lock);
1771 list_add_tail(&master->list, &spi_master_list);
1772 list_for_each_entry(bi, &board_list, list)
1773 spi_match_master_to_boardinfo(master, &bi->board_info);
1774 mutex_unlock(&board_lock);
1775
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001776 /* Register devices from the device tree and ACPI */
Anatolij Gustschin12b15e82010-07-27 22:35:58 +02001777 of_register_spi_devices(master);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001778 acpi_register_spi_devices(master);
David Brownell8ae12a02006-01-08 13:34:19 -08001779done:
1780 return status;
1781}
1782EXPORT_SYMBOL_GPL(spi_register_master);
1783
Mark Brown666d5b42013-08-31 18:50:52 +01001784static void devm_spi_unregister(struct device *dev, void *res)
1785{
1786 spi_unregister_master(*(struct spi_master **)res);
1787}
1788
1789/**
1790 * dev_spi_register_master - register managed SPI master controller
1791 * @dev: device managing SPI master
1792 * @master: initialized master, originally from spi_alloc_master()
1793 * Context: can sleep
1794 *
1795 * Register a SPI device as with spi_register_master() which will
1796 * automatically be unregister
1797 */
1798int devm_spi_register_master(struct device *dev, struct spi_master *master)
1799{
1800 struct spi_master **ptr;
1801 int ret;
1802
1803 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1804 if (!ptr)
1805 return -ENOMEM;
1806
1807 ret = spi_register_master(master);
Stephen Warren4b928942013-11-21 16:11:15 -07001808 if (!ret) {
Mark Brown666d5b42013-08-31 18:50:52 +01001809 *ptr = master;
1810 devres_add(dev, ptr);
1811 } else {
1812 devres_free(ptr);
1813 }
1814
1815 return ret;
1816}
1817EXPORT_SYMBOL_GPL(devm_spi_register_master);
1818
David Lamparter34860082010-08-30 23:54:17 +02001819static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08001820{
David Lamparter34860082010-08-30 23:54:17 +02001821 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08001822 return 0;
1823}
1824
1825/**
1826 * spi_unregister_master - unregister SPI master controller
1827 * @master: the master being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07001828 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001829 *
1830 * This call is used only by SPI master controller drivers, which are the
1831 * only ones directly touching chip registers.
1832 *
1833 * This must be called from context that can sleep.
1834 */
1835void spi_unregister_master(struct spi_master *master)
1836{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08001837 int dummy;
1838
Linus Walleijffbbdd212012-02-22 10:05:38 +01001839 if (master->queued) {
1840 if (spi_destroy_queue(master))
1841 dev_err(&master->dev, "queue remove failed\n");
1842 }
1843
Feng Tang2b9603a2010-08-02 15:52:15 +08001844 mutex_lock(&board_lock);
1845 list_del(&master->list);
1846 mutex_unlock(&board_lock);
1847
Sebastian Andrzej Siewior97dbf372010-12-21 17:24:31 -08001848 dummy = device_for_each_child(&master->dev, NULL, __unregister);
Tony Jones49dce682007-10-16 01:27:48 -07001849 device_unregister(&master->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001850}
1851EXPORT_SYMBOL_GPL(spi_unregister_master);
1852
Linus Walleijffbbdd212012-02-22 10:05:38 +01001853int spi_master_suspend(struct spi_master *master)
1854{
1855 int ret;
1856
1857 /* Basically no-ops for non-queued masters */
1858 if (!master->queued)
1859 return 0;
1860
1861 ret = spi_stop_queue(master);
1862 if (ret)
1863 dev_err(&master->dev, "queue stop failed\n");
1864
1865 return ret;
1866}
1867EXPORT_SYMBOL_GPL(spi_master_suspend);
1868
1869int spi_master_resume(struct spi_master *master)
1870{
1871 int ret;
1872
1873 if (!master->queued)
1874 return 0;
1875
1876 ret = spi_start_queue(master);
1877 if (ret)
1878 dev_err(&master->dev, "queue restart failed\n");
1879
1880 return ret;
1881}
1882EXPORT_SYMBOL_GPL(spi_master_resume);
1883
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001884static int __spi_master_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08001885{
1886 struct spi_master *m;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001887 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08001888
1889 m = container_of(dev, struct spi_master, dev);
1890 return m->bus_num == *bus_num;
1891}
1892
David Brownell8ae12a02006-01-08 13:34:19 -08001893/**
1894 * spi_busnum_to_master - look up master associated with bus_num
1895 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07001896 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001897 *
1898 * This call may be used with devices that are registered after
1899 * arch init time. It returns a refcounted pointer to the relevant
1900 * spi_master (which the caller must release), or NULL if there is
1901 * no such master registered.
1902 */
1903struct spi_master *spi_busnum_to_master(u16 bus_num)
1904{
Tony Jones49dce682007-10-16 01:27:48 -07001905 struct device *dev;
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001906 struct spi_master *master = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08001907
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001908 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Dave Young5ed2c832008-01-22 15:14:18 +08001909 __spi_master_match);
1910 if (dev)
1911 master = container_of(dev, struct spi_master, dev);
1912 /* reference got in class_find_device */
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001913 return master;
David Brownell8ae12a02006-01-08 13:34:19 -08001914}
1915EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1916
1917
1918/*-------------------------------------------------------------------------*/
1919
David Brownell7d077192009-06-17 16:26:03 -07001920/* Core methods for SPI master protocol drivers. Some of the
1921 * other core methods are currently defined as inline functions.
1922 */
1923
Stefan Brüns63ab6452015-08-23 16:06:30 +02001924static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
1925{
1926 if (master->bits_per_word_mask) {
1927 /* Only 32 bits fit in the mask */
1928 if (bits_per_word > 32)
1929 return -EINVAL;
1930 if (!(master->bits_per_word_mask &
1931 SPI_BPW_MASK(bits_per_word)))
1932 return -EINVAL;
1933 }
1934
1935 return 0;
1936}
1937
David Brownell7d077192009-06-17 16:26:03 -07001938/**
1939 * spi_setup - setup SPI mode and clock rate
1940 * @spi: the device whose settings are being modified
1941 * Context: can sleep, and no requests are queued to the device
1942 *
1943 * SPI protocol drivers may need to update the transfer mode if the
1944 * device doesn't work with its default. They may likewise need
1945 * to update clock rates or word sizes from initial values. This function
1946 * changes those settings, and must be called from a context that can sleep.
1947 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
1948 * effect the next time the device is selected and data is transferred to
1949 * or from it. When this function returns, the spi device is deselected.
1950 *
1951 * Note that this call will fail if the protocol driver specifies an option
1952 * that the underlying controller or its driver does not support. For
1953 * example, not all hardware supports wire transfers using nine bit words,
1954 * LSB-first wire encoding, or active-high chipselects.
1955 */
1956int spi_setup(struct spi_device *spi)
1957{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02001958 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03001959 int status;
David Brownell7d077192009-06-17 16:26:03 -07001960
wangyuhangf477b7f2013-08-11 18:15:17 +08001961 /* check mode to prevent that DUAL and QUAD set at the same time
1962 */
1963 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1964 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1965 dev_err(&spi->dev,
1966 "setup: can not select dual and quad at the same time\n");
1967 return -EINVAL;
1968 }
1969 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1970 */
1971 if ((spi->mode & SPI_3WIRE) && (spi->mode &
1972 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1973 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07001974 /* help drivers fail *cleanly* when they need options
1975 * that aren't supported with their current master
1976 */
1977 bad_bits = spi->mode & ~spi->master->mode_bits;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02001978 ugly_bits = bad_bits &
1979 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
1980 if (ugly_bits) {
1981 dev_warn(&spi->dev,
1982 "setup: ignoring unsupported mode bits %x\n",
1983 ugly_bits);
1984 spi->mode &= ~ugly_bits;
1985 bad_bits &= ~ugly_bits;
1986 }
David Brownelle7db06b2009-06-17 16:26:04 -07001987 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02001988 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07001989 bad_bits);
1990 return -EINVAL;
1991 }
1992
David Brownell7d077192009-06-17 16:26:03 -07001993 if (!spi->bits_per_word)
1994 spi->bits_per_word = 8;
1995
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03001996 status = __spi_validate_bits_per_word(spi->master, spi->bits_per_word);
1997 if (status)
1998 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02001999
Axel Lin052eb2d2014-02-10 00:08:05 +08002000 if (!spi->max_speed_hz)
2001 spi->max_speed_hz = spi->master->max_speed_hz;
2002
Ivan T. Ivanov1a7b7ee2015-03-13 18:43:49 +02002003 spi_set_cs(spi, false);
2004
Laxman Dewangancaae0702012-11-09 14:35:22 +05302005 if (spi->master->setup)
2006 status = spi->master->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07002007
Jingoo Han5fe5f052013-10-14 10:31:51 +09002008 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
David Brownell7d077192009-06-17 16:26:03 -07002009 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2010 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2011 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2012 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2013 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2014 spi->bits_per_word, spi->max_speed_hz,
2015 status);
2016
2017 return status;
2018}
2019EXPORT_SYMBOL_GPL(spi_setup);
2020
Mark Brown90808732013-11-13 23:44:15 +00002021static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002022{
2023 struct spi_master *master = spi->master;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302024 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002025 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002026
Mark Brown24a00132013-07-10 15:05:40 +01002027 if (list_empty(&message->transfers))
2028 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01002029
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002030 /* Half-duplex links include original MicroWire, and ones with
2031 * only one data pin like SPI_3WIRE (switches direction) or where
2032 * either MOSI or MISO is missing. They can also be caused by
2033 * software limitations.
2034 */
2035 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
2036 || (spi->mode & SPI_3WIRE)) {
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002037 unsigned flags = master->flags;
2038
2039 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2040 if (xfer->rx_buf && xfer->tx_buf)
2041 return -EINVAL;
2042 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
2043 return -EINVAL;
2044 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
2045 return -EINVAL;
2046 }
2047 }
2048
Laxman Dewangane6811d12012-11-09 14:36:45 +05302049 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302050 * Set transfer bits_per_word and max speed as spi device default if
2051 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08002052 * Set transfer tx_nbits and rx_nbits as single transfer default
2053 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05302054 */
2055 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05302056 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302057 if (!xfer->bits_per_word)
2058 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08002059
2060 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302061 xfer->speed_hz = spi->max_speed_hz;
Mark Brown7dc9fbc2015-08-20 11:52:18 -07002062 if (!xfer->speed_hz)
2063 xfer->speed_hz = master->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08002064
2065 if (master->max_speed_hz &&
2066 xfer->speed_hz > master->max_speed_hz)
2067 xfer->speed_hz = master->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02002068
Stefan Brüns63ab6452015-08-23 16:06:30 +02002069 if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
2070 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01002071
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002072 /*
2073 * SPI transfer length should be multiple of SPI word size
2074 * where SPI word size should be power-of-two multiple
2075 */
2076 if (xfer->bits_per_word <= 8)
2077 w_size = 1;
2078 else if (xfer->bits_per_word <= 16)
2079 w_size = 2;
2080 else
2081 w_size = 4;
2082
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002083 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002084 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002085 return -EINVAL;
2086
Mark Browna2fd4f92013-07-10 14:57:26 +01002087 if (xfer->speed_hz && master->min_speed_hz &&
2088 xfer->speed_hz < master->min_speed_hz)
2089 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08002090
2091 if (xfer->tx_buf && !xfer->tx_nbits)
2092 xfer->tx_nbits = SPI_NBITS_SINGLE;
2093 if (xfer->rx_buf && !xfer->rx_nbits)
2094 xfer->rx_nbits = SPI_NBITS_SINGLE;
2095 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01002096 * 1. check the value matches one of single, dual and quad
2097 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08002098 */
Sourav Poddardb90a442013-08-22 21:20:48 +05302099 if (xfer->tx_buf) {
2100 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2101 xfer->tx_nbits != SPI_NBITS_DUAL &&
2102 xfer->tx_nbits != SPI_NBITS_QUAD)
2103 return -EINVAL;
2104 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2105 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2106 return -EINVAL;
2107 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2108 !(spi->mode & SPI_TX_QUAD))
2109 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302110 }
wangyuhangf477b7f2013-08-11 18:15:17 +08002111 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05302112 if (xfer->rx_buf) {
2113 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2114 xfer->rx_nbits != SPI_NBITS_DUAL &&
2115 xfer->rx_nbits != SPI_NBITS_QUAD)
2116 return -EINVAL;
2117 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2118 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2119 return -EINVAL;
2120 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2121 !(spi->mode & SPI_RX_QUAD))
2122 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302123 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05302124 }
2125
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002126 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00002127
2128 return 0;
2129}
2130
2131static int __spi_async(struct spi_device *spi, struct spi_message *message)
2132{
2133 struct spi_master *master = spi->master;
2134
2135 message->spi = spi;
2136
Martin Sperleca2ebc2015-06-22 13:00:36 +00002137 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
2138 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2139
Mark Brown90808732013-11-13 23:44:15 +00002140 trace_spi_message_submit(message);
2141
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002142 return master->transfer(spi, message);
2143}
2144
David Brownell568d0692009-09-22 16:46:18 -07002145/**
2146 * spi_async - asynchronous SPI transfer
2147 * @spi: device with which data will be exchanged
2148 * @message: describes the data transfers, including completion callback
2149 * Context: any (irqs may be blocked, etc)
2150 *
2151 * This call may be used in_irq and other contexts which can't sleep,
2152 * as well as from task contexts which can sleep.
2153 *
2154 * The completion callback is invoked in a context which can't sleep.
2155 * Before that invocation, the value of message->status is undefined.
2156 * When the callback is issued, message->status holds either zero (to
2157 * indicate complete success) or a negative error code. After that
2158 * callback returns, the driver which issued the transfer request may
2159 * deallocate the associated memory; it's no longer in use by any SPI
2160 * core or controller driver code.
2161 *
2162 * Note that although all messages to a spi_device are handled in
2163 * FIFO order, messages may go to different devices in other orders.
2164 * Some device might be higher priority, or have various "hard" access
2165 * time requirements, for example.
2166 *
2167 * On detection of any fault during the transfer, processing of
2168 * the entire message is aborted, and the device is deselected.
2169 * Until returning from the associated message completion callback,
2170 * no other spi_message queued to that device will be processed.
2171 * (This rule applies equally to all the synchronous transfer calls,
2172 * which are wrappers around this core asynchronous primitive.)
2173 */
2174int spi_async(struct spi_device *spi, struct spi_message *message)
2175{
2176 struct spi_master *master = spi->master;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002177 int ret;
2178 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07002179
Mark Brown90808732013-11-13 23:44:15 +00002180 ret = __spi_validate(spi, message);
2181 if (ret != 0)
2182 return ret;
2183
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002184 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07002185
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002186 if (master->bus_lock_flag)
2187 ret = -EBUSY;
2188 else
2189 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07002190
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002191 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2192
2193 return ret;
David Brownell568d0692009-09-22 16:46:18 -07002194}
2195EXPORT_SYMBOL_GPL(spi_async);
2196
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002197/**
2198 * spi_async_locked - version of spi_async with exclusive bus usage
2199 * @spi: device with which data will be exchanged
2200 * @message: describes the data transfers, including completion callback
2201 * Context: any (irqs may be blocked, etc)
2202 *
2203 * This call may be used in_irq and other contexts which can't sleep,
2204 * as well as from task contexts which can sleep.
2205 *
2206 * The completion callback is invoked in a context which can't sleep.
2207 * Before that invocation, the value of message->status is undefined.
2208 * When the callback is issued, message->status holds either zero (to
2209 * indicate complete success) or a negative error code. After that
2210 * callback returns, the driver which issued the transfer request may
2211 * deallocate the associated memory; it's no longer in use by any SPI
2212 * core or controller driver code.
2213 *
2214 * Note that although all messages to a spi_device are handled in
2215 * FIFO order, messages may go to different devices in other orders.
2216 * Some device might be higher priority, or have various "hard" access
2217 * time requirements, for example.
2218 *
2219 * On detection of any fault during the transfer, processing of
2220 * the entire message is aborted, and the device is deselected.
2221 * Until returning from the associated message completion callback,
2222 * no other spi_message queued to that device will be processed.
2223 * (This rule applies equally to all the synchronous transfer calls,
2224 * which are wrappers around this core asynchronous primitive.)
2225 */
2226int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2227{
2228 struct spi_master *master = spi->master;
2229 int ret;
2230 unsigned long flags;
2231
Mark Brown90808732013-11-13 23:44:15 +00002232 ret = __spi_validate(spi, message);
2233 if (ret != 0)
2234 return ret;
2235
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002236 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2237
2238 ret = __spi_async(spi, message);
2239
2240 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2241
2242 return ret;
2243
2244}
2245EXPORT_SYMBOL_GPL(spi_async_locked);
2246
David Brownell7d077192009-06-17 16:26:03 -07002247
2248/*-------------------------------------------------------------------------*/
2249
2250/* Utility methods for SPI master protocol drivers, layered on
2251 * top of the core. Some other utility methods are defined as
2252 * inline functions.
2253 */
2254
Andrew Morton5d870c82006-01-11 11:23:49 -08002255static void spi_complete(void *arg)
2256{
2257 complete(arg);
2258}
2259
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002260static int __spi_sync(struct spi_device *spi, struct spi_message *message,
2261 int bus_locked)
2262{
2263 DECLARE_COMPLETION_ONSTACK(done);
2264 int status;
2265 struct spi_master *master = spi->master;
Mark Brown0461a412014-12-09 21:38:05 +00002266 unsigned long flags;
2267
2268 status = __spi_validate(spi, message);
2269 if (status != 0)
2270 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002271
2272 message->complete = spi_complete;
2273 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00002274 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002275
Martin Sperleca2ebc2015-06-22 13:00:36 +00002276 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
2277 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
2278
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002279 if (!bus_locked)
2280 mutex_lock(&master->bus_lock_mutex);
2281
Mark Brown0461a412014-12-09 21:38:05 +00002282 /* If we're not using the legacy transfer method then we will
2283 * try to transfer in the calling context so special case.
2284 * This code would be less tricky if we could remove the
2285 * support for driver implemented message queues.
2286 */
2287 if (master->transfer == spi_queued_transfer) {
2288 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2289
2290 trace_spi_message_submit(message);
2291
2292 status = __spi_queued_transfer(spi, message, false);
2293
2294 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2295 } else {
2296 status = spi_async_locked(spi, message);
2297 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002298
2299 if (!bus_locked)
2300 mutex_unlock(&master->bus_lock_mutex);
2301
2302 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00002303 /* Push out the messages in the calling context if we
2304 * can.
2305 */
Martin Sperleca2ebc2015-06-22 13:00:36 +00002306 if (master->transfer == spi_queued_transfer) {
2307 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2308 spi_sync_immediate);
2309 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
2310 spi_sync_immediate);
Mark Brownfc9e0f72014-12-10 13:46:33 +00002311 __spi_pump_messages(master, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00002312 }
Mark Brown0461a412014-12-09 21:38:05 +00002313
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002314 wait_for_completion(&done);
2315 status = message->status;
2316 }
2317 message->context = NULL;
2318 return status;
2319}
2320
David Brownell8ae12a02006-01-08 13:34:19 -08002321/**
2322 * spi_sync - blocking/synchronous SPI data transfers
2323 * @spi: device with which data will be exchanged
2324 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07002325 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002326 *
2327 * This call may only be used from a context that may sleep. The sleep
2328 * is non-interruptible, and has no timeout. Low-overhead controller
2329 * drivers may DMA directly into and out of the message buffers.
2330 *
2331 * Note that the SPI device's chip select is active during the message,
2332 * and then is normally disabled between messages. Drivers for some
2333 * frequently-used devices may want to minimize costs of selecting a chip,
2334 * by leaving it selected in anticipation that the next message will go
2335 * to the same chip. (That may increase power usage.)
2336 *
David Brownell0c8684612006-01-08 13:34:25 -08002337 * Also, the caller is guaranteeing that the memory associated with the
2338 * message will not be freed before this call returns.
2339 *
Marc Pignat9b938b72007-12-04 23:45:10 -08002340 * It returns zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002341 */
2342int spi_sync(struct spi_device *spi, struct spi_message *message)
2343{
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002344 return __spi_sync(spi, message, 0);
David Brownell8ae12a02006-01-08 13:34:19 -08002345}
2346EXPORT_SYMBOL_GPL(spi_sync);
2347
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002348/**
2349 * spi_sync_locked - version of spi_sync with exclusive bus usage
2350 * @spi: device with which data will be exchanged
2351 * @message: describes the data transfers
2352 * Context: can sleep
2353 *
2354 * This call may only be used from a context that may sleep. The sleep
2355 * is non-interruptible, and has no timeout. Low-overhead controller
2356 * drivers may DMA directly into and out of the message buffers.
2357 *
2358 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002359 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002360 * be released by a spi_bus_unlock call when the exclusive access is over.
2361 *
2362 * It returns zero on success, else a negative error code.
2363 */
2364int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2365{
2366 return __spi_sync(spi, message, 1);
2367}
2368EXPORT_SYMBOL_GPL(spi_sync_locked);
2369
2370/**
2371 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
2372 * @master: SPI bus master that should be locked for exclusive bus access
2373 * Context: can sleep
2374 *
2375 * This call may only be used from a context that may sleep. The sleep
2376 * is non-interruptible, and has no timeout.
2377 *
2378 * This call should be used by drivers that require exclusive access to the
2379 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
2380 * exclusive access is over. Data transfer must be done by spi_sync_locked
2381 * and spi_async_locked calls when the SPI bus lock is held.
2382 *
2383 * It returns zero on success, else a negative error code.
2384 */
2385int spi_bus_lock(struct spi_master *master)
2386{
2387 unsigned long flags;
2388
2389 mutex_lock(&master->bus_lock_mutex);
2390
2391 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2392 master->bus_lock_flag = 1;
2393 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2394
2395 /* mutex remains locked until spi_bus_unlock is called */
2396
2397 return 0;
2398}
2399EXPORT_SYMBOL_GPL(spi_bus_lock);
2400
2401/**
2402 * spi_bus_unlock - release the lock for exclusive SPI bus usage
2403 * @master: SPI bus master that was locked for exclusive bus access
2404 * Context: can sleep
2405 *
2406 * This call may only be used from a context that may sleep. The sleep
2407 * is non-interruptible, and has no timeout.
2408 *
2409 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
2410 * call.
2411 *
2412 * It returns zero on success, else a negative error code.
2413 */
2414int spi_bus_unlock(struct spi_master *master)
2415{
2416 master->bus_lock_flag = 0;
2417
2418 mutex_unlock(&master->bus_lock_mutex);
2419
2420 return 0;
2421}
2422EXPORT_SYMBOL_GPL(spi_bus_unlock);
2423
David Brownella9948b62006-04-02 10:37:40 -08002424/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09002425#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08002426
2427static u8 *buf;
2428
2429/**
2430 * spi_write_then_read - SPI synchronous write followed by read
2431 * @spi: device with which data will be exchanged
2432 * @txbuf: data to be written (need not be dma-safe)
2433 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07002434 * @rxbuf: buffer into which data will be read (need not be dma-safe)
2435 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07002436 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002437 *
2438 * This performs a half duplex MicroWire style transaction with the
2439 * device, sending txbuf and then reading rxbuf. The return value
2440 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08002441 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08002442 *
David Brownell0c8684612006-01-08 13:34:25 -08002443 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07002444 * portable code should never use this for more than 32 bytes.
2445 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c8684612006-01-08 13:34:25 -08002446 * spi_{async,sync}() calls with dma-safe buffers.
David Brownell8ae12a02006-01-08 13:34:19 -08002447 */
2448int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02002449 const void *txbuf, unsigned n_tx,
2450 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08002451{
David Brownell068f4072007-12-04 23:45:09 -08002452 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002453
2454 int status;
2455 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07002456 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08002457 u8 *local_buf;
2458
Mark Brownb3a223e2012-12-02 12:54:25 +09002459 /* Use preallocated DMA-safe buffer if we can. We can't avoid
2460 * copying here, (as a pure convenience thing), but we can
2461 * keep heap costs out of the hot path unless someone else is
2462 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08002463 */
Mark Brownb3a223e2012-12-02 12:54:25 +09002464 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08002465 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
2466 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09002467 if (!local_buf)
2468 return -ENOMEM;
2469 } else {
2470 local_buf = buf;
2471 }
David Brownell8ae12a02006-01-08 13:34:19 -08002472
Vitaly Wool8275c642006-01-08 13:34:28 -08002473 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09002474 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07002475 if (n_tx) {
2476 x[0].len = n_tx;
2477 spi_message_add_tail(&x[0], &message);
2478 }
2479 if (n_rx) {
2480 x[1].len = n_rx;
2481 spi_message_add_tail(&x[1], &message);
2482 }
Vitaly Wool8275c642006-01-08 13:34:28 -08002483
David Brownell8ae12a02006-01-08 13:34:19 -08002484 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07002485 x[0].tx_buf = local_buf;
2486 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08002487
2488 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08002489 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08002490 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07002491 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08002492
David Brownellbdff5492009-04-13 14:39:57 -07002493 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08002494 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002495 else
2496 kfree(local_buf);
2497
2498 return status;
2499}
2500EXPORT_SYMBOL_GPL(spi_write_then_read);
2501
2502/*-------------------------------------------------------------------------*/
2503
Pantelis Antoniouce79d542014-10-28 22:36:05 +02002504#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2505static int __spi_of_device_match(struct device *dev, void *data)
2506{
2507 return dev->of_node == data;
2508}
2509
2510/* must call put_device() when done with returned spi_device device */
2511static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
2512{
2513 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
2514 __spi_of_device_match);
2515 return dev ? to_spi_device(dev) : NULL;
2516}
2517
2518static int __spi_of_master_match(struct device *dev, const void *data)
2519{
2520 return dev->of_node == data;
2521}
2522
2523/* the spi masters are not using spi_bus, so we find it with another way */
2524static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
2525{
2526 struct device *dev;
2527
2528 dev = class_find_device(&spi_master_class, NULL, node,
2529 __spi_of_master_match);
2530 if (!dev)
2531 return NULL;
2532
2533 /* reference got in class_find_device */
2534 return container_of(dev, struct spi_master, dev);
2535}
2536
2537static int of_spi_notify(struct notifier_block *nb, unsigned long action,
2538 void *arg)
2539{
2540 struct of_reconfig_data *rd = arg;
2541 struct spi_master *master;
2542 struct spi_device *spi;
2543
2544 switch (of_reconfig_get_state_change(action, arg)) {
2545 case OF_RECONFIG_CHANGE_ADD:
2546 master = of_find_spi_master_by_node(rd->dn->parent);
2547 if (master == NULL)
2548 return NOTIFY_OK; /* not for us */
2549
2550 spi = of_register_spi_device(master, rd->dn);
2551 put_device(&master->dev);
2552
2553 if (IS_ERR(spi)) {
2554 pr_err("%s: failed to create for '%s'\n",
2555 __func__, rd->dn->full_name);
2556 return notifier_from_errno(PTR_ERR(spi));
2557 }
2558 break;
2559
2560 case OF_RECONFIG_CHANGE_REMOVE:
2561 /* find our device by node */
2562 spi = of_find_spi_device_by_node(rd->dn);
2563 if (spi == NULL)
2564 return NOTIFY_OK; /* no? not meant for us */
2565
2566 /* unregister takes one ref away */
2567 spi_unregister_device(spi);
2568
2569 /* and put the reference of the find */
2570 put_device(&spi->dev);
2571 break;
2572 }
2573
2574 return NOTIFY_OK;
2575}
2576
2577static struct notifier_block spi_of_notifier = {
2578 .notifier_call = of_spi_notify,
2579};
2580#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
2581extern struct notifier_block spi_of_notifier;
2582#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
2583
David Brownell8ae12a02006-01-08 13:34:19 -08002584static int __init spi_init(void)
2585{
David Brownellb8852442006-01-08 13:34:23 -08002586 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08002587
Christoph Lametere94b1762006-12-06 20:33:17 -08002588 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08002589 if (!buf) {
2590 status = -ENOMEM;
2591 goto err0;
2592 }
2593
2594 status = bus_register(&spi_bus_type);
2595 if (status < 0)
2596 goto err1;
2597
2598 status = class_register(&spi_master_class);
2599 if (status < 0)
2600 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02002601
Fabio Estevam52677202014-11-26 20:13:57 -02002602 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02002603 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
2604
David Brownell8ae12a02006-01-08 13:34:19 -08002605 return 0;
David Brownellb8852442006-01-08 13:34:23 -08002606
2607err2:
2608 bus_unregister(&spi_bus_type);
2609err1:
2610 kfree(buf);
2611 buf = NULL;
2612err0:
2613 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08002614}
David Brownellb8852442006-01-08 13:34:23 -08002615
David Brownell8ae12a02006-01-08 13:34:19 -08002616/* board_info is normally registered in arch_initcall(),
2617 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08002618 *
2619 * REVISIT only boardinfo really needs static linking. the rest (device and
2620 * driver registration) _could_ be dynamically linked (modular) ... costs
2621 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08002622 */
David Brownell673c0c02008-10-15 22:02:46 -07002623postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08002624