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 { 955039563eSTrent Piepho /* Emptry 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 40844af7927SJon Hunter ret = sdrv->probe(spi); 40933cf00e5SMika Westerberg if (ret) 410676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 41133cf00e5SMika Westerberg 41233cf00e5SMika Westerberg return ret; 413b885244eSDavid Brownell } 414b885244eSDavid Brownell 415b885244eSDavid Brownell static int spi_drv_remove(struct device *dev) 416b885244eSDavid Brownell { 417b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 41833cf00e5SMika Westerberg int ret; 419b885244eSDavid Brownell 420aec35f4eSJean Delvare ret = sdrv->remove(to_spi_device(dev)); 421676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 42233cf00e5SMika Westerberg 42333cf00e5SMika Westerberg return ret; 424b885244eSDavid Brownell } 425b885244eSDavid Brownell 426b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev) 427b885244eSDavid Brownell { 428b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 429b885244eSDavid Brownell 430b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 431b885244eSDavid Brownell } 432b885244eSDavid Brownell 43333e34dc6SDavid Brownell /** 434ca5d2485SAndrew F. Davis * __spi_register_driver - register a SPI driver 43588c9321dSThierry Reding * @owner: owner module of the driver to register 43633e34dc6SDavid Brownell * @sdrv: the driver to register 43733e34dc6SDavid Brownell * Context: can sleep 43897d56dc6SJavier Martinez Canillas * 43997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 44033e34dc6SDavid Brownell */ 441ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) 442b885244eSDavid Brownell { 443ca5d2485SAndrew F. Davis sdrv->driver.owner = owner; 444b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 445b885244eSDavid Brownell if (sdrv->probe) 446b885244eSDavid Brownell sdrv->driver.probe = spi_drv_probe; 447b885244eSDavid Brownell if (sdrv->remove) 448b885244eSDavid Brownell sdrv->driver.remove = spi_drv_remove; 449b885244eSDavid Brownell if (sdrv->shutdown) 450b885244eSDavid Brownell sdrv->driver.shutdown = spi_drv_shutdown; 451b885244eSDavid Brownell return driver_register(&sdrv->driver); 452b885244eSDavid Brownell } 453ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver); 454b885244eSDavid Brownell 4558ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 4568ae12a0dSDavid Brownell 4578ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that 4588caab75fSGeert Uytterhoeven * would make them board-specific. Similarly with SPI controller drivers. 4598ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 4608ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 4618ae12a0dSDavid Brownell */ 4628ae12a0dSDavid Brownell 4638ae12a0dSDavid Brownell struct boardinfo { 4648ae12a0dSDavid Brownell struct list_head list; 4652b9603a0SFeng Tang struct spi_board_info board_info; 4668ae12a0dSDavid Brownell }; 4678ae12a0dSDavid Brownell 4688ae12a0dSDavid Brownell static LIST_HEAD(board_list); 4698caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list); 4702b9603a0SFeng Tang 4712b9603a0SFeng Tang /* 4722b9603a0SFeng Tang * Used to protect add/del opertion for board_info list and 4738caab75fSGeert Uytterhoeven * spi_controller list, and their matching process 4749b61e302SSuniel Mahesh * also used to protect object of type struct idr 4752b9603a0SFeng Tang */ 47694040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 4778ae12a0dSDavid Brownell 478dc87c98eSGrant Likely /** 479dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 4808caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 481dc87c98eSGrant Likely * Context: can sleep 482dc87c98eSGrant Likely * 483dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 484dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 485dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 486dc87c98eSGrant Likely * spi_add_device() on it. 487dc87c98eSGrant Likely * 488dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 4898caab75fSGeert Uytterhoeven * spi_device structure to add it to the SPI controller. If the caller 490dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 491dc87c98eSGrant Likely * call spi_dev_put() on it. 492dc87c98eSGrant Likely * 49397d56dc6SJavier Martinez Canillas * Return: a pointer to the new device, or NULL. 494dc87c98eSGrant Likely */ 4958caab75fSGeert Uytterhoeven struct spi_device *spi_alloc_device(struct spi_controller *ctlr) 496dc87c98eSGrant Likely { 497dc87c98eSGrant Likely struct spi_device *spi; 498dc87c98eSGrant Likely 4998caab75fSGeert Uytterhoeven if (!spi_controller_get(ctlr)) 500dc87c98eSGrant Likely return NULL; 501dc87c98eSGrant Likely 5025fe5f05eSJingoo Han spi = kzalloc(sizeof(*spi), GFP_KERNEL); 503dc87c98eSGrant Likely if (!spi) { 5048caab75fSGeert Uytterhoeven spi_controller_put(ctlr); 505dc87c98eSGrant Likely return NULL; 506dc87c98eSGrant Likely } 507dc87c98eSGrant Likely 5088caab75fSGeert Uytterhoeven spi->master = spi->controller = ctlr; 5098caab75fSGeert Uytterhoeven spi->dev.parent = &ctlr->dev; 510dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 511dc87c98eSGrant Likely spi->dev.release = spidev_release; 512446411e1SAndreas Larsson spi->cs_gpio = -ENOENT; 513eca2ebc7SMartin Sperl 514eca2ebc7SMartin Sperl spin_lock_init(&spi->statistics.lock); 515eca2ebc7SMartin Sperl 516dc87c98eSGrant Likely device_initialize(&spi->dev); 517dc87c98eSGrant Likely return spi; 518dc87c98eSGrant Likely } 519dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device); 520dc87c98eSGrant Likely 521e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi) 522e13ac47bSJarkko Nikula { 523e13ac47bSJarkko Nikula struct acpi_device *adev = ACPI_COMPANION(&spi->dev); 524e13ac47bSJarkko Nikula 525e13ac47bSJarkko Nikula if (adev) { 526e13ac47bSJarkko Nikula dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); 527e13ac47bSJarkko Nikula return; 528e13ac47bSJarkko Nikula } 529e13ac47bSJarkko Nikula 5308caab75fSGeert Uytterhoeven dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), 531e13ac47bSJarkko Nikula spi->chip_select); 532e13ac47bSJarkko Nikula } 533e13ac47bSJarkko Nikula 534b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data) 535b6fb8d3aSMika Westerberg { 536b6fb8d3aSMika Westerberg struct spi_device *spi = to_spi_device(dev); 537b6fb8d3aSMika Westerberg struct spi_device *new_spi = data; 538b6fb8d3aSMika Westerberg 5398caab75fSGeert Uytterhoeven if (spi->controller == new_spi->controller && 540b6fb8d3aSMika Westerberg spi->chip_select == new_spi->chip_select) 541b6fb8d3aSMika Westerberg return -EBUSY; 542b6fb8d3aSMika Westerberg return 0; 543b6fb8d3aSMika Westerberg } 544b6fb8d3aSMika Westerberg 545dc87c98eSGrant Likely /** 546dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 547dc87c98eSGrant Likely * @spi: spi_device to register 548dc87c98eSGrant Likely * 549dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 550dc87c98eSGrant Likely * spi_alloc_device can be added onto the spi bus with this function. 551dc87c98eSGrant Likely * 55297d56dc6SJavier Martinez Canillas * Return: 0 on success; negative errno on failure 553dc87c98eSGrant Likely */ 554dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi) 555dc87c98eSGrant Likely { 556e48880e0SDavid Brownell static DEFINE_MUTEX(spi_add_lock); 5578caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 5588caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 559dc87c98eSGrant Likely int status; 560dc87c98eSGrant Likely 561dc87c98eSGrant Likely /* Chipselects are numbered 0..max; validate. */ 5628caab75fSGeert Uytterhoeven if (spi->chip_select >= ctlr->num_chipselect) { 5638caab75fSGeert Uytterhoeven dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 5648caab75fSGeert Uytterhoeven ctlr->num_chipselect); 565dc87c98eSGrant Likely return -EINVAL; 566dc87c98eSGrant Likely } 567dc87c98eSGrant Likely 568dc87c98eSGrant Likely /* Set the bus ID string */ 569e13ac47bSJarkko Nikula spi_dev_set_name(spi); 570e48880e0SDavid Brownell 571e48880e0SDavid Brownell /* We need to make sure there's no other device with this 572e48880e0SDavid Brownell * chipselect **BEFORE** we call setup(), else we'll trash 573e48880e0SDavid Brownell * its configuration. Lock against concurrent add() calls. 574e48880e0SDavid Brownell */ 575e48880e0SDavid Brownell mutex_lock(&spi_add_lock); 576e48880e0SDavid Brownell 577b6fb8d3aSMika Westerberg status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); 578b6fb8d3aSMika Westerberg if (status) { 579e48880e0SDavid Brownell dev_err(dev, "chipselect %d already in use\n", 580e48880e0SDavid Brownell spi->chip_select); 581e48880e0SDavid Brownell goto done; 582e48880e0SDavid Brownell } 583e48880e0SDavid Brownell 584f3186dd8SLinus Walleij /* Descriptors take precedence */ 585f3186dd8SLinus Walleij if (ctlr->cs_gpiods) 586f3186dd8SLinus Walleij spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; 587f3186dd8SLinus Walleij else if (ctlr->cs_gpios) 5888caab75fSGeert Uytterhoeven spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; 58974317984SJean-Christophe PLAGNIOL-VILLARD 590e48880e0SDavid Brownell /* Drivers may modify this initial i/o setup, but will 591e48880e0SDavid Brownell * normally rely on the device being setup. Devices 592e48880e0SDavid Brownell * using SPI_CS_HIGH can't coexist well otherwise... 593e48880e0SDavid Brownell */ 5947d077197SDavid Brownell status = spi_setup(spi); 595dc87c98eSGrant Likely if (status < 0) { 596eb288a1fSLinus Walleij dev_err(dev, "can't setup %s, status %d\n", 597eb288a1fSLinus Walleij dev_name(&spi->dev), status); 598e48880e0SDavid Brownell goto done; 599dc87c98eSGrant Likely } 600dc87c98eSGrant Likely 601e48880e0SDavid Brownell /* Device may be bound to an active driver when this returns */ 602dc87c98eSGrant Likely status = device_add(&spi->dev); 603e48880e0SDavid Brownell if (status < 0) 604eb288a1fSLinus Walleij dev_err(dev, "can't add %s, status %d\n", 605eb288a1fSLinus Walleij dev_name(&spi->dev), status); 606e48880e0SDavid Brownell else 60735f74fcaSKay Sievers dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 608e48880e0SDavid Brownell 609e48880e0SDavid Brownell done: 610e48880e0SDavid Brownell mutex_unlock(&spi_add_lock); 611e48880e0SDavid Brownell return status; 612dc87c98eSGrant Likely } 613dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device); 6148ae12a0dSDavid Brownell 61533e34dc6SDavid Brownell /** 61633e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 6178caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 61833e34dc6SDavid Brownell * @chip: Describes the SPI device 61933e34dc6SDavid Brownell * Context: can sleep 62033e34dc6SDavid Brownell * 62133e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 6228ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 6238ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 6248ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 6258ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 626082c8cb4SDavid Brownell * 62797d56dc6SJavier Martinez Canillas * Return: the new device, or NULL. 6288ae12a0dSDavid Brownell */ 6298caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr, 630e9d5a461SAdrian Bunk struct spi_board_info *chip) 6318ae12a0dSDavid Brownell { 6328ae12a0dSDavid Brownell struct spi_device *proxy; 6338ae12a0dSDavid Brownell int status; 6348ae12a0dSDavid Brownell 635082c8cb4SDavid Brownell /* NOTE: caller did any chip->bus_num checks necessary. 636082c8cb4SDavid Brownell * 637082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 638082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 639082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 640082c8cb4SDavid Brownell */ 641082c8cb4SDavid Brownell 6428caab75fSGeert Uytterhoeven proxy = spi_alloc_device(ctlr); 643dc87c98eSGrant Likely if (!proxy) 6448ae12a0dSDavid Brownell return NULL; 6458ae12a0dSDavid Brownell 646102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 647102eb975SGrant Likely 6488ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 6498ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 650980a01c9SDavid Brownell proxy->mode = chip->mode; 6518ae12a0dSDavid Brownell proxy->irq = chip->irq; 652102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 6538ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 6548ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 6558ae12a0dSDavid Brownell proxy->controller_state = NULL; 6568ae12a0dSDavid Brownell 657826cf175SDmitry Torokhov if (chip->properties) { 658826cf175SDmitry Torokhov status = device_add_properties(&proxy->dev, chip->properties); 659826cf175SDmitry Torokhov if (status) { 6608caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 661826cf175SDmitry Torokhov "failed to add properties to '%s': %d\n", 662826cf175SDmitry Torokhov chip->modalias, status); 663826cf175SDmitry Torokhov goto err_dev_put; 664826cf175SDmitry Torokhov } 6658ae12a0dSDavid Brownell } 666dc87c98eSGrant Likely 667826cf175SDmitry Torokhov status = spi_add_device(proxy); 668826cf175SDmitry Torokhov if (status < 0) 669826cf175SDmitry Torokhov goto err_remove_props; 670826cf175SDmitry Torokhov 671dc87c98eSGrant Likely return proxy; 672826cf175SDmitry Torokhov 673826cf175SDmitry Torokhov err_remove_props: 674826cf175SDmitry Torokhov if (chip->properties) 675826cf175SDmitry Torokhov device_remove_properties(&proxy->dev); 676826cf175SDmitry Torokhov err_dev_put: 677826cf175SDmitry Torokhov spi_dev_put(proxy); 678826cf175SDmitry Torokhov return NULL; 679dc87c98eSGrant Likely } 6808ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 6818ae12a0dSDavid Brownell 6823b1884c2SGeert Uytterhoeven /** 6833b1884c2SGeert Uytterhoeven * spi_unregister_device - unregister a single SPI device 6843b1884c2SGeert Uytterhoeven * @spi: spi_device to unregister 6853b1884c2SGeert Uytterhoeven * 6863b1884c2SGeert Uytterhoeven * Start making the passed SPI device vanish. Normally this would be handled 6878caab75fSGeert Uytterhoeven * by spi_unregister_controller(). 6883b1884c2SGeert Uytterhoeven */ 6893b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi) 6903b1884c2SGeert Uytterhoeven { 691bd6c1644SGeert Uytterhoeven if (!spi) 692bd6c1644SGeert Uytterhoeven return; 693bd6c1644SGeert Uytterhoeven 6948324147fSJohan Hovold if (spi->dev.of_node) { 695bd6c1644SGeert Uytterhoeven of_node_clear_flag(spi->dev.of_node, OF_POPULATED); 6968324147fSJohan Hovold of_node_put(spi->dev.of_node); 6978324147fSJohan Hovold } 6987f24467fSOctavian Purdila if (ACPI_COMPANION(&spi->dev)) 6997f24467fSOctavian Purdila acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); 7003b1884c2SGeert Uytterhoeven device_unregister(&spi->dev); 7013b1884c2SGeert Uytterhoeven } 7023b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device); 7033b1884c2SGeert Uytterhoeven 7048caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, 7052b9603a0SFeng Tang struct spi_board_info *bi) 7062b9603a0SFeng Tang { 7072b9603a0SFeng Tang struct spi_device *dev; 7082b9603a0SFeng Tang 7098caab75fSGeert Uytterhoeven if (ctlr->bus_num != bi->bus_num) 7102b9603a0SFeng Tang return; 7112b9603a0SFeng Tang 7128caab75fSGeert Uytterhoeven dev = spi_new_device(ctlr, bi); 7132b9603a0SFeng Tang if (!dev) 7148caab75fSGeert Uytterhoeven dev_err(ctlr->dev.parent, "can't create new device for %s\n", 7152b9603a0SFeng Tang bi->modalias); 7162b9603a0SFeng Tang } 7172b9603a0SFeng Tang 71833e34dc6SDavid Brownell /** 71933e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 72033e34dc6SDavid Brownell * @info: array of chip descriptors 72133e34dc6SDavid Brownell * @n: how many descriptors are provided 72233e34dc6SDavid Brownell * Context: can sleep 72333e34dc6SDavid Brownell * 7248ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 7258ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 7268ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 7278ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 7288ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 7298ae12a0dSDavid Brownell * 7308ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 7318ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 7328ae12a0dSDavid Brownell * would naturally declare its SPI devices. 7338ae12a0dSDavid Brownell * 7348ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 7358ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 736826cf175SDmitry Torokhov * Device properties are deep-copied though. 73797d56dc6SJavier Martinez Canillas * 73897d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 7398ae12a0dSDavid Brownell */ 740fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n) 7418ae12a0dSDavid Brownell { 7428ae12a0dSDavid Brownell struct boardinfo *bi; 7432b9603a0SFeng Tang int i; 7448ae12a0dSDavid Brownell 745c7908a37SXiubo Li if (!n) 746f974cf57SDmitry Torokhov return 0; 747c7908a37SXiubo Li 748f9bdb7fdSMarkus Elfring bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); 7498ae12a0dSDavid Brownell if (!bi) 7508ae12a0dSDavid Brownell return -ENOMEM; 7518ae12a0dSDavid Brownell 7522b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 7538caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 7542b9603a0SFeng Tang 7552b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 756826cf175SDmitry Torokhov if (info->properties) { 757826cf175SDmitry Torokhov bi->board_info.properties = 758826cf175SDmitry Torokhov property_entries_dup(info->properties); 759826cf175SDmitry Torokhov if (IS_ERR(bi->board_info.properties)) 760826cf175SDmitry Torokhov return PTR_ERR(bi->board_info.properties); 761826cf175SDmitry Torokhov } 762826cf175SDmitry Torokhov 76394040828SMatthias Kaehlcke mutex_lock(&board_lock); 7648ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 7658caab75fSGeert Uytterhoeven list_for_each_entry(ctlr, &spi_controller_list, list) 7668caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, 7678caab75fSGeert Uytterhoeven &bi->board_info); 76894040828SMatthias Kaehlcke mutex_unlock(&board_lock); 7692b9603a0SFeng Tang } 7702b9603a0SFeng Tang 7718ae12a0dSDavid Brownell return 0; 7728ae12a0dSDavid Brownell } 7738ae12a0dSDavid Brownell 7748ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 7758ae12a0dSDavid Brownell 776b158935fSMark Brown static void spi_set_cs(struct spi_device *spi, bool enable) 777b158935fSMark Brown { 778b158935fSMark Brown if (spi->mode & SPI_CS_HIGH) 779b158935fSMark Brown enable = !enable; 780b158935fSMark Brown 781f3186dd8SLinus Walleij if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { 782f3186dd8SLinus Walleij /* 783f3186dd8SLinus Walleij * Honour the SPI_NO_CS flag and invert the enable line, as 784f3186dd8SLinus Walleij * active low is default for SPI. Execution paths that handle 785f3186dd8SLinus Walleij * polarity inversion in gpiolib (such as device tree) will 786f3186dd8SLinus Walleij * enforce active high using the SPI_CS_HIGH resulting in a 787f3186dd8SLinus Walleij * double inversion through the code above. 788f3186dd8SLinus Walleij */ 789f3186dd8SLinus Walleij if (!(spi->mode & SPI_NO_CS)) { 790f3186dd8SLinus Walleij if (spi->cs_gpiod) 79128f7604fSFelix Fietkau gpiod_set_value_cansleep(spi->cs_gpiod, 79228f7604fSFelix Fietkau !enable); 793f3186dd8SLinus Walleij else 79428f7604fSFelix Fietkau gpio_set_value_cansleep(spi->cs_gpio, !enable); 795f3186dd8SLinus Walleij } 7968eee6b9dSThor Thayer /* Some SPI masters need both GPIO CS & slave_select */ 7978caab75fSGeert Uytterhoeven if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && 7988caab75fSGeert Uytterhoeven spi->controller->set_cs) 7998caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 8008caab75fSGeert Uytterhoeven } else if (spi->controller->set_cs) { 8018caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 8028eee6b9dSThor Thayer } 803b158935fSMark Brown } 804b158935fSMark Brown 8052de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA 80646336966SBoris Brezillon int spi_map_buf(struct spi_controller *ctlr, struct device *dev, 8076ad45a27SMark Brown struct sg_table *sgt, void *buf, size_t len, 8086ad45a27SMark Brown enum dma_data_direction dir) 8096ad45a27SMark Brown { 8106ad45a27SMark Brown const bool vmalloced_buf = is_vmalloc_addr(buf); 811df88e91bSAndy Shevchenko unsigned int max_seg_size = dma_get_max_seg_size(dev); 812b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM 813b1b8153cSVignesh R const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && 814b1b8153cSVignesh R (unsigned long)buf < (PKMAP_BASE + 815b1b8153cSVignesh R (LAST_PKMAP * PAGE_SIZE))); 816b1b8153cSVignesh R #else 817b1b8153cSVignesh R const bool kmap_buf = false; 818b1b8153cSVignesh R #endif 81965598c13SAndrew Gabbasov int desc_len; 82065598c13SAndrew Gabbasov int sgs; 8216ad45a27SMark Brown struct page *vm_page; 8228dd4a016SJuan Gutierrez struct scatterlist *sg; 8236ad45a27SMark Brown void *sg_buf; 8246ad45a27SMark Brown size_t min; 8256ad45a27SMark Brown int i, ret; 8266ad45a27SMark Brown 827b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 828df88e91bSAndy Shevchenko desc_len = min_t(int, max_seg_size, PAGE_SIZE); 82965598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); 8300569a88fSVignesh R } else if (virt_addr_valid(buf)) { 8318caab75fSGeert Uytterhoeven desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); 83265598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len, desc_len); 8330569a88fSVignesh R } else { 8340569a88fSVignesh R return -EINVAL; 83565598c13SAndrew Gabbasov } 83665598c13SAndrew Gabbasov 8376ad45a27SMark Brown ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); 8386ad45a27SMark Brown if (ret != 0) 8396ad45a27SMark Brown return ret; 8406ad45a27SMark Brown 8418dd4a016SJuan Gutierrez sg = &sgt->sgl[0]; 8426ad45a27SMark Brown for (i = 0; i < sgs; i++) { 8436ad45a27SMark Brown 844b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 845ce99319aSMaxime Chevallier /* 846ce99319aSMaxime Chevallier * Next scatterlist entry size is the minimum between 847ce99319aSMaxime Chevallier * the desc_len and the remaining buffer length that 848ce99319aSMaxime Chevallier * fits in a page. 849ce99319aSMaxime Chevallier */ 850ce99319aSMaxime Chevallier min = min_t(size_t, desc_len, 851ce99319aSMaxime Chevallier min_t(size_t, len, 852ce99319aSMaxime Chevallier PAGE_SIZE - offset_in_page(buf))); 853b1b8153cSVignesh R if (vmalloced_buf) 8546ad45a27SMark Brown vm_page = vmalloc_to_page(buf); 855b1b8153cSVignesh R else 856b1b8153cSVignesh R vm_page = kmap_to_page(buf); 8576ad45a27SMark Brown if (!vm_page) { 8586ad45a27SMark Brown sg_free_table(sgt); 8596ad45a27SMark Brown return -ENOMEM; 8606ad45a27SMark Brown } 8618dd4a016SJuan Gutierrez sg_set_page(sg, vm_page, 862c1aefbddSCharles Keepax min, offset_in_page(buf)); 8636ad45a27SMark Brown } else { 86465598c13SAndrew Gabbasov min = min_t(size_t, len, desc_len); 8656ad45a27SMark Brown sg_buf = buf; 8668dd4a016SJuan Gutierrez sg_set_buf(sg, sg_buf, min); 8676ad45a27SMark Brown } 8686ad45a27SMark Brown 8696ad45a27SMark Brown buf += min; 8706ad45a27SMark Brown len -= min; 8718dd4a016SJuan Gutierrez sg = sg_next(sg); 8726ad45a27SMark Brown } 8736ad45a27SMark Brown 8746ad45a27SMark Brown ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); 87589e4b66aSGeert Uytterhoeven if (!ret) 87689e4b66aSGeert Uytterhoeven ret = -ENOMEM; 8776ad45a27SMark Brown if (ret < 0) { 8786ad45a27SMark Brown sg_free_table(sgt); 8796ad45a27SMark Brown return ret; 8806ad45a27SMark Brown } 8816ad45a27SMark Brown 8826ad45a27SMark Brown sgt->nents = ret; 8836ad45a27SMark Brown 8846ad45a27SMark Brown return 0; 8856ad45a27SMark Brown } 8866ad45a27SMark Brown 88746336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, 8886ad45a27SMark Brown struct sg_table *sgt, enum dma_data_direction dir) 8896ad45a27SMark Brown { 8906ad45a27SMark Brown if (sgt->orig_nents) { 8916ad45a27SMark Brown dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); 8926ad45a27SMark Brown sg_free_table(sgt); 8936ad45a27SMark Brown } 8946ad45a27SMark Brown } 8956ad45a27SMark Brown 8968caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 89799adef31SMark Brown { 89899adef31SMark Brown struct device *tx_dev, *rx_dev; 89999adef31SMark Brown struct spi_transfer *xfer; 9006ad45a27SMark Brown int ret; 9013a2eba9bSMark Brown 9028caab75fSGeert Uytterhoeven if (!ctlr->can_dma) 90399adef31SMark Brown return 0; 90499adef31SMark Brown 9058caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 9068caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 907c37f45b5SLeilk Liu else 9088caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 909c37f45b5SLeilk Liu 9108caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 9118caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 912c37f45b5SLeilk Liu else 9138caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 91499adef31SMark Brown 91599adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9168caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 91799adef31SMark Brown continue; 91899adef31SMark Brown 91999adef31SMark Brown if (xfer->tx_buf != NULL) { 9208caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, 9216ad45a27SMark Brown (void *)xfer->tx_buf, xfer->len, 92299adef31SMark Brown DMA_TO_DEVICE); 9236ad45a27SMark Brown if (ret != 0) 9246ad45a27SMark Brown return ret; 92599adef31SMark Brown } 92699adef31SMark Brown 92799adef31SMark Brown if (xfer->rx_buf != NULL) { 9288caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, 92999adef31SMark Brown xfer->rx_buf, xfer->len, 93099adef31SMark Brown DMA_FROM_DEVICE); 9316ad45a27SMark Brown if (ret != 0) { 9328caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, 9336ad45a27SMark Brown DMA_TO_DEVICE); 9346ad45a27SMark Brown return ret; 93599adef31SMark Brown } 93699adef31SMark Brown } 93799adef31SMark Brown } 93899adef31SMark Brown 9398caab75fSGeert Uytterhoeven ctlr->cur_msg_mapped = true; 94099adef31SMark Brown 94199adef31SMark Brown return 0; 94299adef31SMark Brown } 94399adef31SMark Brown 9448caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg) 94599adef31SMark Brown { 94699adef31SMark Brown struct spi_transfer *xfer; 94799adef31SMark Brown struct device *tx_dev, *rx_dev; 94899adef31SMark Brown 9498caab75fSGeert Uytterhoeven if (!ctlr->cur_msg_mapped || !ctlr->can_dma) 95099adef31SMark Brown return 0; 95199adef31SMark Brown 9528caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 9538caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 954c37f45b5SLeilk Liu else 9558caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 956c37f45b5SLeilk Liu 9578caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 9588caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 959c37f45b5SLeilk Liu else 9608caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 96199adef31SMark Brown 96299adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9638caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 96499adef31SMark Brown continue; 96599adef31SMark Brown 9668caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 9678caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 96899adef31SMark Brown } 96999adef31SMark Brown 97099adef31SMark Brown return 0; 97199adef31SMark Brown } 9722de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */ 9738caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr, 9742de440f5SGeert Uytterhoeven struct spi_message *msg) 9752de440f5SGeert Uytterhoeven { 9762de440f5SGeert Uytterhoeven return 0; 9772de440f5SGeert Uytterhoeven } 9782de440f5SGeert Uytterhoeven 9798caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr, 9802de440f5SGeert Uytterhoeven struct spi_message *msg) 9812de440f5SGeert Uytterhoeven { 9822de440f5SGeert Uytterhoeven return 0; 9832de440f5SGeert Uytterhoeven } 9842de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */ 9852de440f5SGeert Uytterhoeven 9868caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr, 9874b786458SMartin Sperl struct spi_message *msg) 9884b786458SMartin Sperl { 9894b786458SMartin Sperl struct spi_transfer *xfer; 9904b786458SMartin Sperl 9914b786458SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9924b786458SMartin Sperl /* 9934b786458SMartin Sperl * Restore the original value of tx_buf or rx_buf if they are 9944b786458SMartin Sperl * NULL. 9954b786458SMartin Sperl */ 9968caab75fSGeert Uytterhoeven if (xfer->tx_buf == ctlr->dummy_tx) 9974b786458SMartin Sperl xfer->tx_buf = NULL; 9988caab75fSGeert Uytterhoeven if (xfer->rx_buf == ctlr->dummy_rx) 9994b786458SMartin Sperl xfer->rx_buf = NULL; 10004b786458SMartin Sperl } 10014b786458SMartin Sperl 10028caab75fSGeert Uytterhoeven return __spi_unmap_msg(ctlr, msg); 10034b786458SMartin Sperl } 10044b786458SMartin Sperl 10058caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 10062de440f5SGeert Uytterhoeven { 10072de440f5SGeert Uytterhoeven struct spi_transfer *xfer; 10082de440f5SGeert Uytterhoeven void *tmp; 10092de440f5SGeert Uytterhoeven unsigned int max_tx, max_rx; 10102de440f5SGeert Uytterhoeven 10118caab75fSGeert Uytterhoeven if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) { 10122de440f5SGeert Uytterhoeven max_tx = 0; 10132de440f5SGeert Uytterhoeven max_rx = 0; 10142de440f5SGeert Uytterhoeven 10152de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, transfer_list) { 10168caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && 10172de440f5SGeert Uytterhoeven !xfer->tx_buf) 10182de440f5SGeert Uytterhoeven max_tx = max(xfer->len, max_tx); 10198caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && 10202de440f5SGeert Uytterhoeven !xfer->rx_buf) 10212de440f5SGeert Uytterhoeven max_rx = max(xfer->len, max_rx); 10222de440f5SGeert Uytterhoeven } 10232de440f5SGeert Uytterhoeven 10242de440f5SGeert Uytterhoeven if (max_tx) { 10258caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_tx, max_tx, 10262de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 10272de440f5SGeert Uytterhoeven if (!tmp) 10282de440f5SGeert Uytterhoeven return -ENOMEM; 10298caab75fSGeert Uytterhoeven ctlr->dummy_tx = tmp; 10302de440f5SGeert Uytterhoeven memset(tmp, 0, max_tx); 10312de440f5SGeert Uytterhoeven } 10322de440f5SGeert Uytterhoeven 10332de440f5SGeert Uytterhoeven if (max_rx) { 10348caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_rx, max_rx, 10352de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 10362de440f5SGeert Uytterhoeven if (!tmp) 10372de440f5SGeert Uytterhoeven return -ENOMEM; 10388caab75fSGeert Uytterhoeven ctlr->dummy_rx = tmp; 10392de440f5SGeert Uytterhoeven } 10402de440f5SGeert Uytterhoeven 10412de440f5SGeert Uytterhoeven if (max_tx || max_rx) { 10422de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, 10432de440f5SGeert Uytterhoeven transfer_list) { 10445442dcaaSChris Lesiak if (!xfer->len) 10455442dcaaSChris Lesiak continue; 10462de440f5SGeert Uytterhoeven if (!xfer->tx_buf) 10478caab75fSGeert Uytterhoeven xfer->tx_buf = ctlr->dummy_tx; 10482de440f5SGeert Uytterhoeven if (!xfer->rx_buf) 10498caab75fSGeert Uytterhoeven xfer->rx_buf = ctlr->dummy_rx; 10502de440f5SGeert Uytterhoeven } 10512de440f5SGeert Uytterhoeven } 10522de440f5SGeert Uytterhoeven } 10532de440f5SGeert Uytterhoeven 10548caab75fSGeert Uytterhoeven return __spi_map_msg(ctlr, msg); 10552de440f5SGeert Uytterhoeven } 105699adef31SMark Brown 1057810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr, 1058810923f3SLubomir Rintel struct spi_message *msg, 1059810923f3SLubomir Rintel struct spi_transfer *xfer) 1060810923f3SLubomir Rintel { 1061810923f3SLubomir Rintel struct spi_statistics *statm = &ctlr->statistics; 1062810923f3SLubomir Rintel struct spi_statistics *stats = &msg->spi->statistics; 1063810923f3SLubomir Rintel unsigned long long ms = 1; 1064810923f3SLubomir Rintel 1065810923f3SLubomir Rintel if (spi_controller_is_slave(ctlr)) { 1066810923f3SLubomir Rintel if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { 1067810923f3SLubomir Rintel dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); 1068810923f3SLubomir Rintel return -EINTR; 1069810923f3SLubomir Rintel } 1070810923f3SLubomir Rintel } else { 1071810923f3SLubomir Rintel ms = 8LL * 1000LL * xfer->len; 1072810923f3SLubomir Rintel do_div(ms, xfer->speed_hz); 1073810923f3SLubomir Rintel ms += ms + 200; /* some tolerance */ 1074810923f3SLubomir Rintel 1075810923f3SLubomir Rintel if (ms > UINT_MAX) 1076810923f3SLubomir Rintel ms = UINT_MAX; 1077810923f3SLubomir Rintel 1078810923f3SLubomir Rintel ms = wait_for_completion_timeout(&ctlr->xfer_completion, 1079810923f3SLubomir Rintel msecs_to_jiffies(ms)); 1080810923f3SLubomir Rintel 1081810923f3SLubomir Rintel if (ms == 0) { 1082810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(statm, timedout); 1083810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(stats, timedout); 1084810923f3SLubomir Rintel dev_err(&msg->spi->dev, 1085810923f3SLubomir Rintel "SPI transfer timed out\n"); 1086810923f3SLubomir Rintel return -ETIMEDOUT; 1087810923f3SLubomir Rintel } 1088810923f3SLubomir Rintel } 1089810923f3SLubomir Rintel 1090810923f3SLubomir Rintel return 0; 1091810923f3SLubomir Rintel } 1092810923f3SLubomir Rintel 10930ff2de8bSMartin Sperl static void _spi_transfer_delay_ns(u32 ns) 10940ff2de8bSMartin Sperl { 10950ff2de8bSMartin Sperl if (!ns) 10960ff2de8bSMartin Sperl return; 10970ff2de8bSMartin Sperl if (ns <= 1000) { 10980ff2de8bSMartin Sperl ndelay(ns); 10990ff2de8bSMartin Sperl } else { 11000ff2de8bSMartin Sperl u32 us = DIV_ROUND_UP(ns, 1000); 11010ff2de8bSMartin Sperl 11020ff2de8bSMartin Sperl if (us <= 10) 11030ff2de8bSMartin Sperl udelay(us); 11040ff2de8bSMartin Sperl else 11050ff2de8bSMartin Sperl usleep_range(us, us + DIV_ROUND_UP(us, 10)); 11060ff2de8bSMartin Sperl } 11070ff2de8bSMartin Sperl } 11080ff2de8bSMartin Sperl 11090ff2de8bSMartin Sperl static void _spi_transfer_cs_change_delay(struct spi_message *msg, 11100ff2de8bSMartin Sperl struct spi_transfer *xfer) 11110ff2de8bSMartin Sperl { 11120ff2de8bSMartin Sperl u32 delay = xfer->cs_change_delay; 11130ff2de8bSMartin Sperl u32 unit = xfer->cs_change_delay_unit; 1114d5864e5bSMartin Sperl u32 hz; 11150ff2de8bSMartin Sperl 11160ff2de8bSMartin Sperl /* return early on "fast" mode - for everything but USECS */ 11170ff2de8bSMartin Sperl if (!delay && unit != SPI_DELAY_UNIT_USECS) 11180ff2de8bSMartin Sperl return; 11190ff2de8bSMartin Sperl 11200ff2de8bSMartin Sperl switch (unit) { 11210ff2de8bSMartin Sperl case SPI_DELAY_UNIT_USECS: 11220ff2de8bSMartin Sperl /* for compatibility use default of 10us */ 11230ff2de8bSMartin Sperl if (!delay) 11240ff2de8bSMartin Sperl delay = 10000; 11250ff2de8bSMartin Sperl else 11260ff2de8bSMartin Sperl delay *= 1000; 11270ff2de8bSMartin Sperl break; 11280ff2de8bSMartin Sperl case SPI_DELAY_UNIT_NSECS: /* nothing to do here */ 11290ff2de8bSMartin Sperl break; 1130d5864e5bSMartin Sperl case SPI_DELAY_UNIT_SCK: 1131d5864e5bSMartin Sperl /* if there is no effective speed know, then approximate 1132d5864e5bSMartin Sperl * by underestimating with half the requested hz 1133d5864e5bSMartin Sperl */ 1134d5864e5bSMartin Sperl hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; 1135d5864e5bSMartin Sperl delay *= DIV_ROUND_UP(1000000000, hz); 1136d5864e5bSMartin Sperl break; 11370ff2de8bSMartin Sperl default: 11380ff2de8bSMartin Sperl dev_err_once(&msg->spi->dev, 11390ff2de8bSMartin Sperl "Use of unsupported delay unit %i, using default of 10us\n", 11400ff2de8bSMartin Sperl xfer->cs_change_delay_unit); 11410ff2de8bSMartin Sperl delay = 10000; 11420ff2de8bSMartin Sperl } 11430ff2de8bSMartin Sperl /* now sleep for the requested amount of time */ 11440ff2de8bSMartin Sperl _spi_transfer_delay_ns(delay); 11450ff2de8bSMartin Sperl } 11460ff2de8bSMartin Sperl 1147b158935fSMark Brown /* 1148b158935fSMark Brown * spi_transfer_one_message - Default implementation of transfer_one_message() 1149b158935fSMark Brown * 1150b158935fSMark Brown * This is a standard implementation of transfer_one_message() for 11518ba811a7SMoritz Fischer * drivers which implement a transfer_one() operation. It provides 1152b158935fSMark Brown * standard handling of delays and chip select management. 1153b158935fSMark Brown */ 11548caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr, 1155b158935fSMark Brown struct spi_message *msg) 1156b158935fSMark Brown { 1157b158935fSMark Brown struct spi_transfer *xfer; 1158b158935fSMark Brown bool keep_cs = false; 1159b158935fSMark Brown int ret = 0; 11608caab75fSGeert Uytterhoeven struct spi_statistics *statm = &ctlr->statistics; 1161eca2ebc7SMartin Sperl struct spi_statistics *stats = &msg->spi->statistics; 1162b158935fSMark Brown 1163b158935fSMark Brown spi_set_cs(msg->spi, true); 1164b158935fSMark Brown 1165eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, messages); 1166eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, messages); 1167eca2ebc7SMartin Sperl 1168b158935fSMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1169b158935fSMark Brown trace_spi_transfer_start(msg, xfer); 1170b158935fSMark Brown 11718caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(statm, xfer, ctlr); 11728caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(stats, xfer, ctlr); 1173eca2ebc7SMartin Sperl 117438ec10f6SMark Brown if (xfer->tx_buf || xfer->rx_buf) { 11758caab75fSGeert Uytterhoeven reinit_completion(&ctlr->xfer_completion); 1176b158935fSMark Brown 11778caab75fSGeert Uytterhoeven ret = ctlr->transfer_one(ctlr, msg->spi, xfer); 1178b158935fSMark Brown if (ret < 0) { 1179eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, 1180eca2ebc7SMartin Sperl errors); 1181eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, 1182eca2ebc7SMartin Sperl errors); 1183b158935fSMark Brown dev_err(&msg->spi->dev, 1184b158935fSMark Brown "SPI transfer failed: %d\n", ret); 1185b158935fSMark Brown goto out; 1186b158935fSMark Brown } 1187b158935fSMark Brown 1188d57e7960SMark Brown if (ret > 0) { 1189810923f3SLubomir Rintel ret = spi_transfer_wait(ctlr, msg, xfer); 1190810923f3SLubomir Rintel if (ret < 0) 1191810923f3SLubomir Rintel msg->status = ret; 1192d57e7960SMark Brown } 119338ec10f6SMark Brown } else { 119438ec10f6SMark Brown if (xfer->len) 119538ec10f6SMark Brown dev_err(&msg->spi->dev, 119638ec10f6SMark Brown "Bufferless transfer has length %u\n", 119738ec10f6SMark Brown xfer->len); 119838ec10f6SMark Brown } 1199b158935fSMark Brown 1200b158935fSMark Brown trace_spi_transfer_stop(msg, xfer); 1201b158935fSMark Brown 1202b158935fSMark Brown if (msg->status != -EINPROGRESS) 1203b158935fSMark Brown goto out; 1204b158935fSMark Brown 12050ff2de8bSMartin Sperl if (xfer->delay_usecs) 12060ff2de8bSMartin Sperl _spi_transfer_delay_ns(xfer->delay_usecs * 1000); 1207b158935fSMark Brown 1208b158935fSMark Brown if (xfer->cs_change) { 1209b158935fSMark Brown if (list_is_last(&xfer->transfer_list, 1210b158935fSMark Brown &msg->transfers)) { 1211b158935fSMark Brown keep_cs = true; 1212b158935fSMark Brown } else { 12130b73aa63SMark Brown spi_set_cs(msg->spi, false); 12140ff2de8bSMartin Sperl _spi_transfer_cs_change_delay(msg, xfer); 12150b73aa63SMark Brown spi_set_cs(msg->spi, true); 1216b158935fSMark Brown } 1217b158935fSMark Brown } 1218b158935fSMark Brown 1219b158935fSMark Brown msg->actual_length += xfer->len; 1220b158935fSMark Brown } 1221b158935fSMark Brown 1222b158935fSMark Brown out: 1223b158935fSMark Brown if (ret != 0 || !keep_cs) 1224b158935fSMark Brown spi_set_cs(msg->spi, false); 1225b158935fSMark Brown 1226b158935fSMark Brown if (msg->status == -EINPROGRESS) 1227b158935fSMark Brown msg->status = ret; 1228b158935fSMark Brown 12298caab75fSGeert Uytterhoeven if (msg->status && ctlr->handle_err) 12308caab75fSGeert Uytterhoeven ctlr->handle_err(ctlr, msg); 1231b716c4ffSAndy Shevchenko 1232c9ba7a16SNoralf Trønnes spi_res_release(ctlr, msg); 1233c9ba7a16SNoralf Trønnes 12340ed56252SMark Brown spi_finalize_current_message(ctlr); 12350ed56252SMark Brown 1236b158935fSMark Brown return ret; 1237b158935fSMark Brown } 1238b158935fSMark Brown 1239b158935fSMark Brown /** 1240b158935fSMark Brown * spi_finalize_current_transfer - report completion of a transfer 12418caab75fSGeert Uytterhoeven * @ctlr: the controller reporting completion 1242b158935fSMark Brown * 1243b158935fSMark Brown * Called by SPI drivers using the core transfer_one_message() 1244b158935fSMark Brown * implementation to notify it that the current interrupt driven 12459e8f4882SGeert Uytterhoeven * transfer has finished and the next one may be scheduled. 1246b158935fSMark Brown */ 12478caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr) 1248b158935fSMark Brown { 12498caab75fSGeert Uytterhoeven complete(&ctlr->xfer_completion); 1250b158935fSMark Brown } 1251b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); 1252b158935fSMark Brown 1253ffbbdd21SLinus Walleij /** 1254fc9e0f71SMark Brown * __spi_pump_messages - function which processes spi message queue 12558caab75fSGeert Uytterhoeven * @ctlr: controller to process queue for 1256fc9e0f71SMark Brown * @in_kthread: true if we are in the context of the message pump thread 1257ffbbdd21SLinus Walleij * 1258ffbbdd21SLinus Walleij * This function checks if there is any spi message in the queue that 1259ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 1260ffbbdd21SLinus Walleij * and transfer each message. 1261ffbbdd21SLinus Walleij * 12620461a414SMark Brown * Note that it is called both from the kthread itself and also from 12630461a414SMark Brown * inside spi_sync(); the queue extraction handling at the top of the 12640461a414SMark Brown * function should deal with this safely. 1265ffbbdd21SLinus Walleij */ 12668caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1267ffbbdd21SLinus Walleij { 1268ffbbdd21SLinus Walleij unsigned long flags; 1269ffbbdd21SLinus Walleij bool was_busy = false; 1270ffbbdd21SLinus Walleij int ret; 1271ffbbdd21SLinus Walleij 1272983aee5dSMark Brown /* Lock queue */ 12738caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1274983aee5dSMark Brown 1275983aee5dSMark Brown /* Make sure we are not already running a message */ 12768caab75fSGeert Uytterhoeven if (ctlr->cur_msg) { 12778caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1278983aee5dSMark Brown return; 1279983aee5dSMark Brown } 1280983aee5dSMark Brown 1281f0125f1aSMark Brown /* If another context is idling the device then defer */ 12828caab75fSGeert Uytterhoeven if (ctlr->idling) { 12838caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 12848caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 12850461a414SMark Brown return; 12860461a414SMark Brown } 12870461a414SMark Brown 1288983aee5dSMark Brown /* Check if the queue is idle */ 12898caab75fSGeert Uytterhoeven if (list_empty(&ctlr->queue) || !ctlr->running) { 12908caab75fSGeert Uytterhoeven if (!ctlr->busy) { 12918caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1292ffbbdd21SLinus Walleij return; 1293ffbbdd21SLinus Walleij } 1294fc9e0f71SMark Brown 1295f0125f1aSMark Brown /* Only do teardown in the thread */ 1296f0125f1aSMark Brown if (!in_kthread) { 1297f0125f1aSMark Brown kthread_queue_work(&ctlr->kworker, 1298f0125f1aSMark Brown &ctlr->pump_messages); 1299f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1300f0125f1aSMark Brown return; 1301f0125f1aSMark Brown } 1302f0125f1aSMark Brown 1303f0125f1aSMark Brown ctlr->busy = false; 1304f0125f1aSMark Brown ctlr->idling = true; 1305f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1306f0125f1aSMark Brown 1307f0125f1aSMark Brown kfree(ctlr->dummy_rx); 1308f0125f1aSMark Brown ctlr->dummy_rx = NULL; 1309f0125f1aSMark Brown kfree(ctlr->dummy_tx); 1310f0125f1aSMark Brown ctlr->dummy_tx = NULL; 1311f0125f1aSMark Brown if (ctlr->unprepare_transfer_hardware && 1312f0125f1aSMark Brown ctlr->unprepare_transfer_hardware(ctlr)) 1313f0125f1aSMark Brown dev_err(&ctlr->dev, 1314f0125f1aSMark Brown "failed to unprepare transfer hardware\n"); 1315f0125f1aSMark Brown if (ctlr->auto_runtime_pm) { 1316f0125f1aSMark Brown pm_runtime_mark_last_busy(ctlr->dev.parent); 1317f0125f1aSMark Brown pm_runtime_put_autosuspend(ctlr->dev.parent); 1318f0125f1aSMark Brown } 1319f0125f1aSMark Brown trace_spi_controller_idle(ctlr); 1320f0125f1aSMark Brown 1321f0125f1aSMark Brown spin_lock_irqsave(&ctlr->queue_lock, flags); 1322f0125f1aSMark Brown ctlr->idling = false; 13238caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1324ffbbdd21SLinus Walleij return; 1325ffbbdd21SLinus Walleij } 1326ffbbdd21SLinus Walleij 1327ffbbdd21SLinus Walleij /* Extract head of queue */ 13288caab75fSGeert Uytterhoeven ctlr->cur_msg = 13298caab75fSGeert Uytterhoeven list_first_entry(&ctlr->queue, struct spi_message, queue); 1330ffbbdd21SLinus Walleij 13318caab75fSGeert Uytterhoeven list_del_init(&ctlr->cur_msg->queue); 13328caab75fSGeert Uytterhoeven if (ctlr->busy) 1333ffbbdd21SLinus Walleij was_busy = true; 1334ffbbdd21SLinus Walleij else 13358caab75fSGeert Uytterhoeven ctlr->busy = true; 13368caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1337ffbbdd21SLinus Walleij 13388caab75fSGeert Uytterhoeven mutex_lock(&ctlr->io_mutex); 1339ef4d96ecSMark Brown 13408caab75fSGeert Uytterhoeven if (!was_busy && ctlr->auto_runtime_pm) { 13418caab75fSGeert Uytterhoeven ret = pm_runtime_get_sync(ctlr->dev.parent); 134249834de2SMark Brown if (ret < 0) { 13437e48e23aSTony Lindgren pm_runtime_put_noidle(ctlr->dev.parent); 13448caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "Failed to power device: %d\n", 134549834de2SMark Brown ret); 13468caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 134749834de2SMark Brown return; 134849834de2SMark Brown } 134949834de2SMark Brown } 135049834de2SMark Brown 135156ec1978SMark Brown if (!was_busy) 13528caab75fSGeert Uytterhoeven trace_spi_controller_busy(ctlr); 135356ec1978SMark Brown 13548caab75fSGeert Uytterhoeven if (!was_busy && ctlr->prepare_transfer_hardware) { 13558caab75fSGeert Uytterhoeven ret = ctlr->prepare_transfer_hardware(ctlr); 1356ffbbdd21SLinus Walleij if (ret) { 13578caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 1358f3440d9aSSuper Liu "failed to prepare transfer hardware: %d\n", 1359f3440d9aSSuper Liu ret); 136049834de2SMark Brown 13618caab75fSGeert Uytterhoeven if (ctlr->auto_runtime_pm) 13628caab75fSGeert Uytterhoeven pm_runtime_put(ctlr->dev.parent); 1363f3440d9aSSuper Liu 1364f3440d9aSSuper Liu ctlr->cur_msg->status = ret; 1365f3440d9aSSuper Liu spi_finalize_current_message(ctlr); 1366f3440d9aSSuper Liu 13678caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 1368ffbbdd21SLinus Walleij return; 1369ffbbdd21SLinus Walleij } 1370ffbbdd21SLinus Walleij } 1371ffbbdd21SLinus Walleij 13728caab75fSGeert Uytterhoeven trace_spi_message_start(ctlr->cur_msg); 137356ec1978SMark Brown 13748caab75fSGeert Uytterhoeven if (ctlr->prepare_message) { 13758caab75fSGeert Uytterhoeven ret = ctlr->prepare_message(ctlr, ctlr->cur_msg); 13762841a5fcSMark Brown if (ret) { 13778caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to prepare message: %d\n", 13788caab75fSGeert Uytterhoeven ret); 13798caab75fSGeert Uytterhoeven ctlr->cur_msg->status = ret; 13808caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 138149023d2eSJon Hunter goto out; 13822841a5fcSMark Brown } 13838caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = true; 13842841a5fcSMark Brown } 13852841a5fcSMark Brown 13868caab75fSGeert Uytterhoeven ret = spi_map_msg(ctlr, ctlr->cur_msg); 138799adef31SMark Brown if (ret) { 13888caab75fSGeert Uytterhoeven ctlr->cur_msg->status = ret; 13898caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 139049023d2eSJon Hunter goto out; 139199adef31SMark Brown } 139299adef31SMark Brown 13938caab75fSGeert Uytterhoeven ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg); 1394ffbbdd21SLinus Walleij if (ret) { 13958caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 13961f802f82SGeert Uytterhoeven "failed to transfer one message from queue\n"); 139749023d2eSJon Hunter goto out; 1398ffbbdd21SLinus Walleij } 139949023d2eSJon Hunter 140049023d2eSJon Hunter out: 14018caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 140262826970SMark Brown 140362826970SMark Brown /* Prod the scheduler in case transfer_one() was busy waiting */ 140449023d2eSJon Hunter if (!ret) 140562826970SMark Brown cond_resched(); 1406ffbbdd21SLinus Walleij } 1407ffbbdd21SLinus Walleij 1408fc9e0f71SMark Brown /** 1409fc9e0f71SMark Brown * spi_pump_messages - kthread work function which processes spi message queue 14108caab75fSGeert Uytterhoeven * @work: pointer to kthread work struct contained in the controller struct 1411fc9e0f71SMark Brown */ 1412fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work) 1413fc9e0f71SMark Brown { 14148caab75fSGeert Uytterhoeven struct spi_controller *ctlr = 14158caab75fSGeert Uytterhoeven container_of(work, struct spi_controller, pump_messages); 1416fc9e0f71SMark Brown 14178caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, true); 1418fc9e0f71SMark Brown } 1419fc9e0f71SMark Brown 1420924b5867SDouglas Anderson /** 1421924b5867SDouglas Anderson * spi_set_thread_rt - set the controller to pump at realtime priority 1422924b5867SDouglas Anderson * @ctlr: controller to boost priority of 1423924b5867SDouglas Anderson * 1424924b5867SDouglas Anderson * This can be called because the controller requested realtime priority 1425924b5867SDouglas Anderson * (by setting the ->rt value before calling spi_register_controller()) or 1426924b5867SDouglas Anderson * because a device on the bus said that its transfers needed realtime 1427924b5867SDouglas Anderson * priority. 1428924b5867SDouglas Anderson * 1429924b5867SDouglas Anderson * NOTE: at the moment if any device on a bus says it needs realtime then 1430924b5867SDouglas Anderson * the thread will be at realtime priority for all transfers on that 1431924b5867SDouglas Anderson * controller. If this eventually becomes a problem we may see if we can 1432924b5867SDouglas Anderson * find a way to boost the priority only temporarily during relevant 1433924b5867SDouglas Anderson * transfers. 1434924b5867SDouglas Anderson */ 1435924b5867SDouglas Anderson static void spi_set_thread_rt(struct spi_controller *ctlr) 1436ffbbdd21SLinus Walleij { 1437ffbbdd21SLinus Walleij struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; 1438ffbbdd21SLinus Walleij 1439924b5867SDouglas Anderson dev_info(&ctlr->dev, 1440924b5867SDouglas Anderson "will run message pump with realtime priority\n"); 1441924b5867SDouglas Anderson sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, ¶m); 1442924b5867SDouglas Anderson } 1443924b5867SDouglas Anderson 1444924b5867SDouglas Anderson static int spi_init_queue(struct spi_controller *ctlr) 1445924b5867SDouglas Anderson { 14468caab75fSGeert Uytterhoeven ctlr->running = false; 14478caab75fSGeert Uytterhoeven ctlr->busy = false; 1448ffbbdd21SLinus Walleij 14498caab75fSGeert Uytterhoeven kthread_init_worker(&ctlr->kworker); 14508caab75fSGeert Uytterhoeven ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker, 14518caab75fSGeert Uytterhoeven "%s", dev_name(&ctlr->dev)); 14528caab75fSGeert Uytterhoeven if (IS_ERR(ctlr->kworker_task)) { 14538caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to create message pump task\n"); 14548caab75fSGeert Uytterhoeven return PTR_ERR(ctlr->kworker_task); 1455ffbbdd21SLinus Walleij } 14568caab75fSGeert Uytterhoeven kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1457f0125f1aSMark Brown 1458ffbbdd21SLinus Walleij /* 14598caab75fSGeert Uytterhoeven * Controller config will indicate if this controller should run the 1460ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 1461ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 1462ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 1463ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 1464ffbbdd21SLinus Walleij */ 1465924b5867SDouglas Anderson if (ctlr->rt) 1466924b5867SDouglas Anderson spi_set_thread_rt(ctlr); 1467ffbbdd21SLinus Walleij 1468ffbbdd21SLinus Walleij return 0; 1469ffbbdd21SLinus Walleij } 1470ffbbdd21SLinus Walleij 1471ffbbdd21SLinus Walleij /** 1472ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 1473ffbbdd21SLinus Walleij * messages 14748caab75fSGeert Uytterhoeven * @ctlr: the controller to check for queued messages 1475ffbbdd21SLinus Walleij * 1476ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 1477ffbbdd21SLinus Walleij * this call. 147897d56dc6SJavier Martinez Canillas * 147997d56dc6SJavier Martinez Canillas * Return: the next message in the queue, else NULL if the queue is empty. 1480ffbbdd21SLinus Walleij */ 14818caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1482ffbbdd21SLinus Walleij { 1483ffbbdd21SLinus Walleij struct spi_message *next; 1484ffbbdd21SLinus Walleij unsigned long flags; 1485ffbbdd21SLinus Walleij 1486ffbbdd21SLinus Walleij /* get a pointer to the next message, if any */ 14878caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 14888caab75fSGeert Uytterhoeven next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 14891cfd97f9SAxel Lin queue); 14908caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1491ffbbdd21SLinus Walleij 1492ffbbdd21SLinus Walleij return next; 1493ffbbdd21SLinus Walleij } 1494ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1495ffbbdd21SLinus Walleij 1496ffbbdd21SLinus Walleij /** 1497ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 14988caab75fSGeert Uytterhoeven * @ctlr: the controller to return the message to 1499ffbbdd21SLinus Walleij * 1500ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 1501ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 1502ffbbdd21SLinus Walleij */ 15038caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr) 1504ffbbdd21SLinus Walleij { 1505ffbbdd21SLinus Walleij struct spi_message *mesg; 1506ffbbdd21SLinus Walleij unsigned long flags; 15072841a5fcSMark Brown int ret; 1508ffbbdd21SLinus Walleij 15098caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 15108caab75fSGeert Uytterhoeven mesg = ctlr->cur_msg; 15118caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1512ffbbdd21SLinus Walleij 15138caab75fSGeert Uytterhoeven spi_unmap_msg(ctlr, mesg); 151499adef31SMark Brown 15158caab75fSGeert Uytterhoeven if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { 15168caab75fSGeert Uytterhoeven ret = ctlr->unprepare_message(ctlr, mesg); 15172841a5fcSMark Brown if (ret) { 15188caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 15198caab75fSGeert Uytterhoeven ret); 15202841a5fcSMark Brown } 15212841a5fcSMark Brown } 1522391949b6SUwe Kleine-König 15238caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 15248caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 15258caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = false; 15268caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 15278caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 15288e76ef88SMartin Sperl 15298e76ef88SMartin Sperl trace_spi_message_done(mesg); 15302841a5fcSMark Brown 1531ffbbdd21SLinus Walleij mesg->state = NULL; 1532ffbbdd21SLinus Walleij if (mesg->complete) 1533ffbbdd21SLinus Walleij mesg->complete(mesg->context); 1534ffbbdd21SLinus Walleij } 1535ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 1536ffbbdd21SLinus Walleij 15378caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr) 1538ffbbdd21SLinus Walleij { 1539ffbbdd21SLinus Walleij unsigned long flags; 1540ffbbdd21SLinus Walleij 15418caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1542ffbbdd21SLinus Walleij 15438caab75fSGeert Uytterhoeven if (ctlr->running || ctlr->busy) { 15448caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1545ffbbdd21SLinus Walleij return -EBUSY; 1546ffbbdd21SLinus Walleij } 1547ffbbdd21SLinus Walleij 15488caab75fSGeert Uytterhoeven ctlr->running = true; 15498caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 15508caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1551ffbbdd21SLinus Walleij 15528caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 1553ffbbdd21SLinus Walleij 1554ffbbdd21SLinus Walleij return 0; 1555ffbbdd21SLinus Walleij } 1556ffbbdd21SLinus Walleij 15578caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr) 1558ffbbdd21SLinus Walleij { 1559ffbbdd21SLinus Walleij unsigned long flags; 1560ffbbdd21SLinus Walleij unsigned limit = 500; 1561ffbbdd21SLinus Walleij int ret = 0; 1562ffbbdd21SLinus Walleij 15638caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1564ffbbdd21SLinus Walleij 1565ffbbdd21SLinus Walleij /* 1566ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 15678caab75fSGeert Uytterhoeven * A wait_queue on the ctlr->busy could be used, but then the common 1568ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 1569ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 1570ffbbdd21SLinus Walleij */ 15718caab75fSGeert Uytterhoeven while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 15728caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1573f97b26b0SAxel Lin usleep_range(10000, 11000); 15748caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1575ffbbdd21SLinus Walleij } 1576ffbbdd21SLinus Walleij 15778caab75fSGeert Uytterhoeven if (!list_empty(&ctlr->queue) || ctlr->busy) 1578ffbbdd21SLinus Walleij ret = -EBUSY; 1579ffbbdd21SLinus Walleij else 15808caab75fSGeert Uytterhoeven ctlr->running = false; 1581ffbbdd21SLinus Walleij 15828caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1583ffbbdd21SLinus Walleij 1584ffbbdd21SLinus Walleij if (ret) { 15858caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "could not stop message queue\n"); 1586ffbbdd21SLinus Walleij return ret; 1587ffbbdd21SLinus Walleij } 1588ffbbdd21SLinus Walleij return ret; 1589ffbbdd21SLinus Walleij } 1590ffbbdd21SLinus Walleij 15918caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr) 1592ffbbdd21SLinus Walleij { 1593ffbbdd21SLinus Walleij int ret; 1594ffbbdd21SLinus Walleij 15958caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 1596ffbbdd21SLinus Walleij 1597ffbbdd21SLinus Walleij /* 15983989144fSPetr Mladek * kthread_flush_worker will block until all work is done. 1599ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 1600ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 1601ffbbdd21SLinus Walleij * return anyway. 1602ffbbdd21SLinus Walleij */ 1603ffbbdd21SLinus Walleij if (ret) { 16048caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem destroying queue\n"); 1605ffbbdd21SLinus Walleij return ret; 1606ffbbdd21SLinus Walleij } 1607ffbbdd21SLinus Walleij 16088caab75fSGeert Uytterhoeven kthread_flush_worker(&ctlr->kworker); 16098caab75fSGeert Uytterhoeven kthread_stop(ctlr->kworker_task); 1610ffbbdd21SLinus Walleij 1611ffbbdd21SLinus Walleij return 0; 1612ffbbdd21SLinus Walleij } 1613ffbbdd21SLinus Walleij 16140461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi, 16150461a414SMark Brown struct spi_message *msg, 16160461a414SMark Brown bool need_pump) 1617ffbbdd21SLinus Walleij { 16188caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 1619ffbbdd21SLinus Walleij unsigned long flags; 1620ffbbdd21SLinus Walleij 16218caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1622ffbbdd21SLinus Walleij 16238caab75fSGeert Uytterhoeven if (!ctlr->running) { 16248caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1625ffbbdd21SLinus Walleij return -ESHUTDOWN; 1626ffbbdd21SLinus Walleij } 1627ffbbdd21SLinus Walleij msg->actual_length = 0; 1628ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 1629ffbbdd21SLinus Walleij 16308caab75fSGeert Uytterhoeven list_add_tail(&msg->queue, &ctlr->queue); 1631f0125f1aSMark Brown if (!ctlr->busy && need_pump) 16328caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 1633ffbbdd21SLinus Walleij 16348caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1635ffbbdd21SLinus Walleij return 0; 1636ffbbdd21SLinus Walleij } 1637ffbbdd21SLinus Walleij 16380461a414SMark Brown /** 16390461a414SMark Brown * spi_queued_transfer - transfer function for queued transfers 16400461a414SMark Brown * @spi: spi device which is requesting transfer 16410461a414SMark Brown * @msg: spi message which is to handled is queued to driver queue 164297d56dc6SJavier Martinez Canillas * 164397d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 16440461a414SMark Brown */ 16450461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 16460461a414SMark Brown { 16470461a414SMark Brown return __spi_queued_transfer(spi, msg, true); 16480461a414SMark Brown } 16490461a414SMark Brown 16508caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr) 1651ffbbdd21SLinus Walleij { 1652ffbbdd21SLinus Walleij int ret; 1653ffbbdd21SLinus Walleij 16548caab75fSGeert Uytterhoeven ctlr->transfer = spi_queued_transfer; 16558caab75fSGeert Uytterhoeven if (!ctlr->transfer_one_message) 16568caab75fSGeert Uytterhoeven ctlr->transfer_one_message = spi_transfer_one_message; 1657ffbbdd21SLinus Walleij 1658ffbbdd21SLinus Walleij /* Initialize and start queue */ 16598caab75fSGeert Uytterhoeven ret = spi_init_queue(ctlr); 1660ffbbdd21SLinus Walleij if (ret) { 16618caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem initializing queue\n"); 1662ffbbdd21SLinus Walleij goto err_init_queue; 1663ffbbdd21SLinus Walleij } 16648caab75fSGeert Uytterhoeven ctlr->queued = true; 16658caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 1666ffbbdd21SLinus Walleij if (ret) { 16678caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem starting queue\n"); 1668ffbbdd21SLinus Walleij goto err_start_queue; 1669ffbbdd21SLinus Walleij } 1670ffbbdd21SLinus Walleij 1671ffbbdd21SLinus Walleij return 0; 1672ffbbdd21SLinus Walleij 1673ffbbdd21SLinus Walleij err_start_queue: 16748caab75fSGeert Uytterhoeven spi_destroy_queue(ctlr); 1675c3676d5cSMark Brown err_init_queue: 1676ffbbdd21SLinus Walleij return ret; 1677ffbbdd21SLinus Walleij } 1678ffbbdd21SLinus Walleij 1679988f259bSBoris Brezillon /** 1680988f259bSBoris Brezillon * spi_flush_queue - Send all pending messages in the queue from the callers' 1681988f259bSBoris Brezillon * context 1682988f259bSBoris Brezillon * @ctlr: controller to process queue for 1683988f259bSBoris Brezillon * 1684988f259bSBoris Brezillon * This should be used when one wants to ensure all pending messages have been 1685988f259bSBoris Brezillon * sent before doing something. Is used by the spi-mem code to make sure SPI 1686988f259bSBoris Brezillon * memory operations do not preempt regular SPI transfers that have been queued 1687988f259bSBoris Brezillon * before the spi-mem operation. 1688988f259bSBoris Brezillon */ 1689988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr) 1690988f259bSBoris Brezillon { 1691988f259bSBoris Brezillon if (ctlr->transfer == spi_queued_transfer) 1692988f259bSBoris Brezillon __spi_pump_messages(ctlr, false); 1693988f259bSBoris Brezillon } 1694988f259bSBoris Brezillon 1695ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 1696ffbbdd21SLinus Walleij 16977cb94361SAndreas Larsson #if defined(CONFIG_OF) 16988caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 1699c2e51ac3SGeert Uytterhoeven struct device_node *nc) 1700d57a4282SGrant Likely { 170189da4293STrent Piepho u32 value; 1702c2e51ac3SGeert Uytterhoeven int rc; 1703d57a4282SGrant Likely 1704d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 1705e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpha")) 1706d57a4282SGrant Likely spi->mode |= SPI_CPHA; 1707e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpol")) 1708d57a4282SGrant Likely spi->mode |= SPI_CPOL; 1709e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-3wire")) 1710c20151dfSLars-Peter Clausen spi->mode |= SPI_3WIRE; 1711e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-lsb-first")) 1712cd6339e6SZhao Qiang spi->mode |= SPI_LSB_FIRST; 1713d57a4282SGrant Likely 1714f3186dd8SLinus Walleij /* 1715f3186dd8SLinus Walleij * For descriptors associated with the device, polarity inversion is 1716f3186dd8SLinus Walleij * handled in the gpiolib, so all chip selects are "active high" in 1717f3186dd8SLinus Walleij * the logical sense, the gpiolib will invert the line if need be. 1718f3186dd8SLinus Walleij */ 1719f3186dd8SLinus Walleij if (ctlr->use_gpio_descriptors) 1720f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 1721f3186dd8SLinus Walleij else if (of_property_read_bool(nc, "spi-cs-high")) 1722f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 1723f3186dd8SLinus Walleij 1724f477b7fbSwangyuhang /* Device DUAL/QUAD mode */ 172589da4293STrent Piepho if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 172689da4293STrent Piepho switch (value) { 172789da4293STrent Piepho case 1: 1728f477b7fbSwangyuhang break; 172989da4293STrent Piepho case 2: 1730f477b7fbSwangyuhang spi->mode |= SPI_TX_DUAL; 1731f477b7fbSwangyuhang break; 173289da4293STrent Piepho case 4: 1733f477b7fbSwangyuhang spi->mode |= SPI_TX_QUAD; 1734f477b7fbSwangyuhang break; 17356b03061fSYogesh Narayan Gaur case 8: 17366b03061fSYogesh Narayan Gaur spi->mode |= SPI_TX_OCTAL; 17376b03061fSYogesh Narayan Gaur break; 1738f477b7fbSwangyuhang default: 17398caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 1740a110f93dSwangyuhang "spi-tx-bus-width %d not supported\n", 174189da4293STrent Piepho value); 174280874d8cSGeert Uytterhoeven break; 1743f477b7fbSwangyuhang } 1744a822e99cSMark Brown } 1745f477b7fbSwangyuhang 174689da4293STrent Piepho if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 174789da4293STrent Piepho switch (value) { 174889da4293STrent Piepho case 1: 1749f477b7fbSwangyuhang break; 175089da4293STrent Piepho case 2: 1751f477b7fbSwangyuhang spi->mode |= SPI_RX_DUAL; 1752f477b7fbSwangyuhang break; 175389da4293STrent Piepho case 4: 1754f477b7fbSwangyuhang spi->mode |= SPI_RX_QUAD; 1755f477b7fbSwangyuhang break; 17566b03061fSYogesh Narayan Gaur case 8: 17576b03061fSYogesh Narayan Gaur spi->mode |= SPI_RX_OCTAL; 17586b03061fSYogesh Narayan Gaur break; 1759f477b7fbSwangyuhang default: 17608caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 1761a110f93dSwangyuhang "spi-rx-bus-width %d not supported\n", 176289da4293STrent Piepho value); 176380874d8cSGeert Uytterhoeven break; 1764f477b7fbSwangyuhang } 1765a822e99cSMark Brown } 1766f477b7fbSwangyuhang 17678caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr)) { 1768194276b0SRob Herring if (!of_node_name_eq(nc, "slave")) { 176925c56c88SRob Herring dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 177025c56c88SRob Herring nc); 17716c364062SGeert Uytterhoeven return -EINVAL; 17726c364062SGeert Uytterhoeven } 17736c364062SGeert Uytterhoeven return 0; 17746c364062SGeert Uytterhoeven } 17756c364062SGeert Uytterhoeven 17766c364062SGeert Uytterhoeven /* Device address */ 17776c364062SGeert Uytterhoeven rc = of_property_read_u32(nc, "reg", &value); 17786c364062SGeert Uytterhoeven if (rc) { 177925c56c88SRob Herring dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 178025c56c88SRob Herring nc, rc); 17816c364062SGeert Uytterhoeven return rc; 17826c364062SGeert Uytterhoeven } 17836c364062SGeert Uytterhoeven spi->chip_select = value; 17846c364062SGeert Uytterhoeven 1785d57a4282SGrant Likely /* Device speed */ 178689da4293STrent Piepho rc = of_property_read_u32(nc, "spi-max-frequency", &value); 178789da4293STrent Piepho if (rc) { 17888caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 178925c56c88SRob Herring "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc); 1790c2e51ac3SGeert Uytterhoeven return rc; 1791d57a4282SGrant Likely } 179289da4293STrent Piepho spi->max_speed_hz = value; 1793d57a4282SGrant Likely 1794c2e51ac3SGeert Uytterhoeven return 0; 1795c2e51ac3SGeert Uytterhoeven } 1796c2e51ac3SGeert Uytterhoeven 1797c2e51ac3SGeert Uytterhoeven static struct spi_device * 17988caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 1799c2e51ac3SGeert Uytterhoeven { 1800c2e51ac3SGeert Uytterhoeven struct spi_device *spi; 1801c2e51ac3SGeert Uytterhoeven int rc; 1802c2e51ac3SGeert Uytterhoeven 1803c2e51ac3SGeert Uytterhoeven /* Alloc an spi_device */ 18048caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 1805c2e51ac3SGeert Uytterhoeven if (!spi) { 180625c56c88SRob Herring dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 1807c2e51ac3SGeert Uytterhoeven rc = -ENOMEM; 1808c2e51ac3SGeert Uytterhoeven goto err_out; 1809c2e51ac3SGeert Uytterhoeven } 1810c2e51ac3SGeert Uytterhoeven 1811c2e51ac3SGeert Uytterhoeven /* Select device driver */ 1812c2e51ac3SGeert Uytterhoeven rc = of_modalias_node(nc, spi->modalias, 1813c2e51ac3SGeert Uytterhoeven sizeof(spi->modalias)); 1814c2e51ac3SGeert Uytterhoeven if (rc < 0) { 181525c56c88SRob Herring dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 1816c2e51ac3SGeert Uytterhoeven goto err_out; 1817c2e51ac3SGeert Uytterhoeven } 1818c2e51ac3SGeert Uytterhoeven 18198caab75fSGeert Uytterhoeven rc = of_spi_parse_dt(ctlr, spi, nc); 1820c2e51ac3SGeert Uytterhoeven if (rc) 1821c2e51ac3SGeert Uytterhoeven goto err_out; 1822c2e51ac3SGeert Uytterhoeven 1823d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 1824d57a4282SGrant Likely of_node_get(nc); 1825d57a4282SGrant Likely spi->dev.of_node = nc; 1826d57a4282SGrant Likely 1827d57a4282SGrant Likely /* Register the new device */ 1828d57a4282SGrant Likely rc = spi_add_device(spi); 1829d57a4282SGrant Likely if (rc) { 183025c56c88SRob Herring dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 18318324147fSJohan Hovold goto err_of_node_put; 1832d57a4282SGrant Likely } 1833d57a4282SGrant Likely 1834aff5e3f8SPantelis Antoniou return spi; 1835aff5e3f8SPantelis Antoniou 18368324147fSJohan Hovold err_of_node_put: 18378324147fSJohan Hovold of_node_put(nc); 1838aff5e3f8SPantelis Antoniou err_out: 1839aff5e3f8SPantelis Antoniou spi_dev_put(spi); 1840aff5e3f8SPantelis Antoniou return ERR_PTR(rc); 1841aff5e3f8SPantelis Antoniou } 1842aff5e3f8SPantelis Antoniou 1843aff5e3f8SPantelis Antoniou /** 1844aff5e3f8SPantelis Antoniou * of_register_spi_devices() - Register child devices onto the SPI bus 18458caab75fSGeert Uytterhoeven * @ctlr: Pointer to spi_controller device 1846aff5e3f8SPantelis Antoniou * 18476c364062SGeert Uytterhoeven * Registers an spi_device for each child node of controller node which 18486c364062SGeert Uytterhoeven * represents a valid SPI slave. 1849aff5e3f8SPantelis Antoniou */ 18508caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) 1851aff5e3f8SPantelis Antoniou { 1852aff5e3f8SPantelis Antoniou struct spi_device *spi; 1853aff5e3f8SPantelis Antoniou struct device_node *nc; 1854aff5e3f8SPantelis Antoniou 18558caab75fSGeert Uytterhoeven if (!ctlr->dev.of_node) 1856aff5e3f8SPantelis Antoniou return; 1857aff5e3f8SPantelis Antoniou 18588caab75fSGeert Uytterhoeven for_each_available_child_of_node(ctlr->dev.of_node, nc) { 1859bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(nc, OF_POPULATED)) 1860bd6c1644SGeert Uytterhoeven continue; 18618caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, nc); 1862e0af98a7SRalf Ramsauer if (IS_ERR(spi)) { 18638caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 186425c56c88SRob Herring "Failed to create SPI device for %pOF\n", nc); 1865e0af98a7SRalf Ramsauer of_node_clear_flag(nc, OF_POPULATED); 1866e0af98a7SRalf Ramsauer } 1867d57a4282SGrant Likely } 1868d57a4282SGrant Likely } 1869d57a4282SGrant Likely #else 18708caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { } 1871d57a4282SGrant Likely #endif 1872d57a4282SGrant Likely 187364bee4d2SMika Westerberg #ifdef CONFIG_ACPI 18744c3c5954SArd Biesheuvel struct acpi_spi_lookup { 18754c3c5954SArd Biesheuvel struct spi_controller *ctlr; 18764c3c5954SArd Biesheuvel u32 max_speed_hz; 18774c3c5954SArd Biesheuvel u32 mode; 18784c3c5954SArd Biesheuvel int irq; 18794c3c5954SArd Biesheuvel u8 bits_per_word; 18804c3c5954SArd Biesheuvel u8 chip_select; 18814c3c5954SArd Biesheuvel }; 18824c3c5954SArd Biesheuvel 18834c3c5954SArd Biesheuvel static void acpi_spi_parse_apple_properties(struct acpi_device *dev, 18844c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup) 18858a2e487eSLukas Wunner { 18868a2e487eSLukas Wunner const union acpi_object *obj; 18878a2e487eSLukas Wunner 18888a2e487eSLukas Wunner if (!x86_apple_machine) 18898a2e487eSLukas Wunner return; 18908a2e487eSLukas Wunner 18918a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 18928a2e487eSLukas Wunner && obj->buffer.length >= 4) 18934c3c5954SArd Biesheuvel lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 18948a2e487eSLukas Wunner 18958a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 18968a2e487eSLukas Wunner && obj->buffer.length == 8) 18974c3c5954SArd Biesheuvel lookup->bits_per_word = *(u64 *)obj->buffer.pointer; 18988a2e487eSLukas Wunner 18998a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 19008a2e487eSLukas Wunner && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 19014c3c5954SArd Biesheuvel lookup->mode |= SPI_LSB_FIRST; 19028a2e487eSLukas Wunner 19038a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 19048a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 19054c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 19068a2e487eSLukas Wunner 19078a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 19088a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 19094c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 19108a2e487eSLukas Wunner } 19118a2e487eSLukas Wunner 191264bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 191364bee4d2SMika Westerberg { 19144c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup = data; 19154c3c5954SArd Biesheuvel struct spi_controller *ctlr = lookup->ctlr; 191664bee4d2SMika Westerberg 191764bee4d2SMika Westerberg if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 191864bee4d2SMika Westerberg struct acpi_resource_spi_serialbus *sb; 19194c3c5954SArd Biesheuvel acpi_handle parent_handle; 19204c3c5954SArd Biesheuvel acpi_status status; 192164bee4d2SMika Westerberg 192264bee4d2SMika Westerberg sb = &ares->data.spi_serial_bus; 192364bee4d2SMika Westerberg if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 19244c3c5954SArd Biesheuvel 19254c3c5954SArd Biesheuvel status = acpi_get_handle(NULL, 19264c3c5954SArd Biesheuvel sb->resource_source.string_ptr, 19274c3c5954SArd Biesheuvel &parent_handle); 19284c3c5954SArd Biesheuvel 1929b5e3cf41SArd Biesheuvel if (ACPI_FAILURE(status) || 19304c3c5954SArd Biesheuvel ACPI_HANDLE(ctlr->dev.parent) != parent_handle) 19314c3c5954SArd Biesheuvel return -ENODEV; 19324c3c5954SArd Biesheuvel 1933a0a90718SMika Westerberg /* 1934a0a90718SMika Westerberg * ACPI DeviceSelection numbering is handled by the 1935a0a90718SMika Westerberg * host controller driver in Windows and can vary 1936a0a90718SMika Westerberg * from driver to driver. In Linux we always expect 1937a0a90718SMika Westerberg * 0 .. max - 1 so we need to ask the driver to 1938a0a90718SMika Westerberg * translate between the two schemes. 1939a0a90718SMika Westerberg */ 19408caab75fSGeert Uytterhoeven if (ctlr->fw_translate_cs) { 19418caab75fSGeert Uytterhoeven int cs = ctlr->fw_translate_cs(ctlr, 1942a0a90718SMika Westerberg sb->device_selection); 1943a0a90718SMika Westerberg if (cs < 0) 1944a0a90718SMika Westerberg return cs; 19454c3c5954SArd Biesheuvel lookup->chip_select = cs; 1946a0a90718SMika Westerberg } else { 19474c3c5954SArd Biesheuvel lookup->chip_select = sb->device_selection; 1948a0a90718SMika Westerberg } 1949a0a90718SMika Westerberg 19504c3c5954SArd Biesheuvel lookup->max_speed_hz = sb->connection_speed; 195164bee4d2SMika Westerberg 195264bee4d2SMika Westerberg if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 19534c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 195464bee4d2SMika Westerberg if (sb->clock_polarity == ACPI_SPI_START_HIGH) 19554c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 195664bee4d2SMika Westerberg if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 19574c3c5954SArd Biesheuvel lookup->mode |= SPI_CS_HIGH; 195864bee4d2SMika Westerberg } 19594c3c5954SArd Biesheuvel } else if (lookup->irq < 0) { 196064bee4d2SMika Westerberg struct resource r; 196164bee4d2SMika Westerberg 196264bee4d2SMika Westerberg if (acpi_dev_resource_interrupt(ares, 0, &r)) 19634c3c5954SArd Biesheuvel lookup->irq = r.start; 196464bee4d2SMika Westerberg } 196564bee4d2SMika Westerberg 196664bee4d2SMika Westerberg /* Always tell the ACPI core to skip this resource */ 196764bee4d2SMika Westerberg return 1; 196864bee4d2SMika Westerberg } 196964bee4d2SMika Westerberg 19708caab75fSGeert Uytterhoeven static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 19717f24467fSOctavian Purdila struct acpi_device *adev) 197264bee4d2SMika Westerberg { 19734c3c5954SArd Biesheuvel acpi_handle parent_handle = NULL; 197464bee4d2SMika Westerberg struct list_head resource_list; 1975b28944c6SArd Biesheuvel struct acpi_spi_lookup lookup = {}; 197664bee4d2SMika Westerberg struct spi_device *spi; 197764bee4d2SMika Westerberg int ret; 197864bee4d2SMika Westerberg 19797f24467fSOctavian Purdila if (acpi_bus_get_status(adev) || !adev->status.present || 19807f24467fSOctavian Purdila acpi_device_enumerated(adev)) 198164bee4d2SMika Westerberg return AE_OK; 198264bee4d2SMika Westerberg 19834c3c5954SArd Biesheuvel lookup.ctlr = ctlr; 19844c3c5954SArd Biesheuvel lookup.irq = -1; 19854c3c5954SArd Biesheuvel 19864c3c5954SArd Biesheuvel INIT_LIST_HEAD(&resource_list); 19874c3c5954SArd Biesheuvel ret = acpi_dev_get_resources(adev, &resource_list, 19884c3c5954SArd Biesheuvel acpi_spi_add_resource, &lookup); 19894c3c5954SArd Biesheuvel acpi_dev_free_resource_list(&resource_list); 19904c3c5954SArd Biesheuvel 19914c3c5954SArd Biesheuvel if (ret < 0) 19924c3c5954SArd Biesheuvel /* found SPI in _CRS but it points to another controller */ 19934c3c5954SArd Biesheuvel return AE_OK; 19944c3c5954SArd Biesheuvel 19954c3c5954SArd Biesheuvel if (!lookup.max_speed_hz && 19964c3c5954SArd Biesheuvel !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) && 19974c3c5954SArd Biesheuvel ACPI_HANDLE(ctlr->dev.parent) == parent_handle) { 19984c3c5954SArd Biesheuvel /* Apple does not use _CRS but nested devices for SPI slaves */ 19994c3c5954SArd Biesheuvel acpi_spi_parse_apple_properties(adev, &lookup); 20004c3c5954SArd Biesheuvel } 20014c3c5954SArd Biesheuvel 20024c3c5954SArd Biesheuvel if (!lookup.max_speed_hz) 20034c3c5954SArd Biesheuvel return AE_OK; 20044c3c5954SArd Biesheuvel 20058caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 200664bee4d2SMika Westerberg if (!spi) { 20078caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", 200864bee4d2SMika Westerberg dev_name(&adev->dev)); 200964bee4d2SMika Westerberg return AE_NO_MEMORY; 201064bee4d2SMika Westerberg } 201164bee4d2SMika Westerberg 20127b199811SRafael J. Wysocki ACPI_COMPANION_SET(&spi->dev, adev); 20134c3c5954SArd Biesheuvel spi->max_speed_hz = lookup.max_speed_hz; 20144c3c5954SArd Biesheuvel spi->mode = lookup.mode; 20154c3c5954SArd Biesheuvel spi->irq = lookup.irq; 20164c3c5954SArd Biesheuvel spi->bits_per_word = lookup.bits_per_word; 20174c3c5954SArd Biesheuvel spi->chip_select = lookup.chip_select; 201864bee4d2SMika Westerberg 20190c6543f6SDan O'Donovan acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 20200c6543f6SDan O'Donovan sizeof(spi->modalias)); 20210c6543f6SDan O'Donovan 202233ada67dSChristophe RICARD if (spi->irq < 0) 202333ada67dSChristophe RICARD spi->irq = acpi_dev_gpio_irq_get(adev, 0); 202433ada67dSChristophe RICARD 20257f24467fSOctavian Purdila acpi_device_set_enumerated(adev); 20267f24467fSOctavian Purdila 202733cf00e5SMika Westerberg adev->power.flags.ignore_parent = true; 202864bee4d2SMika Westerberg if (spi_add_device(spi)) { 202933cf00e5SMika Westerberg adev->power.flags.ignore_parent = false; 20308caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 203164bee4d2SMika Westerberg dev_name(&adev->dev)); 203264bee4d2SMika Westerberg spi_dev_put(spi); 203364bee4d2SMika Westerberg } 203464bee4d2SMika Westerberg 203564bee4d2SMika Westerberg return AE_OK; 203664bee4d2SMika Westerberg } 203764bee4d2SMika Westerberg 20387f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 20397f24467fSOctavian Purdila void *data, void **return_value) 20407f24467fSOctavian Purdila { 20418caab75fSGeert Uytterhoeven struct spi_controller *ctlr = data; 20427f24467fSOctavian Purdila struct acpi_device *adev; 20437f24467fSOctavian Purdila 20447f24467fSOctavian Purdila if (acpi_bus_get_device(handle, &adev)) 20457f24467fSOctavian Purdila return AE_OK; 20467f24467fSOctavian Purdila 20478caab75fSGeert Uytterhoeven return acpi_register_spi_device(ctlr, adev); 20487f24467fSOctavian Purdila } 20497f24467fSOctavian Purdila 20504c3c5954SArd Biesheuvel #define SPI_ACPI_ENUMERATE_MAX_DEPTH 32 20514c3c5954SArd Biesheuvel 20528caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr) 205364bee4d2SMika Westerberg { 205464bee4d2SMika Westerberg acpi_status status; 205564bee4d2SMika Westerberg acpi_handle handle; 205664bee4d2SMika Westerberg 20578caab75fSGeert Uytterhoeven handle = ACPI_HANDLE(ctlr->dev.parent); 205864bee4d2SMika Westerberg if (!handle) 205964bee4d2SMika Westerberg return; 206064bee4d2SMika Westerberg 20614c3c5954SArd Biesheuvel status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 20624c3c5954SArd Biesheuvel SPI_ACPI_ENUMERATE_MAX_DEPTH, 20638caab75fSGeert Uytterhoeven acpi_spi_add_device, NULL, ctlr, NULL); 206464bee4d2SMika Westerberg if (ACPI_FAILURE(status)) 20658caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 206664bee4d2SMika Westerberg } 206764bee4d2SMika Westerberg #else 20688caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 206964bee4d2SMika Westerberg #endif /* CONFIG_ACPI */ 207064bee4d2SMika Westerberg 20718caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev) 20728ae12a0dSDavid Brownell { 20738caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 20748ae12a0dSDavid Brownell 20758caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 20768caab75fSGeert Uytterhoeven kfree(ctlr); 20778ae12a0dSDavid Brownell } 20788ae12a0dSDavid Brownell 20798ae12a0dSDavid Brownell static struct class spi_master_class = { 20808ae12a0dSDavid Brownell .name = "spi_master", 20818ae12a0dSDavid Brownell .owner = THIS_MODULE, 20828caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 2083eca2ebc7SMartin Sperl .dev_groups = spi_master_groups, 20848ae12a0dSDavid Brownell }; 20858ae12a0dSDavid Brownell 20866c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE 20876c364062SGeert Uytterhoeven /** 20886c364062SGeert Uytterhoeven * spi_slave_abort - abort the ongoing transfer request on an SPI slave 20896c364062SGeert Uytterhoeven * controller 20906c364062SGeert Uytterhoeven * @spi: device used for the current transfer 20916c364062SGeert Uytterhoeven */ 20926c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi) 20936c364062SGeert Uytterhoeven { 20948caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 20956c364062SGeert Uytterhoeven 20968caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 20978caab75fSGeert Uytterhoeven return ctlr->slave_abort(ctlr); 20986c364062SGeert Uytterhoeven 20996c364062SGeert Uytterhoeven return -ENOTSUPP; 21006c364062SGeert Uytterhoeven } 21016c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort); 21026c364062SGeert Uytterhoeven 21036c364062SGeert Uytterhoeven static int match_true(struct device *dev, void *data) 21046c364062SGeert Uytterhoeven { 21056c364062SGeert Uytterhoeven return 1; 21066c364062SGeert Uytterhoeven } 21076c364062SGeert Uytterhoeven 21086c364062SGeert Uytterhoeven static ssize_t spi_slave_show(struct device *dev, 21096c364062SGeert Uytterhoeven struct device_attribute *attr, char *buf) 21106c364062SGeert Uytterhoeven { 21118caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 21128caab75fSGeert Uytterhoeven dev); 21136c364062SGeert Uytterhoeven struct device *child; 21146c364062SGeert Uytterhoeven 21156c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 21166c364062SGeert Uytterhoeven return sprintf(buf, "%s\n", 21176c364062SGeert Uytterhoeven child ? to_spi_device(child)->modalias : NULL); 21186c364062SGeert Uytterhoeven } 21196c364062SGeert Uytterhoeven 21206c364062SGeert Uytterhoeven static ssize_t spi_slave_store(struct device *dev, 21216c364062SGeert Uytterhoeven struct device_attribute *attr, const char *buf, 21226c364062SGeert Uytterhoeven size_t count) 21236c364062SGeert Uytterhoeven { 21248caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 21258caab75fSGeert Uytterhoeven dev); 21266c364062SGeert Uytterhoeven struct spi_device *spi; 21276c364062SGeert Uytterhoeven struct device *child; 21286c364062SGeert Uytterhoeven char name[32]; 21296c364062SGeert Uytterhoeven int rc; 21306c364062SGeert Uytterhoeven 21316c364062SGeert Uytterhoeven rc = sscanf(buf, "%31s", name); 21326c364062SGeert Uytterhoeven if (rc != 1 || !name[0]) 21336c364062SGeert Uytterhoeven return -EINVAL; 21346c364062SGeert Uytterhoeven 21356c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 21366c364062SGeert Uytterhoeven if (child) { 21376c364062SGeert Uytterhoeven /* Remove registered slave */ 21386c364062SGeert Uytterhoeven device_unregister(child); 21396c364062SGeert Uytterhoeven put_device(child); 21406c364062SGeert Uytterhoeven } 21416c364062SGeert Uytterhoeven 21426c364062SGeert Uytterhoeven if (strcmp(name, "(null)")) { 21436c364062SGeert Uytterhoeven /* Register new slave */ 21446c364062SGeert Uytterhoeven spi = spi_alloc_device(ctlr); 21456c364062SGeert Uytterhoeven if (!spi) 21466c364062SGeert Uytterhoeven return -ENOMEM; 21476c364062SGeert Uytterhoeven 21486c364062SGeert Uytterhoeven strlcpy(spi->modalias, name, sizeof(spi->modalias)); 21496c364062SGeert Uytterhoeven 21506c364062SGeert Uytterhoeven rc = spi_add_device(spi); 21516c364062SGeert Uytterhoeven if (rc) { 21526c364062SGeert Uytterhoeven spi_dev_put(spi); 21536c364062SGeert Uytterhoeven return rc; 21546c364062SGeert Uytterhoeven } 21556c364062SGeert Uytterhoeven } 21566c364062SGeert Uytterhoeven 21576c364062SGeert Uytterhoeven return count; 21586c364062SGeert Uytterhoeven } 21596c364062SGeert Uytterhoeven 21606c364062SGeert Uytterhoeven static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store); 21616c364062SGeert Uytterhoeven 21626c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = { 21636c364062SGeert Uytterhoeven &dev_attr_slave.attr, 21646c364062SGeert Uytterhoeven NULL, 21656c364062SGeert Uytterhoeven }; 21666c364062SGeert Uytterhoeven 21676c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = { 21686c364062SGeert Uytterhoeven .attrs = spi_slave_attrs, 21696c364062SGeert Uytterhoeven }; 21706c364062SGeert Uytterhoeven 21716c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = { 21728caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 21736c364062SGeert Uytterhoeven &spi_slave_group, 21746c364062SGeert Uytterhoeven NULL, 21756c364062SGeert Uytterhoeven }; 21766c364062SGeert Uytterhoeven 21776c364062SGeert Uytterhoeven static struct class spi_slave_class = { 21786c364062SGeert Uytterhoeven .name = "spi_slave", 21796c364062SGeert Uytterhoeven .owner = THIS_MODULE, 21808caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 21816c364062SGeert Uytterhoeven .dev_groups = spi_slave_groups, 21826c364062SGeert Uytterhoeven }; 21836c364062SGeert Uytterhoeven #else 21846c364062SGeert Uytterhoeven extern struct class spi_slave_class; /* dummy */ 21856c364062SGeert Uytterhoeven #endif 21868ae12a0dSDavid Brownell 21878ae12a0dSDavid Brownell /** 21886c364062SGeert Uytterhoeven * __spi_alloc_controller - allocate an SPI master or slave controller 21898ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 219033e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 219149dce689STony Jones * memory is in the driver_data field of the returned device, 21928caab75fSGeert Uytterhoeven * accessible with spi_controller_get_devdata(). 21936c364062SGeert Uytterhoeven * @slave: flag indicating whether to allocate an SPI master (false) or SPI 21946c364062SGeert Uytterhoeven * slave (true) controller 219533e34dc6SDavid Brownell * Context: can sleep 21968ae12a0dSDavid Brownell * 21976c364062SGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 21988ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 21998caab75fSGeert Uytterhoeven * an spi_controller structure, prior to calling spi_register_controller(). 22008ae12a0dSDavid Brownell * 220197d56dc6SJavier Martinez Canillas * This must be called from context that can sleep. 22028ae12a0dSDavid Brownell * 22036c364062SGeert Uytterhoeven * The caller is responsible for assigning the bus number and initializing the 22048caab75fSGeert Uytterhoeven * controller's methods before calling spi_register_controller(); and (after 22058caab75fSGeert Uytterhoeven * errors adding the device) calling spi_controller_put() to prevent a memory 22068caab75fSGeert Uytterhoeven * leak. 220797d56dc6SJavier Martinez Canillas * 22086c364062SGeert Uytterhoeven * Return: the SPI controller structure on success, else NULL. 22098ae12a0dSDavid Brownell */ 22108caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev, 22116c364062SGeert Uytterhoeven unsigned int size, bool slave) 22128ae12a0dSDavid Brownell { 22138caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 22148ae12a0dSDavid Brownell 22150c868461SDavid Brownell if (!dev) 22160c868461SDavid Brownell return NULL; 22170c868461SDavid Brownell 22188caab75fSGeert Uytterhoeven ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL); 22198caab75fSGeert Uytterhoeven if (!ctlr) 22208ae12a0dSDavid Brownell return NULL; 22218ae12a0dSDavid Brownell 22228caab75fSGeert Uytterhoeven device_initialize(&ctlr->dev); 22238caab75fSGeert Uytterhoeven ctlr->bus_num = -1; 22248caab75fSGeert Uytterhoeven ctlr->num_chipselect = 1; 22258caab75fSGeert Uytterhoeven ctlr->slave = slave; 22266c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 22278caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_slave_class; 22286c364062SGeert Uytterhoeven else 22298caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_master_class; 22308caab75fSGeert Uytterhoeven ctlr->dev.parent = dev; 22318caab75fSGeert Uytterhoeven pm_suspend_ignore_children(&ctlr->dev, true); 22328caab75fSGeert Uytterhoeven spi_controller_set_devdata(ctlr, &ctlr[1]); 22338ae12a0dSDavid Brownell 22348caab75fSGeert Uytterhoeven return ctlr; 22358ae12a0dSDavid Brownell } 22366c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller); 22378ae12a0dSDavid Brownell 223874317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF 22398caab75fSGeert Uytterhoeven static int of_spi_register_master(struct spi_controller *ctlr) 224074317984SJean-Christophe PLAGNIOL-VILLARD { 2241e80beb27SGrant Likely int nb, i, *cs; 22428caab75fSGeert Uytterhoeven struct device_node *np = ctlr->dev.of_node; 224374317984SJean-Christophe PLAGNIOL-VILLARD 224474317984SJean-Christophe PLAGNIOL-VILLARD if (!np) 224574317984SJean-Christophe PLAGNIOL-VILLARD return 0; 224674317984SJean-Christophe PLAGNIOL-VILLARD 224774317984SJean-Christophe PLAGNIOL-VILLARD nb = of_gpio_named_count(np, "cs-gpios"); 22488caab75fSGeert Uytterhoeven ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 224974317984SJean-Christophe PLAGNIOL-VILLARD 22508ec5d84eSAndreas Larsson /* Return error only for an incorrectly formed cs-gpios property */ 22518ec5d84eSAndreas Larsson if (nb == 0 || nb == -ENOENT) 225274317984SJean-Christophe PLAGNIOL-VILLARD return 0; 22538ec5d84eSAndreas Larsson else if (nb < 0) 22548ec5d84eSAndreas Larsson return nb; 225574317984SJean-Christophe PLAGNIOL-VILLARD 2256a86854d0SKees Cook cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), 225774317984SJean-Christophe PLAGNIOL-VILLARD GFP_KERNEL); 22588caab75fSGeert Uytterhoeven ctlr->cs_gpios = cs; 225974317984SJean-Christophe PLAGNIOL-VILLARD 22608caab75fSGeert Uytterhoeven if (!ctlr->cs_gpios) 226174317984SJean-Christophe PLAGNIOL-VILLARD return -ENOMEM; 226274317984SJean-Christophe PLAGNIOL-VILLARD 22638caab75fSGeert Uytterhoeven for (i = 0; i < ctlr->num_chipselect; i++) 2264446411e1SAndreas Larsson cs[i] = -ENOENT; 226574317984SJean-Christophe PLAGNIOL-VILLARD 226674317984SJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < nb; i++) 226774317984SJean-Christophe PLAGNIOL-VILLARD cs[i] = of_get_named_gpio(np, "cs-gpios", i); 226874317984SJean-Christophe PLAGNIOL-VILLARD 226974317984SJean-Christophe PLAGNIOL-VILLARD return 0; 227074317984SJean-Christophe PLAGNIOL-VILLARD } 227174317984SJean-Christophe PLAGNIOL-VILLARD #else 22728caab75fSGeert Uytterhoeven static int of_spi_register_master(struct spi_controller *ctlr) 227374317984SJean-Christophe PLAGNIOL-VILLARD { 227474317984SJean-Christophe PLAGNIOL-VILLARD return 0; 227574317984SJean-Christophe PLAGNIOL-VILLARD } 227674317984SJean-Christophe PLAGNIOL-VILLARD #endif 227774317984SJean-Christophe PLAGNIOL-VILLARD 2278f3186dd8SLinus Walleij /** 2279f3186dd8SLinus Walleij * spi_get_gpio_descs() - grab chip select GPIOs for the master 2280f3186dd8SLinus Walleij * @ctlr: The SPI master to grab GPIO descriptors for 2281f3186dd8SLinus Walleij */ 2282f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr) 2283f3186dd8SLinus Walleij { 2284f3186dd8SLinus Walleij int nb, i; 2285f3186dd8SLinus Walleij struct gpio_desc **cs; 2286f3186dd8SLinus Walleij struct device *dev = &ctlr->dev; 2287f3186dd8SLinus Walleij 2288f3186dd8SLinus Walleij nb = gpiod_count(dev, "cs"); 2289f3186dd8SLinus Walleij ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2290f3186dd8SLinus Walleij 2291f3186dd8SLinus Walleij /* No GPIOs at all is fine, else return the error */ 2292f3186dd8SLinus Walleij if (nb == 0 || nb == -ENOENT) 2293f3186dd8SLinus Walleij return 0; 2294f3186dd8SLinus Walleij else if (nb < 0) 2295f3186dd8SLinus Walleij return nb; 2296f3186dd8SLinus Walleij 2297f3186dd8SLinus Walleij cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), 2298f3186dd8SLinus Walleij GFP_KERNEL); 2299f3186dd8SLinus Walleij if (!cs) 2300f3186dd8SLinus Walleij return -ENOMEM; 2301f3186dd8SLinus Walleij ctlr->cs_gpiods = cs; 2302f3186dd8SLinus Walleij 2303f3186dd8SLinus Walleij for (i = 0; i < nb; i++) { 2304f3186dd8SLinus Walleij /* 2305f3186dd8SLinus Walleij * Most chipselects are active low, the inverted 2306f3186dd8SLinus Walleij * semantics are handled by special quirks in gpiolib, 2307f3186dd8SLinus Walleij * so initializing them GPIOD_OUT_LOW here means 2308f3186dd8SLinus Walleij * "unasserted", in most cases this will drive the physical 2309f3186dd8SLinus Walleij * line high. 2310f3186dd8SLinus Walleij */ 2311f3186dd8SLinus Walleij cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, 2312f3186dd8SLinus Walleij GPIOD_OUT_LOW); 23131723fdecSGeert Uytterhoeven if (IS_ERR(cs[i])) 23141723fdecSGeert Uytterhoeven return PTR_ERR(cs[i]); 2315f3186dd8SLinus Walleij 2316f3186dd8SLinus Walleij if (cs[i]) { 2317f3186dd8SLinus Walleij /* 2318f3186dd8SLinus Walleij * If we find a CS GPIO, name it after the device and 2319f3186dd8SLinus Walleij * chip select line. 2320f3186dd8SLinus Walleij */ 2321f3186dd8SLinus Walleij char *gpioname; 2322f3186dd8SLinus Walleij 2323f3186dd8SLinus Walleij gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", 2324f3186dd8SLinus Walleij dev_name(dev), i); 2325f3186dd8SLinus Walleij if (!gpioname) 2326f3186dd8SLinus Walleij return -ENOMEM; 2327f3186dd8SLinus Walleij gpiod_set_consumer_name(cs[i], gpioname); 2328f3186dd8SLinus Walleij } 2329f3186dd8SLinus Walleij } 2330f3186dd8SLinus Walleij 2331f3186dd8SLinus Walleij return 0; 2332f3186dd8SLinus Walleij } 2333f3186dd8SLinus Walleij 2334bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr) 2335bdf3a3b5SBoris Brezillon { 2336bdf3a3b5SBoris Brezillon /* 2337b5932f5cSBoris Brezillon * The controller may implement only the high-level SPI-memory like 2338b5932f5cSBoris Brezillon * operations if it does not support regular SPI transfers, and this is 2339b5932f5cSBoris Brezillon * valid use case. 2340b5932f5cSBoris Brezillon * If ->mem_ops is NULL, we request that at least one of the 2341b5932f5cSBoris Brezillon * ->transfer_xxx() method be implemented. 2342bdf3a3b5SBoris Brezillon */ 2343b5932f5cSBoris Brezillon if (ctlr->mem_ops) { 2344b5932f5cSBoris Brezillon if (!ctlr->mem_ops->exec_op) 2345bdf3a3b5SBoris Brezillon return -EINVAL; 2346b5932f5cSBoris Brezillon } else if (!ctlr->transfer && !ctlr->transfer_one && 2347b5932f5cSBoris Brezillon !ctlr->transfer_one_message) { 2348b5932f5cSBoris Brezillon return -EINVAL; 2349b5932f5cSBoris Brezillon } 2350bdf3a3b5SBoris Brezillon 2351bdf3a3b5SBoris Brezillon return 0; 2352bdf3a3b5SBoris Brezillon } 2353bdf3a3b5SBoris Brezillon 23548ae12a0dSDavid Brownell /** 23558caab75fSGeert Uytterhoeven * spi_register_controller - register SPI master or slave controller 23568caab75fSGeert Uytterhoeven * @ctlr: initialized master, originally from spi_alloc_master() or 23578caab75fSGeert Uytterhoeven * spi_alloc_slave() 235833e34dc6SDavid Brownell * Context: can sleep 23598ae12a0dSDavid Brownell * 23608caab75fSGeert Uytterhoeven * SPI controllers connect to their drivers using some non-SPI bus, 23618ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 23628caab75fSGeert Uytterhoeven * includes calling spi_register_controller() to hook up to this SPI bus glue. 23638ae12a0dSDavid Brownell * 23648ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 23658ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 23668ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 23678ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 23688ae12a0dSDavid Brownell * chip is at which address. 23698ae12a0dSDavid Brownell * 23708ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 23718caab75fSGeert Uytterhoeven * success, else a negative error code (dropping the controller's refcount). 23720c868461SDavid Brownell * After a successful return, the caller is responsible for calling 23738caab75fSGeert Uytterhoeven * spi_unregister_controller(). 237497d56dc6SJavier Martinez Canillas * 237597d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 23768ae12a0dSDavid Brownell */ 23778caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr) 23788ae12a0dSDavid Brownell { 23798caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 23802b9603a0SFeng Tang struct boardinfo *bi; 2381b93318a2SSergei Shtylyov int status; 238242bdd706SLucas Stach int id, first_dynamic; 23838ae12a0dSDavid Brownell 23840c868461SDavid Brownell if (!dev) 23850c868461SDavid Brownell return -ENODEV; 23860c868461SDavid Brownell 2387bdf3a3b5SBoris Brezillon /* 2388bdf3a3b5SBoris Brezillon * Make sure all necessary hooks are implemented before registering 2389bdf3a3b5SBoris Brezillon * the SPI controller. 2390bdf3a3b5SBoris Brezillon */ 2391bdf3a3b5SBoris Brezillon status = spi_controller_check_ops(ctlr); 2392bdf3a3b5SBoris Brezillon if (status) 2393bdf3a3b5SBoris Brezillon return status; 2394bdf3a3b5SBoris Brezillon 239504b2d03aSGeert Uytterhoeven if (ctlr->bus_num >= 0) { 239604b2d03aSGeert Uytterhoeven /* devices with a fixed bus num must check-in with the num */ 239704b2d03aSGeert Uytterhoeven mutex_lock(&board_lock); 239804b2d03aSGeert Uytterhoeven id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 239904b2d03aSGeert Uytterhoeven ctlr->bus_num + 1, GFP_KERNEL); 240004b2d03aSGeert Uytterhoeven mutex_unlock(&board_lock); 240104b2d03aSGeert Uytterhoeven if (WARN(id < 0, "couldn't get idr")) 240204b2d03aSGeert Uytterhoeven return id == -ENOSPC ? -EBUSY : id; 240304b2d03aSGeert Uytterhoeven ctlr->bus_num = id; 240404b2d03aSGeert Uytterhoeven } else if (ctlr->dev.of_node) { 24059b61e302SSuniel Mahesh /* allocate dynamic bus number using Linux idr */ 24069b61e302SSuniel Mahesh id = of_alias_get_id(ctlr->dev.of_node, "spi"); 24079b61e302SSuniel Mahesh if (id >= 0) { 24089b61e302SSuniel Mahesh ctlr->bus_num = id; 24099b61e302SSuniel Mahesh mutex_lock(&board_lock); 24109b61e302SSuniel Mahesh id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 24119b61e302SSuniel Mahesh ctlr->bus_num + 1, GFP_KERNEL); 24129b61e302SSuniel Mahesh mutex_unlock(&board_lock); 24139b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 24149b61e302SSuniel Mahesh return id == -ENOSPC ? -EBUSY : id; 24159b61e302SSuniel Mahesh } 24169b61e302SSuniel Mahesh } 24178caab75fSGeert Uytterhoeven if (ctlr->bus_num < 0) { 241842bdd706SLucas Stach first_dynamic = of_alias_get_highest_id("spi"); 241942bdd706SLucas Stach if (first_dynamic < 0) 242042bdd706SLucas Stach first_dynamic = 0; 242142bdd706SLucas Stach else 242242bdd706SLucas Stach first_dynamic++; 242342bdd706SLucas Stach 24249b61e302SSuniel Mahesh mutex_lock(&board_lock); 242542bdd706SLucas Stach id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, 242642bdd706SLucas Stach 0, GFP_KERNEL); 24279b61e302SSuniel Mahesh mutex_unlock(&board_lock); 24289b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 24299b61e302SSuniel Mahesh return id; 24309b61e302SSuniel Mahesh ctlr->bus_num = id; 24318ae12a0dSDavid Brownell } 24328caab75fSGeert Uytterhoeven INIT_LIST_HEAD(&ctlr->queue); 24338caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->queue_lock); 24348caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->bus_lock_spinlock); 24358caab75fSGeert Uytterhoeven mutex_init(&ctlr->bus_lock_mutex); 24368caab75fSGeert Uytterhoeven mutex_init(&ctlr->io_mutex); 24378caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 24388caab75fSGeert Uytterhoeven init_completion(&ctlr->xfer_completion); 24398caab75fSGeert Uytterhoeven if (!ctlr->max_dma_len) 24408caab75fSGeert Uytterhoeven ctlr->max_dma_len = INT_MAX; 2441cf32b71eSErnst Schwab 24428ae12a0dSDavid Brownell /* register the device, then userspace will see it. 24438ae12a0dSDavid Brownell * registration fails if the bus ID is in use. 24448ae12a0dSDavid Brownell */ 24458caab75fSGeert Uytterhoeven dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 24460a919ae4SAndrey Smirnov 24470a919ae4SAndrey Smirnov if (!spi_controller_is_slave(ctlr)) { 24480a919ae4SAndrey Smirnov if (ctlr->use_gpio_descriptors) { 24490a919ae4SAndrey Smirnov status = spi_get_gpio_descs(ctlr); 24500a919ae4SAndrey Smirnov if (status) 24510a919ae4SAndrey Smirnov return status; 24520a919ae4SAndrey Smirnov /* 24530a919ae4SAndrey Smirnov * A controller using GPIO descriptors always 24540a919ae4SAndrey Smirnov * supports SPI_CS_HIGH if need be. 24550a919ae4SAndrey Smirnov */ 24560a919ae4SAndrey Smirnov ctlr->mode_bits |= SPI_CS_HIGH; 24570a919ae4SAndrey Smirnov } else { 24580a919ae4SAndrey Smirnov /* Legacy code path for GPIOs from DT */ 24590a919ae4SAndrey Smirnov status = of_spi_register_master(ctlr); 24600a919ae4SAndrey Smirnov if (status) 24610a919ae4SAndrey Smirnov return status; 24620a919ae4SAndrey Smirnov } 24630a919ae4SAndrey Smirnov } 24640a919ae4SAndrey Smirnov 2465f9481b08STudor Ambarus /* 2466f9481b08STudor Ambarus * Even if it's just one always-selected device, there must 2467f9481b08STudor Ambarus * be at least one chipselect. 2468f9481b08STudor Ambarus */ 2469f9481b08STudor Ambarus if (!ctlr->num_chipselect) 2470f9481b08STudor Ambarus return -EINVAL; 2471f9481b08STudor Ambarus 24728caab75fSGeert Uytterhoeven status = device_add(&ctlr->dev); 24739b61e302SSuniel Mahesh if (status < 0) { 24749b61e302SSuniel Mahesh /* free bus id */ 24759b61e302SSuniel Mahesh mutex_lock(&board_lock); 24769b61e302SSuniel Mahesh idr_remove(&spi_master_idr, ctlr->bus_num); 24779b61e302SSuniel Mahesh mutex_unlock(&board_lock); 24788ae12a0dSDavid Brownell goto done; 24799b61e302SSuniel Mahesh } 24809b61e302SSuniel Mahesh dev_dbg(dev, "registered %s %s\n", 24818caab75fSGeert Uytterhoeven spi_controller_is_slave(ctlr) ? "slave" : "master", 24829b61e302SSuniel Mahesh dev_name(&ctlr->dev)); 24838ae12a0dSDavid Brownell 2484b5932f5cSBoris Brezillon /* 2485b5932f5cSBoris Brezillon * If we're using a queued driver, start the queue. Note that we don't 2486b5932f5cSBoris Brezillon * need the queueing logic if the driver is only supporting high-level 2487b5932f5cSBoris Brezillon * memory operations. 2488b5932f5cSBoris Brezillon */ 2489b5932f5cSBoris Brezillon if (ctlr->transfer) { 24908caab75fSGeert Uytterhoeven dev_info(dev, "controller is unqueued, this is deprecated\n"); 2491b5932f5cSBoris Brezillon } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 24928caab75fSGeert Uytterhoeven status = spi_controller_initialize_queue(ctlr); 2493ffbbdd21SLinus Walleij if (status) { 24948caab75fSGeert Uytterhoeven device_del(&ctlr->dev); 24959b61e302SSuniel Mahesh /* free bus id */ 24969b61e302SSuniel Mahesh mutex_lock(&board_lock); 24979b61e302SSuniel Mahesh idr_remove(&spi_master_idr, ctlr->bus_num); 24989b61e302SSuniel Mahesh mutex_unlock(&board_lock); 2499ffbbdd21SLinus Walleij goto done; 2500ffbbdd21SLinus Walleij } 2501ffbbdd21SLinus Walleij } 2502eca2ebc7SMartin Sperl /* add statistics */ 25038caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->statistics.lock); 2504ffbbdd21SLinus Walleij 25052b9603a0SFeng Tang mutex_lock(&board_lock); 25068caab75fSGeert Uytterhoeven list_add_tail(&ctlr->list, &spi_controller_list); 25072b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 25088caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 25092b9603a0SFeng Tang mutex_unlock(&board_lock); 25102b9603a0SFeng Tang 251164bee4d2SMika Westerberg /* Register devices from the device tree and ACPI */ 25128caab75fSGeert Uytterhoeven of_register_spi_devices(ctlr); 25138caab75fSGeert Uytterhoeven acpi_register_spi_devices(ctlr); 25148ae12a0dSDavid Brownell done: 25158ae12a0dSDavid Brownell return status; 25168ae12a0dSDavid Brownell } 25178caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller); 25188ae12a0dSDavid Brownell 2519666d5b4cSMark Brown static void devm_spi_unregister(struct device *dev, void *res) 2520666d5b4cSMark Brown { 25218caab75fSGeert Uytterhoeven spi_unregister_controller(*(struct spi_controller **)res); 2522666d5b4cSMark Brown } 2523666d5b4cSMark Brown 2524666d5b4cSMark Brown /** 25258caab75fSGeert Uytterhoeven * devm_spi_register_controller - register managed SPI master or slave 25268caab75fSGeert Uytterhoeven * controller 25278caab75fSGeert Uytterhoeven * @dev: device managing SPI controller 25288caab75fSGeert Uytterhoeven * @ctlr: initialized controller, originally from spi_alloc_master() or 25298caab75fSGeert Uytterhoeven * spi_alloc_slave() 2530666d5b4cSMark Brown * Context: can sleep 2531666d5b4cSMark Brown * 25328caab75fSGeert Uytterhoeven * Register a SPI device as with spi_register_controller() which will 253368b892f1SJohan Hovold * automatically be unregistered and freed. 253497d56dc6SJavier Martinez Canillas * 253597d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 2536666d5b4cSMark Brown */ 25378caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev, 25388caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 2539666d5b4cSMark Brown { 25408caab75fSGeert Uytterhoeven struct spi_controller **ptr; 2541666d5b4cSMark Brown int ret; 2542666d5b4cSMark Brown 2543666d5b4cSMark Brown ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); 2544666d5b4cSMark Brown if (!ptr) 2545666d5b4cSMark Brown return -ENOMEM; 2546666d5b4cSMark Brown 25478caab75fSGeert Uytterhoeven ret = spi_register_controller(ctlr); 25484b92894eSStephen Warren if (!ret) { 25498caab75fSGeert Uytterhoeven *ptr = ctlr; 2550666d5b4cSMark Brown devres_add(dev, ptr); 2551666d5b4cSMark Brown } else { 2552666d5b4cSMark Brown devres_free(ptr); 2553666d5b4cSMark Brown } 2554666d5b4cSMark Brown 2555666d5b4cSMark Brown return ret; 2556666d5b4cSMark Brown } 25578caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller); 2558666d5b4cSMark Brown 255934860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 25608ae12a0dSDavid Brownell { 25610c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 25628ae12a0dSDavid Brownell return 0; 25638ae12a0dSDavid Brownell } 25648ae12a0dSDavid Brownell 25658ae12a0dSDavid Brownell /** 25668caab75fSGeert Uytterhoeven * spi_unregister_controller - unregister SPI master or slave controller 25678caab75fSGeert Uytterhoeven * @ctlr: the controller being unregistered 256833e34dc6SDavid Brownell * Context: can sleep 25698ae12a0dSDavid Brownell * 25708caab75fSGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 25718ae12a0dSDavid Brownell * only ones directly touching chip registers. 25728ae12a0dSDavid Brownell * 25738ae12a0dSDavid Brownell * This must be called from context that can sleep. 257468b892f1SJohan Hovold * 257568b892f1SJohan Hovold * Note that this function also drops a reference to the controller. 25768ae12a0dSDavid Brownell */ 25778caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr) 25788ae12a0dSDavid Brownell { 25799b61e302SSuniel Mahesh struct spi_controller *found; 258067f7b278SJohan Hovold int id = ctlr->bus_num; 258189fc9a1aSJeff Garzik 25829b61e302SSuniel Mahesh /* First make sure that this controller was ever added */ 25839b61e302SSuniel Mahesh mutex_lock(&board_lock); 258467f7b278SJohan Hovold found = idr_find(&spi_master_idr, id); 25859b61e302SSuniel Mahesh mutex_unlock(&board_lock); 25868caab75fSGeert Uytterhoeven if (ctlr->queued) { 25878caab75fSGeert Uytterhoeven if (spi_destroy_queue(ctlr)) 25888caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue remove failed\n"); 2589ffbbdd21SLinus Walleij } 25902b9603a0SFeng Tang mutex_lock(&board_lock); 25918caab75fSGeert Uytterhoeven list_del(&ctlr->list); 25922b9603a0SFeng Tang mutex_unlock(&board_lock); 25932b9603a0SFeng Tang 2594ebc37af5SAndy Shevchenko device_for_each_child(&ctlr->dev, NULL, __unregister); 25958caab75fSGeert Uytterhoeven device_unregister(&ctlr->dev); 25969b61e302SSuniel Mahesh /* free bus id */ 25979b61e302SSuniel Mahesh mutex_lock(&board_lock); 2598613bd1eaSJarkko Nikula if (found == ctlr) 259967f7b278SJohan Hovold idr_remove(&spi_master_idr, id); 26009b61e302SSuniel Mahesh mutex_unlock(&board_lock); 26018ae12a0dSDavid Brownell } 26028caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller); 26038ae12a0dSDavid Brownell 26048caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr) 2605ffbbdd21SLinus Walleij { 2606ffbbdd21SLinus Walleij int ret; 2607ffbbdd21SLinus Walleij 26088caab75fSGeert Uytterhoeven /* Basically no-ops for non-queued controllers */ 26098caab75fSGeert Uytterhoeven if (!ctlr->queued) 2610ffbbdd21SLinus Walleij return 0; 2611ffbbdd21SLinus Walleij 26128caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 2613ffbbdd21SLinus Walleij if (ret) 26148caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue stop failed\n"); 2615ffbbdd21SLinus Walleij 2616ffbbdd21SLinus Walleij return ret; 2617ffbbdd21SLinus Walleij } 26188caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend); 2619ffbbdd21SLinus Walleij 26208caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr) 2621ffbbdd21SLinus Walleij { 2622ffbbdd21SLinus Walleij int ret; 2623ffbbdd21SLinus Walleij 26248caab75fSGeert Uytterhoeven if (!ctlr->queued) 2625ffbbdd21SLinus Walleij return 0; 2626ffbbdd21SLinus Walleij 26278caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 2628ffbbdd21SLinus Walleij if (ret) 26298caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue restart failed\n"); 2630ffbbdd21SLinus Walleij 2631ffbbdd21SLinus Walleij return ret; 2632ffbbdd21SLinus Walleij } 26338caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume); 2634ffbbdd21SLinus Walleij 26358caab75fSGeert Uytterhoeven static int __spi_controller_match(struct device *dev, const void *data) 26365ed2c832SDave Young { 26378caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 26389f3b795aSMichał Mirosław const u16 *bus_num = data; 26395ed2c832SDave Young 26408caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 26418caab75fSGeert Uytterhoeven return ctlr->bus_num == *bus_num; 26425ed2c832SDave Young } 26435ed2c832SDave Young 26448ae12a0dSDavid Brownell /** 26458ae12a0dSDavid Brownell * spi_busnum_to_master - look up master associated with bus_num 26468ae12a0dSDavid Brownell * @bus_num: the master's bus number 264733e34dc6SDavid Brownell * Context: can sleep 26488ae12a0dSDavid Brownell * 26498ae12a0dSDavid Brownell * This call may be used with devices that are registered after 26508ae12a0dSDavid Brownell * arch init time. It returns a refcounted pointer to the relevant 26518caab75fSGeert Uytterhoeven * spi_controller (which the caller must release), or NULL if there is 26528ae12a0dSDavid Brownell * no such master registered. 265397d56dc6SJavier Martinez Canillas * 265497d56dc6SJavier Martinez Canillas * Return: the SPI master structure on success, else NULL. 26558ae12a0dSDavid Brownell */ 26568caab75fSGeert Uytterhoeven struct spi_controller *spi_busnum_to_master(u16 bus_num) 26578ae12a0dSDavid Brownell { 265849dce689STony Jones struct device *dev; 26598caab75fSGeert Uytterhoeven struct spi_controller *ctlr = NULL; 26608ae12a0dSDavid Brownell 2661695794aeSGreg Kroah-Hartman dev = class_find_device(&spi_master_class, NULL, &bus_num, 26628caab75fSGeert Uytterhoeven __spi_controller_match); 26635ed2c832SDave Young if (dev) 26648caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 26655ed2c832SDave Young /* reference got in class_find_device */ 26668caab75fSGeert Uytterhoeven return ctlr; 26678ae12a0dSDavid Brownell } 26688ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master); 26698ae12a0dSDavid Brownell 2670d780c371SMartin Sperl /*-------------------------------------------------------------------------*/ 2671d780c371SMartin Sperl 2672d780c371SMartin Sperl /* Core methods for SPI resource management */ 2673d780c371SMartin Sperl 2674d780c371SMartin Sperl /** 2675d780c371SMartin Sperl * spi_res_alloc - allocate a spi resource that is life-cycle managed 2676d780c371SMartin Sperl * during the processing of a spi_message while using 2677d780c371SMartin Sperl * spi_transfer_one 2678d780c371SMartin Sperl * @spi: the spi device for which we allocate memory 2679d780c371SMartin Sperl * @release: the release code to execute for this resource 2680d780c371SMartin Sperl * @size: size to alloc and return 2681d780c371SMartin Sperl * @gfp: GFP allocation flags 2682d780c371SMartin Sperl * 2683d780c371SMartin Sperl * Return: the pointer to the allocated data 2684d780c371SMartin Sperl * 2685d780c371SMartin Sperl * This may get enhanced in the future to allocate from a memory pool 26868caab75fSGeert Uytterhoeven * of the @spi_device or @spi_controller to avoid repeated allocations. 2687d780c371SMartin Sperl */ 2688d780c371SMartin Sperl void *spi_res_alloc(struct spi_device *spi, 2689d780c371SMartin Sperl spi_res_release_t release, 2690d780c371SMartin Sperl size_t size, gfp_t gfp) 2691d780c371SMartin Sperl { 2692d780c371SMartin Sperl struct spi_res *sres; 2693d780c371SMartin Sperl 2694d780c371SMartin Sperl sres = kzalloc(sizeof(*sres) + size, gfp); 2695d780c371SMartin Sperl if (!sres) 2696d780c371SMartin Sperl return NULL; 2697d780c371SMartin Sperl 2698d780c371SMartin Sperl INIT_LIST_HEAD(&sres->entry); 2699d780c371SMartin Sperl sres->release = release; 2700d780c371SMartin Sperl 2701d780c371SMartin Sperl return sres->data; 2702d780c371SMartin Sperl } 2703d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_alloc); 2704d780c371SMartin Sperl 2705d780c371SMartin Sperl /** 2706d780c371SMartin Sperl * spi_res_free - free an spi resource 2707d780c371SMartin Sperl * @res: pointer to the custom data of a resource 2708d780c371SMartin Sperl * 2709d780c371SMartin Sperl */ 2710d780c371SMartin Sperl void spi_res_free(void *res) 2711d780c371SMartin Sperl { 2712d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 2713d780c371SMartin Sperl 2714d780c371SMartin Sperl if (!res) 2715d780c371SMartin Sperl return; 2716d780c371SMartin Sperl 2717d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 2718d780c371SMartin Sperl kfree(sres); 2719d780c371SMartin Sperl } 2720d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_free); 2721d780c371SMartin Sperl 2722d780c371SMartin Sperl /** 2723d780c371SMartin Sperl * spi_res_add - add a spi_res to the spi_message 2724d780c371SMartin Sperl * @message: the spi message 2725d780c371SMartin Sperl * @res: the spi_resource 2726d780c371SMartin Sperl */ 2727d780c371SMartin Sperl void spi_res_add(struct spi_message *message, void *res) 2728d780c371SMartin Sperl { 2729d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 2730d780c371SMartin Sperl 2731d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 2732d780c371SMartin Sperl list_add_tail(&sres->entry, &message->resources); 2733d780c371SMartin Sperl } 2734d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_add); 2735d780c371SMartin Sperl 2736d780c371SMartin Sperl /** 2737d780c371SMartin Sperl * spi_res_release - release all spi resources for this message 27388caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller 2739d780c371SMartin Sperl * @message: the @spi_message 2740d780c371SMartin Sperl */ 27418caab75fSGeert Uytterhoeven void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 2742d780c371SMartin Sperl { 2743f5694369SVladimir Zapolskiy struct spi_res *res, *tmp; 2744d780c371SMartin Sperl 2745f5694369SVladimir Zapolskiy list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { 2746d780c371SMartin Sperl if (res->release) 27478caab75fSGeert Uytterhoeven res->release(ctlr, message, res->data); 2748d780c371SMartin Sperl 2749d780c371SMartin Sperl list_del(&res->entry); 2750d780c371SMartin Sperl 2751d780c371SMartin Sperl kfree(res); 2752d780c371SMartin Sperl } 2753d780c371SMartin Sperl } 2754d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_release); 27558ae12a0dSDavid Brownell 27568ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 27578ae12a0dSDavid Brownell 2758523baf5aSMartin Sperl /* Core methods for spi_message alterations */ 2759523baf5aSMartin Sperl 27608caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr, 2761523baf5aSMartin Sperl struct spi_message *msg, 2762523baf5aSMartin Sperl void *res) 2763523baf5aSMartin Sperl { 2764523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer = res; 2765523baf5aSMartin Sperl size_t i; 2766523baf5aSMartin Sperl 2767523baf5aSMartin Sperl /* call extra callback if requested */ 2768523baf5aSMartin Sperl if (rxfer->release) 27698caab75fSGeert Uytterhoeven rxfer->release(ctlr, msg, res); 2770523baf5aSMartin Sperl 2771523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 2772523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 2773523baf5aSMartin Sperl 2774523baf5aSMartin Sperl /* remove the formerly inserted entries */ 2775523baf5aSMartin Sperl for (i = 0; i < rxfer->inserted; i++) 2776523baf5aSMartin Sperl list_del(&rxfer->inserted_transfers[i].transfer_list); 2777523baf5aSMartin Sperl } 2778523baf5aSMartin Sperl 2779523baf5aSMartin Sperl /** 2780523baf5aSMartin Sperl * spi_replace_transfers - replace transfers with several transfers 2781523baf5aSMartin Sperl * and register change with spi_message.resources 2782523baf5aSMartin Sperl * @msg: the spi_message we work upon 2783523baf5aSMartin Sperl * @xfer_first: the first spi_transfer we want to replace 2784523baf5aSMartin Sperl * @remove: number of transfers to remove 2785523baf5aSMartin Sperl * @insert: the number of transfers we want to insert instead 2786523baf5aSMartin Sperl * @release: extra release code necessary in some circumstances 2787523baf5aSMartin Sperl * @extradatasize: extra data to allocate (with alignment guarantees 2788523baf5aSMartin Sperl * of struct @spi_transfer) 278905885397SMartin Sperl * @gfp: gfp flags 2790523baf5aSMartin Sperl * 2791523baf5aSMartin Sperl * Returns: pointer to @spi_replaced_transfers, 2792523baf5aSMartin Sperl * PTR_ERR(...) in case of errors. 2793523baf5aSMartin Sperl */ 2794523baf5aSMartin Sperl struct spi_replaced_transfers *spi_replace_transfers( 2795523baf5aSMartin Sperl struct spi_message *msg, 2796523baf5aSMartin Sperl struct spi_transfer *xfer_first, 2797523baf5aSMartin Sperl size_t remove, 2798523baf5aSMartin Sperl size_t insert, 2799523baf5aSMartin Sperl spi_replaced_release_t release, 2800523baf5aSMartin Sperl size_t extradatasize, 2801523baf5aSMartin Sperl gfp_t gfp) 2802523baf5aSMartin Sperl { 2803523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer; 2804523baf5aSMartin Sperl struct spi_transfer *xfer; 2805523baf5aSMartin Sperl size_t i; 2806523baf5aSMartin Sperl 2807523baf5aSMartin Sperl /* allocate the structure using spi_res */ 2808523baf5aSMartin Sperl rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 2809aef97522SGustavo A. R. Silva struct_size(rxfer, inserted_transfers, insert) 2810523baf5aSMartin Sperl + extradatasize, 2811523baf5aSMartin Sperl gfp); 2812523baf5aSMartin Sperl if (!rxfer) 2813523baf5aSMartin Sperl return ERR_PTR(-ENOMEM); 2814523baf5aSMartin Sperl 2815523baf5aSMartin Sperl /* the release code to invoke before running the generic release */ 2816523baf5aSMartin Sperl rxfer->release = release; 2817523baf5aSMartin Sperl 2818523baf5aSMartin Sperl /* assign extradata */ 2819523baf5aSMartin Sperl if (extradatasize) 2820523baf5aSMartin Sperl rxfer->extradata = 2821523baf5aSMartin Sperl &rxfer->inserted_transfers[insert]; 2822523baf5aSMartin Sperl 2823523baf5aSMartin Sperl /* init the replaced_transfers list */ 2824523baf5aSMartin Sperl INIT_LIST_HEAD(&rxfer->replaced_transfers); 2825523baf5aSMartin Sperl 2826523baf5aSMartin Sperl /* assign the list_entry after which we should reinsert 2827523baf5aSMartin Sperl * the @replaced_transfers - it may be spi_message.messages! 2828523baf5aSMartin Sperl */ 2829523baf5aSMartin Sperl rxfer->replaced_after = xfer_first->transfer_list.prev; 2830523baf5aSMartin Sperl 2831523baf5aSMartin Sperl /* remove the requested number of transfers */ 2832523baf5aSMartin Sperl for (i = 0; i < remove; i++) { 2833523baf5aSMartin Sperl /* if the entry after replaced_after it is msg->transfers 2834523baf5aSMartin Sperl * then we have been requested to remove more transfers 2835523baf5aSMartin Sperl * than are in the list 2836523baf5aSMartin Sperl */ 2837523baf5aSMartin Sperl if (rxfer->replaced_after->next == &msg->transfers) { 2838523baf5aSMartin Sperl dev_err(&msg->spi->dev, 2839523baf5aSMartin Sperl "requested to remove more spi_transfers than are available\n"); 2840523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 2841523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, 2842523baf5aSMartin Sperl rxfer->replaced_after); 2843523baf5aSMartin Sperl 2844523baf5aSMartin Sperl /* free the spi_replace_transfer structure */ 2845523baf5aSMartin Sperl spi_res_free(rxfer); 2846523baf5aSMartin Sperl 2847523baf5aSMartin Sperl /* and return with an error */ 2848523baf5aSMartin Sperl return ERR_PTR(-EINVAL); 2849523baf5aSMartin Sperl } 2850523baf5aSMartin Sperl 2851523baf5aSMartin Sperl /* remove the entry after replaced_after from list of 2852523baf5aSMartin Sperl * transfers and add it to list of replaced_transfers 2853523baf5aSMartin Sperl */ 2854523baf5aSMartin Sperl list_move_tail(rxfer->replaced_after->next, 2855523baf5aSMartin Sperl &rxfer->replaced_transfers); 2856523baf5aSMartin Sperl } 2857523baf5aSMartin Sperl 2858523baf5aSMartin Sperl /* create copy of the given xfer with identical settings 2859523baf5aSMartin Sperl * based on the first transfer to get removed 2860523baf5aSMartin Sperl */ 2861523baf5aSMartin Sperl for (i = 0; i < insert; i++) { 2862523baf5aSMartin Sperl /* we need to run in reverse order */ 2863523baf5aSMartin Sperl xfer = &rxfer->inserted_transfers[insert - 1 - i]; 2864523baf5aSMartin Sperl 2865523baf5aSMartin Sperl /* copy all spi_transfer data */ 2866523baf5aSMartin Sperl memcpy(xfer, xfer_first, sizeof(*xfer)); 2867523baf5aSMartin Sperl 2868523baf5aSMartin Sperl /* add to list */ 2869523baf5aSMartin Sperl list_add(&xfer->transfer_list, rxfer->replaced_after); 2870523baf5aSMartin Sperl 2871523baf5aSMartin Sperl /* clear cs_change and delay_usecs for all but the last */ 2872523baf5aSMartin Sperl if (i) { 2873523baf5aSMartin Sperl xfer->cs_change = false; 2874523baf5aSMartin Sperl xfer->delay_usecs = 0; 2875523baf5aSMartin Sperl } 2876523baf5aSMartin Sperl } 2877523baf5aSMartin Sperl 2878523baf5aSMartin Sperl /* set up inserted */ 2879523baf5aSMartin Sperl rxfer->inserted = insert; 2880523baf5aSMartin Sperl 2881523baf5aSMartin Sperl /* and register it with spi_res/spi_message */ 2882523baf5aSMartin Sperl spi_res_add(msg, rxfer); 2883523baf5aSMartin Sperl 2884523baf5aSMartin Sperl return rxfer; 2885523baf5aSMartin Sperl } 2886523baf5aSMartin Sperl EXPORT_SYMBOL_GPL(spi_replace_transfers); 2887523baf5aSMartin Sperl 28888caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 2889d9f12122SMartin Sperl struct spi_message *msg, 2890d9f12122SMartin Sperl struct spi_transfer **xferp, 2891d9f12122SMartin Sperl size_t maxsize, 2892d9f12122SMartin Sperl gfp_t gfp) 2893d9f12122SMartin Sperl { 2894d9f12122SMartin Sperl struct spi_transfer *xfer = *xferp, *xfers; 2895d9f12122SMartin Sperl struct spi_replaced_transfers *srt; 2896d9f12122SMartin Sperl size_t offset; 2897d9f12122SMartin Sperl size_t count, i; 2898d9f12122SMartin Sperl 2899d9f12122SMartin Sperl /* calculate how many we have to replace */ 2900d9f12122SMartin Sperl count = DIV_ROUND_UP(xfer->len, maxsize); 2901d9f12122SMartin Sperl 2902d9f12122SMartin Sperl /* create replacement */ 2903d9f12122SMartin Sperl srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 2904657d32efSDan Carpenter if (IS_ERR(srt)) 2905657d32efSDan Carpenter return PTR_ERR(srt); 2906d9f12122SMartin Sperl xfers = srt->inserted_transfers; 2907d9f12122SMartin Sperl 2908d9f12122SMartin Sperl /* now handle each of those newly inserted spi_transfers 2909d9f12122SMartin Sperl * note that the replacements spi_transfers all are preset 2910d9f12122SMartin Sperl * to the same values as *xferp, so tx_buf, rx_buf and len 2911d9f12122SMartin Sperl * are all identical (as well as most others) 2912d9f12122SMartin Sperl * so we just have to fix up len and the pointers. 2913d9f12122SMartin Sperl * 2914d9f12122SMartin Sperl * this also includes support for the depreciated 2915d9f12122SMartin Sperl * spi_message.is_dma_mapped interface 2916d9f12122SMartin Sperl */ 2917d9f12122SMartin Sperl 2918d9f12122SMartin Sperl /* the first transfer just needs the length modified, so we 2919d9f12122SMartin Sperl * run it outside the loop 2920d9f12122SMartin Sperl */ 2921c8dab77aSFabio Estevam xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 2922d9f12122SMartin Sperl 2923d9f12122SMartin Sperl /* all the others need rx_buf/tx_buf also set */ 2924d9f12122SMartin Sperl for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 2925d9f12122SMartin Sperl /* update rx_buf, tx_buf and dma */ 2926d9f12122SMartin Sperl if (xfers[i].rx_buf) 2927d9f12122SMartin Sperl xfers[i].rx_buf += offset; 2928d9f12122SMartin Sperl if (xfers[i].rx_dma) 2929d9f12122SMartin Sperl xfers[i].rx_dma += offset; 2930d9f12122SMartin Sperl if (xfers[i].tx_buf) 2931d9f12122SMartin Sperl xfers[i].tx_buf += offset; 2932d9f12122SMartin Sperl if (xfers[i].tx_dma) 2933d9f12122SMartin Sperl xfers[i].tx_dma += offset; 2934d9f12122SMartin Sperl 2935d9f12122SMartin Sperl /* update length */ 2936d9f12122SMartin Sperl xfers[i].len = min(maxsize, xfers[i].len - offset); 2937d9f12122SMartin Sperl } 2938d9f12122SMartin Sperl 2939d9f12122SMartin Sperl /* we set up xferp to the last entry we have inserted, 2940d9f12122SMartin Sperl * so that we skip those already split transfers 2941d9f12122SMartin Sperl */ 2942d9f12122SMartin Sperl *xferp = &xfers[count - 1]; 2943d9f12122SMartin Sperl 2944d9f12122SMartin Sperl /* increment statistics counters */ 29458caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 2946d9f12122SMartin Sperl transfers_split_maxsize); 2947d9f12122SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, 2948d9f12122SMartin Sperl transfers_split_maxsize); 2949d9f12122SMartin Sperl 2950d9f12122SMartin Sperl return 0; 2951d9f12122SMartin Sperl } 2952d9f12122SMartin Sperl 2953d9f12122SMartin Sperl /** 2954d9f12122SMartin Sperl * spi_split_tranfers_maxsize - split spi transfers into multiple transfers 2955d9f12122SMartin Sperl * when an individual transfer exceeds a 2956d9f12122SMartin Sperl * certain size 29578caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller for this transfer 29583700ce95SMasanari Iida * @msg: the @spi_message to transform 29593700ce95SMasanari Iida * @maxsize: the maximum when to apply this 296010f11a22SJavier Martinez Canillas * @gfp: GFP allocation flags 2961d9f12122SMartin Sperl * 2962d9f12122SMartin Sperl * Return: status of transformation 2963d9f12122SMartin Sperl */ 29648caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr, 2965d9f12122SMartin Sperl struct spi_message *msg, 2966d9f12122SMartin Sperl size_t maxsize, 2967d9f12122SMartin Sperl gfp_t gfp) 2968d9f12122SMartin Sperl { 2969d9f12122SMartin Sperl struct spi_transfer *xfer; 2970d9f12122SMartin Sperl int ret; 2971d9f12122SMartin Sperl 2972d9f12122SMartin Sperl /* iterate over the transfer_list, 2973d9f12122SMartin Sperl * but note that xfer is advanced to the last transfer inserted 2974d9f12122SMartin Sperl * to avoid checking sizes again unnecessarily (also xfer does 2975d9f12122SMartin Sperl * potentiall belong to a different list by the time the 2976d9f12122SMartin Sperl * replacement has happened 2977d9f12122SMartin Sperl */ 2978d9f12122SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 2979d9f12122SMartin Sperl if (xfer->len > maxsize) { 29808caab75fSGeert Uytterhoeven ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 29818caab75fSGeert Uytterhoeven maxsize, gfp); 2982d9f12122SMartin Sperl if (ret) 2983d9f12122SMartin Sperl return ret; 2984d9f12122SMartin Sperl } 2985d9f12122SMartin Sperl } 2986d9f12122SMartin Sperl 2987d9f12122SMartin Sperl return 0; 2988d9f12122SMartin Sperl } 2989d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 29908ae12a0dSDavid Brownell 29918ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 29928ae12a0dSDavid Brownell 29938caab75fSGeert Uytterhoeven /* Core methods for SPI controller protocol drivers. Some of the 29947d077197SDavid Brownell * other core methods are currently defined as inline functions. 29957d077197SDavid Brownell */ 29967d077197SDavid Brownell 29978caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 29988caab75fSGeert Uytterhoeven u8 bits_per_word) 299963ab645fSStefan Brüns { 30008caab75fSGeert Uytterhoeven if (ctlr->bits_per_word_mask) { 300163ab645fSStefan Brüns /* Only 32 bits fit in the mask */ 300263ab645fSStefan Brüns if (bits_per_word > 32) 300363ab645fSStefan Brüns return -EINVAL; 30048caab75fSGeert Uytterhoeven if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 300563ab645fSStefan Brüns return -EINVAL; 300663ab645fSStefan Brüns } 300763ab645fSStefan Brüns 300863ab645fSStefan Brüns return 0; 300963ab645fSStefan Brüns } 301063ab645fSStefan Brüns 30117d077197SDavid Brownell /** 30127d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 30137d077197SDavid Brownell * @spi: the device whose settings are being modified 30147d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 30157d077197SDavid Brownell * 30167d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 30177d077197SDavid Brownell * device doesn't work with its default. They may likewise need 30187d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 30197d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 30207d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 30217d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 30227d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 30237d077197SDavid Brownell * 30247d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 30257d077197SDavid Brownell * that the underlying controller or its driver does not support. For 30267d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 30277d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 302897d56dc6SJavier Martinez Canillas * 302997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 30307d077197SDavid Brownell */ 30317d077197SDavid Brownell int spi_setup(struct spi_device *spi) 30327d077197SDavid Brownell { 303383596fbeSGeert Uytterhoeven unsigned bad_bits, ugly_bits; 30345ab8d262SAndy Shevchenko int status; 30357d077197SDavid Brownell 3036f477b7fbSwangyuhang /* check mode to prevent that DUAL and QUAD set at the same time 3037f477b7fbSwangyuhang */ 3038f477b7fbSwangyuhang if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || 3039f477b7fbSwangyuhang ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { 3040f477b7fbSwangyuhang dev_err(&spi->dev, 3041f477b7fbSwangyuhang "setup: can not select dual and quad at the same time\n"); 3042f477b7fbSwangyuhang return -EINVAL; 3043f477b7fbSwangyuhang } 3044f477b7fbSwangyuhang /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden 3045f477b7fbSwangyuhang */ 3046f477b7fbSwangyuhang if ((spi->mode & SPI_3WIRE) && (spi->mode & 30476b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 30486b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 3049f477b7fbSwangyuhang return -EINVAL; 3050e7db06b5SDavid Brownell /* help drivers fail *cleanly* when they need options 30518caab75fSGeert Uytterhoeven * that aren't supported with their current controller 3052cbaa62e0SDavid Lechner * SPI_CS_WORD has a fallback software implementation, 3053cbaa62e0SDavid Lechner * so it is ignored here. 3054e7db06b5SDavid Brownell */ 3055cbaa62e0SDavid Lechner bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); 3056d61ad23cSSerge Semin /* nothing prevents from working with active-high CS in case if it 3057d61ad23cSSerge Semin * is driven by GPIO. 3058d61ad23cSSerge Semin */ 3059d61ad23cSSerge Semin if (gpio_is_valid(spi->cs_gpio)) 3060d61ad23cSSerge Semin bad_bits &= ~SPI_CS_HIGH; 306183596fbeSGeert Uytterhoeven ugly_bits = bad_bits & 30626b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 30636b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); 306483596fbeSGeert Uytterhoeven if (ugly_bits) { 306583596fbeSGeert Uytterhoeven dev_warn(&spi->dev, 306683596fbeSGeert Uytterhoeven "setup: ignoring unsupported mode bits %x\n", 306783596fbeSGeert Uytterhoeven ugly_bits); 306883596fbeSGeert Uytterhoeven spi->mode &= ~ugly_bits; 306983596fbeSGeert Uytterhoeven bad_bits &= ~ugly_bits; 307083596fbeSGeert Uytterhoeven } 3071e7db06b5SDavid Brownell if (bad_bits) { 3072eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 3073e7db06b5SDavid Brownell bad_bits); 3074e7db06b5SDavid Brownell return -EINVAL; 3075e7db06b5SDavid Brownell } 3076e7db06b5SDavid Brownell 30777d077197SDavid Brownell if (!spi->bits_per_word) 30787d077197SDavid Brownell spi->bits_per_word = 8; 30797d077197SDavid Brownell 30808caab75fSGeert Uytterhoeven status = __spi_validate_bits_per_word(spi->controller, 30818caab75fSGeert Uytterhoeven spi->bits_per_word); 30825ab8d262SAndy Shevchenko if (status) 30835ab8d262SAndy Shevchenko return status; 308463ab645fSStefan Brüns 3085052eb2d4SAxel Lin if (!spi->max_speed_hz) 30868caab75fSGeert Uytterhoeven spi->max_speed_hz = spi->controller->max_speed_hz; 3087052eb2d4SAxel Lin 30888caab75fSGeert Uytterhoeven if (spi->controller->setup) 30898caab75fSGeert Uytterhoeven status = spi->controller->setup(spi); 30907d077197SDavid Brownell 3091abeedb01SFranklin S Cooper Jr spi_set_cs(spi, false); 3092abeedb01SFranklin S Cooper Jr 3093924b5867SDouglas Anderson if (spi->rt && !spi->controller->rt) { 3094924b5867SDouglas Anderson spi->controller->rt = true; 3095924b5867SDouglas Anderson spi_set_thread_rt(spi->controller); 3096924b5867SDouglas Anderson } 3097924b5867SDouglas Anderson 30985fe5f05eSJingoo Han dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 30997d077197SDavid Brownell (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), 31007d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 31017d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 31027d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 31037d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 31047d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 31057d077197SDavid Brownell status); 31067d077197SDavid Brownell 31077d077197SDavid Brownell return status; 31087d077197SDavid Brownell } 31097d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 31107d077197SDavid Brownell 3111f1ca9992SSowjanya Komatineni /** 3112f1ca9992SSowjanya Komatineni * spi_set_cs_timing - configure CS setup, hold, and inactive delays 3113f1ca9992SSowjanya Komatineni * @spi: the device that requires specific CS timing configuration 3114f1ca9992SSowjanya Komatineni * @setup: CS setup time in terms of clock count 3115f1ca9992SSowjanya Komatineni * @hold: CS hold time in terms of clock count 3116f1ca9992SSowjanya Komatineni * @inactive_dly: CS inactive delay between transfers in terms of clock count 3117f1ca9992SSowjanya Komatineni */ 3118f1ca9992SSowjanya Komatineni void spi_set_cs_timing(struct spi_device *spi, u8 setup, u8 hold, 3119f1ca9992SSowjanya Komatineni u8 inactive_dly) 3120f1ca9992SSowjanya Komatineni { 3121f1ca9992SSowjanya Komatineni if (spi->controller->set_cs_timing) 3122f1ca9992SSowjanya Komatineni spi->controller->set_cs_timing(spi, setup, hold, inactive_dly); 3123f1ca9992SSowjanya Komatineni } 3124f1ca9992SSowjanya Komatineni EXPORT_SYMBOL_GPL(spi_set_cs_timing); 3125f1ca9992SSowjanya Komatineni 312690808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message) 3127cf32b71eSErnst Schwab { 31288caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3129e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 31306ea31293SAtsushi Nemoto int w_size; 3131cf32b71eSErnst Schwab 313224a0013aSMark Brown if (list_empty(&message->transfers)) 313324a0013aSMark Brown return -EINVAL; 313424a0013aSMark Brown 3135cbaa62e0SDavid Lechner /* If an SPI controller does not support toggling the CS line on each 313671388b21SDavid Lechner * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO 313771388b21SDavid Lechner * for the CS line, we can emulate the CS-per-word hardware function by 3138cbaa62e0SDavid Lechner * splitting transfers into one-word transfers and ensuring that 3139cbaa62e0SDavid Lechner * cs_change is set for each transfer. 3140cbaa62e0SDavid Lechner */ 314171388b21SDavid Lechner if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || 3142f3186dd8SLinus Walleij spi->cs_gpiod || 314371388b21SDavid Lechner gpio_is_valid(spi->cs_gpio))) { 3144cbaa62e0SDavid Lechner size_t maxsize; 3145cbaa62e0SDavid Lechner int ret; 3146cbaa62e0SDavid Lechner 3147cbaa62e0SDavid Lechner maxsize = (spi->bits_per_word + 7) / 8; 3148cbaa62e0SDavid Lechner 3149cbaa62e0SDavid Lechner /* spi_split_transfers_maxsize() requires message->spi */ 3150cbaa62e0SDavid Lechner message->spi = spi; 3151cbaa62e0SDavid Lechner 3152cbaa62e0SDavid Lechner ret = spi_split_transfers_maxsize(ctlr, message, maxsize, 3153cbaa62e0SDavid Lechner GFP_KERNEL); 3154cbaa62e0SDavid Lechner if (ret) 3155cbaa62e0SDavid Lechner return ret; 3156cbaa62e0SDavid Lechner 3157cbaa62e0SDavid Lechner list_for_each_entry(xfer, &message->transfers, transfer_list) { 3158cbaa62e0SDavid Lechner /* don't change cs_change on the last entry in the list */ 3159cbaa62e0SDavid Lechner if (list_is_last(&xfer->transfer_list, &message->transfers)) 3160cbaa62e0SDavid Lechner break; 3161cbaa62e0SDavid Lechner xfer->cs_change = 1; 3162cbaa62e0SDavid Lechner } 3163cbaa62e0SDavid Lechner } 3164cbaa62e0SDavid Lechner 3165cf32b71eSErnst Schwab /* Half-duplex links include original MicroWire, and ones with 3166cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 3167cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 3168cf32b71eSErnst Schwab * software limitations. 3169cf32b71eSErnst Schwab */ 31708caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 31718caab75fSGeert Uytterhoeven (spi->mode & SPI_3WIRE)) { 31728caab75fSGeert Uytterhoeven unsigned flags = ctlr->flags; 3173cf32b71eSErnst Schwab 3174cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 3175cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 3176cf32b71eSErnst Schwab return -EINVAL; 31778caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 3178cf32b71eSErnst Schwab return -EINVAL; 31798caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 3180cf32b71eSErnst Schwab return -EINVAL; 3181cf32b71eSErnst Schwab } 3182cf32b71eSErnst Schwab } 3183cf32b71eSErnst Schwab 3184e6811d1dSLaxman Dewangan /** 3185059b8ffeSLaxman Dewangan * Set transfer bits_per_word and max speed as spi device default if 3186059b8ffeSLaxman Dewangan * it is not set for this transfer. 3187f477b7fbSwangyuhang * Set transfer tx_nbits and rx_nbits as single transfer default 3188f477b7fbSwangyuhang * (SPI_NBITS_SINGLE) if it is not set for this transfer. 3189b7bb367aSJonas Bonn * Ensure transfer word_delay is at least as long as that required by 3190b7bb367aSJonas Bonn * device itself. 3191e6811d1dSLaxman Dewangan */ 319277e80588SMartin Sperl message->frame_length = 0; 3193e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 31945d7e2b5eSMartin Sperl xfer->effective_speed_hz = 0; 3195078726ceSSourav Poddar message->frame_length += xfer->len; 3196e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 3197e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 3198a6f87fadSAxel Lin 3199a6f87fadSAxel Lin if (!xfer->speed_hz) 3200059b8ffeSLaxman Dewangan xfer->speed_hz = spi->max_speed_hz; 3201a6f87fadSAxel Lin 32028caab75fSGeert Uytterhoeven if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 32038caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 320456ede94aSGabor Juhos 32058caab75fSGeert Uytterhoeven if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 3206543bb255SStephen Warren return -EINVAL; 3207a2fd4f9fSMark Brown 32084d94bd21SIvan T. Ivanov /* 32094d94bd21SIvan T. Ivanov * SPI transfer length should be multiple of SPI word size 32104d94bd21SIvan T. Ivanov * where SPI word size should be power-of-two multiple 32114d94bd21SIvan T. Ivanov */ 32124d94bd21SIvan T. Ivanov if (xfer->bits_per_word <= 8) 32134d94bd21SIvan T. Ivanov w_size = 1; 32144d94bd21SIvan T. Ivanov else if (xfer->bits_per_word <= 16) 32154d94bd21SIvan T. Ivanov w_size = 2; 32164d94bd21SIvan T. Ivanov else 32174d94bd21SIvan T. Ivanov w_size = 4; 32184d94bd21SIvan T. Ivanov 32194d94bd21SIvan T. Ivanov /* No partial transfers accepted */ 32206ea31293SAtsushi Nemoto if (xfer->len % w_size) 32214d94bd21SIvan T. Ivanov return -EINVAL; 32224d94bd21SIvan T. Ivanov 32238caab75fSGeert Uytterhoeven if (xfer->speed_hz && ctlr->min_speed_hz && 32248caab75fSGeert Uytterhoeven xfer->speed_hz < ctlr->min_speed_hz) 3225a2fd4f9fSMark Brown return -EINVAL; 3226f477b7fbSwangyuhang 3227f477b7fbSwangyuhang if (xfer->tx_buf && !xfer->tx_nbits) 3228f477b7fbSwangyuhang xfer->tx_nbits = SPI_NBITS_SINGLE; 3229f477b7fbSwangyuhang if (xfer->rx_buf && !xfer->rx_nbits) 3230f477b7fbSwangyuhang xfer->rx_nbits = SPI_NBITS_SINGLE; 3231f477b7fbSwangyuhang /* check transfer tx/rx_nbits: 32321afd9989SGeert Uytterhoeven * 1. check the value matches one of single, dual and quad 32331afd9989SGeert Uytterhoeven * 2. check tx/rx_nbits match the mode in spi_device 3234f477b7fbSwangyuhang */ 3235db90a441SSourav Poddar if (xfer->tx_buf) { 3236f477b7fbSwangyuhang if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3237f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_DUAL && 3238f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_QUAD) 3239a2fd4f9fSMark Brown return -EINVAL; 3240f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 3241f477b7fbSwangyuhang !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3242f477b7fbSwangyuhang return -EINVAL; 3243f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 3244f477b7fbSwangyuhang !(spi->mode & SPI_TX_QUAD)) 3245f477b7fbSwangyuhang return -EINVAL; 3246db90a441SSourav Poddar } 3247f477b7fbSwangyuhang /* check transfer rx_nbits */ 3248db90a441SSourav Poddar if (xfer->rx_buf) { 3249f477b7fbSwangyuhang if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3250f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_DUAL && 3251f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_QUAD) 3252f477b7fbSwangyuhang return -EINVAL; 3253f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 3254f477b7fbSwangyuhang !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 3255f477b7fbSwangyuhang return -EINVAL; 3256f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 3257f477b7fbSwangyuhang !(spi->mode & SPI_RX_QUAD)) 3258f477b7fbSwangyuhang return -EINVAL; 3259e6811d1dSLaxman Dewangan } 3260b7bb367aSJonas Bonn 3261b7bb367aSJonas Bonn if (xfer->word_delay_usecs < spi->word_delay_usecs) 3262b7bb367aSJonas Bonn xfer->word_delay_usecs = spi->word_delay_usecs; 3263e6811d1dSLaxman Dewangan } 3264e6811d1dSLaxman Dewangan 3265cf32b71eSErnst Schwab message->status = -EINPROGRESS; 326690808738SMark Brown 326790808738SMark Brown return 0; 326890808738SMark Brown } 326990808738SMark Brown 327090808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message) 327190808738SMark Brown { 32728caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 327390808738SMark Brown 3274b5932f5cSBoris Brezillon /* 3275b5932f5cSBoris Brezillon * Some controllers do not support doing regular SPI transfers. Return 3276b5932f5cSBoris Brezillon * ENOTSUPP when this is the case. 3277b5932f5cSBoris Brezillon */ 3278b5932f5cSBoris Brezillon if (!ctlr->transfer) 3279b5932f5cSBoris Brezillon return -ENOTSUPP; 3280b5932f5cSBoris Brezillon 328190808738SMark Brown message->spi = spi; 328290808738SMark Brown 32838caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); 3284eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); 3285eca2ebc7SMartin Sperl 328690808738SMark Brown trace_spi_message_submit(message); 328790808738SMark Brown 32888caab75fSGeert Uytterhoeven return ctlr->transfer(spi, message); 3289cf32b71eSErnst Schwab } 3290cf32b71eSErnst Schwab 3291568d0697SDavid Brownell /** 3292568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 3293568d0697SDavid Brownell * @spi: device with which data will be exchanged 3294568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 3295568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 3296568d0697SDavid Brownell * 3297568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 3298568d0697SDavid Brownell * as well as from task contexts which can sleep. 3299568d0697SDavid Brownell * 3300568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 3301568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 3302568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 3303568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 3304568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 3305568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 3306568d0697SDavid Brownell * core or controller driver code. 3307568d0697SDavid Brownell * 3308568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 3309568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 3310568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 3311568d0697SDavid Brownell * time requirements, for example. 3312568d0697SDavid Brownell * 3313568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 3314568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 3315568d0697SDavid Brownell * Until returning from the associated message completion callback, 3316568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 3317568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 3318568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 331997d56dc6SJavier Martinez Canillas * 332097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3321568d0697SDavid Brownell */ 3322568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 3323568d0697SDavid Brownell { 33248caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3325cf32b71eSErnst Schwab int ret; 3326cf32b71eSErnst Schwab unsigned long flags; 3327568d0697SDavid Brownell 332890808738SMark Brown ret = __spi_validate(spi, message); 332990808738SMark Brown if (ret != 0) 333090808738SMark Brown return ret; 333190808738SMark Brown 33328caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3333568d0697SDavid Brownell 33348caab75fSGeert Uytterhoeven if (ctlr->bus_lock_flag) 3335cf32b71eSErnst Schwab ret = -EBUSY; 3336cf32b71eSErnst Schwab else 3337cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3338568d0697SDavid Brownell 33398caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3340cf32b71eSErnst Schwab 3341cf32b71eSErnst Schwab return ret; 3342568d0697SDavid Brownell } 3343568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 3344568d0697SDavid Brownell 3345cf32b71eSErnst Schwab /** 3346cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 3347cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3348cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 3349cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 3350cf32b71eSErnst Schwab * 3351cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 3352cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 3353cf32b71eSErnst Schwab * 3354cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 3355cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 3356cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 3357cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 3358cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 3359cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 3360cf32b71eSErnst Schwab * core or controller driver code. 3361cf32b71eSErnst Schwab * 3362cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 3363cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 3364cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 3365cf32b71eSErnst Schwab * time requirements, for example. 3366cf32b71eSErnst Schwab * 3367cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 3368cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 3369cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 3370cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 3371cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 3372cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 337397d56dc6SJavier Martinez Canillas * 337497d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3375cf32b71eSErnst Schwab */ 3376cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message) 3377cf32b71eSErnst Schwab { 33788caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3379cf32b71eSErnst Schwab int ret; 3380cf32b71eSErnst Schwab unsigned long flags; 3381cf32b71eSErnst Schwab 338290808738SMark Brown ret = __spi_validate(spi, message); 338390808738SMark Brown if (ret != 0) 338490808738SMark Brown return ret; 338590808738SMark Brown 33868caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3387cf32b71eSErnst Schwab 3388cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3389cf32b71eSErnst Schwab 33908caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3391cf32b71eSErnst Schwab 3392cf32b71eSErnst Schwab return ret; 3393cf32b71eSErnst Schwab 3394cf32b71eSErnst Schwab } 3395cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked); 3396cf32b71eSErnst Schwab 33977d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 33987d077197SDavid Brownell 33998caab75fSGeert Uytterhoeven /* Utility methods for SPI protocol drivers, layered on 34007d077197SDavid Brownell * top of the core. Some other utility methods are defined as 34017d077197SDavid Brownell * inline functions. 34027d077197SDavid Brownell */ 34037d077197SDavid Brownell 34045d870c8eSAndrew Morton static void spi_complete(void *arg) 34055d870c8eSAndrew Morton { 34065d870c8eSAndrew Morton complete(arg); 34075d870c8eSAndrew Morton } 34085d870c8eSAndrew Morton 3409ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message) 3410cf32b71eSErnst Schwab { 3411cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 3412cf32b71eSErnst Schwab int status; 34138caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 34140461a414SMark Brown unsigned long flags; 34150461a414SMark Brown 34160461a414SMark Brown status = __spi_validate(spi, message); 34170461a414SMark Brown if (status != 0) 34180461a414SMark Brown return status; 3419cf32b71eSErnst Schwab 3420cf32b71eSErnst Schwab message->complete = spi_complete; 3421cf32b71eSErnst Schwab message->context = &done; 34220461a414SMark Brown message->spi = spi; 3423cf32b71eSErnst Schwab 34248caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); 3425eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); 3426eca2ebc7SMartin Sperl 34270461a414SMark Brown /* If we're not using the legacy transfer method then we will 34280461a414SMark Brown * try to transfer in the calling context so special case. 34290461a414SMark Brown * This code would be less tricky if we could remove the 34300461a414SMark Brown * support for driver implemented message queues. 34310461a414SMark Brown */ 34328caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 34338caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 34340461a414SMark Brown 34350461a414SMark Brown trace_spi_message_submit(message); 34360461a414SMark Brown 34370461a414SMark Brown status = __spi_queued_transfer(spi, message, false); 34380461a414SMark Brown 34398caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 34400461a414SMark Brown } else { 3441cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 34420461a414SMark Brown } 3443cf32b71eSErnst Schwab 3444cf32b71eSErnst Schwab if (status == 0) { 34450461a414SMark Brown /* Push out the messages in the calling context if we 34460461a414SMark Brown * can. 34470461a414SMark Brown */ 34488caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 34498caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3450eca2ebc7SMartin Sperl spi_sync_immediate); 3451eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, 3452eca2ebc7SMartin Sperl spi_sync_immediate); 34538caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, false); 3454eca2ebc7SMartin Sperl } 34550461a414SMark Brown 3456cf32b71eSErnst Schwab wait_for_completion(&done); 3457cf32b71eSErnst Schwab status = message->status; 3458cf32b71eSErnst Schwab } 3459cf32b71eSErnst Schwab message->context = NULL; 3460cf32b71eSErnst Schwab return status; 3461cf32b71eSErnst Schwab } 3462cf32b71eSErnst Schwab 34638ae12a0dSDavid Brownell /** 34648ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 34658ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 34668ae12a0dSDavid Brownell * @message: describes the data transfers 346733e34dc6SDavid Brownell * Context: can sleep 34688ae12a0dSDavid Brownell * 34698ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 34708ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 34718ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 34728ae12a0dSDavid Brownell * 34738ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 34748ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 34758ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 34768ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 34778ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 34788ae12a0dSDavid Brownell * 34790c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 34800c868461SDavid Brownell * message will not be freed before this call returns. 34810c868461SDavid Brownell * 348297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 34838ae12a0dSDavid Brownell */ 34848ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 34858ae12a0dSDavid Brownell { 3486ef4d96ecSMark Brown int ret; 3487ef4d96ecSMark Brown 34888caab75fSGeert Uytterhoeven mutex_lock(&spi->controller->bus_lock_mutex); 3489ef4d96ecSMark Brown ret = __spi_sync(spi, message); 34908caab75fSGeert Uytterhoeven mutex_unlock(&spi->controller->bus_lock_mutex); 3491ef4d96ecSMark Brown 3492ef4d96ecSMark Brown return ret; 34938ae12a0dSDavid Brownell } 34948ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 34958ae12a0dSDavid Brownell 3496cf32b71eSErnst Schwab /** 3497cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 3498cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3499cf32b71eSErnst Schwab * @message: describes the data transfers 3500cf32b71eSErnst Schwab * Context: can sleep 3501cf32b71eSErnst Schwab * 3502cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3503cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 3504cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 3505cf32b71eSErnst Schwab * 3506cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 350725985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 3508cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 3509cf32b71eSErnst Schwab * 351097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3511cf32b71eSErnst Schwab */ 3512cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 3513cf32b71eSErnst Schwab { 3514ef4d96ecSMark Brown return __spi_sync(spi, message); 3515cf32b71eSErnst Schwab } 3516cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 3517cf32b71eSErnst Schwab 3518cf32b71eSErnst Schwab /** 3519cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 35208caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that should be locked for exclusive bus access 3521cf32b71eSErnst Schwab * Context: can sleep 3522cf32b71eSErnst Schwab * 3523cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3524cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3525cf32b71eSErnst Schwab * 3526cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 3527cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 3528cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 3529cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 3530cf32b71eSErnst Schwab * 353197d56dc6SJavier Martinez Canillas * Return: always zero. 3532cf32b71eSErnst Schwab */ 35338caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr) 3534cf32b71eSErnst Schwab { 3535cf32b71eSErnst Schwab unsigned long flags; 3536cf32b71eSErnst Schwab 35378caab75fSGeert Uytterhoeven mutex_lock(&ctlr->bus_lock_mutex); 3538cf32b71eSErnst Schwab 35398caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 35408caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 1; 35418caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3542cf32b71eSErnst Schwab 3543cf32b71eSErnst Schwab /* mutex remains locked until spi_bus_unlock is called */ 3544cf32b71eSErnst Schwab 3545cf32b71eSErnst Schwab return 0; 3546cf32b71eSErnst Schwab } 3547cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 3548cf32b71eSErnst Schwab 3549cf32b71eSErnst Schwab /** 3550cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 35518caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that was locked for exclusive bus access 3552cf32b71eSErnst Schwab * Context: can sleep 3553cf32b71eSErnst Schwab * 3554cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3555cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3556cf32b71eSErnst Schwab * 3557cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 3558cf32b71eSErnst Schwab * call. 3559cf32b71eSErnst Schwab * 356097d56dc6SJavier Martinez Canillas * Return: always zero. 3561cf32b71eSErnst Schwab */ 35628caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr) 3563cf32b71eSErnst Schwab { 35648caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 3565cf32b71eSErnst Schwab 35668caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->bus_lock_mutex); 3567cf32b71eSErnst Schwab 3568cf32b71eSErnst Schwab return 0; 3569cf32b71eSErnst Schwab } 3570cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 3571cf32b71eSErnst Schwab 3572a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 3573a9948b61SDavid Brownell #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 35748ae12a0dSDavid Brownell 35758ae12a0dSDavid Brownell static u8 *buf; 35768ae12a0dSDavid Brownell 35778ae12a0dSDavid Brownell /** 35788ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 35798ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 35808ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 35818ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 358227570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 358327570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 358433e34dc6SDavid Brownell * Context: can sleep 35858ae12a0dSDavid Brownell * 35868ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 35878ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 35888ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 3589b885244eSDavid Brownell * This call may only be used from a context that may sleep. 35908ae12a0dSDavid Brownell * 35910c868461SDavid Brownell * Parameters to this routine are always copied using a small buffer; 359233e34dc6SDavid Brownell * portable code should never use this for more than 32 bytes. 359333e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 35940c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 359597d56dc6SJavier Martinez Canillas * 359697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 35978ae12a0dSDavid Brownell */ 35988ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 35990c4a1590SMark Brown const void *txbuf, unsigned n_tx, 36000c4a1590SMark Brown void *rxbuf, unsigned n_rx) 36018ae12a0dSDavid Brownell { 3602068f4070SDavid Brownell static DEFINE_MUTEX(lock); 36038ae12a0dSDavid Brownell 36048ae12a0dSDavid Brownell int status; 36058ae12a0dSDavid Brownell struct spi_message message; 3606bdff549eSDavid Brownell struct spi_transfer x[2]; 36078ae12a0dSDavid Brownell u8 *local_buf; 36088ae12a0dSDavid Brownell 3609b3a223eeSMark Brown /* Use preallocated DMA-safe buffer if we can. We can't avoid 3610b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 3611b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 3612b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 36138ae12a0dSDavid Brownell */ 3614b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 36152cd94c8aSMark Brown local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 36162cd94c8aSMark Brown GFP_KERNEL | GFP_DMA); 3617b3a223eeSMark Brown if (!local_buf) 3618b3a223eeSMark Brown return -ENOMEM; 3619b3a223eeSMark Brown } else { 3620b3a223eeSMark Brown local_buf = buf; 3621b3a223eeSMark Brown } 36228ae12a0dSDavid Brownell 36238275c642SVitaly Wool spi_message_init(&message); 36245fe5f05eSJingoo Han memset(x, 0, sizeof(x)); 3625bdff549eSDavid Brownell if (n_tx) { 3626bdff549eSDavid Brownell x[0].len = n_tx; 3627bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 3628bdff549eSDavid Brownell } 3629bdff549eSDavid Brownell if (n_rx) { 3630bdff549eSDavid Brownell x[1].len = n_rx; 3631bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 3632bdff549eSDavid Brownell } 36338275c642SVitaly Wool 36348ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 3635bdff549eSDavid Brownell x[0].tx_buf = local_buf; 3636bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 36378ae12a0dSDavid Brownell 36388ae12a0dSDavid Brownell /* do the i/o */ 36398ae12a0dSDavid Brownell status = spi_sync(spi, &message); 36409b938b74SMarc Pignat if (status == 0) 3641bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 36428ae12a0dSDavid Brownell 3643bdff549eSDavid Brownell if (x[0].tx_buf == buf) 3644068f4070SDavid Brownell mutex_unlock(&lock); 36458ae12a0dSDavid Brownell else 36468ae12a0dSDavid Brownell kfree(local_buf); 36478ae12a0dSDavid Brownell 36488ae12a0dSDavid Brownell return status; 36498ae12a0dSDavid Brownell } 36508ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 36518ae12a0dSDavid Brownell 36528ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 36538ae12a0dSDavid Brownell 36545f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF) 3655ce79d54aSPantelis Antoniou /* must call put_device() when done with returned spi_device device */ 36565f143af7SMarco Felsch struct spi_device *of_find_spi_device_by_node(struct device_node *node) 3657ce79d54aSPantelis Antoniou { 3658cfba5de9SSuzuki K Poulose struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); 3659cfba5de9SSuzuki K Poulose 3660ce79d54aSPantelis Antoniou return dev ? to_spi_device(dev) : NULL; 3661ce79d54aSPantelis Antoniou } 36625f143af7SMarco Felsch EXPORT_SYMBOL_GPL(of_find_spi_device_by_node); 36635f143af7SMarco Felsch #endif /* IS_ENABLED(CONFIG_OF) */ 3664ce79d54aSPantelis Antoniou 36655f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF_DYNAMIC) 36668caab75fSGeert Uytterhoeven /* the spi controllers are not using spi_bus, so we find it with another way */ 36678caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 3668ce79d54aSPantelis Antoniou { 3669ce79d54aSPantelis Antoniou struct device *dev; 3670ce79d54aSPantelis Antoniou 3671cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_master_class, node); 36726c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 3673cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_slave_class, node); 3674ce79d54aSPantelis Antoniou if (!dev) 3675ce79d54aSPantelis Antoniou return NULL; 3676ce79d54aSPantelis Antoniou 3677ce79d54aSPantelis Antoniou /* reference got in class_find_device */ 36788caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 3679ce79d54aSPantelis Antoniou } 3680ce79d54aSPantelis Antoniou 3681ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action, 3682ce79d54aSPantelis Antoniou void *arg) 3683ce79d54aSPantelis Antoniou { 3684ce79d54aSPantelis Antoniou struct of_reconfig_data *rd = arg; 36858caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 3686ce79d54aSPantelis Antoniou struct spi_device *spi; 3687ce79d54aSPantelis Antoniou 3688ce79d54aSPantelis Antoniou switch (of_reconfig_get_state_change(action, arg)) { 3689ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 36908caab75fSGeert Uytterhoeven ctlr = of_find_spi_controller_by_node(rd->dn->parent); 36918caab75fSGeert Uytterhoeven if (ctlr == NULL) 3692ce79d54aSPantelis Antoniou return NOTIFY_OK; /* not for us */ 3693ce79d54aSPantelis Antoniou 3694bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 36958caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 3696bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 3697bd6c1644SGeert Uytterhoeven } 3698bd6c1644SGeert Uytterhoeven 36998caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, rd->dn); 37008caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 3701ce79d54aSPantelis Antoniou 3702ce79d54aSPantelis Antoniou if (IS_ERR(spi)) { 370325c56c88SRob Herring pr_err("%s: failed to create for '%pOF'\n", 370425c56c88SRob Herring __func__, rd->dn); 3705e0af98a7SRalf Ramsauer of_node_clear_flag(rd->dn, OF_POPULATED); 3706ce79d54aSPantelis Antoniou return notifier_from_errno(PTR_ERR(spi)); 3707ce79d54aSPantelis Antoniou } 3708ce79d54aSPantelis Antoniou break; 3709ce79d54aSPantelis Antoniou 3710ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 3711bd6c1644SGeert Uytterhoeven /* already depopulated? */ 3712bd6c1644SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 3713bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 3714bd6c1644SGeert Uytterhoeven 3715ce79d54aSPantelis Antoniou /* find our device by node */ 3716ce79d54aSPantelis Antoniou spi = of_find_spi_device_by_node(rd->dn); 3717ce79d54aSPantelis Antoniou if (spi == NULL) 3718ce79d54aSPantelis Antoniou return NOTIFY_OK; /* no? not meant for us */ 3719ce79d54aSPantelis Antoniou 3720ce79d54aSPantelis Antoniou /* unregister takes one ref away */ 3721ce79d54aSPantelis Antoniou spi_unregister_device(spi); 3722ce79d54aSPantelis Antoniou 3723ce79d54aSPantelis Antoniou /* and put the reference of the find */ 3724ce79d54aSPantelis Antoniou put_device(&spi->dev); 3725ce79d54aSPantelis Antoniou break; 3726ce79d54aSPantelis Antoniou } 3727ce79d54aSPantelis Antoniou 3728ce79d54aSPantelis Antoniou return NOTIFY_OK; 3729ce79d54aSPantelis Antoniou } 3730ce79d54aSPantelis Antoniou 3731ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = { 3732ce79d54aSPantelis Antoniou .notifier_call = of_spi_notify, 3733ce79d54aSPantelis Antoniou }; 3734ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 3735ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier; 3736ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 3737ce79d54aSPantelis Antoniou 37387f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI) 37398caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data) 37407f24467fSOctavian Purdila { 37417f24467fSOctavian Purdila return ACPI_COMPANION(dev->parent) == data; 37427f24467fSOctavian Purdila } 37437f24467fSOctavian Purdila 37448caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 37457f24467fSOctavian Purdila { 37467f24467fSOctavian Purdila struct device *dev; 37477f24467fSOctavian Purdila 37487f24467fSOctavian Purdila dev = class_find_device(&spi_master_class, NULL, adev, 37498caab75fSGeert Uytterhoeven spi_acpi_controller_match); 37506c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 37516c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, adev, 37528caab75fSGeert Uytterhoeven spi_acpi_controller_match); 37537f24467fSOctavian Purdila if (!dev) 37547f24467fSOctavian Purdila return NULL; 37557f24467fSOctavian Purdila 37568caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 37577f24467fSOctavian Purdila } 37587f24467fSOctavian Purdila 37597f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 37607f24467fSOctavian Purdila { 37617f24467fSOctavian Purdila struct device *dev; 37627f24467fSOctavian Purdila 3763*00500147SSuzuki K Poulose dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev); 37647f24467fSOctavian Purdila return dev ? to_spi_device(dev) : NULL; 37657f24467fSOctavian Purdila } 37667f24467fSOctavian Purdila 37677f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 37687f24467fSOctavian Purdila void *arg) 37697f24467fSOctavian Purdila { 37707f24467fSOctavian Purdila struct acpi_device *adev = arg; 37718caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 37727f24467fSOctavian Purdila struct spi_device *spi; 37737f24467fSOctavian Purdila 37747f24467fSOctavian Purdila switch (value) { 37757f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_ADD: 37768caab75fSGeert Uytterhoeven ctlr = acpi_spi_find_controller_by_adev(adev->parent); 37778caab75fSGeert Uytterhoeven if (!ctlr) 37787f24467fSOctavian Purdila break; 37797f24467fSOctavian Purdila 37808caab75fSGeert Uytterhoeven acpi_register_spi_device(ctlr, adev); 37818caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 37827f24467fSOctavian Purdila break; 37837f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_REMOVE: 37847f24467fSOctavian Purdila if (!acpi_device_enumerated(adev)) 37857f24467fSOctavian Purdila break; 37867f24467fSOctavian Purdila 37877f24467fSOctavian Purdila spi = acpi_spi_find_device_by_adev(adev); 37887f24467fSOctavian Purdila if (!spi) 37897f24467fSOctavian Purdila break; 37907f24467fSOctavian Purdila 37917f24467fSOctavian Purdila spi_unregister_device(spi); 37927f24467fSOctavian Purdila put_device(&spi->dev); 37937f24467fSOctavian Purdila break; 37947f24467fSOctavian Purdila } 37957f24467fSOctavian Purdila 37967f24467fSOctavian Purdila return NOTIFY_OK; 37977f24467fSOctavian Purdila } 37987f24467fSOctavian Purdila 37997f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = { 38007f24467fSOctavian Purdila .notifier_call = acpi_spi_notify, 38017f24467fSOctavian Purdila }; 38027f24467fSOctavian Purdila #else 38037f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier; 38047f24467fSOctavian Purdila #endif 38057f24467fSOctavian Purdila 38068ae12a0dSDavid Brownell static int __init spi_init(void) 38078ae12a0dSDavid Brownell { 3808b885244eSDavid Brownell int status; 38098ae12a0dSDavid Brownell 3810e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 3811b885244eSDavid Brownell if (!buf) { 3812b885244eSDavid Brownell status = -ENOMEM; 3813b885244eSDavid Brownell goto err0; 38148ae12a0dSDavid Brownell } 3815b885244eSDavid Brownell 3816b885244eSDavid Brownell status = bus_register(&spi_bus_type); 3817b885244eSDavid Brownell if (status < 0) 3818b885244eSDavid Brownell goto err1; 3819b885244eSDavid Brownell 3820b885244eSDavid Brownell status = class_register(&spi_master_class); 3821b885244eSDavid Brownell if (status < 0) 3822b885244eSDavid Brownell goto err2; 3823ce79d54aSPantelis Antoniou 38246c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 38256c364062SGeert Uytterhoeven status = class_register(&spi_slave_class); 38266c364062SGeert Uytterhoeven if (status < 0) 38276c364062SGeert Uytterhoeven goto err3; 38286c364062SGeert Uytterhoeven } 38296c364062SGeert Uytterhoeven 38305267720eSFabio Estevam if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 3831ce79d54aSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 38327f24467fSOctavian Purdila if (IS_ENABLED(CONFIG_ACPI)) 38337f24467fSOctavian Purdila WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 3834ce79d54aSPantelis Antoniou 3835b885244eSDavid Brownell return 0; 3836b885244eSDavid Brownell 38376c364062SGeert Uytterhoeven err3: 38386c364062SGeert Uytterhoeven class_unregister(&spi_master_class); 3839b885244eSDavid Brownell err2: 3840b885244eSDavid Brownell bus_unregister(&spi_bus_type); 3841b885244eSDavid Brownell err1: 3842b885244eSDavid Brownell kfree(buf); 3843b885244eSDavid Brownell buf = NULL; 3844b885244eSDavid Brownell err0: 3845b885244eSDavid Brownell return status; 3846b885244eSDavid Brownell } 3847b885244eSDavid Brownell 38488ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(), 38498ae12a0dSDavid Brownell * but even essential drivers wait till later 3850b885244eSDavid Brownell * 3851b885244eSDavid Brownell * REVISIT only boardinfo really needs static linking. the rest (device and 3852b885244eSDavid Brownell * driver registration) _could_ be dynamically linked (modular) ... costs 3853b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 38548ae12a0dSDavid Brownell */ 3855673c0c00SDavid Brownell postcore_initcall(spi_init); 3856