blob: d74d341f9890d1badf9cdbea171206ae1b095911 [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 Hovold9846c672017-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 Hovold9846c672017-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
Feng Tang2b9603a2010-08-02 15:52:15 +0800677 bi = kzalloc(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
Andy Shevchenko243f07b2015-10-20 12:28:29 +0300702 if (gpio_is_valid(spi->cs_gpio))
Mark Brownb1589352013-10-05 11:50:40 +0100703 gpio_set_value(spi->cs_gpio, !enable);
704 else if (spi->master->set_cs)
705 spi->master->set_cs(spi, !enable);
706}
707
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200708#ifdef CONFIG_HAS_DMA
Mark Brown6ad45a22014-02-02 13:47:47 +0000709static int spi_map_buf(struct spi_master *master, struct device *dev,
710 struct sg_table *sgt, void *buf, size_t len,
711 enum dma_data_direction dir)
712{
713 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200714 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530715#ifdef CONFIG_HIGHMEM
716 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
717 (unsigned long)buf < (PKMAP_BASE +
718 (LAST_PKMAP * PAGE_SIZE)));
719#else
720 const bool kmap_buf = false;
721#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500722 int desc_len;
723 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000724 struct page *vm_page;
725 void *sg_buf;
726 size_t min;
727 int i, ret;
728
Vignesh Rb1b81532016-08-17 15:22:36 +0530729 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200730 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500731 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530732 } else if (virt_addr_valid(buf)) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200733 desc_len = min_t(int, max_seg_size, master->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500734 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530735 } else {
736 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500737 }
738
Mark Brown6ad45a22014-02-02 13:47:47 +0000739 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
740 if (ret != 0)
741 return ret;
742
743 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000744
Vignesh Rb1b81532016-08-17 15:22:36 +0530745 if (vmalloced_buf || kmap_buf) {
Maxime Chevallier34023ca2018-03-02 15:55:09 +0100746 /*
747 * Next scatterlist entry size is the minimum between
748 * the desc_len and the remaining buffer length that
749 * fits in a page.
750 */
751 min = min_t(size_t, desc_len,
752 min_t(size_t, len,
753 PAGE_SIZE - offset_in_page(buf)));
Vignesh Rb1b81532016-08-17 15:22:36 +0530754 if (vmalloced_buf)
755 vm_page = vmalloc_to_page(buf);
756 else
757 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000758 if (!vm_page) {
759 sg_free_table(sgt);
760 return -ENOMEM;
761 }
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000762 sg_set_page(&sgt->sgl[i], vm_page,
763 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000764 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500765 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000766 sg_buf = buf;
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000767 sg_set_buf(&sgt->sgl[i], sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000768 }
769
Mark Brown6ad45a22014-02-02 13:47:47 +0000770 buf += min;
771 len -= min;
772 }
773
774 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200775 if (!ret)
776 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000777 if (ret < 0) {
778 sg_free_table(sgt);
779 return ret;
780 }
781
782 sgt->nents = ret;
783
784 return 0;
785}
786
787static void spi_unmap_buf(struct spi_master *master, struct device *dev,
788 struct sg_table *sgt, enum dma_data_direction dir)
789{
790 if (sgt->orig_nents) {
791 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
792 sg_free_table(sgt);
793 }
794}
795
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200796static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000797{
Mark Brown99adef32014-01-16 12:22:43 +0000798 struct device *tx_dev, *rx_dev;
799 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000800 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000801
Mark Brown6ad45a22014-02-02 13:47:47 +0000802 if (!master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000803 return 0;
804
Leilk Liuc37f45b2015-07-23 17:10:40 +0800805 if (master->dma_tx)
806 tx_dev = master->dma_tx->device->dev;
807 else
Daniel Kurtzc52829f2017-01-27 00:21:53 +0800808 tx_dev = master->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800809
810 if (master->dma_rx)
811 rx_dev = master->dma_rx->device->dev;
812 else
Daniel Kurtzc52829f2017-01-27 00:21:53 +0800813 rx_dev = master->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000814
815 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
816 if (!master->can_dma(master, msg->spi, xfer))
817 continue;
818
819 if (xfer->tx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000820 ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
821 (void *)xfer->tx_buf, xfer->len,
822 DMA_TO_DEVICE);
823 if (ret != 0)
824 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000825 }
826
827 if (xfer->rx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000828 ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
829 xfer->rx_buf, xfer->len,
830 DMA_FROM_DEVICE);
831 if (ret != 0) {
832 spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
833 DMA_TO_DEVICE);
834 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000835 }
836 }
837 }
838
839 master->cur_msg_mapped = true;
840
841 return 0;
842}
843
Martin Sperl4b786452015-05-25 10:13:10 +0000844static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000845{
846 struct spi_transfer *xfer;
847 struct device *tx_dev, *rx_dev;
848
Mark Brown6ad45a22014-02-02 13:47:47 +0000849 if (!master->cur_msg_mapped || !master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000850 return 0;
851
Leilk Liuc37f45b2015-07-23 17:10:40 +0800852 if (master->dma_tx)
853 tx_dev = master->dma_tx->device->dev;
854 else
Daniel Kurtzc52829f2017-01-27 00:21:53 +0800855 tx_dev = master->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800856
857 if (master->dma_rx)
858 rx_dev = master->dma_rx->device->dev;
859 else
Daniel Kurtzc52829f2017-01-27 00:21:53 +0800860 rx_dev = master->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000861
862 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
863 if (!master->can_dma(master, msg->spi, xfer))
864 continue;
865
Mark Brown6ad45a22014-02-02 13:47:47 +0000866 spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
867 spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000868 }
869
870 return 0;
871}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200872#else /* !CONFIG_HAS_DMA */
Vignesh Rf4502dd2016-06-08 12:18:31 +0530873static inline int spi_map_buf(struct spi_master *master,
874 struct device *dev, struct sg_table *sgt,
875 void *buf, size_t len,
876 enum dma_data_direction dir)
877{
878 return -EINVAL;
879}
880
881static inline void spi_unmap_buf(struct spi_master *master,
882 struct device *dev, struct sg_table *sgt,
883 enum dma_data_direction dir)
884{
885}
886
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200887static inline int __spi_map_msg(struct spi_master *master,
888 struct spi_message *msg)
889{
890 return 0;
891}
892
Martin Sperl4b786452015-05-25 10:13:10 +0000893static inline int __spi_unmap_msg(struct spi_master *master,
894 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200895{
896 return 0;
897}
898#endif /* !CONFIG_HAS_DMA */
899
Martin Sperl4b786452015-05-25 10:13:10 +0000900static inline int spi_unmap_msg(struct spi_master *master,
901 struct spi_message *msg)
902{
903 struct spi_transfer *xfer;
904
905 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
906 /*
907 * Restore the original value of tx_buf or rx_buf if they are
908 * NULL.
909 */
910 if (xfer->tx_buf == master->dummy_tx)
911 xfer->tx_buf = NULL;
912 if (xfer->rx_buf == master->dummy_rx)
913 xfer->rx_buf = NULL;
914 }
915
916 return __spi_unmap_msg(master, msg);
917}
918
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200919static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
920{
921 struct spi_transfer *xfer;
922 void *tmp;
923 unsigned int max_tx, max_rx;
924
925 if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
926 max_tx = 0;
927 max_rx = 0;
928
929 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
930 if ((master->flags & SPI_MASTER_MUST_TX) &&
931 !xfer->tx_buf)
932 max_tx = max(xfer->len, max_tx);
933 if ((master->flags & SPI_MASTER_MUST_RX) &&
934 !xfer->rx_buf)
935 max_rx = max(xfer->len, max_rx);
936 }
937
938 if (max_tx) {
939 tmp = krealloc(master->dummy_tx, max_tx,
940 GFP_KERNEL | GFP_DMA);
941 if (!tmp)
942 return -ENOMEM;
943 master->dummy_tx = tmp;
944 memset(tmp, 0, max_tx);
945 }
946
947 if (max_rx) {
948 tmp = krealloc(master->dummy_rx, max_rx,
949 GFP_KERNEL | GFP_DMA);
950 if (!tmp)
951 return -ENOMEM;
952 master->dummy_rx = tmp;
953 }
954
955 if (max_tx || max_rx) {
956 list_for_each_entry(xfer, &msg->transfers,
957 transfer_list) {
Chris Lesiakdbf40db2019-03-07 20:39:00 +0000958 if (!xfer->len)
959 continue;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200960 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);
Hauke Mehrtens5da64152017-04-17 01:38:05 +02001017 ms += ms + 200; /* 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
1047 if (xfer->delay_usecs)
1048 udelay(xfer->delay_usecs);
1049
1050 if (xfer->cs_change) {
1051 if (list_is_last(&xfer->transfer_list,
1052 &msg->transfers)) {
1053 keep_cs = true;
1054 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001055 spi_set_cs(msg->spi, false);
1056 udelay(10);
1057 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001058 }
1059 }
1060
1061 msg->actual_length += xfer->len;
1062 }
1063
1064out:
1065 if (ret != 0 || !keep_cs)
1066 spi_set_cs(msg->spi, false);
1067
1068 if (msg->status == -EINPROGRESS)
1069 msg->status = ret;
1070
Geert Uytterhoevenff61eb42015-04-07 20:39:19 +02001071 if (msg->status && master->handle_err)
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001072 master->handle_err(master, msg);
1073
Martin Sperld780c372015-12-14 15:20:18 +00001074 spi_res_release(master, msg);
1075
Mark Brownb1589352013-10-05 11:50:40 +01001076 spi_finalize_current_message(master);
1077
1078 return ret;
1079}
1080
1081/**
1082 * spi_finalize_current_transfer - report completion of a transfer
Thierry Reding2c675682014-08-08 13:02:36 +02001083 * @master: the master reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001084 *
1085 * Called by SPI drivers using the core transfer_one_message()
1086 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001087 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001088 */
1089void spi_finalize_current_transfer(struct spi_master *master)
1090{
1091 complete(&master->xfer_completion);
1092}
1093EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1094
Linus Walleijffbbdd212012-02-22 10:05:38 +01001095/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001096 * __spi_pump_messages - function which processes spi message queue
1097 * @master: master to process queue for
1098 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001099 *
1100 * This function checks if there is any spi message in the queue that
1101 * needs processing and if so call out to the driver to initialize hardware
1102 * and transfer each message.
1103 *
Mark Brown0461a412014-12-09 21:38:05 +00001104 * Note that it is called both from the kthread itself and also from
1105 * inside spi_sync(); the queue extraction handling at the top of the
1106 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001107 */
Mark Brownef4d96e2016-07-21 23:53:31 +01001108static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001109{
Linus Walleijffbbdd212012-02-22 10:05:38 +01001110 unsigned long flags;
1111 bool was_busy = false;
1112 int ret;
1113
Mark Brown983aee52014-12-09 19:46:56 +00001114 /* Lock queue */
Linus Walleijffbbdd212012-02-22 10:05:38 +01001115 spin_lock_irqsave(&master->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001116
1117 /* Make sure we are not already running a message */
1118 if (master->cur_msg) {
1119 spin_unlock_irqrestore(&master->queue_lock, flags);
1120 return;
1121 }
1122
Mark Brown0461a412014-12-09 21:38:05 +00001123 /* If another context is idling the device then defer */
1124 if (master->idling) {
Petr Mladek39891442016-10-11 13:55:20 -07001125 kthread_queue_work(&master->kworker, &master->pump_messages);
Mark Brown0461a412014-12-09 21:38:05 +00001126 spin_unlock_irqrestore(&master->queue_lock, flags);
1127 return;
1128 }
1129
Mark Brown983aee52014-12-09 19:46:56 +00001130 /* Check if the queue is idle */
Linus Walleijffbbdd212012-02-22 10:05:38 +01001131 if (list_empty(&master->queue) || !master->running) {
Bryan Freedb0b36b82013-03-13 11:17:40 -07001132 if (!master->busy) {
1133 spin_unlock_irqrestore(&master->queue_lock, flags);
1134 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001135 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001136
1137 /* Only do teardown in the thread */
1138 if (!in_kthread) {
Petr Mladek39891442016-10-11 13:55:20 -07001139 kthread_queue_work(&master->kworker,
Mark Brownfc9e0f72014-12-10 13:46:33 +00001140 &master->pump_messages);
1141 spin_unlock_irqrestore(&master->queue_lock, flags);
1142 return;
1143 }
1144
Linus Walleijffbbdd212012-02-22 10:05:38 +01001145 master->busy = false;
Mark Brown0461a412014-12-09 21:38:05 +00001146 master->idling = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001147 spin_unlock_irqrestore(&master->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001148
Mark Brown3a2eba92014-01-28 20:17:03 +00001149 kfree(master->dummy_rx);
1150 master->dummy_rx = NULL;
1151 kfree(master->dummy_tx);
1152 master->dummy_tx = NULL;
Bryan Freedb0b36b82013-03-13 11:17:40 -07001153 if (master->unprepare_transfer_hardware &&
1154 master->unprepare_transfer_hardware(master))
1155 dev_err(&master->dev,
1156 "failed to unprepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001157 if (master->auto_runtime_pm) {
1158 pm_runtime_mark_last_busy(master->dev.parent);
1159 pm_runtime_put_autosuspend(master->dev.parent);
1160 }
Mark Brown56ec1972013-10-07 19:33:53 +01001161 trace_spi_master_idle(master);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001162
Mark Brown0461a412014-12-09 21:38:05 +00001163 spin_lock_irqsave(&master->queue_lock, flags);
1164 master->idling = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001165 spin_unlock_irqrestore(&master->queue_lock, flags);
1166 return;
1167 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001168
Linus Walleijffbbdd212012-02-22 10:05:38 +01001169 /* Extract head of queue */
1170 master->cur_msg =
Axel Lina89e2d22014-01-09 16:03:58 +08001171 list_first_entry(&master->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001172
1173 list_del_init(&master->cur_msg->queue);
1174 if (master->busy)
1175 was_busy = true;
1176 else
1177 master->busy = true;
1178 spin_unlock_irqrestore(&master->queue_lock, flags);
1179
Mark Brownef4d96e2016-07-21 23:53:31 +01001180 mutex_lock(&master->io_mutex);
1181
Mark Brown49834de2013-07-28 14:47:02 +01001182 if (!was_busy && master->auto_runtime_pm) {
1183 ret = pm_runtime_get_sync(master->dev.parent);
1184 if (ret < 0) {
1185 dev_err(&master->dev, "Failed to power device: %d\n",
1186 ret);
Mark Brown764f21662016-08-09 17:45:33 +01001187 mutex_unlock(&master->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001188 return;
1189 }
1190 }
1191
Mark Brown56ec1972013-10-07 19:33:53 +01001192 if (!was_busy)
1193 trace_spi_master_busy(master);
1194
Shubhrajyoti D7dfd2bd2012-05-10 19:20:41 +05301195 if (!was_busy && master->prepare_transfer_hardware) {
Linus Walleijffbbdd212012-02-22 10:05:38 +01001196 ret = master->prepare_transfer_hardware(master);
1197 if (ret) {
1198 dev_err(&master->dev,
1199 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001200
1201 if (master->auto_runtime_pm)
1202 pm_runtime_put(master->dev.parent);
Mark Brown764f21662016-08-09 17:45:33 +01001203 mutex_unlock(&master->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001204 return;
1205 }
1206 }
1207
Mark Brown56ec1972013-10-07 19:33:53 +01001208 trace_spi_message_start(master->cur_msg);
1209
Mark Brown2841a5f2013-10-05 00:23:12 +01001210 if (master->prepare_message) {
1211 ret = master->prepare_message(master, master->cur_msg);
1212 if (ret) {
1213 dev_err(&master->dev,
1214 "failed to prepare message: %d\n", ret);
1215 master->cur_msg->status = ret;
1216 spi_finalize_current_message(master);
Jon Hunter49023d22016-03-08 12:28:20 +00001217 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001218 }
1219 master->cur_msg_prepared = true;
1220 }
1221
Mark Brown99adef32014-01-16 12:22:43 +00001222 ret = spi_map_msg(master, master->cur_msg);
1223 if (ret) {
1224 master->cur_msg->status = ret;
1225 spi_finalize_current_message(master);
Jon Hunter49023d22016-03-08 12:28:20 +00001226 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001227 }
1228
Linus Walleijffbbdd212012-02-22 10:05:38 +01001229 ret = master->transfer_one_message(master, master->cur_msg);
1230 if (ret) {
1231 dev_err(&master->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001232 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001233 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001234 }
Jon Hunter49023d22016-03-08 12:28:20 +00001235
1236out:
Mark Brownef4d96e2016-07-21 23:53:31 +01001237 mutex_unlock(&master->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001238
1239 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001240 if (!ret)
1241 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001242}
1243
Mark Brownfc9e0f72014-12-10 13:46:33 +00001244/**
1245 * spi_pump_messages - kthread work function which processes spi message queue
1246 * @work: pointer to kthread work struct contained in the master struct
1247 */
1248static void spi_pump_messages(struct kthread_work *work)
1249{
1250 struct spi_master *master =
1251 container_of(work, struct spi_master, pump_messages);
1252
Mark Brownef4d96e2016-07-21 23:53:31 +01001253 __spi_pump_messages(master, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001254}
1255
Linus Walleijffbbdd212012-02-22 10:05:38 +01001256static int spi_init_queue(struct spi_master *master)
1257{
1258 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1259
Linus Walleijffbbdd212012-02-22 10:05:38 +01001260 master->running = false;
1261 master->busy = false;
1262
Petr Mladek39891442016-10-11 13:55:20 -07001263 kthread_init_worker(&master->kworker);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001264 master->kworker_task = kthread_run(kthread_worker_fn,
Kees Cookf1701682013-07-03 15:04:58 -07001265 &master->kworker, "%s",
Linus Walleijffbbdd212012-02-22 10:05:38 +01001266 dev_name(&master->dev));
1267 if (IS_ERR(master->kworker_task)) {
1268 dev_err(&master->dev, "failed to create message pump task\n");
Jarkko Nikula98a8f5a2014-12-04 11:02:25 +02001269 return PTR_ERR(master->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001270 }
Petr Mladek39891442016-10-11 13:55:20 -07001271 kthread_init_work(&master->pump_messages, spi_pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001272
1273 /*
1274 * Master config will indicate if this controller should run the
1275 * message pump with high (realtime) priority to reduce the transfer
1276 * latency on the bus by minimising the delay between a transfer
1277 * request and the scheduling of the message pump thread. Without this
1278 * setting the message pump thread will remain at default priority.
1279 */
1280 if (master->rt) {
1281 dev_info(&master->dev,
1282 "will run message pump with realtime priority\n");
1283 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
1284 }
1285
1286 return 0;
1287}
1288
1289/**
1290 * spi_get_next_queued_message() - called by driver to check for queued
1291 * messages
1292 * @master: the master to check for queued messages
1293 *
1294 * If there are more messages in the queue, the next message is returned from
1295 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001296 *
1297 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001298 */
1299struct spi_message *spi_get_next_queued_message(struct spi_master *master)
1300{
1301 struct spi_message *next;
1302 unsigned long flags;
1303
1304 /* get a pointer to the next message, if any */
1305 spin_lock_irqsave(&master->queue_lock, flags);
Axel Lin1cfd97f2014-01-02 15:16:40 +08001306 next = list_first_entry_or_null(&master->queue, struct spi_message,
1307 queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001308 spin_unlock_irqrestore(&master->queue_lock, flags);
1309
1310 return next;
1311}
1312EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1313
1314/**
1315 * spi_finalize_current_message() - the current message is complete
1316 * @master: the master to return the message to
1317 *
1318 * Called by the driver to notify the core that the message in the front of the
1319 * queue is complete and can be removed from the queue.
1320 */
1321void spi_finalize_current_message(struct spi_master *master)
1322{
1323 struct spi_message *mesg;
1324 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001325 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001326
1327 spin_lock_irqsave(&master->queue_lock, flags);
1328 mesg = master->cur_msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001329 spin_unlock_irqrestore(&master->queue_lock, flags);
1330
Mark Brown99adef32014-01-16 12:22:43 +00001331 spi_unmap_msg(master, mesg);
1332
Mark Brown2841a5f2013-10-05 00:23:12 +01001333 if (master->cur_msg_prepared && master->unprepare_message) {
1334 ret = master->unprepare_message(master, mesg);
1335 if (ret) {
1336 dev_err(&master->dev,
1337 "failed to unprepare message: %d\n", ret);
1338 }
1339 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001340
Martin Sperl8e76ef82015-05-10 07:50:45 +00001341 spin_lock_irqsave(&master->queue_lock, flags);
1342 master->cur_msg = NULL;
Mark Brown2841a5f2013-10-05 00:23:12 +01001343 master->cur_msg_prepared = false;
Petr Mladek39891442016-10-11 13:55:20 -07001344 kthread_queue_work(&master->kworker, &master->pump_messages);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001345 spin_unlock_irqrestore(&master->queue_lock, flags);
1346
1347 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001348
Linus Walleijffbbdd212012-02-22 10:05:38 +01001349 mesg->state = NULL;
1350 if (mesg->complete)
1351 mesg->complete(mesg->context);
1352}
1353EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1354
1355static int spi_start_queue(struct spi_master *master)
1356{
1357 unsigned long flags;
1358
1359 spin_lock_irqsave(&master->queue_lock, flags);
1360
1361 if (master->running || master->busy) {
1362 spin_unlock_irqrestore(&master->queue_lock, flags);
1363 return -EBUSY;
1364 }
1365
1366 master->running = true;
1367 master->cur_msg = NULL;
1368 spin_unlock_irqrestore(&master->queue_lock, flags);
1369
Petr Mladek39891442016-10-11 13:55:20 -07001370 kthread_queue_work(&master->kworker, &master->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001371
1372 return 0;
1373}
1374
1375static int spi_stop_queue(struct spi_master *master)
1376{
1377 unsigned long flags;
1378 unsigned limit = 500;
1379 int ret = 0;
1380
1381 spin_lock_irqsave(&master->queue_lock, flags);
1382
1383 /*
1384 * This is a bit lame, but is optimized for the common execution path.
1385 * A wait_queue on the master->busy could be used, but then the common
1386 * execution path (pump_messages) would be required to call wake_up or
1387 * friends on every SPI message. Do this instead.
1388 */
1389 while ((!list_empty(&master->queue) || master->busy) && limit--) {
1390 spin_unlock_irqrestore(&master->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001391 usleep_range(10000, 11000);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001392 spin_lock_irqsave(&master->queue_lock, flags);
1393 }
1394
1395 if (!list_empty(&master->queue) || master->busy)
1396 ret = -EBUSY;
1397 else
1398 master->running = false;
1399
1400 spin_unlock_irqrestore(&master->queue_lock, flags);
1401
1402 if (ret) {
1403 dev_warn(&master->dev,
1404 "could not stop message queue\n");
1405 return ret;
1406 }
1407 return ret;
1408}
1409
1410static int spi_destroy_queue(struct spi_master *master)
1411{
1412 int ret;
1413
1414 ret = spi_stop_queue(master);
1415
1416 /*
Petr Mladek39891442016-10-11 13:55:20 -07001417 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001418 * If the reason that stop_queue timed out is that the work will never
1419 * finish, then it does no good to call flush/stop thread, so
1420 * return anyway.
1421 */
1422 if (ret) {
1423 dev_err(&master->dev, "problem destroying queue\n");
1424 return ret;
1425 }
1426
Petr Mladek39891442016-10-11 13:55:20 -07001427 kthread_flush_worker(&master->kworker);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001428 kthread_stop(master->kworker_task);
1429
1430 return 0;
1431}
1432
Mark Brown0461a412014-12-09 21:38:05 +00001433static int __spi_queued_transfer(struct spi_device *spi,
1434 struct spi_message *msg,
1435 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001436{
1437 struct spi_master *master = spi->master;
1438 unsigned long flags;
1439
1440 spin_lock_irqsave(&master->queue_lock, flags);
1441
1442 if (!master->running) {
1443 spin_unlock_irqrestore(&master->queue_lock, flags);
1444 return -ESHUTDOWN;
1445 }
1446 msg->actual_length = 0;
1447 msg->status = -EINPROGRESS;
1448
1449 list_add_tail(&msg->queue, &master->queue);
Mark Brown0461a412014-12-09 21:38:05 +00001450 if (!master->busy && need_pump)
Petr Mladek39891442016-10-11 13:55:20 -07001451 kthread_queue_work(&master->kworker, &master->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001452
1453 spin_unlock_irqrestore(&master->queue_lock, flags);
1454 return 0;
1455}
1456
Mark Brown0461a412014-12-09 21:38:05 +00001457/**
1458 * spi_queued_transfer - transfer function for queued transfers
1459 * @spi: spi device which is requesting transfer
1460 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001461 *
1462 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001463 */
1464static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1465{
1466 return __spi_queued_transfer(spi, msg, true);
1467}
1468
Linus Walleijffbbdd212012-02-22 10:05:38 +01001469static int spi_master_initialize_queue(struct spi_master *master)
1470{
1471 int ret;
1472
Linus Walleijffbbdd212012-02-22 10:05:38 +01001473 master->transfer = spi_queued_transfer;
Mark Brownb1589352013-10-05 11:50:40 +01001474 if (!master->transfer_one_message)
1475 master->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001476
1477 /* Initialize and start queue */
1478 ret = spi_init_queue(master);
1479 if (ret) {
1480 dev_err(&master->dev, "problem initializing queue\n");
1481 goto err_init_queue;
1482 }
Mark Brownc3676d52014-05-01 10:47:52 -07001483 master->queued = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001484 ret = spi_start_queue(master);
1485 if (ret) {
1486 dev_err(&master->dev, "problem starting queue\n");
1487 goto err_start_queue;
1488 }
1489
1490 return 0;
1491
1492err_start_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001493 spi_destroy_queue(master);
Mark Brownc3676d52014-05-01 10:47:52 -07001494err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001495 return ret;
1496}
1497
1498/*-------------------------------------------------------------------------*/
1499
Andreas Larsson7cb94362012-12-04 15:09:38 +01001500#if defined(CONFIG_OF)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001501static struct spi_device *
1502of_register_spi_device(struct spi_master *master, struct device_node *nc)
1503{
1504 struct spi_device *spi;
1505 int rc;
1506 u32 value;
1507
1508 /* Alloc an spi_device */
1509 spi = spi_alloc_device(master);
1510 if (!spi) {
1511 dev_err(&master->dev, "spi_device alloc error for %s\n",
1512 nc->full_name);
1513 rc = -ENOMEM;
1514 goto err_out;
1515 }
1516
1517 /* Select device driver */
1518 rc = of_modalias_node(nc, spi->modalias,
1519 sizeof(spi->modalias));
1520 if (rc < 0) {
1521 dev_err(&master->dev, "cannot find modalias for %s\n",
1522 nc->full_name);
1523 goto err_out;
1524 }
1525
1526 /* Device address */
1527 rc = of_property_read_u32(nc, "reg", &value);
1528 if (rc) {
1529 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1530 nc->full_name, rc);
1531 goto err_out;
1532 }
1533 spi->chip_select = value;
1534
1535 /* Mode (clock phase/polarity/etc.) */
1536 if (of_find_property(nc, "spi-cpha", NULL))
1537 spi->mode |= SPI_CPHA;
1538 if (of_find_property(nc, "spi-cpol", NULL))
1539 spi->mode |= SPI_CPOL;
1540 if (of_find_property(nc, "spi-cs-high", NULL))
1541 spi->mode |= SPI_CS_HIGH;
1542 if (of_find_property(nc, "spi-3wire", NULL))
1543 spi->mode |= SPI_3WIRE;
1544 if (of_find_property(nc, "spi-lsb-first", NULL))
1545 spi->mode |= SPI_LSB_FIRST;
1546
1547 /* Device DUAL/QUAD mode */
1548 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1549 switch (value) {
1550 case 1:
1551 break;
1552 case 2:
1553 spi->mode |= SPI_TX_DUAL;
1554 break;
1555 case 4:
1556 spi->mode |= SPI_TX_QUAD;
1557 break;
1558 default:
1559 dev_warn(&master->dev,
1560 "spi-tx-bus-width %d not supported\n",
1561 value);
1562 break;
1563 }
1564 }
1565
1566 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1567 switch (value) {
1568 case 1:
1569 break;
1570 case 2:
1571 spi->mode |= SPI_RX_DUAL;
1572 break;
1573 case 4:
1574 spi->mode |= SPI_RX_QUAD;
1575 break;
1576 default:
1577 dev_warn(&master->dev,
1578 "spi-rx-bus-width %d not supported\n",
1579 value);
1580 break;
1581 }
1582 }
1583
1584 /* Device speed */
1585 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1586 if (rc) {
1587 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1588 nc->full_name, rc);
1589 goto err_out;
1590 }
1591 spi->max_speed_hz = value;
1592
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001593 /* Store a pointer to the node in the device structure */
1594 of_node_get(nc);
1595 spi->dev.of_node = nc;
1596
1597 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001598 rc = spi_add_device(spi);
1599 if (rc) {
1600 dev_err(&master->dev, "spi_device register error %s\n",
1601 nc->full_name);
Johan Hovold9846c672017-01-30 17:47:05 +01001602 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001603 }
1604
1605 return spi;
1606
Johan Hovold9846c672017-01-30 17:47:05 +01001607err_of_node_put:
1608 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001609err_out:
1610 spi_dev_put(spi);
1611 return ERR_PTR(rc);
1612}
1613
Grant Likelyd57a4282012-04-07 14:16:53 -06001614/**
1615 * of_register_spi_devices() - Register child devices onto the SPI bus
1616 * @master: Pointer to spi_master device
1617 *
1618 * Registers an spi_device for each child node of master node which has a 'reg'
1619 * property.
1620 */
1621static void of_register_spi_devices(struct spi_master *master)
1622{
1623 struct spi_device *spi;
1624 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001625
1626 if (!master->dev.of_node)
1627 return;
1628
Alexander Sverdlinf3b61592012-11-29 08:59:29 +01001629 for_each_available_child_of_node(master->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01001630 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1631 continue;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001632 spi = of_register_spi_device(master, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001633 if (IS_ERR(spi)) {
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001634 dev_warn(&master->dev, "Failed to create SPI device for %s\n",
Grant Likelyd57a4282012-04-07 14:16:53 -06001635 nc->full_name);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001636 of_node_clear_flag(nc, OF_POPULATED);
1637 }
Grant Likelyd57a4282012-04-07 14:16:53 -06001638 }
1639}
1640#else
1641static void of_register_spi_devices(struct spi_master *master) { }
1642#endif
1643
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001644#ifdef CONFIG_ACPI
1645static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1646{
1647 struct spi_device *spi = data;
Mika Westerberga0a90712016-02-08 17:14:28 +02001648 struct spi_master *master = spi->master;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001649
1650 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1651 struct acpi_resource_spi_serialbus *sb;
1652
1653 sb = &ares->data.spi_serial_bus;
1654 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Mika Westerberga0a90712016-02-08 17:14:28 +02001655 /*
1656 * ACPI DeviceSelection numbering is handled by the
1657 * host controller driver in Windows and can vary
1658 * from driver to driver. In Linux we always expect
1659 * 0 .. max - 1 so we need to ask the driver to
1660 * translate between the two schemes.
1661 */
1662 if (master->fw_translate_cs) {
1663 int cs = master->fw_translate_cs(master,
1664 sb->device_selection);
1665 if (cs < 0)
1666 return cs;
1667 spi->chip_select = cs;
1668 } else {
1669 spi->chip_select = sb->device_selection;
1670 }
1671
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001672 spi->max_speed_hz = sb->connection_speed;
1673
1674 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1675 spi->mode |= SPI_CPHA;
1676 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1677 spi->mode |= SPI_CPOL;
1678 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1679 spi->mode |= SPI_CS_HIGH;
1680 }
1681 } else if (spi->irq < 0) {
1682 struct resource r;
1683
1684 if (acpi_dev_resource_interrupt(ares, 0, &r))
1685 spi->irq = r.start;
1686 }
1687
1688 /* Always tell the ACPI core to skip this resource */
1689 return 1;
1690}
1691
Octavian Purdila7f244672016-07-08 19:13:11 +03001692static acpi_status acpi_register_spi_device(struct spi_master *master,
1693 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001694{
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001695 struct list_head resource_list;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001696 struct spi_device *spi;
1697 int ret;
1698
Octavian Purdila7f244672016-07-08 19:13:11 +03001699 if (acpi_bus_get_status(adev) || !adev->status.present ||
1700 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001701 return AE_OK;
1702
1703 spi = spi_alloc_device(master);
1704 if (!spi) {
1705 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1706 dev_name(&adev->dev));
1707 return AE_NO_MEMORY;
1708 }
1709
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001710 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001711 spi->irq = -1;
1712
1713 INIT_LIST_HEAD(&resource_list);
1714 ret = acpi_dev_get_resources(adev, &resource_list,
1715 acpi_spi_add_resource, spi);
1716 acpi_dev_free_resource_list(&resource_list);
1717
1718 if (ret < 0 || !spi->max_speed_hz) {
1719 spi_dev_put(spi);
1720 return AE_OK;
1721 }
1722
Christophe RICARD33ada672015-12-23 23:25:35 +01001723 if (spi->irq < 0)
1724 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1725
Octavian Purdila7f244672016-07-08 19:13:11 +03001726 acpi_device_set_enumerated(adev);
1727
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001728 adev->power.flags.ignore_parent = true;
Jarkko Nikulacf9eb392013-10-10 17:19:17 +03001729 strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias));
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001730 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001731 adev->power.flags.ignore_parent = false;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001732 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1733 dev_name(&adev->dev));
1734 spi_dev_put(spi);
1735 }
1736
1737 return AE_OK;
1738}
1739
Octavian Purdila7f244672016-07-08 19:13:11 +03001740static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1741 void *data, void **return_value)
1742{
1743 struct spi_master *master = data;
1744 struct acpi_device *adev;
1745
1746 if (acpi_bus_get_device(handle, &adev))
1747 return AE_OK;
1748
1749 return acpi_register_spi_device(master, adev);
1750}
1751
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001752static void acpi_register_spi_devices(struct spi_master *master)
1753{
1754 acpi_status status;
1755 acpi_handle handle;
1756
Rafael J. Wysocki29896172013-04-01 00:21:08 +00001757 handle = ACPI_HANDLE(master->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001758 if (!handle)
1759 return;
1760
1761 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1762 acpi_spi_add_device, NULL,
1763 master, NULL);
1764 if (ACPI_FAILURE(status))
1765 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1766}
1767#else
1768static inline void acpi_register_spi_devices(struct spi_master *master) {}
1769#endif /* CONFIG_ACPI */
1770
Tony Jones49dce682007-10-16 01:27:48 -07001771static void spi_master_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001772{
1773 struct spi_master *master;
1774
Tony Jones49dce682007-10-16 01:27:48 -07001775 master = container_of(dev, struct spi_master, dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001776 kfree(master);
1777}
1778
1779static struct class spi_master_class = {
1780 .name = "spi_master",
1781 .owner = THIS_MODULE,
Tony Jones49dce682007-10-16 01:27:48 -07001782 .dev_release = spi_master_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00001783 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08001784};
1785
1786
1787/**
1788 * spi_alloc_master - allocate SPI master controller
1789 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001790 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001791 * memory is in the driver_data field of the returned device,
David Brownell0c868462006-01-08 13:34:25 -08001792 * accessible with spi_master_get_devdata().
David Brownell33e34dc2007-05-08 00:32:21 -07001793 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001794 *
1795 * This call is used only by SPI master controller drivers, which are the
1796 * only ones directly touching chip registers. It's how they allocate
dmitry pervushinba1a0512006-05-20 15:00:14 -07001797 * an spi_master structure, prior to calling spi_register_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001798 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001799 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08001800 *
1801 * The caller is responsible for assigning the bus number and initializing
dmitry pervushinba1a0512006-05-20 15:00:14 -07001802 * the master's methods before calling spi_register_master(); and (after errors
Guenter Roecka394d632015-09-06 01:46:54 +03001803 * adding the device) calling spi_master_put() to prevent a memory leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001804 *
1805 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08001806 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001807struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
David Brownell8ae12a02006-01-08 13:34:19 -08001808{
1809 struct spi_master *master;
1810
David Brownell0c868462006-01-08 13:34:25 -08001811 if (!dev)
1812 return NULL;
1813
Jingoo Han5fe5f052013-10-14 10:31:51 +09001814 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -08001815 if (!master)
1816 return NULL;
1817
Tony Jones49dce682007-10-16 01:27:48 -07001818 device_initialize(&master->dev);
Grant Likely1e8a52e2012-05-19 23:42:08 -06001819 master->bus_num = -1;
1820 master->num_chipselect = 1;
Tony Jones49dce682007-10-16 01:27:48 -07001821 master->dev.class = &spi_master_class;
Johan Hovold157f38f2015-12-14 16:16:19 +01001822 master->dev.parent = dev;
Linus Walleijd7e2ee22016-04-11 13:51:03 +02001823 pm_suspend_ignore_children(&master->dev, true);
David Brownell0c868462006-01-08 13:34:25 -08001824 spi_master_set_devdata(master, &master[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001825
1826 return master;
1827}
1828EXPORT_SYMBOL_GPL(spi_alloc_master);
1829
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001830#ifdef CONFIG_OF
1831static int of_spi_register_master(struct spi_master *master)
1832{
Grant Likelye80beb22013-02-12 17:48:37 +00001833 int nb, i, *cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001834 struct device_node *np = master->dev.of_node;
1835
1836 if (!np)
1837 return 0;
1838
1839 nb = of_gpio_named_count(np, "cs-gpios");
Jingoo Han5fe5f052013-10-14 10:31:51 +09001840 master->num_chipselect = max_t(int, nb, master->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001841
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001842 /* Return error only for an incorrectly formed cs-gpios property */
1843 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001844 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001845 else if (nb < 0)
1846 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001847
1848 cs = devm_kzalloc(&master->dev,
1849 sizeof(int) * master->num_chipselect,
1850 GFP_KERNEL);
1851 master->cs_gpios = cs;
1852
1853 if (!master->cs_gpios)
1854 return -ENOMEM;
1855
Andreas Larsson0da83bb2013-01-29 15:53:40 +01001856 for (i = 0; i < master->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01001857 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001858
1859 for (i = 0; i < nb; i++)
1860 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1861
1862 return 0;
1863}
1864#else
1865static int of_spi_register_master(struct spi_master *master)
1866{
1867 return 0;
1868}
1869#endif
1870
David Brownell8ae12a02006-01-08 13:34:19 -08001871/**
1872 * spi_register_master - register SPI master controller
1873 * @master: initialized master, originally from spi_alloc_master()
David Brownell33e34dc2007-05-08 00:32:21 -07001874 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001875 *
1876 * SPI master controllers connect to their drivers using some non-SPI bus,
1877 * such as the platform bus. The final stage of probe() in that code
1878 * includes calling spi_register_master() to hook up to this SPI bus glue.
1879 *
1880 * SPI controllers use board specific (often SOC specific) bus numbers,
1881 * and board-specific addressing for SPI devices combines those numbers
1882 * with chip select numbers. Since SPI does not directly support dynamic
1883 * device identification, boards need configuration tables telling which
1884 * chip is at which address.
1885 *
1886 * This must be called from context that can sleep. It returns zero on
1887 * success, else a negative error code (dropping the master's refcount).
David Brownell0c868462006-01-08 13:34:25 -08001888 * After a successful return, the caller is responsible for calling
1889 * spi_unregister_master().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001890 *
1891 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08001892 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001893int spi_register_master(struct spi_master *master)
David Brownell8ae12a02006-01-08 13:34:19 -08001894{
David Brownelle44a45a2007-06-03 13:50:40 -07001895 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Tony Jones49dce682007-10-16 01:27:48 -07001896 struct device *dev = master->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08001897 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08001898 int status = -ENODEV;
1899 int dynamic = 0;
1900
David Brownell0c868462006-01-08 13:34:25 -08001901 if (!dev)
1902 return -ENODEV;
1903
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001904 status = of_spi_register_master(master);
1905 if (status)
1906 return status;
1907
David Brownell082c8cb2007-07-31 00:39:45 -07001908 /* even if it's just one always-selected device, there must
1909 * be at least one chipselect
1910 */
1911 if (master->num_chipselect == 0)
1912 return -EINVAL;
1913
Grant Likelybb297852012-12-21 19:32:09 +00001914 if ((master->bus_num < 0) && master->dev.of_node)
1915 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1916
David Brownell8ae12a02006-01-08 13:34:19 -08001917 /* convention: dynamically assigned bus IDs count down from the max */
David Brownella020ed72006-04-03 15:49:04 -07001918 if (master->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07001919 /* FIXME switch to an IDR based scheme, something like
1920 * I2C now uses, so we can't run out of "dynamic" IDs
1921 */
David Brownell8ae12a02006-01-08 13:34:19 -08001922 master->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08001923 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08001924 }
1925
Mark Brown5424d432014-12-10 17:40:53 +00001926 INIT_LIST_HEAD(&master->queue);
1927 spin_lock_init(&master->queue_lock);
Ernst Schwabcf32b712010-06-28 17:49:29 -07001928 spin_lock_init(&master->bus_lock_spinlock);
1929 mutex_init(&master->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001930 mutex_init(&master->io_mutex);
Ernst Schwabcf32b712010-06-28 17:49:29 -07001931 master->bus_lock_flag = 0;
Mark Brownb1589352013-10-05 11:50:40 +01001932 init_completion(&master->xfer_completion);
Mark Brown6ad45a22014-02-02 13:47:47 +00001933 if (!master->max_dma_len)
1934 master->max_dma_len = INT_MAX;
Ernst Schwabcf32b712010-06-28 17:49:29 -07001935
David Brownell8ae12a02006-01-08 13:34:19 -08001936 /* register the device, then userspace will see it.
1937 * registration fails if the bus ID is in use.
1938 */
Kay Sievers35f74fc2009-01-06 10:44:37 -08001939 dev_set_name(&master->dev, "spi%u", master->bus_num);
Tony Jones49dce682007-10-16 01:27:48 -07001940 status = device_add(&master->dev);
David Brownellb8852442006-01-08 13:34:23 -08001941 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08001942 goto done;
Kay Sievers35f74fc2009-01-06 10:44:37 -08001943 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
David Brownell8ae12a02006-01-08 13:34:19 -08001944 dynamic ? " (dynamic)" : "");
1945
Linus Walleijffbbdd212012-02-22 10:05:38 +01001946 /* If we're using a queued driver, start the queue */
1947 if (master->transfer)
1948 dev_info(dev, "master is unqueued, this is deprecated\n");
1949 else {
1950 status = spi_master_initialize_queue(master);
1951 if (status) {
Axel Line93b0722013-08-31 20:25:52 +08001952 device_del(&master->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001953 goto done;
1954 }
1955 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00001956 /* add statistics */
1957 spin_lock_init(&master->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001958
Feng Tang2b9603a2010-08-02 15:52:15 +08001959 mutex_lock(&board_lock);
1960 list_add_tail(&master->list, &spi_master_list);
1961 list_for_each_entry(bi, &board_list, list)
1962 spi_match_master_to_boardinfo(master, &bi->board_info);
1963 mutex_unlock(&board_lock);
1964
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001965 /* Register devices from the device tree and ACPI */
Anatolij Gustschin12b15e82010-07-27 22:35:58 +02001966 of_register_spi_devices(master);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001967 acpi_register_spi_devices(master);
David Brownell8ae12a02006-01-08 13:34:19 -08001968done:
1969 return status;
1970}
1971EXPORT_SYMBOL_GPL(spi_register_master);
1972
Mark Brown666d5b42013-08-31 18:50:52 +01001973static void devm_spi_unregister(struct device *dev, void *res)
1974{
1975 spi_unregister_master(*(struct spi_master **)res);
1976}
1977
1978/**
1979 * dev_spi_register_master - register managed SPI master controller
1980 * @dev: device managing SPI master
1981 * @master: initialized master, originally from spi_alloc_master()
1982 * Context: can sleep
1983 *
1984 * Register a SPI device as with spi_register_master() which will
1985 * automatically be unregister
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001986 *
1987 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01001988 */
1989int devm_spi_register_master(struct device *dev, struct spi_master *master)
1990{
1991 struct spi_master **ptr;
1992 int ret;
1993
1994 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
1995 if (!ptr)
1996 return -ENOMEM;
1997
1998 ret = spi_register_master(master);
Stephen Warren4b928942013-11-21 16:11:15 -07001999 if (!ret) {
Mark Brown666d5b42013-08-31 18:50:52 +01002000 *ptr = master;
2001 devres_add(dev, ptr);
2002 } else {
2003 devres_free(ptr);
2004 }
2005
2006 return ret;
2007}
2008EXPORT_SYMBOL_GPL(devm_spi_register_master);
2009
David Lamparter34860082010-08-30 23:54:17 +02002010static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002011{
David Lamparter34860082010-08-30 23:54:17 +02002012 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002013 return 0;
2014}
2015
2016/**
2017 * spi_unregister_master - unregister SPI master controller
2018 * @master: the master being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002019 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002020 *
2021 * This call is used only by SPI master controller drivers, which are the
2022 * only ones directly touching chip registers.
2023 *
2024 * This must be called from context that can sleep.
2025 */
2026void spi_unregister_master(struct spi_master *master)
2027{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002028 int dummy;
2029
Linus Walleijffbbdd212012-02-22 10:05:38 +01002030 if (master->queued) {
2031 if (spi_destroy_queue(master))
2032 dev_err(&master->dev, "queue remove failed\n");
2033 }
2034
Feng Tang2b9603a2010-08-02 15:52:15 +08002035 mutex_lock(&board_lock);
2036 list_del(&master->list);
2037 mutex_unlock(&board_lock);
2038
Sebastian Andrzej Siewior97dbf372010-12-21 17:24:31 -08002039 dummy = device_for_each_child(&master->dev, NULL, __unregister);
Tony Jones49dce682007-10-16 01:27:48 -07002040 device_unregister(&master->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08002041}
2042EXPORT_SYMBOL_GPL(spi_unregister_master);
2043
Linus Walleijffbbdd212012-02-22 10:05:38 +01002044int spi_master_suspend(struct spi_master *master)
2045{
2046 int ret;
2047
2048 /* Basically no-ops for non-queued masters */
2049 if (!master->queued)
2050 return 0;
2051
2052 ret = spi_stop_queue(master);
2053 if (ret)
2054 dev_err(&master->dev, "queue stop failed\n");
2055
2056 return ret;
2057}
2058EXPORT_SYMBOL_GPL(spi_master_suspend);
2059
2060int spi_master_resume(struct spi_master *master)
2061{
2062 int ret;
2063
2064 if (!master->queued)
2065 return 0;
2066
2067 ret = spi_start_queue(master);
2068 if (ret)
2069 dev_err(&master->dev, "queue restart failed\n");
2070
2071 return ret;
2072}
2073EXPORT_SYMBOL_GPL(spi_master_resume);
2074
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002075static int __spi_master_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002076{
2077 struct spi_master *m;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002078 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002079
2080 m = container_of(dev, struct spi_master, dev);
2081 return m->bus_num == *bus_num;
2082}
2083
David Brownell8ae12a02006-01-08 13:34:19 -08002084/**
2085 * spi_busnum_to_master - look up master associated with bus_num
2086 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002087 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002088 *
2089 * This call may be used with devices that are registered after
2090 * arch init time. It returns a refcounted pointer to the relevant
2091 * spi_master (which the caller must release), or NULL if there is
2092 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002093 *
2094 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002095 */
2096struct spi_master *spi_busnum_to_master(u16 bus_num)
2097{
Tony Jones49dce682007-10-16 01:27:48 -07002098 struct device *dev;
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08002099 struct spi_master *master = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002100
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002101 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Dave Young5ed2c832008-01-22 15:14:18 +08002102 __spi_master_match);
2103 if (dev)
2104 master = container_of(dev, struct spi_master, dev);
2105 /* reference got in class_find_device */
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08002106 return master;
David Brownell8ae12a02006-01-08 13:34:19 -08002107}
2108EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2109
Martin Sperld780c372015-12-14 15:20:18 +00002110/*-------------------------------------------------------------------------*/
2111
2112/* Core methods for SPI resource management */
2113
2114/**
2115 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2116 * during the processing of a spi_message while using
2117 * spi_transfer_one
2118 * @spi: the spi device for which we allocate memory
2119 * @release: the release code to execute for this resource
2120 * @size: size to alloc and return
2121 * @gfp: GFP allocation flags
2122 *
2123 * Return: the pointer to the allocated data
2124 *
2125 * This may get enhanced in the future to allocate from a memory pool
2126 * of the @spi_device or @spi_master to avoid repeated allocations.
2127 */
2128void *spi_res_alloc(struct spi_device *spi,
2129 spi_res_release_t release,
2130 size_t size, gfp_t gfp)
2131{
2132 struct spi_res *sres;
2133
2134 sres = kzalloc(sizeof(*sres) + size, gfp);
2135 if (!sres)
2136 return NULL;
2137
2138 INIT_LIST_HEAD(&sres->entry);
2139 sres->release = release;
2140
2141 return sres->data;
2142}
2143EXPORT_SYMBOL_GPL(spi_res_alloc);
2144
2145/**
2146 * spi_res_free - free an spi resource
2147 * @res: pointer to the custom data of a resource
2148 *
2149 */
2150void spi_res_free(void *res)
2151{
2152 struct spi_res *sres = container_of(res, struct spi_res, data);
2153
2154 if (!res)
2155 return;
2156
2157 WARN_ON(!list_empty(&sres->entry));
2158 kfree(sres);
2159}
2160EXPORT_SYMBOL_GPL(spi_res_free);
2161
2162/**
2163 * spi_res_add - add a spi_res to the spi_message
2164 * @message: the spi message
2165 * @res: the spi_resource
2166 */
2167void spi_res_add(struct spi_message *message, void *res)
2168{
2169 struct spi_res *sres = container_of(res, struct spi_res, data);
2170
2171 WARN_ON(!list_empty(&sres->entry));
2172 list_add_tail(&sres->entry, &message->resources);
2173}
2174EXPORT_SYMBOL_GPL(spi_res_add);
2175
2176/**
2177 * spi_res_release - release all spi resources for this message
2178 * @master: the @spi_master
2179 * @message: the @spi_message
2180 */
2181void spi_res_release(struct spi_master *master,
2182 struct spi_message *message)
2183{
2184 struct spi_res *res;
2185
2186 while (!list_empty(&message->resources)) {
2187 res = list_last_entry(&message->resources,
2188 struct spi_res, entry);
2189
2190 if (res->release)
2191 res->release(master, message, res->data);
2192
2193 list_del(&res->entry);
2194
2195 kfree(res);
2196 }
2197}
2198EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002199
2200/*-------------------------------------------------------------------------*/
2201
Martin Sperl523baf5a2015-12-14 15:20:19 +00002202/* Core methods for spi_message alterations */
2203
2204static void __spi_replace_transfers_release(struct spi_master *master,
2205 struct spi_message *msg,
2206 void *res)
2207{
2208 struct spi_replaced_transfers *rxfer = res;
2209 size_t i;
2210
2211 /* call extra callback if requested */
2212 if (rxfer->release)
2213 rxfer->release(master, msg, res);
2214
2215 /* insert replaced transfers back into the message */
2216 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2217
2218 /* remove the formerly inserted entries */
2219 for (i = 0; i < rxfer->inserted; i++)
2220 list_del(&rxfer->inserted_transfers[i].transfer_list);
2221}
2222
2223/**
2224 * spi_replace_transfers - replace transfers with several transfers
2225 * and register change with spi_message.resources
2226 * @msg: the spi_message we work upon
2227 * @xfer_first: the first spi_transfer we want to replace
2228 * @remove: number of transfers to remove
2229 * @insert: the number of transfers we want to insert instead
2230 * @release: extra release code necessary in some circumstances
2231 * @extradatasize: extra data to allocate (with alignment guarantees
2232 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002233 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002234 *
2235 * Returns: pointer to @spi_replaced_transfers,
2236 * PTR_ERR(...) in case of errors.
2237 */
2238struct spi_replaced_transfers *spi_replace_transfers(
2239 struct spi_message *msg,
2240 struct spi_transfer *xfer_first,
2241 size_t remove,
2242 size_t insert,
2243 spi_replaced_release_t release,
2244 size_t extradatasize,
2245 gfp_t gfp)
2246{
2247 struct spi_replaced_transfers *rxfer;
2248 struct spi_transfer *xfer;
2249 size_t i;
2250
2251 /* allocate the structure using spi_res */
2252 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2253 insert * sizeof(struct spi_transfer)
2254 + sizeof(struct spi_replaced_transfers)
2255 + extradatasize,
2256 gfp);
2257 if (!rxfer)
2258 return ERR_PTR(-ENOMEM);
2259
2260 /* the release code to invoke before running the generic release */
2261 rxfer->release = release;
2262
2263 /* assign extradata */
2264 if (extradatasize)
2265 rxfer->extradata =
2266 &rxfer->inserted_transfers[insert];
2267
2268 /* init the replaced_transfers list */
2269 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2270
2271 /* assign the list_entry after which we should reinsert
2272 * the @replaced_transfers - it may be spi_message.messages!
2273 */
2274 rxfer->replaced_after = xfer_first->transfer_list.prev;
2275
2276 /* remove the requested number of transfers */
2277 for (i = 0; i < remove; i++) {
2278 /* if the entry after replaced_after it is msg->transfers
2279 * then we have been requested to remove more transfers
2280 * than are in the list
2281 */
2282 if (rxfer->replaced_after->next == &msg->transfers) {
2283 dev_err(&msg->spi->dev,
2284 "requested to remove more spi_transfers than are available\n");
2285 /* insert replaced transfers back into the message */
2286 list_splice(&rxfer->replaced_transfers,
2287 rxfer->replaced_after);
2288
2289 /* free the spi_replace_transfer structure */
2290 spi_res_free(rxfer);
2291
2292 /* and return with an error */
2293 return ERR_PTR(-EINVAL);
2294 }
2295
2296 /* remove the entry after replaced_after from list of
2297 * transfers and add it to list of replaced_transfers
2298 */
2299 list_move_tail(rxfer->replaced_after->next,
2300 &rxfer->replaced_transfers);
2301 }
2302
2303 /* create copy of the given xfer with identical settings
2304 * based on the first transfer to get removed
2305 */
2306 for (i = 0; i < insert; i++) {
2307 /* we need to run in reverse order */
2308 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2309
2310 /* copy all spi_transfer data */
2311 memcpy(xfer, xfer_first, sizeof(*xfer));
2312
2313 /* add to list */
2314 list_add(&xfer->transfer_list, rxfer->replaced_after);
2315
2316 /* clear cs_change and delay_usecs for all but the last */
2317 if (i) {
2318 xfer->cs_change = false;
2319 xfer->delay_usecs = 0;
2320 }
2321 }
2322
2323 /* set up inserted */
2324 rxfer->inserted = insert;
2325
2326 /* and register it with spi_res/spi_message */
2327 spi_res_add(msg, rxfer);
2328
2329 return rxfer;
2330}
2331EXPORT_SYMBOL_GPL(spi_replace_transfers);
2332
Fabio Estevam08933412016-02-14 13:33:50 -02002333static int __spi_split_transfer_maxsize(struct spi_master *master,
2334 struct spi_message *msg,
2335 struct spi_transfer **xferp,
2336 size_t maxsize,
2337 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00002338{
2339 struct spi_transfer *xfer = *xferp, *xfers;
2340 struct spi_replaced_transfers *srt;
2341 size_t offset;
2342 size_t count, i;
2343
2344 /* warn once about this fact that we are splitting a transfer */
2345 dev_warn_once(&msg->spi->dev,
Fabio Estevam7d62f512016-02-17 15:42:27 -02002346 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
Martin Sperld9f12122015-12-14 15:20:20 +00002347 xfer->len, maxsize);
2348
2349 /* calculate how many we have to replace */
2350 count = DIV_ROUND_UP(xfer->len, maxsize);
2351
2352 /* create replacement */
2353 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03002354 if (IS_ERR(srt))
2355 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00002356 xfers = srt->inserted_transfers;
2357
2358 /* now handle each of those newly inserted spi_transfers
2359 * note that the replacements spi_transfers all are preset
2360 * to the same values as *xferp, so tx_buf, rx_buf and len
2361 * are all identical (as well as most others)
2362 * so we just have to fix up len and the pointers.
2363 *
2364 * this also includes support for the depreciated
2365 * spi_message.is_dma_mapped interface
2366 */
2367
2368 /* the first transfer just needs the length modified, so we
2369 * run it outside the loop
2370 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02002371 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00002372
2373 /* all the others need rx_buf/tx_buf also set */
2374 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2375 /* update rx_buf, tx_buf and dma */
2376 if (xfers[i].rx_buf)
2377 xfers[i].rx_buf += offset;
2378 if (xfers[i].rx_dma)
2379 xfers[i].rx_dma += offset;
2380 if (xfers[i].tx_buf)
2381 xfers[i].tx_buf += offset;
2382 if (xfers[i].tx_dma)
2383 xfers[i].tx_dma += offset;
2384
2385 /* update length */
2386 xfers[i].len = min(maxsize, xfers[i].len - offset);
2387 }
2388
2389 /* we set up xferp to the last entry we have inserted,
2390 * so that we skip those already split transfers
2391 */
2392 *xferp = &xfers[count - 1];
2393
2394 /* increment statistics counters */
2395 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2396 transfers_split_maxsize);
2397 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2398 transfers_split_maxsize);
2399
2400 return 0;
2401}
2402
2403/**
2404 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2405 * when an individual transfer exceeds a
2406 * certain size
2407 * @master: the @spi_master for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09002408 * @msg: the @spi_message to transform
2409 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03002410 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00002411 *
2412 * Return: status of transformation
2413 */
2414int spi_split_transfers_maxsize(struct spi_master *master,
2415 struct spi_message *msg,
2416 size_t maxsize,
2417 gfp_t gfp)
2418{
2419 struct spi_transfer *xfer;
2420 int ret;
2421
2422 /* iterate over the transfer_list,
2423 * but note that xfer is advanced to the last transfer inserted
2424 * to avoid checking sizes again unnecessarily (also xfer does
2425 * potentiall belong to a different list by the time the
2426 * replacement has happened
2427 */
2428 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2429 if (xfer->len > maxsize) {
2430 ret = __spi_split_transfer_maxsize(
2431 master, msg, &xfer, maxsize, gfp);
2432 if (ret)
2433 return ret;
2434 }
2435 }
2436
2437 return 0;
2438}
2439EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08002440
2441/*-------------------------------------------------------------------------*/
2442
David Brownell7d077192009-06-17 16:26:03 -07002443/* Core methods for SPI master protocol drivers. Some of the
2444 * other core methods are currently defined as inline functions.
2445 */
2446
Stefan Brüns63ab6452015-08-23 16:06:30 +02002447static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
2448{
2449 if (master->bits_per_word_mask) {
2450 /* Only 32 bits fit in the mask */
2451 if (bits_per_word > 32)
2452 return -EINVAL;
2453 if (!(master->bits_per_word_mask &
2454 SPI_BPW_MASK(bits_per_word)))
2455 return -EINVAL;
2456 }
2457
2458 return 0;
2459}
2460
David Brownell7d077192009-06-17 16:26:03 -07002461/**
2462 * spi_setup - setup SPI mode and clock rate
2463 * @spi: the device whose settings are being modified
2464 * Context: can sleep, and no requests are queued to the device
2465 *
2466 * SPI protocol drivers may need to update the transfer mode if the
2467 * device doesn't work with its default. They may likewise need
2468 * to update clock rates or word sizes from initial values. This function
2469 * changes those settings, and must be called from a context that can sleep.
2470 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
2471 * effect the next time the device is selected and data is transferred to
2472 * or from it. When this function returns, the spi device is deselected.
2473 *
2474 * Note that this call will fail if the protocol driver specifies an option
2475 * that the underlying controller or its driver does not support. For
2476 * example, not all hardware supports wire transfers using nine bit words,
2477 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002478 *
2479 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07002480 */
2481int spi_setup(struct spi_device *spi)
2482{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002483 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002484 int status;
David Brownell7d077192009-06-17 16:26:03 -07002485
wangyuhangf477b7f2013-08-11 18:15:17 +08002486 /* check mode to prevent that DUAL and QUAD set at the same time
2487 */
2488 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
2489 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
2490 dev_err(&spi->dev,
2491 "setup: can not select dual and quad at the same time\n");
2492 return -EINVAL;
2493 }
2494 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
2495 */
2496 if ((spi->mode & SPI_3WIRE) && (spi->mode &
2497 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
2498 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07002499 /* help drivers fail *cleanly* when they need options
2500 * that aren't supported with their current master
2501 */
2502 bad_bits = spi->mode & ~spi->master->mode_bits;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002503 ugly_bits = bad_bits &
2504 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
2505 if (ugly_bits) {
2506 dev_warn(&spi->dev,
2507 "setup: ignoring unsupported mode bits %x\n",
2508 ugly_bits);
2509 spi->mode &= ~ugly_bits;
2510 bad_bits &= ~ugly_bits;
2511 }
David Brownelle7db06b2009-06-17 16:26:04 -07002512 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02002513 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07002514 bad_bits);
2515 return -EINVAL;
2516 }
2517
David Brownell7d077192009-06-17 16:26:03 -07002518 if (!spi->bits_per_word)
2519 spi->bits_per_word = 8;
2520
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002521 status = __spi_validate_bits_per_word(spi->master, spi->bits_per_word);
2522 if (status)
2523 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02002524
Axel Lin052eb2d42014-02-10 00:08:05 +08002525 if (!spi->max_speed_hz)
2526 spi->max_speed_hz = spi->master->max_speed_hz;
2527
Laxman Dewangancaae0702012-11-09 14:35:22 +05302528 if (spi->master->setup)
2529 status = spi->master->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07002530
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05002531 spi_set_cs(spi, false);
2532
Jingoo Han5fe5f052013-10-14 10:31:51 +09002533 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 -07002534 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2535 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2536 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2537 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2538 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2539 spi->bits_per_word, spi->max_speed_hz,
2540 status);
2541
2542 return status;
2543}
2544EXPORT_SYMBOL_GPL(spi_setup);
2545
Mark Brown90808732013-11-13 23:44:15 +00002546static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b712010-06-28 17:49:29 -07002547{
2548 struct spi_master *master = spi->master;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302549 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002550 int w_size;
Ernst Schwabcf32b712010-06-28 17:49:29 -07002551
Mark Brown24a00132013-07-10 15:05:40 +01002552 if (list_empty(&message->transfers))
2553 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01002554
Ernst Schwabcf32b712010-06-28 17:49:29 -07002555 /* Half-duplex links include original MicroWire, and ones with
2556 * only one data pin like SPI_3WIRE (switches direction) or where
2557 * either MOSI or MISO is missing. They can also be caused by
2558 * software limitations.
2559 */
2560 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
2561 || (spi->mode & SPI_3WIRE)) {
Ernst Schwabcf32b712010-06-28 17:49:29 -07002562 unsigned flags = master->flags;
2563
2564 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2565 if (xfer->rx_buf && xfer->tx_buf)
2566 return -EINVAL;
2567 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
2568 return -EINVAL;
2569 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
2570 return -EINVAL;
2571 }
2572 }
2573
Laxman Dewangane6811d12012-11-09 14:36:45 +05302574 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302575 * Set transfer bits_per_word and max speed as spi device default if
2576 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08002577 * Set transfer tx_nbits and rx_nbits as single transfer default
2578 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05302579 */
Martin Sperl77e80582015-11-27 12:31:09 +00002580 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302581 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05302582 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302583 if (!xfer->bits_per_word)
2584 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08002585
2586 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302587 xfer->speed_hz = spi->max_speed_hz;
Mark Brown7dc9fbc2015-08-20 11:52:18 -07002588 if (!xfer->speed_hz)
2589 xfer->speed_hz = master->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08002590
2591 if (master->max_speed_hz &&
2592 xfer->speed_hz > master->max_speed_hz)
2593 xfer->speed_hz = master->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02002594
Stefan Brüns63ab6452015-08-23 16:06:30 +02002595 if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
2596 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01002597
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002598 /*
2599 * SPI transfer length should be multiple of SPI word size
2600 * where SPI word size should be power-of-two multiple
2601 */
2602 if (xfer->bits_per_word <= 8)
2603 w_size = 1;
2604 else if (xfer->bits_per_word <= 16)
2605 w_size = 2;
2606 else
2607 w_size = 4;
2608
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002609 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002610 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002611 return -EINVAL;
2612
Mark Browna2fd4f92013-07-10 14:57:26 +01002613 if (xfer->speed_hz && master->min_speed_hz &&
2614 xfer->speed_hz < master->min_speed_hz)
2615 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08002616
2617 if (xfer->tx_buf && !xfer->tx_nbits)
2618 xfer->tx_nbits = SPI_NBITS_SINGLE;
2619 if (xfer->rx_buf && !xfer->rx_nbits)
2620 xfer->rx_nbits = SPI_NBITS_SINGLE;
2621 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01002622 * 1. check the value matches one of single, dual and quad
2623 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08002624 */
Sourav Poddardb90a442013-08-22 21:20:48 +05302625 if (xfer->tx_buf) {
2626 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2627 xfer->tx_nbits != SPI_NBITS_DUAL &&
2628 xfer->tx_nbits != SPI_NBITS_QUAD)
2629 return -EINVAL;
2630 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2631 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2632 return -EINVAL;
2633 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2634 !(spi->mode & SPI_TX_QUAD))
2635 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302636 }
wangyuhangf477b7f2013-08-11 18:15:17 +08002637 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05302638 if (xfer->rx_buf) {
2639 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2640 xfer->rx_nbits != SPI_NBITS_DUAL &&
2641 xfer->rx_nbits != SPI_NBITS_QUAD)
2642 return -EINVAL;
2643 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2644 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2645 return -EINVAL;
2646 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2647 !(spi->mode & SPI_RX_QUAD))
2648 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302649 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05302650 }
2651
Ernst Schwabcf32b712010-06-28 17:49:29 -07002652 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00002653
2654 return 0;
2655}
2656
2657static int __spi_async(struct spi_device *spi, struct spi_message *message)
2658{
2659 struct spi_master *master = spi->master;
2660
2661 message->spi = spi;
2662
Martin Sperleca2ebc2015-06-22 13:00:36 +00002663 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
2664 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2665
Mark Brown90808732013-11-13 23:44:15 +00002666 trace_spi_message_submit(message);
2667
Ernst Schwabcf32b712010-06-28 17:49:29 -07002668 return master->transfer(spi, message);
2669}
2670
David Brownell568d0692009-09-22 16:46:18 -07002671/**
2672 * spi_async - asynchronous SPI transfer
2673 * @spi: device with which data will be exchanged
2674 * @message: describes the data transfers, including completion callback
2675 * Context: any (irqs may be blocked, etc)
2676 *
2677 * This call may be used in_irq and other contexts which can't sleep,
2678 * as well as from task contexts which can sleep.
2679 *
2680 * The completion callback is invoked in a context which can't sleep.
2681 * Before that invocation, the value of message->status is undefined.
2682 * When the callback is issued, message->status holds either zero (to
2683 * indicate complete success) or a negative error code. After that
2684 * callback returns, the driver which issued the transfer request may
2685 * deallocate the associated memory; it's no longer in use by any SPI
2686 * core or controller driver code.
2687 *
2688 * Note that although all messages to a spi_device are handled in
2689 * FIFO order, messages may go to different devices in other orders.
2690 * Some device might be higher priority, or have various "hard" access
2691 * time requirements, for example.
2692 *
2693 * On detection of any fault during the transfer, processing of
2694 * the entire message is aborted, and the device is deselected.
2695 * Until returning from the associated message completion callback,
2696 * no other spi_message queued to that device will be processed.
2697 * (This rule applies equally to all the synchronous transfer calls,
2698 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002699 *
2700 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07002701 */
2702int spi_async(struct spi_device *spi, struct spi_message *message)
2703{
2704 struct spi_master *master = spi->master;
Ernst Schwabcf32b712010-06-28 17:49:29 -07002705 int ret;
2706 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07002707
Mark Brown90808732013-11-13 23:44:15 +00002708 ret = __spi_validate(spi, message);
2709 if (ret != 0)
2710 return ret;
2711
Ernst Schwabcf32b712010-06-28 17:49:29 -07002712 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07002713
Ernst Schwabcf32b712010-06-28 17:49:29 -07002714 if (master->bus_lock_flag)
2715 ret = -EBUSY;
2716 else
2717 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07002718
Ernst Schwabcf32b712010-06-28 17:49:29 -07002719 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2720
2721 return ret;
David Brownell568d0692009-09-22 16:46:18 -07002722}
2723EXPORT_SYMBOL_GPL(spi_async);
2724
Ernst Schwabcf32b712010-06-28 17:49:29 -07002725/**
2726 * spi_async_locked - version of spi_async with exclusive bus usage
2727 * @spi: device with which data will be exchanged
2728 * @message: describes the data transfers, including completion callback
2729 * Context: any (irqs may be blocked, etc)
2730 *
2731 * This call may be used in_irq and other contexts which can't sleep,
2732 * as well as from task contexts which can sleep.
2733 *
2734 * The completion callback is invoked in a context which can't sleep.
2735 * Before that invocation, the value of message->status is undefined.
2736 * When the callback is issued, message->status holds either zero (to
2737 * indicate complete success) or a negative error code. After that
2738 * callback returns, the driver which issued the transfer request may
2739 * deallocate the associated memory; it's no longer in use by any SPI
2740 * core or controller driver code.
2741 *
2742 * Note that although all messages to a spi_device are handled in
2743 * FIFO order, messages may go to different devices in other orders.
2744 * Some device might be higher priority, or have various "hard" access
2745 * time requirements, for example.
2746 *
2747 * On detection of any fault during the transfer, processing of
2748 * the entire message is aborted, and the device is deselected.
2749 * Until returning from the associated message completion callback,
2750 * no other spi_message queued to that device will be processed.
2751 * (This rule applies equally to all the synchronous transfer calls,
2752 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002753 *
2754 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b712010-06-28 17:49:29 -07002755 */
2756int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2757{
2758 struct spi_master *master = spi->master;
2759 int ret;
2760 unsigned long flags;
2761
Mark Brown90808732013-11-13 23:44:15 +00002762 ret = __spi_validate(spi, message);
2763 if (ret != 0)
2764 return ret;
2765
Ernst Schwabcf32b712010-06-28 17:49:29 -07002766 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2767
2768 ret = __spi_async(spi, message);
2769
2770 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2771
2772 return ret;
2773
2774}
2775EXPORT_SYMBOL_GPL(spi_async_locked);
2776
David Brownell7d077192009-06-17 16:26:03 -07002777
Vignesh R556351f2015-12-11 09:39:56 +05302778int spi_flash_read(struct spi_device *spi,
2779 struct spi_flash_read_message *msg)
2780
2781{
2782 struct spi_master *master = spi->master;
Vignesh Rf4502dd2016-06-08 12:18:31 +05302783 struct device *rx_dev = NULL;
Vignesh R556351f2015-12-11 09:39:56 +05302784 int ret;
2785
2786 if ((msg->opcode_nbits == SPI_NBITS_DUAL ||
2787 msg->addr_nbits == SPI_NBITS_DUAL) &&
2788 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2789 return -EINVAL;
2790 if ((msg->opcode_nbits == SPI_NBITS_QUAD ||
2791 msg->addr_nbits == SPI_NBITS_QUAD) &&
2792 !(spi->mode & SPI_TX_QUAD))
2793 return -EINVAL;
2794 if (msg->data_nbits == SPI_NBITS_DUAL &&
2795 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2796 return -EINVAL;
2797 if (msg->data_nbits == SPI_NBITS_QUAD &&
2798 !(spi->mode & SPI_RX_QUAD))
2799 return -EINVAL;
2800
2801 if (master->auto_runtime_pm) {
2802 ret = pm_runtime_get_sync(master->dev.parent);
2803 if (ret < 0) {
2804 dev_err(&master->dev, "Failed to power device: %d\n",
2805 ret);
2806 return ret;
2807 }
2808 }
Vignesh Rf4502dd2016-06-08 12:18:31 +05302809
Vignesh R556351f2015-12-11 09:39:56 +05302810 mutex_lock(&master->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01002811 mutex_lock(&master->io_mutex);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302812 if (master->dma_rx) {
2813 rx_dev = master->dma_rx->device->dev;
2814 ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
2815 msg->buf, msg->len,
2816 DMA_FROM_DEVICE);
2817 if (!ret)
2818 msg->cur_msg_mapped = true;
2819 }
Vignesh R556351f2015-12-11 09:39:56 +05302820 ret = master->spi_flash_read(spi, msg);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302821 if (msg->cur_msg_mapped)
2822 spi_unmap_buf(master, rx_dev, &msg->rx_sg,
2823 DMA_FROM_DEVICE);
Mark Brownef4d96e2016-07-21 23:53:31 +01002824 mutex_unlock(&master->io_mutex);
Vignesh R556351f2015-12-11 09:39:56 +05302825 mutex_unlock(&master->bus_lock_mutex);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302826
Vignesh R556351f2015-12-11 09:39:56 +05302827 if (master->auto_runtime_pm)
2828 pm_runtime_put(master->dev.parent);
2829
2830 return ret;
2831}
2832EXPORT_SYMBOL_GPL(spi_flash_read);
2833
David Brownell7d077192009-06-17 16:26:03 -07002834/*-------------------------------------------------------------------------*/
2835
2836/* Utility methods for SPI master protocol drivers, layered on
2837 * top of the core. Some other utility methods are defined as
2838 * inline functions.
2839 */
2840
Andrew Morton5d870c82006-01-11 11:23:49 -08002841static void spi_complete(void *arg)
2842{
2843 complete(arg);
2844}
2845
Mark Brownef4d96e2016-07-21 23:53:31 +01002846static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b712010-06-28 17:49:29 -07002847{
2848 DECLARE_COMPLETION_ONSTACK(done);
2849 int status;
2850 struct spi_master *master = spi->master;
Mark Brown0461a412014-12-09 21:38:05 +00002851 unsigned long flags;
2852
2853 status = __spi_validate(spi, message);
2854 if (status != 0)
2855 return status;
Ernst Schwabcf32b712010-06-28 17:49:29 -07002856
2857 message->complete = spi_complete;
2858 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00002859 message->spi = spi;
Ernst Schwabcf32b712010-06-28 17:49:29 -07002860
Martin Sperleca2ebc2015-06-22 13:00:36 +00002861 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
2862 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
2863
Mark Brown0461a412014-12-09 21:38:05 +00002864 /* If we're not using the legacy transfer method then we will
2865 * try to transfer in the calling context so special case.
2866 * This code would be less tricky if we could remove the
2867 * support for driver implemented message queues.
2868 */
2869 if (master->transfer == spi_queued_transfer) {
2870 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2871
2872 trace_spi_message_submit(message);
2873
2874 status = __spi_queued_transfer(spi, message, false);
2875
2876 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2877 } else {
2878 status = spi_async_locked(spi, message);
2879 }
Ernst Schwabcf32b712010-06-28 17:49:29 -07002880
Ernst Schwabcf32b712010-06-28 17:49:29 -07002881 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00002882 /* Push out the messages in the calling context if we
2883 * can.
2884 */
Martin Sperleca2ebc2015-06-22 13:00:36 +00002885 if (master->transfer == spi_queued_transfer) {
2886 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2887 spi_sync_immediate);
2888 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
2889 spi_sync_immediate);
Mark Brownef4d96e2016-07-21 23:53:31 +01002890 __spi_pump_messages(master, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00002891 }
Mark Brown0461a412014-12-09 21:38:05 +00002892
Ernst Schwabcf32b712010-06-28 17:49:29 -07002893 wait_for_completion(&done);
2894 status = message->status;
2895 }
2896 message->context = NULL;
2897 return status;
2898}
2899
David Brownell8ae12a02006-01-08 13:34:19 -08002900/**
2901 * spi_sync - blocking/synchronous SPI data transfers
2902 * @spi: device with which data will be exchanged
2903 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07002904 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002905 *
2906 * This call may only be used from a context that may sleep. The sleep
2907 * is non-interruptible, and has no timeout. Low-overhead controller
2908 * drivers may DMA directly into and out of the message buffers.
2909 *
2910 * Note that the SPI device's chip select is active during the message,
2911 * and then is normally disabled between messages. Drivers for some
2912 * frequently-used devices may want to minimize costs of selecting a chip,
2913 * by leaving it selected in anticipation that the next message will go
2914 * to the same chip. (That may increase power usage.)
2915 *
David Brownell0c868462006-01-08 13:34:25 -08002916 * Also, the caller is guaranteeing that the memory associated with the
2917 * message will not be freed before this call returns.
2918 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002919 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002920 */
2921int spi_sync(struct spi_device *spi, struct spi_message *message)
2922{
Mark Brownef4d96e2016-07-21 23:53:31 +01002923 int ret;
2924
2925 mutex_lock(&spi->master->bus_lock_mutex);
2926 ret = __spi_sync(spi, message);
2927 mutex_unlock(&spi->master->bus_lock_mutex);
2928
2929 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08002930}
2931EXPORT_SYMBOL_GPL(spi_sync);
2932
Ernst Schwabcf32b712010-06-28 17:49:29 -07002933/**
2934 * spi_sync_locked - version of spi_sync with exclusive bus usage
2935 * @spi: device with which data will be exchanged
2936 * @message: describes the data transfers
2937 * Context: can sleep
2938 *
2939 * This call may only be used from a context that may sleep. The sleep
2940 * is non-interruptible, and has no timeout. Low-overhead controller
2941 * drivers may DMA directly into and out of the message buffers.
2942 *
2943 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002944 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b712010-06-28 17:49:29 -07002945 * be released by a spi_bus_unlock call when the exclusive access is over.
2946 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002947 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b712010-06-28 17:49:29 -07002948 */
2949int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2950{
Mark Brownef4d96e2016-07-21 23:53:31 +01002951 return __spi_sync(spi, message);
Ernst Schwabcf32b712010-06-28 17:49:29 -07002952}
2953EXPORT_SYMBOL_GPL(spi_sync_locked);
2954
2955/**
2956 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
2957 * @master: SPI bus master that should be locked for exclusive bus access
2958 * Context: can sleep
2959 *
2960 * This call may only be used from a context that may sleep. The sleep
2961 * is non-interruptible, and has no timeout.
2962 *
2963 * This call should be used by drivers that require exclusive access to the
2964 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
2965 * exclusive access is over. Data transfer must be done by spi_sync_locked
2966 * and spi_async_locked calls when the SPI bus lock is held.
2967 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002968 * Return: always zero.
Ernst Schwabcf32b712010-06-28 17:49:29 -07002969 */
2970int spi_bus_lock(struct spi_master *master)
2971{
2972 unsigned long flags;
2973
2974 mutex_lock(&master->bus_lock_mutex);
2975
2976 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2977 master->bus_lock_flag = 1;
2978 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2979
2980 /* mutex remains locked until spi_bus_unlock is called */
2981
2982 return 0;
2983}
2984EXPORT_SYMBOL_GPL(spi_bus_lock);
2985
2986/**
2987 * spi_bus_unlock - release the lock for exclusive SPI bus usage
2988 * @master: SPI bus master that was locked for exclusive bus access
2989 * Context: can sleep
2990 *
2991 * This call may only be used from a context that may sleep. The sleep
2992 * is non-interruptible, and has no timeout.
2993 *
2994 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
2995 * call.
2996 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002997 * Return: always zero.
Ernst Schwabcf32b712010-06-28 17:49:29 -07002998 */
2999int spi_bus_unlock(struct spi_master *master)
3000{
3001 master->bus_lock_flag = 0;
3002
3003 mutex_unlock(&master->bus_lock_mutex);
3004
3005 return 0;
3006}
3007EXPORT_SYMBOL_GPL(spi_bus_unlock);
3008
David Brownella9948b62006-04-02 10:37:40 -08003009/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003010#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003011
3012static u8 *buf;
3013
3014/**
3015 * spi_write_then_read - SPI synchronous write followed by read
3016 * @spi: device with which data will be exchanged
3017 * @txbuf: data to be written (need not be dma-safe)
3018 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003019 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3020 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003021 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003022 *
3023 * This performs a half duplex MicroWire style transaction with the
3024 * device, sending txbuf and then reading rxbuf. The return value
3025 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003026 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003027 *
David Brownell0c868462006-01-08 13:34:25 -08003028 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003029 * portable code should never use this for more than 32 bytes.
3030 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c868462006-01-08 13:34:25 -08003031 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003032 *
3033 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003034 */
3035int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003036 const void *txbuf, unsigned n_tx,
3037 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003038{
David Brownell068f4072007-12-04 23:45:09 -08003039 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003040
3041 int status;
3042 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003043 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003044 u8 *local_buf;
3045
Mark Brownb3a223e2012-12-02 12:54:25 +09003046 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3047 * copying here, (as a pure convenience thing), but we can
3048 * keep heap costs out of the hot path unless someone else is
3049 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003050 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003051 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003052 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3053 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003054 if (!local_buf)
3055 return -ENOMEM;
3056 } else {
3057 local_buf = buf;
3058 }
David Brownell8ae12a02006-01-08 13:34:19 -08003059
Vitaly Wool8275c642006-01-08 13:34:28 -08003060 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003061 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003062 if (n_tx) {
3063 x[0].len = n_tx;
3064 spi_message_add_tail(&x[0], &message);
3065 }
3066 if (n_rx) {
3067 x[1].len = n_rx;
3068 spi_message_add_tail(&x[1], &message);
3069 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003070
David Brownell8ae12a02006-01-08 13:34:19 -08003071 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003072 x[0].tx_buf = local_buf;
3073 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003074
3075 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003076 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003077 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003078 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003079
David Brownellbdff5492009-04-13 14:39:57 -07003080 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003081 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003082 else
3083 kfree(local_buf);
3084
3085 return status;
3086}
3087EXPORT_SYMBOL_GPL(spi_write_then_read);
3088
3089/*-------------------------------------------------------------------------*/
3090
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003091#if IS_ENABLED(CONFIG_OF_DYNAMIC)
3092static int __spi_of_device_match(struct device *dev, void *data)
3093{
3094 return dev->of_node == data;
3095}
3096
3097/* must call put_device() when done with returned spi_device device */
3098static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
3099{
3100 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3101 __spi_of_device_match);
3102 return dev ? to_spi_device(dev) : NULL;
3103}
3104
3105static int __spi_of_master_match(struct device *dev, const void *data)
3106{
3107 return dev->of_node == data;
3108}
3109
3110/* the spi masters are not using spi_bus, so we find it with another way */
3111static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
3112{
3113 struct device *dev;
3114
3115 dev = class_find_device(&spi_master_class, NULL, node,
3116 __spi_of_master_match);
3117 if (!dev)
3118 return NULL;
3119
3120 /* reference got in class_find_device */
3121 return container_of(dev, struct spi_master, dev);
3122}
3123
3124static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3125 void *arg)
3126{
3127 struct of_reconfig_data *rd = arg;
3128 struct spi_master *master;
3129 struct spi_device *spi;
3130
3131 switch (of_reconfig_get_state_change(action, arg)) {
3132 case OF_RECONFIG_CHANGE_ADD:
3133 master = of_find_spi_master_by_node(rd->dn->parent);
3134 if (master == NULL)
3135 return NOTIFY_OK; /* not for us */
3136
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003137 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
3138 put_device(&master->dev);
3139 return NOTIFY_OK;
3140 }
3141
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003142 spi = of_register_spi_device(master, rd->dn);
3143 put_device(&master->dev);
3144
3145 if (IS_ERR(spi)) {
3146 pr_err("%s: failed to create for '%s'\n",
3147 __func__, rd->dn->full_name);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003148 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003149 return notifier_from_errno(PTR_ERR(spi));
3150 }
3151 break;
3152
3153 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003154 /* already depopulated? */
3155 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3156 return NOTIFY_OK;
3157
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003158 /* find our device by node */
3159 spi = of_find_spi_device_by_node(rd->dn);
3160 if (spi == NULL)
3161 return NOTIFY_OK; /* no? not meant for us */
3162
3163 /* unregister takes one ref away */
3164 spi_unregister_device(spi);
3165
3166 /* and put the reference of the find */
3167 put_device(&spi->dev);
3168 break;
3169 }
3170
3171 return NOTIFY_OK;
3172}
3173
3174static struct notifier_block spi_of_notifier = {
3175 .notifier_call = of_spi_notify,
3176};
3177#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3178extern struct notifier_block spi_of_notifier;
3179#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3180
Octavian Purdila7f244672016-07-08 19:13:11 +03003181#if IS_ENABLED(CONFIG_ACPI)
3182static int spi_acpi_master_match(struct device *dev, const void *data)
3183{
3184 return ACPI_COMPANION(dev->parent) == data;
3185}
3186
3187static int spi_acpi_device_match(struct device *dev, void *data)
3188{
3189 return ACPI_COMPANION(dev) == data;
3190}
3191
3192static struct spi_master *acpi_spi_find_master_by_adev(struct acpi_device *adev)
3193{
3194 struct device *dev;
3195
3196 dev = class_find_device(&spi_master_class, NULL, adev,
3197 spi_acpi_master_match);
3198 if (!dev)
3199 return NULL;
3200
3201 return container_of(dev, struct spi_master, dev);
3202}
3203
3204static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3205{
3206 struct device *dev;
3207
3208 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3209
3210 return dev ? to_spi_device(dev) : NULL;
3211}
3212
3213static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3214 void *arg)
3215{
3216 struct acpi_device *adev = arg;
3217 struct spi_master *master;
3218 struct spi_device *spi;
3219
3220 switch (value) {
3221 case ACPI_RECONFIG_DEVICE_ADD:
3222 master = acpi_spi_find_master_by_adev(adev->parent);
3223 if (!master)
3224 break;
3225
3226 acpi_register_spi_device(master, adev);
3227 put_device(&master->dev);
3228 break;
3229 case ACPI_RECONFIG_DEVICE_REMOVE:
3230 if (!acpi_device_enumerated(adev))
3231 break;
3232
3233 spi = acpi_spi_find_device_by_adev(adev);
3234 if (!spi)
3235 break;
3236
3237 spi_unregister_device(spi);
3238 put_device(&spi->dev);
3239 break;
3240 }
3241
3242 return NOTIFY_OK;
3243}
3244
3245static struct notifier_block spi_acpi_notifier = {
3246 .notifier_call = acpi_spi_notify,
3247};
3248#else
3249extern struct notifier_block spi_acpi_notifier;
3250#endif
3251
David Brownell8ae12a02006-01-08 13:34:19 -08003252static int __init spi_init(void)
3253{
David Brownellb8852442006-01-08 13:34:23 -08003254 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08003255
Christoph Lametere94b1762006-12-06 20:33:17 -08003256 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08003257 if (!buf) {
3258 status = -ENOMEM;
3259 goto err0;
3260 }
3261
3262 status = bus_register(&spi_bus_type);
3263 if (status < 0)
3264 goto err1;
3265
3266 status = class_register(&spi_master_class);
3267 if (status < 0)
3268 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003269
Fabio Estevam52677202014-11-26 20:13:57 -02003270 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003271 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03003272 if (IS_ENABLED(CONFIG_ACPI))
3273 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003274
David Brownell8ae12a02006-01-08 13:34:19 -08003275 return 0;
David Brownellb8852442006-01-08 13:34:23 -08003276
3277err2:
3278 bus_unregister(&spi_bus_type);
3279err1:
3280 kfree(buf);
3281 buf = NULL;
3282err0:
3283 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08003284}
David Brownellb8852442006-01-08 13:34:23 -08003285
David Brownell8ae12a02006-01-08 13:34:19 -08003286/* board_info is normally registered in arch_initcall(),
3287 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08003288 *
3289 * REVISIT only boardinfo really needs static linking. the rest (device and
3290 * driver registration) _could_ be dynamically linked (modular) ... costs
3291 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08003292 */
David Brownell673c0c02008-10-15 22:02:46 -07003293postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08003294