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: 1357b2c98153SAlexandru Ardelean /* 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 14060ff2de8bSMartin Sperl /* 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 1616ae7d2346SDavid Jander ret = ctlr->transfer_one_message(ctlr, msg); 1617ae7d2346SDavid Jander if (ret) { 1618ae7d2346SDavid Jander dev_err(&ctlr->dev, 1619ae7d2346SDavid Jander "failed to transfer one message from queue\n"); 1620ae7d2346SDavid Jander return ret; 1621ae7d2346SDavid Jander } 1622ae7d2346SDavid Jander 1623ae7d2346SDavid Jander return 0; 1624ae7d2346SDavid Jander } 1625ae7d2346SDavid Jander 1626ffbbdd21SLinus Walleij /** 1627fc9e0f71SMark Brown * __spi_pump_messages - function which processes spi message queue 16288caab75fSGeert Uytterhoeven * @ctlr: controller to process queue for 1629fc9e0f71SMark Brown * @in_kthread: true if we are in the context of the message pump thread 1630ffbbdd21SLinus Walleij * 1631ffbbdd21SLinus Walleij * This function checks if there is any spi message in the queue that 1632ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 1633ffbbdd21SLinus Walleij * and transfer each message. 1634ffbbdd21SLinus Walleij * 16350461a414SMark Brown * Note that it is called both from the kthread itself and also from 16360461a414SMark Brown * inside spi_sync(); the queue extraction handling at the top of the 16370461a414SMark Brown * function should deal with this safely. 1638ffbbdd21SLinus Walleij */ 16398caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1640ffbbdd21SLinus Walleij { 1641d1c44c93SVladimir Oltean struct spi_message *msg; 1642ffbbdd21SLinus Walleij bool was_busy = false; 1643d1c44c93SVladimir Oltean unsigned long flags; 1644ffbbdd21SLinus Walleij int ret; 1645ffbbdd21SLinus Walleij 1646c1038165SDavid Jander /* Take the IO mutex */ 1647c1038165SDavid Jander mutex_lock(&ctlr->io_mutex); 1648c1038165SDavid Jander 1649983aee5dSMark Brown /* Lock queue */ 16508caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1651983aee5dSMark Brown 1652983aee5dSMark Brown /* Make sure we are not already running a message */ 16538711a2abSDavid Jander if (ctlr->cur_msg) 1654c1038165SDavid Jander goto out_unlock; 1655983aee5dSMark Brown 1656983aee5dSMark Brown /* Check if the queue is idle */ 16578caab75fSGeert Uytterhoeven if (list_empty(&ctlr->queue) || !ctlr->running) { 16588711a2abSDavid Jander if (!ctlr->busy) 1659c1038165SDavid Jander goto out_unlock; 1660fc9e0f71SMark Brown 1661e1268597SMark Brown /* Defer any non-atomic teardown to the thread */ 1662f0125f1aSMark Brown if (!in_kthread) { 1663e1268597SMark Brown if (!ctlr->dummy_rx && !ctlr->dummy_tx && 1664e1268597SMark Brown !ctlr->unprepare_transfer_hardware) { 1665e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1666e1268597SMark Brown ctlr->busy = false; 1667ae7d2346SDavid Jander ctlr->queue_empty = true; 1668e1268597SMark Brown trace_spi_controller_idle(ctlr); 1669e1268597SMark Brown } else { 167060a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, 1671f0125f1aSMark Brown &ctlr->pump_messages); 1672e1268597SMark Brown } 1673c1038165SDavid Jander goto out_unlock; 1674f0125f1aSMark Brown } 1675f0125f1aSMark Brown 1676f0125f1aSMark Brown ctlr->busy = false; 1677f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1678f0125f1aSMark Brown 1679f0125f1aSMark Brown kfree(ctlr->dummy_rx); 1680f0125f1aSMark Brown ctlr->dummy_rx = NULL; 1681f0125f1aSMark Brown kfree(ctlr->dummy_tx); 1682f0125f1aSMark Brown ctlr->dummy_tx = NULL; 1683f0125f1aSMark Brown if (ctlr->unprepare_transfer_hardware && 1684f0125f1aSMark Brown ctlr->unprepare_transfer_hardware(ctlr)) 1685f0125f1aSMark Brown dev_err(&ctlr->dev, 1686f0125f1aSMark Brown "failed to unprepare transfer hardware\n"); 1687e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1688f0125f1aSMark Brown trace_spi_controller_idle(ctlr); 1689f0125f1aSMark Brown 1690f0125f1aSMark Brown spin_lock_irqsave(&ctlr->queue_lock, flags); 1691ae7d2346SDavid Jander ctlr->queue_empty = true; 1692c1038165SDavid Jander goto out_unlock; 1693ffbbdd21SLinus Walleij } 1694ffbbdd21SLinus Walleij 1695ffbbdd21SLinus Walleij /* Extract head of queue */ 1696d1c44c93SVladimir Oltean msg = list_first_entry(&ctlr->queue, struct spi_message, queue); 1697d1c44c93SVladimir Oltean ctlr->cur_msg = msg; 1698ffbbdd21SLinus Walleij 1699d1c44c93SVladimir Oltean list_del_init(&msg->queue); 17008caab75fSGeert Uytterhoeven if (ctlr->busy) 1701ffbbdd21SLinus Walleij was_busy = true; 1702ffbbdd21SLinus Walleij else 17038caab75fSGeert Uytterhoeven ctlr->busy = true; 17048caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1705ffbbdd21SLinus Walleij 1706ae7d2346SDavid Jander ret = __spi_pump_transfer_message(ctlr, msg, was_busy); 17078caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 170862826970SMark Brown 170962826970SMark Brown /* Prod the scheduler in case transfer_one() was busy waiting */ 171049023d2eSJon Hunter if (!ret) 171162826970SMark Brown cond_resched(); 1712c1038165SDavid Jander return; 1713c1038165SDavid Jander 1714c1038165SDavid Jander out_unlock: 17158711a2abSDavid Jander spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1716c1038165SDavid Jander mutex_unlock(&ctlr->io_mutex); 1717ffbbdd21SLinus Walleij } 1718ffbbdd21SLinus Walleij 1719fc9e0f71SMark Brown /** 1720fc9e0f71SMark Brown * spi_pump_messages - kthread work function which processes spi message queue 17218caab75fSGeert Uytterhoeven * @work: pointer to kthread work struct contained in the controller struct 1722fc9e0f71SMark Brown */ 1723fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work) 1724fc9e0f71SMark Brown { 17258caab75fSGeert Uytterhoeven struct spi_controller *ctlr = 17268caab75fSGeert Uytterhoeven container_of(work, struct spi_controller, pump_messages); 1727fc9e0f71SMark Brown 17288caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, true); 1729fc9e0f71SMark Brown } 1730fc9e0f71SMark Brown 1731924b5867SDouglas Anderson /** 1732350de7ceSAndy Shevchenko * spi_take_timestamp_pre - helper to collect the beginning of the TX timestamp 1733b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1734b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1735862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1736b42faeeeSVladimir Oltean * @irqs_off: If true, will disable IRQs and preemption for the duration of the 1737b42faeeeSVladimir Oltean * transfer, for less jitter in time measurement. Only compatible 1738b42faeeeSVladimir Oltean * with PIO drivers. If true, must follow up with 1739b42faeeeSVladimir Oltean * spi_take_timestamp_post or otherwise system will crash. 1740b42faeeeSVladimir Oltean * WARNING: for fully predictable results, the CPU frequency must 1741b42faeeeSVladimir Oltean * also be under control (governor). 1742350de7ceSAndy Shevchenko * 1743350de7ceSAndy Shevchenko * This is a helper for drivers to collect the beginning of the TX timestamp 1744350de7ceSAndy Shevchenko * for the requested byte from the SPI transfer. The frequency with which this 1745350de7ceSAndy Shevchenko * function must be called (once per word, once for the whole transfer, once 1746350de7ceSAndy Shevchenko * per batch of words etc) is arbitrary as long as the @tx buffer offset is 1747350de7ceSAndy Shevchenko * greater than or equal to the requested byte at the time of the call. The 1748350de7ceSAndy Shevchenko * timestamp is only taken once, at the first such call. It is assumed that 1749350de7ceSAndy Shevchenko * the driver advances its @tx buffer pointer monotonically. 1750b42faeeeSVladimir Oltean */ 1751b42faeeeSVladimir Oltean void spi_take_timestamp_pre(struct spi_controller *ctlr, 1752b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1753862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1754b42faeeeSVladimir Oltean { 1755b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1756b42faeeeSVladimir Oltean return; 1757b42faeeeSVladimir Oltean 17586a726824SVladimir Oltean if (xfer->timestamped) 1759b42faeeeSVladimir Oltean return; 1760b42faeeeSVladimir Oltean 17616a726824SVladimir Oltean if (progress > xfer->ptp_sts_word_pre) 1762b42faeeeSVladimir Oltean return; 1763b42faeeeSVladimir Oltean 1764b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1765862dd2a9SVladimir Oltean xfer->ptp_sts_word_pre = progress; 1766b42faeeeSVladimir Oltean 1767b42faeeeSVladimir Oltean if (irqs_off) { 1768b42faeeeSVladimir Oltean local_irq_save(ctlr->irq_flags); 1769b42faeeeSVladimir Oltean preempt_disable(); 1770b42faeeeSVladimir Oltean } 1771b42faeeeSVladimir Oltean 1772b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1773b42faeeeSVladimir Oltean } 1774b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_pre); 1775b42faeeeSVladimir Oltean 1776b42faeeeSVladimir Oltean /** 1777350de7ceSAndy Shevchenko * spi_take_timestamp_post - helper to collect the end of the TX timestamp 1778b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1779b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1780862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1781b42faeeeSVladimir Oltean * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU. 1782350de7ceSAndy Shevchenko * 1783350de7ceSAndy Shevchenko * This is a helper for drivers to collect the end of the TX timestamp for 1784350de7ceSAndy Shevchenko * the requested byte from the SPI transfer. Can be called with an arbitrary 1785350de7ceSAndy Shevchenko * frequency: only the first call where @tx exceeds or is equal to the 1786350de7ceSAndy Shevchenko * requested word will be timestamped. 1787b42faeeeSVladimir Oltean */ 1788b42faeeeSVladimir Oltean void spi_take_timestamp_post(struct spi_controller *ctlr, 1789b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1790862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1791b42faeeeSVladimir Oltean { 1792b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1793b42faeeeSVladimir Oltean return; 1794b42faeeeSVladimir Oltean 17956a726824SVladimir Oltean if (xfer->timestamped) 1796b42faeeeSVladimir Oltean return; 1797b42faeeeSVladimir Oltean 1798862dd2a9SVladimir Oltean if (progress < xfer->ptp_sts_word_post) 1799b42faeeeSVladimir Oltean return; 1800b42faeeeSVladimir Oltean 1801b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1802b42faeeeSVladimir Oltean 1803b42faeeeSVladimir Oltean if (irqs_off) { 1804b42faeeeSVladimir Oltean local_irq_restore(ctlr->irq_flags); 1805b42faeeeSVladimir Oltean preempt_enable(); 1806b42faeeeSVladimir Oltean } 1807b42faeeeSVladimir Oltean 1808b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1809862dd2a9SVladimir Oltean xfer->ptp_sts_word_post = progress; 1810b42faeeeSVladimir Oltean 18116a726824SVladimir Oltean xfer->timestamped = true; 1812b42faeeeSVladimir Oltean } 1813b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_post); 1814b42faeeeSVladimir Oltean 1815b42faeeeSVladimir Oltean /** 1816924b5867SDouglas Anderson * spi_set_thread_rt - set the controller to pump at realtime priority 1817924b5867SDouglas Anderson * @ctlr: controller to boost priority of 1818924b5867SDouglas Anderson * 1819924b5867SDouglas Anderson * This can be called because the controller requested realtime priority 1820924b5867SDouglas Anderson * (by setting the ->rt value before calling spi_register_controller()) or 1821924b5867SDouglas Anderson * because a device on the bus said that its transfers needed realtime 1822924b5867SDouglas Anderson * priority. 1823924b5867SDouglas Anderson * 1824924b5867SDouglas Anderson * NOTE: at the moment if any device on a bus says it needs realtime then 1825924b5867SDouglas Anderson * the thread will be at realtime priority for all transfers on that 1826924b5867SDouglas Anderson * controller. If this eventually becomes a problem we may see if we can 1827924b5867SDouglas Anderson * find a way to boost the priority only temporarily during relevant 1828924b5867SDouglas Anderson * transfers. 1829924b5867SDouglas Anderson */ 1830924b5867SDouglas Anderson static void spi_set_thread_rt(struct spi_controller *ctlr) 1831ffbbdd21SLinus Walleij { 1832924b5867SDouglas Anderson dev_info(&ctlr->dev, 1833924b5867SDouglas Anderson "will run message pump with realtime priority\n"); 18346d2b84a4SLinus Torvalds sched_set_fifo(ctlr->kworker->task); 1835924b5867SDouglas Anderson } 1836924b5867SDouglas Anderson 1837924b5867SDouglas Anderson static int spi_init_queue(struct spi_controller *ctlr) 1838924b5867SDouglas Anderson { 18398caab75fSGeert Uytterhoeven ctlr->running = false; 18408caab75fSGeert Uytterhoeven ctlr->busy = false; 1841ae7d2346SDavid Jander ctlr->queue_empty = true; 1842ffbbdd21SLinus Walleij 184360a883d1SMarek Szyprowski ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); 184460a883d1SMarek Szyprowski if (IS_ERR(ctlr->kworker)) { 184560a883d1SMarek Szyprowski dev_err(&ctlr->dev, "failed to create message pump kworker\n"); 184660a883d1SMarek Szyprowski return PTR_ERR(ctlr->kworker); 1847ffbbdd21SLinus Walleij } 184860a883d1SMarek Szyprowski 18498caab75fSGeert Uytterhoeven kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1850f0125f1aSMark Brown 1851ffbbdd21SLinus Walleij /* 18528caab75fSGeert Uytterhoeven * Controller config will indicate if this controller should run the 1853ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 1854ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 1855ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 1856ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 1857ffbbdd21SLinus Walleij */ 1858924b5867SDouglas Anderson if (ctlr->rt) 1859924b5867SDouglas Anderson spi_set_thread_rt(ctlr); 1860ffbbdd21SLinus Walleij 1861ffbbdd21SLinus Walleij return 0; 1862ffbbdd21SLinus Walleij } 1863ffbbdd21SLinus Walleij 1864ffbbdd21SLinus Walleij /** 1865ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 1866ffbbdd21SLinus Walleij * messages 18678caab75fSGeert Uytterhoeven * @ctlr: the controller to check for queued messages 1868ffbbdd21SLinus Walleij * 1869ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 1870ffbbdd21SLinus Walleij * this call. 187197d56dc6SJavier Martinez Canillas * 187297d56dc6SJavier Martinez Canillas * Return: the next message in the queue, else NULL if the queue is empty. 1873ffbbdd21SLinus Walleij */ 18748caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1875ffbbdd21SLinus Walleij { 1876ffbbdd21SLinus Walleij struct spi_message *next; 1877ffbbdd21SLinus Walleij unsigned long flags; 1878ffbbdd21SLinus Walleij 1879ffbbdd21SLinus Walleij /* get a pointer to the next message, if any */ 18808caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 18818caab75fSGeert Uytterhoeven next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 18821cfd97f9SAxel Lin queue); 18838caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1884ffbbdd21SLinus Walleij 1885ffbbdd21SLinus Walleij return next; 1886ffbbdd21SLinus Walleij } 1887ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1888ffbbdd21SLinus Walleij 1889ffbbdd21SLinus Walleij /** 1890ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 18918caab75fSGeert Uytterhoeven * @ctlr: the controller to return the message to 1892ffbbdd21SLinus Walleij * 1893ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 1894ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 1895ffbbdd21SLinus Walleij */ 18968caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr) 1897ffbbdd21SLinus Walleij { 1898b42faeeeSVladimir Oltean struct spi_transfer *xfer; 1899ffbbdd21SLinus Walleij struct spi_message *mesg; 1900ffbbdd21SLinus Walleij unsigned long flags; 19012841a5fcSMark Brown int ret; 1902ffbbdd21SLinus Walleij 19038caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 19048caab75fSGeert Uytterhoeven mesg = ctlr->cur_msg; 19058caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1906ffbbdd21SLinus Walleij 1907b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1908b42faeeeSVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) { 1909b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1910b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 1911b42faeeeSVladimir Oltean } 1912b42faeeeSVladimir Oltean } 1913b42faeeeSVladimir Oltean 19146a726824SVladimir Oltean if (unlikely(ctlr->ptp_sts_supported)) 19156a726824SVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) 19166a726824SVladimir Oltean WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); 1917f971a207SVladimir Oltean 19188caab75fSGeert Uytterhoeven spi_unmap_msg(ctlr, mesg); 191999adef31SMark Brown 1920350de7ceSAndy Shevchenko /* 1921350de7ceSAndy Shevchenko * In the prepare_messages callback the SPI bus has the opportunity 1922350de7ceSAndy Shevchenko * to split a transfer to smaller chunks. 1923350de7ceSAndy Shevchenko * 1924350de7ceSAndy Shevchenko * Release the split transfers here since spi_map_msg() is done on 1925350de7ceSAndy Shevchenko * the split transfers. 1926b59a7ca1SGustav Wiklander */ 1927b59a7ca1SGustav Wiklander spi_res_release(ctlr, mesg); 1928b59a7ca1SGustav Wiklander 19291714582aSDavid Jander if (mesg->prepared && ctlr->unprepare_message) { 19308caab75fSGeert Uytterhoeven ret = ctlr->unprepare_message(ctlr, mesg); 19312841a5fcSMark Brown if (ret) { 19328caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 19338caab75fSGeert Uytterhoeven ret); 19342841a5fcSMark Brown } 19352841a5fcSMark Brown } 1936391949b6SUwe Kleine-König 19371714582aSDavid Jander mesg->prepared = false; 19381714582aSDavid Jander 1939ae7d2346SDavid Jander if (!mesg->sync) { 1940ae7d2346SDavid Jander /* 1941ae7d2346SDavid Jander * This message was sent via the async message queue. Handle 1942ae7d2346SDavid Jander * the queue and kick the worker thread to do the 1943ae7d2346SDavid Jander * idling/shutdown or send the next message if needed. 1944ae7d2346SDavid Jander */ 19458caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1946ae7d2346SDavid Jander WARN(ctlr->cur_msg != mesg, 1947ae7d2346SDavid Jander "Finalizing queued message that is not the current head of queue!"); 19488caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 1949809b1b04SRobin Gong ctlr->fallback = false; 195060a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 19518caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1952ae7d2346SDavid Jander } 19538e76ef88SMartin Sperl 19548e76ef88SMartin Sperl trace_spi_message_done(mesg); 19552841a5fcSMark Brown 1956ffbbdd21SLinus Walleij mesg->state = NULL; 1957ffbbdd21SLinus Walleij if (mesg->complete) 1958ffbbdd21SLinus Walleij mesg->complete(mesg->context); 1959ffbbdd21SLinus Walleij } 1960ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 1961ffbbdd21SLinus Walleij 19628caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr) 1963ffbbdd21SLinus Walleij { 1964ffbbdd21SLinus Walleij unsigned long flags; 1965ffbbdd21SLinus Walleij 19668caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1967ffbbdd21SLinus Walleij 19688caab75fSGeert Uytterhoeven if (ctlr->running || ctlr->busy) { 19698caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1970ffbbdd21SLinus Walleij return -EBUSY; 1971ffbbdd21SLinus Walleij } 1972ffbbdd21SLinus Walleij 19738caab75fSGeert Uytterhoeven ctlr->running = true; 19748caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 19758caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1976ffbbdd21SLinus Walleij 197760a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1978ffbbdd21SLinus Walleij 1979ffbbdd21SLinus Walleij return 0; 1980ffbbdd21SLinus Walleij } 1981ffbbdd21SLinus Walleij 19828caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr) 1983ffbbdd21SLinus Walleij { 1984ffbbdd21SLinus Walleij unsigned long flags; 1985ffbbdd21SLinus Walleij unsigned limit = 500; 1986ffbbdd21SLinus Walleij int ret = 0; 1987ffbbdd21SLinus Walleij 19888caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1989ffbbdd21SLinus Walleij 1990ffbbdd21SLinus Walleij /* 1991ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 19928caab75fSGeert Uytterhoeven * A wait_queue on the ctlr->busy could be used, but then the common 1993ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 1994ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 1995ffbbdd21SLinus Walleij */ 19968caab75fSGeert Uytterhoeven while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 19978caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1998f97b26b0SAxel Lin usleep_range(10000, 11000); 19998caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 2000ffbbdd21SLinus Walleij } 2001ffbbdd21SLinus Walleij 20028caab75fSGeert Uytterhoeven if (!list_empty(&ctlr->queue) || ctlr->busy) 2003ffbbdd21SLinus Walleij ret = -EBUSY; 2004ffbbdd21SLinus Walleij else 20058caab75fSGeert Uytterhoeven ctlr->running = false; 2006ffbbdd21SLinus Walleij 20078caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2008ffbbdd21SLinus Walleij 2009ffbbdd21SLinus Walleij if (ret) { 20108caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "could not stop message queue\n"); 2011ffbbdd21SLinus Walleij return ret; 2012ffbbdd21SLinus Walleij } 2013ffbbdd21SLinus Walleij return ret; 2014ffbbdd21SLinus Walleij } 2015ffbbdd21SLinus Walleij 20168caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr) 2017ffbbdd21SLinus Walleij { 2018ffbbdd21SLinus Walleij int ret; 2019ffbbdd21SLinus Walleij 20208caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 2021ffbbdd21SLinus Walleij 2022ffbbdd21SLinus Walleij /* 20233989144fSPetr Mladek * kthread_flush_worker will block until all work is done. 2024ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 2025ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 2026ffbbdd21SLinus Walleij * return anyway. 2027ffbbdd21SLinus Walleij */ 2028ffbbdd21SLinus Walleij if (ret) { 20298caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem destroying queue\n"); 2030ffbbdd21SLinus Walleij return ret; 2031ffbbdd21SLinus Walleij } 2032ffbbdd21SLinus Walleij 203360a883d1SMarek Szyprowski kthread_destroy_worker(ctlr->kworker); 2034ffbbdd21SLinus Walleij 2035ffbbdd21SLinus Walleij return 0; 2036ffbbdd21SLinus Walleij } 2037ffbbdd21SLinus Walleij 20380461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi, 20390461a414SMark Brown struct spi_message *msg, 20400461a414SMark Brown bool need_pump) 2041ffbbdd21SLinus Walleij { 20428caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 2043ffbbdd21SLinus Walleij unsigned long flags; 2044ffbbdd21SLinus Walleij 20458caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 2046ffbbdd21SLinus Walleij 20478caab75fSGeert Uytterhoeven if (!ctlr->running) { 20488caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2049ffbbdd21SLinus Walleij return -ESHUTDOWN; 2050ffbbdd21SLinus Walleij } 2051ffbbdd21SLinus Walleij msg->actual_length = 0; 2052ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 2053ffbbdd21SLinus Walleij 20548caab75fSGeert Uytterhoeven list_add_tail(&msg->queue, &ctlr->queue); 2055ae7d2346SDavid Jander ctlr->queue_empty = false; 2056f0125f1aSMark Brown if (!ctlr->busy && need_pump) 205760a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 2058ffbbdd21SLinus Walleij 20598caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 2060ffbbdd21SLinus Walleij return 0; 2061ffbbdd21SLinus Walleij } 2062ffbbdd21SLinus Walleij 20630461a414SMark Brown /** 20640461a414SMark Brown * spi_queued_transfer - transfer function for queued transfers 20650461a414SMark Brown * @spi: spi device which is requesting transfer 20660461a414SMark Brown * @msg: spi message which is to handled is queued to driver queue 206797d56dc6SJavier Martinez Canillas * 206897d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 20690461a414SMark Brown */ 20700461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 20710461a414SMark Brown { 20720461a414SMark Brown return __spi_queued_transfer(spi, msg, true); 20730461a414SMark Brown } 20740461a414SMark Brown 20758caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr) 2076ffbbdd21SLinus Walleij { 2077ffbbdd21SLinus Walleij int ret; 2078ffbbdd21SLinus Walleij 20798caab75fSGeert Uytterhoeven ctlr->transfer = spi_queued_transfer; 20808caab75fSGeert Uytterhoeven if (!ctlr->transfer_one_message) 20818caab75fSGeert Uytterhoeven ctlr->transfer_one_message = spi_transfer_one_message; 2082ffbbdd21SLinus Walleij 2083ffbbdd21SLinus Walleij /* Initialize and start queue */ 20848caab75fSGeert Uytterhoeven ret = spi_init_queue(ctlr); 2085ffbbdd21SLinus Walleij if (ret) { 20868caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem initializing queue\n"); 2087ffbbdd21SLinus Walleij goto err_init_queue; 2088ffbbdd21SLinus Walleij } 20898caab75fSGeert Uytterhoeven ctlr->queued = true; 20908caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 2091ffbbdd21SLinus Walleij if (ret) { 20928caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem starting queue\n"); 2093ffbbdd21SLinus Walleij goto err_start_queue; 2094ffbbdd21SLinus Walleij } 2095ffbbdd21SLinus Walleij 2096ffbbdd21SLinus Walleij return 0; 2097ffbbdd21SLinus Walleij 2098ffbbdd21SLinus Walleij err_start_queue: 20998caab75fSGeert Uytterhoeven spi_destroy_queue(ctlr); 2100c3676d5cSMark Brown err_init_queue: 2101ffbbdd21SLinus Walleij return ret; 2102ffbbdd21SLinus Walleij } 2103ffbbdd21SLinus Walleij 2104988f259bSBoris Brezillon /** 2105988f259bSBoris Brezillon * spi_flush_queue - Send all pending messages in the queue from the callers' 2106988f259bSBoris Brezillon * context 2107988f259bSBoris Brezillon * @ctlr: controller to process queue for 2108988f259bSBoris Brezillon * 2109988f259bSBoris Brezillon * This should be used when one wants to ensure all pending messages have been 2110988f259bSBoris Brezillon * sent before doing something. Is used by the spi-mem code to make sure SPI 2111988f259bSBoris Brezillon * memory operations do not preempt regular SPI transfers that have been queued 2112988f259bSBoris Brezillon * before the spi-mem operation. 2113988f259bSBoris Brezillon */ 2114988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr) 2115988f259bSBoris Brezillon { 2116988f259bSBoris Brezillon if (ctlr->transfer == spi_queued_transfer) 2117988f259bSBoris Brezillon __spi_pump_messages(ctlr, false); 2118988f259bSBoris Brezillon } 2119988f259bSBoris Brezillon 2120ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 2121ffbbdd21SLinus Walleij 21227cb94361SAndreas Larsson #if defined(CONFIG_OF) 21238caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 2124c2e51ac3SGeert Uytterhoeven struct device_node *nc) 2125d57a4282SGrant Likely { 212689da4293STrent Piepho u32 value; 2127c2e51ac3SGeert Uytterhoeven int rc; 2128d57a4282SGrant Likely 2129d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 2130e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpha")) 2131d57a4282SGrant Likely spi->mode |= SPI_CPHA; 2132e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpol")) 2133d57a4282SGrant Likely spi->mode |= SPI_CPOL; 2134e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-3wire")) 2135c20151dfSLars-Peter Clausen spi->mode |= SPI_3WIRE; 2136e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-lsb-first")) 2137cd6339e6SZhao Qiang spi->mode |= SPI_LSB_FIRST; 21383e5ec1dbSGregory CLEMENT if (of_property_read_bool(nc, "spi-cs-high")) 2139f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 2140f3186dd8SLinus Walleij 2141f477b7fbSwangyuhang /* Device DUAL/QUAD mode */ 214289da4293STrent Piepho if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 214389da4293STrent Piepho switch (value) { 2144d962608cSDragos Bogdan case 0: 2145d962608cSDragos Bogdan spi->mode |= SPI_NO_TX; 2146d962608cSDragos Bogdan break; 214789da4293STrent Piepho case 1: 2148f477b7fbSwangyuhang break; 214989da4293STrent Piepho case 2: 2150f477b7fbSwangyuhang spi->mode |= SPI_TX_DUAL; 2151f477b7fbSwangyuhang break; 215289da4293STrent Piepho case 4: 2153f477b7fbSwangyuhang spi->mode |= SPI_TX_QUAD; 2154f477b7fbSwangyuhang break; 21556b03061fSYogesh Narayan Gaur case 8: 21566b03061fSYogesh Narayan Gaur spi->mode |= SPI_TX_OCTAL; 21576b03061fSYogesh Narayan Gaur break; 2158f477b7fbSwangyuhang default: 21598caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 2160a110f93dSwangyuhang "spi-tx-bus-width %d not supported\n", 216189da4293STrent Piepho value); 216280874d8cSGeert Uytterhoeven break; 2163f477b7fbSwangyuhang } 2164a822e99cSMark Brown } 2165f477b7fbSwangyuhang 216689da4293STrent Piepho if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 216789da4293STrent Piepho switch (value) { 2168d962608cSDragos Bogdan case 0: 2169d962608cSDragos Bogdan spi->mode |= SPI_NO_RX; 2170d962608cSDragos Bogdan break; 217189da4293STrent Piepho case 1: 2172f477b7fbSwangyuhang break; 217389da4293STrent Piepho case 2: 2174f477b7fbSwangyuhang spi->mode |= SPI_RX_DUAL; 2175f477b7fbSwangyuhang break; 217689da4293STrent Piepho case 4: 2177f477b7fbSwangyuhang spi->mode |= SPI_RX_QUAD; 2178f477b7fbSwangyuhang break; 21796b03061fSYogesh Narayan Gaur case 8: 21806b03061fSYogesh Narayan Gaur spi->mode |= SPI_RX_OCTAL; 21816b03061fSYogesh Narayan Gaur break; 2182f477b7fbSwangyuhang default: 21838caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 2184a110f93dSwangyuhang "spi-rx-bus-width %d not supported\n", 218589da4293STrent Piepho value); 218680874d8cSGeert Uytterhoeven break; 2187f477b7fbSwangyuhang } 2188a822e99cSMark Brown } 2189f477b7fbSwangyuhang 21908caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr)) { 2191194276b0SRob Herring if (!of_node_name_eq(nc, "slave")) { 219225c56c88SRob Herring dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 219325c56c88SRob Herring nc); 21946c364062SGeert Uytterhoeven return -EINVAL; 21956c364062SGeert Uytterhoeven } 21966c364062SGeert Uytterhoeven return 0; 21976c364062SGeert Uytterhoeven } 21986c364062SGeert Uytterhoeven 21996c364062SGeert Uytterhoeven /* Device address */ 22006c364062SGeert Uytterhoeven rc = of_property_read_u32(nc, "reg", &value); 22016c364062SGeert Uytterhoeven if (rc) { 220225c56c88SRob Herring dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 220325c56c88SRob Herring nc, rc); 22046c364062SGeert Uytterhoeven return rc; 22056c364062SGeert Uytterhoeven } 22066c364062SGeert Uytterhoeven spi->chip_select = value; 22076c364062SGeert Uytterhoeven 2208d57a4282SGrant Likely /* Device speed */ 2209671c3bf5SChuanhong Guo if (!of_property_read_u32(nc, "spi-max-frequency", &value)) 221089da4293STrent Piepho spi->max_speed_hz = value; 2211d57a4282SGrant Likely 2212c2e51ac3SGeert Uytterhoeven return 0; 2213c2e51ac3SGeert Uytterhoeven } 2214c2e51ac3SGeert Uytterhoeven 2215c2e51ac3SGeert Uytterhoeven static struct spi_device * 22168caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 2217c2e51ac3SGeert Uytterhoeven { 2218c2e51ac3SGeert Uytterhoeven struct spi_device *spi; 2219c2e51ac3SGeert Uytterhoeven int rc; 2220c2e51ac3SGeert Uytterhoeven 2221c2e51ac3SGeert Uytterhoeven /* Alloc an spi_device */ 22228caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 2223c2e51ac3SGeert Uytterhoeven if (!spi) { 222425c56c88SRob Herring dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 2225c2e51ac3SGeert Uytterhoeven rc = -ENOMEM; 2226c2e51ac3SGeert Uytterhoeven goto err_out; 2227c2e51ac3SGeert Uytterhoeven } 2228c2e51ac3SGeert Uytterhoeven 2229c2e51ac3SGeert Uytterhoeven /* Select device driver */ 2230c2e51ac3SGeert Uytterhoeven rc = of_modalias_node(nc, spi->modalias, 2231c2e51ac3SGeert Uytterhoeven sizeof(spi->modalias)); 2232c2e51ac3SGeert Uytterhoeven if (rc < 0) { 223325c56c88SRob Herring dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 2234c2e51ac3SGeert Uytterhoeven goto err_out; 2235c2e51ac3SGeert Uytterhoeven } 2236c2e51ac3SGeert Uytterhoeven 22378caab75fSGeert Uytterhoeven rc = of_spi_parse_dt(ctlr, spi, nc); 2238c2e51ac3SGeert Uytterhoeven if (rc) 2239c2e51ac3SGeert Uytterhoeven goto err_out; 2240c2e51ac3SGeert Uytterhoeven 2241d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 2242d57a4282SGrant Likely of_node_get(nc); 2243d57a4282SGrant Likely spi->dev.of_node = nc; 22440e793ba7SCharles Keepax spi->dev.fwnode = of_fwnode_handle(nc); 2245d57a4282SGrant Likely 2246d57a4282SGrant Likely /* Register the new device */ 2247d57a4282SGrant Likely rc = spi_add_device(spi); 2248d57a4282SGrant Likely if (rc) { 224925c56c88SRob Herring dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 22508324147fSJohan Hovold goto err_of_node_put; 2251d57a4282SGrant Likely } 2252d57a4282SGrant Likely 2253aff5e3f8SPantelis Antoniou return spi; 2254aff5e3f8SPantelis Antoniou 22558324147fSJohan Hovold err_of_node_put: 22568324147fSJohan Hovold of_node_put(nc); 2257aff5e3f8SPantelis Antoniou err_out: 2258aff5e3f8SPantelis Antoniou spi_dev_put(spi); 2259aff5e3f8SPantelis Antoniou return ERR_PTR(rc); 2260aff5e3f8SPantelis Antoniou } 2261aff5e3f8SPantelis Antoniou 2262aff5e3f8SPantelis Antoniou /** 2263aff5e3f8SPantelis Antoniou * of_register_spi_devices() - Register child devices onto the SPI bus 22648caab75fSGeert Uytterhoeven * @ctlr: Pointer to spi_controller device 2265aff5e3f8SPantelis Antoniou * 22666c364062SGeert Uytterhoeven * Registers an spi_device for each child node of controller node which 22676c364062SGeert Uytterhoeven * represents a valid SPI slave. 2268aff5e3f8SPantelis Antoniou */ 22698caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) 2270aff5e3f8SPantelis Antoniou { 2271aff5e3f8SPantelis Antoniou struct spi_device *spi; 2272aff5e3f8SPantelis Antoniou struct device_node *nc; 2273aff5e3f8SPantelis Antoniou 22748caab75fSGeert Uytterhoeven if (!ctlr->dev.of_node) 2275aff5e3f8SPantelis Antoniou return; 2276aff5e3f8SPantelis Antoniou 22778caab75fSGeert Uytterhoeven for_each_available_child_of_node(ctlr->dev.of_node, nc) { 2278bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(nc, OF_POPULATED)) 2279bd6c1644SGeert Uytterhoeven continue; 22808caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, nc); 2281e0af98a7SRalf Ramsauer if (IS_ERR(spi)) { 22828caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 228325c56c88SRob Herring "Failed to create SPI device for %pOF\n", nc); 2284e0af98a7SRalf Ramsauer of_node_clear_flag(nc, OF_POPULATED); 2285e0af98a7SRalf Ramsauer } 2286d57a4282SGrant Likely } 2287d57a4282SGrant Likely } 2288d57a4282SGrant Likely #else 22898caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { } 2290d57a4282SGrant Likely #endif 2291d57a4282SGrant Likely 22920c79378cSSebastian Reichel /** 22930c79378cSSebastian Reichel * spi_new_ancillary_device() - Register ancillary SPI device 22940c79378cSSebastian Reichel * @spi: Pointer to the main SPI device registering the ancillary device 22950c79378cSSebastian Reichel * @chip_select: Chip Select of the ancillary device 22960c79378cSSebastian Reichel * 22970c79378cSSebastian Reichel * Register an ancillary SPI device; for example some chips have a chip-select 22980c79378cSSebastian Reichel * for normal device usage and another one for setup/firmware upload. 22990c79378cSSebastian Reichel * 23000c79378cSSebastian Reichel * This may only be called from main SPI device's probe routine. 23010c79378cSSebastian Reichel * 23020c79378cSSebastian Reichel * Return: 0 on success; negative errno on failure 23030c79378cSSebastian Reichel */ 23040c79378cSSebastian Reichel struct spi_device *spi_new_ancillary_device(struct spi_device *spi, 23050c79378cSSebastian Reichel u8 chip_select) 23060c79378cSSebastian Reichel { 23070c79378cSSebastian Reichel struct spi_device *ancillary; 23080c79378cSSebastian Reichel int rc = 0; 23090c79378cSSebastian Reichel 23100c79378cSSebastian Reichel /* Alloc an spi_device */ 23110c79378cSSebastian Reichel ancillary = spi_alloc_device(spi->controller); 23120c79378cSSebastian Reichel if (!ancillary) { 23130c79378cSSebastian Reichel rc = -ENOMEM; 23140c79378cSSebastian Reichel goto err_out; 23150c79378cSSebastian Reichel } 23160c79378cSSebastian Reichel 23170c79378cSSebastian Reichel strlcpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias)); 23180c79378cSSebastian Reichel 23190c79378cSSebastian Reichel /* Use provided chip-select for ancillary device */ 23200c79378cSSebastian Reichel ancillary->chip_select = chip_select; 23210c79378cSSebastian Reichel 23220c79378cSSebastian Reichel /* Take over SPI mode/speed from SPI main device */ 23230c79378cSSebastian Reichel ancillary->max_speed_hz = spi->max_speed_hz; 2324b01d5506SColin Ian King ancillary->mode = spi->mode; 23250c79378cSSebastian Reichel 23260c79378cSSebastian Reichel /* Register the new device */ 23270c79378cSSebastian Reichel rc = spi_add_device_locked(ancillary); 23280c79378cSSebastian Reichel if (rc) { 23290c79378cSSebastian Reichel dev_err(&spi->dev, "failed to register ancillary device\n"); 23300c79378cSSebastian Reichel goto err_out; 23310c79378cSSebastian Reichel } 23320c79378cSSebastian Reichel 23330c79378cSSebastian Reichel return ancillary; 23340c79378cSSebastian Reichel 23350c79378cSSebastian Reichel err_out: 23360c79378cSSebastian Reichel spi_dev_put(ancillary); 23370c79378cSSebastian Reichel return ERR_PTR(rc); 23380c79378cSSebastian Reichel } 23390c79378cSSebastian Reichel EXPORT_SYMBOL_GPL(spi_new_ancillary_device); 23400c79378cSSebastian Reichel 234164bee4d2SMika Westerberg #ifdef CONFIG_ACPI 23424c3c5954SArd Biesheuvel struct acpi_spi_lookup { 23434c3c5954SArd Biesheuvel struct spi_controller *ctlr; 23444c3c5954SArd Biesheuvel u32 max_speed_hz; 23454c3c5954SArd Biesheuvel u32 mode; 23464c3c5954SArd Biesheuvel int irq; 23474c3c5954SArd Biesheuvel u8 bits_per_word; 23484c3c5954SArd Biesheuvel u8 chip_select; 234987e59b36SStefan Binding int n; 235087e59b36SStefan Binding int index; 23514c3c5954SArd Biesheuvel }; 23524c3c5954SArd Biesheuvel 2353e612af7aSStefan Binding static int acpi_spi_count(struct acpi_resource *ares, void *data) 2354e612af7aSStefan Binding { 2355e612af7aSStefan Binding struct acpi_resource_spi_serialbus *sb; 2356e612af7aSStefan Binding int *count = data; 2357e612af7aSStefan Binding 2358e612af7aSStefan Binding if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) 2359e612af7aSStefan Binding return 1; 2360e612af7aSStefan Binding 2361e612af7aSStefan Binding sb = &ares->data.spi_serial_bus; 2362e612af7aSStefan Binding if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_SPI) 2363e612af7aSStefan Binding return 1; 2364e612af7aSStefan Binding 2365e612af7aSStefan Binding *count = *count + 1; 2366e612af7aSStefan Binding 2367e612af7aSStefan Binding return 1; 2368e612af7aSStefan Binding } 2369e612af7aSStefan Binding 2370e612af7aSStefan Binding /** 2371e612af7aSStefan Binding * acpi_spi_count_resources - Count the number of SpiSerialBus resources 2372e612af7aSStefan Binding * @adev: ACPI device 2373e612af7aSStefan Binding * 2374e612af7aSStefan Binding * Returns the number of SpiSerialBus resources in the ACPI-device's 2375e612af7aSStefan Binding * resource-list; or a negative error code. 2376e612af7aSStefan Binding */ 2377e612af7aSStefan Binding int acpi_spi_count_resources(struct acpi_device *adev) 2378e612af7aSStefan Binding { 2379e612af7aSStefan Binding LIST_HEAD(r); 2380e612af7aSStefan Binding int count = 0; 2381e612af7aSStefan Binding int ret; 2382e612af7aSStefan Binding 2383e612af7aSStefan Binding ret = acpi_dev_get_resources(adev, &r, acpi_spi_count, &count); 2384e612af7aSStefan Binding if (ret < 0) 2385e612af7aSStefan Binding return ret; 2386e612af7aSStefan Binding 2387e612af7aSStefan Binding acpi_dev_free_resource_list(&r); 2388e612af7aSStefan Binding 2389e612af7aSStefan Binding return count; 2390e612af7aSStefan Binding } 2391e612af7aSStefan Binding EXPORT_SYMBOL_GPL(acpi_spi_count_resources); 2392e612af7aSStefan Binding 23934c3c5954SArd Biesheuvel static void acpi_spi_parse_apple_properties(struct acpi_device *dev, 23944c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup) 23958a2e487eSLukas Wunner { 23968a2e487eSLukas Wunner const union acpi_object *obj; 23978a2e487eSLukas Wunner 23988a2e487eSLukas Wunner if (!x86_apple_machine) 23998a2e487eSLukas Wunner return; 24008a2e487eSLukas Wunner 24018a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 24028a2e487eSLukas Wunner && obj->buffer.length >= 4) 24034c3c5954SArd Biesheuvel lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 24048a2e487eSLukas Wunner 24058a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 24068a2e487eSLukas Wunner && obj->buffer.length == 8) 24074c3c5954SArd Biesheuvel lookup->bits_per_word = *(u64 *)obj->buffer.pointer; 24088a2e487eSLukas Wunner 24098a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 24108a2e487eSLukas Wunner && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 24114c3c5954SArd Biesheuvel lookup->mode |= SPI_LSB_FIRST; 24128a2e487eSLukas Wunner 24138a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 24148a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 24154c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 24168a2e487eSLukas Wunner 24178a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 24188a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 24194c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 24208a2e487eSLukas Wunner } 24218a2e487eSLukas Wunner 242287e59b36SStefan Binding static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev); 242387e59b36SStefan Binding 242464bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 242564bee4d2SMika Westerberg { 24264c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup = data; 24274c3c5954SArd Biesheuvel struct spi_controller *ctlr = lookup->ctlr; 242864bee4d2SMika Westerberg 242964bee4d2SMika Westerberg if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 243064bee4d2SMika Westerberg struct acpi_resource_spi_serialbus *sb; 24314c3c5954SArd Biesheuvel acpi_handle parent_handle; 24324c3c5954SArd Biesheuvel acpi_status status; 243364bee4d2SMika Westerberg 243464bee4d2SMika Westerberg sb = &ares->data.spi_serial_bus; 243564bee4d2SMika Westerberg if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 24364c3c5954SArd Biesheuvel 243787e59b36SStefan Binding if (lookup->index != -1 && lookup->n++ != lookup->index) 243887e59b36SStefan Binding return 1; 243987e59b36SStefan Binding 244087e59b36SStefan Binding if (lookup->index == -1 && !ctlr) 244187e59b36SStefan Binding return -ENODEV; 244287e59b36SStefan Binding 24434c3c5954SArd Biesheuvel status = acpi_get_handle(NULL, 24444c3c5954SArd Biesheuvel sb->resource_source.string_ptr, 24454c3c5954SArd Biesheuvel &parent_handle); 24464c3c5954SArd Biesheuvel 244787e59b36SStefan Binding if (ACPI_FAILURE(status)) 24484c3c5954SArd Biesheuvel return -ENODEV; 24494c3c5954SArd Biesheuvel 245087e59b36SStefan Binding if (ctlr) { 245187e59b36SStefan Binding if (ACPI_HANDLE(ctlr->dev.parent) != parent_handle) 245287e59b36SStefan Binding return -ENODEV; 245387e59b36SStefan Binding } else { 245487e59b36SStefan Binding struct acpi_device *adev; 245587e59b36SStefan Binding 2456ac2a3feeSRafael J. Wysocki adev = acpi_fetch_acpi_dev(parent_handle); 2457ac2a3feeSRafael J. Wysocki if (!adev) 245887e59b36SStefan Binding return -ENODEV; 245987e59b36SStefan Binding 246087e59b36SStefan Binding ctlr = acpi_spi_find_controller_by_adev(adev); 246187e59b36SStefan Binding if (!ctlr) 246287e59b36SStefan Binding return -ENODEV; 246387e59b36SStefan Binding 246487e59b36SStefan Binding lookup->ctlr = ctlr; 246587e59b36SStefan Binding } 246687e59b36SStefan Binding 2467a0a90718SMika Westerberg /* 2468a0a90718SMika Westerberg * ACPI DeviceSelection numbering is handled by the 2469a0a90718SMika Westerberg * host controller driver in Windows and can vary 2470a0a90718SMika Westerberg * from driver to driver. In Linux we always expect 2471a0a90718SMika Westerberg * 0 .. max - 1 so we need to ask the driver to 2472a0a90718SMika Westerberg * translate between the two schemes. 2473a0a90718SMika Westerberg */ 24748caab75fSGeert Uytterhoeven if (ctlr->fw_translate_cs) { 24758caab75fSGeert Uytterhoeven int cs = ctlr->fw_translate_cs(ctlr, 2476a0a90718SMika Westerberg sb->device_selection); 2477a0a90718SMika Westerberg if (cs < 0) 2478a0a90718SMika Westerberg return cs; 24794c3c5954SArd Biesheuvel lookup->chip_select = cs; 2480a0a90718SMika Westerberg } else { 24814c3c5954SArd Biesheuvel lookup->chip_select = sb->device_selection; 2482a0a90718SMika Westerberg } 2483a0a90718SMika Westerberg 24844c3c5954SArd Biesheuvel lookup->max_speed_hz = sb->connection_speed; 24850dadde34SAndy Shevchenko lookup->bits_per_word = sb->data_bit_length; 248664bee4d2SMika Westerberg 248764bee4d2SMika Westerberg if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 24884c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 248964bee4d2SMika Westerberg if (sb->clock_polarity == ACPI_SPI_START_HIGH) 24904c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 249164bee4d2SMika Westerberg if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 24924c3c5954SArd Biesheuvel lookup->mode |= SPI_CS_HIGH; 249364bee4d2SMika Westerberg } 24944c3c5954SArd Biesheuvel } else if (lookup->irq < 0) { 249564bee4d2SMika Westerberg struct resource r; 249664bee4d2SMika Westerberg 249764bee4d2SMika Westerberg if (acpi_dev_resource_interrupt(ares, 0, &r)) 24984c3c5954SArd Biesheuvel lookup->irq = r.start; 249964bee4d2SMika Westerberg } 250064bee4d2SMika Westerberg 250164bee4d2SMika Westerberg /* Always tell the ACPI core to skip this resource */ 250264bee4d2SMika Westerberg return 1; 250364bee4d2SMika Westerberg } 250464bee4d2SMika Westerberg 2505000bee0eSStefan Binding /** 2506000bee0eSStefan Binding * acpi_spi_device_alloc - Allocate a spi device, and fill it in with ACPI information 2507000bee0eSStefan Binding * @ctlr: controller to which the spi device belongs 2508000bee0eSStefan Binding * @adev: ACPI Device for the spi device 250987e59b36SStefan Binding * @index: Index of the spi resource inside the ACPI Node 2510000bee0eSStefan Binding * 2511000bee0eSStefan Binding * This should be used to allocate a new spi device from and ACPI Node. 2512000bee0eSStefan Binding * The caller is responsible for calling spi_add_device to register the spi device. 2513000bee0eSStefan Binding * 251487e59b36SStefan Binding * If ctlr is set to NULL, the Controller for the spi device will be looked up 251587e59b36SStefan Binding * using the resource. 251687e59b36SStefan Binding * If index is set to -1, index is not used. 251787e59b36SStefan Binding * Note: If index is -1, ctlr must be set. 251887e59b36SStefan Binding * 2519000bee0eSStefan Binding * Return: a pointer to the new device, or ERR_PTR on error. 2520000bee0eSStefan Binding */ 2521000bee0eSStefan Binding struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr, 252287e59b36SStefan Binding struct acpi_device *adev, 252387e59b36SStefan Binding int index) 252464bee4d2SMika Westerberg { 25254c3c5954SArd Biesheuvel acpi_handle parent_handle = NULL; 252664bee4d2SMika Westerberg struct list_head resource_list; 2527b28944c6SArd Biesheuvel struct acpi_spi_lookup lookup = {}; 252864bee4d2SMika Westerberg struct spi_device *spi; 252964bee4d2SMika Westerberg int ret; 253064bee4d2SMika Westerberg 253187e59b36SStefan Binding if (!ctlr && index == -1) 253287e59b36SStefan Binding return ERR_PTR(-EINVAL); 253387e59b36SStefan Binding 25344c3c5954SArd Biesheuvel lookup.ctlr = ctlr; 25354c3c5954SArd Biesheuvel lookup.irq = -1; 253687e59b36SStefan Binding lookup.index = index; 253787e59b36SStefan Binding lookup.n = 0; 25384c3c5954SArd Biesheuvel 25394c3c5954SArd Biesheuvel INIT_LIST_HEAD(&resource_list); 25404c3c5954SArd Biesheuvel ret = acpi_dev_get_resources(adev, &resource_list, 25414c3c5954SArd Biesheuvel acpi_spi_add_resource, &lookup); 25424c3c5954SArd Biesheuvel acpi_dev_free_resource_list(&resource_list); 25434c3c5954SArd Biesheuvel 25444c3c5954SArd Biesheuvel if (ret < 0) 25454c3c5954SArd Biesheuvel /* found SPI in _CRS but it points to another controller */ 2546000bee0eSStefan Binding return ERR_PTR(-ENODEV); 25474c3c5954SArd Biesheuvel 25484c3c5954SArd Biesheuvel if (!lookup.max_speed_hz && 254910e92724SBjorn Helgaas ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) && 255087e59b36SStefan Binding ACPI_HANDLE(lookup.ctlr->dev.parent) == parent_handle) { 25514c3c5954SArd Biesheuvel /* Apple does not use _CRS but nested devices for SPI slaves */ 25524c3c5954SArd Biesheuvel acpi_spi_parse_apple_properties(adev, &lookup); 25534c3c5954SArd Biesheuvel } 25544c3c5954SArd Biesheuvel 25554c3c5954SArd Biesheuvel if (!lookup.max_speed_hz) 2556000bee0eSStefan Binding return ERR_PTR(-ENODEV); 25574c3c5954SArd Biesheuvel 255887e59b36SStefan Binding spi = spi_alloc_device(lookup.ctlr); 255964bee4d2SMika Westerberg if (!spi) { 256087e59b36SStefan Binding dev_err(&lookup.ctlr->dev, "failed to allocate SPI device for %s\n", 256164bee4d2SMika Westerberg dev_name(&adev->dev)); 2562000bee0eSStefan Binding return ERR_PTR(-ENOMEM); 256364bee4d2SMika Westerberg } 256464bee4d2SMika Westerberg 25657b199811SRafael J. Wysocki ACPI_COMPANION_SET(&spi->dev, adev); 25664c3c5954SArd Biesheuvel spi->max_speed_hz = lookup.max_speed_hz; 2567ea235786SJohn Garry spi->mode |= lookup.mode; 25684c3c5954SArd Biesheuvel spi->irq = lookup.irq; 25694c3c5954SArd Biesheuvel spi->bits_per_word = lookup.bits_per_word; 25704c3c5954SArd Biesheuvel spi->chip_select = lookup.chip_select; 257164bee4d2SMika Westerberg 2572000bee0eSStefan Binding return spi; 2573000bee0eSStefan Binding } 2574000bee0eSStefan Binding EXPORT_SYMBOL_GPL(acpi_spi_device_alloc); 2575000bee0eSStefan Binding 2576000bee0eSStefan Binding static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 2577000bee0eSStefan Binding struct acpi_device *adev) 2578000bee0eSStefan Binding { 2579000bee0eSStefan Binding struct spi_device *spi; 2580000bee0eSStefan Binding 2581000bee0eSStefan Binding if (acpi_bus_get_status(adev) || !adev->status.present || 2582000bee0eSStefan Binding acpi_device_enumerated(adev)) 2583000bee0eSStefan Binding return AE_OK; 2584000bee0eSStefan Binding 258587e59b36SStefan Binding spi = acpi_spi_device_alloc(ctlr, adev, -1); 2586000bee0eSStefan Binding if (IS_ERR(spi)) { 2587000bee0eSStefan Binding if (PTR_ERR(spi) == -ENOMEM) 2588000bee0eSStefan Binding return AE_NO_MEMORY; 2589000bee0eSStefan Binding else 2590000bee0eSStefan Binding return AE_OK; 2591000bee0eSStefan Binding } 2592000bee0eSStefan Binding 25930c6543f6SDan O'Donovan acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 25940c6543f6SDan O'Donovan sizeof(spi->modalias)); 25950c6543f6SDan O'Donovan 259633ada67dSChristophe RICARD if (spi->irq < 0) 259733ada67dSChristophe RICARD spi->irq = acpi_dev_gpio_irq_get(adev, 0); 259833ada67dSChristophe RICARD 25997f24467fSOctavian Purdila acpi_device_set_enumerated(adev); 26007f24467fSOctavian Purdila 260133cf00e5SMika Westerberg adev->power.flags.ignore_parent = true; 260264bee4d2SMika Westerberg if (spi_add_device(spi)) { 260333cf00e5SMika Westerberg adev->power.flags.ignore_parent = false; 26048caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 260564bee4d2SMika Westerberg dev_name(&adev->dev)); 260664bee4d2SMika Westerberg spi_dev_put(spi); 260764bee4d2SMika Westerberg } 260864bee4d2SMika Westerberg 260964bee4d2SMika Westerberg return AE_OK; 261064bee4d2SMika Westerberg } 261164bee4d2SMika Westerberg 26127f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 26137f24467fSOctavian Purdila void *data, void **return_value) 26147f24467fSOctavian Purdila { 26157030c428SRafael J. Wysocki struct acpi_device *adev = acpi_fetch_acpi_dev(handle); 26168caab75fSGeert Uytterhoeven struct spi_controller *ctlr = data; 26177f24467fSOctavian Purdila 26187030c428SRafael J. Wysocki if (!adev) 26197f24467fSOctavian Purdila return AE_OK; 26207f24467fSOctavian Purdila 26218caab75fSGeert Uytterhoeven return acpi_register_spi_device(ctlr, adev); 26227f24467fSOctavian Purdila } 26237f24467fSOctavian Purdila 26244c3c5954SArd Biesheuvel #define SPI_ACPI_ENUMERATE_MAX_DEPTH 32 26254c3c5954SArd Biesheuvel 26268caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr) 262764bee4d2SMika Westerberg { 262864bee4d2SMika Westerberg acpi_status status; 262964bee4d2SMika Westerberg acpi_handle handle; 263064bee4d2SMika Westerberg 26318caab75fSGeert Uytterhoeven handle = ACPI_HANDLE(ctlr->dev.parent); 263264bee4d2SMika Westerberg if (!handle) 263364bee4d2SMika Westerberg return; 263464bee4d2SMika Westerberg 26354c3c5954SArd Biesheuvel status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 26364c3c5954SArd Biesheuvel SPI_ACPI_ENUMERATE_MAX_DEPTH, 26378caab75fSGeert Uytterhoeven acpi_spi_add_device, NULL, ctlr, NULL); 263864bee4d2SMika Westerberg if (ACPI_FAILURE(status)) 26398caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 264064bee4d2SMika Westerberg } 264164bee4d2SMika Westerberg #else 26428caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 264364bee4d2SMika Westerberg #endif /* CONFIG_ACPI */ 264464bee4d2SMika Westerberg 26458caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev) 26468ae12a0dSDavid Brownell { 26478caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 26488ae12a0dSDavid Brownell 26498caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 26508caab75fSGeert Uytterhoeven kfree(ctlr); 26518ae12a0dSDavid Brownell } 26528ae12a0dSDavid Brownell 26538ae12a0dSDavid Brownell static struct class spi_master_class = { 26548ae12a0dSDavid Brownell .name = "spi_master", 26558ae12a0dSDavid Brownell .owner = THIS_MODULE, 26568caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 2657eca2ebc7SMartin Sperl .dev_groups = spi_master_groups, 26588ae12a0dSDavid Brownell }; 26598ae12a0dSDavid Brownell 26606c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE 26616c364062SGeert Uytterhoeven /** 26626c364062SGeert Uytterhoeven * spi_slave_abort - abort the ongoing transfer request on an SPI slave 26636c364062SGeert Uytterhoeven * controller 26646c364062SGeert Uytterhoeven * @spi: device used for the current transfer 26656c364062SGeert Uytterhoeven */ 26666c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi) 26676c364062SGeert Uytterhoeven { 26688caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 26696c364062SGeert Uytterhoeven 26708caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 26718caab75fSGeert Uytterhoeven return ctlr->slave_abort(ctlr); 26726c364062SGeert Uytterhoeven 26736c364062SGeert Uytterhoeven return -ENOTSUPP; 26746c364062SGeert Uytterhoeven } 26756c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort); 26766c364062SGeert Uytterhoeven 26776c364062SGeert Uytterhoeven static int match_true(struct device *dev, void *data) 26786c364062SGeert Uytterhoeven { 26796c364062SGeert Uytterhoeven return 1; 26806c364062SGeert Uytterhoeven } 26816c364062SGeert Uytterhoeven 2682cc8b4659SGeert Uytterhoeven static ssize_t slave_show(struct device *dev, struct device_attribute *attr, 2683cc8b4659SGeert Uytterhoeven char *buf) 26846c364062SGeert Uytterhoeven { 26858caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 26868caab75fSGeert Uytterhoeven dev); 26876c364062SGeert Uytterhoeven struct device *child; 26886c364062SGeert Uytterhoeven 26896c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 26906c364062SGeert Uytterhoeven return sprintf(buf, "%s\n", 26916c364062SGeert Uytterhoeven child ? to_spi_device(child)->modalias : NULL); 26926c364062SGeert Uytterhoeven } 26936c364062SGeert Uytterhoeven 2694cc8b4659SGeert Uytterhoeven static ssize_t slave_store(struct device *dev, struct device_attribute *attr, 2695cc8b4659SGeert Uytterhoeven const char *buf, size_t count) 26966c364062SGeert Uytterhoeven { 26978caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 26988caab75fSGeert Uytterhoeven dev); 26996c364062SGeert Uytterhoeven struct spi_device *spi; 27006c364062SGeert Uytterhoeven struct device *child; 27016c364062SGeert Uytterhoeven char name[32]; 27026c364062SGeert Uytterhoeven int rc; 27036c364062SGeert Uytterhoeven 27046c364062SGeert Uytterhoeven rc = sscanf(buf, "%31s", name); 27056c364062SGeert Uytterhoeven if (rc != 1 || !name[0]) 27066c364062SGeert Uytterhoeven return -EINVAL; 27076c364062SGeert Uytterhoeven 27086c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 27096c364062SGeert Uytterhoeven if (child) { 27106c364062SGeert Uytterhoeven /* Remove registered slave */ 27116c364062SGeert Uytterhoeven device_unregister(child); 27126c364062SGeert Uytterhoeven put_device(child); 27136c364062SGeert Uytterhoeven } 27146c364062SGeert Uytterhoeven 27156c364062SGeert Uytterhoeven if (strcmp(name, "(null)")) { 27166c364062SGeert Uytterhoeven /* Register new slave */ 27176c364062SGeert Uytterhoeven spi = spi_alloc_device(ctlr); 27186c364062SGeert Uytterhoeven if (!spi) 27196c364062SGeert Uytterhoeven return -ENOMEM; 27206c364062SGeert Uytterhoeven 27216c364062SGeert Uytterhoeven strlcpy(spi->modalias, name, sizeof(spi->modalias)); 27226c364062SGeert Uytterhoeven 27236c364062SGeert Uytterhoeven rc = spi_add_device(spi); 27246c364062SGeert Uytterhoeven if (rc) { 27256c364062SGeert Uytterhoeven spi_dev_put(spi); 27266c364062SGeert Uytterhoeven return rc; 27276c364062SGeert Uytterhoeven } 27286c364062SGeert Uytterhoeven } 27296c364062SGeert Uytterhoeven 27306c364062SGeert Uytterhoeven return count; 27316c364062SGeert Uytterhoeven } 27326c364062SGeert Uytterhoeven 2733cc8b4659SGeert Uytterhoeven static DEVICE_ATTR_RW(slave); 27346c364062SGeert Uytterhoeven 27356c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = { 27366c364062SGeert Uytterhoeven &dev_attr_slave.attr, 27376c364062SGeert Uytterhoeven NULL, 27386c364062SGeert Uytterhoeven }; 27396c364062SGeert Uytterhoeven 27406c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = { 27416c364062SGeert Uytterhoeven .attrs = spi_slave_attrs, 27426c364062SGeert Uytterhoeven }; 27436c364062SGeert Uytterhoeven 27446c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = { 27458caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 27466c364062SGeert Uytterhoeven &spi_slave_group, 27476c364062SGeert Uytterhoeven NULL, 27486c364062SGeert Uytterhoeven }; 27496c364062SGeert Uytterhoeven 27506c364062SGeert Uytterhoeven static struct class spi_slave_class = { 27516c364062SGeert Uytterhoeven .name = "spi_slave", 27526c364062SGeert Uytterhoeven .owner = THIS_MODULE, 27538caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 27546c364062SGeert Uytterhoeven .dev_groups = spi_slave_groups, 27556c364062SGeert Uytterhoeven }; 27566c364062SGeert Uytterhoeven #else 27576c364062SGeert Uytterhoeven extern struct class spi_slave_class; /* dummy */ 27586c364062SGeert Uytterhoeven #endif 27598ae12a0dSDavid Brownell 27608ae12a0dSDavid Brownell /** 27616c364062SGeert Uytterhoeven * __spi_alloc_controller - allocate an SPI master or slave controller 27628ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 276333e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 2764229e6af1SLukas Wunner * memory is in the driver_data field of the returned device, accessible 2765229e6af1SLukas Wunner * with spi_controller_get_devdata(); the memory is cacheline aligned; 2766229e6af1SLukas Wunner * drivers granting DMA access to portions of their private data need to 2767229e6af1SLukas Wunner * round up @size using ALIGN(size, dma_get_cache_alignment()). 27686c364062SGeert Uytterhoeven * @slave: flag indicating whether to allocate an SPI master (false) or SPI 27696c364062SGeert Uytterhoeven * slave (true) controller 277033e34dc6SDavid Brownell * Context: can sleep 27718ae12a0dSDavid Brownell * 27726c364062SGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 27738ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 27748caab75fSGeert Uytterhoeven * an spi_controller structure, prior to calling spi_register_controller(). 27758ae12a0dSDavid Brownell * 277697d56dc6SJavier Martinez Canillas * This must be called from context that can sleep. 27778ae12a0dSDavid Brownell * 27786c364062SGeert Uytterhoeven * The caller is responsible for assigning the bus number and initializing the 27798caab75fSGeert Uytterhoeven * controller's methods before calling spi_register_controller(); and (after 27808caab75fSGeert Uytterhoeven * errors adding the device) calling spi_controller_put() to prevent a memory 27818caab75fSGeert Uytterhoeven * leak. 278297d56dc6SJavier Martinez Canillas * 27836c364062SGeert Uytterhoeven * Return: the SPI controller structure on success, else NULL. 27848ae12a0dSDavid Brownell */ 27858caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev, 27866c364062SGeert Uytterhoeven unsigned int size, bool slave) 27878ae12a0dSDavid Brownell { 27888caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 2789229e6af1SLukas Wunner size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment()); 27908ae12a0dSDavid Brownell 27910c868461SDavid Brownell if (!dev) 27920c868461SDavid Brownell return NULL; 27930c868461SDavid Brownell 2794229e6af1SLukas Wunner ctlr = kzalloc(size + ctlr_size, GFP_KERNEL); 27958caab75fSGeert Uytterhoeven if (!ctlr) 27968ae12a0dSDavid Brownell return NULL; 27978ae12a0dSDavid Brownell 27988caab75fSGeert Uytterhoeven device_initialize(&ctlr->dev); 279916a8e2fbSUwe Kleine-König INIT_LIST_HEAD(&ctlr->queue); 280016a8e2fbSUwe Kleine-König spin_lock_init(&ctlr->queue_lock); 280116a8e2fbSUwe Kleine-König spin_lock_init(&ctlr->bus_lock_spinlock); 280216a8e2fbSUwe Kleine-König mutex_init(&ctlr->bus_lock_mutex); 280316a8e2fbSUwe Kleine-König mutex_init(&ctlr->io_mutex); 280416a8e2fbSUwe Kleine-König mutex_init(&ctlr->add_lock); 28058caab75fSGeert Uytterhoeven ctlr->bus_num = -1; 28068caab75fSGeert Uytterhoeven ctlr->num_chipselect = 1; 28078caab75fSGeert Uytterhoeven ctlr->slave = slave; 28086c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 28098caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_slave_class; 28106c364062SGeert Uytterhoeven else 28118caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_master_class; 28128caab75fSGeert Uytterhoeven ctlr->dev.parent = dev; 28138caab75fSGeert Uytterhoeven pm_suspend_ignore_children(&ctlr->dev, true); 2814229e6af1SLukas Wunner spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size); 28158ae12a0dSDavid Brownell 28168caab75fSGeert Uytterhoeven return ctlr; 28178ae12a0dSDavid Brownell } 28186c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller); 28198ae12a0dSDavid Brownell 28205e844cc3SLukas Wunner static void devm_spi_release_controller(struct device *dev, void *ctlr) 28215e844cc3SLukas Wunner { 28225e844cc3SLukas Wunner spi_controller_put(*(struct spi_controller **)ctlr); 28235e844cc3SLukas Wunner } 28245e844cc3SLukas Wunner 28255e844cc3SLukas Wunner /** 28265e844cc3SLukas Wunner * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller() 28275e844cc3SLukas Wunner * @dev: physical device of SPI controller 28285e844cc3SLukas Wunner * @size: how much zeroed driver-private data to allocate 28295e844cc3SLukas Wunner * @slave: whether to allocate an SPI master (false) or SPI slave (true) 28305e844cc3SLukas Wunner * Context: can sleep 28315e844cc3SLukas Wunner * 28325e844cc3SLukas Wunner * Allocate an SPI controller and automatically release a reference on it 28335e844cc3SLukas Wunner * when @dev is unbound from its driver. Drivers are thus relieved from 28345e844cc3SLukas Wunner * having to call spi_controller_put(). 28355e844cc3SLukas Wunner * 28365e844cc3SLukas Wunner * The arguments to this function are identical to __spi_alloc_controller(). 28375e844cc3SLukas Wunner * 28385e844cc3SLukas Wunner * Return: the SPI controller structure on success, else NULL. 28395e844cc3SLukas Wunner */ 28405e844cc3SLukas Wunner struct spi_controller *__devm_spi_alloc_controller(struct device *dev, 28415e844cc3SLukas Wunner unsigned int size, 28425e844cc3SLukas Wunner bool slave) 28435e844cc3SLukas Wunner { 28445e844cc3SLukas Wunner struct spi_controller **ptr, *ctlr; 28455e844cc3SLukas Wunner 28465e844cc3SLukas Wunner ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr), 28475e844cc3SLukas Wunner GFP_KERNEL); 28485e844cc3SLukas Wunner if (!ptr) 28495e844cc3SLukas Wunner return NULL; 28505e844cc3SLukas Wunner 28515e844cc3SLukas Wunner ctlr = __spi_alloc_controller(dev, size, slave); 28525e844cc3SLukas Wunner if (ctlr) { 2853794aaf01SWilliam A. Kennington III ctlr->devm_allocated = true; 28545e844cc3SLukas Wunner *ptr = ctlr; 28555e844cc3SLukas Wunner devres_add(dev, ptr); 28565e844cc3SLukas Wunner } else { 28575e844cc3SLukas Wunner devres_free(ptr); 28585e844cc3SLukas Wunner } 28595e844cc3SLukas Wunner 28605e844cc3SLukas Wunner return ctlr; 28615e844cc3SLukas Wunner } 28625e844cc3SLukas Wunner EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller); 28635e844cc3SLukas Wunner 2864f3186dd8SLinus Walleij /** 2865f3186dd8SLinus Walleij * spi_get_gpio_descs() - grab chip select GPIOs for the master 2866f3186dd8SLinus Walleij * @ctlr: The SPI master to grab GPIO descriptors for 2867f3186dd8SLinus Walleij */ 2868f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr) 2869f3186dd8SLinus Walleij { 2870f3186dd8SLinus Walleij int nb, i; 2871f3186dd8SLinus Walleij struct gpio_desc **cs; 2872f3186dd8SLinus Walleij struct device *dev = &ctlr->dev; 28737d93aecdSGeert Uytterhoeven unsigned long native_cs_mask = 0; 28747d93aecdSGeert Uytterhoeven unsigned int num_cs_gpios = 0; 2875f3186dd8SLinus Walleij 2876f3186dd8SLinus Walleij nb = gpiod_count(dev, "cs"); 287731ed8ebcSAndy Shevchenko if (nb < 0) { 2878f3186dd8SLinus Walleij /* No GPIOs at all is fine, else return the error */ 287931ed8ebcSAndy Shevchenko if (nb == -ENOENT) 2880f3186dd8SLinus Walleij return 0; 2881f3186dd8SLinus Walleij return nb; 288231ed8ebcSAndy Shevchenko } 288331ed8ebcSAndy Shevchenko 288431ed8ebcSAndy Shevchenko ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2885f3186dd8SLinus Walleij 2886f3186dd8SLinus Walleij cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), 2887f3186dd8SLinus Walleij GFP_KERNEL); 2888f3186dd8SLinus Walleij if (!cs) 2889f3186dd8SLinus Walleij return -ENOMEM; 2890f3186dd8SLinus Walleij ctlr->cs_gpiods = cs; 2891f3186dd8SLinus Walleij 2892f3186dd8SLinus Walleij for (i = 0; i < nb; i++) { 2893f3186dd8SLinus Walleij /* 2894f3186dd8SLinus Walleij * Most chipselects are active low, the inverted 2895f3186dd8SLinus Walleij * semantics are handled by special quirks in gpiolib, 2896f3186dd8SLinus Walleij * so initializing them GPIOD_OUT_LOW here means 2897f3186dd8SLinus Walleij * "unasserted", in most cases this will drive the physical 2898f3186dd8SLinus Walleij * line high. 2899f3186dd8SLinus Walleij */ 2900f3186dd8SLinus Walleij cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, 2901f3186dd8SLinus Walleij GPIOD_OUT_LOW); 29021723fdecSGeert Uytterhoeven if (IS_ERR(cs[i])) 29031723fdecSGeert Uytterhoeven return PTR_ERR(cs[i]); 2904f3186dd8SLinus Walleij 2905f3186dd8SLinus Walleij if (cs[i]) { 2906f3186dd8SLinus Walleij /* 2907f3186dd8SLinus Walleij * If we find a CS GPIO, name it after the device and 2908f3186dd8SLinus Walleij * chip select line. 2909f3186dd8SLinus Walleij */ 2910f3186dd8SLinus Walleij char *gpioname; 2911f3186dd8SLinus Walleij 2912f3186dd8SLinus Walleij gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", 2913f3186dd8SLinus Walleij dev_name(dev), i); 2914f3186dd8SLinus Walleij if (!gpioname) 2915f3186dd8SLinus Walleij return -ENOMEM; 2916f3186dd8SLinus Walleij gpiod_set_consumer_name(cs[i], gpioname); 29177d93aecdSGeert Uytterhoeven num_cs_gpios++; 29187d93aecdSGeert Uytterhoeven continue; 2919f3186dd8SLinus Walleij } 29207d93aecdSGeert Uytterhoeven 29217d93aecdSGeert Uytterhoeven if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { 29227d93aecdSGeert Uytterhoeven dev_err(dev, "Invalid native chip select %d\n", i); 29237d93aecdSGeert Uytterhoeven return -EINVAL; 29247d93aecdSGeert Uytterhoeven } 29257d93aecdSGeert Uytterhoeven native_cs_mask |= BIT(i); 29267d93aecdSGeert Uytterhoeven } 29277d93aecdSGeert Uytterhoeven 2928f60d7270SAndy Shevchenko ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; 2929dbaca8e5SAndy Shevchenko 2930dbaca8e5SAndy Shevchenko if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && 2931dbaca8e5SAndy Shevchenko ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { 29327d93aecdSGeert Uytterhoeven dev_err(dev, "No unused native chip select available\n"); 29337d93aecdSGeert Uytterhoeven return -EINVAL; 2934f3186dd8SLinus Walleij } 2935f3186dd8SLinus Walleij 2936f3186dd8SLinus Walleij return 0; 2937f3186dd8SLinus Walleij } 2938f3186dd8SLinus Walleij 2939bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr) 2940bdf3a3b5SBoris Brezillon { 2941bdf3a3b5SBoris Brezillon /* 2942b5932f5cSBoris Brezillon * The controller may implement only the high-level SPI-memory like 2943b5932f5cSBoris Brezillon * operations if it does not support regular SPI transfers, and this is 2944b5932f5cSBoris Brezillon * valid use case. 2945b5932f5cSBoris Brezillon * If ->mem_ops is NULL, we request that at least one of the 2946b5932f5cSBoris Brezillon * ->transfer_xxx() method be implemented. 2947bdf3a3b5SBoris Brezillon */ 2948b5932f5cSBoris Brezillon if (ctlr->mem_ops) { 2949b5932f5cSBoris Brezillon if (!ctlr->mem_ops->exec_op) 2950bdf3a3b5SBoris Brezillon return -EINVAL; 2951b5932f5cSBoris Brezillon } else if (!ctlr->transfer && !ctlr->transfer_one && 2952b5932f5cSBoris Brezillon !ctlr->transfer_one_message) { 2953b5932f5cSBoris Brezillon return -EINVAL; 2954b5932f5cSBoris Brezillon } 2955bdf3a3b5SBoris Brezillon 2956bdf3a3b5SBoris Brezillon return 0; 2957bdf3a3b5SBoris Brezillon } 2958bdf3a3b5SBoris Brezillon 29598ae12a0dSDavid Brownell /** 29608caab75fSGeert Uytterhoeven * spi_register_controller - register SPI master or slave controller 29618caab75fSGeert Uytterhoeven * @ctlr: initialized master, originally from spi_alloc_master() or 29628caab75fSGeert Uytterhoeven * spi_alloc_slave() 296333e34dc6SDavid Brownell * Context: can sleep 29648ae12a0dSDavid Brownell * 29658caab75fSGeert Uytterhoeven * SPI controllers connect to their drivers using some non-SPI bus, 29668ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 29678caab75fSGeert Uytterhoeven * includes calling spi_register_controller() to hook up to this SPI bus glue. 29688ae12a0dSDavid Brownell * 29698ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 29708ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 29718ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 29728ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 29738ae12a0dSDavid Brownell * chip is at which address. 29748ae12a0dSDavid Brownell * 29758ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 29768caab75fSGeert Uytterhoeven * success, else a negative error code (dropping the controller's refcount). 29770c868461SDavid Brownell * After a successful return, the caller is responsible for calling 29788caab75fSGeert Uytterhoeven * spi_unregister_controller(). 297997d56dc6SJavier Martinez Canillas * 298097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 29818ae12a0dSDavid Brownell */ 29828caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr) 29838ae12a0dSDavid Brownell { 29848caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 29852b9603a0SFeng Tang struct boardinfo *bi; 2986b93318a2SSergei Shtylyov int status; 298742bdd706SLucas Stach int id, first_dynamic; 29888ae12a0dSDavid Brownell 29890c868461SDavid Brownell if (!dev) 29900c868461SDavid Brownell return -ENODEV; 29910c868461SDavid Brownell 2992bdf3a3b5SBoris Brezillon /* 2993bdf3a3b5SBoris Brezillon * Make sure all necessary hooks are implemented before registering 2994bdf3a3b5SBoris Brezillon * the SPI controller. 2995bdf3a3b5SBoris Brezillon */ 2996bdf3a3b5SBoris Brezillon status = spi_controller_check_ops(ctlr); 2997bdf3a3b5SBoris Brezillon if (status) 2998bdf3a3b5SBoris Brezillon return status; 2999bdf3a3b5SBoris Brezillon 300004b2d03aSGeert Uytterhoeven if (ctlr->bus_num >= 0) { 300104b2d03aSGeert Uytterhoeven /* devices with a fixed bus num must check-in with the num */ 300204b2d03aSGeert Uytterhoeven mutex_lock(&board_lock); 300304b2d03aSGeert Uytterhoeven id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 300404b2d03aSGeert Uytterhoeven ctlr->bus_num + 1, GFP_KERNEL); 300504b2d03aSGeert Uytterhoeven mutex_unlock(&board_lock); 300604b2d03aSGeert Uytterhoeven if (WARN(id < 0, "couldn't get idr")) 300704b2d03aSGeert Uytterhoeven return id == -ENOSPC ? -EBUSY : id; 300804b2d03aSGeert Uytterhoeven ctlr->bus_num = id; 300904b2d03aSGeert Uytterhoeven } else if (ctlr->dev.of_node) { 30109b61e302SSuniel Mahesh /* allocate dynamic bus number using Linux idr */ 30119b61e302SSuniel Mahesh id = of_alias_get_id(ctlr->dev.of_node, "spi"); 30129b61e302SSuniel Mahesh if (id >= 0) { 30139b61e302SSuniel Mahesh ctlr->bus_num = id; 30149b61e302SSuniel Mahesh mutex_lock(&board_lock); 30159b61e302SSuniel Mahesh id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 30169b61e302SSuniel Mahesh ctlr->bus_num + 1, GFP_KERNEL); 30179b61e302SSuniel Mahesh mutex_unlock(&board_lock); 30189b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 30199b61e302SSuniel Mahesh return id == -ENOSPC ? -EBUSY : id; 30209b61e302SSuniel Mahesh } 30219b61e302SSuniel Mahesh } 30228caab75fSGeert Uytterhoeven if (ctlr->bus_num < 0) { 302342bdd706SLucas Stach first_dynamic = of_alias_get_highest_id("spi"); 302442bdd706SLucas Stach if (first_dynamic < 0) 302542bdd706SLucas Stach first_dynamic = 0; 302642bdd706SLucas Stach else 302742bdd706SLucas Stach first_dynamic++; 302842bdd706SLucas Stach 30299b61e302SSuniel Mahesh mutex_lock(&board_lock); 303042bdd706SLucas Stach id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, 303142bdd706SLucas Stach 0, GFP_KERNEL); 30329b61e302SSuniel Mahesh mutex_unlock(&board_lock); 30339b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 30349b61e302SSuniel Mahesh return id; 30359b61e302SSuniel Mahesh ctlr->bus_num = id; 30368ae12a0dSDavid Brownell } 30378caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 30388caab75fSGeert Uytterhoeven init_completion(&ctlr->xfer_completion); 30398caab75fSGeert Uytterhoeven if (!ctlr->max_dma_len) 30408caab75fSGeert Uytterhoeven ctlr->max_dma_len = INT_MAX; 3041cf32b71eSErnst Schwab 3042350de7ceSAndy Shevchenko /* 3043350de7ceSAndy Shevchenko * Register the device, then userspace will see it. 3044350de7ceSAndy Shevchenko * Registration fails if the bus ID is in use. 30458ae12a0dSDavid Brownell */ 30468caab75fSGeert Uytterhoeven dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 30470a919ae4SAndrey Smirnov 3048f48dc6b9SLinus Walleij if (!spi_controller_is_slave(ctlr) && ctlr->use_gpio_descriptors) { 30490a919ae4SAndrey Smirnov status = spi_get_gpio_descs(ctlr); 30500a919ae4SAndrey Smirnov if (status) 3051f9981d4fSAaro Koskinen goto free_bus_id; 30520a919ae4SAndrey Smirnov /* 30530a919ae4SAndrey Smirnov * A controller using GPIO descriptors always 30540a919ae4SAndrey Smirnov * supports SPI_CS_HIGH if need be. 30550a919ae4SAndrey Smirnov */ 30560a919ae4SAndrey Smirnov ctlr->mode_bits |= SPI_CS_HIGH; 30570a919ae4SAndrey Smirnov } 30580a919ae4SAndrey Smirnov 3059f9481b08STudor Ambarus /* 3060f9481b08STudor Ambarus * Even if it's just one always-selected device, there must 3061f9481b08STudor Ambarus * be at least one chipselect. 3062f9481b08STudor Ambarus */ 3063f9981d4fSAaro Koskinen if (!ctlr->num_chipselect) { 3064f9981d4fSAaro Koskinen status = -EINVAL; 3065f9981d4fSAaro Koskinen goto free_bus_id; 3066f9981d4fSAaro Koskinen } 3067f9481b08STudor Ambarus 30686bb477dfSYun Zhou /* setting last_cs to -1 means no chip selected */ 30696bb477dfSYun Zhou ctlr->last_cs = -1; 30706bb477dfSYun Zhou 30718caab75fSGeert Uytterhoeven status = device_add(&ctlr->dev); 3072f9981d4fSAaro Koskinen if (status < 0) 3073f9981d4fSAaro Koskinen goto free_bus_id; 30749b61e302SSuniel Mahesh dev_dbg(dev, "registered %s %s\n", 30758caab75fSGeert Uytterhoeven spi_controller_is_slave(ctlr) ? "slave" : "master", 30769b61e302SSuniel Mahesh dev_name(&ctlr->dev)); 30778ae12a0dSDavid Brownell 3078b5932f5cSBoris Brezillon /* 3079b5932f5cSBoris Brezillon * If we're using a queued driver, start the queue. Note that we don't 3080b5932f5cSBoris Brezillon * need the queueing logic if the driver is only supporting high-level 3081b5932f5cSBoris Brezillon * memory operations. 3082b5932f5cSBoris Brezillon */ 3083b5932f5cSBoris Brezillon if (ctlr->transfer) { 30848caab75fSGeert Uytterhoeven dev_info(dev, "controller is unqueued, this is deprecated\n"); 3085b5932f5cSBoris Brezillon } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 30868caab75fSGeert Uytterhoeven status = spi_controller_initialize_queue(ctlr); 3087ffbbdd21SLinus Walleij if (status) { 30888caab75fSGeert Uytterhoeven device_del(&ctlr->dev); 3089f9981d4fSAaro Koskinen goto free_bus_id; 3090ffbbdd21SLinus Walleij } 3091ffbbdd21SLinus Walleij } 3092eca2ebc7SMartin Sperl /* add statistics */ 30936598b91bSDavid Jander ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev); 30946598b91bSDavid Jander if (!ctlr->pcpu_statistics) { 30956598b91bSDavid Jander dev_err(dev, "Error allocating per-cpu statistics\n"); 3096d52b095bSDan Carpenter status = -ENOMEM; 30976598b91bSDavid Jander goto destroy_queue; 30986598b91bSDavid Jander } 3099ffbbdd21SLinus Walleij 31002b9603a0SFeng Tang mutex_lock(&board_lock); 31018caab75fSGeert Uytterhoeven list_add_tail(&ctlr->list, &spi_controller_list); 31022b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 31038caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 31042b9603a0SFeng Tang mutex_unlock(&board_lock); 31052b9603a0SFeng Tang 310664bee4d2SMika Westerberg /* Register devices from the device tree and ACPI */ 31078caab75fSGeert Uytterhoeven of_register_spi_devices(ctlr); 31088caab75fSGeert Uytterhoeven acpi_register_spi_devices(ctlr); 3109f9981d4fSAaro Koskinen return status; 3110f9981d4fSAaro Koskinen 31116598b91bSDavid Jander destroy_queue: 31126598b91bSDavid Jander spi_destroy_queue(ctlr); 3113f9981d4fSAaro Koskinen free_bus_id: 3114f9981d4fSAaro Koskinen mutex_lock(&board_lock); 3115f9981d4fSAaro Koskinen idr_remove(&spi_master_idr, ctlr->bus_num); 3116f9981d4fSAaro Koskinen mutex_unlock(&board_lock); 31178ae12a0dSDavid Brownell return status; 31188ae12a0dSDavid Brownell } 31198caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller); 31208ae12a0dSDavid Brownell 312159ebbe40STian Tao static void devm_spi_unregister(void *ctlr) 3122666d5b4cSMark Brown { 312359ebbe40STian Tao spi_unregister_controller(ctlr); 3124666d5b4cSMark Brown } 3125666d5b4cSMark Brown 3126666d5b4cSMark Brown /** 31278caab75fSGeert Uytterhoeven * devm_spi_register_controller - register managed SPI master or slave 31288caab75fSGeert Uytterhoeven * controller 31298caab75fSGeert Uytterhoeven * @dev: device managing SPI controller 31308caab75fSGeert Uytterhoeven * @ctlr: initialized controller, originally from spi_alloc_master() or 31318caab75fSGeert Uytterhoeven * spi_alloc_slave() 3132666d5b4cSMark Brown * Context: can sleep 3133666d5b4cSMark Brown * 31348caab75fSGeert Uytterhoeven * Register a SPI device as with spi_register_controller() which will 313568b892f1SJohan Hovold * automatically be unregistered and freed. 313697d56dc6SJavier Martinez Canillas * 313797d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3138666d5b4cSMark Brown */ 31398caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev, 31408caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 3141666d5b4cSMark Brown { 3142666d5b4cSMark Brown int ret; 3143666d5b4cSMark Brown 31448caab75fSGeert Uytterhoeven ret = spi_register_controller(ctlr); 314559ebbe40STian Tao if (ret) 3146666d5b4cSMark Brown return ret; 314759ebbe40STian Tao 314859ebbe40STian Tao return devm_add_action_or_reset(dev, devm_spi_unregister, ctlr); 3149666d5b4cSMark Brown } 31508caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller); 3151666d5b4cSMark Brown 315234860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 31538ae12a0dSDavid Brownell { 31540c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 31558ae12a0dSDavid Brownell return 0; 31568ae12a0dSDavid Brownell } 31578ae12a0dSDavid Brownell 31588ae12a0dSDavid Brownell /** 31598caab75fSGeert Uytterhoeven * spi_unregister_controller - unregister SPI master or slave controller 31608caab75fSGeert Uytterhoeven * @ctlr: the controller being unregistered 316133e34dc6SDavid Brownell * Context: can sleep 31628ae12a0dSDavid Brownell * 31638caab75fSGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 31648ae12a0dSDavid Brownell * only ones directly touching chip registers. 31658ae12a0dSDavid Brownell * 31668ae12a0dSDavid Brownell * This must be called from context that can sleep. 316768b892f1SJohan Hovold * 316868b892f1SJohan Hovold * Note that this function also drops a reference to the controller. 31698ae12a0dSDavid Brownell */ 31708caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr) 31718ae12a0dSDavid Brownell { 31729b61e302SSuniel Mahesh struct spi_controller *found; 317367f7b278SJohan Hovold int id = ctlr->bus_num; 317489fc9a1aSJeff Garzik 3175ddf75be4SLukas Wunner /* Prevent addition of new devices, unregister existing ones */ 3176ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 31776098475dSMark Brown mutex_lock(&ctlr->add_lock); 3178ddf75be4SLukas Wunner 317984855678SLukas Wunner device_for_each_child(&ctlr->dev, NULL, __unregister); 318084855678SLukas Wunner 31819b61e302SSuniel Mahesh /* First make sure that this controller was ever added */ 31829b61e302SSuniel Mahesh mutex_lock(&board_lock); 318367f7b278SJohan Hovold found = idr_find(&spi_master_idr, id); 31849b61e302SSuniel Mahesh mutex_unlock(&board_lock); 31858caab75fSGeert Uytterhoeven if (ctlr->queued) { 31868caab75fSGeert Uytterhoeven if (spi_destroy_queue(ctlr)) 31878caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue remove failed\n"); 3188ffbbdd21SLinus Walleij } 31892b9603a0SFeng Tang mutex_lock(&board_lock); 31908caab75fSGeert Uytterhoeven list_del(&ctlr->list); 31912b9603a0SFeng Tang mutex_unlock(&board_lock); 31922b9603a0SFeng Tang 31935e844cc3SLukas Wunner device_del(&ctlr->dev); 31945e844cc3SLukas Wunner 31959b61e302SSuniel Mahesh /* free bus id */ 31969b61e302SSuniel Mahesh mutex_lock(&board_lock); 3197613bd1eaSJarkko Nikula if (found == ctlr) 319867f7b278SJohan Hovold idr_remove(&spi_master_idr, id); 31999b61e302SSuniel Mahesh mutex_unlock(&board_lock); 3200ddf75be4SLukas Wunner 3201ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 32026098475dSMark Brown mutex_unlock(&ctlr->add_lock); 32036c53b45cSMichael Walle 32046c53b45cSMichael Walle /* Release the last reference on the controller if its driver 32056c53b45cSMichael Walle * has not yet been converted to devm_spi_alloc_master/slave(). 32066c53b45cSMichael Walle */ 32076c53b45cSMichael Walle if (!ctlr->devm_allocated) 32086c53b45cSMichael Walle put_device(&ctlr->dev); 32098ae12a0dSDavid Brownell } 32108caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller); 32118ae12a0dSDavid Brownell 32128caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr) 3213ffbbdd21SLinus Walleij { 3214ffbbdd21SLinus Walleij int ret; 3215ffbbdd21SLinus Walleij 32168caab75fSGeert Uytterhoeven /* Basically no-ops for non-queued controllers */ 32178caab75fSGeert Uytterhoeven if (!ctlr->queued) 3218ffbbdd21SLinus Walleij return 0; 3219ffbbdd21SLinus Walleij 32208caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 3221ffbbdd21SLinus Walleij if (ret) 32228caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue stop failed\n"); 3223ffbbdd21SLinus Walleij 3224ffbbdd21SLinus Walleij return ret; 3225ffbbdd21SLinus Walleij } 32268caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend); 3227ffbbdd21SLinus Walleij 32288caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr) 3229ffbbdd21SLinus Walleij { 3230ffbbdd21SLinus Walleij int ret; 3231ffbbdd21SLinus Walleij 32328caab75fSGeert Uytterhoeven if (!ctlr->queued) 3233ffbbdd21SLinus Walleij return 0; 3234ffbbdd21SLinus Walleij 32358caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 3236ffbbdd21SLinus Walleij if (ret) 32378caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue restart failed\n"); 3238ffbbdd21SLinus Walleij 3239ffbbdd21SLinus Walleij return ret; 3240ffbbdd21SLinus Walleij } 32418caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume); 3242ffbbdd21SLinus Walleij 32438ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 32448ae12a0dSDavid Brownell 3245523baf5aSMartin Sperl /* Core methods for spi_message alterations */ 3246523baf5aSMartin Sperl 32478caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr, 3248523baf5aSMartin Sperl struct spi_message *msg, 3249523baf5aSMartin Sperl void *res) 3250523baf5aSMartin Sperl { 3251523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer = res; 3252523baf5aSMartin Sperl size_t i; 3253523baf5aSMartin Sperl 3254523baf5aSMartin Sperl /* call extra callback if requested */ 3255523baf5aSMartin Sperl if (rxfer->release) 32568caab75fSGeert Uytterhoeven rxfer->release(ctlr, msg, res); 3257523baf5aSMartin Sperl 3258523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 3259523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 3260523baf5aSMartin Sperl 3261523baf5aSMartin Sperl /* remove the formerly inserted entries */ 3262523baf5aSMartin Sperl for (i = 0; i < rxfer->inserted; i++) 3263523baf5aSMartin Sperl list_del(&rxfer->inserted_transfers[i].transfer_list); 3264523baf5aSMartin Sperl } 3265523baf5aSMartin Sperl 3266523baf5aSMartin Sperl /** 3267523baf5aSMartin Sperl * spi_replace_transfers - replace transfers with several transfers 3268523baf5aSMartin Sperl * and register change with spi_message.resources 3269523baf5aSMartin Sperl * @msg: the spi_message we work upon 3270523baf5aSMartin Sperl * @xfer_first: the first spi_transfer we want to replace 3271523baf5aSMartin Sperl * @remove: number of transfers to remove 3272523baf5aSMartin Sperl * @insert: the number of transfers we want to insert instead 3273523baf5aSMartin Sperl * @release: extra release code necessary in some circumstances 3274523baf5aSMartin Sperl * @extradatasize: extra data to allocate (with alignment guarantees 3275523baf5aSMartin Sperl * of struct @spi_transfer) 327605885397SMartin Sperl * @gfp: gfp flags 3277523baf5aSMartin Sperl * 3278523baf5aSMartin Sperl * Returns: pointer to @spi_replaced_transfers, 3279523baf5aSMartin Sperl * PTR_ERR(...) in case of errors. 3280523baf5aSMartin Sperl */ 3281da21fde0SUwe Kleine-König static struct spi_replaced_transfers *spi_replace_transfers( 3282523baf5aSMartin Sperl struct spi_message *msg, 3283523baf5aSMartin Sperl struct spi_transfer *xfer_first, 3284523baf5aSMartin Sperl size_t remove, 3285523baf5aSMartin Sperl size_t insert, 3286523baf5aSMartin Sperl spi_replaced_release_t release, 3287523baf5aSMartin Sperl size_t extradatasize, 3288523baf5aSMartin Sperl gfp_t gfp) 3289523baf5aSMartin Sperl { 3290523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer; 3291523baf5aSMartin Sperl struct spi_transfer *xfer; 3292523baf5aSMartin Sperl size_t i; 3293523baf5aSMartin Sperl 3294523baf5aSMartin Sperl /* allocate the structure using spi_res */ 3295523baf5aSMartin Sperl rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 3296aef97522SGustavo A. R. Silva struct_size(rxfer, inserted_transfers, insert) 3297523baf5aSMartin Sperl + extradatasize, 3298523baf5aSMartin Sperl gfp); 3299523baf5aSMartin Sperl if (!rxfer) 3300523baf5aSMartin Sperl return ERR_PTR(-ENOMEM); 3301523baf5aSMartin Sperl 3302523baf5aSMartin Sperl /* the release code to invoke before running the generic release */ 3303523baf5aSMartin Sperl rxfer->release = release; 3304523baf5aSMartin Sperl 3305523baf5aSMartin Sperl /* assign extradata */ 3306523baf5aSMartin Sperl if (extradatasize) 3307523baf5aSMartin Sperl rxfer->extradata = 3308523baf5aSMartin Sperl &rxfer->inserted_transfers[insert]; 3309523baf5aSMartin Sperl 3310523baf5aSMartin Sperl /* init the replaced_transfers list */ 3311523baf5aSMartin Sperl INIT_LIST_HEAD(&rxfer->replaced_transfers); 3312523baf5aSMartin Sperl 3313350de7ceSAndy Shevchenko /* 3314350de7ceSAndy Shevchenko * Assign the list_entry after which we should reinsert 3315523baf5aSMartin Sperl * the @replaced_transfers - it may be spi_message.messages! 3316523baf5aSMartin Sperl */ 3317523baf5aSMartin Sperl rxfer->replaced_after = xfer_first->transfer_list.prev; 3318523baf5aSMartin Sperl 3319523baf5aSMartin Sperl /* remove the requested number of transfers */ 3320523baf5aSMartin Sperl for (i = 0; i < remove; i++) { 3321350de7ceSAndy Shevchenko /* 3322350de7ceSAndy Shevchenko * If the entry after replaced_after it is msg->transfers 3323523baf5aSMartin Sperl * then we have been requested to remove more transfers 3324350de7ceSAndy Shevchenko * than are in the list. 3325523baf5aSMartin Sperl */ 3326523baf5aSMartin Sperl if (rxfer->replaced_after->next == &msg->transfers) { 3327523baf5aSMartin Sperl dev_err(&msg->spi->dev, 3328523baf5aSMartin Sperl "requested to remove more spi_transfers than are available\n"); 3329523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 3330523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, 3331523baf5aSMartin Sperl rxfer->replaced_after); 3332523baf5aSMartin Sperl 3333523baf5aSMartin Sperl /* free the spi_replace_transfer structure */ 3334523baf5aSMartin Sperl spi_res_free(rxfer); 3335523baf5aSMartin Sperl 3336523baf5aSMartin Sperl /* and return with an error */ 3337523baf5aSMartin Sperl return ERR_PTR(-EINVAL); 3338523baf5aSMartin Sperl } 3339523baf5aSMartin Sperl 3340350de7ceSAndy Shevchenko /* 3341350de7ceSAndy Shevchenko * Remove the entry after replaced_after from list of 3342350de7ceSAndy Shevchenko * transfers and add it to list of replaced_transfers. 3343523baf5aSMartin Sperl */ 3344523baf5aSMartin Sperl list_move_tail(rxfer->replaced_after->next, 3345523baf5aSMartin Sperl &rxfer->replaced_transfers); 3346523baf5aSMartin Sperl } 3347523baf5aSMartin Sperl 3348350de7ceSAndy Shevchenko /* 3349350de7ceSAndy Shevchenko * Create copy of the given xfer with identical settings 3350350de7ceSAndy Shevchenko * based on the first transfer to get removed. 3351523baf5aSMartin Sperl */ 3352523baf5aSMartin Sperl for (i = 0; i < insert; i++) { 3353523baf5aSMartin Sperl /* we need to run in reverse order */ 3354523baf5aSMartin Sperl xfer = &rxfer->inserted_transfers[insert - 1 - i]; 3355523baf5aSMartin Sperl 3356523baf5aSMartin Sperl /* copy all spi_transfer data */ 3357523baf5aSMartin Sperl memcpy(xfer, xfer_first, sizeof(*xfer)); 3358523baf5aSMartin Sperl 3359523baf5aSMartin Sperl /* add to list */ 3360523baf5aSMartin Sperl list_add(&xfer->transfer_list, rxfer->replaced_after); 3361523baf5aSMartin Sperl 3362bebcfd27SAlexandru Ardelean /* clear cs_change and delay for all but the last */ 3363523baf5aSMartin Sperl if (i) { 3364523baf5aSMartin Sperl xfer->cs_change = false; 3365bebcfd27SAlexandru Ardelean xfer->delay.value = 0; 3366523baf5aSMartin Sperl } 3367523baf5aSMartin Sperl } 3368523baf5aSMartin Sperl 3369523baf5aSMartin Sperl /* set up inserted */ 3370523baf5aSMartin Sperl rxfer->inserted = insert; 3371523baf5aSMartin Sperl 3372523baf5aSMartin Sperl /* and register it with spi_res/spi_message */ 3373523baf5aSMartin Sperl spi_res_add(msg, rxfer); 3374523baf5aSMartin Sperl 3375523baf5aSMartin Sperl return rxfer; 3376523baf5aSMartin Sperl } 3377523baf5aSMartin Sperl 33788caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 3379d9f12122SMartin Sperl struct spi_message *msg, 3380d9f12122SMartin Sperl struct spi_transfer **xferp, 3381d9f12122SMartin Sperl size_t maxsize, 3382d9f12122SMartin Sperl gfp_t gfp) 3383d9f12122SMartin Sperl { 3384d9f12122SMartin Sperl struct spi_transfer *xfer = *xferp, *xfers; 3385d9f12122SMartin Sperl struct spi_replaced_transfers *srt; 3386d9f12122SMartin Sperl size_t offset; 3387d9f12122SMartin Sperl size_t count, i; 3388d9f12122SMartin Sperl 3389d9f12122SMartin Sperl /* calculate how many we have to replace */ 3390d9f12122SMartin Sperl count = DIV_ROUND_UP(xfer->len, maxsize); 3391d9f12122SMartin Sperl 3392d9f12122SMartin Sperl /* create replacement */ 3393d9f12122SMartin Sperl srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 3394657d32efSDan Carpenter if (IS_ERR(srt)) 3395657d32efSDan Carpenter return PTR_ERR(srt); 3396d9f12122SMartin Sperl xfers = srt->inserted_transfers; 3397d9f12122SMartin Sperl 3398350de7ceSAndy Shevchenko /* 3399350de7ceSAndy Shevchenko * Now handle each of those newly inserted spi_transfers. 3400350de7ceSAndy Shevchenko * Note that the replacements spi_transfers all are preset 3401d9f12122SMartin Sperl * to the same values as *xferp, so tx_buf, rx_buf and len 3402d9f12122SMartin Sperl * are all identical (as well as most others) 3403d9f12122SMartin Sperl * so we just have to fix up len and the pointers. 3404d9f12122SMartin Sperl * 3405350de7ceSAndy Shevchenko * This also includes support for the depreciated 3406350de7ceSAndy Shevchenko * spi_message.is_dma_mapped interface. 3407d9f12122SMartin Sperl */ 3408d9f12122SMartin Sperl 3409350de7ceSAndy Shevchenko /* 3410350de7ceSAndy Shevchenko * The first transfer just needs the length modified, so we 3411350de7ceSAndy Shevchenko * run it outside the loop. 3412d9f12122SMartin Sperl */ 3413c8dab77aSFabio Estevam xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 3414d9f12122SMartin Sperl 3415d9f12122SMartin Sperl /* all the others need rx_buf/tx_buf also set */ 3416d9f12122SMartin Sperl for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 3417d9f12122SMartin Sperl /* update rx_buf, tx_buf and dma */ 3418d9f12122SMartin Sperl if (xfers[i].rx_buf) 3419d9f12122SMartin Sperl xfers[i].rx_buf += offset; 3420d9f12122SMartin Sperl if (xfers[i].rx_dma) 3421d9f12122SMartin Sperl xfers[i].rx_dma += offset; 3422d9f12122SMartin Sperl if (xfers[i].tx_buf) 3423d9f12122SMartin Sperl xfers[i].tx_buf += offset; 3424d9f12122SMartin Sperl if (xfers[i].tx_dma) 3425d9f12122SMartin Sperl xfers[i].tx_dma += offset; 3426d9f12122SMartin Sperl 3427d9f12122SMartin Sperl /* update length */ 3428d9f12122SMartin Sperl xfers[i].len = min(maxsize, xfers[i].len - offset); 3429d9f12122SMartin Sperl } 3430d9f12122SMartin Sperl 3431350de7ceSAndy Shevchenko /* 3432350de7ceSAndy Shevchenko * We set up xferp to the last entry we have inserted, 3433350de7ceSAndy Shevchenko * so that we skip those already split transfers. 3434d9f12122SMartin Sperl */ 3435d9f12122SMartin Sperl *xferp = &xfers[count - 1]; 3436d9f12122SMartin Sperl 3437d9f12122SMartin Sperl /* increment statistics counters */ 34386598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, 3439d9f12122SMartin Sperl transfers_split_maxsize); 34406598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(msg->spi->pcpu_statistics, 3441d9f12122SMartin Sperl transfers_split_maxsize); 3442d9f12122SMartin Sperl 3443d9f12122SMartin Sperl return 0; 3444d9f12122SMartin Sperl } 3445d9f12122SMartin Sperl 3446d9f12122SMartin Sperl /** 3447ce2424d7SMauro Carvalho Chehab * spi_split_transfers_maxsize - split spi transfers into multiple transfers 3448d9f12122SMartin Sperl * when an individual transfer exceeds a 3449d9f12122SMartin Sperl * certain size 34508caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller for this transfer 34513700ce95SMasanari Iida * @msg: the @spi_message to transform 34523700ce95SMasanari Iida * @maxsize: the maximum when to apply this 345310f11a22SJavier Martinez Canillas * @gfp: GFP allocation flags 3454d9f12122SMartin Sperl * 3455d9f12122SMartin Sperl * Return: status of transformation 3456d9f12122SMartin Sperl */ 34578caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr, 3458d9f12122SMartin Sperl struct spi_message *msg, 3459d9f12122SMartin Sperl size_t maxsize, 3460d9f12122SMartin Sperl gfp_t gfp) 3461d9f12122SMartin Sperl { 3462d9f12122SMartin Sperl struct spi_transfer *xfer; 3463d9f12122SMartin Sperl int ret; 3464d9f12122SMartin Sperl 3465350de7ceSAndy Shevchenko /* 3466350de7ceSAndy Shevchenko * Iterate over the transfer_list, 3467d9f12122SMartin Sperl * but note that xfer is advanced to the last transfer inserted 3468d9f12122SMartin Sperl * to avoid checking sizes again unnecessarily (also xfer does 3469350de7ceSAndy Shevchenko * potentially belong to a different list by the time the 3470350de7ceSAndy Shevchenko * replacement has happened). 3471d9f12122SMartin Sperl */ 3472d9f12122SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 3473d9f12122SMartin Sperl if (xfer->len > maxsize) { 34748caab75fSGeert Uytterhoeven ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 34758caab75fSGeert Uytterhoeven maxsize, gfp); 3476d9f12122SMartin Sperl if (ret) 3477d9f12122SMartin Sperl return ret; 3478d9f12122SMartin Sperl } 3479d9f12122SMartin Sperl } 3480d9f12122SMartin Sperl 3481d9f12122SMartin Sperl return 0; 3482d9f12122SMartin Sperl } 3483d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 34848ae12a0dSDavid Brownell 34858ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 34868ae12a0dSDavid Brownell 34878caab75fSGeert Uytterhoeven /* Core methods for SPI controller protocol drivers. Some of the 34887d077197SDavid Brownell * other core methods are currently defined as inline functions. 34897d077197SDavid Brownell */ 34907d077197SDavid Brownell 34918caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 34928caab75fSGeert Uytterhoeven u8 bits_per_word) 349363ab645fSStefan Brüns { 34948caab75fSGeert Uytterhoeven if (ctlr->bits_per_word_mask) { 349563ab645fSStefan Brüns /* Only 32 bits fit in the mask */ 349663ab645fSStefan Brüns if (bits_per_word > 32) 349763ab645fSStefan Brüns return -EINVAL; 34988caab75fSGeert Uytterhoeven if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 349963ab645fSStefan Brüns return -EINVAL; 350063ab645fSStefan Brüns } 350163ab645fSStefan Brüns 350263ab645fSStefan Brüns return 0; 350363ab645fSStefan Brüns } 350463ab645fSStefan Brüns 35057d077197SDavid Brownell /** 35067d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 35077d077197SDavid Brownell * @spi: the device whose settings are being modified 35087d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 35097d077197SDavid Brownell * 35107d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 35117d077197SDavid Brownell * device doesn't work with its default. They may likewise need 35127d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 35137d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 35147d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 35157d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 35167d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 35177d077197SDavid Brownell * 35187d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 35197d077197SDavid Brownell * that the underlying controller or its driver does not support. For 35207d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 35217d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 352297d56dc6SJavier Martinez Canillas * 352397d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 35247d077197SDavid Brownell */ 35257d077197SDavid Brownell int spi_setup(struct spi_device *spi) 35267d077197SDavid Brownell { 352783596fbeSGeert Uytterhoeven unsigned bad_bits, ugly_bits; 352873f93db5SPaul Kocialkowski int status = 0; 35297d077197SDavid Brownell 3530d962608cSDragos Bogdan /* 3531350de7ceSAndy Shevchenko * Check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO 3532350de7ceSAndy Shevchenko * are set at the same time. 3533f477b7fbSwangyuhang */ 3534d962608cSDragos Bogdan if ((hweight_long(spi->mode & 3535d962608cSDragos Bogdan (SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) || 3536d962608cSDragos Bogdan (hweight_long(spi->mode & 3537d962608cSDragos Bogdan (SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) { 3538f477b7fbSwangyuhang dev_err(&spi->dev, 3539d962608cSDragos Bogdan "setup: can not select any two of dual, quad and no-rx/tx at the same time\n"); 3540f477b7fbSwangyuhang return -EINVAL; 3541f477b7fbSwangyuhang } 3542350de7ceSAndy Shevchenko /* If it is SPI_3WIRE mode, DUAL and QUAD should be forbidden */ 3543f477b7fbSwangyuhang if ((spi->mode & SPI_3WIRE) && (spi->mode & 35446b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 35456b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 3546f477b7fbSwangyuhang return -EINVAL; 3547350de7ceSAndy Shevchenko /* 3548350de7ceSAndy Shevchenko * Help drivers fail *cleanly* when they need options 3549350de7ceSAndy Shevchenko * that aren't supported with their current controller. 3550cbaa62e0SDavid Lechner * SPI_CS_WORD has a fallback software implementation, 3551cbaa62e0SDavid Lechner * so it is ignored here. 3552e7db06b5SDavid Brownell */ 3553d962608cSDragos Bogdan bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | 3554d962608cSDragos Bogdan SPI_NO_TX | SPI_NO_RX); 355583596fbeSGeert Uytterhoeven ugly_bits = bad_bits & 35566b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 35576b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); 355883596fbeSGeert Uytterhoeven if (ugly_bits) { 355983596fbeSGeert Uytterhoeven dev_warn(&spi->dev, 356083596fbeSGeert Uytterhoeven "setup: ignoring unsupported mode bits %x\n", 356183596fbeSGeert Uytterhoeven ugly_bits); 356283596fbeSGeert Uytterhoeven spi->mode &= ~ugly_bits; 356383596fbeSGeert Uytterhoeven bad_bits &= ~ugly_bits; 356483596fbeSGeert Uytterhoeven } 3565e7db06b5SDavid Brownell if (bad_bits) { 3566eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 3567e7db06b5SDavid Brownell bad_bits); 3568e7db06b5SDavid Brownell return -EINVAL; 3569e7db06b5SDavid Brownell } 3570e7db06b5SDavid Brownell 3571b3fe2e51SPaul Kocialkowski if (!spi->bits_per_word) { 35727d077197SDavid Brownell spi->bits_per_word = 8; 3573b3fe2e51SPaul Kocialkowski } else { 3574b3fe2e51SPaul Kocialkowski /* 3575b3fe2e51SPaul Kocialkowski * Some controllers may not support the default 8 bits-per-word 3576b3fe2e51SPaul Kocialkowski * so only perform the check when this is explicitly provided. 3577b3fe2e51SPaul Kocialkowski */ 35788caab75fSGeert Uytterhoeven status = __spi_validate_bits_per_word(spi->controller, 35798caab75fSGeert Uytterhoeven spi->bits_per_word); 35805ab8d262SAndy Shevchenko if (status) 35815ab8d262SAndy Shevchenko return status; 3582b3fe2e51SPaul Kocialkowski } 358363ab645fSStefan Brüns 35846820e812STudor Ambarus if (spi->controller->max_speed_hz && 35856820e812STudor Ambarus (!spi->max_speed_hz || 35866820e812STudor Ambarus spi->max_speed_hz > spi->controller->max_speed_hz)) 35878caab75fSGeert Uytterhoeven spi->max_speed_hz = spi->controller->max_speed_hz; 3588052eb2d4SAxel Lin 35894fae3a58SSerge Semin mutex_lock(&spi->controller->io_mutex); 35904fae3a58SSerge Semin 3591c914dbf8SJoe Burmeister if (spi->controller->setup) { 35928caab75fSGeert Uytterhoeven status = spi->controller->setup(spi); 3593c914dbf8SJoe Burmeister if (status) { 3594c914dbf8SJoe Burmeister mutex_unlock(&spi->controller->io_mutex); 3595c914dbf8SJoe Burmeister dev_err(&spi->controller->dev, "Failed to setup device: %d\n", 3596c914dbf8SJoe Burmeister status); 3597c914dbf8SJoe Burmeister return status; 3598c914dbf8SJoe Burmeister } 3599c914dbf8SJoe Burmeister } 36007d077197SDavid Brownell 3601d948e6caSLuhua Xu if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { 3602dd769f15SMinghao Chi status = pm_runtime_resume_and_get(spi->controller->dev.parent); 3603d948e6caSLuhua Xu if (status < 0) { 36044fae3a58SSerge Semin mutex_unlock(&spi->controller->io_mutex); 3605d948e6caSLuhua Xu dev_err(&spi->controller->dev, "Failed to power device: %d\n", 3606d948e6caSLuhua Xu status); 3607d948e6caSLuhua Xu return status; 3608d948e6caSLuhua Xu } 360957a94607STony Lindgren 361057a94607STony Lindgren /* 361157a94607STony Lindgren * We do not want to return positive value from pm_runtime_get, 361257a94607STony Lindgren * there are many instances of devices calling spi_setup() and 361357a94607STony Lindgren * checking for a non-zero return value instead of a negative 361457a94607STony Lindgren * return value. 361557a94607STony Lindgren */ 361657a94607STony Lindgren status = 0; 361757a94607STony Lindgren 3618d347b4aaSDavid Bauer spi_set_cs(spi, false, true); 3619d948e6caSLuhua Xu pm_runtime_mark_last_busy(spi->controller->dev.parent); 3620d948e6caSLuhua Xu pm_runtime_put_autosuspend(spi->controller->dev.parent); 3621d948e6caSLuhua Xu } else { 3622d347b4aaSDavid Bauer spi_set_cs(spi, false, true); 3623d948e6caSLuhua Xu } 3624abeedb01SFranklin S Cooper Jr 36254fae3a58SSerge Semin mutex_unlock(&spi->controller->io_mutex); 36264fae3a58SSerge Semin 3627924b5867SDouglas Anderson if (spi->rt && !spi->controller->rt) { 3628924b5867SDouglas Anderson spi->controller->rt = true; 3629924b5867SDouglas Anderson spi_set_thread_rt(spi->controller); 3630924b5867SDouglas Anderson } 3631924b5867SDouglas Anderson 36325cb4e1f3SAndy Shevchenko trace_spi_setup(spi, status); 36335cb4e1f3SAndy Shevchenko 363440b82c2dSAndy Shevchenko dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 363540b82c2dSAndy Shevchenko spi->mode & SPI_MODE_X_MASK, 36367d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 36377d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 36387d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 36397d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 36407d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 36417d077197SDavid Brownell status); 36427d077197SDavid Brownell 36437d077197SDavid Brownell return status; 36447d077197SDavid Brownell } 36457d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 36467d077197SDavid Brownell 36476c613f68SAlexandru Ardelean static int _spi_xfer_word_delay_update(struct spi_transfer *xfer, 36486c613f68SAlexandru Ardelean struct spi_device *spi) 36496c613f68SAlexandru Ardelean { 36506c613f68SAlexandru Ardelean int delay1, delay2; 36516c613f68SAlexandru Ardelean 36523984d39bSAlexandru Ardelean delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); 36536c613f68SAlexandru Ardelean if (delay1 < 0) 36546c613f68SAlexandru Ardelean return delay1; 36556c613f68SAlexandru Ardelean 36563984d39bSAlexandru Ardelean delay2 = spi_delay_to_ns(&spi->word_delay, xfer); 36576c613f68SAlexandru Ardelean if (delay2 < 0) 36586c613f68SAlexandru Ardelean return delay2; 36596c613f68SAlexandru Ardelean 36606c613f68SAlexandru Ardelean if (delay1 < delay2) 36616c613f68SAlexandru Ardelean memcpy(&xfer->word_delay, &spi->word_delay, 36626c613f68SAlexandru Ardelean sizeof(xfer->word_delay)); 36636c613f68SAlexandru Ardelean 36646c613f68SAlexandru Ardelean return 0; 36656c613f68SAlexandru Ardelean } 36666c613f68SAlexandru Ardelean 366790808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message) 3668cf32b71eSErnst Schwab { 36698caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3670e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 36716ea31293SAtsushi Nemoto int w_size; 3672cf32b71eSErnst Schwab 367324a0013aSMark Brown if (list_empty(&message->transfers)) 367424a0013aSMark Brown return -EINVAL; 367524a0013aSMark Brown 3676350de7ceSAndy Shevchenko /* 3677350de7ceSAndy Shevchenko * If an SPI controller does not support toggling the CS line on each 367871388b21SDavid Lechner * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO 367971388b21SDavid Lechner * for the CS line, we can emulate the CS-per-word hardware function by 3680cbaa62e0SDavid Lechner * splitting transfers into one-word transfers and ensuring that 3681cbaa62e0SDavid Lechner * cs_change is set for each transfer. 3682cbaa62e0SDavid Lechner */ 368371388b21SDavid Lechner if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || 3684f48dc6b9SLinus Walleij spi->cs_gpiod)) { 3685cbaa62e0SDavid Lechner size_t maxsize; 3686cbaa62e0SDavid Lechner int ret; 3687cbaa62e0SDavid Lechner 3688cbaa62e0SDavid Lechner maxsize = (spi->bits_per_word + 7) / 8; 3689cbaa62e0SDavid Lechner 3690cbaa62e0SDavid Lechner /* spi_split_transfers_maxsize() requires message->spi */ 3691cbaa62e0SDavid Lechner message->spi = spi; 3692cbaa62e0SDavid Lechner 3693cbaa62e0SDavid Lechner ret = spi_split_transfers_maxsize(ctlr, message, maxsize, 3694cbaa62e0SDavid Lechner GFP_KERNEL); 3695cbaa62e0SDavid Lechner if (ret) 3696cbaa62e0SDavid Lechner return ret; 3697cbaa62e0SDavid Lechner 3698cbaa62e0SDavid Lechner list_for_each_entry(xfer, &message->transfers, transfer_list) { 3699cbaa62e0SDavid Lechner /* don't change cs_change on the last entry in the list */ 3700cbaa62e0SDavid Lechner if (list_is_last(&xfer->transfer_list, &message->transfers)) 3701cbaa62e0SDavid Lechner break; 3702cbaa62e0SDavid Lechner xfer->cs_change = 1; 3703cbaa62e0SDavid Lechner } 3704cbaa62e0SDavid Lechner } 3705cbaa62e0SDavid Lechner 3706350de7ceSAndy Shevchenko /* 3707350de7ceSAndy Shevchenko * Half-duplex links include original MicroWire, and ones with 3708cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 3709cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 3710cf32b71eSErnst Schwab * software limitations. 3711cf32b71eSErnst Schwab */ 37128caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 37138caab75fSGeert Uytterhoeven (spi->mode & SPI_3WIRE)) { 37148caab75fSGeert Uytterhoeven unsigned flags = ctlr->flags; 3715cf32b71eSErnst Schwab 3716cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 3717cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 3718cf32b71eSErnst Schwab return -EINVAL; 37198caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 3720cf32b71eSErnst Schwab return -EINVAL; 37218caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 3722cf32b71eSErnst Schwab return -EINVAL; 3723cf32b71eSErnst Schwab } 3724cf32b71eSErnst Schwab } 3725cf32b71eSErnst Schwab 3726350de7ceSAndy Shevchenko /* 3727059b8ffeSLaxman Dewangan * Set transfer bits_per_word and max speed as spi device default if 3728059b8ffeSLaxman Dewangan * it is not set for this transfer. 3729f477b7fbSwangyuhang * Set transfer tx_nbits and rx_nbits as single transfer default 3730f477b7fbSwangyuhang * (SPI_NBITS_SINGLE) if it is not set for this transfer. 3731b7bb367aSJonas Bonn * Ensure transfer word_delay is at least as long as that required by 3732b7bb367aSJonas Bonn * device itself. 3733e6811d1dSLaxman Dewangan */ 373477e80588SMartin Sperl message->frame_length = 0; 3735e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 37365d7e2b5eSMartin Sperl xfer->effective_speed_hz = 0; 3737078726ceSSourav Poddar message->frame_length += xfer->len; 3738e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 3739e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 3740a6f87fadSAxel Lin 3741a6f87fadSAxel Lin if (!xfer->speed_hz) 3742059b8ffeSLaxman Dewangan xfer->speed_hz = spi->max_speed_hz; 3743a6f87fadSAxel Lin 37448caab75fSGeert Uytterhoeven if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 37458caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 374656ede94aSGabor Juhos 37478caab75fSGeert Uytterhoeven if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 3748543bb255SStephen Warren return -EINVAL; 3749a2fd4f9fSMark Brown 37504d94bd21SIvan T. Ivanov /* 37514d94bd21SIvan T. Ivanov * SPI transfer length should be multiple of SPI word size 3752350de7ceSAndy Shevchenko * where SPI word size should be power-of-two multiple. 37534d94bd21SIvan T. Ivanov */ 37544d94bd21SIvan T. Ivanov if (xfer->bits_per_word <= 8) 37554d94bd21SIvan T. Ivanov w_size = 1; 37564d94bd21SIvan T. Ivanov else if (xfer->bits_per_word <= 16) 37574d94bd21SIvan T. Ivanov w_size = 2; 37584d94bd21SIvan T. Ivanov else 37594d94bd21SIvan T. Ivanov w_size = 4; 37604d94bd21SIvan T. Ivanov 37614d94bd21SIvan T. Ivanov /* No partial transfers accepted */ 37626ea31293SAtsushi Nemoto if (xfer->len % w_size) 37634d94bd21SIvan T. Ivanov return -EINVAL; 37644d94bd21SIvan T. Ivanov 37658caab75fSGeert Uytterhoeven if (xfer->speed_hz && ctlr->min_speed_hz && 37668caab75fSGeert Uytterhoeven xfer->speed_hz < ctlr->min_speed_hz) 3767a2fd4f9fSMark Brown return -EINVAL; 3768f477b7fbSwangyuhang 3769f477b7fbSwangyuhang if (xfer->tx_buf && !xfer->tx_nbits) 3770f477b7fbSwangyuhang xfer->tx_nbits = SPI_NBITS_SINGLE; 3771f477b7fbSwangyuhang if (xfer->rx_buf && !xfer->rx_nbits) 3772f477b7fbSwangyuhang xfer->rx_nbits = SPI_NBITS_SINGLE; 3773350de7ceSAndy Shevchenko /* 3774350de7ceSAndy Shevchenko * Check transfer tx/rx_nbits: 37751afd9989SGeert Uytterhoeven * 1. check the value matches one of single, dual and quad 37761afd9989SGeert Uytterhoeven * 2. check tx/rx_nbits match the mode in spi_device 3777f477b7fbSwangyuhang */ 3778db90a441SSourav Poddar if (xfer->tx_buf) { 3779d962608cSDragos Bogdan if (spi->mode & SPI_NO_TX) 3780d962608cSDragos Bogdan return -EINVAL; 3781f477b7fbSwangyuhang if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3782f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_DUAL && 3783f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_QUAD) 3784a2fd4f9fSMark Brown return -EINVAL; 3785f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 3786f477b7fbSwangyuhang !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3787f477b7fbSwangyuhang return -EINVAL; 3788f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 3789f477b7fbSwangyuhang !(spi->mode & SPI_TX_QUAD)) 3790f477b7fbSwangyuhang return -EINVAL; 3791db90a441SSourav Poddar } 3792f477b7fbSwangyuhang /* check transfer rx_nbits */ 3793db90a441SSourav Poddar if (xfer->rx_buf) { 3794d962608cSDragos Bogdan if (spi->mode & SPI_NO_RX) 3795d962608cSDragos Bogdan return -EINVAL; 3796f477b7fbSwangyuhang if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3797f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_DUAL && 3798f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_QUAD) 3799f477b7fbSwangyuhang return -EINVAL; 3800f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 3801f477b7fbSwangyuhang !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 3802f477b7fbSwangyuhang return -EINVAL; 3803f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 3804f477b7fbSwangyuhang !(spi->mode & SPI_RX_QUAD)) 3805f477b7fbSwangyuhang return -EINVAL; 3806e6811d1dSLaxman Dewangan } 3807b7bb367aSJonas Bonn 38086c613f68SAlexandru Ardelean if (_spi_xfer_word_delay_update(xfer, spi)) 38096c613f68SAlexandru Ardelean return -EINVAL; 3810e6811d1dSLaxman Dewangan } 3811e6811d1dSLaxman Dewangan 3812cf32b71eSErnst Schwab message->status = -EINPROGRESS; 381390808738SMark Brown 381490808738SMark Brown return 0; 381590808738SMark Brown } 381690808738SMark Brown 381790808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message) 381890808738SMark Brown { 38198caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3820b42faeeeSVladimir Oltean struct spi_transfer *xfer; 382190808738SMark Brown 3822b5932f5cSBoris Brezillon /* 3823b5932f5cSBoris Brezillon * Some controllers do not support doing regular SPI transfers. Return 3824b5932f5cSBoris Brezillon * ENOTSUPP when this is the case. 3825b5932f5cSBoris Brezillon */ 3826b5932f5cSBoris Brezillon if (!ctlr->transfer) 3827b5932f5cSBoris Brezillon return -ENOTSUPP; 3828b5932f5cSBoris Brezillon 382990808738SMark Brown message->spi = spi; 383090808738SMark Brown 38316598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_async); 38326598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_async); 3833eca2ebc7SMartin Sperl 383490808738SMark Brown trace_spi_message_submit(message); 383590808738SMark Brown 3836b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 3837b42faeeeSVladimir Oltean list_for_each_entry(xfer, &message->transfers, transfer_list) { 3838b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 3839b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 3840b42faeeeSVladimir Oltean } 3841b42faeeeSVladimir Oltean } 3842b42faeeeSVladimir Oltean 38438caab75fSGeert Uytterhoeven return ctlr->transfer(spi, message); 3844cf32b71eSErnst Schwab } 3845cf32b71eSErnst Schwab 3846568d0697SDavid Brownell /** 3847568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 3848568d0697SDavid Brownell * @spi: device with which data will be exchanged 3849568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 3850568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 3851568d0697SDavid Brownell * 3852568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 3853568d0697SDavid Brownell * as well as from task contexts which can sleep. 3854568d0697SDavid Brownell * 3855568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 3856568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 3857568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 3858568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 3859568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 3860568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 3861568d0697SDavid Brownell * core or controller driver code. 3862568d0697SDavid Brownell * 3863568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 3864568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 3865568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 3866568d0697SDavid Brownell * time requirements, for example. 3867568d0697SDavid Brownell * 3868568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 3869568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 3870568d0697SDavid Brownell * Until returning from the associated message completion callback, 3871568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 3872568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 3873568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 387497d56dc6SJavier Martinez Canillas * 387597d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3876568d0697SDavid Brownell */ 3877568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 3878568d0697SDavid Brownell { 38798caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3880cf32b71eSErnst Schwab int ret; 3881cf32b71eSErnst Schwab unsigned long flags; 3882568d0697SDavid Brownell 388390808738SMark Brown ret = __spi_validate(spi, message); 388490808738SMark Brown if (ret != 0) 388590808738SMark Brown return ret; 388690808738SMark Brown 38878caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3888568d0697SDavid Brownell 38898caab75fSGeert Uytterhoeven if (ctlr->bus_lock_flag) 3890cf32b71eSErnst Schwab ret = -EBUSY; 3891cf32b71eSErnst Schwab else 3892cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3893568d0697SDavid Brownell 38948caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3895cf32b71eSErnst Schwab 3896cf32b71eSErnst Schwab return ret; 3897568d0697SDavid Brownell } 3898568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 3899568d0697SDavid Brownell 3900cf32b71eSErnst Schwab /** 3901cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 3902cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3903cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 3904cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 3905cf32b71eSErnst Schwab * 3906cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 3907cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 3908cf32b71eSErnst Schwab * 3909cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 3910cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 3911cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 3912cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 3913cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 3914cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 3915cf32b71eSErnst Schwab * core or controller driver code. 3916cf32b71eSErnst Schwab * 3917cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 3918cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 3919cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 3920cf32b71eSErnst Schwab * time requirements, for example. 3921cf32b71eSErnst Schwab * 3922cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 3923cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 3924cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 3925cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 3926cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 3927cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 392897d56dc6SJavier Martinez Canillas * 392997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3930cf32b71eSErnst Schwab */ 3931da21fde0SUwe Kleine-König static int spi_async_locked(struct spi_device *spi, struct spi_message *message) 3932cf32b71eSErnst Schwab { 39338caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3934cf32b71eSErnst Schwab int ret; 3935cf32b71eSErnst Schwab unsigned long flags; 3936cf32b71eSErnst Schwab 393790808738SMark Brown ret = __spi_validate(spi, message); 393890808738SMark Brown if (ret != 0) 393990808738SMark Brown return ret; 394090808738SMark Brown 39418caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3942cf32b71eSErnst Schwab 3943cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3944cf32b71eSErnst Schwab 39458caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3946cf32b71eSErnst Schwab 3947cf32b71eSErnst Schwab return ret; 3948cf32b71eSErnst Schwab 3949cf32b71eSErnst Schwab } 3950cf32b71eSErnst Schwab 3951ae7d2346SDavid Jander static void __spi_transfer_message_noqueue(struct spi_controller *ctlr, struct spi_message *msg) 3952ae7d2346SDavid Jander { 3953ae7d2346SDavid Jander bool was_busy; 3954ae7d2346SDavid Jander int ret; 3955ae7d2346SDavid Jander 3956ae7d2346SDavid Jander mutex_lock(&ctlr->io_mutex); 3957ae7d2346SDavid Jander 3958*1a9cafcbSDavid Jander was_busy = ctlr->busy; 3959ae7d2346SDavid Jander 3960ae7d2346SDavid Jander ret = __spi_pump_transfer_message(ctlr, msg, was_busy); 3961ae7d2346SDavid Jander if (ret) 3962ae7d2346SDavid Jander goto out; 3963ae7d2346SDavid Jander 3964ae7d2346SDavid Jander if (!was_busy) { 3965ae7d2346SDavid Jander kfree(ctlr->dummy_rx); 3966ae7d2346SDavid Jander ctlr->dummy_rx = NULL; 3967ae7d2346SDavid Jander kfree(ctlr->dummy_tx); 3968ae7d2346SDavid Jander ctlr->dummy_tx = NULL; 3969ae7d2346SDavid Jander if (ctlr->unprepare_transfer_hardware && 3970ae7d2346SDavid Jander ctlr->unprepare_transfer_hardware(ctlr)) 3971ae7d2346SDavid Jander dev_err(&ctlr->dev, 3972ae7d2346SDavid Jander "failed to unprepare transfer hardware\n"); 3973ae7d2346SDavid Jander spi_idle_runtime_pm(ctlr); 3974ae7d2346SDavid Jander } 3975ae7d2346SDavid Jander 3976ae7d2346SDavid Jander out: 3977ae7d2346SDavid Jander mutex_unlock(&ctlr->io_mutex); 3978ae7d2346SDavid Jander } 3979ae7d2346SDavid Jander 39807d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 39817d077197SDavid Brownell 3982350de7ceSAndy Shevchenko /* 3983350de7ceSAndy Shevchenko * Utility methods for SPI protocol drivers, layered on 39847d077197SDavid Brownell * top of the core. Some other utility methods are defined as 39857d077197SDavid Brownell * inline functions. 39867d077197SDavid Brownell */ 39877d077197SDavid Brownell 39885d870c8eSAndrew Morton static void spi_complete(void *arg) 39895d870c8eSAndrew Morton { 39905d870c8eSAndrew Morton complete(arg); 39915d870c8eSAndrew Morton } 39925d870c8eSAndrew Morton 3993ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message) 3994cf32b71eSErnst Schwab { 3995cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 3996cf32b71eSErnst Schwab int status; 39978caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 39980461a414SMark Brown 39990461a414SMark Brown status = __spi_validate(spi, message); 40000461a414SMark Brown if (status != 0) 40010461a414SMark Brown return status; 4002cf32b71eSErnst Schwab 40030461a414SMark Brown message->spi = spi; 4004cf32b71eSErnst Schwab 40056598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync); 40066598b91bSDavid Jander SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); 4007eca2ebc7SMartin Sperl 4008350de7ceSAndy Shevchenko /* 4009ae7d2346SDavid Jander * Checking queue_empty here only guarantees async/sync message 4010ae7d2346SDavid Jander * ordering when coming from the same context. It does not need to 4011ae7d2346SDavid Jander * guard against reentrancy from a different context. The io_mutex 4012ae7d2346SDavid Jander * will catch those cases. 40130461a414SMark Brown */ 4014ae7d2346SDavid Jander if (READ_ONCE(ctlr->queue_empty)) { 4015ae7d2346SDavid Jander message->sync = true; 4016ae7d2346SDavid Jander message->actual_length = 0; 4017ae7d2346SDavid Jander message->status = -EINPROGRESS; 40180461a414SMark Brown 40190461a414SMark Brown trace_spi_message_submit(message); 40200461a414SMark Brown 4021ae7d2346SDavid Jander SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync_immediate); 4022ae7d2346SDavid Jander SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync_immediate); 40230461a414SMark Brown 4024ae7d2346SDavid Jander __spi_transfer_message_noqueue(ctlr, message); 4025ae7d2346SDavid Jander 4026ae7d2346SDavid Jander return message->status; 4027ae7d2346SDavid Jander } 4028ae7d2346SDavid Jander 4029ae7d2346SDavid Jander /* 4030ae7d2346SDavid Jander * There are messages in the async queue that could have originated 4031ae7d2346SDavid Jander * from the same context, so we need to preserve ordering. 4032ae7d2346SDavid Jander * Therefor we send the message to the async queue and wait until they 4033ae7d2346SDavid Jander * are completed. 4034ae7d2346SDavid Jander */ 4035ae7d2346SDavid Jander message->complete = spi_complete; 4036ae7d2346SDavid Jander message->context = &done; 4037cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 4038cf32b71eSErnst Schwab if (status == 0) { 4039cf32b71eSErnst Schwab wait_for_completion(&done); 4040cf32b71eSErnst Schwab status = message->status; 4041cf32b71eSErnst Schwab } 4042cf32b71eSErnst Schwab message->context = NULL; 4043ae7d2346SDavid Jander 4044cf32b71eSErnst Schwab return status; 4045cf32b71eSErnst Schwab } 4046cf32b71eSErnst Schwab 40478ae12a0dSDavid Brownell /** 40488ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 40498ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 40508ae12a0dSDavid Brownell * @message: describes the data transfers 405133e34dc6SDavid Brownell * Context: can sleep 40528ae12a0dSDavid Brownell * 40538ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 40548ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 40558ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 40568ae12a0dSDavid Brownell * 40578ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 40588ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 40598ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 40608ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 40618ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 40628ae12a0dSDavid Brownell * 40630c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 40640c868461SDavid Brownell * message will not be freed before this call returns. 40650c868461SDavid Brownell * 406697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 40678ae12a0dSDavid Brownell */ 40688ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 40698ae12a0dSDavid Brownell { 4070ef4d96ecSMark Brown int ret; 4071ef4d96ecSMark Brown 40728caab75fSGeert Uytterhoeven mutex_lock(&spi->controller->bus_lock_mutex); 4073ef4d96ecSMark Brown ret = __spi_sync(spi, message); 40748caab75fSGeert Uytterhoeven mutex_unlock(&spi->controller->bus_lock_mutex); 4075ef4d96ecSMark Brown 4076ef4d96ecSMark Brown return ret; 40778ae12a0dSDavid Brownell } 40788ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 40798ae12a0dSDavid Brownell 4080cf32b71eSErnst Schwab /** 4081cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 4082cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 4083cf32b71eSErnst Schwab * @message: describes the data transfers 4084cf32b71eSErnst Schwab * Context: can sleep 4085cf32b71eSErnst Schwab * 4086cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 4087cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 4088cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 4089cf32b71eSErnst Schwab * 4090cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 409125985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 4092cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 4093cf32b71eSErnst Schwab * 409497d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 4095cf32b71eSErnst Schwab */ 4096cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 4097cf32b71eSErnst Schwab { 4098ef4d96ecSMark Brown return __spi_sync(spi, message); 4099cf32b71eSErnst Schwab } 4100cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 4101cf32b71eSErnst Schwab 4102cf32b71eSErnst Schwab /** 4103cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 41048caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that should be locked for exclusive bus access 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. 4109cf32b71eSErnst Schwab * 4110cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 4111cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 4112cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 4113cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 4114cf32b71eSErnst Schwab * 411597d56dc6SJavier Martinez Canillas * Return: always zero. 4116cf32b71eSErnst Schwab */ 41178caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr) 4118cf32b71eSErnst Schwab { 4119cf32b71eSErnst Schwab unsigned long flags; 4120cf32b71eSErnst Schwab 41218caab75fSGeert Uytterhoeven mutex_lock(&ctlr->bus_lock_mutex); 4122cf32b71eSErnst Schwab 41238caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 41248caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 1; 41258caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 4126cf32b71eSErnst Schwab 4127cf32b71eSErnst Schwab /* mutex remains locked until spi_bus_unlock is called */ 4128cf32b71eSErnst Schwab 4129cf32b71eSErnst Schwab return 0; 4130cf32b71eSErnst Schwab } 4131cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 4132cf32b71eSErnst Schwab 4133cf32b71eSErnst Schwab /** 4134cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 41358caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that was locked for exclusive bus access 4136cf32b71eSErnst Schwab * Context: can sleep 4137cf32b71eSErnst Schwab * 4138cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 4139cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 4140cf32b71eSErnst Schwab * 4141cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 4142cf32b71eSErnst Schwab * call. 4143cf32b71eSErnst Schwab * 414497d56dc6SJavier Martinez Canillas * Return: always zero. 4145cf32b71eSErnst Schwab */ 41468caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr) 4147cf32b71eSErnst Schwab { 41488caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 4149cf32b71eSErnst Schwab 41508caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->bus_lock_mutex); 4151cf32b71eSErnst Schwab 4152cf32b71eSErnst Schwab return 0; 4153cf32b71eSErnst Schwab } 4154cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 4155cf32b71eSErnst Schwab 4156a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 4157a9948b61SDavid Brownell #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 41588ae12a0dSDavid Brownell 41598ae12a0dSDavid Brownell static u8 *buf; 41608ae12a0dSDavid Brownell 41618ae12a0dSDavid Brownell /** 41628ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 41638ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 41648ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 41658ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 416627570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 416727570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 416833e34dc6SDavid Brownell * Context: can sleep 41698ae12a0dSDavid Brownell * 41708ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 41718ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 41728ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 4173b885244eSDavid Brownell * This call may only be used from a context that may sleep. 41748ae12a0dSDavid Brownell * 4175c373643bSMark Brown * Parameters to this routine are always copied using a small buffer. 417633e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 41770c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 417897d56dc6SJavier Martinez Canillas * 417997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 41808ae12a0dSDavid Brownell */ 41818ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 41820c4a1590SMark Brown const void *txbuf, unsigned n_tx, 41830c4a1590SMark Brown void *rxbuf, unsigned n_rx) 41848ae12a0dSDavid Brownell { 4185068f4070SDavid Brownell static DEFINE_MUTEX(lock); 41868ae12a0dSDavid Brownell 41878ae12a0dSDavid Brownell int status; 41888ae12a0dSDavid Brownell struct spi_message message; 4189bdff549eSDavid Brownell struct spi_transfer x[2]; 41908ae12a0dSDavid Brownell u8 *local_buf; 41918ae12a0dSDavid Brownell 4192350de7ceSAndy Shevchenko /* 4193350de7ceSAndy Shevchenko * Use preallocated DMA-safe buffer if we can. We can't avoid 4194b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 4195b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 4196b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 41978ae12a0dSDavid Brownell */ 4198b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 41992cd94c8aSMark Brown local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 42002cd94c8aSMark Brown GFP_KERNEL | GFP_DMA); 4201b3a223eeSMark Brown if (!local_buf) 4202b3a223eeSMark Brown return -ENOMEM; 4203b3a223eeSMark Brown } else { 4204b3a223eeSMark Brown local_buf = buf; 4205b3a223eeSMark Brown } 42068ae12a0dSDavid Brownell 42078275c642SVitaly Wool spi_message_init(&message); 42085fe5f05eSJingoo Han memset(x, 0, sizeof(x)); 4209bdff549eSDavid Brownell if (n_tx) { 4210bdff549eSDavid Brownell x[0].len = n_tx; 4211bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 4212bdff549eSDavid Brownell } 4213bdff549eSDavid Brownell if (n_rx) { 4214bdff549eSDavid Brownell x[1].len = n_rx; 4215bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 4216bdff549eSDavid Brownell } 42178275c642SVitaly Wool 42188ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 4219bdff549eSDavid Brownell x[0].tx_buf = local_buf; 4220bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 42218ae12a0dSDavid Brownell 42228ae12a0dSDavid Brownell /* do the i/o */ 42238ae12a0dSDavid Brownell status = spi_sync(spi, &message); 42249b938b74SMarc Pignat if (status == 0) 4225bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 42268ae12a0dSDavid Brownell 4227bdff549eSDavid Brownell if (x[0].tx_buf == buf) 4228068f4070SDavid Brownell mutex_unlock(&lock); 42298ae12a0dSDavid Brownell else 42308ae12a0dSDavid Brownell kfree(local_buf); 42318ae12a0dSDavid Brownell 42328ae12a0dSDavid Brownell return status; 42338ae12a0dSDavid Brownell } 42348ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 42358ae12a0dSDavid Brownell 42368ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 42378ae12a0dSDavid Brownell 4238da21fde0SUwe Kleine-König #if IS_ENABLED(CONFIG_OF_DYNAMIC) 4239ce79d54aSPantelis Antoniou /* must call put_device() when done with returned spi_device device */ 4240da21fde0SUwe Kleine-König static struct spi_device *of_find_spi_device_by_node(struct device_node *node) 4241ce79d54aSPantelis Antoniou { 4242cfba5de9SSuzuki K Poulose struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); 4243cfba5de9SSuzuki K Poulose 4244ce79d54aSPantelis Antoniou return dev ? to_spi_device(dev) : NULL; 4245ce79d54aSPantelis Antoniou } 4246ce79d54aSPantelis Antoniou 42478caab75fSGeert Uytterhoeven /* the spi controllers are not using spi_bus, so we find it with another way */ 42488caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 4249ce79d54aSPantelis Antoniou { 4250ce79d54aSPantelis Antoniou struct device *dev; 4251ce79d54aSPantelis Antoniou 4252cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_master_class, node); 42536c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 4254cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_slave_class, node); 4255ce79d54aSPantelis Antoniou if (!dev) 4256ce79d54aSPantelis Antoniou return NULL; 4257ce79d54aSPantelis Antoniou 4258ce79d54aSPantelis Antoniou /* reference got in class_find_device */ 42598caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 4260ce79d54aSPantelis Antoniou } 4261ce79d54aSPantelis Antoniou 4262ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action, 4263ce79d54aSPantelis Antoniou void *arg) 4264ce79d54aSPantelis Antoniou { 4265ce79d54aSPantelis Antoniou struct of_reconfig_data *rd = arg; 42668caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 4267ce79d54aSPantelis Antoniou struct spi_device *spi; 4268ce79d54aSPantelis Antoniou 4269ce79d54aSPantelis Antoniou switch (of_reconfig_get_state_change(action, arg)) { 4270ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 42718caab75fSGeert Uytterhoeven ctlr = of_find_spi_controller_by_node(rd->dn->parent); 42728caab75fSGeert Uytterhoeven if (ctlr == NULL) 4273ce79d54aSPantelis Antoniou return NOTIFY_OK; /* not for us */ 4274ce79d54aSPantelis Antoniou 4275bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 42768caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4277bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4278bd6c1644SGeert Uytterhoeven } 4279bd6c1644SGeert Uytterhoeven 42808caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, rd->dn); 42818caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4282ce79d54aSPantelis Antoniou 4283ce79d54aSPantelis Antoniou if (IS_ERR(spi)) { 428425c56c88SRob Herring pr_err("%s: failed to create for '%pOF'\n", 428525c56c88SRob Herring __func__, rd->dn); 4286e0af98a7SRalf Ramsauer of_node_clear_flag(rd->dn, OF_POPULATED); 4287ce79d54aSPantelis Antoniou return notifier_from_errno(PTR_ERR(spi)); 4288ce79d54aSPantelis Antoniou } 4289ce79d54aSPantelis Antoniou break; 4290ce79d54aSPantelis Antoniou 4291ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 4292bd6c1644SGeert Uytterhoeven /* already depopulated? */ 4293bd6c1644SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 4294bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4295bd6c1644SGeert Uytterhoeven 4296ce79d54aSPantelis Antoniou /* find our device by node */ 4297ce79d54aSPantelis Antoniou spi = of_find_spi_device_by_node(rd->dn); 4298ce79d54aSPantelis Antoniou if (spi == NULL) 4299ce79d54aSPantelis Antoniou return NOTIFY_OK; /* no? not meant for us */ 4300ce79d54aSPantelis Antoniou 4301ce79d54aSPantelis Antoniou /* unregister takes one ref away */ 4302ce79d54aSPantelis Antoniou spi_unregister_device(spi); 4303ce79d54aSPantelis Antoniou 4304ce79d54aSPantelis Antoniou /* and put the reference of the find */ 4305ce79d54aSPantelis Antoniou put_device(&spi->dev); 4306ce79d54aSPantelis Antoniou break; 4307ce79d54aSPantelis Antoniou } 4308ce79d54aSPantelis Antoniou 4309ce79d54aSPantelis Antoniou return NOTIFY_OK; 4310ce79d54aSPantelis Antoniou } 4311ce79d54aSPantelis Antoniou 4312ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = { 4313ce79d54aSPantelis Antoniou .notifier_call = of_spi_notify, 4314ce79d54aSPantelis Antoniou }; 4315ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4316ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier; 4317ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4318ce79d54aSPantelis Antoniou 43197f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI) 43208caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data) 43217f24467fSOctavian Purdila { 43227f24467fSOctavian Purdila return ACPI_COMPANION(dev->parent) == data; 43237f24467fSOctavian Purdila } 43247f24467fSOctavian Purdila 43258caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 43267f24467fSOctavian Purdila { 43277f24467fSOctavian Purdila struct device *dev; 43287f24467fSOctavian Purdila 43297f24467fSOctavian Purdila dev = class_find_device(&spi_master_class, NULL, adev, 43308caab75fSGeert Uytterhoeven spi_acpi_controller_match); 43316c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 43326c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, adev, 43338caab75fSGeert Uytterhoeven spi_acpi_controller_match); 43347f24467fSOctavian Purdila if (!dev) 43357f24467fSOctavian Purdila return NULL; 43367f24467fSOctavian Purdila 43378caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 43387f24467fSOctavian Purdila } 43397f24467fSOctavian Purdila 43407f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 43417f24467fSOctavian Purdila { 43427f24467fSOctavian Purdila struct device *dev; 43437f24467fSOctavian Purdila 434400500147SSuzuki K Poulose dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev); 43455b16668eSWolfram Sang return to_spi_device(dev); 43467f24467fSOctavian Purdila } 43477f24467fSOctavian Purdila 43487f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 43497f24467fSOctavian Purdila void *arg) 43507f24467fSOctavian Purdila { 43517f24467fSOctavian Purdila struct acpi_device *adev = arg; 43528caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 43537f24467fSOctavian Purdila struct spi_device *spi; 43547f24467fSOctavian Purdila 43557f24467fSOctavian Purdila switch (value) { 43567f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_ADD: 43578caab75fSGeert Uytterhoeven ctlr = acpi_spi_find_controller_by_adev(adev->parent); 43588caab75fSGeert Uytterhoeven if (!ctlr) 43597f24467fSOctavian Purdila break; 43607f24467fSOctavian Purdila 43618caab75fSGeert Uytterhoeven acpi_register_spi_device(ctlr, adev); 43628caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 43637f24467fSOctavian Purdila break; 43647f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_REMOVE: 43657f24467fSOctavian Purdila if (!acpi_device_enumerated(adev)) 43667f24467fSOctavian Purdila break; 43677f24467fSOctavian Purdila 43687f24467fSOctavian Purdila spi = acpi_spi_find_device_by_adev(adev); 43697f24467fSOctavian Purdila if (!spi) 43707f24467fSOctavian Purdila break; 43717f24467fSOctavian Purdila 43727f24467fSOctavian Purdila spi_unregister_device(spi); 43737f24467fSOctavian Purdila put_device(&spi->dev); 43747f24467fSOctavian Purdila break; 43757f24467fSOctavian Purdila } 43767f24467fSOctavian Purdila 43777f24467fSOctavian Purdila return NOTIFY_OK; 43787f24467fSOctavian Purdila } 43797f24467fSOctavian Purdila 43807f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = { 43817f24467fSOctavian Purdila .notifier_call = acpi_spi_notify, 43827f24467fSOctavian Purdila }; 43837f24467fSOctavian Purdila #else 43847f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier; 43857f24467fSOctavian Purdila #endif 43867f24467fSOctavian Purdila 43878ae12a0dSDavid Brownell static int __init spi_init(void) 43888ae12a0dSDavid Brownell { 4389b885244eSDavid Brownell int status; 43908ae12a0dSDavid Brownell 4391e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 4392b885244eSDavid Brownell if (!buf) { 4393b885244eSDavid Brownell status = -ENOMEM; 4394b885244eSDavid Brownell goto err0; 43958ae12a0dSDavid Brownell } 4396b885244eSDavid Brownell 4397b885244eSDavid Brownell status = bus_register(&spi_bus_type); 4398b885244eSDavid Brownell if (status < 0) 4399b885244eSDavid Brownell goto err1; 4400b885244eSDavid Brownell 4401b885244eSDavid Brownell status = class_register(&spi_master_class); 4402b885244eSDavid Brownell if (status < 0) 4403b885244eSDavid Brownell goto err2; 4404ce79d54aSPantelis Antoniou 44056c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 44066c364062SGeert Uytterhoeven status = class_register(&spi_slave_class); 44076c364062SGeert Uytterhoeven if (status < 0) 44086c364062SGeert Uytterhoeven goto err3; 44096c364062SGeert Uytterhoeven } 44106c364062SGeert Uytterhoeven 44115267720eSFabio Estevam if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 4412ce79d54aSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 44137f24467fSOctavian Purdila if (IS_ENABLED(CONFIG_ACPI)) 44147f24467fSOctavian Purdila WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 4415ce79d54aSPantelis Antoniou 4416b885244eSDavid Brownell return 0; 4417b885244eSDavid Brownell 44186c364062SGeert Uytterhoeven err3: 44196c364062SGeert Uytterhoeven class_unregister(&spi_master_class); 4420b885244eSDavid Brownell err2: 4421b885244eSDavid Brownell bus_unregister(&spi_bus_type); 4422b885244eSDavid Brownell err1: 4423b885244eSDavid Brownell kfree(buf); 4424b885244eSDavid Brownell buf = NULL; 4425b885244eSDavid Brownell err0: 4426b885244eSDavid Brownell return status; 4427b885244eSDavid Brownell } 4428b885244eSDavid Brownell 4429350de7ceSAndy Shevchenko /* 4430350de7ceSAndy Shevchenko * A board_info is normally registered in arch_initcall(), 4431350de7ceSAndy Shevchenko * but even essential drivers wait till later. 4432b885244eSDavid Brownell * 4433350de7ceSAndy Shevchenko * REVISIT only boardinfo really needs static linking. The rest (device and 4434350de7ceSAndy Shevchenko * driver registration) _could_ be dynamically linked (modular) ... Costs 4435b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 44368ae12a0dSDavid Brownell */ 4437673c0c00SDavid Brownell postcore_initcall(spi_init); 4438