blob: 294d0038eea6d0e21dbb000a6c0aa0c9826c7df0 [file] [log] [blame]
Marco Felschb445bfc2018-09-25 11:42:28 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Brown787f4882018-11-29 16:24:37 +00002// SPI init/core code
3//
4// Copyright (C) 2005 David Brownell
5// Copyright (C) 2008 Secret Lab Technologies Ltd.
David Brownell8ae12a02006-01-08 13:34:19 -08006
David Brownell8ae12a02006-01-08 13:34:19 -08007#include <linux/kernel.h>
8#include <linux/device.h>
9#include <linux/init.h>
10#include <linux/cache.h>
Mark Brown99adef32014-01-16 12:22:43 +000011#include <linux/dma-mapping.h>
12#include <linux/dmaengine.h>
Matthias Kaehlcke94040822007-07-17 04:04:16 -070013#include <linux/mutex.h>
Sinan Akman2b7a32f2010-10-02 21:28:29 -060014#include <linux/of_device.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060015#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020016#include <linux/clk/clk-conf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Anton Vorontsove0626e32009-09-22 16:46:08 -070018#include <linux/mod_devicetable.h>
David Brownell8ae12a02006-01-08 13:34:19 -080019#include <linux/spi/spi.h>
Boris Brezillonb5932f52018-04-26 18:18:15 +020020#include <linux/spi/spi-mem.h>
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +010021#include <linux/of_gpio.h>
Linus Walleijf3186dd2019-01-07 16:51:50 +010022#include <linux/gpio/consumer.h>
Mark Brown3ae22e82010-12-25 15:32:27 +010023#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020024#include <linux/pm_domain.h>
Dmitry Torokhov826cf172017-02-28 14:25:18 -080025#include <linux/property.h>
Paul Gortmaker025ed132011-07-10 12:57:55 -040026#include <linux/export.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060027#include <linux/sched/rt.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010028#include <uapi/linux/sched/types.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010029#include <linux/delay.h>
30#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010031#include <linux/ioport.h>
32#include <linux/acpi.h>
Vignesh Rb1b81532016-08-17 15:22:36 +053033#include <linux/highmem.h>
Suniel Mahesh9b61e302017-08-03 10:05:57 +053034#include <linux/idr.h>
Lukas Wunner8a2e4872017-08-01 14:10:41 +020035#include <linux/platform_data/x86/apple.h>
David Brownell8ae12a02006-01-08 13:34:19 -080036
Mark Brown56ec1972013-10-07 19:33:53 +010037#define CREATE_TRACE_POINTS
38#include <trace/events/spi.h>
Arnd Bergmannca1438d2019-03-21 13:42:25 +010039EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);
40EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
Suniel Mahesh9b61e302017-08-03 10:05:57 +053041
Boris Brezillon46336962018-04-22 20:35:14 +020042#include "internals.h"
43
Suniel Mahesh9b61e302017-08-03 10:05:57 +053044static DEFINE_IDR(spi_master_idr);
Mark Brown56ec1972013-10-07 19:33:53 +010045
David Brownell8ae12a02006-01-08 13:34:19 -080046static void spidev_release(struct device *dev)
47{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080048 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080049
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020050 /* spi controllers may cleanup for released devices */
51 if (spi->controller->cleanup)
52 spi->controller->cleanup(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080053
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020054 spi_controller_put(spi->controller);
Trent Piepho50395632018-09-20 19:18:32 +000055 kfree(spi->driver_override);
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
Trent Piepho50395632018-09-20 19:18:32 +000073static ssize_t driver_override_store(struct device *dev,
74 struct device_attribute *a,
75 const char *buf, size_t count)
76{
77 struct spi_device *spi = to_spi_device(dev);
78 const char *end = memchr(buf, '\n', count);
79 const size_t len = end ? end - buf : count;
80 const char *driver_override, *old;
81
82 /* We need to keep extra room for a newline when displaying value */
83 if (len >= (PAGE_SIZE - 1))
84 return -EINVAL;
85
86 driver_override = kstrndup(buf, len, GFP_KERNEL);
87 if (!driver_override)
88 return -ENOMEM;
89
90 device_lock(dev);
91 old = spi->driver_override;
92 if (len) {
93 spi->driver_override = driver_override;
94 } else {
Andy Shevchenkobe73e322019-10-23 15:16:43 +030095 /* Empty string, disable driver override */
Trent Piepho50395632018-09-20 19:18:32 +000096 spi->driver_override = NULL;
97 kfree(driver_override);
98 }
99 device_unlock(dev);
100 kfree(old);
101
102 return count;
103}
104
105static ssize_t driver_override_show(struct device *dev,
106 struct device_attribute *a, char *buf)
107{
108 const struct spi_device *spi = to_spi_device(dev);
109 ssize_t len;
110
111 device_lock(dev);
112 len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : "");
113 device_unlock(dev);
114 return len;
115}
116static DEVICE_ATTR_RW(driver_override);
117
Martin Sperleca2ebc2015-06-22 13:00:36 +0000118#define SPI_STATISTICS_ATTRS(field, file) \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200119static ssize_t spi_controller_##field##_show(struct device *dev, \
120 struct device_attribute *attr, \
121 char *buf) \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000122{ \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200123 struct spi_controller *ctlr = container_of(dev, \
124 struct spi_controller, dev); \
125 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000126} \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200127static struct device_attribute dev_attr_spi_controller_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +0200128 .attr = { .name = file, .mode = 0444 }, \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200129 .show = spi_controller_##field##_show, \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000130}; \
131static ssize_t spi_device_##field##_show(struct device *dev, \
132 struct device_attribute *attr, \
133 char *buf) \
134{ \
Geliang Tangd1eba932015-12-23 00:18:41 +0800135 struct spi_device *spi = to_spi_device(dev); \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000136 return spi_statistics_##field##_show(&spi->statistics, buf); \
137} \
138static struct device_attribute dev_attr_spi_device_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +0200139 .attr = { .name = file, .mode = 0444 }, \
Martin Sperleca2ebc2015-06-22 13:00:36 +0000140 .show = spi_device_##field##_show, \
141}
142
143#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
144static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
145 char *buf) \
146{ \
147 unsigned long flags; \
148 ssize_t len; \
149 spin_lock_irqsave(&stat->lock, flags); \
150 len = sprintf(buf, format_string, stat->field); \
151 spin_unlock_irqrestore(&stat->lock, flags); \
152 return len; \
153} \
154SPI_STATISTICS_ATTRS(name, file)
155
156#define SPI_STATISTICS_SHOW(field, format_string) \
157 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
158 field, format_string)
159
160SPI_STATISTICS_SHOW(messages, "%lu");
161SPI_STATISTICS_SHOW(transfers, "%lu");
162SPI_STATISTICS_SHOW(errors, "%lu");
163SPI_STATISTICS_SHOW(timedout, "%lu");
164
165SPI_STATISTICS_SHOW(spi_sync, "%lu");
166SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
167SPI_STATISTICS_SHOW(spi_async, "%lu");
168
169SPI_STATISTICS_SHOW(bytes, "%llu");
170SPI_STATISTICS_SHOW(bytes_rx, "%llu");
171SPI_STATISTICS_SHOW(bytes_tx, "%llu");
172
Martin Sperl6b7bc062015-06-22 13:02:04 +0000173#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
174 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
175 "transfer_bytes_histo_" number, \
176 transfer_bytes_histo[index], "%lu")
177SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
178SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
179SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
180SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
181SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
182SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
183SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
184SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
185SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
186SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
187SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
188SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
189SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
190SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
191SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
192SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
193SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
194
Martin Sperld9f12122015-12-14 15:20:20 +0000195SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
196
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700197static struct attribute *spi_dev_attrs[] = {
198 &dev_attr_modalias.attr,
Trent Piepho50395632018-09-20 19:18:32 +0000199 &dev_attr_driver_override.attr,
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700200 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800201};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000202
203static const struct attribute_group spi_dev_group = {
204 .attrs = spi_dev_attrs,
205};
206
207static struct attribute *spi_device_statistics_attrs[] = {
208 &dev_attr_spi_device_messages.attr,
209 &dev_attr_spi_device_transfers.attr,
210 &dev_attr_spi_device_errors.attr,
211 &dev_attr_spi_device_timedout.attr,
212 &dev_attr_spi_device_spi_sync.attr,
213 &dev_attr_spi_device_spi_sync_immediate.attr,
214 &dev_attr_spi_device_spi_async.attr,
215 &dev_attr_spi_device_bytes.attr,
216 &dev_attr_spi_device_bytes_rx.attr,
217 &dev_attr_spi_device_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000218 &dev_attr_spi_device_transfer_bytes_histo0.attr,
219 &dev_attr_spi_device_transfer_bytes_histo1.attr,
220 &dev_attr_spi_device_transfer_bytes_histo2.attr,
221 &dev_attr_spi_device_transfer_bytes_histo3.attr,
222 &dev_attr_spi_device_transfer_bytes_histo4.attr,
223 &dev_attr_spi_device_transfer_bytes_histo5.attr,
224 &dev_attr_spi_device_transfer_bytes_histo6.attr,
225 &dev_attr_spi_device_transfer_bytes_histo7.attr,
226 &dev_attr_spi_device_transfer_bytes_histo8.attr,
227 &dev_attr_spi_device_transfer_bytes_histo9.attr,
228 &dev_attr_spi_device_transfer_bytes_histo10.attr,
229 &dev_attr_spi_device_transfer_bytes_histo11.attr,
230 &dev_attr_spi_device_transfer_bytes_histo12.attr,
231 &dev_attr_spi_device_transfer_bytes_histo13.attr,
232 &dev_attr_spi_device_transfer_bytes_histo14.attr,
233 &dev_attr_spi_device_transfer_bytes_histo15.attr,
234 &dev_attr_spi_device_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000235 &dev_attr_spi_device_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000236 NULL,
237};
238
239static const struct attribute_group spi_device_statistics_group = {
240 .name = "statistics",
241 .attrs = spi_device_statistics_attrs,
242};
243
244static const struct attribute_group *spi_dev_groups[] = {
245 &spi_dev_group,
246 &spi_device_statistics_group,
247 NULL,
248};
249
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200250static struct attribute *spi_controller_statistics_attrs[] = {
251 &dev_attr_spi_controller_messages.attr,
252 &dev_attr_spi_controller_transfers.attr,
253 &dev_attr_spi_controller_errors.attr,
254 &dev_attr_spi_controller_timedout.attr,
255 &dev_attr_spi_controller_spi_sync.attr,
256 &dev_attr_spi_controller_spi_sync_immediate.attr,
257 &dev_attr_spi_controller_spi_async.attr,
258 &dev_attr_spi_controller_bytes.attr,
259 &dev_attr_spi_controller_bytes_rx.attr,
260 &dev_attr_spi_controller_bytes_tx.attr,
261 &dev_attr_spi_controller_transfer_bytes_histo0.attr,
262 &dev_attr_spi_controller_transfer_bytes_histo1.attr,
263 &dev_attr_spi_controller_transfer_bytes_histo2.attr,
264 &dev_attr_spi_controller_transfer_bytes_histo3.attr,
265 &dev_attr_spi_controller_transfer_bytes_histo4.attr,
266 &dev_attr_spi_controller_transfer_bytes_histo5.attr,
267 &dev_attr_spi_controller_transfer_bytes_histo6.attr,
268 &dev_attr_spi_controller_transfer_bytes_histo7.attr,
269 &dev_attr_spi_controller_transfer_bytes_histo8.attr,
270 &dev_attr_spi_controller_transfer_bytes_histo9.attr,
271 &dev_attr_spi_controller_transfer_bytes_histo10.attr,
272 &dev_attr_spi_controller_transfer_bytes_histo11.attr,
273 &dev_attr_spi_controller_transfer_bytes_histo12.attr,
274 &dev_attr_spi_controller_transfer_bytes_histo13.attr,
275 &dev_attr_spi_controller_transfer_bytes_histo14.attr,
276 &dev_attr_spi_controller_transfer_bytes_histo15.attr,
277 &dev_attr_spi_controller_transfer_bytes_histo16.attr,
278 &dev_attr_spi_controller_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000279 NULL,
280};
281
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200282static const struct attribute_group spi_controller_statistics_group = {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000283 .name = "statistics",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200284 .attrs = spi_controller_statistics_attrs,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000285};
286
287static const struct attribute_group *spi_master_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200288 &spi_controller_statistics_group,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000289 NULL,
290};
291
292void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
293 struct spi_transfer *xfer,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200294 struct spi_controller *ctlr)
Martin Sperleca2ebc2015-06-22 13:00:36 +0000295{
296 unsigned long flags;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000297 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
298
299 if (l2len < 0)
300 l2len = 0;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000301
302 spin_lock_irqsave(&stats->lock, flags);
303
304 stats->transfers++;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000305 stats->transfer_bytes_histo[l2len]++;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000306
307 stats->bytes += xfer->len;
308 if ((xfer->tx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200309 (xfer->tx_buf != ctlr->dummy_tx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000310 stats->bytes_tx += xfer->len;
311 if ((xfer->rx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200312 (xfer->rx_buf != ctlr->dummy_rx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000313 stats->bytes_rx += xfer->len;
314
315 spin_unlock_irqrestore(&stats->lock, flags);
316}
317EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800318
319/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
320 * and the sysfs version makes coldplug work too.
321 */
322
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700323static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
324 const struct spi_device *sdev)
325{
326 while (id->name[0]) {
327 if (!strcmp(sdev->modalias, id->name))
328 return id;
329 id++;
330 }
331 return NULL;
332}
333
334const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
335{
336 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
337
338 return spi_match_id(sdrv->id_table, sdev);
339}
340EXPORT_SYMBOL_GPL(spi_get_device_id);
341
David Brownell8ae12a02006-01-08 13:34:19 -0800342static int spi_match_device(struct device *dev, struct device_driver *drv)
343{
344 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700345 const struct spi_driver *sdrv = to_spi_driver(drv);
346
Trent Piepho50395632018-09-20 19:18:32 +0000347 /* Check override first, and if set, only use the named driver */
348 if (spi->driver_override)
349 return strcmp(spi->driver_override, drv->name) == 0;
350
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600351 /* Attempt an OF style match */
352 if (of_driver_match_device(dev, drv))
353 return 1;
354
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100355 /* Then try ACPI */
356 if (acpi_driver_match_device(dev, drv))
357 return 1;
358
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700359 if (sdrv->id_table)
360 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800361
Kay Sievers35f74fc2009-01-06 10:44:37 -0800362 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800363}
364
Kay Sievers7eff2e72007-08-14 15:15:12 +0200365static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800366{
367 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800368 int rc;
369
370 rc = acpi_device_uevent_modalias(dev, env);
371 if (rc != -ENODEV)
372 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800373
Andy Shevchenko28566702017-07-26 16:14:00 +0300374 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800375}
376
David Brownell8ae12a02006-01-08 13:34:19 -0800377struct bus_type spi_bus_type = {
378 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700379 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800380 .match = spi_match_device,
381 .uevent = spi_uevent,
David Brownell8ae12a02006-01-08 13:34:19 -0800382};
383EXPORT_SYMBOL_GPL(spi_bus_type);
384
David Brownellb8852442006-01-08 13:34:23 -0800385
386static int spi_drv_probe(struct device *dev)
387{
388 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Jon Hunter44af7922015-10-09 15:45:55 +0100389 struct spi_device *spi = to_spi_device(dev);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300390 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800391
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200392 ret = of_clk_set_defaults(dev->of_node, false);
393 if (ret)
394 return ret;
395
Jon Hunter44af7922015-10-09 15:45:55 +0100396 if (dev->of_node) {
397 spi->irq = of_irq_get(dev->of_node, 0);
398 if (spi->irq == -EPROBE_DEFER)
399 return -EPROBE_DEFER;
400 if (spi->irq < 0)
401 spi->irq = 0;
402 }
403
Ulf Hansson676e7c22014-09-19 20:27:41 +0200404 ret = dev_pm_domain_attach(dev, true);
Ulf Hansson71f277a2018-04-26 10:53:10 +0200405 if (ret)
406 return ret;
407
408 ret = sdrv->probe(spi);
409 if (ret)
410 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300411
412 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800413}
414
415static int spi_drv_remove(struct device *dev)
416{
417 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300418 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800419
Jean Delvareaec35f42014-02-13 15:28:41 +0100420 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200421 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300422
423 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800424}
425
426static void spi_drv_shutdown(struct device *dev)
427{
428 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
429
430 sdrv->shutdown(to_spi_device(dev));
431}
432
David Brownell33e34dc2007-05-08 00:32:21 -0700433/**
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500434 * __spi_register_driver - register a SPI driver
Thierry Reding88c93212015-11-10 13:03:04 +0100435 * @owner: owner module of the driver to register
David Brownell33e34dc2007-05-08 00:32:21 -0700436 * @sdrv: the driver to register
437 * Context: can sleep
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200438 *
439 * Return: zero on success, else a negative error code.
David Brownell33e34dc2007-05-08 00:32:21 -0700440 */
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500441int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
David Brownellb8852442006-01-08 13:34:23 -0800442{
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500443 sdrv->driver.owner = owner;
David Brownellb8852442006-01-08 13:34:23 -0800444 sdrv->driver.bus = &spi_bus_type;
445 if (sdrv->probe)
446 sdrv->driver.probe = spi_drv_probe;
447 if (sdrv->remove)
448 sdrv->driver.remove = spi_drv_remove;
449 if (sdrv->shutdown)
450 sdrv->driver.shutdown = spi_drv_shutdown;
451 return driver_register(&sdrv->driver);
452}
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500453EXPORT_SYMBOL_GPL(__spi_register_driver);
David Brownellb8852442006-01-08 13:34:23 -0800454
David Brownell8ae12a02006-01-08 13:34:19 -0800455/*-------------------------------------------------------------------------*/
456
457/* SPI devices should normally not be created by SPI device drivers; that
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200458 * would make them board-specific. Similarly with SPI controller drivers.
David Brownell8ae12a02006-01-08 13:34:19 -0800459 * Device registration normally goes into like arch/.../mach.../board-YYY.c
460 * with other readonly (flashable) information about mainboard devices.
461 */
462
463struct boardinfo {
464 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800465 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800466};
467
468static LIST_HEAD(board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200469static LIST_HEAD(spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800470
471/*
Andy Shevchenkobe73e322019-10-23 15:16:43 +0300472 * Used to protect add/del operation for board_info list and
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200473 * spi_controller list, and their matching process
Suniel Mahesh9a9a0472017-08-17 18:18:22 +0530474 * also used to protect object of type struct idr
Feng Tang2b9603a2010-08-02 15:52:15 +0800475 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700476static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800477
Grant Likelydc87c982008-05-15 16:50:22 -0600478/**
479 * spi_alloc_device - Allocate a new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200480 * @ctlr: Controller to which device is connected
Grant Likelydc87c982008-05-15 16:50:22 -0600481 * Context: can sleep
482 *
483 * Allows a driver to allocate and initialize a spi_device without
484 * registering it immediately. This allows a driver to directly
485 * fill the spi_device with device parameters before calling
486 * spi_add_device() on it.
487 *
488 * Caller is responsible to call spi_add_device() on the returned
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200489 * spi_device structure to add it to the SPI controller. If the caller
Grant Likelydc87c982008-05-15 16:50:22 -0600490 * needs to discard the spi_device without adding it, then it should
491 * call spi_dev_put() on it.
492 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200493 * Return: a pointer to the new device, or NULL.
Grant Likelydc87c982008-05-15 16:50:22 -0600494 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200495struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
Grant Likelydc87c982008-05-15 16:50:22 -0600496{
497 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600498
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200499 if (!spi_controller_get(ctlr))
Grant Likelydc87c982008-05-15 16:50:22 -0600500 return NULL;
501
Jingoo Han5fe5f052013-10-14 10:31:51 +0900502 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600503 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200504 spi_controller_put(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600505 return NULL;
506 }
507
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200508 spi->master = spi->controller = ctlr;
509 spi->dev.parent = &ctlr->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600510 spi->dev.bus = &spi_bus_type;
511 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100512 spi->cs_gpio = -ENOENT;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000513
514 spin_lock_init(&spi->statistics.lock);
515
Grant Likelydc87c982008-05-15 16:50:22 -0600516 device_initialize(&spi->dev);
517 return spi;
518}
519EXPORT_SYMBOL_GPL(spi_alloc_device);
520
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200521static void spi_dev_set_name(struct spi_device *spi)
522{
523 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
524
525 if (adev) {
526 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
527 return;
528 }
529
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200530 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200531 spi->chip_select);
532}
533
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200534static int spi_dev_check(struct device *dev, void *data)
535{
536 struct spi_device *spi = to_spi_device(dev);
537 struct spi_device *new_spi = data;
538
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200539 if (spi->controller == new_spi->controller &&
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200540 spi->chip_select == new_spi->chip_select)
541 return -EBUSY;
542 return 0;
543}
544
Grant Likelydc87c982008-05-15 16:50:22 -0600545/**
546 * spi_add_device - Add spi_device allocated with spi_alloc_device
547 * @spi: spi_device to register
548 *
549 * Companion function to spi_alloc_device. Devices allocated with
550 * spi_alloc_device can be added onto the spi bus with this function.
551 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200552 * Return: 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600553 */
554int spi_add_device(struct spi_device *spi)
555{
David Brownelle48880e2008-08-15 00:40:44 -0700556 static DEFINE_MUTEX(spi_add_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200557 struct spi_controller *ctlr = spi->controller;
558 struct device *dev = ctlr->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600559 int status;
560
561 /* Chipselects are numbered 0..max; validate. */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200562 if (spi->chip_select >= ctlr->num_chipselect) {
563 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
564 ctlr->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600565 return -EINVAL;
566 }
567
568 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200569 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700570
571 /* We need to make sure there's no other device with this
572 * chipselect **BEFORE** we call setup(), else we'll trash
573 * its configuration. Lock against concurrent add() calls.
574 */
575 mutex_lock(&spi_add_lock);
576
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200577 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
578 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700579 dev_err(dev, "chipselect %d already in use\n",
580 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700581 goto done;
582 }
583
Linus Walleijf3186dd2019-01-07 16:51:50 +0100584 /* Descriptors take precedence */
585 if (ctlr->cs_gpiods)
586 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
587 else if (ctlr->cs_gpios)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200588 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100589
David Brownelle48880e2008-08-15 00:40:44 -0700590 /* Drivers may modify this initial i/o setup, but will
591 * normally rely on the device being setup. Devices
592 * using SPI_CS_HIGH can't coexist well otherwise...
593 */
David Brownell7d077192009-06-17 16:26:03 -0700594 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600595 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200596 dev_err(dev, "can't setup %s, status %d\n",
597 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700598 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600599 }
600
David Brownelle48880e2008-08-15 00:40:44 -0700601 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600602 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700603 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200604 dev_err(dev, "can't add %s, status %d\n",
605 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700606 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800607 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600608
David Brownelle48880e2008-08-15 00:40:44 -0700609done:
610 mutex_unlock(&spi_add_lock);
611 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600612}
613EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800614
David Brownell33e34dc2007-05-08 00:32:21 -0700615/**
616 * spi_new_device - instantiate one new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200617 * @ctlr: Controller to which device is connected
David Brownell33e34dc2007-05-08 00:32:21 -0700618 * @chip: Describes the SPI device
619 * Context: can sleep
620 *
621 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800622 * after board init creates the hard-wired devices. Some development
623 * platforms may not be able to use spi_register_board_info though, and
624 * this is exported so that for example a USB or parport based adapter
625 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700626 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200627 * Return: the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800628 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200629struct spi_device *spi_new_device(struct spi_controller *ctlr,
Adrian Bunke9d5a462007-03-26 21:32:23 -0800630 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800631{
632 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800633 int status;
634
David Brownell082c8cb2007-07-31 00:39:45 -0700635 /* NOTE: caller did any chip->bus_num checks necessary.
636 *
637 * Also, unless we change the return value convention to use
638 * error-or-pointer (not NULL-or-pointer), troubleshootability
639 * suggests syslogged diagnostics are best here (ugh).
640 */
641
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200642 proxy = spi_alloc_device(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600643 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800644 return NULL;
645
Grant Likely102eb972008-07-23 21:29:55 -0700646 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
647
David Brownell8ae12a02006-01-08 13:34:19 -0800648 proxy->chip_select = chip->chip_select;
649 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700650 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800651 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700652 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800653 proxy->dev.platform_data = (void *) chip->platform_data;
654 proxy->controller_data = chip->controller_data;
655 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800656
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800657 if (chip->properties) {
658 status = device_add_properties(&proxy->dev, chip->properties);
659 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200660 dev_err(&ctlr->dev,
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800661 "failed to add properties to '%s': %d\n",
662 chip->modalias, status);
663 goto err_dev_put;
664 }
David Brownell8ae12a02006-01-08 13:34:19 -0800665 }
666
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800667 status = spi_add_device(proxy);
668 if (status < 0)
669 goto err_remove_props;
670
David Brownell8ae12a02006-01-08 13:34:19 -0800671 return proxy;
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800672
673err_remove_props:
674 if (chip->properties)
675 device_remove_properties(&proxy->dev);
676err_dev_put:
677 spi_dev_put(proxy);
678 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800679}
680EXPORT_SYMBOL_GPL(spi_new_device);
681
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100682/**
683 * spi_unregister_device - unregister a single SPI device
684 * @spi: spi_device to unregister
685 *
686 * Start making the passed SPI device vanish. Normally this would be handled
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200687 * by spi_unregister_controller().
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100688 */
689void spi_unregister_device(struct spi_device *spi)
690{
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100691 if (!spi)
692 return;
693
Johan Hovold83241472017-01-30 17:47:05 +0100694 if (spi->dev.of_node) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100695 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
Johan Hovold83241472017-01-30 17:47:05 +0100696 of_node_put(spi->dev.of_node);
697 }
Octavian Purdila7f244672016-07-08 19:13:11 +0300698 if (ACPI_COMPANION(&spi->dev))
699 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100700 device_unregister(&spi->dev);
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100701}
702EXPORT_SYMBOL_GPL(spi_unregister_device);
703
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200704static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
705 struct spi_board_info *bi)
Feng Tang2b9603a2010-08-02 15:52:15 +0800706{
707 struct spi_device *dev;
708
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200709 if (ctlr->bus_num != bi->bus_num)
Feng Tang2b9603a2010-08-02 15:52:15 +0800710 return;
711
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200712 dev = spi_new_device(ctlr, bi);
Feng Tang2b9603a2010-08-02 15:52:15 +0800713 if (!dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200714 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
Feng Tang2b9603a2010-08-02 15:52:15 +0800715 bi->modalias);
716}
717
David Brownell33e34dc2007-05-08 00:32:21 -0700718/**
719 * spi_register_board_info - register SPI devices for a given board
720 * @info: array of chip descriptors
721 * @n: how many descriptors are provided
722 * Context: can sleep
723 *
David Brownell8ae12a02006-01-08 13:34:19 -0800724 * Board-specific early init code calls this (probably during arch_initcall)
725 * with segments of the SPI device table. Any device nodes are created later,
726 * after the relevant parent SPI controller (bus_num) is defined. We keep
727 * this table of devices forever, so that reloading a controller driver will
728 * not make Linux forget about these hard-wired devices.
729 *
730 * Other code can also call this, e.g. a particular add-on board might provide
731 * SPI devices through its expansion connector, so code initializing that board
732 * would naturally declare its SPI devices.
733 *
734 * The board info passed can safely be __initdata ... but be careful of
735 * any embedded pointers (platform_data, etc), they're copied as-is.
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800736 * Device properties are deep-copied though.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200737 *
738 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -0800739 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000740int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800741{
Feng Tang2b9603a2010-08-02 15:52:15 +0800742 struct boardinfo *bi;
743 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800744
Xiubo Lic7908a32014-09-24 14:30:29 +0800745 if (!n)
Dmitry Torokhovf974cf52017-02-28 14:25:19 -0800746 return 0;
Xiubo Lic7908a32014-09-24 14:30:29 +0800747
Markus Elfringf9bdb7f2017-01-13 12:28:04 +0100748 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800749 if (!bi)
750 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800751
Feng Tang2b9603a2010-08-02 15:52:15 +0800752 for (i = 0; i < n; i++, bi++, info++) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200753 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -0800754
Feng Tang2b9603a2010-08-02 15:52:15 +0800755 memcpy(&bi->board_info, info, sizeof(*info));
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800756 if (info->properties) {
757 bi->board_info.properties =
758 property_entries_dup(info->properties);
759 if (IS_ERR(bi->board_info.properties))
760 return PTR_ERR(bi->board_info.properties);
761 }
762
Feng Tang2b9603a2010-08-02 15:52:15 +0800763 mutex_lock(&board_lock);
764 list_add_tail(&bi->list, &board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200765 list_for_each_entry(ctlr, &spi_controller_list, list)
766 spi_match_controller_to_boardinfo(ctlr,
767 &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +0800768 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800769 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800770
771 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800772}
773
774/*-------------------------------------------------------------------------*/
775
Mark Brownb1589352013-10-05 11:50:40 +0100776static void spi_set_cs(struct spi_device *spi, bool enable)
777{
Alexandru Ardelean25093bd2019-09-26 13:51:43 +0300778 bool enable1 = enable;
779
780 if (!spi->controller->set_cs_timing) {
781 if (enable1)
782 spi_delay_exec(&spi->controller->cs_setup, NULL);
783 else
784 spi_delay_exec(&spi->controller->cs_hold, NULL);
785 }
786
Mark Brownb1589352013-10-05 11:50:40 +0100787 if (spi->mode & SPI_CS_HIGH)
788 enable = !enable;
789
Linus Walleijf3186dd2019-01-07 16:51:50 +0100790 if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
791 /*
792 * Honour the SPI_NO_CS flag and invert the enable line, as
793 * active low is default for SPI. Execution paths that handle
794 * polarity inversion in gpiolib (such as device tree) will
795 * enforce active high using the SPI_CS_HIGH resulting in a
796 * double inversion through the code above.
797 */
798 if (!(spi->mode & SPI_NO_CS)) {
799 if (spi->cs_gpiod)
Felix Fietkau28f76042019-02-10 00:38:25 +0100800 gpiod_set_value_cansleep(spi->cs_gpiod,
801 !enable);
Linus Walleijf3186dd2019-01-07 16:51:50 +0100802 else
Felix Fietkau28f76042019-02-10 00:38:25 +0100803 gpio_set_value_cansleep(spi->cs_gpio, !enable);
Linus Walleijf3186dd2019-01-07 16:51:50 +0100804 }
Thor Thayer8eee6b92016-10-10 09:25:24 -0500805 /* Some SPI masters need both GPIO CS & slave_select */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200806 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
807 spi->controller->set_cs)
808 spi->controller->set_cs(spi, !enable);
809 } else if (spi->controller->set_cs) {
810 spi->controller->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500811 }
Alexandru Ardelean25093bd2019-09-26 13:51:43 +0300812
813 if (!spi->controller->set_cs_timing) {
814 if (!enable1)
815 spi_delay_exec(&spi->controller->cs_inactive, NULL);
816 }
Mark Brownb1589352013-10-05 11:50:40 +0100817}
818
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200819#ifdef CONFIG_HAS_DMA
Boris Brezillon46336962018-04-22 20:35:14 +0200820int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
821 struct sg_table *sgt, void *buf, size_t len,
822 enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000823{
824 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200825 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530826#ifdef CONFIG_HIGHMEM
827 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
828 (unsigned long)buf < (PKMAP_BASE +
829 (LAST_PKMAP * PAGE_SIZE)));
830#else
831 const bool kmap_buf = false;
832#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500833 int desc_len;
834 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000835 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600836 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000837 void *sg_buf;
838 size_t min;
839 int i, ret;
840
Vignesh Rb1b81532016-08-17 15:22:36 +0530841 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200842 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500843 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530844 } else if (virt_addr_valid(buf)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200845 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500846 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530847 } else {
848 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500849 }
850
Mark Brown6ad45a22014-02-02 13:47:47 +0000851 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
852 if (ret != 0)
853 return ret;
854
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600855 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000856 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000857
Vignesh Rb1b81532016-08-17 15:22:36 +0530858 if (vmalloced_buf || kmap_buf) {
Maxime Chevallierce993192018-03-02 15:55:09 +0100859 /*
860 * Next scatterlist entry size is the minimum between
861 * the desc_len and the remaining buffer length that
862 * fits in a page.
863 */
864 min = min_t(size_t, desc_len,
865 min_t(size_t, len,
866 PAGE_SIZE - offset_in_page(buf)));
Vignesh Rb1b81532016-08-17 15:22:36 +0530867 if (vmalloced_buf)
868 vm_page = vmalloc_to_page(buf);
869 else
870 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000871 if (!vm_page) {
872 sg_free_table(sgt);
873 return -ENOMEM;
874 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600875 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000876 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000877 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500878 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000879 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600880 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000881 }
882
Mark Brown6ad45a22014-02-02 13:47:47 +0000883 buf += min;
884 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600885 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000886 }
887
888 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200889 if (!ret)
890 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000891 if (ret < 0) {
892 sg_free_table(sgt);
893 return ret;
894 }
895
896 sgt->nents = ret;
897
898 return 0;
899}
900
Boris Brezillon46336962018-04-22 20:35:14 +0200901void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
902 struct sg_table *sgt, enum dma_data_direction dir)
Mark Brown6ad45a22014-02-02 13:47:47 +0000903{
904 if (sgt->orig_nents) {
905 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
906 sg_free_table(sgt);
907 }
908}
909
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200910static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000911{
Mark Brown99adef32014-01-16 12:22:43 +0000912 struct device *tx_dev, *rx_dev;
913 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000914 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000915
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200916 if (!ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000917 return 0;
918
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200919 if (ctlr->dma_tx)
920 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800921 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200922 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800923
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200924 if (ctlr->dma_rx)
925 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800926 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200927 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000928
929 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200930 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000931 continue;
932
933 if (xfer->tx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200934 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000935 (void *)xfer->tx_buf, xfer->len,
936 DMA_TO_DEVICE);
937 if (ret != 0)
938 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000939 }
940
941 if (xfer->rx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200942 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000943 xfer->rx_buf, xfer->len,
944 DMA_FROM_DEVICE);
945 if (ret != 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200946 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000947 DMA_TO_DEVICE);
948 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000949 }
950 }
951 }
952
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200953 ctlr->cur_msg_mapped = true;
Mark Brown99adef32014-01-16 12:22:43 +0000954
955 return 0;
956}
957
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200958static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000959{
960 struct spi_transfer *xfer;
961 struct device *tx_dev, *rx_dev;
962
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200963 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000964 return 0;
965
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200966 if (ctlr->dma_tx)
967 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800968 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200969 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800970
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200971 if (ctlr->dma_rx)
972 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800973 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200974 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000975
976 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200977 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000978 continue;
979
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200980 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
981 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000982 }
983
984 return 0;
985}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200986#else /* !CONFIG_HAS_DMA */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200987static inline int __spi_map_msg(struct spi_controller *ctlr,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200988 struct spi_message *msg)
989{
990 return 0;
991}
992
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200993static inline int __spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000994 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200995{
996 return 0;
997}
998#endif /* !CONFIG_HAS_DMA */
999
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001000static inline int spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +00001001 struct spi_message *msg)
1002{
1003 struct spi_transfer *xfer;
1004
1005 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1006 /*
1007 * Restore the original value of tx_buf or rx_buf if they are
1008 * NULL.
1009 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001010 if (xfer->tx_buf == ctlr->dummy_tx)
Martin Sperl4b786452015-05-25 10:13:10 +00001011 xfer->tx_buf = NULL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001012 if (xfer->rx_buf == ctlr->dummy_rx)
Martin Sperl4b786452015-05-25 10:13:10 +00001013 xfer->rx_buf = NULL;
1014 }
1015
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001016 return __spi_unmap_msg(ctlr, msg);
Martin Sperl4b786452015-05-25 10:13:10 +00001017}
1018
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001019static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001020{
1021 struct spi_transfer *xfer;
1022 void *tmp;
1023 unsigned int max_tx, max_rx;
1024
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001025 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001026 max_tx = 0;
1027 max_rx = 0;
1028
1029 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001030 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001031 !xfer->tx_buf)
1032 max_tx = max(xfer->len, max_tx);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001033 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001034 !xfer->rx_buf)
1035 max_rx = max(xfer->len, max_rx);
1036 }
1037
1038 if (max_tx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001039 tmp = krealloc(ctlr->dummy_tx, max_tx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001040 GFP_KERNEL | GFP_DMA);
1041 if (!tmp)
1042 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001043 ctlr->dummy_tx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001044 memset(tmp, 0, max_tx);
1045 }
1046
1047 if (max_rx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001048 tmp = krealloc(ctlr->dummy_rx, max_rx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001049 GFP_KERNEL | GFP_DMA);
1050 if (!tmp)
1051 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001052 ctlr->dummy_rx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001053 }
1054
1055 if (max_tx || max_rx) {
1056 list_for_each_entry(xfer, &msg->transfers,
1057 transfer_list) {
Chris Lesiak5442dca2019-03-07 20:39:00 +00001058 if (!xfer->len)
1059 continue;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001060 if (!xfer->tx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001061 xfer->tx_buf = ctlr->dummy_tx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001062 if (!xfer->rx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001063 xfer->rx_buf = ctlr->dummy_rx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001064 }
1065 }
1066 }
1067
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001068 return __spi_map_msg(ctlr, msg);
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001069}
Mark Brown99adef32014-01-16 12:22:43 +00001070
Lubomir Rintel810923f2018-11-13 11:22:24 +01001071static int spi_transfer_wait(struct spi_controller *ctlr,
1072 struct spi_message *msg,
1073 struct spi_transfer *xfer)
1074{
1075 struct spi_statistics *statm = &ctlr->statistics;
1076 struct spi_statistics *stats = &msg->spi->statistics;
1077 unsigned long long ms = 1;
1078
1079 if (spi_controller_is_slave(ctlr)) {
1080 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
1081 dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
1082 return -EINTR;
1083 }
1084 } else {
1085 ms = 8LL * 1000LL * xfer->len;
1086 do_div(ms, xfer->speed_hz);
1087 ms += ms + 200; /* some tolerance */
1088
1089 if (ms > UINT_MAX)
1090 ms = UINT_MAX;
1091
1092 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
1093 msecs_to_jiffies(ms));
1094
1095 if (ms == 0) {
1096 SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
1097 SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
1098 dev_err(&msg->spi->dev,
1099 "SPI transfer timed out\n");
1100 return -ETIMEDOUT;
1101 }
1102 }
1103
1104 return 0;
1105}
1106
Martin Sperl0ff2de82019-02-23 08:49:48 +00001107static void _spi_transfer_delay_ns(u32 ns)
1108{
1109 if (!ns)
1110 return;
1111 if (ns <= 1000) {
1112 ndelay(ns);
1113 } else {
1114 u32 us = DIV_ROUND_UP(ns, 1000);
1115
1116 if (us <= 10)
1117 udelay(us);
1118 else
1119 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1120 }
1121}
1122
Alexandru Ardelean3984d392019-09-26 13:51:44 +03001123int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer)
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001124{
1125 u32 delay = _delay->value;
1126 u32 unit = _delay->unit;
1127 u32 hz;
1128
1129 if (!delay)
1130 return 0;
1131
1132 switch (unit) {
1133 case SPI_DELAY_UNIT_USECS:
1134 delay *= 1000;
1135 break;
1136 case SPI_DELAY_UNIT_NSECS: /* nothing to do here */
1137 break;
1138 case SPI_DELAY_UNIT_SCK:
1139 /* clock cycles need to be obtained from spi_transfer */
1140 if (!xfer)
1141 return -EINVAL;
1142 /* if there is no effective speed know, then approximate
1143 * by underestimating with half the requested hz
1144 */
1145 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
1146 if (!hz)
1147 return -EINVAL;
1148 delay *= DIV_ROUND_UP(1000000000, hz);
1149 break;
1150 default:
1151 return -EINVAL;
1152 }
1153
1154 return delay;
1155}
Alexandru Ardelean3984d392019-09-26 13:51:44 +03001156EXPORT_SYMBOL_GPL(spi_delay_to_ns);
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001157
1158int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer)
1159{
1160 int delay;
1161
1162 if (!_delay)
1163 return -EINVAL;
1164
Alexandru Ardelean3984d392019-09-26 13:51:44 +03001165 delay = spi_delay_to_ns(_delay, xfer);
Alexandru Ardeleanb2c98152019-09-26 13:51:30 +03001166 if (delay < 0)
1167 return delay;
1168
1169 _spi_transfer_delay_ns(delay);
1170
1171 return 0;
1172}
1173EXPORT_SYMBOL_GPL(spi_delay_exec);
1174
Martin Sperl0ff2de82019-02-23 08:49:48 +00001175static void _spi_transfer_cs_change_delay(struct spi_message *msg,
1176 struct spi_transfer *xfer)
1177{
Alexandru Ardelean329f0da2019-09-26 13:51:31 +03001178 u32 delay = xfer->cs_change_delay.value;
1179 u32 unit = xfer->cs_change_delay.unit;
1180 int ret;
Martin Sperl0ff2de82019-02-23 08:49:48 +00001181
1182 /* return early on "fast" mode - for everything but USECS */
Alexandru Ardelean6b3f2362019-09-26 13:51:29 +03001183 if (!delay) {
1184 if (unit == SPI_DELAY_UNIT_USECS)
1185 _spi_transfer_delay_ns(10000);
Martin Sperl0ff2de82019-02-23 08:49:48 +00001186 return;
Alexandru Ardelean6b3f2362019-09-26 13:51:29 +03001187 }
Martin Sperl0ff2de82019-02-23 08:49:48 +00001188
Alexandru Ardelean329f0da2019-09-26 13:51:31 +03001189 ret = spi_delay_exec(&xfer->cs_change_delay, xfer);
1190 if (ret) {
Martin Sperl0ff2de82019-02-23 08:49:48 +00001191 dev_err_once(&msg->spi->dev,
1192 "Use of unsupported delay unit %i, using default of 10us\n",
Alexandru Ardelean329f0da2019-09-26 13:51:31 +03001193 unit);
1194 _spi_transfer_delay_ns(10000);
Martin Sperl0ff2de82019-02-23 08:49:48 +00001195 }
Martin Sperl0ff2de82019-02-23 08:49:48 +00001196}
1197
Mark Brownb1589352013-10-05 11:50:40 +01001198/*
1199 * spi_transfer_one_message - Default implementation of transfer_one_message()
1200 *
1201 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -07001202 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +01001203 * standard handling of delays and chip select management.
1204 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001205static int spi_transfer_one_message(struct spi_controller *ctlr,
Mark Brownb1589352013-10-05 11:50:40 +01001206 struct spi_message *msg)
1207{
1208 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +01001209 bool keep_cs = false;
1210 int ret = 0;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001211 struct spi_statistics *statm = &ctlr->statistics;
Martin Sperleca2ebc2015-06-22 13:00:36 +00001212 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +01001213
1214 spi_set_cs(msg->spi, true);
1215
Martin Sperleca2ebc2015-06-22 13:00:36 +00001216 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1217 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1218
Mark Brownb1589352013-10-05 11:50:40 +01001219 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1220 trace_spi_transfer_start(msg, xfer);
1221
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001222 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1223 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
Martin Sperleca2ebc2015-06-22 13:00:36 +00001224
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001225 if (!ctlr->ptp_sts_supported) {
1226 xfer->ptp_sts_word_pre = 0;
1227 ptp_read_system_prets(xfer->ptp_sts);
1228 }
1229
Mark Brown38ec10f2014-08-16 16:27:41 +01001230 if (xfer->tx_buf || xfer->rx_buf) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001231 reinit_completion(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001232
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001233 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
Mark Brown38ec10f2014-08-16 16:27:41 +01001234 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001235 SPI_STATISTICS_INCREMENT_FIELD(statm,
1236 errors);
1237 SPI_STATISTICS_INCREMENT_FIELD(stats,
1238 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001239 dev_err(&msg->spi->dev,
1240 "SPI transfer failed: %d\n", ret);
1241 goto out;
1242 }
Mark Brownb1589352013-10-05 11:50:40 +01001243
Mark Brownd57e7962018-11-15 16:08:32 -08001244 if (ret > 0) {
1245 ret = spi_transfer_wait(ctlr, msg, xfer);
1246 if (ret < 0)
1247 msg->status = ret;
1248 }
Mark Brown38ec10f2014-08-16 16:27:41 +01001249 } else {
1250 if (xfer->len)
1251 dev_err(&msg->spi->dev,
1252 "Bufferless transfer has length %u\n",
1253 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001254 }
Mark Brownb1589352013-10-05 11:50:40 +01001255
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001256 if (!ctlr->ptp_sts_supported) {
1257 ptp_read_system_postts(xfer->ptp_sts);
1258 xfer->ptp_sts_word_post = xfer->len;
1259 }
1260
Mark Brownb1589352013-10-05 11:50:40 +01001261 trace_spi_transfer_stop(msg, xfer);
1262
1263 if (msg->status != -EINPROGRESS)
1264 goto out;
1265
Alexandru Ardeleanbebcfd22019-09-26 13:51:36 +03001266 spi_transfer_delay_exec(xfer);
Mark Brownb1589352013-10-05 11:50:40 +01001267
1268 if (xfer->cs_change) {
1269 if (list_is_last(&xfer->transfer_list,
1270 &msg->transfers)) {
1271 keep_cs = true;
1272 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001273 spi_set_cs(msg->spi, false);
Martin Sperl0ff2de82019-02-23 08:49:48 +00001274 _spi_transfer_cs_change_delay(msg, xfer);
Mark Brown0b73aa62014-03-29 23:48:07 +00001275 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001276 }
1277 }
1278
1279 msg->actual_length += xfer->len;
1280 }
1281
1282out:
1283 if (ret != 0 || !keep_cs)
1284 spi_set_cs(msg->spi, false);
1285
1286 if (msg->status == -EINPROGRESS)
1287 msg->status = ret;
1288
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001289 if (msg->status && ctlr->handle_err)
1290 ctlr->handle_err(ctlr, msg);
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001291
Noralf Trønnesc9ba7a12019-04-13 20:24:13 +02001292 spi_res_release(ctlr, msg);
1293
Mark Brown0ed56252019-05-09 11:27:17 +09001294 spi_finalize_current_message(ctlr);
1295
Mark Brownb1589352013-10-05 11:50:40 +01001296 return ret;
1297}
1298
1299/**
1300 * spi_finalize_current_transfer - report completion of a transfer
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001301 * @ctlr: the controller reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001302 *
1303 * Called by SPI drivers using the core transfer_one_message()
1304 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001305 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001306 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001307void spi_finalize_current_transfer(struct spi_controller *ctlr)
Mark Brownb1589352013-10-05 11:50:40 +01001308{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001309 complete(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001310}
1311EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1312
Linus Walleijffbbdd212012-02-22 10:05:38 +01001313/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001314 * __spi_pump_messages - function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001315 * @ctlr: controller to process queue for
Mark Brownfc9e0f72014-12-10 13:46:33 +00001316 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001317 *
1318 * This function checks if there is any spi message in the queue that
1319 * needs processing and if so call out to the driver to initialize hardware
1320 * and transfer each message.
1321 *
Mark Brown0461a412014-12-09 21:38:05 +00001322 * Note that it is called both from the kthread itself and also from
1323 * inside spi_sync(); the queue extraction handling at the top of the
1324 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001325 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001326static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001327{
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001328 struct spi_transfer *xfer;
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001329 struct spi_message *msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001330 bool was_busy = false;
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001331 unsigned long flags;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001332 int ret;
1333
Mark Brown983aee52014-12-09 19:46:56 +00001334 /* Lock queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001335 spin_lock_irqsave(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001336
1337 /* Make sure we are not already running a message */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001338 if (ctlr->cur_msg) {
1339 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001340 return;
1341 }
1342
Mark Brownf0125f12019-01-23 17:29:53 +00001343 /* If another context is idling the device then defer */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001344 if (ctlr->idling) {
1345 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1346 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001347 return;
1348 }
1349
Mark Brown983aee52014-12-09 19:46:56 +00001350 /* Check if the queue is idle */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001351 if (list_empty(&ctlr->queue) || !ctlr->running) {
1352 if (!ctlr->busy) {
1353 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -07001354 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001355 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001356
Mark Brownf0125f12019-01-23 17:29:53 +00001357 /* Only do teardown in the thread */
1358 if (!in_kthread) {
1359 kthread_queue_work(&ctlr->kworker,
1360 &ctlr->pump_messages);
1361 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1362 return;
1363 }
1364
1365 ctlr->busy = false;
1366 ctlr->idling = true;
1367 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
1368
1369 kfree(ctlr->dummy_rx);
1370 ctlr->dummy_rx = NULL;
1371 kfree(ctlr->dummy_tx);
1372 ctlr->dummy_tx = NULL;
1373 if (ctlr->unprepare_transfer_hardware &&
1374 ctlr->unprepare_transfer_hardware(ctlr))
1375 dev_err(&ctlr->dev,
1376 "failed to unprepare transfer hardware\n");
1377 if (ctlr->auto_runtime_pm) {
1378 pm_runtime_mark_last_busy(ctlr->dev.parent);
1379 pm_runtime_put_autosuspend(ctlr->dev.parent);
1380 }
1381 trace_spi_controller_idle(ctlr);
1382
1383 spin_lock_irqsave(&ctlr->queue_lock, flags);
1384 ctlr->idling = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001385 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001386 return;
1387 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001388
Linus Walleijffbbdd212012-02-22 10:05:38 +01001389 /* Extract head of queue */
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001390 msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
1391 ctlr->cur_msg = msg;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001392
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001393 list_del_init(&msg->queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001394 if (ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001395 was_busy = true;
1396 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001397 ctlr->busy = true;
1398 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001399
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001400 mutex_lock(&ctlr->io_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001401
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001402 if (!was_busy && ctlr->auto_runtime_pm) {
1403 ret = pm_runtime_get_sync(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001404 if (ret < 0) {
Tony Lindgren7e48e232018-05-18 10:30:07 -07001405 pm_runtime_put_noidle(ctlr->dev.parent);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001406 dev_err(&ctlr->dev, "Failed to power device: %d\n",
Mark Brown49834de2013-07-28 14:47:02 +01001407 ret);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001408 mutex_unlock(&ctlr->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001409 return;
1410 }
1411 }
1412
Mark Brown56ec1972013-10-07 19:33:53 +01001413 if (!was_busy)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001414 trace_spi_controller_busy(ctlr);
Mark Brown56ec1972013-10-07 19:33:53 +01001415
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001416 if (!was_busy && ctlr->prepare_transfer_hardware) {
1417 ret = ctlr->prepare_transfer_hardware(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001418 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001419 dev_err(&ctlr->dev,
Super Liuf3440d92019-05-22 14:30:14 +08001420 "failed to prepare transfer hardware: %d\n",
1421 ret);
Mark Brown49834de2013-07-28 14:47:02 +01001422
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001423 if (ctlr->auto_runtime_pm)
1424 pm_runtime_put(ctlr->dev.parent);
Super Liuf3440d92019-05-22 14:30:14 +08001425
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001426 msg->status = ret;
Super Liuf3440d92019-05-22 14:30:14 +08001427 spi_finalize_current_message(ctlr);
1428
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001429 mutex_unlock(&ctlr->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001430 return;
1431 }
1432 }
1433
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001434 trace_spi_message_start(msg);
Mark Brown56ec1972013-10-07 19:33:53 +01001435
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001436 if (ctlr->prepare_message) {
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001437 ret = ctlr->prepare_message(ctlr, msg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001438 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001439 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1440 ret);
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001441 msg->status = ret;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001442 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001443 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001444 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001445 ctlr->cur_msg_prepared = true;
Mark Brown2841a5f2013-10-05 00:23:12 +01001446 }
1447
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001448 ret = spi_map_msg(ctlr, msg);
Mark Brown99adef32014-01-16 12:22:43 +00001449 if (ret) {
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001450 msg->status = ret;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001451 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001452 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001453 }
1454
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001455 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1456 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1457 xfer->ptp_sts_word_pre = 0;
1458 ptp_read_system_prets(xfer->ptp_sts);
1459 }
1460 }
1461
Vladimir Olteand1c44c92019-09-05 04:01:11 +03001462 ret = ctlr->transfer_one_message(ctlr, msg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001463 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001464 dev_err(&ctlr->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001465 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001466 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001467 }
Jon Hunter49023d22016-03-08 12:28:20 +00001468
1469out:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001470 mutex_unlock(&ctlr->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001471
1472 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001473 if (!ret)
1474 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001475}
1476
Mark Brownfc9e0f72014-12-10 13:46:33 +00001477/**
1478 * spi_pump_messages - kthread work function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001479 * @work: pointer to kthread work struct contained in the controller struct
Mark Brownfc9e0f72014-12-10 13:46:33 +00001480 */
1481static void spi_pump_messages(struct kthread_work *work)
1482{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001483 struct spi_controller *ctlr =
1484 container_of(work, struct spi_controller, pump_messages);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001485
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001486 __spi_pump_messages(ctlr, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001487}
1488
Douglas Anderson924b5862019-05-15 09:48:12 -07001489/**
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001490 * spi_take_timestamp_pre - helper for drivers to collect the beginning of the
1491 * TX timestamp for the requested byte from the SPI
1492 * transfer. The frequency with which this function
1493 * must be called (once per word, once for the whole
1494 * transfer, once per batch of words etc) is arbitrary
1495 * as long as the @tx buffer offset is greater than or
1496 * equal to the requested byte at the time of the
1497 * call. The timestamp is only taken once, at the
1498 * first such call. It is assumed that the driver
1499 * advances its @tx buffer pointer monotonically.
1500 * @ctlr: Pointer to the spi_controller structure of the driver
1501 * @xfer: Pointer to the transfer being timestamped
1502 * @tx: Pointer to the current word within the xfer->tx_buf that the driver is
1503 * preparing to transmit right now.
1504 * @irqs_off: If true, will disable IRQs and preemption for the duration of the
1505 * transfer, for less jitter in time measurement. Only compatible
1506 * with PIO drivers. If true, must follow up with
1507 * spi_take_timestamp_post or otherwise system will crash.
1508 * WARNING: for fully predictable results, the CPU frequency must
1509 * also be under control (governor).
1510 */
1511void spi_take_timestamp_pre(struct spi_controller *ctlr,
1512 struct spi_transfer *xfer,
1513 const void *tx, bool irqs_off)
1514{
1515 u8 bytes_per_word = DIV_ROUND_UP(xfer->bits_per_word, 8);
1516
1517 if (!xfer->ptp_sts)
1518 return;
1519
1520 if (xfer->timestamped_pre)
1521 return;
1522
1523 if (tx < (xfer->tx_buf + xfer->ptp_sts_word_pre * bytes_per_word))
1524 return;
1525
1526 /* Capture the resolution of the timestamp */
1527 xfer->ptp_sts_word_pre = (tx - xfer->tx_buf) / bytes_per_word;
1528
1529 xfer->timestamped_pre = true;
1530
1531 if (irqs_off) {
1532 local_irq_save(ctlr->irq_flags);
1533 preempt_disable();
1534 }
1535
1536 ptp_read_system_prets(xfer->ptp_sts);
1537}
1538EXPORT_SYMBOL_GPL(spi_take_timestamp_pre);
1539
1540/**
1541 * spi_take_timestamp_post - helper for drivers to collect the end of the
1542 * TX timestamp for the requested byte from the SPI
1543 * transfer. Can be called with an arbitrary
1544 * frequency: only the first call where @tx exceeds
1545 * or is equal to the requested word will be
1546 * timestamped.
1547 * @ctlr: Pointer to the spi_controller structure of the driver
1548 * @xfer: Pointer to the transfer being timestamped
1549 * @tx: Pointer to the current word within the xfer->tx_buf that the driver has
1550 * just transmitted.
1551 * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU.
1552 */
1553void spi_take_timestamp_post(struct spi_controller *ctlr,
1554 struct spi_transfer *xfer,
1555 const void *tx, bool irqs_off)
1556{
1557 u8 bytes_per_word = DIV_ROUND_UP(xfer->bits_per_word, 8);
1558
1559 if (!xfer->ptp_sts)
1560 return;
1561
1562 if (xfer->timestamped_post)
1563 return;
1564
1565 if (tx < (xfer->tx_buf + xfer->ptp_sts_word_post * bytes_per_word))
1566 return;
1567
1568 ptp_read_system_postts(xfer->ptp_sts);
1569
1570 if (irqs_off) {
1571 local_irq_restore(ctlr->irq_flags);
1572 preempt_enable();
1573 }
1574
1575 /* Capture the resolution of the timestamp */
1576 xfer->ptp_sts_word_post = (tx - xfer->tx_buf) / bytes_per_word;
1577
1578 xfer->timestamped_post = true;
1579}
1580EXPORT_SYMBOL_GPL(spi_take_timestamp_post);
1581
1582/**
Douglas Anderson924b5862019-05-15 09:48:12 -07001583 * spi_set_thread_rt - set the controller to pump at realtime priority
1584 * @ctlr: controller to boost priority of
1585 *
1586 * This can be called because the controller requested realtime priority
1587 * (by setting the ->rt value before calling spi_register_controller()) or
1588 * because a device on the bus said that its transfers needed realtime
1589 * priority.
1590 *
1591 * NOTE: at the moment if any device on a bus says it needs realtime then
1592 * the thread will be at realtime priority for all transfers on that
1593 * controller. If this eventually becomes a problem we may see if we can
1594 * find a way to boost the priority only temporarily during relevant
1595 * transfers.
1596 */
1597static void spi_set_thread_rt(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001598{
Peter Zijlstra4ff13d02019-08-01 13:13:53 +02001599 struct sched_param param = { .sched_priority = MAX_RT_PRIO / 2 };
Linus Walleijffbbdd212012-02-22 10:05:38 +01001600
Douglas Anderson924b5862019-05-15 09:48:12 -07001601 dev_info(&ctlr->dev,
1602 "will run message pump with realtime priority\n");
1603 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
1604}
1605
1606static int spi_init_queue(struct spi_controller *ctlr)
1607{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001608 ctlr->running = false;
1609 ctlr->busy = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001610
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001611 kthread_init_worker(&ctlr->kworker);
1612 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1613 "%s", dev_name(&ctlr->dev));
1614 if (IS_ERR(ctlr->kworker_task)) {
1615 dev_err(&ctlr->dev, "failed to create message pump task\n");
1616 return PTR_ERR(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001617 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001618 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
Mark Brownf0125f12019-01-23 17:29:53 +00001619
Linus Walleijffbbdd212012-02-22 10:05:38 +01001620 /*
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001621 * Controller config will indicate if this controller should run the
Linus Walleijffbbdd212012-02-22 10:05:38 +01001622 * message pump with high (realtime) priority to reduce the transfer
1623 * latency on the bus by minimising the delay between a transfer
1624 * request and the scheduling of the message pump thread. Without this
1625 * setting the message pump thread will remain at default priority.
1626 */
Douglas Anderson924b5862019-05-15 09:48:12 -07001627 if (ctlr->rt)
1628 spi_set_thread_rt(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001629
1630 return 0;
1631}
1632
1633/**
1634 * spi_get_next_queued_message() - called by driver to check for queued
1635 * messages
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001636 * @ctlr: the controller to check for queued messages
Linus Walleijffbbdd212012-02-22 10:05:38 +01001637 *
1638 * If there are more messages in the queue, the next message is returned from
1639 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001640 *
1641 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001642 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001643struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001644{
1645 struct spi_message *next;
1646 unsigned long flags;
1647
1648 /* get a pointer to the next message, if any */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001649 spin_lock_irqsave(&ctlr->queue_lock, flags);
1650 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
Axel Lin1cfd97f2014-01-02 15:16:40 +08001651 queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001652 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001653
1654 return next;
1655}
1656EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1657
1658/**
1659 * spi_finalize_current_message() - the current message is complete
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001660 * @ctlr: the controller to return the message to
Linus Walleijffbbdd212012-02-22 10:05:38 +01001661 *
1662 * Called by the driver to notify the core that the message in the front of the
1663 * queue is complete and can be removed from the queue.
1664 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001665void spi_finalize_current_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001666{
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001667 struct spi_transfer *xfer;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001668 struct spi_message *mesg;
1669 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001670 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001671
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001672 spin_lock_irqsave(&ctlr->queue_lock, flags);
1673 mesg = ctlr->cur_msg;
1674 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001675
Vladimir Olteanb42faee2019-09-05 04:01:12 +03001676 if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) {
1677 list_for_each_entry(xfer, &mesg->transfers, transfer_list) {
1678 ptp_read_system_postts(xfer->ptp_sts);
1679 xfer->ptp_sts_word_post = xfer->len;
1680 }
1681 }
1682
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001683 spi_unmap_msg(ctlr, mesg);
Mark Brown99adef32014-01-16 12:22:43 +00001684
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001685 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1686 ret = ctlr->unprepare_message(ctlr, mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001687 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001688 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1689 ret);
Mark Brown2841a5f2013-10-05 00:23:12 +01001690 }
1691 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001692
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001693 spin_lock_irqsave(&ctlr->queue_lock, flags);
1694 ctlr->cur_msg = NULL;
1695 ctlr->cur_msg_prepared = false;
Mark Brownf0125f12019-01-23 17:29:53 +00001696 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001697 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001698
1699 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001700
Linus Walleijffbbdd212012-02-22 10:05:38 +01001701 mesg->state = NULL;
1702 if (mesg->complete)
1703 mesg->complete(mesg->context);
1704}
1705EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1706
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001707static int spi_start_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001708{
1709 unsigned long flags;
1710
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001711 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001712
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001713 if (ctlr->running || ctlr->busy) {
1714 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001715 return -EBUSY;
1716 }
1717
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001718 ctlr->running = true;
1719 ctlr->cur_msg = NULL;
1720 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001721
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001722 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001723
1724 return 0;
1725}
1726
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001727static int spi_stop_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001728{
1729 unsigned long flags;
1730 unsigned limit = 500;
1731 int ret = 0;
1732
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001733 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001734
1735 /*
1736 * This is a bit lame, but is optimized for the common execution path.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001737 * A wait_queue on the ctlr->busy could be used, but then the common
Linus Walleijffbbdd212012-02-22 10:05:38 +01001738 * execution path (pump_messages) would be required to call wake_up or
1739 * friends on every SPI message. Do this instead.
1740 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001741 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1742 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001743 usleep_range(10000, 11000);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001744 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001745 }
1746
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001747 if (!list_empty(&ctlr->queue) || ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001748 ret = -EBUSY;
1749 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001750 ctlr->running = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001751
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001752 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001753
1754 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001755 dev_warn(&ctlr->dev, "could not stop message queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001756 return ret;
1757 }
1758 return ret;
1759}
1760
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001761static int spi_destroy_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001762{
1763 int ret;
1764
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001765 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001766
1767 /*
Petr Mladek39891442016-10-11 13:55:20 -07001768 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001769 * If the reason that stop_queue timed out is that the work will never
1770 * finish, then it does no good to call flush/stop thread, so
1771 * return anyway.
1772 */
1773 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001774 dev_err(&ctlr->dev, "problem destroying queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001775 return ret;
1776 }
1777
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001778 kthread_flush_worker(&ctlr->kworker);
1779 kthread_stop(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001780
1781 return 0;
1782}
1783
Mark Brown0461a412014-12-09 21:38:05 +00001784static int __spi_queued_transfer(struct spi_device *spi,
1785 struct spi_message *msg,
1786 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001787{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001788 struct spi_controller *ctlr = spi->controller;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001789 unsigned long flags;
1790
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001791 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001792
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001793 if (!ctlr->running) {
1794 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001795 return -ESHUTDOWN;
1796 }
1797 msg->actual_length = 0;
1798 msg->status = -EINPROGRESS;
1799
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001800 list_add_tail(&msg->queue, &ctlr->queue);
Mark Brownf0125f12019-01-23 17:29:53 +00001801 if (!ctlr->busy && need_pump)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001802 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001803
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001804 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001805 return 0;
1806}
1807
Mark Brown0461a412014-12-09 21:38:05 +00001808/**
1809 * spi_queued_transfer - transfer function for queued transfers
1810 * @spi: spi device which is requesting transfer
1811 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001812 *
1813 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001814 */
1815static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1816{
1817 return __spi_queued_transfer(spi, msg, true);
1818}
1819
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001820static int spi_controller_initialize_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001821{
1822 int ret;
1823
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001824 ctlr->transfer = spi_queued_transfer;
1825 if (!ctlr->transfer_one_message)
1826 ctlr->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001827
1828 /* Initialize and start queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001829 ret = spi_init_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001830 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001831 dev_err(&ctlr->dev, "problem initializing queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001832 goto err_init_queue;
1833 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001834 ctlr->queued = true;
1835 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001836 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001837 dev_err(&ctlr->dev, "problem starting queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001838 goto err_start_queue;
1839 }
1840
1841 return 0;
1842
1843err_start_queue:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001844 spi_destroy_queue(ctlr);
Mark Brownc3676d52014-05-01 10:47:52 -07001845err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001846 return ret;
1847}
1848
Boris Brezillon988f2592018-04-22 20:35:15 +02001849/**
1850 * spi_flush_queue - Send all pending messages in the queue from the callers'
1851 * context
1852 * @ctlr: controller to process queue for
1853 *
1854 * This should be used when one wants to ensure all pending messages have been
1855 * sent before doing something. Is used by the spi-mem code to make sure SPI
1856 * memory operations do not preempt regular SPI transfers that have been queued
1857 * before the spi-mem operation.
1858 */
1859void spi_flush_queue(struct spi_controller *ctlr)
1860{
1861 if (ctlr->transfer == spi_queued_transfer)
1862 __spi_pump_messages(ctlr, false);
1863}
1864
Linus Walleijffbbdd212012-02-22 10:05:38 +01001865/*-------------------------------------------------------------------------*/
1866
Andreas Larsson7cb94362012-12-04 15:09:38 +01001867#if defined(CONFIG_OF)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001868static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001869 struct device_node *nc)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001870{
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001871 u32 value;
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001872 int rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001873
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001874 /* Mode (clock phase/polarity/etc.) */
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001875 if (of_property_read_bool(nc, "spi-cpha"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001876 spi->mode |= SPI_CPHA;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001877 if (of_property_read_bool(nc, "spi-cpol"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001878 spi->mode |= SPI_CPOL;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001879 if (of_property_read_bool(nc, "spi-3wire"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001880 spi->mode |= SPI_3WIRE;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001881 if (of_property_read_bool(nc, "spi-lsb-first"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001882 spi->mode |= SPI_LSB_FIRST;
1883
Linus Walleijf3186dd2019-01-07 16:51:50 +01001884 /*
1885 * For descriptors associated with the device, polarity inversion is
1886 * handled in the gpiolib, so all chip selects are "active high" in
1887 * the logical sense, the gpiolib will invert the line if need be.
1888 */
1889 if (ctlr->use_gpio_descriptors)
1890 spi->mode |= SPI_CS_HIGH;
1891 else if (of_property_read_bool(nc, "spi-cs-high"))
1892 spi->mode |= SPI_CS_HIGH;
1893
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001894 /* Device DUAL/QUAD mode */
1895 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1896 switch (value) {
1897 case 1:
1898 break;
1899 case 2:
1900 spi->mode |= SPI_TX_DUAL;
1901 break;
1902 case 4:
1903 spi->mode |= SPI_TX_QUAD;
1904 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001905 case 8:
1906 spi->mode |= SPI_TX_OCTAL;
1907 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001908 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001909 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001910 "spi-tx-bus-width %d not supported\n",
1911 value);
1912 break;
1913 }
1914 }
1915
1916 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1917 switch (value) {
1918 case 1:
1919 break;
1920 case 2:
1921 spi->mode |= SPI_RX_DUAL;
1922 break;
1923 case 4:
1924 spi->mode |= SPI_RX_QUAD;
1925 break;
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00001926 case 8:
1927 spi->mode |= SPI_RX_OCTAL;
1928 break;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001929 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001930 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001931 "spi-rx-bus-width %d not supported\n",
1932 value);
1933 break;
1934 }
1935 }
1936
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001937 if (spi_controller_is_slave(ctlr)) {
Rob Herring194276b2018-12-05 13:50:41 -06001938 if (!of_node_name_eq(nc, "slave")) {
Rob Herring25c56c82017-07-18 16:43:31 -05001939 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
1940 nc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001941 return -EINVAL;
1942 }
1943 return 0;
1944 }
1945
1946 /* Device address */
1947 rc = of_property_read_u32(nc, "reg", &value);
1948 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05001949 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
1950 nc, rc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001951 return rc;
1952 }
1953 spi->chip_select = value;
1954
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001955 /* Device speed */
1956 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1957 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001958 dev_err(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05001959 "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001960 return rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001961 }
1962 spi->max_speed_hz = value;
1963
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001964 return 0;
1965}
1966
1967static struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001968of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001969{
1970 struct spi_device *spi;
1971 int rc;
1972
1973 /* Alloc an spi_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001974 spi = spi_alloc_device(ctlr);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001975 if (!spi) {
Rob Herring25c56c82017-07-18 16:43:31 -05001976 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001977 rc = -ENOMEM;
1978 goto err_out;
1979 }
1980
1981 /* Select device driver */
1982 rc = of_modalias_node(nc, spi->modalias,
1983 sizeof(spi->modalias));
1984 if (rc < 0) {
Rob Herring25c56c82017-07-18 16:43:31 -05001985 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001986 goto err_out;
1987 }
1988
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001989 rc = of_spi_parse_dt(ctlr, spi, nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001990 if (rc)
1991 goto err_out;
1992
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001993 /* Store a pointer to the node in the device structure */
1994 of_node_get(nc);
1995 spi->dev.of_node = nc;
1996
1997 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001998 rc = spi_add_device(spi);
1999 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05002000 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
Johan Hovold83241472017-01-30 17:47:05 +01002001 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002002 }
2003
2004 return spi;
2005
Johan Hovold83241472017-01-30 17:47:05 +01002006err_of_node_put:
2007 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02002008err_out:
2009 spi_dev_put(spi);
2010 return ERR_PTR(rc);
2011}
2012
Grant Likelyd57a4282012-04-07 14:16:53 -06002013/**
2014 * of_register_spi_devices() - Register child devices onto the SPI bus
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002015 * @ctlr: Pointer to spi_controller device
Grant Likelyd57a4282012-04-07 14:16:53 -06002016 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002017 * Registers an spi_device for each child node of controller node which
2018 * represents a valid SPI slave.
Grant Likelyd57a4282012-04-07 14:16:53 -06002019 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002020static void of_register_spi_devices(struct spi_controller *ctlr)
Grant Likelyd57a4282012-04-07 14:16:53 -06002021{
2022 struct spi_device *spi;
2023 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06002024
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002025 if (!ctlr->dev.of_node)
Grant Likelyd57a4282012-04-07 14:16:53 -06002026 return;
2027
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002028 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01002029 if (of_node_test_and_set_flag(nc, OF_POPULATED))
2030 continue;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002031 spi = of_register_spi_device(ctlr, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02002032 if (IS_ERR(spi)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002033 dev_warn(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05002034 "Failed to create SPI device for %pOF\n", nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02002035 of_node_clear_flag(nc, OF_POPULATED);
2036 }
Grant Likelyd57a4282012-04-07 14:16:53 -06002037 }
2038}
2039#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002040static void of_register_spi_devices(struct spi_controller *ctlr) { }
Grant Likelyd57a4282012-04-07 14:16:53 -06002041#endif
2042
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002043#ifdef CONFIG_ACPI
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002044struct acpi_spi_lookup {
2045 struct spi_controller *ctlr;
2046 u32 max_speed_hz;
2047 u32 mode;
2048 int irq;
2049 u8 bits_per_word;
2050 u8 chip_select;
2051};
2052
2053static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
2054 struct acpi_spi_lookup *lookup)
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002055{
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002056 const union acpi_object *obj;
2057
2058 if (!x86_apple_machine)
2059 return;
2060
2061 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
2062 && obj->buffer.length >= 4)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002063 lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002064
2065 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
2066 && obj->buffer.length == 8)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002067 lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002068
2069 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
2070 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002071 lookup->mode |= SPI_LSB_FIRST;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002072
2073 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
2074 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002075 lookup->mode |= SPI_CPOL;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002076
2077 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
2078 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002079 lookup->mode |= SPI_CPHA;
Lukas Wunner8a2e4872017-08-01 14:10:41 +02002080}
2081
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002082static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
2083{
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002084 struct acpi_spi_lookup *lookup = data;
2085 struct spi_controller *ctlr = lookup->ctlr;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002086
2087 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
2088 struct acpi_resource_spi_serialbus *sb;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002089 acpi_handle parent_handle;
2090 acpi_status status;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002091
2092 sb = &ares->data.spi_serial_bus;
2093 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002094
2095 status = acpi_get_handle(NULL,
2096 sb->resource_source.string_ptr,
2097 &parent_handle);
2098
Ard Biesheuvelb5e3cf42019-06-19 11:52:54 +02002099 if (ACPI_FAILURE(status) ||
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002100 ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
2101 return -ENODEV;
2102
Mika Westerberga0a90712016-02-08 17:14:28 +02002103 /*
2104 * ACPI DeviceSelection numbering is handled by the
2105 * host controller driver in Windows and can vary
2106 * from driver to driver. In Linux we always expect
2107 * 0 .. max - 1 so we need to ask the driver to
2108 * translate between the two schemes.
2109 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002110 if (ctlr->fw_translate_cs) {
2111 int cs = ctlr->fw_translate_cs(ctlr,
Mika Westerberga0a90712016-02-08 17:14:28 +02002112 sb->device_selection);
2113 if (cs < 0)
2114 return cs;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002115 lookup->chip_select = cs;
Mika Westerberga0a90712016-02-08 17:14:28 +02002116 } else {
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002117 lookup->chip_select = sb->device_selection;
Mika Westerberga0a90712016-02-08 17:14:28 +02002118 }
2119
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002120 lookup->max_speed_hz = sb->connection_speed;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002121
2122 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002123 lookup->mode |= SPI_CPHA;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002124 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002125 lookup->mode |= SPI_CPOL;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002126 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002127 lookup->mode |= SPI_CS_HIGH;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002128 }
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002129 } else if (lookup->irq < 0) {
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002130 struct resource r;
2131
2132 if (acpi_dev_resource_interrupt(ares, 0, &r))
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002133 lookup->irq = r.start;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002134 }
2135
2136 /* Always tell the ACPI core to skip this resource */
2137 return 1;
2138}
2139
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002140static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
Octavian Purdila7f244672016-07-08 19:13:11 +03002141 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002142{
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002143 acpi_handle parent_handle = NULL;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002144 struct list_head resource_list;
Ard Biesheuvelb28944c2019-06-20 14:36:49 +02002145 struct acpi_spi_lookup lookup = {};
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002146 struct spi_device *spi;
2147 int ret;
2148
Octavian Purdila7f244672016-07-08 19:13:11 +03002149 if (acpi_bus_get_status(adev) || !adev->status.present ||
2150 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002151 return AE_OK;
2152
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002153 lookup.ctlr = ctlr;
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002154 lookup.irq = -1;
2155
2156 INIT_LIST_HEAD(&resource_list);
2157 ret = acpi_dev_get_resources(adev, &resource_list,
2158 acpi_spi_add_resource, &lookup);
2159 acpi_dev_free_resource_list(&resource_list);
2160
2161 if (ret < 0)
2162 /* found SPI in _CRS but it points to another controller */
2163 return AE_OK;
2164
2165 if (!lookup.max_speed_hz &&
2166 !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) &&
2167 ACPI_HANDLE(ctlr->dev.parent) == parent_handle) {
2168 /* Apple does not use _CRS but nested devices for SPI slaves */
2169 acpi_spi_parse_apple_properties(adev, &lookup);
2170 }
2171
2172 if (!lookup.max_speed_hz)
2173 return AE_OK;
2174
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002175 spi = spi_alloc_device(ctlr);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002176 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002177 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002178 dev_name(&adev->dev));
2179 return AE_NO_MEMORY;
2180 }
2181
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01002182 ACPI_COMPANION_SET(&spi->dev, adev);
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002183 spi->max_speed_hz = lookup.max_speed_hz;
2184 spi->mode = lookup.mode;
2185 spi->irq = lookup.irq;
2186 spi->bits_per_word = lookup.bits_per_word;
2187 spi->chip_select = lookup.chip_select;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002188
Dan O'Donovan0c6543f2017-02-05 16:30:14 +00002189 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
2190 sizeof(spi->modalias));
2191
Christophe RICARD33ada672015-12-23 23:25:35 +01002192 if (spi->irq < 0)
2193 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
2194
Octavian Purdila7f244672016-07-08 19:13:11 +03002195 acpi_device_set_enumerated(adev);
2196
Mika Westerberg33cf00e2013-10-10 13:28:48 +03002197 adev->power.flags.ignore_parent = true;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002198 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03002199 adev->power.flags.ignore_parent = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002200 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002201 dev_name(&adev->dev));
2202 spi_dev_put(spi);
2203 }
2204
2205 return AE_OK;
2206}
2207
Octavian Purdila7f244672016-07-08 19:13:11 +03002208static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
2209 void *data, void **return_value)
2210{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002211 struct spi_controller *ctlr = data;
Octavian Purdila7f244672016-07-08 19:13:11 +03002212 struct acpi_device *adev;
2213
2214 if (acpi_bus_get_device(handle, &adev))
2215 return AE_OK;
2216
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002217 return acpi_register_spi_device(ctlr, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03002218}
2219
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002220#define SPI_ACPI_ENUMERATE_MAX_DEPTH 32
2221
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002222static void acpi_register_spi_devices(struct spi_controller *ctlr)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002223{
2224 acpi_status status;
2225 acpi_handle handle;
2226
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002227 handle = ACPI_HANDLE(ctlr->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002228 if (!handle)
2229 return;
2230
Ard Biesheuvel4c3c5952019-05-30 13:16:34 +02002231 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
2232 SPI_ACPI_ENUMERATE_MAX_DEPTH,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002233 acpi_spi_add_device, NULL, ctlr, NULL);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002234 if (ACPI_FAILURE(status))
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002235 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002236}
2237#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002238static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002239#endif /* CONFIG_ACPI */
2240
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002241static void spi_controller_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08002242{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002243 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002244
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002245 ctlr = container_of(dev, struct spi_controller, dev);
2246 kfree(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002247}
2248
2249static struct class spi_master_class = {
2250 .name = "spi_master",
2251 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002252 .dev_release = spi_controller_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00002253 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08002254};
2255
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002256#ifdef CONFIG_SPI_SLAVE
2257/**
2258 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
2259 * controller
2260 * @spi: device used for the current transfer
2261 */
2262int spi_slave_abort(struct spi_device *spi)
2263{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002264 struct spi_controller *ctlr = spi->controller;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002265
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002266 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
2267 return ctlr->slave_abort(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002268
2269 return -ENOTSUPP;
2270}
2271EXPORT_SYMBOL_GPL(spi_slave_abort);
2272
2273static int match_true(struct device *dev, void *data)
2274{
2275 return 1;
2276}
2277
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002278static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
2279 char *buf)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002280{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002281 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2282 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002283 struct device *child;
2284
2285 child = device_find_child(&ctlr->dev, NULL, match_true);
2286 return sprintf(buf, "%s\n",
2287 child ? to_spi_device(child)->modalias : NULL);
2288}
2289
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002290static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
2291 const char *buf, size_t count)
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002292{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002293 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
2294 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002295 struct spi_device *spi;
2296 struct device *child;
2297 char name[32];
2298 int rc;
2299
2300 rc = sscanf(buf, "%31s", name);
2301 if (rc != 1 || !name[0])
2302 return -EINVAL;
2303
2304 child = device_find_child(&ctlr->dev, NULL, match_true);
2305 if (child) {
2306 /* Remove registered slave */
2307 device_unregister(child);
2308 put_device(child);
2309 }
2310
2311 if (strcmp(name, "(null)")) {
2312 /* Register new slave */
2313 spi = spi_alloc_device(ctlr);
2314 if (!spi)
2315 return -ENOMEM;
2316
2317 strlcpy(spi->modalias, name, sizeof(spi->modalias));
2318
2319 rc = spi_add_device(spi);
2320 if (rc) {
2321 spi_dev_put(spi);
2322 return rc;
2323 }
2324 }
2325
2326 return count;
2327}
2328
Geert Uytterhoevencc8b4652019-07-31 14:47:38 +02002329static DEVICE_ATTR_RW(slave);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002330
2331static struct attribute *spi_slave_attrs[] = {
2332 &dev_attr_slave.attr,
2333 NULL,
2334};
2335
2336static const struct attribute_group spi_slave_group = {
2337 .attrs = spi_slave_attrs,
2338};
2339
2340static const struct attribute_group *spi_slave_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002341 &spi_controller_statistics_group,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002342 &spi_slave_group,
2343 NULL,
2344};
2345
2346static struct class spi_slave_class = {
2347 .name = "spi_slave",
2348 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002349 .dev_release = spi_controller_release,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002350 .dev_groups = spi_slave_groups,
2351};
2352#else
2353extern struct class spi_slave_class; /* dummy */
2354#endif
David Brownell8ae12a02006-01-08 13:34:19 -08002355
2356/**
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002357 * __spi_alloc_controller - allocate an SPI master or slave controller
David Brownell8ae12a02006-01-08 13:34:19 -08002358 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07002359 * @size: how much zeroed driver-private data to allocate; the pointer to this
Lukas Wunner229e6af2019-09-11 12:15:30 +02002360 * memory is in the driver_data field of the returned device, accessible
2361 * with spi_controller_get_devdata(); the memory is cacheline aligned;
2362 * drivers granting DMA access to portions of their private data need to
2363 * round up @size using ALIGN(size, dma_get_cache_alignment()).
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002364 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
2365 * slave (true) controller
David Brownell33e34dc2007-05-08 00:32:21 -07002366 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002367 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002368 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002369 * only ones directly touching chip registers. It's how they allocate
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002370 * an spi_controller structure, prior to calling spi_register_controller().
David Brownell8ae12a02006-01-08 13:34:19 -08002371 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002372 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08002373 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002374 * The caller is responsible for assigning the bus number and initializing the
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002375 * controller's methods before calling spi_register_controller(); and (after
2376 * errors adding the device) calling spi_controller_put() to prevent a memory
2377 * leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002378 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002379 * Return: the SPI controller structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002380 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002381struct spi_controller *__spi_alloc_controller(struct device *dev,
2382 unsigned int size, bool slave)
David Brownell8ae12a02006-01-08 13:34:19 -08002383{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002384 struct spi_controller *ctlr;
Lukas Wunner229e6af2019-09-11 12:15:30 +02002385 size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
David Brownell8ae12a02006-01-08 13:34:19 -08002386
David Brownell0c8684612006-01-08 13:34:25 -08002387 if (!dev)
2388 return NULL;
2389
Lukas Wunner229e6af2019-09-11 12:15:30 +02002390 ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002391 if (!ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002392 return NULL;
2393
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002394 device_initialize(&ctlr->dev);
2395 ctlr->bus_num = -1;
2396 ctlr->num_chipselect = 1;
2397 ctlr->slave = slave;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002398 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002399 ctlr->dev.class = &spi_slave_class;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002400 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002401 ctlr->dev.class = &spi_master_class;
2402 ctlr->dev.parent = dev;
2403 pm_suspend_ignore_children(&ctlr->dev, true);
Lukas Wunner229e6af2019-09-11 12:15:30 +02002404 spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
David Brownell8ae12a02006-01-08 13:34:19 -08002405
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002406 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002407}
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002408EXPORT_SYMBOL_GPL(__spi_alloc_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002409
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002410#ifdef CONFIG_OF
Linus Walleij43004f32019-08-08 17:03:21 +02002411static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002412{
Grant Likelye80beb22013-02-12 17:48:37 +00002413 int nb, i, *cs;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002414 struct device_node *np = ctlr->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002415
2416 if (!np)
2417 return 0;
2418
2419 nb = of_gpio_named_count(np, "cs-gpios");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002420 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002421
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002422 /* Return error only for an incorrectly formed cs-gpios property */
2423 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002424 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002425 else if (nb < 0)
2426 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002427
Kees Cooka86854d2018-06-12 14:07:58 -07002428 cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002429 GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002430 ctlr->cs_gpios = cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002431
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002432 if (!ctlr->cs_gpios)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002433 return -ENOMEM;
2434
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002435 for (i = 0; i < ctlr->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01002436 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002437
2438 for (i = 0; i < nb; i++)
2439 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2440
2441 return 0;
2442}
2443#else
Linus Walleij43004f32019-08-08 17:03:21 +02002444static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002445{
2446 return 0;
2447}
2448#endif
2449
Linus Walleijf3186dd2019-01-07 16:51:50 +01002450/**
2451 * spi_get_gpio_descs() - grab chip select GPIOs for the master
2452 * @ctlr: The SPI master to grab GPIO descriptors for
2453 */
2454static int spi_get_gpio_descs(struct spi_controller *ctlr)
2455{
2456 int nb, i;
2457 struct gpio_desc **cs;
2458 struct device *dev = &ctlr->dev;
2459
2460 nb = gpiod_count(dev, "cs");
2461 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
2462
2463 /* No GPIOs at all is fine, else return the error */
2464 if (nb == 0 || nb == -ENOENT)
2465 return 0;
2466 else if (nb < 0)
2467 return nb;
2468
2469 cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
2470 GFP_KERNEL);
2471 if (!cs)
2472 return -ENOMEM;
2473 ctlr->cs_gpiods = cs;
2474
2475 for (i = 0; i < nb; i++) {
2476 /*
2477 * Most chipselects are active low, the inverted
2478 * semantics are handled by special quirks in gpiolib,
2479 * so initializing them GPIOD_OUT_LOW here means
2480 * "unasserted", in most cases this will drive the physical
2481 * line high.
2482 */
2483 cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
2484 GPIOD_OUT_LOW);
Geert Uytterhoeven1723fde2019-04-03 16:46:56 +02002485 if (IS_ERR(cs[i]))
2486 return PTR_ERR(cs[i]);
Linus Walleijf3186dd2019-01-07 16:51:50 +01002487
2488 if (cs[i]) {
2489 /*
2490 * If we find a CS GPIO, name it after the device and
2491 * chip select line.
2492 */
2493 char *gpioname;
2494
2495 gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
2496 dev_name(dev), i);
2497 if (!gpioname)
2498 return -ENOMEM;
2499 gpiod_set_consumer_name(cs[i], gpioname);
2500 }
2501 }
2502
2503 return 0;
2504}
2505
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002506static int spi_controller_check_ops(struct spi_controller *ctlr)
2507{
2508 /*
Boris Brezillonb5932f52018-04-26 18:18:15 +02002509 * The controller may implement only the high-level SPI-memory like
2510 * operations if it does not support regular SPI transfers, and this is
2511 * valid use case.
2512 * If ->mem_ops is NULL, we request that at least one of the
2513 * ->transfer_xxx() method be implemented.
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002514 */
Boris Brezillonb5932f52018-04-26 18:18:15 +02002515 if (ctlr->mem_ops) {
2516 if (!ctlr->mem_ops->exec_op)
2517 return -EINVAL;
2518 } else if (!ctlr->transfer && !ctlr->transfer_one &&
2519 !ctlr->transfer_one_message) {
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002520 return -EINVAL;
Boris Brezillonb5932f52018-04-26 18:18:15 +02002521 }
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002522
2523 return 0;
2524}
2525
David Brownell8ae12a02006-01-08 13:34:19 -08002526/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002527 * spi_register_controller - register SPI master or slave controller
2528 * @ctlr: initialized master, originally from spi_alloc_master() or
2529 * spi_alloc_slave()
David Brownell33e34dc2007-05-08 00:32:21 -07002530 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002531 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002532 * SPI controllers connect to their drivers using some non-SPI bus,
David Brownell8ae12a02006-01-08 13:34:19 -08002533 * such as the platform bus. The final stage of probe() in that code
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002534 * includes calling spi_register_controller() to hook up to this SPI bus glue.
David Brownell8ae12a02006-01-08 13:34:19 -08002535 *
2536 * SPI controllers use board specific (often SOC specific) bus numbers,
2537 * and board-specific addressing for SPI devices combines those numbers
2538 * with chip select numbers. Since SPI does not directly support dynamic
2539 * device identification, boards need configuration tables telling which
2540 * chip is at which address.
2541 *
2542 * This must be called from context that can sleep. It returns zero on
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002543 * success, else a negative error code (dropping the controller's refcount).
David Brownell0c8684612006-01-08 13:34:25 -08002544 * After a successful return, the caller is responsible for calling
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002545 * spi_unregister_controller().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002546 *
2547 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002548 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002549int spi_register_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002550{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002551 struct device *dev = ctlr->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08002552 struct boardinfo *bi;
Sergei Shtylyovb93318a2019-04-05 18:48:50 +03002553 int status;
Lucas Stach42bdd702017-10-16 12:27:58 +02002554 int id, first_dynamic;
David Brownell8ae12a02006-01-08 13:34:19 -08002555
David Brownell0c8684612006-01-08 13:34:25 -08002556 if (!dev)
2557 return -ENODEV;
2558
Boris Brezillonbdf3a3b2018-04-11 00:44:30 +02002559 /*
2560 * Make sure all necessary hooks are implemented before registering
2561 * the SPI controller.
2562 */
2563 status = spi_controller_check_ops(ctlr);
2564 if (status)
2565 return status;
2566
Geert Uytterhoeven04b2d032018-08-21 11:53:03 +02002567 if (ctlr->bus_num >= 0) {
2568 /* devices with a fixed bus num must check-in with the num */
2569 mutex_lock(&board_lock);
2570 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2571 ctlr->bus_num + 1, GFP_KERNEL);
2572 mutex_unlock(&board_lock);
2573 if (WARN(id < 0, "couldn't get idr"))
2574 return id == -ENOSPC ? -EBUSY : id;
2575 ctlr->bus_num = id;
2576 } else if (ctlr->dev.of_node) {
2577 /* allocate dynamic bus number using Linux idr */
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302578 id = of_alias_get_id(ctlr->dev.of_node, "spi");
2579 if (id >= 0) {
2580 ctlr->bus_num = id;
2581 mutex_lock(&board_lock);
2582 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2583 ctlr->bus_num + 1, GFP_KERNEL);
2584 mutex_unlock(&board_lock);
2585 if (WARN(id < 0, "couldn't get idr"))
2586 return id == -ENOSPC ? -EBUSY : id;
2587 }
2588 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002589 if (ctlr->bus_num < 0) {
Lucas Stach42bdd702017-10-16 12:27:58 +02002590 first_dynamic = of_alias_get_highest_id("spi");
2591 if (first_dynamic < 0)
2592 first_dynamic = 0;
2593 else
2594 first_dynamic++;
2595
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302596 mutex_lock(&board_lock);
Lucas Stach42bdd702017-10-16 12:27:58 +02002597 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
2598 0, GFP_KERNEL);
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302599 mutex_unlock(&board_lock);
2600 if (WARN(id < 0, "couldn't get idr"))
2601 return id;
2602 ctlr->bus_num = id;
David Brownell8ae12a02006-01-08 13:34:19 -08002603 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002604 INIT_LIST_HEAD(&ctlr->queue);
2605 spin_lock_init(&ctlr->queue_lock);
2606 spin_lock_init(&ctlr->bus_lock_spinlock);
2607 mutex_init(&ctlr->bus_lock_mutex);
2608 mutex_init(&ctlr->io_mutex);
2609 ctlr->bus_lock_flag = 0;
2610 init_completion(&ctlr->xfer_completion);
2611 if (!ctlr->max_dma_len)
2612 ctlr->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002613
David Brownell8ae12a02006-01-08 13:34:19 -08002614 /* register the device, then userspace will see it.
2615 * registration fails if the bus ID is in use.
2616 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002617 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
Andrey Smirnov0a919ae2019-04-02 21:01:28 -07002618
2619 if (!spi_controller_is_slave(ctlr)) {
2620 if (ctlr->use_gpio_descriptors) {
2621 status = spi_get_gpio_descs(ctlr);
2622 if (status)
2623 return status;
2624 /*
2625 * A controller using GPIO descriptors always
2626 * supports SPI_CS_HIGH if need be.
2627 */
2628 ctlr->mode_bits |= SPI_CS_HIGH;
2629 } else {
2630 /* Legacy code path for GPIOs from DT */
Linus Walleij43004f32019-08-08 17:03:21 +02002631 status = of_spi_get_gpio_numbers(ctlr);
Andrey Smirnov0a919ae2019-04-02 21:01:28 -07002632 if (status)
2633 return status;
2634 }
2635 }
2636
Tudor Ambarusf9481b02019-06-19 14:38:28 +00002637 /*
2638 * Even if it's just one always-selected device, there must
2639 * be at least one chipselect.
2640 */
2641 if (!ctlr->num_chipselect)
2642 return -EINVAL;
2643
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002644 status = device_add(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302645 if (status < 0) {
2646 /* free bus id */
2647 mutex_lock(&board_lock);
2648 idr_remove(&spi_master_idr, ctlr->bus_num);
2649 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002650 goto done;
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302651 }
2652 dev_dbg(dev, "registered %s %s\n",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002653 spi_controller_is_slave(ctlr) ? "slave" : "master",
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302654 dev_name(&ctlr->dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002655
Boris Brezillonb5932f52018-04-26 18:18:15 +02002656 /*
2657 * If we're using a queued driver, start the queue. Note that we don't
2658 * need the queueing logic if the driver is only supporting high-level
2659 * memory operations.
2660 */
2661 if (ctlr->transfer) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002662 dev_info(dev, "controller is unqueued, this is deprecated\n");
Boris Brezillonb5932f52018-04-26 18:18:15 +02002663 } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002664 status = spi_controller_initialize_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002665 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002666 device_del(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302667 /* free bus id */
2668 mutex_lock(&board_lock);
2669 idr_remove(&spi_master_idr, ctlr->bus_num);
2670 mutex_unlock(&board_lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002671 goto done;
2672 }
2673 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00002674 /* add statistics */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002675 spin_lock_init(&ctlr->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002676
Feng Tang2b9603a2010-08-02 15:52:15 +08002677 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002678 list_add_tail(&ctlr->list, &spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002679 list_for_each_entry(bi, &board_list, list)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002680 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +08002681 mutex_unlock(&board_lock);
2682
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002683 /* Register devices from the device tree and ACPI */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002684 of_register_spi_devices(ctlr);
2685 acpi_register_spi_devices(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002686done:
2687 return status;
2688}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002689EXPORT_SYMBOL_GPL(spi_register_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002690
Mark Brown666d5b42013-08-31 18:50:52 +01002691static void devm_spi_unregister(struct device *dev, void *res)
2692{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002693 spi_unregister_controller(*(struct spi_controller **)res);
Mark Brown666d5b42013-08-31 18:50:52 +01002694}
2695
2696/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002697 * devm_spi_register_controller - register managed SPI master or slave
2698 * controller
2699 * @dev: device managing SPI controller
2700 * @ctlr: initialized controller, originally from spi_alloc_master() or
2701 * spi_alloc_slave()
Mark Brown666d5b42013-08-31 18:50:52 +01002702 * Context: can sleep
2703 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002704 * Register a SPI device as with spi_register_controller() which will
Johan Hovold68b892f2017-10-30 11:35:26 +01002705 * automatically be unregistered and freed.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002706 *
2707 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01002708 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002709int devm_spi_register_controller(struct device *dev,
2710 struct spi_controller *ctlr)
Mark Brown666d5b42013-08-31 18:50:52 +01002711{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002712 struct spi_controller **ptr;
Mark Brown666d5b42013-08-31 18:50:52 +01002713 int ret;
2714
2715 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2716 if (!ptr)
2717 return -ENOMEM;
2718
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002719 ret = spi_register_controller(ctlr);
Stephen Warren4b928942013-11-21 16:11:15 -07002720 if (!ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002721 *ptr = ctlr;
Mark Brown666d5b42013-08-31 18:50:52 +01002722 devres_add(dev, ptr);
2723 } else {
2724 devres_free(ptr);
2725 }
2726
2727 return ret;
2728}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002729EXPORT_SYMBOL_GPL(devm_spi_register_controller);
Mark Brown666d5b42013-08-31 18:50:52 +01002730
David Lamparter34860082010-08-30 23:54:17 +02002731static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002732{
David Lamparter34860082010-08-30 23:54:17 +02002733 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002734 return 0;
2735}
2736
2737/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002738 * spi_unregister_controller - unregister SPI master or slave controller
2739 * @ctlr: the controller being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002740 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002741 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002742 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002743 * only ones directly touching chip registers.
2744 *
2745 * This must be called from context that can sleep.
Johan Hovold68b892f2017-10-30 11:35:26 +01002746 *
2747 * Note that this function also drops a reference to the controller.
David Brownell8ae12a02006-01-08 13:34:19 -08002748 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002749void spi_unregister_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002750{
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302751 struct spi_controller *found;
Johan Hovold67f7b272017-10-30 11:35:25 +01002752 int id = ctlr->bus_num;
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002753
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302754 /* First make sure that this controller was ever added */
2755 mutex_lock(&board_lock);
Johan Hovold67f7b272017-10-30 11:35:25 +01002756 found = idr_find(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302757 mutex_unlock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002758 if (ctlr->queued) {
2759 if (spi_destroy_queue(ctlr))
2760 dev_err(&ctlr->dev, "queue remove failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002761 }
Feng Tang2b9603a2010-08-02 15:52:15 +08002762 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002763 list_del(&ctlr->list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002764 mutex_unlock(&board_lock);
2765
Andy Shevchenkoebc37af2019-06-15 20:41:35 +03002766 device_for_each_child(&ctlr->dev, NULL, __unregister);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002767 device_unregister(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302768 /* free bus id */
2769 mutex_lock(&board_lock);
Jarkko Nikula613bd1e2018-03-20 10:27:50 +02002770 if (found == ctlr)
2771 idr_remove(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302772 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002773}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002774EXPORT_SYMBOL_GPL(spi_unregister_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002775
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002776int spi_controller_suspend(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002777{
2778 int ret;
2779
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002780 /* Basically no-ops for non-queued controllers */
2781 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002782 return 0;
2783
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002784 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002785 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002786 dev_err(&ctlr->dev, "queue stop failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002787
2788 return ret;
2789}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002790EXPORT_SYMBOL_GPL(spi_controller_suspend);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002791
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002792int spi_controller_resume(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002793{
2794 int ret;
2795
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002796 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002797 return 0;
2798
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002799 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002800 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002801 dev_err(&ctlr->dev, "queue restart failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002802
2803 return ret;
2804}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002805EXPORT_SYMBOL_GPL(spi_controller_resume);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002806
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002807static int __spi_controller_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002808{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002809 struct spi_controller *ctlr;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002810 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002811
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002812 ctlr = container_of(dev, struct spi_controller, dev);
2813 return ctlr->bus_num == *bus_num;
Dave Young5ed2c832008-01-22 15:14:18 +08002814}
2815
David Brownell8ae12a02006-01-08 13:34:19 -08002816/**
2817 * spi_busnum_to_master - look up master associated with bus_num
2818 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002819 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002820 *
2821 * This call may be used with devices that are registered after
2822 * arch init time. It returns a refcounted pointer to the relevant
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002823 * spi_controller (which the caller must release), or NULL if there is
David Brownell8ae12a02006-01-08 13:34:19 -08002824 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002825 *
2826 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002827 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002828struct spi_controller *spi_busnum_to_master(u16 bus_num)
David Brownell8ae12a02006-01-08 13:34:19 -08002829{
Tony Jones49dce682007-10-16 01:27:48 -07002830 struct device *dev;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002831 struct spi_controller *ctlr = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002832
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002833 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002834 __spi_controller_match);
Dave Young5ed2c832008-01-22 15:14:18 +08002835 if (dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002836 ctlr = container_of(dev, struct spi_controller, dev);
Dave Young5ed2c832008-01-22 15:14:18 +08002837 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002838 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002839}
2840EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2841
Martin Sperld780c372015-12-14 15:20:18 +00002842/*-------------------------------------------------------------------------*/
2843
2844/* Core methods for SPI resource management */
2845
2846/**
2847 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2848 * during the processing of a spi_message while using
2849 * spi_transfer_one
2850 * @spi: the spi device for which we allocate memory
2851 * @release: the release code to execute for this resource
2852 * @size: size to alloc and return
2853 * @gfp: GFP allocation flags
2854 *
2855 * Return: the pointer to the allocated data
2856 *
2857 * This may get enhanced in the future to allocate from a memory pool
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002858 * of the @spi_device or @spi_controller to avoid repeated allocations.
Martin Sperld780c372015-12-14 15:20:18 +00002859 */
2860void *spi_res_alloc(struct spi_device *spi,
2861 spi_res_release_t release,
2862 size_t size, gfp_t gfp)
2863{
2864 struct spi_res *sres;
2865
2866 sres = kzalloc(sizeof(*sres) + size, gfp);
2867 if (!sres)
2868 return NULL;
2869
2870 INIT_LIST_HEAD(&sres->entry);
2871 sres->release = release;
2872
2873 return sres->data;
2874}
2875EXPORT_SYMBOL_GPL(spi_res_alloc);
2876
2877/**
2878 * spi_res_free - free an spi resource
2879 * @res: pointer to the custom data of a resource
2880 *
2881 */
2882void spi_res_free(void *res)
2883{
2884 struct spi_res *sres = container_of(res, struct spi_res, data);
2885
2886 if (!res)
2887 return;
2888
2889 WARN_ON(!list_empty(&sres->entry));
2890 kfree(sres);
2891}
2892EXPORT_SYMBOL_GPL(spi_res_free);
2893
2894/**
2895 * spi_res_add - add a spi_res to the spi_message
2896 * @message: the spi message
2897 * @res: the spi_resource
2898 */
2899void spi_res_add(struct spi_message *message, void *res)
2900{
2901 struct spi_res *sres = container_of(res, struct spi_res, data);
2902
2903 WARN_ON(!list_empty(&sres->entry));
2904 list_add_tail(&sres->entry, &message->resources);
2905}
2906EXPORT_SYMBOL_GPL(spi_res_add);
2907
2908/**
2909 * spi_res_release - release all spi resources for this message
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002910 * @ctlr: the @spi_controller
Martin Sperld780c372015-12-14 15:20:18 +00002911 * @message: the @spi_message
2912 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002913void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
Martin Sperld780c372015-12-14 15:20:18 +00002914{
Vladimir Zapolskiyf5694362019-06-18 19:28:18 +03002915 struct spi_res *res, *tmp;
Martin Sperld780c372015-12-14 15:20:18 +00002916
Vladimir Zapolskiyf5694362019-06-18 19:28:18 +03002917 list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
Martin Sperld780c372015-12-14 15:20:18 +00002918 if (res->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002919 res->release(ctlr, message, res->data);
Martin Sperld780c372015-12-14 15:20:18 +00002920
2921 list_del(&res->entry);
2922
2923 kfree(res);
2924 }
2925}
2926EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002927
2928/*-------------------------------------------------------------------------*/
2929
Martin Sperl523baf5a2015-12-14 15:20:19 +00002930/* Core methods for spi_message alterations */
2931
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002932static void __spi_replace_transfers_release(struct spi_controller *ctlr,
Martin Sperl523baf5a2015-12-14 15:20:19 +00002933 struct spi_message *msg,
2934 void *res)
2935{
2936 struct spi_replaced_transfers *rxfer = res;
2937 size_t i;
2938
2939 /* call extra callback if requested */
2940 if (rxfer->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002941 rxfer->release(ctlr, msg, res);
Martin Sperl523baf5a2015-12-14 15:20:19 +00002942
2943 /* insert replaced transfers back into the message */
2944 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2945
2946 /* remove the formerly inserted entries */
2947 for (i = 0; i < rxfer->inserted; i++)
2948 list_del(&rxfer->inserted_transfers[i].transfer_list);
2949}
2950
2951/**
2952 * spi_replace_transfers - replace transfers with several transfers
2953 * and register change with spi_message.resources
2954 * @msg: the spi_message we work upon
2955 * @xfer_first: the first spi_transfer we want to replace
2956 * @remove: number of transfers to remove
2957 * @insert: the number of transfers we want to insert instead
2958 * @release: extra release code necessary in some circumstances
2959 * @extradatasize: extra data to allocate (with alignment guarantees
2960 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002961 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002962 *
2963 * Returns: pointer to @spi_replaced_transfers,
2964 * PTR_ERR(...) in case of errors.
2965 */
2966struct spi_replaced_transfers *spi_replace_transfers(
2967 struct spi_message *msg,
2968 struct spi_transfer *xfer_first,
2969 size_t remove,
2970 size_t insert,
2971 spi_replaced_release_t release,
2972 size_t extradatasize,
2973 gfp_t gfp)
2974{
2975 struct spi_replaced_transfers *rxfer;
2976 struct spi_transfer *xfer;
2977 size_t i;
2978
2979 /* allocate the structure using spi_res */
2980 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
Gustavo A. R. Silvaaef97522019-06-07 13:48:45 -05002981 struct_size(rxfer, inserted_transfers, insert)
Martin Sperl523baf5a2015-12-14 15:20:19 +00002982 + extradatasize,
2983 gfp);
2984 if (!rxfer)
2985 return ERR_PTR(-ENOMEM);
2986
2987 /* the release code to invoke before running the generic release */
2988 rxfer->release = release;
2989
2990 /* assign extradata */
2991 if (extradatasize)
2992 rxfer->extradata =
2993 &rxfer->inserted_transfers[insert];
2994
2995 /* init the replaced_transfers list */
2996 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2997
2998 /* assign the list_entry after which we should reinsert
2999 * the @replaced_transfers - it may be spi_message.messages!
3000 */
3001 rxfer->replaced_after = xfer_first->transfer_list.prev;
3002
3003 /* remove the requested number of transfers */
3004 for (i = 0; i < remove; i++) {
3005 /* if the entry after replaced_after it is msg->transfers
3006 * then we have been requested to remove more transfers
3007 * than are in the list
3008 */
3009 if (rxfer->replaced_after->next == &msg->transfers) {
3010 dev_err(&msg->spi->dev,
3011 "requested to remove more spi_transfers than are available\n");
3012 /* insert replaced transfers back into the message */
3013 list_splice(&rxfer->replaced_transfers,
3014 rxfer->replaced_after);
3015
3016 /* free the spi_replace_transfer structure */
3017 spi_res_free(rxfer);
3018
3019 /* and return with an error */
3020 return ERR_PTR(-EINVAL);
3021 }
3022
3023 /* remove the entry after replaced_after from list of
3024 * transfers and add it to list of replaced_transfers
3025 */
3026 list_move_tail(rxfer->replaced_after->next,
3027 &rxfer->replaced_transfers);
3028 }
3029
3030 /* create copy of the given xfer with identical settings
3031 * based on the first transfer to get removed
3032 */
3033 for (i = 0; i < insert; i++) {
3034 /* we need to run in reverse order */
3035 xfer = &rxfer->inserted_transfers[insert - 1 - i];
3036
3037 /* copy all spi_transfer data */
3038 memcpy(xfer, xfer_first, sizeof(*xfer));
3039
3040 /* add to list */
3041 list_add(&xfer->transfer_list, rxfer->replaced_after);
3042
Alexandru Ardeleanbebcfd22019-09-26 13:51:36 +03003043 /* clear cs_change and delay for all but the last */
Martin Sperl523baf5a2015-12-14 15:20:19 +00003044 if (i) {
3045 xfer->cs_change = false;
3046 xfer->delay_usecs = 0;
Alexandru Ardeleanbebcfd22019-09-26 13:51:36 +03003047 xfer->delay.value = 0;
Martin Sperl523baf5a2015-12-14 15:20:19 +00003048 }
3049 }
3050
3051 /* set up inserted */
3052 rxfer->inserted = insert;
3053
3054 /* and register it with spi_res/spi_message */
3055 spi_res_add(msg, rxfer);
3056
3057 return rxfer;
3058}
3059EXPORT_SYMBOL_GPL(spi_replace_transfers);
3060
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003061static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
Fabio Estevam08933412016-02-14 13:33:50 -02003062 struct spi_message *msg,
3063 struct spi_transfer **xferp,
3064 size_t maxsize,
3065 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00003066{
3067 struct spi_transfer *xfer = *xferp, *xfers;
3068 struct spi_replaced_transfers *srt;
3069 size_t offset;
3070 size_t count, i;
3071
Martin Sperld9f12122015-12-14 15:20:20 +00003072 /* calculate how many we have to replace */
3073 count = DIV_ROUND_UP(xfer->len, maxsize);
3074
3075 /* create replacement */
3076 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03003077 if (IS_ERR(srt))
3078 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00003079 xfers = srt->inserted_transfers;
3080
3081 /* now handle each of those newly inserted spi_transfers
3082 * note that the replacements spi_transfers all are preset
3083 * to the same values as *xferp, so tx_buf, rx_buf and len
3084 * are all identical (as well as most others)
3085 * so we just have to fix up len and the pointers.
3086 *
3087 * this also includes support for the depreciated
3088 * spi_message.is_dma_mapped interface
3089 */
3090
3091 /* the first transfer just needs the length modified, so we
3092 * run it outside the loop
3093 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02003094 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00003095
3096 /* all the others need rx_buf/tx_buf also set */
3097 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
3098 /* update rx_buf, tx_buf and dma */
3099 if (xfers[i].rx_buf)
3100 xfers[i].rx_buf += offset;
3101 if (xfers[i].rx_dma)
3102 xfers[i].rx_dma += offset;
3103 if (xfers[i].tx_buf)
3104 xfers[i].tx_buf += offset;
3105 if (xfers[i].tx_dma)
3106 xfers[i].tx_dma += offset;
3107
3108 /* update length */
3109 xfers[i].len = min(maxsize, xfers[i].len - offset);
3110 }
3111
3112 /* we set up xferp to the last entry we have inserted,
3113 * so that we skip those already split transfers
3114 */
3115 *xferp = &xfers[count - 1];
3116
3117 /* increment statistics counters */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003118 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperld9f12122015-12-14 15:20:20 +00003119 transfers_split_maxsize);
3120 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
3121 transfers_split_maxsize);
3122
3123 return 0;
3124}
3125
3126/**
3127 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
3128 * when an individual transfer exceeds a
3129 * certain size
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003130 * @ctlr: the @spi_controller for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09003131 * @msg: the @spi_message to transform
3132 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03003133 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00003134 *
3135 * Return: status of transformation
3136 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003137int spi_split_transfers_maxsize(struct spi_controller *ctlr,
Martin Sperld9f12122015-12-14 15:20:20 +00003138 struct spi_message *msg,
3139 size_t maxsize,
3140 gfp_t gfp)
3141{
3142 struct spi_transfer *xfer;
3143 int ret;
3144
3145 /* iterate over the transfer_list,
3146 * but note that xfer is advanced to the last transfer inserted
3147 * to avoid checking sizes again unnecessarily (also xfer does
3148 * potentiall belong to a different list by the time the
3149 * replacement has happened
3150 */
3151 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
3152 if (xfer->len > maxsize) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003153 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
3154 maxsize, gfp);
Martin Sperld9f12122015-12-14 15:20:20 +00003155 if (ret)
3156 return ret;
3157 }
3158 }
3159
3160 return 0;
3161}
3162EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08003163
3164/*-------------------------------------------------------------------------*/
3165
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003166/* Core methods for SPI controller protocol drivers. Some of the
David Brownell7d077192009-06-17 16:26:03 -07003167 * other core methods are currently defined as inline functions.
3168 */
3169
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003170static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
3171 u8 bits_per_word)
Stefan Brüns63ab6452015-08-23 16:06:30 +02003172{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003173 if (ctlr->bits_per_word_mask) {
Stefan Brüns63ab6452015-08-23 16:06:30 +02003174 /* Only 32 bits fit in the mask */
3175 if (bits_per_word > 32)
3176 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003177 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
Stefan Brüns63ab6452015-08-23 16:06:30 +02003178 return -EINVAL;
3179 }
3180
3181 return 0;
3182}
3183
David Brownell7d077192009-06-17 16:26:03 -07003184/**
3185 * spi_setup - setup SPI mode and clock rate
3186 * @spi: the device whose settings are being modified
3187 * Context: can sleep, and no requests are queued to the device
3188 *
3189 * SPI protocol drivers may need to update the transfer mode if the
3190 * device doesn't work with its default. They may likewise need
3191 * to update clock rates or word sizes from initial values. This function
3192 * changes those settings, and must be called from a context that can sleep.
3193 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
3194 * effect the next time the device is selected and data is transferred to
3195 * or from it. When this function returns, the spi device is deselected.
3196 *
3197 * Note that this call will fail if the protocol driver specifies an option
3198 * that the underlying controller or its driver does not support. For
3199 * example, not all hardware supports wire transfers using nine bit words,
3200 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003201 *
3202 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07003203 */
3204int spi_setup(struct spi_device *spi)
3205{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003206 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03003207 int status;
David Brownell7d077192009-06-17 16:26:03 -07003208
wangyuhangf477b7f2013-08-11 18:15:17 +08003209 /* check mode to prevent that DUAL and QUAD set at the same time
3210 */
3211 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
3212 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
3213 dev_err(&spi->dev,
3214 "setup: can not select dual and quad at the same time\n");
3215 return -EINVAL;
3216 }
3217 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
3218 */
3219 if ((spi->mode & SPI_3WIRE) && (spi->mode &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00003220 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3221 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
wangyuhangf477b7f2013-08-11 18:15:17 +08003222 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07003223 /* help drivers fail *cleanly* when they need options
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003224 * that aren't supported with their current controller
David Lechnercbaa62e2018-09-12 19:39:18 -05003225 * SPI_CS_WORD has a fallback software implementation,
3226 * so it is ignored here.
David Brownelle7db06b2009-06-17 16:26:04 -07003227 */
David Lechnercbaa62e2018-09-12 19:39:18 -05003228 bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
Serge Semind61ad23c2019-04-26 13:30:07 +03003229 /* nothing prevents from working with active-high CS in case if it
3230 * is driven by GPIO.
3231 */
3232 if (gpio_is_valid(spi->cs_gpio))
3233 bad_bits &= ~SPI_CS_HIGH;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003234 ugly_bits = bad_bits &
Yogesh Narayan Gaur6b030612018-12-03 08:39:06 +00003235 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
3236 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02003237 if (ugly_bits) {
3238 dev_warn(&spi->dev,
3239 "setup: ignoring unsupported mode bits %x\n",
3240 ugly_bits);
3241 spi->mode &= ~ugly_bits;
3242 bad_bits &= ~ugly_bits;
3243 }
David Brownelle7db06b2009-06-17 16:26:04 -07003244 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02003245 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07003246 bad_bits);
3247 return -EINVAL;
3248 }
3249
David Brownell7d077192009-06-17 16:26:03 -07003250 if (!spi->bits_per_word)
3251 spi->bits_per_word = 8;
3252
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003253 status = __spi_validate_bits_per_word(spi->controller,
3254 spi->bits_per_word);
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03003255 if (status)
3256 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02003257
Axel Lin052eb2d2014-02-10 00:08:05 +08003258 if (!spi->max_speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003259 spi->max_speed_hz = spi->controller->max_speed_hz;
Axel Lin052eb2d2014-02-10 00:08:05 +08003260
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003261 if (spi->controller->setup)
3262 status = spi->controller->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07003263
Luhua Xud948e6c2019-10-30 17:03:54 +08003264 if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
3265 status = pm_runtime_get_sync(spi->controller->dev.parent);
3266 if (status < 0) {
3267 pm_runtime_put_noidle(spi->controller->dev.parent);
3268 dev_err(&spi->controller->dev, "Failed to power device: %d\n",
3269 status);
3270 return status;
3271 }
3272 spi_set_cs(spi, false);
3273 pm_runtime_mark_last_busy(spi->controller->dev.parent);
3274 pm_runtime_put_autosuspend(spi->controller->dev.parent);
3275 } else {
3276 spi_set_cs(spi, false);
3277 }
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05003278
Douglas Anderson924b5862019-05-15 09:48:12 -07003279 if (spi->rt && !spi->controller->rt) {
3280 spi->controller->rt = true;
3281 spi_set_thread_rt(spi->controller);
3282 }
3283
Jingoo Han5fe5f052013-10-14 10:31:51 +09003284 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 -07003285 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
3286 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
3287 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
3288 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
3289 (spi->mode & SPI_LOOP) ? "loopback, " : "",
3290 spi->bits_per_word, spi->max_speed_hz,
3291 status);
3292
3293 return status;
3294}
3295EXPORT_SYMBOL_GPL(spi_setup);
3296
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003297/**
3298 * spi_set_cs_timing - configure CS setup, hold, and inactive delays
3299 * @spi: the device that requires specific CS timing configuration
Alexandru Ardelean81059362019-09-26 13:51:42 +03003300 * @setup: CS setup time specified via @spi_delay
3301 * @hold: CS hold time specified via @spi_delay
3302 * @inactive: CS inactive delay between transfers specified via @spi_delay
3303 *
3304 * Return: zero on success, else a negative error code.
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003305 */
Alexandru Ardelean81059362019-09-26 13:51:42 +03003306int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup,
3307 struct spi_delay *hold, struct spi_delay *inactive)
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003308{
Alexandru Ardelean25093bd2019-09-26 13:51:43 +03003309 size_t len;
3310
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003311 if (spi->controller->set_cs_timing)
Alexandru Ardelean81059362019-09-26 13:51:42 +03003312 return spi->controller->set_cs_timing(spi, setup, hold,
3313 inactive);
Alexandru Ardelean25093bd2019-09-26 13:51:43 +03003314
3315 if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) ||
3316 (hold && hold->unit == SPI_DELAY_UNIT_SCK) ||
3317 (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) {
3318 dev_err(&spi->dev,
3319 "Clock-cycle delays for CS not supported in SW mode\n");
3320 return -ENOTSUPP;
3321 }
3322
3323 len = sizeof(struct spi_delay);
3324
3325 /* copy delays to controller */
3326 if (setup)
3327 memcpy(&spi->controller->cs_setup, setup, len);
3328 else
3329 memset(&spi->controller->cs_setup, 0, len);
3330
3331 if (hold)
3332 memcpy(&spi->controller->cs_hold, hold, len);
3333 else
3334 memset(&spi->controller->cs_hold, 0, len);
3335
3336 if (inactive)
3337 memcpy(&spi->controller->cs_inactive, inactive, len);
3338 else
3339 memset(&spi->controller->cs_inactive, 0, len);
3340
3341 return 0;
Sowjanya Komatinenif1ca9992019-04-04 17:14:16 -07003342}
3343EXPORT_SYMBOL_GPL(spi_set_cs_timing);
3344
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003345static int _spi_xfer_word_delay_update(struct spi_transfer *xfer,
3346 struct spi_device *spi)
3347{
3348 int delay1, delay2;
3349
Alexandru Ardelean3984d392019-09-26 13:51:44 +03003350 delay1 = spi_delay_to_ns(&xfer->word_delay, xfer);
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003351 if (delay1 < 0)
3352 return delay1;
3353
Alexandru Ardelean3984d392019-09-26 13:51:44 +03003354 delay2 = spi_delay_to_ns(&spi->word_delay, xfer);
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003355 if (delay2 < 0)
3356 return delay2;
3357
3358 if (delay1 < delay2)
3359 memcpy(&xfer->word_delay, &spi->word_delay,
3360 sizeof(xfer->word_delay));
3361
3362 return 0;
3363}
3364
Mark Brown90808732013-11-13 23:44:15 +00003365static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003366{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003367 struct spi_controller *ctlr = spi->controller;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303368 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09003369 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003370
Mark Brown24a00132013-07-10 15:05:40 +01003371 if (list_empty(&message->transfers))
3372 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01003373
David Lechnercbaa62e2018-09-12 19:39:18 -05003374 /* If an SPI controller does not support toggling the CS line on each
David Lechner71388b212018-09-18 12:08:48 -05003375 * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
3376 * for the CS line, we can emulate the CS-per-word hardware function by
David Lechnercbaa62e2018-09-12 19:39:18 -05003377 * splitting transfers into one-word transfers and ensuring that
3378 * cs_change is set for each transfer.
3379 */
David Lechner71388b212018-09-18 12:08:48 -05003380 if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
Linus Walleijf3186dd2019-01-07 16:51:50 +01003381 spi->cs_gpiod ||
David Lechner71388b212018-09-18 12:08:48 -05003382 gpio_is_valid(spi->cs_gpio))) {
David Lechnercbaa62e2018-09-12 19:39:18 -05003383 size_t maxsize;
3384 int ret;
3385
3386 maxsize = (spi->bits_per_word + 7) / 8;
3387
3388 /* spi_split_transfers_maxsize() requires message->spi */
3389 message->spi = spi;
3390
3391 ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
3392 GFP_KERNEL);
3393 if (ret)
3394 return ret;
3395
3396 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3397 /* don't change cs_change on the last entry in the list */
3398 if (list_is_last(&xfer->transfer_list, &message->transfers))
3399 break;
3400 xfer->cs_change = 1;
3401 }
3402 }
3403
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003404 /* Half-duplex links include original MicroWire, and ones with
3405 * only one data pin like SPI_3WIRE (switches direction) or where
3406 * either MOSI or MISO is missing. They can also be caused by
3407 * software limitations.
3408 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003409 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
3410 (spi->mode & SPI_3WIRE)) {
3411 unsigned flags = ctlr->flags;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003412
3413 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3414 if (xfer->rx_buf && xfer->tx_buf)
3415 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003416 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003417 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003418 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003419 return -EINVAL;
3420 }
3421 }
3422
Laxman Dewangane6811d12012-11-09 14:36:45 +05303423 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303424 * Set transfer bits_per_word and max speed as spi device default if
3425 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08003426 * Set transfer tx_nbits and rx_nbits as single transfer default
3427 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Jonas Bonnb7bb3672019-01-30 09:40:04 +01003428 * Ensure transfer word_delay is at least as long as that required by
3429 * device itself.
Laxman Dewangane6811d12012-11-09 14:36:45 +05303430 */
Martin Sperl77e80582015-11-27 12:31:09 +00003431 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303432 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Martin Sperl5d7e2b52019-02-23 08:49:49 +00003433 xfer->effective_speed_hz = 0;
Sourav Poddar078726c2013-07-18 15:31:25 +05303434 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303435 if (!xfer->bits_per_word)
3436 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08003437
3438 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05303439 xfer->speed_hz = spi->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08003440
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003441 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
3442 xfer->speed_hz = ctlr->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02003443
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003444 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
Stefan Brüns63ab6452015-08-23 16:06:30 +02003445 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01003446
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003447 /*
3448 * SPI transfer length should be multiple of SPI word size
3449 * where SPI word size should be power-of-two multiple
3450 */
3451 if (xfer->bits_per_word <= 8)
3452 w_size = 1;
3453 else if (xfer->bits_per_word <= 16)
3454 w_size = 2;
3455 else
3456 w_size = 4;
3457
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003458 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09003459 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02003460 return -EINVAL;
3461
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003462 if (xfer->speed_hz && ctlr->min_speed_hz &&
3463 xfer->speed_hz < ctlr->min_speed_hz)
Mark Browna2fd4f92013-07-10 14:57:26 +01003464 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08003465
3466 if (xfer->tx_buf && !xfer->tx_nbits)
3467 xfer->tx_nbits = SPI_NBITS_SINGLE;
3468 if (xfer->rx_buf && !xfer->rx_nbits)
3469 xfer->rx_nbits = SPI_NBITS_SINGLE;
3470 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01003471 * 1. check the value matches one of single, dual and quad
3472 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08003473 */
Sourav Poddardb90a442013-08-22 21:20:48 +05303474 if (xfer->tx_buf) {
3475 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
3476 xfer->tx_nbits != SPI_NBITS_DUAL &&
3477 xfer->tx_nbits != SPI_NBITS_QUAD)
3478 return -EINVAL;
3479 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
3480 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3481 return -EINVAL;
3482 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
3483 !(spi->mode & SPI_TX_QUAD))
3484 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303485 }
wangyuhangf477b7f2013-08-11 18:15:17 +08003486 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05303487 if (xfer->rx_buf) {
3488 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
3489 xfer->rx_nbits != SPI_NBITS_DUAL &&
3490 xfer->rx_nbits != SPI_NBITS_QUAD)
3491 return -EINVAL;
3492 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
3493 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3494 return -EINVAL;
3495 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
3496 !(spi->mode & SPI_RX_QUAD))
3497 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05303498 }
Jonas Bonnb7bb3672019-01-30 09:40:04 +01003499
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03003500 if (_spi_xfer_word_delay_update(xfer, spi))
3501 return -EINVAL;
Laxman Dewangane6811d12012-11-09 14:36:45 +05303502 }
3503
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003504 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00003505
3506 return 0;
3507}
3508
3509static int __spi_async(struct spi_device *spi, struct spi_message *message)
3510{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003511 struct spi_controller *ctlr = spi->controller;
Vladimir Olteanb42faee2019-09-05 04:01:12 +03003512 struct spi_transfer *xfer;
Mark Brown90808732013-11-13 23:44:15 +00003513
Boris Brezillonb5932f52018-04-26 18:18:15 +02003514 /*
3515 * Some controllers do not support doing regular SPI transfers. Return
3516 * ENOTSUPP when this is the case.
3517 */
3518 if (!ctlr->transfer)
3519 return -ENOTSUPP;
3520
Mark Brown90808732013-11-13 23:44:15 +00003521 message->spi = spi;
3522
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003523 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003524 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
3525
Mark Brown90808732013-11-13 23:44:15 +00003526 trace_spi_message_submit(message);
3527
Vladimir Olteanb42faee2019-09-05 04:01:12 +03003528 if (!ctlr->ptp_sts_supported) {
3529 list_for_each_entry(xfer, &message->transfers, transfer_list) {
3530 xfer->ptp_sts_word_pre = 0;
3531 ptp_read_system_prets(xfer->ptp_sts);
3532 }
3533 }
3534
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003535 return ctlr->transfer(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003536}
3537
David Brownell568d0692009-09-22 16:46:18 -07003538/**
3539 * spi_async - asynchronous SPI transfer
3540 * @spi: device with which data will be exchanged
3541 * @message: describes the data transfers, including completion callback
3542 * Context: any (irqs may be blocked, etc)
3543 *
3544 * This call may be used in_irq and other contexts which can't sleep,
3545 * as well as from task contexts which can sleep.
3546 *
3547 * The completion callback is invoked in a context which can't sleep.
3548 * Before that invocation, the value of message->status is undefined.
3549 * When the callback is issued, message->status holds either zero (to
3550 * indicate complete success) or a negative error code. After that
3551 * callback returns, the driver which issued the transfer request may
3552 * deallocate the associated memory; it's no longer in use by any SPI
3553 * core or controller driver code.
3554 *
3555 * Note that although all messages to a spi_device are handled in
3556 * FIFO order, messages may go to different devices in other orders.
3557 * Some device might be higher priority, or have various "hard" access
3558 * time requirements, for example.
3559 *
3560 * On detection of any fault during the transfer, processing of
3561 * the entire message is aborted, and the device is deselected.
3562 * Until returning from the associated message completion callback,
3563 * no other spi_message queued to that device will be processed.
3564 * (This rule applies equally to all the synchronous transfer calls,
3565 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003566 *
3567 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07003568 */
3569int spi_async(struct spi_device *spi, struct spi_message *message)
3570{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003571 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003572 int ret;
3573 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07003574
Mark Brown90808732013-11-13 23:44:15 +00003575 ret = __spi_validate(spi, message);
3576 if (ret != 0)
3577 return ret;
3578
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003579 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07003580
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003581 if (ctlr->bus_lock_flag)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003582 ret = -EBUSY;
3583 else
3584 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07003585
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003586 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003587
3588 return ret;
David Brownell568d0692009-09-22 16:46:18 -07003589}
3590EXPORT_SYMBOL_GPL(spi_async);
3591
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003592/**
3593 * spi_async_locked - version of spi_async with exclusive bus usage
3594 * @spi: device with which data will be exchanged
3595 * @message: describes the data transfers, including completion callback
3596 * Context: any (irqs may be blocked, etc)
3597 *
3598 * This call may be used in_irq and other contexts which can't sleep,
3599 * as well as from task contexts which can sleep.
3600 *
3601 * The completion callback is invoked in a context which can't sleep.
3602 * Before that invocation, the value of message->status is undefined.
3603 * When the callback is issued, message->status holds either zero (to
3604 * indicate complete success) or a negative error code. After that
3605 * callback returns, the driver which issued the transfer request may
3606 * deallocate the associated memory; it's no longer in use by any SPI
3607 * core or controller driver code.
3608 *
3609 * Note that although all messages to a spi_device are handled in
3610 * FIFO order, messages may go to different devices in other orders.
3611 * Some device might be higher priority, or have various "hard" access
3612 * time requirements, for example.
3613 *
3614 * On detection of any fault during the transfer, processing of
3615 * the entire message is aborted, and the device is deselected.
3616 * Until returning from the associated message completion callback,
3617 * no other spi_message queued to that device will be processed.
3618 * (This rule applies equally to all the synchronous transfer calls,
3619 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003620 *
3621 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003622 */
3623int spi_async_locked(struct spi_device *spi, struct spi_message *message)
3624{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003625 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003626 int ret;
3627 unsigned long flags;
3628
Mark Brown90808732013-11-13 23:44:15 +00003629 ret = __spi_validate(spi, message);
3630 if (ret != 0)
3631 return ret;
3632
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003633 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003634
3635 ret = __spi_async(spi, message);
3636
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003637 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003638
3639 return ret;
3640
3641}
3642EXPORT_SYMBOL_GPL(spi_async_locked);
3643
David Brownell7d077192009-06-17 16:26:03 -07003644/*-------------------------------------------------------------------------*/
3645
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003646/* Utility methods for SPI protocol drivers, layered on
David Brownell7d077192009-06-17 16:26:03 -07003647 * top of the core. Some other utility methods are defined as
3648 * inline functions.
3649 */
3650
Andrew Morton5d870c82006-01-11 11:23:49 -08003651static void spi_complete(void *arg)
3652{
3653 complete(arg);
3654}
3655
Mark Brownef4d96e2016-07-21 23:53:31 +01003656static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003657{
3658 DECLARE_COMPLETION_ONSTACK(done);
3659 int status;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003660 struct spi_controller *ctlr = spi->controller;
Mark Brown0461a412014-12-09 21:38:05 +00003661 unsigned long flags;
3662
3663 status = __spi_validate(spi, message);
3664 if (status != 0)
3665 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003666
3667 message->complete = spi_complete;
3668 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00003669 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003670
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003671 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003672 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3673
Mark Brown0461a412014-12-09 21:38:05 +00003674 /* If we're not using the legacy transfer method then we will
3675 * try to transfer in the calling context so special case.
3676 * This code would be less tricky if we could remove the
3677 * support for driver implemented message queues.
3678 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003679 if (ctlr->transfer == spi_queued_transfer) {
3680 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003681
3682 trace_spi_message_submit(message);
3683
3684 status = __spi_queued_transfer(spi, message, false);
3685
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003686 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003687 } else {
3688 status = spi_async_locked(spi, message);
3689 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003690
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003691 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00003692 /* Push out the messages in the calling context if we
3693 * can.
3694 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003695 if (ctlr->transfer == spi_queued_transfer) {
3696 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperleca2ebc2015-06-22 13:00:36 +00003697 spi_sync_immediate);
3698 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3699 spi_sync_immediate);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003700 __spi_pump_messages(ctlr, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003701 }
Mark Brown0461a412014-12-09 21:38:05 +00003702
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003703 wait_for_completion(&done);
3704 status = message->status;
3705 }
3706 message->context = NULL;
3707 return status;
3708}
3709
David Brownell8ae12a02006-01-08 13:34:19 -08003710/**
3711 * spi_sync - blocking/synchronous SPI data transfers
3712 * @spi: device with which data will be exchanged
3713 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07003714 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003715 *
3716 * This call may only be used from a context that may sleep. The sleep
3717 * is non-interruptible, and has no timeout. Low-overhead controller
3718 * drivers may DMA directly into and out of the message buffers.
3719 *
3720 * Note that the SPI device's chip select is active during the message,
3721 * and then is normally disabled between messages. Drivers for some
3722 * frequently-used devices may want to minimize costs of selecting a chip,
3723 * by leaving it selected in anticipation that the next message will go
3724 * to the same chip. (That may increase power usage.)
3725 *
David Brownell0c8684612006-01-08 13:34:25 -08003726 * Also, the caller is guaranteeing that the memory associated with the
3727 * message will not be freed before this call returns.
3728 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003729 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003730 */
3731int spi_sync(struct spi_device *spi, struct spi_message *message)
3732{
Mark Brownef4d96e2016-07-21 23:53:31 +01003733 int ret;
3734
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003735 mutex_lock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003736 ret = __spi_sync(spi, message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003737 mutex_unlock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003738
3739 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08003740}
3741EXPORT_SYMBOL_GPL(spi_sync);
3742
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003743/**
3744 * spi_sync_locked - version of spi_sync with exclusive bus usage
3745 * @spi: device with which data will be exchanged
3746 * @message: describes the data transfers
3747 * Context: can sleep
3748 *
3749 * This call may only be used from a context that may sleep. The sleep
3750 * is non-interruptible, and has no timeout. Low-overhead controller
3751 * drivers may DMA directly into and out of the message buffers.
3752 *
3753 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003754 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003755 * be released by a spi_bus_unlock call when the exclusive access is over.
3756 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003757 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003758 */
3759int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3760{
Mark Brownef4d96e2016-07-21 23:53:31 +01003761 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003762}
3763EXPORT_SYMBOL_GPL(spi_sync_locked);
3764
3765/**
3766 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003767 * @ctlr: SPI bus master that should be locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003768 * Context: can sleep
3769 *
3770 * This call may only be used from a context that may sleep. The sleep
3771 * is non-interruptible, and has no timeout.
3772 *
3773 * This call should be used by drivers that require exclusive access to the
3774 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3775 * exclusive access is over. Data transfer must be done by spi_sync_locked
3776 * and spi_async_locked calls when the SPI bus lock is held.
3777 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003778 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003779 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003780int spi_bus_lock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003781{
3782 unsigned long flags;
3783
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003784 mutex_lock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003785
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003786 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3787 ctlr->bus_lock_flag = 1;
3788 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003789
3790 /* mutex remains locked until spi_bus_unlock is called */
3791
3792 return 0;
3793}
3794EXPORT_SYMBOL_GPL(spi_bus_lock);
3795
3796/**
3797 * spi_bus_unlock - release the lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003798 * @ctlr: SPI bus master that was locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003799 * Context: can sleep
3800 *
3801 * This call may only be used from a context that may sleep. The sleep
3802 * is non-interruptible, and has no timeout.
3803 *
3804 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3805 * call.
3806 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003807 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003808 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003809int spi_bus_unlock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003810{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003811 ctlr->bus_lock_flag = 0;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003812
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003813 mutex_unlock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003814
3815 return 0;
3816}
3817EXPORT_SYMBOL_GPL(spi_bus_unlock);
3818
David Brownella9948b62006-04-02 10:37:40 -08003819/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003820#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003821
3822static u8 *buf;
3823
3824/**
3825 * spi_write_then_read - SPI synchronous write followed by read
3826 * @spi: device with which data will be exchanged
3827 * @txbuf: data to be written (need not be dma-safe)
3828 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003829 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3830 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003831 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003832 *
3833 * This performs a half duplex MicroWire style transaction with the
3834 * device, sending txbuf and then reading rxbuf. The return value
3835 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003836 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003837 *
David Brownell0c8684612006-01-08 13:34:25 -08003838 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003839 * portable code should never use this for more than 32 bytes.
3840 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c8684612006-01-08 13:34:25 -08003841 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003842 *
3843 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003844 */
3845int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003846 const void *txbuf, unsigned n_tx,
3847 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003848{
David Brownell068f4072007-12-04 23:45:09 -08003849 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003850
3851 int status;
3852 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003853 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003854 u8 *local_buf;
3855
Mark Brownb3a223e2012-12-02 12:54:25 +09003856 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3857 * copying here, (as a pure convenience thing), but we can
3858 * keep heap costs out of the hot path unless someone else is
3859 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003860 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003861 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003862 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3863 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003864 if (!local_buf)
3865 return -ENOMEM;
3866 } else {
3867 local_buf = buf;
3868 }
David Brownell8ae12a02006-01-08 13:34:19 -08003869
Vitaly Wool8275c642006-01-08 13:34:28 -08003870 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003871 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003872 if (n_tx) {
3873 x[0].len = n_tx;
3874 spi_message_add_tail(&x[0], &message);
3875 }
3876 if (n_rx) {
3877 x[1].len = n_rx;
3878 spi_message_add_tail(&x[1], &message);
3879 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003880
David Brownell8ae12a02006-01-08 13:34:19 -08003881 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003882 x[0].tx_buf = local_buf;
3883 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003884
3885 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003886 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003887 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003888 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003889
David Brownellbdff5492009-04-13 14:39:57 -07003890 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003891 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003892 else
3893 kfree(local_buf);
3894
3895 return status;
3896}
3897EXPORT_SYMBOL_GPL(spi_write_then_read);
3898
3899/*-------------------------------------------------------------------------*/
3900
Marco Felsch5f143af2018-09-25 11:42:29 +02003901#if IS_ENABLED(CONFIG_OF)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003902/* must call put_device() when done with returned spi_device device */
Marco Felsch5f143af2018-09-25 11:42:29 +02003903struct spi_device *of_find_spi_device_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003904{
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +01003905 struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
3906
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003907 return dev ? to_spi_device(dev) : NULL;
3908}
Marco Felsch5f143af2018-09-25 11:42:29 +02003909EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
3910#endif /* IS_ENABLED(CONFIG_OF) */
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003911
Marco Felsch5f143af2018-09-25 11:42:29 +02003912#if IS_ENABLED(CONFIG_OF_DYNAMIC)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003913/* the spi controllers are not using spi_bus, so we find it with another way */
3914static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003915{
3916 struct device *dev;
3917
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +01003918 dev = class_find_device_by_of_node(&spi_master_class, node);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003919 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
Suzuki K Poulosecfba5de2019-07-23 23:18:33 +01003920 dev = class_find_device_by_of_node(&spi_slave_class, node);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003921 if (!dev)
3922 return NULL;
3923
3924 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003925 return container_of(dev, struct spi_controller, dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003926}
3927
3928static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3929 void *arg)
3930{
3931 struct of_reconfig_data *rd = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003932 struct spi_controller *ctlr;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003933 struct spi_device *spi;
3934
3935 switch (of_reconfig_get_state_change(action, arg)) {
3936 case OF_RECONFIG_CHANGE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003937 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3938 if (ctlr == NULL)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003939 return NOTIFY_OK; /* not for us */
3940
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003941 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003942 put_device(&ctlr->dev);
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003943 return NOTIFY_OK;
3944 }
3945
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003946 spi = of_register_spi_device(ctlr, rd->dn);
3947 put_device(&ctlr->dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003948
3949 if (IS_ERR(spi)) {
Rob Herring25c56c82017-07-18 16:43:31 -05003950 pr_err("%s: failed to create for '%pOF'\n",
3951 __func__, rd->dn);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003952 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003953 return notifier_from_errno(PTR_ERR(spi));
3954 }
3955 break;
3956
3957 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003958 /* already depopulated? */
3959 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3960 return NOTIFY_OK;
3961
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003962 /* find our device by node */
3963 spi = of_find_spi_device_by_node(rd->dn);
3964 if (spi == NULL)
3965 return NOTIFY_OK; /* no? not meant for us */
3966
3967 /* unregister takes one ref away */
3968 spi_unregister_device(spi);
3969
3970 /* and put the reference of the find */
3971 put_device(&spi->dev);
3972 break;
3973 }
3974
3975 return NOTIFY_OK;
3976}
3977
3978static struct notifier_block spi_of_notifier = {
3979 .notifier_call = of_spi_notify,
3980};
3981#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3982extern struct notifier_block spi_of_notifier;
3983#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3984
Octavian Purdila7f244672016-07-08 19:13:11 +03003985#if IS_ENABLED(CONFIG_ACPI)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003986static int spi_acpi_controller_match(struct device *dev, const void *data)
Octavian Purdila7f244672016-07-08 19:13:11 +03003987{
3988 return ACPI_COMPANION(dev->parent) == data;
3989}
3990
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003991static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
Octavian Purdila7f244672016-07-08 19:13:11 +03003992{
3993 struct device *dev;
3994
3995 dev = class_find_device(&spi_master_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003996 spi_acpi_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003997 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3998 dev = class_find_device(&spi_slave_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003999 spi_acpi_controller_match);
Octavian Purdila7f244672016-07-08 19:13:11 +03004000 if (!dev)
4001 return NULL;
4002
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004003 return container_of(dev, struct spi_controller, dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03004004}
4005
4006static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
4007{
4008 struct device *dev;
4009
Suzuki K Poulose00500142019-07-23 23:18:36 +01004010 dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03004011 return dev ? to_spi_device(dev) : NULL;
4012}
4013
4014static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
4015 void *arg)
4016{
4017 struct acpi_device *adev = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004018 struct spi_controller *ctlr;
Octavian Purdila7f244672016-07-08 19:13:11 +03004019 struct spi_device *spi;
4020
4021 switch (value) {
4022 case ACPI_RECONFIG_DEVICE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004023 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
4024 if (!ctlr)
Octavian Purdila7f244672016-07-08 19:13:11 +03004025 break;
4026
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02004027 acpi_register_spi_device(ctlr, adev);
4028 put_device(&ctlr->dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03004029 break;
4030 case ACPI_RECONFIG_DEVICE_REMOVE:
4031 if (!acpi_device_enumerated(adev))
4032 break;
4033
4034 spi = acpi_spi_find_device_by_adev(adev);
4035 if (!spi)
4036 break;
4037
4038 spi_unregister_device(spi);
4039 put_device(&spi->dev);
4040 break;
4041 }
4042
4043 return NOTIFY_OK;
4044}
4045
4046static struct notifier_block spi_acpi_notifier = {
4047 .notifier_call = acpi_spi_notify,
4048};
4049#else
4050extern struct notifier_block spi_acpi_notifier;
4051#endif
4052
David Brownell8ae12a02006-01-08 13:34:19 -08004053static int __init spi_init(void)
4054{
David Brownellb8852442006-01-08 13:34:23 -08004055 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08004056
Christoph Lametere94b1762006-12-06 20:33:17 -08004057 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08004058 if (!buf) {
4059 status = -ENOMEM;
4060 goto err0;
4061 }
4062
4063 status = bus_register(&spi_bus_type);
4064 if (status < 0)
4065 goto err1;
4066
4067 status = class_register(&spi_master_class);
4068 if (status < 0)
4069 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004070
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02004071 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
4072 status = class_register(&spi_slave_class);
4073 if (status < 0)
4074 goto err3;
4075 }
4076
Fabio Estevam52677202014-11-26 20:13:57 -02004077 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004078 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03004079 if (IS_ENABLED(CONFIG_ACPI))
4080 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02004081
David Brownell8ae12a02006-01-08 13:34:19 -08004082 return 0;
David Brownellb8852442006-01-08 13:34:23 -08004083
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02004084err3:
4085 class_unregister(&spi_master_class);
David Brownellb8852442006-01-08 13:34:23 -08004086err2:
4087 bus_unregister(&spi_bus_type);
4088err1:
4089 kfree(buf);
4090 buf = NULL;
4091err0:
4092 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08004093}
David Brownellb8852442006-01-08 13:34:23 -08004094
David Brownell8ae12a02006-01-08 13:34:19 -08004095/* board_info is normally registered in arch_initcall(),
4096 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08004097 *
4098 * REVISIT only boardinfo really needs static linking. the rest (device and
4099 * driver registration) _could_ be dynamically linked (modular) ... costs
4100 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08004101 */
David Brownell673c0c02008-10-15 22:02:46 -07004102postcore_initcall(spi_init);