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 7edf6a864SAndy Shevchenko #include <linux/acpi.h> 88ae12a0dSDavid Brownell #include <linux/cache.h> 9edf6a864SAndy Shevchenko #include <linux/clk/clk-conf.h> 10edf6a864SAndy Shevchenko #include <linux/delay.h> 11edf6a864SAndy Shevchenko #include <linux/device.h> 1299adef31SMark Brown #include <linux/dmaengine.h> 13edf6a864SAndy Shevchenko #include <linux/dma-mapping.h> 14edf6a864SAndy Shevchenko #include <linux/export.h> 15edf6a864SAndy Shevchenko #include <linux/gpio/consumer.h> 16edf6a864SAndy Shevchenko #include <linux/highmem.h> 17edf6a864SAndy Shevchenko #include <linux/idr.h> 18edf6a864SAndy Shevchenko #include <linux/init.h> 19edf6a864SAndy Shevchenko #include <linux/ioport.h> 20edf6a864SAndy Shevchenko #include <linux/kernel.h> 21edf6a864SAndy Shevchenko #include <linux/kthread.h> 22edf6a864SAndy Shevchenko #include <linux/mod_devicetable.h> 2394040828SMatthias Kaehlcke #include <linux/mutex.h> 242b7a32f7SSinan Akman #include <linux/of_device.h> 25d57a4282SGrant Likely #include <linux/of_irq.h> 26edf6a864SAndy Shevchenko #include <linux/percpu.h> 27edf6a864SAndy Shevchenko #include <linux/platform_data/x86/apple.h> 28edf6a864SAndy Shevchenko #include <linux/pm_domain.h> 29edf6a864SAndy Shevchenko #include <linux/pm_runtime.h> 30edf6a864SAndy Shevchenko #include <linux/property.h> 31edf6a864SAndy Shevchenko #include <linux/ptp_clock_kernel.h> 32edf6a864SAndy Shevchenko #include <linux/sched/rt.h> 335a0e3ad6STejun Heo #include <linux/slab.h> 348ae12a0dSDavid Brownell #include <linux/spi/spi.h> 35b5932f5cSBoris Brezillon #include <linux/spi/spi-mem.h> 36ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h> 378ae12a0dSDavid Brownell 3856ec1978SMark Brown #define CREATE_TRACE_POINTS 3956ec1978SMark Brown #include <trace/events/spi.h> 40ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start); 41ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop); 429b61e302SSuniel Mahesh 4346336966SBoris Brezillon #include "internals.h" 4446336966SBoris Brezillon 459b61e302SSuniel Mahesh static DEFINE_IDR(spi_master_idr); 4656ec1978SMark Brown 478ae12a0dSDavid Brownell static void spidev_release(struct device *dev) 488ae12a0dSDavid Brownell { 490ffa0285SHans-Peter Nilsson struct spi_device *spi = to_spi_device(dev); 508ae12a0dSDavid Brownell 518caab75fSGeert Uytterhoeven spi_controller_put(spi->controller); 525039563eSTrent Piepho kfree(spi->driver_override); 536598b91bSDavid Jander free_percpu(spi->pcpu_statistics); 5407a389feSRoman Tereshonkov kfree(spi); 558ae12a0dSDavid Brownell } 568ae12a0dSDavid Brownell 578ae12a0dSDavid Brownell static ssize_t 588ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf) 598ae12a0dSDavid Brownell { 608ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 618c4ff6d0SZhang Rui int len; 628c4ff6d0SZhang Rui 638c4ff6d0SZhang Rui len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 648c4ff6d0SZhang Rui if (len != -ENODEV) 658c4ff6d0SZhang Rui return len; 668ae12a0dSDavid Brownell 67f2daa466SAndy Shevchenko return sysfs_emit(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 688ae12a0dSDavid Brownell } 69aa7da564SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias); 708ae12a0dSDavid Brownell 715039563eSTrent Piepho static ssize_t driver_override_store(struct device *dev, 725039563eSTrent Piepho struct device_attribute *a, 735039563eSTrent Piepho const char *buf, size_t count) 745039563eSTrent Piepho { 755039563eSTrent Piepho struct spi_device *spi = to_spi_device(dev); 7619368f0fSKrzysztof Kozlowski int ret; 775039563eSTrent Piepho 7819368f0fSKrzysztof Kozlowski ret = driver_set_override(dev, &spi->driver_override, buf, count); 7919368f0fSKrzysztof Kozlowski if (ret) 8019368f0fSKrzysztof Kozlowski return ret; 815039563eSTrent Piepho 825039563eSTrent Piepho return count; 835039563eSTrent Piepho } 845039563eSTrent Piepho 855039563eSTrent Piepho static ssize_t driver_override_show(struct device *dev, 865039563eSTrent Piepho struct device_attribute *a, char *buf) 875039563eSTrent Piepho { 885039563eSTrent Piepho const struct spi_device *spi = to_spi_device(dev); 895039563eSTrent Piepho ssize_t len; 905039563eSTrent Piepho 915039563eSTrent Piepho device_lock(dev); 92f2daa466SAndy Shevchenko len = sysfs_emit(buf, "%s\n", spi->driver_override ? : ""); 935039563eSTrent Piepho device_unlock(dev); 945039563eSTrent Piepho return len; 955039563eSTrent Piepho } 965039563eSTrent Piepho static DEVICE_ATTR_RW(driver_override); 975039563eSTrent Piepho 98d501cc4cSDavid Jander static struct spi_statistics __percpu *spi_alloc_pcpu_stats(struct device *dev) 996598b91bSDavid Jander { 1006598b91bSDavid Jander struct spi_statistics __percpu *pcpu_stats; 1016598b91bSDavid Jander 1026598b91bSDavid Jander if (dev) 1036598b91bSDavid Jander pcpu_stats = devm_alloc_percpu(dev, struct spi_statistics); 1046598b91bSDavid Jander else 1056598b91bSDavid Jander pcpu_stats = alloc_percpu_gfp(struct spi_statistics, GFP_KERNEL); 1066598b91bSDavid Jander 1076598b91bSDavid Jander if (pcpu_stats) { 1086598b91bSDavid Jander int cpu; 1096598b91bSDavid Jander 1106598b91bSDavid Jander for_each_possible_cpu(cpu) { 1116598b91bSDavid Jander struct spi_statistics *stat; 1126598b91bSDavid Jander 1136598b91bSDavid Jander stat = per_cpu_ptr(pcpu_stats, cpu); 1146598b91bSDavid Jander u64_stats_init(&stat->syncp); 1156598b91bSDavid Jander } 1166598b91bSDavid Jander } 1176598b91bSDavid Jander return pcpu_stats; 1186598b91bSDavid Jander } 1196598b91bSDavid Jander 120fc12d4bbSGeert Uytterhoeven static ssize_t spi_emit_pcpu_stats(struct spi_statistics __percpu *stat, 121fc12d4bbSGeert Uytterhoeven char *buf, size_t offset) 122fc12d4bbSGeert Uytterhoeven { 123fc12d4bbSGeert Uytterhoeven u64 val = 0; 124fc12d4bbSGeert Uytterhoeven int i; 125fc12d4bbSGeert Uytterhoeven 126fc12d4bbSGeert Uytterhoeven for_each_possible_cpu(i) { 127fc12d4bbSGeert Uytterhoeven const struct spi_statistics *pcpu_stats; 128fc12d4bbSGeert Uytterhoeven u64_stats_t *field; 129fc12d4bbSGeert Uytterhoeven unsigned int start; 130fc12d4bbSGeert Uytterhoeven u64 inc; 131fc12d4bbSGeert Uytterhoeven 132fc12d4bbSGeert Uytterhoeven pcpu_stats = per_cpu_ptr(stat, i); 133fc12d4bbSGeert Uytterhoeven field = (void *)pcpu_stats + offset; 134fc12d4bbSGeert Uytterhoeven do { 135fc12d4bbSGeert Uytterhoeven start = u64_stats_fetch_begin(&pcpu_stats->syncp); 136fc12d4bbSGeert Uytterhoeven inc = u64_stats_read(field); 137fc12d4bbSGeert Uytterhoeven } while (u64_stats_fetch_retry(&pcpu_stats->syncp, start)); 138fc12d4bbSGeert Uytterhoeven val += inc; 139fc12d4bbSGeert Uytterhoeven } 140fc12d4bbSGeert Uytterhoeven return sysfs_emit(buf, "%llu\n", val); 141fc12d4bbSGeert Uytterhoeven } 1426598b91bSDavid Jander 143eca2ebc7SMartin Sperl #define SPI_STATISTICS_ATTRS(field, file) \ 1448caab75fSGeert Uytterhoeven static ssize_t spi_controller_##field##_show(struct device *dev, \ 145eca2ebc7SMartin Sperl struct device_attribute *attr, \ 146eca2ebc7SMartin Sperl char *buf) \ 147eca2ebc7SMartin Sperl { \ 1488caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, \ 1498caab75fSGeert Uytterhoeven struct spi_controller, dev); \ 1506598b91bSDavid Jander return spi_statistics_##field##_show(ctlr->pcpu_statistics, buf); \ 151eca2ebc7SMartin Sperl } \ 1528caab75fSGeert Uytterhoeven static struct device_attribute dev_attr_spi_controller_##field = { \ 153ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 1548caab75fSGeert Uytterhoeven .show = spi_controller_##field##_show, \ 155eca2ebc7SMartin Sperl }; \ 156eca2ebc7SMartin Sperl static ssize_t spi_device_##field##_show(struct device *dev, \ 157eca2ebc7SMartin Sperl struct device_attribute *attr, \ 158eca2ebc7SMartin Sperl char *buf) \ 159eca2ebc7SMartin Sperl { \ 160d1eba93bSGeliang Tang struct spi_device *spi = to_spi_device(dev); \ 1616598b91bSDavid Jander return spi_statistics_##field##_show(spi->pcpu_statistics, buf); \ 162eca2ebc7SMartin Sperl } \ 163eca2ebc7SMartin Sperl static struct device_attribute dev_attr_spi_device_##field = { \ 164ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 165eca2ebc7SMartin Sperl .show = spi_device_##field##_show, \ 166eca2ebc7SMartin Sperl } 167eca2ebc7SMartin Sperl 1686598b91bSDavid Jander #define SPI_STATISTICS_SHOW_NAME(name, file, field) \ 169d501cc4cSDavid Jander static ssize_t spi_statistics_##name##_show(struct spi_statistics __percpu *stat, \ 170eca2ebc7SMartin Sperl char *buf) \ 171eca2ebc7SMartin Sperl { \ 172fc12d4bbSGeert Uytterhoeven return spi_emit_pcpu_stats(stat, buf, \ 173fc12d4bbSGeert Uytterhoeven offsetof(struct spi_statistics, field)); \ 174eca2ebc7SMartin Sperl } \ 175eca2ebc7SMartin Sperl SPI_STATISTICS_ATTRS(name, file) 176eca2ebc7SMartin Sperl 1776598b91bSDavid Jander #define SPI_STATISTICS_SHOW(field) \ 178eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ 1796598b91bSDavid Jander field) 180eca2ebc7SMartin Sperl 1816598b91bSDavid Jander SPI_STATISTICS_SHOW(messages); 1826598b91bSDavid Jander SPI_STATISTICS_SHOW(transfers); 1836598b91bSDavid Jander SPI_STATISTICS_SHOW(errors); 1846598b91bSDavid Jander SPI_STATISTICS_SHOW(timedout); 185eca2ebc7SMartin Sperl 1866598b91bSDavid Jander SPI_STATISTICS_SHOW(spi_sync); 1876598b91bSDavid Jander SPI_STATISTICS_SHOW(spi_sync_immediate); 1886598b91bSDavid Jander SPI_STATISTICS_SHOW(spi_async); 189eca2ebc7SMartin Sperl 1906598b91bSDavid Jander SPI_STATISTICS_SHOW(bytes); 1916598b91bSDavid Jander SPI_STATISTICS_SHOW(bytes_rx); 1926598b91bSDavid Jander SPI_STATISTICS_SHOW(bytes_tx); 193eca2ebc7SMartin Sperl 1946b7bc061SMartin Sperl #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ 1956b7bc061SMartin Sperl SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ 1966b7bc061SMartin Sperl "transfer_bytes_histo_" number, \ 1976598b91bSDavid Jander transfer_bytes_histo[index]) 1986b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); 1996b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); 2006b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); 2016b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); 2026b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); 2036b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); 2046b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); 2056b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); 2066b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); 2076b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); 2086b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); 2096b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); 2106b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); 2116b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); 2126b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); 2136b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); 2146b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); 2156b7bc061SMartin Sperl 2166598b91bSDavid Jander SPI_STATISTICS_SHOW(transfers_split_maxsize); 217d9f12122SMartin Sperl 218aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = { 219aa7da564SGreg Kroah-Hartman &dev_attr_modalias.attr, 2205039563eSTrent Piepho &dev_attr_driver_override.attr, 221aa7da564SGreg Kroah-Hartman NULL, 2228ae12a0dSDavid Brownell }; 223eca2ebc7SMartin Sperl 224eca2ebc7SMartin Sperl static const struct attribute_group spi_dev_group = { 225eca2ebc7SMartin Sperl .attrs = spi_dev_attrs, 226eca2ebc7SMartin Sperl }; 227eca2ebc7SMartin Sperl 228eca2ebc7SMartin Sperl static struct attribute *spi_device_statistics_attrs[] = { 229eca2ebc7SMartin Sperl &dev_attr_spi_device_messages.attr, 230eca2ebc7SMartin Sperl &dev_attr_spi_device_transfers.attr, 231eca2ebc7SMartin Sperl &dev_attr_spi_device_errors.attr, 232eca2ebc7SMartin Sperl &dev_attr_spi_device_timedout.attr, 233eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync.attr, 234eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync_immediate.attr, 235eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_async.attr, 236eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes.attr, 237eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_rx.attr, 238eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_tx.attr, 2396b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo0.attr, 2406b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo1.attr, 2416b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo2.attr, 2426b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo3.attr, 2436b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo4.attr, 2446b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo5.attr, 2456b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo6.attr, 2466b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo7.attr, 2476b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo8.attr, 2486b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo9.attr, 2496b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo10.attr, 2506b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo11.attr, 2516b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo12.attr, 2526b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo13.attr, 2536b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo14.attr, 2546b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo15.attr, 2556b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo16.attr, 256d9f12122SMartin Sperl &dev_attr_spi_device_transfers_split_maxsize.attr, 257eca2ebc7SMartin Sperl NULL, 258eca2ebc7SMartin Sperl }; 259eca2ebc7SMartin Sperl 260eca2ebc7SMartin Sperl static const struct attribute_group spi_device_statistics_group = { 261eca2ebc7SMartin Sperl .name = "statistics", 262eca2ebc7SMartin Sperl .attrs = spi_device_statistics_attrs, 263eca2ebc7SMartin Sperl }; 264eca2ebc7SMartin Sperl 265eca2ebc7SMartin Sperl static const struct attribute_group *spi_dev_groups[] = { 266eca2ebc7SMartin Sperl &spi_dev_group, 267eca2ebc7SMartin Sperl &spi_device_statistics_group, 268eca2ebc7SMartin Sperl NULL, 269eca2ebc7SMartin Sperl }; 270eca2ebc7SMartin Sperl 2718caab75fSGeert Uytterhoeven static struct attribute *spi_controller_statistics_attrs[] = { 2728caab75fSGeert Uytterhoeven &dev_attr_spi_controller_messages.attr, 2738caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers.attr, 2748caab75fSGeert Uytterhoeven &dev_attr_spi_controller_errors.attr, 2758caab75fSGeert Uytterhoeven &dev_attr_spi_controller_timedout.attr, 2768caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync.attr, 2778caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync_immediate.attr, 2788caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_async.attr, 2798caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes.attr, 2808caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_rx.attr, 2818caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_tx.attr, 2828caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo0.attr, 2838caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo1.attr, 2848caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo2.attr, 2858caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo3.attr, 2868caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo4.attr, 2878caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo5.attr, 2888caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo6.attr, 2898caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo7.attr, 2908caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo8.attr, 2918caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo9.attr, 2928caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo10.attr, 2938caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo11.attr, 2948caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo12.attr, 2958caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo13.attr, 2968caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo14.attr, 2978caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo15.attr, 2988caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo16.attr, 2998caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers_split_maxsize.attr, 300eca2ebc7SMartin Sperl NULL, 301eca2ebc7SMartin Sperl }; 302eca2ebc7SMartin Sperl 3038caab75fSGeert Uytterhoeven static const struct attribute_group spi_controller_statistics_group = { 304eca2ebc7SMartin Sperl .name = "statistics", 3058caab75fSGeert Uytterhoeven .attrs = spi_controller_statistics_attrs, 306eca2ebc7SMartin Sperl }; 307eca2ebc7SMartin Sperl 308eca2ebc7SMartin Sperl static const struct attribute_group *spi_master_groups[] = { 3098caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 310eca2ebc7SMartin Sperl NULL, 311eca2ebc7SMartin Sperl }; 312eca2ebc7SMartin Sperl 313d501cc4cSDavid Jander static void spi_statistics_add_transfer_stats(struct spi_statistics __percpu *pcpu_stats, 314eca2ebc7SMartin Sperl struct spi_transfer *xfer, 3158caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 316eca2ebc7SMartin Sperl { 3176b7bc061SMartin Sperl int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; 31867b9d641SDavid Jander struct spi_statistics *stats; 3196b7bc061SMartin Sperl 3206b7bc061SMartin Sperl if (l2len < 0) 3216b7bc061SMartin Sperl l2len = 0; 322eca2ebc7SMartin Sperl 32367b9d641SDavid Jander get_cpu(); 32467b9d641SDavid Jander stats = this_cpu_ptr(pcpu_stats); 3256598b91bSDavid Jander u64_stats_update_begin(&stats->syncp); 326eca2ebc7SMartin Sperl 3276598b91bSDavid Jander u64_stats_inc(&stats->transfers); 3286598b91bSDavid Jander u64_stats_inc(&stats->transfer_bytes_histo[l2len]); 329eca2ebc7SMartin Sperl 3306598b91bSDavid Jander u64_stats_add(&stats->bytes, xfer->len); 331eca2ebc7SMartin Sperl if ((xfer->tx_buf) && 3328caab75fSGeert Uytterhoeven (xfer->tx_buf != ctlr->dummy_tx)) 3336598b91bSDavid Jander u64_stats_add(&stats->bytes_tx, xfer->len); 334eca2ebc7SMartin Sperl if ((xfer->rx_buf) && 3358caab75fSGeert Uytterhoeven (xfer->rx_buf != ctlr->dummy_rx)) 3366598b91bSDavid Jander u64_stats_add(&stats->bytes_rx, xfer->len); 337eca2ebc7SMartin Sperl 3386598b91bSDavid Jander u64_stats_update_end(&stats->syncp); 33967b9d641SDavid Jander put_cpu(); 340eca2ebc7SMartin Sperl } 3418ae12a0dSDavid Brownell 342350de7ceSAndy Shevchenko /* 343350de7ceSAndy Shevchenko * modalias support makes "modprobe $MODALIAS" new-style hotplug work, 3448ae12a0dSDavid Brownell * and the sysfs version makes coldplug work too. 3458ae12a0dSDavid Brownell */ 3463f076575SAndy Shevchenko static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, const char *name) 34775368bf6SAnton Vorontsov { 34875368bf6SAnton Vorontsov while (id->name[0]) { 3493f076575SAndy Shevchenko if (!strcmp(name, id->name)) 35075368bf6SAnton Vorontsov return id; 35175368bf6SAnton Vorontsov id++; 35275368bf6SAnton Vorontsov } 35375368bf6SAnton Vorontsov return NULL; 35475368bf6SAnton Vorontsov } 35575368bf6SAnton Vorontsov 35675368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 35775368bf6SAnton Vorontsov { 35875368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 35975368bf6SAnton Vorontsov 3603f076575SAndy Shevchenko return spi_match_id(sdrv->id_table, sdev->modalias); 36175368bf6SAnton Vorontsov } 36275368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id); 36375368bf6SAnton Vorontsov 364aea672d0SAndy Shevchenko const void *spi_get_device_match_data(const struct spi_device *sdev) 365aea672d0SAndy Shevchenko { 366aea672d0SAndy Shevchenko const void *match; 367aea672d0SAndy Shevchenko 368aea672d0SAndy Shevchenko match = device_get_match_data(&sdev->dev); 369aea672d0SAndy Shevchenko if (match) 370aea672d0SAndy Shevchenko return match; 371aea672d0SAndy Shevchenko 372aea672d0SAndy Shevchenko return (const void *)spi_get_device_id(sdev)->driver_data; 373aea672d0SAndy Shevchenko } 374aea672d0SAndy Shevchenko EXPORT_SYMBOL_GPL(spi_get_device_match_data); 375aea672d0SAndy Shevchenko 3768ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 3778ae12a0dSDavid Brownell { 3788ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 37975368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(drv); 38075368bf6SAnton Vorontsov 3815039563eSTrent Piepho /* Check override first, and if set, only use the named driver */ 3825039563eSTrent Piepho if (spi->driver_override) 3835039563eSTrent Piepho return strcmp(spi->driver_override, drv->name) == 0; 3845039563eSTrent Piepho 3852b7a32f7SSinan Akman /* Attempt an OF style match */ 3862b7a32f7SSinan Akman if (of_driver_match_device(dev, drv)) 3872b7a32f7SSinan Akman return 1; 3882b7a32f7SSinan Akman 38964bee4d2SMika Westerberg /* Then try ACPI */ 39064bee4d2SMika Westerberg if (acpi_driver_match_device(dev, drv)) 39164bee4d2SMika Westerberg return 1; 39264bee4d2SMika Westerberg 39375368bf6SAnton Vorontsov if (sdrv->id_table) 3943f076575SAndy Shevchenko return !!spi_match_id(sdrv->id_table, spi->modalias); 3958ae12a0dSDavid Brownell 39635f74fcaSKay Sievers return strcmp(spi->modalias, drv->name) == 0; 3978ae12a0dSDavid Brownell } 3988ae12a0dSDavid Brownell 3992a81ada3SGreg Kroah-Hartman static int spi_uevent(const struct device *dev, struct kobj_uevent_env *env) 4008ae12a0dSDavid Brownell { 4018ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 4028c4ff6d0SZhang Rui int rc; 4038c4ff6d0SZhang Rui 4048c4ff6d0SZhang Rui rc = acpi_device_uevent_modalias(dev, env); 4058c4ff6d0SZhang Rui if (rc != -ENODEV) 4068c4ff6d0SZhang Rui return rc; 4078ae12a0dSDavid Brownell 4082856670fSAndy Shevchenko return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 4098ae12a0dSDavid Brownell } 4108ae12a0dSDavid Brownell 4119db34ee6SUwe Kleine-König static int spi_probe(struct device *dev) 412b885244eSDavid Brownell { 413b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 41444af7927SJon Hunter struct spi_device *spi = to_spi_device(dev); 41533cf00e5SMika Westerberg int ret; 416b885244eSDavid Brownell 41786be408bSSylwester Nawrocki ret = of_clk_set_defaults(dev->of_node, false); 41886be408bSSylwester Nawrocki if (ret) 41986be408bSSylwester Nawrocki return ret; 42086be408bSSylwester Nawrocki 42144af7927SJon Hunter if (dev->of_node) { 42244af7927SJon Hunter spi->irq = of_irq_get(dev->of_node, 0); 42344af7927SJon Hunter if (spi->irq == -EPROBE_DEFER) 42444af7927SJon Hunter return -EPROBE_DEFER; 42544af7927SJon Hunter if (spi->irq < 0) 42644af7927SJon Hunter spi->irq = 0; 42744af7927SJon Hunter } 42844af7927SJon Hunter 429676e7c25SUlf Hansson ret = dev_pm_domain_attach(dev, true); 43071f277a7SUlf Hansson if (ret) 43171f277a7SUlf Hansson return ret; 43271f277a7SUlf Hansson 433440408dbSUwe Kleine-König if (sdrv->probe) { 43444af7927SJon Hunter ret = sdrv->probe(spi); 43533cf00e5SMika Westerberg if (ret) 436676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 437440408dbSUwe Kleine-König } 43833cf00e5SMika Westerberg 43933cf00e5SMika Westerberg return ret; 440b885244eSDavid Brownell } 441b885244eSDavid Brownell 442fc7a6209SUwe Kleine-König static void spi_remove(struct device *dev) 443b885244eSDavid Brownell { 444b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 445b885244eSDavid Brownell 446a0386bbaSUwe Kleine-König if (sdrv->remove) 447a0386bbaSUwe Kleine-König sdrv->remove(to_spi_device(dev)); 4487795d475SUwe Kleine-König 449676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 450b885244eSDavid Brownell } 451b885244eSDavid Brownell 4529db34ee6SUwe Kleine-König static void spi_shutdown(struct device *dev) 453b885244eSDavid Brownell { 454a6f483b2SMarek Szyprowski if (dev->driver) { 455b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 456b885244eSDavid Brownell 4579db34ee6SUwe Kleine-König if (sdrv->shutdown) 458b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 459b885244eSDavid Brownell } 460a6f483b2SMarek Szyprowski } 461b885244eSDavid Brownell 4629db34ee6SUwe Kleine-König struct bus_type spi_bus_type = { 4639db34ee6SUwe Kleine-König .name = "spi", 4649db34ee6SUwe Kleine-König .dev_groups = spi_dev_groups, 4659db34ee6SUwe Kleine-König .match = spi_match_device, 4669db34ee6SUwe Kleine-König .uevent = spi_uevent, 4679db34ee6SUwe Kleine-König .probe = spi_probe, 4689db34ee6SUwe Kleine-König .remove = spi_remove, 4699db34ee6SUwe Kleine-König .shutdown = spi_shutdown, 4709db34ee6SUwe Kleine-König }; 4719db34ee6SUwe Kleine-König EXPORT_SYMBOL_GPL(spi_bus_type); 4729db34ee6SUwe Kleine-König 47333e34dc6SDavid Brownell /** 474ca5d2485SAndrew F. Davis * __spi_register_driver - register a SPI driver 47588c9321dSThierry Reding * @owner: owner module of the driver to register 47633e34dc6SDavid Brownell * @sdrv: the driver to register 47733e34dc6SDavid Brownell * Context: can sleep 47897d56dc6SJavier Martinez Canillas * 47997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 48033e34dc6SDavid Brownell */ 481ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) 482b885244eSDavid Brownell { 483ca5d2485SAndrew F. Davis sdrv->driver.owner = owner; 484b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 4855fa6863bSMark Brown 4865fa6863bSMark Brown /* 4875fa6863bSMark Brown * For Really Good Reasons we use spi: modaliases not of: 4885fa6863bSMark Brown * modaliases for DT so module autoloading won't work if we 4895fa6863bSMark Brown * don't have a spi_device_id as well as a compatible string. 4905fa6863bSMark Brown */ 4915fa6863bSMark Brown if (sdrv->driver.of_match_table) { 4925fa6863bSMark Brown const struct of_device_id *of_id; 4935fa6863bSMark Brown 4945fa6863bSMark Brown for (of_id = sdrv->driver.of_match_table; of_id->compatible[0]; 4955fa6863bSMark Brown of_id++) { 4965fa6863bSMark Brown const char *of_name; 4975fa6863bSMark Brown 4985fa6863bSMark Brown /* Strip off any vendor prefix */ 4995fa6863bSMark Brown of_name = strnchr(of_id->compatible, 5005fa6863bSMark Brown sizeof(of_id->compatible), ','); 5015fa6863bSMark Brown if (of_name) 5025fa6863bSMark Brown of_name++; 5035fa6863bSMark Brown else 5045fa6863bSMark Brown of_name = of_id->compatible; 5055fa6863bSMark Brown 5065fa6863bSMark Brown if (sdrv->id_table) { 5075fa6863bSMark Brown const struct spi_device_id *spi_id; 5085fa6863bSMark Brown 5093f076575SAndy Shevchenko spi_id = spi_match_id(sdrv->id_table, of_name); 510b79332efSAndy Shevchenko if (spi_id) 5115fa6863bSMark Brown continue; 5125fa6863bSMark Brown } else { 5135fa6863bSMark Brown if (strcmp(sdrv->driver.name, of_name) == 0) 5145fa6863bSMark Brown continue; 5155fa6863bSMark Brown } 5165fa6863bSMark Brown 5175fa6863bSMark Brown pr_warn("SPI driver %s has no spi_device_id for %s\n", 5185fa6863bSMark Brown sdrv->driver.name, of_id->compatible); 5195fa6863bSMark Brown } 5205fa6863bSMark Brown } 5215fa6863bSMark Brown 522b885244eSDavid Brownell return driver_register(&sdrv->driver); 523b885244eSDavid Brownell } 524ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver); 525b885244eSDavid Brownell 5268ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 5278ae12a0dSDavid Brownell 528350de7ceSAndy Shevchenko /* 529350de7ceSAndy Shevchenko * SPI devices should normally not be created by SPI device drivers; that 5308caab75fSGeert Uytterhoeven * would make them board-specific. Similarly with SPI controller drivers. 5318ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 5328ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 5338ae12a0dSDavid Brownell */ 5348ae12a0dSDavid Brownell 5358ae12a0dSDavid Brownell struct boardinfo { 5368ae12a0dSDavid Brownell struct list_head list; 5372b9603a0SFeng Tang struct spi_board_info board_info; 5388ae12a0dSDavid Brownell }; 5398ae12a0dSDavid Brownell 5408ae12a0dSDavid Brownell static LIST_HEAD(board_list); 5418caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list); 5422b9603a0SFeng Tang 5432b9603a0SFeng Tang /* 544be73e323SAndy Shevchenko * Used to protect add/del operation for board_info list and 545350de7ceSAndy Shevchenko * spi_controller list, and their matching process also used 546350de7ceSAndy Shevchenko * to protect object of type struct idr. 5472b9603a0SFeng Tang */ 54894040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 5498ae12a0dSDavid Brownell 550dc87c98eSGrant Likely /** 551dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 5528caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 553dc87c98eSGrant Likely * Context: can sleep 554dc87c98eSGrant Likely * 555dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 556dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 557dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 558dc87c98eSGrant Likely * spi_add_device() on it. 559dc87c98eSGrant Likely * 560dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 5618caab75fSGeert Uytterhoeven * spi_device structure to add it to the SPI controller. If the caller 562dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 563dc87c98eSGrant Likely * call spi_dev_put() on it. 564dc87c98eSGrant Likely * 56597d56dc6SJavier Martinez Canillas * Return: a pointer to the new device, or NULL. 566dc87c98eSGrant Likely */ 567e3dc1399SStefan Binding struct spi_device *spi_alloc_device(struct spi_controller *ctlr) 568dc87c98eSGrant Likely { 569dc87c98eSGrant Likely struct spi_device *spi; 570dc87c98eSGrant Likely 5718caab75fSGeert Uytterhoeven if (!spi_controller_get(ctlr)) 572dc87c98eSGrant Likely return NULL; 573dc87c98eSGrant Likely 5745fe5f05eSJingoo Han spi = kzalloc(sizeof(*spi), GFP_KERNEL); 575dc87c98eSGrant Likely if (!spi) { 5768caab75fSGeert Uytterhoeven spi_controller_put(ctlr); 577dc87c98eSGrant Likely return NULL; 578dc87c98eSGrant Likely } 579dc87c98eSGrant Likely 5806598b91bSDavid Jander spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL); 5816598b91bSDavid Jander if (!spi->pcpu_statistics) { 5826598b91bSDavid Jander kfree(spi); 5836598b91bSDavid Jander spi_controller_put(ctlr); 5846598b91bSDavid Jander return NULL; 5856598b91bSDavid Jander } 5866598b91bSDavid Jander 5878caab75fSGeert Uytterhoeven spi->master = spi->controller = ctlr; 5888caab75fSGeert Uytterhoeven spi->dev.parent = &ctlr->dev; 589dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 590dc87c98eSGrant Likely spi->dev.release = spidev_release; 591ea235786SJohn Garry spi->mode = ctlr->buswidth_override_bits; 592eca2ebc7SMartin Sperl 593dc87c98eSGrant Likely device_initialize(&spi->dev); 594dc87c98eSGrant Likely return spi; 595dc87c98eSGrant Likely } 596e3dc1399SStefan Binding EXPORT_SYMBOL_GPL(spi_alloc_device); 597dc87c98eSGrant Likely 598e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi) 599e13ac47bSJarkko Nikula { 600e13ac47bSJarkko Nikula struct acpi_device *adev = ACPI_COMPANION(&spi->dev); 601e13ac47bSJarkko Nikula 602e13ac47bSJarkko Nikula if (adev) { 603e13ac47bSJarkko Nikula dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); 604e13ac47bSJarkko Nikula return; 605e13ac47bSJarkko Nikula } 606e13ac47bSJarkko Nikula 6078caab75fSGeert Uytterhoeven dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), 608303feb3cSAmit Kumar Mahapatra spi_get_chipselect(spi, 0)); 609e13ac47bSJarkko Nikula } 610e13ac47bSJarkko Nikula 611b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data) 612b6fb8d3aSMika Westerberg { 613b6fb8d3aSMika Westerberg struct spi_device *spi = to_spi_device(dev); 614b6fb8d3aSMika Westerberg struct spi_device *new_spi = data; 615b6fb8d3aSMika Westerberg 6168caab75fSGeert Uytterhoeven if (spi->controller == new_spi->controller && 617303feb3cSAmit Kumar Mahapatra spi_get_chipselect(spi, 0) == spi_get_chipselect(new_spi, 0)) 618b6fb8d3aSMika Westerberg return -EBUSY; 619b6fb8d3aSMika Westerberg return 0; 620b6fb8d3aSMika Westerberg } 621b6fb8d3aSMika Westerberg 622c7299feaSSaravana Kannan static void spi_cleanup(struct spi_device *spi) 623c7299feaSSaravana Kannan { 624c7299feaSSaravana Kannan if (spi->controller->cleanup) 625c7299feaSSaravana Kannan spi->controller->cleanup(spi); 626c7299feaSSaravana Kannan } 627c7299feaSSaravana Kannan 6280c79378cSSebastian Reichel static int __spi_add_device(struct spi_device *spi) 6290c79378cSSebastian Reichel { 6300c79378cSSebastian Reichel struct spi_controller *ctlr = spi->controller; 6310c79378cSSebastian Reichel struct device *dev = ctlr->dev.parent; 6320c79378cSSebastian Reichel int status; 6330c79378cSSebastian Reichel 634*36124deaSAndy Shevchenko /* Chipselects are numbered 0..max; validate. */ 635*36124deaSAndy Shevchenko if (spi_get_chipselect(spi, 0) >= ctlr->num_chipselect) { 636*36124deaSAndy Shevchenko dev_err(dev, "cs%d >= max %d\n", spi_get_chipselect(spi, 0), 637*36124deaSAndy Shevchenko ctlr->num_chipselect); 638*36124deaSAndy Shevchenko return -EINVAL; 639*36124deaSAndy Shevchenko } 640*36124deaSAndy Shevchenko 641*36124deaSAndy Shevchenko /* Set the bus ID string */ 642*36124deaSAndy Shevchenko spi_dev_set_name(spi); 643*36124deaSAndy Shevchenko 6446bfb15f3SUwe Kleine-König /* 6456bfb15f3SUwe Kleine-König * We need to make sure there's no other device with this 6466bfb15f3SUwe Kleine-König * chipselect **BEFORE** we call setup(), else we'll trash 6476bfb15f3SUwe Kleine-König * its configuration. 6486bfb15f3SUwe Kleine-König */ 6490c79378cSSebastian Reichel status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); 6500c79378cSSebastian Reichel if (status) { 6510c79378cSSebastian Reichel dev_err(dev, "chipselect %d already in use\n", 652303feb3cSAmit Kumar Mahapatra spi_get_chipselect(spi, 0)); 6530c79378cSSebastian Reichel return status; 6540c79378cSSebastian Reichel } 6550c79378cSSebastian Reichel 6560c79378cSSebastian Reichel /* Controller may unregister concurrently */ 6570c79378cSSebastian Reichel if (IS_ENABLED(CONFIG_SPI_DYNAMIC) && 6580c79378cSSebastian Reichel !device_is_registered(&ctlr->dev)) { 6590c79378cSSebastian Reichel return -ENODEV; 6600c79378cSSebastian Reichel } 6610c79378cSSebastian Reichel 6620c79378cSSebastian Reichel if (ctlr->cs_gpiods) 663303feb3cSAmit Kumar Mahapatra spi_set_csgpiod(spi, 0, ctlr->cs_gpiods[spi_get_chipselect(spi, 0)]); 6640c79378cSSebastian Reichel 665350de7ceSAndy Shevchenko /* 666350de7ceSAndy Shevchenko * Drivers may modify this initial i/o setup, but will 6670c79378cSSebastian Reichel * normally rely on the device being setup. Devices 6680c79378cSSebastian Reichel * using SPI_CS_HIGH can't coexist well otherwise... 6690c79378cSSebastian Reichel */ 6700c79378cSSebastian Reichel status = spi_setup(spi); 6710c79378cSSebastian Reichel if (status < 0) { 6720c79378cSSebastian Reichel dev_err(dev, "can't setup %s, status %d\n", 6730c79378cSSebastian Reichel dev_name(&spi->dev), status); 6740c79378cSSebastian Reichel return status; 6750c79378cSSebastian Reichel } 6760c79378cSSebastian Reichel 6770c79378cSSebastian Reichel /* Device may be bound to an active driver when this returns */ 6780c79378cSSebastian Reichel status = device_add(&spi->dev); 6790c79378cSSebastian Reichel if (status < 0) { 6800c79378cSSebastian Reichel dev_err(dev, "can't add %s, status %d\n", 6810c79378cSSebastian Reichel dev_name(&spi->dev), status); 6820c79378cSSebastian Reichel spi_cleanup(spi); 6830c79378cSSebastian Reichel } else { 6840c79378cSSebastian Reichel dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 6850c79378cSSebastian Reichel } 6860c79378cSSebastian Reichel 6870c79378cSSebastian Reichel return status; 6880c79378cSSebastian Reichel } 6890c79378cSSebastian Reichel 690dc87c98eSGrant Likely /** 691dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 692dc87c98eSGrant Likely * @spi: spi_device to register 693dc87c98eSGrant Likely * 694dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 695702ca026SAndy Shevchenko * spi_alloc_device can be added onto the SPI bus with this function. 696dc87c98eSGrant Likely * 69797d56dc6SJavier Martinez Canillas * Return: 0 on success; negative errno on failure 698dc87c98eSGrant Likely */ 699e3dc1399SStefan Binding int spi_add_device(struct spi_device *spi) 700dc87c98eSGrant Likely { 7018caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 702dc87c98eSGrant Likely int status; 703dc87c98eSGrant Likely 7046098475dSMark Brown mutex_lock(&ctlr->add_lock); 7050c79378cSSebastian Reichel status = __spi_add_device(spi); 7066098475dSMark Brown mutex_unlock(&ctlr->add_lock); 707e48880e0SDavid Brownell return status; 708dc87c98eSGrant Likely } 709e3dc1399SStefan Binding EXPORT_SYMBOL_GPL(spi_add_device); 7108ae12a0dSDavid Brownell 7110c79378cSSebastian Reichel static int spi_add_device_locked(struct spi_device *spi) 7120c79378cSSebastian Reichel { 7130c79378cSSebastian Reichel struct spi_controller *ctlr = spi->controller; 7140c79378cSSebastian Reichel 7156098475dSMark Brown WARN_ON(!mutex_is_locked(&ctlr->add_lock)); 7160c79378cSSebastian Reichel return __spi_add_device(spi); 7170c79378cSSebastian Reichel } 7180c79378cSSebastian Reichel 71933e34dc6SDavid Brownell /** 72033e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 7218caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 72233e34dc6SDavid Brownell * @chip: Describes the SPI device 72333e34dc6SDavid Brownell * Context: can sleep 72433e34dc6SDavid Brownell * 72533e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 7268ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 7278ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 7288ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 7298ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 730082c8cb4SDavid Brownell * 73197d56dc6SJavier Martinez Canillas * Return: the new device, or NULL. 7328ae12a0dSDavid Brownell */ 7338caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr, 734e9d5a461SAdrian Bunk struct spi_board_info *chip) 7358ae12a0dSDavid Brownell { 7368ae12a0dSDavid Brownell struct spi_device *proxy; 7378ae12a0dSDavid Brownell int status; 7388ae12a0dSDavid Brownell 739350de7ceSAndy Shevchenko /* 740350de7ceSAndy Shevchenko * NOTE: caller did any chip->bus_num checks necessary. 741082c8cb4SDavid Brownell * 742082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 743082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 744082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 745082c8cb4SDavid Brownell */ 746082c8cb4SDavid Brownell 7478caab75fSGeert Uytterhoeven proxy = spi_alloc_device(ctlr); 748dc87c98eSGrant Likely if (!proxy) 7498ae12a0dSDavid Brownell return NULL; 7508ae12a0dSDavid Brownell 751102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 752102eb975SGrant Likely 753303feb3cSAmit Kumar Mahapatra spi_set_chipselect(proxy, 0, chip->chip_select); 7548ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 755980a01c9SDavid Brownell proxy->mode = chip->mode; 7568ae12a0dSDavid Brownell proxy->irq = chip->irq; 75751e99de5SWolfram Sang strscpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 7588ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 7598ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 7608ae12a0dSDavid Brownell proxy->controller_state = NULL; 7618ae12a0dSDavid Brownell 76247afc77bSHeikki Krogerus if (chip->swnode) { 76347afc77bSHeikki Krogerus status = device_add_software_node(&proxy->dev, chip->swnode); 764826cf175SDmitry Torokhov if (status) { 7659d902c2aSColin Ian King dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n", 766826cf175SDmitry Torokhov chip->modalias, status); 767826cf175SDmitry Torokhov goto err_dev_put; 768826cf175SDmitry Torokhov } 7698ae12a0dSDavid Brownell } 770dc87c98eSGrant Likely 771826cf175SDmitry Torokhov status = spi_add_device(proxy); 772826cf175SDmitry Torokhov if (status < 0) 773df41a5daSHeikki Krogerus goto err_dev_put; 774826cf175SDmitry Torokhov 775dc87c98eSGrant Likely return proxy; 776826cf175SDmitry Torokhov 777826cf175SDmitry Torokhov err_dev_put: 778df41a5daSHeikki Krogerus device_remove_software_node(&proxy->dev); 779826cf175SDmitry Torokhov spi_dev_put(proxy); 780826cf175SDmitry Torokhov return NULL; 781dc87c98eSGrant Likely } 7828ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 7838ae12a0dSDavid Brownell 7843b1884c2SGeert Uytterhoeven /** 7853b1884c2SGeert Uytterhoeven * spi_unregister_device - unregister a single SPI device 7863b1884c2SGeert Uytterhoeven * @spi: spi_device to unregister 7873b1884c2SGeert Uytterhoeven * 7883b1884c2SGeert Uytterhoeven * Start making the passed SPI device vanish. Normally this would be handled 7898caab75fSGeert Uytterhoeven * by spi_unregister_controller(). 7903b1884c2SGeert Uytterhoeven */ 7913b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi) 7923b1884c2SGeert Uytterhoeven { 793bd6c1644SGeert Uytterhoeven if (!spi) 794bd6c1644SGeert Uytterhoeven return; 795bd6c1644SGeert Uytterhoeven 7968324147fSJohan Hovold if (spi->dev.of_node) { 797bd6c1644SGeert Uytterhoeven of_node_clear_flag(spi->dev.of_node, OF_POPULATED); 7988324147fSJohan Hovold of_node_put(spi->dev.of_node); 7998324147fSJohan Hovold } 8007f24467fSOctavian Purdila if (ACPI_COMPANION(&spi->dev)) 8017f24467fSOctavian Purdila acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); 80247afc77bSHeikki Krogerus device_remove_software_node(&spi->dev); 80327e7db56SSaravana Kannan device_del(&spi->dev); 80427e7db56SSaravana Kannan spi_cleanup(spi); 80527e7db56SSaravana Kannan put_device(&spi->dev); 8063b1884c2SGeert Uytterhoeven } 8073b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device); 8083b1884c2SGeert Uytterhoeven 8098caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, 8102b9603a0SFeng Tang struct spi_board_info *bi) 8112b9603a0SFeng Tang { 8122b9603a0SFeng Tang struct spi_device *dev; 8132b9603a0SFeng Tang 8148caab75fSGeert Uytterhoeven if (ctlr->bus_num != bi->bus_num) 8152b9603a0SFeng Tang return; 8162b9603a0SFeng Tang 8178caab75fSGeert Uytterhoeven dev = spi_new_device(ctlr, bi); 8182b9603a0SFeng Tang if (!dev) 8198caab75fSGeert Uytterhoeven dev_err(ctlr->dev.parent, "can't create new device for %s\n", 8202b9603a0SFeng Tang bi->modalias); 8212b9603a0SFeng Tang } 8222b9603a0SFeng Tang 82333e34dc6SDavid Brownell /** 82433e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 82533e34dc6SDavid Brownell * @info: array of chip descriptors 82633e34dc6SDavid Brownell * @n: how many descriptors are provided 82733e34dc6SDavid Brownell * Context: can sleep 82833e34dc6SDavid Brownell * 8298ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 8308ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 8318ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 8328ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 8338ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 8348ae12a0dSDavid Brownell * 8358ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 8368ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 8378ae12a0dSDavid Brownell * would naturally declare its SPI devices. 8388ae12a0dSDavid Brownell * 8398ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 8408ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 84197d56dc6SJavier Martinez Canillas * 84297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 8438ae12a0dSDavid Brownell */ 844fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n) 8458ae12a0dSDavid Brownell { 8468ae12a0dSDavid Brownell struct boardinfo *bi; 8472b9603a0SFeng Tang int i; 8488ae12a0dSDavid Brownell 849c7908a37SXiubo Li if (!n) 850f974cf57SDmitry Torokhov return 0; 851c7908a37SXiubo Li 852f9bdb7fdSMarkus Elfring bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); 8538ae12a0dSDavid Brownell if (!bi) 8548ae12a0dSDavid Brownell return -ENOMEM; 8558ae12a0dSDavid Brownell 8562b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 8578caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 8582b9603a0SFeng Tang 8592b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 860826cf175SDmitry Torokhov 86194040828SMatthias Kaehlcke mutex_lock(&board_lock); 8628ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 8638caab75fSGeert Uytterhoeven list_for_each_entry(ctlr, &spi_controller_list, list) 8648caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, 8658caab75fSGeert Uytterhoeven &bi->board_info); 86694040828SMatthias Kaehlcke mutex_unlock(&board_lock); 8672b9603a0SFeng Tang } 8682b9603a0SFeng Tang 8698ae12a0dSDavid Brownell return 0; 8708ae12a0dSDavid Brownell } 8718ae12a0dSDavid Brownell 8728ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 8738ae12a0dSDavid Brownell 874fb51601bSUwe Kleine-König /* Core methods for SPI resource management */ 875fb51601bSUwe Kleine-König 876fb51601bSUwe Kleine-König /** 877fb51601bSUwe Kleine-König * spi_res_alloc - allocate a spi resource that is life-cycle managed 878fb51601bSUwe Kleine-König * during the processing of a spi_message while using 879fb51601bSUwe Kleine-König * spi_transfer_one 880702ca026SAndy Shevchenko * @spi: the SPI device for which we allocate memory 881fb51601bSUwe Kleine-König * @release: the release code to execute for this resource 882fb51601bSUwe Kleine-König * @size: size to alloc and return 883fb51601bSUwe Kleine-König * @gfp: GFP allocation flags 884fb51601bSUwe Kleine-König * 885fb51601bSUwe Kleine-König * Return: the pointer to the allocated data 886fb51601bSUwe Kleine-König * 887fb51601bSUwe Kleine-König * This may get enhanced in the future to allocate from a memory pool 888fb51601bSUwe Kleine-König * of the @spi_device or @spi_controller to avoid repeated allocations. 889fb51601bSUwe Kleine-König */ 890da21fde0SUwe Kleine-König static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release, 891fb51601bSUwe Kleine-König size_t size, gfp_t gfp) 892fb51601bSUwe Kleine-König { 893fb51601bSUwe Kleine-König struct spi_res *sres; 894fb51601bSUwe Kleine-König 895fb51601bSUwe Kleine-König sres = kzalloc(sizeof(*sres) + size, gfp); 896fb51601bSUwe Kleine-König if (!sres) 897fb51601bSUwe Kleine-König return NULL; 898fb51601bSUwe Kleine-König 899fb51601bSUwe Kleine-König INIT_LIST_HEAD(&sres->entry); 900fb51601bSUwe Kleine-König sres->release = release; 901fb51601bSUwe Kleine-König 902fb51601bSUwe Kleine-König return sres->data; 903fb51601bSUwe Kleine-König } 904fb51601bSUwe Kleine-König 905fb51601bSUwe Kleine-König /** 906702ca026SAndy Shevchenko * spi_res_free - free an SPI resource 907fb51601bSUwe Kleine-König * @res: pointer to the custom data of a resource 908fb51601bSUwe Kleine-König */ 909da21fde0SUwe Kleine-König static void spi_res_free(void *res) 910fb51601bSUwe Kleine-König { 911fb51601bSUwe Kleine-König struct spi_res *sres = container_of(res, struct spi_res, data); 912fb51601bSUwe Kleine-König 913fb51601bSUwe Kleine-König if (!res) 914fb51601bSUwe Kleine-König return; 915fb51601bSUwe Kleine-König 916fb51601bSUwe Kleine-König WARN_ON(!list_empty(&sres->entry)); 917fb51601bSUwe Kleine-König kfree(sres); 918fb51601bSUwe Kleine-König } 919fb51601bSUwe Kleine-König 920fb51601bSUwe Kleine-König /** 921fb51601bSUwe Kleine-König * spi_res_add - add a spi_res to the spi_message 922702ca026SAndy Shevchenko * @message: the SPI message 923fb51601bSUwe Kleine-König * @res: the spi_resource 924fb51601bSUwe Kleine-König */ 925da21fde0SUwe Kleine-König static void spi_res_add(struct spi_message *message, void *res) 926fb51601bSUwe Kleine-König { 927fb51601bSUwe Kleine-König struct spi_res *sres = container_of(res, struct spi_res, data); 928fb51601bSUwe Kleine-König 929fb51601bSUwe Kleine-König WARN_ON(!list_empty(&sres->entry)); 930fb51601bSUwe Kleine-König list_add_tail(&sres->entry, &message->resources); 931fb51601bSUwe Kleine-König } 932fb51601bSUwe Kleine-König 933fb51601bSUwe Kleine-König /** 934702ca026SAndy Shevchenko * spi_res_release - release all SPI resources for this message 935fb51601bSUwe Kleine-König * @ctlr: the @spi_controller 936fb51601bSUwe Kleine-König * @message: the @spi_message 937fb51601bSUwe Kleine-König */ 938da21fde0SUwe Kleine-König static void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 939fb51601bSUwe Kleine-König { 940fb51601bSUwe Kleine-König struct spi_res *res, *tmp; 941fb51601bSUwe Kleine-König 942fb51601bSUwe Kleine-König list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { 943fb51601bSUwe Kleine-König if (res->release) 944fb51601bSUwe Kleine-König res->release(ctlr, message, res->data); 945fb51601bSUwe Kleine-König 946fb51601bSUwe Kleine-König list_del(&res->entry); 947fb51601bSUwe Kleine-König 948fb51601bSUwe Kleine-König kfree(res); 949fb51601bSUwe Kleine-König } 950fb51601bSUwe Kleine-König } 951fb51601bSUwe Kleine-König 952fb51601bSUwe Kleine-König /*-------------------------------------------------------------------------*/ 953fb51601bSUwe Kleine-König 954d347b4aaSDavid Bauer static void spi_set_cs(struct spi_device *spi, bool enable, bool force) 955b158935fSMark Brown { 95686527bcbSAndy Shevchenko bool activate = enable; 95725093bdeSAlexandru Ardelean 958d40f0b6fSDouglas Anderson /* 959d40f0b6fSDouglas Anderson * Avoid calling into the driver (or doing delays) if the chip select 960d40f0b6fSDouglas Anderson * isn't actually changing from the last time this was called. 961d40f0b6fSDouglas Anderson */ 962303feb3cSAmit Kumar Mahapatra if (!force && ((enable && spi->controller->last_cs == spi_get_chipselect(spi, 0)) || 963303feb3cSAmit Kumar Mahapatra (!enable && spi->controller->last_cs != spi_get_chipselect(spi, 0))) && 964d40f0b6fSDouglas Anderson (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) 965d40f0b6fSDouglas Anderson return; 966d40f0b6fSDouglas Anderson 9675cb4e1f3SAndy Shevchenko trace_spi_set_cs(spi, activate); 9685cb4e1f3SAndy Shevchenko 969303feb3cSAmit Kumar Mahapatra spi->controller->last_cs = enable ? spi_get_chipselect(spi, 0) : -1; 970d40f0b6fSDouglas Anderson spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; 971d40f0b6fSDouglas Anderson 972303feb3cSAmit Kumar Mahapatra if ((spi_get_csgpiod(spi, 0) || !spi->controller->set_cs_timing) && !activate) 9738c33ebfeSMason Zhang spi_delay_exec(&spi->cs_hold, NULL); 97425093bdeSAlexandru Ardelean 975b158935fSMark Brown if (spi->mode & SPI_CS_HIGH) 976b158935fSMark Brown enable = !enable; 977b158935fSMark Brown 978303feb3cSAmit Kumar Mahapatra if (spi_get_csgpiod(spi, 0)) { 979f48dc6b9SLinus Walleij if (!(spi->mode & SPI_NO_CS)) { 9806b695469SAndy Shevchenko /* 9816b695469SAndy Shevchenko * Historically ACPI has no means of the GPIO polarity and 9826b695469SAndy Shevchenko * thus the SPISerialBus() resource defines it on the per-chip 9836b695469SAndy Shevchenko * basis. In order to avoid a chain of negations, the GPIO 9846b695469SAndy Shevchenko * polarity is considered being Active High. Even for the cases 9856b695469SAndy Shevchenko * when _DSD() is involved (in the updated versions of ACPI) 9866b695469SAndy Shevchenko * the GPIO CS polarity must be defined Active High to avoid 9876b695469SAndy Shevchenko * ambiguity. That's why we use enable, that takes SPI_CS_HIGH 9886b695469SAndy Shevchenko * into account. 9896b695469SAndy Shevchenko */ 9906b695469SAndy Shevchenko if (has_acpi_companion(&spi->dev)) 991303feb3cSAmit Kumar Mahapatra gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), !enable); 992f3186dd8SLinus Walleij else 9936b695469SAndy Shevchenko /* Polarity handled by GPIO library */ 994303feb3cSAmit Kumar Mahapatra gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), activate); 9956b695469SAndy Shevchenko } 9968eee6b9dSThor Thayer /* Some SPI masters need both GPIO CS & slave_select */ 99782238d2cSAndy Shevchenko if ((spi->controller->flags & SPI_CONTROLLER_GPIO_SS) && 9988caab75fSGeert Uytterhoeven spi->controller->set_cs) 9998caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 10008caab75fSGeert Uytterhoeven } else if (spi->controller->set_cs) { 10018caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 10028eee6b9dSThor Thayer } 100325093bdeSAlexandru Ardelean 1004303feb3cSAmit Kumar Mahapatra if (spi_get_csgpiod(spi, 0) || !spi->controller->set_cs_timing) { 100595c07247SHector Martin if (activate) 100695c07247SHector Martin spi_delay_exec(&spi->cs_setup, NULL); 100795c07247SHector Martin else 10088c33ebfeSMason Zhang spi_delay_exec(&spi->cs_inactive, NULL); 100925093bdeSAlexandru Ardelean } 1010b158935fSMark Brown } 1011b158935fSMark Brown 10122de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA 10130c17ba73SVincent Whitchurch static int spi_map_buf_attrs(struct spi_controller *ctlr, struct device *dev, 10146ad45a27SMark Brown struct sg_table *sgt, void *buf, size_t len, 10150c17ba73SVincent Whitchurch enum dma_data_direction dir, unsigned long attrs) 10166ad45a27SMark Brown { 10176ad45a27SMark Brown const bool vmalloced_buf = is_vmalloc_addr(buf); 1018df88e91bSAndy Shevchenko unsigned int max_seg_size = dma_get_max_seg_size(dev); 1019b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM 1020b1b8153cSVignesh R const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && 1021b1b8153cSVignesh R (unsigned long)buf < (PKMAP_BASE + 1022b1b8153cSVignesh R (LAST_PKMAP * PAGE_SIZE))); 1023b1b8153cSVignesh R #else 1024b1b8153cSVignesh R const bool kmap_buf = false; 1025b1b8153cSVignesh R #endif 102665598c13SAndrew Gabbasov int desc_len; 102765598c13SAndrew Gabbasov int sgs; 10286ad45a27SMark Brown struct page *vm_page; 10298dd4a016SJuan Gutierrez struct scatterlist *sg; 10306ad45a27SMark Brown void *sg_buf; 10316ad45a27SMark Brown size_t min; 10326ad45a27SMark Brown int i, ret; 10336ad45a27SMark Brown 1034b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 1035ebc4cb43SBiju Das desc_len = min_t(unsigned long, max_seg_size, PAGE_SIZE); 103665598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); 10370569a88fSVignesh R } else if (virt_addr_valid(buf)) { 1038ebc4cb43SBiju Das desc_len = min_t(size_t, max_seg_size, ctlr->max_dma_len); 103965598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len, desc_len); 10400569a88fSVignesh R } else { 10410569a88fSVignesh R return -EINVAL; 104265598c13SAndrew Gabbasov } 104365598c13SAndrew Gabbasov 10446ad45a27SMark Brown ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); 10456ad45a27SMark Brown if (ret != 0) 10466ad45a27SMark Brown return ret; 10476ad45a27SMark Brown 10488dd4a016SJuan Gutierrez sg = &sgt->sgl[0]; 10496ad45a27SMark Brown for (i = 0; i < sgs; i++) { 10506ad45a27SMark Brown 1051b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 1052ce99319aSMaxime Chevallier /* 1053ce99319aSMaxime Chevallier * Next scatterlist entry size is the minimum between 1054ce99319aSMaxime Chevallier * the desc_len and the remaining buffer length that 1055ce99319aSMaxime Chevallier * fits in a page. 1056ce99319aSMaxime Chevallier */ 1057ce99319aSMaxime Chevallier min = min_t(size_t, desc_len, 1058ce99319aSMaxime Chevallier min_t(size_t, len, 1059ce99319aSMaxime Chevallier PAGE_SIZE - offset_in_page(buf))); 1060b1b8153cSVignesh R if (vmalloced_buf) 10616ad45a27SMark Brown vm_page = vmalloc_to_page(buf); 1062b1b8153cSVignesh R else 1063b1b8153cSVignesh R vm_page = kmap_to_page(buf); 10646ad45a27SMark Brown if (!vm_page) { 10656ad45a27SMark Brown sg_free_table(sgt); 10666ad45a27SMark Brown return -ENOMEM; 10676ad45a27SMark Brown } 10688dd4a016SJuan Gutierrez sg_set_page(sg, vm_page, 1069c1aefbddSCharles Keepax min, offset_in_page(buf)); 10706ad45a27SMark Brown } else { 107165598c13SAndrew Gabbasov min = min_t(size_t, len, desc_len); 10726ad45a27SMark Brown sg_buf = buf; 10738dd4a016SJuan Gutierrez sg_set_buf(sg, sg_buf, min); 10746ad45a27SMark Brown } 10756ad45a27SMark Brown 10766ad45a27SMark Brown buf += min; 10776ad45a27SMark Brown len -= min; 10788dd4a016SJuan Gutierrez sg = sg_next(sg); 10796ad45a27SMark Brown } 10806ad45a27SMark Brown 10810c17ba73SVincent Whitchurch ret = dma_map_sgtable(dev, sgt, dir, attrs); 10826ad45a27SMark Brown if (ret < 0) { 10836ad45a27SMark Brown sg_free_table(sgt); 10846ad45a27SMark Brown return ret; 10856ad45a27SMark Brown } 10866ad45a27SMark Brown 10876ad45a27SMark Brown return 0; 10886ad45a27SMark Brown } 10896ad45a27SMark Brown 10900c17ba73SVincent Whitchurch int spi_map_buf(struct spi_controller *ctlr, struct device *dev, 10910c17ba73SVincent Whitchurch struct sg_table *sgt, void *buf, size_t len, 10920c17ba73SVincent Whitchurch enum dma_data_direction dir) 10930c17ba73SVincent Whitchurch { 10940c17ba73SVincent Whitchurch return spi_map_buf_attrs(ctlr, dev, sgt, buf, len, dir, 0); 10950c17ba73SVincent Whitchurch } 10960c17ba73SVincent Whitchurch 10970c17ba73SVincent Whitchurch static void spi_unmap_buf_attrs(struct spi_controller *ctlr, 10980c17ba73SVincent Whitchurch struct device *dev, struct sg_table *sgt, 10990c17ba73SVincent Whitchurch enum dma_data_direction dir, 11000c17ba73SVincent Whitchurch unsigned long attrs) 11010c17ba73SVincent Whitchurch { 11020c17ba73SVincent Whitchurch if (sgt->orig_nents) { 11030c17ba73SVincent Whitchurch dma_unmap_sgtable(dev, sgt, dir, attrs); 11040c17ba73SVincent Whitchurch sg_free_table(sgt); 11058e9204cdSMarek Szyprowski sgt->orig_nents = 0; 11068e9204cdSMarek Szyprowski sgt->nents = 0; 11070c17ba73SVincent Whitchurch } 11080c17ba73SVincent Whitchurch } 11090c17ba73SVincent Whitchurch 111046336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, 11116ad45a27SMark Brown struct sg_table *sgt, enum dma_data_direction dir) 11126ad45a27SMark Brown { 11130c17ba73SVincent Whitchurch spi_unmap_buf_attrs(ctlr, dev, sgt, dir, 0); 11146ad45a27SMark Brown } 11156ad45a27SMark Brown 11168caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 111799adef31SMark Brown { 111899adef31SMark Brown struct device *tx_dev, *rx_dev; 111999adef31SMark Brown struct spi_transfer *xfer; 11206ad45a27SMark Brown int ret; 11213a2eba9bSMark Brown 11228caab75fSGeert Uytterhoeven if (!ctlr->can_dma) 112399adef31SMark Brown return 0; 112499adef31SMark Brown 11258caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 11268caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 1127b470e10eSVinod Koul else if (ctlr->dma_map_dev) 1128b470e10eSVinod Koul tx_dev = ctlr->dma_map_dev; 1129c37f45b5SLeilk Liu else 11308caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 1131c37f45b5SLeilk Liu 11328caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 11338caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 1134b470e10eSVinod Koul else if (ctlr->dma_map_dev) 1135b470e10eSVinod Koul rx_dev = ctlr->dma_map_dev; 1136c37f45b5SLeilk Liu else 11378caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 113899adef31SMark Brown 113999adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 11400c17ba73SVincent Whitchurch /* The sync is done before each transfer. */ 11410c17ba73SVincent Whitchurch unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC; 11420c17ba73SVincent Whitchurch 11438caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 114499adef31SMark Brown continue; 114599adef31SMark Brown 114699adef31SMark Brown if (xfer->tx_buf != NULL) { 11470c17ba73SVincent Whitchurch ret = spi_map_buf_attrs(ctlr, tx_dev, &xfer->tx_sg, 11480c17ba73SVincent Whitchurch (void *)xfer->tx_buf, 11490c17ba73SVincent Whitchurch xfer->len, DMA_TO_DEVICE, 11500c17ba73SVincent Whitchurch attrs); 11516ad45a27SMark Brown if (ret != 0) 11526ad45a27SMark Brown return ret; 115399adef31SMark Brown } 115499adef31SMark Brown 115599adef31SMark Brown if (xfer->rx_buf != NULL) { 11560c17ba73SVincent Whitchurch ret = spi_map_buf_attrs(ctlr, rx_dev, &xfer->rx_sg, 115799adef31SMark Brown xfer->rx_buf, xfer->len, 11580c17ba73SVincent Whitchurch DMA_FROM_DEVICE, attrs); 11596ad45a27SMark Brown if (ret != 0) { 11600c17ba73SVincent Whitchurch spi_unmap_buf_attrs(ctlr, tx_dev, 11610c17ba73SVincent Whitchurch &xfer->tx_sg, DMA_TO_DEVICE, 11620c17ba73SVincent Whitchurch attrs); 11630c17ba73SVincent Whitchurch 11646ad45a27SMark Brown return ret; 116599adef31SMark Brown } 116699adef31SMark Brown } 116799adef31SMark Brown } 116899adef31SMark Brown 1169f25723dcSVincent Whitchurch ctlr->cur_rx_dma_dev = rx_dev; 1170f25723dcSVincent Whitchurch ctlr->cur_tx_dma_dev = tx_dev; 11718caab75fSGeert Uytterhoeven ctlr->cur_msg_mapped = true; 117299adef31SMark Brown 117399adef31SMark Brown return 0; 117499adef31SMark Brown } 117599adef31SMark Brown 11768caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg) 117799adef31SMark Brown { 1178f25723dcSVincent Whitchurch struct device *rx_dev = ctlr->cur_rx_dma_dev; 1179f25723dcSVincent Whitchurch struct device *tx_dev = ctlr->cur_tx_dma_dev; 118099adef31SMark Brown struct spi_transfer *xfer; 118199adef31SMark Brown 11828caab75fSGeert Uytterhoeven if (!ctlr->cur_msg_mapped || !ctlr->can_dma) 118399adef31SMark Brown return 0; 118499adef31SMark Brown 118599adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 11860c17ba73SVincent Whitchurch /* The sync has already been done after each transfer. */ 11870c17ba73SVincent Whitchurch unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC; 11880c17ba73SVincent Whitchurch 11898caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 119099adef31SMark Brown continue; 119199adef31SMark Brown 11920c17ba73SVincent Whitchurch spi_unmap_buf_attrs(ctlr, rx_dev, &xfer->rx_sg, 11930c17ba73SVincent Whitchurch DMA_FROM_DEVICE, attrs); 11940c17ba73SVincent Whitchurch spi_unmap_buf_attrs(ctlr, tx_dev, &xfer->tx_sg, 11950c17ba73SVincent Whitchurch DMA_TO_DEVICE, attrs); 119699adef31SMark Brown } 119799adef31SMark Brown 1198809b1b04SRobin Gong ctlr->cur_msg_mapped = false; 1199809b1b04SRobin Gong 120099adef31SMark Brown return 0; 120199adef31SMark Brown } 12020c17ba73SVincent Whitchurch 12030c17ba73SVincent Whitchurch static void spi_dma_sync_for_device(struct spi_controller *ctlr, 12040c17ba73SVincent Whitchurch struct spi_transfer *xfer) 12050c17ba73SVincent Whitchurch { 12060c17ba73SVincent Whitchurch struct device *rx_dev = ctlr->cur_rx_dma_dev; 12070c17ba73SVincent Whitchurch struct device *tx_dev = ctlr->cur_tx_dma_dev; 12080c17ba73SVincent Whitchurch 12090c17ba73SVincent Whitchurch if (!ctlr->cur_msg_mapped) 12100c17ba73SVincent Whitchurch return; 12110c17ba73SVincent Whitchurch 12120c17ba73SVincent Whitchurch if (xfer->tx_sg.orig_nents) 12130c17ba73SVincent Whitchurch dma_sync_sgtable_for_device(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 12140c17ba73SVincent Whitchurch if (xfer->rx_sg.orig_nents) 12150c17ba73SVincent Whitchurch dma_sync_sgtable_for_device(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 12160c17ba73SVincent Whitchurch } 12170c17ba73SVincent Whitchurch 12180c17ba73SVincent Whitchurch static void spi_dma_sync_for_cpu(struct spi_controller *ctlr, 12190c17ba73SVincent Whitchurch struct spi_transfer *xfer) 12200c17ba73SVincent Whitchurch { 12210c17ba73SVincent Whitchurch struct device *rx_dev = ctlr->cur_rx_dma_dev; 12220c17ba73SVincent Whitchurch struct device *tx_dev = ctlr->cur_tx_dma_dev; 12230c17ba73SVincent Whitchurch 12240c17ba73SVincent Whitchurch if (!ctlr->cur_msg_mapped) 12250c17ba73SVincent Whitchurch return; 12260c17ba73SVincent Whitchurch 12270c17ba73SVincent Whitchurch if (xfer->rx_sg.orig_nents) 12280c17ba73SVincent Whitchurch dma_sync_sgtable_for_cpu(rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 12290c17ba73SVincent Whitchurch if (xfer->tx_sg.orig_nents) 12300c17ba73SVincent Whitchurch dma_sync_sgtable_for_cpu(tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 12310c17ba73SVincent Whitchurch } 12322de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */ 12338caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr, 12342de440f5SGeert Uytterhoeven struct spi_message *msg) 12352de440f5SGeert Uytterhoeven { 12362de440f5SGeert Uytterhoeven return 0; 12372de440f5SGeert Uytterhoeven } 12382de440f5SGeert Uytterhoeven 12398caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr, 12402de440f5SGeert Uytterhoeven struct spi_message *msg) 12412de440f5SGeert Uytterhoeven { 12422de440f5SGeert Uytterhoeven return 0; 12432de440f5SGeert Uytterhoeven } 12440c17ba73SVincent Whitchurch 12450c17ba73SVincent Whitchurch static void spi_dma_sync_for_device(struct spi_controller *ctrl, 12460c17ba73SVincent Whitchurch struct spi_transfer *xfer) 12470c17ba73SVincent Whitchurch { 12480c17ba73SVincent Whitchurch } 12490c17ba73SVincent Whitchurch 12500c17ba73SVincent Whitchurch static void spi_dma_sync_for_cpu(struct spi_controller *ctrl, 12510c17ba73SVincent Whitchurch struct spi_transfer *xfer) 12520c17ba73SVincent Whitchurch { 12530c17ba73SVincent Whitchurch } 12542de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */ 12552de440f5SGeert Uytterhoeven 12568caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr, 12574b786458SMartin Sperl struct spi_message *msg) 12584b786458SMartin Sperl { 12594b786458SMartin Sperl struct spi_transfer *xfer; 12604b786458SMartin Sperl 12614b786458SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 12624b786458SMartin Sperl /* 12634b786458SMartin Sperl * Restore the original value of tx_buf or rx_buf if they are 12644b786458SMartin Sperl * NULL. 12654b786458SMartin Sperl */ 12668caab75fSGeert Uytterhoeven if (xfer->tx_buf == ctlr->dummy_tx) 12674b786458SMartin Sperl xfer->tx_buf = NULL; 12688caab75fSGeert Uytterhoeven if (xfer->rx_buf == ctlr->dummy_rx) 12694b786458SMartin Sperl xfer->rx_buf = NULL; 12704b786458SMartin Sperl } 12714b786458SMartin Sperl 12728caab75fSGeert Uytterhoeven return __spi_unmap_msg(ctlr, msg); 12734b786458SMartin Sperl } 12744b786458SMartin Sperl 12758caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 12762de440f5SGeert Uytterhoeven { 12772de440f5SGeert Uytterhoeven struct spi_transfer *xfer; 12782de440f5SGeert Uytterhoeven void *tmp; 12792de440f5SGeert Uytterhoeven unsigned int max_tx, max_rx; 12802de440f5SGeert Uytterhoeven 1281aee67fe8Sdillon min if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) 1282aee67fe8Sdillon min && !(msg->spi->mode & SPI_3WIRE)) { 12832de440f5SGeert Uytterhoeven max_tx = 0; 12842de440f5SGeert Uytterhoeven max_rx = 0; 12852de440f5SGeert Uytterhoeven 12862de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, transfer_list) { 12878caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && 12882de440f5SGeert Uytterhoeven !xfer->tx_buf) 12892de440f5SGeert Uytterhoeven max_tx = max(xfer->len, max_tx); 12908caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && 12912de440f5SGeert Uytterhoeven !xfer->rx_buf) 12922de440f5SGeert Uytterhoeven max_rx = max(xfer->len, max_rx); 12932de440f5SGeert Uytterhoeven } 12942de440f5SGeert Uytterhoeven 12952de440f5SGeert Uytterhoeven if (max_tx) { 12968caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_tx, max_tx, 1297b00bab9dSAndy Shevchenko GFP_KERNEL | GFP_DMA | __GFP_ZERO); 12982de440f5SGeert Uytterhoeven if (!tmp) 12992de440f5SGeert Uytterhoeven return -ENOMEM; 13008caab75fSGeert Uytterhoeven ctlr->dummy_tx = tmp; 13012de440f5SGeert Uytterhoeven } 13022de440f5SGeert Uytterhoeven 13032de440f5SGeert Uytterhoeven if (max_rx) { 13048caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_rx, max_rx, 13052de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 13062de440f5SGeert Uytterhoeven if (!tmp) 13072de440f5SGeert Uytterhoeven return -ENOMEM; 13088caab75fSGeert Uytterhoeven ctlr->dummy_rx = tmp; 13092de440f5SGeert Uytterhoeven } 13102de440f5SGeert Uytterhoeven 13112de440f5SGeert Uytterhoeven if (max_tx || max_rx) { 13122de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, 13132de440f5SGeert Uytterhoeven transfer_list) { 13145442dcaaSChris Lesiak if (!xfer->len) 13155442dcaaSChris Lesiak continue; 13162de440f5SGeert Uytterhoeven if (!xfer->tx_buf) 13178caab75fSGeert Uytterhoeven xfer->tx_buf = ctlr->dummy_tx; 13182de440f5SGeert Uytterhoeven if (!xfer->rx_buf) 13198caab75fSGeert Uytterhoeven xfer->rx_buf = ctlr->dummy_rx; 13202de440f5SGeert Uytterhoeven } 13212de440f5SGeert Uytterhoeven } 13222de440f5SGeert Uytterhoeven } 13232de440f5SGeert Uytterhoeven 13248caab75fSGeert Uytterhoeven return __spi_map_msg(ctlr, msg); 13252de440f5SGeert Uytterhoeven } 132699adef31SMark Brown 1327810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr, 1328810923f3SLubomir Rintel struct spi_message *msg, 1329810923f3SLubomir Rintel struct spi_transfer *xfer) 1330810923f3SLubomir Rintel { 1331d501cc4cSDavid Jander struct spi_statistics __percpu *statm = ctlr->pcpu_statistics; 1332d501cc4cSDavid Jander struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; 13336170d077SXu Yilun u32 speed_hz = xfer->speed_hz; 133449686df5SColin Ian King unsigned long long ms; 1335810923f3SLubomir Rintel 1336810923f3SLubomir Rintel if (spi_controller_is_slave(ctlr)) { 1337810923f3SLubomir Rintel if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { 1338810923f3SLubomir Rintel dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); 1339810923f3SLubomir Rintel return -EINTR; 1340810923f3SLubomir Rintel } 1341810923f3SLubomir Rintel } else { 13426170d077SXu Yilun if (!speed_hz) 13436170d077SXu Yilun speed_hz = 100000; 13446170d077SXu Yilun 134586b8bff7SAndy Shevchenko /* 134686b8bff7SAndy Shevchenko * For each byte we wait for 8 cycles of the SPI clock. 134786b8bff7SAndy Shevchenko * Since speed is defined in Hz and we want milliseconds, 134886b8bff7SAndy Shevchenko * use respective multiplier, but before the division, 134986b8bff7SAndy Shevchenko * otherwise we may get 0 for short transfers. 135086b8bff7SAndy Shevchenko */ 135186b8bff7SAndy Shevchenko ms = 8LL * MSEC_PER_SEC * xfer->len; 13526170d077SXu Yilun do_div(ms, speed_hz); 1353810923f3SLubomir Rintel 135486b8bff7SAndy Shevchenko /* 135586b8bff7SAndy Shevchenko * Increase it twice and add 200 ms tolerance, use 135686b8bff7SAndy Shevchenko * predefined maximum in case of overflow. 135786b8bff7SAndy Shevchenko */ 135886b8bff7SAndy Shevchenko ms += ms + 200; 1359810923f3SLubomir Rintel if (ms > UINT_MAX) 1360810923f3SLubomir Rintel ms = UINT_MAX; 1361810923f3SLubomir Rintel 1362810923f3SLubomir Rintel ms = wait_for_completion_timeout(&ctlr->xfer_completion, 1363810923f3SLubomir Rintel msecs_to_jiffies(ms)); 1364810923f3SLubomir Rintel 1365810923f3SLubomir Rintel if (ms == 0) { 1366810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(statm, timedout); 1367810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(stats, timedout); 1368810923f3SLubomir Rintel dev_err(&msg->spi->dev, 1369810923f3SLubomir Rintel "SPI transfer timed out\n"); 1370810923f3SLubomir Rintel return -ETIMEDOUT; 1371810923f3SLubomir Rintel } 1372810923f3SLubomir Rintel } 1373810923f3SLubomir Rintel 1374810923f3SLubomir Rintel return 0; 1375810923f3SLubomir Rintel } 1376810923f3SLubomir Rintel 13770ff2de8bSMartin Sperl static void _spi_transfer_delay_ns(u32 ns) 13780ff2de8bSMartin Sperl { 13790ff2de8bSMartin Sperl if (!ns) 13800ff2de8bSMartin Sperl return; 138186b8bff7SAndy Shevchenko if (ns <= NSEC_PER_USEC) { 13820ff2de8bSMartin Sperl ndelay(ns); 13830ff2de8bSMartin Sperl } else { 138486b8bff7SAndy Shevchenko u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC); 13850ff2de8bSMartin Sperl 13860ff2de8bSMartin Sperl if (us <= 10) 13870ff2de8bSMartin Sperl udelay(us); 13880ff2de8bSMartin Sperl else 13890ff2de8bSMartin Sperl usleep_range(us, us + DIV_ROUND_UP(us, 10)); 13900ff2de8bSMartin Sperl } 13910ff2de8bSMartin Sperl } 13920ff2de8bSMartin Sperl 13933984d39bSAlexandru Ardelean int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer) 13940ff2de8bSMartin Sperl { 1395b2c98153SAlexandru Ardelean u32 delay = _delay->value; 1396b2c98153SAlexandru Ardelean u32 unit = _delay->unit; 1397d5864e5bSMartin Sperl u32 hz; 13980ff2de8bSMartin Sperl 1399b2c98153SAlexandru Ardelean if (!delay) 1400b2c98153SAlexandru Ardelean return 0; 14010ff2de8bSMartin Sperl 14020ff2de8bSMartin Sperl switch (unit) { 14030ff2de8bSMartin Sperl case SPI_DELAY_UNIT_USECS: 140486b8bff7SAndy Shevchenko delay *= NSEC_PER_USEC; 14050ff2de8bSMartin Sperl break; 140686b8bff7SAndy Shevchenko case SPI_DELAY_UNIT_NSECS: 140786b8bff7SAndy Shevchenko /* Nothing to do here */ 14080ff2de8bSMartin Sperl break; 1409d5864e5bSMartin Sperl case SPI_DELAY_UNIT_SCK: 141095c8222fSDavid Jander /* Clock cycles need to be obtained from spi_transfer */ 1411b2c98153SAlexandru Ardelean if (!xfer) 1412b2c98153SAlexandru Ardelean return -EINVAL; 141386b8bff7SAndy Shevchenko /* 141486b8bff7SAndy Shevchenko * If there is unknown effective speed, approximate it 1415702ca026SAndy Shevchenko * by underestimating with half of the requested Hz. 1416d5864e5bSMartin Sperl */ 1417d5864e5bSMartin Sperl hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; 1418b2c98153SAlexandru Ardelean if (!hz) 1419b2c98153SAlexandru Ardelean return -EINVAL; 142086b8bff7SAndy Shevchenko 142186b8bff7SAndy Shevchenko /* Convert delay to nanoseconds */ 142286b8bff7SAndy Shevchenko delay *= DIV_ROUND_UP(NSEC_PER_SEC, hz); 1423d5864e5bSMartin Sperl break; 14240ff2de8bSMartin Sperl default: 1425b2c98153SAlexandru Ardelean return -EINVAL; 1426b2c98153SAlexandru Ardelean } 1427b2c98153SAlexandru Ardelean 1428b2c98153SAlexandru Ardelean return delay; 1429b2c98153SAlexandru Ardelean } 14303984d39bSAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_to_ns); 1431b2c98153SAlexandru Ardelean 1432b2c98153SAlexandru Ardelean int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer) 1433b2c98153SAlexandru Ardelean { 1434b2c98153SAlexandru Ardelean int delay; 1435b2c98153SAlexandru Ardelean 14368fede89fSMark Brown might_sleep(); 14378fede89fSMark Brown 1438b2c98153SAlexandru Ardelean if (!_delay) 1439b2c98153SAlexandru Ardelean return -EINVAL; 1440b2c98153SAlexandru Ardelean 14413984d39bSAlexandru Ardelean delay = spi_delay_to_ns(_delay, xfer); 1442b2c98153SAlexandru Ardelean if (delay < 0) 1443b2c98153SAlexandru Ardelean return delay; 1444b2c98153SAlexandru Ardelean 1445b2c98153SAlexandru Ardelean _spi_transfer_delay_ns(delay); 1446b2c98153SAlexandru Ardelean 1447b2c98153SAlexandru Ardelean return 0; 1448b2c98153SAlexandru Ardelean } 1449b2c98153SAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_exec); 1450b2c98153SAlexandru Ardelean 14510ff2de8bSMartin Sperl static void _spi_transfer_cs_change_delay(struct spi_message *msg, 14520ff2de8bSMartin Sperl struct spi_transfer *xfer) 14530ff2de8bSMartin Sperl { 145486b8bff7SAndy Shevchenko u32 default_delay_ns = 10 * NSEC_PER_USEC; 1455329f0dacSAlexandru Ardelean u32 delay = xfer->cs_change_delay.value; 1456329f0dacSAlexandru Ardelean u32 unit = xfer->cs_change_delay.unit; 1457329f0dacSAlexandru Ardelean int ret; 14580ff2de8bSMartin Sperl 145995c8222fSDavid Jander /* Return early on "fast" mode - for everything but USECS */ 14606b3f236aSAlexandru Ardelean if (!delay) { 14616b3f236aSAlexandru Ardelean if (unit == SPI_DELAY_UNIT_USECS) 146286b8bff7SAndy Shevchenko _spi_transfer_delay_ns(default_delay_ns); 14630ff2de8bSMartin Sperl return; 14646b3f236aSAlexandru Ardelean } 14650ff2de8bSMartin Sperl 1466329f0dacSAlexandru Ardelean ret = spi_delay_exec(&xfer->cs_change_delay, xfer); 1467329f0dacSAlexandru Ardelean if (ret) { 14680ff2de8bSMartin Sperl dev_err_once(&msg->spi->dev, 146986b8bff7SAndy Shevchenko "Use of unsupported delay unit %i, using default of %luus\n", 147086b8bff7SAndy Shevchenko unit, default_delay_ns / NSEC_PER_USEC); 147186b8bff7SAndy Shevchenko _spi_transfer_delay_ns(default_delay_ns); 14720ff2de8bSMartin Sperl } 14730ff2de8bSMartin Sperl } 14740ff2de8bSMartin Sperl 14756e80133aSWilliam Zhang void spi_transfer_cs_change_delay_exec(struct spi_message *msg, 14766e80133aSWilliam Zhang struct spi_transfer *xfer) 14776e80133aSWilliam Zhang { 14786e80133aSWilliam Zhang _spi_transfer_cs_change_delay(msg, xfer); 14796e80133aSWilliam Zhang } 14806e80133aSWilliam Zhang EXPORT_SYMBOL_GPL(spi_transfer_cs_change_delay_exec); 14816e80133aSWilliam Zhang 1482b158935fSMark Brown /* 1483b158935fSMark Brown * spi_transfer_one_message - Default implementation of transfer_one_message() 1484b158935fSMark Brown * 1485b158935fSMark Brown * This is a standard implementation of transfer_one_message() for 14868ba811a7SMoritz Fischer * drivers which implement a transfer_one() operation. It provides 1487b158935fSMark Brown * standard handling of delays and chip select management. 1488b158935fSMark Brown */ 14898caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr, 1490b158935fSMark Brown struct spi_message *msg) 1491b158935fSMark Brown { 1492b158935fSMark Brown struct spi_transfer *xfer; 1493b158935fSMark Brown bool keep_cs = false; 1494b158935fSMark Brown int ret = 0; 1495d501cc4cSDavid Jander struct spi_statistics __percpu *statm = ctlr->pcpu_statistics; 1496d501cc4cSDavid Jander struct spi_statistics __percpu *stats = msg->spi->pcpu_statistics; 1497b158935fSMark Brown 14985e0531f6SChristophe Leroy xfer = list_first_entry(&msg->transfers, struct spi_transfer, transfer_list); 14995e0531f6SChristophe Leroy spi_set_cs(msg->spi, !xfer->cs_off, false); 1500b158935fSMark Brown 1501eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, messages); 1502eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, messages); 1503eca2ebc7SMartin Sperl 1504b158935fSMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1505b158935fSMark Brown trace_spi_transfer_start(msg, xfer); 1506b158935fSMark Brown 15078caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(statm, xfer, ctlr); 15088caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(stats, xfer, ctlr); 1509eca2ebc7SMartin Sperl 1510b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 1511b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 1512b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1513b42faeeeSVladimir Oltean } 1514b42faeeeSVladimir Oltean 1515b3063203SNicolas Saenz Julienne if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) { 15168caab75fSGeert Uytterhoeven reinit_completion(&ctlr->xfer_completion); 1517b158935fSMark Brown 1518809b1b04SRobin Gong fallback_pio: 15190c17ba73SVincent Whitchurch spi_dma_sync_for_device(ctlr, xfer); 15208caab75fSGeert Uytterhoeven ret = ctlr->transfer_one(ctlr, msg->spi, xfer); 1521b158935fSMark Brown if (ret < 0) { 15220c17ba73SVincent Whitchurch spi_dma_sync_for_cpu(ctlr, xfer); 15230c17ba73SVincent Whitchurch 1524809b1b04SRobin Gong if (ctlr->cur_msg_mapped && 1525809b1b04SRobin Gong (xfer->error & SPI_TRANS_FAIL_NO_START)) { 1526809b1b04SRobin Gong __spi_unmap_msg(ctlr, msg); 1527809b1b04SRobin Gong ctlr->fallback = true; 1528809b1b04SRobin Gong xfer->error &= ~SPI_TRANS_FAIL_NO_START; 1529809b1b04SRobin Gong goto fallback_pio; 1530809b1b04SRobin Gong } 1531809b1b04SRobin Gong 1532eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, 1533eca2ebc7SMartin Sperl errors); 1534eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, 1535eca2ebc7SMartin Sperl errors); 1536b158935fSMark Brown dev_err(&msg->spi->dev, 1537b158935fSMark Brown "SPI transfer failed: %d\n", ret); 1538b158935fSMark Brown goto out; 1539b158935fSMark Brown } 1540b158935fSMark Brown 1541d57e7960SMark Brown if (ret > 0) { 1542810923f3SLubomir Rintel ret = spi_transfer_wait(ctlr, msg, xfer); 1543810923f3SLubomir Rintel if (ret < 0) 1544810923f3SLubomir Rintel msg->status = ret; 1545d57e7960SMark Brown } 15460c17ba73SVincent Whitchurch 15470c17ba73SVincent Whitchurch spi_dma_sync_for_cpu(ctlr, xfer); 154838ec10f6SMark Brown } else { 154938ec10f6SMark Brown if (xfer->len) 155038ec10f6SMark Brown dev_err(&msg->spi->dev, 155138ec10f6SMark Brown "Bufferless transfer has length %u\n", 155238ec10f6SMark Brown xfer->len); 155338ec10f6SMark Brown } 1554b158935fSMark Brown 1555b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 1556b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1557b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 1558b42faeeeSVladimir Oltean } 1559b42faeeeSVladimir Oltean 1560b158935fSMark Brown trace_spi_transfer_stop(msg, xfer); 1561b158935fSMark Brown 1562b158935fSMark Brown if (msg->status != -EINPROGRESS) 1563b158935fSMark Brown goto out; 1564b158935fSMark Brown 1565bebcfd27SAlexandru Ardelean spi_transfer_delay_exec(xfer); 1566b158935fSMark Brown 1567b158935fSMark Brown if (xfer->cs_change) { 1568b158935fSMark Brown if (list_is_last(&xfer->transfer_list, 1569b158935fSMark Brown &msg->transfers)) { 1570b158935fSMark Brown keep_cs = true; 1571b158935fSMark Brown } else { 15725e0531f6SChristophe Leroy if (!xfer->cs_off) 1573d347b4aaSDavid Bauer spi_set_cs(msg->spi, false, false); 15740ff2de8bSMartin Sperl _spi_transfer_cs_change_delay(msg, xfer); 15755e0531f6SChristophe Leroy if (!list_next_entry(xfer, transfer_list)->cs_off) 1576d347b4aaSDavid Bauer spi_set_cs(msg->spi, true, false); 1577b158935fSMark Brown } 15785e0531f6SChristophe Leroy } else if (!list_is_last(&xfer->transfer_list, &msg->transfers) && 15795e0531f6SChristophe Leroy xfer->cs_off != list_next_entry(xfer, transfer_list)->cs_off) { 15805e0531f6SChristophe Leroy spi_set_cs(msg->spi, xfer->cs_off, false); 1581b158935fSMark Brown } 1582b158935fSMark Brown 1583b158935fSMark Brown msg->actual_length += xfer->len; 1584b158935fSMark Brown } 1585b158935fSMark Brown 1586b158935fSMark Brown out: 1587b158935fSMark Brown if (ret != 0 || !keep_cs) 1588d347b4aaSDavid Bauer spi_set_cs(msg->spi, false, false); 1589b158935fSMark Brown 1590b158935fSMark Brown if (msg->status == -EINPROGRESS) 1591b158935fSMark Brown msg->status = ret; 1592b158935fSMark Brown 15938caab75fSGeert Uytterhoeven if (msg->status && ctlr->handle_err) 15948caab75fSGeert Uytterhoeven ctlr->handle_err(ctlr, msg); 1595b716c4ffSAndy Shevchenko 15960ed56252SMark Brown spi_finalize_current_message(ctlr); 15970ed56252SMark Brown 1598b158935fSMark Brown return ret; 1599b158935fSMark Brown } 1600b158935fSMark Brown 1601b158935fSMark Brown /** 1602b158935fSMark Brown * spi_finalize_current_transfer - report completion of a transfer 16038caab75fSGeert Uytterhoeven * @ctlr: the controller reporting completion 1604b158935fSMark Brown * 1605b158935fSMark Brown * Called by SPI drivers using the core transfer_one_message() 1606b158935fSMark Brown * implementation to notify it that the current interrupt driven 16079e8f4882SGeert Uytterhoeven * transfer has finished and the next one may be scheduled. 1608b158935fSMark Brown */ 16098caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr) 1610b158935fSMark Brown { 16118caab75fSGeert Uytterhoeven complete(&ctlr->xfer_completion); 1612b158935fSMark Brown } 1613b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); 1614b158935fSMark Brown 1615e1268597SMark Brown static void spi_idle_runtime_pm(struct spi_controller *ctlr) 1616e1268597SMark Brown { 1617e1268597SMark Brown if (ctlr->auto_runtime_pm) { 1618e1268597SMark Brown pm_runtime_mark_last_busy(ctlr->dev.parent); 1619e1268597SMark Brown pm_runtime_put_autosuspend(ctlr->dev.parent); 1620e1268597SMark Brown } 1621e1268597SMark Brown } 1622e1268597SMark Brown 1623ae7d2346SDavid Jander static int __spi_pump_transfer_message(struct spi_controller *ctlr, 1624ae7d2346SDavid Jander struct spi_message *msg, bool was_busy) 1625ae7d2346SDavid Jander { 1626ae7d2346SDavid Jander struct spi_transfer *xfer; 1627ae7d2346SDavid Jander int ret; 1628ae7d2346SDavid Jander 1629ae7d2346SDavid Jander if (!was_busy && ctlr->auto_runtime_pm) { 1630ae7d2346SDavid Jander ret = pm_runtime_get_sync(ctlr->dev.parent); 1631ae7d2346SDavid Jander if (ret < 0) { 1632ae7d2346SDavid Jander pm_runtime_put_noidle(ctlr->dev.parent); 1633ae7d2346SDavid Jander dev_err(&ctlr->dev, "Failed to power device: %d\n", 1634ae7d2346SDavid Jander ret); 1635ae7d2346SDavid Jander return ret; 1636ae7d2346SDavid Jander } 1637ae7d2346SDavid Jander } 1638ae7d2346SDavid Jander 1639ae7d2346SDavid Jander if (!was_busy) 1640ae7d2346SDavid Jander trace_spi_controller_busy(ctlr); 1641ae7d2346SDavid Jander 1642ae7d2346SDavid Jander if (!was_busy && ctlr->prepare_transfer_hardware) { 1643ae7d2346SDavid Jander ret = ctlr->prepare_transfer_hardware(ctlr); 1644ae7d2346SDavid Jander if (ret) { 1645ae7d2346SDavid Jander dev_err(&ctlr->dev, 1646ae7d2346SDavid Jander "failed to prepare transfer hardware: %d\n", 1647ae7d2346SDavid Jander ret); 1648ae7d2346SDavid Jander 1649ae7d2346SDavid Jander if (ctlr->auto_runtime_pm) 1650ae7d2346SDavid Jander pm_runtime_put(ctlr->dev.parent); 1651ae7d2346SDavid Jander 1652ae7d2346SDavid Jander msg->status = ret; 1653ae7d2346SDavid Jander spi_finalize_current_message(ctlr); 1654ae7d2346SDavid Jander 1655ae7d2346SDavid Jander return ret; 1656ae7d2346SDavid Jander } 1657ae7d2346SDavid Jander } 1658ae7d2346SDavid Jander 1659ae7d2346SDavid Jander trace_spi_message_start(msg); 1660ae7d2346SDavid Jander 16618d699ff9SVincent Whitchurch ret = spi_split_transfers_maxsize(ctlr, msg, 16628d699ff9SVincent Whitchurch spi_max_transfer_size(msg->spi), 16638d699ff9SVincent Whitchurch GFP_KERNEL | GFP_DMA); 16648d699ff9SVincent Whitchurch if (ret) { 16658d699ff9SVincent Whitchurch msg->status = ret; 16668d699ff9SVincent Whitchurch spi_finalize_current_message(ctlr); 16678d699ff9SVincent Whitchurch return ret; 16688d699ff9SVincent Whitchurch } 16698d699ff9SVincent Whitchurch 1670ae7d2346SDavid Jander if (ctlr->prepare_message) { 1671ae7d2346SDavid Jander ret = ctlr->prepare_message(ctlr, msg); 1672ae7d2346SDavid Jander if (ret) { 1673ae7d2346SDavid Jander dev_err(&ctlr->dev, "failed to prepare message: %d\n", 1674ae7d2346SDavid Jander ret); 1675ae7d2346SDavid Jander msg->status = ret; 1676ae7d2346SDavid Jander spi_finalize_current_message(ctlr); 1677ae7d2346SDavid Jander return ret; 1678ae7d2346SDavid Jander } 1679ae7d2346SDavid Jander msg->prepared = true; 1680ae7d2346SDavid Jander } 1681ae7d2346SDavid Jander 1682ae7d2346SDavid Jander ret = spi_map_msg(ctlr, msg); 1683ae7d2346SDavid Jander if (ret) { 1684ae7d2346SDavid Jander msg->status = ret; 1685ae7d2346SDavid Jander spi_finalize_current_message(ctlr); 1686ae7d2346SDavid Jander return ret; 1687ae7d2346SDavid Jander } 1688ae7d2346SDavid Jander 1689ae7d2346SDavid Jander if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1690ae7d2346SDavid Jander list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1691ae7d2346SDavid Jander xfer->ptp_sts_word_pre = 0; 1692ae7d2346SDavid Jander ptp_read_system_prets(xfer->ptp_sts); 1693ae7d2346SDavid Jander } 1694ae7d2346SDavid Jander } 1695ae7d2346SDavid Jander 1696dc302905SDavid Jander /* 1697dc302905SDavid Jander * Drivers implementation of transfer_one_message() must arrange for 1698dc302905SDavid Jander * spi_finalize_current_message() to get called. Most drivers will do 1699dc302905SDavid Jander * this in the calling context, but some don't. For those cases, a 1700dc302905SDavid Jander * completion is used to guarantee that this function does not return 1701dc302905SDavid Jander * until spi_finalize_current_message() is done accessing 1702dc302905SDavid Jander * ctlr->cur_msg. 1703dc302905SDavid Jander * Use of the following two flags enable to opportunistically skip the 1704dc302905SDavid Jander * use of the completion since its use involves expensive spin locks. 1705dc302905SDavid Jander * In case of a race with the context that calls 1706dc302905SDavid Jander * spi_finalize_current_message() the completion will always be used, 1707dc302905SDavid Jander * due to strict ordering of these flags using barriers. 1708dc302905SDavid Jander */ 1709dc302905SDavid Jander WRITE_ONCE(ctlr->cur_msg_incomplete, true); 1710dc302905SDavid Jander WRITE_ONCE(ctlr->cur_msg_need_completion, false); 171169fa9590SDavid Jander reinit_completion(&ctlr->cur_msg_completion); 171295c8222fSDavid Jander smp_wmb(); /* Make these available to spi_finalize_current_message() */ 1713dc302905SDavid Jander 1714ae7d2346SDavid Jander ret = ctlr->transfer_one_message(ctlr, msg); 1715ae7d2346SDavid Jander if (ret) { 1716ae7d2346SDavid Jander dev_err(&ctlr->dev, 1717ae7d2346SDavid Jander "failed to transfer one message from queue\n"); 1718ae7d2346SDavid Jander return ret; 171931d4c1bdSDavid Jander } 172031d4c1bdSDavid Jander 1721dc302905SDavid Jander WRITE_ONCE(ctlr->cur_msg_need_completion, true); 172231d4c1bdSDavid Jander smp_mb(); /* See spi_finalize_current_message()... */ 1723dc302905SDavid Jander if (READ_ONCE(ctlr->cur_msg_incomplete)) 172469fa9590SDavid Jander wait_for_completion(&ctlr->cur_msg_completion); 1725ae7d2346SDavid Jander 1726ae7d2346SDavid Jander return 0; 1727ae7d2346SDavid Jander } 1728ae7d2346SDavid Jander 1729ffbbdd21SLinus Walleij /** 1730702ca026SAndy Shevchenko * __spi_pump_messages - function which processes SPI message queue 17318caab75fSGeert Uytterhoeven * @ctlr: controller to process queue for 1732fc9e0f71SMark Brown * @in_kthread: true if we are in the context of the message pump thread 1733ffbbdd21SLinus Walleij * 1734702ca026SAndy Shevchenko * This function checks if there is any SPI message in the queue that 1735ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 1736ffbbdd21SLinus Walleij * and transfer each message. 1737ffbbdd21SLinus Walleij * 17380461a414SMark Brown * Note that it is called both from the kthread itself and also from 17390461a414SMark Brown * inside spi_sync(); the queue extraction handling at the top of the 17400461a414SMark Brown * function should deal with this safely. 1741ffbbdd21SLinus Walleij */ 17428caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1743ffbbdd21SLinus Walleij { 1744d1c44c93SVladimir Oltean struct spi_message *msg; 1745ffbbdd21SLinus Walleij bool was_busy = false; 1746d1c44c93SVladimir Oltean unsigned long flags; 1747ffbbdd21SLinus Walleij int ret; 1748ffbbdd21SLinus Walleij 1749702ca026SAndy Shevchenko /* Take the I/O mutex */ 1750c1038165SDavid Jander mutex_lock(&ctlr->io_mutex); 1751c1038165SDavid Jander 1752983aee5dSMark Brown /* Lock queue */ 17538caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1754983aee5dSMark Brown 1755983aee5dSMark Brown /* Make sure we are not already running a message */ 17568711a2abSDavid Jander if (ctlr->cur_msg) 1757c1038165SDavid Jander goto out_unlock; 1758983aee5dSMark Brown 1759983aee5dSMark Brown /* Check if the queue is idle */ 17608caab75fSGeert Uytterhoeven if (list_empty(&ctlr->queue) || !ctlr->running) { 17618711a2abSDavid Jander if (!ctlr->busy) 1762c1038165SDavid Jander goto out_unlock; 1763fc9e0f71SMark Brown 1764e1268597SMark Brown /* Defer any non-atomic teardown to the thread */ 1765f0125f1aSMark Brown if (!in_kthread) { 1766e1268597SMark Brown if (!ctlr->dummy_rx && !ctlr->dummy_tx && 1767e1268597SMark Brown !ctlr->unprepare_transfer_hardware) { 1768e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1769e1268597SMark Brown ctlr->busy = false; 1770ae7d2346SDavid Jander ctlr->queue_empty = true; 1771e1268597SMark Brown trace_spi_controller_idle(ctlr); 1772e1268597SMark Brown } else { 177360a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, 1774f0125f1aSMark Brown &ctlr->pump_messages); 1775e1268597SMark Brown } 1776c1038165SDavid Jander goto out_unlock; 1777f0125f1aSMark Brown } 1778f0125f1aSMark Brown 1779f0125f1aSMark Brown ctlr->busy = false; 1780f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1781f0125f1aSMark Brown 1782f0125f1aSMark Brown kfree(ctlr->dummy_rx); 1783f0125f1aSMark Brown ctlr->dummy_rx = NULL; 1784f0125f1aSMark Brown kfree(ctlr->dummy_tx); 1785f0125f1aSMark Brown ctlr->dummy_tx = NULL; 1786f0125f1aSMark Brown if (ctlr->unprepare_transfer_hardware && 1787f0125f1aSMark Brown ctlr->unprepare_transfer_hardware(ctlr)) 1788f0125f1aSMark Brown dev_err(&ctlr->dev, 1789f0125f1aSMark Brown "failed to unprepare transfer hardware\n"); 1790e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1791f0125f1aSMark Brown trace_spi_controller_idle(ctlr); 1792f0125f1aSMark Brown 1793f0125f1aSMark Brown spin_lock_irqsave(&ctlr->queue_lock, flags); 1794ae7d2346SDavid Jander ctlr->queue_empty = true; 1795c1038165SDavid Jander goto out_unlock; 1796ffbbdd21SLinus Walleij } 1797ffbbdd21SLinus Walleij 1798ffbbdd21SLinus Walleij /* Extract head of queue */ 1799d1c44c93SVladimir Oltean msg = list_first_entry(&ctlr->queue, struct spi_message, queue); 1800d1c44c93SVladimir Oltean ctlr->cur_msg = msg; 1801ffbbdd21SLinus Walleij 1802d1c44c93SVladimir Oltean list_del_init(&msg->queue); 18038caab75fSGeert Uytterhoeven if (ctlr->busy) 1804ffbbdd21SLinus Walleij was_busy = true; 1805ffbbdd21SLinus Walleij else 18068caab75fSGeert Uytterhoeven ctlr->busy = true; 18078caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1808ffbbdd21SLinus Walleij 1809ae7d2346SDavid Jander ret = __spi_pump_transfer_message(ctlr, msg, was_busy); 181069fa9590SDavid Jander kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1811c191543eSDavid Jander 181269fa9590SDavid Jander ctlr->cur_msg = NULL; 181369fa9590SDavid Jander ctlr->fallback = false; 181469fa9590SDavid Jander 18158caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 181662826970SMark Brown 181762826970SMark Brown /* Prod the scheduler in case transfer_one() was busy waiting */ 181849023d2eSJon Hunter if (!ret) 181962826970SMark Brown cond_resched(); 1820c1038165SDavid Jander return; 1821c1038165SDavid Jander 1822c1038165SDavid Jander out_unlock: 18238711a2abSDavid Jander spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1824c1038165SDavid Jander mutex_unlock(&ctlr->io_mutex); 1825ffbbdd21SLinus Walleij } 1826ffbbdd21SLinus Walleij 1827fc9e0f71SMark Brown /** 1828fc9e0f71SMark Brown * spi_pump_messages - kthread work function which processes spi message queue 18298caab75fSGeert Uytterhoeven * @work: pointer to kthread work struct contained in the controller struct 1830fc9e0f71SMark Brown */ 1831fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work) 1832fc9e0f71SMark Brown { 18338caab75fSGeert Uytterhoeven struct spi_controller *ctlr = 18348caab75fSGeert Uytterhoeven container_of(work, struct spi_controller, pump_messages); 1835fc9e0f71SMark Brown 18368caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, true); 1837fc9e0f71SMark Brown } 1838fc9e0f71SMark Brown 1839924b5867SDouglas Anderson /** 1840350de7ceSAndy Shevchenko * spi_take_timestamp_pre - helper to collect the beginning of the TX timestamp 1841b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1842b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1843862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1844b42faeeeSVladimir Oltean * @irqs_off: If true, will disable IRQs and preemption for the duration of the 1845b42faeeeSVladimir Oltean * transfer, for less jitter in time measurement. Only compatible 1846b42faeeeSVladimir Oltean * with PIO drivers. If true, must follow up with 1847b42faeeeSVladimir Oltean * spi_take_timestamp_post or otherwise system will crash. 1848b42faeeeSVladimir Oltean * WARNING: for fully predictable results, the CPU frequency must 1849b42faeeeSVladimir Oltean * also be under control (governor). 1850350de7ceSAndy Shevchenko * 1851350de7ceSAndy Shevchenko * This is a helper for drivers to collect the beginning of the TX timestamp 1852350de7ceSAndy Shevchenko * for the requested byte from the SPI transfer. The frequency with which this 1853350de7ceSAndy Shevchenko * function must be called (once per word, once for the whole transfer, once 1854350de7ceSAndy Shevchenko * per batch of words etc) is arbitrary as long as the @tx buffer offset is 1855350de7ceSAndy Shevchenko * greater than or equal to the requested byte at the time of the call. The 1856350de7ceSAndy Shevchenko * timestamp is only taken once, at the first such call. It is assumed that 1857350de7ceSAndy Shevchenko * the driver advances its @tx buffer pointer monotonically. 1858b42faeeeSVladimir Oltean */ 1859b42faeeeSVladimir Oltean void spi_take_timestamp_pre(struct spi_controller *ctlr, 1860b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1861862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1862b42faeeeSVladimir Oltean { 1863b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1864b42faeeeSVladimir Oltean return; 1865b42faeeeSVladimir Oltean 18666a726824SVladimir Oltean if (xfer->timestamped) 1867b42faeeeSVladimir Oltean return; 1868b42faeeeSVladimir Oltean 18696a726824SVladimir Oltean if (progress > xfer->ptp_sts_word_pre) 1870b42faeeeSVladimir Oltean return; 1871b42faeeeSVladimir Oltean 1872b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1873862dd2a9SVladimir Oltean xfer->ptp_sts_word_pre = progress; 1874b42faeeeSVladimir Oltean 1875b42faeeeSVladimir Oltean if (irqs_off) { 1876b42faeeeSVladimir Oltean local_irq_save(ctlr->irq_flags); 1877b42faeeeSVladimir Oltean preempt_disable(); 1878b42faeeeSVladimir Oltean } 1879b42faeeeSVladimir Oltean 1880b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1881b42faeeeSVladimir Oltean } 1882b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_pre); 1883b42faeeeSVladimir Oltean 1884b42faeeeSVladimir Oltean /** 1885350de7ceSAndy Shevchenko * spi_take_timestamp_post - helper to collect the end of the TX timestamp 1886b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1887b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1888862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1889b42faeeeSVladimir Oltean * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU. 1890350de7ceSAndy Shevchenko * 1891350de7ceSAndy Shevchenko * This is a helper for drivers to collect the end of the TX timestamp for 1892350de7ceSAndy Shevchenko * the requested byte from the SPI transfer. Can be called with an arbitrary 1893350de7ceSAndy Shevchenko * frequency: only the first call where @tx exceeds or is equal to the 1894350de7ceSAndy Shevchenko * requested word will be timestamped. 1895b42faeeeSVladimir Oltean */ 1896b42faeeeSVladimir Oltean void spi_take_timestamp_post(struct spi_controller *ctlr, 1897b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1898862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1899b42faeeeSVladimir Oltean { 1900b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1901b42faeeeSVladimir Oltean return; 1902b42faeeeSVladimir Oltean 19036a726824SVladimir Oltean if (xfer->timestamped) 1904b42faeeeSVladimir Oltean return; 1905b42faeeeSVladimir Oltean 1906862dd2a9SVladimir Oltean if (progress < xfer->ptp_sts_word_post) 1907b42faeeeSVladimir Oltean return; 1908b42faeeeSVladimir Oltean 1909b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1910b42faeeeSVladimir Oltean 1911b42faeeeSVladimir Oltean if (irqs_off) { 1912b42faeeeSVladimir Oltean local_irq_restore(ctlr->irq_flags); 1913b42faeeeSVladimir Oltean preempt_enable(); 1914b42faeeeSVladimir Oltean } 1915b42faeeeSVladimir Oltean 1916b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1917862dd2a9SVladimir Oltean xfer->ptp_sts_word_post = progress; 1918b42faeeeSVladimir Oltean 19199d77522bSChristophe JAILLET xfer->timestamped = 1; 1920b42faeeeSVladimir Oltean } 1921b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_post); 1922b42faeeeSVladimir Oltean 1923b42faeeeSVladimir Oltean /** 1924924b5867SDouglas Anderson * spi_set_thread_rt - set the controller to pump at realtime priority 1925924b5867SDouglas Anderson * @ctlr: controller to boost priority of 1926924b5867SDouglas Anderson * 1927924b5867SDouglas Anderson * This can be called because the controller requested realtime priority 1928924b5867SDouglas Anderson * (by setting the ->rt value before calling spi_register_controller()) or 1929924b5867SDouglas Anderson * because a device on the bus said that its transfers needed realtime 1930924b5867SDouglas Anderson * priority. 1931924b5867SDouglas Anderson * 1932924b5867SDouglas Anderson * NOTE: at the moment if any device on a bus says it needs realtime then 1933924b5867SDouglas Anderson * the thread will be at realtime priority for all transfers on that 1934924b5867SDouglas Anderson * controller. If this eventually becomes a problem we may see if we can 1935924b5867SDouglas Anderson * find a way to boost the priority only temporarily during relevant 1936924b5867SDouglas Anderson * transfers. 1937924b5867SDouglas Anderson */ 1938924b5867SDouglas Anderson static void spi_set_thread_rt(struct spi_controller *ctlr) 1939ffbbdd21SLinus Walleij { 1940924b5867SDouglas Anderson dev_info(&ctlr->dev, 1941924b5867SDouglas Anderson "will run message pump with realtime priority\n"); 19426d2b84a4SLinus Torvalds sched_set_fifo(ctlr->kworker->task); 1943924b5867SDouglas Anderson } 1944924b5867SDouglas Anderson 1945924b5867SDouglas Anderson static int spi_init_queue(struct spi_controller *ctlr) 1946924b5867SDouglas Anderson { 19478caab75fSGeert Uytterhoeven ctlr->running = false; 19488caab75fSGeert Uytterhoeven ctlr->busy = false; 1949ae7d2346SDavid Jander ctlr->queue_empty = true; 1950ffbbdd21SLinus Walleij 195160a883d1SMarek Szyprowski ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); 195260a883d1SMarek Szyprowski if (IS_ERR(ctlr->kworker)) { 195360a883d1SMarek Szyprowski dev_err(&ctlr->dev, "failed to create message pump kworker\n"); 195460a883d1SMarek Szyprowski return PTR_ERR(ctlr->kworker); 1955ffbbdd21SLinus Walleij } 195660a883d1SMarek Szyprowski 19578caab75fSGeert Uytterhoeven kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1958f0125f1aSMark Brown 1959ffbbdd21SLinus Walleij /* 19608caab75fSGeert Uytterhoeven * Controller config will indicate if this controller should run the 1961ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 1962ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 1963ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 1964ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 1965ffbbdd21SLinus Walleij */ 1966924b5867SDouglas Anderson if (ctlr->rt) 1967924b5867SDouglas Anderson spi_set_thread_rt(ctlr); 1968ffbbdd21SLinus Walleij 1969ffbbdd21SLinus Walleij return 0; 1970ffbbdd21SLinus Walleij } 1971ffbbdd21SLinus Walleij 1972ffbbdd21SLinus Walleij /** 1973ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 1974ffbbdd21SLinus Walleij * messages 19758caab75fSGeert Uytterhoeven * @ctlr: the controller to check for queued messages 1976ffbbdd21SLinus Walleij * 1977ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 1978ffbbdd21SLinus Walleij * this call. 197997d56dc6SJavier Martinez Canillas * 198097d56dc6SJavier Martinez Canillas * Return: the next message in the queue, else NULL if the queue is empty. 1981ffbbdd21SLinus Walleij */ 19828caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1983ffbbdd21SLinus Walleij { 1984ffbbdd21SLinus Walleij struct spi_message *next; 1985ffbbdd21SLinus Walleij unsigned long flags; 1986ffbbdd21SLinus Walleij 198795c8222fSDavid Jander /* Get a pointer to the next message, if any */ 19888caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 19898caab75fSGeert Uytterhoeven next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 19901cfd97f9SAxel Lin queue); 19918caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1992ffbbdd21SLinus Walleij 1993ffbbdd21SLinus Walleij return next; 1994ffbbdd21SLinus Walleij } 1995ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1996ffbbdd21SLinus Walleij 1997ffbbdd21SLinus Walleij /** 1998ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 19998caab75fSGeert Uytterhoeven * @ctlr: the controller to return the message to 2000ffbbdd21SLinus Walleij * 2001ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 2002ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 2003ffbbdd21SLinus Walleij */ 20048caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr) 2005ffbbdd21SLinus Walleij { 2006b42faeeeSVladimir Oltean struct spi_transfer *xfer; 2007ffbbdd21SLinus Walleij struct spi_message *mesg; 20082841a5fcSMark Brown int ret; 2009ffbbdd21SLinus Walleij 20108caab75fSGeert Uytterhoeven mesg = ctlr->cur_msg; 2011ffbbdd21SLinus Walleij 2012b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 2013b42faeeeSVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) { 2014b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 2015b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 2016b42faeeeSVladimir Oltean } 2017b42faeeeSVladimir Oltean } 2018b42faeeeSVladimir Oltean 20196a726824SVladimir Oltean if (unlikely(ctlr->ptp_sts_supported)) 20206a726824SVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) 20216a726824SVladimir Oltean WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); 2022f971a207SVladimir Oltean 20238caab75fSGeert Uytterhoeven spi_unmap_msg(ctlr, mesg); 202499adef31SMark Brown 2025350de7ceSAndy Shevchenko /* 2026350de7ceSAndy Shevchenko * In the prepare_messages callback the SPI bus has the opportunity 2027350de7ceSAndy Shevchenko * to split a transfer to smaller chunks. 2028350de7ceSAndy Shevchenko * 2029350de7ceSAndy Shevchenko * Release the split transfers here since spi_map_msg() is done on 2030350de7ceSAndy Shevchenko * the split transfers. 2031b59a7ca1SGustav Wiklander */ 2032b59a7ca1SGustav Wiklander spi_res_release(ctlr, mesg); 2033b59a7ca1SGustav Wiklander 20341714582aSDavid Jander if (mesg->prepared && ctlr->unprepare_message) { 20358caab75fSGeert Uytterhoeven ret = ctlr->unprepare_message(ctlr, mesg); 20362841a5fcSMark Brown if (ret) { 20378caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 20388caab75fSGeert Uytterhoeven ret); 20392841a5fcSMark Brown } 20402841a5fcSMark Brown } 2041391949b6SUwe Kleine-König 20421714582aSDavid Jander mesg->prepared = false; 20431714582aSDavid Jander 2044dc302905SDavid Jander WRITE_ONCE(ctlr->cur_msg_incomplete, false); 2045dc302905SDavid Jander smp_mb(); /* See __spi_pump_transfer_message()... */ 2046dc302905SDavid Jander if (READ_ONCE(ctlr->cur_msg_need_completion)) 204769fa9590SDavid Jander complete(&ctlr->cur_msg_completion); 20488e76ef88SMartin Sperl 20498e76ef88SMartin Sperl trace_spi_message_done(mesg); 20502841a5fcSMark Brown 2051ffbbdd21SLinus Walleij mesg->state = NULL; 2052ffbbdd21SLinus Walleij if (mesg->complete) 2053ffbbdd21SLinus Walleij mesg->complete(mesg->context); 2054ffbbdd21SLinus Walleij } 2055ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 2056ffbbdd21SLinus Walleij 20578caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr) 2058ffbbdd21SLinus Walleij { 2059ffbbdd21SLinus Walleij unsigned long flags; 2060ffbbdd21SLinus Walleij 20618caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 2062ffbbdd21SLinus Walleij 20638caab75fSGeert Uytterhoeven if (ctlr->running || ctlr->busy) { 20648caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2065ffbbdd21SLinus Walleij return -EBUSY; 2066ffbbdd21SLinus Walleij } 2067ffbbdd21SLinus Walleij 20688caab75fSGeert Uytterhoeven ctlr->running = true; 20698caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 20708caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2071ffbbdd21SLinus Walleij 207260a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 2073ffbbdd21SLinus Walleij 2074ffbbdd21SLinus Walleij return 0; 2075ffbbdd21SLinus Walleij } 2076ffbbdd21SLinus Walleij 20778caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr) 2078ffbbdd21SLinus Walleij { 2079ffbbdd21SLinus Walleij unsigned long flags; 2080ffbbdd21SLinus Walleij unsigned limit = 500; 2081ffbbdd21SLinus Walleij int ret = 0; 2082ffbbdd21SLinus Walleij 20838caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 2084ffbbdd21SLinus Walleij 2085ffbbdd21SLinus Walleij /* 2086ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 20878caab75fSGeert Uytterhoeven * A wait_queue on the ctlr->busy could be used, but then the common 2088ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 2089ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 2090ffbbdd21SLinus Walleij */ 20918caab75fSGeert Uytterhoeven while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 20928caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2093f97b26b0SAxel Lin usleep_range(10000, 11000); 20948caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 2095ffbbdd21SLinus Walleij } 2096ffbbdd21SLinus Walleij 20978caab75fSGeert Uytterhoeven if (!list_empty(&ctlr->queue) || ctlr->busy) 2098ffbbdd21SLinus Walleij ret = -EBUSY; 2099ffbbdd21SLinus Walleij else 21008caab75fSGeert Uytterhoeven ctlr->running = false; 2101ffbbdd21SLinus Walleij 21028caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2103ffbbdd21SLinus Walleij 2104ffbbdd21SLinus Walleij if (ret) { 21058caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "could not stop message queue\n"); 2106ffbbdd21SLinus Walleij return ret; 2107ffbbdd21SLinus Walleij } 2108ffbbdd21SLinus Walleij return ret; 2109ffbbdd21SLinus Walleij } 2110ffbbdd21SLinus Walleij 21118caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr) 2112ffbbdd21SLinus Walleij { 2113ffbbdd21SLinus Walleij int ret; 2114ffbbdd21SLinus Walleij 21158caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 2116ffbbdd21SLinus Walleij 2117ffbbdd21SLinus Walleij /* 21183989144fSPetr Mladek * kthread_flush_worker will block until all work is done. 2119ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 2120ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 2121ffbbdd21SLinus Walleij * return anyway. 2122ffbbdd21SLinus Walleij */ 2123ffbbdd21SLinus Walleij if (ret) { 21248caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem destroying queue\n"); 2125ffbbdd21SLinus Walleij return ret; 2126ffbbdd21SLinus Walleij } 2127ffbbdd21SLinus Walleij 212860a883d1SMarek Szyprowski kthread_destroy_worker(ctlr->kworker); 2129ffbbdd21SLinus Walleij 2130ffbbdd21SLinus Walleij return 0; 2131ffbbdd21SLinus Walleij } 2132ffbbdd21SLinus Walleij 21330461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi, 21340461a414SMark Brown struct spi_message *msg, 21350461a414SMark Brown bool need_pump) 2136ffbbdd21SLinus Walleij { 21378caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 2138ffbbdd21SLinus Walleij unsigned long flags; 2139ffbbdd21SLinus Walleij 21408caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 2141ffbbdd21SLinus Walleij 21428caab75fSGeert Uytterhoeven if (!ctlr->running) { 21438caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2144ffbbdd21SLinus Walleij return -ESHUTDOWN; 2145ffbbdd21SLinus Walleij } 2146ffbbdd21SLinus Walleij msg->actual_length = 0; 2147ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 2148ffbbdd21SLinus Walleij 21498caab75fSGeert Uytterhoeven list_add_tail(&msg->queue, &ctlr->queue); 2150ae7d2346SDavid Jander ctlr->queue_empty = false; 2151f0125f1aSMark Brown if (!ctlr->busy && need_pump) 215260a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 2153ffbbdd21SLinus Walleij 21548caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2155ffbbdd21SLinus Walleij return 0; 2156ffbbdd21SLinus Walleij } 2157ffbbdd21SLinus Walleij 21580461a414SMark Brown /** 21590461a414SMark Brown * spi_queued_transfer - transfer function for queued transfers 2160702ca026SAndy Shevchenko * @spi: SPI device which is requesting transfer 2161702ca026SAndy Shevchenko * @msg: SPI message which is to handled is queued to driver queue 216297d56dc6SJavier Martinez Canillas * 216397d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 21640461a414SMark Brown */ 21650461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 21660461a414SMark Brown { 21670461a414SMark Brown return __spi_queued_transfer(spi, msg, true); 21680461a414SMark Brown } 21690461a414SMark Brown 21708caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr) 2171ffbbdd21SLinus Walleij { 2172ffbbdd21SLinus Walleij int ret; 2173ffbbdd21SLinus Walleij 21748caab75fSGeert Uytterhoeven ctlr->transfer = spi_queued_transfer; 21758caab75fSGeert Uytterhoeven if (!ctlr->transfer_one_message) 21768caab75fSGeert Uytterhoeven ctlr->transfer_one_message = spi_transfer_one_message; 2177ffbbdd21SLinus Walleij 2178ffbbdd21SLinus Walleij /* Initialize and start queue */ 21798caab75fSGeert Uytterhoeven ret = spi_init_queue(ctlr); 2180ffbbdd21SLinus Walleij if (ret) { 21818caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem initializing queue\n"); 2182ffbbdd21SLinus Walleij goto err_init_queue; 2183ffbbdd21SLinus Walleij } 21848caab75fSGeert Uytterhoeven ctlr->queued = true; 21858caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 2186ffbbdd21SLinus Walleij if (ret) { 21878caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem starting queue\n"); 2188ffbbdd21SLinus Walleij goto err_start_queue; 2189ffbbdd21SLinus Walleij } 2190ffbbdd21SLinus Walleij 2191ffbbdd21SLinus Walleij return 0; 2192ffbbdd21SLinus Walleij 2193ffbbdd21SLinus Walleij err_start_queue: 21948caab75fSGeert Uytterhoeven spi_destroy_queue(ctlr); 2195c3676d5cSMark Brown err_init_queue: 2196ffbbdd21SLinus Walleij return ret; 2197ffbbdd21SLinus Walleij } 2198ffbbdd21SLinus Walleij 2199988f259bSBoris Brezillon /** 2200988f259bSBoris Brezillon * spi_flush_queue - Send all pending messages in the queue from the callers' 2201988f259bSBoris Brezillon * context 2202988f259bSBoris Brezillon * @ctlr: controller to process queue for 2203988f259bSBoris Brezillon * 2204988f259bSBoris Brezillon * This should be used when one wants to ensure all pending messages have been 2205988f259bSBoris Brezillon * sent before doing something. Is used by the spi-mem code to make sure SPI 2206988f259bSBoris Brezillon * memory operations do not preempt regular SPI transfers that have been queued 2207988f259bSBoris Brezillon * before the spi-mem operation. 2208988f259bSBoris Brezillon */ 2209988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr) 2210988f259bSBoris Brezillon { 2211988f259bSBoris Brezillon if (ctlr->transfer == spi_queued_transfer) 2212988f259bSBoris Brezillon __spi_pump_messages(ctlr, false); 2213988f259bSBoris Brezillon } 2214988f259bSBoris Brezillon 2215ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 2216ffbbdd21SLinus Walleij 22177cb94361SAndreas Larsson #if defined(CONFIG_OF) 2218f276aacfSJanne Grunau static void of_spi_parse_dt_cs_delay(struct device_node *nc, 2219f276aacfSJanne Grunau struct spi_delay *delay, const char *prop) 2220f276aacfSJanne Grunau { 2221f276aacfSJanne Grunau u32 value; 2222f276aacfSJanne Grunau 2223f276aacfSJanne Grunau if (!of_property_read_u32(nc, prop, &value)) { 2224f276aacfSJanne Grunau if (value > U16_MAX) { 2225f276aacfSJanne Grunau delay->value = DIV_ROUND_UP(value, 1000); 2226f276aacfSJanne Grunau delay->unit = SPI_DELAY_UNIT_USECS; 2227f276aacfSJanne Grunau } else { 2228f276aacfSJanne Grunau delay->value = value; 2229f276aacfSJanne Grunau delay->unit = SPI_DELAY_UNIT_NSECS; 2230f276aacfSJanne Grunau } 2231f276aacfSJanne Grunau } 2232f276aacfSJanne Grunau } 2233f276aacfSJanne Grunau 22348caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 2235c2e51ac3SGeert Uytterhoeven struct device_node *nc) 2236d57a4282SGrant Likely { 223789da4293STrent Piepho u32 value; 2238c2e51ac3SGeert Uytterhoeven int rc; 2239d57a4282SGrant Likely 2240d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 2241e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpha")) 2242d57a4282SGrant Likely spi->mode |= SPI_CPHA; 2243e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpol")) 2244d57a4282SGrant Likely spi->mode |= SPI_CPOL; 2245e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-3wire")) 2246c20151dfSLars-Peter Clausen spi->mode |= SPI_3WIRE; 2247e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-lsb-first")) 2248cd6339e6SZhao Qiang spi->mode |= SPI_LSB_FIRST; 22493e5ec1dbSGregory CLEMENT if (of_property_read_bool(nc, "spi-cs-high")) 2250f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 2251f3186dd8SLinus Walleij 2252f477b7fbSwangyuhang /* Device DUAL/QUAD mode */ 225389da4293STrent Piepho if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 225489da4293STrent Piepho switch (value) { 2255d962608cSDragos Bogdan case 0: 2256d962608cSDragos Bogdan spi->mode |= SPI_NO_TX; 2257d962608cSDragos Bogdan break; 225889da4293STrent Piepho case 1: 2259f477b7fbSwangyuhang break; 226089da4293STrent Piepho case 2: 2261f477b7fbSwangyuhang spi->mode |= SPI_TX_DUAL; 2262f477b7fbSwangyuhang break; 226389da4293STrent Piepho case 4: 2264f477b7fbSwangyuhang spi->mode |= SPI_TX_QUAD; 2265f477b7fbSwangyuhang break; 22666b03061fSYogesh Narayan Gaur case 8: 22676b03061fSYogesh Narayan Gaur spi->mode |= SPI_TX_OCTAL; 22686b03061fSYogesh Narayan Gaur break; 2269f477b7fbSwangyuhang default: 22708caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 2271a110f93dSwangyuhang "spi-tx-bus-width %d not supported\n", 227289da4293STrent Piepho value); 227380874d8cSGeert Uytterhoeven break; 2274f477b7fbSwangyuhang } 2275a822e99cSMark Brown } 2276f477b7fbSwangyuhang 227789da4293STrent Piepho if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 227889da4293STrent Piepho switch (value) { 2279d962608cSDragos Bogdan case 0: 2280d962608cSDragos Bogdan spi->mode |= SPI_NO_RX; 2281d962608cSDragos Bogdan break; 228289da4293STrent Piepho case 1: 2283f477b7fbSwangyuhang break; 228489da4293STrent Piepho case 2: 2285f477b7fbSwangyuhang spi->mode |= SPI_RX_DUAL; 2286f477b7fbSwangyuhang break; 228789da4293STrent Piepho case 4: 2288f477b7fbSwangyuhang spi->mode |= SPI_RX_QUAD; 2289f477b7fbSwangyuhang break; 22906b03061fSYogesh Narayan Gaur case 8: 22916b03061fSYogesh Narayan Gaur spi->mode |= SPI_RX_OCTAL; 22926b03061fSYogesh Narayan Gaur break; 2293f477b7fbSwangyuhang default: 22948caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 2295a110f93dSwangyuhang "spi-rx-bus-width %d not supported\n", 229689da4293STrent Piepho value); 229780874d8cSGeert Uytterhoeven break; 2298f477b7fbSwangyuhang } 2299a822e99cSMark Brown } 2300f477b7fbSwangyuhang 23018caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr)) { 2302194276b0SRob Herring if (!of_node_name_eq(nc, "slave")) { 230325c56c88SRob Herring dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 230425c56c88SRob Herring nc); 23056c364062SGeert Uytterhoeven return -EINVAL; 23066c364062SGeert Uytterhoeven } 23076c364062SGeert Uytterhoeven return 0; 23086c364062SGeert Uytterhoeven } 23096c364062SGeert Uytterhoeven 23106c364062SGeert Uytterhoeven /* Device address */ 23116c364062SGeert Uytterhoeven rc = of_property_read_u32(nc, "reg", &value); 23126c364062SGeert Uytterhoeven if (rc) { 231325c56c88SRob Herring dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 231425c56c88SRob Herring nc, rc); 23156c364062SGeert Uytterhoeven return rc; 23166c364062SGeert Uytterhoeven } 2317303feb3cSAmit Kumar Mahapatra spi_set_chipselect(spi, 0, value); 23186c364062SGeert Uytterhoeven 2319d57a4282SGrant Likely /* Device speed */ 2320671c3bf5SChuanhong Guo if (!of_property_read_u32(nc, "spi-max-frequency", &value)) 232189da4293STrent Piepho spi->max_speed_hz = value; 2322d57a4282SGrant Likely 2323f276aacfSJanne Grunau /* Device CS delays */ 2324f276aacfSJanne Grunau of_spi_parse_dt_cs_delay(nc, &spi->cs_setup, "spi-cs-setup-delay-ns"); 23255827b31dSJanne Grunau of_spi_parse_dt_cs_delay(nc, &spi->cs_hold, "spi-cs-hold-delay-ns"); 23265827b31dSJanne Grunau of_spi_parse_dt_cs_delay(nc, &spi->cs_inactive, "spi-cs-inactive-delay-ns"); 232733a2fde5STudor Ambarus 2328c2e51ac3SGeert Uytterhoeven return 0; 2329c2e51ac3SGeert Uytterhoeven } 2330c2e51ac3SGeert Uytterhoeven 2331c2e51ac3SGeert Uytterhoeven static struct spi_device * 23328caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 2333c2e51ac3SGeert Uytterhoeven { 2334c2e51ac3SGeert Uytterhoeven struct spi_device *spi; 2335c2e51ac3SGeert Uytterhoeven int rc; 2336c2e51ac3SGeert Uytterhoeven 2337c2e51ac3SGeert Uytterhoeven /* Alloc an spi_device */ 23388caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 2339c2e51ac3SGeert Uytterhoeven if (!spi) { 234025c56c88SRob Herring dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 2341c2e51ac3SGeert Uytterhoeven rc = -ENOMEM; 2342c2e51ac3SGeert Uytterhoeven goto err_out; 2343c2e51ac3SGeert Uytterhoeven } 2344c2e51ac3SGeert Uytterhoeven 2345c2e51ac3SGeert Uytterhoeven /* Select device driver */ 2346673aa1edSMiquel Raynal rc = of_alias_from_compatible(nc, spi->modalias, 2347c2e51ac3SGeert Uytterhoeven sizeof(spi->modalias)); 2348c2e51ac3SGeert Uytterhoeven if (rc < 0) { 234925c56c88SRob Herring dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 2350c2e51ac3SGeert Uytterhoeven goto err_out; 2351c2e51ac3SGeert Uytterhoeven } 2352c2e51ac3SGeert Uytterhoeven 23538caab75fSGeert Uytterhoeven rc = of_spi_parse_dt(ctlr, spi, nc); 2354c2e51ac3SGeert Uytterhoeven if (rc) 2355c2e51ac3SGeert Uytterhoeven goto err_out; 2356c2e51ac3SGeert Uytterhoeven 2357d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 2358d57a4282SGrant Likely of_node_get(nc); 2359c7cc588bSAndy Shevchenko 2360c7cc588bSAndy Shevchenko device_set_node(&spi->dev, of_fwnode_handle(nc)); 2361d57a4282SGrant Likely 2362d57a4282SGrant Likely /* Register the new device */ 2363d57a4282SGrant Likely rc = spi_add_device(spi); 2364d57a4282SGrant Likely if (rc) { 236525c56c88SRob Herring dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 23668324147fSJohan Hovold goto err_of_node_put; 2367d57a4282SGrant Likely } 2368d57a4282SGrant Likely 2369aff5e3f8SPantelis Antoniou return spi; 2370aff5e3f8SPantelis Antoniou 23718324147fSJohan Hovold err_of_node_put: 23728324147fSJohan Hovold of_node_put(nc); 2373aff5e3f8SPantelis Antoniou err_out: 2374aff5e3f8SPantelis Antoniou spi_dev_put(spi); 2375aff5e3f8SPantelis Antoniou return ERR_PTR(rc); 2376aff5e3f8SPantelis Antoniou } 2377aff5e3f8SPantelis Antoniou 2378aff5e3f8SPantelis Antoniou /** 2379aff5e3f8SPantelis Antoniou * of_register_spi_devices() - Register child devices onto the SPI bus 23808caab75fSGeert Uytterhoeven * @ctlr: Pointer to spi_controller device 2381aff5e3f8SPantelis Antoniou * 23826c364062SGeert Uytterhoeven * Registers an spi_device for each child node of controller node which 23836c364062SGeert Uytterhoeven * represents a valid SPI slave. 2384aff5e3f8SPantelis Antoniou */ 23858caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) 2386aff5e3f8SPantelis Antoniou { 2387aff5e3f8SPantelis Antoniou struct spi_device *spi; 2388aff5e3f8SPantelis Antoniou struct device_node *nc; 2389aff5e3f8SPantelis Antoniou 23908caab75fSGeert Uytterhoeven for_each_available_child_of_node(ctlr->dev.of_node, nc) { 2391bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(nc, OF_POPULATED)) 2392bd6c1644SGeert Uytterhoeven continue; 23938caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, nc); 2394e0af98a7SRalf Ramsauer if (IS_ERR(spi)) { 23958caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 239625c56c88SRob Herring "Failed to create SPI device for %pOF\n", nc); 2397e0af98a7SRalf Ramsauer of_node_clear_flag(nc, OF_POPULATED); 2398e0af98a7SRalf Ramsauer } 2399d57a4282SGrant Likely } 2400d57a4282SGrant Likely } 2401d57a4282SGrant Likely #else 24028caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { } 2403d57a4282SGrant Likely #endif 2404d57a4282SGrant Likely 24050c79378cSSebastian Reichel /** 24060c79378cSSebastian Reichel * spi_new_ancillary_device() - Register ancillary SPI device 24070c79378cSSebastian Reichel * @spi: Pointer to the main SPI device registering the ancillary device 24080c79378cSSebastian Reichel * @chip_select: Chip Select of the ancillary device 24090c79378cSSebastian Reichel * 24100c79378cSSebastian Reichel * Register an ancillary SPI device; for example some chips have a chip-select 24110c79378cSSebastian Reichel * for normal device usage and another one for setup/firmware upload. 24120c79378cSSebastian Reichel * 24130c79378cSSebastian Reichel * This may only be called from main SPI device's probe routine. 24140c79378cSSebastian Reichel * 24150c79378cSSebastian Reichel * Return: 0 on success; negative errno on failure 24160c79378cSSebastian Reichel */ 24170c79378cSSebastian Reichel struct spi_device *spi_new_ancillary_device(struct spi_device *spi, 24180c79378cSSebastian Reichel u8 chip_select) 24190c79378cSSebastian Reichel { 24200c79378cSSebastian Reichel struct spi_device *ancillary; 24210c79378cSSebastian Reichel int rc = 0; 24220c79378cSSebastian Reichel 24230c79378cSSebastian Reichel /* Alloc an spi_device */ 24240c79378cSSebastian Reichel ancillary = spi_alloc_device(spi->controller); 24250c79378cSSebastian Reichel if (!ancillary) { 24260c79378cSSebastian Reichel rc = -ENOMEM; 24270c79378cSSebastian Reichel goto err_out; 24280c79378cSSebastian Reichel } 24290c79378cSSebastian Reichel 243051e99de5SWolfram Sang strscpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias)); 24310c79378cSSebastian Reichel 24320c79378cSSebastian Reichel /* Use provided chip-select for ancillary device */ 2433303feb3cSAmit Kumar Mahapatra spi_set_chipselect(ancillary, 0, chip_select); 24340c79378cSSebastian Reichel 24350c79378cSSebastian Reichel /* Take over SPI mode/speed from SPI main device */ 24360c79378cSSebastian Reichel ancillary->max_speed_hz = spi->max_speed_hz; 2437b01d5506SColin Ian King ancillary->mode = spi->mode; 24380c79378cSSebastian Reichel 24390c79378cSSebastian Reichel /* Register the new device */ 24400c79378cSSebastian Reichel rc = spi_add_device_locked(ancillary); 24410c79378cSSebastian Reichel if (rc) { 24420c79378cSSebastian Reichel dev_err(&spi->dev, "failed to register ancillary device\n"); 24430c79378cSSebastian Reichel goto err_out; 24440c79378cSSebastian Reichel } 24450c79378cSSebastian Reichel 24460c79378cSSebastian Reichel return ancillary; 24470c79378cSSebastian Reichel 24480c79378cSSebastian Reichel err_out: 24490c79378cSSebastian Reichel spi_dev_put(ancillary); 24500c79378cSSebastian Reichel return ERR_PTR(rc); 24510c79378cSSebastian Reichel } 24520c79378cSSebastian Reichel EXPORT_SYMBOL_GPL(spi_new_ancillary_device); 24530c79378cSSebastian Reichel 245464bee4d2SMika Westerberg #ifdef CONFIG_ACPI 24554c3c5954SArd Biesheuvel struct acpi_spi_lookup { 24564c3c5954SArd Biesheuvel struct spi_controller *ctlr; 24574c3c5954SArd Biesheuvel u32 max_speed_hz; 24584c3c5954SArd Biesheuvel u32 mode; 24594c3c5954SArd Biesheuvel int irq; 24604c3c5954SArd Biesheuvel u8 bits_per_word; 24614c3c5954SArd Biesheuvel u8 chip_select; 246287e59b36SStefan Binding int n; 246387e59b36SStefan Binding int index; 24644c3c5954SArd Biesheuvel }; 24654c3c5954SArd Biesheuvel 2466e612af7aSStefan Binding static int acpi_spi_count(struct acpi_resource *ares, void *data) 2467e612af7aSStefan Binding { 2468e612af7aSStefan Binding struct acpi_resource_spi_serialbus *sb; 2469e612af7aSStefan Binding int *count = data; 2470e612af7aSStefan Binding 2471e612af7aSStefan Binding if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) 2472e612af7aSStefan Binding return 1; 2473e612af7aSStefan Binding 2474e612af7aSStefan Binding sb = &ares->data.spi_serial_bus; 2475e612af7aSStefan Binding if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_SPI) 2476e612af7aSStefan Binding return 1; 2477e612af7aSStefan Binding 2478e612af7aSStefan Binding *count = *count + 1; 2479e612af7aSStefan Binding 2480e612af7aSStefan Binding return 1; 2481e612af7aSStefan Binding } 2482e612af7aSStefan Binding 2483e612af7aSStefan Binding /** 2484e612af7aSStefan Binding * acpi_spi_count_resources - Count the number of SpiSerialBus resources 2485e612af7aSStefan Binding * @adev: ACPI device 2486e612af7aSStefan Binding * 2487702ca026SAndy Shevchenko * Return: the number of SpiSerialBus resources in the ACPI-device's 2488e612af7aSStefan Binding * resource-list; or a negative error code. 2489e612af7aSStefan Binding */ 2490e612af7aSStefan Binding int acpi_spi_count_resources(struct acpi_device *adev) 2491e612af7aSStefan Binding { 2492e612af7aSStefan Binding LIST_HEAD(r); 2493e612af7aSStefan Binding int count = 0; 2494e612af7aSStefan Binding int ret; 2495e612af7aSStefan Binding 2496e612af7aSStefan Binding ret = acpi_dev_get_resources(adev, &r, acpi_spi_count, &count); 2497e612af7aSStefan Binding if (ret < 0) 2498e612af7aSStefan Binding return ret; 2499e612af7aSStefan Binding 2500e612af7aSStefan Binding acpi_dev_free_resource_list(&r); 2501e612af7aSStefan Binding 2502e612af7aSStefan Binding return count; 2503e612af7aSStefan Binding } 2504e612af7aSStefan Binding EXPORT_SYMBOL_GPL(acpi_spi_count_resources); 2505e612af7aSStefan Binding 25064c3c5954SArd Biesheuvel static void acpi_spi_parse_apple_properties(struct acpi_device *dev, 25074c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup) 25088a2e487eSLukas Wunner { 25098a2e487eSLukas Wunner const union acpi_object *obj; 25108a2e487eSLukas Wunner 25118a2e487eSLukas Wunner if (!x86_apple_machine) 25128a2e487eSLukas Wunner return; 25138a2e487eSLukas Wunner 25148a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 25158a2e487eSLukas Wunner && obj->buffer.length >= 4) 25164c3c5954SArd Biesheuvel lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 25178a2e487eSLukas Wunner 25188a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 25198a2e487eSLukas Wunner && obj->buffer.length == 8) 25204c3c5954SArd Biesheuvel lookup->bits_per_word = *(u64 *)obj->buffer.pointer; 25218a2e487eSLukas Wunner 25228a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 25238a2e487eSLukas Wunner && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 25244c3c5954SArd Biesheuvel lookup->mode |= SPI_LSB_FIRST; 25258a2e487eSLukas Wunner 25268a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 25278a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 25284c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 25298a2e487eSLukas Wunner 25308a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 25318a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 25324c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 25338a2e487eSLukas Wunner } 25348a2e487eSLukas Wunner 253587e59b36SStefan Binding static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev); 253687e59b36SStefan Binding 253764bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 253864bee4d2SMika Westerberg { 25394c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup = data; 25404c3c5954SArd Biesheuvel struct spi_controller *ctlr = lookup->ctlr; 254164bee4d2SMika Westerberg 254264bee4d2SMika Westerberg if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 254364bee4d2SMika Westerberg struct acpi_resource_spi_serialbus *sb; 25444c3c5954SArd Biesheuvel acpi_handle parent_handle; 25454c3c5954SArd Biesheuvel acpi_status status; 254664bee4d2SMika Westerberg 254764bee4d2SMika Westerberg sb = &ares->data.spi_serial_bus; 254864bee4d2SMika Westerberg if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 25494c3c5954SArd Biesheuvel 255087e59b36SStefan Binding if (lookup->index != -1 && lookup->n++ != lookup->index) 255187e59b36SStefan Binding return 1; 255287e59b36SStefan Binding 25534c3c5954SArd Biesheuvel status = acpi_get_handle(NULL, 25544c3c5954SArd Biesheuvel sb->resource_source.string_ptr, 25554c3c5954SArd Biesheuvel &parent_handle); 25564c3c5954SArd Biesheuvel 255787e59b36SStefan Binding if (ACPI_FAILURE(status)) 25584c3c5954SArd Biesheuvel return -ENODEV; 25594c3c5954SArd Biesheuvel 256087e59b36SStefan Binding if (ctlr) { 256187e59b36SStefan Binding if (ACPI_HANDLE(ctlr->dev.parent) != parent_handle) 256287e59b36SStefan Binding return -ENODEV; 256387e59b36SStefan Binding } else { 256487e59b36SStefan Binding struct acpi_device *adev; 256587e59b36SStefan Binding 2566ac2a3feeSRafael J. Wysocki adev = acpi_fetch_acpi_dev(parent_handle); 2567ac2a3feeSRafael J. Wysocki if (!adev) 256887e59b36SStefan Binding return -ENODEV; 256987e59b36SStefan Binding 257087e59b36SStefan Binding ctlr = acpi_spi_find_controller_by_adev(adev); 257187e59b36SStefan Binding if (!ctlr) 25729c22ec4aSAndy Shevchenko return -EPROBE_DEFER; 257387e59b36SStefan Binding 257487e59b36SStefan Binding lookup->ctlr = ctlr; 257587e59b36SStefan Binding } 257687e59b36SStefan Binding 2577a0a90718SMika Westerberg /* 2578a0a90718SMika Westerberg * ACPI DeviceSelection numbering is handled by the 2579a0a90718SMika Westerberg * host controller driver in Windows and can vary 2580a0a90718SMika Westerberg * from driver to driver. In Linux we always expect 2581a0a90718SMika Westerberg * 0 .. max - 1 so we need to ask the driver to 2582a0a90718SMika Westerberg * translate between the two schemes. 2583a0a90718SMika Westerberg */ 25848caab75fSGeert Uytterhoeven if (ctlr->fw_translate_cs) { 25858caab75fSGeert Uytterhoeven int cs = ctlr->fw_translate_cs(ctlr, 2586a0a90718SMika Westerberg sb->device_selection); 2587a0a90718SMika Westerberg if (cs < 0) 2588a0a90718SMika Westerberg return cs; 25894c3c5954SArd Biesheuvel lookup->chip_select = cs; 2590a0a90718SMika Westerberg } else { 25914c3c5954SArd Biesheuvel lookup->chip_select = sb->device_selection; 2592a0a90718SMika Westerberg } 2593a0a90718SMika Westerberg 25944c3c5954SArd Biesheuvel lookup->max_speed_hz = sb->connection_speed; 25950dadde34SAndy Shevchenko lookup->bits_per_word = sb->data_bit_length; 259664bee4d2SMika Westerberg 259764bee4d2SMika Westerberg if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 25984c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 259964bee4d2SMika Westerberg if (sb->clock_polarity == ACPI_SPI_START_HIGH) 26004c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 260164bee4d2SMika Westerberg if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 26024c3c5954SArd Biesheuvel lookup->mode |= SPI_CS_HIGH; 260364bee4d2SMika Westerberg } 26044c3c5954SArd Biesheuvel } else if (lookup->irq < 0) { 260564bee4d2SMika Westerberg struct resource r; 260664bee4d2SMika Westerberg 260764bee4d2SMika Westerberg if (acpi_dev_resource_interrupt(ares, 0, &r)) 26084c3c5954SArd Biesheuvel lookup->irq = r.start; 260964bee4d2SMika Westerberg } 261064bee4d2SMika Westerberg 261164bee4d2SMika Westerberg /* Always tell the ACPI core to skip this resource */ 261264bee4d2SMika Westerberg return 1; 261364bee4d2SMika Westerberg } 261464bee4d2SMika Westerberg 2615000bee0eSStefan Binding /** 2616000bee0eSStefan Binding * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information 2617000bee0eSStefan Binding * @ctlr: controller to which the spi device belongs 2618000bee0eSStefan Binding * @adev: ACPI Device for the spi device 261987e59b36SStefan Binding * @index: Index of the spi resource inside the ACPI Node 2620000bee0eSStefan Binding * 2621702ca026SAndy Shevchenko * This should be used to allocate a new SPI device from and ACPI Device node. 2622702ca026SAndy Shevchenko * The caller is responsible for calling spi_add_device to register the SPI device. 2623000bee0eSStefan Binding * 2624702ca026SAndy Shevchenko * If ctlr is set to NULL, the Controller for the SPI device will be looked up 262587e59b36SStefan Binding * using the resource. 262687e59b36SStefan Binding * If index is set to -1, index is not used. 262787e59b36SStefan Binding * Note: If index is -1, ctlr must be set. 262887e59b36SStefan Binding * 2629000bee0eSStefan Binding * Return: a pointer to the new device, or ERR_PTR on error. 2630000bee0eSStefan Binding */ 2631000bee0eSStefan Binding struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr, 263287e59b36SStefan Binding struct acpi_device *adev, 263387e59b36SStefan Binding int index) 263464bee4d2SMika Westerberg { 26354c3c5954SArd Biesheuvel acpi_handle parent_handle = NULL; 263664bee4d2SMika Westerberg struct list_head resource_list; 2637b28944c6SArd Biesheuvel struct acpi_spi_lookup lookup = {}; 263864bee4d2SMika Westerberg struct spi_device *spi; 263964bee4d2SMika Westerberg int ret; 264064bee4d2SMika Westerberg 264187e59b36SStefan Binding if (!ctlr && index == -1) 264287e59b36SStefan Binding return ERR_PTR(-EINVAL); 264387e59b36SStefan Binding 26444c3c5954SArd Biesheuvel lookup.ctlr = ctlr; 26454c3c5954SArd Biesheuvel lookup.irq = -1; 264687e59b36SStefan Binding lookup.index = index; 264787e59b36SStefan Binding lookup.n = 0; 26484c3c5954SArd Biesheuvel 26494c3c5954SArd Biesheuvel INIT_LIST_HEAD(&resource_list); 26504c3c5954SArd Biesheuvel ret = acpi_dev_get_resources(adev, &resource_list, 26514c3c5954SArd Biesheuvel acpi_spi_add_resource, &lookup); 26524c3c5954SArd Biesheuvel acpi_dev_free_resource_list(&resource_list); 26534c3c5954SArd Biesheuvel 26544c3c5954SArd Biesheuvel if (ret < 0) 265595c8222fSDavid Jander /* Found SPI in _CRS but it points to another controller */ 2656b6747f4fSAndy Shevchenko return ERR_PTR(ret); 26574c3c5954SArd Biesheuvel 26584c3c5954SArd Biesheuvel if (!lookup.max_speed_hz && 265910e92724SBjorn Helgaas ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) && 266087e59b36SStefan Binding ACPI_HANDLE(lookup.ctlr->dev.parent) == parent_handle) { 26614c3c5954SArd Biesheuvel /* Apple does not use _CRS but nested devices for SPI slaves */ 26624c3c5954SArd Biesheuvel acpi_spi_parse_apple_properties(adev, &lookup); 26634c3c5954SArd Biesheuvel } 26644c3c5954SArd Biesheuvel 26654c3c5954SArd Biesheuvel if (!lookup.max_speed_hz) 2666000bee0eSStefan Binding return ERR_PTR(-ENODEV); 26674c3c5954SArd Biesheuvel 266887e59b36SStefan Binding spi = spi_alloc_device(lookup.ctlr); 266964bee4d2SMika Westerberg if (!spi) { 267087e59b36SStefan Binding dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n", 267164bee4d2SMika Westerberg dev_name(&adev->dev)); 2672000bee0eSStefan Binding return ERR_PTR(-ENOMEM); 267364bee4d2SMika Westerberg } 267464bee4d2SMika Westerberg 26757b199811SRafael J. Wysocki ACPI_COMPANION_SET(&spi->dev, adev); 26764c3c5954SArd Biesheuvel spi->max_speed_hz = lookup.max_speed_hz; 2677ea235786SJohn Garry spi->mode |= lookup.mode; 26784c3c5954SArd Biesheuvel spi->irq = lookup.irq; 26794c3c5954SArd Biesheuvel spi->bits_per_word = lookup.bits_per_word; 2680303feb3cSAmit Kumar Mahapatra spi_set_chipselect(spi, 0, lookup.chip_select); 268164bee4d2SMika Westerberg 2682000bee0eSStefan Binding return spi; 2683000bee0eSStefan Binding } 2684000bee0eSStefan Binding EXPORT_SYMBOL_GPL(acpi_spi_device_alloc); 2685000bee0eSStefan Binding 2686000bee0eSStefan Binding static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 2687000bee0eSStefan Binding struct acpi_device *adev) 2688000bee0eSStefan Binding { 2689000bee0eSStefan Binding struct spi_device *spi; 2690000bee0eSStefan Binding 2691000bee0eSStefan Binding if (acpi_bus_get_status(adev) || !adev->status.present || 2692000bee0eSStefan Binding acpi_device_enumerated(adev)) 2693000bee0eSStefan Binding return AE_OK; 2694000bee0eSStefan Binding 269587e59b36SStefan Binding spi = acpi_spi_device_alloc(ctlr, adev, -1); 2696000bee0eSStefan Binding if (IS_ERR(spi)) { 2697000bee0eSStefan Binding if (PTR_ERR(spi) == -ENOMEM) 2698000bee0eSStefan Binding return AE_NO_MEMORY; 2699000bee0eSStefan Binding else 2700000bee0eSStefan Binding return AE_OK; 2701000bee0eSStefan Binding } 2702000bee0eSStefan Binding 27030c6543f6SDan O'Donovan acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 27040c6543f6SDan O'Donovan sizeof(spi->modalias)); 27050c6543f6SDan O'Donovan 270633ada67dSChristophe RICARD if (spi->irq < 0) 270733ada67dSChristophe RICARD spi->irq = acpi_dev_gpio_irq_get(adev, 0); 270833ada67dSChristophe RICARD 27097f24467fSOctavian Purdila acpi_device_set_enumerated(adev); 27107f24467fSOctavian Purdila 271133cf00e5SMika Westerberg adev->power.flags.ignore_parent = true; 271264bee4d2SMika Westerberg if (spi_add_device(spi)) { 271333cf00e5SMika Westerberg adev->power.flags.ignore_parent = false; 27148caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 271564bee4d2SMika Westerberg dev_name(&adev->dev)); 271664bee4d2SMika Westerberg spi_dev_put(spi); 271764bee4d2SMika Westerberg } 271864bee4d2SMika Westerberg 271964bee4d2SMika Westerberg return AE_OK; 272064bee4d2SMika Westerberg } 272164bee4d2SMika Westerberg 27227f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 27237f24467fSOctavian Purdila void *data, void **return_value) 27247f24467fSOctavian Purdila { 27257030c428SRafael J. Wysocki struct acpi_device *adev = acpi_fetch_acpi_dev(handle); 27268caab75fSGeert Uytterhoeven struct spi_controller *ctlr = data; 27277f24467fSOctavian Purdila 27287030c428SRafael J. Wysocki if (!adev) 27297f24467fSOctavian Purdila return AE_OK; 27307f24467fSOctavian Purdila 27318caab75fSGeert Uytterhoeven return acpi_register_spi_device(ctlr, adev); 27327f24467fSOctavian Purdila } 27337f24467fSOctavian Purdila 27344c3c5954SArd Biesheuvel #define SPI_ACPI_ENUMERATE_MAX_DEPTH 32 27354c3c5954SArd Biesheuvel 27368caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr) 273764bee4d2SMika Westerberg { 273864bee4d2SMika Westerberg acpi_status status; 273964bee4d2SMika Westerberg acpi_handle handle; 274064bee4d2SMika Westerberg 27418caab75fSGeert Uytterhoeven handle = ACPI_HANDLE(ctlr->dev.parent); 274264bee4d2SMika Westerberg if (!handle) 274364bee4d2SMika Westerberg return; 274464bee4d2SMika Westerberg 27454c3c5954SArd Biesheuvel status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 27464c3c5954SArd Biesheuvel SPI_ACPI_ENUMERATE_MAX_DEPTH, 27478caab75fSGeert Uytterhoeven acpi_spi_add_device, NULL, ctlr, NULL); 274864bee4d2SMika Westerberg if (ACPI_FAILURE(status)) 27498caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 275064bee4d2SMika Westerberg } 275164bee4d2SMika Westerberg #else 27528caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 275364bee4d2SMika Westerberg #endif /* CONFIG_ACPI */ 275464bee4d2SMika Westerberg 27558caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev) 27568ae12a0dSDavid Brownell { 27578caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 27588ae12a0dSDavid Brownell 27598caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 27608caab75fSGeert Uytterhoeven kfree(ctlr); 27618ae12a0dSDavid Brownell } 27628ae12a0dSDavid Brownell 27638ae12a0dSDavid Brownell static struct class spi_master_class = { 27648ae12a0dSDavid Brownell .name = "spi_master", 27658caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 2766eca2ebc7SMartin Sperl .dev_groups = spi_master_groups, 27678ae12a0dSDavid Brownell }; 27688ae12a0dSDavid Brownell 27696c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE 27706c364062SGeert Uytterhoeven /** 27716c364062SGeert Uytterhoeven * spi_slave_abort - abort the ongoing transfer request on an SPI slave 27726c364062SGeert Uytterhoeven * controller 27736c364062SGeert Uytterhoeven * @spi: device used for the current transfer 27746c364062SGeert Uytterhoeven */ 27756c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi) 27766c364062SGeert Uytterhoeven { 27778caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 27786c364062SGeert Uytterhoeven 27798caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 27808caab75fSGeert Uytterhoeven return ctlr->slave_abort(ctlr); 27816c364062SGeert Uytterhoeven 27826c364062SGeert Uytterhoeven return -ENOTSUPP; 27836c364062SGeert Uytterhoeven } 27846c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort); 27856c364062SGeert Uytterhoeven 2786b8d3b056SYang Yingliang int spi_target_abort(struct spi_device *spi) 2787b8d3b056SYang Yingliang { 2788b8d3b056SYang Yingliang struct spi_controller *ctlr = spi->controller; 2789b8d3b056SYang Yingliang 2790b8d3b056SYang Yingliang if (spi_controller_is_target(ctlr) && ctlr->target_abort) 2791b8d3b056SYang Yingliang return ctlr->target_abort(ctlr); 2792b8d3b056SYang Yingliang 2793b8d3b056SYang Yingliang return -ENOTSUPP; 2794b8d3b056SYang Yingliang } 2795b8d3b056SYang Yingliang EXPORT_SYMBOL_GPL(spi_target_abort); 2796b8d3b056SYang Yingliang 2797cc8b4659SGeert Uytterhoeven static ssize_t slave_show(struct device *dev, struct device_attribute *attr, 2798cc8b4659SGeert Uytterhoeven char *buf) 27996c364062SGeert Uytterhoeven { 28008caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 28018caab75fSGeert Uytterhoeven dev); 28026c364062SGeert Uytterhoeven struct device *child; 28036c364062SGeert Uytterhoeven 2804c21b0837SAndy Shevchenko child = device_find_any_child(&ctlr->dev); 2805f2daa466SAndy Shevchenko return sysfs_emit(buf, "%s\n", child ? to_spi_device(child)->modalias : NULL); 28066c364062SGeert Uytterhoeven } 28076c364062SGeert Uytterhoeven 2808cc8b4659SGeert Uytterhoeven static ssize_t slave_store(struct device *dev, struct device_attribute *attr, 2809cc8b4659SGeert Uytterhoeven const char *buf, size_t count) 28106c364062SGeert Uytterhoeven { 28118caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 28128caab75fSGeert Uytterhoeven dev); 28136c364062SGeert Uytterhoeven struct spi_device *spi; 28146c364062SGeert Uytterhoeven struct device *child; 28156c364062SGeert Uytterhoeven char name[32]; 28166c364062SGeert Uytterhoeven int rc; 28176c364062SGeert Uytterhoeven 28186c364062SGeert Uytterhoeven rc = sscanf(buf, "%31s", name); 28196c364062SGeert Uytterhoeven if (rc != 1 || !name[0]) 28206c364062SGeert Uytterhoeven return -EINVAL; 28216c364062SGeert Uytterhoeven 2822c21b0837SAndy Shevchenko child = device_find_any_child(&ctlr->dev); 28236c364062SGeert Uytterhoeven if (child) { 28246c364062SGeert Uytterhoeven /* Remove registered slave */ 28256c364062SGeert Uytterhoeven device_unregister(child); 28266c364062SGeert Uytterhoeven put_device(child); 28276c364062SGeert Uytterhoeven } 28286c364062SGeert Uytterhoeven 28296c364062SGeert Uytterhoeven if (strcmp(name, "(null)")) { 28306c364062SGeert Uytterhoeven /* Register new slave */ 28316c364062SGeert Uytterhoeven spi = spi_alloc_device(ctlr); 28326c364062SGeert Uytterhoeven if (!spi) 28336c364062SGeert Uytterhoeven return -ENOMEM; 28346c364062SGeert Uytterhoeven 283551e99de5SWolfram Sang strscpy(spi->modalias, name, sizeof(spi->modalias)); 28366c364062SGeert Uytterhoeven 28376c364062SGeert Uytterhoeven rc = spi_add_device(spi); 28386c364062SGeert Uytterhoeven if (rc) { 28396c364062SGeert Uytterhoeven spi_dev_put(spi); 28406c364062SGeert Uytterhoeven return rc; 28416c364062SGeert Uytterhoeven } 28426c364062SGeert Uytterhoeven } 28436c364062SGeert Uytterhoeven 28446c364062SGeert Uytterhoeven return count; 28456c364062SGeert Uytterhoeven } 28466c364062SGeert Uytterhoeven 2847cc8b4659SGeert Uytterhoeven static DEVICE_ATTR_RW(slave); 28486c364062SGeert Uytterhoeven 28496c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = { 28506c364062SGeert Uytterhoeven &dev_attr_slave.attr, 28516c364062SGeert Uytterhoeven NULL, 28526c364062SGeert Uytterhoeven }; 28536c364062SGeert Uytterhoeven 28546c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = { 28556c364062SGeert Uytterhoeven .attrs = spi_slave_attrs, 28566c364062SGeert Uytterhoeven }; 28576c364062SGeert Uytterhoeven 28586c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = { 28598caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 28606c364062SGeert Uytterhoeven &spi_slave_group, 28616c364062SGeert Uytterhoeven NULL, 28626c364062SGeert Uytterhoeven }; 28636c364062SGeert Uytterhoeven 28646c364062SGeert Uytterhoeven static struct class spi_slave_class = { 28656c364062SGeert Uytterhoeven .name = "spi_slave", 28668caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 28676c364062SGeert Uytterhoeven .dev_groups = spi_slave_groups, 28686c364062SGeert Uytterhoeven }; 28696c364062SGeert Uytterhoeven #else 28706c364062SGeert Uytterhoeven extern struct class spi_slave_class; /* dummy */ 28716c364062SGeert Uytterhoeven #endif 28728ae12a0dSDavid Brownell 28738ae12a0dSDavid Brownell /** 28746c364062SGeert Uytterhoeven * __spi_alloc_controller - allocate an SPI master or slave controller 28758ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 287633e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 2877229e6af1SLukas Wunner * memory is in the driver_data field of the returned device, accessible 2878229e6af1SLukas Wunner * with spi_controller_get_devdata(); the memory is cacheline aligned; 2879229e6af1SLukas Wunner * drivers granting DMA access to portions of their private data need to 2880229e6af1SLukas Wunner * round up @size using ALIGN(size, dma_get_cache_alignment()). 28816c364062SGeert Uytterhoeven * @slave: flag indicating whether to allocate an SPI master (false) or SPI 28826c364062SGeert Uytterhoeven * slave (true) controller 288333e34dc6SDavid Brownell * Context: can sleep 28848ae12a0dSDavid Brownell * 28856c364062SGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 28868ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 28878caab75fSGeert Uytterhoeven * an spi_controller structure, prior to calling spi_register_controller(). 28888ae12a0dSDavid Brownell * 288997d56dc6SJavier Martinez Canillas * This must be called from context that can sleep. 28908ae12a0dSDavid Brownell * 28916c364062SGeert Uytterhoeven * The caller is responsible for assigning the bus number and initializing the 28928caab75fSGeert Uytterhoeven * controller's methods before calling spi_register_controller(); and (after 28938caab75fSGeert Uytterhoeven * errors adding the device) calling spi_controller_put() to prevent a memory 28948caab75fSGeert Uytterhoeven * leak. 289597d56dc6SJavier Martinez Canillas * 28966c364062SGeert Uytterhoeven * Return: the SPI controller structure on success, else NULL. 28978ae12a0dSDavid Brownell */ 28988caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev, 28996c364062SGeert Uytterhoeven unsigned int size, bool slave) 29008ae12a0dSDavid Brownell { 29018caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 2902229e6af1SLukas Wunner size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment()); 29038ae12a0dSDavid Brownell 29040c868461SDavid Brownell if (!dev) 29050c868461SDavid Brownell return NULL; 29060c868461SDavid Brownell 2907229e6af1SLukas Wunner ctlr = kzalloc(size + ctlr_size, GFP_KERNEL); 29088caab75fSGeert Uytterhoeven if (!ctlr) 29098ae12a0dSDavid Brownell return NULL; 29108ae12a0dSDavid Brownell 29118caab75fSGeert Uytterhoeven device_initialize(&ctlr->dev); 291216a8e2fbSUwe Kleine-König INIT_LIST_HEAD(&ctlr->queue); 291316a8e2fbSUwe Kleine-König spin_lock_init(&ctlr->queue_lock); 291416a8e2fbSUwe Kleine-König spin_lock_init(&ctlr->bus_lock_spinlock); 291516a8e2fbSUwe Kleine-König mutex_init(&ctlr->bus_lock_mutex); 291616a8e2fbSUwe Kleine-König mutex_init(&ctlr->io_mutex); 291716a8e2fbSUwe Kleine-König mutex_init(&ctlr->add_lock); 29188caab75fSGeert Uytterhoeven ctlr->bus_num = -1; 29198caab75fSGeert Uytterhoeven ctlr->num_chipselect = 1; 29208caab75fSGeert Uytterhoeven ctlr->slave = slave; 29216c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 29228caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_slave_class; 29236c364062SGeert Uytterhoeven else 29248caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_master_class; 29258caab75fSGeert Uytterhoeven ctlr->dev.parent = dev; 29268caab75fSGeert Uytterhoeven pm_suspend_ignore_children(&ctlr->dev, true); 2927229e6af1SLukas Wunner spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size); 29288ae12a0dSDavid Brownell 29298caab75fSGeert Uytterhoeven return ctlr; 29308ae12a0dSDavid Brownell } 29316c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller); 29328ae12a0dSDavid Brownell 29335e844cc3SLukas Wunner static void devm_spi_release_controller(struct device *dev, void *ctlr) 29345e844cc3SLukas Wunner { 29355e844cc3SLukas Wunner spi_controller_put(*(struct spi_controller **)ctlr); 29365e844cc3SLukas Wunner } 29375e844cc3SLukas Wunner 29385e844cc3SLukas Wunner /** 29395e844cc3SLukas Wunner * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller() 29405e844cc3SLukas Wunner * @dev: physical device of SPI controller 29415e844cc3SLukas Wunner * @size: how much zeroed driver-private data to allocate 29425e844cc3SLukas Wunner * @slave: whether to allocate an SPI master (false) or SPI slave (true) 29435e844cc3SLukas Wunner * Context: can sleep 29445e844cc3SLukas Wunner * 29455e844cc3SLukas Wunner * Allocate an SPI controller and automatically release a reference on it 29465e844cc3SLukas Wunner * when @dev is unbound from its driver. Drivers are thus relieved from 29475e844cc3SLukas Wunner * having to call spi_controller_put(). 29485e844cc3SLukas Wunner * 29495e844cc3SLukas Wunner * The arguments to this function are identical to __spi_alloc_controller(). 29505e844cc3SLukas Wunner * 29515e844cc3SLukas Wunner * Return: the SPI controller structure on success, else NULL. 29525e844cc3SLukas Wunner */ 29535e844cc3SLukas Wunner struct spi_controller *__devm_spi_alloc_controller(struct device *dev, 29545e844cc3SLukas Wunner unsigned int size, 29555e844cc3SLukas Wunner bool slave) 29565e844cc3SLukas Wunner { 29575e844cc3SLukas Wunner struct spi_controller **ptr, *ctlr; 29585e844cc3SLukas Wunner 29595e844cc3SLukas Wunner ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr), 29605e844cc3SLukas Wunner GFP_KERNEL); 29615e844cc3SLukas Wunner if (!ptr) 29625e844cc3SLukas Wunner return NULL; 29635e844cc3SLukas Wunner 29645e844cc3SLukas Wunner ctlr = __spi_alloc_controller(dev, size, slave); 29655e844cc3SLukas Wunner if (ctlr) { 2966794aaf01SWilliam A. Kennington III ctlr->devm_allocated = true; 29675e844cc3SLukas Wunner *ptr = ctlr; 29685e844cc3SLukas Wunner devres_add(dev, ptr); 29695e844cc3SLukas Wunner } else { 29705e844cc3SLukas Wunner devres_free(ptr); 29715e844cc3SLukas Wunner } 29725e844cc3SLukas Wunner 29735e844cc3SLukas Wunner return ctlr; 29745e844cc3SLukas Wunner } 29755e844cc3SLukas Wunner EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller); 29765e844cc3SLukas Wunner 2977f3186dd8SLinus Walleij /** 2978f3186dd8SLinus Walleij * spi_get_gpio_descs() - grab chip select GPIOs for the master 2979f3186dd8SLinus Walleij * @ctlr: The SPI master to grab GPIO descriptors for 2980f3186dd8SLinus Walleij */ 2981f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr) 2982f3186dd8SLinus Walleij { 2983f3186dd8SLinus Walleij int nb, i; 2984f3186dd8SLinus Walleij struct gpio_desc **cs; 2985f3186dd8SLinus Walleij struct device *dev = &ctlr->dev; 29867d93aecdSGeert Uytterhoeven unsigned long native_cs_mask = 0; 29877d93aecdSGeert Uytterhoeven unsigned int num_cs_gpios = 0; 2988f3186dd8SLinus Walleij 2989f3186dd8SLinus Walleij nb = gpiod_count(dev, "cs"); 299031ed8ebcSAndy Shevchenko if (nb < 0) { 2991f3186dd8SLinus Walleij /* No GPIOs at all is fine, else return the error */ 299231ed8ebcSAndy Shevchenko if (nb == -ENOENT) 2993f3186dd8SLinus Walleij return 0; 2994f3186dd8SLinus Walleij return nb; 299531ed8ebcSAndy Shevchenko } 299631ed8ebcSAndy Shevchenko 299731ed8ebcSAndy Shevchenko ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2998f3186dd8SLinus Walleij 2999f3186dd8SLinus Walleij cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), 3000f3186dd8SLinus Walleij GFP_KERNEL); 3001f3186dd8SLinus Walleij if (!cs) 3002f3186dd8SLinus Walleij return -ENOMEM; 3003f3186dd8SLinus Walleij ctlr->cs_gpiods = cs; 3004f3186dd8SLinus Walleij 3005f3186dd8SLinus Walleij for (i = 0; i < nb; i++) { 3006f3186dd8SLinus Walleij /* 3007f3186dd8SLinus Walleij * Most chipselects are active low, the inverted 3008f3186dd8SLinus Walleij * semantics are handled by special quirks in gpiolib, 3009f3186dd8SLinus Walleij * so initializing them GPIOD_OUT_LOW here means 3010f3186dd8SLinus Walleij * "unasserted", in most cases this will drive the physical 3011f3186dd8SLinus Walleij * line high. 3012f3186dd8SLinus Walleij */ 3013f3186dd8SLinus Walleij cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, 3014f3186dd8SLinus Walleij GPIOD_OUT_LOW); 30151723fdecSGeert Uytterhoeven if (IS_ERR(cs[i])) 30161723fdecSGeert Uytterhoeven return PTR_ERR(cs[i]); 3017f3186dd8SLinus Walleij 3018f3186dd8SLinus Walleij if (cs[i]) { 3019f3186dd8SLinus Walleij /* 3020f3186dd8SLinus Walleij * If we find a CS GPIO, name it after the device and 3021f3186dd8SLinus Walleij * chip select line. 3022f3186dd8SLinus Walleij */ 3023f3186dd8SLinus Walleij char *gpioname; 3024f3186dd8SLinus Walleij 3025f3186dd8SLinus Walleij gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", 3026f3186dd8SLinus Walleij dev_name(dev), i); 3027f3186dd8SLinus Walleij if (!gpioname) 3028f3186dd8SLinus Walleij return -ENOMEM; 3029f3186dd8SLinus Walleij gpiod_set_consumer_name(cs[i], gpioname); 30307d93aecdSGeert Uytterhoeven num_cs_gpios++; 30317d93aecdSGeert Uytterhoeven continue; 3032f3186dd8SLinus Walleij } 30337d93aecdSGeert Uytterhoeven 30347d93aecdSGeert Uytterhoeven if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { 30357d93aecdSGeert Uytterhoeven dev_err(dev, "Invalid native chip select %d\n", i); 30367d93aecdSGeert Uytterhoeven return -EINVAL; 30377d93aecdSGeert Uytterhoeven } 30387d93aecdSGeert Uytterhoeven native_cs_mask |= BIT(i); 30397d93aecdSGeert Uytterhoeven } 30407d93aecdSGeert Uytterhoeven 3041f60d7270SAndy Shevchenko ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; 3042dbaca8e5SAndy Shevchenko 304382238d2cSAndy Shevchenko if ((ctlr->flags & SPI_CONTROLLER_GPIO_SS) && num_cs_gpios && 3044dbaca8e5SAndy Shevchenko ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { 30457d93aecdSGeert Uytterhoeven dev_err(dev, "No unused native chip select available\n"); 30467d93aecdSGeert Uytterhoeven return -EINVAL; 3047f3186dd8SLinus Walleij } 3048f3186dd8SLinus Walleij 3049f3186dd8SLinus Walleij return 0; 3050f3186dd8SLinus Walleij } 3051f3186dd8SLinus Walleij 3052bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr) 3053bdf3a3b5SBoris Brezillon { 3054bdf3a3b5SBoris Brezillon /* 3055b5932f5cSBoris Brezillon * The controller may implement only the high-level SPI-memory like 3056b5932f5cSBoris Brezillon * operations if it does not support regular SPI transfers, and this is 3057b5932f5cSBoris Brezillon * valid use case. 305876a85704SWilliam Zhang * If ->mem_ops or ->mem_ops->exec_op is NULL, we request that at least 305976a85704SWilliam Zhang * one of the ->transfer_xxx() method be implemented. 3060bdf3a3b5SBoris Brezillon */ 306120064c47SWilliam Zhang if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) { 306276a85704SWilliam Zhang if (!ctlr->transfer && !ctlr->transfer_one && 3063b5932f5cSBoris Brezillon !ctlr->transfer_one_message) { 3064b5932f5cSBoris Brezillon return -EINVAL; 3065b5932f5cSBoris Brezillon } 306676a85704SWilliam Zhang } 3067bdf3a3b5SBoris Brezillon 3068bdf3a3b5SBoris Brezillon return 0; 3069bdf3a3b5SBoris Brezillon } 3070bdf3a3b5SBoris Brezillon 3071440c4733SAndy Shevchenko /* Allocate dynamic bus number using Linux idr */ 3072440c4733SAndy Shevchenko static int spi_controller_id_alloc(struct spi_controller *ctlr, int start, int end) 3073440c4733SAndy Shevchenko { 3074440c4733SAndy Shevchenko int id; 3075440c4733SAndy Shevchenko 3076440c4733SAndy Shevchenko mutex_lock(&board_lock); 3077440c4733SAndy Shevchenko id = idr_alloc(&spi_master_idr, ctlr, start, end, GFP_KERNEL); 3078440c4733SAndy Shevchenko mutex_unlock(&board_lock); 3079440c4733SAndy Shevchenko if (WARN(id < 0, "couldn't get idr")) 3080440c4733SAndy Shevchenko return id == -ENOSPC ? -EBUSY : id; 3081440c4733SAndy Shevchenko ctlr->bus_num = id; 3082440c4733SAndy Shevchenko return 0; 3083440c4733SAndy Shevchenko } 3084440c4733SAndy Shevchenko 30858ae12a0dSDavid Brownell /** 30868caab75fSGeert Uytterhoeven * spi_register_controller - register SPI master or slave controller 30878caab75fSGeert Uytterhoeven * @ctlr: initialized master, originally from spi_alloc_master() or 30888caab75fSGeert Uytterhoeven * spi_alloc_slave() 308933e34dc6SDavid Brownell * Context: can sleep 30908ae12a0dSDavid Brownell * 30918caab75fSGeert Uytterhoeven * SPI controllers connect to their drivers using some non-SPI bus, 30928ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 30938caab75fSGeert Uytterhoeven * includes calling spi_register_controller() to hook up to this SPI bus glue. 30948ae12a0dSDavid Brownell * 30958ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 30968ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 30978ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 30988ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 30998ae12a0dSDavid Brownell * chip is at which address. 31008ae12a0dSDavid Brownell * 31018ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 31028caab75fSGeert Uytterhoeven * success, else a negative error code (dropping the controller's refcount). 31030c868461SDavid Brownell * After a successful return, the caller is responsible for calling 31048caab75fSGeert Uytterhoeven * spi_unregister_controller(). 310597d56dc6SJavier Martinez Canillas * 310697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 31078ae12a0dSDavid Brownell */ 31088caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr) 31098ae12a0dSDavid Brownell { 31108caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 31112b9603a0SFeng Tang struct boardinfo *bi; 3112440c4733SAndy Shevchenko int first_dynamic; 3113b93318a2SSergei Shtylyov int status; 31148ae12a0dSDavid Brownell 31150c868461SDavid Brownell if (!dev) 31160c868461SDavid Brownell return -ENODEV; 31170c868461SDavid Brownell 3118bdf3a3b5SBoris Brezillon /* 3119bdf3a3b5SBoris Brezillon * Make sure all necessary hooks are implemented before registering 3120bdf3a3b5SBoris Brezillon * the SPI controller. 3121bdf3a3b5SBoris Brezillon */ 3122bdf3a3b5SBoris Brezillon status = spi_controller_check_ops(ctlr); 3123bdf3a3b5SBoris Brezillon if (status) 3124bdf3a3b5SBoris Brezillon return status; 3125bdf3a3b5SBoris Brezillon 3126440c4733SAndy Shevchenko if (ctlr->bus_num < 0) 3127440c4733SAndy Shevchenko ctlr->bus_num = of_alias_get_id(ctlr->dev.of_node, "spi"); 312804b2d03aSGeert Uytterhoeven if (ctlr->bus_num >= 0) { 312995c8222fSDavid Jander /* Devices with a fixed bus num must check-in with the num */ 3130440c4733SAndy Shevchenko status = spi_controller_id_alloc(ctlr, ctlr->bus_num, ctlr->bus_num + 1); 3131440c4733SAndy Shevchenko if (status) 3132440c4733SAndy Shevchenko return status; 31339b61e302SSuniel Mahesh } 31348caab75fSGeert Uytterhoeven if (ctlr->bus_num < 0) { 313542bdd706SLucas Stach first_dynamic = of_alias_get_highest_id("spi"); 313642bdd706SLucas Stach if (first_dynamic < 0) 313742bdd706SLucas Stach first_dynamic = 0; 313842bdd706SLucas Stach else 313942bdd706SLucas Stach first_dynamic++; 314042bdd706SLucas Stach 3141440c4733SAndy Shevchenko status = spi_controller_id_alloc(ctlr, first_dynamic, 0); 3142440c4733SAndy Shevchenko if (status) 3143440c4733SAndy Shevchenko return status; 31448ae12a0dSDavid Brownell } 31458caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 31468caab75fSGeert Uytterhoeven init_completion(&ctlr->xfer_completion); 314769fa9590SDavid Jander init_completion(&ctlr->cur_msg_completion); 31488caab75fSGeert Uytterhoeven if (!ctlr->max_dma_len) 31498caab75fSGeert Uytterhoeven ctlr->max_dma_len = INT_MAX; 3150cf32b71eSErnst Schwab 3151350de7ceSAndy Shevchenko /* 3152350de7ceSAndy Shevchenko * Register the device, then userspace will see it. 3153350de7ceSAndy Shevchenko * Registration fails if the bus ID is in use. 31548ae12a0dSDavid Brownell */ 31558caab75fSGeert Uytterhoeven dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 31560a919ae4SAndrey Smirnov 3157f48dc6b9SLinus Walleij if (!spi_controller_is_slave(ctlr) && ctlr->use_gpio_descriptors) { 31580a919ae4SAndrey Smirnov status = spi_get_gpio_descs(ctlr); 31590a919ae4SAndrey Smirnov if (status) 3160f9981d4fSAaro Koskinen goto free_bus_id; 31610a919ae4SAndrey Smirnov /* 31620a919ae4SAndrey Smirnov * A controller using GPIO descriptors always 31630a919ae4SAndrey Smirnov * supports SPI_CS_HIGH if need be. 31640a919ae4SAndrey Smirnov */ 31650a919ae4SAndrey Smirnov ctlr->mode_bits |= SPI_CS_HIGH; 31660a919ae4SAndrey Smirnov } 31670a919ae4SAndrey Smirnov 3168f9481b08STudor Ambarus /* 3169f9481b08STudor Ambarus * Even if it's just one always-selected device, there must 3170f9481b08STudor Ambarus * be at least one chipselect. 3171f9481b08STudor Ambarus */ 3172f9981d4fSAaro Koskinen if (!ctlr->num_chipselect) { 3173f9981d4fSAaro Koskinen status = -EINVAL; 3174f9981d4fSAaro Koskinen goto free_bus_id; 3175f9981d4fSAaro Koskinen } 3176f9481b08STudor Ambarus 317795c8222fSDavid Jander /* Setting last_cs to -1 means no chip selected */ 31786bb477dfSYun Zhou ctlr->last_cs = -1; 31796bb477dfSYun Zhou 31808caab75fSGeert Uytterhoeven status = device_add(&ctlr->dev); 3181f9981d4fSAaro Koskinen if (status < 0) 3182f9981d4fSAaro Koskinen goto free_bus_id; 31839b61e302SSuniel Mahesh dev_dbg(dev, "registered %s %s\n", 31848caab75fSGeert Uytterhoeven spi_controller_is_slave(ctlr) ? "slave" : "master", 31859b61e302SSuniel Mahesh dev_name(&ctlr->dev)); 31868ae12a0dSDavid Brownell 3187b5932f5cSBoris Brezillon /* 3188b5932f5cSBoris Brezillon * If we're using a queued driver, start the queue. Note that we don't 3189b5932f5cSBoris Brezillon * need the queueing logic if the driver is only supporting high-level 3190b5932f5cSBoris Brezillon * memory operations. 3191b5932f5cSBoris Brezillon */ 3192b5932f5cSBoris Brezillon if (ctlr->transfer) { 31938caab75fSGeert Uytterhoeven dev_info(dev, "controller is unqueued, this is deprecated\n"); 3194b5932f5cSBoris Brezillon } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 31958caab75fSGeert Uytterhoeven status = spi_controller_initialize_queue(ctlr); 3196ffbbdd21SLinus Walleij if (status) { 31978caab75fSGeert Uytterhoeven device_del(&ctlr->dev); 3198f9981d4fSAaro Koskinen goto free_bus_id; 3199ffbbdd21SLinus Walleij } 3200ffbbdd21SLinus Walleij } 320195c8222fSDavid Jander /* Add statistics */ 32026598b91bSDavid Jander ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev); 32036598b91bSDavid Jander if (!ctlr->pcpu_statistics) { 32046598b91bSDavid Jander dev_err(dev, "Error allocating per-cpu statistics\n"); 3205d52b095bSDan Carpenter status = -ENOMEM; 32066598b91bSDavid Jander goto destroy_queue; 32076598b91bSDavid Jander } 3208ffbbdd21SLinus Walleij 32092b9603a0SFeng Tang mutex_lock(&board_lock); 32108caab75fSGeert Uytterhoeven list_add_tail(&ctlr->list, &spi_controller_list); 32112b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 32128caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 32132b9603a0SFeng Tang mutex_unlock(&board_lock); 32142b9603a0SFeng Tang 321564bee4d2SMika Westerberg /* Register devices from the device tree and ACPI */ 32168caab75fSGeert Uytterhoeven of_register_spi_devices(ctlr); 32178caab75fSGeert Uytterhoeven acpi_register_spi_devices(ctlr); 3218f9981d4fSAaro Koskinen return status; 3219f9981d4fSAaro Koskinen 32206598b91bSDavid Jander destroy_queue: 32216598b91bSDavid Jander spi_destroy_queue(ctlr); 3222f9981d4fSAaro Koskinen free_bus_id: 3223f9981d4fSAaro Koskinen mutex_lock(&board_lock); 3224f9981d4fSAaro Koskinen idr_remove(&spi_master_idr, ctlr->bus_num); 3225f9981d4fSAaro Koskinen mutex_unlock(&board_lock); 32268ae12a0dSDavid Brownell return status; 32278ae12a0dSDavid Brownell } 32288caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller); 32298ae12a0dSDavid Brownell 323043cc5a0aSYang Yingliang static void devm_spi_unregister(struct device *dev, void *res) 3231666d5b4cSMark Brown { 323243cc5a0aSYang Yingliang spi_unregister_controller(*(struct spi_controller **)res); 3233666d5b4cSMark Brown } 3234666d5b4cSMark Brown 3235666d5b4cSMark Brown /** 32368caab75fSGeert Uytterhoeven * devm_spi_register_controller - register managed SPI master or slave 32378caab75fSGeert Uytterhoeven * controller 32388caab75fSGeert Uytterhoeven * @dev: device managing SPI controller 32398caab75fSGeert Uytterhoeven * @ctlr: initialized controller, originally from spi_alloc_master() or 32408caab75fSGeert Uytterhoeven * spi_alloc_slave() 3241666d5b4cSMark Brown * Context: can sleep 3242666d5b4cSMark Brown * 32438caab75fSGeert Uytterhoeven * Register a SPI device as with spi_register_controller() which will 324468b892f1SJohan Hovold * automatically be unregistered and freed. 324597d56dc6SJavier Martinez Canillas * 324697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3247666d5b4cSMark Brown */ 32488caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev, 32498caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 3250666d5b4cSMark Brown { 325143cc5a0aSYang Yingliang struct spi_controller **ptr; 3252666d5b4cSMark Brown int ret; 3253666d5b4cSMark Brown 325443cc5a0aSYang Yingliang ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); 325543cc5a0aSYang Yingliang if (!ptr) 325643cc5a0aSYang Yingliang return -ENOMEM; 325759ebbe40STian Tao 325843cc5a0aSYang Yingliang ret = spi_register_controller(ctlr); 325943cc5a0aSYang Yingliang if (!ret) { 326043cc5a0aSYang Yingliang *ptr = ctlr; 326143cc5a0aSYang Yingliang devres_add(dev, ptr); 326243cc5a0aSYang Yingliang } else { 326343cc5a0aSYang Yingliang devres_free(ptr); 326443cc5a0aSYang Yingliang } 326543cc5a0aSYang Yingliang 326643cc5a0aSYang Yingliang return ret; 3267666d5b4cSMark Brown } 32688caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller); 3269666d5b4cSMark Brown 327034860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 32718ae12a0dSDavid Brownell { 32720c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 32738ae12a0dSDavid Brownell return 0; 32748ae12a0dSDavid Brownell } 32758ae12a0dSDavid Brownell 32768ae12a0dSDavid Brownell /** 32778caab75fSGeert Uytterhoeven * spi_unregister_controller - unregister SPI master or slave controller 32788caab75fSGeert Uytterhoeven * @ctlr: the controller being unregistered 327933e34dc6SDavid Brownell * Context: can sleep 32808ae12a0dSDavid Brownell * 32818caab75fSGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 32828ae12a0dSDavid Brownell * only ones directly touching chip registers. 32838ae12a0dSDavid Brownell * 32848ae12a0dSDavid Brownell * This must be called from context that can sleep. 328568b892f1SJohan Hovold * 328668b892f1SJohan Hovold * Note that this function also drops a reference to the controller. 32878ae12a0dSDavid Brownell */ 32888caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr) 32898ae12a0dSDavid Brownell { 32909b61e302SSuniel Mahesh struct spi_controller *found; 329167f7b278SJohan Hovold int id = ctlr->bus_num; 329289fc9a1aSJeff Garzik 3293ddf75be4SLukas Wunner /* Prevent addition of new devices, unregister existing ones */ 3294ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 32956098475dSMark Brown mutex_lock(&ctlr->add_lock); 3296ddf75be4SLukas Wunner 329784855678SLukas Wunner device_for_each_child(&ctlr->dev, NULL, __unregister); 329884855678SLukas Wunner 32999b61e302SSuniel Mahesh /* First make sure that this controller was ever added */ 33009b61e302SSuniel Mahesh mutex_lock(&board_lock); 330167f7b278SJohan Hovold found = idr_find(&spi_master_idr, id); 33029b61e302SSuniel Mahesh mutex_unlock(&board_lock); 33038caab75fSGeert Uytterhoeven if (ctlr->queued) { 33048caab75fSGeert Uytterhoeven if (spi_destroy_queue(ctlr)) 33058caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue remove failed\n"); 3306ffbbdd21SLinus Walleij } 33072b9603a0SFeng Tang mutex_lock(&board_lock); 33088caab75fSGeert Uytterhoeven list_del(&ctlr->list); 33092b9603a0SFeng Tang mutex_unlock(&board_lock); 33102b9603a0SFeng Tang 33115e844cc3SLukas Wunner device_del(&ctlr->dev); 33125e844cc3SLukas Wunner 331395c8222fSDavid Jander /* Free bus id */ 33149b61e302SSuniel Mahesh mutex_lock(&board_lock); 3315613bd1eaSJarkko Nikula if (found == ctlr) 331667f7b278SJohan Hovold idr_remove(&spi_master_idr, id); 33179b61e302SSuniel Mahesh mutex_unlock(&board_lock); 3318ddf75be4SLukas Wunner 3319ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 33206098475dSMark Brown mutex_unlock(&ctlr->add_lock); 33216c53b45cSMichael Walle 3322702ca026SAndy Shevchenko /* 3323702ca026SAndy Shevchenko * Release the last reference on the controller if its driver 33246c53b45cSMichael Walle * has not yet been converted to devm_spi_alloc_master/slave(). 33256c53b45cSMichael Walle */ 33266c53b45cSMichael Walle if (!ctlr->devm_allocated) 33276c53b45cSMichael Walle put_device(&ctlr->dev); 33288ae12a0dSDavid Brownell } 33298caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller); 33308ae12a0dSDavid Brownell 33318caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr) 3332ffbbdd21SLinus Walleij { 3333ffbbdd21SLinus Walleij int ret; 3334ffbbdd21SLinus Walleij 33358caab75fSGeert Uytterhoeven /* Basically no-ops for non-queued controllers */ 33368caab75fSGeert Uytterhoeven if (!ctlr->queued) 3337ffbbdd21SLinus Walleij return 0; 3338ffbbdd21SLinus Walleij 33398caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 3340ffbbdd21SLinus Walleij if (ret) 33418caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue stop failed\n"); 3342ffbbdd21SLinus Walleij 3343ffbbdd21SLinus Walleij return ret; 3344ffbbdd21SLinus Walleij } 33458caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend); 3346ffbbdd21SLinus Walleij 33478caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr) 3348ffbbdd21SLinus Walleij { 3349ffbbdd21SLinus Walleij int ret; 3350ffbbdd21SLinus Walleij 33518caab75fSGeert Uytterhoeven if (!ctlr->queued) 3352ffbbdd21SLinus Walleij return 0; 3353ffbbdd21SLinus Walleij 33548caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 3355ffbbdd21SLinus Walleij if (ret) 33568caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue restart failed\n"); 3357ffbbdd21SLinus Walleij 3358ffbbdd21SLinus Walleij return ret; 3359ffbbdd21SLinus Walleij } 33608caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume); 3361ffbbdd21SLinus Walleij 33628ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 33638ae12a0dSDavid Brownell 3364523baf5aSMartin Sperl /* Core methods for spi_message alterations */ 3365523baf5aSMartin Sperl 33668caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr, 3367523baf5aSMartin Sperl struct spi_message *msg, 3368523baf5aSMartin Sperl void *res) 3369523baf5aSMartin Sperl { 3370523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer = res; 3371523baf5aSMartin Sperl size_t i; 3372523baf5aSMartin Sperl 337395c8222fSDavid Jander /* Call extra callback if requested */ 3374523baf5aSMartin Sperl if (rxfer->release) 33758caab75fSGeert Uytterhoeven rxfer->release(ctlr, msg, res); 3376523baf5aSMartin Sperl 337795c8222fSDavid Jander /* Insert replaced transfers back into the message */ 3378523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 3379523baf5aSMartin Sperl 338095c8222fSDavid Jander /* Remove the formerly inserted entries */ 3381523baf5aSMartin Sperl for (i = 0; i < rxfer->inserted; i++) 3382523baf5aSMartin Sperl list_del(&rxfer->inserted_transfers[i].transfer_list); 3383523baf5aSMartin Sperl } 3384523baf5aSMartin Sperl 3385523baf5aSMartin Sperl /** 3386523baf5aSMartin Sperl * spi_replace_transfers - replace transfers with several transfers 3387523baf5aSMartin Sperl * and register change with spi_message.resources 3388523baf5aSMartin Sperl * @msg: the spi_message we work upon 3389523baf5aSMartin Sperl * @xfer_first: the first spi_transfer we want to replace 3390523baf5aSMartin Sperl * @remove: number of transfers to remove 3391523baf5aSMartin Sperl * @insert: the number of transfers we want to insert instead 3392523baf5aSMartin Sperl * @release: extra release code necessary in some circumstances 3393523baf5aSMartin Sperl * @extradatasize: extra data to allocate (with alignment guarantees 3394523baf5aSMartin Sperl * of struct @spi_transfer) 339505885397SMartin Sperl * @gfp: gfp flags 3396523baf5aSMartin Sperl * 3397523baf5aSMartin Sperl * Returns: pointer to @spi_replaced_transfers, 3398523baf5aSMartin Sperl * PTR_ERR(...) in case of errors. 3399523baf5aSMartin Sperl */ 3400da21fde0SUwe Kleine-König static struct spi_replaced_transfers *spi_replace_transfers( 3401523baf5aSMartin Sperl struct spi_message *msg, 3402523baf5aSMartin Sperl struct spi_transfer *xfer_first, 3403523baf5aSMartin Sperl size_t remove, 3404523baf5aSMartin Sperl size_t insert, 3405523baf5aSMartin Sperl spi_replaced_release_t release, 3406523baf5aSMartin Sperl size_t extradatasize, 3407523baf5aSMartin Sperl gfp_t gfp) 3408523baf5aSMartin Sperl { 3409523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer; 3410523baf5aSMartin Sperl struct spi_transfer *xfer; 3411523baf5aSMartin Sperl size_t i; 3412523baf5aSMartin Sperl 341395c8222fSDavid Jander /* Allocate the structure using spi_res */ 3414523baf5aSMartin Sperl rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 3415aef97522SGustavo A. R. Silva struct_size(rxfer, inserted_transfers, insert) 3416523baf5aSMartin Sperl + extradatasize, 3417523baf5aSMartin Sperl gfp); 3418523baf5aSMartin Sperl if (!rxfer) 3419523baf5aSMartin Sperl return ERR_PTR(-ENOMEM); 3420523baf5aSMartin Sperl 342195c8222fSDavid Jander /* The release code to invoke before running the generic release */ 3422523baf5aSMartin Sperl rxfer->release = release; 3423523baf5aSMartin Sperl 342495c8222fSDavid Jander /* Assign extradata */ 3425523baf5aSMartin Sperl if (extradatasize) 3426523baf5aSMartin Sperl rxfer->extradata = 3427523baf5aSMartin Sperl &rxfer->inserted_transfers[insert]; 3428523baf5aSMartin Sperl 342995c8222fSDavid Jander /* Init the replaced_transfers list */ 3430523baf5aSMartin Sperl INIT_LIST_HEAD(&rxfer->replaced_transfers); 3431523baf5aSMartin Sperl 3432350de7ceSAndy Shevchenko /* 3433350de7ceSAndy Shevchenko * Assign the list_entry after which we should reinsert 3434523baf5aSMartin Sperl * the @replaced_transfers - it may be spi_message.messages! 3435523baf5aSMartin Sperl */ 3436523baf5aSMartin Sperl rxfer->replaced_after = xfer_first->transfer_list.prev; 3437523baf5aSMartin Sperl 343895c8222fSDavid Jander /* Remove the requested number of transfers */ 3439523baf5aSMartin Sperl for (i = 0; i < remove; i++) { 3440350de7ceSAndy Shevchenko /* 3441350de7ceSAndy Shevchenko * If the entry after replaced_after it is msg->transfers 3442523baf5aSMartin Sperl * then we have been requested to remove more transfers 3443350de7ceSAndy Shevchenko * than are in the list. 3444523baf5aSMartin Sperl */ 3445523baf5aSMartin Sperl if (rxfer->replaced_after->next == &msg->transfers) { 3446523baf5aSMartin Sperl dev_err(&msg->spi->dev, 3447523baf5aSMartin Sperl "requested to remove more spi_transfers than are available\n"); 344895c8222fSDavid Jander /* Insert replaced transfers back into the message */ 3449523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, 3450523baf5aSMartin Sperl rxfer->replaced_after); 3451523baf5aSMartin Sperl 345295c8222fSDavid Jander /* Free the spi_replace_transfer structure... */ 3453523baf5aSMartin Sperl spi_res_free(rxfer); 3454523baf5aSMartin Sperl 345595c8222fSDavid Jander /* ...and return with an error */ 3456523baf5aSMartin Sperl return ERR_PTR(-EINVAL); 3457523baf5aSMartin Sperl } 3458523baf5aSMartin Sperl 3459350de7ceSAndy Shevchenko /* 3460350de7ceSAndy Shevchenko * Remove the entry after replaced_after from list of 3461350de7ceSAndy Shevchenko * transfers and add it to list of replaced_transfers. 3462523baf5aSMartin Sperl */ 3463523baf5aSMartin Sperl list_move_tail(rxfer->replaced_after->next, 3464523baf5aSMartin Sperl &rxfer->replaced_transfers); 3465523baf5aSMartin Sperl } 3466523baf5aSMartin Sperl 3467350de7ceSAndy Shevchenko /* 3468350de7ceSAndy Shevchenko * Create copy of the given xfer with identical settings 3469350de7ceSAndy Shevchenko * based on the first transfer to get removed. 3470523baf5aSMartin Sperl */ 3471523baf5aSMartin Sperl for (i = 0; i < insert; i++) { 347295c8222fSDavid Jander /* We need to run in reverse order */ 3473523baf5aSMartin Sperl xfer = &rxfer->inserted_transfers[insert - 1 - i]; 3474523baf5aSMartin Sperl 347595c8222fSDavid Jander /* Copy all spi_transfer data */ 3476523baf5aSMartin Sperl memcpy(xfer, xfer_first, sizeof(*xfer)); 3477523baf5aSMartin Sperl 347895c8222fSDavid Jander /* Add to list */ 3479523baf5aSMartin Sperl list_add(&xfer->transfer_list, rxfer->replaced_after); 3480523baf5aSMartin Sperl 348195c8222fSDavid Jander /* Clear cs_change and delay for all but the last */ 3482523baf5aSMartin Sperl if (i) { 3483523baf5aSMartin Sperl xfer->cs_change = false; 3484bebcfd27SAlexandru Ardelean xfer->delay.value = 0; 3485523baf5aSMartin Sperl } 3486523baf5aSMartin Sperl } 3487523baf5aSMartin Sperl 348895c8222fSDavid Jander /* Set up inserted... */ 3489523baf5aSMartin Sperl rxfer->inserted = insert; 3490523baf5aSMartin Sperl 349195c8222fSDavid Jander /* ...and register it with spi_res/spi_message */ 3492523baf5aSMartin Sperl spi_res_add(msg, rxfer); 3493523baf5aSMartin Sperl 3494523baf5aSMartin Sperl return rxfer; 3495523baf5aSMartin Sperl } 3496523baf5aSMartin Sperl 34978caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 3498d9f12122SMartin Sperl struct spi_message *msg, 3499d9f12122SMartin Sperl struct spi_transfer **xferp, 3500d9f12122SMartin Sperl size_t maxsize, 3501d9f12122SMartin Sperl gfp_t gfp) 3502d9f12122SMartin Sperl { 3503d9f12122SMartin Sperl struct spi_transfer *xfer = *xferp, *xfers; 3504d9f12122SMartin Sperl struct spi_replaced_transfers *srt; 3505d9f12122SMartin Sperl size_t offset; 3506d9f12122SMartin Sperl size_t count, i; 3507d9f12122SMartin Sperl 350895c8222fSDavid Jander /* Calculate how many we have to replace */ 3509d9f12122SMartin Sperl count = DIV_ROUND_UP(xfer->len, maxsize); 3510d9f12122SMartin Sperl 351195c8222fSDavid Jander /* Create replacement */ 3512d9f12122SMartin Sperl srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 3513657d32efSDan Carpenter if (IS_ERR(srt)) 3514657d32efSDan Carpenter return PTR_ERR(srt); 3515d9f12122SMartin Sperl xfers = srt->inserted_transfers; 3516d9f12122SMartin Sperl 3517350de7ceSAndy Shevchenko /* 3518350de7ceSAndy Shevchenko * Now handle each of those newly inserted spi_transfers. 3519350de7ceSAndy Shevchenko * Note that the replacements spi_transfers all are preset 3520d9f12122SMartin Sperl * to the same values as *xferp, so tx_buf, rx_buf and len 3521d9f12122SMartin Sperl * are all identical (as well as most others) 3522d9f12122SMartin Sperl * so we just have to fix up len and the pointers. 3523d9f12122SMartin Sperl * 3524350de7ceSAndy Shevchenko * This also includes support for the depreciated 3525350de7ceSAndy Shevchenko * spi_message.is_dma_mapped interface. 3526d9f12122SMartin Sperl */ 3527d9f12122SMartin Sperl 3528350de7ceSAndy Shevchenko /* 3529350de7ceSAndy Shevchenko * The first transfer just needs the length modified, so we 3530350de7ceSAndy Shevchenko * run it outside the loop. 3531d9f12122SMartin Sperl */ 3532c8dab77aSFabio Estevam xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 3533d9f12122SMartin Sperl 353495c8222fSDavid Jander /* All the others need rx_buf/tx_buf also set */ 3535d9f12122SMartin Sperl for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 3536702ca026SAndy Shevchenko /* Update rx_buf, tx_buf and DMA */ 3537d9f12122SMartin Sperl if (xfers[i].rx_buf) 3538d9f12122SMartin Sperl xfers[i].rx_buf += offset; 3539d9f12122SMartin Sperl if (xfers[i].rx_dma) 3540d9f12122SMartin Sperl xfers[i].rx_dma += offset; 3541d9f12122SMartin Sperl if (xfers[i].tx_buf) 3542d9f12122SMartin Sperl xfers[i].tx_buf += offset; 3543d9f12122SMartin Sperl if (xfers[i].tx_dma) 3544d9f12122SMartin Sperl xfers[i].tx_dma += offset; 3545d9f12122SMartin Sperl 354695c8222fSDavid Jander /* Update length */ 3547d9f12122SMartin Sperl xfers[i].len = min(maxsize, xfers[i].len - offset); 3548d9f12122SMartin Sperl } 3549d9f12122SMartin Sperl 3550350de7ceSAndy Shevchenko /* 3551350de7ceSAndy Shevchenko * We set up xferp to the last entry we have inserted, 3552350de7ceSAndy Shevchenko * so that we skip those already split transfers. 3553d9f12122SMartin Sperl */ 3554d9f12122SMartin Sperl *xferp = &xfers[count - 1]; 3555d9f12122SMartin Sperl 355695c8222fSDavid Jander /* Increment statistics counters */ 35576598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, 3558d9f12122SMartin Sperl transfers_split_maxsize); 35596598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics, 3560d9f12122SMartin Sperl transfers_split_maxsize); 3561d9f12122SMartin Sperl 3562d9f12122SMartin Sperl return 0; 3563d9f12122SMartin Sperl } 3564d9f12122SMartin Sperl 3565d9f12122SMartin Sperl /** 3566ce2424d7SMauro Carvalho Chehab * spi_split_transfers_maxsize - split spi transfers into multiple transfers 3567d9f12122SMartin Sperl * when an individual transfer exceeds a 3568d9f12122SMartin Sperl * certain size 35698caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller for this transfer 35703700ce95SMasanari Iida * @msg: the @spi_message to transform 35713700ce95SMasanari Iida * @maxsize: the maximum when to apply this 357210f11a22SJavier Martinez Canillas * @gfp: GFP allocation flags 3573d9f12122SMartin Sperl * 3574d9f12122SMartin Sperl * Return: status of transformation 3575d9f12122SMartin Sperl */ 35768caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr, 3577d9f12122SMartin Sperl struct spi_message *msg, 3578d9f12122SMartin Sperl size_t maxsize, 3579d9f12122SMartin Sperl gfp_t gfp) 3580d9f12122SMartin Sperl { 3581d9f12122SMartin Sperl struct spi_transfer *xfer; 3582d9f12122SMartin Sperl int ret; 3583d9f12122SMartin Sperl 3584350de7ceSAndy Shevchenko /* 3585350de7ceSAndy Shevchenko * Iterate over the transfer_list, 3586d9f12122SMartin Sperl * but note that xfer is advanced to the last transfer inserted 3587d9f12122SMartin Sperl * to avoid checking sizes again unnecessarily (also xfer does 3588350de7ceSAndy Shevchenko * potentially belong to a different list by the time the 3589350de7ceSAndy Shevchenko * replacement has happened). 3590d9f12122SMartin Sperl */ 3591d9f12122SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 3592d9f12122SMartin Sperl if (xfer->len > maxsize) { 35938caab75fSGeert Uytterhoeven ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 35948caab75fSGeert Uytterhoeven maxsize, gfp); 3595d9f12122SMartin Sperl if (ret) 3596d9f12122SMartin Sperl return ret; 3597d9f12122SMartin Sperl } 3598d9f12122SMartin Sperl } 3599d9f12122SMartin Sperl 3600d9f12122SMartin Sperl return 0; 3601d9f12122SMartin Sperl } 3602d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 36038ae12a0dSDavid Brownell 3604027781f3SLeonard Göhrs 3605027781f3SLeonard Göhrs /** 3606702ca026SAndy Shevchenko * spi_split_transfers_maxwords - split SPI transfers into multiple transfers 3607027781f3SLeonard Göhrs * when an individual transfer exceeds a 3608027781f3SLeonard Göhrs * certain number of SPI words 3609027781f3SLeonard Göhrs * @ctlr: the @spi_controller for this transfer 3610027781f3SLeonard Göhrs * @msg: the @spi_message to transform 3611027781f3SLeonard Göhrs * @maxwords: the number of words to limit each transfer to 3612027781f3SLeonard Göhrs * @gfp: GFP allocation flags 3613027781f3SLeonard Göhrs * 3614027781f3SLeonard Göhrs * Return: status of transformation 3615027781f3SLeonard Göhrs */ 3616027781f3SLeonard Göhrs int spi_split_transfers_maxwords(struct spi_controller *ctlr, 3617027781f3SLeonard Göhrs struct spi_message *msg, 3618027781f3SLeonard Göhrs size_t maxwords, 3619027781f3SLeonard Göhrs gfp_t gfp) 3620027781f3SLeonard Göhrs { 3621027781f3SLeonard Göhrs struct spi_transfer *xfer; 3622027781f3SLeonard Göhrs 3623027781f3SLeonard Göhrs /* 3624027781f3SLeonard Göhrs * Iterate over the transfer_list, 3625027781f3SLeonard Göhrs * but note that xfer is advanced to the last transfer inserted 3626027781f3SLeonard Göhrs * to avoid checking sizes again unnecessarily (also xfer does 3627027781f3SLeonard Göhrs * potentially belong to a different list by the time the 3628027781f3SLeonard Göhrs * replacement has happened). 3629027781f3SLeonard Göhrs */ 3630027781f3SLeonard Göhrs list_for_each_entry(xfer, &msg->transfers, transfer_list) { 3631027781f3SLeonard Göhrs size_t maxsize; 3632027781f3SLeonard Göhrs int ret; 3633027781f3SLeonard Göhrs 36342b308e71SAndy Shevchenko maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word)); 3635027781f3SLeonard Göhrs if (xfer->len > maxsize) { 3636027781f3SLeonard Göhrs ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 3637027781f3SLeonard Göhrs maxsize, gfp); 3638027781f3SLeonard Göhrs if (ret) 3639027781f3SLeonard Göhrs return ret; 3640027781f3SLeonard Göhrs } 3641027781f3SLeonard Göhrs } 3642027781f3SLeonard Göhrs 3643027781f3SLeonard Göhrs return 0; 3644027781f3SLeonard Göhrs } 3645027781f3SLeonard Göhrs EXPORT_SYMBOL_GPL(spi_split_transfers_maxwords); 3646027781f3SLeonard Göhrs 36478ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 36488ae12a0dSDavid Brownell 3649702ca026SAndy Shevchenko /* 3650702ca026SAndy Shevchenko * Core methods for SPI controller protocol drivers. Some of the 36517d077197SDavid Brownell * other core methods are currently defined as inline functions. 36527d077197SDavid Brownell */ 36537d077197SDavid Brownell 36548caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 36558caab75fSGeert Uytterhoeven u8 bits_per_word) 365663ab645fSStefan Brüns { 36578caab75fSGeert Uytterhoeven if (ctlr->bits_per_word_mask) { 365863ab645fSStefan Brüns /* Only 32 bits fit in the mask */ 365963ab645fSStefan Brüns if (bits_per_word > 32) 366063ab645fSStefan Brüns return -EINVAL; 36618caab75fSGeert Uytterhoeven if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 366263ab645fSStefan Brüns return -EINVAL; 366363ab645fSStefan Brüns } 366463ab645fSStefan Brüns 366563ab645fSStefan Brüns return 0; 366663ab645fSStefan Brüns } 366763ab645fSStefan Brüns 36687d077197SDavid Brownell /** 3669684a4784STudor Ambarus * spi_set_cs_timing - configure CS setup, hold, and inactive delays 3670684a4784STudor Ambarus * @spi: the device that requires specific CS timing configuration 3671684a4784STudor Ambarus * 3672684a4784STudor Ambarus * Return: zero on success, else a negative error code. 3673684a4784STudor Ambarus */ 3674684a4784STudor Ambarus static int spi_set_cs_timing(struct spi_device *spi) 3675684a4784STudor Ambarus { 3676684a4784STudor Ambarus struct device *parent = spi->controller->dev.parent; 3677684a4784STudor Ambarus int status = 0; 3678684a4784STudor Ambarus 3679303feb3cSAmit Kumar Mahapatra if (spi->controller->set_cs_timing && !spi_get_csgpiod(spi, 0)) { 3680684a4784STudor Ambarus if (spi->controller->auto_runtime_pm) { 3681684a4784STudor Ambarus status = pm_runtime_get_sync(parent); 3682684a4784STudor Ambarus if (status < 0) { 3683684a4784STudor Ambarus pm_runtime_put_noidle(parent); 3684684a4784STudor Ambarus dev_err(&spi->controller->dev, "Failed to power device: %d\n", 3685684a4784STudor Ambarus status); 3686684a4784STudor Ambarus return status; 3687684a4784STudor Ambarus } 3688684a4784STudor Ambarus 3689684a4784STudor Ambarus status = spi->controller->set_cs_timing(spi); 3690684a4784STudor Ambarus pm_runtime_mark_last_busy(parent); 3691684a4784STudor Ambarus pm_runtime_put_autosuspend(parent); 3692684a4784STudor Ambarus } else { 3693684a4784STudor Ambarus status = spi->controller->set_cs_timing(spi); 3694684a4784STudor Ambarus } 3695684a4784STudor Ambarus } 3696684a4784STudor Ambarus return status; 3697684a4784STudor Ambarus } 3698684a4784STudor Ambarus 3699684a4784STudor Ambarus /** 37007d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 37017d077197SDavid Brownell * @spi: the device whose settings are being modified 37027d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 37037d077197SDavid Brownell * 37047d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 37057d077197SDavid Brownell * device doesn't work with its default. They may likewise need 37067d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 37077d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 37087d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 37097d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 3710702ca026SAndy Shevchenko * or from it. When this function returns, the SPI device is deselected. 37117d077197SDavid Brownell * 37127d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 37137d077197SDavid Brownell * that the underlying controller or its driver does not support. For 37147d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 37157d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 371697d56dc6SJavier Martinez Canillas * 371797d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 37187d077197SDavid Brownell */ 37197d077197SDavid Brownell int spi_setup(struct spi_device *spi) 37207d077197SDavid Brownell { 372183596fbeSGeert Uytterhoeven unsigned bad_bits, ugly_bits; 372273f93db5SPaul Kocialkowski int status = 0; 37237d077197SDavid Brownell 3724d962608cSDragos Bogdan /* 3725350de7ceSAndy Shevchenko * Check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO 3726350de7ceSAndy Shevchenko * are set at the same time. 3727f477b7fbSwangyuhang */ 3728d962608cSDragos Bogdan if ((hweight_long(spi->mode & 3729d962608cSDragos Bogdan (SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) || 3730d962608cSDragos Bogdan (hweight_long(spi->mode & 3731d962608cSDragos Bogdan (SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) { 3732f477b7fbSwangyuhang dev_err(&spi->dev, 3733d962608cSDragos Bogdan "setup: can not select any two of dual, quad and no-rx/tx at the same time\n"); 3734f477b7fbSwangyuhang return -EINVAL; 3735f477b7fbSwangyuhang } 3736350de7ceSAndy Shevchenko /* If it is SPI_3WIRE mode, DUAL and QUAD should be forbidden */ 3737f477b7fbSwangyuhang if ((spi->mode & SPI_3WIRE) && (spi->mode & 37386b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 37396b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 3740f477b7fbSwangyuhang return -EINVAL; 3741350de7ceSAndy Shevchenko /* 3742350de7ceSAndy Shevchenko * Help drivers fail *cleanly* when they need options 3743350de7ceSAndy Shevchenko * that aren't supported with their current controller. 3744cbaa62e0SDavid Lechner * SPI_CS_WORD has a fallback software implementation, 3745cbaa62e0SDavid Lechner * so it is ignored here. 3746e7db06b5SDavid Brownell */ 3747d962608cSDragos Bogdan bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | 3748d962608cSDragos Bogdan SPI_NO_TX | SPI_NO_RX); 374983596fbeSGeert Uytterhoeven ugly_bits = bad_bits & 37506b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 37516b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); 375283596fbeSGeert Uytterhoeven if (ugly_bits) { 375383596fbeSGeert Uytterhoeven dev_warn(&spi->dev, 375483596fbeSGeert Uytterhoeven "setup: ignoring unsupported mode bits %x\n", 375583596fbeSGeert Uytterhoeven ugly_bits); 375683596fbeSGeert Uytterhoeven spi->mode &= ~ugly_bits; 375783596fbeSGeert Uytterhoeven bad_bits &= ~ugly_bits; 375883596fbeSGeert Uytterhoeven } 3759e7db06b5SDavid Brownell if (bad_bits) { 3760eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 3761e7db06b5SDavid Brownell bad_bits); 3762e7db06b5SDavid Brownell return -EINVAL; 3763e7db06b5SDavid Brownell } 3764e7db06b5SDavid Brownell 3765b3fe2e51SPaul Kocialkowski if (!spi->bits_per_word) { 37667d077197SDavid Brownell spi->bits_per_word = 8; 3767b3fe2e51SPaul Kocialkowski } else { 3768b3fe2e51SPaul Kocialkowski /* 3769b3fe2e51SPaul Kocialkowski * Some controllers may not support the default 8 bits-per-word 3770b3fe2e51SPaul Kocialkowski * so only perform the check when this is explicitly provided. 3771b3fe2e51SPaul Kocialkowski */ 37728caab75fSGeert Uytterhoeven status = __spi_validate_bits_per_word(spi->controller, 37738caab75fSGeert Uytterhoeven spi->bits_per_word); 37745ab8d262SAndy Shevchenko if (status) 37755ab8d262SAndy Shevchenko return status; 3776b3fe2e51SPaul Kocialkowski } 377763ab645fSStefan Brüns 37786820e812STudor Ambarus if (spi->controller->max_speed_hz && 37796820e812STudor Ambarus (!spi->max_speed_hz || 37806820e812STudor Ambarus spi->max_speed_hz > spi->controller->max_speed_hz)) 37818caab75fSGeert Uytterhoeven spi->max_speed_hz = spi->controller->max_speed_hz; 3782052eb2d4SAxel Lin 37834fae3a58SSerge Semin mutex_lock(&spi->controller->io_mutex); 37844fae3a58SSerge Semin 3785c914dbf8SJoe Burmeister if (spi->controller->setup) { 37868caab75fSGeert Uytterhoeven status = spi->controller->setup(spi); 3787c914dbf8SJoe Burmeister if (status) { 3788c914dbf8SJoe Burmeister mutex_unlock(&spi->controller->io_mutex); 3789c914dbf8SJoe Burmeister dev_err(&spi->controller->dev, "Failed to setup device: %d\n", 3790c914dbf8SJoe Burmeister status); 3791c914dbf8SJoe Burmeister return status; 3792c914dbf8SJoe Burmeister } 3793c914dbf8SJoe Burmeister } 37947d077197SDavid Brownell 3795684a4784STudor Ambarus status = spi_set_cs_timing(spi); 3796684a4784STudor Ambarus if (status) { 3797684a4784STudor Ambarus mutex_unlock(&spi->controller->io_mutex); 3798684a4784STudor Ambarus return status; 3799684a4784STudor Ambarus } 3800684a4784STudor Ambarus 3801d948e6caSLuhua Xu if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { 3802dd769f15SMinghao Chi status = pm_runtime_resume_and_get(spi->controller->dev.parent); 3803d948e6caSLuhua Xu if (status < 0) { 38044fae3a58SSerge Semin mutex_unlock(&spi->controller->io_mutex); 3805d948e6caSLuhua Xu dev_err(&spi->controller->dev, "Failed to power device: %d\n", 3806d948e6caSLuhua Xu status); 3807d948e6caSLuhua Xu return status; 3808d948e6caSLuhua Xu } 380957a94607STony Lindgren 381057a94607STony Lindgren /* 381157a94607STony Lindgren * We do not want to return positive value from pm_runtime_get, 381257a94607STony Lindgren * there are many instances of devices calling spi_setup() and 381357a94607STony Lindgren * checking for a non-zero return value instead of a negative 381457a94607STony Lindgren * return value. 381557a94607STony Lindgren */ 381657a94607STony Lindgren status = 0; 381757a94607STony Lindgren 3818d347b4aaSDavid Bauer spi_set_cs(spi, false, true); 3819d948e6caSLuhua Xu pm_runtime_mark_last_busy(spi->controller->dev.parent); 3820d948e6caSLuhua Xu pm_runtime_put_autosuspend(spi->controller->dev.parent); 3821d948e6caSLuhua Xu } else { 3822d347b4aaSDavid Bauer spi_set_cs(spi, false, true); 3823d948e6caSLuhua Xu } 3824abeedb01SFranklin S Cooper Jr 38254fae3a58SSerge Semin mutex_unlock(&spi->controller->io_mutex); 38264fae3a58SSerge Semin 3827924b5867SDouglas Anderson if (spi->rt && !spi->controller->rt) { 3828924b5867SDouglas Anderson spi->controller->rt = true; 3829924b5867SDouglas Anderson spi_set_thread_rt(spi->controller); 3830924b5867SDouglas Anderson } 3831924b5867SDouglas Anderson 38325cb4e1f3SAndy Shevchenko trace_spi_setup(spi, status); 38335cb4e1f3SAndy Shevchenko 383440b82c2dSAndy Shevchenko dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 383540b82c2dSAndy Shevchenko spi->mode & SPI_MODE_X_MASK, 38367d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 38377d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 38387d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 38397d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 38407d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 38417d077197SDavid Brownell status); 38427d077197SDavid Brownell 38437d077197SDavid Brownell return status; 38447d077197SDavid Brownell } 38457d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 38467d077197SDavid Brownell 38476c613f68SAlexandru Ardelean static int _spi_xfer_word_delay_update(struct spi_transfer *xfer, 38486c613f68SAlexandru Ardelean struct spi_device *spi) 38496c613f68SAlexandru Ardelean { 38506c613f68SAlexandru Ardelean int delay1, delay2; 38516c613f68SAlexandru Ardelean 38523984d39bSAlexandru Ardelean delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); 38536c613f68SAlexandru Ardelean if (delay1 < 0) 38546c613f68SAlexandru Ardelean return delay1; 38556c613f68SAlexandru Ardelean 38563984d39bSAlexandru Ardelean delay2 = spi_delay_to_ns(&spi->word_delay, xfer); 38576c613f68SAlexandru Ardelean if (delay2 < 0) 38586c613f68SAlexandru Ardelean return delay2; 38596c613f68SAlexandru Ardelean 38606c613f68SAlexandru Ardelean if (delay1 < delay2) 38616c613f68SAlexandru Ardelean memcpy(&xfer->word_delay, &spi->word_delay, 38626c613f68SAlexandru Ardelean sizeof(xfer->word_delay)); 38636c613f68SAlexandru Ardelean 38646c613f68SAlexandru Ardelean return 0; 38656c613f68SAlexandru Ardelean } 38666c613f68SAlexandru Ardelean 386790808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message) 3868cf32b71eSErnst Schwab { 38698caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3870e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 38716ea31293SAtsushi Nemoto int w_size; 3872cf32b71eSErnst Schwab 387324a0013aSMark Brown if (list_empty(&message->transfers)) 387424a0013aSMark Brown return -EINVAL; 387524a0013aSMark Brown 3876350de7ceSAndy Shevchenko /* 3877350de7ceSAndy Shevchenko * If an SPI controller does not support toggling the CS line on each 387871388b21SDavid Lechner * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO 387971388b21SDavid Lechner * for the CS line, we can emulate the CS-per-word hardware function by 3880cbaa62e0SDavid Lechner * splitting transfers into one-word transfers and ensuring that 3881cbaa62e0SDavid Lechner * cs_change is set for each transfer. 3882cbaa62e0SDavid Lechner */ 388371388b21SDavid Lechner if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || 3884303feb3cSAmit Kumar Mahapatra spi_get_csgpiod(spi, 0))) { 3885cbaa62e0SDavid Lechner size_t maxsize; 3886cbaa62e0SDavid Lechner int ret; 3887cbaa62e0SDavid Lechner 3888cbaa62e0SDavid Lechner maxsize = (spi->bits_per_word + 7) / 8; 3889cbaa62e0SDavid Lechner 3890cbaa62e0SDavid Lechner /* spi_split_transfers_maxsize() requires message->spi */ 3891cbaa62e0SDavid Lechner message->spi = spi; 3892cbaa62e0SDavid Lechner 3893cbaa62e0SDavid Lechner ret = spi_split_transfers_maxsize(ctlr, message, maxsize, 3894cbaa62e0SDavid Lechner GFP_KERNEL); 3895cbaa62e0SDavid Lechner if (ret) 3896cbaa62e0SDavid Lechner return ret; 3897cbaa62e0SDavid Lechner 3898cbaa62e0SDavid Lechner list_for_each_entry(xfer, &message->transfers, transfer_list) { 389995c8222fSDavid Jander /* Don't change cs_change on the last entry in the list */ 3900cbaa62e0SDavid Lechner if (list_is_last(&xfer->transfer_list, &message->transfers)) 3901cbaa62e0SDavid Lechner break; 3902cbaa62e0SDavid Lechner xfer->cs_change = 1; 3903cbaa62e0SDavid Lechner } 3904cbaa62e0SDavid Lechner } 3905cbaa62e0SDavid Lechner 3906350de7ceSAndy Shevchenko /* 3907350de7ceSAndy Shevchenko * Half-duplex links include original MicroWire, and ones with 3908cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 3909cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 3910cf32b71eSErnst Schwab * software limitations. 3911cf32b71eSErnst Schwab */ 39128caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 39138caab75fSGeert Uytterhoeven (spi->mode & SPI_3WIRE)) { 39148caab75fSGeert Uytterhoeven unsigned flags = ctlr->flags; 3915cf32b71eSErnst Schwab 3916cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 3917cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 3918cf32b71eSErnst Schwab return -EINVAL; 39198caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 3920cf32b71eSErnst Schwab return -EINVAL; 39218caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 3922cf32b71eSErnst Schwab return -EINVAL; 3923cf32b71eSErnst Schwab } 3924cf32b71eSErnst Schwab } 3925cf32b71eSErnst Schwab 3926350de7ceSAndy Shevchenko /* 3927059b8ffeSLaxman Dewangan * Set transfer bits_per_word and max speed as spi device default if 3928059b8ffeSLaxman Dewangan * it is not set for this transfer. 3929f477b7fbSwangyuhang * Set transfer tx_nbits and rx_nbits as single transfer default 3930f477b7fbSwangyuhang * (SPI_NBITS_SINGLE) if it is not set for this transfer. 3931b7bb367aSJonas Bonn * Ensure transfer word_delay is at least as long as that required by 3932b7bb367aSJonas Bonn * device itself. 3933e6811d1dSLaxman Dewangan */ 393477e80588SMartin Sperl message->frame_length = 0; 3935e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 39365d7e2b5eSMartin Sperl xfer->effective_speed_hz = 0; 3937078726ceSSourav Poddar message->frame_length += xfer->len; 3938e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 3939e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 3940a6f87fadSAxel Lin 3941a6f87fadSAxel Lin if (!xfer->speed_hz) 3942059b8ffeSLaxman Dewangan xfer->speed_hz = spi->max_speed_hz; 3943a6f87fadSAxel Lin 39448caab75fSGeert Uytterhoeven if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 39458caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 394656ede94aSGabor Juhos 39478caab75fSGeert Uytterhoeven if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 3948543bb255SStephen Warren return -EINVAL; 3949a2fd4f9fSMark Brown 39504d94bd21SIvan T. Ivanov /* 39514d94bd21SIvan T. Ivanov * SPI transfer length should be multiple of SPI word size 3952350de7ceSAndy Shevchenko * where SPI word size should be power-of-two multiple. 39534d94bd21SIvan T. Ivanov */ 39544d94bd21SIvan T. Ivanov if (xfer->bits_per_word <= 8) 39554d94bd21SIvan T. Ivanov w_size = 1; 39564d94bd21SIvan T. Ivanov else if (xfer->bits_per_word <= 16) 39574d94bd21SIvan T. Ivanov w_size = 2; 39584d94bd21SIvan T. Ivanov else 39594d94bd21SIvan T. Ivanov w_size = 4; 39604d94bd21SIvan T. Ivanov 39614d94bd21SIvan T. Ivanov /* No partial transfers accepted */ 39626ea31293SAtsushi Nemoto if (xfer->len % w_size) 39634d94bd21SIvan T. Ivanov return -EINVAL; 39644d94bd21SIvan T. Ivanov 39658caab75fSGeert Uytterhoeven if (xfer->speed_hz && ctlr->min_speed_hz && 39668caab75fSGeert Uytterhoeven xfer->speed_hz < ctlr->min_speed_hz) 3967a2fd4f9fSMark Brown return -EINVAL; 3968f477b7fbSwangyuhang 3969f477b7fbSwangyuhang if (xfer->tx_buf && !xfer->tx_nbits) 3970f477b7fbSwangyuhang xfer->tx_nbits = SPI_NBITS_SINGLE; 3971f477b7fbSwangyuhang if (xfer->rx_buf && !xfer->rx_nbits) 3972f477b7fbSwangyuhang xfer->rx_nbits = SPI_NBITS_SINGLE; 3973350de7ceSAndy Shevchenko /* 3974350de7ceSAndy Shevchenko * Check transfer tx/rx_nbits: 39751afd9989SGeert Uytterhoeven * 1. check the value matches one of single, dual and quad 39761afd9989SGeert Uytterhoeven * 2. check tx/rx_nbits match the mode in spi_device 3977f477b7fbSwangyuhang */ 3978db90a441SSourav Poddar if (xfer->tx_buf) { 3979d962608cSDragos Bogdan if (spi->mode & SPI_NO_TX) 3980d962608cSDragos Bogdan return -EINVAL; 3981f477b7fbSwangyuhang if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3982f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_DUAL && 3983f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_QUAD) 3984a2fd4f9fSMark Brown return -EINVAL; 3985f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 3986f477b7fbSwangyuhang !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3987f477b7fbSwangyuhang return -EINVAL; 3988f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 3989f477b7fbSwangyuhang !(spi->mode & SPI_TX_QUAD)) 3990f477b7fbSwangyuhang return -EINVAL; 3991db90a441SSourav Poddar } 399295c8222fSDavid Jander /* Check transfer rx_nbits */ 3993db90a441SSourav Poddar if (xfer->rx_buf) { 3994d962608cSDragos Bogdan if (spi->mode & SPI_NO_RX) 3995d962608cSDragos Bogdan return -EINVAL; 3996f477b7fbSwangyuhang if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3997f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_DUAL && 3998f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_QUAD) 3999f477b7fbSwangyuhang return -EINVAL; 4000f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 4001f477b7fbSwangyuhang !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 4002f477b7fbSwangyuhang return -EINVAL; 4003f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 4004f477b7fbSwangyuhang !(spi->mode & SPI_RX_QUAD)) 4005f477b7fbSwangyuhang return -EINVAL; 4006e6811d1dSLaxman Dewangan } 4007b7bb367aSJonas Bonn 40086c613f68SAlexandru Ardelean if (_spi_xfer_word_delay_update(xfer, spi)) 40096c613f68SAlexandru Ardelean return -EINVAL; 4010e6811d1dSLaxman Dewangan } 4011e6811d1dSLaxman Dewangan 4012cf32b71eSErnst Schwab message->status = -EINPROGRESS; 401390808738SMark Brown 401490808738SMark Brown return 0; 401590808738SMark Brown } 401690808738SMark Brown 401790808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message) 401890808738SMark Brown { 40198caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 4020b42faeeeSVladimir Oltean struct spi_transfer *xfer; 402190808738SMark Brown 4022b5932f5cSBoris Brezillon /* 4023b5932f5cSBoris Brezillon * Some controllers do not support doing regular SPI transfers. Return 4024b5932f5cSBoris Brezillon * ENOTSUPP when this is the case. 4025b5932f5cSBoris Brezillon */ 4026b5932f5cSBoris Brezillon if (!ctlr->transfer) 4027b5932f5cSBoris Brezillon return -ENOTSUPP; 4028b5932f5cSBoris Brezillon 402990808738SMark Brown message->spi = spi; 403090808738SMark Brown 40316598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_async); 40326598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async); 4033eca2ebc7SMartin Sperl 403490808738SMark Brown trace_spi_message_submit(message); 403590808738SMark Brown 4036b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 4037b42faeeeSVladimir Oltean list_for_each_entry(xfer, &message->transfers, transfer_list) { 4038b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 4039b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 4040b42faeeeSVladimir Oltean } 4041b42faeeeSVladimir Oltean } 4042b42faeeeSVladimir Oltean 40438caab75fSGeert Uytterhoeven return ctlr->transfer(spi, message); 4044cf32b71eSErnst Schwab } 4045cf32b71eSErnst Schwab 4046568d0697SDavid Brownell /** 4047568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 4048568d0697SDavid Brownell * @spi: device with which data will be exchanged 4049568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 4050702ca026SAndy Shevchenko * Context: any (IRQs may be blocked, etc) 4051568d0697SDavid Brownell * 4052568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 4053568d0697SDavid Brownell * as well as from task contexts which can sleep. 4054568d0697SDavid Brownell * 4055568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 4056568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 4057568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 4058568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 4059568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 4060568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 4061568d0697SDavid Brownell * core or controller driver code. 4062568d0697SDavid Brownell * 4063568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 4064568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 4065568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 4066568d0697SDavid Brownell * time requirements, for example. 4067568d0697SDavid Brownell * 4068568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 4069568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 4070568d0697SDavid Brownell * Until returning from the associated message completion callback, 4071568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 4072568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 4073568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 407497d56dc6SJavier Martinez Canillas * 407597d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 4076568d0697SDavid Brownell */ 4077568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 4078568d0697SDavid Brownell { 40798caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 4080cf32b71eSErnst Schwab int ret; 4081cf32b71eSErnst Schwab unsigned long flags; 4082568d0697SDavid Brownell 408390808738SMark Brown ret = __spi_validate(spi, message); 408490808738SMark Brown if (ret != 0) 408590808738SMark Brown return ret; 408690808738SMark Brown 40878caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 4088568d0697SDavid Brownell 40898caab75fSGeert Uytterhoeven if (ctlr->bus_lock_flag) 4090cf32b71eSErnst Schwab ret = -EBUSY; 4091cf32b71eSErnst Schwab else 4092cf32b71eSErnst Schwab ret = __spi_async(spi, message); 4093568d0697SDavid Brownell 40948caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 4095cf32b71eSErnst Schwab 4096cf32b71eSErnst Schwab return ret; 4097568d0697SDavid Brownell } 4098568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 4099568d0697SDavid Brownell 4100cf32b71eSErnst Schwab /** 4101cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 4102cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 4103cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 4104702ca026SAndy Shevchenko * Context: any (IRQs may be blocked, etc) 4105cf32b71eSErnst Schwab * 4106cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 4107cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 4108cf32b71eSErnst Schwab * 4109cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 4110cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 4111cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 4112cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 4113cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 4114cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 4115cf32b71eSErnst Schwab * core or controller driver code. 4116cf32b71eSErnst Schwab * 4117cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 4118cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 4119cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 4120cf32b71eSErnst Schwab * time requirements, for example. 4121cf32b71eSErnst Schwab * 4122cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 4123cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 4124cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 4125cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 4126cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 4127cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 412897d56dc6SJavier Martinez Canillas * 412997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 4130cf32b71eSErnst Schwab */ 4131da21fde0SUwe Kleine-König static int spi_async_locked(struct spi_device *spi, struct spi_message *message) 4132cf32b71eSErnst Schwab { 41338caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 4134cf32b71eSErnst Schwab int ret; 4135cf32b71eSErnst Schwab unsigned long flags; 4136cf32b71eSErnst Schwab 413790808738SMark Brown ret = __spi_validate(spi, message); 413890808738SMark Brown if (ret != 0) 413990808738SMark Brown return ret; 414090808738SMark Brown 41418caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 4142cf32b71eSErnst Schwab 4143cf32b71eSErnst Schwab ret = __spi_async(spi, message); 4144cf32b71eSErnst Schwab 41458caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 4146cf32b71eSErnst Schwab 4147cf32b71eSErnst Schwab return ret; 4148cf32b71eSErnst Schwab 4149cf32b71eSErnst Schwab } 4150cf32b71eSErnst Schwab 4151ae7d2346SDavid Jander static void __spi_transfer_message_noqueue(struct spi_controller *ctlr, struct spi_message *msg) 4152ae7d2346SDavid Jander { 4153ae7d2346SDavid Jander bool was_busy; 4154ae7d2346SDavid Jander int ret; 4155ae7d2346SDavid Jander 4156ae7d2346SDavid Jander mutex_lock(&ctlr->io_mutex); 4157ae7d2346SDavid Jander 41581a9cafcbSDavid Jander was_busy = ctlr->busy; 4159ae7d2346SDavid Jander 416072c5c59bSDavid Jander ctlr->cur_msg = msg; 4161ae7d2346SDavid Jander ret = __spi_pump_transfer_message(ctlr, msg, was_busy); 4162ae7d2346SDavid Jander if (ret) 4163ae7d2346SDavid Jander goto out; 4164ae7d2346SDavid Jander 416569fa9590SDavid Jander ctlr->cur_msg = NULL; 416669fa9590SDavid Jander ctlr->fallback = false; 416769fa9590SDavid Jander 4168ae7d2346SDavid Jander if (!was_busy) { 4169ae7d2346SDavid Jander kfree(ctlr->dummy_rx); 4170ae7d2346SDavid Jander ctlr->dummy_rx = NULL; 4171ae7d2346SDavid Jander kfree(ctlr->dummy_tx); 4172ae7d2346SDavid Jander ctlr->dummy_tx = NULL; 4173ae7d2346SDavid Jander if (ctlr->unprepare_transfer_hardware && 4174ae7d2346SDavid Jander ctlr->unprepare_transfer_hardware(ctlr)) 4175ae7d2346SDavid Jander dev_err(&ctlr->dev, 4176ae7d2346SDavid Jander "failed to unprepare transfer hardware\n"); 4177ae7d2346SDavid Jander spi_idle_runtime_pm(ctlr); 4178ae7d2346SDavid Jander } 4179ae7d2346SDavid Jander 4180ae7d2346SDavid Jander out: 4181ae7d2346SDavid Jander mutex_unlock(&ctlr->io_mutex); 4182ae7d2346SDavid Jander } 4183ae7d2346SDavid Jander 41847d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 41857d077197SDavid Brownell 4186350de7ceSAndy Shevchenko /* 4187350de7ceSAndy Shevchenko * Utility methods for SPI protocol drivers, layered on 41887d077197SDavid Brownell * top of the core. Some other utility methods are defined as 41897d077197SDavid Brownell * inline functions. 41907d077197SDavid Brownell */ 41917d077197SDavid Brownell 41925d870c8eSAndrew Morton static void spi_complete(void *arg) 41935d870c8eSAndrew Morton { 41945d870c8eSAndrew Morton complete(arg); 41955d870c8eSAndrew Morton } 41965d870c8eSAndrew Morton 4197ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message) 4198cf32b71eSErnst Schwab { 4199cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 4200cf32b71eSErnst Schwab int status; 42018caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 42020461a414SMark Brown 42030461a414SMark Brown status = __spi_validate(spi, message); 42040461a414SMark Brown if (status != 0) 42050461a414SMark Brown return status; 4206cf32b71eSErnst Schwab 42070461a414SMark Brown message->spi = spi; 4208cf32b71eSErnst Schwab 42096598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync); 42106598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); 4211eca2ebc7SMartin Sperl 4212350de7ceSAndy Shevchenko /* 4213ae7d2346SDavid Jander * Checking queue_empty here only guarantees async/sync message 4214ae7d2346SDavid Jander * ordering when coming from the same context. It does not need to 4215ae7d2346SDavid Jander * guard against reentrancy from a different context. The io_mutex 4216ae7d2346SDavid Jander * will catch those cases. 42170461a414SMark Brown */ 4218b30f7c8eSMark Brown if (READ_ONCE(ctlr->queue_empty) && !ctlr->must_async) { 4219ae7d2346SDavid Jander message->actual_length = 0; 4220ae7d2346SDavid Jander message->status = -EINPROGRESS; 42210461a414SMark Brown 42220461a414SMark Brown trace_spi_message_submit(message); 42230461a414SMark Brown 4224ae7d2346SDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync_immediate); 4225ae7d2346SDavid Jander SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate); 42260461a414SMark Brown 4227ae7d2346SDavid Jander __spi_transfer_message_noqueue(ctlr, message); 4228ae7d2346SDavid Jander 4229ae7d2346SDavid Jander return message->status; 4230ae7d2346SDavid Jander } 4231ae7d2346SDavid Jander 4232ae7d2346SDavid Jander /* 4233ae7d2346SDavid Jander * There are messages in the async queue that could have originated 4234ae7d2346SDavid Jander * from the same context, so we need to preserve ordering. 4235ae7d2346SDavid Jander * Therefor we send the message to the async queue and wait until they 4236ae7d2346SDavid Jander * are completed. 4237ae7d2346SDavid Jander */ 4238ae7d2346SDavid Jander message->complete = spi_complete; 4239ae7d2346SDavid Jander message->context = &done; 4240cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 4241cf32b71eSErnst Schwab if (status == 0) { 4242cf32b71eSErnst Schwab wait_for_completion(&done); 4243cf32b71eSErnst Schwab status = message->status; 4244cf32b71eSErnst Schwab } 4245cf32b71eSErnst Schwab message->context = NULL; 4246ae7d2346SDavid Jander 4247cf32b71eSErnst Schwab return status; 4248cf32b71eSErnst Schwab } 4249cf32b71eSErnst Schwab 42508ae12a0dSDavid Brownell /** 42518ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 42528ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 42538ae12a0dSDavid Brownell * @message: describes the data transfers 425433e34dc6SDavid Brownell * Context: can sleep 42558ae12a0dSDavid Brownell * 42568ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 42578ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 42588ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 42598ae12a0dSDavid Brownell * 42608ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 42618ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 42628ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 42638ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 42648ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 42658ae12a0dSDavid Brownell * 42660c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 42670c868461SDavid Brownell * message will not be freed before this call returns. 42680c868461SDavid Brownell * 426997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 42708ae12a0dSDavid Brownell */ 42718ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 42728ae12a0dSDavid Brownell { 4273ef4d96ecSMark Brown int ret; 4274ef4d96ecSMark Brown 42758caab75fSGeert Uytterhoeven mutex_lock(&spi->controller->bus_lock_mutex); 4276ef4d96ecSMark Brown ret = __spi_sync(spi, message); 42778caab75fSGeert Uytterhoeven mutex_unlock(&spi->controller->bus_lock_mutex); 4278ef4d96ecSMark Brown 4279ef4d96ecSMark Brown return ret; 42808ae12a0dSDavid Brownell } 42818ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 42828ae12a0dSDavid Brownell 4283cf32b71eSErnst Schwab /** 4284cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 4285cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 4286cf32b71eSErnst Schwab * @message: describes the data transfers 4287cf32b71eSErnst Schwab * Context: can sleep 4288cf32b71eSErnst Schwab * 4289cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 4290cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 4291cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 4292cf32b71eSErnst Schwab * 4293cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 429425985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 4295cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 4296cf32b71eSErnst Schwab * 429797d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 4298cf32b71eSErnst Schwab */ 4299cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 4300cf32b71eSErnst Schwab { 4301ef4d96ecSMark Brown return __spi_sync(spi, message); 4302cf32b71eSErnst Schwab } 4303cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 4304cf32b71eSErnst Schwab 4305cf32b71eSErnst Schwab /** 4306cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 43078caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that should be locked for exclusive bus access 4308cf32b71eSErnst Schwab * Context: can sleep 4309cf32b71eSErnst Schwab * 4310cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 4311cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 4312cf32b71eSErnst Schwab * 4313cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 4314cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 4315cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 4316cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 4317cf32b71eSErnst Schwab * 431897d56dc6SJavier Martinez Canillas * Return: always zero. 4319cf32b71eSErnst Schwab */ 43208caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr) 4321cf32b71eSErnst Schwab { 4322cf32b71eSErnst Schwab unsigned long flags; 4323cf32b71eSErnst Schwab 43248caab75fSGeert Uytterhoeven mutex_lock(&ctlr->bus_lock_mutex); 4325cf32b71eSErnst Schwab 43268caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 43278caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 1; 43288caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 4329cf32b71eSErnst Schwab 433095c8222fSDavid Jander /* Mutex remains locked until spi_bus_unlock() is called */ 4331cf32b71eSErnst Schwab 4332cf32b71eSErnst Schwab return 0; 4333cf32b71eSErnst Schwab } 4334cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 4335cf32b71eSErnst Schwab 4336cf32b71eSErnst Schwab /** 4337cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 43388caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that was locked for exclusive bus access 4339cf32b71eSErnst Schwab * Context: can sleep 4340cf32b71eSErnst Schwab * 4341cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 4342cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 4343cf32b71eSErnst Schwab * 4344cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 4345cf32b71eSErnst Schwab * call. 4346cf32b71eSErnst Schwab * 434797d56dc6SJavier Martinez Canillas * Return: always zero. 4348cf32b71eSErnst Schwab */ 43498caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr) 4350cf32b71eSErnst Schwab { 43518caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 4352cf32b71eSErnst Schwab 43538caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->bus_lock_mutex); 4354cf32b71eSErnst Schwab 4355cf32b71eSErnst Schwab return 0; 4356cf32b71eSErnst Schwab } 4357cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 4358cf32b71eSErnst Schwab 435995c8222fSDavid Jander /* Portable code must never pass more than 32 bytes */ 4360a9948b61SDavid Brownell #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 43618ae12a0dSDavid Brownell 43628ae12a0dSDavid Brownell static u8 *buf; 43638ae12a0dSDavid Brownell 43648ae12a0dSDavid Brownell /** 43658ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 43668ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 4367702ca026SAndy Shevchenko * @txbuf: data to be written (need not be DMA-safe) 43688ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 4369702ca026SAndy Shevchenko * @rxbuf: buffer into which data will be read (need not be DMA-safe) 437027570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 437133e34dc6SDavid Brownell * Context: can sleep 43728ae12a0dSDavid Brownell * 43738ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 43748ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 43758ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 4376b885244eSDavid Brownell * This call may only be used from a context that may sleep. 43778ae12a0dSDavid Brownell * 4378c373643bSMark Brown * Parameters to this routine are always copied using a small buffer. 437933e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 4380702ca026SAndy Shevchenko * spi_{async,sync}() calls with DMA-safe buffers. 438197d56dc6SJavier Martinez Canillas * 438297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 43838ae12a0dSDavid Brownell */ 43848ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 43850c4a1590SMark Brown const void *txbuf, unsigned n_tx, 43860c4a1590SMark Brown void *rxbuf, unsigned n_rx) 43878ae12a0dSDavid Brownell { 4388068f4070SDavid Brownell static DEFINE_MUTEX(lock); 43898ae12a0dSDavid Brownell 43908ae12a0dSDavid Brownell int status; 43918ae12a0dSDavid Brownell struct spi_message message; 4392bdff549eSDavid Brownell struct spi_transfer x[2]; 43938ae12a0dSDavid Brownell u8 *local_buf; 43948ae12a0dSDavid Brownell 4395350de7ceSAndy Shevchenko /* 4396350de7ceSAndy Shevchenko * Use preallocated DMA-safe buffer if we can. We can't avoid 4397b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 4398b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 4399b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 44008ae12a0dSDavid Brownell */ 4401b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 44022cd94c8aSMark Brown local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 44032cd94c8aSMark Brown GFP_KERNEL | GFP_DMA); 4404b3a223eeSMark Brown if (!local_buf) 4405b3a223eeSMark Brown return -ENOMEM; 4406b3a223eeSMark Brown } else { 4407b3a223eeSMark Brown local_buf = buf; 4408b3a223eeSMark Brown } 44098ae12a0dSDavid Brownell 44108275c642SVitaly Wool spi_message_init(&message); 44115fe5f05eSJingoo Han memset(x, 0, sizeof(x)); 4412bdff549eSDavid Brownell if (n_tx) { 4413bdff549eSDavid Brownell x[0].len = n_tx; 4414bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 4415bdff549eSDavid Brownell } 4416bdff549eSDavid Brownell if (n_rx) { 4417bdff549eSDavid Brownell x[1].len = n_rx; 4418bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 4419bdff549eSDavid Brownell } 44208275c642SVitaly Wool 44218ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 4422bdff549eSDavid Brownell x[0].tx_buf = local_buf; 4423bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 44248ae12a0dSDavid Brownell 4425702ca026SAndy Shevchenko /* Do the I/O */ 44268ae12a0dSDavid Brownell status = spi_sync(spi, &message); 44279b938b74SMarc Pignat if (status == 0) 4428bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 44298ae12a0dSDavid Brownell 4430bdff549eSDavid Brownell if (x[0].tx_buf == buf) 4431068f4070SDavid Brownell mutex_unlock(&lock); 44328ae12a0dSDavid Brownell else 44338ae12a0dSDavid Brownell kfree(local_buf); 44348ae12a0dSDavid Brownell 44358ae12a0dSDavid Brownell return status; 44368ae12a0dSDavid Brownell } 44378ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 44388ae12a0dSDavid Brownell 44398ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 44408ae12a0dSDavid Brownell 4441da21fde0SUwe Kleine-König #if IS_ENABLED(CONFIG_OF_DYNAMIC) 444295c8222fSDavid Jander /* Must call put_device() when done with returned spi_device device */ 4443da21fde0SUwe Kleine-König static struct spi_device *of_find_spi_device_by_node(struct device_node *node) 4444ce79d54aSPantelis Antoniou { 4445cfba5de9SSuzuki K Poulose struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); 4446cfba5de9SSuzuki K Poulose 4447ce79d54aSPantelis Antoniou return dev ? to_spi_device(dev) : NULL; 4448ce79d54aSPantelis Antoniou } 4449ce79d54aSPantelis Antoniou 445095c8222fSDavid Jander /* The spi controllers are not using spi_bus, so we find it with another way */ 44518caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 4452ce79d54aSPantelis Antoniou { 4453ce79d54aSPantelis Antoniou struct device *dev; 4454ce79d54aSPantelis Antoniou 4455cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_master_class, node); 44566c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 4457cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_slave_class, node); 4458ce79d54aSPantelis Antoniou if (!dev) 4459ce79d54aSPantelis Antoniou return NULL; 4460ce79d54aSPantelis Antoniou 446195c8222fSDavid Jander /* Reference got in class_find_device */ 44628caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 4463ce79d54aSPantelis Antoniou } 4464ce79d54aSPantelis Antoniou 4465ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action, 4466ce79d54aSPantelis Antoniou void *arg) 4467ce79d54aSPantelis Antoniou { 4468ce79d54aSPantelis Antoniou struct of_reconfig_data *rd = arg; 44698caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 4470ce79d54aSPantelis Antoniou struct spi_device *spi; 4471ce79d54aSPantelis Antoniou 4472ce79d54aSPantelis Antoniou switch (of_reconfig_get_state_change(action, arg)) { 4473ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 44748caab75fSGeert Uytterhoeven ctlr = of_find_spi_controller_by_node(rd->dn->parent); 44758caab75fSGeert Uytterhoeven if (ctlr == NULL) 447695c8222fSDavid Jander return NOTIFY_OK; /* Not for us */ 4477ce79d54aSPantelis Antoniou 4478bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 44798caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4480bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4481bd6c1644SGeert Uytterhoeven } 4482bd6c1644SGeert Uytterhoeven 44831a50d940SGeert Uytterhoeven /* 44841a50d940SGeert Uytterhoeven * Clear the flag before adding the device so that fw_devlink 44851a50d940SGeert Uytterhoeven * doesn't skip adding consumers to this device. 44861a50d940SGeert Uytterhoeven */ 44871a50d940SGeert Uytterhoeven rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE; 44888caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, rd->dn); 44898caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4490ce79d54aSPantelis Antoniou 4491ce79d54aSPantelis Antoniou if (IS_ERR(spi)) { 449225c56c88SRob Herring pr_err("%s: failed to create for '%pOF'\n", 449325c56c88SRob Herring __func__, rd->dn); 4494e0af98a7SRalf Ramsauer of_node_clear_flag(rd->dn, OF_POPULATED); 4495ce79d54aSPantelis Antoniou return notifier_from_errno(PTR_ERR(spi)); 4496ce79d54aSPantelis Antoniou } 4497ce79d54aSPantelis Antoniou break; 4498ce79d54aSPantelis Antoniou 4499ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 450095c8222fSDavid Jander /* Already depopulated? */ 4501bd6c1644SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 4502bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4503bd6c1644SGeert Uytterhoeven 450495c8222fSDavid Jander /* Find our device by node */ 4505ce79d54aSPantelis Antoniou spi = of_find_spi_device_by_node(rd->dn); 4506ce79d54aSPantelis Antoniou if (spi == NULL) 450795c8222fSDavid Jander return NOTIFY_OK; /* No? not meant for us */ 4508ce79d54aSPantelis Antoniou 450995c8222fSDavid Jander /* Unregister takes one ref away */ 4510ce79d54aSPantelis Antoniou spi_unregister_device(spi); 4511ce79d54aSPantelis Antoniou 451295c8222fSDavid Jander /* And put the reference of the find */ 4513ce79d54aSPantelis Antoniou put_device(&spi->dev); 4514ce79d54aSPantelis Antoniou break; 4515ce79d54aSPantelis Antoniou } 4516ce79d54aSPantelis Antoniou 4517ce79d54aSPantelis Antoniou return NOTIFY_OK; 4518ce79d54aSPantelis Antoniou } 4519ce79d54aSPantelis Antoniou 4520ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = { 4521ce79d54aSPantelis Antoniou .notifier_call = of_spi_notify, 4522ce79d54aSPantelis Antoniou }; 4523ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4524ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier; 4525ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4526ce79d54aSPantelis Antoniou 45277f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI) 45288caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data) 45297f24467fSOctavian Purdila { 45307f24467fSOctavian Purdila return ACPI_COMPANION(dev->parent) == data; 45317f24467fSOctavian Purdila } 45327f24467fSOctavian Purdila 45338caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 45347f24467fSOctavian Purdila { 45357f24467fSOctavian Purdila struct device *dev; 45367f24467fSOctavian Purdila 45377f24467fSOctavian Purdila dev = class_find_device(&spi_master_class, NULL, adev, 45388caab75fSGeert Uytterhoeven spi_acpi_controller_match); 45396c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 45406c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, adev, 45418caab75fSGeert Uytterhoeven spi_acpi_controller_match); 45427f24467fSOctavian Purdila if (!dev) 45437f24467fSOctavian Purdila return NULL; 45447f24467fSOctavian Purdila 45458caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 45467f24467fSOctavian Purdila } 45477f24467fSOctavian Purdila 45487f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 45497f24467fSOctavian Purdila { 45507f24467fSOctavian Purdila struct device *dev; 45517f24467fSOctavian Purdila 455200500147SSuzuki K Poulose dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev); 45535b16668eSWolfram Sang return to_spi_device(dev); 45547f24467fSOctavian Purdila } 45557f24467fSOctavian Purdila 45567f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 45577f24467fSOctavian Purdila void *arg) 45587f24467fSOctavian Purdila { 45597f24467fSOctavian Purdila struct acpi_device *adev = arg; 45608caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 45617f24467fSOctavian Purdila struct spi_device *spi; 45627f24467fSOctavian Purdila 45637f24467fSOctavian Purdila switch (value) { 45647f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_ADD: 456562fcb99bSRafael J. Wysocki ctlr = acpi_spi_find_controller_by_adev(acpi_dev_parent(adev)); 45668caab75fSGeert Uytterhoeven if (!ctlr) 45677f24467fSOctavian Purdila break; 45687f24467fSOctavian Purdila 45698caab75fSGeert Uytterhoeven acpi_register_spi_device(ctlr, adev); 45708caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 45717f24467fSOctavian Purdila break; 45727f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_REMOVE: 45737f24467fSOctavian Purdila if (!acpi_device_enumerated(adev)) 45747f24467fSOctavian Purdila break; 45757f24467fSOctavian Purdila 45767f24467fSOctavian Purdila spi = acpi_spi_find_device_by_adev(adev); 45777f24467fSOctavian Purdila if (!spi) 45787f24467fSOctavian Purdila break; 45797f24467fSOctavian Purdila 45807f24467fSOctavian Purdila spi_unregister_device(spi); 45817f24467fSOctavian Purdila put_device(&spi->dev); 45827f24467fSOctavian Purdila break; 45837f24467fSOctavian Purdila } 45847f24467fSOctavian Purdila 45857f24467fSOctavian Purdila return NOTIFY_OK; 45867f24467fSOctavian Purdila } 45877f24467fSOctavian Purdila 45887f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = { 45897f24467fSOctavian Purdila .notifier_call = acpi_spi_notify, 45907f24467fSOctavian Purdila }; 45917f24467fSOctavian Purdila #else 45927f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier; 45937f24467fSOctavian Purdila #endif 45947f24467fSOctavian Purdila 45958ae12a0dSDavid Brownell static int __init spi_init(void) 45968ae12a0dSDavid Brownell { 4597b885244eSDavid Brownell int status; 45988ae12a0dSDavid Brownell 4599e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 4600b885244eSDavid Brownell if (!buf) { 4601b885244eSDavid Brownell status = -ENOMEM; 4602b885244eSDavid Brownell goto err0; 46038ae12a0dSDavid Brownell } 4604b885244eSDavid Brownell 4605b885244eSDavid Brownell status = bus_register(&spi_bus_type); 4606b885244eSDavid Brownell if (status < 0) 4607b885244eSDavid Brownell goto err1; 4608b885244eSDavid Brownell 4609b885244eSDavid Brownell status = class_register(&spi_master_class); 4610b885244eSDavid Brownell if (status < 0) 4611b885244eSDavid Brownell goto err2; 4612ce79d54aSPantelis Antoniou 46136c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 46146c364062SGeert Uytterhoeven status = class_register(&spi_slave_class); 46156c364062SGeert Uytterhoeven if (status < 0) 46166c364062SGeert Uytterhoeven goto err3; 46176c364062SGeert Uytterhoeven } 46186c364062SGeert Uytterhoeven 46195267720eSFabio Estevam if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 4620ce79d54aSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 46217f24467fSOctavian Purdila if (IS_ENABLED(CONFIG_ACPI)) 46227f24467fSOctavian Purdila WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 4623ce79d54aSPantelis Antoniou 4624b885244eSDavid Brownell return 0; 4625b885244eSDavid Brownell 46266c364062SGeert Uytterhoeven err3: 46276c364062SGeert Uytterhoeven class_unregister(&spi_master_class); 4628b885244eSDavid Brownell err2: 4629b885244eSDavid Brownell bus_unregister(&spi_bus_type); 4630b885244eSDavid Brownell err1: 4631b885244eSDavid Brownell kfree(buf); 4632b885244eSDavid Brownell buf = NULL; 4633b885244eSDavid Brownell err0: 4634b885244eSDavid Brownell return status; 4635b885244eSDavid Brownell } 4636b885244eSDavid Brownell 4637350de7ceSAndy Shevchenko /* 4638350de7ceSAndy Shevchenko * A board_info is normally registered in arch_initcall(), 4639350de7ceSAndy Shevchenko * but even essential drivers wait till later. 4640b885244eSDavid Brownell * 4641350de7ceSAndy Shevchenko * REVISIT only boardinfo really needs static linking. The rest (device and 4642350de7ceSAndy Shevchenko * driver registration) _could_ be dynamically linked (modular) ... Costs 4643b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 46448ae12a0dSDavid Brownell */ 4645673c0c00SDavid Brownell postcore_initcall(spi_init); 4646