blob: 4153f959f28cb0dc714511a84d194966119c967f [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>
Suniel Mahesh9b61e302017-08-03 10:05:57 +053043#include <linux/idr.h>
Lukas Wunner8a2e4872017-08-01 14:10:41 +020044#include <linux/platform_data/x86/apple.h>
David Brownell8ae12a02006-01-08 13:34:19 -080045
Mark Brown56ec1972013-10-07 19:33:53 +010046#define CREATE_TRACE_POINTS
47#include <trace/events/spi.h>
Suniel Mahesh9b61e302017-08-03 10:05:57 +053048
49static DEFINE_IDR(spi_master_idr);
Mark Brown56ec1972013-10-07 19:33:53 +010050
David Brownell8ae12a02006-01-08 13:34:19 -080051static void spidev_release(struct device *dev)
52{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -080053 struct spi_device *spi = to_spi_device(dev);
David Brownell8ae12a02006-01-08 13:34:19 -080054
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020055 /* spi controllers may cleanup for released devices */
56 if (spi->controller->cleanup)
57 spi->controller->cleanup(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080058
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020059 spi_controller_put(spi->controller);
Roman Tereshonkov07a389f2010-04-12 09:56:35 +000060 kfree(spi);
David Brownell8ae12a02006-01-08 13:34:19 -080061}
62
63static ssize_t
64modalias_show(struct device *dev, struct device_attribute *a, char *buf)
65{
66 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +080067 int len;
68
69 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
70 if (len != -ENODEV)
71 return len;
David Brownell8ae12a02006-01-08 13:34:19 -080072
Grant Likelyd8e328b2012-05-20 00:08:13 -060073 return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080074}
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -070075static DEVICE_ATTR_RO(modalias);
David Brownell8ae12a02006-01-08 13:34:19 -080076
Martin Sperleca2ebc2015-06-22 13:00:36 +000077#define SPI_STATISTICS_ATTRS(field, file) \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020078static ssize_t spi_controller_##field##_show(struct device *dev, \
79 struct device_attribute *attr, \
80 char *buf) \
Martin Sperleca2ebc2015-06-22 13:00:36 +000081{ \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020082 struct spi_controller *ctlr = container_of(dev, \
83 struct spi_controller, dev); \
84 return spi_statistics_##field##_show(&ctlr->statistics, buf); \
Martin Sperleca2ebc2015-06-22 13:00:36 +000085} \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020086static struct device_attribute dev_attr_spi_controller_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +020087 .attr = { .name = file, .mode = 0444 }, \
Geert Uytterhoeven8caab752017-06-13 13:23:52 +020088 .show = spi_controller_##field##_show, \
Martin Sperleca2ebc2015-06-22 13:00:36 +000089}; \
90static ssize_t spi_device_##field##_show(struct device *dev, \
91 struct device_attribute *attr, \
92 char *buf) \
93{ \
Geliang Tangd1eba932015-12-23 00:18:41 +080094 struct spi_device *spi = to_spi_device(dev); \
Martin Sperleca2ebc2015-06-22 13:00:36 +000095 return spi_statistics_##field##_show(&spi->statistics, buf); \
96} \
97static struct device_attribute dev_attr_spi_device_##field = { \
Geert Uytterhoevenad25c922017-05-04 16:29:56 +020098 .attr = { .name = file, .mode = 0444 }, \
Martin Sperleca2ebc2015-06-22 13:00:36 +000099 .show = spi_device_##field##_show, \
100}
101
102#define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \
103static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
104 char *buf) \
105{ \
106 unsigned long flags; \
107 ssize_t len; \
108 spin_lock_irqsave(&stat->lock, flags); \
109 len = sprintf(buf, format_string, stat->field); \
110 spin_unlock_irqrestore(&stat->lock, flags); \
111 return len; \
112} \
113SPI_STATISTICS_ATTRS(name, file)
114
115#define SPI_STATISTICS_SHOW(field, format_string) \
116 SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \
117 field, format_string)
118
119SPI_STATISTICS_SHOW(messages, "%lu");
120SPI_STATISTICS_SHOW(transfers, "%lu");
121SPI_STATISTICS_SHOW(errors, "%lu");
122SPI_STATISTICS_SHOW(timedout, "%lu");
123
124SPI_STATISTICS_SHOW(spi_sync, "%lu");
125SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
126SPI_STATISTICS_SHOW(spi_async, "%lu");
127
128SPI_STATISTICS_SHOW(bytes, "%llu");
129SPI_STATISTICS_SHOW(bytes_rx, "%llu");
130SPI_STATISTICS_SHOW(bytes_tx, "%llu");
131
Martin Sperl6b7bc062015-06-22 13:02:04 +0000132#define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \
133 SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \
134 "transfer_bytes_histo_" number, \
135 transfer_bytes_histo[index], "%lu")
136SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1");
137SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3");
138SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7");
139SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15");
140SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31");
141SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63");
142SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127");
143SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255");
144SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511");
145SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023");
146SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
147SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
148SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
149SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
150SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
151SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
152SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
153
Martin Sperld9f12122015-12-14 15:20:20 +0000154SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
155
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700156static struct attribute *spi_dev_attrs[] = {
157 &dev_attr_modalias.attr,
158 NULL,
David Brownell8ae12a02006-01-08 13:34:19 -0800159};
Martin Sperleca2ebc2015-06-22 13:00:36 +0000160
161static const struct attribute_group spi_dev_group = {
162 .attrs = spi_dev_attrs,
163};
164
165static struct attribute *spi_device_statistics_attrs[] = {
166 &dev_attr_spi_device_messages.attr,
167 &dev_attr_spi_device_transfers.attr,
168 &dev_attr_spi_device_errors.attr,
169 &dev_attr_spi_device_timedout.attr,
170 &dev_attr_spi_device_spi_sync.attr,
171 &dev_attr_spi_device_spi_sync_immediate.attr,
172 &dev_attr_spi_device_spi_async.attr,
173 &dev_attr_spi_device_bytes.attr,
174 &dev_attr_spi_device_bytes_rx.attr,
175 &dev_attr_spi_device_bytes_tx.attr,
Martin Sperl6b7bc062015-06-22 13:02:04 +0000176 &dev_attr_spi_device_transfer_bytes_histo0.attr,
177 &dev_attr_spi_device_transfer_bytes_histo1.attr,
178 &dev_attr_spi_device_transfer_bytes_histo2.attr,
179 &dev_attr_spi_device_transfer_bytes_histo3.attr,
180 &dev_attr_spi_device_transfer_bytes_histo4.attr,
181 &dev_attr_spi_device_transfer_bytes_histo5.attr,
182 &dev_attr_spi_device_transfer_bytes_histo6.attr,
183 &dev_attr_spi_device_transfer_bytes_histo7.attr,
184 &dev_attr_spi_device_transfer_bytes_histo8.attr,
185 &dev_attr_spi_device_transfer_bytes_histo9.attr,
186 &dev_attr_spi_device_transfer_bytes_histo10.attr,
187 &dev_attr_spi_device_transfer_bytes_histo11.attr,
188 &dev_attr_spi_device_transfer_bytes_histo12.attr,
189 &dev_attr_spi_device_transfer_bytes_histo13.attr,
190 &dev_attr_spi_device_transfer_bytes_histo14.attr,
191 &dev_attr_spi_device_transfer_bytes_histo15.attr,
192 &dev_attr_spi_device_transfer_bytes_histo16.attr,
Martin Sperld9f12122015-12-14 15:20:20 +0000193 &dev_attr_spi_device_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000194 NULL,
195};
196
197static const struct attribute_group spi_device_statistics_group = {
198 .name = "statistics",
199 .attrs = spi_device_statistics_attrs,
200};
201
202static const struct attribute_group *spi_dev_groups[] = {
203 &spi_dev_group,
204 &spi_device_statistics_group,
205 NULL,
206};
207
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200208static struct attribute *spi_controller_statistics_attrs[] = {
209 &dev_attr_spi_controller_messages.attr,
210 &dev_attr_spi_controller_transfers.attr,
211 &dev_attr_spi_controller_errors.attr,
212 &dev_attr_spi_controller_timedout.attr,
213 &dev_attr_spi_controller_spi_sync.attr,
214 &dev_attr_spi_controller_spi_sync_immediate.attr,
215 &dev_attr_spi_controller_spi_async.attr,
216 &dev_attr_spi_controller_bytes.attr,
217 &dev_attr_spi_controller_bytes_rx.attr,
218 &dev_attr_spi_controller_bytes_tx.attr,
219 &dev_attr_spi_controller_transfer_bytes_histo0.attr,
220 &dev_attr_spi_controller_transfer_bytes_histo1.attr,
221 &dev_attr_spi_controller_transfer_bytes_histo2.attr,
222 &dev_attr_spi_controller_transfer_bytes_histo3.attr,
223 &dev_attr_spi_controller_transfer_bytes_histo4.attr,
224 &dev_attr_spi_controller_transfer_bytes_histo5.attr,
225 &dev_attr_spi_controller_transfer_bytes_histo6.attr,
226 &dev_attr_spi_controller_transfer_bytes_histo7.attr,
227 &dev_attr_spi_controller_transfer_bytes_histo8.attr,
228 &dev_attr_spi_controller_transfer_bytes_histo9.attr,
229 &dev_attr_spi_controller_transfer_bytes_histo10.attr,
230 &dev_attr_spi_controller_transfer_bytes_histo11.attr,
231 &dev_attr_spi_controller_transfer_bytes_histo12.attr,
232 &dev_attr_spi_controller_transfer_bytes_histo13.attr,
233 &dev_attr_spi_controller_transfer_bytes_histo14.attr,
234 &dev_attr_spi_controller_transfer_bytes_histo15.attr,
235 &dev_attr_spi_controller_transfer_bytes_histo16.attr,
236 &dev_attr_spi_controller_transfers_split_maxsize.attr,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000237 NULL,
238};
239
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200240static const struct attribute_group spi_controller_statistics_group = {
Martin Sperleca2ebc2015-06-22 13:00:36 +0000241 .name = "statistics",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200242 .attrs = spi_controller_statistics_attrs,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000243};
244
245static const struct attribute_group *spi_master_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200246 &spi_controller_statistics_group,
Martin Sperleca2ebc2015-06-22 13:00:36 +0000247 NULL,
248};
249
250void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
251 struct spi_transfer *xfer,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200252 struct spi_controller *ctlr)
Martin Sperleca2ebc2015-06-22 13:00:36 +0000253{
254 unsigned long flags;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000255 int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
256
257 if (l2len < 0)
258 l2len = 0;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000259
260 spin_lock_irqsave(&stats->lock, flags);
261
262 stats->transfers++;
Martin Sperl6b7bc062015-06-22 13:02:04 +0000263 stats->transfer_bytes_histo[l2len]++;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000264
265 stats->bytes += xfer->len;
266 if ((xfer->tx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200267 (xfer->tx_buf != ctlr->dummy_tx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000268 stats->bytes_tx += xfer->len;
269 if ((xfer->rx_buf) &&
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200270 (xfer->rx_buf != ctlr->dummy_rx))
Martin Sperleca2ebc2015-06-22 13:00:36 +0000271 stats->bytes_rx += xfer->len;
272
273 spin_unlock_irqrestore(&stats->lock, flags);
274}
275EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
David Brownell8ae12a02006-01-08 13:34:19 -0800276
277/* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
278 * and the sysfs version makes coldplug work too.
279 */
280
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700281static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
282 const struct spi_device *sdev)
283{
284 while (id->name[0]) {
285 if (!strcmp(sdev->modalias, id->name))
286 return id;
287 id++;
288 }
289 return NULL;
290}
291
292const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
293{
294 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
295
296 return spi_match_id(sdrv->id_table, sdev);
297}
298EXPORT_SYMBOL_GPL(spi_get_device_id);
299
David Brownell8ae12a02006-01-08 13:34:19 -0800300static int spi_match_device(struct device *dev, struct device_driver *drv)
301{
302 const struct spi_device *spi = to_spi_device(dev);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700303 const struct spi_driver *sdrv = to_spi_driver(drv);
304
Sinan Akman2b7a32f2010-10-02 21:28:29 -0600305 /* Attempt an OF style match */
306 if (of_driver_match_device(dev, drv))
307 return 1;
308
Mika Westerberg64bee4d2012-11-30 12:37:53 +0100309 /* Then try ACPI */
310 if (acpi_driver_match_device(dev, drv))
311 return 1;
312
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700313 if (sdrv->id_table)
314 return !!spi_match_id(sdrv->id_table, spi);
David Brownell8ae12a02006-01-08 13:34:19 -0800315
Kay Sievers35f74fc2009-01-06 10:44:37 -0800316 return strcmp(spi->modalias, drv->name) == 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800317}
318
Kay Sievers7eff2e72007-08-14 15:15:12 +0200319static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell8ae12a02006-01-08 13:34:19 -0800320{
321 const struct spi_device *spi = to_spi_device(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800322 int rc;
323
324 rc = acpi_device_uevent_modalias(dev, env);
325 if (rc != -ENODEV)
326 return rc;
David Brownell8ae12a02006-01-08 13:34:19 -0800327
Andy Shevchenko28566702017-07-26 16:14:00 +0300328 return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
David Brownell8ae12a02006-01-08 13:34:19 -0800329}
330
David Brownell8ae12a02006-01-08 13:34:19 -0800331struct bus_type spi_bus_type = {
332 .name = "spi",
Greg Kroah-Hartmanaa7da562013-10-07 18:27:38 -0700333 .dev_groups = spi_dev_groups,
David Brownell8ae12a02006-01-08 13:34:19 -0800334 .match = spi_match_device,
335 .uevent = spi_uevent,
David Brownell8ae12a02006-01-08 13:34:19 -0800336};
337EXPORT_SYMBOL_GPL(spi_bus_type);
338
David Brownellb8852442006-01-08 13:34:23 -0800339
340static int spi_drv_probe(struct device *dev)
341{
342 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Jon Hunter44af7922015-10-09 15:45:55 +0100343 struct spi_device *spi = to_spi_device(dev);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300344 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800345
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200346 ret = of_clk_set_defaults(dev->of_node, false);
347 if (ret)
348 return ret;
349
Jon Hunter44af7922015-10-09 15:45:55 +0100350 if (dev->of_node) {
351 spi->irq = of_irq_get(dev->of_node, 0);
352 if (spi->irq == -EPROBE_DEFER)
353 return -EPROBE_DEFER;
354 if (spi->irq < 0)
355 spi->irq = 0;
356 }
357
Ulf Hansson676e7c22014-09-19 20:27:41 +0200358 ret = dev_pm_domain_attach(dev, true);
359 if (ret != -EPROBE_DEFER) {
Jon Hunter44af7922015-10-09 15:45:55 +0100360 ret = sdrv->probe(spi);
Ulf Hansson676e7c22014-09-19 20:27:41 +0200361 if (ret)
362 dev_pm_domain_detach(dev, true);
363 }
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300364
365 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800366}
367
368static int spi_drv_remove(struct device *dev)
369{
370 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300371 int ret;
David Brownellb8852442006-01-08 13:34:23 -0800372
Jean Delvareaec35f42014-02-13 15:28:41 +0100373 ret = sdrv->remove(to_spi_device(dev));
Ulf Hansson676e7c22014-09-19 20:27:41 +0200374 dev_pm_domain_detach(dev, true);
Mika Westerberg33cf00e2013-10-10 13:28:48 +0300375
376 return ret;
David Brownellb8852442006-01-08 13:34:23 -0800377}
378
379static void spi_drv_shutdown(struct device *dev)
380{
381 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
382
383 sdrv->shutdown(to_spi_device(dev));
384}
385
David Brownell33e34dc2007-05-08 00:32:21 -0700386/**
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500387 * __spi_register_driver - register a SPI driver
Thierry Reding88c93212015-11-10 13:03:04 +0100388 * @owner: owner module of the driver to register
David Brownell33e34dc2007-05-08 00:32:21 -0700389 * @sdrv: the driver to register
390 * Context: can sleep
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200391 *
392 * Return: zero on success, else a negative error code.
David Brownell33e34dc2007-05-08 00:32:21 -0700393 */
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500394int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
David Brownellb8852442006-01-08 13:34:23 -0800395{
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500396 sdrv->driver.owner = owner;
David Brownellb8852442006-01-08 13:34:23 -0800397 sdrv->driver.bus = &spi_bus_type;
398 if (sdrv->probe)
399 sdrv->driver.probe = spi_drv_probe;
400 if (sdrv->remove)
401 sdrv->driver.remove = spi_drv_remove;
402 if (sdrv->shutdown)
403 sdrv->driver.shutdown = spi_drv_shutdown;
404 return driver_register(&sdrv->driver);
405}
Andrew F. Davisca5d2482015-10-23 08:59:10 -0500406EXPORT_SYMBOL_GPL(__spi_register_driver);
David Brownellb8852442006-01-08 13:34:23 -0800407
David Brownell8ae12a02006-01-08 13:34:19 -0800408/*-------------------------------------------------------------------------*/
409
410/* SPI devices should normally not be created by SPI device drivers; that
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200411 * would make them board-specific. Similarly with SPI controller drivers.
David Brownell8ae12a02006-01-08 13:34:19 -0800412 * Device registration normally goes into like arch/.../mach.../board-YYY.c
413 * with other readonly (flashable) information about mainboard devices.
414 */
415
416struct boardinfo {
417 struct list_head list;
Feng Tang2b9603a2010-08-02 15:52:15 +0800418 struct spi_board_info board_info;
David Brownell8ae12a02006-01-08 13:34:19 -0800419};
420
421static LIST_HEAD(board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200422static LIST_HEAD(spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +0800423
424/*
425 * Used to protect add/del opertion for board_info list and
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200426 * spi_controller list, and their matching process
Suniel Mahesh9a9a0472017-08-17 18:18:22 +0530427 * also used to protect object of type struct idr
Feng Tang2b9603a2010-08-02 15:52:15 +0800428 */
Matthias Kaehlcke94040822007-07-17 04:04:16 -0700429static DEFINE_MUTEX(board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800430
Grant Likelydc87c982008-05-15 16:50:22 -0600431/**
432 * spi_alloc_device - Allocate a new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200433 * @ctlr: Controller to which device is connected
Grant Likelydc87c982008-05-15 16:50:22 -0600434 * Context: can sleep
435 *
436 * Allows a driver to allocate and initialize a spi_device without
437 * registering it immediately. This allows a driver to directly
438 * fill the spi_device with device parameters before calling
439 * spi_add_device() on it.
440 *
441 * Caller is responsible to call spi_add_device() on the returned
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200442 * spi_device structure to add it to the SPI controller. If the caller
Grant Likelydc87c982008-05-15 16:50:22 -0600443 * needs to discard the spi_device without adding it, then it should
444 * call spi_dev_put() on it.
445 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200446 * Return: a pointer to the new device, or NULL.
Grant Likelydc87c982008-05-15 16:50:22 -0600447 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200448struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
Grant Likelydc87c982008-05-15 16:50:22 -0600449{
450 struct spi_device *spi;
Grant Likelydc87c982008-05-15 16:50:22 -0600451
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200452 if (!spi_controller_get(ctlr))
Grant Likelydc87c982008-05-15 16:50:22 -0600453 return NULL;
454
Jingoo Han5fe5f052013-10-14 10:31:51 +0900455 spi = kzalloc(sizeof(*spi), GFP_KERNEL);
Grant Likelydc87c982008-05-15 16:50:22 -0600456 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200457 spi_controller_put(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600458 return NULL;
459 }
460
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200461 spi->master = spi->controller = ctlr;
462 spi->dev.parent = &ctlr->dev;
Grant Likelydc87c982008-05-15 16:50:22 -0600463 spi->dev.bus = &spi_bus_type;
464 spi->dev.release = spidev_release;
Andreas Larsson446411e2013-02-13 14:20:25 +0100465 spi->cs_gpio = -ENOENT;
Martin Sperleca2ebc2015-06-22 13:00:36 +0000466
467 spin_lock_init(&spi->statistics.lock);
468
Grant Likelydc87c982008-05-15 16:50:22 -0600469 device_initialize(&spi->dev);
470 return spi;
471}
472EXPORT_SYMBOL_GPL(spi_alloc_device);
473
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200474static void spi_dev_set_name(struct spi_device *spi)
475{
476 struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
477
478 if (adev) {
479 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
480 return;
481 }
482
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200483 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200484 spi->chip_select);
485}
486
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200487static int spi_dev_check(struct device *dev, void *data)
488{
489 struct spi_device *spi = to_spi_device(dev);
490 struct spi_device *new_spi = data;
491
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200492 if (spi->controller == new_spi->controller &&
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200493 spi->chip_select == new_spi->chip_select)
494 return -EBUSY;
495 return 0;
496}
497
Grant Likelydc87c982008-05-15 16:50:22 -0600498/**
499 * spi_add_device - Add spi_device allocated with spi_alloc_device
500 * @spi: spi_device to register
501 *
502 * Companion function to spi_alloc_device. Devices allocated with
503 * spi_alloc_device can be added onto the spi bus with this function.
504 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200505 * Return: 0 on success; negative errno on failure
Grant Likelydc87c982008-05-15 16:50:22 -0600506 */
507int spi_add_device(struct spi_device *spi)
508{
David Brownelle48880e2008-08-15 00:40:44 -0700509 static DEFINE_MUTEX(spi_add_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200510 struct spi_controller *ctlr = spi->controller;
511 struct device *dev = ctlr->dev.parent;
Grant Likelydc87c982008-05-15 16:50:22 -0600512 int status;
513
514 /* Chipselects are numbered 0..max; validate. */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200515 if (spi->chip_select >= ctlr->num_chipselect) {
516 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
517 ctlr->num_chipselect);
Grant Likelydc87c982008-05-15 16:50:22 -0600518 return -EINVAL;
519 }
520
521 /* Set the bus ID string */
Jarkko Nikulae13ac472013-11-14 14:03:53 +0200522 spi_dev_set_name(spi);
David Brownelle48880e2008-08-15 00:40:44 -0700523
524 /* We need to make sure there's no other device with this
525 * chipselect **BEFORE** we call setup(), else we'll trash
526 * its configuration. Lock against concurrent add() calls.
527 */
528 mutex_lock(&spi_add_lock);
529
Mika Westerbergb6fb8d32014-01-09 15:23:55 +0200530 status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
531 if (status) {
David Brownelle48880e2008-08-15 00:40:44 -0700532 dev_err(dev, "chipselect %d already in use\n",
533 spi->chip_select);
David Brownelle48880e2008-08-15 00:40:44 -0700534 goto done;
535 }
536
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200537 if (ctlr->cs_gpios)
538 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +0100539
David Brownelle48880e2008-08-15 00:40:44 -0700540 /* Drivers may modify this initial i/o setup, but will
541 * normally rely on the device being setup. Devices
542 * using SPI_CS_HIGH can't coexist well otherwise...
543 */
David Brownell7d077192009-06-17 16:26:03 -0700544 status = spi_setup(spi);
Grant Likelydc87c982008-05-15 16:50:22 -0600545 if (status < 0) {
Linus Walleijeb288a12010-10-21 21:06:44 +0200546 dev_err(dev, "can't setup %s, status %d\n",
547 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700548 goto done;
Grant Likelydc87c982008-05-15 16:50:22 -0600549 }
550
David Brownelle48880e2008-08-15 00:40:44 -0700551 /* Device may be bound to an active driver when this returns */
Grant Likelydc87c982008-05-15 16:50:22 -0600552 status = device_add(&spi->dev);
David Brownelle48880e2008-08-15 00:40:44 -0700553 if (status < 0)
Linus Walleijeb288a12010-10-21 21:06:44 +0200554 dev_err(dev, "can't add %s, status %d\n",
555 dev_name(&spi->dev), status);
David Brownelle48880e2008-08-15 00:40:44 -0700556 else
Kay Sievers35f74fc2009-01-06 10:44:37 -0800557 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
Grant Likelydc87c982008-05-15 16:50:22 -0600558
David Brownelle48880e2008-08-15 00:40:44 -0700559done:
560 mutex_unlock(&spi_add_lock);
561 return status;
Grant Likelydc87c982008-05-15 16:50:22 -0600562}
563EXPORT_SYMBOL_GPL(spi_add_device);
David Brownell8ae12a02006-01-08 13:34:19 -0800564
David Brownell33e34dc2007-05-08 00:32:21 -0700565/**
566 * spi_new_device - instantiate one new SPI device
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200567 * @ctlr: Controller to which device is connected
David Brownell33e34dc2007-05-08 00:32:21 -0700568 * @chip: Describes the SPI device
569 * Context: can sleep
570 *
571 * On typical mainboards, this is purely internal; and it's not needed
David Brownell8ae12a02006-01-08 13:34:19 -0800572 * after board init creates the hard-wired devices. Some development
573 * platforms may not be able to use spi_register_board_info though, and
574 * this is exported so that for example a USB or parport based adapter
575 * driver could add devices (which it would learn about out-of-band).
David Brownell082c8cb2007-07-31 00:39:45 -0700576 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200577 * Return: the new device, or NULL.
David Brownell8ae12a02006-01-08 13:34:19 -0800578 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200579struct spi_device *spi_new_device(struct spi_controller *ctlr,
Adrian Bunke9d5a462007-03-26 21:32:23 -0800580 struct spi_board_info *chip)
David Brownell8ae12a02006-01-08 13:34:19 -0800581{
582 struct spi_device *proxy;
David Brownell8ae12a02006-01-08 13:34:19 -0800583 int status;
584
David Brownell082c8cb2007-07-31 00:39:45 -0700585 /* NOTE: caller did any chip->bus_num checks necessary.
586 *
587 * Also, unless we change the return value convention to use
588 * error-or-pointer (not NULL-or-pointer), troubleshootability
589 * suggests syslogged diagnostics are best here (ugh).
590 */
591
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200592 proxy = spi_alloc_device(ctlr);
Grant Likelydc87c982008-05-15 16:50:22 -0600593 if (!proxy)
David Brownell8ae12a02006-01-08 13:34:19 -0800594 return NULL;
595
Grant Likely102eb972008-07-23 21:29:55 -0700596 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
597
David Brownell8ae12a02006-01-08 13:34:19 -0800598 proxy->chip_select = chip->chip_select;
599 proxy->max_speed_hz = chip->max_speed_hz;
David Brownell980a01c2006-06-28 07:47:15 -0700600 proxy->mode = chip->mode;
David Brownell8ae12a02006-01-08 13:34:19 -0800601 proxy->irq = chip->irq;
Grant Likely102eb972008-07-23 21:29:55 -0700602 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
David Brownell8ae12a02006-01-08 13:34:19 -0800603 proxy->dev.platform_data = (void *) chip->platform_data;
604 proxy->controller_data = chip->controller_data;
605 proxy->controller_state = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800606
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800607 if (chip->properties) {
608 status = device_add_properties(&proxy->dev, chip->properties);
609 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200610 dev_err(&ctlr->dev,
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800611 "failed to add properties to '%s': %d\n",
612 chip->modalias, status);
613 goto err_dev_put;
614 }
David Brownell8ae12a02006-01-08 13:34:19 -0800615 }
616
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800617 status = spi_add_device(proxy);
618 if (status < 0)
619 goto err_remove_props;
620
David Brownell8ae12a02006-01-08 13:34:19 -0800621 return proxy;
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800622
623err_remove_props:
624 if (chip->properties)
625 device_remove_properties(&proxy->dev);
626err_dev_put:
627 spi_dev_put(proxy);
628 return NULL;
David Brownell8ae12a02006-01-08 13:34:19 -0800629}
630EXPORT_SYMBOL_GPL(spi_new_device);
631
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100632/**
633 * spi_unregister_device - unregister a single SPI device
634 * @spi: spi_device to unregister
635 *
636 * Start making the passed SPI device vanish. Normally this would be handled
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200637 * by spi_unregister_controller().
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100638 */
639void spi_unregister_device(struct spi_device *spi)
640{
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100641 if (!spi)
642 return;
643
Johan Hovold83241472017-01-30 17:47:05 +0100644 if (spi->dev.of_node) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100645 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
Johan Hovold83241472017-01-30 17:47:05 +0100646 of_node_put(spi->dev.of_node);
647 }
Octavian Purdila7f244672016-07-08 19:13:11 +0300648 if (ACPI_COMPANION(&spi->dev))
649 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +0100650 device_unregister(&spi->dev);
Geert Uytterhoeven3b1884c2015-11-30 15:28:06 +0100651}
652EXPORT_SYMBOL_GPL(spi_unregister_device);
653
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200654static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
655 struct spi_board_info *bi)
Feng Tang2b9603a2010-08-02 15:52:15 +0800656{
657 struct spi_device *dev;
658
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200659 if (ctlr->bus_num != bi->bus_num)
Feng Tang2b9603a2010-08-02 15:52:15 +0800660 return;
661
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200662 dev = spi_new_device(ctlr, bi);
Feng Tang2b9603a2010-08-02 15:52:15 +0800663 if (!dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200664 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
Feng Tang2b9603a2010-08-02 15:52:15 +0800665 bi->modalias);
666}
667
David Brownell33e34dc2007-05-08 00:32:21 -0700668/**
669 * spi_register_board_info - register SPI devices for a given board
670 * @info: array of chip descriptors
671 * @n: how many descriptors are provided
672 * Context: can sleep
673 *
David Brownell8ae12a02006-01-08 13:34:19 -0800674 * Board-specific early init code calls this (probably during arch_initcall)
675 * with segments of the SPI device table. Any device nodes are created later,
676 * after the relevant parent SPI controller (bus_num) is defined. We keep
677 * this table of devices forever, so that reloading a controller driver will
678 * not make Linux forget about these hard-wired devices.
679 *
680 * Other code can also call this, e.g. a particular add-on board might provide
681 * SPI devices through its expansion connector, so code initializing that board
682 * would naturally declare its SPI devices.
683 *
684 * The board info passed can safely be __initdata ... but be careful of
685 * any embedded pointers (platform_data, etc), they're copied as-is.
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800686 * Device properties are deep-copied though.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +0200687 *
688 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -0800689 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000690int spi_register_board_info(struct spi_board_info const *info, unsigned n)
David Brownell8ae12a02006-01-08 13:34:19 -0800691{
Feng Tang2b9603a2010-08-02 15:52:15 +0800692 struct boardinfo *bi;
693 int i;
David Brownell8ae12a02006-01-08 13:34:19 -0800694
Xiubo Lic7908a32014-09-24 14:30:29 +0800695 if (!n)
Dmitry Torokhovf974cf52017-02-28 14:25:19 -0800696 return 0;
Xiubo Lic7908a32014-09-24 14:30:29 +0800697
Markus Elfringf9bdb7f2017-01-13 12:28:04 +0100698 bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
David Brownell8ae12a02006-01-08 13:34:19 -0800699 if (!bi)
700 return -ENOMEM;
David Brownell8ae12a02006-01-08 13:34:19 -0800701
Feng Tang2b9603a2010-08-02 15:52:15 +0800702 for (i = 0; i < n; i++, bi++, info++) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200703 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -0800704
Feng Tang2b9603a2010-08-02 15:52:15 +0800705 memcpy(&bi->board_info, info, sizeof(*info));
Dmitry Torokhov826cf172017-02-28 14:25:18 -0800706 if (info->properties) {
707 bi->board_info.properties =
708 property_entries_dup(info->properties);
709 if (IS_ERR(bi->board_info.properties))
710 return PTR_ERR(bi->board_info.properties);
711 }
712
Feng Tang2b9603a2010-08-02 15:52:15 +0800713 mutex_lock(&board_lock);
714 list_add_tail(&bi->list, &board_list);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200715 list_for_each_entry(ctlr, &spi_controller_list, list)
716 spi_match_controller_to_boardinfo(ctlr,
717 &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +0800718 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -0800719 }
Feng Tang2b9603a2010-08-02 15:52:15 +0800720
721 return 0;
David Brownell8ae12a02006-01-08 13:34:19 -0800722}
723
724/*-------------------------------------------------------------------------*/
725
Mark Brownb1589352013-10-05 11:50:40 +0100726static void spi_set_cs(struct spi_device *spi, bool enable)
727{
728 if (spi->mode & SPI_CS_HIGH)
729 enable = !enable;
730
Thor Thayer8eee6b92016-10-10 09:25:24 -0500731 if (gpio_is_valid(spi->cs_gpio)) {
Mark Brownb1589352013-10-05 11:50:40 +0100732 gpio_set_value(spi->cs_gpio, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500733 /* Some SPI masters need both GPIO CS & slave_select */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200734 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
735 spi->controller->set_cs)
736 spi->controller->set_cs(spi, !enable);
737 } else if (spi->controller->set_cs) {
738 spi->controller->set_cs(spi, !enable);
Thor Thayer8eee6b92016-10-10 09:25:24 -0500739 }
Mark Brownb1589352013-10-05 11:50:40 +0100740}
741
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200742#ifdef CONFIG_HAS_DMA
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200743static int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
Mark Brown6ad45a22014-02-02 13:47:47 +0000744 struct sg_table *sgt, void *buf, size_t len,
745 enum dma_data_direction dir)
746{
747 const bool vmalloced_buf = is_vmalloc_addr(buf);
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200748 unsigned int max_seg_size = dma_get_max_seg_size(dev);
Vignesh Rb1b81532016-08-17 15:22:36 +0530749#ifdef CONFIG_HIGHMEM
750 const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
751 (unsigned long)buf < (PKMAP_BASE +
752 (LAST_PKMAP * PAGE_SIZE)));
753#else
754 const bool kmap_buf = false;
755#endif
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500756 int desc_len;
757 int sgs;
Mark Brown6ad45a22014-02-02 13:47:47 +0000758 struct page *vm_page;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600759 struct scatterlist *sg;
Mark Brown6ad45a22014-02-02 13:47:47 +0000760 void *sg_buf;
761 size_t min;
762 int i, ret;
763
Vignesh Rb1b81532016-08-17 15:22:36 +0530764 if (vmalloced_buf || kmap_buf) {
Andy Shevchenkodf88e912016-03-09 11:20:00 +0200765 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500766 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530767 } else if (virt_addr_valid(buf)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200768 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500769 sgs = DIV_ROUND_UP(len, desc_len);
Vignesh R0569a882016-04-25 15:14:00 +0530770 } else {
771 return -EINVAL;
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500772 }
773
Mark Brown6ad45a22014-02-02 13:47:47 +0000774 ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
775 if (ret != 0)
776 return ret;
777
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600778 sg = &sgt->sgl[0];
Mark Brown6ad45a22014-02-02 13:47:47 +0000779 for (i = 0; i < sgs; i++) {
Mark Brown6ad45a22014-02-02 13:47:47 +0000780
Vignesh Rb1b81532016-08-17 15:22:36 +0530781 if (vmalloced_buf || kmap_buf) {
Maxime Chevallierce993192018-03-02 15:55:09 +0100782 /*
783 * Next scatterlist entry size is the minimum between
784 * the desc_len and the remaining buffer length that
785 * fits in a page.
786 */
787 min = min_t(size_t, desc_len,
788 min_t(size_t, len,
789 PAGE_SIZE - offset_in_page(buf)));
Vignesh Rb1b81532016-08-17 15:22:36 +0530790 if (vmalloced_buf)
791 vm_page = vmalloc_to_page(buf);
792 else
793 vm_page = kmap_to_page(buf);
Mark Brown6ad45a22014-02-02 13:47:47 +0000794 if (!vm_page) {
795 sg_free_table(sgt);
796 return -ENOMEM;
797 }
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600798 sg_set_page(sg, vm_page,
Charles Keepaxc1aefbd2014-11-17 09:14:31 +0000799 min, offset_in_page(buf));
Mark Brown6ad45a22014-02-02 13:47:47 +0000800 } else {
Andrew Gabbasov65598c12015-06-30 10:48:37 -0500801 min = min_t(size_t, len, desc_len);
Mark Brown6ad45a22014-02-02 13:47:47 +0000802 sg_buf = buf;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600803 sg_set_buf(sg, sg_buf, min);
Mark Brown6ad45a22014-02-02 13:47:47 +0000804 }
805
Mark Brown6ad45a22014-02-02 13:47:47 +0000806 buf += min;
807 len -= min;
Juan Gutierrez8dd4a012016-11-21 16:50:03 -0600808 sg = sg_next(sg);
Mark Brown6ad45a22014-02-02 13:47:47 +0000809 }
810
811 ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
Geert Uytterhoeven89e4b662014-07-10 15:29:32 +0200812 if (!ret)
813 ret = -ENOMEM;
Mark Brown6ad45a22014-02-02 13:47:47 +0000814 if (ret < 0) {
815 sg_free_table(sgt);
816 return ret;
817 }
818
819 sgt->nents = ret;
820
821 return 0;
822}
823
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200824static void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
Mark Brown6ad45a22014-02-02 13:47:47 +0000825 struct sg_table *sgt, enum dma_data_direction dir)
826{
827 if (sgt->orig_nents) {
828 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
829 sg_free_table(sgt);
830 }
831}
832
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200833static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000834{
Mark Brown99adef32014-01-16 12:22:43 +0000835 struct device *tx_dev, *rx_dev;
836 struct spi_transfer *xfer;
Mark Brown6ad45a22014-02-02 13:47:47 +0000837 int ret;
Mark Brown3a2eba92014-01-28 20:17:03 +0000838
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200839 if (!ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000840 return 0;
841
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200842 if (ctlr->dma_tx)
843 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800844 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200845 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800846
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200847 if (ctlr->dma_rx)
848 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800849 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200850 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000851
852 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200853 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000854 continue;
855
856 if (xfer->tx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200857 ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000858 (void *)xfer->tx_buf, xfer->len,
859 DMA_TO_DEVICE);
860 if (ret != 0)
861 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000862 }
863
864 if (xfer->rx_buf != NULL) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200865 ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000866 xfer->rx_buf, xfer->len,
867 DMA_FROM_DEVICE);
868 if (ret != 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200869 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
Mark Brown6ad45a22014-02-02 13:47:47 +0000870 DMA_TO_DEVICE);
871 return ret;
Mark Brown99adef32014-01-16 12:22:43 +0000872 }
873 }
874 }
875
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200876 ctlr->cur_msg_mapped = true;
Mark Brown99adef32014-01-16 12:22:43 +0000877
878 return 0;
879}
880
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200881static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
Mark Brown99adef32014-01-16 12:22:43 +0000882{
883 struct spi_transfer *xfer;
884 struct device *tx_dev, *rx_dev;
885
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200886 if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
Mark Brown99adef32014-01-16 12:22:43 +0000887 return 0;
888
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200889 if (ctlr->dma_tx)
890 tx_dev = ctlr->dma_tx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800891 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200892 tx_dev = ctlr->dev.parent;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800893
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200894 if (ctlr->dma_rx)
895 rx_dev = ctlr->dma_rx->device->dev;
Leilk Liuc37f45b2015-07-23 17:10:40 +0800896 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200897 rx_dev = ctlr->dev.parent;
Mark Brown99adef32014-01-16 12:22:43 +0000898
899 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200900 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
Mark Brown99adef32014-01-16 12:22:43 +0000901 continue;
902
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200903 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
904 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
Mark Brown99adef32014-01-16 12:22:43 +0000905 }
906
907 return 0;
908}
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200909#else /* !CONFIG_HAS_DMA */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200910static inline int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
911 struct sg_table *sgt, void *buf, size_t len,
Vignesh Rf4502dd2016-06-08 12:18:31 +0530912 enum dma_data_direction dir)
913{
914 return -EINVAL;
915}
916
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200917static inline void spi_unmap_buf(struct spi_controller *ctlr,
Vignesh Rf4502dd2016-06-08 12:18:31 +0530918 struct device *dev, struct sg_table *sgt,
919 enum dma_data_direction dir)
920{
921}
922
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200923static inline int __spi_map_msg(struct spi_controller *ctlr,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200924 struct spi_message *msg)
925{
926 return 0;
927}
928
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200929static inline int __spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000930 struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200931{
932 return 0;
933}
934#endif /* !CONFIG_HAS_DMA */
935
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200936static inline int spi_unmap_msg(struct spi_controller *ctlr,
Martin Sperl4b786452015-05-25 10:13:10 +0000937 struct spi_message *msg)
938{
939 struct spi_transfer *xfer;
940
941 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
942 /*
943 * Restore the original value of tx_buf or rx_buf if they are
944 * NULL.
945 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200946 if (xfer->tx_buf == ctlr->dummy_tx)
Martin Sperl4b786452015-05-25 10:13:10 +0000947 xfer->tx_buf = NULL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200948 if (xfer->rx_buf == ctlr->dummy_rx)
Martin Sperl4b786452015-05-25 10:13:10 +0000949 xfer->rx_buf = NULL;
950 }
951
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200952 return __spi_unmap_msg(ctlr, msg);
Martin Sperl4b786452015-05-25 10:13:10 +0000953}
954
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200955static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200956{
957 struct spi_transfer *xfer;
958 void *tmp;
959 unsigned int max_tx, max_rx;
960
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200961 if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200962 max_tx = 0;
963 max_rx = 0;
964
965 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200966 if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200967 !xfer->tx_buf)
968 max_tx = max(xfer->len, max_tx);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200969 if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200970 !xfer->rx_buf)
971 max_rx = max(xfer->len, max_rx);
972 }
973
974 if (max_tx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200975 tmp = krealloc(ctlr->dummy_tx, max_tx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200976 GFP_KERNEL | GFP_DMA);
977 if (!tmp)
978 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200979 ctlr->dummy_tx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200980 memset(tmp, 0, max_tx);
981 }
982
983 if (max_rx) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200984 tmp = krealloc(ctlr->dummy_rx, max_rx,
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200985 GFP_KERNEL | GFP_DMA);
986 if (!tmp)
987 return -ENOMEM;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200988 ctlr->dummy_rx = tmp;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200989 }
990
991 if (max_tx || max_rx) {
992 list_for_each_entry(xfer, &msg->transfers,
993 transfer_list) {
994 if (!xfer->tx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200995 xfer->tx_buf = ctlr->dummy_tx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200996 if (!xfer->rx_buf)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +0200997 xfer->rx_buf = ctlr->dummy_rx;
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +0200998 }
999 }
1000 }
1001
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001002 return __spi_map_msg(ctlr, msg);
Geert Uytterhoeven2de440f2014-05-02 06:29:34 +02001003}
Mark Brown99adef32014-01-16 12:22:43 +00001004
Mark Brownb1589352013-10-05 11:50:40 +01001005/*
1006 * spi_transfer_one_message - Default implementation of transfer_one_message()
1007 *
1008 * This is a standard implementation of transfer_one_message() for
Moritz Fischer8ba811a2016-05-03 11:59:30 -07001009 * drivers which implement a transfer_one() operation. It provides
Mark Brownb1589352013-10-05 11:50:40 +01001010 * standard handling of delays and chip select management.
1011 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001012static int spi_transfer_one_message(struct spi_controller *ctlr,
Mark Brownb1589352013-10-05 11:50:40 +01001013 struct spi_message *msg)
1014{
1015 struct spi_transfer *xfer;
Mark Brownb1589352013-10-05 11:50:40 +01001016 bool keep_cs = false;
1017 int ret = 0;
Sien Wud0716dd2016-09-01 18:24:29 -05001018 unsigned long long ms = 1;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001019 struct spi_statistics *statm = &ctlr->statistics;
Martin Sperleca2ebc2015-06-22 13:00:36 +00001020 struct spi_statistics *stats = &msg->spi->statistics;
Mark Brownb1589352013-10-05 11:50:40 +01001021
1022 spi_set_cs(msg->spi, true);
1023
Martin Sperleca2ebc2015-06-22 13:00:36 +00001024 SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
1025 SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
1026
Mark Brownb1589352013-10-05 11:50:40 +01001027 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1028 trace_spi_transfer_start(msg, xfer);
1029
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001030 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
1031 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
Martin Sperleca2ebc2015-06-22 13:00:36 +00001032
Mark Brown38ec10f2014-08-16 16:27:41 +01001033 if (xfer->tx_buf || xfer->rx_buf) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001034 reinit_completion(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001035
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001036 ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
Mark Brown38ec10f2014-08-16 16:27:41 +01001037 if (ret < 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001038 SPI_STATISTICS_INCREMENT_FIELD(statm,
1039 errors);
1040 SPI_STATISTICS_INCREMENT_FIELD(stats,
1041 errors);
Mark Brown38ec10f2014-08-16 16:27:41 +01001042 dev_err(&msg->spi->dev,
1043 "SPI transfer failed: %d\n", ret);
1044 goto out;
1045 }
Mark Brownb1589352013-10-05 11:50:40 +01001046
Mark Brown38ec10f2014-08-16 16:27:41 +01001047 if (ret > 0) {
1048 ret = 0;
Sien Wud0716dd2016-09-01 18:24:29 -05001049 ms = 8LL * 1000LL * xfer->len;
1050 do_div(ms, xfer->speed_hz);
Hauke Mehrtens833bfad2017-04-17 01:38:05 +02001051 ms += ms + 200; /* some tolerance */
Mark Brown16a0ce42014-01-30 22:16:41 +00001052
Sien Wud0716dd2016-09-01 18:24:29 -05001053 if (ms > UINT_MAX)
1054 ms = UINT_MAX;
1055
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001056 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
Mark Brown38ec10f2014-08-16 16:27:41 +01001057 msecs_to_jiffies(ms));
1058 }
Mark Brown16a0ce42014-01-30 22:16:41 +00001059
Mark Brown38ec10f2014-08-16 16:27:41 +01001060 if (ms == 0) {
Martin Sperleca2ebc2015-06-22 13:00:36 +00001061 SPI_STATISTICS_INCREMENT_FIELD(statm,
1062 timedout);
1063 SPI_STATISTICS_INCREMENT_FIELD(stats,
1064 timedout);
Mark Brown38ec10f2014-08-16 16:27:41 +01001065 dev_err(&msg->spi->dev,
1066 "SPI transfer timed out\n");
1067 msg->status = -ETIMEDOUT;
1068 }
1069 } else {
1070 if (xfer->len)
1071 dev_err(&msg->spi->dev,
1072 "Bufferless transfer has length %u\n",
1073 xfer->len);
Axel Lin13a42792014-01-18 22:05:22 +08001074 }
Mark Brownb1589352013-10-05 11:50:40 +01001075
1076 trace_spi_transfer_stop(msg, xfer);
1077
1078 if (msg->status != -EINPROGRESS)
1079 goto out;
1080
Daniel Kurtz8244bd32016-10-07 18:55:47 +08001081 if (xfer->delay_usecs) {
1082 u16 us = xfer->delay_usecs;
1083
1084 if (us <= 10)
1085 udelay(us);
1086 else
1087 usleep_range(us, us + DIV_ROUND_UP(us, 10));
1088 }
Mark Brownb1589352013-10-05 11:50:40 +01001089
1090 if (xfer->cs_change) {
1091 if (list_is_last(&xfer->transfer_list,
1092 &msg->transfers)) {
1093 keep_cs = true;
1094 } else {
Mark Brown0b73aa62014-03-29 23:48:07 +00001095 spi_set_cs(msg->spi, false);
1096 udelay(10);
1097 spi_set_cs(msg->spi, true);
Mark Brownb1589352013-10-05 11:50:40 +01001098 }
1099 }
1100
1101 msg->actual_length += xfer->len;
1102 }
1103
1104out:
1105 if (ret != 0 || !keep_cs)
1106 spi_set_cs(msg->spi, false);
1107
1108 if (msg->status == -EINPROGRESS)
1109 msg->status = ret;
1110
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001111 if (msg->status && ctlr->handle_err)
1112 ctlr->handle_err(ctlr, msg);
Andy Shevchenkob716c4f2015-02-27 17:34:15 +02001113
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001114 spi_res_release(ctlr, msg);
Martin Sperld780c372015-12-14 15:20:18 +00001115
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001116 spi_finalize_current_message(ctlr);
Mark Brownb1589352013-10-05 11:50:40 +01001117
1118 return ret;
1119}
1120
1121/**
1122 * spi_finalize_current_transfer - report completion of a transfer
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001123 * @ctlr: the controller reporting completion
Mark Brownb1589352013-10-05 11:50:40 +01001124 *
1125 * Called by SPI drivers using the core transfer_one_message()
1126 * implementation to notify it that the current interrupt driven
Geert Uytterhoeven9e8f4882014-01-21 16:10:05 +01001127 * transfer has finished and the next one may be scheduled.
Mark Brownb1589352013-10-05 11:50:40 +01001128 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001129void spi_finalize_current_transfer(struct spi_controller *ctlr)
Mark Brownb1589352013-10-05 11:50:40 +01001130{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001131 complete(&ctlr->xfer_completion);
Mark Brownb1589352013-10-05 11:50:40 +01001132}
1133EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
1134
Linus Walleijffbbdd212012-02-22 10:05:38 +01001135/**
Mark Brownfc9e0f72014-12-10 13:46:33 +00001136 * __spi_pump_messages - function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001137 * @ctlr: controller to process queue for
Mark Brownfc9e0f72014-12-10 13:46:33 +00001138 * @in_kthread: true if we are in the context of the message pump thread
Linus Walleijffbbdd212012-02-22 10:05:38 +01001139 *
1140 * This function checks if there is any spi message in the queue that
1141 * needs processing and if so call out to the driver to initialize hardware
1142 * and transfer each message.
1143 *
Mark Brown0461a412014-12-09 21:38:05 +00001144 * Note that it is called both from the kthread itself and also from
1145 * inside spi_sync(); the queue extraction handling at the top of the
1146 * function should deal with this safely.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001147 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001148static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001149{
Linus Walleijffbbdd212012-02-22 10:05:38 +01001150 unsigned long flags;
1151 bool was_busy = false;
1152 int ret;
1153
Mark Brown983aee52014-12-09 19:46:56 +00001154 /* Lock queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001155 spin_lock_irqsave(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001156
1157 /* Make sure we are not already running a message */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001158 if (ctlr->cur_msg) {
1159 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown983aee52014-12-09 19:46:56 +00001160 return;
1161 }
1162
Mark Brown0461a412014-12-09 21:38:05 +00001163 /* If another context is idling the device then defer */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001164 if (ctlr->idling) {
1165 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1166 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001167 return;
1168 }
1169
Mark Brown983aee52014-12-09 19:46:56 +00001170 /* Check if the queue is idle */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001171 if (list_empty(&ctlr->queue) || !ctlr->running) {
1172 if (!ctlr->busy) {
1173 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Bryan Freedb0b36b82013-03-13 11:17:40 -07001174 return;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001175 }
Mark Brownfc9e0f72014-12-10 13:46:33 +00001176
1177 /* Only do teardown in the thread */
1178 if (!in_kthread) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001179 kthread_queue_work(&ctlr->kworker,
1180 &ctlr->pump_messages);
1181 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001182 return;
1183 }
1184
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001185 ctlr->busy = false;
1186 ctlr->idling = true;
1187 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00001188
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001189 kfree(ctlr->dummy_rx);
1190 ctlr->dummy_rx = NULL;
1191 kfree(ctlr->dummy_tx);
1192 ctlr->dummy_tx = NULL;
1193 if (ctlr->unprepare_transfer_hardware &&
1194 ctlr->unprepare_transfer_hardware(ctlr))
1195 dev_err(&ctlr->dev,
Bryan Freedb0b36b82013-03-13 11:17:40 -07001196 "failed to unprepare transfer hardware\n");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001197 if (ctlr->auto_runtime_pm) {
1198 pm_runtime_mark_last_busy(ctlr->dev.parent);
1199 pm_runtime_put_autosuspend(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001200 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001201 trace_spi_controller_idle(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001202
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001203 spin_lock_irqsave(&ctlr->queue_lock, flags);
1204 ctlr->idling = false;
1205 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001206 return;
1207 }
Linus Walleijffbbdd212012-02-22 10:05:38 +01001208
Linus Walleijffbbdd212012-02-22 10:05:38 +01001209 /* Extract head of queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001210 ctlr->cur_msg =
1211 list_first_entry(&ctlr->queue, struct spi_message, queue);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001212
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001213 list_del_init(&ctlr->cur_msg->queue);
1214 if (ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001215 was_busy = true;
1216 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001217 ctlr->busy = true;
1218 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001219
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001220 mutex_lock(&ctlr->io_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01001221
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001222 if (!was_busy && ctlr->auto_runtime_pm) {
1223 ret = pm_runtime_get_sync(ctlr->dev.parent);
Mark Brown49834de2013-07-28 14:47:02 +01001224 if (ret < 0) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001225 dev_err(&ctlr->dev, "Failed to power device: %d\n",
Mark Brown49834de2013-07-28 14:47:02 +01001226 ret);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001227 mutex_unlock(&ctlr->io_mutex);
Mark Brown49834de2013-07-28 14:47:02 +01001228 return;
1229 }
1230 }
1231
Mark Brown56ec1972013-10-07 19:33:53 +01001232 if (!was_busy)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001233 trace_spi_controller_busy(ctlr);
Mark Brown56ec1972013-10-07 19:33:53 +01001234
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001235 if (!was_busy && ctlr->prepare_transfer_hardware) {
1236 ret = ctlr->prepare_transfer_hardware(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001237 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001238 dev_err(&ctlr->dev,
Linus Walleijffbbdd212012-02-22 10:05:38 +01001239 "failed to prepare transfer hardware\n");
Mark Brown49834de2013-07-28 14:47:02 +01001240
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001241 if (ctlr->auto_runtime_pm)
1242 pm_runtime_put(ctlr->dev.parent);
1243 mutex_unlock(&ctlr->io_mutex);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001244 return;
1245 }
1246 }
1247
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001248 trace_spi_message_start(ctlr->cur_msg);
Mark Brown56ec1972013-10-07 19:33:53 +01001249
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001250 if (ctlr->prepare_message) {
1251 ret = ctlr->prepare_message(ctlr, ctlr->cur_msg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001252 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001253 dev_err(&ctlr->dev, "failed to prepare message: %d\n",
1254 ret);
1255 ctlr->cur_msg->status = ret;
1256 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001257 goto out;
Mark Brown2841a5f2013-10-05 00:23:12 +01001258 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001259 ctlr->cur_msg_prepared = true;
Mark Brown2841a5f2013-10-05 00:23:12 +01001260 }
1261
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001262 ret = spi_map_msg(ctlr, ctlr->cur_msg);
Mark Brown99adef32014-01-16 12:22:43 +00001263 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001264 ctlr->cur_msg->status = ret;
1265 spi_finalize_current_message(ctlr);
Jon Hunter49023d22016-03-08 12:28:20 +00001266 goto out;
Mark Brown99adef32014-01-16 12:22:43 +00001267 }
1268
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001269 ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001270 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001271 dev_err(&ctlr->dev,
Geert Uytterhoeven1f802f82014-01-28 10:33:03 +01001272 "failed to transfer one message from queue\n");
Jon Hunter49023d22016-03-08 12:28:20 +00001273 goto out;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001274 }
Jon Hunter49023d22016-03-08 12:28:20 +00001275
1276out:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001277 mutex_unlock(&ctlr->io_mutex);
Mark Brown62826972016-02-15 18:28:02 +00001278
1279 /* Prod the scheduler in case transfer_one() was busy waiting */
Jon Hunter49023d22016-03-08 12:28:20 +00001280 if (!ret)
1281 cond_resched();
Linus Walleijffbbdd212012-02-22 10:05:38 +01001282}
1283
Mark Brownfc9e0f72014-12-10 13:46:33 +00001284/**
1285 * spi_pump_messages - kthread work function which processes spi message queue
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001286 * @work: pointer to kthread work struct contained in the controller struct
Mark Brownfc9e0f72014-12-10 13:46:33 +00001287 */
1288static void spi_pump_messages(struct kthread_work *work)
1289{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001290 struct spi_controller *ctlr =
1291 container_of(work, struct spi_controller, pump_messages);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001292
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001293 __spi_pump_messages(ctlr, true);
Mark Brownfc9e0f72014-12-10 13:46:33 +00001294}
1295
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001296static int spi_init_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001297{
1298 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
1299
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001300 ctlr->running = false;
1301 ctlr->busy = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001302
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001303 kthread_init_worker(&ctlr->kworker);
1304 ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
1305 "%s", dev_name(&ctlr->dev));
1306 if (IS_ERR(ctlr->kworker_task)) {
1307 dev_err(&ctlr->dev, "failed to create message pump task\n");
1308 return PTR_ERR(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001309 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001310 kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001311
1312 /*
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001313 * Controller config will indicate if this controller should run the
Linus Walleijffbbdd212012-02-22 10:05:38 +01001314 * message pump with high (realtime) priority to reduce the transfer
1315 * latency on the bus by minimising the delay between a transfer
1316 * request and the scheduling of the message pump thread. Without this
1317 * setting the message pump thread will remain at default priority.
1318 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001319 if (ctlr->rt) {
1320 dev_info(&ctlr->dev,
Linus Walleijffbbdd212012-02-22 10:05:38 +01001321 "will run message pump with realtime priority\n");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001322 sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001323 }
1324
1325 return 0;
1326}
1327
1328/**
1329 * spi_get_next_queued_message() - called by driver to check for queued
1330 * messages
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001331 * @ctlr: the controller to check for queued messages
Linus Walleijffbbdd212012-02-22 10:05:38 +01001332 *
1333 * If there are more messages in the queue, the next message is returned from
1334 * this call.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001335 *
1336 * Return: the next message in the queue, else NULL if the queue is empty.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001337 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001338struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001339{
1340 struct spi_message *next;
1341 unsigned long flags;
1342
1343 /* get a pointer to the next message, if any */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001344 spin_lock_irqsave(&ctlr->queue_lock, flags);
1345 next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
Axel Lin1cfd97f2014-01-02 15:16:40 +08001346 queue);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001347 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001348
1349 return next;
1350}
1351EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
1352
1353/**
1354 * spi_finalize_current_message() - the current message is complete
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001355 * @ctlr: the controller to return the message to
Linus Walleijffbbdd212012-02-22 10:05:38 +01001356 *
1357 * Called by the driver to notify the core that the message in the front of the
1358 * queue is complete and can be removed from the queue.
1359 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001360void spi_finalize_current_message(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001361{
1362 struct spi_message *mesg;
1363 unsigned long flags;
Mark Brown2841a5f2013-10-05 00:23:12 +01001364 int ret;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001365
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001366 spin_lock_irqsave(&ctlr->queue_lock, flags);
1367 mesg = ctlr->cur_msg;
1368 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001369
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001370 spi_unmap_msg(ctlr, mesg);
Mark Brown99adef32014-01-16 12:22:43 +00001371
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001372 if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
1373 ret = ctlr->unprepare_message(ctlr, mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001374 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001375 dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
1376 ret);
Mark Brown2841a5f2013-10-05 00:23:12 +01001377 }
1378 }
Uwe Kleine-König391949b2015-03-18 11:27:28 +01001379
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001380 spin_lock_irqsave(&ctlr->queue_lock, flags);
1381 ctlr->cur_msg = NULL;
1382 ctlr->cur_msg_prepared = false;
1383 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
1384 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Martin Sperl8e76ef82015-05-10 07:50:45 +00001385
1386 trace_spi_message_done(mesg);
Mark Brown2841a5f2013-10-05 00:23:12 +01001387
Linus Walleijffbbdd212012-02-22 10:05:38 +01001388 mesg->state = NULL;
1389 if (mesg->complete)
1390 mesg->complete(mesg->context);
1391}
1392EXPORT_SYMBOL_GPL(spi_finalize_current_message);
1393
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001394static int spi_start_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001395{
1396 unsigned long flags;
1397
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001398 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001399
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001400 if (ctlr->running || ctlr->busy) {
1401 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001402 return -EBUSY;
1403 }
1404
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001405 ctlr->running = true;
1406 ctlr->cur_msg = NULL;
1407 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001408
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001409 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001410
1411 return 0;
1412}
1413
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001414static int spi_stop_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001415{
1416 unsigned long flags;
1417 unsigned limit = 500;
1418 int ret = 0;
1419
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001420 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001421
1422 /*
1423 * This is a bit lame, but is optimized for the common execution path.
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001424 * A wait_queue on the ctlr->busy could be used, but then the common
Linus Walleijffbbdd212012-02-22 10:05:38 +01001425 * execution path (pump_messages) would be required to call wake_up or
1426 * friends on every SPI message. Do this instead.
1427 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001428 while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
1429 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Axel Linf97b26b2014-02-21 09:15:18 +08001430 usleep_range(10000, 11000);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001431 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001432 }
1433
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001434 if (!list_empty(&ctlr->queue) || ctlr->busy)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001435 ret = -EBUSY;
1436 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001437 ctlr->running = false;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001438
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001439 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001440
1441 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001442 dev_warn(&ctlr->dev, "could not stop message queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001443 return ret;
1444 }
1445 return ret;
1446}
1447
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001448static int spi_destroy_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001449{
1450 int ret;
1451
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001452 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001453
1454 /*
Petr Mladek39891442016-10-11 13:55:20 -07001455 * kthread_flush_worker will block until all work is done.
Linus Walleijffbbdd212012-02-22 10:05:38 +01001456 * If the reason that stop_queue timed out is that the work will never
1457 * finish, then it does no good to call flush/stop thread, so
1458 * return anyway.
1459 */
1460 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001461 dev_err(&ctlr->dev, "problem destroying queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001462 return ret;
1463 }
1464
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001465 kthread_flush_worker(&ctlr->kworker);
1466 kthread_stop(ctlr->kworker_task);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001467
1468 return 0;
1469}
1470
Mark Brown0461a412014-12-09 21:38:05 +00001471static int __spi_queued_transfer(struct spi_device *spi,
1472 struct spi_message *msg,
1473 bool need_pump)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001474{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001475 struct spi_controller *ctlr = spi->controller;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001476 unsigned long flags;
1477
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001478 spin_lock_irqsave(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001479
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001480 if (!ctlr->running) {
1481 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001482 return -ESHUTDOWN;
1483 }
1484 msg->actual_length = 0;
1485 msg->status = -EINPROGRESS;
1486
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001487 list_add_tail(&msg->queue, &ctlr->queue);
1488 if (!ctlr->busy && need_pump)
1489 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001490
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001491 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001492 return 0;
1493}
1494
Mark Brown0461a412014-12-09 21:38:05 +00001495/**
1496 * spi_queued_transfer - transfer function for queued transfers
1497 * @spi: spi device which is requesting transfer
1498 * @msg: spi message which is to handled is queued to driver queue
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001499 *
1500 * Return: zero on success, else a negative error code.
Mark Brown0461a412014-12-09 21:38:05 +00001501 */
1502static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
1503{
1504 return __spi_queued_transfer(spi, msg, true);
1505}
1506
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001507static int spi_controller_initialize_queue(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01001508{
1509 int ret;
1510
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001511 ctlr->transfer = spi_queued_transfer;
1512 if (!ctlr->transfer_one_message)
1513 ctlr->transfer_one_message = spi_transfer_one_message;
Linus Walleijffbbdd212012-02-22 10:05:38 +01001514
1515 /* Initialize and start queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001516 ret = spi_init_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001517 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001518 dev_err(&ctlr->dev, "problem initializing queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001519 goto err_init_queue;
1520 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001521 ctlr->queued = true;
1522 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01001523 if (ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001524 dev_err(&ctlr->dev, "problem starting queue\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01001525 goto err_start_queue;
1526 }
1527
1528 return 0;
1529
1530err_start_queue:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001531 spi_destroy_queue(ctlr);
Mark Brownc3676d52014-05-01 10:47:52 -07001532err_init_queue:
Linus Walleijffbbdd212012-02-22 10:05:38 +01001533 return ret;
1534}
1535
1536/*-------------------------------------------------------------------------*/
1537
Andreas Larsson7cb94362012-12-04 15:09:38 +01001538#if defined(CONFIG_OF)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001539static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001540 struct device_node *nc)
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001541{
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001542 u32 value;
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001543 int rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001544
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001545 /* Mode (clock phase/polarity/etc.) */
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001546 if (of_property_read_bool(nc, "spi-cpha"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001547 spi->mode |= SPI_CPHA;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001548 if (of_property_read_bool(nc, "spi-cpol"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001549 spi->mode |= SPI_CPOL;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001550 if (of_property_read_bool(nc, "spi-cs-high"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001551 spi->mode |= SPI_CS_HIGH;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001552 if (of_property_read_bool(nc, "spi-3wire"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001553 spi->mode |= SPI_3WIRE;
Sergei Shtylyove0bcb682017-08-06 23:15:31 +03001554 if (of_property_read_bool(nc, "spi-lsb-first"))
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001555 spi->mode |= SPI_LSB_FIRST;
1556
1557 /* Device DUAL/QUAD mode */
1558 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1559 switch (value) {
1560 case 1:
1561 break;
1562 case 2:
1563 spi->mode |= SPI_TX_DUAL;
1564 break;
1565 case 4:
1566 spi->mode |= SPI_TX_QUAD;
1567 break;
1568 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001569 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001570 "spi-tx-bus-width %d not supported\n",
1571 value);
1572 break;
1573 }
1574 }
1575
1576 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1577 switch (value) {
1578 case 1:
1579 break;
1580 case 2:
1581 spi->mode |= SPI_RX_DUAL;
1582 break;
1583 case 4:
1584 spi->mode |= SPI_RX_QUAD;
1585 break;
1586 default:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001587 dev_warn(&ctlr->dev,
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001588 "spi-rx-bus-width %d not supported\n",
1589 value);
1590 break;
1591 }
1592 }
1593
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001594 if (spi_controller_is_slave(ctlr)) {
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001595 if (strcmp(nc->name, "slave")) {
Rob Herring25c56c82017-07-18 16:43:31 -05001596 dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
1597 nc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001598 return -EINVAL;
1599 }
1600 return 0;
1601 }
1602
1603 /* Device address */
1604 rc = of_property_read_u32(nc, "reg", &value);
1605 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05001606 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
1607 nc, rc);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001608 return rc;
1609 }
1610 spi->chip_select = value;
1611
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001612 /* Device speed */
1613 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1614 if (rc) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001615 dev_err(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05001616 "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001617 return rc;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001618 }
1619 spi->max_speed_hz = value;
1620
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001621 return 0;
1622}
1623
1624static struct spi_device *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001625of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001626{
1627 struct spi_device *spi;
1628 int rc;
1629
1630 /* Alloc an spi_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001631 spi = spi_alloc_device(ctlr);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001632 if (!spi) {
Rob Herring25c56c82017-07-18 16:43:31 -05001633 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001634 rc = -ENOMEM;
1635 goto err_out;
1636 }
1637
1638 /* Select device driver */
1639 rc = of_modalias_node(nc, spi->modalias,
1640 sizeof(spi->modalias));
1641 if (rc < 0) {
Rob Herring25c56c82017-07-18 16:43:31 -05001642 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001643 goto err_out;
1644 }
1645
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001646 rc = of_spi_parse_dt(ctlr, spi, nc);
Geert Uytterhoevenc2e51ac2016-09-12 22:50:41 +02001647 if (rc)
1648 goto err_out;
1649
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001650 /* Store a pointer to the node in the device structure */
1651 of_node_get(nc);
1652 spi->dev.of_node = nc;
1653
1654 /* Register the new device */
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001655 rc = spi_add_device(spi);
1656 if (rc) {
Rob Herring25c56c82017-07-18 16:43:31 -05001657 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
Johan Hovold83241472017-01-30 17:47:05 +01001658 goto err_of_node_put;
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001659 }
1660
1661 return spi;
1662
Johan Hovold83241472017-01-30 17:47:05 +01001663err_of_node_put:
1664 of_node_put(nc);
Pantelis Antoniouaff5e3f2014-10-29 10:40:37 +02001665err_out:
1666 spi_dev_put(spi);
1667 return ERR_PTR(rc);
1668}
1669
Grant Likelyd57a4282012-04-07 14:16:53 -06001670/**
1671 * of_register_spi_devices() - Register child devices onto the SPI bus
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001672 * @ctlr: Pointer to spi_controller device
Grant Likelyd57a4282012-04-07 14:16:53 -06001673 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001674 * Registers an spi_device for each child node of controller node which
1675 * represents a valid SPI slave.
Grant Likelyd57a4282012-04-07 14:16:53 -06001676 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001677static void of_register_spi_devices(struct spi_controller *ctlr)
Grant Likelyd57a4282012-04-07 14:16:53 -06001678{
1679 struct spi_device *spi;
1680 struct device_node *nc;
Grant Likelyd57a4282012-04-07 14:16:53 -06001681
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001682 if (!ctlr->dev.of_node)
Grant Likelyd57a4282012-04-07 14:16:53 -06001683 return;
1684
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001685 for_each_available_child_of_node(ctlr->dev.of_node, nc) {
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01001686 if (of_node_test_and_set_flag(nc, OF_POPULATED))
1687 continue;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001688 spi = of_register_spi_device(ctlr, nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001689 if (IS_ERR(spi)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001690 dev_warn(&ctlr->dev,
Rob Herring25c56c82017-07-18 16:43:31 -05001691 "Failed to create SPI device for %pOF\n", nc);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02001692 of_node_clear_flag(nc, OF_POPULATED);
1693 }
Grant Likelyd57a4282012-04-07 14:16:53 -06001694 }
1695}
1696#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001697static void of_register_spi_devices(struct spi_controller *ctlr) { }
Grant Likelyd57a4282012-04-07 14:16:53 -06001698#endif
1699
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001700#ifdef CONFIG_ACPI
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001701static void acpi_spi_parse_apple_properties(struct spi_device *spi)
1702{
1703 struct acpi_device *dev = ACPI_COMPANION(&spi->dev);
1704 const union acpi_object *obj;
1705
1706 if (!x86_apple_machine)
1707 return;
1708
1709 if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
1710 && obj->buffer.length >= 4)
1711 spi->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
1712
1713 if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
1714 && obj->buffer.length == 8)
1715 spi->bits_per_word = *(u64 *)obj->buffer.pointer;
1716
1717 if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
1718 && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
1719 spi->mode |= SPI_LSB_FIRST;
1720
1721 if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
1722 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1723 spi->mode |= SPI_CPOL;
1724
1725 if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
1726 && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer)
1727 spi->mode |= SPI_CPHA;
1728}
1729
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001730static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
1731{
1732 struct spi_device *spi = data;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001733 struct spi_controller *ctlr = spi->controller;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001734
1735 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1736 struct acpi_resource_spi_serialbus *sb;
1737
1738 sb = &ares->data.spi_serial_bus;
1739 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
Mika Westerberga0a90712016-02-08 17:14:28 +02001740 /*
1741 * ACPI DeviceSelection numbering is handled by the
1742 * host controller driver in Windows and can vary
1743 * from driver to driver. In Linux we always expect
1744 * 0 .. max - 1 so we need to ask the driver to
1745 * translate between the two schemes.
1746 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001747 if (ctlr->fw_translate_cs) {
1748 int cs = ctlr->fw_translate_cs(ctlr,
Mika Westerberga0a90712016-02-08 17:14:28 +02001749 sb->device_selection);
1750 if (cs < 0)
1751 return cs;
1752 spi->chip_select = cs;
1753 } else {
1754 spi->chip_select = sb->device_selection;
1755 }
1756
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001757 spi->max_speed_hz = sb->connection_speed;
1758
1759 if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
1760 spi->mode |= SPI_CPHA;
1761 if (sb->clock_polarity == ACPI_SPI_START_HIGH)
1762 spi->mode |= SPI_CPOL;
1763 if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
1764 spi->mode |= SPI_CS_HIGH;
1765 }
1766 } else if (spi->irq < 0) {
1767 struct resource r;
1768
1769 if (acpi_dev_resource_interrupt(ares, 0, &r))
1770 spi->irq = r.start;
1771 }
1772
1773 /* Always tell the ACPI core to skip this resource */
1774 return 1;
1775}
1776
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001777static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
Octavian Purdila7f244672016-07-08 19:13:11 +03001778 struct acpi_device *adev)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001779{
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001780 struct list_head resource_list;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001781 struct spi_device *spi;
1782 int ret;
1783
Octavian Purdila7f244672016-07-08 19:13:11 +03001784 if (acpi_bus_get_status(adev) || !adev->status.present ||
1785 acpi_device_enumerated(adev))
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001786 return AE_OK;
1787
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001788 spi = spi_alloc_device(ctlr);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001789 if (!spi) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001790 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001791 dev_name(&adev->dev));
1792 return AE_NO_MEMORY;
1793 }
1794
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001795 ACPI_COMPANION_SET(&spi->dev, adev);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001796 spi->irq = -1;
1797
1798 INIT_LIST_HEAD(&resource_list);
1799 ret = acpi_dev_get_resources(adev, &resource_list,
1800 acpi_spi_add_resource, spi);
1801 acpi_dev_free_resource_list(&resource_list);
1802
Lukas Wunner8a2e4872017-08-01 14:10:41 +02001803 acpi_spi_parse_apple_properties(spi);
1804
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001805 if (ret < 0 || !spi->max_speed_hz) {
1806 spi_dev_put(spi);
1807 return AE_OK;
1808 }
1809
Dan O'Donovan0c6543f2017-02-05 16:30:14 +00001810 acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
1811 sizeof(spi->modalias));
1812
Christophe RICARD33ada672015-12-23 23:25:35 +01001813 if (spi->irq < 0)
1814 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
1815
Octavian Purdila7f244672016-07-08 19:13:11 +03001816 acpi_device_set_enumerated(adev);
1817
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001818 adev->power.flags.ignore_parent = true;
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001819 if (spi_add_device(spi)) {
Mika Westerberg33cf00e2013-10-10 13:28:48 +03001820 adev->power.flags.ignore_parent = false;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001821 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001822 dev_name(&adev->dev));
1823 spi_dev_put(spi);
1824 }
1825
1826 return AE_OK;
1827}
1828
Octavian Purdila7f244672016-07-08 19:13:11 +03001829static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
1830 void *data, void **return_value)
1831{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001832 struct spi_controller *ctlr = data;
Octavian Purdila7f244672016-07-08 19:13:11 +03001833 struct acpi_device *adev;
1834
1835 if (acpi_bus_get_device(handle, &adev))
1836 return AE_OK;
1837
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001838 return acpi_register_spi_device(ctlr, adev);
Octavian Purdila7f244672016-07-08 19:13:11 +03001839}
1840
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001841static void acpi_register_spi_devices(struct spi_controller *ctlr)
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001842{
1843 acpi_status status;
1844 acpi_handle handle;
1845
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001846 handle = ACPI_HANDLE(ctlr->dev.parent);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001847 if (!handle)
1848 return;
1849
1850 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001851 acpi_spi_add_device, NULL, ctlr, NULL);
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001852 if (ACPI_FAILURE(status))
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001853 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001854}
1855#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001856static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
Mika Westerberg64bee4d2012-11-30 12:37:53 +01001857#endif /* CONFIG_ACPI */
1858
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001859static void spi_controller_release(struct device *dev)
David Brownell8ae12a02006-01-08 13:34:19 -08001860{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001861 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08001862
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001863 ctlr = container_of(dev, struct spi_controller, dev);
1864 kfree(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08001865}
1866
1867static struct class spi_master_class = {
1868 .name = "spi_master",
1869 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001870 .dev_release = spi_controller_release,
Martin Sperleca2ebc2015-06-22 13:00:36 +00001871 .dev_groups = spi_master_groups,
David Brownell8ae12a02006-01-08 13:34:19 -08001872};
1873
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001874#ifdef CONFIG_SPI_SLAVE
1875/**
1876 * spi_slave_abort - abort the ongoing transfer request on an SPI slave
1877 * controller
1878 * @spi: device used for the current transfer
1879 */
1880int spi_slave_abort(struct spi_device *spi)
1881{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001882 struct spi_controller *ctlr = spi->controller;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001883
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001884 if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
1885 return ctlr->slave_abort(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001886
1887 return -ENOTSUPP;
1888}
1889EXPORT_SYMBOL_GPL(spi_slave_abort);
1890
1891static int match_true(struct device *dev, void *data)
1892{
1893 return 1;
1894}
1895
1896static ssize_t spi_slave_show(struct device *dev,
1897 struct device_attribute *attr, char *buf)
1898{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001899 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1900 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001901 struct device *child;
1902
1903 child = device_find_child(&ctlr->dev, NULL, match_true);
1904 return sprintf(buf, "%s\n",
1905 child ? to_spi_device(child)->modalias : NULL);
1906}
1907
1908static ssize_t spi_slave_store(struct device *dev,
1909 struct device_attribute *attr, const char *buf,
1910 size_t count)
1911{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001912 struct spi_controller *ctlr = container_of(dev, struct spi_controller,
1913 dev);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001914 struct spi_device *spi;
1915 struct device *child;
1916 char name[32];
1917 int rc;
1918
1919 rc = sscanf(buf, "%31s", name);
1920 if (rc != 1 || !name[0])
1921 return -EINVAL;
1922
1923 child = device_find_child(&ctlr->dev, NULL, match_true);
1924 if (child) {
1925 /* Remove registered slave */
1926 device_unregister(child);
1927 put_device(child);
1928 }
1929
1930 if (strcmp(name, "(null)")) {
1931 /* Register new slave */
1932 spi = spi_alloc_device(ctlr);
1933 if (!spi)
1934 return -ENOMEM;
1935
1936 strlcpy(spi->modalias, name, sizeof(spi->modalias));
1937
1938 rc = spi_add_device(spi);
1939 if (rc) {
1940 spi_dev_put(spi);
1941 return rc;
1942 }
1943 }
1944
1945 return count;
1946}
1947
1948static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store);
1949
1950static struct attribute *spi_slave_attrs[] = {
1951 &dev_attr_slave.attr,
1952 NULL,
1953};
1954
1955static const struct attribute_group spi_slave_group = {
1956 .attrs = spi_slave_attrs,
1957};
1958
1959static const struct attribute_group *spi_slave_groups[] = {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001960 &spi_controller_statistics_group,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001961 &spi_slave_group,
1962 NULL,
1963};
1964
1965static struct class spi_slave_class = {
1966 .name = "spi_slave",
1967 .owner = THIS_MODULE,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001968 .dev_release = spi_controller_release,
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001969 .dev_groups = spi_slave_groups,
1970};
1971#else
1972extern struct class spi_slave_class; /* dummy */
1973#endif
David Brownell8ae12a02006-01-08 13:34:19 -08001974
1975/**
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001976 * __spi_alloc_controller - allocate an SPI master or slave controller
David Brownell8ae12a02006-01-08 13:34:19 -08001977 * @dev: the controller, possibly using the platform_bus
David Brownell33e34dc2007-05-08 00:32:21 -07001978 * @size: how much zeroed driver-private data to allocate; the pointer to this
Tony Jones49dce682007-10-16 01:27:48 -07001979 * memory is in the driver_data field of the returned device,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001980 * accessible with spi_controller_get_devdata().
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001981 * @slave: flag indicating whether to allocate an SPI master (false) or SPI
1982 * slave (true) controller
David Brownell33e34dc2007-05-08 00:32:21 -07001983 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08001984 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001985 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08001986 * only ones directly touching chip registers. It's how they allocate
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001987 * an spi_controller structure, prior to calling spi_register_controller().
David Brownell8ae12a02006-01-08 13:34:19 -08001988 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001989 * This must be called from context that can sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08001990 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001991 * The caller is responsible for assigning the bus number and initializing the
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001992 * controller's methods before calling spi_register_controller(); and (after
1993 * errors adding the device) calling spi_controller_put() to prevent a memory
1994 * leak.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02001995 *
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02001996 * Return: the SPI controller structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08001997 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02001998struct spi_controller *__spi_alloc_controller(struct device *dev,
1999 unsigned int size, bool slave)
David Brownell8ae12a02006-01-08 13:34:19 -08002000{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002001 struct spi_controller *ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002002
David Brownell0c8684612006-01-08 13:34:25 -08002003 if (!dev)
2004 return NULL;
2005
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002006 ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL);
2007 if (!ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002008 return NULL;
2009
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002010 device_initialize(&ctlr->dev);
2011 ctlr->bus_num = -1;
2012 ctlr->num_chipselect = 1;
2013 ctlr->slave = slave;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002014 if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002015 ctlr->dev.class = &spi_slave_class;
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002016 else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002017 ctlr->dev.class = &spi_master_class;
2018 ctlr->dev.parent = dev;
2019 pm_suspend_ignore_children(&ctlr->dev, true);
2020 spi_controller_set_devdata(ctlr, &ctlr[1]);
David Brownell8ae12a02006-01-08 13:34:19 -08002021
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002022 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002023}
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002024EXPORT_SYMBOL_GPL(__spi_alloc_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002025
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002026#ifdef CONFIG_OF
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002027static int of_spi_register_master(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002028{
Grant Likelye80beb22013-02-12 17:48:37 +00002029 int nb, i, *cs;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002030 struct device_node *np = ctlr->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002031
2032 if (!np)
2033 return 0;
2034
2035 nb = of_gpio_named_count(np, "cs-gpios");
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002036 ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002037
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002038 /* Return error only for an incorrectly formed cs-gpios property */
2039 if (nb == 0 || nb == -ENOENT)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002040 return 0;
Andreas Larsson8ec5d842013-02-13 14:23:24 +01002041 else if (nb < 0)
2042 return nb;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002043
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002044 cs = devm_kzalloc(&ctlr->dev, sizeof(int) * ctlr->num_chipselect,
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002045 GFP_KERNEL);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002046 ctlr->cs_gpios = cs;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002047
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002048 if (!ctlr->cs_gpios)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002049 return -ENOMEM;
2050
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002051 for (i = 0; i < ctlr->num_chipselect; i++)
Andreas Larsson446411e2013-02-13 14:20:25 +01002052 cs[i] = -ENOENT;
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002053
2054 for (i = 0; i < nb; i++)
2055 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
2056
2057 return 0;
2058}
2059#else
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002060static int of_spi_register_master(struct spi_controller *ctlr)
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002061{
2062 return 0;
2063}
2064#endif
2065
David Brownell8ae12a02006-01-08 13:34:19 -08002066/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002067 * spi_register_controller - register SPI master or slave controller
2068 * @ctlr: initialized master, originally from spi_alloc_master() or
2069 * spi_alloc_slave()
David Brownell33e34dc2007-05-08 00:32:21 -07002070 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002071 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002072 * SPI controllers connect to their drivers using some non-SPI bus,
David Brownell8ae12a02006-01-08 13:34:19 -08002073 * such as the platform bus. The final stage of probe() in that code
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002074 * includes calling spi_register_controller() to hook up to this SPI bus glue.
David Brownell8ae12a02006-01-08 13:34:19 -08002075 *
2076 * SPI controllers use board specific (often SOC specific) bus numbers,
2077 * and board-specific addressing for SPI devices combines those numbers
2078 * with chip select numbers. Since SPI does not directly support dynamic
2079 * device identification, boards need configuration tables telling which
2080 * chip is at which address.
2081 *
2082 * This must be called from context that can sleep. It returns zero on
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002083 * success, else a negative error code (dropping the controller's refcount).
David Brownell0c8684612006-01-08 13:34:25 -08002084 * After a successful return, the caller is responsible for calling
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002085 * spi_unregister_controller().
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002086 *
2087 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08002088 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002089int spi_register_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002090{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002091 struct device *dev = ctlr->dev.parent;
Feng Tang2b9603a2010-08-02 15:52:15 +08002092 struct boardinfo *bi;
David Brownell8ae12a02006-01-08 13:34:19 -08002093 int status = -ENODEV;
Lucas Stach42bdd702017-10-16 12:27:58 +02002094 int id, first_dynamic;
David Brownell8ae12a02006-01-08 13:34:19 -08002095
David Brownell0c8684612006-01-08 13:34:25 -08002096 if (!dev)
2097 return -ENODEV;
2098
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002099 if (!spi_controller_is_slave(ctlr)) {
2100 status = of_spi_register_master(ctlr);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02002101 if (status)
2102 return status;
2103 }
Jean-Christophe PLAGNIOL-VILLARD74317982012-11-15 20:19:57 +01002104
David Brownell082c8cb2007-07-31 00:39:45 -07002105 /* even if it's just one always-selected device, there must
2106 * be at least one chipselect
2107 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002108 if (ctlr->num_chipselect == 0)
David Brownell082c8cb2007-07-31 00:39:45 -07002109 return -EINVAL;
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302110 /* allocate dynamic bus number using Linux idr */
2111 if ((ctlr->bus_num < 0) && ctlr->dev.of_node) {
2112 id = of_alias_get_id(ctlr->dev.of_node, "spi");
2113 if (id >= 0) {
2114 ctlr->bus_num = id;
2115 mutex_lock(&board_lock);
2116 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2117 ctlr->bus_num + 1, GFP_KERNEL);
2118 mutex_unlock(&board_lock);
2119 if (WARN(id < 0, "couldn't get idr"))
2120 return id == -ENOSPC ? -EBUSY : id;
2121 }
2122 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002123 if (ctlr->bus_num < 0) {
Lucas Stach42bdd702017-10-16 12:27:58 +02002124 first_dynamic = of_alias_get_highest_id("spi");
2125 if (first_dynamic < 0)
2126 first_dynamic = 0;
2127 else
2128 first_dynamic++;
2129
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302130 mutex_lock(&board_lock);
Lucas Stach42bdd702017-10-16 12:27:58 +02002131 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
2132 0, GFP_KERNEL);
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302133 mutex_unlock(&board_lock);
2134 if (WARN(id < 0, "couldn't get idr"))
2135 return id;
2136 ctlr->bus_num = id;
David Brownell8ae12a02006-01-08 13:34:19 -08002137 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002138 INIT_LIST_HEAD(&ctlr->queue);
2139 spin_lock_init(&ctlr->queue_lock);
2140 spin_lock_init(&ctlr->bus_lock_spinlock);
2141 mutex_init(&ctlr->bus_lock_mutex);
2142 mutex_init(&ctlr->io_mutex);
2143 ctlr->bus_lock_flag = 0;
2144 init_completion(&ctlr->xfer_completion);
2145 if (!ctlr->max_dma_len)
2146 ctlr->max_dma_len = INT_MAX;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002147
David Brownell8ae12a02006-01-08 13:34:19 -08002148 /* register the device, then userspace will see it.
2149 * registration fails if the bus ID is in use.
2150 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002151 dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
2152 status = device_add(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302153 if (status < 0) {
2154 /* free bus id */
2155 mutex_lock(&board_lock);
2156 idr_remove(&spi_master_idr, ctlr->bus_num);
2157 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002158 goto done;
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302159 }
2160 dev_dbg(dev, "registered %s %s\n",
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002161 spi_controller_is_slave(ctlr) ? "slave" : "master",
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302162 dev_name(&ctlr->dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002163
Linus Walleijffbbdd212012-02-22 10:05:38 +01002164 /* If we're using a queued driver, start the queue */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002165 if (ctlr->transfer)
2166 dev_info(dev, "controller is unqueued, this is deprecated\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002167 else {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002168 status = spi_controller_initialize_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002169 if (status) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002170 device_del(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302171 /* free bus id */
2172 mutex_lock(&board_lock);
2173 idr_remove(&spi_master_idr, ctlr->bus_num);
2174 mutex_unlock(&board_lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002175 goto done;
2176 }
2177 }
Martin Sperleca2ebc2015-06-22 13:00:36 +00002178 /* add statistics */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002179 spin_lock_init(&ctlr->statistics.lock);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002180
Feng Tang2b9603a2010-08-02 15:52:15 +08002181 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002182 list_add_tail(&ctlr->list, &spi_controller_list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002183 list_for_each_entry(bi, &board_list, list)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002184 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
Feng Tang2b9603a2010-08-02 15:52:15 +08002185 mutex_unlock(&board_lock);
2186
Mika Westerberg64bee4d2012-11-30 12:37:53 +01002187 /* Register devices from the device tree and ACPI */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002188 of_register_spi_devices(ctlr);
2189 acpi_register_spi_devices(ctlr);
David Brownell8ae12a02006-01-08 13:34:19 -08002190done:
2191 return status;
2192}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002193EXPORT_SYMBOL_GPL(spi_register_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002194
Mark Brown666d5b42013-08-31 18:50:52 +01002195static void devm_spi_unregister(struct device *dev, void *res)
2196{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002197 spi_unregister_controller(*(struct spi_controller **)res);
Mark Brown666d5b42013-08-31 18:50:52 +01002198}
2199
2200/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002201 * devm_spi_register_controller - register managed SPI master or slave
2202 * controller
2203 * @dev: device managing SPI controller
2204 * @ctlr: initialized controller, originally from spi_alloc_master() or
2205 * spi_alloc_slave()
Mark Brown666d5b42013-08-31 18:50:52 +01002206 * Context: can sleep
2207 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002208 * Register a SPI device as with spi_register_controller() which will
Johan Hovold68b892f2017-10-30 11:35:26 +01002209 * automatically be unregistered and freed.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002210 *
2211 * Return: zero on success, else a negative error code.
Mark Brown666d5b42013-08-31 18:50:52 +01002212 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002213int devm_spi_register_controller(struct device *dev,
2214 struct spi_controller *ctlr)
Mark Brown666d5b42013-08-31 18:50:52 +01002215{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002216 struct spi_controller **ptr;
Mark Brown666d5b42013-08-31 18:50:52 +01002217 int ret;
2218
2219 ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
2220 if (!ptr)
2221 return -ENOMEM;
2222
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002223 ret = spi_register_controller(ctlr);
Stephen Warren4b928942013-11-21 16:11:15 -07002224 if (!ret) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002225 *ptr = ctlr;
Mark Brown666d5b42013-08-31 18:50:52 +01002226 devres_add(dev, ptr);
2227 } else {
2228 devres_free(ptr);
2229 }
2230
2231 return ret;
2232}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002233EXPORT_SYMBOL_GPL(devm_spi_register_controller);
Mark Brown666d5b42013-08-31 18:50:52 +01002234
David Lamparter34860082010-08-30 23:54:17 +02002235static int __unregister(struct device *dev, void *null)
David Brownell8ae12a02006-01-08 13:34:19 -08002236{
David Lamparter34860082010-08-30 23:54:17 +02002237 spi_unregister_device(to_spi_device(dev));
David Brownell8ae12a02006-01-08 13:34:19 -08002238 return 0;
2239}
2240
2241/**
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002242 * spi_unregister_controller - unregister SPI master or slave controller
2243 * @ctlr: the controller being unregistered
David Brownell33e34dc2007-05-08 00:32:21 -07002244 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002245 *
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002246 * This call is used only by SPI controller drivers, which are the
David Brownell8ae12a02006-01-08 13:34:19 -08002247 * only ones directly touching chip registers.
2248 *
2249 * This must be called from context that can sleep.
Johan Hovold68b892f2017-10-30 11:35:26 +01002250 *
2251 * Note that this function also drops a reference to the controller.
David Brownell8ae12a02006-01-08 13:34:19 -08002252 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002253void spi_unregister_controller(struct spi_controller *ctlr)
David Brownell8ae12a02006-01-08 13:34:19 -08002254{
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302255 struct spi_controller *found;
Johan Hovold67f7b272017-10-30 11:35:25 +01002256 int id = ctlr->bus_num;
Jeff Garzik89fc9a12006-12-06 20:35:35 -08002257 int dummy;
2258
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302259 /* First make sure that this controller was ever added */
2260 mutex_lock(&board_lock);
Johan Hovold67f7b272017-10-30 11:35:25 +01002261 found = idr_find(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302262 mutex_unlock(&board_lock);
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302263 if (found != ctlr) {
2264 dev_dbg(&ctlr->dev,
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302265 "attempting to delete unregistered controller [%s]\n",
2266 dev_name(&ctlr->dev));
Suniel Mahesh9a9a0472017-08-17 18:18:22 +05302267 return;
2268 }
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002269 if (ctlr->queued) {
2270 if (spi_destroy_queue(ctlr))
2271 dev_err(&ctlr->dev, "queue remove failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002272 }
Feng Tang2b9603a2010-08-02 15:52:15 +08002273 mutex_lock(&board_lock);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002274 list_del(&ctlr->list);
Feng Tang2b9603a2010-08-02 15:52:15 +08002275 mutex_unlock(&board_lock);
2276
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002277 dummy = device_for_each_child(&ctlr->dev, NULL, __unregister);
2278 device_unregister(&ctlr->dev);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302279 /* free bus id */
2280 mutex_lock(&board_lock);
Johan Hovold67f7b272017-10-30 11:35:25 +01002281 idr_remove(&spi_master_idr, id);
Suniel Mahesh9b61e302017-08-03 10:05:57 +05302282 mutex_unlock(&board_lock);
David Brownell8ae12a02006-01-08 13:34:19 -08002283}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002284EXPORT_SYMBOL_GPL(spi_unregister_controller);
David Brownell8ae12a02006-01-08 13:34:19 -08002285
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002286int spi_controller_suspend(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002287{
2288 int ret;
2289
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002290 /* Basically no-ops for non-queued controllers */
2291 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002292 return 0;
2293
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002294 ret = spi_stop_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002295 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002296 dev_err(&ctlr->dev, "queue stop failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002297
2298 return ret;
2299}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002300EXPORT_SYMBOL_GPL(spi_controller_suspend);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002301
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002302int spi_controller_resume(struct spi_controller *ctlr)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002303{
2304 int ret;
2305
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002306 if (!ctlr->queued)
Linus Walleijffbbdd212012-02-22 10:05:38 +01002307 return 0;
2308
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002309 ret = spi_start_queue(ctlr);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002310 if (ret)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002311 dev_err(&ctlr->dev, "queue restart failed\n");
Linus Walleijffbbdd212012-02-22 10:05:38 +01002312
2313 return ret;
2314}
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002315EXPORT_SYMBOL_GPL(spi_controller_resume);
Linus Walleijffbbdd212012-02-22 10:05:38 +01002316
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002317static int __spi_controller_match(struct device *dev, const void *data)
Dave Young5ed2c832008-01-22 15:14:18 +08002318{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002319 struct spi_controller *ctlr;
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002320 const u16 *bus_num = data;
Dave Young5ed2c832008-01-22 15:14:18 +08002321
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002322 ctlr = container_of(dev, struct spi_controller, dev);
2323 return ctlr->bus_num == *bus_num;
Dave Young5ed2c832008-01-22 15:14:18 +08002324}
2325
David Brownell8ae12a02006-01-08 13:34:19 -08002326/**
2327 * spi_busnum_to_master - look up master associated with bus_num
2328 * @bus_num: the master's bus number
David Brownell33e34dc2007-05-08 00:32:21 -07002329 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08002330 *
2331 * This call may be used with devices that are registered after
2332 * arch init time. It returns a refcounted pointer to the relevant
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002333 * spi_controller (which the caller must release), or NULL if there is
David Brownell8ae12a02006-01-08 13:34:19 -08002334 * no such master registered.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002335 *
2336 * Return: the SPI master structure on success, else NULL.
David Brownell8ae12a02006-01-08 13:34:19 -08002337 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002338struct spi_controller *spi_busnum_to_master(u16 bus_num)
David Brownell8ae12a02006-01-08 13:34:19 -08002339{
Tony Jones49dce682007-10-16 01:27:48 -07002340 struct device *dev;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002341 struct spi_controller *ctlr = NULL;
David Brownell8ae12a02006-01-08 13:34:19 -08002342
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002343 dev = class_find_device(&spi_master_class, NULL, &bus_num,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002344 __spi_controller_match);
Dave Young5ed2c832008-01-22 15:14:18 +08002345 if (dev)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002346 ctlr = container_of(dev, struct spi_controller, dev);
Dave Young5ed2c832008-01-22 15:14:18 +08002347 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002348 return ctlr;
David Brownell8ae12a02006-01-08 13:34:19 -08002349}
2350EXPORT_SYMBOL_GPL(spi_busnum_to_master);
2351
Martin Sperld780c372015-12-14 15:20:18 +00002352/*-------------------------------------------------------------------------*/
2353
2354/* Core methods for SPI resource management */
2355
2356/**
2357 * spi_res_alloc - allocate a spi resource that is life-cycle managed
2358 * during the processing of a spi_message while using
2359 * spi_transfer_one
2360 * @spi: the spi device for which we allocate memory
2361 * @release: the release code to execute for this resource
2362 * @size: size to alloc and return
2363 * @gfp: GFP allocation flags
2364 *
2365 * Return: the pointer to the allocated data
2366 *
2367 * This may get enhanced in the future to allocate from a memory pool
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002368 * of the @spi_device or @spi_controller to avoid repeated allocations.
Martin Sperld780c372015-12-14 15:20:18 +00002369 */
2370void *spi_res_alloc(struct spi_device *spi,
2371 spi_res_release_t release,
2372 size_t size, gfp_t gfp)
2373{
2374 struct spi_res *sres;
2375
2376 sres = kzalloc(sizeof(*sres) + size, gfp);
2377 if (!sres)
2378 return NULL;
2379
2380 INIT_LIST_HEAD(&sres->entry);
2381 sres->release = release;
2382
2383 return sres->data;
2384}
2385EXPORT_SYMBOL_GPL(spi_res_alloc);
2386
2387/**
2388 * spi_res_free - free an spi resource
2389 * @res: pointer to the custom data of a resource
2390 *
2391 */
2392void spi_res_free(void *res)
2393{
2394 struct spi_res *sres = container_of(res, struct spi_res, data);
2395
2396 if (!res)
2397 return;
2398
2399 WARN_ON(!list_empty(&sres->entry));
2400 kfree(sres);
2401}
2402EXPORT_SYMBOL_GPL(spi_res_free);
2403
2404/**
2405 * spi_res_add - add a spi_res to the spi_message
2406 * @message: the spi message
2407 * @res: the spi_resource
2408 */
2409void spi_res_add(struct spi_message *message, void *res)
2410{
2411 struct spi_res *sres = container_of(res, struct spi_res, data);
2412
2413 WARN_ON(!list_empty(&sres->entry));
2414 list_add_tail(&sres->entry, &message->resources);
2415}
2416EXPORT_SYMBOL_GPL(spi_res_add);
2417
2418/**
2419 * spi_res_release - release all spi resources for this message
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002420 * @ctlr: the @spi_controller
Martin Sperld780c372015-12-14 15:20:18 +00002421 * @message: the @spi_message
2422 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002423void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
Martin Sperld780c372015-12-14 15:20:18 +00002424{
2425 struct spi_res *res;
2426
2427 while (!list_empty(&message->resources)) {
2428 res = list_last_entry(&message->resources,
2429 struct spi_res, entry);
2430
2431 if (res->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002432 res->release(ctlr, message, res->data);
Martin Sperld780c372015-12-14 15:20:18 +00002433
2434 list_del(&res->entry);
2435
2436 kfree(res);
2437 }
2438}
2439EXPORT_SYMBOL_GPL(spi_res_release);
David Brownell8ae12a02006-01-08 13:34:19 -08002440
2441/*-------------------------------------------------------------------------*/
2442
Martin Sperl523baf5a2015-12-14 15:20:19 +00002443/* Core methods for spi_message alterations */
2444
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002445static void __spi_replace_transfers_release(struct spi_controller *ctlr,
Martin Sperl523baf5a2015-12-14 15:20:19 +00002446 struct spi_message *msg,
2447 void *res)
2448{
2449 struct spi_replaced_transfers *rxfer = res;
2450 size_t i;
2451
2452 /* call extra callback if requested */
2453 if (rxfer->release)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002454 rxfer->release(ctlr, msg, res);
Martin Sperl523baf5a2015-12-14 15:20:19 +00002455
2456 /* insert replaced transfers back into the message */
2457 list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
2458
2459 /* remove the formerly inserted entries */
2460 for (i = 0; i < rxfer->inserted; i++)
2461 list_del(&rxfer->inserted_transfers[i].transfer_list);
2462}
2463
2464/**
2465 * spi_replace_transfers - replace transfers with several transfers
2466 * and register change with spi_message.resources
2467 * @msg: the spi_message we work upon
2468 * @xfer_first: the first spi_transfer we want to replace
2469 * @remove: number of transfers to remove
2470 * @insert: the number of transfers we want to insert instead
2471 * @release: extra release code necessary in some circumstances
2472 * @extradatasize: extra data to allocate (with alignment guarantees
2473 * of struct @spi_transfer)
Martin Sperl05885392016-02-18 15:53:11 +00002474 * @gfp: gfp flags
Martin Sperl523baf5a2015-12-14 15:20:19 +00002475 *
2476 * Returns: pointer to @spi_replaced_transfers,
2477 * PTR_ERR(...) in case of errors.
2478 */
2479struct spi_replaced_transfers *spi_replace_transfers(
2480 struct spi_message *msg,
2481 struct spi_transfer *xfer_first,
2482 size_t remove,
2483 size_t insert,
2484 spi_replaced_release_t release,
2485 size_t extradatasize,
2486 gfp_t gfp)
2487{
2488 struct spi_replaced_transfers *rxfer;
2489 struct spi_transfer *xfer;
2490 size_t i;
2491
2492 /* allocate the structure using spi_res */
2493 rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
2494 insert * sizeof(struct spi_transfer)
2495 + sizeof(struct spi_replaced_transfers)
2496 + extradatasize,
2497 gfp);
2498 if (!rxfer)
2499 return ERR_PTR(-ENOMEM);
2500
2501 /* the release code to invoke before running the generic release */
2502 rxfer->release = release;
2503
2504 /* assign extradata */
2505 if (extradatasize)
2506 rxfer->extradata =
2507 &rxfer->inserted_transfers[insert];
2508
2509 /* init the replaced_transfers list */
2510 INIT_LIST_HEAD(&rxfer->replaced_transfers);
2511
2512 /* assign the list_entry after which we should reinsert
2513 * the @replaced_transfers - it may be spi_message.messages!
2514 */
2515 rxfer->replaced_after = xfer_first->transfer_list.prev;
2516
2517 /* remove the requested number of transfers */
2518 for (i = 0; i < remove; i++) {
2519 /* if the entry after replaced_after it is msg->transfers
2520 * then we have been requested to remove more transfers
2521 * than are in the list
2522 */
2523 if (rxfer->replaced_after->next == &msg->transfers) {
2524 dev_err(&msg->spi->dev,
2525 "requested to remove more spi_transfers than are available\n");
2526 /* insert replaced transfers back into the message */
2527 list_splice(&rxfer->replaced_transfers,
2528 rxfer->replaced_after);
2529
2530 /* free the spi_replace_transfer structure */
2531 spi_res_free(rxfer);
2532
2533 /* and return with an error */
2534 return ERR_PTR(-EINVAL);
2535 }
2536
2537 /* remove the entry after replaced_after from list of
2538 * transfers and add it to list of replaced_transfers
2539 */
2540 list_move_tail(rxfer->replaced_after->next,
2541 &rxfer->replaced_transfers);
2542 }
2543
2544 /* create copy of the given xfer with identical settings
2545 * based on the first transfer to get removed
2546 */
2547 for (i = 0; i < insert; i++) {
2548 /* we need to run in reverse order */
2549 xfer = &rxfer->inserted_transfers[insert - 1 - i];
2550
2551 /* copy all spi_transfer data */
2552 memcpy(xfer, xfer_first, sizeof(*xfer));
2553
2554 /* add to list */
2555 list_add(&xfer->transfer_list, rxfer->replaced_after);
2556
2557 /* clear cs_change and delay_usecs for all but the last */
2558 if (i) {
2559 xfer->cs_change = false;
2560 xfer->delay_usecs = 0;
2561 }
2562 }
2563
2564 /* set up inserted */
2565 rxfer->inserted = insert;
2566
2567 /* and register it with spi_res/spi_message */
2568 spi_res_add(msg, rxfer);
2569
2570 return rxfer;
2571}
2572EXPORT_SYMBOL_GPL(spi_replace_transfers);
2573
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002574static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
Fabio Estevam08933412016-02-14 13:33:50 -02002575 struct spi_message *msg,
2576 struct spi_transfer **xferp,
2577 size_t maxsize,
2578 gfp_t gfp)
Martin Sperld9f12122015-12-14 15:20:20 +00002579{
2580 struct spi_transfer *xfer = *xferp, *xfers;
2581 struct spi_replaced_transfers *srt;
2582 size_t offset;
2583 size_t count, i;
2584
2585 /* warn once about this fact that we are splitting a transfer */
2586 dev_warn_once(&msg->spi->dev,
Fabio Estevam7d62f512016-02-17 15:42:27 -02002587 "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n",
Martin Sperld9f12122015-12-14 15:20:20 +00002588 xfer->len, maxsize);
2589
2590 /* calculate how many we have to replace */
2591 count = DIV_ROUND_UP(xfer->len, maxsize);
2592
2593 /* create replacement */
2594 srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
Dan Carpenter657d32e2016-02-12 09:38:33 +03002595 if (IS_ERR(srt))
2596 return PTR_ERR(srt);
Martin Sperld9f12122015-12-14 15:20:20 +00002597 xfers = srt->inserted_transfers;
2598
2599 /* now handle each of those newly inserted spi_transfers
2600 * note that the replacements spi_transfers all are preset
2601 * to the same values as *xferp, so tx_buf, rx_buf and len
2602 * are all identical (as well as most others)
2603 * so we just have to fix up len and the pointers.
2604 *
2605 * this also includes support for the depreciated
2606 * spi_message.is_dma_mapped interface
2607 */
2608
2609 /* the first transfer just needs the length modified, so we
2610 * run it outside the loop
2611 */
Fabio Estevamc8dab772016-02-17 15:42:28 -02002612 xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
Martin Sperld9f12122015-12-14 15:20:20 +00002613
2614 /* all the others need rx_buf/tx_buf also set */
2615 for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
2616 /* update rx_buf, tx_buf and dma */
2617 if (xfers[i].rx_buf)
2618 xfers[i].rx_buf += offset;
2619 if (xfers[i].rx_dma)
2620 xfers[i].rx_dma += offset;
2621 if (xfers[i].tx_buf)
2622 xfers[i].tx_buf += offset;
2623 if (xfers[i].tx_dma)
2624 xfers[i].tx_dma += offset;
2625
2626 /* update length */
2627 xfers[i].len = min(maxsize, xfers[i].len - offset);
2628 }
2629
2630 /* we set up xferp to the last entry we have inserted,
2631 * so that we skip those already split transfers
2632 */
2633 *xferp = &xfers[count - 1];
2634
2635 /* increment statistics counters */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002636 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperld9f12122015-12-14 15:20:20 +00002637 transfers_split_maxsize);
2638 SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
2639 transfers_split_maxsize);
2640
2641 return 0;
2642}
2643
2644/**
2645 * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
2646 * when an individual transfer exceeds a
2647 * certain size
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002648 * @ctlr: the @spi_controller for this transfer
Masanari Iida3700ce92016-02-22 20:33:44 +09002649 * @msg: the @spi_message to transform
2650 * @maxsize: the maximum when to apply this
Javier Martinez Canillas10f11a22016-03-10 15:01:14 -03002651 * @gfp: GFP allocation flags
Martin Sperld9f12122015-12-14 15:20:20 +00002652 *
2653 * Return: status of transformation
2654 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002655int spi_split_transfers_maxsize(struct spi_controller *ctlr,
Martin Sperld9f12122015-12-14 15:20:20 +00002656 struct spi_message *msg,
2657 size_t maxsize,
2658 gfp_t gfp)
2659{
2660 struct spi_transfer *xfer;
2661 int ret;
2662
2663 /* iterate over the transfer_list,
2664 * but note that xfer is advanced to the last transfer inserted
2665 * to avoid checking sizes again unnecessarily (also xfer does
2666 * potentiall belong to a different list by the time the
2667 * replacement has happened
2668 */
2669 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
2670 if (xfer->len > maxsize) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002671 ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
2672 maxsize, gfp);
Martin Sperld9f12122015-12-14 15:20:20 +00002673 if (ret)
2674 return ret;
2675 }
2676 }
2677
2678 return 0;
2679}
2680EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
David Brownell8ae12a02006-01-08 13:34:19 -08002681
2682/*-------------------------------------------------------------------------*/
2683
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002684/* Core methods for SPI controller protocol drivers. Some of the
David Brownell7d077192009-06-17 16:26:03 -07002685 * other core methods are currently defined as inline functions.
2686 */
2687
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002688static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
2689 u8 bits_per_word)
Stefan Brüns63ab6452015-08-23 16:06:30 +02002690{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002691 if (ctlr->bits_per_word_mask) {
Stefan Brüns63ab6452015-08-23 16:06:30 +02002692 /* Only 32 bits fit in the mask */
2693 if (bits_per_word > 32)
2694 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002695 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
Stefan Brüns63ab6452015-08-23 16:06:30 +02002696 return -EINVAL;
2697 }
2698
2699 return 0;
2700}
2701
David Brownell7d077192009-06-17 16:26:03 -07002702/**
2703 * spi_setup - setup SPI mode and clock rate
2704 * @spi: the device whose settings are being modified
2705 * Context: can sleep, and no requests are queued to the device
2706 *
2707 * SPI protocol drivers may need to update the transfer mode if the
2708 * device doesn't work with its default. They may likewise need
2709 * to update clock rates or word sizes from initial values. This function
2710 * changes those settings, and must be called from a context that can sleep.
2711 * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
2712 * effect the next time the device is selected and data is transferred to
2713 * or from it. When this function returns, the spi device is deselected.
2714 *
2715 * Note that this call will fail if the protocol driver specifies an option
2716 * that the underlying controller or its driver does not support. For
2717 * example, not all hardware supports wire transfers using nine bit words,
2718 * LSB-first wire encoding, or active-high chipselects.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002719 *
2720 * Return: zero on success, else a negative error code.
David Brownell7d077192009-06-17 16:26:03 -07002721 */
2722int spi_setup(struct spi_device *spi)
2723{
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002724 unsigned bad_bits, ugly_bits;
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002725 int status;
David Brownell7d077192009-06-17 16:26:03 -07002726
wangyuhangf477b7f2013-08-11 18:15:17 +08002727 /* check mode to prevent that DUAL and QUAD set at the same time
2728 */
2729 if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
2730 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
2731 dev_err(&spi->dev,
2732 "setup: can not select dual and quad at the same time\n");
2733 return -EINVAL;
2734 }
2735 /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
2736 */
2737 if ((spi->mode & SPI_3WIRE) && (spi->mode &
2738 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)))
2739 return -EINVAL;
David Brownelle7db06b2009-06-17 16:26:04 -07002740 /* help drivers fail *cleanly* when they need options
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002741 * that aren't supported with their current controller
David Brownelle7db06b2009-06-17 16:26:04 -07002742 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002743 bad_bits = spi->mode & ~spi->controller->mode_bits;
Geert Uytterhoeven83596fb2014-04-14 19:39:53 +02002744 ugly_bits = bad_bits &
2745 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD);
2746 if (ugly_bits) {
2747 dev_warn(&spi->dev,
2748 "setup: ignoring unsupported mode bits %x\n",
2749 ugly_bits);
2750 spi->mode &= ~ugly_bits;
2751 bad_bits &= ~ugly_bits;
2752 }
David Brownelle7db06b2009-06-17 16:26:04 -07002753 if (bad_bits) {
Linus Walleijeb288a12010-10-21 21:06:44 +02002754 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
David Brownelle7db06b2009-06-17 16:26:04 -07002755 bad_bits);
2756 return -EINVAL;
2757 }
2758
David Brownell7d077192009-06-17 16:26:03 -07002759 if (!spi->bits_per_word)
2760 spi->bits_per_word = 8;
2761
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002762 status = __spi_validate_bits_per_word(spi->controller,
2763 spi->bits_per_word);
Andy Shevchenko5ab8d262015-10-14 22:43:07 +03002764 if (status)
2765 return status;
Stefan Brüns63ab6452015-08-23 16:06:30 +02002766
Axel Lin052eb2d2014-02-10 00:08:05 +08002767 if (!spi->max_speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002768 spi->max_speed_hz = spi->controller->max_speed_hz;
Axel Lin052eb2d2014-02-10 00:08:05 +08002769
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002770 if (spi->controller->setup)
2771 status = spi->controller->setup(spi);
David Brownell7d077192009-06-17 16:26:03 -07002772
Franklin S Cooper Jrabeedb02015-10-16 10:29:03 -05002773 spi_set_cs(spi, false);
2774
Jingoo Han5fe5f052013-10-14 10:31:51 +09002775 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 -07002776 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
2777 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
2778 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
2779 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
2780 (spi->mode & SPI_LOOP) ? "loopback, " : "",
2781 spi->bits_per_word, spi->max_speed_hz,
2782 status);
2783
2784 return status;
2785}
2786EXPORT_SYMBOL_GPL(spi_setup);
2787
Mark Brown90808732013-11-13 23:44:15 +00002788static int __spi_validate(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002789{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002790 struct spi_controller *ctlr = spi->controller;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302791 struct spi_transfer *xfer;
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002792 int w_size;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002793
Mark Brown24a00132013-07-10 15:05:40 +01002794 if (list_empty(&message->transfers))
2795 return -EINVAL;
Mark Brown24a00132013-07-10 15:05:40 +01002796
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002797 /* Half-duplex links include original MicroWire, and ones with
2798 * only one data pin like SPI_3WIRE (switches direction) or where
2799 * either MOSI or MISO is missing. They can also be caused by
2800 * software limitations.
2801 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002802 if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
2803 (spi->mode & SPI_3WIRE)) {
2804 unsigned flags = ctlr->flags;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002805
2806 list_for_each_entry(xfer, &message->transfers, transfer_list) {
2807 if (xfer->rx_buf && xfer->tx_buf)
2808 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002809 if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002810 return -EINVAL;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002811 if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002812 return -EINVAL;
2813 }
2814 }
2815
Laxman Dewangane6811d12012-11-09 14:36:45 +05302816 /**
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302817 * Set transfer bits_per_word and max speed as spi device default if
2818 * it is not set for this transfer.
wangyuhangf477b7f2013-08-11 18:15:17 +08002819 * Set transfer tx_nbits and rx_nbits as single transfer default
2820 * (SPI_NBITS_SINGLE) if it is not set for this transfer.
Laxman Dewangane6811d12012-11-09 14:36:45 +05302821 */
Martin Sperl77e80582015-11-27 12:31:09 +00002822 message->frame_length = 0;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302823 list_for_each_entry(xfer, &message->transfers, transfer_list) {
Sourav Poddar078726c2013-07-18 15:31:25 +05302824 message->frame_length += xfer->len;
Laxman Dewangane6811d12012-11-09 14:36:45 +05302825 if (!xfer->bits_per_word)
2826 xfer->bits_per_word = spi->bits_per_word;
Axel Lina6f87fa2014-03-17 10:08:12 +08002827
2828 if (!xfer->speed_hz)
Laxman Dewangan059b8ff2013-01-05 00:17:14 +05302829 xfer->speed_hz = spi->max_speed_hz;
Mark Brown7dc9fbc2015-08-20 11:52:18 -07002830 if (!xfer->speed_hz)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002831 xfer->speed_hz = ctlr->max_speed_hz;
Axel Lina6f87fa2014-03-17 10:08:12 +08002832
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002833 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
2834 xfer->speed_hz = ctlr->max_speed_hz;
Gabor Juhos56ede942013-08-14 10:25:28 +02002835
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002836 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
Stefan Brüns63ab6452015-08-23 16:06:30 +02002837 return -EINVAL;
Mark Browna2fd4f92013-07-10 14:57:26 +01002838
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002839 /*
2840 * SPI transfer length should be multiple of SPI word size
2841 * where SPI word size should be power-of-two multiple
2842 */
2843 if (xfer->bits_per_word <= 8)
2844 w_size = 1;
2845 else if (xfer->bits_per_word <= 16)
2846 w_size = 2;
2847 else
2848 w_size = 4;
2849
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002850 /* No partial transfers accepted */
Atsushi Nemoto6ea31292014-02-28 23:03:16 +09002851 if (xfer->len % w_size)
Ivan T. Ivanov4d94bd22014-02-20 12:02:08 +02002852 return -EINVAL;
2853
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002854 if (xfer->speed_hz && ctlr->min_speed_hz &&
2855 xfer->speed_hz < ctlr->min_speed_hz)
Mark Browna2fd4f92013-07-10 14:57:26 +01002856 return -EINVAL;
wangyuhangf477b7f2013-08-11 18:15:17 +08002857
2858 if (xfer->tx_buf && !xfer->tx_nbits)
2859 xfer->tx_nbits = SPI_NBITS_SINGLE;
2860 if (xfer->rx_buf && !xfer->rx_nbits)
2861 xfer->rx_nbits = SPI_NBITS_SINGLE;
2862 /* check transfer tx/rx_nbits:
Geert Uytterhoeven1afd9982014-01-12 14:00:29 +01002863 * 1. check the value matches one of single, dual and quad
2864 * 2. check tx/rx_nbits match the mode in spi_device
wangyuhangf477b7f2013-08-11 18:15:17 +08002865 */
Sourav Poddardb90a442013-08-22 21:20:48 +05302866 if (xfer->tx_buf) {
2867 if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
2868 xfer->tx_nbits != SPI_NBITS_DUAL &&
2869 xfer->tx_nbits != SPI_NBITS_QUAD)
2870 return -EINVAL;
2871 if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
2872 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
2873 return -EINVAL;
2874 if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
2875 !(spi->mode & SPI_TX_QUAD))
2876 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302877 }
wangyuhangf477b7f2013-08-11 18:15:17 +08002878 /* check transfer rx_nbits */
Sourav Poddardb90a442013-08-22 21:20:48 +05302879 if (xfer->rx_buf) {
2880 if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
2881 xfer->rx_nbits != SPI_NBITS_DUAL &&
2882 xfer->rx_nbits != SPI_NBITS_QUAD)
2883 return -EINVAL;
2884 if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
2885 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
2886 return -EINVAL;
2887 if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
2888 !(spi->mode & SPI_RX_QUAD))
2889 return -EINVAL;
Sourav Poddardb90a442013-08-22 21:20:48 +05302890 }
Laxman Dewangane6811d12012-11-09 14:36:45 +05302891 }
2892
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002893 message->status = -EINPROGRESS;
Mark Brown90808732013-11-13 23:44:15 +00002894
2895 return 0;
2896}
2897
2898static int __spi_async(struct spi_device *spi, struct spi_message *message)
2899{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002900 struct spi_controller *ctlr = spi->controller;
Mark Brown90808732013-11-13 23:44:15 +00002901
2902 message->spi = spi;
2903
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002904 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
Martin Sperleca2ebc2015-06-22 13:00:36 +00002905 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
2906
Mark Brown90808732013-11-13 23:44:15 +00002907 trace_spi_message_submit(message);
2908
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002909 return ctlr->transfer(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002910}
2911
David Brownell568d0692009-09-22 16:46:18 -07002912/**
2913 * spi_async - asynchronous SPI transfer
2914 * @spi: device with which data will be exchanged
2915 * @message: describes the data transfers, including completion callback
2916 * Context: any (irqs may be blocked, etc)
2917 *
2918 * This call may be used in_irq and other contexts which can't sleep,
2919 * as well as from task contexts which can sleep.
2920 *
2921 * The completion callback is invoked in a context which can't sleep.
2922 * Before that invocation, the value of message->status is undefined.
2923 * When the callback is issued, message->status holds either zero (to
2924 * indicate complete success) or a negative error code. After that
2925 * callback returns, the driver which issued the transfer request may
2926 * deallocate the associated memory; it's no longer in use by any SPI
2927 * core or controller driver code.
2928 *
2929 * Note that although all messages to a spi_device are handled in
2930 * FIFO order, messages may go to different devices in other orders.
2931 * Some device might be higher priority, or have various "hard" access
2932 * time requirements, for example.
2933 *
2934 * On detection of any fault during the transfer, processing of
2935 * the entire message is aborted, and the device is deselected.
2936 * Until returning from the associated message completion callback,
2937 * no other spi_message queued to that device will be processed.
2938 * (This rule applies equally to all the synchronous transfer calls,
2939 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002940 *
2941 * Return: zero on success, else a negative error code.
David Brownell568d0692009-09-22 16:46:18 -07002942 */
2943int spi_async(struct spi_device *spi, struct spi_message *message)
2944{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002945 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002946 int ret;
2947 unsigned long flags;
David Brownell568d0692009-09-22 16:46:18 -07002948
Mark Brown90808732013-11-13 23:44:15 +00002949 ret = __spi_validate(spi, message);
2950 if (ret != 0)
2951 return ret;
2952
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002953 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
David Brownell568d0692009-09-22 16:46:18 -07002954
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002955 if (ctlr->bus_lock_flag)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002956 ret = -EBUSY;
2957 else
2958 ret = __spi_async(spi, message);
David Brownell568d0692009-09-22 16:46:18 -07002959
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002960 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002961
2962 return ret;
David Brownell568d0692009-09-22 16:46:18 -07002963}
2964EXPORT_SYMBOL_GPL(spi_async);
2965
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002966/**
2967 * spi_async_locked - version of spi_async with exclusive bus usage
2968 * @spi: device with which data will be exchanged
2969 * @message: describes the data transfers, including completion callback
2970 * Context: any (irqs may be blocked, etc)
2971 *
2972 * This call may be used in_irq and other contexts which can't sleep,
2973 * as well as from task contexts which can sleep.
2974 *
2975 * The completion callback is invoked in a context which can't sleep.
2976 * Before that invocation, the value of message->status is undefined.
2977 * When the callback is issued, message->status holds either zero (to
2978 * indicate complete success) or a negative error code. After that
2979 * callback returns, the driver which issued the transfer request may
2980 * deallocate the associated memory; it's no longer in use by any SPI
2981 * core or controller driver code.
2982 *
2983 * Note that although all messages to a spi_device are handled in
2984 * FIFO order, messages may go to different devices in other orders.
2985 * Some device might be higher priority, or have various "hard" access
2986 * time requirements, for example.
2987 *
2988 * On detection of any fault during the transfer, processing of
2989 * the entire message is aborted, and the device is deselected.
2990 * Until returning from the associated message completion callback,
2991 * no other spi_message queued to that device will be processed.
2992 * (This rule applies equally to all the synchronous transfer calls,
2993 * which are wrappers around this core asynchronous primitive.)
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02002994 *
2995 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07002996 */
2997int spi_async_locked(struct spi_device *spi, struct spi_message *message)
2998{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02002999 struct spi_controller *ctlr = spi->controller;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003000 int ret;
3001 unsigned long flags;
3002
Mark Brown90808732013-11-13 23:44:15 +00003003 ret = __spi_validate(spi, message);
3004 if (ret != 0)
3005 return ret;
3006
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003007 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003008
3009 ret = __spi_async(spi, message);
3010
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003011 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003012
3013 return ret;
3014
3015}
3016EXPORT_SYMBOL_GPL(spi_async_locked);
3017
David Brownell7d077192009-06-17 16:26:03 -07003018
Vignesh R556351f2015-12-11 09:39:56 +05303019int spi_flash_read(struct spi_device *spi,
3020 struct spi_flash_read_message *msg)
3021
3022{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003023 struct spi_controller *master = spi->controller;
Vignesh Rf4502dd2016-06-08 12:18:31 +05303024 struct device *rx_dev = NULL;
Vignesh R556351f2015-12-11 09:39:56 +05303025 int ret;
3026
3027 if ((msg->opcode_nbits == SPI_NBITS_DUAL ||
3028 msg->addr_nbits == SPI_NBITS_DUAL) &&
3029 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
3030 return -EINVAL;
3031 if ((msg->opcode_nbits == SPI_NBITS_QUAD ||
3032 msg->addr_nbits == SPI_NBITS_QUAD) &&
3033 !(spi->mode & SPI_TX_QUAD))
3034 return -EINVAL;
3035 if (msg->data_nbits == SPI_NBITS_DUAL &&
3036 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
3037 return -EINVAL;
3038 if (msg->data_nbits == SPI_NBITS_QUAD &&
3039 !(spi->mode & SPI_RX_QUAD))
3040 return -EINVAL;
3041
3042 if (master->auto_runtime_pm) {
3043 ret = pm_runtime_get_sync(master->dev.parent);
3044 if (ret < 0) {
3045 dev_err(&master->dev, "Failed to power device: %d\n",
3046 ret);
3047 return ret;
3048 }
3049 }
Vignesh Rf4502dd2016-06-08 12:18:31 +05303050
Vignesh R556351f2015-12-11 09:39:56 +05303051 mutex_lock(&master->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003052 mutex_lock(&master->io_mutex);
Vignesh R2bca3442017-04-11 17:22:24 +05303053 if (master->dma_rx && master->spi_flash_can_dma(spi, msg)) {
Vignesh Rf4502dd2016-06-08 12:18:31 +05303054 rx_dev = master->dma_rx->device->dev;
3055 ret = spi_map_buf(master, rx_dev, &msg->rx_sg,
3056 msg->buf, msg->len,
3057 DMA_FROM_DEVICE);
3058 if (!ret)
3059 msg->cur_msg_mapped = true;
3060 }
Vignesh R556351f2015-12-11 09:39:56 +05303061 ret = master->spi_flash_read(spi, msg);
Vignesh Rf4502dd2016-06-08 12:18:31 +05303062 if (msg->cur_msg_mapped)
3063 spi_unmap_buf(master, rx_dev, &msg->rx_sg,
3064 DMA_FROM_DEVICE);
Mark Brownef4d96e2016-07-21 23:53:31 +01003065 mutex_unlock(&master->io_mutex);
Vignesh R556351f2015-12-11 09:39:56 +05303066 mutex_unlock(&master->bus_lock_mutex);
Vignesh Rf4502dd2016-06-08 12:18:31 +05303067
Vignesh R556351f2015-12-11 09:39:56 +05303068 if (master->auto_runtime_pm)
3069 pm_runtime_put(master->dev.parent);
3070
3071 return ret;
3072}
3073EXPORT_SYMBOL_GPL(spi_flash_read);
3074
David Brownell7d077192009-06-17 16:26:03 -07003075/*-------------------------------------------------------------------------*/
3076
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003077/* Utility methods for SPI protocol drivers, layered on
David Brownell7d077192009-06-17 16:26:03 -07003078 * top of the core. Some other utility methods are defined as
3079 * inline functions.
3080 */
3081
Andrew Morton5d870c82006-01-11 11:23:49 -08003082static void spi_complete(void *arg)
3083{
3084 complete(arg);
3085}
3086
Mark Brownef4d96e2016-07-21 23:53:31 +01003087static int __spi_sync(struct spi_device *spi, struct spi_message *message)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003088{
3089 DECLARE_COMPLETION_ONSTACK(done);
3090 int status;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003091 struct spi_controller *ctlr = spi->controller;
Mark Brown0461a412014-12-09 21:38:05 +00003092 unsigned long flags;
3093
3094 status = __spi_validate(spi, message);
3095 if (status != 0)
3096 return status;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003097
3098 message->complete = spi_complete;
3099 message->context = &done;
Mark Brown0461a412014-12-09 21:38:05 +00003100 message->spi = spi;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003101
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003102 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003103 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
3104
Mark Brown0461a412014-12-09 21:38:05 +00003105 /* If we're not using the legacy transfer method then we will
3106 * try to transfer in the calling context so special case.
3107 * This code would be less tricky if we could remove the
3108 * support for driver implemented message queues.
3109 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003110 if (ctlr->transfer == spi_queued_transfer) {
3111 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003112
3113 trace_spi_message_submit(message);
3114
3115 status = __spi_queued_transfer(spi, message, false);
3116
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003117 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Mark Brown0461a412014-12-09 21:38:05 +00003118 } else {
3119 status = spi_async_locked(spi, message);
3120 }
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003121
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003122 if (status == 0) {
Mark Brown0461a412014-12-09 21:38:05 +00003123 /* Push out the messages in the calling context if we
3124 * can.
3125 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003126 if (ctlr->transfer == spi_queued_transfer) {
3127 SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
Martin Sperleca2ebc2015-06-22 13:00:36 +00003128 spi_sync_immediate);
3129 SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
3130 spi_sync_immediate);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003131 __spi_pump_messages(ctlr, false);
Martin Sperleca2ebc2015-06-22 13:00:36 +00003132 }
Mark Brown0461a412014-12-09 21:38:05 +00003133
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003134 wait_for_completion(&done);
3135 status = message->status;
3136 }
3137 message->context = NULL;
3138 return status;
3139}
3140
David Brownell8ae12a02006-01-08 13:34:19 -08003141/**
3142 * spi_sync - blocking/synchronous SPI data transfers
3143 * @spi: device with which data will be exchanged
3144 * @message: describes the data transfers
David Brownell33e34dc2007-05-08 00:32:21 -07003145 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003146 *
3147 * This call may only be used from a context that may sleep. The sleep
3148 * is non-interruptible, and has no timeout. Low-overhead controller
3149 * drivers may DMA directly into and out of the message buffers.
3150 *
3151 * Note that the SPI device's chip select is active during the message,
3152 * and then is normally disabled between messages. Drivers for some
3153 * frequently-used devices may want to minimize costs of selecting a chip,
3154 * by leaving it selected in anticipation that the next message will go
3155 * to the same chip. (That may increase power usage.)
3156 *
David Brownell0c8684612006-01-08 13:34:25 -08003157 * Also, the caller is guaranteeing that the memory associated with the
3158 * message will not be freed before this call returns.
3159 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003160 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003161 */
3162int spi_sync(struct spi_device *spi, struct spi_message *message)
3163{
Mark Brownef4d96e2016-07-21 23:53:31 +01003164 int ret;
3165
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003166 mutex_lock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003167 ret = __spi_sync(spi, message);
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003168 mutex_unlock(&spi->controller->bus_lock_mutex);
Mark Brownef4d96e2016-07-21 23:53:31 +01003169
3170 return ret;
David Brownell8ae12a02006-01-08 13:34:19 -08003171}
3172EXPORT_SYMBOL_GPL(spi_sync);
3173
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003174/**
3175 * spi_sync_locked - version of spi_sync with exclusive bus usage
3176 * @spi: device with which data will be exchanged
3177 * @message: describes the data transfers
3178 * Context: can sleep
3179 *
3180 * This call may only be used from a context that may sleep. The sleep
3181 * is non-interruptible, and has no timeout. Low-overhead controller
3182 * drivers may DMA directly into and out of the message buffers.
3183 *
3184 * This call should be used by drivers that require exclusive access to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003185 * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003186 * be released by a spi_bus_unlock call when the exclusive access is over.
3187 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003188 * Return: zero on success, else a negative error code.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003189 */
3190int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
3191{
Mark Brownef4d96e2016-07-21 23:53:31 +01003192 return __spi_sync(spi, message);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003193}
3194EXPORT_SYMBOL_GPL(spi_sync_locked);
3195
3196/**
3197 * spi_bus_lock - obtain a lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003198 * @ctlr: SPI bus master that should be locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003199 * Context: can sleep
3200 *
3201 * This call may only be used from a context that may sleep. The sleep
3202 * is non-interruptible, and has no timeout.
3203 *
3204 * This call should be used by drivers that require exclusive access to the
3205 * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
3206 * exclusive access is over. Data transfer must be done by spi_sync_locked
3207 * and spi_async_locked calls when the SPI bus lock is held.
3208 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003209 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003210 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003211int spi_bus_lock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003212{
3213 unsigned long flags;
3214
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003215 mutex_lock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003216
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003217 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
3218 ctlr->bus_lock_flag = 1;
3219 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003220
3221 /* mutex remains locked until spi_bus_unlock is called */
3222
3223 return 0;
3224}
3225EXPORT_SYMBOL_GPL(spi_bus_lock);
3226
3227/**
3228 * spi_bus_unlock - release the lock for exclusive SPI bus usage
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003229 * @ctlr: SPI bus master that was locked for exclusive bus access
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003230 * Context: can sleep
3231 *
3232 * This call may only be used from a context that may sleep. The sleep
3233 * is non-interruptible, and has no timeout.
3234 *
3235 * This call releases an SPI bus lock previously obtained by an spi_bus_lock
3236 * call.
3237 *
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003238 * Return: always zero.
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003239 */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003240int spi_bus_unlock(struct spi_controller *ctlr)
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003241{
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003242 ctlr->bus_lock_flag = 0;
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003243
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003244 mutex_unlock(&ctlr->bus_lock_mutex);
Ernst Schwabcf32b71e2010-06-28 17:49:29 -07003245
3246 return 0;
3247}
3248EXPORT_SYMBOL_GPL(spi_bus_unlock);
3249
David Brownella9948b62006-04-02 10:37:40 -08003250/* portable code must never pass more than 32 bytes */
Jingoo Han5fe5f052013-10-14 10:31:51 +09003251#define SPI_BUFSIZ max(32, SMP_CACHE_BYTES)
David Brownell8ae12a02006-01-08 13:34:19 -08003252
3253static u8 *buf;
3254
3255/**
3256 * spi_write_then_read - SPI synchronous write followed by read
3257 * @spi: device with which data will be exchanged
3258 * @txbuf: data to be written (need not be dma-safe)
3259 * @n_tx: size of txbuf, in bytes
Jiri Pirko27570492009-06-17 16:26:06 -07003260 * @rxbuf: buffer into which data will be read (need not be dma-safe)
3261 * @n_rx: size of rxbuf, in bytes
David Brownell33e34dc2007-05-08 00:32:21 -07003262 * Context: can sleep
David Brownell8ae12a02006-01-08 13:34:19 -08003263 *
3264 * This performs a half duplex MicroWire style transaction with the
3265 * device, sending txbuf and then reading rxbuf. The return value
3266 * is zero for success, else a negative errno status code.
David Brownellb8852442006-01-08 13:34:23 -08003267 * This call may only be used from a context that may sleep.
David Brownell8ae12a02006-01-08 13:34:19 -08003268 *
David Brownell0c8684612006-01-08 13:34:25 -08003269 * Parameters to this routine are always copied using a small buffer;
David Brownell33e34dc2007-05-08 00:32:21 -07003270 * portable code should never use this for more than 32 bytes.
3271 * Performance-sensitive or bulk transfer code should instead use
David Brownell0c8684612006-01-08 13:34:25 -08003272 * spi_{async,sync}() calls with dma-safe buffers.
Javier Martinez Canillas97d56dc2015-10-22 18:59:23 +02003273 *
3274 * Return: zero on success, else a negative error code.
David Brownell8ae12a02006-01-08 13:34:19 -08003275 */
3276int spi_write_then_read(struct spi_device *spi,
Mark Brown0c4a1592011-05-11 00:09:30 +02003277 const void *txbuf, unsigned n_tx,
3278 void *rxbuf, unsigned n_rx)
David Brownell8ae12a02006-01-08 13:34:19 -08003279{
David Brownell068f4072007-12-04 23:45:09 -08003280 static DEFINE_MUTEX(lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003281
3282 int status;
3283 struct spi_message message;
David Brownellbdff5492009-04-13 14:39:57 -07003284 struct spi_transfer x[2];
David Brownell8ae12a02006-01-08 13:34:19 -08003285 u8 *local_buf;
3286
Mark Brownb3a223e2012-12-02 12:54:25 +09003287 /* Use preallocated DMA-safe buffer if we can. We can't avoid
3288 * copying here, (as a pure convenience thing), but we can
3289 * keep heap costs out of the hot path unless someone else is
3290 * using the pre-allocated buffer or the transfer is too large.
David Brownell8ae12a02006-01-08 13:34:19 -08003291 */
Mark Brownb3a223e2012-12-02 12:54:25 +09003292 if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
Mark Brown2cd94c82013-01-27 14:35:04 +08003293 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
3294 GFP_KERNEL | GFP_DMA);
Mark Brownb3a223e2012-12-02 12:54:25 +09003295 if (!local_buf)
3296 return -ENOMEM;
3297 } else {
3298 local_buf = buf;
3299 }
David Brownell8ae12a02006-01-08 13:34:19 -08003300
Vitaly Wool8275c642006-01-08 13:34:28 -08003301 spi_message_init(&message);
Jingoo Han5fe5f052013-10-14 10:31:51 +09003302 memset(x, 0, sizeof(x));
David Brownellbdff5492009-04-13 14:39:57 -07003303 if (n_tx) {
3304 x[0].len = n_tx;
3305 spi_message_add_tail(&x[0], &message);
3306 }
3307 if (n_rx) {
3308 x[1].len = n_rx;
3309 spi_message_add_tail(&x[1], &message);
3310 }
Vitaly Wool8275c642006-01-08 13:34:28 -08003311
David Brownell8ae12a02006-01-08 13:34:19 -08003312 memcpy(local_buf, txbuf, n_tx);
David Brownellbdff5492009-04-13 14:39:57 -07003313 x[0].tx_buf = local_buf;
3314 x[1].rx_buf = local_buf + n_tx;
David Brownell8ae12a02006-01-08 13:34:19 -08003315
3316 /* do the i/o */
David Brownell8ae12a02006-01-08 13:34:19 -08003317 status = spi_sync(spi, &message);
Marc Pignat9b938b72007-12-04 23:45:10 -08003318 if (status == 0)
David Brownellbdff5492009-04-13 14:39:57 -07003319 memcpy(rxbuf, x[1].rx_buf, n_rx);
David Brownell8ae12a02006-01-08 13:34:19 -08003320
David Brownellbdff5492009-04-13 14:39:57 -07003321 if (x[0].tx_buf == buf)
David Brownell068f4072007-12-04 23:45:09 -08003322 mutex_unlock(&lock);
David Brownell8ae12a02006-01-08 13:34:19 -08003323 else
3324 kfree(local_buf);
3325
3326 return status;
3327}
3328EXPORT_SYMBOL_GPL(spi_write_then_read);
3329
3330/*-------------------------------------------------------------------------*/
3331
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003332#if IS_ENABLED(CONFIG_OF_DYNAMIC)
3333static int __spi_of_device_match(struct device *dev, void *data)
3334{
3335 return dev->of_node == data;
3336}
3337
3338/* must call put_device() when done with returned spi_device device */
3339static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
3340{
3341 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
3342 __spi_of_device_match);
3343 return dev ? to_spi_device(dev) : NULL;
3344}
3345
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003346static int __spi_of_controller_match(struct device *dev, const void *data)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003347{
3348 return dev->of_node == data;
3349}
3350
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003351/* the spi controllers are not using spi_bus, so we find it with another way */
3352static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003353{
3354 struct device *dev;
3355
3356 dev = class_find_device(&spi_master_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003357 __spi_of_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003358 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3359 dev = class_find_device(&spi_slave_class, NULL, node,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003360 __spi_of_controller_match);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003361 if (!dev)
3362 return NULL;
3363
3364 /* reference got in class_find_device */
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003365 return container_of(dev, struct spi_controller, dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003366}
3367
3368static int of_spi_notify(struct notifier_block *nb, unsigned long action,
3369 void *arg)
3370{
3371 struct of_reconfig_data *rd = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003372 struct spi_controller *ctlr;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003373 struct spi_device *spi;
3374
3375 switch (of_reconfig_get_state_change(action, arg)) {
3376 case OF_RECONFIG_CHANGE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003377 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
3378 if (ctlr == NULL)
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003379 return NOTIFY_OK; /* not for us */
3380
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003381 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003382 put_device(&ctlr->dev);
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003383 return NOTIFY_OK;
3384 }
3385
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003386 spi = of_register_spi_device(ctlr, rd->dn);
3387 put_device(&ctlr->dev);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003388
3389 if (IS_ERR(spi)) {
Rob Herring25c56c82017-07-18 16:43:31 -05003390 pr_err("%s: failed to create for '%pOF'\n",
3391 __func__, rd->dn);
Ralf Ramsauere0af98a2016-10-17 15:59:56 +02003392 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003393 return notifier_from_errno(PTR_ERR(spi));
3394 }
3395 break;
3396
3397 case OF_RECONFIG_CHANGE_REMOVE:
Geert Uytterhoevenbd6c1642015-11-30 15:28:07 +01003398 /* already depopulated? */
3399 if (!of_node_check_flag(rd->dn, OF_POPULATED))
3400 return NOTIFY_OK;
3401
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003402 /* find our device by node */
3403 spi = of_find_spi_device_by_node(rd->dn);
3404 if (spi == NULL)
3405 return NOTIFY_OK; /* no? not meant for us */
3406
3407 /* unregister takes one ref away */
3408 spi_unregister_device(spi);
3409
3410 /* and put the reference of the find */
3411 put_device(&spi->dev);
3412 break;
3413 }
3414
3415 return NOTIFY_OK;
3416}
3417
3418static struct notifier_block spi_of_notifier = {
3419 .notifier_call = of_spi_notify,
3420};
3421#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3422extern struct notifier_block spi_of_notifier;
3423#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
3424
Octavian Purdila7f244672016-07-08 19:13:11 +03003425#if IS_ENABLED(CONFIG_ACPI)
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003426static int spi_acpi_controller_match(struct device *dev, const void *data)
Octavian Purdila7f244672016-07-08 19:13:11 +03003427{
3428 return ACPI_COMPANION(dev->parent) == data;
3429}
3430
3431static int spi_acpi_device_match(struct device *dev, void *data)
3432{
3433 return ACPI_COMPANION(dev) == data;
3434}
3435
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003436static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
Octavian Purdila7f244672016-07-08 19:13:11 +03003437{
3438 struct device *dev;
3439
3440 dev = class_find_device(&spi_master_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003441 spi_acpi_controller_match);
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003442 if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
3443 dev = class_find_device(&spi_slave_class, NULL, adev,
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003444 spi_acpi_controller_match);
Octavian Purdila7f244672016-07-08 19:13:11 +03003445 if (!dev)
3446 return NULL;
3447
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003448 return container_of(dev, struct spi_controller, dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003449}
3450
3451static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
3452{
3453 struct device *dev;
3454
3455 dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match);
3456
3457 return dev ? to_spi_device(dev) : NULL;
3458}
3459
3460static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
3461 void *arg)
3462{
3463 struct acpi_device *adev = arg;
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003464 struct spi_controller *ctlr;
Octavian Purdila7f244672016-07-08 19:13:11 +03003465 struct spi_device *spi;
3466
3467 switch (value) {
3468 case ACPI_RECONFIG_DEVICE_ADD:
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003469 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
3470 if (!ctlr)
Octavian Purdila7f244672016-07-08 19:13:11 +03003471 break;
3472
Geert Uytterhoeven8caab752017-06-13 13:23:52 +02003473 acpi_register_spi_device(ctlr, adev);
3474 put_device(&ctlr->dev);
Octavian Purdila7f244672016-07-08 19:13:11 +03003475 break;
3476 case ACPI_RECONFIG_DEVICE_REMOVE:
3477 if (!acpi_device_enumerated(adev))
3478 break;
3479
3480 spi = acpi_spi_find_device_by_adev(adev);
3481 if (!spi)
3482 break;
3483
3484 spi_unregister_device(spi);
3485 put_device(&spi->dev);
3486 break;
3487 }
3488
3489 return NOTIFY_OK;
3490}
3491
3492static struct notifier_block spi_acpi_notifier = {
3493 .notifier_call = acpi_spi_notify,
3494};
3495#else
3496extern struct notifier_block spi_acpi_notifier;
3497#endif
3498
David Brownell8ae12a02006-01-08 13:34:19 -08003499static int __init spi_init(void)
3500{
David Brownellb8852442006-01-08 13:34:23 -08003501 int status;
David Brownell8ae12a02006-01-08 13:34:19 -08003502
Christoph Lametere94b1762006-12-06 20:33:17 -08003503 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
David Brownellb8852442006-01-08 13:34:23 -08003504 if (!buf) {
3505 status = -ENOMEM;
3506 goto err0;
3507 }
3508
3509 status = bus_register(&spi_bus_type);
3510 if (status < 0)
3511 goto err1;
3512
3513 status = class_register(&spi_master_class);
3514 if (status < 0)
3515 goto err2;
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003516
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003517 if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
3518 status = class_register(&spi_slave_class);
3519 if (status < 0)
3520 goto err3;
3521 }
3522
Fabio Estevam52677202014-11-26 20:13:57 -02003523 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003524 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
Octavian Purdila7f244672016-07-08 19:13:11 +03003525 if (IS_ENABLED(CONFIG_ACPI))
3526 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
Pantelis Antoniouce79d542014-10-28 22:36:05 +02003527
David Brownell8ae12a02006-01-08 13:34:19 -08003528 return 0;
David Brownellb8852442006-01-08 13:34:23 -08003529
Geert Uytterhoeven6c364062017-05-22 15:11:41 +02003530err3:
3531 class_unregister(&spi_master_class);
David Brownellb8852442006-01-08 13:34:23 -08003532err2:
3533 bus_unregister(&spi_bus_type);
3534err1:
3535 kfree(buf);
3536 buf = NULL;
3537err0:
3538 return status;
David Brownell8ae12a02006-01-08 13:34:19 -08003539}
David Brownellb8852442006-01-08 13:34:23 -08003540
David Brownell8ae12a02006-01-08 13:34:19 -08003541/* board_info is normally registered in arch_initcall(),
3542 * but even essential drivers wait till later
David Brownellb8852442006-01-08 13:34:23 -08003543 *
3544 * REVISIT only boardinfo really needs static linking. the rest (device and
3545 * driver registration) _could_ be dynamically linked (modular) ... costs
3546 * include needing to have boardinfo data structures be much more public.
David Brownell8ae12a02006-01-08 13:34:19 -08003547 */
David Brownell673c0c02008-10-15 22:02:46 -07003548postcore_initcall(spi_init);
David Brownell8ae12a02006-01-08 13:34:19 -08003549