blob: 481e13f0db54e202a88fedd1a52533f9244ec7b6 [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}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070064static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080065
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070066static struct attribute *spi_dev_attrs[] = {
67 &dev_attr_modalias.attr,
68 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -080069};
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070070ATTRIBUTE_GROUPS(spi_dev);
David Brownell8ae12a02006-01-08 13:34:19 -080071
72/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
73 * and the sysfs version makes coldplug work too.
74 */
75
Anton Vorontsov75368bf2009-09-22 16:46:04 -070076static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
77 const struct spi_device *sdev)
78{
79 while (id->name[0]) {
80 if (!strcmp(sdev->modalias, id->name))
81 return id;
82 id++;
83 }
84 return NULL;
85}
86
87const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
88{
89 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
90
91 return spi_match_id(sdrv->id_table, sdev);
92}
93EXPORT_SYMBOL_GPL(spi_get_device_id);
94
David Brownell8ae12a02006-01-08 13:34:19 -080095static int spi_match_device(struct device *dev, struct device_driver *drv)
96{
97 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -070098 const struct spi_driver *sdrv = to_spi_driver(drv);
99
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600100 /* Attempt an OF style match */
101 if (of_driver_match_device(dev, drv))
102 return 1;
103
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100104 /* Then try ACPI */
105 if (acpi_driver_match_device(dev, drv))
106 return 1;
107
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700108 if (sdrv->id_table)
109 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800110
Kay Sievers35f74fc2009-01-06 10:44:37 -0800111 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800112}
113
Kay Sievers7eff2e72007-08-14 15:15:12 +0200114static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800115{
116 const struct spi_device *spi = to_spi_device(dev);
117
Anton Vorontsove0626e32009-09-22 16:46:08 -0700118 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800119 return 0;
120}
121
Mark Brown3ae22e82010-12-25 15:32:27 +0100122#ifdef CONFIG_PM_SLEEP
123static int spi_legacy_suspend(struct device *dev, pm_message_t message)
David Brownell8ae12a02006-01-08 13:34:19 -0800124{
David Brownell3c724262008-02-06 01:38:10 -0800125 int value = 0;
David Brownellb8852442006-01-08 13:34:23 -0800126 struct spi_driver *drv = to_spi_driver(dev->driver);
David Brownell8ae12a02006-01-08 13:34:19 -0800127
David Brownell8ae12a02006-01-08 13:34:19 -0800128 /* suspend will stop irqs and dma; no more i/o */
David Brownell3c724262008-02-06 01:38:10 -0800129 if (drv) {
130 if (drv->suspend)
131 value = drv->suspend(to_spi_device(dev), message);
132 else
133 dev_dbg(dev, "... can't suspend\n");
134 }
David Brownell8ae12a02006-01-08 13:34:19 -0800135 return value;
136}
137
Mark Brown3ae22e82010-12-25 15:32:27 +0100138static int spi_legacy_resume(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -0800139{
David Brownell3c724262008-02-06 01:38:10 -0800140 int value = 0;
David Brownellb8852442006-01-08 13:34:23 -0800141 struct spi_driver *drv = to_spi_driver(dev->driver);
David Brownell8ae12a02006-01-08 13:34:19 -0800142
David Brownell8ae12a02006-01-08 13:34:19 -0800143 /* resume may restart the i/o queue */
David Brownell3c724262008-02-06 01:38:10 -0800144 if (drv) {
145 if (drv->resume)
146 value = drv->resume(to_spi_device(dev));
147 else
148 dev_dbg(dev, "... can't resume\n");
149 }
David Brownell8ae12a02006-01-08 13:34:19 -0800150 return value;
151}
152
Mark Brown3ae22e82010-12-25 15:32:27 +0100153static int spi_pm_suspend(struct device *dev)
154{
155 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
156
157 if (pm)
158 return pm_generic_suspend(dev);
159 else
160 return spi_legacy_suspend(dev, PMSG_SUSPEND);
161}
162
163static int spi_pm_resume(struct device *dev)
164{
165 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
166
167 if (pm)
168 return pm_generic_resume(dev);
169 else
170 return spi_legacy_resume(dev);
171}
172
173static int spi_pm_freeze(struct device *dev)
174{
175 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
176
177 if (pm)
178 return pm_generic_freeze(dev);
179 else
180 return spi_legacy_suspend(dev, PMSG_FREEZE);
181}
182
183static int spi_pm_thaw(struct device *dev)
184{
185 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
186
187 if (pm)
188 return pm_generic_thaw(dev);
189 else
190 return spi_legacy_resume(dev);
191}
192
193static int spi_pm_poweroff(struct device *dev)
194{
195 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
196
197 if (pm)
198 return pm_generic_poweroff(dev);
199 else
200 return spi_legacy_suspend(dev, PMSG_HIBERNATE);
201}
202
203static int spi_pm_restore(struct device *dev)
204{
205 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
206
207 if (pm)
208 return pm_generic_restore(dev);
209 else
210 return spi_legacy_resume(dev);
211}
David Brownell8ae12a02006-01-08 13:34:19 -0800212#else
Mark Brown3ae22e82010-12-25 15:32:27 +0100213#define spi_pm_suspend NULL
214#define spi_pm_resume NULL
215#define spi_pm_freeze NULL
216#define spi_pm_thaw NULL
217#define spi_pm_poweroff NULL
218#define spi_pm_restore NULL
David Brownell8ae12a02006-01-08 13:34:19 -0800219#endif
220
Mark Brown3ae22e82010-12-25 15:32:27 +0100221static const struct dev_pm_ops spi_pm = {
222 .suspend = spi_pm_suspend,
223 .resume = spi_pm_resume,
224 .freeze = spi_pm_freeze,
225 .thaw = spi_pm_thaw,
226 .poweroff = spi_pm_poweroff,
227 .restore = spi_pm_restore,
228 SET_RUNTIME_PM_OPS(
229 pm_generic_runtime_suspend,
230 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200231 NULL
Mark Brown3ae22e82010-12-25 15:32:27 +0100232 )
233};
234
David Brownell8ae12a02006-01-08 13:34:19 -0800235struct bus_type spi_bus_type = {
236 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700237 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800238 .match = spi_match_device,
239 .uevent = spi_uevent,
Mark Brown3ae22e82010-12-25 15:32:27 +0100240 .pm = &spi_pm,
David Brownell8ae12a02006-01-08 13:34:19 -0800241};
242EXPORT_SYMBOL_GPL(spi_bus_type);
243
David Brownellb8852442006-01-08 13:34:23 -0800244
245static int spi_drv_probe(struct device *dev)
246{
247 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
248
249 return sdrv->probe(to_spi_device(dev));
250}
251
252static int spi_drv_remove(struct device *dev)
253{
254 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
255
256 return sdrv->remove(to_spi_device(dev));
257}
258
259static void spi_drv_shutdown(struct device *dev)
260{
261 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
262
263 sdrv->shutdown(to_spi_device(dev));
264}
265
David Brownell33e34dc2007-05-08 00:32:21 -0700266/**
267 * spi_register_driver - register a SPI driver
268 * @sdrv: the driver to register
269 * Context: can sleep
270 */
David Brownellb8852442006-01-08 13:34:23 -0800271int spi_register_driver(struct spi_driver *sdrv)
272{
273 sdrv->driver.bus = &spi_bus_type;
274 if (sdrv->probe)
275 sdrv->driver.probe = spi_drv_probe;
276 if (sdrv->remove)
277 sdrv->driver.remove = spi_drv_remove;
278 if (sdrv->shutdown)
279 sdrv->driver.shutdown = spi_drv_shutdown;
280 return driver_register(&sdrv->driver);
281}
282EXPORT_SYMBOL_GPL(spi_register_driver);
283
David Brownell8ae12a02006-01-08 13:34:19 -0800284/*-------------------------------------------------------------------------*/
285
286/* SPI devices should normally not be created by SPI device drivers; that
287 * would make them board-specific. Similarly with SPI master drivers.
288 * Device registration normally goes into like arch/.../mach.../board-YYY.c
289 * with other readonly (flashable) information about mainboard devices.
290 */
291
292struct boardinfo {
293 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800294 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800295};
296
297static LIST_HEAD(board_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800298static LIST_HEAD(spi_master_list);
299
300/*
301 * Used to protect add/del opertion for board_info list and
302 * spi_master list, and their matching process
303 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700304static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800305
Grant Likelydc87c982008-05-15 16:50:22 -0600306/**
307 * spi_alloc_device - Allocate a new SPI device
308 * @master: Controller to which device is connected
309 * Context: can sleep
310 *
311 * Allows a driver to allocate and initialize a spi_device without
312 * registering it immediately. This allows a driver to directly
313 * fill the spi_device with device parameters before calling
314 * spi_add_device() on it.
315 *
316 * Caller is responsible to call spi_add_device() on the returned
317 * spi_device structure to add it to the SPI master. If the caller
318 * needs to discard the spi_device without adding it, then it should
319 * call spi_dev_put() on it.
320 *
321 * Returns a pointer to the new device, or NULL.
322 */
323struct spi_device *spi_alloc_device(struct spi_master *master)
324{
325 struct spi_device *spi;
326 struct device *dev = master->dev.parent;
327
328 if (!spi_master_get(master))
329 return NULL;
330
Jingoo Han5fe5f052013-10-14 10:31:51 +0900331 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600332 if (!spi) {
333 dev_err(dev, "cannot alloc spi_device\n");
334 spi_master_put(master);
335 return NULL;
336 }
337
338 spi->master = master;
Laurent Pinchart178db7d2011-12-12 01:15:06 +0100339 spi->dev.parent = &master->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600340 spi->dev.bus = &spi_bus_type;
341 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100342 spi->cs_gpio = -ENOENT;
Grant Likelydc87c982008-05-15 16:50:22 -0600343 device_initialize(&spi->dev);
344 return spi;
345}
346EXPORT_SYMBOL_GPL(spi_alloc_device);
347
348/**
349 * spi_add_device - Add spi_device allocated with spi_alloc_device
350 * @spi: spi_device to register
351 *
352 * Companion function to spi_alloc_device. Devices allocated with
353 * spi_alloc_device can be added onto the spi bus with this function.
354 *
David Brownelle48880e2008-08-15 00:40:44 -0700355 * Returns 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600356 */
357int spi_add_device(struct spi_device *spi)
358{
David Brownelle48880e2008-08-15 00:40:44 -0700359 static DEFINE_MUTEX(spi_add_lock);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100360 struct spi_master *master = spi->master;
361 struct device *dev = master->dev.parent;
Roman Tereshonkov8ec130a2010-04-16 09:52:59 +0000362 struct device *d;
Grant Likelydc87c982008-05-15 16:50:22 -0600363 int status;
364
365 /* Chipselects are numbered 0..max; validate. */
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100366 if (spi->chip_select >= master->num_chipselect) {
Grant Likelydc87c982008-05-15 16:50:22 -0600367 dev_err(dev, "cs%d >= max %d\n",
368 spi->chip_select,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100369 master->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600370 return -EINVAL;
371 }
372
373 /* Set the bus ID string */
Kay Sievers35f74fc2009-01-06 10:44:37 -0800374 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
Grant Likelydc87c982008-05-15 16:50:22 -0600375 spi->chip_select);
376
David Brownelle48880e2008-08-15 00:40:44 -0700377
378 /* We need to make sure there's no other device with this
379 * chipselect **BEFORE** we call setup(), else we'll trash
380 * its configuration. Lock against concurrent add() calls.
381 */
382 mutex_lock(&spi_add_lock);
383
Roman Tereshonkov8ec130a2010-04-16 09:52:59 +0000384 d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
385 if (d != NULL) {
David Brownelle48880e2008-08-15 00:40:44 -0700386 dev_err(dev, "chipselect %d already in use\n",
387 spi->chip_select);
Roman Tereshonkov8ec130a2010-04-16 09:52:59 +0000388 put_device(d);
David Brownelle48880e2008-08-15 00:40:44 -0700389 status = -EBUSY;
390 goto done;
391 }
392
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100393 if (master->cs_gpios)
394 spi->cs_gpio = master->cs_gpios[spi->chip_select];
395
David Brownelle48880e2008-08-15 00:40:44 -0700396 /* Drivers may modify this initial i/o setup, but will
397 * normally rely on the device being setup. Devices
398 * using SPI_CS_HIGH can't coexist well otherwise...
399 */
David Brownell7d077192009-06-17 16:26:03 -0700400 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600401 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200402 dev_err(dev, "can't setup %s, status %d\n",
403 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700404 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600405 }
406
David Brownelle48880e2008-08-15 00:40:44 -0700407 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600408 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700409 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200410 dev_err(dev, "can't add %s, status %d\n",
411 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700412 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800413 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600414
David Brownelle48880e2008-08-15 00:40:44 -0700415done:
416 mutex_unlock(&spi_add_lock);
417 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600418}
419EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800420
David Brownell33e34dc2007-05-08 00:32:21 -0700421/**
422 * spi_new_device - instantiate one new SPI device
423 * @master: Controller to which device is connected
424 * @chip: Describes the SPI device
425 * Context: can sleep
426 *
427 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800428 * after board init creates the hard-wired devices. Some development
429 * platforms may not be able to use spi_register_board_info though, and
430 * this is exported so that for example a USB or parport based adapter
431 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700432 *
433 * Returns the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800434 */
Adrian Bunke9d5a462007-03-26 21:32:23 -0800435struct spi_device *spi_new_device(struct spi_master *master,
436 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800437{
438 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800439 int status;
440
David Brownell082c8cb2007-07-31 00:39:45 -0700441 /* NOTE: caller did any chip->bus_num checks necessary.
442 *
443 * Also, unless we change the return value convention to use
444 * error-or-pointer (not NULL-or-pointer), troubleshootability
445 * suggests syslogged diagnostics are best here (ugh).
446 */
447
Grant Likelydc87c982008-05-15 16:50:22 -0600448 proxy = spi_alloc_device(master);
449 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800450 return NULL;
451
Grant Likely102eb972008-07-23 21:29:55 -0700452 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
453
David Brownell8ae12a02006-01-08 13:34:19 -0800454 proxy->chip_select = chip->chip_select;
455 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700456 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800457 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700458 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800459 proxy->dev.platform_data = (void *) chip->platform_data;
460 proxy->controller_data = chip->controller_data;
461 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800462
Grant Likelydc87c982008-05-15 16:50:22 -0600463 status = spi_add_device(proxy);
David Brownell8ae12a02006-01-08 13:34:19 -0800464 if (status < 0) {
Grant Likelydc87c982008-05-15 16:50:22 -0600465 spi_dev_put(proxy);
466 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800467 }
468
David Brownell8ae12a02006-01-08 13:34:19 -0800469 return proxy;
470}
471EXPORT_SYMBOL_GPL(spi_new_device);
472
Feng Tang2b9603a2010-08-02 15:52:15 +0800473static void spi_match_master_to_boardinfo(struct spi_master *master,
474 struct spi_board_info *bi)
475{
476 struct spi_device *dev;
477
478 if (master->bus_num != bi->bus_num)
479 return;
480
481 dev = spi_new_device(master, bi);
482 if (!dev)
483 dev_err(master->dev.parent, "can't create new device for %s\n",
484 bi->modalias);
485}
486
David Brownell33e34dc2007-05-08 00:32:21 -0700487/**
488 * spi_register_board_info - register SPI devices for a given board
489 * @info: array of chip descriptors
490 * @n: how many descriptors are provided
491 * Context: can sleep
492 *
David Brownell8ae12a02006-01-08 13:34:19 -0800493 * Board-specific early init code calls this (probably during arch_initcall)
494 * with segments of the SPI device table. Any device nodes are created later,
495 * after the relevant parent SPI controller (bus_num) is defined. We keep
496 * this table of devices forever, so that reloading a controller driver will
497 * not make Linux forget about these hard-wired devices.
498 *
499 * Other code can also call this, e.g. a particular add-on board might provide
500 * SPI devices through its expansion connector, so code initializing that board
501 * would naturally declare its SPI devices.
502 *
503 * The board info passed can safely be __initdata ... but be careful of
504 * any embedded pointers (platform_data, etc), they're copied as-is.
505 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000506int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800507{
Feng Tang2b9603a2010-08-02 15:52:15 +0800508 struct boardinfo *bi;
509 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800510
Feng Tang2b9603a2010-08-02 15:52:15 +0800511 bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800512 if (!bi)
513 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800514
Feng Tang2b9603a2010-08-02 15:52:15 +0800515 for (i = 0; i < n; i++, bi++, info++) {
516 struct spi_master *master;
David Brownell8ae12a02006-01-08 13:34:19 -0800517
Feng Tang2b9603a2010-08-02 15:52:15 +0800518 memcpy(&bi->board_info, info, sizeof(*info));
519 mutex_lock(&board_lock);
520 list_add_tail(&bi->list, &board_list);
521 list_for_each_entry(master, &spi_master_list, list)
522 spi_match_master_to_boardinfo(master, &bi->board_info);
523 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800524 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800525
526 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800527}
528
529/*-------------------------------------------------------------------------*/
530
Mark Brownb1589352013-10-05 11:50:40 +0100531static void spi_set_cs(struct spi_device *spi, bool enable)
532{
533 if (spi->mode & SPI_CS_HIGH)
534 enable = !enable;
535
536 if (spi->cs_gpio >= 0)
537 gpio_set_value(spi->cs_gpio, !enable);
538 else if (spi->master->set_cs)
539 spi->master->set_cs(spi, !enable);
540}
541
542/*
543 * spi_transfer_one_message - Default implementation of transfer_one_message()
544 *
545 * This is a standard implementation of transfer_one_message() for
546 * drivers which impelment a transfer_one() operation. It provides
547 * standard handling of delays and chip select management.
548 */
549static int spi_transfer_one_message(struct spi_master *master,
550 struct spi_message *msg)
551{
552 struct spi_transfer *xfer;
553 bool cur_cs = true;
554 bool keep_cs = false;
555 int ret = 0;
556
557 spi_set_cs(msg->spi, true);
558
559 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
560 trace_spi_transfer_start(msg, xfer);
561
562 INIT_COMPLETION(master->xfer_completion);
563
564 ret = master->transfer_one(master, msg->spi, xfer);
565 if (ret < 0) {
566 dev_err(&msg->spi->dev,
567 "SPI transfer failed: %d\n", ret);
568 goto out;
569 }
570
571 if (ret > 0)
572 wait_for_completion(&master->xfer_completion);
573
574 trace_spi_transfer_stop(msg, xfer);
575
576 if (msg->status != -EINPROGRESS)
577 goto out;
578
579 if (xfer->delay_usecs)
580 udelay(xfer->delay_usecs);
581
582 if (xfer->cs_change) {
583 if (list_is_last(&xfer->transfer_list,
584 &msg->transfers)) {
585 keep_cs = true;
586 } else {
587 cur_cs = !cur_cs;
588 spi_set_cs(msg->spi, cur_cs);
589 }
590 }
591
592 msg->actual_length += xfer->len;
593 }
594
595out:
596 if (ret != 0 || !keep_cs)
597 spi_set_cs(msg->spi, false);
598
599 if (msg->status == -EINPROGRESS)
600 msg->status = ret;
601
602 spi_finalize_current_message(master);
603
604 return ret;
605}
606
607/**
608 * spi_finalize_current_transfer - report completion of a transfer
609 *
610 * Called by SPI drivers using the core transfer_one_message()
611 * implementation to notify it that the current interrupt driven
612 * transfer has finised and the next one may be scheduled.
613 */
614void spi_finalize_current_transfer(struct spi_master *master)
615{
616 complete(&master->xfer_completion);
617}
618EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
619
Linus Walleijffbbdd212012-02-22 10:05:38 +0100620/**
621 * spi_pump_messages - kthread work function which processes spi message queue
622 * @work: pointer to kthread work struct contained in the master struct
623 *
624 * This function checks if there is any spi message in the queue that
625 * needs processing and if so call out to the driver to initialize hardware
626 * and transfer each message.
627 *
628 */
629static void spi_pump_messages(struct kthread_work *work)
630{
631 struct spi_master *master =
632 container_of(work, struct spi_master, pump_messages);
633 unsigned long flags;
634 bool was_busy = false;
635 int ret;
636
637 /* Lock queue and check for queue work */
638 spin_lock_irqsave(&master->queue_lock, flags);
639 if (list_empty(&master->queue) || !master->running) {
Bryan Freedb0b36b82013-03-13 11:17:40 -0700640 if (!master->busy) {
641 spin_unlock_irqrestore(&master->queue_lock, flags);
642 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +0100643 }
644 master->busy = false;
645 spin_unlock_irqrestore(&master->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -0700646 if (master->unprepare_transfer_hardware &&
647 master->unprepare_transfer_hardware(master))
648 dev_err(&master->dev,
649 "failed to unprepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +0100650 if (master->auto_runtime_pm) {
651 pm_runtime_mark_last_busy(master->dev.parent);
652 pm_runtime_put_autosuspend(master->dev.parent);
653 }
Mark Brown56ec1972013-10-07 19:33:53 +0100654 trace_spi_master_idle(master);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100655 return;
656 }
657
658 /* Make sure we are not already running a message */
659 if (master->cur_msg) {
660 spin_unlock_irqrestore(&master->queue_lock, flags);
661 return;
662 }
663 /* Extract head of queue */
664 master->cur_msg =
665 list_entry(master->queue.next, struct spi_message, queue);
666
667 list_del_init(&master->cur_msg->queue);
668 if (master->busy)
669 was_busy = true;
670 else
671 master->busy = true;
672 spin_unlock_irqrestore(&master->queue_lock, flags);
673
Mark Brown49834de2013-07-28 14:47:02 +0100674 if (!was_busy && master->auto_runtime_pm) {
675 ret = pm_runtime_get_sync(master->dev.parent);
676 if (ret < 0) {
677 dev_err(&master->dev, "Failed to power device: %d\n",
678 ret);
679 return;
680 }
681 }
682
Mark Brown56ec1972013-10-07 19:33:53 +0100683 if (!was_busy)
684 trace_spi_master_busy(master);
685
Shubhrajyoti D7dfd2bd2012-05-10 19:20:41 +0530686 if (!was_busy && master->prepare_transfer_hardware) {
Linus Walleijffbbdd212012-02-22 10:05:38 +0100687 ret = master->prepare_transfer_hardware(master);
688 if (ret) {
689 dev_err(&master->dev,
690 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +0100691
692 if (master->auto_runtime_pm)
693 pm_runtime_put(master->dev.parent);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100694 return;
695 }
696 }
697
Mark Brown56ec1972013-10-07 19:33:53 +0100698 trace_spi_message_start(master->cur_msg);
699
Mark Brown2841a5f2013-10-05 00:23:12 +0100700 if (master->prepare_message) {
701 ret = master->prepare_message(master, master->cur_msg);
702 if (ret) {
703 dev_err(&master->dev,
704 "failed to prepare message: %d\n", ret);
705 master->cur_msg->status = ret;
706 spi_finalize_current_message(master);
707 return;
708 }
709 master->cur_msg_prepared = true;
710 }
711
Linus Walleijffbbdd212012-02-22 10:05:38 +0100712 ret = master->transfer_one_message(master, master->cur_msg);
713 if (ret) {
714 dev_err(&master->dev,
715 "failed to transfer one message from queue\n");
716 return;
717 }
718}
719
720static int spi_init_queue(struct spi_master *master)
721{
722 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
723
724 INIT_LIST_HEAD(&master->queue);
725 spin_lock_init(&master->queue_lock);
726
727 master->running = false;
728 master->busy = false;
729
730 init_kthread_worker(&master->kworker);
731 master->kworker_task = kthread_run(kthread_worker_fn,
Kees Cookf1701682013-07-03 15:04:58 -0700732 &master->kworker, "%s",
Linus Walleijffbbdd212012-02-22 10:05:38 +0100733 dev_name(&master->dev));
734 if (IS_ERR(master->kworker_task)) {
735 dev_err(&master->dev, "failed to create message pump task\n");
736 return -ENOMEM;
737 }
738 init_kthread_work(&master->pump_messages, spi_pump_messages);
739
740 /*
741 * Master config will indicate if this controller should run the
742 * message pump with high (realtime) priority to reduce the transfer
743 * latency on the bus by minimising the delay between a transfer
744 * request and the scheduling of the message pump thread. Without this
745 * setting the message pump thread will remain at default priority.
746 */
747 if (master->rt) {
748 dev_info(&master->dev,
749 "will run message pump with realtime priority\n");
750 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
751 }
752
753 return 0;
754}
755
756/**
757 * spi_get_next_queued_message() - called by driver to check for queued
758 * messages
759 * @master: the master to check for queued messages
760 *
761 * If there are more messages in the queue, the next message is returned from
762 * this call.
763 */
764struct spi_message *spi_get_next_queued_message(struct spi_master *master)
765{
766 struct spi_message *next;
767 unsigned long flags;
768
769 /* get a pointer to the next message, if any */
770 spin_lock_irqsave(&master->queue_lock, flags);
771 if (list_empty(&master->queue))
772 next = NULL;
773 else
774 next = list_entry(master->queue.next,
775 struct spi_message, queue);
776 spin_unlock_irqrestore(&master->queue_lock, flags);
777
778 return next;
779}
780EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
781
782/**
783 * spi_finalize_current_message() - the current message is complete
784 * @master: the master to return the message to
785 *
786 * Called by the driver to notify the core that the message in the front of the
787 * queue is complete and can be removed from the queue.
788 */
789void spi_finalize_current_message(struct spi_master *master)
790{
791 struct spi_message *mesg;
792 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +0100793 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +0100794
795 spin_lock_irqsave(&master->queue_lock, flags);
796 mesg = master->cur_msg;
797 master->cur_msg = NULL;
798
799 queue_kthread_work(&master->kworker, &master->pump_messages);
800 spin_unlock_irqrestore(&master->queue_lock, flags);
801
Mark Brown2841a5f2013-10-05 00:23:12 +0100802 if (master->cur_msg_prepared && master->unprepare_message) {
803 ret = master->unprepare_message(master, mesg);
804 if (ret) {
805 dev_err(&master->dev,
806 "failed to unprepare message: %d\n", ret);
807 }
808 }
809 master->cur_msg_prepared = false;
810
Linus Walleijffbbdd212012-02-22 10:05:38 +0100811 mesg->state = NULL;
812 if (mesg->complete)
813 mesg->complete(mesg->context);
Mark Brown56ec1972013-10-07 19:33:53 +0100814
815 trace_spi_message_done(mesg);
Linus Walleijffbbdd212012-02-22 10:05:38 +0100816}
817EXPORT_SYMBOL_GPL(spi_finalize_current_message);
818
819static int spi_start_queue(struct spi_master *master)
820{
821 unsigned long flags;
822
823 spin_lock_irqsave(&master->queue_lock, flags);
824
825 if (master->running || master->busy) {
826 spin_unlock_irqrestore(&master->queue_lock, flags);
827 return -EBUSY;
828 }
829
830 master->running = true;
831 master->cur_msg = NULL;
832 spin_unlock_irqrestore(&master->queue_lock, flags);
833
834 queue_kthread_work(&master->kworker, &master->pump_messages);
835
836 return 0;
837}
838
839static int spi_stop_queue(struct spi_master *master)
840{
841 unsigned long flags;
842 unsigned limit = 500;
843 int ret = 0;
844
845 spin_lock_irqsave(&master->queue_lock, flags);
846
847 /*
848 * This is a bit lame, but is optimized for the common execution path.
849 * A wait_queue on the master->busy could be used, but then the common
850 * execution path (pump_messages) would be required to call wake_up or
851 * friends on every SPI message. Do this instead.
852 */
853 while ((!list_empty(&master->queue) || master->busy) && limit--) {
854 spin_unlock_irqrestore(&master->queue_lock, flags);
855 msleep(10);
856 spin_lock_irqsave(&master->queue_lock, flags);
857 }
858
859 if (!list_empty(&master->queue) || master->busy)
860 ret = -EBUSY;
861 else
862 master->running = false;
863
864 spin_unlock_irqrestore(&master->queue_lock, flags);
865
866 if (ret) {
867 dev_warn(&master->dev,
868 "could not stop message queue\n");
869 return ret;
870 }
871 return ret;
872}
873
874static int spi_destroy_queue(struct spi_master *master)
875{
876 int ret;
877
878 ret = spi_stop_queue(master);
879
880 /*
881 * flush_kthread_worker will block until all work is done.
882 * If the reason that stop_queue timed out is that the work will never
883 * finish, then it does no good to call flush/stop thread, so
884 * return anyway.
885 */
886 if (ret) {
887 dev_err(&master->dev, "problem destroying queue\n");
888 return ret;
889 }
890
891 flush_kthread_worker(&master->kworker);
892 kthread_stop(master->kworker_task);
893
894 return 0;
895}
896
897/**
898 * spi_queued_transfer - transfer function for queued transfers
899 * @spi: spi device which is requesting transfer
900 * @msg: spi message which is to handled is queued to driver queue
901 */
902static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
903{
904 struct spi_master *master = spi->master;
905 unsigned long flags;
906
907 spin_lock_irqsave(&master->queue_lock, flags);
908
909 if (!master->running) {
910 spin_unlock_irqrestore(&master->queue_lock, flags);
911 return -ESHUTDOWN;
912 }
913 msg->actual_length = 0;
914 msg->status = -EINPROGRESS;
915
916 list_add_tail(&msg->queue, &master->queue);
Axel Lin96b3eac2013-08-22 23:41:34 +0800917 if (!master->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +0100918 queue_kthread_work(&master->kworker, &master->pump_messages);
919
920 spin_unlock_irqrestore(&master->queue_lock, flags);
921 return 0;
922}
923
924static int spi_master_initialize_queue(struct spi_master *master)
925{
926 int ret;
927
928 master->queued = true;
929 master->transfer = spi_queued_transfer;
Mark Brownb1589352013-10-05 11:50:40 +0100930 if (!master->transfer_one_message)
931 master->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +0100932
933 /* Initialize and start queue */
934 ret = spi_init_queue(master);
935 if (ret) {
936 dev_err(&master->dev, "problem initializing queue\n");
937 goto err_init_queue;
938 }
939 ret = spi_start_queue(master);
940 if (ret) {
941 dev_err(&master->dev, "problem starting queue\n");
942 goto err_start_queue;
943 }
944
945 return 0;
946
947err_start_queue:
948err_init_queue:
949 spi_destroy_queue(master);
950 return ret;
951}
952
953/*-------------------------------------------------------------------------*/
954
Andreas Larsson7cb94362012-12-04 15:09:38 +0100955#if defined(CONFIG_OF)
Grant Likelyd57a4282012-04-07 14:16:53 -0600956/**
957 * of_register_spi_devices() - Register child devices onto the SPI bus
958 * @master: Pointer to spi_master device
959 *
960 * Registers an spi_device for each child node of master node which has a 'reg'
961 * property.
962 */
963static void of_register_spi_devices(struct spi_master *master)
964{
965 struct spi_device *spi;
966 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -0600967 int rc;
Trent Piepho89da4292013-09-27 05:37:25 -0700968 u32 value;
Grant Likelyd57a4282012-04-07 14:16:53 -0600969
970 if (!master->dev.of_node)
971 return;
972
Alexander Sverdlinf3b61592012-11-29 08:59:29 +0100973 for_each_available_child_of_node(master->dev.of_node, nc) {
Grant Likelyd57a4282012-04-07 14:16:53 -0600974 /* Alloc an spi_device */
975 spi = spi_alloc_device(master);
976 if (!spi) {
977 dev_err(&master->dev, "spi_device alloc error for %s\n",
978 nc->full_name);
979 spi_dev_put(spi);
980 continue;
981 }
982
983 /* Select device driver */
984 if (of_modalias_node(nc, spi->modalias,
985 sizeof(spi->modalias)) < 0) {
986 dev_err(&master->dev, "cannot find modalias for %s\n",
987 nc->full_name);
988 spi_dev_put(spi);
989 continue;
990 }
991
992 /* Device address */
Trent Piepho89da4292013-09-27 05:37:25 -0700993 rc = of_property_read_u32(nc, "reg", &value);
994 if (rc) {
995 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
996 nc->full_name, rc);
Grant Likelyd57a4282012-04-07 14:16:53 -0600997 spi_dev_put(spi);
998 continue;
999 }
Trent Piepho89da4292013-09-27 05:37:25 -07001000 spi->chip_select = value;
Grant Likelyd57a4282012-04-07 14:16:53 -06001001
1002 /* Mode (clock phase/polarity/etc.) */
1003 if (of_find_property(nc, "spi-cpha", NULL))
1004 spi->mode |= SPI_CPHA;
1005 if (of_find_property(nc, "spi-cpol", NULL))
1006 spi->mode |= SPI_CPOL;
1007 if (of_find_property(nc, "spi-cs-high", NULL))
1008 spi->mode |= SPI_CS_HIGH;
Lars-Peter Clausenc20151d2012-12-06 16:55:33 +01001009 if (of_find_property(nc, "spi-3wire", NULL))
1010 spi->mode |= SPI_3WIRE;
Grant Likelyd57a4282012-04-07 14:16:53 -06001011
wangyuhangf477b7f2013-08-11 18:15:17 +08001012 /* Device DUAL/QUAD mode */
Trent Piepho89da4292013-09-27 05:37:25 -07001013 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1014 switch (value) {
1015 case 1:
Mark Browna822e992013-08-30 23:19:40 +01001016 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001017 case 2:
Mark Browna822e992013-08-30 23:19:40 +01001018 spi->mode |= SPI_TX_DUAL;
1019 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001020 case 4:
Mark Browna822e992013-08-30 23:19:40 +01001021 spi->mode |= SPI_TX_QUAD;
1022 break;
1023 default:
1024 dev_err(&master->dev,
wangyuhanga110f932013-09-01 17:36:21 +08001025 "spi-tx-bus-width %d not supported\n",
Trent Piepho89da4292013-09-27 05:37:25 -07001026 value);
Mark Browna822e992013-08-30 23:19:40 +01001027 spi_dev_put(spi);
1028 continue;
1029 }
wangyuhangf477b7f2013-08-11 18:15:17 +08001030 }
1031
Trent Piepho89da4292013-09-27 05:37:25 -07001032 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1033 switch (value) {
1034 case 1:
Mark Browna822e992013-08-30 23:19:40 +01001035 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001036 case 2:
Mark Browna822e992013-08-30 23:19:40 +01001037 spi->mode |= SPI_RX_DUAL;
1038 break;
Trent Piepho89da4292013-09-27 05:37:25 -07001039 case 4:
Mark Browna822e992013-08-30 23:19:40 +01001040 spi->mode |= SPI_RX_QUAD;
1041 break;
1042 default:
1043 dev_err(&master->dev,
wangyuhanga110f932013-09-01 17:36:21 +08001044 "spi-rx-bus-width %d not supported\n",
Trent Piepho89da4292013-09-27 05:37:25 -07001045 value);
Mark Browna822e992013-08-30 23:19:40 +01001046 spi_dev_put(spi);
1047 continue;
1048 }
wangyuhangf477b7f2013-08-11 18:15:17 +08001049 }
1050
Grant Likelyd57a4282012-04-07 14:16:53 -06001051 /* Device speed */
Trent Piepho89da4292013-09-27 05:37:25 -07001052 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1053 if (rc) {
1054 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1055 nc->full_name, rc);
Grant Likelyd57a4282012-04-07 14:16:53 -06001056 spi_dev_put(spi);
1057 continue;
1058 }
Trent Piepho89da4292013-09-27 05:37:25 -07001059 spi->max_speed_hz = value;
Grant Likelyd57a4282012-04-07 14:16:53 -06001060
1061 /* IRQ */
1062 spi->irq = irq_of_parse_and_map(nc, 0);
1063
1064 /* Store a pointer to the node in the device structure */
1065 of_node_get(nc);
1066 spi->dev.of_node = nc;
1067
1068 /* Register the new device */
Mathias Krause70fac172013-08-31 20:24:14 +02001069 request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
Grant Likelyd57a4282012-04-07 14:16:53 -06001070 rc = spi_add_device(spi);
1071 if (rc) {
1072 dev_err(&master->dev, "spi_device register error %s\n",
1073 nc->full_name);
1074 spi_dev_put(spi);
1075 }
1076
1077 }
1078}
1079#else
1080static void of_register_spi_devices(struct spi_master *master) { }
1081#endif
1082
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001083#ifdef CONFIG_ACPI
1084static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1085{
1086 struct spi_device *spi = data;
1087
1088 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1089 struct acpi_resource_spi_serialbus *sb;
1090
1091 sb = &ares->data.spi_serial_bus;
1092 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
1093 spi->chip_select = sb->device_selection;
1094 spi->max_speed_hz = sb->connection_speed;
1095
1096 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1097 spi->mode |= SPI_CPHA;
1098 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1099 spi->mode |= SPI_CPOL;
1100 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1101 spi->mode |= SPI_CS_HIGH;
1102 }
1103 } else if (spi->irq < 0) {
1104 struct resource r;
1105
1106 if (acpi_dev_resource_interrupt(ares, 0, &r))
1107 spi->irq = r.start;
1108 }
1109
1110 /* Always tell the ACPI core to skip this resource */
1111 return 1;
1112}
1113
1114static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1115 void *data, void **return_value)
1116{
1117 struct spi_master *master = data;
1118 struct list_head resource_list;
1119 struct acpi_device *adev;
1120 struct spi_device *spi;
1121 int ret;
1122
1123 if (acpi_bus_get_device(handle, &adev))
1124 return AE_OK;
1125 if (acpi_bus_get_status(adev) || !adev->status.present)
1126 return AE_OK;
1127
1128 spi = spi_alloc_device(master);
1129 if (!spi) {
1130 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1131 dev_name(&adev->dev));
1132 return AE_NO_MEMORY;
1133 }
1134
1135 ACPI_HANDLE_SET(&spi->dev, handle);
1136 spi->irq = -1;
1137
1138 INIT_LIST_HEAD(&resource_list);
1139 ret = acpi_dev_get_resources(adev, &resource_list,
1140 acpi_spi_add_resource, spi);
1141 acpi_dev_free_resource_list(&resource_list);
1142
1143 if (ret < 0 || !spi->max_speed_hz) {
1144 spi_dev_put(spi);
1145 return AE_OK;
1146 }
1147
Jarkko Nikulacf9eb392013-10-10 17:19:17 +03001148 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001149 if (spi_add_device(spi)) {
1150 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1151 dev_name(&adev->dev));
1152 spi_dev_put(spi);
1153 }
1154
1155 return AE_OK;
1156}
1157
1158static void acpi_register_spi_devices(struct spi_master *master)
1159{
1160 acpi_status status;
1161 acpi_handle handle;
1162
Rafael J. Wysocki29896172013-04-01 00:21:08 +00001163 handle = ACPI_HANDLE(master->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001164 if (!handle)
1165 return;
1166
1167 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1168 acpi_spi_add_device, NULL,
1169 master, NULL);
1170 if (ACPI_FAILURE(status))
1171 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1172}
1173#else
1174static inline void acpi_register_spi_devices(struct spi_master *master) {}
1175#endif /* CONFIG_ACPI */
1176
Tony Jones49dce682007-10-16 01:27:48 -07001177static void spi_master_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001178{
1179 struct spi_master *master;
1180
Tony Jones49dce682007-10-16 01:27:48 -07001181 master = container_of(dev, struct spi_master, dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001182 kfree(master);
1183}
1184
1185static struct class spi_master_class = {
1186 .name = "spi_master",
1187 .owner = THIS_MODULE,
Tony Jones49dce682007-10-16 01:27:48 -07001188 .dev_release = spi_master_release,
David Brownell8ae12a02006-01-08 13:34:19 -08001189};
1190
1191
Linus Walleijffbbdd212012-02-22 10:05:38 +01001192
David Brownell8ae12a02006-01-08 13:34:19 -08001193/**
1194 * spi_alloc_master - allocate SPI master controller
1195 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001196 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001197 * memory is in the driver_data field of the returned device,
David Brownell0c868462006-01-08 13:34:25 -08001198 * accessible with spi_master_get_devdata().
David Brownell33e34dc2007-05-08 00:32:21 -07001199 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001200 *
1201 * This call is used only by SPI master controller drivers, which are the
1202 * only ones directly touching chip registers. It's how they allocate
dmitry pervushinba1a0512006-05-20 15:00:14 -07001203 * an spi_master structure, prior to calling spi_register_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001204 *
1205 * This must be called from context that can sleep. It returns the SPI
1206 * master structure on success, else NULL.
1207 *
1208 * The caller is responsible for assigning the bus number and initializing
dmitry pervushinba1a0512006-05-20 15:00:14 -07001209 * the master's methods before calling spi_register_master(); and (after errors
Uwe Kleine-Königeb4af0f2012-02-23 10:40:14 +01001210 * adding the device) calling spi_master_put() and kfree() to prevent a memory
1211 * leak.
David Brownell8ae12a02006-01-08 13:34:19 -08001212 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001213struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
David Brownell8ae12a02006-01-08 13:34:19 -08001214{
1215 struct spi_master *master;
1216
David Brownell0c868462006-01-08 13:34:25 -08001217 if (!dev)
1218 return NULL;
1219
Jingoo Han5fe5f052013-10-14 10:31:51 +09001220 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -08001221 if (!master)
1222 return NULL;
1223
Tony Jones49dce682007-10-16 01:27:48 -07001224 device_initialize(&master->dev);
Grant Likely1e8a52e2012-05-19 23:42:08 -06001225 master->bus_num = -1;
1226 master->num_chipselect = 1;
Tony Jones49dce682007-10-16 01:27:48 -07001227 master->dev.class = &spi_master_class;
1228 master->dev.parent = get_device(dev);
David Brownell0c868462006-01-08 13:34:25 -08001229 spi_master_set_devdata(master, &master[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001230
1231 return master;
1232}
1233EXPORT_SYMBOL_GPL(spi_alloc_master);
1234
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001235#ifdef CONFIG_OF
1236static int of_spi_register_master(struct spi_master *master)
1237{
Grant Likelye80beb22013-02-12 17:48:37 +00001238 int nb, i, *cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001239 struct device_node *np = master->dev.of_node;
1240
1241 if (!np)
1242 return 0;
1243
1244 nb = of_gpio_named_count(np, "cs-gpios");
Jingoo Han5fe5f052013-10-14 10:31:51 +09001245 master->num_chipselect = max_t(int, nb, master->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001246
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001247 /* Return error only for an incorrectly formed cs-gpios property */
1248 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001249 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001250 else if (nb < 0)
1251 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001252
1253 cs = devm_kzalloc(&master->dev,
1254 sizeof(int) * master->num_chipselect,
1255 GFP_KERNEL);
1256 master->cs_gpios = cs;
1257
1258 if (!master->cs_gpios)
1259 return -ENOMEM;
1260
Andreas Larsson0da83bb2013-01-29 15:53:40 +01001261 for (i = 0; i < master->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01001262 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001263
1264 for (i = 0; i < nb; i++)
1265 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1266
1267 return 0;
1268}
1269#else
1270static int of_spi_register_master(struct spi_master *master)
1271{
1272 return 0;
1273}
1274#endif
1275
David Brownell8ae12a02006-01-08 13:34:19 -08001276/**
1277 * spi_register_master - register SPI master controller
1278 * @master: initialized master, originally from spi_alloc_master()
David Brownell33e34dc2007-05-08 00:32:21 -07001279 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001280 *
1281 * SPI master controllers connect to their drivers using some non-SPI bus,
1282 * such as the platform bus. The final stage of probe() in that code
1283 * includes calling spi_register_master() to hook up to this SPI bus glue.
1284 *
1285 * SPI controllers use board specific (often SOC specific) bus numbers,
1286 * and board-specific addressing for SPI devices combines those numbers
1287 * with chip select numbers. Since SPI does not directly support dynamic
1288 * device identification, boards need configuration tables telling which
1289 * chip is at which address.
1290 *
1291 * This must be called from context that can sleep. It returns zero on
1292 * success, else a negative error code (dropping the master's refcount).
David Brownell0c868462006-01-08 13:34:25 -08001293 * After a successful return, the caller is responsible for calling
1294 * spi_unregister_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001295 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001296int spi_register_master(struct spi_master *master)
David Brownell8ae12a02006-01-08 13:34:19 -08001297{
David Brownelle44a45a2007-06-03 13:50:40 -07001298 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Tony Jones49dce682007-10-16 01:27:48 -07001299 struct device *dev = master->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08001300 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08001301 int status = -ENODEV;
1302 int dynamic = 0;
1303
David Brownell0c868462006-01-08 13:34:25 -08001304 if (!dev)
1305 return -ENODEV;
1306
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001307 status = of_spi_register_master(master);
1308 if (status)
1309 return status;
1310
David Brownell082c8cb2007-07-31 00:39:45 -07001311 /* even if it's just one always-selected device, there must
1312 * be at least one chipselect
1313 */
1314 if (master->num_chipselect == 0)
1315 return -EINVAL;
1316
Grant Likelybb297852012-12-21 19:32:09 +00001317 if ((master->bus_num < 0) && master->dev.of_node)
1318 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1319
David Brownell8ae12a02006-01-08 13:34:19 -08001320 /* convention: dynamically assigned bus IDs count down from the max */
David Brownella020ed72006-04-03 15:49:04 -07001321 if (master->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07001322 /* FIXME switch to an IDR based scheme, something like
1323 * I2C now uses, so we can't run out of "dynamic" IDs
1324 */
David Brownell8ae12a02006-01-08 13:34:19 -08001325 master->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08001326 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08001327 }
1328
Ernst Schwabcf32b712010-06-28 17:49:29 -07001329 spin_lock_init(&master->bus_lock_spinlock);
1330 mutex_init(&master->bus_lock_mutex);
1331 master->bus_lock_flag = 0;
Mark Brownb1589352013-10-05 11:50:40 +01001332 init_completion(&master->xfer_completion);
Ernst Schwabcf32b712010-06-28 17:49:29 -07001333
David Brownell8ae12a02006-01-08 13:34:19 -08001334 /* register the device, then userspace will see it.
1335 * registration fails if the bus ID is in use.
1336 */
Kay Sievers35f74fc2009-01-06 10:44:37 -08001337 dev_set_name(&master->dev, "spi%u", master->bus_num);
Tony Jones49dce682007-10-16 01:27:48 -07001338 status = device_add(&master->dev);
David Brownellb8852442006-01-08 13:34:23 -08001339 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08001340 goto done;
Kay Sievers35f74fc2009-01-06 10:44:37 -08001341 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
David Brownell8ae12a02006-01-08 13:34:19 -08001342 dynamic ? " (dynamic)" : "");
1343
Linus Walleijffbbdd212012-02-22 10:05:38 +01001344 /* If we're using a queued driver, start the queue */
1345 if (master->transfer)
1346 dev_info(dev, "master is unqueued, this is deprecated\n");
1347 else {
1348 status = spi_master_initialize_queue(master);
1349 if (status) {
Axel Line93b0722013-08-31 20:25:52 +08001350 device_del(&master->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001351 goto done;
1352 }
1353 }
1354
Feng Tang2b9603a2010-08-02 15:52:15 +08001355 mutex_lock(&board_lock);
1356 list_add_tail(&master->list, &spi_master_list);
1357 list_for_each_entry(bi, &board_list, list)
1358 spi_match_master_to_boardinfo(master, &bi->board_info);
1359 mutex_unlock(&board_lock);
1360
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001361 /* Register devices from the device tree and ACPI */
Anatolij Gustschin12b15e82010-07-27 22:35:58 +02001362 of_register_spi_devices(master);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001363 acpi_register_spi_devices(master);
David Brownell8ae12a02006-01-08 13:34:19 -08001364done:
1365 return status;
1366}
1367EXPORT_SYMBOL_GPL(spi_register_master);
1368
Mark Brown666d5b42013-08-31 18:50:52 +01001369static void devm_spi_unregister(struct device *dev, void *res)
1370{
1371 spi_unregister_master(*(struct spi_master **)res);
1372}
1373
1374/**
1375 * dev_spi_register_master - register managed SPI master controller
1376 * @dev: device managing SPI master
1377 * @master: initialized master, originally from spi_alloc_master()
1378 * Context: can sleep
1379 *
1380 * Register a SPI device as with spi_register_master() which will
1381 * automatically be unregister
1382 */
1383int devm_spi_register_master(struct device *dev, struct spi_master *master)
1384{
1385 struct spi_master **ptr;
1386 int ret;
1387
1388 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1389 if (!ptr)
1390 return -ENOMEM;
1391
1392 ret = spi_register_master(master);
Stephen Warren4b928942013-11-21 16:11:15 -07001393 if (!ret) {
Mark Brown666d5b42013-08-31 18:50:52 +01001394 *ptr = master;
1395 devres_add(dev, ptr);
1396 } else {
1397 devres_free(ptr);
1398 }
1399
1400 return ret;
1401}
1402EXPORT_SYMBOL_GPL(devm_spi_register_master);
1403
David Lamparter34860082010-08-30 23:54:17 +02001404static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08001405{
David Lamparter34860082010-08-30 23:54:17 +02001406 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08001407 return 0;
1408}
1409
1410/**
1411 * spi_unregister_master - unregister SPI master controller
1412 * @master: the master being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07001413 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001414 *
1415 * This call is used only by SPI master controller drivers, which are the
1416 * only ones directly touching chip registers.
1417 *
1418 * This must be called from context that can sleep.
1419 */
1420void spi_unregister_master(struct spi_master *master)
1421{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08001422 int dummy;
1423
Linus Walleijffbbdd212012-02-22 10:05:38 +01001424 if (master->queued) {
1425 if (spi_destroy_queue(master))
1426 dev_err(&master->dev, "queue remove failed\n");
1427 }
1428
Feng Tang2b9603a2010-08-02 15:52:15 +08001429 mutex_lock(&board_lock);
1430 list_del(&master->list);
1431 mutex_unlock(&board_lock);
1432
Sebastian Andrzej Siewior97dbf372010-12-21 17:24:31 -08001433 dummy = device_for_each_child(&master->dev, NULL, __unregister);
Tony Jones49dce682007-10-16 01:27:48 -07001434 device_unregister(&master->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001435}
1436EXPORT_SYMBOL_GPL(spi_unregister_master);
1437
Linus Walleijffbbdd212012-02-22 10:05:38 +01001438int spi_master_suspend(struct spi_master *master)
1439{
1440 int ret;
1441
1442 /* Basically no-ops for non-queued masters */
1443 if (!master->queued)
1444 return 0;
1445
1446 ret = spi_stop_queue(master);
1447 if (ret)
1448 dev_err(&master->dev, "queue stop failed\n");
1449
1450 return ret;
1451}
1452EXPORT_SYMBOL_GPL(spi_master_suspend);
1453
1454int spi_master_resume(struct spi_master *master)
1455{
1456 int ret;
1457
1458 if (!master->queued)
1459 return 0;
1460
1461 ret = spi_start_queue(master);
1462 if (ret)
1463 dev_err(&master->dev, "queue restart failed\n");
1464
1465 return ret;
1466}
1467EXPORT_SYMBOL_GPL(spi_master_resume);
1468
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001469static int __spi_master_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08001470{
1471 struct spi_master *m;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01001472 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08001473
1474 m = container_of(dev, struct spi_master, dev);
1475 return m->bus_num == *bus_num;
1476}
1477
David Brownell8ae12a02006-01-08 13:34:19 -08001478/**
1479 * spi_busnum_to_master - look up master associated with bus_num
1480 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07001481 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001482 *
1483 * This call may be used with devices that are registered after
1484 * arch init time. It returns a refcounted pointer to the relevant
1485 * spi_master (which the caller must release), or NULL if there is
1486 * no such master registered.
1487 */
1488struct spi_master *spi_busnum_to_master(u16 bus_num)
1489{
Tony Jones49dce682007-10-16 01:27:48 -07001490 struct device *dev;
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001491 struct spi_master *master = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08001492
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04001493 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Dave Young5ed2c832008-01-22 15:14:18 +08001494 __spi_master_match);
1495 if (dev)
1496 master = container_of(dev, struct spi_master, dev);
1497 /* reference got in class_find_device */
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08001498 return master;
David Brownell8ae12a02006-01-08 13:34:19 -08001499}
1500EXPORT_SYMBOL_GPL(spi_busnum_to_master);
1501
1502
1503/*-------------------------------------------------------------------------*/
1504
David Brownell7d077192009-06-17 16:26:03 -07001505/* Core methods for SPI master protocol drivers. Some of the
1506 * other core methods are currently defined as inline functions.
1507 */
1508
1509/**
1510 * spi_setup - setup SPI mode and clock rate
1511 * @spi: the device whose settings are being modified
1512 * Context: can sleep, and no requests are queued to the device
1513 *
1514 * SPI protocol drivers may need to update the transfer mode if the
1515 * device doesn't work with its default. They may likewise need
1516 * to update clock rates or word sizes from initial values. This function
1517 * changes those settings, and must be called from a context that can sleep.
1518 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
1519 * effect the next time the device is selected and data is transferred to
1520 * or from it. When this function returns, the spi device is deselected.
1521 *
1522 * Note that this call will fail if the protocol driver specifies an option
1523 * that the underlying controller or its driver does not support. For
1524 * example, not all hardware supports wire transfers using nine bit words,
1525 * LSB-first wire encoding, or active-high chipselects.
1526 */
1527int spi_setup(struct spi_device *spi)
1528{
David Brownelle7db06b2009-06-17 16:26:04 -07001529 unsigned bad_bits;
Laxman Dewangancaae0702012-11-09 14:35:22 +05301530 int status = 0;
David Brownell7d077192009-06-17 16:26:03 -07001531
wangyuhangf477b7f2013-08-11 18:15:17 +08001532 /* check mode to prevent that DUAL and QUAD set at the same time
1533 */
1534 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
1535 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
1536 dev_err(&spi->dev,
1537 "setup: can not select dual and quad at the same time\n");
1538 return -EINVAL;
1539 }
1540 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
1541 */
1542 if ((spi->mode & SPI_3WIRE) && (spi->mode &
1543 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
1544 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07001545 /* help drivers fail *cleanly* when they need options
1546 * that aren't supported with their current master
1547 */
1548 bad_bits = spi->mode & ~spi->master->mode_bits;
1549 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02001550 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07001551 bad_bits);
1552 return -EINVAL;
1553 }
1554
David Brownell7d077192009-06-17 16:26:03 -07001555 if (!spi->bits_per_word)
1556 spi->bits_per_word = 8;
1557
Laxman Dewangancaae0702012-11-09 14:35:22 +05301558 if (spi->master->setup)
1559 status = spi->master->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07001560
Jingoo Han5fe5f052013-10-14 10:31:51 +09001561 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 -07001562 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
1563 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
1564 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
1565 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
1566 (spi->mode & SPI_LOOP) ? "loopback, " : "",
1567 spi->bits_per_word, spi->max_speed_hz,
1568 status);
1569
1570 return status;
1571}
1572EXPORT_SYMBOL_GPL(spi_setup);
1573
Ernst Schwabcf32b712010-06-28 17:49:29 -07001574static int __spi_async(struct spi_device *spi, struct spi_message *message)
1575{
1576 struct spi_master *master = spi->master;
Laxman Dewangane6811d12012-11-09 14:36:45 +05301577 struct spi_transfer *xfer;
Ernst Schwabcf32b712010-06-28 17:49:29 -07001578
Mark Brown56ec1972013-10-07 19:33:53 +01001579 message->spi = spi;
1580
1581 trace_spi_message_submit(message);
1582
Mark Brown24a00132013-07-10 15:05:40 +01001583 if (list_empty(&message->transfers))
1584 return -EINVAL;
1585 if (!message->complete)
1586 return -EINVAL;
1587
Ernst Schwabcf32b712010-06-28 17:49:29 -07001588 /* Half-duplex links include original MicroWire, and ones with
1589 * only one data pin like SPI_3WIRE (switches direction) or where
1590 * either MOSI or MISO is missing. They can also be caused by
1591 * software limitations.
1592 */
1593 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
1594 || (spi->mode & SPI_3WIRE)) {
Ernst Schwabcf32b712010-06-28 17:49:29 -07001595 unsigned flags = master->flags;
1596
1597 list_for_each_entry(xfer, &message->transfers, transfer_list) {
1598 if (xfer->rx_buf && xfer->tx_buf)
1599 return -EINVAL;
1600 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
1601 return -EINVAL;
1602 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
1603 return -EINVAL;
1604 }
1605 }
1606
Laxman Dewangane6811d12012-11-09 14:36:45 +05301607 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05301608 * Set transfer bits_per_word and max speed as spi device default if
1609 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08001610 * Set transfer tx_nbits and rx_nbits as single transfer default
1611 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05301612 */
1613 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05301614 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05301615 if (!xfer->bits_per_word)
1616 xfer->bits_per_word = spi->bits_per_word;
Gabor Juhos56ede942013-08-14 10:25:28 +02001617 if (!xfer->speed_hz) {
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05301618 xfer->speed_hz = spi->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02001619 if (master->max_speed_hz &&
1620 xfer->speed_hz > master->max_speed_hz)
1621 xfer->speed_hz = master->max_speed_hz;
1622 }
1623
Stephen Warren543bb252013-03-26 20:37:57 -06001624 if (master->bits_per_word_mask) {
1625 /* Only 32 bits fit in the mask */
1626 if (xfer->bits_per_word > 32)
1627 return -EINVAL;
1628 if (!(master->bits_per_word_mask &
1629 BIT(xfer->bits_per_word - 1)))
1630 return -EINVAL;
1631 }
Mark Browna2fd4f92013-07-10 14:57:26 +01001632
1633 if (xfer->speed_hz && master->min_speed_hz &&
1634 xfer->speed_hz < master->min_speed_hz)
1635 return -EINVAL;
1636 if (xfer->speed_hz && master->max_speed_hz &&
1637 xfer->speed_hz > master->max_speed_hz)
wangyuhangd5ee7222013-08-30 18:05:10 +08001638 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08001639
1640 if (xfer->tx_buf && !xfer->tx_nbits)
1641 xfer->tx_nbits = SPI_NBITS_SINGLE;
1642 if (xfer->rx_buf && !xfer->rx_nbits)
1643 xfer->rx_nbits = SPI_NBITS_SINGLE;
1644 /* check transfer tx/rx_nbits:
1645 * 1. keep the value is not out of single, dual and quad
1646 * 2. keep tx/rx_nbits is contained by mode in spi_device
1647 * 3. if SPI_3WIRE, tx/rx_nbits should be in single
1648 */
Sourav Poddardb90a442013-08-22 21:20:48 +05301649 if (xfer->tx_buf) {
1650 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
1651 xfer->tx_nbits != SPI_NBITS_DUAL &&
1652 xfer->tx_nbits != SPI_NBITS_QUAD)
1653 return -EINVAL;
1654 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
1655 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
1656 return -EINVAL;
1657 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
1658 !(spi->mode & SPI_TX_QUAD))
1659 return -EINVAL;
1660 if ((spi->mode & SPI_3WIRE) &&
1661 (xfer->tx_nbits != SPI_NBITS_SINGLE))
1662 return -EINVAL;
1663 }
wangyuhangf477b7f2013-08-11 18:15:17 +08001664 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05301665 if (xfer->rx_buf) {
1666 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
1667 xfer->rx_nbits != SPI_NBITS_DUAL &&
1668 xfer->rx_nbits != SPI_NBITS_QUAD)
1669 return -EINVAL;
1670 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
1671 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
1672 return -EINVAL;
1673 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
1674 !(spi->mode & SPI_RX_QUAD))
1675 return -EINVAL;
1676 if ((spi->mode & SPI_3WIRE) &&
1677 (xfer->rx_nbits != SPI_NBITS_SINGLE))
1678 return -EINVAL;
1679 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05301680 }
1681
Ernst Schwabcf32b712010-06-28 17:49:29 -07001682 message->status = -EINPROGRESS;
1683 return master->transfer(spi, message);
1684}
1685
David Brownell568d0692009-09-22 16:46:18 -07001686/**
1687 * spi_async - asynchronous SPI transfer
1688 * @spi: device with which data will be exchanged
1689 * @message: describes the data transfers, including completion callback
1690 * Context: any (irqs may be blocked, etc)
1691 *
1692 * This call may be used in_irq and other contexts which can't sleep,
1693 * as well as from task contexts which can sleep.
1694 *
1695 * The completion callback is invoked in a context which can't sleep.
1696 * Before that invocation, the value of message->status is undefined.
1697 * When the callback is issued, message->status holds either zero (to
1698 * indicate complete success) or a negative error code. After that
1699 * callback returns, the driver which issued the transfer request may
1700 * deallocate the associated memory; it's no longer in use by any SPI
1701 * core or controller driver code.
1702 *
1703 * Note that although all messages to a spi_device are handled in
1704 * FIFO order, messages may go to different devices in other orders.
1705 * Some device might be higher priority, or have various "hard" access
1706 * time requirements, for example.
1707 *
1708 * On detection of any fault during the transfer, processing of
1709 * the entire message is aborted, and the device is deselected.
1710 * Until returning from the associated message completion callback,
1711 * no other spi_message queued to that device will be processed.
1712 * (This rule applies equally to all the synchronous transfer calls,
1713 * which are wrappers around this core asynchronous primitive.)
1714 */
1715int spi_async(struct spi_device *spi, struct spi_message *message)
1716{
1717 struct spi_master *master = spi->master;
Ernst Schwabcf32b712010-06-28 17:49:29 -07001718 int ret;
1719 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07001720
Ernst Schwabcf32b712010-06-28 17:49:29 -07001721 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07001722
Ernst Schwabcf32b712010-06-28 17:49:29 -07001723 if (master->bus_lock_flag)
1724 ret = -EBUSY;
1725 else
1726 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07001727
Ernst Schwabcf32b712010-06-28 17:49:29 -07001728 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1729
1730 return ret;
David Brownell568d0692009-09-22 16:46:18 -07001731}
1732EXPORT_SYMBOL_GPL(spi_async);
1733
Ernst Schwabcf32b712010-06-28 17:49:29 -07001734/**
1735 * spi_async_locked - version of spi_async with exclusive bus usage
1736 * @spi: device with which data will be exchanged
1737 * @message: describes the data transfers, including completion callback
1738 * Context: any (irqs may be blocked, etc)
1739 *
1740 * This call may be used in_irq and other contexts which can't sleep,
1741 * as well as from task contexts which can sleep.
1742 *
1743 * The completion callback is invoked in a context which can't sleep.
1744 * Before that invocation, the value of message->status is undefined.
1745 * When the callback is issued, message->status holds either zero (to
1746 * indicate complete success) or a negative error code. After that
1747 * callback returns, the driver which issued the transfer request may
1748 * deallocate the associated memory; it's no longer in use by any SPI
1749 * core or controller driver code.
1750 *
1751 * Note that although all messages to a spi_device are handled in
1752 * FIFO order, messages may go to different devices in other orders.
1753 * Some device might be higher priority, or have various "hard" access
1754 * time requirements, for example.
1755 *
1756 * On detection of any fault during the transfer, processing of
1757 * the entire message is aborted, and the device is deselected.
1758 * Until returning from the associated message completion callback,
1759 * no other spi_message queued to that device will be processed.
1760 * (This rule applies equally to all the synchronous transfer calls,
1761 * which are wrappers around this core asynchronous primitive.)
1762 */
1763int spi_async_locked(struct spi_device *spi, struct spi_message *message)
1764{
1765 struct spi_master *master = spi->master;
1766 int ret;
1767 unsigned long flags;
1768
1769 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1770
1771 ret = __spi_async(spi, message);
1772
1773 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1774
1775 return ret;
1776
1777}
1778EXPORT_SYMBOL_GPL(spi_async_locked);
1779
David Brownell7d077192009-06-17 16:26:03 -07001780
1781/*-------------------------------------------------------------------------*/
1782
1783/* Utility methods for SPI master protocol drivers, layered on
1784 * top of the core. Some other utility methods are defined as
1785 * inline functions.
1786 */
1787
Andrew Morton5d870c82006-01-11 11:23:49 -08001788static void spi_complete(void *arg)
1789{
1790 complete(arg);
1791}
1792
Ernst Schwabcf32b712010-06-28 17:49:29 -07001793static int __spi_sync(struct spi_device *spi, struct spi_message *message,
1794 int bus_locked)
1795{
1796 DECLARE_COMPLETION_ONSTACK(done);
1797 int status;
1798 struct spi_master *master = spi->master;
1799
1800 message->complete = spi_complete;
1801 message->context = &done;
1802
1803 if (!bus_locked)
1804 mutex_lock(&master->bus_lock_mutex);
1805
1806 status = spi_async_locked(spi, message);
1807
1808 if (!bus_locked)
1809 mutex_unlock(&master->bus_lock_mutex);
1810
1811 if (status == 0) {
1812 wait_for_completion(&done);
1813 status = message->status;
1814 }
1815 message->context = NULL;
1816 return status;
1817}
1818
David Brownell8ae12a02006-01-08 13:34:19 -08001819/**
1820 * spi_sync - blocking/synchronous SPI data transfers
1821 * @spi: device with which data will be exchanged
1822 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07001823 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001824 *
1825 * This call may only be used from a context that may sleep. The sleep
1826 * is non-interruptible, and has no timeout. Low-overhead controller
1827 * drivers may DMA directly into and out of the message buffers.
1828 *
1829 * Note that the SPI device's chip select is active during the message,
1830 * and then is normally disabled between messages. Drivers for some
1831 * frequently-used devices may want to minimize costs of selecting a chip,
1832 * by leaving it selected in anticipation that the next message will go
1833 * to the same chip. (That may increase power usage.)
1834 *
David Brownell0c868462006-01-08 13:34:25 -08001835 * Also, the caller is guaranteeing that the memory associated with the
1836 * message will not be freed before this call returns.
1837 *
Marc Pignat9b938b72007-12-04 23:45:10 -08001838 * It returns zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08001839 */
1840int spi_sync(struct spi_device *spi, struct spi_message *message)
1841{
Ernst Schwabcf32b712010-06-28 17:49:29 -07001842 return __spi_sync(spi, message, 0);
David Brownell8ae12a02006-01-08 13:34:19 -08001843}
1844EXPORT_SYMBOL_GPL(spi_sync);
1845
Ernst Schwabcf32b712010-06-28 17:49:29 -07001846/**
1847 * spi_sync_locked - version of spi_sync with exclusive bus usage
1848 * @spi: device with which data will be exchanged
1849 * @message: describes the data transfers
1850 * Context: can sleep
1851 *
1852 * This call may only be used from a context that may sleep. The sleep
1853 * is non-interruptible, and has no timeout. Low-overhead controller
1854 * drivers may DMA directly into and out of the message buffers.
1855 *
1856 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001857 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b712010-06-28 17:49:29 -07001858 * be released by a spi_bus_unlock call when the exclusive access is over.
1859 *
1860 * It returns zero on success, else a negative error code.
1861 */
1862int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
1863{
1864 return __spi_sync(spi, message, 1);
1865}
1866EXPORT_SYMBOL_GPL(spi_sync_locked);
1867
1868/**
1869 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
1870 * @master: SPI bus master that should be locked for exclusive bus access
1871 * Context: can sleep
1872 *
1873 * This call may only be used from a context that may sleep. The sleep
1874 * is non-interruptible, and has no timeout.
1875 *
1876 * This call should be used by drivers that require exclusive access to the
1877 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
1878 * exclusive access is over. Data transfer must be done by spi_sync_locked
1879 * and spi_async_locked calls when the SPI bus lock is held.
1880 *
1881 * It returns zero on success, else a negative error code.
1882 */
1883int spi_bus_lock(struct spi_master *master)
1884{
1885 unsigned long flags;
1886
1887 mutex_lock(&master->bus_lock_mutex);
1888
1889 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
1890 master->bus_lock_flag = 1;
1891 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
1892
1893 /* mutex remains locked until spi_bus_unlock is called */
1894
1895 return 0;
1896}
1897EXPORT_SYMBOL_GPL(spi_bus_lock);
1898
1899/**
1900 * spi_bus_unlock - release the lock for exclusive SPI bus usage
1901 * @master: SPI bus master that was locked for exclusive bus access
1902 * Context: can sleep
1903 *
1904 * This call may only be used from a context that may sleep. The sleep
1905 * is non-interruptible, and has no timeout.
1906 *
1907 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
1908 * call.
1909 *
1910 * It returns zero on success, else a negative error code.
1911 */
1912int spi_bus_unlock(struct spi_master *master)
1913{
1914 master->bus_lock_flag = 0;
1915
1916 mutex_unlock(&master->bus_lock_mutex);
1917
1918 return 0;
1919}
1920EXPORT_SYMBOL_GPL(spi_bus_unlock);
1921
David Brownella9948b62006-04-02 10:37:40 -08001922/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09001923#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08001924
1925static u8 *buf;
1926
1927/**
1928 * spi_write_then_read - SPI synchronous write followed by read
1929 * @spi: device with which data will be exchanged
1930 * @txbuf: data to be written (need not be dma-safe)
1931 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07001932 * @rxbuf: buffer into which data will be read (need not be dma-safe)
1933 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07001934 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001935 *
1936 * This performs a half duplex MicroWire style transaction with the
1937 * device, sending txbuf and then reading rxbuf. The return value
1938 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08001939 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08001940 *
David Brownell0c868462006-01-08 13:34:25 -08001941 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07001942 * portable code should never use this for more than 32 bytes.
1943 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c868462006-01-08 13:34:25 -08001944 * spi_{async,sync}() calls with dma-safe buffers.
David Brownell8ae12a02006-01-08 13:34:19 -08001945 */
1946int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02001947 const void *txbuf, unsigned n_tx,
1948 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08001949{
David Brownell068f4072007-12-04 23:45:09 -08001950 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08001951
1952 int status;
1953 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07001954 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08001955 u8 *local_buf;
1956
Mark Brownb3a223e2012-12-02 12:54:25 +09001957 /* Use preallocated DMA-safe buffer if we can. We can't avoid
1958 * copying here, (as a pure convenience thing), but we can
1959 * keep heap costs out of the hot path unless someone else is
1960 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08001961 */
Mark Brownb3a223e2012-12-02 12:54:25 +09001962 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08001963 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
1964 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09001965 if (!local_buf)
1966 return -ENOMEM;
1967 } else {
1968 local_buf = buf;
1969 }
David Brownell8ae12a02006-01-08 13:34:19 -08001970
Vitaly Wool8275c642006-01-08 13:34:28 -08001971 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09001972 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07001973 if (n_tx) {
1974 x[0].len = n_tx;
1975 spi_message_add_tail(&x[0], &message);
1976 }
1977 if (n_rx) {
1978 x[1].len = n_rx;
1979 spi_message_add_tail(&x[1], &message);
1980 }
Vitaly Wool8275c642006-01-08 13:34:28 -08001981
David Brownell8ae12a02006-01-08 13:34:19 -08001982 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07001983 x[0].tx_buf = local_buf;
1984 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08001985
1986 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08001987 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08001988 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07001989 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08001990
David Brownellbdff5492009-04-13 14:39:57 -07001991 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08001992 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08001993 else
1994 kfree(local_buf);
1995
1996 return status;
1997}
1998EXPORT_SYMBOL_GPL(spi_write_then_read);
1999
2000/*-------------------------------------------------------------------------*/
2001
2002static int __init spi_init(void)
2003{
David Brownellb8852442006-01-08 13:34:23 -08002004 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08002005
Christoph Lametere94b1762006-12-06 20:33:17 -08002006 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08002007 if (!buf) {
2008 status = -ENOMEM;
2009 goto err0;
2010 }
2011
2012 status = bus_register(&spi_bus_type);
2013 if (status < 0)
2014 goto err1;
2015
2016 status = class_register(&spi_master_class);
2017 if (status < 0)
2018 goto err2;
David Brownell8ae12a02006-01-08 13:34:19 -08002019 return 0;
David Brownellb8852442006-01-08 13:34:23 -08002020
2021err2:
2022 bus_unregister(&spi_bus_type);
2023err1:
2024 kfree(buf);
2025 buf = NULL;
2026err0:
2027 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08002028}
David Brownellb8852442006-01-08 13:34:23 -08002029
David Brownell8ae12a02006-01-08 13:34:19 -08002030/* board_info is normally registered in arch_initcall(),
2031 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08002032 *
2033 * REVISIT only boardinfo really needs static linking. the rest (device and
2034 * driver registration) _could_ be dynamically linked (modular) ... costs
2035 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08002036 */
David Brownell673c0c02008-10-15 22:02:46 -07002037postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08002038