1b445bfcbSMarco Felsch // SPDX-License-Identifier: GPL-2.0-or-later 2787f4889SMark Brown // SPI init/core code 3787f4889SMark Brown // 4787f4889SMark Brown // Copyright (C) 2005 David Brownell 5787f4889SMark Brown // Copyright (C) 2008 Secret Lab Technologies Ltd. 68ae12a0dSDavid Brownell 78ae12a0dSDavid Brownell #include <linux/kernel.h> 88ae12a0dSDavid Brownell #include <linux/device.h> 98ae12a0dSDavid Brownell #include <linux/init.h> 108ae12a0dSDavid Brownell #include <linux/cache.h> 1199adef31SMark Brown #include <linux/dma-mapping.h> 1299adef31SMark Brown #include <linux/dmaengine.h> 1394040828SMatthias Kaehlcke #include <linux/mutex.h> 142b7a32f7SSinan Akman #include <linux/of_device.h> 15d57a4282SGrant Likely #include <linux/of_irq.h> 1686be408bSSylwester Nawrocki #include <linux/clk/clk-conf.h> 175a0e3ad6STejun Heo #include <linux/slab.h> 18e0626e38SAnton Vorontsov #include <linux/mod_devicetable.h> 198ae12a0dSDavid Brownell #include <linux/spi/spi.h> 20b5932f5cSBoris Brezillon #include <linux/spi/spi-mem.h> 2174317984SJean-Christophe PLAGNIOL-VILLARD #include <linux/of_gpio.h> 22f3186dd8SLinus Walleij #include <linux/gpio/consumer.h> 233ae22e8cSMark Brown #include <linux/pm_runtime.h> 24f48c767cSUlf Hansson #include <linux/pm_domain.h> 25826cf175SDmitry Torokhov #include <linux/property.h> 26025ed130SPaul Gortmaker #include <linux/export.h> 278bd75c77SClark Williams #include <linux/sched/rt.h> 28ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h> 29ffbbdd21SLinus Walleij #include <linux/delay.h> 30ffbbdd21SLinus Walleij #include <linux/kthread.h> 3164bee4d2SMika Westerberg #include <linux/ioport.h> 3264bee4d2SMika Westerberg #include <linux/acpi.h> 33b1b8153cSVignesh R #include <linux/highmem.h> 349b61e302SSuniel Mahesh #include <linux/idr.h> 358a2e487eSLukas Wunner #include <linux/platform_data/x86/apple.h> 368ae12a0dSDavid Brownell 3756ec1978SMark Brown #define CREATE_TRACE_POINTS 3856ec1978SMark Brown #include <trace/events/spi.h> 39ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start); 40ca1438dcSArnd Bergmann EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop); 419b61e302SSuniel Mahesh 4246336966SBoris Brezillon #include "internals.h" 4346336966SBoris Brezillon 449b61e302SSuniel Mahesh static DEFINE_IDR(spi_master_idr); 4556ec1978SMark Brown 468ae12a0dSDavid Brownell static void spidev_release(struct device *dev) 478ae12a0dSDavid Brownell { 480ffa0285SHans-Peter Nilsson struct spi_device *spi = to_spi_device(dev); 498ae12a0dSDavid Brownell 508caab75fSGeert Uytterhoeven /* spi controllers may cleanup for released devices */ 518caab75fSGeert Uytterhoeven if (spi->controller->cleanup) 528caab75fSGeert Uytterhoeven spi->controller->cleanup(spi); 538ae12a0dSDavid Brownell 548caab75fSGeert Uytterhoeven spi_controller_put(spi->controller); 555039563eSTrent Piepho kfree(spi->driver_override); 5607a389feSRoman Tereshonkov kfree(spi); 578ae12a0dSDavid Brownell } 588ae12a0dSDavid Brownell 598ae12a0dSDavid Brownell static ssize_t 608ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf) 618ae12a0dSDavid Brownell { 628ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 638c4ff6d0SZhang Rui int len; 648c4ff6d0SZhang Rui 658c4ff6d0SZhang Rui len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 668c4ff6d0SZhang Rui if (len != -ENODEV) 678c4ff6d0SZhang Rui return len; 688ae12a0dSDavid Brownell 69d8e328b3SGrant Likely return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 708ae12a0dSDavid Brownell } 71aa7da564SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias); 728ae12a0dSDavid Brownell 735039563eSTrent Piepho static ssize_t driver_override_store(struct device *dev, 745039563eSTrent Piepho struct device_attribute *a, 755039563eSTrent Piepho const char *buf, size_t count) 765039563eSTrent Piepho { 775039563eSTrent Piepho struct spi_device *spi = to_spi_device(dev); 785039563eSTrent Piepho const char *end = memchr(buf, '\n', count); 795039563eSTrent Piepho const size_t len = end ? end - buf : count; 805039563eSTrent Piepho const char *driver_override, *old; 815039563eSTrent Piepho 825039563eSTrent Piepho /* We need to keep extra room for a newline when displaying value */ 835039563eSTrent Piepho if (len >= (PAGE_SIZE - 1)) 845039563eSTrent Piepho return -EINVAL; 855039563eSTrent Piepho 865039563eSTrent Piepho driver_override = kstrndup(buf, len, GFP_KERNEL); 875039563eSTrent Piepho if (!driver_override) 885039563eSTrent Piepho return -ENOMEM; 895039563eSTrent Piepho 905039563eSTrent Piepho device_lock(dev); 915039563eSTrent Piepho old = spi->driver_override; 925039563eSTrent Piepho if (len) { 935039563eSTrent Piepho spi->driver_override = driver_override; 945039563eSTrent Piepho } else { 955039563eSTrent Piepho /* Emptry string, disable driver override */ 965039563eSTrent Piepho spi->driver_override = NULL; 975039563eSTrent Piepho kfree(driver_override); 985039563eSTrent Piepho } 995039563eSTrent Piepho device_unlock(dev); 1005039563eSTrent Piepho kfree(old); 1015039563eSTrent Piepho 1025039563eSTrent Piepho return count; 1035039563eSTrent Piepho } 1045039563eSTrent Piepho 1055039563eSTrent Piepho static ssize_t driver_override_show(struct device *dev, 1065039563eSTrent Piepho struct device_attribute *a, char *buf) 1075039563eSTrent Piepho { 1085039563eSTrent Piepho const struct spi_device *spi = to_spi_device(dev); 1095039563eSTrent Piepho ssize_t len; 1105039563eSTrent Piepho 1115039563eSTrent Piepho device_lock(dev); 1125039563eSTrent Piepho len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : ""); 1135039563eSTrent Piepho device_unlock(dev); 1145039563eSTrent Piepho return len; 1155039563eSTrent Piepho } 1165039563eSTrent Piepho static DEVICE_ATTR_RW(driver_override); 1175039563eSTrent Piepho 118eca2ebc7SMartin Sperl #define SPI_STATISTICS_ATTRS(field, file) \ 1198caab75fSGeert Uytterhoeven static ssize_t spi_controller_##field##_show(struct device *dev, \ 120eca2ebc7SMartin Sperl struct device_attribute *attr, \ 121eca2ebc7SMartin Sperl char *buf) \ 122eca2ebc7SMartin Sperl { \ 1238caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, \ 1248caab75fSGeert Uytterhoeven struct spi_controller, dev); \ 1258caab75fSGeert Uytterhoeven return spi_statistics_##field##_show(&ctlr->statistics, buf); \ 126eca2ebc7SMartin Sperl } \ 1278caab75fSGeert Uytterhoeven static struct device_attribute dev_attr_spi_controller_##field = { \ 128ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 1298caab75fSGeert Uytterhoeven .show = spi_controller_##field##_show, \ 130eca2ebc7SMartin Sperl }; \ 131eca2ebc7SMartin Sperl static ssize_t spi_device_##field##_show(struct device *dev, \ 132eca2ebc7SMartin Sperl struct device_attribute *attr, \ 133eca2ebc7SMartin Sperl char *buf) \ 134eca2ebc7SMartin Sperl { \ 135d1eba93bSGeliang Tang struct spi_device *spi = to_spi_device(dev); \ 136eca2ebc7SMartin Sperl return spi_statistics_##field##_show(&spi->statistics, buf); \ 137eca2ebc7SMartin Sperl } \ 138eca2ebc7SMartin Sperl static struct device_attribute dev_attr_spi_device_##field = { \ 139ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 140eca2ebc7SMartin Sperl .show = spi_device_##field##_show, \ 141eca2ebc7SMartin Sperl } 142eca2ebc7SMartin Sperl 143eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \ 144eca2ebc7SMartin Sperl static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ 145eca2ebc7SMartin Sperl char *buf) \ 146eca2ebc7SMartin Sperl { \ 147eca2ebc7SMartin Sperl unsigned long flags; \ 148eca2ebc7SMartin Sperl ssize_t len; \ 149eca2ebc7SMartin Sperl spin_lock_irqsave(&stat->lock, flags); \ 150eca2ebc7SMartin Sperl len = sprintf(buf, format_string, stat->field); \ 151eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stat->lock, flags); \ 152eca2ebc7SMartin Sperl return len; \ 153eca2ebc7SMartin Sperl } \ 154eca2ebc7SMartin Sperl SPI_STATISTICS_ATTRS(name, file) 155eca2ebc7SMartin Sperl 156eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW(field, format_string) \ 157eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ 158eca2ebc7SMartin Sperl field, format_string) 159eca2ebc7SMartin Sperl 160eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(messages, "%lu"); 161eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(transfers, "%lu"); 162eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(errors, "%lu"); 163eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(timedout, "%lu"); 164eca2ebc7SMartin Sperl 165eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync, "%lu"); 166eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu"); 167eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_async, "%lu"); 168eca2ebc7SMartin Sperl 169eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes, "%llu"); 170eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_rx, "%llu"); 171eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_tx, "%llu"); 172eca2ebc7SMartin Sperl 1736b7bc061SMartin Sperl #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ 1746b7bc061SMartin Sperl SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ 1756b7bc061SMartin Sperl "transfer_bytes_histo_" number, \ 1766b7bc061SMartin Sperl transfer_bytes_histo[index], "%lu") 1776b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); 1786b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); 1796b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); 1806b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); 1816b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); 1826b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); 1836b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); 1846b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); 1856b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); 1866b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); 1876b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); 1886b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); 1896b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); 1906b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); 1916b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); 1926b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); 1936b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); 1946b7bc061SMartin Sperl 195d9f12122SMartin Sperl SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu"); 196d9f12122SMartin Sperl 197aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = { 198aa7da564SGreg Kroah-Hartman &dev_attr_modalias.attr, 1995039563eSTrent Piepho &dev_attr_driver_override.attr, 200aa7da564SGreg Kroah-Hartman NULL, 2018ae12a0dSDavid Brownell }; 202eca2ebc7SMartin Sperl 203eca2ebc7SMartin Sperl static const struct attribute_group spi_dev_group = { 204eca2ebc7SMartin Sperl .attrs = spi_dev_attrs, 205eca2ebc7SMartin Sperl }; 206eca2ebc7SMartin Sperl 207eca2ebc7SMartin Sperl static struct attribute *spi_device_statistics_attrs[] = { 208eca2ebc7SMartin Sperl &dev_attr_spi_device_messages.attr, 209eca2ebc7SMartin Sperl &dev_attr_spi_device_transfers.attr, 210eca2ebc7SMartin Sperl &dev_attr_spi_device_errors.attr, 211eca2ebc7SMartin Sperl &dev_attr_spi_device_timedout.attr, 212eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync.attr, 213eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync_immediate.attr, 214eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_async.attr, 215eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes.attr, 216eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_rx.attr, 217eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_tx.attr, 2186b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo0.attr, 2196b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo1.attr, 2206b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo2.attr, 2216b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo3.attr, 2226b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo4.attr, 2236b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo5.attr, 2246b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo6.attr, 2256b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo7.attr, 2266b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo8.attr, 2276b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo9.attr, 2286b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo10.attr, 2296b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo11.attr, 2306b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo12.attr, 2316b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo13.attr, 2326b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo14.attr, 2336b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo15.attr, 2346b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo16.attr, 235d9f12122SMartin Sperl &dev_attr_spi_device_transfers_split_maxsize.attr, 236eca2ebc7SMartin Sperl NULL, 237eca2ebc7SMartin Sperl }; 238eca2ebc7SMartin Sperl 239eca2ebc7SMartin Sperl static const struct attribute_group spi_device_statistics_group = { 240eca2ebc7SMartin Sperl .name = "statistics", 241eca2ebc7SMartin Sperl .attrs = spi_device_statistics_attrs, 242eca2ebc7SMartin Sperl }; 243eca2ebc7SMartin Sperl 244eca2ebc7SMartin Sperl static const struct attribute_group *spi_dev_groups[] = { 245eca2ebc7SMartin Sperl &spi_dev_group, 246eca2ebc7SMartin Sperl &spi_device_statistics_group, 247eca2ebc7SMartin Sperl NULL, 248eca2ebc7SMartin Sperl }; 249eca2ebc7SMartin Sperl 2508caab75fSGeert Uytterhoeven static struct attribute *spi_controller_statistics_attrs[] = { 2518caab75fSGeert Uytterhoeven &dev_attr_spi_controller_messages.attr, 2528caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers.attr, 2538caab75fSGeert Uytterhoeven &dev_attr_spi_controller_errors.attr, 2548caab75fSGeert Uytterhoeven &dev_attr_spi_controller_timedout.attr, 2558caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync.attr, 2568caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync_immediate.attr, 2578caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_async.attr, 2588caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes.attr, 2598caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_rx.attr, 2608caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_tx.attr, 2618caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo0.attr, 2628caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo1.attr, 2638caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo2.attr, 2648caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo3.attr, 2658caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo4.attr, 2668caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo5.attr, 2678caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo6.attr, 2688caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo7.attr, 2698caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo8.attr, 2708caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo9.attr, 2718caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo10.attr, 2728caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo11.attr, 2738caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo12.attr, 2748caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo13.attr, 2758caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo14.attr, 2768caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo15.attr, 2778caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo16.attr, 2788caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers_split_maxsize.attr, 279eca2ebc7SMartin Sperl NULL, 280eca2ebc7SMartin Sperl }; 281eca2ebc7SMartin Sperl 2828caab75fSGeert Uytterhoeven static const struct attribute_group spi_controller_statistics_group = { 283eca2ebc7SMartin Sperl .name = "statistics", 2848caab75fSGeert Uytterhoeven .attrs = spi_controller_statistics_attrs, 285eca2ebc7SMartin Sperl }; 286eca2ebc7SMartin Sperl 287eca2ebc7SMartin Sperl static const struct attribute_group *spi_master_groups[] = { 2888caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 289eca2ebc7SMartin Sperl NULL, 290eca2ebc7SMartin Sperl }; 291eca2ebc7SMartin Sperl 292eca2ebc7SMartin Sperl void spi_statistics_add_transfer_stats(struct spi_statistics *stats, 293eca2ebc7SMartin Sperl struct spi_transfer *xfer, 2948caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 295eca2ebc7SMartin Sperl { 296eca2ebc7SMartin Sperl unsigned long flags; 2976b7bc061SMartin Sperl int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; 2986b7bc061SMartin Sperl 2996b7bc061SMartin Sperl if (l2len < 0) 3006b7bc061SMartin Sperl l2len = 0; 301eca2ebc7SMartin Sperl 302eca2ebc7SMartin Sperl spin_lock_irqsave(&stats->lock, flags); 303eca2ebc7SMartin Sperl 304eca2ebc7SMartin Sperl stats->transfers++; 3056b7bc061SMartin Sperl stats->transfer_bytes_histo[l2len]++; 306eca2ebc7SMartin Sperl 307eca2ebc7SMartin Sperl stats->bytes += xfer->len; 308eca2ebc7SMartin Sperl if ((xfer->tx_buf) && 3098caab75fSGeert Uytterhoeven (xfer->tx_buf != ctlr->dummy_tx)) 310eca2ebc7SMartin Sperl stats->bytes_tx += xfer->len; 311eca2ebc7SMartin Sperl if ((xfer->rx_buf) && 3128caab75fSGeert Uytterhoeven (xfer->rx_buf != ctlr->dummy_rx)) 313eca2ebc7SMartin Sperl stats->bytes_rx += xfer->len; 314eca2ebc7SMartin Sperl 315eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stats->lock, flags); 316eca2ebc7SMartin Sperl } 317eca2ebc7SMartin Sperl EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats); 3188ae12a0dSDavid Brownell 3198ae12a0dSDavid Brownell /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, 3208ae12a0dSDavid Brownell * and the sysfs version makes coldplug work too. 3218ae12a0dSDavid Brownell */ 3228ae12a0dSDavid Brownell 32375368bf6SAnton Vorontsov static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, 32475368bf6SAnton Vorontsov const struct spi_device *sdev) 32575368bf6SAnton Vorontsov { 32675368bf6SAnton Vorontsov while (id->name[0]) { 32775368bf6SAnton Vorontsov if (!strcmp(sdev->modalias, id->name)) 32875368bf6SAnton Vorontsov return id; 32975368bf6SAnton Vorontsov id++; 33075368bf6SAnton Vorontsov } 33175368bf6SAnton Vorontsov return NULL; 33275368bf6SAnton Vorontsov } 33375368bf6SAnton Vorontsov 33475368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 33575368bf6SAnton Vorontsov { 33675368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 33775368bf6SAnton Vorontsov 33875368bf6SAnton Vorontsov return spi_match_id(sdrv->id_table, sdev); 33975368bf6SAnton Vorontsov } 34075368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id); 34175368bf6SAnton Vorontsov 3428ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 3438ae12a0dSDavid Brownell { 3448ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 34575368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(drv); 34675368bf6SAnton Vorontsov 3475039563eSTrent Piepho /* Check override first, and if set, only use the named driver */ 3485039563eSTrent Piepho if (spi->driver_override) 3495039563eSTrent Piepho return strcmp(spi->driver_override, drv->name) == 0; 3505039563eSTrent Piepho 3512b7a32f7SSinan Akman /* Attempt an OF style match */ 3522b7a32f7SSinan Akman if (of_driver_match_device(dev, drv)) 3532b7a32f7SSinan Akman return 1; 3542b7a32f7SSinan Akman 35564bee4d2SMika Westerberg /* Then try ACPI */ 35664bee4d2SMika Westerberg if (acpi_driver_match_device(dev, drv)) 35764bee4d2SMika Westerberg return 1; 35864bee4d2SMika Westerberg 35975368bf6SAnton Vorontsov if (sdrv->id_table) 36075368bf6SAnton Vorontsov return !!spi_match_id(sdrv->id_table, spi); 3618ae12a0dSDavid Brownell 36235f74fcaSKay Sievers return strcmp(spi->modalias, drv->name) == 0; 3638ae12a0dSDavid Brownell } 3648ae12a0dSDavid Brownell 3657eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 3668ae12a0dSDavid Brownell { 3678ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 3688c4ff6d0SZhang Rui int rc; 3698c4ff6d0SZhang Rui 3708c4ff6d0SZhang Rui rc = acpi_device_uevent_modalias(dev, env); 3718c4ff6d0SZhang Rui if (rc != -ENODEV) 3728c4ff6d0SZhang Rui return rc; 3738ae12a0dSDavid Brownell 3742856670fSAndy Shevchenko return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 3758ae12a0dSDavid Brownell } 3768ae12a0dSDavid Brownell 3778ae12a0dSDavid Brownell struct bus_type spi_bus_type = { 3788ae12a0dSDavid Brownell .name = "spi", 379aa7da564SGreg Kroah-Hartman .dev_groups = spi_dev_groups, 3808ae12a0dSDavid Brownell .match = spi_match_device, 3818ae12a0dSDavid Brownell .uevent = spi_uevent, 3828ae12a0dSDavid Brownell }; 3838ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_bus_type); 3848ae12a0dSDavid Brownell 385b885244eSDavid Brownell 386b885244eSDavid Brownell static int spi_drv_probe(struct device *dev) 387b885244eSDavid Brownell { 388b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 38944af7927SJon Hunter struct spi_device *spi = to_spi_device(dev); 39033cf00e5SMika Westerberg int ret; 391b885244eSDavid Brownell 39286be408bSSylwester Nawrocki ret = of_clk_set_defaults(dev->of_node, false); 39386be408bSSylwester Nawrocki if (ret) 39486be408bSSylwester Nawrocki return ret; 39586be408bSSylwester Nawrocki 39644af7927SJon Hunter if (dev->of_node) { 39744af7927SJon Hunter spi->irq = of_irq_get(dev->of_node, 0); 39844af7927SJon Hunter if (spi->irq == -EPROBE_DEFER) 39944af7927SJon Hunter return -EPROBE_DEFER; 40044af7927SJon Hunter if (spi->irq < 0) 40144af7927SJon Hunter spi->irq = 0; 40244af7927SJon Hunter } 40344af7927SJon Hunter 404676e7c25SUlf Hansson ret = dev_pm_domain_attach(dev, true); 40571f277a7SUlf Hansson if (ret) 40671f277a7SUlf Hansson return ret; 40771f277a7SUlf Hansson 40844af7927SJon Hunter ret = sdrv->probe(spi); 40933cf00e5SMika Westerberg if (ret) 410676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 41133cf00e5SMika Westerberg 41233cf00e5SMika Westerberg return ret; 413b885244eSDavid Brownell } 414b885244eSDavid Brownell 415b885244eSDavid Brownell static int spi_drv_remove(struct device *dev) 416b885244eSDavid Brownell { 417b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 41833cf00e5SMika Westerberg int ret; 419b885244eSDavid Brownell 420aec35f4eSJean Delvare ret = sdrv->remove(to_spi_device(dev)); 421676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 42233cf00e5SMika Westerberg 42333cf00e5SMika Westerberg return ret; 424b885244eSDavid Brownell } 425b885244eSDavid Brownell 426b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev) 427b885244eSDavid Brownell { 428b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 429b885244eSDavid Brownell 430b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 431b885244eSDavid Brownell } 432b885244eSDavid Brownell 43333e34dc6SDavid Brownell /** 434ca5d2485SAndrew F. Davis * __spi_register_driver - register a SPI driver 43588c9321dSThierry Reding * @owner: owner module of the driver to register 43633e34dc6SDavid Brownell * @sdrv: the driver to register 43733e34dc6SDavid Brownell * Context: can sleep 43897d56dc6SJavier Martinez Canillas * 43997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 44033e34dc6SDavid Brownell */ 441ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) 442b885244eSDavid Brownell { 443ca5d2485SAndrew F. Davis sdrv->driver.owner = owner; 444b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 445b885244eSDavid Brownell if (sdrv->probe) 446b885244eSDavid Brownell sdrv->driver.probe = spi_drv_probe; 447b885244eSDavid Brownell if (sdrv->remove) 448b885244eSDavid Brownell sdrv->driver.remove = spi_drv_remove; 449b885244eSDavid Brownell if (sdrv->shutdown) 450b885244eSDavid Brownell sdrv->driver.shutdown = spi_drv_shutdown; 451b885244eSDavid Brownell return driver_register(&sdrv->driver); 452b885244eSDavid Brownell } 453ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver); 454b885244eSDavid Brownell 4558ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 4568ae12a0dSDavid Brownell 4578ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that 4588caab75fSGeert Uytterhoeven * would make them board-specific. Similarly with SPI controller drivers. 4598ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 4608ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 4618ae12a0dSDavid Brownell */ 4628ae12a0dSDavid Brownell 4638ae12a0dSDavid Brownell struct boardinfo { 4648ae12a0dSDavid Brownell struct list_head list; 4652b9603a0SFeng Tang struct spi_board_info board_info; 4668ae12a0dSDavid Brownell }; 4678ae12a0dSDavid Brownell 4688ae12a0dSDavid Brownell static LIST_HEAD(board_list); 4698caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list); 4702b9603a0SFeng Tang 4712b9603a0SFeng Tang /* 4722b9603a0SFeng Tang * Used to protect add/del opertion for board_info list and 4738caab75fSGeert Uytterhoeven * spi_controller list, and their matching process 4749b61e302SSuniel Mahesh * also used to protect object of type struct idr 4752b9603a0SFeng Tang */ 47694040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 4778ae12a0dSDavid Brownell 478dc87c98eSGrant Likely /** 479dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 4808caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 481dc87c98eSGrant Likely * Context: can sleep 482dc87c98eSGrant Likely * 483dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 484dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 485dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 486dc87c98eSGrant Likely * spi_add_device() on it. 487dc87c98eSGrant Likely * 488dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 4898caab75fSGeert Uytterhoeven * spi_device structure to add it to the SPI controller. If the caller 490dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 491dc87c98eSGrant Likely * call spi_dev_put() on it. 492dc87c98eSGrant Likely * 49397d56dc6SJavier Martinez Canillas * Return: a pointer to the new device, or NULL. 494dc87c98eSGrant Likely */ 4958caab75fSGeert Uytterhoeven struct spi_device *spi_alloc_device(struct spi_controller *ctlr) 496dc87c98eSGrant Likely { 497dc87c98eSGrant Likely struct spi_device *spi; 498dc87c98eSGrant Likely 4998caab75fSGeert Uytterhoeven if (!spi_controller_get(ctlr)) 500dc87c98eSGrant Likely return NULL; 501dc87c98eSGrant Likely 5025fe5f05eSJingoo Han spi = kzalloc(sizeof(*spi), GFP_KERNEL); 503dc87c98eSGrant Likely if (!spi) { 5048caab75fSGeert Uytterhoeven spi_controller_put(ctlr); 505dc87c98eSGrant Likely return NULL; 506dc87c98eSGrant Likely } 507dc87c98eSGrant Likely 5088caab75fSGeert Uytterhoeven spi->master = spi->controller = ctlr; 5098caab75fSGeert Uytterhoeven spi->dev.parent = &ctlr->dev; 510dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 511dc87c98eSGrant Likely spi->dev.release = spidev_release; 512446411e1SAndreas Larsson spi->cs_gpio = -ENOENT; 513eca2ebc7SMartin Sperl 514eca2ebc7SMartin Sperl spin_lock_init(&spi->statistics.lock); 515eca2ebc7SMartin Sperl 516dc87c98eSGrant Likely device_initialize(&spi->dev); 517dc87c98eSGrant Likely return spi; 518dc87c98eSGrant Likely } 519dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device); 520dc87c98eSGrant Likely 521e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi) 522e13ac47bSJarkko Nikula { 523e13ac47bSJarkko Nikula struct acpi_device *adev = ACPI_COMPANION(&spi->dev); 524e13ac47bSJarkko Nikula 525e13ac47bSJarkko Nikula if (adev) { 526e13ac47bSJarkko Nikula dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); 527e13ac47bSJarkko Nikula return; 528e13ac47bSJarkko Nikula } 529e13ac47bSJarkko Nikula 5308caab75fSGeert Uytterhoeven dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), 531e13ac47bSJarkko Nikula spi->chip_select); 532e13ac47bSJarkko Nikula } 533e13ac47bSJarkko Nikula 534b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data) 535b6fb8d3aSMika Westerberg { 536b6fb8d3aSMika Westerberg struct spi_device *spi = to_spi_device(dev); 537b6fb8d3aSMika Westerberg struct spi_device *new_spi = data; 538b6fb8d3aSMika Westerberg 5398caab75fSGeert Uytterhoeven if (spi->controller == new_spi->controller && 540b6fb8d3aSMika Westerberg spi->chip_select == new_spi->chip_select) 541b6fb8d3aSMika Westerberg return -EBUSY; 542b6fb8d3aSMika Westerberg return 0; 543b6fb8d3aSMika Westerberg } 544b6fb8d3aSMika Westerberg 545dc87c98eSGrant Likely /** 546dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 547dc87c98eSGrant Likely * @spi: spi_device to register 548dc87c98eSGrant Likely * 549dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 550dc87c98eSGrant Likely * spi_alloc_device can be added onto the spi bus with this function. 551dc87c98eSGrant Likely * 55297d56dc6SJavier Martinez Canillas * Return: 0 on success; negative errno on failure 553dc87c98eSGrant Likely */ 554dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi) 555dc87c98eSGrant Likely { 556e48880e0SDavid Brownell static DEFINE_MUTEX(spi_add_lock); 5578caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 5588caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 559dc87c98eSGrant Likely int status; 560dc87c98eSGrant Likely 561dc87c98eSGrant Likely /* Chipselects are numbered 0..max; validate. */ 5628caab75fSGeert Uytterhoeven if (spi->chip_select >= ctlr->num_chipselect) { 5638caab75fSGeert Uytterhoeven dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 5648caab75fSGeert Uytterhoeven ctlr->num_chipselect); 565dc87c98eSGrant Likely return -EINVAL; 566dc87c98eSGrant Likely } 567dc87c98eSGrant Likely 568dc87c98eSGrant Likely /* Set the bus ID string */ 569e13ac47bSJarkko Nikula spi_dev_set_name(spi); 570e48880e0SDavid Brownell 571e48880e0SDavid Brownell /* We need to make sure there's no other device with this 572e48880e0SDavid Brownell * chipselect **BEFORE** we call setup(), else we'll trash 573e48880e0SDavid Brownell * its configuration. Lock against concurrent add() calls. 574e48880e0SDavid Brownell */ 575e48880e0SDavid Brownell mutex_lock(&spi_add_lock); 576e48880e0SDavid Brownell 577b6fb8d3aSMika Westerberg status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); 578b6fb8d3aSMika Westerberg if (status) { 579e48880e0SDavid Brownell dev_err(dev, "chipselect %d already in use\n", 580e48880e0SDavid Brownell spi->chip_select); 581e48880e0SDavid Brownell goto done; 582e48880e0SDavid Brownell } 583e48880e0SDavid Brownell 584f3186dd8SLinus Walleij /* Descriptors take precedence */ 585f3186dd8SLinus Walleij if (ctlr->cs_gpiods) 586f3186dd8SLinus Walleij spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; 587f3186dd8SLinus Walleij else if (ctlr->cs_gpios) 5888caab75fSGeert Uytterhoeven spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; 58974317984SJean-Christophe PLAGNIOL-VILLARD 590e48880e0SDavid Brownell /* Drivers may modify this initial i/o setup, but will 591e48880e0SDavid Brownell * normally rely on the device being setup. Devices 592e48880e0SDavid Brownell * using SPI_CS_HIGH can't coexist well otherwise... 593e48880e0SDavid Brownell */ 5947d077197SDavid Brownell status = spi_setup(spi); 595dc87c98eSGrant Likely if (status < 0) { 596eb288a1fSLinus Walleij dev_err(dev, "can't setup %s, status %d\n", 597eb288a1fSLinus Walleij dev_name(&spi->dev), status); 598e48880e0SDavid Brownell goto done; 599dc87c98eSGrant Likely } 600dc87c98eSGrant Likely 601e48880e0SDavid Brownell /* Device may be bound to an active driver when this returns */ 602dc87c98eSGrant Likely status = device_add(&spi->dev); 603e48880e0SDavid Brownell if (status < 0) 604eb288a1fSLinus Walleij dev_err(dev, "can't add %s, status %d\n", 605eb288a1fSLinus Walleij dev_name(&spi->dev), status); 606e48880e0SDavid Brownell else 60735f74fcaSKay Sievers dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 608e48880e0SDavid Brownell 609e48880e0SDavid Brownell done: 610e48880e0SDavid Brownell mutex_unlock(&spi_add_lock); 611e48880e0SDavid Brownell return status; 612dc87c98eSGrant Likely } 613dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device); 6148ae12a0dSDavid Brownell 61533e34dc6SDavid Brownell /** 61633e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 6178caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 61833e34dc6SDavid Brownell * @chip: Describes the SPI device 61933e34dc6SDavid Brownell * Context: can sleep 62033e34dc6SDavid Brownell * 62133e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 6228ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 6238ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 6248ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 6258ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 626082c8cb4SDavid Brownell * 62797d56dc6SJavier Martinez Canillas * Return: the new device, or NULL. 6288ae12a0dSDavid Brownell */ 6298caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr, 630e9d5a461SAdrian Bunk struct spi_board_info *chip) 6318ae12a0dSDavid Brownell { 6328ae12a0dSDavid Brownell struct spi_device *proxy; 6338ae12a0dSDavid Brownell int status; 6348ae12a0dSDavid Brownell 635082c8cb4SDavid Brownell /* NOTE: caller did any chip->bus_num checks necessary. 636082c8cb4SDavid Brownell * 637082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 638082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 639082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 640082c8cb4SDavid Brownell */ 641082c8cb4SDavid Brownell 6428caab75fSGeert Uytterhoeven proxy = spi_alloc_device(ctlr); 643dc87c98eSGrant Likely if (!proxy) 6448ae12a0dSDavid Brownell return NULL; 6458ae12a0dSDavid Brownell 646102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 647102eb975SGrant Likely 6488ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 6498ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 650980a01c9SDavid Brownell proxy->mode = chip->mode; 6518ae12a0dSDavid Brownell proxy->irq = chip->irq; 652102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 6538ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 6548ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 6558ae12a0dSDavid Brownell proxy->controller_state = NULL; 6568ae12a0dSDavid Brownell 657826cf175SDmitry Torokhov if (chip->properties) { 658826cf175SDmitry Torokhov status = device_add_properties(&proxy->dev, chip->properties); 659826cf175SDmitry Torokhov if (status) { 6608caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 661826cf175SDmitry Torokhov "failed to add properties to '%s': %d\n", 662826cf175SDmitry Torokhov chip->modalias, status); 663826cf175SDmitry Torokhov goto err_dev_put; 664826cf175SDmitry Torokhov } 6658ae12a0dSDavid Brownell } 666dc87c98eSGrant Likely 667826cf175SDmitry Torokhov status = spi_add_device(proxy); 668826cf175SDmitry Torokhov if (status < 0) 669826cf175SDmitry Torokhov goto err_remove_props; 670826cf175SDmitry Torokhov 671dc87c98eSGrant Likely return proxy; 672826cf175SDmitry Torokhov 673826cf175SDmitry Torokhov err_remove_props: 674826cf175SDmitry Torokhov if (chip->properties) 675826cf175SDmitry Torokhov device_remove_properties(&proxy->dev); 676826cf175SDmitry Torokhov err_dev_put: 677826cf175SDmitry Torokhov spi_dev_put(proxy); 678826cf175SDmitry Torokhov return NULL; 679dc87c98eSGrant Likely } 6808ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 6818ae12a0dSDavid Brownell 6823b1884c2SGeert Uytterhoeven /** 6833b1884c2SGeert Uytterhoeven * spi_unregister_device - unregister a single SPI device 6843b1884c2SGeert Uytterhoeven * @spi: spi_device to unregister 6853b1884c2SGeert Uytterhoeven * 6863b1884c2SGeert Uytterhoeven * Start making the passed SPI device vanish. Normally this would be handled 6878caab75fSGeert Uytterhoeven * by spi_unregister_controller(). 6883b1884c2SGeert Uytterhoeven */ 6893b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi) 6903b1884c2SGeert Uytterhoeven { 691bd6c1644SGeert Uytterhoeven if (!spi) 692bd6c1644SGeert Uytterhoeven return; 693bd6c1644SGeert Uytterhoeven 6948324147fSJohan Hovold if (spi->dev.of_node) { 695bd6c1644SGeert Uytterhoeven of_node_clear_flag(spi->dev.of_node, OF_POPULATED); 6968324147fSJohan Hovold of_node_put(spi->dev.of_node); 6978324147fSJohan Hovold } 6987f24467fSOctavian Purdila if (ACPI_COMPANION(&spi->dev)) 6997f24467fSOctavian Purdila acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); 7003b1884c2SGeert Uytterhoeven device_unregister(&spi->dev); 7013b1884c2SGeert Uytterhoeven } 7023b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device); 7033b1884c2SGeert Uytterhoeven 7048caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, 7052b9603a0SFeng Tang struct spi_board_info *bi) 7062b9603a0SFeng Tang { 7072b9603a0SFeng Tang struct spi_device *dev; 7082b9603a0SFeng Tang 7098caab75fSGeert Uytterhoeven if (ctlr->bus_num != bi->bus_num) 7102b9603a0SFeng Tang return; 7112b9603a0SFeng Tang 7128caab75fSGeert Uytterhoeven dev = spi_new_device(ctlr, bi); 7132b9603a0SFeng Tang if (!dev) 7148caab75fSGeert Uytterhoeven dev_err(ctlr->dev.parent, "can't create new device for %s\n", 7152b9603a0SFeng Tang bi->modalias); 7162b9603a0SFeng Tang } 7172b9603a0SFeng Tang 71833e34dc6SDavid Brownell /** 71933e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 72033e34dc6SDavid Brownell * @info: array of chip descriptors 72133e34dc6SDavid Brownell * @n: how many descriptors are provided 72233e34dc6SDavid Brownell * Context: can sleep 72333e34dc6SDavid Brownell * 7248ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 7258ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 7268ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 7278ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 7288ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 7298ae12a0dSDavid Brownell * 7308ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 7318ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 7328ae12a0dSDavid Brownell * would naturally declare its SPI devices. 7338ae12a0dSDavid Brownell * 7348ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 7358ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 736826cf175SDmitry Torokhov * Device properties are deep-copied though. 73797d56dc6SJavier Martinez Canillas * 73897d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 7398ae12a0dSDavid Brownell */ 740fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n) 7418ae12a0dSDavid Brownell { 7428ae12a0dSDavid Brownell struct boardinfo *bi; 7432b9603a0SFeng Tang int i; 7448ae12a0dSDavid Brownell 745c7908a37SXiubo Li if (!n) 746f974cf57SDmitry Torokhov return 0; 747c7908a37SXiubo Li 748f9bdb7fdSMarkus Elfring bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); 7498ae12a0dSDavid Brownell if (!bi) 7508ae12a0dSDavid Brownell return -ENOMEM; 7518ae12a0dSDavid Brownell 7522b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 7538caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 7542b9603a0SFeng Tang 7552b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 756826cf175SDmitry Torokhov if (info->properties) { 757826cf175SDmitry Torokhov bi->board_info.properties = 758826cf175SDmitry Torokhov property_entries_dup(info->properties); 759826cf175SDmitry Torokhov if (IS_ERR(bi->board_info.properties)) 760826cf175SDmitry Torokhov return PTR_ERR(bi->board_info.properties); 761826cf175SDmitry Torokhov } 762826cf175SDmitry Torokhov 76394040828SMatthias Kaehlcke mutex_lock(&board_lock); 7648ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 7658caab75fSGeert Uytterhoeven list_for_each_entry(ctlr, &spi_controller_list, list) 7668caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, 7678caab75fSGeert Uytterhoeven &bi->board_info); 76894040828SMatthias Kaehlcke mutex_unlock(&board_lock); 7692b9603a0SFeng Tang } 7702b9603a0SFeng Tang 7718ae12a0dSDavid Brownell return 0; 7728ae12a0dSDavid Brownell } 7738ae12a0dSDavid Brownell 7748ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 7758ae12a0dSDavid Brownell 776b158935fSMark Brown static void spi_set_cs(struct spi_device *spi, bool enable) 777b158935fSMark Brown { 778b158935fSMark Brown if (spi->mode & SPI_CS_HIGH) 779b158935fSMark Brown enable = !enable; 780b158935fSMark Brown 781f3186dd8SLinus Walleij if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { 782f3186dd8SLinus Walleij /* 783f3186dd8SLinus Walleij * Honour the SPI_NO_CS flag and invert the enable line, as 784f3186dd8SLinus Walleij * active low is default for SPI. Execution paths that handle 785f3186dd8SLinus Walleij * polarity inversion in gpiolib (such as device tree) will 786f3186dd8SLinus Walleij * enforce active high using the SPI_CS_HIGH resulting in a 787f3186dd8SLinus Walleij * double inversion through the code above. 788f3186dd8SLinus Walleij */ 789f3186dd8SLinus Walleij if (!(spi->mode & SPI_NO_CS)) { 790f3186dd8SLinus Walleij if (spi->cs_gpiod) 79128f7604fSFelix Fietkau gpiod_set_value_cansleep(spi->cs_gpiod, 79228f7604fSFelix Fietkau !enable); 793f3186dd8SLinus Walleij else 79428f7604fSFelix Fietkau gpio_set_value_cansleep(spi->cs_gpio, !enable); 795f3186dd8SLinus Walleij } 7968eee6b9dSThor Thayer /* Some SPI masters need both GPIO CS & slave_select */ 7978caab75fSGeert Uytterhoeven if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && 7988caab75fSGeert Uytterhoeven spi->controller->set_cs) 7998caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 8008caab75fSGeert Uytterhoeven } else if (spi->controller->set_cs) { 8018caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 8028eee6b9dSThor Thayer } 803b158935fSMark Brown } 804b158935fSMark Brown 8052de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA 80646336966SBoris Brezillon int spi_map_buf(struct spi_controller *ctlr, struct device *dev, 8076ad45a27SMark Brown struct sg_table *sgt, void *buf, size_t len, 8086ad45a27SMark Brown enum dma_data_direction dir) 8096ad45a27SMark Brown { 8106ad45a27SMark Brown const bool vmalloced_buf = is_vmalloc_addr(buf); 811df88e91bSAndy Shevchenko unsigned int max_seg_size = dma_get_max_seg_size(dev); 812b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM 813b1b8153cSVignesh R const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && 814b1b8153cSVignesh R (unsigned long)buf < (PKMAP_BASE + 815b1b8153cSVignesh R (LAST_PKMAP * PAGE_SIZE))); 816b1b8153cSVignesh R #else 817b1b8153cSVignesh R const bool kmap_buf = false; 818b1b8153cSVignesh R #endif 81965598c13SAndrew Gabbasov int desc_len; 82065598c13SAndrew Gabbasov int sgs; 8216ad45a27SMark Brown struct page *vm_page; 8228dd4a016SJuan Gutierrez struct scatterlist *sg; 8236ad45a27SMark Brown void *sg_buf; 8246ad45a27SMark Brown size_t min; 8256ad45a27SMark Brown int i, ret; 8266ad45a27SMark Brown 827b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 828df88e91bSAndy Shevchenko desc_len = min_t(int, max_seg_size, PAGE_SIZE); 82965598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); 8300569a88fSVignesh R } else if (virt_addr_valid(buf)) { 8318caab75fSGeert Uytterhoeven desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); 83265598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len, desc_len); 8330569a88fSVignesh R } else { 8340569a88fSVignesh R return -EINVAL; 83565598c13SAndrew Gabbasov } 83665598c13SAndrew Gabbasov 8376ad45a27SMark Brown ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); 8386ad45a27SMark Brown if (ret != 0) 8396ad45a27SMark Brown return ret; 8406ad45a27SMark Brown 8418dd4a016SJuan Gutierrez sg = &sgt->sgl[0]; 8426ad45a27SMark Brown for (i = 0; i < sgs; i++) { 8436ad45a27SMark Brown 844b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 845ce99319aSMaxime Chevallier /* 846ce99319aSMaxime Chevallier * Next scatterlist entry size is the minimum between 847ce99319aSMaxime Chevallier * the desc_len and the remaining buffer length that 848ce99319aSMaxime Chevallier * fits in a page. 849ce99319aSMaxime Chevallier */ 850ce99319aSMaxime Chevallier min = min_t(size_t, desc_len, 851ce99319aSMaxime Chevallier min_t(size_t, len, 852ce99319aSMaxime Chevallier PAGE_SIZE - offset_in_page(buf))); 853b1b8153cSVignesh R if (vmalloced_buf) 8546ad45a27SMark Brown vm_page = vmalloc_to_page(buf); 855b1b8153cSVignesh R else 856b1b8153cSVignesh R vm_page = kmap_to_page(buf); 8576ad45a27SMark Brown if (!vm_page) { 8586ad45a27SMark Brown sg_free_table(sgt); 8596ad45a27SMark Brown return -ENOMEM; 8606ad45a27SMark Brown } 8618dd4a016SJuan Gutierrez sg_set_page(sg, vm_page, 862c1aefbddSCharles Keepax min, offset_in_page(buf)); 8636ad45a27SMark Brown } else { 86465598c13SAndrew Gabbasov min = min_t(size_t, len, desc_len); 8656ad45a27SMark Brown sg_buf = buf; 8668dd4a016SJuan Gutierrez sg_set_buf(sg, sg_buf, min); 8676ad45a27SMark Brown } 8686ad45a27SMark Brown 8696ad45a27SMark Brown buf += min; 8706ad45a27SMark Brown len -= min; 8718dd4a016SJuan Gutierrez sg = sg_next(sg); 8726ad45a27SMark Brown } 8736ad45a27SMark Brown 8746ad45a27SMark Brown ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); 87589e4b66aSGeert Uytterhoeven if (!ret) 87689e4b66aSGeert Uytterhoeven ret = -ENOMEM; 8776ad45a27SMark Brown if (ret < 0) { 8786ad45a27SMark Brown sg_free_table(sgt); 8796ad45a27SMark Brown return ret; 8806ad45a27SMark Brown } 8816ad45a27SMark Brown 8826ad45a27SMark Brown sgt->nents = ret; 8836ad45a27SMark Brown 8846ad45a27SMark Brown return 0; 8856ad45a27SMark Brown } 8866ad45a27SMark Brown 88746336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, 8886ad45a27SMark Brown struct sg_table *sgt, enum dma_data_direction dir) 8896ad45a27SMark Brown { 8906ad45a27SMark Brown if (sgt->orig_nents) { 8916ad45a27SMark Brown dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); 8926ad45a27SMark Brown sg_free_table(sgt); 8936ad45a27SMark Brown } 8946ad45a27SMark Brown } 8956ad45a27SMark Brown 8968caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 89799adef31SMark Brown { 89899adef31SMark Brown struct device *tx_dev, *rx_dev; 89999adef31SMark Brown struct spi_transfer *xfer; 9006ad45a27SMark Brown int ret; 9013a2eba9bSMark Brown 9028caab75fSGeert Uytterhoeven if (!ctlr->can_dma) 90399adef31SMark Brown return 0; 90499adef31SMark Brown 9058caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 9068caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 907c37f45b5SLeilk Liu else 9088caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 909c37f45b5SLeilk Liu 9108caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 9118caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 912c37f45b5SLeilk Liu else 9138caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 91499adef31SMark Brown 91599adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9168caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 91799adef31SMark Brown continue; 91899adef31SMark Brown 91999adef31SMark Brown if (xfer->tx_buf != NULL) { 9208caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, 9216ad45a27SMark Brown (void *)xfer->tx_buf, xfer->len, 92299adef31SMark Brown DMA_TO_DEVICE); 9236ad45a27SMark Brown if (ret != 0) 9246ad45a27SMark Brown return ret; 92599adef31SMark Brown } 92699adef31SMark Brown 92799adef31SMark Brown if (xfer->rx_buf != NULL) { 9288caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, 92999adef31SMark Brown xfer->rx_buf, xfer->len, 93099adef31SMark Brown DMA_FROM_DEVICE); 9316ad45a27SMark Brown if (ret != 0) { 9328caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, 9336ad45a27SMark Brown DMA_TO_DEVICE); 9346ad45a27SMark Brown return ret; 93599adef31SMark Brown } 93699adef31SMark Brown } 93799adef31SMark Brown } 93899adef31SMark Brown 9398caab75fSGeert Uytterhoeven ctlr->cur_msg_mapped = true; 94099adef31SMark Brown 94199adef31SMark Brown return 0; 94299adef31SMark Brown } 94399adef31SMark Brown 9448caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg) 94599adef31SMark Brown { 94699adef31SMark Brown struct spi_transfer *xfer; 94799adef31SMark Brown struct device *tx_dev, *rx_dev; 94899adef31SMark Brown 9498caab75fSGeert Uytterhoeven if (!ctlr->cur_msg_mapped || !ctlr->can_dma) 95099adef31SMark Brown return 0; 95199adef31SMark Brown 9528caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 9538caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 954c37f45b5SLeilk Liu else 9558caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 956c37f45b5SLeilk Liu 9578caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 9588caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 959c37f45b5SLeilk Liu else 9608caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 96199adef31SMark Brown 96299adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9638caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 96499adef31SMark Brown continue; 96599adef31SMark Brown 9668caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 9678caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 96899adef31SMark Brown } 96999adef31SMark Brown 97099adef31SMark Brown return 0; 97199adef31SMark Brown } 9722de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */ 9738caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr, 9742de440f5SGeert Uytterhoeven struct spi_message *msg) 9752de440f5SGeert Uytterhoeven { 9762de440f5SGeert Uytterhoeven return 0; 9772de440f5SGeert Uytterhoeven } 9782de440f5SGeert Uytterhoeven 9798caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr, 9802de440f5SGeert Uytterhoeven struct spi_message *msg) 9812de440f5SGeert Uytterhoeven { 9822de440f5SGeert Uytterhoeven return 0; 9832de440f5SGeert Uytterhoeven } 9842de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */ 9852de440f5SGeert Uytterhoeven 9868caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr, 9874b786458SMartin Sperl struct spi_message *msg) 9884b786458SMartin Sperl { 9894b786458SMartin Sperl struct spi_transfer *xfer; 9904b786458SMartin Sperl 9914b786458SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9924b786458SMartin Sperl /* 9934b786458SMartin Sperl * Restore the original value of tx_buf or rx_buf if they are 9944b786458SMartin Sperl * NULL. 9954b786458SMartin Sperl */ 9968caab75fSGeert Uytterhoeven if (xfer->tx_buf == ctlr->dummy_tx) 9974b786458SMartin Sperl xfer->tx_buf = NULL; 9988caab75fSGeert Uytterhoeven if (xfer->rx_buf == ctlr->dummy_rx) 9994b786458SMartin Sperl xfer->rx_buf = NULL; 10004b786458SMartin Sperl } 10014b786458SMartin Sperl 10028caab75fSGeert Uytterhoeven return __spi_unmap_msg(ctlr, msg); 10034b786458SMartin Sperl } 10044b786458SMartin Sperl 10058caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 10062de440f5SGeert Uytterhoeven { 10072de440f5SGeert Uytterhoeven struct spi_transfer *xfer; 10082de440f5SGeert Uytterhoeven void *tmp; 10092de440f5SGeert Uytterhoeven unsigned int max_tx, max_rx; 10102de440f5SGeert Uytterhoeven 10118caab75fSGeert Uytterhoeven if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) { 10122de440f5SGeert Uytterhoeven max_tx = 0; 10132de440f5SGeert Uytterhoeven max_rx = 0; 10142de440f5SGeert Uytterhoeven 10152de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, transfer_list) { 10168caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && 10172de440f5SGeert Uytterhoeven !xfer->tx_buf) 10182de440f5SGeert Uytterhoeven max_tx = max(xfer->len, max_tx); 10198caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && 10202de440f5SGeert Uytterhoeven !xfer->rx_buf) 10212de440f5SGeert Uytterhoeven max_rx = max(xfer->len, max_rx); 10222de440f5SGeert Uytterhoeven } 10232de440f5SGeert Uytterhoeven 10242de440f5SGeert Uytterhoeven if (max_tx) { 10258caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_tx, max_tx, 10262de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 10272de440f5SGeert Uytterhoeven if (!tmp) 10282de440f5SGeert Uytterhoeven return -ENOMEM; 10298caab75fSGeert Uytterhoeven ctlr->dummy_tx = tmp; 10302de440f5SGeert Uytterhoeven memset(tmp, 0, max_tx); 10312de440f5SGeert Uytterhoeven } 10322de440f5SGeert Uytterhoeven 10332de440f5SGeert Uytterhoeven if (max_rx) { 10348caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_rx, max_rx, 10352de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 10362de440f5SGeert Uytterhoeven if (!tmp) 10372de440f5SGeert Uytterhoeven return -ENOMEM; 10388caab75fSGeert Uytterhoeven ctlr->dummy_rx = tmp; 10392de440f5SGeert Uytterhoeven } 10402de440f5SGeert Uytterhoeven 10412de440f5SGeert Uytterhoeven if (max_tx || max_rx) { 10422de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, 10432de440f5SGeert Uytterhoeven transfer_list) { 10445442dcaaSChris Lesiak if (!xfer->len) 10455442dcaaSChris Lesiak continue; 10462de440f5SGeert Uytterhoeven if (!xfer->tx_buf) 10478caab75fSGeert Uytterhoeven xfer->tx_buf = ctlr->dummy_tx; 10482de440f5SGeert Uytterhoeven if (!xfer->rx_buf) 10498caab75fSGeert Uytterhoeven xfer->rx_buf = ctlr->dummy_rx; 10502de440f5SGeert Uytterhoeven } 10512de440f5SGeert Uytterhoeven } 10522de440f5SGeert Uytterhoeven } 10532de440f5SGeert Uytterhoeven 10548caab75fSGeert Uytterhoeven return __spi_map_msg(ctlr, msg); 10552de440f5SGeert Uytterhoeven } 105699adef31SMark Brown 1057810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr, 1058810923f3SLubomir Rintel struct spi_message *msg, 1059810923f3SLubomir Rintel struct spi_transfer *xfer) 1060810923f3SLubomir Rintel { 1061810923f3SLubomir Rintel struct spi_statistics *statm = &ctlr->statistics; 1062810923f3SLubomir Rintel struct spi_statistics *stats = &msg->spi->statistics; 1063810923f3SLubomir Rintel unsigned long long ms = 1; 1064810923f3SLubomir Rintel 1065810923f3SLubomir Rintel if (spi_controller_is_slave(ctlr)) { 1066810923f3SLubomir Rintel if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { 1067810923f3SLubomir Rintel dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); 1068810923f3SLubomir Rintel return -EINTR; 1069810923f3SLubomir Rintel } 1070810923f3SLubomir Rintel } else { 1071810923f3SLubomir Rintel ms = 8LL * 1000LL * xfer->len; 1072810923f3SLubomir Rintel do_div(ms, xfer->speed_hz); 1073810923f3SLubomir Rintel ms += ms + 200; /* some tolerance */ 1074810923f3SLubomir Rintel 1075810923f3SLubomir Rintel if (ms > UINT_MAX) 1076810923f3SLubomir Rintel ms = UINT_MAX; 1077810923f3SLubomir Rintel 1078810923f3SLubomir Rintel ms = wait_for_completion_timeout(&ctlr->xfer_completion, 1079810923f3SLubomir Rintel msecs_to_jiffies(ms)); 1080810923f3SLubomir Rintel 1081810923f3SLubomir Rintel if (ms == 0) { 1082810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(statm, timedout); 1083810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(stats, timedout); 1084810923f3SLubomir Rintel dev_err(&msg->spi->dev, 1085810923f3SLubomir Rintel "SPI transfer timed out\n"); 1086810923f3SLubomir Rintel return -ETIMEDOUT; 1087810923f3SLubomir Rintel } 1088810923f3SLubomir Rintel } 1089810923f3SLubomir Rintel 1090810923f3SLubomir Rintel return 0; 1091810923f3SLubomir Rintel } 1092810923f3SLubomir Rintel 1093b158935fSMark Brown /* 1094b158935fSMark Brown * spi_transfer_one_message - Default implementation of transfer_one_message() 1095b158935fSMark Brown * 1096b158935fSMark Brown * This is a standard implementation of transfer_one_message() for 10978ba811a7SMoritz Fischer * drivers which implement a transfer_one() operation. It provides 1098b158935fSMark Brown * standard handling of delays and chip select management. 1099b158935fSMark Brown */ 11008caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr, 1101b158935fSMark Brown struct spi_message *msg) 1102b158935fSMark Brown { 1103b158935fSMark Brown struct spi_transfer *xfer; 1104b158935fSMark Brown bool keep_cs = false; 1105b158935fSMark Brown int ret = 0; 11068caab75fSGeert Uytterhoeven struct spi_statistics *statm = &ctlr->statistics; 1107eca2ebc7SMartin Sperl struct spi_statistics *stats = &msg->spi->statistics; 1108b158935fSMark Brown 1109b158935fSMark Brown spi_set_cs(msg->spi, true); 1110b158935fSMark Brown 1111eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, messages); 1112eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, messages); 1113eca2ebc7SMartin Sperl 1114b158935fSMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1115b158935fSMark Brown trace_spi_transfer_start(msg, xfer); 1116b158935fSMark Brown 11178caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(statm, xfer, ctlr); 11188caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(stats, xfer, ctlr); 1119eca2ebc7SMartin Sperl 112038ec10f6SMark Brown if (xfer->tx_buf || xfer->rx_buf) { 11218caab75fSGeert Uytterhoeven reinit_completion(&ctlr->xfer_completion); 1122b158935fSMark Brown 11238caab75fSGeert Uytterhoeven ret = ctlr->transfer_one(ctlr, msg->spi, xfer); 1124b158935fSMark Brown if (ret < 0) { 1125eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, 1126eca2ebc7SMartin Sperl errors); 1127eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, 1128eca2ebc7SMartin Sperl errors); 1129b158935fSMark Brown dev_err(&msg->spi->dev, 1130b158935fSMark Brown "SPI transfer failed: %d\n", ret); 1131b158935fSMark Brown goto out; 1132b158935fSMark Brown } 1133b158935fSMark Brown 1134d57e7960SMark Brown if (ret > 0) { 1135810923f3SLubomir Rintel ret = spi_transfer_wait(ctlr, msg, xfer); 1136810923f3SLubomir Rintel if (ret < 0) 1137810923f3SLubomir Rintel msg->status = ret; 1138d57e7960SMark Brown } 113938ec10f6SMark Brown } else { 114038ec10f6SMark Brown if (xfer->len) 114138ec10f6SMark Brown dev_err(&msg->spi->dev, 114238ec10f6SMark Brown "Bufferless transfer has length %u\n", 114338ec10f6SMark Brown xfer->len); 114438ec10f6SMark Brown } 1145b158935fSMark Brown 1146b158935fSMark Brown trace_spi_transfer_stop(msg, xfer); 1147b158935fSMark Brown 1148b158935fSMark Brown if (msg->status != -EINPROGRESS) 1149b158935fSMark Brown goto out; 1150b158935fSMark Brown 11518244bd3aSDaniel Kurtz if (xfer->delay_usecs) { 11528244bd3aSDaniel Kurtz u16 us = xfer->delay_usecs; 11538244bd3aSDaniel Kurtz 11548244bd3aSDaniel Kurtz if (us <= 10) 11558244bd3aSDaniel Kurtz udelay(us); 11568244bd3aSDaniel Kurtz else 11578244bd3aSDaniel Kurtz usleep_range(us, us + DIV_ROUND_UP(us, 10)); 11588244bd3aSDaniel Kurtz } 1159b158935fSMark Brown 1160b158935fSMark Brown if (xfer->cs_change) { 1161b158935fSMark Brown if (list_is_last(&xfer->transfer_list, 1162b158935fSMark Brown &msg->transfers)) { 1163b158935fSMark Brown keep_cs = true; 1164b158935fSMark Brown } else { 11650b73aa63SMark Brown spi_set_cs(msg->spi, false); 11660b73aa63SMark Brown udelay(10); 11670b73aa63SMark Brown spi_set_cs(msg->spi, true); 1168b158935fSMark Brown } 1169b158935fSMark Brown } 1170b158935fSMark Brown 1171b158935fSMark Brown msg->actual_length += xfer->len; 1172b158935fSMark Brown } 1173b158935fSMark Brown 1174b158935fSMark Brown out: 1175b158935fSMark Brown if (ret != 0 || !keep_cs) 1176b158935fSMark Brown spi_set_cs(msg->spi, false); 1177b158935fSMark Brown 1178b158935fSMark Brown if (msg->status == -EINPROGRESS) 1179b158935fSMark Brown msg->status = ret; 1180b158935fSMark Brown 11818caab75fSGeert Uytterhoeven if (msg->status && ctlr->handle_err) 11828caab75fSGeert Uytterhoeven ctlr->handle_err(ctlr, msg); 1183b716c4ffSAndy Shevchenko 11848caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 1185b158935fSMark Brown 1186*c9ba7a16SNoralf Trønnes spi_res_release(ctlr, msg); 1187*c9ba7a16SNoralf Trønnes 1188b158935fSMark Brown return ret; 1189b158935fSMark Brown } 1190b158935fSMark Brown 1191b158935fSMark Brown /** 1192b158935fSMark Brown * spi_finalize_current_transfer - report completion of a transfer 11938caab75fSGeert Uytterhoeven * @ctlr: the controller reporting completion 1194b158935fSMark Brown * 1195b158935fSMark Brown * Called by SPI drivers using the core transfer_one_message() 1196b158935fSMark Brown * implementation to notify it that the current interrupt driven 11979e8f4882SGeert Uytterhoeven * transfer has finished and the next one may be scheduled. 1198b158935fSMark Brown */ 11998caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr) 1200b158935fSMark Brown { 12018caab75fSGeert Uytterhoeven complete(&ctlr->xfer_completion); 1202b158935fSMark Brown } 1203b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); 1204b158935fSMark Brown 1205ffbbdd21SLinus Walleij /** 1206fc9e0f71SMark Brown * __spi_pump_messages - function which processes spi message queue 12078caab75fSGeert Uytterhoeven * @ctlr: controller to process queue for 1208fc9e0f71SMark Brown * @in_kthread: true if we are in the context of the message pump thread 1209ffbbdd21SLinus Walleij * 1210ffbbdd21SLinus Walleij * This function checks if there is any spi message in the queue that 1211ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 1212ffbbdd21SLinus Walleij * and transfer each message. 1213ffbbdd21SLinus Walleij * 12140461a414SMark Brown * Note that it is called both from the kthread itself and also from 12150461a414SMark Brown * inside spi_sync(); the queue extraction handling at the top of the 12160461a414SMark Brown * function should deal with this safely. 1217ffbbdd21SLinus Walleij */ 12188caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1219ffbbdd21SLinus Walleij { 1220ffbbdd21SLinus Walleij unsigned long flags; 1221ffbbdd21SLinus Walleij bool was_busy = false; 1222ffbbdd21SLinus Walleij int ret; 1223ffbbdd21SLinus Walleij 1224983aee5dSMark Brown /* Lock queue */ 12258caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1226983aee5dSMark Brown 1227983aee5dSMark Brown /* Make sure we are not already running a message */ 12288caab75fSGeert Uytterhoeven if (ctlr->cur_msg) { 12298caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1230983aee5dSMark Brown return; 1231983aee5dSMark Brown } 1232983aee5dSMark Brown 1233f0125f1aSMark Brown /* If another context is idling the device then defer */ 12348caab75fSGeert Uytterhoeven if (ctlr->idling) { 12358caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 12368caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 12370461a414SMark Brown return; 12380461a414SMark Brown } 12390461a414SMark Brown 1240983aee5dSMark Brown /* Check if the queue is idle */ 12418caab75fSGeert Uytterhoeven if (list_empty(&ctlr->queue) || !ctlr->running) { 12428caab75fSGeert Uytterhoeven if (!ctlr->busy) { 12438caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1244ffbbdd21SLinus Walleij return; 1245ffbbdd21SLinus Walleij } 1246fc9e0f71SMark Brown 1247f0125f1aSMark Brown /* Only do teardown in the thread */ 1248f0125f1aSMark Brown if (!in_kthread) { 1249f0125f1aSMark Brown kthread_queue_work(&ctlr->kworker, 1250f0125f1aSMark Brown &ctlr->pump_messages); 1251f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1252f0125f1aSMark Brown return; 1253f0125f1aSMark Brown } 1254f0125f1aSMark Brown 1255f0125f1aSMark Brown ctlr->busy = false; 1256f0125f1aSMark Brown ctlr->idling = true; 1257f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1258f0125f1aSMark Brown 1259f0125f1aSMark Brown kfree(ctlr->dummy_rx); 1260f0125f1aSMark Brown ctlr->dummy_rx = NULL; 1261f0125f1aSMark Brown kfree(ctlr->dummy_tx); 1262f0125f1aSMark Brown ctlr->dummy_tx = NULL; 1263f0125f1aSMark Brown if (ctlr->unprepare_transfer_hardware && 1264f0125f1aSMark Brown ctlr->unprepare_transfer_hardware(ctlr)) 1265f0125f1aSMark Brown dev_err(&ctlr->dev, 1266f0125f1aSMark Brown "failed to unprepare transfer hardware\n"); 1267f0125f1aSMark Brown if (ctlr->auto_runtime_pm) { 1268f0125f1aSMark Brown pm_runtime_mark_last_busy(ctlr->dev.parent); 1269f0125f1aSMark Brown pm_runtime_put_autosuspend(ctlr->dev.parent); 1270f0125f1aSMark Brown } 1271f0125f1aSMark Brown trace_spi_controller_idle(ctlr); 1272f0125f1aSMark Brown 1273f0125f1aSMark Brown spin_lock_irqsave(&ctlr->queue_lock, flags); 1274f0125f1aSMark Brown ctlr->idling = false; 12758caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1276ffbbdd21SLinus Walleij return; 1277ffbbdd21SLinus Walleij } 1278ffbbdd21SLinus Walleij 1279ffbbdd21SLinus Walleij /* Extract head of queue */ 12808caab75fSGeert Uytterhoeven ctlr->cur_msg = 12818caab75fSGeert Uytterhoeven list_first_entry(&ctlr->queue, struct spi_message, queue); 1282ffbbdd21SLinus Walleij 12838caab75fSGeert Uytterhoeven list_del_init(&ctlr->cur_msg->queue); 12848caab75fSGeert Uytterhoeven if (ctlr->busy) 1285ffbbdd21SLinus Walleij was_busy = true; 1286ffbbdd21SLinus Walleij else 12878caab75fSGeert Uytterhoeven ctlr->busy = true; 12888caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1289ffbbdd21SLinus Walleij 12908caab75fSGeert Uytterhoeven mutex_lock(&ctlr->io_mutex); 1291ef4d96ecSMark Brown 12928caab75fSGeert Uytterhoeven if (!was_busy && ctlr->auto_runtime_pm) { 12938caab75fSGeert Uytterhoeven ret = pm_runtime_get_sync(ctlr->dev.parent); 129449834de2SMark Brown if (ret < 0) { 12957e48e23aSTony Lindgren pm_runtime_put_noidle(ctlr->dev.parent); 12968caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "Failed to power device: %d\n", 129749834de2SMark Brown ret); 12988caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 129949834de2SMark Brown return; 130049834de2SMark Brown } 130149834de2SMark Brown } 130249834de2SMark Brown 130356ec1978SMark Brown if (!was_busy) 13048caab75fSGeert Uytterhoeven trace_spi_controller_busy(ctlr); 130556ec1978SMark Brown 13068caab75fSGeert Uytterhoeven if (!was_busy && ctlr->prepare_transfer_hardware) { 13078caab75fSGeert Uytterhoeven ret = ctlr->prepare_transfer_hardware(ctlr); 1308ffbbdd21SLinus Walleij if (ret) { 13098caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 1310ffbbdd21SLinus Walleij "failed to prepare transfer hardware\n"); 131149834de2SMark Brown 13128caab75fSGeert Uytterhoeven if (ctlr->auto_runtime_pm) 13138caab75fSGeert Uytterhoeven pm_runtime_put(ctlr->dev.parent); 13148caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 1315ffbbdd21SLinus Walleij return; 1316ffbbdd21SLinus Walleij } 1317ffbbdd21SLinus Walleij } 1318ffbbdd21SLinus Walleij 13198caab75fSGeert Uytterhoeven trace_spi_message_start(ctlr->cur_msg); 132056ec1978SMark Brown 13218caab75fSGeert Uytterhoeven if (ctlr->prepare_message) { 13228caab75fSGeert Uytterhoeven ret = ctlr->prepare_message(ctlr, ctlr->cur_msg); 13232841a5fcSMark Brown if (ret) { 13248caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to prepare message: %d\n", 13258caab75fSGeert Uytterhoeven ret); 13268caab75fSGeert Uytterhoeven ctlr->cur_msg->status = ret; 13278caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 132849023d2eSJon Hunter goto out; 13292841a5fcSMark Brown } 13308caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = true; 13312841a5fcSMark Brown } 13322841a5fcSMark Brown 13338caab75fSGeert Uytterhoeven ret = spi_map_msg(ctlr, ctlr->cur_msg); 133499adef31SMark Brown if (ret) { 13358caab75fSGeert Uytterhoeven ctlr->cur_msg->status = ret; 13368caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 133749023d2eSJon Hunter goto out; 133899adef31SMark Brown } 133999adef31SMark Brown 13408caab75fSGeert Uytterhoeven ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg); 1341ffbbdd21SLinus Walleij if (ret) { 13428caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 13431f802f82SGeert Uytterhoeven "failed to transfer one message from queue\n"); 134449023d2eSJon Hunter goto out; 1345ffbbdd21SLinus Walleij } 134649023d2eSJon Hunter 134749023d2eSJon Hunter out: 13488caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 134962826970SMark Brown 135062826970SMark Brown /* Prod the scheduler in case transfer_one() was busy waiting */ 135149023d2eSJon Hunter if (!ret) 135262826970SMark Brown cond_resched(); 1353ffbbdd21SLinus Walleij } 1354ffbbdd21SLinus Walleij 1355fc9e0f71SMark Brown /** 1356fc9e0f71SMark Brown * spi_pump_messages - kthread work function which processes spi message queue 13578caab75fSGeert Uytterhoeven * @work: pointer to kthread work struct contained in the controller struct 1358fc9e0f71SMark Brown */ 1359fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work) 1360fc9e0f71SMark Brown { 13618caab75fSGeert Uytterhoeven struct spi_controller *ctlr = 13628caab75fSGeert Uytterhoeven container_of(work, struct spi_controller, pump_messages); 1363fc9e0f71SMark Brown 13648caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, true); 1365fc9e0f71SMark Brown } 1366fc9e0f71SMark Brown 13678caab75fSGeert Uytterhoeven static int spi_init_queue(struct spi_controller *ctlr) 1368ffbbdd21SLinus Walleij { 1369ffbbdd21SLinus Walleij struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; 1370ffbbdd21SLinus Walleij 13718caab75fSGeert Uytterhoeven ctlr->running = false; 13728caab75fSGeert Uytterhoeven ctlr->busy = false; 1373ffbbdd21SLinus Walleij 13748caab75fSGeert Uytterhoeven kthread_init_worker(&ctlr->kworker); 13758caab75fSGeert Uytterhoeven ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker, 13768caab75fSGeert Uytterhoeven "%s", dev_name(&ctlr->dev)); 13778caab75fSGeert Uytterhoeven if (IS_ERR(ctlr->kworker_task)) { 13788caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to create message pump task\n"); 13798caab75fSGeert Uytterhoeven return PTR_ERR(ctlr->kworker_task); 1380ffbbdd21SLinus Walleij } 13818caab75fSGeert Uytterhoeven kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1382f0125f1aSMark Brown 1383ffbbdd21SLinus Walleij /* 13848caab75fSGeert Uytterhoeven * Controller config will indicate if this controller should run the 1385ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 1386ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 1387ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 1388ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 1389ffbbdd21SLinus Walleij */ 13908caab75fSGeert Uytterhoeven if (ctlr->rt) { 13918caab75fSGeert Uytterhoeven dev_info(&ctlr->dev, 1392ffbbdd21SLinus Walleij "will run message pump with realtime priority\n"); 13938caab75fSGeert Uytterhoeven sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, ¶m); 1394ffbbdd21SLinus Walleij } 1395ffbbdd21SLinus Walleij 1396ffbbdd21SLinus Walleij return 0; 1397ffbbdd21SLinus Walleij } 1398ffbbdd21SLinus Walleij 1399ffbbdd21SLinus Walleij /** 1400ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 1401ffbbdd21SLinus Walleij * messages 14028caab75fSGeert Uytterhoeven * @ctlr: the controller to check for queued messages 1403ffbbdd21SLinus Walleij * 1404ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 1405ffbbdd21SLinus Walleij * this call. 140697d56dc6SJavier Martinez Canillas * 140797d56dc6SJavier Martinez Canillas * Return: the next message in the queue, else NULL if the queue is empty. 1408ffbbdd21SLinus Walleij */ 14098caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1410ffbbdd21SLinus Walleij { 1411ffbbdd21SLinus Walleij struct spi_message *next; 1412ffbbdd21SLinus Walleij unsigned long flags; 1413ffbbdd21SLinus Walleij 1414ffbbdd21SLinus Walleij /* get a pointer to the next message, if any */ 14158caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 14168caab75fSGeert Uytterhoeven next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 14171cfd97f9SAxel Lin queue); 14188caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1419ffbbdd21SLinus Walleij 1420ffbbdd21SLinus Walleij return next; 1421ffbbdd21SLinus Walleij } 1422ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1423ffbbdd21SLinus Walleij 1424ffbbdd21SLinus Walleij /** 1425ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 14268caab75fSGeert Uytterhoeven * @ctlr: the controller to return the message to 1427ffbbdd21SLinus Walleij * 1428ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 1429ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 1430ffbbdd21SLinus Walleij */ 14318caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr) 1432ffbbdd21SLinus Walleij { 1433ffbbdd21SLinus Walleij struct spi_message *mesg; 1434ffbbdd21SLinus Walleij unsigned long flags; 14352841a5fcSMark Brown int ret; 1436ffbbdd21SLinus Walleij 14378caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 14388caab75fSGeert Uytterhoeven mesg = ctlr->cur_msg; 14398caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1440ffbbdd21SLinus Walleij 14418caab75fSGeert Uytterhoeven spi_unmap_msg(ctlr, mesg); 144299adef31SMark Brown 14438caab75fSGeert Uytterhoeven if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { 14448caab75fSGeert Uytterhoeven ret = ctlr->unprepare_message(ctlr, mesg); 14452841a5fcSMark Brown if (ret) { 14468caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 14478caab75fSGeert Uytterhoeven ret); 14482841a5fcSMark Brown } 14492841a5fcSMark Brown } 1450391949b6SUwe Kleine-König 14518caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 14528caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 14538caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = false; 14548caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 14558caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 14568e76ef88SMartin Sperl 14578e76ef88SMartin Sperl trace_spi_message_done(mesg); 14582841a5fcSMark Brown 1459ffbbdd21SLinus Walleij mesg->state = NULL; 1460ffbbdd21SLinus Walleij if (mesg->complete) 1461ffbbdd21SLinus Walleij mesg->complete(mesg->context); 1462ffbbdd21SLinus Walleij } 1463ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 1464ffbbdd21SLinus Walleij 14658caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr) 1466ffbbdd21SLinus Walleij { 1467ffbbdd21SLinus Walleij unsigned long flags; 1468ffbbdd21SLinus Walleij 14698caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1470ffbbdd21SLinus Walleij 14718caab75fSGeert Uytterhoeven if (ctlr->running || ctlr->busy) { 14728caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1473ffbbdd21SLinus Walleij return -EBUSY; 1474ffbbdd21SLinus Walleij } 1475ffbbdd21SLinus Walleij 14768caab75fSGeert Uytterhoeven ctlr->running = true; 14778caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 14788caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1479ffbbdd21SLinus Walleij 14808caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 1481ffbbdd21SLinus Walleij 1482ffbbdd21SLinus Walleij return 0; 1483ffbbdd21SLinus Walleij } 1484ffbbdd21SLinus Walleij 14858caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr) 1486ffbbdd21SLinus Walleij { 1487ffbbdd21SLinus Walleij unsigned long flags; 1488ffbbdd21SLinus Walleij unsigned limit = 500; 1489ffbbdd21SLinus Walleij int ret = 0; 1490ffbbdd21SLinus Walleij 14918caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1492ffbbdd21SLinus Walleij 1493ffbbdd21SLinus Walleij /* 1494ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 14958caab75fSGeert Uytterhoeven * A wait_queue on the ctlr->busy could be used, but then the common 1496ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 1497ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 1498ffbbdd21SLinus Walleij */ 14998caab75fSGeert Uytterhoeven while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 15008caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1501f97b26b0SAxel Lin usleep_range(10000, 11000); 15028caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1503ffbbdd21SLinus Walleij } 1504ffbbdd21SLinus Walleij 15058caab75fSGeert Uytterhoeven if (!list_empty(&ctlr->queue) || ctlr->busy) 1506ffbbdd21SLinus Walleij ret = -EBUSY; 1507ffbbdd21SLinus Walleij else 15088caab75fSGeert Uytterhoeven ctlr->running = false; 1509ffbbdd21SLinus Walleij 15108caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1511ffbbdd21SLinus Walleij 1512ffbbdd21SLinus Walleij if (ret) { 15138caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "could not stop message queue\n"); 1514ffbbdd21SLinus Walleij return ret; 1515ffbbdd21SLinus Walleij } 1516ffbbdd21SLinus Walleij return ret; 1517ffbbdd21SLinus Walleij } 1518ffbbdd21SLinus Walleij 15198caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr) 1520ffbbdd21SLinus Walleij { 1521ffbbdd21SLinus Walleij int ret; 1522ffbbdd21SLinus Walleij 15238caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 1524ffbbdd21SLinus Walleij 1525ffbbdd21SLinus Walleij /* 15263989144fSPetr Mladek * kthread_flush_worker will block until all work is done. 1527ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 1528ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 1529ffbbdd21SLinus Walleij * return anyway. 1530ffbbdd21SLinus Walleij */ 1531ffbbdd21SLinus Walleij if (ret) { 15328caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem destroying queue\n"); 1533ffbbdd21SLinus Walleij return ret; 1534ffbbdd21SLinus Walleij } 1535ffbbdd21SLinus Walleij 15368caab75fSGeert Uytterhoeven kthread_flush_worker(&ctlr->kworker); 15378caab75fSGeert Uytterhoeven kthread_stop(ctlr->kworker_task); 1538ffbbdd21SLinus Walleij 1539ffbbdd21SLinus Walleij return 0; 1540ffbbdd21SLinus Walleij } 1541ffbbdd21SLinus Walleij 15420461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi, 15430461a414SMark Brown struct spi_message *msg, 15440461a414SMark Brown bool need_pump) 1545ffbbdd21SLinus Walleij { 15468caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 1547ffbbdd21SLinus Walleij unsigned long flags; 1548ffbbdd21SLinus Walleij 15498caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1550ffbbdd21SLinus Walleij 15518caab75fSGeert Uytterhoeven if (!ctlr->running) { 15528caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1553ffbbdd21SLinus Walleij return -ESHUTDOWN; 1554ffbbdd21SLinus Walleij } 1555ffbbdd21SLinus Walleij msg->actual_length = 0; 1556ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 1557ffbbdd21SLinus Walleij 15588caab75fSGeert Uytterhoeven list_add_tail(&msg->queue, &ctlr->queue); 1559f0125f1aSMark Brown if (!ctlr->busy && need_pump) 15608caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 1561ffbbdd21SLinus Walleij 15628caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1563ffbbdd21SLinus Walleij return 0; 1564ffbbdd21SLinus Walleij } 1565ffbbdd21SLinus Walleij 15660461a414SMark Brown /** 15670461a414SMark Brown * spi_queued_transfer - transfer function for queued transfers 15680461a414SMark Brown * @spi: spi device which is requesting transfer 15690461a414SMark Brown * @msg: spi message which is to handled is queued to driver queue 157097d56dc6SJavier Martinez Canillas * 157197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 15720461a414SMark Brown */ 15730461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 15740461a414SMark Brown { 15750461a414SMark Brown return __spi_queued_transfer(spi, msg, true); 15760461a414SMark Brown } 15770461a414SMark Brown 15788caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr) 1579ffbbdd21SLinus Walleij { 1580ffbbdd21SLinus Walleij int ret; 1581ffbbdd21SLinus Walleij 15828caab75fSGeert Uytterhoeven ctlr->transfer = spi_queued_transfer; 15838caab75fSGeert Uytterhoeven if (!ctlr->transfer_one_message) 15848caab75fSGeert Uytterhoeven ctlr->transfer_one_message = spi_transfer_one_message; 1585ffbbdd21SLinus Walleij 1586ffbbdd21SLinus Walleij /* Initialize and start queue */ 15878caab75fSGeert Uytterhoeven ret = spi_init_queue(ctlr); 1588ffbbdd21SLinus Walleij if (ret) { 15898caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem initializing queue\n"); 1590ffbbdd21SLinus Walleij goto err_init_queue; 1591ffbbdd21SLinus Walleij } 15928caab75fSGeert Uytterhoeven ctlr->queued = true; 15938caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 1594ffbbdd21SLinus Walleij if (ret) { 15958caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem starting queue\n"); 1596ffbbdd21SLinus Walleij goto err_start_queue; 1597ffbbdd21SLinus Walleij } 1598ffbbdd21SLinus Walleij 1599ffbbdd21SLinus Walleij return 0; 1600ffbbdd21SLinus Walleij 1601ffbbdd21SLinus Walleij err_start_queue: 16028caab75fSGeert Uytterhoeven spi_destroy_queue(ctlr); 1603c3676d5cSMark Brown err_init_queue: 1604ffbbdd21SLinus Walleij return ret; 1605ffbbdd21SLinus Walleij } 1606ffbbdd21SLinus Walleij 1607988f259bSBoris Brezillon /** 1608988f259bSBoris Brezillon * spi_flush_queue - Send all pending messages in the queue from the callers' 1609988f259bSBoris Brezillon * context 1610988f259bSBoris Brezillon * @ctlr: controller to process queue for 1611988f259bSBoris Brezillon * 1612988f259bSBoris Brezillon * This should be used when one wants to ensure all pending messages have been 1613988f259bSBoris Brezillon * sent before doing something. Is used by the spi-mem code to make sure SPI 1614988f259bSBoris Brezillon * memory operations do not preempt regular SPI transfers that have been queued 1615988f259bSBoris Brezillon * before the spi-mem operation. 1616988f259bSBoris Brezillon */ 1617988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr) 1618988f259bSBoris Brezillon { 1619988f259bSBoris Brezillon if (ctlr->transfer == spi_queued_transfer) 1620988f259bSBoris Brezillon __spi_pump_messages(ctlr, false); 1621988f259bSBoris Brezillon } 1622988f259bSBoris Brezillon 1623ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 1624ffbbdd21SLinus Walleij 16257cb94361SAndreas Larsson #if defined(CONFIG_OF) 16268caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 1627c2e51ac3SGeert Uytterhoeven struct device_node *nc) 1628d57a4282SGrant Likely { 162989da4293STrent Piepho u32 value; 1630c2e51ac3SGeert Uytterhoeven int rc; 1631d57a4282SGrant Likely 1632d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 1633e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpha")) 1634d57a4282SGrant Likely spi->mode |= SPI_CPHA; 1635e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpol")) 1636d57a4282SGrant Likely spi->mode |= SPI_CPOL; 1637e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-3wire")) 1638c20151dfSLars-Peter Clausen spi->mode |= SPI_3WIRE; 1639e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-lsb-first")) 1640cd6339e6SZhao Qiang spi->mode |= SPI_LSB_FIRST; 1641d57a4282SGrant Likely 1642f3186dd8SLinus Walleij /* 1643f3186dd8SLinus Walleij * For descriptors associated with the device, polarity inversion is 1644f3186dd8SLinus Walleij * handled in the gpiolib, so all chip selects are "active high" in 1645f3186dd8SLinus Walleij * the logical sense, the gpiolib will invert the line if need be. 1646f3186dd8SLinus Walleij */ 1647f3186dd8SLinus Walleij if (ctlr->use_gpio_descriptors) 1648f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 1649f3186dd8SLinus Walleij else if (of_property_read_bool(nc, "spi-cs-high")) 1650f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 1651f3186dd8SLinus Walleij 1652f477b7fbSwangyuhang /* Device DUAL/QUAD mode */ 165389da4293STrent Piepho if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 165489da4293STrent Piepho switch (value) { 165589da4293STrent Piepho case 1: 1656f477b7fbSwangyuhang break; 165789da4293STrent Piepho case 2: 1658f477b7fbSwangyuhang spi->mode |= SPI_TX_DUAL; 1659f477b7fbSwangyuhang break; 166089da4293STrent Piepho case 4: 1661f477b7fbSwangyuhang spi->mode |= SPI_TX_QUAD; 1662f477b7fbSwangyuhang break; 16636b03061fSYogesh Narayan Gaur case 8: 16646b03061fSYogesh Narayan Gaur spi->mode |= SPI_TX_OCTAL; 16656b03061fSYogesh Narayan Gaur break; 1666f477b7fbSwangyuhang default: 16678caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 1668a110f93dSwangyuhang "spi-tx-bus-width %d not supported\n", 166989da4293STrent Piepho value); 167080874d8cSGeert Uytterhoeven break; 1671f477b7fbSwangyuhang } 1672a822e99cSMark Brown } 1673f477b7fbSwangyuhang 167489da4293STrent Piepho if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 167589da4293STrent Piepho switch (value) { 167689da4293STrent Piepho case 1: 1677f477b7fbSwangyuhang break; 167889da4293STrent Piepho case 2: 1679f477b7fbSwangyuhang spi->mode |= SPI_RX_DUAL; 1680f477b7fbSwangyuhang break; 168189da4293STrent Piepho case 4: 1682f477b7fbSwangyuhang spi->mode |= SPI_RX_QUAD; 1683f477b7fbSwangyuhang break; 16846b03061fSYogesh Narayan Gaur case 8: 16856b03061fSYogesh Narayan Gaur spi->mode |= SPI_RX_OCTAL; 16866b03061fSYogesh Narayan Gaur break; 1687f477b7fbSwangyuhang default: 16888caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 1689a110f93dSwangyuhang "spi-rx-bus-width %d not supported\n", 169089da4293STrent Piepho value); 169180874d8cSGeert Uytterhoeven break; 1692f477b7fbSwangyuhang } 1693a822e99cSMark Brown } 1694f477b7fbSwangyuhang 16958caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr)) { 1696194276b0SRob Herring if (!of_node_name_eq(nc, "slave")) { 169725c56c88SRob Herring dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 169825c56c88SRob Herring nc); 16996c364062SGeert Uytterhoeven return -EINVAL; 17006c364062SGeert Uytterhoeven } 17016c364062SGeert Uytterhoeven return 0; 17026c364062SGeert Uytterhoeven } 17036c364062SGeert Uytterhoeven 17046c364062SGeert Uytterhoeven /* Device address */ 17056c364062SGeert Uytterhoeven rc = of_property_read_u32(nc, "reg", &value); 17066c364062SGeert Uytterhoeven if (rc) { 170725c56c88SRob Herring dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 170825c56c88SRob Herring nc, rc); 17096c364062SGeert Uytterhoeven return rc; 17106c364062SGeert Uytterhoeven } 17116c364062SGeert Uytterhoeven spi->chip_select = value; 17126c364062SGeert Uytterhoeven 1713d57a4282SGrant Likely /* Device speed */ 171489da4293STrent Piepho rc = of_property_read_u32(nc, "spi-max-frequency", &value); 171589da4293STrent Piepho if (rc) { 17168caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 171725c56c88SRob Herring "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc); 1718c2e51ac3SGeert Uytterhoeven return rc; 1719d57a4282SGrant Likely } 172089da4293STrent Piepho spi->max_speed_hz = value; 1721d57a4282SGrant Likely 1722c2e51ac3SGeert Uytterhoeven return 0; 1723c2e51ac3SGeert Uytterhoeven } 1724c2e51ac3SGeert Uytterhoeven 1725c2e51ac3SGeert Uytterhoeven static struct spi_device * 17268caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 1727c2e51ac3SGeert Uytterhoeven { 1728c2e51ac3SGeert Uytterhoeven struct spi_device *spi; 1729c2e51ac3SGeert Uytterhoeven int rc; 1730c2e51ac3SGeert Uytterhoeven 1731c2e51ac3SGeert Uytterhoeven /* Alloc an spi_device */ 17328caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 1733c2e51ac3SGeert Uytterhoeven if (!spi) { 173425c56c88SRob Herring dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 1735c2e51ac3SGeert Uytterhoeven rc = -ENOMEM; 1736c2e51ac3SGeert Uytterhoeven goto err_out; 1737c2e51ac3SGeert Uytterhoeven } 1738c2e51ac3SGeert Uytterhoeven 1739c2e51ac3SGeert Uytterhoeven /* Select device driver */ 1740c2e51ac3SGeert Uytterhoeven rc = of_modalias_node(nc, spi->modalias, 1741c2e51ac3SGeert Uytterhoeven sizeof(spi->modalias)); 1742c2e51ac3SGeert Uytterhoeven if (rc < 0) { 174325c56c88SRob Herring dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 1744c2e51ac3SGeert Uytterhoeven goto err_out; 1745c2e51ac3SGeert Uytterhoeven } 1746c2e51ac3SGeert Uytterhoeven 17478caab75fSGeert Uytterhoeven rc = of_spi_parse_dt(ctlr, spi, nc); 1748c2e51ac3SGeert Uytterhoeven if (rc) 1749c2e51ac3SGeert Uytterhoeven goto err_out; 1750c2e51ac3SGeert Uytterhoeven 1751d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 1752d57a4282SGrant Likely of_node_get(nc); 1753d57a4282SGrant Likely spi->dev.of_node = nc; 1754d57a4282SGrant Likely 1755d57a4282SGrant Likely /* Register the new device */ 1756d57a4282SGrant Likely rc = spi_add_device(spi); 1757d57a4282SGrant Likely if (rc) { 175825c56c88SRob Herring dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 17598324147fSJohan Hovold goto err_of_node_put; 1760d57a4282SGrant Likely } 1761d57a4282SGrant Likely 1762aff5e3f8SPantelis Antoniou return spi; 1763aff5e3f8SPantelis Antoniou 17648324147fSJohan Hovold err_of_node_put: 17658324147fSJohan Hovold of_node_put(nc); 1766aff5e3f8SPantelis Antoniou err_out: 1767aff5e3f8SPantelis Antoniou spi_dev_put(spi); 1768aff5e3f8SPantelis Antoniou return ERR_PTR(rc); 1769aff5e3f8SPantelis Antoniou } 1770aff5e3f8SPantelis Antoniou 1771aff5e3f8SPantelis Antoniou /** 1772aff5e3f8SPantelis Antoniou * of_register_spi_devices() - Register child devices onto the SPI bus 17738caab75fSGeert Uytterhoeven * @ctlr: Pointer to spi_controller device 1774aff5e3f8SPantelis Antoniou * 17756c364062SGeert Uytterhoeven * Registers an spi_device for each child node of controller node which 17766c364062SGeert Uytterhoeven * represents a valid SPI slave. 1777aff5e3f8SPantelis Antoniou */ 17788caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) 1779aff5e3f8SPantelis Antoniou { 1780aff5e3f8SPantelis Antoniou struct spi_device *spi; 1781aff5e3f8SPantelis Antoniou struct device_node *nc; 1782aff5e3f8SPantelis Antoniou 17838caab75fSGeert Uytterhoeven if (!ctlr->dev.of_node) 1784aff5e3f8SPantelis Antoniou return; 1785aff5e3f8SPantelis Antoniou 17868caab75fSGeert Uytterhoeven for_each_available_child_of_node(ctlr->dev.of_node, nc) { 1787bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(nc, OF_POPULATED)) 1788bd6c1644SGeert Uytterhoeven continue; 17898caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, nc); 1790e0af98a7SRalf Ramsauer if (IS_ERR(spi)) { 17918caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 179225c56c88SRob Herring "Failed to create SPI device for %pOF\n", nc); 1793e0af98a7SRalf Ramsauer of_node_clear_flag(nc, OF_POPULATED); 1794e0af98a7SRalf Ramsauer } 1795d57a4282SGrant Likely } 1796d57a4282SGrant Likely } 1797d57a4282SGrant Likely #else 17988caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { } 1799d57a4282SGrant Likely #endif 1800d57a4282SGrant Likely 180164bee4d2SMika Westerberg #ifdef CONFIG_ACPI 18028a2e487eSLukas Wunner static void acpi_spi_parse_apple_properties(struct spi_device *spi) 18038a2e487eSLukas Wunner { 18048a2e487eSLukas Wunner struct acpi_device *dev = ACPI_COMPANION(&spi->dev); 18058a2e487eSLukas Wunner const union acpi_object *obj; 18068a2e487eSLukas Wunner 18078a2e487eSLukas Wunner if (!x86_apple_machine) 18088a2e487eSLukas Wunner return; 18098a2e487eSLukas Wunner 18108a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 18118a2e487eSLukas Wunner && obj->buffer.length >= 4) 18128a2e487eSLukas Wunner spi->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 18138a2e487eSLukas Wunner 18148a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 18158a2e487eSLukas Wunner && obj->buffer.length == 8) 18168a2e487eSLukas Wunner spi->bits_per_word = *(u64 *)obj->buffer.pointer; 18178a2e487eSLukas Wunner 18188a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 18198a2e487eSLukas Wunner && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 18208a2e487eSLukas Wunner spi->mode |= SPI_LSB_FIRST; 18218a2e487eSLukas Wunner 18228a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 18238a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 18248a2e487eSLukas Wunner spi->mode |= SPI_CPOL; 18258a2e487eSLukas Wunner 18268a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 18278a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 18288a2e487eSLukas Wunner spi->mode |= SPI_CPHA; 18298a2e487eSLukas Wunner } 18308a2e487eSLukas Wunner 183164bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 183264bee4d2SMika Westerberg { 183364bee4d2SMika Westerberg struct spi_device *spi = data; 18348caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 183564bee4d2SMika Westerberg 183664bee4d2SMika Westerberg if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 183764bee4d2SMika Westerberg struct acpi_resource_spi_serialbus *sb; 183864bee4d2SMika Westerberg 183964bee4d2SMika Westerberg sb = &ares->data.spi_serial_bus; 184064bee4d2SMika Westerberg if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 1841a0a90718SMika Westerberg /* 1842a0a90718SMika Westerberg * ACPI DeviceSelection numbering is handled by the 1843a0a90718SMika Westerberg * host controller driver in Windows and can vary 1844a0a90718SMika Westerberg * from driver to driver. In Linux we always expect 1845a0a90718SMika Westerberg * 0 .. max - 1 so we need to ask the driver to 1846a0a90718SMika Westerberg * translate between the two schemes. 1847a0a90718SMika Westerberg */ 18488caab75fSGeert Uytterhoeven if (ctlr->fw_translate_cs) { 18498caab75fSGeert Uytterhoeven int cs = ctlr->fw_translate_cs(ctlr, 1850a0a90718SMika Westerberg sb->device_selection); 1851a0a90718SMika Westerberg if (cs < 0) 1852a0a90718SMika Westerberg return cs; 1853a0a90718SMika Westerberg spi->chip_select = cs; 1854a0a90718SMika Westerberg } else { 185564bee4d2SMika Westerberg spi->chip_select = sb->device_selection; 1856a0a90718SMika Westerberg } 1857a0a90718SMika Westerberg 185864bee4d2SMika Westerberg spi->max_speed_hz = sb->connection_speed; 185964bee4d2SMika Westerberg 186064bee4d2SMika Westerberg if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 186164bee4d2SMika Westerberg spi->mode |= SPI_CPHA; 186264bee4d2SMika Westerberg if (sb->clock_polarity == ACPI_SPI_START_HIGH) 186364bee4d2SMika Westerberg spi->mode |= SPI_CPOL; 186464bee4d2SMika Westerberg if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 186564bee4d2SMika Westerberg spi->mode |= SPI_CS_HIGH; 186664bee4d2SMika Westerberg } 186764bee4d2SMika Westerberg } else if (spi->irq < 0) { 186864bee4d2SMika Westerberg struct resource r; 186964bee4d2SMika Westerberg 187064bee4d2SMika Westerberg if (acpi_dev_resource_interrupt(ares, 0, &r)) 187164bee4d2SMika Westerberg spi->irq = r.start; 187264bee4d2SMika Westerberg } 187364bee4d2SMika Westerberg 187464bee4d2SMika Westerberg /* Always tell the ACPI core to skip this resource */ 187564bee4d2SMika Westerberg return 1; 187664bee4d2SMika Westerberg } 187764bee4d2SMika Westerberg 18788caab75fSGeert Uytterhoeven static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 18797f24467fSOctavian Purdila struct acpi_device *adev) 188064bee4d2SMika Westerberg { 188164bee4d2SMika Westerberg struct list_head resource_list; 188264bee4d2SMika Westerberg struct spi_device *spi; 188364bee4d2SMika Westerberg int ret; 188464bee4d2SMika Westerberg 18857f24467fSOctavian Purdila if (acpi_bus_get_status(adev) || !adev->status.present || 18867f24467fSOctavian Purdila acpi_device_enumerated(adev)) 188764bee4d2SMika Westerberg return AE_OK; 188864bee4d2SMika Westerberg 18898caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 189064bee4d2SMika Westerberg if (!spi) { 18918caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", 189264bee4d2SMika Westerberg dev_name(&adev->dev)); 189364bee4d2SMika Westerberg return AE_NO_MEMORY; 189464bee4d2SMika Westerberg } 189564bee4d2SMika Westerberg 18967b199811SRafael J. Wysocki ACPI_COMPANION_SET(&spi->dev, adev); 189764bee4d2SMika Westerberg spi->irq = -1; 189864bee4d2SMika Westerberg 189964bee4d2SMika Westerberg INIT_LIST_HEAD(&resource_list); 190064bee4d2SMika Westerberg ret = acpi_dev_get_resources(adev, &resource_list, 190164bee4d2SMika Westerberg acpi_spi_add_resource, spi); 190264bee4d2SMika Westerberg acpi_dev_free_resource_list(&resource_list); 190364bee4d2SMika Westerberg 19048a2e487eSLukas Wunner acpi_spi_parse_apple_properties(spi); 19058a2e487eSLukas Wunner 190664bee4d2SMika Westerberg if (ret < 0 || !spi->max_speed_hz) { 190764bee4d2SMika Westerberg spi_dev_put(spi); 190864bee4d2SMika Westerberg return AE_OK; 190964bee4d2SMika Westerberg } 191064bee4d2SMika Westerberg 19110c6543f6SDan O'Donovan acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 19120c6543f6SDan O'Donovan sizeof(spi->modalias)); 19130c6543f6SDan O'Donovan 191433ada67dSChristophe RICARD if (spi->irq < 0) 191533ada67dSChristophe RICARD spi->irq = acpi_dev_gpio_irq_get(adev, 0); 191633ada67dSChristophe RICARD 19177f24467fSOctavian Purdila acpi_device_set_enumerated(adev); 19187f24467fSOctavian Purdila 191933cf00e5SMika Westerberg adev->power.flags.ignore_parent = true; 192064bee4d2SMika Westerberg if (spi_add_device(spi)) { 192133cf00e5SMika Westerberg adev->power.flags.ignore_parent = false; 19228caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 192364bee4d2SMika Westerberg dev_name(&adev->dev)); 192464bee4d2SMika Westerberg spi_dev_put(spi); 192564bee4d2SMika Westerberg } 192664bee4d2SMika Westerberg 192764bee4d2SMika Westerberg return AE_OK; 192864bee4d2SMika Westerberg } 192964bee4d2SMika Westerberg 19307f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 19317f24467fSOctavian Purdila void *data, void **return_value) 19327f24467fSOctavian Purdila { 19338caab75fSGeert Uytterhoeven struct spi_controller *ctlr = data; 19347f24467fSOctavian Purdila struct acpi_device *adev; 19357f24467fSOctavian Purdila 19367f24467fSOctavian Purdila if (acpi_bus_get_device(handle, &adev)) 19377f24467fSOctavian Purdila return AE_OK; 19387f24467fSOctavian Purdila 19398caab75fSGeert Uytterhoeven return acpi_register_spi_device(ctlr, adev); 19407f24467fSOctavian Purdila } 19417f24467fSOctavian Purdila 19428caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr) 194364bee4d2SMika Westerberg { 194464bee4d2SMika Westerberg acpi_status status; 194564bee4d2SMika Westerberg acpi_handle handle; 194664bee4d2SMika Westerberg 19478caab75fSGeert Uytterhoeven handle = ACPI_HANDLE(ctlr->dev.parent); 194864bee4d2SMika Westerberg if (!handle) 194964bee4d2SMika Westerberg return; 195064bee4d2SMika Westerberg 195164bee4d2SMika Westerberg status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, 19528caab75fSGeert Uytterhoeven acpi_spi_add_device, NULL, ctlr, NULL); 195364bee4d2SMika Westerberg if (ACPI_FAILURE(status)) 19548caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 195564bee4d2SMika Westerberg } 195664bee4d2SMika Westerberg #else 19578caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 195864bee4d2SMika Westerberg #endif /* CONFIG_ACPI */ 195964bee4d2SMika Westerberg 19608caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev) 19618ae12a0dSDavid Brownell { 19628caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 19638ae12a0dSDavid Brownell 19648caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 19658caab75fSGeert Uytterhoeven kfree(ctlr); 19668ae12a0dSDavid Brownell } 19678ae12a0dSDavid Brownell 19688ae12a0dSDavid Brownell static struct class spi_master_class = { 19698ae12a0dSDavid Brownell .name = "spi_master", 19708ae12a0dSDavid Brownell .owner = THIS_MODULE, 19718caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 1972eca2ebc7SMartin Sperl .dev_groups = spi_master_groups, 19738ae12a0dSDavid Brownell }; 19748ae12a0dSDavid Brownell 19756c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE 19766c364062SGeert Uytterhoeven /** 19776c364062SGeert Uytterhoeven * spi_slave_abort - abort the ongoing transfer request on an SPI slave 19786c364062SGeert Uytterhoeven * controller 19796c364062SGeert Uytterhoeven * @spi: device used for the current transfer 19806c364062SGeert Uytterhoeven */ 19816c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi) 19826c364062SGeert Uytterhoeven { 19838caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 19846c364062SGeert Uytterhoeven 19858caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 19868caab75fSGeert Uytterhoeven return ctlr->slave_abort(ctlr); 19876c364062SGeert Uytterhoeven 19886c364062SGeert Uytterhoeven return -ENOTSUPP; 19896c364062SGeert Uytterhoeven } 19906c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort); 19916c364062SGeert Uytterhoeven 19926c364062SGeert Uytterhoeven static int match_true(struct device *dev, void *data) 19936c364062SGeert Uytterhoeven { 19946c364062SGeert Uytterhoeven return 1; 19956c364062SGeert Uytterhoeven } 19966c364062SGeert Uytterhoeven 19976c364062SGeert Uytterhoeven static ssize_t spi_slave_show(struct device *dev, 19986c364062SGeert Uytterhoeven struct device_attribute *attr, char *buf) 19996c364062SGeert Uytterhoeven { 20008caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 20018caab75fSGeert Uytterhoeven dev); 20026c364062SGeert Uytterhoeven struct device *child; 20036c364062SGeert Uytterhoeven 20046c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 20056c364062SGeert Uytterhoeven return sprintf(buf, "%s\n", 20066c364062SGeert Uytterhoeven child ? to_spi_device(child)->modalias : NULL); 20076c364062SGeert Uytterhoeven } 20086c364062SGeert Uytterhoeven 20096c364062SGeert Uytterhoeven static ssize_t spi_slave_store(struct device *dev, 20106c364062SGeert Uytterhoeven struct device_attribute *attr, const char *buf, 20116c364062SGeert Uytterhoeven size_t count) 20126c364062SGeert Uytterhoeven { 20138caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 20148caab75fSGeert Uytterhoeven dev); 20156c364062SGeert Uytterhoeven struct spi_device *spi; 20166c364062SGeert Uytterhoeven struct device *child; 20176c364062SGeert Uytterhoeven char name[32]; 20186c364062SGeert Uytterhoeven int rc; 20196c364062SGeert Uytterhoeven 20206c364062SGeert Uytterhoeven rc = sscanf(buf, "%31s", name); 20216c364062SGeert Uytterhoeven if (rc != 1 || !name[0]) 20226c364062SGeert Uytterhoeven return -EINVAL; 20236c364062SGeert Uytterhoeven 20246c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 20256c364062SGeert Uytterhoeven if (child) { 20266c364062SGeert Uytterhoeven /* Remove registered slave */ 20276c364062SGeert Uytterhoeven device_unregister(child); 20286c364062SGeert Uytterhoeven put_device(child); 20296c364062SGeert Uytterhoeven } 20306c364062SGeert Uytterhoeven 20316c364062SGeert Uytterhoeven if (strcmp(name, "(null)")) { 20326c364062SGeert Uytterhoeven /* Register new slave */ 20336c364062SGeert Uytterhoeven spi = spi_alloc_device(ctlr); 20346c364062SGeert Uytterhoeven if (!spi) 20356c364062SGeert Uytterhoeven return -ENOMEM; 20366c364062SGeert Uytterhoeven 20376c364062SGeert Uytterhoeven strlcpy(spi->modalias, name, sizeof(spi->modalias)); 20386c364062SGeert Uytterhoeven 20396c364062SGeert Uytterhoeven rc = spi_add_device(spi); 20406c364062SGeert Uytterhoeven if (rc) { 20416c364062SGeert Uytterhoeven spi_dev_put(spi); 20426c364062SGeert Uytterhoeven return rc; 20436c364062SGeert Uytterhoeven } 20446c364062SGeert Uytterhoeven } 20456c364062SGeert Uytterhoeven 20466c364062SGeert Uytterhoeven return count; 20476c364062SGeert Uytterhoeven } 20486c364062SGeert Uytterhoeven 20496c364062SGeert Uytterhoeven static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store); 20506c364062SGeert Uytterhoeven 20516c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = { 20526c364062SGeert Uytterhoeven &dev_attr_slave.attr, 20536c364062SGeert Uytterhoeven NULL, 20546c364062SGeert Uytterhoeven }; 20556c364062SGeert Uytterhoeven 20566c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = { 20576c364062SGeert Uytterhoeven .attrs = spi_slave_attrs, 20586c364062SGeert Uytterhoeven }; 20596c364062SGeert Uytterhoeven 20606c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = { 20618caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 20626c364062SGeert Uytterhoeven &spi_slave_group, 20636c364062SGeert Uytterhoeven NULL, 20646c364062SGeert Uytterhoeven }; 20656c364062SGeert Uytterhoeven 20666c364062SGeert Uytterhoeven static struct class spi_slave_class = { 20676c364062SGeert Uytterhoeven .name = "spi_slave", 20686c364062SGeert Uytterhoeven .owner = THIS_MODULE, 20698caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 20706c364062SGeert Uytterhoeven .dev_groups = spi_slave_groups, 20716c364062SGeert Uytterhoeven }; 20726c364062SGeert Uytterhoeven #else 20736c364062SGeert Uytterhoeven extern struct class spi_slave_class; /* dummy */ 20746c364062SGeert Uytterhoeven #endif 20758ae12a0dSDavid Brownell 20768ae12a0dSDavid Brownell /** 20776c364062SGeert Uytterhoeven * __spi_alloc_controller - allocate an SPI master or slave controller 20788ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 207933e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 208049dce689STony Jones * memory is in the driver_data field of the returned device, 20818caab75fSGeert Uytterhoeven * accessible with spi_controller_get_devdata(). 20826c364062SGeert Uytterhoeven * @slave: flag indicating whether to allocate an SPI master (false) or SPI 20836c364062SGeert Uytterhoeven * slave (true) controller 208433e34dc6SDavid Brownell * Context: can sleep 20858ae12a0dSDavid Brownell * 20866c364062SGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 20878ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 20888caab75fSGeert Uytterhoeven * an spi_controller structure, prior to calling spi_register_controller(). 20898ae12a0dSDavid Brownell * 209097d56dc6SJavier Martinez Canillas * This must be called from context that can sleep. 20918ae12a0dSDavid Brownell * 20926c364062SGeert Uytterhoeven * The caller is responsible for assigning the bus number and initializing the 20938caab75fSGeert Uytterhoeven * controller's methods before calling spi_register_controller(); and (after 20948caab75fSGeert Uytterhoeven * errors adding the device) calling spi_controller_put() to prevent a memory 20958caab75fSGeert Uytterhoeven * leak. 209697d56dc6SJavier Martinez Canillas * 20976c364062SGeert Uytterhoeven * Return: the SPI controller structure on success, else NULL. 20988ae12a0dSDavid Brownell */ 20998caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev, 21006c364062SGeert Uytterhoeven unsigned int size, bool slave) 21018ae12a0dSDavid Brownell { 21028caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 21038ae12a0dSDavid Brownell 21040c868461SDavid Brownell if (!dev) 21050c868461SDavid Brownell return NULL; 21060c868461SDavid Brownell 21078caab75fSGeert Uytterhoeven ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL); 21088caab75fSGeert Uytterhoeven if (!ctlr) 21098ae12a0dSDavid Brownell return NULL; 21108ae12a0dSDavid Brownell 21118caab75fSGeert Uytterhoeven device_initialize(&ctlr->dev); 21128caab75fSGeert Uytterhoeven ctlr->bus_num = -1; 21138caab75fSGeert Uytterhoeven ctlr->num_chipselect = 1; 21148caab75fSGeert Uytterhoeven ctlr->slave = slave; 21156c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 21168caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_slave_class; 21176c364062SGeert Uytterhoeven else 21188caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_master_class; 21198caab75fSGeert Uytterhoeven ctlr->dev.parent = dev; 21208caab75fSGeert Uytterhoeven pm_suspend_ignore_children(&ctlr->dev, true); 21218caab75fSGeert Uytterhoeven spi_controller_set_devdata(ctlr, &ctlr[1]); 21228ae12a0dSDavid Brownell 21238caab75fSGeert Uytterhoeven return ctlr; 21248ae12a0dSDavid Brownell } 21256c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller); 21268ae12a0dSDavid Brownell 212774317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF 21288caab75fSGeert Uytterhoeven static int of_spi_register_master(struct spi_controller *ctlr) 212974317984SJean-Christophe PLAGNIOL-VILLARD { 2130e80beb27SGrant Likely int nb, i, *cs; 21318caab75fSGeert Uytterhoeven struct device_node *np = ctlr->dev.of_node; 213274317984SJean-Christophe PLAGNIOL-VILLARD 213374317984SJean-Christophe PLAGNIOL-VILLARD if (!np) 213474317984SJean-Christophe PLAGNIOL-VILLARD return 0; 213574317984SJean-Christophe PLAGNIOL-VILLARD 213674317984SJean-Christophe PLAGNIOL-VILLARD nb = of_gpio_named_count(np, "cs-gpios"); 21378caab75fSGeert Uytterhoeven ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 213874317984SJean-Christophe PLAGNIOL-VILLARD 21398ec5d84eSAndreas Larsson /* Return error only for an incorrectly formed cs-gpios property */ 21408ec5d84eSAndreas Larsson if (nb == 0 || nb == -ENOENT) 214174317984SJean-Christophe PLAGNIOL-VILLARD return 0; 21428ec5d84eSAndreas Larsson else if (nb < 0) 21438ec5d84eSAndreas Larsson return nb; 214474317984SJean-Christophe PLAGNIOL-VILLARD 2145a86854d0SKees Cook cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), 214674317984SJean-Christophe PLAGNIOL-VILLARD GFP_KERNEL); 21478caab75fSGeert Uytterhoeven ctlr->cs_gpios = cs; 214874317984SJean-Christophe PLAGNIOL-VILLARD 21498caab75fSGeert Uytterhoeven if (!ctlr->cs_gpios) 215074317984SJean-Christophe PLAGNIOL-VILLARD return -ENOMEM; 215174317984SJean-Christophe PLAGNIOL-VILLARD 21528caab75fSGeert Uytterhoeven for (i = 0; i < ctlr->num_chipselect; i++) 2153446411e1SAndreas Larsson cs[i] = -ENOENT; 215474317984SJean-Christophe PLAGNIOL-VILLARD 215574317984SJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < nb; i++) 215674317984SJean-Christophe PLAGNIOL-VILLARD cs[i] = of_get_named_gpio(np, "cs-gpios", i); 215774317984SJean-Christophe PLAGNIOL-VILLARD 215874317984SJean-Christophe PLAGNIOL-VILLARD return 0; 215974317984SJean-Christophe PLAGNIOL-VILLARD } 216074317984SJean-Christophe PLAGNIOL-VILLARD #else 21618caab75fSGeert Uytterhoeven static int of_spi_register_master(struct spi_controller *ctlr) 216274317984SJean-Christophe PLAGNIOL-VILLARD { 216374317984SJean-Christophe PLAGNIOL-VILLARD return 0; 216474317984SJean-Christophe PLAGNIOL-VILLARD } 216574317984SJean-Christophe PLAGNIOL-VILLARD #endif 216674317984SJean-Christophe PLAGNIOL-VILLARD 2167f3186dd8SLinus Walleij /** 2168f3186dd8SLinus Walleij * spi_get_gpio_descs() - grab chip select GPIOs for the master 2169f3186dd8SLinus Walleij * @ctlr: The SPI master to grab GPIO descriptors for 2170f3186dd8SLinus Walleij */ 2171f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr) 2172f3186dd8SLinus Walleij { 2173f3186dd8SLinus Walleij int nb, i; 2174f3186dd8SLinus Walleij struct gpio_desc **cs; 2175f3186dd8SLinus Walleij struct device *dev = &ctlr->dev; 2176f3186dd8SLinus Walleij 2177f3186dd8SLinus Walleij nb = gpiod_count(dev, "cs"); 2178f3186dd8SLinus Walleij ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2179f3186dd8SLinus Walleij 2180f3186dd8SLinus Walleij /* No GPIOs at all is fine, else return the error */ 2181f3186dd8SLinus Walleij if (nb == 0 || nb == -ENOENT) 2182f3186dd8SLinus Walleij return 0; 2183f3186dd8SLinus Walleij else if (nb < 0) 2184f3186dd8SLinus Walleij return nb; 2185f3186dd8SLinus Walleij 2186f3186dd8SLinus Walleij cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), 2187f3186dd8SLinus Walleij GFP_KERNEL); 2188f3186dd8SLinus Walleij if (!cs) 2189f3186dd8SLinus Walleij return -ENOMEM; 2190f3186dd8SLinus Walleij ctlr->cs_gpiods = cs; 2191f3186dd8SLinus Walleij 2192f3186dd8SLinus Walleij for (i = 0; i < nb; i++) { 2193f3186dd8SLinus Walleij /* 2194f3186dd8SLinus Walleij * Most chipselects are active low, the inverted 2195f3186dd8SLinus Walleij * semantics are handled by special quirks in gpiolib, 2196f3186dd8SLinus Walleij * so initializing them GPIOD_OUT_LOW here means 2197f3186dd8SLinus Walleij * "unasserted", in most cases this will drive the physical 2198f3186dd8SLinus Walleij * line high. 2199f3186dd8SLinus Walleij */ 2200f3186dd8SLinus Walleij cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, 2201f3186dd8SLinus Walleij GPIOD_OUT_LOW); 22021723fdecSGeert Uytterhoeven if (IS_ERR(cs[i])) 22031723fdecSGeert Uytterhoeven return PTR_ERR(cs[i]); 2204f3186dd8SLinus Walleij 2205f3186dd8SLinus Walleij if (cs[i]) { 2206f3186dd8SLinus Walleij /* 2207f3186dd8SLinus Walleij * If we find a CS GPIO, name it after the device and 2208f3186dd8SLinus Walleij * chip select line. 2209f3186dd8SLinus Walleij */ 2210f3186dd8SLinus Walleij char *gpioname; 2211f3186dd8SLinus Walleij 2212f3186dd8SLinus Walleij gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", 2213f3186dd8SLinus Walleij dev_name(dev), i); 2214f3186dd8SLinus Walleij if (!gpioname) 2215f3186dd8SLinus Walleij return -ENOMEM; 2216f3186dd8SLinus Walleij gpiod_set_consumer_name(cs[i], gpioname); 2217f3186dd8SLinus Walleij } 2218f3186dd8SLinus Walleij } 2219f3186dd8SLinus Walleij 2220f3186dd8SLinus Walleij return 0; 2221f3186dd8SLinus Walleij } 2222f3186dd8SLinus Walleij 2223bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr) 2224bdf3a3b5SBoris Brezillon { 2225bdf3a3b5SBoris Brezillon /* 2226b5932f5cSBoris Brezillon * The controller may implement only the high-level SPI-memory like 2227b5932f5cSBoris Brezillon * operations if it does not support regular SPI transfers, and this is 2228b5932f5cSBoris Brezillon * valid use case. 2229b5932f5cSBoris Brezillon * If ->mem_ops is NULL, we request that at least one of the 2230b5932f5cSBoris Brezillon * ->transfer_xxx() method be implemented. 2231bdf3a3b5SBoris Brezillon */ 2232b5932f5cSBoris Brezillon if (ctlr->mem_ops) { 2233b5932f5cSBoris Brezillon if (!ctlr->mem_ops->exec_op) 2234bdf3a3b5SBoris Brezillon return -EINVAL; 2235b5932f5cSBoris Brezillon } else if (!ctlr->transfer && !ctlr->transfer_one && 2236b5932f5cSBoris Brezillon !ctlr->transfer_one_message) { 2237b5932f5cSBoris Brezillon return -EINVAL; 2238b5932f5cSBoris Brezillon } 2239bdf3a3b5SBoris Brezillon 2240bdf3a3b5SBoris Brezillon return 0; 2241bdf3a3b5SBoris Brezillon } 2242bdf3a3b5SBoris Brezillon 22438ae12a0dSDavid Brownell /** 22448caab75fSGeert Uytterhoeven * spi_register_controller - register SPI master or slave controller 22458caab75fSGeert Uytterhoeven * @ctlr: initialized master, originally from spi_alloc_master() or 22468caab75fSGeert Uytterhoeven * spi_alloc_slave() 224733e34dc6SDavid Brownell * Context: can sleep 22488ae12a0dSDavid Brownell * 22498caab75fSGeert Uytterhoeven * SPI controllers connect to their drivers using some non-SPI bus, 22508ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 22518caab75fSGeert Uytterhoeven * includes calling spi_register_controller() to hook up to this SPI bus glue. 22528ae12a0dSDavid Brownell * 22538ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 22548ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 22558ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 22568ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 22578ae12a0dSDavid Brownell * chip is at which address. 22588ae12a0dSDavid Brownell * 22598ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 22608caab75fSGeert Uytterhoeven * success, else a negative error code (dropping the controller's refcount). 22610c868461SDavid Brownell * After a successful return, the caller is responsible for calling 22628caab75fSGeert Uytterhoeven * spi_unregister_controller(). 226397d56dc6SJavier Martinez Canillas * 226497d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 22658ae12a0dSDavid Brownell */ 22668caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr) 22678ae12a0dSDavid Brownell { 22688caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 22692b9603a0SFeng Tang struct boardinfo *bi; 2270b93318a2SSergei Shtylyov int status; 227142bdd706SLucas Stach int id, first_dynamic; 22728ae12a0dSDavid Brownell 22730c868461SDavid Brownell if (!dev) 22740c868461SDavid Brownell return -ENODEV; 22750c868461SDavid Brownell 2276bdf3a3b5SBoris Brezillon /* 2277bdf3a3b5SBoris Brezillon * Make sure all necessary hooks are implemented before registering 2278bdf3a3b5SBoris Brezillon * the SPI controller. 2279bdf3a3b5SBoris Brezillon */ 2280bdf3a3b5SBoris Brezillon status = spi_controller_check_ops(ctlr); 2281bdf3a3b5SBoris Brezillon if (status) 2282bdf3a3b5SBoris Brezillon return status; 2283bdf3a3b5SBoris Brezillon 2284082c8cb4SDavid Brownell /* even if it's just one always-selected device, there must 2285082c8cb4SDavid Brownell * be at least one chipselect 2286082c8cb4SDavid Brownell */ 22878caab75fSGeert Uytterhoeven if (ctlr->num_chipselect == 0) 2288082c8cb4SDavid Brownell return -EINVAL; 228904b2d03aSGeert Uytterhoeven if (ctlr->bus_num >= 0) { 229004b2d03aSGeert Uytterhoeven /* devices with a fixed bus num must check-in with the num */ 229104b2d03aSGeert Uytterhoeven mutex_lock(&board_lock); 229204b2d03aSGeert Uytterhoeven id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 229304b2d03aSGeert Uytterhoeven ctlr->bus_num + 1, GFP_KERNEL); 229404b2d03aSGeert Uytterhoeven mutex_unlock(&board_lock); 229504b2d03aSGeert Uytterhoeven if (WARN(id < 0, "couldn't get idr")) 229604b2d03aSGeert Uytterhoeven return id == -ENOSPC ? -EBUSY : id; 229704b2d03aSGeert Uytterhoeven ctlr->bus_num = id; 229804b2d03aSGeert Uytterhoeven } else if (ctlr->dev.of_node) { 22999b61e302SSuniel Mahesh /* allocate dynamic bus number using Linux idr */ 23009b61e302SSuniel Mahesh id = of_alias_get_id(ctlr->dev.of_node, "spi"); 23019b61e302SSuniel Mahesh if (id >= 0) { 23029b61e302SSuniel Mahesh ctlr->bus_num = id; 23039b61e302SSuniel Mahesh mutex_lock(&board_lock); 23049b61e302SSuniel Mahesh id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 23059b61e302SSuniel Mahesh ctlr->bus_num + 1, GFP_KERNEL); 23069b61e302SSuniel Mahesh mutex_unlock(&board_lock); 23079b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 23089b61e302SSuniel Mahesh return id == -ENOSPC ? -EBUSY : id; 23099b61e302SSuniel Mahesh } 23109b61e302SSuniel Mahesh } 23118caab75fSGeert Uytterhoeven if (ctlr->bus_num < 0) { 231242bdd706SLucas Stach first_dynamic = of_alias_get_highest_id("spi"); 231342bdd706SLucas Stach if (first_dynamic < 0) 231442bdd706SLucas Stach first_dynamic = 0; 231542bdd706SLucas Stach else 231642bdd706SLucas Stach first_dynamic++; 231742bdd706SLucas Stach 23189b61e302SSuniel Mahesh mutex_lock(&board_lock); 231942bdd706SLucas Stach id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, 232042bdd706SLucas Stach 0, GFP_KERNEL); 23219b61e302SSuniel Mahesh mutex_unlock(&board_lock); 23229b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 23239b61e302SSuniel Mahesh return id; 23249b61e302SSuniel Mahesh ctlr->bus_num = id; 23258ae12a0dSDavid Brownell } 23268caab75fSGeert Uytterhoeven INIT_LIST_HEAD(&ctlr->queue); 23278caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->queue_lock); 23288caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->bus_lock_spinlock); 23298caab75fSGeert Uytterhoeven mutex_init(&ctlr->bus_lock_mutex); 23308caab75fSGeert Uytterhoeven mutex_init(&ctlr->io_mutex); 23318caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 23328caab75fSGeert Uytterhoeven init_completion(&ctlr->xfer_completion); 23338caab75fSGeert Uytterhoeven if (!ctlr->max_dma_len) 23348caab75fSGeert Uytterhoeven ctlr->max_dma_len = INT_MAX; 2335cf32b71eSErnst Schwab 23368ae12a0dSDavid Brownell /* register the device, then userspace will see it. 23378ae12a0dSDavid Brownell * registration fails if the bus ID is in use. 23388ae12a0dSDavid Brownell */ 23398caab75fSGeert Uytterhoeven dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 23400a919ae4SAndrey Smirnov 23410a919ae4SAndrey Smirnov if (!spi_controller_is_slave(ctlr)) { 23420a919ae4SAndrey Smirnov if (ctlr->use_gpio_descriptors) { 23430a919ae4SAndrey Smirnov status = spi_get_gpio_descs(ctlr); 23440a919ae4SAndrey Smirnov if (status) 23450a919ae4SAndrey Smirnov return status; 23460a919ae4SAndrey Smirnov /* 23470a919ae4SAndrey Smirnov * A controller using GPIO descriptors always 23480a919ae4SAndrey Smirnov * supports SPI_CS_HIGH if need be. 23490a919ae4SAndrey Smirnov */ 23500a919ae4SAndrey Smirnov ctlr->mode_bits |= SPI_CS_HIGH; 23510a919ae4SAndrey Smirnov } else { 23520a919ae4SAndrey Smirnov /* Legacy code path for GPIOs from DT */ 23530a919ae4SAndrey Smirnov status = of_spi_register_master(ctlr); 23540a919ae4SAndrey Smirnov if (status) 23550a919ae4SAndrey Smirnov return status; 23560a919ae4SAndrey Smirnov } 23570a919ae4SAndrey Smirnov } 23580a919ae4SAndrey Smirnov 23598caab75fSGeert Uytterhoeven status = device_add(&ctlr->dev); 23609b61e302SSuniel Mahesh if (status < 0) { 23619b61e302SSuniel Mahesh /* free bus id */ 23629b61e302SSuniel Mahesh mutex_lock(&board_lock); 23639b61e302SSuniel Mahesh idr_remove(&spi_master_idr, ctlr->bus_num); 23649b61e302SSuniel Mahesh mutex_unlock(&board_lock); 23658ae12a0dSDavid Brownell goto done; 23669b61e302SSuniel Mahesh } 23679b61e302SSuniel Mahesh dev_dbg(dev, "registered %s %s\n", 23688caab75fSGeert Uytterhoeven spi_controller_is_slave(ctlr) ? "slave" : "master", 23699b61e302SSuniel Mahesh dev_name(&ctlr->dev)); 23708ae12a0dSDavid Brownell 2371b5932f5cSBoris Brezillon /* 2372b5932f5cSBoris Brezillon * If we're using a queued driver, start the queue. Note that we don't 2373b5932f5cSBoris Brezillon * need the queueing logic if the driver is only supporting high-level 2374b5932f5cSBoris Brezillon * memory operations. 2375b5932f5cSBoris Brezillon */ 2376b5932f5cSBoris Brezillon if (ctlr->transfer) { 23778caab75fSGeert Uytterhoeven dev_info(dev, "controller is unqueued, this is deprecated\n"); 2378b5932f5cSBoris Brezillon } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 23798caab75fSGeert Uytterhoeven status = spi_controller_initialize_queue(ctlr); 2380ffbbdd21SLinus Walleij if (status) { 23818caab75fSGeert Uytterhoeven device_del(&ctlr->dev); 23829b61e302SSuniel Mahesh /* free bus id */ 23839b61e302SSuniel Mahesh mutex_lock(&board_lock); 23849b61e302SSuniel Mahesh idr_remove(&spi_master_idr, ctlr->bus_num); 23859b61e302SSuniel Mahesh mutex_unlock(&board_lock); 2386ffbbdd21SLinus Walleij goto done; 2387ffbbdd21SLinus Walleij } 2388ffbbdd21SLinus Walleij } 2389eca2ebc7SMartin Sperl /* add statistics */ 23908caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->statistics.lock); 2391ffbbdd21SLinus Walleij 23922b9603a0SFeng Tang mutex_lock(&board_lock); 23938caab75fSGeert Uytterhoeven list_add_tail(&ctlr->list, &spi_controller_list); 23942b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 23958caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 23962b9603a0SFeng Tang mutex_unlock(&board_lock); 23972b9603a0SFeng Tang 239864bee4d2SMika Westerberg /* Register devices from the device tree and ACPI */ 23998caab75fSGeert Uytterhoeven of_register_spi_devices(ctlr); 24008caab75fSGeert Uytterhoeven acpi_register_spi_devices(ctlr); 24018ae12a0dSDavid Brownell done: 24028ae12a0dSDavid Brownell return status; 24038ae12a0dSDavid Brownell } 24048caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller); 24058ae12a0dSDavid Brownell 2406666d5b4cSMark Brown static void devm_spi_unregister(struct device *dev, void *res) 2407666d5b4cSMark Brown { 24088caab75fSGeert Uytterhoeven spi_unregister_controller(*(struct spi_controller **)res); 2409666d5b4cSMark Brown } 2410666d5b4cSMark Brown 2411666d5b4cSMark Brown /** 24128caab75fSGeert Uytterhoeven * devm_spi_register_controller - register managed SPI master or slave 24138caab75fSGeert Uytterhoeven * controller 24148caab75fSGeert Uytterhoeven * @dev: device managing SPI controller 24158caab75fSGeert Uytterhoeven * @ctlr: initialized controller, originally from spi_alloc_master() or 24168caab75fSGeert Uytterhoeven * spi_alloc_slave() 2417666d5b4cSMark Brown * Context: can sleep 2418666d5b4cSMark Brown * 24198caab75fSGeert Uytterhoeven * Register a SPI device as with spi_register_controller() which will 242068b892f1SJohan Hovold * automatically be unregistered and freed. 242197d56dc6SJavier Martinez Canillas * 242297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 2423666d5b4cSMark Brown */ 24248caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev, 24258caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 2426666d5b4cSMark Brown { 24278caab75fSGeert Uytterhoeven struct spi_controller **ptr; 2428666d5b4cSMark Brown int ret; 2429666d5b4cSMark Brown 2430666d5b4cSMark Brown ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); 2431666d5b4cSMark Brown if (!ptr) 2432666d5b4cSMark Brown return -ENOMEM; 2433666d5b4cSMark Brown 24348caab75fSGeert Uytterhoeven ret = spi_register_controller(ctlr); 24354b92894eSStephen Warren if (!ret) { 24368caab75fSGeert Uytterhoeven *ptr = ctlr; 2437666d5b4cSMark Brown devres_add(dev, ptr); 2438666d5b4cSMark Brown } else { 2439666d5b4cSMark Brown devres_free(ptr); 2440666d5b4cSMark Brown } 2441666d5b4cSMark Brown 2442666d5b4cSMark Brown return ret; 2443666d5b4cSMark Brown } 24448caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller); 2445666d5b4cSMark Brown 244634860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 24478ae12a0dSDavid Brownell { 24480c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 24498ae12a0dSDavid Brownell return 0; 24508ae12a0dSDavid Brownell } 24518ae12a0dSDavid Brownell 24528ae12a0dSDavid Brownell /** 24538caab75fSGeert Uytterhoeven * spi_unregister_controller - unregister SPI master or slave controller 24548caab75fSGeert Uytterhoeven * @ctlr: the controller being unregistered 245533e34dc6SDavid Brownell * Context: can sleep 24568ae12a0dSDavid Brownell * 24578caab75fSGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 24588ae12a0dSDavid Brownell * only ones directly touching chip registers. 24598ae12a0dSDavid Brownell * 24608ae12a0dSDavid Brownell * This must be called from context that can sleep. 246168b892f1SJohan Hovold * 246268b892f1SJohan Hovold * Note that this function also drops a reference to the controller. 24638ae12a0dSDavid Brownell */ 24648caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr) 24658ae12a0dSDavid Brownell { 24669b61e302SSuniel Mahesh struct spi_controller *found; 246767f7b278SJohan Hovold int id = ctlr->bus_num; 246889fc9a1aSJeff Garzik int dummy; 246989fc9a1aSJeff Garzik 24709b61e302SSuniel Mahesh /* First make sure that this controller was ever added */ 24719b61e302SSuniel Mahesh mutex_lock(&board_lock); 247267f7b278SJohan Hovold found = idr_find(&spi_master_idr, id); 24739b61e302SSuniel Mahesh mutex_unlock(&board_lock); 24748caab75fSGeert Uytterhoeven if (ctlr->queued) { 24758caab75fSGeert Uytterhoeven if (spi_destroy_queue(ctlr)) 24768caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue remove failed\n"); 2477ffbbdd21SLinus Walleij } 24782b9603a0SFeng Tang mutex_lock(&board_lock); 24798caab75fSGeert Uytterhoeven list_del(&ctlr->list); 24802b9603a0SFeng Tang mutex_unlock(&board_lock); 24812b9603a0SFeng Tang 24828caab75fSGeert Uytterhoeven dummy = device_for_each_child(&ctlr->dev, NULL, __unregister); 24838caab75fSGeert Uytterhoeven device_unregister(&ctlr->dev); 24849b61e302SSuniel Mahesh /* free bus id */ 24859b61e302SSuniel Mahesh mutex_lock(&board_lock); 2486613bd1eaSJarkko Nikula if (found == ctlr) 248767f7b278SJohan Hovold idr_remove(&spi_master_idr, id); 24889b61e302SSuniel Mahesh mutex_unlock(&board_lock); 24898ae12a0dSDavid Brownell } 24908caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller); 24918ae12a0dSDavid Brownell 24928caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr) 2493ffbbdd21SLinus Walleij { 2494ffbbdd21SLinus Walleij int ret; 2495ffbbdd21SLinus Walleij 24968caab75fSGeert Uytterhoeven /* Basically no-ops for non-queued controllers */ 24978caab75fSGeert Uytterhoeven if (!ctlr->queued) 2498ffbbdd21SLinus Walleij return 0; 2499ffbbdd21SLinus Walleij 25008caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 2501ffbbdd21SLinus Walleij if (ret) 25028caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue stop failed\n"); 2503ffbbdd21SLinus Walleij 2504ffbbdd21SLinus Walleij return ret; 2505ffbbdd21SLinus Walleij } 25068caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend); 2507ffbbdd21SLinus Walleij 25088caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr) 2509ffbbdd21SLinus Walleij { 2510ffbbdd21SLinus Walleij int ret; 2511ffbbdd21SLinus Walleij 25128caab75fSGeert Uytterhoeven if (!ctlr->queued) 2513ffbbdd21SLinus Walleij return 0; 2514ffbbdd21SLinus Walleij 25158caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 2516ffbbdd21SLinus Walleij if (ret) 25178caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue restart failed\n"); 2518ffbbdd21SLinus Walleij 2519ffbbdd21SLinus Walleij return ret; 2520ffbbdd21SLinus Walleij } 25218caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume); 2522ffbbdd21SLinus Walleij 25238caab75fSGeert Uytterhoeven static int __spi_controller_match(struct device *dev, const void *data) 25245ed2c832SDave Young { 25258caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 25269f3b795aSMichał Mirosław const u16 *bus_num = data; 25275ed2c832SDave Young 25288caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 25298caab75fSGeert Uytterhoeven return ctlr->bus_num == *bus_num; 25305ed2c832SDave Young } 25315ed2c832SDave Young 25328ae12a0dSDavid Brownell /** 25338ae12a0dSDavid Brownell * spi_busnum_to_master - look up master associated with bus_num 25348ae12a0dSDavid Brownell * @bus_num: the master's bus number 253533e34dc6SDavid Brownell * Context: can sleep 25368ae12a0dSDavid Brownell * 25378ae12a0dSDavid Brownell * This call may be used with devices that are registered after 25388ae12a0dSDavid Brownell * arch init time. It returns a refcounted pointer to the relevant 25398caab75fSGeert Uytterhoeven * spi_controller (which the caller must release), or NULL if there is 25408ae12a0dSDavid Brownell * no such master registered. 254197d56dc6SJavier Martinez Canillas * 254297d56dc6SJavier Martinez Canillas * Return: the SPI master structure on success, else NULL. 25438ae12a0dSDavid Brownell */ 25448caab75fSGeert Uytterhoeven struct spi_controller *spi_busnum_to_master(u16 bus_num) 25458ae12a0dSDavid Brownell { 254649dce689STony Jones struct device *dev; 25478caab75fSGeert Uytterhoeven struct spi_controller *ctlr = NULL; 25488ae12a0dSDavid Brownell 2549695794aeSGreg Kroah-Hartman dev = class_find_device(&spi_master_class, NULL, &bus_num, 25508caab75fSGeert Uytterhoeven __spi_controller_match); 25515ed2c832SDave Young if (dev) 25528caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 25535ed2c832SDave Young /* reference got in class_find_device */ 25548caab75fSGeert Uytterhoeven return ctlr; 25558ae12a0dSDavid Brownell } 25568ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master); 25578ae12a0dSDavid Brownell 2558d780c371SMartin Sperl /*-------------------------------------------------------------------------*/ 2559d780c371SMartin Sperl 2560d780c371SMartin Sperl /* Core methods for SPI resource management */ 2561d780c371SMartin Sperl 2562d780c371SMartin Sperl /** 2563d780c371SMartin Sperl * spi_res_alloc - allocate a spi resource that is life-cycle managed 2564d780c371SMartin Sperl * during the processing of a spi_message while using 2565d780c371SMartin Sperl * spi_transfer_one 2566d780c371SMartin Sperl * @spi: the spi device for which we allocate memory 2567d780c371SMartin Sperl * @release: the release code to execute for this resource 2568d780c371SMartin Sperl * @size: size to alloc and return 2569d780c371SMartin Sperl * @gfp: GFP allocation flags 2570d780c371SMartin Sperl * 2571d780c371SMartin Sperl * Return: the pointer to the allocated data 2572d780c371SMartin Sperl * 2573d780c371SMartin Sperl * This may get enhanced in the future to allocate from a memory pool 25748caab75fSGeert Uytterhoeven * of the @spi_device or @spi_controller to avoid repeated allocations. 2575d780c371SMartin Sperl */ 2576d780c371SMartin Sperl void *spi_res_alloc(struct spi_device *spi, 2577d780c371SMartin Sperl spi_res_release_t release, 2578d780c371SMartin Sperl size_t size, gfp_t gfp) 2579d780c371SMartin Sperl { 2580d780c371SMartin Sperl struct spi_res *sres; 2581d780c371SMartin Sperl 2582d780c371SMartin Sperl sres = kzalloc(sizeof(*sres) + size, gfp); 2583d780c371SMartin Sperl if (!sres) 2584d780c371SMartin Sperl return NULL; 2585d780c371SMartin Sperl 2586d780c371SMartin Sperl INIT_LIST_HEAD(&sres->entry); 2587d780c371SMartin Sperl sres->release = release; 2588d780c371SMartin Sperl 2589d780c371SMartin Sperl return sres->data; 2590d780c371SMartin Sperl } 2591d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_alloc); 2592d780c371SMartin Sperl 2593d780c371SMartin Sperl /** 2594d780c371SMartin Sperl * spi_res_free - free an spi resource 2595d780c371SMartin Sperl * @res: pointer to the custom data of a resource 2596d780c371SMartin Sperl * 2597d780c371SMartin Sperl */ 2598d780c371SMartin Sperl void spi_res_free(void *res) 2599d780c371SMartin Sperl { 2600d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 2601d780c371SMartin Sperl 2602d780c371SMartin Sperl if (!res) 2603d780c371SMartin Sperl return; 2604d780c371SMartin Sperl 2605d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 2606d780c371SMartin Sperl kfree(sres); 2607d780c371SMartin Sperl } 2608d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_free); 2609d780c371SMartin Sperl 2610d780c371SMartin Sperl /** 2611d780c371SMartin Sperl * spi_res_add - add a spi_res to the spi_message 2612d780c371SMartin Sperl * @message: the spi message 2613d780c371SMartin Sperl * @res: the spi_resource 2614d780c371SMartin Sperl */ 2615d780c371SMartin Sperl void spi_res_add(struct spi_message *message, void *res) 2616d780c371SMartin Sperl { 2617d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 2618d780c371SMartin Sperl 2619d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 2620d780c371SMartin Sperl list_add_tail(&sres->entry, &message->resources); 2621d780c371SMartin Sperl } 2622d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_add); 2623d780c371SMartin Sperl 2624d780c371SMartin Sperl /** 2625d780c371SMartin Sperl * spi_res_release - release all spi resources for this message 26268caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller 2627d780c371SMartin Sperl * @message: the @spi_message 2628d780c371SMartin Sperl */ 26298caab75fSGeert Uytterhoeven void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 2630d780c371SMartin Sperl { 2631d780c371SMartin Sperl struct spi_res *res; 2632d780c371SMartin Sperl 2633d780c371SMartin Sperl while (!list_empty(&message->resources)) { 2634d780c371SMartin Sperl res = list_last_entry(&message->resources, 2635d780c371SMartin Sperl struct spi_res, entry); 2636d780c371SMartin Sperl 2637d780c371SMartin Sperl if (res->release) 26388caab75fSGeert Uytterhoeven res->release(ctlr, message, res->data); 2639d780c371SMartin Sperl 2640d780c371SMartin Sperl list_del(&res->entry); 2641d780c371SMartin Sperl 2642d780c371SMartin Sperl kfree(res); 2643d780c371SMartin Sperl } 2644d780c371SMartin Sperl } 2645d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_release); 26468ae12a0dSDavid Brownell 26478ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 26488ae12a0dSDavid Brownell 2649523baf5aSMartin Sperl /* Core methods for spi_message alterations */ 2650523baf5aSMartin Sperl 26518caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr, 2652523baf5aSMartin Sperl struct spi_message *msg, 2653523baf5aSMartin Sperl void *res) 2654523baf5aSMartin Sperl { 2655523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer = res; 2656523baf5aSMartin Sperl size_t i; 2657523baf5aSMartin Sperl 2658523baf5aSMartin Sperl /* call extra callback if requested */ 2659523baf5aSMartin Sperl if (rxfer->release) 26608caab75fSGeert Uytterhoeven rxfer->release(ctlr, msg, res); 2661523baf5aSMartin Sperl 2662523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 2663523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 2664523baf5aSMartin Sperl 2665523baf5aSMartin Sperl /* remove the formerly inserted entries */ 2666523baf5aSMartin Sperl for (i = 0; i < rxfer->inserted; i++) 2667523baf5aSMartin Sperl list_del(&rxfer->inserted_transfers[i].transfer_list); 2668523baf5aSMartin Sperl } 2669523baf5aSMartin Sperl 2670523baf5aSMartin Sperl /** 2671523baf5aSMartin Sperl * spi_replace_transfers - replace transfers with several transfers 2672523baf5aSMartin Sperl * and register change with spi_message.resources 2673523baf5aSMartin Sperl * @msg: the spi_message we work upon 2674523baf5aSMartin Sperl * @xfer_first: the first spi_transfer we want to replace 2675523baf5aSMartin Sperl * @remove: number of transfers to remove 2676523baf5aSMartin Sperl * @insert: the number of transfers we want to insert instead 2677523baf5aSMartin Sperl * @release: extra release code necessary in some circumstances 2678523baf5aSMartin Sperl * @extradatasize: extra data to allocate (with alignment guarantees 2679523baf5aSMartin Sperl * of struct @spi_transfer) 268005885397SMartin Sperl * @gfp: gfp flags 2681523baf5aSMartin Sperl * 2682523baf5aSMartin Sperl * Returns: pointer to @spi_replaced_transfers, 2683523baf5aSMartin Sperl * PTR_ERR(...) in case of errors. 2684523baf5aSMartin Sperl */ 2685523baf5aSMartin Sperl struct spi_replaced_transfers *spi_replace_transfers( 2686523baf5aSMartin Sperl struct spi_message *msg, 2687523baf5aSMartin Sperl struct spi_transfer *xfer_first, 2688523baf5aSMartin Sperl size_t remove, 2689523baf5aSMartin Sperl size_t insert, 2690523baf5aSMartin Sperl spi_replaced_release_t release, 2691523baf5aSMartin Sperl size_t extradatasize, 2692523baf5aSMartin Sperl gfp_t gfp) 2693523baf5aSMartin Sperl { 2694523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer; 2695523baf5aSMartin Sperl struct spi_transfer *xfer; 2696523baf5aSMartin Sperl size_t i; 2697523baf5aSMartin Sperl 2698523baf5aSMartin Sperl /* allocate the structure using spi_res */ 2699523baf5aSMartin Sperl rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 2700523baf5aSMartin Sperl insert * sizeof(struct spi_transfer) 2701523baf5aSMartin Sperl + sizeof(struct spi_replaced_transfers) 2702523baf5aSMartin Sperl + extradatasize, 2703523baf5aSMartin Sperl gfp); 2704523baf5aSMartin Sperl if (!rxfer) 2705523baf5aSMartin Sperl return ERR_PTR(-ENOMEM); 2706523baf5aSMartin Sperl 2707523baf5aSMartin Sperl /* the release code to invoke before running the generic release */ 2708523baf5aSMartin Sperl rxfer->release = release; 2709523baf5aSMartin Sperl 2710523baf5aSMartin Sperl /* assign extradata */ 2711523baf5aSMartin Sperl if (extradatasize) 2712523baf5aSMartin Sperl rxfer->extradata = 2713523baf5aSMartin Sperl &rxfer->inserted_transfers[insert]; 2714523baf5aSMartin Sperl 2715523baf5aSMartin Sperl /* init the replaced_transfers list */ 2716523baf5aSMartin Sperl INIT_LIST_HEAD(&rxfer->replaced_transfers); 2717523baf5aSMartin Sperl 2718523baf5aSMartin Sperl /* assign the list_entry after which we should reinsert 2719523baf5aSMartin Sperl * the @replaced_transfers - it may be spi_message.messages! 2720523baf5aSMartin Sperl */ 2721523baf5aSMartin Sperl rxfer->replaced_after = xfer_first->transfer_list.prev; 2722523baf5aSMartin Sperl 2723523baf5aSMartin Sperl /* remove the requested number of transfers */ 2724523baf5aSMartin Sperl for (i = 0; i < remove; i++) { 2725523baf5aSMartin Sperl /* if the entry after replaced_after it is msg->transfers 2726523baf5aSMartin Sperl * then we have been requested to remove more transfers 2727523baf5aSMartin Sperl * than are in the list 2728523baf5aSMartin Sperl */ 2729523baf5aSMartin Sperl if (rxfer->replaced_after->next == &msg->transfers) { 2730523baf5aSMartin Sperl dev_err(&msg->spi->dev, 2731523baf5aSMartin Sperl "requested to remove more spi_transfers than are available\n"); 2732523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 2733523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, 2734523baf5aSMartin Sperl rxfer->replaced_after); 2735523baf5aSMartin Sperl 2736523baf5aSMartin Sperl /* free the spi_replace_transfer structure */ 2737523baf5aSMartin Sperl spi_res_free(rxfer); 2738523baf5aSMartin Sperl 2739523baf5aSMartin Sperl /* and return with an error */ 2740523baf5aSMartin Sperl return ERR_PTR(-EINVAL); 2741523baf5aSMartin Sperl } 2742523baf5aSMartin Sperl 2743523baf5aSMartin Sperl /* remove the entry after replaced_after from list of 2744523baf5aSMartin Sperl * transfers and add it to list of replaced_transfers 2745523baf5aSMartin Sperl */ 2746523baf5aSMartin Sperl list_move_tail(rxfer->replaced_after->next, 2747523baf5aSMartin Sperl &rxfer->replaced_transfers); 2748523baf5aSMartin Sperl } 2749523baf5aSMartin Sperl 2750523baf5aSMartin Sperl /* create copy of the given xfer with identical settings 2751523baf5aSMartin Sperl * based on the first transfer to get removed 2752523baf5aSMartin Sperl */ 2753523baf5aSMartin Sperl for (i = 0; i < insert; i++) { 2754523baf5aSMartin Sperl /* we need to run in reverse order */ 2755523baf5aSMartin Sperl xfer = &rxfer->inserted_transfers[insert - 1 - i]; 2756523baf5aSMartin Sperl 2757523baf5aSMartin Sperl /* copy all spi_transfer data */ 2758523baf5aSMartin Sperl memcpy(xfer, xfer_first, sizeof(*xfer)); 2759523baf5aSMartin Sperl 2760523baf5aSMartin Sperl /* add to list */ 2761523baf5aSMartin Sperl list_add(&xfer->transfer_list, rxfer->replaced_after); 2762523baf5aSMartin Sperl 2763523baf5aSMartin Sperl /* clear cs_change and delay_usecs for all but the last */ 2764523baf5aSMartin Sperl if (i) { 2765523baf5aSMartin Sperl xfer->cs_change = false; 2766523baf5aSMartin Sperl xfer->delay_usecs = 0; 2767523baf5aSMartin Sperl } 2768523baf5aSMartin Sperl } 2769523baf5aSMartin Sperl 2770523baf5aSMartin Sperl /* set up inserted */ 2771523baf5aSMartin Sperl rxfer->inserted = insert; 2772523baf5aSMartin Sperl 2773523baf5aSMartin Sperl /* and register it with spi_res/spi_message */ 2774523baf5aSMartin Sperl spi_res_add(msg, rxfer); 2775523baf5aSMartin Sperl 2776523baf5aSMartin Sperl return rxfer; 2777523baf5aSMartin Sperl } 2778523baf5aSMartin Sperl EXPORT_SYMBOL_GPL(spi_replace_transfers); 2779523baf5aSMartin Sperl 27808caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 2781d9f12122SMartin Sperl struct spi_message *msg, 2782d9f12122SMartin Sperl struct spi_transfer **xferp, 2783d9f12122SMartin Sperl size_t maxsize, 2784d9f12122SMartin Sperl gfp_t gfp) 2785d9f12122SMartin Sperl { 2786d9f12122SMartin Sperl struct spi_transfer *xfer = *xferp, *xfers; 2787d9f12122SMartin Sperl struct spi_replaced_transfers *srt; 2788d9f12122SMartin Sperl size_t offset; 2789d9f12122SMartin Sperl size_t count, i; 2790d9f12122SMartin Sperl 2791d9f12122SMartin Sperl /* calculate how many we have to replace */ 2792d9f12122SMartin Sperl count = DIV_ROUND_UP(xfer->len, maxsize); 2793d9f12122SMartin Sperl 2794d9f12122SMartin Sperl /* create replacement */ 2795d9f12122SMartin Sperl srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 2796657d32efSDan Carpenter if (IS_ERR(srt)) 2797657d32efSDan Carpenter return PTR_ERR(srt); 2798d9f12122SMartin Sperl xfers = srt->inserted_transfers; 2799d9f12122SMartin Sperl 2800d9f12122SMartin Sperl /* now handle each of those newly inserted spi_transfers 2801d9f12122SMartin Sperl * note that the replacements spi_transfers all are preset 2802d9f12122SMartin Sperl * to the same values as *xferp, so tx_buf, rx_buf and len 2803d9f12122SMartin Sperl * are all identical (as well as most others) 2804d9f12122SMartin Sperl * so we just have to fix up len and the pointers. 2805d9f12122SMartin Sperl * 2806d9f12122SMartin Sperl * this also includes support for the depreciated 2807d9f12122SMartin Sperl * spi_message.is_dma_mapped interface 2808d9f12122SMartin Sperl */ 2809d9f12122SMartin Sperl 2810d9f12122SMartin Sperl /* the first transfer just needs the length modified, so we 2811d9f12122SMartin Sperl * run it outside the loop 2812d9f12122SMartin Sperl */ 2813c8dab77aSFabio Estevam xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 2814d9f12122SMartin Sperl 2815d9f12122SMartin Sperl /* all the others need rx_buf/tx_buf also set */ 2816d9f12122SMartin Sperl for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 2817d9f12122SMartin Sperl /* update rx_buf, tx_buf and dma */ 2818d9f12122SMartin Sperl if (xfers[i].rx_buf) 2819d9f12122SMartin Sperl xfers[i].rx_buf += offset; 2820d9f12122SMartin Sperl if (xfers[i].rx_dma) 2821d9f12122SMartin Sperl xfers[i].rx_dma += offset; 2822d9f12122SMartin Sperl if (xfers[i].tx_buf) 2823d9f12122SMartin Sperl xfers[i].tx_buf += offset; 2824d9f12122SMartin Sperl if (xfers[i].tx_dma) 2825d9f12122SMartin Sperl xfers[i].tx_dma += offset; 2826d9f12122SMartin Sperl 2827d9f12122SMartin Sperl /* update length */ 2828d9f12122SMartin Sperl xfers[i].len = min(maxsize, xfers[i].len - offset); 2829d9f12122SMartin Sperl } 2830d9f12122SMartin Sperl 2831d9f12122SMartin Sperl /* we set up xferp to the last entry we have inserted, 2832d9f12122SMartin Sperl * so that we skip those already split transfers 2833d9f12122SMartin Sperl */ 2834d9f12122SMartin Sperl *xferp = &xfers[count - 1]; 2835d9f12122SMartin Sperl 2836d9f12122SMartin Sperl /* increment statistics counters */ 28378caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 2838d9f12122SMartin Sperl transfers_split_maxsize); 2839d9f12122SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, 2840d9f12122SMartin Sperl transfers_split_maxsize); 2841d9f12122SMartin Sperl 2842d9f12122SMartin Sperl return 0; 2843d9f12122SMartin Sperl } 2844d9f12122SMartin Sperl 2845d9f12122SMartin Sperl /** 2846d9f12122SMartin Sperl * spi_split_tranfers_maxsize - split spi transfers into multiple transfers 2847d9f12122SMartin Sperl * when an individual transfer exceeds a 2848d9f12122SMartin Sperl * certain size 28498caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller for this transfer 28503700ce95SMasanari Iida * @msg: the @spi_message to transform 28513700ce95SMasanari Iida * @maxsize: the maximum when to apply this 285210f11a22SJavier Martinez Canillas * @gfp: GFP allocation flags 2853d9f12122SMartin Sperl * 2854d9f12122SMartin Sperl * Return: status of transformation 2855d9f12122SMartin Sperl */ 28568caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr, 2857d9f12122SMartin Sperl struct spi_message *msg, 2858d9f12122SMartin Sperl size_t maxsize, 2859d9f12122SMartin Sperl gfp_t gfp) 2860d9f12122SMartin Sperl { 2861d9f12122SMartin Sperl struct spi_transfer *xfer; 2862d9f12122SMartin Sperl int ret; 2863d9f12122SMartin Sperl 2864d9f12122SMartin Sperl /* iterate over the transfer_list, 2865d9f12122SMartin Sperl * but note that xfer is advanced to the last transfer inserted 2866d9f12122SMartin Sperl * to avoid checking sizes again unnecessarily (also xfer does 2867d9f12122SMartin Sperl * potentiall belong to a different list by the time the 2868d9f12122SMartin Sperl * replacement has happened 2869d9f12122SMartin Sperl */ 2870d9f12122SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 2871d9f12122SMartin Sperl if (xfer->len > maxsize) { 28728caab75fSGeert Uytterhoeven ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 28738caab75fSGeert Uytterhoeven maxsize, gfp); 2874d9f12122SMartin Sperl if (ret) 2875d9f12122SMartin Sperl return ret; 2876d9f12122SMartin Sperl } 2877d9f12122SMartin Sperl } 2878d9f12122SMartin Sperl 2879d9f12122SMartin Sperl return 0; 2880d9f12122SMartin Sperl } 2881d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 28828ae12a0dSDavid Brownell 28838ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 28848ae12a0dSDavid Brownell 28858caab75fSGeert Uytterhoeven /* Core methods for SPI controller protocol drivers. Some of the 28867d077197SDavid Brownell * other core methods are currently defined as inline functions. 28877d077197SDavid Brownell */ 28887d077197SDavid Brownell 28898caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 28908caab75fSGeert Uytterhoeven u8 bits_per_word) 289163ab645fSStefan Brüns { 28928caab75fSGeert Uytterhoeven if (ctlr->bits_per_word_mask) { 289363ab645fSStefan Brüns /* Only 32 bits fit in the mask */ 289463ab645fSStefan Brüns if (bits_per_word > 32) 289563ab645fSStefan Brüns return -EINVAL; 28968caab75fSGeert Uytterhoeven if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 289763ab645fSStefan Brüns return -EINVAL; 289863ab645fSStefan Brüns } 289963ab645fSStefan Brüns 290063ab645fSStefan Brüns return 0; 290163ab645fSStefan Brüns } 290263ab645fSStefan Brüns 29037d077197SDavid Brownell /** 29047d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 29057d077197SDavid Brownell * @spi: the device whose settings are being modified 29067d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 29077d077197SDavid Brownell * 29087d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 29097d077197SDavid Brownell * device doesn't work with its default. They may likewise need 29107d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 29117d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 29127d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 29137d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 29147d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 29157d077197SDavid Brownell * 29167d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 29177d077197SDavid Brownell * that the underlying controller or its driver does not support. For 29187d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 29197d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 292097d56dc6SJavier Martinez Canillas * 292197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 29227d077197SDavid Brownell */ 29237d077197SDavid Brownell int spi_setup(struct spi_device *spi) 29247d077197SDavid Brownell { 292583596fbeSGeert Uytterhoeven unsigned bad_bits, ugly_bits; 29265ab8d262SAndy Shevchenko int status; 29277d077197SDavid Brownell 2928f477b7fbSwangyuhang /* check mode to prevent that DUAL and QUAD set at the same time 2929f477b7fbSwangyuhang */ 2930f477b7fbSwangyuhang if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || 2931f477b7fbSwangyuhang ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { 2932f477b7fbSwangyuhang dev_err(&spi->dev, 2933f477b7fbSwangyuhang "setup: can not select dual and quad at the same time\n"); 2934f477b7fbSwangyuhang return -EINVAL; 2935f477b7fbSwangyuhang } 2936f477b7fbSwangyuhang /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden 2937f477b7fbSwangyuhang */ 2938f477b7fbSwangyuhang if ((spi->mode & SPI_3WIRE) && (spi->mode & 29396b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 29406b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 2941f477b7fbSwangyuhang return -EINVAL; 2942e7db06b5SDavid Brownell /* help drivers fail *cleanly* when they need options 29438caab75fSGeert Uytterhoeven * that aren't supported with their current controller 2944cbaa62e0SDavid Lechner * SPI_CS_WORD has a fallback software implementation, 2945cbaa62e0SDavid Lechner * so it is ignored here. 2946e7db06b5SDavid Brownell */ 2947cbaa62e0SDavid Lechner bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); 294883596fbeSGeert Uytterhoeven ugly_bits = bad_bits & 29496b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 29506b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); 295183596fbeSGeert Uytterhoeven if (ugly_bits) { 295283596fbeSGeert Uytterhoeven dev_warn(&spi->dev, 295383596fbeSGeert Uytterhoeven "setup: ignoring unsupported mode bits %x\n", 295483596fbeSGeert Uytterhoeven ugly_bits); 295583596fbeSGeert Uytterhoeven spi->mode &= ~ugly_bits; 295683596fbeSGeert Uytterhoeven bad_bits &= ~ugly_bits; 295783596fbeSGeert Uytterhoeven } 2958e7db06b5SDavid Brownell if (bad_bits) { 2959eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 2960e7db06b5SDavid Brownell bad_bits); 2961e7db06b5SDavid Brownell return -EINVAL; 2962e7db06b5SDavid Brownell } 2963e7db06b5SDavid Brownell 29647d077197SDavid Brownell if (!spi->bits_per_word) 29657d077197SDavid Brownell spi->bits_per_word = 8; 29667d077197SDavid Brownell 29678caab75fSGeert Uytterhoeven status = __spi_validate_bits_per_word(spi->controller, 29688caab75fSGeert Uytterhoeven spi->bits_per_word); 29695ab8d262SAndy Shevchenko if (status) 29705ab8d262SAndy Shevchenko return status; 297163ab645fSStefan Brüns 2972052eb2d4SAxel Lin if (!spi->max_speed_hz) 29738caab75fSGeert Uytterhoeven spi->max_speed_hz = spi->controller->max_speed_hz; 2974052eb2d4SAxel Lin 29758caab75fSGeert Uytterhoeven if (spi->controller->setup) 29768caab75fSGeert Uytterhoeven status = spi->controller->setup(spi); 29777d077197SDavid Brownell 2978abeedb01SFranklin S Cooper Jr spi_set_cs(spi, false); 2979abeedb01SFranklin S Cooper Jr 29805fe5f05eSJingoo Han dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 29817d077197SDavid Brownell (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), 29827d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 29837d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 29847d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 29857d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 29867d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 29877d077197SDavid Brownell status); 29887d077197SDavid Brownell 29897d077197SDavid Brownell return status; 29907d077197SDavid Brownell } 29917d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 29927d077197SDavid Brownell 2993f1ca9992SSowjanya Komatineni /** 2994f1ca9992SSowjanya Komatineni * spi_set_cs_timing - configure CS setup, hold, and inactive delays 2995f1ca9992SSowjanya Komatineni * @spi: the device that requires specific CS timing configuration 2996f1ca9992SSowjanya Komatineni * @setup: CS setup time in terms of clock count 2997f1ca9992SSowjanya Komatineni * @hold: CS hold time in terms of clock count 2998f1ca9992SSowjanya Komatineni * @inactive_dly: CS inactive delay between transfers in terms of clock count 2999f1ca9992SSowjanya Komatineni */ 3000f1ca9992SSowjanya Komatineni void spi_set_cs_timing(struct spi_device *spi, u8 setup, u8 hold, 3001f1ca9992SSowjanya Komatineni u8 inactive_dly) 3002f1ca9992SSowjanya Komatineni { 3003f1ca9992SSowjanya Komatineni if (spi->controller->set_cs_timing) 3004f1ca9992SSowjanya Komatineni spi->controller->set_cs_timing(spi, setup, hold, inactive_dly); 3005f1ca9992SSowjanya Komatineni } 3006f1ca9992SSowjanya Komatineni EXPORT_SYMBOL_GPL(spi_set_cs_timing); 3007f1ca9992SSowjanya Komatineni 300890808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message) 3009cf32b71eSErnst Schwab { 30108caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3011e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 30126ea31293SAtsushi Nemoto int w_size; 3013cf32b71eSErnst Schwab 301424a0013aSMark Brown if (list_empty(&message->transfers)) 301524a0013aSMark Brown return -EINVAL; 301624a0013aSMark Brown 3017cbaa62e0SDavid Lechner /* If an SPI controller does not support toggling the CS line on each 301871388b21SDavid Lechner * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO 301971388b21SDavid Lechner * for the CS line, we can emulate the CS-per-word hardware function by 3020cbaa62e0SDavid Lechner * splitting transfers into one-word transfers and ensuring that 3021cbaa62e0SDavid Lechner * cs_change is set for each transfer. 3022cbaa62e0SDavid Lechner */ 302371388b21SDavid Lechner if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || 3024f3186dd8SLinus Walleij spi->cs_gpiod || 302571388b21SDavid Lechner gpio_is_valid(spi->cs_gpio))) { 3026cbaa62e0SDavid Lechner size_t maxsize; 3027cbaa62e0SDavid Lechner int ret; 3028cbaa62e0SDavid Lechner 3029cbaa62e0SDavid Lechner maxsize = (spi->bits_per_word + 7) / 8; 3030cbaa62e0SDavid Lechner 3031cbaa62e0SDavid Lechner /* spi_split_transfers_maxsize() requires message->spi */ 3032cbaa62e0SDavid Lechner message->spi = spi; 3033cbaa62e0SDavid Lechner 3034cbaa62e0SDavid Lechner ret = spi_split_transfers_maxsize(ctlr, message, maxsize, 3035cbaa62e0SDavid Lechner GFP_KERNEL); 3036cbaa62e0SDavid Lechner if (ret) 3037cbaa62e0SDavid Lechner return ret; 3038cbaa62e0SDavid Lechner 3039cbaa62e0SDavid Lechner list_for_each_entry(xfer, &message->transfers, transfer_list) { 3040cbaa62e0SDavid Lechner /* don't change cs_change on the last entry in the list */ 3041cbaa62e0SDavid Lechner if (list_is_last(&xfer->transfer_list, &message->transfers)) 3042cbaa62e0SDavid Lechner break; 3043cbaa62e0SDavid Lechner xfer->cs_change = 1; 3044cbaa62e0SDavid Lechner } 3045cbaa62e0SDavid Lechner } 3046cbaa62e0SDavid Lechner 3047cf32b71eSErnst Schwab /* Half-duplex links include original MicroWire, and ones with 3048cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 3049cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 3050cf32b71eSErnst Schwab * software limitations. 3051cf32b71eSErnst Schwab */ 30528caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 30538caab75fSGeert Uytterhoeven (spi->mode & SPI_3WIRE)) { 30548caab75fSGeert Uytterhoeven unsigned flags = ctlr->flags; 3055cf32b71eSErnst Schwab 3056cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 3057cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 3058cf32b71eSErnst Schwab return -EINVAL; 30598caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 3060cf32b71eSErnst Schwab return -EINVAL; 30618caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 3062cf32b71eSErnst Schwab return -EINVAL; 3063cf32b71eSErnst Schwab } 3064cf32b71eSErnst Schwab } 3065cf32b71eSErnst Schwab 3066e6811d1dSLaxman Dewangan /** 3067059b8ffeSLaxman Dewangan * Set transfer bits_per_word and max speed as spi device default if 3068059b8ffeSLaxman Dewangan * it is not set for this transfer. 3069f477b7fbSwangyuhang * Set transfer tx_nbits and rx_nbits as single transfer default 3070f477b7fbSwangyuhang * (SPI_NBITS_SINGLE) if it is not set for this transfer. 3071b7bb367aSJonas Bonn * Ensure transfer word_delay is at least as long as that required by 3072b7bb367aSJonas Bonn * device itself. 3073e6811d1dSLaxman Dewangan */ 307477e80588SMartin Sperl message->frame_length = 0; 3075e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 3076078726ceSSourav Poddar message->frame_length += xfer->len; 3077e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 3078e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 3079a6f87fadSAxel Lin 3080a6f87fadSAxel Lin if (!xfer->speed_hz) 3081059b8ffeSLaxman Dewangan xfer->speed_hz = spi->max_speed_hz; 3082a6f87fadSAxel Lin 30838caab75fSGeert Uytterhoeven if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 30848caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 308556ede94aSGabor Juhos 30868caab75fSGeert Uytterhoeven if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 3087543bb255SStephen Warren return -EINVAL; 3088a2fd4f9fSMark Brown 30894d94bd21SIvan T. Ivanov /* 30904d94bd21SIvan T. Ivanov * SPI transfer length should be multiple of SPI word size 30914d94bd21SIvan T. Ivanov * where SPI word size should be power-of-two multiple 30924d94bd21SIvan T. Ivanov */ 30934d94bd21SIvan T. Ivanov if (xfer->bits_per_word <= 8) 30944d94bd21SIvan T. Ivanov w_size = 1; 30954d94bd21SIvan T. Ivanov else if (xfer->bits_per_word <= 16) 30964d94bd21SIvan T. Ivanov w_size = 2; 30974d94bd21SIvan T. Ivanov else 30984d94bd21SIvan T. Ivanov w_size = 4; 30994d94bd21SIvan T. Ivanov 31004d94bd21SIvan T. Ivanov /* No partial transfers accepted */ 31016ea31293SAtsushi Nemoto if (xfer->len % w_size) 31024d94bd21SIvan T. Ivanov return -EINVAL; 31034d94bd21SIvan T. Ivanov 31048caab75fSGeert Uytterhoeven if (xfer->speed_hz && ctlr->min_speed_hz && 31058caab75fSGeert Uytterhoeven xfer->speed_hz < ctlr->min_speed_hz) 3106a2fd4f9fSMark Brown return -EINVAL; 3107f477b7fbSwangyuhang 3108f477b7fbSwangyuhang if (xfer->tx_buf && !xfer->tx_nbits) 3109f477b7fbSwangyuhang xfer->tx_nbits = SPI_NBITS_SINGLE; 3110f477b7fbSwangyuhang if (xfer->rx_buf && !xfer->rx_nbits) 3111f477b7fbSwangyuhang xfer->rx_nbits = SPI_NBITS_SINGLE; 3112f477b7fbSwangyuhang /* check transfer tx/rx_nbits: 31131afd9989SGeert Uytterhoeven * 1. check the value matches one of single, dual and quad 31141afd9989SGeert Uytterhoeven * 2. check tx/rx_nbits match the mode in spi_device 3115f477b7fbSwangyuhang */ 3116db90a441SSourav Poddar if (xfer->tx_buf) { 3117f477b7fbSwangyuhang if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3118f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_DUAL && 3119f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_QUAD) 3120a2fd4f9fSMark Brown return -EINVAL; 3121f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 3122f477b7fbSwangyuhang !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3123f477b7fbSwangyuhang return -EINVAL; 3124f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 3125f477b7fbSwangyuhang !(spi->mode & SPI_TX_QUAD)) 3126f477b7fbSwangyuhang return -EINVAL; 3127db90a441SSourav Poddar } 3128f477b7fbSwangyuhang /* check transfer rx_nbits */ 3129db90a441SSourav Poddar if (xfer->rx_buf) { 3130f477b7fbSwangyuhang if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3131f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_DUAL && 3132f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_QUAD) 3133f477b7fbSwangyuhang return -EINVAL; 3134f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 3135f477b7fbSwangyuhang !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 3136f477b7fbSwangyuhang return -EINVAL; 3137f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 3138f477b7fbSwangyuhang !(spi->mode & SPI_RX_QUAD)) 3139f477b7fbSwangyuhang return -EINVAL; 3140e6811d1dSLaxman Dewangan } 3141b7bb367aSJonas Bonn 3142b7bb367aSJonas Bonn if (xfer->word_delay_usecs < spi->word_delay_usecs) 3143b7bb367aSJonas Bonn xfer->word_delay_usecs = spi->word_delay_usecs; 3144e6811d1dSLaxman Dewangan } 3145e6811d1dSLaxman Dewangan 3146cf32b71eSErnst Schwab message->status = -EINPROGRESS; 314790808738SMark Brown 314890808738SMark Brown return 0; 314990808738SMark Brown } 315090808738SMark Brown 315190808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message) 315290808738SMark Brown { 31538caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 315490808738SMark Brown 3155b5932f5cSBoris Brezillon /* 3156b5932f5cSBoris Brezillon * Some controllers do not support doing regular SPI transfers. Return 3157b5932f5cSBoris Brezillon * ENOTSUPP when this is the case. 3158b5932f5cSBoris Brezillon */ 3159b5932f5cSBoris Brezillon if (!ctlr->transfer) 3160b5932f5cSBoris Brezillon return -ENOTSUPP; 3161b5932f5cSBoris Brezillon 316290808738SMark Brown message->spi = spi; 316390808738SMark Brown 31648caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); 3165eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); 3166eca2ebc7SMartin Sperl 316790808738SMark Brown trace_spi_message_submit(message); 316890808738SMark Brown 31698caab75fSGeert Uytterhoeven return ctlr->transfer(spi, message); 3170cf32b71eSErnst Schwab } 3171cf32b71eSErnst Schwab 3172568d0697SDavid Brownell /** 3173568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 3174568d0697SDavid Brownell * @spi: device with which data will be exchanged 3175568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 3176568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 3177568d0697SDavid Brownell * 3178568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 3179568d0697SDavid Brownell * as well as from task contexts which can sleep. 3180568d0697SDavid Brownell * 3181568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 3182568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 3183568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 3184568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 3185568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 3186568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 3187568d0697SDavid Brownell * core or controller driver code. 3188568d0697SDavid Brownell * 3189568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 3190568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 3191568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 3192568d0697SDavid Brownell * time requirements, for example. 3193568d0697SDavid Brownell * 3194568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 3195568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 3196568d0697SDavid Brownell * Until returning from the associated message completion callback, 3197568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 3198568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 3199568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 320097d56dc6SJavier Martinez Canillas * 320197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3202568d0697SDavid Brownell */ 3203568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 3204568d0697SDavid Brownell { 32058caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3206cf32b71eSErnst Schwab int ret; 3207cf32b71eSErnst Schwab unsigned long flags; 3208568d0697SDavid Brownell 320990808738SMark Brown ret = __spi_validate(spi, message); 321090808738SMark Brown if (ret != 0) 321190808738SMark Brown return ret; 321290808738SMark Brown 32138caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3214568d0697SDavid Brownell 32158caab75fSGeert Uytterhoeven if (ctlr->bus_lock_flag) 3216cf32b71eSErnst Schwab ret = -EBUSY; 3217cf32b71eSErnst Schwab else 3218cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3219568d0697SDavid Brownell 32208caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3221cf32b71eSErnst Schwab 3222cf32b71eSErnst Schwab return ret; 3223568d0697SDavid Brownell } 3224568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 3225568d0697SDavid Brownell 3226cf32b71eSErnst Schwab /** 3227cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 3228cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3229cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 3230cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 3231cf32b71eSErnst Schwab * 3232cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 3233cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 3234cf32b71eSErnst Schwab * 3235cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 3236cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 3237cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 3238cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 3239cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 3240cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 3241cf32b71eSErnst Schwab * core or controller driver code. 3242cf32b71eSErnst Schwab * 3243cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 3244cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 3245cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 3246cf32b71eSErnst Schwab * time requirements, for example. 3247cf32b71eSErnst Schwab * 3248cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 3249cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 3250cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 3251cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 3252cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 3253cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 325497d56dc6SJavier Martinez Canillas * 325597d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3256cf32b71eSErnst Schwab */ 3257cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message) 3258cf32b71eSErnst Schwab { 32598caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3260cf32b71eSErnst Schwab int ret; 3261cf32b71eSErnst Schwab unsigned long flags; 3262cf32b71eSErnst Schwab 326390808738SMark Brown ret = __spi_validate(spi, message); 326490808738SMark Brown if (ret != 0) 326590808738SMark Brown return ret; 326690808738SMark Brown 32678caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3268cf32b71eSErnst Schwab 3269cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3270cf32b71eSErnst Schwab 32718caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3272cf32b71eSErnst Schwab 3273cf32b71eSErnst Schwab return ret; 3274cf32b71eSErnst Schwab 3275cf32b71eSErnst Schwab } 3276cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked); 3277cf32b71eSErnst Schwab 32787d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 32797d077197SDavid Brownell 32808caab75fSGeert Uytterhoeven /* Utility methods for SPI protocol drivers, layered on 32817d077197SDavid Brownell * top of the core. Some other utility methods are defined as 32827d077197SDavid Brownell * inline functions. 32837d077197SDavid Brownell */ 32847d077197SDavid Brownell 32855d870c8eSAndrew Morton static void spi_complete(void *arg) 32865d870c8eSAndrew Morton { 32875d870c8eSAndrew Morton complete(arg); 32885d870c8eSAndrew Morton } 32895d870c8eSAndrew Morton 3290ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message) 3291cf32b71eSErnst Schwab { 3292cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 3293cf32b71eSErnst Schwab int status; 32948caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 32950461a414SMark Brown unsigned long flags; 32960461a414SMark Brown 32970461a414SMark Brown status = __spi_validate(spi, message); 32980461a414SMark Brown if (status != 0) 32990461a414SMark Brown return status; 3300cf32b71eSErnst Schwab 3301cf32b71eSErnst Schwab message->complete = spi_complete; 3302cf32b71eSErnst Schwab message->context = &done; 33030461a414SMark Brown message->spi = spi; 3304cf32b71eSErnst Schwab 33058caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); 3306eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); 3307eca2ebc7SMartin Sperl 33080461a414SMark Brown /* If we're not using the legacy transfer method then we will 33090461a414SMark Brown * try to transfer in the calling context so special case. 33100461a414SMark Brown * This code would be less tricky if we could remove the 33110461a414SMark Brown * support for driver implemented message queues. 33120461a414SMark Brown */ 33138caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 33148caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 33150461a414SMark Brown 33160461a414SMark Brown trace_spi_message_submit(message); 33170461a414SMark Brown 33180461a414SMark Brown status = __spi_queued_transfer(spi, message, false); 33190461a414SMark Brown 33208caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 33210461a414SMark Brown } else { 3322cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 33230461a414SMark Brown } 3324cf32b71eSErnst Schwab 3325cf32b71eSErnst Schwab if (status == 0) { 33260461a414SMark Brown /* Push out the messages in the calling context if we 33270461a414SMark Brown * can. 33280461a414SMark Brown */ 33298caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 33308caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3331eca2ebc7SMartin Sperl spi_sync_immediate); 3332eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, 3333eca2ebc7SMartin Sperl spi_sync_immediate); 33348caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, false); 3335eca2ebc7SMartin Sperl } 33360461a414SMark Brown 3337cf32b71eSErnst Schwab wait_for_completion(&done); 3338cf32b71eSErnst Schwab status = message->status; 3339cf32b71eSErnst Schwab } 3340cf32b71eSErnst Schwab message->context = NULL; 3341cf32b71eSErnst Schwab return status; 3342cf32b71eSErnst Schwab } 3343cf32b71eSErnst Schwab 33448ae12a0dSDavid Brownell /** 33458ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 33468ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 33478ae12a0dSDavid Brownell * @message: describes the data transfers 334833e34dc6SDavid Brownell * Context: can sleep 33498ae12a0dSDavid Brownell * 33508ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 33518ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 33528ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 33538ae12a0dSDavid Brownell * 33548ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 33558ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 33568ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 33578ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 33588ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 33598ae12a0dSDavid Brownell * 33600c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 33610c868461SDavid Brownell * message will not be freed before this call returns. 33620c868461SDavid Brownell * 336397d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 33648ae12a0dSDavid Brownell */ 33658ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 33668ae12a0dSDavid Brownell { 3367ef4d96ecSMark Brown int ret; 3368ef4d96ecSMark Brown 33698caab75fSGeert Uytterhoeven mutex_lock(&spi->controller->bus_lock_mutex); 3370ef4d96ecSMark Brown ret = __spi_sync(spi, message); 33718caab75fSGeert Uytterhoeven mutex_unlock(&spi->controller->bus_lock_mutex); 3372ef4d96ecSMark Brown 3373ef4d96ecSMark Brown return ret; 33748ae12a0dSDavid Brownell } 33758ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 33768ae12a0dSDavid Brownell 3377cf32b71eSErnst Schwab /** 3378cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 3379cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3380cf32b71eSErnst Schwab * @message: describes the data transfers 3381cf32b71eSErnst Schwab * Context: can sleep 3382cf32b71eSErnst Schwab * 3383cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3384cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 3385cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 3386cf32b71eSErnst Schwab * 3387cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 338825985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 3389cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 3390cf32b71eSErnst Schwab * 339197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3392cf32b71eSErnst Schwab */ 3393cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 3394cf32b71eSErnst Schwab { 3395ef4d96ecSMark Brown return __spi_sync(spi, message); 3396cf32b71eSErnst Schwab } 3397cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 3398cf32b71eSErnst Schwab 3399cf32b71eSErnst Schwab /** 3400cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 34018caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that should be locked for exclusive bus access 3402cf32b71eSErnst Schwab * Context: can sleep 3403cf32b71eSErnst Schwab * 3404cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3405cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3406cf32b71eSErnst Schwab * 3407cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 3408cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 3409cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 3410cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 3411cf32b71eSErnst Schwab * 341297d56dc6SJavier Martinez Canillas * Return: always zero. 3413cf32b71eSErnst Schwab */ 34148caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr) 3415cf32b71eSErnst Schwab { 3416cf32b71eSErnst Schwab unsigned long flags; 3417cf32b71eSErnst Schwab 34188caab75fSGeert Uytterhoeven mutex_lock(&ctlr->bus_lock_mutex); 3419cf32b71eSErnst Schwab 34208caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 34218caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 1; 34228caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3423cf32b71eSErnst Schwab 3424cf32b71eSErnst Schwab /* mutex remains locked until spi_bus_unlock is called */ 3425cf32b71eSErnst Schwab 3426cf32b71eSErnst Schwab return 0; 3427cf32b71eSErnst Schwab } 3428cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 3429cf32b71eSErnst Schwab 3430cf32b71eSErnst Schwab /** 3431cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 34328caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that was locked for exclusive bus access 3433cf32b71eSErnst Schwab * Context: can sleep 3434cf32b71eSErnst Schwab * 3435cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3436cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3437cf32b71eSErnst Schwab * 3438cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 3439cf32b71eSErnst Schwab * call. 3440cf32b71eSErnst Schwab * 344197d56dc6SJavier Martinez Canillas * Return: always zero. 3442cf32b71eSErnst Schwab */ 34438caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr) 3444cf32b71eSErnst Schwab { 34458caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 3446cf32b71eSErnst Schwab 34478caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->bus_lock_mutex); 3448cf32b71eSErnst Schwab 3449cf32b71eSErnst Schwab return 0; 3450cf32b71eSErnst Schwab } 3451cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 3452cf32b71eSErnst Schwab 3453a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 3454a9948b61SDavid Brownell #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 34558ae12a0dSDavid Brownell 34568ae12a0dSDavid Brownell static u8 *buf; 34578ae12a0dSDavid Brownell 34588ae12a0dSDavid Brownell /** 34598ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 34608ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 34618ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 34628ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 346327570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 346427570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 346533e34dc6SDavid Brownell * Context: can sleep 34668ae12a0dSDavid Brownell * 34678ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 34688ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 34698ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 3470b885244eSDavid Brownell * This call may only be used from a context that may sleep. 34718ae12a0dSDavid Brownell * 34720c868461SDavid Brownell * Parameters to this routine are always copied using a small buffer; 347333e34dc6SDavid Brownell * portable code should never use this for more than 32 bytes. 347433e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 34750c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 347697d56dc6SJavier Martinez Canillas * 347797d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 34788ae12a0dSDavid Brownell */ 34798ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 34800c4a1590SMark Brown const void *txbuf, unsigned n_tx, 34810c4a1590SMark Brown void *rxbuf, unsigned n_rx) 34828ae12a0dSDavid Brownell { 3483068f4070SDavid Brownell static DEFINE_MUTEX(lock); 34848ae12a0dSDavid Brownell 34858ae12a0dSDavid Brownell int status; 34868ae12a0dSDavid Brownell struct spi_message message; 3487bdff549eSDavid Brownell struct spi_transfer x[2]; 34888ae12a0dSDavid Brownell u8 *local_buf; 34898ae12a0dSDavid Brownell 3490b3a223eeSMark Brown /* Use preallocated DMA-safe buffer if we can. We can't avoid 3491b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 3492b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 3493b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 34948ae12a0dSDavid Brownell */ 3495b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 34962cd94c8aSMark Brown local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 34972cd94c8aSMark Brown GFP_KERNEL | GFP_DMA); 3498b3a223eeSMark Brown if (!local_buf) 3499b3a223eeSMark Brown return -ENOMEM; 3500b3a223eeSMark Brown } else { 3501b3a223eeSMark Brown local_buf = buf; 3502b3a223eeSMark Brown } 35038ae12a0dSDavid Brownell 35048275c642SVitaly Wool spi_message_init(&message); 35055fe5f05eSJingoo Han memset(x, 0, sizeof(x)); 3506bdff549eSDavid Brownell if (n_tx) { 3507bdff549eSDavid Brownell x[0].len = n_tx; 3508bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 3509bdff549eSDavid Brownell } 3510bdff549eSDavid Brownell if (n_rx) { 3511bdff549eSDavid Brownell x[1].len = n_rx; 3512bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 3513bdff549eSDavid Brownell } 35148275c642SVitaly Wool 35158ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 3516bdff549eSDavid Brownell x[0].tx_buf = local_buf; 3517bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 35188ae12a0dSDavid Brownell 35198ae12a0dSDavid Brownell /* do the i/o */ 35208ae12a0dSDavid Brownell status = spi_sync(spi, &message); 35219b938b74SMarc Pignat if (status == 0) 3522bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 35238ae12a0dSDavid Brownell 3524bdff549eSDavid Brownell if (x[0].tx_buf == buf) 3525068f4070SDavid Brownell mutex_unlock(&lock); 35268ae12a0dSDavid Brownell else 35278ae12a0dSDavid Brownell kfree(local_buf); 35288ae12a0dSDavid Brownell 35298ae12a0dSDavid Brownell return status; 35308ae12a0dSDavid Brownell } 35318ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 35328ae12a0dSDavid Brownell 35338ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 35348ae12a0dSDavid Brownell 35355f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF) 3536ce79d54aSPantelis Antoniou static int __spi_of_device_match(struct device *dev, void *data) 3537ce79d54aSPantelis Antoniou { 3538ce79d54aSPantelis Antoniou return dev->of_node == data; 3539ce79d54aSPantelis Antoniou } 3540ce79d54aSPantelis Antoniou 3541ce79d54aSPantelis Antoniou /* must call put_device() when done with returned spi_device device */ 35425f143af7SMarco Felsch struct spi_device *of_find_spi_device_by_node(struct device_node *node) 3543ce79d54aSPantelis Antoniou { 3544ce79d54aSPantelis Antoniou struct device *dev = bus_find_device(&spi_bus_type, NULL, node, 3545ce79d54aSPantelis Antoniou __spi_of_device_match); 3546ce79d54aSPantelis Antoniou return dev ? to_spi_device(dev) : NULL; 3547ce79d54aSPantelis Antoniou } 35485f143af7SMarco Felsch EXPORT_SYMBOL_GPL(of_find_spi_device_by_node); 35495f143af7SMarco Felsch #endif /* IS_ENABLED(CONFIG_OF) */ 3550ce79d54aSPantelis Antoniou 35515f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF_DYNAMIC) 35528caab75fSGeert Uytterhoeven static int __spi_of_controller_match(struct device *dev, const void *data) 3553ce79d54aSPantelis Antoniou { 3554ce79d54aSPantelis Antoniou return dev->of_node == data; 3555ce79d54aSPantelis Antoniou } 3556ce79d54aSPantelis Antoniou 35578caab75fSGeert Uytterhoeven /* the spi controllers are not using spi_bus, so we find it with another way */ 35588caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 3559ce79d54aSPantelis Antoniou { 3560ce79d54aSPantelis Antoniou struct device *dev; 3561ce79d54aSPantelis Antoniou 3562ce79d54aSPantelis Antoniou dev = class_find_device(&spi_master_class, NULL, node, 35638caab75fSGeert Uytterhoeven __spi_of_controller_match); 35646c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 35656c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, node, 35668caab75fSGeert Uytterhoeven __spi_of_controller_match); 3567ce79d54aSPantelis Antoniou if (!dev) 3568ce79d54aSPantelis Antoniou return NULL; 3569ce79d54aSPantelis Antoniou 3570ce79d54aSPantelis Antoniou /* reference got in class_find_device */ 35718caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 3572ce79d54aSPantelis Antoniou } 3573ce79d54aSPantelis Antoniou 3574ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action, 3575ce79d54aSPantelis Antoniou void *arg) 3576ce79d54aSPantelis Antoniou { 3577ce79d54aSPantelis Antoniou struct of_reconfig_data *rd = arg; 35788caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 3579ce79d54aSPantelis Antoniou struct spi_device *spi; 3580ce79d54aSPantelis Antoniou 3581ce79d54aSPantelis Antoniou switch (of_reconfig_get_state_change(action, arg)) { 3582ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 35838caab75fSGeert Uytterhoeven ctlr = of_find_spi_controller_by_node(rd->dn->parent); 35848caab75fSGeert Uytterhoeven if (ctlr == NULL) 3585ce79d54aSPantelis Antoniou return NOTIFY_OK; /* not for us */ 3586ce79d54aSPantelis Antoniou 3587bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 35888caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 3589bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 3590bd6c1644SGeert Uytterhoeven } 3591bd6c1644SGeert Uytterhoeven 35928caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, rd->dn); 35938caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 3594ce79d54aSPantelis Antoniou 3595ce79d54aSPantelis Antoniou if (IS_ERR(spi)) { 359625c56c88SRob Herring pr_err("%s: failed to create for '%pOF'\n", 359725c56c88SRob Herring __func__, rd->dn); 3598e0af98a7SRalf Ramsauer of_node_clear_flag(rd->dn, OF_POPULATED); 3599ce79d54aSPantelis Antoniou return notifier_from_errno(PTR_ERR(spi)); 3600ce79d54aSPantelis Antoniou } 3601ce79d54aSPantelis Antoniou break; 3602ce79d54aSPantelis Antoniou 3603ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 3604bd6c1644SGeert Uytterhoeven /* already depopulated? */ 3605bd6c1644SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 3606bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 3607bd6c1644SGeert Uytterhoeven 3608ce79d54aSPantelis Antoniou /* find our device by node */ 3609ce79d54aSPantelis Antoniou spi = of_find_spi_device_by_node(rd->dn); 3610ce79d54aSPantelis Antoniou if (spi == NULL) 3611ce79d54aSPantelis Antoniou return NOTIFY_OK; /* no? not meant for us */ 3612ce79d54aSPantelis Antoniou 3613ce79d54aSPantelis Antoniou /* unregister takes one ref away */ 3614ce79d54aSPantelis Antoniou spi_unregister_device(spi); 3615ce79d54aSPantelis Antoniou 3616ce79d54aSPantelis Antoniou /* and put the reference of the find */ 3617ce79d54aSPantelis Antoniou put_device(&spi->dev); 3618ce79d54aSPantelis Antoniou break; 3619ce79d54aSPantelis Antoniou } 3620ce79d54aSPantelis Antoniou 3621ce79d54aSPantelis Antoniou return NOTIFY_OK; 3622ce79d54aSPantelis Antoniou } 3623ce79d54aSPantelis Antoniou 3624ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = { 3625ce79d54aSPantelis Antoniou .notifier_call = of_spi_notify, 3626ce79d54aSPantelis Antoniou }; 3627ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 3628ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier; 3629ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 3630ce79d54aSPantelis Antoniou 36317f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI) 36328caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data) 36337f24467fSOctavian Purdila { 36347f24467fSOctavian Purdila return ACPI_COMPANION(dev->parent) == data; 36357f24467fSOctavian Purdila } 36367f24467fSOctavian Purdila 36377f24467fSOctavian Purdila static int spi_acpi_device_match(struct device *dev, void *data) 36387f24467fSOctavian Purdila { 36397f24467fSOctavian Purdila return ACPI_COMPANION(dev) == data; 36407f24467fSOctavian Purdila } 36417f24467fSOctavian Purdila 36428caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 36437f24467fSOctavian Purdila { 36447f24467fSOctavian Purdila struct device *dev; 36457f24467fSOctavian Purdila 36467f24467fSOctavian Purdila dev = class_find_device(&spi_master_class, NULL, adev, 36478caab75fSGeert Uytterhoeven spi_acpi_controller_match); 36486c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 36496c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, adev, 36508caab75fSGeert Uytterhoeven spi_acpi_controller_match); 36517f24467fSOctavian Purdila if (!dev) 36527f24467fSOctavian Purdila return NULL; 36537f24467fSOctavian Purdila 36548caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 36557f24467fSOctavian Purdila } 36567f24467fSOctavian Purdila 36577f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 36587f24467fSOctavian Purdila { 36597f24467fSOctavian Purdila struct device *dev; 36607f24467fSOctavian Purdila 36617f24467fSOctavian Purdila dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match); 36627f24467fSOctavian Purdila 36637f24467fSOctavian Purdila return dev ? to_spi_device(dev) : NULL; 36647f24467fSOctavian Purdila } 36657f24467fSOctavian Purdila 36667f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 36677f24467fSOctavian Purdila void *arg) 36687f24467fSOctavian Purdila { 36697f24467fSOctavian Purdila struct acpi_device *adev = arg; 36708caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 36717f24467fSOctavian Purdila struct spi_device *spi; 36727f24467fSOctavian Purdila 36737f24467fSOctavian Purdila switch (value) { 36747f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_ADD: 36758caab75fSGeert Uytterhoeven ctlr = acpi_spi_find_controller_by_adev(adev->parent); 36768caab75fSGeert Uytterhoeven if (!ctlr) 36777f24467fSOctavian Purdila break; 36787f24467fSOctavian Purdila 36798caab75fSGeert Uytterhoeven acpi_register_spi_device(ctlr, adev); 36808caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 36817f24467fSOctavian Purdila break; 36827f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_REMOVE: 36837f24467fSOctavian Purdila if (!acpi_device_enumerated(adev)) 36847f24467fSOctavian Purdila break; 36857f24467fSOctavian Purdila 36867f24467fSOctavian Purdila spi = acpi_spi_find_device_by_adev(adev); 36877f24467fSOctavian Purdila if (!spi) 36887f24467fSOctavian Purdila break; 36897f24467fSOctavian Purdila 36907f24467fSOctavian Purdila spi_unregister_device(spi); 36917f24467fSOctavian Purdila put_device(&spi->dev); 36927f24467fSOctavian Purdila break; 36937f24467fSOctavian Purdila } 36947f24467fSOctavian Purdila 36957f24467fSOctavian Purdila return NOTIFY_OK; 36967f24467fSOctavian Purdila } 36977f24467fSOctavian Purdila 36987f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = { 36997f24467fSOctavian Purdila .notifier_call = acpi_spi_notify, 37007f24467fSOctavian Purdila }; 37017f24467fSOctavian Purdila #else 37027f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier; 37037f24467fSOctavian Purdila #endif 37047f24467fSOctavian Purdila 37058ae12a0dSDavid Brownell static int __init spi_init(void) 37068ae12a0dSDavid Brownell { 3707b885244eSDavid Brownell int status; 37088ae12a0dSDavid Brownell 3709e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 3710b885244eSDavid Brownell if (!buf) { 3711b885244eSDavid Brownell status = -ENOMEM; 3712b885244eSDavid Brownell goto err0; 37138ae12a0dSDavid Brownell } 3714b885244eSDavid Brownell 3715b885244eSDavid Brownell status = bus_register(&spi_bus_type); 3716b885244eSDavid Brownell if (status < 0) 3717b885244eSDavid Brownell goto err1; 3718b885244eSDavid Brownell 3719b885244eSDavid Brownell status = class_register(&spi_master_class); 3720b885244eSDavid Brownell if (status < 0) 3721b885244eSDavid Brownell goto err2; 3722ce79d54aSPantelis Antoniou 37236c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 37246c364062SGeert Uytterhoeven status = class_register(&spi_slave_class); 37256c364062SGeert Uytterhoeven if (status < 0) 37266c364062SGeert Uytterhoeven goto err3; 37276c364062SGeert Uytterhoeven } 37286c364062SGeert Uytterhoeven 37295267720eSFabio Estevam if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 3730ce79d54aSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 37317f24467fSOctavian Purdila if (IS_ENABLED(CONFIG_ACPI)) 37327f24467fSOctavian Purdila WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 3733ce79d54aSPantelis Antoniou 3734b885244eSDavid Brownell return 0; 3735b885244eSDavid Brownell 37366c364062SGeert Uytterhoeven err3: 37376c364062SGeert Uytterhoeven class_unregister(&spi_master_class); 3738b885244eSDavid Brownell err2: 3739b885244eSDavid Brownell bus_unregister(&spi_bus_type); 3740b885244eSDavid Brownell err1: 3741b885244eSDavid Brownell kfree(buf); 3742b885244eSDavid Brownell buf = NULL; 3743b885244eSDavid Brownell err0: 3744b885244eSDavid Brownell return status; 3745b885244eSDavid Brownell } 3746b885244eSDavid Brownell 37478ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(), 37488ae12a0dSDavid Brownell * but even essential drivers wait till later 3749b885244eSDavid Brownell * 3750b885244eSDavid Brownell * REVISIT only boardinfo really needs static linking. the rest (device and 3751b885244eSDavid Brownell * driver registration) _could_ be dynamically linked (modular) ... costs 3752b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 37538ae12a0dSDavid Brownell */ 3754673c0c00SDavid Brownell postcore_initcall(spi_init); 3755f0125f1aSMark Brown 3756