blob: 3abb3903f2ad454ef964299204bd3c2454890062 [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 Brownell0c868462006-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);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300273 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800274
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200275 ret = of_clk_set_defaults(dev->of_node, false);
276 if (ret)
277 return ret;
278
Ulf Hansson676e7c22014-09-19 20:27:41 +0200279 ret = dev_pm_domain_attach(dev, true);
280 if (ret != -EPROBE_DEFER) {
281 ret = sdrv->probe(to_spi_device(dev));
282 if (ret)
283 dev_pm_domain_detach(dev, true);
284 }
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300285
286 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800287}
288
289static int spi_drv_remove(struct device *dev)
290{
291 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300292 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800293
Jean Delvareaec35f42014-02-13 15:28:41 +0100294 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200295 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300296
297 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800298}
299
300static void spi_drv_shutdown(struct device *dev)
301{
302 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
303
304 sdrv->shutdown(to_spi_device(dev));
305}
306
David Brownell33e34dc2007-05-08 00:32:21 -0700307/**
308 * spi_register_driver - register a SPI driver
309 * @sdrv: the driver to register
310 * Context: can sleep
311 */
David Brownellb8852442006-01-08 13:34:23 -0800312int spi_register_driver(struct spi_driver *sdrv)
313{
314 sdrv->driver.bus = &spi_bus_type;
315 if (sdrv->probe)
316 sdrv->driver.probe = spi_drv_probe;
317 if (sdrv->remove)
318 sdrv->driver.remove = spi_drv_remove;
319 if (sdrv->shutdown)
320 sdrv->driver.shutdown = spi_drv_shutdown;
321 return driver_register(&sdrv->driver);
322}
323EXPORT_SYMBOL_GPL(spi_register_driver);
324
David Brownell8ae12a02006-01-08 13:34:19 -0800325/*-------------------------------------------------------------------------*/
326
327/* SPI devices should normally not be created by SPI device drivers; that
328 * would make them board-specific. Similarly with SPI master drivers.
329 * Device registration normally goes into like arch/.../mach.../board-YYY.c
330 * with other readonly (flashable) information about mainboard devices.
331 */
332
333struct boardinfo {
334 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800335 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800336};
337
338static LIST_HEAD(board_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800339static LIST_HEAD(spi_master_list);
340
341/*
342 * Used to protect add/del opertion for board_info list and
343 * spi_master list, and their matching process
344 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700345static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800346
Grant Likelydc87c982008-05-15 16:50:22 -0600347/**
348 * spi_alloc_device - Allocate a new SPI device
349 * @master: Controller to which device is connected
350 * Context: can sleep
351 *
352 * Allows a driver to allocate and initialize a spi_device without
353 * registering it immediately. This allows a driver to directly
354 * fill the spi_device with device parameters before calling
355 * spi_add_device() on it.
356 *
357 * Caller is responsible to call spi_add_device() on the returned
358 * spi_device structure to add it to the SPI master. If the caller
359 * needs to discard the spi_device without adding it, then it should
360 * call spi_dev_put() on it.
361 *
362 * Returns a pointer to the new device, or NULL.
363 */
364struct spi_device *spi_alloc_device(struct spi_master *master)
365{
366 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600367
368 if (!spi_master_get(master))
369 return NULL;
370
Jingoo Han5fe5f052013-10-14 10:31:51 +0900371 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600372 if (!spi) {
Grant Likelydc87c982008-05-15 16:50:22 -0600373 spi_master_put(master);
374 return NULL;
375 }
376
377 spi->master = master;
Laurent Pinchart178db7d2011-12-12 01:15:06 +0100378 spi->dev.parent = &master->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600379 spi->dev.bus = &spi_bus_type;
380 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100381 spi->cs_gpio = -ENOENT;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000382
383 spin_lock_init(&spi->statistics.lock);
384
Grant Likelydc87c982008-05-15 16:50:22 -0600385 device_initialize(&spi->dev);
386 return spi;
387}
388EXPORT_SYMBOL_GPL(spi_alloc_device);
389
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200390static void spi_dev_set_name(struct spi_device *spi)
391{
392 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
393
394 if (adev) {
395 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
396 return;
397 }
398
399 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
400 spi->chip_select);
401}
402
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200403static int spi_dev_check(struct device *dev, void *data)
404{
405 struct spi_device *spi = to_spi_device(dev);
406 struct spi_device *new_spi = data;
407
408 if (spi->master == new_spi->master &&
409 spi->chip_select == new_spi->chip_select)
410 return -EBUSY;
411 return 0;
412}
413
Grant Likelydc87c982008-05-15 16:50:22 -0600414/**
415 * spi_add_device - Add spi_device allocated with spi_alloc_device
416 * @spi: spi_device to register
417 *
418 * Companion function to spi_alloc_device. Devices allocated with
419 * spi_alloc_device can be added onto the spi bus with this function.
420 *
David Brownelle48880e2008-08-15 00:40:44 -0700421 * Returns 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600422 */
423int spi_add_device(struct spi_device *spi)
424{
David Brownelle48880e2008-08-15 00:40:44 -0700425 static DEFINE_MUTEX(spi_add_lock);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100426 struct spi_master *master = spi->master;
427 struct device *dev = master->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600428 int status;
429
430 /* Chipselects are numbered 0..max; validate. */
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100431 if (spi->chip_select >= master->num_chipselect) {
Grant Likelydc87c982008-05-15 16:50:22 -0600432 dev_err(dev, "cs%d >= max %d\n",
433 spi->chip_select,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100434 master->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600435 return -EINVAL;
436 }
437
438 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200439 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700440
441 /* We need to make sure there's no other device with this
442 * chipselect **BEFORE** we call setup(), else we'll trash
443 * its configuration. Lock against concurrent add() calls.
444 */
445 mutex_lock(&spi_add_lock);
446
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200447 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
448 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700449 dev_err(dev, "chipselect %d already in use\n",
450 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700451 goto done;
452 }
453
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100454 if (master->cs_gpios)
455 spi->cs_gpio = master->cs_gpios[spi->chip_select];
456
David Brownelle48880e2008-08-15 00:40:44 -0700457 /* Drivers may modify this initial i/o setup, but will
458 * normally rely on the device being setup. Devices
459 * using SPI_CS_HIGH can't coexist well otherwise...
460 */
David Brownell7d077192009-06-17 16:26:03 -0700461 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600462 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200463 dev_err(dev, "can't setup %s, status %d\n",
464 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700465 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600466 }
467
David Brownelle48880e2008-08-15 00:40:44 -0700468 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600469 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700470 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200471 dev_err(dev, "can't add %s, status %d\n",
472 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700473 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800474 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600475
David Brownelle48880e2008-08-15 00:40:44 -0700476done:
477 mutex_unlock(&spi_add_lock);
478 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600479}
480EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800481
David Brownell33e34dc2007-05-08 00:32:21 -0700482/**
483 * spi_new_device - instantiate one new SPI device
484 * @master: Controller to which device is connected
485 * @chip: Describes the SPI device
486 * Context: can sleep
487 *
488 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800489 * after board init creates the hard-wired devices. Some development
490 * platforms may not be able to use spi_register_board_info though, and
491 * this is exported so that for example a USB or parport based adapter
492 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700493 *
494 * Returns the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800495 */
Adrian Bunke9d5a462007-03-26 21:32:23 -0800496struct spi_device *spi_new_device(struct spi_master *master,
497 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800498{
499 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800500 int status;
501
David Brownell082c8cb2007-07-31 00:39:45 -0700502 /* NOTE: caller did any chip->bus_num checks necessary.
503 *
504 * Also, unless we change the return value convention to use
505 * error-or-pointer (not NULL-or-pointer), troubleshootability
506 * suggests syslogged diagnostics are best here (ugh).
507 */
508
Grant Likelydc87c982008-05-15 16:50:22 -0600509 proxy = spi_alloc_device(master);
510 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800511 return NULL;
512
Grant Likely102eb972008-07-23 21:29:55 -0700513 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
514
David Brownell8ae12a02006-01-08 13:34:19 -0800515 proxy->chip_select = chip->chip_select;
516 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700517 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800518 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700519 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800520 proxy->dev.platform_data = (void *) chip->platform_data;
521 proxy->controller_data = chip->controller_data;
522 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800523
Grant Likelydc87c982008-05-15 16:50:22 -0600524 status = spi_add_device(proxy);
David Brownell8ae12a02006-01-08 13:34:19 -0800525 if (status < 0) {
Grant Likelydc87c982008-05-15 16:50:22 -0600526 spi_dev_put(proxy);
527 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800528 }
529
David Brownell8ae12a02006-01-08 13:34:19 -0800530 return proxy;
531}
532EXPORT_SYMBOL_GPL(spi_new_device);
533
Feng Tang2b9603a2010-08-02 15:52:15 +0800534static void spi_match_master_to_boardinfo(struct spi_master *master,
535 struct spi_board_info *bi)
536{
537 struct spi_device *dev;
538
539 if (master->bus_num != bi->bus_num)
540 return;
541
542 dev = spi_new_device(master, bi);
543 if (!dev)
544 dev_err(master->dev.parent, "can't create new device for %s\n",
545 bi->modalias);
546}
547
David Brownell33e34dc2007-05-08 00:32:21 -0700548/**
549 * spi_register_board_info - register SPI devices for a given board
550 * @info: array of chip descriptors
551 * @n: how many descriptors are provided
552 * Context: can sleep
553 *
David Brownell8ae12a02006-01-08 13:34:19 -0800554 * Board-specific early init code calls this (probably during arch_initcall)
555 * with segments of the SPI device table. Any device nodes are created later,
556 * after the relevant parent SPI controller (bus_num) is defined. We keep
557 * this table of devices forever, so that reloading a controller driver will
558 * not make Linux forget about these hard-wired devices.
559 *
560 * Other code can also call this, e.g. a particular add-on board might provide
561 * SPI devices through its expansion connector, so code initializing that board
562 * would naturally declare its SPI devices.
563 *
564 * The board info passed can safely be __initdata ... but be careful of
565 * any embedded pointers (platform_data, etc), they're copied as-is.
566 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000567int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800568{
Feng Tang2b9603a2010-08-02 15:52:15 +0800569 struct boardinfo *bi;
570 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800571
Xiubo Lic7908a32014-09-24 14:30:29 +0800572 if (!n)
573 return -EINVAL;
574
Feng Tang2b9603a2010-08-02 15:52:15 +0800575 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800576 if (!bi)
577 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800578
Feng Tang2b9603a2010-08-02 15:52:15 +0800579 for (i = 0; i < n; i++, bi++, info++) {
580 struct spi_master *master;
David Brownell8ae12a02006-01-08 13:34:19 -0800581
Feng Tang2b9603a2010-08-02 15:52:15 +0800582 memcpy(&bi->board_info, info, sizeof(*info));
583 mutex_lock(&board_lock);
584 list_add_tail(&bi->list, &board_list);
585 list_for_each_entry(master, &spi_master_list, list)
586 spi_match_master_to_boardinfo(master, &bi->board_info);
587 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800588 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800589
590 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800591}
592
593/*-------------------------------------------------------------------------*/
594
Mark Brownb1589352013-10-05 11:50:40 +0100595static void spi_set_cs(struct spi_device *spi, bool enable)
596{
597 if (spi->mode & SPI_CS_HIGH)
598 enable = !enable;
599
600 if (spi->cs_gpio >= 0)
601 gpio_set_value(spi->cs_gpio, !enable);
602 else if (spi->master->set_cs)
603 spi->master->set_cs(spi, !enable);
604}
605
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200606#ifdef CONFIG_HAS_DMA
Mark Brown6ad45a22014-02-02 13:47:47 +0000607static int spi_map_buf(struct spi_master *master, struct device *dev,
608 struct sg_table *sgt, void *buf, size_t len,
609 enum dma_data_direction dir)
610{
611 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500612 int desc_len;
613 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000614 struct page *vm_page;
615 void *sg_buf;
616 size_t min;
617 int i, ret;
618
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500619 if (vmalloced_buf) {
620 desc_len = PAGE_SIZE;
621 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
622 } else {
623 desc_len = master->max_dma_len;
624 sgs = DIV_ROUND_UP(len, desc_len);
625 }
626
Mark Brown6ad45a22014-02-02 13:47:47 +0000627 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
628 if (ret != 0)
629 return ret;
630
631 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000632
633 if (vmalloced_buf) {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500634 min = min_t(size_t,
635 len, desc_len - offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000636 vm_page = vmalloc_to_page(buf);
637 if (!vm_page) {
638 sg_free_table(sgt);
639 return -ENOMEM;
640 }
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000641 sg_set_page(&sgt->sgl[i], vm_page,
642 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000643 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500644 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000645 sg_buf = buf;
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000646 sg_set_buf(&sgt->sgl[i], sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000647 }
648
Mark Brown6ad45a22014-02-02 13:47:47 +0000649
650 buf += min;
651 len -= min;
652 }
653
654 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200655 if (!ret)
656 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000657 if (ret < 0) {
658 sg_free_table(sgt);
659 return ret;
660 }
661
662 sgt->nents = ret;
663
664 return 0;
665}
666
667static void spi_unmap_buf(struct spi_master *master, struct device *dev,
668 struct sg_table *sgt, enum dma_data_direction dir)
669{
670 if (sgt->orig_nents) {
671 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
672 sg_free_table(sgt);
673 }
674}
675
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200676static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000677{
Mark Brown99adef32014-01-16 12:22:43 +0000678 struct device *tx_dev, *rx_dev;
679 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000680 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000681
Mark Brown6ad45a22014-02-02 13:47:47 +0000682 if (!master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000683 return 0;
684
Leilk Liuc37f45b2015-07-23 17:10:40 +0800685 if (master->dma_tx)
686 tx_dev = master->dma_tx->device->dev;
687 else
688 tx_dev = &master->dev;
689
690 if (master->dma_rx)
691 rx_dev = master->dma_rx->device->dev;
692 else
693 rx_dev = &master->dev;
Mark Brown99adef32014-01-16 12:22:43 +0000694
695 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
696 if (!master->can_dma(master, msg->spi, xfer))
697 continue;
698
699 if (xfer->tx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000700 ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
701 (void *)xfer->tx_buf, xfer->len,
702 DMA_TO_DEVICE);
703 if (ret != 0)
704 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000705 }
706
707 if (xfer->rx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000708 ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
709 xfer->rx_buf, xfer->len,
710 DMA_FROM_DEVICE);
711 if (ret != 0) {
712 spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
713 DMA_TO_DEVICE);
714 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000715 }
716 }
717 }
718
719 master->cur_msg_mapped = true;
720
721 return 0;
722}
723
Martin Sperl4b786452015-05-25 10:13:10 +0000724static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000725{
726 struct spi_transfer *xfer;
727 struct device *tx_dev, *rx_dev;
728
Mark Brown6ad45a22014-02-02 13:47:47 +0000729 if (!master->cur_msg_mapped || !master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000730 return 0;
731
Leilk Liuc37f45b2015-07-23 17:10:40 +0800732 if (master->dma_tx)
733 tx_dev = master->dma_tx->device->dev;
734 else
735 tx_dev = &master->dev;
736
737 if (master->dma_rx)
738 rx_dev = master->dma_rx->device->dev;
739 else
740 rx_dev = &master->dev;
Mark Brown99adef32014-01-16 12:22:43 +0000741
742 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
743 if (!master->can_dma(master, msg->spi, xfer))
744 continue;
745
Mark Brown6ad45a22014-02-02 13:47:47 +0000746 spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
747 spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000748 }
749
750 return 0;
751}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200752#else /* !CONFIG_HAS_DMA */
753static inline int __spi_map_msg(struct spi_master *master,
754 struct spi_message *msg)
755{
756 return 0;
757}
758
Martin Sperl4b786452015-05-25 10:13:10 +0000759static inline int __spi_unmap_msg(struct spi_master *master,
760 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200761{
762 return 0;
763}
764#endif /* !CONFIG_HAS_DMA */
765
Martin Sperl4b786452015-05-25 10:13:10 +0000766static inline int spi_unmap_msg(struct spi_master *master,
767 struct spi_message *msg)
768{
769 struct spi_transfer *xfer;
770
771 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
772 /*
773 * Restore the original value of tx_buf or rx_buf if they are
774 * NULL.
775 */
776 if (xfer->tx_buf == master->dummy_tx)
777 xfer->tx_buf = NULL;
778 if (xfer->rx_buf == master->dummy_rx)
779 xfer->rx_buf = NULL;
780 }
781
782 return __spi_unmap_msg(master, msg);
783}
784
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200785static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
786{
787 struct spi_transfer *xfer;
788 void *tmp;
789 unsigned int max_tx, max_rx;
790
791 if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
792 max_tx = 0;
793 max_rx = 0;
794
795 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
796 if ((master->flags & SPI_MASTER_MUST_TX) &&
797 !xfer->tx_buf)
798 max_tx = max(xfer->len, max_tx);
799 if ((master->flags & SPI_MASTER_MUST_RX) &&
800 !xfer->rx_buf)
801 max_rx = max(xfer->len, max_rx);
802 }
803
804 if (max_tx) {
805 tmp = krealloc(master->dummy_tx, max_tx,
806 GFP_KERNEL | GFP_DMA);
807 if (!tmp)
808 return -ENOMEM;
809 master->dummy_tx = tmp;
810 memset(tmp, 0, max_tx);
811 }
812
813 if (max_rx) {
814 tmp = krealloc(master->dummy_rx, max_rx,
815 GFP_KERNEL | GFP_DMA);
816 if (!tmp)
817 return -ENOMEM;
818 master->dummy_rx = tmp;
819 }
820
821 if (max_tx || max_rx) {
822 list_for_each_entry(xfer, &msg->transfers,
823 transfer_list) {
824 if (!xfer->tx_buf)
825 xfer->tx_buf = master->dummy_tx;
826 if (!xfer->rx_buf)
827 xfer->rx_buf = master->dummy_rx;
828 }
829 }
830 }
831
832 return __spi_map_msg(master, msg);
833}
Mark Brown99adef32014-01-16 12:22:43 +0000834
Mark Brownb1589352013-10-05 11:50:40 +0100835/*
836 * spi_transfer_one_message - Default implementation of transfer_one_message()
837 *
838 * This is a standard implementation of transfer_one_message() for
839 * drivers which impelment a transfer_one() operation. It provides
840 * standard handling of delays and chip select management.
841 */
842static int spi_transfer_one_message(struct spi_master *master,
843 struct spi_message *msg)
844{
845 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +0100846 bool keep_cs = false;
847 int ret = 0;
Nicholas Mc Guire682a71b2015-02-02 03:30:32 -0500848 unsigned long ms = 1;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000849 struct spi_statistics *statm = &master->statistics;
850 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +0100851
852 spi_set_cs(msg->spi, true);
853
Martin Sperleca2ebc2015-06-22 13:00:36 +0000854 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
855 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
856
Mark Brownb1589352013-10-05 11:50:40 +0100857 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
858 trace_spi_transfer_start(msg, xfer);
859
Martin Sperleca2ebc2015-06-22 13:00:36 +0000860 spi_statistics_add_transfer_stats(statm, xfer, master);
861 spi_statistics_add_transfer_stats(stats, xfer, master);
862
Mark Brown38ec10f2014-08-16 16:27:41 +0100863 if (xfer->tx_buf || xfer->rx_buf) {
864 reinit_completion(&master->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +0100865
Mark Brown38ec10f2014-08-16 16:27:41 +0100866 ret = master->transfer_one(master, msg->spi, xfer);
867 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000868 SPI_STATISTICS_INCREMENT_FIELD(statm,
869 errors);
870 SPI_STATISTICS_INCREMENT_FIELD(stats,
871 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +0100872 dev_err(&msg->spi->dev,
873 "SPI transfer failed: %d\n", ret);
874 goto out;
875 }
Mark Brownb1589352013-10-05 11:50:40 +0100876
Mark Brown38ec10f2014-08-16 16:27:41 +0100877 if (ret > 0) {
878 ret = 0;
879 ms = xfer->len * 8 * 1000 / xfer->speed_hz;
880 ms += ms + 100; /* some tolerance */
Mark Brown16a0ce42014-01-30 22:16:41 +0000881
Mark Brown38ec10f2014-08-16 16:27:41 +0100882 ms = wait_for_completion_timeout(&master->xfer_completion,
883 msecs_to_jiffies(ms));
884 }
Mark Brown16a0ce42014-01-30 22:16:41 +0000885
Mark Brown38ec10f2014-08-16 16:27:41 +0100886 if (ms == 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000887 SPI_STATISTICS_INCREMENT_FIELD(statm,
888 timedout);
889 SPI_STATISTICS_INCREMENT_FIELD(stats,
890 timedout);
Mark Brown38ec10f2014-08-16 16:27:41 +0100891 dev_err(&msg->spi->dev,
892 "SPI transfer timed out\n");
893 msg->status = -ETIMEDOUT;
894 }
895 } else {
896 if (xfer->len)
897 dev_err(&msg->spi->dev,
898 "Bufferless transfer has length %u\n",
899 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +0800900 }
Mark Brownb1589352013-10-05 11:50:40 +0100901
902 trace_spi_transfer_stop(msg, xfer);
903
904 if (msg->status != -EINPROGRESS)
905 goto out;
906
907 if (xfer->delay_usecs)
908 udelay(xfer->delay_usecs);
909
910 if (xfer->cs_change) {
911 if (list_is_last(&xfer->transfer_list,
912 &msg->transfers)) {
913 keep_cs = true;
914 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +0000915 spi_set_cs(msg->spi, false);
916 udelay(10);
917 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +0100918 }
919 }
920
921 msg->actual_length += xfer->len;
922 }
923
924out:
925 if (ret != 0 || !keep_cs)
926 spi_set_cs(msg->spi, false);
927
928 if (msg->status == -EINPROGRESS)
929 msg->status = ret;
930
Geert Uytterhoevenff61eb42015-04-07 20:39:19 +0200931 if (msg->status && master->handle_err)
Andy Shevchenkob716c4f2015-02-27 17:34:15 +0200932 master->handle_err(master, msg);
933
Mark Brownb1589352013-10-05 11:50:40 +0100934 spi_finalize_current_message(master);
935
936 return ret;
937}
938
939/**
940 * spi_finalize_current_transfer - report completion of a transfer
Thierry Reding2c675682014-08-08 13:02:36 +0200941 * @master: the master reporting completion
Mark Brownb1589352013-10-05 11:50:40 +0100942 *
943 * Called by SPI drivers using the core transfer_one_message()
944 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +0100945 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +0100946 */
947void spi_finalize_current_transfer(struct spi_master *master)
948{
949 complete(&master->xfer_completion);
950}
951EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
952
Linus Walleijffbbdd212012-02-22 10:05:38 +0100953/**
Mark Brownfc9e0f72014-12-10 13:46:33 +0000954 * __spi_pump_messages - function which processes spi message queue
955 * @master: master to process queue for
956 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +0100957 *
958 * This function checks if there is any spi message in the queue that
959 * needs processing and if so call out to the driver to initialize hardware
960 * and transfer each message.
961 *
Mark Brown0461a412014-12-09 21:38:05 +0000962 * Note that it is called both from the kthread itself and also from
963 * inside spi_sync(); the queue extraction handling at the top of the
964 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +0100965 */
Mark Brownfc9e0f72014-12-10 13:46:33 +0000966static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +0100967{
Linus Walleijffbbdd212012-02-22 10:05:38 +0100968 unsigned long flags;
969 bool was_busy = false;
970 int ret;
971
Mark Brown983aee52014-12-09 19:46:56 +0000972 /* Lock queue */
Linus Walleijffbbdd212012-02-22 10:05:38 +0100973 spin_lock_irqsave(&master->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +0000974
975 /* Make sure we are not already running a message */
976 if (master->cur_msg) {
977 spin_unlock_irqrestore(&master->queue_lock, flags);
978 return;
979 }
980
Mark Brown0461a412014-12-09 21:38:05 +0000981 /* If another context is idling the device then defer */
982 if (master->idling) {
983 queue_kthread_work(&master->kworker, &master->pump_messages);
984 spin_unlock_irqrestore(&master->queue_lock, flags);
985 return;
986 }
987
Mark Brown983aee52014-12-09 19:46:56 +0000988 /* Check if the queue is idle */
Linus Walleijffbbdd212012-02-22 10:05:38 +0100989 if (list_empty(&master->queue) || !master->running) {
Bryan Freedb0b36b82013-03-13 11:17:40 -0700990 if (!master->busy) {
991 spin_unlock_irqrestore(&master->queue_lock, flags);
992 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +0100993 }
Mark Brownfc9e0f72014-12-10 13:46:33 +0000994
995 /* Only do teardown in the thread */
996 if (!in_kthread) {
997 queue_kthread_work(&master->kworker,
998 &master->pump_messages);
999 spin_unlock_irqrestore(&master->queue_lock, flags);
1000 return;
1001 }
1002
Linus Walleijffbbdd212012-02-22 10:05:38 +01001003 master->busy = false;
Mark Brown0461a412014-12-09 21:38:05 +00001004 master->idling = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001005 spin_unlock_irqrestore(&master->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001006
Mark Brown3a2eba92014-01-28 20:17:03 +00001007 kfree(master->dummy_rx);
1008 master->dummy_rx = NULL;
1009 kfree(master->dummy_tx);
1010 master->dummy_tx = NULL;
Bryan Freedb0b36b82013-03-13 11:17:40 -07001011 if (master->unprepare_transfer_hardware &&
1012 master->unprepare_transfer_hardware(master))
1013 dev_err(&master->dev,
1014 "failed to unprepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001015 if (master->auto_runtime_pm) {
1016 pm_runtime_mark_last_busy(master->dev.parent);
1017 pm_runtime_put_autosuspend(master->dev.parent);
1018 }
Mark Brown56ec1972013-10-07 19:33:53 +01001019 trace_spi_master_idle(master);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001020
Mark Brown0461a412014-12-09 21:38:05 +00001021 spin_lock_irqsave(&master->queue_lock, flags);
1022 master->idling = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001023 spin_unlock_irqrestore(&master->queue_lock, flags);
1024 return;
1025 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001026
Linus Walleijffbbdd212012-02-22 10:05:38 +01001027 /* Extract head of queue */
1028 master->cur_msg =
Axel Lina89e2d22014-01-09 16:03:58 +08001029 list_first_entry(&master->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001030
1031 list_del_init(&master->cur_msg->queue);
1032 if (master->busy)
1033 was_busy = true;
1034 else
1035 master->busy = true;
1036 spin_unlock_irqrestore(&master->queue_lock, flags);
1037
Mark Brown49834de2013-07-28 14:47:02 +01001038 if (!was_busy && master->auto_runtime_pm) {
1039 ret = pm_runtime_get_sync(master->dev.parent);
1040 if (ret < 0) {
1041 dev_err(&master->dev, "Failed to power device: %d\n",
1042 ret);
1043 return;
1044 }
1045 }
1046
Mark Brown56ec1972013-10-07 19:33:53 +01001047 if (!was_busy)
1048 trace_spi_master_busy(master);
1049
Shubhrajyoti D7dfd2bd2012-05-10 19:20:41 +05301050 if (!was_busy && master->prepare_transfer_hardware) {
Linus Walleijffbbdd212012-02-22 10:05:38 +01001051 ret = master->prepare_transfer_hardware(master);
1052 if (ret) {
1053 dev_err(&master->dev,
1054 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001055
1056 if (master->auto_runtime_pm)
1057 pm_runtime_put(master->dev.parent);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001058 return;
1059 }
1060 }
1061
Mark Brown56ec1972013-10-07 19:33:53 +01001062 trace_spi_message_start(master->cur_msg);
1063
Mark Brown2841a5f2013-10-05 00:23:12 +01001064 if (master->prepare_message) {
1065 ret = master->prepare_message(master, master->cur_msg);
1066 if (ret) {
1067 dev_err(&master->dev,
1068 "failed to prepare message: %d\n", ret);
1069 master->cur_msg->status = ret;
1070 spi_finalize_current_message(master);
1071 return;
1072 }
1073 master->cur_msg_prepared = true;
1074 }
1075
Mark Brown99adef32014-01-16 12:22:43 +00001076 ret = spi_map_msg(master, master->cur_msg);
1077 if (ret) {
1078 master->cur_msg->status = ret;
1079 spi_finalize_current_message(master);
1080 return;
1081 }
1082
Linus Walleijffbbdd212012-02-22 10:05:38 +01001083 ret = master->transfer_one_message(master, master->cur_msg);
1084 if (ret) {
1085 dev_err(&master->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001086 "failed to transfer one message from queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001087 return;
1088 }
1089}
1090
Mark Brownfc9e0f72014-12-10 13:46:33 +00001091/**
1092 * spi_pump_messages - kthread work function which processes spi message queue
1093 * @work: pointer to kthread work struct contained in the master struct
1094 */
1095static void spi_pump_messages(struct kthread_work *work)
1096{
1097 struct spi_master *master =
1098 container_of(work, struct spi_master, pump_messages);
1099
1100 __spi_pump_messages(master, true);
1101}
1102
Linus Walleijffbbdd212012-02-22 10:05:38 +01001103static int spi_init_queue(struct spi_master *master)
1104{
1105 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1106
Linus Walleijffbbdd212012-02-22 10:05:38 +01001107 master->running = false;
1108 master->busy = false;
1109
1110 init_kthread_worker(&master->kworker);
1111 master->kworker_task = kthread_run(kthread_worker_fn,
Kees Cookf1701682013-07-03 15:04:58 -07001112 &master->kworker, "%s",
Linus Walleijffbbdd212012-02-22 10:05:38 +01001113 dev_name(&master->dev));
1114 if (IS_ERR(master->kworker_task)) {
1115 dev_err(&master->dev, "failed to create message pump task\n");
Jarkko Nikula98a8f5a2014-12-04 11:02:25 +02001116 return PTR_ERR(master->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001117 }
1118 init_kthread_work(&master->pump_messages, spi_pump_messages);
1119
1120 /*
1121 * Master config will indicate if this controller should run the
1122 * message pump with high (realtime) priority to reduce the transfer
1123 * latency on the bus by minimising the delay between a transfer
1124 * request and the scheduling of the message pump thread. Without this
1125 * setting the message pump thread will remain at default priority.
1126 */
1127 if (master->rt) {
1128 dev_info(&master->dev,
1129 "will run message pump with realtime priority\n");
1130 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
1131 }
1132
1133 return 0;
1134}
1135
1136/**
1137 * spi_get_next_queued_message() - called by driver to check for queued
1138 * messages
1139 * @master: the master to check for queued messages
1140 *
1141 * If there are more messages in the queue, the next message is returned from
1142 * this call.
1143 */
1144struct spi_message *spi_get_next_queued_message(struct spi_master *master)
1145{
1146 struct spi_message *next;
1147 unsigned long flags;
1148
1149 /* get a pointer to the next message, if any */
1150 spin_lock_irqsave(&master->queue_lock, flags);
Axel Lin1cfd97f2014-01-02 15:16:40 +08001151 next = list_first_entry_or_null(&master->queue, struct spi_message,
1152 queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001153 spin_unlock_irqrestore(&master->queue_lock, flags);
1154
1155 return next;
1156}
1157EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1158
1159/**
1160 * spi_finalize_current_message() - the current message is complete
1161 * @master: the master to return the message to
1162 *
1163 * Called by the driver to notify the core that the message in the front of the
1164 * queue is complete and can be removed from the queue.
1165 */
1166void spi_finalize_current_message(struct spi_master *master)
1167{
1168 struct spi_message *mesg;
1169 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001170 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001171
1172 spin_lock_irqsave(&master->queue_lock, flags);
1173 mesg = master->cur_msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001174 spin_unlock_irqrestore(&master->queue_lock, flags);
1175
Mark Brown99adef32014-01-16 12:22:43 +00001176 spi_unmap_msg(master, mesg);
1177
Mark Brown2841a5f2013-10-05 00:23:12 +01001178 if (master->cur_msg_prepared && master->unprepare_message) {
1179 ret = master->unprepare_message(master, mesg);
1180 if (ret) {
1181 dev_err(&master->dev,
1182 "failed to unprepare message: %d\n", ret);
1183 }
1184 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001185
Martin Sperl8e76ef82015-05-10 07:50:45 +00001186 spin_lock_irqsave(&master->queue_lock, flags);
1187 master->cur_msg = NULL;
Mark Brown2841a5f2013-10-05 00:23:12 +01001188 master->cur_msg_prepared = false;
Martin Sperl8e76ef82015-05-10 07:50:45 +00001189 queue_kthread_work(&master->kworker, &master->pump_messages);
1190 spin_unlock_irqrestore(&master->queue_lock, flags);
1191
1192 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001193
Linus Walleijffbbdd212012-02-22 10:05:38 +01001194 mesg->state = NULL;
1195 if (mesg->complete)
1196 mesg->complete(mesg->context);
1197}
1198EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1199
1200static int spi_start_queue(struct spi_master *master)
1201{
1202 unsigned long flags;
1203
1204 spin_lock_irqsave(&master->queue_lock, flags);
1205
1206 if (master->running || master->busy) {
1207 spin_unlock_irqrestore(&master->queue_lock, flags);
1208 return -EBUSY;
1209 }
1210
1211 master->running = true;
1212 master->cur_msg = NULL;
1213 spin_unlock_irqrestore(&master->queue_lock, flags);
1214
1215 queue_kthread_work(&master->kworker, &master->pump_messages);
1216
1217 return 0;
1218}
1219
1220static int spi_stop_queue(struct spi_master *master)
1221{
1222 unsigned long flags;
1223 unsigned limit = 500;
1224 int ret = 0;
1225
1226 spin_lock_irqsave(&master->queue_lock, flags);
1227
1228 /*
1229 * This is a bit lame, but is optimized for the common execution path.
1230 * A wait_queue on the master->busy could be used, but then the common
1231 * execution path (pump_messages) would be required to call wake_up or
1232 * friends on every SPI message. Do this instead.
1233 */
1234 while ((!list_empty(&master->queue) || master->busy) && limit--) {
1235 spin_unlock_irqrestore(&master->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001236 usleep_range(10000, 11000);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001237 spin_lock_irqsave(&master->queue_lock, flags);
1238 }
1239
1240 if (!list_empty(&master->queue) || master->busy)
1241 ret = -EBUSY;
1242 else
1243 master->running = false;
1244
1245 spin_unlock_irqrestore(&master->queue_lock, flags);
1246
1247 if (ret) {
1248 dev_warn(&master->dev,
1249 "could not stop message queue\n");
1250 return ret;
1251 }
1252 return ret;
1253}
1254
1255static int spi_destroy_queue(struct spi_master *master)
1256{
1257 int ret;
1258
1259 ret = spi_stop_queue(master);
1260
1261 /*
1262 * flush_kthread_worker will block until all work is done.
1263 * If the reason that stop_queue timed out is that the work will never
1264 * finish, then it does no good to call flush/stop thread, so
1265 * return anyway.
1266 */
1267 if (ret) {
1268 dev_err(&master->dev, "problem destroying queue\n");
1269 return ret;
1270 }
1271
1272 flush_kthread_worker(&master->kworker);
1273 kthread_stop(master->kworker_task);
1274
1275 return 0;
1276}
1277
Mark Brown0461a412014-12-09 21:38:05 +00001278static int __spi_queued_transfer(struct spi_device *spi,
1279 struct spi_message *msg,
1280 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001281{
1282 struct spi_master *master = spi->master;
1283 unsigned long flags;
1284
1285 spin_lock_irqsave(&master->queue_lock, flags);
1286
1287 if (!master->running) {
1288 spin_unlock_irqrestore(&master->queue_lock, flags);
1289 return -ESHUTDOWN;
1290 }
1291 msg->actual_length = 0;
1292 msg->status = -EINPROGRESS;
1293
1294 list_add_tail(&msg->queue, &master->queue);
Mark Brown0461a412014-12-09 21:38:05 +00001295 if (!master->busy && need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001296 queue_kthread_work(&master->kworker, &master->pump_messages);
1297
1298 spin_unlock_irqrestore(&master->queue_lock, flags);
1299 return 0;
1300}
1301
Mark Brown0461a412014-12-09 21:38:05 +00001302/**
1303 * spi_queued_transfer - transfer function for queued transfers
1304 * @spi: spi device which is requesting transfer
1305 * @msg: spi message which is to handled is queued to driver queue
1306 */
1307static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1308{
1309 return __spi_queued_transfer(spi, msg, true);
1310}
1311
Linus Walleijffbbdd212012-02-22 10:05:38 +01001312static int spi_master_initialize_queue(struct spi_master *master)
1313{
1314 int ret;
1315
Linus Walleijffbbdd212012-02-22 10:05:38 +01001316 master->transfer = spi_queued_transfer;
Mark Brownb1589352013-10-05 11:50:40 +01001317 if (!master->transfer_one_message)
1318 master->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001319
1320 /* Initialize and start queue */
1321 ret = spi_init_queue(master);
1322 if (ret) {
1323 dev_err(&master->dev, "problem initializing queue\n");
1324 goto err_init_queue;
1325 }
Mark Brownc3676d52014-05-01 10:47:52 -07001326 master->queued = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001327 ret = spi_start_queue(master);
1328 if (ret) {
1329 dev_err(&master->dev, "problem starting queue\n");
1330 goto err_start_queue;
1331 }
1332
1333 return 0;
1334
1335err_start_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001336 spi_destroy_queue(master);
Mark Brownc3676d52014-05-01 10:47:52 -07001337err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001338 return ret;
1339}
1340
1341/*-------------------------------------------------------------------------*/
1342
Andreas Larsson7cb94362012-12-04 15:09:38 +01001343#if defined(CONFIG_OF)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001344static struct spi_device *
1345of_register_spi_device(struct spi_master *master, struct device_node *nc)
1346{
1347 struct spi_device *spi;
1348 int rc;
1349 u32 value;
1350
1351 /* Alloc an spi_device */
1352 spi = spi_alloc_device(master);
1353 if (!spi) {
1354 dev_err(&master->dev, "spi_device alloc error for %s\n",
1355 nc->full_name);
1356 rc = -ENOMEM;
1357 goto err_out;
1358 }
1359
1360 /* Select device driver */
1361 rc = of_modalias_node(nc, spi->modalias,
1362 sizeof(spi->modalias));
1363 if (rc < 0) {
1364 dev_err(&master->dev, "cannot find modalias for %s\n",
1365 nc->full_name);
1366 goto err_out;
1367 }
1368
1369 /* Device address */
1370 rc = of_property_read_u32(nc, "reg", &value);
1371 if (rc) {
1372 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1373 nc->full_name, rc);
1374 goto err_out;
1375 }
1376 spi->chip_select = value;
1377
1378 /* Mode (clock phase/polarity/etc.) */
1379 if (of_find_property(nc, "spi-cpha", NULL))
1380 spi->mode |= SPI_CPHA;
1381 if (of_find_property(nc, "spi-cpol", NULL))
1382 spi->mode |= SPI_CPOL;
1383 if (of_find_property(nc, "spi-cs-high", NULL))
1384 spi->mode |= SPI_CS_HIGH;
1385 if (of_find_property(nc, "spi-3wire", NULL))
1386 spi->mode |= SPI_3WIRE;
1387 if (of_find_property(nc, "spi-lsb-first", NULL))
1388 spi->mode |= SPI_LSB_FIRST;
1389
1390 /* Device DUAL/QUAD mode */
1391 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1392 switch (value) {
1393 case 1:
1394 break;
1395 case 2:
1396 spi->mode |= SPI_TX_DUAL;
1397 break;
1398 case 4:
1399 spi->mode |= SPI_TX_QUAD;
1400 break;
1401 default:
1402 dev_warn(&master->dev,
1403 "spi-tx-bus-width %d not supported\n",
1404 value);
1405 break;
1406 }
1407 }
1408
1409 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1410 switch (value) {
1411 case 1:
1412 break;
1413 case 2:
1414 spi->mode |= SPI_RX_DUAL;
1415 break;
1416 case 4:
1417 spi->mode |= SPI_RX_QUAD;
1418 break;
1419 default:
1420 dev_warn(&master->dev,
1421 "spi-rx-bus-width %d not supported\n",
1422 value);
1423 break;
1424 }
1425 }
1426
1427 /* Device speed */
1428 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1429 if (rc) {
1430 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1431 nc->full_name, rc);
1432 goto err_out;
1433 }
1434 spi->max_speed_hz = value;
1435
1436 /* IRQ */
1437 spi->irq = irq_of_parse_and_map(nc, 0);
1438
1439 /* Store a pointer to the node in the device structure */
1440 of_node_get(nc);
1441 spi->dev.of_node = nc;
1442
1443 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001444 rc = spi_add_device(spi);
1445 if (rc) {
1446 dev_err(&master->dev, "spi_device register error %s\n",
1447 nc->full_name);
1448 goto err_out;
1449 }
1450
1451 return spi;
1452
1453err_out:
1454 spi_dev_put(spi);
1455 return ERR_PTR(rc);
1456}
1457
Grant Likelyd57a4282012-04-07 14:16:53 -06001458/**
1459 * of_register_spi_devices() - Register child devices onto the SPI bus
1460 * @master: Pointer to spi_master device
1461 *
1462 * Registers an spi_device for each child node of master node which has a 'reg'
1463 * property.
1464 */
1465static void of_register_spi_devices(struct spi_master *master)
1466{
1467 struct spi_device *spi;
1468 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001469
1470 if (!master->dev.of_node)
1471 return;
1472
Alexander Sverdlinf3b61592012-11-29 08:59:29 +01001473 for_each_available_child_of_node(master->dev.of_node, nc) {
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001474 spi = of_register_spi_device(master, nc);
1475 if (IS_ERR(spi))
1476 dev_warn(&master->dev, "Failed to create SPI device for %s\n",
Grant Likelyd57a4282012-04-07 14:16:53 -06001477 nc->full_name);
Grant Likelyd57a4282012-04-07 14:16:53 -06001478 }
1479}
1480#else
1481static void of_register_spi_devices(struct spi_master *master) { }
1482#endif
1483
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001484#ifdef CONFIG_ACPI
1485static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1486{
1487 struct spi_device *spi = data;
1488
1489 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1490 struct acpi_resource_spi_serialbus *sb;
1491
1492 sb = &ares->data.spi_serial_bus;
1493 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1494 spi->chip_select = sb->device_selection;
1495 spi->max_speed_hz = sb->connection_speed;
1496
1497 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1498 spi->mode |= SPI_CPHA;
1499 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1500 spi->mode |= SPI_CPOL;
1501 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1502 spi->mode |= SPI_CS_HIGH;
1503 }
1504 } else if (spi->irq < 0) {
1505 struct resource r;
1506
1507 if (acpi_dev_resource_interrupt(ares, 0, &r))
1508 spi->irq = r.start;
1509 }
1510
1511 /* Always tell the ACPI core to skip this resource */
1512 return 1;
1513}
1514
1515static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1516 void *data, void **return_value)
1517{
1518 struct spi_master *master = data;
1519 struct list_head resource_list;
1520 struct acpi_device *adev;
1521 struct spi_device *spi;
1522 int ret;
1523
1524 if (acpi_bus_get_device(handle, &adev))
1525 return AE_OK;
1526 if (acpi_bus_get_status(adev) || !adev->status.present)
1527 return AE_OK;
1528
1529 spi = spi_alloc_device(master);
1530 if (!spi) {
1531 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1532 dev_name(&adev->dev));
1533 return AE_NO_MEMORY;
1534 }
1535
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001536 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001537 spi->irq = -1;
1538
1539 INIT_LIST_HEAD(&resource_list);
1540 ret = acpi_dev_get_resources(adev, &resource_list,
1541 acpi_spi_add_resource, spi);
1542 acpi_dev_free_resource_list(&resource_list);
1543
1544 if (ret < 0 || !spi->max_speed_hz) {
1545 spi_dev_put(spi);
1546 return AE_OK;
1547 }
1548
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001549 adev->power.flags.ignore_parent = true;
Jarkko Nikulacf9eb392013-10-10 17:19:17 +03001550 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001551 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001552 adev->power.flags.ignore_parent = false;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001553 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1554 dev_name(&adev->dev));
1555 spi_dev_put(spi);
1556 }
1557
1558 return AE_OK;
1559}
1560
1561static void acpi_register_spi_devices(struct spi_master *master)
1562{
1563 acpi_status status;
1564 acpi_handle handle;
1565
Rafael J. Wysocki29896172013-04-01 00:21:08 +00001566 handle = ACPI_HANDLE(master->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001567 if (!handle)
1568 return;
1569
1570 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1571 acpi_spi_add_device, NULL,
1572 master, NULL);
1573 if (ACPI_FAILURE(status))
1574 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1575}
1576#else
1577static inline void acpi_register_spi_devices(struct spi_master *master) {}
1578#endif /* CONFIG_ACPI */
1579
Tony Jones49dce682007-10-16 01:27:48 -07001580static void spi_master_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001581{
1582 struct spi_master *master;
1583
Tony Jones49dce682007-10-16 01:27:48 -07001584 master = container_of(dev, struct spi_master, dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001585 kfree(master);
1586}
1587
1588static struct class spi_master_class = {
1589 .name = "spi_master",
1590 .owner = THIS_MODULE,
Tony Jones49dce682007-10-16 01:27:48 -07001591 .dev_release = spi_master_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00001592 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08001593};
1594
1595
1596/**
1597 * spi_alloc_master - allocate SPI master controller
1598 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001599 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001600 * memory is in the driver_data field of the returned device,
David Brownell0c868462006-01-08 13:34:25 -08001601 * accessible with spi_master_get_devdata().
David Brownell33e34dc2007-05-08 00:32:21 -07001602 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001603 *
1604 * This call is used only by SPI master controller drivers, which are the
1605 * only ones directly touching chip registers. It's how they allocate
dmitry pervushinba1a0512006-05-20 15:00:14 -07001606 * an spi_master structure, prior to calling spi_register_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001607 *
1608 * This must be called from context that can sleep. It returns the SPI
1609 * master structure on success, else NULL.
1610 *
1611 * The caller is responsible for assigning the bus number and initializing
dmitry pervushinba1a0512006-05-20 15:00:14 -07001612 * the master's methods before calling spi_register_master(); and (after errors
Uwe Kleine-Königeb4af0f2012-02-23 10:40:14 +01001613 * adding the device) calling spi_master_put() and kfree() to prevent a memory
1614 * leak.
David Brownell8ae12a02006-01-08 13:34:19 -08001615 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001616struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
David Brownell8ae12a02006-01-08 13:34:19 -08001617{
1618 struct spi_master *master;
1619
David Brownell0c868462006-01-08 13:34:25 -08001620 if (!dev)
1621 return NULL;
1622
Jingoo Han5fe5f052013-10-14 10:31:51 +09001623 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -08001624 if (!master)
1625 return NULL;
1626
Tony Jones49dce682007-10-16 01:27:48 -07001627 device_initialize(&master->dev);
Grant Likely1e8a52e2012-05-19 23:42:08 -06001628 master->bus_num = -1;
1629 master->num_chipselect = 1;
Tony Jones49dce682007-10-16 01:27:48 -07001630 master->dev.class = &spi_master_class;
1631 master->dev.parent = get_device(dev);
David Brownell0c868462006-01-08 13:34:25 -08001632 spi_master_set_devdata(master, &master[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001633
1634 return master;
1635}
1636EXPORT_SYMBOL_GPL(spi_alloc_master);
1637
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001638#ifdef CONFIG_OF
1639static int of_spi_register_master(struct spi_master *master)
1640{
Grant Likelye80beb22013-02-12 17:48:37 +00001641 int nb, i, *cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001642 struct device_node *np = master->dev.of_node;
1643
1644 if (!np)
1645 return 0;
1646
1647 nb = of_gpio_named_count(np, "cs-gpios");
Jingoo Han5fe5f052013-10-14 10:31:51 +09001648 master->num_chipselect = max_t(int, nb, master->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001649
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001650 /* Return error only for an incorrectly formed cs-gpios property */
1651 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001652 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001653 else if (nb < 0)
1654 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001655
1656 cs = devm_kzalloc(&master->dev,
1657 sizeof(int) * master->num_chipselect,
1658 GFP_KERNEL);
1659 master->cs_gpios = cs;
1660
1661 if (!master->cs_gpios)
1662 return -ENOMEM;
1663
Andreas Larsson0da83bb2013-01-29 15:53:40 +01001664 for (i = 0; i < master->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01001665 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001666
1667 for (i = 0; i < nb; i++)
1668 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1669
1670 return 0;
1671}
1672#else
1673static int of_spi_register_master(struct spi_master *master)
1674{
1675 return 0;
1676}
1677#endif
1678
David Brownell8ae12a02006-01-08 13:34:19 -08001679/**
1680 * spi_register_master - register SPI master controller
1681 * @master: initialized master, originally from spi_alloc_master()
David Brownell33e34dc2007-05-08 00:32:21 -07001682 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001683 *
1684 * SPI master controllers connect to their drivers using some non-SPI bus,
1685 * such as the platform bus. The final stage of probe() in that code
1686 * includes calling spi_register_master() to hook up to this SPI bus glue.
1687 *
1688 * SPI controllers use board specific (often SOC specific) bus numbers,
1689 * and board-specific addressing for SPI devices combines those numbers
1690 * with chip select numbers. Since SPI does not directly support dynamic
1691 * device identification, boards need configuration tables telling which
1692 * chip is at which address.
1693 *
1694 * This must be called from context that can sleep. It returns zero on
1695 * success, else a negative error code (dropping the master's refcount).
David Brownell0c868462006-01-08 13:34:25 -08001696 * After a successful return, the caller is responsible for calling
1697 * spi_unregister_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001698 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001699int spi_register_master(struct spi_master *master)
David Brownell8ae12a02006-01-08 13:34:19 -08001700{
David Brownelle44a45a2007-06-03 13:50:40 -07001701 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Tony Jones49dce682007-10-16 01:27:48 -07001702 struct device *dev = master->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08001703 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08001704 int status = -ENODEV;
1705 int dynamic = 0;
1706
David Brownell0c868462006-01-08 13:34:25 -08001707 if (!dev)
1708 return -ENODEV;
1709
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001710 status = of_spi_register_master(master);
1711 if (status)
1712 return status;
1713
David Brownell082c8cb2007-07-31 00:39:45 -07001714 /* even if it's just one always-selected device, there must
1715 * be at least one chipselect
1716 */
1717 if (master->num_chipselect == 0)
1718 return -EINVAL;
1719
Grant Likelybb297852012-12-21 19:32:09 +00001720 if ((master->bus_num < 0) && master->dev.of_node)
1721 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1722
David Brownell8ae12a02006-01-08 13:34:19 -08001723 /* convention: dynamically assigned bus IDs count down from the max */
David Brownella020ed72006-04-03 15:49:04 -07001724 if (master->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07001725 /* FIXME switch to an IDR based scheme, something like
1726 * I2C now uses, so we can't run out of "dynamic" IDs
1727 */
David Brownell8ae12a02006-01-08 13:34:19 -08001728 master->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08001729 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08001730 }
1731
Mark Brown5424d432014-12-10 17:40:53 +00001732 INIT_LIST_HEAD(&master->queue);
1733 spin_lock_init(&master->queue_lock);
Ernst Schwabcf32b712010-06-28 17:49:29 -07001734 spin_lock_init(&master->bus_lock_spinlock);
1735 mutex_init(&master->bus_lock_mutex);
1736 master->bus_lock_flag = 0;
Mark Brownb1589352013-10-05 11:50:40 +01001737 init_completion(&master->xfer_completion);
Mark Brown6ad45a22014-02-02 13:47:47 +00001738 if (!master->max_dma_len)
1739 master->max_dma_len = INT_MAX;
Ernst Schwabcf32b712010-06-28 17:49:29 -07001740
David Brownell8ae12a02006-01-08 13:34:19 -08001741 /* register the device, then userspace will see it.
1742 * registration fails if the bus ID is in use.
1743 */
Kay Sievers35f74fc2009-01-06 10:44:37 -08001744 dev_set_name(&master->dev, "spi%u", master->bus_num);
Tony Jones49dce682007-10-16 01:27:48 -07001745 status = device_add(&master->dev);
David Brownellb8852442006-01-08 13:34:23 -08001746 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08001747 goto done;
Kay Sievers35f74fc2009-01-06 10:44:37 -08001748 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
David Brownell8ae12a02006-01-08 13:34:19 -08001749 dynamic ? " (dynamic)" : "");
1750
Linus Walleijffbbdd212012-02-22 10:05:38 +01001751 /* If we're using a queued driver, start the queue */
1752 if (master->transfer)
1753 dev_info(dev, "master is unqueued, this is deprecated\n");
1754 else {
1755 status = spi_master_initialize_queue(master);
1756 if (status) {
Axel Line93b0722013-08-31 20:25:52 +08001757 device_del(&master->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001758 goto done;
1759 }
1760 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00001761 /* add statistics */
1762 spin_lock_init(&master->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001763
Feng Tang2b9603a2010-08-02 15:52:15 +08001764 mutex_lock(&board_lock);
1765 list_add_tail(&master->list, &spi_master_list);
1766 list_for_each_entry(bi, &board_list, list)
1767 spi_match_master_to_boardinfo(master, &bi->board_info);
1768 mutex_unlock(&board_lock);
1769
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001770 /* Register devices from the device tree and ACPI */
Anatolij Gustschin12b15e82010-07-27 22:35:58 +02001771 of_register_spi_devices(master);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001772 acpi_register_spi_devices(master);
David Brownell8ae12a02006-01-08 13:34:19 -08001773done:
1774 return status;
1775}
1776EXPORT_SYMBOL_GPL(spi_register_master);
1777
Mark Brown666d5b42013-08-31 18:50:52 +01001778static void devm_spi_unregister(struct device *dev, void *res)
1779{
1780 spi_unregister_master(*(struct spi_master **)res);
1781}
1782
1783/**
1784 * dev_spi_register_master - register managed SPI master controller
1785 * @dev: device managing SPI master
1786 * @master: initialized master, originally from spi_alloc_master()
1787 * Context: can sleep
1788 *
1789 * Register a SPI device as with spi_register_master() which will
1790 * automatically be unregister
1791 */
1792int devm_spi_register_master(struct device *dev, struct spi_master *master)
1793{
1794 struct spi_master **ptr;
1795 int ret;
1796
1797 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1798 if (!ptr)
1799 return -ENOMEM;
1800
1801 ret = spi_register_master(master);
Stephen Warren4b928942013-11-21 16:11:15 -07001802 if (!ret) {
Mark Brown666d5b42013-08-31 18:50:52 +01001803 *ptr = master;
1804 devres_add(dev, ptr);
1805 } else {
1806 devres_free(ptr);
1807 }
1808
1809 return ret;
1810}
1811EXPORT_SYMBOL_GPL(devm_spi_register_master);
1812
David Lamparter34860082010-08-30 23:54:17 +02001813static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08001814{
David Lamparter34860082010-08-30 23:54:17 +02001815 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08001816 return 0;
1817}
1818
1819/**
1820 * spi_unregister_master - unregister SPI master controller
1821 * @master: the master being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07001822 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001823 *
1824 * This call is used only by SPI master controller drivers, which are the
1825 * only ones directly touching chip registers.
1826 *
1827 * This must be called from context that can sleep.
1828 */
1829void spi_unregister_master(struct spi_master *master)
1830{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08001831 int dummy;
1832
Linus Walleijffbbdd212012-02-22 10:05:38 +01001833 if (master->queued) {
1834 if (spi_destroy_queue(master))
1835 dev_err(&master->dev, "queue remove failed\n");
1836 }
1837
Feng Tang2b9603a2010-08-02 15:52:15 +08001838 mutex_lock(&board_lock);
1839 list_del(&master->list);
1840 mutex_unlock(&board_lock);
1841
Sebastian Andrzej Siewior97dbf372010-12-21 17:24:31 -08001842 dummy = device_for_each_child(&master->dev, NULL, __unregister);
Tony Jones49dce682007-10-16 01:27:48 -07001843 device_unregister(&master->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001844}
1845EXPORT_SYMBOL_GPL(spi_unregister_master);
1846
Linus Walleijffbbdd212012-02-22 10:05:38 +01001847int spi_master_suspend(struct spi_master *master)
1848{
1849 int ret;
1850
1851 /* Basically no-ops for non-queued masters */
1852 if (!master->queued)
1853 return 0;
1854
1855 ret = spi_stop_queue(master);
1856 if (ret)
1857 dev_err(&master->dev, "queue stop failed\n");
1858
1859 return ret;
1860}
1861EXPORT_SYMBOL_GPL(spi_master_suspend);
1862
1863int spi_master_resume(struct spi_master *master)
1864{
1865 int ret;
1866
1867 if (!master->queued)
1868 return 0;
1869
1870 ret = spi_start_queue(master);
1871 if (ret)
1872 dev_err(&master->dev, "queue restart failed\n");
1873
1874 return ret;
1875}
1876EXPORT_SYMBOL_GPL(spi_master_resume);
1877
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001878static int __spi_master_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08001879{
1880 struct spi_master *m;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001881 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08001882
1883 m = container_of(dev, struct spi_master, dev);
1884 return m->bus_num == *bus_num;
1885}
1886
David Brownell8ae12a02006-01-08 13:34:19 -08001887/**
1888 * spi_busnum_to_master - look up master associated with bus_num
1889 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07001890 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001891 *
1892 * This call may be used with devices that are registered after
1893 * arch init time. It returns a refcounted pointer to the relevant
1894 * spi_master (which the caller must release), or NULL if there is
1895 * no such master registered.
1896 */
1897struct spi_master *spi_busnum_to_master(u16 bus_num)
1898{
Tony Jones49dce682007-10-16 01:27:48 -07001899 struct device *dev;
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001900 struct spi_master *master = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08001901
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001902 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Dave Young5ed2c832008-01-22 15:14:18 +08001903 __spi_master_match);
1904 if (dev)
1905 master = container_of(dev, struct spi_master, dev);
1906 /* reference got in class_find_device */
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001907 return master;
David Brownell8ae12a02006-01-08 13:34:19 -08001908}
1909EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1910
1911
1912/*-------------------------------------------------------------------------*/
1913
David Brownell7d077192009-06-17 16:26:03 -07001914/* Core methods for SPI master protocol drivers. Some of the
1915 * other core methods are currently defined as inline functions.
1916 */
1917
Stefan Brüns63ab6452015-08-23 16:06:30 +02001918static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
1919{
1920 if (master->bits_per_word_mask) {
1921 /* Only 32 bits fit in the mask */
1922 if (bits_per_word > 32)
1923 return -EINVAL;
1924 if (!(master->bits_per_word_mask &
1925 SPI_BPW_MASK(bits_per_word)))
1926 return -EINVAL;
1927 }
1928
1929 return 0;
1930}
1931
David Brownell7d077192009-06-17 16:26:03 -07001932/**
1933 * spi_setup - setup SPI mode and clock rate
1934 * @spi: the device whose settings are being modified
1935 * Context: can sleep, and no requests are queued to the device
1936 *
1937 * SPI protocol drivers may need to update the transfer mode if the
1938 * device doesn't work with its default. They may likewise need
1939 * to update clock rates or word sizes from initial values. This function
1940 * changes those settings, and must be called from a context that can sleep.
1941 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
1942 * effect the next time the device is selected and data is transferred to
1943 * or from it. When this function returns, the spi device is deselected.
1944 *
1945 * Note that this call will fail if the protocol driver specifies an option
1946 * that the underlying controller or its driver does not support. For
1947 * example, not all hardware supports wire transfers using nine bit words,
1948 * LSB-first wire encoding, or active-high chipselects.
1949 */
1950int spi_setup(struct spi_device *spi)
1951{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02001952 unsigned bad_bits, ugly_bits;
Laxman Dewangancaae0702012-11-09 14:35:22 +05301953 int status = 0;
David Brownell7d077192009-06-17 16:26:03 -07001954
wangyuhangf477b7f2013-08-11 18:15:17 +08001955 /* check mode to prevent that DUAL and QUAD set at the same time
1956 */
1957 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1958 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1959 dev_err(&spi->dev,
1960 "setup: can not select dual and quad at the same time\n");
1961 return -EINVAL;
1962 }
1963 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1964 */
1965 if ((spi->mode & SPI_3WIRE) && (spi->mode &
1966 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1967 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07001968 /* help drivers fail *cleanly* when they need options
1969 * that aren't supported with their current master
1970 */
1971 bad_bits = spi->mode & ~spi->master->mode_bits;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02001972 ugly_bits = bad_bits &
1973 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
1974 if (ugly_bits) {
1975 dev_warn(&spi->dev,
1976 "setup: ignoring unsupported mode bits %x\n",
1977 ugly_bits);
1978 spi->mode &= ~ugly_bits;
1979 bad_bits &= ~ugly_bits;
1980 }
David Brownelle7db06b2009-06-17 16:26:04 -07001981 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02001982 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07001983 bad_bits);
1984 return -EINVAL;
1985 }
1986
David Brownell7d077192009-06-17 16:26:03 -07001987 if (!spi->bits_per_word)
1988 spi->bits_per_word = 8;
1989
Stefan Brüns63ab6452015-08-23 16:06:30 +02001990 if (__spi_validate_bits_per_word(spi->master, spi->bits_per_word))
1991 return -EINVAL;
1992
Axel Lin052eb2d42014-02-10 00:08:05 +08001993 if (!spi->max_speed_hz)
1994 spi->max_speed_hz = spi->master->max_speed_hz;
1995
Ivan T. Ivanov1a7b7ee2015-03-13 18:43:49 +02001996 spi_set_cs(spi, false);
1997
Laxman Dewangancaae0702012-11-09 14:35:22 +05301998 if (spi->master->setup)
1999 status = spi->master->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07002000
Jingoo Han5fe5f052013-10-14 10:31:51 +09002001 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 -07002002 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2003 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2004 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2005 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2006 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2007 spi->bits_per_word, spi->max_speed_hz,
2008 status);
2009
2010 return status;
2011}
2012EXPORT_SYMBOL_GPL(spi_setup);
2013
Mark Brown90808732013-11-13 23:44:15 +00002014static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b712010-06-28 17:49:29 -07002015{
2016 struct spi_master *master = spi->master;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302017 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002018 int w_size;
Ernst Schwabcf32b712010-06-28 17:49:29 -07002019
Mark Brown24a00132013-07-10 15:05:40 +01002020 if (list_empty(&message->transfers))
2021 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01002022
Ernst Schwabcf32b712010-06-28 17:49:29 -07002023 /* Half-duplex links include original MicroWire, and ones with
2024 * only one data pin like SPI_3WIRE (switches direction) or where
2025 * either MOSI or MISO is missing. They can also be caused by
2026 * software limitations.
2027 */
2028 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
2029 || (spi->mode & SPI_3WIRE)) {
Ernst Schwabcf32b712010-06-28 17:49:29 -07002030 unsigned flags = master->flags;
2031
2032 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2033 if (xfer->rx_buf && xfer->tx_buf)
2034 return -EINVAL;
2035 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
2036 return -EINVAL;
2037 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
2038 return -EINVAL;
2039 }
2040 }
2041
Laxman Dewangane6811d12012-11-09 14:36:45 +05302042 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302043 * Set transfer bits_per_word and max speed as spi device default if
2044 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08002045 * Set transfer tx_nbits and rx_nbits as single transfer default
2046 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05302047 */
2048 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05302049 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302050 if (!xfer->bits_per_word)
2051 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08002052
2053 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302054 xfer->speed_hz = spi->max_speed_hz;
Mark Brown7dc9fbc2015-08-20 11:52:18 -07002055 if (!xfer->speed_hz)
2056 xfer->speed_hz = master->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08002057
2058 if (master->max_speed_hz &&
2059 xfer->speed_hz > master->max_speed_hz)
2060 xfer->speed_hz = master->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02002061
Stefan Brüns63ab6452015-08-23 16:06:30 +02002062 if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
2063 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01002064
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002065 /*
2066 * SPI transfer length should be multiple of SPI word size
2067 * where SPI word size should be power-of-two multiple
2068 */
2069 if (xfer->bits_per_word <= 8)
2070 w_size = 1;
2071 else if (xfer->bits_per_word <= 16)
2072 w_size = 2;
2073 else
2074 w_size = 4;
2075
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002076 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002077 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002078 return -EINVAL;
2079
Mark Browna2fd4f92013-07-10 14:57:26 +01002080 if (xfer->speed_hz && master->min_speed_hz &&
2081 xfer->speed_hz < master->min_speed_hz)
2082 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08002083
2084 if (xfer->tx_buf && !xfer->tx_nbits)
2085 xfer->tx_nbits = SPI_NBITS_SINGLE;
2086 if (xfer->rx_buf && !xfer->rx_nbits)
2087 xfer->rx_nbits = SPI_NBITS_SINGLE;
2088 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01002089 * 1. check the value matches one of single, dual and quad
2090 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08002091 */
Sourav Poddardb90a442013-08-22 21:20:48 +05302092 if (xfer->tx_buf) {
2093 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2094 xfer->tx_nbits != SPI_NBITS_DUAL &&
2095 xfer->tx_nbits != SPI_NBITS_QUAD)
2096 return -EINVAL;
2097 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2098 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2099 return -EINVAL;
2100 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2101 !(spi->mode & SPI_TX_QUAD))
2102 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302103 }
wangyuhangf477b7f2013-08-11 18:15:17 +08002104 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05302105 if (xfer->rx_buf) {
2106 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2107 xfer->rx_nbits != SPI_NBITS_DUAL &&
2108 xfer->rx_nbits != SPI_NBITS_QUAD)
2109 return -EINVAL;
2110 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2111 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2112 return -EINVAL;
2113 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2114 !(spi->mode & SPI_RX_QUAD))
2115 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302116 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05302117 }
2118
Ernst Schwabcf32b712010-06-28 17:49:29 -07002119 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00002120
2121 return 0;
2122}
2123
2124static int __spi_async(struct spi_device *spi, struct spi_message *message)
2125{
2126 struct spi_master *master = spi->master;
2127
2128 message->spi = spi;
2129
Martin Sperleca2ebc2015-06-22 13:00:36 +00002130 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
2131 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2132
Mark Brown90808732013-11-13 23:44:15 +00002133 trace_spi_message_submit(message);
2134
Ernst Schwabcf32b712010-06-28 17:49:29 -07002135 return master->transfer(spi, message);
2136}
2137
David Brownell568d0692009-09-22 16:46:18 -07002138/**
2139 * spi_async - asynchronous SPI transfer
2140 * @spi: device with which data will be exchanged
2141 * @message: describes the data transfers, including completion callback
2142 * Context: any (irqs may be blocked, etc)
2143 *
2144 * This call may be used in_irq and other contexts which can't sleep,
2145 * as well as from task contexts which can sleep.
2146 *
2147 * The completion callback is invoked in a context which can't sleep.
2148 * Before that invocation, the value of message->status is undefined.
2149 * When the callback is issued, message->status holds either zero (to
2150 * indicate complete success) or a negative error code. After that
2151 * callback returns, the driver which issued the transfer request may
2152 * deallocate the associated memory; it's no longer in use by any SPI
2153 * core or controller driver code.
2154 *
2155 * Note that although all messages to a spi_device are handled in
2156 * FIFO order, messages may go to different devices in other orders.
2157 * Some device might be higher priority, or have various "hard" access
2158 * time requirements, for example.
2159 *
2160 * On detection of any fault during the transfer, processing of
2161 * the entire message is aborted, and the device is deselected.
2162 * Until returning from the associated message completion callback,
2163 * no other spi_message queued to that device will be processed.
2164 * (This rule applies equally to all the synchronous transfer calls,
2165 * which are wrappers around this core asynchronous primitive.)
2166 */
2167int spi_async(struct spi_device *spi, struct spi_message *message)
2168{
2169 struct spi_master *master = spi->master;
Ernst Schwabcf32b712010-06-28 17:49:29 -07002170 int ret;
2171 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07002172
Mark Brown90808732013-11-13 23:44:15 +00002173 ret = __spi_validate(spi, message);
2174 if (ret != 0)
2175 return ret;
2176
Ernst Schwabcf32b712010-06-28 17:49:29 -07002177 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07002178
Ernst Schwabcf32b712010-06-28 17:49:29 -07002179 if (master->bus_lock_flag)
2180 ret = -EBUSY;
2181 else
2182 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07002183
Ernst Schwabcf32b712010-06-28 17:49:29 -07002184 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2185
2186 return ret;
David Brownell568d0692009-09-22 16:46:18 -07002187}
2188EXPORT_SYMBOL_GPL(spi_async);
2189
Ernst Schwabcf32b712010-06-28 17:49:29 -07002190/**
2191 * spi_async_locked - version of spi_async with exclusive bus usage
2192 * @spi: device with which data will be exchanged
2193 * @message: describes the data transfers, including completion callback
2194 * Context: any (irqs may be blocked, etc)
2195 *
2196 * This call may be used in_irq and other contexts which can't sleep,
2197 * as well as from task contexts which can sleep.
2198 *
2199 * The completion callback is invoked in a context which can't sleep.
2200 * Before that invocation, the value of message->status is undefined.
2201 * When the callback is issued, message->status holds either zero (to
2202 * indicate complete success) or a negative error code. After that
2203 * callback returns, the driver which issued the transfer request may
2204 * deallocate the associated memory; it's no longer in use by any SPI
2205 * core or controller driver code.
2206 *
2207 * Note that although all messages to a spi_device are handled in
2208 * FIFO order, messages may go to different devices in other orders.
2209 * Some device might be higher priority, or have various "hard" access
2210 * time requirements, for example.
2211 *
2212 * On detection of any fault during the transfer, processing of
2213 * the entire message is aborted, and the device is deselected.
2214 * Until returning from the associated message completion callback,
2215 * no other spi_message queued to that device will be processed.
2216 * (This rule applies equally to all the synchronous transfer calls,
2217 * which are wrappers around this core asynchronous primitive.)
2218 */
2219int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2220{
2221 struct spi_master *master = spi->master;
2222 int ret;
2223 unsigned long flags;
2224
Mark Brown90808732013-11-13 23:44:15 +00002225 ret = __spi_validate(spi, message);
2226 if (ret != 0)
2227 return ret;
2228
Ernst Schwabcf32b712010-06-28 17:49:29 -07002229 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2230
2231 ret = __spi_async(spi, message);
2232
2233 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2234
2235 return ret;
2236
2237}
2238EXPORT_SYMBOL_GPL(spi_async_locked);
2239
David Brownell7d077192009-06-17 16:26:03 -07002240
2241/*-------------------------------------------------------------------------*/
2242
2243/* Utility methods for SPI master protocol drivers, layered on
2244 * top of the core. Some other utility methods are defined as
2245 * inline functions.
2246 */
2247
Andrew Morton5d870c82006-01-11 11:23:49 -08002248static void spi_complete(void *arg)
2249{
2250 complete(arg);
2251}
2252
Ernst Schwabcf32b712010-06-28 17:49:29 -07002253static int __spi_sync(struct spi_device *spi, struct spi_message *message,
2254 int bus_locked)
2255{
2256 DECLARE_COMPLETION_ONSTACK(done);
2257 int status;
2258 struct spi_master *master = spi->master;
Mark Brown0461a412014-12-09 21:38:05 +00002259 unsigned long flags;
2260
2261 status = __spi_validate(spi, message);
2262 if (status != 0)
2263 return status;
Ernst Schwabcf32b712010-06-28 17:49:29 -07002264
2265 message->complete = spi_complete;
2266 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00002267 message->spi = spi;
Ernst Schwabcf32b712010-06-28 17:49:29 -07002268
Martin Sperleca2ebc2015-06-22 13:00:36 +00002269 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
2270 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
2271
Ernst Schwabcf32b712010-06-28 17:49:29 -07002272 if (!bus_locked)
2273 mutex_lock(&master->bus_lock_mutex);
2274
Mark Brown0461a412014-12-09 21:38:05 +00002275 /* If we're not using the legacy transfer method then we will
2276 * try to transfer in the calling context so special case.
2277 * This code would be less tricky if we could remove the
2278 * support for driver implemented message queues.
2279 */
2280 if (master->transfer == spi_queued_transfer) {
2281 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2282
2283 trace_spi_message_submit(message);
2284
2285 status = __spi_queued_transfer(spi, message, false);
2286
2287 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2288 } else {
2289 status = spi_async_locked(spi, message);
2290 }
Ernst Schwabcf32b712010-06-28 17:49:29 -07002291
2292 if (!bus_locked)
2293 mutex_unlock(&master->bus_lock_mutex);
2294
2295 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00002296 /* Push out the messages in the calling context if we
2297 * can.
2298 */
Martin Sperleca2ebc2015-06-22 13:00:36 +00002299 if (master->transfer == spi_queued_transfer) {
2300 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2301 spi_sync_immediate);
2302 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
2303 spi_sync_immediate);
Mark Brownfc9e0f72014-12-10 13:46:33 +00002304 __spi_pump_messages(master, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00002305 }
Mark Brown0461a412014-12-09 21:38:05 +00002306
Ernst Schwabcf32b712010-06-28 17:49:29 -07002307 wait_for_completion(&done);
2308 status = message->status;
2309 }
2310 message->context = NULL;
2311 return status;
2312}
2313
David Brownell8ae12a02006-01-08 13:34:19 -08002314/**
2315 * spi_sync - blocking/synchronous SPI data transfers
2316 * @spi: device with which data will be exchanged
2317 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07002318 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002319 *
2320 * This call may only be used from a context that may sleep. The sleep
2321 * is non-interruptible, and has no timeout. Low-overhead controller
2322 * drivers may DMA directly into and out of the message buffers.
2323 *
2324 * Note that the SPI device's chip select is active during the message,
2325 * and then is normally disabled between messages. Drivers for some
2326 * frequently-used devices may want to minimize costs of selecting a chip,
2327 * by leaving it selected in anticipation that the next message will go
2328 * to the same chip. (That may increase power usage.)
2329 *
David Brownell0c868462006-01-08 13:34:25 -08002330 * Also, the caller is guaranteeing that the memory associated with the
2331 * message will not be freed before this call returns.
2332 *
Marc Pignat9b938b72007-12-04 23:45:10 -08002333 * It returns zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002334 */
2335int spi_sync(struct spi_device *spi, struct spi_message *message)
2336{
Ernst Schwabcf32b712010-06-28 17:49:29 -07002337 return __spi_sync(spi, message, 0);
David Brownell8ae12a02006-01-08 13:34:19 -08002338}
2339EXPORT_SYMBOL_GPL(spi_sync);
2340
Ernst Schwabcf32b712010-06-28 17:49:29 -07002341/**
2342 * spi_sync_locked - version of spi_sync with exclusive bus usage
2343 * @spi: device with which data will be exchanged
2344 * @message: describes the data transfers
2345 * Context: can sleep
2346 *
2347 * This call may only be used from a context that may sleep. The sleep
2348 * is non-interruptible, and has no timeout. Low-overhead controller
2349 * drivers may DMA directly into and out of the message buffers.
2350 *
2351 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002352 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b712010-06-28 17:49:29 -07002353 * be released by a spi_bus_unlock call when the exclusive access is over.
2354 *
2355 * It returns zero on success, else a negative error code.
2356 */
2357int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2358{
2359 return __spi_sync(spi, message, 1);
2360}
2361EXPORT_SYMBOL_GPL(spi_sync_locked);
2362
2363/**
2364 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
2365 * @master: SPI bus master that should be locked for exclusive bus access
2366 * Context: can sleep
2367 *
2368 * This call may only be used from a context that may sleep. The sleep
2369 * is non-interruptible, and has no timeout.
2370 *
2371 * This call should be used by drivers that require exclusive access to the
2372 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
2373 * exclusive access is over. Data transfer must be done by spi_sync_locked
2374 * and spi_async_locked calls when the SPI bus lock is held.
2375 *
2376 * It returns zero on success, else a negative error code.
2377 */
2378int spi_bus_lock(struct spi_master *master)
2379{
2380 unsigned long flags;
2381
2382 mutex_lock(&master->bus_lock_mutex);
2383
2384 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2385 master->bus_lock_flag = 1;
2386 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2387
2388 /* mutex remains locked until spi_bus_unlock is called */
2389
2390 return 0;
2391}
2392EXPORT_SYMBOL_GPL(spi_bus_lock);
2393
2394/**
2395 * spi_bus_unlock - release the lock for exclusive SPI bus usage
2396 * @master: SPI bus master that was locked for exclusive bus access
2397 * Context: can sleep
2398 *
2399 * This call may only be used from a context that may sleep. The sleep
2400 * is non-interruptible, and has no timeout.
2401 *
2402 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
2403 * call.
2404 *
2405 * It returns zero on success, else a negative error code.
2406 */
2407int spi_bus_unlock(struct spi_master *master)
2408{
2409 master->bus_lock_flag = 0;
2410
2411 mutex_unlock(&master->bus_lock_mutex);
2412
2413 return 0;
2414}
2415EXPORT_SYMBOL_GPL(spi_bus_unlock);
2416
David Brownella9948b62006-04-02 10:37:40 -08002417/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09002418#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08002419
2420static u8 *buf;
2421
2422/**
2423 * spi_write_then_read - SPI synchronous write followed by read
2424 * @spi: device with which data will be exchanged
2425 * @txbuf: data to be written (need not be dma-safe)
2426 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07002427 * @rxbuf: buffer into which data will be read (need not be dma-safe)
2428 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07002429 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002430 *
2431 * This performs a half duplex MicroWire style transaction with the
2432 * device, sending txbuf and then reading rxbuf. The return value
2433 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08002434 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08002435 *
David Brownell0c868462006-01-08 13:34:25 -08002436 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07002437 * portable code should never use this for more than 32 bytes.
2438 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c868462006-01-08 13:34:25 -08002439 * spi_{async,sync}() calls with dma-safe buffers.
David Brownell8ae12a02006-01-08 13:34:19 -08002440 */
2441int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02002442 const void *txbuf, unsigned n_tx,
2443 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08002444{
David Brownell068f4072007-12-04 23:45:09 -08002445 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002446
2447 int status;
2448 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07002449 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08002450 u8 *local_buf;
2451
Mark Brownb3a223e2012-12-02 12:54:25 +09002452 /* Use preallocated DMA-safe buffer if we can. We can't avoid
2453 * copying here, (as a pure convenience thing), but we can
2454 * keep heap costs out of the hot path unless someone else is
2455 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08002456 */
Mark Brownb3a223e2012-12-02 12:54:25 +09002457 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08002458 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
2459 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09002460 if (!local_buf)
2461 return -ENOMEM;
2462 } else {
2463 local_buf = buf;
2464 }
David Brownell8ae12a02006-01-08 13:34:19 -08002465
Vitaly Wool8275c642006-01-08 13:34:28 -08002466 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09002467 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07002468 if (n_tx) {
2469 x[0].len = n_tx;
2470 spi_message_add_tail(&x[0], &message);
2471 }
2472 if (n_rx) {
2473 x[1].len = n_rx;
2474 spi_message_add_tail(&x[1], &message);
2475 }
Vitaly Wool8275c642006-01-08 13:34:28 -08002476
David Brownell8ae12a02006-01-08 13:34:19 -08002477 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07002478 x[0].tx_buf = local_buf;
2479 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08002480
2481 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08002482 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08002483 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07002484 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08002485
David Brownellbdff5492009-04-13 14:39:57 -07002486 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08002487 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002488 else
2489 kfree(local_buf);
2490
2491 return status;
2492}
2493EXPORT_SYMBOL_GPL(spi_write_then_read);
2494
2495/*-------------------------------------------------------------------------*/
2496
Pantelis Antoniouce79d542014-10-28 22:36:05 +02002497#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2498static int __spi_of_device_match(struct device *dev, void *data)
2499{
2500 return dev->of_node == data;
2501}
2502
2503/* must call put_device() when done with returned spi_device device */
2504static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
2505{
2506 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
2507 __spi_of_device_match);
2508 return dev ? to_spi_device(dev) : NULL;
2509}
2510
2511static int __spi_of_master_match(struct device *dev, const void *data)
2512{
2513 return dev->of_node == data;
2514}
2515
2516/* the spi masters are not using spi_bus, so we find it with another way */
2517static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
2518{
2519 struct device *dev;
2520
2521 dev = class_find_device(&spi_master_class, NULL, node,
2522 __spi_of_master_match);
2523 if (!dev)
2524 return NULL;
2525
2526 /* reference got in class_find_device */
2527 return container_of(dev, struct spi_master, dev);
2528}
2529
2530static int of_spi_notify(struct notifier_block *nb, unsigned long action,
2531 void *arg)
2532{
2533 struct of_reconfig_data *rd = arg;
2534 struct spi_master *master;
2535 struct spi_device *spi;
2536
2537 switch (of_reconfig_get_state_change(action, arg)) {
2538 case OF_RECONFIG_CHANGE_ADD:
2539 master = of_find_spi_master_by_node(rd->dn->parent);
2540 if (master == NULL)
2541 return NOTIFY_OK; /* not for us */
2542
2543 spi = of_register_spi_device(master, rd->dn);
2544 put_device(&master->dev);
2545
2546 if (IS_ERR(spi)) {
2547 pr_err("%s: failed to create for '%s'\n",
2548 __func__, rd->dn->full_name);
2549 return notifier_from_errno(PTR_ERR(spi));
2550 }
2551 break;
2552
2553 case OF_RECONFIG_CHANGE_REMOVE:
2554 /* find our device by node */
2555 spi = of_find_spi_device_by_node(rd->dn);
2556 if (spi == NULL)
2557 return NOTIFY_OK; /* no? not meant for us */
2558
2559 /* unregister takes one ref away */
2560 spi_unregister_device(spi);
2561
2562 /* and put the reference of the find */
2563 put_device(&spi->dev);
2564 break;
2565 }
2566
2567 return NOTIFY_OK;
2568}
2569
2570static struct notifier_block spi_of_notifier = {
2571 .notifier_call = of_spi_notify,
2572};
2573#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
2574extern struct notifier_block spi_of_notifier;
2575#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
2576
David Brownell8ae12a02006-01-08 13:34:19 -08002577static int __init spi_init(void)
2578{
David Brownellb8852442006-01-08 13:34:23 -08002579 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08002580
Christoph Lametere94b1762006-12-06 20:33:17 -08002581 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08002582 if (!buf) {
2583 status = -ENOMEM;
2584 goto err0;
2585 }
2586
2587 status = bus_register(&spi_bus_type);
2588 if (status < 0)
2589 goto err1;
2590
2591 status = class_register(&spi_master_class);
2592 if (status < 0)
2593 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02002594
Fabio Estevam52677202014-11-26 20:13:57 -02002595 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02002596 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
2597
David Brownell8ae12a02006-01-08 13:34:19 -08002598 return 0;
David Brownellb8852442006-01-08 13:34:23 -08002599
2600err2:
2601 bus_unregister(&spi_bus_type);
2602err1:
2603 kfree(buf);
2604 buf = NULL;
2605err0:
2606 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08002607}
David Brownellb8852442006-01-08 13:34:23 -08002608
David Brownell8ae12a02006-01-08 13:34:19 -08002609/* board_info is normally registered in arch_initcall(),
2610 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08002611 *
2612 * REVISIT only boardinfo really needs static linking. the rest (device and
2613 * driver registration) _could_ be dynamically linked (modular) ... costs
2614 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08002615 */
David Brownell673c0c02008-10-15 22:02:46 -07002616postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08002617