Commit | Line | Data |
---|---|---|
8ae12a0d | 1 | /* |
ca632f55 | 2 | * SPI init/core code |
8ae12a0d DB |
3 | * |
4 | * Copyright (C) 2005 David Brownell | |
d57a4282 | 5 | * Copyright (C) 2008 Secret Lab Technologies Ltd. |
8ae12a0d DB |
6 | * |
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. | |
8ae12a0d DB |
16 | */ |
17 | ||
8ae12a0d DB |
18 | #include <linux/kernel.h> |
19 | #include <linux/device.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/cache.h> | |
99adef31 MB |
22 | #include <linux/dma-mapping.h> |
23 | #include <linux/dmaengine.h> | |
94040828 | 24 | #include <linux/mutex.h> |
2b7a32f7 | 25 | #include <linux/of_device.h> |
d57a4282 | 26 | #include <linux/of_irq.h> |
86be408b | 27 | #include <linux/clk/clk-conf.h> |
5a0e3ad6 | 28 | #include <linux/slab.h> |
e0626e38 | 29 | #include <linux/mod_devicetable.h> |
8ae12a0d | 30 | #include <linux/spi/spi.h> |
74317984 | 31 | #include <linux/of_gpio.h> |
3ae22e8c | 32 | #include <linux/pm_runtime.h> |
f48c767c | 33 | #include <linux/pm_domain.h> |
025ed130 | 34 | #include <linux/export.h> |
8bd75c77 | 35 | #include <linux/sched/rt.h> |
ffbbdd21 LW |
36 | #include <linux/delay.h> |
37 | #include <linux/kthread.h> | |
64bee4d2 MW |
38 | #include <linux/ioport.h> |
39 | #include <linux/acpi.h> | |
8ae12a0d | 40 | |
56ec1978 MB |
41 | #define CREATE_TRACE_POINTS |
42 | #include <trace/events/spi.h> | |
43 | ||
8ae12a0d DB |
44 | static void spidev_release(struct device *dev) |
45 | { | |
0ffa0285 | 46 | struct spi_device *spi = to_spi_device(dev); |
8ae12a0d DB |
47 | |
48 | /* spi masters may cleanup for released devices */ | |
49 | if (spi->master->cleanup) | |
50 | spi->master->cleanup(spi); | |
51 | ||
0c868461 | 52 | spi_master_put(spi->master); |
07a389fe | 53 | kfree(spi); |
8ae12a0d DB |
54 | } |
55 | ||
56 | static ssize_t | |
57 | modalias_show(struct device *dev, struct device_attribute *a, char *buf) | |
58 | { | |
59 | const struct spi_device *spi = to_spi_device(dev); | |
8c4ff6d0 ZR |
60 | int len; |
61 | ||
62 | len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); | |
63 | if (len != -ENODEV) | |
64 | return len; | |
8ae12a0d | 65 | |
d8e328b3 | 66 | return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); |
8ae12a0d | 67 | } |
aa7da564 | 68 | static DEVICE_ATTR_RO(modalias); |
8ae12a0d | 69 | |
eca2ebc7 MS |
70 | #define SPI_STATISTICS_ATTRS(field, file) \ |
71 | static ssize_t spi_master_##field##_show(struct device *dev, \ | |
72 | struct device_attribute *attr, \ | |
73 | char *buf) \ | |
74 | { \ | |
75 | struct spi_master *master = container_of(dev, \ | |
76 | struct spi_master, dev); \ | |
77 | return spi_statistics_##field##_show(&master->statistics, buf); \ | |
78 | } \ | |
79 | static struct device_attribute dev_attr_spi_master_##field = { \ | |
80 | .attr = { .name = file, .mode = S_IRUGO }, \ | |
81 | .show = spi_master_##field##_show, \ | |
82 | }; \ | |
83 | static ssize_t spi_device_##field##_show(struct device *dev, \ | |
84 | struct device_attribute *attr, \ | |
85 | char *buf) \ | |
86 | { \ | |
87 | struct spi_device *spi = container_of(dev, \ | |
88 | struct spi_device, dev); \ | |
89 | return spi_statistics_##field##_show(&spi->statistics, buf); \ | |
90 | } \ | |
91 | static struct device_attribute dev_attr_spi_device_##field = { \ | |
92 | .attr = { .name = file, .mode = S_IRUGO }, \ | |
93 | .show = spi_device_##field##_show, \ | |
94 | } | |
95 | ||
96 | #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \ | |
97 | static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ | |
98 | char *buf) \ | |
99 | { \ | |
100 | unsigned long flags; \ | |
101 | ssize_t len; \ | |
102 | spin_lock_irqsave(&stat->lock, flags); \ | |
103 | len = sprintf(buf, format_string, stat->field); \ | |
104 | spin_unlock_irqrestore(&stat->lock, flags); \ | |
105 | return len; \ | |
106 | } \ | |
107 | SPI_STATISTICS_ATTRS(name, file) | |
108 | ||
109 | #define SPI_STATISTICS_SHOW(field, format_string) \ | |
110 | SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ | |
111 | field, format_string) | |
112 | ||
113 | SPI_STATISTICS_SHOW(messages, "%lu"); | |
114 | SPI_STATISTICS_SHOW(transfers, "%lu"); | |
115 | SPI_STATISTICS_SHOW(errors, "%lu"); | |
116 | SPI_STATISTICS_SHOW(timedout, "%lu"); | |
117 | ||
118 | SPI_STATISTICS_SHOW(spi_sync, "%lu"); | |
119 | SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu"); | |
120 | SPI_STATISTICS_SHOW(spi_async, "%lu"); | |
121 | ||
122 | SPI_STATISTICS_SHOW(bytes, "%llu"); | |
123 | SPI_STATISTICS_SHOW(bytes_rx, "%llu"); | |
124 | SPI_STATISTICS_SHOW(bytes_tx, "%llu"); | |
125 | ||
6b7bc061 MS |
126 | #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ |
127 | SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ | |
128 | "transfer_bytes_histo_" number, \ | |
129 | transfer_bytes_histo[index], "%lu") | |
130 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); | |
131 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); | |
132 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); | |
133 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); | |
134 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); | |
135 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); | |
136 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); | |
137 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); | |
138 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); | |
139 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); | |
140 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); | |
141 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); | |
142 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); | |
143 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); | |
144 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); | |
145 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); | |
146 | SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); | |
147 | ||
aa7da564 GKH |
148 | static struct attribute *spi_dev_attrs[] = { |
149 | &dev_attr_modalias.attr, | |
150 | NULL, | |
8ae12a0d | 151 | }; |
eca2ebc7 MS |
152 | |
153 | static const struct attribute_group spi_dev_group = { | |
154 | .attrs = spi_dev_attrs, | |
155 | }; | |
156 | ||
157 | static struct attribute *spi_device_statistics_attrs[] = { | |
158 | &dev_attr_spi_device_messages.attr, | |
159 | &dev_attr_spi_device_transfers.attr, | |
160 | &dev_attr_spi_device_errors.attr, | |
161 | &dev_attr_spi_device_timedout.attr, | |
162 | &dev_attr_spi_device_spi_sync.attr, | |
163 | &dev_attr_spi_device_spi_sync_immediate.attr, | |
164 | &dev_attr_spi_device_spi_async.attr, | |
165 | &dev_attr_spi_device_bytes.attr, | |
166 | &dev_attr_spi_device_bytes_rx.attr, | |
167 | &dev_attr_spi_device_bytes_tx.attr, | |
6b7bc061 MS |
168 | &dev_attr_spi_device_transfer_bytes_histo0.attr, |
169 | &dev_attr_spi_device_transfer_bytes_histo1.attr, | |
170 | &dev_attr_spi_device_transfer_bytes_histo2.attr, | |
171 | &dev_attr_spi_device_transfer_bytes_histo3.attr, | |
172 | &dev_attr_spi_device_transfer_bytes_histo4.attr, | |
173 | &dev_attr_spi_device_transfer_bytes_histo5.attr, | |
174 | &dev_attr_spi_device_transfer_bytes_histo6.attr, | |
175 | &dev_attr_spi_device_transfer_bytes_histo7.attr, | |
176 | &dev_attr_spi_device_transfer_bytes_histo8.attr, | |
177 | &dev_attr_spi_device_transfer_bytes_histo9.attr, | |
178 | &dev_attr_spi_device_transfer_bytes_histo10.attr, | |
179 | &dev_attr_spi_device_transfer_bytes_histo11.attr, | |
180 | &dev_attr_spi_device_transfer_bytes_histo12.attr, | |
181 | &dev_attr_spi_device_transfer_bytes_histo13.attr, | |
182 | &dev_attr_spi_device_transfer_bytes_histo14.attr, | |
183 | &dev_attr_spi_device_transfer_bytes_histo15.attr, | |
184 | &dev_attr_spi_device_transfer_bytes_histo16.attr, | |
eca2ebc7 MS |
185 | NULL, |
186 | }; | |
187 | ||
188 | static const struct attribute_group spi_device_statistics_group = { | |
189 | .name = "statistics", | |
190 | .attrs = spi_device_statistics_attrs, | |
191 | }; | |
192 | ||
193 | static const struct attribute_group *spi_dev_groups[] = { | |
194 | &spi_dev_group, | |
195 | &spi_device_statistics_group, | |
196 | NULL, | |
197 | }; | |
198 | ||
199 | static struct attribute *spi_master_statistics_attrs[] = { | |
200 | &dev_attr_spi_master_messages.attr, | |
201 | &dev_attr_spi_master_transfers.attr, | |
202 | &dev_attr_spi_master_errors.attr, | |
203 | &dev_attr_spi_master_timedout.attr, | |
204 | &dev_attr_spi_master_spi_sync.attr, | |
205 | &dev_attr_spi_master_spi_sync_immediate.attr, | |
206 | &dev_attr_spi_master_spi_async.attr, | |
207 | &dev_attr_spi_master_bytes.attr, | |
208 | &dev_attr_spi_master_bytes_rx.attr, | |
209 | &dev_attr_spi_master_bytes_tx.attr, | |
6b7bc061 MS |
210 | &dev_attr_spi_master_transfer_bytes_histo0.attr, |
211 | &dev_attr_spi_master_transfer_bytes_histo1.attr, | |
212 | &dev_attr_spi_master_transfer_bytes_histo2.attr, | |
213 | &dev_attr_spi_master_transfer_bytes_histo3.attr, | |
214 | &dev_attr_spi_master_transfer_bytes_histo4.attr, | |
215 | &dev_attr_spi_master_transfer_bytes_histo5.attr, | |
216 | &dev_attr_spi_master_transfer_bytes_histo6.attr, | |
217 | &dev_attr_spi_master_transfer_bytes_histo7.attr, | |
218 | &dev_attr_spi_master_transfer_bytes_histo8.attr, | |
219 | &dev_attr_spi_master_transfer_bytes_histo9.attr, | |
220 | &dev_attr_spi_master_transfer_bytes_histo10.attr, | |
221 | &dev_attr_spi_master_transfer_bytes_histo11.attr, | |
222 | &dev_attr_spi_master_transfer_bytes_histo12.attr, | |
223 | &dev_attr_spi_master_transfer_bytes_histo13.attr, | |
224 | &dev_attr_spi_master_transfer_bytes_histo14.attr, | |
225 | &dev_attr_spi_master_transfer_bytes_histo15.attr, | |
226 | &dev_attr_spi_master_transfer_bytes_histo16.attr, | |
eca2ebc7 MS |
227 | NULL, |
228 | }; | |
229 | ||
230 | static const struct attribute_group spi_master_statistics_group = { | |
231 | .name = "statistics", | |
232 | .attrs = spi_master_statistics_attrs, | |
233 | }; | |
234 | ||
235 | static const struct attribute_group *spi_master_groups[] = { | |
236 | &spi_master_statistics_group, | |
237 | NULL, | |
238 | }; | |
239 | ||
240 | void spi_statistics_add_transfer_stats(struct spi_statistics *stats, | |
241 | struct spi_transfer *xfer, | |
242 | struct spi_master *master) | |
243 | { | |
244 | unsigned long flags; | |
6b7bc061 MS |
245 | int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; |
246 | ||
247 | if (l2len < 0) | |
248 | l2len = 0; | |
eca2ebc7 MS |
249 | |
250 | spin_lock_irqsave(&stats->lock, flags); | |
251 | ||
252 | stats->transfers++; | |
6b7bc061 | 253 | stats->transfer_bytes_histo[l2len]++; |
eca2ebc7 MS |
254 | |
255 | stats->bytes += xfer->len; | |
256 | if ((xfer->tx_buf) && | |
257 | (xfer->tx_buf != master->dummy_tx)) | |
258 | stats->bytes_tx += xfer->len; | |
259 | if ((xfer->rx_buf) && | |
260 | (xfer->rx_buf != master->dummy_rx)) | |
261 | stats->bytes_rx += xfer->len; | |
262 | ||
263 | spin_unlock_irqrestore(&stats->lock, flags); | |
264 | } | |
265 | EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats); | |
8ae12a0d DB |
266 | |
267 | /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, | |
268 | * and the sysfs version makes coldplug work too. | |
269 | */ | |
270 | ||
75368bf6 AV |
271 | static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, |
272 | const struct spi_device *sdev) | |
273 | { | |
274 | while (id->name[0]) { | |
275 | if (!strcmp(sdev->modalias, id->name)) | |
276 | return id; | |
277 | id++; | |
278 | } | |
279 | return NULL; | |
280 | } | |
281 | ||
282 | const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) | |
283 | { | |
284 | const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); | |
285 | ||
286 | return spi_match_id(sdrv->id_table, sdev); | |
287 | } | |
288 | EXPORT_SYMBOL_GPL(spi_get_device_id); | |
289 | ||
8ae12a0d DB |
290 | static int spi_match_device(struct device *dev, struct device_driver *drv) |
291 | { | |
292 | const struct spi_device *spi = to_spi_device(dev); | |
75368bf6 AV |
293 | const struct spi_driver *sdrv = to_spi_driver(drv); |
294 | ||
2b7a32f7 SA |
295 | /* Attempt an OF style match */ |
296 | if (of_driver_match_device(dev, drv)) | |
297 | return 1; | |
298 | ||
64bee4d2 MW |
299 | /* Then try ACPI */ |
300 | if (acpi_driver_match_device(dev, drv)) | |
301 | return 1; | |
302 | ||
75368bf6 AV |
303 | if (sdrv->id_table) |
304 | return !!spi_match_id(sdrv->id_table, spi); | |
8ae12a0d | 305 | |
35f74fca | 306 | return strcmp(spi->modalias, drv->name) == 0; |
8ae12a0d DB |
307 | } |
308 | ||
7eff2e7a | 309 | static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) |
8ae12a0d DB |
310 | { |
311 | const struct spi_device *spi = to_spi_device(dev); | |
8c4ff6d0 ZR |
312 | int rc; |
313 | ||
314 | rc = acpi_device_uevent_modalias(dev, env); | |
315 | if (rc != -ENODEV) | |
316 | return rc; | |
8ae12a0d | 317 | |
e0626e38 | 318 | add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); |
8ae12a0d DB |
319 | return 0; |
320 | } | |
321 | ||
8ae12a0d DB |
322 | struct bus_type spi_bus_type = { |
323 | .name = "spi", | |
aa7da564 | 324 | .dev_groups = spi_dev_groups, |
8ae12a0d DB |
325 | .match = spi_match_device, |
326 | .uevent = spi_uevent, | |
8ae12a0d DB |
327 | }; |
328 | EXPORT_SYMBOL_GPL(spi_bus_type); | |
329 | ||
b885244e DB |
330 | |
331 | static int spi_drv_probe(struct device *dev) | |
332 | { | |
333 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); | |
44af7927 | 334 | struct spi_device *spi = to_spi_device(dev); |
33cf00e5 MW |
335 | int ret; |
336 | ||
86be408b SN |
337 | ret = of_clk_set_defaults(dev->of_node, false); |
338 | if (ret) | |
339 | return ret; | |
340 | ||
44af7927 JH |
341 | if (dev->of_node) { |
342 | spi->irq = of_irq_get(dev->of_node, 0); | |
343 | if (spi->irq == -EPROBE_DEFER) | |
344 | return -EPROBE_DEFER; | |
345 | if (spi->irq < 0) | |
346 | spi->irq = 0; | |
347 | } | |
348 | ||
676e7c25 UH |
349 | ret = dev_pm_domain_attach(dev, true); |
350 | if (ret != -EPROBE_DEFER) { | |
44af7927 | 351 | ret = sdrv->probe(spi); |
676e7c25 UH |
352 | if (ret) |
353 | dev_pm_domain_detach(dev, true); | |
354 | } | |
b885244e | 355 | |
33cf00e5 | 356 | return ret; |
b885244e DB |
357 | } |
358 | ||
359 | static int spi_drv_remove(struct device *dev) | |
360 | { | |
361 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); | |
33cf00e5 MW |
362 | int ret; |
363 | ||
aec35f4e | 364 | ret = sdrv->remove(to_spi_device(dev)); |
676e7c25 | 365 | dev_pm_domain_detach(dev, true); |
b885244e | 366 | |
33cf00e5 | 367 | return ret; |
b885244e DB |
368 | } |
369 | ||
370 | static void spi_drv_shutdown(struct device *dev) | |
371 | { | |
372 | const struct spi_driver *sdrv = to_spi_driver(dev->driver); | |
373 | ||
374 | sdrv->shutdown(to_spi_device(dev)); | |
375 | } | |
376 | ||
33e34dc6 DB |
377 | /** |
378 | * spi_register_driver - register a SPI driver | |
379 | * @sdrv: the driver to register | |
380 | * Context: can sleep | |
381 | */ | |
b885244e DB |
382 | int spi_register_driver(struct spi_driver *sdrv) |
383 | { | |
384 | sdrv->driver.bus = &spi_bus_type; | |
385 | if (sdrv->probe) | |
386 | sdrv->driver.probe = spi_drv_probe; | |
387 | if (sdrv->remove) | |
388 | sdrv->driver.remove = spi_drv_remove; | |
389 | if (sdrv->shutdown) | |
390 | sdrv->driver.shutdown = spi_drv_shutdown; | |
391 | return driver_register(&sdrv->driver); | |
392 | } | |
393 | EXPORT_SYMBOL_GPL(spi_register_driver); | |
394 | ||
8ae12a0d DB |
395 | /*-------------------------------------------------------------------------*/ |
396 | ||
397 | /* SPI devices should normally not be created by SPI device drivers; that | |
398 | * would make them board-specific. Similarly with SPI master drivers. | |
399 | * Device registration normally goes into like arch/.../mach.../board-YYY.c | |
400 | * with other readonly (flashable) information about mainboard devices. | |
401 | */ | |
402 | ||
403 | struct boardinfo { | |
404 | struct list_head list; | |
2b9603a0 | 405 | struct spi_board_info board_info; |
8ae12a0d DB |
406 | }; |
407 | ||
408 | static LIST_HEAD(board_list); | |
2b9603a0 FT |
409 | static LIST_HEAD(spi_master_list); |
410 | ||
411 | /* | |
412 | * Used to protect add/del opertion for board_info list and | |
413 | * spi_master list, and their matching process | |
414 | */ | |
94040828 | 415 | static DEFINE_MUTEX(board_lock); |
8ae12a0d | 416 | |
dc87c98e GL |
417 | /** |
418 | * spi_alloc_device - Allocate a new SPI device | |
419 | * @master: Controller to which device is connected | |
420 | * Context: can sleep | |
421 | * | |
422 | * Allows a driver to allocate and initialize a spi_device without | |
423 | * registering it immediately. This allows a driver to directly | |
424 | * fill the spi_device with device parameters before calling | |
425 | * spi_add_device() on it. | |
426 | * | |
427 | * Caller is responsible to call spi_add_device() on the returned | |
428 | * spi_device structure to add it to the SPI master. If the caller | |
429 | * needs to discard the spi_device without adding it, then it should | |
430 | * call spi_dev_put() on it. | |
431 | * | |
432 | * Returns a pointer to the new device, or NULL. | |
433 | */ | |
434 | struct spi_device *spi_alloc_device(struct spi_master *master) | |
435 | { | |
436 | struct spi_device *spi; | |
dc87c98e GL |
437 | |
438 | if (!spi_master_get(master)) | |
439 | return NULL; | |
440 | ||
5fe5f05e | 441 | spi = kzalloc(sizeof(*spi), GFP_KERNEL); |
dc87c98e | 442 | if (!spi) { |
dc87c98e GL |
443 | spi_master_put(master); |
444 | return NULL; | |
445 | } | |
446 | ||
447 | spi->master = master; | |
178db7d3 | 448 | spi->dev.parent = &master->dev; |
dc87c98e GL |
449 | spi->dev.bus = &spi_bus_type; |
450 | spi->dev.release = spidev_release; | |
446411e1 | 451 | spi->cs_gpio = -ENOENT; |
eca2ebc7 MS |
452 | |
453 | spin_lock_init(&spi->statistics.lock); | |
454 | ||
dc87c98e GL |
455 | device_initialize(&spi->dev); |
456 | return spi; | |
457 | } | |
458 | EXPORT_SYMBOL_GPL(spi_alloc_device); | |
459 | ||
e13ac47b JN |
460 | static void spi_dev_set_name(struct spi_device *spi) |
461 | { | |
462 | struct acpi_device *adev = ACPI_COMPANION(&spi->dev); | |
463 | ||
464 | if (adev) { | |
465 | dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); | |
466 | return; | |
467 | } | |
468 | ||
469 | dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev), | |
470 | spi->chip_select); | |
471 | } | |
472 | ||
b6fb8d3a MW |
473 | static int spi_dev_check(struct device *dev, void *data) |
474 | { | |
475 | struct spi_device *spi = to_spi_device(dev); | |
476 | struct spi_device *new_spi = data; | |
477 | ||
478 | if (spi->master == new_spi->master && | |
479 | spi->chip_select == new_spi->chip_select) | |
480 | return -EBUSY; | |
481 | return 0; | |
482 | } | |
483 | ||
dc87c98e GL |
484 | /** |
485 | * spi_add_device - Add spi_device allocated with spi_alloc_device | |
486 | * @spi: spi_device to register | |
487 | * | |
488 | * Companion function to spi_alloc_device. Devices allocated with | |
489 | * spi_alloc_device can be added onto the spi bus with this function. | |
490 | * | |
e48880e0 | 491 | * Returns 0 on success; negative errno on failure |
dc87c98e GL |
492 | */ |
493 | int spi_add_device(struct spi_device *spi) | |
494 | { | |
e48880e0 | 495 | static DEFINE_MUTEX(spi_add_lock); |
74317984 JCPV |
496 | struct spi_master *master = spi->master; |
497 | struct device *dev = master->dev.parent; | |
dc87c98e GL |
498 | int status; |
499 | ||
500 | /* Chipselects are numbered 0..max; validate. */ | |
74317984 | 501 | if (spi->chip_select >= master->num_chipselect) { |
dc87c98e GL |
502 | dev_err(dev, "cs%d >= max %d\n", |
503 | spi->chip_select, | |
74317984 | 504 | master->num_chipselect); |
dc87c98e GL |
505 | return -EINVAL; |
506 | } | |
507 | ||
508 | /* Set the bus ID string */ | |
e13ac47b | 509 | spi_dev_set_name(spi); |
e48880e0 DB |
510 | |
511 | /* We need to make sure there's no other device with this | |
512 | * chipselect **BEFORE** we call setup(), else we'll trash | |
513 | * its configuration. Lock against concurrent add() calls. | |
514 | */ | |
515 | mutex_lock(&spi_add_lock); | |
516 | ||
b6fb8d3a MW |
517 | status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); |
518 | if (status) { | |
e48880e0 DB |
519 | dev_err(dev, "chipselect %d already in use\n", |
520 | spi->chip_select); | |
e48880e0 DB |
521 | goto done; |
522 | } | |
523 | ||
74317984 JCPV |
524 | if (master->cs_gpios) |
525 | spi->cs_gpio = master->cs_gpios[spi->chip_select]; | |
526 | ||
e48880e0 DB |
527 | /* Drivers may modify this initial i/o setup, but will |
528 | * normally rely on the device being setup. Devices | |
529 | * using SPI_CS_HIGH can't coexist well otherwise... | |
530 | */ | |
7d077197 | 531 | status = spi_setup(spi); |
dc87c98e | 532 | if (status < 0) { |
eb288a1f LW |
533 | dev_err(dev, "can't setup %s, status %d\n", |
534 | dev_name(&spi->dev), status); | |
e48880e0 | 535 | goto done; |
dc87c98e GL |
536 | } |
537 | ||
e48880e0 | 538 | /* Device may be bound to an active driver when this returns */ |
dc87c98e | 539 | status = device_add(&spi->dev); |
e48880e0 | 540 | if (status < 0) |
eb288a1f LW |
541 | dev_err(dev, "can't add %s, status %d\n", |
542 | dev_name(&spi->dev), status); | |
e48880e0 | 543 | else |
35f74fca | 544 | dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); |
dc87c98e | 545 | |
e48880e0 DB |
546 | done: |
547 | mutex_unlock(&spi_add_lock); | |
548 | return status; | |
dc87c98e GL |
549 | } |
550 | EXPORT_SYMBOL_GPL(spi_add_device); | |
8ae12a0d | 551 | |
33e34dc6 DB |
552 | /** |
553 | * spi_new_device - instantiate one new SPI device | |
554 | * @master: Controller to which device is connected | |
555 | * @chip: Describes the SPI device | |
556 | * Context: can sleep | |
557 | * | |
558 | * On typical mainboards, this is purely internal; and it's not needed | |
8ae12a0d DB |
559 | * after board init creates the hard-wired devices. Some development |
560 | * platforms may not be able to use spi_register_board_info though, and | |
561 | * this is exported so that for example a USB or parport based adapter | |
562 | * driver could add devices (which it would learn about out-of-band). | |
082c8cb4 DB |
563 | * |
564 | * Returns the new device, or NULL. | |
8ae12a0d | 565 | */ |
e9d5a461 AB |
566 | struct spi_device *spi_new_device(struct spi_master *master, |
567 | struct spi_board_info *chip) | |
8ae12a0d DB |
568 | { |
569 | struct spi_device *proxy; | |
8ae12a0d DB |
570 | int status; |
571 | ||
082c8cb4 DB |
572 | /* NOTE: caller did any chip->bus_num checks necessary. |
573 | * | |
574 | * Also, unless we change the return value convention to use | |
575 | * error-or-pointer (not NULL-or-pointer), troubleshootability | |
576 | * suggests syslogged diagnostics are best here (ugh). | |
577 | */ | |
578 | ||
dc87c98e GL |
579 | proxy = spi_alloc_device(master); |
580 | if (!proxy) | |
8ae12a0d DB |
581 | return NULL; |
582 | ||
102eb975 GL |
583 | WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); |
584 | ||
8ae12a0d DB |
585 | proxy->chip_select = chip->chip_select; |
586 | proxy->max_speed_hz = chip->max_speed_hz; | |
980a01c9 | 587 | proxy->mode = chip->mode; |
8ae12a0d | 588 | proxy->irq = chip->irq; |
102eb975 | 589 | strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); |
8ae12a0d DB |
590 | proxy->dev.platform_data = (void *) chip->platform_data; |
591 | proxy->controller_data = chip->controller_data; | |
592 | proxy->controller_state = NULL; | |
8ae12a0d | 593 | |
dc87c98e | 594 | status = spi_add_device(proxy); |
8ae12a0d | 595 | if (status < 0) { |
dc87c98e GL |
596 | spi_dev_put(proxy); |
597 | return NULL; | |
8ae12a0d DB |
598 | } |
599 | ||
8ae12a0d DB |
600 | return proxy; |
601 | } | |
602 | EXPORT_SYMBOL_GPL(spi_new_device); | |
603 | ||
2b9603a0 FT |
604 | static void spi_match_master_to_boardinfo(struct spi_master *master, |
605 | struct spi_board_info *bi) | |
606 | { | |
607 | struct spi_device *dev; | |
608 | ||
609 | if (master->bus_num != bi->bus_num) | |
610 | return; | |
611 | ||
612 | dev = spi_new_device(master, bi); | |
613 | if (!dev) | |
614 | dev_err(master->dev.parent, "can't create new device for %s\n", | |
615 | bi->modalias); | |
616 | } | |
617 | ||
33e34dc6 DB |
618 | /** |
619 | * spi_register_board_info - register SPI devices for a given board | |
620 | * @info: array of chip descriptors | |
621 | * @n: how many descriptors are provided | |
622 | * Context: can sleep | |
623 | * | |
8ae12a0d DB |
624 | * Board-specific early init code calls this (probably during arch_initcall) |
625 | * with segments of the SPI device table. Any device nodes are created later, | |
626 | * after the relevant parent SPI controller (bus_num) is defined. We keep | |
627 | * this table of devices forever, so that reloading a controller driver will | |
628 | * not make Linux forget about these hard-wired devices. | |
629 | * | |
630 | * Other code can also call this, e.g. a particular add-on board might provide | |
631 | * SPI devices through its expansion connector, so code initializing that board | |
632 | * would naturally declare its SPI devices. | |
633 | * | |
634 | * The board info passed can safely be __initdata ... but be careful of | |
635 | * any embedded pointers (platform_data, etc), they're copied as-is. | |
636 | */ | |
fd4a319b | 637 | int spi_register_board_info(struct spi_board_info const *info, unsigned n) |
8ae12a0d | 638 | { |
2b9603a0 FT |
639 | struct boardinfo *bi; |
640 | int i; | |
8ae12a0d | 641 | |
c7908a37 XL |
642 | if (!n) |
643 | return -EINVAL; | |
644 | ||
2b9603a0 | 645 | bi = kzalloc(n * sizeof(*bi), GFP_KERNEL); |
8ae12a0d DB |
646 | if (!bi) |
647 | return -ENOMEM; | |
8ae12a0d | 648 | |
2b9603a0 FT |
649 | for (i = 0; i < n; i++, bi++, info++) { |
650 | struct spi_master *master; | |
8ae12a0d | 651 | |
2b9603a0 FT |
652 | memcpy(&bi->board_info, info, sizeof(*info)); |
653 | mutex_lock(&board_lock); | |
654 | list_add_tail(&bi->list, &board_list); | |
655 | list_for_each_entry(master, &spi_master_list, list) | |
656 | spi_match_master_to_boardinfo(master, &bi->board_info); | |
657 | mutex_unlock(&board_lock); | |
8ae12a0d | 658 | } |
2b9603a0 FT |
659 | |
660 | return 0; | |
8ae12a0d DB |
661 | } |
662 | ||
663 | /*-------------------------------------------------------------------------*/ | |
664 | ||
b158935f MB |
665 | static void spi_set_cs(struct spi_device *spi, bool enable) |
666 | { | |
667 | if (spi->mode & SPI_CS_HIGH) | |
668 | enable = !enable; | |
669 | ||
243f07be | 670 | if (gpio_is_valid(spi->cs_gpio)) |
b158935f MB |
671 | gpio_set_value(spi->cs_gpio, !enable); |
672 | else if (spi->master->set_cs) | |
673 | spi->master->set_cs(spi, !enable); | |
674 | } | |
675 | ||
2de440f5 | 676 | #ifdef CONFIG_HAS_DMA |
6ad45a27 MB |
677 | static int spi_map_buf(struct spi_master *master, struct device *dev, |
678 | struct sg_table *sgt, void *buf, size_t len, | |
679 | enum dma_data_direction dir) | |
680 | { | |
681 | const bool vmalloced_buf = is_vmalloc_addr(buf); | |
65598c13 AG |
682 | int desc_len; |
683 | int sgs; | |
6ad45a27 MB |
684 | struct page *vm_page; |
685 | void *sg_buf; | |
686 | size_t min; | |
687 | int i, ret; | |
688 | ||
65598c13 AG |
689 | if (vmalloced_buf) { |
690 | desc_len = PAGE_SIZE; | |
691 | sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); | |
692 | } else { | |
693 | desc_len = master->max_dma_len; | |
694 | sgs = DIV_ROUND_UP(len, desc_len); | |
695 | } | |
696 | ||
6ad45a27 MB |
697 | ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); |
698 | if (ret != 0) | |
699 | return ret; | |
700 | ||
701 | for (i = 0; i < sgs; i++) { | |
6ad45a27 MB |
702 | |
703 | if (vmalloced_buf) { | |
65598c13 AG |
704 | min = min_t(size_t, |
705 | len, desc_len - offset_in_page(buf)); | |
6ad45a27 MB |
706 | vm_page = vmalloc_to_page(buf); |
707 | if (!vm_page) { | |
708 | sg_free_table(sgt); | |
709 | return -ENOMEM; | |
710 | } | |
c1aefbdd CK |
711 | sg_set_page(&sgt->sgl[i], vm_page, |
712 | min, offset_in_page(buf)); | |
6ad45a27 | 713 | } else { |
65598c13 | 714 | min = min_t(size_t, len, desc_len); |
6ad45a27 | 715 | sg_buf = buf; |
c1aefbdd | 716 | sg_set_buf(&sgt->sgl[i], sg_buf, min); |
6ad45a27 MB |
717 | } |
718 | ||
6ad45a27 MB |
719 | |
720 | buf += min; | |
721 | len -= min; | |
722 | } | |
723 | ||
724 | ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); | |
89e4b66a GU |
725 | if (!ret) |
726 | ret = -ENOMEM; | |
6ad45a27 MB |
727 | if (ret < 0) { |
728 | sg_free_table(sgt); | |
729 | return ret; | |
730 | } | |
731 | ||
732 | sgt->nents = ret; | |
733 | ||
734 | return 0; | |
735 | } | |
736 | ||
737 | static void spi_unmap_buf(struct spi_master *master, struct device *dev, | |
738 | struct sg_table *sgt, enum dma_data_direction dir) | |
739 | { | |
740 | if (sgt->orig_nents) { | |
741 | dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); | |
742 | sg_free_table(sgt); | |
743 | } | |
744 | } | |
745 | ||
2de440f5 | 746 | static int __spi_map_msg(struct spi_master *master, struct spi_message *msg) |
99adef31 | 747 | { |
99adef31 MB |
748 | struct device *tx_dev, *rx_dev; |
749 | struct spi_transfer *xfer; | |
6ad45a27 | 750 | int ret; |
3a2eba9b | 751 | |
6ad45a27 | 752 | if (!master->can_dma) |
99adef31 MB |
753 | return 0; |
754 | ||
c37f45b5 LL |
755 | if (master->dma_tx) |
756 | tx_dev = master->dma_tx->device->dev; | |
757 | else | |
758 | tx_dev = &master->dev; | |
759 | ||
760 | if (master->dma_rx) | |
761 | rx_dev = master->dma_rx->device->dev; | |
762 | else | |
763 | rx_dev = &master->dev; | |
99adef31 MB |
764 | |
765 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { | |
766 | if (!master->can_dma(master, msg->spi, xfer)) | |
767 | continue; | |
768 | ||
769 | if (xfer->tx_buf != NULL) { | |
6ad45a27 MB |
770 | ret = spi_map_buf(master, tx_dev, &xfer->tx_sg, |
771 | (void *)xfer->tx_buf, xfer->len, | |
772 | DMA_TO_DEVICE); | |
773 | if (ret != 0) | |
774 | return ret; | |
99adef31 MB |
775 | } |
776 | ||
777 | if (xfer->rx_buf != NULL) { | |
6ad45a27 MB |
778 | ret = spi_map_buf(master, rx_dev, &xfer->rx_sg, |
779 | xfer->rx_buf, xfer->len, | |
780 | DMA_FROM_DEVICE); | |
781 | if (ret != 0) { | |
782 | spi_unmap_buf(master, tx_dev, &xfer->tx_sg, | |
783 | DMA_TO_DEVICE); | |
784 | return ret; | |
99adef31 MB |
785 | } |
786 | } | |
787 | } | |
788 | ||
789 | master->cur_msg_mapped = true; | |
790 | ||
791 | return 0; | |
792 | } | |
793 | ||
4b786458 | 794 | static int __spi_unmap_msg(struct spi_master *master, struct spi_message *msg) |
99adef31 MB |
795 | { |
796 | struct spi_transfer *xfer; | |
797 | struct device *tx_dev, *rx_dev; | |
798 | ||
6ad45a27 | 799 | if (!master->cur_msg_mapped || !master->can_dma) |
99adef31 MB |
800 | return 0; |
801 | ||
c37f45b5 LL |
802 | if (master->dma_tx) |
803 | tx_dev = master->dma_tx->device->dev; | |
804 | else | |
805 | tx_dev = &master->dev; | |
806 | ||
807 | if (master->dma_rx) | |
808 | rx_dev = master->dma_rx->device->dev; | |
809 | else | |
810 | rx_dev = &master->dev; | |
99adef31 MB |
811 | |
812 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { | |
813 | if (!master->can_dma(master, msg->spi, xfer)) | |
814 | continue; | |
815 | ||
6ad45a27 MB |
816 | spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); |
817 | spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); | |
99adef31 MB |
818 | } |
819 | ||
820 | return 0; | |
821 | } | |
2de440f5 GU |
822 | #else /* !CONFIG_HAS_DMA */ |
823 | static inline int __spi_map_msg(struct spi_master *master, | |
824 | struct spi_message *msg) | |
825 | { | |
826 | return 0; | |
827 | } | |
828 | ||
4b786458 MS |
829 | static inline int __spi_unmap_msg(struct spi_master *master, |
830 | struct spi_message *msg) | |
2de440f5 GU |
831 | { |
832 | return 0; | |
833 | } | |
834 | #endif /* !CONFIG_HAS_DMA */ | |
835 | ||
4b786458 MS |
836 | static inline int spi_unmap_msg(struct spi_master *master, |
837 | struct spi_message *msg) | |
838 | { | |
839 | struct spi_transfer *xfer; | |
840 | ||
841 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { | |
842 | /* | |
843 | * Restore the original value of tx_buf or rx_buf if they are | |
844 | * NULL. | |
845 | */ | |
846 | if (xfer->tx_buf == master->dummy_tx) | |
847 | xfer->tx_buf = NULL; | |
848 | if (xfer->rx_buf == master->dummy_rx) | |
849 | xfer->rx_buf = NULL; | |
850 | } | |
851 | ||
852 | return __spi_unmap_msg(master, msg); | |
853 | } | |
854 | ||
2de440f5 GU |
855 | static int spi_map_msg(struct spi_master *master, struct spi_message *msg) |
856 | { | |
857 | struct spi_transfer *xfer; | |
858 | void *tmp; | |
859 | unsigned int max_tx, max_rx; | |
860 | ||
861 | if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) { | |
862 | max_tx = 0; | |
863 | max_rx = 0; | |
864 | ||
865 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { | |
866 | if ((master->flags & SPI_MASTER_MUST_TX) && | |
867 | !xfer->tx_buf) | |
868 | max_tx = max(xfer->len, max_tx); | |
869 | if ((master->flags & SPI_MASTER_MUST_RX) && | |
870 | !xfer->rx_buf) | |
871 | max_rx = max(xfer->len, max_rx); | |
872 | } | |
873 | ||
874 | if (max_tx) { | |
875 | tmp = krealloc(master->dummy_tx, max_tx, | |
876 | GFP_KERNEL | GFP_DMA); | |
877 | if (!tmp) | |
878 | return -ENOMEM; | |
879 | master->dummy_tx = tmp; | |
880 | memset(tmp, 0, max_tx); | |
881 | } | |
882 | ||
883 | if (max_rx) { | |
884 | tmp = krealloc(master->dummy_rx, max_rx, | |
885 | GFP_KERNEL | GFP_DMA); | |
886 | if (!tmp) | |
887 | return -ENOMEM; | |
888 | master->dummy_rx = tmp; | |
889 | } | |
890 | ||
891 | if (max_tx || max_rx) { | |
892 | list_for_each_entry(xfer, &msg->transfers, | |
893 | transfer_list) { | |
894 | if (!xfer->tx_buf) | |
895 | xfer->tx_buf = master->dummy_tx; | |
896 | if (!xfer->rx_buf) | |
897 | xfer->rx_buf = master->dummy_rx; | |
898 | } | |
899 | } | |
900 | } | |
901 | ||
902 | return __spi_map_msg(master, msg); | |
903 | } | |
99adef31 | 904 | |
b158935f MB |
905 | /* |
906 | * spi_transfer_one_message - Default implementation of transfer_one_message() | |
907 | * | |
908 | * This is a standard implementation of transfer_one_message() for | |
909 | * drivers which impelment a transfer_one() operation. It provides | |
910 | * standard handling of delays and chip select management. | |
911 | */ | |
912 | static int spi_transfer_one_message(struct spi_master *master, | |
913 | struct spi_message *msg) | |
914 | { | |
915 | struct spi_transfer *xfer; | |
b158935f MB |
916 | bool keep_cs = false; |
917 | int ret = 0; | |
682a71b2 | 918 | unsigned long ms = 1; |
eca2ebc7 MS |
919 | struct spi_statistics *statm = &master->statistics; |
920 | struct spi_statistics *stats = &msg->spi->statistics; | |
b158935f MB |
921 | |
922 | spi_set_cs(msg->spi, true); | |
923 | ||
eca2ebc7 MS |
924 | SPI_STATISTICS_INCREMENT_FIELD(statm, messages); |
925 | SPI_STATISTICS_INCREMENT_FIELD(stats, messages); | |
926 | ||
b158935f MB |
927 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
928 | trace_spi_transfer_start(msg, xfer); | |
929 | ||
eca2ebc7 MS |
930 | spi_statistics_add_transfer_stats(statm, xfer, master); |
931 | spi_statistics_add_transfer_stats(stats, xfer, master); | |
932 | ||
38ec10f6 MB |
933 | if (xfer->tx_buf || xfer->rx_buf) { |
934 | reinit_completion(&master->xfer_completion); | |
b158935f | 935 | |
38ec10f6 MB |
936 | ret = master->transfer_one(master, msg->spi, xfer); |
937 | if (ret < 0) { | |
eca2ebc7 MS |
938 | SPI_STATISTICS_INCREMENT_FIELD(statm, |
939 | errors); | |
940 | SPI_STATISTICS_INCREMENT_FIELD(stats, | |
941 | errors); | |
38ec10f6 MB |
942 | dev_err(&msg->spi->dev, |
943 | "SPI transfer failed: %d\n", ret); | |
944 | goto out; | |
945 | } | |
b158935f | 946 | |
38ec10f6 MB |
947 | if (ret > 0) { |
948 | ret = 0; | |
949 | ms = xfer->len * 8 * 1000 / xfer->speed_hz; | |
950 | ms += ms + 100; /* some tolerance */ | |
16a0ce4e | 951 | |
38ec10f6 MB |
952 | ms = wait_for_completion_timeout(&master->xfer_completion, |
953 | msecs_to_jiffies(ms)); | |
954 | } | |
16a0ce4e | 955 | |
38ec10f6 | 956 | if (ms == 0) { |
eca2ebc7 MS |
957 | SPI_STATISTICS_INCREMENT_FIELD(statm, |
958 | timedout); | |
959 | SPI_STATISTICS_INCREMENT_FIELD(stats, | |
960 | timedout); | |
38ec10f6 MB |
961 | dev_err(&msg->spi->dev, |
962 | "SPI transfer timed out\n"); | |
963 | msg->status = -ETIMEDOUT; | |
964 | } | |
965 | } else { | |
966 | if (xfer->len) | |
967 | dev_err(&msg->spi->dev, | |
968 | "Bufferless transfer has length %u\n", | |
969 | xfer->len); | |
13a42798 | 970 | } |
b158935f MB |
971 | |
972 | trace_spi_transfer_stop(msg, xfer); | |
973 | ||
974 | if (msg->status != -EINPROGRESS) | |
975 | goto out; | |
976 | ||
977 | if (xfer->delay_usecs) | |
978 | udelay(xfer->delay_usecs); | |
979 | ||
980 | if (xfer->cs_change) { | |
981 | if (list_is_last(&xfer->transfer_list, | |
982 | &msg->transfers)) { | |
983 | keep_cs = true; | |
984 | } else { | |
0b73aa63 MB |
985 | spi_set_cs(msg->spi, false); |
986 | udelay(10); | |
987 | spi_set_cs(msg->spi, true); | |
b158935f MB |
988 | } |
989 | } | |
990 | ||
991 | msg->actual_length += xfer->len; | |
992 | } | |
993 | ||
994 | out: | |
995 | if (ret != 0 || !keep_cs) | |
996 | spi_set_cs(msg->spi, false); | |
997 | ||
998 | if (msg->status == -EINPROGRESS) | |
999 | msg->status = ret; | |
1000 | ||
ff61eb42 | 1001 | if (msg->status && master->handle_err) |
b716c4ff AS |
1002 | master->handle_err(master, msg); |
1003 | ||
b158935f MB |
1004 | spi_finalize_current_message(master); |
1005 | ||
1006 | return ret; | |
1007 | } | |
1008 | ||
1009 | /** | |
1010 | * spi_finalize_current_transfer - report completion of a transfer | |
2c675689 | 1011 | * @master: the master reporting completion |
b158935f MB |
1012 | * |
1013 | * Called by SPI drivers using the core transfer_one_message() | |
1014 | * implementation to notify it that the current interrupt driven | |
9e8f4882 | 1015 | * transfer has finished and the next one may be scheduled. |
b158935f MB |
1016 | */ |
1017 | void spi_finalize_current_transfer(struct spi_master *master) | |
1018 | { | |
1019 | complete(&master->xfer_completion); | |
1020 | } | |
1021 | EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); | |
1022 | ||
ffbbdd21 | 1023 | /** |
fc9e0f71 MB |
1024 | * __spi_pump_messages - function which processes spi message queue |
1025 | * @master: master to process queue for | |
1026 | * @in_kthread: true if we are in the context of the message pump thread | |
ffbbdd21 LW |
1027 | * |
1028 | * This function checks if there is any spi message in the queue that | |
1029 | * needs processing and if so call out to the driver to initialize hardware | |
1030 | * and transfer each message. | |
1031 | * | |
0461a414 MB |
1032 | * Note that it is called both from the kthread itself and also from |
1033 | * inside spi_sync(); the queue extraction handling at the top of the | |
1034 | * function should deal with this safely. | |
ffbbdd21 | 1035 | */ |
fc9e0f71 | 1036 | static void __spi_pump_messages(struct spi_master *master, bool in_kthread) |
ffbbdd21 | 1037 | { |
ffbbdd21 LW |
1038 | unsigned long flags; |
1039 | bool was_busy = false; | |
1040 | int ret; | |
1041 | ||
983aee5d | 1042 | /* Lock queue */ |
ffbbdd21 | 1043 | spin_lock_irqsave(&master->queue_lock, flags); |
983aee5d MB |
1044 | |
1045 | /* Make sure we are not already running a message */ | |
1046 | if (master->cur_msg) { | |
1047 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1048 | return; | |
1049 | } | |
1050 | ||
0461a414 MB |
1051 | /* If another context is idling the device then defer */ |
1052 | if (master->idling) { | |
1053 | queue_kthread_work(&master->kworker, &master->pump_messages); | |
1054 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1055 | return; | |
1056 | } | |
1057 | ||
983aee5d | 1058 | /* Check if the queue is idle */ |
ffbbdd21 | 1059 | if (list_empty(&master->queue) || !master->running) { |
b0b36b86 BF |
1060 | if (!master->busy) { |
1061 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1062 | return; | |
ffbbdd21 | 1063 | } |
fc9e0f71 MB |
1064 | |
1065 | /* Only do teardown in the thread */ | |
1066 | if (!in_kthread) { | |
1067 | queue_kthread_work(&master->kworker, | |
1068 | &master->pump_messages); | |
1069 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1070 | return; | |
1071 | } | |
1072 | ||
ffbbdd21 | 1073 | master->busy = false; |
0461a414 | 1074 | master->idling = true; |
ffbbdd21 | 1075 | spin_unlock_irqrestore(&master->queue_lock, flags); |
0461a414 | 1076 | |
3a2eba9b MB |
1077 | kfree(master->dummy_rx); |
1078 | master->dummy_rx = NULL; | |
1079 | kfree(master->dummy_tx); | |
1080 | master->dummy_tx = NULL; | |
b0b36b86 BF |
1081 | if (master->unprepare_transfer_hardware && |
1082 | master->unprepare_transfer_hardware(master)) | |
1083 | dev_err(&master->dev, | |
1084 | "failed to unprepare transfer hardware\n"); | |
49834de2 MB |
1085 | if (master->auto_runtime_pm) { |
1086 | pm_runtime_mark_last_busy(master->dev.parent); | |
1087 | pm_runtime_put_autosuspend(master->dev.parent); | |
1088 | } | |
56ec1978 | 1089 | trace_spi_master_idle(master); |
ffbbdd21 | 1090 | |
0461a414 MB |
1091 | spin_lock_irqsave(&master->queue_lock, flags); |
1092 | master->idling = false; | |
ffbbdd21 LW |
1093 | spin_unlock_irqrestore(&master->queue_lock, flags); |
1094 | return; | |
1095 | } | |
ffbbdd21 | 1096 | |
ffbbdd21 LW |
1097 | /* Extract head of queue */ |
1098 | master->cur_msg = | |
a89e2d27 | 1099 | list_first_entry(&master->queue, struct spi_message, queue); |
ffbbdd21 LW |
1100 | |
1101 | list_del_init(&master->cur_msg->queue); | |
1102 | if (master->busy) | |
1103 | was_busy = true; | |
1104 | else | |
1105 | master->busy = true; | |
1106 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1107 | ||
49834de2 MB |
1108 | if (!was_busy && master->auto_runtime_pm) { |
1109 | ret = pm_runtime_get_sync(master->dev.parent); | |
1110 | if (ret < 0) { | |
1111 | dev_err(&master->dev, "Failed to power device: %d\n", | |
1112 | ret); | |
1113 | return; | |
1114 | } | |
1115 | } | |
1116 | ||
56ec1978 MB |
1117 | if (!was_busy) |
1118 | trace_spi_master_busy(master); | |
1119 | ||
7dfd2bd7 | 1120 | if (!was_busy && master->prepare_transfer_hardware) { |
ffbbdd21 LW |
1121 | ret = master->prepare_transfer_hardware(master); |
1122 | if (ret) { | |
1123 | dev_err(&master->dev, | |
1124 | "failed to prepare transfer hardware\n"); | |
49834de2 MB |
1125 | |
1126 | if (master->auto_runtime_pm) | |
1127 | pm_runtime_put(master->dev.parent); | |
ffbbdd21 LW |
1128 | return; |
1129 | } | |
1130 | } | |
1131 | ||
56ec1978 MB |
1132 | trace_spi_message_start(master->cur_msg); |
1133 | ||
2841a5fc MB |
1134 | if (master->prepare_message) { |
1135 | ret = master->prepare_message(master, master->cur_msg); | |
1136 | if (ret) { | |
1137 | dev_err(&master->dev, | |
1138 | "failed to prepare message: %d\n", ret); | |
1139 | master->cur_msg->status = ret; | |
1140 | spi_finalize_current_message(master); | |
1141 | return; | |
1142 | } | |
1143 | master->cur_msg_prepared = true; | |
1144 | } | |
1145 | ||
99adef31 MB |
1146 | ret = spi_map_msg(master, master->cur_msg); |
1147 | if (ret) { | |
1148 | master->cur_msg->status = ret; | |
1149 | spi_finalize_current_message(master); | |
1150 | return; | |
1151 | } | |
1152 | ||
ffbbdd21 LW |
1153 | ret = master->transfer_one_message(master, master->cur_msg); |
1154 | if (ret) { | |
1155 | dev_err(&master->dev, | |
1f802f82 | 1156 | "failed to transfer one message from queue\n"); |
ffbbdd21 LW |
1157 | return; |
1158 | } | |
1159 | } | |
1160 | ||
fc9e0f71 MB |
1161 | /** |
1162 | * spi_pump_messages - kthread work function which processes spi message queue | |
1163 | * @work: pointer to kthread work struct contained in the master struct | |
1164 | */ | |
1165 | static void spi_pump_messages(struct kthread_work *work) | |
1166 | { | |
1167 | struct spi_master *master = | |
1168 | container_of(work, struct spi_master, pump_messages); | |
1169 | ||
1170 | __spi_pump_messages(master, true); | |
1171 | } | |
1172 | ||
ffbbdd21 LW |
1173 | static int spi_init_queue(struct spi_master *master) |
1174 | { | |
1175 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; | |
1176 | ||
ffbbdd21 LW |
1177 | master->running = false; |
1178 | master->busy = false; | |
1179 | ||
1180 | init_kthread_worker(&master->kworker); | |
1181 | master->kworker_task = kthread_run(kthread_worker_fn, | |
f170168b | 1182 | &master->kworker, "%s", |
ffbbdd21 LW |
1183 | dev_name(&master->dev)); |
1184 | if (IS_ERR(master->kworker_task)) { | |
1185 | dev_err(&master->dev, "failed to create message pump task\n"); | |
98a8f5a0 | 1186 | return PTR_ERR(master->kworker_task); |
ffbbdd21 LW |
1187 | } |
1188 | init_kthread_work(&master->pump_messages, spi_pump_messages); | |
1189 | ||
1190 | /* | |
1191 | * Master config will indicate if this controller should run the | |
1192 | * message pump with high (realtime) priority to reduce the transfer | |
1193 | * latency on the bus by minimising the delay between a transfer | |
1194 | * request and the scheduling of the message pump thread. Without this | |
1195 | * setting the message pump thread will remain at default priority. | |
1196 | */ | |
1197 | if (master->rt) { | |
1198 | dev_info(&master->dev, | |
1199 | "will run message pump with realtime priority\n"); | |
1200 | sched_setscheduler(master->kworker_task, SCHED_FIFO, ¶m); | |
1201 | } | |
1202 | ||
1203 | return 0; | |
1204 | } | |
1205 | ||
1206 | /** | |
1207 | * spi_get_next_queued_message() - called by driver to check for queued | |
1208 | * messages | |
1209 | * @master: the master to check for queued messages | |
1210 | * | |
1211 | * If there are more messages in the queue, the next message is returned from | |
1212 | * this call. | |
1213 | */ | |
1214 | struct spi_message *spi_get_next_queued_message(struct spi_master *master) | |
1215 | { | |
1216 | struct spi_message *next; | |
1217 | unsigned long flags; | |
1218 | ||
1219 | /* get a pointer to the next message, if any */ | |
1220 | spin_lock_irqsave(&master->queue_lock, flags); | |
1cfd97f9 AL |
1221 | next = list_first_entry_or_null(&master->queue, struct spi_message, |
1222 | queue); | |
ffbbdd21 LW |
1223 | spin_unlock_irqrestore(&master->queue_lock, flags); |
1224 | ||
1225 | return next; | |
1226 | } | |
1227 | EXPORT_SYMBOL_GPL(spi_get_next_queued_message); | |
1228 | ||
1229 | /** | |
1230 | * spi_finalize_current_message() - the current message is complete | |
1231 | * @master: the master to return the message to | |
1232 | * | |
1233 | * Called by the driver to notify the core that the message in the front of the | |
1234 | * queue is complete and can be removed from the queue. | |
1235 | */ | |
1236 | void spi_finalize_current_message(struct spi_master *master) | |
1237 | { | |
1238 | struct spi_message *mesg; | |
1239 | unsigned long flags; | |
2841a5fc | 1240 | int ret; |
ffbbdd21 LW |
1241 | |
1242 | spin_lock_irqsave(&master->queue_lock, flags); | |
1243 | mesg = master->cur_msg; | |
ffbbdd21 LW |
1244 | spin_unlock_irqrestore(&master->queue_lock, flags); |
1245 | ||
99adef31 MB |
1246 | spi_unmap_msg(master, mesg); |
1247 | ||
2841a5fc MB |
1248 | if (master->cur_msg_prepared && master->unprepare_message) { |
1249 | ret = master->unprepare_message(master, mesg); | |
1250 | if (ret) { | |
1251 | dev_err(&master->dev, | |
1252 | "failed to unprepare message: %d\n", ret); | |
1253 | } | |
1254 | } | |
391949b6 | 1255 | |
8e76ef88 MS |
1256 | spin_lock_irqsave(&master->queue_lock, flags); |
1257 | master->cur_msg = NULL; | |
2841a5fc | 1258 | master->cur_msg_prepared = false; |
8e76ef88 MS |
1259 | queue_kthread_work(&master->kworker, &master->pump_messages); |
1260 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1261 | ||
1262 | trace_spi_message_done(mesg); | |
2841a5fc | 1263 | |
ffbbdd21 LW |
1264 | mesg->state = NULL; |
1265 | if (mesg->complete) | |
1266 | mesg->complete(mesg->context); | |
1267 | } | |
1268 | EXPORT_SYMBOL_GPL(spi_finalize_current_message); | |
1269 | ||
1270 | static int spi_start_queue(struct spi_master *master) | |
1271 | { | |
1272 | unsigned long flags; | |
1273 | ||
1274 | spin_lock_irqsave(&master->queue_lock, flags); | |
1275 | ||
1276 | if (master->running || master->busy) { | |
1277 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1278 | return -EBUSY; | |
1279 | } | |
1280 | ||
1281 | master->running = true; | |
1282 | master->cur_msg = NULL; | |
1283 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1284 | ||
1285 | queue_kthread_work(&master->kworker, &master->pump_messages); | |
1286 | ||
1287 | return 0; | |
1288 | } | |
1289 | ||
1290 | static int spi_stop_queue(struct spi_master *master) | |
1291 | { | |
1292 | unsigned long flags; | |
1293 | unsigned limit = 500; | |
1294 | int ret = 0; | |
1295 | ||
1296 | spin_lock_irqsave(&master->queue_lock, flags); | |
1297 | ||
1298 | /* | |
1299 | * This is a bit lame, but is optimized for the common execution path. | |
1300 | * A wait_queue on the master->busy could be used, but then the common | |
1301 | * execution path (pump_messages) would be required to call wake_up or | |
1302 | * friends on every SPI message. Do this instead. | |
1303 | */ | |
1304 | while ((!list_empty(&master->queue) || master->busy) && limit--) { | |
1305 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
f97b26b0 | 1306 | usleep_range(10000, 11000); |
ffbbdd21 LW |
1307 | spin_lock_irqsave(&master->queue_lock, flags); |
1308 | } | |
1309 | ||
1310 | if (!list_empty(&master->queue) || master->busy) | |
1311 | ret = -EBUSY; | |
1312 | else | |
1313 | master->running = false; | |
1314 | ||
1315 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1316 | ||
1317 | if (ret) { | |
1318 | dev_warn(&master->dev, | |
1319 | "could not stop message queue\n"); | |
1320 | return ret; | |
1321 | } | |
1322 | return ret; | |
1323 | } | |
1324 | ||
1325 | static int spi_destroy_queue(struct spi_master *master) | |
1326 | { | |
1327 | int ret; | |
1328 | ||
1329 | ret = spi_stop_queue(master); | |
1330 | ||
1331 | /* | |
1332 | * flush_kthread_worker will block until all work is done. | |
1333 | * If the reason that stop_queue timed out is that the work will never | |
1334 | * finish, then it does no good to call flush/stop thread, so | |
1335 | * return anyway. | |
1336 | */ | |
1337 | if (ret) { | |
1338 | dev_err(&master->dev, "problem destroying queue\n"); | |
1339 | return ret; | |
1340 | } | |
1341 | ||
1342 | flush_kthread_worker(&master->kworker); | |
1343 | kthread_stop(master->kworker_task); | |
1344 | ||
1345 | return 0; | |
1346 | } | |
1347 | ||
0461a414 MB |
1348 | static int __spi_queued_transfer(struct spi_device *spi, |
1349 | struct spi_message *msg, | |
1350 | bool need_pump) | |
ffbbdd21 LW |
1351 | { |
1352 | struct spi_master *master = spi->master; | |
1353 | unsigned long flags; | |
1354 | ||
1355 | spin_lock_irqsave(&master->queue_lock, flags); | |
1356 | ||
1357 | if (!master->running) { | |
1358 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1359 | return -ESHUTDOWN; | |
1360 | } | |
1361 | msg->actual_length = 0; | |
1362 | msg->status = -EINPROGRESS; | |
1363 | ||
1364 | list_add_tail(&msg->queue, &master->queue); | |
0461a414 | 1365 | if (!master->busy && need_pump) |
ffbbdd21 LW |
1366 | queue_kthread_work(&master->kworker, &master->pump_messages); |
1367 | ||
1368 | spin_unlock_irqrestore(&master->queue_lock, flags); | |
1369 | return 0; | |
1370 | } | |
1371 | ||
0461a414 MB |
1372 | /** |
1373 | * spi_queued_transfer - transfer function for queued transfers | |
1374 | * @spi: spi device which is requesting transfer | |
1375 | * @msg: spi message which is to handled is queued to driver queue | |
1376 | */ | |
1377 | static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) | |
1378 | { | |
1379 | return __spi_queued_transfer(spi, msg, true); | |
1380 | } | |
1381 | ||
ffbbdd21 LW |
1382 | static int spi_master_initialize_queue(struct spi_master *master) |
1383 | { | |
1384 | int ret; | |
1385 | ||
ffbbdd21 | 1386 | master->transfer = spi_queued_transfer; |
b158935f MB |
1387 | if (!master->transfer_one_message) |
1388 | master->transfer_one_message = spi_transfer_one_message; | |
ffbbdd21 LW |
1389 | |
1390 | /* Initialize and start queue */ | |
1391 | ret = spi_init_queue(master); | |
1392 | if (ret) { | |
1393 | dev_err(&master->dev, "problem initializing queue\n"); | |
1394 | goto err_init_queue; | |
1395 | } | |
c3676d5c | 1396 | master->queued = true; |
ffbbdd21 LW |
1397 | ret = spi_start_queue(master); |
1398 | if (ret) { | |
1399 | dev_err(&master->dev, "problem starting queue\n"); | |
1400 | goto err_start_queue; | |
1401 | } | |
1402 | ||
1403 | return 0; | |
1404 | ||
1405 | err_start_queue: | |
ffbbdd21 | 1406 | spi_destroy_queue(master); |
c3676d5c | 1407 | err_init_queue: |
ffbbdd21 LW |
1408 | return ret; |
1409 | } | |
1410 | ||
1411 | /*-------------------------------------------------------------------------*/ | |
1412 | ||
7cb94361 | 1413 | #if defined(CONFIG_OF) |
aff5e3f8 PA |
1414 | static struct spi_device * |
1415 | of_register_spi_device(struct spi_master *master, struct device_node *nc) | |
1416 | { | |
1417 | struct spi_device *spi; | |
1418 | int rc; | |
1419 | u32 value; | |
1420 | ||
1421 | /* Alloc an spi_device */ | |
1422 | spi = spi_alloc_device(master); | |
1423 | if (!spi) { | |
1424 | dev_err(&master->dev, "spi_device alloc error for %s\n", | |
1425 | nc->full_name); | |
1426 | rc = -ENOMEM; | |
1427 | goto err_out; | |
1428 | } | |
1429 | ||
1430 | /* Select device driver */ | |
1431 | rc = of_modalias_node(nc, spi->modalias, | |
1432 | sizeof(spi->modalias)); | |
1433 | if (rc < 0) { | |
1434 | dev_err(&master->dev, "cannot find modalias for %s\n", | |
1435 | nc->full_name); | |
1436 | goto err_out; | |
1437 | } | |
1438 | ||
1439 | /* Device address */ | |
1440 | rc = of_property_read_u32(nc, "reg", &value); | |
1441 | if (rc) { | |
1442 | dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n", | |
1443 | nc->full_name, rc); | |
1444 | goto err_out; | |
1445 | } | |
1446 | spi->chip_select = value; | |
1447 | ||
1448 | /* Mode (clock phase/polarity/etc.) */ | |
1449 | if (of_find_property(nc, "spi-cpha", NULL)) | |
1450 | spi->mode |= SPI_CPHA; | |
1451 | if (of_find_property(nc, "spi-cpol", NULL)) | |
1452 | spi->mode |= SPI_CPOL; | |
1453 | if (of_find_property(nc, "spi-cs-high", NULL)) | |
1454 | spi->mode |= SPI_CS_HIGH; | |
1455 | if (of_find_property(nc, "spi-3wire", NULL)) | |
1456 | spi->mode |= SPI_3WIRE; | |
1457 | if (of_find_property(nc, "spi-lsb-first", NULL)) | |
1458 | spi->mode |= SPI_LSB_FIRST; | |
1459 | ||
1460 | /* Device DUAL/QUAD mode */ | |
1461 | if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { | |
1462 | switch (value) { | |
1463 | case 1: | |
1464 | break; | |
1465 | case 2: | |
1466 | spi->mode |= SPI_TX_DUAL; | |
1467 | break; | |
1468 | case 4: | |
1469 | spi->mode |= SPI_TX_QUAD; | |
1470 | break; | |
1471 | default: | |
1472 | dev_warn(&master->dev, | |
1473 | "spi-tx-bus-width %d not supported\n", | |
1474 | value); | |
1475 | break; | |
1476 | } | |
1477 | } | |
1478 | ||
1479 | if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { | |
1480 | switch (value) { | |
1481 | case 1: | |
1482 | break; | |
1483 | case 2: | |
1484 | spi->mode |= SPI_RX_DUAL; | |
1485 | break; | |
1486 | case 4: | |
1487 | spi->mode |= SPI_RX_QUAD; | |
1488 | break; | |
1489 | default: | |
1490 | dev_warn(&master->dev, | |
1491 | "spi-rx-bus-width %d not supported\n", | |
1492 | value); | |
1493 | break; | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | /* Device speed */ | |
1498 | rc = of_property_read_u32(nc, "spi-max-frequency", &value); | |
1499 | if (rc) { | |
1500 | dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n", | |
1501 | nc->full_name, rc); | |
1502 | goto err_out; | |
1503 | } | |
1504 | spi->max_speed_hz = value; | |
1505 | ||
aff5e3f8 PA |
1506 | /* Store a pointer to the node in the device structure */ |
1507 | of_node_get(nc); | |
1508 | spi->dev.of_node = nc; | |
1509 | ||
1510 | /* Register the new device */ | |
aff5e3f8 PA |
1511 | rc = spi_add_device(spi); |
1512 | if (rc) { | |
1513 | dev_err(&master->dev, "spi_device register error %s\n", | |
1514 | nc->full_name); | |
1515 | goto err_out; | |
1516 | } | |
1517 | ||
1518 | return spi; | |
1519 | ||
1520 | err_out: | |
1521 | spi_dev_put(spi); | |
1522 | return ERR_PTR(rc); | |
1523 | } | |
1524 | ||
d57a4282 GL |
1525 | /** |
1526 | * of_register_spi_devices() - Register child devices onto the SPI bus | |
1527 | * @master: Pointer to spi_master device | |
1528 | * | |
1529 | * Registers an spi_device for each child node of master node which has a 'reg' | |
1530 | * property. | |
1531 | */ | |
1532 | static void of_register_spi_devices(struct spi_master *master) | |
1533 | { | |
1534 | struct spi_device *spi; | |
1535 | struct device_node *nc; | |
d57a4282 GL |
1536 | |
1537 | if (!master->dev.of_node) | |
1538 | return; | |
1539 | ||
f3b6159e | 1540 | for_each_available_child_of_node(master->dev.of_node, nc) { |
aff5e3f8 PA |
1541 | spi = of_register_spi_device(master, nc); |
1542 | if (IS_ERR(spi)) | |
1543 | dev_warn(&master->dev, "Failed to create SPI device for %s\n", | |
d57a4282 | 1544 | nc->full_name); |
d57a4282 GL |
1545 | } |
1546 | } | |
1547 | #else | |
1548 | static void of_register_spi_devices(struct spi_master *master) { } | |
1549 | #endif | |
1550 | ||
64bee4d2 MW |
1551 | #ifdef CONFIG_ACPI |
1552 | static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) | |
1553 | { | |
1554 | struct spi_device *spi = data; | |
1555 | ||
1556 | if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { | |
1557 | struct acpi_resource_spi_serialbus *sb; | |
1558 | ||
1559 | sb = &ares->data.spi_serial_bus; | |
1560 | if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { | |
1561 | spi->chip_select = sb->device_selection; | |
1562 | spi->max_speed_hz = sb->connection_speed; | |
1563 | ||
1564 | if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) | |
1565 | spi->mode |= SPI_CPHA; | |
1566 | if (sb->clock_polarity == ACPI_SPI_START_HIGH) | |
1567 | spi->mode |= SPI_CPOL; | |
1568 | if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) | |
1569 | spi->mode |= SPI_CS_HIGH; | |
1570 | } | |
1571 | } else if (spi->irq < 0) { | |
1572 | struct resource r; | |
1573 | ||
1574 | if (acpi_dev_resource_interrupt(ares, 0, &r)) | |
1575 | spi->irq = r.start; | |
1576 | } | |
1577 | ||
1578 | /* Always tell the ACPI core to skip this resource */ | |
1579 | return 1; | |
1580 | } | |
1581 | ||
1582 | static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, | |
1583 | void *data, void **return_value) | |
1584 | { | |
1585 | struct spi_master *master = data; | |
1586 | struct list_head resource_list; | |
1587 | struct acpi_device *adev; | |
1588 | struct spi_device *spi; | |
1589 | int ret; | |
1590 | ||
1591 | if (acpi_bus_get_device(handle, &adev)) | |
1592 | return AE_OK; | |
1593 | if (acpi_bus_get_status(adev) || !adev->status.present) | |
1594 | return AE_OK; | |
1595 | ||
1596 | spi = spi_alloc_device(master); | |
1597 | if (!spi) { | |
1598 | dev_err(&master->dev, "failed to allocate SPI device for %s\n", | |
1599 | dev_name(&adev->dev)); | |
1600 | return AE_NO_MEMORY; | |
1601 | } | |
1602 | ||
7b199811 | 1603 | ACPI_COMPANION_SET(&spi->dev, adev); |
64bee4d2 MW |
1604 | spi->irq = -1; |
1605 | ||
1606 | INIT_LIST_HEAD(&resource_list); | |
1607 | ret = acpi_dev_get_resources(adev, &resource_list, | |
1608 | acpi_spi_add_resource, spi); | |
1609 | acpi_dev_free_resource_list(&resource_list); | |
1610 | ||
1611 | if (ret < 0 || !spi->max_speed_hz) { | |
1612 | spi_dev_put(spi); | |
1613 | return AE_OK; | |
1614 | } | |
1615 | ||
33cf00e5 | 1616 | adev->power.flags.ignore_parent = true; |
cf9eb39c | 1617 | strlcpy(spi->modalias, acpi_device_hid(adev), sizeof(spi->modalias)); |
64bee4d2 | 1618 | if (spi_add_device(spi)) { |
33cf00e5 | 1619 | adev->power.flags.ignore_parent = false; |
64bee4d2 MW |
1620 | dev_err(&master->dev, "failed to add SPI device %s from ACPI\n", |
1621 | dev_name(&adev->dev)); | |
1622 | spi_dev_put(spi); | |
1623 | } | |
1624 | ||
1625 | return AE_OK; | |
1626 | } | |
1627 | ||
1628 | static void acpi_register_spi_devices(struct spi_master *master) | |
1629 | { | |
1630 | acpi_status status; | |
1631 | acpi_handle handle; | |
1632 | ||
29896178 | 1633 | handle = ACPI_HANDLE(master->dev.parent); |
64bee4d2 MW |
1634 | if (!handle) |
1635 | return; | |
1636 | ||
1637 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, | |
1638 | acpi_spi_add_device, NULL, | |
1639 | master, NULL); | |
1640 | if (ACPI_FAILURE(status)) | |
1641 | dev_warn(&master->dev, "failed to enumerate SPI slaves\n"); | |
1642 | } | |
1643 | #else | |
1644 | static inline void acpi_register_spi_devices(struct spi_master *master) {} | |
1645 | #endif /* CONFIG_ACPI */ | |
1646 | ||
49dce689 | 1647 | static void spi_master_release(struct device *dev) |
8ae12a0d DB |
1648 | { |
1649 | struct spi_master *master; | |
1650 | ||
49dce689 | 1651 | master = container_of(dev, struct spi_master, dev); |
8ae12a0d DB |
1652 | kfree(master); |
1653 | } | |
1654 | ||
1655 | static struct class spi_master_class = { | |
1656 | .name = "spi_master", | |
1657 | .owner = THIS_MODULE, | |
49dce689 | 1658 | .dev_release = spi_master_release, |
eca2ebc7 | 1659 | .dev_groups = spi_master_groups, |
8ae12a0d DB |
1660 | }; |
1661 | ||
1662 | ||
1663 | /** | |
1664 | * spi_alloc_master - allocate SPI master controller | |
1665 | * @dev: the controller, possibly using the platform_bus | |
33e34dc6 | 1666 | * @size: how much zeroed driver-private data to allocate; the pointer to this |
49dce689 | 1667 | * memory is in the driver_data field of the returned device, |
0c868461 | 1668 | * accessible with spi_master_get_devdata(). |
33e34dc6 | 1669 | * Context: can sleep |
8ae12a0d DB |
1670 | * |
1671 | * This call is used only by SPI master controller drivers, which are the | |
1672 | * only ones directly touching chip registers. It's how they allocate | |
ba1a0513 | 1673 | * an spi_master structure, prior to calling spi_register_master(). |
8ae12a0d DB |
1674 | * |
1675 | * This must be called from context that can sleep. It returns the SPI | |
1676 | * master structure on success, else NULL. | |
1677 | * | |
1678 | * The caller is responsible for assigning the bus number and initializing | |
ba1a0513 | 1679 | * the master's methods before calling spi_register_master(); and (after errors |
a394d635 | 1680 | * adding the device) calling spi_master_put() to prevent a memory leak. |
8ae12a0d | 1681 | */ |
e9d5a461 | 1682 | struct spi_master *spi_alloc_master(struct device *dev, unsigned size) |
8ae12a0d DB |
1683 | { |
1684 | struct spi_master *master; | |
1685 | ||
0c868461 DB |
1686 | if (!dev) |
1687 | return NULL; | |
1688 | ||
5fe5f05e | 1689 | master = kzalloc(size + sizeof(*master), GFP_KERNEL); |
8ae12a0d DB |
1690 | if (!master) |
1691 | return NULL; | |
1692 | ||
49dce689 | 1693 | device_initialize(&master->dev); |
1e8a52e1 GL |
1694 | master->bus_num = -1; |
1695 | master->num_chipselect = 1; | |
49dce689 TJ |
1696 | master->dev.class = &spi_master_class; |
1697 | master->dev.parent = get_device(dev); | |
0c868461 | 1698 | spi_master_set_devdata(master, &master[1]); |
8ae12a0d DB |
1699 | |
1700 | return master; | |
1701 | } | |
1702 | EXPORT_SYMBOL_GPL(spi_alloc_master); | |
1703 | ||
74317984 JCPV |
1704 | #ifdef CONFIG_OF |
1705 | static int of_spi_register_master(struct spi_master *master) | |
1706 | { | |
e80beb27 | 1707 | int nb, i, *cs; |
74317984 JCPV |
1708 | struct device_node *np = master->dev.of_node; |
1709 | ||
1710 | if (!np) | |
1711 | return 0; | |
1712 | ||
1713 | nb = of_gpio_named_count(np, "cs-gpios"); | |
5fe5f05e | 1714 | master->num_chipselect = max_t(int, nb, master->num_chipselect); |
74317984 | 1715 | |
8ec5d84e AL |
1716 | /* Return error only for an incorrectly formed cs-gpios property */ |
1717 | if (nb == 0 || nb == -ENOENT) | |
74317984 | 1718 | return 0; |
8ec5d84e AL |
1719 | else if (nb < 0) |
1720 | return nb; | |
74317984 JCPV |
1721 | |
1722 | cs = devm_kzalloc(&master->dev, | |
1723 | sizeof(int) * master->num_chipselect, | |
1724 | GFP_KERNEL); | |
1725 | master->cs_gpios = cs; | |
1726 | ||
1727 | if (!master->cs_gpios) | |
1728 | return -ENOMEM; | |
1729 | ||
0da83bb1 | 1730 | for (i = 0; i < master->num_chipselect; i++) |
446411e1 | 1731 | cs[i] = -ENOENT; |
74317984 JCPV |
1732 | |
1733 | for (i = 0; i < nb; i++) | |
1734 | cs[i] = of_get_named_gpio(np, "cs-gpios", i); | |
1735 | ||
1736 | return 0; | |
1737 | } | |
1738 | #else | |
1739 | static int of_spi_register_master(struct spi_master *master) | |
1740 | { | |
1741 | return 0; | |
1742 | } | |
1743 | #endif | |
1744 | ||
8ae12a0d DB |
1745 | /** |
1746 | * spi_register_master - register SPI master controller | |
1747 | * @master: initialized master, originally from spi_alloc_master() | |
33e34dc6 | 1748 | * Context: can sleep |
8ae12a0d DB |
1749 | * |
1750 | * SPI master controllers connect to their drivers using some non-SPI bus, | |
1751 | * such as the platform bus. The final stage of probe() in that code | |
1752 | * includes calling spi_register_master() to hook up to this SPI bus glue. | |
1753 | * | |
1754 | * SPI controllers use board specific (often SOC specific) bus numbers, | |
1755 | * and board-specific addressing for SPI devices combines those numbers | |
1756 | * with chip select numbers. Since SPI does not directly support dynamic | |
1757 | * device identification, boards need configuration tables telling which | |
1758 | * chip is at which address. | |
1759 | * | |
1760 | * This must be called from context that can sleep. It returns zero on | |
1761 | * success, else a negative error code (dropping the master's refcount). | |
0c868461 DB |
1762 | * After a successful return, the caller is responsible for calling |
1763 | * spi_unregister_master(). | |
8ae12a0d | 1764 | */ |
e9d5a461 | 1765 | int spi_register_master(struct spi_master *master) |
8ae12a0d | 1766 | { |
e44a45ae | 1767 | static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1); |
49dce689 | 1768 | struct device *dev = master->dev.parent; |
2b9603a0 | 1769 | struct boardinfo *bi; |
8ae12a0d DB |
1770 | int status = -ENODEV; |
1771 | int dynamic = 0; | |
1772 | ||
0c868461 DB |
1773 | if (!dev) |
1774 | return -ENODEV; | |
1775 | ||
74317984 JCPV |
1776 | status = of_spi_register_master(master); |
1777 | if (status) | |
1778 | return status; | |
1779 | ||
082c8cb4 DB |
1780 | /* even if it's just one always-selected device, there must |
1781 | * be at least one chipselect | |
1782 | */ | |
1783 | if (master->num_chipselect == 0) | |
1784 | return -EINVAL; | |
1785 | ||
bb29785e GL |
1786 | if ((master->bus_num < 0) && master->dev.of_node) |
1787 | master->bus_num = of_alias_get_id(master->dev.of_node, "spi"); | |
1788 | ||
8ae12a0d | 1789 | /* convention: dynamically assigned bus IDs count down from the max */ |
a020ed75 | 1790 | if (master->bus_num < 0) { |
082c8cb4 DB |
1791 | /* FIXME switch to an IDR based scheme, something like |
1792 | * I2C now uses, so we can't run out of "dynamic" IDs | |
1793 | */ | |
8ae12a0d | 1794 | master->bus_num = atomic_dec_return(&dyn_bus_id); |
b885244e | 1795 | dynamic = 1; |
8ae12a0d DB |
1796 | } |
1797 | ||
5424d43e MB |
1798 | INIT_LIST_HEAD(&master->queue); |
1799 | spin_lock_init(&master->queue_lock); | |
cf32b71e ES |
1800 | spin_lock_init(&master->bus_lock_spinlock); |
1801 | mutex_init(&master->bus_lock_mutex); | |
1802 | master->bus_lock_flag = 0; | |
b158935f | 1803 | init_completion(&master->xfer_completion); |
6ad45a27 MB |
1804 | if (!master->max_dma_len) |
1805 | master->max_dma_len = INT_MAX; | |
cf32b71e | 1806 | |
8ae12a0d DB |
1807 | /* register the device, then userspace will see it. |
1808 | * registration fails if the bus ID is in use. | |
1809 | */ | |
35f74fca | 1810 | dev_set_name(&master->dev, "spi%u", master->bus_num); |
49dce689 | 1811 | status = device_add(&master->dev); |
b885244e | 1812 | if (status < 0) |
8ae12a0d | 1813 | goto done; |
35f74fca | 1814 | dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev), |
8ae12a0d DB |
1815 | dynamic ? " (dynamic)" : ""); |
1816 | ||
ffbbdd21 LW |
1817 | /* If we're using a queued driver, start the queue */ |
1818 | if (master->transfer) | |
1819 | dev_info(dev, "master is unqueued, this is deprecated\n"); | |
1820 | else { | |
1821 | status = spi_master_initialize_queue(master); | |
1822 | if (status) { | |
e93b0724 | 1823 | device_del(&master->dev); |
ffbbdd21 LW |
1824 | goto done; |
1825 | } | |
1826 | } | |
eca2ebc7 MS |
1827 | /* add statistics */ |
1828 | spin_lock_init(&master->statistics.lock); | |
ffbbdd21 | 1829 | |
2b9603a0 FT |
1830 | mutex_lock(&board_lock); |
1831 | list_add_tail(&master->list, &spi_master_list); | |
1832 | list_for_each_entry(bi, &board_list, list) | |
1833 | spi_match_master_to_boardinfo(master, &bi->board_info); | |
1834 | mutex_unlock(&board_lock); | |
1835 | ||
64bee4d2 | 1836 | /* Register devices from the device tree and ACPI */ |
12b15e83 | 1837 | of_register_spi_devices(master); |
64bee4d2 | 1838 | acpi_register_spi_devices(master); |
8ae12a0d DB |
1839 | done: |
1840 | return status; | |
1841 | } | |
1842 | EXPORT_SYMBOL_GPL(spi_register_master); | |
1843 | ||
666d5b4c MB |
1844 | static void devm_spi_unregister(struct device *dev, void *res) |
1845 | { | |
1846 | spi_unregister_master(*(struct spi_master **)res); | |
1847 | } | |
1848 | ||
1849 | /** | |
1850 | * dev_spi_register_master - register managed SPI master controller | |
1851 | * @dev: device managing SPI master | |
1852 | * @master: initialized master, originally from spi_alloc_master() | |
1853 | * Context: can sleep | |
1854 | * | |
1855 | * Register a SPI device as with spi_register_master() which will | |
1856 | * automatically be unregister | |
1857 | */ | |
1858 | int devm_spi_register_master(struct device *dev, struct spi_master *master) | |
1859 | { | |
1860 | struct spi_master **ptr; | |
1861 | int ret; | |
1862 | ||
1863 | ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); | |
1864 | if (!ptr) | |
1865 | return -ENOMEM; | |
1866 | ||
1867 | ret = spi_register_master(master); | |
4b92894e | 1868 | if (!ret) { |
666d5b4c MB |
1869 | *ptr = master; |
1870 | devres_add(dev, ptr); | |
1871 | } else { | |
1872 | devres_free(ptr); | |
1873 | } | |
1874 | ||
1875 | return ret; | |
1876 | } | |
1877 | EXPORT_SYMBOL_GPL(devm_spi_register_master); | |
1878 | ||
34860089 | 1879 | static int __unregister(struct device *dev, void *null) |
8ae12a0d | 1880 | { |
34860089 | 1881 | spi_unregister_device(to_spi_device(dev)); |
8ae12a0d DB |
1882 | return 0; |
1883 | } | |
1884 | ||
1885 | /** | |
1886 | * spi_unregister_master - unregister SPI master controller | |
1887 | * @master: the master being unregistered | |
33e34dc6 | 1888 | * Context: can sleep |
8ae12a0d DB |
1889 | * |
1890 | * This call is used only by SPI master controller drivers, which are the | |
1891 | * only ones directly touching chip registers. | |
1892 | * | |
1893 | * This must be called from context that can sleep. | |
1894 | */ | |
1895 | void spi_unregister_master(struct spi_master *master) | |
1896 | { | |
89fc9a1a JG |
1897 | int dummy; |
1898 | ||
ffbbdd21 LW |
1899 | if (master->queued) { |
1900 | if (spi_destroy_queue(master)) | |
1901 | dev_err(&master->dev, "queue remove failed\n"); | |
1902 | } | |
1903 | ||
2b9603a0 FT |
1904 | mutex_lock(&board_lock); |
1905 | list_del(&master->list); | |
1906 | mutex_unlock(&board_lock); | |
1907 | ||
97dbf37d | 1908 | dummy = device_for_each_child(&master->dev, NULL, __unregister); |
49dce689 | 1909 | device_unregister(&master->dev); |
8ae12a0d DB |
1910 | } |
1911 | EXPORT_SYMBOL_GPL(spi_unregister_master); | |
1912 | ||
ffbbdd21 LW |
1913 | int spi_master_suspend(struct spi_master *master) |
1914 | { | |
1915 | int ret; | |
1916 | ||
1917 | /* Basically no-ops for non-queued masters */ | |
1918 | if (!master->queued) | |
1919 | return 0; | |
1920 | ||
1921 | ret = spi_stop_queue(master); | |
1922 | if (ret) | |
1923 | dev_err(&master->dev, "queue stop failed\n"); | |
1924 | ||
1925 | return ret; | |
1926 | } | |
1927 | EXPORT_SYMBOL_GPL(spi_master_suspend); | |
1928 | ||
1929 | int spi_master_resume(struct spi_master *master) | |
1930 | { | |
1931 | int ret; | |
1932 | ||
1933 | if (!master->queued) | |
1934 | return 0; | |
1935 | ||
1936 | ret = spi_start_queue(master); | |
1937 | if (ret) | |
1938 | dev_err(&master->dev, "queue restart failed\n"); | |
1939 | ||
1940 | return ret; | |
1941 | } | |
1942 | EXPORT_SYMBOL_GPL(spi_master_resume); | |
1943 | ||
9f3b795a | 1944 | static int __spi_master_match(struct device *dev, const void *data) |
5ed2c832 DY |
1945 | { |
1946 | struct spi_master *m; | |
9f3b795a | 1947 | const u16 *bus_num = data; |
5ed2c832 DY |
1948 | |
1949 | m = container_of(dev, struct spi_master, dev); | |
1950 | return m->bus_num == *bus_num; | |
1951 | } | |
1952 | ||
8ae12a0d DB |
1953 | /** |
1954 | * spi_busnum_to_master - look up master associated with bus_num | |
1955 | * @bus_num: the master's bus number | |
33e34dc6 | 1956 | * Context: can sleep |
8ae12a0d DB |
1957 | * |
1958 | * This call may be used with devices that are registered after | |
1959 | * arch init time. It returns a refcounted pointer to the relevant | |
1960 | * spi_master (which the caller must release), or NULL if there is | |
1961 | * no such master registered. | |
1962 | */ | |
1963 | struct spi_master *spi_busnum_to_master(u16 bus_num) | |
1964 | { | |
49dce689 | 1965 | struct device *dev; |
1e9a51dc | 1966 | struct spi_master *master = NULL; |
5ed2c832 | 1967 | |
695794ae | 1968 | dev = class_find_device(&spi_master_class, NULL, &bus_num, |
5ed2c832 DY |
1969 | __spi_master_match); |
1970 | if (dev) | |
1971 | master = container_of(dev, struct spi_master, dev); | |
1972 | /* reference got in class_find_device */ | |
1e9a51dc | 1973 | return master; |
8ae12a0d DB |
1974 | } |
1975 | EXPORT_SYMBOL_GPL(spi_busnum_to_master); | |
1976 | ||
1977 | ||
1978 | /*-------------------------------------------------------------------------*/ | |
1979 | ||
7d077197 DB |
1980 | /* Core methods for SPI master protocol drivers. Some of the |
1981 | * other core methods are currently defined as inline functions. | |
1982 | */ | |
1983 | ||
63ab645f SB |
1984 | static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word) |
1985 | { | |
1986 | if (master->bits_per_word_mask) { | |
1987 | /* Only 32 bits fit in the mask */ | |
1988 | if (bits_per_word > 32) | |
1989 | return -EINVAL; | |
1990 | if (!(master->bits_per_word_mask & | |
1991 | SPI_BPW_MASK(bits_per_word))) | |
1992 | return -EINVAL; | |
1993 | } | |
1994 | ||
1995 | return 0; | |
1996 | } | |
1997 | ||
7d077197 DB |
1998 | /** |
1999 | * spi_setup - setup SPI mode and clock rate | |
2000 | * @spi: the device whose settings are being modified | |
2001 | * Context: can sleep, and no requests are queued to the device | |
2002 | * | |
2003 | * SPI protocol drivers may need to update the transfer mode if the | |
2004 | * device doesn't work with its default. They may likewise need | |
2005 | * to update clock rates or word sizes from initial values. This function | |
2006 | * changes those settings, and must be called from a context that can sleep. | |
2007 | * Except for SPI_CS_HIGH, which takes effect immediately, the changes take | |
2008 | * effect the next time the device is selected and data is transferred to | |
2009 | * or from it. When this function returns, the spi device is deselected. | |
2010 | * | |
2011 | * Note that this call will fail if the protocol driver specifies an option | |
2012 | * that the underlying controller or its driver does not support. For | |
2013 | * example, not all hardware supports wire transfers using nine bit words, | |
2014 | * LSB-first wire encoding, or active-high chipselects. | |
2015 | */ | |
2016 | int spi_setup(struct spi_device *spi) | |
2017 | { | |
83596fbe | 2018 | unsigned bad_bits, ugly_bits; |
5ab8d262 | 2019 | int status; |
7d077197 | 2020 | |
f477b7fb | 2021 | /* check mode to prevent that DUAL and QUAD set at the same time |
2022 | */ | |
2023 | if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || | |
2024 | ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { | |
2025 | dev_err(&spi->dev, | |
2026 | "setup: can not select dual and quad at the same time\n"); | |
2027 | return -EINVAL; | |
2028 | } | |
2029 | /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden | |
2030 | */ | |
2031 | if ((spi->mode & SPI_3WIRE) && (spi->mode & | |
2032 | (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD))) | |
2033 | return -EINVAL; | |
e7db06b5 DB |
2034 | /* help drivers fail *cleanly* when they need options |
2035 | * that aren't supported with their current master | |
2036 | */ | |
2037 | bad_bits = spi->mode & ~spi->master->mode_bits; | |
83596fbe GU |
2038 | ugly_bits = bad_bits & |
2039 | (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD); | |
2040 | if (ugly_bits) { | |
2041 | dev_warn(&spi->dev, | |
2042 | "setup: ignoring unsupported mode bits %x\n", | |
2043 | ugly_bits); | |
2044 | spi->mode &= ~ugly_bits; | |
2045 | bad_bits &= ~ugly_bits; | |
2046 | } | |
e7db06b5 | 2047 | if (bad_bits) { |
eb288a1f | 2048 | dev_err(&spi->dev, "setup: unsupported mode bits %x\n", |
e7db06b5 DB |
2049 | bad_bits); |
2050 | return -EINVAL; | |
2051 | } | |
2052 | ||
7d077197 DB |
2053 | if (!spi->bits_per_word) |
2054 | spi->bits_per_word = 8; | |
2055 | ||
5ab8d262 AS |
2056 | status = __spi_validate_bits_per_word(spi->master, spi->bits_per_word); |
2057 | if (status) | |
2058 | return status; | |
63ab645f | 2059 | |
052eb2d4 AL |
2060 | if (!spi->max_speed_hz) |
2061 | spi->max_speed_hz = spi->master->max_speed_hz; | |
2062 | ||
1a7b7ee7 II |
2063 | spi_set_cs(spi, false); |
2064 | ||
caae070c LD |
2065 | if (spi->master->setup) |
2066 | status = spi->master->setup(spi); | |
7d077197 | 2067 | |
5fe5f05e | 2068 | dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", |
7d077197 DB |
2069 | (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), |
2070 | (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", | |
2071 | (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", | |
2072 | (spi->mode & SPI_3WIRE) ? "3wire, " : "", | |
2073 | (spi->mode & SPI_LOOP) ? "loopback, " : "", | |
2074 | spi->bits_per_word, spi->max_speed_hz, | |
2075 | status); | |
2076 | ||
2077 | return status; | |
2078 | } | |
2079 | EXPORT_SYMBOL_GPL(spi_setup); | |
2080 | ||
90808738 | 2081 | static int __spi_validate(struct spi_device *spi, struct spi_message *message) |
cf32b71e ES |
2082 | { |
2083 | struct spi_master *master = spi->master; | |
e6811d1d | 2084 | struct spi_transfer *xfer; |
6ea31293 | 2085 | int w_size; |
cf32b71e | 2086 | |
24a0013a MB |
2087 | if (list_empty(&message->transfers)) |
2088 | return -EINVAL; | |
24a0013a | 2089 | |
cf32b71e ES |
2090 | /* Half-duplex links include original MicroWire, and ones with |
2091 | * only one data pin like SPI_3WIRE (switches direction) or where | |
2092 | * either MOSI or MISO is missing. They can also be caused by | |
2093 | * software limitations. | |
2094 | */ | |
2095 | if ((master->flags & SPI_MASTER_HALF_DUPLEX) | |
2096 | || (spi->mode & SPI_3WIRE)) { | |
cf32b71e ES |
2097 | unsigned flags = master->flags; |
2098 | ||
2099 | list_for_each_entry(xfer, &message->transfers, transfer_list) { | |
2100 | if (xfer->rx_buf && xfer->tx_buf) | |
2101 | return -EINVAL; | |
2102 | if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf) | |
2103 | return -EINVAL; | |
2104 | if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf) | |
2105 | return -EINVAL; | |
2106 | } | |
2107 | } | |
2108 | ||
e6811d1d | 2109 | /** |
059b8ffe LD |
2110 | * Set transfer bits_per_word and max speed as spi device default if |
2111 | * it is not set for this transfer. | |
f477b7fb | 2112 | * Set transfer tx_nbits and rx_nbits as single transfer default |
2113 | * (SPI_NBITS_SINGLE) if it is not set for this transfer. | |
e6811d1d LD |
2114 | */ |
2115 | list_for_each_entry(xfer, &message->transfers, transfer_list) { | |
078726ce | 2116 | message->frame_length += xfer->len; |
e6811d1d LD |
2117 | if (!xfer->bits_per_word) |
2118 | xfer->bits_per_word = spi->bits_per_word; | |
a6f87fad AL |
2119 | |
2120 | if (!xfer->speed_hz) | |
059b8ffe | 2121 | xfer->speed_hz = spi->max_speed_hz; |
7dc9fbc3 MB |
2122 | if (!xfer->speed_hz) |
2123 | xfer->speed_hz = master->max_speed_hz; | |
a6f87fad AL |
2124 | |
2125 | if (master->max_speed_hz && | |
2126 | xfer->speed_hz > master->max_speed_hz) | |
2127 | xfer->speed_hz = master->max_speed_hz; | |
56ede94a | 2128 | |
63ab645f SB |
2129 | if (__spi_validate_bits_per_word(master, xfer->bits_per_word)) |
2130 | return -EINVAL; | |
a2fd4f9f | 2131 | |
4d94bd21 II |
2132 | /* |
2133 | * SPI transfer length should be multiple of SPI word size | |
2134 | * where SPI word size should be power-of-two multiple | |
2135 | */ | |
2136 | if (xfer->bits_per_word <= 8) | |
2137 | w_size = 1; | |
2138 | else if (xfer->bits_per_word <= 16) | |
2139 | w_size = 2; | |
2140 | else | |
2141 | w_size = 4; | |
2142 | ||
4d94bd21 | 2143 | /* No partial transfers accepted */ |
6ea31293 | 2144 | if (xfer->len % w_size) |
4d94bd21 II |
2145 | return -EINVAL; |
2146 | ||
a2fd4f9f MB |
2147 | if (xfer->speed_hz && master->min_speed_hz && |
2148 | xfer->speed_hz < master->min_speed_hz) | |
2149 | return -EINVAL; | |
f477b7fb | 2150 | |
2151 | if (xfer->tx_buf && !xfer->tx_nbits) | |
2152 | xfer->tx_nbits = SPI_NBITS_SINGLE; | |
2153 | if (xfer->rx_buf && !xfer->rx_nbits) | |
2154 | xfer->rx_nbits = SPI_NBITS_SINGLE; | |
2155 | /* check transfer tx/rx_nbits: | |
1afd9989 GU |
2156 | * 1. check the value matches one of single, dual and quad |
2157 | * 2. check tx/rx_nbits match the mode in spi_device | |
f477b7fb | 2158 | */ |
db90a441 SP |
2159 | if (xfer->tx_buf) { |
2160 | if (xfer->tx_nbits != SPI_NBITS_SINGLE && | |
2161 | xfer->tx_nbits != SPI_NBITS_DUAL && | |
2162 | xfer->tx_nbits != SPI_NBITS_QUAD) | |
2163 | return -EINVAL; | |
2164 | if ((xfer->tx_nbits == SPI_NBITS_DUAL) && | |
2165 | !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) | |
2166 | return -EINVAL; | |
2167 | if ((xfer->tx_nbits == SPI_NBITS_QUAD) && | |
2168 | !(spi->mode & SPI_TX_QUAD)) | |
2169 | return -EINVAL; | |
db90a441 | 2170 | } |
f477b7fb | 2171 | /* check transfer rx_nbits */ |
db90a441 SP |
2172 | if (xfer->rx_buf) { |
2173 | if (xfer->rx_nbits != SPI_NBITS_SINGLE && | |
2174 | xfer->rx_nbits != SPI_NBITS_DUAL && | |
2175 | xfer->rx_nbits != SPI_NBITS_QUAD) | |
2176 | return -EINVAL; | |
2177 | if ((xfer->rx_nbits == SPI_NBITS_DUAL) && | |
2178 | !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) | |
2179 | return -EINVAL; | |
2180 | if ((xfer->rx_nbits == SPI_NBITS_QUAD) && | |
2181 | !(spi->mode & SPI_RX_QUAD)) | |
2182 | return -EINVAL; | |
db90a441 | 2183 | } |
e6811d1d LD |
2184 | } |
2185 | ||
cf32b71e | 2186 | message->status = -EINPROGRESS; |
90808738 MB |
2187 | |
2188 | return 0; | |
2189 | } | |
2190 | ||
2191 | static int __spi_async(struct spi_device *spi, struct spi_message *message) | |
2192 | { | |
2193 | struct spi_master *master = spi->master; | |
2194 | ||
2195 | message->spi = spi; | |
2196 | ||
eca2ebc7 MS |
2197 | SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_async); |
2198 | SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); | |
2199 | ||
90808738 MB |
2200 | trace_spi_message_submit(message); |
2201 | ||
cf32b71e ES |
2202 | return master->transfer(spi, message); |
2203 | } | |
2204 | ||
568d0697 DB |
2205 | /** |
2206 | * spi_async - asynchronous SPI transfer | |
2207 | * @spi: device with which data will be exchanged | |
2208 | * @message: describes the data transfers, including completion callback | |
2209 | * Context: any (irqs may be blocked, etc) | |
2210 | * | |
2211 | * This call may be used in_irq and other contexts which can't sleep, | |
2212 | * as well as from task contexts which can sleep. | |
2213 | * | |
2214 | * The completion callback is invoked in a context which can't sleep. | |
2215 | * Before that invocation, the value of message->status is undefined. | |
2216 | * When the callback is issued, message->status holds either zero (to | |
2217 | * indicate complete success) or a negative error code. After that | |
2218 | * callback returns, the driver which issued the transfer request may | |
2219 | * deallocate the associated memory; it's no longer in use by any SPI | |
2220 | * core or controller driver code. | |
2221 | * | |
2222 | * Note that although all messages to a spi_device are handled in | |
2223 | * FIFO order, messages may go to different devices in other orders. | |
2224 | * Some device might be higher priority, or have various "hard" access | |
2225 | * time requirements, for example. | |
2226 | * | |
2227 | * On detection of any fault during the transfer, processing of | |
2228 | * the entire message is aborted, and the device is deselected. | |
2229 | * Until returning from the associated message completion callback, | |
2230 | * no other spi_message queued to that device will be processed. | |
2231 | * (This rule applies equally to all the synchronous transfer calls, | |
2232 | * which are wrappers around this core asynchronous primitive.) | |
2233 | */ | |
2234 | int spi_async(struct spi_device *spi, struct spi_message *message) | |
2235 | { | |
2236 | struct spi_master *master = spi->master; | |
cf32b71e ES |
2237 | int ret; |
2238 | unsigned long flags; | |
568d0697 | 2239 | |
90808738 MB |
2240 | ret = __spi_validate(spi, message); |
2241 | if (ret != 0) | |
2242 | return ret; | |
2243 | ||
cf32b71e | 2244 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
568d0697 | 2245 | |
cf32b71e ES |
2246 | if (master->bus_lock_flag) |
2247 | ret = -EBUSY; | |
2248 | else | |
2249 | ret = __spi_async(spi, message); | |
568d0697 | 2250 | |
cf32b71e ES |
2251 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); |
2252 | ||
2253 | return ret; | |
568d0697 DB |
2254 | } |
2255 | EXPORT_SYMBOL_GPL(spi_async); | |
2256 | ||
cf32b71e ES |
2257 | /** |
2258 | * spi_async_locked - version of spi_async with exclusive bus usage | |
2259 | * @spi: device with which data will be exchanged | |
2260 | * @message: describes the data transfers, including completion callback | |
2261 | * Context: any (irqs may be blocked, etc) | |
2262 | * | |
2263 | * This call may be used in_irq and other contexts which can't sleep, | |
2264 | * as well as from task contexts which can sleep. | |
2265 | * | |
2266 | * The completion callback is invoked in a context which can't sleep. | |
2267 | * Before that invocation, the value of message->status is undefined. | |
2268 | * When the callback is issued, message->status holds either zero (to | |
2269 | * indicate complete success) or a negative error code. After that | |
2270 | * callback returns, the driver which issued the transfer request may | |
2271 | * deallocate the associated memory; it's no longer in use by any SPI | |
2272 | * core or controller driver code. | |
2273 | * | |
2274 | * Note that although all messages to a spi_device are handled in | |
2275 | * FIFO order, messages may go to different devices in other orders. | |
2276 | * Some device might be higher priority, or have various "hard" access | |
2277 | * time requirements, for example. | |
2278 | * | |
2279 | * On detection of any fault during the transfer, processing of | |
2280 | * the entire message is aborted, and the device is deselected. | |
2281 | * Until returning from the associated message completion callback, | |
2282 | * no other spi_message queued to that device will be processed. | |
2283 | * (This rule applies equally to all the synchronous transfer calls, | |
2284 | * which are wrappers around this core asynchronous primitive.) | |
2285 | */ | |
2286 | int spi_async_locked(struct spi_device *spi, struct spi_message *message) | |
2287 | { | |
2288 | struct spi_master *master = spi->master; | |
2289 | int ret; | |
2290 | unsigned long flags; | |
2291 | ||
90808738 MB |
2292 | ret = __spi_validate(spi, message); |
2293 | if (ret != 0) | |
2294 | return ret; | |
2295 | ||
cf32b71e ES |
2296 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
2297 | ||
2298 | ret = __spi_async(spi, message); | |
2299 | ||
2300 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); | |
2301 | ||
2302 | return ret; | |
2303 | ||
2304 | } | |
2305 | EXPORT_SYMBOL_GPL(spi_async_locked); | |
2306 | ||
7d077197 DB |
2307 | |
2308 | /*-------------------------------------------------------------------------*/ | |
2309 | ||
2310 | /* Utility methods for SPI master protocol drivers, layered on | |
2311 | * top of the core. Some other utility methods are defined as | |
2312 | * inline functions. | |
2313 | */ | |
2314 | ||
5d870c8e AM |
2315 | static void spi_complete(void *arg) |
2316 | { | |
2317 | complete(arg); | |
2318 | } | |
2319 | ||
cf32b71e ES |
2320 | static int __spi_sync(struct spi_device *spi, struct spi_message *message, |
2321 | int bus_locked) | |
2322 | { | |
2323 | DECLARE_COMPLETION_ONSTACK(done); | |
2324 | int status; | |
2325 | struct spi_master *master = spi->master; | |
0461a414 MB |
2326 | unsigned long flags; |
2327 | ||
2328 | status = __spi_validate(spi, message); | |
2329 | if (status != 0) | |
2330 | return status; | |
cf32b71e ES |
2331 | |
2332 | message->complete = spi_complete; | |
2333 | message->context = &done; | |
0461a414 | 2334 | message->spi = spi; |
cf32b71e | 2335 | |
eca2ebc7 MS |
2336 | SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, spi_sync); |
2337 | SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); | |
2338 | ||
cf32b71e ES |
2339 | if (!bus_locked) |
2340 | mutex_lock(&master->bus_lock_mutex); | |
2341 | ||
0461a414 MB |
2342 | /* If we're not using the legacy transfer method then we will |
2343 | * try to transfer in the calling context so special case. | |
2344 | * This code would be less tricky if we could remove the | |
2345 | * support for driver implemented message queues. | |
2346 | */ | |
2347 | if (master->transfer == spi_queued_transfer) { | |
2348 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); | |
2349 | ||
2350 | trace_spi_message_submit(message); | |
2351 | ||
2352 | status = __spi_queued_transfer(spi, message, false); | |
2353 | ||
2354 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); | |
2355 | } else { | |
2356 | status = spi_async_locked(spi, message); | |
2357 | } | |
cf32b71e ES |
2358 | |
2359 | if (!bus_locked) | |
2360 | mutex_unlock(&master->bus_lock_mutex); | |
2361 | ||
2362 | if (status == 0) { | |
0461a414 MB |
2363 | /* Push out the messages in the calling context if we |
2364 | * can. | |
2365 | */ | |
eca2ebc7 MS |
2366 | if (master->transfer == spi_queued_transfer) { |
2367 | SPI_STATISTICS_INCREMENT_FIELD(&master->statistics, | |
2368 | spi_sync_immediate); | |
2369 | SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, | |
2370 | spi_sync_immediate); | |
fc9e0f71 | 2371 | __spi_pump_messages(master, false); |
eca2ebc7 | 2372 | } |
0461a414 | 2373 | |
cf32b71e ES |
2374 | wait_for_completion(&done); |
2375 | status = message->status; | |
2376 | } | |
2377 | message->context = NULL; | |
2378 | return status; | |
2379 | } | |
2380 | ||
8ae12a0d DB |
2381 | /** |
2382 | * spi_sync - blocking/synchronous SPI data transfers | |
2383 | * @spi: device with which data will be exchanged | |
2384 | * @message: describes the data transfers | |
33e34dc6 | 2385 | * Context: can sleep |
8ae12a0d DB |
2386 | * |
2387 | * This call may only be used from a context that may sleep. The sleep | |
2388 | * is non-interruptible, and has no timeout. Low-overhead controller | |
2389 | * drivers may DMA directly into and out of the message buffers. | |
2390 | * | |
2391 | * Note that the SPI device's chip select is active during the message, | |
2392 | * and then is normally disabled between messages. Drivers for some | |
2393 | * frequently-used devices may want to minimize costs of selecting a chip, | |
2394 | * by leaving it selected in anticipation that the next message will go | |
2395 | * to the same chip. (That may increase power usage.) | |
2396 | * | |
0c868461 DB |
2397 | * Also, the caller is guaranteeing that the memory associated with the |
2398 | * message will not be freed before this call returns. | |
2399 | * | |
9b938b74 | 2400 | * It returns zero on success, else a negative error code. |
8ae12a0d DB |
2401 | */ |
2402 | int spi_sync(struct spi_device *spi, struct spi_message *message) | |
2403 | { | |
cf32b71e | 2404 | return __spi_sync(spi, message, 0); |
8ae12a0d DB |
2405 | } |
2406 | EXPORT_SYMBOL_GPL(spi_sync); | |
2407 | ||
cf32b71e ES |
2408 | /** |
2409 | * spi_sync_locked - version of spi_sync with exclusive bus usage | |
2410 | * @spi: device with which data will be exchanged | |
2411 | * @message: describes the data transfers | |
2412 | * Context: can sleep | |
2413 | * | |
2414 | * This call may only be used from a context that may sleep. The sleep | |
2415 | * is non-interruptible, and has no timeout. Low-overhead controller | |
2416 | * drivers may DMA directly into and out of the message buffers. | |
2417 | * | |
2418 | * This call should be used by drivers that require exclusive access to the | |
25985edc | 2419 | * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must |
cf32b71e ES |
2420 | * be released by a spi_bus_unlock call when the exclusive access is over. |
2421 | * | |
2422 | * It returns zero on success, else a negative error code. | |
2423 | */ | |
2424 | int spi_sync_locked(struct spi_device *spi, struct spi_message *message) | |
2425 | { | |
2426 | return __spi_sync(spi, message, 1); | |
2427 | } | |
2428 | EXPORT_SYMBOL_GPL(spi_sync_locked); | |
2429 | ||
2430 | /** | |
2431 | * spi_bus_lock - obtain a lock for exclusive SPI bus usage | |
2432 | * @master: SPI bus master that should be locked for exclusive bus access | |
2433 | * Context: can sleep | |
2434 | * | |
2435 | * This call may only be used from a context that may sleep. The sleep | |
2436 | * is non-interruptible, and has no timeout. | |
2437 | * | |
2438 | * This call should be used by drivers that require exclusive access to the | |
2439 | * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the | |
2440 | * exclusive access is over. Data transfer must be done by spi_sync_locked | |
2441 | * and spi_async_locked calls when the SPI bus lock is held. | |
2442 | * | |
2443 | * It returns zero on success, else a negative error code. | |
2444 | */ | |
2445 | int spi_bus_lock(struct spi_master *master) | |
2446 | { | |
2447 | unsigned long flags; | |
2448 | ||
2449 | mutex_lock(&master->bus_lock_mutex); | |
2450 | ||
2451 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); | |
2452 | master->bus_lock_flag = 1; | |
2453 | spin_unlock_irqrestore(&master->bus_lock_spinlock, flags); | |
2454 | ||
2455 | /* mutex remains locked until spi_bus_unlock is called */ | |
2456 | ||
2457 | return 0; | |
2458 | } | |
2459 | EXPORT_SYMBOL_GPL(spi_bus_lock); | |
2460 | ||
2461 | /** | |
2462 | * spi_bus_unlock - release the lock for exclusive SPI bus usage | |
2463 | * @master: SPI bus master that was locked for exclusive bus access | |
2464 | * Context: can sleep | |
2465 | * | |
2466 | * This call may only be used from a context that may sleep. The sleep | |
2467 | * is non-interruptible, and has no timeout. | |
2468 | * | |
2469 | * This call releases an SPI bus lock previously obtained by an spi_bus_lock | |
2470 | * call. | |
2471 | * | |
2472 | * It returns zero on success, else a negative error code. | |
2473 | */ | |
2474 | int spi_bus_unlock(struct spi_master *master) | |
2475 | { | |
2476 | master->bus_lock_flag = 0; | |
2477 | ||
2478 | mutex_unlock(&master->bus_lock_mutex); | |
2479 | ||
2480 | return 0; | |
2481 | } | |
2482 | EXPORT_SYMBOL_GPL(spi_bus_unlock); | |
2483 | ||
a9948b61 | 2484 | /* portable code must never pass more than 32 bytes */ |
5fe5f05e | 2485 | #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) |
8ae12a0d DB |
2486 | |
2487 | static u8 *buf; | |
2488 | ||
2489 | /** | |
2490 | * spi_write_then_read - SPI synchronous write followed by read | |
2491 | * @spi: device with which data will be exchanged | |
2492 | * @txbuf: data to be written (need not be dma-safe) | |
2493 | * @n_tx: size of txbuf, in bytes | |
27570497 JP |
2494 | * @rxbuf: buffer into which data will be read (need not be dma-safe) |
2495 | * @n_rx: size of rxbuf, in bytes | |
33e34dc6 | 2496 | * Context: can sleep |
8ae12a0d DB |
2497 | * |
2498 | * This performs a half duplex MicroWire style transaction with the | |
2499 | * device, sending txbuf and then reading rxbuf. The return value | |
2500 | * is zero for success, else a negative errno status code. | |
b885244e | 2501 | * This call may only be used from a context that may sleep. |
8ae12a0d | 2502 | * |
0c868461 | 2503 | * Parameters to this routine are always copied using a small buffer; |
33e34dc6 DB |
2504 | * portable code should never use this for more than 32 bytes. |
2505 | * Performance-sensitive or bulk transfer code should instead use | |
0c868461 | 2506 | * spi_{async,sync}() calls with dma-safe buffers. |
8ae12a0d DB |
2507 | */ |
2508 | int spi_write_then_read(struct spi_device *spi, | |
0c4a1590 MB |
2509 | const void *txbuf, unsigned n_tx, |
2510 | void *rxbuf, unsigned n_rx) | |
8ae12a0d | 2511 | { |
068f4070 | 2512 | static DEFINE_MUTEX(lock); |
8ae12a0d DB |
2513 | |
2514 | int status; | |
2515 | struct spi_message message; | |
bdff549e | 2516 | struct spi_transfer x[2]; |
8ae12a0d DB |
2517 | u8 *local_buf; |
2518 | ||
b3a223ee MB |
2519 | /* Use preallocated DMA-safe buffer if we can. We can't avoid |
2520 | * copying here, (as a pure convenience thing), but we can | |
2521 | * keep heap costs out of the hot path unless someone else is | |
2522 | * using the pre-allocated buffer or the transfer is too large. | |
8ae12a0d | 2523 | */ |
b3a223ee | 2524 | if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { |
2cd94c8a MB |
2525 | local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), |
2526 | GFP_KERNEL | GFP_DMA); | |
b3a223ee MB |
2527 | if (!local_buf) |
2528 | return -ENOMEM; | |
2529 | } else { | |
2530 | local_buf = buf; | |
2531 | } | |
8ae12a0d | 2532 | |
8275c642 | 2533 | spi_message_init(&message); |
5fe5f05e | 2534 | memset(x, 0, sizeof(x)); |
bdff549e DB |
2535 | if (n_tx) { |
2536 | x[0].len = n_tx; | |
2537 | spi_message_add_tail(&x[0], &message); | |
2538 | } | |
2539 | if (n_rx) { | |
2540 | x[1].len = n_rx; | |
2541 | spi_message_add_tail(&x[1], &message); | |
2542 | } | |
8275c642 | 2543 | |
8ae12a0d | 2544 | memcpy(local_buf, txbuf, n_tx); |
bdff549e DB |
2545 | x[0].tx_buf = local_buf; |
2546 | x[1].rx_buf = local_buf + n_tx; | |
8ae12a0d DB |
2547 | |
2548 | /* do the i/o */ | |
8ae12a0d | 2549 | status = spi_sync(spi, &message); |
9b938b74 | 2550 | if (status == 0) |
bdff549e | 2551 | memcpy(rxbuf, x[1].rx_buf, n_rx); |
8ae12a0d | 2552 | |
bdff549e | 2553 | if (x[0].tx_buf == buf) |
068f4070 | 2554 | mutex_unlock(&lock); |
8ae12a0d DB |
2555 | else |
2556 | kfree(local_buf); | |
2557 | ||
2558 | return status; | |
2559 | } | |
2560 | EXPORT_SYMBOL_GPL(spi_write_then_read); | |
2561 | ||
2562 | /*-------------------------------------------------------------------------*/ | |
2563 | ||
ce79d54a PA |
2564 | #if IS_ENABLED(CONFIG_OF_DYNAMIC) |
2565 | static int __spi_of_device_match(struct device *dev, void *data) | |
2566 | { | |
2567 | return dev->of_node == data; | |
2568 | } | |
2569 | ||
2570 | /* must call put_device() when done with returned spi_device device */ | |
2571 | static struct spi_device *of_find_spi_device_by_node(struct device_node *node) | |
2572 | { | |
2573 | struct device *dev = bus_find_device(&spi_bus_type, NULL, node, | |
2574 | __spi_of_device_match); | |
2575 | return dev ? to_spi_device(dev) : NULL; | |
2576 | } | |
2577 | ||
2578 | static int __spi_of_master_match(struct device *dev, const void *data) | |
2579 | { | |
2580 | return dev->of_node == data; | |
2581 | } | |
2582 | ||
2583 | /* the spi masters are not using spi_bus, so we find it with another way */ | |
2584 | static struct spi_master *of_find_spi_master_by_node(struct device_node *node) | |
2585 | { | |
2586 | struct device *dev; | |
2587 | ||
2588 | dev = class_find_device(&spi_master_class, NULL, node, | |
2589 | __spi_of_master_match); | |
2590 | if (!dev) | |
2591 | return NULL; | |
2592 | ||
2593 | /* reference got in class_find_device */ | |
2594 | return container_of(dev, struct spi_master, dev); | |
2595 | } | |
2596 | ||
2597 | static int of_spi_notify(struct notifier_block *nb, unsigned long action, | |
2598 | void *arg) | |
2599 | { | |
2600 | struct of_reconfig_data *rd = arg; | |
2601 | struct spi_master *master; | |
2602 | struct spi_device *spi; | |
2603 | ||
2604 | switch (of_reconfig_get_state_change(action, arg)) { | |
2605 | case OF_RECONFIG_CHANGE_ADD: | |
2606 | master = of_find_spi_master_by_node(rd->dn->parent); | |
2607 | if (master == NULL) | |
2608 | return NOTIFY_OK; /* not for us */ | |
2609 | ||
2610 | spi = of_register_spi_device(master, rd->dn); | |
2611 | put_device(&master->dev); | |
2612 | ||
2613 | if (IS_ERR(spi)) { | |
2614 | pr_err("%s: failed to create for '%s'\n", | |
2615 | __func__, rd->dn->full_name); | |
2616 | return notifier_from_errno(PTR_ERR(spi)); | |
2617 | } | |
2618 | break; | |
2619 | ||
2620 | case OF_RECONFIG_CHANGE_REMOVE: | |
2621 | /* find our device by node */ | |
2622 | spi = of_find_spi_device_by_node(rd->dn); | |
2623 | if (spi == NULL) | |
2624 | return NOTIFY_OK; /* no? not meant for us */ | |
2625 | ||
2626 | /* unregister takes one ref away */ | |
2627 | spi_unregister_device(spi); | |
2628 | ||
2629 | /* and put the reference of the find */ | |
2630 | put_device(&spi->dev); | |
2631 | break; | |
2632 | } | |
2633 | ||
2634 | return NOTIFY_OK; | |
2635 | } | |
2636 | ||
2637 | static struct notifier_block spi_of_notifier = { | |
2638 | .notifier_call = of_spi_notify, | |
2639 | }; | |
2640 | #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ | |
2641 | extern struct notifier_block spi_of_notifier; | |
2642 | #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ | |
2643 | ||
8ae12a0d DB |
2644 | static int __init spi_init(void) |
2645 | { | |
b885244e DB |
2646 | int status; |
2647 | ||
e94b1766 | 2648 | buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); |
b885244e DB |
2649 | if (!buf) { |
2650 | status = -ENOMEM; | |
2651 | goto err0; | |
2652 | } | |
2653 | ||
2654 | status = bus_register(&spi_bus_type); | |
2655 | if (status < 0) | |
2656 | goto err1; | |
8ae12a0d | 2657 | |
b885244e DB |
2658 | status = class_register(&spi_master_class); |
2659 | if (status < 0) | |
2660 | goto err2; | |
ce79d54a | 2661 | |
5267720e | 2662 | if (IS_ENABLED(CONFIG_OF_DYNAMIC)) |
ce79d54a PA |
2663 | WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); |
2664 | ||
8ae12a0d | 2665 | return 0; |
b885244e DB |
2666 | |
2667 | err2: | |
2668 | bus_unregister(&spi_bus_type); | |
2669 | err1: | |
2670 | kfree(buf); | |
2671 | buf = NULL; | |
2672 | err0: | |
2673 | return status; | |
8ae12a0d | 2674 | } |
b885244e | 2675 | |
8ae12a0d DB |
2676 | /* board_info is normally registered in arch_initcall(), |
2677 | * but even essential drivers wait till later | |
b885244e DB |
2678 | * |
2679 | * REVISIT only boardinfo really needs static linking. the rest (device and | |
2680 | * driver registration) _could_ be dynamically linked (modular) ... costs | |
2681 | * include needing to have boardinfo data structures be much more public. | |
8ae12a0d | 2682 | */ |
673c0c00 | 2683 | postcore_initcall(spi_init); |
8ae12a0d | 2684 |