blob: aed78093b1306f0f21c77377c0a09b58bdc215ac [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>
Dmitry Torokhov826cf172017-02-28 14:25:18 -080034#include <linux/property.h>
Paul Gortmaker025ed132011-07-10 12:57:55 -040035#include <linux/export.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060036#include <linux/sched/rt.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010037#include <uapi/linux/sched/types.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010038#include <linux/delay.h>
39#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010040#include <linux/ioport.h>
41#include <linux/acpi.h>
Vignesh Rb1b81532016-08-17 15:22:36 +053042#include <linux/highmem.h>
David Brownell8ae12a02006-01-08 13:34:19 -080043
Mark Brown56ec1972013-10-07 19:33:53 +010044#define CREATE_TRACE_POINTS
45#include <trace/events/spi.h>
46
David Brownell8ae12a02006-01-08 13:34:19 -080047static void spidev_release(struct device *dev)
48{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080049 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080050
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020051 /* spi controllers may cleanup for released devices */
52 if (spi->controller->cleanup)
53 spi->controller->cleanup(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080054
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020055 spi_controller_put(spi->controller);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000056 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080057}
58
59static ssize_t
60modalias_show(struct device *dev, struct device_attribute *a, char *buf)
61{
62 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080063 int len;
64
65 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
66 if (len != -ENODEV)
67 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080068
Grant Likelyd8e328b2012-05-20 00:08:13 -060069 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080070}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070071static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080072
Martin Sperleca2ebc2015-06-22 13:00:36 +000073#define SPI_STATISTICS_ATTRS(field, file) \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020074static ssize_t spi_controller_##field##_show(struct device *dev, \
75 struct device_attribute *attr, \
76 char *buf) \
Martin Sperleca2ebc2015-06-22 13:00:36 +000077{ \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020078 struct spi_controller *ctlr = container_of(dev, \
79 struct spi_controller, dev); \
80 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
Martin Sperleca2ebc2015-06-22 13:00:36 +000081} \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020082static struct device_attribute dev_attr_spi_controller_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +020083 .attr = { .name = file, .mode = 0444 }, \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020084 .show = spi_controller_##field##_show, \
Martin Sperleca2ebc2015-06-22 13:00:36 +000085}; \
86static ssize_t spi_device_##field##_show(struct device *dev, \
87 struct device_attribute *attr, \
88 char *buf) \
89{ \
Geliang Tangd1eba932015-12-23 00:18:41 +080090 struct spi_device *spi = to_spi_device(dev); \
Martin Sperleca2ebc2015-06-22 13:00:36 +000091 return spi_statistics_##field##_show(&spi->statistics, buf); \
92} \
93static struct device_attribute dev_attr_spi_device_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +020094 .attr = { .name = file, .mode = 0444 }, \
Martin Sperleca2ebc2015-06-22 13:00:36 +000095 .show = spi_device_##field##_show, \
96}
97
98#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
99static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
100 char *buf) \
101{ \
102 unsigned long flags; \
103 ssize_t len; \
104 spin_lock_irqsave(&stat->lock, flags); \
105 len = sprintf(buf, format_string, stat->field); \
106 spin_unlock_irqrestore(&stat->lock, flags); \
107 return len; \
108} \
109SPI_STATISTICS_ATTRS(name, file)
110
111#define SPI_STATISTICS_SHOW(field, format_string) \
112 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
113 field, format_string)
114
115SPI_STATISTICS_SHOW(messages, "%lu");
116SPI_STATISTICS_SHOW(transfers, "%lu");
117SPI_STATISTICS_SHOW(errors, "%lu");
118SPI_STATISTICS_SHOW(timedout, "%lu");
119
120SPI_STATISTICS_SHOW(spi_sync, "%lu");
121SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
122SPI_STATISTICS_SHOW(spi_async, "%lu");
123
124SPI_STATISTICS_SHOW(bytes, "%llu");
125SPI_STATISTICS_SHOW(bytes_rx, "%llu");
126SPI_STATISTICS_SHOW(bytes_tx, "%llu");
127
Martin Sperl6b7bc062015-06-22 13:02:04 +0000128#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
129 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
130 "transfer_bytes_histo_" number, \
131 transfer_bytes_histo[index], "%lu")
132SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
133SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
134SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
135SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
136SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
137SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
138SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
139SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
140SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
141SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
142SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
143SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
144SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
145SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
146SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
147SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
148SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
149
Martin Sperld9f12122015-12-14 15:20:20 +0000150SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
151
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700152static struct attribute *spi_dev_attrs[] = {
153 &dev_attr_modalias.attr,
154 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800155};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000156
157static const struct attribute_group spi_dev_group = {
158 .attrs = spi_dev_attrs,
159};
160
161static struct attribute *spi_device_statistics_attrs[] = {
162 &dev_attr_spi_device_messages.attr,
163 &dev_attr_spi_device_transfers.attr,
164 &dev_attr_spi_device_errors.attr,
165 &dev_attr_spi_device_timedout.attr,
166 &dev_attr_spi_device_spi_sync.attr,
167 &dev_attr_spi_device_spi_sync_immediate.attr,
168 &dev_attr_spi_device_spi_async.attr,
169 &dev_attr_spi_device_bytes.attr,
170 &dev_attr_spi_device_bytes_rx.attr,
171 &dev_attr_spi_device_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000172 &dev_attr_spi_device_transfer_bytes_histo0.attr,
173 &dev_attr_spi_device_transfer_bytes_histo1.attr,
174 &dev_attr_spi_device_transfer_bytes_histo2.attr,
175 &dev_attr_spi_device_transfer_bytes_histo3.attr,
176 &dev_attr_spi_device_transfer_bytes_histo4.attr,
177 &dev_attr_spi_device_transfer_bytes_histo5.attr,
178 &dev_attr_spi_device_transfer_bytes_histo6.attr,
179 &dev_attr_spi_device_transfer_bytes_histo7.attr,
180 &dev_attr_spi_device_transfer_bytes_histo8.attr,
181 &dev_attr_spi_device_transfer_bytes_histo9.attr,
182 &dev_attr_spi_device_transfer_bytes_histo10.attr,
183 &dev_attr_spi_device_transfer_bytes_histo11.attr,
184 &dev_attr_spi_device_transfer_bytes_histo12.attr,
185 &dev_attr_spi_device_transfer_bytes_histo13.attr,
186 &dev_attr_spi_device_transfer_bytes_histo14.attr,
187 &dev_attr_spi_device_transfer_bytes_histo15.attr,
188 &dev_attr_spi_device_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000189 &dev_attr_spi_device_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000190 NULL,
191};
192
193static const struct attribute_group spi_device_statistics_group = {
194 .name = "statistics",
195 .attrs = spi_device_statistics_attrs,
196};
197
198static const struct attribute_group *spi_dev_groups[] = {
199 &spi_dev_group,
200 &spi_device_statistics_group,
201 NULL,
202};
203
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200204static struct attribute *spi_controller_statistics_attrs[] = {
205 &dev_attr_spi_controller_messages.attr,
206 &dev_attr_spi_controller_transfers.attr,
207 &dev_attr_spi_controller_errors.attr,
208 &dev_attr_spi_controller_timedout.attr,
209 &dev_attr_spi_controller_spi_sync.attr,
210 &dev_attr_spi_controller_spi_sync_immediate.attr,
211 &dev_attr_spi_controller_spi_async.attr,
212 &dev_attr_spi_controller_bytes.attr,
213 &dev_attr_spi_controller_bytes_rx.attr,
214 &dev_attr_spi_controller_bytes_tx.attr,
215 &dev_attr_spi_controller_transfer_bytes_histo0.attr,
216 &dev_attr_spi_controller_transfer_bytes_histo1.attr,
217 &dev_attr_spi_controller_transfer_bytes_histo2.attr,
218 &dev_attr_spi_controller_transfer_bytes_histo3.attr,
219 &dev_attr_spi_controller_transfer_bytes_histo4.attr,
220 &dev_attr_spi_controller_transfer_bytes_histo5.attr,
221 &dev_attr_spi_controller_transfer_bytes_histo6.attr,
222 &dev_attr_spi_controller_transfer_bytes_histo7.attr,
223 &dev_attr_spi_controller_transfer_bytes_histo8.attr,
224 &dev_attr_spi_controller_transfer_bytes_histo9.attr,
225 &dev_attr_spi_controller_transfer_bytes_histo10.attr,
226 &dev_attr_spi_controller_transfer_bytes_histo11.attr,
227 &dev_attr_spi_controller_transfer_bytes_histo12.attr,
228 &dev_attr_spi_controller_transfer_bytes_histo13.attr,
229 &dev_attr_spi_controller_transfer_bytes_histo14.attr,
230 &dev_attr_spi_controller_transfer_bytes_histo15.attr,
231 &dev_attr_spi_controller_transfer_bytes_histo16.attr,
232 &dev_attr_spi_controller_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000233 NULL,
234};
235
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200236static const struct attribute_group spi_controller_statistics_group = {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000237 .name = "statistics",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200238 .attrs = spi_controller_statistics_attrs,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000239};
240
241static const struct attribute_group *spi_master_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200242 &spi_controller_statistics_group,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000243 NULL,
244};
245
246void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
247 struct spi_transfer *xfer,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200248 struct spi_controller *ctlr)
Martin Sperleca2ebc2015-06-22 13:00:36 +0000249{
250 unsigned long flags;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000251 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
252
253 if (l2len < 0)
254 l2len = 0;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000255
256 spin_lock_irqsave(&stats->lock, flags);
257
258 stats->transfers++;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000259 stats->transfer_bytes_histo[l2len]++;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000260
261 stats->bytes += xfer->len;
262 if ((xfer->tx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200263 (xfer->tx_buf != ctlr->dummy_tx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000264 stats->bytes_tx += xfer->len;
265 if ((xfer->rx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200266 (xfer->rx_buf != ctlr->dummy_rx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000267 stats->bytes_rx += xfer->len;
268
269 spin_unlock_irqrestore(&stats->lock, flags);
270}
271EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800272
273/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
274 * and the sysfs version makes coldplug work too.
275 */
276
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700277static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
278 const struct spi_device *sdev)
279{
280 while (id->name[0]) {
281 if (!strcmp(sdev->modalias, id->name))
282 return id;
283 id++;
284 }
285 return NULL;
286}
287
288const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
289{
290 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
291
292 return spi_match_id(sdrv->id_table, sdev);
293}
294EXPORT_SYMBOL_GPL(spi_get_device_id);
295
David Brownell8ae12a02006-01-08 13:34:19 -0800296static int spi_match_device(struct device *dev, struct device_driver *drv)
297{
298 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700299 const struct spi_driver *sdrv = to_spi_driver(drv);
300
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600301 /* Attempt an OF style match */
302 if (of_driver_match_device(dev, drv))
303 return 1;
304
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100305 /* Then try ACPI */
306 if (acpi_driver_match_device(dev, drv))
307 return 1;
308
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700309 if (sdrv->id_table)
310 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800311
Kay Sievers35f74fc2009-01-06 10:44:37 -0800312 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800313}
314
Kay Sievers7eff2e72007-08-14 15:15:12 +0200315static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800316{
317 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800318 int rc;
319
320 rc = acpi_device_uevent_modalias(dev, env);
321 if (rc != -ENODEV)
322 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800323
Andy Shevchenko28566702017-07-26 16:14:00 +0300324 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800325}
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
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200407 * would make them board-specific. Similarly with SPI controller drivers.
David Brownell8ae12a02006-01-08 13:34:19 -0800408 * 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);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200418static LIST_HEAD(spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800419
420/*
421 * Used to protect add/del opertion for board_info list and
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200422 * spi_controller list, and their matching process
Feng Tang2b9603a2010-08-02 15:52:15 +0800423 */
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
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200428 * @ctlr: Controller to which device is connected
Grant Likelydc87c982008-05-15 16:50:22 -0600429 * 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
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200437 * spi_device structure to add it to the SPI controller. If the caller
Grant Likelydc87c982008-05-15 16:50:22 -0600438 * 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 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200443struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
Grant Likelydc87c982008-05-15 16:50:22 -0600444{
445 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600446
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200447 if (!spi_controller_get(ctlr))
Grant Likelydc87c982008-05-15 16:50:22 -0600448 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) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200452 spi_controller_put(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600453 return NULL;
454 }
455
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200456 spi->master = spi->controller = ctlr;
457 spi->dev.parent = &ctlr->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
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200478 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200479 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
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200487 if (spi->controller == new_spi->controller &&
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200488 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);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200505 struct spi_controller *ctlr = spi->controller;
506 struct device *dev = ctlr->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600507 int status;
508
509 /* Chipselects are numbered 0..max; validate. */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200510 if (spi->chip_select >= ctlr->num_chipselect) {
511 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
512 ctlr->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600513 return -EINVAL;
514 }
515
516 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200517 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700518
519 /* We need to make sure there's no other device with this
520 * chipselect **BEFORE** we call setup(), else we'll trash
521 * its configuration. Lock against concurrent add() calls.
522 */
523 mutex_lock(&spi_add_lock);
524
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200525 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
526 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700527 dev_err(dev, "chipselect %d already in use\n",
528 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700529 goto done;
530 }
531
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200532 if (ctlr->cs_gpios)
533 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100534
David Brownelle48880e2008-08-15 00:40:44 -0700535 /* Drivers may modify this initial i/o setup, but will
536 * normally rely on the device being setup. Devices
537 * using SPI_CS_HIGH can't coexist well otherwise...
538 */
David Brownell7d077192009-06-17 16:26:03 -0700539 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600540 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200541 dev_err(dev, "can't setup %s, status %d\n",
542 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700543 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600544 }
545
David Brownelle48880e2008-08-15 00:40:44 -0700546 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600547 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700548 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200549 dev_err(dev, "can't add %s, status %d\n",
550 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700551 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800552 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600553
David Brownelle48880e2008-08-15 00:40:44 -0700554done:
555 mutex_unlock(&spi_add_lock);
556 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600557}
558EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800559
David Brownell33e34dc2007-05-08 00:32:21 -0700560/**
561 * spi_new_device - instantiate one new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200562 * @ctlr: Controller to which device is connected
David Brownell33e34dc2007-05-08 00:32:21 -0700563 * @chip: Describes the SPI device
564 * Context: can sleep
565 *
566 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800567 * after board init creates the hard-wired devices. Some development
568 * platforms may not be able to use spi_register_board_info though, and
569 * this is exported so that for example a USB or parport based adapter
570 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700571 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200572 * Return: the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800573 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200574struct spi_device *spi_new_device(struct spi_controller *ctlr,
Adrian Bunke9d5a462007-03-26 21:32:23 -0800575 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800576{
577 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800578 int status;
579
David Brownell082c8cb2007-07-31 00:39:45 -0700580 /* NOTE: caller did any chip->bus_num checks necessary.
581 *
582 * Also, unless we change the return value convention to use
583 * error-or-pointer (not NULL-or-pointer), troubleshootability
584 * suggests syslogged diagnostics are best here (ugh).
585 */
586
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200587 proxy = spi_alloc_device(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600588 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800589 return NULL;
590
Grant Likely102eb972008-07-23 21:29:55 -0700591 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
592
David Brownell8ae12a02006-01-08 13:34:19 -0800593 proxy->chip_select = chip->chip_select;
594 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700595 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800596 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700597 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800598 proxy->dev.platform_data = (void *) chip->platform_data;
599 proxy->controller_data = chip->controller_data;
600 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800601
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800602 if (chip->properties) {
603 status = device_add_properties(&proxy->dev, chip->properties);
604 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200605 dev_err(&ctlr->dev,
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800606 "failed to add properties to '%s': %d\n",
607 chip->modalias, status);
608 goto err_dev_put;
609 }
David Brownell8ae12a02006-01-08 13:34:19 -0800610 }
611
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800612 status = spi_add_device(proxy);
613 if (status < 0)
614 goto err_remove_props;
615
David Brownell8ae12a02006-01-08 13:34:19 -0800616 return proxy;
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800617
618err_remove_props:
619 if (chip->properties)
620 device_remove_properties(&proxy->dev);
621err_dev_put:
622 spi_dev_put(proxy);
623 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800624}
625EXPORT_SYMBOL_GPL(spi_new_device);
626
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100627/**
628 * spi_unregister_device - unregister a single SPI device
629 * @spi: spi_device to unregister
630 *
631 * Start making the passed SPI device vanish. Normally this would be handled
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200632 * by spi_unregister_controller().
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100633 */
634void spi_unregister_device(struct spi_device *spi)
635{
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100636 if (!spi)
637 return;
638
Johan Hovold83241472017-01-30 17:47:05 +0100639 if (spi->dev.of_node) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100640 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
Johan Hovold83241472017-01-30 17:47:05 +0100641 of_node_put(spi->dev.of_node);
642 }
Octavian Purdila7f244672016-07-08 19:13:11 +0300643 if (ACPI_COMPANION(&spi->dev))
644 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100645 device_unregister(&spi->dev);
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100646}
647EXPORT_SYMBOL_GPL(spi_unregister_device);
648
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200649static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
650 struct spi_board_info *bi)
Feng Tang2b9603a2010-08-02 15:52:15 +0800651{
652 struct spi_device *dev;
653
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200654 if (ctlr->bus_num != bi->bus_num)
Feng Tang2b9603a2010-08-02 15:52:15 +0800655 return;
656
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200657 dev = spi_new_device(ctlr, bi);
Feng Tang2b9603a2010-08-02 15:52:15 +0800658 if (!dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200659 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
Feng Tang2b9603a2010-08-02 15:52:15 +0800660 bi->modalias);
661}
662
David Brownell33e34dc2007-05-08 00:32:21 -0700663/**
664 * spi_register_board_info - register SPI devices for a given board
665 * @info: array of chip descriptors
666 * @n: how many descriptors are provided
667 * Context: can sleep
668 *
David Brownell8ae12a02006-01-08 13:34:19 -0800669 * Board-specific early init code calls this (probably during arch_initcall)
670 * with segments of the SPI device table. Any device nodes are created later,
671 * after the relevant parent SPI controller (bus_num) is defined. We keep
672 * this table of devices forever, so that reloading a controller driver will
673 * not make Linux forget about these hard-wired devices.
674 *
675 * Other code can also call this, e.g. a particular add-on board might provide
676 * SPI devices through its expansion connector, so code initializing that board
677 * would naturally declare its SPI devices.
678 *
679 * The board info passed can safely be __initdata ... but be careful of
680 * any embedded pointers (platform_data, etc), they're copied as-is.
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800681 * Device properties are deep-copied though.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200682 *
683 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -0800684 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000685int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800686{
Feng Tang2b9603a2010-08-02 15:52:15 +0800687 struct boardinfo *bi;
688 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800689
Xiubo Lic7908a32014-09-24 14:30:29 +0800690 if (!n)
Dmitry Torokhovf974cf52017-02-28 14:25:19 -0800691 return 0;
Xiubo Lic7908a32014-09-24 14:30:29 +0800692
Markus Elfringf9bdb7f2017-01-13 12:28:04 +0100693 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800694 if (!bi)
695 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800696
Feng Tang2b9603a2010-08-02 15:52:15 +0800697 for (i = 0; i < n; i++, bi++, info++) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200698 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -0800699
Feng Tang2b9603a2010-08-02 15:52:15 +0800700 memcpy(&bi->board_info, info, sizeof(*info));
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800701 if (info->properties) {
702 bi->board_info.properties =
703 property_entries_dup(info->properties);
704 if (IS_ERR(bi->board_info.properties))
705 return PTR_ERR(bi->board_info.properties);
706 }
707
Feng Tang2b9603a2010-08-02 15:52:15 +0800708 mutex_lock(&board_lock);
709 list_add_tail(&bi->list, &board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200710 list_for_each_entry(ctlr, &spi_controller_list, list)
711 spi_match_controller_to_boardinfo(ctlr,
712 &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +0800713 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800714 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800715
716 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800717}
718
719/*-------------------------------------------------------------------------*/
720
Mark Brownb1589352013-10-05 11:50:40 +0100721static void spi_set_cs(struct spi_device *spi, bool enable)
722{
723 if (spi->mode & SPI_CS_HIGH)
724 enable = !enable;
725
Thor Thayer8eee6b92016-10-10 09:25:24 -0500726 if (gpio_is_valid(spi->cs_gpio)) {
Mark Brownb1589352013-10-05 11:50:40 +0100727 gpio_set_value(spi->cs_gpio, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500728 /* Some SPI masters need both GPIO CS & slave_select */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200729 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
730 spi->controller->set_cs)
731 spi->controller->set_cs(spi, !enable);
732 } else if (spi->controller->set_cs) {
733 spi->controller->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500734 }
Mark Brownb1589352013-10-05 11:50:40 +0100735}
736
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200737#ifdef CONFIG_HAS_DMA
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200738static int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
Mark Brown6ad45a22014-02-02 13:47:47 +0000739 struct sg_table *sgt, void *buf, size_t len,
740 enum dma_data_direction dir)
741{
742 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200743 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530744#ifdef CONFIG_HIGHMEM
745 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
746 (unsigned long)buf < (PKMAP_BASE +
747 (LAST_PKMAP * PAGE_SIZE)));
748#else
749 const bool kmap_buf = false;
750#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500751 int desc_len;
752 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000753 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600754 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000755 void *sg_buf;
756 size_t min;
757 int i, ret;
758
Vignesh Rb1b81532016-08-17 15:22:36 +0530759 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200760 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500761 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530762 } else if (virt_addr_valid(buf)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200763 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500764 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530765 } else {
766 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500767 }
768
Mark Brown6ad45a22014-02-02 13:47:47 +0000769 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
770 if (ret != 0)
771 return ret;
772
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600773 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000774 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000775
Vignesh Rb1b81532016-08-17 15:22:36 +0530776 if (vmalloced_buf || kmap_buf) {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500777 min = min_t(size_t,
778 len, desc_len - offset_in_page(buf));
Vignesh Rb1b81532016-08-17 15:22:36 +0530779 if (vmalloced_buf)
780 vm_page = vmalloc_to_page(buf);
781 else
782 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000783 if (!vm_page) {
784 sg_free_table(sgt);
785 return -ENOMEM;
786 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600787 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000788 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000789 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500790 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000791 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600792 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000793 }
794
Mark Brown6ad45a22014-02-02 13:47:47 +0000795 buf += min;
796 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600797 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000798 }
799
800 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200801 if (!ret)
802 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000803 if (ret < 0) {
804 sg_free_table(sgt);
805 return ret;
806 }
807
808 sgt->nents = ret;
809
810 return 0;
811}
812
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200813static void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
Mark Brown6ad45a22014-02-02 13:47:47 +0000814 struct sg_table *sgt, enum dma_data_direction dir)
815{
816 if (sgt->orig_nents) {
817 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
818 sg_free_table(sgt);
819 }
820}
821
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200822static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000823{
Mark Brown99adef32014-01-16 12:22:43 +0000824 struct device *tx_dev, *rx_dev;
825 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000826 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000827
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200828 if (!ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000829 return 0;
830
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200831 if (ctlr->dma_tx)
832 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800833 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200834 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800835
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200836 if (ctlr->dma_rx)
837 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800838 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200839 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000840
841 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200842 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000843 continue;
844
845 if (xfer->tx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200846 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000847 (void *)xfer->tx_buf, xfer->len,
848 DMA_TO_DEVICE);
849 if (ret != 0)
850 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000851 }
852
853 if (xfer->rx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200854 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000855 xfer->rx_buf, xfer->len,
856 DMA_FROM_DEVICE);
857 if (ret != 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200858 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000859 DMA_TO_DEVICE);
860 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000861 }
862 }
863 }
864
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200865 ctlr->cur_msg_mapped = true;
Mark Brown99adef32014-01-16 12:22:43 +0000866
867 return 0;
868}
869
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200870static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000871{
872 struct spi_transfer *xfer;
873 struct device *tx_dev, *rx_dev;
874
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200875 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000876 return 0;
877
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200878 if (ctlr->dma_tx)
879 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800880 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200881 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800882
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200883 if (ctlr->dma_rx)
884 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800885 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200886 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000887
888 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200889 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000890 continue;
891
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200892 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
893 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000894 }
895
896 return 0;
897}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200898#else /* !CONFIG_HAS_DMA */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200899static inline int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
900 struct sg_table *sgt, void *buf, size_t len,
Vignesh Rf4502dd2016-06-08 12:18:31 +0530901 enum dma_data_direction dir)
902{
903 return -EINVAL;
904}
905
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200906static inline void spi_unmap_buf(struct spi_controller *ctlr,
Vignesh Rf4502dd2016-06-08 12:18:31 +0530907 struct device *dev, struct sg_table *sgt,
908 enum dma_data_direction dir)
909{
910}
911
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200912static inline int __spi_map_msg(struct spi_controller *ctlr,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200913 struct spi_message *msg)
914{
915 return 0;
916}
917
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200918static inline int __spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000919 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200920{
921 return 0;
922}
923#endif /* !CONFIG_HAS_DMA */
924
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200925static inline int spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000926 struct spi_message *msg)
927{
928 struct spi_transfer *xfer;
929
930 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
931 /*
932 * Restore the original value of tx_buf or rx_buf if they are
933 * NULL.
934 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200935 if (xfer->tx_buf == ctlr->dummy_tx)
Martin Sperl4b786452015-05-25 10:13:10 +0000936 xfer->tx_buf = NULL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200937 if (xfer->rx_buf == ctlr->dummy_rx)
Martin Sperl4b786452015-05-25 10:13:10 +0000938 xfer->rx_buf = NULL;
939 }
940
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200941 return __spi_unmap_msg(ctlr, msg);
Martin Sperl4b786452015-05-25 10:13:10 +0000942}
943
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200944static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200945{
946 struct spi_transfer *xfer;
947 void *tmp;
948 unsigned int max_tx, max_rx;
949
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200950 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200951 max_tx = 0;
952 max_rx = 0;
953
954 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200955 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200956 !xfer->tx_buf)
957 max_tx = max(xfer->len, max_tx);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200958 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200959 !xfer->rx_buf)
960 max_rx = max(xfer->len, max_rx);
961 }
962
963 if (max_tx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200964 tmp = krealloc(ctlr->dummy_tx, max_tx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200965 GFP_KERNEL | GFP_DMA);
966 if (!tmp)
967 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200968 ctlr->dummy_tx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200969 memset(tmp, 0, max_tx);
970 }
971
972 if (max_rx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200973 tmp = krealloc(ctlr->dummy_rx, max_rx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200974 GFP_KERNEL | GFP_DMA);
975 if (!tmp)
976 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200977 ctlr->dummy_rx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200978 }
979
980 if (max_tx || max_rx) {
981 list_for_each_entry(xfer, &msg->transfers,
982 transfer_list) {
983 if (!xfer->tx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200984 xfer->tx_buf = ctlr->dummy_tx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200985 if (!xfer->rx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200986 xfer->rx_buf = ctlr->dummy_rx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200987 }
988 }
989 }
990
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200991 return __spi_map_msg(ctlr, msg);
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200992}
Mark Brown99adef32014-01-16 12:22:43 +0000993
Mark Brownb1589352013-10-05 11:50:40 +0100994/*
995 * spi_transfer_one_message - Default implementation of transfer_one_message()
996 *
997 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -0700998 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +0100999 * standard handling of delays and chip select management.
1000 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001001static int spi_transfer_one_message(struct spi_controller *ctlr,
Mark Brownb1589352013-10-05 11:50:40 +01001002 struct spi_message *msg)
1003{
1004 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +01001005 bool keep_cs = false;
1006 int ret = 0;
Sien Wud0716dd2016-09-01 18:24:29 -05001007 unsigned long long ms = 1;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001008 struct spi_statistics *statm = &ctlr->statistics;
Martin Sperleca2ebc2015-06-22 13:00:36 +00001009 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +01001010
1011 spi_set_cs(msg->spi, true);
1012
Martin Sperleca2ebc2015-06-22 13:00:36 +00001013 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1014 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1015
Mark Brownb1589352013-10-05 11:50:40 +01001016 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1017 trace_spi_transfer_start(msg, xfer);
1018
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001019 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1020 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
Martin Sperleca2ebc2015-06-22 13:00:36 +00001021
Mark Brown38ec10f2014-08-16 16:27:41 +01001022 if (xfer->tx_buf || xfer->rx_buf) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001023 reinit_completion(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001024
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001025 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
Mark Brown38ec10f2014-08-16 16:27:41 +01001026 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001027 SPI_STATISTICS_INCREMENT_FIELD(statm,
1028 errors);
1029 SPI_STATISTICS_INCREMENT_FIELD(stats,
1030 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001031 dev_err(&msg->spi->dev,
1032 "SPI transfer failed: %d\n", ret);
1033 goto out;
1034 }
Mark Brownb1589352013-10-05 11:50:40 +01001035
Mark Brown38ec10f2014-08-16 16:27:41 +01001036 if (ret > 0) {
1037 ret = 0;
Sien Wud0716dd2016-09-01 18:24:29 -05001038 ms = 8LL * 1000LL * xfer->len;
1039 do_div(ms, xfer->speed_hz);
Hauke Mehrtens833bfad2017-04-17 01:38:05 +02001040 ms += ms + 200; /* some tolerance */
Mark Brown16a0ce42014-01-30 22:16:41 +00001041
Sien Wud0716dd2016-09-01 18:24:29 -05001042 if (ms > UINT_MAX)
1043 ms = UINT_MAX;
1044
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001045 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
Mark Brown38ec10f2014-08-16 16:27:41 +01001046 msecs_to_jiffies(ms));
1047 }
Mark Brown16a0ce42014-01-30 22:16:41 +00001048
Mark Brown38ec10f2014-08-16 16:27:41 +01001049 if (ms == 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001050 SPI_STATISTICS_INCREMENT_FIELD(statm,
1051 timedout);
1052 SPI_STATISTICS_INCREMENT_FIELD(stats,
1053 timedout);
Mark Brown38ec10f2014-08-16 16:27:41 +01001054 dev_err(&msg->spi->dev,
1055 "SPI transfer timed out\n");
1056 msg->status = -ETIMEDOUT;
1057 }
1058 } else {
1059 if (xfer->len)
1060 dev_err(&msg->spi->dev,
1061 "Bufferless transfer has length %u\n",
1062 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001063 }
Mark Brownb1589352013-10-05 11:50:40 +01001064
1065 trace_spi_transfer_stop(msg, xfer);
1066
1067 if (msg->status != -EINPROGRESS)
1068 goto out;
1069
Daniel Kurtz8244bd32016-10-07 18:55:47 +08001070 if (xfer->delay_usecs) {
1071 u16 us = xfer->delay_usecs;
1072
1073 if (us <= 10)
1074 udelay(us);
1075 else
1076 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1077 }
Mark Brownb1589352013-10-05 11:50:40 +01001078
1079 if (xfer->cs_change) {
1080 if (list_is_last(&xfer->transfer_list,
1081 &msg->transfers)) {
1082 keep_cs = true;
1083 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001084 spi_set_cs(msg->spi, false);
1085 udelay(10);
1086 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001087 }
1088 }
1089
1090 msg->actual_length += xfer->len;
1091 }
1092
1093out:
1094 if (ret != 0 || !keep_cs)
1095 spi_set_cs(msg->spi, false);
1096
1097 if (msg->status == -EINPROGRESS)
1098 msg->status = ret;
1099
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001100 if (msg->status && ctlr->handle_err)
1101 ctlr->handle_err(ctlr, msg);
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001102
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001103 spi_res_release(ctlr, msg);
Martin Sperld780c372015-12-14 15:20:18 +00001104
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001105 spi_finalize_current_message(ctlr);
Mark Brownb1589352013-10-05 11:50:40 +01001106
1107 return ret;
1108}
1109
1110/**
1111 * spi_finalize_current_transfer - report completion of a transfer
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001112 * @ctlr: the controller reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001113 *
1114 * Called by SPI drivers using the core transfer_one_message()
1115 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001116 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001117 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001118void spi_finalize_current_transfer(struct spi_controller *ctlr)
Mark Brownb1589352013-10-05 11:50:40 +01001119{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001120 complete(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001121}
1122EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1123
Linus Walleijffbbdd212012-02-22 10:05:38 +01001124/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001125 * __spi_pump_messages - function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001126 * @ctlr: controller to process queue for
Mark Brownfc9e0f72014-12-10 13:46:33 +00001127 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001128 *
1129 * This function checks if there is any spi message in the queue that
1130 * needs processing and if so call out to the driver to initialize hardware
1131 * and transfer each message.
1132 *
Mark Brown0461a412014-12-09 21:38:05 +00001133 * Note that it is called both from the kthread itself and also from
1134 * inside spi_sync(); the queue extraction handling at the top of the
1135 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001136 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001137static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001138{
Linus Walleijffbbdd212012-02-22 10:05:38 +01001139 unsigned long flags;
1140 bool was_busy = false;
1141 int ret;
1142
Mark Brown983aee52014-12-09 19:46:56 +00001143 /* Lock queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001144 spin_lock_irqsave(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001145
1146 /* Make sure we are not already running a message */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001147 if (ctlr->cur_msg) {
1148 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001149 return;
1150 }
1151
Mark Brown0461a412014-12-09 21:38:05 +00001152 /* If another context is idling the device then defer */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001153 if (ctlr->idling) {
1154 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1155 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001156 return;
1157 }
1158
Mark Brown983aee52014-12-09 19:46:56 +00001159 /* Check if the queue is idle */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001160 if (list_empty(&ctlr->queue) || !ctlr->running) {
1161 if (!ctlr->busy) {
1162 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -07001163 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001164 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001165
1166 /* Only do teardown in the thread */
1167 if (!in_kthread) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001168 kthread_queue_work(&ctlr->kworker,
1169 &ctlr->pump_messages);
1170 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001171 return;
1172 }
1173
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001174 ctlr->busy = false;
1175 ctlr->idling = true;
1176 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001177
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001178 kfree(ctlr->dummy_rx);
1179 ctlr->dummy_rx = NULL;
1180 kfree(ctlr->dummy_tx);
1181 ctlr->dummy_tx = NULL;
1182 if (ctlr->unprepare_transfer_hardware &&
1183 ctlr->unprepare_transfer_hardware(ctlr))
1184 dev_err(&ctlr->dev,
Bryan Freedb0b36b82013-03-13 11:17:40 -07001185 "failed to unprepare transfer hardware\n");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001186 if (ctlr->auto_runtime_pm) {
1187 pm_runtime_mark_last_busy(ctlr->dev.parent);
1188 pm_runtime_put_autosuspend(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001189 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001190 trace_spi_controller_idle(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001191
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001192 spin_lock_irqsave(&ctlr->queue_lock, flags);
1193 ctlr->idling = false;
1194 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001195 return;
1196 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001197
Linus Walleijffbbdd212012-02-22 10:05:38 +01001198 /* Extract head of queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001199 ctlr->cur_msg =
1200 list_first_entry(&ctlr->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001201
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001202 list_del_init(&ctlr->cur_msg->queue);
1203 if (ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001204 was_busy = true;
1205 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001206 ctlr->busy = true;
1207 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001208
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001209 mutex_lock(&ctlr->io_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001210
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001211 if (!was_busy && ctlr->auto_runtime_pm) {
1212 ret = pm_runtime_get_sync(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001213 if (ret < 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001214 dev_err(&ctlr->dev, "Failed to power device: %d\n",
Mark Brown49834de2013-07-28 14:47:02 +01001215 ret);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001216 mutex_unlock(&ctlr->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001217 return;
1218 }
1219 }
1220
Mark Brown56ec1972013-10-07 19:33:53 +01001221 if (!was_busy)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001222 trace_spi_controller_busy(ctlr);
Mark Brown56ec1972013-10-07 19:33:53 +01001223
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001224 if (!was_busy && ctlr->prepare_transfer_hardware) {
1225 ret = ctlr->prepare_transfer_hardware(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001226 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001227 dev_err(&ctlr->dev,
Linus Walleijffbbdd212012-02-22 10:05:38 +01001228 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001229
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001230 if (ctlr->auto_runtime_pm)
1231 pm_runtime_put(ctlr->dev.parent);
1232 mutex_unlock(&ctlr->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001233 return;
1234 }
1235 }
1236
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001237 trace_spi_message_start(ctlr->cur_msg);
Mark Brown56ec1972013-10-07 19:33:53 +01001238
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001239 if (ctlr->prepare_message) {
1240 ret = ctlr->prepare_message(ctlr, ctlr->cur_msg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001241 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001242 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1243 ret);
1244 ctlr->cur_msg->status = ret;
1245 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001246 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001247 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001248 ctlr->cur_msg_prepared = true;
Mark Brown2841a5f2013-10-05 00:23:12 +01001249 }
1250
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001251 ret = spi_map_msg(ctlr, ctlr->cur_msg);
Mark Brown99adef32014-01-16 12:22:43 +00001252 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001253 ctlr->cur_msg->status = ret;
1254 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001255 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001256 }
1257
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001258 ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001259 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001260 dev_err(&ctlr->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001261 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001262 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001263 }
Jon Hunter49023d22016-03-08 12:28:20 +00001264
1265out:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001266 mutex_unlock(&ctlr->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001267
1268 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001269 if (!ret)
1270 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001271}
1272
Mark Brownfc9e0f72014-12-10 13:46:33 +00001273/**
1274 * spi_pump_messages - kthread work function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001275 * @work: pointer to kthread work struct contained in the controller struct
Mark Brownfc9e0f72014-12-10 13:46:33 +00001276 */
1277static void spi_pump_messages(struct kthread_work *work)
1278{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001279 struct spi_controller *ctlr =
1280 container_of(work, struct spi_controller, pump_messages);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001281
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001282 __spi_pump_messages(ctlr, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001283}
1284
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001285static int spi_init_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001286{
1287 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1288
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001289 ctlr->running = false;
1290 ctlr->busy = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001291
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001292 kthread_init_worker(&ctlr->kworker);
1293 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1294 "%s", dev_name(&ctlr->dev));
1295 if (IS_ERR(ctlr->kworker_task)) {
1296 dev_err(&ctlr->dev, "failed to create message pump task\n");
1297 return PTR_ERR(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001298 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001299 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001300
1301 /*
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001302 * Controller config will indicate if this controller should run the
Linus Walleijffbbdd212012-02-22 10:05:38 +01001303 * message pump with high (realtime) priority to reduce the transfer
1304 * latency on the bus by minimising the delay between a transfer
1305 * request and the scheduling of the message pump thread. Without this
1306 * setting the message pump thread will remain at default priority.
1307 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001308 if (ctlr->rt) {
1309 dev_info(&ctlr->dev,
Linus Walleijffbbdd212012-02-22 10:05:38 +01001310 "will run message pump with realtime priority\n");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001311 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001312 }
1313
1314 return 0;
1315}
1316
1317/**
1318 * spi_get_next_queued_message() - called by driver to check for queued
1319 * messages
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001320 * @ctlr: the controller to check for queued messages
Linus Walleijffbbdd212012-02-22 10:05:38 +01001321 *
1322 * If there are more messages in the queue, the next message is returned from
1323 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001324 *
1325 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001326 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001327struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001328{
1329 struct spi_message *next;
1330 unsigned long flags;
1331
1332 /* get a pointer to the next message, if any */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001333 spin_lock_irqsave(&ctlr->queue_lock, flags);
1334 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
Axel Lin1cfd97f2014-01-02 15:16:40 +08001335 queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001336 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001337
1338 return next;
1339}
1340EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1341
1342/**
1343 * spi_finalize_current_message() - the current message is complete
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001344 * @ctlr: the controller to return the message to
Linus Walleijffbbdd212012-02-22 10:05:38 +01001345 *
1346 * Called by the driver to notify the core that the message in the front of the
1347 * queue is complete and can be removed from the queue.
1348 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001349void spi_finalize_current_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001350{
1351 struct spi_message *mesg;
1352 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001353 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001354
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001355 spin_lock_irqsave(&ctlr->queue_lock, flags);
1356 mesg = ctlr->cur_msg;
1357 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001358
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001359 spi_unmap_msg(ctlr, mesg);
Mark Brown99adef32014-01-16 12:22:43 +00001360
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001361 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1362 ret = ctlr->unprepare_message(ctlr, mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001363 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001364 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1365 ret);
Mark Brown2841a5f2013-10-05 00:23:12 +01001366 }
1367 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001368
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001369 spin_lock_irqsave(&ctlr->queue_lock, flags);
1370 ctlr->cur_msg = NULL;
1371 ctlr->cur_msg_prepared = false;
1372 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1373 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001374
1375 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001376
Linus Walleijffbbdd212012-02-22 10:05:38 +01001377 mesg->state = NULL;
1378 if (mesg->complete)
1379 mesg->complete(mesg->context);
1380}
1381EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1382
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001383static int spi_start_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001384{
1385 unsigned long flags;
1386
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001387 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001388
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001389 if (ctlr->running || ctlr->busy) {
1390 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001391 return -EBUSY;
1392 }
1393
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001394 ctlr->running = true;
1395 ctlr->cur_msg = NULL;
1396 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001397
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001398 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001399
1400 return 0;
1401}
1402
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001403static int spi_stop_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001404{
1405 unsigned long flags;
1406 unsigned limit = 500;
1407 int ret = 0;
1408
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001409 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001410
1411 /*
1412 * This is a bit lame, but is optimized for the common execution path.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001413 * A wait_queue on the ctlr->busy could be used, but then the common
Linus Walleijffbbdd212012-02-22 10:05:38 +01001414 * execution path (pump_messages) would be required to call wake_up or
1415 * friends on every SPI message. Do this instead.
1416 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001417 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1418 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001419 usleep_range(10000, 11000);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001420 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001421 }
1422
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001423 if (!list_empty(&ctlr->queue) || ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001424 ret = -EBUSY;
1425 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001426 ctlr->running = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001427
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001428 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001429
1430 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001431 dev_warn(&ctlr->dev, "could not stop message queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001432 return ret;
1433 }
1434 return ret;
1435}
1436
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001437static int spi_destroy_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001438{
1439 int ret;
1440
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001441 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001442
1443 /*
Petr Mladek39891442016-10-11 13:55:20 -07001444 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001445 * If the reason that stop_queue timed out is that the work will never
1446 * finish, then it does no good to call flush/stop thread, so
1447 * return anyway.
1448 */
1449 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001450 dev_err(&ctlr->dev, "problem destroying queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001451 return ret;
1452 }
1453
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001454 kthread_flush_worker(&ctlr->kworker);
1455 kthread_stop(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001456
1457 return 0;
1458}
1459
Mark Brown0461a412014-12-09 21:38:05 +00001460static int __spi_queued_transfer(struct spi_device *spi,
1461 struct spi_message *msg,
1462 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001463{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001464 struct spi_controller *ctlr = spi->controller;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001465 unsigned long flags;
1466
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001467 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001468
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001469 if (!ctlr->running) {
1470 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001471 return -ESHUTDOWN;
1472 }
1473 msg->actual_length = 0;
1474 msg->status = -EINPROGRESS;
1475
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001476 list_add_tail(&msg->queue, &ctlr->queue);
1477 if (!ctlr->busy && need_pump)
1478 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001479
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001480 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001481 return 0;
1482}
1483
Mark Brown0461a412014-12-09 21:38:05 +00001484/**
1485 * spi_queued_transfer - transfer function for queued transfers
1486 * @spi: spi device which is requesting transfer
1487 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001488 *
1489 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001490 */
1491static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1492{
1493 return __spi_queued_transfer(spi, msg, true);
1494}
1495
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001496static int spi_controller_initialize_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001497{
1498 int ret;
1499
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001500 ctlr->transfer = spi_queued_transfer;
1501 if (!ctlr->transfer_one_message)
1502 ctlr->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001503
1504 /* Initialize and start queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001505 ret = spi_init_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001506 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001507 dev_err(&ctlr->dev, "problem initializing queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001508 goto err_init_queue;
1509 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001510 ctlr->queued = true;
1511 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001512 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001513 dev_err(&ctlr->dev, "problem starting queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001514 goto err_start_queue;
1515 }
1516
1517 return 0;
1518
1519err_start_queue:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001520 spi_destroy_queue(ctlr);
Mark Brownc3676d52014-05-01 10:47:52 -07001521err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001522 return ret;
1523}
1524
1525/*-------------------------------------------------------------------------*/
1526
Andreas Larsson7cb94362012-12-04 15:09:38 +01001527#if defined(CONFIG_OF)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001528static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001529 struct device_node *nc)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001530{
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001531 u32 value;
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001532 int rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001533
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001534 /* Mode (clock phase/polarity/etc.) */
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001535 if (of_property_read_bool(nc, "spi-cpha"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001536 spi->mode |= SPI_CPHA;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001537 if (of_property_read_bool(nc, "spi-cpol"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001538 spi->mode |= SPI_CPOL;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001539 if (of_property_read_bool(nc, "spi-cs-high"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001540 spi->mode |= SPI_CS_HIGH;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001541 if (of_property_read_bool(nc, "spi-3wire"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001542 spi->mode |= SPI_3WIRE;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001543 if (of_property_read_bool(nc, "spi-lsb-first"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001544 spi->mode |= SPI_LSB_FIRST;
1545
1546 /* Device DUAL/QUAD mode */
1547 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1548 switch (value) {
1549 case 1:
1550 break;
1551 case 2:
1552 spi->mode |= SPI_TX_DUAL;
1553 break;
1554 case 4:
1555 spi->mode |= SPI_TX_QUAD;
1556 break;
1557 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001558 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001559 "spi-tx-bus-width %d not supported\n",
1560 value);
1561 break;
1562 }
1563 }
1564
1565 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1566 switch (value) {
1567 case 1:
1568 break;
1569 case 2:
1570 spi->mode |= SPI_RX_DUAL;
1571 break;
1572 case 4:
1573 spi->mode |= SPI_RX_QUAD;
1574 break;
1575 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001576 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001577 "spi-rx-bus-width %d not supported\n",
1578 value);
1579 break;
1580 }
1581 }
1582
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001583 if (spi_controller_is_slave(ctlr)) {
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001584 if (strcmp(nc->name, "slave")) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001585 dev_err(&ctlr->dev, "%s is not called 'slave'\n",
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001586 nc->full_name);
1587 return -EINVAL;
1588 }
1589 return 0;
1590 }
1591
1592 /* Device address */
1593 rc = of_property_read_u32(nc, "reg", &value);
1594 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001595 dev_err(&ctlr->dev, "%s has no valid 'reg' property (%d)\n",
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001596 nc->full_name, rc);
1597 return rc;
1598 }
1599 spi->chip_select = value;
1600
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001601 /* Device speed */
1602 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1603 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001604 dev_err(&ctlr->dev,
1605 "%s has no valid 'spi-max-frequency' property (%d)\n",
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001606 nc->full_name, rc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001607 return rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001608 }
1609 spi->max_speed_hz = value;
1610
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001611 return 0;
1612}
1613
1614static struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001615of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001616{
1617 struct spi_device *spi;
1618 int rc;
1619
1620 /* Alloc an spi_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001621 spi = spi_alloc_device(ctlr);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001622 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001623 dev_err(&ctlr->dev, "spi_device alloc error for %s\n",
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001624 nc->full_name);
1625 rc = -ENOMEM;
1626 goto err_out;
1627 }
1628
1629 /* Select device driver */
1630 rc = of_modalias_node(nc, spi->modalias,
1631 sizeof(spi->modalias));
1632 if (rc < 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001633 dev_err(&ctlr->dev, "cannot find modalias for %s\n",
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001634 nc->full_name);
1635 goto err_out;
1636 }
1637
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001638 rc = of_spi_parse_dt(ctlr, spi, nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001639 if (rc)
1640 goto err_out;
1641
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001642 /* Store a pointer to the node in the device structure */
1643 of_node_get(nc);
1644 spi->dev.of_node = nc;
1645
1646 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001647 rc = spi_add_device(spi);
1648 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001649 dev_err(&ctlr->dev, "spi_device register error %s\n",
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001650 nc->full_name);
Johan Hovold83241472017-01-30 17:47:05 +01001651 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001652 }
1653
1654 return spi;
1655
Johan Hovold83241472017-01-30 17:47:05 +01001656err_of_node_put:
1657 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001658err_out:
1659 spi_dev_put(spi);
1660 return ERR_PTR(rc);
1661}
1662
Grant Likelyd57a4282012-04-07 14:16:53 -06001663/**
1664 * of_register_spi_devices() - Register child devices onto the SPI bus
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001665 * @ctlr: Pointer to spi_controller device
Grant Likelyd57a4282012-04-07 14:16:53 -06001666 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001667 * Registers an spi_device for each child node of controller node which
1668 * represents a valid SPI slave.
Grant Likelyd57a4282012-04-07 14:16:53 -06001669 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001670static void of_register_spi_devices(struct spi_controller *ctlr)
Grant Likelyd57a4282012-04-07 14:16:53 -06001671{
1672 struct spi_device *spi;
1673 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001674
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001675 if (!ctlr->dev.of_node)
Grant Likelyd57a4282012-04-07 14:16:53 -06001676 return;
1677
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001678 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01001679 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1680 continue;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001681 spi = of_register_spi_device(ctlr, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001682 if (IS_ERR(spi)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001683 dev_warn(&ctlr->dev,
1684 "Failed to create SPI device for %s\n",
1685 nc->full_name);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001686 of_node_clear_flag(nc, OF_POPULATED);
1687 }
Grant Likelyd57a4282012-04-07 14:16:53 -06001688 }
1689}
1690#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001691static void of_register_spi_devices(struct spi_controller *ctlr) { }
Grant Likelyd57a4282012-04-07 14:16:53 -06001692#endif
1693
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001694#ifdef CONFIG_ACPI
1695static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1696{
1697 struct spi_device *spi = data;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001698 struct spi_controller *ctlr = spi->controller;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001699
1700 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1701 struct acpi_resource_spi_serialbus *sb;
1702
1703 sb = &ares->data.spi_serial_bus;
1704 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Mika Westerberga0a90712016-02-08 17:14:28 +02001705 /*
1706 * ACPI DeviceSelection numbering is handled by the
1707 * host controller driver in Windows and can vary
1708 * from driver to driver. In Linux we always expect
1709 * 0 .. max - 1 so we need to ask the driver to
1710 * translate between the two schemes.
1711 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001712 if (ctlr->fw_translate_cs) {
1713 int cs = ctlr->fw_translate_cs(ctlr,
Mika Westerberga0a90712016-02-08 17:14:28 +02001714 sb->device_selection);
1715 if (cs < 0)
1716 return cs;
1717 spi->chip_select = cs;
1718 } else {
1719 spi->chip_select = sb->device_selection;
1720 }
1721
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001722 spi->max_speed_hz = sb->connection_speed;
1723
1724 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1725 spi->mode |= SPI_CPHA;
1726 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1727 spi->mode |= SPI_CPOL;
1728 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1729 spi->mode |= SPI_CS_HIGH;
1730 }
1731 } else if (spi->irq < 0) {
1732 struct resource r;
1733
1734 if (acpi_dev_resource_interrupt(ares, 0, &r))
1735 spi->irq = r.start;
1736 }
1737
1738 /* Always tell the ACPI core to skip this resource */
1739 return 1;
1740}
1741
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001742static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
Octavian Purdila7f244672016-07-08 19:13:11 +03001743 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001744{
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001745 struct list_head resource_list;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001746 struct spi_device *spi;
1747 int ret;
1748
Octavian Purdila7f244672016-07-08 19:13:11 +03001749 if (acpi_bus_get_status(adev) || !adev->status.present ||
1750 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001751 return AE_OK;
1752
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001753 spi = spi_alloc_device(ctlr);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001754 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001755 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001756 dev_name(&adev->dev));
1757 return AE_NO_MEMORY;
1758 }
1759
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001760 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001761 spi->irq = -1;
1762
1763 INIT_LIST_HEAD(&resource_list);
1764 ret = acpi_dev_get_resources(adev, &resource_list,
1765 acpi_spi_add_resource, spi);
1766 acpi_dev_free_resource_list(&resource_list);
1767
1768 if (ret < 0 || !spi->max_speed_hz) {
1769 spi_dev_put(spi);
1770 return AE_OK;
1771 }
1772
Dan O'Donovan0c6543f2017-02-05 16:30:14 +00001773 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
1774 sizeof(spi->modalias));
1775
Christophe RICARD33ada672015-12-23 23:25:35 +01001776 if (spi->irq < 0)
1777 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1778
Octavian Purdila7f244672016-07-08 19:13:11 +03001779 acpi_device_set_enumerated(adev);
1780
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001781 adev->power.flags.ignore_parent = true;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001782 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001783 adev->power.flags.ignore_parent = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001784 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001785 dev_name(&adev->dev));
1786 spi_dev_put(spi);
1787 }
1788
1789 return AE_OK;
1790}
1791
Octavian Purdila7f244672016-07-08 19:13:11 +03001792static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1793 void *data, void **return_value)
1794{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001795 struct spi_controller *ctlr = data;
Octavian Purdila7f244672016-07-08 19:13:11 +03001796 struct acpi_device *adev;
1797
1798 if (acpi_bus_get_device(handle, &adev))
1799 return AE_OK;
1800
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001801 return acpi_register_spi_device(ctlr, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03001802}
1803
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001804static void acpi_register_spi_devices(struct spi_controller *ctlr)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001805{
1806 acpi_status status;
1807 acpi_handle handle;
1808
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001809 handle = ACPI_HANDLE(ctlr->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001810 if (!handle)
1811 return;
1812
1813 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001814 acpi_spi_add_device, NULL, ctlr, NULL);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001815 if (ACPI_FAILURE(status))
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001816 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001817}
1818#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001819static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001820#endif /* CONFIG_ACPI */
1821
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001822static void spi_controller_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001823{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001824 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08001825
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001826 ctlr = container_of(dev, struct spi_controller, dev);
1827 kfree(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08001828}
1829
1830static struct class spi_master_class = {
1831 .name = "spi_master",
1832 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001833 .dev_release = spi_controller_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00001834 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08001835};
1836
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001837#ifdef CONFIG_SPI_SLAVE
1838/**
1839 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
1840 * controller
1841 * @spi: device used for the current transfer
1842 */
1843int spi_slave_abort(struct spi_device *spi)
1844{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001845 struct spi_controller *ctlr = spi->controller;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001846
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001847 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
1848 return ctlr->slave_abort(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001849
1850 return -ENOTSUPP;
1851}
1852EXPORT_SYMBOL_GPL(spi_slave_abort);
1853
1854static int match_true(struct device *dev, void *data)
1855{
1856 return 1;
1857}
1858
1859static ssize_t spi_slave_show(struct device *dev,
1860 struct device_attribute *attr, char *buf)
1861{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001862 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1863 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001864 struct device *child;
1865
1866 child = device_find_child(&ctlr->dev, NULL, match_true);
1867 return sprintf(buf, "%s\n",
1868 child ? to_spi_device(child)->modalias : NULL);
1869}
1870
1871static ssize_t spi_slave_store(struct device *dev,
1872 struct device_attribute *attr, const char *buf,
1873 size_t count)
1874{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001875 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1876 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001877 struct spi_device *spi;
1878 struct device *child;
1879 char name[32];
1880 int rc;
1881
1882 rc = sscanf(buf, "%31s", name);
1883 if (rc != 1 || !name[0])
1884 return -EINVAL;
1885
1886 child = device_find_child(&ctlr->dev, NULL, match_true);
1887 if (child) {
1888 /* Remove registered slave */
1889 device_unregister(child);
1890 put_device(child);
1891 }
1892
1893 if (strcmp(name, "(null)")) {
1894 /* Register new slave */
1895 spi = spi_alloc_device(ctlr);
1896 if (!spi)
1897 return -ENOMEM;
1898
1899 strlcpy(spi->modalias, name, sizeof(spi->modalias));
1900
1901 rc = spi_add_device(spi);
1902 if (rc) {
1903 spi_dev_put(spi);
1904 return rc;
1905 }
1906 }
1907
1908 return count;
1909}
1910
1911static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store);
1912
1913static struct attribute *spi_slave_attrs[] = {
1914 &dev_attr_slave.attr,
1915 NULL,
1916};
1917
1918static const struct attribute_group spi_slave_group = {
1919 .attrs = spi_slave_attrs,
1920};
1921
1922static const struct attribute_group *spi_slave_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001923 &spi_controller_statistics_group,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001924 &spi_slave_group,
1925 NULL,
1926};
1927
1928static struct class spi_slave_class = {
1929 .name = "spi_slave",
1930 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001931 .dev_release = spi_controller_release,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001932 .dev_groups = spi_slave_groups,
1933};
1934#else
1935extern struct class spi_slave_class; /* dummy */
1936#endif
David Brownell8ae12a02006-01-08 13:34:19 -08001937
1938/**
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001939 * __spi_alloc_controller - allocate an SPI master or slave controller
David Brownell8ae12a02006-01-08 13:34:19 -08001940 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001941 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001942 * memory is in the driver_data field of the returned device,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001943 * accessible with spi_controller_get_devdata().
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001944 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
1945 * slave (true) controller
David Brownell33e34dc2007-05-08 00:32:21 -07001946 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001947 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001948 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08001949 * only ones directly touching chip registers. It's how they allocate
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001950 * an spi_controller structure, prior to calling spi_register_controller().
David Brownell8ae12a02006-01-08 13:34:19 -08001951 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001952 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08001953 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001954 * The caller is responsible for assigning the bus number and initializing the
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001955 * controller's methods before calling spi_register_controller(); and (after
1956 * errors adding the device) calling spi_controller_put() to prevent a memory
1957 * leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001958 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001959 * Return: the SPI controller structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08001960 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001961struct spi_controller *__spi_alloc_controller(struct device *dev,
1962 unsigned int size, bool slave)
David Brownell8ae12a02006-01-08 13:34:19 -08001963{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001964 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08001965
David Brownell0c868462006-01-08 13:34:25 -08001966 if (!dev)
1967 return NULL;
1968
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001969 ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL);
1970 if (!ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08001971 return NULL;
1972
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001973 device_initialize(&ctlr->dev);
1974 ctlr->bus_num = -1;
1975 ctlr->num_chipselect = 1;
1976 ctlr->slave = slave;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001977 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001978 ctlr->dev.class = &spi_slave_class;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001979 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001980 ctlr->dev.class = &spi_master_class;
1981 ctlr->dev.parent = dev;
1982 pm_suspend_ignore_children(&ctlr->dev, true);
1983 spi_controller_set_devdata(ctlr, &ctlr[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001984
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001985 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08001986}
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001987EXPORT_SYMBOL_GPL(__spi_alloc_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08001988
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001989#ifdef CONFIG_OF
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001990static int of_spi_register_master(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001991{
Grant Likelye80beb22013-02-12 17:48:37 +00001992 int nb, i, *cs;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001993 struct device_node *np = ctlr->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001994
1995 if (!np)
1996 return 0;
1997
1998 nb = of_gpio_named_count(np, "cs-gpios");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001999 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002000
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002001 /* Return error only for an incorrectly formed cs-gpios property */
2002 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002003 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002004 else if (nb < 0)
2005 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002006
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002007 cs = devm_kzalloc(&ctlr->dev, sizeof(int) * ctlr->num_chipselect,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002008 GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002009 ctlr->cs_gpios = cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002010
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002011 if (!ctlr->cs_gpios)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002012 return -ENOMEM;
2013
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002014 for (i = 0; i < ctlr->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01002015 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002016
2017 for (i = 0; i < nb; i++)
2018 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2019
2020 return 0;
2021}
2022#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002023static int of_spi_register_master(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002024{
2025 return 0;
2026}
2027#endif
2028
David Brownell8ae12a02006-01-08 13:34:19 -08002029/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002030 * spi_register_controller - register SPI master or slave controller
2031 * @ctlr: initialized master, originally from spi_alloc_master() or
2032 * spi_alloc_slave()
David Brownell33e34dc2007-05-08 00:32:21 -07002033 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002034 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002035 * SPI controllers connect to their drivers using some non-SPI bus,
David Brownell8ae12a02006-01-08 13:34:19 -08002036 * such as the platform bus. The final stage of probe() in that code
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002037 * includes calling spi_register_controller() to hook up to this SPI bus glue.
David Brownell8ae12a02006-01-08 13:34:19 -08002038 *
2039 * SPI controllers use board specific (often SOC specific) bus numbers,
2040 * and board-specific addressing for SPI devices combines those numbers
2041 * with chip select numbers. Since SPI does not directly support dynamic
2042 * device identification, boards need configuration tables telling which
2043 * chip is at which address.
2044 *
2045 * This must be called from context that can sleep. It returns zero on
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002046 * success, else a negative error code (dropping the controller's refcount).
David Brownell0c868462006-01-08 13:34:25 -08002047 * After a successful return, the caller is responsible for calling
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002048 * spi_unregister_controller().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002049 *
2050 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002051 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002052int spi_register_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002053{
David Brownelle44a45a2007-06-03 13:50:40 -07002054 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002055 struct device *dev = ctlr->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08002056 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08002057 int status = -ENODEV;
2058 int dynamic = 0;
2059
David Brownell0c868462006-01-08 13:34:25 -08002060 if (!dev)
2061 return -ENODEV;
2062
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002063 if (!spi_controller_is_slave(ctlr)) {
2064 status = of_spi_register_master(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002065 if (status)
2066 return status;
2067 }
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002068
David Brownell082c8cb2007-07-31 00:39:45 -07002069 /* even if it's just one always-selected device, there must
2070 * be at least one chipselect
2071 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002072 if (ctlr->num_chipselect == 0)
David Brownell082c8cb2007-07-31 00:39:45 -07002073 return -EINVAL;
2074
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002075 if ((ctlr->bus_num < 0) && ctlr->dev.of_node)
2076 ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi");
Grant Likelybb297852012-12-21 19:32:09 +00002077
David Brownell8ae12a02006-01-08 13:34:19 -08002078 /* convention: dynamically assigned bus IDs count down from the max */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002079 if (ctlr->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07002080 /* FIXME switch to an IDR based scheme, something like
2081 * I2C now uses, so we can't run out of "dynamic" IDs
2082 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002083 ctlr->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08002084 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08002085 }
2086
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002087 INIT_LIST_HEAD(&ctlr->queue);
2088 spin_lock_init(&ctlr->queue_lock);
2089 spin_lock_init(&ctlr->bus_lock_spinlock);
2090 mutex_init(&ctlr->bus_lock_mutex);
2091 mutex_init(&ctlr->io_mutex);
2092 ctlr->bus_lock_flag = 0;
2093 init_completion(&ctlr->xfer_completion);
2094 if (!ctlr->max_dma_len)
2095 ctlr->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002096
David Brownell8ae12a02006-01-08 13:34:19 -08002097 /* register the device, then userspace will see it.
2098 * registration fails if the bus ID is in use.
2099 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002100 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
2101 status = device_add(&ctlr->dev);
David Brownellb8852442006-01-08 13:34:23 -08002102 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08002103 goto done;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002104 dev_dbg(dev, "registered %s %s%s\n",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002105 spi_controller_is_slave(ctlr) ? "slave" : "master",
2106 dev_name(&ctlr->dev), dynamic ? " (dynamic)" : "");
David Brownell8ae12a02006-01-08 13:34:19 -08002107
Linus Walleijffbbdd212012-02-22 10:05:38 +01002108 /* If we're using a queued driver, start the queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002109 if (ctlr->transfer)
2110 dev_info(dev, "controller is unqueued, this is deprecated\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002111 else {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002112 status = spi_controller_initialize_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002113 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002114 device_del(&ctlr->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002115 goto done;
2116 }
2117 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00002118 /* add statistics */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002119 spin_lock_init(&ctlr->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002120
Feng Tang2b9603a2010-08-02 15:52:15 +08002121 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002122 list_add_tail(&ctlr->list, &spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002123 list_for_each_entry(bi, &board_list, list)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002124 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +08002125 mutex_unlock(&board_lock);
2126
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002127 /* Register devices from the device tree and ACPI */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002128 of_register_spi_devices(ctlr);
2129 acpi_register_spi_devices(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002130done:
2131 return status;
2132}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002133EXPORT_SYMBOL_GPL(spi_register_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002134
Mark Brown666d5b42013-08-31 18:50:52 +01002135static void devm_spi_unregister(struct device *dev, void *res)
2136{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002137 spi_unregister_controller(*(struct spi_controller **)res);
Mark Brown666d5b42013-08-31 18:50:52 +01002138}
2139
2140/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002141 * devm_spi_register_controller - register managed SPI master or slave
2142 * controller
2143 * @dev: device managing SPI controller
2144 * @ctlr: initialized controller, originally from spi_alloc_master() or
2145 * spi_alloc_slave()
Mark Brown666d5b42013-08-31 18:50:52 +01002146 * Context: can sleep
2147 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002148 * Register a SPI device as with spi_register_controller() which will
Mark Brown666d5b42013-08-31 18:50:52 +01002149 * automatically be unregister
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002150 *
2151 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01002152 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002153int devm_spi_register_controller(struct device *dev,
2154 struct spi_controller *ctlr)
Mark Brown666d5b42013-08-31 18:50:52 +01002155{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002156 struct spi_controller **ptr;
Mark Brown666d5b42013-08-31 18:50:52 +01002157 int ret;
2158
2159 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2160 if (!ptr)
2161 return -ENOMEM;
2162
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002163 ret = spi_register_controller(ctlr);
Stephen Warren4b928942013-11-21 16:11:15 -07002164 if (!ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002165 *ptr = ctlr;
Mark Brown666d5b42013-08-31 18:50:52 +01002166 devres_add(dev, ptr);
2167 } else {
2168 devres_free(ptr);
2169 }
2170
2171 return ret;
2172}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002173EXPORT_SYMBOL_GPL(devm_spi_register_controller);
Mark Brown666d5b42013-08-31 18:50:52 +01002174
David Lamparter34860082010-08-30 23:54:17 +02002175static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002176{
David Lamparter34860082010-08-30 23:54:17 +02002177 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002178 return 0;
2179}
2180
2181/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002182 * spi_unregister_controller - unregister SPI master or slave controller
2183 * @ctlr: the controller being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002184 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002185 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002186 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002187 * only ones directly touching chip registers.
2188 *
2189 * This must be called from context that can sleep.
2190 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002191void spi_unregister_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002192{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002193 int dummy;
2194
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002195 if (ctlr->queued) {
2196 if (spi_destroy_queue(ctlr))
2197 dev_err(&ctlr->dev, "queue remove failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002198 }
2199
Feng Tang2b9603a2010-08-02 15:52:15 +08002200 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002201 list_del(&ctlr->list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002202 mutex_unlock(&board_lock);
2203
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002204 dummy = device_for_each_child(&ctlr->dev, NULL, __unregister);
2205 device_unregister(&ctlr->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08002206}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002207EXPORT_SYMBOL_GPL(spi_unregister_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002208
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002209int spi_controller_suspend(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002210{
2211 int ret;
2212
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002213 /* Basically no-ops for non-queued controllers */
2214 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002215 return 0;
2216
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002217 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002218 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002219 dev_err(&ctlr->dev, "queue stop failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002220
2221 return ret;
2222}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002223EXPORT_SYMBOL_GPL(spi_controller_suspend);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002224
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002225int spi_controller_resume(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002226{
2227 int ret;
2228
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002229 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002230 return 0;
2231
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002232 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002233 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002234 dev_err(&ctlr->dev, "queue restart failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002235
2236 return ret;
2237}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002238EXPORT_SYMBOL_GPL(spi_controller_resume);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002239
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002240static int __spi_controller_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002241{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002242 struct spi_controller *ctlr;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002243 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002244
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002245 ctlr = container_of(dev, struct spi_controller, dev);
2246 return ctlr->bus_num == *bus_num;
Dave Young5ed2c832008-01-22 15:14:18 +08002247}
2248
David Brownell8ae12a02006-01-08 13:34:19 -08002249/**
2250 * spi_busnum_to_master - look up master associated with bus_num
2251 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002252 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002253 *
2254 * This call may be used with devices that are registered after
2255 * arch init time. It returns a refcounted pointer to the relevant
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002256 * spi_controller (which the caller must release), or NULL if there is
David Brownell8ae12a02006-01-08 13:34:19 -08002257 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002258 *
2259 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002260 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002261struct spi_controller *spi_busnum_to_master(u16 bus_num)
David Brownell8ae12a02006-01-08 13:34:19 -08002262{
Tony Jones49dce682007-10-16 01:27:48 -07002263 struct device *dev;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002264 struct spi_controller *ctlr = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002265
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002266 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002267 __spi_controller_match);
Dave Young5ed2c832008-01-22 15:14:18 +08002268 if (dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002269 ctlr = container_of(dev, struct spi_controller, dev);
Dave Young5ed2c832008-01-22 15:14:18 +08002270 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002271 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002272}
2273EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2274
Martin Sperld780c372015-12-14 15:20:18 +00002275/*-------------------------------------------------------------------------*/
2276
2277/* Core methods for SPI resource management */
2278
2279/**
2280 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2281 * during the processing of a spi_message while using
2282 * spi_transfer_one
2283 * @spi: the spi device for which we allocate memory
2284 * @release: the release code to execute for this resource
2285 * @size: size to alloc and return
2286 * @gfp: GFP allocation flags
2287 *
2288 * Return: the pointer to the allocated data
2289 *
2290 * This may get enhanced in the future to allocate from a memory pool
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002291 * of the @spi_device or @spi_controller to avoid repeated allocations.
Martin Sperld780c372015-12-14 15:20:18 +00002292 */
2293void *spi_res_alloc(struct spi_device *spi,
2294 spi_res_release_t release,
2295 size_t size, gfp_t gfp)
2296{
2297 struct spi_res *sres;
2298
2299 sres = kzalloc(sizeof(*sres) + size, gfp);
2300 if (!sres)
2301 return NULL;
2302
2303 INIT_LIST_HEAD(&sres->entry);
2304 sres->release = release;
2305
2306 return sres->data;
2307}
2308EXPORT_SYMBOL_GPL(spi_res_alloc);
2309
2310/**
2311 * spi_res_free - free an spi resource
2312 * @res: pointer to the custom data of a resource
2313 *
2314 */
2315void spi_res_free(void *res)
2316{
2317 struct spi_res *sres = container_of(res, struct spi_res, data);
2318
2319 if (!res)
2320 return;
2321
2322 WARN_ON(!list_empty(&sres->entry));
2323 kfree(sres);
2324}
2325EXPORT_SYMBOL_GPL(spi_res_free);
2326
2327/**
2328 * spi_res_add - add a spi_res to the spi_message
2329 * @message: the spi message
2330 * @res: the spi_resource
2331 */
2332void spi_res_add(struct spi_message *message, void *res)
2333{
2334 struct spi_res *sres = container_of(res, struct spi_res, data);
2335
2336 WARN_ON(!list_empty(&sres->entry));
2337 list_add_tail(&sres->entry, &message->resources);
2338}
2339EXPORT_SYMBOL_GPL(spi_res_add);
2340
2341/**
2342 * spi_res_release - release all spi resources for this message
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002343 * @ctlr: the @spi_controller
Martin Sperld780c372015-12-14 15:20:18 +00002344 * @message: the @spi_message
2345 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002346void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
Martin Sperld780c372015-12-14 15:20:18 +00002347{
2348 struct spi_res *res;
2349
2350 while (!list_empty(&message->resources)) {
2351 res = list_last_entry(&message->resources,
2352 struct spi_res, entry);
2353
2354 if (res->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002355 res->release(ctlr, message, res->data);
Martin Sperld780c372015-12-14 15:20:18 +00002356
2357 list_del(&res->entry);
2358
2359 kfree(res);
2360 }
2361}
2362EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002363
2364/*-------------------------------------------------------------------------*/
2365
Martin Sperl523baf5a2015-12-14 15:20:19 +00002366/* Core methods for spi_message alterations */
2367
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002368static void __spi_replace_transfers_release(struct spi_controller *ctlr,
Martin Sperl523baf5a2015-12-14 15:20:19 +00002369 struct spi_message *msg,
2370 void *res)
2371{
2372 struct spi_replaced_transfers *rxfer = res;
2373 size_t i;
2374
2375 /* call extra callback if requested */
2376 if (rxfer->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002377 rxfer->release(ctlr, msg, res);
Martin Sperl523baf5a2015-12-14 15:20:19 +00002378
2379 /* insert replaced transfers back into the message */
2380 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2381
2382 /* remove the formerly inserted entries */
2383 for (i = 0; i < rxfer->inserted; i++)
2384 list_del(&rxfer->inserted_transfers[i].transfer_list);
2385}
2386
2387/**
2388 * spi_replace_transfers - replace transfers with several transfers
2389 * and register change with spi_message.resources
2390 * @msg: the spi_message we work upon
2391 * @xfer_first: the first spi_transfer we want to replace
2392 * @remove: number of transfers to remove
2393 * @insert: the number of transfers we want to insert instead
2394 * @release: extra release code necessary in some circumstances
2395 * @extradatasize: extra data to allocate (with alignment guarantees
2396 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002397 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002398 *
2399 * Returns: pointer to @spi_replaced_transfers,
2400 * PTR_ERR(...) in case of errors.
2401 */
2402struct spi_replaced_transfers *spi_replace_transfers(
2403 struct spi_message *msg,
2404 struct spi_transfer *xfer_first,
2405 size_t remove,
2406 size_t insert,
2407 spi_replaced_release_t release,
2408 size_t extradatasize,
2409 gfp_t gfp)
2410{
2411 struct spi_replaced_transfers *rxfer;
2412 struct spi_transfer *xfer;
2413 size_t i;
2414
2415 /* allocate the structure using spi_res */
2416 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2417 insert * sizeof(struct spi_transfer)
2418 + sizeof(struct spi_replaced_transfers)
2419 + extradatasize,
2420 gfp);
2421 if (!rxfer)
2422 return ERR_PTR(-ENOMEM);
2423
2424 /* the release code to invoke before running the generic release */
2425 rxfer->release = release;
2426
2427 /* assign extradata */
2428 if (extradatasize)
2429 rxfer->extradata =
2430 &rxfer->inserted_transfers[insert];
2431
2432 /* init the replaced_transfers list */
2433 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2434
2435 /* assign the list_entry after which we should reinsert
2436 * the @replaced_transfers - it may be spi_message.messages!
2437 */
2438 rxfer->replaced_after = xfer_first->transfer_list.prev;
2439
2440 /* remove the requested number of transfers */
2441 for (i = 0; i < remove; i++) {
2442 /* if the entry after replaced_after it is msg->transfers
2443 * then we have been requested to remove more transfers
2444 * than are in the list
2445 */
2446 if (rxfer->replaced_after->next == &msg->transfers) {
2447 dev_err(&msg->spi->dev,
2448 "requested to remove more spi_transfers than are available\n");
2449 /* insert replaced transfers back into the message */
2450 list_splice(&rxfer->replaced_transfers,
2451 rxfer->replaced_after);
2452
2453 /* free the spi_replace_transfer structure */
2454 spi_res_free(rxfer);
2455
2456 /* and return with an error */
2457 return ERR_PTR(-EINVAL);
2458 }
2459
2460 /* remove the entry after replaced_after from list of
2461 * transfers and add it to list of replaced_transfers
2462 */
2463 list_move_tail(rxfer->replaced_after->next,
2464 &rxfer->replaced_transfers);
2465 }
2466
2467 /* create copy of the given xfer with identical settings
2468 * based on the first transfer to get removed
2469 */
2470 for (i = 0; i < insert; i++) {
2471 /* we need to run in reverse order */
2472 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2473
2474 /* copy all spi_transfer data */
2475 memcpy(xfer, xfer_first, sizeof(*xfer));
2476
2477 /* add to list */
2478 list_add(&xfer->transfer_list, rxfer->replaced_after);
2479
2480 /* clear cs_change and delay_usecs for all but the last */
2481 if (i) {
2482 xfer->cs_change = false;
2483 xfer->delay_usecs = 0;
2484 }
2485 }
2486
2487 /* set up inserted */
2488 rxfer->inserted = insert;
2489
2490 /* and register it with spi_res/spi_message */
2491 spi_res_add(msg, rxfer);
2492
2493 return rxfer;
2494}
2495EXPORT_SYMBOL_GPL(spi_replace_transfers);
2496
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002497static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
Fabio Estevam08933412016-02-14 13:33:50 -02002498 struct spi_message *msg,
2499 struct spi_transfer **xferp,
2500 size_t maxsize,
2501 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00002502{
2503 struct spi_transfer *xfer = *xferp, *xfers;
2504 struct spi_replaced_transfers *srt;
2505 size_t offset;
2506 size_t count, i;
2507
2508 /* warn once about this fact that we are splitting a transfer */
2509 dev_warn_once(&msg->spi->dev,
Fabio Estevam7d62f512016-02-17 15:42:27 -02002510 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
Martin Sperld9f12122015-12-14 15:20:20 +00002511 xfer->len, maxsize);
2512
2513 /* calculate how many we have to replace */
2514 count = DIV_ROUND_UP(xfer->len, maxsize);
2515
2516 /* create replacement */
2517 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03002518 if (IS_ERR(srt))
2519 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00002520 xfers = srt->inserted_transfers;
2521
2522 /* now handle each of those newly inserted spi_transfers
2523 * note that the replacements spi_transfers all are preset
2524 * to the same values as *xferp, so tx_buf, rx_buf and len
2525 * are all identical (as well as most others)
2526 * so we just have to fix up len and the pointers.
2527 *
2528 * this also includes support for the depreciated
2529 * spi_message.is_dma_mapped interface
2530 */
2531
2532 /* the first transfer just needs the length modified, so we
2533 * run it outside the loop
2534 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02002535 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00002536
2537 /* all the others need rx_buf/tx_buf also set */
2538 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2539 /* update rx_buf, tx_buf and dma */
2540 if (xfers[i].rx_buf)
2541 xfers[i].rx_buf += offset;
2542 if (xfers[i].rx_dma)
2543 xfers[i].rx_dma += offset;
2544 if (xfers[i].tx_buf)
2545 xfers[i].tx_buf += offset;
2546 if (xfers[i].tx_dma)
2547 xfers[i].tx_dma += offset;
2548
2549 /* update length */
2550 xfers[i].len = min(maxsize, xfers[i].len - offset);
2551 }
2552
2553 /* we set up xferp to the last entry we have inserted,
2554 * so that we skip those already split transfers
2555 */
2556 *xferp = &xfers[count - 1];
2557
2558 /* increment statistics counters */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002559 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperld9f12122015-12-14 15:20:20 +00002560 transfers_split_maxsize);
2561 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2562 transfers_split_maxsize);
2563
2564 return 0;
2565}
2566
2567/**
2568 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2569 * when an individual transfer exceeds a
2570 * certain size
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002571 * @ctlr: the @spi_controller for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09002572 * @msg: the @spi_message to transform
2573 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03002574 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00002575 *
2576 * Return: status of transformation
2577 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002578int spi_split_transfers_maxsize(struct spi_controller *ctlr,
Martin Sperld9f12122015-12-14 15:20:20 +00002579 struct spi_message *msg,
2580 size_t maxsize,
2581 gfp_t gfp)
2582{
2583 struct spi_transfer *xfer;
2584 int ret;
2585
2586 /* iterate over the transfer_list,
2587 * but note that xfer is advanced to the last transfer inserted
2588 * to avoid checking sizes again unnecessarily (also xfer does
2589 * potentiall belong to a different list by the time the
2590 * replacement has happened
2591 */
2592 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2593 if (xfer->len > maxsize) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002594 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
2595 maxsize, gfp);
Martin Sperld9f12122015-12-14 15:20:20 +00002596 if (ret)
2597 return ret;
2598 }
2599 }
2600
2601 return 0;
2602}
2603EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08002604
2605/*-------------------------------------------------------------------------*/
2606
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002607/* Core methods for SPI controller protocol drivers. Some of the
David Brownell7d077192009-06-17 16:26:03 -07002608 * other core methods are currently defined as inline functions.
2609 */
2610
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002611static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
2612 u8 bits_per_word)
Stefan Brüns63ab6452015-08-23 16:06:30 +02002613{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002614 if (ctlr->bits_per_word_mask) {
Stefan Brüns63ab6452015-08-23 16:06:30 +02002615 /* Only 32 bits fit in the mask */
2616 if (bits_per_word > 32)
2617 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002618 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
Stefan Brüns63ab6452015-08-23 16:06:30 +02002619 return -EINVAL;
2620 }
2621
2622 return 0;
2623}
2624
David Brownell7d077192009-06-17 16:26:03 -07002625/**
2626 * spi_setup - setup SPI mode and clock rate
2627 * @spi: the device whose settings are being modified
2628 * Context: can sleep, and no requests are queued to the device
2629 *
2630 * SPI protocol drivers may need to update the transfer mode if the
2631 * device doesn't work with its default. They may likewise need
2632 * to update clock rates or word sizes from initial values. This function
2633 * changes those settings, and must be called from a context that can sleep.
2634 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
2635 * effect the next time the device is selected and data is transferred to
2636 * or from it. When this function returns, the spi device is deselected.
2637 *
2638 * Note that this call will fail if the protocol driver specifies an option
2639 * that the underlying controller or its driver does not support. For
2640 * example, not all hardware supports wire transfers using nine bit words,
2641 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002642 *
2643 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07002644 */
2645int spi_setup(struct spi_device *spi)
2646{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002647 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002648 int status;
David Brownell7d077192009-06-17 16:26:03 -07002649
wangyuhangf477b7f2013-08-11 18:15:17 +08002650 /* check mode to prevent that DUAL and QUAD set at the same time
2651 */
2652 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
2653 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
2654 dev_err(&spi->dev,
2655 "setup: can not select dual and quad at the same time\n");
2656 return -EINVAL;
2657 }
2658 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
2659 */
2660 if ((spi->mode & SPI_3WIRE) && (spi->mode &
2661 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
2662 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07002663 /* help drivers fail *cleanly* when they need options
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002664 * that aren't supported with their current controller
David Brownelle7db06b2009-06-17 16:26:04 -07002665 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002666 bad_bits = spi->mode & ~spi->controller->mode_bits;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002667 ugly_bits = bad_bits &
2668 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
2669 if (ugly_bits) {
2670 dev_warn(&spi->dev,
2671 "setup: ignoring unsupported mode bits %x\n",
2672 ugly_bits);
2673 spi->mode &= ~ugly_bits;
2674 bad_bits &= ~ugly_bits;
2675 }
David Brownelle7db06b2009-06-17 16:26:04 -07002676 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02002677 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07002678 bad_bits);
2679 return -EINVAL;
2680 }
2681
David Brownell7d077192009-06-17 16:26:03 -07002682 if (!spi->bits_per_word)
2683 spi->bits_per_word = 8;
2684
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002685 status = __spi_validate_bits_per_word(spi->controller,
2686 spi->bits_per_word);
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002687 if (status)
2688 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02002689
Axel Lin052eb2d2014-02-10 00:08:05 +08002690 if (!spi->max_speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002691 spi->max_speed_hz = spi->controller->max_speed_hz;
Axel Lin052eb2d2014-02-10 00:08:05 +08002692
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002693 if (spi->controller->setup)
2694 status = spi->controller->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07002695
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05002696 spi_set_cs(spi, false);
2697
Jingoo Han5fe5f052013-10-14 10:31:51 +09002698 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 -07002699 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2700 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2701 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2702 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2703 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2704 spi->bits_per_word, spi->max_speed_hz,
2705 status);
2706
2707 return status;
2708}
2709EXPORT_SYMBOL_GPL(spi_setup);
2710
Mark Brown90808732013-11-13 23:44:15 +00002711static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002712{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002713 struct spi_controller *ctlr = spi->controller;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302714 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002715 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002716
Mark Brown24a00132013-07-10 15:05:40 +01002717 if (list_empty(&message->transfers))
2718 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01002719
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002720 /* Half-duplex links include original MicroWire, and ones with
2721 * only one data pin like SPI_3WIRE (switches direction) or where
2722 * either MOSI or MISO is missing. They can also be caused by
2723 * software limitations.
2724 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002725 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
2726 (spi->mode & SPI_3WIRE)) {
2727 unsigned flags = ctlr->flags;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002728
2729 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2730 if (xfer->rx_buf && xfer->tx_buf)
2731 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002732 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002733 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002734 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002735 return -EINVAL;
2736 }
2737 }
2738
Laxman Dewangane6811d12012-11-09 14:36:45 +05302739 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302740 * Set transfer bits_per_word and max speed as spi device default if
2741 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08002742 * Set transfer tx_nbits and rx_nbits as single transfer default
2743 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05302744 */
Martin Sperl77e80582015-11-27 12:31:09 +00002745 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302746 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05302747 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302748 if (!xfer->bits_per_word)
2749 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08002750
2751 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302752 xfer->speed_hz = spi->max_speed_hz;
Mark Brown7dc9fbc2015-08-20 11:52:18 -07002753 if (!xfer->speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002754 xfer->speed_hz = ctlr->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08002755
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002756 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
2757 xfer->speed_hz = ctlr->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02002758
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002759 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
Stefan Brüns63ab6452015-08-23 16:06:30 +02002760 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01002761
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002762 /*
2763 * SPI transfer length should be multiple of SPI word size
2764 * where SPI word size should be power-of-two multiple
2765 */
2766 if (xfer->bits_per_word <= 8)
2767 w_size = 1;
2768 else if (xfer->bits_per_word <= 16)
2769 w_size = 2;
2770 else
2771 w_size = 4;
2772
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002773 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002774 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002775 return -EINVAL;
2776
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002777 if (xfer->speed_hz && ctlr->min_speed_hz &&
2778 xfer->speed_hz < ctlr->min_speed_hz)
Mark Browna2fd4f92013-07-10 14:57:26 +01002779 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08002780
2781 if (xfer->tx_buf && !xfer->tx_nbits)
2782 xfer->tx_nbits = SPI_NBITS_SINGLE;
2783 if (xfer->rx_buf && !xfer->rx_nbits)
2784 xfer->rx_nbits = SPI_NBITS_SINGLE;
2785 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01002786 * 1. check the value matches one of single, dual and quad
2787 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08002788 */
Sourav Poddardb90a442013-08-22 21:20:48 +05302789 if (xfer->tx_buf) {
2790 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2791 xfer->tx_nbits != SPI_NBITS_DUAL &&
2792 xfer->tx_nbits != SPI_NBITS_QUAD)
2793 return -EINVAL;
2794 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2795 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2796 return -EINVAL;
2797 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2798 !(spi->mode & SPI_TX_QUAD))
2799 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302800 }
wangyuhangf477b7f2013-08-11 18:15:17 +08002801 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05302802 if (xfer->rx_buf) {
2803 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2804 xfer->rx_nbits != SPI_NBITS_DUAL &&
2805 xfer->rx_nbits != SPI_NBITS_QUAD)
2806 return -EINVAL;
2807 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2808 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2809 return -EINVAL;
2810 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2811 !(spi->mode & SPI_RX_QUAD))
2812 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302813 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05302814 }
2815
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002816 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00002817
2818 return 0;
2819}
2820
2821static int __spi_async(struct spi_device *spi, struct spi_message *message)
2822{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002823 struct spi_controller *ctlr = spi->controller;
Mark Brown90808732013-11-13 23:44:15 +00002824
2825 message->spi = spi;
2826
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002827 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
Martin Sperleca2ebc2015-06-22 13:00:36 +00002828 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2829
Mark Brown90808732013-11-13 23:44:15 +00002830 trace_spi_message_submit(message);
2831
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002832 return ctlr->transfer(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002833}
2834
David Brownell568d0692009-09-22 16:46:18 -07002835/**
2836 * spi_async - asynchronous SPI transfer
2837 * @spi: device with which data will be exchanged
2838 * @message: describes the data transfers, including completion callback
2839 * Context: any (irqs may be blocked, etc)
2840 *
2841 * This call may be used in_irq and other contexts which can't sleep,
2842 * as well as from task contexts which can sleep.
2843 *
2844 * The completion callback is invoked in a context which can't sleep.
2845 * Before that invocation, the value of message->status is undefined.
2846 * When the callback is issued, message->status holds either zero (to
2847 * indicate complete success) or a negative error code. After that
2848 * callback returns, the driver which issued the transfer request may
2849 * deallocate the associated memory; it's no longer in use by any SPI
2850 * core or controller driver code.
2851 *
2852 * Note that although all messages to a spi_device are handled in
2853 * FIFO order, messages may go to different devices in other orders.
2854 * Some device might be higher priority, or have various "hard" access
2855 * time requirements, for example.
2856 *
2857 * On detection of any fault during the transfer, processing of
2858 * the entire message is aborted, and the device is deselected.
2859 * Until returning from the associated message completion callback,
2860 * no other spi_message queued to that device will be processed.
2861 * (This rule applies equally to all the synchronous transfer calls,
2862 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002863 *
2864 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07002865 */
2866int spi_async(struct spi_device *spi, struct spi_message *message)
2867{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002868 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002869 int ret;
2870 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07002871
Mark Brown90808732013-11-13 23:44:15 +00002872 ret = __spi_validate(spi, message);
2873 if (ret != 0)
2874 return ret;
2875
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002876 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07002877
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002878 if (ctlr->bus_lock_flag)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002879 ret = -EBUSY;
2880 else
2881 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07002882
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002883 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002884
2885 return ret;
David Brownell568d0692009-09-22 16:46:18 -07002886}
2887EXPORT_SYMBOL_GPL(spi_async);
2888
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002889/**
2890 * spi_async_locked - version of spi_async with exclusive bus usage
2891 * @spi: device with which data will be exchanged
2892 * @message: describes the data transfers, including completion callback
2893 * Context: any (irqs may be blocked, etc)
2894 *
2895 * This call may be used in_irq and other contexts which can't sleep,
2896 * as well as from task contexts which can sleep.
2897 *
2898 * The completion callback is invoked in a context which can't sleep.
2899 * Before that invocation, the value of message->status is undefined.
2900 * When the callback is issued, message->status holds either zero (to
2901 * indicate complete success) or a negative error code. After that
2902 * callback returns, the driver which issued the transfer request may
2903 * deallocate the associated memory; it's no longer in use by any SPI
2904 * core or controller driver code.
2905 *
2906 * Note that although all messages to a spi_device are handled in
2907 * FIFO order, messages may go to different devices in other orders.
2908 * Some device might be higher priority, or have various "hard" access
2909 * time requirements, for example.
2910 *
2911 * On detection of any fault during the transfer, processing of
2912 * the entire message is aborted, and the device is deselected.
2913 * Until returning from the associated message completion callback,
2914 * no other spi_message queued to that device will be processed.
2915 * (This rule applies equally to all the synchronous transfer calls,
2916 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002917 *
2918 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002919 */
2920int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2921{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002922 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002923 int ret;
2924 unsigned long flags;
2925
Mark Brown90808732013-11-13 23:44:15 +00002926 ret = __spi_validate(spi, message);
2927 if (ret != 0)
2928 return ret;
2929
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002930 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002931
2932 ret = __spi_async(spi, message);
2933
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002934 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002935
2936 return ret;
2937
2938}
2939EXPORT_SYMBOL_GPL(spi_async_locked);
2940
David Brownell7d077192009-06-17 16:26:03 -07002941
Vignesh R556351f2015-12-11 09:39:56 +05302942int spi_flash_read(struct spi_device *spi,
2943 struct spi_flash_read_message *msg)
2944
2945{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002946 struct spi_controller *master = spi->controller;
Vignesh Rf4502dd2016-06-08 12:18:31 +05302947 struct device *rx_dev = NULL;
Vignesh R556351f2015-12-11 09:39:56 +05302948 int ret;
2949
2950 if ((msg->opcode_nbits == SPI_NBITS_DUAL ||
2951 msg->addr_nbits == SPI_NBITS_DUAL) &&
2952 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2953 return -EINVAL;
2954 if ((msg->opcode_nbits == SPI_NBITS_QUAD ||
2955 msg->addr_nbits == SPI_NBITS_QUAD) &&
2956 !(spi->mode & SPI_TX_QUAD))
2957 return -EINVAL;
2958 if (msg->data_nbits == SPI_NBITS_DUAL &&
2959 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2960 return -EINVAL;
2961 if (msg->data_nbits == SPI_NBITS_QUAD &&
2962 !(spi->mode & SPI_RX_QUAD))
2963 return -EINVAL;
2964
2965 if (master->auto_runtime_pm) {
2966 ret = pm_runtime_get_sync(master->dev.parent);
2967 if (ret < 0) {
2968 dev_err(&master->dev, "Failed to power device: %d\n",
2969 ret);
2970 return ret;
2971 }
2972 }
Vignesh Rf4502dd2016-06-08 12:18:31 +05302973
Vignesh R556351f2015-12-11 09:39:56 +05302974 mutex_lock(&master->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01002975 mutex_lock(&master->io_mutex);
Vignesh R2bca3442017-04-11 17:22:24 +05302976 if (master->dma_rx && master->spi_flash_can_dma(spi, msg)) {
Vignesh Rf4502dd2016-06-08 12:18:31 +05302977 rx_dev = master->dma_rx->device->dev;
2978 ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
2979 msg->buf, msg->len,
2980 DMA_FROM_DEVICE);
2981 if (!ret)
2982 msg->cur_msg_mapped = true;
2983 }
Vignesh R556351f2015-12-11 09:39:56 +05302984 ret = master->spi_flash_read(spi, msg);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302985 if (msg->cur_msg_mapped)
2986 spi_unmap_buf(master, rx_dev, &msg->rx_sg,
2987 DMA_FROM_DEVICE);
Mark Brownef4d96e2016-07-21 23:53:31 +01002988 mutex_unlock(&master->io_mutex);
Vignesh R556351f2015-12-11 09:39:56 +05302989 mutex_unlock(&master->bus_lock_mutex);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302990
Vignesh R556351f2015-12-11 09:39:56 +05302991 if (master->auto_runtime_pm)
2992 pm_runtime_put(master->dev.parent);
2993
2994 return ret;
2995}
2996EXPORT_SYMBOL_GPL(spi_flash_read);
2997
David Brownell7d077192009-06-17 16:26:03 -07002998/*-------------------------------------------------------------------------*/
2999
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003000/* Utility methods for SPI protocol drivers, layered on
David Brownell7d077192009-06-17 16:26:03 -07003001 * top of the core. Some other utility methods are defined as
3002 * inline functions.
3003 */
3004
Andrew Morton5d870c82006-01-11 11:23:49 -08003005static void spi_complete(void *arg)
3006{
3007 complete(arg);
3008}
3009
Mark Brownef4d96e2016-07-21 23:53:31 +01003010static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003011{
3012 DECLARE_COMPLETION_ONSTACK(done);
3013 int status;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003014 struct spi_controller *ctlr = spi->controller;
Mark Brown0461a412014-12-09 21:38:05 +00003015 unsigned long flags;
3016
3017 status = __spi_validate(spi, message);
3018 if (status != 0)
3019 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003020
3021 message->complete = spi_complete;
3022 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00003023 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003024
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003025 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003026 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3027
Mark Brown0461a412014-12-09 21:38:05 +00003028 /* If we're not using the legacy transfer method then we will
3029 * try to transfer in the calling context so special case.
3030 * This code would be less tricky if we could remove the
3031 * support for driver implemented message queues.
3032 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003033 if (ctlr->transfer == spi_queued_transfer) {
3034 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003035
3036 trace_spi_message_submit(message);
3037
3038 status = __spi_queued_transfer(spi, message, false);
3039
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003040 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003041 } else {
3042 status = spi_async_locked(spi, message);
3043 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003044
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003045 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00003046 /* Push out the messages in the calling context if we
3047 * can.
3048 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003049 if (ctlr->transfer == spi_queued_transfer) {
3050 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperleca2ebc2015-06-22 13:00:36 +00003051 spi_sync_immediate);
3052 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3053 spi_sync_immediate);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003054 __spi_pump_messages(ctlr, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003055 }
Mark Brown0461a412014-12-09 21:38:05 +00003056
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003057 wait_for_completion(&done);
3058 status = message->status;
3059 }
3060 message->context = NULL;
3061 return status;
3062}
3063
David Brownell8ae12a02006-01-08 13:34:19 -08003064/**
3065 * spi_sync - blocking/synchronous SPI data transfers
3066 * @spi: device with which data will be exchanged
3067 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07003068 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003069 *
3070 * This call may only be used from a context that may sleep. The sleep
3071 * is non-interruptible, and has no timeout. Low-overhead controller
3072 * drivers may DMA directly into and out of the message buffers.
3073 *
3074 * Note that the SPI device's chip select is active during the message,
3075 * and then is normally disabled between messages. Drivers for some
3076 * frequently-used devices may want to minimize costs of selecting a chip,
3077 * by leaving it selected in anticipation that the next message will go
3078 * to the same chip. (That may increase power usage.)
3079 *
David Brownell0c868462006-01-08 13:34:25 -08003080 * Also, the caller is guaranteeing that the memory associated with the
3081 * message will not be freed before this call returns.
3082 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003083 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003084 */
3085int spi_sync(struct spi_device *spi, struct spi_message *message)
3086{
Mark Brownef4d96e2016-07-21 23:53:31 +01003087 int ret;
3088
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003089 mutex_lock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003090 ret = __spi_sync(spi, message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003091 mutex_unlock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003092
3093 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08003094}
3095EXPORT_SYMBOL_GPL(spi_sync);
3096
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003097/**
3098 * spi_sync_locked - version of spi_sync with exclusive bus usage
3099 * @spi: device with which data will be exchanged
3100 * @message: describes the data transfers
3101 * Context: can sleep
3102 *
3103 * This call may only be used from a context that may sleep. The sleep
3104 * is non-interruptible, and has no timeout. Low-overhead controller
3105 * drivers may DMA directly into and out of the message buffers.
3106 *
3107 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003108 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003109 * be released by a spi_bus_unlock call when the exclusive access is over.
3110 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003111 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003112 */
3113int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3114{
Mark Brownef4d96e2016-07-21 23:53:31 +01003115 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003116}
3117EXPORT_SYMBOL_GPL(spi_sync_locked);
3118
3119/**
3120 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003121 * @ctlr: SPI bus master that should be locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003122 * Context: can sleep
3123 *
3124 * This call may only be used from a context that may sleep. The sleep
3125 * is non-interruptible, and has no timeout.
3126 *
3127 * This call should be used by drivers that require exclusive access to the
3128 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3129 * exclusive access is over. Data transfer must be done by spi_sync_locked
3130 * and spi_async_locked calls when the SPI bus lock is held.
3131 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003132 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003133 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003134int spi_bus_lock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003135{
3136 unsigned long flags;
3137
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003138 mutex_lock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003139
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003140 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3141 ctlr->bus_lock_flag = 1;
3142 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003143
3144 /* mutex remains locked until spi_bus_unlock is called */
3145
3146 return 0;
3147}
3148EXPORT_SYMBOL_GPL(spi_bus_lock);
3149
3150/**
3151 * spi_bus_unlock - release the lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003152 * @ctlr: SPI bus master that was locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003153 * Context: can sleep
3154 *
3155 * This call may only be used from a context that may sleep. The sleep
3156 * is non-interruptible, and has no timeout.
3157 *
3158 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3159 * call.
3160 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003161 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003162 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003163int spi_bus_unlock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003164{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003165 ctlr->bus_lock_flag = 0;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003166
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003167 mutex_unlock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003168
3169 return 0;
3170}
3171EXPORT_SYMBOL_GPL(spi_bus_unlock);
3172
David Brownella9948b62006-04-02 10:37:40 -08003173/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003174#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003175
3176static u8 *buf;
3177
3178/**
3179 * spi_write_then_read - SPI synchronous write followed by read
3180 * @spi: device with which data will be exchanged
3181 * @txbuf: data to be written (need not be dma-safe)
3182 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003183 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3184 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003185 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003186 *
3187 * This performs a half duplex MicroWire style transaction with the
3188 * device, sending txbuf and then reading rxbuf. The return value
3189 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003190 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003191 *
David Brownell0c868462006-01-08 13:34:25 -08003192 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003193 * portable code should never use this for more than 32 bytes.
3194 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c868462006-01-08 13:34:25 -08003195 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003196 *
3197 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003198 */
3199int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003200 const void *txbuf, unsigned n_tx,
3201 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003202{
David Brownell068f4072007-12-04 23:45:09 -08003203 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003204
3205 int status;
3206 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003207 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003208 u8 *local_buf;
3209
Mark Brownb3a223e2012-12-02 12:54:25 +09003210 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3211 * copying here, (as a pure convenience thing), but we can
3212 * keep heap costs out of the hot path unless someone else is
3213 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003214 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003215 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003216 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3217 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003218 if (!local_buf)
3219 return -ENOMEM;
3220 } else {
3221 local_buf = buf;
3222 }
David Brownell8ae12a02006-01-08 13:34:19 -08003223
Vitaly Wool8275c642006-01-08 13:34:28 -08003224 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003225 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003226 if (n_tx) {
3227 x[0].len = n_tx;
3228 spi_message_add_tail(&x[0], &message);
3229 }
3230 if (n_rx) {
3231 x[1].len = n_rx;
3232 spi_message_add_tail(&x[1], &message);
3233 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003234
David Brownell8ae12a02006-01-08 13:34:19 -08003235 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003236 x[0].tx_buf = local_buf;
3237 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003238
3239 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003240 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003241 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003242 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003243
David Brownellbdff5492009-04-13 14:39:57 -07003244 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003245 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003246 else
3247 kfree(local_buf);
3248
3249 return status;
3250}
3251EXPORT_SYMBOL_GPL(spi_write_then_read);
3252
3253/*-------------------------------------------------------------------------*/
3254
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003255#if IS_ENABLED(CONFIG_OF_DYNAMIC)
3256static int __spi_of_device_match(struct device *dev, void *data)
3257{
3258 return dev->of_node == data;
3259}
3260
3261/* must call put_device() when done with returned spi_device device */
3262static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
3263{
3264 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3265 __spi_of_device_match);
3266 return dev ? to_spi_device(dev) : NULL;
3267}
3268
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003269static int __spi_of_controller_match(struct device *dev, const void *data)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003270{
3271 return dev->of_node == data;
3272}
3273
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003274/* the spi controllers are not using spi_bus, so we find it with another way */
3275static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003276{
3277 struct device *dev;
3278
3279 dev = class_find_device(&spi_master_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003280 __spi_of_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003281 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3282 dev = class_find_device(&spi_slave_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003283 __spi_of_controller_match);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003284 if (!dev)
3285 return NULL;
3286
3287 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003288 return container_of(dev, struct spi_controller, dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003289}
3290
3291static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3292 void *arg)
3293{
3294 struct of_reconfig_data *rd = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003295 struct spi_controller *ctlr;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003296 struct spi_device *spi;
3297
3298 switch (of_reconfig_get_state_change(action, arg)) {
3299 case OF_RECONFIG_CHANGE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003300 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3301 if (ctlr == NULL)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003302 return NOTIFY_OK; /* not for us */
3303
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003304 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003305 put_device(&ctlr->dev);
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003306 return NOTIFY_OK;
3307 }
3308
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003309 spi = of_register_spi_device(ctlr, rd->dn);
3310 put_device(&ctlr->dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003311
3312 if (IS_ERR(spi)) {
3313 pr_err("%s: failed to create for '%s'\n",
3314 __func__, rd->dn->full_name);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003315 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003316 return notifier_from_errno(PTR_ERR(spi));
3317 }
3318 break;
3319
3320 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003321 /* already depopulated? */
3322 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3323 return NOTIFY_OK;
3324
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003325 /* find our device by node */
3326 spi = of_find_spi_device_by_node(rd->dn);
3327 if (spi == NULL)
3328 return NOTIFY_OK; /* no? not meant for us */
3329
3330 /* unregister takes one ref away */
3331 spi_unregister_device(spi);
3332
3333 /* and put the reference of the find */
3334 put_device(&spi->dev);
3335 break;
3336 }
3337
3338 return NOTIFY_OK;
3339}
3340
3341static struct notifier_block spi_of_notifier = {
3342 .notifier_call = of_spi_notify,
3343};
3344#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3345extern struct notifier_block spi_of_notifier;
3346#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3347
Octavian Purdila7f244672016-07-08 19:13:11 +03003348#if IS_ENABLED(CONFIG_ACPI)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003349static int spi_acpi_controller_match(struct device *dev, const void *data)
Octavian Purdila7f244672016-07-08 19:13:11 +03003350{
3351 return ACPI_COMPANION(dev->parent) == data;
3352}
3353
3354static int spi_acpi_device_match(struct device *dev, void *data)
3355{
3356 return ACPI_COMPANION(dev) == data;
3357}
3358
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003359static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
Octavian Purdila7f244672016-07-08 19:13:11 +03003360{
3361 struct device *dev;
3362
3363 dev = class_find_device(&spi_master_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003364 spi_acpi_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003365 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3366 dev = class_find_device(&spi_slave_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003367 spi_acpi_controller_match);
Octavian Purdila7f244672016-07-08 19:13:11 +03003368 if (!dev)
3369 return NULL;
3370
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003371 return container_of(dev, struct spi_controller, dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003372}
3373
3374static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3375{
3376 struct device *dev;
3377
3378 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3379
3380 return dev ? to_spi_device(dev) : NULL;
3381}
3382
3383static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3384 void *arg)
3385{
3386 struct acpi_device *adev = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003387 struct spi_controller *ctlr;
Octavian Purdila7f244672016-07-08 19:13:11 +03003388 struct spi_device *spi;
3389
3390 switch (value) {
3391 case ACPI_RECONFIG_DEVICE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003392 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
3393 if (!ctlr)
Octavian Purdila7f244672016-07-08 19:13:11 +03003394 break;
3395
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003396 acpi_register_spi_device(ctlr, adev);
3397 put_device(&ctlr->dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003398 break;
3399 case ACPI_RECONFIG_DEVICE_REMOVE:
3400 if (!acpi_device_enumerated(adev))
3401 break;
3402
3403 spi = acpi_spi_find_device_by_adev(adev);
3404 if (!spi)
3405 break;
3406
3407 spi_unregister_device(spi);
3408 put_device(&spi->dev);
3409 break;
3410 }
3411
3412 return NOTIFY_OK;
3413}
3414
3415static struct notifier_block spi_acpi_notifier = {
3416 .notifier_call = acpi_spi_notify,
3417};
3418#else
3419extern struct notifier_block spi_acpi_notifier;
3420#endif
3421
David Brownell8ae12a02006-01-08 13:34:19 -08003422static int __init spi_init(void)
3423{
David Brownellb8852442006-01-08 13:34:23 -08003424 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08003425
Christoph Lametere94b1762006-12-06 20:33:17 -08003426 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08003427 if (!buf) {
3428 status = -ENOMEM;
3429 goto err0;
3430 }
3431
3432 status = bus_register(&spi_bus_type);
3433 if (status < 0)
3434 goto err1;
3435
3436 status = class_register(&spi_master_class);
3437 if (status < 0)
3438 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003439
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003440 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
3441 status = class_register(&spi_slave_class);
3442 if (status < 0)
3443 goto err3;
3444 }
3445
Fabio Estevam52677202014-11-26 20:13:57 -02003446 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003447 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03003448 if (IS_ENABLED(CONFIG_ACPI))
3449 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003450
David Brownell8ae12a02006-01-08 13:34:19 -08003451 return 0;
David Brownellb8852442006-01-08 13:34:23 -08003452
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003453err3:
3454 class_unregister(&spi_master_class);
David Brownellb8852442006-01-08 13:34:23 -08003455err2:
3456 bus_unregister(&spi_bus_type);
3457err1:
3458 kfree(buf);
3459 buf = NULL;
3460err0:
3461 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08003462}
David Brownellb8852442006-01-08 13:34:23 -08003463
David Brownell8ae12a02006-01-08 13:34:19 -08003464/* board_info is normally registered in arch_initcall(),
3465 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08003466 *
3467 * REVISIT only boardinfo really needs static linking. the rest (device and
3468 * driver registration) _could_ be dynamically linked (modular) ... costs
3469 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08003470 */
David Brownell673c0c02008-10-15 22:02:46 -07003471postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08003472