blob: 3907f1493e7dcb6903120c9d24aee7a914dfddf8 [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>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020038#include <linux/pm_domain.h>
Paul Gortmaker025ed132011-07-10 12:57:55 -040039#include <linux/export.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060040#include <linux/sched/rt.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010041#include <linux/delay.h>
42#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010043#include <linux/ioport.h>
44#include <linux/acpi.h>
David Brownell8ae12a02006-01-08 13:34:19 -080045
Mark Brown56ec1972013-10-07 19:33:53 +010046#define CREATE_TRACE_POINTS
47#include <trace/events/spi.h>
48
David Brownell8ae12a02006-01-08 13:34:19 -080049static void spidev_release(struct device *dev)
50{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080051 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080052
53 /* spi masters may cleanup for released devices */
54 if (spi->master->cleanup)
55 spi->master->cleanup(spi);
56
David Brownell0c868462006-01-08 13:34:25 -080057 spi_master_put(spi->master);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000058 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080059}
60
61static ssize_t
62modalias_show(struct device *dev, struct device_attribute *a, char *buf)
63{
64 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080065 int len;
66
67 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
68 if (len != -ENODEV)
69 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080070
Grant Likelyd8e328b2012-05-20 00:08:13 -060071 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080072}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070073static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080074
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070075static struct attribute *spi_dev_attrs[] = {
76 &dev_attr_modalias.attr,
77 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -080078};
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070079ATTRIBUTE_GROUPS(spi_dev);
David Brownell8ae12a02006-01-08 13:34:19 -080080
81/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
82 * and the sysfs version makes coldplug work too.
83 */
84
Anton Vorontsov75368bf2009-09-22 16:46:04 -070085static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
86 const struct spi_device *sdev)
87{
88 while (id->name[0]) {
89 if (!strcmp(sdev->modalias, id->name))
90 return id;
91 id++;
92 }
93 return NULL;
94}
95
96const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
97{
98 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
99
100 return spi_match_id(sdrv->id_table, sdev);
101}
102EXPORT_SYMBOL_GPL(spi_get_device_id);
103
David Brownell8ae12a02006-01-08 13:34:19 -0800104static int spi_match_device(struct device *dev, struct device_driver *drv)
105{
106 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700107 const struct spi_driver *sdrv = to_spi_driver(drv);
108
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600109 /* Attempt an OF style match */
110 if (of_driver_match_device(dev, drv))
111 return 1;
112
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100113 /* Then try ACPI */
114 if (acpi_driver_match_device(dev, drv))
115 return 1;
116
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700117 if (sdrv->id_table)
118 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800119
Kay Sievers35f74fc2009-01-06 10:44:37 -0800120 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800121}
122
Kay Sievers7eff2e72007-08-14 15:15:12 +0200123static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800124{
125 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800126 int rc;
127
128 rc = acpi_device_uevent_modalias(dev, env);
129 if (rc != -ENODEV)
130 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800131
Anton Vorontsove0626e32009-09-22 16:46:08 -0700132 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800133 return 0;
134}
135
Mark Brown3ae22e82010-12-25 15:32:27 +0100136#ifdef CONFIG_PM_SLEEP
137static int spi_legacy_suspend(struct device *dev, pm_message_t message)
David Brownell8ae12a02006-01-08 13:34:19 -0800138{
David Brownell3c724262008-02-06 01:38:10 -0800139 int value = 0;
David Brownellb8852442006-01-08 13:34:23 -0800140 struct spi_driver *drv = to_spi_driver(dev->driver);
David Brownell8ae12a02006-01-08 13:34:19 -0800141
David Brownell8ae12a02006-01-08 13:34:19 -0800142 /* suspend will stop irqs and dma; no more i/o */
David Brownell3c724262008-02-06 01:38:10 -0800143 if (drv) {
144 if (drv->suspend)
145 value = drv->suspend(to_spi_device(dev), message);
146 else
147 dev_dbg(dev, "... can't suspend\n");
148 }
David Brownell8ae12a02006-01-08 13:34:19 -0800149 return value;
150}
151
Mark Brown3ae22e82010-12-25 15:32:27 +0100152static int spi_legacy_resume(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -0800153{
David Brownell3c724262008-02-06 01:38:10 -0800154 int value = 0;
David Brownellb8852442006-01-08 13:34:23 -0800155 struct spi_driver *drv = to_spi_driver(dev->driver);
David Brownell8ae12a02006-01-08 13:34:19 -0800156
David Brownell8ae12a02006-01-08 13:34:19 -0800157 /* resume may restart the i/o queue */
David Brownell3c724262008-02-06 01:38:10 -0800158 if (drv) {
159 if (drv->resume)
160 value = drv->resume(to_spi_device(dev));
161 else
162 dev_dbg(dev, "... can't resume\n");
163 }
David Brownell8ae12a02006-01-08 13:34:19 -0800164 return value;
165}
166
Mark Brown3ae22e82010-12-25 15:32:27 +0100167static int spi_pm_suspend(struct device *dev)
168{
169 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
170
171 if (pm)
172 return pm_generic_suspend(dev);
173 else
174 return spi_legacy_suspend(dev, PMSG_SUSPEND);
175}
176
177static int spi_pm_resume(struct device *dev)
178{
179 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
180
181 if (pm)
182 return pm_generic_resume(dev);
183 else
184 return spi_legacy_resume(dev);
185}
186
187static int spi_pm_freeze(struct device *dev)
188{
189 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
190
191 if (pm)
192 return pm_generic_freeze(dev);
193 else
194 return spi_legacy_suspend(dev, PMSG_FREEZE);
195}
196
197static int spi_pm_thaw(struct device *dev)
198{
199 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
200
201 if (pm)
202 return pm_generic_thaw(dev);
203 else
204 return spi_legacy_resume(dev);
205}
206
207static int spi_pm_poweroff(struct device *dev)
208{
209 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
210
211 if (pm)
212 return pm_generic_poweroff(dev);
213 else
214 return spi_legacy_suspend(dev, PMSG_HIBERNATE);
215}
216
217static int spi_pm_restore(struct device *dev)
218{
219 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
220
221 if (pm)
222 return pm_generic_restore(dev);
223 else
224 return spi_legacy_resume(dev);
225}
David Brownell8ae12a02006-01-08 13:34:19 -0800226#else
Mark Brown3ae22e82010-12-25 15:32:27 +0100227#define spi_pm_suspend NULL
228#define spi_pm_resume NULL
229#define spi_pm_freeze NULL
230#define spi_pm_thaw NULL
231#define spi_pm_poweroff NULL
232#define spi_pm_restore NULL
David Brownell8ae12a02006-01-08 13:34:19 -0800233#endif
234
Mark Brown3ae22e82010-12-25 15:32:27 +0100235static const struct dev_pm_ops spi_pm = {
236 .suspend = spi_pm_suspend,
237 .resume = spi_pm_resume,
238 .freeze = spi_pm_freeze,
239 .thaw = spi_pm_thaw,
240 .poweroff = spi_pm_poweroff,
241 .restore = spi_pm_restore,
242 SET_RUNTIME_PM_OPS(
243 pm_generic_runtime_suspend,
244 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200245 NULL
Mark Brown3ae22e82010-12-25 15:32:27 +0100246 )
247};
248
David Brownell8ae12a02006-01-08 13:34:19 -0800249struct bus_type spi_bus_type = {
250 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700251 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800252 .match = spi_match_device,
253 .uevent = spi_uevent,
Mark Brown3ae22e82010-12-25 15:32:27 +0100254 .pm = &spi_pm,
David Brownell8ae12a02006-01-08 13:34:19 -0800255};
256EXPORT_SYMBOL_GPL(spi_bus_type);
257
David Brownellb8852442006-01-08 13:34:23 -0800258
259static int spi_drv_probe(struct device *dev)
260{
261 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300262 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800263
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200264 ret = of_clk_set_defaults(dev->of_node, false);
265 if (ret)
266 return ret;
267
Ulf Hansson676e7c22014-09-19 20:27:41 +0200268 ret = dev_pm_domain_attach(dev, true);
269 if (ret != -EPROBE_DEFER) {
270 ret = sdrv->probe(to_spi_device(dev));
271 if (ret)
272 dev_pm_domain_detach(dev, true);
273 }
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300274
275 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800276}
277
278static int spi_drv_remove(struct device *dev)
279{
280 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300281 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800282
Jean Delvareaec35f42014-02-13 15:28:41 +0100283 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200284 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300285
286 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800287}
288
289static void spi_drv_shutdown(struct device *dev)
290{
291 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
292
293 sdrv->shutdown(to_spi_device(dev));
294}
295
David Brownell33e34dc2007-05-08 00:32:21 -0700296/**
297 * spi_register_driver - register a SPI driver
298 * @sdrv: the driver to register
299 * Context: can sleep
300 */
David Brownellb8852442006-01-08 13:34:23 -0800301int spi_register_driver(struct spi_driver *sdrv)
302{
303 sdrv->driver.bus = &spi_bus_type;
304 if (sdrv->probe)
305 sdrv->driver.probe = spi_drv_probe;
306 if (sdrv->remove)
307 sdrv->driver.remove = spi_drv_remove;
308 if (sdrv->shutdown)
309 sdrv->driver.shutdown = spi_drv_shutdown;
310 return driver_register(&sdrv->driver);
311}
312EXPORT_SYMBOL_GPL(spi_register_driver);
313
David Brownell8ae12a02006-01-08 13:34:19 -0800314/*-------------------------------------------------------------------------*/
315
316/* SPI devices should normally not be created by SPI device drivers; that
317 * would make them board-specific. Similarly with SPI master drivers.
318 * Device registration normally goes into like arch/.../mach.../board-YYY.c
319 * with other readonly (flashable) information about mainboard devices.
320 */
321
322struct boardinfo {
323 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800324 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800325};
326
327static LIST_HEAD(board_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800328static LIST_HEAD(spi_master_list);
329
330/*
331 * Used to protect add/del opertion for board_info list and
332 * spi_master list, and their matching process
333 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700334static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800335
Grant Likelydc87c982008-05-15 16:50:22 -0600336/**
337 * spi_alloc_device - Allocate a new SPI device
338 * @master: Controller to which device is connected
339 * Context: can sleep
340 *
341 * Allows a driver to allocate and initialize a spi_device without
342 * registering it immediately. This allows a driver to directly
343 * fill the spi_device with device parameters before calling
344 * spi_add_device() on it.
345 *
346 * Caller is responsible to call spi_add_device() on the returned
347 * spi_device structure to add it to the SPI master. If the caller
348 * needs to discard the spi_device without adding it, then it should
349 * call spi_dev_put() on it.
350 *
351 * Returns a pointer to the new device, or NULL.
352 */
353struct spi_device *spi_alloc_device(struct spi_master *master)
354{
355 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600356
357 if (!spi_master_get(master))
358 return NULL;
359
Jingoo Han5fe5f052013-10-14 10:31:51 +0900360 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600361 if (!spi) {
Grant Likelydc87c982008-05-15 16:50:22 -0600362 spi_master_put(master);
363 return NULL;
364 }
365
366 spi->master = master;
Laurent Pinchart178db7d2011-12-12 01:15:06 +0100367 spi->dev.parent = &master->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600368 spi->dev.bus = &spi_bus_type;
369 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100370 spi->cs_gpio = -ENOENT;
Grant Likelydc87c982008-05-15 16:50:22 -0600371 device_initialize(&spi->dev);
372 return spi;
373}
374EXPORT_SYMBOL_GPL(spi_alloc_device);
375
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200376static void spi_dev_set_name(struct spi_device *spi)
377{
378 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
379
380 if (adev) {
381 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
382 return;
383 }
384
385 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
386 spi->chip_select);
387}
388
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200389static int spi_dev_check(struct device *dev, void *data)
390{
391 struct spi_device *spi = to_spi_device(dev);
392 struct spi_device *new_spi = data;
393
394 if (spi->master == new_spi->master &&
395 spi->chip_select == new_spi->chip_select)
396 return -EBUSY;
397 return 0;
398}
399
Grant Likelydc87c982008-05-15 16:50:22 -0600400/**
401 * spi_add_device - Add spi_device allocated with spi_alloc_device
402 * @spi: spi_device to register
403 *
404 * Companion function to spi_alloc_device. Devices allocated with
405 * spi_alloc_device can be added onto the spi bus with this function.
406 *
David Brownelle48880e2008-08-15 00:40:44 -0700407 * Returns 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600408 */
409int spi_add_device(struct spi_device *spi)
410{
David Brownelle48880e2008-08-15 00:40:44 -0700411 static DEFINE_MUTEX(spi_add_lock);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100412 struct spi_master *master = spi->master;
413 struct device *dev = master->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600414 int status;
415
416 /* Chipselects are numbered 0..max; validate. */
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100417 if (spi->chip_select >= master->num_chipselect) {
Grant Likelydc87c982008-05-15 16:50:22 -0600418 dev_err(dev, "cs%d >= max %d\n",
419 spi->chip_select,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100420 master->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600421 return -EINVAL;
422 }
423
424 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200425 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700426
427 /* We need to make sure there's no other device with this
428 * chipselect **BEFORE** we call setup(), else we'll trash
429 * its configuration. Lock against concurrent add() calls.
430 */
431 mutex_lock(&spi_add_lock);
432
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200433 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
434 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700435 dev_err(dev, "chipselect %d already in use\n",
436 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700437 goto done;
438 }
439
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100440 if (master->cs_gpios)
441 spi->cs_gpio = master->cs_gpios[spi->chip_select];
442
David Brownelle48880e2008-08-15 00:40:44 -0700443 /* Drivers may modify this initial i/o setup, but will
444 * normally rely on the device being setup. Devices
445 * using SPI_CS_HIGH can't coexist well otherwise...
446 */
David Brownell7d077192009-06-17 16:26:03 -0700447 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600448 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200449 dev_err(dev, "can't setup %s, status %d\n",
450 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700451 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600452 }
453
David Brownelle48880e2008-08-15 00:40:44 -0700454 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600455 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700456 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200457 dev_err(dev, "can't add %s, status %d\n",
458 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700459 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800460 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600461
David Brownelle48880e2008-08-15 00:40:44 -0700462done:
463 mutex_unlock(&spi_add_lock);
464 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600465}
466EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800467
David Brownell33e34dc2007-05-08 00:32:21 -0700468/**
469 * spi_new_device - instantiate one new SPI device
470 * @master: Controller to which device is connected
471 * @chip: Describes the SPI device
472 * Context: can sleep
473 *
474 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800475 * after board init creates the hard-wired devices. Some development
476 * platforms may not be able to use spi_register_board_info though, and
477 * this is exported so that for example a USB or parport based adapter
478 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700479 *
480 * Returns the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800481 */
Adrian Bunke9d5a462007-03-26 21:32:23 -0800482struct spi_device *spi_new_device(struct spi_master *master,
483 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800484{
485 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800486 int status;
487
David Brownell082c8cb2007-07-31 00:39:45 -0700488 /* NOTE: caller did any chip->bus_num checks necessary.
489 *
490 * Also, unless we change the return value convention to use
491 * error-or-pointer (not NULL-or-pointer), troubleshootability
492 * suggests syslogged diagnostics are best here (ugh).
493 */
494
Grant Likelydc87c982008-05-15 16:50:22 -0600495 proxy = spi_alloc_device(master);
496 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800497 return NULL;
498
Grant Likely102eb972008-07-23 21:29:55 -0700499 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
500
David Brownell8ae12a02006-01-08 13:34:19 -0800501 proxy->chip_select = chip->chip_select;
502 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700503 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800504 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700505 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800506 proxy->dev.platform_data = (void *) chip->platform_data;
507 proxy->controller_data = chip->controller_data;
508 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800509
Grant Likelydc87c982008-05-15 16:50:22 -0600510 status = spi_add_device(proxy);
David Brownell8ae12a02006-01-08 13:34:19 -0800511 if (status < 0) {
Grant Likelydc87c982008-05-15 16:50:22 -0600512 spi_dev_put(proxy);
513 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800514 }
515
David Brownell8ae12a02006-01-08 13:34:19 -0800516 return proxy;
517}
518EXPORT_SYMBOL_GPL(spi_new_device);
519
Feng Tang2b9603a2010-08-02 15:52:15 +0800520static void spi_match_master_to_boardinfo(struct spi_master *master,
521 struct spi_board_info *bi)
522{
523 struct spi_device *dev;
524
525 if (master->bus_num != bi->bus_num)
526 return;
527
528 dev = spi_new_device(master, bi);
529 if (!dev)
530 dev_err(master->dev.parent, "can't create new device for %s\n",
531 bi->modalias);
532}
533
David Brownell33e34dc2007-05-08 00:32:21 -0700534/**
535 * spi_register_board_info - register SPI devices for a given board
536 * @info: array of chip descriptors
537 * @n: how many descriptors are provided
538 * Context: can sleep
539 *
David Brownell8ae12a02006-01-08 13:34:19 -0800540 * Board-specific early init code calls this (probably during arch_initcall)
541 * with segments of the SPI device table. Any device nodes are created later,
542 * after the relevant parent SPI controller (bus_num) is defined. We keep
543 * this table of devices forever, so that reloading a controller driver will
544 * not make Linux forget about these hard-wired devices.
545 *
546 * Other code can also call this, e.g. a particular add-on board might provide
547 * SPI devices through its expansion connector, so code initializing that board
548 * would naturally declare its SPI devices.
549 *
550 * The board info passed can safely be __initdata ... but be careful of
551 * any embedded pointers (platform_data, etc), they're copied as-is.
552 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000553int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800554{
Feng Tang2b9603a2010-08-02 15:52:15 +0800555 struct boardinfo *bi;
556 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800557
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
Wolfram Sang16735d02013-11-14 14:32:02 -0800795 reinit_completion(&master->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +0100796
797 ret = master->transfer_one(master, msg->spi, xfer);
798 if (ret < 0) {
799 dev_err(&msg->spi->dev,
800 "SPI transfer failed: %d\n", ret);
801 goto out;
802 }
803
Axel Lin13a42792014-01-18 22:05:22 +0800804 if (ret > 0) {
805 ret = 0;
Mark Brown16a0ce42014-01-30 22:16:41 +0000806 ms = xfer->len * 8 * 1000 / xfer->speed_hz;
Harini Katakameee668a2014-04-11 12:06:28 +0530807 ms += ms + 100; /* some tolerance */
Mark Brown16a0ce42014-01-30 22:16:41 +0000808
809 ms = wait_for_completion_timeout(&master->xfer_completion,
810 msecs_to_jiffies(ms));
811 }
812
813 if (ms == 0) {
814 dev_err(&msg->spi->dev, "SPI transfer timed out\n");
815 msg->status = -ETIMEDOUT;
Axel Lin13a42792014-01-18 22:05:22 +0800816 }
Mark Brownb1589352013-10-05 11:50:40 +0100817
818 trace_spi_transfer_stop(msg, xfer);
819
820 if (msg->status != -EINPROGRESS)
821 goto out;
822
823 if (xfer->delay_usecs)
824 udelay(xfer->delay_usecs);
825
826 if (xfer->cs_change) {
827 if (list_is_last(&xfer->transfer_list,
828 &msg->transfers)) {
829 keep_cs = true;
830 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +0000831 spi_set_cs(msg->spi, false);
832 udelay(10);
833 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +0100834 }
835 }
836
837 msg->actual_length += xfer->len;
838 }
839
840out:
841 if (ret != 0 || !keep_cs)
842 spi_set_cs(msg->spi, false);
843
844 if (msg->status == -EINPROGRESS)
845 msg->status = ret;
846
847 spi_finalize_current_message(master);
848
849 return ret;
850}
851
852/**
853 * spi_finalize_current_transfer - report completion of a transfer
Thierry Reding2c675682014-08-08 13:02:36 +0200854 * @master: the master reporting completion
Mark Brownb1589352013-10-05 11:50:40 +0100855 *
856 * Called by SPI drivers using the core transfer_one_message()
857 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +0100858 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +0100859 */
860void spi_finalize_current_transfer(struct spi_master *master)
861{
862 complete(&master->xfer_completion);
863}
864EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
865
Linus Walleijffbbdd212012-02-22 10:05:38 +0100866/**
867 * spi_pump_messages - kthread work function which processes spi message queue
868 * @work: pointer to kthread work struct contained in the master struct
869 *
870 * This function checks if there is any spi message in the queue that
871 * needs processing and if so call out to the driver to initialize hardware
872 * and transfer each message.
873 *
874 */
875static void spi_pump_messages(struct kthread_work *work)
876{
877 struct spi_master *master =
878 container_of(work, struct spi_master, pump_messages);
879 unsigned long flags;
880 bool was_busy = false;
881 int ret;
882
883 /* Lock queue and check for queue work */
884 spin_lock_irqsave(&master->queue_lock, flags);
885 if (list_empty(&master->queue) || !master->running) {
Bryan Freedb0b36b82013-03-13 11:17:40 -0700886 if (!master->busy) {
887 spin_unlock_irqrestore(&master->queue_lock, flags);
888 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +0100889 }
890 master->busy = false;
891 spin_unlock_irqrestore(&master->queue_lock, flags);
Mark Brown3a2eba92014-01-28 20:17:03 +0000892 kfree(master->dummy_rx);
893 master->dummy_rx = NULL;
894 kfree(master->dummy_tx);
895 master->dummy_tx = NULL;
Bryan Freedb0b36b82013-03-13 11:17:40 -0700896 if (master->unprepare_transfer_hardware &&
897 master->unprepare_transfer_hardware(master))
898 dev_err(&master->dev,
899 "failed to unprepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +0100900 if (master->auto_runtime_pm) {
901 pm_runtime_mark_last_busy(master->dev.parent);
902 pm_runtime_put_autosuspend(master->dev.parent);
903 }
Mark Brown56ec1972013-10-07 19:33:53 +0100904 trace_spi_master_idle(master);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100905 return;
906 }
907
908 /* Make sure we are not already running a message */
909 if (master->cur_msg) {
910 spin_unlock_irqrestore(&master->queue_lock, flags);
911 return;
912 }
913 /* Extract head of queue */
914 master->cur_msg =
Axel Lina89e2d22014-01-09 16:03:58 +0800915 list_first_entry(&master->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100916
917 list_del_init(&master->cur_msg->queue);
918 if (master->busy)
919 was_busy = true;
920 else
921 master->busy = true;
922 spin_unlock_irqrestore(&master->queue_lock, flags);
923
Mark Brown49834de2013-07-28 14:47:02 +0100924 if (!was_busy && master->auto_runtime_pm) {
925 ret = pm_runtime_get_sync(master->dev.parent);
926 if (ret < 0) {
927 dev_err(&master->dev, "Failed to power device: %d\n",
928 ret);
929 return;
930 }
931 }
932
Mark Brown56ec1972013-10-07 19:33:53 +0100933 if (!was_busy)
934 trace_spi_master_busy(master);
935
Shubhrajyoti D7dfd2bd2012-05-10 19:20:41 +0530936 if (!was_busy && master->prepare_transfer_hardware) {
Linus Walleijffbbdd212012-02-22 10:05:38 +0100937 ret = master->prepare_transfer_hardware(master);
938 if (ret) {
939 dev_err(&master->dev,
940 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +0100941
942 if (master->auto_runtime_pm)
943 pm_runtime_put(master->dev.parent);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100944 return;
945 }
946 }
947
Mark Brown56ec1972013-10-07 19:33:53 +0100948 trace_spi_message_start(master->cur_msg);
949
Mark Brown2841a5f2013-10-05 00:23:12 +0100950 if (master->prepare_message) {
951 ret = master->prepare_message(master, master->cur_msg);
952 if (ret) {
953 dev_err(&master->dev,
954 "failed to prepare message: %d\n", ret);
955 master->cur_msg->status = ret;
956 spi_finalize_current_message(master);
957 return;
958 }
959 master->cur_msg_prepared = true;
960 }
961
Mark Brown99adef32014-01-16 12:22:43 +0000962 ret = spi_map_msg(master, master->cur_msg);
963 if (ret) {
964 master->cur_msg->status = ret;
965 spi_finalize_current_message(master);
966 return;
967 }
968
Linus Walleijffbbdd212012-02-22 10:05:38 +0100969 ret = master->transfer_one_message(master, master->cur_msg);
970 if (ret) {
971 dev_err(&master->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +0100972 "failed to transfer one message from queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +0100973 return;
974 }
975}
976
977static int spi_init_queue(struct spi_master *master)
978{
979 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
980
981 INIT_LIST_HEAD(&master->queue);
982 spin_lock_init(&master->queue_lock);
983
984 master->running = false;
985 master->busy = false;
986
987 init_kthread_worker(&master->kworker);
988 master->kworker_task = kthread_run(kthread_worker_fn,
Kees Cookf1701682013-07-03 15:04:58 -0700989 &master->kworker, "%s",
Linus Walleijffbbdd212012-02-22 10:05:38 +0100990 dev_name(&master->dev));
991 if (IS_ERR(master->kworker_task)) {
992 dev_err(&master->dev, "failed to create message pump task\n");
993 return -ENOMEM;
994 }
995 init_kthread_work(&master->pump_messages, spi_pump_messages);
996
997 /*
998 * Master config will indicate if this controller should run the
999 * message pump with high (realtime) priority to reduce the transfer
1000 * latency on the bus by minimising the delay between a transfer
1001 * request and the scheduling of the message pump thread. Without this
1002 * setting the message pump thread will remain at default priority.
1003 */
1004 if (master->rt) {
1005 dev_info(&master->dev,
1006 "will run message pump with realtime priority\n");
1007 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
1008 }
1009
1010 return 0;
1011}
1012
1013/**
1014 * spi_get_next_queued_message() - called by driver to check for queued
1015 * messages
1016 * @master: the master to check for queued messages
1017 *
1018 * If there are more messages in the queue, the next message is returned from
1019 * this call.
1020 */
1021struct spi_message *spi_get_next_queued_message(struct spi_master *master)
1022{
1023 struct spi_message *next;
1024 unsigned long flags;
1025
1026 /* get a pointer to the next message, if any */
1027 spin_lock_irqsave(&master->queue_lock, flags);
Axel Lin1cfd97f2014-01-02 15:16:40 +08001028 next = list_first_entry_or_null(&master->queue, struct spi_message,
1029 queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001030 spin_unlock_irqrestore(&master->queue_lock, flags);
1031
1032 return next;
1033}
1034EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1035
1036/**
1037 * spi_finalize_current_message() - the current message is complete
1038 * @master: the master to return the message to
1039 *
1040 * Called by the driver to notify the core that the message in the front of the
1041 * queue is complete and can be removed from the queue.
1042 */
1043void spi_finalize_current_message(struct spi_master *master)
1044{
1045 struct spi_message *mesg;
1046 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001047 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001048
1049 spin_lock_irqsave(&master->queue_lock, flags);
1050 mesg = master->cur_msg;
1051 master->cur_msg = NULL;
1052
1053 queue_kthread_work(&master->kworker, &master->pump_messages);
1054 spin_unlock_irqrestore(&master->queue_lock, flags);
1055
Mark Brown99adef32014-01-16 12:22:43 +00001056 spi_unmap_msg(master, mesg);
1057
Mark Brown2841a5f2013-10-05 00:23:12 +01001058 if (master->cur_msg_prepared && master->unprepare_message) {
1059 ret = master->unprepare_message(master, mesg);
1060 if (ret) {
1061 dev_err(&master->dev,
1062 "failed to unprepare message: %d\n", ret);
1063 }
1064 }
1065 master->cur_msg_prepared = false;
1066
Linus Walleijffbbdd212012-02-22 10:05:38 +01001067 mesg->state = NULL;
1068 if (mesg->complete)
1069 mesg->complete(mesg->context);
Mark Brown56ec1972013-10-07 19:33:53 +01001070
1071 trace_spi_message_done(mesg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001072}
1073EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1074
1075static int spi_start_queue(struct spi_master *master)
1076{
1077 unsigned long flags;
1078
1079 spin_lock_irqsave(&master->queue_lock, flags);
1080
1081 if (master->running || master->busy) {
1082 spin_unlock_irqrestore(&master->queue_lock, flags);
1083 return -EBUSY;
1084 }
1085
1086 master->running = true;
1087 master->cur_msg = NULL;
1088 spin_unlock_irqrestore(&master->queue_lock, flags);
1089
1090 queue_kthread_work(&master->kworker, &master->pump_messages);
1091
1092 return 0;
1093}
1094
1095static int spi_stop_queue(struct spi_master *master)
1096{
1097 unsigned long flags;
1098 unsigned limit = 500;
1099 int ret = 0;
1100
1101 spin_lock_irqsave(&master->queue_lock, flags);
1102
1103 /*
1104 * This is a bit lame, but is optimized for the common execution path.
1105 * A wait_queue on the master->busy could be used, but then the common
1106 * execution path (pump_messages) would be required to call wake_up or
1107 * friends on every SPI message. Do this instead.
1108 */
1109 while ((!list_empty(&master->queue) || master->busy) && limit--) {
1110 spin_unlock_irqrestore(&master->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001111 usleep_range(10000, 11000);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001112 spin_lock_irqsave(&master->queue_lock, flags);
1113 }
1114
1115 if (!list_empty(&master->queue) || master->busy)
1116 ret = -EBUSY;
1117 else
1118 master->running = false;
1119
1120 spin_unlock_irqrestore(&master->queue_lock, flags);
1121
1122 if (ret) {
1123 dev_warn(&master->dev,
1124 "could not stop message queue\n");
1125 return ret;
1126 }
1127 return ret;
1128}
1129
1130static int spi_destroy_queue(struct spi_master *master)
1131{
1132 int ret;
1133
1134 ret = spi_stop_queue(master);
1135
1136 /*
1137 * flush_kthread_worker will block until all work is done.
1138 * If the reason that stop_queue timed out is that the work will never
1139 * finish, then it does no good to call flush/stop thread, so
1140 * return anyway.
1141 */
1142 if (ret) {
1143 dev_err(&master->dev, "problem destroying queue\n");
1144 return ret;
1145 }
1146
1147 flush_kthread_worker(&master->kworker);
1148 kthread_stop(master->kworker_task);
1149
1150 return 0;
1151}
1152
1153/**
1154 * spi_queued_transfer - transfer function for queued transfers
1155 * @spi: spi device which is requesting transfer
1156 * @msg: spi message which is to handled is queued to driver queue
1157 */
1158static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1159{
1160 struct spi_master *master = spi->master;
1161 unsigned long flags;
1162
1163 spin_lock_irqsave(&master->queue_lock, flags);
1164
1165 if (!master->running) {
1166 spin_unlock_irqrestore(&master->queue_lock, flags);
1167 return -ESHUTDOWN;
1168 }
1169 msg->actual_length = 0;
1170 msg->status = -EINPROGRESS;
1171
1172 list_add_tail(&msg->queue, &master->queue);
Axel Lin96b3eac2013-08-22 23:41:34 +08001173 if (!master->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001174 queue_kthread_work(&master->kworker, &master->pump_messages);
1175
1176 spin_unlock_irqrestore(&master->queue_lock, flags);
1177 return 0;
1178}
1179
1180static int spi_master_initialize_queue(struct spi_master *master)
1181{
1182 int ret;
1183
Linus Walleijffbbdd212012-02-22 10:05:38 +01001184 master->transfer = spi_queued_transfer;
Mark Brownb1589352013-10-05 11:50:40 +01001185 if (!master->transfer_one_message)
1186 master->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001187
1188 /* Initialize and start queue */
1189 ret = spi_init_queue(master);
1190 if (ret) {
1191 dev_err(&master->dev, "problem initializing queue\n");
1192 goto err_init_queue;
1193 }
Mark Brownc3676d52014-05-01 10:47:52 -07001194 master->queued = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001195 ret = spi_start_queue(master);
1196 if (ret) {
1197 dev_err(&master->dev, "problem starting queue\n");
1198 goto err_start_queue;
1199 }
1200
1201 return 0;
1202
1203err_start_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001204 spi_destroy_queue(master);
Mark Brownc3676d52014-05-01 10:47:52 -07001205err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001206 return ret;
1207}
1208
1209/*-------------------------------------------------------------------------*/
1210
Andreas Larsson7cb94362012-12-04 15:09:38 +01001211#if defined(CONFIG_OF)
Grant Likelyd57a4282012-04-07 14:16:53 -06001212/**
1213 * of_register_spi_devices() - Register child devices onto the SPI bus
1214 * @master: Pointer to spi_master device
1215 *
1216 * Registers an spi_device for each child node of master node which has a 'reg'
1217 * property.
1218 */
1219static void of_register_spi_devices(struct spi_master *master)
1220{
1221 struct spi_device *spi;
1222 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001223 int rc;
Trent Piepho89da4292013-09-27 05:37:25 -07001224 u32 value;
Grant Likelyd57a4282012-04-07 14:16:53 -06001225
1226 if (!master->dev.of_node)
1227 return;
1228
Alexander Sverdlinf3b61592012-11-29 08:59:29 +01001229 for_each_available_child_of_node(master->dev.of_node, nc) {
Grant Likelyd57a4282012-04-07 14:16:53 -06001230 /* Alloc an spi_device */
1231 spi = spi_alloc_device(master);
1232 if (!spi) {
1233 dev_err(&master->dev, "spi_device alloc error for %s\n",
1234 nc->full_name);
1235 spi_dev_put(spi);
1236 continue;
1237 }
1238
1239 /* Select device driver */
1240 if (of_modalias_node(nc, spi->modalias,
1241 sizeof(spi->modalias)) < 0) {
1242 dev_err(&master->dev, "cannot find modalias for %s\n",
1243 nc->full_name);
1244 spi_dev_put(spi);
1245 continue;
1246 }
1247
1248 /* Device address */
Trent Piepho89da4292013-09-27 05:37:25 -07001249 rc = of_property_read_u32(nc, "reg", &value);
1250 if (rc) {
1251 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1252 nc->full_name, rc);
Grant Likelyd57a4282012-04-07 14:16:53 -06001253 spi_dev_put(spi);
1254 continue;
1255 }
Trent Piepho89da4292013-09-27 05:37:25 -07001256 spi->chip_select = value;
Grant Likelyd57a4282012-04-07 14:16:53 -06001257
1258 /* Mode (clock phase/polarity/etc.) */
1259 if (of_find_property(nc, "spi-cpha", NULL))
1260 spi->mode |= SPI_CPHA;
1261 if (of_find_property(nc, "spi-cpol", NULL))
1262 spi->mode |= SPI_CPOL;
1263 if (of_find_property(nc, "spi-cs-high", NULL))
1264 spi->mode |= SPI_CS_HIGH;
Lars-Peter Clausenc20151d2012-12-06 16:55:33 +01001265 if (of_find_property(nc, "spi-3wire", NULL))
1266 spi->mode |= SPI_3WIRE;
Zhao Qiangcd6339e2014-04-01 17:10:50 +08001267 if (of_find_property(nc, "spi-lsb-first", NULL))
1268 spi->mode |= SPI_LSB_FIRST;
Grant Likelyd57a4282012-04-07 14:16:53 -06001269
wangyuhangf477b7f2013-08-11 18:15:17 +08001270 /* Device DUAL/QUAD mode */
Trent Piepho89da4292013-09-27 05:37:25 -07001271 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1272 switch (value) {
1273 case 1:
Mark Browna822e992013-08-30 23:19:40 +01001274 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001275 case 2:
Mark Browna822e992013-08-30 23:19:40 +01001276 spi->mode |= SPI_TX_DUAL;
1277 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001278 case 4:
Mark Browna822e992013-08-30 23:19:40 +01001279 spi->mode |= SPI_TX_QUAD;
1280 break;
1281 default:
Geert Uytterhoeven80874d82014-05-26 14:05:25 +02001282 dev_warn(&master->dev,
1283 "spi-tx-bus-width %d not supported\n",
1284 value);
1285 break;
Mark Browna822e992013-08-30 23:19:40 +01001286 }
wangyuhangf477b7f2013-08-11 18:15:17 +08001287 }
1288
Trent Piepho89da4292013-09-27 05:37:25 -07001289 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1290 switch (value) {
1291 case 1:
Mark Browna822e992013-08-30 23:19:40 +01001292 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001293 case 2:
Mark Browna822e992013-08-30 23:19:40 +01001294 spi->mode |= SPI_RX_DUAL;
1295 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001296 case 4:
Mark Browna822e992013-08-30 23:19:40 +01001297 spi->mode |= SPI_RX_QUAD;
1298 break;
1299 default:
Geert Uytterhoeven80874d82014-05-26 14:05:25 +02001300 dev_warn(&master->dev,
1301 "spi-rx-bus-width %d not supported\n",
1302 value);
1303 break;
Mark Browna822e992013-08-30 23:19:40 +01001304 }
wangyuhangf477b7f2013-08-11 18:15:17 +08001305 }
1306
Grant Likelyd57a4282012-04-07 14:16:53 -06001307 /* Device speed */
Trent Piepho89da4292013-09-27 05:37:25 -07001308 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1309 if (rc) {
1310 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1311 nc->full_name, rc);
Grant Likelyd57a4282012-04-07 14:16:53 -06001312 spi_dev_put(spi);
1313 continue;
1314 }
Trent Piepho89da4292013-09-27 05:37:25 -07001315 spi->max_speed_hz = value;
Grant Likelyd57a4282012-04-07 14:16:53 -06001316
1317 /* IRQ */
1318 spi->irq = irq_of_parse_and_map(nc, 0);
1319
1320 /* Store a pointer to the node in the device structure */
1321 of_node_get(nc);
1322 spi->dev.of_node = nc;
1323
1324 /* Register the new device */
Mathias Krause70fac172013-08-31 20:24:14 +02001325 request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
Grant Likelyd57a4282012-04-07 14:16:53 -06001326 rc = spi_add_device(spi);
1327 if (rc) {
1328 dev_err(&master->dev, "spi_device register error %s\n",
1329 nc->full_name);
1330 spi_dev_put(spi);
1331 }
1332
1333 }
1334}
1335#else
1336static void of_register_spi_devices(struct spi_master *master) { }
1337#endif
1338
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001339#ifdef CONFIG_ACPI
1340static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1341{
1342 struct spi_device *spi = data;
1343
1344 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1345 struct acpi_resource_spi_serialbus *sb;
1346
1347 sb = &ares->data.spi_serial_bus;
1348 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1349 spi->chip_select = sb->device_selection;
1350 spi->max_speed_hz = sb->connection_speed;
1351
1352 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1353 spi->mode |= SPI_CPHA;
1354 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1355 spi->mode |= SPI_CPOL;
1356 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1357 spi->mode |= SPI_CS_HIGH;
1358 }
1359 } else if (spi->irq < 0) {
1360 struct resource r;
1361
1362 if (acpi_dev_resource_interrupt(ares, 0, &r))
1363 spi->irq = r.start;
1364 }
1365
1366 /* Always tell the ACPI core to skip this resource */
1367 return 1;
1368}
1369
1370static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1371 void *data, void **return_value)
1372{
1373 struct spi_master *master = data;
1374 struct list_head resource_list;
1375 struct acpi_device *adev;
1376 struct spi_device *spi;
1377 int ret;
1378
1379 if (acpi_bus_get_device(handle, &adev))
1380 return AE_OK;
1381 if (acpi_bus_get_status(adev) || !adev->status.present)
1382 return AE_OK;
1383
1384 spi = spi_alloc_device(master);
1385 if (!spi) {
1386 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1387 dev_name(&adev->dev));
1388 return AE_NO_MEMORY;
1389 }
1390
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001391 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001392 spi->irq = -1;
1393
1394 INIT_LIST_HEAD(&resource_list);
1395 ret = acpi_dev_get_resources(adev, &resource_list,
1396 acpi_spi_add_resource, spi);
1397 acpi_dev_free_resource_list(&resource_list);
1398
1399 if (ret < 0 || !spi->max_speed_hz) {
1400 spi_dev_put(spi);
1401 return AE_OK;
1402 }
1403
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001404 adev->power.flags.ignore_parent = true;
Jarkko Nikulacf9eb392013-10-10 17:19:17 +03001405 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001406 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001407 adev->power.flags.ignore_parent = false;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001408 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1409 dev_name(&adev->dev));
1410 spi_dev_put(spi);
1411 }
1412
1413 return AE_OK;
1414}
1415
1416static void acpi_register_spi_devices(struct spi_master *master)
1417{
1418 acpi_status status;
1419 acpi_handle handle;
1420
Rafael J. Wysocki29896172013-04-01 00:21:08 +00001421 handle = ACPI_HANDLE(master->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001422 if (!handle)
1423 return;
1424
1425 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1426 acpi_spi_add_device, NULL,
1427 master, NULL);
1428 if (ACPI_FAILURE(status))
1429 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1430}
1431#else
1432static inline void acpi_register_spi_devices(struct spi_master *master) {}
1433#endif /* CONFIG_ACPI */
1434
Tony Jones49dce682007-10-16 01:27:48 -07001435static void spi_master_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001436{
1437 struct spi_master *master;
1438
Tony Jones49dce682007-10-16 01:27:48 -07001439 master = container_of(dev, struct spi_master, dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001440 kfree(master);
1441}
1442
1443static struct class spi_master_class = {
1444 .name = "spi_master",
1445 .owner = THIS_MODULE,
Tony Jones49dce682007-10-16 01:27:48 -07001446 .dev_release = spi_master_release,
David Brownell8ae12a02006-01-08 13:34:19 -08001447};
1448
1449
Linus Walleijffbbdd212012-02-22 10:05:38 +01001450
David Brownell8ae12a02006-01-08 13:34:19 -08001451/**
1452 * spi_alloc_master - allocate SPI master controller
1453 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001454 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001455 * memory is in the driver_data field of the returned device,
David Brownell0c868462006-01-08 13:34:25 -08001456 * accessible with spi_master_get_devdata().
David Brownell33e34dc2007-05-08 00:32:21 -07001457 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001458 *
1459 * This call is used only by SPI master controller drivers, which are the
1460 * only ones directly touching chip registers. It's how they allocate
dmitry pervushinba1a0512006-05-20 15:00:14 -07001461 * an spi_master structure, prior to calling spi_register_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001462 *
1463 * This must be called from context that can sleep. It returns the SPI
1464 * master structure on success, else NULL.
1465 *
1466 * The caller is responsible for assigning the bus number and initializing
dmitry pervushinba1a0512006-05-20 15:00:14 -07001467 * the master's methods before calling spi_register_master(); and (after errors
Uwe Kleine-Königeb4af0f2012-02-23 10:40:14 +01001468 * adding the device) calling spi_master_put() and kfree() to prevent a memory
1469 * leak.
David Brownell8ae12a02006-01-08 13:34:19 -08001470 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001471struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
David Brownell8ae12a02006-01-08 13:34:19 -08001472{
1473 struct spi_master *master;
1474
David Brownell0c868462006-01-08 13:34:25 -08001475 if (!dev)
1476 return NULL;
1477
Jingoo Han5fe5f052013-10-14 10:31:51 +09001478 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -08001479 if (!master)
1480 return NULL;
1481
Tony Jones49dce682007-10-16 01:27:48 -07001482 device_initialize(&master->dev);
Grant Likely1e8a52e2012-05-19 23:42:08 -06001483 master->bus_num = -1;
1484 master->num_chipselect = 1;
Tony Jones49dce682007-10-16 01:27:48 -07001485 master->dev.class = &spi_master_class;
1486 master->dev.parent = get_device(dev);
David Brownell0c868462006-01-08 13:34:25 -08001487 spi_master_set_devdata(master, &master[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001488
1489 return master;
1490}
1491EXPORT_SYMBOL_GPL(spi_alloc_master);
1492
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001493#ifdef CONFIG_OF
1494static int of_spi_register_master(struct spi_master *master)
1495{
Grant Likelye80beb22013-02-12 17:48:37 +00001496 int nb, i, *cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001497 struct device_node *np = master->dev.of_node;
1498
1499 if (!np)
1500 return 0;
1501
1502 nb = of_gpio_named_count(np, "cs-gpios");
Jingoo Han5fe5f052013-10-14 10:31:51 +09001503 master->num_chipselect = max_t(int, nb, master->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001504
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001505 /* Return error only for an incorrectly formed cs-gpios property */
1506 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001507 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001508 else if (nb < 0)
1509 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001510
1511 cs = devm_kzalloc(&master->dev,
1512 sizeof(int) * master->num_chipselect,
1513 GFP_KERNEL);
1514 master->cs_gpios = cs;
1515
1516 if (!master->cs_gpios)
1517 return -ENOMEM;
1518
Andreas Larsson0da83bb2013-01-29 15:53:40 +01001519 for (i = 0; i < master->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01001520 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001521
1522 for (i = 0; i < nb; i++)
1523 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1524
1525 return 0;
1526}
1527#else
1528static int of_spi_register_master(struct spi_master *master)
1529{
1530 return 0;
1531}
1532#endif
1533
David Brownell8ae12a02006-01-08 13:34:19 -08001534/**
1535 * spi_register_master - register SPI master controller
1536 * @master: initialized master, originally from spi_alloc_master()
David Brownell33e34dc2007-05-08 00:32:21 -07001537 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001538 *
1539 * SPI master controllers connect to their drivers using some non-SPI bus,
1540 * such as the platform bus. The final stage of probe() in that code
1541 * includes calling spi_register_master() to hook up to this SPI bus glue.
1542 *
1543 * SPI controllers use board specific (often SOC specific) bus numbers,
1544 * and board-specific addressing for SPI devices combines those numbers
1545 * with chip select numbers. Since SPI does not directly support dynamic
1546 * device identification, boards need configuration tables telling which
1547 * chip is at which address.
1548 *
1549 * This must be called from context that can sleep. It returns zero on
1550 * success, else a negative error code (dropping the master's refcount).
David Brownell0c868462006-01-08 13:34:25 -08001551 * After a successful return, the caller is responsible for calling
1552 * spi_unregister_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001553 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001554int spi_register_master(struct spi_master *master)
David Brownell8ae12a02006-01-08 13:34:19 -08001555{
David Brownelle44a45a2007-06-03 13:50:40 -07001556 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Tony Jones49dce682007-10-16 01:27:48 -07001557 struct device *dev = master->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08001558 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08001559 int status = -ENODEV;
1560 int dynamic = 0;
1561
David Brownell0c868462006-01-08 13:34:25 -08001562 if (!dev)
1563 return -ENODEV;
1564
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001565 status = of_spi_register_master(master);
1566 if (status)
1567 return status;
1568
David Brownell082c8cb2007-07-31 00:39:45 -07001569 /* even if it's just one always-selected device, there must
1570 * be at least one chipselect
1571 */
1572 if (master->num_chipselect == 0)
1573 return -EINVAL;
1574
Grant Likelybb297852012-12-21 19:32:09 +00001575 if ((master->bus_num < 0) && master->dev.of_node)
1576 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1577
David Brownell8ae12a02006-01-08 13:34:19 -08001578 /* convention: dynamically assigned bus IDs count down from the max */
David Brownella020ed72006-04-03 15:49:04 -07001579 if (master->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07001580 /* FIXME switch to an IDR based scheme, something like
1581 * I2C now uses, so we can't run out of "dynamic" IDs
1582 */
David Brownell8ae12a02006-01-08 13:34:19 -08001583 master->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08001584 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08001585 }
1586
Ernst Schwabcf32b712010-06-28 17:49:29 -07001587 spin_lock_init(&master->bus_lock_spinlock);
1588 mutex_init(&master->bus_lock_mutex);
1589 master->bus_lock_flag = 0;
Mark Brownb1589352013-10-05 11:50:40 +01001590 init_completion(&master->xfer_completion);
Mark Brown6ad45a22014-02-02 13:47:47 +00001591 if (!master->max_dma_len)
1592 master->max_dma_len = INT_MAX;
Ernst Schwabcf32b712010-06-28 17:49:29 -07001593
David Brownell8ae12a02006-01-08 13:34:19 -08001594 /* register the device, then userspace will see it.
1595 * registration fails if the bus ID is in use.
1596 */
Kay Sievers35f74fc2009-01-06 10:44:37 -08001597 dev_set_name(&master->dev, "spi%u", master->bus_num);
Tony Jones49dce682007-10-16 01:27:48 -07001598 status = device_add(&master->dev);
David Brownellb8852442006-01-08 13:34:23 -08001599 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08001600 goto done;
Kay Sievers35f74fc2009-01-06 10:44:37 -08001601 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
David Brownell8ae12a02006-01-08 13:34:19 -08001602 dynamic ? " (dynamic)" : "");
1603
Linus Walleijffbbdd212012-02-22 10:05:38 +01001604 /* If we're using a queued driver, start the queue */
1605 if (master->transfer)
1606 dev_info(dev, "master is unqueued, this is deprecated\n");
1607 else {
1608 status = spi_master_initialize_queue(master);
1609 if (status) {
Axel Line93b0722013-08-31 20:25:52 +08001610 device_del(&master->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001611 goto done;
1612 }
1613 }
1614
Feng Tang2b9603a2010-08-02 15:52:15 +08001615 mutex_lock(&board_lock);
1616 list_add_tail(&master->list, &spi_master_list);
1617 list_for_each_entry(bi, &board_list, list)
1618 spi_match_master_to_boardinfo(master, &bi->board_info);
1619 mutex_unlock(&board_lock);
1620
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001621 /* Register devices from the device tree and ACPI */
Anatolij Gustschin12b15e82010-07-27 22:35:58 +02001622 of_register_spi_devices(master);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001623 acpi_register_spi_devices(master);
David Brownell8ae12a02006-01-08 13:34:19 -08001624done:
1625 return status;
1626}
1627EXPORT_SYMBOL_GPL(spi_register_master);
1628
Mark Brown666d5b42013-08-31 18:50:52 +01001629static void devm_spi_unregister(struct device *dev, void *res)
1630{
1631 spi_unregister_master(*(struct spi_master **)res);
1632}
1633
1634/**
1635 * dev_spi_register_master - register managed SPI master controller
1636 * @dev: device managing SPI master
1637 * @master: initialized master, originally from spi_alloc_master()
1638 * Context: can sleep
1639 *
1640 * Register a SPI device as with spi_register_master() which will
1641 * automatically be unregister
1642 */
1643int devm_spi_register_master(struct device *dev, struct spi_master *master)
1644{
1645 struct spi_master **ptr;
1646 int ret;
1647
1648 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1649 if (!ptr)
1650 return -ENOMEM;
1651
1652 ret = spi_register_master(master);
Stephen Warren4b928942013-11-21 16:11:15 -07001653 if (!ret) {
Mark Brown666d5b42013-08-31 18:50:52 +01001654 *ptr = master;
1655 devres_add(dev, ptr);
1656 } else {
1657 devres_free(ptr);
1658 }
1659
1660 return ret;
1661}
1662EXPORT_SYMBOL_GPL(devm_spi_register_master);
1663
David Lamparter34860082010-08-30 23:54:17 +02001664static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08001665{
David Lamparter34860082010-08-30 23:54:17 +02001666 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08001667 return 0;
1668}
1669
1670/**
1671 * spi_unregister_master - unregister SPI master controller
1672 * @master: the master being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07001673 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001674 *
1675 * This call is used only by SPI master controller drivers, which are the
1676 * only ones directly touching chip registers.
1677 *
1678 * This must be called from context that can sleep.
1679 */
1680void spi_unregister_master(struct spi_master *master)
1681{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08001682 int dummy;
1683
Linus Walleijffbbdd212012-02-22 10:05:38 +01001684 if (master->queued) {
1685 if (spi_destroy_queue(master))
1686 dev_err(&master->dev, "queue remove failed\n");
1687 }
1688
Feng Tang2b9603a2010-08-02 15:52:15 +08001689 mutex_lock(&board_lock);
1690 list_del(&master->list);
1691 mutex_unlock(&board_lock);
1692
Sebastian Andrzej Siewior97dbf372010-12-21 17:24:31 -08001693 dummy = device_for_each_child(&master->dev, NULL, __unregister);
Tony Jones49dce682007-10-16 01:27:48 -07001694 device_unregister(&master->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001695}
1696EXPORT_SYMBOL_GPL(spi_unregister_master);
1697
Linus Walleijffbbdd212012-02-22 10:05:38 +01001698int spi_master_suspend(struct spi_master *master)
1699{
1700 int ret;
1701
1702 /* Basically no-ops for non-queued masters */
1703 if (!master->queued)
1704 return 0;
1705
1706 ret = spi_stop_queue(master);
1707 if (ret)
1708 dev_err(&master->dev, "queue stop failed\n");
1709
1710 return ret;
1711}
1712EXPORT_SYMBOL_GPL(spi_master_suspend);
1713
1714int spi_master_resume(struct spi_master *master)
1715{
1716 int ret;
1717
1718 if (!master->queued)
1719 return 0;
1720
1721 ret = spi_start_queue(master);
1722 if (ret)
1723 dev_err(&master->dev, "queue restart failed\n");
1724
1725 return ret;
1726}
1727EXPORT_SYMBOL_GPL(spi_master_resume);
1728
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001729static int __spi_master_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08001730{
1731 struct spi_master *m;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001732 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08001733
1734 m = container_of(dev, struct spi_master, dev);
1735 return m->bus_num == *bus_num;
1736}
1737
David Brownell8ae12a02006-01-08 13:34:19 -08001738/**
1739 * spi_busnum_to_master - look up master associated with bus_num
1740 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07001741 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001742 *
1743 * This call may be used with devices that are registered after
1744 * arch init time. It returns a refcounted pointer to the relevant
1745 * spi_master (which the caller must release), or NULL if there is
1746 * no such master registered.
1747 */
1748struct spi_master *spi_busnum_to_master(u16 bus_num)
1749{
Tony Jones49dce682007-10-16 01:27:48 -07001750 struct device *dev;
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001751 struct spi_master *master = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08001752
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001753 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Dave Young5ed2c832008-01-22 15:14:18 +08001754 __spi_master_match);
1755 if (dev)
1756 master = container_of(dev, struct spi_master, dev);
1757 /* reference got in class_find_device */
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001758 return master;
David Brownell8ae12a02006-01-08 13:34:19 -08001759}
1760EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1761
1762
1763/*-------------------------------------------------------------------------*/
1764
David Brownell7d077192009-06-17 16:26:03 -07001765/* Core methods for SPI master protocol drivers. Some of the
1766 * other core methods are currently defined as inline functions.
1767 */
1768
1769/**
1770 * spi_setup - setup SPI mode and clock rate
1771 * @spi: the device whose settings are being modified
1772 * Context: can sleep, and no requests are queued to the device
1773 *
1774 * SPI protocol drivers may need to update the transfer mode if the
1775 * device doesn't work with its default. They may likewise need
1776 * to update clock rates or word sizes from initial values. This function
1777 * changes those settings, and must be called from a context that can sleep.
1778 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
1779 * effect the next time the device is selected and data is transferred to
1780 * or from it. When this function returns, the spi device is deselected.
1781 *
1782 * Note that this call will fail if the protocol driver specifies an option
1783 * that the underlying controller or its driver does not support. For
1784 * example, not all hardware supports wire transfers using nine bit words,
1785 * LSB-first wire encoding, or active-high chipselects.
1786 */
1787int spi_setup(struct spi_device *spi)
1788{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02001789 unsigned bad_bits, ugly_bits;
Laxman Dewangancaae0702012-11-09 14:35:22 +05301790 int status = 0;
David Brownell7d077192009-06-17 16:26:03 -07001791
wangyuhangf477b7f2013-08-11 18:15:17 +08001792 /* check mode to prevent that DUAL and QUAD set at the same time
1793 */
1794 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1795 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1796 dev_err(&spi->dev,
1797 "setup: can not select dual and quad at the same time\n");
1798 return -EINVAL;
1799 }
1800 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1801 */
1802 if ((spi->mode & SPI_3WIRE) && (spi->mode &
1803 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1804 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07001805 /* help drivers fail *cleanly* when they need options
1806 * that aren't supported with their current master
1807 */
1808 bad_bits = spi->mode & ~spi->master->mode_bits;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02001809 ugly_bits = bad_bits &
1810 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
1811 if (ugly_bits) {
1812 dev_warn(&spi->dev,
1813 "setup: ignoring unsupported mode bits %x\n",
1814 ugly_bits);
1815 spi->mode &= ~ugly_bits;
1816 bad_bits &= ~ugly_bits;
1817 }
David Brownelle7db06b2009-06-17 16:26:04 -07001818 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02001819 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07001820 bad_bits);
1821 return -EINVAL;
1822 }
1823
David Brownell7d077192009-06-17 16:26:03 -07001824 if (!spi->bits_per_word)
1825 spi->bits_per_word = 8;
1826
Axel Lin052eb2d42014-02-10 00:08:05 +08001827 if (!spi->max_speed_hz)
1828 spi->max_speed_hz = spi->master->max_speed_hz;
1829
Laxman Dewangancaae0702012-11-09 14:35:22 +05301830 if (spi->master->setup)
1831 status = spi->master->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07001832
Jingoo Han5fe5f052013-10-14 10:31:51 +09001833 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 -07001834 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
1835 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
1836 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
1837 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
1838 (spi->mode & SPI_LOOP) ? "loopback, " : "",
1839 spi->bits_per_word, spi->max_speed_hz,
1840 status);
1841
1842 return status;
1843}
1844EXPORT_SYMBOL_GPL(spi_setup);
1845
Mark Brown90808732013-11-13 23:44:15 +00001846static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b712010-06-28 17:49:29 -07001847{
1848 struct spi_master *master = spi->master;
Laxman Dewangane6811d12012-11-09 14:36:45 +05301849 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09001850 int w_size;
Ernst Schwabcf32b712010-06-28 17:49:29 -07001851
Mark Brown24a00132013-07-10 15:05:40 +01001852 if (list_empty(&message->transfers))
1853 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01001854
Ernst Schwabcf32b712010-06-28 17:49:29 -07001855 /* Half-duplex links include original MicroWire, and ones with
1856 * only one data pin like SPI_3WIRE (switches direction) or where
1857 * either MOSI or MISO is missing. They can also be caused by
1858 * software limitations.
1859 */
1860 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
1861 || (spi->mode & SPI_3WIRE)) {
Ernst Schwabcf32b712010-06-28 17:49:29 -07001862 unsigned flags = master->flags;
1863
1864 list_for_each_entry(xfer, &message->transfers, transfer_list) {
1865 if (xfer->rx_buf && xfer->tx_buf)
1866 return -EINVAL;
1867 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
1868 return -EINVAL;
1869 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
1870 return -EINVAL;
1871 }
1872 }
1873
Laxman Dewangane6811d12012-11-09 14:36:45 +05301874 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05301875 * Set transfer bits_per_word and max speed as spi device default if
1876 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08001877 * Set transfer tx_nbits and rx_nbits as single transfer default
1878 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05301879 */
1880 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05301881 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05301882 if (!xfer->bits_per_word)
1883 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08001884
1885 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05301886 xfer->speed_hz = spi->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08001887
1888 if (master->max_speed_hz &&
1889 xfer->speed_hz > master->max_speed_hz)
1890 xfer->speed_hz = master->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02001891
Stephen Warren543bb252013-03-26 20:37:57 -06001892 if (master->bits_per_word_mask) {
1893 /* Only 32 bits fit in the mask */
1894 if (xfer->bits_per_word > 32)
1895 return -EINVAL;
1896 if (!(master->bits_per_word_mask &
1897 BIT(xfer->bits_per_word - 1)))
1898 return -EINVAL;
1899 }
Mark Browna2fd4f92013-07-10 14:57:26 +01001900
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02001901 /*
1902 * SPI transfer length should be multiple of SPI word size
1903 * where SPI word size should be power-of-two multiple
1904 */
1905 if (xfer->bits_per_word <= 8)
1906 w_size = 1;
1907 else if (xfer->bits_per_word <= 16)
1908 w_size = 2;
1909 else
1910 w_size = 4;
1911
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02001912 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09001913 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02001914 return -EINVAL;
1915
Mark Browna2fd4f92013-07-10 14:57:26 +01001916 if (xfer->speed_hz && master->min_speed_hz &&
1917 xfer->speed_hz < master->min_speed_hz)
1918 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08001919
1920 if (xfer->tx_buf && !xfer->tx_nbits)
1921 xfer->tx_nbits = SPI_NBITS_SINGLE;
1922 if (xfer->rx_buf && !xfer->rx_nbits)
1923 xfer->rx_nbits = SPI_NBITS_SINGLE;
1924 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01001925 * 1. check the value matches one of single, dual and quad
1926 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08001927 */
Sourav Poddardb90a442013-08-22 21:20:48 +05301928 if (xfer->tx_buf) {
1929 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
1930 xfer->tx_nbits != SPI_NBITS_DUAL &&
1931 xfer->tx_nbits != SPI_NBITS_QUAD)
1932 return -EINVAL;
1933 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
1934 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
1935 return -EINVAL;
1936 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
1937 !(spi->mode & SPI_TX_QUAD))
1938 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05301939 }
wangyuhangf477b7f2013-08-11 18:15:17 +08001940 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05301941 if (xfer->rx_buf) {
1942 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
1943 xfer->rx_nbits != SPI_NBITS_DUAL &&
1944 xfer->rx_nbits != SPI_NBITS_QUAD)
1945 return -EINVAL;
1946 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
1947 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
1948 return -EINVAL;
1949 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
1950 !(spi->mode & SPI_RX_QUAD))
1951 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05301952 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05301953 }
1954
Ernst Schwabcf32b712010-06-28 17:49:29 -07001955 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00001956
1957 return 0;
1958}
1959
1960static int __spi_async(struct spi_device *spi, struct spi_message *message)
1961{
1962 struct spi_master *master = spi->master;
1963
1964 message->spi = spi;
1965
1966 trace_spi_message_submit(message);
1967
Ernst Schwabcf32b712010-06-28 17:49:29 -07001968 return master->transfer(spi, message);
1969}
1970
David Brownell568d0692009-09-22 16:46:18 -07001971/**
1972 * spi_async - asynchronous SPI transfer
1973 * @spi: device with which data will be exchanged
1974 * @message: describes the data transfers, including completion callback
1975 * Context: any (irqs may be blocked, etc)
1976 *
1977 * This call may be used in_irq and other contexts which can't sleep,
1978 * as well as from task contexts which can sleep.
1979 *
1980 * The completion callback is invoked in a context which can't sleep.
1981 * Before that invocation, the value of message->status is undefined.
1982 * When the callback is issued, message->status holds either zero (to
1983 * indicate complete success) or a negative error code. After that
1984 * callback returns, the driver which issued the transfer request may
1985 * deallocate the associated memory; it's no longer in use by any SPI
1986 * core or controller driver code.
1987 *
1988 * Note that although all messages to a spi_device are handled in
1989 * FIFO order, messages may go to different devices in other orders.
1990 * Some device might be higher priority, or have various "hard" access
1991 * time requirements, for example.
1992 *
1993 * On detection of any fault during the transfer, processing of
1994 * the entire message is aborted, and the device is deselected.
1995 * Until returning from the associated message completion callback,
1996 * no other spi_message queued to that device will be processed.
1997 * (This rule applies equally to all the synchronous transfer calls,
1998 * which are wrappers around this core asynchronous primitive.)
1999 */
2000int spi_async(struct spi_device *spi, struct spi_message *message)
2001{
2002 struct spi_master *master = spi->master;
Ernst Schwabcf32b712010-06-28 17:49:29 -07002003 int ret;
2004 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07002005
Mark Brown90808732013-11-13 23:44:15 +00002006 ret = __spi_validate(spi, message);
2007 if (ret != 0)
2008 return ret;
2009
Ernst Schwabcf32b712010-06-28 17:49:29 -07002010 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07002011
Ernst Schwabcf32b712010-06-28 17:49:29 -07002012 if (master->bus_lock_flag)
2013 ret = -EBUSY;
2014 else
2015 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07002016
Ernst Schwabcf32b712010-06-28 17:49:29 -07002017 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2018
2019 return ret;
David Brownell568d0692009-09-22 16:46:18 -07002020}
2021EXPORT_SYMBOL_GPL(spi_async);
2022
Ernst Schwabcf32b712010-06-28 17:49:29 -07002023/**
2024 * spi_async_locked - version of spi_async with exclusive bus usage
2025 * @spi: device with which data will be exchanged
2026 * @message: describes the data transfers, including completion callback
2027 * Context: any (irqs may be blocked, etc)
2028 *
2029 * This call may be used in_irq and other contexts which can't sleep,
2030 * as well as from task contexts which can sleep.
2031 *
2032 * The completion callback is invoked in a context which can't sleep.
2033 * Before that invocation, the value of message->status is undefined.
2034 * When the callback is issued, message->status holds either zero (to
2035 * indicate complete success) or a negative error code. After that
2036 * callback returns, the driver which issued the transfer request may
2037 * deallocate the associated memory; it's no longer in use by any SPI
2038 * core or controller driver code.
2039 *
2040 * Note that although all messages to a spi_device are handled in
2041 * FIFO order, messages may go to different devices in other orders.
2042 * Some device might be higher priority, or have various "hard" access
2043 * time requirements, for example.
2044 *
2045 * On detection of any fault during the transfer, processing of
2046 * the entire message is aborted, and the device is deselected.
2047 * Until returning from the associated message completion callback,
2048 * no other spi_message queued to that device will be processed.
2049 * (This rule applies equally to all the synchronous transfer calls,
2050 * which are wrappers around this core asynchronous primitive.)
2051 */
2052int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2053{
2054 struct spi_master *master = spi->master;
2055 int ret;
2056 unsigned long flags;
2057
Mark Brown90808732013-11-13 23:44:15 +00002058 ret = __spi_validate(spi, message);
2059 if (ret != 0)
2060 return ret;
2061
Ernst Schwabcf32b712010-06-28 17:49:29 -07002062 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2063
2064 ret = __spi_async(spi, message);
2065
2066 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2067
2068 return ret;
2069
2070}
2071EXPORT_SYMBOL_GPL(spi_async_locked);
2072
David Brownell7d077192009-06-17 16:26:03 -07002073
2074/*-------------------------------------------------------------------------*/
2075
2076/* Utility methods for SPI master protocol drivers, layered on
2077 * top of the core. Some other utility methods are defined as
2078 * inline functions.
2079 */
2080
Andrew Morton5d870c82006-01-11 11:23:49 -08002081static void spi_complete(void *arg)
2082{
2083 complete(arg);
2084}
2085
Ernst Schwabcf32b712010-06-28 17:49:29 -07002086static int __spi_sync(struct spi_device *spi, struct spi_message *message,
2087 int bus_locked)
2088{
2089 DECLARE_COMPLETION_ONSTACK(done);
2090 int status;
2091 struct spi_master *master = spi->master;
2092
2093 message->complete = spi_complete;
2094 message->context = &done;
2095
2096 if (!bus_locked)
2097 mutex_lock(&master->bus_lock_mutex);
2098
2099 status = spi_async_locked(spi, message);
2100
2101 if (!bus_locked)
2102 mutex_unlock(&master->bus_lock_mutex);
2103
2104 if (status == 0) {
2105 wait_for_completion(&done);
2106 status = message->status;
2107 }
2108 message->context = NULL;
2109 return status;
2110}
2111
David Brownell8ae12a02006-01-08 13:34:19 -08002112/**
2113 * spi_sync - blocking/synchronous SPI data transfers
2114 * @spi: device with which data will be exchanged
2115 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07002116 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002117 *
2118 * This call may only be used from a context that may sleep. The sleep
2119 * is non-interruptible, and has no timeout. Low-overhead controller
2120 * drivers may DMA directly into and out of the message buffers.
2121 *
2122 * Note that the SPI device's chip select is active during the message,
2123 * and then is normally disabled between messages. Drivers for some
2124 * frequently-used devices may want to minimize costs of selecting a chip,
2125 * by leaving it selected in anticipation that the next message will go
2126 * to the same chip. (That may increase power usage.)
2127 *
David Brownell0c868462006-01-08 13:34:25 -08002128 * Also, the caller is guaranteeing that the memory associated with the
2129 * message will not be freed before this call returns.
2130 *
Marc Pignat9b938b72007-12-04 23:45:10 -08002131 * It returns zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002132 */
2133int spi_sync(struct spi_device *spi, struct spi_message *message)
2134{
Ernst Schwabcf32b712010-06-28 17:49:29 -07002135 return __spi_sync(spi, message, 0);
David Brownell8ae12a02006-01-08 13:34:19 -08002136}
2137EXPORT_SYMBOL_GPL(spi_sync);
2138
Ernst Schwabcf32b712010-06-28 17:49:29 -07002139/**
2140 * spi_sync_locked - version of spi_sync with exclusive bus usage
2141 * @spi: device with which data will be exchanged
2142 * @message: describes the data transfers
2143 * Context: can sleep
2144 *
2145 * This call may only be used from a context that may sleep. The sleep
2146 * is non-interruptible, and has no timeout. Low-overhead controller
2147 * drivers may DMA directly into and out of the message buffers.
2148 *
2149 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002150 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b712010-06-28 17:49:29 -07002151 * be released by a spi_bus_unlock call when the exclusive access is over.
2152 *
2153 * It returns zero on success, else a negative error code.
2154 */
2155int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2156{
2157 return __spi_sync(spi, message, 1);
2158}
2159EXPORT_SYMBOL_GPL(spi_sync_locked);
2160
2161/**
2162 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
2163 * @master: SPI bus master that should be locked for exclusive bus access
2164 * Context: can sleep
2165 *
2166 * This call may only be used from a context that may sleep. The sleep
2167 * is non-interruptible, and has no timeout.
2168 *
2169 * This call should be used by drivers that require exclusive access to the
2170 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
2171 * exclusive access is over. Data transfer must be done by spi_sync_locked
2172 * and spi_async_locked calls when the SPI bus lock is held.
2173 *
2174 * It returns zero on success, else a negative error code.
2175 */
2176int spi_bus_lock(struct spi_master *master)
2177{
2178 unsigned long flags;
2179
2180 mutex_lock(&master->bus_lock_mutex);
2181
2182 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2183 master->bus_lock_flag = 1;
2184 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2185
2186 /* mutex remains locked until spi_bus_unlock is called */
2187
2188 return 0;
2189}
2190EXPORT_SYMBOL_GPL(spi_bus_lock);
2191
2192/**
2193 * spi_bus_unlock - release the lock for exclusive SPI bus usage
2194 * @master: SPI bus master that was locked for exclusive bus access
2195 * Context: can sleep
2196 *
2197 * This call may only be used from a context that may sleep. The sleep
2198 * is non-interruptible, and has no timeout.
2199 *
2200 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
2201 * call.
2202 *
2203 * It returns zero on success, else a negative error code.
2204 */
2205int spi_bus_unlock(struct spi_master *master)
2206{
2207 master->bus_lock_flag = 0;
2208
2209 mutex_unlock(&master->bus_lock_mutex);
2210
2211 return 0;
2212}
2213EXPORT_SYMBOL_GPL(spi_bus_unlock);
2214
David Brownella9948b62006-04-02 10:37:40 -08002215/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09002216#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08002217
2218static u8 *buf;
2219
2220/**
2221 * spi_write_then_read - SPI synchronous write followed by read
2222 * @spi: device with which data will be exchanged
2223 * @txbuf: data to be written (need not be dma-safe)
2224 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07002225 * @rxbuf: buffer into which data will be read (need not be dma-safe)
2226 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07002227 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002228 *
2229 * This performs a half duplex MicroWire style transaction with the
2230 * device, sending txbuf and then reading rxbuf. The return value
2231 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08002232 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08002233 *
David Brownell0c868462006-01-08 13:34:25 -08002234 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07002235 * portable code should never use this for more than 32 bytes.
2236 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c868462006-01-08 13:34:25 -08002237 * spi_{async,sync}() calls with dma-safe buffers.
David Brownell8ae12a02006-01-08 13:34:19 -08002238 */
2239int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02002240 const void *txbuf, unsigned n_tx,
2241 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08002242{
David Brownell068f4072007-12-04 23:45:09 -08002243 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002244
2245 int status;
2246 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07002247 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08002248 u8 *local_buf;
2249
Mark Brownb3a223e2012-12-02 12:54:25 +09002250 /* Use preallocated DMA-safe buffer if we can. We can't avoid
2251 * copying here, (as a pure convenience thing), but we can
2252 * keep heap costs out of the hot path unless someone else is
2253 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08002254 */
Mark Brownb3a223e2012-12-02 12:54:25 +09002255 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08002256 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
2257 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09002258 if (!local_buf)
2259 return -ENOMEM;
2260 } else {
2261 local_buf = buf;
2262 }
David Brownell8ae12a02006-01-08 13:34:19 -08002263
Vitaly Wool8275c642006-01-08 13:34:28 -08002264 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09002265 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07002266 if (n_tx) {
2267 x[0].len = n_tx;
2268 spi_message_add_tail(&x[0], &message);
2269 }
2270 if (n_rx) {
2271 x[1].len = n_rx;
2272 spi_message_add_tail(&x[1], &message);
2273 }
Vitaly Wool8275c642006-01-08 13:34:28 -08002274
David Brownell8ae12a02006-01-08 13:34:19 -08002275 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07002276 x[0].tx_buf = local_buf;
2277 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08002278
2279 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08002280 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08002281 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07002282 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08002283
David Brownellbdff5492009-04-13 14:39:57 -07002284 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08002285 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002286 else
2287 kfree(local_buf);
2288
2289 return status;
2290}
2291EXPORT_SYMBOL_GPL(spi_write_then_read);
2292
2293/*-------------------------------------------------------------------------*/
2294
2295static int __init spi_init(void)
2296{
David Brownellb8852442006-01-08 13:34:23 -08002297 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08002298
Christoph Lametere94b1762006-12-06 20:33:17 -08002299 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08002300 if (!buf) {
2301 status = -ENOMEM;
2302 goto err0;
2303 }
2304
2305 status = bus_register(&spi_bus_type);
2306 if (status < 0)
2307 goto err1;
2308
2309 status = class_register(&spi_master_class);
2310 if (status < 0)
2311 goto err2;
David Brownell8ae12a02006-01-08 13:34:19 -08002312 return 0;
David Brownellb8852442006-01-08 13:34:23 -08002313
2314err2:
2315 bus_unregister(&spi_bus_type);
2316err1:
2317 kfree(buf);
2318 buf = NULL;
2319err0:
2320 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08002321}
David Brownellb8852442006-01-08 13:34:23 -08002322
David Brownell8ae12a02006-01-08 13:34:19 -08002323/* board_info is normally registered in arch_initcall(),
2324 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08002325 *
2326 * REVISIT only boardinfo really needs static linking. the rest (device and
2327 * driver registration) _could_ be dynamically linked (modular) ... costs
2328 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08002329 */
David Brownell673c0c02008-10-15 22:02:46 -07002330postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08002331