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_controller_put(spi->controller); 515039563eSTrent Piepho kfree(spi->driver_override); 5207a389feSRoman Tereshonkov kfree(spi); 538ae12a0dSDavid Brownell } 548ae12a0dSDavid Brownell 558ae12a0dSDavid Brownell static ssize_t 568ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf) 578ae12a0dSDavid Brownell { 588ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 598c4ff6d0SZhang Rui int len; 608c4ff6d0SZhang Rui 618c4ff6d0SZhang Rui len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 628c4ff6d0SZhang Rui if (len != -ENODEV) 638c4ff6d0SZhang Rui return len; 648ae12a0dSDavid Brownell 65d8e328b3SGrant Likely return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 668ae12a0dSDavid Brownell } 67aa7da564SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias); 688ae12a0dSDavid Brownell 695039563eSTrent Piepho static ssize_t driver_override_store(struct device *dev, 705039563eSTrent Piepho struct device_attribute *a, 715039563eSTrent Piepho const char *buf, size_t count) 725039563eSTrent Piepho { 735039563eSTrent Piepho struct spi_device *spi = to_spi_device(dev); 745039563eSTrent Piepho const char *end = memchr(buf, '\n', count); 755039563eSTrent Piepho const size_t len = end ? end - buf : count; 765039563eSTrent Piepho const char *driver_override, *old; 775039563eSTrent Piepho 785039563eSTrent Piepho /* We need to keep extra room for a newline when displaying value */ 795039563eSTrent Piepho if (len >= (PAGE_SIZE - 1)) 805039563eSTrent Piepho return -EINVAL; 815039563eSTrent Piepho 825039563eSTrent Piepho driver_override = kstrndup(buf, len, GFP_KERNEL); 835039563eSTrent Piepho if (!driver_override) 845039563eSTrent Piepho return -ENOMEM; 855039563eSTrent Piepho 865039563eSTrent Piepho device_lock(dev); 875039563eSTrent Piepho old = spi->driver_override; 885039563eSTrent Piepho if (len) { 895039563eSTrent Piepho spi->driver_override = driver_override; 905039563eSTrent Piepho } else { 91be73e323SAndy Shevchenko /* Empty string, disable driver override */ 925039563eSTrent Piepho spi->driver_override = NULL; 935039563eSTrent Piepho kfree(driver_override); 945039563eSTrent Piepho } 955039563eSTrent Piepho device_unlock(dev); 965039563eSTrent Piepho kfree(old); 975039563eSTrent Piepho 985039563eSTrent Piepho return count; 995039563eSTrent Piepho } 1005039563eSTrent Piepho 1015039563eSTrent Piepho static ssize_t driver_override_show(struct device *dev, 1025039563eSTrent Piepho struct device_attribute *a, char *buf) 1035039563eSTrent Piepho { 1045039563eSTrent Piepho const struct spi_device *spi = to_spi_device(dev); 1055039563eSTrent Piepho ssize_t len; 1065039563eSTrent Piepho 1075039563eSTrent Piepho device_lock(dev); 1085039563eSTrent Piepho len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : ""); 1095039563eSTrent Piepho device_unlock(dev); 1105039563eSTrent Piepho return len; 1115039563eSTrent Piepho } 1125039563eSTrent Piepho static DEVICE_ATTR_RW(driver_override); 1135039563eSTrent Piepho 114eca2ebc7SMartin Sperl #define SPI_STATISTICS_ATTRS(field, file) \ 1158caab75fSGeert Uytterhoeven static ssize_t spi_controller_##field##_show(struct device *dev, \ 116eca2ebc7SMartin Sperl struct device_attribute *attr, \ 117eca2ebc7SMartin Sperl char *buf) \ 118eca2ebc7SMartin Sperl { \ 1198caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, \ 1208caab75fSGeert Uytterhoeven struct spi_controller, dev); \ 1218caab75fSGeert Uytterhoeven return spi_statistics_##field##_show(&ctlr->statistics, buf); \ 122eca2ebc7SMartin Sperl } \ 1238caab75fSGeert Uytterhoeven static struct device_attribute dev_attr_spi_controller_##field = { \ 124ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 1258caab75fSGeert Uytterhoeven .show = spi_controller_##field##_show, \ 126eca2ebc7SMartin Sperl }; \ 127eca2ebc7SMartin Sperl static ssize_t spi_device_##field##_show(struct device *dev, \ 128eca2ebc7SMartin Sperl struct device_attribute *attr, \ 129eca2ebc7SMartin Sperl char *buf) \ 130eca2ebc7SMartin Sperl { \ 131d1eba93bSGeliang Tang struct spi_device *spi = to_spi_device(dev); \ 132eca2ebc7SMartin Sperl return spi_statistics_##field##_show(&spi->statistics, buf); \ 133eca2ebc7SMartin Sperl } \ 134eca2ebc7SMartin Sperl static struct device_attribute dev_attr_spi_device_##field = { \ 135ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 136eca2ebc7SMartin Sperl .show = spi_device_##field##_show, \ 137eca2ebc7SMartin Sperl } 138eca2ebc7SMartin Sperl 139eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \ 140eca2ebc7SMartin Sperl static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ 141eca2ebc7SMartin Sperl char *buf) \ 142eca2ebc7SMartin Sperl { \ 143eca2ebc7SMartin Sperl unsigned long flags; \ 144eca2ebc7SMartin Sperl ssize_t len; \ 145eca2ebc7SMartin Sperl spin_lock_irqsave(&stat->lock, flags); \ 146eca2ebc7SMartin Sperl len = sprintf(buf, format_string, stat->field); \ 147eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stat->lock, flags); \ 148eca2ebc7SMartin Sperl return len; \ 149eca2ebc7SMartin Sperl } \ 150eca2ebc7SMartin Sperl SPI_STATISTICS_ATTRS(name, file) 151eca2ebc7SMartin Sperl 152eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW(field, format_string) \ 153eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ 154eca2ebc7SMartin Sperl field, format_string) 155eca2ebc7SMartin Sperl 156eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(messages, "%lu"); 157eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(transfers, "%lu"); 158eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(errors, "%lu"); 159eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(timedout, "%lu"); 160eca2ebc7SMartin Sperl 161eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync, "%lu"); 162eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu"); 163eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_async, "%lu"); 164eca2ebc7SMartin Sperl 165eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes, "%llu"); 166eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_rx, "%llu"); 167eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_tx, "%llu"); 168eca2ebc7SMartin Sperl 1696b7bc061SMartin Sperl #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ 1706b7bc061SMartin Sperl SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ 1716b7bc061SMartin Sperl "transfer_bytes_histo_" number, \ 1726b7bc061SMartin Sperl transfer_bytes_histo[index], "%lu") 1736b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); 1746b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); 1756b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); 1766b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); 1776b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); 1786b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); 1796b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); 1806b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); 1816b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); 1826b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); 1836b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); 1846b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); 1856b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); 1866b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); 1876b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); 1886b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); 1896b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); 1906b7bc061SMartin Sperl 191d9f12122SMartin Sperl SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu"); 192d9f12122SMartin Sperl 193aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = { 194aa7da564SGreg Kroah-Hartman &dev_attr_modalias.attr, 1955039563eSTrent Piepho &dev_attr_driver_override.attr, 196aa7da564SGreg Kroah-Hartman NULL, 1978ae12a0dSDavid Brownell }; 198eca2ebc7SMartin Sperl 199eca2ebc7SMartin Sperl static const struct attribute_group spi_dev_group = { 200eca2ebc7SMartin Sperl .attrs = spi_dev_attrs, 201eca2ebc7SMartin Sperl }; 202eca2ebc7SMartin Sperl 203eca2ebc7SMartin Sperl static struct attribute *spi_device_statistics_attrs[] = { 204eca2ebc7SMartin Sperl &dev_attr_spi_device_messages.attr, 205eca2ebc7SMartin Sperl &dev_attr_spi_device_transfers.attr, 206eca2ebc7SMartin Sperl &dev_attr_spi_device_errors.attr, 207eca2ebc7SMartin Sperl &dev_attr_spi_device_timedout.attr, 208eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync.attr, 209eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync_immediate.attr, 210eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_async.attr, 211eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes.attr, 212eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_rx.attr, 213eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_tx.attr, 2146b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo0.attr, 2156b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo1.attr, 2166b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo2.attr, 2176b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo3.attr, 2186b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo4.attr, 2196b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo5.attr, 2206b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo6.attr, 2216b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo7.attr, 2226b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo8.attr, 2236b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo9.attr, 2246b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo10.attr, 2256b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo11.attr, 2266b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo12.attr, 2276b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo13.attr, 2286b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo14.attr, 2296b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo15.attr, 2306b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo16.attr, 231d9f12122SMartin Sperl &dev_attr_spi_device_transfers_split_maxsize.attr, 232eca2ebc7SMartin Sperl NULL, 233eca2ebc7SMartin Sperl }; 234eca2ebc7SMartin Sperl 235eca2ebc7SMartin Sperl static const struct attribute_group spi_device_statistics_group = { 236eca2ebc7SMartin Sperl .name = "statistics", 237eca2ebc7SMartin Sperl .attrs = spi_device_statistics_attrs, 238eca2ebc7SMartin Sperl }; 239eca2ebc7SMartin Sperl 240eca2ebc7SMartin Sperl static const struct attribute_group *spi_dev_groups[] = { 241eca2ebc7SMartin Sperl &spi_dev_group, 242eca2ebc7SMartin Sperl &spi_device_statistics_group, 243eca2ebc7SMartin Sperl NULL, 244eca2ebc7SMartin Sperl }; 245eca2ebc7SMartin Sperl 2468caab75fSGeert Uytterhoeven static struct attribute *spi_controller_statistics_attrs[] = { 2478caab75fSGeert Uytterhoeven &dev_attr_spi_controller_messages.attr, 2488caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers.attr, 2498caab75fSGeert Uytterhoeven &dev_attr_spi_controller_errors.attr, 2508caab75fSGeert Uytterhoeven &dev_attr_spi_controller_timedout.attr, 2518caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync.attr, 2528caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync_immediate.attr, 2538caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_async.attr, 2548caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes.attr, 2558caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_rx.attr, 2568caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_tx.attr, 2578caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo0.attr, 2588caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo1.attr, 2598caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo2.attr, 2608caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo3.attr, 2618caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo4.attr, 2628caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo5.attr, 2638caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo6.attr, 2648caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo7.attr, 2658caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo8.attr, 2668caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo9.attr, 2678caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo10.attr, 2688caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo11.attr, 2698caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo12.attr, 2708caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo13.attr, 2718caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo14.attr, 2728caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo15.attr, 2738caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo16.attr, 2748caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers_split_maxsize.attr, 275eca2ebc7SMartin Sperl NULL, 276eca2ebc7SMartin Sperl }; 277eca2ebc7SMartin Sperl 2788caab75fSGeert Uytterhoeven static const struct attribute_group spi_controller_statistics_group = { 279eca2ebc7SMartin Sperl .name = "statistics", 2808caab75fSGeert Uytterhoeven .attrs = spi_controller_statistics_attrs, 281eca2ebc7SMartin Sperl }; 282eca2ebc7SMartin Sperl 283eca2ebc7SMartin Sperl static const struct attribute_group *spi_master_groups[] = { 2848caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 285eca2ebc7SMartin Sperl NULL, 286eca2ebc7SMartin Sperl }; 287eca2ebc7SMartin Sperl 288eca2ebc7SMartin Sperl void spi_statistics_add_transfer_stats(struct spi_statistics *stats, 289eca2ebc7SMartin Sperl struct spi_transfer *xfer, 2908caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 291eca2ebc7SMartin Sperl { 292eca2ebc7SMartin Sperl unsigned long flags; 2936b7bc061SMartin Sperl int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; 2946b7bc061SMartin Sperl 2956b7bc061SMartin Sperl if (l2len < 0) 2966b7bc061SMartin Sperl l2len = 0; 297eca2ebc7SMartin Sperl 298eca2ebc7SMartin Sperl spin_lock_irqsave(&stats->lock, flags); 299eca2ebc7SMartin Sperl 300eca2ebc7SMartin Sperl stats->transfers++; 3016b7bc061SMartin Sperl stats->transfer_bytes_histo[l2len]++; 302eca2ebc7SMartin Sperl 303eca2ebc7SMartin Sperl stats->bytes += xfer->len; 304eca2ebc7SMartin Sperl if ((xfer->tx_buf) && 3058caab75fSGeert Uytterhoeven (xfer->tx_buf != ctlr->dummy_tx)) 306eca2ebc7SMartin Sperl stats->bytes_tx += xfer->len; 307eca2ebc7SMartin Sperl if ((xfer->rx_buf) && 3088caab75fSGeert Uytterhoeven (xfer->rx_buf != ctlr->dummy_rx)) 309eca2ebc7SMartin Sperl stats->bytes_rx += xfer->len; 310eca2ebc7SMartin Sperl 311eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stats->lock, flags); 312eca2ebc7SMartin Sperl } 313eca2ebc7SMartin Sperl EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats); 3148ae12a0dSDavid Brownell 3158ae12a0dSDavid Brownell /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, 3168ae12a0dSDavid Brownell * and the sysfs version makes coldplug work too. 3178ae12a0dSDavid Brownell */ 3188ae12a0dSDavid Brownell 31975368bf6SAnton Vorontsov static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, 32075368bf6SAnton Vorontsov const struct spi_device *sdev) 32175368bf6SAnton Vorontsov { 32275368bf6SAnton Vorontsov while (id->name[0]) { 32375368bf6SAnton Vorontsov if (!strcmp(sdev->modalias, id->name)) 32475368bf6SAnton Vorontsov return id; 32575368bf6SAnton Vorontsov id++; 32675368bf6SAnton Vorontsov } 32775368bf6SAnton Vorontsov return NULL; 32875368bf6SAnton Vorontsov } 32975368bf6SAnton Vorontsov 33075368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 33175368bf6SAnton Vorontsov { 33275368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 33375368bf6SAnton Vorontsov 33475368bf6SAnton Vorontsov return spi_match_id(sdrv->id_table, sdev); 33575368bf6SAnton Vorontsov } 33675368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id); 33775368bf6SAnton Vorontsov 3388ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 3398ae12a0dSDavid Brownell { 3408ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 34175368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(drv); 34275368bf6SAnton Vorontsov 3435039563eSTrent Piepho /* Check override first, and if set, only use the named driver */ 3445039563eSTrent Piepho if (spi->driver_override) 3455039563eSTrent Piepho return strcmp(spi->driver_override, drv->name) == 0; 3465039563eSTrent Piepho 3472b7a32f7SSinan Akman /* Attempt an OF style match */ 3482b7a32f7SSinan Akman if (of_driver_match_device(dev, drv)) 3492b7a32f7SSinan Akman return 1; 3502b7a32f7SSinan Akman 35164bee4d2SMika Westerberg /* Then try ACPI */ 35264bee4d2SMika Westerberg if (acpi_driver_match_device(dev, drv)) 35364bee4d2SMika Westerberg return 1; 35464bee4d2SMika Westerberg 35575368bf6SAnton Vorontsov if (sdrv->id_table) 35675368bf6SAnton Vorontsov return !!spi_match_id(sdrv->id_table, spi); 3578ae12a0dSDavid Brownell 35835f74fcaSKay Sievers return strcmp(spi->modalias, drv->name) == 0; 3598ae12a0dSDavid Brownell } 3608ae12a0dSDavid Brownell 3617eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 3628ae12a0dSDavid Brownell { 3638ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 3648c4ff6d0SZhang Rui int rc; 3658c4ff6d0SZhang Rui 3668c4ff6d0SZhang Rui rc = acpi_device_uevent_modalias(dev, env); 3678c4ff6d0SZhang Rui if (rc != -ENODEV) 3688c4ff6d0SZhang Rui return rc; 3698ae12a0dSDavid Brownell 3702856670fSAndy Shevchenko return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 3718ae12a0dSDavid Brownell } 3728ae12a0dSDavid Brownell 3739db34ee6SUwe Kleine-König static int spi_probe(struct device *dev) 374b885244eSDavid Brownell { 375b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 37644af7927SJon Hunter struct spi_device *spi = to_spi_device(dev); 37733cf00e5SMika Westerberg int ret; 378b885244eSDavid Brownell 37986be408bSSylwester Nawrocki ret = of_clk_set_defaults(dev->of_node, false); 38086be408bSSylwester Nawrocki if (ret) 38186be408bSSylwester Nawrocki return ret; 38286be408bSSylwester Nawrocki 38344af7927SJon Hunter if (dev->of_node) { 38444af7927SJon Hunter spi->irq = of_irq_get(dev->of_node, 0); 38544af7927SJon Hunter if (spi->irq == -EPROBE_DEFER) 38644af7927SJon Hunter return -EPROBE_DEFER; 38744af7927SJon Hunter if (spi->irq < 0) 38844af7927SJon Hunter spi->irq = 0; 38944af7927SJon Hunter } 39044af7927SJon Hunter 391676e7c25SUlf Hansson ret = dev_pm_domain_attach(dev, true); 39271f277a7SUlf Hansson if (ret) 39371f277a7SUlf Hansson return ret; 39471f277a7SUlf Hansson 395440408dbSUwe Kleine-König if (sdrv->probe) { 39644af7927SJon Hunter ret = sdrv->probe(spi); 39733cf00e5SMika Westerberg if (ret) 398676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 399440408dbSUwe Kleine-König } 40033cf00e5SMika Westerberg 40133cf00e5SMika Westerberg return ret; 402b885244eSDavid Brownell } 403b885244eSDavid Brownell 404fc7a6209SUwe Kleine-König static void spi_remove(struct device *dev) 405b885244eSDavid Brownell { 406b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 407b885244eSDavid Brownell 4087795d475SUwe Kleine-König if (sdrv->remove) { 4097795d475SUwe Kleine-König int ret; 4107795d475SUwe Kleine-König 411aec35f4eSJean Delvare ret = sdrv->remove(to_spi_device(dev)); 4127795d475SUwe Kleine-König if (ret) 4137795d475SUwe Kleine-König dev_warn(dev, 4147795d475SUwe Kleine-König "Failed to unbind driver (%pe), ignoring\n", 4157795d475SUwe Kleine-König ERR_PTR(ret)); 4167795d475SUwe Kleine-König } 4177795d475SUwe Kleine-König 418676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 419b885244eSDavid Brownell } 420b885244eSDavid Brownell 4219db34ee6SUwe Kleine-König static void spi_shutdown(struct device *dev) 422b885244eSDavid Brownell { 423a6f483b2SMarek Szyprowski if (dev->driver) { 424b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 425b885244eSDavid Brownell 4269db34ee6SUwe Kleine-König if (sdrv->shutdown) 427b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 428b885244eSDavid Brownell } 429a6f483b2SMarek Szyprowski } 430b885244eSDavid Brownell 4319db34ee6SUwe Kleine-König struct bus_type spi_bus_type = { 4329db34ee6SUwe Kleine-König .name = "spi", 4339db34ee6SUwe Kleine-König .dev_groups = spi_dev_groups, 4349db34ee6SUwe Kleine-König .match = spi_match_device, 4359db34ee6SUwe Kleine-König .uevent = spi_uevent, 4369db34ee6SUwe Kleine-König .probe = spi_probe, 4379db34ee6SUwe Kleine-König .remove = spi_remove, 4389db34ee6SUwe Kleine-König .shutdown = spi_shutdown, 4399db34ee6SUwe Kleine-König }; 4409db34ee6SUwe Kleine-König EXPORT_SYMBOL_GPL(spi_bus_type); 4419db34ee6SUwe Kleine-König 44233e34dc6SDavid Brownell /** 443ca5d2485SAndrew F. Davis * __spi_register_driver - register a SPI driver 44488c9321dSThierry Reding * @owner: owner module of the driver to register 44533e34dc6SDavid Brownell * @sdrv: the driver to register 44633e34dc6SDavid Brownell * Context: can sleep 44797d56dc6SJavier Martinez Canillas * 44897d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 44933e34dc6SDavid Brownell */ 450ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) 451b885244eSDavid Brownell { 452ca5d2485SAndrew F. Davis sdrv->driver.owner = owner; 453b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 454b885244eSDavid Brownell return driver_register(&sdrv->driver); 455b885244eSDavid Brownell } 456ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver); 457b885244eSDavid Brownell 4588ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 4598ae12a0dSDavid Brownell 4608ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that 4618caab75fSGeert Uytterhoeven * would make them board-specific. Similarly with SPI controller drivers. 4628ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 4638ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 4648ae12a0dSDavid Brownell */ 4658ae12a0dSDavid Brownell 4668ae12a0dSDavid Brownell struct boardinfo { 4678ae12a0dSDavid Brownell struct list_head list; 4682b9603a0SFeng Tang struct spi_board_info board_info; 4698ae12a0dSDavid Brownell }; 4708ae12a0dSDavid Brownell 4718ae12a0dSDavid Brownell static LIST_HEAD(board_list); 4728caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list); 4732b9603a0SFeng Tang 4742b9603a0SFeng Tang /* 475be73e323SAndy Shevchenko * Used to protect add/del operation for board_info list and 4768caab75fSGeert Uytterhoeven * spi_controller list, and their matching process 4779b61e302SSuniel Mahesh * also used to protect object of type struct idr 4782b9603a0SFeng Tang */ 47994040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 4808ae12a0dSDavid Brownell 481dc87c98eSGrant Likely /** 482dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 4838caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 484dc87c98eSGrant Likely * Context: can sleep 485dc87c98eSGrant Likely * 486dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 487dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 488dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 489dc87c98eSGrant Likely * spi_add_device() on it. 490dc87c98eSGrant Likely * 491dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 4928caab75fSGeert Uytterhoeven * spi_device structure to add it to the SPI controller. If the caller 493dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 494dc87c98eSGrant Likely * call spi_dev_put() on it. 495dc87c98eSGrant Likely * 49697d56dc6SJavier Martinez Canillas * Return: a pointer to the new device, or NULL. 497dc87c98eSGrant Likely */ 4988caab75fSGeert Uytterhoeven struct spi_device *spi_alloc_device(struct spi_controller *ctlr) 499dc87c98eSGrant Likely { 500dc87c98eSGrant Likely struct spi_device *spi; 501dc87c98eSGrant Likely 5028caab75fSGeert Uytterhoeven if (!spi_controller_get(ctlr)) 503dc87c98eSGrant Likely return NULL; 504dc87c98eSGrant Likely 5055fe5f05eSJingoo Han spi = kzalloc(sizeof(*spi), GFP_KERNEL); 506dc87c98eSGrant Likely if (!spi) { 5078caab75fSGeert Uytterhoeven spi_controller_put(ctlr); 508dc87c98eSGrant Likely return NULL; 509dc87c98eSGrant Likely } 510dc87c98eSGrant Likely 5118caab75fSGeert Uytterhoeven spi->master = spi->controller = ctlr; 5128caab75fSGeert Uytterhoeven spi->dev.parent = &ctlr->dev; 513dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 514dc87c98eSGrant Likely spi->dev.release = spidev_release; 515446411e1SAndreas Larsson spi->cs_gpio = -ENOENT; 516ea235786SJohn Garry spi->mode = ctlr->buswidth_override_bits; 517eca2ebc7SMartin Sperl 518eca2ebc7SMartin Sperl spin_lock_init(&spi->statistics.lock); 519eca2ebc7SMartin Sperl 520dc87c98eSGrant Likely device_initialize(&spi->dev); 521dc87c98eSGrant Likely return spi; 522dc87c98eSGrant Likely } 523dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device); 524dc87c98eSGrant Likely 525e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi) 526e13ac47bSJarkko Nikula { 527e13ac47bSJarkko Nikula struct acpi_device *adev = ACPI_COMPANION(&spi->dev); 528e13ac47bSJarkko Nikula 529e13ac47bSJarkko Nikula if (adev) { 530e13ac47bSJarkko Nikula dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); 531e13ac47bSJarkko Nikula return; 532e13ac47bSJarkko Nikula } 533e13ac47bSJarkko Nikula 5348caab75fSGeert Uytterhoeven dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), 535e13ac47bSJarkko Nikula spi->chip_select); 536e13ac47bSJarkko Nikula } 537e13ac47bSJarkko Nikula 538b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data) 539b6fb8d3aSMika Westerberg { 540b6fb8d3aSMika Westerberg struct spi_device *spi = to_spi_device(dev); 541b6fb8d3aSMika Westerberg struct spi_device *new_spi = data; 542b6fb8d3aSMika Westerberg 5438caab75fSGeert Uytterhoeven if (spi->controller == new_spi->controller && 544b6fb8d3aSMika Westerberg spi->chip_select == new_spi->chip_select) 545b6fb8d3aSMika Westerberg return -EBUSY; 546b6fb8d3aSMika Westerberg return 0; 547b6fb8d3aSMika Westerberg } 548b6fb8d3aSMika Westerberg 549c7299feaSSaravana Kannan static void spi_cleanup(struct spi_device *spi) 550c7299feaSSaravana Kannan { 551c7299feaSSaravana Kannan if (spi->controller->cleanup) 552c7299feaSSaravana Kannan spi->controller->cleanup(spi); 553c7299feaSSaravana Kannan } 554c7299feaSSaravana Kannan 5550c79378cSSebastian Reichel static int __spi_add_device(struct spi_device *spi) 5560c79378cSSebastian Reichel { 5570c79378cSSebastian Reichel struct spi_controller *ctlr = spi->controller; 5580c79378cSSebastian Reichel struct device *dev = ctlr->dev.parent; 5590c79378cSSebastian Reichel int status; 5600c79378cSSebastian Reichel 5610c79378cSSebastian Reichel status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); 5620c79378cSSebastian Reichel if (status) { 5630c79378cSSebastian Reichel dev_err(dev, "chipselect %d already in use\n", 5640c79378cSSebastian Reichel spi->chip_select); 5650c79378cSSebastian Reichel return status; 5660c79378cSSebastian Reichel } 5670c79378cSSebastian Reichel 5680c79378cSSebastian Reichel /* Controller may unregister concurrently */ 5690c79378cSSebastian Reichel if (IS_ENABLED(CONFIG_SPI_DYNAMIC) && 5700c79378cSSebastian Reichel !device_is_registered(&ctlr->dev)) { 5710c79378cSSebastian Reichel return -ENODEV; 5720c79378cSSebastian Reichel } 5730c79378cSSebastian Reichel 5740c79378cSSebastian Reichel /* Descriptors take precedence */ 5750c79378cSSebastian Reichel if (ctlr->cs_gpiods) 5760c79378cSSebastian Reichel spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; 5770c79378cSSebastian Reichel else if (ctlr->cs_gpios) 5780c79378cSSebastian Reichel spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; 5790c79378cSSebastian Reichel 5800c79378cSSebastian Reichel /* Drivers may modify this initial i/o setup, but will 5810c79378cSSebastian Reichel * normally rely on the device being setup. Devices 5820c79378cSSebastian Reichel * using SPI_CS_HIGH can't coexist well otherwise... 5830c79378cSSebastian Reichel */ 5840c79378cSSebastian Reichel status = spi_setup(spi); 5850c79378cSSebastian Reichel if (status < 0) { 5860c79378cSSebastian Reichel dev_err(dev, "can't setup %s, status %d\n", 5870c79378cSSebastian Reichel dev_name(&spi->dev), status); 5880c79378cSSebastian Reichel return status; 5890c79378cSSebastian Reichel } 5900c79378cSSebastian Reichel 5910c79378cSSebastian Reichel /* Device may be bound to an active driver when this returns */ 5920c79378cSSebastian Reichel status = device_add(&spi->dev); 5930c79378cSSebastian Reichel if (status < 0) { 5940c79378cSSebastian Reichel dev_err(dev, "can't add %s, status %d\n", 5950c79378cSSebastian Reichel dev_name(&spi->dev), status); 5960c79378cSSebastian Reichel spi_cleanup(spi); 5970c79378cSSebastian Reichel } else { 5980c79378cSSebastian Reichel dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 5990c79378cSSebastian Reichel } 6000c79378cSSebastian Reichel 6010c79378cSSebastian Reichel return status; 6020c79378cSSebastian Reichel } 6030c79378cSSebastian Reichel 604dc87c98eSGrant Likely /** 605dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 606dc87c98eSGrant Likely * @spi: spi_device to register 607dc87c98eSGrant Likely * 608dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 609dc87c98eSGrant Likely * spi_alloc_device can be added onto the spi bus with this function. 610dc87c98eSGrant Likely * 61197d56dc6SJavier Martinez Canillas * Return: 0 on success; negative errno on failure 612dc87c98eSGrant Likely */ 613dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi) 614dc87c98eSGrant Likely { 6158caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 6168caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 617dc87c98eSGrant Likely int status; 618dc87c98eSGrant Likely 619dc87c98eSGrant Likely /* Chipselects are numbered 0..max; validate. */ 6208caab75fSGeert Uytterhoeven if (spi->chip_select >= ctlr->num_chipselect) { 6218caab75fSGeert Uytterhoeven dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 6228caab75fSGeert Uytterhoeven ctlr->num_chipselect); 623dc87c98eSGrant Likely return -EINVAL; 624dc87c98eSGrant Likely } 625dc87c98eSGrant Likely 626dc87c98eSGrant Likely /* Set the bus ID string */ 627e13ac47bSJarkko Nikula spi_dev_set_name(spi); 628e48880e0SDavid Brownell 629e48880e0SDavid Brownell /* We need to make sure there's no other device with this 630e48880e0SDavid Brownell * chipselect **BEFORE** we call setup(), else we'll trash 631e48880e0SDavid Brownell * its configuration. Lock against concurrent add() calls. 632e48880e0SDavid Brownell */ 6336098475dSMark Brown mutex_lock(&ctlr->add_lock); 6340c79378cSSebastian Reichel status = __spi_add_device(spi); 6356098475dSMark Brown mutex_unlock(&ctlr->add_lock); 636e48880e0SDavid Brownell return status; 637dc87c98eSGrant Likely } 638dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device); 6398ae12a0dSDavid Brownell 6400c79378cSSebastian Reichel static int spi_add_device_locked(struct spi_device *spi) 6410c79378cSSebastian Reichel { 6420c79378cSSebastian Reichel struct spi_controller *ctlr = spi->controller; 6430c79378cSSebastian Reichel struct device *dev = ctlr->dev.parent; 6440c79378cSSebastian Reichel 6450c79378cSSebastian Reichel /* Chipselects are numbered 0..max; validate. */ 6460c79378cSSebastian Reichel if (spi->chip_select >= ctlr->num_chipselect) { 6470c79378cSSebastian Reichel dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 6480c79378cSSebastian Reichel ctlr->num_chipselect); 6490c79378cSSebastian Reichel return -EINVAL; 6500c79378cSSebastian Reichel } 6510c79378cSSebastian Reichel 6520c79378cSSebastian Reichel /* Set the bus ID string */ 6530c79378cSSebastian Reichel spi_dev_set_name(spi); 6540c79378cSSebastian Reichel 6556098475dSMark Brown WARN_ON(!mutex_is_locked(&ctlr->add_lock)); 6560c79378cSSebastian Reichel return __spi_add_device(spi); 6570c79378cSSebastian Reichel } 6580c79378cSSebastian Reichel 65933e34dc6SDavid Brownell /** 66033e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 6618caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 66233e34dc6SDavid Brownell * @chip: Describes the SPI device 66333e34dc6SDavid Brownell * Context: can sleep 66433e34dc6SDavid Brownell * 66533e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 6668ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 6678ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 6688ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 6698ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 670082c8cb4SDavid Brownell * 67197d56dc6SJavier Martinez Canillas * Return: the new device, or NULL. 6728ae12a0dSDavid Brownell */ 6738caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr, 674e9d5a461SAdrian Bunk struct spi_board_info *chip) 6758ae12a0dSDavid Brownell { 6768ae12a0dSDavid Brownell struct spi_device *proxy; 6778ae12a0dSDavid Brownell int status; 6788ae12a0dSDavid Brownell 679082c8cb4SDavid Brownell /* NOTE: caller did any chip->bus_num checks necessary. 680082c8cb4SDavid Brownell * 681082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 682082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 683082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 684082c8cb4SDavid Brownell */ 685082c8cb4SDavid Brownell 6868caab75fSGeert Uytterhoeven proxy = spi_alloc_device(ctlr); 687dc87c98eSGrant Likely if (!proxy) 6888ae12a0dSDavid Brownell return NULL; 6898ae12a0dSDavid Brownell 690102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 691102eb975SGrant Likely 6928ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 6938ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 694980a01c9SDavid Brownell proxy->mode = chip->mode; 6958ae12a0dSDavid Brownell proxy->irq = chip->irq; 696102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 6978ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 6988ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 6998ae12a0dSDavid Brownell proxy->controller_state = NULL; 7008ae12a0dSDavid Brownell 70147afc77bSHeikki Krogerus if (chip->swnode) { 70247afc77bSHeikki Krogerus status = device_add_software_node(&proxy->dev, chip->swnode); 703826cf175SDmitry Torokhov if (status) { 7049d902c2aSColin Ian King dev_err(&ctlr->dev, "failed to add software node to '%s': %d\n", 705826cf175SDmitry Torokhov chip->modalias, status); 706826cf175SDmitry Torokhov goto err_dev_put; 707826cf175SDmitry Torokhov } 7088ae12a0dSDavid Brownell } 709dc87c98eSGrant Likely 710826cf175SDmitry Torokhov status = spi_add_device(proxy); 711826cf175SDmitry Torokhov if (status < 0) 712df41a5daSHeikki Krogerus goto err_dev_put; 713826cf175SDmitry Torokhov 714dc87c98eSGrant Likely return proxy; 715826cf175SDmitry Torokhov 716826cf175SDmitry Torokhov err_dev_put: 717df41a5daSHeikki Krogerus device_remove_software_node(&proxy->dev); 718826cf175SDmitry Torokhov spi_dev_put(proxy); 719826cf175SDmitry Torokhov return NULL; 720dc87c98eSGrant Likely } 7218ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 7228ae12a0dSDavid Brownell 7233b1884c2SGeert Uytterhoeven /** 7243b1884c2SGeert Uytterhoeven * spi_unregister_device - unregister a single SPI device 7253b1884c2SGeert Uytterhoeven * @spi: spi_device to unregister 7263b1884c2SGeert Uytterhoeven * 7273b1884c2SGeert Uytterhoeven * Start making the passed SPI device vanish. Normally this would be handled 7288caab75fSGeert Uytterhoeven * by spi_unregister_controller(). 7293b1884c2SGeert Uytterhoeven */ 7303b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi) 7313b1884c2SGeert Uytterhoeven { 732bd6c1644SGeert Uytterhoeven if (!spi) 733bd6c1644SGeert Uytterhoeven return; 734bd6c1644SGeert Uytterhoeven 7358324147fSJohan Hovold if (spi->dev.of_node) { 736bd6c1644SGeert Uytterhoeven of_node_clear_flag(spi->dev.of_node, OF_POPULATED); 7378324147fSJohan Hovold of_node_put(spi->dev.of_node); 7388324147fSJohan Hovold } 7397f24467fSOctavian Purdila if (ACPI_COMPANION(&spi->dev)) 7407f24467fSOctavian Purdila acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); 74147afc77bSHeikki Krogerus device_remove_software_node(&spi->dev); 74227e7db56SSaravana Kannan device_del(&spi->dev); 74327e7db56SSaravana Kannan spi_cleanup(spi); 74427e7db56SSaravana Kannan put_device(&spi->dev); 7453b1884c2SGeert Uytterhoeven } 7463b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device); 7473b1884c2SGeert Uytterhoeven 7488caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, 7492b9603a0SFeng Tang struct spi_board_info *bi) 7502b9603a0SFeng Tang { 7512b9603a0SFeng Tang struct spi_device *dev; 7522b9603a0SFeng Tang 7538caab75fSGeert Uytterhoeven if (ctlr->bus_num != bi->bus_num) 7542b9603a0SFeng Tang return; 7552b9603a0SFeng Tang 7568caab75fSGeert Uytterhoeven dev = spi_new_device(ctlr, bi); 7572b9603a0SFeng Tang if (!dev) 7588caab75fSGeert Uytterhoeven dev_err(ctlr->dev.parent, "can't create new device for %s\n", 7592b9603a0SFeng Tang bi->modalias); 7602b9603a0SFeng Tang } 7612b9603a0SFeng Tang 76233e34dc6SDavid Brownell /** 76333e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 76433e34dc6SDavid Brownell * @info: array of chip descriptors 76533e34dc6SDavid Brownell * @n: how many descriptors are provided 76633e34dc6SDavid Brownell * Context: can sleep 76733e34dc6SDavid Brownell * 7688ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 7698ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 7708ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 7718ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 7728ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 7738ae12a0dSDavid Brownell * 7748ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 7758ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 7768ae12a0dSDavid Brownell * would naturally declare its SPI devices. 7778ae12a0dSDavid Brownell * 7788ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 7798ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 78097d56dc6SJavier Martinez Canillas * 78197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 7828ae12a0dSDavid Brownell */ 783fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n) 7848ae12a0dSDavid Brownell { 7858ae12a0dSDavid Brownell struct boardinfo *bi; 7862b9603a0SFeng Tang int i; 7878ae12a0dSDavid Brownell 788c7908a37SXiubo Li if (!n) 789f974cf57SDmitry Torokhov return 0; 790c7908a37SXiubo Li 791f9bdb7fdSMarkus Elfring bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); 7928ae12a0dSDavid Brownell if (!bi) 7938ae12a0dSDavid Brownell return -ENOMEM; 7948ae12a0dSDavid Brownell 7952b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 7968caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 7972b9603a0SFeng Tang 7982b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 799826cf175SDmitry Torokhov 80094040828SMatthias Kaehlcke mutex_lock(&board_lock); 8018ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 8028caab75fSGeert Uytterhoeven list_for_each_entry(ctlr, &spi_controller_list, list) 8038caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, 8048caab75fSGeert Uytterhoeven &bi->board_info); 80594040828SMatthias Kaehlcke mutex_unlock(&board_lock); 8062b9603a0SFeng Tang } 8072b9603a0SFeng Tang 8088ae12a0dSDavid Brownell return 0; 8098ae12a0dSDavid Brownell } 8108ae12a0dSDavid Brownell 8118ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 8128ae12a0dSDavid Brownell 813d347b4aaSDavid Bauer static void spi_set_cs(struct spi_device *spi, bool enable, bool force) 814b158935fSMark Brown { 81586527bcbSAndy Shevchenko bool activate = enable; 81625093bdeSAlexandru Ardelean 817d40f0b6fSDouglas Anderson /* 818d40f0b6fSDouglas Anderson * Avoid calling into the driver (or doing delays) if the chip select 819d40f0b6fSDouglas Anderson * isn't actually changing from the last time this was called. 820d40f0b6fSDouglas Anderson */ 821d347b4aaSDavid Bauer if (!force && (spi->controller->last_cs_enable == enable) && 822d40f0b6fSDouglas Anderson (spi->controller->last_cs_mode_high == (spi->mode & SPI_CS_HIGH))) 823d40f0b6fSDouglas Anderson return; 824d40f0b6fSDouglas Anderson 8255cb4e1f3SAndy Shevchenko trace_spi_set_cs(spi, activate); 8265cb4e1f3SAndy Shevchenko 827d40f0b6fSDouglas Anderson spi->controller->last_cs_enable = enable; 828d40f0b6fSDouglas Anderson spi->controller->last_cs_mode_high = spi->mode & SPI_CS_HIGH; 829d40f0b6fSDouglas Anderson 8300486d9f9Sleilk.liu if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio) || 8310486d9f9Sleilk.liu !spi->controller->set_cs_timing) { 83286527bcbSAndy Shevchenko if (activate) 8338c33ebfeSMason Zhang spi_delay_exec(&spi->cs_setup, NULL); 83425093bdeSAlexandru Ardelean else 8358c33ebfeSMason Zhang spi_delay_exec(&spi->cs_hold, NULL); 83625093bdeSAlexandru Ardelean } 83725093bdeSAlexandru Ardelean 838b158935fSMark Brown if (spi->mode & SPI_CS_HIGH) 839b158935fSMark Brown enable = !enable; 840b158935fSMark Brown 841f3186dd8SLinus Walleij if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { 842f3186dd8SLinus Walleij if (!(spi->mode & SPI_NO_CS)) { 8436b695469SAndy Shevchenko if (spi->cs_gpiod) { 8446b695469SAndy Shevchenko /* 8456b695469SAndy Shevchenko * Historically ACPI has no means of the GPIO polarity and 8466b695469SAndy Shevchenko * thus the SPISerialBus() resource defines it on the per-chip 8476b695469SAndy Shevchenko * basis. In order to avoid a chain of negations, the GPIO 8486b695469SAndy Shevchenko * polarity is considered being Active High. Even for the cases 8496b695469SAndy Shevchenko * when _DSD() is involved (in the updated versions of ACPI) 8506b695469SAndy Shevchenko * the GPIO CS polarity must be defined Active High to avoid 8516b695469SAndy Shevchenko * ambiguity. That's why we use enable, that takes SPI_CS_HIGH 8526b695469SAndy Shevchenko * into account. 8536b695469SAndy Shevchenko */ 8546b695469SAndy Shevchenko if (has_acpi_companion(&spi->dev)) 8556b695469SAndy Shevchenko gpiod_set_value_cansleep(spi->cs_gpiod, !enable); 856f3186dd8SLinus Walleij else 8576b695469SAndy Shevchenko /* Polarity handled by GPIO library */ 8586b695469SAndy Shevchenko gpiod_set_value_cansleep(spi->cs_gpiod, activate); 8596b695469SAndy Shevchenko } else { 860766c6b63SSven Van Asbroeck /* 861766c6b63SSven Van Asbroeck * invert the enable line, as active low is 862766c6b63SSven Van Asbroeck * default for SPI. 863766c6b63SSven Van Asbroeck */ 86428f7604fSFelix Fietkau gpio_set_value_cansleep(spi->cs_gpio, !enable); 865f3186dd8SLinus Walleij } 8666b695469SAndy Shevchenko } 8678eee6b9dSThor Thayer /* Some SPI masters need both GPIO CS & slave_select */ 8688caab75fSGeert Uytterhoeven if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && 8698caab75fSGeert Uytterhoeven spi->controller->set_cs) 8708caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 8718caab75fSGeert Uytterhoeven } else if (spi->controller->set_cs) { 8728caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 8738eee6b9dSThor Thayer } 87425093bdeSAlexandru Ardelean 8750486d9f9Sleilk.liu if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio) || 8760486d9f9Sleilk.liu !spi->controller->set_cs_timing) { 87786527bcbSAndy Shevchenko if (!activate) 8788c33ebfeSMason Zhang spi_delay_exec(&spi->cs_inactive, NULL); 87925093bdeSAlexandru Ardelean } 880b158935fSMark Brown } 881b158935fSMark Brown 8822de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA 88346336966SBoris Brezillon int spi_map_buf(struct spi_controller *ctlr, struct device *dev, 8846ad45a27SMark Brown struct sg_table *sgt, void *buf, size_t len, 8856ad45a27SMark Brown enum dma_data_direction dir) 8866ad45a27SMark Brown { 8876ad45a27SMark Brown const bool vmalloced_buf = is_vmalloc_addr(buf); 888df88e91bSAndy Shevchenko unsigned int max_seg_size = dma_get_max_seg_size(dev); 889b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM 890b1b8153cSVignesh R const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && 891b1b8153cSVignesh R (unsigned long)buf < (PKMAP_BASE + 892b1b8153cSVignesh R (LAST_PKMAP * PAGE_SIZE))); 893b1b8153cSVignesh R #else 894b1b8153cSVignesh R const bool kmap_buf = false; 895b1b8153cSVignesh R #endif 89665598c13SAndrew Gabbasov int desc_len; 89765598c13SAndrew Gabbasov int sgs; 8986ad45a27SMark Brown struct page *vm_page; 8998dd4a016SJuan Gutierrez struct scatterlist *sg; 9006ad45a27SMark Brown void *sg_buf; 9016ad45a27SMark Brown size_t min; 9026ad45a27SMark Brown int i, ret; 9036ad45a27SMark Brown 904b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 905df88e91bSAndy Shevchenko desc_len = min_t(int, max_seg_size, PAGE_SIZE); 90665598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); 9070569a88fSVignesh R } else if (virt_addr_valid(buf)) { 9088caab75fSGeert Uytterhoeven desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); 90965598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len, desc_len); 9100569a88fSVignesh R } else { 9110569a88fSVignesh R return -EINVAL; 91265598c13SAndrew Gabbasov } 91365598c13SAndrew Gabbasov 9146ad45a27SMark Brown ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); 9156ad45a27SMark Brown if (ret != 0) 9166ad45a27SMark Brown return ret; 9176ad45a27SMark Brown 9188dd4a016SJuan Gutierrez sg = &sgt->sgl[0]; 9196ad45a27SMark Brown for (i = 0; i < sgs; i++) { 9206ad45a27SMark Brown 921b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 922ce99319aSMaxime Chevallier /* 923ce99319aSMaxime Chevallier * Next scatterlist entry size is the minimum between 924ce99319aSMaxime Chevallier * the desc_len and the remaining buffer length that 925ce99319aSMaxime Chevallier * fits in a page. 926ce99319aSMaxime Chevallier */ 927ce99319aSMaxime Chevallier min = min_t(size_t, desc_len, 928ce99319aSMaxime Chevallier min_t(size_t, len, 929ce99319aSMaxime Chevallier PAGE_SIZE - offset_in_page(buf))); 930b1b8153cSVignesh R if (vmalloced_buf) 9316ad45a27SMark Brown vm_page = vmalloc_to_page(buf); 932b1b8153cSVignesh R else 933b1b8153cSVignesh R vm_page = kmap_to_page(buf); 9346ad45a27SMark Brown if (!vm_page) { 9356ad45a27SMark Brown sg_free_table(sgt); 9366ad45a27SMark Brown return -ENOMEM; 9376ad45a27SMark Brown } 9388dd4a016SJuan Gutierrez sg_set_page(sg, vm_page, 939c1aefbddSCharles Keepax min, offset_in_page(buf)); 9406ad45a27SMark Brown } else { 94165598c13SAndrew Gabbasov min = min_t(size_t, len, desc_len); 9426ad45a27SMark Brown sg_buf = buf; 9438dd4a016SJuan Gutierrez sg_set_buf(sg, sg_buf, min); 9446ad45a27SMark Brown } 9456ad45a27SMark Brown 9466ad45a27SMark Brown buf += min; 9476ad45a27SMark Brown len -= min; 9488dd4a016SJuan Gutierrez sg = sg_next(sg); 9496ad45a27SMark Brown } 9506ad45a27SMark Brown 9516ad45a27SMark Brown ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); 95289e4b66aSGeert Uytterhoeven if (!ret) 95389e4b66aSGeert Uytterhoeven ret = -ENOMEM; 9546ad45a27SMark Brown if (ret < 0) { 9556ad45a27SMark Brown sg_free_table(sgt); 9566ad45a27SMark Brown return ret; 9576ad45a27SMark Brown } 9586ad45a27SMark Brown 9596ad45a27SMark Brown sgt->nents = ret; 9606ad45a27SMark Brown 9616ad45a27SMark Brown return 0; 9626ad45a27SMark Brown } 9636ad45a27SMark Brown 96446336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, 9656ad45a27SMark Brown struct sg_table *sgt, enum dma_data_direction dir) 9666ad45a27SMark Brown { 9676ad45a27SMark Brown if (sgt->orig_nents) { 9686ad45a27SMark Brown dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); 9696ad45a27SMark Brown sg_free_table(sgt); 9706ad45a27SMark Brown } 9716ad45a27SMark Brown } 9726ad45a27SMark Brown 9738caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 97499adef31SMark Brown { 97599adef31SMark Brown struct device *tx_dev, *rx_dev; 97699adef31SMark Brown struct spi_transfer *xfer; 9776ad45a27SMark Brown int ret; 9783a2eba9bSMark Brown 9798caab75fSGeert Uytterhoeven if (!ctlr->can_dma) 98099adef31SMark Brown return 0; 98199adef31SMark Brown 9828caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 9838caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 984b470e10eSVinod Koul else if (ctlr->dma_map_dev) 985b470e10eSVinod Koul tx_dev = ctlr->dma_map_dev; 986c37f45b5SLeilk Liu else 9878caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 988c37f45b5SLeilk Liu 9898caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 9908caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 991b470e10eSVinod Koul else if (ctlr->dma_map_dev) 992b470e10eSVinod Koul rx_dev = ctlr->dma_map_dev; 993c37f45b5SLeilk Liu else 9948caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 99599adef31SMark Brown 99699adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9978caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 99899adef31SMark Brown continue; 99999adef31SMark Brown 100099adef31SMark Brown if (xfer->tx_buf != NULL) { 10018caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, 10026ad45a27SMark Brown (void *)xfer->tx_buf, xfer->len, 100399adef31SMark Brown DMA_TO_DEVICE); 10046ad45a27SMark Brown if (ret != 0) 10056ad45a27SMark Brown return ret; 100699adef31SMark Brown } 100799adef31SMark Brown 100899adef31SMark Brown if (xfer->rx_buf != NULL) { 10098caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, 101099adef31SMark Brown xfer->rx_buf, xfer->len, 101199adef31SMark Brown DMA_FROM_DEVICE); 10126ad45a27SMark Brown if (ret != 0) { 10138caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, 10146ad45a27SMark Brown DMA_TO_DEVICE); 10156ad45a27SMark Brown return ret; 101699adef31SMark Brown } 101799adef31SMark Brown } 101899adef31SMark Brown } 101999adef31SMark Brown 10208caab75fSGeert Uytterhoeven ctlr->cur_msg_mapped = true; 102199adef31SMark Brown 102299adef31SMark Brown return 0; 102399adef31SMark Brown } 102499adef31SMark Brown 10258caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg) 102699adef31SMark Brown { 102799adef31SMark Brown struct spi_transfer *xfer; 102899adef31SMark Brown struct device *tx_dev, *rx_dev; 102999adef31SMark Brown 10308caab75fSGeert Uytterhoeven if (!ctlr->cur_msg_mapped || !ctlr->can_dma) 103199adef31SMark Brown return 0; 103299adef31SMark Brown 10338caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 10348caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 1035c37f45b5SLeilk Liu else 10368caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 1037c37f45b5SLeilk Liu 10388caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 10398caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 1040c37f45b5SLeilk Liu else 10418caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 104299adef31SMark Brown 104399adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 10448caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 104599adef31SMark Brown continue; 104699adef31SMark Brown 10478caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 10488caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 104999adef31SMark Brown } 105099adef31SMark Brown 1051809b1b04SRobin Gong ctlr->cur_msg_mapped = false; 1052809b1b04SRobin Gong 105399adef31SMark Brown return 0; 105499adef31SMark Brown } 10552de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */ 10568caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr, 10572de440f5SGeert Uytterhoeven struct spi_message *msg) 10582de440f5SGeert Uytterhoeven { 10592de440f5SGeert Uytterhoeven return 0; 10602de440f5SGeert Uytterhoeven } 10612de440f5SGeert Uytterhoeven 10628caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr, 10632de440f5SGeert Uytterhoeven struct spi_message *msg) 10642de440f5SGeert Uytterhoeven { 10652de440f5SGeert Uytterhoeven return 0; 10662de440f5SGeert Uytterhoeven } 10672de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */ 10682de440f5SGeert Uytterhoeven 10698caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr, 10704b786458SMartin Sperl struct spi_message *msg) 10714b786458SMartin Sperl { 10724b786458SMartin Sperl struct spi_transfer *xfer; 10734b786458SMartin Sperl 10744b786458SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 10754b786458SMartin Sperl /* 10764b786458SMartin Sperl * Restore the original value of tx_buf or rx_buf if they are 10774b786458SMartin Sperl * NULL. 10784b786458SMartin Sperl */ 10798caab75fSGeert Uytterhoeven if (xfer->tx_buf == ctlr->dummy_tx) 10804b786458SMartin Sperl xfer->tx_buf = NULL; 10818caab75fSGeert Uytterhoeven if (xfer->rx_buf == ctlr->dummy_rx) 10824b786458SMartin Sperl xfer->rx_buf = NULL; 10834b786458SMartin Sperl } 10844b786458SMartin Sperl 10858caab75fSGeert Uytterhoeven return __spi_unmap_msg(ctlr, msg); 10864b786458SMartin Sperl } 10874b786458SMartin Sperl 10888caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 10892de440f5SGeert Uytterhoeven { 10902de440f5SGeert Uytterhoeven struct spi_transfer *xfer; 10912de440f5SGeert Uytterhoeven void *tmp; 10922de440f5SGeert Uytterhoeven unsigned int max_tx, max_rx; 10932de440f5SGeert Uytterhoeven 1094aee67fe8Sdillon min if ((ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) 1095aee67fe8Sdillon min && !(msg->spi->mode & SPI_3WIRE)) { 10962de440f5SGeert Uytterhoeven max_tx = 0; 10972de440f5SGeert Uytterhoeven max_rx = 0; 10982de440f5SGeert Uytterhoeven 10992de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, transfer_list) { 11008caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && 11012de440f5SGeert Uytterhoeven !xfer->tx_buf) 11022de440f5SGeert Uytterhoeven max_tx = max(xfer->len, max_tx); 11038caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && 11042de440f5SGeert Uytterhoeven !xfer->rx_buf) 11052de440f5SGeert Uytterhoeven max_rx = max(xfer->len, max_rx); 11062de440f5SGeert Uytterhoeven } 11072de440f5SGeert Uytterhoeven 11082de440f5SGeert Uytterhoeven if (max_tx) { 11098caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_tx, max_tx, 11102de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 11112de440f5SGeert Uytterhoeven if (!tmp) 11122de440f5SGeert Uytterhoeven return -ENOMEM; 11138caab75fSGeert Uytterhoeven ctlr->dummy_tx = tmp; 11142de440f5SGeert Uytterhoeven memset(tmp, 0, max_tx); 11152de440f5SGeert Uytterhoeven } 11162de440f5SGeert Uytterhoeven 11172de440f5SGeert Uytterhoeven if (max_rx) { 11188caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_rx, max_rx, 11192de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 11202de440f5SGeert Uytterhoeven if (!tmp) 11212de440f5SGeert Uytterhoeven return -ENOMEM; 11228caab75fSGeert Uytterhoeven ctlr->dummy_rx = tmp; 11232de440f5SGeert Uytterhoeven } 11242de440f5SGeert Uytterhoeven 11252de440f5SGeert Uytterhoeven if (max_tx || max_rx) { 11262de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, 11272de440f5SGeert Uytterhoeven transfer_list) { 11285442dcaaSChris Lesiak if (!xfer->len) 11295442dcaaSChris Lesiak continue; 11302de440f5SGeert Uytterhoeven if (!xfer->tx_buf) 11318caab75fSGeert Uytterhoeven xfer->tx_buf = ctlr->dummy_tx; 11322de440f5SGeert Uytterhoeven if (!xfer->rx_buf) 11338caab75fSGeert Uytterhoeven xfer->rx_buf = ctlr->dummy_rx; 11342de440f5SGeert Uytterhoeven } 11352de440f5SGeert Uytterhoeven } 11362de440f5SGeert Uytterhoeven } 11372de440f5SGeert Uytterhoeven 11388caab75fSGeert Uytterhoeven return __spi_map_msg(ctlr, msg); 11392de440f5SGeert Uytterhoeven } 114099adef31SMark Brown 1141810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr, 1142810923f3SLubomir Rintel struct spi_message *msg, 1143810923f3SLubomir Rintel struct spi_transfer *xfer) 1144810923f3SLubomir Rintel { 1145810923f3SLubomir Rintel struct spi_statistics *statm = &ctlr->statistics; 1146810923f3SLubomir Rintel struct spi_statistics *stats = &msg->spi->statistics; 11476170d077SXu Yilun u32 speed_hz = xfer->speed_hz; 114849686df5SColin Ian King unsigned long long ms; 1149810923f3SLubomir Rintel 1150810923f3SLubomir Rintel if (spi_controller_is_slave(ctlr)) { 1151810923f3SLubomir Rintel if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { 1152810923f3SLubomir Rintel dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); 1153810923f3SLubomir Rintel return -EINTR; 1154810923f3SLubomir Rintel } 1155810923f3SLubomir Rintel } else { 11566170d077SXu Yilun if (!speed_hz) 11576170d077SXu Yilun speed_hz = 100000; 11586170d077SXu Yilun 115986b8bff7SAndy Shevchenko /* 116086b8bff7SAndy Shevchenko * For each byte we wait for 8 cycles of the SPI clock. 116186b8bff7SAndy Shevchenko * Since speed is defined in Hz and we want milliseconds, 116286b8bff7SAndy Shevchenko * use respective multiplier, but before the division, 116386b8bff7SAndy Shevchenko * otherwise we may get 0 for short transfers. 116486b8bff7SAndy Shevchenko */ 116586b8bff7SAndy Shevchenko ms = 8LL * MSEC_PER_SEC * xfer->len; 11666170d077SXu Yilun do_div(ms, speed_hz); 1167810923f3SLubomir Rintel 116886b8bff7SAndy Shevchenko /* 116986b8bff7SAndy Shevchenko * Increase it twice and add 200 ms tolerance, use 117086b8bff7SAndy Shevchenko * predefined maximum in case of overflow. 117186b8bff7SAndy Shevchenko */ 117286b8bff7SAndy Shevchenko ms += ms + 200; 1173810923f3SLubomir Rintel if (ms > UINT_MAX) 1174810923f3SLubomir Rintel ms = UINT_MAX; 1175810923f3SLubomir Rintel 1176810923f3SLubomir Rintel ms = wait_for_completion_timeout(&ctlr->xfer_completion, 1177810923f3SLubomir Rintel msecs_to_jiffies(ms)); 1178810923f3SLubomir Rintel 1179810923f3SLubomir Rintel if (ms == 0) { 1180810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(statm, timedout); 1181810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(stats, timedout); 1182810923f3SLubomir Rintel dev_err(&msg->spi->dev, 1183810923f3SLubomir Rintel "SPI transfer timed out\n"); 1184810923f3SLubomir Rintel return -ETIMEDOUT; 1185810923f3SLubomir Rintel } 1186810923f3SLubomir Rintel } 1187810923f3SLubomir Rintel 1188810923f3SLubomir Rintel return 0; 1189810923f3SLubomir Rintel } 1190810923f3SLubomir Rintel 11910ff2de8bSMartin Sperl static void _spi_transfer_delay_ns(u32 ns) 11920ff2de8bSMartin Sperl { 11930ff2de8bSMartin Sperl if (!ns) 11940ff2de8bSMartin Sperl return; 119586b8bff7SAndy Shevchenko if (ns <= NSEC_PER_USEC) { 11960ff2de8bSMartin Sperl ndelay(ns); 11970ff2de8bSMartin Sperl } else { 119886b8bff7SAndy Shevchenko u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC); 11990ff2de8bSMartin Sperl 12000ff2de8bSMartin Sperl if (us <= 10) 12010ff2de8bSMartin Sperl udelay(us); 12020ff2de8bSMartin Sperl else 12030ff2de8bSMartin Sperl usleep_range(us, us + DIV_ROUND_UP(us, 10)); 12040ff2de8bSMartin Sperl } 12050ff2de8bSMartin Sperl } 12060ff2de8bSMartin Sperl 12073984d39bSAlexandru Ardelean int spi_delay_to_ns(struct spi_delay *_delay, struct spi_transfer *xfer) 12080ff2de8bSMartin Sperl { 1209b2c98153SAlexandru Ardelean u32 delay = _delay->value; 1210b2c98153SAlexandru Ardelean u32 unit = _delay->unit; 1211d5864e5bSMartin Sperl u32 hz; 12120ff2de8bSMartin Sperl 1213b2c98153SAlexandru Ardelean if (!delay) 1214b2c98153SAlexandru Ardelean return 0; 12150ff2de8bSMartin Sperl 12160ff2de8bSMartin Sperl switch (unit) { 12170ff2de8bSMartin Sperl case SPI_DELAY_UNIT_USECS: 121886b8bff7SAndy Shevchenko delay *= NSEC_PER_USEC; 12190ff2de8bSMartin Sperl break; 122086b8bff7SAndy Shevchenko case SPI_DELAY_UNIT_NSECS: 122186b8bff7SAndy Shevchenko /* Nothing to do here */ 12220ff2de8bSMartin Sperl break; 1223d5864e5bSMartin Sperl case SPI_DELAY_UNIT_SCK: 1224b2c98153SAlexandru Ardelean /* clock cycles need to be obtained from spi_transfer */ 1225b2c98153SAlexandru Ardelean if (!xfer) 1226b2c98153SAlexandru Ardelean return -EINVAL; 122786b8bff7SAndy Shevchenko /* 122886b8bff7SAndy Shevchenko * If there is unknown effective speed, approximate it 122986b8bff7SAndy Shevchenko * by underestimating with half of the requested hz. 1230d5864e5bSMartin Sperl */ 1231d5864e5bSMartin Sperl hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2; 1232b2c98153SAlexandru Ardelean if (!hz) 1233b2c98153SAlexandru Ardelean return -EINVAL; 123486b8bff7SAndy Shevchenko 123586b8bff7SAndy Shevchenko /* Convert delay to nanoseconds */ 123686b8bff7SAndy Shevchenko delay *= DIV_ROUND_UP(NSEC_PER_SEC, hz); 1237d5864e5bSMartin Sperl break; 12380ff2de8bSMartin Sperl default: 1239b2c98153SAlexandru Ardelean return -EINVAL; 1240b2c98153SAlexandru Ardelean } 1241b2c98153SAlexandru Ardelean 1242b2c98153SAlexandru Ardelean return delay; 1243b2c98153SAlexandru Ardelean } 12443984d39bSAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_to_ns); 1245b2c98153SAlexandru Ardelean 1246b2c98153SAlexandru Ardelean int spi_delay_exec(struct spi_delay *_delay, struct spi_transfer *xfer) 1247b2c98153SAlexandru Ardelean { 1248b2c98153SAlexandru Ardelean int delay; 1249b2c98153SAlexandru Ardelean 12508fede89fSMark Brown might_sleep(); 12518fede89fSMark Brown 1252b2c98153SAlexandru Ardelean if (!_delay) 1253b2c98153SAlexandru Ardelean return -EINVAL; 1254b2c98153SAlexandru Ardelean 12553984d39bSAlexandru Ardelean delay = spi_delay_to_ns(_delay, xfer); 1256b2c98153SAlexandru Ardelean if (delay < 0) 1257b2c98153SAlexandru Ardelean return delay; 1258b2c98153SAlexandru Ardelean 1259b2c98153SAlexandru Ardelean _spi_transfer_delay_ns(delay); 1260b2c98153SAlexandru Ardelean 1261b2c98153SAlexandru Ardelean return 0; 1262b2c98153SAlexandru Ardelean } 1263b2c98153SAlexandru Ardelean EXPORT_SYMBOL_GPL(spi_delay_exec); 1264b2c98153SAlexandru Ardelean 12650ff2de8bSMartin Sperl static void _spi_transfer_cs_change_delay(struct spi_message *msg, 12660ff2de8bSMartin Sperl struct spi_transfer *xfer) 12670ff2de8bSMartin Sperl { 126886b8bff7SAndy Shevchenko u32 default_delay_ns = 10 * NSEC_PER_USEC; 1269329f0dacSAlexandru Ardelean u32 delay = xfer->cs_change_delay.value; 1270329f0dacSAlexandru Ardelean u32 unit = xfer->cs_change_delay.unit; 1271329f0dacSAlexandru Ardelean int ret; 12720ff2de8bSMartin Sperl 12730ff2de8bSMartin Sperl /* return early on "fast" mode - for everything but USECS */ 12746b3f236aSAlexandru Ardelean if (!delay) { 12756b3f236aSAlexandru Ardelean if (unit == SPI_DELAY_UNIT_USECS) 127686b8bff7SAndy Shevchenko _spi_transfer_delay_ns(default_delay_ns); 12770ff2de8bSMartin Sperl return; 12786b3f236aSAlexandru Ardelean } 12790ff2de8bSMartin Sperl 1280329f0dacSAlexandru Ardelean ret = spi_delay_exec(&xfer->cs_change_delay, xfer); 1281329f0dacSAlexandru Ardelean if (ret) { 12820ff2de8bSMartin Sperl dev_err_once(&msg->spi->dev, 128386b8bff7SAndy Shevchenko "Use of unsupported delay unit %i, using default of %luus\n", 128486b8bff7SAndy Shevchenko unit, default_delay_ns / NSEC_PER_USEC); 128586b8bff7SAndy Shevchenko _spi_transfer_delay_ns(default_delay_ns); 12860ff2de8bSMartin Sperl } 12870ff2de8bSMartin Sperl } 12880ff2de8bSMartin Sperl 1289b158935fSMark Brown /* 1290b158935fSMark Brown * spi_transfer_one_message - Default implementation of transfer_one_message() 1291b158935fSMark Brown * 1292b158935fSMark Brown * This is a standard implementation of transfer_one_message() for 12938ba811a7SMoritz Fischer * drivers which implement a transfer_one() operation. It provides 1294b158935fSMark Brown * standard handling of delays and chip select management. 1295b158935fSMark Brown */ 12968caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr, 1297b158935fSMark Brown struct spi_message *msg) 1298b158935fSMark Brown { 1299b158935fSMark Brown struct spi_transfer *xfer; 1300b158935fSMark Brown bool keep_cs = false; 1301b158935fSMark Brown int ret = 0; 13028caab75fSGeert Uytterhoeven struct spi_statistics *statm = &ctlr->statistics; 1303eca2ebc7SMartin Sperl struct spi_statistics *stats = &msg->spi->statistics; 1304b158935fSMark Brown 1305d347b4aaSDavid Bauer spi_set_cs(msg->spi, true, false); 1306b158935fSMark Brown 1307eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, messages); 1308eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, messages); 1309eca2ebc7SMartin Sperl 1310b158935fSMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1311b158935fSMark Brown trace_spi_transfer_start(msg, xfer); 1312b158935fSMark Brown 13138caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(statm, xfer, ctlr); 13148caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(stats, xfer, ctlr); 1315eca2ebc7SMartin Sperl 1316b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 1317b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 1318b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1319b42faeeeSVladimir Oltean } 1320b42faeeeSVladimir Oltean 1321b3063203SNicolas Saenz Julienne if ((xfer->tx_buf || xfer->rx_buf) && xfer->len) { 13228caab75fSGeert Uytterhoeven reinit_completion(&ctlr->xfer_completion); 1323b158935fSMark Brown 1324809b1b04SRobin Gong fallback_pio: 13258caab75fSGeert Uytterhoeven ret = ctlr->transfer_one(ctlr, msg->spi, xfer); 1326b158935fSMark Brown if (ret < 0) { 1327809b1b04SRobin Gong if (ctlr->cur_msg_mapped && 1328809b1b04SRobin Gong (xfer->error & SPI_TRANS_FAIL_NO_START)) { 1329809b1b04SRobin Gong __spi_unmap_msg(ctlr, msg); 1330809b1b04SRobin Gong ctlr->fallback = true; 1331809b1b04SRobin Gong xfer->error &= ~SPI_TRANS_FAIL_NO_START; 1332809b1b04SRobin Gong goto fallback_pio; 1333809b1b04SRobin Gong } 1334809b1b04SRobin Gong 1335eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, 1336eca2ebc7SMartin Sperl errors); 1337eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, 1338eca2ebc7SMartin Sperl errors); 1339b158935fSMark Brown dev_err(&msg->spi->dev, 1340b158935fSMark Brown "SPI transfer failed: %d\n", ret); 1341b158935fSMark Brown goto out; 1342b158935fSMark Brown } 1343b158935fSMark Brown 1344d57e7960SMark Brown if (ret > 0) { 1345810923f3SLubomir Rintel ret = spi_transfer_wait(ctlr, msg, xfer); 1346810923f3SLubomir Rintel if (ret < 0) 1347810923f3SLubomir Rintel msg->status = ret; 1348d57e7960SMark Brown } 134938ec10f6SMark Brown } else { 135038ec10f6SMark Brown if (xfer->len) 135138ec10f6SMark Brown dev_err(&msg->spi->dev, 135238ec10f6SMark Brown "Bufferless transfer has length %u\n", 135338ec10f6SMark Brown xfer->len); 135438ec10f6SMark Brown } 1355b158935fSMark Brown 1356b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 1357b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1358b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 1359b42faeeeSVladimir Oltean } 1360b42faeeeSVladimir Oltean 1361b158935fSMark Brown trace_spi_transfer_stop(msg, xfer); 1362b158935fSMark Brown 1363b158935fSMark Brown if (msg->status != -EINPROGRESS) 1364b158935fSMark Brown goto out; 1365b158935fSMark Brown 1366bebcfd27SAlexandru Ardelean spi_transfer_delay_exec(xfer); 1367b158935fSMark Brown 1368b158935fSMark Brown if (xfer->cs_change) { 1369b158935fSMark Brown if (list_is_last(&xfer->transfer_list, 1370b158935fSMark Brown &msg->transfers)) { 1371b158935fSMark Brown keep_cs = true; 1372b158935fSMark Brown } else { 1373d347b4aaSDavid Bauer spi_set_cs(msg->spi, false, false); 13740ff2de8bSMartin Sperl _spi_transfer_cs_change_delay(msg, xfer); 1375d347b4aaSDavid Bauer spi_set_cs(msg->spi, true, false); 1376b158935fSMark Brown } 1377b158935fSMark Brown } 1378b158935fSMark Brown 1379b158935fSMark Brown msg->actual_length += xfer->len; 1380b158935fSMark Brown } 1381b158935fSMark Brown 1382b158935fSMark Brown out: 1383b158935fSMark Brown if (ret != 0 || !keep_cs) 1384d347b4aaSDavid Bauer spi_set_cs(msg->spi, false, false); 1385b158935fSMark Brown 1386b158935fSMark Brown if (msg->status == -EINPROGRESS) 1387b158935fSMark Brown msg->status = ret; 1388b158935fSMark Brown 13898caab75fSGeert Uytterhoeven if (msg->status && ctlr->handle_err) 13908caab75fSGeert Uytterhoeven ctlr->handle_err(ctlr, msg); 1391b716c4ffSAndy Shevchenko 13920ed56252SMark Brown spi_finalize_current_message(ctlr); 13930ed56252SMark Brown 1394b158935fSMark Brown return ret; 1395b158935fSMark Brown } 1396b158935fSMark Brown 1397b158935fSMark Brown /** 1398b158935fSMark Brown * spi_finalize_current_transfer - report completion of a transfer 13998caab75fSGeert Uytterhoeven * @ctlr: the controller reporting completion 1400b158935fSMark Brown * 1401b158935fSMark Brown * Called by SPI drivers using the core transfer_one_message() 1402b158935fSMark Brown * implementation to notify it that the current interrupt driven 14039e8f4882SGeert Uytterhoeven * transfer has finished and the next one may be scheduled. 1404b158935fSMark Brown */ 14058caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr) 1406b158935fSMark Brown { 14078caab75fSGeert Uytterhoeven complete(&ctlr->xfer_completion); 1408b158935fSMark Brown } 1409b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); 1410b158935fSMark Brown 1411e1268597SMark Brown static void spi_idle_runtime_pm(struct spi_controller *ctlr) 1412e1268597SMark Brown { 1413e1268597SMark Brown if (ctlr->auto_runtime_pm) { 1414e1268597SMark Brown pm_runtime_mark_last_busy(ctlr->dev.parent); 1415e1268597SMark Brown pm_runtime_put_autosuspend(ctlr->dev.parent); 1416e1268597SMark Brown } 1417e1268597SMark Brown } 1418e1268597SMark Brown 1419ffbbdd21SLinus Walleij /** 1420fc9e0f71SMark Brown * __spi_pump_messages - function which processes spi message queue 14218caab75fSGeert Uytterhoeven * @ctlr: controller to process queue for 1422fc9e0f71SMark Brown * @in_kthread: true if we are in the context of the message pump thread 1423ffbbdd21SLinus Walleij * 1424ffbbdd21SLinus Walleij * This function checks if there is any spi message in the queue that 1425ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 1426ffbbdd21SLinus Walleij * and transfer each message. 1427ffbbdd21SLinus Walleij * 14280461a414SMark Brown * Note that it is called both from the kthread itself and also from 14290461a414SMark Brown * inside spi_sync(); the queue extraction handling at the top of the 14300461a414SMark Brown * function should deal with this safely. 1431ffbbdd21SLinus Walleij */ 14328caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1433ffbbdd21SLinus Walleij { 1434b42faeeeSVladimir Oltean struct spi_transfer *xfer; 1435d1c44c93SVladimir Oltean struct spi_message *msg; 1436ffbbdd21SLinus Walleij bool was_busy = false; 1437d1c44c93SVladimir Oltean unsigned long flags; 1438ffbbdd21SLinus Walleij int ret; 1439ffbbdd21SLinus Walleij 1440983aee5dSMark Brown /* Lock queue */ 14418caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1442983aee5dSMark Brown 1443983aee5dSMark Brown /* Make sure we are not already running a message */ 14448caab75fSGeert Uytterhoeven if (ctlr->cur_msg) { 14458caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1446983aee5dSMark Brown return; 1447983aee5dSMark Brown } 1448983aee5dSMark Brown 1449f0125f1aSMark Brown /* If another context is idling the device then defer */ 14508caab75fSGeert Uytterhoeven if (ctlr->idling) { 145160a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 14528caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 14530461a414SMark Brown return; 14540461a414SMark Brown } 14550461a414SMark Brown 1456983aee5dSMark Brown /* Check if the queue is idle */ 14578caab75fSGeert Uytterhoeven if (list_empty(&ctlr->queue) || !ctlr->running) { 14588caab75fSGeert Uytterhoeven if (!ctlr->busy) { 14598caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1460ffbbdd21SLinus Walleij return; 1461ffbbdd21SLinus Walleij } 1462fc9e0f71SMark Brown 1463e1268597SMark Brown /* Defer any non-atomic teardown to the thread */ 1464f0125f1aSMark Brown if (!in_kthread) { 1465e1268597SMark Brown if (!ctlr->dummy_rx && !ctlr->dummy_tx && 1466e1268597SMark Brown !ctlr->unprepare_transfer_hardware) { 1467e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1468e1268597SMark Brown ctlr->busy = false; 1469e1268597SMark Brown trace_spi_controller_idle(ctlr); 1470e1268597SMark Brown } else { 147160a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, 1472f0125f1aSMark Brown &ctlr->pump_messages); 1473e1268597SMark Brown } 1474f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1475f0125f1aSMark Brown return; 1476f0125f1aSMark Brown } 1477f0125f1aSMark Brown 1478f0125f1aSMark Brown ctlr->busy = false; 1479f0125f1aSMark Brown ctlr->idling = true; 1480f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1481f0125f1aSMark Brown 1482f0125f1aSMark Brown kfree(ctlr->dummy_rx); 1483f0125f1aSMark Brown ctlr->dummy_rx = NULL; 1484f0125f1aSMark Brown kfree(ctlr->dummy_tx); 1485f0125f1aSMark Brown ctlr->dummy_tx = NULL; 1486f0125f1aSMark Brown if (ctlr->unprepare_transfer_hardware && 1487f0125f1aSMark Brown ctlr->unprepare_transfer_hardware(ctlr)) 1488f0125f1aSMark Brown dev_err(&ctlr->dev, 1489f0125f1aSMark Brown "failed to unprepare transfer hardware\n"); 1490e1268597SMark Brown spi_idle_runtime_pm(ctlr); 1491f0125f1aSMark Brown trace_spi_controller_idle(ctlr); 1492f0125f1aSMark Brown 1493f0125f1aSMark Brown spin_lock_irqsave(&ctlr->queue_lock, flags); 1494f0125f1aSMark Brown ctlr->idling = false; 14958caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1496ffbbdd21SLinus Walleij return; 1497ffbbdd21SLinus Walleij } 1498ffbbdd21SLinus Walleij 1499ffbbdd21SLinus Walleij /* Extract head of queue */ 1500d1c44c93SVladimir Oltean msg = list_first_entry(&ctlr->queue, struct spi_message, queue); 1501d1c44c93SVladimir Oltean ctlr->cur_msg = msg; 1502ffbbdd21SLinus Walleij 1503d1c44c93SVladimir Oltean list_del_init(&msg->queue); 15048caab75fSGeert Uytterhoeven if (ctlr->busy) 1505ffbbdd21SLinus Walleij was_busy = true; 1506ffbbdd21SLinus Walleij else 15078caab75fSGeert Uytterhoeven ctlr->busy = true; 15088caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1509ffbbdd21SLinus Walleij 15108caab75fSGeert Uytterhoeven mutex_lock(&ctlr->io_mutex); 1511ef4d96ecSMark Brown 15128caab75fSGeert Uytterhoeven if (!was_busy && ctlr->auto_runtime_pm) { 15138caab75fSGeert Uytterhoeven ret = pm_runtime_get_sync(ctlr->dev.parent); 151449834de2SMark Brown if (ret < 0) { 15157e48e23aSTony Lindgren pm_runtime_put_noidle(ctlr->dev.parent); 15168caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "Failed to power device: %d\n", 151749834de2SMark Brown ret); 15188caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 151949834de2SMark Brown return; 152049834de2SMark Brown } 152149834de2SMark Brown } 152249834de2SMark Brown 152356ec1978SMark Brown if (!was_busy) 15248caab75fSGeert Uytterhoeven trace_spi_controller_busy(ctlr); 152556ec1978SMark Brown 15268caab75fSGeert Uytterhoeven if (!was_busy && ctlr->prepare_transfer_hardware) { 15278caab75fSGeert Uytterhoeven ret = ctlr->prepare_transfer_hardware(ctlr); 1528ffbbdd21SLinus Walleij if (ret) { 15298caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 1530f3440d9aSSuper Liu "failed to prepare transfer hardware: %d\n", 1531f3440d9aSSuper Liu ret); 153249834de2SMark Brown 15338caab75fSGeert Uytterhoeven if (ctlr->auto_runtime_pm) 15348caab75fSGeert Uytterhoeven pm_runtime_put(ctlr->dev.parent); 1535f3440d9aSSuper Liu 1536d1c44c93SVladimir Oltean msg->status = ret; 1537f3440d9aSSuper Liu spi_finalize_current_message(ctlr); 1538f3440d9aSSuper Liu 15398caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 1540ffbbdd21SLinus Walleij return; 1541ffbbdd21SLinus Walleij } 1542ffbbdd21SLinus Walleij } 1543ffbbdd21SLinus Walleij 1544d1c44c93SVladimir Oltean trace_spi_message_start(msg); 154556ec1978SMark Brown 15468caab75fSGeert Uytterhoeven if (ctlr->prepare_message) { 1547d1c44c93SVladimir Oltean ret = ctlr->prepare_message(ctlr, msg); 15482841a5fcSMark Brown if (ret) { 15498caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to prepare message: %d\n", 15508caab75fSGeert Uytterhoeven ret); 1551d1c44c93SVladimir Oltean msg->status = ret; 15528caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 155349023d2eSJon Hunter goto out; 15542841a5fcSMark Brown } 15558caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = true; 15562841a5fcSMark Brown } 15572841a5fcSMark Brown 1558d1c44c93SVladimir Oltean ret = spi_map_msg(ctlr, msg); 155999adef31SMark Brown if (ret) { 1560d1c44c93SVladimir Oltean msg->status = ret; 15618caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 156249023d2eSJon Hunter goto out; 156399adef31SMark Brown } 156499adef31SMark Brown 1565b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1566b42faeeeSVladimir Oltean list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1567b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 1568b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1569b42faeeeSVladimir Oltean } 1570b42faeeeSVladimir Oltean } 1571b42faeeeSVladimir Oltean 1572d1c44c93SVladimir Oltean ret = ctlr->transfer_one_message(ctlr, msg); 1573ffbbdd21SLinus Walleij if (ret) { 15748caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 15751f802f82SGeert Uytterhoeven "failed to transfer one message from queue\n"); 157649023d2eSJon Hunter goto out; 1577ffbbdd21SLinus Walleij } 157849023d2eSJon Hunter 157949023d2eSJon Hunter out: 15808caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 158162826970SMark Brown 158262826970SMark Brown /* Prod the scheduler in case transfer_one() was busy waiting */ 158349023d2eSJon Hunter if (!ret) 158462826970SMark Brown cond_resched(); 1585ffbbdd21SLinus Walleij } 1586ffbbdd21SLinus Walleij 1587fc9e0f71SMark Brown /** 1588fc9e0f71SMark Brown * spi_pump_messages - kthread work function which processes spi message queue 15898caab75fSGeert Uytterhoeven * @work: pointer to kthread work struct contained in the controller struct 1590fc9e0f71SMark Brown */ 1591fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work) 1592fc9e0f71SMark Brown { 15938caab75fSGeert Uytterhoeven struct spi_controller *ctlr = 15948caab75fSGeert Uytterhoeven container_of(work, struct spi_controller, pump_messages); 1595fc9e0f71SMark Brown 15968caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, true); 1597fc9e0f71SMark Brown } 1598fc9e0f71SMark Brown 1599924b5867SDouglas Anderson /** 1600b42faeeeSVladimir Oltean * spi_take_timestamp_pre - helper for drivers to collect the beginning of the 1601b42faeeeSVladimir Oltean * TX timestamp for the requested byte from the SPI 1602b42faeeeSVladimir Oltean * transfer. The frequency with which this function 1603b42faeeeSVladimir Oltean * must be called (once per word, once for the whole 1604b42faeeeSVladimir Oltean * transfer, once per batch of words etc) is arbitrary 1605b42faeeeSVladimir Oltean * as long as the @tx buffer offset is greater than or 1606b42faeeeSVladimir Oltean * equal to the requested byte at the time of the 1607b42faeeeSVladimir Oltean * call. The timestamp is only taken once, at the 1608b42faeeeSVladimir Oltean * first such call. It is assumed that the driver 1609b42faeeeSVladimir Oltean * advances its @tx buffer pointer monotonically. 1610b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1611b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1612862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1613b42faeeeSVladimir Oltean * @irqs_off: If true, will disable IRQs and preemption for the duration of the 1614b42faeeeSVladimir Oltean * transfer, for less jitter in time measurement. Only compatible 1615b42faeeeSVladimir Oltean * with PIO drivers. If true, must follow up with 1616b42faeeeSVladimir Oltean * spi_take_timestamp_post or otherwise system will crash. 1617b42faeeeSVladimir Oltean * WARNING: for fully predictable results, the CPU frequency must 1618b42faeeeSVladimir Oltean * also be under control (governor). 1619b42faeeeSVladimir Oltean */ 1620b42faeeeSVladimir Oltean void spi_take_timestamp_pre(struct spi_controller *ctlr, 1621b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1622862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1623b42faeeeSVladimir Oltean { 1624b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1625b42faeeeSVladimir Oltean return; 1626b42faeeeSVladimir Oltean 16276a726824SVladimir Oltean if (xfer->timestamped) 1628b42faeeeSVladimir Oltean return; 1629b42faeeeSVladimir Oltean 16306a726824SVladimir Oltean if (progress > xfer->ptp_sts_word_pre) 1631b42faeeeSVladimir Oltean return; 1632b42faeeeSVladimir Oltean 1633b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1634862dd2a9SVladimir Oltean xfer->ptp_sts_word_pre = progress; 1635b42faeeeSVladimir Oltean 1636b42faeeeSVladimir Oltean if (irqs_off) { 1637b42faeeeSVladimir Oltean local_irq_save(ctlr->irq_flags); 1638b42faeeeSVladimir Oltean preempt_disable(); 1639b42faeeeSVladimir Oltean } 1640b42faeeeSVladimir Oltean 1641b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 1642b42faeeeSVladimir Oltean } 1643b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_pre); 1644b42faeeeSVladimir Oltean 1645b42faeeeSVladimir Oltean /** 1646b42faeeeSVladimir Oltean * spi_take_timestamp_post - helper for drivers to collect the end of the 1647b42faeeeSVladimir Oltean * TX timestamp for the requested byte from the SPI 1648b42faeeeSVladimir Oltean * transfer. Can be called with an arbitrary 1649b42faeeeSVladimir Oltean * frequency: only the first call where @tx exceeds 1650b42faeeeSVladimir Oltean * or is equal to the requested word will be 1651b42faeeeSVladimir Oltean * timestamped. 1652b42faeeeSVladimir Oltean * @ctlr: Pointer to the spi_controller structure of the driver 1653b42faeeeSVladimir Oltean * @xfer: Pointer to the transfer being timestamped 1654862dd2a9SVladimir Oltean * @progress: How many words (not bytes) have been transferred so far 1655b42faeeeSVladimir Oltean * @irqs_off: If true, will re-enable IRQs and preemption for the local CPU. 1656b42faeeeSVladimir Oltean */ 1657b42faeeeSVladimir Oltean void spi_take_timestamp_post(struct spi_controller *ctlr, 1658b42faeeeSVladimir Oltean struct spi_transfer *xfer, 1659862dd2a9SVladimir Oltean size_t progress, bool irqs_off) 1660b42faeeeSVladimir Oltean { 1661b42faeeeSVladimir Oltean if (!xfer->ptp_sts) 1662b42faeeeSVladimir Oltean return; 1663b42faeeeSVladimir Oltean 16646a726824SVladimir Oltean if (xfer->timestamped) 1665b42faeeeSVladimir Oltean return; 1666b42faeeeSVladimir Oltean 1667862dd2a9SVladimir Oltean if (progress < xfer->ptp_sts_word_post) 1668b42faeeeSVladimir Oltean return; 1669b42faeeeSVladimir Oltean 1670b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1671b42faeeeSVladimir Oltean 1672b42faeeeSVladimir Oltean if (irqs_off) { 1673b42faeeeSVladimir Oltean local_irq_restore(ctlr->irq_flags); 1674b42faeeeSVladimir Oltean preempt_enable(); 1675b42faeeeSVladimir Oltean } 1676b42faeeeSVladimir Oltean 1677b42faeeeSVladimir Oltean /* Capture the resolution of the timestamp */ 1678862dd2a9SVladimir Oltean xfer->ptp_sts_word_post = progress; 1679b42faeeeSVladimir Oltean 16806a726824SVladimir Oltean xfer->timestamped = true; 1681b42faeeeSVladimir Oltean } 1682b42faeeeSVladimir Oltean EXPORT_SYMBOL_GPL(spi_take_timestamp_post); 1683b42faeeeSVladimir Oltean 1684b42faeeeSVladimir Oltean /** 1685924b5867SDouglas Anderson * spi_set_thread_rt - set the controller to pump at realtime priority 1686924b5867SDouglas Anderson * @ctlr: controller to boost priority of 1687924b5867SDouglas Anderson * 1688924b5867SDouglas Anderson * This can be called because the controller requested realtime priority 1689924b5867SDouglas Anderson * (by setting the ->rt value before calling spi_register_controller()) or 1690924b5867SDouglas Anderson * because a device on the bus said that its transfers needed realtime 1691924b5867SDouglas Anderson * priority. 1692924b5867SDouglas Anderson * 1693924b5867SDouglas Anderson * NOTE: at the moment if any device on a bus says it needs realtime then 1694924b5867SDouglas Anderson * the thread will be at realtime priority for all transfers on that 1695924b5867SDouglas Anderson * controller. If this eventually becomes a problem we may see if we can 1696924b5867SDouglas Anderson * find a way to boost the priority only temporarily during relevant 1697924b5867SDouglas Anderson * transfers. 1698924b5867SDouglas Anderson */ 1699924b5867SDouglas Anderson static void spi_set_thread_rt(struct spi_controller *ctlr) 1700ffbbdd21SLinus Walleij { 1701924b5867SDouglas Anderson dev_info(&ctlr->dev, 1702924b5867SDouglas Anderson "will run message pump with realtime priority\n"); 17036d2b84a4SLinus Torvalds sched_set_fifo(ctlr->kworker->task); 1704924b5867SDouglas Anderson } 1705924b5867SDouglas Anderson 1706924b5867SDouglas Anderson static int spi_init_queue(struct spi_controller *ctlr) 1707924b5867SDouglas Anderson { 17088caab75fSGeert Uytterhoeven ctlr->running = false; 17098caab75fSGeert Uytterhoeven ctlr->busy = false; 1710ffbbdd21SLinus Walleij 171160a883d1SMarek Szyprowski ctlr->kworker = kthread_create_worker(0, dev_name(&ctlr->dev)); 171260a883d1SMarek Szyprowski if (IS_ERR(ctlr->kworker)) { 171360a883d1SMarek Szyprowski dev_err(&ctlr->dev, "failed to create message pump kworker\n"); 171460a883d1SMarek Szyprowski return PTR_ERR(ctlr->kworker); 1715ffbbdd21SLinus Walleij } 171660a883d1SMarek Szyprowski 17178caab75fSGeert Uytterhoeven kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1718f0125f1aSMark Brown 1719ffbbdd21SLinus Walleij /* 17208caab75fSGeert Uytterhoeven * Controller config will indicate if this controller should run the 1721ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 1722ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 1723ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 1724ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 1725ffbbdd21SLinus Walleij */ 1726924b5867SDouglas Anderson if (ctlr->rt) 1727924b5867SDouglas Anderson spi_set_thread_rt(ctlr); 1728ffbbdd21SLinus Walleij 1729ffbbdd21SLinus Walleij return 0; 1730ffbbdd21SLinus Walleij } 1731ffbbdd21SLinus Walleij 1732ffbbdd21SLinus Walleij /** 1733ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 1734ffbbdd21SLinus Walleij * messages 17358caab75fSGeert Uytterhoeven * @ctlr: the controller to check for queued messages 1736ffbbdd21SLinus Walleij * 1737ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 1738ffbbdd21SLinus Walleij * this call. 173997d56dc6SJavier Martinez Canillas * 174097d56dc6SJavier Martinez Canillas * Return: the next message in the queue, else NULL if the queue is empty. 1741ffbbdd21SLinus Walleij */ 17428caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1743ffbbdd21SLinus Walleij { 1744ffbbdd21SLinus Walleij struct spi_message *next; 1745ffbbdd21SLinus Walleij unsigned long flags; 1746ffbbdd21SLinus Walleij 1747ffbbdd21SLinus Walleij /* get a pointer to the next message, if any */ 17488caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 17498caab75fSGeert Uytterhoeven next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 17501cfd97f9SAxel Lin queue); 17518caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1752ffbbdd21SLinus Walleij 1753ffbbdd21SLinus Walleij return next; 1754ffbbdd21SLinus Walleij } 1755ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1756ffbbdd21SLinus Walleij 1757ffbbdd21SLinus Walleij /** 1758ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 17598caab75fSGeert Uytterhoeven * @ctlr: the controller to return the message to 1760ffbbdd21SLinus Walleij * 1761ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 1762ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 1763ffbbdd21SLinus Walleij */ 17648caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr) 1765ffbbdd21SLinus Walleij { 1766b42faeeeSVladimir Oltean struct spi_transfer *xfer; 1767ffbbdd21SLinus Walleij struct spi_message *mesg; 1768ffbbdd21SLinus Walleij unsigned long flags; 17692841a5fcSMark Brown int ret; 1770ffbbdd21SLinus Walleij 17718caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 17728caab75fSGeert Uytterhoeven mesg = ctlr->cur_msg; 17738caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1774ffbbdd21SLinus Walleij 1775b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported && !ctlr->transfer_one) { 1776b42faeeeSVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) { 1777b42faeeeSVladimir Oltean ptp_read_system_postts(xfer->ptp_sts); 1778b42faeeeSVladimir Oltean xfer->ptp_sts_word_post = xfer->len; 1779b42faeeeSVladimir Oltean } 1780b42faeeeSVladimir Oltean } 1781b42faeeeSVladimir Oltean 17826a726824SVladimir Oltean if (unlikely(ctlr->ptp_sts_supported)) 17836a726824SVladimir Oltean list_for_each_entry(xfer, &mesg->transfers, transfer_list) 17846a726824SVladimir Oltean WARN_ON_ONCE(xfer->ptp_sts && !xfer->timestamped); 1785f971a207SVladimir Oltean 17868caab75fSGeert Uytterhoeven spi_unmap_msg(ctlr, mesg); 178799adef31SMark Brown 1788b59a7ca1SGustav Wiklander /* In the prepare_messages callback the spi bus has the opportunity to 1789b59a7ca1SGustav Wiklander * split a transfer to smaller chunks. 1790b59a7ca1SGustav Wiklander * Release splited transfers here since spi_map_msg is done on the 1791b59a7ca1SGustav Wiklander * splited transfers. 1792b59a7ca1SGustav Wiklander */ 1793b59a7ca1SGustav Wiklander spi_res_release(ctlr, mesg); 1794b59a7ca1SGustav Wiklander 17958caab75fSGeert Uytterhoeven if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { 17968caab75fSGeert Uytterhoeven ret = ctlr->unprepare_message(ctlr, mesg); 17972841a5fcSMark Brown if (ret) { 17988caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 17998caab75fSGeert Uytterhoeven ret); 18002841a5fcSMark Brown } 18012841a5fcSMark Brown } 1802391949b6SUwe Kleine-König 18038caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 18048caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 18058caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = false; 1806809b1b04SRobin Gong ctlr->fallback = false; 180760a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 18088caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 18098e76ef88SMartin Sperl 18108e76ef88SMartin Sperl trace_spi_message_done(mesg); 18112841a5fcSMark Brown 1812ffbbdd21SLinus Walleij mesg->state = NULL; 1813ffbbdd21SLinus Walleij if (mesg->complete) 1814ffbbdd21SLinus Walleij mesg->complete(mesg->context); 1815ffbbdd21SLinus Walleij } 1816ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 1817ffbbdd21SLinus Walleij 18188caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr) 1819ffbbdd21SLinus Walleij { 1820ffbbdd21SLinus Walleij unsigned long flags; 1821ffbbdd21SLinus Walleij 18228caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1823ffbbdd21SLinus Walleij 18248caab75fSGeert Uytterhoeven if (ctlr->running || ctlr->busy) { 18258caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1826ffbbdd21SLinus Walleij return -EBUSY; 1827ffbbdd21SLinus Walleij } 1828ffbbdd21SLinus Walleij 18298caab75fSGeert Uytterhoeven ctlr->running = true; 18308caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 18318caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1832ffbbdd21SLinus Walleij 183360a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1834ffbbdd21SLinus Walleij 1835ffbbdd21SLinus Walleij return 0; 1836ffbbdd21SLinus Walleij } 1837ffbbdd21SLinus Walleij 18388caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr) 1839ffbbdd21SLinus Walleij { 1840ffbbdd21SLinus Walleij unsigned long flags; 1841ffbbdd21SLinus Walleij unsigned limit = 500; 1842ffbbdd21SLinus Walleij int ret = 0; 1843ffbbdd21SLinus Walleij 18448caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1845ffbbdd21SLinus Walleij 1846ffbbdd21SLinus Walleij /* 1847ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 18488caab75fSGeert Uytterhoeven * A wait_queue on the ctlr->busy could be used, but then the common 1849ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 1850ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 1851ffbbdd21SLinus Walleij */ 18528caab75fSGeert Uytterhoeven while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 18538caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1854f97b26b0SAxel Lin usleep_range(10000, 11000); 18558caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1856ffbbdd21SLinus Walleij } 1857ffbbdd21SLinus Walleij 18588caab75fSGeert Uytterhoeven if (!list_empty(&ctlr->queue) || ctlr->busy) 1859ffbbdd21SLinus Walleij ret = -EBUSY; 1860ffbbdd21SLinus Walleij else 18618caab75fSGeert Uytterhoeven ctlr->running = false; 1862ffbbdd21SLinus Walleij 18638caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1864ffbbdd21SLinus Walleij 1865ffbbdd21SLinus Walleij if (ret) { 18668caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "could not stop message queue\n"); 1867ffbbdd21SLinus Walleij return ret; 1868ffbbdd21SLinus Walleij } 1869ffbbdd21SLinus Walleij return ret; 1870ffbbdd21SLinus Walleij } 1871ffbbdd21SLinus Walleij 18728caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr) 1873ffbbdd21SLinus Walleij { 1874ffbbdd21SLinus Walleij int ret; 1875ffbbdd21SLinus Walleij 18768caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 1877ffbbdd21SLinus Walleij 1878ffbbdd21SLinus Walleij /* 18793989144fSPetr Mladek * kthread_flush_worker will block until all work is done. 1880ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 1881ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 1882ffbbdd21SLinus Walleij * return anyway. 1883ffbbdd21SLinus Walleij */ 1884ffbbdd21SLinus Walleij if (ret) { 18858caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem destroying queue\n"); 1886ffbbdd21SLinus Walleij return ret; 1887ffbbdd21SLinus Walleij } 1888ffbbdd21SLinus Walleij 188960a883d1SMarek Szyprowski kthread_destroy_worker(ctlr->kworker); 1890ffbbdd21SLinus Walleij 1891ffbbdd21SLinus Walleij return 0; 1892ffbbdd21SLinus Walleij } 1893ffbbdd21SLinus Walleij 18940461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi, 18950461a414SMark Brown struct spi_message *msg, 18960461a414SMark Brown bool need_pump) 1897ffbbdd21SLinus Walleij { 18988caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 1899ffbbdd21SLinus Walleij unsigned long flags; 1900ffbbdd21SLinus Walleij 19018caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1902ffbbdd21SLinus Walleij 19038caab75fSGeert Uytterhoeven if (!ctlr->running) { 19048caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1905ffbbdd21SLinus Walleij return -ESHUTDOWN; 1906ffbbdd21SLinus Walleij } 1907ffbbdd21SLinus Walleij msg->actual_length = 0; 1908ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 1909ffbbdd21SLinus Walleij 19108caab75fSGeert Uytterhoeven list_add_tail(&msg->queue, &ctlr->queue); 1911f0125f1aSMark Brown if (!ctlr->busy && need_pump) 191260a883d1SMarek Szyprowski kthread_queue_work(ctlr->kworker, &ctlr->pump_messages); 1913ffbbdd21SLinus Walleij 19148caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1915ffbbdd21SLinus Walleij return 0; 1916ffbbdd21SLinus Walleij } 1917ffbbdd21SLinus Walleij 19180461a414SMark Brown /** 19190461a414SMark Brown * spi_queued_transfer - transfer function for queued transfers 19200461a414SMark Brown * @spi: spi device which is requesting transfer 19210461a414SMark Brown * @msg: spi message which is to handled is queued to driver queue 192297d56dc6SJavier Martinez Canillas * 192397d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 19240461a414SMark Brown */ 19250461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 19260461a414SMark Brown { 19270461a414SMark Brown return __spi_queued_transfer(spi, msg, true); 19280461a414SMark Brown } 19290461a414SMark Brown 19308caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr) 1931ffbbdd21SLinus Walleij { 1932ffbbdd21SLinus Walleij int ret; 1933ffbbdd21SLinus Walleij 19348caab75fSGeert Uytterhoeven ctlr->transfer = spi_queued_transfer; 19358caab75fSGeert Uytterhoeven if (!ctlr->transfer_one_message) 19368caab75fSGeert Uytterhoeven ctlr->transfer_one_message = spi_transfer_one_message; 1937ffbbdd21SLinus Walleij 1938ffbbdd21SLinus Walleij /* Initialize and start queue */ 19398caab75fSGeert Uytterhoeven ret = spi_init_queue(ctlr); 1940ffbbdd21SLinus Walleij if (ret) { 19418caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem initializing queue\n"); 1942ffbbdd21SLinus Walleij goto err_init_queue; 1943ffbbdd21SLinus Walleij } 19448caab75fSGeert Uytterhoeven ctlr->queued = true; 19458caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 1946ffbbdd21SLinus Walleij if (ret) { 19478caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem starting queue\n"); 1948ffbbdd21SLinus Walleij goto err_start_queue; 1949ffbbdd21SLinus Walleij } 1950ffbbdd21SLinus Walleij 1951ffbbdd21SLinus Walleij return 0; 1952ffbbdd21SLinus Walleij 1953ffbbdd21SLinus Walleij err_start_queue: 19548caab75fSGeert Uytterhoeven spi_destroy_queue(ctlr); 1955c3676d5cSMark Brown err_init_queue: 1956ffbbdd21SLinus Walleij return ret; 1957ffbbdd21SLinus Walleij } 1958ffbbdd21SLinus Walleij 1959988f259bSBoris Brezillon /** 1960988f259bSBoris Brezillon * spi_flush_queue - Send all pending messages in the queue from the callers' 1961988f259bSBoris Brezillon * context 1962988f259bSBoris Brezillon * @ctlr: controller to process queue for 1963988f259bSBoris Brezillon * 1964988f259bSBoris Brezillon * This should be used when one wants to ensure all pending messages have been 1965988f259bSBoris Brezillon * sent before doing something. Is used by the spi-mem code to make sure SPI 1966988f259bSBoris Brezillon * memory operations do not preempt regular SPI transfers that have been queued 1967988f259bSBoris Brezillon * before the spi-mem operation. 1968988f259bSBoris Brezillon */ 1969988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr) 1970988f259bSBoris Brezillon { 1971988f259bSBoris Brezillon if (ctlr->transfer == spi_queued_transfer) 1972988f259bSBoris Brezillon __spi_pump_messages(ctlr, false); 1973988f259bSBoris Brezillon } 1974988f259bSBoris Brezillon 1975ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 1976ffbbdd21SLinus Walleij 19777cb94361SAndreas Larsson #if defined(CONFIG_OF) 19788caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 1979c2e51ac3SGeert Uytterhoeven struct device_node *nc) 1980d57a4282SGrant Likely { 198189da4293STrent Piepho u32 value; 1982c2e51ac3SGeert Uytterhoeven int rc; 1983d57a4282SGrant Likely 1984d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 1985e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpha")) 1986d57a4282SGrant Likely spi->mode |= SPI_CPHA; 1987e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpol")) 1988d57a4282SGrant Likely spi->mode |= SPI_CPOL; 1989e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-3wire")) 1990c20151dfSLars-Peter Clausen spi->mode |= SPI_3WIRE; 1991e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-lsb-first")) 1992cd6339e6SZhao Qiang spi->mode |= SPI_LSB_FIRST; 19933e5ec1dbSGregory CLEMENT if (of_property_read_bool(nc, "spi-cs-high")) 1994f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 1995f3186dd8SLinus Walleij 1996f477b7fbSwangyuhang /* Device DUAL/QUAD mode */ 199789da4293STrent Piepho if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 199889da4293STrent Piepho switch (value) { 1999d962608cSDragos Bogdan case 0: 2000d962608cSDragos Bogdan spi->mode |= SPI_NO_TX; 2001d962608cSDragos Bogdan break; 200289da4293STrent Piepho case 1: 2003f477b7fbSwangyuhang break; 200489da4293STrent Piepho case 2: 2005f477b7fbSwangyuhang spi->mode |= SPI_TX_DUAL; 2006f477b7fbSwangyuhang break; 200789da4293STrent Piepho case 4: 2008f477b7fbSwangyuhang spi->mode |= SPI_TX_QUAD; 2009f477b7fbSwangyuhang break; 20106b03061fSYogesh Narayan Gaur case 8: 20116b03061fSYogesh Narayan Gaur spi->mode |= SPI_TX_OCTAL; 20126b03061fSYogesh Narayan Gaur break; 2013f477b7fbSwangyuhang default: 20148caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 2015a110f93dSwangyuhang "spi-tx-bus-width %d not supported\n", 201689da4293STrent Piepho value); 201780874d8cSGeert Uytterhoeven break; 2018f477b7fbSwangyuhang } 2019a822e99cSMark Brown } 2020f477b7fbSwangyuhang 202189da4293STrent Piepho if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 202289da4293STrent Piepho switch (value) { 2023d962608cSDragos Bogdan case 0: 2024d962608cSDragos Bogdan spi->mode |= SPI_NO_RX; 2025d962608cSDragos Bogdan break; 202689da4293STrent Piepho case 1: 2027f477b7fbSwangyuhang break; 202889da4293STrent Piepho case 2: 2029f477b7fbSwangyuhang spi->mode |= SPI_RX_DUAL; 2030f477b7fbSwangyuhang break; 203189da4293STrent Piepho case 4: 2032f477b7fbSwangyuhang spi->mode |= SPI_RX_QUAD; 2033f477b7fbSwangyuhang break; 20346b03061fSYogesh Narayan Gaur case 8: 20356b03061fSYogesh Narayan Gaur spi->mode |= SPI_RX_OCTAL; 20366b03061fSYogesh Narayan Gaur break; 2037f477b7fbSwangyuhang default: 20388caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 2039a110f93dSwangyuhang "spi-rx-bus-width %d not supported\n", 204089da4293STrent Piepho value); 204180874d8cSGeert Uytterhoeven break; 2042f477b7fbSwangyuhang } 2043a822e99cSMark Brown } 2044f477b7fbSwangyuhang 20458caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr)) { 2046194276b0SRob Herring if (!of_node_name_eq(nc, "slave")) { 204725c56c88SRob Herring dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 204825c56c88SRob Herring nc); 20496c364062SGeert Uytterhoeven return -EINVAL; 20506c364062SGeert Uytterhoeven } 20516c364062SGeert Uytterhoeven return 0; 20526c364062SGeert Uytterhoeven } 20536c364062SGeert Uytterhoeven 20546c364062SGeert Uytterhoeven /* Device address */ 20556c364062SGeert Uytterhoeven rc = of_property_read_u32(nc, "reg", &value); 20566c364062SGeert Uytterhoeven if (rc) { 205725c56c88SRob Herring dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 205825c56c88SRob Herring nc, rc); 20596c364062SGeert Uytterhoeven return rc; 20606c364062SGeert Uytterhoeven } 20616c364062SGeert Uytterhoeven spi->chip_select = value; 20626c364062SGeert Uytterhoeven 2063d57a4282SGrant Likely /* Device speed */ 2064671c3bf5SChuanhong Guo if (!of_property_read_u32(nc, "spi-max-frequency", &value)) 206589da4293STrent Piepho spi->max_speed_hz = value; 2066d57a4282SGrant Likely 2067c2e51ac3SGeert Uytterhoeven return 0; 2068c2e51ac3SGeert Uytterhoeven } 2069c2e51ac3SGeert Uytterhoeven 2070c2e51ac3SGeert Uytterhoeven static struct spi_device * 20718caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 2072c2e51ac3SGeert Uytterhoeven { 2073c2e51ac3SGeert Uytterhoeven struct spi_device *spi; 2074c2e51ac3SGeert Uytterhoeven int rc; 2075c2e51ac3SGeert Uytterhoeven 2076c2e51ac3SGeert Uytterhoeven /* Alloc an spi_device */ 20778caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 2078c2e51ac3SGeert Uytterhoeven if (!spi) { 207925c56c88SRob Herring dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 2080c2e51ac3SGeert Uytterhoeven rc = -ENOMEM; 2081c2e51ac3SGeert Uytterhoeven goto err_out; 2082c2e51ac3SGeert Uytterhoeven } 2083c2e51ac3SGeert Uytterhoeven 2084c2e51ac3SGeert Uytterhoeven /* Select device driver */ 2085c2e51ac3SGeert Uytterhoeven rc = of_modalias_node(nc, spi->modalias, 2086c2e51ac3SGeert Uytterhoeven sizeof(spi->modalias)); 2087c2e51ac3SGeert Uytterhoeven if (rc < 0) { 208825c56c88SRob Herring dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 2089c2e51ac3SGeert Uytterhoeven goto err_out; 2090c2e51ac3SGeert Uytterhoeven } 2091c2e51ac3SGeert Uytterhoeven 20928caab75fSGeert Uytterhoeven rc = of_spi_parse_dt(ctlr, spi, nc); 2093c2e51ac3SGeert Uytterhoeven if (rc) 2094c2e51ac3SGeert Uytterhoeven goto err_out; 2095c2e51ac3SGeert Uytterhoeven 2096d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 2097d57a4282SGrant Likely of_node_get(nc); 2098d57a4282SGrant Likely spi->dev.of_node = nc; 20990e793ba7SCharles Keepax spi->dev.fwnode = of_fwnode_handle(nc); 2100d57a4282SGrant Likely 2101d57a4282SGrant Likely /* Register the new device */ 2102d57a4282SGrant Likely rc = spi_add_device(spi); 2103d57a4282SGrant Likely if (rc) { 210425c56c88SRob Herring dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 21058324147fSJohan Hovold goto err_of_node_put; 2106d57a4282SGrant Likely } 2107d57a4282SGrant Likely 2108aff5e3f8SPantelis Antoniou return spi; 2109aff5e3f8SPantelis Antoniou 21108324147fSJohan Hovold err_of_node_put: 21118324147fSJohan Hovold of_node_put(nc); 2112aff5e3f8SPantelis Antoniou err_out: 2113aff5e3f8SPantelis Antoniou spi_dev_put(spi); 2114aff5e3f8SPantelis Antoniou return ERR_PTR(rc); 2115aff5e3f8SPantelis Antoniou } 2116aff5e3f8SPantelis Antoniou 2117aff5e3f8SPantelis Antoniou /** 2118aff5e3f8SPantelis Antoniou * of_register_spi_devices() - Register child devices onto the SPI bus 21198caab75fSGeert Uytterhoeven * @ctlr: Pointer to spi_controller device 2120aff5e3f8SPantelis Antoniou * 21216c364062SGeert Uytterhoeven * Registers an spi_device for each child node of controller node which 21226c364062SGeert Uytterhoeven * represents a valid SPI slave. 2123aff5e3f8SPantelis Antoniou */ 21248caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) 2125aff5e3f8SPantelis Antoniou { 2126aff5e3f8SPantelis Antoniou struct spi_device *spi; 2127aff5e3f8SPantelis Antoniou struct device_node *nc; 2128aff5e3f8SPantelis Antoniou 21298caab75fSGeert Uytterhoeven if (!ctlr->dev.of_node) 2130aff5e3f8SPantelis Antoniou return; 2131aff5e3f8SPantelis Antoniou 21328caab75fSGeert Uytterhoeven for_each_available_child_of_node(ctlr->dev.of_node, nc) { 2133bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(nc, OF_POPULATED)) 2134bd6c1644SGeert Uytterhoeven continue; 21358caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, nc); 2136e0af98a7SRalf Ramsauer if (IS_ERR(spi)) { 21378caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 213825c56c88SRob Herring "Failed to create SPI device for %pOF\n", nc); 2139e0af98a7SRalf Ramsauer of_node_clear_flag(nc, OF_POPULATED); 2140e0af98a7SRalf Ramsauer } 2141d57a4282SGrant Likely } 2142d57a4282SGrant Likely } 2143d57a4282SGrant Likely #else 21448caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { } 2145d57a4282SGrant Likely #endif 2146d57a4282SGrant Likely 21470c79378cSSebastian Reichel /** 21480c79378cSSebastian Reichel * spi_new_ancillary_device() - Register ancillary SPI device 21490c79378cSSebastian Reichel * @spi: Pointer to the main SPI device registering the ancillary device 21500c79378cSSebastian Reichel * @chip_select: Chip Select of the ancillary device 21510c79378cSSebastian Reichel * 21520c79378cSSebastian Reichel * Register an ancillary SPI device; for example some chips have a chip-select 21530c79378cSSebastian Reichel * for normal device usage and another one for setup/firmware upload. 21540c79378cSSebastian Reichel * 21550c79378cSSebastian Reichel * This may only be called from main SPI device's probe routine. 21560c79378cSSebastian Reichel * 21570c79378cSSebastian Reichel * Return: 0 on success; negative errno on failure 21580c79378cSSebastian Reichel */ 21590c79378cSSebastian Reichel struct spi_device *spi_new_ancillary_device(struct spi_device *spi, 21600c79378cSSebastian Reichel u8 chip_select) 21610c79378cSSebastian Reichel { 21620c79378cSSebastian Reichel struct spi_device *ancillary; 21630c79378cSSebastian Reichel int rc = 0; 21640c79378cSSebastian Reichel 21650c79378cSSebastian Reichel /* Alloc an spi_device */ 21660c79378cSSebastian Reichel ancillary = spi_alloc_device(spi->controller); 21670c79378cSSebastian Reichel if (!ancillary) { 21680c79378cSSebastian Reichel rc = -ENOMEM; 21690c79378cSSebastian Reichel goto err_out; 21700c79378cSSebastian Reichel } 21710c79378cSSebastian Reichel 21720c79378cSSebastian Reichel strlcpy(ancillary->modalias, "dummy", sizeof(ancillary->modalias)); 21730c79378cSSebastian Reichel 21740c79378cSSebastian Reichel /* Use provided chip-select for ancillary device */ 21750c79378cSSebastian Reichel ancillary->chip_select = chip_select; 21760c79378cSSebastian Reichel 21770c79378cSSebastian Reichel /* Take over SPI mode/speed from SPI main device */ 21780c79378cSSebastian Reichel ancillary->max_speed_hz = spi->max_speed_hz; 2179b01d5506SColin Ian King ancillary->mode = spi->mode; 21800c79378cSSebastian Reichel 21810c79378cSSebastian Reichel /* Register the new device */ 21820c79378cSSebastian Reichel rc = spi_add_device_locked(ancillary); 21830c79378cSSebastian Reichel if (rc) { 21840c79378cSSebastian Reichel dev_err(&spi->dev, "failed to register ancillary device\n"); 21850c79378cSSebastian Reichel goto err_out; 21860c79378cSSebastian Reichel } 21870c79378cSSebastian Reichel 21880c79378cSSebastian Reichel return ancillary; 21890c79378cSSebastian Reichel 21900c79378cSSebastian Reichel err_out: 21910c79378cSSebastian Reichel spi_dev_put(ancillary); 21920c79378cSSebastian Reichel return ERR_PTR(rc); 21930c79378cSSebastian Reichel } 21940c79378cSSebastian Reichel EXPORT_SYMBOL_GPL(spi_new_ancillary_device); 21950c79378cSSebastian Reichel 219664bee4d2SMika Westerberg #ifdef CONFIG_ACPI 21974c3c5954SArd Biesheuvel struct acpi_spi_lookup { 21984c3c5954SArd Biesheuvel struct spi_controller *ctlr; 21994c3c5954SArd Biesheuvel u32 max_speed_hz; 22004c3c5954SArd Biesheuvel u32 mode; 22014c3c5954SArd Biesheuvel int irq; 22024c3c5954SArd Biesheuvel u8 bits_per_word; 22034c3c5954SArd Biesheuvel u8 chip_select; 22044c3c5954SArd Biesheuvel }; 22054c3c5954SArd Biesheuvel 22064c3c5954SArd Biesheuvel static void acpi_spi_parse_apple_properties(struct acpi_device *dev, 22074c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup) 22088a2e487eSLukas Wunner { 22098a2e487eSLukas Wunner const union acpi_object *obj; 22108a2e487eSLukas Wunner 22118a2e487eSLukas Wunner if (!x86_apple_machine) 22128a2e487eSLukas Wunner return; 22138a2e487eSLukas Wunner 22148a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 22158a2e487eSLukas Wunner && obj->buffer.length >= 4) 22164c3c5954SArd Biesheuvel lookup->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 22178a2e487eSLukas Wunner 22188a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 22198a2e487eSLukas Wunner && obj->buffer.length == 8) 22204c3c5954SArd Biesheuvel lookup->bits_per_word = *(u64 *)obj->buffer.pointer; 22218a2e487eSLukas Wunner 22228a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 22238a2e487eSLukas Wunner && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 22244c3c5954SArd Biesheuvel lookup->mode |= SPI_LSB_FIRST; 22258a2e487eSLukas Wunner 22268a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 22278a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 22284c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 22298a2e487eSLukas Wunner 22308a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 22318a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 22324c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 22338a2e487eSLukas Wunner } 22348a2e487eSLukas Wunner 223564bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 223664bee4d2SMika Westerberg { 22374c3c5954SArd Biesheuvel struct acpi_spi_lookup *lookup = data; 22384c3c5954SArd Biesheuvel struct spi_controller *ctlr = lookup->ctlr; 223964bee4d2SMika Westerberg 224064bee4d2SMika Westerberg if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 224164bee4d2SMika Westerberg struct acpi_resource_spi_serialbus *sb; 22424c3c5954SArd Biesheuvel acpi_handle parent_handle; 22434c3c5954SArd Biesheuvel acpi_status status; 224464bee4d2SMika Westerberg 224564bee4d2SMika Westerberg sb = &ares->data.spi_serial_bus; 224664bee4d2SMika Westerberg if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 22474c3c5954SArd Biesheuvel 22484c3c5954SArd Biesheuvel status = acpi_get_handle(NULL, 22494c3c5954SArd Biesheuvel sb->resource_source.string_ptr, 22504c3c5954SArd Biesheuvel &parent_handle); 22514c3c5954SArd Biesheuvel 2252b5e3cf41SArd Biesheuvel if (ACPI_FAILURE(status) || 22534c3c5954SArd Biesheuvel ACPI_HANDLE(ctlr->dev.parent) != parent_handle) 22544c3c5954SArd Biesheuvel return -ENODEV; 22554c3c5954SArd Biesheuvel 2256a0a90718SMika Westerberg /* 2257a0a90718SMika Westerberg * ACPI DeviceSelection numbering is handled by the 2258a0a90718SMika Westerberg * host controller driver in Windows and can vary 2259a0a90718SMika Westerberg * from driver to driver. In Linux we always expect 2260a0a90718SMika Westerberg * 0 .. max - 1 so we need to ask the driver to 2261a0a90718SMika Westerberg * translate between the two schemes. 2262a0a90718SMika Westerberg */ 22638caab75fSGeert Uytterhoeven if (ctlr->fw_translate_cs) { 22648caab75fSGeert Uytterhoeven int cs = ctlr->fw_translate_cs(ctlr, 2265a0a90718SMika Westerberg sb->device_selection); 2266a0a90718SMika Westerberg if (cs < 0) 2267a0a90718SMika Westerberg return cs; 22684c3c5954SArd Biesheuvel lookup->chip_select = cs; 2269a0a90718SMika Westerberg } else { 22704c3c5954SArd Biesheuvel lookup->chip_select = sb->device_selection; 2271a0a90718SMika Westerberg } 2272a0a90718SMika Westerberg 22734c3c5954SArd Biesheuvel lookup->max_speed_hz = sb->connection_speed; 22740dadde34SAndy Shevchenko lookup->bits_per_word = sb->data_bit_length; 227564bee4d2SMika Westerberg 227664bee4d2SMika Westerberg if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 22774c3c5954SArd Biesheuvel lookup->mode |= SPI_CPHA; 227864bee4d2SMika Westerberg if (sb->clock_polarity == ACPI_SPI_START_HIGH) 22794c3c5954SArd Biesheuvel lookup->mode |= SPI_CPOL; 228064bee4d2SMika Westerberg if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 22814c3c5954SArd Biesheuvel lookup->mode |= SPI_CS_HIGH; 228264bee4d2SMika Westerberg } 22834c3c5954SArd Biesheuvel } else if (lookup->irq < 0) { 228464bee4d2SMika Westerberg struct resource r; 228564bee4d2SMika Westerberg 228664bee4d2SMika Westerberg if (acpi_dev_resource_interrupt(ares, 0, &r)) 22874c3c5954SArd Biesheuvel lookup->irq = r.start; 228864bee4d2SMika Westerberg } 228964bee4d2SMika Westerberg 229064bee4d2SMika Westerberg /* Always tell the ACPI core to skip this resource */ 229164bee4d2SMika Westerberg return 1; 229264bee4d2SMika Westerberg } 229364bee4d2SMika Westerberg 22948caab75fSGeert Uytterhoeven static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 22957f24467fSOctavian Purdila struct acpi_device *adev) 229664bee4d2SMika Westerberg { 22974c3c5954SArd Biesheuvel acpi_handle parent_handle = NULL; 229864bee4d2SMika Westerberg struct list_head resource_list; 2299b28944c6SArd Biesheuvel struct acpi_spi_lookup lookup = {}; 230064bee4d2SMika Westerberg struct spi_device *spi; 230164bee4d2SMika Westerberg int ret; 230264bee4d2SMika Westerberg 23037f24467fSOctavian Purdila if (acpi_bus_get_status(adev) || !adev->status.present || 23047f24467fSOctavian Purdila acpi_device_enumerated(adev)) 230564bee4d2SMika Westerberg return AE_OK; 230664bee4d2SMika Westerberg 23074c3c5954SArd Biesheuvel lookup.ctlr = ctlr; 23084c3c5954SArd Biesheuvel lookup.irq = -1; 23094c3c5954SArd Biesheuvel 23104c3c5954SArd Biesheuvel INIT_LIST_HEAD(&resource_list); 23114c3c5954SArd Biesheuvel ret = acpi_dev_get_resources(adev, &resource_list, 23124c3c5954SArd Biesheuvel acpi_spi_add_resource, &lookup); 23134c3c5954SArd Biesheuvel acpi_dev_free_resource_list(&resource_list); 23144c3c5954SArd Biesheuvel 23154c3c5954SArd Biesheuvel if (ret < 0) 23164c3c5954SArd Biesheuvel /* found SPI in _CRS but it points to another controller */ 23174c3c5954SArd Biesheuvel return AE_OK; 23184c3c5954SArd Biesheuvel 23194c3c5954SArd Biesheuvel if (!lookup.max_speed_hz && 232010e92724SBjorn Helgaas ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) && 23214c3c5954SArd Biesheuvel ACPI_HANDLE(ctlr->dev.parent) == parent_handle) { 23224c3c5954SArd Biesheuvel /* Apple does not use _CRS but nested devices for SPI slaves */ 23234c3c5954SArd Biesheuvel acpi_spi_parse_apple_properties(adev, &lookup); 23244c3c5954SArd Biesheuvel } 23254c3c5954SArd Biesheuvel 23264c3c5954SArd Biesheuvel if (!lookup.max_speed_hz) 23274c3c5954SArd Biesheuvel return AE_OK; 23284c3c5954SArd Biesheuvel 23298caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 233064bee4d2SMika Westerberg if (!spi) { 23318caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", 233264bee4d2SMika Westerberg dev_name(&adev->dev)); 233364bee4d2SMika Westerberg return AE_NO_MEMORY; 233464bee4d2SMika Westerberg } 233564bee4d2SMika Westerberg 2336ea235786SJohn Garry 23377b199811SRafael J. Wysocki ACPI_COMPANION_SET(&spi->dev, adev); 23384c3c5954SArd Biesheuvel spi->max_speed_hz = lookup.max_speed_hz; 2339ea235786SJohn Garry spi->mode |= lookup.mode; 23404c3c5954SArd Biesheuvel spi->irq = lookup.irq; 23414c3c5954SArd Biesheuvel spi->bits_per_word = lookup.bits_per_word; 23424c3c5954SArd Biesheuvel spi->chip_select = lookup.chip_select; 234364bee4d2SMika Westerberg 23440c6543f6SDan O'Donovan acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 23450c6543f6SDan O'Donovan sizeof(spi->modalias)); 23460c6543f6SDan O'Donovan 234733ada67dSChristophe RICARD if (spi->irq < 0) 234833ada67dSChristophe RICARD spi->irq = acpi_dev_gpio_irq_get(adev, 0); 234933ada67dSChristophe RICARD 23507f24467fSOctavian Purdila acpi_device_set_enumerated(adev); 23517f24467fSOctavian Purdila 235233cf00e5SMika Westerberg adev->power.flags.ignore_parent = true; 235364bee4d2SMika Westerberg if (spi_add_device(spi)) { 235433cf00e5SMika Westerberg adev->power.flags.ignore_parent = false; 23558caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 235664bee4d2SMika Westerberg dev_name(&adev->dev)); 235764bee4d2SMika Westerberg spi_dev_put(spi); 235864bee4d2SMika Westerberg } 235964bee4d2SMika Westerberg 236064bee4d2SMika Westerberg return AE_OK; 236164bee4d2SMika Westerberg } 236264bee4d2SMika Westerberg 23637f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 23647f24467fSOctavian Purdila void *data, void **return_value) 23657f24467fSOctavian Purdila { 23668caab75fSGeert Uytterhoeven struct spi_controller *ctlr = data; 23677f24467fSOctavian Purdila struct acpi_device *adev; 23687f24467fSOctavian Purdila 23697f24467fSOctavian Purdila if (acpi_bus_get_device(handle, &adev)) 23707f24467fSOctavian Purdila return AE_OK; 23717f24467fSOctavian Purdila 23728caab75fSGeert Uytterhoeven return acpi_register_spi_device(ctlr, adev); 23737f24467fSOctavian Purdila } 23747f24467fSOctavian Purdila 23754c3c5954SArd Biesheuvel #define SPI_ACPI_ENUMERATE_MAX_DEPTH 32 23764c3c5954SArd Biesheuvel 23778caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr) 237864bee4d2SMika Westerberg { 237964bee4d2SMika Westerberg acpi_status status; 238064bee4d2SMika Westerberg acpi_handle handle; 238164bee4d2SMika Westerberg 23828caab75fSGeert Uytterhoeven handle = ACPI_HANDLE(ctlr->dev.parent); 238364bee4d2SMika Westerberg if (!handle) 238464bee4d2SMika Westerberg return; 238564bee4d2SMika Westerberg 23864c3c5954SArd Biesheuvel status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 23874c3c5954SArd Biesheuvel SPI_ACPI_ENUMERATE_MAX_DEPTH, 23888caab75fSGeert Uytterhoeven acpi_spi_add_device, NULL, ctlr, NULL); 238964bee4d2SMika Westerberg if (ACPI_FAILURE(status)) 23908caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 239164bee4d2SMika Westerberg } 239264bee4d2SMika Westerberg #else 23938caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 239464bee4d2SMika Westerberg #endif /* CONFIG_ACPI */ 239564bee4d2SMika Westerberg 23968caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev) 23978ae12a0dSDavid Brownell { 23988caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 23998ae12a0dSDavid Brownell 24008caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 24018caab75fSGeert Uytterhoeven kfree(ctlr); 24028ae12a0dSDavid Brownell } 24038ae12a0dSDavid Brownell 24048ae12a0dSDavid Brownell static struct class spi_master_class = { 24058ae12a0dSDavid Brownell .name = "spi_master", 24068ae12a0dSDavid Brownell .owner = THIS_MODULE, 24078caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 2408eca2ebc7SMartin Sperl .dev_groups = spi_master_groups, 24098ae12a0dSDavid Brownell }; 24108ae12a0dSDavid Brownell 24116c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE 24126c364062SGeert Uytterhoeven /** 24136c364062SGeert Uytterhoeven * spi_slave_abort - abort the ongoing transfer request on an SPI slave 24146c364062SGeert Uytterhoeven * controller 24156c364062SGeert Uytterhoeven * @spi: device used for the current transfer 24166c364062SGeert Uytterhoeven */ 24176c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi) 24186c364062SGeert Uytterhoeven { 24198caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 24206c364062SGeert Uytterhoeven 24218caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 24228caab75fSGeert Uytterhoeven return ctlr->slave_abort(ctlr); 24236c364062SGeert Uytterhoeven 24246c364062SGeert Uytterhoeven return -ENOTSUPP; 24256c364062SGeert Uytterhoeven } 24266c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort); 24276c364062SGeert Uytterhoeven 24286c364062SGeert Uytterhoeven static int match_true(struct device *dev, void *data) 24296c364062SGeert Uytterhoeven { 24306c364062SGeert Uytterhoeven return 1; 24316c364062SGeert Uytterhoeven } 24326c364062SGeert Uytterhoeven 2433cc8b4659SGeert Uytterhoeven static ssize_t slave_show(struct device *dev, struct device_attribute *attr, 2434cc8b4659SGeert Uytterhoeven char *buf) 24356c364062SGeert Uytterhoeven { 24368caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 24378caab75fSGeert Uytterhoeven dev); 24386c364062SGeert Uytterhoeven struct device *child; 24396c364062SGeert Uytterhoeven 24406c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 24416c364062SGeert Uytterhoeven return sprintf(buf, "%s\n", 24426c364062SGeert Uytterhoeven child ? to_spi_device(child)->modalias : NULL); 24436c364062SGeert Uytterhoeven } 24446c364062SGeert Uytterhoeven 2445cc8b4659SGeert Uytterhoeven static ssize_t slave_store(struct device *dev, struct device_attribute *attr, 2446cc8b4659SGeert Uytterhoeven const char *buf, size_t count) 24476c364062SGeert Uytterhoeven { 24488caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 24498caab75fSGeert Uytterhoeven dev); 24506c364062SGeert Uytterhoeven struct spi_device *spi; 24516c364062SGeert Uytterhoeven struct device *child; 24526c364062SGeert Uytterhoeven char name[32]; 24536c364062SGeert Uytterhoeven int rc; 24546c364062SGeert Uytterhoeven 24556c364062SGeert Uytterhoeven rc = sscanf(buf, "%31s", name); 24566c364062SGeert Uytterhoeven if (rc != 1 || !name[0]) 24576c364062SGeert Uytterhoeven return -EINVAL; 24586c364062SGeert Uytterhoeven 24596c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 24606c364062SGeert Uytterhoeven if (child) { 24616c364062SGeert Uytterhoeven /* Remove registered slave */ 24626c364062SGeert Uytterhoeven device_unregister(child); 24636c364062SGeert Uytterhoeven put_device(child); 24646c364062SGeert Uytterhoeven } 24656c364062SGeert Uytterhoeven 24666c364062SGeert Uytterhoeven if (strcmp(name, "(null)")) { 24676c364062SGeert Uytterhoeven /* Register new slave */ 24686c364062SGeert Uytterhoeven spi = spi_alloc_device(ctlr); 24696c364062SGeert Uytterhoeven if (!spi) 24706c364062SGeert Uytterhoeven return -ENOMEM; 24716c364062SGeert Uytterhoeven 24726c364062SGeert Uytterhoeven strlcpy(spi->modalias, name, sizeof(spi->modalias)); 24736c364062SGeert Uytterhoeven 24746c364062SGeert Uytterhoeven rc = spi_add_device(spi); 24756c364062SGeert Uytterhoeven if (rc) { 24766c364062SGeert Uytterhoeven spi_dev_put(spi); 24776c364062SGeert Uytterhoeven return rc; 24786c364062SGeert Uytterhoeven } 24796c364062SGeert Uytterhoeven } 24806c364062SGeert Uytterhoeven 24816c364062SGeert Uytterhoeven return count; 24826c364062SGeert Uytterhoeven } 24836c364062SGeert Uytterhoeven 2484cc8b4659SGeert Uytterhoeven static DEVICE_ATTR_RW(slave); 24856c364062SGeert Uytterhoeven 24866c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = { 24876c364062SGeert Uytterhoeven &dev_attr_slave.attr, 24886c364062SGeert Uytterhoeven NULL, 24896c364062SGeert Uytterhoeven }; 24906c364062SGeert Uytterhoeven 24916c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = { 24926c364062SGeert Uytterhoeven .attrs = spi_slave_attrs, 24936c364062SGeert Uytterhoeven }; 24946c364062SGeert Uytterhoeven 24956c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = { 24968caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 24976c364062SGeert Uytterhoeven &spi_slave_group, 24986c364062SGeert Uytterhoeven NULL, 24996c364062SGeert Uytterhoeven }; 25006c364062SGeert Uytterhoeven 25016c364062SGeert Uytterhoeven static struct class spi_slave_class = { 25026c364062SGeert Uytterhoeven .name = "spi_slave", 25036c364062SGeert Uytterhoeven .owner = THIS_MODULE, 25048caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 25056c364062SGeert Uytterhoeven .dev_groups = spi_slave_groups, 25066c364062SGeert Uytterhoeven }; 25076c364062SGeert Uytterhoeven #else 25086c364062SGeert Uytterhoeven extern struct class spi_slave_class; /* dummy */ 25096c364062SGeert Uytterhoeven #endif 25108ae12a0dSDavid Brownell 25118ae12a0dSDavid Brownell /** 25126c364062SGeert Uytterhoeven * __spi_alloc_controller - allocate an SPI master or slave controller 25138ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 251433e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 2515229e6af1SLukas Wunner * memory is in the driver_data field of the returned device, accessible 2516229e6af1SLukas Wunner * with spi_controller_get_devdata(); the memory is cacheline aligned; 2517229e6af1SLukas Wunner * drivers granting DMA access to portions of their private data need to 2518229e6af1SLukas Wunner * round up @size using ALIGN(size, dma_get_cache_alignment()). 25196c364062SGeert Uytterhoeven * @slave: flag indicating whether to allocate an SPI master (false) or SPI 25206c364062SGeert Uytterhoeven * slave (true) controller 252133e34dc6SDavid Brownell * Context: can sleep 25228ae12a0dSDavid Brownell * 25236c364062SGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 25248ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 25258caab75fSGeert Uytterhoeven * an spi_controller structure, prior to calling spi_register_controller(). 25268ae12a0dSDavid Brownell * 252797d56dc6SJavier Martinez Canillas * This must be called from context that can sleep. 25288ae12a0dSDavid Brownell * 25296c364062SGeert Uytterhoeven * The caller is responsible for assigning the bus number and initializing the 25308caab75fSGeert Uytterhoeven * controller's methods before calling spi_register_controller(); and (after 25318caab75fSGeert Uytterhoeven * errors adding the device) calling spi_controller_put() to prevent a memory 25328caab75fSGeert Uytterhoeven * leak. 253397d56dc6SJavier Martinez Canillas * 25346c364062SGeert Uytterhoeven * Return: the SPI controller structure on success, else NULL. 25358ae12a0dSDavid Brownell */ 25368caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev, 25376c364062SGeert Uytterhoeven unsigned int size, bool slave) 25388ae12a0dSDavid Brownell { 25398caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 2540229e6af1SLukas Wunner size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment()); 25418ae12a0dSDavid Brownell 25420c868461SDavid Brownell if (!dev) 25430c868461SDavid Brownell return NULL; 25440c868461SDavid Brownell 2545229e6af1SLukas Wunner ctlr = kzalloc(size + ctlr_size, GFP_KERNEL); 25468caab75fSGeert Uytterhoeven if (!ctlr) 25478ae12a0dSDavid Brownell return NULL; 25488ae12a0dSDavid Brownell 25498caab75fSGeert Uytterhoeven device_initialize(&ctlr->dev); 2550*16a8e2fbSUwe Kleine-König INIT_LIST_HEAD(&ctlr->queue); 2551*16a8e2fbSUwe Kleine-König spin_lock_init(&ctlr->queue_lock); 2552*16a8e2fbSUwe Kleine-König spin_lock_init(&ctlr->bus_lock_spinlock); 2553*16a8e2fbSUwe Kleine-König mutex_init(&ctlr->bus_lock_mutex); 2554*16a8e2fbSUwe Kleine-König mutex_init(&ctlr->io_mutex); 2555*16a8e2fbSUwe Kleine-König mutex_init(&ctlr->add_lock); 25568caab75fSGeert Uytterhoeven ctlr->bus_num = -1; 25578caab75fSGeert Uytterhoeven ctlr->num_chipselect = 1; 25588caab75fSGeert Uytterhoeven ctlr->slave = slave; 25596c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 25608caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_slave_class; 25616c364062SGeert Uytterhoeven else 25628caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_master_class; 25638caab75fSGeert Uytterhoeven ctlr->dev.parent = dev; 25648caab75fSGeert Uytterhoeven pm_suspend_ignore_children(&ctlr->dev, true); 2565229e6af1SLukas Wunner spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size); 25668ae12a0dSDavid Brownell 25678caab75fSGeert Uytterhoeven return ctlr; 25688ae12a0dSDavid Brownell } 25696c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller); 25708ae12a0dSDavid Brownell 25715e844cc3SLukas Wunner static void devm_spi_release_controller(struct device *dev, void *ctlr) 25725e844cc3SLukas Wunner { 25735e844cc3SLukas Wunner spi_controller_put(*(struct spi_controller **)ctlr); 25745e844cc3SLukas Wunner } 25755e844cc3SLukas Wunner 25765e844cc3SLukas Wunner /** 25775e844cc3SLukas Wunner * __devm_spi_alloc_controller - resource-managed __spi_alloc_controller() 25785e844cc3SLukas Wunner * @dev: physical device of SPI controller 25795e844cc3SLukas Wunner * @size: how much zeroed driver-private data to allocate 25805e844cc3SLukas Wunner * @slave: whether to allocate an SPI master (false) or SPI slave (true) 25815e844cc3SLukas Wunner * Context: can sleep 25825e844cc3SLukas Wunner * 25835e844cc3SLukas Wunner * Allocate an SPI controller and automatically release a reference on it 25845e844cc3SLukas Wunner * when @dev is unbound from its driver. Drivers are thus relieved from 25855e844cc3SLukas Wunner * having to call spi_controller_put(). 25865e844cc3SLukas Wunner * 25875e844cc3SLukas Wunner * The arguments to this function are identical to __spi_alloc_controller(). 25885e844cc3SLukas Wunner * 25895e844cc3SLukas Wunner * Return: the SPI controller structure on success, else NULL. 25905e844cc3SLukas Wunner */ 25915e844cc3SLukas Wunner struct spi_controller *__devm_spi_alloc_controller(struct device *dev, 25925e844cc3SLukas Wunner unsigned int size, 25935e844cc3SLukas Wunner bool slave) 25945e844cc3SLukas Wunner { 25955e844cc3SLukas Wunner struct spi_controller **ptr, *ctlr; 25965e844cc3SLukas Wunner 25975e844cc3SLukas Wunner ptr = devres_alloc(devm_spi_release_controller, sizeof(*ptr), 25985e844cc3SLukas Wunner GFP_KERNEL); 25995e844cc3SLukas Wunner if (!ptr) 26005e844cc3SLukas Wunner return NULL; 26015e844cc3SLukas Wunner 26025e844cc3SLukas Wunner ctlr = __spi_alloc_controller(dev, size, slave); 26035e844cc3SLukas Wunner if (ctlr) { 2604794aaf01SWilliam A. Kennington III ctlr->devm_allocated = true; 26055e844cc3SLukas Wunner *ptr = ctlr; 26065e844cc3SLukas Wunner devres_add(dev, ptr); 26075e844cc3SLukas Wunner } else { 26085e844cc3SLukas Wunner devres_free(ptr); 26095e844cc3SLukas Wunner } 26105e844cc3SLukas Wunner 26115e844cc3SLukas Wunner return ctlr; 26125e844cc3SLukas Wunner } 26135e844cc3SLukas Wunner EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller); 26145e844cc3SLukas Wunner 261574317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF 261643004f31SLinus Walleij static int of_spi_get_gpio_numbers(struct spi_controller *ctlr) 261774317984SJean-Christophe PLAGNIOL-VILLARD { 2618e80beb27SGrant Likely int nb, i, *cs; 26198caab75fSGeert Uytterhoeven struct device_node *np = ctlr->dev.of_node; 262074317984SJean-Christophe PLAGNIOL-VILLARD 262174317984SJean-Christophe PLAGNIOL-VILLARD if (!np) 262274317984SJean-Christophe PLAGNIOL-VILLARD return 0; 262374317984SJean-Christophe PLAGNIOL-VILLARD 262474317984SJean-Christophe PLAGNIOL-VILLARD nb = of_gpio_named_count(np, "cs-gpios"); 26258caab75fSGeert Uytterhoeven ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 262674317984SJean-Christophe PLAGNIOL-VILLARD 26278ec5d84eSAndreas Larsson /* Return error only for an incorrectly formed cs-gpios property */ 26288ec5d84eSAndreas Larsson if (nb == 0 || nb == -ENOENT) 262974317984SJean-Christophe PLAGNIOL-VILLARD return 0; 26308ec5d84eSAndreas Larsson else if (nb < 0) 26318ec5d84eSAndreas Larsson return nb; 263274317984SJean-Christophe PLAGNIOL-VILLARD 2633a86854d0SKees Cook cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), 263474317984SJean-Christophe PLAGNIOL-VILLARD GFP_KERNEL); 26358caab75fSGeert Uytterhoeven ctlr->cs_gpios = cs; 263674317984SJean-Christophe PLAGNIOL-VILLARD 26378caab75fSGeert Uytterhoeven if (!ctlr->cs_gpios) 263874317984SJean-Christophe PLAGNIOL-VILLARD return -ENOMEM; 263974317984SJean-Christophe PLAGNIOL-VILLARD 26408caab75fSGeert Uytterhoeven for (i = 0; i < ctlr->num_chipselect; i++) 2641446411e1SAndreas Larsson cs[i] = -ENOENT; 264274317984SJean-Christophe PLAGNIOL-VILLARD 264374317984SJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < nb; i++) 264474317984SJean-Christophe PLAGNIOL-VILLARD cs[i] = of_get_named_gpio(np, "cs-gpios", i); 264574317984SJean-Christophe PLAGNIOL-VILLARD 264674317984SJean-Christophe PLAGNIOL-VILLARD return 0; 264774317984SJean-Christophe PLAGNIOL-VILLARD } 264874317984SJean-Christophe PLAGNIOL-VILLARD #else 264943004f31SLinus Walleij static int of_spi_get_gpio_numbers(struct spi_controller *ctlr) 265074317984SJean-Christophe PLAGNIOL-VILLARD { 265174317984SJean-Christophe PLAGNIOL-VILLARD return 0; 265274317984SJean-Christophe PLAGNIOL-VILLARD } 265374317984SJean-Christophe PLAGNIOL-VILLARD #endif 265474317984SJean-Christophe PLAGNIOL-VILLARD 2655f3186dd8SLinus Walleij /** 2656f3186dd8SLinus Walleij * spi_get_gpio_descs() - grab chip select GPIOs for the master 2657f3186dd8SLinus Walleij * @ctlr: The SPI master to grab GPIO descriptors for 2658f3186dd8SLinus Walleij */ 2659f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr) 2660f3186dd8SLinus Walleij { 2661f3186dd8SLinus Walleij int nb, i; 2662f3186dd8SLinus Walleij struct gpio_desc **cs; 2663f3186dd8SLinus Walleij struct device *dev = &ctlr->dev; 26647d93aecdSGeert Uytterhoeven unsigned long native_cs_mask = 0; 26657d93aecdSGeert Uytterhoeven unsigned int num_cs_gpios = 0; 2666f3186dd8SLinus Walleij 2667f3186dd8SLinus Walleij nb = gpiod_count(dev, "cs"); 266831ed8ebcSAndy Shevchenko if (nb < 0) { 2669f3186dd8SLinus Walleij /* No GPIOs at all is fine, else return the error */ 267031ed8ebcSAndy Shevchenko if (nb == -ENOENT) 2671f3186dd8SLinus Walleij return 0; 2672f3186dd8SLinus Walleij return nb; 267331ed8ebcSAndy Shevchenko } 267431ed8ebcSAndy Shevchenko 267531ed8ebcSAndy Shevchenko ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2676f3186dd8SLinus Walleij 2677f3186dd8SLinus Walleij cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), 2678f3186dd8SLinus Walleij GFP_KERNEL); 2679f3186dd8SLinus Walleij if (!cs) 2680f3186dd8SLinus Walleij return -ENOMEM; 2681f3186dd8SLinus Walleij ctlr->cs_gpiods = cs; 2682f3186dd8SLinus Walleij 2683f3186dd8SLinus Walleij for (i = 0; i < nb; i++) { 2684f3186dd8SLinus Walleij /* 2685f3186dd8SLinus Walleij * Most chipselects are active low, the inverted 2686f3186dd8SLinus Walleij * semantics are handled by special quirks in gpiolib, 2687f3186dd8SLinus Walleij * so initializing them GPIOD_OUT_LOW here means 2688f3186dd8SLinus Walleij * "unasserted", in most cases this will drive the physical 2689f3186dd8SLinus Walleij * line high. 2690f3186dd8SLinus Walleij */ 2691f3186dd8SLinus Walleij cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, 2692f3186dd8SLinus Walleij GPIOD_OUT_LOW); 26931723fdecSGeert Uytterhoeven if (IS_ERR(cs[i])) 26941723fdecSGeert Uytterhoeven return PTR_ERR(cs[i]); 2695f3186dd8SLinus Walleij 2696f3186dd8SLinus Walleij if (cs[i]) { 2697f3186dd8SLinus Walleij /* 2698f3186dd8SLinus Walleij * If we find a CS GPIO, name it after the device and 2699f3186dd8SLinus Walleij * chip select line. 2700f3186dd8SLinus Walleij */ 2701f3186dd8SLinus Walleij char *gpioname; 2702f3186dd8SLinus Walleij 2703f3186dd8SLinus Walleij gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", 2704f3186dd8SLinus Walleij dev_name(dev), i); 2705f3186dd8SLinus Walleij if (!gpioname) 2706f3186dd8SLinus Walleij return -ENOMEM; 2707f3186dd8SLinus Walleij gpiod_set_consumer_name(cs[i], gpioname); 27087d93aecdSGeert Uytterhoeven num_cs_gpios++; 27097d93aecdSGeert Uytterhoeven continue; 2710f3186dd8SLinus Walleij } 27117d93aecdSGeert Uytterhoeven 27127d93aecdSGeert Uytterhoeven if (ctlr->max_native_cs && i >= ctlr->max_native_cs) { 27137d93aecdSGeert Uytterhoeven dev_err(dev, "Invalid native chip select %d\n", i); 27147d93aecdSGeert Uytterhoeven return -EINVAL; 27157d93aecdSGeert Uytterhoeven } 27167d93aecdSGeert Uytterhoeven native_cs_mask |= BIT(i); 27177d93aecdSGeert Uytterhoeven } 27187d93aecdSGeert Uytterhoeven 2719f60d7270SAndy Shevchenko ctlr->unused_native_cs = ffs(~native_cs_mask) - 1; 2720dbaca8e5SAndy Shevchenko 2721dbaca8e5SAndy Shevchenko if ((ctlr->flags & SPI_MASTER_GPIO_SS) && num_cs_gpios && 2722dbaca8e5SAndy Shevchenko ctlr->max_native_cs && ctlr->unused_native_cs >= ctlr->max_native_cs) { 27237d93aecdSGeert Uytterhoeven dev_err(dev, "No unused native chip select available\n"); 27247d93aecdSGeert Uytterhoeven return -EINVAL; 2725f3186dd8SLinus Walleij } 2726f3186dd8SLinus Walleij 2727f3186dd8SLinus Walleij return 0; 2728f3186dd8SLinus Walleij } 2729f3186dd8SLinus Walleij 2730bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr) 2731bdf3a3b5SBoris Brezillon { 2732bdf3a3b5SBoris Brezillon /* 2733b5932f5cSBoris Brezillon * The controller may implement only the high-level SPI-memory like 2734b5932f5cSBoris Brezillon * operations if it does not support regular SPI transfers, and this is 2735b5932f5cSBoris Brezillon * valid use case. 2736b5932f5cSBoris Brezillon * If ->mem_ops is NULL, we request that at least one of the 2737b5932f5cSBoris Brezillon * ->transfer_xxx() method be implemented. 2738bdf3a3b5SBoris Brezillon */ 2739b5932f5cSBoris Brezillon if (ctlr->mem_ops) { 2740b5932f5cSBoris Brezillon if (!ctlr->mem_ops->exec_op) 2741bdf3a3b5SBoris Brezillon return -EINVAL; 2742b5932f5cSBoris Brezillon } else if (!ctlr->transfer && !ctlr->transfer_one && 2743b5932f5cSBoris Brezillon !ctlr->transfer_one_message) { 2744b5932f5cSBoris Brezillon return -EINVAL; 2745b5932f5cSBoris Brezillon } 2746bdf3a3b5SBoris Brezillon 2747bdf3a3b5SBoris Brezillon return 0; 2748bdf3a3b5SBoris Brezillon } 2749bdf3a3b5SBoris Brezillon 27508ae12a0dSDavid Brownell /** 27518caab75fSGeert Uytterhoeven * spi_register_controller - register SPI master or slave controller 27528caab75fSGeert Uytterhoeven * @ctlr: initialized master, originally from spi_alloc_master() or 27538caab75fSGeert Uytterhoeven * spi_alloc_slave() 275433e34dc6SDavid Brownell * Context: can sleep 27558ae12a0dSDavid Brownell * 27568caab75fSGeert Uytterhoeven * SPI controllers connect to their drivers using some non-SPI bus, 27578ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 27588caab75fSGeert Uytterhoeven * includes calling spi_register_controller() to hook up to this SPI bus glue. 27598ae12a0dSDavid Brownell * 27608ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 27618ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 27628ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 27638ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 27648ae12a0dSDavid Brownell * chip is at which address. 27658ae12a0dSDavid Brownell * 27668ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 27678caab75fSGeert Uytterhoeven * success, else a negative error code (dropping the controller's refcount). 27680c868461SDavid Brownell * After a successful return, the caller is responsible for calling 27698caab75fSGeert Uytterhoeven * spi_unregister_controller(). 277097d56dc6SJavier Martinez Canillas * 277197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 27728ae12a0dSDavid Brownell */ 27738caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr) 27748ae12a0dSDavid Brownell { 27758caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 27762b9603a0SFeng Tang struct boardinfo *bi; 2777b93318a2SSergei Shtylyov int status; 277842bdd706SLucas Stach int id, first_dynamic; 27798ae12a0dSDavid Brownell 27800c868461SDavid Brownell if (!dev) 27810c868461SDavid Brownell return -ENODEV; 27820c868461SDavid Brownell 2783bdf3a3b5SBoris Brezillon /* 2784bdf3a3b5SBoris Brezillon * Make sure all necessary hooks are implemented before registering 2785bdf3a3b5SBoris Brezillon * the SPI controller. 2786bdf3a3b5SBoris Brezillon */ 2787bdf3a3b5SBoris Brezillon status = spi_controller_check_ops(ctlr); 2788bdf3a3b5SBoris Brezillon if (status) 2789bdf3a3b5SBoris Brezillon return status; 2790bdf3a3b5SBoris Brezillon 279104b2d03aSGeert Uytterhoeven if (ctlr->bus_num >= 0) { 279204b2d03aSGeert Uytterhoeven /* devices with a fixed bus num must check-in with the num */ 279304b2d03aSGeert Uytterhoeven mutex_lock(&board_lock); 279404b2d03aSGeert Uytterhoeven id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 279504b2d03aSGeert Uytterhoeven ctlr->bus_num + 1, GFP_KERNEL); 279604b2d03aSGeert Uytterhoeven mutex_unlock(&board_lock); 279704b2d03aSGeert Uytterhoeven if (WARN(id < 0, "couldn't get idr")) 279804b2d03aSGeert Uytterhoeven return id == -ENOSPC ? -EBUSY : id; 279904b2d03aSGeert Uytterhoeven ctlr->bus_num = id; 280004b2d03aSGeert Uytterhoeven } else if (ctlr->dev.of_node) { 28019b61e302SSuniel Mahesh /* allocate dynamic bus number using Linux idr */ 28029b61e302SSuniel Mahesh id = of_alias_get_id(ctlr->dev.of_node, "spi"); 28039b61e302SSuniel Mahesh if (id >= 0) { 28049b61e302SSuniel Mahesh ctlr->bus_num = id; 28059b61e302SSuniel Mahesh mutex_lock(&board_lock); 28069b61e302SSuniel Mahesh id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 28079b61e302SSuniel Mahesh ctlr->bus_num + 1, GFP_KERNEL); 28089b61e302SSuniel Mahesh mutex_unlock(&board_lock); 28099b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 28109b61e302SSuniel Mahesh return id == -ENOSPC ? -EBUSY : id; 28119b61e302SSuniel Mahesh } 28129b61e302SSuniel Mahesh } 28138caab75fSGeert Uytterhoeven if (ctlr->bus_num < 0) { 281442bdd706SLucas Stach first_dynamic = of_alias_get_highest_id("spi"); 281542bdd706SLucas Stach if (first_dynamic < 0) 281642bdd706SLucas Stach first_dynamic = 0; 281742bdd706SLucas Stach else 281842bdd706SLucas Stach first_dynamic++; 281942bdd706SLucas Stach 28209b61e302SSuniel Mahesh mutex_lock(&board_lock); 282142bdd706SLucas Stach id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, 282242bdd706SLucas Stach 0, GFP_KERNEL); 28239b61e302SSuniel Mahesh mutex_unlock(&board_lock); 28249b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 28259b61e302SSuniel Mahesh return id; 28269b61e302SSuniel Mahesh ctlr->bus_num = id; 28278ae12a0dSDavid Brownell } 28288caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 28298caab75fSGeert Uytterhoeven init_completion(&ctlr->xfer_completion); 28308caab75fSGeert Uytterhoeven if (!ctlr->max_dma_len) 28318caab75fSGeert Uytterhoeven ctlr->max_dma_len = INT_MAX; 2832cf32b71eSErnst Schwab 28338ae12a0dSDavid Brownell /* register the device, then userspace will see it. 28348ae12a0dSDavid Brownell * registration fails if the bus ID is in use. 28358ae12a0dSDavid Brownell */ 28368caab75fSGeert Uytterhoeven dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 28370a919ae4SAndrey Smirnov 28380a919ae4SAndrey Smirnov if (!spi_controller_is_slave(ctlr)) { 28390a919ae4SAndrey Smirnov if (ctlr->use_gpio_descriptors) { 28400a919ae4SAndrey Smirnov status = spi_get_gpio_descs(ctlr); 28410a919ae4SAndrey Smirnov if (status) 2842f9981d4fSAaro Koskinen goto free_bus_id; 28430a919ae4SAndrey Smirnov /* 28440a919ae4SAndrey Smirnov * A controller using GPIO descriptors always 28450a919ae4SAndrey Smirnov * supports SPI_CS_HIGH if need be. 28460a919ae4SAndrey Smirnov */ 28470a919ae4SAndrey Smirnov ctlr->mode_bits |= SPI_CS_HIGH; 28480a919ae4SAndrey Smirnov } else { 28490a919ae4SAndrey Smirnov /* Legacy code path for GPIOs from DT */ 285043004f31SLinus Walleij status = of_spi_get_gpio_numbers(ctlr); 28510a919ae4SAndrey Smirnov if (status) 2852f9981d4fSAaro Koskinen goto free_bus_id; 28530a919ae4SAndrey Smirnov } 28540a919ae4SAndrey Smirnov } 28550a919ae4SAndrey Smirnov 2856f9481b08STudor Ambarus /* 2857f9481b08STudor Ambarus * Even if it's just one always-selected device, there must 2858f9481b08STudor Ambarus * be at least one chipselect. 2859f9481b08STudor Ambarus */ 2860f9981d4fSAaro Koskinen if (!ctlr->num_chipselect) { 2861f9981d4fSAaro Koskinen status = -EINVAL; 2862f9981d4fSAaro Koskinen goto free_bus_id; 2863f9981d4fSAaro Koskinen } 2864f9481b08STudor Ambarus 28658caab75fSGeert Uytterhoeven status = device_add(&ctlr->dev); 2866f9981d4fSAaro Koskinen if (status < 0) 2867f9981d4fSAaro Koskinen goto free_bus_id; 28689b61e302SSuniel Mahesh dev_dbg(dev, "registered %s %s\n", 28698caab75fSGeert Uytterhoeven spi_controller_is_slave(ctlr) ? "slave" : "master", 28709b61e302SSuniel Mahesh dev_name(&ctlr->dev)); 28718ae12a0dSDavid Brownell 2872b5932f5cSBoris Brezillon /* 2873b5932f5cSBoris Brezillon * If we're using a queued driver, start the queue. Note that we don't 2874b5932f5cSBoris Brezillon * need the queueing logic if the driver is only supporting high-level 2875b5932f5cSBoris Brezillon * memory operations. 2876b5932f5cSBoris Brezillon */ 2877b5932f5cSBoris Brezillon if (ctlr->transfer) { 28788caab75fSGeert Uytterhoeven dev_info(dev, "controller is unqueued, this is deprecated\n"); 2879b5932f5cSBoris Brezillon } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 28808caab75fSGeert Uytterhoeven status = spi_controller_initialize_queue(ctlr); 2881ffbbdd21SLinus Walleij if (status) { 28828caab75fSGeert Uytterhoeven device_del(&ctlr->dev); 2883f9981d4fSAaro Koskinen goto free_bus_id; 2884ffbbdd21SLinus Walleij } 2885ffbbdd21SLinus Walleij } 2886eca2ebc7SMartin Sperl /* add statistics */ 28878caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->statistics.lock); 2888ffbbdd21SLinus Walleij 28892b9603a0SFeng Tang mutex_lock(&board_lock); 28908caab75fSGeert Uytterhoeven list_add_tail(&ctlr->list, &spi_controller_list); 28912b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 28928caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 28932b9603a0SFeng Tang mutex_unlock(&board_lock); 28942b9603a0SFeng Tang 289564bee4d2SMika Westerberg /* Register devices from the device tree and ACPI */ 28968caab75fSGeert Uytterhoeven of_register_spi_devices(ctlr); 28978caab75fSGeert Uytterhoeven acpi_register_spi_devices(ctlr); 2898f9981d4fSAaro Koskinen return status; 2899f9981d4fSAaro Koskinen 2900f9981d4fSAaro Koskinen free_bus_id: 2901f9981d4fSAaro Koskinen mutex_lock(&board_lock); 2902f9981d4fSAaro Koskinen idr_remove(&spi_master_idr, ctlr->bus_num); 2903f9981d4fSAaro Koskinen mutex_unlock(&board_lock); 29048ae12a0dSDavid Brownell return status; 29058ae12a0dSDavid Brownell } 29068caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller); 29078ae12a0dSDavid Brownell 290859ebbe40STian Tao static void devm_spi_unregister(void *ctlr) 2909666d5b4cSMark Brown { 291059ebbe40STian Tao spi_unregister_controller(ctlr); 2911666d5b4cSMark Brown } 2912666d5b4cSMark Brown 2913666d5b4cSMark Brown /** 29148caab75fSGeert Uytterhoeven * devm_spi_register_controller - register managed SPI master or slave 29158caab75fSGeert Uytterhoeven * controller 29168caab75fSGeert Uytterhoeven * @dev: device managing SPI controller 29178caab75fSGeert Uytterhoeven * @ctlr: initialized controller, originally from spi_alloc_master() or 29188caab75fSGeert Uytterhoeven * spi_alloc_slave() 2919666d5b4cSMark Brown * Context: can sleep 2920666d5b4cSMark Brown * 29218caab75fSGeert Uytterhoeven * Register a SPI device as with spi_register_controller() which will 292268b892f1SJohan Hovold * automatically be unregistered and freed. 292397d56dc6SJavier Martinez Canillas * 292497d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 2925666d5b4cSMark Brown */ 29268caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev, 29278caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 2928666d5b4cSMark Brown { 2929666d5b4cSMark Brown int ret; 2930666d5b4cSMark Brown 29318caab75fSGeert Uytterhoeven ret = spi_register_controller(ctlr); 293259ebbe40STian Tao if (ret) 2933666d5b4cSMark Brown return ret; 293459ebbe40STian Tao 293559ebbe40STian Tao return devm_add_action_or_reset(dev, devm_spi_unregister, ctlr); 2936666d5b4cSMark Brown } 29378caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller); 2938666d5b4cSMark Brown 293934860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 29408ae12a0dSDavid Brownell { 29410c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 29428ae12a0dSDavid Brownell return 0; 29438ae12a0dSDavid Brownell } 29448ae12a0dSDavid Brownell 29458ae12a0dSDavid Brownell /** 29468caab75fSGeert Uytterhoeven * spi_unregister_controller - unregister SPI master or slave controller 29478caab75fSGeert Uytterhoeven * @ctlr: the controller being unregistered 294833e34dc6SDavid Brownell * Context: can sleep 29498ae12a0dSDavid Brownell * 29508caab75fSGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 29518ae12a0dSDavid Brownell * only ones directly touching chip registers. 29528ae12a0dSDavid Brownell * 29538ae12a0dSDavid Brownell * This must be called from context that can sleep. 295468b892f1SJohan Hovold * 295568b892f1SJohan Hovold * Note that this function also drops a reference to the controller. 29568ae12a0dSDavid Brownell */ 29578caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr) 29588ae12a0dSDavid Brownell { 29599b61e302SSuniel Mahesh struct spi_controller *found; 296067f7b278SJohan Hovold int id = ctlr->bus_num; 296189fc9a1aSJeff Garzik 2962ddf75be4SLukas Wunner /* Prevent addition of new devices, unregister existing ones */ 2963ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 29646098475dSMark Brown mutex_lock(&ctlr->add_lock); 2965ddf75be4SLukas Wunner 296684855678SLukas Wunner device_for_each_child(&ctlr->dev, NULL, __unregister); 296784855678SLukas Wunner 29689b61e302SSuniel Mahesh /* First make sure that this controller was ever added */ 29699b61e302SSuniel Mahesh mutex_lock(&board_lock); 297067f7b278SJohan Hovold found = idr_find(&spi_master_idr, id); 29719b61e302SSuniel Mahesh mutex_unlock(&board_lock); 29728caab75fSGeert Uytterhoeven if (ctlr->queued) { 29738caab75fSGeert Uytterhoeven if (spi_destroy_queue(ctlr)) 29748caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue remove failed\n"); 2975ffbbdd21SLinus Walleij } 29762b9603a0SFeng Tang mutex_lock(&board_lock); 29778caab75fSGeert Uytterhoeven list_del(&ctlr->list); 29782b9603a0SFeng Tang mutex_unlock(&board_lock); 29792b9603a0SFeng Tang 29805e844cc3SLukas Wunner device_del(&ctlr->dev); 29815e844cc3SLukas Wunner 29825e844cc3SLukas Wunner /* Release the last reference on the controller if its driver 29835e844cc3SLukas Wunner * has not yet been converted to devm_spi_alloc_master/slave(). 29845e844cc3SLukas Wunner */ 2985794aaf01SWilliam A. Kennington III if (!ctlr->devm_allocated) 29865e844cc3SLukas Wunner put_device(&ctlr->dev); 29875e844cc3SLukas Wunner 29889b61e302SSuniel Mahesh /* free bus id */ 29899b61e302SSuniel Mahesh mutex_lock(&board_lock); 2990613bd1eaSJarkko Nikula if (found == ctlr) 299167f7b278SJohan Hovold idr_remove(&spi_master_idr, id); 29929b61e302SSuniel Mahesh mutex_unlock(&board_lock); 2993ddf75be4SLukas Wunner 2994ddf75be4SLukas Wunner if (IS_ENABLED(CONFIG_SPI_DYNAMIC)) 29956098475dSMark Brown mutex_unlock(&ctlr->add_lock); 29968ae12a0dSDavid Brownell } 29978caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller); 29988ae12a0dSDavid Brownell 29998caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr) 3000ffbbdd21SLinus Walleij { 3001ffbbdd21SLinus Walleij int ret; 3002ffbbdd21SLinus Walleij 30038caab75fSGeert Uytterhoeven /* Basically no-ops for non-queued controllers */ 30048caab75fSGeert Uytterhoeven if (!ctlr->queued) 3005ffbbdd21SLinus Walleij return 0; 3006ffbbdd21SLinus Walleij 30078caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 3008ffbbdd21SLinus Walleij if (ret) 30098caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue stop failed\n"); 3010ffbbdd21SLinus Walleij 3011ffbbdd21SLinus Walleij return ret; 3012ffbbdd21SLinus Walleij } 30138caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend); 3014ffbbdd21SLinus Walleij 30158caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr) 3016ffbbdd21SLinus Walleij { 3017ffbbdd21SLinus Walleij int ret; 3018ffbbdd21SLinus Walleij 30198caab75fSGeert Uytterhoeven if (!ctlr->queued) 3020ffbbdd21SLinus Walleij return 0; 3021ffbbdd21SLinus Walleij 30228caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 3023ffbbdd21SLinus Walleij if (ret) 30248caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue restart failed\n"); 3025ffbbdd21SLinus Walleij 3026ffbbdd21SLinus Walleij return ret; 3027ffbbdd21SLinus Walleij } 30288caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume); 3029ffbbdd21SLinus Walleij 30308caab75fSGeert Uytterhoeven static int __spi_controller_match(struct device *dev, const void *data) 30315ed2c832SDave Young { 30328caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 30339f3b795aSMichał Mirosław const u16 *bus_num = data; 30345ed2c832SDave Young 30358caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 30368caab75fSGeert Uytterhoeven return ctlr->bus_num == *bus_num; 30375ed2c832SDave Young } 30385ed2c832SDave Young 30398ae12a0dSDavid Brownell /** 30408ae12a0dSDavid Brownell * spi_busnum_to_master - look up master associated with bus_num 30418ae12a0dSDavid Brownell * @bus_num: the master's bus number 304233e34dc6SDavid Brownell * Context: can sleep 30438ae12a0dSDavid Brownell * 30448ae12a0dSDavid Brownell * This call may be used with devices that are registered after 30458ae12a0dSDavid Brownell * arch init time. It returns a refcounted pointer to the relevant 30468caab75fSGeert Uytterhoeven * spi_controller (which the caller must release), or NULL if there is 30478ae12a0dSDavid Brownell * no such master registered. 304897d56dc6SJavier Martinez Canillas * 304997d56dc6SJavier Martinez Canillas * Return: the SPI master structure on success, else NULL. 30508ae12a0dSDavid Brownell */ 30518caab75fSGeert Uytterhoeven struct spi_controller *spi_busnum_to_master(u16 bus_num) 30528ae12a0dSDavid Brownell { 305349dce689STony Jones struct device *dev; 30548caab75fSGeert Uytterhoeven struct spi_controller *ctlr = NULL; 30558ae12a0dSDavid Brownell 3056695794aeSGreg Kroah-Hartman dev = class_find_device(&spi_master_class, NULL, &bus_num, 30578caab75fSGeert Uytterhoeven __spi_controller_match); 30585ed2c832SDave Young if (dev) 30598caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 30605ed2c832SDave Young /* reference got in class_find_device */ 30618caab75fSGeert Uytterhoeven return ctlr; 30628ae12a0dSDavid Brownell } 30638ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master); 30648ae12a0dSDavid Brownell 3065d780c371SMartin Sperl /*-------------------------------------------------------------------------*/ 3066d780c371SMartin Sperl 3067d780c371SMartin Sperl /* Core methods for SPI resource management */ 3068d780c371SMartin Sperl 3069d780c371SMartin Sperl /** 3070d780c371SMartin Sperl * spi_res_alloc - allocate a spi resource that is life-cycle managed 3071d780c371SMartin Sperl * during the processing of a spi_message while using 3072d780c371SMartin Sperl * spi_transfer_one 3073d780c371SMartin Sperl * @spi: the spi device for which we allocate memory 3074d780c371SMartin Sperl * @release: the release code to execute for this resource 3075d780c371SMartin Sperl * @size: size to alloc and return 3076d780c371SMartin Sperl * @gfp: GFP allocation flags 3077d780c371SMartin Sperl * 3078d780c371SMartin Sperl * Return: the pointer to the allocated data 3079d780c371SMartin Sperl * 3080d780c371SMartin Sperl * This may get enhanced in the future to allocate from a memory pool 30818caab75fSGeert Uytterhoeven * of the @spi_device or @spi_controller to avoid repeated allocations. 3082d780c371SMartin Sperl */ 3083d780c371SMartin Sperl void *spi_res_alloc(struct spi_device *spi, 3084d780c371SMartin Sperl spi_res_release_t release, 3085d780c371SMartin Sperl size_t size, gfp_t gfp) 3086d780c371SMartin Sperl { 3087d780c371SMartin Sperl struct spi_res *sres; 3088d780c371SMartin Sperl 3089d780c371SMartin Sperl sres = kzalloc(sizeof(*sres) + size, gfp); 3090d780c371SMartin Sperl if (!sres) 3091d780c371SMartin Sperl return NULL; 3092d780c371SMartin Sperl 3093d780c371SMartin Sperl INIT_LIST_HEAD(&sres->entry); 3094d780c371SMartin Sperl sres->release = release; 3095d780c371SMartin Sperl 3096d780c371SMartin Sperl return sres->data; 3097d780c371SMartin Sperl } 3098d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_alloc); 3099d780c371SMartin Sperl 3100d780c371SMartin Sperl /** 3101d780c371SMartin Sperl * spi_res_free - free an spi resource 3102d780c371SMartin Sperl * @res: pointer to the custom data of a resource 3103d780c371SMartin Sperl * 3104d780c371SMartin Sperl */ 3105d780c371SMartin Sperl void spi_res_free(void *res) 3106d780c371SMartin Sperl { 3107d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 3108d780c371SMartin Sperl 3109d780c371SMartin Sperl if (!res) 3110d780c371SMartin Sperl return; 3111d780c371SMartin Sperl 3112d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 3113d780c371SMartin Sperl kfree(sres); 3114d780c371SMartin Sperl } 3115d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_free); 3116d780c371SMartin Sperl 3117d780c371SMartin Sperl /** 3118d780c371SMartin Sperl * spi_res_add - add a spi_res to the spi_message 3119d780c371SMartin Sperl * @message: the spi message 3120d780c371SMartin Sperl * @res: the spi_resource 3121d780c371SMartin Sperl */ 3122d780c371SMartin Sperl void spi_res_add(struct spi_message *message, void *res) 3123d780c371SMartin Sperl { 3124d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 3125d780c371SMartin Sperl 3126d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 3127d780c371SMartin Sperl list_add_tail(&sres->entry, &message->resources); 3128d780c371SMartin Sperl } 3129d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_add); 3130d780c371SMartin Sperl 3131d780c371SMartin Sperl /** 3132d780c371SMartin Sperl * spi_res_release - release all spi resources for this message 31338caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller 3134d780c371SMartin Sperl * @message: the @spi_message 3135d780c371SMartin Sperl */ 31368caab75fSGeert Uytterhoeven void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 3137d780c371SMartin Sperl { 3138f5694369SVladimir Zapolskiy struct spi_res *res, *tmp; 3139d780c371SMartin Sperl 3140f5694369SVladimir Zapolskiy list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) { 3141d780c371SMartin Sperl if (res->release) 31428caab75fSGeert Uytterhoeven res->release(ctlr, message, res->data); 3143d780c371SMartin Sperl 3144d780c371SMartin Sperl list_del(&res->entry); 3145d780c371SMartin Sperl 3146d780c371SMartin Sperl kfree(res); 3147d780c371SMartin Sperl } 3148d780c371SMartin Sperl } 3149d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_release); 31508ae12a0dSDavid Brownell 31518ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 31528ae12a0dSDavid Brownell 3153523baf5aSMartin Sperl /* Core methods for spi_message alterations */ 3154523baf5aSMartin Sperl 31558caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr, 3156523baf5aSMartin Sperl struct spi_message *msg, 3157523baf5aSMartin Sperl void *res) 3158523baf5aSMartin Sperl { 3159523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer = res; 3160523baf5aSMartin Sperl size_t i; 3161523baf5aSMartin Sperl 3162523baf5aSMartin Sperl /* call extra callback if requested */ 3163523baf5aSMartin Sperl if (rxfer->release) 31648caab75fSGeert Uytterhoeven rxfer->release(ctlr, msg, res); 3165523baf5aSMartin Sperl 3166523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 3167523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 3168523baf5aSMartin Sperl 3169523baf5aSMartin Sperl /* remove the formerly inserted entries */ 3170523baf5aSMartin Sperl for (i = 0; i < rxfer->inserted; i++) 3171523baf5aSMartin Sperl list_del(&rxfer->inserted_transfers[i].transfer_list); 3172523baf5aSMartin Sperl } 3173523baf5aSMartin Sperl 3174523baf5aSMartin Sperl /** 3175523baf5aSMartin Sperl * spi_replace_transfers - replace transfers with several transfers 3176523baf5aSMartin Sperl * and register change with spi_message.resources 3177523baf5aSMartin Sperl * @msg: the spi_message we work upon 3178523baf5aSMartin Sperl * @xfer_first: the first spi_transfer we want to replace 3179523baf5aSMartin Sperl * @remove: number of transfers to remove 3180523baf5aSMartin Sperl * @insert: the number of transfers we want to insert instead 3181523baf5aSMartin Sperl * @release: extra release code necessary in some circumstances 3182523baf5aSMartin Sperl * @extradatasize: extra data to allocate (with alignment guarantees 3183523baf5aSMartin Sperl * of struct @spi_transfer) 318405885397SMartin Sperl * @gfp: gfp flags 3185523baf5aSMartin Sperl * 3186523baf5aSMartin Sperl * Returns: pointer to @spi_replaced_transfers, 3187523baf5aSMartin Sperl * PTR_ERR(...) in case of errors. 3188523baf5aSMartin Sperl */ 3189523baf5aSMartin Sperl struct spi_replaced_transfers *spi_replace_transfers( 3190523baf5aSMartin Sperl struct spi_message *msg, 3191523baf5aSMartin Sperl struct spi_transfer *xfer_first, 3192523baf5aSMartin Sperl size_t remove, 3193523baf5aSMartin Sperl size_t insert, 3194523baf5aSMartin Sperl spi_replaced_release_t release, 3195523baf5aSMartin Sperl size_t extradatasize, 3196523baf5aSMartin Sperl gfp_t gfp) 3197523baf5aSMartin Sperl { 3198523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer; 3199523baf5aSMartin Sperl struct spi_transfer *xfer; 3200523baf5aSMartin Sperl size_t i; 3201523baf5aSMartin Sperl 3202523baf5aSMartin Sperl /* allocate the structure using spi_res */ 3203523baf5aSMartin Sperl rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 3204aef97522SGustavo A. R. Silva struct_size(rxfer, inserted_transfers, insert) 3205523baf5aSMartin Sperl + extradatasize, 3206523baf5aSMartin Sperl gfp); 3207523baf5aSMartin Sperl if (!rxfer) 3208523baf5aSMartin Sperl return ERR_PTR(-ENOMEM); 3209523baf5aSMartin Sperl 3210523baf5aSMartin Sperl /* the release code to invoke before running the generic release */ 3211523baf5aSMartin Sperl rxfer->release = release; 3212523baf5aSMartin Sperl 3213523baf5aSMartin Sperl /* assign extradata */ 3214523baf5aSMartin Sperl if (extradatasize) 3215523baf5aSMartin Sperl rxfer->extradata = 3216523baf5aSMartin Sperl &rxfer->inserted_transfers[insert]; 3217523baf5aSMartin Sperl 3218523baf5aSMartin Sperl /* init the replaced_transfers list */ 3219523baf5aSMartin Sperl INIT_LIST_HEAD(&rxfer->replaced_transfers); 3220523baf5aSMartin Sperl 3221523baf5aSMartin Sperl /* assign the list_entry after which we should reinsert 3222523baf5aSMartin Sperl * the @replaced_transfers - it may be spi_message.messages! 3223523baf5aSMartin Sperl */ 3224523baf5aSMartin Sperl rxfer->replaced_after = xfer_first->transfer_list.prev; 3225523baf5aSMartin Sperl 3226523baf5aSMartin Sperl /* remove the requested number of transfers */ 3227523baf5aSMartin Sperl for (i = 0; i < remove; i++) { 3228523baf5aSMartin Sperl /* if the entry after replaced_after it is msg->transfers 3229523baf5aSMartin Sperl * then we have been requested to remove more transfers 3230523baf5aSMartin Sperl * than are in the list 3231523baf5aSMartin Sperl */ 3232523baf5aSMartin Sperl if (rxfer->replaced_after->next == &msg->transfers) { 3233523baf5aSMartin Sperl dev_err(&msg->spi->dev, 3234523baf5aSMartin Sperl "requested to remove more spi_transfers than are available\n"); 3235523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 3236523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, 3237523baf5aSMartin Sperl rxfer->replaced_after); 3238523baf5aSMartin Sperl 3239523baf5aSMartin Sperl /* free the spi_replace_transfer structure */ 3240523baf5aSMartin Sperl spi_res_free(rxfer); 3241523baf5aSMartin Sperl 3242523baf5aSMartin Sperl /* and return with an error */ 3243523baf5aSMartin Sperl return ERR_PTR(-EINVAL); 3244523baf5aSMartin Sperl } 3245523baf5aSMartin Sperl 3246523baf5aSMartin Sperl /* remove the entry after replaced_after from list of 3247523baf5aSMartin Sperl * transfers and add it to list of replaced_transfers 3248523baf5aSMartin Sperl */ 3249523baf5aSMartin Sperl list_move_tail(rxfer->replaced_after->next, 3250523baf5aSMartin Sperl &rxfer->replaced_transfers); 3251523baf5aSMartin Sperl } 3252523baf5aSMartin Sperl 3253523baf5aSMartin Sperl /* create copy of the given xfer with identical settings 3254523baf5aSMartin Sperl * based on the first transfer to get removed 3255523baf5aSMartin Sperl */ 3256523baf5aSMartin Sperl for (i = 0; i < insert; i++) { 3257523baf5aSMartin Sperl /* we need to run in reverse order */ 3258523baf5aSMartin Sperl xfer = &rxfer->inserted_transfers[insert - 1 - i]; 3259523baf5aSMartin Sperl 3260523baf5aSMartin Sperl /* copy all spi_transfer data */ 3261523baf5aSMartin Sperl memcpy(xfer, xfer_first, sizeof(*xfer)); 3262523baf5aSMartin Sperl 3263523baf5aSMartin Sperl /* add to list */ 3264523baf5aSMartin Sperl list_add(&xfer->transfer_list, rxfer->replaced_after); 3265523baf5aSMartin Sperl 3266bebcfd27SAlexandru Ardelean /* clear cs_change and delay for all but the last */ 3267523baf5aSMartin Sperl if (i) { 3268523baf5aSMartin Sperl xfer->cs_change = false; 3269bebcfd27SAlexandru Ardelean xfer->delay.value = 0; 3270523baf5aSMartin Sperl } 3271523baf5aSMartin Sperl } 3272523baf5aSMartin Sperl 3273523baf5aSMartin Sperl /* set up inserted */ 3274523baf5aSMartin Sperl rxfer->inserted = insert; 3275523baf5aSMartin Sperl 3276523baf5aSMartin Sperl /* and register it with spi_res/spi_message */ 3277523baf5aSMartin Sperl spi_res_add(msg, rxfer); 3278523baf5aSMartin Sperl 3279523baf5aSMartin Sperl return rxfer; 3280523baf5aSMartin Sperl } 3281523baf5aSMartin Sperl EXPORT_SYMBOL_GPL(spi_replace_transfers); 3282523baf5aSMartin Sperl 32838caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 3284d9f12122SMartin Sperl struct spi_message *msg, 3285d9f12122SMartin Sperl struct spi_transfer **xferp, 3286d9f12122SMartin Sperl size_t maxsize, 3287d9f12122SMartin Sperl gfp_t gfp) 3288d9f12122SMartin Sperl { 3289d9f12122SMartin Sperl struct spi_transfer *xfer = *xferp, *xfers; 3290d9f12122SMartin Sperl struct spi_replaced_transfers *srt; 3291d9f12122SMartin Sperl size_t offset; 3292d9f12122SMartin Sperl size_t count, i; 3293d9f12122SMartin Sperl 3294d9f12122SMartin Sperl /* calculate how many we have to replace */ 3295d9f12122SMartin Sperl count = DIV_ROUND_UP(xfer->len, maxsize); 3296d9f12122SMartin Sperl 3297d9f12122SMartin Sperl /* create replacement */ 3298d9f12122SMartin Sperl srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 3299657d32efSDan Carpenter if (IS_ERR(srt)) 3300657d32efSDan Carpenter return PTR_ERR(srt); 3301d9f12122SMartin Sperl xfers = srt->inserted_transfers; 3302d9f12122SMartin Sperl 3303d9f12122SMartin Sperl /* now handle each of those newly inserted spi_transfers 3304d9f12122SMartin Sperl * note that the replacements spi_transfers all are preset 3305d9f12122SMartin Sperl * to the same values as *xferp, so tx_buf, rx_buf and len 3306d9f12122SMartin Sperl * are all identical (as well as most others) 3307d9f12122SMartin Sperl * so we just have to fix up len and the pointers. 3308d9f12122SMartin Sperl * 3309d9f12122SMartin Sperl * this also includes support for the depreciated 3310d9f12122SMartin Sperl * spi_message.is_dma_mapped interface 3311d9f12122SMartin Sperl */ 3312d9f12122SMartin Sperl 3313d9f12122SMartin Sperl /* the first transfer just needs the length modified, so we 3314d9f12122SMartin Sperl * run it outside the loop 3315d9f12122SMartin Sperl */ 3316c8dab77aSFabio Estevam xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 3317d9f12122SMartin Sperl 3318d9f12122SMartin Sperl /* all the others need rx_buf/tx_buf also set */ 3319d9f12122SMartin Sperl for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 3320d9f12122SMartin Sperl /* update rx_buf, tx_buf and dma */ 3321d9f12122SMartin Sperl if (xfers[i].rx_buf) 3322d9f12122SMartin Sperl xfers[i].rx_buf += offset; 3323d9f12122SMartin Sperl if (xfers[i].rx_dma) 3324d9f12122SMartin Sperl xfers[i].rx_dma += offset; 3325d9f12122SMartin Sperl if (xfers[i].tx_buf) 3326d9f12122SMartin Sperl xfers[i].tx_buf += offset; 3327d9f12122SMartin Sperl if (xfers[i].tx_dma) 3328d9f12122SMartin Sperl xfers[i].tx_dma += offset; 3329d9f12122SMartin Sperl 3330d9f12122SMartin Sperl /* update length */ 3331d9f12122SMartin Sperl xfers[i].len = min(maxsize, xfers[i].len - offset); 3332d9f12122SMartin Sperl } 3333d9f12122SMartin Sperl 3334d9f12122SMartin Sperl /* we set up xferp to the last entry we have inserted, 3335d9f12122SMartin Sperl * so that we skip those already split transfers 3336d9f12122SMartin Sperl */ 3337d9f12122SMartin Sperl *xferp = &xfers[count - 1]; 3338d9f12122SMartin Sperl 3339d9f12122SMartin Sperl /* increment statistics counters */ 33408caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3341d9f12122SMartin Sperl transfers_split_maxsize); 3342d9f12122SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, 3343d9f12122SMartin Sperl transfers_split_maxsize); 3344d9f12122SMartin Sperl 3345d9f12122SMartin Sperl return 0; 3346d9f12122SMartin Sperl } 3347d9f12122SMartin Sperl 3348d9f12122SMartin Sperl /** 3349ce2424d7SMauro Carvalho Chehab * spi_split_transfers_maxsize - split spi transfers into multiple transfers 3350d9f12122SMartin Sperl * when an individual transfer exceeds a 3351d9f12122SMartin Sperl * certain size 33528caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller for this transfer 33533700ce95SMasanari Iida * @msg: the @spi_message to transform 33543700ce95SMasanari Iida * @maxsize: the maximum when to apply this 335510f11a22SJavier Martinez Canillas * @gfp: GFP allocation flags 3356d9f12122SMartin Sperl * 3357d9f12122SMartin Sperl * Return: status of transformation 3358d9f12122SMartin Sperl */ 33598caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr, 3360d9f12122SMartin Sperl struct spi_message *msg, 3361d9f12122SMartin Sperl size_t maxsize, 3362d9f12122SMartin Sperl gfp_t gfp) 3363d9f12122SMartin Sperl { 3364d9f12122SMartin Sperl struct spi_transfer *xfer; 3365d9f12122SMartin Sperl int ret; 3366d9f12122SMartin Sperl 3367d9f12122SMartin Sperl /* iterate over the transfer_list, 3368d9f12122SMartin Sperl * but note that xfer is advanced to the last transfer inserted 3369d9f12122SMartin Sperl * to avoid checking sizes again unnecessarily (also xfer does 3370d9f12122SMartin Sperl * potentiall belong to a different list by the time the 3371d9f12122SMartin Sperl * replacement has happened 3372d9f12122SMartin Sperl */ 3373d9f12122SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 3374d9f12122SMartin Sperl if (xfer->len > maxsize) { 33758caab75fSGeert Uytterhoeven ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 33768caab75fSGeert Uytterhoeven maxsize, gfp); 3377d9f12122SMartin Sperl if (ret) 3378d9f12122SMartin Sperl return ret; 3379d9f12122SMartin Sperl } 3380d9f12122SMartin Sperl } 3381d9f12122SMartin Sperl 3382d9f12122SMartin Sperl return 0; 3383d9f12122SMartin Sperl } 3384d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 33858ae12a0dSDavid Brownell 33868ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 33878ae12a0dSDavid Brownell 33888caab75fSGeert Uytterhoeven /* Core methods for SPI controller protocol drivers. Some of the 33897d077197SDavid Brownell * other core methods are currently defined as inline functions. 33907d077197SDavid Brownell */ 33917d077197SDavid Brownell 33928caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 33938caab75fSGeert Uytterhoeven u8 bits_per_word) 339463ab645fSStefan Brüns { 33958caab75fSGeert Uytterhoeven if (ctlr->bits_per_word_mask) { 339663ab645fSStefan Brüns /* Only 32 bits fit in the mask */ 339763ab645fSStefan Brüns if (bits_per_word > 32) 339863ab645fSStefan Brüns return -EINVAL; 33998caab75fSGeert Uytterhoeven if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 340063ab645fSStefan Brüns return -EINVAL; 340163ab645fSStefan Brüns } 340263ab645fSStefan Brüns 340363ab645fSStefan Brüns return 0; 340463ab645fSStefan Brüns } 340563ab645fSStefan Brüns 34067d077197SDavid Brownell /** 34077d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 34087d077197SDavid Brownell * @spi: the device whose settings are being modified 34097d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 34107d077197SDavid Brownell * 34117d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 34127d077197SDavid Brownell * device doesn't work with its default. They may likewise need 34137d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 34147d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 34157d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 34167d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 34177d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 34187d077197SDavid Brownell * 34197d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 34207d077197SDavid Brownell * that the underlying controller or its driver does not support. For 34217d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 34227d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 342397d56dc6SJavier Martinez Canillas * 342497d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 34257d077197SDavid Brownell */ 34267d077197SDavid Brownell int spi_setup(struct spi_device *spi) 34277d077197SDavid Brownell { 342883596fbeSGeert Uytterhoeven unsigned bad_bits, ugly_bits; 34295ab8d262SAndy Shevchenko int status; 34307d077197SDavid Brownell 3431d962608cSDragos Bogdan /* 3432d962608cSDragos Bogdan * check mode to prevent that any two of DUAL, QUAD and NO_MOSI/MISO 3433d962608cSDragos Bogdan * are set at the same time 3434f477b7fbSwangyuhang */ 3435d962608cSDragos Bogdan if ((hweight_long(spi->mode & 3436d962608cSDragos Bogdan (SPI_TX_DUAL | SPI_TX_QUAD | SPI_NO_TX)) > 1) || 3437d962608cSDragos Bogdan (hweight_long(spi->mode & 3438d962608cSDragos Bogdan (SPI_RX_DUAL | SPI_RX_QUAD | SPI_NO_RX)) > 1)) { 3439f477b7fbSwangyuhang dev_err(&spi->dev, 3440d962608cSDragos Bogdan "setup: can not select any two of dual, quad and no-rx/tx at the same time\n"); 3441f477b7fbSwangyuhang return -EINVAL; 3442f477b7fbSwangyuhang } 3443f477b7fbSwangyuhang /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden 3444f477b7fbSwangyuhang */ 3445f477b7fbSwangyuhang if ((spi->mode & SPI_3WIRE) && (spi->mode & 34466b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 34476b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 3448f477b7fbSwangyuhang return -EINVAL; 3449e7db06b5SDavid Brownell /* help drivers fail *cleanly* when they need options 34508caab75fSGeert Uytterhoeven * that aren't supported with their current controller 3451cbaa62e0SDavid Lechner * SPI_CS_WORD has a fallback software implementation, 3452cbaa62e0SDavid Lechner * so it is ignored here. 3453e7db06b5SDavid Brownell */ 3454d962608cSDragos Bogdan bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD | 3455d962608cSDragos Bogdan SPI_NO_TX | SPI_NO_RX); 3456d61ad23cSSerge Semin /* nothing prevents from working with active-high CS in case if it 3457d61ad23cSSerge Semin * is driven by GPIO. 3458d61ad23cSSerge Semin */ 3459d61ad23cSSerge Semin if (gpio_is_valid(spi->cs_gpio)) 3460d61ad23cSSerge Semin bad_bits &= ~SPI_CS_HIGH; 346183596fbeSGeert Uytterhoeven ugly_bits = bad_bits & 34626b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 34636b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); 346483596fbeSGeert Uytterhoeven if (ugly_bits) { 346583596fbeSGeert Uytterhoeven dev_warn(&spi->dev, 346683596fbeSGeert Uytterhoeven "setup: ignoring unsupported mode bits %x\n", 346783596fbeSGeert Uytterhoeven ugly_bits); 346883596fbeSGeert Uytterhoeven spi->mode &= ~ugly_bits; 346983596fbeSGeert Uytterhoeven bad_bits &= ~ugly_bits; 347083596fbeSGeert Uytterhoeven } 3471e7db06b5SDavid Brownell if (bad_bits) { 3472eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 3473e7db06b5SDavid Brownell bad_bits); 3474e7db06b5SDavid Brownell return -EINVAL; 3475e7db06b5SDavid Brownell } 3476e7db06b5SDavid Brownell 34777d077197SDavid Brownell if (!spi->bits_per_word) 34787d077197SDavid Brownell spi->bits_per_word = 8; 34797d077197SDavid Brownell 34808caab75fSGeert Uytterhoeven status = __spi_validate_bits_per_word(spi->controller, 34818caab75fSGeert Uytterhoeven spi->bits_per_word); 34825ab8d262SAndy Shevchenko if (status) 34835ab8d262SAndy Shevchenko return status; 348463ab645fSStefan Brüns 34856820e812STudor Ambarus if (spi->controller->max_speed_hz && 34866820e812STudor Ambarus (!spi->max_speed_hz || 34876820e812STudor Ambarus spi->max_speed_hz > spi->controller->max_speed_hz)) 34888caab75fSGeert Uytterhoeven spi->max_speed_hz = spi->controller->max_speed_hz; 3489052eb2d4SAxel Lin 34904fae3a58SSerge Semin mutex_lock(&spi->controller->io_mutex); 34914fae3a58SSerge Semin 3492c914dbf8SJoe Burmeister if (spi->controller->setup) { 34938caab75fSGeert Uytterhoeven status = spi->controller->setup(spi); 3494c914dbf8SJoe Burmeister if (status) { 3495c914dbf8SJoe Burmeister mutex_unlock(&spi->controller->io_mutex); 3496c914dbf8SJoe Burmeister dev_err(&spi->controller->dev, "Failed to setup device: %d\n", 3497c914dbf8SJoe Burmeister status); 3498c914dbf8SJoe Burmeister return status; 3499c914dbf8SJoe Burmeister } 3500c914dbf8SJoe Burmeister } 35017d077197SDavid Brownell 3502d948e6caSLuhua Xu if (spi->controller->auto_runtime_pm && spi->controller->set_cs) { 3503d948e6caSLuhua Xu status = pm_runtime_get_sync(spi->controller->dev.parent); 3504d948e6caSLuhua Xu if (status < 0) { 35054fae3a58SSerge Semin mutex_unlock(&spi->controller->io_mutex); 3506d948e6caSLuhua Xu pm_runtime_put_noidle(spi->controller->dev.parent); 3507d948e6caSLuhua Xu dev_err(&spi->controller->dev, "Failed to power device: %d\n", 3508d948e6caSLuhua Xu status); 3509d948e6caSLuhua Xu return status; 3510d948e6caSLuhua Xu } 351157a94607STony Lindgren 351257a94607STony Lindgren /* 351357a94607STony Lindgren * We do not want to return positive value from pm_runtime_get, 351457a94607STony Lindgren * there are many instances of devices calling spi_setup() and 351557a94607STony Lindgren * checking for a non-zero return value instead of a negative 351657a94607STony Lindgren * return value. 351757a94607STony Lindgren */ 351857a94607STony Lindgren status = 0; 351957a94607STony Lindgren 3520d347b4aaSDavid Bauer spi_set_cs(spi, false, true); 3521d948e6caSLuhua Xu pm_runtime_mark_last_busy(spi->controller->dev.parent); 3522d948e6caSLuhua Xu pm_runtime_put_autosuspend(spi->controller->dev.parent); 3523d948e6caSLuhua Xu } else { 3524d347b4aaSDavid Bauer spi_set_cs(spi, false, true); 3525d948e6caSLuhua Xu } 3526abeedb01SFranklin S Cooper Jr 35274fae3a58SSerge Semin mutex_unlock(&spi->controller->io_mutex); 35284fae3a58SSerge Semin 3529924b5867SDouglas Anderson if (spi->rt && !spi->controller->rt) { 3530924b5867SDouglas Anderson spi->controller->rt = true; 3531924b5867SDouglas Anderson spi_set_thread_rt(spi->controller); 3532924b5867SDouglas Anderson } 3533924b5867SDouglas Anderson 35345cb4e1f3SAndy Shevchenko trace_spi_setup(spi, status); 35355cb4e1f3SAndy Shevchenko 353640b82c2dSAndy Shevchenko dev_dbg(&spi->dev, "setup mode %lu, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 353740b82c2dSAndy Shevchenko spi->mode & SPI_MODE_X_MASK, 35387d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 35397d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 35407d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 35417d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 35427d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 35437d077197SDavid Brownell status); 35447d077197SDavid Brownell 35457d077197SDavid Brownell return status; 35467d077197SDavid Brownell } 35477d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 35487d077197SDavid Brownell 35496c613f68SAlexandru Ardelean static int _spi_xfer_word_delay_update(struct spi_transfer *xfer, 35506c613f68SAlexandru Ardelean struct spi_device *spi) 35516c613f68SAlexandru Ardelean { 35526c613f68SAlexandru Ardelean int delay1, delay2; 35536c613f68SAlexandru Ardelean 35543984d39bSAlexandru Ardelean delay1 = spi_delay_to_ns(&xfer->word_delay, xfer); 35556c613f68SAlexandru Ardelean if (delay1 < 0) 35566c613f68SAlexandru Ardelean return delay1; 35576c613f68SAlexandru Ardelean 35583984d39bSAlexandru Ardelean delay2 = spi_delay_to_ns(&spi->word_delay, xfer); 35596c613f68SAlexandru Ardelean if (delay2 < 0) 35606c613f68SAlexandru Ardelean return delay2; 35616c613f68SAlexandru Ardelean 35626c613f68SAlexandru Ardelean if (delay1 < delay2) 35636c613f68SAlexandru Ardelean memcpy(&xfer->word_delay, &spi->word_delay, 35646c613f68SAlexandru Ardelean sizeof(xfer->word_delay)); 35656c613f68SAlexandru Ardelean 35666c613f68SAlexandru Ardelean return 0; 35676c613f68SAlexandru Ardelean } 35686c613f68SAlexandru Ardelean 356990808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message) 3570cf32b71eSErnst Schwab { 35718caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3572e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 35736ea31293SAtsushi Nemoto int w_size; 3574cf32b71eSErnst Schwab 357524a0013aSMark Brown if (list_empty(&message->transfers)) 357624a0013aSMark Brown return -EINVAL; 357724a0013aSMark Brown 3578cbaa62e0SDavid Lechner /* If an SPI controller does not support toggling the CS line on each 357971388b21SDavid Lechner * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO 358071388b21SDavid Lechner * for the CS line, we can emulate the CS-per-word hardware function by 3581cbaa62e0SDavid Lechner * splitting transfers into one-word transfers and ensuring that 3582cbaa62e0SDavid Lechner * cs_change is set for each transfer. 3583cbaa62e0SDavid Lechner */ 358471388b21SDavid Lechner if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || 3585f3186dd8SLinus Walleij spi->cs_gpiod || 358671388b21SDavid Lechner gpio_is_valid(spi->cs_gpio))) { 3587cbaa62e0SDavid Lechner size_t maxsize; 3588cbaa62e0SDavid Lechner int ret; 3589cbaa62e0SDavid Lechner 3590cbaa62e0SDavid Lechner maxsize = (spi->bits_per_word + 7) / 8; 3591cbaa62e0SDavid Lechner 3592cbaa62e0SDavid Lechner /* spi_split_transfers_maxsize() requires message->spi */ 3593cbaa62e0SDavid Lechner message->spi = spi; 3594cbaa62e0SDavid Lechner 3595cbaa62e0SDavid Lechner ret = spi_split_transfers_maxsize(ctlr, message, maxsize, 3596cbaa62e0SDavid Lechner GFP_KERNEL); 3597cbaa62e0SDavid Lechner if (ret) 3598cbaa62e0SDavid Lechner return ret; 3599cbaa62e0SDavid Lechner 3600cbaa62e0SDavid Lechner list_for_each_entry(xfer, &message->transfers, transfer_list) { 3601cbaa62e0SDavid Lechner /* don't change cs_change on the last entry in the list */ 3602cbaa62e0SDavid Lechner if (list_is_last(&xfer->transfer_list, &message->transfers)) 3603cbaa62e0SDavid Lechner break; 3604cbaa62e0SDavid Lechner xfer->cs_change = 1; 3605cbaa62e0SDavid Lechner } 3606cbaa62e0SDavid Lechner } 3607cbaa62e0SDavid Lechner 3608cf32b71eSErnst Schwab /* Half-duplex links include original MicroWire, and ones with 3609cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 3610cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 3611cf32b71eSErnst Schwab * software limitations. 3612cf32b71eSErnst Schwab */ 36138caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 36148caab75fSGeert Uytterhoeven (spi->mode & SPI_3WIRE)) { 36158caab75fSGeert Uytterhoeven unsigned flags = ctlr->flags; 3616cf32b71eSErnst Schwab 3617cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 3618cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 3619cf32b71eSErnst Schwab return -EINVAL; 36208caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 3621cf32b71eSErnst Schwab return -EINVAL; 36228caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 3623cf32b71eSErnst Schwab return -EINVAL; 3624cf32b71eSErnst Schwab } 3625cf32b71eSErnst Schwab } 3626cf32b71eSErnst Schwab 3627e6811d1dSLaxman Dewangan /** 3628059b8ffeSLaxman Dewangan * Set transfer bits_per_word and max speed as spi device default if 3629059b8ffeSLaxman Dewangan * it is not set for this transfer. 3630f477b7fbSwangyuhang * Set transfer tx_nbits and rx_nbits as single transfer default 3631f477b7fbSwangyuhang * (SPI_NBITS_SINGLE) if it is not set for this transfer. 3632b7bb367aSJonas Bonn * Ensure transfer word_delay is at least as long as that required by 3633b7bb367aSJonas Bonn * device itself. 3634e6811d1dSLaxman Dewangan */ 363577e80588SMartin Sperl message->frame_length = 0; 3636e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 36375d7e2b5eSMartin Sperl xfer->effective_speed_hz = 0; 3638078726ceSSourav Poddar message->frame_length += xfer->len; 3639e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 3640e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 3641a6f87fadSAxel Lin 3642a6f87fadSAxel Lin if (!xfer->speed_hz) 3643059b8ffeSLaxman Dewangan xfer->speed_hz = spi->max_speed_hz; 3644a6f87fadSAxel Lin 36458caab75fSGeert Uytterhoeven if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 36468caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 364756ede94aSGabor Juhos 36488caab75fSGeert Uytterhoeven if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 3649543bb255SStephen Warren return -EINVAL; 3650a2fd4f9fSMark Brown 36514d94bd21SIvan T. Ivanov /* 36524d94bd21SIvan T. Ivanov * SPI transfer length should be multiple of SPI word size 36534d94bd21SIvan T. Ivanov * where SPI word size should be power-of-two multiple 36544d94bd21SIvan T. Ivanov */ 36554d94bd21SIvan T. Ivanov if (xfer->bits_per_word <= 8) 36564d94bd21SIvan T. Ivanov w_size = 1; 36574d94bd21SIvan T. Ivanov else if (xfer->bits_per_word <= 16) 36584d94bd21SIvan T. Ivanov w_size = 2; 36594d94bd21SIvan T. Ivanov else 36604d94bd21SIvan T. Ivanov w_size = 4; 36614d94bd21SIvan T. Ivanov 36624d94bd21SIvan T. Ivanov /* No partial transfers accepted */ 36636ea31293SAtsushi Nemoto if (xfer->len % w_size) 36644d94bd21SIvan T. Ivanov return -EINVAL; 36654d94bd21SIvan T. Ivanov 36668caab75fSGeert Uytterhoeven if (xfer->speed_hz && ctlr->min_speed_hz && 36678caab75fSGeert Uytterhoeven xfer->speed_hz < ctlr->min_speed_hz) 3668a2fd4f9fSMark Brown return -EINVAL; 3669f477b7fbSwangyuhang 3670f477b7fbSwangyuhang if (xfer->tx_buf && !xfer->tx_nbits) 3671f477b7fbSwangyuhang xfer->tx_nbits = SPI_NBITS_SINGLE; 3672f477b7fbSwangyuhang if (xfer->rx_buf && !xfer->rx_nbits) 3673f477b7fbSwangyuhang xfer->rx_nbits = SPI_NBITS_SINGLE; 3674f477b7fbSwangyuhang /* check transfer tx/rx_nbits: 36751afd9989SGeert Uytterhoeven * 1. check the value matches one of single, dual and quad 36761afd9989SGeert Uytterhoeven * 2. check tx/rx_nbits match the mode in spi_device 3677f477b7fbSwangyuhang */ 3678db90a441SSourav Poddar if (xfer->tx_buf) { 3679d962608cSDragos Bogdan if (spi->mode & SPI_NO_TX) 3680d962608cSDragos Bogdan return -EINVAL; 3681f477b7fbSwangyuhang if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3682f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_DUAL && 3683f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_QUAD) 3684a2fd4f9fSMark Brown return -EINVAL; 3685f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 3686f477b7fbSwangyuhang !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3687f477b7fbSwangyuhang return -EINVAL; 3688f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 3689f477b7fbSwangyuhang !(spi->mode & SPI_TX_QUAD)) 3690f477b7fbSwangyuhang return -EINVAL; 3691db90a441SSourav Poddar } 3692f477b7fbSwangyuhang /* check transfer rx_nbits */ 3693db90a441SSourav Poddar if (xfer->rx_buf) { 3694d962608cSDragos Bogdan if (spi->mode & SPI_NO_RX) 3695d962608cSDragos Bogdan return -EINVAL; 3696f477b7fbSwangyuhang if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3697f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_DUAL && 3698f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_QUAD) 3699f477b7fbSwangyuhang return -EINVAL; 3700f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 3701f477b7fbSwangyuhang !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 3702f477b7fbSwangyuhang return -EINVAL; 3703f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 3704f477b7fbSwangyuhang !(spi->mode & SPI_RX_QUAD)) 3705f477b7fbSwangyuhang return -EINVAL; 3706e6811d1dSLaxman Dewangan } 3707b7bb367aSJonas Bonn 37086c613f68SAlexandru Ardelean if (_spi_xfer_word_delay_update(xfer, spi)) 37096c613f68SAlexandru Ardelean return -EINVAL; 3710e6811d1dSLaxman Dewangan } 3711e6811d1dSLaxman Dewangan 3712cf32b71eSErnst Schwab message->status = -EINPROGRESS; 371390808738SMark Brown 371490808738SMark Brown return 0; 371590808738SMark Brown } 371690808738SMark Brown 371790808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message) 371890808738SMark Brown { 37198caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3720b42faeeeSVladimir Oltean struct spi_transfer *xfer; 372190808738SMark Brown 3722b5932f5cSBoris Brezillon /* 3723b5932f5cSBoris Brezillon * Some controllers do not support doing regular SPI transfers. Return 3724b5932f5cSBoris Brezillon * ENOTSUPP when this is the case. 3725b5932f5cSBoris Brezillon */ 3726b5932f5cSBoris Brezillon if (!ctlr->transfer) 3727b5932f5cSBoris Brezillon return -ENOTSUPP; 3728b5932f5cSBoris Brezillon 372990808738SMark Brown message->spi = spi; 373090808738SMark Brown 37318caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); 3732eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); 3733eca2ebc7SMartin Sperl 373490808738SMark Brown trace_spi_message_submit(message); 373590808738SMark Brown 3736b42faeeeSVladimir Oltean if (!ctlr->ptp_sts_supported) { 3737b42faeeeSVladimir Oltean list_for_each_entry(xfer, &message->transfers, transfer_list) { 3738b42faeeeSVladimir Oltean xfer->ptp_sts_word_pre = 0; 3739b42faeeeSVladimir Oltean ptp_read_system_prets(xfer->ptp_sts); 3740b42faeeeSVladimir Oltean } 3741b42faeeeSVladimir Oltean } 3742b42faeeeSVladimir Oltean 37438caab75fSGeert Uytterhoeven return ctlr->transfer(spi, message); 3744cf32b71eSErnst Schwab } 3745cf32b71eSErnst Schwab 3746568d0697SDavid Brownell /** 3747568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 3748568d0697SDavid Brownell * @spi: device with which data will be exchanged 3749568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 3750568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 3751568d0697SDavid Brownell * 3752568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 3753568d0697SDavid Brownell * as well as from task contexts which can sleep. 3754568d0697SDavid Brownell * 3755568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 3756568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 3757568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 3758568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 3759568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 3760568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 3761568d0697SDavid Brownell * core or controller driver code. 3762568d0697SDavid Brownell * 3763568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 3764568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 3765568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 3766568d0697SDavid Brownell * time requirements, for example. 3767568d0697SDavid Brownell * 3768568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 3769568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 3770568d0697SDavid Brownell * Until returning from the associated message completion callback, 3771568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 3772568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 3773568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 377497d56dc6SJavier Martinez Canillas * 377597d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3776568d0697SDavid Brownell */ 3777568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 3778568d0697SDavid Brownell { 37798caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3780cf32b71eSErnst Schwab int ret; 3781cf32b71eSErnst Schwab unsigned long flags; 3782568d0697SDavid Brownell 378390808738SMark Brown ret = __spi_validate(spi, message); 378490808738SMark Brown if (ret != 0) 378590808738SMark Brown return ret; 378690808738SMark Brown 37878caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3788568d0697SDavid Brownell 37898caab75fSGeert Uytterhoeven if (ctlr->bus_lock_flag) 3790cf32b71eSErnst Schwab ret = -EBUSY; 3791cf32b71eSErnst Schwab else 3792cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3793568d0697SDavid Brownell 37948caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3795cf32b71eSErnst Schwab 3796cf32b71eSErnst Schwab return ret; 3797568d0697SDavid Brownell } 3798568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 3799568d0697SDavid Brownell 3800cf32b71eSErnst Schwab /** 3801cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 3802cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3803cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 3804cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 3805cf32b71eSErnst Schwab * 3806cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 3807cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 3808cf32b71eSErnst Schwab * 3809cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 3810cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 3811cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 3812cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 3813cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 3814cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 3815cf32b71eSErnst Schwab * core or controller driver code. 3816cf32b71eSErnst Schwab * 3817cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 3818cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 3819cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 3820cf32b71eSErnst Schwab * time requirements, for example. 3821cf32b71eSErnst Schwab * 3822cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 3823cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 3824cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 3825cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 3826cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 3827cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 382897d56dc6SJavier Martinez Canillas * 382997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3830cf32b71eSErnst Schwab */ 3831cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message) 3832cf32b71eSErnst Schwab { 38338caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3834cf32b71eSErnst Schwab int ret; 3835cf32b71eSErnst Schwab unsigned long flags; 3836cf32b71eSErnst Schwab 383790808738SMark Brown ret = __spi_validate(spi, message); 383890808738SMark Brown if (ret != 0) 383990808738SMark Brown return ret; 384090808738SMark Brown 38418caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3842cf32b71eSErnst Schwab 3843cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3844cf32b71eSErnst Schwab 38458caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3846cf32b71eSErnst Schwab 3847cf32b71eSErnst Schwab return ret; 3848cf32b71eSErnst Schwab 3849cf32b71eSErnst Schwab } 3850cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked); 3851cf32b71eSErnst Schwab 38527d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 38537d077197SDavid Brownell 38548caab75fSGeert Uytterhoeven /* Utility methods for SPI protocol drivers, layered on 38557d077197SDavid Brownell * top of the core. Some other utility methods are defined as 38567d077197SDavid Brownell * inline functions. 38577d077197SDavid Brownell */ 38587d077197SDavid Brownell 38595d870c8eSAndrew Morton static void spi_complete(void *arg) 38605d870c8eSAndrew Morton { 38615d870c8eSAndrew Morton complete(arg); 38625d870c8eSAndrew Morton } 38635d870c8eSAndrew Morton 3864ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message) 3865cf32b71eSErnst Schwab { 3866cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 3867cf32b71eSErnst Schwab int status; 38688caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 38690461a414SMark Brown unsigned long flags; 38700461a414SMark Brown 38710461a414SMark Brown status = __spi_validate(spi, message); 38720461a414SMark Brown if (status != 0) 38730461a414SMark Brown return status; 3874cf32b71eSErnst Schwab 3875cf32b71eSErnst Schwab message->complete = spi_complete; 3876cf32b71eSErnst Schwab message->context = &done; 38770461a414SMark Brown message->spi = spi; 3878cf32b71eSErnst Schwab 38798caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); 3880eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); 3881eca2ebc7SMartin Sperl 38820461a414SMark Brown /* If we're not using the legacy transfer method then we will 38830461a414SMark Brown * try to transfer in the calling context so special case. 38840461a414SMark Brown * This code would be less tricky if we could remove the 38850461a414SMark Brown * support for driver implemented message queues. 38860461a414SMark Brown */ 38878caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 38888caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 38890461a414SMark Brown 38900461a414SMark Brown trace_spi_message_submit(message); 38910461a414SMark Brown 38920461a414SMark Brown status = __spi_queued_transfer(spi, message, false); 38930461a414SMark Brown 38948caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 38950461a414SMark Brown } else { 3896cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 38970461a414SMark Brown } 3898cf32b71eSErnst Schwab 3899cf32b71eSErnst Schwab if (status == 0) { 39000461a414SMark Brown /* Push out the messages in the calling context if we 39010461a414SMark Brown * can. 39020461a414SMark Brown */ 39038caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 39048caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3905eca2ebc7SMartin Sperl spi_sync_immediate); 3906eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, 3907eca2ebc7SMartin Sperl spi_sync_immediate); 39088caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, false); 3909eca2ebc7SMartin Sperl } 39100461a414SMark Brown 3911cf32b71eSErnst Schwab wait_for_completion(&done); 3912cf32b71eSErnst Schwab status = message->status; 3913cf32b71eSErnst Schwab } 3914cf32b71eSErnst Schwab message->context = NULL; 3915cf32b71eSErnst Schwab return status; 3916cf32b71eSErnst Schwab } 3917cf32b71eSErnst Schwab 39188ae12a0dSDavid Brownell /** 39198ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 39208ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 39218ae12a0dSDavid Brownell * @message: describes the data transfers 392233e34dc6SDavid Brownell * Context: can sleep 39238ae12a0dSDavid Brownell * 39248ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 39258ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 39268ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 39278ae12a0dSDavid Brownell * 39288ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 39298ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 39308ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 39318ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 39328ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 39338ae12a0dSDavid Brownell * 39340c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 39350c868461SDavid Brownell * message will not be freed before this call returns. 39360c868461SDavid Brownell * 393797d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 39388ae12a0dSDavid Brownell */ 39398ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 39408ae12a0dSDavid Brownell { 3941ef4d96ecSMark Brown int ret; 3942ef4d96ecSMark Brown 39438caab75fSGeert Uytterhoeven mutex_lock(&spi->controller->bus_lock_mutex); 3944ef4d96ecSMark Brown ret = __spi_sync(spi, message); 39458caab75fSGeert Uytterhoeven mutex_unlock(&spi->controller->bus_lock_mutex); 3946ef4d96ecSMark Brown 3947ef4d96ecSMark Brown return ret; 39488ae12a0dSDavid Brownell } 39498ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 39508ae12a0dSDavid Brownell 3951cf32b71eSErnst Schwab /** 3952cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 3953cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3954cf32b71eSErnst Schwab * @message: describes the data transfers 3955cf32b71eSErnst Schwab * Context: can sleep 3956cf32b71eSErnst Schwab * 3957cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3958cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 3959cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 3960cf32b71eSErnst Schwab * 3961cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 396225985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 3963cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 3964cf32b71eSErnst Schwab * 396597d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3966cf32b71eSErnst Schwab */ 3967cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 3968cf32b71eSErnst Schwab { 3969ef4d96ecSMark Brown return __spi_sync(spi, message); 3970cf32b71eSErnst Schwab } 3971cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 3972cf32b71eSErnst Schwab 3973cf32b71eSErnst Schwab /** 3974cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 39758caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that should be locked for exclusive bus access 3976cf32b71eSErnst Schwab * Context: can sleep 3977cf32b71eSErnst Schwab * 3978cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3979cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3980cf32b71eSErnst Schwab * 3981cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 3982cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 3983cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 3984cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 3985cf32b71eSErnst Schwab * 398697d56dc6SJavier Martinez Canillas * Return: always zero. 3987cf32b71eSErnst Schwab */ 39888caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr) 3989cf32b71eSErnst Schwab { 3990cf32b71eSErnst Schwab unsigned long flags; 3991cf32b71eSErnst Schwab 39928caab75fSGeert Uytterhoeven mutex_lock(&ctlr->bus_lock_mutex); 3993cf32b71eSErnst Schwab 39948caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 39958caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 1; 39968caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3997cf32b71eSErnst Schwab 3998cf32b71eSErnst Schwab /* mutex remains locked until spi_bus_unlock is called */ 3999cf32b71eSErnst Schwab 4000cf32b71eSErnst Schwab return 0; 4001cf32b71eSErnst Schwab } 4002cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 4003cf32b71eSErnst Schwab 4004cf32b71eSErnst Schwab /** 4005cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 40068caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that was locked for exclusive bus access 4007cf32b71eSErnst Schwab * Context: can sleep 4008cf32b71eSErnst Schwab * 4009cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 4010cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 4011cf32b71eSErnst Schwab * 4012cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 4013cf32b71eSErnst Schwab * call. 4014cf32b71eSErnst Schwab * 401597d56dc6SJavier Martinez Canillas * Return: always zero. 4016cf32b71eSErnst Schwab */ 40178caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr) 4018cf32b71eSErnst Schwab { 40198caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 4020cf32b71eSErnst Schwab 40218caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->bus_lock_mutex); 4022cf32b71eSErnst Schwab 4023cf32b71eSErnst Schwab return 0; 4024cf32b71eSErnst Schwab } 4025cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 4026cf32b71eSErnst Schwab 4027a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 4028a9948b61SDavid Brownell #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 40298ae12a0dSDavid Brownell 40308ae12a0dSDavid Brownell static u8 *buf; 40318ae12a0dSDavid Brownell 40328ae12a0dSDavid Brownell /** 40338ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 40348ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 40358ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 40368ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 403727570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 403827570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 403933e34dc6SDavid Brownell * Context: can sleep 40408ae12a0dSDavid Brownell * 40418ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 40428ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 40438ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 4044b885244eSDavid Brownell * This call may only be used from a context that may sleep. 40458ae12a0dSDavid Brownell * 4046c373643bSMark Brown * Parameters to this routine are always copied using a small buffer. 404733e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 40480c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 404997d56dc6SJavier Martinez Canillas * 405097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 40518ae12a0dSDavid Brownell */ 40528ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 40530c4a1590SMark Brown const void *txbuf, unsigned n_tx, 40540c4a1590SMark Brown void *rxbuf, unsigned n_rx) 40558ae12a0dSDavid Brownell { 4056068f4070SDavid Brownell static DEFINE_MUTEX(lock); 40578ae12a0dSDavid Brownell 40588ae12a0dSDavid Brownell int status; 40598ae12a0dSDavid Brownell struct spi_message message; 4060bdff549eSDavid Brownell struct spi_transfer x[2]; 40618ae12a0dSDavid Brownell u8 *local_buf; 40628ae12a0dSDavid Brownell 4063b3a223eeSMark Brown /* Use preallocated DMA-safe buffer if we can. We can't avoid 4064b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 4065b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 4066b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 40678ae12a0dSDavid Brownell */ 4068b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 40692cd94c8aSMark Brown local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 40702cd94c8aSMark Brown GFP_KERNEL | GFP_DMA); 4071b3a223eeSMark Brown if (!local_buf) 4072b3a223eeSMark Brown return -ENOMEM; 4073b3a223eeSMark Brown } else { 4074b3a223eeSMark Brown local_buf = buf; 4075b3a223eeSMark Brown } 40768ae12a0dSDavid Brownell 40778275c642SVitaly Wool spi_message_init(&message); 40785fe5f05eSJingoo Han memset(x, 0, sizeof(x)); 4079bdff549eSDavid Brownell if (n_tx) { 4080bdff549eSDavid Brownell x[0].len = n_tx; 4081bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 4082bdff549eSDavid Brownell } 4083bdff549eSDavid Brownell if (n_rx) { 4084bdff549eSDavid Brownell x[1].len = n_rx; 4085bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 4086bdff549eSDavid Brownell } 40878275c642SVitaly Wool 40888ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 4089bdff549eSDavid Brownell x[0].tx_buf = local_buf; 4090bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 40918ae12a0dSDavid Brownell 40928ae12a0dSDavid Brownell /* do the i/o */ 40938ae12a0dSDavid Brownell status = spi_sync(spi, &message); 40949b938b74SMarc Pignat if (status == 0) 4095bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 40968ae12a0dSDavid Brownell 4097bdff549eSDavid Brownell if (x[0].tx_buf == buf) 4098068f4070SDavid Brownell mutex_unlock(&lock); 40998ae12a0dSDavid Brownell else 41008ae12a0dSDavid Brownell kfree(local_buf); 41018ae12a0dSDavid Brownell 41028ae12a0dSDavid Brownell return status; 41038ae12a0dSDavid Brownell } 41048ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 41058ae12a0dSDavid Brownell 41068ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 41078ae12a0dSDavid Brownell 41085f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF) 4109ce79d54aSPantelis Antoniou /* must call put_device() when done with returned spi_device device */ 41105f143af7SMarco Felsch struct spi_device *of_find_spi_device_by_node(struct device_node *node) 4111ce79d54aSPantelis Antoniou { 4112cfba5de9SSuzuki K Poulose struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node); 4113cfba5de9SSuzuki K Poulose 4114ce79d54aSPantelis Antoniou return dev ? to_spi_device(dev) : NULL; 4115ce79d54aSPantelis Antoniou } 41165f143af7SMarco Felsch EXPORT_SYMBOL_GPL(of_find_spi_device_by_node); 41175f143af7SMarco Felsch #endif /* IS_ENABLED(CONFIG_OF) */ 4118ce79d54aSPantelis Antoniou 41195f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF_DYNAMIC) 41208caab75fSGeert Uytterhoeven /* the spi controllers are not using spi_bus, so we find it with another way */ 41218caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 4122ce79d54aSPantelis Antoniou { 4123ce79d54aSPantelis Antoniou struct device *dev; 4124ce79d54aSPantelis Antoniou 4125cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_master_class, node); 41266c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 4127cfba5de9SSuzuki K Poulose dev = class_find_device_by_of_node(&spi_slave_class, node); 4128ce79d54aSPantelis Antoniou if (!dev) 4129ce79d54aSPantelis Antoniou return NULL; 4130ce79d54aSPantelis Antoniou 4131ce79d54aSPantelis Antoniou /* reference got in class_find_device */ 41328caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 4133ce79d54aSPantelis Antoniou } 4134ce79d54aSPantelis Antoniou 4135ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action, 4136ce79d54aSPantelis Antoniou void *arg) 4137ce79d54aSPantelis Antoniou { 4138ce79d54aSPantelis Antoniou struct of_reconfig_data *rd = arg; 41398caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 4140ce79d54aSPantelis Antoniou struct spi_device *spi; 4141ce79d54aSPantelis Antoniou 4142ce79d54aSPantelis Antoniou switch (of_reconfig_get_state_change(action, arg)) { 4143ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 41448caab75fSGeert Uytterhoeven ctlr = of_find_spi_controller_by_node(rd->dn->parent); 41458caab75fSGeert Uytterhoeven if (ctlr == NULL) 4146ce79d54aSPantelis Antoniou return NOTIFY_OK; /* not for us */ 4147ce79d54aSPantelis Antoniou 4148bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 41498caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4150bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4151bd6c1644SGeert Uytterhoeven } 4152bd6c1644SGeert Uytterhoeven 41538caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, rd->dn); 41548caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 4155ce79d54aSPantelis Antoniou 4156ce79d54aSPantelis Antoniou if (IS_ERR(spi)) { 415725c56c88SRob Herring pr_err("%s: failed to create for '%pOF'\n", 415825c56c88SRob Herring __func__, rd->dn); 4159e0af98a7SRalf Ramsauer of_node_clear_flag(rd->dn, OF_POPULATED); 4160ce79d54aSPantelis Antoniou return notifier_from_errno(PTR_ERR(spi)); 4161ce79d54aSPantelis Antoniou } 4162ce79d54aSPantelis Antoniou break; 4163ce79d54aSPantelis Antoniou 4164ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 4165bd6c1644SGeert Uytterhoeven /* already depopulated? */ 4166bd6c1644SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 4167bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 4168bd6c1644SGeert Uytterhoeven 4169ce79d54aSPantelis Antoniou /* find our device by node */ 4170ce79d54aSPantelis Antoniou spi = of_find_spi_device_by_node(rd->dn); 4171ce79d54aSPantelis Antoniou if (spi == NULL) 4172ce79d54aSPantelis Antoniou return NOTIFY_OK; /* no? not meant for us */ 4173ce79d54aSPantelis Antoniou 4174ce79d54aSPantelis Antoniou /* unregister takes one ref away */ 4175ce79d54aSPantelis Antoniou spi_unregister_device(spi); 4176ce79d54aSPantelis Antoniou 4177ce79d54aSPantelis Antoniou /* and put the reference of the find */ 4178ce79d54aSPantelis Antoniou put_device(&spi->dev); 4179ce79d54aSPantelis Antoniou break; 4180ce79d54aSPantelis Antoniou } 4181ce79d54aSPantelis Antoniou 4182ce79d54aSPantelis Antoniou return NOTIFY_OK; 4183ce79d54aSPantelis Antoniou } 4184ce79d54aSPantelis Antoniou 4185ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = { 4186ce79d54aSPantelis Antoniou .notifier_call = of_spi_notify, 4187ce79d54aSPantelis Antoniou }; 4188ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4189ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier; 4190ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 4191ce79d54aSPantelis Antoniou 41927f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI) 41938caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data) 41947f24467fSOctavian Purdila { 41957f24467fSOctavian Purdila return ACPI_COMPANION(dev->parent) == data; 41967f24467fSOctavian Purdila } 41977f24467fSOctavian Purdila 41988caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 41997f24467fSOctavian Purdila { 42007f24467fSOctavian Purdila struct device *dev; 42017f24467fSOctavian Purdila 42027f24467fSOctavian Purdila dev = class_find_device(&spi_master_class, NULL, adev, 42038caab75fSGeert Uytterhoeven spi_acpi_controller_match); 42046c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 42056c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, adev, 42068caab75fSGeert Uytterhoeven spi_acpi_controller_match); 42077f24467fSOctavian Purdila if (!dev) 42087f24467fSOctavian Purdila return NULL; 42097f24467fSOctavian Purdila 42108caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 42117f24467fSOctavian Purdila } 42127f24467fSOctavian Purdila 42137f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 42147f24467fSOctavian Purdila { 42157f24467fSOctavian Purdila struct device *dev; 42167f24467fSOctavian Purdila 421700500147SSuzuki K Poulose dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev); 42185b16668eSWolfram Sang return to_spi_device(dev); 42197f24467fSOctavian Purdila } 42207f24467fSOctavian Purdila 42217f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 42227f24467fSOctavian Purdila void *arg) 42237f24467fSOctavian Purdila { 42247f24467fSOctavian Purdila struct acpi_device *adev = arg; 42258caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 42267f24467fSOctavian Purdila struct spi_device *spi; 42277f24467fSOctavian Purdila 42287f24467fSOctavian Purdila switch (value) { 42297f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_ADD: 42308caab75fSGeert Uytterhoeven ctlr = acpi_spi_find_controller_by_adev(adev->parent); 42318caab75fSGeert Uytterhoeven if (!ctlr) 42327f24467fSOctavian Purdila break; 42337f24467fSOctavian Purdila 42348caab75fSGeert Uytterhoeven acpi_register_spi_device(ctlr, adev); 42358caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 42367f24467fSOctavian Purdila break; 42377f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_REMOVE: 42387f24467fSOctavian Purdila if (!acpi_device_enumerated(adev)) 42397f24467fSOctavian Purdila break; 42407f24467fSOctavian Purdila 42417f24467fSOctavian Purdila spi = acpi_spi_find_device_by_adev(adev); 42427f24467fSOctavian Purdila if (!spi) 42437f24467fSOctavian Purdila break; 42447f24467fSOctavian Purdila 42457f24467fSOctavian Purdila spi_unregister_device(spi); 42467f24467fSOctavian Purdila put_device(&spi->dev); 42477f24467fSOctavian Purdila break; 42487f24467fSOctavian Purdila } 42497f24467fSOctavian Purdila 42507f24467fSOctavian Purdila return NOTIFY_OK; 42517f24467fSOctavian Purdila } 42527f24467fSOctavian Purdila 42537f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = { 42547f24467fSOctavian Purdila .notifier_call = acpi_spi_notify, 42557f24467fSOctavian Purdila }; 42567f24467fSOctavian Purdila #else 42577f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier; 42587f24467fSOctavian Purdila #endif 42597f24467fSOctavian Purdila 42608ae12a0dSDavid Brownell static int __init spi_init(void) 42618ae12a0dSDavid Brownell { 4262b885244eSDavid Brownell int status; 42638ae12a0dSDavid Brownell 4264e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 4265b885244eSDavid Brownell if (!buf) { 4266b885244eSDavid Brownell status = -ENOMEM; 4267b885244eSDavid Brownell goto err0; 42688ae12a0dSDavid Brownell } 4269b885244eSDavid Brownell 4270b885244eSDavid Brownell status = bus_register(&spi_bus_type); 4271b885244eSDavid Brownell if (status < 0) 4272b885244eSDavid Brownell goto err1; 4273b885244eSDavid Brownell 4274b885244eSDavid Brownell status = class_register(&spi_master_class); 4275b885244eSDavid Brownell if (status < 0) 4276b885244eSDavid Brownell goto err2; 4277ce79d54aSPantelis Antoniou 42786c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 42796c364062SGeert Uytterhoeven status = class_register(&spi_slave_class); 42806c364062SGeert Uytterhoeven if (status < 0) 42816c364062SGeert Uytterhoeven goto err3; 42826c364062SGeert Uytterhoeven } 42836c364062SGeert Uytterhoeven 42845267720eSFabio Estevam if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 4285ce79d54aSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 42867f24467fSOctavian Purdila if (IS_ENABLED(CONFIG_ACPI)) 42877f24467fSOctavian Purdila WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 4288ce79d54aSPantelis Antoniou 4289b885244eSDavid Brownell return 0; 4290b885244eSDavid Brownell 42916c364062SGeert Uytterhoeven err3: 42926c364062SGeert Uytterhoeven class_unregister(&spi_master_class); 4293b885244eSDavid Brownell err2: 4294b885244eSDavid Brownell bus_unregister(&spi_bus_type); 4295b885244eSDavid Brownell err1: 4296b885244eSDavid Brownell kfree(buf); 4297b885244eSDavid Brownell buf = NULL; 4298b885244eSDavid Brownell err0: 4299b885244eSDavid Brownell return status; 4300b885244eSDavid Brownell } 4301b885244eSDavid Brownell 43028ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(), 43038ae12a0dSDavid Brownell * but even essential drivers wait till later 4304b885244eSDavid Brownell * 4305b885244eSDavid Brownell * REVISIT only boardinfo really needs static linking. the rest (device and 4306b885244eSDavid Brownell * driver registration) _could_ be dynamically linked (modular) ... costs 4307b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 43088ae12a0dSDavid Brownell */ 4309673c0c00SDavid Brownell postcore_initcall(spi_init); 4310