1b445bfcbSMarco Felsch // SPDX-License-Identifier: GPL-2.0-or-later 2787f4889SMark Brown // SPI init/core code 3787f4889SMark Brown // 4787f4889SMark Brown // Copyright (C) 2005 David Brownell 5787f4889SMark Brown // Copyright (C) 2008 Secret Lab Technologies Ltd. 68ae12a0dSDavid Brownell 78ae12a0dSDavid Brownell #include <linux/kernel.h> 88ae12a0dSDavid Brownell #include <linux/device.h> 98ae12a0dSDavid Brownell #include <linux/init.h> 108ae12a0dSDavid Brownell #include <linux/cache.h> 1199adef31SMark Brown #include <linux/dma-mapping.h> 1299adef31SMark Brown #include <linux/dmaengine.h> 1394040828SMatthias Kaehlcke #include <linux/mutex.h> 142b7a32f7SSinan Akman #include <linux/of_device.h> 15d57a4282SGrant Likely #include <linux/of_irq.h> 1686be408bSSylwester Nawrocki #include <linux/clk/clk-conf.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 18e0626e38SAnton Vorontsov #include <linux/mod_devicetable.h> 198ae12a0dSDavid Brownell #include <linux/spi/spi.h> 20b5932f5cSBoris Brezillon #include <linux/spi/spi-mem.h> 21f3186dd8SLinus Walleij #include <linux/gpio/consumer.h> 223ae22e8cSMark Brown #include <linux/pm_runtime.h> 23f48c767cSUlf Hansson #include <linux/pm_domain.h> 24826cf175SDmitry Torokhov #include <linux/property.h> 25025ed130SPaul Gortmaker #include <linux/export.h> 268bd75c77SClark Williams #include <linux/sched/rt.h> 27ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h> 28ffbbdd21SLinus Walleij #include <linux/delay.h> 29ffbbdd21SLinus Walleij #include <linux/kthread.h> 3064bee4d2SMika Westerberg #include <linux/ioport.h> 3164bee4d2SMika Westerberg #include <linux/acpi.h> 32b1b8153cSVignesh R #include <linux/highmem.h> 339b61e302SSuniel Mahesh #include <linux/idr.h> 348a2e487eSLukas Wunner #include <linux/platform_data/x86/apple.h> 3544ea6281SJakub Kicinski #include <linux/ptp_clock_kernel.h> 366598b91bSDavid Jander #include <linux/percpu.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 67d8e328b3SGrant Likely return sprintf(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); 925039563eSTrent Piepho len = snprintf(buf, PAGE_SIZE, "%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 986598b91bSDavid Jander static struct spi_statistics *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 1206598b91bSDavid Jander #define spi_pcpu_stats_totalize(ret, in, field) \ 1216598b91bSDavid Jander do { \ 1226598b91bSDavid Jander int i; \ 1236598b91bSDavid Jander ret = 0; \ 1246598b91bSDavid Jander for_each_possible_cpu(i) { \ 1256598b91bSDavid Jander const struct spi_statistics *pcpu_stats; \ 1266598b91bSDavid Jander u64 inc; \ 1276598b91bSDavid Jander unsigned int start; \ 1286598b91bSDavid Jander pcpu_stats = per_cpu_ptr(in, i); \ 1296598b91bSDavid Jander do { \ 1306598b91bSDavid Jander start = u64_stats_fetch_begin_irq( \ 1316598b91bSDavid Jander &pcpu_stats->syncp); \ 1326598b91bSDavid Jander inc = u64_stats_read(&pcpu_stats->field); \ 1336598b91bSDavid Jander } while (u64_stats_fetch_retry_irq( \ 1346598b91bSDavid Jander &pcpu_stats->syncp, start)); \ 1356598b91bSDavid Jander ret += inc; \ 1366598b91bSDavid Jander } \ 1376598b91bSDavid Jander } while (0) 1386598b91bSDavid Jander 139eca2ebc7SMartin Sperl #define SPI_STATISTICS_ATTRS(field, file) \ 1408caab75fSGeert Uytterhoeven static ssize_t spi_controller_##field##_show(struct device *dev, \ 141eca2ebc7SMartin Sperl struct device_attribute *attr, \ 142eca2ebc7SMartin Sperl char *buf) \ 143eca2ebc7SMartin Sperl { \ 1448caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, \ 1458caab75fSGeert Uytterhoeven struct spi_controller, dev); \ 1466598b91bSDavid Jander return spi_statistics_##field##_show(ctlr->pcpu_statistics, buf); \ 147eca2ebc7SMartin Sperl } \ 1488caab75fSGeert Uytterhoeven static struct device_attribute dev_attr_spi_controller_##field = { \ 149ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 1508caab75fSGeert Uytterhoeven .show = spi_controller_##field##_show, \ 151eca2ebc7SMartin Sperl }; \ 152eca2ebc7SMartin Sperl static ssize_t spi_device_##field##_show(struct device *dev, \ 153eca2ebc7SMartin Sperl struct device_attribute *attr, \ 154eca2ebc7SMartin Sperl char *buf) \ 155eca2ebc7SMartin Sperl { \ 156d1eba93bSGeliang Tang struct spi_device *spi = to_spi_device(dev); \ 1576598b91bSDavid Jander return spi_statistics_##field##_show(spi->pcpu_statistics, buf); \ 158eca2ebc7SMartin Sperl } \ 159eca2ebc7SMartin Sperl static struct device_attribute dev_attr_spi_device_##field = { \ 160ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 161eca2ebc7SMartin Sperl .show = spi_device_##field##_show, \ 162eca2ebc7SMartin Sperl } 163eca2ebc7SMartin Sperl 1646598b91bSDavid Jander #define SPI_STATISTICS_SHOW_NAME(name, file, field) \ 165eca2ebc7SMartin Sperl static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ 166eca2ebc7SMartin Sperl char *buf) \ 167eca2ebc7SMartin Sperl { \ 168eca2ebc7SMartin Sperl ssize_t len; \ 1696598b91bSDavid Jander u64 val; \ 1706598b91bSDavid Jander spi_pcpu_stats_totalize(val, stat, field); \ 1716598b91bSDavid Jander len = sysfs_emit(buf, "%llu\n", val); \ 172eca2ebc7SMartin Sperl return len; \ 173eca2ebc7SMartin Sperl } \ 174eca2ebc7SMartin Sperl SPI_STATISTICS_ATTRS(name, file) 175eca2ebc7SMartin Sperl 1766598b91bSDavid Jander #define SPI_STATISTICS_SHOW(field) \ 177eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ 1786598b91bSDavid Jander field) 179eca2ebc7SMartin Sperl 1806598b91bSDavid Jander SPI_STATISTICS_SHOW(messages); 1816598b91bSDavid Jander SPI_STATISTICS_SHOW(transfers); 1826598b91bSDavid Jander SPI_STATISTICS_SHOW(errors); 1836598b91bSDavid Jander SPI_STATISTICS_SHOW(timedout); 184eca2ebc7SMartin Sperl 1856598b91bSDavid Jander SPI_STATISTICS_SHOW(spi_sync); 1866598b91bSDavid Jander SPI_STATISTICS_SHOW(spi_sync_immediate); 1876598b91bSDavid Jander SPI_STATISTICS_SHOW(spi_async); 188eca2ebc7SMartin Sperl 1896598b91bSDavid Jander SPI_STATISTICS_SHOW(bytes); 1906598b91bSDavid Jander SPI_STATISTICS_SHOW(bytes_rx); 1916598b91bSDavid Jander SPI_STATISTICS_SHOW(bytes_tx); 192eca2ebc7SMartin Sperl 1936b7bc061SMartin Sperl #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ 1946b7bc061SMartin Sperl SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ 1956b7bc061SMartin Sperl "transfer_bytes_histo_" number, \ 1966598b91bSDavid Jander transfer_bytes_histo[index]) 1976b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); 1986b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); 1996b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); 2006b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); 2016b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); 2026b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); 2036b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); 2046b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); 2056b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); 2066b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); 2076b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); 2086b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); 2096b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); 2106b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); 2116b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); 2126b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); 2136b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); 2146b7bc061SMartin Sperl 2156598b91bSDavid Jander SPI_STATISTICS_SHOW(transfers_split_maxsize); 216d9f12122SMartin Sperl 217aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = { 218aa7da564SGreg Kroah-Hartman &dev_attr_modalias.attr, 2195039563eSTrent Piepho &dev_attr_driver_override.attr, 220aa7da564SGreg Kroah-Hartman NULL, 2218ae12a0dSDavid Brownell }; 222eca2ebc7SMartin Sperl 223eca2ebc7SMartin Sperl static const struct attribute_group spi_dev_group = { 224eca2ebc7SMartin Sperl .attrs = spi_dev_attrs, 225eca2ebc7SMartin Sperl }; 226eca2ebc7SMartin Sperl 227eca2ebc7SMartin Sperl static struct attribute *spi_device_statistics_attrs[] = { 228eca2ebc7SMartin Sperl &dev_attr_spi_device_messages.attr, 229eca2ebc7SMartin Sperl &dev_attr_spi_device_transfers.attr, 230eca2ebc7SMartin Sperl &dev_attr_spi_device_errors.attr, 231eca2ebc7SMartin Sperl &dev_attr_spi_device_timedout.attr, 232eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync.attr, 233eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync_immediate.attr, 234eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_async.attr, 235eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes.attr, 236eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_rx.attr, 237eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_tx.attr, 2386b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo0.attr, 2396b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo1.attr, 2406b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo2.attr, 2416b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo3.attr, 2426b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo4.attr, 2436b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo5.attr, 2446b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo6.attr, 2456b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo7.attr, 2466b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo8.attr, 2476b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo9.attr, 2486b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo10.attr, 2496b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo11.attr, 2506b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo12.attr, 2516b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo13.attr, 2526b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo14.attr, 2536b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo15.attr, 2546b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo16.attr, 255d9f12122SMartin Sperl &dev_attr_spi_device_transfers_split_maxsize.attr, 256eca2ebc7SMartin Sperl NULL, 257eca2ebc7SMartin Sperl }; 258eca2ebc7SMartin Sperl 259eca2ebc7SMartin Sperl static const struct attribute_group spi_device_statistics_group = { 260eca2ebc7SMartin Sperl .name = "statistics", 261eca2ebc7SMartin Sperl .attrs = spi_device_statistics_attrs, 262eca2ebc7SMartin Sperl }; 263eca2ebc7SMartin Sperl 264eca2ebc7SMartin Sperl static const struct attribute_group *spi_dev_groups[] = { 265eca2ebc7SMartin Sperl &spi_dev_group, 266eca2ebc7SMartin Sperl &spi_device_statistics_group, 267eca2ebc7SMartin Sperl NULL, 268eca2ebc7SMartin Sperl }; 269eca2ebc7SMartin Sperl 2708caab75fSGeert Uytterhoeven static struct attribute *spi_controller_statistics_attrs[] = { 2718caab75fSGeert Uytterhoeven &dev_attr_spi_controller_messages.attr, 2728caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers.attr, 2738caab75fSGeert Uytterhoeven &dev_attr_spi_controller_errors.attr, 2748caab75fSGeert Uytterhoeven &dev_attr_spi_controller_timedout.attr, 2758caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync.attr, 2768caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync_immediate.attr, 2778caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_async.attr, 2788caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes.attr, 2798caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_rx.attr, 2808caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_tx.attr, 2818caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo0.attr, 2828caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo1.attr, 2838caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo2.attr, 2848caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo3.attr, 2858caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo4.attr, 2868caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo5.attr, 2878caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo6.attr, 2888caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo7.attr, 2898caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo8.attr, 2908caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo9.attr, 2918caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo10.attr, 2928caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo11.attr, 2938caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo12.attr, 2948caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo13.attr, 2958caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo14.attr, 2968caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo15.attr, 2978caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo16.attr, 2988caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers_split_maxsize.attr, 299eca2ebc7SMartin Sperl NULL, 300eca2ebc7SMartin Sperl }; 301eca2ebc7SMartin Sperl 3028caab75fSGeert Uytterhoeven static const struct attribute_group spi_controller_statistics_group = { 303eca2ebc7SMartin Sperl .name = "statistics", 3048caab75fSGeert Uytterhoeven .attrs = spi_controller_statistics_attrs, 305eca2ebc7SMartin Sperl }; 306eca2ebc7SMartin Sperl 307eca2ebc7SMartin Sperl static const struct attribute_group *spi_master_groups[] = { 3088caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 309eca2ebc7SMartin Sperl NULL, 310eca2ebc7SMartin Sperl }; 311eca2ebc7SMartin Sperl 3126598b91bSDavid Jander static void spi_statistics_add_transfer_stats(struct spi_statistics *pcpu_stats, 313eca2ebc7SMartin Sperl struct spi_transfer *xfer, 3148caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 315eca2ebc7SMartin Sperl { 3166b7bc061SMartin Sperl int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; 31767b9d641SDavid Jander struct spi_statistics *stats; 3186b7bc061SMartin Sperl 3196b7bc061SMartin Sperl if (l2len < 0) 3206b7bc061SMartin Sperl l2len = 0; 321eca2ebc7SMartin Sperl 32267b9d641SDavid Jander get_cpu(); 32367b9d641SDavid Jander stats = this_cpu_ptr(pcpu_stats); 3246598b91bSDavid Jander u64_stats_update_begin(&stats->syncp); 325eca2ebc7SMartin Sperl 3266598b91bSDavid Jander u64_stats_inc(&stats->transfers); 3276598b91bSDavid Jander u64_stats_inc(&stats->transfer_bytes_histo[l2len]); 328eca2ebc7SMartin Sperl 3296598b91bSDavid Jander u64_stats_add(&stats->bytes, xfer->len); 330eca2ebc7SMartin Sperl if ((xfer->tx_buf) && 3318caab75fSGeert Uytterhoeven (xfer->tx_buf != ctlr->dummy_tx)) 3326598b91bSDavid Jander u64_stats_add(&stats->bytes_tx, xfer->len); 333eca2ebc7SMartin Sperl if ((xfer->rx_buf) && 3348caab75fSGeert Uytterhoeven (xfer->rx_buf != ctlr->dummy_rx)) 3356598b91bSDavid Jander u64_stats_add(&stats->bytes_rx, xfer->len); 336eca2ebc7SMartin Sperl 3376598b91bSDavid Jander u64_stats_update_end(&stats->syncp); 33867b9d641SDavid Jander put_cpu(); 339eca2ebc7SMartin Sperl } 3408ae12a0dSDavid Brownell 341350de7ceSAndy Shevchenko /* 342350de7ceSAndy Shevchenko * modalias support makes "modprobe $MODALIAS" new-style hotplug work, 3438ae12a0dSDavid Brownell * and the sysfs version makes coldplug work too. 3448ae12a0dSDavid Brownell */ 3453f076575SAndy Shevchenko static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, const char *name) 34675368bf6SAnton Vorontsov { 34775368bf6SAnton Vorontsov while (id->name[0]) { 3483f076575SAndy Shevchenko if (!strcmp(name, id->name)) 34975368bf6SAnton Vorontsov return id; 35075368bf6SAnton Vorontsov id++; 35175368bf6SAnton Vorontsov } 35275368bf6SAnton Vorontsov return NULL; 35375368bf6SAnton Vorontsov } 35475368bf6SAnton Vorontsov 35575368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 35675368bf6SAnton Vorontsov { 35775368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 35875368bf6SAnton Vorontsov 3593f076575SAndy Shevchenko return spi_match_id(sdrv->id_table, sdev->modalias); 36075368bf6SAnton Vorontsov } 36175368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id); 36275368bf6SAnton Vorontsov 3638ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 3648ae12a0dSDavid Brownell { 3658ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 36675368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(drv); 36775368bf6SAnton Vorontsov 3685039563eSTrent Piepho /* Check override first, and if set, only use the named driver */ 3695039563eSTrent Piepho if (spi->driver_override) 3705039563eSTrent Piepho return strcmp(spi->driver_override, drv->name) == 0; 3715039563eSTrent Piepho 3722b7a32f7SSinan Akman /* Attempt an OF style match */ 3732b7a32f7SSinan Akman if (of_driver_match_device(dev, drv)) 3742b7a32f7SSinan Akman return 1; 3752b7a32f7SSinan Akman 37664bee4d2SMika Westerberg /* Then try ACPI */ 37764bee4d2SMika Westerberg if (acpi_driver_match_device(dev, drv)) 37864bee4d2SMika Westerberg return 1; 37964bee4d2SMika Westerberg 38075368bf6SAnton Vorontsov if (sdrv->id_table) 3813f076575SAndy Shevchenko return !!spi_match_id(sdrv->id_table, spi->modalias); 3828ae12a0dSDavid Brownell 38335f74fcaSKay Sievers return strcmp(spi->modalias, drv->name) == 0; 3848ae12a0dSDavid Brownell } 3858ae12a0dSDavid Brownell 3867eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 3878ae12a0dSDavid Brownell { 3888ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 3898c4ff6d0SZhang Rui int rc; 3908c4ff6d0SZhang Rui 3918c4ff6d0SZhang Rui rc = acpi_device_uevent_modalias(dev, env); 3928c4ff6d0SZhang Rui if (rc != -ENODEV) 3938c4ff6d0SZhang Rui return rc; 3948ae12a0dSDavid Brownell 3952856670fSAndy Shevchenko return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 3968ae12a0dSDavid Brownell } 3978ae12a0dSDavid Brownell 3989db34ee6SUwe Kleine-König static int spi_probe(struct device *dev) 399b885244eSDavid Brownell { 400b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 40144af7927SJon Hunter struct spi_device *spi = to_spi_device(dev); 40233cf00e5SMika Westerberg int ret; 403b885244eSDavid Brownell 40486be408bSSylwester Nawrocki ret = of_clk_set_defaults(dev->of_node, false); 40586be408bSSylwester Nawrocki if (ret) 40686be408bSSylwester Nawrocki return ret; 40786be408bSSylwester Nawrocki 40844af7927SJon Hunter if (dev->of_node) { 40944af7927SJon Hunter spi->irq = of_irq_get(dev->of_node, 0); 41044af7927SJon Hunter if (spi->irq == -EPROBE_DEFER) 41144af7927SJon Hunter return -EPROBE_DEFER; 41244af7927SJon Hunter if (spi->irq < 0) 41344af7927SJon Hunter spi->irq = 0; 41444af7927SJon Hunter } 41544af7927SJon Hunter 416676e7c25SUlf Hansson ret = dev_pm_domain_attach(dev, true); 41771f277a7SUlf Hansson if (ret) 41871f277a7SUlf Hansson return ret; 41971f277a7SUlf Hansson 420440408dbSUwe Kleine-König if (sdrv->probe) { 42144af7927SJon Hunter ret = sdrv->probe(spi); 42233cf00e5SMika Westerberg if (ret) 423676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 424440408dbSUwe Kleine-König } 42533cf00e5SMika Westerberg 42633cf00e5SMika Westerberg return ret; 427b885244eSDavid Brownell } 428b885244eSDavid Brownell 429fc7a6209SUwe Kleine-König static void spi_remove(struct device *dev) 430b885244eSDavid Brownell { 431b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 432b885244eSDavid Brownell 433a0386bbaSUwe Kleine-König if (sdrv->remove) 434a0386bbaSUwe Kleine-König sdrv->remove(to_spi_device(dev)); 4357795d475SUwe Kleine-König 436676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 437b885244eSDavid Brownell } 438b885244eSDavid Brownell 4399db34ee6SUwe Kleine-König static void spi_shutdown(struct device *dev) 440b885244eSDavid Brownell { 441a6f483b2SMarek Szyprowski if (dev->driver) { 442b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 443b885244eSDavid Brownell 4449db34ee6SUwe Kleine-König if (sdrv->shutdown) 445b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 446b885244eSDavid Brownell } 447a6f483b2SMarek Szyprowski } 448b885244eSDavid Brownell 4499db34ee6SUwe Kleine-König struct bus_type spi_bus_type = { 4509db34ee6SUwe Kleine-König .name = "spi", 4519db34ee6SUwe Kleine-König .dev_groups = spi_dev_groups, 4529db34ee6SUwe Kleine-König .match = spi_match_device, 4539db34ee6SUwe Kleine-König .uevent = spi_uevent, 4549db34ee6SUwe Kleine-König .probe = spi_probe, 4559db34ee6SUwe Kleine-König .remove = spi_remove, 4569db34ee6SUwe Kleine-König .shutdown = spi_shutdown, 4579db34ee6SUwe Kleine-König }; 4589db34ee6SUwe Kleine-König EXPORT_SYMBOL_GPL(spi_bus_type); 4599db34ee6SUwe Kleine-König 46033e34dc6SDavid Brownell /** 461ca5d2485SAndrew F. Davis * __spi_register_driver - register a SPI driver 46288c9321dSThierry Reding * @owner: owner module of the driver to register 46333e34dc6SDavid Brownell * @sdrv: the driver to register 46433e34dc6SDavid Brownell * Context: can sleep 46597d56dc6SJavier Martinez Canillas * 46697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 46733e34dc6SDavid Brownell */ 468ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) 469b885244eSDavid Brownell { 470ca5d2485SAndrew F. Davis sdrv->driver.owner = owner; 471b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 4725fa6863bSMark Brown 4735fa6863bSMark Brown /* 4745fa6863bSMark Brown * For Really Good Reasons we use spi: modaliases not of: 4755fa6863bSMark Brown * modaliases for DT so module autoloading won't work if we 4765fa6863bSMark Brown * don't have a spi_device_id as well as a compatible string. 4775fa6863bSMark Brown */ 4785fa6863bSMark Brown if (sdrv->driver.of_match_table) { 4795fa6863bSMark Brown const struct of_device_id *of_id; 4805fa6863bSMark Brown 4815fa6863bSMark Brown for (of_id = sdrv->driver.of_match_table; of_id->compatible[0]; 4825fa6863bSMark Brown of_id++) { 4835fa6863bSMark Brown const char *of_name; 4845fa6863bSMark Brown 4855fa6863bSMark Brown /* Strip off any vendor prefix */ 4865fa6863bSMark Brown of_name = strnchr(of_id->compatible, 4875fa6863bSMark Brown sizeof(of_id->compatible), ','); 4885fa6863bSMark Brown if (of_name) 4895fa6863bSMark Brown of_name++; 4905fa6863bSMark Brown else 4915fa6863bSMark Brown of_name = of_id->compatible; 4925fa6863bSMark Brown 4935fa6863bSMark Brown if (sdrv->id_table) { 4945fa6863bSMark Brown const struct spi_device_id *spi_id; 4955fa6863bSMark Brown 4963f076575SAndy Shevchenko spi_id = spi_match_id(sdrv->id_table, of_name); 497b79332efSAndy Shevchenko if (spi_id) 4985fa6863bSMark Brown continue; 4995fa6863bSMark Brown } else { 5005fa6863bSMark Brown if (strcmp(sdrv->driver.name, of_name) == 0) 5015fa6863bSMark Brown continue; 5025fa6863bSMark Brown } 5035fa6863bSMark Brown 5045fa6863bSMark Brown pr_warn("SPI driver %s has no spi_device_id for %s\n", 5055fa6863bSMark Brown sdrv->driver.name, of_id->compatible); 5065fa6863bSMark Brown } 5075fa6863bSMark Brown } 5085fa6863bSMark Brown 509b885244eSDavid Brownell return driver_register(&sdrv->driver); 510b885244eSDavid Brownell } 511ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver); 512b885244eSDavid Brownell 5138ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 5148ae12a0dSDavid Brownell 515350de7ceSAndy Shevchenko /* 516350de7ceSAndy Shevchenko * SPI devices should normally not be created by SPI device drivers; that 5178caab75fSGeert Uytterhoeven * would make them board-specific. Similarly with SPI controller drivers. 5188ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 5198ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 5208ae12a0dSDavid Brownell */ 5218ae12a0dSDavid Brownell 5228ae12a0dSDavid Brownell struct boardinfo { 5238ae12a0dSDavid Brownell struct list_head list; 5242b9603a0SFeng Tang struct spi_board_info board_info; 5258ae12a0dSDavid Brownell }; 5268ae12a0dSDavid Brownell 5278ae12a0dSDavid Brownell static LIST_HEAD(board_list); 5288caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list); 5292b9603a0SFeng Tang 5302b9603a0SFeng Tang /* 531be73e323SAndy Shevchenko * Used to protect add/del operation for board_info list and 532350de7ceSAndy Shevchenko * spi_controller list, and their matching process also used 533350de7ceSAndy Shevchenko * to protect object of type struct idr. 5342b9603a0SFeng Tang */ 53594040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 5368ae12a0dSDavid Brownell 537dc87c98eSGrant Likely /** 538dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 5398caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 540dc87c98eSGrant Likely * Context: can sleep 541dc87c98eSGrant Likely * 542dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 543dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 544dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 545dc87c98eSGrant Likely * spi_add_device() on it. 546dc87c98eSGrant Likely * 547dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 5488caab75fSGeert Uytterhoeven * spi_device structure to add it to the SPI controller. If the caller 549dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 550dc87c98eSGrant Likely * call spi_dev_put() on it. 551dc87c98eSGrant Likely * 55297d56dc6SJavier Martinez Canillas * Return: a pointer to the new device, or NULL. 553dc87c98eSGrant Likely */ 554e3dc1399SStefan Binding struct spi_device *spi_alloc_device(struct spi_controller *ctlr) 555dc87c98eSGrant Likely { 556dc87c98eSGrant Likely struct spi_device *spi; 557dc87c98eSGrant Likely 5588caab75fSGeert Uytterhoeven if (!spi_controller_get(ctlr)) 559dc87c98eSGrant Likely return NULL; 560dc87c98eSGrant Likely 5615fe5f05eSJingoo Han spi = kzalloc(sizeof(*spi), GFP_KERNEL); 562dc87c98eSGrant Likely if (!spi) { 5638caab75fSGeert Uytterhoeven spi_controller_put(ctlr); 564dc87c98eSGrant Likely return NULL; 565dc87c98eSGrant Likely } 566dc87c98eSGrant Likely 5676598b91bSDavid Jander spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL); 5686598b91bSDavid Jander if (!spi->pcpu_statistics) { 5696598b91bSDavid Jander kfree(spi); 5706598b91bSDavid Jander spi_controller_put(ctlr); 5716598b91bSDavid Jander return NULL; 5726598b91bSDavid Jander } 5736598b91bSDavid Jander 5748caab75fSGeert Uytterhoeven spi->master = spi->controller = ctlr; 5758caab75fSGeert Uytterhoeven spi->dev.parent = &ctlr->dev; 576dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 577dc87c98eSGrant Likely spi->dev.release = spidev_release; 578ea235786SJohn Garry spi->mode = ctlr->buswidth_override_bits; 579eca2ebc7SMartin Sperl 580dc87c98eSGrant Likely device_initialize(&spi->dev); 581dc87c98eSGrant Likely return spi; 582dc87c98eSGrant Likely } 583e3dc1399SStefan Binding EXPORT_SYMBOL_GPL(spi_alloc_device); 584dc87c98eSGrant Likely 585e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi) 586e13ac47bSJarkko Nikula { 587e13ac47bSJarkko Nikula struct acpi_device *adev = ACPI_COMPANION(&spi->dev); 588e13ac47bSJarkko Nikula 589e13ac47bSJarkko Nikula if (adev) { 590e13ac47bSJarkko Nikula dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); 591e13ac47bSJarkko Nikula return; 592e13ac47bSJarkko Nikula } 593e13ac47bSJarkko Nikula 5948caab75fSGeert Uytterhoeven dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), 595e13ac47bSJarkko Nikula spi->chip_select); 596e13ac47bSJarkko Nikula } 597e13ac47bSJarkko Nikula 598b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data) 599b6fb8d3aSMika Westerberg { 600b6fb8d3aSMika Westerberg struct spi_device *spi = to_spi_device(dev); 601b6fb8d3aSMika Westerberg struct spi_device *new_spi = data; 602b6fb8d3aSMika Westerberg 6038caab75fSGeert Uytterhoeven if (spi->controller == new_spi->controller && 604b6fb8d3aSMika Westerberg spi->chip_select == new_spi->chip_select) 605b6fb8d3aSMika Westerberg return -EBUSY; 606b6fb8d3aSMika Westerberg return 0; 607b6fb8d3aSMika Westerberg } 608b6fb8d3aSMika Westerberg 609c7299feaSSaravana Kannan static void spi_cleanup(struct spi_device *spi) 610c7299feaSSaravana Kannan { 611c7299feaSSaravana Kannan if (spi->controller->cleanup) 612c7299feaSSaravana Kannan spi->controller->cleanup(spi); 613c7299feaSSaravana Kannan } 614c7299feaSSaravana Kannan 6150c79378cSSebastian Reichel static int __spi_add_device(struct spi_device *spi) 6160c79378cSSebastian Reichel { 6170c79378cSSebastian Reichel struct spi_controller *ctlr = spi->controller; 6180c79378cSSebastian Reichel struct device *dev = ctlr->dev.parent; 6190c79378cSSebastian Reichel int status; 6200c79378cSSebastian Reichel 6216bfb15f3SUwe Kleine-König /* 6226bfb15f3SUwe Kleine-König * We need to make sure there's no other device with this 6236bfb15f3SUwe Kleine-König * chipselect **BEFORE** we call setup(), else we'll trash 6246bfb15f3SUwe Kleine-König * its configuration. 6256bfb15f3SUwe Kleine-König */ 6260c79378cSSebastian Reichel status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); 6270c79378cSSebastian Reichel if (status) { 6280c79378cSSebastian Reichel dev_err(dev, "chipselect %d already in use\n", 6290c79378cSSebastian Reichel spi->chip_select); 6300c79378cSSebastian Reichel return status; 6310c79378cSSebastian Reichel } 6320c79378cSSebastian Reichel 6330c79378cSSebastian Reichel /* Controller may unregister concurrently */ 6340c79378cSSebastian Reichel if (IS_ENABLED(CONFIG_SPI_DYNAMIC) && 6350c79378cSSebastian Reichel !device_is_registered(&ctlr->dev)) { 6360c79378cSSebastian Reichel return -ENODEV; 6370c79378cSSebastian Reichel } 6380c79378cSSebastian Reichel 6390c79378cSSebastian Reichel if (ctlr->cs_gpiods) 6400c79378cSSebastian Reichel spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; 6410c79378cSSebastian Reichel 642350de7ceSAndy Shevchenko /* 643350de7ceSAndy Shevchenko * Drivers may modify this initial i/o setup, but will 6440c79378cSSebastian Reichel * normally rely on the device being setup. Devices 6450c79378cSSebastian Reichel * using SPI_CS_HIGH can't coexist well otherwise... 6460c79378cSSebastian Reichel */ 6470c79378cSSebastian Reichel status = spi_setup(spi); 6480c79378cSSebastian Reichel if (status < 0) { 6490c79378cSSebastian Reichel dev_err(dev, "can't setup %s, status %d\n", 6500c79378cSSebastian Reichel dev_name(&spi->dev), status); 6510c79378cSSebastian Reichel return status; 6520c79378cSSebastian Reichel } 6530c79378cSSebastian Reichel 6540c79378cSSebastian Reichel /* Device may be bound to an active driver when this returns */ 6550c79378cSSebastian Reichel status = device_add(&spi->dev); 6560c79378cSSebastian Reichel if (status < 0) { 6570c79378cSSebastian Reichel dev_err(dev, "can't add %s, status %d\n", 6580c79378cSSebastian Reichel dev_name(&spi->dev), status); 6590c79378cSSebastian Reichel spi_cleanup(spi); 6600c79378cSSebastian Reichel } else { 6610c79378cSSebastian Reichel dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 6620c79378cSSebastian Reichel } 6630c79378cSSebastian Reichel 6640c79378cSSebastian Reichel return status; 6650c79378cSSebastian Reichel } 6660c79378cSSebastian Reichel 667dc87c98eSGrant Likely /** 668dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 669dc87c98eSGrant Likely * @spi: spi_device to register 670dc87c98eSGrant Likely * 671dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 672dc87c98eSGrant Likely * spi_alloc_device can be added onto the spi bus with this function. 673dc87c98eSGrant Likely * 67497d56dc6SJavier Martinez Canillas * Return: 0 on success; negative errno on failure 675dc87c98eSGrant Likely */ 676e3dc1399SStefan Binding int spi_add_device(struct spi_device *spi) 677dc87c98eSGrant Likely { 6788caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 6798caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 680dc87c98eSGrant Likely int status; 681dc87c98eSGrant Likely 682dc87c98eSGrant Likely /* Chipselects are numbered 0..max; validate. */ 6838caab75fSGeert Uytterhoeven if (spi->chip_select >= ctlr->num_chipselect) { 6848caab75fSGeert Uytterhoeven dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 6858caab75fSGeert Uytterhoeven ctlr->num_chipselect); 686dc87c98eSGrant Likely return -EINVAL; 687dc87c98eSGrant Likely } 688dc87c98eSGrant Likely 689dc87c98eSGrant Likely /* Set the bus ID string */ 690e13ac47bSJarkko Nikula spi_dev_set_name(spi); 691e48880e0SDavid Brownell 6926098475dSMark Brown mutex_lock(&ctlr->add_lock); 6930c79378cSSebastian Reichel status = __spi_add_device(spi); 6946098475dSMark Brown mutex_unlock(&ctlr->add_lock); 695e48880e0SDavid Brownell return status; 696dc87c98eSGrant Likely } 697e3dc1399SStefan Binding EXPORT_SYMBOL_GPL(spi_add_device); 6988ae12a0dSDavid Brownell 6990c79378cSSebastian Reichel static int spi_add_device_locked(struct spi_device *spi) 7000c79378cSSebastian Reichel { 7010c79378cSSebastian Reichel struct spi_controller *ctlr = spi->controller; 7020c79378cSSebastian Reichel struct device *dev = ctlr->dev.parent; 7030c79378cSSebastian Reichel 7040c79378cSSebastian Reichel /* Chipselects are numbered 0..max; validate. */ 7050c79378cSSebastian Reichel if (spi->chip_select >= ctlr->num_chipselect) { 7060c79378cSSebastian Reichel dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 7070c79378cSSebastian Reichel ctlr->num_chipselect); 7080c79378cSSebastian Reichel return -EINVAL; 7090c79378cSSebastian Reichel } 7100c79378cSSebastian Reichel 7110c79378cSSebastian Reichel /* Set the bus ID string */ 7120c79378cSSebastian Reichel spi_dev_set_name(spi); 7130c79378cSSebastian Reichel 7146098475dSMark Brown WARN_ON(!mutex_is_locked(&ctlr->add_lock)); 7150c79378cSSebastian Reichel return __spi_add_device(spi); 7160c79378cSSebastian Reichel } 7170c79378cSSebastian Reichel 71833e34dc6SDavid Brownell /** 71933e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 7208caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 72133e34dc6SDavid Brownell * @chip: Describes the SPI device 72233e34dc6SDavid Brownell * Context: can sleep 72333e34dc6SDavid Brownell * 72433e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 7258ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 7268ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 7278ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 7288ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 729082c8cb4SDavid Brownell * 73097d56dc6SJavier Martinez Canillas * Return: the new device, or NULL. 7318ae12a0dSDavid Brownell */ 7328caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr, 733e9d5a461SAdrian Bunk struct spi_board_info *chip) 7348ae12a0dSDavid Brownell { 7358ae12a0dSDavid Brownell struct spi_device *proxy; 7368ae12a0dSDavid Brownell int status; 7378ae12a0dSDavid Brownell 738350de7ceSAndy Shevchenko /* 739350de7ceSAndy Shevchenko * NOTE: caller did any chip->bus_num checks necessary. 740082c8cb4SDavid Brownell * 741082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 742082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 743082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 744082c8cb4SDavid Brownell */ 745082c8cb4SDavid Brownell 7468caab75fSGeert Uytterhoeven proxy = spi_alloc_device(ctlr); 747dc87c98eSGrant Likely if (!proxy) 7488ae12a0dSDavid Brownell return NULL; 7498ae12a0dSDavid Brownell 750102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 751102eb975SGrant Likely 7528ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 7538ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 754980a01c9SDavid Brownell proxy->mode = chip->mode; 7558ae12a0dSDavid Brownell proxy->irq = chip->irq; 756102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 7578ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 7588ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 7598ae12a0dSDavid Brownell proxy->controller_state = NULL; 7608ae12a0dSDavid Brownell 76147afc77bSHeikki Krogerus if (chip->swnode) { 76247afc77bSHeikki Krogerus status = device_add_software_node(&proxy->dev, chip->swnode); 763826cf175SDmitry Torokhov if (status) { 7649d902c2aSColin Ian King dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n", 765826cf175SDmitry Torokhov chip->modalias, status); 766826cf175SDmitry Torokhov goto err_dev_put; 767826cf175SDmitry Torokhov } 7688ae12a0dSDavid Brownell } 769dc87c98eSGrant Likely 770826cf175SDmitry Torokhov status = spi_add_device(proxy); 771826cf175SDmitry Torokhov if (status < 0) 772df41a5daSHeikki Krogerus goto err_dev_put; 773826cf175SDmitry Torokhov 774dc87c98eSGrant Likely return proxy; 775826cf175SDmitry Torokhov 776826cf175SDmitry Torokhov err_dev_put: 777df41a5daSHeikki Krogerus device_remove_software_node(&proxy->dev); 778826cf175SDmitry Torokhov spi_dev_put(proxy); 779826cf175SDmitry Torokhov return NULL; 780dc87c98eSGrant Likely } 7818ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 7828ae12a0dSDavid Brownell 7833b1884c2SGeert Uytterhoeven /** 7843b1884c2SGeert Uytterhoeven * spi_unregister_device - unregister a single SPI device 7853b1884c2SGeert Uytterhoeven * @spi: spi_device to unregister 7863b1884c2SGeert Uytterhoeven * 7873b1884c2SGeert Uytterhoeven * Start making the passed SPI device vanish. Normally this would be handled 7888caab75fSGeert Uytterhoeven * by spi_unregister_controller(). 7893b1884c2SGeert Uytterhoeven */ 7903b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi) 7913b1884c2SGeert Uytterhoeven { 792bd6c1644SGeert Uytterhoeven if (!spi) 793bd6c1644SGeert Uytterhoeven return; 794bd6c1644SGeert Uytterhoeven 7958324147fSJohan Hovold if (spi->dev.of_node) { 796bd6c1644SGeert Uytterhoeven of_node_clear_flag(spi->dev.of_node, OF_POPULATED); 7978324147fSJohan Hovold of_node_put(spi->dev.of_node); 7988324147fSJohan Hovold } 7997f24467fSOctavian Purdila if (ACPI_COMPANION(&spi->dev)) 8007f24467fSOctavian Purdila acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); 80147afc77bSHeikki Krogerus device_remove_software_node(&spi->dev); 80227e7db56SSaravana Kannan device_del(&spi->dev); 80327e7db56SSaravana Kannan spi_cleanup(spi); 80427e7db56SSaravana Kannan put_device(&spi->dev); 8053b1884c2SGeert Uytterhoeven } 8063b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device); 8073b1884c2SGeert Uytterhoeven 8088caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, 8092b9603a0SFeng Tang struct spi_board_info *bi) 8102b9603a0SFeng Tang { 8112b9603a0SFeng Tang struct spi_device *dev; 8122b9603a0SFeng Tang 8138caab75fSGeert Uytterhoeven if (ctlr->bus_num != bi->bus_num) 8142b9603a0SFeng Tang return; 8152b9603a0SFeng Tang 8168caab75fSGeert Uytterhoeven dev = spi_new_device(ctlr, bi); 8172b9603a0SFeng Tang if (!dev) 8188caab75fSGeert Uytterhoeven dev_err(ctlr->dev.parent, "can't create new device for %s\n", 8192b9603a0SFeng Tang bi->modalias); 8202b9603a0SFeng Tang } 8212b9603a0SFeng Tang 82233e34dc6SDavid Brownell /** 82333e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 82433e34dc6SDavid Brownell * @info: array of chip descriptors 82533e34dc6SDavid Brownell * @n: how many descriptors are provided 82633e34dc6SDavid Brownell * Context: can sleep 82733e34dc6SDavid Brownell * 8288ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 8298ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 8308ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 8318ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 8328ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 8338ae12a0dSDavid Brownell * 8348ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 8358ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 8368ae12a0dSDavid Brownell * would naturally declare its SPI devices. 8378ae12a0dSDavid Brownell * 8388ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 8398ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 84097d56dc6SJavier Martinez Canillas * 84197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 8428ae12a0dSDavid Brownell */ 843fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n) 8448ae12a0dSDavid Brownell { 8458ae12a0dSDavid Brownell struct boardinfo *bi; 8462b9603a0SFeng Tang int i; 8478ae12a0dSDavid Brownell 848c7908a37SXiubo Li if (!n) 849f974cf57SDmitry Torokhov return 0; 850c7908a37SXiubo Li 851f9bdb7fdSMarkus Elfring bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); 8528ae12a0dSDavid Brownell if (!bi) 8538ae12a0dSDavid Brownell return -ENOMEM; 8548ae12a0dSDavid Brownell 8552b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 8568caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 8572b9603a0SFeng Tang 8582b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 859826cf175SDmitry Torokhov 86094040828SMatthias Kaehlcke mutex_lock(&board_lock); 8618ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 8628caab75fSGeert Uytterhoeven list_for_each_entry(ctlr, &spi_controller_list, list) 8638caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, 8648caab75fSGeert Uytterhoeven &bi->board_info); 86594040828SMatthias Kaehlcke mutex_unlock(&board_lock); 8662b9603a0SFeng Tang } 8672b9603a0SFeng Tang 8688ae12a0dSDavid Brownell return 0; 8698ae12a0dSDavid Brownell } 8708ae12a0dSDavid Brownell 8718ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 8728ae12a0dSDavid Brownell 873fb51601bSUwe Kleine-König /* Core methods for SPI resource management */ 874fb51601bSUwe Kleine-König 875fb51601bSUwe Kleine-König /** 876fb51601bSUwe Kleine-König * spi_res_alloc - allocate a spi resource that is life-cycle managed 877fb51601bSUwe Kleine-König * during the processing of a spi_message while using 878fb51601bSUwe Kleine-König * spi_transfer_one 879fb51601bSUwe Kleine-König * @spi: the spi device for which we allocate memory 880fb51601bSUwe Kleine-König * @release: the release code to execute for this resource 881fb51601bSUwe Kleine-König * @size: size to alloc and return 882fb51601bSUwe Kleine-König * @gfp: GFP allocation flags 883fb51601bSUwe Kleine-König * 884fb51601bSUwe Kleine-König * Return: the pointer to the allocated data 885fb51601bSUwe Kleine-König * 886fb51601bSUwe Kleine-König * This may get enhanced in the future to allocate from a memory pool 887fb51601bSUwe Kleine-König * of the @spi_device or @spi_controller to avoid repeated allocations. 888fb51601bSUwe Kleine-König */ 889da21fde0SUwe Kleine-König static void *spi_res_alloc(struct spi_device *spi, spi_res_release_t release, 890fb51601bSUwe Kleine-König size_t size, gfp_t gfp) 891fb51601bSUwe Kleine-König { 892fb51601bSUwe Kleine-König struct spi_res *sres; 893fb51601bSUwe Kleine-König 894fb51601bSUwe Kleine-König sres = kzalloc(sizeof(*sres) + size, gfp); 895fb51601bSUwe Kleine-König if (!sres) 896fb51601bSUwe Kleine-König return NULL; 897fb51601bSUwe Kleine-König 898fb51601bSUwe Kleine-König INIT_LIST_HEAD(&sres->entry); 899fb51601bSUwe Kleine-König sres->release = release; 900fb51601bSUwe Kleine-König 901fb51601bSUwe Kleine-König return sres->data; 902fb51601bSUwe Kleine-König } 903fb51601bSUwe Kleine-König 904fb51601bSUwe Kleine-König /** 905fb51601bSUwe Kleine-König * spi_res_free - free an spi resource 906fb51601bSUwe Kleine-König * @res: pointer to the custom data of a resource 907fb51601bSUwe Kleine-König */ 908da21fde0SUwe Kleine-König static void spi_res_free(void *res) 909fb51601bSUwe Kleine-König { 910fb51601bSUwe Kleine-König struct spi_res *sres = container_of(res, struct spi_res, data); 911fb51601bSUwe Kleine-König 912fb51601bSUwe Kleine-König if (!res) 913fb51601bSUwe Kleine-König return; 914fb51601bSUwe Kleine-König 915fb51601bSUwe Kleine-König WARN_ON(!list_empty(&sres->entry)); 916fb51601bSUwe Kleine-König kfree(sres); 917fb51601bSUwe Kleine-König } 918fb51601bSUwe Kleine-König 919fb51601bSUwe Kleine-König /** 920fb51601bSUwe Kleine-König * spi_res_add - add a spi_res to the spi_message 921fb51601bSUwe Kleine-König * @message: the spi message 922fb51601bSUwe Kleine-König * @res: the spi_resource 923fb51601bSUwe Kleine-König */ 924da21fde0SUwe Kleine-König static void spi_res_add(struct spi_message *message, void *res) 925fb51601bSUwe Kleine-König { 926fb51601bSUwe Kleine-König struct spi_res *sres = container_of(res, struct spi_res, data); 927fb51601bSUwe Kleine-König 928fb51601bSUwe Kleine-König WARN_ON(!list_empty(&sres->entry)); 929fb51601bSUwe Kleine-König list_add_tail(&sres->entry, &message->resources); 930fb51601bSUwe Kleine-König } 931fb51601bSUwe Kleine-König 932fb51601bSUwe Kleine-König /** 933fb51601bSUwe Kleine-König * spi_res_release - release all spi resources for this message 934fb51601bSUwe Kleine-König * @ctlr: the @spi_controller 935fb51601bSUwe Kleine-König * @message: the @spi_message 936fb51601bSUwe Kleine-König */ 937da21fde0SUwe Kleine-König static void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 938fb51601bSUwe Kleine-König { 939fb51601bSUwe Kleine-König struct spi_res *res, *tmp; 940fb51601bSUwe Kleine-König 941fb51601bSUwe Kleine-König list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { 942fb51601bSUwe Kleine-König if (res->release) 943fb51601bSUwe Kleine-König res->release(ctlr, message, res->data); 944fb51601bSUwe Kleine-König 945fb51601bSUwe Kleine-König list_del(&res->entry); 946fb51601bSUwe Kleine-König 947fb51601bSUwe Kleine-König kfree(res); 948fb51601bSUwe Kleine-König } 949fb51601bSUwe Kleine-König } 950fb51601bSUwe Kleine-König 951fb51601bSUwe Kleine-König /*-------------------------------------------------------------------------*/ 952fb51601bSUwe Kleine-König 953d347b4aaSDavid Bauer static void spi_set_cs(struct spi_device *spi, bool enable, bool force) 954b158935fSMark Brown { 95586527bcbSAndy Shevchenko bool activate = enable; 95625093bdeSAlexandru Ardelean 957d40f0b6fSDouglas Anderson /* 958d40f0b6fSDouglas Anderson * Avoid calling into the driver (or doing delays) if the chip select 959d40f0b6fSDouglas Anderson * isn't actually changing from the last time this was called. 960d40f0b6fSDouglas Anderson */ 9616bb477dfSYun Zhou if (!force && ((enable && spi->controller->last_cs == spi->chip_select) || 9626bb477dfSYun Zhou (!enable && spi->controller->last_cs != spi->chip_select)) && 963d40f0b6fSDouglas Anderson (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) 964d40f0b6fSDouglas Anderson return; 965d40f0b6fSDouglas Anderson 9665cb4e1f3SAndy Shevchenko trace_spi_set_cs(spi, activate); 9675cb4e1f3SAndy Shevchenko 9686bb477dfSYun Zhou spi->controller->last_cs = enable ? spi->chip_select : -1; 969d40f0b6fSDouglas Anderson spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; 970d40f0b6fSDouglas Anderson 971f48dc6b9SLinus Walleij if ((spi->cs_gpiod || !spi->controller->set_cs_timing) && !activate) { 9728c33ebfeSMason Zhang spi_delay_exec(&spi->cs_hold, NULL); 97325093bdeSAlexandru Ardelean } 97425093bdeSAlexandru Ardelean 975b158935fSMark Brown if (spi->mode & SPI_CS_HIGH) 976b158935fSMark Brown enable = !enable; 977b158935fSMark Brown 9786b695469SAndy Shevchenko if (spi->cs_gpiod) { 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)) 9916b695469SAndy Shevchenko gpiod_set_value_cansleep(spi->cs_gpiod, !enable); 992f3186dd8SLinus Walleij else 9936b695469SAndy Shevchenko /* Polarity handled by GPIO library */ 9946b695469SAndy Shevchenko gpiod_set_value_cansleep(spi->cs_gpiod, activate); 9956b695469SAndy Shevchenko } 9968eee6b9dSThor Thayer /* Some SPI masters need both GPIO CS & slave_select */ 9978caab75fSGeert Uytterhoeven if ((spi->controller->flags & SPI_MASTER_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 1004f48dc6b9SLinus Walleij if (spi->cs_gpiod || !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 101346336966SBoris Brezillon int spi_map_buf(struct spi_controller *ctlr, struct device *dev, 10146ad45a27SMark Brown struct sg_table *sgt, void *buf, size_t len, 10156ad45a27SMark Brown enum dma_data_direction dir) 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 10816ad45a27SMark Brown ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); 108289e4b66aSGeert Uytterhoeven if (!ret) 108389e4b66aSGeert Uytterhoeven ret = -ENOMEM; 10846ad45a27SMark Brown if (ret < 0) { 10856ad45a27SMark Brown sg_free_table(sgt); 10866ad45a27SMark Brown return ret; 10876ad45a27SMark Brown } 10886ad45a27SMark Brown 10896ad45a27SMark Brown sgt->nents = ret; 10906ad45a27SMark Brown 10916ad45a27SMark Brown return 0; 10926ad45a27SMark Brown } 10936ad45a27SMark Brown 109446336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, 10956ad45a27SMark Brown struct sg_table *sgt, enum dma_data_direction dir) 10966ad45a27SMark Brown { 10976ad45a27SMark Brown if (sgt->orig_nents) { 10986ad45a27SMark Brown dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); 10996ad45a27SMark Brown sg_free_table(sgt); 11006ad45a27SMark Brown } 11016ad45a27SMark Brown } 11026ad45a27SMark Brown 11038caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 110499adef31SMark Brown { 110599adef31SMark Brown struct device *tx_dev, *rx_dev; 110699adef31SMark Brown struct spi_transfer *xfer; 11076ad45a27SMark Brown int ret; 11083a2eba9bSMark Brown 11098caab75fSGeert Uytterhoeven if (!ctlr->can_dma) 111099adef31SMark Brown return 0; 111199adef31SMark Brown 11128caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 11138caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 1114b470e10eSVinod Koul else if (ctlr->dma_map_dev) 1115b470e10eSVinod Koul tx_dev = ctlr->dma_map_dev; 1116c37f45b5SLeilk Liu else 11178caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 1118c37f45b5SLeilk Liu 11198caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 11208caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 1121b470e10eSVinod Koul else if (ctlr->dma_map_dev) 1122b470e10eSVinod Koul rx_dev = ctlr->dma_map_dev; 1123c37f45b5SLeilk Liu else 11248caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 112599adef31SMark Brown 112699adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 11278caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 112899adef31SMark Brown continue; 112999adef31SMark Brown 113099adef31SMark Brown if (xfer->tx_buf != NULL) { 11318caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, 11326ad45a27SMark Brown (void *)xfer->tx_buf, xfer->len, 113399adef31SMark Brown DMA_TO_DEVICE); 11346ad45a27SMark Brown if (ret != 0) 11356ad45a27SMark Brown return ret; 113699adef31SMark Brown } 113799adef31SMark Brown 113899adef31SMark Brown if (xfer->rx_buf != NULL) { 11398caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, 114099adef31SMark Brown xfer->rx_buf, xfer->len, 114199adef31SMark Brown DMA_FROM_DEVICE); 11426ad45a27SMark Brown if (ret != 0) { 11438caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, 11446ad45a27SMark Brown DMA_TO_DEVICE); 11456ad45a27SMark Brown return ret; 114699adef31SMark Brown } 114799adef31SMark Brown } 114899adef31SMark Brown } 114999adef31SMark Brown 11508caab75fSGeert Uytterhoeven ctlr->cur_msg_mapped = true; 115199adef31SMark Brown 115299adef31SMark Brown return 0; 115399adef31SMark Brown } 115499adef31SMark Brown 11558caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg) 115699adef31SMark Brown { 115799adef31SMark Brown struct spi_transfer *xfer; 115899adef31SMark Brown struct device *tx_dev, *rx_dev; 115999adef31SMark Brown 11608caab75fSGeert Uytterhoeven if (!ctlr->cur_msg_mapped || !ctlr->can_dma) 116199adef31SMark Brown return 0; 116299adef31SMark Brown 11638caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 11648caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 1165409543ceSVinod Koul else if (ctlr->dma_map_dev) 1166409543ceSVinod Koul tx_dev = ctlr->dma_map_dev; 1167c37f45b5SLeilk Liu else 11688caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 1169c37f45b5SLeilk Liu 11708caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 11718caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 1172409543ceSVinod Koul else if (ctlr->dma_map_dev) 1173409543ceSVinod Koul rx_dev = ctlr->dma_map_dev; 1174c37f45b5SLeilk Liu else 11758caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 117699adef31SMark Brown 117799adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 11788caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 117999adef31SMark Brown continue; 118099adef31SMark Brown 11818caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 11828caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 118399adef31SMark Brown } 118499adef31SMark Brown 1185809b1b04SRobin Gong ctlr->cur_msg_mapped = false; 1186809b1b04SRobin Gong 118799adef31SMark Brown return 0; 118899adef31SMark Brown } 11892de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */ 11908caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr, 11912de440f5SGeert Uytterhoeven struct spi_message *msg) 11922de440f5SGeert Uytterhoeven { 11932de440f5SGeert Uytterhoeven return 0; 11942de440f5SGeert Uytterhoeven } 11952de440f5SGeert Uytterhoeven 11968caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr, 11972de440f5SGeert Uytterhoeven struct spi_message *msg) 11982de440f5SGeert Uytterhoeven { 11992de440f5SGeert Uytterhoeven return 0; 12002de440f5SGeert Uytterhoeven } 12012de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */ 12022de440f5SGeert Uytterhoeven 12038caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr, 12044b786458SMartin Sperl struct spi_message *msg) 12054b786458SMartin Sperl { 12064b786458SMartin Sperl struct spi_transfer *xfer; 12074b786458SMartin Sperl 12084b786458SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 12094b786458SMartin Sperl /* 12104b786458SMartin Sperl * Restore the original value of tx_buf or rx_buf if they are 12114b786458SMartin Sperl * NULL. 12124b786458SMartin Sperl */ 12138caab75fSGeert Uytterhoeven if (xfer->tx_buf == ctlr->dummy_tx) 12144b786458SMartin Sperl xfer->tx_buf = NULL; 12158caab75fSGeert Uytterhoeven if (xfer->rx_buf == ctlr->dummy_rx) 12164b786458SMartin Sperl xfer->rx_buf = NULL; 12174b786458SMartin Sperl } 12184b786458SMartin Sperl 12198caab75fSGeert Uytterhoeven return __spi_unmap_msg(ctlr, msg); 12204b786458SMartin Sperl } 12214b786458SMartin Sperl 12228caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 12232de440f5SGeert Uytterhoeven { 12242de440f5SGeert Uytterhoeven struct spi_transfer *xfer; 12252de440f5SGeert Uytterhoeven void *tmp; 12262de440f5SGeert Uytterhoeven unsigned int max_tx, max_rx; 12272de440f5SGeert Uytterhoeven 1228aee67fe8Sdillon min if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) 1229aee67fe8Sdillon min && !(msg->spi->mode & SPI_3WIRE)) { 12302de440f5SGeert Uytterhoeven max_tx = 0; 12312de440f5SGeert Uytterhoeven max_rx = 0; 12322de440f5SGeert Uytterhoeven 12332de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, transfer_list) { 12348caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && 12352de440f5SGeert Uytterhoeven !xfer->tx_buf) 12362de440f5SGeert Uytterhoeven max_tx = max(xfer->len, max_tx); 12378caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && 12382de440f5SGeert Uytterhoeven !xfer->rx_buf) 12392de440f5SGeert Uytterhoeven max_rx = max(xfer->len, max_rx); 12402de440f5SGeert Uytterhoeven } 12412de440f5SGeert Uytterhoeven 12422de440f5SGeert Uytterhoeven if (max_tx) { 12438caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_tx, max_tx, 1244b00bab9dSAndy Shevchenko GFP_KERNEL | GFP_DMA | __GFP_ZERO); 12452de440f5SGeert Uytterhoeven if (!tmp) 12462de440f5SGeert Uytterhoeven return -ENOMEM; 12478caab75fSGeert Uytterhoeven ctlr->dummy_tx = tmp; 12482de440f5SGeert Uytterhoeven } 12492de440f5SGeert Uytterhoeven 12502de440f5SGeert Uytterhoeven if (max_rx) { 12518caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_rx, max_rx, 12522de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 12532de440f5SGeert Uytterhoeven if (!tmp) 12542de440f5SGeert Uytterhoeven return -ENOMEM; 12558caab75fSGeert Uytterhoeven ctlr->dummy_rx = tmp; 12562de440f5SGeert Uytterhoeven } 12572de440f5SGeert Uytterhoeven 12582de440f5SGeert Uytterhoeven if (max_tx || max_rx) { 12592de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, 12602de440f5SGeert Uytterhoeven transfer_list) { 12615442dcaaSChris Lesiak if (!xfer->len) 12625442dcaaSChris Lesiak continue; 12632de440f5SGeert Uytterhoeven if (!xfer->tx_buf) 12648caab75fSGeert Uytterhoeven xfer->tx_buf = ctlr->dummy_tx; 12652de440f5SGeert Uytterhoeven if (!xfer->rx_buf) 12668caab75fSGeert Uytterhoeven xfer->rx_buf = ctlr->dummy_rx; 12672de440f5SGeert Uytterhoeven } 12682de440f5SGeert Uytterhoeven } 12692de440f5SGeert Uytterhoeven } 12702de440f5SGeert Uytterhoeven 12718caab75fSGeert Uytterhoeven return __spi_map_msg(ctlr, msg); 12722de440f5SGeert Uytterhoeven } 127399adef31SMark Brown 1274810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr, 1275810923f3SLubomir Rintel struct spi_message *msg, 1276810923f3SLubomir Rintel struct spi_transfer *xfer) 1277810923f3SLubomir Rintel { 12786598b91bSDavid Jander struct spi_statistics *statm = ctlr->pcpu_statistics; 12796598b91bSDavid Jander struct spi_statistics *stats = msg->spi->pcpu_statistics; 12806170d077SXu Yilun u32 speed_hz = xfer->speed_hz; 128149686df5SColin Ian King unsigned long long ms; 1282810923f3SLubomir Rintel 1283810923f3SLubomir Rintel if (spi_controller_is_slave(ctlr)) { 1284810923f3SLubomir Rintel if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { 1285810923f3SLubomir Rintel dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); 1286810923f3SLubomir Rintel return -EINTR; 1287810923f3SLubomir Rintel } 1288810923f3SLubomir Rintel } else { 12896170d077SXu Yilun if (!speed_hz) 12906170d077SXu Yilun speed_hz = 100000; 12916170d077SXu Yilun 129286b8bff7SAndy Shevchenko /* 129386b8bff7SAndy Shevchenko * For each byte we wait for 8 cycles of the SPI clock. 129486b8bff7SAndy Shevchenko * Since speed is defined in Hz and we want milliseconds, 129586b8bff7SAndy Shevchenko * use respective multiplier, but before the division, 129686b8bff7SAndy Shevchenko * otherwise we may get 0 for short transfers. 129786b8bff7SAndy Shevchenko */ 129886b8bff7SAndy Shevchenko ms = 8LL * MSEC_PER_SEC * xfer->len; 12996170d077SXu Yilun do_div(ms, speed_hz); 1300810923f3SLubomir Rintel 130186b8bff7SAndy Shevchenko /* 130286b8bff7SAndy Shevchenko * Increase it twice and add 200 ms tolerance, use 130386b8bff7SAndy Shevchenko * predefined maximum in case of overflow. 130486b8bff7SAndy Shevchenko */ 130586b8bff7SAndy Shevchenko ms += ms + 200; 1306810923f3SLubomir Rintel if (ms > UINT_MAX) 1307810923f3SLubomir Rintel ms = UINT_MAX; 1308810923f3SLubomir Rintel 1309810923f3SLubomir Rintel ms = wait_for_completion_timeout(&ctlr->xfer_completion, 1310810923f3SLubomir Rintel msecs_to_jiffies(ms)); 1311810923f3SLubomir Rintel 1312810923f3SLubomir Rintel if (ms == 0) { 1313810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(statm, timedout); 1314810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(stats, timedout); 1315810923f3SLubomir Rintel dev_err(&msg->spi->dev, 1316810923f3SLubomir Rintel "SPI transfer timed out\n"); 1317810923f3SLubomir Rintel return -ETIMEDOUT; 1318810923f3SLubomir Rintel } 1319810923f3SLubomir Rintel } 1320810923f3SLubomir Rintel 1321810923f3SLubomir Rintel return 0; 1322810923f3SLubomir Rintel } 1323810923f3SLubomir Rintel 13240ff2de8bSMartin Sperl static void _spi_transfer_delay_ns(u32 ns) 13250ff2de8bSMartin Sperl { 13260ff2de8bSMartin Sperl if (!ns) 13270ff2de8bSMartin Sperl return; 132886b8bff7SAndy Shevchenko if (ns <= NSEC_PER_USEC) { 13290ff2de8bSMartin Sperl ndelay(ns); 13300ff2de8bSMartin Sperl } else { 133186b8bff7SAndy Shevchenko u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC); 13320ff2de8bSMartin Sperl 13330ff2de8bSMartin Sperl if (us <= 10) 13340ff2de8bSMartin Sperl udelay(us); 13350ff2de8bSMartin Sperl else 13360ff2de8bSMartin Sperl usleep_range(us, us + DIV_ROUND_UP(us, 10)); 13370ff2de8bSMartin Sperl } 13380ff2de8bSMartin Sperl } 13390ff2de8bSMartin Sperl 13403984d39bSAlexandru Ardelean int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer) 13410ff2de8bSMartin Sperl { 1342b2c98153SAlexandru Ardelean u32 delay = _delay->value; 1343b2c98153SAlexandru Ardelean u32 unit = _delay->unit; 1344d5864e5bSMartin Sperl u32 hz; 13450ff2de8bSMartin Sperl 1346b2c98153SAlexandru Ardelean if (!delay) 1347b2c98153SAlexandru Ardelean return 0; 13480ff2de8bSMartin Sperl 13490ff2de8bSMartin Sperl switch (unit) { 13500ff2de8bSMartin Sperl case SPI_DELAY_UNIT_USECS: 135186b8bff7SAndy Shevchenko delay *= NSEC_PER_USEC; 13520ff2de8bSMartin Sperl break; 135386b8bff7SAndy Shevchenko case SPI_DELAY_UNIT_NSECS: 135486b8bff7SAndy Shevchenko /* Nothing to do here */ 13550ff2de8bSMartin Sperl break; 1356d5864e5bSMartin Sperl case SPI_DELAY_UNIT_SCK: 135795c8222fSDavid Jander /* Clock cycles need to be obtained from spi_transfer */ 1358b2c98153SAlexandru Ardelean if (!xfer) 1359b2c98153SAlexandru Ardelean return -EINVAL; 136086b8bff7SAndy Shevchenko /* 136186b8bff7SAndy Shevchenko * If there is unknown effective speed, approximate it 136286b8bff7SAndy Shevchenko * by underestimating with half of the requested hz. 1363d5864e5bSMartin Sperl */ 1364d5864e5bSMartin Sperl hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; 1365b2c98153SAlexandru Ardelean if (!hz) 1366b2c98153SAlexandru Ardelean return -EINVAL; 136786b8bff7SAndy Shevchenko 136886b8bff7SAndy Shevchenko /* Convert delay to nanoseconds */ 136986b8bff7SAndy Shevchenko delay *= DIV_ROUND_UP(NSEC_PER_SEC, hz); 1370d5864e5bSMartin Sperl break; 13710ff2de8bSMartin Sperl default: 1372b2c98153SAlexandru Ardelean return -EINVAL; 1373b2c98153SAlexandru Ardelean } 1374b2c98153SAlexandru Ardelean 1375b2c98153SAlexandru Ardelean return delay; 1376b2c98153SAlexandru Ardelean } 13773984d39bSAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_to_ns); 1378b2c98153SAlexandru Ardelean 1379b2c98153SAlexandru Ardelean int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer) 1380b2c98153SAlexandru Ardelean { 1381b2c98153SAlexandru Ardelean int delay; 1382b2c98153SAlexandru Ardelean 13838fede89fSMark Brown might_sleep(); 13848fede89fSMark Brown 1385b2c98153SAlexandru Ardelean if (!_delay) 1386b2c98153SAlexandru Ardelean return -EINVAL; 1387b2c98153SAlexandru Ardelean 13883984d39bSAlexandru Ardelean delay = spi_delay_to_ns(_delay, xfer); 1389b2c98153SAlexandru Ardelean if (delay < 0) 1390b2c98153SAlexandru Ardelean return delay; 1391b2c98153SAlexandru Ardelean 1392b2c98153SAlexandru Ardelean _spi_transfer_delay_ns(delay); 1393b2c98153SAlexandru Ardelean 1394b2c98153SAlexandru Ardelean return 0; 1395b2c98153SAlexandru Ardelean } 1396b2c98153SAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_exec); 1397b2c98153SAlexandru Ardelean 13980ff2de8bSMartin Sperl static void _spi_transfer_cs_change_delay(struct spi_message *msg, 13990ff2de8bSMartin Sperl struct spi_transfer *xfer) 14000ff2de8bSMartin Sperl { 140186b8bff7SAndy Shevchenko u32 default_delay_ns = 10 * NSEC_PER_USEC; 1402329f0dacSAlexandru Ardelean u32 delay = xfer->cs_change_delay.value; 1403329f0dacSAlexandru Ardelean u32 unit = xfer->cs_change_delay.unit; 1404329f0dacSAlexandru Ardelean int ret; 14050ff2de8bSMartin Sperl 140695c8222fSDavid Jander /* Return early on "fast" mode - for everything but USECS */ 14076b3f236aSAlexandru Ardelean if (!delay) { 14086b3f236aSAlexandru Ardelean if (unit == SPI_DELAY_UNIT_USECS) 140986b8bff7SAndy Shevchenko _spi_transfer_delay_ns(default_delay_ns); 14100ff2de8bSMartin Sperl return; 14116b3f236aSAlexandru Ardelean } 14120ff2de8bSMartin Sperl 1413329f0dacSAlexandru Ardelean ret = spi_delay_exec(&xfer->cs_change_delay, xfer); 1414329f0dacSAlexandru Ardelean if (ret) { 14150ff2de8bSMartin Sperl dev_err_once(&msg->spi->dev, 141686b8bff7SAndy Shevchenko "Use of unsupported delay unit %i, using default of %luus\n", 141786b8bff7SAndy Shevchenko unit, default_delay_ns / NSEC_PER_USEC); 141886b8bff7SAndy Shevchenko _spi_transfer_delay_ns(default_delay_ns); 14190ff2de8bSMartin Sperl } 14200ff2de8bSMartin Sperl } 14210ff2de8bSMartin Sperl 1422b158935fSMark Brown /* 1423b158935fSMark Brown * spi_transfer_one_message - Default implementation of transfer_one_message() 1424b158935fSMark Brown * 1425b158935fSMark Brown * This is a standard implementation of transfer_one_message() for 14268ba811a7SMoritz Fischer * drivers which implement a transfer_one() operation. It provides 1427b158935fSMark Brown * standard handling of delays and chip select management. 1428b158935fSMark Brown */ 14298caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr, 1430b158935fSMark Brown struct spi_message *msg) 1431b158935fSMark Brown { 1432b158935fSMark Brown struct spi_transfer *xfer; 1433b158935fSMark Brown bool keep_cs = false; 1434b158935fSMark Brown int ret = 0; 14356598b91bSDavid Jander struct spi_statistics *statm = ctlr->pcpu_statistics; 14366598b91bSDavid Jander struct spi_statistics *stats = msg->spi->pcpu_statistics; 1437b158935fSMark Brown 1438d347b4aaSDavid Bauer spi_set_cs(msg->spi, true, false); 1439b158935fSMark Brown 1440eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, messages); 1441eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, messages); 1442eca2ebc7SMartin Sperl 1443b158935fSMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1444b158935fSMark Brown trace_spi_transfer_start(msg, xfer); 1445b158935fSMark Brown 14468caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(statm, xfer, ctlr); 14478caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(stats, xfer, ctlr); 1448eca2ebc7SMartin Sperl 1449b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 1450b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 1451b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1452b42faeeeSVladimir Oltean } 1453b42faeeeSVladimir Oltean 1454b3063203SNicolas Saenz Julienne if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) { 14558caab75fSGeert Uytterhoeven reinit_completion(&ctlr->xfer_completion); 1456b158935fSMark Brown 1457809b1b04SRobin Gong fallback_pio: 14588caab75fSGeert Uytterhoeven ret = ctlr->transfer_one(ctlr, msg->spi, xfer); 1459b158935fSMark Brown if (ret < 0) { 1460809b1b04SRobin Gong if (ctlr->cur_msg_mapped && 1461809b1b04SRobin Gong (xfer->error & SPI_TRANS_FAIL_NO_START)) { 1462809b1b04SRobin Gong __spi_unmap_msg(ctlr, msg); 1463809b1b04SRobin Gong ctlr->fallback = true; 1464809b1b04SRobin Gong xfer->error &= ~SPI_TRANS_FAIL_NO_START; 1465809b1b04SRobin Gong goto fallback_pio; 1466809b1b04SRobin Gong } 1467809b1b04SRobin Gong 1468eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, 1469eca2ebc7SMartin Sperl errors); 1470eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, 1471eca2ebc7SMartin Sperl errors); 1472b158935fSMark Brown dev_err(&msg->spi->dev, 1473b158935fSMark Brown "SPI transfer failed: %d\n", ret); 1474b158935fSMark Brown goto out; 1475b158935fSMark Brown } 1476b158935fSMark Brown 1477d57e7960SMark Brown if (ret > 0) { 1478810923f3SLubomir Rintel ret = spi_transfer_wait(ctlr, msg, xfer); 1479810923f3SLubomir Rintel if (ret < 0) 1480810923f3SLubomir Rintel msg->status = ret; 1481d57e7960SMark Brown } 148238ec10f6SMark Brown } else { 148338ec10f6SMark Brown if (xfer->len) 148438ec10f6SMark Brown dev_err(&msg->spi->dev, 148538ec10f6SMark Brown "Bufferless transfer has length %u\n", 148638ec10f6SMark Brown xfer->len); 148738ec10f6SMark Brown } 1488b158935fSMark Brown 1489b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 1490b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1491b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 1492b42faeeeSVladimir Oltean } 1493b42faeeeSVladimir Oltean 1494b158935fSMark Brown trace_spi_transfer_stop(msg, xfer); 1495b158935fSMark Brown 1496b158935fSMark Brown if (msg->status != -EINPROGRESS) 1497b158935fSMark Brown goto out; 1498b158935fSMark Brown 1499bebcfd27SAlexandru Ardelean spi_transfer_delay_exec(xfer); 1500b158935fSMark Brown 1501b158935fSMark Brown if (xfer->cs_change) { 1502b158935fSMark Brown if (list_is_last(&xfer->transfer_list, 1503b158935fSMark Brown &msg->transfers)) { 1504b158935fSMark Brown keep_cs = true; 1505b158935fSMark Brown } else { 1506d347b4aaSDavid Bauer spi_set_cs(msg->spi, false, false); 15070ff2de8bSMartin Sperl _spi_transfer_cs_change_delay(msg, xfer); 1508d347b4aaSDavid Bauer spi_set_cs(msg->spi, true, false); 1509b158935fSMark Brown } 1510b158935fSMark Brown } 1511b158935fSMark Brown 1512b158935fSMark Brown msg->actual_length += xfer->len; 1513b158935fSMark Brown } 1514b158935fSMark Brown 1515b158935fSMark Brown out: 1516b158935fSMark Brown if (ret != 0 || !keep_cs) 1517d347b4aaSDavid Bauer spi_set_cs(msg->spi, false, false); 1518b158935fSMark Brown 1519b158935fSMark Brown if (msg->status == -EINPROGRESS) 1520b158935fSMark Brown msg->status = ret; 1521b158935fSMark Brown 15228caab75fSGeert Uytterhoeven if (msg->status && ctlr->handle_err) 15238caab75fSGeert Uytterhoeven ctlr->handle_err(ctlr, msg); 1524b716c4ffSAndy Shevchenko 15250ed56252SMark Brown spi_finalize_current_message(ctlr); 15260ed56252SMark Brown 1527b158935fSMark Brown return ret; 1528b158935fSMark Brown } 1529b158935fSMark Brown 1530b158935fSMark Brown /** 1531b158935fSMark Brown * spi_finalize_current_transfer - report completion of a transfer 15328caab75fSGeert Uytterhoeven * @ctlr: the controller reporting completion 1533b158935fSMark Brown * 1534b158935fSMark Brown * Called by SPI drivers using the core transfer_one_message() 1535b158935fSMark Brown * implementation to notify it that the current interrupt driven 15369e8f4882SGeert Uytterhoeven * transfer has finished and the next one may be scheduled. 1537b158935fSMark Brown */ 15388caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr) 1539b158935fSMark Brown { 15408caab75fSGeert Uytterhoeven complete(&ctlr->xfer_completion); 1541b158935fSMark Brown } 1542b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); 1543b158935fSMark Brown 1544e1268597SMark Brown static void spi_idle_runtime_pm(struct spi_controller *ctlr) 1545e1268597SMark Brown { 1546e1268597SMark Brown if (ctlr->auto_runtime_pm) { 1547e1268597SMark Brown pm_runtime_mark_last_busy(ctlr->dev.parent); 1548e1268597SMark Brown pm_runtime_put_autosuspend(ctlr->dev.parent); 1549e1268597SMark Brown } 1550e1268597SMark Brown } 1551e1268597SMark Brown 1552ae7d2346SDavid Jander static int __spi_pump_transfer_message(struct spi_controller *ctlr, 1553ae7d2346SDavid Jander struct spi_message *msg, bool was_busy) 1554ae7d2346SDavid Jander { 1555ae7d2346SDavid Jander struct spi_transfer *xfer; 1556ae7d2346SDavid Jander int ret; 1557ae7d2346SDavid Jander 1558ae7d2346SDavid Jander if (!was_busy && ctlr->auto_runtime_pm) { 1559ae7d2346SDavid Jander ret = pm_runtime_get_sync(ctlr->dev.parent); 1560ae7d2346SDavid Jander if (ret < 0) { 1561ae7d2346SDavid Jander pm_runtime_put_noidle(ctlr->dev.parent); 1562ae7d2346SDavid Jander dev_err(&ctlr->dev, "Failed to power device: %d\n", 1563ae7d2346SDavid Jander ret); 1564ae7d2346SDavid Jander return ret; 1565ae7d2346SDavid Jander } 1566ae7d2346SDavid Jander } 1567ae7d2346SDavid Jander 1568ae7d2346SDavid Jander if (!was_busy) 1569ae7d2346SDavid Jander trace_spi_controller_busy(ctlr); 1570ae7d2346SDavid Jander 1571ae7d2346SDavid Jander if (!was_busy && ctlr->prepare_transfer_hardware) { 1572ae7d2346SDavid Jander ret = ctlr->prepare_transfer_hardware(ctlr); 1573ae7d2346SDavid Jander if (ret) { 1574ae7d2346SDavid Jander dev_err(&ctlr->dev, 1575ae7d2346SDavid Jander "failed to prepare transfer hardware: %d\n", 1576ae7d2346SDavid Jander ret); 1577ae7d2346SDavid Jander 1578ae7d2346SDavid Jander if (ctlr->auto_runtime_pm) 1579ae7d2346SDavid Jander pm_runtime_put(ctlr->dev.parent); 1580ae7d2346SDavid Jander 1581ae7d2346SDavid Jander msg->status = ret; 1582ae7d2346SDavid Jander spi_finalize_current_message(ctlr); 1583ae7d2346SDavid Jander 1584ae7d2346SDavid Jander return ret; 1585ae7d2346SDavid Jander } 1586ae7d2346SDavid Jander } 1587ae7d2346SDavid Jander 1588ae7d2346SDavid Jander trace_spi_message_start(msg); 1589ae7d2346SDavid Jander 1590ae7d2346SDavid Jander if (ctlr->prepare_message) { 1591ae7d2346SDavid Jander ret = ctlr->prepare_message(ctlr, msg); 1592ae7d2346SDavid Jander if (ret) { 1593ae7d2346SDavid Jander dev_err(&ctlr->dev, "failed to prepare message: %d\n", 1594ae7d2346SDavid Jander ret); 1595ae7d2346SDavid Jander msg->status = ret; 1596ae7d2346SDavid Jander spi_finalize_current_message(ctlr); 1597ae7d2346SDavid Jander return ret; 1598ae7d2346SDavid Jander } 1599ae7d2346SDavid Jander msg->prepared = true; 1600ae7d2346SDavid Jander } 1601ae7d2346SDavid Jander 1602ae7d2346SDavid Jander ret = spi_map_msg(ctlr, msg); 1603ae7d2346SDavid Jander if (ret) { 1604ae7d2346SDavid Jander msg->status = ret; 1605ae7d2346SDavid Jander spi_finalize_current_message(ctlr); 1606ae7d2346SDavid Jander return ret; 1607ae7d2346SDavid Jander } 1608ae7d2346SDavid Jander 1609ae7d2346SDavid Jander if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1610ae7d2346SDavid Jander list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1611ae7d2346SDavid Jander xfer->ptp_sts_word_pre = 0; 1612ae7d2346SDavid Jander ptp_read_system_prets(xfer->ptp_sts); 1613ae7d2346SDavid Jander } 1614ae7d2346SDavid Jander } 1615ae7d2346SDavid Jander 1616dc302905SDavid Jander /* 1617dc302905SDavid Jander * Drivers implementation of transfer_one_message() must arrange for 1618dc302905SDavid Jander * spi_finalize_current_message() to get called. Most drivers will do 1619dc302905SDavid Jander * this in the calling context, but some don't. For those cases, a 1620dc302905SDavid Jander * completion is used to guarantee that this function does not return 1621dc302905SDavid Jander * until spi_finalize_current_message() is done accessing 1622dc302905SDavid Jander * ctlr->cur_msg. 1623dc302905SDavid Jander * Use of the following two flags enable to opportunistically skip the 1624dc302905SDavid Jander * use of the completion since its use involves expensive spin locks. 1625dc302905SDavid Jander * In case of a race with the context that calls 1626dc302905SDavid Jander * spi_finalize_current_message() the completion will always be used, 1627dc302905SDavid Jander * due to strict ordering of these flags using barriers. 1628dc302905SDavid Jander */ 1629dc302905SDavid Jander WRITE_ONCE(ctlr->cur_msg_incomplete, true); 1630dc302905SDavid Jander WRITE_ONCE(ctlr->cur_msg_need_completion, false); 163169fa9590SDavid Jander reinit_completion(&ctlr->cur_msg_completion); 163295c8222fSDavid Jander smp_wmb(); /* Make these available to spi_finalize_current_message() */ 1633dc302905SDavid Jander 1634ae7d2346SDavid Jander ret = ctlr->transfer_one_message(ctlr, msg); 1635ae7d2346SDavid Jander if (ret) { 1636ae7d2346SDavid Jander dev_err(&ctlr->dev, 1637ae7d2346SDavid Jander "failed to transfer one message from queue\n"); 1638ae7d2346SDavid Jander return ret; 163931d4c1bdSDavid Jander } 164031d4c1bdSDavid Jander 1641dc302905SDavid Jander WRITE_ONCE(ctlr->cur_msg_need_completion, true); 164231d4c1bdSDavid Jander smp_mb(); /* See spi_finalize_current_message()... */ 1643dc302905SDavid Jander if (READ_ONCE(ctlr->cur_msg_incomplete)) 164469fa9590SDavid Jander wait_for_completion(&ctlr->cur_msg_completion); 1645ae7d2346SDavid Jander 1646ae7d2346SDavid Jander return 0; 1647ae7d2346SDavid Jander } 1648ae7d2346SDavid Jander 1649ffbbdd21SLinus Walleij /** 1650fc9e0f71SMark Brown * __spi_pump_messages - function which processes spi message queue 16518caab75fSGeert Uytterhoeven * @ctlr: controller to process queue for 1652fc9e0f71SMark Brown * @in_kthread: true if we are in the context of the message pump thread 1653ffbbdd21SLinus Walleij * 1654ffbbdd21SLinus Walleij * This function checks if there is any spi message in the queue that 1655ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 1656ffbbdd21SLinus Walleij * and transfer each message. 1657ffbbdd21SLinus Walleij * 16580461a414SMark Brown * Note that it is called both from the kthread itself and also from 16590461a414SMark Brown * inside spi_sync(); the queue extraction handling at the top of the 16600461a414SMark Brown * function should deal with this safely. 1661ffbbdd21SLinus Walleij */ 16628caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1663ffbbdd21SLinus Walleij { 1664d1c44c93SVladimir Oltean struct spi_message *msg; 1665ffbbdd21SLinus Walleij bool was_busy = false; 1666d1c44c93SVladimir Oltean unsigned long flags; 1667ffbbdd21SLinus Walleij int ret; 1668ffbbdd21SLinus Walleij 1669c1038165SDavid Jander /* Take the IO mutex */ 1670c1038165SDavid Jander mutex_lock(&ctlr->io_mutex); 1671c1038165SDavid Jander 1672983aee5dSMark Brown /* Lock queue */ 16738caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1674983aee5dSMark Brown 1675983aee5dSMark Brown /* Make sure we are not already running a message */ 16768711a2abSDavid Jander if (ctlr->cur_msg) 1677c1038165SDavid Jander goto out_unlock; 1678983aee5dSMark Brown 1679983aee5dSMark Brown /* Check if the queue is idle */ 16808caab75fSGeert Uytterhoeven if (list_empty(&ctlr->queue) || !ctlr->running) { 16818711a2abSDavid Jander if (!ctlr->busy) 1682c1038165SDavid Jander goto out_unlock; 1683fc9e0f71SMark Brown 1684e1268597SMark Brown /* Defer any non-atomic teardown to the thread */ 1685f0125f1aSMark Brown if (!in_kthread) { 1686e1268597SMark Brown if (!ctlr->dummy_rx && !ctlr->dummy_tx && 1687e1268597SMark Brown !ctlr->unprepare_transfer_hardware) { 1688e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1689e1268597SMark Brown ctlr->busy = false; 1690ae7d2346SDavid Jander ctlr->queue_empty = true; 1691e1268597SMark Brown trace_spi_controller_idle(ctlr); 1692e1268597SMark Brown } else { 169360a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, 1694f0125f1aSMark Brown &ctlr->pump_messages); 1695e1268597SMark Brown } 1696c1038165SDavid Jander goto out_unlock; 1697f0125f1aSMark Brown } 1698f0125f1aSMark Brown 1699f0125f1aSMark Brown ctlr->busy = false; 1700f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1701f0125f1aSMark Brown 1702f0125f1aSMark Brown kfree(ctlr->dummy_rx); 1703f0125f1aSMark Brown ctlr->dummy_rx = NULL; 1704f0125f1aSMark Brown kfree(ctlr->dummy_tx); 1705f0125f1aSMark Brown ctlr->dummy_tx = NULL; 1706f0125f1aSMark Brown if (ctlr->unprepare_transfer_hardware && 1707f0125f1aSMark Brown ctlr->unprepare_transfer_hardware(ctlr)) 1708f0125f1aSMark Brown dev_err(&ctlr->dev, 1709f0125f1aSMark Brown "failed to unprepare transfer hardware\n"); 1710e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1711f0125f1aSMark Brown trace_spi_controller_idle(ctlr); 1712f0125f1aSMark Brown 1713f0125f1aSMark Brown spin_lock_irqsave(&ctlr->queue_lock, flags); 1714ae7d2346SDavid Jander ctlr->queue_empty = true; 1715c1038165SDavid Jander goto out_unlock; 1716ffbbdd21SLinus Walleij } 1717ffbbdd21SLinus Walleij 1718ffbbdd21SLinus Walleij /* Extract head of queue */ 1719d1c44c93SVladimir Oltean msg = list_first_entry(&ctlr->queue, struct spi_message, queue); 1720d1c44c93SVladimir Oltean ctlr->cur_msg = msg; 1721ffbbdd21SLinus Walleij 1722d1c44c93SVladimir Oltean list_del_init(&msg->queue); 17238caab75fSGeert Uytterhoeven if (ctlr->busy) 1724ffbbdd21SLinus Walleij was_busy = true; 1725ffbbdd21SLinus Walleij else 17268caab75fSGeert Uytterhoeven ctlr->busy = true; 17278caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1728ffbbdd21SLinus Walleij 1729ae7d2346SDavid Jander ret = __spi_pump_transfer_message(ctlr, msg, was_busy); 173069fa9590SDavid Jander if (!ret) 173169fa9590SDavid Jander kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1732c191543eSDavid Jander 173369fa9590SDavid Jander ctlr->cur_msg = NULL; 173469fa9590SDavid Jander ctlr->fallback = false; 173569fa9590SDavid Jander 17368caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 173762826970SMark Brown 173862826970SMark Brown /* Prod the scheduler in case transfer_one() was busy waiting */ 173949023d2eSJon Hunter if (!ret) 174062826970SMark Brown cond_resched(); 1741c1038165SDavid Jander return; 1742c1038165SDavid Jander 1743c1038165SDavid Jander out_unlock: 17448711a2abSDavid Jander spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1745c1038165SDavid Jander mutex_unlock(&ctlr->io_mutex); 1746ffbbdd21SLinus Walleij } 1747ffbbdd21SLinus Walleij 1748fc9e0f71SMark Brown /** 1749fc9e0f71SMark Brown * spi_pump_messages - kthread work function which processes spi message queue 17508caab75fSGeert Uytterhoeven * @work: pointer to kthread work struct contained in the controller struct 1751fc9e0f71SMark Brown */ 1752fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work) 1753fc9e0f71SMark Brown { 17548caab75fSGeert Uytterhoeven struct spi_controller *ctlr = 17558caab75fSGeert Uytterhoeven container_of(work, struct spi_controller, pump_messages); 1756fc9e0f71SMark Brown 17578caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, true); 1758fc9e0f71SMark Brown } 1759fc9e0f71SMark Brown 1760924b5867SDouglas Anderson /** 1761350de7ceSAndy Shevchenko * spi_take_timestamp_pre - helper to collect the beginning of the TX timestamp 1762b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1763b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1764862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1765b42faeeeSVladimir Oltean * @irqs_off: If true, will disable IRQs and preemption for the duration of the 1766b42faeeeSVladimir Oltean * transfer, for less jitter in time measurement. Only compatible 1767b42faeeeSVladimir Oltean * with PIO drivers. If true, must follow up with 1768b42faeeeSVladimir Oltean * spi_take_timestamp_post or otherwise system will crash. 1769b42faeeeSVladimir Oltean * WARNING: for fully predictable results, the CPU frequency must 1770b42faeeeSVladimir Oltean * also be under control (governor). 1771350de7ceSAndy Shevchenko * 1772350de7ceSAndy Shevchenko * This is a helper for drivers to collect the beginning of the TX timestamp 1773350de7ceSAndy Shevchenko * for the requested byte from the SPI transfer. The frequency with which this 1774350de7ceSAndy Shevchenko * function must be called (once per word, once for the whole transfer, once 1775350de7ceSAndy Shevchenko * per batch of words etc) is arbitrary as long as the @tx buffer offset is 1776350de7ceSAndy Shevchenko * greater than or equal to the requested byte at the time of the call. The 1777350de7ceSAndy Shevchenko * timestamp is only taken once, at the first such call. It is assumed that 1778350de7ceSAndy Shevchenko * the driver advances its @tx buffer pointer monotonically. 1779b42faeeeSVladimir Oltean */ 1780b42faeeeSVladimir Oltean void spi_take_timestamp_pre(struct spi_controller *ctlr, 1781b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1782862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1783b42faeeeSVladimir Oltean { 1784b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1785b42faeeeSVladimir Oltean return; 1786b42faeeeSVladimir Oltean 17876a726824SVladimir Oltean if (xfer->timestamped) 1788b42faeeeSVladimir Oltean return; 1789b42faeeeSVladimir Oltean 17906a726824SVladimir Oltean if (progress > xfer->ptp_sts_word_pre) 1791b42faeeeSVladimir Oltean return; 1792b42faeeeSVladimir Oltean 1793b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1794862dd2a9SVladimir Oltean xfer->ptp_sts_word_pre = progress; 1795b42faeeeSVladimir Oltean 1796b42faeeeSVladimir Oltean if (irqs_off) { 1797b42faeeeSVladimir Oltean local_irq_save(ctlr->irq_flags); 1798b42faeeeSVladimir Oltean preempt_disable(); 1799b42faeeeSVladimir Oltean } 1800b42faeeeSVladimir Oltean 1801b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1802b42faeeeSVladimir Oltean } 1803b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_pre); 1804b42faeeeSVladimir Oltean 1805b42faeeeSVladimir Oltean /** 1806350de7ceSAndy Shevchenko * spi_take_timestamp_post - helper to collect the end of the TX timestamp 1807b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1808b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1809862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1810b42faeeeSVladimir Oltean * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU. 1811350de7ceSAndy Shevchenko * 1812350de7ceSAndy Shevchenko * This is a helper for drivers to collect the end of the TX timestamp for 1813350de7ceSAndy Shevchenko * the requested byte from the SPI transfer. Can be called with an arbitrary 1814350de7ceSAndy Shevchenko * frequency: only the first call where @tx exceeds or is equal to the 1815350de7ceSAndy Shevchenko * requested word will be timestamped. 1816b42faeeeSVladimir Oltean */ 1817b42faeeeSVladimir Oltean void spi_take_timestamp_post(struct spi_controller *ctlr, 1818b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1819862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1820b42faeeeSVladimir Oltean { 1821b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1822b42faeeeSVladimir Oltean return; 1823b42faeeeSVladimir Oltean 18246a726824SVladimir Oltean if (xfer->timestamped) 1825b42faeeeSVladimir Oltean return; 1826b42faeeeSVladimir Oltean 1827862dd2a9SVladimir Oltean if (progress < xfer->ptp_sts_word_post) 1828b42faeeeSVladimir Oltean return; 1829b42faeeeSVladimir Oltean 1830b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1831b42faeeeSVladimir Oltean 1832b42faeeeSVladimir Oltean if (irqs_off) { 1833b42faeeeSVladimir Oltean local_irq_restore(ctlr->irq_flags); 1834b42faeeeSVladimir Oltean preempt_enable(); 1835b42faeeeSVladimir Oltean } 1836b42faeeeSVladimir Oltean 1837b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1838862dd2a9SVladimir Oltean xfer->ptp_sts_word_post = progress; 1839b42faeeeSVladimir Oltean 18406a726824SVladimir Oltean xfer->timestamped = true; 1841b42faeeeSVladimir Oltean } 1842b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_post); 1843b42faeeeSVladimir Oltean 1844b42faeeeSVladimir Oltean /** 1845924b5867SDouglas Anderson * spi_set_thread_rt - set the controller to pump at realtime priority 1846924b5867SDouglas Anderson * @ctlr: controller to boost priority of 1847924b5867SDouglas Anderson * 1848924b5867SDouglas Anderson * This can be called because the controller requested realtime priority 1849924b5867SDouglas Anderson * (by setting the ->rt value before calling spi_register_controller()) or 1850924b5867SDouglas Anderson * because a device on the bus said that its transfers needed realtime 1851924b5867SDouglas Anderson * priority. 1852924b5867SDouglas Anderson * 1853924b5867SDouglas Anderson * NOTE: at the moment if any device on a bus says it needs realtime then 1854924b5867SDouglas Anderson * the thread will be at realtime priority for all transfers on that 1855924b5867SDouglas Anderson * controller. If this eventually becomes a problem we may see if we can 1856924b5867SDouglas Anderson * find a way to boost the priority only temporarily during relevant 1857924b5867SDouglas Anderson * transfers. 1858924b5867SDouglas Anderson */ 1859924b5867SDouglas Anderson static void spi_set_thread_rt(struct spi_controller *ctlr) 1860ffbbdd21SLinus Walleij { 1861924b5867SDouglas Anderson dev_info(&ctlr->dev, 1862924b5867SDouglas Anderson "will run message pump with realtime priority\n"); 18636d2b84a4SLinus Torvalds sched_set_fifo(ctlr->kworker->task); 1864924b5867SDouglas Anderson } 1865924b5867SDouglas Anderson 1866924b5867SDouglas Anderson static int spi_init_queue(struct spi_controller *ctlr) 1867924b5867SDouglas Anderson { 18688caab75fSGeert Uytterhoeven ctlr->running = false; 18698caab75fSGeert Uytterhoeven ctlr->busy = false; 1870ae7d2346SDavid Jander ctlr->queue_empty = true; 1871ffbbdd21SLinus Walleij 187260a883d1SMarek Szyprowski ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); 187360a883d1SMarek Szyprowski if (IS_ERR(ctlr->kworker)) { 187460a883d1SMarek Szyprowski dev_err(&ctlr->dev, "failed to create message pump kworker\n"); 187560a883d1SMarek Szyprowski return PTR_ERR(ctlr->kworker); 1876ffbbdd21SLinus Walleij } 187760a883d1SMarek Szyprowski 18788caab75fSGeert Uytterhoeven kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1879f0125f1aSMark Brown 1880ffbbdd21SLinus Walleij /* 18818caab75fSGeert Uytterhoeven * Controller config will indicate if this controller should run the 1882ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 1883ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 1884ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 1885ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 1886ffbbdd21SLinus Walleij */ 1887924b5867SDouglas Anderson if (ctlr->rt) 1888924b5867SDouglas Anderson spi_set_thread_rt(ctlr); 1889ffbbdd21SLinus Walleij 1890ffbbdd21SLinus Walleij return 0; 1891ffbbdd21SLinus Walleij } 1892ffbbdd21SLinus Walleij 1893ffbbdd21SLinus Walleij /** 1894ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 1895ffbbdd21SLinus Walleij * messages 18968caab75fSGeert Uytterhoeven * @ctlr: the controller to check for queued messages 1897ffbbdd21SLinus Walleij * 1898ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 1899ffbbdd21SLinus Walleij * this call. 190097d56dc6SJavier Martinez Canillas * 190197d56dc6SJavier Martinez Canillas * Return: the next message in the queue, else NULL if the queue is empty. 1902ffbbdd21SLinus Walleij */ 19038caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1904ffbbdd21SLinus Walleij { 1905ffbbdd21SLinus Walleij struct spi_message *next; 1906ffbbdd21SLinus Walleij unsigned long flags; 1907ffbbdd21SLinus Walleij 190895c8222fSDavid Jander /* Get a pointer to the next message, if any */ 19098caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 19108caab75fSGeert Uytterhoeven next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 19111cfd97f9SAxel Lin queue); 19128caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1913ffbbdd21SLinus Walleij 1914ffbbdd21SLinus Walleij return next; 1915ffbbdd21SLinus Walleij } 1916ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1917ffbbdd21SLinus Walleij 1918ffbbdd21SLinus Walleij /** 1919ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 19208caab75fSGeert Uytterhoeven * @ctlr: the controller to return the message to 1921ffbbdd21SLinus Walleij * 1922ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 1923ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 1924ffbbdd21SLinus Walleij */ 19258caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr) 1926ffbbdd21SLinus Walleij { 1927b42faeeeSVladimir Oltean struct spi_transfer *xfer; 1928ffbbdd21SLinus Walleij struct spi_message *mesg; 19292841a5fcSMark Brown int ret; 1930ffbbdd21SLinus Walleij 19318caab75fSGeert Uytterhoeven mesg = ctlr->cur_msg; 1932ffbbdd21SLinus Walleij 1933b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1934b42faeeeSVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) { 1935b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1936b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 1937b42faeeeSVladimir Oltean } 1938b42faeeeSVladimir Oltean } 1939b42faeeeSVladimir Oltean 19406a726824SVladimir Oltean if (unlikely(ctlr->ptp_sts_supported)) 19416a726824SVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) 19426a726824SVladimir Oltean WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); 1943f971a207SVladimir Oltean 19448caab75fSGeert Uytterhoeven spi_unmap_msg(ctlr, mesg); 194599adef31SMark Brown 1946350de7ceSAndy Shevchenko /* 1947350de7ceSAndy Shevchenko * In the prepare_messages callback the SPI bus has the opportunity 1948350de7ceSAndy Shevchenko * to split a transfer to smaller chunks. 1949350de7ceSAndy Shevchenko * 1950350de7ceSAndy Shevchenko * Release the split transfers here since spi_map_msg() is done on 1951350de7ceSAndy Shevchenko * the split transfers. 1952b59a7ca1SGustav Wiklander */ 1953b59a7ca1SGustav Wiklander spi_res_release(ctlr, mesg); 1954b59a7ca1SGustav Wiklander 19551714582aSDavid Jander if (mesg->prepared && ctlr->unprepare_message) { 19568caab75fSGeert Uytterhoeven ret = ctlr->unprepare_message(ctlr, mesg); 19572841a5fcSMark Brown if (ret) { 19588caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 19598caab75fSGeert Uytterhoeven ret); 19602841a5fcSMark Brown } 19612841a5fcSMark Brown } 1962391949b6SUwe Kleine-König 19631714582aSDavid Jander mesg->prepared = false; 19641714582aSDavid Jander 1965dc302905SDavid Jander WRITE_ONCE(ctlr->cur_msg_incomplete, false); 1966dc302905SDavid Jander smp_mb(); /* See __spi_pump_transfer_message()... */ 1967dc302905SDavid Jander if (READ_ONCE(ctlr->cur_msg_need_completion)) 196869fa9590SDavid Jander complete(&ctlr->cur_msg_completion); 19698e76ef88SMartin Sperl 19708e76ef88SMartin Sperl trace_spi_message_done(mesg); 19712841a5fcSMark Brown 1972ffbbdd21SLinus Walleij mesg->state = NULL; 1973ffbbdd21SLinus Walleij if (mesg->complete) 1974ffbbdd21SLinus Walleij mesg->complete(mesg->context); 1975ffbbdd21SLinus Walleij } 1976ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 1977ffbbdd21SLinus Walleij 19788caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr) 1979ffbbdd21SLinus Walleij { 1980ffbbdd21SLinus Walleij unsigned long flags; 1981ffbbdd21SLinus Walleij 19828caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1983ffbbdd21SLinus Walleij 19848caab75fSGeert Uytterhoeven if (ctlr->running || ctlr->busy) { 19858caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1986ffbbdd21SLinus Walleij return -EBUSY; 1987ffbbdd21SLinus Walleij } 1988ffbbdd21SLinus Walleij 19898caab75fSGeert Uytterhoeven ctlr->running = true; 19908caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 19918caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1992ffbbdd21SLinus Walleij 199360a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1994ffbbdd21SLinus Walleij 1995ffbbdd21SLinus Walleij return 0; 1996ffbbdd21SLinus Walleij } 1997ffbbdd21SLinus Walleij 19988caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr) 1999ffbbdd21SLinus Walleij { 2000ffbbdd21SLinus Walleij unsigned long flags; 2001ffbbdd21SLinus Walleij unsigned limit = 500; 2002ffbbdd21SLinus Walleij int ret = 0; 2003ffbbdd21SLinus Walleij 20048caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 2005ffbbdd21SLinus Walleij 2006ffbbdd21SLinus Walleij /* 2007ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 20088caab75fSGeert Uytterhoeven * A wait_queue on the ctlr->busy could be used, but then the common 2009ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 2010ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 2011ffbbdd21SLinus Walleij */ 20128caab75fSGeert Uytterhoeven while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 20138caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2014f97b26b0SAxel Lin usleep_range(10000, 11000); 20158caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 2016ffbbdd21SLinus Walleij } 2017ffbbdd21SLinus Walleij 20188caab75fSGeert Uytterhoeven if (!list_empty(&ctlr->queue) || ctlr->busy) 2019ffbbdd21SLinus Walleij ret = -EBUSY; 2020ffbbdd21SLinus Walleij else 20218caab75fSGeert Uytterhoeven ctlr->running = false; 2022ffbbdd21SLinus Walleij 20238caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2024ffbbdd21SLinus Walleij 2025ffbbdd21SLinus Walleij if (ret) { 20268caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "could not stop message queue\n"); 2027ffbbdd21SLinus Walleij return ret; 2028ffbbdd21SLinus Walleij } 2029ffbbdd21SLinus Walleij return ret; 2030ffbbdd21SLinus Walleij } 2031ffbbdd21SLinus Walleij 20328caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr) 2033ffbbdd21SLinus Walleij { 2034ffbbdd21SLinus Walleij int ret; 2035ffbbdd21SLinus Walleij 20368caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 2037ffbbdd21SLinus Walleij 2038ffbbdd21SLinus Walleij /* 20393989144fSPetr Mladek * kthread_flush_worker will block until all work is done. 2040ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 2041ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 2042ffbbdd21SLinus Walleij * return anyway. 2043ffbbdd21SLinus Walleij */ 2044ffbbdd21SLinus Walleij if (ret) { 20458caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem destroying queue\n"); 2046ffbbdd21SLinus Walleij return ret; 2047ffbbdd21SLinus Walleij } 2048ffbbdd21SLinus Walleij 204960a883d1SMarek Szyprowski kthread_destroy_worker(ctlr->kworker); 2050ffbbdd21SLinus Walleij 2051ffbbdd21SLinus Walleij return 0; 2052ffbbdd21SLinus Walleij } 2053ffbbdd21SLinus Walleij 20540461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi, 20550461a414SMark Brown struct spi_message *msg, 20560461a414SMark Brown bool need_pump) 2057ffbbdd21SLinus Walleij { 20588caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 2059ffbbdd21SLinus Walleij unsigned long flags; 2060ffbbdd21SLinus Walleij 20618caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 2062ffbbdd21SLinus Walleij 20638caab75fSGeert Uytterhoeven if (!ctlr->running) { 20648caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2065ffbbdd21SLinus Walleij return -ESHUTDOWN; 2066ffbbdd21SLinus Walleij } 2067ffbbdd21SLinus Walleij msg->actual_length = 0; 2068ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 2069ffbbdd21SLinus Walleij 20708caab75fSGeert Uytterhoeven list_add_tail(&msg->queue, &ctlr->queue); 2071ae7d2346SDavid Jander ctlr->queue_empty = false; 2072f0125f1aSMark Brown if (!ctlr->busy && need_pump) 207360a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 2074ffbbdd21SLinus Walleij 20758caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2076ffbbdd21SLinus Walleij return 0; 2077ffbbdd21SLinus Walleij } 2078ffbbdd21SLinus Walleij 20790461a414SMark Brown /** 20800461a414SMark Brown * spi_queued_transfer - transfer function for queued transfers 20810461a414SMark Brown * @spi: spi device which is requesting transfer 20820461a414SMark Brown * @msg: spi message which is to handled is queued to driver queue 208397d56dc6SJavier Martinez Canillas * 208497d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 20850461a414SMark Brown */ 20860461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 20870461a414SMark Brown { 20880461a414SMark Brown return __spi_queued_transfer(spi, msg, true); 20890461a414SMark Brown } 20900461a414SMark Brown 20918caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr) 2092ffbbdd21SLinus Walleij { 2093ffbbdd21SLinus Walleij int ret; 2094ffbbdd21SLinus Walleij 20958caab75fSGeert Uytterhoeven ctlr->transfer = spi_queued_transfer; 20968caab75fSGeert Uytterhoeven if (!ctlr->transfer_one_message) 20978caab75fSGeert Uytterhoeven ctlr->transfer_one_message = spi_transfer_one_message; 2098ffbbdd21SLinus Walleij 2099ffbbdd21SLinus Walleij /* Initialize and start queue */ 21008caab75fSGeert Uytterhoeven ret = spi_init_queue(ctlr); 2101ffbbdd21SLinus Walleij if (ret) { 21028caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem initializing queue\n"); 2103ffbbdd21SLinus Walleij goto err_init_queue; 2104ffbbdd21SLinus Walleij } 21058caab75fSGeert Uytterhoeven ctlr->queued = true; 21068caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 2107ffbbdd21SLinus Walleij if (ret) { 21088caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem starting queue\n"); 2109ffbbdd21SLinus Walleij goto err_start_queue; 2110ffbbdd21SLinus Walleij } 2111ffbbdd21SLinus Walleij 2112ffbbdd21SLinus Walleij return 0; 2113ffbbdd21SLinus Walleij 2114ffbbdd21SLinus Walleij err_start_queue: 21158caab75fSGeert Uytterhoeven spi_destroy_queue(ctlr); 2116c3676d5cSMark Brown err_init_queue: 2117ffbbdd21SLinus Walleij return ret; 2118ffbbdd21SLinus Walleij } 2119ffbbdd21SLinus Walleij 2120988f259bSBoris Brezillon /** 2121988f259bSBoris Brezillon * spi_flush_queue - Send all pending messages in the queue from the callers' 2122988f259bSBoris Brezillon * context 2123988f259bSBoris Brezillon * @ctlr: controller to process queue for 2124988f259bSBoris Brezillon * 2125988f259bSBoris Brezillon * This should be used when one wants to ensure all pending messages have been 2126988f259bSBoris Brezillon * sent before doing something. Is used by the spi-mem code to make sure SPI 2127988f259bSBoris Brezillon * memory operations do not preempt regular SPI transfers that have been queued 2128988f259bSBoris Brezillon * before the spi-mem operation. 2129988f259bSBoris Brezillon */ 2130988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr) 2131988f259bSBoris Brezillon { 2132988f259bSBoris Brezillon if (ctlr->transfer == spi_queued_transfer) 2133988f259bSBoris Brezillon __spi_pump_messages(ctlr, false); 2134988f259bSBoris Brezillon } 2135988f259bSBoris Brezillon 2136ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 2137ffbbdd21SLinus Walleij 21387cb94361SAndreas Larsson #if defined(CONFIG_OF) 21398caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 2140c2e51ac3SGeert Uytterhoeven struct device_node *nc) 2141d57a4282SGrant Likely { 214289da4293STrent Piepho u32 value; 2143c2e51ac3SGeert Uytterhoeven int rc; 2144d57a4282SGrant Likely 2145d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 2146e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpha")) 2147d57a4282SGrant Likely spi->mode |= SPI_CPHA; 2148e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpol")) 2149d57a4282SGrant Likely spi->mode |= SPI_CPOL; 2150e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-3wire")) 2151c20151dfSLars-Peter Clausen spi->mode |= SPI_3WIRE; 2152e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-lsb-first")) 2153cd6339e6SZhao Qiang spi->mode |= SPI_LSB_FIRST; 21543e5ec1dbSGregory CLEMENT if (of_property_read_bool(nc, "spi-cs-high")) 2155f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 2156f3186dd8SLinus Walleij 2157f477b7fbSwangyuhang /* Device DUAL/QUAD mode */ 215889da4293STrent Piepho if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 215989da4293STrent Piepho switch (value) { 2160d962608cSDragos Bogdan case 0: 2161d962608cSDragos Bogdan spi->mode |= SPI_NO_TX; 2162d962608cSDragos Bogdan break; 216389da4293STrent Piepho case 1: 2164f477b7fbSwangyuhang break; 216589da4293STrent Piepho case 2: 2166f477b7fbSwangyuhang spi->mode |= SPI_TX_DUAL; 2167f477b7fbSwangyuhang break; 216889da4293STrent Piepho case 4: 2169f477b7fbSwangyuhang spi->mode |= SPI_TX_QUAD; 2170f477b7fbSwangyuhang break; 21716b03061fSYogesh Narayan Gaur case 8: 21726b03061fSYogesh Narayan Gaur spi->mode |= SPI_TX_OCTAL; 21736b03061fSYogesh Narayan Gaur break; 2174f477b7fbSwangyuhang default: 21758caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 2176a110f93dSwangyuhang "spi-tx-bus-width %d not supported\n", 217789da4293STrent Piepho value); 217880874d8cSGeert Uytterhoeven break; 2179f477b7fbSwangyuhang } 2180a822e99cSMark Brown } 2181f477b7fbSwangyuhang 218289da4293STrent Piepho if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 218389da4293STrent Piepho switch (value) { 2184d962608cSDragos Bogdan case 0: 2185d962608cSDragos Bogdan spi->mode |= SPI_NO_RX; 2186d962608cSDragos Bogdan break; 218789da4293STrent Piepho case 1: 2188f477b7fbSwangyuhang break; 218989da4293STrent Piepho case 2: 2190f477b7fbSwangyuhang spi->mode |= SPI_RX_DUAL; 2191f477b7fbSwangyuhang break; 219289da4293STrent Piepho case 4: 2193f477b7fbSwangyuhang spi->mode |= SPI_RX_QUAD; 2194f477b7fbSwangyuhang break; 21956b03061fSYogesh Narayan Gaur case 8: 21966b03061fSYogesh Narayan Gaur spi->mode |= SPI_RX_OCTAL; 21976b03061fSYogesh Narayan Gaur break; 2198f477b7fbSwangyuhang default: 21998caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 2200a110f93dSwangyuhang "spi-rx-bus-width %d not supported\n", 220189da4293STrent Piepho value); 220280874d8cSGeert Uytterhoeven break; 2203f477b7fbSwangyuhang } 2204a822e99cSMark Brown } 2205f477b7fbSwangyuhang 22068caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr)) { 2207194276b0SRob Herring if (!of_node_name_eq(nc, "slave")) { 220825c56c88SRob Herring dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 220925c56c88SRob Herring nc); 22106c364062SGeert Uytterhoeven return -EINVAL; 22116c364062SGeert Uytterhoeven } 22126c364062SGeert Uytterhoeven return 0; 22136c364062SGeert Uytterhoeven } 22146c364062SGeert Uytterhoeven 22156c364062SGeert Uytterhoeven /* Device address */ 22166c364062SGeert Uytterhoeven rc = of_property_read_u32(nc, "reg", &value); 22176c364062SGeert Uytterhoeven if (rc) { 221825c56c88SRob Herring dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 221925c56c88SRob Herring nc, rc); 22206c364062SGeert Uytterhoeven return rc; 22216c364062SGeert Uytterhoeven } 22226c364062SGeert Uytterhoeven spi->chip_select = value; 22236c364062SGeert Uytterhoeven 2224d57a4282SGrant Likely /* Device speed */ 2225671c3bf5SChuanhong Guo if (!of_property_read_u32(nc, "spi-max-frequency", &value)) 222689da4293STrent Piepho spi->max_speed_hz = value; 2227d57a4282SGrant Likely 2228c2e51ac3SGeert Uytterhoeven return 0; 2229c2e51ac3SGeert Uytterhoeven } 2230c2e51ac3SGeert Uytterhoeven 2231c2e51ac3SGeert Uytterhoeven static struct spi_device * 22328caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 2233c2e51ac3SGeert Uytterhoeven { 2234c2e51ac3SGeert Uytterhoeven struct spi_device *spi; 2235c2e51ac3SGeert Uytterhoeven int rc; 2236c2e51ac3SGeert Uytterhoeven 2237c2e51ac3SGeert Uytterhoeven /* Alloc an spi_device */ 22388caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 2239c2e51ac3SGeert Uytterhoeven if (!spi) { 224025c56c88SRob Herring dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 2241c2e51ac3SGeert Uytterhoeven rc = -ENOMEM; 2242c2e51ac3SGeert Uytterhoeven goto err_out; 2243c2e51ac3SGeert Uytterhoeven } 2244c2e51ac3SGeert Uytterhoeven 2245c2e51ac3SGeert Uytterhoeven /* Select device driver */ 2246c2e51ac3SGeert Uytterhoeven rc = of_modalias_node(nc, spi->modalias, 2247c2e51ac3SGeert Uytterhoeven sizeof(spi->modalias)); 2248c2e51ac3SGeert Uytterhoeven if (rc < 0) { 224925c56c88SRob Herring dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 2250c2e51ac3SGeert Uytterhoeven goto err_out; 2251c2e51ac3SGeert Uytterhoeven } 2252c2e51ac3SGeert Uytterhoeven 22538caab75fSGeert Uytterhoeven rc = of_spi_parse_dt(ctlr, spi, nc); 2254c2e51ac3SGeert Uytterhoeven if (rc) 2255c2e51ac3SGeert Uytterhoeven goto err_out; 2256c2e51ac3SGeert Uytterhoeven 2257d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 2258d57a4282SGrant Likely of_node_get(nc); 2259d57a4282SGrant Likely spi->dev.of_node = nc; 22600e793ba7SCharles Keepax spi->dev.fwnode = of_fwnode_handle(nc); 2261d57a4282SGrant Likely 2262d57a4282SGrant Likely /* Register the new device */ 2263d57a4282SGrant Likely rc = spi_add_device(spi); 2264d57a4282SGrant Likely if (rc) { 226525c56c88SRob Herring dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 22668324147fSJohan Hovold goto err_of_node_put; 2267d57a4282SGrant Likely } 2268d57a4282SGrant Likely 2269aff5e3f8SPantelis Antoniou return spi; 2270aff5e3f8SPantelis Antoniou 22718324147fSJohan Hovold err_of_node_put: 22728324147fSJohan Hovold of_node_put(nc); 2273aff5e3f8SPantelis Antoniou err_out: 2274aff5e3f8SPantelis Antoniou spi_dev_put(spi); 2275aff5e3f8SPantelis Antoniou return ERR_PTR(rc); 2276aff5e3f8SPantelis Antoniou } 2277aff5e3f8SPantelis Antoniou 2278aff5e3f8SPantelis Antoniou /** 2279aff5e3f8SPantelis Antoniou * of_register_spi_devices() - Register child devices onto the SPI bus 22808caab75fSGeert Uytterhoeven * @ctlr: Pointer to spi_controller device 2281aff5e3f8SPantelis Antoniou * 22826c364062SGeert Uytterhoeven * Registers an spi_device for each child node of controller node which 22836c364062SGeert Uytterhoeven * represents a valid SPI slave. 2284aff5e3f8SPantelis Antoniou */ 22858caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) 2286aff5e3f8SPantelis Antoniou { 2287aff5e3f8SPantelis Antoniou struct spi_device *spi; 2288aff5e3f8SPantelis Antoniou struct device_node *nc; 2289aff5e3f8SPantelis Antoniou 22908caab75fSGeert Uytterhoeven if (!ctlr->dev.of_node) 2291aff5e3f8SPantelis Antoniou return; 2292aff5e3f8SPantelis Antoniou 22938caab75fSGeert Uytterhoeven for_each_available_child_of_node(ctlr->dev.of_node, nc) { 2294bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(nc, OF_POPULATED)) 2295bd6c1644SGeert Uytterhoeven continue; 22968caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, nc); 2297e0af98a7SRalf Ramsauer if (IS_ERR(spi)) { 22988caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 229925c56c88SRob Herring "Failed to create SPI device for %pOF\n", nc); 2300e0af98a7SRalf Ramsauer of_node_clear_flag(nc, OF_POPULATED); 2301e0af98a7SRalf Ramsauer } 2302d57a4282SGrant Likely } 2303d57a4282SGrant Likely } 2304d57a4282SGrant Likely #else 23058caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { } 2306d57a4282SGrant Likely #endif 2307d57a4282SGrant Likely 23080c79378cSSebastian Reichel /** 23090c79378cSSebastian Reichel * spi_new_ancillary_device() - Register ancillary SPI device 23100c79378cSSebastian Reichel * @spi: Pointer to the main SPI device registering the ancillary device 23110c79378cSSebastian Reichel * @chip_select: Chip Select of the ancillary device 23120c79378cSSebastian Reichel * 23130c79378cSSebastian Reichel * Register an ancillary SPI device; for example some chips have a chip-select 23140c79378cSSebastian Reichel * for normal device usage and another one for setup/firmware upload. 23150c79378cSSebastian Reichel * 23160c79378cSSebastian Reichel * This may only be called from main SPI device's probe routine. 23170c79378cSSebastian Reichel * 23180c79378cSSebastian Reichel * Return: 0 on success; negative errno on failure 23190c79378cSSebastian Reichel */ 23200c79378cSSebastian Reichel struct spi_device *spi_new_ancillary_device(struct spi_device *spi, 23210c79378cSSebastian Reichel u8 chip_select) 23220c79378cSSebastian Reichel { 23230c79378cSSebastian Reichel struct spi_device *ancillary; 23240c79378cSSebastian Reichel int rc = 0; 23250c79378cSSebastian Reichel 23260c79378cSSebastian Reichel /* Alloc an spi_device */ 23270c79378cSSebastian Reichel ancillary = spi_alloc_device(spi->controller); 23280c79378cSSebastian Reichel if (!ancillary) { 23290c79378cSSebastian Reichel rc = -ENOMEM; 23300c79378cSSebastian Reichel goto err_out; 23310c79378cSSebastian Reichel } 23320c79378cSSebastian Reichel 23330c79378cSSebastian Reichel strlcpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias)); 23340c79378cSSebastian Reichel 23350c79378cSSebastian Reichel /* Use provided chip-select for ancillary device */ 23360c79378cSSebastian Reichel ancillary->chip_select = chip_select; 23370c79378cSSebastian Reichel 23380c79378cSSebastian Reichel /* Take over SPI mode/speed from SPI main device */ 23390c79378cSSebastian Reichel ancillary->max_speed_hz = spi->max_speed_hz; 2340b01d5506SColin Ian King ancillary->mode = spi->mode; 23410c79378cSSebastian Reichel 23420c79378cSSebastian Reichel /* Register the new device */ 23430c79378cSSebastian Reichel rc = spi_add_device_locked(ancillary); 23440c79378cSSebastian Reichel if (rc) { 23450c79378cSSebastian Reichel dev_err(&spi->dev, "failed to register ancillary device\n"); 23460c79378cSSebastian Reichel goto err_out; 23470c79378cSSebastian Reichel } 23480c79378cSSebastian Reichel 23490c79378cSSebastian Reichel return ancillary; 23500c79378cSSebastian Reichel 23510c79378cSSebastian Reichel err_out: 23520c79378cSSebastian Reichel spi_dev_put(ancillary); 23530c79378cSSebastian Reichel return ERR_PTR(rc); 23540c79378cSSebastian Reichel } 23550c79378cSSebastian Reichel EXPORT_SYMBOL_GPL(spi_new_ancillary_device); 23560c79378cSSebastian Reichel 235764bee4d2SMika Westerberg #ifdef CONFIG_ACPI 23584c3c5954SArd Biesheuvel struct acpi_spi_lookup { 23594c3c5954SArd Biesheuvel struct spi_controller *ctlr; 23604c3c5954SArd Biesheuvel u32 max_speed_hz; 23614c3c5954SArd Biesheuvel u32 mode; 23624c3c5954SArd Biesheuvel int irq; 23634c3c5954SArd Biesheuvel u8 bits_per_word; 23644c3c5954SArd Biesheuvel u8 chip_select; 236587e59b36SStefan Binding int n; 236687e59b36SStefan Binding int index; 23674c3c5954SArd Biesheuvel }; 23684c3c5954SArd Biesheuvel 2369e612af7aSStefan Binding static int acpi_spi_count(struct acpi_resource *ares, void *data) 2370e612af7aSStefan Binding { 2371e612af7aSStefan Binding struct acpi_resource_spi_serialbus *sb; 2372e612af7aSStefan Binding int *count = data; 2373e612af7aSStefan Binding 2374e612af7aSStefan Binding if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) 2375e612af7aSStefan Binding return 1; 2376e612af7aSStefan Binding 2377e612af7aSStefan Binding sb = &ares->data.spi_serial_bus; 2378e612af7aSStefan Binding if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_SPI) 2379e612af7aSStefan Binding return 1; 2380e612af7aSStefan Binding 2381e612af7aSStefan Binding *count = *count + 1; 2382e612af7aSStefan Binding 2383e612af7aSStefan Binding return 1; 2384e612af7aSStefan Binding } 2385e612af7aSStefan Binding 2386e612af7aSStefan Binding /** 2387e612af7aSStefan Binding * acpi_spi_count_resources - Count the number of SpiSerialBus resources 2388e612af7aSStefan Binding * @adev: ACPI device 2389e612af7aSStefan Binding * 2390e612af7aSStefan Binding * Returns the number of SpiSerialBus resources in the ACPI-device's 2391e612af7aSStefan Binding * resource-list; or a negative error code. 2392e612af7aSStefan Binding */ 2393e612af7aSStefan Binding int acpi_spi_count_resources(struct acpi_device *adev) 2394e612af7aSStefan Binding { 2395e612af7aSStefan Binding LIST_HEAD(r); 2396e612af7aSStefan Binding int count = 0; 2397e612af7aSStefan Binding int ret; 2398e612af7aSStefan Binding 2399e612af7aSStefan Binding ret = acpi_dev_get_resources(adev, &r, acpi_spi_count, &count); 2400e612af7aSStefan Binding if (ret < 0) 2401e612af7aSStefan Binding return ret; 2402e612af7aSStefan Binding 2403e612af7aSStefan Binding acpi_dev_free_resource_list(&r); 2404e612af7aSStefan Binding 2405e612af7aSStefan Binding return count; 2406e612af7aSStefan Binding } 2407e612af7aSStefan Binding EXPORT_SYMBOL_GPL(acpi_spi_count_resources); 2408e612af7aSStefan Binding 24094c3c5954SArd Biesheuvel static void acpi_spi_parse_apple_properties(struct acpi_device *dev, 24104c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup) 24118a2e487eSLukas Wunner { 24128a2e487eSLukas Wunner const union acpi_object *obj; 24138a2e487eSLukas Wunner 24148a2e487eSLukas Wunner if (!x86_apple_machine) 24158a2e487eSLukas Wunner return; 24168a2e487eSLukas Wunner 24178a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 24188a2e487eSLukas Wunner && obj->buffer.length >= 4) 24194c3c5954SArd Biesheuvel lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 24208a2e487eSLukas Wunner 24218a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 24228a2e487eSLukas Wunner && obj->buffer.length == 8) 24234c3c5954SArd Biesheuvel lookup->bits_per_word = *(u64 *)obj->buffer.pointer; 24248a2e487eSLukas Wunner 24258a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 24268a2e487eSLukas Wunner && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 24274c3c5954SArd Biesheuvel lookup->mode |= SPI_LSB_FIRST; 24288a2e487eSLukas Wunner 24298a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 24308a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 24314c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 24328a2e487eSLukas Wunner 24338a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 24348a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 24354c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 24368a2e487eSLukas Wunner } 24378a2e487eSLukas Wunner 243887e59b36SStefan Binding static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev); 243987e59b36SStefan Binding 244064bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 244164bee4d2SMika Westerberg { 24424c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup = data; 24434c3c5954SArd Biesheuvel struct spi_controller *ctlr = lookup->ctlr; 244464bee4d2SMika Westerberg 244564bee4d2SMika Westerberg if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 244664bee4d2SMika Westerberg struct acpi_resource_spi_serialbus *sb; 24474c3c5954SArd Biesheuvel acpi_handle parent_handle; 24484c3c5954SArd Biesheuvel acpi_status status; 244964bee4d2SMika Westerberg 245064bee4d2SMika Westerberg sb = &ares->data.spi_serial_bus; 245164bee4d2SMika Westerberg if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 24524c3c5954SArd Biesheuvel 245387e59b36SStefan Binding if (lookup->index != -1 && lookup->n++ != lookup->index) 245487e59b36SStefan Binding return 1; 245587e59b36SStefan Binding 24564c3c5954SArd Biesheuvel status = acpi_get_handle(NULL, 24574c3c5954SArd Biesheuvel sb->resource_source.string_ptr, 24584c3c5954SArd Biesheuvel &parent_handle); 24594c3c5954SArd Biesheuvel 246087e59b36SStefan Binding if (ACPI_FAILURE(status)) 24614c3c5954SArd Biesheuvel return -ENODEV; 24624c3c5954SArd Biesheuvel 246387e59b36SStefan Binding if (ctlr) { 246487e59b36SStefan Binding if (ACPI_HANDLE(ctlr->dev.parent) != parent_handle) 246587e59b36SStefan Binding return -ENODEV; 246687e59b36SStefan Binding } else { 246787e59b36SStefan Binding struct acpi_device *adev; 246887e59b36SStefan Binding 2469ac2a3feeSRafael J. Wysocki adev = acpi_fetch_acpi_dev(parent_handle); 2470ac2a3feeSRafael J. Wysocki if (!adev) 247187e59b36SStefan Binding return -ENODEV; 247287e59b36SStefan Binding 247387e59b36SStefan Binding ctlr = acpi_spi_find_controller_by_adev(adev); 247487e59b36SStefan Binding if (!ctlr) 24759c22ec4aSAndy Shevchenko return -EPROBE_DEFER; 247687e59b36SStefan Binding 247787e59b36SStefan Binding lookup->ctlr = ctlr; 247887e59b36SStefan Binding } 247987e59b36SStefan Binding 2480a0a90718SMika Westerberg /* 2481a0a90718SMika Westerberg * ACPI DeviceSelection numbering is handled by the 2482a0a90718SMika Westerberg * host controller driver in Windows and can vary 2483a0a90718SMika Westerberg * from driver to driver. In Linux we always expect 2484a0a90718SMika Westerberg * 0 .. max - 1 so we need to ask the driver to 2485a0a90718SMika Westerberg * translate between the two schemes. 2486a0a90718SMika Westerberg */ 24878caab75fSGeert Uytterhoeven if (ctlr->fw_translate_cs) { 24888caab75fSGeert Uytterhoeven int cs = ctlr->fw_translate_cs(ctlr, 2489a0a90718SMika Westerberg sb->device_selection); 2490a0a90718SMika Westerberg if (cs < 0) 2491a0a90718SMika Westerberg return cs; 24924c3c5954SArd Biesheuvel lookup->chip_select = cs; 2493a0a90718SMika Westerberg } else { 24944c3c5954SArd Biesheuvel lookup->chip_select = sb->device_selection; 2495a0a90718SMika Westerberg } 2496a0a90718SMika Westerberg 24974c3c5954SArd Biesheuvel lookup->max_speed_hz = sb->connection_speed; 24980dadde34SAndy Shevchenko lookup->bits_per_word = sb->data_bit_length; 249964bee4d2SMika Westerberg 250064bee4d2SMika Westerberg if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 25014c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 250264bee4d2SMika Westerberg if (sb->clock_polarity == ACPI_SPI_START_HIGH) 25034c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 250464bee4d2SMika Westerberg if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 25054c3c5954SArd Biesheuvel lookup->mode |= SPI_CS_HIGH; 250664bee4d2SMika Westerberg } 25074c3c5954SArd Biesheuvel } else if (lookup->irq < 0) { 250864bee4d2SMika Westerberg struct resource r; 250964bee4d2SMika Westerberg 251064bee4d2SMika Westerberg if (acpi_dev_resource_interrupt(ares, 0, &r)) 25114c3c5954SArd Biesheuvel lookup->irq = r.start; 251264bee4d2SMika Westerberg } 251364bee4d2SMika Westerberg 251464bee4d2SMika Westerberg /* Always tell the ACPI core to skip this resource */ 251564bee4d2SMika Westerberg return 1; 251664bee4d2SMika Westerberg } 251764bee4d2SMika Westerberg 2518000bee0eSStefan Binding /** 2519000bee0eSStefan Binding * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information 2520000bee0eSStefan Binding * @ctlr: controller to which the spi device belongs 2521000bee0eSStefan Binding * @adev: ACPI Device for the spi device 252287e59b36SStefan Binding * @index: Index of the spi resource inside the ACPI Node 2523000bee0eSStefan Binding * 2524000bee0eSStefan Binding * This should be used to allocate a new spi device from and ACPI Node. 2525000bee0eSStefan Binding * The caller is responsible for calling spi_add_device to register the spi device. 2526000bee0eSStefan Binding * 252787e59b36SStefan Binding * If ctlr is set to NULL, the Controller for the spi device will be looked up 252887e59b36SStefan Binding * using the resource. 252987e59b36SStefan Binding * If index is set to -1, index is not used. 253087e59b36SStefan Binding * Note: If index is -1, ctlr must be set. 253187e59b36SStefan Binding * 2532000bee0eSStefan Binding * Return: a pointer to the new device, or ERR_PTR on error. 2533000bee0eSStefan Binding */ 2534000bee0eSStefan Binding struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr, 253587e59b36SStefan Binding struct acpi_device *adev, 253687e59b36SStefan Binding int index) 253764bee4d2SMika Westerberg { 25384c3c5954SArd Biesheuvel acpi_handle parent_handle = NULL; 253964bee4d2SMika Westerberg struct list_head resource_list; 2540b28944c6SArd Biesheuvel struct acpi_spi_lookup lookup = {}; 254164bee4d2SMika Westerberg struct spi_device *spi; 254264bee4d2SMika Westerberg int ret; 254364bee4d2SMika Westerberg 254487e59b36SStefan Binding if (!ctlr && index == -1) 254587e59b36SStefan Binding return ERR_PTR(-EINVAL); 254687e59b36SStefan Binding 25474c3c5954SArd Biesheuvel lookup.ctlr = ctlr; 25484c3c5954SArd Biesheuvel lookup.irq = -1; 254987e59b36SStefan Binding lookup.index = index; 255087e59b36SStefan Binding lookup.n = 0; 25514c3c5954SArd Biesheuvel 25524c3c5954SArd Biesheuvel INIT_LIST_HEAD(&resource_list); 25534c3c5954SArd Biesheuvel ret = acpi_dev_get_resources(adev, &resource_list, 25544c3c5954SArd Biesheuvel acpi_spi_add_resource, &lookup); 25554c3c5954SArd Biesheuvel acpi_dev_free_resource_list(&resource_list); 25564c3c5954SArd Biesheuvel 25574c3c5954SArd Biesheuvel if (ret < 0) 255895c8222fSDavid Jander /* Found SPI in _CRS but it points to another controller */ 2559b6747f4fSAndy Shevchenko return ERR_PTR(ret); 25604c3c5954SArd Biesheuvel 25614c3c5954SArd Biesheuvel if (!lookup.max_speed_hz && 256210e92724SBjorn Helgaas ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) && 256387e59b36SStefan Binding ACPI_HANDLE(lookup.ctlr->dev.parent) == parent_handle) { 25644c3c5954SArd Biesheuvel /* Apple does not use _CRS but nested devices for SPI slaves */ 25654c3c5954SArd Biesheuvel acpi_spi_parse_apple_properties(adev, &lookup); 25664c3c5954SArd Biesheuvel } 25674c3c5954SArd Biesheuvel 25684c3c5954SArd Biesheuvel if (!lookup.max_speed_hz) 2569000bee0eSStefan Binding return ERR_PTR(-ENODEV); 25704c3c5954SArd Biesheuvel 257187e59b36SStefan Binding spi = spi_alloc_device(lookup.ctlr); 257264bee4d2SMika Westerberg if (!spi) { 257387e59b36SStefan Binding dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n", 257464bee4d2SMika Westerberg dev_name(&adev->dev)); 2575000bee0eSStefan Binding return ERR_PTR(-ENOMEM); 257664bee4d2SMika Westerberg } 257764bee4d2SMika Westerberg 25787b199811SRafael J. Wysocki ACPI_COMPANION_SET(&spi->dev, adev); 25794c3c5954SArd Biesheuvel spi->max_speed_hz = lookup.max_speed_hz; 2580ea235786SJohn Garry spi->mode |= lookup.mode; 25814c3c5954SArd Biesheuvel spi->irq = lookup.irq; 25824c3c5954SArd Biesheuvel spi->bits_per_word = lookup.bits_per_word; 25834c3c5954SArd Biesheuvel spi->chip_select = lookup.chip_select; 258464bee4d2SMika Westerberg 2585000bee0eSStefan Binding return spi; 2586000bee0eSStefan Binding } 2587000bee0eSStefan Binding EXPORT_SYMBOL_GPL(acpi_spi_device_alloc); 2588000bee0eSStefan Binding 2589000bee0eSStefan Binding static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 2590000bee0eSStefan Binding struct acpi_device *adev) 2591000bee0eSStefan Binding { 2592000bee0eSStefan Binding struct spi_device *spi; 2593000bee0eSStefan Binding 2594000bee0eSStefan Binding if (acpi_bus_get_status(adev) || !adev->status.present || 2595000bee0eSStefan Binding acpi_device_enumerated(adev)) 2596000bee0eSStefan Binding return AE_OK; 2597000bee0eSStefan Binding 259887e59b36SStefan Binding spi = acpi_spi_device_alloc(ctlr, adev, -1); 2599000bee0eSStefan Binding if (IS_ERR(spi)) { 2600000bee0eSStefan Binding if (PTR_ERR(spi) == -ENOMEM) 2601000bee0eSStefan Binding return AE_NO_MEMORY; 2602000bee0eSStefan Binding else 2603000bee0eSStefan Binding return AE_OK; 2604000bee0eSStefan Binding } 2605000bee0eSStefan Binding 26060c6543f6SDan O'Donovan acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 26070c6543f6SDan O'Donovan sizeof(spi->modalias)); 26080c6543f6SDan O'Donovan 260933ada67dSChristophe RICARD if (spi->irq < 0) 261033ada67dSChristophe RICARD spi->irq = acpi_dev_gpio_irq_get(adev, 0); 261133ada67dSChristophe RICARD 26127f24467fSOctavian Purdila acpi_device_set_enumerated(adev); 26137f24467fSOctavian Purdila 261433cf00e5SMika Westerberg adev->power.flags.ignore_parent = true; 261564bee4d2SMika Westerberg if (spi_add_device(spi)) { 261633cf00e5SMika Westerberg adev->power.flags.ignore_parent = false; 26178caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 261864bee4d2SMika Westerberg dev_name(&adev->dev)); 261964bee4d2SMika Westerberg spi_dev_put(spi); 262064bee4d2SMika Westerberg } 262164bee4d2SMika Westerberg 262264bee4d2SMika Westerberg return AE_OK; 262364bee4d2SMika Westerberg } 262464bee4d2SMika Westerberg 26257f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 26267f24467fSOctavian Purdila void *data, void **return_value) 26277f24467fSOctavian Purdila { 26287030c428SRafael J. Wysocki struct acpi_device *adev = acpi_fetch_acpi_dev(handle); 26298caab75fSGeert Uytterhoeven struct spi_controller *ctlr = data; 26307f24467fSOctavian Purdila 26317030c428SRafael J. Wysocki if (!adev) 26327f24467fSOctavian Purdila return AE_OK; 26337f24467fSOctavian Purdila 26348caab75fSGeert Uytterhoeven return acpi_register_spi_device(ctlr, adev); 26357f24467fSOctavian Purdila } 26367f24467fSOctavian Purdila 26374c3c5954SArd Biesheuvel #define SPI_ACPI_ENUMERATE_MAX_DEPTH 32 26384c3c5954SArd Biesheuvel 26398caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr) 264064bee4d2SMika Westerberg { 264164bee4d2SMika Westerberg acpi_status status; 264264bee4d2SMika Westerberg acpi_handle handle; 264364bee4d2SMika Westerberg 26448caab75fSGeert Uytterhoeven handle = ACPI_HANDLE(ctlr->dev.parent); 264564bee4d2SMika Westerberg if (!handle) 264664bee4d2SMika Westerberg return; 264764bee4d2SMika Westerberg 26484c3c5954SArd Biesheuvel status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 26494c3c5954SArd Biesheuvel SPI_ACPI_ENUMERATE_MAX_DEPTH, 26508caab75fSGeert Uytterhoeven acpi_spi_add_device, NULL, ctlr, NULL); 265164bee4d2SMika Westerberg if (ACPI_FAILURE(status)) 26528caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 265364bee4d2SMika Westerberg } 265464bee4d2SMika Westerberg #else 26558caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 265664bee4d2SMika Westerberg #endif /* CONFIG_ACPI */ 265764bee4d2SMika Westerberg 26588caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev) 26598ae12a0dSDavid Brownell { 26608caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 26618ae12a0dSDavid Brownell 26628caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 26638caab75fSGeert Uytterhoeven kfree(ctlr); 26648ae12a0dSDavid Brownell } 26658ae12a0dSDavid Brownell 26668ae12a0dSDavid Brownell static struct class spi_master_class = { 26678ae12a0dSDavid Brownell .name = "spi_master", 26688ae12a0dSDavid Brownell .owner = THIS_MODULE, 26698caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 2670eca2ebc7SMartin Sperl .dev_groups = spi_master_groups, 26718ae12a0dSDavid Brownell }; 26728ae12a0dSDavid Brownell 26736c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE 26746c364062SGeert Uytterhoeven /** 26756c364062SGeert Uytterhoeven * spi_slave_abort - abort the ongoing transfer request on an SPI slave 26766c364062SGeert Uytterhoeven * controller 26776c364062SGeert Uytterhoeven * @spi: device used for the current transfer 26786c364062SGeert Uytterhoeven */ 26796c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi) 26806c364062SGeert Uytterhoeven { 26818caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 26826c364062SGeert Uytterhoeven 26838caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 26848caab75fSGeert Uytterhoeven return ctlr->slave_abort(ctlr); 26856c364062SGeert Uytterhoeven 26866c364062SGeert Uytterhoeven return -ENOTSUPP; 26876c364062SGeert Uytterhoeven } 26886c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort); 26896c364062SGeert Uytterhoeven 2690cc8b4659SGeert Uytterhoeven static ssize_t slave_show(struct device *dev, struct device_attribute *attr, 2691cc8b4659SGeert Uytterhoeven char *buf) 26926c364062SGeert Uytterhoeven { 26938caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 26948caab75fSGeert Uytterhoeven dev); 26956c364062SGeert Uytterhoeven struct device *child; 26966c364062SGeert Uytterhoeven 2697c21b0837SAndy Shevchenko child = device_find_any_child(&ctlr->dev); 26986c364062SGeert Uytterhoeven return sprintf(buf, "%s\n", 26996c364062SGeert Uytterhoeven child ? to_spi_device(child)->modalias : NULL); 27006c364062SGeert Uytterhoeven } 27016c364062SGeert Uytterhoeven 2702cc8b4659SGeert Uytterhoeven static ssize_t slave_store(struct device *dev, struct device_attribute *attr, 2703cc8b4659SGeert Uytterhoeven const char *buf, size_t count) 27046c364062SGeert Uytterhoeven { 27058caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 27068caab75fSGeert Uytterhoeven dev); 27076c364062SGeert Uytterhoeven struct spi_device *spi; 27086c364062SGeert Uytterhoeven struct device *child; 27096c364062SGeert Uytterhoeven char name[32]; 27106c364062SGeert Uytterhoeven int rc; 27116c364062SGeert Uytterhoeven 27126c364062SGeert Uytterhoeven rc = sscanf(buf, "%31s", name); 27136c364062SGeert Uytterhoeven if (rc != 1 || !name[0]) 27146c364062SGeert Uytterhoeven return -EINVAL; 27156c364062SGeert Uytterhoeven 2716c21b0837SAndy Shevchenko child = device_find_any_child(&ctlr->dev); 27176c364062SGeert Uytterhoeven if (child) { 27186c364062SGeert Uytterhoeven /* Remove registered slave */ 27196c364062SGeert Uytterhoeven device_unregister(child); 27206c364062SGeert Uytterhoeven put_device(child); 27216c364062SGeert Uytterhoeven } 27226c364062SGeert Uytterhoeven 27236c364062SGeert Uytterhoeven if (strcmp(name, "(null)")) { 27246c364062SGeert Uytterhoeven /* Register new slave */ 27256c364062SGeert Uytterhoeven spi = spi_alloc_device(ctlr); 27266c364062SGeert Uytterhoeven if (!spi) 27276c364062SGeert Uytterhoeven return -ENOMEM; 27286c364062SGeert Uytterhoeven 27296c364062SGeert Uytterhoeven strlcpy(spi->modalias, name, sizeof(spi->modalias)); 27306c364062SGeert Uytterhoeven 27316c364062SGeert Uytterhoeven rc = spi_add_device(spi); 27326c364062SGeert Uytterhoeven if (rc) { 27336c364062SGeert Uytterhoeven spi_dev_put(spi); 27346c364062SGeert Uytterhoeven return rc; 27356c364062SGeert Uytterhoeven } 27366c364062SGeert Uytterhoeven } 27376c364062SGeert Uytterhoeven 27386c364062SGeert Uytterhoeven return count; 27396c364062SGeert Uytterhoeven } 27406c364062SGeert Uytterhoeven 2741cc8b4659SGeert Uytterhoeven static DEVICE_ATTR_RW(slave); 27426c364062SGeert Uytterhoeven 27436c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = { 27446c364062SGeert Uytterhoeven &dev_attr_slave.attr, 27456c364062SGeert Uytterhoeven NULL, 27466c364062SGeert Uytterhoeven }; 27476c364062SGeert Uytterhoeven 27486c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = { 27496c364062SGeert Uytterhoeven .attrs = spi_slave_attrs, 27506c364062SGeert Uytterhoeven }; 27516c364062SGeert Uytterhoeven 27526c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = { 27538caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 27546c364062SGeert Uytterhoeven &spi_slave_group, 27556c364062SGeert Uytterhoeven NULL, 27566c364062SGeert Uytterhoeven }; 27576c364062SGeert Uytterhoeven 27586c364062SGeert Uytterhoeven static struct class spi_slave_class = { 27596c364062SGeert Uytterhoeven .name = "spi_slave", 27606c364062SGeert Uytterhoeven .owner = THIS_MODULE, 27618caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 27626c364062SGeert Uytterhoeven .dev_groups = spi_slave_groups, 27636c364062SGeert Uytterhoeven }; 27646c364062SGeert Uytterhoeven #else 27656c364062SGeert Uytterhoeven extern struct class spi_slave_class; /* dummy */ 27666c364062SGeert Uytterhoeven #endif 27678ae12a0dSDavid Brownell 27688ae12a0dSDavid Brownell /** 27696c364062SGeert Uytterhoeven * __spi_alloc_controller - allocate an SPI master or slave controller 27708ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 277133e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 2772229e6af1SLukas Wunner * memory is in the driver_data field of the returned device, accessible 2773229e6af1SLukas Wunner * with spi_controller_get_devdata(); the memory is cacheline aligned; 2774229e6af1SLukas Wunner * drivers granting DMA access to portions of their private data need to 2775229e6af1SLukas Wunner * round up @size using ALIGN(size, dma_get_cache_alignment()). 27766c364062SGeert Uytterhoeven * @slave: flag indicating whether to allocate an SPI master (false) or SPI 27776c364062SGeert Uytterhoeven * slave (true) controller 277833e34dc6SDavid Brownell * Context: can sleep 27798ae12a0dSDavid Brownell * 27806c364062SGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 27818ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 27828caab75fSGeert Uytterhoeven * an spi_controller structure, prior to calling spi_register_controller(). 27838ae12a0dSDavid Brownell * 278497d56dc6SJavier Martinez Canillas * This must be called from context that can sleep. 27858ae12a0dSDavid Brownell * 27866c364062SGeert Uytterhoeven * The caller is responsible for assigning the bus number and initializing the 27878caab75fSGeert Uytterhoeven * controller's methods before calling spi_register_controller(); and (after 27888caab75fSGeert Uytterhoeven * errors adding the device) calling spi_controller_put() to prevent a memory 27898caab75fSGeert Uytterhoeven * leak. 279097d56dc6SJavier Martinez Canillas * 27916c364062SGeert Uytterhoeven * Return: the SPI controller structure on success, else NULL. 27928ae12a0dSDavid Brownell */ 27938caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev, 27946c364062SGeert Uytterhoeven unsigned int size, bool slave) 27958ae12a0dSDavid Brownell { 27968caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 2797229e6af1SLukas Wunner size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment()); 27988ae12a0dSDavid Brownell 27990c868461SDavid Brownell if (!dev) 28000c868461SDavid Brownell return NULL; 28010c868461SDavid Brownell 2802229e6af1SLukas Wunner ctlr = kzalloc(size + ctlr_size, GFP_KERNEL); 28038caab75fSGeert Uytterhoeven if (!ctlr) 28048ae12a0dSDavid Brownell return NULL; 28058ae12a0dSDavid Brownell 28068caab75fSGeert Uytterhoeven device_initialize(&ctlr->dev); 280716a8e2fbSUwe Kleine-König INIT_LIST_HEAD(&ctlr->queue); 280816a8e2fbSUwe Kleine-König spin_lock_init(&ctlr->queue_lock); 280916a8e2fbSUwe Kleine-König spin_lock_init(&ctlr->bus_lock_spinlock); 281016a8e2fbSUwe Kleine-König mutex_init(&ctlr->bus_lock_mutex); 281116a8e2fbSUwe Kleine-König mutex_init(&ctlr->io_mutex); 281216a8e2fbSUwe Kleine-König mutex_init(&ctlr->add_lock); 28138caab75fSGeert Uytterhoeven ctlr->bus_num = -1; 28148caab75fSGeert Uytterhoeven ctlr->num_chipselect = 1; 28158caab75fSGeert Uytterhoeven ctlr->slave = slave; 28166c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 28178caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_slave_class; 28186c364062SGeert Uytterhoeven else 28198caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_master_class; 28208caab75fSGeert Uytterhoeven ctlr->dev.parent = dev; 28218caab75fSGeert Uytterhoeven pm_suspend_ignore_children(&ctlr->dev, true); 2822229e6af1SLukas Wunner spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size); 28238ae12a0dSDavid Brownell 28248caab75fSGeert Uytterhoeven return ctlr; 28258ae12a0dSDavid Brownell } 28266c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller); 28278ae12a0dSDavid Brownell 28285e844cc3SLukas Wunner static void devm_spi_release_controller(struct device *dev, void *ctlr) 28295e844cc3SLukas Wunner { 28305e844cc3SLukas Wunner spi_controller_put(*(struct spi_controller **)ctlr); 28315e844cc3SLukas Wunner } 28325e844cc3SLukas Wunner 28335e844cc3SLukas Wunner /** 28345e844cc3SLukas Wunner * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller() 28355e844cc3SLukas Wunner * @dev: physical device of SPI controller 28365e844cc3SLukas Wunner * @size: how much zeroed driver-private data to allocate 28375e844cc3SLukas Wunner * @slave: whether to allocate an SPI master (false) or SPI slave (true) 28385e844cc3SLukas Wunner * Context: can sleep 28395e844cc3SLukas Wunner * 28405e844cc3SLukas Wunner * Allocate an SPI controller and automatically release a reference on it 28415e844cc3SLukas Wunner * when @dev is unbound from its driver. Drivers are thus relieved from 28425e844cc3SLukas Wunner * having to call spi_controller_put(). 28435e844cc3SLukas Wunner * 28445e844cc3SLukas Wunner * The arguments to this function are identical to __spi_alloc_controller(). 28455e844cc3SLukas Wunner * 28465e844cc3SLukas Wunner * Return: the SPI controller structure on success, else NULL. 28475e844cc3SLukas Wunner */ 28485e844cc3SLukas Wunner struct spi_controller *__devm_spi_alloc_controller(struct device *dev, 28495e844cc3SLukas Wunner unsigned int size, 28505e844cc3SLukas Wunner bool slave) 28515e844cc3SLukas Wunner { 28525e844cc3SLukas Wunner struct spi_controller **ptr, *ctlr; 28535e844cc3SLukas Wunner 28545e844cc3SLukas Wunner ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr), 28555e844cc3SLukas Wunner GFP_KERNEL); 28565e844cc3SLukas Wunner if (!ptr) 28575e844cc3SLukas Wunner return NULL; 28585e844cc3SLukas Wunner 28595e844cc3SLukas Wunner ctlr = __spi_alloc_controller(dev, size, slave); 28605e844cc3SLukas Wunner if (ctlr) { 2861794aaf01SWilliam A. Kennington III ctlr->devm_allocated = true; 28625e844cc3SLukas Wunner *ptr = ctlr; 28635e844cc3SLukas Wunner devres_add(dev, ptr); 28645e844cc3SLukas Wunner } else { 28655e844cc3SLukas Wunner devres_free(ptr); 28665e844cc3SLukas Wunner } 28675e844cc3SLukas Wunner 28685e844cc3SLukas Wunner return ctlr; 28695e844cc3SLukas Wunner } 28705e844cc3SLukas Wunner EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller); 28715e844cc3SLukas Wunner 2872f3186dd8SLinus Walleij /** 2873f3186dd8SLinus Walleij * spi_get_gpio_descs() - grab chip select GPIOs for the master 2874f3186dd8SLinus Walleij * @ctlr: The SPI master to grab GPIO descriptors for 2875f3186dd8SLinus Walleij */ 2876f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr) 2877f3186dd8SLinus Walleij { 2878f3186dd8SLinus Walleij int nb, i; 2879f3186dd8SLinus Walleij struct gpio_desc **cs; 2880f3186dd8SLinus Walleij struct device *dev = &ctlr->dev; 28817d93aecdSGeert Uytterhoeven unsigned long native_cs_mask = 0; 28827d93aecdSGeert Uytterhoeven unsigned int num_cs_gpios = 0; 2883f3186dd8SLinus Walleij 2884f3186dd8SLinus Walleij nb = gpiod_count(dev, "cs"); 288531ed8ebcSAndy Shevchenko if (nb < 0) { 2886f3186dd8SLinus Walleij /* No GPIOs at all is fine, else return the error */ 288731ed8ebcSAndy Shevchenko if (nb == -ENOENT) 2888f3186dd8SLinus Walleij return 0; 2889f3186dd8SLinus Walleij return nb; 289031ed8ebcSAndy Shevchenko } 289131ed8ebcSAndy Shevchenko 289231ed8ebcSAndy Shevchenko ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2893f3186dd8SLinus Walleij 2894f3186dd8SLinus Walleij cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), 2895f3186dd8SLinus Walleij GFP_KERNEL); 2896f3186dd8SLinus Walleij if (!cs) 2897f3186dd8SLinus Walleij return -ENOMEM; 2898f3186dd8SLinus Walleij ctlr->cs_gpiods = cs; 2899f3186dd8SLinus Walleij 2900f3186dd8SLinus Walleij for (i = 0; i < nb; i++) { 2901f3186dd8SLinus Walleij /* 2902f3186dd8SLinus Walleij * Most chipselects are active low, the inverted 2903f3186dd8SLinus Walleij * semantics are handled by special quirks in gpiolib, 2904f3186dd8SLinus Walleij * so initializing them GPIOD_OUT_LOW here means 2905f3186dd8SLinus Walleij * "unasserted", in most cases this will drive the physical 2906f3186dd8SLinus Walleij * line high. 2907f3186dd8SLinus Walleij */ 2908f3186dd8SLinus Walleij cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, 2909f3186dd8SLinus Walleij GPIOD_OUT_LOW); 29101723fdecSGeert Uytterhoeven if (IS_ERR(cs[i])) 29111723fdecSGeert Uytterhoeven return PTR_ERR(cs[i]); 2912f3186dd8SLinus Walleij 2913f3186dd8SLinus Walleij if (cs[i]) { 2914f3186dd8SLinus Walleij /* 2915f3186dd8SLinus Walleij * If we find a CS GPIO, name it after the device and 2916f3186dd8SLinus Walleij * chip select line. 2917f3186dd8SLinus Walleij */ 2918f3186dd8SLinus Walleij char *gpioname; 2919f3186dd8SLinus Walleij 2920f3186dd8SLinus Walleij gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", 2921f3186dd8SLinus Walleij dev_name(dev), i); 2922f3186dd8SLinus Walleij if (!gpioname) 2923f3186dd8SLinus Walleij return -ENOMEM; 2924f3186dd8SLinus Walleij gpiod_set_consumer_name(cs[i], gpioname); 29257d93aecdSGeert Uytterhoeven num_cs_gpios++; 29267d93aecdSGeert Uytterhoeven continue; 2927f3186dd8SLinus Walleij } 29287d93aecdSGeert Uytterhoeven 29297d93aecdSGeert Uytterhoeven if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { 29307d93aecdSGeert Uytterhoeven dev_err(dev, "Invalid native chip select %d\n", i); 29317d93aecdSGeert Uytterhoeven return -EINVAL; 29327d93aecdSGeert Uytterhoeven } 29337d93aecdSGeert Uytterhoeven native_cs_mask |= BIT(i); 29347d93aecdSGeert Uytterhoeven } 29357d93aecdSGeert Uytterhoeven 2936f60d7270SAndy Shevchenko ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; 2937dbaca8e5SAndy Shevchenko 2938dbaca8e5SAndy Shevchenko if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && 2939dbaca8e5SAndy Shevchenko ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { 29407d93aecdSGeert Uytterhoeven dev_err(dev, "No unused native chip select available\n"); 29417d93aecdSGeert Uytterhoeven return -EINVAL; 2942f3186dd8SLinus Walleij } 2943f3186dd8SLinus Walleij 2944f3186dd8SLinus Walleij return 0; 2945f3186dd8SLinus Walleij } 2946f3186dd8SLinus Walleij 2947bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr) 2948bdf3a3b5SBoris Brezillon { 2949bdf3a3b5SBoris Brezillon /* 2950b5932f5cSBoris Brezillon * The controller may implement only the high-level SPI-memory like 2951b5932f5cSBoris Brezillon * operations if it does not support regular SPI transfers, and this is 2952b5932f5cSBoris Brezillon * valid use case. 2953b5932f5cSBoris Brezillon * If ->mem_ops is NULL, we request that at least one of the 2954b5932f5cSBoris Brezillon * ->transfer_xxx() method be implemented. 2955bdf3a3b5SBoris Brezillon */ 2956b5932f5cSBoris Brezillon if (ctlr->mem_ops) { 2957b5932f5cSBoris Brezillon if (!ctlr->mem_ops->exec_op) 2958bdf3a3b5SBoris Brezillon return -EINVAL; 2959b5932f5cSBoris Brezillon } else if (!ctlr->transfer && !ctlr->transfer_one && 2960b5932f5cSBoris Brezillon !ctlr->transfer_one_message) { 2961b5932f5cSBoris Brezillon return -EINVAL; 2962b5932f5cSBoris Brezillon } 2963bdf3a3b5SBoris Brezillon 2964bdf3a3b5SBoris Brezillon return 0; 2965bdf3a3b5SBoris Brezillon } 2966bdf3a3b5SBoris Brezillon 29678ae12a0dSDavid Brownell /** 29688caab75fSGeert Uytterhoeven * spi_register_controller - register SPI master or slave controller 29698caab75fSGeert Uytterhoeven * @ctlr: initialized master, originally from spi_alloc_master() or 29708caab75fSGeert Uytterhoeven * spi_alloc_slave() 297133e34dc6SDavid Brownell * Context: can sleep 29728ae12a0dSDavid Brownell * 29738caab75fSGeert Uytterhoeven * SPI controllers connect to their drivers using some non-SPI bus, 29748ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 29758caab75fSGeert Uytterhoeven * includes calling spi_register_controller() to hook up to this SPI bus glue. 29768ae12a0dSDavid Brownell * 29778ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 29788ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 29798ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 29808ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 29818ae12a0dSDavid Brownell * chip is at which address. 29828ae12a0dSDavid Brownell * 29838ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 29848caab75fSGeert Uytterhoeven * success, else a negative error code (dropping the controller's refcount). 29850c868461SDavid Brownell * After a successful return, the caller is responsible for calling 29868caab75fSGeert Uytterhoeven * spi_unregister_controller(). 298797d56dc6SJavier Martinez Canillas * 298897d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 29898ae12a0dSDavid Brownell */ 29908caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr) 29918ae12a0dSDavid Brownell { 29928caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 29932b9603a0SFeng Tang struct boardinfo *bi; 2994b93318a2SSergei Shtylyov int status; 299542bdd706SLucas Stach int id, first_dynamic; 29968ae12a0dSDavid Brownell 29970c868461SDavid Brownell if (!dev) 29980c868461SDavid Brownell return -ENODEV; 29990c868461SDavid Brownell 3000bdf3a3b5SBoris Brezillon /* 3001bdf3a3b5SBoris Brezillon * Make sure all necessary hooks are implemented before registering 3002bdf3a3b5SBoris Brezillon * the SPI controller. 3003bdf3a3b5SBoris Brezillon */ 3004bdf3a3b5SBoris Brezillon status = spi_controller_check_ops(ctlr); 3005bdf3a3b5SBoris Brezillon if (status) 3006bdf3a3b5SBoris Brezillon return status; 3007bdf3a3b5SBoris Brezillon 300804b2d03aSGeert Uytterhoeven if (ctlr->bus_num >= 0) { 300995c8222fSDavid Jander /* Devices with a fixed bus num must check-in with the num */ 301004b2d03aSGeert Uytterhoeven mutex_lock(&board_lock); 301104b2d03aSGeert Uytterhoeven id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 301204b2d03aSGeert Uytterhoeven ctlr->bus_num + 1, GFP_KERNEL); 301304b2d03aSGeert Uytterhoeven mutex_unlock(&board_lock); 301404b2d03aSGeert Uytterhoeven if (WARN(id < 0, "couldn't get idr")) 301504b2d03aSGeert Uytterhoeven return id == -ENOSPC ? -EBUSY : id; 301604b2d03aSGeert Uytterhoeven ctlr->bus_num = id; 301704b2d03aSGeert Uytterhoeven } else if (ctlr->dev.of_node) { 301895c8222fSDavid Jander /* Allocate dynamic bus number using Linux idr */ 30199b61e302SSuniel Mahesh id = of_alias_get_id(ctlr->dev.of_node, "spi"); 30209b61e302SSuniel Mahesh if (id >= 0) { 30219b61e302SSuniel Mahesh ctlr->bus_num = id; 30229b61e302SSuniel Mahesh mutex_lock(&board_lock); 30239b61e302SSuniel Mahesh id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 30249b61e302SSuniel Mahesh ctlr->bus_num + 1, GFP_KERNEL); 30259b61e302SSuniel Mahesh mutex_unlock(&board_lock); 30269b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 30279b61e302SSuniel Mahesh return id == -ENOSPC ? -EBUSY : id; 30289b61e302SSuniel Mahesh } 30299b61e302SSuniel Mahesh } 30308caab75fSGeert Uytterhoeven if (ctlr->bus_num < 0) { 303142bdd706SLucas Stach first_dynamic = of_alias_get_highest_id("spi"); 303242bdd706SLucas Stach if (first_dynamic < 0) 303342bdd706SLucas Stach first_dynamic = 0; 303442bdd706SLucas Stach else 303542bdd706SLucas Stach first_dynamic++; 303642bdd706SLucas Stach 30379b61e302SSuniel Mahesh mutex_lock(&board_lock); 303842bdd706SLucas Stach id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, 303942bdd706SLucas Stach 0, GFP_KERNEL); 30409b61e302SSuniel Mahesh mutex_unlock(&board_lock); 30419b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 30429b61e302SSuniel Mahesh return id; 30439b61e302SSuniel Mahesh ctlr->bus_num = id; 30448ae12a0dSDavid Brownell } 30458caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 30468caab75fSGeert Uytterhoeven init_completion(&ctlr->xfer_completion); 304769fa9590SDavid Jander init_completion(&ctlr->cur_msg_completion); 30488caab75fSGeert Uytterhoeven if (!ctlr->max_dma_len) 30498caab75fSGeert Uytterhoeven ctlr->max_dma_len = INT_MAX; 3050cf32b71eSErnst Schwab 3051350de7ceSAndy Shevchenko /* 3052350de7ceSAndy Shevchenko * Register the device, then userspace will see it. 3053350de7ceSAndy Shevchenko * Registration fails if the bus ID is in use. 30548ae12a0dSDavid Brownell */ 30558caab75fSGeert Uytterhoeven dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 30560a919ae4SAndrey Smirnov 3057f48dc6b9SLinus Walleij if (!spi_controller_is_slave(ctlr) && ctlr->use_gpio_descriptors) { 30580a919ae4SAndrey Smirnov status = spi_get_gpio_descs(ctlr); 30590a919ae4SAndrey Smirnov if (status) 3060f9981d4fSAaro Koskinen goto free_bus_id; 30610a919ae4SAndrey Smirnov /* 30620a919ae4SAndrey Smirnov * A controller using GPIO descriptors always 30630a919ae4SAndrey Smirnov * supports SPI_CS_HIGH if need be. 30640a919ae4SAndrey Smirnov */ 30650a919ae4SAndrey Smirnov ctlr->mode_bits |= SPI_CS_HIGH; 30660a919ae4SAndrey Smirnov } 30670a919ae4SAndrey Smirnov 3068f9481b08STudor Ambarus /* 3069f9481b08STudor Ambarus * Even if it's just one always-selected device, there must 3070f9481b08STudor Ambarus * be at least one chipselect. 3071f9481b08STudor Ambarus */ 3072f9981d4fSAaro Koskinen if (!ctlr->num_chipselect) { 3073f9981d4fSAaro Koskinen status = -EINVAL; 3074f9981d4fSAaro Koskinen goto free_bus_id; 3075f9981d4fSAaro Koskinen } 3076f9481b08STudor Ambarus 307795c8222fSDavid Jander /* Setting last_cs to -1 means no chip selected */ 30786bb477dfSYun Zhou ctlr->last_cs = -1; 30796bb477dfSYun Zhou 30808caab75fSGeert Uytterhoeven status = device_add(&ctlr->dev); 3081f9981d4fSAaro Koskinen if (status < 0) 3082f9981d4fSAaro Koskinen goto free_bus_id; 30839b61e302SSuniel Mahesh dev_dbg(dev, "registered %s %s\n", 30848caab75fSGeert Uytterhoeven spi_controller_is_slave(ctlr) ? "slave" : "master", 30859b61e302SSuniel Mahesh dev_name(&ctlr->dev)); 30868ae12a0dSDavid Brownell 3087b5932f5cSBoris Brezillon /* 3088b5932f5cSBoris Brezillon * If we're using a queued driver, start the queue. Note that we don't 3089b5932f5cSBoris Brezillon * need the queueing logic if the driver is only supporting high-level 3090b5932f5cSBoris Brezillon * memory operations. 3091b5932f5cSBoris Brezillon */ 3092b5932f5cSBoris Brezillon if (ctlr->transfer) { 30938caab75fSGeert Uytterhoeven dev_info(dev, "controller is unqueued, this is deprecated\n"); 3094b5932f5cSBoris Brezillon } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 30958caab75fSGeert Uytterhoeven status = spi_controller_initialize_queue(ctlr); 3096ffbbdd21SLinus Walleij if (status) { 30978caab75fSGeert Uytterhoeven device_del(&ctlr->dev); 3098f9981d4fSAaro Koskinen goto free_bus_id; 3099ffbbdd21SLinus Walleij } 3100ffbbdd21SLinus Walleij } 310195c8222fSDavid Jander /* Add statistics */ 31026598b91bSDavid Jander ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev); 31036598b91bSDavid Jander if (!ctlr->pcpu_statistics) { 31046598b91bSDavid Jander dev_err(dev, "Error allocating per-cpu statistics\n"); 3105d52b095bSDan Carpenter status = -ENOMEM; 31066598b91bSDavid Jander goto destroy_queue; 31076598b91bSDavid Jander } 3108ffbbdd21SLinus Walleij 31092b9603a0SFeng Tang mutex_lock(&board_lock); 31108caab75fSGeert Uytterhoeven list_add_tail(&ctlr->list, &spi_controller_list); 31112b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 31128caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 31132b9603a0SFeng Tang mutex_unlock(&board_lock); 31142b9603a0SFeng Tang 311564bee4d2SMika Westerberg /* Register devices from the device tree and ACPI */ 31168caab75fSGeert Uytterhoeven of_register_spi_devices(ctlr); 31178caab75fSGeert Uytterhoeven acpi_register_spi_devices(ctlr); 3118f9981d4fSAaro Koskinen return status; 3119f9981d4fSAaro Koskinen 31206598b91bSDavid Jander destroy_queue: 31216598b91bSDavid Jander spi_destroy_queue(ctlr); 3122f9981d4fSAaro Koskinen free_bus_id: 3123f9981d4fSAaro Koskinen mutex_lock(&board_lock); 3124f9981d4fSAaro Koskinen idr_remove(&spi_master_idr, ctlr->bus_num); 3125f9981d4fSAaro Koskinen mutex_unlock(&board_lock); 31268ae12a0dSDavid Brownell return status; 31278ae12a0dSDavid Brownell } 31288caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller); 31298ae12a0dSDavid Brownell 313043cc5a0aSYang Yingliang static void devm_spi_unregister(struct device *dev, void *res) 3131666d5b4cSMark Brown { 313243cc5a0aSYang Yingliang spi_unregister_controller(*(struct spi_controller **)res); 3133666d5b4cSMark Brown } 3134666d5b4cSMark Brown 3135666d5b4cSMark Brown /** 31368caab75fSGeert Uytterhoeven * devm_spi_register_controller - register managed SPI master or slave 31378caab75fSGeert Uytterhoeven * controller 31388caab75fSGeert Uytterhoeven * @dev: device managing SPI controller 31398caab75fSGeert Uytterhoeven * @ctlr: initialized controller, originally from spi_alloc_master() or 31408caab75fSGeert Uytterhoeven * spi_alloc_slave() 3141666d5b4cSMark Brown * Context: can sleep 3142666d5b4cSMark Brown * 31438caab75fSGeert Uytterhoeven * Register a SPI device as with spi_register_controller() which will 314468b892f1SJohan Hovold * automatically be unregistered and freed. 314597d56dc6SJavier Martinez Canillas * 314697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3147666d5b4cSMark Brown */ 31488caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev, 31498caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 3150666d5b4cSMark Brown { 315143cc5a0aSYang Yingliang struct spi_controller **ptr; 3152666d5b4cSMark Brown int ret; 3153666d5b4cSMark Brown 315443cc5a0aSYang Yingliang ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); 315543cc5a0aSYang Yingliang if (!ptr) 315643cc5a0aSYang Yingliang return -ENOMEM; 315759ebbe40STian Tao 315843cc5a0aSYang Yingliang ret = spi_register_controller(ctlr); 315943cc5a0aSYang Yingliang if (!ret) { 316043cc5a0aSYang Yingliang *ptr = ctlr; 316143cc5a0aSYang Yingliang devres_add(dev, ptr); 316243cc5a0aSYang Yingliang } else { 316343cc5a0aSYang Yingliang devres_free(ptr); 316443cc5a0aSYang Yingliang } 316543cc5a0aSYang Yingliang 316643cc5a0aSYang Yingliang return ret; 3167666d5b4cSMark Brown } 31688caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller); 3169666d5b4cSMark Brown 317034860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 31718ae12a0dSDavid Brownell { 31720c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 31738ae12a0dSDavid Brownell return 0; 31748ae12a0dSDavid Brownell } 31758ae12a0dSDavid Brownell 31768ae12a0dSDavid Brownell /** 31778caab75fSGeert Uytterhoeven * spi_unregister_controller - unregister SPI master or slave controller 31788caab75fSGeert Uytterhoeven * @ctlr: the controller being unregistered 317933e34dc6SDavid Brownell * Context: can sleep 31808ae12a0dSDavid Brownell * 31818caab75fSGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 31828ae12a0dSDavid Brownell * only ones directly touching chip registers. 31838ae12a0dSDavid Brownell * 31848ae12a0dSDavid Brownell * This must be called from context that can sleep. 318568b892f1SJohan Hovold * 318668b892f1SJohan Hovold * Note that this function also drops a reference to the controller. 31878ae12a0dSDavid Brownell */ 31888caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr) 31898ae12a0dSDavid Brownell { 31909b61e302SSuniel Mahesh struct spi_controller *found; 319167f7b278SJohan Hovold int id = ctlr->bus_num; 319289fc9a1aSJeff Garzik 3193ddf75be4SLukas Wunner /* Prevent addition of new devices, unregister existing ones */ 3194ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 31956098475dSMark Brown mutex_lock(&ctlr->add_lock); 3196ddf75be4SLukas Wunner 319784855678SLukas Wunner device_for_each_child(&ctlr->dev, NULL, __unregister); 319884855678SLukas Wunner 31999b61e302SSuniel Mahesh /* First make sure that this controller was ever added */ 32009b61e302SSuniel Mahesh mutex_lock(&board_lock); 320167f7b278SJohan Hovold found = idr_find(&spi_master_idr, id); 32029b61e302SSuniel Mahesh mutex_unlock(&board_lock); 32038caab75fSGeert Uytterhoeven if (ctlr->queued) { 32048caab75fSGeert Uytterhoeven if (spi_destroy_queue(ctlr)) 32058caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue remove failed\n"); 3206ffbbdd21SLinus Walleij } 32072b9603a0SFeng Tang mutex_lock(&board_lock); 32088caab75fSGeert Uytterhoeven list_del(&ctlr->list); 32092b9603a0SFeng Tang mutex_unlock(&board_lock); 32102b9603a0SFeng Tang 32115e844cc3SLukas Wunner device_del(&ctlr->dev); 32125e844cc3SLukas Wunner 321395c8222fSDavid Jander /* Free bus id */ 32149b61e302SSuniel Mahesh mutex_lock(&board_lock); 3215613bd1eaSJarkko Nikula if (found == ctlr) 321667f7b278SJohan Hovold idr_remove(&spi_master_idr, id); 32179b61e302SSuniel Mahesh mutex_unlock(&board_lock); 3218ddf75be4SLukas Wunner 3219ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 32206098475dSMark Brown mutex_unlock(&ctlr->add_lock); 32216c53b45cSMichael Walle 32226c53b45cSMichael Walle /* Release the last reference on the controller if its driver 32236c53b45cSMichael Walle * has not yet been converted to devm_spi_alloc_master/slave(). 32246c53b45cSMichael Walle */ 32256c53b45cSMichael Walle if (!ctlr->devm_allocated) 32266c53b45cSMichael Walle put_device(&ctlr->dev); 32278ae12a0dSDavid Brownell } 32288caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller); 32298ae12a0dSDavid Brownell 32308caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr) 3231ffbbdd21SLinus Walleij { 3232ffbbdd21SLinus Walleij int ret; 3233ffbbdd21SLinus Walleij 32348caab75fSGeert Uytterhoeven /* Basically no-ops for non-queued controllers */ 32358caab75fSGeert Uytterhoeven if (!ctlr->queued) 3236ffbbdd21SLinus Walleij return 0; 3237ffbbdd21SLinus Walleij 32388caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 3239ffbbdd21SLinus Walleij if (ret) 32408caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue stop failed\n"); 3241ffbbdd21SLinus Walleij 3242ffbbdd21SLinus Walleij return ret; 3243ffbbdd21SLinus Walleij } 32448caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend); 3245ffbbdd21SLinus Walleij 32468caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr) 3247ffbbdd21SLinus Walleij { 3248ffbbdd21SLinus Walleij int ret; 3249ffbbdd21SLinus Walleij 32508caab75fSGeert Uytterhoeven if (!ctlr->queued) 3251ffbbdd21SLinus Walleij return 0; 3252ffbbdd21SLinus Walleij 32538caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 3254ffbbdd21SLinus Walleij if (ret) 32558caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue restart failed\n"); 3256ffbbdd21SLinus Walleij 3257ffbbdd21SLinus Walleij return ret; 3258ffbbdd21SLinus Walleij } 32598caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume); 3260ffbbdd21SLinus Walleij 32618ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 32628ae12a0dSDavid Brownell 3263523baf5aSMartin Sperl /* Core methods for spi_message alterations */ 3264523baf5aSMartin Sperl 32658caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr, 3266523baf5aSMartin Sperl struct spi_message *msg, 3267523baf5aSMartin Sperl void *res) 3268523baf5aSMartin Sperl { 3269523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer = res; 3270523baf5aSMartin Sperl size_t i; 3271523baf5aSMartin Sperl 327295c8222fSDavid Jander /* Call extra callback if requested */ 3273523baf5aSMartin Sperl if (rxfer->release) 32748caab75fSGeert Uytterhoeven rxfer->release(ctlr, msg, res); 3275523baf5aSMartin Sperl 327695c8222fSDavid Jander /* Insert replaced transfers back into the message */ 3277523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 3278523baf5aSMartin Sperl 327995c8222fSDavid Jander /* Remove the formerly inserted entries */ 3280523baf5aSMartin Sperl for (i = 0; i < rxfer->inserted; i++) 3281523baf5aSMartin Sperl list_del(&rxfer->inserted_transfers[i].transfer_list); 3282523baf5aSMartin Sperl } 3283523baf5aSMartin Sperl 3284523baf5aSMartin Sperl /** 3285523baf5aSMartin Sperl * spi_replace_transfers - replace transfers with several transfers 3286523baf5aSMartin Sperl * and register change with spi_message.resources 3287523baf5aSMartin Sperl * @msg: the spi_message we work upon 3288523baf5aSMartin Sperl * @xfer_first: the first spi_transfer we want to replace 3289523baf5aSMartin Sperl * @remove: number of transfers to remove 3290523baf5aSMartin Sperl * @insert: the number of transfers we want to insert instead 3291523baf5aSMartin Sperl * @release: extra release code necessary in some circumstances 3292523baf5aSMartin Sperl * @extradatasize: extra data to allocate (with alignment guarantees 3293523baf5aSMartin Sperl * of struct @spi_transfer) 329405885397SMartin Sperl * @gfp: gfp flags 3295523baf5aSMartin Sperl * 3296523baf5aSMartin Sperl * Returns: pointer to @spi_replaced_transfers, 3297523baf5aSMartin Sperl * PTR_ERR(...) in case of errors. 3298523baf5aSMartin Sperl */ 3299da21fde0SUwe Kleine-König static struct spi_replaced_transfers *spi_replace_transfers( 3300523baf5aSMartin Sperl struct spi_message *msg, 3301523baf5aSMartin Sperl struct spi_transfer *xfer_first, 3302523baf5aSMartin Sperl size_t remove, 3303523baf5aSMartin Sperl size_t insert, 3304523baf5aSMartin Sperl spi_replaced_release_t release, 3305523baf5aSMartin Sperl size_t extradatasize, 3306523baf5aSMartin Sperl gfp_t gfp) 3307523baf5aSMartin Sperl { 3308523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer; 3309523baf5aSMartin Sperl struct spi_transfer *xfer; 3310523baf5aSMartin Sperl size_t i; 3311523baf5aSMartin Sperl 331295c8222fSDavid Jander /* Allocate the structure using spi_res */ 3313523baf5aSMartin Sperl rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 3314aef97522SGustavo A. R. Silva struct_size(rxfer, inserted_transfers, insert) 3315523baf5aSMartin Sperl + extradatasize, 3316523baf5aSMartin Sperl gfp); 3317523baf5aSMartin Sperl if (!rxfer) 3318523baf5aSMartin Sperl return ERR_PTR(-ENOMEM); 3319523baf5aSMartin Sperl 332095c8222fSDavid Jander /* The release code to invoke before running the generic release */ 3321523baf5aSMartin Sperl rxfer->release = release; 3322523baf5aSMartin Sperl 332395c8222fSDavid Jander /* Assign extradata */ 3324523baf5aSMartin Sperl if (extradatasize) 3325523baf5aSMartin Sperl rxfer->extradata = 3326523baf5aSMartin Sperl &rxfer->inserted_transfers[insert]; 3327523baf5aSMartin Sperl 332895c8222fSDavid Jander /* Init the replaced_transfers list */ 3329523baf5aSMartin Sperl INIT_LIST_HEAD(&rxfer->replaced_transfers); 3330523baf5aSMartin Sperl 3331350de7ceSAndy Shevchenko /* 3332350de7ceSAndy Shevchenko * Assign the list_entry after which we should reinsert 3333523baf5aSMartin Sperl * the @replaced_transfers - it may be spi_message.messages! 3334523baf5aSMartin Sperl */ 3335523baf5aSMartin Sperl rxfer->replaced_after = xfer_first->transfer_list.prev; 3336523baf5aSMartin Sperl 333795c8222fSDavid Jander /* Remove the requested number of transfers */ 3338523baf5aSMartin Sperl for (i = 0; i < remove; i++) { 3339350de7ceSAndy Shevchenko /* 3340350de7ceSAndy Shevchenko * If the entry after replaced_after it is msg->transfers 3341523baf5aSMartin Sperl * then we have been requested to remove more transfers 3342350de7ceSAndy Shevchenko * than are in the list. 3343523baf5aSMartin Sperl */ 3344523baf5aSMartin Sperl if (rxfer->replaced_after->next == &msg->transfers) { 3345523baf5aSMartin Sperl dev_err(&msg->spi->dev, 3346523baf5aSMartin Sperl "requested to remove more spi_transfers than are available\n"); 334795c8222fSDavid Jander /* Insert replaced transfers back into the message */ 3348523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, 3349523baf5aSMartin Sperl rxfer->replaced_after); 3350523baf5aSMartin Sperl 335195c8222fSDavid Jander /* Free the spi_replace_transfer structure... */ 3352523baf5aSMartin Sperl spi_res_free(rxfer); 3353523baf5aSMartin Sperl 335495c8222fSDavid Jander /* ...and return with an error */ 3355523baf5aSMartin Sperl return ERR_PTR(-EINVAL); 3356523baf5aSMartin Sperl } 3357523baf5aSMartin Sperl 3358350de7ceSAndy Shevchenko /* 3359350de7ceSAndy Shevchenko * Remove the entry after replaced_after from list of 3360350de7ceSAndy Shevchenko * transfers and add it to list of replaced_transfers. 3361523baf5aSMartin Sperl */ 3362523baf5aSMartin Sperl list_move_tail(rxfer->replaced_after->next, 3363523baf5aSMartin Sperl &rxfer->replaced_transfers); 3364523baf5aSMartin Sperl } 3365523baf5aSMartin Sperl 3366350de7ceSAndy Shevchenko /* 3367350de7ceSAndy Shevchenko * Create copy of the given xfer with identical settings 3368350de7ceSAndy Shevchenko * based on the first transfer to get removed. 3369523baf5aSMartin Sperl */ 3370523baf5aSMartin Sperl for (i = 0; i < insert; i++) { 337195c8222fSDavid Jander /* We need to run in reverse order */ 3372523baf5aSMartin Sperl xfer = &rxfer->inserted_transfers[insert - 1 - i]; 3373523baf5aSMartin Sperl 337495c8222fSDavid Jander /* Copy all spi_transfer data */ 3375523baf5aSMartin Sperl memcpy(xfer, xfer_first, sizeof(*xfer)); 3376523baf5aSMartin Sperl 337795c8222fSDavid Jander /* Add to list */ 3378523baf5aSMartin Sperl list_add(&xfer->transfer_list, rxfer->replaced_after); 3379523baf5aSMartin Sperl 338095c8222fSDavid Jander /* Clear cs_change and delay for all but the last */ 3381523baf5aSMartin Sperl if (i) { 3382523baf5aSMartin Sperl xfer->cs_change = false; 3383bebcfd27SAlexandru Ardelean xfer->delay.value = 0; 3384523baf5aSMartin Sperl } 3385523baf5aSMartin Sperl } 3386523baf5aSMartin Sperl 338795c8222fSDavid Jander /* Set up inserted... */ 3388523baf5aSMartin Sperl rxfer->inserted = insert; 3389523baf5aSMartin Sperl 339095c8222fSDavid Jander /* ...and register it with spi_res/spi_message */ 3391523baf5aSMartin Sperl spi_res_add(msg, rxfer); 3392523baf5aSMartin Sperl 3393523baf5aSMartin Sperl return rxfer; 3394523baf5aSMartin Sperl } 3395523baf5aSMartin Sperl 33968caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 3397d9f12122SMartin Sperl struct spi_message *msg, 3398d9f12122SMartin Sperl struct spi_transfer **xferp, 3399d9f12122SMartin Sperl size_t maxsize, 3400d9f12122SMartin Sperl gfp_t gfp) 3401d9f12122SMartin Sperl { 3402d9f12122SMartin Sperl struct spi_transfer *xfer = *xferp, *xfers; 3403d9f12122SMartin Sperl struct spi_replaced_transfers *srt; 3404d9f12122SMartin Sperl size_t offset; 3405d9f12122SMartin Sperl size_t count, i; 3406d9f12122SMartin Sperl 340795c8222fSDavid Jander /* Calculate how many we have to replace */ 3408d9f12122SMartin Sperl count = DIV_ROUND_UP(xfer->len, maxsize); 3409d9f12122SMartin Sperl 341095c8222fSDavid Jander /* Create replacement */ 3411d9f12122SMartin Sperl srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 3412657d32efSDan Carpenter if (IS_ERR(srt)) 3413657d32efSDan Carpenter return PTR_ERR(srt); 3414d9f12122SMartin Sperl xfers = srt->inserted_transfers; 3415d9f12122SMartin Sperl 3416350de7ceSAndy Shevchenko /* 3417350de7ceSAndy Shevchenko * Now handle each of those newly inserted spi_transfers. 3418350de7ceSAndy Shevchenko * Note that the replacements spi_transfers all are preset 3419d9f12122SMartin Sperl * to the same values as *xferp, so tx_buf, rx_buf and len 3420d9f12122SMartin Sperl * are all identical (as well as most others) 3421d9f12122SMartin Sperl * so we just have to fix up len and the pointers. 3422d9f12122SMartin Sperl * 3423350de7ceSAndy Shevchenko * This also includes support for the depreciated 3424350de7ceSAndy Shevchenko * spi_message.is_dma_mapped interface. 3425d9f12122SMartin Sperl */ 3426d9f12122SMartin Sperl 3427350de7ceSAndy Shevchenko /* 3428350de7ceSAndy Shevchenko * The first transfer just needs the length modified, so we 3429350de7ceSAndy Shevchenko * run it outside the loop. 3430d9f12122SMartin Sperl */ 3431c8dab77aSFabio Estevam xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 3432d9f12122SMartin Sperl 343395c8222fSDavid Jander /* All the others need rx_buf/tx_buf also set */ 3434d9f12122SMartin Sperl for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 343595c8222fSDavid Jander /* Update rx_buf, tx_buf and dma */ 3436d9f12122SMartin Sperl if (xfers[i].rx_buf) 3437d9f12122SMartin Sperl xfers[i].rx_buf += offset; 3438d9f12122SMartin Sperl if (xfers[i].rx_dma) 3439d9f12122SMartin Sperl xfers[i].rx_dma += offset; 3440d9f12122SMartin Sperl if (xfers[i].tx_buf) 3441d9f12122SMartin Sperl xfers[i].tx_buf += offset; 3442d9f12122SMartin Sperl if (xfers[i].tx_dma) 3443d9f12122SMartin Sperl xfers[i].tx_dma += offset; 3444d9f12122SMartin Sperl 344595c8222fSDavid Jander /* Update length */ 3446d9f12122SMartin Sperl xfers[i].len = min(maxsize, xfers[i].len - offset); 3447d9f12122SMartin Sperl } 3448d9f12122SMartin Sperl 3449350de7ceSAndy Shevchenko /* 3450350de7ceSAndy Shevchenko * We set up xferp to the last entry we have inserted, 3451350de7ceSAndy Shevchenko * so that we skip those already split transfers. 3452d9f12122SMartin Sperl */ 3453d9f12122SMartin Sperl *xferp = &xfers[count - 1]; 3454d9f12122SMartin Sperl 345595c8222fSDavid Jander /* Increment statistics counters */ 34566598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, 3457d9f12122SMartin Sperl transfers_split_maxsize); 34586598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics, 3459d9f12122SMartin Sperl transfers_split_maxsize); 3460d9f12122SMartin Sperl 3461d9f12122SMartin Sperl return 0; 3462d9f12122SMartin Sperl } 3463d9f12122SMartin Sperl 3464d9f12122SMartin Sperl /** 3465ce2424d7SMauro Carvalho Chehab * spi_split_transfers_maxsize - split spi transfers into multiple transfers 3466d9f12122SMartin Sperl * when an individual transfer exceeds a 3467d9f12122SMartin Sperl * certain size 34688caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller for this transfer 34693700ce95SMasanari Iida * @msg: the @spi_message to transform 34703700ce95SMasanari Iida * @maxsize: the maximum when to apply this 347110f11a22SJavier Martinez Canillas * @gfp: GFP allocation flags 3472d9f12122SMartin Sperl * 3473d9f12122SMartin Sperl * Return: status of transformation 3474d9f12122SMartin Sperl */ 34758caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr, 3476d9f12122SMartin Sperl struct spi_message *msg, 3477d9f12122SMartin Sperl size_t maxsize, 3478d9f12122SMartin Sperl gfp_t gfp) 3479d9f12122SMartin Sperl { 3480d9f12122SMartin Sperl struct spi_transfer *xfer; 3481d9f12122SMartin Sperl int ret; 3482d9f12122SMartin Sperl 3483350de7ceSAndy Shevchenko /* 3484350de7ceSAndy Shevchenko * Iterate over the transfer_list, 3485d9f12122SMartin Sperl * but note that xfer is advanced to the last transfer inserted 3486d9f12122SMartin Sperl * to avoid checking sizes again unnecessarily (also xfer does 3487350de7ceSAndy Shevchenko * potentially belong to a different list by the time the 3488350de7ceSAndy Shevchenko * replacement has happened). 3489d9f12122SMartin Sperl */ 3490d9f12122SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 3491d9f12122SMartin Sperl if (xfer->len > maxsize) { 34928caab75fSGeert Uytterhoeven ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 34938caab75fSGeert Uytterhoeven maxsize, gfp); 3494d9f12122SMartin Sperl if (ret) 3495d9f12122SMartin Sperl return ret; 3496d9f12122SMartin Sperl } 3497d9f12122SMartin Sperl } 3498d9f12122SMartin Sperl 3499d9f12122SMartin Sperl return 0; 3500d9f12122SMartin Sperl } 3501d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 35028ae12a0dSDavid Brownell 35038ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 35048ae12a0dSDavid Brownell 35058caab75fSGeert Uytterhoeven /* Core methods for SPI controller protocol drivers. Some of the 35067d077197SDavid Brownell * other core methods are currently defined as inline functions. 35077d077197SDavid Brownell */ 35087d077197SDavid Brownell 35098caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 35108caab75fSGeert Uytterhoeven u8 bits_per_word) 351163ab645fSStefan Brüns { 35128caab75fSGeert Uytterhoeven if (ctlr->bits_per_word_mask) { 351363ab645fSStefan Brüns /* Only 32 bits fit in the mask */ 351463ab645fSStefan Brüns if (bits_per_word > 32) 351563ab645fSStefan Brüns return -EINVAL; 35168caab75fSGeert Uytterhoeven if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 351763ab645fSStefan Brüns return -EINVAL; 351863ab645fSStefan Brüns } 351963ab645fSStefan Brüns 352063ab645fSStefan Brüns return 0; 352163ab645fSStefan Brüns } 352263ab645fSStefan Brüns 35237d077197SDavid Brownell /** 35247d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 35257d077197SDavid Brownell * @spi: the device whose settings are being modified 35267d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 35277d077197SDavid Brownell * 35287d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 35297d077197SDavid Brownell * device doesn't work with its default. They may likewise need 35307d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 35317d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 35327d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 35337d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 35347d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 35357d077197SDavid Brownell * 35367d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 35377d077197SDavid Brownell * that the underlying controller or its driver does not support. For 35387d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 35397d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 354097d56dc6SJavier Martinez Canillas * 354197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 35427d077197SDavid Brownell */ 35437d077197SDavid Brownell int spi_setup(struct spi_device *spi) 35447d077197SDavid Brownell { 354583596fbeSGeert Uytterhoeven unsigned bad_bits, ugly_bits; 354673f93db5SPaul Kocialkowski int status = 0; 35477d077197SDavid Brownell 3548d962608cSDragos Bogdan /* 3549350de7ceSAndy Shevchenko * Check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO 3550350de7ceSAndy Shevchenko * are set at the same time. 3551f477b7fbSwangyuhang */ 3552d962608cSDragos Bogdan if ((hweight_long(spi->mode & 3553d962608cSDragos Bogdan (SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) || 3554d962608cSDragos Bogdan (hweight_long(spi->mode & 3555d962608cSDragos Bogdan (SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) { 3556f477b7fbSwangyuhang dev_err(&spi->dev, 3557d962608cSDragos Bogdan "setup: can not select any two of dual, quad and no-rx/tx at the same time\n"); 3558f477b7fbSwangyuhang return -EINVAL; 3559f477b7fbSwangyuhang } 3560350de7ceSAndy Shevchenko /* If it is SPI_3WIRE mode, DUAL and QUAD should be forbidden */ 3561f477b7fbSwangyuhang if ((spi->mode & SPI_3WIRE) && (spi->mode & 35626b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 35636b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 3564f477b7fbSwangyuhang return -EINVAL; 3565350de7ceSAndy Shevchenko /* 3566350de7ceSAndy Shevchenko * Help drivers fail *cleanly* when they need options 3567350de7ceSAndy Shevchenko * that aren't supported with their current controller. 3568cbaa62e0SDavid Lechner * SPI_CS_WORD has a fallback software implementation, 3569cbaa62e0SDavid Lechner * so it is ignored here. 3570e7db06b5SDavid Brownell */ 3571d962608cSDragos Bogdan bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | 3572d962608cSDragos Bogdan SPI_NO_TX | SPI_NO_RX); 357383596fbeSGeert Uytterhoeven ugly_bits = bad_bits & 35746b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 35756b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); 357683596fbeSGeert Uytterhoeven if (ugly_bits) { 357783596fbeSGeert Uytterhoeven dev_warn(&spi->dev, 357883596fbeSGeert Uytterhoeven "setup: ignoring unsupported mode bits %x\n", 357983596fbeSGeert Uytterhoeven ugly_bits); 358083596fbeSGeert Uytterhoeven spi->mode &= ~ugly_bits; 358183596fbeSGeert Uytterhoeven bad_bits &= ~ugly_bits; 358283596fbeSGeert Uytterhoeven } 3583e7db06b5SDavid Brownell if (bad_bits) { 3584eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 3585e7db06b5SDavid Brownell bad_bits); 3586e7db06b5SDavid Brownell return -EINVAL; 3587e7db06b5SDavid Brownell } 3588e7db06b5SDavid Brownell 3589b3fe2e51SPaul Kocialkowski if (!spi->bits_per_word) { 35907d077197SDavid Brownell spi->bits_per_word = 8; 3591b3fe2e51SPaul Kocialkowski } else { 3592b3fe2e51SPaul Kocialkowski /* 3593b3fe2e51SPaul Kocialkowski * Some controllers may not support the default 8 bits-per-word 3594b3fe2e51SPaul Kocialkowski * so only perform the check when this is explicitly provided. 3595b3fe2e51SPaul Kocialkowski */ 35968caab75fSGeert Uytterhoeven status = __spi_validate_bits_per_word(spi->controller, 35978caab75fSGeert Uytterhoeven spi->bits_per_word); 35985ab8d262SAndy Shevchenko if (status) 35995ab8d262SAndy Shevchenko return status; 3600b3fe2e51SPaul Kocialkowski } 360163ab645fSStefan Brüns 36026820e812STudor Ambarus if (spi->controller->max_speed_hz && 36036820e812STudor Ambarus (!spi->max_speed_hz || 36046820e812STudor Ambarus spi->max_speed_hz > spi->controller->max_speed_hz)) 36058caab75fSGeert Uytterhoeven spi->max_speed_hz = spi->controller->max_speed_hz; 3606052eb2d4SAxel Lin 36074fae3a58SSerge Semin mutex_lock(&spi->controller->io_mutex); 36084fae3a58SSerge Semin 3609c914dbf8SJoe Burmeister if (spi->controller->setup) { 36108caab75fSGeert Uytterhoeven status = spi->controller->setup(spi); 3611c914dbf8SJoe Burmeister if (status) { 3612c914dbf8SJoe Burmeister mutex_unlock(&spi->controller->io_mutex); 3613c914dbf8SJoe Burmeister dev_err(&spi->controller->dev, "Failed to setup device: %d\n", 3614c914dbf8SJoe Burmeister status); 3615c914dbf8SJoe Burmeister return status; 3616c914dbf8SJoe Burmeister } 3617c914dbf8SJoe Burmeister } 36187d077197SDavid Brownell 3619d948e6caSLuhua Xu if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { 3620dd769f15SMinghao Chi status = pm_runtime_resume_and_get(spi->controller->dev.parent); 3621d948e6caSLuhua Xu if (status < 0) { 36224fae3a58SSerge Semin mutex_unlock(&spi->controller->io_mutex); 3623d948e6caSLuhua Xu dev_err(&spi->controller->dev, "Failed to power device: %d\n", 3624d948e6caSLuhua Xu status); 3625d948e6caSLuhua Xu return status; 3626d948e6caSLuhua Xu } 362757a94607STony Lindgren 362857a94607STony Lindgren /* 362957a94607STony Lindgren * We do not want to return positive value from pm_runtime_get, 363057a94607STony Lindgren * there are many instances of devices calling spi_setup() and 363157a94607STony Lindgren * checking for a non-zero return value instead of a negative 363257a94607STony Lindgren * return value. 363357a94607STony Lindgren */ 363457a94607STony Lindgren status = 0; 363557a94607STony Lindgren 3636d347b4aaSDavid Bauer spi_set_cs(spi, false, true); 3637d948e6caSLuhua Xu pm_runtime_mark_last_busy(spi->controller->dev.parent); 3638d948e6caSLuhua Xu pm_runtime_put_autosuspend(spi->controller->dev.parent); 3639d948e6caSLuhua Xu } else { 3640d347b4aaSDavid Bauer spi_set_cs(spi, false, true); 3641d948e6caSLuhua Xu } 3642abeedb01SFranklin S Cooper Jr 36434fae3a58SSerge Semin mutex_unlock(&spi->controller->io_mutex); 36444fae3a58SSerge Semin 3645924b5867SDouglas Anderson if (spi->rt && !spi->controller->rt) { 3646924b5867SDouglas Anderson spi->controller->rt = true; 3647924b5867SDouglas Anderson spi_set_thread_rt(spi->controller); 3648924b5867SDouglas Anderson } 3649924b5867SDouglas Anderson 36505cb4e1f3SAndy Shevchenko trace_spi_setup(spi, status); 36515cb4e1f3SAndy Shevchenko 365240b82c2dSAndy Shevchenko dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 365340b82c2dSAndy Shevchenko spi->mode & SPI_MODE_X_MASK, 36547d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 36557d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 36567d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 36577d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 36587d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 36597d077197SDavid Brownell status); 36607d077197SDavid Brownell 36617d077197SDavid Brownell return status; 36627d077197SDavid Brownell } 36637d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 36647d077197SDavid Brownell 36656c613f68SAlexandru Ardelean static int _spi_xfer_word_delay_update(struct spi_transfer *xfer, 36666c613f68SAlexandru Ardelean struct spi_device *spi) 36676c613f68SAlexandru Ardelean { 36686c613f68SAlexandru Ardelean int delay1, delay2; 36696c613f68SAlexandru Ardelean 36703984d39bSAlexandru Ardelean delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); 36716c613f68SAlexandru Ardelean if (delay1 < 0) 36726c613f68SAlexandru Ardelean return delay1; 36736c613f68SAlexandru Ardelean 36743984d39bSAlexandru Ardelean delay2 = spi_delay_to_ns(&spi->word_delay, xfer); 36756c613f68SAlexandru Ardelean if (delay2 < 0) 36766c613f68SAlexandru Ardelean return delay2; 36776c613f68SAlexandru Ardelean 36786c613f68SAlexandru Ardelean if (delay1 < delay2) 36796c613f68SAlexandru Ardelean memcpy(&xfer->word_delay, &spi->word_delay, 36806c613f68SAlexandru Ardelean sizeof(xfer->word_delay)); 36816c613f68SAlexandru Ardelean 36826c613f68SAlexandru Ardelean return 0; 36836c613f68SAlexandru Ardelean } 36846c613f68SAlexandru Ardelean 368590808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message) 3686cf32b71eSErnst Schwab { 36878caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3688e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 36896ea31293SAtsushi Nemoto int w_size; 3690cf32b71eSErnst Schwab 369124a0013aSMark Brown if (list_empty(&message->transfers)) 369224a0013aSMark Brown return -EINVAL; 369324a0013aSMark Brown 3694350de7ceSAndy Shevchenko /* 3695350de7ceSAndy Shevchenko * If an SPI controller does not support toggling the CS line on each 369671388b21SDavid Lechner * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO 369771388b21SDavid Lechner * for the CS line, we can emulate the CS-per-word hardware function by 3698cbaa62e0SDavid Lechner * splitting transfers into one-word transfers and ensuring that 3699cbaa62e0SDavid Lechner * cs_change is set for each transfer. 3700cbaa62e0SDavid Lechner */ 370171388b21SDavid Lechner if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || 3702f48dc6b9SLinus Walleij spi->cs_gpiod)) { 3703cbaa62e0SDavid Lechner size_t maxsize; 3704cbaa62e0SDavid Lechner int ret; 3705cbaa62e0SDavid Lechner 3706cbaa62e0SDavid Lechner maxsize = (spi->bits_per_word + 7) / 8; 3707cbaa62e0SDavid Lechner 3708cbaa62e0SDavid Lechner /* spi_split_transfers_maxsize() requires message->spi */ 3709cbaa62e0SDavid Lechner message->spi = spi; 3710cbaa62e0SDavid Lechner 3711cbaa62e0SDavid Lechner ret = spi_split_transfers_maxsize(ctlr, message, maxsize, 3712cbaa62e0SDavid Lechner GFP_KERNEL); 3713cbaa62e0SDavid Lechner if (ret) 3714cbaa62e0SDavid Lechner return ret; 3715cbaa62e0SDavid Lechner 3716cbaa62e0SDavid Lechner list_for_each_entry(xfer, &message->transfers, transfer_list) { 371795c8222fSDavid Jander /* Don't change cs_change on the last entry in the list */ 3718cbaa62e0SDavid Lechner if (list_is_last(&xfer->transfer_list, &message->transfers)) 3719cbaa62e0SDavid Lechner break; 3720cbaa62e0SDavid Lechner xfer->cs_change = 1; 3721cbaa62e0SDavid Lechner } 3722cbaa62e0SDavid Lechner } 3723cbaa62e0SDavid Lechner 3724350de7ceSAndy Shevchenko /* 3725350de7ceSAndy Shevchenko * Half-duplex links include original MicroWire, and ones with 3726cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 3727cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 3728cf32b71eSErnst Schwab * software limitations. 3729cf32b71eSErnst Schwab */ 37308caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 37318caab75fSGeert Uytterhoeven (spi->mode & SPI_3WIRE)) { 37328caab75fSGeert Uytterhoeven unsigned flags = ctlr->flags; 3733cf32b71eSErnst Schwab 3734cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 3735cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 3736cf32b71eSErnst Schwab return -EINVAL; 37378caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 3738cf32b71eSErnst Schwab return -EINVAL; 37398caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 3740cf32b71eSErnst Schwab return -EINVAL; 3741cf32b71eSErnst Schwab } 3742cf32b71eSErnst Schwab } 3743cf32b71eSErnst Schwab 3744350de7ceSAndy Shevchenko /* 3745059b8ffeSLaxman Dewangan * Set transfer bits_per_word and max speed as spi device default if 3746059b8ffeSLaxman Dewangan * it is not set for this transfer. 3747f477b7fbSwangyuhang * Set transfer tx_nbits and rx_nbits as single transfer default 3748f477b7fbSwangyuhang * (SPI_NBITS_SINGLE) if it is not set for this transfer. 3749b7bb367aSJonas Bonn * Ensure transfer word_delay is at least as long as that required by 3750b7bb367aSJonas Bonn * device itself. 3751e6811d1dSLaxman Dewangan */ 375277e80588SMartin Sperl message->frame_length = 0; 3753e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 37545d7e2b5eSMartin Sperl xfer->effective_speed_hz = 0; 3755078726ceSSourav Poddar message->frame_length += xfer->len; 3756e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 3757e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 3758a6f87fadSAxel Lin 3759a6f87fadSAxel Lin if (!xfer->speed_hz) 3760059b8ffeSLaxman Dewangan xfer->speed_hz = spi->max_speed_hz; 3761a6f87fadSAxel Lin 37628caab75fSGeert Uytterhoeven if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 37638caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 376456ede94aSGabor Juhos 37658caab75fSGeert Uytterhoeven if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 3766543bb255SStephen Warren return -EINVAL; 3767a2fd4f9fSMark Brown 37684d94bd21SIvan T. Ivanov /* 37694d94bd21SIvan T. Ivanov * SPI transfer length should be multiple of SPI word size 3770350de7ceSAndy Shevchenko * where SPI word size should be power-of-two multiple. 37714d94bd21SIvan T. Ivanov */ 37724d94bd21SIvan T. Ivanov if (xfer->bits_per_word <= 8) 37734d94bd21SIvan T. Ivanov w_size = 1; 37744d94bd21SIvan T. Ivanov else if (xfer->bits_per_word <= 16) 37754d94bd21SIvan T. Ivanov w_size = 2; 37764d94bd21SIvan T. Ivanov else 37774d94bd21SIvan T. Ivanov w_size = 4; 37784d94bd21SIvan T. Ivanov 37794d94bd21SIvan T. Ivanov /* No partial transfers accepted */ 37806ea31293SAtsushi Nemoto if (xfer->len % w_size) 37814d94bd21SIvan T. Ivanov return -EINVAL; 37824d94bd21SIvan T. Ivanov 37838caab75fSGeert Uytterhoeven if (xfer->speed_hz && ctlr->min_speed_hz && 37848caab75fSGeert Uytterhoeven xfer->speed_hz < ctlr->min_speed_hz) 3785a2fd4f9fSMark Brown return -EINVAL; 3786f477b7fbSwangyuhang 3787f477b7fbSwangyuhang if (xfer->tx_buf && !xfer->tx_nbits) 3788f477b7fbSwangyuhang xfer->tx_nbits = SPI_NBITS_SINGLE; 3789f477b7fbSwangyuhang if (xfer->rx_buf && !xfer->rx_nbits) 3790f477b7fbSwangyuhang xfer->rx_nbits = SPI_NBITS_SINGLE; 3791350de7ceSAndy Shevchenko /* 3792350de7ceSAndy Shevchenko * Check transfer tx/rx_nbits: 37931afd9989SGeert Uytterhoeven * 1. check the value matches one of single, dual and quad 37941afd9989SGeert Uytterhoeven * 2. check tx/rx_nbits match the mode in spi_device 3795f477b7fbSwangyuhang */ 3796db90a441SSourav Poddar if (xfer->tx_buf) { 3797d962608cSDragos Bogdan if (spi->mode & SPI_NO_TX) 3798d962608cSDragos Bogdan return -EINVAL; 3799f477b7fbSwangyuhang if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3800f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_DUAL && 3801f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_QUAD) 3802a2fd4f9fSMark Brown return -EINVAL; 3803f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 3804f477b7fbSwangyuhang !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3805f477b7fbSwangyuhang return -EINVAL; 3806f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 3807f477b7fbSwangyuhang !(spi->mode & SPI_TX_QUAD)) 3808f477b7fbSwangyuhang return -EINVAL; 3809db90a441SSourav Poddar } 381095c8222fSDavid Jander /* Check transfer rx_nbits */ 3811db90a441SSourav Poddar if (xfer->rx_buf) { 3812d962608cSDragos Bogdan if (spi->mode & SPI_NO_RX) 3813d962608cSDragos Bogdan return -EINVAL; 3814f477b7fbSwangyuhang if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3815f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_DUAL && 3816f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_QUAD) 3817f477b7fbSwangyuhang return -EINVAL; 3818f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 3819f477b7fbSwangyuhang !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 3820f477b7fbSwangyuhang return -EINVAL; 3821f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 3822f477b7fbSwangyuhang !(spi->mode & SPI_RX_QUAD)) 3823f477b7fbSwangyuhang return -EINVAL; 3824e6811d1dSLaxman Dewangan } 3825b7bb367aSJonas Bonn 38266c613f68SAlexandru Ardelean if (_spi_xfer_word_delay_update(xfer, spi)) 38276c613f68SAlexandru Ardelean return -EINVAL; 3828e6811d1dSLaxman Dewangan } 3829e6811d1dSLaxman Dewangan 3830cf32b71eSErnst Schwab message->status = -EINPROGRESS; 383190808738SMark Brown 383290808738SMark Brown return 0; 383390808738SMark Brown } 383490808738SMark Brown 383590808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message) 383690808738SMark Brown { 38378caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3838b42faeeeSVladimir Oltean struct spi_transfer *xfer; 383990808738SMark Brown 3840b5932f5cSBoris Brezillon /* 3841b5932f5cSBoris Brezillon * Some controllers do not support doing regular SPI transfers. Return 3842b5932f5cSBoris Brezillon * ENOTSUPP when this is the case. 3843b5932f5cSBoris Brezillon */ 3844b5932f5cSBoris Brezillon if (!ctlr->transfer) 3845b5932f5cSBoris Brezillon return -ENOTSUPP; 3846b5932f5cSBoris Brezillon 384790808738SMark Brown message->spi = spi; 384890808738SMark Brown 38496598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_async); 38506598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async); 3851eca2ebc7SMartin Sperl 385290808738SMark Brown trace_spi_message_submit(message); 385390808738SMark Brown 3854b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 3855b42faeeeSVladimir Oltean list_for_each_entry(xfer, &message->transfers, transfer_list) { 3856b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 3857b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 3858b42faeeeSVladimir Oltean } 3859b42faeeeSVladimir Oltean } 3860b42faeeeSVladimir Oltean 38618caab75fSGeert Uytterhoeven return ctlr->transfer(spi, message); 3862cf32b71eSErnst Schwab } 3863cf32b71eSErnst Schwab 3864568d0697SDavid Brownell /** 3865568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 3866568d0697SDavid Brownell * @spi: device with which data will be exchanged 3867568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 3868568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 3869568d0697SDavid Brownell * 3870568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 3871568d0697SDavid Brownell * as well as from task contexts which can sleep. 3872568d0697SDavid Brownell * 3873568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 3874568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 3875568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 3876568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 3877568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 3878568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 3879568d0697SDavid Brownell * core or controller driver code. 3880568d0697SDavid Brownell * 3881568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 3882568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 3883568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 3884568d0697SDavid Brownell * time requirements, for example. 3885568d0697SDavid Brownell * 3886568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 3887568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 3888568d0697SDavid Brownell * Until returning from the associated message completion callback, 3889568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 3890568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 3891568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 389297d56dc6SJavier Martinez Canillas * 389397d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3894568d0697SDavid Brownell */ 3895568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 3896568d0697SDavid Brownell { 38978caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3898cf32b71eSErnst Schwab int ret; 3899cf32b71eSErnst Schwab unsigned long flags; 3900568d0697SDavid Brownell 390190808738SMark Brown ret = __spi_validate(spi, message); 390290808738SMark Brown if (ret != 0) 390390808738SMark Brown return ret; 390490808738SMark Brown 39058caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3906568d0697SDavid Brownell 39078caab75fSGeert Uytterhoeven if (ctlr->bus_lock_flag) 3908cf32b71eSErnst Schwab ret = -EBUSY; 3909cf32b71eSErnst Schwab else 3910cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3911568d0697SDavid Brownell 39128caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3913cf32b71eSErnst Schwab 3914cf32b71eSErnst Schwab return ret; 3915568d0697SDavid Brownell } 3916568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 3917568d0697SDavid Brownell 3918cf32b71eSErnst Schwab /** 3919cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 3920cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3921cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 3922cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 3923cf32b71eSErnst Schwab * 3924cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 3925cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 3926cf32b71eSErnst Schwab * 3927cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 3928cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 3929cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 3930cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 3931cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 3932cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 3933cf32b71eSErnst Schwab * core or controller driver code. 3934cf32b71eSErnst Schwab * 3935cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 3936cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 3937cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 3938cf32b71eSErnst Schwab * time requirements, for example. 3939cf32b71eSErnst Schwab * 3940cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 3941cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 3942cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 3943cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 3944cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 3945cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 394697d56dc6SJavier Martinez Canillas * 394797d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3948cf32b71eSErnst Schwab */ 3949da21fde0SUwe Kleine-König static int spi_async_locked(struct spi_device *spi, struct spi_message *message) 3950cf32b71eSErnst Schwab { 39518caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3952cf32b71eSErnst Schwab int ret; 3953cf32b71eSErnst Schwab unsigned long flags; 3954cf32b71eSErnst Schwab 395590808738SMark Brown ret = __spi_validate(spi, message); 395690808738SMark Brown if (ret != 0) 395790808738SMark Brown return ret; 395890808738SMark Brown 39598caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3960cf32b71eSErnst Schwab 3961cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3962cf32b71eSErnst Schwab 39638caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3964cf32b71eSErnst Schwab 3965cf32b71eSErnst Schwab return ret; 3966cf32b71eSErnst Schwab 3967cf32b71eSErnst Schwab } 3968cf32b71eSErnst Schwab 3969ae7d2346SDavid Jander static void __spi_transfer_message_noqueue(struct spi_controller *ctlr, struct spi_message *msg) 3970ae7d2346SDavid Jander { 3971ae7d2346SDavid Jander bool was_busy; 3972ae7d2346SDavid Jander int ret; 3973ae7d2346SDavid Jander 3974ae7d2346SDavid Jander mutex_lock(&ctlr->io_mutex); 3975ae7d2346SDavid Jander 39761a9cafcbSDavid Jander was_busy = ctlr->busy; 3977ae7d2346SDavid Jander 397872c5c59bSDavid Jander ctlr->cur_msg = msg; 3979ae7d2346SDavid Jander ret = __spi_pump_transfer_message(ctlr, msg, was_busy); 3980ae7d2346SDavid Jander if (ret) 3981ae7d2346SDavid Jander goto out; 3982ae7d2346SDavid Jander 398369fa9590SDavid Jander ctlr->cur_msg = NULL; 398469fa9590SDavid Jander ctlr->fallback = false; 398569fa9590SDavid Jander 3986ae7d2346SDavid Jander if (!was_busy) { 3987ae7d2346SDavid Jander kfree(ctlr->dummy_rx); 3988ae7d2346SDavid Jander ctlr->dummy_rx = NULL; 3989ae7d2346SDavid Jander kfree(ctlr->dummy_tx); 3990ae7d2346SDavid Jander ctlr->dummy_tx = NULL; 3991ae7d2346SDavid Jander if (ctlr->unprepare_transfer_hardware && 3992ae7d2346SDavid Jander ctlr->unprepare_transfer_hardware(ctlr)) 3993ae7d2346SDavid Jander dev_err(&ctlr->dev, 3994ae7d2346SDavid Jander "failed to unprepare transfer hardware\n"); 3995ae7d2346SDavid Jander spi_idle_runtime_pm(ctlr); 3996ae7d2346SDavid Jander } 3997ae7d2346SDavid Jander 3998ae7d2346SDavid Jander out: 3999ae7d2346SDavid Jander mutex_unlock(&ctlr->io_mutex); 4000ae7d2346SDavid Jander } 4001ae7d2346SDavid Jander 40027d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 40037d077197SDavid Brownell 4004350de7ceSAndy Shevchenko /* 4005350de7ceSAndy Shevchenko * Utility methods for SPI protocol drivers, layered on 40067d077197SDavid Brownell * top of the core. Some other utility methods are defined as 40077d077197SDavid Brownell * inline functions. 40087d077197SDavid Brownell */ 40097d077197SDavid Brownell 40105d870c8eSAndrew Morton static void spi_complete(void *arg) 40115d870c8eSAndrew Morton { 40125d870c8eSAndrew Morton complete(arg); 40135d870c8eSAndrew Morton } 40145d870c8eSAndrew Morton 4015ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message) 4016cf32b71eSErnst Schwab { 4017cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 4018cf32b71eSErnst Schwab int status; 40198caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 40200461a414SMark Brown 40210461a414SMark Brown status = __spi_validate(spi, message); 40220461a414SMark Brown if (status != 0) 40230461a414SMark Brown return status; 4024cf32b71eSErnst Schwab 40250461a414SMark Brown message->spi = spi; 4026cf32b71eSErnst Schwab 40276598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync); 40286598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); 4029eca2ebc7SMartin Sperl 4030350de7ceSAndy Shevchenko /* 4031ae7d2346SDavid Jander * Checking queue_empty here only guarantees async/sync message 4032ae7d2346SDavid Jander * ordering when coming from the same context. It does not need to 4033ae7d2346SDavid Jander * guard against reentrancy from a different context. The io_mutex 4034ae7d2346SDavid Jander * will catch those cases. 40350461a414SMark Brown */ 4036*b30f7c8eSMark Brown if (READ_ONCE(ctlr->queue_empty) && !ctlr->must_async) { 4037ae7d2346SDavid Jander message->actual_length = 0; 4038ae7d2346SDavid Jander message->status = -EINPROGRESS; 40390461a414SMark Brown 40400461a414SMark Brown trace_spi_message_submit(message); 40410461a414SMark Brown 4042ae7d2346SDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync_immediate); 4043ae7d2346SDavid Jander SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate); 40440461a414SMark Brown 4045ae7d2346SDavid Jander __spi_transfer_message_noqueue(ctlr, message); 4046ae7d2346SDavid Jander 4047ae7d2346SDavid Jander return message->status; 4048ae7d2346SDavid Jander } 4049ae7d2346SDavid Jander 4050ae7d2346SDavid Jander /* 4051ae7d2346SDavid Jander * There are messages in the async queue that could have originated 4052ae7d2346SDavid Jander * from the same context, so we need to preserve ordering. 4053ae7d2346SDavid Jander * Therefor we send the message to the async queue and wait until they 4054ae7d2346SDavid Jander * are completed. 4055ae7d2346SDavid Jander */ 4056ae7d2346SDavid Jander message->complete = spi_complete; 4057ae7d2346SDavid Jander message->context = &done; 4058cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 4059cf32b71eSErnst Schwab if (status == 0) { 4060cf32b71eSErnst Schwab wait_for_completion(&done); 4061cf32b71eSErnst Schwab status = message->status; 4062cf32b71eSErnst Schwab } 4063cf32b71eSErnst Schwab message->context = NULL; 4064ae7d2346SDavid Jander 4065cf32b71eSErnst Schwab return status; 4066cf32b71eSErnst Schwab } 4067cf32b71eSErnst Schwab 40688ae12a0dSDavid Brownell /** 40698ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 40708ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 40718ae12a0dSDavid Brownell * @message: describes the data transfers 407233e34dc6SDavid Brownell * Context: can sleep 40738ae12a0dSDavid Brownell * 40748ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 40758ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 40768ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 40778ae12a0dSDavid Brownell * 40788ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 40798ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 40808ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 40818ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 40828ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 40838ae12a0dSDavid Brownell * 40840c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 40850c868461SDavid Brownell * message will not be freed before this call returns. 40860c868461SDavid Brownell * 408797d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 40888ae12a0dSDavid Brownell */ 40898ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 40908ae12a0dSDavid Brownell { 4091ef4d96ecSMark Brown int ret; 4092ef4d96ecSMark Brown 40938caab75fSGeert Uytterhoeven mutex_lock(&spi->controller->bus_lock_mutex); 4094ef4d96ecSMark Brown ret = __spi_sync(spi, message); 40958caab75fSGeert Uytterhoeven mutex_unlock(&spi->controller->bus_lock_mutex); 4096ef4d96ecSMark Brown 4097ef4d96ecSMark Brown return ret; 40988ae12a0dSDavid Brownell } 40998ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 41008ae12a0dSDavid Brownell 4101cf32b71eSErnst Schwab /** 4102cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 4103cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 4104cf32b71eSErnst Schwab * @message: describes the data transfers 4105cf32b71eSErnst Schwab * Context: can sleep 4106cf32b71eSErnst Schwab * 4107cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 4108cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 4109cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 4110cf32b71eSErnst Schwab * 4111cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 411225985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 4113cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 4114cf32b71eSErnst Schwab * 411597d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 4116cf32b71eSErnst Schwab */ 4117cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 4118cf32b71eSErnst Schwab { 4119ef4d96ecSMark Brown return __spi_sync(spi, message); 4120cf32b71eSErnst Schwab } 4121cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 4122cf32b71eSErnst Schwab 4123cf32b71eSErnst Schwab /** 4124cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 41258caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that should be locked for exclusive bus access 4126cf32b71eSErnst Schwab * Context: can sleep 4127cf32b71eSErnst Schwab * 4128cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 4129cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 4130cf32b71eSErnst Schwab * 4131cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 4132cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 4133cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 4134cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 4135cf32b71eSErnst Schwab * 413697d56dc6SJavier Martinez Canillas * Return: always zero. 4137cf32b71eSErnst Schwab */ 41388caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr) 4139cf32b71eSErnst Schwab { 4140cf32b71eSErnst Schwab unsigned long flags; 4141cf32b71eSErnst Schwab 41428caab75fSGeert Uytterhoeven mutex_lock(&ctlr->bus_lock_mutex); 4143cf32b71eSErnst Schwab 41448caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 41458caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 1; 41468caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 4147cf32b71eSErnst Schwab 414895c8222fSDavid Jander /* Mutex remains locked until spi_bus_unlock() is called */ 4149cf32b71eSErnst Schwab 4150cf32b71eSErnst Schwab return 0; 4151cf32b71eSErnst Schwab } 4152cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 4153cf32b71eSErnst Schwab 4154cf32b71eSErnst Schwab /** 4155cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 41568caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that was locked for exclusive bus access 4157cf32b71eSErnst Schwab * Context: can sleep 4158cf32b71eSErnst Schwab * 4159cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 4160cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 4161cf32b71eSErnst Schwab * 4162cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 4163cf32b71eSErnst Schwab * call. 4164cf32b71eSErnst Schwab * 416597d56dc6SJavier Martinez Canillas * Return: always zero. 4166cf32b71eSErnst Schwab */ 41678caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr) 4168cf32b71eSErnst Schwab { 41698caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 4170cf32b71eSErnst Schwab 41718caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->bus_lock_mutex); 4172cf32b71eSErnst Schwab 4173cf32b71eSErnst Schwab return 0; 4174cf32b71eSErnst Schwab } 4175cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 4176cf32b71eSErnst Schwab 417795c8222fSDavid Jander /* Portable code must never pass more than 32 bytes */ 4178a9948b61SDavid Brownell #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 41798ae12a0dSDavid Brownell 41808ae12a0dSDavid Brownell static u8 *buf; 41818ae12a0dSDavid Brownell 41828ae12a0dSDavid Brownell /** 41838ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 41848ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 41858ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 41868ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 418727570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 418827570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 418933e34dc6SDavid Brownell * Context: can sleep 41908ae12a0dSDavid Brownell * 41918ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 41928ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 41938ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 4194b885244eSDavid Brownell * This call may only be used from a context that may sleep. 41958ae12a0dSDavid Brownell * 4196c373643bSMark Brown * Parameters to this routine are always copied using a small buffer. 419733e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 41980c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 419997d56dc6SJavier Martinez Canillas * 420097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 42018ae12a0dSDavid Brownell */ 42028ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 42030c4a1590SMark Brown const void *txbuf, unsigned n_tx, 42040c4a1590SMark Brown void *rxbuf, unsigned n_rx) 42058ae12a0dSDavid Brownell { 4206068f4070SDavid Brownell static DEFINE_MUTEX(lock); 42078ae12a0dSDavid Brownell 42088ae12a0dSDavid Brownell int status; 42098ae12a0dSDavid Brownell struct spi_message message; 4210bdff549eSDavid Brownell struct spi_transfer x[2]; 42118ae12a0dSDavid Brownell u8 *local_buf; 42128ae12a0dSDavid Brownell 4213350de7ceSAndy Shevchenko /* 4214350de7ceSAndy Shevchenko * Use preallocated DMA-safe buffer if we can. We can't avoid 4215b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 4216b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 4217b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 42188ae12a0dSDavid Brownell */ 4219b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 42202cd94c8aSMark Brown local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 42212cd94c8aSMark Brown GFP_KERNEL | GFP_DMA); 4222b3a223eeSMark Brown if (!local_buf) 4223b3a223eeSMark Brown return -ENOMEM; 4224b3a223eeSMark Brown } else { 4225b3a223eeSMark Brown local_buf = buf; 4226b3a223eeSMark Brown } 42278ae12a0dSDavid Brownell 42288275c642SVitaly Wool spi_message_init(&message); 42295fe5f05eSJingoo Han memset(x, 0, sizeof(x)); 4230bdff549eSDavid Brownell if (n_tx) { 4231bdff549eSDavid Brownell x[0].len = n_tx; 4232bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 4233bdff549eSDavid Brownell } 4234bdff549eSDavid Brownell if (n_rx) { 4235bdff549eSDavid Brownell x[1].len = n_rx; 4236bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 4237bdff549eSDavid Brownell } 42388275c642SVitaly Wool 42398ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 4240bdff549eSDavid Brownell x[0].tx_buf = local_buf; 4241bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 42428ae12a0dSDavid Brownell 424395c8222fSDavid Jander /* Do the i/o */ 42448ae12a0dSDavid Brownell status = spi_sync(spi, &message); 42459b938b74SMarc Pignat if (status == 0) 4246bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 42478ae12a0dSDavid Brownell 4248bdff549eSDavid Brownell if (x[0].tx_buf == buf) 4249068f4070SDavid Brownell mutex_unlock(&lock); 42508ae12a0dSDavid Brownell else 42518ae12a0dSDavid Brownell kfree(local_buf); 42528ae12a0dSDavid Brownell 42538ae12a0dSDavid Brownell return status; 42548ae12a0dSDavid Brownell } 42558ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 42568ae12a0dSDavid Brownell 42578ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 42588ae12a0dSDavid Brownell 4259da21fde0SUwe Kleine-König #if IS_ENABLED(CONFIG_OF_DYNAMIC) 426095c8222fSDavid Jander /* Must call put_device() when done with returned spi_device device */ 4261da21fde0SUwe Kleine-König static struct spi_device *of_find_spi_device_by_node(struct device_node *node) 4262ce79d54aSPantelis Antoniou { 4263cfba5de9SSuzuki K Poulose struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); 4264cfba5de9SSuzuki K Poulose 4265ce79d54aSPantelis Antoniou return dev ? to_spi_device(dev) : NULL; 4266ce79d54aSPantelis Antoniou } 4267ce79d54aSPantelis Antoniou 426895c8222fSDavid Jander /* The spi controllers are not using spi_bus, so we find it with another way */ 42698caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 4270ce79d54aSPantelis Antoniou { 4271ce79d54aSPantelis Antoniou struct device *dev; 4272ce79d54aSPantelis Antoniou 4273cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_master_class, node); 42746c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 4275cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_slave_class, node); 4276ce79d54aSPantelis Antoniou if (!dev) 4277ce79d54aSPantelis Antoniou return NULL; 4278ce79d54aSPantelis Antoniou 427995c8222fSDavid Jander /* Reference got in class_find_device */ 42808caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 4281ce79d54aSPantelis Antoniou } 4282ce79d54aSPantelis Antoniou 4283ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action, 4284ce79d54aSPantelis Antoniou void *arg) 4285ce79d54aSPantelis Antoniou { 4286ce79d54aSPantelis Antoniou struct of_reconfig_data *rd = arg; 42878caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 4288ce79d54aSPantelis Antoniou struct spi_device *spi; 4289ce79d54aSPantelis Antoniou 4290ce79d54aSPantelis Antoniou switch (of_reconfig_get_state_change(action, arg)) { 4291ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 42928caab75fSGeert Uytterhoeven ctlr = of_find_spi_controller_by_node(rd->dn->parent); 42938caab75fSGeert Uytterhoeven if (ctlr == NULL) 429495c8222fSDavid Jander return NOTIFY_OK; /* Not for us */ 4295ce79d54aSPantelis Antoniou 4296bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 42978caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4298bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4299bd6c1644SGeert Uytterhoeven } 4300bd6c1644SGeert Uytterhoeven 43018caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, rd->dn); 43028caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4303ce79d54aSPantelis Antoniou 4304ce79d54aSPantelis Antoniou if (IS_ERR(spi)) { 430525c56c88SRob Herring pr_err("%s: failed to create for '%pOF'\n", 430625c56c88SRob Herring __func__, rd->dn); 4307e0af98a7SRalf Ramsauer of_node_clear_flag(rd->dn, OF_POPULATED); 4308ce79d54aSPantelis Antoniou return notifier_from_errno(PTR_ERR(spi)); 4309ce79d54aSPantelis Antoniou } 4310ce79d54aSPantelis Antoniou break; 4311ce79d54aSPantelis Antoniou 4312ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 431395c8222fSDavid Jander /* Already depopulated? */ 4314bd6c1644SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 4315bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4316bd6c1644SGeert Uytterhoeven 431795c8222fSDavid Jander /* Find our device by node */ 4318ce79d54aSPantelis Antoniou spi = of_find_spi_device_by_node(rd->dn); 4319ce79d54aSPantelis Antoniou if (spi == NULL) 432095c8222fSDavid Jander return NOTIFY_OK; /* No? not meant for us */ 4321ce79d54aSPantelis Antoniou 432295c8222fSDavid Jander /* Unregister takes one ref away */ 4323ce79d54aSPantelis Antoniou spi_unregister_device(spi); 4324ce79d54aSPantelis Antoniou 432595c8222fSDavid Jander /* And put the reference of the find */ 4326ce79d54aSPantelis Antoniou put_device(&spi->dev); 4327ce79d54aSPantelis Antoniou break; 4328ce79d54aSPantelis Antoniou } 4329ce79d54aSPantelis Antoniou 4330ce79d54aSPantelis Antoniou return NOTIFY_OK; 4331ce79d54aSPantelis Antoniou } 4332ce79d54aSPantelis Antoniou 4333ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = { 4334ce79d54aSPantelis Antoniou .notifier_call = of_spi_notify, 4335ce79d54aSPantelis Antoniou }; 4336ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4337ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier; 4338ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4339ce79d54aSPantelis Antoniou 43407f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI) 43418caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data) 43427f24467fSOctavian Purdila { 43437f24467fSOctavian Purdila return ACPI_COMPANION(dev->parent) == data; 43447f24467fSOctavian Purdila } 43457f24467fSOctavian Purdila 43468caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 43477f24467fSOctavian Purdila { 43487f24467fSOctavian Purdila struct device *dev; 43497f24467fSOctavian Purdila 43507f24467fSOctavian Purdila dev = class_find_device(&spi_master_class, NULL, adev, 43518caab75fSGeert Uytterhoeven spi_acpi_controller_match); 43526c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 43536c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, adev, 43548caab75fSGeert Uytterhoeven spi_acpi_controller_match); 43557f24467fSOctavian Purdila if (!dev) 43567f24467fSOctavian Purdila return NULL; 43577f24467fSOctavian Purdila 43588caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 43597f24467fSOctavian Purdila } 43607f24467fSOctavian Purdila 43617f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 43627f24467fSOctavian Purdila { 43637f24467fSOctavian Purdila struct device *dev; 43647f24467fSOctavian Purdila 436500500147SSuzuki K Poulose dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev); 43665b16668eSWolfram Sang return to_spi_device(dev); 43677f24467fSOctavian Purdila } 43687f24467fSOctavian Purdila 43697f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 43707f24467fSOctavian Purdila void *arg) 43717f24467fSOctavian Purdila { 43727f24467fSOctavian Purdila struct acpi_device *adev = arg; 43738caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 43747f24467fSOctavian Purdila struct spi_device *spi; 43757f24467fSOctavian Purdila 43767f24467fSOctavian Purdila switch (value) { 43777f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_ADD: 43788caab75fSGeert Uytterhoeven ctlr = acpi_spi_find_controller_by_adev(adev->parent); 43798caab75fSGeert Uytterhoeven if (!ctlr) 43807f24467fSOctavian Purdila break; 43817f24467fSOctavian Purdila 43828caab75fSGeert Uytterhoeven acpi_register_spi_device(ctlr, adev); 43838caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 43847f24467fSOctavian Purdila break; 43857f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_REMOVE: 43867f24467fSOctavian Purdila if (!acpi_device_enumerated(adev)) 43877f24467fSOctavian Purdila break; 43887f24467fSOctavian Purdila 43897f24467fSOctavian Purdila spi = acpi_spi_find_device_by_adev(adev); 43907f24467fSOctavian Purdila if (!spi) 43917f24467fSOctavian Purdila break; 43927f24467fSOctavian Purdila 43937f24467fSOctavian Purdila spi_unregister_device(spi); 43947f24467fSOctavian Purdila put_device(&spi->dev); 43957f24467fSOctavian Purdila break; 43967f24467fSOctavian Purdila } 43977f24467fSOctavian Purdila 43987f24467fSOctavian Purdila return NOTIFY_OK; 43997f24467fSOctavian Purdila } 44007f24467fSOctavian Purdila 44017f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = { 44027f24467fSOctavian Purdila .notifier_call = acpi_spi_notify, 44037f24467fSOctavian Purdila }; 44047f24467fSOctavian Purdila #else 44057f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier; 44067f24467fSOctavian Purdila #endif 44077f24467fSOctavian Purdila 44088ae12a0dSDavid Brownell static int __init spi_init(void) 44098ae12a0dSDavid Brownell { 4410b885244eSDavid Brownell int status; 44118ae12a0dSDavid Brownell 4412e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 4413b885244eSDavid Brownell if (!buf) { 4414b885244eSDavid Brownell status = -ENOMEM; 4415b885244eSDavid Brownell goto err0; 44168ae12a0dSDavid Brownell } 4417b885244eSDavid Brownell 4418b885244eSDavid Brownell status = bus_register(&spi_bus_type); 4419b885244eSDavid Brownell if (status < 0) 4420b885244eSDavid Brownell goto err1; 4421b885244eSDavid Brownell 4422b885244eSDavid Brownell status = class_register(&spi_master_class); 4423b885244eSDavid Brownell if (status < 0) 4424b885244eSDavid Brownell goto err2; 4425ce79d54aSPantelis Antoniou 44266c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 44276c364062SGeert Uytterhoeven status = class_register(&spi_slave_class); 44286c364062SGeert Uytterhoeven if (status < 0) 44296c364062SGeert Uytterhoeven goto err3; 44306c364062SGeert Uytterhoeven } 44316c364062SGeert Uytterhoeven 44325267720eSFabio Estevam if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 4433ce79d54aSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 44347f24467fSOctavian Purdila if (IS_ENABLED(CONFIG_ACPI)) 44357f24467fSOctavian Purdila WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 4436ce79d54aSPantelis Antoniou 4437b885244eSDavid Brownell return 0; 4438b885244eSDavid Brownell 44396c364062SGeert Uytterhoeven err3: 44406c364062SGeert Uytterhoeven class_unregister(&spi_master_class); 4441b885244eSDavid Brownell err2: 4442b885244eSDavid Brownell bus_unregister(&spi_bus_type); 4443b885244eSDavid Brownell err1: 4444b885244eSDavid Brownell kfree(buf); 4445b885244eSDavid Brownell buf = NULL; 4446b885244eSDavid Brownell err0: 4447b885244eSDavid Brownell return status; 4448b885244eSDavid Brownell } 4449b885244eSDavid Brownell 4450350de7ceSAndy Shevchenko /* 4451350de7ceSAndy Shevchenko * A board_info is normally registered in arch_initcall(), 4452350de7ceSAndy Shevchenko * but even essential drivers wait till later. 4453b885244eSDavid Brownell * 4454350de7ceSAndy Shevchenko * REVISIT only boardinfo really needs static linking. The rest (device and 4455350de7ceSAndy Shevchenko * driver registration) _could_ be dynamically linked (modular) ... Costs 4456b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 44578ae12a0dSDavid Brownell */ 4458673c0c00SDavid Brownell postcore_initcall(spi_init); 4459