blob: cc83cdd969ceb4304405885f39c83f5fb31ae4a3 [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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
David Brownell8ae12a02006-01-08 13:34:19 -080022#include <linux/kernel.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060023#include <linux/kmod.h>
David Brownell8ae12a02006-01-08 13:34:19 -080024#include <linux/device.h>
25#include <linux/init.h>
26#include <linux/cache.h>
Mark Brown99adef32014-01-16 12:22:43 +000027#include <linux/dma-mapping.h>
28#include <linux/dmaengine.h>
Matthias Kaehlcke94040822007-07-17 04:04:16 -070029#include <linux/mutex.h>
Sinan Akman2b7a32f2010-10-02 21:28:29 -060030#include <linux/of_device.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060031#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020032#include <linux/clk/clk-conf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Anton Vorontsove0626e32009-09-22 16:46:08 -070034#include <linux/mod_devicetable.h>
David Brownell8ae12a02006-01-08 13:34:19 -080035#include <linux/spi/spi.h>
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +010036#include <linux/of_gpio.h>
Mark Brown3ae22e82010-12-25 15:32:27 +010037#include <linux/pm_runtime.h>
Paul Gortmaker025ed132011-07-10 12:57:55 -040038#include <linux/export.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060039#include <linux/sched/rt.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010040#include <linux/delay.h>
41#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010042#include <linux/ioport.h>
43#include <linux/acpi.h>
David Brownell8ae12a02006-01-08 13:34:19 -080044
Mark Brown56ec1972013-10-07 19:33:53 +010045#define CREATE_TRACE_POINTS
46#include <trace/events/spi.h>
47
David Brownell8ae12a02006-01-08 13:34:19 -080048static void spidev_release(struct device *dev)
49{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080050 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080051
52 /* spi masters may cleanup for released devices */
53 if (spi->master->cleanup)
54 spi->master->cleanup(spi);
55
David Brownell0c868462006-01-08 13:34:25 -080056 spi_master_put(spi->master);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000057 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080058}
59
60static ssize_t
61modalias_show(struct device *dev, struct device_attribute *a, char *buf)
62{
63 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080064 int len;
65
66 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
67 if (len != -ENODEV)
68 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080069
Grant Likelyd8e328b2012-05-20 00:08:13 -060070 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080071}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070072static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080073
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070074static struct attribute *spi_dev_attrs[] = {
75 &dev_attr_modalias.attr,
76 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -080077};
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070078ATTRIBUTE_GROUPS(spi_dev);
David Brownell8ae12a02006-01-08 13:34:19 -080079
80/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
81 * and the sysfs version makes coldplug work too.
82 */
83
Anton Vorontsov75368bf2009-09-22 16:46:04 -070084static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
85 const struct spi_device *sdev)
86{
87 while (id->name[0]) {
88 if (!strcmp(sdev->modalias, id->name))
89 return id;
90 id++;
91 }
92 return NULL;
93}
94
95const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
96{
97 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
98
99 return spi_match_id(sdrv->id_table, sdev);
100}
101EXPORT_SYMBOL_GPL(spi_get_device_id);
102
David Brownell8ae12a02006-01-08 13:34:19 -0800103static int spi_match_device(struct device *dev, struct device_driver *drv)
104{
105 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700106 const struct spi_driver *sdrv = to_spi_driver(drv);
107
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600108 /* Attempt an OF style match */
109 if (of_driver_match_device(dev, drv))
110 return 1;
111
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100112 /* Then try ACPI */
113 if (acpi_driver_match_device(dev, drv))
114 return 1;
115
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700116 if (sdrv->id_table)
117 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800118
Kay Sievers35f74fc2009-01-06 10:44:37 -0800119 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800120}
121
Kay Sievers7eff2e72007-08-14 15:15:12 +0200122static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800123{
124 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800125 int rc;
126
127 rc = acpi_device_uevent_modalias(dev, env);
128 if (rc != -ENODEV)
129 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800130
Anton Vorontsove0626e32009-09-22 16:46:08 -0700131 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800132 return 0;
133}
134
Mark Brown3ae22e82010-12-25 15:32:27 +0100135#ifdef CONFIG_PM_SLEEP
136static int spi_legacy_suspend(struct device *dev, pm_message_t message)
David Brownell8ae12a02006-01-08 13:34:19 -0800137{
David Brownell3c724262008-02-06 01:38:10 -0800138 int value = 0;
David Brownellb8852442006-01-08 13:34:23 -0800139 struct spi_driver *drv = to_spi_driver(dev->driver);
David Brownell8ae12a02006-01-08 13:34:19 -0800140
David Brownell8ae12a02006-01-08 13:34:19 -0800141 /* suspend will stop irqs and dma; no more i/o */
David Brownell3c724262008-02-06 01:38:10 -0800142 if (drv) {
143 if (drv->suspend)
144 value = drv->suspend(to_spi_device(dev), message);
145 else
146 dev_dbg(dev, "... can't suspend\n");
147 }
David Brownell8ae12a02006-01-08 13:34:19 -0800148 return value;
149}
150
Mark Brown3ae22e82010-12-25 15:32:27 +0100151static int spi_legacy_resume(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -0800152{
David Brownell3c724262008-02-06 01:38:10 -0800153 int value = 0;
David Brownellb8852442006-01-08 13:34:23 -0800154 struct spi_driver *drv = to_spi_driver(dev->driver);
David Brownell8ae12a02006-01-08 13:34:19 -0800155
David Brownell8ae12a02006-01-08 13:34:19 -0800156 /* resume may restart the i/o queue */
David Brownell3c724262008-02-06 01:38:10 -0800157 if (drv) {
158 if (drv->resume)
159 value = drv->resume(to_spi_device(dev));
160 else
161 dev_dbg(dev, "... can't resume\n");
162 }
David Brownell8ae12a02006-01-08 13:34:19 -0800163 return value;
164}
165
Mark Brown3ae22e82010-12-25 15:32:27 +0100166static int spi_pm_suspend(struct device *dev)
167{
168 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
169
170 if (pm)
171 return pm_generic_suspend(dev);
172 else
173 return spi_legacy_suspend(dev, PMSG_SUSPEND);
174}
175
176static int spi_pm_resume(struct device *dev)
177{
178 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
179
180 if (pm)
181 return pm_generic_resume(dev);
182 else
183 return spi_legacy_resume(dev);
184}
185
186static int spi_pm_freeze(struct device *dev)
187{
188 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
189
190 if (pm)
191 return pm_generic_freeze(dev);
192 else
193 return spi_legacy_suspend(dev, PMSG_FREEZE);
194}
195
196static int spi_pm_thaw(struct device *dev)
197{
198 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
199
200 if (pm)
201 return pm_generic_thaw(dev);
202 else
203 return spi_legacy_resume(dev);
204}
205
206static int spi_pm_poweroff(struct device *dev)
207{
208 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
209
210 if (pm)
211 return pm_generic_poweroff(dev);
212 else
213 return spi_legacy_suspend(dev, PMSG_HIBERNATE);
214}
215
216static int spi_pm_restore(struct device *dev)
217{
218 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
219
220 if (pm)
221 return pm_generic_restore(dev);
222 else
223 return spi_legacy_resume(dev);
224}
David Brownell8ae12a02006-01-08 13:34:19 -0800225#else
Mark Brown3ae22e82010-12-25 15:32:27 +0100226#define spi_pm_suspend NULL
227#define spi_pm_resume NULL
228#define spi_pm_freeze NULL
229#define spi_pm_thaw NULL
230#define spi_pm_poweroff NULL
231#define spi_pm_restore NULL
David Brownell8ae12a02006-01-08 13:34:19 -0800232#endif
233
Mark Brown3ae22e82010-12-25 15:32:27 +0100234static const struct dev_pm_ops spi_pm = {
235 .suspend = spi_pm_suspend,
236 .resume = spi_pm_resume,
237 .freeze = spi_pm_freeze,
238 .thaw = spi_pm_thaw,
239 .poweroff = spi_pm_poweroff,
240 .restore = spi_pm_restore,
241 SET_RUNTIME_PM_OPS(
242 pm_generic_runtime_suspend,
243 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200244 NULL
Mark Brown3ae22e82010-12-25 15:32:27 +0100245 )
246};
247
David Brownell8ae12a02006-01-08 13:34:19 -0800248struct bus_type spi_bus_type = {
249 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700250 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800251 .match = spi_match_device,
252 .uevent = spi_uevent,
Mark Brown3ae22e82010-12-25 15:32:27 +0100253 .pm = &spi_pm,
David Brownell8ae12a02006-01-08 13:34:19 -0800254};
255EXPORT_SYMBOL_GPL(spi_bus_type);
256
David Brownellb8852442006-01-08 13:34:23 -0800257
258static int spi_drv_probe(struct device *dev)
259{
260 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300261 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800262
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200263 ret = of_clk_set_defaults(dev->of_node, false);
264 if (ret)
265 return ret;
266
Jean Delvareaec35f42014-02-13 15:28:41 +0100267 acpi_dev_pm_attach(dev, true);
268 ret = sdrv->probe(to_spi_device(dev));
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300269 if (ret)
Jean Delvareaec35f42014-02-13 15:28:41 +0100270 acpi_dev_pm_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300271
272 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800273}
274
275static int spi_drv_remove(struct device *dev)
276{
277 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300278 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800279
Jean Delvareaec35f42014-02-13 15:28:41 +0100280 ret = sdrv->remove(to_spi_device(dev));
281 acpi_dev_pm_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300282
283 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800284}
285
286static void spi_drv_shutdown(struct device *dev)
287{
288 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
289
290 sdrv->shutdown(to_spi_device(dev));
291}
292
David Brownell33e34dc2007-05-08 00:32:21 -0700293/**
294 * spi_register_driver - register a SPI driver
295 * @sdrv: the driver to register
296 * Context: can sleep
297 */
David Brownellb8852442006-01-08 13:34:23 -0800298int spi_register_driver(struct spi_driver *sdrv)
299{
300 sdrv->driver.bus = &spi_bus_type;
301 if (sdrv->probe)
302 sdrv->driver.probe = spi_drv_probe;
303 if (sdrv->remove)
304 sdrv->driver.remove = spi_drv_remove;
305 if (sdrv->shutdown)
306 sdrv->driver.shutdown = spi_drv_shutdown;
307 return driver_register(&sdrv->driver);
308}
309EXPORT_SYMBOL_GPL(spi_register_driver);
310
David Brownell8ae12a02006-01-08 13:34:19 -0800311/*-------------------------------------------------------------------------*/
312
313/* SPI devices should normally not be created by SPI device drivers; that
314 * would make them board-specific. Similarly with SPI master drivers.
315 * Device registration normally goes into like arch/.../mach.../board-YYY.c
316 * with other readonly (flashable) information about mainboard devices.
317 */
318
319struct boardinfo {
320 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800321 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800322};
323
324static LIST_HEAD(board_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800325static LIST_HEAD(spi_master_list);
326
327/*
328 * Used to protect add/del opertion for board_info list and
329 * spi_master list, and their matching process
330 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700331static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800332
Grant Likelydc87c982008-05-15 16:50:22 -0600333/**
334 * spi_alloc_device - Allocate a new SPI device
335 * @master: Controller to which device is connected
336 * Context: can sleep
337 *
338 * Allows a driver to allocate and initialize a spi_device without
339 * registering it immediately. This allows a driver to directly
340 * fill the spi_device with device parameters before calling
341 * spi_add_device() on it.
342 *
343 * Caller is responsible to call spi_add_device() on the returned
344 * spi_device structure to add it to the SPI master. If the caller
345 * needs to discard the spi_device without adding it, then it should
346 * call spi_dev_put() on it.
347 *
348 * Returns a pointer to the new device, or NULL.
349 */
350struct spi_device *spi_alloc_device(struct spi_master *master)
351{
352 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600353
354 if (!spi_master_get(master))
355 return NULL;
356
Jingoo Han5fe5f052013-10-14 10:31:51 +0900357 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600358 if (!spi) {
Grant Likelydc87c982008-05-15 16:50:22 -0600359 spi_master_put(master);
360 return NULL;
361 }
362
363 spi->master = master;
Laurent Pinchart178db7d2011-12-12 01:15:06 +0100364 spi->dev.parent = &master->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600365 spi->dev.bus = &spi_bus_type;
366 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100367 spi->cs_gpio = -ENOENT;
Grant Likelydc87c982008-05-15 16:50:22 -0600368 device_initialize(&spi->dev);
369 return spi;
370}
371EXPORT_SYMBOL_GPL(spi_alloc_device);
372
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200373static void spi_dev_set_name(struct spi_device *spi)
374{
375 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
376
377 if (adev) {
378 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
379 return;
380 }
381
382 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
383 spi->chip_select);
384}
385
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200386static int spi_dev_check(struct device *dev, void *data)
387{
388 struct spi_device *spi = to_spi_device(dev);
389 struct spi_device *new_spi = data;
390
391 if (spi->master == new_spi->master &&
392 spi->chip_select == new_spi->chip_select)
393 return -EBUSY;
394 return 0;
395}
396
Grant Likelydc87c982008-05-15 16:50:22 -0600397/**
398 * spi_add_device - Add spi_device allocated with spi_alloc_device
399 * @spi: spi_device to register
400 *
401 * Companion function to spi_alloc_device. Devices allocated with
402 * spi_alloc_device can be added onto the spi bus with this function.
403 *
David Brownelle48880e2008-08-15 00:40:44 -0700404 * Returns 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600405 */
406int spi_add_device(struct spi_device *spi)
407{
David Brownelle48880e2008-08-15 00:40:44 -0700408 static DEFINE_MUTEX(spi_add_lock);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100409 struct spi_master *master = spi->master;
410 struct device *dev = master->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600411 int status;
412
413 /* Chipselects are numbered 0..max; validate. */
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100414 if (spi->chip_select >= master->num_chipselect) {
Grant Likelydc87c982008-05-15 16:50:22 -0600415 dev_err(dev, "cs%d >= max %d\n",
416 spi->chip_select,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100417 master->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600418 return -EINVAL;
419 }
420
421 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200422 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700423
424 /* We need to make sure there's no other device with this
425 * chipselect **BEFORE** we call setup(), else we'll trash
426 * its configuration. Lock against concurrent add() calls.
427 */
428 mutex_lock(&spi_add_lock);
429
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200430 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
431 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700432 dev_err(dev, "chipselect %d already in use\n",
433 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700434 goto done;
435 }
436
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100437 if (master->cs_gpios)
438 spi->cs_gpio = master->cs_gpios[spi->chip_select];
439
David Brownelle48880e2008-08-15 00:40:44 -0700440 /* Drivers may modify this initial i/o setup, but will
441 * normally rely on the device being setup. Devices
442 * using SPI_CS_HIGH can't coexist well otherwise...
443 */
David Brownell7d077192009-06-17 16:26:03 -0700444 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600445 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200446 dev_err(dev, "can't setup %s, status %d\n",
447 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700448 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600449 }
450
David Brownelle48880e2008-08-15 00:40:44 -0700451 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600452 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700453 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200454 dev_err(dev, "can't add %s, status %d\n",
455 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700456 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800457 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600458
David Brownelle48880e2008-08-15 00:40:44 -0700459done:
460 mutex_unlock(&spi_add_lock);
461 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600462}
463EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800464
David Brownell33e34dc2007-05-08 00:32:21 -0700465/**
466 * spi_new_device - instantiate one new SPI device
467 * @master: Controller to which device is connected
468 * @chip: Describes the SPI device
469 * Context: can sleep
470 *
471 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800472 * after board init creates the hard-wired devices. Some development
473 * platforms may not be able to use spi_register_board_info though, and
474 * this is exported so that for example a USB or parport based adapter
475 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700476 *
477 * Returns the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800478 */
Adrian Bunke9d5a462007-03-26 21:32:23 -0800479struct spi_device *spi_new_device(struct spi_master *master,
480 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800481{
482 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800483 int status;
484
David Brownell082c8cb2007-07-31 00:39:45 -0700485 /* NOTE: caller did any chip->bus_num checks necessary.
486 *
487 * Also, unless we change the return value convention to use
488 * error-or-pointer (not NULL-or-pointer), troubleshootability
489 * suggests syslogged diagnostics are best here (ugh).
490 */
491
Grant Likelydc87c982008-05-15 16:50:22 -0600492 proxy = spi_alloc_device(master);
493 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800494 return NULL;
495
Grant Likely102eb972008-07-23 21:29:55 -0700496 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
497
David Brownell8ae12a02006-01-08 13:34:19 -0800498 proxy->chip_select = chip->chip_select;
499 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700500 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800501 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700502 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800503 proxy->dev.platform_data = (void *) chip->platform_data;
504 proxy->controller_data = chip->controller_data;
505 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800506
Grant Likelydc87c982008-05-15 16:50:22 -0600507 status = spi_add_device(proxy);
David Brownell8ae12a02006-01-08 13:34:19 -0800508 if (status < 0) {
Grant Likelydc87c982008-05-15 16:50:22 -0600509 spi_dev_put(proxy);
510 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800511 }
512
David Brownell8ae12a02006-01-08 13:34:19 -0800513 return proxy;
514}
515EXPORT_SYMBOL_GPL(spi_new_device);
516
Feng Tang2b9603a2010-08-02 15:52:15 +0800517static void spi_match_master_to_boardinfo(struct spi_master *master,
518 struct spi_board_info *bi)
519{
520 struct spi_device *dev;
521
522 if (master->bus_num != bi->bus_num)
523 return;
524
525 dev = spi_new_device(master, bi);
526 if (!dev)
527 dev_err(master->dev.parent, "can't create new device for %s\n",
528 bi->modalias);
529}
530
David Brownell33e34dc2007-05-08 00:32:21 -0700531/**
532 * spi_register_board_info - register SPI devices for a given board
533 * @info: array of chip descriptors
534 * @n: how many descriptors are provided
535 * Context: can sleep
536 *
David Brownell8ae12a02006-01-08 13:34:19 -0800537 * Board-specific early init code calls this (probably during arch_initcall)
538 * with segments of the SPI device table. Any device nodes are created later,
539 * after the relevant parent SPI controller (bus_num) is defined. We keep
540 * this table of devices forever, so that reloading a controller driver will
541 * not make Linux forget about these hard-wired devices.
542 *
543 * Other code can also call this, e.g. a particular add-on board might provide
544 * SPI devices through its expansion connector, so code initializing that board
545 * would naturally declare its SPI devices.
546 *
547 * The board info passed can safely be __initdata ... but be careful of
548 * any embedded pointers (platform_data, etc), they're copied as-is.
549 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000550int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800551{
Feng Tang2b9603a2010-08-02 15:52:15 +0800552 struct boardinfo *bi;
553 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800554
Xiubo Lic7908a32014-09-24 14:30:29 +0800555 if (!n)
556 return -EINVAL;
557
Feng Tang2b9603a2010-08-02 15:52:15 +0800558 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800559 if (!bi)
560 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800561
Feng Tang2b9603a2010-08-02 15:52:15 +0800562 for (i = 0; i < n; i++, bi++, info++) {
563 struct spi_master *master;
David Brownell8ae12a02006-01-08 13:34:19 -0800564
Feng Tang2b9603a2010-08-02 15:52:15 +0800565 memcpy(&bi->board_info, info, sizeof(*info));
566 mutex_lock(&board_lock);
567 list_add_tail(&bi->list, &board_list);
568 list_for_each_entry(master, &spi_master_list, list)
569 spi_match_master_to_boardinfo(master, &bi->board_info);
570 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800571 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800572
573 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800574}
575
576/*-------------------------------------------------------------------------*/
577
Mark Brownb1589352013-10-05 11:50:40 +0100578static void spi_set_cs(struct spi_device *spi, bool enable)
579{
580 if (spi->mode & SPI_CS_HIGH)
581 enable = !enable;
582
583 if (spi->cs_gpio >= 0)
584 gpio_set_value(spi->cs_gpio, !enable);
585 else if (spi->master->set_cs)
586 spi->master->set_cs(spi, !enable);
587}
588
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200589#ifdef CONFIG_HAS_DMA
Mark Brown6ad45a22014-02-02 13:47:47 +0000590static int spi_map_buf(struct spi_master *master, struct device *dev,
591 struct sg_table *sgt, void *buf, size_t len,
592 enum dma_data_direction dir)
593{
594 const bool vmalloced_buf = is_vmalloc_addr(buf);
595 const int desc_len = vmalloced_buf ? PAGE_SIZE : master->max_dma_len;
596 const int sgs = DIV_ROUND_UP(len, desc_len);
597 struct page *vm_page;
598 void *sg_buf;
599 size_t min;
600 int i, ret;
601
602 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
603 if (ret != 0)
604 return ret;
605
606 for (i = 0; i < sgs; i++) {
607 min = min_t(size_t, len, desc_len);
608
609 if (vmalloced_buf) {
610 vm_page = vmalloc_to_page(buf);
611 if (!vm_page) {
612 sg_free_table(sgt);
613 return -ENOMEM;
614 }
615 sg_buf = page_address(vm_page) +
616 ((size_t)buf & ~PAGE_MASK);
617 } else {
618 sg_buf = buf;
619 }
620
621 sg_set_buf(&sgt->sgl[i], sg_buf, min);
622
623 buf += min;
624 len -= min;
625 }
626
627 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200628 if (!ret)
629 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000630 if (ret < 0) {
631 sg_free_table(sgt);
632 return ret;
633 }
634
635 sgt->nents = ret;
636
637 return 0;
638}
639
640static void spi_unmap_buf(struct spi_master *master, struct device *dev,
641 struct sg_table *sgt, enum dma_data_direction dir)
642{
643 if (sgt->orig_nents) {
644 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
645 sg_free_table(sgt);
646 }
647}
648
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200649static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000650{
Mark Brown99adef32014-01-16 12:22:43 +0000651 struct device *tx_dev, *rx_dev;
652 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000653 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000654
Mark Brown6ad45a22014-02-02 13:47:47 +0000655 if (!master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000656 return 0;
657
Geert Uytterhoeven3fc25422014-07-10 15:29:33 +0200658 tx_dev = master->dma_tx->device->dev;
659 rx_dev = master->dma_rx->device->dev;
Mark Brown99adef32014-01-16 12:22:43 +0000660
661 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
662 if (!master->can_dma(master, msg->spi, xfer))
663 continue;
664
665 if (xfer->tx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000666 ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
667 (void *)xfer->tx_buf, xfer->len,
668 DMA_TO_DEVICE);
669 if (ret != 0)
670 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000671 }
672
673 if (xfer->rx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000674 ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
675 xfer->rx_buf, xfer->len,
676 DMA_FROM_DEVICE);
677 if (ret != 0) {
678 spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
679 DMA_TO_DEVICE);
680 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000681 }
682 }
683 }
684
685 master->cur_msg_mapped = true;
686
687 return 0;
688}
689
690static int spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
691{
692 struct spi_transfer *xfer;
693 struct device *tx_dev, *rx_dev;
694
Mark Brown6ad45a22014-02-02 13:47:47 +0000695 if (!master->cur_msg_mapped || !master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000696 return 0;
697
Geert Uytterhoeven3fc25422014-07-10 15:29:33 +0200698 tx_dev = master->dma_tx->device->dev;
699 rx_dev = master->dma_rx->device->dev;
Mark Brown99adef32014-01-16 12:22:43 +0000700
701 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
702 if (!master->can_dma(master, msg->spi, xfer))
703 continue;
704
Mark Brown6ad45a22014-02-02 13:47:47 +0000705 spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
706 spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000707 }
708
709 return 0;
710}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200711#else /* !CONFIG_HAS_DMA */
712static inline int __spi_map_msg(struct spi_master *master,
713 struct spi_message *msg)
714{
715 return 0;
716}
717
718static inline int spi_unmap_msg(struct spi_master *master,
719 struct spi_message *msg)
720{
721 return 0;
722}
723#endif /* !CONFIG_HAS_DMA */
724
725static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
726{
727 struct spi_transfer *xfer;
728 void *tmp;
729 unsigned int max_tx, max_rx;
730
731 if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
732 max_tx = 0;
733 max_rx = 0;
734
735 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
736 if ((master->flags & SPI_MASTER_MUST_TX) &&
737 !xfer->tx_buf)
738 max_tx = max(xfer->len, max_tx);
739 if ((master->flags & SPI_MASTER_MUST_RX) &&
740 !xfer->rx_buf)
741 max_rx = max(xfer->len, max_rx);
742 }
743
744 if (max_tx) {
745 tmp = krealloc(master->dummy_tx, max_tx,
746 GFP_KERNEL | GFP_DMA);
747 if (!tmp)
748 return -ENOMEM;
749 master->dummy_tx = tmp;
750 memset(tmp, 0, max_tx);
751 }
752
753 if (max_rx) {
754 tmp = krealloc(master->dummy_rx, max_rx,
755 GFP_KERNEL | GFP_DMA);
756 if (!tmp)
757 return -ENOMEM;
758 master->dummy_rx = tmp;
759 }
760
761 if (max_tx || max_rx) {
762 list_for_each_entry(xfer, &msg->transfers,
763 transfer_list) {
764 if (!xfer->tx_buf)
765 xfer->tx_buf = master->dummy_tx;
766 if (!xfer->rx_buf)
767 xfer->rx_buf = master->dummy_rx;
768 }
769 }
770 }
771
772 return __spi_map_msg(master, msg);
773}
Mark Brown99adef32014-01-16 12:22:43 +0000774
Mark Brownb1589352013-10-05 11:50:40 +0100775/*
776 * spi_transfer_one_message - Default implementation of transfer_one_message()
777 *
778 * This is a standard implementation of transfer_one_message() for
779 * drivers which impelment a transfer_one() operation. It provides
780 * standard handling of delays and chip select management.
781 */
782static int spi_transfer_one_message(struct spi_master *master,
783 struct spi_message *msg)
784{
785 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +0100786 bool keep_cs = false;
787 int ret = 0;
Mark Brown16a0ce42014-01-30 22:16:41 +0000788 int ms = 1;
Mark Brownb1589352013-10-05 11:50:40 +0100789
790 spi_set_cs(msg->spi, true);
791
792 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
793 trace_spi_transfer_start(msg, xfer);
794
Mark Brown38ec10f2014-08-16 16:27:41 +0100795 if (xfer->tx_buf || xfer->rx_buf) {
796 reinit_completion(&master->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +0100797
Mark Brown38ec10f2014-08-16 16:27:41 +0100798 ret = master->transfer_one(master, msg->spi, xfer);
799 if (ret < 0) {
800 dev_err(&msg->spi->dev,
801 "SPI transfer failed: %d\n", ret);
802 goto out;
803 }
Mark Brownb1589352013-10-05 11:50:40 +0100804
Mark Brown38ec10f2014-08-16 16:27:41 +0100805 if (ret > 0) {
806 ret = 0;
807 ms = xfer->len * 8 * 1000 / xfer->speed_hz;
808 ms += ms + 100; /* some tolerance */
Mark Brown16a0ce42014-01-30 22:16:41 +0000809
Mark Brown38ec10f2014-08-16 16:27:41 +0100810 ms = wait_for_completion_timeout(&master->xfer_completion,
811 msecs_to_jiffies(ms));
812 }
Mark Brown16a0ce42014-01-30 22:16:41 +0000813
Mark Brown38ec10f2014-08-16 16:27:41 +0100814 if (ms == 0) {
815 dev_err(&msg->spi->dev,
816 "SPI transfer timed out\n");
817 msg->status = -ETIMEDOUT;
818 }
819 } else {
820 if (xfer->len)
821 dev_err(&msg->spi->dev,
822 "Bufferless transfer has length %u\n",
823 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +0800824 }
Mark Brownb1589352013-10-05 11:50:40 +0100825
826 trace_spi_transfer_stop(msg, xfer);
827
828 if (msg->status != -EINPROGRESS)
829 goto out;
830
831 if (xfer->delay_usecs)
832 udelay(xfer->delay_usecs);
833
834 if (xfer->cs_change) {
835 if (list_is_last(&xfer->transfer_list,
836 &msg->transfers)) {
837 keep_cs = true;
838 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +0000839 spi_set_cs(msg->spi, false);
840 udelay(10);
841 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +0100842 }
843 }
844
845 msg->actual_length += xfer->len;
846 }
847
848out:
849 if (ret != 0 || !keep_cs)
850 spi_set_cs(msg->spi, false);
851
852 if (msg->status == -EINPROGRESS)
853 msg->status = ret;
854
855 spi_finalize_current_message(master);
856
857 return ret;
858}
859
860/**
861 * spi_finalize_current_transfer - report completion of a transfer
862 *
863 * Called by SPI drivers using the core transfer_one_message()
864 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +0100865 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +0100866 */
867void spi_finalize_current_transfer(struct spi_master *master)
868{
869 complete(&master->xfer_completion);
870}
871EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
872
Linus Walleijffbbdd212012-02-22 10:05:38 +0100873/**
874 * spi_pump_messages - kthread work function which processes spi message queue
875 * @work: pointer to kthread work struct contained in the master struct
876 *
877 * This function checks if there is any spi message in the queue that
878 * needs processing and if so call out to the driver to initialize hardware
879 * and transfer each message.
880 *
881 */
882static void spi_pump_messages(struct kthread_work *work)
883{
884 struct spi_master *master =
885 container_of(work, struct spi_master, pump_messages);
886 unsigned long flags;
887 bool was_busy = false;
888 int ret;
889
890 /* Lock queue and check for queue work */
891 spin_lock_irqsave(&master->queue_lock, flags);
892 if (list_empty(&master->queue) || !master->running) {
Bryan Freedb0b36b82013-03-13 11:17:40 -0700893 if (!master->busy) {
894 spin_unlock_irqrestore(&master->queue_lock, flags);
895 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +0100896 }
897 master->busy = false;
898 spin_unlock_irqrestore(&master->queue_lock, flags);
Mark Brown3a2eba92014-01-28 20:17:03 +0000899 kfree(master->dummy_rx);
900 master->dummy_rx = NULL;
901 kfree(master->dummy_tx);
902 master->dummy_tx = NULL;
Bryan Freedb0b36b82013-03-13 11:17:40 -0700903 if (master->unprepare_transfer_hardware &&
904 master->unprepare_transfer_hardware(master))
905 dev_err(&master->dev,
906 "failed to unprepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +0100907 if (master->auto_runtime_pm) {
908 pm_runtime_mark_last_busy(master->dev.parent);
909 pm_runtime_put_autosuspend(master->dev.parent);
910 }
Mark Brown56ec1972013-10-07 19:33:53 +0100911 trace_spi_master_idle(master);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100912 return;
913 }
914
915 /* Make sure we are not already running a message */
916 if (master->cur_msg) {
917 spin_unlock_irqrestore(&master->queue_lock, flags);
918 return;
919 }
920 /* Extract head of queue */
921 master->cur_msg =
Axel Lina89e2d22014-01-09 16:03:58 +0800922 list_first_entry(&master->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100923
924 list_del_init(&master->cur_msg->queue);
925 if (master->busy)
926 was_busy = true;
927 else
928 master->busy = true;
929 spin_unlock_irqrestore(&master->queue_lock, flags);
930
Mark Brown49834de2013-07-28 14:47:02 +0100931 if (!was_busy && master->auto_runtime_pm) {
932 ret = pm_runtime_get_sync(master->dev.parent);
933 if (ret < 0) {
934 dev_err(&master->dev, "Failed to power device: %d\n",
935 ret);
936 return;
937 }
938 }
939
Mark Brown56ec1972013-10-07 19:33:53 +0100940 if (!was_busy)
941 trace_spi_master_busy(master);
942
Shubhrajyoti D7dfd2bd2012-05-10 19:20:41 +0530943 if (!was_busy && master->prepare_transfer_hardware) {
Linus Walleijffbbdd212012-02-22 10:05:38 +0100944 ret = master->prepare_transfer_hardware(master);
945 if (ret) {
946 dev_err(&master->dev,
947 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +0100948
949 if (master->auto_runtime_pm)
950 pm_runtime_put(master->dev.parent);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100951 return;
952 }
953 }
954
Mark Brown56ec1972013-10-07 19:33:53 +0100955 trace_spi_message_start(master->cur_msg);
956
Mark Brown2841a5f2013-10-05 00:23:12 +0100957 if (master->prepare_message) {
958 ret = master->prepare_message(master, master->cur_msg);
959 if (ret) {
960 dev_err(&master->dev,
961 "failed to prepare message: %d\n", ret);
962 master->cur_msg->status = ret;
963 spi_finalize_current_message(master);
964 return;
965 }
966 master->cur_msg_prepared = true;
967 }
968
Mark Brown99adef32014-01-16 12:22:43 +0000969 ret = spi_map_msg(master, master->cur_msg);
970 if (ret) {
971 master->cur_msg->status = ret;
972 spi_finalize_current_message(master);
973 return;
974 }
975
Linus Walleijffbbdd212012-02-22 10:05:38 +0100976 ret = master->transfer_one_message(master, master->cur_msg);
977 if (ret) {
978 dev_err(&master->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +0100979 "failed to transfer one message from queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +0100980 return;
981 }
982}
983
984static int spi_init_queue(struct spi_master *master)
985{
986 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
987
988 INIT_LIST_HEAD(&master->queue);
989 spin_lock_init(&master->queue_lock);
990
991 master->running = false;
992 master->busy = false;
993
994 init_kthread_worker(&master->kworker);
995 master->kworker_task = kthread_run(kthread_worker_fn,
Kees Cookf1701682013-07-03 15:04:58 -0700996 &master->kworker, "%s",
Linus Walleijffbbdd212012-02-22 10:05:38 +0100997 dev_name(&master->dev));
998 if (IS_ERR(master->kworker_task)) {
999 dev_err(&master->dev, "failed to create message pump task\n");
1000 return -ENOMEM;
1001 }
1002 init_kthread_work(&master->pump_messages, spi_pump_messages);
1003
1004 /*
1005 * Master config will indicate if this controller should run the
1006 * message pump with high (realtime) priority to reduce the transfer
1007 * latency on the bus by minimising the delay between a transfer
1008 * request and the scheduling of the message pump thread. Without this
1009 * setting the message pump thread will remain at default priority.
1010 */
1011 if (master->rt) {
1012 dev_info(&master->dev,
1013 "will run message pump with realtime priority\n");
1014 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
1015 }
1016
1017 return 0;
1018}
1019
1020/**
1021 * spi_get_next_queued_message() - called by driver to check for queued
1022 * messages
1023 * @master: the master to check for queued messages
1024 *
1025 * If there are more messages in the queue, the next message is returned from
1026 * this call.
1027 */
1028struct spi_message *spi_get_next_queued_message(struct spi_master *master)
1029{
1030 struct spi_message *next;
1031 unsigned long flags;
1032
1033 /* get a pointer to the next message, if any */
1034 spin_lock_irqsave(&master->queue_lock, flags);
Axel Lin1cfd97f2014-01-02 15:16:40 +08001035 next = list_first_entry_or_null(&master->queue, struct spi_message,
1036 queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001037 spin_unlock_irqrestore(&master->queue_lock, flags);
1038
1039 return next;
1040}
1041EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1042
1043/**
1044 * spi_finalize_current_message() - the current message is complete
1045 * @master: the master to return the message to
1046 *
1047 * Called by the driver to notify the core that the message in the front of the
1048 * queue is complete and can be removed from the queue.
1049 */
1050void spi_finalize_current_message(struct spi_master *master)
1051{
1052 struct spi_message *mesg;
1053 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001054 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001055
1056 spin_lock_irqsave(&master->queue_lock, flags);
1057 mesg = master->cur_msg;
1058 master->cur_msg = NULL;
1059
1060 queue_kthread_work(&master->kworker, &master->pump_messages);
1061 spin_unlock_irqrestore(&master->queue_lock, flags);
1062
Mark Brown99adef32014-01-16 12:22:43 +00001063 spi_unmap_msg(master, mesg);
1064
Mark Brown2841a5f2013-10-05 00:23:12 +01001065 if (master->cur_msg_prepared && master->unprepare_message) {
1066 ret = master->unprepare_message(master, mesg);
1067 if (ret) {
1068 dev_err(&master->dev,
1069 "failed to unprepare message: %d\n", ret);
1070 }
1071 }
1072 master->cur_msg_prepared = false;
1073
Linus Walleijffbbdd212012-02-22 10:05:38 +01001074 mesg->state = NULL;
1075 if (mesg->complete)
1076 mesg->complete(mesg->context);
Mark Brown56ec1972013-10-07 19:33:53 +01001077
1078 trace_spi_message_done(mesg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001079}
1080EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1081
1082static int spi_start_queue(struct spi_master *master)
1083{
1084 unsigned long flags;
1085
1086 spin_lock_irqsave(&master->queue_lock, flags);
1087
1088 if (master->running || master->busy) {
1089 spin_unlock_irqrestore(&master->queue_lock, flags);
1090 return -EBUSY;
1091 }
1092
1093 master->running = true;
1094 master->cur_msg = NULL;
1095 spin_unlock_irqrestore(&master->queue_lock, flags);
1096
1097 queue_kthread_work(&master->kworker, &master->pump_messages);
1098
1099 return 0;
1100}
1101
1102static int spi_stop_queue(struct spi_master *master)
1103{
1104 unsigned long flags;
1105 unsigned limit = 500;
1106 int ret = 0;
1107
1108 spin_lock_irqsave(&master->queue_lock, flags);
1109
1110 /*
1111 * This is a bit lame, but is optimized for the common execution path.
1112 * A wait_queue on the master->busy could be used, but then the common
1113 * execution path (pump_messages) would be required to call wake_up or
1114 * friends on every SPI message. Do this instead.
1115 */
1116 while ((!list_empty(&master->queue) || master->busy) && limit--) {
1117 spin_unlock_irqrestore(&master->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001118 usleep_range(10000, 11000);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001119 spin_lock_irqsave(&master->queue_lock, flags);
1120 }
1121
1122 if (!list_empty(&master->queue) || master->busy)
1123 ret = -EBUSY;
1124 else
1125 master->running = false;
1126
1127 spin_unlock_irqrestore(&master->queue_lock, flags);
1128
1129 if (ret) {
1130 dev_warn(&master->dev,
1131 "could not stop message queue\n");
1132 return ret;
1133 }
1134 return ret;
1135}
1136
1137static int spi_destroy_queue(struct spi_master *master)
1138{
1139 int ret;
1140
1141 ret = spi_stop_queue(master);
1142
1143 /*
1144 * flush_kthread_worker will block until all work is done.
1145 * If the reason that stop_queue timed out is that the work will never
1146 * finish, then it does no good to call flush/stop thread, so
1147 * return anyway.
1148 */
1149 if (ret) {
1150 dev_err(&master->dev, "problem destroying queue\n");
1151 return ret;
1152 }
1153
1154 flush_kthread_worker(&master->kworker);
1155 kthread_stop(master->kworker_task);
1156
1157 return 0;
1158}
1159
1160/**
1161 * spi_queued_transfer - transfer function for queued transfers
1162 * @spi: spi device which is requesting transfer
1163 * @msg: spi message which is to handled is queued to driver queue
1164 */
1165static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1166{
1167 struct spi_master *master = spi->master;
1168 unsigned long flags;
1169
1170 spin_lock_irqsave(&master->queue_lock, flags);
1171
1172 if (!master->running) {
1173 spin_unlock_irqrestore(&master->queue_lock, flags);
1174 return -ESHUTDOWN;
1175 }
1176 msg->actual_length = 0;
1177 msg->status = -EINPROGRESS;
1178
1179 list_add_tail(&msg->queue, &master->queue);
Axel Lin96b3eac2013-08-22 23:41:34 +08001180 if (!master->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001181 queue_kthread_work(&master->kworker, &master->pump_messages);
1182
1183 spin_unlock_irqrestore(&master->queue_lock, flags);
1184 return 0;
1185}
1186
1187static int spi_master_initialize_queue(struct spi_master *master)
1188{
1189 int ret;
1190
Linus Walleijffbbdd212012-02-22 10:05:38 +01001191 master->transfer = spi_queued_transfer;
Mark Brownb1589352013-10-05 11:50:40 +01001192 if (!master->transfer_one_message)
1193 master->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001194
1195 /* Initialize and start queue */
1196 ret = spi_init_queue(master);
1197 if (ret) {
1198 dev_err(&master->dev, "problem initializing queue\n");
1199 goto err_init_queue;
1200 }
Mark Brownc3676d52014-05-01 10:47:52 -07001201 master->queued = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001202 ret = spi_start_queue(master);
1203 if (ret) {
1204 dev_err(&master->dev, "problem starting queue\n");
1205 goto err_start_queue;
1206 }
1207
1208 return 0;
1209
1210err_start_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001211 spi_destroy_queue(master);
Mark Brownc3676d52014-05-01 10:47:52 -07001212err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001213 return ret;
1214}
1215
1216/*-------------------------------------------------------------------------*/
1217
Andreas Larsson7cb94362012-12-04 15:09:38 +01001218#if defined(CONFIG_OF)
Grant Likelyd57a4282012-04-07 14:16:53 -06001219/**
1220 * of_register_spi_devices() - Register child devices onto the SPI bus
1221 * @master: Pointer to spi_master device
1222 *
1223 * Registers an spi_device for each child node of master node which has a 'reg'
1224 * property.
1225 */
1226static void of_register_spi_devices(struct spi_master *master)
1227{
1228 struct spi_device *spi;
1229 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001230 int rc;
Trent Piepho89da4292013-09-27 05:37:25 -07001231 u32 value;
Grant Likelyd57a4282012-04-07 14:16:53 -06001232
1233 if (!master->dev.of_node)
1234 return;
1235
Alexander Sverdlinf3b61592012-11-29 08:59:29 +01001236 for_each_available_child_of_node(master->dev.of_node, nc) {
Grant Likelyd57a4282012-04-07 14:16:53 -06001237 /* Alloc an spi_device */
1238 spi = spi_alloc_device(master);
1239 if (!spi) {
1240 dev_err(&master->dev, "spi_device alloc error for %s\n",
1241 nc->full_name);
1242 spi_dev_put(spi);
1243 continue;
1244 }
1245
1246 /* Select device driver */
1247 if (of_modalias_node(nc, spi->modalias,
1248 sizeof(spi->modalias)) < 0) {
1249 dev_err(&master->dev, "cannot find modalias for %s\n",
1250 nc->full_name);
1251 spi_dev_put(spi);
1252 continue;
1253 }
1254
1255 /* Device address */
Trent Piepho89da4292013-09-27 05:37:25 -07001256 rc = of_property_read_u32(nc, "reg", &value);
1257 if (rc) {
1258 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1259 nc->full_name, rc);
Grant Likelyd57a4282012-04-07 14:16:53 -06001260 spi_dev_put(spi);
1261 continue;
1262 }
Trent Piepho89da4292013-09-27 05:37:25 -07001263 spi->chip_select = value;
Grant Likelyd57a4282012-04-07 14:16:53 -06001264
1265 /* Mode (clock phase/polarity/etc.) */
1266 if (of_find_property(nc, "spi-cpha", NULL))
1267 spi->mode |= SPI_CPHA;
1268 if (of_find_property(nc, "spi-cpol", NULL))
1269 spi->mode |= SPI_CPOL;
1270 if (of_find_property(nc, "spi-cs-high", NULL))
1271 spi->mode |= SPI_CS_HIGH;
Lars-Peter Clausenc20151d2012-12-06 16:55:33 +01001272 if (of_find_property(nc, "spi-3wire", NULL))
1273 spi->mode |= SPI_3WIRE;
Zhao Qiangcd6339e2014-04-01 17:10:50 +08001274 if (of_find_property(nc, "spi-lsb-first", NULL))
1275 spi->mode |= SPI_LSB_FIRST;
Grant Likelyd57a4282012-04-07 14:16:53 -06001276
wangyuhangf477b7f2013-08-11 18:15:17 +08001277 /* Device DUAL/QUAD mode */
Trent Piepho89da4292013-09-27 05:37:25 -07001278 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1279 switch (value) {
1280 case 1:
Mark Browna822e992013-08-30 23:19:40 +01001281 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001282 case 2:
Mark Browna822e992013-08-30 23:19:40 +01001283 spi->mode |= SPI_TX_DUAL;
1284 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001285 case 4:
Mark Browna822e992013-08-30 23:19:40 +01001286 spi->mode |= SPI_TX_QUAD;
1287 break;
1288 default:
Geert Uytterhoeven80874d82014-05-26 14:05:25 +02001289 dev_warn(&master->dev,
1290 "spi-tx-bus-width %d not supported\n",
1291 value);
1292 break;
Mark Browna822e992013-08-30 23:19:40 +01001293 }
wangyuhangf477b7f2013-08-11 18:15:17 +08001294 }
1295
Trent Piepho89da4292013-09-27 05:37:25 -07001296 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1297 switch (value) {
1298 case 1:
Mark Browna822e992013-08-30 23:19:40 +01001299 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001300 case 2:
Mark Browna822e992013-08-30 23:19:40 +01001301 spi->mode |= SPI_RX_DUAL;
1302 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001303 case 4:
Mark Browna822e992013-08-30 23:19:40 +01001304 spi->mode |= SPI_RX_QUAD;
1305 break;
1306 default:
Geert Uytterhoeven80874d82014-05-26 14:05:25 +02001307 dev_warn(&master->dev,
1308 "spi-rx-bus-width %d not supported\n",
1309 value);
1310 break;
Mark Browna822e992013-08-30 23:19:40 +01001311 }
wangyuhangf477b7f2013-08-11 18:15:17 +08001312 }
1313
Grant Likelyd57a4282012-04-07 14:16:53 -06001314 /* Device speed */
Trent Piepho89da4292013-09-27 05:37:25 -07001315 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1316 if (rc) {
1317 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1318 nc->full_name, rc);
Grant Likelyd57a4282012-04-07 14:16:53 -06001319 spi_dev_put(spi);
1320 continue;
1321 }
Trent Piepho89da4292013-09-27 05:37:25 -07001322 spi->max_speed_hz = value;
Grant Likelyd57a4282012-04-07 14:16:53 -06001323
1324 /* IRQ */
1325 spi->irq = irq_of_parse_and_map(nc, 0);
1326
1327 /* Store a pointer to the node in the device structure */
1328 of_node_get(nc);
1329 spi->dev.of_node = nc;
1330
1331 /* Register the new device */
Mathias Krause70fac172013-08-31 20:24:14 +02001332 request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
Grant Likelyd57a4282012-04-07 14:16:53 -06001333 rc = spi_add_device(spi);
1334 if (rc) {
1335 dev_err(&master->dev, "spi_device register error %s\n",
1336 nc->full_name);
1337 spi_dev_put(spi);
1338 }
1339
1340 }
1341}
1342#else
1343static void of_register_spi_devices(struct spi_master *master) { }
1344#endif
1345
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001346#ifdef CONFIG_ACPI
1347static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1348{
1349 struct spi_device *spi = data;
1350
1351 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1352 struct acpi_resource_spi_serialbus *sb;
1353
1354 sb = &ares->data.spi_serial_bus;
1355 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1356 spi->chip_select = sb->device_selection;
1357 spi->max_speed_hz = sb->connection_speed;
1358
1359 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1360 spi->mode |= SPI_CPHA;
1361 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1362 spi->mode |= SPI_CPOL;
1363 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1364 spi->mode |= SPI_CS_HIGH;
1365 }
1366 } else if (spi->irq < 0) {
1367 struct resource r;
1368
1369 if (acpi_dev_resource_interrupt(ares, 0, &r))
1370 spi->irq = r.start;
1371 }
1372
1373 /* Always tell the ACPI core to skip this resource */
1374 return 1;
1375}
1376
1377static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1378 void *data, void **return_value)
1379{
1380 struct spi_master *master = data;
1381 struct list_head resource_list;
1382 struct acpi_device *adev;
1383 struct spi_device *spi;
1384 int ret;
1385
1386 if (acpi_bus_get_device(handle, &adev))
1387 return AE_OK;
1388 if (acpi_bus_get_status(adev) || !adev->status.present)
1389 return AE_OK;
1390
1391 spi = spi_alloc_device(master);
1392 if (!spi) {
1393 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1394 dev_name(&adev->dev));
1395 return AE_NO_MEMORY;
1396 }
1397
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001398 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001399 spi->irq = -1;
1400
1401 INIT_LIST_HEAD(&resource_list);
1402 ret = acpi_dev_get_resources(adev, &resource_list,
1403 acpi_spi_add_resource, spi);
1404 acpi_dev_free_resource_list(&resource_list);
1405
1406 if (ret < 0 || !spi->max_speed_hz) {
1407 spi_dev_put(spi);
1408 return AE_OK;
1409 }
1410
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001411 adev->power.flags.ignore_parent = true;
Jarkko Nikulacf9eb392013-10-10 17:19:17 +03001412 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001413 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001414 adev->power.flags.ignore_parent = false;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001415 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1416 dev_name(&adev->dev));
1417 spi_dev_put(spi);
1418 }
1419
1420 return AE_OK;
1421}
1422
1423static void acpi_register_spi_devices(struct spi_master *master)
1424{
1425 acpi_status status;
1426 acpi_handle handle;
1427
Rafael J. Wysocki29896172013-04-01 00:21:08 +00001428 handle = ACPI_HANDLE(master->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001429 if (!handle)
1430 return;
1431
1432 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1433 acpi_spi_add_device, NULL,
1434 master, NULL);
1435 if (ACPI_FAILURE(status))
1436 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1437}
1438#else
1439static inline void acpi_register_spi_devices(struct spi_master *master) {}
1440#endif /* CONFIG_ACPI */
1441
Tony Jones49dce682007-10-16 01:27:48 -07001442static void spi_master_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001443{
1444 struct spi_master *master;
1445
Tony Jones49dce682007-10-16 01:27:48 -07001446 master = container_of(dev, struct spi_master, dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001447 kfree(master);
1448}
1449
1450static struct class spi_master_class = {
1451 .name = "spi_master",
1452 .owner = THIS_MODULE,
Tony Jones49dce682007-10-16 01:27:48 -07001453 .dev_release = spi_master_release,
David Brownell8ae12a02006-01-08 13:34:19 -08001454};
1455
1456
Linus Walleijffbbdd212012-02-22 10:05:38 +01001457
David Brownell8ae12a02006-01-08 13:34:19 -08001458/**
1459 * spi_alloc_master - allocate SPI master controller
1460 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001461 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001462 * memory is in the driver_data field of the returned device,
David Brownell0c868462006-01-08 13:34:25 -08001463 * accessible with spi_master_get_devdata().
David Brownell33e34dc2007-05-08 00:32:21 -07001464 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001465 *
1466 * This call is used only by SPI master controller drivers, which are the
1467 * only ones directly touching chip registers. It's how they allocate
dmitry pervushinba1a0512006-05-20 15:00:14 -07001468 * an spi_master structure, prior to calling spi_register_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001469 *
1470 * This must be called from context that can sleep. It returns the SPI
1471 * master structure on success, else NULL.
1472 *
1473 * The caller is responsible for assigning the bus number and initializing
dmitry pervushinba1a0512006-05-20 15:00:14 -07001474 * the master's methods before calling spi_register_master(); and (after errors
Uwe Kleine-Königeb4af0f2012-02-23 10:40:14 +01001475 * adding the device) calling spi_master_put() and kfree() to prevent a memory
1476 * leak.
David Brownell8ae12a02006-01-08 13:34:19 -08001477 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001478struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
David Brownell8ae12a02006-01-08 13:34:19 -08001479{
1480 struct spi_master *master;
1481
David Brownell0c868462006-01-08 13:34:25 -08001482 if (!dev)
1483 return NULL;
1484
Jingoo Han5fe5f052013-10-14 10:31:51 +09001485 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -08001486 if (!master)
1487 return NULL;
1488
Tony Jones49dce682007-10-16 01:27:48 -07001489 device_initialize(&master->dev);
Grant Likely1e8a52e2012-05-19 23:42:08 -06001490 master->bus_num = -1;
1491 master->num_chipselect = 1;
Tony Jones49dce682007-10-16 01:27:48 -07001492 master->dev.class = &spi_master_class;
1493 master->dev.parent = get_device(dev);
David Brownell0c868462006-01-08 13:34:25 -08001494 spi_master_set_devdata(master, &master[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001495
1496 return master;
1497}
1498EXPORT_SYMBOL_GPL(spi_alloc_master);
1499
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001500#ifdef CONFIG_OF
1501static int of_spi_register_master(struct spi_master *master)
1502{
Grant Likelye80beb22013-02-12 17:48:37 +00001503 int nb, i, *cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001504 struct device_node *np = master->dev.of_node;
1505
1506 if (!np)
1507 return 0;
1508
1509 nb = of_gpio_named_count(np, "cs-gpios");
Jingoo Han5fe5f052013-10-14 10:31:51 +09001510 master->num_chipselect = max_t(int, nb, master->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001511
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001512 /* Return error only for an incorrectly formed cs-gpios property */
1513 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001514 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001515 else if (nb < 0)
1516 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001517
1518 cs = devm_kzalloc(&master->dev,
1519 sizeof(int) * master->num_chipselect,
1520 GFP_KERNEL);
1521 master->cs_gpios = cs;
1522
1523 if (!master->cs_gpios)
1524 return -ENOMEM;
1525
Andreas Larsson0da83bb2013-01-29 15:53:40 +01001526 for (i = 0; i < master->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01001527 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001528
1529 for (i = 0; i < nb; i++)
1530 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1531
1532 return 0;
1533}
1534#else
1535static int of_spi_register_master(struct spi_master *master)
1536{
1537 return 0;
1538}
1539#endif
1540
David Brownell8ae12a02006-01-08 13:34:19 -08001541/**
1542 * spi_register_master - register SPI master controller
1543 * @master: initialized master, originally from spi_alloc_master()
David Brownell33e34dc2007-05-08 00:32:21 -07001544 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001545 *
1546 * SPI master controllers connect to their drivers using some non-SPI bus,
1547 * such as the platform bus. The final stage of probe() in that code
1548 * includes calling spi_register_master() to hook up to this SPI bus glue.
1549 *
1550 * SPI controllers use board specific (often SOC specific) bus numbers,
1551 * and board-specific addressing for SPI devices combines those numbers
1552 * with chip select numbers. Since SPI does not directly support dynamic
1553 * device identification, boards need configuration tables telling which
1554 * chip is at which address.
1555 *
1556 * This must be called from context that can sleep. It returns zero on
1557 * success, else a negative error code (dropping the master's refcount).
David Brownell0c868462006-01-08 13:34:25 -08001558 * After a successful return, the caller is responsible for calling
1559 * spi_unregister_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001560 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001561int spi_register_master(struct spi_master *master)
David Brownell8ae12a02006-01-08 13:34:19 -08001562{
David Brownelle44a45a2007-06-03 13:50:40 -07001563 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Tony Jones49dce682007-10-16 01:27:48 -07001564 struct device *dev = master->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08001565 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08001566 int status = -ENODEV;
1567 int dynamic = 0;
1568
David Brownell0c868462006-01-08 13:34:25 -08001569 if (!dev)
1570 return -ENODEV;
1571
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001572 status = of_spi_register_master(master);
1573 if (status)
1574 return status;
1575
David Brownell082c8cb2007-07-31 00:39:45 -07001576 /* even if it's just one always-selected device, there must
1577 * be at least one chipselect
1578 */
1579 if (master->num_chipselect == 0)
1580 return -EINVAL;
1581
Grant Likelybb297852012-12-21 19:32:09 +00001582 if ((master->bus_num < 0) && master->dev.of_node)
1583 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1584
David Brownell8ae12a02006-01-08 13:34:19 -08001585 /* convention: dynamically assigned bus IDs count down from the max */
David Brownella020ed72006-04-03 15:49:04 -07001586 if (master->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07001587 /* FIXME switch to an IDR based scheme, something like
1588 * I2C now uses, so we can't run out of "dynamic" IDs
1589 */
David Brownell8ae12a02006-01-08 13:34:19 -08001590 master->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08001591 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08001592 }
1593
Ernst Schwabcf32b712010-06-28 17:49:29 -07001594 spin_lock_init(&master->bus_lock_spinlock);
1595 mutex_init(&master->bus_lock_mutex);
1596 master->bus_lock_flag = 0;
Mark Brownb1589352013-10-05 11:50:40 +01001597 init_completion(&master->xfer_completion);
Mark Brown6ad45a22014-02-02 13:47:47 +00001598 if (!master->max_dma_len)
1599 master->max_dma_len = INT_MAX;
Ernst Schwabcf32b712010-06-28 17:49:29 -07001600
David Brownell8ae12a02006-01-08 13:34:19 -08001601 /* register the device, then userspace will see it.
1602 * registration fails if the bus ID is in use.
1603 */
Kay Sievers35f74fc2009-01-06 10:44:37 -08001604 dev_set_name(&master->dev, "spi%u", master->bus_num);
Tony Jones49dce682007-10-16 01:27:48 -07001605 status = device_add(&master->dev);
David Brownellb8852442006-01-08 13:34:23 -08001606 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08001607 goto done;
Kay Sievers35f74fc2009-01-06 10:44:37 -08001608 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
David Brownell8ae12a02006-01-08 13:34:19 -08001609 dynamic ? " (dynamic)" : "");
1610
Linus Walleijffbbdd212012-02-22 10:05:38 +01001611 /* If we're using a queued driver, start the queue */
1612 if (master->transfer)
1613 dev_info(dev, "master is unqueued, this is deprecated\n");
1614 else {
1615 status = spi_master_initialize_queue(master);
1616 if (status) {
Axel Line93b0722013-08-31 20:25:52 +08001617 device_del(&master->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001618 goto done;
1619 }
1620 }
1621
Feng Tang2b9603a2010-08-02 15:52:15 +08001622 mutex_lock(&board_lock);
1623 list_add_tail(&master->list, &spi_master_list);
1624 list_for_each_entry(bi, &board_list, list)
1625 spi_match_master_to_boardinfo(master, &bi->board_info);
1626 mutex_unlock(&board_lock);
1627
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001628 /* Register devices from the device tree and ACPI */
Anatolij Gustschin12b15e82010-07-27 22:35:58 +02001629 of_register_spi_devices(master);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001630 acpi_register_spi_devices(master);
David Brownell8ae12a02006-01-08 13:34:19 -08001631done:
1632 return status;
1633}
1634EXPORT_SYMBOL_GPL(spi_register_master);
1635
Mark Brown666d5b42013-08-31 18:50:52 +01001636static void devm_spi_unregister(struct device *dev, void *res)
1637{
1638 spi_unregister_master(*(struct spi_master **)res);
1639}
1640
1641/**
1642 * dev_spi_register_master - register managed SPI master controller
1643 * @dev: device managing SPI master
1644 * @master: initialized master, originally from spi_alloc_master()
1645 * Context: can sleep
1646 *
1647 * Register a SPI device as with spi_register_master() which will
1648 * automatically be unregister
1649 */
1650int devm_spi_register_master(struct device *dev, struct spi_master *master)
1651{
1652 struct spi_master **ptr;
1653 int ret;
1654
1655 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1656 if (!ptr)
1657 return -ENOMEM;
1658
1659 ret = spi_register_master(master);
Stephen Warren4b928942013-11-21 16:11:15 -07001660 if (!ret) {
Mark Brown666d5b42013-08-31 18:50:52 +01001661 *ptr = master;
1662 devres_add(dev, ptr);
1663 } else {
1664 devres_free(ptr);
1665 }
1666
1667 return ret;
1668}
1669EXPORT_SYMBOL_GPL(devm_spi_register_master);
1670
David Lamparter34860082010-08-30 23:54:17 +02001671static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08001672{
David Lamparter34860082010-08-30 23:54:17 +02001673 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08001674 return 0;
1675}
1676
1677/**
1678 * spi_unregister_master - unregister SPI master controller
1679 * @master: the master being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07001680 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001681 *
1682 * This call is used only by SPI master controller drivers, which are the
1683 * only ones directly touching chip registers.
1684 *
1685 * This must be called from context that can sleep.
1686 */
1687void spi_unregister_master(struct spi_master *master)
1688{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08001689 int dummy;
1690
Linus Walleijffbbdd212012-02-22 10:05:38 +01001691 if (master->queued) {
1692 if (spi_destroy_queue(master))
1693 dev_err(&master->dev, "queue remove failed\n");
1694 }
1695
Feng Tang2b9603a2010-08-02 15:52:15 +08001696 mutex_lock(&board_lock);
1697 list_del(&master->list);
1698 mutex_unlock(&board_lock);
1699
Sebastian Andrzej Siewior97dbf372010-12-21 17:24:31 -08001700 dummy = device_for_each_child(&master->dev, NULL, __unregister);
Tony Jones49dce682007-10-16 01:27:48 -07001701 device_unregister(&master->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001702}
1703EXPORT_SYMBOL_GPL(spi_unregister_master);
1704
Linus Walleijffbbdd212012-02-22 10:05:38 +01001705int spi_master_suspend(struct spi_master *master)
1706{
1707 int ret;
1708
1709 /* Basically no-ops for non-queued masters */
1710 if (!master->queued)
1711 return 0;
1712
1713 ret = spi_stop_queue(master);
1714 if (ret)
1715 dev_err(&master->dev, "queue stop failed\n");
1716
1717 return ret;
1718}
1719EXPORT_SYMBOL_GPL(spi_master_suspend);
1720
1721int spi_master_resume(struct spi_master *master)
1722{
1723 int ret;
1724
1725 if (!master->queued)
1726 return 0;
1727
1728 ret = spi_start_queue(master);
1729 if (ret)
1730 dev_err(&master->dev, "queue restart failed\n");
1731
1732 return ret;
1733}
1734EXPORT_SYMBOL_GPL(spi_master_resume);
1735
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001736static int __spi_master_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08001737{
1738 struct spi_master *m;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001739 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08001740
1741 m = container_of(dev, struct spi_master, dev);
1742 return m->bus_num == *bus_num;
1743}
1744
David Brownell8ae12a02006-01-08 13:34:19 -08001745/**
1746 * spi_busnum_to_master - look up master associated with bus_num
1747 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07001748 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001749 *
1750 * This call may be used with devices that are registered after
1751 * arch init time. It returns a refcounted pointer to the relevant
1752 * spi_master (which the caller must release), or NULL if there is
1753 * no such master registered.
1754 */
1755struct spi_master *spi_busnum_to_master(u16 bus_num)
1756{
Tony Jones49dce682007-10-16 01:27:48 -07001757 struct device *dev;
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001758 struct spi_master *master = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08001759
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001760 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Dave Young5ed2c832008-01-22 15:14:18 +08001761 __spi_master_match);
1762 if (dev)
1763 master = container_of(dev, struct spi_master, dev);
1764 /* reference got in class_find_device */
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001765 return master;
David Brownell8ae12a02006-01-08 13:34:19 -08001766}
1767EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1768
1769
1770/*-------------------------------------------------------------------------*/
1771
David Brownell7d077192009-06-17 16:26:03 -07001772/* Core methods for SPI master protocol drivers. Some of the
1773 * other core methods are currently defined as inline functions.
1774 */
1775
1776/**
1777 * spi_setup - setup SPI mode and clock rate
1778 * @spi: the device whose settings are being modified
1779 * Context: can sleep, and no requests are queued to the device
1780 *
1781 * SPI protocol drivers may need to update the transfer mode if the
1782 * device doesn't work with its default. They may likewise need
1783 * to update clock rates or word sizes from initial values. This function
1784 * changes those settings, and must be called from a context that can sleep.
1785 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
1786 * effect the next time the device is selected and data is transferred to
1787 * or from it. When this function returns, the spi device is deselected.
1788 *
1789 * Note that this call will fail if the protocol driver specifies an option
1790 * that the underlying controller or its driver does not support. For
1791 * example, not all hardware supports wire transfers using nine bit words,
1792 * LSB-first wire encoding, or active-high chipselects.
1793 */
1794int spi_setup(struct spi_device *spi)
1795{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02001796 unsigned bad_bits, ugly_bits;
Laxman Dewangancaae0702012-11-09 14:35:22 +05301797 int status = 0;
David Brownell7d077192009-06-17 16:26:03 -07001798
wangyuhangf477b7f2013-08-11 18:15:17 +08001799 /* check mode to prevent that DUAL and QUAD set at the same time
1800 */
1801 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1802 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1803 dev_err(&spi->dev,
1804 "setup: can not select dual and quad at the same time\n");
1805 return -EINVAL;
1806 }
1807 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1808 */
1809 if ((spi->mode & SPI_3WIRE) && (spi->mode &
1810 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1811 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07001812 /* help drivers fail *cleanly* when they need options
1813 * that aren't supported with their current master
1814 */
1815 bad_bits = spi->mode & ~spi->master->mode_bits;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02001816 ugly_bits = bad_bits &
1817 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
1818 if (ugly_bits) {
1819 dev_warn(&spi->dev,
1820 "setup: ignoring unsupported mode bits %x\n",
1821 ugly_bits);
1822 spi->mode &= ~ugly_bits;
1823 bad_bits &= ~ugly_bits;
1824 }
David Brownelle7db06b2009-06-17 16:26:04 -07001825 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02001826 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07001827 bad_bits);
1828 return -EINVAL;
1829 }
1830
David Brownell7d077192009-06-17 16:26:03 -07001831 if (!spi->bits_per_word)
1832 spi->bits_per_word = 8;
1833
Axel Lin052eb2d42014-02-10 00:08:05 +08001834 if (!spi->max_speed_hz)
1835 spi->max_speed_hz = spi->master->max_speed_hz;
1836
Laxman Dewangancaae0702012-11-09 14:35:22 +05301837 if (spi->master->setup)
1838 status = spi->master->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07001839
Jingoo Han5fe5f052013-10-14 10:31:51 +09001840 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 -07001841 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
1842 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
1843 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
1844 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
1845 (spi->mode & SPI_LOOP) ? "loopback, " : "",
1846 spi->bits_per_word, spi->max_speed_hz,
1847 status);
1848
1849 return status;
1850}
1851EXPORT_SYMBOL_GPL(spi_setup);
1852
Mark Brown90808732013-11-13 23:44:15 +00001853static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b712010-06-28 17:49:29 -07001854{
1855 struct spi_master *master = spi->master;
Laxman Dewangane6811d12012-11-09 14:36:45 +05301856 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09001857 int w_size;
Ernst Schwabcf32b712010-06-28 17:49:29 -07001858
Mark Brown24a00132013-07-10 15:05:40 +01001859 if (list_empty(&message->transfers))
1860 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01001861
Ernst Schwabcf32b712010-06-28 17:49:29 -07001862 /* Half-duplex links include original MicroWire, and ones with
1863 * only one data pin like SPI_3WIRE (switches direction) or where
1864 * either MOSI or MISO is missing. They can also be caused by
1865 * software limitations.
1866 */
1867 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
1868 || (spi->mode & SPI_3WIRE)) {
Ernst Schwabcf32b712010-06-28 17:49:29 -07001869 unsigned flags = master->flags;
1870
1871 list_for_each_entry(xfer, &message->transfers, transfer_list) {
1872 if (xfer->rx_buf && xfer->tx_buf)
1873 return -EINVAL;
1874 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
1875 return -EINVAL;
1876 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
1877 return -EINVAL;
1878 }
1879 }
1880
Laxman Dewangane6811d12012-11-09 14:36:45 +05301881 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05301882 * Set transfer bits_per_word and max speed as spi device default if
1883 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08001884 * Set transfer tx_nbits and rx_nbits as single transfer default
1885 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05301886 */
1887 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05301888 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05301889 if (!xfer->bits_per_word)
1890 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08001891
1892 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05301893 xfer->speed_hz = spi->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08001894
1895 if (master->max_speed_hz &&
1896 xfer->speed_hz > master->max_speed_hz)
1897 xfer->speed_hz = master->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02001898
Stephen Warren543bb252013-03-26 20:37:57 -06001899 if (master->bits_per_word_mask) {
1900 /* Only 32 bits fit in the mask */
1901 if (xfer->bits_per_word > 32)
1902 return -EINVAL;
1903 if (!(master->bits_per_word_mask &
1904 BIT(xfer->bits_per_word - 1)))
1905 return -EINVAL;
1906 }
Mark Browna2fd4f92013-07-10 14:57:26 +01001907
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02001908 /*
1909 * SPI transfer length should be multiple of SPI word size
1910 * where SPI word size should be power-of-two multiple
1911 */
1912 if (xfer->bits_per_word <= 8)
1913 w_size = 1;
1914 else if (xfer->bits_per_word <= 16)
1915 w_size = 2;
1916 else
1917 w_size = 4;
1918
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02001919 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09001920 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02001921 return -EINVAL;
1922
Mark Browna2fd4f92013-07-10 14:57:26 +01001923 if (xfer->speed_hz && master->min_speed_hz &&
1924 xfer->speed_hz < master->min_speed_hz)
1925 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08001926
1927 if (xfer->tx_buf && !xfer->tx_nbits)
1928 xfer->tx_nbits = SPI_NBITS_SINGLE;
1929 if (xfer->rx_buf && !xfer->rx_nbits)
1930 xfer->rx_nbits = SPI_NBITS_SINGLE;
1931 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01001932 * 1. check the value matches one of single, dual and quad
1933 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08001934 */
Sourav Poddardb90a442013-08-22 21:20:48 +05301935 if (xfer->tx_buf) {
1936 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
1937 xfer->tx_nbits != SPI_NBITS_DUAL &&
1938 xfer->tx_nbits != SPI_NBITS_QUAD)
1939 return -EINVAL;
1940 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
1941 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
1942 return -EINVAL;
1943 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
1944 !(spi->mode & SPI_TX_QUAD))
1945 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05301946 }
wangyuhangf477b7f2013-08-11 18:15:17 +08001947 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05301948 if (xfer->rx_buf) {
1949 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
1950 xfer->rx_nbits != SPI_NBITS_DUAL &&
1951 xfer->rx_nbits != SPI_NBITS_QUAD)
1952 return -EINVAL;
1953 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
1954 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
1955 return -EINVAL;
1956 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
1957 !(spi->mode & SPI_RX_QUAD))
1958 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05301959 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05301960 }
1961
Ernst Schwabcf32b712010-06-28 17:49:29 -07001962 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00001963
1964 return 0;
1965}
1966
1967static int __spi_async(struct spi_device *spi, struct spi_message *message)
1968{
1969 struct spi_master *master = spi->master;
1970
1971 message->spi = spi;
1972
1973 trace_spi_message_submit(message);
1974
Ernst Schwabcf32b712010-06-28 17:49:29 -07001975 return master->transfer(spi, message);
1976}
1977
David Brownell568d0692009-09-22 16:46:18 -07001978/**
1979 * spi_async - asynchronous SPI transfer
1980 * @spi: device with which data will be exchanged
1981 * @message: describes the data transfers, including completion callback
1982 * Context: any (irqs may be blocked, etc)
1983 *
1984 * This call may be used in_irq and other contexts which can't sleep,
1985 * as well as from task contexts which can sleep.
1986 *
1987 * The completion callback is invoked in a context which can't sleep.
1988 * Before that invocation, the value of message->status is undefined.
1989 * When the callback is issued, message->status holds either zero (to
1990 * indicate complete success) or a negative error code. After that
1991 * callback returns, the driver which issued the transfer request may
1992 * deallocate the associated memory; it's no longer in use by any SPI
1993 * core or controller driver code.
1994 *
1995 * Note that although all messages to a spi_device are handled in
1996 * FIFO order, messages may go to different devices in other orders.
1997 * Some device might be higher priority, or have various "hard" access
1998 * time requirements, for example.
1999 *
2000 * On detection of any fault during the transfer, processing of
2001 * the entire message is aborted, and the device is deselected.
2002 * Until returning from the associated message completion callback,
2003 * no other spi_message queued to that device will be processed.
2004 * (This rule applies equally to all the synchronous transfer calls,
2005 * which are wrappers around this core asynchronous primitive.)
2006 */
2007int spi_async(struct spi_device *spi, struct spi_message *message)
2008{
2009 struct spi_master *master = spi->master;
Ernst Schwabcf32b712010-06-28 17:49:29 -07002010 int ret;
2011 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07002012
Mark Brown90808732013-11-13 23:44:15 +00002013 ret = __spi_validate(spi, message);
2014 if (ret != 0)
2015 return ret;
2016
Ernst Schwabcf32b712010-06-28 17:49:29 -07002017 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07002018
Ernst Schwabcf32b712010-06-28 17:49:29 -07002019 if (master->bus_lock_flag)
2020 ret = -EBUSY;
2021 else
2022 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07002023
Ernst Schwabcf32b712010-06-28 17:49:29 -07002024 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2025
2026 return ret;
David Brownell568d0692009-09-22 16:46:18 -07002027}
2028EXPORT_SYMBOL_GPL(spi_async);
2029
Ernst Schwabcf32b712010-06-28 17:49:29 -07002030/**
2031 * spi_async_locked - version of spi_async with exclusive bus usage
2032 * @spi: device with which data will be exchanged
2033 * @message: describes the data transfers, including completion callback
2034 * Context: any (irqs may be blocked, etc)
2035 *
2036 * This call may be used in_irq and other contexts which can't sleep,
2037 * as well as from task contexts which can sleep.
2038 *
2039 * The completion callback is invoked in a context which can't sleep.
2040 * Before that invocation, the value of message->status is undefined.
2041 * When the callback is issued, message->status holds either zero (to
2042 * indicate complete success) or a negative error code. After that
2043 * callback returns, the driver which issued the transfer request may
2044 * deallocate the associated memory; it's no longer in use by any SPI
2045 * core or controller driver code.
2046 *
2047 * Note that although all messages to a spi_device are handled in
2048 * FIFO order, messages may go to different devices in other orders.
2049 * Some device might be higher priority, or have various "hard" access
2050 * time requirements, for example.
2051 *
2052 * On detection of any fault during the transfer, processing of
2053 * the entire message is aborted, and the device is deselected.
2054 * Until returning from the associated message completion callback,
2055 * no other spi_message queued to that device will be processed.
2056 * (This rule applies equally to all the synchronous transfer calls,
2057 * which are wrappers around this core asynchronous primitive.)
2058 */
2059int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2060{
2061 struct spi_master *master = spi->master;
2062 int ret;
2063 unsigned long flags;
2064
Mark Brown90808732013-11-13 23:44:15 +00002065 ret = __spi_validate(spi, message);
2066 if (ret != 0)
2067 return ret;
2068
Ernst Schwabcf32b712010-06-28 17:49:29 -07002069 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2070
2071 ret = __spi_async(spi, message);
2072
2073 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2074
2075 return ret;
2076
2077}
2078EXPORT_SYMBOL_GPL(spi_async_locked);
2079
David Brownell7d077192009-06-17 16:26:03 -07002080
2081/*-------------------------------------------------------------------------*/
2082
2083/* Utility methods for SPI master protocol drivers, layered on
2084 * top of the core. Some other utility methods are defined as
2085 * inline functions.
2086 */
2087
Andrew Morton5d870c82006-01-11 11:23:49 -08002088static void spi_complete(void *arg)
2089{
2090 complete(arg);
2091}
2092
Ernst Schwabcf32b712010-06-28 17:49:29 -07002093static int __spi_sync(struct spi_device *spi, struct spi_message *message,
2094 int bus_locked)
2095{
2096 DECLARE_COMPLETION_ONSTACK(done);
2097 int status;
2098 struct spi_master *master = spi->master;
2099
2100 message->complete = spi_complete;
2101 message->context = &done;
2102
2103 if (!bus_locked)
2104 mutex_lock(&master->bus_lock_mutex);
2105
2106 status = spi_async_locked(spi, message);
2107
2108 if (!bus_locked)
2109 mutex_unlock(&master->bus_lock_mutex);
2110
2111 if (status == 0) {
2112 wait_for_completion(&done);
2113 status = message->status;
2114 }
2115 message->context = NULL;
2116 return status;
2117}
2118
David Brownell8ae12a02006-01-08 13:34:19 -08002119/**
2120 * spi_sync - blocking/synchronous SPI data transfers
2121 * @spi: device with which data will be exchanged
2122 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07002123 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002124 *
2125 * This call may only be used from a context that may sleep. The sleep
2126 * is non-interruptible, and has no timeout. Low-overhead controller
2127 * drivers may DMA directly into and out of the message buffers.
2128 *
2129 * Note that the SPI device's chip select is active during the message,
2130 * and then is normally disabled between messages. Drivers for some
2131 * frequently-used devices may want to minimize costs of selecting a chip,
2132 * by leaving it selected in anticipation that the next message will go
2133 * to the same chip. (That may increase power usage.)
2134 *
David Brownell0c868462006-01-08 13:34:25 -08002135 * Also, the caller is guaranteeing that the memory associated with the
2136 * message will not be freed before this call returns.
2137 *
Marc Pignat9b938b72007-12-04 23:45:10 -08002138 * It returns zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002139 */
2140int spi_sync(struct spi_device *spi, struct spi_message *message)
2141{
Ernst Schwabcf32b712010-06-28 17:49:29 -07002142 return __spi_sync(spi, message, 0);
David Brownell8ae12a02006-01-08 13:34:19 -08002143}
2144EXPORT_SYMBOL_GPL(spi_sync);
2145
Ernst Schwabcf32b712010-06-28 17:49:29 -07002146/**
2147 * spi_sync_locked - version of spi_sync with exclusive bus usage
2148 * @spi: device with which data will be exchanged
2149 * @message: describes the data transfers
2150 * Context: can sleep
2151 *
2152 * This call may only be used from a context that may sleep. The sleep
2153 * is non-interruptible, and has no timeout. Low-overhead controller
2154 * drivers may DMA directly into and out of the message buffers.
2155 *
2156 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002157 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b712010-06-28 17:49:29 -07002158 * be released by a spi_bus_unlock call when the exclusive access is over.
2159 *
2160 * It returns zero on success, else a negative error code.
2161 */
2162int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2163{
2164 return __spi_sync(spi, message, 1);
2165}
2166EXPORT_SYMBOL_GPL(spi_sync_locked);
2167
2168/**
2169 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
2170 * @master: SPI bus master that should be locked for exclusive bus access
2171 * Context: can sleep
2172 *
2173 * This call may only be used from a context that may sleep. The sleep
2174 * is non-interruptible, and has no timeout.
2175 *
2176 * This call should be used by drivers that require exclusive access to the
2177 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
2178 * exclusive access is over. Data transfer must be done by spi_sync_locked
2179 * and spi_async_locked calls when the SPI bus lock is held.
2180 *
2181 * It returns zero on success, else a negative error code.
2182 */
2183int spi_bus_lock(struct spi_master *master)
2184{
2185 unsigned long flags;
2186
2187 mutex_lock(&master->bus_lock_mutex);
2188
2189 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2190 master->bus_lock_flag = 1;
2191 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2192
2193 /* mutex remains locked until spi_bus_unlock is called */
2194
2195 return 0;
2196}
2197EXPORT_SYMBOL_GPL(spi_bus_lock);
2198
2199/**
2200 * spi_bus_unlock - release the lock for exclusive SPI bus usage
2201 * @master: SPI bus master that was locked for exclusive bus access
2202 * Context: can sleep
2203 *
2204 * This call may only be used from a context that may sleep. The sleep
2205 * is non-interruptible, and has no timeout.
2206 *
2207 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
2208 * call.
2209 *
2210 * It returns zero on success, else a negative error code.
2211 */
2212int spi_bus_unlock(struct spi_master *master)
2213{
2214 master->bus_lock_flag = 0;
2215
2216 mutex_unlock(&master->bus_lock_mutex);
2217
2218 return 0;
2219}
2220EXPORT_SYMBOL_GPL(spi_bus_unlock);
2221
David Brownella9948b62006-04-02 10:37:40 -08002222/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09002223#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08002224
2225static u8 *buf;
2226
2227/**
2228 * spi_write_then_read - SPI synchronous write followed by read
2229 * @spi: device with which data will be exchanged
2230 * @txbuf: data to be written (need not be dma-safe)
2231 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07002232 * @rxbuf: buffer into which data will be read (need not be dma-safe)
2233 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07002234 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002235 *
2236 * This performs a half duplex MicroWire style transaction with the
2237 * device, sending txbuf and then reading rxbuf. The return value
2238 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08002239 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08002240 *
David Brownell0c868462006-01-08 13:34:25 -08002241 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07002242 * portable code should never use this for more than 32 bytes.
2243 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c868462006-01-08 13:34:25 -08002244 * spi_{async,sync}() calls with dma-safe buffers.
David Brownell8ae12a02006-01-08 13:34:19 -08002245 */
2246int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02002247 const void *txbuf, unsigned n_tx,
2248 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08002249{
David Brownell068f4072007-12-04 23:45:09 -08002250 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002251
2252 int status;
2253 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07002254 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08002255 u8 *local_buf;
2256
Mark Brownb3a223e2012-12-02 12:54:25 +09002257 /* Use preallocated DMA-safe buffer if we can. We can't avoid
2258 * copying here, (as a pure convenience thing), but we can
2259 * keep heap costs out of the hot path unless someone else is
2260 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08002261 */
Mark Brownb3a223e2012-12-02 12:54:25 +09002262 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08002263 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
2264 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09002265 if (!local_buf)
2266 return -ENOMEM;
2267 } else {
2268 local_buf = buf;
2269 }
David Brownell8ae12a02006-01-08 13:34:19 -08002270
Vitaly Wool8275c642006-01-08 13:34:28 -08002271 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09002272 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07002273 if (n_tx) {
2274 x[0].len = n_tx;
2275 spi_message_add_tail(&x[0], &message);
2276 }
2277 if (n_rx) {
2278 x[1].len = n_rx;
2279 spi_message_add_tail(&x[1], &message);
2280 }
Vitaly Wool8275c642006-01-08 13:34:28 -08002281
David Brownell8ae12a02006-01-08 13:34:19 -08002282 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07002283 x[0].tx_buf = local_buf;
2284 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08002285
2286 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08002287 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08002288 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07002289 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08002290
David Brownellbdff5492009-04-13 14:39:57 -07002291 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08002292 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002293 else
2294 kfree(local_buf);
2295
2296 return status;
2297}
2298EXPORT_SYMBOL_GPL(spi_write_then_read);
2299
2300/*-------------------------------------------------------------------------*/
2301
2302static int __init spi_init(void)
2303{
David Brownellb8852442006-01-08 13:34:23 -08002304 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08002305
Christoph Lametere94b1762006-12-06 20:33:17 -08002306 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08002307 if (!buf) {
2308 status = -ENOMEM;
2309 goto err0;
2310 }
2311
2312 status = bus_register(&spi_bus_type);
2313 if (status < 0)
2314 goto err1;
2315
2316 status = class_register(&spi_master_class);
2317 if (status < 0)
2318 goto err2;
David Brownell8ae12a02006-01-08 13:34:19 -08002319 return 0;
David Brownellb8852442006-01-08 13:34:23 -08002320
2321err2:
2322 bus_unregister(&spi_bus_type);
2323err1:
2324 kfree(buf);
2325 buf = NULL;
2326err0:
2327 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08002328}
David Brownellb8852442006-01-08 13:34:23 -08002329
David Brownell8ae12a02006-01-08 13:34:19 -08002330/* board_info is normally registered in arch_initcall(),
2331 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08002332 *
2333 * REVISIT only boardinfo really needs static linking. the rest (device and
2334 * driver registration) _could_ be dynamically linked (modular) ... costs
2335 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08002336 */
David Brownell673c0c02008-10-15 22:02:46 -07002337postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08002338