1b445bfcbSMarco Felsch // SPDX-License-Identifier: GPL-2.0-or-later 2787f4889SMark Brown // SPI init/core code 3787f4889SMark Brown // 4787f4889SMark Brown // Copyright (C) 2005 David Brownell 5787f4889SMark Brown // Copyright (C) 2008 Secret Lab Technologies Ltd. 68ae12a0dSDavid Brownell 78ae12a0dSDavid Brownell #include <linux/kernel.h> 88ae12a0dSDavid Brownell #include <linux/device.h> 98ae12a0dSDavid Brownell #include <linux/init.h> 108ae12a0dSDavid Brownell #include <linux/cache.h> 1199adef31SMark Brown #include <linux/dma-mapping.h> 1299adef31SMark Brown #include <linux/dmaengine.h> 1394040828SMatthias Kaehlcke #include <linux/mutex.h> 142b7a32f7SSinan Akman #include <linux/of_device.h> 15d57a4282SGrant Likely #include <linux/of_irq.h> 1686be408bSSylwester Nawrocki #include <linux/clk/clk-conf.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 18e0626e38SAnton Vorontsov #include <linux/mod_devicetable.h> 198ae12a0dSDavid Brownell #include <linux/spi/spi.h> 20b5932f5cSBoris Brezillon #include <linux/spi/spi-mem.h> 2174317984SJean-Christophe PLAGNIOL-VILLARD #include <linux/of_gpio.h> 22f3186dd8SLinus Walleij #include <linux/gpio/consumer.h> 233ae22e8cSMark Brown #include <linux/pm_runtime.h> 24f48c767cSUlf Hansson #include <linux/pm_domain.h> 25826cf175SDmitry Torokhov #include <linux/property.h> 26025ed130SPaul Gortmaker #include <linux/export.h> 278bd75c77SClark Williams #include <linux/sched/rt.h> 28ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h> 29ffbbdd21SLinus Walleij #include <linux/delay.h> 30ffbbdd21SLinus Walleij #include <linux/kthread.h> 3164bee4d2SMika Westerberg #include <linux/ioport.h> 3264bee4d2SMika Westerberg #include <linux/acpi.h> 33b1b8153cSVignesh R #include <linux/highmem.h> 349b61e302SSuniel Mahesh #include <linux/idr.h> 358a2e487eSLukas Wunner #include <linux/platform_data/x86/apple.h> 368ae12a0dSDavid Brownell 3756ec1978SMark Brown #define CREATE_TRACE_POINTS 3856ec1978SMark Brown #include <trace/events/spi.h> 39ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start); 40ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop); 419b61e302SSuniel Mahesh 4246336966SBoris Brezillon #include "internals.h" 4346336966SBoris Brezillon 449b61e302SSuniel Mahesh static DEFINE_IDR(spi_master_idr); 4556ec1978SMark Brown 468ae12a0dSDavid Brownell static void spidev_release(struct device *dev) 478ae12a0dSDavid Brownell { 480ffa0285SHans-Peter Nilsson struct spi_device *spi = to_spi_device(dev); 498ae12a0dSDavid Brownell 508caab75fSGeert Uytterhoeven spi_controller_put(spi->controller); 515039563eSTrent Piepho kfree(spi->driver_override); 5207a389feSRoman Tereshonkov kfree(spi); 538ae12a0dSDavid Brownell } 548ae12a0dSDavid Brownell 558ae12a0dSDavid Brownell static ssize_t 568ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf) 578ae12a0dSDavid Brownell { 588ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 598c4ff6d0SZhang Rui int len; 608c4ff6d0SZhang Rui 618c4ff6d0SZhang Rui len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 628c4ff6d0SZhang Rui if (len != -ENODEV) 638c4ff6d0SZhang Rui return len; 648ae12a0dSDavid Brownell 65d8e328b3SGrant Likely return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 668ae12a0dSDavid Brownell } 67aa7da564SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias); 688ae12a0dSDavid Brownell 695039563eSTrent Piepho static ssize_t driver_override_store(struct device *dev, 705039563eSTrent Piepho struct device_attribute *a, 715039563eSTrent Piepho const char *buf, size_t count) 725039563eSTrent Piepho { 735039563eSTrent Piepho struct spi_device *spi = to_spi_device(dev); 745039563eSTrent Piepho const char *end = memchr(buf, '\n', count); 755039563eSTrent Piepho const size_t len = end ? end - buf : count; 765039563eSTrent Piepho const char *driver_override, *old; 775039563eSTrent Piepho 785039563eSTrent Piepho /* We need to keep extra room for a newline when displaying value */ 795039563eSTrent Piepho if (len >= (PAGE_SIZE - 1)) 805039563eSTrent Piepho return -EINVAL; 815039563eSTrent Piepho 825039563eSTrent Piepho driver_override = kstrndup(buf, len, GFP_KERNEL); 835039563eSTrent Piepho if (!driver_override) 845039563eSTrent Piepho return -ENOMEM; 855039563eSTrent Piepho 865039563eSTrent Piepho device_lock(dev); 875039563eSTrent Piepho old = spi->driver_override; 885039563eSTrent Piepho if (len) { 895039563eSTrent Piepho spi->driver_override = driver_override; 905039563eSTrent Piepho } else { 91be73e323SAndy Shevchenko /* Empty string, disable driver override */ 925039563eSTrent Piepho spi->driver_override = NULL; 935039563eSTrent Piepho kfree(driver_override); 945039563eSTrent Piepho } 955039563eSTrent Piepho device_unlock(dev); 965039563eSTrent Piepho kfree(old); 975039563eSTrent Piepho 985039563eSTrent Piepho return count; 995039563eSTrent Piepho } 1005039563eSTrent Piepho 1015039563eSTrent Piepho static ssize_t driver_override_show(struct device *dev, 1025039563eSTrent Piepho struct device_attribute *a, char *buf) 1035039563eSTrent Piepho { 1045039563eSTrent Piepho const struct spi_device *spi = to_spi_device(dev); 1055039563eSTrent Piepho ssize_t len; 1065039563eSTrent Piepho 1075039563eSTrent Piepho device_lock(dev); 1085039563eSTrent Piepho len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : ""); 1095039563eSTrent Piepho device_unlock(dev); 1105039563eSTrent Piepho return len; 1115039563eSTrent Piepho } 1125039563eSTrent Piepho static DEVICE_ATTR_RW(driver_override); 1135039563eSTrent Piepho 114eca2ebc7SMartin Sperl #define SPI_STATISTICS_ATTRS(field, file) \ 1158caab75fSGeert Uytterhoeven static ssize_t spi_controller_##field##_show(struct device *dev, \ 116eca2ebc7SMartin Sperl struct device_attribute *attr, \ 117eca2ebc7SMartin Sperl char *buf) \ 118eca2ebc7SMartin Sperl { \ 1198caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, \ 1208caab75fSGeert Uytterhoeven struct spi_controller, dev); \ 1218caab75fSGeert Uytterhoeven return spi_statistics_##field##_show(&ctlr->statistics, buf); \ 122eca2ebc7SMartin Sperl } \ 1238caab75fSGeert Uytterhoeven static struct device_attribute dev_attr_spi_controller_##field = { \ 124ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 1258caab75fSGeert Uytterhoeven .show = spi_controller_##field##_show, \ 126eca2ebc7SMartin Sperl }; \ 127eca2ebc7SMartin Sperl static ssize_t spi_device_##field##_show(struct device *dev, \ 128eca2ebc7SMartin Sperl struct device_attribute *attr, \ 129eca2ebc7SMartin Sperl char *buf) \ 130eca2ebc7SMartin Sperl { \ 131d1eba93bSGeliang Tang struct spi_device *spi = to_spi_device(dev); \ 132eca2ebc7SMartin Sperl return spi_statistics_##field##_show(&spi->statistics, buf); \ 133eca2ebc7SMartin Sperl } \ 134eca2ebc7SMartin Sperl static struct device_attribute dev_attr_spi_device_##field = { \ 135ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 136eca2ebc7SMartin Sperl .show = spi_device_##field##_show, \ 137eca2ebc7SMartin Sperl } 138eca2ebc7SMartin Sperl 139eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \ 140eca2ebc7SMartin Sperl static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ 141eca2ebc7SMartin Sperl char *buf) \ 142eca2ebc7SMartin Sperl { \ 143eca2ebc7SMartin Sperl unsigned long flags; \ 144eca2ebc7SMartin Sperl ssize_t len; \ 145eca2ebc7SMartin Sperl spin_lock_irqsave(&stat->lock, flags); \ 146eca2ebc7SMartin Sperl len = sprintf(buf, format_string, stat->field); \ 147eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stat->lock, flags); \ 148eca2ebc7SMartin Sperl return len; \ 149eca2ebc7SMartin Sperl } \ 150eca2ebc7SMartin Sperl SPI_STATISTICS_ATTRS(name, file) 151eca2ebc7SMartin Sperl 152eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW(field, format_string) \ 153eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ 154eca2ebc7SMartin Sperl field, format_string) 155eca2ebc7SMartin Sperl 156eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(messages, "%lu"); 157eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(transfers, "%lu"); 158eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(errors, "%lu"); 159eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(timedout, "%lu"); 160eca2ebc7SMartin Sperl 161eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync, "%lu"); 162eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu"); 163eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_async, "%lu"); 164eca2ebc7SMartin Sperl 165eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes, "%llu"); 166eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_rx, "%llu"); 167eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_tx, "%llu"); 168eca2ebc7SMartin Sperl 1696b7bc061SMartin Sperl #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ 1706b7bc061SMartin Sperl SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ 1716b7bc061SMartin Sperl "transfer_bytes_histo_" number, \ 1726b7bc061SMartin Sperl transfer_bytes_histo[index], "%lu") 1736b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); 1746b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); 1756b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); 1766b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); 1776b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); 1786b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); 1796b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); 1806b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); 1816b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); 1826b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); 1836b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); 1846b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); 1856b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); 1866b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); 1876b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); 1886b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); 1896b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); 1906b7bc061SMartin Sperl 191d9f12122SMartin Sperl SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu"); 192d9f12122SMartin Sperl 193aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = { 194aa7da564SGreg Kroah-Hartman &dev_attr_modalias.attr, 1955039563eSTrent Piepho &dev_attr_driver_override.attr, 196aa7da564SGreg Kroah-Hartman NULL, 1978ae12a0dSDavid Brownell }; 198eca2ebc7SMartin Sperl 199eca2ebc7SMartin Sperl static const struct attribute_group spi_dev_group = { 200eca2ebc7SMartin Sperl .attrs = spi_dev_attrs, 201eca2ebc7SMartin Sperl }; 202eca2ebc7SMartin Sperl 203eca2ebc7SMartin Sperl static struct attribute *spi_device_statistics_attrs[] = { 204eca2ebc7SMartin Sperl &dev_attr_spi_device_messages.attr, 205eca2ebc7SMartin Sperl &dev_attr_spi_device_transfers.attr, 206eca2ebc7SMartin Sperl &dev_attr_spi_device_errors.attr, 207eca2ebc7SMartin Sperl &dev_attr_spi_device_timedout.attr, 208eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync.attr, 209eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync_immediate.attr, 210eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_async.attr, 211eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes.attr, 212eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_rx.attr, 213eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_tx.attr, 2146b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo0.attr, 2156b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo1.attr, 2166b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo2.attr, 2176b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo3.attr, 2186b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo4.attr, 2196b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo5.attr, 2206b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo6.attr, 2216b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo7.attr, 2226b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo8.attr, 2236b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo9.attr, 2246b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo10.attr, 2256b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo11.attr, 2266b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo12.attr, 2276b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo13.attr, 2286b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo14.attr, 2296b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo15.attr, 2306b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo16.attr, 231d9f12122SMartin Sperl &dev_attr_spi_device_transfers_split_maxsize.attr, 232eca2ebc7SMartin Sperl NULL, 233eca2ebc7SMartin Sperl }; 234eca2ebc7SMartin Sperl 235eca2ebc7SMartin Sperl static const struct attribute_group spi_device_statistics_group = { 236eca2ebc7SMartin Sperl .name = "statistics", 237eca2ebc7SMartin Sperl .attrs = spi_device_statistics_attrs, 238eca2ebc7SMartin Sperl }; 239eca2ebc7SMartin Sperl 240eca2ebc7SMartin Sperl static const struct attribute_group *spi_dev_groups[] = { 241eca2ebc7SMartin Sperl &spi_dev_group, 242eca2ebc7SMartin Sperl &spi_device_statistics_group, 243eca2ebc7SMartin Sperl NULL, 244eca2ebc7SMartin Sperl }; 245eca2ebc7SMartin Sperl 2468caab75fSGeert Uytterhoeven static struct attribute *spi_controller_statistics_attrs[] = { 2478caab75fSGeert Uytterhoeven &dev_attr_spi_controller_messages.attr, 2488caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers.attr, 2498caab75fSGeert Uytterhoeven &dev_attr_spi_controller_errors.attr, 2508caab75fSGeert Uytterhoeven &dev_attr_spi_controller_timedout.attr, 2518caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync.attr, 2528caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync_immediate.attr, 2538caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_async.attr, 2548caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes.attr, 2558caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_rx.attr, 2568caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_tx.attr, 2578caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo0.attr, 2588caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo1.attr, 2598caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo2.attr, 2608caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo3.attr, 2618caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo4.attr, 2628caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo5.attr, 2638caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo6.attr, 2648caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo7.attr, 2658caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo8.attr, 2668caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo9.attr, 2678caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo10.attr, 2688caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo11.attr, 2698caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo12.attr, 2708caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo13.attr, 2718caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo14.attr, 2728caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo15.attr, 2738caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo16.attr, 2748caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers_split_maxsize.attr, 275eca2ebc7SMartin Sperl NULL, 276eca2ebc7SMartin Sperl }; 277eca2ebc7SMartin Sperl 2788caab75fSGeert Uytterhoeven static const struct attribute_group spi_controller_statistics_group = { 279eca2ebc7SMartin Sperl .name = "statistics", 2808caab75fSGeert Uytterhoeven .attrs = spi_controller_statistics_attrs, 281eca2ebc7SMartin Sperl }; 282eca2ebc7SMartin Sperl 283eca2ebc7SMartin Sperl static const struct attribute_group *spi_master_groups[] = { 2848caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 285eca2ebc7SMartin Sperl NULL, 286eca2ebc7SMartin Sperl }; 287eca2ebc7SMartin Sperl 288da21fde0SUwe Kleine-König static void spi_statistics_add_transfer_stats(struct spi_statistics *stats, 289eca2ebc7SMartin Sperl struct spi_transfer *xfer, 2908caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 291eca2ebc7SMartin Sperl { 292eca2ebc7SMartin Sperl unsigned long flags; 2936b7bc061SMartin Sperl int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; 2946b7bc061SMartin Sperl 2956b7bc061SMartin Sperl if (l2len < 0) 2966b7bc061SMartin Sperl l2len = 0; 297eca2ebc7SMartin Sperl 298eca2ebc7SMartin Sperl spin_lock_irqsave(&stats->lock, flags); 299eca2ebc7SMartin Sperl 300eca2ebc7SMartin Sperl stats->transfers++; 3016b7bc061SMartin Sperl stats->transfer_bytes_histo[l2len]++; 302eca2ebc7SMartin Sperl 303eca2ebc7SMartin Sperl stats->bytes += xfer->len; 304eca2ebc7SMartin Sperl if ((xfer->tx_buf) && 3058caab75fSGeert Uytterhoeven (xfer->tx_buf != ctlr->dummy_tx)) 306eca2ebc7SMartin Sperl stats->bytes_tx += xfer->len; 307eca2ebc7SMartin Sperl if ((xfer->rx_buf) && 3088caab75fSGeert Uytterhoeven (xfer->rx_buf != ctlr->dummy_rx)) 309eca2ebc7SMartin Sperl stats->bytes_rx += xfer->len; 310eca2ebc7SMartin Sperl 311eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stats->lock, flags); 312eca2ebc7SMartin Sperl } 3138ae12a0dSDavid Brownell 314*350de7ceSAndy Shevchenko /* 315*350de7ceSAndy Shevchenko * modalias support makes "modprobe $MODALIAS" new-style hotplug work, 3168ae12a0dSDavid Brownell * and the sysfs version makes coldplug work too. 3178ae12a0dSDavid Brownell */ 3183f076575SAndy Shevchenko static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, const char *name) 31975368bf6SAnton Vorontsov { 32075368bf6SAnton Vorontsov while (id->name[0]) { 3213f076575SAndy Shevchenko if (!strcmp(name, id->name)) 32275368bf6SAnton Vorontsov return id; 32375368bf6SAnton Vorontsov id++; 32475368bf6SAnton Vorontsov } 32575368bf6SAnton Vorontsov return NULL; 32675368bf6SAnton Vorontsov } 32775368bf6SAnton Vorontsov 32875368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 32975368bf6SAnton Vorontsov { 33075368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 33175368bf6SAnton Vorontsov 3323f076575SAndy Shevchenko return spi_match_id(sdrv->id_table, sdev->modalias); 33375368bf6SAnton Vorontsov } 33475368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id); 33575368bf6SAnton Vorontsov 3368ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 3378ae12a0dSDavid Brownell { 3388ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 33975368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(drv); 34075368bf6SAnton Vorontsov 3415039563eSTrent Piepho /* Check override first, and if set, only use the named driver */ 3425039563eSTrent Piepho if (spi->driver_override) 3435039563eSTrent Piepho return strcmp(spi->driver_override, drv->name) == 0; 3445039563eSTrent Piepho 3452b7a32f7SSinan Akman /* Attempt an OF style match */ 3462b7a32f7SSinan Akman if (of_driver_match_device(dev, drv)) 3472b7a32f7SSinan Akman return 1; 3482b7a32f7SSinan Akman 34964bee4d2SMika Westerberg /* Then try ACPI */ 35064bee4d2SMika Westerberg if (acpi_driver_match_device(dev, drv)) 35164bee4d2SMika Westerberg return 1; 35264bee4d2SMika Westerberg 35375368bf6SAnton Vorontsov if (sdrv->id_table) 3543f076575SAndy Shevchenko return !!spi_match_id(sdrv->id_table, spi->modalias); 3558ae12a0dSDavid Brownell 35635f74fcaSKay Sievers return strcmp(spi->modalias, drv->name) == 0; 3578ae12a0dSDavid Brownell } 3588ae12a0dSDavid Brownell 3597eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 3608ae12a0dSDavid Brownell { 3618ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 3628c4ff6d0SZhang Rui int rc; 3638c4ff6d0SZhang Rui 3648c4ff6d0SZhang Rui rc = acpi_device_uevent_modalias(dev, env); 3658c4ff6d0SZhang Rui if (rc != -ENODEV) 3668c4ff6d0SZhang Rui return rc; 3678ae12a0dSDavid Brownell 3682856670fSAndy Shevchenko return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 3698ae12a0dSDavid Brownell } 3708ae12a0dSDavid Brownell 3719db34ee6SUwe Kleine-König static int spi_probe(struct device *dev) 372b885244eSDavid Brownell { 373b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 37444af7927SJon Hunter struct spi_device *spi = to_spi_device(dev); 37533cf00e5SMika Westerberg int ret; 376b885244eSDavid Brownell 37786be408bSSylwester Nawrocki ret = of_clk_set_defaults(dev->of_node, false); 37886be408bSSylwester Nawrocki if (ret) 37986be408bSSylwester Nawrocki return ret; 38086be408bSSylwester Nawrocki 38144af7927SJon Hunter if (dev->of_node) { 38244af7927SJon Hunter spi->irq = of_irq_get(dev->of_node, 0); 38344af7927SJon Hunter if (spi->irq == -EPROBE_DEFER) 38444af7927SJon Hunter return -EPROBE_DEFER; 38544af7927SJon Hunter if (spi->irq < 0) 38644af7927SJon Hunter spi->irq = 0; 38744af7927SJon Hunter } 38844af7927SJon Hunter 389676e7c25SUlf Hansson ret = dev_pm_domain_attach(dev, true); 39071f277a7SUlf Hansson if (ret) 39171f277a7SUlf Hansson return ret; 39271f277a7SUlf Hansson 393440408dbSUwe Kleine-König if (sdrv->probe) { 39444af7927SJon Hunter ret = sdrv->probe(spi); 39533cf00e5SMika Westerberg if (ret) 396676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 397440408dbSUwe Kleine-König } 39833cf00e5SMika Westerberg 39933cf00e5SMika Westerberg return ret; 400b885244eSDavid Brownell } 401b885244eSDavid Brownell 402fc7a6209SUwe Kleine-König static void spi_remove(struct device *dev) 403b885244eSDavid Brownell { 404b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 405b885244eSDavid Brownell 4067795d475SUwe Kleine-König if (sdrv->remove) { 4077795d475SUwe Kleine-König int ret; 4087795d475SUwe Kleine-König 409aec35f4eSJean Delvare ret = sdrv->remove(to_spi_device(dev)); 4107795d475SUwe Kleine-König if (ret) 4117795d475SUwe Kleine-König dev_warn(dev, 4127795d475SUwe Kleine-König "Failed to unbind driver (%pe), ignoring\n", 4137795d475SUwe Kleine-König ERR_PTR(ret)); 4147795d475SUwe Kleine-König } 4157795d475SUwe Kleine-König 416676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 417b885244eSDavid Brownell } 418b885244eSDavid Brownell 4199db34ee6SUwe Kleine-König static void spi_shutdown(struct device *dev) 420b885244eSDavid Brownell { 421a6f483b2SMarek Szyprowski if (dev->driver) { 422b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 423b885244eSDavid Brownell 4249db34ee6SUwe Kleine-König if (sdrv->shutdown) 425b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 426b885244eSDavid Brownell } 427a6f483b2SMarek Szyprowski } 428b885244eSDavid Brownell 4299db34ee6SUwe Kleine-König struct bus_type spi_bus_type = { 4309db34ee6SUwe Kleine-König .name = "spi", 4319db34ee6SUwe Kleine-König .dev_groups = spi_dev_groups, 4329db34ee6SUwe Kleine-König .match = spi_match_device, 4339db34ee6SUwe Kleine-König .uevent = spi_uevent, 4349db34ee6SUwe Kleine-König .probe = spi_probe, 4359db34ee6SUwe Kleine-König .remove = spi_remove, 4369db34ee6SUwe Kleine-König .shutdown = spi_shutdown, 4379db34ee6SUwe Kleine-König }; 4389db34ee6SUwe Kleine-König EXPORT_SYMBOL_GPL(spi_bus_type); 4399db34ee6SUwe Kleine-König 44033e34dc6SDavid Brownell /** 441ca5d2485SAndrew F. Davis * __spi_register_driver - register a SPI driver 44288c9321dSThierry Reding * @owner: owner module of the driver to register 44333e34dc6SDavid Brownell * @sdrv: the driver to register 44433e34dc6SDavid Brownell * Context: can sleep 44597d56dc6SJavier Martinez Canillas * 44697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 44733e34dc6SDavid Brownell */ 448ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) 449b885244eSDavid Brownell { 450ca5d2485SAndrew F. Davis sdrv->driver.owner = owner; 451b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 4525fa6863bSMark Brown 4535fa6863bSMark Brown /* 4545fa6863bSMark Brown * For Really Good Reasons we use spi: modaliases not of: 4555fa6863bSMark Brown * modaliases for DT so module autoloading won't work if we 4565fa6863bSMark Brown * don't have a spi_device_id as well as a compatible string. 4575fa6863bSMark Brown */ 4585fa6863bSMark Brown if (sdrv->driver.of_match_table) { 4595fa6863bSMark Brown const struct of_device_id *of_id; 4605fa6863bSMark Brown 4615fa6863bSMark Brown for (of_id = sdrv->driver.of_match_table; of_id->compatible[0]; 4625fa6863bSMark Brown of_id++) { 4635fa6863bSMark Brown const char *of_name; 4645fa6863bSMark Brown 4655fa6863bSMark Brown /* Strip off any vendor prefix */ 4665fa6863bSMark Brown of_name = strnchr(of_id->compatible, 4675fa6863bSMark Brown sizeof(of_id->compatible), ','); 4685fa6863bSMark Brown if (of_name) 4695fa6863bSMark Brown of_name++; 4705fa6863bSMark Brown else 4715fa6863bSMark Brown of_name = of_id->compatible; 4725fa6863bSMark Brown 4735fa6863bSMark Brown if (sdrv->id_table) { 4745fa6863bSMark Brown const struct spi_device_id *spi_id; 4755fa6863bSMark Brown 4763f076575SAndy Shevchenko spi_id = spi_match_id(sdrv->id_table, of_name); 4773f076575SAndy Shevchenko if (!spi_id) 4785fa6863bSMark Brown continue; 4795fa6863bSMark Brown } else { 4805fa6863bSMark Brown if (strcmp(sdrv->driver.name, of_name) == 0) 4815fa6863bSMark Brown continue; 4825fa6863bSMark Brown } 4835fa6863bSMark Brown 4845fa6863bSMark Brown pr_warn("SPI driver %s has no spi_device_id for %s\n", 4855fa6863bSMark Brown sdrv->driver.name, of_id->compatible); 4865fa6863bSMark Brown } 4875fa6863bSMark Brown } 4885fa6863bSMark Brown 489b885244eSDavid Brownell return driver_register(&sdrv->driver); 490b885244eSDavid Brownell } 491ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver); 492b885244eSDavid Brownell 4938ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 4948ae12a0dSDavid Brownell 495*350de7ceSAndy Shevchenko /* 496*350de7ceSAndy Shevchenko * SPI devices should normally not be created by SPI device drivers; that 4978caab75fSGeert Uytterhoeven * would make them board-specific. Similarly with SPI controller drivers. 4988ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 4998ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 5008ae12a0dSDavid Brownell */ 5018ae12a0dSDavid Brownell 5028ae12a0dSDavid Brownell struct boardinfo { 5038ae12a0dSDavid Brownell struct list_head list; 5042b9603a0SFeng Tang struct spi_board_info board_info; 5058ae12a0dSDavid Brownell }; 5068ae12a0dSDavid Brownell 5078ae12a0dSDavid Brownell static LIST_HEAD(board_list); 5088caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list); 5092b9603a0SFeng Tang 5102b9603a0SFeng Tang /* 511be73e323SAndy Shevchenko * Used to protect add/del operation for board_info list and 512*350de7ceSAndy Shevchenko * spi_controller list, and their matching process also used 513*350de7ceSAndy Shevchenko * to protect object of type struct idr. 5142b9603a0SFeng Tang */ 51594040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 5168ae12a0dSDavid Brownell 517dc87c98eSGrant Likely /** 518dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 5198caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 520dc87c98eSGrant Likely * Context: can sleep 521dc87c98eSGrant Likely * 522dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 523dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 524dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 525dc87c98eSGrant Likely * spi_add_device() on it. 526dc87c98eSGrant Likely * 527dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 5288caab75fSGeert Uytterhoeven * spi_device structure to add it to the SPI controller. If the caller 529dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 530dc87c98eSGrant Likely * call spi_dev_put() on it. 531dc87c98eSGrant Likely * 53297d56dc6SJavier Martinez Canillas * Return: a pointer to the new device, or NULL. 533dc87c98eSGrant Likely */ 534da21fde0SUwe Kleine-König static struct spi_device *spi_alloc_device(struct spi_controller *ctlr) 535dc87c98eSGrant Likely { 536dc87c98eSGrant Likely struct spi_device *spi; 537dc87c98eSGrant Likely 5388caab75fSGeert Uytterhoeven if (!spi_controller_get(ctlr)) 539dc87c98eSGrant Likely return NULL; 540dc87c98eSGrant Likely 5415fe5f05eSJingoo Han spi = kzalloc(sizeof(*spi), GFP_KERNEL); 542dc87c98eSGrant Likely if (!spi) { 5438caab75fSGeert Uytterhoeven spi_controller_put(ctlr); 544dc87c98eSGrant Likely return NULL; 545dc87c98eSGrant Likely } 546dc87c98eSGrant Likely 5478caab75fSGeert Uytterhoeven spi->master = spi->controller = ctlr; 5488caab75fSGeert Uytterhoeven spi->dev.parent = &ctlr->dev; 549dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 550dc87c98eSGrant Likely spi->dev.release = spidev_release; 551446411e1SAndreas Larsson spi->cs_gpio = -ENOENT; 552ea235786SJohn Garry spi->mode = ctlr->buswidth_override_bits; 553eca2ebc7SMartin Sperl 554eca2ebc7SMartin Sperl spin_lock_init(&spi->statistics.lock); 555eca2ebc7SMartin Sperl 556dc87c98eSGrant Likely device_initialize(&spi->dev); 557dc87c98eSGrant Likely return spi; 558dc87c98eSGrant Likely } 559dc87c98eSGrant Likely 560e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi) 561e13ac47bSJarkko Nikula { 562e13ac47bSJarkko Nikula struct acpi_device *adev = ACPI_COMPANION(&spi->dev); 563e13ac47bSJarkko Nikula 564e13ac47bSJarkko Nikula if (adev) { 565e13ac47bSJarkko Nikula dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); 566e13ac47bSJarkko Nikula return; 567e13ac47bSJarkko Nikula } 568e13ac47bSJarkko Nikula 5698caab75fSGeert Uytterhoeven dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), 570e13ac47bSJarkko Nikula spi->chip_select); 571e13ac47bSJarkko Nikula } 572e13ac47bSJarkko Nikula 573b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data) 574b6fb8d3aSMika Westerberg { 575b6fb8d3aSMika Westerberg struct spi_device *spi = to_spi_device(dev); 576b6fb8d3aSMika Westerberg struct spi_device *new_spi = data; 577b6fb8d3aSMika Westerberg 5788caab75fSGeert Uytterhoeven if (spi->controller == new_spi->controller && 579b6fb8d3aSMika Westerberg spi->chip_select == new_spi->chip_select) 580b6fb8d3aSMika Westerberg return -EBUSY; 581b6fb8d3aSMika Westerberg return 0; 582b6fb8d3aSMika Westerberg } 583b6fb8d3aSMika Westerberg 584c7299feaSSaravana Kannan static void spi_cleanup(struct spi_device *spi) 585c7299feaSSaravana Kannan { 586c7299feaSSaravana Kannan if (spi->controller->cleanup) 587c7299feaSSaravana Kannan spi->controller->cleanup(spi); 588c7299feaSSaravana Kannan } 589c7299feaSSaravana Kannan 5900c79378cSSebastian Reichel static int __spi_add_device(struct spi_device *spi) 5910c79378cSSebastian Reichel { 5920c79378cSSebastian Reichel struct spi_controller *ctlr = spi->controller; 5930c79378cSSebastian Reichel struct device *dev = ctlr->dev.parent; 5940c79378cSSebastian Reichel int status; 5950c79378cSSebastian Reichel 5966bfb15f3SUwe Kleine-König /* 5976bfb15f3SUwe Kleine-König * We need to make sure there's no other device with this 5986bfb15f3SUwe Kleine-König * chipselect **BEFORE** we call setup(), else we'll trash 5996bfb15f3SUwe Kleine-König * its configuration. 6006bfb15f3SUwe Kleine-König */ 6010c79378cSSebastian Reichel status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); 6020c79378cSSebastian Reichel if (status) { 6030c79378cSSebastian Reichel dev_err(dev, "chipselect %d already in use\n", 6040c79378cSSebastian Reichel spi->chip_select); 6050c79378cSSebastian Reichel return status; 6060c79378cSSebastian Reichel } 6070c79378cSSebastian Reichel 6080c79378cSSebastian Reichel /* Controller may unregister concurrently */ 6090c79378cSSebastian Reichel if (IS_ENABLED(CONFIG_SPI_DYNAMIC) && 6100c79378cSSebastian Reichel !device_is_registered(&ctlr->dev)) { 6110c79378cSSebastian Reichel return -ENODEV; 6120c79378cSSebastian Reichel } 6130c79378cSSebastian Reichel 6140c79378cSSebastian Reichel /* Descriptors take precedence */ 6150c79378cSSebastian Reichel if (ctlr->cs_gpiods) 6160c79378cSSebastian Reichel spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; 6170c79378cSSebastian Reichel else if (ctlr->cs_gpios) 6180c79378cSSebastian Reichel spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; 6190c79378cSSebastian Reichel 620*350de7ceSAndy Shevchenko /* 621*350de7ceSAndy Shevchenko * Drivers may modify this initial i/o setup, but will 6220c79378cSSebastian Reichel * normally rely on the device being setup. Devices 6230c79378cSSebastian Reichel * using SPI_CS_HIGH can't coexist well otherwise... 6240c79378cSSebastian Reichel */ 6250c79378cSSebastian Reichel status = spi_setup(spi); 6260c79378cSSebastian Reichel if (status < 0) { 6270c79378cSSebastian Reichel dev_err(dev, "can't setup %s, status %d\n", 6280c79378cSSebastian Reichel dev_name(&spi->dev), status); 6290c79378cSSebastian Reichel return status; 6300c79378cSSebastian Reichel } 6310c79378cSSebastian Reichel 6320c79378cSSebastian Reichel /* Device may be bound to an active driver when this returns */ 6330c79378cSSebastian Reichel status = device_add(&spi->dev); 6340c79378cSSebastian Reichel if (status < 0) { 6350c79378cSSebastian Reichel dev_err(dev, "can't add %s, status %d\n", 6360c79378cSSebastian Reichel dev_name(&spi->dev), status); 6370c79378cSSebastian Reichel spi_cleanup(spi); 6380c79378cSSebastian Reichel } else { 6390c79378cSSebastian Reichel dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 6400c79378cSSebastian Reichel } 6410c79378cSSebastian Reichel 6420c79378cSSebastian Reichel return status; 6430c79378cSSebastian Reichel } 6440c79378cSSebastian Reichel 645dc87c98eSGrant Likely /** 646dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 647dc87c98eSGrant Likely * @spi: spi_device to register 648dc87c98eSGrant Likely * 649dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 650dc87c98eSGrant Likely * spi_alloc_device can be added onto the spi bus with this function. 651dc87c98eSGrant Likely * 65297d56dc6SJavier Martinez Canillas * Return: 0 on success; negative errno on failure 653dc87c98eSGrant Likely */ 654da21fde0SUwe Kleine-König static int spi_add_device(struct spi_device *spi) 655dc87c98eSGrant Likely { 6568caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 6578caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 658dc87c98eSGrant Likely int status; 659dc87c98eSGrant Likely 660dc87c98eSGrant Likely /* Chipselects are numbered 0..max; validate. */ 6618caab75fSGeert Uytterhoeven if (spi->chip_select >= ctlr->num_chipselect) { 6628caab75fSGeert Uytterhoeven dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 6638caab75fSGeert Uytterhoeven ctlr->num_chipselect); 664dc87c98eSGrant Likely return -EINVAL; 665dc87c98eSGrant Likely } 666dc87c98eSGrant Likely 667dc87c98eSGrant Likely /* Set the bus ID string */ 668e13ac47bSJarkko Nikula spi_dev_set_name(spi); 669e48880e0SDavid Brownell 6706098475dSMark Brown mutex_lock(&ctlr->add_lock); 6710c79378cSSebastian Reichel status = __spi_add_device(spi); 6726098475dSMark Brown mutex_unlock(&ctlr->add_lock); 673e48880e0SDavid Brownell return status; 674dc87c98eSGrant Likely } 6758ae12a0dSDavid Brownell 6760c79378cSSebastian Reichel static int spi_add_device_locked(struct spi_device *spi) 6770c79378cSSebastian Reichel { 6780c79378cSSebastian Reichel struct spi_controller *ctlr = spi->controller; 6790c79378cSSebastian Reichel struct device *dev = ctlr->dev.parent; 6800c79378cSSebastian Reichel 6810c79378cSSebastian Reichel /* Chipselects are numbered 0..max; validate. */ 6820c79378cSSebastian Reichel if (spi->chip_select >= ctlr->num_chipselect) { 6830c79378cSSebastian Reichel dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 6840c79378cSSebastian Reichel ctlr->num_chipselect); 6850c79378cSSebastian Reichel return -EINVAL; 6860c79378cSSebastian Reichel } 6870c79378cSSebastian Reichel 6880c79378cSSebastian Reichel /* Set the bus ID string */ 6890c79378cSSebastian Reichel spi_dev_set_name(spi); 6900c79378cSSebastian Reichel 6916098475dSMark Brown WARN_ON(!mutex_is_locked(&ctlr->add_lock)); 6920c79378cSSebastian Reichel return __spi_add_device(spi); 6930c79378cSSebastian Reichel } 6940c79378cSSebastian Reichel 69533e34dc6SDavid Brownell /** 69633e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 6978caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 69833e34dc6SDavid Brownell * @chip: Describes the SPI device 69933e34dc6SDavid Brownell * Context: can sleep 70033e34dc6SDavid Brownell * 70133e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 7028ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 7038ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 7048ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 7058ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 706082c8cb4SDavid Brownell * 70797d56dc6SJavier Martinez Canillas * Return: the new device, or NULL. 7088ae12a0dSDavid Brownell */ 7098caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr, 710e9d5a461SAdrian Bunk struct spi_board_info *chip) 7118ae12a0dSDavid Brownell { 7128ae12a0dSDavid Brownell struct spi_device *proxy; 7138ae12a0dSDavid Brownell int status; 7148ae12a0dSDavid Brownell 715*350de7ceSAndy Shevchenko /* 716*350de7ceSAndy Shevchenko * NOTE: caller did any chip->bus_num checks necessary. 717082c8cb4SDavid Brownell * 718082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 719082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 720082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 721082c8cb4SDavid Brownell */ 722082c8cb4SDavid Brownell 7238caab75fSGeert Uytterhoeven proxy = spi_alloc_device(ctlr); 724dc87c98eSGrant Likely if (!proxy) 7258ae12a0dSDavid Brownell return NULL; 7268ae12a0dSDavid Brownell 727102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 728102eb975SGrant Likely 7298ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 7308ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 731980a01c9SDavid Brownell proxy->mode = chip->mode; 7328ae12a0dSDavid Brownell proxy->irq = chip->irq; 733102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 7348ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 7358ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 7368ae12a0dSDavid Brownell proxy->controller_state = NULL; 7378ae12a0dSDavid Brownell 73847afc77bSHeikki Krogerus if (chip->swnode) { 73947afc77bSHeikki Krogerus status = device_add_software_node(&proxy->dev, chip->swnode); 740826cf175SDmitry Torokhov if (status) { 7419d902c2aSColin Ian King dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n", 742826cf175SDmitry Torokhov chip->modalias, status); 743826cf175SDmitry Torokhov goto err_dev_put; 744826cf175SDmitry Torokhov } 7458ae12a0dSDavid Brownell } 746dc87c98eSGrant Likely 747826cf175SDmitry Torokhov status = spi_add_device(proxy); 748826cf175SDmitry Torokhov if (status < 0) 749df41a5daSHeikki Krogerus goto err_dev_put; 750826cf175SDmitry Torokhov 751dc87c98eSGrant Likely return proxy; 752826cf175SDmitry Torokhov 753826cf175SDmitry Torokhov err_dev_put: 754df41a5daSHeikki Krogerus device_remove_software_node(&proxy->dev); 755826cf175SDmitry Torokhov spi_dev_put(proxy); 756826cf175SDmitry Torokhov return NULL; 757dc87c98eSGrant Likely } 7588ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 7598ae12a0dSDavid Brownell 7603b1884c2SGeert Uytterhoeven /** 7613b1884c2SGeert Uytterhoeven * spi_unregister_device - unregister a single SPI device 7623b1884c2SGeert Uytterhoeven * @spi: spi_device to unregister 7633b1884c2SGeert Uytterhoeven * 7643b1884c2SGeert Uytterhoeven * Start making the passed SPI device vanish. Normally this would be handled 7658caab75fSGeert Uytterhoeven * by spi_unregister_controller(). 7663b1884c2SGeert Uytterhoeven */ 7673b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi) 7683b1884c2SGeert Uytterhoeven { 769bd6c1644SGeert Uytterhoeven if (!spi) 770bd6c1644SGeert Uytterhoeven return; 771bd6c1644SGeert Uytterhoeven 7728324147fSJohan Hovold if (spi->dev.of_node) { 773bd6c1644SGeert Uytterhoeven of_node_clear_flag(spi->dev.of_node, OF_POPULATED); 7748324147fSJohan Hovold of_node_put(spi->dev.of_node); 7758324147fSJohan Hovold } 7767f24467fSOctavian Purdila if (ACPI_COMPANION(&spi->dev)) 7777f24467fSOctavian Purdila acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); 77847afc77bSHeikki Krogerus device_remove_software_node(&spi->dev); 77927e7db56SSaravana Kannan device_del(&spi->dev); 78027e7db56SSaravana Kannan spi_cleanup(spi); 78127e7db56SSaravana Kannan put_device(&spi->dev); 7823b1884c2SGeert Uytterhoeven } 7833b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device); 7843b1884c2SGeert Uytterhoeven 7858caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, 7862b9603a0SFeng Tang struct spi_board_info *bi) 7872b9603a0SFeng Tang { 7882b9603a0SFeng Tang struct spi_device *dev; 7892b9603a0SFeng Tang 7908caab75fSGeert Uytterhoeven if (ctlr->bus_num != bi->bus_num) 7912b9603a0SFeng Tang return; 7922b9603a0SFeng Tang 7938caab75fSGeert Uytterhoeven dev = spi_new_device(ctlr, bi); 7942b9603a0SFeng Tang if (!dev) 7958caab75fSGeert Uytterhoeven dev_err(ctlr->dev.parent, "can't create new device for %s\n", 7962b9603a0SFeng Tang bi->modalias); 7972b9603a0SFeng Tang } 7982b9603a0SFeng Tang 79933e34dc6SDavid Brownell /** 80033e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 80133e34dc6SDavid Brownell * @info: array of chip descriptors 80233e34dc6SDavid Brownell * @n: how many descriptors are provided 80333e34dc6SDavid Brownell * Context: can sleep 80433e34dc6SDavid Brownell * 8058ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 8068ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 8078ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 8088ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 8098ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 8108ae12a0dSDavid Brownell * 8118ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 8128ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 8138ae12a0dSDavid Brownell * would naturally declare its SPI devices. 8148ae12a0dSDavid Brownell * 8158ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 8168ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 81797d56dc6SJavier Martinez Canillas * 81897d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 8198ae12a0dSDavid Brownell */ 820fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n) 8218ae12a0dSDavid Brownell { 8228ae12a0dSDavid Brownell struct boardinfo *bi; 8232b9603a0SFeng Tang int i; 8248ae12a0dSDavid Brownell 825c7908a37SXiubo Li if (!n) 826f974cf57SDmitry Torokhov return 0; 827c7908a37SXiubo Li 828f9bdb7fdSMarkus Elfring bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); 8298ae12a0dSDavid Brownell if (!bi) 8308ae12a0dSDavid Brownell return -ENOMEM; 8318ae12a0dSDavid Brownell 8322b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 8338caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 8342b9603a0SFeng Tang 8352b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 836826cf175SDmitry Torokhov 83794040828SMatthias Kaehlcke mutex_lock(&board_lock); 8388ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 8398caab75fSGeert Uytterhoeven list_for_each_entry(ctlr, &spi_controller_list, list) 8408caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, 8418caab75fSGeert Uytterhoeven &bi->board_info); 84294040828SMatthias Kaehlcke mutex_unlock(&board_lock); 8432b9603a0SFeng Tang } 8442b9603a0SFeng Tang 8458ae12a0dSDavid Brownell return 0; 8468ae12a0dSDavid Brownell } 8478ae12a0dSDavid Brownell 8488ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 8498ae12a0dSDavid Brownell 850fb51601bSUwe Kleine-König /* Core methods for SPI resource management */ 851fb51601bSUwe Kleine-König 852fb51601bSUwe Kleine-König /** 853fb51601bSUwe Kleine-König * spi_res_alloc - allocate a spi resource that is life-cycle managed 854fb51601bSUwe Kleine-König * during the processing of a spi_message while using 855fb51601bSUwe Kleine-König * spi_transfer_one 856fb51601bSUwe Kleine-König * @spi: the spi device for which we allocate memory 857fb51601bSUwe Kleine-König * @release: the release code to execute for this resource 858fb51601bSUwe Kleine-König * @size: size to alloc and return 859fb51601bSUwe Kleine-König * @gfp: GFP allocation flags 860fb51601bSUwe Kleine-König * 861fb51601bSUwe Kleine-König * Return: the pointer to the allocated data 862fb51601bSUwe Kleine-König * 863fb51601bSUwe Kleine-König * This may get enhanced in the future to allocate from a memory pool 864fb51601bSUwe Kleine-König * of the @spi_device or @spi_controller to avoid repeated allocations. 865fb51601bSUwe Kleine-König */ 866da21fde0SUwe Kleine-König static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release, 867fb51601bSUwe Kleine-König size_t size, gfp_t gfp) 868fb51601bSUwe Kleine-König { 869fb51601bSUwe Kleine-König struct spi_res *sres; 870fb51601bSUwe Kleine-König 871fb51601bSUwe Kleine-König sres = kzalloc(sizeof(*sres) + size, gfp); 872fb51601bSUwe Kleine-König if (!sres) 873fb51601bSUwe Kleine-König return NULL; 874fb51601bSUwe Kleine-König 875fb51601bSUwe Kleine-König INIT_LIST_HEAD(&sres->entry); 876fb51601bSUwe Kleine-König sres->release = release; 877fb51601bSUwe Kleine-König 878fb51601bSUwe Kleine-König return sres->data; 879fb51601bSUwe Kleine-König } 880fb51601bSUwe Kleine-König 881fb51601bSUwe Kleine-König /** 882fb51601bSUwe Kleine-König * spi_res_free - free an spi resource 883fb51601bSUwe Kleine-König * @res: pointer to the custom data of a resource 884fb51601bSUwe Kleine-König */ 885da21fde0SUwe Kleine-König static void spi_res_free(void *res) 886fb51601bSUwe Kleine-König { 887fb51601bSUwe Kleine-König struct spi_res *sres = container_of(res, struct spi_res, data); 888fb51601bSUwe Kleine-König 889fb51601bSUwe Kleine-König if (!res) 890fb51601bSUwe Kleine-König return; 891fb51601bSUwe Kleine-König 892fb51601bSUwe Kleine-König WARN_ON(!list_empty(&sres->entry)); 893fb51601bSUwe Kleine-König kfree(sres); 894fb51601bSUwe Kleine-König } 895fb51601bSUwe Kleine-König 896fb51601bSUwe Kleine-König /** 897fb51601bSUwe Kleine-König * spi_res_add - add a spi_res to the spi_message 898fb51601bSUwe Kleine-König * @message: the spi message 899fb51601bSUwe Kleine-König * @res: the spi_resource 900fb51601bSUwe Kleine-König */ 901da21fde0SUwe Kleine-König static void spi_res_add(struct spi_message *message, void *res) 902fb51601bSUwe Kleine-König { 903fb51601bSUwe Kleine-König struct spi_res *sres = container_of(res, struct spi_res, data); 904fb51601bSUwe Kleine-König 905fb51601bSUwe Kleine-König WARN_ON(!list_empty(&sres->entry)); 906fb51601bSUwe Kleine-König list_add_tail(&sres->entry, &message->resources); 907fb51601bSUwe Kleine-König } 908fb51601bSUwe Kleine-König 909fb51601bSUwe Kleine-König /** 910fb51601bSUwe Kleine-König * spi_res_release - release all spi resources for this message 911fb51601bSUwe Kleine-König * @ctlr: the @spi_controller 912fb51601bSUwe Kleine-König * @message: the @spi_message 913fb51601bSUwe Kleine-König */ 914da21fde0SUwe Kleine-König static void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 915fb51601bSUwe Kleine-König { 916fb51601bSUwe Kleine-König struct spi_res *res, *tmp; 917fb51601bSUwe Kleine-König 918fb51601bSUwe Kleine-König list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { 919fb51601bSUwe Kleine-König if (res->release) 920fb51601bSUwe Kleine-König res->release(ctlr, message, res->data); 921fb51601bSUwe Kleine-König 922fb51601bSUwe Kleine-König list_del(&res->entry); 923fb51601bSUwe Kleine-König 924fb51601bSUwe Kleine-König kfree(res); 925fb51601bSUwe Kleine-König } 926fb51601bSUwe Kleine-König } 927fb51601bSUwe Kleine-König 928fb51601bSUwe Kleine-König /*-------------------------------------------------------------------------*/ 929fb51601bSUwe Kleine-König 930d347b4aaSDavid Bauer static void spi_set_cs(struct spi_device *spi, bool enable, bool force) 931b158935fSMark Brown { 93286527bcbSAndy Shevchenko bool activate = enable; 93325093bdeSAlexandru Ardelean 934d40f0b6fSDouglas Anderson /* 935d40f0b6fSDouglas Anderson * Avoid calling into the driver (or doing delays) if the chip select 936d40f0b6fSDouglas Anderson * isn't actually changing from the last time this was called. 937d40f0b6fSDouglas Anderson */ 938d347b4aaSDavid Bauer if (!force && (spi->controller->last_cs_enable == enable) && 939d40f0b6fSDouglas Anderson (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) 940d40f0b6fSDouglas Anderson return; 941d40f0b6fSDouglas Anderson 9425cb4e1f3SAndy Shevchenko trace_spi_set_cs(spi, activate); 9435cb4e1f3SAndy Shevchenko 944d40f0b6fSDouglas Anderson spi->controller->last_cs_enable = enable; 945d40f0b6fSDouglas Anderson spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; 946d40f0b6fSDouglas Anderson 9470486d9f9Sleilk.liu if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio) || 9480486d9f9Sleilk.liu !spi->controller->set_cs_timing) { 94986527bcbSAndy Shevchenko if (activate) 9508c33ebfeSMason Zhang spi_delay_exec(&spi->cs_setup, NULL); 95125093bdeSAlexandru Ardelean else 9528c33ebfeSMason Zhang spi_delay_exec(&spi->cs_hold, NULL); 95325093bdeSAlexandru Ardelean } 95425093bdeSAlexandru Ardelean 955b158935fSMark Brown if (spi->mode & SPI_CS_HIGH) 956b158935fSMark Brown enable = !enable; 957b158935fSMark Brown 958f3186dd8SLinus Walleij if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { 959f3186dd8SLinus Walleij if (!(spi->mode & SPI_NO_CS)) { 9606b695469SAndy Shevchenko if (spi->cs_gpiod) { 9616b695469SAndy Shevchenko /* 9626b695469SAndy Shevchenko * Historically ACPI has no means of the GPIO polarity and 9636b695469SAndy Shevchenko * thus the SPISerialBus() resource defines it on the per-chip 9646b695469SAndy Shevchenko * basis. In order to avoid a chain of negations, the GPIO 9656b695469SAndy Shevchenko * polarity is considered being Active High. Even for the cases 9666b695469SAndy Shevchenko * when _DSD() is involved (in the updated versions of ACPI) 9676b695469SAndy Shevchenko * the GPIO CS polarity must be defined Active High to avoid 9686b695469SAndy Shevchenko * ambiguity. That's why we use enable, that takes SPI_CS_HIGH 9696b695469SAndy Shevchenko * into account. 9706b695469SAndy Shevchenko */ 9716b695469SAndy Shevchenko if (has_acpi_companion(&spi->dev)) 9726b695469SAndy Shevchenko gpiod_set_value_cansleep(spi->cs_gpiod, !enable); 973f3186dd8SLinus Walleij else 9746b695469SAndy Shevchenko /* Polarity handled by GPIO library */ 9756b695469SAndy Shevchenko gpiod_set_value_cansleep(spi->cs_gpiod, activate); 9766b695469SAndy Shevchenko } else { 977766c6b63SSven Van Asbroeck /* 978*350de7ceSAndy Shevchenko * Invert the enable line, as active low is 979766c6b63SSven Van Asbroeck * default for SPI. 980766c6b63SSven Van Asbroeck */ 98128f7604fSFelix Fietkau gpio_set_value_cansleep(spi->cs_gpio, !enable); 982f3186dd8SLinus Walleij } 9836b695469SAndy Shevchenko } 9848eee6b9dSThor Thayer /* Some SPI masters need both GPIO CS & slave_select */ 9858caab75fSGeert Uytterhoeven if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && 9868caab75fSGeert Uytterhoeven spi->controller->set_cs) 9878caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 9888caab75fSGeert Uytterhoeven } else if (spi->controller->set_cs) { 9898caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 9908eee6b9dSThor Thayer } 99125093bdeSAlexandru Ardelean 9920486d9f9Sleilk.liu if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio) || 9930486d9f9Sleilk.liu !spi->controller->set_cs_timing) { 99486527bcbSAndy Shevchenko if (!activate) 9958c33ebfeSMason Zhang spi_delay_exec(&spi->cs_inactive, NULL); 99625093bdeSAlexandru Ardelean } 997b158935fSMark Brown } 998b158935fSMark Brown 9992de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA 100046336966SBoris Brezillon int spi_map_buf(struct spi_controller *ctlr, struct device *dev, 10016ad45a27SMark Brown struct sg_table *sgt, void *buf, size_t len, 10026ad45a27SMark Brown enum dma_data_direction dir) 10036ad45a27SMark Brown { 10046ad45a27SMark Brown const bool vmalloced_buf = is_vmalloc_addr(buf); 1005df88e91bSAndy Shevchenko unsigned int max_seg_size = dma_get_max_seg_size(dev); 1006b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM 1007b1b8153cSVignesh R const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && 1008b1b8153cSVignesh R (unsigned long)buf < (PKMAP_BASE + 1009b1b8153cSVignesh R (LAST_PKMAP * PAGE_SIZE))); 1010b1b8153cSVignesh R #else 1011b1b8153cSVignesh R const bool kmap_buf = false; 1012b1b8153cSVignesh R #endif 101365598c13SAndrew Gabbasov int desc_len; 101465598c13SAndrew Gabbasov int sgs; 10156ad45a27SMark Brown struct page *vm_page; 10168dd4a016SJuan Gutierrez struct scatterlist *sg; 10176ad45a27SMark Brown void *sg_buf; 10186ad45a27SMark Brown size_t min; 10196ad45a27SMark Brown int i, ret; 10206ad45a27SMark Brown 1021b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 1022df88e91bSAndy Shevchenko desc_len = min_t(int, max_seg_size, PAGE_SIZE); 102365598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); 10240569a88fSVignesh R } else if (virt_addr_valid(buf)) { 10258caab75fSGeert Uytterhoeven desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); 102665598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len, desc_len); 10270569a88fSVignesh R } else { 10280569a88fSVignesh R return -EINVAL; 102965598c13SAndrew Gabbasov } 103065598c13SAndrew Gabbasov 10316ad45a27SMark Brown ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); 10326ad45a27SMark Brown if (ret != 0) 10336ad45a27SMark Brown return ret; 10346ad45a27SMark Brown 10358dd4a016SJuan Gutierrez sg = &sgt->sgl[0]; 10366ad45a27SMark Brown for (i = 0; i < sgs; i++) { 10376ad45a27SMark Brown 1038b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 1039ce99319aSMaxime Chevallier /* 1040ce99319aSMaxime Chevallier * Next scatterlist entry size is the minimum between 1041ce99319aSMaxime Chevallier * the desc_len and the remaining buffer length that 1042ce99319aSMaxime Chevallier * fits in a page. 1043ce99319aSMaxime Chevallier */ 1044ce99319aSMaxime Chevallier min = min_t(size_t, desc_len, 1045ce99319aSMaxime Chevallier min_t(size_t, len, 1046ce99319aSMaxime Chevallier PAGE_SIZE - offset_in_page(buf))); 1047b1b8153cSVignesh R if (vmalloced_buf) 10486ad45a27SMark Brown vm_page = vmalloc_to_page(buf); 1049b1b8153cSVignesh R else 1050b1b8153cSVignesh R vm_page = kmap_to_page(buf); 10516ad45a27SMark Brown if (!vm_page) { 10526ad45a27SMark Brown sg_free_table(sgt); 10536ad45a27SMark Brown return -ENOMEM; 10546ad45a27SMark Brown } 10558dd4a016SJuan Gutierrez sg_set_page(sg, vm_page, 1056c1aefbddSCharles Keepax min, offset_in_page(buf)); 10576ad45a27SMark Brown } else { 105865598c13SAndrew Gabbasov min = min_t(size_t, len, desc_len); 10596ad45a27SMark Brown sg_buf = buf; 10608dd4a016SJuan Gutierrez sg_set_buf(sg, sg_buf, min); 10616ad45a27SMark Brown } 10626ad45a27SMark Brown 10636ad45a27SMark Brown buf += min; 10646ad45a27SMark Brown len -= min; 10658dd4a016SJuan Gutierrez sg = sg_next(sg); 10666ad45a27SMark Brown } 10676ad45a27SMark Brown 10686ad45a27SMark Brown ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); 106989e4b66aSGeert Uytterhoeven if (!ret) 107089e4b66aSGeert Uytterhoeven ret = -ENOMEM; 10716ad45a27SMark Brown if (ret < 0) { 10726ad45a27SMark Brown sg_free_table(sgt); 10736ad45a27SMark Brown return ret; 10746ad45a27SMark Brown } 10756ad45a27SMark Brown 10766ad45a27SMark Brown sgt->nents = ret; 10776ad45a27SMark Brown 10786ad45a27SMark Brown return 0; 10796ad45a27SMark Brown } 10806ad45a27SMark Brown 108146336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, 10826ad45a27SMark Brown struct sg_table *sgt, enum dma_data_direction dir) 10836ad45a27SMark Brown { 10846ad45a27SMark Brown if (sgt->orig_nents) { 10856ad45a27SMark Brown dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); 10866ad45a27SMark Brown sg_free_table(sgt); 10876ad45a27SMark Brown } 10886ad45a27SMark Brown } 10896ad45a27SMark Brown 10908caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 109199adef31SMark Brown { 109299adef31SMark Brown struct device *tx_dev, *rx_dev; 109399adef31SMark Brown struct spi_transfer *xfer; 10946ad45a27SMark Brown int ret; 10953a2eba9bSMark Brown 10968caab75fSGeert Uytterhoeven if (!ctlr->can_dma) 109799adef31SMark Brown return 0; 109899adef31SMark Brown 10998caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 11008caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 1101b470e10eSVinod Koul else if (ctlr->dma_map_dev) 1102b470e10eSVinod Koul tx_dev = ctlr->dma_map_dev; 1103c37f45b5SLeilk Liu else 11048caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 1105c37f45b5SLeilk Liu 11068caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 11078caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 1108b470e10eSVinod Koul else if (ctlr->dma_map_dev) 1109b470e10eSVinod Koul rx_dev = ctlr->dma_map_dev; 1110c37f45b5SLeilk Liu else 11118caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 111299adef31SMark Brown 111399adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 11148caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 111599adef31SMark Brown continue; 111699adef31SMark Brown 111799adef31SMark Brown if (xfer->tx_buf != NULL) { 11188caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, 11196ad45a27SMark Brown (void *)xfer->tx_buf, xfer->len, 112099adef31SMark Brown DMA_TO_DEVICE); 11216ad45a27SMark Brown if (ret != 0) 11226ad45a27SMark Brown return ret; 112399adef31SMark Brown } 112499adef31SMark Brown 112599adef31SMark Brown if (xfer->rx_buf != NULL) { 11268caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, 112799adef31SMark Brown xfer->rx_buf, xfer->len, 112899adef31SMark Brown DMA_FROM_DEVICE); 11296ad45a27SMark Brown if (ret != 0) { 11308caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, 11316ad45a27SMark Brown DMA_TO_DEVICE); 11326ad45a27SMark Brown return ret; 113399adef31SMark Brown } 113499adef31SMark Brown } 113599adef31SMark Brown } 113699adef31SMark Brown 11378caab75fSGeert Uytterhoeven ctlr->cur_msg_mapped = true; 113899adef31SMark Brown 113999adef31SMark Brown return 0; 114099adef31SMark Brown } 114199adef31SMark Brown 11428caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg) 114399adef31SMark Brown { 114499adef31SMark Brown struct spi_transfer *xfer; 114599adef31SMark Brown struct device *tx_dev, *rx_dev; 114699adef31SMark Brown 11478caab75fSGeert Uytterhoeven if (!ctlr->cur_msg_mapped || !ctlr->can_dma) 114899adef31SMark Brown return 0; 114999adef31SMark Brown 11508caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 11518caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 1152c37f45b5SLeilk Liu else 11538caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 1154c37f45b5SLeilk Liu 11558caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 11568caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 1157c37f45b5SLeilk Liu else 11588caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 115999adef31SMark Brown 116099adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 11618caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 116299adef31SMark Brown continue; 116399adef31SMark Brown 11648caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 11658caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 116699adef31SMark Brown } 116799adef31SMark Brown 1168809b1b04SRobin Gong ctlr->cur_msg_mapped = false; 1169809b1b04SRobin Gong 117099adef31SMark Brown return 0; 117199adef31SMark Brown } 11722de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */ 11738caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr, 11742de440f5SGeert Uytterhoeven struct spi_message *msg) 11752de440f5SGeert Uytterhoeven { 11762de440f5SGeert Uytterhoeven return 0; 11772de440f5SGeert Uytterhoeven } 11782de440f5SGeert Uytterhoeven 11798caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr, 11802de440f5SGeert Uytterhoeven struct spi_message *msg) 11812de440f5SGeert Uytterhoeven { 11822de440f5SGeert Uytterhoeven return 0; 11832de440f5SGeert Uytterhoeven } 11842de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */ 11852de440f5SGeert Uytterhoeven 11868caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr, 11874b786458SMartin Sperl struct spi_message *msg) 11884b786458SMartin Sperl { 11894b786458SMartin Sperl struct spi_transfer *xfer; 11904b786458SMartin Sperl 11914b786458SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 11924b786458SMartin Sperl /* 11934b786458SMartin Sperl * Restore the original value of tx_buf or rx_buf if they are 11944b786458SMartin Sperl * NULL. 11954b786458SMartin Sperl */ 11968caab75fSGeert Uytterhoeven if (xfer->tx_buf == ctlr->dummy_tx) 11974b786458SMartin Sperl xfer->tx_buf = NULL; 11988caab75fSGeert Uytterhoeven if (xfer->rx_buf == ctlr->dummy_rx) 11994b786458SMartin Sperl xfer->rx_buf = NULL; 12004b786458SMartin Sperl } 12014b786458SMartin Sperl 12028caab75fSGeert Uytterhoeven return __spi_unmap_msg(ctlr, msg); 12034b786458SMartin Sperl } 12044b786458SMartin Sperl 12058caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 12062de440f5SGeert Uytterhoeven { 12072de440f5SGeert Uytterhoeven struct spi_transfer *xfer; 12082de440f5SGeert Uytterhoeven void *tmp; 12092de440f5SGeert Uytterhoeven unsigned int max_tx, max_rx; 12102de440f5SGeert Uytterhoeven 1211aee67fe8Sdillon min if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) 1212aee67fe8Sdillon min && !(msg->spi->mode & SPI_3WIRE)) { 12132de440f5SGeert Uytterhoeven max_tx = 0; 12142de440f5SGeert Uytterhoeven max_rx = 0; 12152de440f5SGeert Uytterhoeven 12162de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, transfer_list) { 12178caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && 12182de440f5SGeert Uytterhoeven !xfer->tx_buf) 12192de440f5SGeert Uytterhoeven max_tx = max(xfer->len, max_tx); 12208caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && 12212de440f5SGeert Uytterhoeven !xfer->rx_buf) 12222de440f5SGeert Uytterhoeven max_rx = max(xfer->len, max_rx); 12232de440f5SGeert Uytterhoeven } 12242de440f5SGeert Uytterhoeven 12252de440f5SGeert Uytterhoeven if (max_tx) { 12268caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_tx, max_tx, 1227b00bab9dSAndy Shevchenko GFP_KERNEL | GFP_DMA | __GFP_ZERO); 12282de440f5SGeert Uytterhoeven if (!tmp) 12292de440f5SGeert Uytterhoeven return -ENOMEM; 12308caab75fSGeert Uytterhoeven ctlr->dummy_tx = tmp; 12312de440f5SGeert Uytterhoeven } 12322de440f5SGeert Uytterhoeven 12332de440f5SGeert Uytterhoeven if (max_rx) { 12348caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_rx, max_rx, 12352de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 12362de440f5SGeert Uytterhoeven if (!tmp) 12372de440f5SGeert Uytterhoeven return -ENOMEM; 12388caab75fSGeert Uytterhoeven ctlr->dummy_rx = tmp; 12392de440f5SGeert Uytterhoeven } 12402de440f5SGeert Uytterhoeven 12412de440f5SGeert Uytterhoeven if (max_tx || max_rx) { 12422de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, 12432de440f5SGeert Uytterhoeven transfer_list) { 12445442dcaaSChris Lesiak if (!xfer->len) 12455442dcaaSChris Lesiak continue; 12462de440f5SGeert Uytterhoeven if (!xfer->tx_buf) 12478caab75fSGeert Uytterhoeven xfer->tx_buf = ctlr->dummy_tx; 12482de440f5SGeert Uytterhoeven if (!xfer->rx_buf) 12498caab75fSGeert Uytterhoeven xfer->rx_buf = ctlr->dummy_rx; 12502de440f5SGeert Uytterhoeven } 12512de440f5SGeert Uytterhoeven } 12522de440f5SGeert Uytterhoeven } 12532de440f5SGeert Uytterhoeven 12548caab75fSGeert Uytterhoeven return __spi_map_msg(ctlr, msg); 12552de440f5SGeert Uytterhoeven } 125699adef31SMark Brown 1257810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr, 1258810923f3SLubomir Rintel struct spi_message *msg, 1259810923f3SLubomir Rintel struct spi_transfer *xfer) 1260810923f3SLubomir Rintel { 1261810923f3SLubomir Rintel struct spi_statistics *statm = &ctlr->statistics; 1262810923f3SLubomir Rintel struct spi_statistics *stats = &msg->spi->statistics; 12636170d077SXu Yilun u32 speed_hz = xfer->speed_hz; 126449686df5SColin Ian King unsigned long long ms; 1265810923f3SLubomir Rintel 1266810923f3SLubomir Rintel if (spi_controller_is_slave(ctlr)) { 1267810923f3SLubomir Rintel if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { 1268810923f3SLubomir Rintel dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); 1269810923f3SLubomir Rintel return -EINTR; 1270810923f3SLubomir Rintel } 1271810923f3SLubomir Rintel } else { 12726170d077SXu Yilun if (!speed_hz) 12736170d077SXu Yilun speed_hz = 100000; 12746170d077SXu Yilun 127586b8bff7SAndy Shevchenko /* 127686b8bff7SAndy Shevchenko * For each byte we wait for 8 cycles of the SPI clock. 127786b8bff7SAndy Shevchenko * Since speed is defined in Hz and we want milliseconds, 127886b8bff7SAndy Shevchenko * use respective multiplier, but before the division, 127986b8bff7SAndy Shevchenko * otherwise we may get 0 for short transfers. 128086b8bff7SAndy Shevchenko */ 128186b8bff7SAndy Shevchenko ms = 8LL * MSEC_PER_SEC * xfer->len; 12826170d077SXu Yilun do_div(ms, speed_hz); 1283810923f3SLubomir Rintel 128486b8bff7SAndy Shevchenko /* 128586b8bff7SAndy Shevchenko * Increase it twice and add 200 ms tolerance, use 128686b8bff7SAndy Shevchenko * predefined maximum in case of overflow. 128786b8bff7SAndy Shevchenko */ 128886b8bff7SAndy Shevchenko ms += ms + 200; 1289810923f3SLubomir Rintel if (ms > UINT_MAX) 1290810923f3SLubomir Rintel ms = UINT_MAX; 1291810923f3SLubomir Rintel 1292810923f3SLubomir Rintel ms = wait_for_completion_timeout(&ctlr->xfer_completion, 1293810923f3SLubomir Rintel msecs_to_jiffies(ms)); 1294810923f3SLubomir Rintel 1295810923f3SLubomir Rintel if (ms == 0) { 1296810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(statm, timedout); 1297810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(stats, timedout); 1298810923f3SLubomir Rintel dev_err(&msg->spi->dev, 1299810923f3SLubomir Rintel "SPI transfer timed out\n"); 1300810923f3SLubomir Rintel return -ETIMEDOUT; 1301810923f3SLubomir Rintel } 1302810923f3SLubomir Rintel } 1303810923f3SLubomir Rintel 1304810923f3SLubomir Rintel return 0; 1305810923f3SLubomir Rintel } 1306810923f3SLubomir Rintel 13070ff2de8bSMartin Sperl static void _spi_transfer_delay_ns(u32 ns) 13080ff2de8bSMartin Sperl { 13090ff2de8bSMartin Sperl if (!ns) 13100ff2de8bSMartin Sperl return; 131186b8bff7SAndy Shevchenko if (ns <= NSEC_PER_USEC) { 13120ff2de8bSMartin Sperl ndelay(ns); 13130ff2de8bSMartin Sperl } else { 131486b8bff7SAndy Shevchenko u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC); 13150ff2de8bSMartin Sperl 13160ff2de8bSMartin Sperl if (us <= 10) 13170ff2de8bSMartin Sperl udelay(us); 13180ff2de8bSMartin Sperl else 13190ff2de8bSMartin Sperl usleep_range(us, us + DIV_ROUND_UP(us, 10)); 13200ff2de8bSMartin Sperl } 13210ff2de8bSMartin Sperl } 13220ff2de8bSMartin Sperl 13233984d39bSAlexandru Ardelean int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer) 13240ff2de8bSMartin Sperl { 1325b2c98153SAlexandru Ardelean u32 delay = _delay->value; 1326b2c98153SAlexandru Ardelean u32 unit = _delay->unit; 1327d5864e5bSMartin Sperl u32 hz; 13280ff2de8bSMartin Sperl 1329b2c98153SAlexandru Ardelean if (!delay) 1330b2c98153SAlexandru Ardelean return 0; 13310ff2de8bSMartin Sperl 13320ff2de8bSMartin Sperl switch (unit) { 13330ff2de8bSMartin Sperl case SPI_DELAY_UNIT_USECS: 133486b8bff7SAndy Shevchenko delay *= NSEC_PER_USEC; 13350ff2de8bSMartin Sperl break; 133686b8bff7SAndy Shevchenko case SPI_DELAY_UNIT_NSECS: 133786b8bff7SAndy Shevchenko /* Nothing to do here */ 13380ff2de8bSMartin Sperl break; 1339d5864e5bSMartin Sperl case SPI_DELAY_UNIT_SCK: 1340b2c98153SAlexandru Ardelean /* clock cycles need to be obtained from spi_transfer */ 1341b2c98153SAlexandru Ardelean if (!xfer) 1342b2c98153SAlexandru Ardelean return -EINVAL; 134386b8bff7SAndy Shevchenko /* 134486b8bff7SAndy Shevchenko * If there is unknown effective speed, approximate it 134586b8bff7SAndy Shevchenko * by underestimating with half of the requested hz. 1346d5864e5bSMartin Sperl */ 1347d5864e5bSMartin Sperl hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; 1348b2c98153SAlexandru Ardelean if (!hz) 1349b2c98153SAlexandru Ardelean return -EINVAL; 135086b8bff7SAndy Shevchenko 135186b8bff7SAndy Shevchenko /* Convert delay to nanoseconds */ 135286b8bff7SAndy Shevchenko delay *= DIV_ROUND_UP(NSEC_PER_SEC, hz); 1353d5864e5bSMartin Sperl break; 13540ff2de8bSMartin Sperl default: 1355b2c98153SAlexandru Ardelean return -EINVAL; 1356b2c98153SAlexandru Ardelean } 1357b2c98153SAlexandru Ardelean 1358b2c98153SAlexandru Ardelean return delay; 1359b2c98153SAlexandru Ardelean } 13603984d39bSAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_to_ns); 1361b2c98153SAlexandru Ardelean 1362b2c98153SAlexandru Ardelean int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer) 1363b2c98153SAlexandru Ardelean { 1364b2c98153SAlexandru Ardelean int delay; 1365b2c98153SAlexandru Ardelean 13668fede89fSMark Brown might_sleep(); 13678fede89fSMark Brown 1368b2c98153SAlexandru Ardelean if (!_delay) 1369b2c98153SAlexandru Ardelean return -EINVAL; 1370b2c98153SAlexandru Ardelean 13713984d39bSAlexandru Ardelean delay = spi_delay_to_ns(_delay, xfer); 1372b2c98153SAlexandru Ardelean if (delay < 0) 1373b2c98153SAlexandru Ardelean return delay; 1374b2c98153SAlexandru Ardelean 1375b2c98153SAlexandru Ardelean _spi_transfer_delay_ns(delay); 1376b2c98153SAlexandru Ardelean 1377b2c98153SAlexandru Ardelean return 0; 1378b2c98153SAlexandru Ardelean } 1379b2c98153SAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_exec); 1380b2c98153SAlexandru Ardelean 13810ff2de8bSMartin Sperl static void _spi_transfer_cs_change_delay(struct spi_message *msg, 13820ff2de8bSMartin Sperl struct spi_transfer *xfer) 13830ff2de8bSMartin Sperl { 138486b8bff7SAndy Shevchenko u32 default_delay_ns = 10 * NSEC_PER_USEC; 1385329f0dacSAlexandru Ardelean u32 delay = xfer->cs_change_delay.value; 1386329f0dacSAlexandru Ardelean u32 unit = xfer->cs_change_delay.unit; 1387329f0dacSAlexandru Ardelean int ret; 13880ff2de8bSMartin Sperl 13890ff2de8bSMartin Sperl /* return early on "fast" mode - for everything but USECS */ 13906b3f236aSAlexandru Ardelean if (!delay) { 13916b3f236aSAlexandru Ardelean if (unit == SPI_DELAY_UNIT_USECS) 139286b8bff7SAndy Shevchenko _spi_transfer_delay_ns(default_delay_ns); 13930ff2de8bSMartin Sperl return; 13946b3f236aSAlexandru Ardelean } 13950ff2de8bSMartin Sperl 1396329f0dacSAlexandru Ardelean ret = spi_delay_exec(&xfer->cs_change_delay, xfer); 1397329f0dacSAlexandru Ardelean if (ret) { 13980ff2de8bSMartin Sperl dev_err_once(&msg->spi->dev, 139986b8bff7SAndy Shevchenko "Use of unsupported delay unit %i, using default of %luus\n", 140086b8bff7SAndy Shevchenko unit, default_delay_ns / NSEC_PER_USEC); 140186b8bff7SAndy Shevchenko _spi_transfer_delay_ns(default_delay_ns); 14020ff2de8bSMartin Sperl } 14030ff2de8bSMartin Sperl } 14040ff2de8bSMartin Sperl 1405b158935fSMark Brown /* 1406b158935fSMark Brown * spi_transfer_one_message - Default implementation of transfer_one_message() 1407b158935fSMark Brown * 1408b158935fSMark Brown * This is a standard implementation of transfer_one_message() for 14098ba811a7SMoritz Fischer * drivers which implement a transfer_one() operation. It provides 1410b158935fSMark Brown * standard handling of delays and chip select management. 1411b158935fSMark Brown */ 14128caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr, 1413b158935fSMark Brown struct spi_message *msg) 1414b158935fSMark Brown { 1415b158935fSMark Brown struct spi_transfer *xfer; 1416b158935fSMark Brown bool keep_cs = false; 1417b158935fSMark Brown int ret = 0; 14188caab75fSGeert Uytterhoeven struct spi_statistics *statm = &ctlr->statistics; 1419eca2ebc7SMartin Sperl struct spi_statistics *stats = &msg->spi->statistics; 1420b158935fSMark Brown 1421d347b4aaSDavid Bauer spi_set_cs(msg->spi, true, false); 1422b158935fSMark Brown 1423eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, messages); 1424eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, messages); 1425eca2ebc7SMartin Sperl 1426b158935fSMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1427b158935fSMark Brown trace_spi_transfer_start(msg, xfer); 1428b158935fSMark Brown 14298caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(statm, xfer, ctlr); 14308caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(stats, xfer, ctlr); 1431eca2ebc7SMartin Sperl 1432b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 1433b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 1434b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1435b42faeeeSVladimir Oltean } 1436b42faeeeSVladimir Oltean 1437b3063203SNicolas Saenz Julienne if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) { 14388caab75fSGeert Uytterhoeven reinit_completion(&ctlr->xfer_completion); 1439b158935fSMark Brown 1440809b1b04SRobin Gong fallback_pio: 14418caab75fSGeert Uytterhoeven ret = ctlr->transfer_one(ctlr, msg->spi, xfer); 1442b158935fSMark Brown if (ret < 0) { 1443809b1b04SRobin Gong if (ctlr->cur_msg_mapped && 1444809b1b04SRobin Gong (xfer->error & SPI_TRANS_FAIL_NO_START)) { 1445809b1b04SRobin Gong __spi_unmap_msg(ctlr, msg); 1446809b1b04SRobin Gong ctlr->fallback = true; 1447809b1b04SRobin Gong xfer->error &= ~SPI_TRANS_FAIL_NO_START; 1448809b1b04SRobin Gong goto fallback_pio; 1449809b1b04SRobin Gong } 1450809b1b04SRobin Gong 1451eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, 1452eca2ebc7SMartin Sperl errors); 1453eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, 1454eca2ebc7SMartin Sperl errors); 1455b158935fSMark Brown dev_err(&msg->spi->dev, 1456b158935fSMark Brown "SPI transfer failed: %d\n", ret); 1457b158935fSMark Brown goto out; 1458b158935fSMark Brown } 1459b158935fSMark Brown 1460d57e7960SMark Brown if (ret > 0) { 1461810923f3SLubomir Rintel ret = spi_transfer_wait(ctlr, msg, xfer); 1462810923f3SLubomir Rintel if (ret < 0) 1463810923f3SLubomir Rintel msg->status = ret; 1464d57e7960SMark Brown } 146538ec10f6SMark Brown } else { 146638ec10f6SMark Brown if (xfer->len) 146738ec10f6SMark Brown dev_err(&msg->spi->dev, 146838ec10f6SMark Brown "Bufferless transfer has length %u\n", 146938ec10f6SMark Brown xfer->len); 147038ec10f6SMark Brown } 1471b158935fSMark Brown 1472b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 1473b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1474b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 1475b42faeeeSVladimir Oltean } 1476b42faeeeSVladimir Oltean 1477b158935fSMark Brown trace_spi_transfer_stop(msg, xfer); 1478b158935fSMark Brown 1479b158935fSMark Brown if (msg->status != -EINPROGRESS) 1480b158935fSMark Brown goto out; 1481b158935fSMark Brown 1482bebcfd27SAlexandru Ardelean spi_transfer_delay_exec(xfer); 1483b158935fSMark Brown 1484b158935fSMark Brown if (xfer->cs_change) { 1485b158935fSMark Brown if (list_is_last(&xfer->transfer_list, 1486b158935fSMark Brown &msg->transfers)) { 1487b158935fSMark Brown keep_cs = true; 1488b158935fSMark Brown } else { 1489d347b4aaSDavid Bauer spi_set_cs(msg->spi, false, false); 14900ff2de8bSMartin Sperl _spi_transfer_cs_change_delay(msg, xfer); 1491d347b4aaSDavid Bauer spi_set_cs(msg->spi, true, false); 1492b158935fSMark Brown } 1493b158935fSMark Brown } 1494b158935fSMark Brown 1495b158935fSMark Brown msg->actual_length += xfer->len; 1496b158935fSMark Brown } 1497b158935fSMark Brown 1498b158935fSMark Brown out: 1499b158935fSMark Brown if (ret != 0 || !keep_cs) 1500d347b4aaSDavid Bauer spi_set_cs(msg->spi, false, false); 1501b158935fSMark Brown 1502b158935fSMark Brown if (msg->status == -EINPROGRESS) 1503b158935fSMark Brown msg->status = ret; 1504b158935fSMark Brown 15058caab75fSGeert Uytterhoeven if (msg->status && ctlr->handle_err) 15068caab75fSGeert Uytterhoeven ctlr->handle_err(ctlr, msg); 1507b716c4ffSAndy Shevchenko 15080ed56252SMark Brown spi_finalize_current_message(ctlr); 15090ed56252SMark Brown 1510b158935fSMark Brown return ret; 1511b158935fSMark Brown } 1512b158935fSMark Brown 1513b158935fSMark Brown /** 1514b158935fSMark Brown * spi_finalize_current_transfer - report completion of a transfer 15158caab75fSGeert Uytterhoeven * @ctlr: the controller reporting completion 1516b158935fSMark Brown * 1517b158935fSMark Brown * Called by SPI drivers using the core transfer_one_message() 1518b158935fSMark Brown * implementation to notify it that the current interrupt driven 15199e8f4882SGeert Uytterhoeven * transfer has finished and the next one may be scheduled. 1520b158935fSMark Brown */ 15218caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr) 1522b158935fSMark Brown { 15238caab75fSGeert Uytterhoeven complete(&ctlr->xfer_completion); 1524b158935fSMark Brown } 1525b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); 1526b158935fSMark Brown 1527e1268597SMark Brown static void spi_idle_runtime_pm(struct spi_controller *ctlr) 1528e1268597SMark Brown { 1529e1268597SMark Brown if (ctlr->auto_runtime_pm) { 1530e1268597SMark Brown pm_runtime_mark_last_busy(ctlr->dev.parent); 1531e1268597SMark Brown pm_runtime_put_autosuspend(ctlr->dev.parent); 1532e1268597SMark Brown } 1533e1268597SMark Brown } 1534e1268597SMark Brown 1535ffbbdd21SLinus Walleij /** 1536fc9e0f71SMark Brown * __spi_pump_messages - function which processes spi message queue 15378caab75fSGeert Uytterhoeven * @ctlr: controller to process queue for 1538fc9e0f71SMark Brown * @in_kthread: true if we are in the context of the message pump thread 1539ffbbdd21SLinus Walleij * 1540ffbbdd21SLinus Walleij * This function checks if there is any spi message in the queue that 1541ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 1542ffbbdd21SLinus Walleij * and transfer each message. 1543ffbbdd21SLinus Walleij * 15440461a414SMark Brown * Note that it is called both from the kthread itself and also from 15450461a414SMark Brown * inside spi_sync(); the queue extraction handling at the top of the 15460461a414SMark Brown * function should deal with this safely. 1547ffbbdd21SLinus Walleij */ 15488caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1549ffbbdd21SLinus Walleij { 1550b42faeeeSVladimir Oltean struct spi_transfer *xfer; 1551d1c44c93SVladimir Oltean struct spi_message *msg; 1552ffbbdd21SLinus Walleij bool was_busy = false; 1553d1c44c93SVladimir Oltean unsigned long flags; 1554ffbbdd21SLinus Walleij int ret; 1555ffbbdd21SLinus Walleij 1556983aee5dSMark Brown /* Lock queue */ 15578caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1558983aee5dSMark Brown 1559983aee5dSMark Brown /* Make sure we are not already running a message */ 15608caab75fSGeert Uytterhoeven if (ctlr->cur_msg) { 15618caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1562983aee5dSMark Brown return; 1563983aee5dSMark Brown } 1564983aee5dSMark Brown 1565f0125f1aSMark Brown /* If another context is idling the device then defer */ 15668caab75fSGeert Uytterhoeven if (ctlr->idling) { 156760a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 15688caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 15690461a414SMark Brown return; 15700461a414SMark Brown } 15710461a414SMark Brown 1572983aee5dSMark Brown /* Check if the queue is idle */ 15738caab75fSGeert Uytterhoeven if (list_empty(&ctlr->queue) || !ctlr->running) { 15748caab75fSGeert Uytterhoeven if (!ctlr->busy) { 15758caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1576ffbbdd21SLinus Walleij return; 1577ffbbdd21SLinus Walleij } 1578fc9e0f71SMark Brown 1579e1268597SMark Brown /* Defer any non-atomic teardown to the thread */ 1580f0125f1aSMark Brown if (!in_kthread) { 1581e1268597SMark Brown if (!ctlr->dummy_rx && !ctlr->dummy_tx && 1582e1268597SMark Brown !ctlr->unprepare_transfer_hardware) { 1583e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1584e1268597SMark Brown ctlr->busy = false; 1585e1268597SMark Brown trace_spi_controller_idle(ctlr); 1586e1268597SMark Brown } else { 158760a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, 1588f0125f1aSMark Brown &ctlr->pump_messages); 1589e1268597SMark Brown } 1590f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1591f0125f1aSMark Brown return; 1592f0125f1aSMark Brown } 1593f0125f1aSMark Brown 1594f0125f1aSMark Brown ctlr->busy = false; 1595f0125f1aSMark Brown ctlr->idling = true; 1596f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1597f0125f1aSMark Brown 1598f0125f1aSMark Brown kfree(ctlr->dummy_rx); 1599f0125f1aSMark Brown ctlr->dummy_rx = NULL; 1600f0125f1aSMark Brown kfree(ctlr->dummy_tx); 1601f0125f1aSMark Brown ctlr->dummy_tx = NULL; 1602f0125f1aSMark Brown if (ctlr->unprepare_transfer_hardware && 1603f0125f1aSMark Brown ctlr->unprepare_transfer_hardware(ctlr)) 1604f0125f1aSMark Brown dev_err(&ctlr->dev, 1605f0125f1aSMark Brown "failed to unprepare transfer hardware\n"); 1606e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1607f0125f1aSMark Brown trace_spi_controller_idle(ctlr); 1608f0125f1aSMark Brown 1609f0125f1aSMark Brown spin_lock_irqsave(&ctlr->queue_lock, flags); 1610f0125f1aSMark Brown ctlr->idling = false; 16118caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1612ffbbdd21SLinus Walleij return; 1613ffbbdd21SLinus Walleij } 1614ffbbdd21SLinus Walleij 1615ffbbdd21SLinus Walleij /* Extract head of queue */ 1616d1c44c93SVladimir Oltean msg = list_first_entry(&ctlr->queue, struct spi_message, queue); 1617d1c44c93SVladimir Oltean ctlr->cur_msg = msg; 1618ffbbdd21SLinus Walleij 1619d1c44c93SVladimir Oltean list_del_init(&msg->queue); 16208caab75fSGeert Uytterhoeven if (ctlr->busy) 1621ffbbdd21SLinus Walleij was_busy = true; 1622ffbbdd21SLinus Walleij else 16238caab75fSGeert Uytterhoeven ctlr->busy = true; 16248caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1625ffbbdd21SLinus Walleij 16268caab75fSGeert Uytterhoeven mutex_lock(&ctlr->io_mutex); 1627ef4d96ecSMark Brown 16288caab75fSGeert Uytterhoeven if (!was_busy && ctlr->auto_runtime_pm) { 16298caab75fSGeert Uytterhoeven ret = pm_runtime_get_sync(ctlr->dev.parent); 163049834de2SMark Brown if (ret < 0) { 16317e48e23aSTony Lindgren pm_runtime_put_noidle(ctlr->dev.parent); 16328caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "Failed to power device: %d\n", 163349834de2SMark Brown ret); 16348caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 163549834de2SMark Brown return; 163649834de2SMark Brown } 163749834de2SMark Brown } 163849834de2SMark Brown 163956ec1978SMark Brown if (!was_busy) 16408caab75fSGeert Uytterhoeven trace_spi_controller_busy(ctlr); 164156ec1978SMark Brown 16428caab75fSGeert Uytterhoeven if (!was_busy && ctlr->prepare_transfer_hardware) { 16438caab75fSGeert Uytterhoeven ret = ctlr->prepare_transfer_hardware(ctlr); 1644ffbbdd21SLinus Walleij if (ret) { 16458caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 1646f3440d9aSSuper Liu "failed to prepare transfer hardware: %d\n", 1647f3440d9aSSuper Liu ret); 164849834de2SMark Brown 16498caab75fSGeert Uytterhoeven if (ctlr->auto_runtime_pm) 16508caab75fSGeert Uytterhoeven pm_runtime_put(ctlr->dev.parent); 1651f3440d9aSSuper Liu 1652d1c44c93SVladimir Oltean msg->status = ret; 1653f3440d9aSSuper Liu spi_finalize_current_message(ctlr); 1654f3440d9aSSuper Liu 16558caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 1656ffbbdd21SLinus Walleij return; 1657ffbbdd21SLinus Walleij } 1658ffbbdd21SLinus Walleij } 1659ffbbdd21SLinus Walleij 1660d1c44c93SVladimir Oltean trace_spi_message_start(msg); 166156ec1978SMark Brown 16628caab75fSGeert Uytterhoeven if (ctlr->prepare_message) { 1663d1c44c93SVladimir Oltean ret = ctlr->prepare_message(ctlr, msg); 16642841a5fcSMark Brown if (ret) { 16658caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to prepare message: %d\n", 16668caab75fSGeert Uytterhoeven ret); 1667d1c44c93SVladimir Oltean msg->status = ret; 16688caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 166949023d2eSJon Hunter goto out; 16702841a5fcSMark Brown } 16718caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = true; 16722841a5fcSMark Brown } 16732841a5fcSMark Brown 1674d1c44c93SVladimir Oltean ret = spi_map_msg(ctlr, msg); 167599adef31SMark Brown if (ret) { 1676d1c44c93SVladimir Oltean msg->status = ret; 16778caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 167849023d2eSJon Hunter goto out; 167999adef31SMark Brown } 168099adef31SMark Brown 1681b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1682b42faeeeSVladimir Oltean list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1683b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 1684b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1685b42faeeeSVladimir Oltean } 1686b42faeeeSVladimir Oltean } 1687b42faeeeSVladimir Oltean 1688d1c44c93SVladimir Oltean ret = ctlr->transfer_one_message(ctlr, msg); 1689ffbbdd21SLinus Walleij if (ret) { 16908caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 16911f802f82SGeert Uytterhoeven "failed to transfer one message from queue\n"); 169249023d2eSJon Hunter goto out; 1693ffbbdd21SLinus Walleij } 169449023d2eSJon Hunter 169549023d2eSJon Hunter out: 16968caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 169762826970SMark Brown 169862826970SMark Brown /* Prod the scheduler in case transfer_one() was busy waiting */ 169949023d2eSJon Hunter if (!ret) 170062826970SMark Brown cond_resched(); 1701ffbbdd21SLinus Walleij } 1702ffbbdd21SLinus Walleij 1703fc9e0f71SMark Brown /** 1704fc9e0f71SMark Brown * spi_pump_messages - kthread work function which processes spi message queue 17058caab75fSGeert Uytterhoeven * @work: pointer to kthread work struct contained in the controller struct 1706fc9e0f71SMark Brown */ 1707fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work) 1708fc9e0f71SMark Brown { 17098caab75fSGeert Uytterhoeven struct spi_controller *ctlr = 17108caab75fSGeert Uytterhoeven container_of(work, struct spi_controller, pump_messages); 1711fc9e0f71SMark Brown 17128caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, true); 1713fc9e0f71SMark Brown } 1714fc9e0f71SMark Brown 1715924b5867SDouglas Anderson /** 1716*350de7ceSAndy Shevchenko * spi_take_timestamp_pre - helper to collect the beginning of the TX timestamp 1717b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1718b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1719862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1720b42faeeeSVladimir Oltean * @irqs_off: If true, will disable IRQs and preemption for the duration of the 1721b42faeeeSVladimir Oltean * transfer, for less jitter in time measurement. Only compatible 1722b42faeeeSVladimir Oltean * with PIO drivers. If true, must follow up with 1723b42faeeeSVladimir Oltean * spi_take_timestamp_post or otherwise system will crash. 1724b42faeeeSVladimir Oltean * WARNING: for fully predictable results, the CPU frequency must 1725b42faeeeSVladimir Oltean * also be under control (governor). 1726*350de7ceSAndy Shevchenko * 1727*350de7ceSAndy Shevchenko * This is a helper for drivers to collect the beginning of the TX timestamp 1728*350de7ceSAndy Shevchenko * for the requested byte from the SPI transfer. The frequency with which this 1729*350de7ceSAndy Shevchenko * function must be called (once per word, once for the whole transfer, once 1730*350de7ceSAndy Shevchenko * per batch of words etc) is arbitrary as long as the @tx buffer offset is 1731*350de7ceSAndy Shevchenko * greater than or equal to the requested byte at the time of the call. The 1732*350de7ceSAndy Shevchenko * timestamp is only taken once, at the first such call. It is assumed that 1733*350de7ceSAndy Shevchenko * the driver advances its @tx buffer pointer monotonically. 1734b42faeeeSVladimir Oltean */ 1735b42faeeeSVladimir Oltean void spi_take_timestamp_pre(struct spi_controller *ctlr, 1736b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1737862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1738b42faeeeSVladimir Oltean { 1739b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1740b42faeeeSVladimir Oltean return; 1741b42faeeeSVladimir Oltean 17426a726824SVladimir Oltean if (xfer->timestamped) 1743b42faeeeSVladimir Oltean return; 1744b42faeeeSVladimir Oltean 17456a726824SVladimir Oltean if (progress > xfer->ptp_sts_word_pre) 1746b42faeeeSVladimir Oltean return; 1747b42faeeeSVladimir Oltean 1748b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1749862dd2a9SVladimir Oltean xfer->ptp_sts_word_pre = progress; 1750b42faeeeSVladimir Oltean 1751b42faeeeSVladimir Oltean if (irqs_off) { 1752b42faeeeSVladimir Oltean local_irq_save(ctlr->irq_flags); 1753b42faeeeSVladimir Oltean preempt_disable(); 1754b42faeeeSVladimir Oltean } 1755b42faeeeSVladimir Oltean 1756b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1757b42faeeeSVladimir Oltean } 1758b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_pre); 1759b42faeeeSVladimir Oltean 1760b42faeeeSVladimir Oltean /** 1761*350de7ceSAndy Shevchenko * spi_take_timestamp_post - helper to collect the end of the TX timestamp 1762b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1763b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1764862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1765b42faeeeSVladimir Oltean * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU. 1766*350de7ceSAndy Shevchenko * 1767*350de7ceSAndy Shevchenko * This is a helper for drivers to collect the end of the TX timestamp for 1768*350de7ceSAndy Shevchenko * the requested byte from the SPI transfer. Can be called with an arbitrary 1769*350de7ceSAndy Shevchenko * frequency: only the first call where @tx exceeds or is equal to the 1770*350de7ceSAndy Shevchenko * requested word will be timestamped. 1771b42faeeeSVladimir Oltean */ 1772b42faeeeSVladimir Oltean void spi_take_timestamp_post(struct spi_controller *ctlr, 1773b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1774862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1775b42faeeeSVladimir Oltean { 1776b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1777b42faeeeSVladimir Oltean return; 1778b42faeeeSVladimir Oltean 17796a726824SVladimir Oltean if (xfer->timestamped) 1780b42faeeeSVladimir Oltean return; 1781b42faeeeSVladimir Oltean 1782862dd2a9SVladimir Oltean if (progress < xfer->ptp_sts_word_post) 1783b42faeeeSVladimir Oltean return; 1784b42faeeeSVladimir Oltean 1785b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1786b42faeeeSVladimir Oltean 1787b42faeeeSVladimir Oltean if (irqs_off) { 1788b42faeeeSVladimir Oltean local_irq_restore(ctlr->irq_flags); 1789b42faeeeSVladimir Oltean preempt_enable(); 1790b42faeeeSVladimir Oltean } 1791b42faeeeSVladimir Oltean 1792b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1793862dd2a9SVladimir Oltean xfer->ptp_sts_word_post = progress; 1794b42faeeeSVladimir Oltean 17956a726824SVladimir Oltean xfer->timestamped = true; 1796b42faeeeSVladimir Oltean } 1797b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_post); 1798b42faeeeSVladimir Oltean 1799b42faeeeSVladimir Oltean /** 1800924b5867SDouglas Anderson * spi_set_thread_rt - set the controller to pump at realtime priority 1801924b5867SDouglas Anderson * @ctlr: controller to boost priority of 1802924b5867SDouglas Anderson * 1803924b5867SDouglas Anderson * This can be called because the controller requested realtime priority 1804924b5867SDouglas Anderson * (by setting the ->rt value before calling spi_register_controller()) or 1805924b5867SDouglas Anderson * because a device on the bus said that its transfers needed realtime 1806924b5867SDouglas Anderson * priority. 1807924b5867SDouglas Anderson * 1808924b5867SDouglas Anderson * NOTE: at the moment if any device on a bus says it needs realtime then 1809924b5867SDouglas Anderson * the thread will be at realtime priority for all transfers on that 1810924b5867SDouglas Anderson * controller. If this eventually becomes a problem we may see if we can 1811924b5867SDouglas Anderson * find a way to boost the priority only temporarily during relevant 1812924b5867SDouglas Anderson * transfers. 1813924b5867SDouglas Anderson */ 1814924b5867SDouglas Anderson static void spi_set_thread_rt(struct spi_controller *ctlr) 1815ffbbdd21SLinus Walleij { 1816924b5867SDouglas Anderson dev_info(&ctlr->dev, 1817924b5867SDouglas Anderson "will run message pump with realtime priority\n"); 18186d2b84a4SLinus Torvalds sched_set_fifo(ctlr->kworker->task); 1819924b5867SDouglas Anderson } 1820924b5867SDouglas Anderson 1821924b5867SDouglas Anderson static int spi_init_queue(struct spi_controller *ctlr) 1822924b5867SDouglas Anderson { 18238caab75fSGeert Uytterhoeven ctlr->running = false; 18248caab75fSGeert Uytterhoeven ctlr->busy = false; 1825ffbbdd21SLinus Walleij 182660a883d1SMarek Szyprowski ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); 182760a883d1SMarek Szyprowski if (IS_ERR(ctlr->kworker)) { 182860a883d1SMarek Szyprowski dev_err(&ctlr->dev, "failed to create message pump kworker\n"); 182960a883d1SMarek Szyprowski return PTR_ERR(ctlr->kworker); 1830ffbbdd21SLinus Walleij } 183160a883d1SMarek Szyprowski 18328caab75fSGeert Uytterhoeven kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1833f0125f1aSMark Brown 1834ffbbdd21SLinus Walleij /* 18358caab75fSGeert Uytterhoeven * Controller config will indicate if this controller should run the 1836ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 1837ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 1838ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 1839ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 1840ffbbdd21SLinus Walleij */ 1841924b5867SDouglas Anderson if (ctlr->rt) 1842924b5867SDouglas Anderson spi_set_thread_rt(ctlr); 1843ffbbdd21SLinus Walleij 1844ffbbdd21SLinus Walleij return 0; 1845ffbbdd21SLinus Walleij } 1846ffbbdd21SLinus Walleij 1847ffbbdd21SLinus Walleij /** 1848ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 1849ffbbdd21SLinus Walleij * messages 18508caab75fSGeert Uytterhoeven * @ctlr: the controller to check for queued messages 1851ffbbdd21SLinus Walleij * 1852ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 1853ffbbdd21SLinus Walleij * this call. 185497d56dc6SJavier Martinez Canillas * 185597d56dc6SJavier Martinez Canillas * Return: the next message in the queue, else NULL if the queue is empty. 1856ffbbdd21SLinus Walleij */ 18578caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1858ffbbdd21SLinus Walleij { 1859ffbbdd21SLinus Walleij struct spi_message *next; 1860ffbbdd21SLinus Walleij unsigned long flags; 1861ffbbdd21SLinus Walleij 1862ffbbdd21SLinus Walleij /* get a pointer to the next message, if any */ 18638caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 18648caab75fSGeert Uytterhoeven next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 18651cfd97f9SAxel Lin queue); 18668caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1867ffbbdd21SLinus Walleij 1868ffbbdd21SLinus Walleij return next; 1869ffbbdd21SLinus Walleij } 1870ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1871ffbbdd21SLinus Walleij 1872ffbbdd21SLinus Walleij /** 1873ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 18748caab75fSGeert Uytterhoeven * @ctlr: the controller to return the message to 1875ffbbdd21SLinus Walleij * 1876ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 1877ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 1878ffbbdd21SLinus Walleij */ 18798caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr) 1880ffbbdd21SLinus Walleij { 1881b42faeeeSVladimir Oltean struct spi_transfer *xfer; 1882ffbbdd21SLinus Walleij struct spi_message *mesg; 1883ffbbdd21SLinus Walleij unsigned long flags; 18842841a5fcSMark Brown int ret; 1885ffbbdd21SLinus Walleij 18868caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 18878caab75fSGeert Uytterhoeven mesg = ctlr->cur_msg; 18888caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1889ffbbdd21SLinus Walleij 1890b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1891b42faeeeSVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) { 1892b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1893b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 1894b42faeeeSVladimir Oltean } 1895b42faeeeSVladimir Oltean } 1896b42faeeeSVladimir Oltean 18976a726824SVladimir Oltean if (unlikely(ctlr->ptp_sts_supported)) 18986a726824SVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) 18996a726824SVladimir Oltean WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); 1900f971a207SVladimir Oltean 19018caab75fSGeert Uytterhoeven spi_unmap_msg(ctlr, mesg); 190299adef31SMark Brown 1903*350de7ceSAndy Shevchenko /* 1904*350de7ceSAndy Shevchenko * In the prepare_messages callback the SPI bus has the opportunity 1905*350de7ceSAndy Shevchenko * to split a transfer to smaller chunks. 1906*350de7ceSAndy Shevchenko * 1907*350de7ceSAndy Shevchenko * Release the split transfers here since spi_map_msg() is done on 1908*350de7ceSAndy Shevchenko * the split transfers. 1909b59a7ca1SGustav Wiklander */ 1910b59a7ca1SGustav Wiklander spi_res_release(ctlr, mesg); 1911b59a7ca1SGustav Wiklander 19128caab75fSGeert Uytterhoeven if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { 19138caab75fSGeert Uytterhoeven ret = ctlr->unprepare_message(ctlr, mesg); 19142841a5fcSMark Brown if (ret) { 19158caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 19168caab75fSGeert Uytterhoeven ret); 19172841a5fcSMark Brown } 19182841a5fcSMark Brown } 1919391949b6SUwe Kleine-König 19208caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 19218caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 19228caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = false; 1923809b1b04SRobin Gong ctlr->fallback = false; 192460a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 19258caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 19268e76ef88SMartin Sperl 19278e76ef88SMartin Sperl trace_spi_message_done(mesg); 19282841a5fcSMark Brown 1929ffbbdd21SLinus Walleij mesg->state = NULL; 1930ffbbdd21SLinus Walleij if (mesg->complete) 1931ffbbdd21SLinus Walleij mesg->complete(mesg->context); 1932ffbbdd21SLinus Walleij } 1933ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 1934ffbbdd21SLinus Walleij 19358caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr) 1936ffbbdd21SLinus Walleij { 1937ffbbdd21SLinus Walleij unsigned long flags; 1938ffbbdd21SLinus Walleij 19398caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1940ffbbdd21SLinus Walleij 19418caab75fSGeert Uytterhoeven if (ctlr->running || ctlr->busy) { 19428caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1943ffbbdd21SLinus Walleij return -EBUSY; 1944ffbbdd21SLinus Walleij } 1945ffbbdd21SLinus Walleij 19468caab75fSGeert Uytterhoeven ctlr->running = true; 19478caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 19488caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1949ffbbdd21SLinus Walleij 195060a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1951ffbbdd21SLinus Walleij 1952ffbbdd21SLinus Walleij return 0; 1953ffbbdd21SLinus Walleij } 1954ffbbdd21SLinus Walleij 19558caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr) 1956ffbbdd21SLinus Walleij { 1957ffbbdd21SLinus Walleij unsigned long flags; 1958ffbbdd21SLinus Walleij unsigned limit = 500; 1959ffbbdd21SLinus Walleij int ret = 0; 1960ffbbdd21SLinus Walleij 19618caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1962ffbbdd21SLinus Walleij 1963ffbbdd21SLinus Walleij /* 1964ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 19658caab75fSGeert Uytterhoeven * A wait_queue on the ctlr->busy could be used, but then the common 1966ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 1967ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 1968ffbbdd21SLinus Walleij */ 19698caab75fSGeert Uytterhoeven while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 19708caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1971f97b26b0SAxel Lin usleep_range(10000, 11000); 19728caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1973ffbbdd21SLinus Walleij } 1974ffbbdd21SLinus Walleij 19758caab75fSGeert Uytterhoeven if (!list_empty(&ctlr->queue) || ctlr->busy) 1976ffbbdd21SLinus Walleij ret = -EBUSY; 1977ffbbdd21SLinus Walleij else 19788caab75fSGeert Uytterhoeven ctlr->running = false; 1979ffbbdd21SLinus Walleij 19808caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1981ffbbdd21SLinus Walleij 1982ffbbdd21SLinus Walleij if (ret) { 19838caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "could not stop message queue\n"); 1984ffbbdd21SLinus Walleij return ret; 1985ffbbdd21SLinus Walleij } 1986ffbbdd21SLinus Walleij return ret; 1987ffbbdd21SLinus Walleij } 1988ffbbdd21SLinus Walleij 19898caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr) 1990ffbbdd21SLinus Walleij { 1991ffbbdd21SLinus Walleij int ret; 1992ffbbdd21SLinus Walleij 19938caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 1994ffbbdd21SLinus Walleij 1995ffbbdd21SLinus Walleij /* 19963989144fSPetr Mladek * kthread_flush_worker will block until all work is done. 1997ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 1998ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 1999ffbbdd21SLinus Walleij * return anyway. 2000ffbbdd21SLinus Walleij */ 2001ffbbdd21SLinus Walleij if (ret) { 20028caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem destroying queue\n"); 2003ffbbdd21SLinus Walleij return ret; 2004ffbbdd21SLinus Walleij } 2005ffbbdd21SLinus Walleij 200660a883d1SMarek Szyprowski kthread_destroy_worker(ctlr->kworker); 2007ffbbdd21SLinus Walleij 2008ffbbdd21SLinus Walleij return 0; 2009ffbbdd21SLinus Walleij } 2010ffbbdd21SLinus Walleij 20110461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi, 20120461a414SMark Brown struct spi_message *msg, 20130461a414SMark Brown bool need_pump) 2014ffbbdd21SLinus Walleij { 20158caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 2016ffbbdd21SLinus Walleij unsigned long flags; 2017ffbbdd21SLinus Walleij 20188caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 2019ffbbdd21SLinus Walleij 20208caab75fSGeert Uytterhoeven if (!ctlr->running) { 20218caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2022ffbbdd21SLinus Walleij return -ESHUTDOWN; 2023ffbbdd21SLinus Walleij } 2024ffbbdd21SLinus Walleij msg->actual_length = 0; 2025ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 2026ffbbdd21SLinus Walleij 20278caab75fSGeert Uytterhoeven list_add_tail(&msg->queue, &ctlr->queue); 2028f0125f1aSMark Brown if (!ctlr->busy && need_pump) 202960a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 2030ffbbdd21SLinus Walleij 20318caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2032ffbbdd21SLinus Walleij return 0; 2033ffbbdd21SLinus Walleij } 2034ffbbdd21SLinus Walleij 20350461a414SMark Brown /** 20360461a414SMark Brown * spi_queued_transfer - transfer function for queued transfers 20370461a414SMark Brown * @spi: spi device which is requesting transfer 20380461a414SMark Brown * @msg: spi message which is to handled is queued to driver queue 203997d56dc6SJavier Martinez Canillas * 204097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 20410461a414SMark Brown */ 20420461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 20430461a414SMark Brown { 20440461a414SMark Brown return __spi_queued_transfer(spi, msg, true); 20450461a414SMark Brown } 20460461a414SMark Brown 20478caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr) 2048ffbbdd21SLinus Walleij { 2049ffbbdd21SLinus Walleij int ret; 2050ffbbdd21SLinus Walleij 20518caab75fSGeert Uytterhoeven ctlr->transfer = spi_queued_transfer; 20528caab75fSGeert Uytterhoeven if (!ctlr->transfer_one_message) 20538caab75fSGeert Uytterhoeven ctlr->transfer_one_message = spi_transfer_one_message; 2054ffbbdd21SLinus Walleij 2055ffbbdd21SLinus Walleij /* Initialize and start queue */ 20568caab75fSGeert Uytterhoeven ret = spi_init_queue(ctlr); 2057ffbbdd21SLinus Walleij if (ret) { 20588caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem initializing queue\n"); 2059ffbbdd21SLinus Walleij goto err_init_queue; 2060ffbbdd21SLinus Walleij } 20618caab75fSGeert Uytterhoeven ctlr->queued = true; 20628caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 2063ffbbdd21SLinus Walleij if (ret) { 20648caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem starting queue\n"); 2065ffbbdd21SLinus Walleij goto err_start_queue; 2066ffbbdd21SLinus Walleij } 2067ffbbdd21SLinus Walleij 2068ffbbdd21SLinus Walleij return 0; 2069ffbbdd21SLinus Walleij 2070ffbbdd21SLinus Walleij err_start_queue: 20718caab75fSGeert Uytterhoeven spi_destroy_queue(ctlr); 2072c3676d5cSMark Brown err_init_queue: 2073ffbbdd21SLinus Walleij return ret; 2074ffbbdd21SLinus Walleij } 2075ffbbdd21SLinus Walleij 2076988f259bSBoris Brezillon /** 2077988f259bSBoris Brezillon * spi_flush_queue - Send all pending messages in the queue from the callers' 2078988f259bSBoris Brezillon * context 2079988f259bSBoris Brezillon * @ctlr: controller to process queue for 2080988f259bSBoris Brezillon * 2081988f259bSBoris Brezillon * This should be used when one wants to ensure all pending messages have been 2082988f259bSBoris Brezillon * sent before doing something. Is used by the spi-mem code to make sure SPI 2083988f259bSBoris Brezillon * memory operations do not preempt regular SPI transfers that have been queued 2084988f259bSBoris Brezillon * before the spi-mem operation. 2085988f259bSBoris Brezillon */ 2086988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr) 2087988f259bSBoris Brezillon { 2088988f259bSBoris Brezillon if (ctlr->transfer == spi_queued_transfer) 2089988f259bSBoris Brezillon __spi_pump_messages(ctlr, false); 2090988f259bSBoris Brezillon } 2091988f259bSBoris Brezillon 2092ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 2093ffbbdd21SLinus Walleij 20947cb94361SAndreas Larsson #if defined(CONFIG_OF) 20958caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 2096c2e51ac3SGeert Uytterhoeven struct device_node *nc) 2097d57a4282SGrant Likely { 209889da4293STrent Piepho u32 value; 2099c2e51ac3SGeert Uytterhoeven int rc; 2100d57a4282SGrant Likely 2101d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 2102e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpha")) 2103d57a4282SGrant Likely spi->mode |= SPI_CPHA; 2104e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpol")) 2105d57a4282SGrant Likely spi->mode |= SPI_CPOL; 2106e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-3wire")) 2107c20151dfSLars-Peter Clausen spi->mode |= SPI_3WIRE; 2108e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-lsb-first")) 2109cd6339e6SZhao Qiang spi->mode |= SPI_LSB_FIRST; 21103e5ec1dbSGregory CLEMENT if (of_property_read_bool(nc, "spi-cs-high")) 2111f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 2112f3186dd8SLinus Walleij 2113f477b7fbSwangyuhang /* Device DUAL/QUAD mode */ 211489da4293STrent Piepho if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 211589da4293STrent Piepho switch (value) { 2116d962608cSDragos Bogdan case 0: 2117d962608cSDragos Bogdan spi->mode |= SPI_NO_TX; 2118d962608cSDragos Bogdan break; 211989da4293STrent Piepho case 1: 2120f477b7fbSwangyuhang break; 212189da4293STrent Piepho case 2: 2122f477b7fbSwangyuhang spi->mode |= SPI_TX_DUAL; 2123f477b7fbSwangyuhang break; 212489da4293STrent Piepho case 4: 2125f477b7fbSwangyuhang spi->mode |= SPI_TX_QUAD; 2126f477b7fbSwangyuhang break; 21276b03061fSYogesh Narayan Gaur case 8: 21286b03061fSYogesh Narayan Gaur spi->mode |= SPI_TX_OCTAL; 21296b03061fSYogesh Narayan Gaur break; 2130f477b7fbSwangyuhang default: 21318caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 2132a110f93dSwangyuhang "spi-tx-bus-width %d not supported\n", 213389da4293STrent Piepho value); 213480874d8cSGeert Uytterhoeven break; 2135f477b7fbSwangyuhang } 2136a822e99cSMark Brown } 2137f477b7fbSwangyuhang 213889da4293STrent Piepho if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 213989da4293STrent Piepho switch (value) { 2140d962608cSDragos Bogdan case 0: 2141d962608cSDragos Bogdan spi->mode |= SPI_NO_RX; 2142d962608cSDragos Bogdan break; 214389da4293STrent Piepho case 1: 2144f477b7fbSwangyuhang break; 214589da4293STrent Piepho case 2: 2146f477b7fbSwangyuhang spi->mode |= SPI_RX_DUAL; 2147f477b7fbSwangyuhang break; 214889da4293STrent Piepho case 4: 2149f477b7fbSwangyuhang spi->mode |= SPI_RX_QUAD; 2150f477b7fbSwangyuhang break; 21516b03061fSYogesh Narayan Gaur case 8: 21526b03061fSYogesh Narayan Gaur spi->mode |= SPI_RX_OCTAL; 21536b03061fSYogesh Narayan Gaur break; 2154f477b7fbSwangyuhang default: 21558caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 2156a110f93dSwangyuhang "spi-rx-bus-width %d not supported\n", 215789da4293STrent Piepho value); 215880874d8cSGeert Uytterhoeven break; 2159f477b7fbSwangyuhang } 2160a822e99cSMark Brown } 2161f477b7fbSwangyuhang 21628caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr)) { 2163194276b0SRob Herring if (!of_node_name_eq(nc, "slave")) { 216425c56c88SRob Herring dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 216525c56c88SRob Herring nc); 21666c364062SGeert Uytterhoeven return -EINVAL; 21676c364062SGeert Uytterhoeven } 21686c364062SGeert Uytterhoeven return 0; 21696c364062SGeert Uytterhoeven } 21706c364062SGeert Uytterhoeven 21716c364062SGeert Uytterhoeven /* Device address */ 21726c364062SGeert Uytterhoeven rc = of_property_read_u32(nc, "reg", &value); 21736c364062SGeert Uytterhoeven if (rc) { 217425c56c88SRob Herring dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 217525c56c88SRob Herring nc, rc); 21766c364062SGeert Uytterhoeven return rc; 21776c364062SGeert Uytterhoeven } 21786c364062SGeert Uytterhoeven spi->chip_select = value; 21796c364062SGeert Uytterhoeven 2180d57a4282SGrant Likely /* Device speed */ 2181671c3bf5SChuanhong Guo if (!of_property_read_u32(nc, "spi-max-frequency", &value)) 218289da4293STrent Piepho spi->max_speed_hz = value; 2183d57a4282SGrant Likely 2184c2e51ac3SGeert Uytterhoeven return 0; 2185c2e51ac3SGeert Uytterhoeven } 2186c2e51ac3SGeert Uytterhoeven 2187c2e51ac3SGeert Uytterhoeven static struct spi_device * 21888caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 2189c2e51ac3SGeert Uytterhoeven { 2190c2e51ac3SGeert Uytterhoeven struct spi_device *spi; 2191c2e51ac3SGeert Uytterhoeven int rc; 2192c2e51ac3SGeert Uytterhoeven 2193c2e51ac3SGeert Uytterhoeven /* Alloc an spi_device */ 21948caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 2195c2e51ac3SGeert Uytterhoeven if (!spi) { 219625c56c88SRob Herring dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 2197c2e51ac3SGeert Uytterhoeven rc = -ENOMEM; 2198c2e51ac3SGeert Uytterhoeven goto err_out; 2199c2e51ac3SGeert Uytterhoeven } 2200c2e51ac3SGeert Uytterhoeven 2201c2e51ac3SGeert Uytterhoeven /* Select device driver */ 2202c2e51ac3SGeert Uytterhoeven rc = of_modalias_node(nc, spi->modalias, 2203c2e51ac3SGeert Uytterhoeven sizeof(spi->modalias)); 2204c2e51ac3SGeert Uytterhoeven if (rc < 0) { 220525c56c88SRob Herring dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 2206c2e51ac3SGeert Uytterhoeven goto err_out; 2207c2e51ac3SGeert Uytterhoeven } 2208c2e51ac3SGeert Uytterhoeven 22098caab75fSGeert Uytterhoeven rc = of_spi_parse_dt(ctlr, spi, nc); 2210c2e51ac3SGeert Uytterhoeven if (rc) 2211c2e51ac3SGeert Uytterhoeven goto err_out; 2212c2e51ac3SGeert Uytterhoeven 2213d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 2214d57a4282SGrant Likely of_node_get(nc); 2215d57a4282SGrant Likely spi->dev.of_node = nc; 22160e793ba7SCharles Keepax spi->dev.fwnode = of_fwnode_handle(nc); 2217d57a4282SGrant Likely 2218d57a4282SGrant Likely /* Register the new device */ 2219d57a4282SGrant Likely rc = spi_add_device(spi); 2220d57a4282SGrant Likely if (rc) { 222125c56c88SRob Herring dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 22228324147fSJohan Hovold goto err_of_node_put; 2223d57a4282SGrant Likely } 2224d57a4282SGrant Likely 2225aff5e3f8SPantelis Antoniou return spi; 2226aff5e3f8SPantelis Antoniou 22278324147fSJohan Hovold err_of_node_put: 22288324147fSJohan Hovold of_node_put(nc); 2229aff5e3f8SPantelis Antoniou err_out: 2230aff5e3f8SPantelis Antoniou spi_dev_put(spi); 2231aff5e3f8SPantelis Antoniou return ERR_PTR(rc); 2232aff5e3f8SPantelis Antoniou } 2233aff5e3f8SPantelis Antoniou 2234aff5e3f8SPantelis Antoniou /** 2235aff5e3f8SPantelis Antoniou * of_register_spi_devices() - Register child devices onto the SPI bus 22368caab75fSGeert Uytterhoeven * @ctlr: Pointer to spi_controller device 2237aff5e3f8SPantelis Antoniou * 22386c364062SGeert Uytterhoeven * Registers an spi_device for each child node of controller node which 22396c364062SGeert Uytterhoeven * represents a valid SPI slave. 2240aff5e3f8SPantelis Antoniou */ 22418caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) 2242aff5e3f8SPantelis Antoniou { 2243aff5e3f8SPantelis Antoniou struct spi_device *spi; 2244aff5e3f8SPantelis Antoniou struct device_node *nc; 2245aff5e3f8SPantelis Antoniou 22468caab75fSGeert Uytterhoeven if (!ctlr->dev.of_node) 2247aff5e3f8SPantelis Antoniou return; 2248aff5e3f8SPantelis Antoniou 22498caab75fSGeert Uytterhoeven for_each_available_child_of_node(ctlr->dev.of_node, nc) { 2250bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(nc, OF_POPULATED)) 2251bd6c1644SGeert Uytterhoeven continue; 22528caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, nc); 2253e0af98a7SRalf Ramsauer if (IS_ERR(spi)) { 22548caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 225525c56c88SRob Herring "Failed to create SPI device for %pOF\n", nc); 2256e0af98a7SRalf Ramsauer of_node_clear_flag(nc, OF_POPULATED); 2257e0af98a7SRalf Ramsauer } 2258d57a4282SGrant Likely } 2259d57a4282SGrant Likely } 2260d57a4282SGrant Likely #else 22618caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { } 2262d57a4282SGrant Likely #endif 2263d57a4282SGrant Likely 22640c79378cSSebastian Reichel /** 22650c79378cSSebastian Reichel * spi_new_ancillary_device() - Register ancillary SPI device 22660c79378cSSebastian Reichel * @spi: Pointer to the main SPI device registering the ancillary device 22670c79378cSSebastian Reichel * @chip_select: Chip Select of the ancillary device 22680c79378cSSebastian Reichel * 22690c79378cSSebastian Reichel * Register an ancillary SPI device; for example some chips have a chip-select 22700c79378cSSebastian Reichel * for normal device usage and another one for setup/firmware upload. 22710c79378cSSebastian Reichel * 22720c79378cSSebastian Reichel * This may only be called from main SPI device's probe routine. 22730c79378cSSebastian Reichel * 22740c79378cSSebastian Reichel * Return: 0 on success; negative errno on failure 22750c79378cSSebastian Reichel */ 22760c79378cSSebastian Reichel struct spi_device *spi_new_ancillary_device(struct spi_device *spi, 22770c79378cSSebastian Reichel u8 chip_select) 22780c79378cSSebastian Reichel { 22790c79378cSSebastian Reichel struct spi_device *ancillary; 22800c79378cSSebastian Reichel int rc = 0; 22810c79378cSSebastian Reichel 22820c79378cSSebastian Reichel /* Alloc an spi_device */ 22830c79378cSSebastian Reichel ancillary = spi_alloc_device(spi->controller); 22840c79378cSSebastian Reichel if (!ancillary) { 22850c79378cSSebastian Reichel rc = -ENOMEM; 22860c79378cSSebastian Reichel goto err_out; 22870c79378cSSebastian Reichel } 22880c79378cSSebastian Reichel 22890c79378cSSebastian Reichel strlcpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias)); 22900c79378cSSebastian Reichel 22910c79378cSSebastian Reichel /* Use provided chip-select for ancillary device */ 22920c79378cSSebastian Reichel ancillary->chip_select = chip_select; 22930c79378cSSebastian Reichel 22940c79378cSSebastian Reichel /* Take over SPI mode/speed from SPI main device */ 22950c79378cSSebastian Reichel ancillary->max_speed_hz = spi->max_speed_hz; 2296b01d5506SColin Ian King ancillary->mode = spi->mode; 22970c79378cSSebastian Reichel 22980c79378cSSebastian Reichel /* Register the new device */ 22990c79378cSSebastian Reichel rc = spi_add_device_locked(ancillary); 23000c79378cSSebastian Reichel if (rc) { 23010c79378cSSebastian Reichel dev_err(&spi->dev, "failed to register ancillary device\n"); 23020c79378cSSebastian Reichel goto err_out; 23030c79378cSSebastian Reichel } 23040c79378cSSebastian Reichel 23050c79378cSSebastian Reichel return ancillary; 23060c79378cSSebastian Reichel 23070c79378cSSebastian Reichel err_out: 23080c79378cSSebastian Reichel spi_dev_put(ancillary); 23090c79378cSSebastian Reichel return ERR_PTR(rc); 23100c79378cSSebastian Reichel } 23110c79378cSSebastian Reichel EXPORT_SYMBOL_GPL(spi_new_ancillary_device); 23120c79378cSSebastian Reichel 231364bee4d2SMika Westerberg #ifdef CONFIG_ACPI 23144c3c5954SArd Biesheuvel struct acpi_spi_lookup { 23154c3c5954SArd Biesheuvel struct spi_controller *ctlr; 23164c3c5954SArd Biesheuvel u32 max_speed_hz; 23174c3c5954SArd Biesheuvel u32 mode; 23184c3c5954SArd Biesheuvel int irq; 23194c3c5954SArd Biesheuvel u8 bits_per_word; 23204c3c5954SArd Biesheuvel u8 chip_select; 23214c3c5954SArd Biesheuvel }; 23224c3c5954SArd Biesheuvel 23234c3c5954SArd Biesheuvel static void acpi_spi_parse_apple_properties(struct acpi_device *dev, 23244c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup) 23258a2e487eSLukas Wunner { 23268a2e487eSLukas Wunner const union acpi_object *obj; 23278a2e487eSLukas Wunner 23288a2e487eSLukas Wunner if (!x86_apple_machine) 23298a2e487eSLukas Wunner return; 23308a2e487eSLukas Wunner 23318a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 23328a2e487eSLukas Wunner && obj->buffer.length >= 4) 23334c3c5954SArd Biesheuvel lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 23348a2e487eSLukas Wunner 23358a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 23368a2e487eSLukas Wunner && obj->buffer.length == 8) 23374c3c5954SArd Biesheuvel lookup->bits_per_word = *(u64 *)obj->buffer.pointer; 23388a2e487eSLukas Wunner 23398a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 23408a2e487eSLukas Wunner && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 23414c3c5954SArd Biesheuvel lookup->mode |= SPI_LSB_FIRST; 23428a2e487eSLukas Wunner 23438a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 23448a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 23454c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 23468a2e487eSLukas Wunner 23478a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 23488a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 23494c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 23508a2e487eSLukas Wunner } 23518a2e487eSLukas Wunner 235264bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 235364bee4d2SMika Westerberg { 23544c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup = data; 23554c3c5954SArd Biesheuvel struct spi_controller *ctlr = lookup->ctlr; 235664bee4d2SMika Westerberg 235764bee4d2SMika Westerberg if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 235864bee4d2SMika Westerberg struct acpi_resource_spi_serialbus *sb; 23594c3c5954SArd Biesheuvel acpi_handle parent_handle; 23604c3c5954SArd Biesheuvel acpi_status status; 236164bee4d2SMika Westerberg 236264bee4d2SMika Westerberg sb = &ares->data.spi_serial_bus; 236364bee4d2SMika Westerberg if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 23644c3c5954SArd Biesheuvel 23654c3c5954SArd Biesheuvel status = acpi_get_handle(NULL, 23664c3c5954SArd Biesheuvel sb->resource_source.string_ptr, 23674c3c5954SArd Biesheuvel &parent_handle); 23684c3c5954SArd Biesheuvel 2369b5e3cf41SArd Biesheuvel if (ACPI_FAILURE(status) || 23704c3c5954SArd Biesheuvel ACPI_HANDLE(ctlr->dev.parent) != parent_handle) 23714c3c5954SArd Biesheuvel return -ENODEV; 23724c3c5954SArd Biesheuvel 2373a0a90718SMika Westerberg /* 2374a0a90718SMika Westerberg * ACPI DeviceSelection numbering is handled by the 2375a0a90718SMika Westerberg * host controller driver in Windows and can vary 2376a0a90718SMika Westerberg * from driver to driver. In Linux we always expect 2377a0a90718SMika Westerberg * 0 .. max - 1 so we need to ask the driver to 2378a0a90718SMika Westerberg * translate between the two schemes. 2379a0a90718SMika Westerberg */ 23808caab75fSGeert Uytterhoeven if (ctlr->fw_translate_cs) { 23818caab75fSGeert Uytterhoeven int cs = ctlr->fw_translate_cs(ctlr, 2382a0a90718SMika Westerberg sb->device_selection); 2383a0a90718SMika Westerberg if (cs < 0) 2384a0a90718SMika Westerberg return cs; 23854c3c5954SArd Biesheuvel lookup->chip_select = cs; 2386a0a90718SMika Westerberg } else { 23874c3c5954SArd Biesheuvel lookup->chip_select = sb->device_selection; 2388a0a90718SMika Westerberg } 2389a0a90718SMika Westerberg 23904c3c5954SArd Biesheuvel lookup->max_speed_hz = sb->connection_speed; 23910dadde34SAndy Shevchenko lookup->bits_per_word = sb->data_bit_length; 239264bee4d2SMika Westerberg 239364bee4d2SMika Westerberg if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 23944c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 239564bee4d2SMika Westerberg if (sb->clock_polarity == ACPI_SPI_START_HIGH) 23964c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 239764bee4d2SMika Westerberg if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 23984c3c5954SArd Biesheuvel lookup->mode |= SPI_CS_HIGH; 239964bee4d2SMika Westerberg } 24004c3c5954SArd Biesheuvel } else if (lookup->irq < 0) { 240164bee4d2SMika Westerberg struct resource r; 240264bee4d2SMika Westerberg 240364bee4d2SMika Westerberg if (acpi_dev_resource_interrupt(ares, 0, &r)) 24044c3c5954SArd Biesheuvel lookup->irq = r.start; 240564bee4d2SMika Westerberg } 240664bee4d2SMika Westerberg 240764bee4d2SMika Westerberg /* Always tell the ACPI core to skip this resource */ 240864bee4d2SMika Westerberg return 1; 240964bee4d2SMika Westerberg } 241064bee4d2SMika Westerberg 24118caab75fSGeert Uytterhoeven static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 24127f24467fSOctavian Purdila struct acpi_device *adev) 241364bee4d2SMika Westerberg { 24144c3c5954SArd Biesheuvel acpi_handle parent_handle = NULL; 241564bee4d2SMika Westerberg struct list_head resource_list; 2416b28944c6SArd Biesheuvel struct acpi_spi_lookup lookup = {}; 241764bee4d2SMika Westerberg struct spi_device *spi; 241864bee4d2SMika Westerberg int ret; 241964bee4d2SMika Westerberg 24207f24467fSOctavian Purdila if (acpi_bus_get_status(adev) || !adev->status.present || 24217f24467fSOctavian Purdila acpi_device_enumerated(adev)) 242264bee4d2SMika Westerberg return AE_OK; 242364bee4d2SMika Westerberg 24244c3c5954SArd Biesheuvel lookup.ctlr = ctlr; 24254c3c5954SArd Biesheuvel lookup.irq = -1; 24264c3c5954SArd Biesheuvel 24274c3c5954SArd Biesheuvel INIT_LIST_HEAD(&resource_list); 24284c3c5954SArd Biesheuvel ret = acpi_dev_get_resources(adev, &resource_list, 24294c3c5954SArd Biesheuvel acpi_spi_add_resource, &lookup); 24304c3c5954SArd Biesheuvel acpi_dev_free_resource_list(&resource_list); 24314c3c5954SArd Biesheuvel 24324c3c5954SArd Biesheuvel if (ret < 0) 24334c3c5954SArd Biesheuvel /* found SPI in _CRS but it points to another controller */ 24344c3c5954SArd Biesheuvel return AE_OK; 24354c3c5954SArd Biesheuvel 24364c3c5954SArd Biesheuvel if (!lookup.max_speed_hz && 243710e92724SBjorn Helgaas ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) && 24384c3c5954SArd Biesheuvel ACPI_HANDLE(ctlr->dev.parent) == parent_handle) { 24394c3c5954SArd Biesheuvel /* Apple does not use _CRS but nested devices for SPI slaves */ 24404c3c5954SArd Biesheuvel acpi_spi_parse_apple_properties(adev, &lookup); 24414c3c5954SArd Biesheuvel } 24424c3c5954SArd Biesheuvel 24434c3c5954SArd Biesheuvel if (!lookup.max_speed_hz) 24444c3c5954SArd Biesheuvel return AE_OK; 24454c3c5954SArd Biesheuvel 24468caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 244764bee4d2SMika Westerberg if (!spi) { 24488caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", 244964bee4d2SMika Westerberg dev_name(&adev->dev)); 245064bee4d2SMika Westerberg return AE_NO_MEMORY; 245164bee4d2SMika Westerberg } 245264bee4d2SMika Westerberg 2453ea235786SJohn Garry 24547b199811SRafael J. Wysocki ACPI_COMPANION_SET(&spi->dev, adev); 24554c3c5954SArd Biesheuvel spi->max_speed_hz = lookup.max_speed_hz; 2456ea235786SJohn Garry spi->mode |= lookup.mode; 24574c3c5954SArd Biesheuvel spi->irq = lookup.irq; 24584c3c5954SArd Biesheuvel spi->bits_per_word = lookup.bits_per_word; 24594c3c5954SArd Biesheuvel spi->chip_select = lookup.chip_select; 246064bee4d2SMika Westerberg 24610c6543f6SDan O'Donovan acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 24620c6543f6SDan O'Donovan sizeof(spi->modalias)); 24630c6543f6SDan O'Donovan 246433ada67dSChristophe RICARD if (spi->irq < 0) 246533ada67dSChristophe RICARD spi->irq = acpi_dev_gpio_irq_get(adev, 0); 246633ada67dSChristophe RICARD 24677f24467fSOctavian Purdila acpi_device_set_enumerated(adev); 24687f24467fSOctavian Purdila 246933cf00e5SMika Westerberg adev->power.flags.ignore_parent = true; 247064bee4d2SMika Westerberg if (spi_add_device(spi)) { 247133cf00e5SMika Westerberg adev->power.flags.ignore_parent = false; 24728caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 247364bee4d2SMika Westerberg dev_name(&adev->dev)); 247464bee4d2SMika Westerberg spi_dev_put(spi); 247564bee4d2SMika Westerberg } 247664bee4d2SMika Westerberg 247764bee4d2SMika Westerberg return AE_OK; 247864bee4d2SMika Westerberg } 247964bee4d2SMika Westerberg 24807f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 24817f24467fSOctavian Purdila void *data, void **return_value) 24827f24467fSOctavian Purdila { 24838caab75fSGeert Uytterhoeven struct spi_controller *ctlr = data; 24847f24467fSOctavian Purdila struct acpi_device *adev; 24857f24467fSOctavian Purdila 24867f24467fSOctavian Purdila if (acpi_bus_get_device(handle, &adev)) 24877f24467fSOctavian Purdila return AE_OK; 24887f24467fSOctavian Purdila 24898caab75fSGeert Uytterhoeven return acpi_register_spi_device(ctlr, adev); 24907f24467fSOctavian Purdila } 24917f24467fSOctavian Purdila 24924c3c5954SArd Biesheuvel #define SPI_ACPI_ENUMERATE_MAX_DEPTH 32 24934c3c5954SArd Biesheuvel 24948caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr) 249564bee4d2SMika Westerberg { 249664bee4d2SMika Westerberg acpi_status status; 249764bee4d2SMika Westerberg acpi_handle handle; 249864bee4d2SMika Westerberg 24998caab75fSGeert Uytterhoeven handle = ACPI_HANDLE(ctlr->dev.parent); 250064bee4d2SMika Westerberg if (!handle) 250164bee4d2SMika Westerberg return; 250264bee4d2SMika Westerberg 25034c3c5954SArd Biesheuvel status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 25044c3c5954SArd Biesheuvel SPI_ACPI_ENUMERATE_MAX_DEPTH, 25058caab75fSGeert Uytterhoeven acpi_spi_add_device, NULL, ctlr, NULL); 250664bee4d2SMika Westerberg if (ACPI_FAILURE(status)) 25078caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 250864bee4d2SMika Westerberg } 250964bee4d2SMika Westerberg #else 25108caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 251164bee4d2SMika Westerberg #endif /* CONFIG_ACPI */ 251264bee4d2SMika Westerberg 25138caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev) 25148ae12a0dSDavid Brownell { 25158caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 25168ae12a0dSDavid Brownell 25178caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 25188caab75fSGeert Uytterhoeven kfree(ctlr); 25198ae12a0dSDavid Brownell } 25208ae12a0dSDavid Brownell 25218ae12a0dSDavid Brownell static struct class spi_master_class = { 25228ae12a0dSDavid Brownell .name = "spi_master", 25238ae12a0dSDavid Brownell .owner = THIS_MODULE, 25248caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 2525eca2ebc7SMartin Sperl .dev_groups = spi_master_groups, 25268ae12a0dSDavid Brownell }; 25278ae12a0dSDavid Brownell 25286c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE 25296c364062SGeert Uytterhoeven /** 25306c364062SGeert Uytterhoeven * spi_slave_abort - abort the ongoing transfer request on an SPI slave 25316c364062SGeert Uytterhoeven * controller 25326c364062SGeert Uytterhoeven * @spi: device used for the current transfer 25336c364062SGeert Uytterhoeven */ 25346c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi) 25356c364062SGeert Uytterhoeven { 25368caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 25376c364062SGeert Uytterhoeven 25388caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 25398caab75fSGeert Uytterhoeven return ctlr->slave_abort(ctlr); 25406c364062SGeert Uytterhoeven 25416c364062SGeert Uytterhoeven return -ENOTSUPP; 25426c364062SGeert Uytterhoeven } 25436c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort); 25446c364062SGeert Uytterhoeven 25456c364062SGeert Uytterhoeven static int match_true(struct device *dev, void *data) 25466c364062SGeert Uytterhoeven { 25476c364062SGeert Uytterhoeven return 1; 25486c364062SGeert Uytterhoeven } 25496c364062SGeert Uytterhoeven 2550cc8b4659SGeert Uytterhoeven static ssize_t slave_show(struct device *dev, struct device_attribute *attr, 2551cc8b4659SGeert Uytterhoeven char *buf) 25526c364062SGeert Uytterhoeven { 25538caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 25548caab75fSGeert Uytterhoeven dev); 25556c364062SGeert Uytterhoeven struct device *child; 25566c364062SGeert Uytterhoeven 25576c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 25586c364062SGeert Uytterhoeven return sprintf(buf, "%s\n", 25596c364062SGeert Uytterhoeven child ? to_spi_device(child)->modalias : NULL); 25606c364062SGeert Uytterhoeven } 25616c364062SGeert Uytterhoeven 2562cc8b4659SGeert Uytterhoeven static ssize_t slave_store(struct device *dev, struct device_attribute *attr, 2563cc8b4659SGeert Uytterhoeven const char *buf, size_t count) 25646c364062SGeert Uytterhoeven { 25658caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 25668caab75fSGeert Uytterhoeven dev); 25676c364062SGeert Uytterhoeven struct spi_device *spi; 25686c364062SGeert Uytterhoeven struct device *child; 25696c364062SGeert Uytterhoeven char name[32]; 25706c364062SGeert Uytterhoeven int rc; 25716c364062SGeert Uytterhoeven 25726c364062SGeert Uytterhoeven rc = sscanf(buf, "%31s", name); 25736c364062SGeert Uytterhoeven if (rc != 1 || !name[0]) 25746c364062SGeert Uytterhoeven return -EINVAL; 25756c364062SGeert Uytterhoeven 25766c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 25776c364062SGeert Uytterhoeven if (child) { 25786c364062SGeert Uytterhoeven /* Remove registered slave */ 25796c364062SGeert Uytterhoeven device_unregister(child); 25806c364062SGeert Uytterhoeven put_device(child); 25816c364062SGeert Uytterhoeven } 25826c364062SGeert Uytterhoeven 25836c364062SGeert Uytterhoeven if (strcmp(name, "(null)")) { 25846c364062SGeert Uytterhoeven /* Register new slave */ 25856c364062SGeert Uytterhoeven spi = spi_alloc_device(ctlr); 25866c364062SGeert Uytterhoeven if (!spi) 25876c364062SGeert Uytterhoeven return -ENOMEM; 25886c364062SGeert Uytterhoeven 25896c364062SGeert Uytterhoeven strlcpy(spi->modalias, name, sizeof(spi->modalias)); 25906c364062SGeert Uytterhoeven 25916c364062SGeert Uytterhoeven rc = spi_add_device(spi); 25926c364062SGeert Uytterhoeven if (rc) { 25936c364062SGeert Uytterhoeven spi_dev_put(spi); 25946c364062SGeert Uytterhoeven return rc; 25956c364062SGeert Uytterhoeven } 25966c364062SGeert Uytterhoeven } 25976c364062SGeert Uytterhoeven 25986c364062SGeert Uytterhoeven return count; 25996c364062SGeert Uytterhoeven } 26006c364062SGeert Uytterhoeven 2601cc8b4659SGeert Uytterhoeven static DEVICE_ATTR_RW(slave); 26026c364062SGeert Uytterhoeven 26036c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = { 26046c364062SGeert Uytterhoeven &dev_attr_slave.attr, 26056c364062SGeert Uytterhoeven NULL, 26066c364062SGeert Uytterhoeven }; 26076c364062SGeert Uytterhoeven 26086c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = { 26096c364062SGeert Uytterhoeven .attrs = spi_slave_attrs, 26106c364062SGeert Uytterhoeven }; 26116c364062SGeert Uytterhoeven 26126c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = { 26138caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 26146c364062SGeert Uytterhoeven &spi_slave_group, 26156c364062SGeert Uytterhoeven NULL, 26166c364062SGeert Uytterhoeven }; 26176c364062SGeert Uytterhoeven 26186c364062SGeert Uytterhoeven static struct class spi_slave_class = { 26196c364062SGeert Uytterhoeven .name = "spi_slave", 26206c364062SGeert Uytterhoeven .owner = THIS_MODULE, 26218caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 26226c364062SGeert Uytterhoeven .dev_groups = spi_slave_groups, 26236c364062SGeert Uytterhoeven }; 26246c364062SGeert Uytterhoeven #else 26256c364062SGeert Uytterhoeven extern struct class spi_slave_class; /* dummy */ 26266c364062SGeert Uytterhoeven #endif 26278ae12a0dSDavid Brownell 26288ae12a0dSDavid Brownell /** 26296c364062SGeert Uytterhoeven * __spi_alloc_controller - allocate an SPI master or slave controller 26308ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 263133e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 2632229e6af1SLukas Wunner * memory is in the driver_data field of the returned device, accessible 2633229e6af1SLukas Wunner * with spi_controller_get_devdata(); the memory is cacheline aligned; 2634229e6af1SLukas Wunner * drivers granting DMA access to portions of their private data need to 2635229e6af1SLukas Wunner * round up @size using ALIGN(size, dma_get_cache_alignment()). 26366c364062SGeert Uytterhoeven * @slave: flag indicating whether to allocate an SPI master (false) or SPI 26376c364062SGeert Uytterhoeven * slave (true) controller 263833e34dc6SDavid Brownell * Context: can sleep 26398ae12a0dSDavid Brownell * 26406c364062SGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 26418ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 26428caab75fSGeert Uytterhoeven * an spi_controller structure, prior to calling spi_register_controller(). 26438ae12a0dSDavid Brownell * 264497d56dc6SJavier Martinez Canillas * This must be called from context that can sleep. 26458ae12a0dSDavid Brownell * 26466c364062SGeert Uytterhoeven * The caller is responsible for assigning the bus number and initializing the 26478caab75fSGeert Uytterhoeven * controller's methods before calling spi_register_controller(); and (after 26488caab75fSGeert Uytterhoeven * errors adding the device) calling spi_controller_put() to prevent a memory 26498caab75fSGeert Uytterhoeven * leak. 265097d56dc6SJavier Martinez Canillas * 26516c364062SGeert Uytterhoeven * Return: the SPI controller structure on success, else NULL. 26528ae12a0dSDavid Brownell */ 26538caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev, 26546c364062SGeert Uytterhoeven unsigned int size, bool slave) 26558ae12a0dSDavid Brownell { 26568caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 2657229e6af1SLukas Wunner size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment()); 26588ae12a0dSDavid Brownell 26590c868461SDavid Brownell if (!dev) 26600c868461SDavid Brownell return NULL; 26610c868461SDavid Brownell 2662229e6af1SLukas Wunner ctlr = kzalloc(size + ctlr_size, GFP_KERNEL); 26638caab75fSGeert Uytterhoeven if (!ctlr) 26648ae12a0dSDavid Brownell return NULL; 26658ae12a0dSDavid Brownell 26668caab75fSGeert Uytterhoeven device_initialize(&ctlr->dev); 266716a8e2fbSUwe Kleine-König INIT_LIST_HEAD(&ctlr->queue); 266816a8e2fbSUwe Kleine-König spin_lock_init(&ctlr->queue_lock); 266916a8e2fbSUwe Kleine-König spin_lock_init(&ctlr->bus_lock_spinlock); 267016a8e2fbSUwe Kleine-König mutex_init(&ctlr->bus_lock_mutex); 267116a8e2fbSUwe Kleine-König mutex_init(&ctlr->io_mutex); 267216a8e2fbSUwe Kleine-König mutex_init(&ctlr->add_lock); 26738caab75fSGeert Uytterhoeven ctlr->bus_num = -1; 26748caab75fSGeert Uytterhoeven ctlr->num_chipselect = 1; 26758caab75fSGeert Uytterhoeven ctlr->slave = slave; 26766c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 26778caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_slave_class; 26786c364062SGeert Uytterhoeven else 26798caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_master_class; 26808caab75fSGeert Uytterhoeven ctlr->dev.parent = dev; 26818caab75fSGeert Uytterhoeven pm_suspend_ignore_children(&ctlr->dev, true); 2682229e6af1SLukas Wunner spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size); 26838ae12a0dSDavid Brownell 26848caab75fSGeert Uytterhoeven return ctlr; 26858ae12a0dSDavid Brownell } 26866c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller); 26878ae12a0dSDavid Brownell 26885e844cc3SLukas Wunner static void devm_spi_release_controller(struct device *dev, void *ctlr) 26895e844cc3SLukas Wunner { 26905e844cc3SLukas Wunner spi_controller_put(*(struct spi_controller **)ctlr); 26915e844cc3SLukas Wunner } 26925e844cc3SLukas Wunner 26935e844cc3SLukas Wunner /** 26945e844cc3SLukas Wunner * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller() 26955e844cc3SLukas Wunner * @dev: physical device of SPI controller 26965e844cc3SLukas Wunner * @size: how much zeroed driver-private data to allocate 26975e844cc3SLukas Wunner * @slave: whether to allocate an SPI master (false) or SPI slave (true) 26985e844cc3SLukas Wunner * Context: can sleep 26995e844cc3SLukas Wunner * 27005e844cc3SLukas Wunner * Allocate an SPI controller and automatically release a reference on it 27015e844cc3SLukas Wunner * when @dev is unbound from its driver. Drivers are thus relieved from 27025e844cc3SLukas Wunner * having to call spi_controller_put(). 27035e844cc3SLukas Wunner * 27045e844cc3SLukas Wunner * The arguments to this function are identical to __spi_alloc_controller(). 27055e844cc3SLukas Wunner * 27065e844cc3SLukas Wunner * Return: the SPI controller structure on success, else NULL. 27075e844cc3SLukas Wunner */ 27085e844cc3SLukas Wunner struct spi_controller *__devm_spi_alloc_controller(struct device *dev, 27095e844cc3SLukas Wunner unsigned int size, 27105e844cc3SLukas Wunner bool slave) 27115e844cc3SLukas Wunner { 27125e844cc3SLukas Wunner struct spi_controller **ptr, *ctlr; 27135e844cc3SLukas Wunner 27145e844cc3SLukas Wunner ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr), 27155e844cc3SLukas Wunner GFP_KERNEL); 27165e844cc3SLukas Wunner if (!ptr) 27175e844cc3SLukas Wunner return NULL; 27185e844cc3SLukas Wunner 27195e844cc3SLukas Wunner ctlr = __spi_alloc_controller(dev, size, slave); 27205e844cc3SLukas Wunner if (ctlr) { 2721794aaf01SWilliam A. Kennington III ctlr->devm_allocated = true; 27225e844cc3SLukas Wunner *ptr = ctlr; 27235e844cc3SLukas Wunner devres_add(dev, ptr); 27245e844cc3SLukas Wunner } else { 27255e844cc3SLukas Wunner devres_free(ptr); 27265e844cc3SLukas Wunner } 27275e844cc3SLukas Wunner 27285e844cc3SLukas Wunner return ctlr; 27295e844cc3SLukas Wunner } 27305e844cc3SLukas Wunner EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller); 27315e844cc3SLukas Wunner 273274317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF 273343004f31SLinus Walleij static int of_spi_get_gpio_numbers(struct spi_controller *ctlr) 273474317984SJean-Christophe PLAGNIOL-VILLARD { 2735e80beb27SGrant Likely int nb, i, *cs; 27368caab75fSGeert Uytterhoeven struct device_node *np = ctlr->dev.of_node; 273774317984SJean-Christophe PLAGNIOL-VILLARD 273874317984SJean-Christophe PLAGNIOL-VILLARD if (!np) 273974317984SJean-Christophe PLAGNIOL-VILLARD return 0; 274074317984SJean-Christophe PLAGNIOL-VILLARD 274174317984SJean-Christophe PLAGNIOL-VILLARD nb = of_gpio_named_count(np, "cs-gpios"); 27428caab75fSGeert Uytterhoeven ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 274374317984SJean-Christophe PLAGNIOL-VILLARD 27448ec5d84eSAndreas Larsson /* Return error only for an incorrectly formed cs-gpios property */ 27458ec5d84eSAndreas Larsson if (nb == 0 || nb == -ENOENT) 274674317984SJean-Christophe PLAGNIOL-VILLARD return 0; 27478ec5d84eSAndreas Larsson else if (nb < 0) 27488ec5d84eSAndreas Larsson return nb; 274974317984SJean-Christophe PLAGNIOL-VILLARD 2750a86854d0SKees Cook cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), 275174317984SJean-Christophe PLAGNIOL-VILLARD GFP_KERNEL); 27528caab75fSGeert Uytterhoeven ctlr->cs_gpios = cs; 275374317984SJean-Christophe PLAGNIOL-VILLARD 27548caab75fSGeert Uytterhoeven if (!ctlr->cs_gpios) 275574317984SJean-Christophe PLAGNIOL-VILLARD return -ENOMEM; 275674317984SJean-Christophe PLAGNIOL-VILLARD 27578caab75fSGeert Uytterhoeven for (i = 0; i < ctlr->num_chipselect; i++) 2758446411e1SAndreas Larsson cs[i] = -ENOENT; 275974317984SJean-Christophe PLAGNIOL-VILLARD 276074317984SJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < nb; i++) 276174317984SJean-Christophe PLAGNIOL-VILLARD cs[i] = of_get_named_gpio(np, "cs-gpios", i); 276274317984SJean-Christophe PLAGNIOL-VILLARD 276374317984SJean-Christophe PLAGNIOL-VILLARD return 0; 276474317984SJean-Christophe PLAGNIOL-VILLARD } 276574317984SJean-Christophe PLAGNIOL-VILLARD #else 276643004f31SLinus Walleij static int of_spi_get_gpio_numbers(struct spi_controller *ctlr) 276774317984SJean-Christophe PLAGNIOL-VILLARD { 276874317984SJean-Christophe PLAGNIOL-VILLARD return 0; 276974317984SJean-Christophe PLAGNIOL-VILLARD } 277074317984SJean-Christophe PLAGNIOL-VILLARD #endif 277174317984SJean-Christophe PLAGNIOL-VILLARD 2772f3186dd8SLinus Walleij /** 2773f3186dd8SLinus Walleij * spi_get_gpio_descs() - grab chip select GPIOs for the master 2774f3186dd8SLinus Walleij * @ctlr: The SPI master to grab GPIO descriptors for 2775f3186dd8SLinus Walleij */ 2776f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr) 2777f3186dd8SLinus Walleij { 2778f3186dd8SLinus Walleij int nb, i; 2779f3186dd8SLinus Walleij struct gpio_desc **cs; 2780f3186dd8SLinus Walleij struct device *dev = &ctlr->dev; 27817d93aecdSGeert Uytterhoeven unsigned long native_cs_mask = 0; 27827d93aecdSGeert Uytterhoeven unsigned int num_cs_gpios = 0; 2783f3186dd8SLinus Walleij 2784f3186dd8SLinus Walleij nb = gpiod_count(dev, "cs"); 278531ed8ebcSAndy Shevchenko if (nb < 0) { 2786f3186dd8SLinus Walleij /* No GPIOs at all is fine, else return the error */ 278731ed8ebcSAndy Shevchenko if (nb == -ENOENT) 2788f3186dd8SLinus Walleij return 0; 2789f3186dd8SLinus Walleij return nb; 279031ed8ebcSAndy Shevchenko } 279131ed8ebcSAndy Shevchenko 279231ed8ebcSAndy Shevchenko ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2793f3186dd8SLinus Walleij 2794f3186dd8SLinus Walleij cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), 2795f3186dd8SLinus Walleij GFP_KERNEL); 2796f3186dd8SLinus Walleij if (!cs) 2797f3186dd8SLinus Walleij return -ENOMEM; 2798f3186dd8SLinus Walleij ctlr->cs_gpiods = cs; 2799f3186dd8SLinus Walleij 2800f3186dd8SLinus Walleij for (i = 0; i < nb; i++) { 2801f3186dd8SLinus Walleij /* 2802f3186dd8SLinus Walleij * Most chipselects are active low, the inverted 2803f3186dd8SLinus Walleij * semantics are handled by special quirks in gpiolib, 2804f3186dd8SLinus Walleij * so initializing them GPIOD_OUT_LOW here means 2805f3186dd8SLinus Walleij * "unasserted", in most cases this will drive the physical 2806f3186dd8SLinus Walleij * line high. 2807f3186dd8SLinus Walleij */ 2808f3186dd8SLinus Walleij cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, 2809f3186dd8SLinus Walleij GPIOD_OUT_LOW); 28101723fdecSGeert Uytterhoeven if (IS_ERR(cs[i])) 28111723fdecSGeert Uytterhoeven return PTR_ERR(cs[i]); 2812f3186dd8SLinus Walleij 2813f3186dd8SLinus Walleij if (cs[i]) { 2814f3186dd8SLinus Walleij /* 2815f3186dd8SLinus Walleij * If we find a CS GPIO, name it after the device and 2816f3186dd8SLinus Walleij * chip select line. 2817f3186dd8SLinus Walleij */ 2818f3186dd8SLinus Walleij char *gpioname; 2819f3186dd8SLinus Walleij 2820f3186dd8SLinus Walleij gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", 2821f3186dd8SLinus Walleij dev_name(dev), i); 2822f3186dd8SLinus Walleij if (!gpioname) 2823f3186dd8SLinus Walleij return -ENOMEM; 2824f3186dd8SLinus Walleij gpiod_set_consumer_name(cs[i], gpioname); 28257d93aecdSGeert Uytterhoeven num_cs_gpios++; 28267d93aecdSGeert Uytterhoeven continue; 2827f3186dd8SLinus Walleij } 28287d93aecdSGeert Uytterhoeven 28297d93aecdSGeert Uytterhoeven if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { 28307d93aecdSGeert Uytterhoeven dev_err(dev, "Invalid native chip select %d\n", i); 28317d93aecdSGeert Uytterhoeven return -EINVAL; 28327d93aecdSGeert Uytterhoeven } 28337d93aecdSGeert Uytterhoeven native_cs_mask |= BIT(i); 28347d93aecdSGeert Uytterhoeven } 28357d93aecdSGeert Uytterhoeven 2836f60d7270SAndy Shevchenko ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; 2837dbaca8e5SAndy Shevchenko 2838dbaca8e5SAndy Shevchenko if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && 2839dbaca8e5SAndy Shevchenko ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { 28407d93aecdSGeert Uytterhoeven dev_err(dev, "No unused native chip select available\n"); 28417d93aecdSGeert Uytterhoeven return -EINVAL; 2842f3186dd8SLinus Walleij } 2843f3186dd8SLinus Walleij 2844f3186dd8SLinus Walleij return 0; 2845f3186dd8SLinus Walleij } 2846f3186dd8SLinus Walleij 2847bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr) 2848bdf3a3b5SBoris Brezillon { 2849bdf3a3b5SBoris Brezillon /* 2850b5932f5cSBoris Brezillon * The controller may implement only the high-level SPI-memory like 2851b5932f5cSBoris Brezillon * operations if it does not support regular SPI transfers, and this is 2852b5932f5cSBoris Brezillon * valid use case. 2853b5932f5cSBoris Brezillon * If ->mem_ops is NULL, we request that at least one of the 2854b5932f5cSBoris Brezillon * ->transfer_xxx() method be implemented. 2855bdf3a3b5SBoris Brezillon */ 2856b5932f5cSBoris Brezillon if (ctlr->mem_ops) { 2857b5932f5cSBoris Brezillon if (!ctlr->mem_ops->exec_op) 2858bdf3a3b5SBoris Brezillon return -EINVAL; 2859b5932f5cSBoris Brezillon } else if (!ctlr->transfer && !ctlr->transfer_one && 2860b5932f5cSBoris Brezillon !ctlr->transfer_one_message) { 2861b5932f5cSBoris Brezillon return -EINVAL; 2862b5932f5cSBoris Brezillon } 2863bdf3a3b5SBoris Brezillon 2864bdf3a3b5SBoris Brezillon return 0; 2865bdf3a3b5SBoris Brezillon } 2866bdf3a3b5SBoris Brezillon 28678ae12a0dSDavid Brownell /** 28688caab75fSGeert Uytterhoeven * spi_register_controller - register SPI master or slave controller 28698caab75fSGeert Uytterhoeven * @ctlr: initialized master, originally from spi_alloc_master() or 28708caab75fSGeert Uytterhoeven * spi_alloc_slave() 287133e34dc6SDavid Brownell * Context: can sleep 28728ae12a0dSDavid Brownell * 28738caab75fSGeert Uytterhoeven * SPI controllers connect to their drivers using some non-SPI bus, 28748ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 28758caab75fSGeert Uytterhoeven * includes calling spi_register_controller() to hook up to this SPI bus glue. 28768ae12a0dSDavid Brownell * 28778ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 28788ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 28798ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 28808ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 28818ae12a0dSDavid Brownell * chip is at which address. 28828ae12a0dSDavid Brownell * 28838ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 28848caab75fSGeert Uytterhoeven * success, else a negative error code (dropping the controller's refcount). 28850c868461SDavid Brownell * After a successful return, the caller is responsible for calling 28868caab75fSGeert Uytterhoeven * spi_unregister_controller(). 288797d56dc6SJavier Martinez Canillas * 288897d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 28898ae12a0dSDavid Brownell */ 28908caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr) 28918ae12a0dSDavid Brownell { 28928caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 28932b9603a0SFeng Tang struct boardinfo *bi; 2894b93318a2SSergei Shtylyov int status; 289542bdd706SLucas Stach int id, first_dynamic; 28968ae12a0dSDavid Brownell 28970c868461SDavid Brownell if (!dev) 28980c868461SDavid Brownell return -ENODEV; 28990c868461SDavid Brownell 2900bdf3a3b5SBoris Brezillon /* 2901bdf3a3b5SBoris Brezillon * Make sure all necessary hooks are implemented before registering 2902bdf3a3b5SBoris Brezillon * the SPI controller. 2903bdf3a3b5SBoris Brezillon */ 2904bdf3a3b5SBoris Brezillon status = spi_controller_check_ops(ctlr); 2905bdf3a3b5SBoris Brezillon if (status) 2906bdf3a3b5SBoris Brezillon return status; 2907bdf3a3b5SBoris Brezillon 290804b2d03aSGeert Uytterhoeven if (ctlr->bus_num >= 0) { 290904b2d03aSGeert Uytterhoeven /* devices with a fixed bus num must check-in with the num */ 291004b2d03aSGeert Uytterhoeven mutex_lock(&board_lock); 291104b2d03aSGeert Uytterhoeven id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 291204b2d03aSGeert Uytterhoeven ctlr->bus_num + 1, GFP_KERNEL); 291304b2d03aSGeert Uytterhoeven mutex_unlock(&board_lock); 291404b2d03aSGeert Uytterhoeven if (WARN(id < 0, "couldn't get idr")) 291504b2d03aSGeert Uytterhoeven return id == -ENOSPC ? -EBUSY : id; 291604b2d03aSGeert Uytterhoeven ctlr->bus_num = id; 291704b2d03aSGeert Uytterhoeven } else if (ctlr->dev.of_node) { 29189b61e302SSuniel Mahesh /* allocate dynamic bus number using Linux idr */ 29199b61e302SSuniel Mahesh id = of_alias_get_id(ctlr->dev.of_node, "spi"); 29209b61e302SSuniel Mahesh if (id >= 0) { 29219b61e302SSuniel Mahesh ctlr->bus_num = id; 29229b61e302SSuniel Mahesh mutex_lock(&board_lock); 29239b61e302SSuniel Mahesh id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 29249b61e302SSuniel Mahesh ctlr->bus_num + 1, GFP_KERNEL); 29259b61e302SSuniel Mahesh mutex_unlock(&board_lock); 29269b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 29279b61e302SSuniel Mahesh return id == -ENOSPC ? -EBUSY : id; 29289b61e302SSuniel Mahesh } 29299b61e302SSuniel Mahesh } 29308caab75fSGeert Uytterhoeven if (ctlr->bus_num < 0) { 293142bdd706SLucas Stach first_dynamic = of_alias_get_highest_id("spi"); 293242bdd706SLucas Stach if (first_dynamic < 0) 293342bdd706SLucas Stach first_dynamic = 0; 293442bdd706SLucas Stach else 293542bdd706SLucas Stach first_dynamic++; 293642bdd706SLucas Stach 29379b61e302SSuniel Mahesh mutex_lock(&board_lock); 293842bdd706SLucas Stach id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, 293942bdd706SLucas Stach 0, GFP_KERNEL); 29409b61e302SSuniel Mahesh mutex_unlock(&board_lock); 29419b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 29429b61e302SSuniel Mahesh return id; 29439b61e302SSuniel Mahesh ctlr->bus_num = id; 29448ae12a0dSDavid Brownell } 29458caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 29468caab75fSGeert Uytterhoeven init_completion(&ctlr->xfer_completion); 29478caab75fSGeert Uytterhoeven if (!ctlr->max_dma_len) 29488caab75fSGeert Uytterhoeven ctlr->max_dma_len = INT_MAX; 2949cf32b71eSErnst Schwab 2950*350de7ceSAndy Shevchenko /* 2951*350de7ceSAndy Shevchenko * Register the device, then userspace will see it. 2952*350de7ceSAndy Shevchenko * Registration fails if the bus ID is in use. 29538ae12a0dSDavid Brownell */ 29548caab75fSGeert Uytterhoeven dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 29550a919ae4SAndrey Smirnov 29560a919ae4SAndrey Smirnov if (!spi_controller_is_slave(ctlr)) { 29570a919ae4SAndrey Smirnov if (ctlr->use_gpio_descriptors) { 29580a919ae4SAndrey Smirnov status = spi_get_gpio_descs(ctlr); 29590a919ae4SAndrey Smirnov if (status) 2960f9981d4fSAaro Koskinen goto free_bus_id; 29610a919ae4SAndrey Smirnov /* 29620a919ae4SAndrey Smirnov * A controller using GPIO descriptors always 29630a919ae4SAndrey Smirnov * supports SPI_CS_HIGH if need be. 29640a919ae4SAndrey Smirnov */ 29650a919ae4SAndrey Smirnov ctlr->mode_bits |= SPI_CS_HIGH; 29660a919ae4SAndrey Smirnov } else { 29670a919ae4SAndrey Smirnov /* Legacy code path for GPIOs from DT */ 296843004f31SLinus Walleij status = of_spi_get_gpio_numbers(ctlr); 29690a919ae4SAndrey Smirnov if (status) 2970f9981d4fSAaro Koskinen goto free_bus_id; 29710a919ae4SAndrey Smirnov } 29720a919ae4SAndrey Smirnov } 29730a919ae4SAndrey Smirnov 2974f9481b08STudor Ambarus /* 2975f9481b08STudor Ambarus * Even if it's just one always-selected device, there must 2976f9481b08STudor Ambarus * be at least one chipselect. 2977f9481b08STudor Ambarus */ 2978f9981d4fSAaro Koskinen if (!ctlr->num_chipselect) { 2979f9981d4fSAaro Koskinen status = -EINVAL; 2980f9981d4fSAaro Koskinen goto free_bus_id; 2981f9981d4fSAaro Koskinen } 2982f9481b08STudor Ambarus 29838caab75fSGeert Uytterhoeven status = device_add(&ctlr->dev); 2984f9981d4fSAaro Koskinen if (status < 0) 2985f9981d4fSAaro Koskinen goto free_bus_id; 29869b61e302SSuniel Mahesh dev_dbg(dev, "registered %s %s\n", 29878caab75fSGeert Uytterhoeven spi_controller_is_slave(ctlr) ? "slave" : "master", 29889b61e302SSuniel Mahesh dev_name(&ctlr->dev)); 29898ae12a0dSDavid Brownell 2990b5932f5cSBoris Brezillon /* 2991b5932f5cSBoris Brezillon * If we're using a queued driver, start the queue. Note that we don't 2992b5932f5cSBoris Brezillon * need the queueing logic if the driver is only supporting high-level 2993b5932f5cSBoris Brezillon * memory operations. 2994b5932f5cSBoris Brezillon */ 2995b5932f5cSBoris Brezillon if (ctlr->transfer) { 29968caab75fSGeert Uytterhoeven dev_info(dev, "controller is unqueued, this is deprecated\n"); 2997b5932f5cSBoris Brezillon } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 29988caab75fSGeert Uytterhoeven status = spi_controller_initialize_queue(ctlr); 2999ffbbdd21SLinus Walleij if (status) { 30008caab75fSGeert Uytterhoeven device_del(&ctlr->dev); 3001f9981d4fSAaro Koskinen goto free_bus_id; 3002ffbbdd21SLinus Walleij } 3003ffbbdd21SLinus Walleij } 3004eca2ebc7SMartin Sperl /* add statistics */ 30058caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->statistics.lock); 3006ffbbdd21SLinus Walleij 30072b9603a0SFeng Tang mutex_lock(&board_lock); 30088caab75fSGeert Uytterhoeven list_add_tail(&ctlr->list, &spi_controller_list); 30092b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 30108caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 30112b9603a0SFeng Tang mutex_unlock(&board_lock); 30122b9603a0SFeng Tang 301364bee4d2SMika Westerberg /* Register devices from the device tree and ACPI */ 30148caab75fSGeert Uytterhoeven of_register_spi_devices(ctlr); 30158caab75fSGeert Uytterhoeven acpi_register_spi_devices(ctlr); 3016f9981d4fSAaro Koskinen return status; 3017f9981d4fSAaro Koskinen 3018f9981d4fSAaro Koskinen free_bus_id: 3019f9981d4fSAaro Koskinen mutex_lock(&board_lock); 3020f9981d4fSAaro Koskinen idr_remove(&spi_master_idr, ctlr->bus_num); 3021f9981d4fSAaro Koskinen mutex_unlock(&board_lock); 30228ae12a0dSDavid Brownell return status; 30238ae12a0dSDavid Brownell } 30248caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller); 30258ae12a0dSDavid Brownell 302659ebbe40STian Tao static void devm_spi_unregister(void *ctlr) 3027666d5b4cSMark Brown { 302859ebbe40STian Tao spi_unregister_controller(ctlr); 3029666d5b4cSMark Brown } 3030666d5b4cSMark Brown 3031666d5b4cSMark Brown /** 30328caab75fSGeert Uytterhoeven * devm_spi_register_controller - register managed SPI master or slave 30338caab75fSGeert Uytterhoeven * controller 30348caab75fSGeert Uytterhoeven * @dev: device managing SPI controller 30358caab75fSGeert Uytterhoeven * @ctlr: initialized controller, originally from spi_alloc_master() or 30368caab75fSGeert Uytterhoeven * spi_alloc_slave() 3037666d5b4cSMark Brown * Context: can sleep 3038666d5b4cSMark Brown * 30398caab75fSGeert Uytterhoeven * Register a SPI device as with spi_register_controller() which will 304068b892f1SJohan Hovold * automatically be unregistered and freed. 304197d56dc6SJavier Martinez Canillas * 304297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3043666d5b4cSMark Brown */ 30448caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev, 30458caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 3046666d5b4cSMark Brown { 3047666d5b4cSMark Brown int ret; 3048666d5b4cSMark Brown 30498caab75fSGeert Uytterhoeven ret = spi_register_controller(ctlr); 305059ebbe40STian Tao if (ret) 3051666d5b4cSMark Brown return ret; 305259ebbe40STian Tao 305359ebbe40STian Tao return devm_add_action_or_reset(dev, devm_spi_unregister, ctlr); 3054666d5b4cSMark Brown } 30558caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller); 3056666d5b4cSMark Brown 305734860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 30588ae12a0dSDavid Brownell { 30590c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 30608ae12a0dSDavid Brownell return 0; 30618ae12a0dSDavid Brownell } 30628ae12a0dSDavid Brownell 30638ae12a0dSDavid Brownell /** 30648caab75fSGeert Uytterhoeven * spi_unregister_controller - unregister SPI master or slave controller 30658caab75fSGeert Uytterhoeven * @ctlr: the controller being unregistered 306633e34dc6SDavid Brownell * Context: can sleep 30678ae12a0dSDavid Brownell * 30688caab75fSGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 30698ae12a0dSDavid Brownell * only ones directly touching chip registers. 30708ae12a0dSDavid Brownell * 30718ae12a0dSDavid Brownell * This must be called from context that can sleep. 307268b892f1SJohan Hovold * 307368b892f1SJohan Hovold * Note that this function also drops a reference to the controller. 30748ae12a0dSDavid Brownell */ 30758caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr) 30768ae12a0dSDavid Brownell { 30779b61e302SSuniel Mahesh struct spi_controller *found; 307867f7b278SJohan Hovold int id = ctlr->bus_num; 307989fc9a1aSJeff Garzik 3080ddf75be4SLukas Wunner /* Prevent addition of new devices, unregister existing ones */ 3081ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 30826098475dSMark Brown mutex_lock(&ctlr->add_lock); 3083ddf75be4SLukas Wunner 308484855678SLukas Wunner device_for_each_child(&ctlr->dev, NULL, __unregister); 308584855678SLukas Wunner 30869b61e302SSuniel Mahesh /* First make sure that this controller was ever added */ 30879b61e302SSuniel Mahesh mutex_lock(&board_lock); 308867f7b278SJohan Hovold found = idr_find(&spi_master_idr, id); 30899b61e302SSuniel Mahesh mutex_unlock(&board_lock); 30908caab75fSGeert Uytterhoeven if (ctlr->queued) { 30918caab75fSGeert Uytterhoeven if (spi_destroy_queue(ctlr)) 30928caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue remove failed\n"); 3093ffbbdd21SLinus Walleij } 30942b9603a0SFeng Tang mutex_lock(&board_lock); 30958caab75fSGeert Uytterhoeven list_del(&ctlr->list); 30962b9603a0SFeng Tang mutex_unlock(&board_lock); 30972b9603a0SFeng Tang 30985e844cc3SLukas Wunner device_del(&ctlr->dev); 30995e844cc3SLukas Wunner 31009b61e302SSuniel Mahesh /* free bus id */ 31019b61e302SSuniel Mahesh mutex_lock(&board_lock); 3102613bd1eaSJarkko Nikula if (found == ctlr) 310367f7b278SJohan Hovold idr_remove(&spi_master_idr, id); 31049b61e302SSuniel Mahesh mutex_unlock(&board_lock); 3105ddf75be4SLukas Wunner 3106ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 31076098475dSMark Brown mutex_unlock(&ctlr->add_lock); 31086c53b45cSMichael Walle 31096c53b45cSMichael Walle /* Release the last reference on the controller if its driver 31106c53b45cSMichael Walle * has not yet been converted to devm_spi_alloc_master/slave(). 31116c53b45cSMichael Walle */ 31126c53b45cSMichael Walle if (!ctlr->devm_allocated) 31136c53b45cSMichael Walle put_device(&ctlr->dev); 31148ae12a0dSDavid Brownell } 31158caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller); 31168ae12a0dSDavid Brownell 31178caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr) 3118ffbbdd21SLinus Walleij { 3119ffbbdd21SLinus Walleij int ret; 3120ffbbdd21SLinus Walleij 31218caab75fSGeert Uytterhoeven /* Basically no-ops for non-queued controllers */ 31228caab75fSGeert Uytterhoeven if (!ctlr->queued) 3123ffbbdd21SLinus Walleij return 0; 3124ffbbdd21SLinus Walleij 31258caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 3126ffbbdd21SLinus Walleij if (ret) 31278caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue stop failed\n"); 3128ffbbdd21SLinus Walleij 3129ffbbdd21SLinus Walleij return ret; 3130ffbbdd21SLinus Walleij } 31318caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend); 3132ffbbdd21SLinus Walleij 31338caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr) 3134ffbbdd21SLinus Walleij { 3135ffbbdd21SLinus Walleij int ret; 3136ffbbdd21SLinus Walleij 31378caab75fSGeert Uytterhoeven if (!ctlr->queued) 3138ffbbdd21SLinus Walleij return 0; 3139ffbbdd21SLinus Walleij 31408caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 3141ffbbdd21SLinus Walleij if (ret) 31428caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue restart failed\n"); 3143ffbbdd21SLinus Walleij 3144ffbbdd21SLinus Walleij return ret; 3145ffbbdd21SLinus Walleij } 31468caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume); 3147ffbbdd21SLinus Walleij 31488ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 31498ae12a0dSDavid Brownell 3150523baf5aSMartin Sperl /* Core methods for spi_message alterations */ 3151523baf5aSMartin Sperl 31528caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr, 3153523baf5aSMartin Sperl struct spi_message *msg, 3154523baf5aSMartin Sperl void *res) 3155523baf5aSMartin Sperl { 3156523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer = res; 3157523baf5aSMartin Sperl size_t i; 3158523baf5aSMartin Sperl 3159523baf5aSMartin Sperl /* call extra callback if requested */ 3160523baf5aSMartin Sperl if (rxfer->release) 31618caab75fSGeert Uytterhoeven rxfer->release(ctlr, msg, res); 3162523baf5aSMartin Sperl 3163523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 3164523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 3165523baf5aSMartin Sperl 3166523baf5aSMartin Sperl /* remove the formerly inserted entries */ 3167523baf5aSMartin Sperl for (i = 0; i < rxfer->inserted; i++) 3168523baf5aSMartin Sperl list_del(&rxfer->inserted_transfers[i].transfer_list); 3169523baf5aSMartin Sperl } 3170523baf5aSMartin Sperl 3171523baf5aSMartin Sperl /** 3172523baf5aSMartin Sperl * spi_replace_transfers - replace transfers with several transfers 3173523baf5aSMartin Sperl * and register change with spi_message.resources 3174523baf5aSMartin Sperl * @msg: the spi_message we work upon 3175523baf5aSMartin Sperl * @xfer_first: the first spi_transfer we want to replace 3176523baf5aSMartin Sperl * @remove: number of transfers to remove 3177523baf5aSMartin Sperl * @insert: the number of transfers we want to insert instead 3178523baf5aSMartin Sperl * @release: extra release code necessary in some circumstances 3179523baf5aSMartin Sperl * @extradatasize: extra data to allocate (with alignment guarantees 3180523baf5aSMartin Sperl * of struct @spi_transfer) 318105885397SMartin Sperl * @gfp: gfp flags 3182523baf5aSMartin Sperl * 3183523baf5aSMartin Sperl * Returns: pointer to @spi_replaced_transfers, 3184523baf5aSMartin Sperl * PTR_ERR(...) in case of errors. 3185523baf5aSMartin Sperl */ 3186da21fde0SUwe Kleine-König static struct spi_replaced_transfers *spi_replace_transfers( 3187523baf5aSMartin Sperl struct spi_message *msg, 3188523baf5aSMartin Sperl struct spi_transfer *xfer_first, 3189523baf5aSMartin Sperl size_t remove, 3190523baf5aSMartin Sperl size_t insert, 3191523baf5aSMartin Sperl spi_replaced_release_t release, 3192523baf5aSMartin Sperl size_t extradatasize, 3193523baf5aSMartin Sperl gfp_t gfp) 3194523baf5aSMartin Sperl { 3195523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer; 3196523baf5aSMartin Sperl struct spi_transfer *xfer; 3197523baf5aSMartin Sperl size_t i; 3198523baf5aSMartin Sperl 3199523baf5aSMartin Sperl /* allocate the structure using spi_res */ 3200523baf5aSMartin Sperl rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 3201aef97522SGustavo A. R. Silva struct_size(rxfer, inserted_transfers, insert) 3202523baf5aSMartin Sperl + extradatasize, 3203523baf5aSMartin Sperl gfp); 3204523baf5aSMartin Sperl if (!rxfer) 3205523baf5aSMartin Sperl return ERR_PTR(-ENOMEM); 3206523baf5aSMartin Sperl 3207523baf5aSMartin Sperl /* the release code to invoke before running the generic release */ 3208523baf5aSMartin Sperl rxfer->release = release; 3209523baf5aSMartin Sperl 3210523baf5aSMartin Sperl /* assign extradata */ 3211523baf5aSMartin Sperl if (extradatasize) 3212523baf5aSMartin Sperl rxfer->extradata = 3213523baf5aSMartin Sperl &rxfer->inserted_transfers[insert]; 3214523baf5aSMartin Sperl 3215523baf5aSMartin Sperl /* init the replaced_transfers list */ 3216523baf5aSMartin Sperl INIT_LIST_HEAD(&rxfer->replaced_transfers); 3217523baf5aSMartin Sperl 3218*350de7ceSAndy Shevchenko /* 3219*350de7ceSAndy Shevchenko * Assign the list_entry after which we should reinsert 3220523baf5aSMartin Sperl * the @replaced_transfers - it may be spi_message.messages! 3221523baf5aSMartin Sperl */ 3222523baf5aSMartin Sperl rxfer->replaced_after = xfer_first->transfer_list.prev; 3223523baf5aSMartin Sperl 3224523baf5aSMartin Sperl /* remove the requested number of transfers */ 3225523baf5aSMartin Sperl for (i = 0; i < remove; i++) { 3226*350de7ceSAndy Shevchenko /* 3227*350de7ceSAndy Shevchenko * If the entry after replaced_after it is msg->transfers 3228523baf5aSMartin Sperl * then we have been requested to remove more transfers 3229*350de7ceSAndy Shevchenko * than are in the list. 3230523baf5aSMartin Sperl */ 3231523baf5aSMartin Sperl if (rxfer->replaced_after->next == &msg->transfers) { 3232523baf5aSMartin Sperl dev_err(&msg->spi->dev, 3233523baf5aSMartin Sperl "requested to remove more spi_transfers than are available\n"); 3234523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 3235523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, 3236523baf5aSMartin Sperl rxfer->replaced_after); 3237523baf5aSMartin Sperl 3238523baf5aSMartin Sperl /* free the spi_replace_transfer structure */ 3239523baf5aSMartin Sperl spi_res_free(rxfer); 3240523baf5aSMartin Sperl 3241523baf5aSMartin Sperl /* and return with an error */ 3242523baf5aSMartin Sperl return ERR_PTR(-EINVAL); 3243523baf5aSMartin Sperl } 3244523baf5aSMartin Sperl 3245*350de7ceSAndy Shevchenko /* 3246*350de7ceSAndy Shevchenko * Remove the entry after replaced_after from list of 3247*350de7ceSAndy Shevchenko * transfers and add it to list of replaced_transfers. 3248523baf5aSMartin Sperl */ 3249523baf5aSMartin Sperl list_move_tail(rxfer->replaced_after->next, 3250523baf5aSMartin Sperl &rxfer->replaced_transfers); 3251523baf5aSMartin Sperl } 3252523baf5aSMartin Sperl 3253*350de7ceSAndy Shevchenko /* 3254*350de7ceSAndy Shevchenko * Create copy of the given xfer with identical settings 3255*350de7ceSAndy Shevchenko * based on the first transfer to get removed. 3256523baf5aSMartin Sperl */ 3257523baf5aSMartin Sperl for (i = 0; i < insert; i++) { 3258523baf5aSMartin Sperl /* we need to run in reverse order */ 3259523baf5aSMartin Sperl xfer = &rxfer->inserted_transfers[insert - 1 - i]; 3260523baf5aSMartin Sperl 3261523baf5aSMartin Sperl /* copy all spi_transfer data */ 3262523baf5aSMartin Sperl memcpy(xfer, xfer_first, sizeof(*xfer)); 3263523baf5aSMartin Sperl 3264523baf5aSMartin Sperl /* add to list */ 3265523baf5aSMartin Sperl list_add(&xfer->transfer_list, rxfer->replaced_after); 3266523baf5aSMartin Sperl 3267bebcfd27SAlexandru Ardelean /* clear cs_change and delay for all but the last */ 3268523baf5aSMartin Sperl if (i) { 3269523baf5aSMartin Sperl xfer->cs_change = false; 3270bebcfd27SAlexandru Ardelean xfer->delay.value = 0; 3271523baf5aSMartin Sperl } 3272523baf5aSMartin Sperl } 3273523baf5aSMartin Sperl 3274523baf5aSMartin Sperl /* set up inserted */ 3275523baf5aSMartin Sperl rxfer->inserted = insert; 3276523baf5aSMartin Sperl 3277523baf5aSMartin Sperl /* and register it with spi_res/spi_message */ 3278523baf5aSMartin Sperl spi_res_add(msg, rxfer); 3279523baf5aSMartin Sperl 3280523baf5aSMartin Sperl return rxfer; 3281523baf5aSMartin Sperl } 3282523baf5aSMartin Sperl 32838caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 3284d9f12122SMartin Sperl struct spi_message *msg, 3285d9f12122SMartin Sperl struct spi_transfer **xferp, 3286d9f12122SMartin Sperl size_t maxsize, 3287d9f12122SMartin Sperl gfp_t gfp) 3288d9f12122SMartin Sperl { 3289d9f12122SMartin Sperl struct spi_transfer *xfer = *xferp, *xfers; 3290d9f12122SMartin Sperl struct spi_replaced_transfers *srt; 3291d9f12122SMartin Sperl size_t offset; 3292d9f12122SMartin Sperl size_t count, i; 3293d9f12122SMartin Sperl 3294d9f12122SMartin Sperl /* calculate how many we have to replace */ 3295d9f12122SMartin Sperl count = DIV_ROUND_UP(xfer->len, maxsize); 3296d9f12122SMartin Sperl 3297d9f12122SMartin Sperl /* create replacement */ 3298d9f12122SMartin Sperl srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 3299657d32efSDan Carpenter if (IS_ERR(srt)) 3300657d32efSDan Carpenter return PTR_ERR(srt); 3301d9f12122SMartin Sperl xfers = srt->inserted_transfers; 3302d9f12122SMartin Sperl 3303*350de7ceSAndy Shevchenko /* 3304*350de7ceSAndy Shevchenko * Now handle each of those newly inserted spi_transfers. 3305*350de7ceSAndy Shevchenko * Note that the replacements spi_transfers all are preset 3306d9f12122SMartin Sperl * to the same values as *xferp, so tx_buf, rx_buf and len 3307d9f12122SMartin Sperl * are all identical (as well as most others) 3308d9f12122SMartin Sperl * so we just have to fix up len and the pointers. 3309d9f12122SMartin Sperl * 3310*350de7ceSAndy Shevchenko * This also includes support for the depreciated 3311*350de7ceSAndy Shevchenko * spi_message.is_dma_mapped interface. 3312d9f12122SMartin Sperl */ 3313d9f12122SMartin Sperl 3314*350de7ceSAndy Shevchenko /* 3315*350de7ceSAndy Shevchenko * The first transfer just needs the length modified, so we 3316*350de7ceSAndy Shevchenko * run it outside the loop. 3317d9f12122SMartin Sperl */ 3318c8dab77aSFabio Estevam xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 3319d9f12122SMartin Sperl 3320d9f12122SMartin Sperl /* all the others need rx_buf/tx_buf also set */ 3321d9f12122SMartin Sperl for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 3322d9f12122SMartin Sperl /* update rx_buf, tx_buf and dma */ 3323d9f12122SMartin Sperl if (xfers[i].rx_buf) 3324d9f12122SMartin Sperl xfers[i].rx_buf += offset; 3325d9f12122SMartin Sperl if (xfers[i].rx_dma) 3326d9f12122SMartin Sperl xfers[i].rx_dma += offset; 3327d9f12122SMartin Sperl if (xfers[i].tx_buf) 3328d9f12122SMartin Sperl xfers[i].tx_buf += offset; 3329d9f12122SMartin Sperl if (xfers[i].tx_dma) 3330d9f12122SMartin Sperl xfers[i].tx_dma += offset; 3331d9f12122SMartin Sperl 3332d9f12122SMartin Sperl /* update length */ 3333d9f12122SMartin Sperl xfers[i].len = min(maxsize, xfers[i].len - offset); 3334d9f12122SMartin Sperl } 3335d9f12122SMartin Sperl 3336*350de7ceSAndy Shevchenko /* 3337*350de7ceSAndy Shevchenko * We set up xferp to the last entry we have inserted, 3338*350de7ceSAndy Shevchenko * so that we skip those already split transfers. 3339d9f12122SMartin Sperl */ 3340d9f12122SMartin Sperl *xferp = &xfers[count - 1]; 3341d9f12122SMartin Sperl 3342d9f12122SMartin Sperl /* increment statistics counters */ 33438caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3344d9f12122SMartin Sperl transfers_split_maxsize); 3345d9f12122SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, 3346d9f12122SMartin Sperl transfers_split_maxsize); 3347d9f12122SMartin Sperl 3348d9f12122SMartin Sperl return 0; 3349d9f12122SMartin Sperl } 3350d9f12122SMartin Sperl 3351d9f12122SMartin Sperl /** 3352ce2424d7SMauro Carvalho Chehab * spi_split_transfers_maxsize - split spi transfers into multiple transfers 3353d9f12122SMartin Sperl * when an individual transfer exceeds a 3354d9f12122SMartin Sperl * certain size 33558caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller for this transfer 33563700ce95SMasanari Iida * @msg: the @spi_message to transform 33573700ce95SMasanari Iida * @maxsize: the maximum when to apply this 335810f11a22SJavier Martinez Canillas * @gfp: GFP allocation flags 3359d9f12122SMartin Sperl * 3360d9f12122SMartin Sperl * Return: status of transformation 3361d9f12122SMartin Sperl */ 33628caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr, 3363d9f12122SMartin Sperl struct spi_message *msg, 3364d9f12122SMartin Sperl size_t maxsize, 3365d9f12122SMartin Sperl gfp_t gfp) 3366d9f12122SMartin Sperl { 3367d9f12122SMartin Sperl struct spi_transfer *xfer; 3368d9f12122SMartin Sperl int ret; 3369d9f12122SMartin Sperl 3370*350de7ceSAndy Shevchenko /* 3371*350de7ceSAndy Shevchenko * Iterate over the transfer_list, 3372d9f12122SMartin Sperl * but note that xfer is advanced to the last transfer inserted 3373d9f12122SMartin Sperl * to avoid checking sizes again unnecessarily (also xfer does 3374*350de7ceSAndy Shevchenko * potentially belong to a different list by the time the 3375*350de7ceSAndy Shevchenko * replacement has happened). 3376d9f12122SMartin Sperl */ 3377d9f12122SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 3378d9f12122SMartin Sperl if (xfer->len > maxsize) { 33798caab75fSGeert Uytterhoeven ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 33808caab75fSGeert Uytterhoeven maxsize, gfp); 3381d9f12122SMartin Sperl if (ret) 3382d9f12122SMartin Sperl return ret; 3383d9f12122SMartin Sperl } 3384d9f12122SMartin Sperl } 3385d9f12122SMartin Sperl 3386d9f12122SMartin Sperl return 0; 3387d9f12122SMartin Sperl } 3388d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 33898ae12a0dSDavid Brownell 33908ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 33918ae12a0dSDavid Brownell 33928caab75fSGeert Uytterhoeven /* Core methods for SPI controller protocol drivers. Some of the 33937d077197SDavid Brownell * other core methods are currently defined as inline functions. 33947d077197SDavid Brownell */ 33957d077197SDavid Brownell 33968caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 33978caab75fSGeert Uytterhoeven u8 bits_per_word) 339863ab645fSStefan Brüns { 33998caab75fSGeert Uytterhoeven if (ctlr->bits_per_word_mask) { 340063ab645fSStefan Brüns /* Only 32 bits fit in the mask */ 340163ab645fSStefan Brüns if (bits_per_word > 32) 340263ab645fSStefan Brüns return -EINVAL; 34038caab75fSGeert Uytterhoeven if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 340463ab645fSStefan Brüns return -EINVAL; 340563ab645fSStefan Brüns } 340663ab645fSStefan Brüns 340763ab645fSStefan Brüns return 0; 340863ab645fSStefan Brüns } 340963ab645fSStefan Brüns 34107d077197SDavid Brownell /** 34117d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 34127d077197SDavid Brownell * @spi: the device whose settings are being modified 34137d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 34147d077197SDavid Brownell * 34157d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 34167d077197SDavid Brownell * device doesn't work with its default. They may likewise need 34177d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 34187d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 34197d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 34207d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 34217d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 34227d077197SDavid Brownell * 34237d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 34247d077197SDavid Brownell * that the underlying controller or its driver does not support. For 34257d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 34267d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 342797d56dc6SJavier Martinez Canillas * 342897d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 34297d077197SDavid Brownell */ 34307d077197SDavid Brownell int spi_setup(struct spi_device *spi) 34317d077197SDavid Brownell { 343283596fbeSGeert Uytterhoeven unsigned bad_bits, ugly_bits; 34335ab8d262SAndy Shevchenko int status; 34347d077197SDavid Brownell 3435d962608cSDragos Bogdan /* 3436*350de7ceSAndy Shevchenko * Check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO 3437*350de7ceSAndy Shevchenko * are set at the same time. 3438f477b7fbSwangyuhang */ 3439d962608cSDragos Bogdan if ((hweight_long(spi->mode & 3440d962608cSDragos Bogdan (SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) || 3441d962608cSDragos Bogdan (hweight_long(spi->mode & 3442d962608cSDragos Bogdan (SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) { 3443f477b7fbSwangyuhang dev_err(&spi->dev, 3444d962608cSDragos Bogdan "setup: can not select any two of dual, quad and no-rx/tx at the same time\n"); 3445f477b7fbSwangyuhang return -EINVAL; 3446f477b7fbSwangyuhang } 3447*350de7ceSAndy Shevchenko /* If it is SPI_3WIRE mode, DUAL and QUAD should be forbidden */ 3448f477b7fbSwangyuhang if ((spi->mode & SPI_3WIRE) && (spi->mode & 34496b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 34506b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 3451f477b7fbSwangyuhang return -EINVAL; 3452*350de7ceSAndy Shevchenko /* 3453*350de7ceSAndy Shevchenko * Help drivers fail *cleanly* when they need options 3454*350de7ceSAndy Shevchenko * that aren't supported with their current controller. 3455cbaa62e0SDavid Lechner * SPI_CS_WORD has a fallback software implementation, 3456cbaa62e0SDavid Lechner * so it is ignored here. 3457e7db06b5SDavid Brownell */ 3458d962608cSDragos Bogdan bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | 3459d962608cSDragos Bogdan SPI_NO_TX | SPI_NO_RX); 3460*350de7ceSAndy Shevchenko /* 3461*350de7ceSAndy Shevchenko * Nothing prevents from working with active-high CS in case if it 3462d61ad23cSSerge Semin * is driven by GPIO. 3463d61ad23cSSerge Semin */ 3464d61ad23cSSerge Semin if (gpio_is_valid(spi->cs_gpio)) 3465d61ad23cSSerge Semin bad_bits &= ~SPI_CS_HIGH; 346683596fbeSGeert Uytterhoeven ugly_bits = bad_bits & 34676b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 34686b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); 346983596fbeSGeert Uytterhoeven if (ugly_bits) { 347083596fbeSGeert Uytterhoeven dev_warn(&spi->dev, 347183596fbeSGeert Uytterhoeven "setup: ignoring unsupported mode bits %x\n", 347283596fbeSGeert Uytterhoeven ugly_bits); 347383596fbeSGeert Uytterhoeven spi->mode &= ~ugly_bits; 347483596fbeSGeert Uytterhoeven bad_bits &= ~ugly_bits; 347583596fbeSGeert Uytterhoeven } 3476e7db06b5SDavid Brownell if (bad_bits) { 3477eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 3478e7db06b5SDavid Brownell bad_bits); 3479e7db06b5SDavid Brownell return -EINVAL; 3480e7db06b5SDavid Brownell } 3481e7db06b5SDavid Brownell 34827d077197SDavid Brownell if (!spi->bits_per_word) 34837d077197SDavid Brownell spi->bits_per_word = 8; 34847d077197SDavid Brownell 34858caab75fSGeert Uytterhoeven status = __spi_validate_bits_per_word(spi->controller, 34868caab75fSGeert Uytterhoeven spi->bits_per_word); 34875ab8d262SAndy Shevchenko if (status) 34885ab8d262SAndy Shevchenko return status; 348963ab645fSStefan Brüns 34906820e812STudor Ambarus if (spi->controller->max_speed_hz && 34916820e812STudor Ambarus (!spi->max_speed_hz || 34926820e812STudor Ambarus spi->max_speed_hz > spi->controller->max_speed_hz)) 34938caab75fSGeert Uytterhoeven spi->max_speed_hz = spi->controller->max_speed_hz; 3494052eb2d4SAxel Lin 34954fae3a58SSerge Semin mutex_lock(&spi->controller->io_mutex); 34964fae3a58SSerge Semin 3497c914dbf8SJoe Burmeister if (spi->controller->setup) { 34988caab75fSGeert Uytterhoeven status = spi->controller->setup(spi); 3499c914dbf8SJoe Burmeister if (status) { 3500c914dbf8SJoe Burmeister mutex_unlock(&spi->controller->io_mutex); 3501c914dbf8SJoe Burmeister dev_err(&spi->controller->dev, "Failed to setup device: %d\n", 3502c914dbf8SJoe Burmeister status); 3503c914dbf8SJoe Burmeister return status; 3504c914dbf8SJoe Burmeister } 3505c914dbf8SJoe Burmeister } 35067d077197SDavid Brownell 3507d948e6caSLuhua Xu if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { 3508d948e6caSLuhua Xu status = pm_runtime_get_sync(spi->controller->dev.parent); 3509d948e6caSLuhua Xu if (status < 0) { 35104fae3a58SSerge Semin mutex_unlock(&spi->controller->io_mutex); 3511d948e6caSLuhua Xu pm_runtime_put_noidle(spi->controller->dev.parent); 3512d948e6caSLuhua Xu dev_err(&spi->controller->dev, "Failed to power device: %d\n", 3513d948e6caSLuhua Xu status); 3514d948e6caSLuhua Xu return status; 3515d948e6caSLuhua Xu } 351657a94607STony Lindgren 351757a94607STony Lindgren /* 351857a94607STony Lindgren * We do not want to return positive value from pm_runtime_get, 351957a94607STony Lindgren * there are many instances of devices calling spi_setup() and 352057a94607STony Lindgren * checking for a non-zero return value instead of a negative 352157a94607STony Lindgren * return value. 352257a94607STony Lindgren */ 352357a94607STony Lindgren status = 0; 352457a94607STony Lindgren 3525d347b4aaSDavid Bauer spi_set_cs(spi, false, true); 3526d948e6caSLuhua Xu pm_runtime_mark_last_busy(spi->controller->dev.parent); 3527d948e6caSLuhua Xu pm_runtime_put_autosuspend(spi->controller->dev.parent); 3528d948e6caSLuhua Xu } else { 3529d347b4aaSDavid Bauer spi_set_cs(spi, false, true); 3530d948e6caSLuhua Xu } 3531abeedb01SFranklin S Cooper Jr 35324fae3a58SSerge Semin mutex_unlock(&spi->controller->io_mutex); 35334fae3a58SSerge Semin 3534924b5867SDouglas Anderson if (spi->rt && !spi->controller->rt) { 3535924b5867SDouglas Anderson spi->controller->rt = true; 3536924b5867SDouglas Anderson spi_set_thread_rt(spi->controller); 3537924b5867SDouglas Anderson } 3538924b5867SDouglas Anderson 35395cb4e1f3SAndy Shevchenko trace_spi_setup(spi, status); 35405cb4e1f3SAndy Shevchenko 354140b82c2dSAndy Shevchenko dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 354240b82c2dSAndy Shevchenko spi->mode & SPI_MODE_X_MASK, 35437d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 35447d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 35457d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 35467d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 35477d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 35487d077197SDavid Brownell status); 35497d077197SDavid Brownell 35507d077197SDavid Brownell return status; 35517d077197SDavid Brownell } 35527d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 35537d077197SDavid Brownell 35546c613f68SAlexandru Ardelean static int _spi_xfer_word_delay_update(struct spi_transfer *xfer, 35556c613f68SAlexandru Ardelean struct spi_device *spi) 35566c613f68SAlexandru Ardelean { 35576c613f68SAlexandru Ardelean int delay1, delay2; 35586c613f68SAlexandru Ardelean 35593984d39bSAlexandru Ardelean delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); 35606c613f68SAlexandru Ardelean if (delay1 < 0) 35616c613f68SAlexandru Ardelean return delay1; 35626c613f68SAlexandru Ardelean 35633984d39bSAlexandru Ardelean delay2 = spi_delay_to_ns(&spi->word_delay, xfer); 35646c613f68SAlexandru Ardelean if (delay2 < 0) 35656c613f68SAlexandru Ardelean return delay2; 35666c613f68SAlexandru Ardelean 35676c613f68SAlexandru Ardelean if (delay1 < delay2) 35686c613f68SAlexandru Ardelean memcpy(&xfer->word_delay, &spi->word_delay, 35696c613f68SAlexandru Ardelean sizeof(xfer->word_delay)); 35706c613f68SAlexandru Ardelean 35716c613f68SAlexandru Ardelean return 0; 35726c613f68SAlexandru Ardelean } 35736c613f68SAlexandru Ardelean 357490808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message) 3575cf32b71eSErnst Schwab { 35768caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3577e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 35786ea31293SAtsushi Nemoto int w_size; 3579cf32b71eSErnst Schwab 358024a0013aSMark Brown if (list_empty(&message->transfers)) 358124a0013aSMark Brown return -EINVAL; 358224a0013aSMark Brown 3583*350de7ceSAndy Shevchenko /* 3584*350de7ceSAndy Shevchenko * If an SPI controller does not support toggling the CS line on each 358571388b21SDavid Lechner * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO 358671388b21SDavid Lechner * for the CS line, we can emulate the CS-per-word hardware function by 3587cbaa62e0SDavid Lechner * splitting transfers into one-word transfers and ensuring that 3588cbaa62e0SDavid Lechner * cs_change is set for each transfer. 3589cbaa62e0SDavid Lechner */ 359071388b21SDavid Lechner if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || 3591f3186dd8SLinus Walleij spi->cs_gpiod || 359271388b21SDavid Lechner gpio_is_valid(spi->cs_gpio))) { 3593cbaa62e0SDavid Lechner size_t maxsize; 3594cbaa62e0SDavid Lechner int ret; 3595cbaa62e0SDavid Lechner 3596cbaa62e0SDavid Lechner maxsize = (spi->bits_per_word + 7) / 8; 3597cbaa62e0SDavid Lechner 3598cbaa62e0SDavid Lechner /* spi_split_transfers_maxsize() requires message->spi */ 3599cbaa62e0SDavid Lechner message->spi = spi; 3600cbaa62e0SDavid Lechner 3601cbaa62e0SDavid Lechner ret = spi_split_transfers_maxsize(ctlr, message, maxsize, 3602cbaa62e0SDavid Lechner GFP_KERNEL); 3603cbaa62e0SDavid Lechner if (ret) 3604cbaa62e0SDavid Lechner return ret; 3605cbaa62e0SDavid Lechner 3606cbaa62e0SDavid Lechner list_for_each_entry(xfer, &message->transfers, transfer_list) { 3607cbaa62e0SDavid Lechner /* don't change cs_change on the last entry in the list */ 3608cbaa62e0SDavid Lechner if (list_is_last(&xfer->transfer_list, &message->transfers)) 3609cbaa62e0SDavid Lechner break; 3610cbaa62e0SDavid Lechner xfer->cs_change = 1; 3611cbaa62e0SDavid Lechner } 3612cbaa62e0SDavid Lechner } 3613cbaa62e0SDavid Lechner 3614*350de7ceSAndy Shevchenko /* 3615*350de7ceSAndy Shevchenko * Half-duplex links include original MicroWire, and ones with 3616cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 3617cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 3618cf32b71eSErnst Schwab * software limitations. 3619cf32b71eSErnst Schwab */ 36208caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 36218caab75fSGeert Uytterhoeven (spi->mode & SPI_3WIRE)) { 36228caab75fSGeert Uytterhoeven unsigned flags = ctlr->flags; 3623cf32b71eSErnst Schwab 3624cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 3625cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 3626cf32b71eSErnst Schwab return -EINVAL; 36278caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 3628cf32b71eSErnst Schwab return -EINVAL; 36298caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 3630cf32b71eSErnst Schwab return -EINVAL; 3631cf32b71eSErnst Schwab } 3632cf32b71eSErnst Schwab } 3633cf32b71eSErnst Schwab 3634*350de7ceSAndy Shevchenko /* 3635059b8ffeSLaxman Dewangan * Set transfer bits_per_word and max speed as spi device default if 3636059b8ffeSLaxman Dewangan * it is not set for this transfer. 3637f477b7fbSwangyuhang * Set transfer tx_nbits and rx_nbits as single transfer default 3638f477b7fbSwangyuhang * (SPI_NBITS_SINGLE) if it is not set for this transfer. 3639b7bb367aSJonas Bonn * Ensure transfer word_delay is at least as long as that required by 3640b7bb367aSJonas Bonn * device itself. 3641e6811d1dSLaxman Dewangan */ 364277e80588SMartin Sperl message->frame_length = 0; 3643e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 36445d7e2b5eSMartin Sperl xfer->effective_speed_hz = 0; 3645078726ceSSourav Poddar message->frame_length += xfer->len; 3646e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 3647e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 3648a6f87fadSAxel Lin 3649a6f87fadSAxel Lin if (!xfer->speed_hz) 3650059b8ffeSLaxman Dewangan xfer->speed_hz = spi->max_speed_hz; 3651a6f87fadSAxel Lin 36528caab75fSGeert Uytterhoeven if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 36538caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 365456ede94aSGabor Juhos 36558caab75fSGeert Uytterhoeven if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 3656543bb255SStephen Warren return -EINVAL; 3657a2fd4f9fSMark Brown 36584d94bd21SIvan T. Ivanov /* 36594d94bd21SIvan T. Ivanov * SPI transfer length should be multiple of SPI word size 3660*350de7ceSAndy Shevchenko * where SPI word size should be power-of-two multiple. 36614d94bd21SIvan T. Ivanov */ 36624d94bd21SIvan T. Ivanov if (xfer->bits_per_word <= 8) 36634d94bd21SIvan T. Ivanov w_size = 1; 36644d94bd21SIvan T. Ivanov else if (xfer->bits_per_word <= 16) 36654d94bd21SIvan T. Ivanov w_size = 2; 36664d94bd21SIvan T. Ivanov else 36674d94bd21SIvan T. Ivanov w_size = 4; 36684d94bd21SIvan T. Ivanov 36694d94bd21SIvan T. Ivanov /* No partial transfers accepted */ 36706ea31293SAtsushi Nemoto if (xfer->len % w_size) 36714d94bd21SIvan T. Ivanov return -EINVAL; 36724d94bd21SIvan T. Ivanov 36738caab75fSGeert Uytterhoeven if (xfer->speed_hz && ctlr->min_speed_hz && 36748caab75fSGeert Uytterhoeven xfer->speed_hz < ctlr->min_speed_hz) 3675a2fd4f9fSMark Brown return -EINVAL; 3676f477b7fbSwangyuhang 3677f477b7fbSwangyuhang if (xfer->tx_buf && !xfer->tx_nbits) 3678f477b7fbSwangyuhang xfer->tx_nbits = SPI_NBITS_SINGLE; 3679f477b7fbSwangyuhang if (xfer->rx_buf && !xfer->rx_nbits) 3680f477b7fbSwangyuhang xfer->rx_nbits = SPI_NBITS_SINGLE; 3681*350de7ceSAndy Shevchenko /* 3682*350de7ceSAndy Shevchenko * Check transfer tx/rx_nbits: 36831afd9989SGeert Uytterhoeven * 1. check the value matches one of single, dual and quad 36841afd9989SGeert Uytterhoeven * 2. check tx/rx_nbits match the mode in spi_device 3685f477b7fbSwangyuhang */ 3686db90a441SSourav Poddar if (xfer->tx_buf) { 3687d962608cSDragos Bogdan if (spi->mode & SPI_NO_TX) 3688d962608cSDragos Bogdan return -EINVAL; 3689f477b7fbSwangyuhang if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3690f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_DUAL && 3691f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_QUAD) 3692a2fd4f9fSMark Brown return -EINVAL; 3693f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 3694f477b7fbSwangyuhang !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3695f477b7fbSwangyuhang return -EINVAL; 3696f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 3697f477b7fbSwangyuhang !(spi->mode & SPI_TX_QUAD)) 3698f477b7fbSwangyuhang return -EINVAL; 3699db90a441SSourav Poddar } 3700f477b7fbSwangyuhang /* check transfer rx_nbits */ 3701db90a441SSourav Poddar if (xfer->rx_buf) { 3702d962608cSDragos Bogdan if (spi->mode & SPI_NO_RX) 3703d962608cSDragos Bogdan return -EINVAL; 3704f477b7fbSwangyuhang if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3705f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_DUAL && 3706f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_QUAD) 3707f477b7fbSwangyuhang return -EINVAL; 3708f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 3709f477b7fbSwangyuhang !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 3710f477b7fbSwangyuhang return -EINVAL; 3711f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 3712f477b7fbSwangyuhang !(spi->mode & SPI_RX_QUAD)) 3713f477b7fbSwangyuhang return -EINVAL; 3714e6811d1dSLaxman Dewangan } 3715b7bb367aSJonas Bonn 37166c613f68SAlexandru Ardelean if (_spi_xfer_word_delay_update(xfer, spi)) 37176c613f68SAlexandru Ardelean return -EINVAL; 3718e6811d1dSLaxman Dewangan } 3719e6811d1dSLaxman Dewangan 3720cf32b71eSErnst Schwab message->status = -EINPROGRESS; 372190808738SMark Brown 372290808738SMark Brown return 0; 372390808738SMark Brown } 372490808738SMark Brown 372590808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message) 372690808738SMark Brown { 37278caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3728b42faeeeSVladimir Oltean struct spi_transfer *xfer; 372990808738SMark Brown 3730b5932f5cSBoris Brezillon /* 3731b5932f5cSBoris Brezillon * Some controllers do not support doing regular SPI transfers. Return 3732b5932f5cSBoris Brezillon * ENOTSUPP when this is the case. 3733b5932f5cSBoris Brezillon */ 3734b5932f5cSBoris Brezillon if (!ctlr->transfer) 3735b5932f5cSBoris Brezillon return -ENOTSUPP; 3736b5932f5cSBoris Brezillon 373790808738SMark Brown message->spi = spi; 373890808738SMark Brown 37398caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); 3740eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); 3741eca2ebc7SMartin Sperl 374290808738SMark Brown trace_spi_message_submit(message); 374390808738SMark Brown 3744b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 3745b42faeeeSVladimir Oltean list_for_each_entry(xfer, &message->transfers, transfer_list) { 3746b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 3747b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 3748b42faeeeSVladimir Oltean } 3749b42faeeeSVladimir Oltean } 3750b42faeeeSVladimir Oltean 37518caab75fSGeert Uytterhoeven return ctlr->transfer(spi, message); 3752cf32b71eSErnst Schwab } 3753cf32b71eSErnst Schwab 3754568d0697SDavid Brownell /** 3755568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 3756568d0697SDavid Brownell * @spi: device with which data will be exchanged 3757568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 3758568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 3759568d0697SDavid Brownell * 3760568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 3761568d0697SDavid Brownell * as well as from task contexts which can sleep. 3762568d0697SDavid Brownell * 3763568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 3764568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 3765568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 3766568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 3767568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 3768568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 3769568d0697SDavid Brownell * core or controller driver code. 3770568d0697SDavid Brownell * 3771568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 3772568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 3773568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 3774568d0697SDavid Brownell * time requirements, for example. 3775568d0697SDavid Brownell * 3776568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 3777568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 3778568d0697SDavid Brownell * Until returning from the associated message completion callback, 3779568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 3780568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 3781568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 378297d56dc6SJavier Martinez Canillas * 378397d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3784568d0697SDavid Brownell */ 3785568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 3786568d0697SDavid Brownell { 37878caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3788cf32b71eSErnst Schwab int ret; 3789cf32b71eSErnst Schwab unsigned long flags; 3790568d0697SDavid Brownell 379190808738SMark Brown ret = __spi_validate(spi, message); 379290808738SMark Brown if (ret != 0) 379390808738SMark Brown return ret; 379490808738SMark Brown 37958caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3796568d0697SDavid Brownell 37978caab75fSGeert Uytterhoeven if (ctlr->bus_lock_flag) 3798cf32b71eSErnst Schwab ret = -EBUSY; 3799cf32b71eSErnst Schwab else 3800cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3801568d0697SDavid Brownell 38028caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3803cf32b71eSErnst Schwab 3804cf32b71eSErnst Schwab return ret; 3805568d0697SDavid Brownell } 3806568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 3807568d0697SDavid Brownell 3808cf32b71eSErnst Schwab /** 3809cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 3810cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3811cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 3812cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 3813cf32b71eSErnst Schwab * 3814cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 3815cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 3816cf32b71eSErnst Schwab * 3817cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 3818cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 3819cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 3820cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 3821cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 3822cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 3823cf32b71eSErnst Schwab * core or controller driver code. 3824cf32b71eSErnst Schwab * 3825cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 3826cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 3827cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 3828cf32b71eSErnst Schwab * time requirements, for example. 3829cf32b71eSErnst Schwab * 3830cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 3831cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 3832cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 3833cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 3834cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 3835cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 383697d56dc6SJavier Martinez Canillas * 383797d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3838cf32b71eSErnst Schwab */ 3839da21fde0SUwe Kleine-König static int spi_async_locked(struct spi_device *spi, struct spi_message *message) 3840cf32b71eSErnst Schwab { 38418caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3842cf32b71eSErnst Schwab int ret; 3843cf32b71eSErnst Schwab unsigned long flags; 3844cf32b71eSErnst Schwab 384590808738SMark Brown ret = __spi_validate(spi, message); 384690808738SMark Brown if (ret != 0) 384790808738SMark Brown return ret; 384890808738SMark Brown 38498caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3850cf32b71eSErnst Schwab 3851cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3852cf32b71eSErnst Schwab 38538caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3854cf32b71eSErnst Schwab 3855cf32b71eSErnst Schwab return ret; 3856cf32b71eSErnst Schwab 3857cf32b71eSErnst Schwab } 3858cf32b71eSErnst Schwab 38597d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 38607d077197SDavid Brownell 3861*350de7ceSAndy Shevchenko /* 3862*350de7ceSAndy Shevchenko * Utility methods for SPI protocol drivers, layered on 38637d077197SDavid Brownell * top of the core. Some other utility methods are defined as 38647d077197SDavid Brownell * inline functions. 38657d077197SDavid Brownell */ 38667d077197SDavid Brownell 38675d870c8eSAndrew Morton static void spi_complete(void *arg) 38685d870c8eSAndrew Morton { 38695d870c8eSAndrew Morton complete(arg); 38705d870c8eSAndrew Morton } 38715d870c8eSAndrew Morton 3872ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message) 3873cf32b71eSErnst Schwab { 3874cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 3875cf32b71eSErnst Schwab int status; 38768caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 38770461a414SMark Brown unsigned long flags; 38780461a414SMark Brown 38790461a414SMark Brown status = __spi_validate(spi, message); 38800461a414SMark Brown if (status != 0) 38810461a414SMark Brown return status; 3882cf32b71eSErnst Schwab 3883cf32b71eSErnst Schwab message->complete = spi_complete; 3884cf32b71eSErnst Schwab message->context = &done; 38850461a414SMark Brown message->spi = spi; 3886cf32b71eSErnst Schwab 38878caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); 3888eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); 3889eca2ebc7SMartin Sperl 3890*350de7ceSAndy Shevchenko /* 3891*350de7ceSAndy Shevchenko * If we're not using the legacy transfer method then we will 38920461a414SMark Brown * try to transfer in the calling context so special case. 38930461a414SMark Brown * This code would be less tricky if we could remove the 38940461a414SMark Brown * support for driver implemented message queues. 38950461a414SMark Brown */ 38968caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 38978caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 38980461a414SMark Brown 38990461a414SMark Brown trace_spi_message_submit(message); 39000461a414SMark Brown 39010461a414SMark Brown status = __spi_queued_transfer(spi, message, false); 39020461a414SMark Brown 39038caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 39040461a414SMark Brown } else { 3905cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 39060461a414SMark Brown } 3907cf32b71eSErnst Schwab 3908cf32b71eSErnst Schwab if (status == 0) { 3909*350de7ceSAndy Shevchenko /* Push out the messages in the calling context if we can */ 39108caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 39118caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3912eca2ebc7SMartin Sperl spi_sync_immediate); 3913eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, 3914eca2ebc7SMartin Sperl spi_sync_immediate); 39158caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, false); 3916eca2ebc7SMartin Sperl } 39170461a414SMark Brown 3918cf32b71eSErnst Schwab wait_for_completion(&done); 3919cf32b71eSErnst Schwab status = message->status; 3920cf32b71eSErnst Schwab } 3921cf32b71eSErnst Schwab message->context = NULL; 3922cf32b71eSErnst Schwab return status; 3923cf32b71eSErnst Schwab } 3924cf32b71eSErnst Schwab 39258ae12a0dSDavid Brownell /** 39268ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 39278ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 39288ae12a0dSDavid Brownell * @message: describes the data transfers 392933e34dc6SDavid Brownell * Context: can sleep 39308ae12a0dSDavid Brownell * 39318ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 39328ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 39338ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 39348ae12a0dSDavid Brownell * 39358ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 39368ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 39378ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 39388ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 39398ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 39408ae12a0dSDavid Brownell * 39410c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 39420c868461SDavid Brownell * message will not be freed before this call returns. 39430c868461SDavid Brownell * 394497d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 39458ae12a0dSDavid Brownell */ 39468ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 39478ae12a0dSDavid Brownell { 3948ef4d96ecSMark Brown int ret; 3949ef4d96ecSMark Brown 39508caab75fSGeert Uytterhoeven mutex_lock(&spi->controller->bus_lock_mutex); 3951ef4d96ecSMark Brown ret = __spi_sync(spi, message); 39528caab75fSGeert Uytterhoeven mutex_unlock(&spi->controller->bus_lock_mutex); 3953ef4d96ecSMark Brown 3954ef4d96ecSMark Brown return ret; 39558ae12a0dSDavid Brownell } 39568ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 39578ae12a0dSDavid Brownell 3958cf32b71eSErnst Schwab /** 3959cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 3960cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3961cf32b71eSErnst Schwab * @message: describes the data transfers 3962cf32b71eSErnst Schwab * Context: can sleep 3963cf32b71eSErnst Schwab * 3964cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3965cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 3966cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 3967cf32b71eSErnst Schwab * 3968cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 396925985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 3970cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 3971cf32b71eSErnst Schwab * 397297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3973cf32b71eSErnst Schwab */ 3974cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 3975cf32b71eSErnst Schwab { 3976ef4d96ecSMark Brown return __spi_sync(spi, message); 3977cf32b71eSErnst Schwab } 3978cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 3979cf32b71eSErnst Schwab 3980cf32b71eSErnst Schwab /** 3981cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 39828caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that should be locked for exclusive bus access 3983cf32b71eSErnst Schwab * Context: can sleep 3984cf32b71eSErnst Schwab * 3985cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3986cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3987cf32b71eSErnst Schwab * 3988cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 3989cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 3990cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 3991cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 3992cf32b71eSErnst Schwab * 399397d56dc6SJavier Martinez Canillas * Return: always zero. 3994cf32b71eSErnst Schwab */ 39958caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr) 3996cf32b71eSErnst Schwab { 3997cf32b71eSErnst Schwab unsigned long flags; 3998cf32b71eSErnst Schwab 39998caab75fSGeert Uytterhoeven mutex_lock(&ctlr->bus_lock_mutex); 4000cf32b71eSErnst Schwab 40018caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 40028caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 1; 40038caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 4004cf32b71eSErnst Schwab 4005cf32b71eSErnst Schwab /* mutex remains locked until spi_bus_unlock is called */ 4006cf32b71eSErnst Schwab 4007cf32b71eSErnst Schwab return 0; 4008cf32b71eSErnst Schwab } 4009cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 4010cf32b71eSErnst Schwab 4011cf32b71eSErnst Schwab /** 4012cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 40138caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that was locked for exclusive bus access 4014cf32b71eSErnst Schwab * Context: can sleep 4015cf32b71eSErnst Schwab * 4016cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 4017cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 4018cf32b71eSErnst Schwab * 4019cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 4020cf32b71eSErnst Schwab * call. 4021cf32b71eSErnst Schwab * 402297d56dc6SJavier Martinez Canillas * Return: always zero. 4023cf32b71eSErnst Schwab */ 40248caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr) 4025cf32b71eSErnst Schwab { 40268caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 4027cf32b71eSErnst Schwab 40288caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->bus_lock_mutex); 4029cf32b71eSErnst Schwab 4030cf32b71eSErnst Schwab return 0; 4031cf32b71eSErnst Schwab } 4032cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 4033cf32b71eSErnst Schwab 4034a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 4035a9948b61SDavid Brownell #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 40368ae12a0dSDavid Brownell 40378ae12a0dSDavid Brownell static u8 *buf; 40388ae12a0dSDavid Brownell 40398ae12a0dSDavid Brownell /** 40408ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 40418ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 40428ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 40438ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 404427570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 404527570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 404633e34dc6SDavid Brownell * Context: can sleep 40478ae12a0dSDavid Brownell * 40488ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 40498ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 40508ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 4051b885244eSDavid Brownell * This call may only be used from a context that may sleep. 40528ae12a0dSDavid Brownell * 4053c373643bSMark Brown * Parameters to this routine are always copied using a small buffer. 405433e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 40550c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 405697d56dc6SJavier Martinez Canillas * 405797d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 40588ae12a0dSDavid Brownell */ 40598ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 40600c4a1590SMark Brown const void *txbuf, unsigned n_tx, 40610c4a1590SMark Brown void *rxbuf, unsigned n_rx) 40628ae12a0dSDavid Brownell { 4063068f4070SDavid Brownell static DEFINE_MUTEX(lock); 40648ae12a0dSDavid Brownell 40658ae12a0dSDavid Brownell int status; 40668ae12a0dSDavid Brownell struct spi_message message; 4067bdff549eSDavid Brownell struct spi_transfer x[2]; 40688ae12a0dSDavid Brownell u8 *local_buf; 40698ae12a0dSDavid Brownell 4070*350de7ceSAndy Shevchenko /* 4071*350de7ceSAndy Shevchenko * Use preallocated DMA-safe buffer if we can. We can't avoid 4072b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 4073b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 4074b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 40758ae12a0dSDavid Brownell */ 4076b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 40772cd94c8aSMark Brown local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 40782cd94c8aSMark Brown GFP_KERNEL | GFP_DMA); 4079b3a223eeSMark Brown if (!local_buf) 4080b3a223eeSMark Brown return -ENOMEM; 4081b3a223eeSMark Brown } else { 4082b3a223eeSMark Brown local_buf = buf; 4083b3a223eeSMark Brown } 40848ae12a0dSDavid Brownell 40858275c642SVitaly Wool spi_message_init(&message); 40865fe5f05eSJingoo Han memset(x, 0, sizeof(x)); 4087bdff549eSDavid Brownell if (n_tx) { 4088bdff549eSDavid Brownell x[0].len = n_tx; 4089bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 4090bdff549eSDavid Brownell } 4091bdff549eSDavid Brownell if (n_rx) { 4092bdff549eSDavid Brownell x[1].len = n_rx; 4093bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 4094bdff549eSDavid Brownell } 40958275c642SVitaly Wool 40968ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 4097bdff549eSDavid Brownell x[0].tx_buf = local_buf; 4098bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 40998ae12a0dSDavid Brownell 41008ae12a0dSDavid Brownell /* do the i/o */ 41018ae12a0dSDavid Brownell status = spi_sync(spi, &message); 41029b938b74SMarc Pignat if (status == 0) 4103bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 41048ae12a0dSDavid Brownell 4105bdff549eSDavid Brownell if (x[0].tx_buf == buf) 4106068f4070SDavid Brownell mutex_unlock(&lock); 41078ae12a0dSDavid Brownell else 41088ae12a0dSDavid Brownell kfree(local_buf); 41098ae12a0dSDavid Brownell 41108ae12a0dSDavid Brownell return status; 41118ae12a0dSDavid Brownell } 41128ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 41138ae12a0dSDavid Brownell 41148ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 41158ae12a0dSDavid Brownell 4116da21fde0SUwe Kleine-König #if IS_ENABLED(CONFIG_OF_DYNAMIC) 4117ce79d54aSPantelis Antoniou /* must call put_device() when done with returned spi_device device */ 4118da21fde0SUwe Kleine-König static struct spi_device *of_find_spi_device_by_node(struct device_node *node) 4119ce79d54aSPantelis Antoniou { 4120cfba5de9SSuzuki K Poulose struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); 4121cfba5de9SSuzuki K Poulose 4122ce79d54aSPantelis Antoniou return dev ? to_spi_device(dev) : NULL; 4123ce79d54aSPantelis Antoniou } 4124ce79d54aSPantelis Antoniou 41258caab75fSGeert Uytterhoeven /* the spi controllers are not using spi_bus, so we find it with another way */ 41268caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 4127ce79d54aSPantelis Antoniou { 4128ce79d54aSPantelis Antoniou struct device *dev; 4129ce79d54aSPantelis Antoniou 4130cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_master_class, node); 41316c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 4132cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_slave_class, node); 4133ce79d54aSPantelis Antoniou if (!dev) 4134ce79d54aSPantelis Antoniou return NULL; 4135ce79d54aSPantelis Antoniou 4136ce79d54aSPantelis Antoniou /* reference got in class_find_device */ 41378caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 4138ce79d54aSPantelis Antoniou } 4139ce79d54aSPantelis Antoniou 4140ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action, 4141ce79d54aSPantelis Antoniou void *arg) 4142ce79d54aSPantelis Antoniou { 4143ce79d54aSPantelis Antoniou struct of_reconfig_data *rd = arg; 41448caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 4145ce79d54aSPantelis Antoniou struct spi_device *spi; 4146ce79d54aSPantelis Antoniou 4147ce79d54aSPantelis Antoniou switch (of_reconfig_get_state_change(action, arg)) { 4148ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 41498caab75fSGeert Uytterhoeven ctlr = of_find_spi_controller_by_node(rd->dn->parent); 41508caab75fSGeert Uytterhoeven if (ctlr == NULL) 4151ce79d54aSPantelis Antoniou return NOTIFY_OK; /* not for us */ 4152ce79d54aSPantelis Antoniou 4153bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 41548caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4155bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4156bd6c1644SGeert Uytterhoeven } 4157bd6c1644SGeert Uytterhoeven 41588caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, rd->dn); 41598caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4160ce79d54aSPantelis Antoniou 4161ce79d54aSPantelis Antoniou if (IS_ERR(spi)) { 416225c56c88SRob Herring pr_err("%s: failed to create for '%pOF'\n", 416325c56c88SRob Herring __func__, rd->dn); 4164e0af98a7SRalf Ramsauer of_node_clear_flag(rd->dn, OF_POPULATED); 4165ce79d54aSPantelis Antoniou return notifier_from_errno(PTR_ERR(spi)); 4166ce79d54aSPantelis Antoniou } 4167ce79d54aSPantelis Antoniou break; 4168ce79d54aSPantelis Antoniou 4169ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 4170bd6c1644SGeert Uytterhoeven /* already depopulated? */ 4171bd6c1644SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 4172bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4173bd6c1644SGeert Uytterhoeven 4174ce79d54aSPantelis Antoniou /* find our device by node */ 4175ce79d54aSPantelis Antoniou spi = of_find_spi_device_by_node(rd->dn); 4176ce79d54aSPantelis Antoniou if (spi == NULL) 4177ce79d54aSPantelis Antoniou return NOTIFY_OK; /* no? not meant for us */ 4178ce79d54aSPantelis Antoniou 4179ce79d54aSPantelis Antoniou /* unregister takes one ref away */ 4180ce79d54aSPantelis Antoniou spi_unregister_device(spi); 4181ce79d54aSPantelis Antoniou 4182ce79d54aSPantelis Antoniou /* and put the reference of the find */ 4183ce79d54aSPantelis Antoniou put_device(&spi->dev); 4184ce79d54aSPantelis Antoniou break; 4185ce79d54aSPantelis Antoniou } 4186ce79d54aSPantelis Antoniou 4187ce79d54aSPantelis Antoniou return NOTIFY_OK; 4188ce79d54aSPantelis Antoniou } 4189ce79d54aSPantelis Antoniou 4190ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = { 4191ce79d54aSPantelis Antoniou .notifier_call = of_spi_notify, 4192ce79d54aSPantelis Antoniou }; 4193ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4194ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier; 4195ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4196ce79d54aSPantelis Antoniou 41977f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI) 41988caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data) 41997f24467fSOctavian Purdila { 42007f24467fSOctavian Purdila return ACPI_COMPANION(dev->parent) == data; 42017f24467fSOctavian Purdila } 42027f24467fSOctavian Purdila 42038caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 42047f24467fSOctavian Purdila { 42057f24467fSOctavian Purdila struct device *dev; 42067f24467fSOctavian Purdila 42077f24467fSOctavian Purdila dev = class_find_device(&spi_master_class, NULL, adev, 42088caab75fSGeert Uytterhoeven spi_acpi_controller_match); 42096c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 42106c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, adev, 42118caab75fSGeert Uytterhoeven spi_acpi_controller_match); 42127f24467fSOctavian Purdila if (!dev) 42137f24467fSOctavian Purdila return NULL; 42147f24467fSOctavian Purdila 42158caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 42167f24467fSOctavian Purdila } 42177f24467fSOctavian Purdila 42187f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 42197f24467fSOctavian Purdila { 42207f24467fSOctavian Purdila struct device *dev; 42217f24467fSOctavian Purdila 422200500147SSuzuki K Poulose dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev); 42235b16668eSWolfram Sang return to_spi_device(dev); 42247f24467fSOctavian Purdila } 42257f24467fSOctavian Purdila 42267f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 42277f24467fSOctavian Purdila void *arg) 42287f24467fSOctavian Purdila { 42297f24467fSOctavian Purdila struct acpi_device *adev = arg; 42308caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 42317f24467fSOctavian Purdila struct spi_device *spi; 42327f24467fSOctavian Purdila 42337f24467fSOctavian Purdila switch (value) { 42347f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_ADD: 42358caab75fSGeert Uytterhoeven ctlr = acpi_spi_find_controller_by_adev(adev->parent); 42368caab75fSGeert Uytterhoeven if (!ctlr) 42377f24467fSOctavian Purdila break; 42387f24467fSOctavian Purdila 42398caab75fSGeert Uytterhoeven acpi_register_spi_device(ctlr, adev); 42408caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 42417f24467fSOctavian Purdila break; 42427f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_REMOVE: 42437f24467fSOctavian Purdila if (!acpi_device_enumerated(adev)) 42447f24467fSOctavian Purdila break; 42457f24467fSOctavian Purdila 42467f24467fSOctavian Purdila spi = acpi_spi_find_device_by_adev(adev); 42477f24467fSOctavian Purdila if (!spi) 42487f24467fSOctavian Purdila break; 42497f24467fSOctavian Purdila 42507f24467fSOctavian Purdila spi_unregister_device(spi); 42517f24467fSOctavian Purdila put_device(&spi->dev); 42527f24467fSOctavian Purdila break; 42537f24467fSOctavian Purdila } 42547f24467fSOctavian Purdila 42557f24467fSOctavian Purdila return NOTIFY_OK; 42567f24467fSOctavian Purdila } 42577f24467fSOctavian Purdila 42587f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = { 42597f24467fSOctavian Purdila .notifier_call = acpi_spi_notify, 42607f24467fSOctavian Purdila }; 42617f24467fSOctavian Purdila #else 42627f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier; 42637f24467fSOctavian Purdila #endif 42647f24467fSOctavian Purdila 42658ae12a0dSDavid Brownell static int __init spi_init(void) 42668ae12a0dSDavid Brownell { 4267b885244eSDavid Brownell int status; 42688ae12a0dSDavid Brownell 4269e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 4270b885244eSDavid Brownell if (!buf) { 4271b885244eSDavid Brownell status = -ENOMEM; 4272b885244eSDavid Brownell goto err0; 42738ae12a0dSDavid Brownell } 4274b885244eSDavid Brownell 4275b885244eSDavid Brownell status = bus_register(&spi_bus_type); 4276b885244eSDavid Brownell if (status < 0) 4277b885244eSDavid Brownell goto err1; 4278b885244eSDavid Brownell 4279b885244eSDavid Brownell status = class_register(&spi_master_class); 4280b885244eSDavid Brownell if (status < 0) 4281b885244eSDavid Brownell goto err2; 4282ce79d54aSPantelis Antoniou 42836c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 42846c364062SGeert Uytterhoeven status = class_register(&spi_slave_class); 42856c364062SGeert Uytterhoeven if (status < 0) 42866c364062SGeert Uytterhoeven goto err3; 42876c364062SGeert Uytterhoeven } 42886c364062SGeert Uytterhoeven 42895267720eSFabio Estevam if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 4290ce79d54aSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 42917f24467fSOctavian Purdila if (IS_ENABLED(CONFIG_ACPI)) 42927f24467fSOctavian Purdila WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 4293ce79d54aSPantelis Antoniou 4294b885244eSDavid Brownell return 0; 4295b885244eSDavid Brownell 42966c364062SGeert Uytterhoeven err3: 42976c364062SGeert Uytterhoeven class_unregister(&spi_master_class); 4298b885244eSDavid Brownell err2: 4299b885244eSDavid Brownell bus_unregister(&spi_bus_type); 4300b885244eSDavid Brownell err1: 4301b885244eSDavid Brownell kfree(buf); 4302b885244eSDavid Brownell buf = NULL; 4303b885244eSDavid Brownell err0: 4304b885244eSDavid Brownell return status; 4305b885244eSDavid Brownell } 4306b885244eSDavid Brownell 4307*350de7ceSAndy Shevchenko /* 4308*350de7ceSAndy Shevchenko * A board_info is normally registered in arch_initcall(), 4309*350de7ceSAndy Shevchenko * but even essential drivers wait till later. 4310b885244eSDavid Brownell * 4311*350de7ceSAndy Shevchenko * REVISIT only boardinfo really needs static linking. The rest (device and 4312*350de7ceSAndy Shevchenko * driver registration) _could_ be dynamically linked (modular) ... Costs 4313b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 43148ae12a0dSDavid Brownell */ 4315673c0c00SDavid Brownell postcore_initcall(spi_init); 4316