blob: 90b5b2efafbf45c2435af2771c4842a37786eee7 [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>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010036#include <uapi/linux/sched/types.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010037#include <linux/delay.h>
38#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010039#include <linux/ioport.h>
40#include <linux/acpi.h>
Vignesh Rb1b81532016-08-17 15:22:36 +053041#include <linux/highmem.h>
David Brownell8ae12a02006-01-08 13:34:19 -080042
Mark Brown56ec1972013-10-07 19:33:53 +010043#define CREATE_TRACE_POINTS
44#include <trace/events/spi.h>
45
David Brownell8ae12a02006-01-08 13:34:19 -080046static void spidev_release(struct device *dev)
47{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080048 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080049
50 /* spi masters may cleanup for released devices */
51 if (spi->master->cleanup)
52 spi->master->cleanup(spi);
53
David Brownell0c868462006-01-08 13:34:25 -080054 spi_master_put(spi->master);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000055 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080056}
57
58static ssize_t
59modalias_show(struct device *dev, struct device_attribute *a, char *buf)
60{
61 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080062 int len;
63
64 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
65 if (len != -ENODEV)
66 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080067
Grant Likelyd8e328b2012-05-20 00:08:13 -060068 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080069}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070070static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080071
Martin Sperleca2ebc2015-06-22 13:00:36 +000072#define SPI_STATISTICS_ATTRS(field, file) \
73static ssize_t spi_master_##field##_show(struct device *dev, \
74 struct device_attribute *attr, \
75 char *buf) \
76{ \
77 struct spi_master *master = container_of(dev, \
78 struct spi_master, dev); \
79 return spi_statistics_##field##_show(&master->statistics, buf); \
80} \
81static struct device_attribute dev_attr_spi_master_##field = { \
82 .attr = { .name = file, .mode = S_IRUGO }, \
83 .show = spi_master_##field##_show, \
84}; \
85static ssize_t spi_device_##field##_show(struct device *dev, \
86 struct device_attribute *attr, \
87 char *buf) \
88{ \
Geliang Tangd1eba932015-12-23 00:18:41 +080089 struct spi_device *spi = to_spi_device(dev); \
Martin Sperleca2ebc2015-06-22 13:00:36 +000090 return spi_statistics_##field##_show(&spi->statistics, buf); \
91} \
92static struct device_attribute dev_attr_spi_device_##field = { \
93 .attr = { .name = file, .mode = S_IRUGO }, \
94 .show = spi_device_##field##_show, \
95}
96
97#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
98static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
99 char *buf) \
100{ \
101 unsigned long flags; \
102 ssize_t len; \
103 spin_lock_irqsave(&stat->lock, flags); \
104 len = sprintf(buf, format_string, stat->field); \
105 spin_unlock_irqrestore(&stat->lock, flags); \
106 return len; \
107} \
108SPI_STATISTICS_ATTRS(name, file)
109
110#define SPI_STATISTICS_SHOW(field, format_string) \
111 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
112 field, format_string)
113
114SPI_STATISTICS_SHOW(messages, "%lu");
115SPI_STATISTICS_SHOW(transfers, "%lu");
116SPI_STATISTICS_SHOW(errors, "%lu");
117SPI_STATISTICS_SHOW(timedout, "%lu");
118
119SPI_STATISTICS_SHOW(spi_sync, "%lu");
120SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
121SPI_STATISTICS_SHOW(spi_async, "%lu");
122
123SPI_STATISTICS_SHOW(bytes, "%llu");
124SPI_STATISTICS_SHOW(bytes_rx, "%llu");
125SPI_STATISTICS_SHOW(bytes_tx, "%llu");
126
Martin Sperl6b7bc062015-06-22 13:02:04 +0000127#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
128 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
129 "transfer_bytes_histo_" number, \
130 transfer_bytes_histo[index], "%lu")
131SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
132SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
133SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
134SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
135SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
136SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
137SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
138SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
139SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
140SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
141SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
142SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
143SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
144SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
145SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
146SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
147SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
148
Martin Sperld9f12122015-12-14 15:20:20 +0000149SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
150
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700151static struct attribute *spi_dev_attrs[] = {
152 &dev_attr_modalias.attr,
153 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800154};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000155
156static const struct attribute_group spi_dev_group = {
157 .attrs = spi_dev_attrs,
158};
159
160static struct attribute *spi_device_statistics_attrs[] = {
161 &dev_attr_spi_device_messages.attr,
162 &dev_attr_spi_device_transfers.attr,
163 &dev_attr_spi_device_errors.attr,
164 &dev_attr_spi_device_timedout.attr,
165 &dev_attr_spi_device_spi_sync.attr,
166 &dev_attr_spi_device_spi_sync_immediate.attr,
167 &dev_attr_spi_device_spi_async.attr,
168 &dev_attr_spi_device_bytes.attr,
169 &dev_attr_spi_device_bytes_rx.attr,
170 &dev_attr_spi_device_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000171 &dev_attr_spi_device_transfer_bytes_histo0.attr,
172 &dev_attr_spi_device_transfer_bytes_histo1.attr,
173 &dev_attr_spi_device_transfer_bytes_histo2.attr,
174 &dev_attr_spi_device_transfer_bytes_histo3.attr,
175 &dev_attr_spi_device_transfer_bytes_histo4.attr,
176 &dev_attr_spi_device_transfer_bytes_histo5.attr,
177 &dev_attr_spi_device_transfer_bytes_histo6.attr,
178 &dev_attr_spi_device_transfer_bytes_histo7.attr,
179 &dev_attr_spi_device_transfer_bytes_histo8.attr,
180 &dev_attr_spi_device_transfer_bytes_histo9.attr,
181 &dev_attr_spi_device_transfer_bytes_histo10.attr,
182 &dev_attr_spi_device_transfer_bytes_histo11.attr,
183 &dev_attr_spi_device_transfer_bytes_histo12.attr,
184 &dev_attr_spi_device_transfer_bytes_histo13.attr,
185 &dev_attr_spi_device_transfer_bytes_histo14.attr,
186 &dev_attr_spi_device_transfer_bytes_histo15.attr,
187 &dev_attr_spi_device_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000188 &dev_attr_spi_device_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000189 NULL,
190};
191
192static const struct attribute_group spi_device_statistics_group = {
193 .name = "statistics",
194 .attrs = spi_device_statistics_attrs,
195};
196
197static const struct attribute_group *spi_dev_groups[] = {
198 &spi_dev_group,
199 &spi_device_statistics_group,
200 NULL,
201};
202
203static struct attribute *spi_master_statistics_attrs[] = {
204 &dev_attr_spi_master_messages.attr,
205 &dev_attr_spi_master_transfers.attr,
206 &dev_attr_spi_master_errors.attr,
207 &dev_attr_spi_master_timedout.attr,
208 &dev_attr_spi_master_spi_sync.attr,
209 &dev_attr_spi_master_spi_sync_immediate.attr,
210 &dev_attr_spi_master_spi_async.attr,
211 &dev_attr_spi_master_bytes.attr,
212 &dev_attr_spi_master_bytes_rx.attr,
213 &dev_attr_spi_master_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000214 &dev_attr_spi_master_transfer_bytes_histo0.attr,
215 &dev_attr_spi_master_transfer_bytes_histo1.attr,
216 &dev_attr_spi_master_transfer_bytes_histo2.attr,
217 &dev_attr_spi_master_transfer_bytes_histo3.attr,
218 &dev_attr_spi_master_transfer_bytes_histo4.attr,
219 &dev_attr_spi_master_transfer_bytes_histo5.attr,
220 &dev_attr_spi_master_transfer_bytes_histo6.attr,
221 &dev_attr_spi_master_transfer_bytes_histo7.attr,
222 &dev_attr_spi_master_transfer_bytes_histo8.attr,
223 &dev_attr_spi_master_transfer_bytes_histo9.attr,
224 &dev_attr_spi_master_transfer_bytes_histo10.attr,
225 &dev_attr_spi_master_transfer_bytes_histo11.attr,
226 &dev_attr_spi_master_transfer_bytes_histo12.attr,
227 &dev_attr_spi_master_transfer_bytes_histo13.attr,
228 &dev_attr_spi_master_transfer_bytes_histo14.attr,
229 &dev_attr_spi_master_transfer_bytes_histo15.attr,
230 &dev_attr_spi_master_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000231 &dev_attr_spi_master_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000232 NULL,
233};
234
235static const struct attribute_group spi_master_statistics_group = {
236 .name = "statistics",
237 .attrs = spi_master_statistics_attrs,
238};
239
240static const struct attribute_group *spi_master_groups[] = {
241 &spi_master_statistics_group,
242 NULL,
243};
244
245void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
246 struct spi_transfer *xfer,
247 struct spi_master *master)
248{
249 unsigned long flags;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000250 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
251
252 if (l2len < 0)
253 l2len = 0;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000254
255 spin_lock_irqsave(&stats->lock, flags);
256
257 stats->transfers++;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000258 stats->transfer_bytes_histo[l2len]++;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000259
260 stats->bytes += xfer->len;
261 if ((xfer->tx_buf) &&
262 (xfer->tx_buf != master->dummy_tx))
263 stats->bytes_tx += xfer->len;
264 if ((xfer->rx_buf) &&
265 (xfer->rx_buf != master->dummy_rx))
266 stats->bytes_rx += xfer->len;
267
268 spin_unlock_irqrestore(&stats->lock, flags);
269}
270EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800271
272/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
273 * and the sysfs version makes coldplug work too.
274 */
275
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700276static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
277 const struct spi_device *sdev)
278{
279 while (id->name[0]) {
280 if (!strcmp(sdev->modalias, id->name))
281 return id;
282 id++;
283 }
284 return NULL;
285}
286
287const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
288{
289 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
290
291 return spi_match_id(sdrv->id_table, sdev);
292}
293EXPORT_SYMBOL_GPL(spi_get_device_id);
294
David Brownell8ae12a02006-01-08 13:34:19 -0800295static int spi_match_device(struct device *dev, struct device_driver *drv)
296{
297 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700298 const struct spi_driver *sdrv = to_spi_driver(drv);
299
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600300 /* Attempt an OF style match */
301 if (of_driver_match_device(dev, drv))
302 return 1;
303
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100304 /* Then try ACPI */
305 if (acpi_driver_match_device(dev, drv))
306 return 1;
307
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700308 if (sdrv->id_table)
309 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800310
Kay Sievers35f74fc2009-01-06 10:44:37 -0800311 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800312}
313
Kay Sievers7eff2e72007-08-14 15:15:12 +0200314static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800315{
316 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800317 int rc;
318
319 rc = acpi_device_uevent_modalias(dev, env);
320 if (rc != -ENODEV)
321 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800322
Anton Vorontsove0626e32009-09-22 16:46:08 -0700323 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800324 return 0;
325}
326
David Brownell8ae12a02006-01-08 13:34:19 -0800327struct bus_type spi_bus_type = {
328 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700329 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800330 .match = spi_match_device,
331 .uevent = spi_uevent,
David Brownell8ae12a02006-01-08 13:34:19 -0800332};
333EXPORT_SYMBOL_GPL(spi_bus_type);
334
David Brownellb8852442006-01-08 13:34:23 -0800335
336static int spi_drv_probe(struct device *dev)
337{
338 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Jon Hunter44af7922015-10-09 15:45:55 +0100339 struct spi_device *spi = to_spi_device(dev);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300340 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800341
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200342 ret = of_clk_set_defaults(dev->of_node, false);
343 if (ret)
344 return ret;
345
Jon Hunter44af7922015-10-09 15:45:55 +0100346 if (dev->of_node) {
347 spi->irq = of_irq_get(dev->of_node, 0);
348 if (spi->irq == -EPROBE_DEFER)
349 return -EPROBE_DEFER;
350 if (spi->irq < 0)
351 spi->irq = 0;
352 }
353
Ulf Hansson676e7c22014-09-19 20:27:41 +0200354 ret = dev_pm_domain_attach(dev, true);
355 if (ret != -EPROBE_DEFER) {
Jon Hunter44af7922015-10-09 15:45:55 +0100356 ret = sdrv->probe(spi);
Ulf Hansson676e7c22014-09-19 20:27:41 +0200357 if (ret)
358 dev_pm_domain_detach(dev, true);
359 }
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300360
361 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800362}
363
364static int spi_drv_remove(struct device *dev)
365{
366 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300367 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800368
Jean Delvareaec35f42014-02-13 15:28:41 +0100369 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200370 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300371
372 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800373}
374
375static void spi_drv_shutdown(struct device *dev)
376{
377 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
378
379 sdrv->shutdown(to_spi_device(dev));
380}
381
David Brownell33e34dc2007-05-08 00:32:21 -0700382/**
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500383 * __spi_register_driver - register a SPI driver
Thierry Reding88c93212015-11-10 13:03:04 +0100384 * @owner: owner module of the driver to register
David Brownell33e34dc2007-05-08 00:32:21 -0700385 * @sdrv: the driver to register
386 * Context: can sleep
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200387 *
388 * Return: zero on success, else a negative error code.
David Brownell33e34dc2007-05-08 00:32:21 -0700389 */
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500390int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
David Brownellb8852442006-01-08 13:34:23 -0800391{
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500392 sdrv->driver.owner = owner;
David Brownellb8852442006-01-08 13:34:23 -0800393 sdrv->driver.bus = &spi_bus_type;
394 if (sdrv->probe)
395 sdrv->driver.probe = spi_drv_probe;
396 if (sdrv->remove)
397 sdrv->driver.remove = spi_drv_remove;
398 if (sdrv->shutdown)
399 sdrv->driver.shutdown = spi_drv_shutdown;
400 return driver_register(&sdrv->driver);
401}
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500402EXPORT_SYMBOL_GPL(__spi_register_driver);
David Brownellb8852442006-01-08 13:34:23 -0800403
David Brownell8ae12a02006-01-08 13:34:19 -0800404/*-------------------------------------------------------------------------*/
405
406/* SPI devices should normally not be created by SPI device drivers; that
407 * would make them board-specific. Similarly with SPI master drivers.
408 * Device registration normally goes into like arch/.../mach.../board-YYY.c
409 * with other readonly (flashable) information about mainboard devices.
410 */
411
412struct boardinfo {
413 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800414 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800415};
416
417static LIST_HEAD(board_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800418static LIST_HEAD(spi_master_list);
419
420/*
421 * Used to protect add/del opertion for board_info list and
422 * spi_master list, and their matching process
423 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700424static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800425
Grant Likelydc87c982008-05-15 16:50:22 -0600426/**
427 * spi_alloc_device - Allocate a new SPI device
428 * @master: Controller to which device is connected
429 * Context: can sleep
430 *
431 * Allows a driver to allocate and initialize a spi_device without
432 * registering it immediately. This allows a driver to directly
433 * fill the spi_device with device parameters before calling
434 * spi_add_device() on it.
435 *
436 * Caller is responsible to call spi_add_device() on the returned
437 * spi_device structure to add it to the SPI master. If the caller
438 * needs to discard the spi_device without adding it, then it should
439 * call spi_dev_put() on it.
440 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200441 * Return: a pointer to the new device, or NULL.
Grant Likelydc87c982008-05-15 16:50:22 -0600442 */
443struct spi_device *spi_alloc_device(struct spi_master *master)
444{
445 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600446
447 if (!spi_master_get(master))
448 return NULL;
449
Jingoo Han5fe5f052013-10-14 10:31:51 +0900450 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600451 if (!spi) {
Grant Likelydc87c982008-05-15 16:50:22 -0600452 spi_master_put(master);
453 return NULL;
454 }
455
456 spi->master = master;
Laurent Pinchart178db7d2011-12-12 01:15:06 +0100457 spi->dev.parent = &master->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600458 spi->dev.bus = &spi_bus_type;
459 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100460 spi->cs_gpio = -ENOENT;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000461
462 spin_lock_init(&spi->statistics.lock);
463
Grant Likelydc87c982008-05-15 16:50:22 -0600464 device_initialize(&spi->dev);
465 return spi;
466}
467EXPORT_SYMBOL_GPL(spi_alloc_device);
468
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200469static void spi_dev_set_name(struct spi_device *spi)
470{
471 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
472
473 if (adev) {
474 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
475 return;
476 }
477
478 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
479 spi->chip_select);
480}
481
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200482static int spi_dev_check(struct device *dev, void *data)
483{
484 struct spi_device *spi = to_spi_device(dev);
485 struct spi_device *new_spi = data;
486
487 if (spi->master == new_spi->master &&
488 spi->chip_select == new_spi->chip_select)
489 return -EBUSY;
490 return 0;
491}
492
Grant Likelydc87c982008-05-15 16:50:22 -0600493/**
494 * spi_add_device - Add spi_device allocated with spi_alloc_device
495 * @spi: spi_device to register
496 *
497 * Companion function to spi_alloc_device. Devices allocated with
498 * spi_alloc_device can be added onto the spi bus with this function.
499 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200500 * Return: 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600501 */
502int spi_add_device(struct spi_device *spi)
503{
David Brownelle48880e2008-08-15 00:40:44 -0700504 static DEFINE_MUTEX(spi_add_lock);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100505 struct spi_master *master = spi->master;
506 struct device *dev = master->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600507 int status;
508
509 /* Chipselects are numbered 0..max; validate. */
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100510 if (spi->chip_select >= master->num_chipselect) {
Grant Likelydc87c982008-05-15 16:50:22 -0600511 dev_err(dev, "cs%d >= max %d\n",
512 spi->chip_select,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100513 master->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600514 return -EINVAL;
515 }
516
517 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200518 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700519
520 /* We need to make sure there's no other device with this
521 * chipselect **BEFORE** we call setup(), else we'll trash
522 * its configuration. Lock against concurrent add() calls.
523 */
524 mutex_lock(&spi_add_lock);
525
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200526 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
527 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700528 dev_err(dev, "chipselect %d already in use\n",
529 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700530 goto done;
531 }
532
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100533 if (master->cs_gpios)
534 spi->cs_gpio = master->cs_gpios[spi->chip_select];
535
David Brownelle48880e2008-08-15 00:40:44 -0700536 /* Drivers may modify this initial i/o setup, but will
537 * normally rely on the device being setup. Devices
538 * using SPI_CS_HIGH can't coexist well otherwise...
539 */
David Brownell7d077192009-06-17 16:26:03 -0700540 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600541 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200542 dev_err(dev, "can't setup %s, status %d\n",
543 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700544 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600545 }
546
David Brownelle48880e2008-08-15 00:40:44 -0700547 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600548 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700549 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200550 dev_err(dev, "can't add %s, status %d\n",
551 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700552 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800553 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600554
David Brownelle48880e2008-08-15 00:40:44 -0700555done:
556 mutex_unlock(&spi_add_lock);
557 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600558}
559EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800560
David Brownell33e34dc2007-05-08 00:32:21 -0700561/**
562 * spi_new_device - instantiate one new SPI device
563 * @master: Controller to which device is connected
564 * @chip: Describes the SPI device
565 * Context: can sleep
566 *
567 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800568 * after board init creates the hard-wired devices. Some development
569 * platforms may not be able to use spi_register_board_info though, and
570 * this is exported so that for example a USB or parport based adapter
571 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700572 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200573 * Return: the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800574 */
Adrian Bunke9d5a462007-03-26 21:32:23 -0800575struct spi_device *spi_new_device(struct spi_master *master,
576 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800577{
578 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800579 int status;
580
David Brownell082c8cb2007-07-31 00:39:45 -0700581 /* NOTE: caller did any chip->bus_num checks necessary.
582 *
583 * Also, unless we change the return value convention to use
584 * error-or-pointer (not NULL-or-pointer), troubleshootability
585 * suggests syslogged diagnostics are best here (ugh).
586 */
587
Grant Likelydc87c982008-05-15 16:50:22 -0600588 proxy = spi_alloc_device(master);
589 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800590 return NULL;
591
Grant Likely102eb972008-07-23 21:29:55 -0700592 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
593
David Brownell8ae12a02006-01-08 13:34:19 -0800594 proxy->chip_select = chip->chip_select;
595 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700596 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800597 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700598 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800599 proxy->dev.platform_data = (void *) chip->platform_data;
600 proxy->controller_data = chip->controller_data;
601 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800602
Grant Likelydc87c982008-05-15 16:50:22 -0600603 status = spi_add_device(proxy);
David Brownell8ae12a02006-01-08 13:34:19 -0800604 if (status < 0) {
Grant Likelydc87c982008-05-15 16:50:22 -0600605 spi_dev_put(proxy);
606 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800607 }
608
David Brownell8ae12a02006-01-08 13:34:19 -0800609 return proxy;
610}
611EXPORT_SYMBOL_GPL(spi_new_device);
612
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100613/**
614 * spi_unregister_device - unregister a single SPI device
615 * @spi: spi_device to unregister
616 *
617 * Start making the passed SPI device vanish. Normally this would be handled
618 * by spi_unregister_master().
619 */
620void spi_unregister_device(struct spi_device *spi)
621{
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100622 if (!spi)
623 return;
624
Johan Hovold83241472017-01-30 17:47:05 +0100625 if (spi->dev.of_node) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100626 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
Johan Hovold83241472017-01-30 17:47:05 +0100627 of_node_put(spi->dev.of_node);
628 }
Octavian Purdila7f244672016-07-08 19:13:11 +0300629 if (ACPI_COMPANION(&spi->dev))
630 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100631 device_unregister(&spi->dev);
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100632}
633EXPORT_SYMBOL_GPL(spi_unregister_device);
634
Feng Tang2b9603a2010-08-02 15:52:15 +0800635static void spi_match_master_to_boardinfo(struct spi_master *master,
636 struct spi_board_info *bi)
637{
638 struct spi_device *dev;
639
640 if (master->bus_num != bi->bus_num)
641 return;
642
643 dev = spi_new_device(master, bi);
644 if (!dev)
645 dev_err(master->dev.parent, "can't create new device for %s\n",
646 bi->modalias);
647}
648
David Brownell33e34dc2007-05-08 00:32:21 -0700649/**
650 * spi_register_board_info - register SPI devices for a given board
651 * @info: array of chip descriptors
652 * @n: how many descriptors are provided
653 * Context: can sleep
654 *
David Brownell8ae12a02006-01-08 13:34:19 -0800655 * Board-specific early init code calls this (probably during arch_initcall)
656 * with segments of the SPI device table. Any device nodes are created later,
657 * after the relevant parent SPI controller (bus_num) is defined. We keep
658 * this table of devices forever, so that reloading a controller driver will
659 * not make Linux forget about these hard-wired devices.
660 *
661 * Other code can also call this, e.g. a particular add-on board might provide
662 * SPI devices through its expansion connector, so code initializing that board
663 * would naturally declare its SPI devices.
664 *
665 * The board info passed can safely be __initdata ... but be careful of
666 * any embedded pointers (platform_data, etc), they're copied as-is.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200667 *
668 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -0800669 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000670int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800671{
Feng Tang2b9603a2010-08-02 15:52:15 +0800672 struct boardinfo *bi;
673 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800674
Xiubo Lic7908a32014-09-24 14:30:29 +0800675 if (!n)
676 return -EINVAL;
677
Markus Elfringf9bdb7f2017-01-13 12:28:04 +0100678 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800679 if (!bi)
680 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800681
Feng Tang2b9603a2010-08-02 15:52:15 +0800682 for (i = 0; i < n; i++, bi++, info++) {
683 struct spi_master *master;
David Brownell8ae12a02006-01-08 13:34:19 -0800684
Feng Tang2b9603a2010-08-02 15:52:15 +0800685 memcpy(&bi->board_info, info, sizeof(*info));
686 mutex_lock(&board_lock);
687 list_add_tail(&bi->list, &board_list);
688 list_for_each_entry(master, &spi_master_list, list)
689 spi_match_master_to_boardinfo(master, &bi->board_info);
690 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800691 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800692
693 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800694}
695
696/*-------------------------------------------------------------------------*/
697
Mark Brownb1589352013-10-05 11:50:40 +0100698static void spi_set_cs(struct spi_device *spi, bool enable)
699{
700 if (spi->mode & SPI_CS_HIGH)
701 enable = !enable;
702
Thor Thayer8eee6b92016-10-10 09:25:24 -0500703 if (gpio_is_valid(spi->cs_gpio)) {
Mark Brownb1589352013-10-05 11:50:40 +0100704 gpio_set_value(spi->cs_gpio, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500705 /* Some SPI masters need both GPIO CS & slave_select */
706 if ((spi->master->flags & SPI_MASTER_GPIO_SS) &&
707 spi->master->set_cs)
708 spi->master->set_cs(spi, !enable);
709 } else if (spi->master->set_cs) {
Mark Brownb1589352013-10-05 11:50:40 +0100710 spi->master->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500711 }
Mark Brownb1589352013-10-05 11:50:40 +0100712}
713
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200714#ifdef CONFIG_HAS_DMA
Mark Brown6ad45a22014-02-02 13:47:47 +0000715static int spi_map_buf(struct spi_master *master, struct device *dev,
716 struct sg_table *sgt, void *buf, size_t len,
717 enum dma_data_direction dir)
718{
719 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200720 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530721#ifdef CONFIG_HIGHMEM
722 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
723 (unsigned long)buf < (PKMAP_BASE +
724 (LAST_PKMAP * PAGE_SIZE)));
725#else
726 const bool kmap_buf = false;
727#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500728 int desc_len;
729 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000730 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600731 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000732 void *sg_buf;
733 size_t min;
734 int i, ret;
735
Vignesh Rb1b81532016-08-17 15:22:36 +0530736 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200737 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500738 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530739 } else if (virt_addr_valid(buf)) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200740 desc_len = min_t(int, max_seg_size, master->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500741 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530742 } else {
743 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500744 }
745
Mark Brown6ad45a22014-02-02 13:47:47 +0000746 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
747 if (ret != 0)
748 return ret;
749
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600750 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000751 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000752
Vignesh Rb1b81532016-08-17 15:22:36 +0530753 if (vmalloced_buf || kmap_buf) {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500754 min = min_t(size_t,
755 len, desc_len - offset_in_page(buf));
Vignesh Rb1b81532016-08-17 15:22:36 +0530756 if (vmalloced_buf)
757 vm_page = vmalloc_to_page(buf);
758 else
759 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000760 if (!vm_page) {
761 sg_free_table(sgt);
762 return -ENOMEM;
763 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600764 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000765 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000766 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500767 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000768 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600769 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000770 }
771
Mark Brown6ad45a22014-02-02 13:47:47 +0000772 buf += min;
773 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600774 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000775 }
776
777 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200778 if (!ret)
779 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000780 if (ret < 0) {
781 sg_free_table(sgt);
782 return ret;
783 }
784
785 sgt->nents = ret;
786
787 return 0;
788}
789
790static void spi_unmap_buf(struct spi_master *master, struct device *dev,
791 struct sg_table *sgt, enum dma_data_direction dir)
792{
793 if (sgt->orig_nents) {
794 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
795 sg_free_table(sgt);
796 }
797}
798
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200799static int __spi_map_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000800{
Mark Brown99adef32014-01-16 12:22:43 +0000801 struct device *tx_dev, *rx_dev;
802 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000803 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000804
Mark Brown6ad45a22014-02-02 13:47:47 +0000805 if (!master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000806 return 0;
807
Leilk Liuc37f45b2015-07-23 17:10:40 +0800808 if (master->dma_tx)
809 tx_dev = master->dma_tx->device->dev;
810 else
Daniel Kurtz88b0aa52017-01-27 00:21:53 +0800811 tx_dev = master->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800812
813 if (master->dma_rx)
814 rx_dev = master->dma_rx->device->dev;
815 else
Daniel Kurtz88b0aa52017-01-27 00:21:53 +0800816 rx_dev = master->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000817
818 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
819 if (!master->can_dma(master, msg->spi, xfer))
820 continue;
821
822 if (xfer->tx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000823 ret = spi_map_buf(master, tx_dev, &xfer->tx_sg,
824 (void *)xfer->tx_buf, xfer->len,
825 DMA_TO_DEVICE);
826 if (ret != 0)
827 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000828 }
829
830 if (xfer->rx_buf != NULL) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000831 ret = spi_map_buf(master, rx_dev, &xfer->rx_sg,
832 xfer->rx_buf, xfer->len,
833 DMA_FROM_DEVICE);
834 if (ret != 0) {
835 spi_unmap_buf(master, tx_dev, &xfer->tx_sg,
836 DMA_TO_DEVICE);
837 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000838 }
839 }
840 }
841
842 master->cur_msg_mapped = true;
843
844 return 0;
845}
846
Martin Sperl4b786452015-05-25 10:13:10 +0000847static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000848{
849 struct spi_transfer *xfer;
850 struct device *tx_dev, *rx_dev;
851
Mark Brown6ad45a22014-02-02 13:47:47 +0000852 if (!master->cur_msg_mapped || !master->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000853 return 0;
854
Leilk Liuc37f45b2015-07-23 17:10:40 +0800855 if (master->dma_tx)
856 tx_dev = master->dma_tx->device->dev;
857 else
Daniel Kurtz88b0aa52017-01-27 00:21:53 +0800858 tx_dev = master->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800859
860 if (master->dma_rx)
861 rx_dev = master->dma_rx->device->dev;
862 else
Daniel Kurtz88b0aa52017-01-27 00:21:53 +0800863 rx_dev = master->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000864
865 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
866 if (!master->can_dma(master, msg->spi, xfer))
867 continue;
868
Mark Brown6ad45a22014-02-02 13:47:47 +0000869 spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
870 spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000871 }
872
873 return 0;
874}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200875#else /* !CONFIG_HAS_DMA */
Vignesh Rf4502dd2016-06-08 12:18:31 +0530876static inline int spi_map_buf(struct spi_master *master,
877 struct device *dev, struct sg_table *sgt,
878 void *buf, size_t len,
879 enum dma_data_direction dir)
880{
881 return -EINVAL;
882}
883
884static inline void spi_unmap_buf(struct spi_master *master,
885 struct device *dev, struct sg_table *sgt,
886 enum dma_data_direction dir)
887{
888}
889
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200890static inline int __spi_map_msg(struct spi_master *master,
891 struct spi_message *msg)
892{
893 return 0;
894}
895
Martin Sperl4b786452015-05-25 10:13:10 +0000896static inline int __spi_unmap_msg(struct spi_master *master,
897 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200898{
899 return 0;
900}
901#endif /* !CONFIG_HAS_DMA */
902
Martin Sperl4b786452015-05-25 10:13:10 +0000903static inline int spi_unmap_msg(struct spi_master *master,
904 struct spi_message *msg)
905{
906 struct spi_transfer *xfer;
907
908 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
909 /*
910 * Restore the original value of tx_buf or rx_buf if they are
911 * NULL.
912 */
913 if (xfer->tx_buf == master->dummy_tx)
914 xfer->tx_buf = NULL;
915 if (xfer->rx_buf == master->dummy_rx)
916 xfer->rx_buf = NULL;
917 }
918
919 return __spi_unmap_msg(master, msg);
920}
921
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200922static int spi_map_msg(struct spi_master *master, struct spi_message *msg)
923{
924 struct spi_transfer *xfer;
925 void *tmp;
926 unsigned int max_tx, max_rx;
927
928 if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) {
929 max_tx = 0;
930 max_rx = 0;
931
932 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
933 if ((master->flags & SPI_MASTER_MUST_TX) &&
934 !xfer->tx_buf)
935 max_tx = max(xfer->len, max_tx);
936 if ((master->flags & SPI_MASTER_MUST_RX) &&
937 !xfer->rx_buf)
938 max_rx = max(xfer->len, max_rx);
939 }
940
941 if (max_tx) {
942 tmp = krealloc(master->dummy_tx, max_tx,
943 GFP_KERNEL | GFP_DMA);
944 if (!tmp)
945 return -ENOMEM;
946 master->dummy_tx = tmp;
947 memset(tmp, 0, max_tx);
948 }
949
950 if (max_rx) {
951 tmp = krealloc(master->dummy_rx, max_rx,
952 GFP_KERNEL | GFP_DMA);
953 if (!tmp)
954 return -ENOMEM;
955 master->dummy_rx = tmp;
956 }
957
958 if (max_tx || max_rx) {
959 list_for_each_entry(xfer, &msg->transfers,
960 transfer_list) {
961 if (!xfer->tx_buf)
962 xfer->tx_buf = master->dummy_tx;
963 if (!xfer->rx_buf)
964 xfer->rx_buf = master->dummy_rx;
965 }
966 }
967 }
968
969 return __spi_map_msg(master, msg);
970}
Mark Brown99adef32014-01-16 12:22:43 +0000971
Mark Brownb1589352013-10-05 11:50:40 +0100972/*
973 * spi_transfer_one_message - Default implementation of transfer_one_message()
974 *
975 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -0700976 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +0100977 * standard handling of delays and chip select management.
978 */
979static int spi_transfer_one_message(struct spi_master *master,
980 struct spi_message *msg)
981{
982 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +0100983 bool keep_cs = false;
984 int ret = 0;
Sien Wud0716dd2016-09-01 18:24:29 -0500985 unsigned long long ms = 1;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000986 struct spi_statistics *statm = &master->statistics;
987 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +0100988
989 spi_set_cs(msg->spi, true);
990
Martin Sperleca2ebc2015-06-22 13:00:36 +0000991 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
992 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
993
Mark Brownb1589352013-10-05 11:50:40 +0100994 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
995 trace_spi_transfer_start(msg, xfer);
996
Martin Sperleca2ebc2015-06-22 13:00:36 +0000997 spi_statistics_add_transfer_stats(statm, xfer, master);
998 spi_statistics_add_transfer_stats(stats, xfer, master);
999
Mark Brown38ec10f2014-08-16 16:27:41 +01001000 if (xfer->tx_buf || xfer->rx_buf) {
1001 reinit_completion(&master->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001002
Mark Brown38ec10f2014-08-16 16:27:41 +01001003 ret = master->transfer_one(master, msg->spi, xfer);
1004 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001005 SPI_STATISTICS_INCREMENT_FIELD(statm,
1006 errors);
1007 SPI_STATISTICS_INCREMENT_FIELD(stats,
1008 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001009 dev_err(&msg->spi->dev,
1010 "SPI transfer failed: %d\n", ret);
1011 goto out;
1012 }
Mark Brownb1589352013-10-05 11:50:40 +01001013
Mark Brown38ec10f2014-08-16 16:27:41 +01001014 if (ret > 0) {
1015 ret = 0;
Sien Wud0716dd2016-09-01 18:24:29 -05001016 ms = 8LL * 1000LL * xfer->len;
1017 do_div(ms, xfer->speed_hz);
Mark Brown38ec10f2014-08-16 16:27:41 +01001018 ms += ms + 100; /* some tolerance */
Mark Brown16a0ce42014-01-30 22:16:41 +00001019
Sien Wud0716dd2016-09-01 18:24:29 -05001020 if (ms > UINT_MAX)
1021 ms = UINT_MAX;
1022
Mark Brown38ec10f2014-08-16 16:27:41 +01001023 ms = wait_for_completion_timeout(&master->xfer_completion,
1024 msecs_to_jiffies(ms));
1025 }
Mark Brown16a0ce42014-01-30 22:16:41 +00001026
Mark Brown38ec10f2014-08-16 16:27:41 +01001027 if (ms == 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001028 SPI_STATISTICS_INCREMENT_FIELD(statm,
1029 timedout);
1030 SPI_STATISTICS_INCREMENT_FIELD(stats,
1031 timedout);
Mark Brown38ec10f2014-08-16 16:27:41 +01001032 dev_err(&msg->spi->dev,
1033 "SPI transfer timed out\n");
1034 msg->status = -ETIMEDOUT;
1035 }
1036 } else {
1037 if (xfer->len)
1038 dev_err(&msg->spi->dev,
1039 "Bufferless transfer has length %u\n",
1040 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001041 }
Mark Brownb1589352013-10-05 11:50:40 +01001042
1043 trace_spi_transfer_stop(msg, xfer);
1044
1045 if (msg->status != -EINPROGRESS)
1046 goto out;
1047
Daniel Kurtz8244bd32016-10-07 18:55:47 +08001048 if (xfer->delay_usecs) {
1049 u16 us = xfer->delay_usecs;
1050
1051 if (us <= 10)
1052 udelay(us);
1053 else
1054 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1055 }
Mark Brownb1589352013-10-05 11:50:40 +01001056
1057 if (xfer->cs_change) {
1058 if (list_is_last(&xfer->transfer_list,
1059 &msg->transfers)) {
1060 keep_cs = true;
1061 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001062 spi_set_cs(msg->spi, false);
1063 udelay(10);
1064 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001065 }
1066 }
1067
1068 msg->actual_length += xfer->len;
1069 }
1070
1071out:
1072 if (ret != 0 || !keep_cs)
1073 spi_set_cs(msg->spi, false);
1074
1075 if (msg->status == -EINPROGRESS)
1076 msg->status = ret;
1077
Geert Uytterhoevenff61eb42015-04-07 20:39:19 +02001078 if (msg->status && master->handle_err)
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001079 master->handle_err(master, msg);
1080
Martin Sperld780c372015-12-14 15:20:18 +00001081 spi_res_release(master, msg);
1082
Mark Brownb1589352013-10-05 11:50:40 +01001083 spi_finalize_current_message(master);
1084
1085 return ret;
1086}
1087
1088/**
1089 * spi_finalize_current_transfer - report completion of a transfer
Thierry Reding2c675682014-08-08 13:02:36 +02001090 * @master: the master reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001091 *
1092 * Called by SPI drivers using the core transfer_one_message()
1093 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001094 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001095 */
1096void spi_finalize_current_transfer(struct spi_master *master)
1097{
1098 complete(&master->xfer_completion);
1099}
1100EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1101
Linus Walleijffbbdd212012-02-22 10:05:38 +01001102/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001103 * __spi_pump_messages - function which processes spi message queue
1104 * @master: master to process queue for
1105 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001106 *
1107 * This function checks if there is any spi message in the queue that
1108 * needs processing and if so call out to the driver to initialize hardware
1109 * and transfer each message.
1110 *
Mark Brown0461a412014-12-09 21:38:05 +00001111 * Note that it is called both from the kthread itself and also from
1112 * inside spi_sync(); the queue extraction handling at the top of the
1113 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001114 */
Mark Brownef4d96e2016-07-21 23:53:31 +01001115static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001116{
Linus Walleijffbbdd212012-02-22 10:05:38 +01001117 unsigned long flags;
1118 bool was_busy = false;
1119 int ret;
1120
Mark Brown983aee52014-12-09 19:46:56 +00001121 /* Lock queue */
Linus Walleijffbbdd212012-02-22 10:05:38 +01001122 spin_lock_irqsave(&master->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001123
1124 /* Make sure we are not already running a message */
1125 if (master->cur_msg) {
1126 spin_unlock_irqrestore(&master->queue_lock, flags);
1127 return;
1128 }
1129
Mark Brown0461a412014-12-09 21:38:05 +00001130 /* If another context is idling the device then defer */
1131 if (master->idling) {
Petr Mladek39891442016-10-11 13:55:20 -07001132 kthread_queue_work(&master->kworker, &master->pump_messages);
Mark Brown0461a412014-12-09 21:38:05 +00001133 spin_unlock_irqrestore(&master->queue_lock, flags);
1134 return;
1135 }
1136
Mark Brown983aee52014-12-09 19:46:56 +00001137 /* Check if the queue is idle */
Linus Walleijffbbdd212012-02-22 10:05:38 +01001138 if (list_empty(&master->queue) || !master->running) {
Bryan Freedb0b36b82013-03-13 11:17:40 -07001139 if (!master->busy) {
1140 spin_unlock_irqrestore(&master->queue_lock, flags);
1141 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001142 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001143
1144 /* Only do teardown in the thread */
1145 if (!in_kthread) {
Petr Mladek39891442016-10-11 13:55:20 -07001146 kthread_queue_work(&master->kworker,
Mark Brownfc9e0f72014-12-10 13:46:33 +00001147 &master->pump_messages);
1148 spin_unlock_irqrestore(&master->queue_lock, flags);
1149 return;
1150 }
1151
Linus Walleijffbbdd212012-02-22 10:05:38 +01001152 master->busy = false;
Mark Brown0461a412014-12-09 21:38:05 +00001153 master->idling = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001154 spin_unlock_irqrestore(&master->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001155
Mark Brown3a2eba92014-01-28 20:17:03 +00001156 kfree(master->dummy_rx);
1157 master->dummy_rx = NULL;
1158 kfree(master->dummy_tx);
1159 master->dummy_tx = NULL;
Bryan Freedb0b36b82013-03-13 11:17:40 -07001160 if (master->unprepare_transfer_hardware &&
1161 master->unprepare_transfer_hardware(master))
1162 dev_err(&master->dev,
1163 "failed to unprepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001164 if (master->auto_runtime_pm) {
1165 pm_runtime_mark_last_busy(master->dev.parent);
1166 pm_runtime_put_autosuspend(master->dev.parent);
1167 }
Mark Brown56ec1972013-10-07 19:33:53 +01001168 trace_spi_master_idle(master);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001169
Mark Brown0461a412014-12-09 21:38:05 +00001170 spin_lock_irqsave(&master->queue_lock, flags);
1171 master->idling = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001172 spin_unlock_irqrestore(&master->queue_lock, flags);
1173 return;
1174 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001175
Linus Walleijffbbdd212012-02-22 10:05:38 +01001176 /* Extract head of queue */
1177 master->cur_msg =
Axel Lina89e2d22014-01-09 16:03:58 +08001178 list_first_entry(&master->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001179
1180 list_del_init(&master->cur_msg->queue);
1181 if (master->busy)
1182 was_busy = true;
1183 else
1184 master->busy = true;
1185 spin_unlock_irqrestore(&master->queue_lock, flags);
1186
Mark Brownef4d96e2016-07-21 23:53:31 +01001187 mutex_lock(&master->io_mutex);
1188
Mark Brown49834de2013-07-28 14:47:02 +01001189 if (!was_busy && master->auto_runtime_pm) {
1190 ret = pm_runtime_get_sync(master->dev.parent);
1191 if (ret < 0) {
1192 dev_err(&master->dev, "Failed to power device: %d\n",
1193 ret);
Mark Brown764f2162016-08-09 17:45:33 +01001194 mutex_unlock(&master->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001195 return;
1196 }
1197 }
1198
Mark Brown56ec1972013-10-07 19:33:53 +01001199 if (!was_busy)
1200 trace_spi_master_busy(master);
1201
Shubhrajyoti D7dfd2bd2012-05-10 19:20:41 +05301202 if (!was_busy && master->prepare_transfer_hardware) {
Linus Walleijffbbdd212012-02-22 10:05:38 +01001203 ret = master->prepare_transfer_hardware(master);
1204 if (ret) {
1205 dev_err(&master->dev,
1206 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001207
1208 if (master->auto_runtime_pm)
1209 pm_runtime_put(master->dev.parent);
Mark Brown764f2162016-08-09 17:45:33 +01001210 mutex_unlock(&master->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001211 return;
1212 }
1213 }
1214
Mark Brown56ec1972013-10-07 19:33:53 +01001215 trace_spi_message_start(master->cur_msg);
1216
Mark Brown2841a5f2013-10-05 00:23:12 +01001217 if (master->prepare_message) {
1218 ret = master->prepare_message(master, master->cur_msg);
1219 if (ret) {
1220 dev_err(&master->dev,
1221 "failed to prepare message: %d\n", ret);
1222 master->cur_msg->status = ret;
1223 spi_finalize_current_message(master);
Jon Hunter49023d22016-03-08 12:28:20 +00001224 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001225 }
1226 master->cur_msg_prepared = true;
1227 }
1228
Mark Brown99adef32014-01-16 12:22:43 +00001229 ret = spi_map_msg(master, master->cur_msg);
1230 if (ret) {
1231 master->cur_msg->status = ret;
1232 spi_finalize_current_message(master);
Jon Hunter49023d22016-03-08 12:28:20 +00001233 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001234 }
1235
Linus Walleijffbbdd212012-02-22 10:05:38 +01001236 ret = master->transfer_one_message(master, master->cur_msg);
1237 if (ret) {
1238 dev_err(&master->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001239 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001240 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001241 }
Jon Hunter49023d22016-03-08 12:28:20 +00001242
1243out:
Mark Brownef4d96e2016-07-21 23:53:31 +01001244 mutex_unlock(&master->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001245
1246 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001247 if (!ret)
1248 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001249}
1250
Mark Brownfc9e0f72014-12-10 13:46:33 +00001251/**
1252 * spi_pump_messages - kthread work function which processes spi message queue
1253 * @work: pointer to kthread work struct contained in the master struct
1254 */
1255static void spi_pump_messages(struct kthread_work *work)
1256{
1257 struct spi_master *master =
1258 container_of(work, struct spi_master, pump_messages);
1259
Mark Brownef4d96e2016-07-21 23:53:31 +01001260 __spi_pump_messages(master, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001261}
1262
Linus Walleijffbbdd212012-02-22 10:05:38 +01001263static int spi_init_queue(struct spi_master *master)
1264{
1265 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1266
Linus Walleijffbbdd212012-02-22 10:05:38 +01001267 master->running = false;
1268 master->busy = false;
1269
Petr Mladek39891442016-10-11 13:55:20 -07001270 kthread_init_worker(&master->kworker);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001271 master->kworker_task = kthread_run(kthread_worker_fn,
Kees Cookf1701682013-07-03 15:04:58 -07001272 &master->kworker, "%s",
Linus Walleijffbbdd212012-02-22 10:05:38 +01001273 dev_name(&master->dev));
1274 if (IS_ERR(master->kworker_task)) {
1275 dev_err(&master->dev, "failed to create message pump task\n");
Jarkko Nikula98a8f5a2014-12-04 11:02:25 +02001276 return PTR_ERR(master->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001277 }
Petr Mladek39891442016-10-11 13:55:20 -07001278 kthread_init_work(&master->pump_messages, spi_pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001279
1280 /*
1281 * Master config will indicate if this controller should run the
1282 * message pump with high (realtime) priority to reduce the transfer
1283 * latency on the bus by minimising the delay between a transfer
1284 * request and the scheduling of the message pump thread. Without this
1285 * setting the message pump thread will remain at default priority.
1286 */
1287 if (master->rt) {
1288 dev_info(&master->dev,
1289 "will run message pump with realtime priority\n");
1290 sched_setscheduler(master->kworker_task, SCHED_FIFO, &param);
1291 }
1292
1293 return 0;
1294}
1295
1296/**
1297 * spi_get_next_queued_message() - called by driver to check for queued
1298 * messages
1299 * @master: the master to check for queued messages
1300 *
1301 * If there are more messages in the queue, the next message is returned from
1302 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001303 *
1304 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001305 */
1306struct spi_message *spi_get_next_queued_message(struct spi_master *master)
1307{
1308 struct spi_message *next;
1309 unsigned long flags;
1310
1311 /* get a pointer to the next message, if any */
1312 spin_lock_irqsave(&master->queue_lock, flags);
Axel Lin1cfd97f2014-01-02 15:16:40 +08001313 next = list_first_entry_or_null(&master->queue, struct spi_message,
1314 queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001315 spin_unlock_irqrestore(&master->queue_lock, flags);
1316
1317 return next;
1318}
1319EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1320
1321/**
1322 * spi_finalize_current_message() - the current message is complete
1323 * @master: the master to return the message to
1324 *
1325 * Called by the driver to notify the core that the message in the front of the
1326 * queue is complete and can be removed from the queue.
1327 */
1328void spi_finalize_current_message(struct spi_master *master)
1329{
1330 struct spi_message *mesg;
1331 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001332 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001333
1334 spin_lock_irqsave(&master->queue_lock, flags);
1335 mesg = master->cur_msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001336 spin_unlock_irqrestore(&master->queue_lock, flags);
1337
Mark Brown99adef32014-01-16 12:22:43 +00001338 spi_unmap_msg(master, mesg);
1339
Mark Brown2841a5f2013-10-05 00:23:12 +01001340 if (master->cur_msg_prepared && master->unprepare_message) {
1341 ret = master->unprepare_message(master, mesg);
1342 if (ret) {
1343 dev_err(&master->dev,
1344 "failed to unprepare message: %d\n", ret);
1345 }
1346 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001347
Martin Sperl8e76ef82015-05-10 07:50:45 +00001348 spin_lock_irqsave(&master->queue_lock, flags);
1349 master->cur_msg = NULL;
Mark Brown2841a5f2013-10-05 00:23:12 +01001350 master->cur_msg_prepared = false;
Petr Mladek39891442016-10-11 13:55:20 -07001351 kthread_queue_work(&master->kworker, &master->pump_messages);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001352 spin_unlock_irqrestore(&master->queue_lock, flags);
1353
1354 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001355
Linus Walleijffbbdd212012-02-22 10:05:38 +01001356 mesg->state = NULL;
1357 if (mesg->complete)
1358 mesg->complete(mesg->context);
1359}
1360EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1361
1362static int spi_start_queue(struct spi_master *master)
1363{
1364 unsigned long flags;
1365
1366 spin_lock_irqsave(&master->queue_lock, flags);
1367
1368 if (master->running || master->busy) {
1369 spin_unlock_irqrestore(&master->queue_lock, flags);
1370 return -EBUSY;
1371 }
1372
1373 master->running = true;
1374 master->cur_msg = NULL;
1375 spin_unlock_irqrestore(&master->queue_lock, flags);
1376
Petr Mladek39891442016-10-11 13:55:20 -07001377 kthread_queue_work(&master->kworker, &master->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001378
1379 return 0;
1380}
1381
1382static int spi_stop_queue(struct spi_master *master)
1383{
1384 unsigned long flags;
1385 unsigned limit = 500;
1386 int ret = 0;
1387
1388 spin_lock_irqsave(&master->queue_lock, flags);
1389
1390 /*
1391 * This is a bit lame, but is optimized for the common execution path.
1392 * A wait_queue on the master->busy could be used, but then the common
1393 * execution path (pump_messages) would be required to call wake_up or
1394 * friends on every SPI message. Do this instead.
1395 */
1396 while ((!list_empty(&master->queue) || master->busy) && limit--) {
1397 spin_unlock_irqrestore(&master->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001398 usleep_range(10000, 11000);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001399 spin_lock_irqsave(&master->queue_lock, flags);
1400 }
1401
1402 if (!list_empty(&master->queue) || master->busy)
1403 ret = -EBUSY;
1404 else
1405 master->running = false;
1406
1407 spin_unlock_irqrestore(&master->queue_lock, flags);
1408
1409 if (ret) {
1410 dev_warn(&master->dev,
1411 "could not stop message queue\n");
1412 return ret;
1413 }
1414 return ret;
1415}
1416
1417static int spi_destroy_queue(struct spi_master *master)
1418{
1419 int ret;
1420
1421 ret = spi_stop_queue(master);
1422
1423 /*
Petr Mladek39891442016-10-11 13:55:20 -07001424 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001425 * If the reason that stop_queue timed out is that the work will never
1426 * finish, then it does no good to call flush/stop thread, so
1427 * return anyway.
1428 */
1429 if (ret) {
1430 dev_err(&master->dev, "problem destroying queue\n");
1431 return ret;
1432 }
1433
Petr Mladek39891442016-10-11 13:55:20 -07001434 kthread_flush_worker(&master->kworker);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001435 kthread_stop(master->kworker_task);
1436
1437 return 0;
1438}
1439
Mark Brown0461a412014-12-09 21:38:05 +00001440static int __spi_queued_transfer(struct spi_device *spi,
1441 struct spi_message *msg,
1442 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001443{
1444 struct spi_master *master = spi->master;
1445 unsigned long flags;
1446
1447 spin_lock_irqsave(&master->queue_lock, flags);
1448
1449 if (!master->running) {
1450 spin_unlock_irqrestore(&master->queue_lock, flags);
1451 return -ESHUTDOWN;
1452 }
1453 msg->actual_length = 0;
1454 msg->status = -EINPROGRESS;
1455
1456 list_add_tail(&msg->queue, &master->queue);
Mark Brown0461a412014-12-09 21:38:05 +00001457 if (!master->busy && need_pump)
Petr Mladek39891442016-10-11 13:55:20 -07001458 kthread_queue_work(&master->kworker, &master->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001459
1460 spin_unlock_irqrestore(&master->queue_lock, flags);
1461 return 0;
1462}
1463
Mark Brown0461a412014-12-09 21:38:05 +00001464/**
1465 * spi_queued_transfer - transfer function for queued transfers
1466 * @spi: spi device which is requesting transfer
1467 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001468 *
1469 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001470 */
1471static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1472{
1473 return __spi_queued_transfer(spi, msg, true);
1474}
1475
Linus Walleijffbbdd212012-02-22 10:05:38 +01001476static int spi_master_initialize_queue(struct spi_master *master)
1477{
1478 int ret;
1479
Linus Walleijffbbdd212012-02-22 10:05:38 +01001480 master->transfer = spi_queued_transfer;
Mark Brownb1589352013-10-05 11:50:40 +01001481 if (!master->transfer_one_message)
1482 master->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001483
1484 /* Initialize and start queue */
1485 ret = spi_init_queue(master);
1486 if (ret) {
1487 dev_err(&master->dev, "problem initializing queue\n");
1488 goto err_init_queue;
1489 }
Mark Brownc3676d52014-05-01 10:47:52 -07001490 master->queued = true;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001491 ret = spi_start_queue(master);
1492 if (ret) {
1493 dev_err(&master->dev, "problem starting queue\n");
1494 goto err_start_queue;
1495 }
1496
1497 return 0;
1498
1499err_start_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001500 spi_destroy_queue(master);
Mark Brownc3676d52014-05-01 10:47:52 -07001501err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001502 return ret;
1503}
1504
1505/*-------------------------------------------------------------------------*/
1506
Andreas Larsson7cb94362012-12-04 15:09:38 +01001507#if defined(CONFIG_OF)
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001508static int of_spi_parse_dt(struct spi_master *master, struct spi_device *spi,
1509 struct device_node *nc)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001510{
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001511 u32 value;
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001512 int rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001513
1514 /* Device address */
1515 rc = of_property_read_u32(nc, "reg", &value);
1516 if (rc) {
1517 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1518 nc->full_name, rc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001519 return rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001520 }
1521 spi->chip_select = value;
1522
1523 /* Mode (clock phase/polarity/etc.) */
1524 if (of_find_property(nc, "spi-cpha", NULL))
1525 spi->mode |= SPI_CPHA;
1526 if (of_find_property(nc, "spi-cpol", NULL))
1527 spi->mode |= SPI_CPOL;
1528 if (of_find_property(nc, "spi-cs-high", NULL))
1529 spi->mode |= SPI_CS_HIGH;
1530 if (of_find_property(nc, "spi-3wire", NULL))
1531 spi->mode |= SPI_3WIRE;
1532 if (of_find_property(nc, "spi-lsb-first", NULL))
1533 spi->mode |= SPI_LSB_FIRST;
1534
1535 /* Device DUAL/QUAD mode */
1536 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1537 switch (value) {
1538 case 1:
1539 break;
1540 case 2:
1541 spi->mode |= SPI_TX_DUAL;
1542 break;
1543 case 4:
1544 spi->mode |= SPI_TX_QUAD;
1545 break;
1546 default:
1547 dev_warn(&master->dev,
1548 "spi-tx-bus-width %d not supported\n",
1549 value);
1550 break;
1551 }
1552 }
1553
1554 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1555 switch (value) {
1556 case 1:
1557 break;
1558 case 2:
1559 spi->mode |= SPI_RX_DUAL;
1560 break;
1561 case 4:
1562 spi->mode |= SPI_RX_QUAD;
1563 break;
1564 default:
1565 dev_warn(&master->dev,
1566 "spi-rx-bus-width %d not supported\n",
1567 value);
1568 break;
1569 }
1570 }
1571
1572 /* Device speed */
1573 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1574 if (rc) {
1575 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1576 nc->full_name, rc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001577 return rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001578 }
1579 spi->max_speed_hz = value;
1580
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001581 return 0;
1582}
1583
1584static struct spi_device *
1585of_register_spi_device(struct spi_master *master, struct device_node *nc)
1586{
1587 struct spi_device *spi;
1588 int rc;
1589
1590 /* Alloc an spi_device */
1591 spi = spi_alloc_device(master);
1592 if (!spi) {
1593 dev_err(&master->dev, "spi_device alloc error for %s\n",
1594 nc->full_name);
1595 rc = -ENOMEM;
1596 goto err_out;
1597 }
1598
1599 /* Select device driver */
1600 rc = of_modalias_node(nc, spi->modalias,
1601 sizeof(spi->modalias));
1602 if (rc < 0) {
1603 dev_err(&master->dev, "cannot find modalias for %s\n",
1604 nc->full_name);
1605 goto err_out;
1606 }
1607
1608 rc = of_spi_parse_dt(master, spi, nc);
1609 if (rc)
1610 goto err_out;
1611
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001612 /* Store a pointer to the node in the device structure */
1613 of_node_get(nc);
1614 spi->dev.of_node = nc;
1615
1616 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001617 rc = spi_add_device(spi);
1618 if (rc) {
1619 dev_err(&master->dev, "spi_device register error %s\n",
1620 nc->full_name);
Johan Hovold83241472017-01-30 17:47:05 +01001621 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001622 }
1623
1624 return spi;
1625
Johan Hovold83241472017-01-30 17:47:05 +01001626err_of_node_put:
1627 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001628err_out:
1629 spi_dev_put(spi);
1630 return ERR_PTR(rc);
1631}
1632
Grant Likelyd57a4282012-04-07 14:16:53 -06001633/**
1634 * of_register_spi_devices() - Register child devices onto the SPI bus
1635 * @master: Pointer to spi_master device
1636 *
1637 * Registers an spi_device for each child node of master node which has a 'reg'
1638 * property.
1639 */
1640static void of_register_spi_devices(struct spi_master *master)
1641{
1642 struct spi_device *spi;
1643 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001644
1645 if (!master->dev.of_node)
1646 return;
1647
Alexander Sverdlinf3b61592012-11-29 08:59:29 +01001648 for_each_available_child_of_node(master->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01001649 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1650 continue;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001651 spi = of_register_spi_device(master, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001652 if (IS_ERR(spi)) {
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001653 dev_warn(&master->dev, "Failed to create SPI device for %s\n",
Grant Likelyd57a4282012-04-07 14:16:53 -06001654 nc->full_name);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001655 of_node_clear_flag(nc, OF_POPULATED);
1656 }
Grant Likelyd57a4282012-04-07 14:16:53 -06001657 }
1658}
1659#else
1660static void of_register_spi_devices(struct spi_master *master) { }
1661#endif
1662
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001663#ifdef CONFIG_ACPI
1664static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1665{
1666 struct spi_device *spi = data;
Mika Westerberga0a90712016-02-08 17:14:28 +02001667 struct spi_master *master = spi->master;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001668
1669 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1670 struct acpi_resource_spi_serialbus *sb;
1671
1672 sb = &ares->data.spi_serial_bus;
1673 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Mika Westerberga0a90712016-02-08 17:14:28 +02001674 /*
1675 * ACPI DeviceSelection numbering is handled by the
1676 * host controller driver in Windows and can vary
1677 * from driver to driver. In Linux we always expect
1678 * 0 .. max - 1 so we need to ask the driver to
1679 * translate between the two schemes.
1680 */
1681 if (master->fw_translate_cs) {
1682 int cs = master->fw_translate_cs(master,
1683 sb->device_selection);
1684 if (cs < 0)
1685 return cs;
1686 spi->chip_select = cs;
1687 } else {
1688 spi->chip_select = sb->device_selection;
1689 }
1690
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001691 spi->max_speed_hz = sb->connection_speed;
1692
1693 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1694 spi->mode |= SPI_CPHA;
1695 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1696 spi->mode |= SPI_CPOL;
1697 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1698 spi->mode |= SPI_CS_HIGH;
1699 }
1700 } else if (spi->irq < 0) {
1701 struct resource r;
1702
1703 if (acpi_dev_resource_interrupt(ares, 0, &r))
1704 spi->irq = r.start;
1705 }
1706
1707 /* Always tell the ACPI core to skip this resource */
1708 return 1;
1709}
1710
Octavian Purdila7f244672016-07-08 19:13:11 +03001711static acpi_status acpi_register_spi_device(struct spi_master *master,
1712 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001713{
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001714 struct list_head resource_list;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001715 struct spi_device *spi;
1716 int ret;
1717
Octavian Purdila7f244672016-07-08 19:13:11 +03001718 if (acpi_bus_get_status(adev) || !adev->status.present ||
1719 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001720 return AE_OK;
1721
1722 spi = spi_alloc_device(master);
1723 if (!spi) {
1724 dev_err(&master->dev, "failed to allocate SPI device for %s\n",
1725 dev_name(&adev->dev));
1726 return AE_NO_MEMORY;
1727 }
1728
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001729 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001730 spi->irq = -1;
1731
1732 INIT_LIST_HEAD(&resource_list);
1733 ret = acpi_dev_get_resources(adev, &resource_list,
1734 acpi_spi_add_resource, spi);
1735 acpi_dev_free_resource_list(&resource_list);
1736
1737 if (ret < 0 || !spi->max_speed_hz) {
1738 spi_dev_put(spi);
1739 return AE_OK;
1740 }
1741
Dan O'Donovan0c6543f2017-02-05 16:30:14 +00001742 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
1743 sizeof(spi->modalias));
1744
Christophe RICARD33ada672015-12-23 23:25:35 +01001745 if (spi->irq < 0)
1746 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1747
Octavian Purdila7f244672016-07-08 19:13:11 +03001748 acpi_device_set_enumerated(adev);
1749
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001750 adev->power.flags.ignore_parent = true;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001751 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001752 adev->power.flags.ignore_parent = false;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001753 dev_err(&master->dev, "failed to add SPI device %s from ACPI\n",
1754 dev_name(&adev->dev));
1755 spi_dev_put(spi);
1756 }
1757
1758 return AE_OK;
1759}
1760
Octavian Purdila7f244672016-07-08 19:13:11 +03001761static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1762 void *data, void **return_value)
1763{
1764 struct spi_master *master = data;
1765 struct acpi_device *adev;
1766
1767 if (acpi_bus_get_device(handle, &adev))
1768 return AE_OK;
1769
1770 return acpi_register_spi_device(master, adev);
1771}
1772
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001773static void acpi_register_spi_devices(struct spi_master *master)
1774{
1775 acpi_status status;
1776 acpi_handle handle;
1777
Rafael J. Wysocki29896172013-04-01 00:21:08 +00001778 handle = ACPI_HANDLE(master->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001779 if (!handle)
1780 return;
1781
1782 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1783 acpi_spi_add_device, NULL,
1784 master, NULL);
1785 if (ACPI_FAILURE(status))
1786 dev_warn(&master->dev, "failed to enumerate SPI slaves\n");
1787}
1788#else
1789static inline void acpi_register_spi_devices(struct spi_master *master) {}
1790#endif /* CONFIG_ACPI */
1791
Tony Jones49dce682007-10-16 01:27:48 -07001792static void spi_master_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001793{
1794 struct spi_master *master;
1795
Tony Jones49dce682007-10-16 01:27:48 -07001796 master = container_of(dev, struct spi_master, dev);
David Brownell8ae12a02006-01-08 13:34:19 -08001797 kfree(master);
1798}
1799
1800static struct class spi_master_class = {
1801 .name = "spi_master",
1802 .owner = THIS_MODULE,
Tony Jones49dce682007-10-16 01:27:48 -07001803 .dev_release = spi_master_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00001804 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08001805};
1806
1807
1808/**
1809 * spi_alloc_master - allocate SPI master controller
1810 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001811 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001812 * memory is in the driver_data field of the returned device,
David Brownell0c868462006-01-08 13:34:25 -08001813 * accessible with spi_master_get_devdata().
David Brownell33e34dc2007-05-08 00:32:21 -07001814 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001815 *
1816 * This call is used only by SPI master controller drivers, which are the
1817 * only ones directly touching chip registers. It's how they allocate
dmitry pervushinba1a0512006-05-20 15:00:14 -07001818 * an spi_master structure, prior to calling spi_register_master().
David Brownell8ae12a02006-01-08 13:34:19 -08001819 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001820 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08001821 *
1822 * The caller is responsible for assigning the bus number and initializing
dmitry pervushinba1a0512006-05-20 15:00:14 -07001823 * the master's methods before calling spi_register_master(); and (after errors
Guenter Roecka394d632015-09-06 01:46:54 +03001824 * adding the device) calling spi_master_put() to prevent a memory leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001825 *
1826 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08001827 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001828struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
David Brownell8ae12a02006-01-08 13:34:19 -08001829{
1830 struct spi_master *master;
1831
David Brownell0c868462006-01-08 13:34:25 -08001832 if (!dev)
1833 return NULL;
1834
Jingoo Han5fe5f052013-10-14 10:31:51 +09001835 master = kzalloc(size + sizeof(*master), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -08001836 if (!master)
1837 return NULL;
1838
Tony Jones49dce682007-10-16 01:27:48 -07001839 device_initialize(&master->dev);
Grant Likely1e8a52e2012-05-19 23:42:08 -06001840 master->bus_num = -1;
1841 master->num_chipselect = 1;
Tony Jones49dce682007-10-16 01:27:48 -07001842 master->dev.class = &spi_master_class;
Johan Hovold157f38f2015-12-14 16:16:19 +01001843 master->dev.parent = dev;
Linus Walleijd7e2ee22016-04-11 13:51:03 +02001844 pm_suspend_ignore_children(&master->dev, true);
David Brownell0c868462006-01-08 13:34:25 -08001845 spi_master_set_devdata(master, &master[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001846
1847 return master;
1848}
1849EXPORT_SYMBOL_GPL(spi_alloc_master);
1850
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001851#ifdef CONFIG_OF
1852static int of_spi_register_master(struct spi_master *master)
1853{
Grant Likelye80beb22013-02-12 17:48:37 +00001854 int nb, i, *cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001855 struct device_node *np = master->dev.of_node;
1856
1857 if (!np)
1858 return 0;
1859
1860 nb = of_gpio_named_count(np, "cs-gpios");
Jingoo Han5fe5f052013-10-14 10:31:51 +09001861 master->num_chipselect = max_t(int, nb, master->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001862
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001863 /* Return error only for an incorrectly formed cs-gpios property */
1864 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001865 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01001866 else if (nb < 0)
1867 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001868
1869 cs = devm_kzalloc(&master->dev,
1870 sizeof(int) * master->num_chipselect,
1871 GFP_KERNEL);
1872 master->cs_gpios = cs;
1873
1874 if (!master->cs_gpios)
1875 return -ENOMEM;
1876
Andreas Larsson0da83bb2013-01-29 15:53:40 +01001877 for (i = 0; i < master->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01001878 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001879
1880 for (i = 0; i < nb; i++)
1881 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
1882
1883 return 0;
1884}
1885#else
1886static int of_spi_register_master(struct spi_master *master)
1887{
1888 return 0;
1889}
1890#endif
1891
David Brownell8ae12a02006-01-08 13:34:19 -08001892/**
1893 * spi_register_master - register SPI master controller
1894 * @master: initialized master, originally from spi_alloc_master()
David Brownell33e34dc2007-05-08 00:32:21 -07001895 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001896 *
1897 * SPI master controllers connect to their drivers using some non-SPI bus,
1898 * such as the platform bus. The final stage of probe() in that code
1899 * includes calling spi_register_master() to hook up to this SPI bus glue.
1900 *
1901 * SPI controllers use board specific (often SOC specific) bus numbers,
1902 * and board-specific addressing for SPI devices combines those numbers
1903 * with chip select numbers. Since SPI does not directly support dynamic
1904 * device identification, boards need configuration tables telling which
1905 * chip is at which address.
1906 *
1907 * This must be called from context that can sleep. It returns zero on
1908 * success, else a negative error code (dropping the master's refcount).
David Brownell0c868462006-01-08 13:34:25 -08001909 * After a successful return, the caller is responsible for calling
1910 * spi_unregister_master().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001911 *
1912 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08001913 */
Adrian Bunke9d5a462007-03-26 21:32:23 -08001914int spi_register_master(struct spi_master *master)
David Brownell8ae12a02006-01-08 13:34:19 -08001915{
David Brownelle44a45a2007-06-03 13:50:40 -07001916 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Tony Jones49dce682007-10-16 01:27:48 -07001917 struct device *dev = master->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08001918 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08001919 int status = -ENODEV;
1920 int dynamic = 0;
1921
David Brownell0c868462006-01-08 13:34:25 -08001922 if (!dev)
1923 return -ENODEV;
1924
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001925 status = of_spi_register_master(master);
1926 if (status)
1927 return status;
1928
David Brownell082c8cb2007-07-31 00:39:45 -07001929 /* even if it's just one always-selected device, there must
1930 * be at least one chipselect
1931 */
1932 if (master->num_chipselect == 0)
1933 return -EINVAL;
1934
Grant Likelybb297852012-12-21 19:32:09 +00001935 if ((master->bus_num < 0) && master->dev.of_node)
1936 master->bus_num = of_alias_get_id(master->dev.of_node, "spi");
1937
David Brownell8ae12a02006-01-08 13:34:19 -08001938 /* convention: dynamically assigned bus IDs count down from the max */
David Brownella020ed72006-04-03 15:49:04 -07001939 if (master->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07001940 /* FIXME switch to an IDR based scheme, something like
1941 * I2C now uses, so we can't run out of "dynamic" IDs
1942 */
David Brownell8ae12a02006-01-08 13:34:19 -08001943 master->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08001944 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08001945 }
1946
Mark Brown5424d432014-12-10 17:40:53 +00001947 INIT_LIST_HEAD(&master->queue);
1948 spin_lock_init(&master->queue_lock);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07001949 spin_lock_init(&master->bus_lock_spinlock);
1950 mutex_init(&master->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001951 mutex_init(&master->io_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07001952 master->bus_lock_flag = 0;
Mark Brownb1589352013-10-05 11:50:40 +01001953 init_completion(&master->xfer_completion);
Mark Brown6ad45a22014-02-02 13:47:47 +00001954 if (!master->max_dma_len)
1955 master->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07001956
David Brownell8ae12a02006-01-08 13:34:19 -08001957 /* register the device, then userspace will see it.
1958 * registration fails if the bus ID is in use.
1959 */
Kay Sievers35f74fc2009-01-06 10:44:37 -08001960 dev_set_name(&master->dev, "spi%u", master->bus_num);
Tony Jones49dce682007-10-16 01:27:48 -07001961 status = device_add(&master->dev);
David Brownellb8852442006-01-08 13:34:23 -08001962 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08001963 goto done;
Kay Sievers35f74fc2009-01-06 10:44:37 -08001964 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
David Brownell8ae12a02006-01-08 13:34:19 -08001965 dynamic ? " (dynamic)" : "");
1966
Linus Walleijffbbdd212012-02-22 10:05:38 +01001967 /* If we're using a queued driver, start the queue */
1968 if (master->transfer)
1969 dev_info(dev, "master is unqueued, this is deprecated\n");
1970 else {
1971 status = spi_master_initialize_queue(master);
1972 if (status) {
Axel Line93b0722013-08-31 20:25:52 +08001973 device_del(&master->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001974 goto done;
1975 }
1976 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00001977 /* add statistics */
1978 spin_lock_init(&master->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001979
Feng Tang2b9603a2010-08-02 15:52:15 +08001980 mutex_lock(&board_lock);
1981 list_add_tail(&master->list, &spi_master_list);
1982 list_for_each_entry(bi, &board_list, list)
1983 spi_match_master_to_boardinfo(master, &bi->board_info);
1984 mutex_unlock(&board_lock);
1985
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001986 /* Register devices from the device tree and ACPI */
Anatolij Gustschin12b15e82010-07-27 22:35:58 +02001987 of_register_spi_devices(master);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001988 acpi_register_spi_devices(master);
David Brownell8ae12a02006-01-08 13:34:19 -08001989done:
1990 return status;
1991}
1992EXPORT_SYMBOL_GPL(spi_register_master);
1993
Mark Brown666d5b42013-08-31 18:50:52 +01001994static void devm_spi_unregister(struct device *dev, void *res)
1995{
1996 spi_unregister_master(*(struct spi_master **)res);
1997}
1998
1999/**
2000 * dev_spi_register_master - register managed SPI master controller
2001 * @dev: device managing SPI master
2002 * @master: initialized master, originally from spi_alloc_master()
2003 * Context: can sleep
2004 *
2005 * Register a SPI device as with spi_register_master() which will
2006 * automatically be unregister
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002007 *
2008 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01002009 */
2010int devm_spi_register_master(struct device *dev, struct spi_master *master)
2011{
2012 struct spi_master **ptr;
2013 int ret;
2014
2015 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2016 if (!ptr)
2017 return -ENOMEM;
2018
2019 ret = spi_register_master(master);
Stephen Warren4b928942013-11-21 16:11:15 -07002020 if (!ret) {
Mark Brown666d5b42013-08-31 18:50:52 +01002021 *ptr = master;
2022 devres_add(dev, ptr);
2023 } else {
2024 devres_free(ptr);
2025 }
2026
2027 return ret;
2028}
2029EXPORT_SYMBOL_GPL(devm_spi_register_master);
2030
David Lamparter34860082010-08-30 23:54:17 +02002031static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002032{
David Lamparter34860082010-08-30 23:54:17 +02002033 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002034 return 0;
2035}
2036
2037/**
2038 * spi_unregister_master - unregister SPI master controller
2039 * @master: the master being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002040 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002041 *
2042 * This call is used only by SPI master controller drivers, which are the
2043 * only ones directly touching chip registers.
2044 *
2045 * This must be called from context that can sleep.
2046 */
2047void spi_unregister_master(struct spi_master *master)
2048{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002049 int dummy;
2050
Linus Walleijffbbdd212012-02-22 10:05:38 +01002051 if (master->queued) {
2052 if (spi_destroy_queue(master))
2053 dev_err(&master->dev, "queue remove failed\n");
2054 }
2055
Feng Tang2b9603a2010-08-02 15:52:15 +08002056 mutex_lock(&board_lock);
2057 list_del(&master->list);
2058 mutex_unlock(&board_lock);
2059
Sebastian Andrzej Siewior97dbf372010-12-21 17:24:31 -08002060 dummy = device_for_each_child(&master->dev, NULL, __unregister);
Tony Jones49dce682007-10-16 01:27:48 -07002061 device_unregister(&master->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08002062}
2063EXPORT_SYMBOL_GPL(spi_unregister_master);
2064
Linus Walleijffbbdd212012-02-22 10:05:38 +01002065int spi_master_suspend(struct spi_master *master)
2066{
2067 int ret;
2068
2069 /* Basically no-ops for non-queued masters */
2070 if (!master->queued)
2071 return 0;
2072
2073 ret = spi_stop_queue(master);
2074 if (ret)
2075 dev_err(&master->dev, "queue stop failed\n");
2076
2077 return ret;
2078}
2079EXPORT_SYMBOL_GPL(spi_master_suspend);
2080
2081int spi_master_resume(struct spi_master *master)
2082{
2083 int ret;
2084
2085 if (!master->queued)
2086 return 0;
2087
2088 ret = spi_start_queue(master);
2089 if (ret)
2090 dev_err(&master->dev, "queue restart failed\n");
2091
2092 return ret;
2093}
2094EXPORT_SYMBOL_GPL(spi_master_resume);
2095
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002096static int __spi_master_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002097{
2098 struct spi_master *m;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002099 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002100
2101 m = container_of(dev, struct spi_master, dev);
2102 return m->bus_num == *bus_num;
2103}
2104
David Brownell8ae12a02006-01-08 13:34:19 -08002105/**
2106 * spi_busnum_to_master - look up master associated with bus_num
2107 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002108 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002109 *
2110 * This call may be used with devices that are registered after
2111 * arch init time. It returns a refcounted pointer to the relevant
2112 * spi_master (which the caller must release), or NULL if there is
2113 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002114 *
2115 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002116 */
2117struct spi_master *spi_busnum_to_master(u16 bus_num)
2118{
Tony Jones49dce682007-10-16 01:27:48 -07002119 struct device *dev;
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08002120 struct spi_master *master = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002121
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002122 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Dave Young5ed2c832008-01-22 15:14:18 +08002123 __spi_master_match);
2124 if (dev)
2125 master = container_of(dev, struct spi_master, dev);
2126 /* reference got in class_find_device */
Atsushi Nemoto1e9a51d2007-01-26 00:56:54 -08002127 return master;
David Brownell8ae12a02006-01-08 13:34:19 -08002128}
2129EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2130
Martin Sperld780c372015-12-14 15:20:18 +00002131/*-------------------------------------------------------------------------*/
2132
2133/* Core methods for SPI resource management */
2134
2135/**
2136 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2137 * during the processing of a spi_message while using
2138 * spi_transfer_one
2139 * @spi: the spi device for which we allocate memory
2140 * @release: the release code to execute for this resource
2141 * @size: size to alloc and return
2142 * @gfp: GFP allocation flags
2143 *
2144 * Return: the pointer to the allocated data
2145 *
2146 * This may get enhanced in the future to allocate from a memory pool
2147 * of the @spi_device or @spi_master to avoid repeated allocations.
2148 */
2149void *spi_res_alloc(struct spi_device *spi,
2150 spi_res_release_t release,
2151 size_t size, gfp_t gfp)
2152{
2153 struct spi_res *sres;
2154
2155 sres = kzalloc(sizeof(*sres) + size, gfp);
2156 if (!sres)
2157 return NULL;
2158
2159 INIT_LIST_HEAD(&sres->entry);
2160 sres->release = release;
2161
2162 return sres->data;
2163}
2164EXPORT_SYMBOL_GPL(spi_res_alloc);
2165
2166/**
2167 * spi_res_free - free an spi resource
2168 * @res: pointer to the custom data of a resource
2169 *
2170 */
2171void spi_res_free(void *res)
2172{
2173 struct spi_res *sres = container_of(res, struct spi_res, data);
2174
2175 if (!res)
2176 return;
2177
2178 WARN_ON(!list_empty(&sres->entry));
2179 kfree(sres);
2180}
2181EXPORT_SYMBOL_GPL(spi_res_free);
2182
2183/**
2184 * spi_res_add - add a spi_res to the spi_message
2185 * @message: the spi message
2186 * @res: the spi_resource
2187 */
2188void spi_res_add(struct spi_message *message, void *res)
2189{
2190 struct spi_res *sres = container_of(res, struct spi_res, data);
2191
2192 WARN_ON(!list_empty(&sres->entry));
2193 list_add_tail(&sres->entry, &message->resources);
2194}
2195EXPORT_SYMBOL_GPL(spi_res_add);
2196
2197/**
2198 * spi_res_release - release all spi resources for this message
2199 * @master: the @spi_master
2200 * @message: the @spi_message
2201 */
2202void spi_res_release(struct spi_master *master,
2203 struct spi_message *message)
2204{
2205 struct spi_res *res;
2206
2207 while (!list_empty(&message->resources)) {
2208 res = list_last_entry(&message->resources,
2209 struct spi_res, entry);
2210
2211 if (res->release)
2212 res->release(master, message, res->data);
2213
2214 list_del(&res->entry);
2215
2216 kfree(res);
2217 }
2218}
2219EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002220
2221/*-------------------------------------------------------------------------*/
2222
Martin Sperl523baf5a2015-12-14 15:20:19 +00002223/* Core methods for spi_message alterations */
2224
2225static void __spi_replace_transfers_release(struct spi_master *master,
2226 struct spi_message *msg,
2227 void *res)
2228{
2229 struct spi_replaced_transfers *rxfer = res;
2230 size_t i;
2231
2232 /* call extra callback if requested */
2233 if (rxfer->release)
2234 rxfer->release(master, msg, res);
2235
2236 /* insert replaced transfers back into the message */
2237 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2238
2239 /* remove the formerly inserted entries */
2240 for (i = 0; i < rxfer->inserted; i++)
2241 list_del(&rxfer->inserted_transfers[i].transfer_list);
2242}
2243
2244/**
2245 * spi_replace_transfers - replace transfers with several transfers
2246 * and register change with spi_message.resources
2247 * @msg: the spi_message we work upon
2248 * @xfer_first: the first spi_transfer we want to replace
2249 * @remove: number of transfers to remove
2250 * @insert: the number of transfers we want to insert instead
2251 * @release: extra release code necessary in some circumstances
2252 * @extradatasize: extra data to allocate (with alignment guarantees
2253 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002254 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002255 *
2256 * Returns: pointer to @spi_replaced_transfers,
2257 * PTR_ERR(...) in case of errors.
2258 */
2259struct spi_replaced_transfers *spi_replace_transfers(
2260 struct spi_message *msg,
2261 struct spi_transfer *xfer_first,
2262 size_t remove,
2263 size_t insert,
2264 spi_replaced_release_t release,
2265 size_t extradatasize,
2266 gfp_t gfp)
2267{
2268 struct spi_replaced_transfers *rxfer;
2269 struct spi_transfer *xfer;
2270 size_t i;
2271
2272 /* allocate the structure using spi_res */
2273 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2274 insert * sizeof(struct spi_transfer)
2275 + sizeof(struct spi_replaced_transfers)
2276 + extradatasize,
2277 gfp);
2278 if (!rxfer)
2279 return ERR_PTR(-ENOMEM);
2280
2281 /* the release code to invoke before running the generic release */
2282 rxfer->release = release;
2283
2284 /* assign extradata */
2285 if (extradatasize)
2286 rxfer->extradata =
2287 &rxfer->inserted_transfers[insert];
2288
2289 /* init the replaced_transfers list */
2290 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2291
2292 /* assign the list_entry after which we should reinsert
2293 * the @replaced_transfers - it may be spi_message.messages!
2294 */
2295 rxfer->replaced_after = xfer_first->transfer_list.prev;
2296
2297 /* remove the requested number of transfers */
2298 for (i = 0; i < remove; i++) {
2299 /* if the entry after replaced_after it is msg->transfers
2300 * then we have been requested to remove more transfers
2301 * than are in the list
2302 */
2303 if (rxfer->replaced_after->next == &msg->transfers) {
2304 dev_err(&msg->spi->dev,
2305 "requested to remove more spi_transfers than are available\n");
2306 /* insert replaced transfers back into the message */
2307 list_splice(&rxfer->replaced_transfers,
2308 rxfer->replaced_after);
2309
2310 /* free the spi_replace_transfer structure */
2311 spi_res_free(rxfer);
2312
2313 /* and return with an error */
2314 return ERR_PTR(-EINVAL);
2315 }
2316
2317 /* remove the entry after replaced_after from list of
2318 * transfers and add it to list of replaced_transfers
2319 */
2320 list_move_tail(rxfer->replaced_after->next,
2321 &rxfer->replaced_transfers);
2322 }
2323
2324 /* create copy of the given xfer with identical settings
2325 * based on the first transfer to get removed
2326 */
2327 for (i = 0; i < insert; i++) {
2328 /* we need to run in reverse order */
2329 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2330
2331 /* copy all spi_transfer data */
2332 memcpy(xfer, xfer_first, sizeof(*xfer));
2333
2334 /* add to list */
2335 list_add(&xfer->transfer_list, rxfer->replaced_after);
2336
2337 /* clear cs_change and delay_usecs for all but the last */
2338 if (i) {
2339 xfer->cs_change = false;
2340 xfer->delay_usecs = 0;
2341 }
2342 }
2343
2344 /* set up inserted */
2345 rxfer->inserted = insert;
2346
2347 /* and register it with spi_res/spi_message */
2348 spi_res_add(msg, rxfer);
2349
2350 return rxfer;
2351}
2352EXPORT_SYMBOL_GPL(spi_replace_transfers);
2353
Fabio Estevam08933412016-02-14 13:33:50 -02002354static int __spi_split_transfer_maxsize(struct spi_master *master,
2355 struct spi_message *msg,
2356 struct spi_transfer **xferp,
2357 size_t maxsize,
2358 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00002359{
2360 struct spi_transfer *xfer = *xferp, *xfers;
2361 struct spi_replaced_transfers *srt;
2362 size_t offset;
2363 size_t count, i;
2364
2365 /* warn once about this fact that we are splitting a transfer */
2366 dev_warn_once(&msg->spi->dev,
Fabio Estevam7d62f512016-02-17 15:42:27 -02002367 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
Martin Sperld9f12122015-12-14 15:20:20 +00002368 xfer->len, maxsize);
2369
2370 /* calculate how many we have to replace */
2371 count = DIV_ROUND_UP(xfer->len, maxsize);
2372
2373 /* create replacement */
2374 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03002375 if (IS_ERR(srt))
2376 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00002377 xfers = srt->inserted_transfers;
2378
2379 /* now handle each of those newly inserted spi_transfers
2380 * note that the replacements spi_transfers all are preset
2381 * to the same values as *xferp, so tx_buf, rx_buf and len
2382 * are all identical (as well as most others)
2383 * so we just have to fix up len and the pointers.
2384 *
2385 * this also includes support for the depreciated
2386 * spi_message.is_dma_mapped interface
2387 */
2388
2389 /* the first transfer just needs the length modified, so we
2390 * run it outside the loop
2391 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02002392 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00002393
2394 /* all the others need rx_buf/tx_buf also set */
2395 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2396 /* update rx_buf, tx_buf and dma */
2397 if (xfers[i].rx_buf)
2398 xfers[i].rx_buf += offset;
2399 if (xfers[i].rx_dma)
2400 xfers[i].rx_dma += offset;
2401 if (xfers[i].tx_buf)
2402 xfers[i].tx_buf += offset;
2403 if (xfers[i].tx_dma)
2404 xfers[i].tx_dma += offset;
2405
2406 /* update length */
2407 xfers[i].len = min(maxsize, xfers[i].len - offset);
2408 }
2409
2410 /* we set up xferp to the last entry we have inserted,
2411 * so that we skip those already split transfers
2412 */
2413 *xferp = &xfers[count - 1];
2414
2415 /* increment statistics counters */
2416 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2417 transfers_split_maxsize);
2418 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2419 transfers_split_maxsize);
2420
2421 return 0;
2422}
2423
2424/**
2425 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2426 * when an individual transfer exceeds a
2427 * certain size
2428 * @master: the @spi_master for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09002429 * @msg: the @spi_message to transform
2430 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03002431 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00002432 *
2433 * Return: status of transformation
2434 */
2435int spi_split_transfers_maxsize(struct spi_master *master,
2436 struct spi_message *msg,
2437 size_t maxsize,
2438 gfp_t gfp)
2439{
2440 struct spi_transfer *xfer;
2441 int ret;
2442
2443 /* iterate over the transfer_list,
2444 * but note that xfer is advanced to the last transfer inserted
2445 * to avoid checking sizes again unnecessarily (also xfer does
2446 * potentiall belong to a different list by the time the
2447 * replacement has happened
2448 */
2449 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2450 if (xfer->len > maxsize) {
2451 ret = __spi_split_transfer_maxsize(
2452 master, msg, &xfer, maxsize, gfp);
2453 if (ret)
2454 return ret;
2455 }
2456 }
2457
2458 return 0;
2459}
2460EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08002461
2462/*-------------------------------------------------------------------------*/
2463
David Brownell7d077192009-06-17 16:26:03 -07002464/* Core methods for SPI master protocol drivers. Some of the
2465 * other core methods are currently defined as inline functions.
2466 */
2467
Stefan Brüns63ab6452015-08-23 16:06:30 +02002468static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
2469{
2470 if (master->bits_per_word_mask) {
2471 /* Only 32 bits fit in the mask */
2472 if (bits_per_word > 32)
2473 return -EINVAL;
2474 if (!(master->bits_per_word_mask &
2475 SPI_BPW_MASK(bits_per_word)))
2476 return -EINVAL;
2477 }
2478
2479 return 0;
2480}
2481
David Brownell7d077192009-06-17 16:26:03 -07002482/**
2483 * spi_setup - setup SPI mode and clock rate
2484 * @spi: the device whose settings are being modified
2485 * Context: can sleep, and no requests are queued to the device
2486 *
2487 * SPI protocol drivers may need to update the transfer mode if the
2488 * device doesn't work with its default. They may likewise need
2489 * to update clock rates or word sizes from initial values. This function
2490 * changes those settings, and must be called from a context that can sleep.
2491 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
2492 * effect the next time the device is selected and data is transferred to
2493 * or from it. When this function returns, the spi device is deselected.
2494 *
2495 * Note that this call will fail if the protocol driver specifies an option
2496 * that the underlying controller or its driver does not support. For
2497 * example, not all hardware supports wire transfers using nine bit words,
2498 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002499 *
2500 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07002501 */
2502int spi_setup(struct spi_device *spi)
2503{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002504 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002505 int status;
David Brownell7d077192009-06-17 16:26:03 -07002506
wangyuhangf477b7f2013-08-11 18:15:17 +08002507 /* check mode to prevent that DUAL and QUAD set at the same time
2508 */
2509 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
2510 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
2511 dev_err(&spi->dev,
2512 "setup: can not select dual and quad at the same time\n");
2513 return -EINVAL;
2514 }
2515 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
2516 */
2517 if ((spi->mode & SPI_3WIRE) && (spi->mode &
2518 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
2519 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07002520 /* help drivers fail *cleanly* when they need options
2521 * that aren't supported with their current master
2522 */
2523 bad_bits = spi->mode & ~spi->master->mode_bits;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002524 ugly_bits = bad_bits &
2525 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
2526 if (ugly_bits) {
2527 dev_warn(&spi->dev,
2528 "setup: ignoring unsupported mode bits %x\n",
2529 ugly_bits);
2530 spi->mode &= ~ugly_bits;
2531 bad_bits &= ~ugly_bits;
2532 }
David Brownelle7db06b2009-06-17 16:26:04 -07002533 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02002534 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07002535 bad_bits);
2536 return -EINVAL;
2537 }
2538
David Brownell7d077192009-06-17 16:26:03 -07002539 if (!spi->bits_per_word)
2540 spi->bits_per_word = 8;
2541
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002542 status = __spi_validate_bits_per_word(spi->master, spi->bits_per_word);
2543 if (status)
2544 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02002545
Axel Lin052eb2d2014-02-10 00:08:05 +08002546 if (!spi->max_speed_hz)
2547 spi->max_speed_hz = spi->master->max_speed_hz;
2548
Laxman Dewangancaae0702012-11-09 14:35:22 +05302549 if (spi->master->setup)
2550 status = spi->master->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07002551
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05002552 spi_set_cs(spi, false);
2553
Jingoo Han5fe5f052013-10-14 10:31:51 +09002554 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 -07002555 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2556 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2557 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2558 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2559 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2560 spi->bits_per_word, spi->max_speed_hz,
2561 status);
2562
2563 return status;
2564}
2565EXPORT_SYMBOL_GPL(spi_setup);
2566
Mark Brown90808732013-11-13 23:44:15 +00002567static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002568{
2569 struct spi_master *master = spi->master;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302570 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002571 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002572
Mark Brown24a00132013-07-10 15:05:40 +01002573 if (list_empty(&message->transfers))
2574 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01002575
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002576 /* Half-duplex links include original MicroWire, and ones with
2577 * only one data pin like SPI_3WIRE (switches direction) or where
2578 * either MOSI or MISO is missing. They can also be caused by
2579 * software limitations.
2580 */
2581 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
2582 || (spi->mode & SPI_3WIRE)) {
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002583 unsigned flags = master->flags;
2584
2585 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2586 if (xfer->rx_buf && xfer->tx_buf)
2587 return -EINVAL;
2588 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
2589 return -EINVAL;
2590 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
2591 return -EINVAL;
2592 }
2593 }
2594
Laxman Dewangane6811d12012-11-09 14:36:45 +05302595 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302596 * Set transfer bits_per_word and max speed as spi device default if
2597 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08002598 * Set transfer tx_nbits and rx_nbits as single transfer default
2599 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05302600 */
Martin Sperl77e80582015-11-27 12:31:09 +00002601 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302602 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05302603 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302604 if (!xfer->bits_per_word)
2605 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08002606
2607 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302608 xfer->speed_hz = spi->max_speed_hz;
Mark Brown7dc9fbc2015-08-20 11:52:18 -07002609 if (!xfer->speed_hz)
2610 xfer->speed_hz = master->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08002611
2612 if (master->max_speed_hz &&
2613 xfer->speed_hz > master->max_speed_hz)
2614 xfer->speed_hz = master->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02002615
Stefan Brüns63ab6452015-08-23 16:06:30 +02002616 if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
2617 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01002618
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002619 /*
2620 * SPI transfer length should be multiple of SPI word size
2621 * where SPI word size should be power-of-two multiple
2622 */
2623 if (xfer->bits_per_word <= 8)
2624 w_size = 1;
2625 else if (xfer->bits_per_word <= 16)
2626 w_size = 2;
2627 else
2628 w_size = 4;
2629
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002630 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002631 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002632 return -EINVAL;
2633
Mark Browna2fd4f92013-07-10 14:57:26 +01002634 if (xfer->speed_hz && master->min_speed_hz &&
2635 xfer->speed_hz < master->min_speed_hz)
2636 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08002637
2638 if (xfer->tx_buf && !xfer->tx_nbits)
2639 xfer->tx_nbits = SPI_NBITS_SINGLE;
2640 if (xfer->rx_buf && !xfer->rx_nbits)
2641 xfer->rx_nbits = SPI_NBITS_SINGLE;
2642 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01002643 * 1. check the value matches one of single, dual and quad
2644 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08002645 */
Sourav Poddardb90a442013-08-22 21:20:48 +05302646 if (xfer->tx_buf) {
2647 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2648 xfer->tx_nbits != SPI_NBITS_DUAL &&
2649 xfer->tx_nbits != SPI_NBITS_QUAD)
2650 return -EINVAL;
2651 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2652 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2653 return -EINVAL;
2654 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2655 !(spi->mode & SPI_TX_QUAD))
2656 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302657 }
wangyuhangf477b7f2013-08-11 18:15:17 +08002658 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05302659 if (xfer->rx_buf) {
2660 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2661 xfer->rx_nbits != SPI_NBITS_DUAL &&
2662 xfer->rx_nbits != SPI_NBITS_QUAD)
2663 return -EINVAL;
2664 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2665 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2666 return -EINVAL;
2667 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2668 !(spi->mode & SPI_RX_QUAD))
2669 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302670 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05302671 }
2672
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002673 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00002674
2675 return 0;
2676}
2677
2678static int __spi_async(struct spi_device *spi, struct spi_message *message)
2679{
2680 struct spi_master *master = spi->master;
2681
2682 message->spi = spi;
2683
Martin Sperleca2ebc2015-06-22 13:00:36 +00002684 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async);
2685 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2686
Mark Brown90808732013-11-13 23:44:15 +00002687 trace_spi_message_submit(message);
2688
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002689 return master->transfer(spi, message);
2690}
2691
David Brownell568d0692009-09-22 16:46:18 -07002692/**
2693 * spi_async - asynchronous SPI transfer
2694 * @spi: device with which data will be exchanged
2695 * @message: describes the data transfers, including completion callback
2696 * Context: any (irqs may be blocked, etc)
2697 *
2698 * This call may be used in_irq and other contexts which can't sleep,
2699 * as well as from task contexts which can sleep.
2700 *
2701 * The completion callback is invoked in a context which can't sleep.
2702 * Before that invocation, the value of message->status is undefined.
2703 * When the callback is issued, message->status holds either zero (to
2704 * indicate complete success) or a negative error code. After that
2705 * callback returns, the driver which issued the transfer request may
2706 * deallocate the associated memory; it's no longer in use by any SPI
2707 * core or controller driver code.
2708 *
2709 * Note that although all messages to a spi_device are handled in
2710 * FIFO order, messages may go to different devices in other orders.
2711 * Some device might be higher priority, or have various "hard" access
2712 * time requirements, for example.
2713 *
2714 * On detection of any fault during the transfer, processing of
2715 * the entire message is aborted, and the device is deselected.
2716 * Until returning from the associated message completion callback,
2717 * no other spi_message queued to that device will be processed.
2718 * (This rule applies equally to all the synchronous transfer calls,
2719 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002720 *
2721 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07002722 */
2723int spi_async(struct spi_device *spi, struct spi_message *message)
2724{
2725 struct spi_master *master = spi->master;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002726 int ret;
2727 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07002728
Mark Brown90808732013-11-13 23:44:15 +00002729 ret = __spi_validate(spi, message);
2730 if (ret != 0)
2731 return ret;
2732
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002733 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07002734
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002735 if (master->bus_lock_flag)
2736 ret = -EBUSY;
2737 else
2738 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07002739
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002740 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2741
2742 return ret;
David Brownell568d0692009-09-22 16:46:18 -07002743}
2744EXPORT_SYMBOL_GPL(spi_async);
2745
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002746/**
2747 * spi_async_locked - version of spi_async with exclusive bus usage
2748 * @spi: device with which data will be exchanged
2749 * @message: describes the data transfers, including completion callback
2750 * Context: any (irqs may be blocked, etc)
2751 *
2752 * This call may be used in_irq and other contexts which can't sleep,
2753 * as well as from task contexts which can sleep.
2754 *
2755 * The completion callback is invoked in a context which can't sleep.
2756 * Before that invocation, the value of message->status is undefined.
2757 * When the callback is issued, message->status holds either zero (to
2758 * indicate complete success) or a negative error code. After that
2759 * callback returns, the driver which issued the transfer request may
2760 * deallocate the associated memory; it's no longer in use by any SPI
2761 * core or controller driver code.
2762 *
2763 * Note that although all messages to a spi_device are handled in
2764 * FIFO order, messages may go to different devices in other orders.
2765 * Some device might be higher priority, or have various "hard" access
2766 * time requirements, for example.
2767 *
2768 * On detection of any fault during the transfer, processing of
2769 * the entire message is aborted, and the device is deselected.
2770 * Until returning from the associated message completion callback,
2771 * no other spi_message queued to that device will be processed.
2772 * (This rule applies equally to all the synchronous transfer calls,
2773 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002774 *
2775 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002776 */
2777int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2778{
2779 struct spi_master *master = spi->master;
2780 int ret;
2781 unsigned long flags;
2782
Mark Brown90808732013-11-13 23:44:15 +00002783 ret = __spi_validate(spi, message);
2784 if (ret != 0)
2785 return ret;
2786
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002787 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2788
2789 ret = __spi_async(spi, message);
2790
2791 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2792
2793 return ret;
2794
2795}
2796EXPORT_SYMBOL_GPL(spi_async_locked);
2797
David Brownell7d077192009-06-17 16:26:03 -07002798
Vignesh R556351f2015-12-11 09:39:56 +05302799int spi_flash_read(struct spi_device *spi,
2800 struct spi_flash_read_message *msg)
2801
2802{
2803 struct spi_master *master = spi->master;
Vignesh Rf4502dd2016-06-08 12:18:31 +05302804 struct device *rx_dev = NULL;
Vignesh R556351f2015-12-11 09:39:56 +05302805 int ret;
2806
2807 if ((msg->opcode_nbits == SPI_NBITS_DUAL ||
2808 msg->addr_nbits == SPI_NBITS_DUAL) &&
2809 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2810 return -EINVAL;
2811 if ((msg->opcode_nbits == SPI_NBITS_QUAD ||
2812 msg->addr_nbits == SPI_NBITS_QUAD) &&
2813 !(spi->mode & SPI_TX_QUAD))
2814 return -EINVAL;
2815 if (msg->data_nbits == SPI_NBITS_DUAL &&
2816 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2817 return -EINVAL;
2818 if (msg->data_nbits == SPI_NBITS_QUAD &&
2819 !(spi->mode & SPI_RX_QUAD))
2820 return -EINVAL;
2821
2822 if (master->auto_runtime_pm) {
2823 ret = pm_runtime_get_sync(master->dev.parent);
2824 if (ret < 0) {
2825 dev_err(&master->dev, "Failed to power device: %d\n",
2826 ret);
2827 return ret;
2828 }
2829 }
Vignesh Rf4502dd2016-06-08 12:18:31 +05302830
Vignesh R556351f2015-12-11 09:39:56 +05302831 mutex_lock(&master->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01002832 mutex_lock(&master->io_mutex);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302833 if (master->dma_rx) {
2834 rx_dev = master->dma_rx->device->dev;
2835 ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
2836 msg->buf, msg->len,
2837 DMA_FROM_DEVICE);
2838 if (!ret)
2839 msg->cur_msg_mapped = true;
2840 }
Vignesh R556351f2015-12-11 09:39:56 +05302841 ret = master->spi_flash_read(spi, msg);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302842 if (msg->cur_msg_mapped)
2843 spi_unmap_buf(master, rx_dev, &msg->rx_sg,
2844 DMA_FROM_DEVICE);
Mark Brownef4d96e2016-07-21 23:53:31 +01002845 mutex_unlock(&master->io_mutex);
Vignesh R556351f2015-12-11 09:39:56 +05302846 mutex_unlock(&master->bus_lock_mutex);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302847
Vignesh R556351f2015-12-11 09:39:56 +05302848 if (master->auto_runtime_pm)
2849 pm_runtime_put(master->dev.parent);
2850
2851 return ret;
2852}
2853EXPORT_SYMBOL_GPL(spi_flash_read);
2854
David Brownell7d077192009-06-17 16:26:03 -07002855/*-------------------------------------------------------------------------*/
2856
2857/* Utility methods for SPI master protocol drivers, layered on
2858 * top of the core. Some other utility methods are defined as
2859 * inline functions.
2860 */
2861
Andrew Morton5d870c82006-01-11 11:23:49 -08002862static void spi_complete(void *arg)
2863{
2864 complete(arg);
2865}
2866
Mark Brownef4d96e2016-07-21 23:53:31 +01002867static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002868{
2869 DECLARE_COMPLETION_ONSTACK(done);
2870 int status;
2871 struct spi_master *master = spi->master;
Mark Brown0461a412014-12-09 21:38:05 +00002872 unsigned long flags;
2873
2874 status = __spi_validate(spi, message);
2875 if (status != 0)
2876 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002877
2878 message->complete = spi_complete;
2879 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00002880 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002881
Martin Sperleca2ebc2015-06-22 13:00:36 +00002882 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync);
2883 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
2884
Mark Brown0461a412014-12-09 21:38:05 +00002885 /* If we're not using the legacy transfer method then we will
2886 * try to transfer in the calling context so special case.
2887 * This code would be less tricky if we could remove the
2888 * support for driver implemented message queues.
2889 */
2890 if (master->transfer == spi_queued_transfer) {
2891 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2892
2893 trace_spi_message_submit(message);
2894
2895 status = __spi_queued_transfer(spi, message, false);
2896
2897 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
2898 } else {
2899 status = spi_async_locked(spi, message);
2900 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002901
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002902 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00002903 /* Push out the messages in the calling context if we
2904 * can.
2905 */
Martin Sperleca2ebc2015-06-22 13:00:36 +00002906 if (master->transfer == spi_queued_transfer) {
2907 SPI_STATISTICS_INCREMENT_FIELD(&master->statistics,
2908 spi_sync_immediate);
2909 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
2910 spi_sync_immediate);
Mark Brownef4d96e2016-07-21 23:53:31 +01002911 __spi_pump_messages(master, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00002912 }
Mark Brown0461a412014-12-09 21:38:05 +00002913
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002914 wait_for_completion(&done);
2915 status = message->status;
2916 }
2917 message->context = NULL;
2918 return status;
2919}
2920
David Brownell8ae12a02006-01-08 13:34:19 -08002921/**
2922 * spi_sync - blocking/synchronous SPI data transfers
2923 * @spi: device with which data will be exchanged
2924 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07002925 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002926 *
2927 * This call may only be used from a context that may sleep. The sleep
2928 * is non-interruptible, and has no timeout. Low-overhead controller
2929 * drivers may DMA directly into and out of the message buffers.
2930 *
2931 * Note that the SPI device's chip select is active during the message,
2932 * and then is normally disabled between messages. Drivers for some
2933 * frequently-used devices may want to minimize costs of selecting a chip,
2934 * by leaving it selected in anticipation that the next message will go
2935 * to the same chip. (That may increase power usage.)
2936 *
David Brownell0c868462006-01-08 13:34:25 -08002937 * Also, the caller is guaranteeing that the memory associated with the
2938 * message will not be freed before this call returns.
2939 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002940 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002941 */
2942int spi_sync(struct spi_device *spi, struct spi_message *message)
2943{
Mark Brownef4d96e2016-07-21 23:53:31 +01002944 int ret;
2945
2946 mutex_lock(&spi->master->bus_lock_mutex);
2947 ret = __spi_sync(spi, message);
2948 mutex_unlock(&spi->master->bus_lock_mutex);
2949
2950 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08002951}
2952EXPORT_SYMBOL_GPL(spi_sync);
2953
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002954/**
2955 * spi_sync_locked - version of spi_sync with exclusive bus usage
2956 * @spi: device with which data will be exchanged
2957 * @message: describes the data transfers
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. Low-overhead controller
2962 * drivers may DMA directly into and out of the message buffers.
2963 *
2964 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002965 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002966 * be released by a spi_bus_unlock call when the exclusive access is over.
2967 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002968 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002969 */
2970int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
2971{
Mark Brownef4d96e2016-07-21 23:53:31 +01002972 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002973}
2974EXPORT_SYMBOL_GPL(spi_sync_locked);
2975
2976/**
2977 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
2978 * @master: SPI bus master that should be locked for exclusive bus access
2979 * Context: can sleep
2980 *
2981 * This call may only be used from a context that may sleep. The sleep
2982 * is non-interruptible, and has no timeout.
2983 *
2984 * This call should be used by drivers that require exclusive access to the
2985 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
2986 * exclusive access is over. Data transfer must be done by spi_sync_locked
2987 * and spi_async_locked calls when the SPI bus lock is held.
2988 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002989 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002990 */
2991int spi_bus_lock(struct spi_master *master)
2992{
2993 unsigned long flags;
2994
2995 mutex_lock(&master->bus_lock_mutex);
2996
2997 spin_lock_irqsave(&master->bus_lock_spinlock, flags);
2998 master->bus_lock_flag = 1;
2999 spin_unlock_irqrestore(&master->bus_lock_spinlock, flags);
3000
3001 /* mutex remains locked until spi_bus_unlock is called */
3002
3003 return 0;
3004}
3005EXPORT_SYMBOL_GPL(spi_bus_lock);
3006
3007/**
3008 * spi_bus_unlock - release the lock for exclusive SPI bus usage
3009 * @master: SPI bus master that was locked for exclusive bus access
3010 * Context: can sleep
3011 *
3012 * This call may only be used from a context that may sleep. The sleep
3013 * is non-interruptible, and has no timeout.
3014 *
3015 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3016 * call.
3017 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003018 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003019 */
3020int spi_bus_unlock(struct spi_master *master)
3021{
3022 master->bus_lock_flag = 0;
3023
3024 mutex_unlock(&master->bus_lock_mutex);
3025
3026 return 0;
3027}
3028EXPORT_SYMBOL_GPL(spi_bus_unlock);
3029
David Brownella9948b62006-04-02 10:37:40 -08003030/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003031#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003032
3033static u8 *buf;
3034
3035/**
3036 * spi_write_then_read - SPI synchronous write followed by read
3037 * @spi: device with which data will be exchanged
3038 * @txbuf: data to be written (need not be dma-safe)
3039 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003040 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3041 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003042 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003043 *
3044 * This performs a half duplex MicroWire style transaction with the
3045 * device, sending txbuf and then reading rxbuf. The return value
3046 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003047 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003048 *
David Brownell0c868462006-01-08 13:34:25 -08003049 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003050 * portable code should never use this for more than 32 bytes.
3051 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c868462006-01-08 13:34:25 -08003052 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003053 *
3054 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003055 */
3056int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003057 const void *txbuf, unsigned n_tx,
3058 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003059{
David Brownell068f4072007-12-04 23:45:09 -08003060 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003061
3062 int status;
3063 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003064 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003065 u8 *local_buf;
3066
Mark Brownb3a223e2012-12-02 12:54:25 +09003067 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3068 * copying here, (as a pure convenience thing), but we can
3069 * keep heap costs out of the hot path unless someone else is
3070 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003071 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003072 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003073 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3074 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003075 if (!local_buf)
3076 return -ENOMEM;
3077 } else {
3078 local_buf = buf;
3079 }
David Brownell8ae12a02006-01-08 13:34:19 -08003080
Vitaly Wool8275c642006-01-08 13:34:28 -08003081 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003082 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003083 if (n_tx) {
3084 x[0].len = n_tx;
3085 spi_message_add_tail(&x[0], &message);
3086 }
3087 if (n_rx) {
3088 x[1].len = n_rx;
3089 spi_message_add_tail(&x[1], &message);
3090 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003091
David Brownell8ae12a02006-01-08 13:34:19 -08003092 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003093 x[0].tx_buf = local_buf;
3094 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003095
3096 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003097 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003098 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003099 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003100
David Brownellbdff5492009-04-13 14:39:57 -07003101 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003102 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003103 else
3104 kfree(local_buf);
3105
3106 return status;
3107}
3108EXPORT_SYMBOL_GPL(spi_write_then_read);
3109
3110/*-------------------------------------------------------------------------*/
3111
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003112#if IS_ENABLED(CONFIG_OF_DYNAMIC)
3113static int __spi_of_device_match(struct device *dev, void *data)
3114{
3115 return dev->of_node == data;
3116}
3117
3118/* must call put_device() when done with returned spi_device device */
3119static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
3120{
3121 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3122 __spi_of_device_match);
3123 return dev ? to_spi_device(dev) : NULL;
3124}
3125
3126static int __spi_of_master_match(struct device *dev, const void *data)
3127{
3128 return dev->of_node == data;
3129}
3130
3131/* the spi masters are not using spi_bus, so we find it with another way */
3132static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
3133{
3134 struct device *dev;
3135
3136 dev = class_find_device(&spi_master_class, NULL, node,
3137 __spi_of_master_match);
3138 if (!dev)
3139 return NULL;
3140
3141 /* reference got in class_find_device */
3142 return container_of(dev, struct spi_master, dev);
3143}
3144
3145static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3146 void *arg)
3147{
3148 struct of_reconfig_data *rd = arg;
3149 struct spi_master *master;
3150 struct spi_device *spi;
3151
3152 switch (of_reconfig_get_state_change(action, arg)) {
3153 case OF_RECONFIG_CHANGE_ADD:
3154 master = of_find_spi_master_by_node(rd->dn->parent);
3155 if (master == NULL)
3156 return NOTIFY_OK; /* not for us */
3157
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003158 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
3159 put_device(&master->dev);
3160 return NOTIFY_OK;
3161 }
3162
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003163 spi = of_register_spi_device(master, rd->dn);
3164 put_device(&master->dev);
3165
3166 if (IS_ERR(spi)) {
3167 pr_err("%s: failed to create for '%s'\n",
3168 __func__, rd->dn->full_name);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003169 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003170 return notifier_from_errno(PTR_ERR(spi));
3171 }
3172 break;
3173
3174 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003175 /* already depopulated? */
3176 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3177 return NOTIFY_OK;
3178
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003179 /* find our device by node */
3180 spi = of_find_spi_device_by_node(rd->dn);
3181 if (spi == NULL)
3182 return NOTIFY_OK; /* no? not meant for us */
3183
3184 /* unregister takes one ref away */
3185 spi_unregister_device(spi);
3186
3187 /* and put the reference of the find */
3188 put_device(&spi->dev);
3189 break;
3190 }
3191
3192 return NOTIFY_OK;
3193}
3194
3195static struct notifier_block spi_of_notifier = {
3196 .notifier_call = of_spi_notify,
3197};
3198#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3199extern struct notifier_block spi_of_notifier;
3200#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3201
Octavian Purdila7f244672016-07-08 19:13:11 +03003202#if IS_ENABLED(CONFIG_ACPI)
3203static int spi_acpi_master_match(struct device *dev, const void *data)
3204{
3205 return ACPI_COMPANION(dev->parent) == data;
3206}
3207
3208static int spi_acpi_device_match(struct device *dev, void *data)
3209{
3210 return ACPI_COMPANION(dev) == data;
3211}
3212
3213static struct spi_master *acpi_spi_find_master_by_adev(struct acpi_device *adev)
3214{
3215 struct device *dev;
3216
3217 dev = class_find_device(&spi_master_class, NULL, adev,
3218 spi_acpi_master_match);
3219 if (!dev)
3220 return NULL;
3221
3222 return container_of(dev, struct spi_master, dev);
3223}
3224
3225static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3226{
3227 struct device *dev;
3228
3229 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3230
3231 return dev ? to_spi_device(dev) : NULL;
3232}
3233
3234static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3235 void *arg)
3236{
3237 struct acpi_device *adev = arg;
3238 struct spi_master *master;
3239 struct spi_device *spi;
3240
3241 switch (value) {
3242 case ACPI_RECONFIG_DEVICE_ADD:
3243 master = acpi_spi_find_master_by_adev(adev->parent);
3244 if (!master)
3245 break;
3246
3247 acpi_register_spi_device(master, adev);
3248 put_device(&master->dev);
3249 break;
3250 case ACPI_RECONFIG_DEVICE_REMOVE:
3251 if (!acpi_device_enumerated(adev))
3252 break;
3253
3254 spi = acpi_spi_find_device_by_adev(adev);
3255 if (!spi)
3256 break;
3257
3258 spi_unregister_device(spi);
3259 put_device(&spi->dev);
3260 break;
3261 }
3262
3263 return NOTIFY_OK;
3264}
3265
3266static struct notifier_block spi_acpi_notifier = {
3267 .notifier_call = acpi_spi_notify,
3268};
3269#else
3270extern struct notifier_block spi_acpi_notifier;
3271#endif
3272
David Brownell8ae12a02006-01-08 13:34:19 -08003273static int __init spi_init(void)
3274{
David Brownellb8852442006-01-08 13:34:23 -08003275 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08003276
Christoph Lametere94b1762006-12-06 20:33:17 -08003277 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08003278 if (!buf) {
3279 status = -ENOMEM;
3280 goto err0;
3281 }
3282
3283 status = bus_register(&spi_bus_type);
3284 if (status < 0)
3285 goto err1;
3286
3287 status = class_register(&spi_master_class);
3288 if (status < 0)
3289 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003290
Fabio Estevam52677202014-11-26 20:13:57 -02003291 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003292 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03003293 if (IS_ENABLED(CONFIG_ACPI))
3294 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003295
David Brownell8ae12a02006-01-08 13:34:19 -08003296 return 0;
David Brownellb8852442006-01-08 13:34:23 -08003297
3298err2:
3299 bus_unregister(&spi_bus_type);
3300err1:
3301 kfree(buf);
3302 buf = NULL;
3303err0:
3304 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08003305}
David Brownellb8852442006-01-08 13:34:23 -08003306
David Brownell8ae12a02006-01-08 13:34:19 -08003307/* board_info is normally registered in arch_initcall(),
3308 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08003309 *
3310 * REVISIT only boardinfo really needs static linking. the rest (device and
3311 * driver registration) _could_ be dynamically linked (modular) ... costs
3312 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08003313 */
David Brownell673c0c02008-10-15 22:02:46 -07003314postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08003315