1b445bfcbSMarco Felsch // SPDX-License-Identifier: GPL-2.0-or-later 2787f4889SMark Brown // SPI init/core code 3787f4889SMark Brown // 4787f4889SMark Brown // Copyright (C) 2005 David Brownell 5787f4889SMark Brown // Copyright (C) 2008 Secret Lab Technologies Ltd. 68ae12a0dSDavid Brownell 78ae12a0dSDavid Brownell #include <linux/kernel.h> 88ae12a0dSDavid Brownell #include <linux/device.h> 98ae12a0dSDavid Brownell #include <linux/init.h> 108ae12a0dSDavid Brownell #include <linux/cache.h> 1199adef31SMark Brown #include <linux/dma-mapping.h> 1299adef31SMark Brown #include <linux/dmaengine.h> 1394040828SMatthias Kaehlcke #include <linux/mutex.h> 142b7a32f7SSinan Akman #include <linux/of_device.h> 15d57a4282SGrant Likely #include <linux/of_irq.h> 1686be408bSSylwester Nawrocki #include <linux/clk/clk-conf.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 18e0626e38SAnton Vorontsov #include <linux/mod_devicetable.h> 198ae12a0dSDavid Brownell #include <linux/spi/spi.h> 20b5932f5cSBoris Brezillon #include <linux/spi/spi-mem.h> 2174317984SJean-Christophe PLAGNIOL-VILLARD #include <linux/of_gpio.h> 22f3186dd8SLinus Walleij #include <linux/gpio/consumer.h> 233ae22e8cSMark Brown #include <linux/pm_runtime.h> 24f48c767cSUlf Hansson #include <linux/pm_domain.h> 25826cf175SDmitry Torokhov #include <linux/property.h> 26025ed130SPaul Gortmaker #include <linux/export.h> 278bd75c77SClark Williams #include <linux/sched/rt.h> 28ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h> 29ffbbdd21SLinus Walleij #include <linux/delay.h> 30ffbbdd21SLinus Walleij #include <linux/kthread.h> 3164bee4d2SMika Westerberg #include <linux/ioport.h> 3264bee4d2SMika Westerberg #include <linux/acpi.h> 33b1b8153cSVignesh R #include <linux/highmem.h> 349b61e302SSuniel Mahesh #include <linux/idr.h> 358a2e487eSLukas Wunner #include <linux/platform_data/x86/apple.h> 368ae12a0dSDavid Brownell 3756ec1978SMark Brown #define CREATE_TRACE_POINTS 3856ec1978SMark Brown #include <trace/events/spi.h> 39ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start); 40ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop); 419b61e302SSuniel Mahesh 4246336966SBoris Brezillon #include "internals.h" 4346336966SBoris Brezillon 449b61e302SSuniel Mahesh static DEFINE_IDR(spi_master_idr); 4556ec1978SMark Brown 468ae12a0dSDavid Brownell static void spidev_release(struct device *dev) 478ae12a0dSDavid Brownell { 480ffa0285SHans-Peter Nilsson struct spi_device *spi = to_spi_device(dev); 498ae12a0dSDavid Brownell 508caab75fSGeert Uytterhoeven /* spi controllers may cleanup for released devices */ 518caab75fSGeert Uytterhoeven if (spi->controller->cleanup) 528caab75fSGeert Uytterhoeven spi->controller->cleanup(spi); 538ae12a0dSDavid Brownell 548caab75fSGeert Uytterhoeven spi_controller_put(spi->controller); 555039563eSTrent Piepho kfree(spi->driver_override); 5607a389feSRoman Tereshonkov kfree(spi); 578ae12a0dSDavid Brownell } 588ae12a0dSDavid Brownell 598ae12a0dSDavid Brownell static ssize_t 608ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf) 618ae12a0dSDavid Brownell { 628ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 638c4ff6d0SZhang Rui int len; 648c4ff6d0SZhang Rui 658c4ff6d0SZhang Rui len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 668c4ff6d0SZhang Rui if (len != -ENODEV) 678c4ff6d0SZhang Rui return len; 688ae12a0dSDavid Brownell 69d8e328b3SGrant Likely return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 708ae12a0dSDavid Brownell } 71aa7da564SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias); 728ae12a0dSDavid Brownell 735039563eSTrent Piepho static ssize_t driver_override_store(struct device *dev, 745039563eSTrent Piepho struct device_attribute *a, 755039563eSTrent Piepho const char *buf, size_t count) 765039563eSTrent Piepho { 775039563eSTrent Piepho struct spi_device *spi = to_spi_device(dev); 785039563eSTrent Piepho const char *end = memchr(buf, '\n', count); 795039563eSTrent Piepho const size_t len = end ? end - buf : count; 805039563eSTrent Piepho const char *driver_override, *old; 815039563eSTrent Piepho 825039563eSTrent Piepho /* We need to keep extra room for a newline when displaying value */ 835039563eSTrent Piepho if (len >= (PAGE_SIZE - 1)) 845039563eSTrent Piepho return -EINVAL; 855039563eSTrent Piepho 865039563eSTrent Piepho driver_override = kstrndup(buf, len, GFP_KERNEL); 875039563eSTrent Piepho if (!driver_override) 885039563eSTrent Piepho return -ENOMEM; 895039563eSTrent Piepho 905039563eSTrent Piepho device_lock(dev); 915039563eSTrent Piepho old = spi->driver_override; 925039563eSTrent Piepho if (len) { 935039563eSTrent Piepho spi->driver_override = driver_override; 945039563eSTrent Piepho } else { 95be73e323SAndy Shevchenko /* Empty string, disable driver override */ 965039563eSTrent Piepho spi->driver_override = NULL; 975039563eSTrent Piepho kfree(driver_override); 985039563eSTrent Piepho } 995039563eSTrent Piepho device_unlock(dev); 1005039563eSTrent Piepho kfree(old); 1015039563eSTrent Piepho 1025039563eSTrent Piepho return count; 1035039563eSTrent Piepho } 1045039563eSTrent Piepho 1055039563eSTrent Piepho static ssize_t driver_override_show(struct device *dev, 1065039563eSTrent Piepho struct device_attribute *a, char *buf) 1075039563eSTrent Piepho { 1085039563eSTrent Piepho const struct spi_device *spi = to_spi_device(dev); 1095039563eSTrent Piepho ssize_t len; 1105039563eSTrent Piepho 1115039563eSTrent Piepho device_lock(dev); 1125039563eSTrent Piepho len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : ""); 1135039563eSTrent Piepho device_unlock(dev); 1145039563eSTrent Piepho return len; 1155039563eSTrent Piepho } 1165039563eSTrent Piepho static DEVICE_ATTR_RW(driver_override); 1175039563eSTrent Piepho 118eca2ebc7SMartin Sperl #define SPI_STATISTICS_ATTRS(field, file) \ 1198caab75fSGeert Uytterhoeven static ssize_t spi_controller_##field##_show(struct device *dev, \ 120eca2ebc7SMartin Sperl struct device_attribute *attr, \ 121eca2ebc7SMartin Sperl char *buf) \ 122eca2ebc7SMartin Sperl { \ 1238caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, \ 1248caab75fSGeert Uytterhoeven struct spi_controller, dev); \ 1258caab75fSGeert Uytterhoeven return spi_statistics_##field##_show(&ctlr->statistics, buf); \ 126eca2ebc7SMartin Sperl } \ 1278caab75fSGeert Uytterhoeven static struct device_attribute dev_attr_spi_controller_##field = { \ 128ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 1298caab75fSGeert Uytterhoeven .show = spi_controller_##field##_show, \ 130eca2ebc7SMartin Sperl }; \ 131eca2ebc7SMartin Sperl static ssize_t spi_device_##field##_show(struct device *dev, \ 132eca2ebc7SMartin Sperl struct device_attribute *attr, \ 133eca2ebc7SMartin Sperl char *buf) \ 134eca2ebc7SMartin Sperl { \ 135d1eba93bSGeliang Tang struct spi_device *spi = to_spi_device(dev); \ 136eca2ebc7SMartin Sperl return spi_statistics_##field##_show(&spi->statistics, buf); \ 137eca2ebc7SMartin Sperl } \ 138eca2ebc7SMartin Sperl static struct device_attribute dev_attr_spi_device_##field = { \ 139ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 140eca2ebc7SMartin Sperl .show = spi_device_##field##_show, \ 141eca2ebc7SMartin Sperl } 142eca2ebc7SMartin Sperl 143eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \ 144eca2ebc7SMartin Sperl static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ 145eca2ebc7SMartin Sperl char *buf) \ 146eca2ebc7SMartin Sperl { \ 147eca2ebc7SMartin Sperl unsigned long flags; \ 148eca2ebc7SMartin Sperl ssize_t len; \ 149eca2ebc7SMartin Sperl spin_lock_irqsave(&stat->lock, flags); \ 150eca2ebc7SMartin Sperl len = sprintf(buf, format_string, stat->field); \ 151eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stat->lock, flags); \ 152eca2ebc7SMartin Sperl return len; \ 153eca2ebc7SMartin Sperl } \ 154eca2ebc7SMartin Sperl SPI_STATISTICS_ATTRS(name, file) 155eca2ebc7SMartin Sperl 156eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW(field, format_string) \ 157eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ 158eca2ebc7SMartin Sperl field, format_string) 159eca2ebc7SMartin Sperl 160eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(messages, "%lu"); 161eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(transfers, "%lu"); 162eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(errors, "%lu"); 163eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(timedout, "%lu"); 164eca2ebc7SMartin Sperl 165eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync, "%lu"); 166eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu"); 167eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_async, "%lu"); 168eca2ebc7SMartin Sperl 169eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes, "%llu"); 170eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_rx, "%llu"); 171eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_tx, "%llu"); 172eca2ebc7SMartin Sperl 1736b7bc061SMartin Sperl #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ 1746b7bc061SMartin Sperl SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ 1756b7bc061SMartin Sperl "transfer_bytes_histo_" number, \ 1766b7bc061SMartin Sperl transfer_bytes_histo[index], "%lu") 1776b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); 1786b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); 1796b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); 1806b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); 1816b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); 1826b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); 1836b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); 1846b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); 1856b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); 1866b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); 1876b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); 1886b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); 1896b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); 1906b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); 1916b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); 1926b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); 1936b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); 1946b7bc061SMartin Sperl 195d9f12122SMartin Sperl SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu"); 196d9f12122SMartin Sperl 197aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = { 198aa7da564SGreg Kroah-Hartman &dev_attr_modalias.attr, 1995039563eSTrent Piepho &dev_attr_driver_override.attr, 200aa7da564SGreg Kroah-Hartman NULL, 2018ae12a0dSDavid Brownell }; 202eca2ebc7SMartin Sperl 203eca2ebc7SMartin Sperl static const struct attribute_group spi_dev_group = { 204eca2ebc7SMartin Sperl .attrs = spi_dev_attrs, 205eca2ebc7SMartin Sperl }; 206eca2ebc7SMartin Sperl 207eca2ebc7SMartin Sperl static struct attribute *spi_device_statistics_attrs[] = { 208eca2ebc7SMartin Sperl &dev_attr_spi_device_messages.attr, 209eca2ebc7SMartin Sperl &dev_attr_spi_device_transfers.attr, 210eca2ebc7SMartin Sperl &dev_attr_spi_device_errors.attr, 211eca2ebc7SMartin Sperl &dev_attr_spi_device_timedout.attr, 212eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync.attr, 213eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync_immediate.attr, 214eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_async.attr, 215eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes.attr, 216eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_rx.attr, 217eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_tx.attr, 2186b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo0.attr, 2196b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo1.attr, 2206b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo2.attr, 2216b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo3.attr, 2226b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo4.attr, 2236b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo5.attr, 2246b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo6.attr, 2256b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo7.attr, 2266b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo8.attr, 2276b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo9.attr, 2286b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo10.attr, 2296b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo11.attr, 2306b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo12.attr, 2316b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo13.attr, 2326b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo14.attr, 2336b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo15.attr, 2346b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo16.attr, 235d9f12122SMartin Sperl &dev_attr_spi_device_transfers_split_maxsize.attr, 236eca2ebc7SMartin Sperl NULL, 237eca2ebc7SMartin Sperl }; 238eca2ebc7SMartin Sperl 239eca2ebc7SMartin Sperl static const struct attribute_group spi_device_statistics_group = { 240eca2ebc7SMartin Sperl .name = "statistics", 241eca2ebc7SMartin Sperl .attrs = spi_device_statistics_attrs, 242eca2ebc7SMartin Sperl }; 243eca2ebc7SMartin Sperl 244eca2ebc7SMartin Sperl static const struct attribute_group *spi_dev_groups[] = { 245eca2ebc7SMartin Sperl &spi_dev_group, 246eca2ebc7SMartin Sperl &spi_device_statistics_group, 247eca2ebc7SMartin Sperl NULL, 248eca2ebc7SMartin Sperl }; 249eca2ebc7SMartin Sperl 2508caab75fSGeert Uytterhoeven static struct attribute *spi_controller_statistics_attrs[] = { 2518caab75fSGeert Uytterhoeven &dev_attr_spi_controller_messages.attr, 2528caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers.attr, 2538caab75fSGeert Uytterhoeven &dev_attr_spi_controller_errors.attr, 2548caab75fSGeert Uytterhoeven &dev_attr_spi_controller_timedout.attr, 2558caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync.attr, 2568caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync_immediate.attr, 2578caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_async.attr, 2588caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes.attr, 2598caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_rx.attr, 2608caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_tx.attr, 2618caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo0.attr, 2628caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo1.attr, 2638caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo2.attr, 2648caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo3.attr, 2658caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo4.attr, 2668caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo5.attr, 2678caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo6.attr, 2688caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo7.attr, 2698caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo8.attr, 2708caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo9.attr, 2718caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo10.attr, 2728caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo11.attr, 2738caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo12.attr, 2748caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo13.attr, 2758caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo14.attr, 2768caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo15.attr, 2778caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo16.attr, 2788caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers_split_maxsize.attr, 279eca2ebc7SMartin Sperl NULL, 280eca2ebc7SMartin Sperl }; 281eca2ebc7SMartin Sperl 2828caab75fSGeert Uytterhoeven static const struct attribute_group spi_controller_statistics_group = { 283eca2ebc7SMartin Sperl .name = "statistics", 2848caab75fSGeert Uytterhoeven .attrs = spi_controller_statistics_attrs, 285eca2ebc7SMartin Sperl }; 286eca2ebc7SMartin Sperl 287eca2ebc7SMartin Sperl static const struct attribute_group *spi_master_groups[] = { 2888caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 289eca2ebc7SMartin Sperl NULL, 290eca2ebc7SMartin Sperl }; 291eca2ebc7SMartin Sperl 292eca2ebc7SMartin Sperl void spi_statistics_add_transfer_stats(struct spi_statistics *stats, 293eca2ebc7SMartin Sperl struct spi_transfer *xfer, 2948caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 295eca2ebc7SMartin Sperl { 296eca2ebc7SMartin Sperl unsigned long flags; 2976b7bc061SMartin Sperl int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; 2986b7bc061SMartin Sperl 2996b7bc061SMartin Sperl if (l2len < 0) 3006b7bc061SMartin Sperl l2len = 0; 301eca2ebc7SMartin Sperl 302eca2ebc7SMartin Sperl spin_lock_irqsave(&stats->lock, flags); 303eca2ebc7SMartin Sperl 304eca2ebc7SMartin Sperl stats->transfers++; 3056b7bc061SMartin Sperl stats->transfer_bytes_histo[l2len]++; 306eca2ebc7SMartin Sperl 307eca2ebc7SMartin Sperl stats->bytes += xfer->len; 308eca2ebc7SMartin Sperl if ((xfer->tx_buf) && 3098caab75fSGeert Uytterhoeven (xfer->tx_buf != ctlr->dummy_tx)) 310eca2ebc7SMartin Sperl stats->bytes_tx += xfer->len; 311eca2ebc7SMartin Sperl if ((xfer->rx_buf) && 3128caab75fSGeert Uytterhoeven (xfer->rx_buf != ctlr->dummy_rx)) 313eca2ebc7SMartin Sperl stats->bytes_rx += xfer->len; 314eca2ebc7SMartin Sperl 315eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stats->lock, flags); 316eca2ebc7SMartin Sperl } 317eca2ebc7SMartin Sperl EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats); 3188ae12a0dSDavid Brownell 3198ae12a0dSDavid Brownell /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, 3208ae12a0dSDavid Brownell * and the sysfs version makes coldplug work too. 3218ae12a0dSDavid Brownell */ 3228ae12a0dSDavid Brownell 32375368bf6SAnton Vorontsov static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, 32475368bf6SAnton Vorontsov const struct spi_device *sdev) 32575368bf6SAnton Vorontsov { 32675368bf6SAnton Vorontsov while (id->name[0]) { 32775368bf6SAnton Vorontsov if (!strcmp(sdev->modalias, id->name)) 32875368bf6SAnton Vorontsov return id; 32975368bf6SAnton Vorontsov id++; 33075368bf6SAnton Vorontsov } 33175368bf6SAnton Vorontsov return NULL; 33275368bf6SAnton Vorontsov } 33375368bf6SAnton Vorontsov 33475368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 33575368bf6SAnton Vorontsov { 33675368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 33775368bf6SAnton Vorontsov 33875368bf6SAnton Vorontsov return spi_match_id(sdrv->id_table, sdev); 33975368bf6SAnton Vorontsov } 34075368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id); 34175368bf6SAnton Vorontsov 3428ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 3438ae12a0dSDavid Brownell { 3448ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 34575368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(drv); 34675368bf6SAnton Vorontsov 3475039563eSTrent Piepho /* Check override first, and if set, only use the named driver */ 3485039563eSTrent Piepho if (spi->driver_override) 3495039563eSTrent Piepho return strcmp(spi->driver_override, drv->name) == 0; 3505039563eSTrent Piepho 3512b7a32f7SSinan Akman /* Attempt an OF style match */ 3522b7a32f7SSinan Akman if (of_driver_match_device(dev, drv)) 3532b7a32f7SSinan Akman return 1; 3542b7a32f7SSinan Akman 35564bee4d2SMika Westerberg /* Then try ACPI */ 35664bee4d2SMika Westerberg if (acpi_driver_match_device(dev, drv)) 35764bee4d2SMika Westerberg return 1; 35864bee4d2SMika Westerberg 35975368bf6SAnton Vorontsov if (sdrv->id_table) 36075368bf6SAnton Vorontsov return !!spi_match_id(sdrv->id_table, spi); 3618ae12a0dSDavid Brownell 36235f74fcaSKay Sievers return strcmp(spi->modalias, drv->name) == 0; 3638ae12a0dSDavid Brownell } 3648ae12a0dSDavid Brownell 3657eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 3668ae12a0dSDavid Brownell { 3678ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 3688c4ff6d0SZhang Rui int rc; 3698c4ff6d0SZhang Rui 3708c4ff6d0SZhang Rui rc = acpi_device_uevent_modalias(dev, env); 3718c4ff6d0SZhang Rui if (rc != -ENODEV) 3728c4ff6d0SZhang Rui return rc; 3738ae12a0dSDavid Brownell 3742856670fSAndy Shevchenko return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 3758ae12a0dSDavid Brownell } 3768ae12a0dSDavid Brownell 3778ae12a0dSDavid Brownell struct bus_type spi_bus_type = { 3788ae12a0dSDavid Brownell .name = "spi", 379aa7da564SGreg Kroah-Hartman .dev_groups = spi_dev_groups, 3808ae12a0dSDavid Brownell .match = spi_match_device, 3818ae12a0dSDavid Brownell .uevent = spi_uevent, 3828ae12a0dSDavid Brownell }; 3838ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_bus_type); 3848ae12a0dSDavid Brownell 385b885244eSDavid Brownell 386b885244eSDavid Brownell static int spi_drv_probe(struct device *dev) 387b885244eSDavid Brownell { 388b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 38944af7927SJon Hunter struct spi_device *spi = to_spi_device(dev); 39033cf00e5SMika Westerberg int ret; 391b885244eSDavid Brownell 39286be408bSSylwester Nawrocki ret = of_clk_set_defaults(dev->of_node, false); 39386be408bSSylwester Nawrocki if (ret) 39486be408bSSylwester Nawrocki return ret; 39586be408bSSylwester Nawrocki 39644af7927SJon Hunter if (dev->of_node) { 39744af7927SJon Hunter spi->irq = of_irq_get(dev->of_node, 0); 39844af7927SJon Hunter if (spi->irq == -EPROBE_DEFER) 39944af7927SJon Hunter return -EPROBE_DEFER; 40044af7927SJon Hunter if (spi->irq < 0) 40144af7927SJon Hunter spi->irq = 0; 40244af7927SJon Hunter } 40344af7927SJon Hunter 404676e7c25SUlf Hansson ret = dev_pm_domain_attach(dev, true); 40571f277a7SUlf Hansson if (ret) 40671f277a7SUlf Hansson return ret; 40771f277a7SUlf Hansson 408*440408dbSUwe Kleine-König if (sdrv->probe) { 40944af7927SJon Hunter ret = sdrv->probe(spi); 41033cf00e5SMika Westerberg if (ret) 411676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 412*440408dbSUwe Kleine-König } 41333cf00e5SMika Westerberg 41433cf00e5SMika Westerberg return ret; 415b885244eSDavid Brownell } 416b885244eSDavid Brownell 417b885244eSDavid Brownell static int spi_drv_remove(struct device *dev) 418b885244eSDavid Brownell { 419b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 420*440408dbSUwe Kleine-König int ret = 0; 421b885244eSDavid Brownell 422*440408dbSUwe Kleine-König if (sdrv->remove) 423aec35f4eSJean Delvare ret = sdrv->remove(to_spi_device(dev)); 424676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 42533cf00e5SMika Westerberg 42633cf00e5SMika Westerberg return ret; 427b885244eSDavid Brownell } 428b885244eSDavid Brownell 429b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev) 430b885244eSDavid Brownell { 431b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 432b885244eSDavid Brownell 433b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 434b885244eSDavid Brownell } 435b885244eSDavid Brownell 43633e34dc6SDavid Brownell /** 437ca5d2485SAndrew F. Davis * __spi_register_driver - register a SPI driver 43888c9321dSThierry Reding * @owner: owner module of the driver to register 43933e34dc6SDavid Brownell * @sdrv: the driver to register 44033e34dc6SDavid Brownell * Context: can sleep 44197d56dc6SJavier Martinez Canillas * 44297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 44333e34dc6SDavid Brownell */ 444ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) 445b885244eSDavid Brownell { 446ca5d2485SAndrew F. Davis sdrv->driver.owner = owner; 447b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 448b885244eSDavid Brownell sdrv->driver.probe = spi_drv_probe; 449b885244eSDavid Brownell sdrv->driver.remove = spi_drv_remove; 450b885244eSDavid Brownell if (sdrv->shutdown) 451b885244eSDavid Brownell sdrv->driver.shutdown = spi_drv_shutdown; 452b885244eSDavid Brownell return driver_register(&sdrv->driver); 453b885244eSDavid Brownell } 454ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver); 455b885244eSDavid Brownell 4568ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 4578ae12a0dSDavid Brownell 4588ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that 4598caab75fSGeert Uytterhoeven * would make them board-specific. Similarly with SPI controller drivers. 4608ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 4618ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 4628ae12a0dSDavid Brownell */ 4638ae12a0dSDavid Brownell 4648ae12a0dSDavid Brownell struct boardinfo { 4658ae12a0dSDavid Brownell struct list_head list; 4662b9603a0SFeng Tang struct spi_board_info board_info; 4678ae12a0dSDavid Brownell }; 4688ae12a0dSDavid Brownell 4698ae12a0dSDavid Brownell static LIST_HEAD(board_list); 4708caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list); 4712b9603a0SFeng Tang 4722b9603a0SFeng Tang /* 473be73e323SAndy Shevchenko * Used to protect add/del operation for board_info list and 4748caab75fSGeert Uytterhoeven * spi_controller list, and their matching process 4759b61e302SSuniel Mahesh * also used to protect object of type struct idr 4762b9603a0SFeng Tang */ 47794040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 4788ae12a0dSDavid Brownell 479ddf75be4SLukas Wunner /* 480ddf75be4SLukas Wunner * Prevents addition of devices with same chip select and 481ddf75be4SLukas Wunner * addition of devices below an unregistering controller. 482ddf75be4SLukas Wunner */ 483ddf75be4SLukas Wunner static DEFINE_MUTEX(spi_add_lock); 484ddf75be4SLukas Wunner 485dc87c98eSGrant Likely /** 486dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 4878caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 488dc87c98eSGrant Likely * Context: can sleep 489dc87c98eSGrant Likely * 490dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 491dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 492dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 493dc87c98eSGrant Likely * spi_add_device() on it. 494dc87c98eSGrant Likely * 495dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 4968caab75fSGeert Uytterhoeven * spi_device structure to add it to the SPI controller. If the caller 497dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 498dc87c98eSGrant Likely * call spi_dev_put() on it. 499dc87c98eSGrant Likely * 50097d56dc6SJavier Martinez Canillas * Return: a pointer to the new device, or NULL. 501dc87c98eSGrant Likely */ 5028caab75fSGeert Uytterhoeven struct spi_device *spi_alloc_device(struct spi_controller *ctlr) 503dc87c98eSGrant Likely { 504dc87c98eSGrant Likely struct spi_device *spi; 505dc87c98eSGrant Likely 5068caab75fSGeert Uytterhoeven if (!spi_controller_get(ctlr)) 507dc87c98eSGrant Likely return NULL; 508dc87c98eSGrant Likely 5095fe5f05eSJingoo Han spi = kzalloc(sizeof(*spi), GFP_KERNEL); 510dc87c98eSGrant Likely if (!spi) { 5118caab75fSGeert Uytterhoeven spi_controller_put(ctlr); 512dc87c98eSGrant Likely return NULL; 513dc87c98eSGrant Likely } 514dc87c98eSGrant Likely 5158caab75fSGeert Uytterhoeven spi->master = spi->controller = ctlr; 5168caab75fSGeert Uytterhoeven spi->dev.parent = &ctlr->dev; 517dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 518dc87c98eSGrant Likely spi->dev.release = spidev_release; 519446411e1SAndreas Larsson spi->cs_gpio = -ENOENT; 520ea235786SJohn Garry spi->mode = ctlr->buswidth_override_bits; 521eca2ebc7SMartin Sperl 522eca2ebc7SMartin Sperl spin_lock_init(&spi->statistics.lock); 523eca2ebc7SMartin Sperl 524dc87c98eSGrant Likely device_initialize(&spi->dev); 525dc87c98eSGrant Likely return spi; 526dc87c98eSGrant Likely } 527dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device); 528dc87c98eSGrant Likely 529e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi) 530e13ac47bSJarkko Nikula { 531e13ac47bSJarkko Nikula struct acpi_device *adev = ACPI_COMPANION(&spi->dev); 532e13ac47bSJarkko Nikula 533e13ac47bSJarkko Nikula if (adev) { 534e13ac47bSJarkko Nikula dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); 535e13ac47bSJarkko Nikula return; 536e13ac47bSJarkko Nikula } 537e13ac47bSJarkko Nikula 5388caab75fSGeert Uytterhoeven dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), 539e13ac47bSJarkko Nikula spi->chip_select); 540e13ac47bSJarkko Nikula } 541e13ac47bSJarkko Nikula 542b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data) 543b6fb8d3aSMika Westerberg { 544b6fb8d3aSMika Westerberg struct spi_device *spi = to_spi_device(dev); 545b6fb8d3aSMika Westerberg struct spi_device *new_spi = data; 546b6fb8d3aSMika Westerberg 5478caab75fSGeert Uytterhoeven if (spi->controller == new_spi->controller && 548b6fb8d3aSMika Westerberg spi->chip_select == new_spi->chip_select) 549b6fb8d3aSMika Westerberg return -EBUSY; 550b6fb8d3aSMika Westerberg return 0; 551b6fb8d3aSMika Westerberg } 552b6fb8d3aSMika Westerberg 553dc87c98eSGrant Likely /** 554dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 555dc87c98eSGrant Likely * @spi: spi_device to register 556dc87c98eSGrant Likely * 557dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 558dc87c98eSGrant Likely * spi_alloc_device can be added onto the spi bus with this function. 559dc87c98eSGrant Likely * 56097d56dc6SJavier Martinez Canillas * Return: 0 on success; negative errno on failure 561dc87c98eSGrant Likely */ 562dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi) 563dc87c98eSGrant Likely { 5648caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 5658caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 566dc87c98eSGrant Likely int status; 567dc87c98eSGrant Likely 568dc87c98eSGrant Likely /* Chipselects are numbered 0..max; validate. */ 5698caab75fSGeert Uytterhoeven if (spi->chip_select >= ctlr->num_chipselect) { 5708caab75fSGeert Uytterhoeven dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 5718caab75fSGeert Uytterhoeven ctlr->num_chipselect); 572dc87c98eSGrant Likely return -EINVAL; 573dc87c98eSGrant Likely } 574dc87c98eSGrant Likely 575dc87c98eSGrant Likely /* Set the bus ID string */ 576e13ac47bSJarkko Nikula spi_dev_set_name(spi); 577e48880e0SDavid Brownell 578e48880e0SDavid Brownell /* We need to make sure there's no other device with this 579e48880e0SDavid Brownell * chipselect **BEFORE** we call setup(), else we'll trash 580e48880e0SDavid Brownell * its configuration. Lock against concurrent add() calls. 581e48880e0SDavid Brownell */ 582e48880e0SDavid Brownell mutex_lock(&spi_add_lock); 583e48880e0SDavid Brownell 584b6fb8d3aSMika Westerberg status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); 585b6fb8d3aSMika Westerberg if (status) { 586e48880e0SDavid Brownell dev_err(dev, "chipselect %d already in use\n", 587e48880e0SDavid Brownell spi->chip_select); 588e48880e0SDavid Brownell goto done; 589e48880e0SDavid Brownell } 590e48880e0SDavid Brownell 591ddf75be4SLukas Wunner /* Controller may unregister concurrently */ 592ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC) && 593ddf75be4SLukas Wunner !device_is_registered(&ctlr->dev)) { 594ddf75be4SLukas Wunner status = -ENODEV; 595ddf75be4SLukas Wunner goto done; 596ddf75be4SLukas Wunner } 597ddf75be4SLukas Wunner 598f3186dd8SLinus Walleij /* Descriptors take precedence */ 599f3186dd8SLinus Walleij if (ctlr->cs_gpiods) 600f3186dd8SLinus Walleij spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; 601f3186dd8SLinus Walleij else if (ctlr->cs_gpios) 6028caab75fSGeert Uytterhoeven spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; 60374317984SJean-Christophe PLAGNIOL-VILLARD 604e48880e0SDavid Brownell /* Drivers may modify this initial i/o setup, but will 605e48880e0SDavid Brownell * normally rely on the device being setup. Devices 606e48880e0SDavid Brownell * using SPI_CS_HIGH can't coexist well otherwise... 607e48880e0SDavid Brownell */ 6087d077197SDavid Brownell status = spi_setup(spi); 609dc87c98eSGrant Likely if (status < 0) { 610eb288a1fSLinus Walleij dev_err(dev, "can't setup %s, status %d\n", 611eb288a1fSLinus Walleij dev_name(&spi->dev), status); 612e48880e0SDavid Brownell goto done; 613dc87c98eSGrant Likely } 614dc87c98eSGrant Likely 615e48880e0SDavid Brownell /* Device may be bound to an active driver when this returns */ 616dc87c98eSGrant Likely status = device_add(&spi->dev); 617e48880e0SDavid Brownell if (status < 0) 618eb288a1fSLinus Walleij dev_err(dev, "can't add %s, status %d\n", 619eb288a1fSLinus Walleij dev_name(&spi->dev), status); 620e48880e0SDavid Brownell else 62135f74fcaSKay Sievers dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 622e48880e0SDavid Brownell 623e48880e0SDavid Brownell done: 624e48880e0SDavid Brownell mutex_unlock(&spi_add_lock); 625e48880e0SDavid Brownell return status; 626dc87c98eSGrant Likely } 627dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device); 6288ae12a0dSDavid Brownell 62933e34dc6SDavid Brownell /** 63033e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 6318caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 63233e34dc6SDavid Brownell * @chip: Describes the SPI device 63333e34dc6SDavid Brownell * Context: can sleep 63433e34dc6SDavid Brownell * 63533e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 6368ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 6378ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 6388ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 6398ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 640082c8cb4SDavid Brownell * 64197d56dc6SJavier Martinez Canillas * Return: the new device, or NULL. 6428ae12a0dSDavid Brownell */ 6438caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr, 644e9d5a461SAdrian Bunk struct spi_board_info *chip) 6458ae12a0dSDavid Brownell { 6468ae12a0dSDavid Brownell struct spi_device *proxy; 6478ae12a0dSDavid Brownell int status; 6488ae12a0dSDavid Brownell 649082c8cb4SDavid Brownell /* NOTE: caller did any chip->bus_num checks necessary. 650082c8cb4SDavid Brownell * 651082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 652082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 653082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 654082c8cb4SDavid Brownell */ 655082c8cb4SDavid Brownell 6568caab75fSGeert Uytterhoeven proxy = spi_alloc_device(ctlr); 657dc87c98eSGrant Likely if (!proxy) 6588ae12a0dSDavid Brownell return NULL; 6598ae12a0dSDavid Brownell 660102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 661102eb975SGrant Likely 6628ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 6638ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 664980a01c9SDavid Brownell proxy->mode = chip->mode; 6658ae12a0dSDavid Brownell proxy->irq = chip->irq; 666102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 6678ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 6688ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 6698ae12a0dSDavid Brownell proxy->controller_state = NULL; 6708ae12a0dSDavid Brownell 671826cf175SDmitry Torokhov if (chip->properties) { 672826cf175SDmitry Torokhov status = device_add_properties(&proxy->dev, chip->properties); 673826cf175SDmitry Torokhov if (status) { 6748caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 675826cf175SDmitry Torokhov "failed to add properties to '%s': %d\n", 676826cf175SDmitry Torokhov chip->modalias, status); 677826cf175SDmitry Torokhov goto err_dev_put; 678826cf175SDmitry Torokhov } 6798ae12a0dSDavid Brownell } 680dc87c98eSGrant Likely 681826cf175SDmitry Torokhov status = spi_add_device(proxy); 682826cf175SDmitry Torokhov if (status < 0) 683826cf175SDmitry Torokhov goto err_remove_props; 684826cf175SDmitry Torokhov 685dc87c98eSGrant Likely return proxy; 686826cf175SDmitry Torokhov 687826cf175SDmitry Torokhov err_remove_props: 688826cf175SDmitry Torokhov if (chip->properties) 689826cf175SDmitry Torokhov device_remove_properties(&proxy->dev); 690826cf175SDmitry Torokhov err_dev_put: 691826cf175SDmitry Torokhov spi_dev_put(proxy); 692826cf175SDmitry Torokhov return NULL; 693dc87c98eSGrant Likely } 6948ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 6958ae12a0dSDavid Brownell 6963b1884c2SGeert Uytterhoeven /** 6973b1884c2SGeert Uytterhoeven * spi_unregister_device - unregister a single SPI device 6983b1884c2SGeert Uytterhoeven * @spi: spi_device to unregister 6993b1884c2SGeert Uytterhoeven * 7003b1884c2SGeert Uytterhoeven * Start making the passed SPI device vanish. Normally this would be handled 7018caab75fSGeert Uytterhoeven * by spi_unregister_controller(). 7023b1884c2SGeert Uytterhoeven */ 7033b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi) 7043b1884c2SGeert Uytterhoeven { 705bd6c1644SGeert Uytterhoeven if (!spi) 706bd6c1644SGeert Uytterhoeven return; 707bd6c1644SGeert Uytterhoeven 7088324147fSJohan Hovold if (spi->dev.of_node) { 709bd6c1644SGeert Uytterhoeven of_node_clear_flag(spi->dev.of_node, OF_POPULATED); 7108324147fSJohan Hovold of_node_put(spi->dev.of_node); 7118324147fSJohan Hovold } 7127f24467fSOctavian Purdila if (ACPI_COMPANION(&spi->dev)) 7137f24467fSOctavian Purdila acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); 7143b1884c2SGeert Uytterhoeven device_unregister(&spi->dev); 7153b1884c2SGeert Uytterhoeven } 7163b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device); 7173b1884c2SGeert Uytterhoeven 7188caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, 7192b9603a0SFeng Tang struct spi_board_info *bi) 7202b9603a0SFeng Tang { 7212b9603a0SFeng Tang struct spi_device *dev; 7222b9603a0SFeng Tang 7238caab75fSGeert Uytterhoeven if (ctlr->bus_num != bi->bus_num) 7242b9603a0SFeng Tang return; 7252b9603a0SFeng Tang 7268caab75fSGeert Uytterhoeven dev = spi_new_device(ctlr, bi); 7272b9603a0SFeng Tang if (!dev) 7288caab75fSGeert Uytterhoeven dev_err(ctlr->dev.parent, "can't create new device for %s\n", 7292b9603a0SFeng Tang bi->modalias); 7302b9603a0SFeng Tang } 7312b9603a0SFeng Tang 73233e34dc6SDavid Brownell /** 73333e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 73433e34dc6SDavid Brownell * @info: array of chip descriptors 73533e34dc6SDavid Brownell * @n: how many descriptors are provided 73633e34dc6SDavid Brownell * Context: can sleep 73733e34dc6SDavid Brownell * 7388ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 7398ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 7408ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 7418ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 7428ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 7438ae12a0dSDavid Brownell * 7448ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 7458ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 7468ae12a0dSDavid Brownell * would naturally declare its SPI devices. 7478ae12a0dSDavid Brownell * 7488ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 7498ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 750826cf175SDmitry Torokhov * Device properties are deep-copied though. 75197d56dc6SJavier Martinez Canillas * 75297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 7538ae12a0dSDavid Brownell */ 754fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n) 7558ae12a0dSDavid Brownell { 7568ae12a0dSDavid Brownell struct boardinfo *bi; 7572b9603a0SFeng Tang int i; 7588ae12a0dSDavid Brownell 759c7908a37SXiubo Li if (!n) 760f974cf57SDmitry Torokhov return 0; 761c7908a37SXiubo Li 762f9bdb7fdSMarkus Elfring bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); 7638ae12a0dSDavid Brownell if (!bi) 7648ae12a0dSDavid Brownell return -ENOMEM; 7658ae12a0dSDavid Brownell 7662b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 7678caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 7682b9603a0SFeng Tang 7692b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 770826cf175SDmitry Torokhov if (info->properties) { 771826cf175SDmitry Torokhov bi->board_info.properties = 772826cf175SDmitry Torokhov property_entries_dup(info->properties); 773826cf175SDmitry Torokhov if (IS_ERR(bi->board_info.properties)) 774826cf175SDmitry Torokhov return PTR_ERR(bi->board_info.properties); 775826cf175SDmitry Torokhov } 776826cf175SDmitry Torokhov 77794040828SMatthias Kaehlcke mutex_lock(&board_lock); 7788ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 7798caab75fSGeert Uytterhoeven list_for_each_entry(ctlr, &spi_controller_list, list) 7808caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, 7818caab75fSGeert Uytterhoeven &bi->board_info); 78294040828SMatthias Kaehlcke mutex_unlock(&board_lock); 7832b9603a0SFeng Tang } 7842b9603a0SFeng Tang 7858ae12a0dSDavid Brownell return 0; 7868ae12a0dSDavid Brownell } 7878ae12a0dSDavid Brownell 7888ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 7898ae12a0dSDavid Brownell 790b158935fSMark Brown static void spi_set_cs(struct spi_device *spi, bool enable) 791b158935fSMark Brown { 79225093bdeSAlexandru Ardelean bool enable1 = enable; 79325093bdeSAlexandru Ardelean 794d40f0b6fSDouglas Anderson /* 795d40f0b6fSDouglas Anderson * Avoid calling into the driver (or doing delays) if the chip select 796d40f0b6fSDouglas Anderson * isn't actually changing from the last time this was called. 797d40f0b6fSDouglas Anderson */ 798d40f0b6fSDouglas Anderson if ((spi->controller->last_cs_enable == enable) && 799d40f0b6fSDouglas Anderson (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) 800d40f0b6fSDouglas Anderson return; 801d40f0b6fSDouglas Anderson 802d40f0b6fSDouglas Anderson spi->controller->last_cs_enable = enable; 803d40f0b6fSDouglas Anderson spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; 804d40f0b6fSDouglas Anderson 80525093bdeSAlexandru Ardelean if (!spi->controller->set_cs_timing) { 80625093bdeSAlexandru Ardelean if (enable1) 80725093bdeSAlexandru Ardelean spi_delay_exec(&spi->controller->cs_setup, NULL); 80825093bdeSAlexandru Ardelean else 80925093bdeSAlexandru Ardelean spi_delay_exec(&spi->controller->cs_hold, NULL); 81025093bdeSAlexandru Ardelean } 81125093bdeSAlexandru Ardelean 812b158935fSMark Brown if (spi->mode & SPI_CS_HIGH) 813b158935fSMark Brown enable = !enable; 814b158935fSMark Brown 815f3186dd8SLinus Walleij if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { 816f3186dd8SLinus Walleij /* 817f3186dd8SLinus Walleij * Honour the SPI_NO_CS flag and invert the enable line, as 818f3186dd8SLinus Walleij * active low is default for SPI. Execution paths that handle 819f3186dd8SLinus Walleij * polarity inversion in gpiolib (such as device tree) will 820f3186dd8SLinus Walleij * enforce active high using the SPI_CS_HIGH resulting in a 821f3186dd8SLinus Walleij * double inversion through the code above. 822f3186dd8SLinus Walleij */ 823f3186dd8SLinus Walleij if (!(spi->mode & SPI_NO_CS)) { 824f3186dd8SLinus Walleij if (spi->cs_gpiod) 82528f7604fSFelix Fietkau gpiod_set_value_cansleep(spi->cs_gpiod, 82628f7604fSFelix Fietkau !enable); 827f3186dd8SLinus Walleij else 82828f7604fSFelix Fietkau gpio_set_value_cansleep(spi->cs_gpio, !enable); 829f3186dd8SLinus Walleij } 8308eee6b9dSThor Thayer /* Some SPI masters need both GPIO CS & slave_select */ 8318caab75fSGeert Uytterhoeven if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && 8328caab75fSGeert Uytterhoeven spi->controller->set_cs) 8338caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 8348caab75fSGeert Uytterhoeven } else if (spi->controller->set_cs) { 8358caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 8368eee6b9dSThor Thayer } 83725093bdeSAlexandru Ardelean 83825093bdeSAlexandru Ardelean if (!spi->controller->set_cs_timing) { 83925093bdeSAlexandru Ardelean if (!enable1) 84025093bdeSAlexandru Ardelean spi_delay_exec(&spi->controller->cs_inactive, NULL); 84125093bdeSAlexandru Ardelean } 842b158935fSMark Brown } 843b158935fSMark Brown 8442de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA 84546336966SBoris Brezillon int spi_map_buf(struct spi_controller *ctlr, struct device *dev, 8466ad45a27SMark Brown struct sg_table *sgt, void *buf, size_t len, 8476ad45a27SMark Brown enum dma_data_direction dir) 8486ad45a27SMark Brown { 8496ad45a27SMark Brown const bool vmalloced_buf = is_vmalloc_addr(buf); 850df88e91bSAndy Shevchenko unsigned int max_seg_size = dma_get_max_seg_size(dev); 851b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM 852b1b8153cSVignesh R const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && 853b1b8153cSVignesh R (unsigned long)buf < (PKMAP_BASE + 854b1b8153cSVignesh R (LAST_PKMAP * PAGE_SIZE))); 855b1b8153cSVignesh R #else 856b1b8153cSVignesh R const bool kmap_buf = false; 857b1b8153cSVignesh R #endif 85865598c13SAndrew Gabbasov int desc_len; 85965598c13SAndrew Gabbasov int sgs; 8606ad45a27SMark Brown struct page *vm_page; 8618dd4a016SJuan Gutierrez struct scatterlist *sg; 8626ad45a27SMark Brown void *sg_buf; 8636ad45a27SMark Brown size_t min; 8646ad45a27SMark Brown int i, ret; 8656ad45a27SMark Brown 866b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 867df88e91bSAndy Shevchenko desc_len = min_t(int, max_seg_size, PAGE_SIZE); 86865598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); 8690569a88fSVignesh R } else if (virt_addr_valid(buf)) { 8708caab75fSGeert Uytterhoeven desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); 87165598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len, desc_len); 8720569a88fSVignesh R } else { 8730569a88fSVignesh R return -EINVAL; 87465598c13SAndrew Gabbasov } 87565598c13SAndrew Gabbasov 8766ad45a27SMark Brown ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); 8776ad45a27SMark Brown if (ret != 0) 8786ad45a27SMark Brown return ret; 8796ad45a27SMark Brown 8808dd4a016SJuan Gutierrez sg = &sgt->sgl[0]; 8816ad45a27SMark Brown for (i = 0; i < sgs; i++) { 8826ad45a27SMark Brown 883b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 884ce99319aSMaxime Chevallier /* 885ce99319aSMaxime Chevallier * Next scatterlist entry size is the minimum between 886ce99319aSMaxime Chevallier * the desc_len and the remaining buffer length that 887ce99319aSMaxime Chevallier * fits in a page. 888ce99319aSMaxime Chevallier */ 889ce99319aSMaxime Chevallier min = min_t(size_t, desc_len, 890ce99319aSMaxime Chevallier min_t(size_t, len, 891ce99319aSMaxime Chevallier PAGE_SIZE - offset_in_page(buf))); 892b1b8153cSVignesh R if (vmalloced_buf) 8936ad45a27SMark Brown vm_page = vmalloc_to_page(buf); 894b1b8153cSVignesh R else 895b1b8153cSVignesh R vm_page = kmap_to_page(buf); 8966ad45a27SMark Brown if (!vm_page) { 8976ad45a27SMark Brown sg_free_table(sgt); 8986ad45a27SMark Brown return -ENOMEM; 8996ad45a27SMark Brown } 9008dd4a016SJuan Gutierrez sg_set_page(sg, vm_page, 901c1aefbddSCharles Keepax min, offset_in_page(buf)); 9026ad45a27SMark Brown } else { 90365598c13SAndrew Gabbasov min = min_t(size_t, len, desc_len); 9046ad45a27SMark Brown sg_buf = buf; 9058dd4a016SJuan Gutierrez sg_set_buf(sg, sg_buf, min); 9066ad45a27SMark Brown } 9076ad45a27SMark Brown 9086ad45a27SMark Brown buf += min; 9096ad45a27SMark Brown len -= min; 9108dd4a016SJuan Gutierrez sg = sg_next(sg); 9116ad45a27SMark Brown } 9126ad45a27SMark Brown 9136ad45a27SMark Brown ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); 91489e4b66aSGeert Uytterhoeven if (!ret) 91589e4b66aSGeert Uytterhoeven ret = -ENOMEM; 9166ad45a27SMark Brown if (ret < 0) { 9176ad45a27SMark Brown sg_free_table(sgt); 9186ad45a27SMark Brown return ret; 9196ad45a27SMark Brown } 9206ad45a27SMark Brown 9216ad45a27SMark Brown sgt->nents = ret; 9226ad45a27SMark Brown 9236ad45a27SMark Brown return 0; 9246ad45a27SMark Brown } 9256ad45a27SMark Brown 92646336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, 9276ad45a27SMark Brown struct sg_table *sgt, enum dma_data_direction dir) 9286ad45a27SMark Brown { 9296ad45a27SMark Brown if (sgt->orig_nents) { 9306ad45a27SMark Brown dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); 9316ad45a27SMark Brown sg_free_table(sgt); 9326ad45a27SMark Brown } 9336ad45a27SMark Brown } 9346ad45a27SMark Brown 9358caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 93699adef31SMark Brown { 93799adef31SMark Brown struct device *tx_dev, *rx_dev; 93899adef31SMark Brown struct spi_transfer *xfer; 9396ad45a27SMark Brown int ret; 9403a2eba9bSMark Brown 9418caab75fSGeert Uytterhoeven if (!ctlr->can_dma) 94299adef31SMark Brown return 0; 94399adef31SMark Brown 9448caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 9458caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 946c37f45b5SLeilk Liu else 9478caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 948c37f45b5SLeilk Liu 9498caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 9508caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 951c37f45b5SLeilk Liu else 9528caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 95399adef31SMark Brown 95499adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9558caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 95699adef31SMark Brown continue; 95799adef31SMark Brown 95899adef31SMark Brown if (xfer->tx_buf != NULL) { 9598caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, 9606ad45a27SMark Brown (void *)xfer->tx_buf, xfer->len, 96199adef31SMark Brown DMA_TO_DEVICE); 9626ad45a27SMark Brown if (ret != 0) 9636ad45a27SMark Brown return ret; 96499adef31SMark Brown } 96599adef31SMark Brown 96699adef31SMark Brown if (xfer->rx_buf != NULL) { 9678caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, 96899adef31SMark Brown xfer->rx_buf, xfer->len, 96999adef31SMark Brown DMA_FROM_DEVICE); 9706ad45a27SMark Brown if (ret != 0) { 9718caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, 9726ad45a27SMark Brown DMA_TO_DEVICE); 9736ad45a27SMark Brown return ret; 97499adef31SMark Brown } 97599adef31SMark Brown } 97699adef31SMark Brown } 97799adef31SMark Brown 9788caab75fSGeert Uytterhoeven ctlr->cur_msg_mapped = true; 97999adef31SMark Brown 98099adef31SMark Brown return 0; 98199adef31SMark Brown } 98299adef31SMark Brown 9838caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg) 98499adef31SMark Brown { 98599adef31SMark Brown struct spi_transfer *xfer; 98699adef31SMark Brown struct device *tx_dev, *rx_dev; 98799adef31SMark Brown 9888caab75fSGeert Uytterhoeven if (!ctlr->cur_msg_mapped || !ctlr->can_dma) 98999adef31SMark Brown return 0; 99099adef31SMark Brown 9918caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 9928caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 993c37f45b5SLeilk Liu else 9948caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 995c37f45b5SLeilk Liu 9968caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 9978caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 998c37f45b5SLeilk Liu else 9998caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 100099adef31SMark Brown 100199adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 10028caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 100399adef31SMark Brown continue; 100499adef31SMark Brown 10058caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 10068caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 100799adef31SMark Brown } 100899adef31SMark Brown 1009809b1b04SRobin Gong ctlr->cur_msg_mapped = false; 1010809b1b04SRobin Gong 101199adef31SMark Brown return 0; 101299adef31SMark Brown } 10132de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */ 10148caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr, 10152de440f5SGeert Uytterhoeven struct spi_message *msg) 10162de440f5SGeert Uytterhoeven { 10172de440f5SGeert Uytterhoeven return 0; 10182de440f5SGeert Uytterhoeven } 10192de440f5SGeert Uytterhoeven 10208caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr, 10212de440f5SGeert Uytterhoeven struct spi_message *msg) 10222de440f5SGeert Uytterhoeven { 10232de440f5SGeert Uytterhoeven return 0; 10242de440f5SGeert Uytterhoeven } 10252de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */ 10262de440f5SGeert Uytterhoeven 10278caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr, 10284b786458SMartin Sperl struct spi_message *msg) 10294b786458SMartin Sperl { 10304b786458SMartin Sperl struct spi_transfer *xfer; 10314b786458SMartin Sperl 10324b786458SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 10334b786458SMartin Sperl /* 10344b786458SMartin Sperl * Restore the original value of tx_buf or rx_buf if they are 10354b786458SMartin Sperl * NULL. 10364b786458SMartin Sperl */ 10378caab75fSGeert Uytterhoeven if (xfer->tx_buf == ctlr->dummy_tx) 10384b786458SMartin Sperl xfer->tx_buf = NULL; 10398caab75fSGeert Uytterhoeven if (xfer->rx_buf == ctlr->dummy_rx) 10404b786458SMartin Sperl xfer->rx_buf = NULL; 10414b786458SMartin Sperl } 10424b786458SMartin Sperl 10438caab75fSGeert Uytterhoeven return __spi_unmap_msg(ctlr, msg); 10444b786458SMartin Sperl } 10454b786458SMartin Sperl 10468caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 10472de440f5SGeert Uytterhoeven { 10482de440f5SGeert Uytterhoeven struct spi_transfer *xfer; 10492de440f5SGeert Uytterhoeven void *tmp; 10502de440f5SGeert Uytterhoeven unsigned int max_tx, max_rx; 10512de440f5SGeert Uytterhoeven 1052aee67fe8Sdillon min if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) 1053aee67fe8Sdillon min && !(msg->spi->mode & SPI_3WIRE)) { 10542de440f5SGeert Uytterhoeven max_tx = 0; 10552de440f5SGeert Uytterhoeven max_rx = 0; 10562de440f5SGeert Uytterhoeven 10572de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, transfer_list) { 10588caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && 10592de440f5SGeert Uytterhoeven !xfer->tx_buf) 10602de440f5SGeert Uytterhoeven max_tx = max(xfer->len, max_tx); 10618caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && 10622de440f5SGeert Uytterhoeven !xfer->rx_buf) 10632de440f5SGeert Uytterhoeven max_rx = max(xfer->len, max_rx); 10642de440f5SGeert Uytterhoeven } 10652de440f5SGeert Uytterhoeven 10662de440f5SGeert Uytterhoeven if (max_tx) { 10678caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_tx, max_tx, 10682de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 10692de440f5SGeert Uytterhoeven if (!tmp) 10702de440f5SGeert Uytterhoeven return -ENOMEM; 10718caab75fSGeert Uytterhoeven ctlr->dummy_tx = tmp; 10722de440f5SGeert Uytterhoeven memset(tmp, 0, max_tx); 10732de440f5SGeert Uytterhoeven } 10742de440f5SGeert Uytterhoeven 10752de440f5SGeert Uytterhoeven if (max_rx) { 10768caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_rx, max_rx, 10772de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 10782de440f5SGeert Uytterhoeven if (!tmp) 10792de440f5SGeert Uytterhoeven return -ENOMEM; 10808caab75fSGeert Uytterhoeven ctlr->dummy_rx = tmp; 10812de440f5SGeert Uytterhoeven } 10822de440f5SGeert Uytterhoeven 10832de440f5SGeert Uytterhoeven if (max_tx || max_rx) { 10842de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, 10852de440f5SGeert Uytterhoeven transfer_list) { 10865442dcaaSChris Lesiak if (!xfer->len) 10875442dcaaSChris Lesiak continue; 10882de440f5SGeert Uytterhoeven if (!xfer->tx_buf) 10898caab75fSGeert Uytterhoeven xfer->tx_buf = ctlr->dummy_tx; 10902de440f5SGeert Uytterhoeven if (!xfer->rx_buf) 10918caab75fSGeert Uytterhoeven xfer->rx_buf = ctlr->dummy_rx; 10922de440f5SGeert Uytterhoeven } 10932de440f5SGeert Uytterhoeven } 10942de440f5SGeert Uytterhoeven } 10952de440f5SGeert Uytterhoeven 10968caab75fSGeert Uytterhoeven return __spi_map_msg(ctlr, msg); 10972de440f5SGeert Uytterhoeven } 109899adef31SMark Brown 1099810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr, 1100810923f3SLubomir Rintel struct spi_message *msg, 1101810923f3SLubomir Rintel struct spi_transfer *xfer) 1102810923f3SLubomir Rintel { 1103810923f3SLubomir Rintel struct spi_statistics *statm = &ctlr->statistics; 1104810923f3SLubomir Rintel struct spi_statistics *stats = &msg->spi->statistics; 110549686df5SColin Ian King unsigned long long ms; 1106810923f3SLubomir Rintel 1107810923f3SLubomir Rintel if (spi_controller_is_slave(ctlr)) { 1108810923f3SLubomir Rintel if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { 1109810923f3SLubomir Rintel dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); 1110810923f3SLubomir Rintel return -EINTR; 1111810923f3SLubomir Rintel } 1112810923f3SLubomir Rintel } else { 1113810923f3SLubomir Rintel ms = 8LL * 1000LL * xfer->len; 1114810923f3SLubomir Rintel do_div(ms, xfer->speed_hz); 1115810923f3SLubomir Rintel ms += ms + 200; /* some tolerance */ 1116810923f3SLubomir Rintel 1117810923f3SLubomir Rintel if (ms > UINT_MAX) 1118810923f3SLubomir Rintel ms = UINT_MAX; 1119810923f3SLubomir Rintel 1120810923f3SLubomir Rintel ms = wait_for_completion_timeout(&ctlr->xfer_completion, 1121810923f3SLubomir Rintel msecs_to_jiffies(ms)); 1122810923f3SLubomir Rintel 1123810923f3SLubomir Rintel if (ms == 0) { 1124810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(statm, timedout); 1125810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(stats, timedout); 1126810923f3SLubomir Rintel dev_err(&msg->spi->dev, 1127810923f3SLubomir Rintel "SPI transfer timed out\n"); 1128810923f3SLubomir Rintel return -ETIMEDOUT; 1129810923f3SLubomir Rintel } 1130810923f3SLubomir Rintel } 1131810923f3SLubomir Rintel 1132810923f3SLubomir Rintel return 0; 1133810923f3SLubomir Rintel } 1134810923f3SLubomir Rintel 11350ff2de8bSMartin Sperl static void _spi_transfer_delay_ns(u32 ns) 11360ff2de8bSMartin Sperl { 11370ff2de8bSMartin Sperl if (!ns) 11380ff2de8bSMartin Sperl return; 11390ff2de8bSMartin Sperl if (ns <= 1000) { 11400ff2de8bSMartin Sperl ndelay(ns); 11410ff2de8bSMartin Sperl } else { 11420ff2de8bSMartin Sperl u32 us = DIV_ROUND_UP(ns, 1000); 11430ff2de8bSMartin Sperl 11440ff2de8bSMartin Sperl if (us <= 10) 11450ff2de8bSMartin Sperl udelay(us); 11460ff2de8bSMartin Sperl else 11470ff2de8bSMartin Sperl usleep_range(us, us + DIV_ROUND_UP(us, 10)); 11480ff2de8bSMartin Sperl } 11490ff2de8bSMartin Sperl } 11500ff2de8bSMartin Sperl 11513984d39bSAlexandru Ardelean int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer) 11520ff2de8bSMartin Sperl { 1153b2c98153SAlexandru Ardelean u32 delay = _delay->value; 1154b2c98153SAlexandru Ardelean u32 unit = _delay->unit; 1155d5864e5bSMartin Sperl u32 hz; 11560ff2de8bSMartin Sperl 1157b2c98153SAlexandru Ardelean if (!delay) 1158b2c98153SAlexandru Ardelean return 0; 11590ff2de8bSMartin Sperl 11600ff2de8bSMartin Sperl switch (unit) { 11610ff2de8bSMartin Sperl case SPI_DELAY_UNIT_USECS: 11620ff2de8bSMartin Sperl delay *= 1000; 11630ff2de8bSMartin Sperl break; 11640ff2de8bSMartin Sperl case SPI_DELAY_UNIT_NSECS: /* nothing to do here */ 11650ff2de8bSMartin Sperl break; 1166d5864e5bSMartin Sperl case SPI_DELAY_UNIT_SCK: 1167b2c98153SAlexandru Ardelean /* clock cycles need to be obtained from spi_transfer */ 1168b2c98153SAlexandru Ardelean if (!xfer) 1169b2c98153SAlexandru Ardelean return -EINVAL; 1170d5864e5bSMartin Sperl /* if there is no effective speed know, then approximate 1171d5864e5bSMartin Sperl * by underestimating with half the requested hz 1172d5864e5bSMartin Sperl */ 1173d5864e5bSMartin Sperl hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; 1174b2c98153SAlexandru Ardelean if (!hz) 1175b2c98153SAlexandru Ardelean return -EINVAL; 1176d5864e5bSMartin Sperl delay *= DIV_ROUND_UP(1000000000, hz); 1177d5864e5bSMartin Sperl break; 11780ff2de8bSMartin Sperl default: 1179b2c98153SAlexandru Ardelean return -EINVAL; 1180b2c98153SAlexandru Ardelean } 1181b2c98153SAlexandru Ardelean 1182b2c98153SAlexandru Ardelean return delay; 1183b2c98153SAlexandru Ardelean } 11843984d39bSAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_to_ns); 1185b2c98153SAlexandru Ardelean 1186b2c98153SAlexandru Ardelean int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer) 1187b2c98153SAlexandru Ardelean { 1188b2c98153SAlexandru Ardelean int delay; 1189b2c98153SAlexandru Ardelean 11908fede89fSMark Brown might_sleep(); 11918fede89fSMark Brown 1192b2c98153SAlexandru Ardelean if (!_delay) 1193b2c98153SAlexandru Ardelean return -EINVAL; 1194b2c98153SAlexandru Ardelean 11953984d39bSAlexandru Ardelean delay = spi_delay_to_ns(_delay, xfer); 1196b2c98153SAlexandru Ardelean if (delay < 0) 1197b2c98153SAlexandru Ardelean return delay; 1198b2c98153SAlexandru Ardelean 1199b2c98153SAlexandru Ardelean _spi_transfer_delay_ns(delay); 1200b2c98153SAlexandru Ardelean 1201b2c98153SAlexandru Ardelean return 0; 1202b2c98153SAlexandru Ardelean } 1203b2c98153SAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_exec); 1204b2c98153SAlexandru Ardelean 12050ff2de8bSMartin Sperl static void _spi_transfer_cs_change_delay(struct spi_message *msg, 12060ff2de8bSMartin Sperl struct spi_transfer *xfer) 12070ff2de8bSMartin Sperl { 1208329f0dacSAlexandru Ardelean u32 delay = xfer->cs_change_delay.value; 1209329f0dacSAlexandru Ardelean u32 unit = xfer->cs_change_delay.unit; 1210329f0dacSAlexandru Ardelean int ret; 12110ff2de8bSMartin Sperl 12120ff2de8bSMartin Sperl /* return early on "fast" mode - for everything but USECS */ 12136b3f236aSAlexandru Ardelean if (!delay) { 12146b3f236aSAlexandru Ardelean if (unit == SPI_DELAY_UNIT_USECS) 12156b3f236aSAlexandru Ardelean _spi_transfer_delay_ns(10000); 12160ff2de8bSMartin Sperl return; 12176b3f236aSAlexandru Ardelean } 12180ff2de8bSMartin Sperl 1219329f0dacSAlexandru Ardelean ret = spi_delay_exec(&xfer->cs_change_delay, xfer); 1220329f0dacSAlexandru Ardelean if (ret) { 12210ff2de8bSMartin Sperl dev_err_once(&msg->spi->dev, 12220ff2de8bSMartin Sperl "Use of unsupported delay unit %i, using default of 10us\n", 1223329f0dacSAlexandru Ardelean unit); 1224329f0dacSAlexandru Ardelean _spi_transfer_delay_ns(10000); 12250ff2de8bSMartin Sperl } 12260ff2de8bSMartin Sperl } 12270ff2de8bSMartin Sperl 1228b158935fSMark Brown /* 1229b158935fSMark Brown * spi_transfer_one_message - Default implementation of transfer_one_message() 1230b158935fSMark Brown * 1231b158935fSMark Brown * This is a standard implementation of transfer_one_message() for 12328ba811a7SMoritz Fischer * drivers which implement a transfer_one() operation. It provides 1233b158935fSMark Brown * standard handling of delays and chip select management. 1234b158935fSMark Brown */ 12358caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr, 1236b158935fSMark Brown struct spi_message *msg) 1237b158935fSMark Brown { 1238b158935fSMark Brown struct spi_transfer *xfer; 1239b158935fSMark Brown bool keep_cs = false; 1240b158935fSMark Brown int ret = 0; 12418caab75fSGeert Uytterhoeven struct spi_statistics *statm = &ctlr->statistics; 1242eca2ebc7SMartin Sperl struct spi_statistics *stats = &msg->spi->statistics; 1243b158935fSMark Brown 1244b158935fSMark Brown spi_set_cs(msg->spi, true); 1245b158935fSMark Brown 1246eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, messages); 1247eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, messages); 1248eca2ebc7SMartin Sperl 1249b158935fSMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1250b158935fSMark Brown trace_spi_transfer_start(msg, xfer); 1251b158935fSMark Brown 12528caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(statm, xfer, ctlr); 12538caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(stats, xfer, ctlr); 1254eca2ebc7SMartin Sperl 1255b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 1256b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 1257b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1258b42faeeeSVladimir Oltean } 1259b42faeeeSVladimir Oltean 126038ec10f6SMark Brown if (xfer->tx_buf || xfer->rx_buf) { 12618caab75fSGeert Uytterhoeven reinit_completion(&ctlr->xfer_completion); 1262b158935fSMark Brown 1263809b1b04SRobin Gong fallback_pio: 12648caab75fSGeert Uytterhoeven ret = ctlr->transfer_one(ctlr, msg->spi, xfer); 1265b158935fSMark Brown if (ret < 0) { 1266809b1b04SRobin Gong if (ctlr->cur_msg_mapped && 1267809b1b04SRobin Gong (xfer->error & SPI_TRANS_FAIL_NO_START)) { 1268809b1b04SRobin Gong __spi_unmap_msg(ctlr, msg); 1269809b1b04SRobin Gong ctlr->fallback = true; 1270809b1b04SRobin Gong xfer->error &= ~SPI_TRANS_FAIL_NO_START; 1271809b1b04SRobin Gong goto fallback_pio; 1272809b1b04SRobin Gong } 1273809b1b04SRobin Gong 1274eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, 1275eca2ebc7SMartin Sperl errors); 1276eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, 1277eca2ebc7SMartin Sperl errors); 1278b158935fSMark Brown dev_err(&msg->spi->dev, 1279b158935fSMark Brown "SPI transfer failed: %d\n", ret); 1280b158935fSMark Brown goto out; 1281b158935fSMark Brown } 1282b158935fSMark Brown 1283d57e7960SMark Brown if (ret > 0) { 1284810923f3SLubomir Rintel ret = spi_transfer_wait(ctlr, msg, xfer); 1285810923f3SLubomir Rintel if (ret < 0) 1286810923f3SLubomir Rintel msg->status = ret; 1287d57e7960SMark Brown } 128838ec10f6SMark Brown } else { 128938ec10f6SMark Brown if (xfer->len) 129038ec10f6SMark Brown dev_err(&msg->spi->dev, 129138ec10f6SMark Brown "Bufferless transfer has length %u\n", 129238ec10f6SMark Brown xfer->len); 129338ec10f6SMark Brown } 1294b158935fSMark Brown 1295b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 1296b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1297b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 1298b42faeeeSVladimir Oltean } 1299b42faeeeSVladimir Oltean 1300b158935fSMark Brown trace_spi_transfer_stop(msg, xfer); 1301b158935fSMark Brown 1302b158935fSMark Brown if (msg->status != -EINPROGRESS) 1303b158935fSMark Brown goto out; 1304b158935fSMark Brown 1305bebcfd27SAlexandru Ardelean spi_transfer_delay_exec(xfer); 1306b158935fSMark Brown 1307b158935fSMark Brown if (xfer->cs_change) { 1308b158935fSMark Brown if (list_is_last(&xfer->transfer_list, 1309b158935fSMark Brown &msg->transfers)) { 1310b158935fSMark Brown keep_cs = true; 1311b158935fSMark Brown } else { 13120b73aa63SMark Brown spi_set_cs(msg->spi, false); 13130ff2de8bSMartin Sperl _spi_transfer_cs_change_delay(msg, xfer); 13140b73aa63SMark Brown spi_set_cs(msg->spi, true); 1315b158935fSMark Brown } 1316b158935fSMark Brown } 1317b158935fSMark Brown 1318b158935fSMark Brown msg->actual_length += xfer->len; 1319b158935fSMark Brown } 1320b158935fSMark Brown 1321b158935fSMark Brown out: 1322b158935fSMark Brown if (ret != 0 || !keep_cs) 1323b158935fSMark Brown spi_set_cs(msg->spi, false); 1324b158935fSMark Brown 1325b158935fSMark Brown if (msg->status == -EINPROGRESS) 1326b158935fSMark Brown msg->status = ret; 1327b158935fSMark Brown 13288caab75fSGeert Uytterhoeven if (msg->status && ctlr->handle_err) 13298caab75fSGeert Uytterhoeven ctlr->handle_err(ctlr, msg); 1330b716c4ffSAndy Shevchenko 13310ed56252SMark Brown spi_finalize_current_message(ctlr); 13320ed56252SMark Brown 1333b158935fSMark Brown return ret; 1334b158935fSMark Brown } 1335b158935fSMark Brown 1336b158935fSMark Brown /** 1337b158935fSMark Brown * spi_finalize_current_transfer - report completion of a transfer 13388caab75fSGeert Uytterhoeven * @ctlr: the controller reporting completion 1339b158935fSMark Brown * 1340b158935fSMark Brown * Called by SPI drivers using the core transfer_one_message() 1341b158935fSMark Brown * implementation to notify it that the current interrupt driven 13429e8f4882SGeert Uytterhoeven * transfer has finished and the next one may be scheduled. 1343b158935fSMark Brown */ 13448caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr) 1345b158935fSMark Brown { 13468caab75fSGeert Uytterhoeven complete(&ctlr->xfer_completion); 1347b158935fSMark Brown } 1348b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); 1349b158935fSMark Brown 1350e1268597SMark Brown static void spi_idle_runtime_pm(struct spi_controller *ctlr) 1351e1268597SMark Brown { 1352e1268597SMark Brown if (ctlr->auto_runtime_pm) { 1353e1268597SMark Brown pm_runtime_mark_last_busy(ctlr->dev.parent); 1354e1268597SMark Brown pm_runtime_put_autosuspend(ctlr->dev.parent); 1355e1268597SMark Brown } 1356e1268597SMark Brown } 1357e1268597SMark Brown 1358ffbbdd21SLinus Walleij /** 1359fc9e0f71SMark Brown * __spi_pump_messages - function which processes spi message queue 13608caab75fSGeert Uytterhoeven * @ctlr: controller to process queue for 1361fc9e0f71SMark Brown * @in_kthread: true if we are in the context of the message pump thread 1362ffbbdd21SLinus Walleij * 1363ffbbdd21SLinus Walleij * This function checks if there is any spi message in the queue that 1364ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 1365ffbbdd21SLinus Walleij * and transfer each message. 1366ffbbdd21SLinus Walleij * 13670461a414SMark Brown * Note that it is called both from the kthread itself and also from 13680461a414SMark Brown * inside spi_sync(); the queue extraction handling at the top of the 13690461a414SMark Brown * function should deal with this safely. 1370ffbbdd21SLinus Walleij */ 13718caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1372ffbbdd21SLinus Walleij { 1373b42faeeeSVladimir Oltean struct spi_transfer *xfer; 1374d1c44c93SVladimir Oltean struct spi_message *msg; 1375ffbbdd21SLinus Walleij bool was_busy = false; 1376d1c44c93SVladimir Oltean unsigned long flags; 1377ffbbdd21SLinus Walleij int ret; 1378ffbbdd21SLinus Walleij 1379983aee5dSMark Brown /* Lock queue */ 13808caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1381983aee5dSMark Brown 1382983aee5dSMark Brown /* Make sure we are not already running a message */ 13838caab75fSGeert Uytterhoeven if (ctlr->cur_msg) { 13848caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1385983aee5dSMark Brown return; 1386983aee5dSMark Brown } 1387983aee5dSMark Brown 1388f0125f1aSMark Brown /* If another context is idling the device then defer */ 13898caab75fSGeert Uytterhoeven if (ctlr->idling) { 139060a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 13918caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 13920461a414SMark Brown return; 13930461a414SMark Brown } 13940461a414SMark Brown 1395983aee5dSMark Brown /* Check if the queue is idle */ 13968caab75fSGeert Uytterhoeven if (list_empty(&ctlr->queue) || !ctlr->running) { 13978caab75fSGeert Uytterhoeven if (!ctlr->busy) { 13988caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1399ffbbdd21SLinus Walleij return; 1400ffbbdd21SLinus Walleij } 1401fc9e0f71SMark Brown 1402e1268597SMark Brown /* Defer any non-atomic teardown to the thread */ 1403f0125f1aSMark Brown if (!in_kthread) { 1404e1268597SMark Brown if (!ctlr->dummy_rx && !ctlr->dummy_tx && 1405e1268597SMark Brown !ctlr->unprepare_transfer_hardware) { 1406e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1407e1268597SMark Brown ctlr->busy = false; 1408e1268597SMark Brown trace_spi_controller_idle(ctlr); 1409e1268597SMark Brown } else { 141060a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, 1411f0125f1aSMark Brown &ctlr->pump_messages); 1412e1268597SMark Brown } 1413f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1414f0125f1aSMark Brown return; 1415f0125f1aSMark Brown } 1416f0125f1aSMark Brown 1417f0125f1aSMark Brown ctlr->busy = false; 1418f0125f1aSMark Brown ctlr->idling = true; 1419f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1420f0125f1aSMark Brown 1421f0125f1aSMark Brown kfree(ctlr->dummy_rx); 1422f0125f1aSMark Brown ctlr->dummy_rx = NULL; 1423f0125f1aSMark Brown kfree(ctlr->dummy_tx); 1424f0125f1aSMark Brown ctlr->dummy_tx = NULL; 1425f0125f1aSMark Brown if (ctlr->unprepare_transfer_hardware && 1426f0125f1aSMark Brown ctlr->unprepare_transfer_hardware(ctlr)) 1427f0125f1aSMark Brown dev_err(&ctlr->dev, 1428f0125f1aSMark Brown "failed to unprepare transfer hardware\n"); 1429e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1430f0125f1aSMark Brown trace_spi_controller_idle(ctlr); 1431f0125f1aSMark Brown 1432f0125f1aSMark Brown spin_lock_irqsave(&ctlr->queue_lock, flags); 1433f0125f1aSMark Brown ctlr->idling = false; 14348caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1435ffbbdd21SLinus Walleij return; 1436ffbbdd21SLinus Walleij } 1437ffbbdd21SLinus Walleij 1438ffbbdd21SLinus Walleij /* Extract head of queue */ 1439d1c44c93SVladimir Oltean msg = list_first_entry(&ctlr->queue, struct spi_message, queue); 1440d1c44c93SVladimir Oltean ctlr->cur_msg = msg; 1441ffbbdd21SLinus Walleij 1442d1c44c93SVladimir Oltean list_del_init(&msg->queue); 14438caab75fSGeert Uytterhoeven if (ctlr->busy) 1444ffbbdd21SLinus Walleij was_busy = true; 1445ffbbdd21SLinus Walleij else 14468caab75fSGeert Uytterhoeven ctlr->busy = true; 14478caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1448ffbbdd21SLinus Walleij 14498caab75fSGeert Uytterhoeven mutex_lock(&ctlr->io_mutex); 1450ef4d96ecSMark Brown 14518caab75fSGeert Uytterhoeven if (!was_busy && ctlr->auto_runtime_pm) { 14528caab75fSGeert Uytterhoeven ret = pm_runtime_get_sync(ctlr->dev.parent); 145349834de2SMark Brown if (ret < 0) { 14547e48e23aSTony Lindgren pm_runtime_put_noidle(ctlr->dev.parent); 14558caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "Failed to power device: %d\n", 145649834de2SMark Brown ret); 14578caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 145849834de2SMark Brown return; 145949834de2SMark Brown } 146049834de2SMark Brown } 146149834de2SMark Brown 146256ec1978SMark Brown if (!was_busy) 14638caab75fSGeert Uytterhoeven trace_spi_controller_busy(ctlr); 146456ec1978SMark Brown 14658caab75fSGeert Uytterhoeven if (!was_busy && ctlr->prepare_transfer_hardware) { 14668caab75fSGeert Uytterhoeven ret = ctlr->prepare_transfer_hardware(ctlr); 1467ffbbdd21SLinus Walleij if (ret) { 14688caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 1469f3440d9aSSuper Liu "failed to prepare transfer hardware: %d\n", 1470f3440d9aSSuper Liu ret); 147149834de2SMark Brown 14728caab75fSGeert Uytterhoeven if (ctlr->auto_runtime_pm) 14738caab75fSGeert Uytterhoeven pm_runtime_put(ctlr->dev.parent); 1474f3440d9aSSuper Liu 1475d1c44c93SVladimir Oltean msg->status = ret; 1476f3440d9aSSuper Liu spi_finalize_current_message(ctlr); 1477f3440d9aSSuper Liu 14788caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 1479ffbbdd21SLinus Walleij return; 1480ffbbdd21SLinus Walleij } 1481ffbbdd21SLinus Walleij } 1482ffbbdd21SLinus Walleij 1483d1c44c93SVladimir Oltean trace_spi_message_start(msg); 148456ec1978SMark Brown 14858caab75fSGeert Uytterhoeven if (ctlr->prepare_message) { 1486d1c44c93SVladimir Oltean ret = ctlr->prepare_message(ctlr, msg); 14872841a5fcSMark Brown if (ret) { 14888caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to prepare message: %d\n", 14898caab75fSGeert Uytterhoeven ret); 1490d1c44c93SVladimir Oltean msg->status = ret; 14918caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 149249023d2eSJon Hunter goto out; 14932841a5fcSMark Brown } 14948caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = true; 14952841a5fcSMark Brown } 14962841a5fcSMark Brown 1497d1c44c93SVladimir Oltean ret = spi_map_msg(ctlr, msg); 149899adef31SMark Brown if (ret) { 1499d1c44c93SVladimir Oltean msg->status = ret; 15008caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 150149023d2eSJon Hunter goto out; 150299adef31SMark Brown } 150399adef31SMark Brown 1504b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1505b42faeeeSVladimir Oltean list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1506b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 1507b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1508b42faeeeSVladimir Oltean } 1509b42faeeeSVladimir Oltean } 1510b42faeeeSVladimir Oltean 1511d1c44c93SVladimir Oltean ret = ctlr->transfer_one_message(ctlr, msg); 1512ffbbdd21SLinus Walleij if (ret) { 15138caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 15141f802f82SGeert Uytterhoeven "failed to transfer one message from queue\n"); 151549023d2eSJon Hunter goto out; 1516ffbbdd21SLinus Walleij } 151749023d2eSJon Hunter 151849023d2eSJon Hunter out: 15198caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 152062826970SMark Brown 152162826970SMark Brown /* Prod the scheduler in case transfer_one() was busy waiting */ 152249023d2eSJon Hunter if (!ret) 152362826970SMark Brown cond_resched(); 1524ffbbdd21SLinus Walleij } 1525ffbbdd21SLinus Walleij 1526fc9e0f71SMark Brown /** 1527fc9e0f71SMark Brown * spi_pump_messages - kthread work function which processes spi message queue 15288caab75fSGeert Uytterhoeven * @work: pointer to kthread work struct contained in the controller struct 1529fc9e0f71SMark Brown */ 1530fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work) 1531fc9e0f71SMark Brown { 15328caab75fSGeert Uytterhoeven struct spi_controller *ctlr = 15338caab75fSGeert Uytterhoeven container_of(work, struct spi_controller, pump_messages); 1534fc9e0f71SMark Brown 15358caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, true); 1536fc9e0f71SMark Brown } 1537fc9e0f71SMark Brown 1538924b5867SDouglas Anderson /** 1539b42faeeeSVladimir Oltean * spi_take_timestamp_pre - helper for drivers to collect the beginning of the 1540b42faeeeSVladimir Oltean * TX timestamp for the requested byte from the SPI 1541b42faeeeSVladimir Oltean * transfer. The frequency with which this function 1542b42faeeeSVladimir Oltean * must be called (once per word, once for the whole 1543b42faeeeSVladimir Oltean * transfer, once per batch of words etc) is arbitrary 1544b42faeeeSVladimir Oltean * as long as the @tx buffer offset is greater than or 1545b42faeeeSVladimir Oltean * equal to the requested byte at the time of the 1546b42faeeeSVladimir Oltean * call. The timestamp is only taken once, at the 1547b42faeeeSVladimir Oltean * first such call. It is assumed that the driver 1548b42faeeeSVladimir Oltean * advances its @tx buffer pointer monotonically. 1549b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1550b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1551862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1552b42faeeeSVladimir Oltean * @irqs_off: If true, will disable IRQs and preemption for the duration of the 1553b42faeeeSVladimir Oltean * transfer, for less jitter in time measurement. Only compatible 1554b42faeeeSVladimir Oltean * with PIO drivers. If true, must follow up with 1555b42faeeeSVladimir Oltean * spi_take_timestamp_post or otherwise system will crash. 1556b42faeeeSVladimir Oltean * WARNING: for fully predictable results, the CPU frequency must 1557b42faeeeSVladimir Oltean * also be under control (governor). 1558b42faeeeSVladimir Oltean */ 1559b42faeeeSVladimir Oltean void spi_take_timestamp_pre(struct spi_controller *ctlr, 1560b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1561862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1562b42faeeeSVladimir Oltean { 1563b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1564b42faeeeSVladimir Oltean return; 1565b42faeeeSVladimir Oltean 15666a726824SVladimir Oltean if (xfer->timestamped) 1567b42faeeeSVladimir Oltean return; 1568b42faeeeSVladimir Oltean 15696a726824SVladimir Oltean if (progress > xfer->ptp_sts_word_pre) 1570b42faeeeSVladimir Oltean return; 1571b42faeeeSVladimir Oltean 1572b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1573862dd2a9SVladimir Oltean xfer->ptp_sts_word_pre = progress; 1574b42faeeeSVladimir Oltean 1575b42faeeeSVladimir Oltean if (irqs_off) { 1576b42faeeeSVladimir Oltean local_irq_save(ctlr->irq_flags); 1577b42faeeeSVladimir Oltean preempt_disable(); 1578b42faeeeSVladimir Oltean } 1579b42faeeeSVladimir Oltean 1580b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1581b42faeeeSVladimir Oltean } 1582b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_pre); 1583b42faeeeSVladimir Oltean 1584b42faeeeSVladimir Oltean /** 1585b42faeeeSVladimir Oltean * spi_take_timestamp_post - helper for drivers to collect the end of the 1586b42faeeeSVladimir Oltean * TX timestamp for the requested byte from the SPI 1587b42faeeeSVladimir Oltean * transfer. Can be called with an arbitrary 1588b42faeeeSVladimir Oltean * frequency: only the first call where @tx exceeds 1589b42faeeeSVladimir Oltean * or is equal to the requested word will be 1590b42faeeeSVladimir Oltean * timestamped. 1591b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1592b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1593862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1594b42faeeeSVladimir Oltean * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU. 1595b42faeeeSVladimir Oltean */ 1596b42faeeeSVladimir Oltean void spi_take_timestamp_post(struct spi_controller *ctlr, 1597b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1598862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1599b42faeeeSVladimir Oltean { 1600b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1601b42faeeeSVladimir Oltean return; 1602b42faeeeSVladimir Oltean 16036a726824SVladimir Oltean if (xfer->timestamped) 1604b42faeeeSVladimir Oltean return; 1605b42faeeeSVladimir Oltean 1606862dd2a9SVladimir Oltean if (progress < xfer->ptp_sts_word_post) 1607b42faeeeSVladimir Oltean return; 1608b42faeeeSVladimir Oltean 1609b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1610b42faeeeSVladimir Oltean 1611b42faeeeSVladimir Oltean if (irqs_off) { 1612b42faeeeSVladimir Oltean local_irq_restore(ctlr->irq_flags); 1613b42faeeeSVladimir Oltean preempt_enable(); 1614b42faeeeSVladimir Oltean } 1615b42faeeeSVladimir Oltean 1616b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1617862dd2a9SVladimir Oltean xfer->ptp_sts_word_post = progress; 1618b42faeeeSVladimir Oltean 16196a726824SVladimir Oltean xfer->timestamped = true; 1620b42faeeeSVladimir Oltean } 1621b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_post); 1622b42faeeeSVladimir Oltean 1623b42faeeeSVladimir Oltean /** 1624924b5867SDouglas Anderson * spi_set_thread_rt - set the controller to pump at realtime priority 1625924b5867SDouglas Anderson * @ctlr: controller to boost priority of 1626924b5867SDouglas Anderson * 1627924b5867SDouglas Anderson * This can be called because the controller requested realtime priority 1628924b5867SDouglas Anderson * (by setting the ->rt value before calling spi_register_controller()) or 1629924b5867SDouglas Anderson * because a device on the bus said that its transfers needed realtime 1630924b5867SDouglas Anderson * priority. 1631924b5867SDouglas Anderson * 1632924b5867SDouglas Anderson * NOTE: at the moment if any device on a bus says it needs realtime then 1633924b5867SDouglas Anderson * the thread will be at realtime priority for all transfers on that 1634924b5867SDouglas Anderson * controller. If this eventually becomes a problem we may see if we can 1635924b5867SDouglas Anderson * find a way to boost the priority only temporarily during relevant 1636924b5867SDouglas Anderson * transfers. 1637924b5867SDouglas Anderson */ 1638924b5867SDouglas Anderson static void spi_set_thread_rt(struct spi_controller *ctlr) 1639ffbbdd21SLinus Walleij { 1640924b5867SDouglas Anderson dev_info(&ctlr->dev, 1641924b5867SDouglas Anderson "will run message pump with realtime priority\n"); 16426d2b84a4SLinus Torvalds sched_set_fifo(ctlr->kworker->task); 1643924b5867SDouglas Anderson } 1644924b5867SDouglas Anderson 1645924b5867SDouglas Anderson static int spi_init_queue(struct spi_controller *ctlr) 1646924b5867SDouglas Anderson { 16478caab75fSGeert Uytterhoeven ctlr->running = false; 16488caab75fSGeert Uytterhoeven ctlr->busy = false; 1649ffbbdd21SLinus Walleij 165060a883d1SMarek Szyprowski ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); 165160a883d1SMarek Szyprowski if (IS_ERR(ctlr->kworker)) { 165260a883d1SMarek Szyprowski dev_err(&ctlr->dev, "failed to create message pump kworker\n"); 165360a883d1SMarek Szyprowski return PTR_ERR(ctlr->kworker); 1654ffbbdd21SLinus Walleij } 165560a883d1SMarek Szyprowski 16568caab75fSGeert Uytterhoeven kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1657f0125f1aSMark Brown 1658ffbbdd21SLinus Walleij /* 16598caab75fSGeert Uytterhoeven * Controller config will indicate if this controller should run the 1660ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 1661ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 1662ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 1663ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 1664ffbbdd21SLinus Walleij */ 1665924b5867SDouglas Anderson if (ctlr->rt) 1666924b5867SDouglas Anderson spi_set_thread_rt(ctlr); 1667ffbbdd21SLinus Walleij 1668ffbbdd21SLinus Walleij return 0; 1669ffbbdd21SLinus Walleij } 1670ffbbdd21SLinus Walleij 1671ffbbdd21SLinus Walleij /** 1672ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 1673ffbbdd21SLinus Walleij * messages 16748caab75fSGeert Uytterhoeven * @ctlr: the controller to check for queued messages 1675ffbbdd21SLinus Walleij * 1676ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 1677ffbbdd21SLinus Walleij * this call. 167897d56dc6SJavier Martinez Canillas * 167997d56dc6SJavier Martinez Canillas * Return: the next message in the queue, else NULL if the queue is empty. 1680ffbbdd21SLinus Walleij */ 16818caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1682ffbbdd21SLinus Walleij { 1683ffbbdd21SLinus Walleij struct spi_message *next; 1684ffbbdd21SLinus Walleij unsigned long flags; 1685ffbbdd21SLinus Walleij 1686ffbbdd21SLinus Walleij /* get a pointer to the next message, if any */ 16878caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 16888caab75fSGeert Uytterhoeven next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 16891cfd97f9SAxel Lin queue); 16908caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1691ffbbdd21SLinus Walleij 1692ffbbdd21SLinus Walleij return next; 1693ffbbdd21SLinus Walleij } 1694ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1695ffbbdd21SLinus Walleij 1696ffbbdd21SLinus Walleij /** 1697ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 16988caab75fSGeert Uytterhoeven * @ctlr: the controller to return the message to 1699ffbbdd21SLinus Walleij * 1700ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 1701ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 1702ffbbdd21SLinus Walleij */ 17038caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr) 1704ffbbdd21SLinus Walleij { 1705b42faeeeSVladimir Oltean struct spi_transfer *xfer; 1706ffbbdd21SLinus Walleij struct spi_message *mesg; 1707ffbbdd21SLinus Walleij unsigned long flags; 17082841a5fcSMark Brown int ret; 1709ffbbdd21SLinus Walleij 17108caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 17118caab75fSGeert Uytterhoeven mesg = ctlr->cur_msg; 17128caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1713ffbbdd21SLinus Walleij 1714b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1715b42faeeeSVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) { 1716b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1717b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 1718b42faeeeSVladimir Oltean } 1719b42faeeeSVladimir Oltean } 1720b42faeeeSVladimir Oltean 17216a726824SVladimir Oltean if (unlikely(ctlr->ptp_sts_supported)) 17226a726824SVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) 17236a726824SVladimir Oltean WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); 1724f971a207SVladimir Oltean 17258caab75fSGeert Uytterhoeven spi_unmap_msg(ctlr, mesg); 172699adef31SMark Brown 1727b59a7ca1SGustav Wiklander /* In the prepare_messages callback the spi bus has the opportunity to 1728b59a7ca1SGustav Wiklander * split a transfer to smaller chunks. 1729b59a7ca1SGustav Wiklander * Release splited transfers here since spi_map_msg is done on the 1730b59a7ca1SGustav Wiklander * splited transfers. 1731b59a7ca1SGustav Wiklander */ 1732b59a7ca1SGustav Wiklander spi_res_release(ctlr, mesg); 1733b59a7ca1SGustav Wiklander 17348caab75fSGeert Uytterhoeven if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { 17358caab75fSGeert Uytterhoeven ret = ctlr->unprepare_message(ctlr, mesg); 17362841a5fcSMark Brown if (ret) { 17378caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 17388caab75fSGeert Uytterhoeven ret); 17392841a5fcSMark Brown } 17402841a5fcSMark Brown } 1741391949b6SUwe Kleine-König 17428caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 17438caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 17448caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = false; 1745809b1b04SRobin Gong ctlr->fallback = false; 174660a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 17478caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 17488e76ef88SMartin Sperl 17498e76ef88SMartin Sperl trace_spi_message_done(mesg); 17502841a5fcSMark Brown 1751ffbbdd21SLinus Walleij mesg->state = NULL; 1752ffbbdd21SLinus Walleij if (mesg->complete) 1753ffbbdd21SLinus Walleij mesg->complete(mesg->context); 1754ffbbdd21SLinus Walleij } 1755ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 1756ffbbdd21SLinus Walleij 17578caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr) 1758ffbbdd21SLinus Walleij { 1759ffbbdd21SLinus Walleij unsigned long flags; 1760ffbbdd21SLinus Walleij 17618caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1762ffbbdd21SLinus Walleij 17638caab75fSGeert Uytterhoeven if (ctlr->running || ctlr->busy) { 17648caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1765ffbbdd21SLinus Walleij return -EBUSY; 1766ffbbdd21SLinus Walleij } 1767ffbbdd21SLinus Walleij 17688caab75fSGeert Uytterhoeven ctlr->running = true; 17698caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 17708caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1771ffbbdd21SLinus Walleij 177260a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1773ffbbdd21SLinus Walleij 1774ffbbdd21SLinus Walleij return 0; 1775ffbbdd21SLinus Walleij } 1776ffbbdd21SLinus Walleij 17778caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr) 1778ffbbdd21SLinus Walleij { 1779ffbbdd21SLinus Walleij unsigned long flags; 1780ffbbdd21SLinus Walleij unsigned limit = 500; 1781ffbbdd21SLinus Walleij int ret = 0; 1782ffbbdd21SLinus Walleij 17838caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1784ffbbdd21SLinus Walleij 1785ffbbdd21SLinus Walleij /* 1786ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 17878caab75fSGeert Uytterhoeven * A wait_queue on the ctlr->busy could be used, but then the common 1788ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 1789ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 1790ffbbdd21SLinus Walleij */ 17918caab75fSGeert Uytterhoeven while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 17928caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1793f97b26b0SAxel Lin usleep_range(10000, 11000); 17948caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1795ffbbdd21SLinus Walleij } 1796ffbbdd21SLinus Walleij 17978caab75fSGeert Uytterhoeven if (!list_empty(&ctlr->queue) || ctlr->busy) 1798ffbbdd21SLinus Walleij ret = -EBUSY; 1799ffbbdd21SLinus Walleij else 18008caab75fSGeert Uytterhoeven ctlr->running = false; 1801ffbbdd21SLinus Walleij 18028caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1803ffbbdd21SLinus Walleij 1804ffbbdd21SLinus Walleij if (ret) { 18058caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "could not stop message queue\n"); 1806ffbbdd21SLinus Walleij return ret; 1807ffbbdd21SLinus Walleij } 1808ffbbdd21SLinus Walleij return ret; 1809ffbbdd21SLinus Walleij } 1810ffbbdd21SLinus Walleij 18118caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr) 1812ffbbdd21SLinus Walleij { 1813ffbbdd21SLinus Walleij int ret; 1814ffbbdd21SLinus Walleij 18158caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 1816ffbbdd21SLinus Walleij 1817ffbbdd21SLinus Walleij /* 18183989144fSPetr Mladek * kthread_flush_worker will block until all work is done. 1819ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 1820ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 1821ffbbdd21SLinus Walleij * return anyway. 1822ffbbdd21SLinus Walleij */ 1823ffbbdd21SLinus Walleij if (ret) { 18248caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem destroying queue\n"); 1825ffbbdd21SLinus Walleij return ret; 1826ffbbdd21SLinus Walleij } 1827ffbbdd21SLinus Walleij 182860a883d1SMarek Szyprowski kthread_destroy_worker(ctlr->kworker); 1829ffbbdd21SLinus Walleij 1830ffbbdd21SLinus Walleij return 0; 1831ffbbdd21SLinus Walleij } 1832ffbbdd21SLinus Walleij 18330461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi, 18340461a414SMark Brown struct spi_message *msg, 18350461a414SMark Brown bool need_pump) 1836ffbbdd21SLinus Walleij { 18378caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 1838ffbbdd21SLinus Walleij unsigned long flags; 1839ffbbdd21SLinus Walleij 18408caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1841ffbbdd21SLinus Walleij 18428caab75fSGeert Uytterhoeven if (!ctlr->running) { 18438caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1844ffbbdd21SLinus Walleij return -ESHUTDOWN; 1845ffbbdd21SLinus Walleij } 1846ffbbdd21SLinus Walleij msg->actual_length = 0; 1847ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 1848ffbbdd21SLinus Walleij 18498caab75fSGeert Uytterhoeven list_add_tail(&msg->queue, &ctlr->queue); 1850f0125f1aSMark Brown if (!ctlr->busy && need_pump) 185160a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1852ffbbdd21SLinus Walleij 18538caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1854ffbbdd21SLinus Walleij return 0; 1855ffbbdd21SLinus Walleij } 1856ffbbdd21SLinus Walleij 18570461a414SMark Brown /** 18580461a414SMark Brown * spi_queued_transfer - transfer function for queued transfers 18590461a414SMark Brown * @spi: spi device which is requesting transfer 18600461a414SMark Brown * @msg: spi message which is to handled is queued to driver queue 186197d56dc6SJavier Martinez Canillas * 186297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 18630461a414SMark Brown */ 18640461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 18650461a414SMark Brown { 18660461a414SMark Brown return __spi_queued_transfer(spi, msg, true); 18670461a414SMark Brown } 18680461a414SMark Brown 18698caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr) 1870ffbbdd21SLinus Walleij { 1871ffbbdd21SLinus Walleij int ret; 1872ffbbdd21SLinus Walleij 18738caab75fSGeert Uytterhoeven ctlr->transfer = spi_queued_transfer; 18748caab75fSGeert Uytterhoeven if (!ctlr->transfer_one_message) 18758caab75fSGeert Uytterhoeven ctlr->transfer_one_message = spi_transfer_one_message; 1876ffbbdd21SLinus Walleij 1877ffbbdd21SLinus Walleij /* Initialize and start queue */ 18788caab75fSGeert Uytterhoeven ret = spi_init_queue(ctlr); 1879ffbbdd21SLinus Walleij if (ret) { 18808caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem initializing queue\n"); 1881ffbbdd21SLinus Walleij goto err_init_queue; 1882ffbbdd21SLinus Walleij } 18838caab75fSGeert Uytterhoeven ctlr->queued = true; 18848caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 1885ffbbdd21SLinus Walleij if (ret) { 18868caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem starting queue\n"); 1887ffbbdd21SLinus Walleij goto err_start_queue; 1888ffbbdd21SLinus Walleij } 1889ffbbdd21SLinus Walleij 1890ffbbdd21SLinus Walleij return 0; 1891ffbbdd21SLinus Walleij 1892ffbbdd21SLinus Walleij err_start_queue: 18938caab75fSGeert Uytterhoeven spi_destroy_queue(ctlr); 1894c3676d5cSMark Brown err_init_queue: 1895ffbbdd21SLinus Walleij return ret; 1896ffbbdd21SLinus Walleij } 1897ffbbdd21SLinus Walleij 1898988f259bSBoris Brezillon /** 1899988f259bSBoris Brezillon * spi_flush_queue - Send all pending messages in the queue from the callers' 1900988f259bSBoris Brezillon * context 1901988f259bSBoris Brezillon * @ctlr: controller to process queue for 1902988f259bSBoris Brezillon * 1903988f259bSBoris Brezillon * This should be used when one wants to ensure all pending messages have been 1904988f259bSBoris Brezillon * sent before doing something. Is used by the spi-mem code to make sure SPI 1905988f259bSBoris Brezillon * memory operations do not preempt regular SPI transfers that have been queued 1906988f259bSBoris Brezillon * before the spi-mem operation. 1907988f259bSBoris Brezillon */ 1908988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr) 1909988f259bSBoris Brezillon { 1910988f259bSBoris Brezillon if (ctlr->transfer == spi_queued_transfer) 1911988f259bSBoris Brezillon __spi_pump_messages(ctlr, false); 1912988f259bSBoris Brezillon } 1913988f259bSBoris Brezillon 1914ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 1915ffbbdd21SLinus Walleij 19167cb94361SAndreas Larsson #if defined(CONFIG_OF) 19178caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 1918c2e51ac3SGeert Uytterhoeven struct device_node *nc) 1919d57a4282SGrant Likely { 192089da4293STrent Piepho u32 value; 1921c2e51ac3SGeert Uytterhoeven int rc; 1922d57a4282SGrant Likely 1923d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 1924e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpha")) 1925d57a4282SGrant Likely spi->mode |= SPI_CPHA; 1926e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpol")) 1927d57a4282SGrant Likely spi->mode |= SPI_CPOL; 1928e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-3wire")) 1929c20151dfSLars-Peter Clausen spi->mode |= SPI_3WIRE; 1930e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-lsb-first")) 1931cd6339e6SZhao Qiang spi->mode |= SPI_LSB_FIRST; 19323e5ec1dbSGregory CLEMENT if (of_property_read_bool(nc, "spi-cs-high")) 1933f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 1934f3186dd8SLinus Walleij 1935f477b7fbSwangyuhang /* Device DUAL/QUAD mode */ 193689da4293STrent Piepho if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 193789da4293STrent Piepho switch (value) { 193889da4293STrent Piepho case 1: 1939f477b7fbSwangyuhang break; 194089da4293STrent Piepho case 2: 1941f477b7fbSwangyuhang spi->mode |= SPI_TX_DUAL; 1942f477b7fbSwangyuhang break; 194389da4293STrent Piepho case 4: 1944f477b7fbSwangyuhang spi->mode |= SPI_TX_QUAD; 1945f477b7fbSwangyuhang break; 19466b03061fSYogesh Narayan Gaur case 8: 19476b03061fSYogesh Narayan Gaur spi->mode |= SPI_TX_OCTAL; 19486b03061fSYogesh Narayan Gaur break; 1949f477b7fbSwangyuhang default: 19508caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 1951a110f93dSwangyuhang "spi-tx-bus-width %d not supported\n", 195289da4293STrent Piepho value); 195380874d8cSGeert Uytterhoeven break; 1954f477b7fbSwangyuhang } 1955a822e99cSMark Brown } 1956f477b7fbSwangyuhang 195789da4293STrent Piepho if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 195889da4293STrent Piepho switch (value) { 195989da4293STrent Piepho case 1: 1960f477b7fbSwangyuhang break; 196189da4293STrent Piepho case 2: 1962f477b7fbSwangyuhang spi->mode |= SPI_RX_DUAL; 1963f477b7fbSwangyuhang break; 196489da4293STrent Piepho case 4: 1965f477b7fbSwangyuhang spi->mode |= SPI_RX_QUAD; 1966f477b7fbSwangyuhang break; 19676b03061fSYogesh Narayan Gaur case 8: 19686b03061fSYogesh Narayan Gaur spi->mode |= SPI_RX_OCTAL; 19696b03061fSYogesh Narayan Gaur break; 1970f477b7fbSwangyuhang default: 19718caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 1972a110f93dSwangyuhang "spi-rx-bus-width %d not supported\n", 197389da4293STrent Piepho value); 197480874d8cSGeert Uytterhoeven break; 1975f477b7fbSwangyuhang } 1976a822e99cSMark Brown } 1977f477b7fbSwangyuhang 19788caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr)) { 1979194276b0SRob Herring if (!of_node_name_eq(nc, "slave")) { 198025c56c88SRob Herring dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 198125c56c88SRob Herring nc); 19826c364062SGeert Uytterhoeven return -EINVAL; 19836c364062SGeert Uytterhoeven } 19846c364062SGeert Uytterhoeven return 0; 19856c364062SGeert Uytterhoeven } 19866c364062SGeert Uytterhoeven 19876c364062SGeert Uytterhoeven /* Device address */ 19886c364062SGeert Uytterhoeven rc = of_property_read_u32(nc, "reg", &value); 19896c364062SGeert Uytterhoeven if (rc) { 199025c56c88SRob Herring dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 199125c56c88SRob Herring nc, rc); 19926c364062SGeert Uytterhoeven return rc; 19936c364062SGeert Uytterhoeven } 19946c364062SGeert Uytterhoeven spi->chip_select = value; 19956c364062SGeert Uytterhoeven 19963e5ec1dbSGregory CLEMENT /* 19973e5ec1dbSGregory CLEMENT * For descriptors associated with the device, polarity inversion is 19983e5ec1dbSGregory CLEMENT * handled in the gpiolib, so all gpio chip selects are "active high" 19993e5ec1dbSGregory CLEMENT * in the logical sense, the gpiolib will invert the line if need be. 20003e5ec1dbSGregory CLEMENT */ 200115f794bdSGregory CLEMENT if ((ctlr->use_gpio_descriptors) && ctlr->cs_gpiods && 200215f794bdSGregory CLEMENT ctlr->cs_gpiods[spi->chip_select]) 20033e5ec1dbSGregory CLEMENT spi->mode |= SPI_CS_HIGH; 20043e5ec1dbSGregory CLEMENT 2005d57a4282SGrant Likely /* Device speed */ 2006671c3bf5SChuanhong Guo if (!of_property_read_u32(nc, "spi-max-frequency", &value)) 200789da4293STrent Piepho spi->max_speed_hz = value; 2008d57a4282SGrant Likely 2009c2e51ac3SGeert Uytterhoeven return 0; 2010c2e51ac3SGeert Uytterhoeven } 2011c2e51ac3SGeert Uytterhoeven 2012c2e51ac3SGeert Uytterhoeven static struct spi_device * 20138caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 2014c2e51ac3SGeert Uytterhoeven { 2015c2e51ac3SGeert Uytterhoeven struct spi_device *spi; 2016c2e51ac3SGeert Uytterhoeven int rc; 2017c2e51ac3SGeert Uytterhoeven 2018c2e51ac3SGeert Uytterhoeven /* Alloc an spi_device */ 20198caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 2020c2e51ac3SGeert Uytterhoeven if (!spi) { 202125c56c88SRob Herring dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 2022c2e51ac3SGeert Uytterhoeven rc = -ENOMEM; 2023c2e51ac3SGeert Uytterhoeven goto err_out; 2024c2e51ac3SGeert Uytterhoeven } 2025c2e51ac3SGeert Uytterhoeven 2026c2e51ac3SGeert Uytterhoeven /* Select device driver */ 2027c2e51ac3SGeert Uytterhoeven rc = of_modalias_node(nc, spi->modalias, 2028c2e51ac3SGeert Uytterhoeven sizeof(spi->modalias)); 2029c2e51ac3SGeert Uytterhoeven if (rc < 0) { 203025c56c88SRob Herring dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 2031c2e51ac3SGeert Uytterhoeven goto err_out; 2032c2e51ac3SGeert Uytterhoeven } 2033c2e51ac3SGeert Uytterhoeven 20348caab75fSGeert Uytterhoeven rc = of_spi_parse_dt(ctlr, spi, nc); 2035c2e51ac3SGeert Uytterhoeven if (rc) 2036c2e51ac3SGeert Uytterhoeven goto err_out; 2037c2e51ac3SGeert Uytterhoeven 2038d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 2039d57a4282SGrant Likely of_node_get(nc); 2040d57a4282SGrant Likely spi->dev.of_node = nc; 2041d57a4282SGrant Likely 2042d57a4282SGrant Likely /* Register the new device */ 2043d57a4282SGrant Likely rc = spi_add_device(spi); 2044d57a4282SGrant Likely if (rc) { 204525c56c88SRob Herring dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 20468324147fSJohan Hovold goto err_of_node_put; 2047d57a4282SGrant Likely } 2048d57a4282SGrant Likely 2049aff5e3f8SPantelis Antoniou return spi; 2050aff5e3f8SPantelis Antoniou 20518324147fSJohan Hovold err_of_node_put: 20528324147fSJohan Hovold of_node_put(nc); 2053aff5e3f8SPantelis Antoniou err_out: 2054aff5e3f8SPantelis Antoniou spi_dev_put(spi); 2055aff5e3f8SPantelis Antoniou return ERR_PTR(rc); 2056aff5e3f8SPantelis Antoniou } 2057aff5e3f8SPantelis Antoniou 2058aff5e3f8SPantelis Antoniou /** 2059aff5e3f8SPantelis Antoniou * of_register_spi_devices() - Register child devices onto the SPI bus 20608caab75fSGeert Uytterhoeven * @ctlr: Pointer to spi_controller device 2061aff5e3f8SPantelis Antoniou * 20626c364062SGeert Uytterhoeven * Registers an spi_device for each child node of controller node which 20636c364062SGeert Uytterhoeven * represents a valid SPI slave. 2064aff5e3f8SPantelis Antoniou */ 20658caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) 2066aff5e3f8SPantelis Antoniou { 2067aff5e3f8SPantelis Antoniou struct spi_device *spi; 2068aff5e3f8SPantelis Antoniou struct device_node *nc; 2069aff5e3f8SPantelis Antoniou 20708caab75fSGeert Uytterhoeven if (!ctlr->dev.of_node) 2071aff5e3f8SPantelis Antoniou return; 2072aff5e3f8SPantelis Antoniou 20738caab75fSGeert Uytterhoeven for_each_available_child_of_node(ctlr->dev.of_node, nc) { 2074bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(nc, OF_POPULATED)) 2075bd6c1644SGeert Uytterhoeven continue; 20768caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, nc); 2077e0af98a7SRalf Ramsauer if (IS_ERR(spi)) { 20788caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 207925c56c88SRob Herring "Failed to create SPI device for %pOF\n", nc); 2080e0af98a7SRalf Ramsauer of_node_clear_flag(nc, OF_POPULATED); 2081e0af98a7SRalf Ramsauer } 2082d57a4282SGrant Likely } 2083d57a4282SGrant Likely } 2084d57a4282SGrant Likely #else 20858caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { } 2086d57a4282SGrant Likely #endif 2087d57a4282SGrant Likely 208864bee4d2SMika Westerberg #ifdef CONFIG_ACPI 20894c3c5954SArd Biesheuvel struct acpi_spi_lookup { 20904c3c5954SArd Biesheuvel struct spi_controller *ctlr; 20914c3c5954SArd Biesheuvel u32 max_speed_hz; 20924c3c5954SArd Biesheuvel u32 mode; 20934c3c5954SArd Biesheuvel int irq; 20944c3c5954SArd Biesheuvel u8 bits_per_word; 20954c3c5954SArd Biesheuvel u8 chip_select; 20964c3c5954SArd Biesheuvel }; 20974c3c5954SArd Biesheuvel 20984c3c5954SArd Biesheuvel static void acpi_spi_parse_apple_properties(struct acpi_device *dev, 20994c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup) 21008a2e487eSLukas Wunner { 21018a2e487eSLukas Wunner const union acpi_object *obj; 21028a2e487eSLukas Wunner 21038a2e487eSLukas Wunner if (!x86_apple_machine) 21048a2e487eSLukas Wunner return; 21058a2e487eSLukas Wunner 21068a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 21078a2e487eSLukas Wunner && obj->buffer.length >= 4) 21084c3c5954SArd Biesheuvel lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 21098a2e487eSLukas Wunner 21108a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 21118a2e487eSLukas Wunner && obj->buffer.length == 8) 21124c3c5954SArd Biesheuvel lookup->bits_per_word = *(u64 *)obj->buffer.pointer; 21138a2e487eSLukas Wunner 21148a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 21158a2e487eSLukas Wunner && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 21164c3c5954SArd Biesheuvel lookup->mode |= SPI_LSB_FIRST; 21178a2e487eSLukas Wunner 21188a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 21198a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 21204c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 21218a2e487eSLukas Wunner 21228a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 21238a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 21244c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 21258a2e487eSLukas Wunner } 21268a2e487eSLukas Wunner 212764bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 212864bee4d2SMika Westerberg { 21294c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup = data; 21304c3c5954SArd Biesheuvel struct spi_controller *ctlr = lookup->ctlr; 213164bee4d2SMika Westerberg 213264bee4d2SMika Westerberg if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 213364bee4d2SMika Westerberg struct acpi_resource_spi_serialbus *sb; 21344c3c5954SArd Biesheuvel acpi_handle parent_handle; 21354c3c5954SArd Biesheuvel acpi_status status; 213664bee4d2SMika Westerberg 213764bee4d2SMika Westerberg sb = &ares->data.spi_serial_bus; 213864bee4d2SMika Westerberg if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 21394c3c5954SArd Biesheuvel 21404c3c5954SArd Biesheuvel status = acpi_get_handle(NULL, 21414c3c5954SArd Biesheuvel sb->resource_source.string_ptr, 21424c3c5954SArd Biesheuvel &parent_handle); 21434c3c5954SArd Biesheuvel 2144b5e3cf41SArd Biesheuvel if (ACPI_FAILURE(status) || 21454c3c5954SArd Biesheuvel ACPI_HANDLE(ctlr->dev.parent) != parent_handle) 21464c3c5954SArd Biesheuvel return -ENODEV; 21474c3c5954SArd Biesheuvel 2148a0a90718SMika Westerberg /* 2149a0a90718SMika Westerberg * ACPI DeviceSelection numbering is handled by the 2150a0a90718SMika Westerberg * host controller driver in Windows and can vary 2151a0a90718SMika Westerberg * from driver to driver. In Linux we always expect 2152a0a90718SMika Westerberg * 0 .. max - 1 so we need to ask the driver to 2153a0a90718SMika Westerberg * translate between the two schemes. 2154a0a90718SMika Westerberg */ 21558caab75fSGeert Uytterhoeven if (ctlr->fw_translate_cs) { 21568caab75fSGeert Uytterhoeven int cs = ctlr->fw_translate_cs(ctlr, 2157a0a90718SMika Westerberg sb->device_selection); 2158a0a90718SMika Westerberg if (cs < 0) 2159a0a90718SMika Westerberg return cs; 21604c3c5954SArd Biesheuvel lookup->chip_select = cs; 2161a0a90718SMika Westerberg } else { 21624c3c5954SArd Biesheuvel lookup->chip_select = sb->device_selection; 2163a0a90718SMika Westerberg } 2164a0a90718SMika Westerberg 21654c3c5954SArd Biesheuvel lookup->max_speed_hz = sb->connection_speed; 21660dadde34SAndy Shevchenko lookup->bits_per_word = sb->data_bit_length; 216764bee4d2SMika Westerberg 216864bee4d2SMika Westerberg if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 21694c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 217064bee4d2SMika Westerberg if (sb->clock_polarity == ACPI_SPI_START_HIGH) 21714c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 217264bee4d2SMika Westerberg if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 21734c3c5954SArd Biesheuvel lookup->mode |= SPI_CS_HIGH; 217464bee4d2SMika Westerberg } 21754c3c5954SArd Biesheuvel } else if (lookup->irq < 0) { 217664bee4d2SMika Westerberg struct resource r; 217764bee4d2SMika Westerberg 217864bee4d2SMika Westerberg if (acpi_dev_resource_interrupt(ares, 0, &r)) 21794c3c5954SArd Biesheuvel lookup->irq = r.start; 218064bee4d2SMika Westerberg } 218164bee4d2SMika Westerberg 218264bee4d2SMika Westerberg /* Always tell the ACPI core to skip this resource */ 218364bee4d2SMika Westerberg return 1; 218464bee4d2SMika Westerberg } 218564bee4d2SMika Westerberg 21868caab75fSGeert Uytterhoeven static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 21877f24467fSOctavian Purdila struct acpi_device *adev) 218864bee4d2SMika Westerberg { 21894c3c5954SArd Biesheuvel acpi_handle parent_handle = NULL; 219064bee4d2SMika Westerberg struct list_head resource_list; 2191b28944c6SArd Biesheuvel struct acpi_spi_lookup lookup = {}; 219264bee4d2SMika Westerberg struct spi_device *spi; 219364bee4d2SMika Westerberg int ret; 219464bee4d2SMika Westerberg 21957f24467fSOctavian Purdila if (acpi_bus_get_status(adev) || !adev->status.present || 21967f24467fSOctavian Purdila acpi_device_enumerated(adev)) 219764bee4d2SMika Westerberg return AE_OK; 219864bee4d2SMika Westerberg 21994c3c5954SArd Biesheuvel lookup.ctlr = ctlr; 22004c3c5954SArd Biesheuvel lookup.irq = -1; 22014c3c5954SArd Biesheuvel 22024c3c5954SArd Biesheuvel INIT_LIST_HEAD(&resource_list); 22034c3c5954SArd Biesheuvel ret = acpi_dev_get_resources(adev, &resource_list, 22044c3c5954SArd Biesheuvel acpi_spi_add_resource, &lookup); 22054c3c5954SArd Biesheuvel acpi_dev_free_resource_list(&resource_list); 22064c3c5954SArd Biesheuvel 22074c3c5954SArd Biesheuvel if (ret < 0) 22084c3c5954SArd Biesheuvel /* found SPI in _CRS but it points to another controller */ 22094c3c5954SArd Biesheuvel return AE_OK; 22104c3c5954SArd Biesheuvel 22114c3c5954SArd Biesheuvel if (!lookup.max_speed_hz && 22124c3c5954SArd Biesheuvel !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) && 22134c3c5954SArd Biesheuvel ACPI_HANDLE(ctlr->dev.parent) == parent_handle) { 22144c3c5954SArd Biesheuvel /* Apple does not use _CRS but nested devices for SPI slaves */ 22154c3c5954SArd Biesheuvel acpi_spi_parse_apple_properties(adev, &lookup); 22164c3c5954SArd Biesheuvel } 22174c3c5954SArd Biesheuvel 22184c3c5954SArd Biesheuvel if (!lookup.max_speed_hz) 22194c3c5954SArd Biesheuvel return AE_OK; 22204c3c5954SArd Biesheuvel 22218caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 222264bee4d2SMika Westerberg if (!spi) { 22238caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", 222464bee4d2SMika Westerberg dev_name(&adev->dev)); 222564bee4d2SMika Westerberg return AE_NO_MEMORY; 222664bee4d2SMika Westerberg } 222764bee4d2SMika Westerberg 2228ea235786SJohn Garry 22297b199811SRafael J. Wysocki ACPI_COMPANION_SET(&spi->dev, adev); 22304c3c5954SArd Biesheuvel spi->max_speed_hz = lookup.max_speed_hz; 2231ea235786SJohn Garry spi->mode |= lookup.mode; 22324c3c5954SArd Biesheuvel spi->irq = lookup.irq; 22334c3c5954SArd Biesheuvel spi->bits_per_word = lookup.bits_per_word; 22344c3c5954SArd Biesheuvel spi->chip_select = lookup.chip_select; 223564bee4d2SMika Westerberg 22360c6543f6SDan O'Donovan acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 22370c6543f6SDan O'Donovan sizeof(spi->modalias)); 22380c6543f6SDan O'Donovan 223933ada67dSChristophe RICARD if (spi->irq < 0) 224033ada67dSChristophe RICARD spi->irq = acpi_dev_gpio_irq_get(adev, 0); 224133ada67dSChristophe RICARD 22427f24467fSOctavian Purdila acpi_device_set_enumerated(adev); 22437f24467fSOctavian Purdila 224433cf00e5SMika Westerberg adev->power.flags.ignore_parent = true; 224564bee4d2SMika Westerberg if (spi_add_device(spi)) { 224633cf00e5SMika Westerberg adev->power.flags.ignore_parent = false; 22478caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 224864bee4d2SMika Westerberg dev_name(&adev->dev)); 224964bee4d2SMika Westerberg spi_dev_put(spi); 225064bee4d2SMika Westerberg } 225164bee4d2SMika Westerberg 225264bee4d2SMika Westerberg return AE_OK; 225364bee4d2SMika Westerberg } 225464bee4d2SMika Westerberg 22557f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 22567f24467fSOctavian Purdila void *data, void **return_value) 22577f24467fSOctavian Purdila { 22588caab75fSGeert Uytterhoeven struct spi_controller *ctlr = data; 22597f24467fSOctavian Purdila struct acpi_device *adev; 22607f24467fSOctavian Purdila 22617f24467fSOctavian Purdila if (acpi_bus_get_device(handle, &adev)) 22627f24467fSOctavian Purdila return AE_OK; 22637f24467fSOctavian Purdila 22648caab75fSGeert Uytterhoeven return acpi_register_spi_device(ctlr, adev); 22657f24467fSOctavian Purdila } 22667f24467fSOctavian Purdila 22674c3c5954SArd Biesheuvel #define SPI_ACPI_ENUMERATE_MAX_DEPTH 32 22684c3c5954SArd Biesheuvel 22698caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr) 227064bee4d2SMika Westerberg { 227164bee4d2SMika Westerberg acpi_status status; 227264bee4d2SMika Westerberg acpi_handle handle; 227364bee4d2SMika Westerberg 22748caab75fSGeert Uytterhoeven handle = ACPI_HANDLE(ctlr->dev.parent); 227564bee4d2SMika Westerberg if (!handle) 227664bee4d2SMika Westerberg return; 227764bee4d2SMika Westerberg 22784c3c5954SArd Biesheuvel status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 22794c3c5954SArd Biesheuvel SPI_ACPI_ENUMERATE_MAX_DEPTH, 22808caab75fSGeert Uytterhoeven acpi_spi_add_device, NULL, ctlr, NULL); 228164bee4d2SMika Westerberg if (ACPI_FAILURE(status)) 22828caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 228364bee4d2SMika Westerberg } 228464bee4d2SMika Westerberg #else 22858caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 228664bee4d2SMika Westerberg #endif /* CONFIG_ACPI */ 228764bee4d2SMika Westerberg 22888caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev) 22898ae12a0dSDavid Brownell { 22908caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 22918ae12a0dSDavid Brownell 22928caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 22938caab75fSGeert Uytterhoeven kfree(ctlr); 22948ae12a0dSDavid Brownell } 22958ae12a0dSDavid Brownell 22968ae12a0dSDavid Brownell static struct class spi_master_class = { 22978ae12a0dSDavid Brownell .name = "spi_master", 22988ae12a0dSDavid Brownell .owner = THIS_MODULE, 22998caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 2300eca2ebc7SMartin Sperl .dev_groups = spi_master_groups, 23018ae12a0dSDavid Brownell }; 23028ae12a0dSDavid Brownell 23036c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE 23046c364062SGeert Uytterhoeven /** 23056c364062SGeert Uytterhoeven * spi_slave_abort - abort the ongoing transfer request on an SPI slave 23066c364062SGeert Uytterhoeven * controller 23076c364062SGeert Uytterhoeven * @spi: device used for the current transfer 23086c364062SGeert Uytterhoeven */ 23096c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi) 23106c364062SGeert Uytterhoeven { 23118caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 23126c364062SGeert Uytterhoeven 23138caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 23148caab75fSGeert Uytterhoeven return ctlr->slave_abort(ctlr); 23156c364062SGeert Uytterhoeven 23166c364062SGeert Uytterhoeven return -ENOTSUPP; 23176c364062SGeert Uytterhoeven } 23186c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort); 23196c364062SGeert Uytterhoeven 23206c364062SGeert Uytterhoeven static int match_true(struct device *dev, void *data) 23216c364062SGeert Uytterhoeven { 23226c364062SGeert Uytterhoeven return 1; 23236c364062SGeert Uytterhoeven } 23246c364062SGeert Uytterhoeven 2325cc8b4659SGeert Uytterhoeven static ssize_t slave_show(struct device *dev, struct device_attribute *attr, 2326cc8b4659SGeert Uytterhoeven char *buf) 23276c364062SGeert Uytterhoeven { 23288caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 23298caab75fSGeert Uytterhoeven dev); 23306c364062SGeert Uytterhoeven struct device *child; 23316c364062SGeert Uytterhoeven 23326c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 23336c364062SGeert Uytterhoeven return sprintf(buf, "%s\n", 23346c364062SGeert Uytterhoeven child ? to_spi_device(child)->modalias : NULL); 23356c364062SGeert Uytterhoeven } 23366c364062SGeert Uytterhoeven 2337cc8b4659SGeert Uytterhoeven static ssize_t slave_store(struct device *dev, struct device_attribute *attr, 2338cc8b4659SGeert Uytterhoeven const char *buf, size_t count) 23396c364062SGeert Uytterhoeven { 23408caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 23418caab75fSGeert Uytterhoeven dev); 23426c364062SGeert Uytterhoeven struct spi_device *spi; 23436c364062SGeert Uytterhoeven struct device *child; 23446c364062SGeert Uytterhoeven char name[32]; 23456c364062SGeert Uytterhoeven int rc; 23466c364062SGeert Uytterhoeven 23476c364062SGeert Uytterhoeven rc = sscanf(buf, "%31s", name); 23486c364062SGeert Uytterhoeven if (rc != 1 || !name[0]) 23496c364062SGeert Uytterhoeven return -EINVAL; 23506c364062SGeert Uytterhoeven 23516c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 23526c364062SGeert Uytterhoeven if (child) { 23536c364062SGeert Uytterhoeven /* Remove registered slave */ 23546c364062SGeert Uytterhoeven device_unregister(child); 23556c364062SGeert Uytterhoeven put_device(child); 23566c364062SGeert Uytterhoeven } 23576c364062SGeert Uytterhoeven 23586c364062SGeert Uytterhoeven if (strcmp(name, "(null)")) { 23596c364062SGeert Uytterhoeven /* Register new slave */ 23606c364062SGeert Uytterhoeven spi = spi_alloc_device(ctlr); 23616c364062SGeert Uytterhoeven if (!spi) 23626c364062SGeert Uytterhoeven return -ENOMEM; 23636c364062SGeert Uytterhoeven 23646c364062SGeert Uytterhoeven strlcpy(spi->modalias, name, sizeof(spi->modalias)); 23656c364062SGeert Uytterhoeven 23666c364062SGeert Uytterhoeven rc = spi_add_device(spi); 23676c364062SGeert Uytterhoeven if (rc) { 23686c364062SGeert Uytterhoeven spi_dev_put(spi); 23696c364062SGeert Uytterhoeven return rc; 23706c364062SGeert Uytterhoeven } 23716c364062SGeert Uytterhoeven } 23726c364062SGeert Uytterhoeven 23736c364062SGeert Uytterhoeven return count; 23746c364062SGeert Uytterhoeven } 23756c364062SGeert Uytterhoeven 2376cc8b4659SGeert Uytterhoeven static DEVICE_ATTR_RW(slave); 23776c364062SGeert Uytterhoeven 23786c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = { 23796c364062SGeert Uytterhoeven &dev_attr_slave.attr, 23806c364062SGeert Uytterhoeven NULL, 23816c364062SGeert Uytterhoeven }; 23826c364062SGeert Uytterhoeven 23836c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = { 23846c364062SGeert Uytterhoeven .attrs = spi_slave_attrs, 23856c364062SGeert Uytterhoeven }; 23866c364062SGeert Uytterhoeven 23876c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = { 23888caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 23896c364062SGeert Uytterhoeven &spi_slave_group, 23906c364062SGeert Uytterhoeven NULL, 23916c364062SGeert Uytterhoeven }; 23926c364062SGeert Uytterhoeven 23936c364062SGeert Uytterhoeven static struct class spi_slave_class = { 23946c364062SGeert Uytterhoeven .name = "spi_slave", 23956c364062SGeert Uytterhoeven .owner = THIS_MODULE, 23968caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 23976c364062SGeert Uytterhoeven .dev_groups = spi_slave_groups, 23986c364062SGeert Uytterhoeven }; 23996c364062SGeert Uytterhoeven #else 24006c364062SGeert Uytterhoeven extern struct class spi_slave_class; /* dummy */ 24016c364062SGeert Uytterhoeven #endif 24028ae12a0dSDavid Brownell 24038ae12a0dSDavid Brownell /** 24046c364062SGeert Uytterhoeven * __spi_alloc_controller - allocate an SPI master or slave controller 24058ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 240633e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 2407229e6af1SLukas Wunner * memory is in the driver_data field of the returned device, accessible 2408229e6af1SLukas Wunner * with spi_controller_get_devdata(); the memory is cacheline aligned; 2409229e6af1SLukas Wunner * drivers granting DMA access to portions of their private data need to 2410229e6af1SLukas Wunner * round up @size using ALIGN(size, dma_get_cache_alignment()). 24116c364062SGeert Uytterhoeven * @slave: flag indicating whether to allocate an SPI master (false) or SPI 24126c364062SGeert Uytterhoeven * slave (true) controller 241333e34dc6SDavid Brownell * Context: can sleep 24148ae12a0dSDavid Brownell * 24156c364062SGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 24168ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 24178caab75fSGeert Uytterhoeven * an spi_controller structure, prior to calling spi_register_controller(). 24188ae12a0dSDavid Brownell * 241997d56dc6SJavier Martinez Canillas * This must be called from context that can sleep. 24208ae12a0dSDavid Brownell * 24216c364062SGeert Uytterhoeven * The caller is responsible for assigning the bus number and initializing the 24228caab75fSGeert Uytterhoeven * controller's methods before calling spi_register_controller(); and (after 24238caab75fSGeert Uytterhoeven * errors adding the device) calling spi_controller_put() to prevent a memory 24248caab75fSGeert Uytterhoeven * leak. 242597d56dc6SJavier Martinez Canillas * 24266c364062SGeert Uytterhoeven * Return: the SPI controller structure on success, else NULL. 24278ae12a0dSDavid Brownell */ 24288caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev, 24296c364062SGeert Uytterhoeven unsigned int size, bool slave) 24308ae12a0dSDavid Brownell { 24318caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 2432229e6af1SLukas Wunner size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment()); 24338ae12a0dSDavid Brownell 24340c868461SDavid Brownell if (!dev) 24350c868461SDavid Brownell return NULL; 24360c868461SDavid Brownell 2437229e6af1SLukas Wunner ctlr = kzalloc(size + ctlr_size, GFP_KERNEL); 24388caab75fSGeert Uytterhoeven if (!ctlr) 24398ae12a0dSDavid Brownell return NULL; 24408ae12a0dSDavid Brownell 24418caab75fSGeert Uytterhoeven device_initialize(&ctlr->dev); 24428caab75fSGeert Uytterhoeven ctlr->bus_num = -1; 24438caab75fSGeert Uytterhoeven ctlr->num_chipselect = 1; 24448caab75fSGeert Uytterhoeven ctlr->slave = slave; 24456c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 24468caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_slave_class; 24476c364062SGeert Uytterhoeven else 24488caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_master_class; 24498caab75fSGeert Uytterhoeven ctlr->dev.parent = dev; 24508caab75fSGeert Uytterhoeven pm_suspend_ignore_children(&ctlr->dev, true); 2451229e6af1SLukas Wunner spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size); 24528ae12a0dSDavid Brownell 24538caab75fSGeert Uytterhoeven return ctlr; 24548ae12a0dSDavid Brownell } 24556c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller); 24568ae12a0dSDavid Brownell 245774317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF 245843004f31SLinus Walleij static int of_spi_get_gpio_numbers(struct spi_controller *ctlr) 245974317984SJean-Christophe PLAGNIOL-VILLARD { 2460e80beb27SGrant Likely int nb, i, *cs; 24618caab75fSGeert Uytterhoeven struct device_node *np = ctlr->dev.of_node; 246274317984SJean-Christophe PLAGNIOL-VILLARD 246374317984SJean-Christophe PLAGNIOL-VILLARD if (!np) 246474317984SJean-Christophe PLAGNIOL-VILLARD return 0; 246574317984SJean-Christophe PLAGNIOL-VILLARD 246674317984SJean-Christophe PLAGNIOL-VILLARD nb = of_gpio_named_count(np, "cs-gpios"); 24678caab75fSGeert Uytterhoeven ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 246874317984SJean-Christophe PLAGNIOL-VILLARD 24698ec5d84eSAndreas Larsson /* Return error only for an incorrectly formed cs-gpios property */ 24708ec5d84eSAndreas Larsson if (nb == 0 || nb == -ENOENT) 247174317984SJean-Christophe PLAGNIOL-VILLARD return 0; 24728ec5d84eSAndreas Larsson else if (nb < 0) 24738ec5d84eSAndreas Larsson return nb; 247474317984SJean-Christophe PLAGNIOL-VILLARD 2475a86854d0SKees Cook cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), 247674317984SJean-Christophe PLAGNIOL-VILLARD GFP_KERNEL); 24778caab75fSGeert Uytterhoeven ctlr->cs_gpios = cs; 247874317984SJean-Christophe PLAGNIOL-VILLARD 24798caab75fSGeert Uytterhoeven if (!ctlr->cs_gpios) 248074317984SJean-Christophe PLAGNIOL-VILLARD return -ENOMEM; 248174317984SJean-Christophe PLAGNIOL-VILLARD 24828caab75fSGeert Uytterhoeven for (i = 0; i < ctlr->num_chipselect; i++) 2483446411e1SAndreas Larsson cs[i] = -ENOENT; 248474317984SJean-Christophe PLAGNIOL-VILLARD 248574317984SJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < nb; i++) 248674317984SJean-Christophe PLAGNIOL-VILLARD cs[i] = of_get_named_gpio(np, "cs-gpios", i); 248774317984SJean-Christophe PLAGNIOL-VILLARD 248874317984SJean-Christophe PLAGNIOL-VILLARD return 0; 248974317984SJean-Christophe PLAGNIOL-VILLARD } 249074317984SJean-Christophe PLAGNIOL-VILLARD #else 249143004f31SLinus Walleij static int of_spi_get_gpio_numbers(struct spi_controller *ctlr) 249274317984SJean-Christophe PLAGNIOL-VILLARD { 249374317984SJean-Christophe PLAGNIOL-VILLARD return 0; 249474317984SJean-Christophe PLAGNIOL-VILLARD } 249574317984SJean-Christophe PLAGNIOL-VILLARD #endif 249674317984SJean-Christophe PLAGNIOL-VILLARD 2497f3186dd8SLinus Walleij /** 2498f3186dd8SLinus Walleij * spi_get_gpio_descs() - grab chip select GPIOs for the master 2499f3186dd8SLinus Walleij * @ctlr: The SPI master to grab GPIO descriptors for 2500f3186dd8SLinus Walleij */ 2501f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr) 2502f3186dd8SLinus Walleij { 2503f3186dd8SLinus Walleij int nb, i; 2504f3186dd8SLinus Walleij struct gpio_desc **cs; 2505f3186dd8SLinus Walleij struct device *dev = &ctlr->dev; 25067d93aecdSGeert Uytterhoeven unsigned long native_cs_mask = 0; 25077d93aecdSGeert Uytterhoeven unsigned int num_cs_gpios = 0; 2508f3186dd8SLinus Walleij 2509f3186dd8SLinus Walleij nb = gpiod_count(dev, "cs"); 2510f3186dd8SLinus Walleij ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2511f3186dd8SLinus Walleij 2512f3186dd8SLinus Walleij /* No GPIOs at all is fine, else return the error */ 2513f3186dd8SLinus Walleij if (nb == 0 || nb == -ENOENT) 2514f3186dd8SLinus Walleij return 0; 2515f3186dd8SLinus Walleij else if (nb < 0) 2516f3186dd8SLinus Walleij return nb; 2517f3186dd8SLinus Walleij 2518f3186dd8SLinus Walleij cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), 2519f3186dd8SLinus Walleij GFP_KERNEL); 2520f3186dd8SLinus Walleij if (!cs) 2521f3186dd8SLinus Walleij return -ENOMEM; 2522f3186dd8SLinus Walleij ctlr->cs_gpiods = cs; 2523f3186dd8SLinus Walleij 2524f3186dd8SLinus Walleij for (i = 0; i < nb; i++) { 2525f3186dd8SLinus Walleij /* 2526f3186dd8SLinus Walleij * Most chipselects are active low, the inverted 2527f3186dd8SLinus Walleij * semantics are handled by special quirks in gpiolib, 2528f3186dd8SLinus Walleij * so initializing them GPIOD_OUT_LOW here means 2529f3186dd8SLinus Walleij * "unasserted", in most cases this will drive the physical 2530f3186dd8SLinus Walleij * line high. 2531f3186dd8SLinus Walleij */ 2532f3186dd8SLinus Walleij cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, 2533f3186dd8SLinus Walleij GPIOD_OUT_LOW); 25341723fdecSGeert Uytterhoeven if (IS_ERR(cs[i])) 25351723fdecSGeert Uytterhoeven return PTR_ERR(cs[i]); 2536f3186dd8SLinus Walleij 2537f3186dd8SLinus Walleij if (cs[i]) { 2538f3186dd8SLinus Walleij /* 2539f3186dd8SLinus Walleij * If we find a CS GPIO, name it after the device and 2540f3186dd8SLinus Walleij * chip select line. 2541f3186dd8SLinus Walleij */ 2542f3186dd8SLinus Walleij char *gpioname; 2543f3186dd8SLinus Walleij 2544f3186dd8SLinus Walleij gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", 2545f3186dd8SLinus Walleij dev_name(dev), i); 2546f3186dd8SLinus Walleij if (!gpioname) 2547f3186dd8SLinus Walleij return -ENOMEM; 2548f3186dd8SLinus Walleij gpiod_set_consumer_name(cs[i], gpioname); 25497d93aecdSGeert Uytterhoeven num_cs_gpios++; 25507d93aecdSGeert Uytterhoeven continue; 2551f3186dd8SLinus Walleij } 25527d93aecdSGeert Uytterhoeven 25537d93aecdSGeert Uytterhoeven if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { 25547d93aecdSGeert Uytterhoeven dev_err(dev, "Invalid native chip select %d\n", i); 25557d93aecdSGeert Uytterhoeven return -EINVAL; 25567d93aecdSGeert Uytterhoeven } 25577d93aecdSGeert Uytterhoeven native_cs_mask |= BIT(i); 25587d93aecdSGeert Uytterhoeven } 25597d93aecdSGeert Uytterhoeven 25607d93aecdSGeert Uytterhoeven ctlr->unused_native_cs = ffz(native_cs_mask); 25617d93aecdSGeert Uytterhoeven if (num_cs_gpios && ctlr->max_native_cs && 25627d93aecdSGeert Uytterhoeven ctlr->unused_native_cs >= ctlr->max_native_cs) { 25637d93aecdSGeert Uytterhoeven dev_err(dev, "No unused native chip select available\n"); 25647d93aecdSGeert Uytterhoeven return -EINVAL; 2565f3186dd8SLinus Walleij } 2566f3186dd8SLinus Walleij 2567f3186dd8SLinus Walleij return 0; 2568f3186dd8SLinus Walleij } 2569f3186dd8SLinus Walleij 2570bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr) 2571bdf3a3b5SBoris Brezillon { 2572bdf3a3b5SBoris Brezillon /* 2573b5932f5cSBoris Brezillon * The controller may implement only the high-level SPI-memory like 2574b5932f5cSBoris Brezillon * operations if it does not support regular SPI transfers, and this is 2575b5932f5cSBoris Brezillon * valid use case. 2576b5932f5cSBoris Brezillon * If ->mem_ops is NULL, we request that at least one of the 2577b5932f5cSBoris Brezillon * ->transfer_xxx() method be implemented. 2578bdf3a3b5SBoris Brezillon */ 2579b5932f5cSBoris Brezillon if (ctlr->mem_ops) { 2580b5932f5cSBoris Brezillon if (!ctlr->mem_ops->exec_op) 2581bdf3a3b5SBoris Brezillon return -EINVAL; 2582b5932f5cSBoris Brezillon } else if (!ctlr->transfer && !ctlr->transfer_one && 2583b5932f5cSBoris Brezillon !ctlr->transfer_one_message) { 2584b5932f5cSBoris Brezillon return -EINVAL; 2585b5932f5cSBoris Brezillon } 2586bdf3a3b5SBoris Brezillon 2587bdf3a3b5SBoris Brezillon return 0; 2588bdf3a3b5SBoris Brezillon } 2589bdf3a3b5SBoris Brezillon 25908ae12a0dSDavid Brownell /** 25918caab75fSGeert Uytterhoeven * spi_register_controller - register SPI master or slave controller 25928caab75fSGeert Uytterhoeven * @ctlr: initialized master, originally from spi_alloc_master() or 25938caab75fSGeert Uytterhoeven * spi_alloc_slave() 259433e34dc6SDavid Brownell * Context: can sleep 25958ae12a0dSDavid Brownell * 25968caab75fSGeert Uytterhoeven * SPI controllers connect to their drivers using some non-SPI bus, 25978ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 25988caab75fSGeert Uytterhoeven * includes calling spi_register_controller() to hook up to this SPI bus glue. 25998ae12a0dSDavid Brownell * 26008ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 26018ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 26028ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 26038ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 26048ae12a0dSDavid Brownell * chip is at which address. 26058ae12a0dSDavid Brownell * 26068ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 26078caab75fSGeert Uytterhoeven * success, else a negative error code (dropping the controller's refcount). 26080c868461SDavid Brownell * After a successful return, the caller is responsible for calling 26098caab75fSGeert Uytterhoeven * spi_unregister_controller(). 261097d56dc6SJavier Martinez Canillas * 261197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 26128ae12a0dSDavid Brownell */ 26138caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr) 26148ae12a0dSDavid Brownell { 26158caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 26162b9603a0SFeng Tang struct boardinfo *bi; 2617b93318a2SSergei Shtylyov int status; 261842bdd706SLucas Stach int id, first_dynamic; 26198ae12a0dSDavid Brownell 26200c868461SDavid Brownell if (!dev) 26210c868461SDavid Brownell return -ENODEV; 26220c868461SDavid Brownell 2623bdf3a3b5SBoris Brezillon /* 2624bdf3a3b5SBoris Brezillon * Make sure all necessary hooks are implemented before registering 2625bdf3a3b5SBoris Brezillon * the SPI controller. 2626bdf3a3b5SBoris Brezillon */ 2627bdf3a3b5SBoris Brezillon status = spi_controller_check_ops(ctlr); 2628bdf3a3b5SBoris Brezillon if (status) 2629bdf3a3b5SBoris Brezillon return status; 2630bdf3a3b5SBoris Brezillon 263104b2d03aSGeert Uytterhoeven if (ctlr->bus_num >= 0) { 263204b2d03aSGeert Uytterhoeven /* devices with a fixed bus num must check-in with the num */ 263304b2d03aSGeert Uytterhoeven mutex_lock(&board_lock); 263404b2d03aSGeert Uytterhoeven id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 263504b2d03aSGeert Uytterhoeven ctlr->bus_num + 1, GFP_KERNEL); 263604b2d03aSGeert Uytterhoeven mutex_unlock(&board_lock); 263704b2d03aSGeert Uytterhoeven if (WARN(id < 0, "couldn't get idr")) 263804b2d03aSGeert Uytterhoeven return id == -ENOSPC ? -EBUSY : id; 263904b2d03aSGeert Uytterhoeven ctlr->bus_num = id; 264004b2d03aSGeert Uytterhoeven } else if (ctlr->dev.of_node) { 26419b61e302SSuniel Mahesh /* allocate dynamic bus number using Linux idr */ 26429b61e302SSuniel Mahesh id = of_alias_get_id(ctlr->dev.of_node, "spi"); 26439b61e302SSuniel Mahesh if (id >= 0) { 26449b61e302SSuniel Mahesh ctlr->bus_num = id; 26459b61e302SSuniel Mahesh mutex_lock(&board_lock); 26469b61e302SSuniel Mahesh id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 26479b61e302SSuniel Mahesh ctlr->bus_num + 1, GFP_KERNEL); 26489b61e302SSuniel Mahesh mutex_unlock(&board_lock); 26499b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 26509b61e302SSuniel Mahesh return id == -ENOSPC ? -EBUSY : id; 26519b61e302SSuniel Mahesh } 26529b61e302SSuniel Mahesh } 26538caab75fSGeert Uytterhoeven if (ctlr->bus_num < 0) { 265442bdd706SLucas Stach first_dynamic = of_alias_get_highest_id("spi"); 265542bdd706SLucas Stach if (first_dynamic < 0) 265642bdd706SLucas Stach first_dynamic = 0; 265742bdd706SLucas Stach else 265842bdd706SLucas Stach first_dynamic++; 265942bdd706SLucas Stach 26609b61e302SSuniel Mahesh mutex_lock(&board_lock); 266142bdd706SLucas Stach id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, 266242bdd706SLucas Stach 0, GFP_KERNEL); 26639b61e302SSuniel Mahesh mutex_unlock(&board_lock); 26649b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 26659b61e302SSuniel Mahesh return id; 26669b61e302SSuniel Mahesh ctlr->bus_num = id; 26678ae12a0dSDavid Brownell } 26688caab75fSGeert Uytterhoeven INIT_LIST_HEAD(&ctlr->queue); 26698caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->queue_lock); 26708caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->bus_lock_spinlock); 26718caab75fSGeert Uytterhoeven mutex_init(&ctlr->bus_lock_mutex); 26728caab75fSGeert Uytterhoeven mutex_init(&ctlr->io_mutex); 26738caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 26748caab75fSGeert Uytterhoeven init_completion(&ctlr->xfer_completion); 26758caab75fSGeert Uytterhoeven if (!ctlr->max_dma_len) 26768caab75fSGeert Uytterhoeven ctlr->max_dma_len = INT_MAX; 2677cf32b71eSErnst Schwab 26788ae12a0dSDavid Brownell /* register the device, then userspace will see it. 26798ae12a0dSDavid Brownell * registration fails if the bus ID is in use. 26808ae12a0dSDavid Brownell */ 26818caab75fSGeert Uytterhoeven dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 26820a919ae4SAndrey Smirnov 26830a919ae4SAndrey Smirnov if (!spi_controller_is_slave(ctlr)) { 26840a919ae4SAndrey Smirnov if (ctlr->use_gpio_descriptors) { 26850a919ae4SAndrey Smirnov status = spi_get_gpio_descs(ctlr); 26860a919ae4SAndrey Smirnov if (status) 2687f9981d4fSAaro Koskinen goto free_bus_id; 26880a919ae4SAndrey Smirnov /* 26890a919ae4SAndrey Smirnov * A controller using GPIO descriptors always 26900a919ae4SAndrey Smirnov * supports SPI_CS_HIGH if need be. 26910a919ae4SAndrey Smirnov */ 26920a919ae4SAndrey Smirnov ctlr->mode_bits |= SPI_CS_HIGH; 26930a919ae4SAndrey Smirnov } else { 26940a919ae4SAndrey Smirnov /* Legacy code path for GPIOs from DT */ 269543004f31SLinus Walleij status = of_spi_get_gpio_numbers(ctlr); 26960a919ae4SAndrey Smirnov if (status) 2697f9981d4fSAaro Koskinen goto free_bus_id; 26980a919ae4SAndrey Smirnov } 26990a919ae4SAndrey Smirnov } 27000a919ae4SAndrey Smirnov 2701f9481b08STudor Ambarus /* 2702f9481b08STudor Ambarus * Even if it's just one always-selected device, there must 2703f9481b08STudor Ambarus * be at least one chipselect. 2704f9481b08STudor Ambarus */ 2705f9981d4fSAaro Koskinen if (!ctlr->num_chipselect) { 2706f9981d4fSAaro Koskinen status = -EINVAL; 2707f9981d4fSAaro Koskinen goto free_bus_id; 2708f9981d4fSAaro Koskinen } 2709f9481b08STudor Ambarus 27108caab75fSGeert Uytterhoeven status = device_add(&ctlr->dev); 2711f9981d4fSAaro Koskinen if (status < 0) 2712f9981d4fSAaro Koskinen goto free_bus_id; 27139b61e302SSuniel Mahesh dev_dbg(dev, "registered %s %s\n", 27148caab75fSGeert Uytterhoeven spi_controller_is_slave(ctlr) ? "slave" : "master", 27159b61e302SSuniel Mahesh dev_name(&ctlr->dev)); 27168ae12a0dSDavid Brownell 2717b5932f5cSBoris Brezillon /* 2718b5932f5cSBoris Brezillon * If we're using a queued driver, start the queue. Note that we don't 2719b5932f5cSBoris Brezillon * need the queueing logic if the driver is only supporting high-level 2720b5932f5cSBoris Brezillon * memory operations. 2721b5932f5cSBoris Brezillon */ 2722b5932f5cSBoris Brezillon if (ctlr->transfer) { 27238caab75fSGeert Uytterhoeven dev_info(dev, "controller is unqueued, this is deprecated\n"); 2724b5932f5cSBoris Brezillon } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 27258caab75fSGeert Uytterhoeven status = spi_controller_initialize_queue(ctlr); 2726ffbbdd21SLinus Walleij if (status) { 27278caab75fSGeert Uytterhoeven device_del(&ctlr->dev); 2728f9981d4fSAaro Koskinen goto free_bus_id; 2729ffbbdd21SLinus Walleij } 2730ffbbdd21SLinus Walleij } 2731eca2ebc7SMartin Sperl /* add statistics */ 27328caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->statistics.lock); 2733ffbbdd21SLinus Walleij 27342b9603a0SFeng Tang mutex_lock(&board_lock); 27358caab75fSGeert Uytterhoeven list_add_tail(&ctlr->list, &spi_controller_list); 27362b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 27378caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 27382b9603a0SFeng Tang mutex_unlock(&board_lock); 27392b9603a0SFeng Tang 274064bee4d2SMika Westerberg /* Register devices from the device tree and ACPI */ 27418caab75fSGeert Uytterhoeven of_register_spi_devices(ctlr); 27428caab75fSGeert Uytterhoeven acpi_register_spi_devices(ctlr); 2743f9981d4fSAaro Koskinen return status; 2744f9981d4fSAaro Koskinen 2745f9981d4fSAaro Koskinen free_bus_id: 2746f9981d4fSAaro Koskinen mutex_lock(&board_lock); 2747f9981d4fSAaro Koskinen idr_remove(&spi_master_idr, ctlr->bus_num); 2748f9981d4fSAaro Koskinen mutex_unlock(&board_lock); 27498ae12a0dSDavid Brownell return status; 27508ae12a0dSDavid Brownell } 27518caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller); 27528ae12a0dSDavid Brownell 2753666d5b4cSMark Brown static void devm_spi_unregister(struct device *dev, void *res) 2754666d5b4cSMark Brown { 27558caab75fSGeert Uytterhoeven spi_unregister_controller(*(struct spi_controller **)res); 2756666d5b4cSMark Brown } 2757666d5b4cSMark Brown 2758666d5b4cSMark Brown /** 27598caab75fSGeert Uytterhoeven * devm_spi_register_controller - register managed SPI master or slave 27608caab75fSGeert Uytterhoeven * controller 27618caab75fSGeert Uytterhoeven * @dev: device managing SPI controller 27628caab75fSGeert Uytterhoeven * @ctlr: initialized controller, originally from spi_alloc_master() or 27638caab75fSGeert Uytterhoeven * spi_alloc_slave() 2764666d5b4cSMark Brown * Context: can sleep 2765666d5b4cSMark Brown * 27668caab75fSGeert Uytterhoeven * Register a SPI device as with spi_register_controller() which will 276768b892f1SJohan Hovold * automatically be unregistered and freed. 276897d56dc6SJavier Martinez Canillas * 276997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 2770666d5b4cSMark Brown */ 27718caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev, 27728caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 2773666d5b4cSMark Brown { 27748caab75fSGeert Uytterhoeven struct spi_controller **ptr; 2775666d5b4cSMark Brown int ret; 2776666d5b4cSMark Brown 2777666d5b4cSMark Brown ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); 2778666d5b4cSMark Brown if (!ptr) 2779666d5b4cSMark Brown return -ENOMEM; 2780666d5b4cSMark Brown 27818caab75fSGeert Uytterhoeven ret = spi_register_controller(ctlr); 27824b92894eSStephen Warren if (!ret) { 27838caab75fSGeert Uytterhoeven *ptr = ctlr; 2784666d5b4cSMark Brown devres_add(dev, ptr); 2785666d5b4cSMark Brown } else { 2786666d5b4cSMark Brown devres_free(ptr); 2787666d5b4cSMark Brown } 2788666d5b4cSMark Brown 2789666d5b4cSMark Brown return ret; 2790666d5b4cSMark Brown } 27918caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller); 2792666d5b4cSMark Brown 279334860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 27948ae12a0dSDavid Brownell { 27950c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 27968ae12a0dSDavid Brownell return 0; 27978ae12a0dSDavid Brownell } 27988ae12a0dSDavid Brownell 27998ae12a0dSDavid Brownell /** 28008caab75fSGeert Uytterhoeven * spi_unregister_controller - unregister SPI master or slave controller 28018caab75fSGeert Uytterhoeven * @ctlr: the controller being unregistered 280233e34dc6SDavid Brownell * Context: can sleep 28038ae12a0dSDavid Brownell * 28048caab75fSGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 28058ae12a0dSDavid Brownell * only ones directly touching chip registers. 28068ae12a0dSDavid Brownell * 28078ae12a0dSDavid Brownell * This must be called from context that can sleep. 280868b892f1SJohan Hovold * 280968b892f1SJohan Hovold * Note that this function also drops a reference to the controller. 28108ae12a0dSDavid Brownell */ 28118caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr) 28128ae12a0dSDavid Brownell { 28139b61e302SSuniel Mahesh struct spi_controller *found; 281467f7b278SJohan Hovold int id = ctlr->bus_num; 281589fc9a1aSJeff Garzik 2816ddf75be4SLukas Wunner /* Prevent addition of new devices, unregister existing ones */ 2817ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 2818ddf75be4SLukas Wunner mutex_lock(&spi_add_lock); 2819ddf75be4SLukas Wunner 282084855678SLukas Wunner device_for_each_child(&ctlr->dev, NULL, __unregister); 282184855678SLukas Wunner 28229b61e302SSuniel Mahesh /* First make sure that this controller was ever added */ 28239b61e302SSuniel Mahesh mutex_lock(&board_lock); 282467f7b278SJohan Hovold found = idr_find(&spi_master_idr, id); 28259b61e302SSuniel Mahesh mutex_unlock(&board_lock); 28268caab75fSGeert Uytterhoeven if (ctlr->queued) { 28278caab75fSGeert Uytterhoeven if (spi_destroy_queue(ctlr)) 28288caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue remove failed\n"); 2829ffbbdd21SLinus Walleij } 28302b9603a0SFeng Tang mutex_lock(&board_lock); 28318caab75fSGeert Uytterhoeven list_del(&ctlr->list); 28322b9603a0SFeng Tang mutex_unlock(&board_lock); 28332b9603a0SFeng Tang 28348caab75fSGeert Uytterhoeven device_unregister(&ctlr->dev); 28359b61e302SSuniel Mahesh /* free bus id */ 28369b61e302SSuniel Mahesh mutex_lock(&board_lock); 2837613bd1eaSJarkko Nikula if (found == ctlr) 283867f7b278SJohan Hovold idr_remove(&spi_master_idr, id); 28399b61e302SSuniel Mahesh mutex_unlock(&board_lock); 2840ddf75be4SLukas Wunner 2841ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 2842ddf75be4SLukas Wunner mutex_unlock(&spi_add_lock); 28438ae12a0dSDavid Brownell } 28448caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller); 28458ae12a0dSDavid Brownell 28468caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr) 2847ffbbdd21SLinus Walleij { 2848ffbbdd21SLinus Walleij int ret; 2849ffbbdd21SLinus Walleij 28508caab75fSGeert Uytterhoeven /* Basically no-ops for non-queued controllers */ 28518caab75fSGeert Uytterhoeven if (!ctlr->queued) 2852ffbbdd21SLinus Walleij return 0; 2853ffbbdd21SLinus Walleij 28548caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 2855ffbbdd21SLinus Walleij if (ret) 28568caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue stop failed\n"); 2857ffbbdd21SLinus Walleij 2858ffbbdd21SLinus Walleij return ret; 2859ffbbdd21SLinus Walleij } 28608caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend); 2861ffbbdd21SLinus Walleij 28628caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr) 2863ffbbdd21SLinus Walleij { 2864ffbbdd21SLinus Walleij int ret; 2865ffbbdd21SLinus Walleij 28668caab75fSGeert Uytterhoeven if (!ctlr->queued) 2867ffbbdd21SLinus Walleij return 0; 2868ffbbdd21SLinus Walleij 28698caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 2870ffbbdd21SLinus Walleij if (ret) 28718caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue restart failed\n"); 2872ffbbdd21SLinus Walleij 2873ffbbdd21SLinus Walleij return ret; 2874ffbbdd21SLinus Walleij } 28758caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume); 2876ffbbdd21SLinus Walleij 28778caab75fSGeert Uytterhoeven static int __spi_controller_match(struct device *dev, const void *data) 28785ed2c832SDave Young { 28798caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 28809f3b795aSMichał Mirosław const u16 *bus_num = data; 28815ed2c832SDave Young 28828caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 28838caab75fSGeert Uytterhoeven return ctlr->bus_num == *bus_num; 28845ed2c832SDave Young } 28855ed2c832SDave Young 28868ae12a0dSDavid Brownell /** 28878ae12a0dSDavid Brownell * spi_busnum_to_master - look up master associated with bus_num 28888ae12a0dSDavid Brownell * @bus_num: the master's bus number 288933e34dc6SDavid Brownell * Context: can sleep 28908ae12a0dSDavid Brownell * 28918ae12a0dSDavid Brownell * This call may be used with devices that are registered after 28928ae12a0dSDavid Brownell * arch init time. It returns a refcounted pointer to the relevant 28938caab75fSGeert Uytterhoeven * spi_controller (which the caller must release), or NULL if there is 28948ae12a0dSDavid Brownell * no such master registered. 289597d56dc6SJavier Martinez Canillas * 289697d56dc6SJavier Martinez Canillas * Return: the SPI master structure on success, else NULL. 28978ae12a0dSDavid Brownell */ 28988caab75fSGeert Uytterhoeven struct spi_controller *spi_busnum_to_master(u16 bus_num) 28998ae12a0dSDavid Brownell { 290049dce689STony Jones struct device *dev; 29018caab75fSGeert Uytterhoeven struct spi_controller *ctlr = NULL; 29028ae12a0dSDavid Brownell 2903695794aeSGreg Kroah-Hartman dev = class_find_device(&spi_master_class, NULL, &bus_num, 29048caab75fSGeert Uytterhoeven __spi_controller_match); 29055ed2c832SDave Young if (dev) 29068caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 29075ed2c832SDave Young /* reference got in class_find_device */ 29088caab75fSGeert Uytterhoeven return ctlr; 29098ae12a0dSDavid Brownell } 29108ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master); 29118ae12a0dSDavid Brownell 2912d780c371SMartin Sperl /*-------------------------------------------------------------------------*/ 2913d780c371SMartin Sperl 2914d780c371SMartin Sperl /* Core methods for SPI resource management */ 2915d780c371SMartin Sperl 2916d780c371SMartin Sperl /** 2917d780c371SMartin Sperl * spi_res_alloc - allocate a spi resource that is life-cycle managed 2918d780c371SMartin Sperl * during the processing of a spi_message while using 2919d780c371SMartin Sperl * spi_transfer_one 2920d780c371SMartin Sperl * @spi: the spi device for which we allocate memory 2921d780c371SMartin Sperl * @release: the release code to execute for this resource 2922d780c371SMartin Sperl * @size: size to alloc and return 2923d780c371SMartin Sperl * @gfp: GFP allocation flags 2924d780c371SMartin Sperl * 2925d780c371SMartin Sperl * Return: the pointer to the allocated data 2926d780c371SMartin Sperl * 2927d780c371SMartin Sperl * This may get enhanced in the future to allocate from a memory pool 29288caab75fSGeert Uytterhoeven * of the @spi_device or @spi_controller to avoid repeated allocations. 2929d780c371SMartin Sperl */ 2930d780c371SMartin Sperl void *spi_res_alloc(struct spi_device *spi, 2931d780c371SMartin Sperl spi_res_release_t release, 2932d780c371SMartin Sperl size_t size, gfp_t gfp) 2933d780c371SMartin Sperl { 2934d780c371SMartin Sperl struct spi_res *sres; 2935d780c371SMartin Sperl 2936d780c371SMartin Sperl sres = kzalloc(sizeof(*sres) + size, gfp); 2937d780c371SMartin Sperl if (!sres) 2938d780c371SMartin Sperl return NULL; 2939d780c371SMartin Sperl 2940d780c371SMartin Sperl INIT_LIST_HEAD(&sres->entry); 2941d780c371SMartin Sperl sres->release = release; 2942d780c371SMartin Sperl 2943d780c371SMartin Sperl return sres->data; 2944d780c371SMartin Sperl } 2945d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_alloc); 2946d780c371SMartin Sperl 2947d780c371SMartin Sperl /** 2948d780c371SMartin Sperl * spi_res_free - free an spi resource 2949d780c371SMartin Sperl * @res: pointer to the custom data of a resource 2950d780c371SMartin Sperl * 2951d780c371SMartin Sperl */ 2952d780c371SMartin Sperl void spi_res_free(void *res) 2953d780c371SMartin Sperl { 2954d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 2955d780c371SMartin Sperl 2956d780c371SMartin Sperl if (!res) 2957d780c371SMartin Sperl return; 2958d780c371SMartin Sperl 2959d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 2960d780c371SMartin Sperl kfree(sres); 2961d780c371SMartin Sperl } 2962d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_free); 2963d780c371SMartin Sperl 2964d780c371SMartin Sperl /** 2965d780c371SMartin Sperl * spi_res_add - add a spi_res to the spi_message 2966d780c371SMartin Sperl * @message: the spi message 2967d780c371SMartin Sperl * @res: the spi_resource 2968d780c371SMartin Sperl */ 2969d780c371SMartin Sperl void spi_res_add(struct spi_message *message, void *res) 2970d780c371SMartin Sperl { 2971d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 2972d780c371SMartin Sperl 2973d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 2974d780c371SMartin Sperl list_add_tail(&sres->entry, &message->resources); 2975d780c371SMartin Sperl } 2976d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_add); 2977d780c371SMartin Sperl 2978d780c371SMartin Sperl /** 2979d780c371SMartin Sperl * spi_res_release - release all spi resources for this message 29808caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller 2981d780c371SMartin Sperl * @message: the @spi_message 2982d780c371SMartin Sperl */ 29838caab75fSGeert Uytterhoeven void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 2984d780c371SMartin Sperl { 2985f5694369SVladimir Zapolskiy struct spi_res *res, *tmp; 2986d780c371SMartin Sperl 2987f5694369SVladimir Zapolskiy list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { 2988d780c371SMartin Sperl if (res->release) 29898caab75fSGeert Uytterhoeven res->release(ctlr, message, res->data); 2990d780c371SMartin Sperl 2991d780c371SMartin Sperl list_del(&res->entry); 2992d780c371SMartin Sperl 2993d780c371SMartin Sperl kfree(res); 2994d780c371SMartin Sperl } 2995d780c371SMartin Sperl } 2996d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_release); 29978ae12a0dSDavid Brownell 29988ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 29998ae12a0dSDavid Brownell 3000523baf5aSMartin Sperl /* Core methods for spi_message alterations */ 3001523baf5aSMartin Sperl 30028caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr, 3003523baf5aSMartin Sperl struct spi_message *msg, 3004523baf5aSMartin Sperl void *res) 3005523baf5aSMartin Sperl { 3006523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer = res; 3007523baf5aSMartin Sperl size_t i; 3008523baf5aSMartin Sperl 3009523baf5aSMartin Sperl /* call extra callback if requested */ 3010523baf5aSMartin Sperl if (rxfer->release) 30118caab75fSGeert Uytterhoeven rxfer->release(ctlr, msg, res); 3012523baf5aSMartin Sperl 3013523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 3014523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 3015523baf5aSMartin Sperl 3016523baf5aSMartin Sperl /* remove the formerly inserted entries */ 3017523baf5aSMartin Sperl for (i = 0; i < rxfer->inserted; i++) 3018523baf5aSMartin Sperl list_del(&rxfer->inserted_transfers[i].transfer_list); 3019523baf5aSMartin Sperl } 3020523baf5aSMartin Sperl 3021523baf5aSMartin Sperl /** 3022523baf5aSMartin Sperl * spi_replace_transfers - replace transfers with several transfers 3023523baf5aSMartin Sperl * and register change with spi_message.resources 3024523baf5aSMartin Sperl * @msg: the spi_message we work upon 3025523baf5aSMartin Sperl * @xfer_first: the first spi_transfer we want to replace 3026523baf5aSMartin Sperl * @remove: number of transfers to remove 3027523baf5aSMartin Sperl * @insert: the number of transfers we want to insert instead 3028523baf5aSMartin Sperl * @release: extra release code necessary in some circumstances 3029523baf5aSMartin Sperl * @extradatasize: extra data to allocate (with alignment guarantees 3030523baf5aSMartin Sperl * of struct @spi_transfer) 303105885397SMartin Sperl * @gfp: gfp flags 3032523baf5aSMartin Sperl * 3033523baf5aSMartin Sperl * Returns: pointer to @spi_replaced_transfers, 3034523baf5aSMartin Sperl * PTR_ERR(...) in case of errors. 3035523baf5aSMartin Sperl */ 3036523baf5aSMartin Sperl struct spi_replaced_transfers *spi_replace_transfers( 3037523baf5aSMartin Sperl struct spi_message *msg, 3038523baf5aSMartin Sperl struct spi_transfer *xfer_first, 3039523baf5aSMartin Sperl size_t remove, 3040523baf5aSMartin Sperl size_t insert, 3041523baf5aSMartin Sperl spi_replaced_release_t release, 3042523baf5aSMartin Sperl size_t extradatasize, 3043523baf5aSMartin Sperl gfp_t gfp) 3044523baf5aSMartin Sperl { 3045523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer; 3046523baf5aSMartin Sperl struct spi_transfer *xfer; 3047523baf5aSMartin Sperl size_t i; 3048523baf5aSMartin Sperl 3049523baf5aSMartin Sperl /* allocate the structure using spi_res */ 3050523baf5aSMartin Sperl rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 3051aef97522SGustavo A. R. Silva struct_size(rxfer, inserted_transfers, insert) 3052523baf5aSMartin Sperl + extradatasize, 3053523baf5aSMartin Sperl gfp); 3054523baf5aSMartin Sperl if (!rxfer) 3055523baf5aSMartin Sperl return ERR_PTR(-ENOMEM); 3056523baf5aSMartin Sperl 3057523baf5aSMartin Sperl /* the release code to invoke before running the generic release */ 3058523baf5aSMartin Sperl rxfer->release = release; 3059523baf5aSMartin Sperl 3060523baf5aSMartin Sperl /* assign extradata */ 3061523baf5aSMartin Sperl if (extradatasize) 3062523baf5aSMartin Sperl rxfer->extradata = 3063523baf5aSMartin Sperl &rxfer->inserted_transfers[insert]; 3064523baf5aSMartin Sperl 3065523baf5aSMartin Sperl /* init the replaced_transfers list */ 3066523baf5aSMartin Sperl INIT_LIST_HEAD(&rxfer->replaced_transfers); 3067523baf5aSMartin Sperl 3068523baf5aSMartin Sperl /* assign the list_entry after which we should reinsert 3069523baf5aSMartin Sperl * the @replaced_transfers - it may be spi_message.messages! 3070523baf5aSMartin Sperl */ 3071523baf5aSMartin Sperl rxfer->replaced_after = xfer_first->transfer_list.prev; 3072523baf5aSMartin Sperl 3073523baf5aSMartin Sperl /* remove the requested number of transfers */ 3074523baf5aSMartin Sperl for (i = 0; i < remove; i++) { 3075523baf5aSMartin Sperl /* if the entry after replaced_after it is msg->transfers 3076523baf5aSMartin Sperl * then we have been requested to remove more transfers 3077523baf5aSMartin Sperl * than are in the list 3078523baf5aSMartin Sperl */ 3079523baf5aSMartin Sperl if (rxfer->replaced_after->next == &msg->transfers) { 3080523baf5aSMartin Sperl dev_err(&msg->spi->dev, 3081523baf5aSMartin Sperl "requested to remove more spi_transfers than are available\n"); 3082523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 3083523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, 3084523baf5aSMartin Sperl rxfer->replaced_after); 3085523baf5aSMartin Sperl 3086523baf5aSMartin Sperl /* free the spi_replace_transfer structure */ 3087523baf5aSMartin Sperl spi_res_free(rxfer); 3088523baf5aSMartin Sperl 3089523baf5aSMartin Sperl /* and return with an error */ 3090523baf5aSMartin Sperl return ERR_PTR(-EINVAL); 3091523baf5aSMartin Sperl } 3092523baf5aSMartin Sperl 3093523baf5aSMartin Sperl /* remove the entry after replaced_after from list of 3094523baf5aSMartin Sperl * transfers and add it to list of replaced_transfers 3095523baf5aSMartin Sperl */ 3096523baf5aSMartin Sperl list_move_tail(rxfer->replaced_after->next, 3097523baf5aSMartin Sperl &rxfer->replaced_transfers); 3098523baf5aSMartin Sperl } 3099523baf5aSMartin Sperl 3100523baf5aSMartin Sperl /* create copy of the given xfer with identical settings 3101523baf5aSMartin Sperl * based on the first transfer to get removed 3102523baf5aSMartin Sperl */ 3103523baf5aSMartin Sperl for (i = 0; i < insert; i++) { 3104523baf5aSMartin Sperl /* we need to run in reverse order */ 3105523baf5aSMartin Sperl xfer = &rxfer->inserted_transfers[insert - 1 - i]; 3106523baf5aSMartin Sperl 3107523baf5aSMartin Sperl /* copy all spi_transfer data */ 3108523baf5aSMartin Sperl memcpy(xfer, xfer_first, sizeof(*xfer)); 3109523baf5aSMartin Sperl 3110523baf5aSMartin Sperl /* add to list */ 3111523baf5aSMartin Sperl list_add(&xfer->transfer_list, rxfer->replaced_after); 3112523baf5aSMartin Sperl 3113bebcfd27SAlexandru Ardelean /* clear cs_change and delay for all but the last */ 3114523baf5aSMartin Sperl if (i) { 3115523baf5aSMartin Sperl xfer->cs_change = false; 3116523baf5aSMartin Sperl xfer->delay_usecs = 0; 3117bebcfd27SAlexandru Ardelean xfer->delay.value = 0; 3118523baf5aSMartin Sperl } 3119523baf5aSMartin Sperl } 3120523baf5aSMartin Sperl 3121523baf5aSMartin Sperl /* set up inserted */ 3122523baf5aSMartin Sperl rxfer->inserted = insert; 3123523baf5aSMartin Sperl 3124523baf5aSMartin Sperl /* and register it with spi_res/spi_message */ 3125523baf5aSMartin Sperl spi_res_add(msg, rxfer); 3126523baf5aSMartin Sperl 3127523baf5aSMartin Sperl return rxfer; 3128523baf5aSMartin Sperl } 3129523baf5aSMartin Sperl EXPORT_SYMBOL_GPL(spi_replace_transfers); 3130523baf5aSMartin Sperl 31318caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 3132d9f12122SMartin Sperl struct spi_message *msg, 3133d9f12122SMartin Sperl struct spi_transfer **xferp, 3134d9f12122SMartin Sperl size_t maxsize, 3135d9f12122SMartin Sperl gfp_t gfp) 3136d9f12122SMartin Sperl { 3137d9f12122SMartin Sperl struct spi_transfer *xfer = *xferp, *xfers; 3138d9f12122SMartin Sperl struct spi_replaced_transfers *srt; 3139d9f12122SMartin Sperl size_t offset; 3140d9f12122SMartin Sperl size_t count, i; 3141d9f12122SMartin Sperl 3142d9f12122SMartin Sperl /* calculate how many we have to replace */ 3143d9f12122SMartin Sperl count = DIV_ROUND_UP(xfer->len, maxsize); 3144d9f12122SMartin Sperl 3145d9f12122SMartin Sperl /* create replacement */ 3146d9f12122SMartin Sperl srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 3147657d32efSDan Carpenter if (IS_ERR(srt)) 3148657d32efSDan Carpenter return PTR_ERR(srt); 3149d9f12122SMartin Sperl xfers = srt->inserted_transfers; 3150d9f12122SMartin Sperl 3151d9f12122SMartin Sperl /* now handle each of those newly inserted spi_transfers 3152d9f12122SMartin Sperl * note that the replacements spi_transfers all are preset 3153d9f12122SMartin Sperl * to the same values as *xferp, so tx_buf, rx_buf and len 3154d9f12122SMartin Sperl * are all identical (as well as most others) 3155d9f12122SMartin Sperl * so we just have to fix up len and the pointers. 3156d9f12122SMartin Sperl * 3157d9f12122SMartin Sperl * this also includes support for the depreciated 3158d9f12122SMartin Sperl * spi_message.is_dma_mapped interface 3159d9f12122SMartin Sperl */ 3160d9f12122SMartin Sperl 3161d9f12122SMartin Sperl /* the first transfer just needs the length modified, so we 3162d9f12122SMartin Sperl * run it outside the loop 3163d9f12122SMartin Sperl */ 3164c8dab77aSFabio Estevam xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 3165d9f12122SMartin Sperl 3166d9f12122SMartin Sperl /* all the others need rx_buf/tx_buf also set */ 3167d9f12122SMartin Sperl for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 3168d9f12122SMartin Sperl /* update rx_buf, tx_buf and dma */ 3169d9f12122SMartin Sperl if (xfers[i].rx_buf) 3170d9f12122SMartin Sperl xfers[i].rx_buf += offset; 3171d9f12122SMartin Sperl if (xfers[i].rx_dma) 3172d9f12122SMartin Sperl xfers[i].rx_dma += offset; 3173d9f12122SMartin Sperl if (xfers[i].tx_buf) 3174d9f12122SMartin Sperl xfers[i].tx_buf += offset; 3175d9f12122SMartin Sperl if (xfers[i].tx_dma) 3176d9f12122SMartin Sperl xfers[i].tx_dma += offset; 3177d9f12122SMartin Sperl 3178d9f12122SMartin Sperl /* update length */ 3179d9f12122SMartin Sperl xfers[i].len = min(maxsize, xfers[i].len - offset); 3180d9f12122SMartin Sperl } 3181d9f12122SMartin Sperl 3182d9f12122SMartin Sperl /* we set up xferp to the last entry we have inserted, 3183d9f12122SMartin Sperl * so that we skip those already split transfers 3184d9f12122SMartin Sperl */ 3185d9f12122SMartin Sperl *xferp = &xfers[count - 1]; 3186d9f12122SMartin Sperl 3187d9f12122SMartin Sperl /* increment statistics counters */ 31888caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3189d9f12122SMartin Sperl transfers_split_maxsize); 3190d9f12122SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, 3191d9f12122SMartin Sperl transfers_split_maxsize); 3192d9f12122SMartin Sperl 3193d9f12122SMartin Sperl return 0; 3194d9f12122SMartin Sperl } 3195d9f12122SMartin Sperl 3196d9f12122SMartin Sperl /** 3197ce2424d7SMauro Carvalho Chehab * spi_split_transfers_maxsize - split spi transfers into multiple transfers 3198d9f12122SMartin Sperl * when an individual transfer exceeds a 3199d9f12122SMartin Sperl * certain size 32008caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller for this transfer 32013700ce95SMasanari Iida * @msg: the @spi_message to transform 32023700ce95SMasanari Iida * @maxsize: the maximum when to apply this 320310f11a22SJavier Martinez Canillas * @gfp: GFP allocation flags 3204d9f12122SMartin Sperl * 3205d9f12122SMartin Sperl * Return: status of transformation 3206d9f12122SMartin Sperl */ 32078caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr, 3208d9f12122SMartin Sperl struct spi_message *msg, 3209d9f12122SMartin Sperl size_t maxsize, 3210d9f12122SMartin Sperl gfp_t gfp) 3211d9f12122SMartin Sperl { 3212d9f12122SMartin Sperl struct spi_transfer *xfer; 3213d9f12122SMartin Sperl int ret; 3214d9f12122SMartin Sperl 3215d9f12122SMartin Sperl /* iterate over the transfer_list, 3216d9f12122SMartin Sperl * but note that xfer is advanced to the last transfer inserted 3217d9f12122SMartin Sperl * to avoid checking sizes again unnecessarily (also xfer does 3218d9f12122SMartin Sperl * potentiall belong to a different list by the time the 3219d9f12122SMartin Sperl * replacement has happened 3220d9f12122SMartin Sperl */ 3221d9f12122SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 3222d9f12122SMartin Sperl if (xfer->len > maxsize) { 32238caab75fSGeert Uytterhoeven ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 32248caab75fSGeert Uytterhoeven maxsize, gfp); 3225d9f12122SMartin Sperl if (ret) 3226d9f12122SMartin Sperl return ret; 3227d9f12122SMartin Sperl } 3228d9f12122SMartin Sperl } 3229d9f12122SMartin Sperl 3230d9f12122SMartin Sperl return 0; 3231d9f12122SMartin Sperl } 3232d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 32338ae12a0dSDavid Brownell 32348ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 32358ae12a0dSDavid Brownell 32368caab75fSGeert Uytterhoeven /* Core methods for SPI controller protocol drivers. Some of the 32377d077197SDavid Brownell * other core methods are currently defined as inline functions. 32387d077197SDavid Brownell */ 32397d077197SDavid Brownell 32408caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 32418caab75fSGeert Uytterhoeven u8 bits_per_word) 324263ab645fSStefan Brüns { 32438caab75fSGeert Uytterhoeven if (ctlr->bits_per_word_mask) { 324463ab645fSStefan Brüns /* Only 32 bits fit in the mask */ 324563ab645fSStefan Brüns if (bits_per_word > 32) 324663ab645fSStefan Brüns return -EINVAL; 32478caab75fSGeert Uytterhoeven if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 324863ab645fSStefan Brüns return -EINVAL; 324963ab645fSStefan Brüns } 325063ab645fSStefan Brüns 325163ab645fSStefan Brüns return 0; 325263ab645fSStefan Brüns } 325363ab645fSStefan Brüns 32547d077197SDavid Brownell /** 32557d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 32567d077197SDavid Brownell * @spi: the device whose settings are being modified 32577d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 32587d077197SDavid Brownell * 32597d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 32607d077197SDavid Brownell * device doesn't work with its default. They may likewise need 32617d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 32627d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 32637d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 32647d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 32657d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 32667d077197SDavid Brownell * 32677d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 32687d077197SDavid Brownell * that the underlying controller or its driver does not support. For 32697d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 32707d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 327197d56dc6SJavier Martinez Canillas * 327297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 32737d077197SDavid Brownell */ 32747d077197SDavid Brownell int spi_setup(struct spi_device *spi) 32757d077197SDavid Brownell { 327683596fbeSGeert Uytterhoeven unsigned bad_bits, ugly_bits; 32775ab8d262SAndy Shevchenko int status; 32787d077197SDavid Brownell 3279f477b7fbSwangyuhang /* check mode to prevent that DUAL and QUAD set at the same time 3280f477b7fbSwangyuhang */ 3281f477b7fbSwangyuhang if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || 3282f477b7fbSwangyuhang ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { 3283f477b7fbSwangyuhang dev_err(&spi->dev, 3284f477b7fbSwangyuhang "setup: can not select dual and quad at the same time\n"); 3285f477b7fbSwangyuhang return -EINVAL; 3286f477b7fbSwangyuhang } 3287f477b7fbSwangyuhang /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden 3288f477b7fbSwangyuhang */ 3289f477b7fbSwangyuhang if ((spi->mode & SPI_3WIRE) && (spi->mode & 32906b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 32916b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 3292f477b7fbSwangyuhang return -EINVAL; 3293e7db06b5SDavid Brownell /* help drivers fail *cleanly* when they need options 32948caab75fSGeert Uytterhoeven * that aren't supported with their current controller 3295cbaa62e0SDavid Lechner * SPI_CS_WORD has a fallback software implementation, 3296cbaa62e0SDavid Lechner * so it is ignored here. 3297e7db06b5SDavid Brownell */ 3298cbaa62e0SDavid Lechner bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); 3299d61ad23cSSerge Semin /* nothing prevents from working with active-high CS in case if it 3300d61ad23cSSerge Semin * is driven by GPIO. 3301d61ad23cSSerge Semin */ 3302d61ad23cSSerge Semin if (gpio_is_valid(spi->cs_gpio)) 3303d61ad23cSSerge Semin bad_bits &= ~SPI_CS_HIGH; 330483596fbeSGeert Uytterhoeven ugly_bits = bad_bits & 33056b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 33066b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); 330783596fbeSGeert Uytterhoeven if (ugly_bits) { 330883596fbeSGeert Uytterhoeven dev_warn(&spi->dev, 330983596fbeSGeert Uytterhoeven "setup: ignoring unsupported mode bits %x\n", 331083596fbeSGeert Uytterhoeven ugly_bits); 331183596fbeSGeert Uytterhoeven spi->mode &= ~ugly_bits; 331283596fbeSGeert Uytterhoeven bad_bits &= ~ugly_bits; 331383596fbeSGeert Uytterhoeven } 3314e7db06b5SDavid Brownell if (bad_bits) { 3315eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 3316e7db06b5SDavid Brownell bad_bits); 3317e7db06b5SDavid Brownell return -EINVAL; 3318e7db06b5SDavid Brownell } 3319e7db06b5SDavid Brownell 33207d077197SDavid Brownell if (!spi->bits_per_word) 33217d077197SDavid Brownell spi->bits_per_word = 8; 33227d077197SDavid Brownell 33238caab75fSGeert Uytterhoeven status = __spi_validate_bits_per_word(spi->controller, 33248caab75fSGeert Uytterhoeven spi->bits_per_word); 33255ab8d262SAndy Shevchenko if (status) 33265ab8d262SAndy Shevchenko return status; 332763ab645fSStefan Brüns 3328052eb2d4SAxel Lin if (!spi->max_speed_hz) 33298caab75fSGeert Uytterhoeven spi->max_speed_hz = spi->controller->max_speed_hz; 3330052eb2d4SAxel Lin 33318caab75fSGeert Uytterhoeven if (spi->controller->setup) 33328caab75fSGeert Uytterhoeven status = spi->controller->setup(spi); 33337d077197SDavid Brownell 3334d948e6caSLuhua Xu if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { 3335d948e6caSLuhua Xu status = pm_runtime_get_sync(spi->controller->dev.parent); 3336d948e6caSLuhua Xu if (status < 0) { 3337d948e6caSLuhua Xu pm_runtime_put_noidle(spi->controller->dev.parent); 3338d948e6caSLuhua Xu dev_err(&spi->controller->dev, "Failed to power device: %d\n", 3339d948e6caSLuhua Xu status); 3340d948e6caSLuhua Xu return status; 3341d948e6caSLuhua Xu } 334257a94607STony Lindgren 334357a94607STony Lindgren /* 334457a94607STony Lindgren * We do not want to return positive value from pm_runtime_get, 334557a94607STony Lindgren * there are many instances of devices calling spi_setup() and 334657a94607STony Lindgren * checking for a non-zero return value instead of a negative 334757a94607STony Lindgren * return value. 334857a94607STony Lindgren */ 334957a94607STony Lindgren status = 0; 335057a94607STony Lindgren 3351abeedb01SFranklin S Cooper Jr spi_set_cs(spi, false); 3352d948e6caSLuhua Xu pm_runtime_mark_last_busy(spi->controller->dev.parent); 3353d948e6caSLuhua Xu pm_runtime_put_autosuspend(spi->controller->dev.parent); 3354d948e6caSLuhua Xu } else { 3355d948e6caSLuhua Xu spi_set_cs(spi, false); 3356d948e6caSLuhua Xu } 3357abeedb01SFranklin S Cooper Jr 3358924b5867SDouglas Anderson if (spi->rt && !spi->controller->rt) { 3359924b5867SDouglas Anderson spi->controller->rt = true; 3360924b5867SDouglas Anderson spi_set_thread_rt(spi->controller); 3361924b5867SDouglas Anderson } 3362924b5867SDouglas Anderson 33635fe5f05eSJingoo Han dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 33647d077197SDavid Brownell (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), 33657d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 33667d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 33677d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 33687d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 33697d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 33707d077197SDavid Brownell status); 33717d077197SDavid Brownell 33727d077197SDavid Brownell return status; 33737d077197SDavid Brownell } 33747d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 33757d077197SDavid Brownell 3376f1ca9992SSowjanya Komatineni /** 3377f1ca9992SSowjanya Komatineni * spi_set_cs_timing - configure CS setup, hold, and inactive delays 3378f1ca9992SSowjanya Komatineni * @spi: the device that requires specific CS timing configuration 337981059366SAlexandru Ardelean * @setup: CS setup time specified via @spi_delay 338081059366SAlexandru Ardelean * @hold: CS hold time specified via @spi_delay 338181059366SAlexandru Ardelean * @inactive: CS inactive delay between transfers specified via @spi_delay 338281059366SAlexandru Ardelean * 338381059366SAlexandru Ardelean * Return: zero on success, else a negative error code. 3384f1ca9992SSowjanya Komatineni */ 338581059366SAlexandru Ardelean int spi_set_cs_timing(struct spi_device *spi, struct spi_delay *setup, 338681059366SAlexandru Ardelean struct spi_delay *hold, struct spi_delay *inactive) 3387f1ca9992SSowjanya Komatineni { 338825093bdeSAlexandru Ardelean size_t len; 338925093bdeSAlexandru Ardelean 3390f1ca9992SSowjanya Komatineni if (spi->controller->set_cs_timing) 339181059366SAlexandru Ardelean return spi->controller->set_cs_timing(spi, setup, hold, 339281059366SAlexandru Ardelean inactive); 339325093bdeSAlexandru Ardelean 339425093bdeSAlexandru Ardelean if ((setup && setup->unit == SPI_DELAY_UNIT_SCK) || 339525093bdeSAlexandru Ardelean (hold && hold->unit == SPI_DELAY_UNIT_SCK) || 339625093bdeSAlexandru Ardelean (inactive && inactive->unit == SPI_DELAY_UNIT_SCK)) { 339725093bdeSAlexandru Ardelean dev_err(&spi->dev, 339825093bdeSAlexandru Ardelean "Clock-cycle delays for CS not supported in SW mode\n"); 339981059366SAlexandru Ardelean return -ENOTSUPP; 3400f1ca9992SSowjanya Komatineni } 340125093bdeSAlexandru Ardelean 340225093bdeSAlexandru Ardelean len = sizeof(struct spi_delay); 340325093bdeSAlexandru Ardelean 340425093bdeSAlexandru Ardelean /* copy delays to controller */ 340525093bdeSAlexandru Ardelean if (setup) 340625093bdeSAlexandru Ardelean memcpy(&spi->controller->cs_setup, setup, len); 340725093bdeSAlexandru Ardelean else 340825093bdeSAlexandru Ardelean memset(&spi->controller->cs_setup, 0, len); 340925093bdeSAlexandru Ardelean 341025093bdeSAlexandru Ardelean if (hold) 341125093bdeSAlexandru Ardelean memcpy(&spi->controller->cs_hold, hold, len); 341225093bdeSAlexandru Ardelean else 341325093bdeSAlexandru Ardelean memset(&spi->controller->cs_hold, 0, len); 341425093bdeSAlexandru Ardelean 341525093bdeSAlexandru Ardelean if (inactive) 341625093bdeSAlexandru Ardelean memcpy(&spi->controller->cs_inactive, inactive, len); 341725093bdeSAlexandru Ardelean else 341825093bdeSAlexandru Ardelean memset(&spi->controller->cs_inactive, 0, len); 341925093bdeSAlexandru Ardelean 342025093bdeSAlexandru Ardelean return 0; 3421f1ca9992SSowjanya Komatineni } 3422f1ca9992SSowjanya Komatineni EXPORT_SYMBOL_GPL(spi_set_cs_timing); 3423f1ca9992SSowjanya Komatineni 34246c613f68SAlexandru Ardelean static int _spi_xfer_word_delay_update(struct spi_transfer *xfer, 34256c613f68SAlexandru Ardelean struct spi_device *spi) 34266c613f68SAlexandru Ardelean { 34276c613f68SAlexandru Ardelean int delay1, delay2; 34286c613f68SAlexandru Ardelean 34293984d39bSAlexandru Ardelean delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); 34306c613f68SAlexandru Ardelean if (delay1 < 0) 34316c613f68SAlexandru Ardelean return delay1; 34326c613f68SAlexandru Ardelean 34333984d39bSAlexandru Ardelean delay2 = spi_delay_to_ns(&spi->word_delay, xfer); 34346c613f68SAlexandru Ardelean if (delay2 < 0) 34356c613f68SAlexandru Ardelean return delay2; 34366c613f68SAlexandru Ardelean 34376c613f68SAlexandru Ardelean if (delay1 < delay2) 34386c613f68SAlexandru Ardelean memcpy(&xfer->word_delay, &spi->word_delay, 34396c613f68SAlexandru Ardelean sizeof(xfer->word_delay)); 34406c613f68SAlexandru Ardelean 34416c613f68SAlexandru Ardelean return 0; 34426c613f68SAlexandru Ardelean } 34436c613f68SAlexandru Ardelean 344490808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message) 3445cf32b71eSErnst Schwab { 34468caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3447e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 34486ea31293SAtsushi Nemoto int w_size; 3449cf32b71eSErnst Schwab 345024a0013aSMark Brown if (list_empty(&message->transfers)) 345124a0013aSMark Brown return -EINVAL; 345224a0013aSMark Brown 3453cbaa62e0SDavid Lechner /* If an SPI controller does not support toggling the CS line on each 345471388b21SDavid Lechner * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO 345571388b21SDavid Lechner * for the CS line, we can emulate the CS-per-word hardware function by 3456cbaa62e0SDavid Lechner * splitting transfers into one-word transfers and ensuring that 3457cbaa62e0SDavid Lechner * cs_change is set for each transfer. 3458cbaa62e0SDavid Lechner */ 345971388b21SDavid Lechner if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || 3460f3186dd8SLinus Walleij spi->cs_gpiod || 346171388b21SDavid Lechner gpio_is_valid(spi->cs_gpio))) { 3462cbaa62e0SDavid Lechner size_t maxsize; 3463cbaa62e0SDavid Lechner int ret; 3464cbaa62e0SDavid Lechner 3465cbaa62e0SDavid Lechner maxsize = (spi->bits_per_word + 7) / 8; 3466cbaa62e0SDavid Lechner 3467cbaa62e0SDavid Lechner /* spi_split_transfers_maxsize() requires message->spi */ 3468cbaa62e0SDavid Lechner message->spi = spi; 3469cbaa62e0SDavid Lechner 3470cbaa62e0SDavid Lechner ret = spi_split_transfers_maxsize(ctlr, message, maxsize, 3471cbaa62e0SDavid Lechner GFP_KERNEL); 3472cbaa62e0SDavid Lechner if (ret) 3473cbaa62e0SDavid Lechner return ret; 3474cbaa62e0SDavid Lechner 3475cbaa62e0SDavid Lechner list_for_each_entry(xfer, &message->transfers, transfer_list) { 3476cbaa62e0SDavid Lechner /* don't change cs_change on the last entry in the list */ 3477cbaa62e0SDavid Lechner if (list_is_last(&xfer->transfer_list, &message->transfers)) 3478cbaa62e0SDavid Lechner break; 3479cbaa62e0SDavid Lechner xfer->cs_change = 1; 3480cbaa62e0SDavid Lechner } 3481cbaa62e0SDavid Lechner } 3482cbaa62e0SDavid Lechner 3483cf32b71eSErnst Schwab /* Half-duplex links include original MicroWire, and ones with 3484cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 3485cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 3486cf32b71eSErnst Schwab * software limitations. 3487cf32b71eSErnst Schwab */ 34888caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 34898caab75fSGeert Uytterhoeven (spi->mode & SPI_3WIRE)) { 34908caab75fSGeert Uytterhoeven unsigned flags = ctlr->flags; 3491cf32b71eSErnst Schwab 3492cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 3493cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 3494cf32b71eSErnst Schwab return -EINVAL; 34958caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 3496cf32b71eSErnst Schwab return -EINVAL; 34978caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 3498cf32b71eSErnst Schwab return -EINVAL; 3499cf32b71eSErnst Schwab } 3500cf32b71eSErnst Schwab } 3501cf32b71eSErnst Schwab 3502e6811d1dSLaxman Dewangan /** 3503059b8ffeSLaxman Dewangan * Set transfer bits_per_word and max speed as spi device default if 3504059b8ffeSLaxman Dewangan * it is not set for this transfer. 3505f477b7fbSwangyuhang * Set transfer tx_nbits and rx_nbits as single transfer default 3506f477b7fbSwangyuhang * (SPI_NBITS_SINGLE) if it is not set for this transfer. 3507b7bb367aSJonas Bonn * Ensure transfer word_delay is at least as long as that required by 3508b7bb367aSJonas Bonn * device itself. 3509e6811d1dSLaxman Dewangan */ 351077e80588SMartin Sperl message->frame_length = 0; 3511e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 35125d7e2b5eSMartin Sperl xfer->effective_speed_hz = 0; 3513078726ceSSourav Poddar message->frame_length += xfer->len; 3514e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 3515e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 3516a6f87fadSAxel Lin 3517a6f87fadSAxel Lin if (!xfer->speed_hz) 3518059b8ffeSLaxman Dewangan xfer->speed_hz = spi->max_speed_hz; 3519a6f87fadSAxel Lin 35208caab75fSGeert Uytterhoeven if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 35218caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 352256ede94aSGabor Juhos 35238caab75fSGeert Uytterhoeven if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 3524543bb255SStephen Warren return -EINVAL; 3525a2fd4f9fSMark Brown 35264d94bd21SIvan T. Ivanov /* 35274d94bd21SIvan T. Ivanov * SPI transfer length should be multiple of SPI word size 35284d94bd21SIvan T. Ivanov * where SPI word size should be power-of-two multiple 35294d94bd21SIvan T. Ivanov */ 35304d94bd21SIvan T. Ivanov if (xfer->bits_per_word <= 8) 35314d94bd21SIvan T. Ivanov w_size = 1; 35324d94bd21SIvan T. Ivanov else if (xfer->bits_per_word <= 16) 35334d94bd21SIvan T. Ivanov w_size = 2; 35344d94bd21SIvan T. Ivanov else 35354d94bd21SIvan T. Ivanov w_size = 4; 35364d94bd21SIvan T. Ivanov 35374d94bd21SIvan T. Ivanov /* No partial transfers accepted */ 35386ea31293SAtsushi Nemoto if (xfer->len % w_size) 35394d94bd21SIvan T. Ivanov return -EINVAL; 35404d94bd21SIvan T. Ivanov 35418caab75fSGeert Uytterhoeven if (xfer->speed_hz && ctlr->min_speed_hz && 35428caab75fSGeert Uytterhoeven xfer->speed_hz < ctlr->min_speed_hz) 3543a2fd4f9fSMark Brown return -EINVAL; 3544f477b7fbSwangyuhang 3545f477b7fbSwangyuhang if (xfer->tx_buf && !xfer->tx_nbits) 3546f477b7fbSwangyuhang xfer->tx_nbits = SPI_NBITS_SINGLE; 3547f477b7fbSwangyuhang if (xfer->rx_buf && !xfer->rx_nbits) 3548f477b7fbSwangyuhang xfer->rx_nbits = SPI_NBITS_SINGLE; 3549f477b7fbSwangyuhang /* check transfer tx/rx_nbits: 35501afd9989SGeert Uytterhoeven * 1. check the value matches one of single, dual and quad 35511afd9989SGeert Uytterhoeven * 2. check tx/rx_nbits match the mode in spi_device 3552f477b7fbSwangyuhang */ 3553db90a441SSourav Poddar if (xfer->tx_buf) { 3554f477b7fbSwangyuhang if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3555f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_DUAL && 3556f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_QUAD) 3557a2fd4f9fSMark Brown return -EINVAL; 3558f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 3559f477b7fbSwangyuhang !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3560f477b7fbSwangyuhang return -EINVAL; 3561f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 3562f477b7fbSwangyuhang !(spi->mode & SPI_TX_QUAD)) 3563f477b7fbSwangyuhang return -EINVAL; 3564db90a441SSourav Poddar } 3565f477b7fbSwangyuhang /* check transfer rx_nbits */ 3566db90a441SSourav Poddar if (xfer->rx_buf) { 3567f477b7fbSwangyuhang if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3568f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_DUAL && 3569f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_QUAD) 3570f477b7fbSwangyuhang return -EINVAL; 3571f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 3572f477b7fbSwangyuhang !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 3573f477b7fbSwangyuhang return -EINVAL; 3574f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 3575f477b7fbSwangyuhang !(spi->mode & SPI_RX_QUAD)) 3576f477b7fbSwangyuhang return -EINVAL; 3577e6811d1dSLaxman Dewangan } 3578b7bb367aSJonas Bonn 35796c613f68SAlexandru Ardelean if (_spi_xfer_word_delay_update(xfer, spi)) 35806c613f68SAlexandru Ardelean return -EINVAL; 3581e6811d1dSLaxman Dewangan } 3582e6811d1dSLaxman Dewangan 3583cf32b71eSErnst Schwab message->status = -EINPROGRESS; 358490808738SMark Brown 358590808738SMark Brown return 0; 358690808738SMark Brown } 358790808738SMark Brown 358890808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message) 358990808738SMark Brown { 35908caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3591b42faeeeSVladimir Oltean struct spi_transfer *xfer; 359290808738SMark Brown 3593b5932f5cSBoris Brezillon /* 3594b5932f5cSBoris Brezillon * Some controllers do not support doing regular SPI transfers. Return 3595b5932f5cSBoris Brezillon * ENOTSUPP when this is the case. 3596b5932f5cSBoris Brezillon */ 3597b5932f5cSBoris Brezillon if (!ctlr->transfer) 3598b5932f5cSBoris Brezillon return -ENOTSUPP; 3599b5932f5cSBoris Brezillon 360090808738SMark Brown message->spi = spi; 360190808738SMark Brown 36028caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); 3603eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); 3604eca2ebc7SMartin Sperl 360590808738SMark Brown trace_spi_message_submit(message); 360690808738SMark Brown 3607b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 3608b42faeeeSVladimir Oltean list_for_each_entry(xfer, &message->transfers, transfer_list) { 3609b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 3610b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 3611b42faeeeSVladimir Oltean } 3612b42faeeeSVladimir Oltean } 3613b42faeeeSVladimir Oltean 36148caab75fSGeert Uytterhoeven return ctlr->transfer(spi, message); 3615cf32b71eSErnst Schwab } 3616cf32b71eSErnst Schwab 3617568d0697SDavid Brownell /** 3618568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 3619568d0697SDavid Brownell * @spi: device with which data will be exchanged 3620568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 3621568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 3622568d0697SDavid Brownell * 3623568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 3624568d0697SDavid Brownell * as well as from task contexts which can sleep. 3625568d0697SDavid Brownell * 3626568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 3627568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 3628568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 3629568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 3630568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 3631568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 3632568d0697SDavid Brownell * core or controller driver code. 3633568d0697SDavid Brownell * 3634568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 3635568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 3636568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 3637568d0697SDavid Brownell * time requirements, for example. 3638568d0697SDavid Brownell * 3639568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 3640568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 3641568d0697SDavid Brownell * Until returning from the associated message completion callback, 3642568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 3643568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 3644568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 364597d56dc6SJavier Martinez Canillas * 364697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3647568d0697SDavid Brownell */ 3648568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 3649568d0697SDavid Brownell { 36508caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3651cf32b71eSErnst Schwab int ret; 3652cf32b71eSErnst Schwab unsigned long flags; 3653568d0697SDavid Brownell 365490808738SMark Brown ret = __spi_validate(spi, message); 365590808738SMark Brown if (ret != 0) 365690808738SMark Brown return ret; 365790808738SMark Brown 36588caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3659568d0697SDavid Brownell 36608caab75fSGeert Uytterhoeven if (ctlr->bus_lock_flag) 3661cf32b71eSErnst Schwab ret = -EBUSY; 3662cf32b71eSErnst Schwab else 3663cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3664568d0697SDavid Brownell 36658caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3666cf32b71eSErnst Schwab 3667cf32b71eSErnst Schwab return ret; 3668568d0697SDavid Brownell } 3669568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 3670568d0697SDavid Brownell 3671cf32b71eSErnst Schwab /** 3672cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 3673cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3674cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 3675cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 3676cf32b71eSErnst Schwab * 3677cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 3678cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 3679cf32b71eSErnst Schwab * 3680cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 3681cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 3682cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 3683cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 3684cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 3685cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 3686cf32b71eSErnst Schwab * core or controller driver code. 3687cf32b71eSErnst Schwab * 3688cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 3689cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 3690cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 3691cf32b71eSErnst Schwab * time requirements, for example. 3692cf32b71eSErnst Schwab * 3693cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 3694cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 3695cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 3696cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 3697cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 3698cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 369997d56dc6SJavier Martinez Canillas * 370097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3701cf32b71eSErnst Schwab */ 3702cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message) 3703cf32b71eSErnst Schwab { 37048caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3705cf32b71eSErnst Schwab int ret; 3706cf32b71eSErnst Schwab unsigned long flags; 3707cf32b71eSErnst Schwab 370890808738SMark Brown ret = __spi_validate(spi, message); 370990808738SMark Brown if (ret != 0) 371090808738SMark Brown return ret; 371190808738SMark Brown 37128caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3713cf32b71eSErnst Schwab 3714cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3715cf32b71eSErnst Schwab 37168caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3717cf32b71eSErnst Schwab 3718cf32b71eSErnst Schwab return ret; 3719cf32b71eSErnst Schwab 3720cf32b71eSErnst Schwab } 3721cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked); 3722cf32b71eSErnst Schwab 37237d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 37247d077197SDavid Brownell 37258caab75fSGeert Uytterhoeven /* Utility methods for SPI protocol drivers, layered on 37267d077197SDavid Brownell * top of the core. Some other utility methods are defined as 37277d077197SDavid Brownell * inline functions. 37287d077197SDavid Brownell */ 37297d077197SDavid Brownell 37305d870c8eSAndrew Morton static void spi_complete(void *arg) 37315d870c8eSAndrew Morton { 37325d870c8eSAndrew Morton complete(arg); 37335d870c8eSAndrew Morton } 37345d870c8eSAndrew Morton 3735ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message) 3736cf32b71eSErnst Schwab { 3737cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 3738cf32b71eSErnst Schwab int status; 37398caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 37400461a414SMark Brown unsigned long flags; 37410461a414SMark Brown 37420461a414SMark Brown status = __spi_validate(spi, message); 37430461a414SMark Brown if (status != 0) 37440461a414SMark Brown return status; 3745cf32b71eSErnst Schwab 3746cf32b71eSErnst Schwab message->complete = spi_complete; 3747cf32b71eSErnst Schwab message->context = &done; 37480461a414SMark Brown message->spi = spi; 3749cf32b71eSErnst Schwab 37508caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); 3751eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); 3752eca2ebc7SMartin Sperl 37530461a414SMark Brown /* If we're not using the legacy transfer method then we will 37540461a414SMark Brown * try to transfer in the calling context so special case. 37550461a414SMark Brown * This code would be less tricky if we could remove the 37560461a414SMark Brown * support for driver implemented message queues. 37570461a414SMark Brown */ 37588caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 37598caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 37600461a414SMark Brown 37610461a414SMark Brown trace_spi_message_submit(message); 37620461a414SMark Brown 37630461a414SMark Brown status = __spi_queued_transfer(spi, message, false); 37640461a414SMark Brown 37658caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 37660461a414SMark Brown } else { 3767cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 37680461a414SMark Brown } 3769cf32b71eSErnst Schwab 3770cf32b71eSErnst Schwab if (status == 0) { 37710461a414SMark Brown /* Push out the messages in the calling context if we 37720461a414SMark Brown * can. 37730461a414SMark Brown */ 37748caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 37758caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3776eca2ebc7SMartin Sperl spi_sync_immediate); 3777eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, 3778eca2ebc7SMartin Sperl spi_sync_immediate); 37798caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, false); 3780eca2ebc7SMartin Sperl } 37810461a414SMark Brown 3782cf32b71eSErnst Schwab wait_for_completion(&done); 3783cf32b71eSErnst Schwab status = message->status; 3784cf32b71eSErnst Schwab } 3785cf32b71eSErnst Schwab message->context = NULL; 3786cf32b71eSErnst Schwab return status; 3787cf32b71eSErnst Schwab } 3788cf32b71eSErnst Schwab 37898ae12a0dSDavid Brownell /** 37908ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 37918ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 37928ae12a0dSDavid Brownell * @message: describes the data transfers 379333e34dc6SDavid Brownell * Context: can sleep 37948ae12a0dSDavid Brownell * 37958ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 37968ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 37978ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 37988ae12a0dSDavid Brownell * 37998ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 38008ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 38018ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 38028ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 38038ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 38048ae12a0dSDavid Brownell * 38050c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 38060c868461SDavid Brownell * message will not be freed before this call returns. 38070c868461SDavid Brownell * 380897d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 38098ae12a0dSDavid Brownell */ 38108ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 38118ae12a0dSDavid Brownell { 3812ef4d96ecSMark Brown int ret; 3813ef4d96ecSMark Brown 38148caab75fSGeert Uytterhoeven mutex_lock(&spi->controller->bus_lock_mutex); 3815ef4d96ecSMark Brown ret = __spi_sync(spi, message); 38168caab75fSGeert Uytterhoeven mutex_unlock(&spi->controller->bus_lock_mutex); 3817ef4d96ecSMark Brown 3818ef4d96ecSMark Brown return ret; 38198ae12a0dSDavid Brownell } 38208ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 38218ae12a0dSDavid Brownell 3822cf32b71eSErnst Schwab /** 3823cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 3824cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3825cf32b71eSErnst Schwab * @message: describes the data transfers 3826cf32b71eSErnst Schwab * Context: can sleep 3827cf32b71eSErnst Schwab * 3828cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3829cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 3830cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 3831cf32b71eSErnst Schwab * 3832cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 383325985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 3834cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 3835cf32b71eSErnst Schwab * 383697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3837cf32b71eSErnst Schwab */ 3838cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 3839cf32b71eSErnst Schwab { 3840ef4d96ecSMark Brown return __spi_sync(spi, message); 3841cf32b71eSErnst Schwab } 3842cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 3843cf32b71eSErnst Schwab 3844cf32b71eSErnst Schwab /** 3845cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 38468caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that should be locked for exclusive bus access 3847cf32b71eSErnst Schwab * Context: can sleep 3848cf32b71eSErnst Schwab * 3849cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3850cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3851cf32b71eSErnst Schwab * 3852cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 3853cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 3854cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 3855cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 3856cf32b71eSErnst Schwab * 385797d56dc6SJavier Martinez Canillas * Return: always zero. 3858cf32b71eSErnst Schwab */ 38598caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr) 3860cf32b71eSErnst Schwab { 3861cf32b71eSErnst Schwab unsigned long flags; 3862cf32b71eSErnst Schwab 38638caab75fSGeert Uytterhoeven mutex_lock(&ctlr->bus_lock_mutex); 3864cf32b71eSErnst Schwab 38658caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 38668caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 1; 38678caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3868cf32b71eSErnst Schwab 3869cf32b71eSErnst Schwab /* mutex remains locked until spi_bus_unlock is called */ 3870cf32b71eSErnst Schwab 3871cf32b71eSErnst Schwab return 0; 3872cf32b71eSErnst Schwab } 3873cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 3874cf32b71eSErnst Schwab 3875cf32b71eSErnst Schwab /** 3876cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 38778caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that was locked for exclusive bus access 3878cf32b71eSErnst Schwab * Context: can sleep 3879cf32b71eSErnst Schwab * 3880cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3881cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3882cf32b71eSErnst Schwab * 3883cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 3884cf32b71eSErnst Schwab * call. 3885cf32b71eSErnst Schwab * 388697d56dc6SJavier Martinez Canillas * Return: always zero. 3887cf32b71eSErnst Schwab */ 38888caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr) 3889cf32b71eSErnst Schwab { 38908caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 3891cf32b71eSErnst Schwab 38928caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->bus_lock_mutex); 3893cf32b71eSErnst Schwab 3894cf32b71eSErnst Schwab return 0; 3895cf32b71eSErnst Schwab } 3896cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 3897cf32b71eSErnst Schwab 3898a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 3899a9948b61SDavid Brownell #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 39008ae12a0dSDavid Brownell 39018ae12a0dSDavid Brownell static u8 *buf; 39028ae12a0dSDavid Brownell 39038ae12a0dSDavid Brownell /** 39048ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 39058ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 39068ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 39078ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 390827570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 390927570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 391033e34dc6SDavid Brownell * Context: can sleep 39118ae12a0dSDavid Brownell * 39128ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 39138ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 39148ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 3915b885244eSDavid Brownell * This call may only be used from a context that may sleep. 39168ae12a0dSDavid Brownell * 3917c373643bSMark Brown * Parameters to this routine are always copied using a small buffer. 391833e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 39190c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 392097d56dc6SJavier Martinez Canillas * 392197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 39228ae12a0dSDavid Brownell */ 39238ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 39240c4a1590SMark Brown const void *txbuf, unsigned n_tx, 39250c4a1590SMark Brown void *rxbuf, unsigned n_rx) 39268ae12a0dSDavid Brownell { 3927068f4070SDavid Brownell static DEFINE_MUTEX(lock); 39288ae12a0dSDavid Brownell 39298ae12a0dSDavid Brownell int status; 39308ae12a0dSDavid Brownell struct spi_message message; 3931bdff549eSDavid Brownell struct spi_transfer x[2]; 39328ae12a0dSDavid Brownell u8 *local_buf; 39338ae12a0dSDavid Brownell 3934b3a223eeSMark Brown /* Use preallocated DMA-safe buffer if we can. We can't avoid 3935b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 3936b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 3937b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 39388ae12a0dSDavid Brownell */ 3939b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 39402cd94c8aSMark Brown local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 39412cd94c8aSMark Brown GFP_KERNEL | GFP_DMA); 3942b3a223eeSMark Brown if (!local_buf) 3943b3a223eeSMark Brown return -ENOMEM; 3944b3a223eeSMark Brown } else { 3945b3a223eeSMark Brown local_buf = buf; 3946b3a223eeSMark Brown } 39478ae12a0dSDavid Brownell 39488275c642SVitaly Wool spi_message_init(&message); 39495fe5f05eSJingoo Han memset(x, 0, sizeof(x)); 3950bdff549eSDavid Brownell if (n_tx) { 3951bdff549eSDavid Brownell x[0].len = n_tx; 3952bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 3953bdff549eSDavid Brownell } 3954bdff549eSDavid Brownell if (n_rx) { 3955bdff549eSDavid Brownell x[1].len = n_rx; 3956bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 3957bdff549eSDavid Brownell } 39588275c642SVitaly Wool 39598ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 3960bdff549eSDavid Brownell x[0].tx_buf = local_buf; 3961bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 39628ae12a0dSDavid Brownell 39638ae12a0dSDavid Brownell /* do the i/o */ 39648ae12a0dSDavid Brownell status = spi_sync(spi, &message); 39659b938b74SMarc Pignat if (status == 0) 3966bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 39678ae12a0dSDavid Brownell 3968bdff549eSDavid Brownell if (x[0].tx_buf == buf) 3969068f4070SDavid Brownell mutex_unlock(&lock); 39708ae12a0dSDavid Brownell else 39718ae12a0dSDavid Brownell kfree(local_buf); 39728ae12a0dSDavid Brownell 39738ae12a0dSDavid Brownell return status; 39748ae12a0dSDavid Brownell } 39758ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 39768ae12a0dSDavid Brownell 39778ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 39788ae12a0dSDavid Brownell 39795f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF) 3980ce79d54aSPantelis Antoniou /* must call put_device() when done with returned spi_device device */ 39815f143af7SMarco Felsch struct spi_device *of_find_spi_device_by_node(struct device_node *node) 3982ce79d54aSPantelis Antoniou { 3983cfba5de9SSuzuki K Poulose struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); 3984cfba5de9SSuzuki K Poulose 3985ce79d54aSPantelis Antoniou return dev ? to_spi_device(dev) : NULL; 3986ce79d54aSPantelis Antoniou } 39875f143af7SMarco Felsch EXPORT_SYMBOL_GPL(of_find_spi_device_by_node); 39885f143af7SMarco Felsch #endif /* IS_ENABLED(CONFIG_OF) */ 3989ce79d54aSPantelis Antoniou 39905f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF_DYNAMIC) 39918caab75fSGeert Uytterhoeven /* the spi controllers are not using spi_bus, so we find it with another way */ 39928caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 3993ce79d54aSPantelis Antoniou { 3994ce79d54aSPantelis Antoniou struct device *dev; 3995ce79d54aSPantelis Antoniou 3996cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_master_class, node); 39976c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 3998cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_slave_class, node); 3999ce79d54aSPantelis Antoniou if (!dev) 4000ce79d54aSPantelis Antoniou return NULL; 4001ce79d54aSPantelis Antoniou 4002ce79d54aSPantelis Antoniou /* reference got in class_find_device */ 40038caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 4004ce79d54aSPantelis Antoniou } 4005ce79d54aSPantelis Antoniou 4006ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action, 4007ce79d54aSPantelis Antoniou void *arg) 4008ce79d54aSPantelis Antoniou { 4009ce79d54aSPantelis Antoniou struct of_reconfig_data *rd = arg; 40108caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 4011ce79d54aSPantelis Antoniou struct spi_device *spi; 4012ce79d54aSPantelis Antoniou 4013ce79d54aSPantelis Antoniou switch (of_reconfig_get_state_change(action, arg)) { 4014ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 40158caab75fSGeert Uytterhoeven ctlr = of_find_spi_controller_by_node(rd->dn->parent); 40168caab75fSGeert Uytterhoeven if (ctlr == NULL) 4017ce79d54aSPantelis Antoniou return NOTIFY_OK; /* not for us */ 4018ce79d54aSPantelis Antoniou 4019bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 40208caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4021bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4022bd6c1644SGeert Uytterhoeven } 4023bd6c1644SGeert Uytterhoeven 40248caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, rd->dn); 40258caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4026ce79d54aSPantelis Antoniou 4027ce79d54aSPantelis Antoniou if (IS_ERR(spi)) { 402825c56c88SRob Herring pr_err("%s: failed to create for '%pOF'\n", 402925c56c88SRob Herring __func__, rd->dn); 4030e0af98a7SRalf Ramsauer of_node_clear_flag(rd->dn, OF_POPULATED); 4031ce79d54aSPantelis Antoniou return notifier_from_errno(PTR_ERR(spi)); 4032ce79d54aSPantelis Antoniou } 4033ce79d54aSPantelis Antoniou break; 4034ce79d54aSPantelis Antoniou 4035ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 4036bd6c1644SGeert Uytterhoeven /* already depopulated? */ 4037bd6c1644SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 4038bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4039bd6c1644SGeert Uytterhoeven 4040ce79d54aSPantelis Antoniou /* find our device by node */ 4041ce79d54aSPantelis Antoniou spi = of_find_spi_device_by_node(rd->dn); 4042ce79d54aSPantelis Antoniou if (spi == NULL) 4043ce79d54aSPantelis Antoniou return NOTIFY_OK; /* no? not meant for us */ 4044ce79d54aSPantelis Antoniou 4045ce79d54aSPantelis Antoniou /* unregister takes one ref away */ 4046ce79d54aSPantelis Antoniou spi_unregister_device(spi); 4047ce79d54aSPantelis Antoniou 4048ce79d54aSPantelis Antoniou /* and put the reference of the find */ 4049ce79d54aSPantelis Antoniou put_device(&spi->dev); 4050ce79d54aSPantelis Antoniou break; 4051ce79d54aSPantelis Antoniou } 4052ce79d54aSPantelis Antoniou 4053ce79d54aSPantelis Antoniou return NOTIFY_OK; 4054ce79d54aSPantelis Antoniou } 4055ce79d54aSPantelis Antoniou 4056ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = { 4057ce79d54aSPantelis Antoniou .notifier_call = of_spi_notify, 4058ce79d54aSPantelis Antoniou }; 4059ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4060ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier; 4061ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4062ce79d54aSPantelis Antoniou 40637f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI) 40648caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data) 40657f24467fSOctavian Purdila { 40667f24467fSOctavian Purdila return ACPI_COMPANION(dev->parent) == data; 40677f24467fSOctavian Purdila } 40687f24467fSOctavian Purdila 40698caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 40707f24467fSOctavian Purdila { 40717f24467fSOctavian Purdila struct device *dev; 40727f24467fSOctavian Purdila 40737f24467fSOctavian Purdila dev = class_find_device(&spi_master_class, NULL, adev, 40748caab75fSGeert Uytterhoeven spi_acpi_controller_match); 40756c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 40766c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, adev, 40778caab75fSGeert Uytterhoeven spi_acpi_controller_match); 40787f24467fSOctavian Purdila if (!dev) 40797f24467fSOctavian Purdila return NULL; 40807f24467fSOctavian Purdila 40818caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 40827f24467fSOctavian Purdila } 40837f24467fSOctavian Purdila 40847f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 40857f24467fSOctavian Purdila { 40867f24467fSOctavian Purdila struct device *dev; 40877f24467fSOctavian Purdila 408800500147SSuzuki K Poulose dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev); 40895b16668eSWolfram Sang return to_spi_device(dev); 40907f24467fSOctavian Purdila } 40917f24467fSOctavian Purdila 40927f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 40937f24467fSOctavian Purdila void *arg) 40947f24467fSOctavian Purdila { 40957f24467fSOctavian Purdila struct acpi_device *adev = arg; 40968caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 40977f24467fSOctavian Purdila struct spi_device *spi; 40987f24467fSOctavian Purdila 40997f24467fSOctavian Purdila switch (value) { 41007f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_ADD: 41018caab75fSGeert Uytterhoeven ctlr = acpi_spi_find_controller_by_adev(adev->parent); 41028caab75fSGeert Uytterhoeven if (!ctlr) 41037f24467fSOctavian Purdila break; 41047f24467fSOctavian Purdila 41058caab75fSGeert Uytterhoeven acpi_register_spi_device(ctlr, adev); 41068caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 41077f24467fSOctavian Purdila break; 41087f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_REMOVE: 41097f24467fSOctavian Purdila if (!acpi_device_enumerated(adev)) 41107f24467fSOctavian Purdila break; 41117f24467fSOctavian Purdila 41127f24467fSOctavian Purdila spi = acpi_spi_find_device_by_adev(adev); 41137f24467fSOctavian Purdila if (!spi) 41147f24467fSOctavian Purdila break; 41157f24467fSOctavian Purdila 41167f24467fSOctavian Purdila spi_unregister_device(spi); 41177f24467fSOctavian Purdila put_device(&spi->dev); 41187f24467fSOctavian Purdila break; 41197f24467fSOctavian Purdila } 41207f24467fSOctavian Purdila 41217f24467fSOctavian Purdila return NOTIFY_OK; 41227f24467fSOctavian Purdila } 41237f24467fSOctavian Purdila 41247f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = { 41257f24467fSOctavian Purdila .notifier_call = acpi_spi_notify, 41267f24467fSOctavian Purdila }; 41277f24467fSOctavian Purdila #else 41287f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier; 41297f24467fSOctavian Purdila #endif 41307f24467fSOctavian Purdila 41318ae12a0dSDavid Brownell static int __init spi_init(void) 41328ae12a0dSDavid Brownell { 4133b885244eSDavid Brownell int status; 41348ae12a0dSDavid Brownell 4135e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 4136b885244eSDavid Brownell if (!buf) { 4137b885244eSDavid Brownell status = -ENOMEM; 4138b885244eSDavid Brownell goto err0; 41398ae12a0dSDavid Brownell } 4140b885244eSDavid Brownell 4141b885244eSDavid Brownell status = bus_register(&spi_bus_type); 4142b885244eSDavid Brownell if (status < 0) 4143b885244eSDavid Brownell goto err1; 4144b885244eSDavid Brownell 4145b885244eSDavid Brownell status = class_register(&spi_master_class); 4146b885244eSDavid Brownell if (status < 0) 4147b885244eSDavid Brownell goto err2; 4148ce79d54aSPantelis Antoniou 41496c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 41506c364062SGeert Uytterhoeven status = class_register(&spi_slave_class); 41516c364062SGeert Uytterhoeven if (status < 0) 41526c364062SGeert Uytterhoeven goto err3; 41536c364062SGeert Uytterhoeven } 41546c364062SGeert Uytterhoeven 41555267720eSFabio Estevam if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 4156ce79d54aSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 41577f24467fSOctavian Purdila if (IS_ENABLED(CONFIG_ACPI)) 41587f24467fSOctavian Purdila WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 4159ce79d54aSPantelis Antoniou 4160b885244eSDavid Brownell return 0; 4161b885244eSDavid Brownell 41626c364062SGeert Uytterhoeven err3: 41636c364062SGeert Uytterhoeven class_unregister(&spi_master_class); 4164b885244eSDavid Brownell err2: 4165b885244eSDavid Brownell bus_unregister(&spi_bus_type); 4166b885244eSDavid Brownell err1: 4167b885244eSDavid Brownell kfree(buf); 4168b885244eSDavid Brownell buf = NULL; 4169b885244eSDavid Brownell err0: 4170b885244eSDavid Brownell return status; 4171b885244eSDavid Brownell } 4172b885244eSDavid Brownell 41738ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(), 41748ae12a0dSDavid Brownell * but even essential drivers wait till later 4175b885244eSDavid Brownell * 4176b885244eSDavid Brownell * REVISIT only boardinfo really needs static linking. the rest (device and 4177b885244eSDavid Brownell * driver registration) _could_ be dynamically linked (modular) ... costs 4178b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 41798ae12a0dSDavid Brownell */ 4180673c0c00SDavid Brownell postcore_initcall(spi_init); 4181