blob: 59cb26470d7058a22fccdc41b92fb43385758967 [file] [log] [blame]
David Brownell8ae12a02006-01-08 13:34:19 -08001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * SPI init/core code
David Brownell8ae12a02006-01-08 13:34:19 -08003 *
4 * Copyright (C) 2005 David Brownell
Grant Likelyd57a4282012-04-07 14:16:53 -06005 * Copyright (C) 2008 Secret Lab Technologies Ltd.
David Brownell8ae12a02006-01-08 13:34:19 -08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
David Brownell8ae12a02006-01-08 13:34:19 -080016 */
17
David Brownell8ae12a02006-01-08 13:34:19 -080018#include <linux/kernel.h>
19#include <linux/device.h>
20#include <linux/init.h>
21#include <linux/cache.h>
Mark Brown99adef32014-01-16 12:22:43 +000022#include <linux/dma-mapping.h>
23#include <linux/dmaengine.h>
Matthias Kaehlcke94040822007-07-17 04:04:16 -070024#include <linux/mutex.h>
Sinan Akman2b7a32f2010-10-02 21:28:29 -060025#include <linux/of_device.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060026#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020027#include <linux/clk/clk-conf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Anton Vorontsove0626e32009-09-22 16:46:08 -070029#include <linux/mod_devicetable.h>
David Brownell8ae12a02006-01-08 13:34:19 -080030#include <linux/spi/spi.h>
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +010031#include <linux/of_gpio.h>
Mark Brown3ae22e82010-12-25 15:32:27 +010032#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020033#include <linux/pm_domain.h>
Paul Gortmaker025ed132011-07-10 12:57:55 -040034#include <linux/export.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060035#include <linux/sched/rt.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010036#include <linux/delay.h>
37#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010038#include <linux/ioport.h>
39#include <linux/acpi.h>
Vignesh Rb1b81532016-08-17 15:22:36 +053040#include <linux/highmem.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);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080061 int len;
62
63 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
64 if (len != -ENODEV)
65 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080066
Grant Likelyd8e328b2012-05-20 00:08:13 -060067 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080068}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070069static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080070
Martin Sperleca2ebc2015-06-22 13:00:36 +000071#define SPI_STATISTICS_ATTRS(field, file) \
72static ssize_t spi_master_##field##_show(struct device *dev, \
73 struct device_attribute *attr, \
74 char *buf) \
75{ \
76 struct spi_master *master = container_of(dev, \
77 struct spi_master, dev); \
78 return spi_statistics_##field##_show(&master->statistics, buf); \
79} \
80static struct device_attribute dev_attr_spi_master_##field = { \
81 .attr = { .name = file, .mode = S_IRUGO }, \
82 .show = spi_master_##field##_show, \
83}; \
84static ssize_t spi_device_##field##_show(struct device *dev, \
85 struct device_attribute *attr, \
86 char *buf) \
87{ \
Geliang Tangd1eba932015-12-23 00:18:41 +080088 struct spi_device *spi = to_spi_device(dev); \
Martin Sperleca2ebc2015-06-22 13:00:36 +000089 return spi_statistics_##field##_show(&spi->statistics, buf); \
90} \
91static struct device_attribute dev_attr_spi_device_##field = { \
92 .attr = { .name = file, .mode = S_IRUGO }, \
93 .show = spi_device_##field##_show, \
94}
95
96#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
97static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
98 char *buf) \
99{ \
100 unsigned long flags; \
101 ssize_t len; \
102 spin_lock_irqsave(&stat->lock, flags); \
103 len = sprintf(buf, format_string, stat->field); \
104 spin_unlock_irqrestore(&stat->lock, flags); \
105 return len; \
106} \
107SPI_STATISTICS_ATTRS(name, file)
108
109#define SPI_STATISTICS_SHOW(field, format_string) \
110 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
111 field, format_string)
112
113SPI_STATISTICS_SHOW(messages, "%lu");
114SPI_STATISTICS_SHOW(transfers, "%lu");
115SPI_STATISTICS_SHOW(errors, "%lu");
116SPI_STATISTICS_SHOW(timedout, "%lu");
117
118SPI_STATISTICS_SHOW(spi_sync, "%lu");
119SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
120SPI_STATISTICS_SHOW(spi_async, "%lu");
121
122SPI_STATISTICS_SHOW(bytes, "%llu");
123SPI_STATISTICS_SHOW(bytes_rx, "%llu");
124SPI_STATISTICS_SHOW(bytes_tx, "%llu");
125
Martin Sperl6b7bc062015-06-22 13:02:04 +0000126#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
127 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
128 "transfer_bytes_histo_" number, \
129 transfer_bytes_histo[index], "%lu")
130SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
131SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
132SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
133SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
134SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
135SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
136SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
137SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
138SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
139SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
140SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
141SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
142SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
143SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
144SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
145SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
146SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
147
Martin Sperld9f12122015-12-14 15:20:20 +0000148SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
149
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700150static struct attribute *spi_dev_attrs[] = {
151 &dev_attr_modalias.attr,
152 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800153};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000154
155static const struct attribute_group spi_dev_group = {
156 .attrs = spi_dev_attrs,
157};
158
159static struct attribute *spi_device_statistics_attrs[] = {
160 &dev_attr_spi_device_messages.attr,
161 &dev_attr_spi_device_transfers.attr,
162 &dev_attr_spi_device_errors.attr,
163 &dev_attr_spi_device_timedout.attr,
164 &dev_attr_spi_device_spi_sync.attr,
165 &dev_attr_spi_device_spi_sync_immediate.attr,
166 &dev_attr_spi_device_spi_async.attr,
167 &dev_attr_spi_device_bytes.attr,
168 &dev_attr_spi_device_bytes_rx.attr,
169 &dev_attr_spi_device_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000170 &dev_attr_spi_device_transfer_bytes_histo0.attr,
171 &dev_attr_spi_device_transfer_bytes_histo1.attr,
172 &dev_attr_spi_device_transfer_bytes_histo2.attr,
173 &dev_attr_spi_device_transfer_bytes_histo3.attr,
174 &dev_attr_spi_device_transfer_bytes_histo4.attr,
175 &dev_attr_spi_device_transfer_bytes_histo5.attr,
176 &dev_attr_spi_device_transfer_bytes_histo6.attr,
177 &dev_attr_spi_device_transfer_bytes_histo7.attr,
178 &dev_attr_spi_device_transfer_bytes_histo8.attr,
179 &dev_attr_spi_device_transfer_bytes_histo9.attr,
180 &dev_attr_spi_device_transfer_bytes_histo10.attr,
181 &dev_attr_spi_device_transfer_bytes_histo11.attr,
182 &dev_attr_spi_device_transfer_bytes_histo12.attr,
183 &dev_attr_spi_device_transfer_bytes_histo13.attr,
184 &dev_attr_spi_device_transfer_bytes_histo14.attr,
185 &dev_attr_spi_device_transfer_bytes_histo15.attr,
186 &dev_attr_spi_device_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000187 &dev_attr_spi_device_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000188 NULL,
189};
190
191static const struct attribute_group spi_device_statistics_group = {
192 .name = "statistics",
193 .attrs = spi_device_statistics_attrs,
194};
195
196static const struct attribute_group *spi_dev_groups[] = {
197 &spi_dev_group,
198 &spi_device_statistics_group,
199 NULL,
200};
201
202static struct attribute *spi_master_statistics_attrs[] = {
203 &dev_attr_spi_master_messages.attr,
204 &dev_attr_spi_master_transfers.attr,
205 &dev_attr_spi_master_errors.attr,
206 &dev_attr_spi_master_timedout.attr,
207 &dev_attr_spi_master_spi_sync.attr,
208 &dev_attr_spi_master_spi_sync_immediate.attr,
209 &dev_attr_spi_master_spi_async.attr,
210 &dev_attr_spi_master_bytes.attr,
211 &dev_attr_spi_master_bytes_rx.attr,
212 &dev_attr_spi_master_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000213 &dev_attr_spi_master_transfer_bytes_histo0.attr,
214 &dev_attr_spi_master_transfer_bytes_histo1.attr,
215 &dev_attr_spi_master_transfer_bytes_histo2.attr,
216 &dev_attr_spi_master_transfer_bytes_histo3.attr,
217 &dev_attr_spi_master_transfer_bytes_histo4.attr,
218 &dev_attr_spi_master_transfer_bytes_histo5.attr,
219 &dev_attr_spi_master_transfer_bytes_histo6.attr,
220 &dev_attr_spi_master_transfer_bytes_histo7.attr,
221 &dev_attr_spi_master_transfer_bytes_histo8.attr,
222 &dev_attr_spi_master_transfer_bytes_histo9.attr,
223 &dev_attr_spi_master_transfer_bytes_histo10.attr,
224 &dev_attr_spi_master_transfer_bytes_histo11.attr,
225 &dev_attr_spi_master_transfer_bytes_histo12.attr,
226 &dev_attr_spi_master_transfer_bytes_histo13.attr,
227 &dev_attr_spi_master_transfer_bytes_histo14.attr,
228 &dev_attr_spi_master_transfer_bytes_histo15.attr,
229 &dev_attr_spi_master_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000230 &dev_attr_spi_master_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000231 NULL,
232};
233
234static const struct attribute_group spi_master_statistics_group = {
235 .name = "statistics",
236 .attrs = spi_master_statistics_attrs,
237};
238
239static const struct attribute_group *spi_master_groups[] = {
240 &spi_master_statistics_group,
241 NULL,
242};
243
244void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
245 struct spi_transfer *xfer,
246 struct spi_master *master)
247{
248 unsigned long flags;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000249 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
250
251 if (l2len < 0)
252 l2len = 0;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000253
254 spin_lock_irqsave(&stats->lock, flags);
255
256 stats->transfers++;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000257 stats->transfer_bytes_histo[l2len]++;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000258
259 stats->bytes += xfer->len;
260 if ((xfer->tx_buf) &&
261 (xfer->tx_buf != master->dummy_tx))
262 stats->bytes_tx += xfer->len;
263 if ((xfer->rx_buf) &&
264 (xfer->rx_buf != master->dummy_rx))
265 stats->bytes_rx += xfer->len;
266
267 spin_unlock_irqrestore(&stats->lock, flags);
268}
269EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800270
271/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
272 * and the sysfs version makes coldplug work too.
273 */
274
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700275static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
276 const struct spi_device *sdev)
277{
278 while (id->name[0]) {
279 if (!strcmp(sdev->modalias, id->name))
280 return id;
281 id++;
282 }
283 return NULL;
284}
285
286const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
287{
288 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
289
290 return spi_match_id(sdrv->id_table, sdev);
291}
292EXPORT_SYMBOL_GPL(spi_get_device_id);
293
David Brownell8ae12a02006-01-08 13:34:19 -0800294static int spi_match_device(struct device *dev, struct device_driver *drv)
295{
296 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700297 const struct spi_driver *sdrv = to_spi_driver(drv);
298
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600299 /* Attempt an OF style match */
300 if (of_driver_match_device(dev, drv))
301 return 1;
302
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100303 /* Then try ACPI */
304 if (acpi_driver_match_device(dev, drv))
305 return 1;
306
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700307 if (sdrv->id_table)
308 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800309
Kay Sievers35f74fc2009-01-06 10:44:37 -0800310 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800311}
312
Kay Sievers7eff2e72007-08-14 15:15:12 +0200313static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800314{
315 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800316 int rc;
317
318 rc = acpi_device_uevent_modalias(dev, env);
319 if (rc != -ENODEV)
320 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800321
Anton Vorontsove0626e32009-09-22 16:46:08 -0700322 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800323 return 0;
324}
325
David Brownell8ae12a02006-01-08 13:34:19 -0800326struct bus_type spi_bus_type = {
327 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700328 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800329 .match = spi_match_device,
330 .uevent = spi_uevent,
David Brownell8ae12a02006-01-08 13:34:19 -0800331};
332EXPORT_SYMBOL_GPL(spi_bus_type);
333
David Brownellb8852442006-01-08 13:34:23 -0800334
335static int spi_drv_probe(struct device *dev)
336{
337 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Jon Hunter44af7922015-10-09 15:45:55 +0100338 struct spi_device *spi = to_spi_device(dev);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300339 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800340
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200341 ret = of_clk_set_defaults(dev->of_node, false);
342 if (ret)
343 return ret;
344
Jon Hunter44af7922015-10-09 15:45:55 +0100345 if (dev->of_node) {
346 spi->irq = of_irq_get(dev->of_node, 0);
347 if (spi->irq == -EPROBE_DEFER)
348 return -EPROBE_DEFER;
349 if (spi->irq < 0)
350 spi->irq = 0;
351 }
352
Ulf Hansson676e7c22014-09-19 20:27:41 +0200353 ret = dev_pm_domain_attach(dev, true);
354 if (ret != -EPROBE_DEFER) {
Jon Hunter44af7922015-10-09 15:45:55 +0100355 ret = sdrv->probe(spi);
Ulf Hansson676e7c22014-09-19 20:27:41 +0200356 if (ret)
357 dev_pm_domain_detach(dev, true);
358 }
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300359
360 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800361}
362
363static int spi_drv_remove(struct device *dev)
364{
365 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300366 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800367
Jean Delvareaec35f42014-02-13 15:28:41 +0100368 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200369 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300370
371 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800372}
373
374static void spi_drv_shutdown(struct device *dev)
375{
376 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
377
378 sdrv->shutdown(to_spi_device(dev));
379}
380
David Brownell33e34dc2007-05-08 00:32:21 -0700381/**
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500382 * __spi_register_driver - register a SPI driver
Thierry Reding88c93212015-11-10 13:03:04 +0100383 * @owner: owner module of the driver to register
David Brownell33e34dc2007-05-08 00:32:21 -0700384 * @sdrv: the driver to register
385 * Context: can sleep
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200386 *
387 * Return: zero on success, else a negative error code.
David Brownell33e34dc2007-05-08 00:32:21 -0700388 */
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500389int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
David Brownellb8852442006-01-08 13:34:23 -0800390{
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500391 sdrv->driver.owner = owner;
David Brownellb8852442006-01-08 13:34:23 -0800392 sdrv->driver.bus = &spi_bus_type;
393 if (sdrv->probe)
394 sdrv->driver.probe = spi_drv_probe;
395 if (sdrv->remove)
396 sdrv->driver.remove = spi_drv_remove;
397 if (sdrv->shutdown)
398 sdrv->driver.shutdown = spi_drv_shutdown;
399 return driver_register(&sdrv->driver);
400}
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500401EXPORT_SYMBOL_GPL(__spi_register_driver);
David Brownellb8852442006-01-08 13:34:23 -0800402
David Brownell8ae12a02006-01-08 13:34:19 -0800403/*-------------------------------------------------------------------------*/
404
405/* SPI devices should normally not be created by SPI device drivers; that
406 * would make them board-specific. Similarly with SPI master drivers.
407 * Device registration normally goes into like arch/.../mach.../board-YYY.c
408 * with other readonly (flashable) information about mainboard devices.
409 */
410
411struct boardinfo {
412 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800413 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800414};
415
416static LIST_HEAD(board_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800417static LIST_HEAD(spi_master_list);
418
419/*
420 * Used to protect add/del opertion for board_info list and
421 * spi_master list, and their matching process
422 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700423static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800424
Grant Likelydc87c982008-05-15 16:50:22 -0600425/**
426 * spi_alloc_device - Allocate a new SPI device
427 * @master: Controller to which device is connected
428 * Context: can sleep
429 *
430 * Allows a driver to allocate and initialize a spi_device without
431 * registering it immediately. This allows a driver to directly
432 * fill the spi_device with device parameters before calling
433 * spi_add_device() on it.
434 *
435 * Caller is responsible to call spi_add_device() on the returned
436 * spi_device structure to add it to the SPI master. If the caller
437 * needs to discard the spi_device without adding it, then it should
438 * call spi_dev_put() on it.
439 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200440 * Return: a pointer to the new device, or NULL.
Grant Likelydc87c982008-05-15 16:50:22 -0600441 */
442struct spi_device *spi_alloc_device(struct spi_master *master)
443{
444 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600445
446 if (!spi_master_get(master))
447 return NULL;
448
Jingoo Han5fe5f052013-10-14 10:31:51 +0900449 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600450 if (!spi) {
Grant Likelydc87c982008-05-15 16:50:22 -0600451 spi_master_put(master);
452 return NULL;
453 }
454
455 spi->master = master;
Laurent Pinchart178db7d2011-12-12 01:15:06 +0100456 spi->dev.parent = &master->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600457 spi->dev.bus = &spi_bus_type;
458 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100459 spi->cs_gpio = -ENOENT;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000460
461 spin_lock_init(&spi->statistics.lock);
462
Grant Likelydc87c982008-05-15 16:50:22 -0600463 device_initialize(&spi->dev);
464 return spi;
465}
466EXPORT_SYMBOL_GPL(spi_alloc_device);
467
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200468static void spi_dev_set_name(struct spi_device *spi)
469{
470 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
471
472 if (adev) {
473 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
474 return;
475 }
476
477 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
478 spi->chip_select);
479}
480
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200481static int spi_dev_check(struct device *dev, void *data)
482{
483 struct spi_device *spi = to_spi_device(dev);
484 struct spi_device *new_spi = data;
485
486 if (spi->master == new_spi->master &&
487 spi->chip_select == new_spi->chip_select)
488 return -EBUSY;
489 return 0;
490}
491
Grant Likelydc87c982008-05-15 16:50:22 -0600492/**
493 * spi_add_device - Add spi_device allocated with spi_alloc_device
494 * @spi: spi_device to register
495 *
496 * Companion function to spi_alloc_device. Devices allocated with
497 * spi_alloc_device can be added onto the spi bus with this function.
498 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200499 * Return: 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600500 */
501int spi_add_device(struct spi_device *spi)
502{
David Brownelle48880e2008-08-15 00:40:44 -0700503 static DEFINE_MUTEX(spi_add_lock);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100504 struct spi_master *master = spi->master;
505 struct device *dev = master->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600506 int status;
507
508 /* Chipselects are numbered 0..max; validate. */
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100509 if (spi->chip_select >= master->num_chipselect) {
Grant Likelydc87c982008-05-15 16:50:22 -0600510 dev_err(dev, "cs%d >= max %d\n",
511 spi->chip_select,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100512 master->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600513 return -EINVAL;
514 }
515
516 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200517 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700518
519 /* We need to make sure there's no other device with this
520 * chipselect **BEFORE** we call setup(), else we'll trash
521 * its configuration. Lock against concurrent add() calls.
522 */
523 mutex_lock(&spi_add_lock);
524
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200525 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
526 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700527 dev_err(dev, "chipselect %d already in use\n",
528 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700529 goto done;
530 }
531
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100532 if (master->cs_gpios)
533 spi->cs_gpio = master->cs_gpios[spi->chip_select];
534
David Brownelle48880e2008-08-15 00:40:44 -0700535 /* Drivers may modify this initial i/o setup, but will
536 * normally rely on the device being setup. Devices
537 * using SPI_CS_HIGH can't coexist well otherwise...
538 */
David Brownell7d077192009-06-17 16:26:03 -0700539 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600540 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200541 dev_err(dev, "can't setup %s, status %d\n",
542 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700543 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600544 }
545
David Brownelle48880e2008-08-15 00:40:44 -0700546 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600547 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700548 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200549 dev_err(dev, "can't add %s, status %d\n",
550 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700551 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800552 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600553
David Brownelle48880e2008-08-15 00:40:44 -0700554done:
555 mutex_unlock(&spi_add_lock);
556 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600557}
558EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800559
David Brownell33e34dc2007-05-08 00:32:21 -0700560/**
561 * spi_new_device - instantiate one new SPI device
562 * @master: Controller to which device is connected
563 * @chip: Describes the SPI device
564 * Context: can sleep
565 *
566 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800567 * after board init creates the hard-wired devices. Some development
568 * platforms may not be able to use spi_register_board_info though, and
569 * this is exported so that for example a USB or parport based adapter
570 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700571 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200572 * Return: the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800573 */
Adrian Bunke9d5a462007-03-26 21:32:23 -0800574struct spi_device *spi_new_device(struct spi_master *master,
575 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800576{
577 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800578 int status;
579
David Brownell082c8cb2007-07-31 00:39:45 -0700580 /* NOTE: caller did any chip->bus_num checks necessary.
581 *
582 * Also, unless we change the return value convention to use
583 * error-or-pointer (not NULL-or-pointer), troubleshootability
584 * suggests syslogged diagnostics are best here (ugh).
585 */
586
Grant Likelydc87c982008-05-15 16:50:22 -0600587 proxy = spi_alloc_device(master);
588 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800589 return NULL;
590
Grant Likely102eb972008-07-23 21:29:55 -0700591 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
592
David Brownell8ae12a02006-01-08 13:34:19 -0800593 proxy->chip_select = chip->chip_select;
594 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700595 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800596 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700597 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800598 proxy->dev.platform_data = (void *) chip->platform_data;
599 proxy->controller_data = chip->controller_data;
600 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800601
Grant Likelydc87c982008-05-15 16:50:22 -0600602 status = spi_add_device(proxy);
David Brownell8ae12a02006-01-08 13:34:19 -0800603 if (status < 0) {
Grant Likelydc87c982008-05-15 16:50:22 -0600604 spi_dev_put(proxy);
605 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800606 }
607
David Brownell8ae12a02006-01-08 13:34:19 -0800608 return proxy;
609}
610EXPORT_SYMBOL_GPL(spi_new_device);
611
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100612/**
613 * spi_unregister_device - unregister a single SPI device
614 * @spi: spi_device to unregister
615 *
616 * Start making the passed SPI device vanish. Normally this would be handled
617 * by spi_unregister_master().
618 */
619void spi_unregister_device(struct spi_device *spi)
620{
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100621 if (!spi)
622 return;
623
Johan Hovold83241472017-01-30 17:47:05 +0100624 if (spi->dev.of_node) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100625 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
Johan Hovold83241472017-01-30 17:47:05 +0100626 of_node_put(spi->dev.of_node);
627 }
Octavian Purdila7f244672016-07-08 19:13:11 +0300628 if (ACPI_COMPANION(&spi->dev))
629 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100630 device_unregister(&spi->dev);
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100631}
632EXPORT_SYMBOL_GPL(spi_unregister_device);
633
Feng Tang2b9603a2010-08-02 15:52:15 +0800634static void spi_match_master_to_boardinfo(struct spi_master *master,
635 struct spi_board_info *bi)
636{
637 struct spi_device *dev;
638
639 if (master->bus_num != bi->bus_num)
640 return;
641
642 dev = spi_new_device(master, bi);
643 if (!dev)
644 dev_err(master->dev.parent, "can't create new device for %s\n",
645 bi->modalias);
646}
647
David Brownell33e34dc2007-05-08 00:32:21 -0700648/**
649 * spi_register_board_info - register SPI devices for a given board
650 * @info: array of chip descriptors
651 * @n: how many descriptors are provided
652 * Context: can sleep
653 *
David Brownell8ae12a02006-01-08 13:34:19 -0800654 * Board-specific early init code calls this (probably during arch_initcall)
655 * with segments of the SPI device table. Any device nodes are created later,
656 * after the relevant parent SPI controller (bus_num) is defined. We keep
657 * this table of devices forever, so that reloading a controller driver will
658 * not make Linux forget about these hard-wired devices.
659 *
660 * Other code can also call this, e.g. a particular add-on board might provide
661 * SPI devices through its expansion connector, so code initializing that board
662 * would naturally declare its SPI devices.
663 *
664 * The board info passed can safely be __initdata ... but be careful of
665 * any embedded pointers (platform_data, etc), they're copied as-is.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200666 *
667 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -0800668 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000669int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800670{
Feng Tang2b9603a2010-08-02 15:52:15 +0800671 struct boardinfo *bi;
672 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800673
Xiubo Lic7908a32014-09-24 14:30:29 +0800674 if (!n)
675 return -EINVAL;
676
Markus Elfringf9bdb7f2017-01-13 12:28:04 +0100677 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800678 if (!bi)
679 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800680
Feng Tang2b9603a2010-08-02 15:52:15 +0800681 for (i = 0; i < n; i++, bi++, info++) {
682 struct spi_master *master;
David Brownell8ae12a02006-01-08 13:34:19 -0800683
Feng Tang2b9603a2010-08-02 15:52:15 +0800684 memcpy(&bi->board_info, info, sizeof(*info));
685 mutex_lock(&board_lock);
686 list_add_tail(&bi->list, &board_list);
687 list_for_each_entry(master, &spi_master_list, list)
688 spi_match_master_to_boardinfo(master, &bi->board_info);
689 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800690 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800691
692 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800693}
694
695/*-------------------------------------------------------------------------*/
696
Mark Brownb1589352013-10-05 11:50:40 +0100697static void spi_set_cs(struct spi_device *spi, bool enable)
698{
699 if (spi->mode & SPI_CS_HIGH)
700 enable = !enable;
701
Thor Thayer8eee6b92016-10-10 09:25:24 -0500702 if (gpio_is_valid(spi->cs_gpio)) {
Mark Brownb1589352013-10-05 11:50:40 +0100703 gpio_set_value(spi->cs_gpio, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500704 /* Some SPI masters need both GPIO CS & slave_select */
705 if ((spi->master->flags & SPI_MASTER_GPIO_SS) &&
706 spi->master->set_cs)
707 spi->master->set_cs(spi, !enable);
708 } else if (spi->master->set_cs) {
Mark Brownb1589352013-10-05 11:50:40 +0100709 spi->master->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500710 }
Mark Brownb1589352013-10-05 11:50:40 +0100711}
712
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200713#ifdef CONFIG_HAS_DMA
Mark Brown6ad45a22014-02-02 13:47:47 +0000714static int spi_map_buf(struct spi_master *master, struct device *dev,
715 struct sg_table *sgt, void *buf, size_t len,
716 enum dma_data_direction dir)
717{
718 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200719 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530720#ifdef CONFIG_HIGHMEM
721 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
722 (unsigned long)buf < (PKMAP_BASE +
723 (LAST_PKMAP * PAGE_SIZE)));
724#else
725 const bool kmap_buf = false;
726#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500727 int desc_len;
728 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000729 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600730 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000731 void *sg_buf;
732 size_t min;
733 int i, ret;
734
Vignesh Rb1b81532016-08-17 15:22:36 +0530735 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200736 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500737 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530738 } else if (virt_addr_valid(buf)) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200739 desc_len = min_t(int, max_seg_size, master->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500740 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530741 } else {
742 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500743 }
744
Mark Brown6ad45a22014-02-02 13:47:47 +0000745 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
746 if (ret != 0)
747 return ret;
748
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600749 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000750 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000751
Vignesh Rb1b81532016-08-17 15:22:36 +0530752 if (vmalloced_buf || kmap_buf) {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500753 min = min_t(size_t,
754 len, desc_len - offset_in_page(buf));
Vignesh Rb1b81532016-08-17 15:22:36 +0530755 if (vmalloced_buf)
756 vm_page = vmalloc_to_page(buf);
757 else
758 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000759 if (!vm_page) {
760 sg_free_table(sgt);
761 return -ENOMEM;
762 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600763 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000764 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000765 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500766 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000767 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600768 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000769 }
770
Mark Brown6ad45a22014-02-02 13:47:47 +0000771 buf += min;
772 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600773 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000774 }
775
776 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200777 if (!ret)
778 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000779 if (ret < 0) {
780 sg_free_table(sgt);
781 return ret;
782 }
783
784 sgt->nents = ret;
785
786 return 0;
787}
788
789static void spi_unmap_buf(struct spi_master *master, struct device *dev,
790 struct sg_table *sgt, enum dma_data_direction dir)
791{
792 if (sgt->orig_nents) {
793 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
794 sg_free_table(sgt);
795 }
796}
797
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200798static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000799{
Mark Brown99adef32014-01-16 12:22:43 +0000800 struct device *tx_dev, *rx_dev;
801 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000802 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000803
Mark Brown6ad45a22014-02-02 13:47:47 +0000804 if (!master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000805 return 0;
806
Leilk Liuc37f45b2015-07-23 17:10:40 +0800807 if (master->dma_tx)
808 tx_dev = master->dma_tx->device->dev;
809 else
810 tx_dev = &master->dev;
811
812 if (master->dma_rx)
813 rx_dev = master->dma_rx->device->dev;
814 else
815 rx_dev = &master->dev;
Mark Brown99adef32014-01-16 12:22:43 +0000816
817 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
818 if (!master->can_dma(master, msg->spi, xfer))
819 continue;
820
821 if (xfer->tx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000822 ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
823 (void *)xfer->tx_buf, xfer->len,
824 DMA_TO_DEVICE);
825 if (ret != 0)
826 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000827 }
828
829 if (xfer->rx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000830 ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
831 xfer->rx_buf, xfer->len,
832 DMA_FROM_DEVICE);
833 if (ret != 0) {
834 spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
835 DMA_TO_DEVICE);
836 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000837 }
838 }
839 }
840
841 master->cur_msg_mapped = true;
842
843 return 0;
844}
845
Martin Sperl4b786452015-05-25 10:13:10 +0000846static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000847{
848 struct spi_transfer *xfer;
849 struct device *tx_dev, *rx_dev;
850
Mark Brown6ad45a22014-02-02 13:47:47 +0000851 if (!master->cur_msg_mapped || !master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000852 return 0;
853
Leilk Liuc37f45b2015-07-23 17:10:40 +0800854 if (master->dma_tx)
855 tx_dev = master->dma_tx->device->dev;
856 else
857 tx_dev = &master->dev;
858
859 if (master->dma_rx)
860 rx_dev = master->dma_rx->device->dev;
861 else
862 rx_dev = &master->dev;
Mark Brown99adef32014-01-16 12:22:43 +0000863
864 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
865 if (!master->can_dma(master, msg->spi, xfer))
866 continue;
867
Mark Brown6ad45a22014-02-02 13:47:47 +0000868 spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
869 spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000870 }
871
872 return 0;
873}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200874#else /* !CONFIG_HAS_DMA */
Vignesh Rf4502dd2016-06-08 12:18:31 +0530875static inline int spi_map_buf(struct spi_master *master,
876 struct device *dev, struct sg_table *sgt,
877 void *buf, size_t len,
878 enum dma_data_direction dir)
879{
880 return -EINVAL;
881}
882
883static inline void spi_unmap_buf(struct spi_master *master,
884 struct device *dev, struct sg_table *sgt,
885 enum dma_data_direction dir)
886{
887}
888
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200889static inline int __spi_map_msg(struct spi_master *master,
890 struct spi_message *msg)
891{
892 return 0;
893}
894
Martin Sperl4b786452015-05-25 10:13:10 +0000895static inline int __spi_unmap_msg(struct spi_master *master,
896 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200897{
898 return 0;
899}
900#endif /* !CONFIG_HAS_DMA */
901
Martin Sperl4b786452015-05-25 10:13:10 +0000902static inline int spi_unmap_msg(struct spi_master *master,
903 struct spi_message *msg)
904{
905 struct spi_transfer *xfer;
906
907 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
908 /*
909 * Restore the original value of tx_buf or rx_buf if they are
910 * NULL.
911 */
912 if (xfer->tx_buf == master->dummy_tx)
913 xfer->tx_buf = NULL;
914 if (xfer->rx_buf == master->dummy_rx)
915 xfer->rx_buf = NULL;
916 }
917
918 return __spi_unmap_msg(master, msg);
919}
920
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200921static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
922{
923 struct spi_transfer *xfer;
924 void *tmp;
925 unsigned int max_tx, max_rx;
926
927 if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
928 max_tx = 0;
929 max_rx = 0;
930
931 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
932 if ((master->flags & SPI_MASTER_MUST_TX) &&
933 !xfer->tx_buf)
934 max_tx = max(xfer->len, max_tx);
935 if ((master->flags & SPI_MASTER_MUST_RX) &&
936 !xfer->rx_buf)
937 max_rx = max(xfer->len, max_rx);
938 }
939
940 if (max_tx) {
941 tmp = krealloc(master->dummy_tx, max_tx,
942 GFP_KERNEL | GFP_DMA);
943 if (!tmp)
944 return -ENOMEM;
945 master->dummy_tx = tmp;
946 memset(tmp, 0, max_tx);
947 }
948
949 if (max_rx) {
950 tmp = krealloc(master->dummy_rx, max_rx,
951 GFP_KERNEL | GFP_DMA);
952 if (!tmp)
953 return -ENOMEM;
954 master->dummy_rx = tmp;
955 }
956
957 if (max_tx || max_rx) {
958 list_for_each_entry(xfer, &msg->transfers,
959 transfer_list) {
960 if (!xfer->tx_buf)
961 xfer->tx_buf = master->dummy_tx;
962 if (!xfer->rx_buf)
963 xfer->rx_buf = master->dummy_rx;
964 }
965 }
966 }
967
968 return __spi_map_msg(master, msg);
969}
Mark Brown99adef32014-01-16 12:22:43 +0000970
Mark Brownb1589352013-10-05 11:50:40 +0100971/*
972 * spi_transfer_one_message - Default implementation of transfer_one_message()
973 *
974 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -0700975 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +0100976 * standard handling of delays and chip select management.
977 */
978static int spi_transfer_one_message(struct spi_master *master,
979 struct spi_message *msg)
980{
981 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +0100982 bool keep_cs = false;
983 int ret = 0;
Sien Wud0716dd2016-09-01 18:24:29 -0500984 unsigned long long ms = 1;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000985 struct spi_statistics *statm = &master->statistics;
986 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +0100987
988 spi_set_cs(msg->spi, true);
989
Martin Sperleca2ebc2015-06-22 13:00:36 +0000990 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
991 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
992
Mark Brownb1589352013-10-05 11:50:40 +0100993 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
994 trace_spi_transfer_start(msg, xfer);
995
Martin Sperleca2ebc2015-06-22 13:00:36 +0000996 spi_statistics_add_transfer_stats(statm, xfer, master);
997 spi_statistics_add_transfer_stats(stats, xfer, master);
998
Mark Brown38ec10f2014-08-16 16:27:41 +0100999 if (xfer->tx_buf || xfer->rx_buf) {
1000 reinit_completion(&master->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001001
Mark Brown38ec10f2014-08-16 16:27:41 +01001002 ret = master->transfer_one(master, msg->spi, xfer);
1003 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001004 SPI_STATISTICS_INCREMENT_FIELD(statm,
1005 errors);
1006 SPI_STATISTICS_INCREMENT_FIELD(stats,
1007 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001008 dev_err(&msg->spi->dev,
1009 "SPI transfer failed: %d\n", ret);
1010 goto out;
1011 }
Mark Brownb1589352013-10-05 11:50:40 +01001012
Mark Brown38ec10f2014-08-16 16:27:41 +01001013 if (ret > 0) {
1014 ret = 0;
Sien Wud0716dd2016-09-01 18:24:29 -05001015 ms = 8LL * 1000LL * xfer->len;
1016 do_div(ms, xfer->speed_hz);
Mark Brown38ec10f2014-08-16 16:27:41 +01001017 ms += ms + 100; /* some tolerance */
Mark Brown16a0ce42014-01-30 22:16:41 +00001018
Sien Wud0716dd2016-09-01 18:24:29 -05001019 if (ms > UINT_MAX)
1020 ms = UINT_MAX;
1021
Mark Brown38ec10f2014-08-16 16:27:41 +01001022 ms = wait_for_completion_timeout(&master->xfer_completion,
1023 msecs_to_jiffies(ms));
1024 }
Mark Brown16a0ce42014-01-30 22:16:41 +00001025
Mark Brown38ec10f2014-08-16 16:27:41 +01001026 if (ms == 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001027 SPI_STATISTICS_INCREMENT_FIELD(statm,
1028 timedout);
1029 SPI_STATISTICS_INCREMENT_FIELD(stats,
1030 timedout);
Mark Brown38ec10f2014-08-16 16:27:41 +01001031 dev_err(&msg->spi->dev,
1032 "SPI transfer timed out\n");
1033 msg->status = -ETIMEDOUT;
1034 }
1035 } else {
1036 if (xfer->len)
1037 dev_err(&msg->spi->dev,
1038 "Bufferless transfer has length %u\n",
1039 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001040 }
Mark Brownb1589352013-10-05 11:50:40 +01001041
1042 trace_spi_transfer_stop(msg, xfer);
1043
1044 if (msg->status != -EINPROGRESS)
1045 goto out;
1046
Daniel Kurtz8244bd32016-10-07 18:55:47 +08001047 if (xfer->delay_usecs) {
1048 u16 us = xfer->delay_usecs;
1049
1050 if (us <= 10)
1051 udelay(us);
1052 else
1053 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1054 }
Mark Brownb1589352013-10-05 11:50:40 +01001055
1056 if (xfer->cs_change) {
1057 if (list_is_last(&xfer->transfer_list,
1058 &msg->transfers)) {
1059 keep_cs = true;
1060 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001061 spi_set_cs(msg->spi, false);
1062 udelay(10);
1063 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001064 }
1065 }
1066
1067 msg->actual_length += xfer->len;
1068 }
1069
1070out:
1071 if (ret != 0 || !keep_cs)
1072 spi_set_cs(msg->spi, false);
1073
1074 if (msg->status == -EINPROGRESS)
1075 msg->status = ret;
1076
Geert Uytterhoevenff61eb42015-04-07 20:39:19 +02001077 if (msg->status && master->handle_err)
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001078 master->handle_err(master, msg);
1079
Martin Sperld780c372015-12-14 15:20:18 +00001080 spi_res_release(master, msg);
1081
Mark Brownb1589352013-10-05 11:50:40 +01001082 spi_finalize_current_message(master);
1083
1084 return ret;
1085}
1086
1087/**
1088 * spi_finalize_current_transfer - report completion of a transfer
Thierry Reding2c675682014-08-08 13:02:36 +02001089 * @master: the master reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001090 *
1091 * Called by SPI drivers using the core transfer_one_message()
1092 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001093 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001094 */
1095void spi_finalize_current_transfer(struct spi_master *master)
1096{
1097 complete(&master->xfer_completion);
1098}
1099EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1100
Linus Walleijffbbdd212012-02-22 10:05:38 +01001101/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001102 * __spi_pump_messages - function which processes spi message queue
1103 * @master: master to process queue for
1104 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001105 *
1106 * This function checks if there is any spi message in the queue that
1107 * needs processing and if so call out to the driver to initialize hardware
1108 * and transfer each message.
1109 *
Mark Brown0461a412014-12-09 21:38:05 +00001110 * Note that it is called both from the kthread itself and also from
1111 * inside spi_sync(); the queue extraction handling at the top of the
1112 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001113 */
Mark Brownef4d96e2016-07-21 23:53:31 +01001114static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001115{
Linus Walleijffbbdd212012-02-22 10:05:38 +01001116 unsigned long flags;
1117 bool was_busy = false;
1118 int ret;
1119
Mark Brown983aee52014-12-09 19:46:56 +00001120 /* Lock queue */
Linus Walleijffbbdd212012-02-22 10:05:38 +01001121 spin_lock_irqsave(&master->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001122
1123 /* Make sure we are not already running a message */
1124 if (master->cur_msg) {
1125 spin_unlock_irqrestore(&master->queue_lock, flags);
1126 return;
1127 }
1128
Mark Brown0461a412014-12-09 21:38:05 +00001129 /* If another context is idling the device then defer */
1130 if (master->idling) {
Petr Mladek39891442016-10-11 13:55:20 -07001131 kthread_queue_work(&master->kworker, &master->pump_messages);
Mark Brown0461a412014-12-09 21:38:05 +00001132 spin_unlock_irqrestore(&master->queue_lock, flags);
1133 return;
1134 }
1135
Mark Brown983aee52014-12-09 19:46:56 +00001136 /* Check if the queue is idle */
Linus Walleijffbbdd212012-02-22 10:05:38 +01001137 if (list_empty(&master->queue) || !master->running) {
Bryan Freedb0b36b82013-03-13 11:17:40 -07001138 if (!master->busy) {
1139 spin_unlock_irqrestore(&master->queue_lock, flags);
1140 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001141 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001142
1143 /* Only do teardown in the thread */
1144 if (!in_kthread) {
Petr Mladek39891442016-10-11 13:55:20 -07001145 kthread_queue_work(&master->kworker,
Mark Brownfc9e0f72014-12-10 13:46:33 +00001146 &master->pump_messages);
1147 spin_unlock_irqrestore(&master->queue_lock, flags);
1148 return;
1149 }
1150
Linus Walleijffbbdd212012-02-22 10:05:38 +01001151 master->busy = false;
Mark Brown0461a412014-12-09 21:38:05 +00001152 master->idling = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001153 spin_unlock_irqrestore(&master->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001154
Mark Brown3a2eba92014-01-28 20:17:03 +00001155 kfree(master->dummy_rx);
1156 master->dummy_rx = NULL;
1157 kfree(master->dummy_tx);
1158 master->dummy_tx = NULL;
Bryan Freedb0b36b82013-03-13 11:17:40 -07001159 if (master->unprepare_transfer_hardware &&
1160 master->unprepare_transfer_hardware(master))
1161 dev_err(&master->dev,
1162 "failed to unprepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001163 if (master->auto_runtime_pm) {
1164 pm_runtime_mark_last_busy(master->dev.parent);
1165 pm_runtime_put_autosuspend(master->dev.parent);
1166 }
Mark Brown56ec1972013-10-07 19:33:53 +01001167 trace_spi_master_idle(master);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001168
Mark Brown0461a412014-12-09 21:38:05 +00001169 spin_lock_irqsave(&master->queue_lock, flags);
1170 master->idling = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001171 spin_unlock_irqrestore(&master->queue_lock, flags);
1172 return;
1173 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001174
Linus Walleijffbbdd212012-02-22 10:05:38 +01001175 /* Extract head of queue */
1176 master->cur_msg =
Axel Lina89e2d22014-01-09 16:03:58 +08001177 list_first_entry(&master->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001178
1179 list_del_init(&master->cur_msg->queue);
1180 if (master->busy)
1181 was_busy = true;
1182 else
1183 master->busy = true;
1184 spin_unlock_irqrestore(&master->queue_lock, flags);
1185
Mark Brownef4d96e2016-07-21 23:53:31 +01001186 mutex_lock(&master->io_mutex);
1187
Mark Brown49834de2013-07-28 14:47:02 +01001188 if (!was_busy && master->auto_runtime_pm) {
1189 ret = pm_runtime_get_sync(master->dev.parent);
1190 if (ret < 0) {
1191 dev_err(&master->dev, "Failed to power device: %d\n",
1192 ret);
Mark Brown764f2162016-08-09 17:45:33 +01001193 mutex_unlock(&master->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001194 return;
1195 }
1196 }
1197
Mark Brown56ec1972013-10-07 19:33:53 +01001198 if (!was_busy)
1199 trace_spi_master_busy(master);
1200
Shubhrajyoti D7dfd2bd2012-05-10 19:20:41 +05301201 if (!was_busy && master->prepare_transfer_hardware) {
Linus Walleijffbbdd212012-02-22 10:05:38 +01001202 ret = master->prepare_transfer_hardware(master);
1203 if (ret) {
1204 dev_err(&master->dev,
1205 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001206
1207 if (master->auto_runtime_pm)
1208 pm_runtime_put(master->dev.parent);
Mark Brown764f2162016-08-09 17:45:33 +01001209 mutex_unlock(&master->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001210 return;
1211 }
1212 }
1213
Mark Brown56ec1972013-10-07 19:33:53 +01001214 trace_spi_message_start(master->cur_msg);
1215
Mark Brown2841a5f2013-10-05 00:23:12 +01001216 if (master->prepare_message) {
1217 ret = master->prepare_message(master, master->cur_msg);
1218 if (ret) {
1219 dev_err(&master->dev,
1220 "failed to prepare message: %d\n", ret);
1221 master->cur_msg->status = ret;
1222 spi_finalize_current_message(master);
Jon Hunter49023d22016-03-08 12:28:20 +00001223 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001224 }
1225 master->cur_msg_prepared = true;
1226 }
1227
Mark Brown99adef32014-01-16 12:22:43 +00001228 ret = spi_map_msg(master, master->cur_msg);
1229 if (ret) {
1230 master->cur_msg->status = ret;
1231 spi_finalize_current_message(master);
Jon Hunter49023d22016-03-08 12:28:20 +00001232 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001233 }
1234
Linus Walleijffbbdd212012-02-22 10:05:38 +01001235 ret = master->transfer_one_message(master, master->cur_msg);
1236 if (ret) {
1237 dev_err(&master->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001238 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001239 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001240 }
Jon Hunter49023d22016-03-08 12:28:20 +00001241
1242out:
Mark Brownef4d96e2016-07-21 23:53:31 +01001243 mutex_unlock(&master->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001244
1245 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001246 if (!ret)
1247 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001248}
1249
Mark Brownfc9e0f72014-12-10 13:46:33 +00001250/**
1251 * spi_pump_messages - kthread work function which processes spi message queue
1252 * @work: pointer to kthread work struct contained in the master struct
1253 */
1254static void spi_pump_messages(struct kthread_work *work)
1255{
1256 struct spi_master *master =
1257 container_of(work, struct spi_master, pump_messages);
1258
Mark Brownef4d96e2016-07-21 23:53:31 +01001259 __spi_pump_messages(master, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001260}
1261
Linus Walleijffbbdd212012-02-22 10:05:38 +01001262static int spi_init_queue(struct spi_master *master)
1263{
1264 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1265
Linus Walleijffbbdd212012-02-22 10:05:38 +01001266 master->running = false;
1267 master->busy = false;
1268
Petr Mladek39891442016-10-11 13:55:20 -07001269 kthread_init_worker(&master->kworker);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001270 master->kworker_task = kthread_run(kthread_worker_fn,
Kees Cookf1701682013-07-03 15:04:58 -07001271 &master->kworker, "%s",
Linus Walleijffbbdd212012-02-22 10:05:38 +01001272 dev_name(&master->dev));
1273 if (IS_ERR(master->kworker_task)) {
1274 dev_err(&master->dev, "failed to create message pump task\n");
Jarkko Nikula98a8f5a2014-12-04 11:02:25 +02001275 return PTR_ERR(master->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001276 }
Petr Mladek39891442016-10-11 13:55:20 -07001277 kthread_init_work(&master->pump_messages, spi_pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001278
1279 /*
1280 * Master config will indicate if this controller should run the
1281 * message pump with high (realtime) priority to reduce the transfer
1282 * latency on the bus by minimising the delay between a transfer
1283 * request and the scheduling of the message pump thread. Without this
1284 * setting the message pump thread will remain at default priority.
1285 */
1286 if (master->rt) {
1287 dev_info(&master->dev,
1288 "will run message pump with realtime priority\n");
1289 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
1290 }
1291
1292 return 0;
1293}
1294
1295/**
1296 * spi_get_next_queued_message() - called by driver to check for queued
1297 * messages
1298 * @master: the master to check for queued messages
1299 *
1300 * If there are more messages in the queue, the next message is returned from
1301 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001302 *
1303 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001304 */
1305struct spi_message *spi_get_next_queued_message(struct spi_master *master)
1306{
1307 struct spi_message *next;
1308 unsigned long flags;
1309
1310 /* get a pointer to the next message, if any */
1311 spin_lock_irqsave(&master->queue_lock, flags);
Axel Lin1cfd97f2014-01-02 15:16:40 +08001312 next = list_first_entry_or_null(&master->queue, struct spi_message,
1313 queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001314 spin_unlock_irqrestore(&master->queue_lock, flags);
1315
1316 return next;
1317}
1318EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1319
1320/**
1321 * spi_finalize_current_message() - the current message is complete
1322 * @master: the master to return the message to
1323 *
1324 * Called by the driver to notify the core that the message in the front of the
1325 * queue is complete and can be removed from the queue.
1326 */
1327void spi_finalize_current_message(struct spi_master *master)
1328{
1329 struct spi_message *mesg;
1330 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001331 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001332
1333 spin_lock_irqsave(&master->queue_lock, flags);
1334 mesg = master->cur_msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001335 spin_unlock_irqrestore(&master->queue_lock, flags);
1336
Mark Brown99adef32014-01-16 12:22:43 +00001337 spi_unmap_msg(master, mesg);
1338
Mark Brown2841a5f2013-10-05 00:23:12 +01001339 if (master->cur_msg_prepared && master->unprepare_message) {
1340 ret = master->unprepare_message(master, mesg);
1341 if (ret) {
1342 dev_err(&master->dev,
1343 "failed to unprepare message: %d\n", ret);
1344 }
1345 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001346
Martin Sperl8e76ef82015-05-10 07:50:45 +00001347 spin_lock_irqsave(&master->queue_lock, flags);
1348 master->cur_msg = NULL;
Mark Brown2841a5f2013-10-05 00:23:12 +01001349 master->cur_msg_prepared = false;
Petr Mladek39891442016-10-11 13:55:20 -07001350 kthread_queue_work(&master->kworker, &master->pump_messages);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001351 spin_unlock_irqrestore(&master->queue_lock, flags);
1352
1353 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001354
Linus Walleijffbbdd212012-02-22 10:05:38 +01001355 mesg->state = NULL;
1356 if (mesg->complete)
1357 mesg->complete(mesg->context);
1358}
1359EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1360
1361static int spi_start_queue(struct spi_master *master)
1362{
1363 unsigned long flags;
1364
1365 spin_lock_irqsave(&master->queue_lock, flags);
1366
1367 if (master->running || master->busy) {
1368 spin_unlock_irqrestore(&master->queue_lock, flags);
1369 return -EBUSY;
1370 }
1371
1372 master->running = true;
1373 master->cur_msg = NULL;
1374 spin_unlock_irqrestore(&master->queue_lock, flags);
1375
Petr Mladek39891442016-10-11 13:55:20 -07001376 kthread_queue_work(&master->kworker, &master->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001377
1378 return 0;
1379}
1380
1381static int spi_stop_queue(struct spi_master *master)
1382{
1383 unsigned long flags;
1384 unsigned limit = 500;
1385 int ret = 0;
1386
1387 spin_lock_irqsave(&master->queue_lock, flags);
1388
1389 /*
1390 * This is a bit lame, but is optimized for the common execution path.
1391 * A wait_queue on the master->busy could be used, but then the common
1392 * execution path (pump_messages) would be required to call wake_up or
1393 * friends on every SPI message. Do this instead.
1394 */
1395 while ((!list_empty(&master->queue) || master->busy) && limit--) {
1396 spin_unlock_irqrestore(&master->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001397 usleep_range(10000, 11000);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001398 spin_lock_irqsave(&master->queue_lock, flags);
1399 }
1400
1401 if (!list_empty(&master->queue) || master->busy)
1402 ret = -EBUSY;
1403 else
1404 master->running = false;
1405
1406 spin_unlock_irqrestore(&master->queue_lock, flags);
1407
1408 if (ret) {
1409 dev_warn(&master->dev,
1410 "could not stop message queue\n");
1411 return ret;
1412 }
1413 return ret;
1414}
1415
1416static int spi_destroy_queue(struct spi_master *master)
1417{
1418 int ret;
1419
1420 ret = spi_stop_queue(master);
1421
1422 /*
Petr Mladek39891442016-10-11 13:55:20 -07001423 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001424 * If the reason that stop_queue timed out is that the work will never
1425 * finish, then it does no good to call flush/stop thread, so
1426 * return anyway.
1427 */
1428 if (ret) {
1429 dev_err(&master->dev, "problem destroying queue\n");
1430 return ret;
1431 }
1432
Petr Mladek39891442016-10-11 13:55:20 -07001433 kthread_flush_worker(&master->kworker);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001434 kthread_stop(master->kworker_task);
1435
1436 return 0;
1437}
1438
Mark Brown0461a412014-12-09 21:38:05 +00001439static int __spi_queued_transfer(struct spi_device *spi,
1440 struct spi_message *msg,
1441 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001442{
1443 struct spi_master *master = spi->master;
1444 unsigned long flags;
1445
1446 spin_lock_irqsave(&master->queue_lock, flags);
1447
1448 if (!master->running) {
1449 spin_unlock_irqrestore(&master->queue_lock, flags);
1450 return -ESHUTDOWN;
1451 }
1452 msg->actual_length = 0;
1453 msg->status = -EINPROGRESS;
1454
1455 list_add_tail(&msg->queue, &master->queue);
Mark Brown0461a412014-12-09 21:38:05 +00001456 if (!master->busy && need_pump)
Petr Mladek39891442016-10-11 13:55:20 -07001457 kthread_queue_work(&master->kworker, &master->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001458
1459 spin_unlock_irqrestore(&master->queue_lock, flags);
1460 return 0;
1461}
1462
Mark Brown0461a412014-12-09 21:38:05 +00001463/**
1464 * spi_queued_transfer - transfer function for queued transfers
1465 * @spi: spi device which is requesting transfer
1466 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001467 *
1468 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001469 */
1470static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1471{
1472 return __spi_queued_transfer(spi, msg, true);
1473}
1474
Linus Walleijffbbdd212012-02-22 10:05:38 +01001475static int spi_master_initialize_queue(struct spi_master *master)
1476{
1477 int ret;
1478
Linus Walleijffbbdd212012-02-22 10:05:38 +01001479 master->transfer = spi_queued_transfer;
Mark Brownb1589352013-10-05 11:50:40 +01001480 if (!master->transfer_one_message)
1481 master->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001482
1483 /* Initialize and start queue */
1484 ret = spi_init_queue(master);
1485 if (ret) {
1486 dev_err(&master->dev, "problem initializing queue\n");
1487 goto err_init_queue;
1488 }
Mark Brownc3676d52014-05-01 10:47:52 -07001489 master->queued = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001490 ret = spi_start_queue(master);
1491 if (ret) {
1492 dev_err(&master->dev, "problem starting queue\n");
1493 goto err_start_queue;
1494 }
1495
1496 return 0;
1497
1498err_start_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001499 spi_destroy_queue(master);
Mark Brownc3676d52014-05-01 10:47:52 -07001500err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001501 return ret;
1502}
1503
1504/*-------------------------------------------------------------------------*/
1505
Andreas Larsson7cb94362012-12-04 15:09:38 +01001506#if defined(CONFIG_OF)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001507static struct spi_device *
1508of_register_spi_device(struct spi_master *master, struct device_node *nc)
1509{
1510 struct spi_device *spi;
1511 int rc;
1512 u32 value;
1513
1514 /* Alloc an spi_device */
1515 spi = spi_alloc_device(master);
1516 if (!spi) {
1517 dev_err(&master->dev, "spi_device alloc error for %s\n",
1518 nc->full_name);
1519 rc = -ENOMEM;
1520 goto err_out;
1521 }
1522
1523 /* Select device driver */
1524 rc = of_modalias_node(nc, spi->modalias,
1525 sizeof(spi->modalias));
1526 if (rc < 0) {
1527 dev_err(&master->dev, "cannot find modalias for %s\n",
1528 nc->full_name);
1529 goto err_out;
1530 }
1531
1532 /* Device address */
1533 rc = of_property_read_u32(nc, "reg", &value);
1534 if (rc) {
1535 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1536 nc->full_name, rc);
1537 goto err_out;
1538 }
1539 spi->chip_select = value;
1540
1541 /* Mode (clock phase/polarity/etc.) */
1542 if (of_find_property(nc, "spi-cpha", NULL))
1543 spi->mode |= SPI_CPHA;
1544 if (of_find_property(nc, "spi-cpol", NULL))
1545 spi->mode |= SPI_CPOL;
1546 if (of_find_property(nc, "spi-cs-high", NULL))
1547 spi->mode |= SPI_CS_HIGH;
1548 if (of_find_property(nc, "spi-3wire", NULL))
1549 spi->mode |= SPI_3WIRE;
1550 if (of_find_property(nc, "spi-lsb-first", NULL))
1551 spi->mode |= SPI_LSB_FIRST;
1552
1553 /* Device DUAL/QUAD mode */
1554 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1555 switch (value) {
1556 case 1:
1557 break;
1558 case 2:
1559 spi->mode |= SPI_TX_DUAL;
1560 break;
1561 case 4:
1562 spi->mode |= SPI_TX_QUAD;
1563 break;
1564 default:
1565 dev_warn(&master->dev,
1566 "spi-tx-bus-width %d not supported\n",
1567 value);
1568 break;
1569 }
1570 }
1571
1572 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1573 switch (value) {
1574 case 1:
1575 break;
1576 case 2:
1577 spi->mode |= SPI_RX_DUAL;
1578 break;
1579 case 4:
1580 spi->mode |= SPI_RX_QUAD;
1581 break;
1582 default:
1583 dev_warn(&master->dev,
1584 "spi-rx-bus-width %d not supported\n",
1585 value);
1586 break;
1587 }
1588 }
1589
1590 /* Device speed */
1591 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1592 if (rc) {
1593 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1594 nc->full_name, rc);
1595 goto err_out;
1596 }
1597 spi->max_speed_hz = value;
1598
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001599 /* Store a pointer to the node in the device structure */
1600 of_node_get(nc);
1601 spi->dev.of_node = nc;
1602
1603 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001604 rc = spi_add_device(spi);
1605 if (rc) {
1606 dev_err(&master->dev, "spi_device register error %s\n",
1607 nc->full_name);
Johan Hovold83241472017-01-30 17:47:05 +01001608 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001609 }
1610
1611 return spi;
1612
Johan Hovold83241472017-01-30 17:47:05 +01001613err_of_node_put:
1614 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001615err_out:
1616 spi_dev_put(spi);
1617 return ERR_PTR(rc);
1618}
1619
Grant Likelyd57a4282012-04-07 14:16:53 -06001620/**
1621 * of_register_spi_devices() - Register child devices onto the SPI bus
1622 * @master: Pointer to spi_master device
1623 *
1624 * Registers an spi_device for each child node of master node which has a 'reg'
1625 * property.
1626 */
1627static void of_register_spi_devices(struct spi_master *master)
1628{
1629 struct spi_device *spi;
1630 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001631
1632 if (!master->dev.of_node)
1633 return;
1634
Alexander Sverdlinf3b61592012-11-29 08:59:29 +01001635 for_each_available_child_of_node(master->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01001636 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1637 continue;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001638 spi = of_register_spi_device(master, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001639 if (IS_ERR(spi)) {
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001640 dev_warn(&master->dev, "Failed to create SPI device for %s\n",
Grant Likelyd57a4282012-04-07 14:16:53 -06001641 nc->full_name);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001642 of_node_clear_flag(nc, OF_POPULATED);
1643 }
Grant Likelyd57a4282012-04-07 14:16:53 -06001644 }
1645}
1646#else
1647static void of_register_spi_devices(struct spi_master *master) { }
1648#endif
1649
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001650#ifdef CONFIG_ACPI
1651static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1652{
1653 struct spi_device *spi = data;
Mika Westerberga0a90712016-02-08 17:14:28 +02001654 struct spi_master *master = spi->master;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001655
1656 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1657 struct acpi_resource_spi_serialbus *sb;
1658
1659 sb = &ares->data.spi_serial_bus;
1660 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Mika Westerberga0a90712016-02-08 17:14:28 +02001661 /*
1662 * ACPI DeviceSelection numbering is handled by the
1663 * host controller driver in Windows and can vary
1664 * from driver to driver. In Linux we always expect
1665 * 0 .. max - 1 so we need to ask the driver to
1666 * translate between the two schemes.
1667 */
1668 if (master->fw_translate_cs) {
1669 int cs = master->fw_translate_cs(master,
1670 sb->device_selection);
1671 if (cs < 0)
1672 return cs;
1673 spi->chip_select = cs;
1674 } else {
1675 spi->chip_select = sb->device_selection;
1676 }
1677
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001678 spi->max_speed_hz = sb->connection_speed;
1679
1680 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1681 spi->mode |= SPI_CPHA;
1682 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1683 spi->mode |= SPI_CPOL;
1684 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1685 spi->mode |= SPI_CS_HIGH;
1686 }
1687 } else if (spi->irq < 0) {
1688 struct resource r;
1689
1690 if (acpi_dev_resource_interrupt(ares, 0, &r))
1691 spi->irq = r.start;
1692 }
1693
1694 /* Always tell the ACPI core to skip this resource */
1695 return 1;
1696}
1697
Octavian Purdila7f244672016-07-08 19:13:11 +03001698static acpi_status acpi_register_spi_device(struct spi_master *master,
1699 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001700{
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001701 struct list_head resource_list;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001702 struct spi_device *spi;
1703 int ret;
1704
Octavian Purdila7f244672016-07-08 19:13:11 +03001705 if (acpi_bus_get_status(adev) || !adev->status.present ||
1706 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001707 return AE_OK;
1708
1709 spi = spi_alloc_device(master);
1710 if (!spi) {
1711 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1712 dev_name(&adev->dev));
1713 return AE_NO_MEMORY;
1714 }
1715
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001716 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001717 spi->irq = -1;
1718
1719 INIT_LIST_HEAD(&resource_list);
1720 ret = acpi_dev_get_resources(adev, &resource_list,
1721 acpi_spi_add_resource, spi);
1722 acpi_dev_free_resource_list(&resource_list);
1723
1724 if (ret < 0 || !spi->max_speed_hz) {
1725 spi_dev_put(spi);
1726 return AE_OK;
1727 }
1728
Christophe RICARD33ada672015-12-23 23:25:35 +01001729 if (spi->irq < 0)
1730 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1731
Octavian Purdila7f244672016-07-08 19:13:11 +03001732 acpi_device_set_enumerated(adev);
1733
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001734 adev->power.flags.ignore_parent = true;
Jarkko Nikulacf9eb392013-10-10 17:19:17 +03001735 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001736 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001737 adev->power.flags.ignore_parent = false;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001738 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1739 dev_name(&adev->dev));
1740 spi_dev_put(spi);
1741 }
1742
1743 return AE_OK;
1744}
1745
Octavian Purdila7f244672016-07-08 19:13:11 +03001746static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1747 void *data, void **return_value)
1748{
1749 struct spi_master *master = data;
1750 struct acpi_device *adev;
1751
1752 if (acpi_bus_get_device(handle, &adev))
1753 return AE_OK;
1754
1755 return acpi_register_spi_device(master, adev);
1756}
1757
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001758static void acpi_register_spi_devices(struct spi_master *master)
1759{
1760 acpi_status status;
1761 acpi_handle handle;
1762
Rafael J. Wysocki29896172013-04-01 00:21:08 +00001763 handle = ACPI_HANDLE(master->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001764 if (!handle)
1765 return;
1766
1767 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1768 acpi_spi_add_device, NULL,
1769 master, NULL);
1770 if (ACPI_FAILURE(status))
1771 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1772}
1773#else
1774static inline void acpi_register_spi_devices(struct spi_master *master) {}
1775#endif /* CONFIG_ACPI */
1776
Tony Jones49dce682007-10-16 01:27:48 -07001777static void spi_master_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001778{
1779 struct spi_master *master;
1780
Tony Jones49dce682007-10-16 01:27:48 -07001781 master = container_of(dev, struct spi_master, dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001782 kfree(master);
1783}
1784
1785static struct class spi_master_class = {
1786 .name = "spi_master",
1787 .owner = THIS_MODULE,
Tony Jones49dce682007-10-16 01:27:48 -07001788 .dev_release = spi_master_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00001789 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08001790};
1791
1792
1793/**
1794 * spi_alloc_master - allocate SPI master controller
1795 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001796 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001797 * memory is in the driver_data field of the returned device,
David Brownell0c868462006-01-08 13:34:25 -08001798 * accessible with spi_master_get_devdata().
David Brownell33e34dc2007-05-08 00:32:21 -07001799 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001800 *
1801 * This call is used only by SPI master controller drivers, which are the
1802 * only ones directly touching chip registers. It's how they allocate
dmitry pervushinba1a0512006-05-20 15:00:14 -07001803 * an spi_master structure, prior to calling spi_register_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001804 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001805 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08001806 *
1807 * The caller is responsible for assigning the bus number and initializing
dmitry pervushinba1a0512006-05-20 15:00:14 -07001808 * the master's methods before calling spi_register_master(); and (after errors
Guenter Roecka394d632015-09-06 01:46:54 +03001809 * adding the device) calling spi_master_put() to prevent a memory leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001810 *
1811 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08001812 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001813struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
David Brownell8ae12a02006-01-08 13:34:19 -08001814{
1815 struct spi_master *master;
1816
David Brownell0c868462006-01-08 13:34:25 -08001817 if (!dev)
1818 return NULL;
1819
Jingoo Han5fe5f052013-10-14 10:31:51 +09001820 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -08001821 if (!master)
1822 return NULL;
1823
Tony Jones49dce682007-10-16 01:27:48 -07001824 device_initialize(&master->dev);
Grant Likely1e8a52e2012-05-19 23:42:08 -06001825 master->bus_num = -1;
1826 master->num_chipselect = 1;
Tony Jones49dce682007-10-16 01:27:48 -07001827 master->dev.class = &spi_master_class;
Johan Hovold157f38f2015-12-14 16:16:19 +01001828 master->dev.parent = dev;
Linus Walleijd7e2ee22016-04-11 13:51:03 +02001829 pm_suspend_ignore_children(&master->dev, true);
David Brownell0c868462006-01-08 13:34:25 -08001830 spi_master_set_devdata(master, &master[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001831
1832 return master;
1833}
1834EXPORT_SYMBOL_GPL(spi_alloc_master);
1835
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001836#ifdef CONFIG_OF
1837static int of_spi_register_master(struct spi_master *master)
1838{
Grant Likelye80beb22013-02-12 17:48:37 +00001839 int nb, i, *cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001840 struct device_node *np = master->dev.of_node;
1841
1842 if (!np)
1843 return 0;
1844
1845 nb = of_gpio_named_count(np, "cs-gpios");
Jingoo Han5fe5f052013-10-14 10:31:51 +09001846 master->num_chipselect = max_t(int, nb, master->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001847
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001848 /* Return error only for an incorrectly formed cs-gpios property */
1849 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001850 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001851 else if (nb < 0)
1852 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001853
1854 cs = devm_kzalloc(&master->dev,
1855 sizeof(int) * master->num_chipselect,
1856 GFP_KERNEL);
1857 master->cs_gpios = cs;
1858
1859 if (!master->cs_gpios)
1860 return -ENOMEM;
1861
Andreas Larsson0da83bb2013-01-29 15:53:40 +01001862 for (i = 0; i < master->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01001863 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001864
1865 for (i = 0; i < nb; i++)
1866 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1867
1868 return 0;
1869}
1870#else
1871static int of_spi_register_master(struct spi_master *master)
1872{
1873 return 0;
1874}
1875#endif
1876
David Brownell8ae12a02006-01-08 13:34:19 -08001877/**
1878 * spi_register_master - register SPI master controller
1879 * @master: initialized master, originally from spi_alloc_master()
David Brownell33e34dc2007-05-08 00:32:21 -07001880 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001881 *
1882 * SPI master controllers connect to their drivers using some non-SPI bus,
1883 * such as the platform bus. The final stage of probe() in that code
1884 * includes calling spi_register_master() to hook up to this SPI bus glue.
1885 *
1886 * SPI controllers use board specific (often SOC specific) bus numbers,
1887 * and board-specific addressing for SPI devices combines those numbers
1888 * with chip select numbers. Since SPI does not directly support dynamic
1889 * device identification, boards need configuration tables telling which
1890 * chip is at which address.
1891 *
1892 * This must be called from context that can sleep. It returns zero on
1893 * success, else a negative error code (dropping the master's refcount).
David Brownell0c868462006-01-08 13:34:25 -08001894 * After a successful return, the caller is responsible for calling
1895 * spi_unregister_master().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001896 *
1897 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08001898 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001899int spi_register_master(struct spi_master *master)
David Brownell8ae12a02006-01-08 13:34:19 -08001900{
David Brownelle44a45a2007-06-03 13:50:40 -07001901 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Tony Jones49dce682007-10-16 01:27:48 -07001902 struct device *dev = master->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08001903 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08001904 int status = -ENODEV;
1905 int dynamic = 0;
1906
David Brownell0c868462006-01-08 13:34:25 -08001907 if (!dev)
1908 return -ENODEV;
1909
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001910 status = of_spi_register_master(master);
1911 if (status)
1912 return status;
1913
David Brownell082c8cb2007-07-31 00:39:45 -07001914 /* even if it's just one always-selected device, there must
1915 * be at least one chipselect
1916 */
1917 if (master->num_chipselect == 0)
1918 return -EINVAL;
1919
Grant Likelybb297852012-12-21 19:32:09 +00001920 if ((master->bus_num < 0) && master->dev.of_node)
1921 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1922
David Brownell8ae12a02006-01-08 13:34:19 -08001923 /* convention: dynamically assigned bus IDs count down from the max */
David Brownella020ed72006-04-03 15:49:04 -07001924 if (master->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07001925 /* FIXME switch to an IDR based scheme, something like
1926 * I2C now uses, so we can't run out of "dynamic" IDs
1927 */
David Brownell8ae12a02006-01-08 13:34:19 -08001928 master->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08001929 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08001930 }
1931
Mark Brown5424d432014-12-10 17:40:53 +00001932 INIT_LIST_HEAD(&master->queue);
1933 spin_lock_init(&master->queue_lock);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07001934 spin_lock_init(&master->bus_lock_spinlock);
1935 mutex_init(&master->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001936 mutex_init(&master->io_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07001937 master->bus_lock_flag = 0;
Mark Brownb1589352013-10-05 11:50:40 +01001938 init_completion(&master->xfer_completion);
Mark Brown6ad45a22014-02-02 13:47:47 +00001939 if (!master->max_dma_len)
1940 master->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07001941
David Brownell8ae12a02006-01-08 13:34:19 -08001942 /* register the device, then userspace will see it.
1943 * registration fails if the bus ID is in use.
1944 */
Kay Sievers35f74fc2009-01-06 10:44:37 -08001945 dev_set_name(&master->dev, "spi%u", master->bus_num);
Tony Jones49dce682007-10-16 01:27:48 -07001946 status = device_add(&master->dev);
David Brownellb8852442006-01-08 13:34:23 -08001947 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08001948 goto done;
Kay Sievers35f74fc2009-01-06 10:44:37 -08001949 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
David Brownell8ae12a02006-01-08 13:34:19 -08001950 dynamic ? " (dynamic)" : "");
1951
Linus Walleijffbbdd212012-02-22 10:05:38 +01001952 /* If we're using a queued driver, start the queue */
1953 if (master->transfer)
1954 dev_info(dev, "master is unqueued, this is deprecated\n");
1955 else {
1956 status = spi_master_initialize_queue(master);
1957 if (status) {
Axel Line93b0722013-08-31 20:25:52 +08001958 device_del(&master->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001959 goto done;
1960 }
1961 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00001962 /* add statistics */
1963 spin_lock_init(&master->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001964
Feng Tang2b9603a2010-08-02 15:52:15 +08001965 mutex_lock(&board_lock);
1966 list_add_tail(&master->list, &spi_master_list);
1967 list_for_each_entry(bi, &board_list, list)
1968 spi_match_master_to_boardinfo(master, &bi->board_info);
1969 mutex_unlock(&board_lock);
1970
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001971 /* Register devices from the device tree and ACPI */
Anatolij Gustschin12b15e82010-07-27 22:35:58 +02001972 of_register_spi_devices(master);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001973 acpi_register_spi_devices(master);
David Brownell8ae12a02006-01-08 13:34:19 -08001974done:
1975 return status;
1976}
1977EXPORT_SYMBOL_GPL(spi_register_master);
1978
Mark Brown666d5b42013-08-31 18:50:52 +01001979static void devm_spi_unregister(struct device *dev, void *res)
1980{
1981 spi_unregister_master(*(struct spi_master **)res);
1982}
1983
1984/**
1985 * dev_spi_register_master - register managed SPI master controller
1986 * @dev: device managing SPI master
1987 * @master: initialized master, originally from spi_alloc_master()
1988 * Context: can sleep
1989 *
1990 * Register a SPI device as with spi_register_master() which will
1991 * automatically be unregister
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001992 *
1993 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01001994 */
1995int devm_spi_register_master(struct device *dev, struct spi_master *master)
1996{
1997 struct spi_master **ptr;
1998 int ret;
1999
2000 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2001 if (!ptr)
2002 return -ENOMEM;
2003
2004 ret = spi_register_master(master);
Stephen Warren4b928942013-11-21 16:11:15 -07002005 if (!ret) {
Mark Brown666d5b42013-08-31 18:50:52 +01002006 *ptr = master;
2007 devres_add(dev, ptr);
2008 } else {
2009 devres_free(ptr);
2010 }
2011
2012 return ret;
2013}
2014EXPORT_SYMBOL_GPL(devm_spi_register_master);
2015
David Lamparter34860082010-08-30 23:54:17 +02002016static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002017{
David Lamparter34860082010-08-30 23:54:17 +02002018 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002019 return 0;
2020}
2021
2022/**
2023 * spi_unregister_master - unregister SPI master controller
2024 * @master: the master being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002025 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002026 *
2027 * This call is used only by SPI master controller drivers, which are the
2028 * only ones directly touching chip registers.
2029 *
2030 * This must be called from context that can sleep.
2031 */
2032void spi_unregister_master(struct spi_master *master)
2033{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002034 int dummy;
2035
Linus Walleijffbbdd212012-02-22 10:05:38 +01002036 if (master->queued) {
2037 if (spi_destroy_queue(master))
2038 dev_err(&master->dev, "queue remove failed\n");
2039 }
2040
Feng Tang2b9603a2010-08-02 15:52:15 +08002041 mutex_lock(&board_lock);
2042 list_del(&master->list);
2043 mutex_unlock(&board_lock);
2044
Sebastian Andrzej Siewior97dbf372010-12-21 17:24:31 -08002045 dummy = device_for_each_child(&master->dev, NULL, __unregister);
Tony Jones49dce682007-10-16 01:27:48 -07002046 device_unregister(&master->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08002047}
2048EXPORT_SYMBOL_GPL(spi_unregister_master);
2049
Linus Walleijffbbdd212012-02-22 10:05:38 +01002050int spi_master_suspend(struct spi_master *master)
2051{
2052 int ret;
2053
2054 /* Basically no-ops for non-queued masters */
2055 if (!master->queued)
2056 return 0;
2057
2058 ret = spi_stop_queue(master);
2059 if (ret)
2060 dev_err(&master->dev, "queue stop failed\n");
2061
2062 return ret;
2063}
2064EXPORT_SYMBOL_GPL(spi_master_suspend);
2065
2066int spi_master_resume(struct spi_master *master)
2067{
2068 int ret;
2069
2070 if (!master->queued)
2071 return 0;
2072
2073 ret = spi_start_queue(master);
2074 if (ret)
2075 dev_err(&master->dev, "queue restart failed\n");
2076
2077 return ret;
2078}
2079EXPORT_SYMBOL_GPL(spi_master_resume);
2080
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002081static int __spi_master_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002082{
2083 struct spi_master *m;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002084 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002085
2086 m = container_of(dev, struct spi_master, dev);
2087 return m->bus_num == *bus_num;
2088}
2089
David Brownell8ae12a02006-01-08 13:34:19 -08002090/**
2091 * spi_busnum_to_master - look up master associated with bus_num
2092 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002093 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002094 *
2095 * This call may be used with devices that are registered after
2096 * arch init time. It returns a refcounted pointer to the relevant
2097 * spi_master (which the caller must release), or NULL if there is
2098 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002099 *
2100 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002101 */
2102struct spi_master *spi_busnum_to_master(u16 bus_num)
2103{
Tony Jones49dce682007-10-16 01:27:48 -07002104 struct device *dev;
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08002105 struct spi_master *master = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002106
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002107 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Dave Young5ed2c832008-01-22 15:14:18 +08002108 __spi_master_match);
2109 if (dev)
2110 master = container_of(dev, struct spi_master, dev);
2111 /* reference got in class_find_device */
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08002112 return master;
David Brownell8ae12a02006-01-08 13:34:19 -08002113}
2114EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2115
Martin Sperld780c372015-12-14 15:20:18 +00002116/*-------------------------------------------------------------------------*/
2117
2118/* Core methods for SPI resource management */
2119
2120/**
2121 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2122 * during the processing of a spi_message while using
2123 * spi_transfer_one
2124 * @spi: the spi device for which we allocate memory
2125 * @release: the release code to execute for this resource
2126 * @size: size to alloc and return
2127 * @gfp: GFP allocation flags
2128 *
2129 * Return: the pointer to the allocated data
2130 *
2131 * This may get enhanced in the future to allocate from a memory pool
2132 * of the @spi_device or @spi_master to avoid repeated allocations.
2133 */
2134void *spi_res_alloc(struct spi_device *spi,
2135 spi_res_release_t release,
2136 size_t size, gfp_t gfp)
2137{
2138 struct spi_res *sres;
2139
2140 sres = kzalloc(sizeof(*sres) + size, gfp);
2141 if (!sres)
2142 return NULL;
2143
2144 INIT_LIST_HEAD(&sres->entry);
2145 sres->release = release;
2146
2147 return sres->data;
2148}
2149EXPORT_SYMBOL_GPL(spi_res_alloc);
2150
2151/**
2152 * spi_res_free - free an spi resource
2153 * @res: pointer to the custom data of a resource
2154 *
2155 */
2156void spi_res_free(void *res)
2157{
2158 struct spi_res *sres = container_of(res, struct spi_res, data);
2159
2160 if (!res)
2161 return;
2162
2163 WARN_ON(!list_empty(&sres->entry));
2164 kfree(sres);
2165}
2166EXPORT_SYMBOL_GPL(spi_res_free);
2167
2168/**
2169 * spi_res_add - add a spi_res to the spi_message
2170 * @message: the spi message
2171 * @res: the spi_resource
2172 */
2173void spi_res_add(struct spi_message *message, void *res)
2174{
2175 struct spi_res *sres = container_of(res, struct spi_res, data);
2176
2177 WARN_ON(!list_empty(&sres->entry));
2178 list_add_tail(&sres->entry, &message->resources);
2179}
2180EXPORT_SYMBOL_GPL(spi_res_add);
2181
2182/**
2183 * spi_res_release - release all spi resources for this message
2184 * @master: the @spi_master
2185 * @message: the @spi_message
2186 */
2187void spi_res_release(struct spi_master *master,
2188 struct spi_message *message)
2189{
2190 struct spi_res *res;
2191
2192 while (!list_empty(&message->resources)) {
2193 res = list_last_entry(&message->resources,
2194 struct spi_res, entry);
2195
2196 if (res->release)
2197 res->release(master, message, res->data);
2198
2199 list_del(&res->entry);
2200
2201 kfree(res);
2202 }
2203}
2204EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002205
2206/*-------------------------------------------------------------------------*/
2207
Martin Sperl523baf5a2015-12-14 15:20:19 +00002208/* Core methods for spi_message alterations */
2209
2210static void __spi_replace_transfers_release(struct spi_master *master,
2211 struct spi_message *msg,
2212 void *res)
2213{
2214 struct spi_replaced_transfers *rxfer = res;
2215 size_t i;
2216
2217 /* call extra callback if requested */
2218 if (rxfer->release)
2219 rxfer->release(master, msg, res);
2220
2221 /* insert replaced transfers back into the message */
2222 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2223
2224 /* remove the formerly inserted entries */
2225 for (i = 0; i < rxfer->inserted; i++)
2226 list_del(&rxfer->inserted_transfers[i].transfer_list);
2227}
2228
2229/**
2230 * spi_replace_transfers - replace transfers with several transfers
2231 * and register change with spi_message.resources
2232 * @msg: the spi_message we work upon
2233 * @xfer_first: the first spi_transfer we want to replace
2234 * @remove: number of transfers to remove
2235 * @insert: the number of transfers we want to insert instead
2236 * @release: extra release code necessary in some circumstances
2237 * @extradatasize: extra data to allocate (with alignment guarantees
2238 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002239 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002240 *
2241 * Returns: pointer to @spi_replaced_transfers,
2242 * PTR_ERR(...) in case of errors.
2243 */
2244struct spi_replaced_transfers *spi_replace_transfers(
2245 struct spi_message *msg,
2246 struct spi_transfer *xfer_first,
2247 size_t remove,
2248 size_t insert,
2249 spi_replaced_release_t release,
2250 size_t extradatasize,
2251 gfp_t gfp)
2252{
2253 struct spi_replaced_transfers *rxfer;
2254 struct spi_transfer *xfer;
2255 size_t i;
2256
2257 /* allocate the structure using spi_res */
2258 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2259 insert * sizeof(struct spi_transfer)
2260 + sizeof(struct spi_replaced_transfers)
2261 + extradatasize,
2262 gfp);
2263 if (!rxfer)
2264 return ERR_PTR(-ENOMEM);
2265
2266 /* the release code to invoke before running the generic release */
2267 rxfer->release = release;
2268
2269 /* assign extradata */
2270 if (extradatasize)
2271 rxfer->extradata =
2272 &rxfer->inserted_transfers[insert];
2273
2274 /* init the replaced_transfers list */
2275 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2276
2277 /* assign the list_entry after which we should reinsert
2278 * the @replaced_transfers - it may be spi_message.messages!
2279 */
2280 rxfer->replaced_after = xfer_first->transfer_list.prev;
2281
2282 /* remove the requested number of transfers */
2283 for (i = 0; i < remove; i++) {
2284 /* if the entry after replaced_after it is msg->transfers
2285 * then we have been requested to remove more transfers
2286 * than are in the list
2287 */
2288 if (rxfer->replaced_after->next == &msg->transfers) {
2289 dev_err(&msg->spi->dev,
2290 "requested to remove more spi_transfers than are available\n");
2291 /* insert replaced transfers back into the message */
2292 list_splice(&rxfer->replaced_transfers,
2293 rxfer->replaced_after);
2294
2295 /* free the spi_replace_transfer structure */
2296 spi_res_free(rxfer);
2297
2298 /* and return with an error */
2299 return ERR_PTR(-EINVAL);
2300 }
2301
2302 /* remove the entry after replaced_after from list of
2303 * transfers and add it to list of replaced_transfers
2304 */
2305 list_move_tail(rxfer->replaced_after->next,
2306 &rxfer->replaced_transfers);
2307 }
2308
2309 /* create copy of the given xfer with identical settings
2310 * based on the first transfer to get removed
2311 */
2312 for (i = 0; i < insert; i++) {
2313 /* we need to run in reverse order */
2314 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2315
2316 /* copy all spi_transfer data */
2317 memcpy(xfer, xfer_first, sizeof(*xfer));
2318
2319 /* add to list */
2320 list_add(&xfer->transfer_list, rxfer->replaced_after);
2321
2322 /* clear cs_change and delay_usecs for all but the last */
2323 if (i) {
2324 xfer->cs_change = false;
2325 xfer->delay_usecs = 0;
2326 }
2327 }
2328
2329 /* set up inserted */
2330 rxfer->inserted = insert;
2331
2332 /* and register it with spi_res/spi_message */
2333 spi_res_add(msg, rxfer);
2334
2335 return rxfer;
2336}
2337EXPORT_SYMBOL_GPL(spi_replace_transfers);
2338
Fabio Estevam08933412016-02-14 13:33:50 -02002339static int __spi_split_transfer_maxsize(struct spi_master *master,
2340 struct spi_message *msg,
2341 struct spi_transfer **xferp,
2342 size_t maxsize,
2343 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00002344{
2345 struct spi_transfer *xfer = *xferp, *xfers;
2346 struct spi_replaced_transfers *srt;
2347 size_t offset;
2348 size_t count, i;
2349
2350 /* warn once about this fact that we are splitting a transfer */
2351 dev_warn_once(&msg->spi->dev,
Fabio Estevam7d62f512016-02-17 15:42:27 -02002352 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
Martin Sperld9f12122015-12-14 15:20:20 +00002353 xfer->len, maxsize);
2354
2355 /* calculate how many we have to replace */
2356 count = DIV_ROUND_UP(xfer->len, maxsize);
2357
2358 /* create replacement */
2359 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03002360 if (IS_ERR(srt))
2361 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00002362 xfers = srt->inserted_transfers;
2363
2364 /* now handle each of those newly inserted spi_transfers
2365 * note that the replacements spi_transfers all are preset
2366 * to the same values as *xferp, so tx_buf, rx_buf and len
2367 * are all identical (as well as most others)
2368 * so we just have to fix up len and the pointers.
2369 *
2370 * this also includes support for the depreciated
2371 * spi_message.is_dma_mapped interface
2372 */
2373
2374 /* the first transfer just needs the length modified, so we
2375 * run it outside the loop
2376 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02002377 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00002378
2379 /* all the others need rx_buf/tx_buf also set */
2380 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2381 /* update rx_buf, tx_buf and dma */
2382 if (xfers[i].rx_buf)
2383 xfers[i].rx_buf += offset;
2384 if (xfers[i].rx_dma)
2385 xfers[i].rx_dma += offset;
2386 if (xfers[i].tx_buf)
2387 xfers[i].tx_buf += offset;
2388 if (xfers[i].tx_dma)
2389 xfers[i].tx_dma += offset;
2390
2391 /* update length */
2392 xfers[i].len = min(maxsize, xfers[i].len - offset);
2393 }
2394
2395 /* we set up xferp to the last entry we have inserted,
2396 * so that we skip those already split transfers
2397 */
2398 *xferp = &xfers[count - 1];
2399
2400 /* increment statistics counters */
2401 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2402 transfers_split_maxsize);
2403 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2404 transfers_split_maxsize);
2405
2406 return 0;
2407}
2408
2409/**
2410 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2411 * when an individual transfer exceeds a
2412 * certain size
2413 * @master: the @spi_master for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09002414 * @msg: the @spi_message to transform
2415 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03002416 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00002417 *
2418 * Return: status of transformation
2419 */
2420int spi_split_transfers_maxsize(struct spi_master *master,
2421 struct spi_message *msg,
2422 size_t maxsize,
2423 gfp_t gfp)
2424{
2425 struct spi_transfer *xfer;
2426 int ret;
2427
2428 /* iterate over the transfer_list,
2429 * but note that xfer is advanced to the last transfer inserted
2430 * to avoid checking sizes again unnecessarily (also xfer does
2431 * potentiall belong to a different list by the time the
2432 * replacement has happened
2433 */
2434 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2435 if (xfer->len > maxsize) {
2436 ret = __spi_split_transfer_maxsize(
2437 master, msg, &xfer, maxsize, gfp);
2438 if (ret)
2439 return ret;
2440 }
2441 }
2442
2443 return 0;
2444}
2445EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08002446
2447/*-------------------------------------------------------------------------*/
2448
David Brownell7d077192009-06-17 16:26:03 -07002449/* Core methods for SPI master protocol drivers. Some of the
2450 * other core methods are currently defined as inline functions.
2451 */
2452
Stefan Brüns63ab6452015-08-23 16:06:30 +02002453static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
2454{
2455 if (master->bits_per_word_mask) {
2456 /* Only 32 bits fit in the mask */
2457 if (bits_per_word > 32)
2458 return -EINVAL;
2459 if (!(master->bits_per_word_mask &
2460 SPI_BPW_MASK(bits_per_word)))
2461 return -EINVAL;
2462 }
2463
2464 return 0;
2465}
2466
David Brownell7d077192009-06-17 16:26:03 -07002467/**
2468 * spi_setup - setup SPI mode and clock rate
2469 * @spi: the device whose settings are being modified
2470 * Context: can sleep, and no requests are queued to the device
2471 *
2472 * SPI protocol drivers may need to update the transfer mode if the
2473 * device doesn't work with its default. They may likewise need
2474 * to update clock rates or word sizes from initial values. This function
2475 * changes those settings, and must be called from a context that can sleep.
2476 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
2477 * effect the next time the device is selected and data is transferred to
2478 * or from it. When this function returns, the spi device is deselected.
2479 *
2480 * Note that this call will fail if the protocol driver specifies an option
2481 * that the underlying controller or its driver does not support. For
2482 * example, not all hardware supports wire transfers using nine bit words,
2483 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002484 *
2485 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07002486 */
2487int spi_setup(struct spi_device *spi)
2488{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002489 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002490 int status;
David Brownell7d077192009-06-17 16:26:03 -07002491
wangyuhangf477b7f2013-08-11 18:15:17 +08002492 /* check mode to prevent that DUAL and QUAD set at the same time
2493 */
2494 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
2495 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
2496 dev_err(&spi->dev,
2497 "setup: can not select dual and quad at the same time\n");
2498 return -EINVAL;
2499 }
2500 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
2501 */
2502 if ((spi->mode & SPI_3WIRE) && (spi->mode &
2503 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
2504 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07002505 /* help drivers fail *cleanly* when they need options
2506 * that aren't supported with their current master
2507 */
2508 bad_bits = spi->mode & ~spi->master->mode_bits;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002509 ugly_bits = bad_bits &
2510 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
2511 if (ugly_bits) {
2512 dev_warn(&spi->dev,
2513 "setup: ignoring unsupported mode bits %x\n",
2514 ugly_bits);
2515 spi->mode &= ~ugly_bits;
2516 bad_bits &= ~ugly_bits;
2517 }
David Brownelle7db06b2009-06-17 16:26:04 -07002518 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02002519 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07002520 bad_bits);
2521 return -EINVAL;
2522 }
2523
David Brownell7d077192009-06-17 16:26:03 -07002524 if (!spi->bits_per_word)
2525 spi->bits_per_word = 8;
2526
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002527 status = __spi_validate_bits_per_word(spi->master, spi->bits_per_word);
2528 if (status)
2529 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02002530
Axel Lin052eb2d2014-02-10 00:08:05 +08002531 if (!spi->max_speed_hz)
2532 spi->max_speed_hz = spi->master->max_speed_hz;
2533
Laxman Dewangancaae0702012-11-09 14:35:22 +05302534 if (spi->master->setup)
2535 status = spi->master->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07002536
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05002537 spi_set_cs(spi, false);
2538
Jingoo Han5fe5f052013-10-14 10:31:51 +09002539 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 -07002540 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2541 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2542 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2543 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2544 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2545 spi->bits_per_word, spi->max_speed_hz,
2546 status);
2547
2548 return status;
2549}
2550EXPORT_SYMBOL_GPL(spi_setup);
2551
Mark Brown90808732013-11-13 23:44:15 +00002552static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002553{
2554 struct spi_master *master = spi->master;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302555 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002556 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002557
Mark Brown24a00132013-07-10 15:05:40 +01002558 if (list_empty(&message->transfers))
2559 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01002560
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002561 /* Half-duplex links include original MicroWire, and ones with
2562 * only one data pin like SPI_3WIRE (switches direction) or where
2563 * either MOSI or MISO is missing. They can also be caused by
2564 * software limitations.
2565 */
2566 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
2567 || (spi->mode & SPI_3WIRE)) {
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002568 unsigned flags = master->flags;
2569
2570 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2571 if (xfer->rx_buf && xfer->tx_buf)
2572 return -EINVAL;
2573 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
2574 return -EINVAL;
2575 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
2576 return -EINVAL;
2577 }
2578 }
2579
Laxman Dewangane6811d12012-11-09 14:36:45 +05302580 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302581 * Set transfer bits_per_word and max speed as spi device default if
2582 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08002583 * Set transfer tx_nbits and rx_nbits as single transfer default
2584 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05302585 */
Martin Sperl77e80582015-11-27 12:31:09 +00002586 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302587 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05302588 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302589 if (!xfer->bits_per_word)
2590 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08002591
2592 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302593 xfer->speed_hz = spi->max_speed_hz;
Mark Brown7dc9fbc2015-08-20 11:52:18 -07002594 if (!xfer->speed_hz)
2595 xfer->speed_hz = master->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08002596
2597 if (master->max_speed_hz &&
2598 xfer->speed_hz > master->max_speed_hz)
2599 xfer->speed_hz = master->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02002600
Stefan Brüns63ab6452015-08-23 16:06:30 +02002601 if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
2602 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01002603
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002604 /*
2605 * SPI transfer length should be multiple of SPI word size
2606 * where SPI word size should be power-of-two multiple
2607 */
2608 if (xfer->bits_per_word <= 8)
2609 w_size = 1;
2610 else if (xfer->bits_per_word <= 16)
2611 w_size = 2;
2612 else
2613 w_size = 4;
2614
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002615 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002616 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002617 return -EINVAL;
2618
Mark Browna2fd4f92013-07-10 14:57:26 +01002619 if (xfer->speed_hz && master->min_speed_hz &&
2620 xfer->speed_hz < master->min_speed_hz)
2621 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08002622
2623 if (xfer->tx_buf && !xfer->tx_nbits)
2624 xfer->tx_nbits = SPI_NBITS_SINGLE;
2625 if (xfer->rx_buf && !xfer->rx_nbits)
2626 xfer->rx_nbits = SPI_NBITS_SINGLE;
2627 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01002628 * 1. check the value matches one of single, dual and quad
2629 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08002630 */
Sourav Poddardb90a442013-08-22 21:20:48 +05302631 if (xfer->tx_buf) {
2632 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2633 xfer->tx_nbits != SPI_NBITS_DUAL &&
2634 xfer->tx_nbits != SPI_NBITS_QUAD)
2635 return -EINVAL;
2636 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2637 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2638 return -EINVAL;
2639 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2640 !(spi->mode & SPI_TX_QUAD))
2641 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302642 }
wangyuhangf477b7f2013-08-11 18:15:17 +08002643 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05302644 if (xfer->rx_buf) {
2645 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2646 xfer->rx_nbits != SPI_NBITS_DUAL &&
2647 xfer->rx_nbits != SPI_NBITS_QUAD)
2648 return -EINVAL;
2649 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2650 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2651 return -EINVAL;
2652 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2653 !(spi->mode & SPI_RX_QUAD))
2654 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302655 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05302656 }
2657
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002658 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00002659
2660 return 0;
2661}
2662
2663static int __spi_async(struct spi_device *spi, struct spi_message *message)
2664{
2665 struct spi_master *master = spi->master;
2666
2667 message->spi = spi;
2668
Martin Sperleca2ebc2015-06-22 13:00:36 +00002669 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
2670 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2671
Mark Brown90808732013-11-13 23:44:15 +00002672 trace_spi_message_submit(message);
2673
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002674 return master->transfer(spi, message);
2675}
2676
David Brownell568d0692009-09-22 16:46:18 -07002677/**
2678 * spi_async - asynchronous SPI transfer
2679 * @spi: device with which data will be exchanged
2680 * @message: describes the data transfers, including completion callback
2681 * Context: any (irqs may be blocked, etc)
2682 *
2683 * This call may be used in_irq and other contexts which can't sleep,
2684 * as well as from task contexts which can sleep.
2685 *
2686 * The completion callback is invoked in a context which can't sleep.
2687 * Before that invocation, the value of message->status is undefined.
2688 * When the callback is issued, message->status holds either zero (to
2689 * indicate complete success) or a negative error code. After that
2690 * callback returns, the driver which issued the transfer request may
2691 * deallocate the associated memory; it's no longer in use by any SPI
2692 * core or controller driver code.
2693 *
2694 * Note that although all messages to a spi_device are handled in
2695 * FIFO order, messages may go to different devices in other orders.
2696 * Some device might be higher priority, or have various "hard" access
2697 * time requirements, for example.
2698 *
2699 * On detection of any fault during the transfer, processing of
2700 * the entire message is aborted, and the device is deselected.
2701 * Until returning from the associated message completion callback,
2702 * no other spi_message queued to that device will be processed.
2703 * (This rule applies equally to all the synchronous transfer calls,
2704 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002705 *
2706 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07002707 */
2708int spi_async(struct spi_device *spi, struct spi_message *message)
2709{
2710 struct spi_master *master = spi->master;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002711 int ret;
2712 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07002713
Mark Brown90808732013-11-13 23:44:15 +00002714 ret = __spi_validate(spi, message);
2715 if (ret != 0)
2716 return ret;
2717
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002718 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07002719
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002720 if (master->bus_lock_flag)
2721 ret = -EBUSY;
2722 else
2723 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07002724
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002725 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2726
2727 return ret;
David Brownell568d0692009-09-22 16:46:18 -07002728}
2729EXPORT_SYMBOL_GPL(spi_async);
2730
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002731/**
2732 * spi_async_locked - version of spi_async with exclusive bus usage
2733 * @spi: device with which data will be exchanged
2734 * @message: describes the data transfers, including completion callback
2735 * Context: any (irqs may be blocked, etc)
2736 *
2737 * This call may be used in_irq and other contexts which can't sleep,
2738 * as well as from task contexts which can sleep.
2739 *
2740 * The completion callback is invoked in a context which can't sleep.
2741 * Before that invocation, the value of message->status is undefined.
2742 * When the callback is issued, message->status holds either zero (to
2743 * indicate complete success) or a negative error code. After that
2744 * callback returns, the driver which issued the transfer request may
2745 * deallocate the associated memory; it's no longer in use by any SPI
2746 * core or controller driver code.
2747 *
2748 * Note that although all messages to a spi_device are handled in
2749 * FIFO order, messages may go to different devices in other orders.
2750 * Some device might be higher priority, or have various "hard" access
2751 * time requirements, for example.
2752 *
2753 * On detection of any fault during the transfer, processing of
2754 * the entire message is aborted, and the device is deselected.
2755 * Until returning from the associated message completion callback,
2756 * no other spi_message queued to that device will be processed.
2757 * (This rule applies equally to all the synchronous transfer calls,
2758 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002759 *
2760 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002761 */
2762int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2763{
2764 struct spi_master *master = spi->master;
2765 int ret;
2766 unsigned long flags;
2767
Mark Brown90808732013-11-13 23:44:15 +00002768 ret = __spi_validate(spi, message);
2769 if (ret != 0)
2770 return ret;
2771
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002772 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2773
2774 ret = __spi_async(spi, message);
2775
2776 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2777
2778 return ret;
2779
2780}
2781EXPORT_SYMBOL_GPL(spi_async_locked);
2782
David Brownell7d077192009-06-17 16:26:03 -07002783
Vignesh R556351f2015-12-11 09:39:56 +05302784int spi_flash_read(struct spi_device *spi,
2785 struct spi_flash_read_message *msg)
2786
2787{
2788 struct spi_master *master = spi->master;
Vignesh Rf4502dd2016-06-08 12:18:31 +05302789 struct device *rx_dev = NULL;
Vignesh R556351f2015-12-11 09:39:56 +05302790 int ret;
2791
2792 if ((msg->opcode_nbits == SPI_NBITS_DUAL ||
2793 msg->addr_nbits == SPI_NBITS_DUAL) &&
2794 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2795 return -EINVAL;
2796 if ((msg->opcode_nbits == SPI_NBITS_QUAD ||
2797 msg->addr_nbits == SPI_NBITS_QUAD) &&
2798 !(spi->mode & SPI_TX_QUAD))
2799 return -EINVAL;
2800 if (msg->data_nbits == SPI_NBITS_DUAL &&
2801 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2802 return -EINVAL;
2803 if (msg->data_nbits == SPI_NBITS_QUAD &&
2804 !(spi->mode & SPI_RX_QUAD))
2805 return -EINVAL;
2806
2807 if (master->auto_runtime_pm) {
2808 ret = pm_runtime_get_sync(master->dev.parent);
2809 if (ret < 0) {
2810 dev_err(&master->dev, "Failed to power device: %d\n",
2811 ret);
2812 return ret;
2813 }
2814 }
Vignesh Rf4502dd2016-06-08 12:18:31 +05302815
Vignesh R556351f2015-12-11 09:39:56 +05302816 mutex_lock(&master->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01002817 mutex_lock(&master->io_mutex);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302818 if (master->dma_rx) {
2819 rx_dev = master->dma_rx->device->dev;
2820 ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
2821 msg->buf, msg->len,
2822 DMA_FROM_DEVICE);
2823 if (!ret)
2824 msg->cur_msg_mapped = true;
2825 }
Vignesh R556351f2015-12-11 09:39:56 +05302826 ret = master->spi_flash_read(spi, msg);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302827 if (msg->cur_msg_mapped)
2828 spi_unmap_buf(master, rx_dev, &msg->rx_sg,
2829 DMA_FROM_DEVICE);
Mark Brownef4d96e2016-07-21 23:53:31 +01002830 mutex_unlock(&master->io_mutex);
Vignesh R556351f2015-12-11 09:39:56 +05302831 mutex_unlock(&master->bus_lock_mutex);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302832
Vignesh R556351f2015-12-11 09:39:56 +05302833 if (master->auto_runtime_pm)
2834 pm_runtime_put(master->dev.parent);
2835
2836 return ret;
2837}
2838EXPORT_SYMBOL_GPL(spi_flash_read);
2839
David Brownell7d077192009-06-17 16:26:03 -07002840/*-------------------------------------------------------------------------*/
2841
2842/* Utility methods for SPI master protocol drivers, layered on
2843 * top of the core. Some other utility methods are defined as
2844 * inline functions.
2845 */
2846
Andrew Morton5d870c82006-01-11 11:23:49 -08002847static void spi_complete(void *arg)
2848{
2849 complete(arg);
2850}
2851
Mark Brownef4d96e2016-07-21 23:53:31 +01002852static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002853{
2854 DECLARE_COMPLETION_ONSTACK(done);
2855 int status;
2856 struct spi_master *master = spi->master;
Mark Brown0461a412014-12-09 21:38:05 +00002857 unsigned long flags;
2858
2859 status = __spi_validate(spi, message);
2860 if (status != 0)
2861 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002862
2863 message->complete = spi_complete;
2864 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00002865 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002866
Martin Sperleca2ebc2015-06-22 13:00:36 +00002867 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
2868 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
2869
Mark Brown0461a412014-12-09 21:38:05 +00002870 /* If we're not using the legacy transfer method then we will
2871 * try to transfer in the calling context so special case.
2872 * This code would be less tricky if we could remove the
2873 * support for driver implemented message queues.
2874 */
2875 if (master->transfer == spi_queued_transfer) {
2876 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2877
2878 trace_spi_message_submit(message);
2879
2880 status = __spi_queued_transfer(spi, message, false);
2881
2882 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2883 } else {
2884 status = spi_async_locked(spi, message);
2885 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002886
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002887 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00002888 /* Push out the messages in the calling context if we
2889 * can.
2890 */
Martin Sperleca2ebc2015-06-22 13:00:36 +00002891 if (master->transfer == spi_queued_transfer) {
2892 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2893 spi_sync_immediate);
2894 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
2895 spi_sync_immediate);
Mark Brownef4d96e2016-07-21 23:53:31 +01002896 __spi_pump_messages(master, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00002897 }
Mark Brown0461a412014-12-09 21:38:05 +00002898
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002899 wait_for_completion(&done);
2900 status = message->status;
2901 }
2902 message->context = NULL;
2903 return status;
2904}
2905
David Brownell8ae12a02006-01-08 13:34:19 -08002906/**
2907 * spi_sync - blocking/synchronous SPI data transfers
2908 * @spi: device with which data will be exchanged
2909 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07002910 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002911 *
2912 * This call may only be used from a context that may sleep. The sleep
2913 * is non-interruptible, and has no timeout. Low-overhead controller
2914 * drivers may DMA directly into and out of the message buffers.
2915 *
2916 * Note that the SPI device's chip select is active during the message,
2917 * and then is normally disabled between messages. Drivers for some
2918 * frequently-used devices may want to minimize costs of selecting a chip,
2919 * by leaving it selected in anticipation that the next message will go
2920 * to the same chip. (That may increase power usage.)
2921 *
David Brownell0c868462006-01-08 13:34:25 -08002922 * Also, the caller is guaranteeing that the memory associated with the
2923 * message will not be freed before this call returns.
2924 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002925 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002926 */
2927int spi_sync(struct spi_device *spi, struct spi_message *message)
2928{
Mark Brownef4d96e2016-07-21 23:53:31 +01002929 int ret;
2930
2931 mutex_lock(&spi->master->bus_lock_mutex);
2932 ret = __spi_sync(spi, message);
2933 mutex_unlock(&spi->master->bus_lock_mutex);
2934
2935 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08002936}
2937EXPORT_SYMBOL_GPL(spi_sync);
2938
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002939/**
2940 * spi_sync_locked - version of spi_sync with exclusive bus usage
2941 * @spi: device with which data will be exchanged
2942 * @message: describes the data transfers
2943 * Context: can sleep
2944 *
2945 * This call may only be used from a context that may sleep. The sleep
2946 * is non-interruptible, and has no timeout. Low-overhead controller
2947 * drivers may DMA directly into and out of the message buffers.
2948 *
2949 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002950 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002951 * be released by a spi_bus_unlock call when the exclusive access is over.
2952 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002953 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002954 */
2955int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2956{
Mark Brownef4d96e2016-07-21 23:53:31 +01002957 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002958}
2959EXPORT_SYMBOL_GPL(spi_sync_locked);
2960
2961/**
2962 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
2963 * @master: SPI bus master that should be locked for exclusive bus access
2964 * Context: can sleep
2965 *
2966 * This call may only be used from a context that may sleep. The sleep
2967 * is non-interruptible, and has no timeout.
2968 *
2969 * This call should be used by drivers that require exclusive access to the
2970 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
2971 * exclusive access is over. Data transfer must be done by spi_sync_locked
2972 * and spi_async_locked calls when the SPI bus lock is held.
2973 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002974 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002975 */
2976int spi_bus_lock(struct spi_master *master)
2977{
2978 unsigned long flags;
2979
2980 mutex_lock(&master->bus_lock_mutex);
2981
2982 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2983 master->bus_lock_flag = 1;
2984 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2985
2986 /* mutex remains locked until spi_bus_unlock is called */
2987
2988 return 0;
2989}
2990EXPORT_SYMBOL_GPL(spi_bus_lock);
2991
2992/**
2993 * spi_bus_unlock - release the lock for exclusive SPI bus usage
2994 * @master: SPI bus master that was locked for exclusive bus access
2995 * Context: can sleep
2996 *
2997 * This call may only be used from a context that may sleep. The sleep
2998 * is non-interruptible, and has no timeout.
2999 *
3000 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3001 * call.
3002 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003003 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003004 */
3005int spi_bus_unlock(struct spi_master *master)
3006{
3007 master->bus_lock_flag = 0;
3008
3009 mutex_unlock(&master->bus_lock_mutex);
3010
3011 return 0;
3012}
3013EXPORT_SYMBOL_GPL(spi_bus_unlock);
3014
David Brownella9948b62006-04-02 10:37:40 -08003015/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003016#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003017
3018static u8 *buf;
3019
3020/**
3021 * spi_write_then_read - SPI synchronous write followed by read
3022 * @spi: device with which data will be exchanged
3023 * @txbuf: data to be written (need not be dma-safe)
3024 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003025 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3026 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003027 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003028 *
3029 * This performs a half duplex MicroWire style transaction with the
3030 * device, sending txbuf and then reading rxbuf. The return value
3031 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003032 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003033 *
David Brownell0c868462006-01-08 13:34:25 -08003034 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003035 * portable code should never use this for more than 32 bytes.
3036 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c868462006-01-08 13:34:25 -08003037 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003038 *
3039 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003040 */
3041int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003042 const void *txbuf, unsigned n_tx,
3043 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003044{
David Brownell068f4072007-12-04 23:45:09 -08003045 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003046
3047 int status;
3048 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003049 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003050 u8 *local_buf;
3051
Mark Brownb3a223e2012-12-02 12:54:25 +09003052 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3053 * copying here, (as a pure convenience thing), but we can
3054 * keep heap costs out of the hot path unless someone else is
3055 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003056 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003057 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003058 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3059 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003060 if (!local_buf)
3061 return -ENOMEM;
3062 } else {
3063 local_buf = buf;
3064 }
David Brownell8ae12a02006-01-08 13:34:19 -08003065
Vitaly Wool8275c642006-01-08 13:34:28 -08003066 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003067 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003068 if (n_tx) {
3069 x[0].len = n_tx;
3070 spi_message_add_tail(&x[0], &message);
3071 }
3072 if (n_rx) {
3073 x[1].len = n_rx;
3074 spi_message_add_tail(&x[1], &message);
3075 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003076
David Brownell8ae12a02006-01-08 13:34:19 -08003077 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003078 x[0].tx_buf = local_buf;
3079 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003080
3081 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003082 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003083 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003084 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003085
David Brownellbdff5492009-04-13 14:39:57 -07003086 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003087 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003088 else
3089 kfree(local_buf);
3090
3091 return status;
3092}
3093EXPORT_SYMBOL_GPL(spi_write_then_read);
3094
3095/*-------------------------------------------------------------------------*/
3096
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003097#if IS_ENABLED(CONFIG_OF_DYNAMIC)
3098static int __spi_of_device_match(struct device *dev, void *data)
3099{
3100 return dev->of_node == data;
3101}
3102
3103/* must call put_device() when done with returned spi_device device */
3104static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
3105{
3106 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3107 __spi_of_device_match);
3108 return dev ? to_spi_device(dev) : NULL;
3109}
3110
3111static int __spi_of_master_match(struct device *dev, const void *data)
3112{
3113 return dev->of_node == data;
3114}
3115
3116/* the spi masters are not using spi_bus, so we find it with another way */
3117static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
3118{
3119 struct device *dev;
3120
3121 dev = class_find_device(&spi_master_class, NULL, node,
3122 __spi_of_master_match);
3123 if (!dev)
3124 return NULL;
3125
3126 /* reference got in class_find_device */
3127 return container_of(dev, struct spi_master, dev);
3128}
3129
3130static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3131 void *arg)
3132{
3133 struct of_reconfig_data *rd = arg;
3134 struct spi_master *master;
3135 struct spi_device *spi;
3136
3137 switch (of_reconfig_get_state_change(action, arg)) {
3138 case OF_RECONFIG_CHANGE_ADD:
3139 master = of_find_spi_master_by_node(rd->dn->parent);
3140 if (master == NULL)
3141 return NOTIFY_OK; /* not for us */
3142
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003143 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
3144 put_device(&master->dev);
3145 return NOTIFY_OK;
3146 }
3147
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003148 spi = of_register_spi_device(master, rd->dn);
3149 put_device(&master->dev);
3150
3151 if (IS_ERR(spi)) {
3152 pr_err("%s: failed to create for '%s'\n",
3153 __func__, rd->dn->full_name);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003154 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003155 return notifier_from_errno(PTR_ERR(spi));
3156 }
3157 break;
3158
3159 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003160 /* already depopulated? */
3161 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3162 return NOTIFY_OK;
3163
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003164 /* find our device by node */
3165 spi = of_find_spi_device_by_node(rd->dn);
3166 if (spi == NULL)
3167 return NOTIFY_OK; /* no? not meant for us */
3168
3169 /* unregister takes one ref away */
3170 spi_unregister_device(spi);
3171
3172 /* and put the reference of the find */
3173 put_device(&spi->dev);
3174 break;
3175 }
3176
3177 return NOTIFY_OK;
3178}
3179
3180static struct notifier_block spi_of_notifier = {
3181 .notifier_call = of_spi_notify,
3182};
3183#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3184extern struct notifier_block spi_of_notifier;
3185#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3186
Octavian Purdila7f244672016-07-08 19:13:11 +03003187#if IS_ENABLED(CONFIG_ACPI)
3188static int spi_acpi_master_match(struct device *dev, const void *data)
3189{
3190 return ACPI_COMPANION(dev->parent) == data;
3191}
3192
3193static int spi_acpi_device_match(struct device *dev, void *data)
3194{
3195 return ACPI_COMPANION(dev) == data;
3196}
3197
3198static struct spi_master *acpi_spi_find_master_by_adev(struct acpi_device *adev)
3199{
3200 struct device *dev;
3201
3202 dev = class_find_device(&spi_master_class, NULL, adev,
3203 spi_acpi_master_match);
3204 if (!dev)
3205 return NULL;
3206
3207 return container_of(dev, struct spi_master, dev);
3208}
3209
3210static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3211{
3212 struct device *dev;
3213
3214 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3215
3216 return dev ? to_spi_device(dev) : NULL;
3217}
3218
3219static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3220 void *arg)
3221{
3222 struct acpi_device *adev = arg;
3223 struct spi_master *master;
3224 struct spi_device *spi;
3225
3226 switch (value) {
3227 case ACPI_RECONFIG_DEVICE_ADD:
3228 master = acpi_spi_find_master_by_adev(adev->parent);
3229 if (!master)
3230 break;
3231
3232 acpi_register_spi_device(master, adev);
3233 put_device(&master->dev);
3234 break;
3235 case ACPI_RECONFIG_DEVICE_REMOVE:
3236 if (!acpi_device_enumerated(adev))
3237 break;
3238
3239 spi = acpi_spi_find_device_by_adev(adev);
3240 if (!spi)
3241 break;
3242
3243 spi_unregister_device(spi);
3244 put_device(&spi->dev);
3245 break;
3246 }
3247
3248 return NOTIFY_OK;
3249}
3250
3251static struct notifier_block spi_acpi_notifier = {
3252 .notifier_call = acpi_spi_notify,
3253};
3254#else
3255extern struct notifier_block spi_acpi_notifier;
3256#endif
3257
David Brownell8ae12a02006-01-08 13:34:19 -08003258static int __init spi_init(void)
3259{
David Brownellb8852442006-01-08 13:34:23 -08003260 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08003261
Christoph Lametere94b1762006-12-06 20:33:17 -08003262 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08003263 if (!buf) {
3264 status = -ENOMEM;
3265 goto err0;
3266 }
3267
3268 status = bus_register(&spi_bus_type);
3269 if (status < 0)
3270 goto err1;
3271
3272 status = class_register(&spi_master_class);
3273 if (status < 0)
3274 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003275
Fabio Estevam52677202014-11-26 20:13:57 -02003276 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003277 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03003278 if (IS_ENABLED(CONFIG_ACPI))
3279 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003280
David Brownell8ae12a02006-01-08 13:34:19 -08003281 return 0;
David Brownellb8852442006-01-08 13:34:23 -08003282
3283err2:
3284 bus_unregister(&spi_bus_type);
3285err1:
3286 kfree(buf);
3287 buf = NULL;
3288err0:
3289 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08003290}
David Brownellb8852442006-01-08 13:34:23 -08003291
David Brownell8ae12a02006-01-08 13:34:19 -08003292/* board_info is normally registered in arch_initcall(),
3293 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08003294 *
3295 * REVISIT only boardinfo really needs static linking. the rest (device and
3296 * driver registration) _could_ be dynamically linked (modular) ... costs
3297 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08003298 */
David Brownell673c0c02008-10-15 22:02:46 -07003299postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08003300