blob: 8a30c6b66a64cefd42ac39d9d8b8a643fb6ddfe7 [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>
Matthias Kaehlcke94040822007-07-17 04:04:16 -070027#include <linux/mutex.h>
Sinan Akman2b7a32f2010-10-02 21:28:29 -060028#include <linux/of_device.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060029#include <linux/of_irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Anton Vorontsove0626e32009-09-22 16:46:08 -070031#include <linux/mod_devicetable.h>
David Brownell8ae12a02006-01-08 13:34:19 -080032#include <linux/spi/spi.h>
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +010033#include <linux/of_gpio.h>
Mark Brown3ae22e82010-12-25 15:32:27 +010034#include <linux/pm_runtime.h>
Paul Gortmaker025ed132011-07-10 12:57:55 -040035#include <linux/export.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060036#include <linux/sched/rt.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010037#include <linux/delay.h>
38#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010039#include <linux/ioport.h>
40#include <linux/acpi.h>
David Brownell8ae12a02006-01-08 13:34:19 -080041
Mark Brown56ec1972013-10-07 19:33:53 +010042#define CREATE_TRACE_POINTS
43#include <trace/events/spi.h>
44
David Brownell8ae12a02006-01-08 13:34:19 -080045static void spidev_release(struct device *dev)
46{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080047 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080048
49 /* spi masters may cleanup for released devices */
50 if (spi->master->cleanup)
51 spi->master->cleanup(spi);
52
David Brownell0c868462006-01-08 13:34:25 -080053 spi_master_put(spi->master);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000054 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080055}
56
57static ssize_t
58modalias_show(struct device *dev, struct device_attribute *a, char *buf)
59{
60 const struct spi_device *spi = to_spi_device(dev);
61
Grant Likelyd8e328b2012-05-20 00:08:13 -060062 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080063}
64
65static struct device_attribute spi_dev_attrs[] = {
66 __ATTR_RO(modalias),
67 __ATTR_NULL,
68};
69
70/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
71 * and the sysfs version makes coldplug work too.
72 */
73
Anton Vorontsov75368bf2009-09-22 16:46:04 -070074static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
75 const struct spi_device *sdev)
76{
77 while (id->name[0]) {
78 if (!strcmp(sdev->modalias, id->name))
79 return id;
80 id++;
81 }
82 return NULL;
83}
84
85const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
86{
87 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
88
89 return spi_match_id(sdrv->id_table, sdev);
90}
91EXPORT_SYMBOL_GPL(spi_get_device_id);
92
David Brownell8ae12a02006-01-08 13:34:19 -080093static int spi_match_device(struct device *dev, struct device_driver *drv)
94{
95 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -070096 const struct spi_driver *sdrv = to_spi_driver(drv);
97
Sinan Akman2b7a32f2010-10-02 21:28:29 -060098 /* Attempt an OF style match */
99 if (of_driver_match_device(dev, drv))
100 return 1;
101
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100102 /* Then try ACPI */
103 if (acpi_driver_match_device(dev, drv))
104 return 1;
105
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700106 if (sdrv->id_table)
107 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800108
Kay Sievers35f74fc2009-01-06 10:44:37 -0800109 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800110}
111
Kay Sievers7eff2e72007-08-14 15:15:12 +0200112static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800113{
114 const struct spi_device *spi = to_spi_device(dev);
115
Anton Vorontsove0626e32009-09-22 16:46:08 -0700116 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800117 return 0;
118}
119
Mark Brown3ae22e82010-12-25 15:32:27 +0100120#ifdef CONFIG_PM_SLEEP
121static int spi_legacy_suspend(struct device *dev, pm_message_t message)
David Brownell8ae12a02006-01-08 13:34:19 -0800122{
David Brownell3c724262008-02-06 01:38:10 -0800123 int value = 0;
David Brownellb8852442006-01-08 13:34:23 -0800124 struct spi_driver *drv = to_spi_driver(dev->driver);
David Brownell8ae12a02006-01-08 13:34:19 -0800125
David Brownell8ae12a02006-01-08 13:34:19 -0800126 /* suspend will stop irqs and dma; no more i/o */
David Brownell3c724262008-02-06 01:38:10 -0800127 if (drv) {
128 if (drv->suspend)
129 value = drv->suspend(to_spi_device(dev), message);
130 else
131 dev_dbg(dev, "... can't suspend\n");
132 }
David Brownell8ae12a02006-01-08 13:34:19 -0800133 return value;
134}
135
Mark Brown3ae22e82010-12-25 15:32:27 +0100136static int spi_legacy_resume(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -0800137{
David Brownell3c724262008-02-06 01:38:10 -0800138 int value = 0;
David Brownellb8852442006-01-08 13:34:23 -0800139 struct spi_driver *drv = to_spi_driver(dev->driver);
David Brownell8ae12a02006-01-08 13:34:19 -0800140
David Brownell8ae12a02006-01-08 13:34:19 -0800141 /* resume may restart the i/o queue */
David Brownell3c724262008-02-06 01:38:10 -0800142 if (drv) {
143 if (drv->resume)
144 value = drv->resume(to_spi_device(dev));
145 else
146 dev_dbg(dev, "... can't resume\n");
147 }
David Brownell8ae12a02006-01-08 13:34:19 -0800148 return value;
149}
150
Mark Brown3ae22e82010-12-25 15:32:27 +0100151static int spi_pm_suspend(struct device *dev)
152{
153 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
154
155 if (pm)
156 return pm_generic_suspend(dev);
157 else
158 return spi_legacy_suspend(dev, PMSG_SUSPEND);
159}
160
161static int spi_pm_resume(struct device *dev)
162{
163 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
164
165 if (pm)
166 return pm_generic_resume(dev);
167 else
168 return spi_legacy_resume(dev);
169}
170
171static int spi_pm_freeze(struct device *dev)
172{
173 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
174
175 if (pm)
176 return pm_generic_freeze(dev);
177 else
178 return spi_legacy_suspend(dev, PMSG_FREEZE);
179}
180
181static int spi_pm_thaw(struct device *dev)
182{
183 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
184
185 if (pm)
186 return pm_generic_thaw(dev);
187 else
188 return spi_legacy_resume(dev);
189}
190
191static int spi_pm_poweroff(struct device *dev)
192{
193 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
194
195 if (pm)
196 return pm_generic_poweroff(dev);
197 else
198 return spi_legacy_suspend(dev, PMSG_HIBERNATE);
199}
200
201static int spi_pm_restore(struct device *dev)
202{
203 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
204
205 if (pm)
206 return pm_generic_restore(dev);
207 else
208 return spi_legacy_resume(dev);
209}
David Brownell8ae12a02006-01-08 13:34:19 -0800210#else
Mark Brown3ae22e82010-12-25 15:32:27 +0100211#define spi_pm_suspend NULL
212#define spi_pm_resume NULL
213#define spi_pm_freeze NULL
214#define spi_pm_thaw NULL
215#define spi_pm_poweroff NULL
216#define spi_pm_restore NULL
David Brownell8ae12a02006-01-08 13:34:19 -0800217#endif
218
Mark Brown3ae22e82010-12-25 15:32:27 +0100219static const struct dev_pm_ops spi_pm = {
220 .suspend = spi_pm_suspend,
221 .resume = spi_pm_resume,
222 .freeze = spi_pm_freeze,
223 .thaw = spi_pm_thaw,
224 .poweroff = spi_pm_poweroff,
225 .restore = spi_pm_restore,
226 SET_RUNTIME_PM_OPS(
227 pm_generic_runtime_suspend,
228 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200229 NULL
Mark Brown3ae22e82010-12-25 15:32:27 +0100230 )
231};
232
David Brownell8ae12a02006-01-08 13:34:19 -0800233struct bus_type spi_bus_type = {
234 .name = "spi",
235 .dev_attrs = spi_dev_attrs,
236 .match = spi_match_device,
237 .uevent = spi_uevent,
Mark Brown3ae22e82010-12-25 15:32:27 +0100238 .pm = &spi_pm,
David Brownell8ae12a02006-01-08 13:34:19 -0800239};
240EXPORT_SYMBOL_GPL(spi_bus_type);
241
David Brownellb8852442006-01-08 13:34:23 -0800242
243static int spi_drv_probe(struct device *dev)
244{
245 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
246
247 return sdrv->probe(to_spi_device(dev));
248}
249
250static int spi_drv_remove(struct device *dev)
251{
252 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
253
254 return sdrv->remove(to_spi_device(dev));
255}
256
257static void spi_drv_shutdown(struct device *dev)
258{
259 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
260
261 sdrv->shutdown(to_spi_device(dev));
262}
263
David Brownell33e34dc2007-05-08 00:32:21 -0700264/**
265 * spi_register_driver - register a SPI driver
266 * @sdrv: the driver to register
267 * Context: can sleep
268 */
David Brownellb8852442006-01-08 13:34:23 -0800269int spi_register_driver(struct spi_driver *sdrv)
270{
271 sdrv->driver.bus = &spi_bus_type;
272 if (sdrv->probe)
273 sdrv->driver.probe = spi_drv_probe;
274 if (sdrv->remove)
275 sdrv->driver.remove = spi_drv_remove;
276 if (sdrv->shutdown)
277 sdrv->driver.shutdown = spi_drv_shutdown;
278 return driver_register(&sdrv->driver);
279}
280EXPORT_SYMBOL_GPL(spi_register_driver);
281
David Brownell8ae12a02006-01-08 13:34:19 -0800282/*-------------------------------------------------------------------------*/
283
284/* SPI devices should normally not be created by SPI device drivers; that
285 * would make them board-specific. Similarly with SPI master drivers.
286 * Device registration normally goes into like arch/.../mach.../board-YYY.c
287 * with other readonly (flashable) information about mainboard devices.
288 */
289
290struct boardinfo {
291 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800292 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800293};
294
295static LIST_HEAD(board_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800296static LIST_HEAD(spi_master_list);
297
298/*
299 * Used to protect add/del opertion for board_info list and
300 * spi_master list, and their matching process
301 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700302static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800303
Grant Likelydc87c982008-05-15 16:50:22 -0600304/**
305 * spi_alloc_device - Allocate a new SPI device
306 * @master: Controller to which device is connected
307 * Context: can sleep
308 *
309 * Allows a driver to allocate and initialize a spi_device without
310 * registering it immediately. This allows a driver to directly
311 * fill the spi_device with device parameters before calling
312 * spi_add_device() on it.
313 *
314 * Caller is responsible to call spi_add_device() on the returned
315 * spi_device structure to add it to the SPI master. If the caller
316 * needs to discard the spi_device without adding it, then it should
317 * call spi_dev_put() on it.
318 *
319 * Returns a pointer to the new device, or NULL.
320 */
321struct spi_device *spi_alloc_device(struct spi_master *master)
322{
323 struct spi_device *spi;
324 struct device *dev = master->dev.parent;
325
326 if (!spi_master_get(master))
327 return NULL;
328
329 spi = kzalloc(sizeof *spi, GFP_KERNEL);
330 if (!spi) {
331 dev_err(dev, "cannot alloc spi_device\n");
332 spi_master_put(master);
333 return NULL;
334 }
335
336 spi->master = master;
Laurent Pinchart178db7d2011-12-12 01:15:06 +0100337 spi->dev.parent = &master->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600338 spi->dev.bus = &spi_bus_type;
339 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100340 spi->cs_gpio = -ENOENT;
Grant Likelydc87c982008-05-15 16:50:22 -0600341 device_initialize(&spi->dev);
342 return spi;
343}
344EXPORT_SYMBOL_GPL(spi_alloc_device);
345
346/**
347 * spi_add_device - Add spi_device allocated with spi_alloc_device
348 * @spi: spi_device to register
349 *
350 * Companion function to spi_alloc_device. Devices allocated with
351 * spi_alloc_device can be added onto the spi bus with this function.
352 *
David Brownelle48880e2008-08-15 00:40:44 -0700353 * Returns 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600354 */
355int spi_add_device(struct spi_device *spi)
356{
David Brownelle48880e2008-08-15 00:40:44 -0700357 static DEFINE_MUTEX(spi_add_lock);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100358 struct spi_master *master = spi->master;
359 struct device *dev = master->dev.parent;
Roman Tereshonkov8ec130a2010-04-16 09:52:59 +0000360 struct device *d;
Grant Likelydc87c982008-05-15 16:50:22 -0600361 int status;
362
363 /* Chipselects are numbered 0..max; validate. */
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100364 if (spi->chip_select >= master->num_chipselect) {
Grant Likelydc87c982008-05-15 16:50:22 -0600365 dev_err(dev, "cs%d >= max %d\n",
366 spi->chip_select,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100367 master->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600368 return -EINVAL;
369 }
370
371 /* Set the bus ID string */
Kay Sievers35f74fc2009-01-06 10:44:37 -0800372 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
Grant Likelydc87c982008-05-15 16:50:22 -0600373 spi->chip_select);
374
David Brownelle48880e2008-08-15 00:40:44 -0700375
376 /* We need to make sure there's no other device with this
377 * chipselect **BEFORE** we call setup(), else we'll trash
378 * its configuration. Lock against concurrent add() calls.
379 */
380 mutex_lock(&spi_add_lock);
381
Roman Tereshonkov8ec130a2010-04-16 09:52:59 +0000382 d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
383 if (d != NULL) {
David Brownelle48880e2008-08-15 00:40:44 -0700384 dev_err(dev, "chipselect %d already in use\n",
385 spi->chip_select);
Roman Tereshonkov8ec130a2010-04-16 09:52:59 +0000386 put_device(d);
David Brownelle48880e2008-08-15 00:40:44 -0700387 status = -EBUSY;
388 goto done;
389 }
390
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100391 if (master->cs_gpios)
392 spi->cs_gpio = master->cs_gpios[spi->chip_select];
393
David Brownelle48880e2008-08-15 00:40:44 -0700394 /* Drivers may modify this initial i/o setup, but will
395 * normally rely on the device being setup. Devices
396 * using SPI_CS_HIGH can't coexist well otherwise...
397 */
David Brownell7d077192009-06-17 16:26:03 -0700398 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600399 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200400 dev_err(dev, "can't setup %s, status %d\n",
401 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700402 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600403 }
404
David Brownelle48880e2008-08-15 00:40:44 -0700405 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600406 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700407 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200408 dev_err(dev, "can't add %s, status %d\n",
409 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700410 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800411 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600412
David Brownelle48880e2008-08-15 00:40:44 -0700413done:
414 mutex_unlock(&spi_add_lock);
415 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600416}
417EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800418
David Brownell33e34dc2007-05-08 00:32:21 -0700419/**
420 * spi_new_device - instantiate one new SPI device
421 * @master: Controller to which device is connected
422 * @chip: Describes the SPI device
423 * Context: can sleep
424 *
425 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800426 * after board init creates the hard-wired devices. Some development
427 * platforms may not be able to use spi_register_board_info though, and
428 * this is exported so that for example a USB or parport based adapter
429 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700430 *
431 * Returns the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800432 */
Adrian Bunke9d5a462007-03-26 21:32:23 -0800433struct spi_device *spi_new_device(struct spi_master *master,
434 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800435{
436 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800437 int status;
438
David Brownell082c8cb2007-07-31 00:39:45 -0700439 /* NOTE: caller did any chip->bus_num checks necessary.
440 *
441 * Also, unless we change the return value convention to use
442 * error-or-pointer (not NULL-or-pointer), troubleshootability
443 * suggests syslogged diagnostics are best here (ugh).
444 */
445
Grant Likelydc87c982008-05-15 16:50:22 -0600446 proxy = spi_alloc_device(master);
447 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800448 return NULL;
449
Grant Likely102eb972008-07-23 21:29:55 -0700450 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
451
David Brownell8ae12a02006-01-08 13:34:19 -0800452 proxy->chip_select = chip->chip_select;
453 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700454 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800455 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700456 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800457 proxy->dev.platform_data = (void *) chip->platform_data;
458 proxy->controller_data = chip->controller_data;
459 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800460
Grant Likelydc87c982008-05-15 16:50:22 -0600461 status = spi_add_device(proxy);
David Brownell8ae12a02006-01-08 13:34:19 -0800462 if (status < 0) {
Grant Likelydc87c982008-05-15 16:50:22 -0600463 spi_dev_put(proxy);
464 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800465 }
466
David Brownell8ae12a02006-01-08 13:34:19 -0800467 return proxy;
468}
469EXPORT_SYMBOL_GPL(spi_new_device);
470
Feng Tang2b9603a2010-08-02 15:52:15 +0800471static void spi_match_master_to_boardinfo(struct spi_master *master,
472 struct spi_board_info *bi)
473{
474 struct spi_device *dev;
475
476 if (master->bus_num != bi->bus_num)
477 return;
478
479 dev = spi_new_device(master, bi);
480 if (!dev)
481 dev_err(master->dev.parent, "can't create new device for %s\n",
482 bi->modalias);
483}
484
David Brownell33e34dc2007-05-08 00:32:21 -0700485/**
486 * spi_register_board_info - register SPI devices for a given board
487 * @info: array of chip descriptors
488 * @n: how many descriptors are provided
489 * Context: can sleep
490 *
David Brownell8ae12a02006-01-08 13:34:19 -0800491 * Board-specific early init code calls this (probably during arch_initcall)
492 * with segments of the SPI device table. Any device nodes are created later,
493 * after the relevant parent SPI controller (bus_num) is defined. We keep
494 * this table of devices forever, so that reloading a controller driver will
495 * not make Linux forget about these hard-wired devices.
496 *
497 * Other code can also call this, e.g. a particular add-on board might provide
498 * SPI devices through its expansion connector, so code initializing that board
499 * would naturally declare its SPI devices.
500 *
501 * The board info passed can safely be __initdata ... but be careful of
502 * any embedded pointers (platform_data, etc), they're copied as-is.
503 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000504int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800505{
Feng Tang2b9603a2010-08-02 15:52:15 +0800506 struct boardinfo *bi;
507 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800508
Feng Tang2b9603a2010-08-02 15:52:15 +0800509 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800510 if (!bi)
511 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800512
Feng Tang2b9603a2010-08-02 15:52:15 +0800513 for (i = 0; i < n; i++, bi++, info++) {
514 struct spi_master *master;
David Brownell8ae12a02006-01-08 13:34:19 -0800515
Feng Tang2b9603a2010-08-02 15:52:15 +0800516 memcpy(&bi->board_info, info, sizeof(*info));
517 mutex_lock(&board_lock);
518 list_add_tail(&bi->list, &board_list);
519 list_for_each_entry(master, &spi_master_list, list)
520 spi_match_master_to_boardinfo(master, &bi->board_info);
521 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800522 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800523
524 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800525}
526
527/*-------------------------------------------------------------------------*/
528
Linus Walleijffbbdd212012-02-22 10:05:38 +0100529/**
530 * spi_pump_messages - kthread work function which processes spi message queue
531 * @work: pointer to kthread work struct contained in the master struct
532 *
533 * This function checks if there is any spi message in the queue that
534 * needs processing and if so call out to the driver to initialize hardware
535 * and transfer each message.
536 *
537 */
538static void spi_pump_messages(struct kthread_work *work)
539{
540 struct spi_master *master =
541 container_of(work, struct spi_master, pump_messages);
542 unsigned long flags;
543 bool was_busy = false;
544 int ret;
545
546 /* Lock queue and check for queue work */
547 spin_lock_irqsave(&master->queue_lock, flags);
548 if (list_empty(&master->queue) || !master->running) {
Bryan Freedb0b36b82013-03-13 11:17:40 -0700549 if (!master->busy) {
550 spin_unlock_irqrestore(&master->queue_lock, flags);
551 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +0100552 }
553 master->busy = false;
554 spin_unlock_irqrestore(&master->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -0700555 if (master->unprepare_transfer_hardware &&
556 master->unprepare_transfer_hardware(master))
557 dev_err(&master->dev,
558 "failed to unprepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +0100559 if (master->auto_runtime_pm) {
560 pm_runtime_mark_last_busy(master->dev.parent);
561 pm_runtime_put_autosuspend(master->dev.parent);
562 }
Mark Brown56ec1972013-10-07 19:33:53 +0100563 trace_spi_master_idle(master);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100564 return;
565 }
566
567 /* Make sure we are not already running a message */
568 if (master->cur_msg) {
569 spin_unlock_irqrestore(&master->queue_lock, flags);
570 return;
571 }
572 /* Extract head of queue */
573 master->cur_msg =
574 list_entry(master->queue.next, struct spi_message, queue);
575
576 list_del_init(&master->cur_msg->queue);
577 if (master->busy)
578 was_busy = true;
579 else
580 master->busy = true;
581 spin_unlock_irqrestore(&master->queue_lock, flags);
582
Mark Brown49834de2013-07-28 14:47:02 +0100583 if (!was_busy && master->auto_runtime_pm) {
584 ret = pm_runtime_get_sync(master->dev.parent);
585 if (ret < 0) {
586 dev_err(&master->dev, "Failed to power device: %d\n",
587 ret);
588 return;
589 }
590 }
591
Mark Brown56ec1972013-10-07 19:33:53 +0100592 if (!was_busy)
593 trace_spi_master_busy(master);
594
Shubhrajyoti D7dfd2bd2012-05-10 19:20:41 +0530595 if (!was_busy && master->prepare_transfer_hardware) {
Linus Walleijffbbdd212012-02-22 10:05:38 +0100596 ret = master->prepare_transfer_hardware(master);
597 if (ret) {
598 dev_err(&master->dev,
599 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +0100600
601 if (master->auto_runtime_pm)
602 pm_runtime_put(master->dev.parent);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100603 return;
604 }
605 }
606
Mark Brown56ec1972013-10-07 19:33:53 +0100607 trace_spi_message_start(master->cur_msg);
608
Mark Brown2841a5f2013-10-05 00:23:12 +0100609 if (master->prepare_message) {
610 ret = master->prepare_message(master, master->cur_msg);
611 if (ret) {
612 dev_err(&master->dev,
613 "failed to prepare message: %d\n", ret);
614 master->cur_msg->status = ret;
615 spi_finalize_current_message(master);
616 return;
617 }
618 master->cur_msg_prepared = true;
619 }
620
Linus Walleijffbbdd212012-02-22 10:05:38 +0100621 ret = master->transfer_one_message(master, master->cur_msg);
622 if (ret) {
623 dev_err(&master->dev,
624 "failed to transfer one message from queue\n");
625 return;
626 }
627}
628
629static int spi_init_queue(struct spi_master *master)
630{
631 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
632
633 INIT_LIST_HEAD(&master->queue);
634 spin_lock_init(&master->queue_lock);
635
636 master->running = false;
637 master->busy = false;
638
639 init_kthread_worker(&master->kworker);
640 master->kworker_task = kthread_run(kthread_worker_fn,
Kees Cookf1701682013-07-03 15:04:58 -0700641 &master->kworker, "%s",
Linus Walleijffbbdd212012-02-22 10:05:38 +0100642 dev_name(&master->dev));
643 if (IS_ERR(master->kworker_task)) {
644 dev_err(&master->dev, "failed to create message pump task\n");
645 return -ENOMEM;
646 }
647 init_kthread_work(&master->pump_messages, spi_pump_messages);
648
649 /*
650 * Master config will indicate if this controller should run the
651 * message pump with high (realtime) priority to reduce the transfer
652 * latency on the bus by minimising the delay between a transfer
653 * request and the scheduling of the message pump thread. Without this
654 * setting the message pump thread will remain at default priority.
655 */
656 if (master->rt) {
657 dev_info(&master->dev,
658 "will run message pump with realtime priority\n");
659 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
660 }
661
662 return 0;
663}
664
665/**
666 * spi_get_next_queued_message() - called by driver to check for queued
667 * messages
668 * @master: the master to check for queued messages
669 *
670 * If there are more messages in the queue, the next message is returned from
671 * this call.
672 */
673struct spi_message *spi_get_next_queued_message(struct spi_master *master)
674{
675 struct spi_message *next;
676 unsigned long flags;
677
678 /* get a pointer to the next message, if any */
679 spin_lock_irqsave(&master->queue_lock, flags);
680 if (list_empty(&master->queue))
681 next = NULL;
682 else
683 next = list_entry(master->queue.next,
684 struct spi_message, queue);
685 spin_unlock_irqrestore(&master->queue_lock, flags);
686
687 return next;
688}
689EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
690
691/**
692 * spi_finalize_current_message() - the current message is complete
693 * @master: the master to return the message to
694 *
695 * Called by the driver to notify the core that the message in the front of the
696 * queue is complete and can be removed from the queue.
697 */
698void spi_finalize_current_message(struct spi_master *master)
699{
700 struct spi_message *mesg;
701 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +0100702 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +0100703
704 spin_lock_irqsave(&master->queue_lock, flags);
705 mesg = master->cur_msg;
706 master->cur_msg = NULL;
707
708 queue_kthread_work(&master->kworker, &master->pump_messages);
709 spin_unlock_irqrestore(&master->queue_lock, flags);
710
Mark Brown2841a5f2013-10-05 00:23:12 +0100711 if (master->cur_msg_prepared && master->unprepare_message) {
712 ret = master->unprepare_message(master, mesg);
713 if (ret) {
714 dev_err(&master->dev,
715 "failed to unprepare message: %d\n", ret);
716 }
717 }
718 master->cur_msg_prepared = false;
719
Linus Walleijffbbdd212012-02-22 10:05:38 +0100720 mesg->state = NULL;
721 if (mesg->complete)
722 mesg->complete(mesg->context);
Mark Brown56ec1972013-10-07 19:33:53 +0100723
724 trace_spi_message_done(mesg);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100725}
726EXPORT_SYMBOL_GPL(spi_finalize_current_message);
727
728static int spi_start_queue(struct spi_master *master)
729{
730 unsigned long flags;
731
732 spin_lock_irqsave(&master->queue_lock, flags);
733
734 if (master->running || master->busy) {
735 spin_unlock_irqrestore(&master->queue_lock, flags);
736 return -EBUSY;
737 }
738
739 master->running = true;
740 master->cur_msg = NULL;
741 spin_unlock_irqrestore(&master->queue_lock, flags);
742
743 queue_kthread_work(&master->kworker, &master->pump_messages);
744
745 return 0;
746}
747
748static int spi_stop_queue(struct spi_master *master)
749{
750 unsigned long flags;
751 unsigned limit = 500;
752 int ret = 0;
753
754 spin_lock_irqsave(&master->queue_lock, flags);
755
756 /*
757 * This is a bit lame, but is optimized for the common execution path.
758 * A wait_queue on the master->busy could be used, but then the common
759 * execution path (pump_messages) would be required to call wake_up or
760 * friends on every SPI message. Do this instead.
761 */
762 while ((!list_empty(&master->queue) || master->busy) && limit--) {
763 spin_unlock_irqrestore(&master->queue_lock, flags);
764 msleep(10);
765 spin_lock_irqsave(&master->queue_lock, flags);
766 }
767
768 if (!list_empty(&master->queue) || master->busy)
769 ret = -EBUSY;
770 else
771 master->running = false;
772
773 spin_unlock_irqrestore(&master->queue_lock, flags);
774
775 if (ret) {
776 dev_warn(&master->dev,
777 "could not stop message queue\n");
778 return ret;
779 }
780 return ret;
781}
782
783static int spi_destroy_queue(struct spi_master *master)
784{
785 int ret;
786
787 ret = spi_stop_queue(master);
788
789 /*
790 * flush_kthread_worker will block until all work is done.
791 * If the reason that stop_queue timed out is that the work will never
792 * finish, then it does no good to call flush/stop thread, so
793 * return anyway.
794 */
795 if (ret) {
796 dev_err(&master->dev, "problem destroying queue\n");
797 return ret;
798 }
799
800 flush_kthread_worker(&master->kworker);
801 kthread_stop(master->kworker_task);
802
803 return 0;
804}
805
806/**
807 * spi_queued_transfer - transfer function for queued transfers
808 * @spi: spi device which is requesting transfer
809 * @msg: spi message which is to handled is queued to driver queue
810 */
811static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
812{
813 struct spi_master *master = spi->master;
814 unsigned long flags;
815
816 spin_lock_irqsave(&master->queue_lock, flags);
817
818 if (!master->running) {
819 spin_unlock_irqrestore(&master->queue_lock, flags);
820 return -ESHUTDOWN;
821 }
822 msg->actual_length = 0;
823 msg->status = -EINPROGRESS;
824
825 list_add_tail(&msg->queue, &master->queue);
Axel Lin96b3eac2013-08-22 23:41:34 +0800826 if (!master->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +0100827 queue_kthread_work(&master->kworker, &master->pump_messages);
828
829 spin_unlock_irqrestore(&master->queue_lock, flags);
830 return 0;
831}
832
833static int spi_master_initialize_queue(struct spi_master *master)
834{
835 int ret;
836
837 master->queued = true;
838 master->transfer = spi_queued_transfer;
839
840 /* Initialize and start queue */
841 ret = spi_init_queue(master);
842 if (ret) {
843 dev_err(&master->dev, "problem initializing queue\n");
844 goto err_init_queue;
845 }
846 ret = spi_start_queue(master);
847 if (ret) {
848 dev_err(&master->dev, "problem starting queue\n");
849 goto err_start_queue;
850 }
851
852 return 0;
853
854err_start_queue:
855err_init_queue:
856 spi_destroy_queue(master);
857 return ret;
858}
859
860/*-------------------------------------------------------------------------*/
861
Andreas Larsson7cb94362012-12-04 15:09:38 +0100862#if defined(CONFIG_OF)
Grant Likelyd57a4282012-04-07 14:16:53 -0600863/**
864 * of_register_spi_devices() - Register child devices onto the SPI bus
865 * @master: Pointer to spi_master device
866 *
867 * Registers an spi_device for each child node of master node which has a 'reg'
868 * property.
869 */
870static void of_register_spi_devices(struct spi_master *master)
871{
872 struct spi_device *spi;
873 struct device_node *nc;
874 const __be32 *prop;
David Daneycb719412012-05-22 15:47:19 -0700875 char modalias[SPI_NAME_SIZE + 4];
Grant Likelyd57a4282012-04-07 14:16:53 -0600876 int rc;
877 int len;
878
879 if (!master->dev.of_node)
880 return;
881
Alexander Sverdlinf3b61592012-11-29 08:59:29 +0100882 for_each_available_child_of_node(master->dev.of_node, nc) {
Grant Likelyd57a4282012-04-07 14:16:53 -0600883 /* Alloc an spi_device */
884 spi = spi_alloc_device(master);
885 if (!spi) {
886 dev_err(&master->dev, "spi_device alloc error for %s\n",
887 nc->full_name);
888 spi_dev_put(spi);
889 continue;
890 }
891
892 /* Select device driver */
893 if (of_modalias_node(nc, spi->modalias,
894 sizeof(spi->modalias)) < 0) {
895 dev_err(&master->dev, "cannot find modalias for %s\n",
896 nc->full_name);
897 spi_dev_put(spi);
898 continue;
899 }
900
901 /* Device address */
902 prop = of_get_property(nc, "reg", &len);
903 if (!prop || len < sizeof(*prop)) {
904 dev_err(&master->dev, "%s has no 'reg' property\n",
905 nc->full_name);
906 spi_dev_put(spi);
907 continue;
908 }
909 spi->chip_select = be32_to_cpup(prop);
910
911 /* Mode (clock phase/polarity/etc.) */
912 if (of_find_property(nc, "spi-cpha", NULL))
913 spi->mode |= SPI_CPHA;
914 if (of_find_property(nc, "spi-cpol", NULL))
915 spi->mode |= SPI_CPOL;
916 if (of_find_property(nc, "spi-cs-high", NULL))
917 spi->mode |= SPI_CS_HIGH;
Lars-Peter Clausenc20151d2012-12-06 16:55:33 +0100918 if (of_find_property(nc, "spi-3wire", NULL))
919 spi->mode |= SPI_3WIRE;
Grant Likelyd57a4282012-04-07 14:16:53 -0600920
wangyuhangf477b7f2013-08-11 18:15:17 +0800921 /* Device DUAL/QUAD mode */
wangyuhanga110f932013-09-01 17:36:21 +0800922 prop = of_get_property(nc, "spi-tx-bus-width", &len);
Mark Browna822e992013-08-30 23:19:40 +0100923 if (prop && len == sizeof(*prop)) {
924 switch (be32_to_cpup(prop)) {
925 case SPI_NBITS_SINGLE:
926 break;
927 case SPI_NBITS_DUAL:
928 spi->mode |= SPI_TX_DUAL;
929 break;
930 case SPI_NBITS_QUAD:
931 spi->mode |= SPI_TX_QUAD;
932 break;
933 default:
934 dev_err(&master->dev,
wangyuhanga110f932013-09-01 17:36:21 +0800935 "spi-tx-bus-width %d not supported\n",
Mark Browna822e992013-08-30 23:19:40 +0100936 be32_to_cpup(prop));
937 spi_dev_put(spi);
938 continue;
939 }
wangyuhangf477b7f2013-08-11 18:15:17 +0800940 }
941
wangyuhanga110f932013-09-01 17:36:21 +0800942 prop = of_get_property(nc, "spi-rx-bus-width", &len);
Mark Browna822e992013-08-30 23:19:40 +0100943 if (prop && len == sizeof(*prop)) {
944 switch (be32_to_cpup(prop)) {
945 case SPI_NBITS_SINGLE:
946 break;
947 case SPI_NBITS_DUAL:
948 spi->mode |= SPI_RX_DUAL;
949 break;
950 case SPI_NBITS_QUAD:
951 spi->mode |= SPI_RX_QUAD;
952 break;
953 default:
954 dev_err(&master->dev,
wangyuhanga110f932013-09-01 17:36:21 +0800955 "spi-rx-bus-width %d not supported\n",
Mark Browna822e992013-08-30 23:19:40 +0100956 be32_to_cpup(prop));
957 spi_dev_put(spi);
958 continue;
959 }
wangyuhangf477b7f2013-08-11 18:15:17 +0800960 }
961
Grant Likelyd57a4282012-04-07 14:16:53 -0600962 /* Device speed */
963 prop = of_get_property(nc, "spi-max-frequency", &len);
964 if (!prop || len < sizeof(*prop)) {
965 dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n",
966 nc->full_name);
967 spi_dev_put(spi);
968 continue;
969 }
970 spi->max_speed_hz = be32_to_cpup(prop);
971
972 /* IRQ */
973 spi->irq = irq_of_parse_and_map(nc, 0);
974
975 /* Store a pointer to the node in the device structure */
976 of_node_get(nc);
977 spi->dev.of_node = nc;
978
979 /* Register the new device */
David Daneycb719412012-05-22 15:47:19 -0700980 snprintf(modalias, sizeof(modalias), "%s%s", SPI_MODULE_PREFIX,
981 spi->modalias);
982 request_module(modalias);
Grant Likelyd57a4282012-04-07 14:16:53 -0600983 rc = spi_add_device(spi);
984 if (rc) {
985 dev_err(&master->dev, "spi_device register error %s\n",
986 nc->full_name);
987 spi_dev_put(spi);
988 }
989
990 }
991}
992#else
993static void of_register_spi_devices(struct spi_master *master) { }
994#endif
995
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100996#ifdef CONFIG_ACPI
997static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
998{
999 struct spi_device *spi = data;
1000
1001 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1002 struct acpi_resource_spi_serialbus *sb;
1003
1004 sb = &ares->data.spi_serial_bus;
1005 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1006 spi->chip_select = sb->device_selection;
1007 spi->max_speed_hz = sb->connection_speed;
1008
1009 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1010 spi->mode |= SPI_CPHA;
1011 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1012 spi->mode |= SPI_CPOL;
1013 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1014 spi->mode |= SPI_CS_HIGH;
1015 }
1016 } else if (spi->irq < 0) {
1017 struct resource r;
1018
1019 if (acpi_dev_resource_interrupt(ares, 0, &r))
1020 spi->irq = r.start;
1021 }
1022
1023 /* Always tell the ACPI core to skip this resource */
1024 return 1;
1025}
1026
1027static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1028 void *data, void **return_value)
1029{
1030 struct spi_master *master = data;
1031 struct list_head resource_list;
1032 struct acpi_device *adev;
1033 struct spi_device *spi;
1034 int ret;
1035
1036 if (acpi_bus_get_device(handle, &adev))
1037 return AE_OK;
1038 if (acpi_bus_get_status(adev) || !adev->status.present)
1039 return AE_OK;
1040
1041 spi = spi_alloc_device(master);
1042 if (!spi) {
1043 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1044 dev_name(&adev->dev));
1045 return AE_NO_MEMORY;
1046 }
1047
1048 ACPI_HANDLE_SET(&spi->dev, handle);
1049 spi->irq = -1;
1050
1051 INIT_LIST_HEAD(&resource_list);
1052 ret = acpi_dev_get_resources(adev, &resource_list,
1053 acpi_spi_add_resource, spi);
1054 acpi_dev_free_resource_list(&resource_list);
1055
1056 if (ret < 0 || !spi->max_speed_hz) {
1057 spi_dev_put(spi);
1058 return AE_OK;
1059 }
1060
1061 strlcpy(spi->modalias, dev_name(&adev->dev), sizeof(spi->modalias));
1062 if (spi_add_device(spi)) {
1063 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1064 dev_name(&adev->dev));
1065 spi_dev_put(spi);
1066 }
1067
1068 return AE_OK;
1069}
1070
1071static void acpi_register_spi_devices(struct spi_master *master)
1072{
1073 acpi_status status;
1074 acpi_handle handle;
1075
Rafael J. Wysocki29896172013-04-01 00:21:08 +00001076 handle = ACPI_HANDLE(master->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001077 if (!handle)
1078 return;
1079
1080 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1081 acpi_spi_add_device, NULL,
1082 master, NULL);
1083 if (ACPI_FAILURE(status))
1084 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1085}
1086#else
1087static inline void acpi_register_spi_devices(struct spi_master *master) {}
1088#endif /* CONFIG_ACPI */
1089
Tony Jones49dce682007-10-16 01:27:48 -07001090static void spi_master_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001091{
1092 struct spi_master *master;
1093
Tony Jones49dce682007-10-16 01:27:48 -07001094 master = container_of(dev, struct spi_master, dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001095 kfree(master);
1096}
1097
1098static struct class spi_master_class = {
1099 .name = "spi_master",
1100 .owner = THIS_MODULE,
Tony Jones49dce682007-10-16 01:27:48 -07001101 .dev_release = spi_master_release,
David Brownell8ae12a02006-01-08 13:34:19 -08001102};
1103
1104
Linus Walleijffbbdd212012-02-22 10:05:38 +01001105
David Brownell8ae12a02006-01-08 13:34:19 -08001106/**
1107 * spi_alloc_master - allocate SPI master controller
1108 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001109 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001110 * memory is in the driver_data field of the returned device,
David Brownell0c868462006-01-08 13:34:25 -08001111 * accessible with spi_master_get_devdata().
David Brownell33e34dc2007-05-08 00:32:21 -07001112 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001113 *
1114 * This call is used only by SPI master controller drivers, which are the
1115 * only ones directly touching chip registers. It's how they allocate
dmitry pervushinba1a0512006-05-20 15:00:14 -07001116 * an spi_master structure, prior to calling spi_register_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001117 *
1118 * This must be called from context that can sleep. It returns the SPI
1119 * master structure on success, else NULL.
1120 *
1121 * The caller is responsible for assigning the bus number and initializing
dmitry pervushinba1a0512006-05-20 15:00:14 -07001122 * the master's methods before calling spi_register_master(); and (after errors
Uwe Kleine-Königeb4af0f2012-02-23 10:40:14 +01001123 * adding the device) calling spi_master_put() and kfree() to prevent a memory
1124 * leak.
David Brownell8ae12a02006-01-08 13:34:19 -08001125 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001126struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
David Brownell8ae12a02006-01-08 13:34:19 -08001127{
1128 struct spi_master *master;
1129
David Brownell0c868462006-01-08 13:34:25 -08001130 if (!dev)
1131 return NULL;
1132
Christoph Lametere94b1762006-12-06 20:33:17 -08001133 master = kzalloc(size + sizeof *master, GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -08001134 if (!master)
1135 return NULL;
1136
Tony Jones49dce682007-10-16 01:27:48 -07001137 device_initialize(&master->dev);
Grant Likely1e8a52e2012-05-19 23:42:08 -06001138 master->bus_num = -1;
1139 master->num_chipselect = 1;
Tony Jones49dce682007-10-16 01:27:48 -07001140 master->dev.class = &spi_master_class;
1141 master->dev.parent = get_device(dev);
David Brownell0c868462006-01-08 13:34:25 -08001142 spi_master_set_devdata(master, &master[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001143
1144 return master;
1145}
1146EXPORT_SYMBOL_GPL(spi_alloc_master);
1147
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001148#ifdef CONFIG_OF
1149static int of_spi_register_master(struct spi_master *master)
1150{
Grant Likelye80beb22013-02-12 17:48:37 +00001151 int nb, i, *cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001152 struct device_node *np = master->dev.of_node;
1153
1154 if (!np)
1155 return 0;
1156
1157 nb = of_gpio_named_count(np, "cs-gpios");
Grant Likelye80beb22013-02-12 17:48:37 +00001158 master->num_chipselect = max(nb, (int)master->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001159
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001160 /* Return error only for an incorrectly formed cs-gpios property */
1161 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001162 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001163 else if (nb < 0)
1164 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001165
1166 cs = devm_kzalloc(&master->dev,
1167 sizeof(int) * master->num_chipselect,
1168 GFP_KERNEL);
1169 master->cs_gpios = cs;
1170
1171 if (!master->cs_gpios)
1172 return -ENOMEM;
1173
Andreas Larsson0da83bb2013-01-29 15:53:40 +01001174 for (i = 0; i < master->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01001175 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001176
1177 for (i = 0; i < nb; i++)
1178 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1179
1180 return 0;
1181}
1182#else
1183static int of_spi_register_master(struct spi_master *master)
1184{
1185 return 0;
1186}
1187#endif
1188
David Brownell8ae12a02006-01-08 13:34:19 -08001189/**
1190 * spi_register_master - register SPI master controller
1191 * @master: initialized master, originally from spi_alloc_master()
David Brownell33e34dc2007-05-08 00:32:21 -07001192 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001193 *
1194 * SPI master controllers connect to their drivers using some non-SPI bus,
1195 * such as the platform bus. The final stage of probe() in that code
1196 * includes calling spi_register_master() to hook up to this SPI bus glue.
1197 *
1198 * SPI controllers use board specific (often SOC specific) bus numbers,
1199 * and board-specific addressing for SPI devices combines those numbers
1200 * with chip select numbers. Since SPI does not directly support dynamic
1201 * device identification, boards need configuration tables telling which
1202 * chip is at which address.
1203 *
1204 * This must be called from context that can sleep. It returns zero on
1205 * success, else a negative error code (dropping the master's refcount).
David Brownell0c868462006-01-08 13:34:25 -08001206 * After a successful return, the caller is responsible for calling
1207 * spi_unregister_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001208 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001209int spi_register_master(struct spi_master *master)
David Brownell8ae12a02006-01-08 13:34:19 -08001210{
David Brownelle44a45a2007-06-03 13:50:40 -07001211 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Tony Jones49dce682007-10-16 01:27:48 -07001212 struct device *dev = master->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08001213 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08001214 int status = -ENODEV;
1215 int dynamic = 0;
1216
David Brownell0c868462006-01-08 13:34:25 -08001217 if (!dev)
1218 return -ENODEV;
1219
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001220 status = of_spi_register_master(master);
1221 if (status)
1222 return status;
1223
David Brownell082c8cb2007-07-31 00:39:45 -07001224 /* even if it's just one always-selected device, there must
1225 * be at least one chipselect
1226 */
1227 if (master->num_chipselect == 0)
1228 return -EINVAL;
1229
Grant Likelybb297852012-12-21 19:32:09 +00001230 if ((master->bus_num < 0) && master->dev.of_node)
1231 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1232
David Brownell8ae12a02006-01-08 13:34:19 -08001233 /* convention: dynamically assigned bus IDs count down from the max */
David Brownella020ed72006-04-03 15:49:04 -07001234 if (master->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07001235 /* FIXME switch to an IDR based scheme, something like
1236 * I2C now uses, so we can't run out of "dynamic" IDs
1237 */
David Brownell8ae12a02006-01-08 13:34:19 -08001238 master->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08001239 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08001240 }
1241
Ernst Schwabcf32b712010-06-28 17:49:29 -07001242 spin_lock_init(&master->bus_lock_spinlock);
1243 mutex_init(&master->bus_lock_mutex);
1244 master->bus_lock_flag = 0;
1245
David Brownell8ae12a02006-01-08 13:34:19 -08001246 /* register the device, then userspace will see it.
1247 * registration fails if the bus ID is in use.
1248 */
Kay Sievers35f74fc2009-01-06 10:44:37 -08001249 dev_set_name(&master->dev, "spi%u", master->bus_num);
Tony Jones49dce682007-10-16 01:27:48 -07001250 status = device_add(&master->dev);
David Brownellb8852442006-01-08 13:34:23 -08001251 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08001252 goto done;
Kay Sievers35f74fc2009-01-06 10:44:37 -08001253 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
David Brownell8ae12a02006-01-08 13:34:19 -08001254 dynamic ? " (dynamic)" : "");
1255
Linus Walleijffbbdd212012-02-22 10:05:38 +01001256 /* If we're using a queued driver, start the queue */
1257 if (master->transfer)
1258 dev_info(dev, "master is unqueued, this is deprecated\n");
1259 else {
1260 status = spi_master_initialize_queue(master);
1261 if (status) {
Axel Line93b0722013-08-31 20:25:52 +08001262 device_del(&master->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001263 goto done;
1264 }
1265 }
1266
Feng Tang2b9603a2010-08-02 15:52:15 +08001267 mutex_lock(&board_lock);
1268 list_add_tail(&master->list, &spi_master_list);
1269 list_for_each_entry(bi, &board_list, list)
1270 spi_match_master_to_boardinfo(master, &bi->board_info);
1271 mutex_unlock(&board_lock);
1272
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001273 /* Register devices from the device tree and ACPI */
Anatolij Gustschin12b15e82010-07-27 22:35:58 +02001274 of_register_spi_devices(master);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001275 acpi_register_spi_devices(master);
David Brownell8ae12a02006-01-08 13:34:19 -08001276done:
1277 return status;
1278}
1279EXPORT_SYMBOL_GPL(spi_register_master);
1280
David Lamparter34860082010-08-30 23:54:17 +02001281static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08001282{
David Lamparter34860082010-08-30 23:54:17 +02001283 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08001284 return 0;
1285}
1286
1287/**
1288 * spi_unregister_master - unregister SPI master controller
1289 * @master: the master being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07001290 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001291 *
1292 * This call is used only by SPI master controller drivers, which are the
1293 * only ones directly touching chip registers.
1294 *
1295 * This must be called from context that can sleep.
1296 */
1297void spi_unregister_master(struct spi_master *master)
1298{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08001299 int dummy;
1300
Linus Walleijffbbdd212012-02-22 10:05:38 +01001301 if (master->queued) {
1302 if (spi_destroy_queue(master))
1303 dev_err(&master->dev, "queue remove failed\n");
1304 }
1305
Feng Tang2b9603a2010-08-02 15:52:15 +08001306 mutex_lock(&board_lock);
1307 list_del(&master->list);
1308 mutex_unlock(&board_lock);
1309
Sebastian Andrzej Siewior97dbf372010-12-21 17:24:31 -08001310 dummy = device_for_each_child(&master->dev, NULL, __unregister);
Tony Jones49dce682007-10-16 01:27:48 -07001311 device_unregister(&master->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001312}
1313EXPORT_SYMBOL_GPL(spi_unregister_master);
1314
Linus Walleijffbbdd212012-02-22 10:05:38 +01001315int spi_master_suspend(struct spi_master *master)
1316{
1317 int ret;
1318
1319 /* Basically no-ops for non-queued masters */
1320 if (!master->queued)
1321 return 0;
1322
1323 ret = spi_stop_queue(master);
1324 if (ret)
1325 dev_err(&master->dev, "queue stop failed\n");
1326
1327 return ret;
1328}
1329EXPORT_SYMBOL_GPL(spi_master_suspend);
1330
1331int spi_master_resume(struct spi_master *master)
1332{
1333 int ret;
1334
1335 if (!master->queued)
1336 return 0;
1337
1338 ret = spi_start_queue(master);
1339 if (ret)
1340 dev_err(&master->dev, "queue restart failed\n");
1341
1342 return ret;
1343}
1344EXPORT_SYMBOL_GPL(spi_master_resume);
1345
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001346static int __spi_master_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08001347{
1348 struct spi_master *m;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001349 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08001350
1351 m = container_of(dev, struct spi_master, dev);
1352 return m->bus_num == *bus_num;
1353}
1354
David Brownell8ae12a02006-01-08 13:34:19 -08001355/**
1356 * spi_busnum_to_master - look up master associated with bus_num
1357 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07001358 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001359 *
1360 * This call may be used with devices that are registered after
1361 * arch init time. It returns a refcounted pointer to the relevant
1362 * spi_master (which the caller must release), or NULL if there is
1363 * no such master registered.
1364 */
1365struct spi_master *spi_busnum_to_master(u16 bus_num)
1366{
Tony Jones49dce682007-10-16 01:27:48 -07001367 struct device *dev;
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001368 struct spi_master *master = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08001369
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001370 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Dave Young5ed2c832008-01-22 15:14:18 +08001371 __spi_master_match);
1372 if (dev)
1373 master = container_of(dev, struct spi_master, dev);
1374 /* reference got in class_find_device */
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001375 return master;
David Brownell8ae12a02006-01-08 13:34:19 -08001376}
1377EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1378
1379
1380/*-------------------------------------------------------------------------*/
1381
David Brownell7d077192009-06-17 16:26:03 -07001382/* Core methods for SPI master protocol drivers. Some of the
1383 * other core methods are currently defined as inline functions.
1384 */
1385
1386/**
1387 * spi_setup - setup SPI mode and clock rate
1388 * @spi: the device whose settings are being modified
1389 * Context: can sleep, and no requests are queued to the device
1390 *
1391 * SPI protocol drivers may need to update the transfer mode if the
1392 * device doesn't work with its default. They may likewise need
1393 * to update clock rates or word sizes from initial values. This function
1394 * changes those settings, and must be called from a context that can sleep.
1395 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
1396 * effect the next time the device is selected and data is transferred to
1397 * or from it. When this function returns, the spi device is deselected.
1398 *
1399 * Note that this call will fail if the protocol driver specifies an option
1400 * that the underlying controller or its driver does not support. For
1401 * example, not all hardware supports wire transfers using nine bit words,
1402 * LSB-first wire encoding, or active-high chipselects.
1403 */
1404int spi_setup(struct spi_device *spi)
1405{
David Brownelle7db06b2009-06-17 16:26:04 -07001406 unsigned bad_bits;
Laxman Dewangancaae0702012-11-09 14:35:22 +05301407 int status = 0;
David Brownell7d077192009-06-17 16:26:03 -07001408
wangyuhangf477b7f2013-08-11 18:15:17 +08001409 /* check mode to prevent that DUAL and QUAD set at the same time
1410 */
1411 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1412 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1413 dev_err(&spi->dev,
1414 "setup: can not select dual and quad at the same time\n");
1415 return -EINVAL;
1416 }
1417 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1418 */
1419 if ((spi->mode & SPI_3WIRE) && (spi->mode &
1420 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1421 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07001422 /* help drivers fail *cleanly* when they need options
1423 * that aren't supported with their current master
1424 */
1425 bad_bits = spi->mode & ~spi->master->mode_bits;
1426 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02001427 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07001428 bad_bits);
1429 return -EINVAL;
1430 }
1431
David Brownell7d077192009-06-17 16:26:03 -07001432 if (!spi->bits_per_word)
1433 spi->bits_per_word = 8;
1434
Laxman Dewangancaae0702012-11-09 14:35:22 +05301435 if (spi->master->setup)
1436 status = spi->master->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07001437
1438 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
1439 "%u bits/w, %u Hz max --> %d\n",
1440 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
1441 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
1442 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
1443 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
1444 (spi->mode & SPI_LOOP) ? "loopback, " : "",
1445 spi->bits_per_word, spi->max_speed_hz,
1446 status);
1447
1448 return status;
1449}
1450EXPORT_SYMBOL_GPL(spi_setup);
1451
Ernst Schwabcf32b712010-06-28 17:49:29 -07001452static int __spi_async(struct spi_device *spi, struct spi_message *message)
1453{
1454 struct spi_master *master = spi->master;
Laxman Dewangane6811d12012-11-09 14:36:45 +05301455 struct spi_transfer *xfer;
Ernst Schwabcf32b712010-06-28 17:49:29 -07001456
Mark Brown56ec1972013-10-07 19:33:53 +01001457 message->spi = spi;
1458
1459 trace_spi_message_submit(message);
1460
Mark Brown24a00132013-07-10 15:05:40 +01001461 if (list_empty(&message->transfers))
1462 return -EINVAL;
1463 if (!message->complete)
1464 return -EINVAL;
1465
Ernst Schwabcf32b712010-06-28 17:49:29 -07001466 /* Half-duplex links include original MicroWire, and ones with
1467 * only one data pin like SPI_3WIRE (switches direction) or where
1468 * either MOSI or MISO is missing. They can also be caused by
1469 * software limitations.
1470 */
1471 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
1472 || (spi->mode & SPI_3WIRE)) {
Ernst Schwabcf32b712010-06-28 17:49:29 -07001473 unsigned flags = master->flags;
1474
1475 list_for_each_entry(xfer, &message->transfers, transfer_list) {
1476 if (xfer->rx_buf && xfer->tx_buf)
1477 return -EINVAL;
1478 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
1479 return -EINVAL;
1480 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
1481 return -EINVAL;
1482 }
1483 }
1484
Laxman Dewangane6811d12012-11-09 14:36:45 +05301485 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05301486 * Set transfer bits_per_word and max speed as spi device default if
1487 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08001488 * Set transfer tx_nbits and rx_nbits as single transfer default
1489 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05301490 */
1491 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05301492 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05301493 if (!xfer->bits_per_word)
1494 xfer->bits_per_word = spi->bits_per_word;
Gabor Juhos56ede942013-08-14 10:25:28 +02001495 if (!xfer->speed_hz) {
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05301496 xfer->speed_hz = spi->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02001497 if (master->max_speed_hz &&
1498 xfer->speed_hz > master->max_speed_hz)
1499 xfer->speed_hz = master->max_speed_hz;
1500 }
1501
Stephen Warren543bb252013-03-26 20:37:57 -06001502 if (master->bits_per_word_mask) {
1503 /* Only 32 bits fit in the mask */
1504 if (xfer->bits_per_word > 32)
1505 return -EINVAL;
1506 if (!(master->bits_per_word_mask &
1507 BIT(xfer->bits_per_word - 1)))
1508 return -EINVAL;
1509 }
Mark Browna2fd4f92013-07-10 14:57:26 +01001510
1511 if (xfer->speed_hz && master->min_speed_hz &&
1512 xfer->speed_hz < master->min_speed_hz)
1513 return -EINVAL;
1514 if (xfer->speed_hz && master->max_speed_hz &&
1515 xfer->speed_hz > master->max_speed_hz)
wangyuhangd5ee7222013-08-30 18:05:10 +08001516 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08001517
1518 if (xfer->tx_buf && !xfer->tx_nbits)
1519 xfer->tx_nbits = SPI_NBITS_SINGLE;
1520 if (xfer->rx_buf && !xfer->rx_nbits)
1521 xfer->rx_nbits = SPI_NBITS_SINGLE;
1522 /* check transfer tx/rx_nbits:
1523 * 1. keep the value is not out of single, dual and quad
1524 * 2. keep tx/rx_nbits is contained by mode in spi_device
1525 * 3. if SPI_3WIRE, tx/rx_nbits should be in single
1526 */
Sourav Poddardb90a442013-08-22 21:20:48 +05301527 if (xfer->tx_buf) {
1528 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
1529 xfer->tx_nbits != SPI_NBITS_DUAL &&
1530 xfer->tx_nbits != SPI_NBITS_QUAD)
1531 return -EINVAL;
1532 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
1533 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
1534 return -EINVAL;
1535 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
1536 !(spi->mode & SPI_TX_QUAD))
1537 return -EINVAL;
1538 if ((spi->mode & SPI_3WIRE) &&
1539 (xfer->tx_nbits != SPI_NBITS_SINGLE))
1540 return -EINVAL;
1541 }
wangyuhangf477b7f2013-08-11 18:15:17 +08001542 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05301543 if (xfer->rx_buf) {
1544 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
1545 xfer->rx_nbits != SPI_NBITS_DUAL &&
1546 xfer->rx_nbits != SPI_NBITS_QUAD)
1547 return -EINVAL;
1548 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
1549 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
1550 return -EINVAL;
1551 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
1552 !(spi->mode & SPI_RX_QUAD))
1553 return -EINVAL;
1554 if ((spi->mode & SPI_3WIRE) &&
1555 (xfer->rx_nbits != SPI_NBITS_SINGLE))
1556 return -EINVAL;
1557 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05301558 }
1559
Ernst Schwabcf32b712010-06-28 17:49:29 -07001560 message->status = -EINPROGRESS;
1561 return master->transfer(spi, message);
1562}
1563
David Brownell568d0692009-09-22 16:46:18 -07001564/**
1565 * spi_async - asynchronous SPI transfer
1566 * @spi: device with which data will be exchanged
1567 * @message: describes the data transfers, including completion callback
1568 * Context: any (irqs may be blocked, etc)
1569 *
1570 * This call may be used in_irq and other contexts which can't sleep,
1571 * as well as from task contexts which can sleep.
1572 *
1573 * The completion callback is invoked in a context which can't sleep.
1574 * Before that invocation, the value of message->status is undefined.
1575 * When the callback is issued, message->status holds either zero (to
1576 * indicate complete success) or a negative error code. After that
1577 * callback returns, the driver which issued the transfer request may
1578 * deallocate the associated memory; it's no longer in use by any SPI
1579 * core or controller driver code.
1580 *
1581 * Note that although all messages to a spi_device are handled in
1582 * FIFO order, messages may go to different devices in other orders.
1583 * Some device might be higher priority, or have various "hard" access
1584 * time requirements, for example.
1585 *
1586 * On detection of any fault during the transfer, processing of
1587 * the entire message is aborted, and the device is deselected.
1588 * Until returning from the associated message completion callback,
1589 * no other spi_message queued to that device will be processed.
1590 * (This rule applies equally to all the synchronous transfer calls,
1591 * which are wrappers around this core asynchronous primitive.)
1592 */
1593int spi_async(struct spi_device *spi, struct spi_message *message)
1594{
1595 struct spi_master *master = spi->master;
Ernst Schwabcf32b712010-06-28 17:49:29 -07001596 int ret;
1597 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07001598
Ernst Schwabcf32b712010-06-28 17:49:29 -07001599 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07001600
Ernst Schwabcf32b712010-06-28 17:49:29 -07001601 if (master->bus_lock_flag)
1602 ret = -EBUSY;
1603 else
1604 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07001605
Ernst Schwabcf32b712010-06-28 17:49:29 -07001606 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1607
1608 return ret;
David Brownell568d0692009-09-22 16:46:18 -07001609}
1610EXPORT_SYMBOL_GPL(spi_async);
1611
Ernst Schwabcf32b712010-06-28 17:49:29 -07001612/**
1613 * spi_async_locked - version of spi_async with exclusive bus usage
1614 * @spi: device with which data will be exchanged
1615 * @message: describes the data transfers, including completion callback
1616 * Context: any (irqs may be blocked, etc)
1617 *
1618 * This call may be used in_irq and other contexts which can't sleep,
1619 * as well as from task contexts which can sleep.
1620 *
1621 * The completion callback is invoked in a context which can't sleep.
1622 * Before that invocation, the value of message->status is undefined.
1623 * When the callback is issued, message->status holds either zero (to
1624 * indicate complete success) or a negative error code. After that
1625 * callback returns, the driver which issued the transfer request may
1626 * deallocate the associated memory; it's no longer in use by any SPI
1627 * core or controller driver code.
1628 *
1629 * Note that although all messages to a spi_device are handled in
1630 * FIFO order, messages may go to different devices in other orders.
1631 * Some device might be higher priority, or have various "hard" access
1632 * time requirements, for example.
1633 *
1634 * On detection of any fault during the transfer, processing of
1635 * the entire message is aborted, and the device is deselected.
1636 * Until returning from the associated message completion callback,
1637 * no other spi_message queued to that device will be processed.
1638 * (This rule applies equally to all the synchronous transfer calls,
1639 * which are wrappers around this core asynchronous primitive.)
1640 */
1641int spi_async_locked(struct spi_device *spi, struct spi_message *message)
1642{
1643 struct spi_master *master = spi->master;
1644 int ret;
1645 unsigned long flags;
1646
1647 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1648
1649 ret = __spi_async(spi, message);
1650
1651 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1652
1653 return ret;
1654
1655}
1656EXPORT_SYMBOL_GPL(spi_async_locked);
1657
David Brownell7d077192009-06-17 16:26:03 -07001658
1659/*-------------------------------------------------------------------------*/
1660
1661/* Utility methods for SPI master protocol drivers, layered on
1662 * top of the core. Some other utility methods are defined as
1663 * inline functions.
1664 */
1665
Andrew Morton5d870c82006-01-11 11:23:49 -08001666static void spi_complete(void *arg)
1667{
1668 complete(arg);
1669}
1670
Ernst Schwabcf32b712010-06-28 17:49:29 -07001671static int __spi_sync(struct spi_device *spi, struct spi_message *message,
1672 int bus_locked)
1673{
1674 DECLARE_COMPLETION_ONSTACK(done);
1675 int status;
1676 struct spi_master *master = spi->master;
1677
1678 message->complete = spi_complete;
1679 message->context = &done;
1680
1681 if (!bus_locked)
1682 mutex_lock(&master->bus_lock_mutex);
1683
1684 status = spi_async_locked(spi, message);
1685
1686 if (!bus_locked)
1687 mutex_unlock(&master->bus_lock_mutex);
1688
1689 if (status == 0) {
1690 wait_for_completion(&done);
1691 status = message->status;
1692 }
1693 message->context = NULL;
1694 return status;
1695}
1696
David Brownell8ae12a02006-01-08 13:34:19 -08001697/**
1698 * spi_sync - blocking/synchronous SPI data transfers
1699 * @spi: device with which data will be exchanged
1700 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07001701 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001702 *
1703 * This call may only be used from a context that may sleep. The sleep
1704 * is non-interruptible, and has no timeout. Low-overhead controller
1705 * drivers may DMA directly into and out of the message buffers.
1706 *
1707 * Note that the SPI device's chip select is active during the message,
1708 * and then is normally disabled between messages. Drivers for some
1709 * frequently-used devices may want to minimize costs of selecting a chip,
1710 * by leaving it selected in anticipation that the next message will go
1711 * to the same chip. (That may increase power usage.)
1712 *
David Brownell0c868462006-01-08 13:34:25 -08001713 * Also, the caller is guaranteeing that the memory associated with the
1714 * message will not be freed before this call returns.
1715 *
Marc Pignat9b938b72007-12-04 23:45:10 -08001716 * It returns zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08001717 */
1718int spi_sync(struct spi_device *spi, struct spi_message *message)
1719{
Ernst Schwabcf32b712010-06-28 17:49:29 -07001720 return __spi_sync(spi, message, 0);
David Brownell8ae12a02006-01-08 13:34:19 -08001721}
1722EXPORT_SYMBOL_GPL(spi_sync);
1723
Ernst Schwabcf32b712010-06-28 17:49:29 -07001724/**
1725 * spi_sync_locked - version of spi_sync with exclusive bus usage
1726 * @spi: device with which data will be exchanged
1727 * @message: describes the data transfers
1728 * Context: can sleep
1729 *
1730 * This call may only be used from a context that may sleep. The sleep
1731 * is non-interruptible, and has no timeout. Low-overhead controller
1732 * drivers may DMA directly into and out of the message buffers.
1733 *
1734 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001735 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b712010-06-28 17:49:29 -07001736 * be released by a spi_bus_unlock call when the exclusive access is over.
1737 *
1738 * It returns zero on success, else a negative error code.
1739 */
1740int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
1741{
1742 return __spi_sync(spi, message, 1);
1743}
1744EXPORT_SYMBOL_GPL(spi_sync_locked);
1745
1746/**
1747 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
1748 * @master: SPI bus master that should be locked for exclusive bus access
1749 * Context: can sleep
1750 *
1751 * This call may only be used from a context that may sleep. The sleep
1752 * is non-interruptible, and has no timeout.
1753 *
1754 * This call should be used by drivers that require exclusive access to the
1755 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
1756 * exclusive access is over. Data transfer must be done by spi_sync_locked
1757 * and spi_async_locked calls when the SPI bus lock is held.
1758 *
1759 * It returns zero on success, else a negative error code.
1760 */
1761int spi_bus_lock(struct spi_master *master)
1762{
1763 unsigned long flags;
1764
1765 mutex_lock(&master->bus_lock_mutex);
1766
1767 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1768 master->bus_lock_flag = 1;
1769 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1770
1771 /* mutex remains locked until spi_bus_unlock is called */
1772
1773 return 0;
1774}
1775EXPORT_SYMBOL_GPL(spi_bus_lock);
1776
1777/**
1778 * spi_bus_unlock - release the lock for exclusive SPI bus usage
1779 * @master: SPI bus master that was locked for exclusive bus access
1780 * Context: can sleep
1781 *
1782 * This call may only be used from a context that may sleep. The sleep
1783 * is non-interruptible, and has no timeout.
1784 *
1785 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
1786 * call.
1787 *
1788 * It returns zero on success, else a negative error code.
1789 */
1790int spi_bus_unlock(struct spi_master *master)
1791{
1792 master->bus_lock_flag = 0;
1793
1794 mutex_unlock(&master->bus_lock_mutex);
1795
1796 return 0;
1797}
1798EXPORT_SYMBOL_GPL(spi_bus_unlock);
1799
David Brownella9948b62006-04-02 10:37:40 -08001800/* portable code must never pass more than 32 bytes */
1801#define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08001802
1803static u8 *buf;
1804
1805/**
1806 * spi_write_then_read - SPI synchronous write followed by read
1807 * @spi: device with which data will be exchanged
1808 * @txbuf: data to be written (need not be dma-safe)
1809 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07001810 * @rxbuf: buffer into which data will be read (need not be dma-safe)
1811 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07001812 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001813 *
1814 * This performs a half duplex MicroWire style transaction with the
1815 * device, sending txbuf and then reading rxbuf. The return value
1816 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08001817 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08001818 *
David Brownell0c868462006-01-08 13:34:25 -08001819 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07001820 * portable code should never use this for more than 32 bytes.
1821 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c868462006-01-08 13:34:25 -08001822 * spi_{async,sync}() calls with dma-safe buffers.
David Brownell8ae12a02006-01-08 13:34:19 -08001823 */
1824int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02001825 const void *txbuf, unsigned n_tx,
1826 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08001827{
David Brownell068f4072007-12-04 23:45:09 -08001828 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08001829
1830 int status;
1831 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07001832 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08001833 u8 *local_buf;
1834
Mark Brownb3a223e2012-12-02 12:54:25 +09001835 /* Use preallocated DMA-safe buffer if we can. We can't avoid
1836 * copying here, (as a pure convenience thing), but we can
1837 * keep heap costs out of the hot path unless someone else is
1838 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08001839 */
Mark Brownb3a223e2012-12-02 12:54:25 +09001840 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08001841 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
1842 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09001843 if (!local_buf)
1844 return -ENOMEM;
1845 } else {
1846 local_buf = buf;
1847 }
David Brownell8ae12a02006-01-08 13:34:19 -08001848
Vitaly Wool8275c642006-01-08 13:34:28 -08001849 spi_message_init(&message);
David Brownellbdff5492009-04-13 14:39:57 -07001850 memset(x, 0, sizeof x);
1851 if (n_tx) {
1852 x[0].len = n_tx;
1853 spi_message_add_tail(&x[0], &message);
1854 }
1855 if (n_rx) {
1856 x[1].len = n_rx;
1857 spi_message_add_tail(&x[1], &message);
1858 }
Vitaly Wool8275c642006-01-08 13:34:28 -08001859
David Brownell8ae12a02006-01-08 13:34:19 -08001860 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07001861 x[0].tx_buf = local_buf;
1862 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08001863
1864 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08001865 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08001866 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07001867 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08001868
David Brownellbdff5492009-04-13 14:39:57 -07001869 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08001870 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08001871 else
1872 kfree(local_buf);
1873
1874 return status;
1875}
1876EXPORT_SYMBOL_GPL(spi_write_then_read);
1877
1878/*-------------------------------------------------------------------------*/
1879
1880static int __init spi_init(void)
1881{
David Brownellb8852442006-01-08 13:34:23 -08001882 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08001883
Christoph Lametere94b1762006-12-06 20:33:17 -08001884 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08001885 if (!buf) {
1886 status = -ENOMEM;
1887 goto err0;
1888 }
1889
1890 status = bus_register(&spi_bus_type);
1891 if (status < 0)
1892 goto err1;
1893
1894 status = class_register(&spi_master_class);
1895 if (status < 0)
1896 goto err2;
David Brownell8ae12a02006-01-08 13:34:19 -08001897 return 0;
David Brownellb8852442006-01-08 13:34:23 -08001898
1899err2:
1900 bus_unregister(&spi_bus_type);
1901err1:
1902 kfree(buf);
1903 buf = NULL;
1904err0:
1905 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08001906}
David Brownellb8852442006-01-08 13:34:23 -08001907
David Brownell8ae12a02006-01-08 13:34:19 -08001908/* board_info is normally registered in arch_initcall(),
1909 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08001910 *
1911 * REVISIT only boardinfo really needs static linking. the rest (device and
1912 * driver registration) _could_ be dynamically linked (modular) ... costs
1913 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08001914 */
David Brownell673c0c02008-10-15 22:02:46 -07001915postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08001916