blob: 4fcbb0aa71d361e4e5e50600a2d14a7722e22034 [file] [log] [blame]
David Brownell8ae12a02006-01-08 13:34:19 -08001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * SPI init/core code
David Brownell8ae12a02006-01-08 13:34:19 -08003 *
4 * Copyright (C) 2005 David Brownell
Grant Likelyd57a4282012-04-07 14:16:53 -06005 * Copyright (C) 2008 Secret Lab Technologies Ltd.
David Brownell8ae12a02006-01-08 13:34:19 -08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
David Brownell8ae12a02006-01-08 13:34:19 -080016 */
17
David Brownell8ae12a02006-01-08 13:34:19 -080018#include <linux/kernel.h>
19#include <linux/device.h>
20#include <linux/init.h>
21#include <linux/cache.h>
Mark Brown99adef32014-01-16 12:22:43 +000022#include <linux/dma-mapping.h>
23#include <linux/dmaengine.h>
Matthias Kaehlcke94040822007-07-17 04:04:16 -070024#include <linux/mutex.h>
Sinan Akman2b7a32f2010-10-02 21:28:29 -060025#include <linux/of_device.h>
Grant Likelyd57a4282012-04-07 14:16:53 -060026#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020027#include <linux/clk/clk-conf.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Anton Vorontsove0626e32009-09-22 16:46:08 -070029#include <linux/mod_devicetable.h>
David Brownell8ae12a02006-01-08 13:34:19 -080030#include <linux/spi/spi.h>
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +010031#include <linux/of_gpio.h>
Mark Brown3ae22e82010-12-25 15:32:27 +010032#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020033#include <linux/pm_domain.h>
Dmitry Torokhov826cf172017-02-28 14:25:18 -080034#include <linux/property.h>
Paul Gortmaker025ed132011-07-10 12:57:55 -040035#include <linux/export.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060036#include <linux/sched/rt.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010037#include <uapi/linux/sched/types.h>
Linus Walleijffbbdd212012-02-22 10:05:38 +010038#include <linux/delay.h>
39#include <linux/kthread.h>
Mika Westerberg64bee4d2012-11-30 12:37:53 +010040#include <linux/ioport.h>
41#include <linux/acpi.h>
Vignesh Rb1b81532016-08-17 15:22:36 +053042#include <linux/highmem.h>
David Brownell8ae12a02006-01-08 13:34:19 -080043
Mark Brown56ec1972013-10-07 19:33:53 +010044#define CREATE_TRACE_POINTS
45#include <trace/events/spi.h>
46
David Brownell8ae12a02006-01-08 13:34:19 -080047static void spidev_release(struct device *dev)
48{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080049 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080050
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020051 /* spi controllers may cleanup for released devices */
52 if (spi->controller->cleanup)
53 spi->controller->cleanup(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080054
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020055 spi_controller_put(spi->controller);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000056 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080057}
58
59static ssize_t
60modalias_show(struct device *dev, struct device_attribute *a, char *buf)
61{
62 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080063 int len;
64
65 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
66 if (len != -ENODEV)
67 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080068
Grant Likelyd8e328b2012-05-20 00:08:13 -060069 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080070}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070071static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080072
Martin Sperleca2ebc2015-06-22 13:00:36 +000073#define SPI_STATISTICS_ATTRS(field, file) \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020074static ssize_t spi_controller_##field##_show(struct device *dev, \
75 struct device_attribute *attr, \
76 char *buf) \
Martin Sperleca2ebc2015-06-22 13:00:36 +000077{ \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020078 struct spi_controller *ctlr = container_of(dev, \
79 struct spi_controller, dev); \
80 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
Martin Sperleca2ebc2015-06-22 13:00:36 +000081} \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020082static struct device_attribute dev_attr_spi_controller_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +020083 .attr = { .name = file, .mode = 0444 }, \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020084 .show = spi_controller_##field##_show, \
Martin Sperleca2ebc2015-06-22 13:00:36 +000085}; \
86static ssize_t spi_device_##field##_show(struct device *dev, \
87 struct device_attribute *attr, \
88 char *buf) \
89{ \
Geliang Tangd1eba932015-12-23 00:18:41 +080090 struct spi_device *spi = to_spi_device(dev); \
Martin Sperleca2ebc2015-06-22 13:00:36 +000091 return spi_statistics_##field##_show(&spi->statistics, buf); \
92} \
93static struct device_attribute dev_attr_spi_device_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +020094 .attr = { .name = file, .mode = 0444 }, \
Martin Sperleca2ebc2015-06-22 13:00:36 +000095 .show = spi_device_##field##_show, \
96}
97
98#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
99static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
100 char *buf) \
101{ \
102 unsigned long flags; \
103 ssize_t len; \
104 spin_lock_irqsave(&stat->lock, flags); \
105 len = sprintf(buf, format_string, stat->field); \
106 spin_unlock_irqrestore(&stat->lock, flags); \
107 return len; \
108} \
109SPI_STATISTICS_ATTRS(name, file)
110
111#define SPI_STATISTICS_SHOW(field, format_string) \
112 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
113 field, format_string)
114
115SPI_STATISTICS_SHOW(messages, "%lu");
116SPI_STATISTICS_SHOW(transfers, "%lu");
117SPI_STATISTICS_SHOW(errors, "%lu");
118SPI_STATISTICS_SHOW(timedout, "%lu");
119
120SPI_STATISTICS_SHOW(spi_sync, "%lu");
121SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
122SPI_STATISTICS_SHOW(spi_async, "%lu");
123
124SPI_STATISTICS_SHOW(bytes, "%llu");
125SPI_STATISTICS_SHOW(bytes_rx, "%llu");
126SPI_STATISTICS_SHOW(bytes_tx, "%llu");
127
Martin Sperl6b7bc062015-06-22 13:02:04 +0000128#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
129 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
130 "transfer_bytes_histo_" number, \
131 transfer_bytes_histo[index], "%lu")
132SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
133SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
134SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
135SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
136SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
137SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
138SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
139SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
140SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
141SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
142SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
143SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
144SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
145SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
146SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
147SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
148SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
149
Martin Sperld9f12122015-12-14 15:20:20 +0000150SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
151
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700152static struct attribute *spi_dev_attrs[] = {
153 &dev_attr_modalias.attr,
154 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800155};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000156
157static const struct attribute_group spi_dev_group = {
158 .attrs = spi_dev_attrs,
159};
160
161static struct attribute *spi_device_statistics_attrs[] = {
162 &dev_attr_spi_device_messages.attr,
163 &dev_attr_spi_device_transfers.attr,
164 &dev_attr_spi_device_errors.attr,
165 &dev_attr_spi_device_timedout.attr,
166 &dev_attr_spi_device_spi_sync.attr,
167 &dev_attr_spi_device_spi_sync_immediate.attr,
168 &dev_attr_spi_device_spi_async.attr,
169 &dev_attr_spi_device_bytes.attr,
170 &dev_attr_spi_device_bytes_rx.attr,
171 &dev_attr_spi_device_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000172 &dev_attr_spi_device_transfer_bytes_histo0.attr,
173 &dev_attr_spi_device_transfer_bytes_histo1.attr,
174 &dev_attr_spi_device_transfer_bytes_histo2.attr,
175 &dev_attr_spi_device_transfer_bytes_histo3.attr,
176 &dev_attr_spi_device_transfer_bytes_histo4.attr,
177 &dev_attr_spi_device_transfer_bytes_histo5.attr,
178 &dev_attr_spi_device_transfer_bytes_histo6.attr,
179 &dev_attr_spi_device_transfer_bytes_histo7.attr,
180 &dev_attr_spi_device_transfer_bytes_histo8.attr,
181 &dev_attr_spi_device_transfer_bytes_histo9.attr,
182 &dev_attr_spi_device_transfer_bytes_histo10.attr,
183 &dev_attr_spi_device_transfer_bytes_histo11.attr,
184 &dev_attr_spi_device_transfer_bytes_histo12.attr,
185 &dev_attr_spi_device_transfer_bytes_histo13.attr,
186 &dev_attr_spi_device_transfer_bytes_histo14.attr,
187 &dev_attr_spi_device_transfer_bytes_histo15.attr,
188 &dev_attr_spi_device_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000189 &dev_attr_spi_device_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000190 NULL,
191};
192
193static const struct attribute_group spi_device_statistics_group = {
194 .name = "statistics",
195 .attrs = spi_device_statistics_attrs,
196};
197
198static const struct attribute_group *spi_dev_groups[] = {
199 &spi_dev_group,
200 &spi_device_statistics_group,
201 NULL,
202};
203
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200204static struct attribute *spi_controller_statistics_attrs[] = {
205 &dev_attr_spi_controller_messages.attr,
206 &dev_attr_spi_controller_transfers.attr,
207 &dev_attr_spi_controller_errors.attr,
208 &dev_attr_spi_controller_timedout.attr,
209 &dev_attr_spi_controller_spi_sync.attr,
210 &dev_attr_spi_controller_spi_sync_immediate.attr,
211 &dev_attr_spi_controller_spi_async.attr,
212 &dev_attr_spi_controller_bytes.attr,
213 &dev_attr_spi_controller_bytes_rx.attr,
214 &dev_attr_spi_controller_bytes_tx.attr,
215 &dev_attr_spi_controller_transfer_bytes_histo0.attr,
216 &dev_attr_spi_controller_transfer_bytes_histo1.attr,
217 &dev_attr_spi_controller_transfer_bytes_histo2.attr,
218 &dev_attr_spi_controller_transfer_bytes_histo3.attr,
219 &dev_attr_spi_controller_transfer_bytes_histo4.attr,
220 &dev_attr_spi_controller_transfer_bytes_histo5.attr,
221 &dev_attr_spi_controller_transfer_bytes_histo6.attr,
222 &dev_attr_spi_controller_transfer_bytes_histo7.attr,
223 &dev_attr_spi_controller_transfer_bytes_histo8.attr,
224 &dev_attr_spi_controller_transfer_bytes_histo9.attr,
225 &dev_attr_spi_controller_transfer_bytes_histo10.attr,
226 &dev_attr_spi_controller_transfer_bytes_histo11.attr,
227 &dev_attr_spi_controller_transfer_bytes_histo12.attr,
228 &dev_attr_spi_controller_transfer_bytes_histo13.attr,
229 &dev_attr_spi_controller_transfer_bytes_histo14.attr,
230 &dev_attr_spi_controller_transfer_bytes_histo15.attr,
231 &dev_attr_spi_controller_transfer_bytes_histo16.attr,
232 &dev_attr_spi_controller_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000233 NULL,
234};
235
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200236static const struct attribute_group spi_controller_statistics_group = {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000237 .name = "statistics",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200238 .attrs = spi_controller_statistics_attrs,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000239};
240
241static const struct attribute_group *spi_master_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200242 &spi_controller_statistics_group,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000243 NULL,
244};
245
246void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
247 struct spi_transfer *xfer,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200248 struct spi_controller *ctlr)
Martin Sperleca2ebc2015-06-22 13:00:36 +0000249{
250 unsigned long flags;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000251 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
252
253 if (l2len < 0)
254 l2len = 0;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000255
256 spin_lock_irqsave(&stats->lock, flags);
257
258 stats->transfers++;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000259 stats->transfer_bytes_histo[l2len]++;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000260
261 stats->bytes += xfer->len;
262 if ((xfer->tx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200263 (xfer->tx_buf != ctlr->dummy_tx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000264 stats->bytes_tx += xfer->len;
265 if ((xfer->rx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200266 (xfer->rx_buf != ctlr->dummy_rx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000267 stats->bytes_rx += xfer->len;
268
269 spin_unlock_irqrestore(&stats->lock, flags);
270}
271EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800272
273/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
274 * and the sysfs version makes coldplug work too.
275 */
276
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700277static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
278 const struct spi_device *sdev)
279{
280 while (id->name[0]) {
281 if (!strcmp(sdev->modalias, id->name))
282 return id;
283 id++;
284 }
285 return NULL;
286}
287
288const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
289{
290 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
291
292 return spi_match_id(sdrv->id_table, sdev);
293}
294EXPORT_SYMBOL_GPL(spi_get_device_id);
295
David Brownell8ae12a02006-01-08 13:34:19 -0800296static int spi_match_device(struct device *dev, struct device_driver *drv)
297{
298 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700299 const struct spi_driver *sdrv = to_spi_driver(drv);
300
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600301 /* Attempt an OF style match */
302 if (of_driver_match_device(dev, drv))
303 return 1;
304
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100305 /* Then try ACPI */
306 if (acpi_driver_match_device(dev, drv))
307 return 1;
308
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700309 if (sdrv->id_table)
310 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800311
Kay Sievers35f74fc2009-01-06 10:44:37 -0800312 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800313}
314
Kay Sievers7eff2e72007-08-14 15:15:12 +0200315static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800316{
317 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800318 int rc;
319
320 rc = acpi_device_uevent_modalias(dev, env);
321 if (rc != -ENODEV)
322 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800323
Anton Vorontsove0626e32009-09-22 16:46:08 -0700324 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800325 return 0;
326}
327
David Brownell8ae12a02006-01-08 13:34:19 -0800328struct bus_type spi_bus_type = {
329 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700330 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800331 .match = spi_match_device,
332 .uevent = spi_uevent,
David Brownell8ae12a02006-01-08 13:34:19 -0800333};
334EXPORT_SYMBOL_GPL(spi_bus_type);
335
David Brownellb8852442006-01-08 13:34:23 -0800336
337static int spi_drv_probe(struct device *dev)
338{
339 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Jon Hunter44af7922015-10-09 15:45:55 +0100340 struct spi_device *spi = to_spi_device(dev);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300341 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800342
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200343 ret = of_clk_set_defaults(dev->of_node, false);
344 if (ret)
345 return ret;
346
Jon Hunter44af7922015-10-09 15:45:55 +0100347 if (dev->of_node) {
348 spi->irq = of_irq_get(dev->of_node, 0);
349 if (spi->irq == -EPROBE_DEFER)
350 return -EPROBE_DEFER;
351 if (spi->irq < 0)
352 spi->irq = 0;
353 }
354
Ulf Hansson676e7c22014-09-19 20:27:41 +0200355 ret = dev_pm_domain_attach(dev, true);
356 if (ret != -EPROBE_DEFER) {
Jon Hunter44af7922015-10-09 15:45:55 +0100357 ret = sdrv->probe(spi);
Ulf Hansson676e7c22014-09-19 20:27:41 +0200358 if (ret)
359 dev_pm_domain_detach(dev, true);
360 }
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300361
362 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800363}
364
365static int spi_drv_remove(struct device *dev)
366{
367 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300368 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800369
Jean Delvareaec35f42014-02-13 15:28:41 +0100370 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200371 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300372
373 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800374}
375
376static void spi_drv_shutdown(struct device *dev)
377{
378 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
379
380 sdrv->shutdown(to_spi_device(dev));
381}
382
David Brownell33e34dc2007-05-08 00:32:21 -0700383/**
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500384 * __spi_register_driver - register a SPI driver
Thierry Reding88c93212015-11-10 13:03:04 +0100385 * @owner: owner module of the driver to register
David Brownell33e34dc2007-05-08 00:32:21 -0700386 * @sdrv: the driver to register
387 * Context: can sleep
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200388 *
389 * Return: zero on success, else a negative error code.
David Brownell33e34dc2007-05-08 00:32:21 -0700390 */
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500391int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
David Brownellb8852442006-01-08 13:34:23 -0800392{
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500393 sdrv->driver.owner = owner;
David Brownellb8852442006-01-08 13:34:23 -0800394 sdrv->driver.bus = &spi_bus_type;
395 if (sdrv->probe)
396 sdrv->driver.probe = spi_drv_probe;
397 if (sdrv->remove)
398 sdrv->driver.remove = spi_drv_remove;
399 if (sdrv->shutdown)
400 sdrv->driver.shutdown = spi_drv_shutdown;
401 return driver_register(&sdrv->driver);
402}
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500403EXPORT_SYMBOL_GPL(__spi_register_driver);
David Brownellb8852442006-01-08 13:34:23 -0800404
David Brownell8ae12a02006-01-08 13:34:19 -0800405/*-------------------------------------------------------------------------*/
406
407/* SPI devices should normally not be created by SPI device drivers; that
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200408 * would make them board-specific. Similarly with SPI controller drivers.
David Brownell8ae12a02006-01-08 13:34:19 -0800409 * Device registration normally goes into like arch/.../mach.../board-YYY.c
410 * with other readonly (flashable) information about mainboard devices.
411 */
412
413struct boardinfo {
414 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800415 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800416};
417
418static LIST_HEAD(board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200419static LIST_HEAD(spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800420
421/*
422 * Used to protect add/del opertion for board_info list and
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200423 * spi_controller list, and their matching process
Feng Tang2b9603a2010-08-02 15:52:15 +0800424 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700425static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800426
Grant Likelydc87c982008-05-15 16:50:22 -0600427/**
428 * spi_alloc_device - Allocate a new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200429 * @ctlr: Controller to which device is connected
Grant Likelydc87c982008-05-15 16:50:22 -0600430 * Context: can sleep
431 *
432 * Allows a driver to allocate and initialize a spi_device without
433 * registering it immediately. This allows a driver to directly
434 * fill the spi_device with device parameters before calling
435 * spi_add_device() on it.
436 *
437 * Caller is responsible to call spi_add_device() on the returned
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200438 * spi_device structure to add it to the SPI controller. If the caller
Grant Likelydc87c982008-05-15 16:50:22 -0600439 * needs to discard the spi_device without adding it, then it should
440 * call spi_dev_put() on it.
441 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200442 * Return: a pointer to the new device, or NULL.
Grant Likelydc87c982008-05-15 16:50:22 -0600443 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200444struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
Grant Likelydc87c982008-05-15 16:50:22 -0600445{
446 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600447
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200448 if (!spi_controller_get(ctlr))
Grant Likelydc87c982008-05-15 16:50:22 -0600449 return NULL;
450
Jingoo Han5fe5f052013-10-14 10:31:51 +0900451 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600452 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200453 spi_controller_put(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600454 return NULL;
455 }
456
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200457 spi->master = spi->controller = ctlr;
458 spi->dev.parent = &ctlr->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600459 spi->dev.bus = &spi_bus_type;
460 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100461 spi->cs_gpio = -ENOENT;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000462
463 spin_lock_init(&spi->statistics.lock);
464
Grant Likelydc87c982008-05-15 16:50:22 -0600465 device_initialize(&spi->dev);
466 return spi;
467}
468EXPORT_SYMBOL_GPL(spi_alloc_device);
469
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200470static void spi_dev_set_name(struct spi_device *spi)
471{
472 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
473
474 if (adev) {
475 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
476 return;
477 }
478
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200479 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200480 spi->chip_select);
481}
482
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200483static int spi_dev_check(struct device *dev, void *data)
484{
485 struct spi_device *spi = to_spi_device(dev);
486 struct spi_device *new_spi = data;
487
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200488 if (spi->controller == new_spi->controller &&
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200489 spi->chip_select == new_spi->chip_select)
490 return -EBUSY;
491 return 0;
492}
493
Grant Likelydc87c982008-05-15 16:50:22 -0600494/**
495 * spi_add_device - Add spi_device allocated with spi_alloc_device
496 * @spi: spi_device to register
497 *
498 * Companion function to spi_alloc_device. Devices allocated with
499 * spi_alloc_device can be added onto the spi bus with this function.
500 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200501 * Return: 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600502 */
503int spi_add_device(struct spi_device *spi)
504{
David Brownelle48880e2008-08-15 00:40:44 -0700505 static DEFINE_MUTEX(spi_add_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200506 struct spi_controller *ctlr = spi->controller;
507 struct device *dev = ctlr->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600508 int status;
509
510 /* Chipselects are numbered 0..max; validate. */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200511 if (spi->chip_select >= ctlr->num_chipselect) {
512 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
513 ctlr->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600514 return -EINVAL;
515 }
516
517 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200518 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700519
520 /* We need to make sure there's no other device with this
521 * chipselect **BEFORE** we call setup(), else we'll trash
522 * its configuration. Lock against concurrent add() calls.
523 */
524 mutex_lock(&spi_add_lock);
525
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200526 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
527 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700528 dev_err(dev, "chipselect %d already in use\n",
529 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700530 goto done;
531 }
532
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200533 if (ctlr->cs_gpios)
534 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100535
David Brownelle48880e2008-08-15 00:40:44 -0700536 /* Drivers may modify this initial i/o setup, but will
537 * normally rely on the device being setup. Devices
538 * using SPI_CS_HIGH can't coexist well otherwise...
539 */
David Brownell7d077192009-06-17 16:26:03 -0700540 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600541 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200542 dev_err(dev, "can't setup %s, status %d\n",
543 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700544 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600545 }
546
David Brownelle48880e2008-08-15 00:40:44 -0700547 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600548 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700549 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200550 dev_err(dev, "can't add %s, status %d\n",
551 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700552 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800553 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600554
David Brownelle48880e2008-08-15 00:40:44 -0700555done:
556 mutex_unlock(&spi_add_lock);
557 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600558}
559EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800560
David Brownell33e34dc2007-05-08 00:32:21 -0700561/**
562 * spi_new_device - instantiate one new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200563 * @ctlr: Controller to which device is connected
David Brownell33e34dc2007-05-08 00:32:21 -0700564 * @chip: Describes the SPI device
565 * Context: can sleep
566 *
567 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800568 * after board init creates the hard-wired devices. Some development
569 * platforms may not be able to use spi_register_board_info though, and
570 * this is exported so that for example a USB or parport based adapter
571 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700572 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200573 * Return: the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800574 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200575struct spi_device *spi_new_device(struct spi_controller *ctlr,
Adrian Bunke9d5a462007-03-26 21:32:23 -0800576 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800577{
578 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800579 int status;
580
David Brownell082c8cb2007-07-31 00:39:45 -0700581 /* NOTE: caller did any chip->bus_num checks necessary.
582 *
583 * Also, unless we change the return value convention to use
584 * error-or-pointer (not NULL-or-pointer), troubleshootability
585 * suggests syslogged diagnostics are best here (ugh).
586 */
587
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200588 proxy = spi_alloc_device(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600589 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800590 return NULL;
591
Grant Likely102eb972008-07-23 21:29:55 -0700592 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
593
David Brownell8ae12a02006-01-08 13:34:19 -0800594 proxy->chip_select = chip->chip_select;
595 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700596 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800597 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700598 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800599 proxy->dev.platform_data = (void *) chip->platform_data;
600 proxy->controller_data = chip->controller_data;
601 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800602
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800603 if (chip->properties) {
604 status = device_add_properties(&proxy->dev, chip->properties);
605 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200606 dev_err(&ctlr->dev,
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800607 "failed to add properties to '%s': %d\n",
608 chip->modalias, status);
609 goto err_dev_put;
610 }
David Brownell8ae12a02006-01-08 13:34:19 -0800611 }
612
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800613 status = spi_add_device(proxy);
614 if (status < 0)
615 goto err_remove_props;
616
David Brownell8ae12a02006-01-08 13:34:19 -0800617 return proxy;
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800618
619err_remove_props:
620 if (chip->properties)
621 device_remove_properties(&proxy->dev);
622err_dev_put:
623 spi_dev_put(proxy);
624 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800625}
626EXPORT_SYMBOL_GPL(spi_new_device);
627
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100628/**
629 * spi_unregister_device - unregister a single SPI device
630 * @spi: spi_device to unregister
631 *
632 * Start making the passed SPI device vanish. Normally this would be handled
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200633 * by spi_unregister_controller().
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100634 */
635void spi_unregister_device(struct spi_device *spi)
636{
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100637 if (!spi)
638 return;
639
Johan Hovold83241472017-01-30 17:47:05 +0100640 if (spi->dev.of_node) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100641 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
Johan Hovold83241472017-01-30 17:47:05 +0100642 of_node_put(spi->dev.of_node);
643 }
Octavian Purdila7f244672016-07-08 19:13:11 +0300644 if (ACPI_COMPANION(&spi->dev))
645 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100646 device_unregister(&spi->dev);
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100647}
648EXPORT_SYMBOL_GPL(spi_unregister_device);
649
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200650static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
651 struct spi_board_info *bi)
Feng Tang2b9603a2010-08-02 15:52:15 +0800652{
653 struct spi_device *dev;
654
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200655 if (ctlr->bus_num != bi->bus_num)
Feng Tang2b9603a2010-08-02 15:52:15 +0800656 return;
657
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200658 dev = spi_new_device(ctlr, bi);
Feng Tang2b9603a2010-08-02 15:52:15 +0800659 if (!dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200660 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
Feng Tang2b9603a2010-08-02 15:52:15 +0800661 bi->modalias);
662}
663
David Brownell33e34dc2007-05-08 00:32:21 -0700664/**
665 * spi_register_board_info - register SPI devices for a given board
666 * @info: array of chip descriptors
667 * @n: how many descriptors are provided
668 * Context: can sleep
669 *
David Brownell8ae12a02006-01-08 13:34:19 -0800670 * Board-specific early init code calls this (probably during arch_initcall)
671 * with segments of the SPI device table. Any device nodes are created later,
672 * after the relevant parent SPI controller (bus_num) is defined. We keep
673 * this table of devices forever, so that reloading a controller driver will
674 * not make Linux forget about these hard-wired devices.
675 *
676 * Other code can also call this, e.g. a particular add-on board might provide
677 * SPI devices through its expansion connector, so code initializing that board
678 * would naturally declare its SPI devices.
679 *
680 * The board info passed can safely be __initdata ... but be careful of
681 * any embedded pointers (platform_data, etc), they're copied as-is.
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800682 * Device properties are deep-copied though.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200683 *
684 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -0800685 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000686int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800687{
Feng Tang2b9603a2010-08-02 15:52:15 +0800688 struct boardinfo *bi;
689 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800690
Xiubo Lic7908a32014-09-24 14:30:29 +0800691 if (!n)
Dmitry Torokhovf974cf52017-02-28 14:25:19 -0800692 return 0;
Xiubo Lic7908a32014-09-24 14:30:29 +0800693
Markus Elfringf9bdb7f2017-01-13 12:28:04 +0100694 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800695 if (!bi)
696 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800697
Feng Tang2b9603a2010-08-02 15:52:15 +0800698 for (i = 0; i < n; i++, bi++, info++) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200699 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -0800700
Feng Tang2b9603a2010-08-02 15:52:15 +0800701 memcpy(&bi->board_info, info, sizeof(*info));
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800702 if (info->properties) {
703 bi->board_info.properties =
704 property_entries_dup(info->properties);
705 if (IS_ERR(bi->board_info.properties))
706 return PTR_ERR(bi->board_info.properties);
707 }
708
Feng Tang2b9603a2010-08-02 15:52:15 +0800709 mutex_lock(&board_lock);
710 list_add_tail(&bi->list, &board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200711 list_for_each_entry(ctlr, &spi_controller_list, list)
712 spi_match_controller_to_boardinfo(ctlr,
713 &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +0800714 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800715 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800716
717 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800718}
719
720/*-------------------------------------------------------------------------*/
721
Mark Brownb1589352013-10-05 11:50:40 +0100722static void spi_set_cs(struct spi_device *spi, bool enable)
723{
724 if (spi->mode & SPI_CS_HIGH)
725 enable = !enable;
726
Thor Thayer8eee6b92016-10-10 09:25:24 -0500727 if (gpio_is_valid(spi->cs_gpio)) {
Mark Brownb1589352013-10-05 11:50:40 +0100728 gpio_set_value(spi->cs_gpio, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500729 /* Some SPI masters need both GPIO CS & slave_select */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200730 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
731 spi->controller->set_cs)
732 spi->controller->set_cs(spi, !enable);
733 } else if (spi->controller->set_cs) {
734 spi->controller->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500735 }
Mark Brownb1589352013-10-05 11:50:40 +0100736}
737
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200738#ifdef CONFIG_HAS_DMA
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200739static int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
Mark Brown6ad45a22014-02-02 13:47:47 +0000740 struct sg_table *sgt, void *buf, size_t len,
741 enum dma_data_direction dir)
742{
743 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200744 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530745#ifdef CONFIG_HIGHMEM
746 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
747 (unsigned long)buf < (PKMAP_BASE +
748 (LAST_PKMAP * PAGE_SIZE)));
749#else
750 const bool kmap_buf = false;
751#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500752 int desc_len;
753 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000754 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600755 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000756 void *sg_buf;
757 size_t min;
758 int i, ret;
759
Vignesh Rb1b81532016-08-17 15:22:36 +0530760 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200761 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500762 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530763 } else if (virt_addr_valid(buf)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200764 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500765 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530766 } else {
767 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500768 }
769
Mark Brown6ad45a22014-02-02 13:47:47 +0000770 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
771 if (ret != 0)
772 return ret;
773
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600774 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000775 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000776
Vignesh Rb1b81532016-08-17 15:22:36 +0530777 if (vmalloced_buf || kmap_buf) {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500778 min = min_t(size_t,
779 len, desc_len - offset_in_page(buf));
Vignesh Rb1b81532016-08-17 15:22:36 +0530780 if (vmalloced_buf)
781 vm_page = vmalloc_to_page(buf);
782 else
783 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000784 if (!vm_page) {
785 sg_free_table(sgt);
786 return -ENOMEM;
787 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600788 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000789 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000790 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500791 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000792 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600793 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000794 }
795
Mark Brown6ad45a22014-02-02 13:47:47 +0000796 buf += min;
797 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600798 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000799 }
800
801 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200802 if (!ret)
803 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000804 if (ret < 0) {
805 sg_free_table(sgt);
806 return ret;
807 }
808
809 sgt->nents = ret;
810
811 return 0;
812}
813
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200814static void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
Mark Brown6ad45a22014-02-02 13:47:47 +0000815 struct sg_table *sgt, enum dma_data_direction dir)
816{
817 if (sgt->orig_nents) {
818 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
819 sg_free_table(sgt);
820 }
821}
822
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200823static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000824{
Mark Brown99adef32014-01-16 12:22:43 +0000825 struct device *tx_dev, *rx_dev;
826 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000827 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000828
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200829 if (!ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000830 return 0;
831
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200832 if (ctlr->dma_tx)
833 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800834 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200835 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800836
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200837 if (ctlr->dma_rx)
838 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800839 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200840 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000841
842 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200843 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000844 continue;
845
846 if (xfer->tx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200847 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000848 (void *)xfer->tx_buf, xfer->len,
849 DMA_TO_DEVICE);
850 if (ret != 0)
851 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000852 }
853
854 if (xfer->rx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200855 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000856 xfer->rx_buf, xfer->len,
857 DMA_FROM_DEVICE);
858 if (ret != 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200859 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000860 DMA_TO_DEVICE);
861 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000862 }
863 }
864 }
865
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200866 ctlr->cur_msg_mapped = true;
Mark Brown99adef32014-01-16 12:22:43 +0000867
868 return 0;
869}
870
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200871static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000872{
873 struct spi_transfer *xfer;
874 struct device *tx_dev, *rx_dev;
875
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200876 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000877 return 0;
878
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200879 if (ctlr->dma_tx)
880 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800881 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200882 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800883
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200884 if (ctlr->dma_rx)
885 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800886 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200887 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000888
889 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200890 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000891 continue;
892
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200893 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
894 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000895 }
896
897 return 0;
898}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200899#else /* !CONFIG_HAS_DMA */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200900static inline int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
901 struct sg_table *sgt, void *buf, size_t len,
Vignesh Rf4502dd2016-06-08 12:18:31 +0530902 enum dma_data_direction dir)
903{
904 return -EINVAL;
905}
906
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200907static inline void spi_unmap_buf(struct spi_controller *ctlr,
Vignesh Rf4502dd2016-06-08 12:18:31 +0530908 struct device *dev, struct sg_table *sgt,
909 enum dma_data_direction dir)
910{
911}
912
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200913static inline int __spi_map_msg(struct spi_controller *ctlr,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200914 struct spi_message *msg)
915{
916 return 0;
917}
918
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200919static inline int __spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000920 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200921{
922 return 0;
923}
924#endif /* !CONFIG_HAS_DMA */
925
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200926static inline int spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000927 struct spi_message *msg)
928{
929 struct spi_transfer *xfer;
930
931 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
932 /*
933 * Restore the original value of tx_buf or rx_buf if they are
934 * NULL.
935 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200936 if (xfer->tx_buf == ctlr->dummy_tx)
Martin Sperl4b786452015-05-25 10:13:10 +0000937 xfer->tx_buf = NULL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200938 if (xfer->rx_buf == ctlr->dummy_rx)
Martin Sperl4b786452015-05-25 10:13:10 +0000939 xfer->rx_buf = NULL;
940 }
941
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200942 return __spi_unmap_msg(ctlr, msg);
Martin Sperl4b786452015-05-25 10:13:10 +0000943}
944
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200945static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200946{
947 struct spi_transfer *xfer;
948 void *tmp;
949 unsigned int max_tx, max_rx;
950
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200951 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200952 max_tx = 0;
953 max_rx = 0;
954
955 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200956 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200957 !xfer->tx_buf)
958 max_tx = max(xfer->len, max_tx);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200959 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200960 !xfer->rx_buf)
961 max_rx = max(xfer->len, max_rx);
962 }
963
964 if (max_tx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200965 tmp = krealloc(ctlr->dummy_tx, max_tx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200966 GFP_KERNEL | GFP_DMA);
967 if (!tmp)
968 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200969 ctlr->dummy_tx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200970 memset(tmp, 0, max_tx);
971 }
972
973 if (max_rx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200974 tmp = krealloc(ctlr->dummy_rx, max_rx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200975 GFP_KERNEL | GFP_DMA);
976 if (!tmp)
977 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200978 ctlr->dummy_rx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200979 }
980
981 if (max_tx || max_rx) {
982 list_for_each_entry(xfer, &msg->transfers,
983 transfer_list) {
984 if (!xfer->tx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200985 xfer->tx_buf = ctlr->dummy_tx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200986 if (!xfer->rx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200987 xfer->rx_buf = ctlr->dummy_rx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200988 }
989 }
990 }
991
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200992 return __spi_map_msg(ctlr, msg);
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200993}
Mark Brown99adef32014-01-16 12:22:43 +0000994
Mark Brownb1589352013-10-05 11:50:40 +0100995/*
996 * spi_transfer_one_message - Default implementation of transfer_one_message()
997 *
998 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -0700999 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +01001000 * standard handling of delays and chip select management.
1001 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001002static int spi_transfer_one_message(struct spi_controller *ctlr,
Mark Brownb1589352013-10-05 11:50:40 +01001003 struct spi_message *msg)
1004{
1005 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +01001006 bool keep_cs = false;
1007 int ret = 0;
Sien Wud0716dd2016-09-01 18:24:29 -05001008 unsigned long long ms = 1;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001009 struct spi_statistics *statm = &ctlr->statistics;
Martin Sperleca2ebc2015-06-22 13:00:36 +00001010 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +01001011
1012 spi_set_cs(msg->spi, true);
1013
Martin Sperleca2ebc2015-06-22 13:00:36 +00001014 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1015 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1016
Mark Brownb1589352013-10-05 11:50:40 +01001017 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1018 trace_spi_transfer_start(msg, xfer);
1019
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001020 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1021 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
Martin Sperleca2ebc2015-06-22 13:00:36 +00001022
Mark Brown38ec10f2014-08-16 16:27:41 +01001023 if (xfer->tx_buf || xfer->rx_buf) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001024 reinit_completion(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001025
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001026 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
Mark Brown38ec10f2014-08-16 16:27:41 +01001027 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001028 SPI_STATISTICS_INCREMENT_FIELD(statm,
1029 errors);
1030 SPI_STATISTICS_INCREMENT_FIELD(stats,
1031 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001032 dev_err(&msg->spi->dev,
1033 "SPI transfer failed: %d\n", ret);
1034 goto out;
1035 }
Mark Brownb1589352013-10-05 11:50:40 +01001036
Mark Brown38ec10f2014-08-16 16:27:41 +01001037 if (ret > 0) {
1038 ret = 0;
Sien Wud0716dd2016-09-01 18:24:29 -05001039 ms = 8LL * 1000LL * xfer->len;
1040 do_div(ms, xfer->speed_hz);
Hauke Mehrtens833bfad2017-04-17 01:38:05 +02001041 ms += ms + 200; /* some tolerance */
Mark Brown16a0ce42014-01-30 22:16:41 +00001042
Sien Wud0716dd2016-09-01 18:24:29 -05001043 if (ms > UINT_MAX)
1044 ms = UINT_MAX;
1045
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001046 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
Mark Brown38ec10f2014-08-16 16:27:41 +01001047 msecs_to_jiffies(ms));
1048 }
Mark Brown16a0ce42014-01-30 22:16:41 +00001049
Mark Brown38ec10f2014-08-16 16:27:41 +01001050 if (ms == 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001051 SPI_STATISTICS_INCREMENT_FIELD(statm,
1052 timedout);
1053 SPI_STATISTICS_INCREMENT_FIELD(stats,
1054 timedout);
Mark Brown38ec10f2014-08-16 16:27:41 +01001055 dev_err(&msg->spi->dev,
1056 "SPI transfer timed out\n");
1057 msg->status = -ETIMEDOUT;
1058 }
1059 } else {
1060 if (xfer->len)
1061 dev_err(&msg->spi->dev,
1062 "Bufferless transfer has length %u\n",
1063 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001064 }
Mark Brownb1589352013-10-05 11:50:40 +01001065
1066 trace_spi_transfer_stop(msg, xfer);
1067
1068 if (msg->status != -EINPROGRESS)
1069 goto out;
1070
Daniel Kurtz8244bd32016-10-07 18:55:47 +08001071 if (xfer->delay_usecs) {
1072 u16 us = xfer->delay_usecs;
1073
1074 if (us <= 10)
1075 udelay(us);
1076 else
1077 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1078 }
Mark Brownb1589352013-10-05 11:50:40 +01001079
1080 if (xfer->cs_change) {
1081 if (list_is_last(&xfer->transfer_list,
1082 &msg->transfers)) {
1083 keep_cs = true;
1084 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001085 spi_set_cs(msg->spi, false);
1086 udelay(10);
1087 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001088 }
1089 }
1090
1091 msg->actual_length += xfer->len;
1092 }
1093
1094out:
1095 if (ret != 0 || !keep_cs)
1096 spi_set_cs(msg->spi, false);
1097
1098 if (msg->status == -EINPROGRESS)
1099 msg->status = ret;
1100
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001101 if (msg->status && ctlr->handle_err)
1102 ctlr->handle_err(ctlr, msg);
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001103
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001104 spi_res_release(ctlr, msg);
Martin Sperld780c372015-12-14 15:20:18 +00001105
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001106 spi_finalize_current_message(ctlr);
Mark Brownb1589352013-10-05 11:50:40 +01001107
1108 return ret;
1109}
1110
1111/**
1112 * spi_finalize_current_transfer - report completion of a transfer
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001113 * @ctlr: the controller reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001114 *
1115 * Called by SPI drivers using the core transfer_one_message()
1116 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001117 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001118 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001119void spi_finalize_current_transfer(struct spi_controller *ctlr)
Mark Brownb1589352013-10-05 11:50:40 +01001120{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001121 complete(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001122}
1123EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1124
Linus Walleijffbbdd212012-02-22 10:05:38 +01001125/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001126 * __spi_pump_messages - function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001127 * @ctlr: controller to process queue for
Mark Brownfc9e0f72014-12-10 13:46:33 +00001128 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001129 *
1130 * This function checks if there is any spi message in the queue that
1131 * needs processing and if so call out to the driver to initialize hardware
1132 * and transfer each message.
1133 *
Mark Brown0461a412014-12-09 21:38:05 +00001134 * Note that it is called both from the kthread itself and also from
1135 * inside spi_sync(); the queue extraction handling at the top of the
1136 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001137 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001138static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001139{
Linus Walleijffbbdd212012-02-22 10:05:38 +01001140 unsigned long flags;
1141 bool was_busy = false;
1142 int ret;
1143
Mark Brown983aee52014-12-09 19:46:56 +00001144 /* Lock queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001145 spin_lock_irqsave(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001146
1147 /* Make sure we are not already running a message */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001148 if (ctlr->cur_msg) {
1149 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001150 return;
1151 }
1152
Mark Brown0461a412014-12-09 21:38:05 +00001153 /* If another context is idling the device then defer */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001154 if (ctlr->idling) {
1155 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1156 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001157 return;
1158 }
1159
Mark Brown983aee52014-12-09 19:46:56 +00001160 /* Check if the queue is idle */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001161 if (list_empty(&ctlr->queue) || !ctlr->running) {
1162 if (!ctlr->busy) {
1163 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -07001164 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001165 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001166
1167 /* Only do teardown in the thread */
1168 if (!in_kthread) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001169 kthread_queue_work(&ctlr->kworker,
1170 &ctlr->pump_messages);
1171 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001172 return;
1173 }
1174
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001175 ctlr->busy = false;
1176 ctlr->idling = true;
1177 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001178
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001179 kfree(ctlr->dummy_rx);
1180 ctlr->dummy_rx = NULL;
1181 kfree(ctlr->dummy_tx);
1182 ctlr->dummy_tx = NULL;
1183 if (ctlr->unprepare_transfer_hardware &&
1184 ctlr->unprepare_transfer_hardware(ctlr))
1185 dev_err(&ctlr->dev,
Bryan Freedb0b36b82013-03-13 11:17:40 -07001186 "failed to unprepare transfer hardware\n");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001187 if (ctlr->auto_runtime_pm) {
1188 pm_runtime_mark_last_busy(ctlr->dev.parent);
1189 pm_runtime_put_autosuspend(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001190 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001191 trace_spi_controller_idle(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001192
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001193 spin_lock_irqsave(&ctlr->queue_lock, flags);
1194 ctlr->idling = false;
1195 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001196 return;
1197 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001198
Linus Walleijffbbdd212012-02-22 10:05:38 +01001199 /* Extract head of queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001200 ctlr->cur_msg =
1201 list_first_entry(&ctlr->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001202
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001203 list_del_init(&ctlr->cur_msg->queue);
1204 if (ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001205 was_busy = true;
1206 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001207 ctlr->busy = true;
1208 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001209
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001210 mutex_lock(&ctlr->io_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001211
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001212 if (!was_busy && ctlr->auto_runtime_pm) {
1213 ret = pm_runtime_get_sync(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001214 if (ret < 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001215 dev_err(&ctlr->dev, "Failed to power device: %d\n",
Mark Brown49834de2013-07-28 14:47:02 +01001216 ret);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001217 mutex_unlock(&ctlr->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001218 return;
1219 }
1220 }
1221
Mark Brown56ec1972013-10-07 19:33:53 +01001222 if (!was_busy)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001223 trace_spi_controller_busy(ctlr);
Mark Brown56ec1972013-10-07 19:33:53 +01001224
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001225 if (!was_busy && ctlr->prepare_transfer_hardware) {
1226 ret = ctlr->prepare_transfer_hardware(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001227 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001228 dev_err(&ctlr->dev,
Linus Walleijffbbdd212012-02-22 10:05:38 +01001229 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001230
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001231 if (ctlr->auto_runtime_pm)
1232 pm_runtime_put(ctlr->dev.parent);
1233 mutex_unlock(&ctlr->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001234 return;
1235 }
1236 }
1237
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001238 trace_spi_message_start(ctlr->cur_msg);
Mark Brown56ec1972013-10-07 19:33:53 +01001239
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001240 if (ctlr->prepare_message) {
1241 ret = ctlr->prepare_message(ctlr, ctlr->cur_msg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001242 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001243 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1244 ret);
1245 ctlr->cur_msg->status = ret;
1246 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001247 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001248 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001249 ctlr->cur_msg_prepared = true;
Mark Brown2841a5f2013-10-05 00:23:12 +01001250 }
1251
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001252 ret = spi_map_msg(ctlr, ctlr->cur_msg);
Mark Brown99adef32014-01-16 12:22:43 +00001253 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001254 ctlr->cur_msg->status = ret;
1255 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001256 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001257 }
1258
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001259 ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001260 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001261 dev_err(&ctlr->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001262 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001263 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001264 }
Jon Hunter49023d22016-03-08 12:28:20 +00001265
1266out:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001267 mutex_unlock(&ctlr->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001268
1269 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001270 if (!ret)
1271 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001272}
1273
Mark Brownfc9e0f72014-12-10 13:46:33 +00001274/**
1275 * spi_pump_messages - kthread work function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001276 * @work: pointer to kthread work struct contained in the controller struct
Mark Brownfc9e0f72014-12-10 13:46:33 +00001277 */
1278static void spi_pump_messages(struct kthread_work *work)
1279{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001280 struct spi_controller *ctlr =
1281 container_of(work, struct spi_controller, pump_messages);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001282
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001283 __spi_pump_messages(ctlr, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001284}
1285
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001286static int spi_init_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001287{
1288 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1289
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001290 ctlr->running = false;
1291 ctlr->busy = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001292
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001293 kthread_init_worker(&ctlr->kworker);
1294 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1295 "%s", dev_name(&ctlr->dev));
1296 if (IS_ERR(ctlr->kworker_task)) {
1297 dev_err(&ctlr->dev, "failed to create message pump task\n");
1298 return PTR_ERR(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001299 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001300 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001301
1302 /*
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001303 * Controller config will indicate if this controller should run the
Linus Walleijffbbdd212012-02-22 10:05:38 +01001304 * message pump with high (realtime) priority to reduce the transfer
1305 * latency on the bus by minimising the delay between a transfer
1306 * request and the scheduling of the message pump thread. Without this
1307 * setting the message pump thread will remain at default priority.
1308 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001309 if (ctlr->rt) {
1310 dev_info(&ctlr->dev,
Linus Walleijffbbdd212012-02-22 10:05:38 +01001311 "will run message pump with realtime priority\n");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001312 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001313 }
1314
1315 return 0;
1316}
1317
1318/**
1319 * spi_get_next_queued_message() - called by driver to check for queued
1320 * messages
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001321 * @ctlr: the controller to check for queued messages
Linus Walleijffbbdd212012-02-22 10:05:38 +01001322 *
1323 * If there are more messages in the queue, the next message is returned from
1324 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001325 *
1326 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001327 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001328struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001329{
1330 struct spi_message *next;
1331 unsigned long flags;
1332
1333 /* get a pointer to the next message, if any */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001334 spin_lock_irqsave(&ctlr->queue_lock, flags);
1335 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
Axel Lin1cfd97f2014-01-02 15:16:40 +08001336 queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001337 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001338
1339 return next;
1340}
1341EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1342
1343/**
1344 * spi_finalize_current_message() - the current message is complete
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001345 * @ctlr: the controller to return the message to
Linus Walleijffbbdd212012-02-22 10:05:38 +01001346 *
1347 * Called by the driver to notify the core that the message in the front of the
1348 * queue is complete and can be removed from the queue.
1349 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001350void spi_finalize_current_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001351{
1352 struct spi_message *mesg;
1353 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001354 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001355
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001356 spin_lock_irqsave(&ctlr->queue_lock, flags);
1357 mesg = ctlr->cur_msg;
1358 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001359
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001360 spi_unmap_msg(ctlr, mesg);
Mark Brown99adef32014-01-16 12:22:43 +00001361
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001362 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1363 ret = ctlr->unprepare_message(ctlr, mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001364 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001365 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1366 ret);
Mark Brown2841a5f2013-10-05 00:23:12 +01001367 }
1368 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001369
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001370 spin_lock_irqsave(&ctlr->queue_lock, flags);
1371 ctlr->cur_msg = NULL;
1372 ctlr->cur_msg_prepared = false;
1373 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1374 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001375
1376 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001377
Linus Walleijffbbdd212012-02-22 10:05:38 +01001378 mesg->state = NULL;
1379 if (mesg->complete)
1380 mesg->complete(mesg->context);
1381}
1382EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1383
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001384static int spi_start_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001385{
1386 unsigned long flags;
1387
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001388 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001389
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001390 if (ctlr->running || ctlr->busy) {
1391 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001392 return -EBUSY;
1393 }
1394
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001395 ctlr->running = true;
1396 ctlr->cur_msg = NULL;
1397 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001398
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001399 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001400
1401 return 0;
1402}
1403
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001404static int spi_stop_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001405{
1406 unsigned long flags;
1407 unsigned limit = 500;
1408 int ret = 0;
1409
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001410 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001411
1412 /*
1413 * This is a bit lame, but is optimized for the common execution path.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001414 * A wait_queue on the ctlr->busy could be used, but then the common
Linus Walleijffbbdd212012-02-22 10:05:38 +01001415 * execution path (pump_messages) would be required to call wake_up or
1416 * friends on every SPI message. Do this instead.
1417 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001418 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1419 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001420 usleep_range(10000, 11000);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001421 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001422 }
1423
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001424 if (!list_empty(&ctlr->queue) || ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001425 ret = -EBUSY;
1426 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001427 ctlr->running = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001428
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001429 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001430
1431 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001432 dev_warn(&ctlr->dev, "could not stop message queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001433 return ret;
1434 }
1435 return ret;
1436}
1437
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001438static int spi_destroy_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001439{
1440 int ret;
1441
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001442 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001443
1444 /*
Petr Mladek39891442016-10-11 13:55:20 -07001445 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001446 * If the reason that stop_queue timed out is that the work will never
1447 * finish, then it does no good to call flush/stop thread, so
1448 * return anyway.
1449 */
1450 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001451 dev_err(&ctlr->dev, "problem destroying queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001452 return ret;
1453 }
1454
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001455 kthread_flush_worker(&ctlr->kworker);
1456 kthread_stop(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001457
1458 return 0;
1459}
1460
Mark Brown0461a412014-12-09 21:38:05 +00001461static int __spi_queued_transfer(struct spi_device *spi,
1462 struct spi_message *msg,
1463 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001464{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001465 struct spi_controller *ctlr = spi->controller;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001466 unsigned long flags;
1467
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001468 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001469
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001470 if (!ctlr->running) {
1471 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001472 return -ESHUTDOWN;
1473 }
1474 msg->actual_length = 0;
1475 msg->status = -EINPROGRESS;
1476
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001477 list_add_tail(&msg->queue, &ctlr->queue);
1478 if (!ctlr->busy && need_pump)
1479 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001480
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001481 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001482 return 0;
1483}
1484
Mark Brown0461a412014-12-09 21:38:05 +00001485/**
1486 * spi_queued_transfer - transfer function for queued transfers
1487 * @spi: spi device which is requesting transfer
1488 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001489 *
1490 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001491 */
1492static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1493{
1494 return __spi_queued_transfer(spi, msg, true);
1495}
1496
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001497static int spi_controller_initialize_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001498{
1499 int ret;
1500
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001501 ctlr->transfer = spi_queued_transfer;
1502 if (!ctlr->transfer_one_message)
1503 ctlr->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001504
1505 /* Initialize and start queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001506 ret = spi_init_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001507 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001508 dev_err(&ctlr->dev, "problem initializing queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001509 goto err_init_queue;
1510 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001511 ctlr->queued = true;
1512 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001513 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001514 dev_err(&ctlr->dev, "problem starting queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001515 goto err_start_queue;
1516 }
1517
1518 return 0;
1519
1520err_start_queue:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001521 spi_destroy_queue(ctlr);
Mark Brownc3676d52014-05-01 10:47:52 -07001522err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001523 return ret;
1524}
1525
1526/*-------------------------------------------------------------------------*/
1527
Andreas Larsson7cb94362012-12-04 15:09:38 +01001528#if defined(CONFIG_OF)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001529static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001530 struct device_node *nc)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001531{
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001532 u32 value;
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001533 int rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001534
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001535 /* Mode (clock phase/polarity/etc.) */
1536 if (of_find_property(nc, "spi-cpha", NULL))
1537 spi->mode |= SPI_CPHA;
1538 if (of_find_property(nc, "spi-cpol", NULL))
1539 spi->mode |= SPI_CPOL;
1540 if (of_find_property(nc, "spi-cs-high", NULL))
1541 spi->mode |= SPI_CS_HIGH;
1542 if (of_find_property(nc, "spi-3wire", NULL))
1543 spi->mode |= SPI_3WIRE;
1544 if (of_find_property(nc, "spi-lsb-first", NULL))
1545 spi->mode |= SPI_LSB_FIRST;
1546
1547 /* Device DUAL/QUAD mode */
1548 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1549 switch (value) {
1550 case 1:
1551 break;
1552 case 2:
1553 spi->mode |= SPI_TX_DUAL;
1554 break;
1555 case 4:
1556 spi->mode |= SPI_TX_QUAD;
1557 break;
1558 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001559 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001560 "spi-tx-bus-width %d not supported\n",
1561 value);
1562 break;
1563 }
1564 }
1565
1566 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1567 switch (value) {
1568 case 1:
1569 break;
1570 case 2:
1571 spi->mode |= SPI_RX_DUAL;
1572 break;
1573 case 4:
1574 spi->mode |= SPI_RX_QUAD;
1575 break;
1576 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001577 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001578 "spi-rx-bus-width %d not supported\n",
1579 value);
1580 break;
1581 }
1582 }
1583
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001584 if (spi_controller_is_slave(ctlr)) {
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001585 if (strcmp(nc->name, "slave")) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001586 dev_err(&ctlr->dev, "%s is not called 'slave'\n",
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001587 nc->full_name);
1588 return -EINVAL;
1589 }
1590 return 0;
1591 }
1592
1593 /* Device address */
1594 rc = of_property_read_u32(nc, "reg", &value);
1595 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001596 dev_err(&ctlr->dev, "%s has no valid 'reg' property (%d)\n",
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001597 nc->full_name, rc);
1598 return rc;
1599 }
1600 spi->chip_select = value;
1601
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001602 /* Device speed */
1603 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1604 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001605 dev_err(&ctlr->dev,
1606 "%s has no valid 'spi-max-frequency' property (%d)\n",
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001607 nc->full_name, rc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001608 return rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001609 }
1610 spi->max_speed_hz = value;
1611
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001612 return 0;
1613}
1614
1615static struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001616of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001617{
1618 struct spi_device *spi;
1619 int rc;
1620
1621 /* Alloc an spi_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001622 spi = spi_alloc_device(ctlr);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001623 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001624 dev_err(&ctlr->dev, "spi_device alloc error for %s\n",
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001625 nc->full_name);
1626 rc = -ENOMEM;
1627 goto err_out;
1628 }
1629
1630 /* Select device driver */
1631 rc = of_modalias_node(nc, spi->modalias,
1632 sizeof(spi->modalias));
1633 if (rc < 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001634 dev_err(&ctlr->dev, "cannot find modalias for %s\n",
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001635 nc->full_name);
1636 goto err_out;
1637 }
1638
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001639 rc = of_spi_parse_dt(ctlr, spi, nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001640 if (rc)
1641 goto err_out;
1642
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001643 /* Store a pointer to the node in the device structure */
1644 of_node_get(nc);
1645 spi->dev.of_node = nc;
1646
1647 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001648 rc = spi_add_device(spi);
1649 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001650 dev_err(&ctlr->dev, "spi_device register error %s\n",
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001651 nc->full_name);
Johan Hovold83241472017-01-30 17:47:05 +01001652 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001653 }
1654
1655 return spi;
1656
Johan Hovold83241472017-01-30 17:47:05 +01001657err_of_node_put:
1658 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001659err_out:
1660 spi_dev_put(spi);
1661 return ERR_PTR(rc);
1662}
1663
Grant Likelyd57a4282012-04-07 14:16:53 -06001664/**
1665 * of_register_spi_devices() - Register child devices onto the SPI bus
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001666 * @ctlr: Pointer to spi_controller device
Grant Likelyd57a4282012-04-07 14:16:53 -06001667 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001668 * Registers an spi_device for each child node of controller node which
1669 * represents a valid SPI slave.
Grant Likelyd57a4282012-04-07 14:16:53 -06001670 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001671static void of_register_spi_devices(struct spi_controller *ctlr)
Grant Likelyd57a4282012-04-07 14:16:53 -06001672{
1673 struct spi_device *spi;
1674 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001675
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001676 if (!ctlr->dev.of_node)
Grant Likelyd57a4282012-04-07 14:16:53 -06001677 return;
1678
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001679 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01001680 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1681 continue;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001682 spi = of_register_spi_device(ctlr, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001683 if (IS_ERR(spi)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001684 dev_warn(&ctlr->dev,
1685 "Failed to create SPI device for %s\n",
1686 nc->full_name);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001687 of_node_clear_flag(nc, OF_POPULATED);
1688 }
Grant Likelyd57a4282012-04-07 14:16:53 -06001689 }
1690}
1691#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001692static void of_register_spi_devices(struct spi_controller *ctlr) { }
Grant Likelyd57a4282012-04-07 14:16:53 -06001693#endif
1694
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001695#ifdef CONFIG_ACPI
1696static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1697{
1698 struct spi_device *spi = data;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001699 struct spi_controller *ctlr = spi->controller;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001700
1701 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1702 struct acpi_resource_spi_serialbus *sb;
1703
1704 sb = &ares->data.spi_serial_bus;
1705 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Mika Westerberga0a90712016-02-08 17:14:28 +02001706 /*
1707 * ACPI DeviceSelection numbering is handled by the
1708 * host controller driver in Windows and can vary
1709 * from driver to driver. In Linux we always expect
1710 * 0 .. max - 1 so we need to ask the driver to
1711 * translate between the two schemes.
1712 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001713 if (ctlr->fw_translate_cs) {
1714 int cs = ctlr->fw_translate_cs(ctlr,
Mika Westerberga0a90712016-02-08 17:14:28 +02001715 sb->device_selection);
1716 if (cs < 0)
1717 return cs;
1718 spi->chip_select = cs;
1719 } else {
1720 spi->chip_select = sb->device_selection;
1721 }
1722
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001723 spi->max_speed_hz = sb->connection_speed;
1724
1725 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1726 spi->mode |= SPI_CPHA;
1727 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1728 spi->mode |= SPI_CPOL;
1729 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1730 spi->mode |= SPI_CS_HIGH;
1731 }
1732 } else if (spi->irq < 0) {
1733 struct resource r;
1734
1735 if (acpi_dev_resource_interrupt(ares, 0, &r))
1736 spi->irq = r.start;
1737 }
1738
1739 /* Always tell the ACPI core to skip this resource */
1740 return 1;
1741}
1742
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001743static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
Octavian Purdila7f244672016-07-08 19:13:11 +03001744 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001745{
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001746 struct list_head resource_list;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001747 struct spi_device *spi;
1748 int ret;
1749
Octavian Purdila7f244672016-07-08 19:13:11 +03001750 if (acpi_bus_get_status(adev) || !adev->status.present ||
1751 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001752 return AE_OK;
1753
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001754 spi = spi_alloc_device(ctlr);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001755 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001756 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001757 dev_name(&adev->dev));
1758 return AE_NO_MEMORY;
1759 }
1760
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001761 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001762 spi->irq = -1;
1763
1764 INIT_LIST_HEAD(&resource_list);
1765 ret = acpi_dev_get_resources(adev, &resource_list,
1766 acpi_spi_add_resource, spi);
1767 acpi_dev_free_resource_list(&resource_list);
1768
1769 if (ret < 0 || !spi->max_speed_hz) {
1770 spi_dev_put(spi);
1771 return AE_OK;
1772 }
1773
Dan O'Donovan0c6543f2017-02-05 16:30:14 +00001774 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
1775 sizeof(spi->modalias));
1776
Christophe RICARD33ada672015-12-23 23:25:35 +01001777 if (spi->irq < 0)
1778 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1779
Octavian Purdila7f244672016-07-08 19:13:11 +03001780 acpi_device_set_enumerated(adev);
1781
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001782 adev->power.flags.ignore_parent = true;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001783 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001784 adev->power.flags.ignore_parent = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001785 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001786 dev_name(&adev->dev));
1787 spi_dev_put(spi);
1788 }
1789
1790 return AE_OK;
1791}
1792
Octavian Purdila7f244672016-07-08 19:13:11 +03001793static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1794 void *data, void **return_value)
1795{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001796 struct spi_controller *ctlr = data;
Octavian Purdila7f244672016-07-08 19:13:11 +03001797 struct acpi_device *adev;
1798
1799 if (acpi_bus_get_device(handle, &adev))
1800 return AE_OK;
1801
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001802 return acpi_register_spi_device(ctlr, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03001803}
1804
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001805static void acpi_register_spi_devices(struct spi_controller *ctlr)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001806{
1807 acpi_status status;
1808 acpi_handle handle;
1809
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001810 handle = ACPI_HANDLE(ctlr->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001811 if (!handle)
1812 return;
1813
1814 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001815 acpi_spi_add_device, NULL, ctlr, NULL);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001816 if (ACPI_FAILURE(status))
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001817 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001818}
1819#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001820static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001821#endif /* CONFIG_ACPI */
1822
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001823static void spi_controller_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001824{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001825 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08001826
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001827 ctlr = container_of(dev, struct spi_controller, dev);
1828 kfree(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08001829}
1830
1831static struct class spi_master_class = {
1832 .name = "spi_master",
1833 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001834 .dev_release = spi_controller_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00001835 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08001836};
1837
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001838#ifdef CONFIG_SPI_SLAVE
1839/**
1840 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
1841 * controller
1842 * @spi: device used for the current transfer
1843 */
1844int spi_slave_abort(struct spi_device *spi)
1845{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001846 struct spi_controller *ctlr = spi->controller;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001847
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001848 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
1849 return ctlr->slave_abort(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001850
1851 return -ENOTSUPP;
1852}
1853EXPORT_SYMBOL_GPL(spi_slave_abort);
1854
1855static int match_true(struct device *dev, void *data)
1856{
1857 return 1;
1858}
1859
1860static ssize_t spi_slave_show(struct device *dev,
1861 struct device_attribute *attr, char *buf)
1862{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001863 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1864 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001865 struct device *child;
1866
1867 child = device_find_child(&ctlr->dev, NULL, match_true);
1868 return sprintf(buf, "%s\n",
1869 child ? to_spi_device(child)->modalias : NULL);
1870}
1871
1872static ssize_t spi_slave_store(struct device *dev,
1873 struct device_attribute *attr, const char *buf,
1874 size_t count)
1875{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001876 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1877 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001878 struct spi_device *spi;
1879 struct device *child;
1880 char name[32];
1881 int rc;
1882
1883 rc = sscanf(buf, "%31s", name);
1884 if (rc != 1 || !name[0])
1885 return -EINVAL;
1886
1887 child = device_find_child(&ctlr->dev, NULL, match_true);
1888 if (child) {
1889 /* Remove registered slave */
1890 device_unregister(child);
1891 put_device(child);
1892 }
1893
1894 if (strcmp(name, "(null)")) {
1895 /* Register new slave */
1896 spi = spi_alloc_device(ctlr);
1897 if (!spi)
1898 return -ENOMEM;
1899
1900 strlcpy(spi->modalias, name, sizeof(spi->modalias));
1901
1902 rc = spi_add_device(spi);
1903 if (rc) {
1904 spi_dev_put(spi);
1905 return rc;
1906 }
1907 }
1908
1909 return count;
1910}
1911
1912static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store);
1913
1914static struct attribute *spi_slave_attrs[] = {
1915 &dev_attr_slave.attr,
1916 NULL,
1917};
1918
1919static const struct attribute_group spi_slave_group = {
1920 .attrs = spi_slave_attrs,
1921};
1922
1923static const struct attribute_group *spi_slave_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001924 &spi_controller_statistics_group,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001925 &spi_slave_group,
1926 NULL,
1927};
1928
1929static struct class spi_slave_class = {
1930 .name = "spi_slave",
1931 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001932 .dev_release = spi_controller_release,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001933 .dev_groups = spi_slave_groups,
1934};
1935#else
1936extern struct class spi_slave_class; /* dummy */
1937#endif
David Brownell8ae12a02006-01-08 13:34:19 -08001938
1939/**
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001940 * __spi_alloc_controller - allocate an SPI master or slave controller
David Brownell8ae12a02006-01-08 13:34:19 -08001941 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001942 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001943 * memory is in the driver_data field of the returned device,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001944 * accessible with spi_controller_get_devdata().
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001945 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
1946 * slave (true) controller
David Brownell33e34dc2007-05-08 00:32:21 -07001947 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001948 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001949 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08001950 * only ones directly touching chip registers. It's how they allocate
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001951 * an spi_controller structure, prior to calling spi_register_controller().
David Brownell8ae12a02006-01-08 13:34:19 -08001952 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001953 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08001954 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001955 * The caller is responsible for assigning the bus number and initializing the
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001956 * controller's methods before calling spi_register_controller(); and (after
1957 * errors adding the device) calling spi_controller_put() to prevent a memory
1958 * leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001959 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001960 * Return: the SPI controller structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08001961 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001962struct spi_controller *__spi_alloc_controller(struct device *dev,
1963 unsigned int size, bool slave)
David Brownell8ae12a02006-01-08 13:34:19 -08001964{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001965 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08001966
David Brownell0c868462006-01-08 13:34:25 -08001967 if (!dev)
1968 return NULL;
1969
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001970 ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL);
1971 if (!ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08001972 return NULL;
1973
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001974 device_initialize(&ctlr->dev);
1975 ctlr->bus_num = -1;
1976 ctlr->num_chipselect = 1;
1977 ctlr->slave = slave;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001978 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001979 ctlr->dev.class = &spi_slave_class;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001980 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001981 ctlr->dev.class = &spi_master_class;
1982 ctlr->dev.parent = dev;
1983 pm_suspend_ignore_children(&ctlr->dev, true);
1984 spi_controller_set_devdata(ctlr, &ctlr[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08001985
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001986 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08001987}
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001988EXPORT_SYMBOL_GPL(__spi_alloc_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08001989
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001990#ifdef CONFIG_OF
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001991static int of_spi_register_master(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001992{
Grant Likelye80beb22013-02-12 17:48:37 +00001993 int nb, i, *cs;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001994 struct device_node *np = ctlr->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01001995
1996 if (!np)
1997 return 0;
1998
1999 nb = of_gpio_named_count(np, "cs-gpios");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002000 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002001
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002002 /* Return error only for an incorrectly formed cs-gpios property */
2003 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002004 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002005 else if (nb < 0)
2006 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002007
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002008 cs = devm_kzalloc(&ctlr->dev, sizeof(int) * ctlr->num_chipselect,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002009 GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002010 ctlr->cs_gpios = cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002011
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002012 if (!ctlr->cs_gpios)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002013 return -ENOMEM;
2014
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002015 for (i = 0; i < ctlr->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01002016 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002017
2018 for (i = 0; i < nb; i++)
2019 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2020
2021 return 0;
2022}
2023#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002024static int of_spi_register_master(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002025{
2026 return 0;
2027}
2028#endif
2029
David Brownell8ae12a02006-01-08 13:34:19 -08002030/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002031 * spi_register_controller - register SPI master or slave controller
2032 * @ctlr: initialized master, originally from spi_alloc_master() or
2033 * spi_alloc_slave()
David Brownell33e34dc2007-05-08 00:32:21 -07002034 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002035 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002036 * SPI controllers connect to their drivers using some non-SPI bus,
David Brownell8ae12a02006-01-08 13:34:19 -08002037 * such as the platform bus. The final stage of probe() in that code
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002038 * includes calling spi_register_controller() to hook up to this SPI bus glue.
David Brownell8ae12a02006-01-08 13:34:19 -08002039 *
2040 * SPI controllers use board specific (often SOC specific) bus numbers,
2041 * and board-specific addressing for SPI devices combines those numbers
2042 * with chip select numbers. Since SPI does not directly support dynamic
2043 * device identification, boards need configuration tables telling which
2044 * chip is at which address.
2045 *
2046 * This must be called from context that can sleep. It returns zero on
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002047 * success, else a negative error code (dropping the controller's refcount).
David Brownell0c868462006-01-08 13:34:25 -08002048 * After a successful return, the caller is responsible for calling
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002049 * spi_unregister_controller().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002050 *
2051 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002052 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002053int spi_register_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002054{
David Brownelle44a45a2007-06-03 13:50:40 -07002055 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002056 struct device *dev = ctlr->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08002057 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08002058 int status = -ENODEV;
2059 int dynamic = 0;
2060
David Brownell0c868462006-01-08 13:34:25 -08002061 if (!dev)
2062 return -ENODEV;
2063
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002064 if (!spi_controller_is_slave(ctlr)) {
2065 status = of_spi_register_master(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002066 if (status)
2067 return status;
2068 }
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002069
David Brownell082c8cb2007-07-31 00:39:45 -07002070 /* even if it's just one always-selected device, there must
2071 * be at least one chipselect
2072 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002073 if (ctlr->num_chipselect == 0)
David Brownell082c8cb2007-07-31 00:39:45 -07002074 return -EINVAL;
2075
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002076 if ((ctlr->bus_num < 0) && ctlr->dev.of_node)
2077 ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi");
Grant Likelybb297852012-12-21 19:32:09 +00002078
David Brownell8ae12a02006-01-08 13:34:19 -08002079 /* convention: dynamically assigned bus IDs count down from the max */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002080 if (ctlr->bus_num < 0) {
David Brownell082c8cb2007-07-31 00:39:45 -07002081 /* FIXME switch to an IDR based scheme, something like
2082 * I2C now uses, so we can't run out of "dynamic" IDs
2083 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002084 ctlr->bus_num = atomic_dec_return(&dyn_bus_id);
David Brownellb8852442006-01-08 13:34:23 -08002085 dynamic = 1;
David Brownell8ae12a02006-01-08 13:34:19 -08002086 }
2087
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002088 INIT_LIST_HEAD(&ctlr->queue);
2089 spin_lock_init(&ctlr->queue_lock);
2090 spin_lock_init(&ctlr->bus_lock_spinlock);
2091 mutex_init(&ctlr->bus_lock_mutex);
2092 mutex_init(&ctlr->io_mutex);
2093 ctlr->bus_lock_flag = 0;
2094 init_completion(&ctlr->xfer_completion);
2095 if (!ctlr->max_dma_len)
2096 ctlr->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002097
David Brownell8ae12a02006-01-08 13:34:19 -08002098 /* register the device, then userspace will see it.
2099 * registration fails if the bus ID is in use.
2100 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002101 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
2102 status = device_add(&ctlr->dev);
David Brownellb8852442006-01-08 13:34:23 -08002103 if (status < 0)
David Brownell8ae12a02006-01-08 13:34:19 -08002104 goto done;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002105 dev_dbg(dev, "registered %s %s%s\n",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002106 spi_controller_is_slave(ctlr) ? "slave" : "master",
2107 dev_name(&ctlr->dev), dynamic ? " (dynamic)" : "");
David Brownell8ae12a02006-01-08 13:34:19 -08002108
Linus Walleijffbbdd212012-02-22 10:05:38 +01002109 /* If we're using a queued driver, start the queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002110 if (ctlr->transfer)
2111 dev_info(dev, "controller is unqueued, this is deprecated\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002112 else {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002113 status = spi_controller_initialize_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002114 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002115 device_del(&ctlr->dev);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002116 goto done;
2117 }
2118 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00002119 /* add statistics */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002120 spin_lock_init(&ctlr->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002121
Feng Tang2b9603a2010-08-02 15:52:15 +08002122 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002123 list_add_tail(&ctlr->list, &spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002124 list_for_each_entry(bi, &board_list, list)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002125 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +08002126 mutex_unlock(&board_lock);
2127
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002128 /* Register devices from the device tree and ACPI */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002129 of_register_spi_devices(ctlr);
2130 acpi_register_spi_devices(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002131done:
2132 return status;
2133}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002134EXPORT_SYMBOL_GPL(spi_register_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002135
Mark Brown666d5b42013-08-31 18:50:52 +01002136static void devm_spi_unregister(struct device *dev, void *res)
2137{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002138 spi_unregister_controller(*(struct spi_controller **)res);
Mark Brown666d5b42013-08-31 18:50:52 +01002139}
2140
2141/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002142 * devm_spi_register_controller - register managed SPI master or slave
2143 * controller
2144 * @dev: device managing SPI controller
2145 * @ctlr: initialized controller, originally from spi_alloc_master() or
2146 * spi_alloc_slave()
Mark Brown666d5b42013-08-31 18:50:52 +01002147 * Context: can sleep
2148 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002149 * Register a SPI device as with spi_register_controller() which will
Mark Brown666d5b42013-08-31 18:50:52 +01002150 * automatically be unregister
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002151 *
2152 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01002153 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002154int devm_spi_register_controller(struct device *dev,
2155 struct spi_controller *ctlr)
Mark Brown666d5b42013-08-31 18:50:52 +01002156{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002157 struct spi_controller **ptr;
Mark Brown666d5b42013-08-31 18:50:52 +01002158 int ret;
2159
2160 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2161 if (!ptr)
2162 return -ENOMEM;
2163
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002164 ret = spi_register_controller(ctlr);
Stephen Warren4b928942013-11-21 16:11:15 -07002165 if (!ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002166 *ptr = ctlr;
Mark Brown666d5b42013-08-31 18:50:52 +01002167 devres_add(dev, ptr);
2168 } else {
2169 devres_free(ptr);
2170 }
2171
2172 return ret;
2173}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002174EXPORT_SYMBOL_GPL(devm_spi_register_controller);
Mark Brown666d5b42013-08-31 18:50:52 +01002175
David Lamparter34860082010-08-30 23:54:17 +02002176static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002177{
David Lamparter34860082010-08-30 23:54:17 +02002178 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002179 return 0;
2180}
2181
2182/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002183 * spi_unregister_controller - unregister SPI master or slave controller
2184 * @ctlr: the controller being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002185 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002186 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002187 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002188 * only ones directly touching chip registers.
2189 *
2190 * This must be called from context that can sleep.
2191 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002192void spi_unregister_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002193{
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002194 int dummy;
2195
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002196 if (ctlr->queued) {
2197 if (spi_destroy_queue(ctlr))
2198 dev_err(&ctlr->dev, "queue remove failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002199 }
2200
Feng Tang2b9603a2010-08-02 15:52:15 +08002201 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002202 list_del(&ctlr->list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002203 mutex_unlock(&board_lock);
2204
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002205 dummy = device_for_each_child(&ctlr->dev, NULL, __unregister);
2206 device_unregister(&ctlr->dev);
David Brownell8ae12a02006-01-08 13:34:19 -08002207}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002208EXPORT_SYMBOL_GPL(spi_unregister_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002209
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002210int spi_controller_suspend(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002211{
2212 int ret;
2213
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002214 /* Basically no-ops for non-queued controllers */
2215 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002216 return 0;
2217
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002218 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002219 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002220 dev_err(&ctlr->dev, "queue stop failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002221
2222 return ret;
2223}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002224EXPORT_SYMBOL_GPL(spi_controller_suspend);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002225
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002226int spi_controller_resume(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002227{
2228 int ret;
2229
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002230 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002231 return 0;
2232
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002233 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002234 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002235 dev_err(&ctlr->dev, "queue restart failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002236
2237 return ret;
2238}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002239EXPORT_SYMBOL_GPL(spi_controller_resume);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002240
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002241static int __spi_controller_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002242{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002243 struct spi_controller *ctlr;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002244 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002245
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002246 ctlr = container_of(dev, struct spi_controller, dev);
2247 return ctlr->bus_num == *bus_num;
Dave Young5ed2c832008-01-22 15:14:18 +08002248}
2249
David Brownell8ae12a02006-01-08 13:34:19 -08002250/**
2251 * spi_busnum_to_master - look up master associated with bus_num
2252 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002253 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002254 *
2255 * This call may be used with devices that are registered after
2256 * arch init time. It returns a refcounted pointer to the relevant
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002257 * spi_controller (which the caller must release), or NULL if there is
David Brownell8ae12a02006-01-08 13:34:19 -08002258 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002259 *
2260 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002261 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002262struct spi_controller *spi_busnum_to_master(u16 bus_num)
David Brownell8ae12a02006-01-08 13:34:19 -08002263{
Tony Jones49dce682007-10-16 01:27:48 -07002264 struct device *dev;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002265 struct spi_controller *ctlr = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002266
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002267 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002268 __spi_controller_match);
Dave Young5ed2c832008-01-22 15:14:18 +08002269 if (dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002270 ctlr = container_of(dev, struct spi_controller, dev);
Dave Young5ed2c832008-01-22 15:14:18 +08002271 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002272 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002273}
2274EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2275
Martin Sperld780c372015-12-14 15:20:18 +00002276/*-------------------------------------------------------------------------*/
2277
2278/* Core methods for SPI resource management */
2279
2280/**
2281 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2282 * during the processing of a spi_message while using
2283 * spi_transfer_one
2284 * @spi: the spi device for which we allocate memory
2285 * @release: the release code to execute for this resource
2286 * @size: size to alloc and return
2287 * @gfp: GFP allocation flags
2288 *
2289 * Return: the pointer to the allocated data
2290 *
2291 * This may get enhanced in the future to allocate from a memory pool
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002292 * of the @spi_device or @spi_controller to avoid repeated allocations.
Martin Sperld780c372015-12-14 15:20:18 +00002293 */
2294void *spi_res_alloc(struct spi_device *spi,
2295 spi_res_release_t release,
2296 size_t size, gfp_t gfp)
2297{
2298 struct spi_res *sres;
2299
2300 sres = kzalloc(sizeof(*sres) + size, gfp);
2301 if (!sres)
2302 return NULL;
2303
2304 INIT_LIST_HEAD(&sres->entry);
2305 sres->release = release;
2306
2307 return sres->data;
2308}
2309EXPORT_SYMBOL_GPL(spi_res_alloc);
2310
2311/**
2312 * spi_res_free - free an spi resource
2313 * @res: pointer to the custom data of a resource
2314 *
2315 */
2316void spi_res_free(void *res)
2317{
2318 struct spi_res *sres = container_of(res, struct spi_res, data);
2319
2320 if (!res)
2321 return;
2322
2323 WARN_ON(!list_empty(&sres->entry));
2324 kfree(sres);
2325}
2326EXPORT_SYMBOL_GPL(spi_res_free);
2327
2328/**
2329 * spi_res_add - add a spi_res to the spi_message
2330 * @message: the spi message
2331 * @res: the spi_resource
2332 */
2333void spi_res_add(struct spi_message *message, void *res)
2334{
2335 struct spi_res *sres = container_of(res, struct spi_res, data);
2336
2337 WARN_ON(!list_empty(&sres->entry));
2338 list_add_tail(&sres->entry, &message->resources);
2339}
2340EXPORT_SYMBOL_GPL(spi_res_add);
2341
2342/**
2343 * spi_res_release - release all spi resources for this message
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002344 * @ctlr: the @spi_controller
Martin Sperld780c372015-12-14 15:20:18 +00002345 * @message: the @spi_message
2346 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002347void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
Martin Sperld780c372015-12-14 15:20:18 +00002348{
2349 struct spi_res *res;
2350
2351 while (!list_empty(&message->resources)) {
2352 res = list_last_entry(&message->resources,
2353 struct spi_res, entry);
2354
2355 if (res->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002356 res->release(ctlr, message, res->data);
Martin Sperld780c372015-12-14 15:20:18 +00002357
2358 list_del(&res->entry);
2359
2360 kfree(res);
2361 }
2362}
2363EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002364
2365/*-------------------------------------------------------------------------*/
2366
Martin Sperl523baf5a2015-12-14 15:20:19 +00002367/* Core methods for spi_message alterations */
2368
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002369static void __spi_replace_transfers_release(struct spi_controller *ctlr,
Martin Sperl523baf5a2015-12-14 15:20:19 +00002370 struct spi_message *msg,
2371 void *res)
2372{
2373 struct spi_replaced_transfers *rxfer = res;
2374 size_t i;
2375
2376 /* call extra callback if requested */
2377 if (rxfer->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002378 rxfer->release(ctlr, msg, res);
Martin Sperl523baf5a2015-12-14 15:20:19 +00002379
2380 /* insert replaced transfers back into the message */
2381 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2382
2383 /* remove the formerly inserted entries */
2384 for (i = 0; i < rxfer->inserted; i++)
2385 list_del(&rxfer->inserted_transfers[i].transfer_list);
2386}
2387
2388/**
2389 * spi_replace_transfers - replace transfers with several transfers
2390 * and register change with spi_message.resources
2391 * @msg: the spi_message we work upon
2392 * @xfer_first: the first spi_transfer we want to replace
2393 * @remove: number of transfers to remove
2394 * @insert: the number of transfers we want to insert instead
2395 * @release: extra release code necessary in some circumstances
2396 * @extradatasize: extra data to allocate (with alignment guarantees
2397 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002398 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002399 *
2400 * Returns: pointer to @spi_replaced_transfers,
2401 * PTR_ERR(...) in case of errors.
2402 */
2403struct spi_replaced_transfers *spi_replace_transfers(
2404 struct spi_message *msg,
2405 struct spi_transfer *xfer_first,
2406 size_t remove,
2407 size_t insert,
2408 spi_replaced_release_t release,
2409 size_t extradatasize,
2410 gfp_t gfp)
2411{
2412 struct spi_replaced_transfers *rxfer;
2413 struct spi_transfer *xfer;
2414 size_t i;
2415
2416 /* allocate the structure using spi_res */
2417 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2418 insert * sizeof(struct spi_transfer)
2419 + sizeof(struct spi_replaced_transfers)
2420 + extradatasize,
2421 gfp);
2422 if (!rxfer)
2423 return ERR_PTR(-ENOMEM);
2424
2425 /* the release code to invoke before running the generic release */
2426 rxfer->release = release;
2427
2428 /* assign extradata */
2429 if (extradatasize)
2430 rxfer->extradata =
2431 &rxfer->inserted_transfers[insert];
2432
2433 /* init the replaced_transfers list */
2434 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2435
2436 /* assign the list_entry after which we should reinsert
2437 * the @replaced_transfers - it may be spi_message.messages!
2438 */
2439 rxfer->replaced_after = xfer_first->transfer_list.prev;
2440
2441 /* remove the requested number of transfers */
2442 for (i = 0; i < remove; i++) {
2443 /* if the entry after replaced_after it is msg->transfers
2444 * then we have been requested to remove more transfers
2445 * than are in the list
2446 */
2447 if (rxfer->replaced_after->next == &msg->transfers) {
2448 dev_err(&msg->spi->dev,
2449 "requested to remove more spi_transfers than are available\n");
2450 /* insert replaced transfers back into the message */
2451 list_splice(&rxfer->replaced_transfers,
2452 rxfer->replaced_after);
2453
2454 /* free the spi_replace_transfer structure */
2455 spi_res_free(rxfer);
2456
2457 /* and return with an error */
2458 return ERR_PTR(-EINVAL);
2459 }
2460
2461 /* remove the entry after replaced_after from list of
2462 * transfers and add it to list of replaced_transfers
2463 */
2464 list_move_tail(rxfer->replaced_after->next,
2465 &rxfer->replaced_transfers);
2466 }
2467
2468 /* create copy of the given xfer with identical settings
2469 * based on the first transfer to get removed
2470 */
2471 for (i = 0; i < insert; i++) {
2472 /* we need to run in reverse order */
2473 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2474
2475 /* copy all spi_transfer data */
2476 memcpy(xfer, xfer_first, sizeof(*xfer));
2477
2478 /* add to list */
2479 list_add(&xfer->transfer_list, rxfer->replaced_after);
2480
2481 /* clear cs_change and delay_usecs for all but the last */
2482 if (i) {
2483 xfer->cs_change = false;
2484 xfer->delay_usecs = 0;
2485 }
2486 }
2487
2488 /* set up inserted */
2489 rxfer->inserted = insert;
2490
2491 /* and register it with spi_res/spi_message */
2492 spi_res_add(msg, rxfer);
2493
2494 return rxfer;
2495}
2496EXPORT_SYMBOL_GPL(spi_replace_transfers);
2497
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002498static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
Fabio Estevam08933412016-02-14 13:33:50 -02002499 struct spi_message *msg,
2500 struct spi_transfer **xferp,
2501 size_t maxsize,
2502 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00002503{
2504 struct spi_transfer *xfer = *xferp, *xfers;
2505 struct spi_replaced_transfers *srt;
2506 size_t offset;
2507 size_t count, i;
2508
2509 /* warn once about this fact that we are splitting a transfer */
2510 dev_warn_once(&msg->spi->dev,
Fabio Estevam7d62f512016-02-17 15:42:27 -02002511 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
Martin Sperld9f12122015-12-14 15:20:20 +00002512 xfer->len, maxsize);
2513
2514 /* calculate how many we have to replace */
2515 count = DIV_ROUND_UP(xfer->len, maxsize);
2516
2517 /* create replacement */
2518 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03002519 if (IS_ERR(srt))
2520 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00002521 xfers = srt->inserted_transfers;
2522
2523 /* now handle each of those newly inserted spi_transfers
2524 * note that the replacements spi_transfers all are preset
2525 * to the same values as *xferp, so tx_buf, rx_buf and len
2526 * are all identical (as well as most others)
2527 * so we just have to fix up len and the pointers.
2528 *
2529 * this also includes support for the depreciated
2530 * spi_message.is_dma_mapped interface
2531 */
2532
2533 /* the first transfer just needs the length modified, so we
2534 * run it outside the loop
2535 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02002536 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00002537
2538 /* all the others need rx_buf/tx_buf also set */
2539 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2540 /* update rx_buf, tx_buf and dma */
2541 if (xfers[i].rx_buf)
2542 xfers[i].rx_buf += offset;
2543 if (xfers[i].rx_dma)
2544 xfers[i].rx_dma += offset;
2545 if (xfers[i].tx_buf)
2546 xfers[i].tx_buf += offset;
2547 if (xfers[i].tx_dma)
2548 xfers[i].tx_dma += offset;
2549
2550 /* update length */
2551 xfers[i].len = min(maxsize, xfers[i].len - offset);
2552 }
2553
2554 /* we set up xferp to the last entry we have inserted,
2555 * so that we skip those already split transfers
2556 */
2557 *xferp = &xfers[count - 1];
2558
2559 /* increment statistics counters */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002560 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperld9f12122015-12-14 15:20:20 +00002561 transfers_split_maxsize);
2562 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2563 transfers_split_maxsize);
2564
2565 return 0;
2566}
2567
2568/**
2569 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2570 * when an individual transfer exceeds a
2571 * certain size
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002572 * @ctlr: the @spi_controller for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09002573 * @msg: the @spi_message to transform
2574 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03002575 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00002576 *
2577 * Return: status of transformation
2578 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002579int spi_split_transfers_maxsize(struct spi_controller *ctlr,
Martin Sperld9f12122015-12-14 15:20:20 +00002580 struct spi_message *msg,
2581 size_t maxsize,
2582 gfp_t gfp)
2583{
2584 struct spi_transfer *xfer;
2585 int ret;
2586
2587 /* iterate over the transfer_list,
2588 * but note that xfer is advanced to the last transfer inserted
2589 * to avoid checking sizes again unnecessarily (also xfer does
2590 * potentiall belong to a different list by the time the
2591 * replacement has happened
2592 */
2593 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2594 if (xfer->len > maxsize) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002595 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
2596 maxsize, gfp);
Martin Sperld9f12122015-12-14 15:20:20 +00002597 if (ret)
2598 return ret;
2599 }
2600 }
2601
2602 return 0;
2603}
2604EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08002605
2606/*-------------------------------------------------------------------------*/
2607
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002608/* Core methods for SPI controller protocol drivers. Some of the
David Brownell7d077192009-06-17 16:26:03 -07002609 * other core methods are currently defined as inline functions.
2610 */
2611
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002612static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
2613 u8 bits_per_word)
Stefan Brüns63ab6452015-08-23 16:06:30 +02002614{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002615 if (ctlr->bits_per_word_mask) {
Stefan Brüns63ab6452015-08-23 16:06:30 +02002616 /* Only 32 bits fit in the mask */
2617 if (bits_per_word > 32)
2618 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002619 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
Stefan Brüns63ab6452015-08-23 16:06:30 +02002620 return -EINVAL;
2621 }
2622
2623 return 0;
2624}
2625
David Brownell7d077192009-06-17 16:26:03 -07002626/**
2627 * spi_setup - setup SPI mode and clock rate
2628 * @spi: the device whose settings are being modified
2629 * Context: can sleep, and no requests are queued to the device
2630 *
2631 * SPI protocol drivers may need to update the transfer mode if the
2632 * device doesn't work with its default. They may likewise need
2633 * to update clock rates or word sizes from initial values. This function
2634 * changes those settings, and must be called from a context that can sleep.
2635 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
2636 * effect the next time the device is selected and data is transferred to
2637 * or from it. When this function returns, the spi device is deselected.
2638 *
2639 * Note that this call will fail if the protocol driver specifies an option
2640 * that the underlying controller or its driver does not support. For
2641 * example, not all hardware supports wire transfers using nine bit words,
2642 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002643 *
2644 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07002645 */
2646int spi_setup(struct spi_device *spi)
2647{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002648 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002649 int status;
David Brownell7d077192009-06-17 16:26:03 -07002650
wangyuhangf477b7f2013-08-11 18:15:17 +08002651 /* check mode to prevent that DUAL and QUAD set at the same time
2652 */
2653 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
2654 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
2655 dev_err(&spi->dev,
2656 "setup: can not select dual and quad at the same time\n");
2657 return -EINVAL;
2658 }
2659 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
2660 */
2661 if ((spi->mode & SPI_3WIRE) && (spi->mode &
2662 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
2663 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07002664 /* help drivers fail *cleanly* when they need options
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002665 * that aren't supported with their current controller
David Brownelle7db06b2009-06-17 16:26:04 -07002666 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002667 bad_bits = spi->mode & ~spi->controller->mode_bits;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002668 ugly_bits = bad_bits &
2669 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
2670 if (ugly_bits) {
2671 dev_warn(&spi->dev,
2672 "setup: ignoring unsupported mode bits %x\n",
2673 ugly_bits);
2674 spi->mode &= ~ugly_bits;
2675 bad_bits &= ~ugly_bits;
2676 }
David Brownelle7db06b2009-06-17 16:26:04 -07002677 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02002678 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07002679 bad_bits);
2680 return -EINVAL;
2681 }
2682
David Brownell7d077192009-06-17 16:26:03 -07002683 if (!spi->bits_per_word)
2684 spi->bits_per_word = 8;
2685
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002686 status = __spi_validate_bits_per_word(spi->controller,
2687 spi->bits_per_word);
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002688 if (status)
2689 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02002690
Axel Lin052eb2d2014-02-10 00:08:05 +08002691 if (!spi->max_speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002692 spi->max_speed_hz = spi->controller->max_speed_hz;
Axel Lin052eb2d2014-02-10 00:08:05 +08002693
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002694 if (spi->controller->setup)
2695 status = spi->controller->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07002696
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05002697 spi_set_cs(spi, false);
2698
Jingoo Han5fe5f052013-10-14 10:31:51 +09002699 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 -07002700 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2701 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2702 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2703 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2704 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2705 spi->bits_per_word, spi->max_speed_hz,
2706 status);
2707
2708 return status;
2709}
2710EXPORT_SYMBOL_GPL(spi_setup);
2711
Mark Brown90808732013-11-13 23:44:15 +00002712static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002713{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002714 struct spi_controller *ctlr = spi->controller;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302715 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002716 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002717
Mark Brown24a00132013-07-10 15:05:40 +01002718 if (list_empty(&message->transfers))
2719 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01002720
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002721 /* Half-duplex links include original MicroWire, and ones with
2722 * only one data pin like SPI_3WIRE (switches direction) or where
2723 * either MOSI or MISO is missing. They can also be caused by
2724 * software limitations.
2725 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002726 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
2727 (spi->mode & SPI_3WIRE)) {
2728 unsigned flags = ctlr->flags;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002729
2730 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2731 if (xfer->rx_buf && xfer->tx_buf)
2732 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002733 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002734 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002735 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002736 return -EINVAL;
2737 }
2738 }
2739
Laxman Dewangane6811d12012-11-09 14:36:45 +05302740 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302741 * Set transfer bits_per_word and max speed as spi device default if
2742 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08002743 * Set transfer tx_nbits and rx_nbits as single transfer default
2744 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05302745 */
Martin Sperl77e80582015-11-27 12:31:09 +00002746 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302747 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05302748 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302749 if (!xfer->bits_per_word)
2750 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08002751
2752 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302753 xfer->speed_hz = spi->max_speed_hz;
Mark Brown7dc9fbc2015-08-20 11:52:18 -07002754 if (!xfer->speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002755 xfer->speed_hz = ctlr->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08002756
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002757 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
2758 xfer->speed_hz = ctlr->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02002759
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002760 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
Stefan Brüns63ab6452015-08-23 16:06:30 +02002761 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01002762
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002763 /*
2764 * SPI transfer length should be multiple of SPI word size
2765 * where SPI word size should be power-of-two multiple
2766 */
2767 if (xfer->bits_per_word <= 8)
2768 w_size = 1;
2769 else if (xfer->bits_per_word <= 16)
2770 w_size = 2;
2771 else
2772 w_size = 4;
2773
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002774 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002775 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002776 return -EINVAL;
2777
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002778 if (xfer->speed_hz && ctlr->min_speed_hz &&
2779 xfer->speed_hz < ctlr->min_speed_hz)
Mark Browna2fd4f92013-07-10 14:57:26 +01002780 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08002781
2782 if (xfer->tx_buf && !xfer->tx_nbits)
2783 xfer->tx_nbits = SPI_NBITS_SINGLE;
2784 if (xfer->rx_buf && !xfer->rx_nbits)
2785 xfer->rx_nbits = SPI_NBITS_SINGLE;
2786 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01002787 * 1. check the value matches one of single, dual and quad
2788 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08002789 */
Sourav Poddardb90a442013-08-22 21:20:48 +05302790 if (xfer->tx_buf) {
2791 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2792 xfer->tx_nbits != SPI_NBITS_DUAL &&
2793 xfer->tx_nbits != SPI_NBITS_QUAD)
2794 return -EINVAL;
2795 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2796 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2797 return -EINVAL;
2798 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2799 !(spi->mode & SPI_TX_QUAD))
2800 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302801 }
wangyuhangf477b7f2013-08-11 18:15:17 +08002802 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05302803 if (xfer->rx_buf) {
2804 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2805 xfer->rx_nbits != SPI_NBITS_DUAL &&
2806 xfer->rx_nbits != SPI_NBITS_QUAD)
2807 return -EINVAL;
2808 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2809 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2810 return -EINVAL;
2811 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2812 !(spi->mode & SPI_RX_QUAD))
2813 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302814 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05302815 }
2816
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002817 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00002818
2819 return 0;
2820}
2821
2822static int __spi_async(struct spi_device *spi, struct spi_message *message)
2823{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002824 struct spi_controller *ctlr = spi->controller;
Mark Brown90808732013-11-13 23:44:15 +00002825
2826 message->spi = spi;
2827
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002828 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
Martin Sperleca2ebc2015-06-22 13:00:36 +00002829 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2830
Mark Brown90808732013-11-13 23:44:15 +00002831 trace_spi_message_submit(message);
2832
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002833 return ctlr->transfer(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002834}
2835
David Brownell568d0692009-09-22 16:46:18 -07002836/**
2837 * spi_async - asynchronous SPI transfer
2838 * @spi: device with which data will be exchanged
2839 * @message: describes the data transfers, including completion callback
2840 * Context: any (irqs may be blocked, etc)
2841 *
2842 * This call may be used in_irq and other contexts which can't sleep,
2843 * as well as from task contexts which can sleep.
2844 *
2845 * The completion callback is invoked in a context which can't sleep.
2846 * Before that invocation, the value of message->status is undefined.
2847 * When the callback is issued, message->status holds either zero (to
2848 * indicate complete success) or a negative error code. After that
2849 * callback returns, the driver which issued the transfer request may
2850 * deallocate the associated memory; it's no longer in use by any SPI
2851 * core or controller driver code.
2852 *
2853 * Note that although all messages to a spi_device are handled in
2854 * FIFO order, messages may go to different devices in other orders.
2855 * Some device might be higher priority, or have various "hard" access
2856 * time requirements, for example.
2857 *
2858 * On detection of any fault during the transfer, processing of
2859 * the entire message is aborted, and the device is deselected.
2860 * Until returning from the associated message completion callback,
2861 * no other spi_message queued to that device will be processed.
2862 * (This rule applies equally to all the synchronous transfer calls,
2863 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002864 *
2865 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07002866 */
2867int spi_async(struct spi_device *spi, struct spi_message *message)
2868{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002869 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002870 int ret;
2871 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07002872
Mark Brown90808732013-11-13 23:44:15 +00002873 ret = __spi_validate(spi, message);
2874 if (ret != 0)
2875 return ret;
2876
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002877 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07002878
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002879 if (ctlr->bus_lock_flag)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002880 ret = -EBUSY;
2881 else
2882 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07002883
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002884 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002885
2886 return ret;
David Brownell568d0692009-09-22 16:46:18 -07002887}
2888EXPORT_SYMBOL_GPL(spi_async);
2889
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002890/**
2891 * spi_async_locked - version of spi_async with exclusive bus usage
2892 * @spi: device with which data will be exchanged
2893 * @message: describes the data transfers, including completion callback
2894 * Context: any (irqs may be blocked, etc)
2895 *
2896 * This call may be used in_irq and other contexts which can't sleep,
2897 * as well as from task contexts which can sleep.
2898 *
2899 * The completion callback is invoked in a context which can't sleep.
2900 * Before that invocation, the value of message->status is undefined.
2901 * When the callback is issued, message->status holds either zero (to
2902 * indicate complete success) or a negative error code. After that
2903 * callback returns, the driver which issued the transfer request may
2904 * deallocate the associated memory; it's no longer in use by any SPI
2905 * core or controller driver code.
2906 *
2907 * Note that although all messages to a spi_device are handled in
2908 * FIFO order, messages may go to different devices in other orders.
2909 * Some device might be higher priority, or have various "hard" access
2910 * time requirements, for example.
2911 *
2912 * On detection of any fault during the transfer, processing of
2913 * the entire message is aborted, and the device is deselected.
2914 * Until returning from the associated message completion callback,
2915 * no other spi_message queued to that device will be processed.
2916 * (This rule applies equally to all the synchronous transfer calls,
2917 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002918 *
2919 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002920 */
2921int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2922{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002923 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002924 int ret;
2925 unsigned long flags;
2926
Mark Brown90808732013-11-13 23:44:15 +00002927 ret = __spi_validate(spi, message);
2928 if (ret != 0)
2929 return ret;
2930
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002931 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002932
2933 ret = __spi_async(spi, message);
2934
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002935 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002936
2937 return ret;
2938
2939}
2940EXPORT_SYMBOL_GPL(spi_async_locked);
2941
David Brownell7d077192009-06-17 16:26:03 -07002942
Vignesh R556351f2015-12-11 09:39:56 +05302943int spi_flash_read(struct spi_device *spi,
2944 struct spi_flash_read_message *msg)
2945
2946{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002947 struct spi_controller *master = spi->controller;
Vignesh Rf4502dd2016-06-08 12:18:31 +05302948 struct device *rx_dev = NULL;
Vignesh R556351f2015-12-11 09:39:56 +05302949 int ret;
2950
2951 if ((msg->opcode_nbits == SPI_NBITS_DUAL ||
2952 msg->addr_nbits == SPI_NBITS_DUAL) &&
2953 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2954 return -EINVAL;
2955 if ((msg->opcode_nbits == SPI_NBITS_QUAD ||
2956 msg->addr_nbits == SPI_NBITS_QUAD) &&
2957 !(spi->mode & SPI_TX_QUAD))
2958 return -EINVAL;
2959 if (msg->data_nbits == SPI_NBITS_DUAL &&
2960 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2961 return -EINVAL;
2962 if (msg->data_nbits == SPI_NBITS_QUAD &&
2963 !(spi->mode & SPI_RX_QUAD))
2964 return -EINVAL;
2965
2966 if (master->auto_runtime_pm) {
2967 ret = pm_runtime_get_sync(master->dev.parent);
2968 if (ret < 0) {
2969 dev_err(&master->dev, "Failed to power device: %d\n",
2970 ret);
2971 return ret;
2972 }
2973 }
Vignesh Rf4502dd2016-06-08 12:18:31 +05302974
Vignesh R556351f2015-12-11 09:39:56 +05302975 mutex_lock(&master->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01002976 mutex_lock(&master->io_mutex);
Vignesh R2bca3442017-04-11 17:22:24 +05302977 if (master->dma_rx && master->spi_flash_can_dma(spi, msg)) {
Vignesh Rf4502dd2016-06-08 12:18:31 +05302978 rx_dev = master->dma_rx->device->dev;
2979 ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
2980 msg->buf, msg->len,
2981 DMA_FROM_DEVICE);
2982 if (!ret)
2983 msg->cur_msg_mapped = true;
2984 }
Vignesh R556351f2015-12-11 09:39:56 +05302985 ret = master->spi_flash_read(spi, msg);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302986 if (msg->cur_msg_mapped)
2987 spi_unmap_buf(master, rx_dev, &msg->rx_sg,
2988 DMA_FROM_DEVICE);
Mark Brownef4d96e2016-07-21 23:53:31 +01002989 mutex_unlock(&master->io_mutex);
Vignesh R556351f2015-12-11 09:39:56 +05302990 mutex_unlock(&master->bus_lock_mutex);
Vignesh Rf4502dd2016-06-08 12:18:31 +05302991
Vignesh R556351f2015-12-11 09:39:56 +05302992 if (master->auto_runtime_pm)
2993 pm_runtime_put(master->dev.parent);
2994
2995 return ret;
2996}
2997EXPORT_SYMBOL_GPL(spi_flash_read);
2998
David Brownell7d077192009-06-17 16:26:03 -07002999/*-------------------------------------------------------------------------*/
3000
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003001/* Utility methods for SPI protocol drivers, layered on
David Brownell7d077192009-06-17 16:26:03 -07003002 * top of the core. Some other utility methods are defined as
3003 * inline functions.
3004 */
3005
Andrew Morton5d870c82006-01-11 11:23:49 -08003006static void spi_complete(void *arg)
3007{
3008 complete(arg);
3009}
3010
Mark Brownef4d96e2016-07-21 23:53:31 +01003011static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003012{
3013 DECLARE_COMPLETION_ONSTACK(done);
3014 int status;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003015 struct spi_controller *ctlr = spi->controller;
Mark Brown0461a412014-12-09 21:38:05 +00003016 unsigned long flags;
3017
3018 status = __spi_validate(spi, message);
3019 if (status != 0)
3020 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003021
3022 message->complete = spi_complete;
3023 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00003024 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003025
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003026 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003027 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3028
Mark Brown0461a412014-12-09 21:38:05 +00003029 /* If we're not using the legacy transfer method then we will
3030 * try to transfer in the calling context so special case.
3031 * This code would be less tricky if we could remove the
3032 * support for driver implemented message queues.
3033 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003034 if (ctlr->transfer == spi_queued_transfer) {
3035 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003036
3037 trace_spi_message_submit(message);
3038
3039 status = __spi_queued_transfer(spi, message, false);
3040
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003041 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003042 } else {
3043 status = spi_async_locked(spi, message);
3044 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003045
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003046 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00003047 /* Push out the messages in the calling context if we
3048 * can.
3049 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003050 if (ctlr->transfer == spi_queued_transfer) {
3051 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperleca2ebc2015-06-22 13:00:36 +00003052 spi_sync_immediate);
3053 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3054 spi_sync_immediate);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003055 __spi_pump_messages(ctlr, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003056 }
Mark Brown0461a412014-12-09 21:38:05 +00003057
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003058 wait_for_completion(&done);
3059 status = message->status;
3060 }
3061 message->context = NULL;
3062 return status;
3063}
3064
David Brownell8ae12a02006-01-08 13:34:19 -08003065/**
3066 * spi_sync - blocking/synchronous SPI data transfers
3067 * @spi: device with which data will be exchanged
3068 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07003069 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003070 *
3071 * This call may only be used from a context that may sleep. The sleep
3072 * is non-interruptible, and has no timeout. Low-overhead controller
3073 * drivers may DMA directly into and out of the message buffers.
3074 *
3075 * Note that the SPI device's chip select is active during the message,
3076 * and then is normally disabled between messages. Drivers for some
3077 * frequently-used devices may want to minimize costs of selecting a chip,
3078 * by leaving it selected in anticipation that the next message will go
3079 * to the same chip. (That may increase power usage.)
3080 *
David Brownell0c868462006-01-08 13:34:25 -08003081 * Also, the caller is guaranteeing that the memory associated with the
3082 * message will not be freed before this call returns.
3083 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003084 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003085 */
3086int spi_sync(struct spi_device *spi, struct spi_message *message)
3087{
Mark Brownef4d96e2016-07-21 23:53:31 +01003088 int ret;
3089
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003090 mutex_lock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003091 ret = __spi_sync(spi, message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003092 mutex_unlock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003093
3094 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08003095}
3096EXPORT_SYMBOL_GPL(spi_sync);
3097
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003098/**
3099 * spi_sync_locked - version of spi_sync with exclusive bus usage
3100 * @spi: device with which data will be exchanged
3101 * @message: describes the data transfers
3102 * Context: can sleep
3103 *
3104 * This call may only be used from a context that may sleep. The sleep
3105 * is non-interruptible, and has no timeout. Low-overhead controller
3106 * drivers may DMA directly into and out of the message buffers.
3107 *
3108 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003109 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003110 * be released by a spi_bus_unlock call when the exclusive access is over.
3111 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003112 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003113 */
3114int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3115{
Mark Brownef4d96e2016-07-21 23:53:31 +01003116 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003117}
3118EXPORT_SYMBOL_GPL(spi_sync_locked);
3119
3120/**
3121 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003122 * @ctlr: SPI bus master that should be locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003123 * Context: can sleep
3124 *
3125 * This call may only be used from a context that may sleep. The sleep
3126 * is non-interruptible, and has no timeout.
3127 *
3128 * This call should be used by drivers that require exclusive access to the
3129 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3130 * exclusive access is over. Data transfer must be done by spi_sync_locked
3131 * and spi_async_locked calls when the SPI bus lock is held.
3132 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003133 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003134 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003135int spi_bus_lock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003136{
3137 unsigned long flags;
3138
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003139 mutex_lock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003140
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003141 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3142 ctlr->bus_lock_flag = 1;
3143 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003144
3145 /* mutex remains locked until spi_bus_unlock is called */
3146
3147 return 0;
3148}
3149EXPORT_SYMBOL_GPL(spi_bus_lock);
3150
3151/**
3152 * spi_bus_unlock - release the lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003153 * @ctlr: SPI bus master that was locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003154 * Context: can sleep
3155 *
3156 * This call may only be used from a context that may sleep. The sleep
3157 * is non-interruptible, and has no timeout.
3158 *
3159 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3160 * call.
3161 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003162 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003163 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003164int spi_bus_unlock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003165{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003166 ctlr->bus_lock_flag = 0;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003167
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003168 mutex_unlock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003169
3170 return 0;
3171}
3172EXPORT_SYMBOL_GPL(spi_bus_unlock);
3173
David Brownella9948b62006-04-02 10:37:40 -08003174/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003175#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003176
3177static u8 *buf;
3178
3179/**
3180 * spi_write_then_read - SPI synchronous write followed by read
3181 * @spi: device with which data will be exchanged
3182 * @txbuf: data to be written (need not be dma-safe)
3183 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003184 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3185 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003186 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003187 *
3188 * This performs a half duplex MicroWire style transaction with the
3189 * device, sending txbuf and then reading rxbuf. The return value
3190 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003191 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003192 *
David Brownell0c868462006-01-08 13:34:25 -08003193 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003194 * portable code should never use this for more than 32 bytes.
3195 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c868462006-01-08 13:34:25 -08003196 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003197 *
3198 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003199 */
3200int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003201 const void *txbuf, unsigned n_tx,
3202 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003203{
David Brownell068f4072007-12-04 23:45:09 -08003204 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003205
3206 int status;
3207 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003208 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003209 u8 *local_buf;
3210
Mark Brownb3a223e2012-12-02 12:54:25 +09003211 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3212 * copying here, (as a pure convenience thing), but we can
3213 * keep heap costs out of the hot path unless someone else is
3214 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003215 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003216 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003217 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3218 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003219 if (!local_buf)
3220 return -ENOMEM;
3221 } else {
3222 local_buf = buf;
3223 }
David Brownell8ae12a02006-01-08 13:34:19 -08003224
Vitaly Wool8275c642006-01-08 13:34:28 -08003225 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003226 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003227 if (n_tx) {
3228 x[0].len = n_tx;
3229 spi_message_add_tail(&x[0], &message);
3230 }
3231 if (n_rx) {
3232 x[1].len = n_rx;
3233 spi_message_add_tail(&x[1], &message);
3234 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003235
David Brownell8ae12a02006-01-08 13:34:19 -08003236 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003237 x[0].tx_buf = local_buf;
3238 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003239
3240 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003241 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003242 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003243 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003244
David Brownellbdff5492009-04-13 14:39:57 -07003245 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003246 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003247 else
3248 kfree(local_buf);
3249
3250 return status;
3251}
3252EXPORT_SYMBOL_GPL(spi_write_then_read);
3253
3254/*-------------------------------------------------------------------------*/
3255
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003256#if IS_ENABLED(CONFIG_OF_DYNAMIC)
3257static int __spi_of_device_match(struct device *dev, void *data)
3258{
3259 return dev->of_node == data;
3260}
3261
3262/* must call put_device() when done with returned spi_device device */
3263static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
3264{
3265 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3266 __spi_of_device_match);
3267 return dev ? to_spi_device(dev) : NULL;
3268}
3269
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003270static int __spi_of_controller_match(struct device *dev, const void *data)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003271{
3272 return dev->of_node == data;
3273}
3274
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003275/* the spi controllers are not using spi_bus, so we find it with another way */
3276static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003277{
3278 struct device *dev;
3279
3280 dev = class_find_device(&spi_master_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003281 __spi_of_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003282 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3283 dev = class_find_device(&spi_slave_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003284 __spi_of_controller_match);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003285 if (!dev)
3286 return NULL;
3287
3288 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003289 return container_of(dev, struct spi_controller, dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003290}
3291
3292static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3293 void *arg)
3294{
3295 struct of_reconfig_data *rd = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003296 struct spi_controller *ctlr;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003297 struct spi_device *spi;
3298
3299 switch (of_reconfig_get_state_change(action, arg)) {
3300 case OF_RECONFIG_CHANGE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003301 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3302 if (ctlr == NULL)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003303 return NOTIFY_OK; /* not for us */
3304
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003305 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003306 put_device(&ctlr->dev);
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003307 return NOTIFY_OK;
3308 }
3309
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003310 spi = of_register_spi_device(ctlr, rd->dn);
3311 put_device(&ctlr->dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003312
3313 if (IS_ERR(spi)) {
3314 pr_err("%s: failed to create for '%s'\n",
3315 __func__, rd->dn->full_name);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003316 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003317 return notifier_from_errno(PTR_ERR(spi));
3318 }
3319 break;
3320
3321 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003322 /* already depopulated? */
3323 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3324 return NOTIFY_OK;
3325
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003326 /* find our device by node */
3327 spi = of_find_spi_device_by_node(rd->dn);
3328 if (spi == NULL)
3329 return NOTIFY_OK; /* no? not meant for us */
3330
3331 /* unregister takes one ref away */
3332 spi_unregister_device(spi);
3333
3334 /* and put the reference of the find */
3335 put_device(&spi->dev);
3336 break;
3337 }
3338
3339 return NOTIFY_OK;
3340}
3341
3342static struct notifier_block spi_of_notifier = {
3343 .notifier_call = of_spi_notify,
3344};
3345#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3346extern struct notifier_block spi_of_notifier;
3347#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3348
Octavian Purdila7f244672016-07-08 19:13:11 +03003349#if IS_ENABLED(CONFIG_ACPI)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003350static int spi_acpi_controller_match(struct device *dev, const void *data)
Octavian Purdila7f244672016-07-08 19:13:11 +03003351{
3352 return ACPI_COMPANION(dev->parent) == data;
3353}
3354
3355static int spi_acpi_device_match(struct device *dev, void *data)
3356{
3357 return ACPI_COMPANION(dev) == data;
3358}
3359
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003360static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
Octavian Purdila7f244672016-07-08 19:13:11 +03003361{
3362 struct device *dev;
3363
3364 dev = class_find_device(&spi_master_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003365 spi_acpi_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003366 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3367 dev = class_find_device(&spi_slave_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003368 spi_acpi_controller_match);
Octavian Purdila7f244672016-07-08 19:13:11 +03003369 if (!dev)
3370 return NULL;
3371
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003372 return container_of(dev, struct spi_controller, dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003373}
3374
3375static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3376{
3377 struct device *dev;
3378
3379 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3380
3381 return dev ? to_spi_device(dev) : NULL;
3382}
3383
3384static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3385 void *arg)
3386{
3387 struct acpi_device *adev = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003388 struct spi_controller *ctlr;
Octavian Purdila7f244672016-07-08 19:13:11 +03003389 struct spi_device *spi;
3390
3391 switch (value) {
3392 case ACPI_RECONFIG_DEVICE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003393 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
3394 if (!ctlr)
Octavian Purdila7f244672016-07-08 19:13:11 +03003395 break;
3396
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003397 acpi_register_spi_device(ctlr, adev);
3398 put_device(&ctlr->dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003399 break;
3400 case ACPI_RECONFIG_DEVICE_REMOVE:
3401 if (!acpi_device_enumerated(adev))
3402 break;
3403
3404 spi = acpi_spi_find_device_by_adev(adev);
3405 if (!spi)
3406 break;
3407
3408 spi_unregister_device(spi);
3409 put_device(&spi->dev);
3410 break;
3411 }
3412
3413 return NOTIFY_OK;
3414}
3415
3416static struct notifier_block spi_acpi_notifier = {
3417 .notifier_call = acpi_spi_notify,
3418};
3419#else
3420extern struct notifier_block spi_acpi_notifier;
3421#endif
3422
David Brownell8ae12a02006-01-08 13:34:19 -08003423static int __init spi_init(void)
3424{
David Brownellb8852442006-01-08 13:34:23 -08003425 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08003426
Christoph Lametere94b1762006-12-06 20:33:17 -08003427 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08003428 if (!buf) {
3429 status = -ENOMEM;
3430 goto err0;
3431 }
3432
3433 status = bus_register(&spi_bus_type);
3434 if (status < 0)
3435 goto err1;
3436
3437 status = class_register(&spi_master_class);
3438 if (status < 0)
3439 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003440
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003441 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
3442 status = class_register(&spi_slave_class);
3443 if (status < 0)
3444 goto err3;
3445 }
3446
Fabio Estevam52677202014-11-26 20:13:57 -02003447 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003448 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03003449 if (IS_ENABLED(CONFIG_ACPI))
3450 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003451
David Brownell8ae12a02006-01-08 13:34:19 -08003452 return 0;
David Brownellb8852442006-01-08 13:34:23 -08003453
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003454err3:
3455 class_unregister(&spi_master_class);
David Brownellb8852442006-01-08 13:34:23 -08003456err2:
3457 bus_unregister(&spi_bus_type);
3458err1:
3459 kfree(buf);
3460 buf = NULL;
3461err0:
3462 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08003463}
David Brownellb8852442006-01-08 13:34:23 -08003464
David Brownell8ae12a02006-01-08 13:34:19 -08003465/* board_info is normally registered in arch_initcall(),
3466 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08003467 *
3468 * REVISIT only boardinfo really needs static linking. the rest (device and
3469 * driver registration) _could_ be dynamically linked (modular) ... costs
3470 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08003471 */
David Brownell673c0c02008-10-15 22:02:46 -07003472postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08003473