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> 399b61e302SSuniel Mahesh 4046336966SBoris Brezillon #include "internals.h" 4146336966SBoris Brezillon 429b61e302SSuniel Mahesh static DEFINE_IDR(spi_master_idr); 4356ec1978SMark Brown 448ae12a0dSDavid Brownell static void spidev_release(struct device *dev) 458ae12a0dSDavid Brownell { 460ffa0285SHans-Peter Nilsson struct spi_device *spi = to_spi_device(dev); 478ae12a0dSDavid Brownell 488caab75fSGeert Uytterhoeven /* spi controllers may cleanup for released devices */ 498caab75fSGeert Uytterhoeven if (spi->controller->cleanup) 508caab75fSGeert Uytterhoeven spi->controller->cleanup(spi); 518ae12a0dSDavid Brownell 528caab75fSGeert Uytterhoeven spi_controller_put(spi->controller); 535039563eSTrent Piepho kfree(spi->driver_override); 5407a389feSRoman Tereshonkov kfree(spi); 558ae12a0dSDavid Brownell } 568ae12a0dSDavid Brownell 578ae12a0dSDavid Brownell static ssize_t 588ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf) 598ae12a0dSDavid Brownell { 608ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 618c4ff6d0SZhang Rui int len; 628c4ff6d0SZhang Rui 638c4ff6d0SZhang Rui len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 648c4ff6d0SZhang Rui if (len != -ENODEV) 658c4ff6d0SZhang Rui return len; 668ae12a0dSDavid Brownell 67d8e328b3SGrant Likely return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 688ae12a0dSDavid Brownell } 69aa7da564SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias); 708ae12a0dSDavid Brownell 715039563eSTrent Piepho static ssize_t driver_override_store(struct device *dev, 725039563eSTrent Piepho struct device_attribute *a, 735039563eSTrent Piepho const char *buf, size_t count) 745039563eSTrent Piepho { 755039563eSTrent Piepho struct spi_device *spi = to_spi_device(dev); 765039563eSTrent Piepho const char *end = memchr(buf, '\n', count); 775039563eSTrent Piepho const size_t len = end ? end - buf : count; 785039563eSTrent Piepho const char *driver_override, *old; 795039563eSTrent Piepho 805039563eSTrent Piepho /* We need to keep extra room for a newline when displaying value */ 815039563eSTrent Piepho if (len >= (PAGE_SIZE - 1)) 825039563eSTrent Piepho return -EINVAL; 835039563eSTrent Piepho 845039563eSTrent Piepho driver_override = kstrndup(buf, len, GFP_KERNEL); 855039563eSTrent Piepho if (!driver_override) 865039563eSTrent Piepho return -ENOMEM; 875039563eSTrent Piepho 885039563eSTrent Piepho device_lock(dev); 895039563eSTrent Piepho old = spi->driver_override; 905039563eSTrent Piepho if (len) { 915039563eSTrent Piepho spi->driver_override = driver_override; 925039563eSTrent Piepho } else { 935039563eSTrent Piepho /* Emptry string, disable driver override */ 945039563eSTrent Piepho spi->driver_override = NULL; 955039563eSTrent Piepho kfree(driver_override); 965039563eSTrent Piepho } 975039563eSTrent Piepho device_unlock(dev); 985039563eSTrent Piepho kfree(old); 995039563eSTrent Piepho 1005039563eSTrent Piepho return count; 1015039563eSTrent Piepho } 1025039563eSTrent Piepho 1035039563eSTrent Piepho static ssize_t driver_override_show(struct device *dev, 1045039563eSTrent Piepho struct device_attribute *a, char *buf) 1055039563eSTrent Piepho { 1065039563eSTrent Piepho const struct spi_device *spi = to_spi_device(dev); 1075039563eSTrent Piepho ssize_t len; 1085039563eSTrent Piepho 1095039563eSTrent Piepho device_lock(dev); 1105039563eSTrent Piepho len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : ""); 1115039563eSTrent Piepho device_unlock(dev); 1125039563eSTrent Piepho return len; 1135039563eSTrent Piepho } 1145039563eSTrent Piepho static DEVICE_ATTR_RW(driver_override); 1155039563eSTrent Piepho 116eca2ebc7SMartin Sperl #define SPI_STATISTICS_ATTRS(field, file) \ 1178caab75fSGeert Uytterhoeven static ssize_t spi_controller_##field##_show(struct device *dev, \ 118eca2ebc7SMartin Sperl struct device_attribute *attr, \ 119eca2ebc7SMartin Sperl char *buf) \ 120eca2ebc7SMartin Sperl { \ 1218caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, \ 1228caab75fSGeert Uytterhoeven struct spi_controller, dev); \ 1238caab75fSGeert Uytterhoeven return spi_statistics_##field##_show(&ctlr->statistics, buf); \ 124eca2ebc7SMartin Sperl } \ 1258caab75fSGeert Uytterhoeven static struct device_attribute dev_attr_spi_controller_##field = { \ 126ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 1278caab75fSGeert Uytterhoeven .show = spi_controller_##field##_show, \ 128eca2ebc7SMartin Sperl }; \ 129eca2ebc7SMartin Sperl static ssize_t spi_device_##field##_show(struct device *dev, \ 130eca2ebc7SMartin Sperl struct device_attribute *attr, \ 131eca2ebc7SMartin Sperl char *buf) \ 132eca2ebc7SMartin Sperl { \ 133d1eba93bSGeliang Tang struct spi_device *spi = to_spi_device(dev); \ 134eca2ebc7SMartin Sperl return spi_statistics_##field##_show(&spi->statistics, buf); \ 135eca2ebc7SMartin Sperl } \ 136eca2ebc7SMartin Sperl static struct device_attribute dev_attr_spi_device_##field = { \ 137ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 138eca2ebc7SMartin Sperl .show = spi_device_##field##_show, \ 139eca2ebc7SMartin Sperl } 140eca2ebc7SMartin Sperl 141eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \ 142eca2ebc7SMartin Sperl static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ 143eca2ebc7SMartin Sperl char *buf) \ 144eca2ebc7SMartin Sperl { \ 145eca2ebc7SMartin Sperl unsigned long flags; \ 146eca2ebc7SMartin Sperl ssize_t len; \ 147eca2ebc7SMartin Sperl spin_lock_irqsave(&stat->lock, flags); \ 148eca2ebc7SMartin Sperl len = sprintf(buf, format_string, stat->field); \ 149eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stat->lock, flags); \ 150eca2ebc7SMartin Sperl return len; \ 151eca2ebc7SMartin Sperl } \ 152eca2ebc7SMartin Sperl SPI_STATISTICS_ATTRS(name, file) 153eca2ebc7SMartin Sperl 154eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW(field, format_string) \ 155eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ 156eca2ebc7SMartin Sperl field, format_string) 157eca2ebc7SMartin Sperl 158eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(messages, "%lu"); 159eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(transfers, "%lu"); 160eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(errors, "%lu"); 161eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(timedout, "%lu"); 162eca2ebc7SMartin Sperl 163eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync, "%lu"); 164eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu"); 165eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_async, "%lu"); 166eca2ebc7SMartin Sperl 167eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes, "%llu"); 168eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_rx, "%llu"); 169eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_tx, "%llu"); 170eca2ebc7SMartin Sperl 1716b7bc061SMartin Sperl #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ 1726b7bc061SMartin Sperl SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ 1736b7bc061SMartin Sperl "transfer_bytes_histo_" number, \ 1746b7bc061SMartin Sperl transfer_bytes_histo[index], "%lu") 1756b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); 1766b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); 1776b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); 1786b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); 1796b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); 1806b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); 1816b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); 1826b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); 1836b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); 1846b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); 1856b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); 1866b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); 1876b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); 1886b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); 1896b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); 1906b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); 1916b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); 1926b7bc061SMartin Sperl 193d9f12122SMartin Sperl SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu"); 194d9f12122SMartin Sperl 195aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = { 196aa7da564SGreg Kroah-Hartman &dev_attr_modalias.attr, 1975039563eSTrent Piepho &dev_attr_driver_override.attr, 198aa7da564SGreg Kroah-Hartman NULL, 1998ae12a0dSDavid Brownell }; 200eca2ebc7SMartin Sperl 201eca2ebc7SMartin Sperl static const struct attribute_group spi_dev_group = { 202eca2ebc7SMartin Sperl .attrs = spi_dev_attrs, 203eca2ebc7SMartin Sperl }; 204eca2ebc7SMartin Sperl 205eca2ebc7SMartin Sperl static struct attribute *spi_device_statistics_attrs[] = { 206eca2ebc7SMartin Sperl &dev_attr_spi_device_messages.attr, 207eca2ebc7SMartin Sperl &dev_attr_spi_device_transfers.attr, 208eca2ebc7SMartin Sperl &dev_attr_spi_device_errors.attr, 209eca2ebc7SMartin Sperl &dev_attr_spi_device_timedout.attr, 210eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync.attr, 211eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync_immediate.attr, 212eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_async.attr, 213eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes.attr, 214eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_rx.attr, 215eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_tx.attr, 2166b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo0.attr, 2176b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo1.attr, 2186b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo2.attr, 2196b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo3.attr, 2206b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo4.attr, 2216b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo5.attr, 2226b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo6.attr, 2236b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo7.attr, 2246b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo8.attr, 2256b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo9.attr, 2266b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo10.attr, 2276b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo11.attr, 2286b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo12.attr, 2296b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo13.attr, 2306b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo14.attr, 2316b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo15.attr, 2326b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo16.attr, 233d9f12122SMartin Sperl &dev_attr_spi_device_transfers_split_maxsize.attr, 234eca2ebc7SMartin Sperl NULL, 235eca2ebc7SMartin Sperl }; 236eca2ebc7SMartin Sperl 237eca2ebc7SMartin Sperl static const struct attribute_group spi_device_statistics_group = { 238eca2ebc7SMartin Sperl .name = "statistics", 239eca2ebc7SMartin Sperl .attrs = spi_device_statistics_attrs, 240eca2ebc7SMartin Sperl }; 241eca2ebc7SMartin Sperl 242eca2ebc7SMartin Sperl static const struct attribute_group *spi_dev_groups[] = { 243eca2ebc7SMartin Sperl &spi_dev_group, 244eca2ebc7SMartin Sperl &spi_device_statistics_group, 245eca2ebc7SMartin Sperl NULL, 246eca2ebc7SMartin Sperl }; 247eca2ebc7SMartin Sperl 2488caab75fSGeert Uytterhoeven static struct attribute *spi_controller_statistics_attrs[] = { 2498caab75fSGeert Uytterhoeven &dev_attr_spi_controller_messages.attr, 2508caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers.attr, 2518caab75fSGeert Uytterhoeven &dev_attr_spi_controller_errors.attr, 2528caab75fSGeert Uytterhoeven &dev_attr_spi_controller_timedout.attr, 2538caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync.attr, 2548caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync_immediate.attr, 2558caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_async.attr, 2568caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes.attr, 2578caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_rx.attr, 2588caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_tx.attr, 2598caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo0.attr, 2608caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo1.attr, 2618caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo2.attr, 2628caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo3.attr, 2638caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo4.attr, 2648caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo5.attr, 2658caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo6.attr, 2668caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo7.attr, 2678caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo8.attr, 2688caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo9.attr, 2698caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo10.attr, 2708caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo11.attr, 2718caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo12.attr, 2728caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo13.attr, 2738caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo14.attr, 2748caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo15.attr, 2758caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo16.attr, 2768caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers_split_maxsize.attr, 277eca2ebc7SMartin Sperl NULL, 278eca2ebc7SMartin Sperl }; 279eca2ebc7SMartin Sperl 2808caab75fSGeert Uytterhoeven static const struct attribute_group spi_controller_statistics_group = { 281eca2ebc7SMartin Sperl .name = "statistics", 2828caab75fSGeert Uytterhoeven .attrs = spi_controller_statistics_attrs, 283eca2ebc7SMartin Sperl }; 284eca2ebc7SMartin Sperl 285eca2ebc7SMartin Sperl static const struct attribute_group *spi_master_groups[] = { 2868caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 287eca2ebc7SMartin Sperl NULL, 288eca2ebc7SMartin Sperl }; 289eca2ebc7SMartin Sperl 290eca2ebc7SMartin Sperl void spi_statistics_add_transfer_stats(struct spi_statistics *stats, 291eca2ebc7SMartin Sperl struct spi_transfer *xfer, 2928caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 293eca2ebc7SMartin Sperl { 294eca2ebc7SMartin Sperl unsigned long flags; 2956b7bc061SMartin Sperl int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; 2966b7bc061SMartin Sperl 2976b7bc061SMartin Sperl if (l2len < 0) 2986b7bc061SMartin Sperl l2len = 0; 299eca2ebc7SMartin Sperl 300eca2ebc7SMartin Sperl spin_lock_irqsave(&stats->lock, flags); 301eca2ebc7SMartin Sperl 302eca2ebc7SMartin Sperl stats->transfers++; 3036b7bc061SMartin Sperl stats->transfer_bytes_histo[l2len]++; 304eca2ebc7SMartin Sperl 305eca2ebc7SMartin Sperl stats->bytes += xfer->len; 306eca2ebc7SMartin Sperl if ((xfer->tx_buf) && 3078caab75fSGeert Uytterhoeven (xfer->tx_buf != ctlr->dummy_tx)) 308eca2ebc7SMartin Sperl stats->bytes_tx += xfer->len; 309eca2ebc7SMartin Sperl if ((xfer->rx_buf) && 3108caab75fSGeert Uytterhoeven (xfer->rx_buf != ctlr->dummy_rx)) 311eca2ebc7SMartin Sperl stats->bytes_rx += xfer->len; 312eca2ebc7SMartin Sperl 313eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stats->lock, flags); 314eca2ebc7SMartin Sperl } 315eca2ebc7SMartin Sperl EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats); 3168ae12a0dSDavid Brownell 3178ae12a0dSDavid Brownell /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, 3188ae12a0dSDavid Brownell * and the sysfs version makes coldplug work too. 3198ae12a0dSDavid Brownell */ 3208ae12a0dSDavid Brownell 32175368bf6SAnton Vorontsov static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, 32275368bf6SAnton Vorontsov const struct spi_device *sdev) 32375368bf6SAnton Vorontsov { 32475368bf6SAnton Vorontsov while (id->name[0]) { 32575368bf6SAnton Vorontsov if (!strcmp(sdev->modalias, id->name)) 32675368bf6SAnton Vorontsov return id; 32775368bf6SAnton Vorontsov id++; 32875368bf6SAnton Vorontsov } 32975368bf6SAnton Vorontsov return NULL; 33075368bf6SAnton Vorontsov } 33175368bf6SAnton Vorontsov 33275368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 33375368bf6SAnton Vorontsov { 33475368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 33575368bf6SAnton Vorontsov 33675368bf6SAnton Vorontsov return spi_match_id(sdrv->id_table, sdev); 33775368bf6SAnton Vorontsov } 33875368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id); 33975368bf6SAnton Vorontsov 3408ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 3418ae12a0dSDavid Brownell { 3428ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 34375368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(drv); 34475368bf6SAnton Vorontsov 3455039563eSTrent Piepho /* Check override first, and if set, only use the named driver */ 3465039563eSTrent Piepho if (spi->driver_override) 3475039563eSTrent Piepho return strcmp(spi->driver_override, drv->name) == 0; 3485039563eSTrent Piepho 3492b7a32f7SSinan Akman /* Attempt an OF style match */ 3502b7a32f7SSinan Akman if (of_driver_match_device(dev, drv)) 3512b7a32f7SSinan Akman return 1; 3522b7a32f7SSinan Akman 35364bee4d2SMika Westerberg /* Then try ACPI */ 35464bee4d2SMika Westerberg if (acpi_driver_match_device(dev, drv)) 35564bee4d2SMika Westerberg return 1; 35664bee4d2SMika Westerberg 35775368bf6SAnton Vorontsov if (sdrv->id_table) 35875368bf6SAnton Vorontsov return !!spi_match_id(sdrv->id_table, spi); 3598ae12a0dSDavid Brownell 36035f74fcaSKay Sievers return strcmp(spi->modalias, drv->name) == 0; 3618ae12a0dSDavid Brownell } 3628ae12a0dSDavid Brownell 3637eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 3648ae12a0dSDavid Brownell { 3658ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 3668c4ff6d0SZhang Rui int rc; 3678c4ff6d0SZhang Rui 3688c4ff6d0SZhang Rui rc = acpi_device_uevent_modalias(dev, env); 3698c4ff6d0SZhang Rui if (rc != -ENODEV) 3708c4ff6d0SZhang Rui return rc; 3718ae12a0dSDavid Brownell 3722856670fSAndy Shevchenko return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 3738ae12a0dSDavid Brownell } 3748ae12a0dSDavid Brownell 3758ae12a0dSDavid Brownell struct bus_type spi_bus_type = { 3768ae12a0dSDavid Brownell .name = "spi", 377aa7da564SGreg Kroah-Hartman .dev_groups = spi_dev_groups, 3788ae12a0dSDavid Brownell .match = spi_match_device, 3798ae12a0dSDavid Brownell .uevent = spi_uevent, 3808ae12a0dSDavid Brownell }; 3818ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_bus_type); 3828ae12a0dSDavid Brownell 383b885244eSDavid Brownell 384b885244eSDavid Brownell static int spi_drv_probe(struct device *dev) 385b885244eSDavid Brownell { 386b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 38744af7927SJon Hunter struct spi_device *spi = to_spi_device(dev); 38833cf00e5SMika Westerberg int ret; 389b885244eSDavid Brownell 39086be408bSSylwester Nawrocki ret = of_clk_set_defaults(dev->of_node, false); 39186be408bSSylwester Nawrocki if (ret) 39286be408bSSylwester Nawrocki return ret; 39386be408bSSylwester Nawrocki 39444af7927SJon Hunter if (dev->of_node) { 39544af7927SJon Hunter spi->irq = of_irq_get(dev->of_node, 0); 39644af7927SJon Hunter if (spi->irq == -EPROBE_DEFER) 39744af7927SJon Hunter return -EPROBE_DEFER; 39844af7927SJon Hunter if (spi->irq < 0) 39944af7927SJon Hunter spi->irq = 0; 40044af7927SJon Hunter } 40144af7927SJon Hunter 402676e7c25SUlf Hansson ret = dev_pm_domain_attach(dev, true); 40371f277a7SUlf Hansson if (ret) 40471f277a7SUlf Hansson return ret; 40571f277a7SUlf Hansson 40644af7927SJon Hunter ret = sdrv->probe(spi); 40733cf00e5SMika Westerberg if (ret) 408676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 40933cf00e5SMika Westerberg 41033cf00e5SMika Westerberg return ret; 411b885244eSDavid Brownell } 412b885244eSDavid Brownell 413b885244eSDavid Brownell static int spi_drv_remove(struct device *dev) 414b885244eSDavid Brownell { 415b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 41633cf00e5SMika Westerberg int ret; 417b885244eSDavid Brownell 418aec35f4eSJean Delvare ret = sdrv->remove(to_spi_device(dev)); 419676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 42033cf00e5SMika Westerberg 42133cf00e5SMika Westerberg return ret; 422b885244eSDavid Brownell } 423b885244eSDavid Brownell 424b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev) 425b885244eSDavid Brownell { 426b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 427b885244eSDavid Brownell 428b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 429b885244eSDavid Brownell } 430b885244eSDavid Brownell 43133e34dc6SDavid Brownell /** 432ca5d2485SAndrew F. Davis * __spi_register_driver - register a SPI driver 43388c9321dSThierry Reding * @owner: owner module of the driver to register 43433e34dc6SDavid Brownell * @sdrv: the driver to register 43533e34dc6SDavid Brownell * Context: can sleep 43697d56dc6SJavier Martinez Canillas * 43797d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 43833e34dc6SDavid Brownell */ 439ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) 440b885244eSDavid Brownell { 441ca5d2485SAndrew F. Davis sdrv->driver.owner = owner; 442b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 443b885244eSDavid Brownell if (sdrv->probe) 444b885244eSDavid Brownell sdrv->driver.probe = spi_drv_probe; 445b885244eSDavid Brownell if (sdrv->remove) 446b885244eSDavid Brownell sdrv->driver.remove = spi_drv_remove; 447b885244eSDavid Brownell if (sdrv->shutdown) 448b885244eSDavid Brownell sdrv->driver.shutdown = spi_drv_shutdown; 449b885244eSDavid Brownell return driver_register(&sdrv->driver); 450b885244eSDavid Brownell } 451ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver); 452b885244eSDavid Brownell 4538ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 4548ae12a0dSDavid Brownell 4558ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that 4568caab75fSGeert Uytterhoeven * would make them board-specific. Similarly with SPI controller drivers. 4578ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 4588ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 4598ae12a0dSDavid Brownell */ 4608ae12a0dSDavid Brownell 4618ae12a0dSDavid Brownell struct boardinfo { 4628ae12a0dSDavid Brownell struct list_head list; 4632b9603a0SFeng Tang struct spi_board_info board_info; 4648ae12a0dSDavid Brownell }; 4658ae12a0dSDavid Brownell 4668ae12a0dSDavid Brownell static LIST_HEAD(board_list); 4678caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list); 4682b9603a0SFeng Tang 4692b9603a0SFeng Tang /* 4702b9603a0SFeng Tang * Used to protect add/del opertion for board_info list and 4718caab75fSGeert Uytterhoeven * spi_controller list, and their matching process 4729b61e302SSuniel Mahesh * also used to protect object of type struct idr 4732b9603a0SFeng Tang */ 47494040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 4758ae12a0dSDavid Brownell 476dc87c98eSGrant Likely /** 477dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 4788caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 479dc87c98eSGrant Likely * Context: can sleep 480dc87c98eSGrant Likely * 481dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 482dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 483dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 484dc87c98eSGrant Likely * spi_add_device() on it. 485dc87c98eSGrant Likely * 486dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 4878caab75fSGeert Uytterhoeven * spi_device structure to add it to the SPI controller. If the caller 488dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 489dc87c98eSGrant Likely * call spi_dev_put() on it. 490dc87c98eSGrant Likely * 49197d56dc6SJavier Martinez Canillas * Return: a pointer to the new device, or NULL. 492dc87c98eSGrant Likely */ 4938caab75fSGeert Uytterhoeven struct spi_device *spi_alloc_device(struct spi_controller *ctlr) 494dc87c98eSGrant Likely { 495dc87c98eSGrant Likely struct spi_device *spi; 496dc87c98eSGrant Likely 4978caab75fSGeert Uytterhoeven if (!spi_controller_get(ctlr)) 498dc87c98eSGrant Likely return NULL; 499dc87c98eSGrant Likely 5005fe5f05eSJingoo Han spi = kzalloc(sizeof(*spi), GFP_KERNEL); 501dc87c98eSGrant Likely if (!spi) { 5028caab75fSGeert Uytterhoeven spi_controller_put(ctlr); 503dc87c98eSGrant Likely return NULL; 504dc87c98eSGrant Likely } 505dc87c98eSGrant Likely 5068caab75fSGeert Uytterhoeven spi->master = spi->controller = ctlr; 5078caab75fSGeert Uytterhoeven spi->dev.parent = &ctlr->dev; 508dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 509dc87c98eSGrant Likely spi->dev.release = spidev_release; 510446411e1SAndreas Larsson spi->cs_gpio = -ENOENT; 511eca2ebc7SMartin Sperl 512eca2ebc7SMartin Sperl spin_lock_init(&spi->statistics.lock); 513eca2ebc7SMartin Sperl 514dc87c98eSGrant Likely device_initialize(&spi->dev); 515dc87c98eSGrant Likely return spi; 516dc87c98eSGrant Likely } 517dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device); 518dc87c98eSGrant Likely 519e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi) 520e13ac47bSJarkko Nikula { 521e13ac47bSJarkko Nikula struct acpi_device *adev = ACPI_COMPANION(&spi->dev); 522e13ac47bSJarkko Nikula 523e13ac47bSJarkko Nikula if (adev) { 524e13ac47bSJarkko Nikula dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); 525e13ac47bSJarkko Nikula return; 526e13ac47bSJarkko Nikula } 527e13ac47bSJarkko Nikula 5288caab75fSGeert Uytterhoeven dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), 529e13ac47bSJarkko Nikula spi->chip_select); 530e13ac47bSJarkko Nikula } 531e13ac47bSJarkko Nikula 532b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data) 533b6fb8d3aSMika Westerberg { 534b6fb8d3aSMika Westerberg struct spi_device *spi = to_spi_device(dev); 535b6fb8d3aSMika Westerberg struct spi_device *new_spi = data; 536b6fb8d3aSMika Westerberg 5378caab75fSGeert Uytterhoeven if (spi->controller == new_spi->controller && 538b6fb8d3aSMika Westerberg spi->chip_select == new_spi->chip_select) 539b6fb8d3aSMika Westerberg return -EBUSY; 540b6fb8d3aSMika Westerberg return 0; 541b6fb8d3aSMika Westerberg } 542b6fb8d3aSMika Westerberg 543dc87c98eSGrant Likely /** 544dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 545dc87c98eSGrant Likely * @spi: spi_device to register 546dc87c98eSGrant Likely * 547dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 548dc87c98eSGrant Likely * spi_alloc_device can be added onto the spi bus with this function. 549dc87c98eSGrant Likely * 55097d56dc6SJavier Martinez Canillas * Return: 0 on success; negative errno on failure 551dc87c98eSGrant Likely */ 552dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi) 553dc87c98eSGrant Likely { 554e48880e0SDavid Brownell static DEFINE_MUTEX(spi_add_lock); 5558caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 5568caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 557dc87c98eSGrant Likely int status; 558dc87c98eSGrant Likely 559dc87c98eSGrant Likely /* Chipselects are numbered 0..max; validate. */ 5608caab75fSGeert Uytterhoeven if (spi->chip_select >= ctlr->num_chipselect) { 5618caab75fSGeert Uytterhoeven dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 5628caab75fSGeert Uytterhoeven ctlr->num_chipselect); 563dc87c98eSGrant Likely return -EINVAL; 564dc87c98eSGrant Likely } 565dc87c98eSGrant Likely 566dc87c98eSGrant Likely /* Set the bus ID string */ 567e13ac47bSJarkko Nikula spi_dev_set_name(spi); 568e48880e0SDavid Brownell 569e48880e0SDavid Brownell /* We need to make sure there's no other device with this 570e48880e0SDavid Brownell * chipselect **BEFORE** we call setup(), else we'll trash 571e48880e0SDavid Brownell * its configuration. Lock against concurrent add() calls. 572e48880e0SDavid Brownell */ 573e48880e0SDavid Brownell mutex_lock(&spi_add_lock); 574e48880e0SDavid Brownell 575b6fb8d3aSMika Westerberg status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); 576b6fb8d3aSMika Westerberg if (status) { 577e48880e0SDavid Brownell dev_err(dev, "chipselect %d already in use\n", 578e48880e0SDavid Brownell spi->chip_select); 579e48880e0SDavid Brownell goto done; 580e48880e0SDavid Brownell } 581e48880e0SDavid Brownell 582f3186dd8SLinus Walleij /* Descriptors take precedence */ 583f3186dd8SLinus Walleij if (ctlr->cs_gpiods) 584f3186dd8SLinus Walleij spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select]; 585f3186dd8SLinus Walleij else if (ctlr->cs_gpios) 5868caab75fSGeert Uytterhoeven spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; 58774317984SJean-Christophe PLAGNIOL-VILLARD 588e48880e0SDavid Brownell /* Drivers may modify this initial i/o setup, but will 589e48880e0SDavid Brownell * normally rely on the device being setup. Devices 590e48880e0SDavid Brownell * using SPI_CS_HIGH can't coexist well otherwise... 591e48880e0SDavid Brownell */ 5927d077197SDavid Brownell status = spi_setup(spi); 593dc87c98eSGrant Likely if (status < 0) { 594eb288a1fSLinus Walleij dev_err(dev, "can't setup %s, status %d\n", 595eb288a1fSLinus Walleij dev_name(&spi->dev), status); 596e48880e0SDavid Brownell goto done; 597dc87c98eSGrant Likely } 598dc87c98eSGrant Likely 599e48880e0SDavid Brownell /* Device may be bound to an active driver when this returns */ 600dc87c98eSGrant Likely status = device_add(&spi->dev); 601e48880e0SDavid Brownell if (status < 0) 602eb288a1fSLinus Walleij dev_err(dev, "can't add %s, status %d\n", 603eb288a1fSLinus Walleij dev_name(&spi->dev), status); 604e48880e0SDavid Brownell else 60535f74fcaSKay Sievers dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 606e48880e0SDavid Brownell 607e48880e0SDavid Brownell done: 608e48880e0SDavid Brownell mutex_unlock(&spi_add_lock); 609e48880e0SDavid Brownell return status; 610dc87c98eSGrant Likely } 611dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device); 6128ae12a0dSDavid Brownell 61333e34dc6SDavid Brownell /** 61433e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 6158caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 61633e34dc6SDavid Brownell * @chip: Describes the SPI device 61733e34dc6SDavid Brownell * Context: can sleep 61833e34dc6SDavid Brownell * 61933e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 6208ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 6218ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 6228ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 6238ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 624082c8cb4SDavid Brownell * 62597d56dc6SJavier Martinez Canillas * Return: the new device, or NULL. 6268ae12a0dSDavid Brownell */ 6278caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr, 628e9d5a461SAdrian Bunk struct spi_board_info *chip) 6298ae12a0dSDavid Brownell { 6308ae12a0dSDavid Brownell struct spi_device *proxy; 6318ae12a0dSDavid Brownell int status; 6328ae12a0dSDavid Brownell 633082c8cb4SDavid Brownell /* NOTE: caller did any chip->bus_num checks necessary. 634082c8cb4SDavid Brownell * 635082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 636082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 637082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 638082c8cb4SDavid Brownell */ 639082c8cb4SDavid Brownell 6408caab75fSGeert Uytterhoeven proxy = spi_alloc_device(ctlr); 641dc87c98eSGrant Likely if (!proxy) 6428ae12a0dSDavid Brownell return NULL; 6438ae12a0dSDavid Brownell 644102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 645102eb975SGrant Likely 6468ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 6478ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 648980a01c9SDavid Brownell proxy->mode = chip->mode; 6498ae12a0dSDavid Brownell proxy->irq = chip->irq; 650102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 6518ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 6528ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 6538ae12a0dSDavid Brownell proxy->controller_state = NULL; 6548ae12a0dSDavid Brownell 655826cf175SDmitry Torokhov if (chip->properties) { 656826cf175SDmitry Torokhov status = device_add_properties(&proxy->dev, chip->properties); 657826cf175SDmitry Torokhov if (status) { 6588caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 659826cf175SDmitry Torokhov "failed to add properties to '%s': %d\n", 660826cf175SDmitry Torokhov chip->modalias, status); 661826cf175SDmitry Torokhov goto err_dev_put; 662826cf175SDmitry Torokhov } 6638ae12a0dSDavid Brownell } 664dc87c98eSGrant Likely 665826cf175SDmitry Torokhov status = spi_add_device(proxy); 666826cf175SDmitry Torokhov if (status < 0) 667826cf175SDmitry Torokhov goto err_remove_props; 668826cf175SDmitry Torokhov 669dc87c98eSGrant Likely return proxy; 670826cf175SDmitry Torokhov 671826cf175SDmitry Torokhov err_remove_props: 672826cf175SDmitry Torokhov if (chip->properties) 673826cf175SDmitry Torokhov device_remove_properties(&proxy->dev); 674826cf175SDmitry Torokhov err_dev_put: 675826cf175SDmitry Torokhov spi_dev_put(proxy); 676826cf175SDmitry Torokhov return NULL; 677dc87c98eSGrant Likely } 6788ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 6798ae12a0dSDavid Brownell 6803b1884c2SGeert Uytterhoeven /** 6813b1884c2SGeert Uytterhoeven * spi_unregister_device - unregister a single SPI device 6823b1884c2SGeert Uytterhoeven * @spi: spi_device to unregister 6833b1884c2SGeert Uytterhoeven * 6843b1884c2SGeert Uytterhoeven * Start making the passed SPI device vanish. Normally this would be handled 6858caab75fSGeert Uytterhoeven * by spi_unregister_controller(). 6863b1884c2SGeert Uytterhoeven */ 6873b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi) 6883b1884c2SGeert Uytterhoeven { 689bd6c1644SGeert Uytterhoeven if (!spi) 690bd6c1644SGeert Uytterhoeven return; 691bd6c1644SGeert Uytterhoeven 6928324147fSJohan Hovold if (spi->dev.of_node) { 693bd6c1644SGeert Uytterhoeven of_node_clear_flag(spi->dev.of_node, OF_POPULATED); 6948324147fSJohan Hovold of_node_put(spi->dev.of_node); 6958324147fSJohan Hovold } 6967f24467fSOctavian Purdila if (ACPI_COMPANION(&spi->dev)) 6977f24467fSOctavian Purdila acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); 6983b1884c2SGeert Uytterhoeven device_unregister(&spi->dev); 6993b1884c2SGeert Uytterhoeven } 7003b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device); 7013b1884c2SGeert Uytterhoeven 7028caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, 7032b9603a0SFeng Tang struct spi_board_info *bi) 7042b9603a0SFeng Tang { 7052b9603a0SFeng Tang struct spi_device *dev; 7062b9603a0SFeng Tang 7078caab75fSGeert Uytterhoeven if (ctlr->bus_num != bi->bus_num) 7082b9603a0SFeng Tang return; 7092b9603a0SFeng Tang 7108caab75fSGeert Uytterhoeven dev = spi_new_device(ctlr, bi); 7112b9603a0SFeng Tang if (!dev) 7128caab75fSGeert Uytterhoeven dev_err(ctlr->dev.parent, "can't create new device for %s\n", 7132b9603a0SFeng Tang bi->modalias); 7142b9603a0SFeng Tang } 7152b9603a0SFeng Tang 71633e34dc6SDavid Brownell /** 71733e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 71833e34dc6SDavid Brownell * @info: array of chip descriptors 71933e34dc6SDavid Brownell * @n: how many descriptors are provided 72033e34dc6SDavid Brownell * Context: can sleep 72133e34dc6SDavid Brownell * 7228ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 7238ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 7248ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 7258ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 7268ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 7278ae12a0dSDavid Brownell * 7288ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 7298ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 7308ae12a0dSDavid Brownell * would naturally declare its SPI devices. 7318ae12a0dSDavid Brownell * 7328ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 7338ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 734826cf175SDmitry Torokhov * Device properties are deep-copied though. 73597d56dc6SJavier Martinez Canillas * 73697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 7378ae12a0dSDavid Brownell */ 738fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n) 7398ae12a0dSDavid Brownell { 7408ae12a0dSDavid Brownell struct boardinfo *bi; 7412b9603a0SFeng Tang int i; 7428ae12a0dSDavid Brownell 743c7908a37SXiubo Li if (!n) 744f974cf57SDmitry Torokhov return 0; 745c7908a37SXiubo Li 746f9bdb7fdSMarkus Elfring bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); 7478ae12a0dSDavid Brownell if (!bi) 7488ae12a0dSDavid Brownell return -ENOMEM; 7498ae12a0dSDavid Brownell 7502b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 7518caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 7522b9603a0SFeng Tang 7532b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 754826cf175SDmitry Torokhov if (info->properties) { 755826cf175SDmitry Torokhov bi->board_info.properties = 756826cf175SDmitry Torokhov property_entries_dup(info->properties); 757826cf175SDmitry Torokhov if (IS_ERR(bi->board_info.properties)) 758826cf175SDmitry Torokhov return PTR_ERR(bi->board_info.properties); 759826cf175SDmitry Torokhov } 760826cf175SDmitry Torokhov 76194040828SMatthias Kaehlcke mutex_lock(&board_lock); 7628ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 7638caab75fSGeert Uytterhoeven list_for_each_entry(ctlr, &spi_controller_list, list) 7648caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, 7658caab75fSGeert Uytterhoeven &bi->board_info); 76694040828SMatthias Kaehlcke mutex_unlock(&board_lock); 7672b9603a0SFeng Tang } 7682b9603a0SFeng Tang 7698ae12a0dSDavid Brownell return 0; 7708ae12a0dSDavid Brownell } 7718ae12a0dSDavid Brownell 7728ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 7738ae12a0dSDavid Brownell 774b158935fSMark Brown static void spi_set_cs(struct spi_device *spi, bool enable) 775b158935fSMark Brown { 776b158935fSMark Brown if (spi->mode & SPI_CS_HIGH) 777b158935fSMark Brown enable = !enable; 778b158935fSMark Brown 779f3186dd8SLinus Walleij if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) { 780f3186dd8SLinus Walleij /* 781f3186dd8SLinus Walleij * Honour the SPI_NO_CS flag and invert the enable line, as 782f3186dd8SLinus Walleij * active low is default for SPI. Execution paths that handle 783f3186dd8SLinus Walleij * polarity inversion in gpiolib (such as device tree) will 784f3186dd8SLinus Walleij * enforce active high using the SPI_CS_HIGH resulting in a 785f3186dd8SLinus Walleij * double inversion through the code above. 786f3186dd8SLinus Walleij */ 787f3186dd8SLinus Walleij if (!(spi->mode & SPI_NO_CS)) { 788f3186dd8SLinus Walleij if (spi->cs_gpiod) 78928f7604fSFelix Fietkau gpiod_set_value_cansleep(spi->cs_gpiod, 79028f7604fSFelix Fietkau !enable); 791f3186dd8SLinus Walleij else 79228f7604fSFelix Fietkau gpio_set_value_cansleep(spi->cs_gpio, !enable); 793f3186dd8SLinus Walleij } 7948eee6b9dSThor Thayer /* Some SPI masters need both GPIO CS & slave_select */ 7958caab75fSGeert Uytterhoeven if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && 7968caab75fSGeert Uytterhoeven spi->controller->set_cs) 7978caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 7988caab75fSGeert Uytterhoeven } else if (spi->controller->set_cs) { 7998caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 8008eee6b9dSThor Thayer } 801b158935fSMark Brown } 802b158935fSMark Brown 8032de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA 80446336966SBoris Brezillon int spi_map_buf(struct spi_controller *ctlr, struct device *dev, 8056ad45a27SMark Brown struct sg_table *sgt, void *buf, size_t len, 8066ad45a27SMark Brown enum dma_data_direction dir) 8076ad45a27SMark Brown { 8086ad45a27SMark Brown const bool vmalloced_buf = is_vmalloc_addr(buf); 809df88e91bSAndy Shevchenko unsigned int max_seg_size = dma_get_max_seg_size(dev); 810b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM 811b1b8153cSVignesh R const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && 812b1b8153cSVignesh R (unsigned long)buf < (PKMAP_BASE + 813b1b8153cSVignesh R (LAST_PKMAP * PAGE_SIZE))); 814b1b8153cSVignesh R #else 815b1b8153cSVignesh R const bool kmap_buf = false; 816b1b8153cSVignesh R #endif 81765598c13SAndrew Gabbasov int desc_len; 81865598c13SAndrew Gabbasov int sgs; 8196ad45a27SMark Brown struct page *vm_page; 8208dd4a016SJuan Gutierrez struct scatterlist *sg; 8216ad45a27SMark Brown void *sg_buf; 8226ad45a27SMark Brown size_t min; 8236ad45a27SMark Brown int i, ret; 8246ad45a27SMark Brown 825b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 826df88e91bSAndy Shevchenko desc_len = min_t(int, max_seg_size, PAGE_SIZE); 82765598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); 8280569a88fSVignesh R } else if (virt_addr_valid(buf)) { 8298caab75fSGeert Uytterhoeven desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); 83065598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len, desc_len); 8310569a88fSVignesh R } else { 8320569a88fSVignesh R return -EINVAL; 83365598c13SAndrew Gabbasov } 83465598c13SAndrew Gabbasov 8356ad45a27SMark Brown ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); 8366ad45a27SMark Brown if (ret != 0) 8376ad45a27SMark Brown return ret; 8386ad45a27SMark Brown 8398dd4a016SJuan Gutierrez sg = &sgt->sgl[0]; 8406ad45a27SMark Brown for (i = 0; i < sgs; i++) { 8416ad45a27SMark Brown 842b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 843ce99319aSMaxime Chevallier /* 844ce99319aSMaxime Chevallier * Next scatterlist entry size is the minimum between 845ce99319aSMaxime Chevallier * the desc_len and the remaining buffer length that 846ce99319aSMaxime Chevallier * fits in a page. 847ce99319aSMaxime Chevallier */ 848ce99319aSMaxime Chevallier min = min_t(size_t, desc_len, 849ce99319aSMaxime Chevallier min_t(size_t, len, 850ce99319aSMaxime Chevallier PAGE_SIZE - offset_in_page(buf))); 851b1b8153cSVignesh R if (vmalloced_buf) 8526ad45a27SMark Brown vm_page = vmalloc_to_page(buf); 853b1b8153cSVignesh R else 854b1b8153cSVignesh R vm_page = kmap_to_page(buf); 8556ad45a27SMark Brown if (!vm_page) { 8566ad45a27SMark Brown sg_free_table(sgt); 8576ad45a27SMark Brown return -ENOMEM; 8586ad45a27SMark Brown } 8598dd4a016SJuan Gutierrez sg_set_page(sg, vm_page, 860c1aefbddSCharles Keepax min, offset_in_page(buf)); 8616ad45a27SMark Brown } else { 86265598c13SAndrew Gabbasov min = min_t(size_t, len, desc_len); 8636ad45a27SMark Brown sg_buf = buf; 8648dd4a016SJuan Gutierrez sg_set_buf(sg, sg_buf, min); 8656ad45a27SMark Brown } 8666ad45a27SMark Brown 8676ad45a27SMark Brown buf += min; 8686ad45a27SMark Brown len -= min; 8698dd4a016SJuan Gutierrez sg = sg_next(sg); 8706ad45a27SMark Brown } 8716ad45a27SMark Brown 8726ad45a27SMark Brown ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); 87389e4b66aSGeert Uytterhoeven if (!ret) 87489e4b66aSGeert Uytterhoeven ret = -ENOMEM; 8756ad45a27SMark Brown if (ret < 0) { 8766ad45a27SMark Brown sg_free_table(sgt); 8776ad45a27SMark Brown return ret; 8786ad45a27SMark Brown } 8796ad45a27SMark Brown 8806ad45a27SMark Brown sgt->nents = ret; 8816ad45a27SMark Brown 8826ad45a27SMark Brown return 0; 8836ad45a27SMark Brown } 8846ad45a27SMark Brown 88546336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, 8866ad45a27SMark Brown struct sg_table *sgt, enum dma_data_direction dir) 8876ad45a27SMark Brown { 8886ad45a27SMark Brown if (sgt->orig_nents) { 8896ad45a27SMark Brown dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); 8906ad45a27SMark Brown sg_free_table(sgt); 8916ad45a27SMark Brown } 8926ad45a27SMark Brown } 8936ad45a27SMark Brown 8948caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 89599adef31SMark Brown { 89699adef31SMark Brown struct device *tx_dev, *rx_dev; 89799adef31SMark Brown struct spi_transfer *xfer; 8986ad45a27SMark Brown int ret; 8993a2eba9bSMark Brown 9008caab75fSGeert Uytterhoeven if (!ctlr->can_dma) 90199adef31SMark Brown return 0; 90299adef31SMark Brown 9038caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 9048caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 905c37f45b5SLeilk Liu else 9068caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 907c37f45b5SLeilk Liu 9088caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 9098caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 910c37f45b5SLeilk Liu else 9118caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 91299adef31SMark Brown 91399adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9148caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 91599adef31SMark Brown continue; 91699adef31SMark Brown 91799adef31SMark Brown if (xfer->tx_buf != NULL) { 9188caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, 9196ad45a27SMark Brown (void *)xfer->tx_buf, xfer->len, 92099adef31SMark Brown DMA_TO_DEVICE); 9216ad45a27SMark Brown if (ret != 0) 9226ad45a27SMark Brown return ret; 92399adef31SMark Brown } 92499adef31SMark Brown 92599adef31SMark Brown if (xfer->rx_buf != NULL) { 9268caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, 92799adef31SMark Brown xfer->rx_buf, xfer->len, 92899adef31SMark Brown DMA_FROM_DEVICE); 9296ad45a27SMark Brown if (ret != 0) { 9308caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, 9316ad45a27SMark Brown DMA_TO_DEVICE); 9326ad45a27SMark Brown return ret; 93399adef31SMark Brown } 93499adef31SMark Brown } 93599adef31SMark Brown } 93699adef31SMark Brown 9378caab75fSGeert Uytterhoeven ctlr->cur_msg_mapped = true; 93899adef31SMark Brown 93999adef31SMark Brown return 0; 94099adef31SMark Brown } 94199adef31SMark Brown 9428caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg) 94399adef31SMark Brown { 94499adef31SMark Brown struct spi_transfer *xfer; 94599adef31SMark Brown struct device *tx_dev, *rx_dev; 94699adef31SMark Brown 9478caab75fSGeert Uytterhoeven if (!ctlr->cur_msg_mapped || !ctlr->can_dma) 94899adef31SMark Brown return 0; 94999adef31SMark Brown 9508caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 9518caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 952c37f45b5SLeilk Liu else 9538caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 954c37f45b5SLeilk Liu 9558caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 9568caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 957c37f45b5SLeilk Liu else 9588caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 95999adef31SMark Brown 96099adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9618caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 96299adef31SMark Brown continue; 96399adef31SMark Brown 9648caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 9658caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 96699adef31SMark Brown } 96799adef31SMark Brown 96899adef31SMark Brown return 0; 96999adef31SMark Brown } 9702de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */ 9718caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr, 9722de440f5SGeert Uytterhoeven struct spi_message *msg) 9732de440f5SGeert Uytterhoeven { 9742de440f5SGeert Uytterhoeven return 0; 9752de440f5SGeert Uytterhoeven } 9762de440f5SGeert Uytterhoeven 9778caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr, 9782de440f5SGeert Uytterhoeven struct spi_message *msg) 9792de440f5SGeert Uytterhoeven { 9802de440f5SGeert Uytterhoeven return 0; 9812de440f5SGeert Uytterhoeven } 9822de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */ 9832de440f5SGeert Uytterhoeven 9848caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr, 9854b786458SMartin Sperl struct spi_message *msg) 9864b786458SMartin Sperl { 9874b786458SMartin Sperl struct spi_transfer *xfer; 9884b786458SMartin Sperl 9894b786458SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9904b786458SMartin Sperl /* 9914b786458SMartin Sperl * Restore the original value of tx_buf or rx_buf if they are 9924b786458SMartin Sperl * NULL. 9934b786458SMartin Sperl */ 9948caab75fSGeert Uytterhoeven if (xfer->tx_buf == ctlr->dummy_tx) 9954b786458SMartin Sperl xfer->tx_buf = NULL; 9968caab75fSGeert Uytterhoeven if (xfer->rx_buf == ctlr->dummy_rx) 9974b786458SMartin Sperl xfer->rx_buf = NULL; 9984b786458SMartin Sperl } 9994b786458SMartin Sperl 10008caab75fSGeert Uytterhoeven return __spi_unmap_msg(ctlr, msg); 10014b786458SMartin Sperl } 10024b786458SMartin Sperl 10038caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 10042de440f5SGeert Uytterhoeven { 10052de440f5SGeert Uytterhoeven struct spi_transfer *xfer; 10062de440f5SGeert Uytterhoeven void *tmp; 10072de440f5SGeert Uytterhoeven unsigned int max_tx, max_rx; 10082de440f5SGeert Uytterhoeven 10098caab75fSGeert Uytterhoeven if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) { 10102de440f5SGeert Uytterhoeven max_tx = 0; 10112de440f5SGeert Uytterhoeven max_rx = 0; 10122de440f5SGeert Uytterhoeven 10132de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, transfer_list) { 10148caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && 10152de440f5SGeert Uytterhoeven !xfer->tx_buf) 10162de440f5SGeert Uytterhoeven max_tx = max(xfer->len, max_tx); 10178caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && 10182de440f5SGeert Uytterhoeven !xfer->rx_buf) 10192de440f5SGeert Uytterhoeven max_rx = max(xfer->len, max_rx); 10202de440f5SGeert Uytterhoeven } 10212de440f5SGeert Uytterhoeven 10222de440f5SGeert Uytterhoeven if (max_tx) { 10238caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_tx, max_tx, 10242de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 10252de440f5SGeert Uytterhoeven if (!tmp) 10262de440f5SGeert Uytterhoeven return -ENOMEM; 10278caab75fSGeert Uytterhoeven ctlr->dummy_tx = tmp; 10282de440f5SGeert Uytterhoeven memset(tmp, 0, max_tx); 10292de440f5SGeert Uytterhoeven } 10302de440f5SGeert Uytterhoeven 10312de440f5SGeert Uytterhoeven if (max_rx) { 10328caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_rx, max_rx, 10332de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 10342de440f5SGeert Uytterhoeven if (!tmp) 10352de440f5SGeert Uytterhoeven return -ENOMEM; 10368caab75fSGeert Uytterhoeven ctlr->dummy_rx = tmp; 10372de440f5SGeert Uytterhoeven } 10382de440f5SGeert Uytterhoeven 10392de440f5SGeert Uytterhoeven if (max_tx || max_rx) { 10402de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, 10412de440f5SGeert Uytterhoeven transfer_list) { 10425442dcaaSChris Lesiak if (!xfer->len) 10435442dcaaSChris Lesiak continue; 10442de440f5SGeert Uytterhoeven if (!xfer->tx_buf) 10458caab75fSGeert Uytterhoeven xfer->tx_buf = ctlr->dummy_tx; 10462de440f5SGeert Uytterhoeven if (!xfer->rx_buf) 10478caab75fSGeert Uytterhoeven xfer->rx_buf = ctlr->dummy_rx; 10482de440f5SGeert Uytterhoeven } 10492de440f5SGeert Uytterhoeven } 10502de440f5SGeert Uytterhoeven } 10512de440f5SGeert Uytterhoeven 10528caab75fSGeert Uytterhoeven return __spi_map_msg(ctlr, msg); 10532de440f5SGeert Uytterhoeven } 105499adef31SMark Brown 1055810923f3SLubomir Rintel static int spi_transfer_wait(struct spi_controller *ctlr, 1056810923f3SLubomir Rintel struct spi_message *msg, 1057810923f3SLubomir Rintel struct spi_transfer *xfer) 1058810923f3SLubomir Rintel { 1059810923f3SLubomir Rintel struct spi_statistics *statm = &ctlr->statistics; 1060810923f3SLubomir Rintel struct spi_statistics *stats = &msg->spi->statistics; 1061810923f3SLubomir Rintel unsigned long long ms = 1; 1062810923f3SLubomir Rintel 1063810923f3SLubomir Rintel if (spi_controller_is_slave(ctlr)) { 1064810923f3SLubomir Rintel if (wait_for_completion_interruptible(&ctlr->xfer_completion)) { 1065810923f3SLubomir Rintel dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n"); 1066810923f3SLubomir Rintel return -EINTR; 1067810923f3SLubomir Rintel } 1068810923f3SLubomir Rintel } else { 1069810923f3SLubomir Rintel ms = 8LL * 1000LL * xfer->len; 1070810923f3SLubomir Rintel do_div(ms, xfer->speed_hz); 1071810923f3SLubomir Rintel ms += ms + 200; /* some tolerance */ 1072810923f3SLubomir Rintel 1073810923f3SLubomir Rintel if (ms > UINT_MAX) 1074810923f3SLubomir Rintel ms = UINT_MAX; 1075810923f3SLubomir Rintel 1076810923f3SLubomir Rintel ms = wait_for_completion_timeout(&ctlr->xfer_completion, 1077810923f3SLubomir Rintel msecs_to_jiffies(ms)); 1078810923f3SLubomir Rintel 1079810923f3SLubomir Rintel if (ms == 0) { 1080810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(statm, timedout); 1081810923f3SLubomir Rintel SPI_STATISTICS_INCREMENT_FIELD(stats, timedout); 1082810923f3SLubomir Rintel dev_err(&msg->spi->dev, 1083810923f3SLubomir Rintel "SPI transfer timed out\n"); 1084810923f3SLubomir Rintel return -ETIMEDOUT; 1085810923f3SLubomir Rintel } 1086810923f3SLubomir Rintel } 1087810923f3SLubomir Rintel 1088810923f3SLubomir Rintel return 0; 1089810923f3SLubomir Rintel } 1090810923f3SLubomir Rintel 1091b158935fSMark Brown /* 1092b158935fSMark Brown * spi_transfer_one_message - Default implementation of transfer_one_message() 1093b158935fSMark Brown * 1094b158935fSMark Brown * This is a standard implementation of transfer_one_message() for 10958ba811a7SMoritz Fischer * drivers which implement a transfer_one() operation. It provides 1096b158935fSMark Brown * standard handling of delays and chip select management. 1097b158935fSMark Brown */ 10988caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr, 1099b158935fSMark Brown struct spi_message *msg) 1100b158935fSMark Brown { 1101b158935fSMark Brown struct spi_transfer *xfer; 1102b158935fSMark Brown bool keep_cs = false; 1103b158935fSMark Brown int ret = 0; 11048caab75fSGeert Uytterhoeven struct spi_statistics *statm = &ctlr->statistics; 1105eca2ebc7SMartin Sperl struct spi_statistics *stats = &msg->spi->statistics; 1106b158935fSMark Brown 1107b158935fSMark Brown spi_set_cs(msg->spi, true); 1108b158935fSMark Brown 1109eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, messages); 1110eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, messages); 1111eca2ebc7SMartin Sperl 1112b158935fSMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1113b158935fSMark Brown trace_spi_transfer_start(msg, xfer); 1114b158935fSMark Brown 11158caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(statm, xfer, ctlr); 11168caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(stats, xfer, ctlr); 1117eca2ebc7SMartin Sperl 111838ec10f6SMark Brown if (xfer->tx_buf || xfer->rx_buf) { 11198caab75fSGeert Uytterhoeven reinit_completion(&ctlr->xfer_completion); 1120b158935fSMark Brown 11218caab75fSGeert Uytterhoeven ret = ctlr->transfer_one(ctlr, msg->spi, xfer); 1122b158935fSMark Brown if (ret < 0) { 1123eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, 1124eca2ebc7SMartin Sperl errors); 1125eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, 1126eca2ebc7SMartin Sperl errors); 1127b158935fSMark Brown dev_err(&msg->spi->dev, 1128b158935fSMark Brown "SPI transfer failed: %d\n", ret); 1129b158935fSMark Brown goto out; 1130b158935fSMark Brown } 1131b158935fSMark Brown 1132d57e7960SMark Brown if (ret > 0) { 1133810923f3SLubomir Rintel ret = spi_transfer_wait(ctlr, msg, xfer); 1134810923f3SLubomir Rintel if (ret < 0) 1135810923f3SLubomir Rintel msg->status = ret; 1136d57e7960SMark Brown } 113738ec10f6SMark Brown } else { 113838ec10f6SMark Brown if (xfer->len) 113938ec10f6SMark Brown dev_err(&msg->spi->dev, 114038ec10f6SMark Brown "Bufferless transfer has length %u\n", 114138ec10f6SMark Brown xfer->len); 114238ec10f6SMark Brown } 1143b158935fSMark Brown 1144b158935fSMark Brown trace_spi_transfer_stop(msg, xfer); 1145b158935fSMark Brown 1146b158935fSMark Brown if (msg->status != -EINPROGRESS) 1147b158935fSMark Brown goto out; 1148b158935fSMark Brown 11498244bd3aSDaniel Kurtz if (xfer->delay_usecs) { 11508244bd3aSDaniel Kurtz u16 us = xfer->delay_usecs; 11518244bd3aSDaniel Kurtz 11528244bd3aSDaniel Kurtz if (us <= 10) 11538244bd3aSDaniel Kurtz udelay(us); 11548244bd3aSDaniel Kurtz else 11558244bd3aSDaniel Kurtz usleep_range(us, us + DIV_ROUND_UP(us, 10)); 11568244bd3aSDaniel Kurtz } 1157b158935fSMark Brown 1158b158935fSMark Brown if (xfer->cs_change) { 1159b158935fSMark Brown if (list_is_last(&xfer->transfer_list, 1160b158935fSMark Brown &msg->transfers)) { 1161b158935fSMark Brown keep_cs = true; 1162b158935fSMark Brown } else { 11630b73aa63SMark Brown spi_set_cs(msg->spi, false); 11640b73aa63SMark Brown udelay(10); 11650b73aa63SMark Brown spi_set_cs(msg->spi, true); 1166b158935fSMark Brown } 1167b158935fSMark Brown } 1168b158935fSMark Brown 1169b158935fSMark Brown msg->actual_length += xfer->len; 1170b158935fSMark Brown } 1171b158935fSMark Brown 1172b158935fSMark Brown out: 1173b158935fSMark Brown if (ret != 0 || !keep_cs) 1174b158935fSMark Brown spi_set_cs(msg->spi, false); 1175b158935fSMark Brown 1176b158935fSMark Brown if (msg->status == -EINPROGRESS) 1177b158935fSMark Brown msg->status = ret; 1178b158935fSMark Brown 11798caab75fSGeert Uytterhoeven if (msg->status && ctlr->handle_err) 11808caab75fSGeert Uytterhoeven ctlr->handle_err(ctlr, msg); 1181b716c4ffSAndy Shevchenko 11828caab75fSGeert Uytterhoeven spi_res_release(ctlr, msg); 1183d780c371SMartin Sperl 11848caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 1185b158935fSMark Brown 1186b158935fSMark Brown return ret; 1187b158935fSMark Brown } 1188b158935fSMark Brown 1189b158935fSMark Brown /** 1190b158935fSMark Brown * spi_finalize_current_transfer - report completion of a transfer 11918caab75fSGeert Uytterhoeven * @ctlr: the controller reporting completion 1192b158935fSMark Brown * 1193b158935fSMark Brown * Called by SPI drivers using the core transfer_one_message() 1194b158935fSMark Brown * implementation to notify it that the current interrupt driven 11959e8f4882SGeert Uytterhoeven * transfer has finished and the next one may be scheduled. 1196b158935fSMark Brown */ 11978caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr) 1198b158935fSMark Brown { 11998caab75fSGeert Uytterhoeven complete(&ctlr->xfer_completion); 1200b158935fSMark Brown } 1201b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); 1202b158935fSMark Brown 1203ffbbdd21SLinus Walleij /** 1204fc9e0f71SMark Brown * __spi_pump_messages - function which processes spi message queue 12058caab75fSGeert Uytterhoeven * @ctlr: controller to process queue for 1206fc9e0f71SMark Brown * @in_kthread: true if we are in the context of the message pump thread 1207ffbbdd21SLinus Walleij * 1208ffbbdd21SLinus Walleij * This function checks if there is any spi message in the queue that 1209ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 1210ffbbdd21SLinus Walleij * and transfer each message. 1211ffbbdd21SLinus Walleij * 12120461a414SMark Brown * Note that it is called both from the kthread itself and also from 12130461a414SMark Brown * inside spi_sync(); the queue extraction handling at the top of the 12140461a414SMark Brown * function should deal with this safely. 1215ffbbdd21SLinus Walleij */ 12168caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1217ffbbdd21SLinus Walleij { 1218ffbbdd21SLinus Walleij unsigned long flags; 1219ffbbdd21SLinus Walleij bool was_busy = false; 1220ffbbdd21SLinus Walleij int ret; 1221ffbbdd21SLinus Walleij 1222983aee5dSMark Brown /* Lock queue */ 12238caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1224983aee5dSMark Brown 1225983aee5dSMark Brown /* Make sure we are not already running a message */ 12268caab75fSGeert Uytterhoeven if (ctlr->cur_msg) { 12278caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1228983aee5dSMark Brown return; 1229983aee5dSMark Brown } 1230983aee5dSMark Brown 1231f0125f1aSMark Brown /* If another context is idling the device then defer */ 12328caab75fSGeert Uytterhoeven if (ctlr->idling) { 12338caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 12348caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 12350461a414SMark Brown return; 12360461a414SMark Brown } 12370461a414SMark Brown 1238983aee5dSMark Brown /* Check if the queue is idle */ 12398caab75fSGeert Uytterhoeven if (list_empty(&ctlr->queue) || !ctlr->running) { 12408caab75fSGeert Uytterhoeven if (!ctlr->busy) { 12418caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1242ffbbdd21SLinus Walleij return; 1243ffbbdd21SLinus Walleij } 1244fc9e0f71SMark Brown 1245f0125f1aSMark Brown /* Only do teardown in the thread */ 1246f0125f1aSMark Brown if (!in_kthread) { 1247f0125f1aSMark Brown kthread_queue_work(&ctlr->kworker, 1248f0125f1aSMark Brown &ctlr->pump_messages); 1249f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1250f0125f1aSMark Brown return; 1251f0125f1aSMark Brown } 1252f0125f1aSMark Brown 1253f0125f1aSMark Brown ctlr->busy = false; 1254f0125f1aSMark Brown ctlr->idling = true; 1255f0125f1aSMark Brown spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1256f0125f1aSMark Brown 1257f0125f1aSMark Brown kfree(ctlr->dummy_rx); 1258f0125f1aSMark Brown ctlr->dummy_rx = NULL; 1259f0125f1aSMark Brown kfree(ctlr->dummy_tx); 1260f0125f1aSMark Brown ctlr->dummy_tx = NULL; 1261f0125f1aSMark Brown if (ctlr->unprepare_transfer_hardware && 1262f0125f1aSMark Brown ctlr->unprepare_transfer_hardware(ctlr)) 1263f0125f1aSMark Brown dev_err(&ctlr->dev, 1264f0125f1aSMark Brown "failed to unprepare transfer hardware\n"); 1265f0125f1aSMark Brown if (ctlr->auto_runtime_pm) { 1266f0125f1aSMark Brown pm_runtime_mark_last_busy(ctlr->dev.parent); 1267f0125f1aSMark Brown pm_runtime_put_autosuspend(ctlr->dev.parent); 1268f0125f1aSMark Brown } 1269f0125f1aSMark Brown trace_spi_controller_idle(ctlr); 1270f0125f1aSMark Brown 1271f0125f1aSMark Brown spin_lock_irqsave(&ctlr->queue_lock, flags); 1272f0125f1aSMark Brown ctlr->idling = false; 12738caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1274ffbbdd21SLinus Walleij return; 1275ffbbdd21SLinus Walleij } 1276ffbbdd21SLinus Walleij 1277ffbbdd21SLinus Walleij /* Extract head of queue */ 12788caab75fSGeert Uytterhoeven ctlr->cur_msg = 12798caab75fSGeert Uytterhoeven list_first_entry(&ctlr->queue, struct spi_message, queue); 1280ffbbdd21SLinus Walleij 12818caab75fSGeert Uytterhoeven list_del_init(&ctlr->cur_msg->queue); 12828caab75fSGeert Uytterhoeven if (ctlr->busy) 1283ffbbdd21SLinus Walleij was_busy = true; 1284ffbbdd21SLinus Walleij else 12858caab75fSGeert Uytterhoeven ctlr->busy = true; 12868caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1287ffbbdd21SLinus Walleij 12888caab75fSGeert Uytterhoeven mutex_lock(&ctlr->io_mutex); 1289ef4d96ecSMark Brown 12908caab75fSGeert Uytterhoeven if (!was_busy && ctlr->auto_runtime_pm) { 12918caab75fSGeert Uytterhoeven ret = pm_runtime_get_sync(ctlr->dev.parent); 129249834de2SMark Brown if (ret < 0) { 12937e48e23aSTony Lindgren pm_runtime_put_noidle(ctlr->dev.parent); 12948caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "Failed to power device: %d\n", 129549834de2SMark Brown ret); 12968caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 129749834de2SMark Brown return; 129849834de2SMark Brown } 129949834de2SMark Brown } 130049834de2SMark Brown 130156ec1978SMark Brown if (!was_busy) 13028caab75fSGeert Uytterhoeven trace_spi_controller_busy(ctlr); 130356ec1978SMark Brown 13048caab75fSGeert Uytterhoeven if (!was_busy && ctlr->prepare_transfer_hardware) { 13058caab75fSGeert Uytterhoeven ret = ctlr->prepare_transfer_hardware(ctlr); 1306ffbbdd21SLinus Walleij if (ret) { 13078caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 1308ffbbdd21SLinus Walleij "failed to prepare transfer hardware\n"); 130949834de2SMark Brown 13108caab75fSGeert Uytterhoeven if (ctlr->auto_runtime_pm) 13118caab75fSGeert Uytterhoeven pm_runtime_put(ctlr->dev.parent); 13128caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 1313ffbbdd21SLinus Walleij return; 1314ffbbdd21SLinus Walleij } 1315ffbbdd21SLinus Walleij } 1316ffbbdd21SLinus Walleij 13178caab75fSGeert Uytterhoeven trace_spi_message_start(ctlr->cur_msg); 131856ec1978SMark Brown 13198caab75fSGeert Uytterhoeven if (ctlr->prepare_message) { 13208caab75fSGeert Uytterhoeven ret = ctlr->prepare_message(ctlr, ctlr->cur_msg); 13212841a5fcSMark Brown if (ret) { 13228caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to prepare message: %d\n", 13238caab75fSGeert Uytterhoeven ret); 13248caab75fSGeert Uytterhoeven ctlr->cur_msg->status = ret; 13258caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 132649023d2eSJon Hunter goto out; 13272841a5fcSMark Brown } 13288caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = true; 13292841a5fcSMark Brown } 13302841a5fcSMark Brown 13318caab75fSGeert Uytterhoeven ret = spi_map_msg(ctlr, ctlr->cur_msg); 133299adef31SMark Brown if (ret) { 13338caab75fSGeert Uytterhoeven ctlr->cur_msg->status = ret; 13348caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 133549023d2eSJon Hunter goto out; 133699adef31SMark Brown } 133799adef31SMark Brown 13388caab75fSGeert Uytterhoeven ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg); 1339ffbbdd21SLinus Walleij if (ret) { 13408caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 13411f802f82SGeert Uytterhoeven "failed to transfer one message from queue\n"); 134249023d2eSJon Hunter goto out; 1343ffbbdd21SLinus Walleij } 134449023d2eSJon Hunter 134549023d2eSJon Hunter out: 13468caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 134762826970SMark Brown 134862826970SMark Brown /* Prod the scheduler in case transfer_one() was busy waiting */ 134949023d2eSJon Hunter if (!ret) 135062826970SMark Brown cond_resched(); 1351ffbbdd21SLinus Walleij } 1352ffbbdd21SLinus Walleij 1353fc9e0f71SMark Brown /** 1354fc9e0f71SMark Brown * spi_pump_messages - kthread work function which processes spi message queue 13558caab75fSGeert Uytterhoeven * @work: pointer to kthread work struct contained in the controller struct 1356fc9e0f71SMark Brown */ 1357fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work) 1358fc9e0f71SMark Brown { 13598caab75fSGeert Uytterhoeven struct spi_controller *ctlr = 13608caab75fSGeert Uytterhoeven container_of(work, struct spi_controller, pump_messages); 1361fc9e0f71SMark Brown 13628caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, true); 1363fc9e0f71SMark Brown } 1364fc9e0f71SMark Brown 13658caab75fSGeert Uytterhoeven static int spi_init_queue(struct spi_controller *ctlr) 1366ffbbdd21SLinus Walleij { 1367ffbbdd21SLinus Walleij struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; 1368ffbbdd21SLinus Walleij 13698caab75fSGeert Uytterhoeven ctlr->running = false; 13708caab75fSGeert Uytterhoeven ctlr->busy = false; 1371ffbbdd21SLinus Walleij 13728caab75fSGeert Uytterhoeven kthread_init_worker(&ctlr->kworker); 13738caab75fSGeert Uytterhoeven ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker, 13748caab75fSGeert Uytterhoeven "%s", dev_name(&ctlr->dev)); 13758caab75fSGeert Uytterhoeven if (IS_ERR(ctlr->kworker_task)) { 13768caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to create message pump task\n"); 13778caab75fSGeert Uytterhoeven return PTR_ERR(ctlr->kworker_task); 1378ffbbdd21SLinus Walleij } 13798caab75fSGeert Uytterhoeven kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1380f0125f1aSMark Brown 1381ffbbdd21SLinus Walleij /* 13828caab75fSGeert Uytterhoeven * Controller config will indicate if this controller should run the 1383ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 1384ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 1385ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 1386ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 1387ffbbdd21SLinus Walleij */ 13888caab75fSGeert Uytterhoeven if (ctlr->rt) { 13898caab75fSGeert Uytterhoeven dev_info(&ctlr->dev, 1390ffbbdd21SLinus Walleij "will run message pump with realtime priority\n"); 13918caab75fSGeert Uytterhoeven sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, ¶m); 1392ffbbdd21SLinus Walleij } 1393ffbbdd21SLinus Walleij 1394ffbbdd21SLinus Walleij return 0; 1395ffbbdd21SLinus Walleij } 1396ffbbdd21SLinus Walleij 1397ffbbdd21SLinus Walleij /** 1398ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 1399ffbbdd21SLinus Walleij * messages 14008caab75fSGeert Uytterhoeven * @ctlr: the controller to check for queued messages 1401ffbbdd21SLinus Walleij * 1402ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 1403ffbbdd21SLinus Walleij * this call. 140497d56dc6SJavier Martinez Canillas * 140597d56dc6SJavier Martinez Canillas * Return: the next message in the queue, else NULL if the queue is empty. 1406ffbbdd21SLinus Walleij */ 14078caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1408ffbbdd21SLinus Walleij { 1409ffbbdd21SLinus Walleij struct spi_message *next; 1410ffbbdd21SLinus Walleij unsigned long flags; 1411ffbbdd21SLinus Walleij 1412ffbbdd21SLinus Walleij /* get a pointer to the next message, if any */ 14138caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 14148caab75fSGeert Uytterhoeven next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 14151cfd97f9SAxel Lin queue); 14168caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1417ffbbdd21SLinus Walleij 1418ffbbdd21SLinus Walleij return next; 1419ffbbdd21SLinus Walleij } 1420ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1421ffbbdd21SLinus Walleij 1422ffbbdd21SLinus Walleij /** 1423ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 14248caab75fSGeert Uytterhoeven * @ctlr: the controller to return the message to 1425ffbbdd21SLinus Walleij * 1426ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 1427ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 1428ffbbdd21SLinus Walleij */ 14298caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr) 1430ffbbdd21SLinus Walleij { 1431ffbbdd21SLinus Walleij struct spi_message *mesg; 1432ffbbdd21SLinus Walleij unsigned long flags; 14332841a5fcSMark Brown int ret; 1434ffbbdd21SLinus Walleij 14358caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 14368caab75fSGeert Uytterhoeven mesg = ctlr->cur_msg; 14378caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1438ffbbdd21SLinus Walleij 14398caab75fSGeert Uytterhoeven spi_unmap_msg(ctlr, mesg); 144099adef31SMark Brown 14418caab75fSGeert Uytterhoeven if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { 14428caab75fSGeert Uytterhoeven ret = ctlr->unprepare_message(ctlr, mesg); 14432841a5fcSMark Brown if (ret) { 14448caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 14458caab75fSGeert Uytterhoeven ret); 14462841a5fcSMark Brown } 14472841a5fcSMark Brown } 1448391949b6SUwe Kleine-König 14498caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 14508caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 14518caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = false; 14528caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 14538caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 14548e76ef88SMartin Sperl 14558e76ef88SMartin Sperl trace_spi_message_done(mesg); 14562841a5fcSMark Brown 1457ffbbdd21SLinus Walleij mesg->state = NULL; 1458ffbbdd21SLinus Walleij if (mesg->complete) 1459ffbbdd21SLinus Walleij mesg->complete(mesg->context); 1460ffbbdd21SLinus Walleij } 1461ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 1462ffbbdd21SLinus Walleij 14638caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr) 1464ffbbdd21SLinus Walleij { 1465ffbbdd21SLinus Walleij unsigned long flags; 1466ffbbdd21SLinus Walleij 14678caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1468ffbbdd21SLinus Walleij 14698caab75fSGeert Uytterhoeven if (ctlr->running || ctlr->busy) { 14708caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1471ffbbdd21SLinus Walleij return -EBUSY; 1472ffbbdd21SLinus Walleij } 1473ffbbdd21SLinus Walleij 14748caab75fSGeert Uytterhoeven ctlr->running = true; 14758caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 14768caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1477ffbbdd21SLinus Walleij 14788caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 1479ffbbdd21SLinus Walleij 1480ffbbdd21SLinus Walleij return 0; 1481ffbbdd21SLinus Walleij } 1482ffbbdd21SLinus Walleij 14838caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr) 1484ffbbdd21SLinus Walleij { 1485ffbbdd21SLinus Walleij unsigned long flags; 1486ffbbdd21SLinus Walleij unsigned limit = 500; 1487ffbbdd21SLinus Walleij int ret = 0; 1488ffbbdd21SLinus Walleij 14898caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1490ffbbdd21SLinus Walleij 1491ffbbdd21SLinus Walleij /* 1492ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 14938caab75fSGeert Uytterhoeven * A wait_queue on the ctlr->busy could be used, but then the common 1494ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 1495ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 1496ffbbdd21SLinus Walleij */ 14978caab75fSGeert Uytterhoeven while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 14988caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1499f97b26b0SAxel Lin usleep_range(10000, 11000); 15008caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1501ffbbdd21SLinus Walleij } 1502ffbbdd21SLinus Walleij 15038caab75fSGeert Uytterhoeven if (!list_empty(&ctlr->queue) || ctlr->busy) 1504ffbbdd21SLinus Walleij ret = -EBUSY; 1505ffbbdd21SLinus Walleij else 15068caab75fSGeert Uytterhoeven ctlr->running = false; 1507ffbbdd21SLinus Walleij 15088caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1509ffbbdd21SLinus Walleij 1510ffbbdd21SLinus Walleij if (ret) { 15118caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "could not stop message queue\n"); 1512ffbbdd21SLinus Walleij return ret; 1513ffbbdd21SLinus Walleij } 1514ffbbdd21SLinus Walleij return ret; 1515ffbbdd21SLinus Walleij } 1516ffbbdd21SLinus Walleij 15178caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr) 1518ffbbdd21SLinus Walleij { 1519ffbbdd21SLinus Walleij int ret; 1520ffbbdd21SLinus Walleij 15218caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 1522ffbbdd21SLinus Walleij 1523ffbbdd21SLinus Walleij /* 15243989144fSPetr Mladek * kthread_flush_worker will block until all work is done. 1525ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 1526ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 1527ffbbdd21SLinus Walleij * return anyway. 1528ffbbdd21SLinus Walleij */ 1529ffbbdd21SLinus Walleij if (ret) { 15308caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem destroying queue\n"); 1531ffbbdd21SLinus Walleij return ret; 1532ffbbdd21SLinus Walleij } 1533ffbbdd21SLinus Walleij 15348caab75fSGeert Uytterhoeven kthread_flush_worker(&ctlr->kworker); 15358caab75fSGeert Uytterhoeven kthread_stop(ctlr->kworker_task); 1536ffbbdd21SLinus Walleij 1537ffbbdd21SLinus Walleij return 0; 1538ffbbdd21SLinus Walleij } 1539ffbbdd21SLinus Walleij 15400461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi, 15410461a414SMark Brown struct spi_message *msg, 15420461a414SMark Brown bool need_pump) 1543ffbbdd21SLinus Walleij { 15448caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 1545ffbbdd21SLinus Walleij unsigned long flags; 1546ffbbdd21SLinus Walleij 15478caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1548ffbbdd21SLinus Walleij 15498caab75fSGeert Uytterhoeven if (!ctlr->running) { 15508caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1551ffbbdd21SLinus Walleij return -ESHUTDOWN; 1552ffbbdd21SLinus Walleij } 1553ffbbdd21SLinus Walleij msg->actual_length = 0; 1554ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 1555ffbbdd21SLinus Walleij 15568caab75fSGeert Uytterhoeven list_add_tail(&msg->queue, &ctlr->queue); 1557f0125f1aSMark Brown if (!ctlr->busy && need_pump) 15588caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 1559ffbbdd21SLinus Walleij 15608caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1561ffbbdd21SLinus Walleij return 0; 1562ffbbdd21SLinus Walleij } 1563ffbbdd21SLinus Walleij 15640461a414SMark Brown /** 15650461a414SMark Brown * spi_queued_transfer - transfer function for queued transfers 15660461a414SMark Brown * @spi: spi device which is requesting transfer 15670461a414SMark Brown * @msg: spi message which is to handled is queued to driver queue 156897d56dc6SJavier Martinez Canillas * 156997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 15700461a414SMark Brown */ 15710461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 15720461a414SMark Brown { 15730461a414SMark Brown return __spi_queued_transfer(spi, msg, true); 15740461a414SMark Brown } 15750461a414SMark Brown 15768caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr) 1577ffbbdd21SLinus Walleij { 1578ffbbdd21SLinus Walleij int ret; 1579ffbbdd21SLinus Walleij 15808caab75fSGeert Uytterhoeven ctlr->transfer = spi_queued_transfer; 15818caab75fSGeert Uytterhoeven if (!ctlr->transfer_one_message) 15828caab75fSGeert Uytterhoeven ctlr->transfer_one_message = spi_transfer_one_message; 1583ffbbdd21SLinus Walleij 1584ffbbdd21SLinus Walleij /* Initialize and start queue */ 15858caab75fSGeert Uytterhoeven ret = spi_init_queue(ctlr); 1586ffbbdd21SLinus Walleij if (ret) { 15878caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem initializing queue\n"); 1588ffbbdd21SLinus Walleij goto err_init_queue; 1589ffbbdd21SLinus Walleij } 15908caab75fSGeert Uytterhoeven ctlr->queued = true; 15918caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 1592ffbbdd21SLinus Walleij if (ret) { 15938caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem starting queue\n"); 1594ffbbdd21SLinus Walleij goto err_start_queue; 1595ffbbdd21SLinus Walleij } 1596ffbbdd21SLinus Walleij 1597ffbbdd21SLinus Walleij return 0; 1598ffbbdd21SLinus Walleij 1599ffbbdd21SLinus Walleij err_start_queue: 16008caab75fSGeert Uytterhoeven spi_destroy_queue(ctlr); 1601c3676d5cSMark Brown err_init_queue: 1602ffbbdd21SLinus Walleij return ret; 1603ffbbdd21SLinus Walleij } 1604ffbbdd21SLinus Walleij 1605988f259bSBoris Brezillon /** 1606988f259bSBoris Brezillon * spi_flush_queue - Send all pending messages in the queue from the callers' 1607988f259bSBoris Brezillon * context 1608988f259bSBoris Brezillon * @ctlr: controller to process queue for 1609988f259bSBoris Brezillon * 1610988f259bSBoris Brezillon * This should be used when one wants to ensure all pending messages have been 1611988f259bSBoris Brezillon * sent before doing something. Is used by the spi-mem code to make sure SPI 1612988f259bSBoris Brezillon * memory operations do not preempt regular SPI transfers that have been queued 1613988f259bSBoris Brezillon * before the spi-mem operation. 1614988f259bSBoris Brezillon */ 1615988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr) 1616988f259bSBoris Brezillon { 1617988f259bSBoris Brezillon if (ctlr->transfer == spi_queued_transfer) 1618988f259bSBoris Brezillon __spi_pump_messages(ctlr, false); 1619988f259bSBoris Brezillon } 1620988f259bSBoris Brezillon 1621ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 1622ffbbdd21SLinus Walleij 16237cb94361SAndreas Larsson #if defined(CONFIG_OF) 16248caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 1625c2e51ac3SGeert Uytterhoeven struct device_node *nc) 1626d57a4282SGrant Likely { 162789da4293STrent Piepho u32 value; 1628c2e51ac3SGeert Uytterhoeven int rc; 1629d57a4282SGrant Likely 1630d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 1631e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpha")) 1632d57a4282SGrant Likely spi->mode |= SPI_CPHA; 1633e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpol")) 1634d57a4282SGrant Likely spi->mode |= SPI_CPOL; 1635e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-3wire")) 1636c20151dfSLars-Peter Clausen spi->mode |= SPI_3WIRE; 1637e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-lsb-first")) 1638cd6339e6SZhao Qiang spi->mode |= SPI_LSB_FIRST; 1639d57a4282SGrant Likely 1640f3186dd8SLinus Walleij /* 1641f3186dd8SLinus Walleij * For descriptors associated with the device, polarity inversion is 1642f3186dd8SLinus Walleij * handled in the gpiolib, so all chip selects are "active high" in 1643f3186dd8SLinus Walleij * the logical sense, the gpiolib will invert the line if need be. 1644f3186dd8SLinus Walleij */ 1645f3186dd8SLinus Walleij if (ctlr->use_gpio_descriptors) 1646f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 1647f3186dd8SLinus Walleij else if (of_property_read_bool(nc, "spi-cs-high")) 1648f3186dd8SLinus Walleij spi->mode |= SPI_CS_HIGH; 1649f3186dd8SLinus Walleij 1650f477b7fbSwangyuhang /* Device DUAL/QUAD mode */ 165189da4293STrent Piepho if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 165289da4293STrent Piepho switch (value) { 165389da4293STrent Piepho case 1: 1654f477b7fbSwangyuhang break; 165589da4293STrent Piepho case 2: 1656f477b7fbSwangyuhang spi->mode |= SPI_TX_DUAL; 1657f477b7fbSwangyuhang break; 165889da4293STrent Piepho case 4: 1659f477b7fbSwangyuhang spi->mode |= SPI_TX_QUAD; 1660f477b7fbSwangyuhang break; 16616b03061fSYogesh Narayan Gaur case 8: 16626b03061fSYogesh Narayan Gaur spi->mode |= SPI_TX_OCTAL; 16636b03061fSYogesh Narayan Gaur break; 1664f477b7fbSwangyuhang default: 16658caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 1666a110f93dSwangyuhang "spi-tx-bus-width %d not supported\n", 166789da4293STrent Piepho value); 166880874d8cSGeert Uytterhoeven break; 1669f477b7fbSwangyuhang } 1670a822e99cSMark Brown } 1671f477b7fbSwangyuhang 167289da4293STrent Piepho if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 167389da4293STrent Piepho switch (value) { 167489da4293STrent Piepho case 1: 1675f477b7fbSwangyuhang break; 167689da4293STrent Piepho case 2: 1677f477b7fbSwangyuhang spi->mode |= SPI_RX_DUAL; 1678f477b7fbSwangyuhang break; 167989da4293STrent Piepho case 4: 1680f477b7fbSwangyuhang spi->mode |= SPI_RX_QUAD; 1681f477b7fbSwangyuhang break; 16826b03061fSYogesh Narayan Gaur case 8: 16836b03061fSYogesh Narayan Gaur spi->mode |= SPI_RX_OCTAL; 16846b03061fSYogesh Narayan Gaur break; 1685f477b7fbSwangyuhang default: 16868caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 1687a110f93dSwangyuhang "spi-rx-bus-width %d not supported\n", 168889da4293STrent Piepho value); 168980874d8cSGeert Uytterhoeven break; 1690f477b7fbSwangyuhang } 1691a822e99cSMark Brown } 1692f477b7fbSwangyuhang 16938caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr)) { 1694194276b0SRob Herring if (!of_node_name_eq(nc, "slave")) { 169525c56c88SRob Herring dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 169625c56c88SRob Herring nc); 16976c364062SGeert Uytterhoeven return -EINVAL; 16986c364062SGeert Uytterhoeven } 16996c364062SGeert Uytterhoeven return 0; 17006c364062SGeert Uytterhoeven } 17016c364062SGeert Uytterhoeven 17026c364062SGeert Uytterhoeven /* Device address */ 17036c364062SGeert Uytterhoeven rc = of_property_read_u32(nc, "reg", &value); 17046c364062SGeert Uytterhoeven if (rc) { 170525c56c88SRob Herring dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 170625c56c88SRob Herring nc, rc); 17076c364062SGeert Uytterhoeven return rc; 17086c364062SGeert Uytterhoeven } 17096c364062SGeert Uytterhoeven spi->chip_select = value; 17106c364062SGeert Uytterhoeven 1711d57a4282SGrant Likely /* Device speed */ 171289da4293STrent Piepho rc = of_property_read_u32(nc, "spi-max-frequency", &value); 171389da4293STrent Piepho if (rc) { 17148caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 171525c56c88SRob Herring "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc); 1716c2e51ac3SGeert Uytterhoeven return rc; 1717d57a4282SGrant Likely } 171889da4293STrent Piepho spi->max_speed_hz = value; 1719d57a4282SGrant Likely 1720c2e51ac3SGeert Uytterhoeven return 0; 1721c2e51ac3SGeert Uytterhoeven } 1722c2e51ac3SGeert Uytterhoeven 1723c2e51ac3SGeert Uytterhoeven static struct spi_device * 17248caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 1725c2e51ac3SGeert Uytterhoeven { 1726c2e51ac3SGeert Uytterhoeven struct spi_device *spi; 1727c2e51ac3SGeert Uytterhoeven int rc; 1728c2e51ac3SGeert Uytterhoeven 1729c2e51ac3SGeert Uytterhoeven /* Alloc an spi_device */ 17308caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 1731c2e51ac3SGeert Uytterhoeven if (!spi) { 173225c56c88SRob Herring dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 1733c2e51ac3SGeert Uytterhoeven rc = -ENOMEM; 1734c2e51ac3SGeert Uytterhoeven goto err_out; 1735c2e51ac3SGeert Uytterhoeven } 1736c2e51ac3SGeert Uytterhoeven 1737c2e51ac3SGeert Uytterhoeven /* Select device driver */ 1738c2e51ac3SGeert Uytterhoeven rc = of_modalias_node(nc, spi->modalias, 1739c2e51ac3SGeert Uytterhoeven sizeof(spi->modalias)); 1740c2e51ac3SGeert Uytterhoeven if (rc < 0) { 174125c56c88SRob Herring dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 1742c2e51ac3SGeert Uytterhoeven goto err_out; 1743c2e51ac3SGeert Uytterhoeven } 1744c2e51ac3SGeert Uytterhoeven 17458caab75fSGeert Uytterhoeven rc = of_spi_parse_dt(ctlr, spi, nc); 1746c2e51ac3SGeert Uytterhoeven if (rc) 1747c2e51ac3SGeert Uytterhoeven goto err_out; 1748c2e51ac3SGeert Uytterhoeven 1749d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 1750d57a4282SGrant Likely of_node_get(nc); 1751d57a4282SGrant Likely spi->dev.of_node = nc; 1752d57a4282SGrant Likely 1753d57a4282SGrant Likely /* Register the new device */ 1754d57a4282SGrant Likely rc = spi_add_device(spi); 1755d57a4282SGrant Likely if (rc) { 175625c56c88SRob Herring dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 17578324147fSJohan Hovold goto err_of_node_put; 1758d57a4282SGrant Likely } 1759d57a4282SGrant Likely 1760aff5e3f8SPantelis Antoniou return spi; 1761aff5e3f8SPantelis Antoniou 17628324147fSJohan Hovold err_of_node_put: 17638324147fSJohan Hovold of_node_put(nc); 1764aff5e3f8SPantelis Antoniou err_out: 1765aff5e3f8SPantelis Antoniou spi_dev_put(spi); 1766aff5e3f8SPantelis Antoniou return ERR_PTR(rc); 1767aff5e3f8SPantelis Antoniou } 1768aff5e3f8SPantelis Antoniou 1769aff5e3f8SPantelis Antoniou /** 1770aff5e3f8SPantelis Antoniou * of_register_spi_devices() - Register child devices onto the SPI bus 17718caab75fSGeert Uytterhoeven * @ctlr: Pointer to spi_controller device 1772aff5e3f8SPantelis Antoniou * 17736c364062SGeert Uytterhoeven * Registers an spi_device for each child node of controller node which 17746c364062SGeert Uytterhoeven * represents a valid SPI slave. 1775aff5e3f8SPantelis Antoniou */ 17768caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) 1777aff5e3f8SPantelis Antoniou { 1778aff5e3f8SPantelis Antoniou struct spi_device *spi; 1779aff5e3f8SPantelis Antoniou struct device_node *nc; 1780aff5e3f8SPantelis Antoniou 17818caab75fSGeert Uytterhoeven if (!ctlr->dev.of_node) 1782aff5e3f8SPantelis Antoniou return; 1783aff5e3f8SPantelis Antoniou 17848caab75fSGeert Uytterhoeven for_each_available_child_of_node(ctlr->dev.of_node, nc) { 1785bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(nc, OF_POPULATED)) 1786bd6c1644SGeert Uytterhoeven continue; 17878caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, nc); 1788e0af98a7SRalf Ramsauer if (IS_ERR(spi)) { 17898caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 179025c56c88SRob Herring "Failed to create SPI device for %pOF\n", nc); 1791e0af98a7SRalf Ramsauer of_node_clear_flag(nc, OF_POPULATED); 1792e0af98a7SRalf Ramsauer } 1793d57a4282SGrant Likely } 1794d57a4282SGrant Likely } 1795d57a4282SGrant Likely #else 17968caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { } 1797d57a4282SGrant Likely #endif 1798d57a4282SGrant Likely 179964bee4d2SMika Westerberg #ifdef CONFIG_ACPI 18008a2e487eSLukas Wunner static void acpi_spi_parse_apple_properties(struct spi_device *spi) 18018a2e487eSLukas Wunner { 18028a2e487eSLukas Wunner struct acpi_device *dev = ACPI_COMPANION(&spi->dev); 18038a2e487eSLukas Wunner const union acpi_object *obj; 18048a2e487eSLukas Wunner 18058a2e487eSLukas Wunner if (!x86_apple_machine) 18068a2e487eSLukas Wunner return; 18078a2e487eSLukas Wunner 18088a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 18098a2e487eSLukas Wunner && obj->buffer.length >= 4) 18108a2e487eSLukas Wunner spi->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 18118a2e487eSLukas Wunner 18128a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 18138a2e487eSLukas Wunner && obj->buffer.length == 8) 18148a2e487eSLukas Wunner spi->bits_per_word = *(u64 *)obj->buffer.pointer; 18158a2e487eSLukas Wunner 18168a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 18178a2e487eSLukas Wunner && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 18188a2e487eSLukas Wunner spi->mode |= SPI_LSB_FIRST; 18198a2e487eSLukas Wunner 18208a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 18218a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 18228a2e487eSLukas Wunner spi->mode |= SPI_CPOL; 18238a2e487eSLukas Wunner 18248a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 18258a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 18268a2e487eSLukas Wunner spi->mode |= SPI_CPHA; 18278a2e487eSLukas Wunner } 18288a2e487eSLukas Wunner 182964bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 183064bee4d2SMika Westerberg { 183164bee4d2SMika Westerberg struct spi_device *spi = data; 18328caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 183364bee4d2SMika Westerberg 183464bee4d2SMika Westerberg if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 183564bee4d2SMika Westerberg struct acpi_resource_spi_serialbus *sb; 183664bee4d2SMika Westerberg 183764bee4d2SMika Westerberg sb = &ares->data.spi_serial_bus; 183864bee4d2SMika Westerberg if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 1839a0a90718SMika Westerberg /* 1840a0a90718SMika Westerberg * ACPI DeviceSelection numbering is handled by the 1841a0a90718SMika Westerberg * host controller driver in Windows and can vary 1842a0a90718SMika Westerberg * from driver to driver. In Linux we always expect 1843a0a90718SMika Westerberg * 0 .. max - 1 so we need to ask the driver to 1844a0a90718SMika Westerberg * translate between the two schemes. 1845a0a90718SMika Westerberg */ 18468caab75fSGeert Uytterhoeven if (ctlr->fw_translate_cs) { 18478caab75fSGeert Uytterhoeven int cs = ctlr->fw_translate_cs(ctlr, 1848a0a90718SMika Westerberg sb->device_selection); 1849a0a90718SMika Westerberg if (cs < 0) 1850a0a90718SMika Westerberg return cs; 1851a0a90718SMika Westerberg spi->chip_select = cs; 1852a0a90718SMika Westerberg } else { 185364bee4d2SMika Westerberg spi->chip_select = sb->device_selection; 1854a0a90718SMika Westerberg } 1855a0a90718SMika Westerberg 185664bee4d2SMika Westerberg spi->max_speed_hz = sb->connection_speed; 185764bee4d2SMika Westerberg 185864bee4d2SMika Westerberg if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 185964bee4d2SMika Westerberg spi->mode |= SPI_CPHA; 186064bee4d2SMika Westerberg if (sb->clock_polarity == ACPI_SPI_START_HIGH) 186164bee4d2SMika Westerberg spi->mode |= SPI_CPOL; 186264bee4d2SMika Westerberg if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 186364bee4d2SMika Westerberg spi->mode |= SPI_CS_HIGH; 186464bee4d2SMika Westerberg } 186564bee4d2SMika Westerberg } else if (spi->irq < 0) { 186664bee4d2SMika Westerberg struct resource r; 186764bee4d2SMika Westerberg 186864bee4d2SMika Westerberg if (acpi_dev_resource_interrupt(ares, 0, &r)) 186964bee4d2SMika Westerberg spi->irq = r.start; 187064bee4d2SMika Westerberg } 187164bee4d2SMika Westerberg 187264bee4d2SMika Westerberg /* Always tell the ACPI core to skip this resource */ 187364bee4d2SMika Westerberg return 1; 187464bee4d2SMika Westerberg } 187564bee4d2SMika Westerberg 18768caab75fSGeert Uytterhoeven static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 18777f24467fSOctavian Purdila struct acpi_device *adev) 187864bee4d2SMika Westerberg { 187964bee4d2SMika Westerberg struct list_head resource_list; 188064bee4d2SMika Westerberg struct spi_device *spi; 188164bee4d2SMika Westerberg int ret; 188264bee4d2SMika Westerberg 18837f24467fSOctavian Purdila if (acpi_bus_get_status(adev) || !adev->status.present || 18847f24467fSOctavian Purdila acpi_device_enumerated(adev)) 188564bee4d2SMika Westerberg return AE_OK; 188664bee4d2SMika Westerberg 18878caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 188864bee4d2SMika Westerberg if (!spi) { 18898caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", 189064bee4d2SMika Westerberg dev_name(&adev->dev)); 189164bee4d2SMika Westerberg return AE_NO_MEMORY; 189264bee4d2SMika Westerberg } 189364bee4d2SMika Westerberg 18947b199811SRafael J. Wysocki ACPI_COMPANION_SET(&spi->dev, adev); 189564bee4d2SMika Westerberg spi->irq = -1; 189664bee4d2SMika Westerberg 189764bee4d2SMika Westerberg INIT_LIST_HEAD(&resource_list); 189864bee4d2SMika Westerberg ret = acpi_dev_get_resources(adev, &resource_list, 189964bee4d2SMika Westerberg acpi_spi_add_resource, spi); 190064bee4d2SMika Westerberg acpi_dev_free_resource_list(&resource_list); 190164bee4d2SMika Westerberg 19028a2e487eSLukas Wunner acpi_spi_parse_apple_properties(spi); 19038a2e487eSLukas Wunner 190464bee4d2SMika Westerberg if (ret < 0 || !spi->max_speed_hz) { 190564bee4d2SMika Westerberg spi_dev_put(spi); 190664bee4d2SMika Westerberg return AE_OK; 190764bee4d2SMika Westerberg } 190864bee4d2SMika Westerberg 19090c6543f6SDan O'Donovan acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 19100c6543f6SDan O'Donovan sizeof(spi->modalias)); 19110c6543f6SDan O'Donovan 191233ada67dSChristophe RICARD if (spi->irq < 0) 191333ada67dSChristophe RICARD spi->irq = acpi_dev_gpio_irq_get(adev, 0); 191433ada67dSChristophe RICARD 19157f24467fSOctavian Purdila acpi_device_set_enumerated(adev); 19167f24467fSOctavian Purdila 191733cf00e5SMika Westerberg adev->power.flags.ignore_parent = true; 191864bee4d2SMika Westerberg if (spi_add_device(spi)) { 191933cf00e5SMika Westerberg adev->power.flags.ignore_parent = false; 19208caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 192164bee4d2SMika Westerberg dev_name(&adev->dev)); 192264bee4d2SMika Westerberg spi_dev_put(spi); 192364bee4d2SMika Westerberg } 192464bee4d2SMika Westerberg 192564bee4d2SMika Westerberg return AE_OK; 192664bee4d2SMika Westerberg } 192764bee4d2SMika Westerberg 19287f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 19297f24467fSOctavian Purdila void *data, void **return_value) 19307f24467fSOctavian Purdila { 19318caab75fSGeert Uytterhoeven struct spi_controller *ctlr = data; 19327f24467fSOctavian Purdila struct acpi_device *adev; 19337f24467fSOctavian Purdila 19347f24467fSOctavian Purdila if (acpi_bus_get_device(handle, &adev)) 19357f24467fSOctavian Purdila return AE_OK; 19367f24467fSOctavian Purdila 19378caab75fSGeert Uytterhoeven return acpi_register_spi_device(ctlr, adev); 19387f24467fSOctavian Purdila } 19397f24467fSOctavian Purdila 19408caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr) 194164bee4d2SMika Westerberg { 194264bee4d2SMika Westerberg acpi_status status; 194364bee4d2SMika Westerberg acpi_handle handle; 194464bee4d2SMika Westerberg 19458caab75fSGeert Uytterhoeven handle = ACPI_HANDLE(ctlr->dev.parent); 194664bee4d2SMika Westerberg if (!handle) 194764bee4d2SMika Westerberg return; 194864bee4d2SMika Westerberg 194964bee4d2SMika Westerberg status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, 19508caab75fSGeert Uytterhoeven acpi_spi_add_device, NULL, ctlr, NULL); 195164bee4d2SMika Westerberg if (ACPI_FAILURE(status)) 19528caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 195364bee4d2SMika Westerberg } 195464bee4d2SMika Westerberg #else 19558caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 195664bee4d2SMika Westerberg #endif /* CONFIG_ACPI */ 195764bee4d2SMika Westerberg 19588caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev) 19598ae12a0dSDavid Brownell { 19608caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 19618ae12a0dSDavid Brownell 19628caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 19638caab75fSGeert Uytterhoeven kfree(ctlr); 19648ae12a0dSDavid Brownell } 19658ae12a0dSDavid Brownell 19668ae12a0dSDavid Brownell static struct class spi_master_class = { 19678ae12a0dSDavid Brownell .name = "spi_master", 19688ae12a0dSDavid Brownell .owner = THIS_MODULE, 19698caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 1970eca2ebc7SMartin Sperl .dev_groups = spi_master_groups, 19718ae12a0dSDavid Brownell }; 19728ae12a0dSDavid Brownell 19736c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE 19746c364062SGeert Uytterhoeven /** 19756c364062SGeert Uytterhoeven * spi_slave_abort - abort the ongoing transfer request on an SPI slave 19766c364062SGeert Uytterhoeven * controller 19776c364062SGeert Uytterhoeven * @spi: device used for the current transfer 19786c364062SGeert Uytterhoeven */ 19796c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi) 19806c364062SGeert Uytterhoeven { 19818caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 19826c364062SGeert Uytterhoeven 19838caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 19848caab75fSGeert Uytterhoeven return ctlr->slave_abort(ctlr); 19856c364062SGeert Uytterhoeven 19866c364062SGeert Uytterhoeven return -ENOTSUPP; 19876c364062SGeert Uytterhoeven } 19886c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort); 19896c364062SGeert Uytterhoeven 19906c364062SGeert Uytterhoeven static int match_true(struct device *dev, void *data) 19916c364062SGeert Uytterhoeven { 19926c364062SGeert Uytterhoeven return 1; 19936c364062SGeert Uytterhoeven } 19946c364062SGeert Uytterhoeven 19956c364062SGeert Uytterhoeven static ssize_t spi_slave_show(struct device *dev, 19966c364062SGeert Uytterhoeven struct device_attribute *attr, char *buf) 19976c364062SGeert Uytterhoeven { 19988caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 19998caab75fSGeert Uytterhoeven dev); 20006c364062SGeert Uytterhoeven struct device *child; 20016c364062SGeert Uytterhoeven 20026c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 20036c364062SGeert Uytterhoeven return sprintf(buf, "%s\n", 20046c364062SGeert Uytterhoeven child ? to_spi_device(child)->modalias : NULL); 20056c364062SGeert Uytterhoeven } 20066c364062SGeert Uytterhoeven 20076c364062SGeert Uytterhoeven static ssize_t spi_slave_store(struct device *dev, 20086c364062SGeert Uytterhoeven struct device_attribute *attr, const char *buf, 20096c364062SGeert Uytterhoeven size_t count) 20106c364062SGeert Uytterhoeven { 20118caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 20128caab75fSGeert Uytterhoeven dev); 20136c364062SGeert Uytterhoeven struct spi_device *spi; 20146c364062SGeert Uytterhoeven struct device *child; 20156c364062SGeert Uytterhoeven char name[32]; 20166c364062SGeert Uytterhoeven int rc; 20176c364062SGeert Uytterhoeven 20186c364062SGeert Uytterhoeven rc = sscanf(buf, "%31s", name); 20196c364062SGeert Uytterhoeven if (rc != 1 || !name[0]) 20206c364062SGeert Uytterhoeven return -EINVAL; 20216c364062SGeert Uytterhoeven 20226c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 20236c364062SGeert Uytterhoeven if (child) { 20246c364062SGeert Uytterhoeven /* Remove registered slave */ 20256c364062SGeert Uytterhoeven device_unregister(child); 20266c364062SGeert Uytterhoeven put_device(child); 20276c364062SGeert Uytterhoeven } 20286c364062SGeert Uytterhoeven 20296c364062SGeert Uytterhoeven if (strcmp(name, "(null)")) { 20306c364062SGeert Uytterhoeven /* Register new slave */ 20316c364062SGeert Uytterhoeven spi = spi_alloc_device(ctlr); 20326c364062SGeert Uytterhoeven if (!spi) 20336c364062SGeert Uytterhoeven return -ENOMEM; 20346c364062SGeert Uytterhoeven 20356c364062SGeert Uytterhoeven strlcpy(spi->modalias, name, sizeof(spi->modalias)); 20366c364062SGeert Uytterhoeven 20376c364062SGeert Uytterhoeven rc = spi_add_device(spi); 20386c364062SGeert Uytterhoeven if (rc) { 20396c364062SGeert Uytterhoeven spi_dev_put(spi); 20406c364062SGeert Uytterhoeven return rc; 20416c364062SGeert Uytterhoeven } 20426c364062SGeert Uytterhoeven } 20436c364062SGeert Uytterhoeven 20446c364062SGeert Uytterhoeven return count; 20456c364062SGeert Uytterhoeven } 20466c364062SGeert Uytterhoeven 20476c364062SGeert Uytterhoeven static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store); 20486c364062SGeert Uytterhoeven 20496c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = { 20506c364062SGeert Uytterhoeven &dev_attr_slave.attr, 20516c364062SGeert Uytterhoeven NULL, 20526c364062SGeert Uytterhoeven }; 20536c364062SGeert Uytterhoeven 20546c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = { 20556c364062SGeert Uytterhoeven .attrs = spi_slave_attrs, 20566c364062SGeert Uytterhoeven }; 20576c364062SGeert Uytterhoeven 20586c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = { 20598caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 20606c364062SGeert Uytterhoeven &spi_slave_group, 20616c364062SGeert Uytterhoeven NULL, 20626c364062SGeert Uytterhoeven }; 20636c364062SGeert Uytterhoeven 20646c364062SGeert Uytterhoeven static struct class spi_slave_class = { 20656c364062SGeert Uytterhoeven .name = "spi_slave", 20666c364062SGeert Uytterhoeven .owner = THIS_MODULE, 20678caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 20686c364062SGeert Uytterhoeven .dev_groups = spi_slave_groups, 20696c364062SGeert Uytterhoeven }; 20706c364062SGeert Uytterhoeven #else 20716c364062SGeert Uytterhoeven extern struct class spi_slave_class; /* dummy */ 20726c364062SGeert Uytterhoeven #endif 20738ae12a0dSDavid Brownell 20748ae12a0dSDavid Brownell /** 20756c364062SGeert Uytterhoeven * __spi_alloc_controller - allocate an SPI master or slave controller 20768ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 207733e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 207849dce689STony Jones * memory is in the driver_data field of the returned device, 20798caab75fSGeert Uytterhoeven * accessible with spi_controller_get_devdata(). 20806c364062SGeert Uytterhoeven * @slave: flag indicating whether to allocate an SPI master (false) or SPI 20816c364062SGeert Uytterhoeven * slave (true) controller 208233e34dc6SDavid Brownell * Context: can sleep 20838ae12a0dSDavid Brownell * 20846c364062SGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 20858ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 20868caab75fSGeert Uytterhoeven * an spi_controller structure, prior to calling spi_register_controller(). 20878ae12a0dSDavid Brownell * 208897d56dc6SJavier Martinez Canillas * This must be called from context that can sleep. 20898ae12a0dSDavid Brownell * 20906c364062SGeert Uytterhoeven * The caller is responsible for assigning the bus number and initializing the 20918caab75fSGeert Uytterhoeven * controller's methods before calling spi_register_controller(); and (after 20928caab75fSGeert Uytterhoeven * errors adding the device) calling spi_controller_put() to prevent a memory 20938caab75fSGeert Uytterhoeven * leak. 209497d56dc6SJavier Martinez Canillas * 20956c364062SGeert Uytterhoeven * Return: the SPI controller structure on success, else NULL. 20968ae12a0dSDavid Brownell */ 20978caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev, 20986c364062SGeert Uytterhoeven unsigned int size, bool slave) 20998ae12a0dSDavid Brownell { 21008caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 21018ae12a0dSDavid Brownell 21020c868461SDavid Brownell if (!dev) 21030c868461SDavid Brownell return NULL; 21040c868461SDavid Brownell 21058caab75fSGeert Uytterhoeven ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL); 21068caab75fSGeert Uytterhoeven if (!ctlr) 21078ae12a0dSDavid Brownell return NULL; 21088ae12a0dSDavid Brownell 21098caab75fSGeert Uytterhoeven device_initialize(&ctlr->dev); 21108caab75fSGeert Uytterhoeven ctlr->bus_num = -1; 21118caab75fSGeert Uytterhoeven ctlr->num_chipselect = 1; 21128caab75fSGeert Uytterhoeven ctlr->slave = slave; 21136c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 21148caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_slave_class; 21156c364062SGeert Uytterhoeven else 21168caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_master_class; 21178caab75fSGeert Uytterhoeven ctlr->dev.parent = dev; 21188caab75fSGeert Uytterhoeven pm_suspend_ignore_children(&ctlr->dev, true); 21198caab75fSGeert Uytterhoeven spi_controller_set_devdata(ctlr, &ctlr[1]); 21208ae12a0dSDavid Brownell 21218caab75fSGeert Uytterhoeven return ctlr; 21228ae12a0dSDavid Brownell } 21236c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller); 21248ae12a0dSDavid Brownell 212574317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF 21268caab75fSGeert Uytterhoeven static int of_spi_register_master(struct spi_controller *ctlr) 212774317984SJean-Christophe PLAGNIOL-VILLARD { 2128e80beb27SGrant Likely int nb, i, *cs; 21298caab75fSGeert Uytterhoeven struct device_node *np = ctlr->dev.of_node; 213074317984SJean-Christophe PLAGNIOL-VILLARD 213174317984SJean-Christophe PLAGNIOL-VILLARD if (!np) 213274317984SJean-Christophe PLAGNIOL-VILLARD return 0; 213374317984SJean-Christophe PLAGNIOL-VILLARD 213474317984SJean-Christophe PLAGNIOL-VILLARD nb = of_gpio_named_count(np, "cs-gpios"); 21358caab75fSGeert Uytterhoeven ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 213674317984SJean-Christophe PLAGNIOL-VILLARD 21378ec5d84eSAndreas Larsson /* Return error only for an incorrectly formed cs-gpios property */ 21388ec5d84eSAndreas Larsson if (nb == 0 || nb == -ENOENT) 213974317984SJean-Christophe PLAGNIOL-VILLARD return 0; 21408ec5d84eSAndreas Larsson else if (nb < 0) 21418ec5d84eSAndreas Larsson return nb; 214274317984SJean-Christophe PLAGNIOL-VILLARD 2143a86854d0SKees Cook cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int), 214474317984SJean-Christophe PLAGNIOL-VILLARD GFP_KERNEL); 21458caab75fSGeert Uytterhoeven ctlr->cs_gpios = cs; 214674317984SJean-Christophe PLAGNIOL-VILLARD 21478caab75fSGeert Uytterhoeven if (!ctlr->cs_gpios) 214874317984SJean-Christophe PLAGNIOL-VILLARD return -ENOMEM; 214974317984SJean-Christophe PLAGNIOL-VILLARD 21508caab75fSGeert Uytterhoeven for (i = 0; i < ctlr->num_chipselect; i++) 2151446411e1SAndreas Larsson cs[i] = -ENOENT; 215274317984SJean-Christophe PLAGNIOL-VILLARD 215374317984SJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < nb; i++) 215474317984SJean-Christophe PLAGNIOL-VILLARD cs[i] = of_get_named_gpio(np, "cs-gpios", i); 215574317984SJean-Christophe PLAGNIOL-VILLARD 215674317984SJean-Christophe PLAGNIOL-VILLARD return 0; 215774317984SJean-Christophe PLAGNIOL-VILLARD } 215874317984SJean-Christophe PLAGNIOL-VILLARD #else 21598caab75fSGeert Uytterhoeven static int of_spi_register_master(struct spi_controller *ctlr) 216074317984SJean-Christophe PLAGNIOL-VILLARD { 216174317984SJean-Christophe PLAGNIOL-VILLARD return 0; 216274317984SJean-Christophe PLAGNIOL-VILLARD } 216374317984SJean-Christophe PLAGNIOL-VILLARD #endif 216474317984SJean-Christophe PLAGNIOL-VILLARD 2165f3186dd8SLinus Walleij /** 2166f3186dd8SLinus Walleij * spi_get_gpio_descs() - grab chip select GPIOs for the master 2167f3186dd8SLinus Walleij * @ctlr: The SPI master to grab GPIO descriptors for 2168f3186dd8SLinus Walleij */ 2169f3186dd8SLinus Walleij static int spi_get_gpio_descs(struct spi_controller *ctlr) 2170f3186dd8SLinus Walleij { 2171f3186dd8SLinus Walleij int nb, i; 2172f3186dd8SLinus Walleij struct gpio_desc **cs; 2173f3186dd8SLinus Walleij struct device *dev = &ctlr->dev; 2174f3186dd8SLinus Walleij 2175f3186dd8SLinus Walleij nb = gpiod_count(dev, "cs"); 2176f3186dd8SLinus Walleij ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 2177f3186dd8SLinus Walleij 2178f3186dd8SLinus Walleij /* No GPIOs at all is fine, else return the error */ 2179f3186dd8SLinus Walleij if (nb == 0 || nb == -ENOENT) 2180f3186dd8SLinus Walleij return 0; 2181f3186dd8SLinus Walleij else if (nb < 0) 2182f3186dd8SLinus Walleij return nb; 2183f3186dd8SLinus Walleij 2184f3186dd8SLinus Walleij cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs), 2185f3186dd8SLinus Walleij GFP_KERNEL); 2186f3186dd8SLinus Walleij if (!cs) 2187f3186dd8SLinus Walleij return -ENOMEM; 2188f3186dd8SLinus Walleij ctlr->cs_gpiods = cs; 2189f3186dd8SLinus Walleij 2190f3186dd8SLinus Walleij for (i = 0; i < nb; i++) { 2191f3186dd8SLinus Walleij /* 2192f3186dd8SLinus Walleij * Most chipselects are active low, the inverted 2193f3186dd8SLinus Walleij * semantics are handled by special quirks in gpiolib, 2194f3186dd8SLinus Walleij * so initializing them GPIOD_OUT_LOW here means 2195f3186dd8SLinus Walleij * "unasserted", in most cases this will drive the physical 2196f3186dd8SLinus Walleij * line high. 2197f3186dd8SLinus Walleij */ 2198f3186dd8SLinus Walleij cs[i] = devm_gpiod_get_index_optional(dev, "cs", i, 2199f3186dd8SLinus Walleij GPIOD_OUT_LOW); 2200*1723fdecSGeert Uytterhoeven if (IS_ERR(cs[i])) 2201*1723fdecSGeert Uytterhoeven return PTR_ERR(cs[i]); 2202f3186dd8SLinus Walleij 2203f3186dd8SLinus Walleij if (cs[i]) { 2204f3186dd8SLinus Walleij /* 2205f3186dd8SLinus Walleij * If we find a CS GPIO, name it after the device and 2206f3186dd8SLinus Walleij * chip select line. 2207f3186dd8SLinus Walleij */ 2208f3186dd8SLinus Walleij char *gpioname; 2209f3186dd8SLinus Walleij 2210f3186dd8SLinus Walleij gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d", 2211f3186dd8SLinus Walleij dev_name(dev), i); 2212f3186dd8SLinus Walleij if (!gpioname) 2213f3186dd8SLinus Walleij return -ENOMEM; 2214f3186dd8SLinus Walleij gpiod_set_consumer_name(cs[i], gpioname); 2215f3186dd8SLinus Walleij } 2216f3186dd8SLinus Walleij } 2217f3186dd8SLinus Walleij 2218f3186dd8SLinus Walleij return 0; 2219f3186dd8SLinus Walleij } 2220f3186dd8SLinus Walleij 2221bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr) 2222bdf3a3b5SBoris Brezillon { 2223bdf3a3b5SBoris Brezillon /* 2224b5932f5cSBoris Brezillon * The controller may implement only the high-level SPI-memory like 2225b5932f5cSBoris Brezillon * operations if it does not support regular SPI transfers, and this is 2226b5932f5cSBoris Brezillon * valid use case. 2227b5932f5cSBoris Brezillon * If ->mem_ops is NULL, we request that at least one of the 2228b5932f5cSBoris Brezillon * ->transfer_xxx() method be implemented. 2229bdf3a3b5SBoris Brezillon */ 2230b5932f5cSBoris Brezillon if (ctlr->mem_ops) { 2231b5932f5cSBoris Brezillon if (!ctlr->mem_ops->exec_op) 2232bdf3a3b5SBoris Brezillon return -EINVAL; 2233b5932f5cSBoris Brezillon } else if (!ctlr->transfer && !ctlr->transfer_one && 2234b5932f5cSBoris Brezillon !ctlr->transfer_one_message) { 2235b5932f5cSBoris Brezillon return -EINVAL; 2236b5932f5cSBoris Brezillon } 2237bdf3a3b5SBoris Brezillon 2238bdf3a3b5SBoris Brezillon return 0; 2239bdf3a3b5SBoris Brezillon } 2240bdf3a3b5SBoris Brezillon 22418ae12a0dSDavid Brownell /** 22428caab75fSGeert Uytterhoeven * spi_register_controller - register SPI master or slave controller 22438caab75fSGeert Uytterhoeven * @ctlr: initialized master, originally from spi_alloc_master() or 22448caab75fSGeert Uytterhoeven * spi_alloc_slave() 224533e34dc6SDavid Brownell * Context: can sleep 22468ae12a0dSDavid Brownell * 22478caab75fSGeert Uytterhoeven * SPI controllers connect to their drivers using some non-SPI bus, 22488ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 22498caab75fSGeert Uytterhoeven * includes calling spi_register_controller() to hook up to this SPI bus glue. 22508ae12a0dSDavid Brownell * 22518ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 22528ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 22538ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 22548ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 22558ae12a0dSDavid Brownell * chip is at which address. 22568ae12a0dSDavid Brownell * 22578ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 22588caab75fSGeert Uytterhoeven * success, else a negative error code (dropping the controller's refcount). 22590c868461SDavid Brownell * After a successful return, the caller is responsible for calling 22608caab75fSGeert Uytterhoeven * spi_unregister_controller(). 226197d56dc6SJavier Martinez Canillas * 226297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 22638ae12a0dSDavid Brownell */ 22648caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr) 22658ae12a0dSDavid Brownell { 22668caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 22672b9603a0SFeng Tang struct boardinfo *bi; 22688ae12a0dSDavid Brownell int status = -ENODEV; 226942bdd706SLucas Stach int id, first_dynamic; 22708ae12a0dSDavid Brownell 22710c868461SDavid Brownell if (!dev) 22720c868461SDavid Brownell return -ENODEV; 22730c868461SDavid Brownell 2274bdf3a3b5SBoris Brezillon /* 2275bdf3a3b5SBoris Brezillon * Make sure all necessary hooks are implemented before registering 2276bdf3a3b5SBoris Brezillon * the SPI controller. 2277bdf3a3b5SBoris Brezillon */ 2278bdf3a3b5SBoris Brezillon status = spi_controller_check_ops(ctlr); 2279bdf3a3b5SBoris Brezillon if (status) 2280bdf3a3b5SBoris Brezillon return status; 2281bdf3a3b5SBoris Brezillon 22828caab75fSGeert Uytterhoeven if (!spi_controller_is_slave(ctlr)) { 2283f3186dd8SLinus Walleij if (ctlr->use_gpio_descriptors) { 2284f3186dd8SLinus Walleij status = spi_get_gpio_descs(ctlr); 2285f3186dd8SLinus Walleij if (status) 2286f3186dd8SLinus Walleij return status; 22872df201e0SLinus Walleij /* 22882df201e0SLinus Walleij * A controller using GPIO descriptors always 22892df201e0SLinus Walleij * supports SPI_CS_HIGH if need be. 22902df201e0SLinus Walleij */ 22912df201e0SLinus Walleij ctlr->mode_bits |= SPI_CS_HIGH; 2292f3186dd8SLinus Walleij } else { 2293f3186dd8SLinus Walleij /* Legacy code path for GPIOs from DT */ 22948caab75fSGeert Uytterhoeven status = of_spi_register_master(ctlr); 229574317984SJean-Christophe PLAGNIOL-VILLARD if (status) 229674317984SJean-Christophe PLAGNIOL-VILLARD return status; 22976c364062SGeert Uytterhoeven } 2298f3186dd8SLinus Walleij } 229974317984SJean-Christophe PLAGNIOL-VILLARD 2300082c8cb4SDavid Brownell /* even if it's just one always-selected device, there must 2301082c8cb4SDavid Brownell * be at least one chipselect 2302082c8cb4SDavid Brownell */ 23038caab75fSGeert Uytterhoeven if (ctlr->num_chipselect == 0) 2304082c8cb4SDavid Brownell return -EINVAL; 230504b2d03aSGeert Uytterhoeven if (ctlr->bus_num >= 0) { 230604b2d03aSGeert Uytterhoeven /* devices with a fixed bus num must check-in with the num */ 230704b2d03aSGeert Uytterhoeven mutex_lock(&board_lock); 230804b2d03aSGeert Uytterhoeven id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 230904b2d03aSGeert Uytterhoeven ctlr->bus_num + 1, GFP_KERNEL); 231004b2d03aSGeert Uytterhoeven mutex_unlock(&board_lock); 231104b2d03aSGeert Uytterhoeven if (WARN(id < 0, "couldn't get idr")) 231204b2d03aSGeert Uytterhoeven return id == -ENOSPC ? -EBUSY : id; 231304b2d03aSGeert Uytterhoeven ctlr->bus_num = id; 231404b2d03aSGeert Uytterhoeven } else if (ctlr->dev.of_node) { 23159b61e302SSuniel Mahesh /* allocate dynamic bus number using Linux idr */ 23169b61e302SSuniel Mahesh id = of_alias_get_id(ctlr->dev.of_node, "spi"); 23179b61e302SSuniel Mahesh if (id >= 0) { 23189b61e302SSuniel Mahesh ctlr->bus_num = id; 23199b61e302SSuniel Mahesh mutex_lock(&board_lock); 23209b61e302SSuniel Mahesh id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 23219b61e302SSuniel Mahesh ctlr->bus_num + 1, GFP_KERNEL); 23229b61e302SSuniel Mahesh mutex_unlock(&board_lock); 23239b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 23249b61e302SSuniel Mahesh return id == -ENOSPC ? -EBUSY : id; 23259b61e302SSuniel Mahesh } 23269b61e302SSuniel Mahesh } 23278caab75fSGeert Uytterhoeven if (ctlr->bus_num < 0) { 232842bdd706SLucas Stach first_dynamic = of_alias_get_highest_id("spi"); 232942bdd706SLucas Stach if (first_dynamic < 0) 233042bdd706SLucas Stach first_dynamic = 0; 233142bdd706SLucas Stach else 233242bdd706SLucas Stach first_dynamic++; 233342bdd706SLucas Stach 23349b61e302SSuniel Mahesh mutex_lock(&board_lock); 233542bdd706SLucas Stach id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, 233642bdd706SLucas Stach 0, GFP_KERNEL); 23379b61e302SSuniel Mahesh mutex_unlock(&board_lock); 23389b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 23399b61e302SSuniel Mahesh return id; 23409b61e302SSuniel Mahesh ctlr->bus_num = id; 23418ae12a0dSDavid Brownell } 23428caab75fSGeert Uytterhoeven INIT_LIST_HEAD(&ctlr->queue); 23438caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->queue_lock); 23448caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->bus_lock_spinlock); 23458caab75fSGeert Uytterhoeven mutex_init(&ctlr->bus_lock_mutex); 23468caab75fSGeert Uytterhoeven mutex_init(&ctlr->io_mutex); 23478caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 23488caab75fSGeert Uytterhoeven init_completion(&ctlr->xfer_completion); 23498caab75fSGeert Uytterhoeven if (!ctlr->max_dma_len) 23508caab75fSGeert Uytterhoeven ctlr->max_dma_len = INT_MAX; 2351cf32b71eSErnst Schwab 23528ae12a0dSDavid Brownell /* register the device, then userspace will see it. 23538ae12a0dSDavid Brownell * registration fails if the bus ID is in use. 23548ae12a0dSDavid Brownell */ 23558caab75fSGeert Uytterhoeven dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 23568caab75fSGeert Uytterhoeven status = device_add(&ctlr->dev); 23579b61e302SSuniel Mahesh if (status < 0) { 23589b61e302SSuniel Mahesh /* free bus id */ 23599b61e302SSuniel Mahesh mutex_lock(&board_lock); 23609b61e302SSuniel Mahesh idr_remove(&spi_master_idr, ctlr->bus_num); 23619b61e302SSuniel Mahesh mutex_unlock(&board_lock); 23628ae12a0dSDavid Brownell goto done; 23639b61e302SSuniel Mahesh } 23649b61e302SSuniel Mahesh dev_dbg(dev, "registered %s %s\n", 23658caab75fSGeert Uytterhoeven spi_controller_is_slave(ctlr) ? "slave" : "master", 23669b61e302SSuniel Mahesh dev_name(&ctlr->dev)); 23678ae12a0dSDavid Brownell 2368b5932f5cSBoris Brezillon /* 2369b5932f5cSBoris Brezillon * If we're using a queued driver, start the queue. Note that we don't 2370b5932f5cSBoris Brezillon * need the queueing logic if the driver is only supporting high-level 2371b5932f5cSBoris Brezillon * memory operations. 2372b5932f5cSBoris Brezillon */ 2373b5932f5cSBoris Brezillon if (ctlr->transfer) { 23748caab75fSGeert Uytterhoeven dev_info(dev, "controller is unqueued, this is deprecated\n"); 2375b5932f5cSBoris Brezillon } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 23768caab75fSGeert Uytterhoeven status = spi_controller_initialize_queue(ctlr); 2377ffbbdd21SLinus Walleij if (status) { 23788caab75fSGeert Uytterhoeven device_del(&ctlr->dev); 23799b61e302SSuniel Mahesh /* free bus id */ 23809b61e302SSuniel Mahesh mutex_lock(&board_lock); 23819b61e302SSuniel Mahesh idr_remove(&spi_master_idr, ctlr->bus_num); 23829b61e302SSuniel Mahesh mutex_unlock(&board_lock); 2383ffbbdd21SLinus Walleij goto done; 2384ffbbdd21SLinus Walleij } 2385ffbbdd21SLinus Walleij } 2386eca2ebc7SMartin Sperl /* add statistics */ 23878caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->statistics.lock); 2388ffbbdd21SLinus Walleij 23892b9603a0SFeng Tang mutex_lock(&board_lock); 23908caab75fSGeert Uytterhoeven list_add_tail(&ctlr->list, &spi_controller_list); 23912b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 23928caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 23932b9603a0SFeng Tang mutex_unlock(&board_lock); 23942b9603a0SFeng Tang 239564bee4d2SMika Westerberg /* Register devices from the device tree and ACPI */ 23968caab75fSGeert Uytterhoeven of_register_spi_devices(ctlr); 23978caab75fSGeert Uytterhoeven acpi_register_spi_devices(ctlr); 23988ae12a0dSDavid Brownell done: 23998ae12a0dSDavid Brownell return status; 24008ae12a0dSDavid Brownell } 24018caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller); 24028ae12a0dSDavid Brownell 2403666d5b4cSMark Brown static void devm_spi_unregister(struct device *dev, void *res) 2404666d5b4cSMark Brown { 24058caab75fSGeert Uytterhoeven spi_unregister_controller(*(struct spi_controller **)res); 2406666d5b4cSMark Brown } 2407666d5b4cSMark Brown 2408666d5b4cSMark Brown /** 24098caab75fSGeert Uytterhoeven * devm_spi_register_controller - register managed SPI master or slave 24108caab75fSGeert Uytterhoeven * controller 24118caab75fSGeert Uytterhoeven * @dev: device managing SPI controller 24128caab75fSGeert Uytterhoeven * @ctlr: initialized controller, originally from spi_alloc_master() or 24138caab75fSGeert Uytterhoeven * spi_alloc_slave() 2414666d5b4cSMark Brown * Context: can sleep 2415666d5b4cSMark Brown * 24168caab75fSGeert Uytterhoeven * Register a SPI device as with spi_register_controller() which will 241768b892f1SJohan Hovold * automatically be unregistered and freed. 241897d56dc6SJavier Martinez Canillas * 241997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 2420666d5b4cSMark Brown */ 24218caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev, 24228caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 2423666d5b4cSMark Brown { 24248caab75fSGeert Uytterhoeven struct spi_controller **ptr; 2425666d5b4cSMark Brown int ret; 2426666d5b4cSMark Brown 2427666d5b4cSMark Brown ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); 2428666d5b4cSMark Brown if (!ptr) 2429666d5b4cSMark Brown return -ENOMEM; 2430666d5b4cSMark Brown 24318caab75fSGeert Uytterhoeven ret = spi_register_controller(ctlr); 24324b92894eSStephen Warren if (!ret) { 24338caab75fSGeert Uytterhoeven *ptr = ctlr; 2434666d5b4cSMark Brown devres_add(dev, ptr); 2435666d5b4cSMark Brown } else { 2436666d5b4cSMark Brown devres_free(ptr); 2437666d5b4cSMark Brown } 2438666d5b4cSMark Brown 2439666d5b4cSMark Brown return ret; 2440666d5b4cSMark Brown } 24418caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller); 2442666d5b4cSMark Brown 244334860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 24448ae12a0dSDavid Brownell { 24450c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 24468ae12a0dSDavid Brownell return 0; 24478ae12a0dSDavid Brownell } 24488ae12a0dSDavid Brownell 24498ae12a0dSDavid Brownell /** 24508caab75fSGeert Uytterhoeven * spi_unregister_controller - unregister SPI master or slave controller 24518caab75fSGeert Uytterhoeven * @ctlr: the controller being unregistered 245233e34dc6SDavid Brownell * Context: can sleep 24538ae12a0dSDavid Brownell * 24548caab75fSGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 24558ae12a0dSDavid Brownell * only ones directly touching chip registers. 24568ae12a0dSDavid Brownell * 24578ae12a0dSDavid Brownell * This must be called from context that can sleep. 245868b892f1SJohan Hovold * 245968b892f1SJohan Hovold * Note that this function also drops a reference to the controller. 24608ae12a0dSDavid Brownell */ 24618caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr) 24628ae12a0dSDavid Brownell { 24639b61e302SSuniel Mahesh struct spi_controller *found; 246467f7b278SJohan Hovold int id = ctlr->bus_num; 246589fc9a1aSJeff Garzik int dummy; 246689fc9a1aSJeff Garzik 24679b61e302SSuniel Mahesh /* First make sure that this controller was ever added */ 24689b61e302SSuniel Mahesh mutex_lock(&board_lock); 246967f7b278SJohan Hovold found = idr_find(&spi_master_idr, id); 24709b61e302SSuniel Mahesh mutex_unlock(&board_lock); 24718caab75fSGeert Uytterhoeven if (ctlr->queued) { 24728caab75fSGeert Uytterhoeven if (spi_destroy_queue(ctlr)) 24738caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue remove failed\n"); 2474ffbbdd21SLinus Walleij } 24752b9603a0SFeng Tang mutex_lock(&board_lock); 24768caab75fSGeert Uytterhoeven list_del(&ctlr->list); 24772b9603a0SFeng Tang mutex_unlock(&board_lock); 24782b9603a0SFeng Tang 24798caab75fSGeert Uytterhoeven dummy = device_for_each_child(&ctlr->dev, NULL, __unregister); 24808caab75fSGeert Uytterhoeven device_unregister(&ctlr->dev); 24819b61e302SSuniel Mahesh /* free bus id */ 24829b61e302SSuniel Mahesh mutex_lock(&board_lock); 2483613bd1eaSJarkko Nikula if (found == ctlr) 248467f7b278SJohan Hovold idr_remove(&spi_master_idr, id); 24859b61e302SSuniel Mahesh mutex_unlock(&board_lock); 24868ae12a0dSDavid Brownell } 24878caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller); 24888ae12a0dSDavid Brownell 24898caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr) 2490ffbbdd21SLinus Walleij { 2491ffbbdd21SLinus Walleij int ret; 2492ffbbdd21SLinus Walleij 24938caab75fSGeert Uytterhoeven /* Basically no-ops for non-queued controllers */ 24948caab75fSGeert Uytterhoeven if (!ctlr->queued) 2495ffbbdd21SLinus Walleij return 0; 2496ffbbdd21SLinus Walleij 24978caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 2498ffbbdd21SLinus Walleij if (ret) 24998caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue stop failed\n"); 2500ffbbdd21SLinus Walleij 2501ffbbdd21SLinus Walleij return ret; 2502ffbbdd21SLinus Walleij } 25038caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend); 2504ffbbdd21SLinus Walleij 25058caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr) 2506ffbbdd21SLinus Walleij { 2507ffbbdd21SLinus Walleij int ret; 2508ffbbdd21SLinus Walleij 25098caab75fSGeert Uytterhoeven if (!ctlr->queued) 2510ffbbdd21SLinus Walleij return 0; 2511ffbbdd21SLinus Walleij 25128caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 2513ffbbdd21SLinus Walleij if (ret) 25148caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue restart failed\n"); 2515ffbbdd21SLinus Walleij 2516ffbbdd21SLinus Walleij return ret; 2517ffbbdd21SLinus Walleij } 25188caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume); 2519ffbbdd21SLinus Walleij 25208caab75fSGeert Uytterhoeven static int __spi_controller_match(struct device *dev, const void *data) 25215ed2c832SDave Young { 25228caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 25239f3b795aSMichał Mirosław const u16 *bus_num = data; 25245ed2c832SDave Young 25258caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 25268caab75fSGeert Uytterhoeven return ctlr->bus_num == *bus_num; 25275ed2c832SDave Young } 25285ed2c832SDave Young 25298ae12a0dSDavid Brownell /** 25308ae12a0dSDavid Brownell * spi_busnum_to_master - look up master associated with bus_num 25318ae12a0dSDavid Brownell * @bus_num: the master's bus number 253233e34dc6SDavid Brownell * Context: can sleep 25338ae12a0dSDavid Brownell * 25348ae12a0dSDavid Brownell * This call may be used with devices that are registered after 25358ae12a0dSDavid Brownell * arch init time. It returns a refcounted pointer to the relevant 25368caab75fSGeert Uytterhoeven * spi_controller (which the caller must release), or NULL if there is 25378ae12a0dSDavid Brownell * no such master registered. 253897d56dc6SJavier Martinez Canillas * 253997d56dc6SJavier Martinez Canillas * Return: the SPI master structure on success, else NULL. 25408ae12a0dSDavid Brownell */ 25418caab75fSGeert Uytterhoeven struct spi_controller *spi_busnum_to_master(u16 bus_num) 25428ae12a0dSDavid Brownell { 254349dce689STony Jones struct device *dev; 25448caab75fSGeert Uytterhoeven struct spi_controller *ctlr = NULL; 25458ae12a0dSDavid Brownell 2546695794aeSGreg Kroah-Hartman dev = class_find_device(&spi_master_class, NULL, &bus_num, 25478caab75fSGeert Uytterhoeven __spi_controller_match); 25485ed2c832SDave Young if (dev) 25498caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 25505ed2c832SDave Young /* reference got in class_find_device */ 25518caab75fSGeert Uytterhoeven return ctlr; 25528ae12a0dSDavid Brownell } 25538ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master); 25548ae12a0dSDavid Brownell 2555d780c371SMartin Sperl /*-------------------------------------------------------------------------*/ 2556d780c371SMartin Sperl 2557d780c371SMartin Sperl /* Core methods for SPI resource management */ 2558d780c371SMartin Sperl 2559d780c371SMartin Sperl /** 2560d780c371SMartin Sperl * spi_res_alloc - allocate a spi resource that is life-cycle managed 2561d780c371SMartin Sperl * during the processing of a spi_message while using 2562d780c371SMartin Sperl * spi_transfer_one 2563d780c371SMartin Sperl * @spi: the spi device for which we allocate memory 2564d780c371SMartin Sperl * @release: the release code to execute for this resource 2565d780c371SMartin Sperl * @size: size to alloc and return 2566d780c371SMartin Sperl * @gfp: GFP allocation flags 2567d780c371SMartin Sperl * 2568d780c371SMartin Sperl * Return: the pointer to the allocated data 2569d780c371SMartin Sperl * 2570d780c371SMartin Sperl * This may get enhanced in the future to allocate from a memory pool 25718caab75fSGeert Uytterhoeven * of the @spi_device or @spi_controller to avoid repeated allocations. 2572d780c371SMartin Sperl */ 2573d780c371SMartin Sperl void *spi_res_alloc(struct spi_device *spi, 2574d780c371SMartin Sperl spi_res_release_t release, 2575d780c371SMartin Sperl size_t size, gfp_t gfp) 2576d780c371SMartin Sperl { 2577d780c371SMartin Sperl struct spi_res *sres; 2578d780c371SMartin Sperl 2579d780c371SMartin Sperl sres = kzalloc(sizeof(*sres) + size, gfp); 2580d780c371SMartin Sperl if (!sres) 2581d780c371SMartin Sperl return NULL; 2582d780c371SMartin Sperl 2583d780c371SMartin Sperl INIT_LIST_HEAD(&sres->entry); 2584d780c371SMartin Sperl sres->release = release; 2585d780c371SMartin Sperl 2586d780c371SMartin Sperl return sres->data; 2587d780c371SMartin Sperl } 2588d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_alloc); 2589d780c371SMartin Sperl 2590d780c371SMartin Sperl /** 2591d780c371SMartin Sperl * spi_res_free - free an spi resource 2592d780c371SMartin Sperl * @res: pointer to the custom data of a resource 2593d780c371SMartin Sperl * 2594d780c371SMartin Sperl */ 2595d780c371SMartin Sperl void spi_res_free(void *res) 2596d780c371SMartin Sperl { 2597d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 2598d780c371SMartin Sperl 2599d780c371SMartin Sperl if (!res) 2600d780c371SMartin Sperl return; 2601d780c371SMartin Sperl 2602d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 2603d780c371SMartin Sperl kfree(sres); 2604d780c371SMartin Sperl } 2605d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_free); 2606d780c371SMartin Sperl 2607d780c371SMartin Sperl /** 2608d780c371SMartin Sperl * spi_res_add - add a spi_res to the spi_message 2609d780c371SMartin Sperl * @message: the spi message 2610d780c371SMartin Sperl * @res: the spi_resource 2611d780c371SMartin Sperl */ 2612d780c371SMartin Sperl void spi_res_add(struct spi_message *message, void *res) 2613d780c371SMartin Sperl { 2614d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 2615d780c371SMartin Sperl 2616d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 2617d780c371SMartin Sperl list_add_tail(&sres->entry, &message->resources); 2618d780c371SMartin Sperl } 2619d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_add); 2620d780c371SMartin Sperl 2621d780c371SMartin Sperl /** 2622d780c371SMartin Sperl * spi_res_release - release all spi resources for this message 26238caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller 2624d780c371SMartin Sperl * @message: the @spi_message 2625d780c371SMartin Sperl */ 26268caab75fSGeert Uytterhoeven void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 2627d780c371SMartin Sperl { 2628d780c371SMartin Sperl struct spi_res *res; 2629d780c371SMartin Sperl 2630d780c371SMartin Sperl while (!list_empty(&message->resources)) { 2631d780c371SMartin Sperl res = list_last_entry(&message->resources, 2632d780c371SMartin Sperl struct spi_res, entry); 2633d780c371SMartin Sperl 2634d780c371SMartin Sperl if (res->release) 26358caab75fSGeert Uytterhoeven res->release(ctlr, message, res->data); 2636d780c371SMartin Sperl 2637d780c371SMartin Sperl list_del(&res->entry); 2638d780c371SMartin Sperl 2639d780c371SMartin Sperl kfree(res); 2640d780c371SMartin Sperl } 2641d780c371SMartin Sperl } 2642d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_release); 26438ae12a0dSDavid Brownell 26448ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 26458ae12a0dSDavid Brownell 2646523baf5aSMartin Sperl /* Core methods for spi_message alterations */ 2647523baf5aSMartin Sperl 26488caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr, 2649523baf5aSMartin Sperl struct spi_message *msg, 2650523baf5aSMartin Sperl void *res) 2651523baf5aSMartin Sperl { 2652523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer = res; 2653523baf5aSMartin Sperl size_t i; 2654523baf5aSMartin Sperl 2655523baf5aSMartin Sperl /* call extra callback if requested */ 2656523baf5aSMartin Sperl if (rxfer->release) 26578caab75fSGeert Uytterhoeven rxfer->release(ctlr, msg, res); 2658523baf5aSMartin Sperl 2659523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 2660523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 2661523baf5aSMartin Sperl 2662523baf5aSMartin Sperl /* remove the formerly inserted entries */ 2663523baf5aSMartin Sperl for (i = 0; i < rxfer->inserted; i++) 2664523baf5aSMartin Sperl list_del(&rxfer->inserted_transfers[i].transfer_list); 2665523baf5aSMartin Sperl } 2666523baf5aSMartin Sperl 2667523baf5aSMartin Sperl /** 2668523baf5aSMartin Sperl * spi_replace_transfers - replace transfers with several transfers 2669523baf5aSMartin Sperl * and register change with spi_message.resources 2670523baf5aSMartin Sperl * @msg: the spi_message we work upon 2671523baf5aSMartin Sperl * @xfer_first: the first spi_transfer we want to replace 2672523baf5aSMartin Sperl * @remove: number of transfers to remove 2673523baf5aSMartin Sperl * @insert: the number of transfers we want to insert instead 2674523baf5aSMartin Sperl * @release: extra release code necessary in some circumstances 2675523baf5aSMartin Sperl * @extradatasize: extra data to allocate (with alignment guarantees 2676523baf5aSMartin Sperl * of struct @spi_transfer) 267705885397SMartin Sperl * @gfp: gfp flags 2678523baf5aSMartin Sperl * 2679523baf5aSMartin Sperl * Returns: pointer to @spi_replaced_transfers, 2680523baf5aSMartin Sperl * PTR_ERR(...) in case of errors. 2681523baf5aSMartin Sperl */ 2682523baf5aSMartin Sperl struct spi_replaced_transfers *spi_replace_transfers( 2683523baf5aSMartin Sperl struct spi_message *msg, 2684523baf5aSMartin Sperl struct spi_transfer *xfer_first, 2685523baf5aSMartin Sperl size_t remove, 2686523baf5aSMartin Sperl size_t insert, 2687523baf5aSMartin Sperl spi_replaced_release_t release, 2688523baf5aSMartin Sperl size_t extradatasize, 2689523baf5aSMartin Sperl gfp_t gfp) 2690523baf5aSMartin Sperl { 2691523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer; 2692523baf5aSMartin Sperl struct spi_transfer *xfer; 2693523baf5aSMartin Sperl size_t i; 2694523baf5aSMartin Sperl 2695523baf5aSMartin Sperl /* allocate the structure using spi_res */ 2696523baf5aSMartin Sperl rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 2697523baf5aSMartin Sperl insert * sizeof(struct spi_transfer) 2698523baf5aSMartin Sperl + sizeof(struct spi_replaced_transfers) 2699523baf5aSMartin Sperl + extradatasize, 2700523baf5aSMartin Sperl gfp); 2701523baf5aSMartin Sperl if (!rxfer) 2702523baf5aSMartin Sperl return ERR_PTR(-ENOMEM); 2703523baf5aSMartin Sperl 2704523baf5aSMartin Sperl /* the release code to invoke before running the generic release */ 2705523baf5aSMartin Sperl rxfer->release = release; 2706523baf5aSMartin Sperl 2707523baf5aSMartin Sperl /* assign extradata */ 2708523baf5aSMartin Sperl if (extradatasize) 2709523baf5aSMartin Sperl rxfer->extradata = 2710523baf5aSMartin Sperl &rxfer->inserted_transfers[insert]; 2711523baf5aSMartin Sperl 2712523baf5aSMartin Sperl /* init the replaced_transfers list */ 2713523baf5aSMartin Sperl INIT_LIST_HEAD(&rxfer->replaced_transfers); 2714523baf5aSMartin Sperl 2715523baf5aSMartin Sperl /* assign the list_entry after which we should reinsert 2716523baf5aSMartin Sperl * the @replaced_transfers - it may be spi_message.messages! 2717523baf5aSMartin Sperl */ 2718523baf5aSMartin Sperl rxfer->replaced_after = xfer_first->transfer_list.prev; 2719523baf5aSMartin Sperl 2720523baf5aSMartin Sperl /* remove the requested number of transfers */ 2721523baf5aSMartin Sperl for (i = 0; i < remove; i++) { 2722523baf5aSMartin Sperl /* if the entry after replaced_after it is msg->transfers 2723523baf5aSMartin Sperl * then we have been requested to remove more transfers 2724523baf5aSMartin Sperl * than are in the list 2725523baf5aSMartin Sperl */ 2726523baf5aSMartin Sperl if (rxfer->replaced_after->next == &msg->transfers) { 2727523baf5aSMartin Sperl dev_err(&msg->spi->dev, 2728523baf5aSMartin Sperl "requested to remove more spi_transfers than are available\n"); 2729523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 2730523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, 2731523baf5aSMartin Sperl rxfer->replaced_after); 2732523baf5aSMartin Sperl 2733523baf5aSMartin Sperl /* free the spi_replace_transfer structure */ 2734523baf5aSMartin Sperl spi_res_free(rxfer); 2735523baf5aSMartin Sperl 2736523baf5aSMartin Sperl /* and return with an error */ 2737523baf5aSMartin Sperl return ERR_PTR(-EINVAL); 2738523baf5aSMartin Sperl } 2739523baf5aSMartin Sperl 2740523baf5aSMartin Sperl /* remove the entry after replaced_after from list of 2741523baf5aSMartin Sperl * transfers and add it to list of replaced_transfers 2742523baf5aSMartin Sperl */ 2743523baf5aSMartin Sperl list_move_tail(rxfer->replaced_after->next, 2744523baf5aSMartin Sperl &rxfer->replaced_transfers); 2745523baf5aSMartin Sperl } 2746523baf5aSMartin Sperl 2747523baf5aSMartin Sperl /* create copy of the given xfer with identical settings 2748523baf5aSMartin Sperl * based on the first transfer to get removed 2749523baf5aSMartin Sperl */ 2750523baf5aSMartin Sperl for (i = 0; i < insert; i++) { 2751523baf5aSMartin Sperl /* we need to run in reverse order */ 2752523baf5aSMartin Sperl xfer = &rxfer->inserted_transfers[insert - 1 - i]; 2753523baf5aSMartin Sperl 2754523baf5aSMartin Sperl /* copy all spi_transfer data */ 2755523baf5aSMartin Sperl memcpy(xfer, xfer_first, sizeof(*xfer)); 2756523baf5aSMartin Sperl 2757523baf5aSMartin Sperl /* add to list */ 2758523baf5aSMartin Sperl list_add(&xfer->transfer_list, rxfer->replaced_after); 2759523baf5aSMartin Sperl 2760523baf5aSMartin Sperl /* clear cs_change and delay_usecs for all but the last */ 2761523baf5aSMartin Sperl if (i) { 2762523baf5aSMartin Sperl xfer->cs_change = false; 2763523baf5aSMartin Sperl xfer->delay_usecs = 0; 2764523baf5aSMartin Sperl } 2765523baf5aSMartin Sperl } 2766523baf5aSMartin Sperl 2767523baf5aSMartin Sperl /* set up inserted */ 2768523baf5aSMartin Sperl rxfer->inserted = insert; 2769523baf5aSMartin Sperl 2770523baf5aSMartin Sperl /* and register it with spi_res/spi_message */ 2771523baf5aSMartin Sperl spi_res_add(msg, rxfer); 2772523baf5aSMartin Sperl 2773523baf5aSMartin Sperl return rxfer; 2774523baf5aSMartin Sperl } 2775523baf5aSMartin Sperl EXPORT_SYMBOL_GPL(spi_replace_transfers); 2776523baf5aSMartin Sperl 27778caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 2778d9f12122SMartin Sperl struct spi_message *msg, 2779d9f12122SMartin Sperl struct spi_transfer **xferp, 2780d9f12122SMartin Sperl size_t maxsize, 2781d9f12122SMartin Sperl gfp_t gfp) 2782d9f12122SMartin Sperl { 2783d9f12122SMartin Sperl struct spi_transfer *xfer = *xferp, *xfers; 2784d9f12122SMartin Sperl struct spi_replaced_transfers *srt; 2785d9f12122SMartin Sperl size_t offset; 2786d9f12122SMartin Sperl size_t count, i; 2787d9f12122SMartin Sperl 2788d9f12122SMartin Sperl /* warn once about this fact that we are splitting a transfer */ 2789d9f12122SMartin Sperl dev_warn_once(&msg->spi->dev, 27907d62f51eSFabio Estevam "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n", 2791d9f12122SMartin Sperl xfer->len, maxsize); 2792d9f12122SMartin Sperl 2793d9f12122SMartin Sperl /* calculate how many we have to replace */ 2794d9f12122SMartin Sperl count = DIV_ROUND_UP(xfer->len, maxsize); 2795d9f12122SMartin Sperl 2796d9f12122SMartin Sperl /* create replacement */ 2797d9f12122SMartin Sperl srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 2798657d32efSDan Carpenter if (IS_ERR(srt)) 2799657d32efSDan Carpenter return PTR_ERR(srt); 2800d9f12122SMartin Sperl xfers = srt->inserted_transfers; 2801d9f12122SMartin Sperl 2802d9f12122SMartin Sperl /* now handle each of those newly inserted spi_transfers 2803d9f12122SMartin Sperl * note that the replacements spi_transfers all are preset 2804d9f12122SMartin Sperl * to the same values as *xferp, so tx_buf, rx_buf and len 2805d9f12122SMartin Sperl * are all identical (as well as most others) 2806d9f12122SMartin Sperl * so we just have to fix up len and the pointers. 2807d9f12122SMartin Sperl * 2808d9f12122SMartin Sperl * this also includes support for the depreciated 2809d9f12122SMartin Sperl * spi_message.is_dma_mapped interface 2810d9f12122SMartin Sperl */ 2811d9f12122SMartin Sperl 2812d9f12122SMartin Sperl /* the first transfer just needs the length modified, so we 2813d9f12122SMartin Sperl * run it outside the loop 2814d9f12122SMartin Sperl */ 2815c8dab77aSFabio Estevam xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 2816d9f12122SMartin Sperl 2817d9f12122SMartin Sperl /* all the others need rx_buf/tx_buf also set */ 2818d9f12122SMartin Sperl for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 2819d9f12122SMartin Sperl /* update rx_buf, tx_buf and dma */ 2820d9f12122SMartin Sperl if (xfers[i].rx_buf) 2821d9f12122SMartin Sperl xfers[i].rx_buf += offset; 2822d9f12122SMartin Sperl if (xfers[i].rx_dma) 2823d9f12122SMartin Sperl xfers[i].rx_dma += offset; 2824d9f12122SMartin Sperl if (xfers[i].tx_buf) 2825d9f12122SMartin Sperl xfers[i].tx_buf += offset; 2826d9f12122SMartin Sperl if (xfers[i].tx_dma) 2827d9f12122SMartin Sperl xfers[i].tx_dma += offset; 2828d9f12122SMartin Sperl 2829d9f12122SMartin Sperl /* update length */ 2830d9f12122SMartin Sperl xfers[i].len = min(maxsize, xfers[i].len - offset); 2831d9f12122SMartin Sperl } 2832d9f12122SMartin Sperl 2833d9f12122SMartin Sperl /* we set up xferp to the last entry we have inserted, 2834d9f12122SMartin Sperl * so that we skip those already split transfers 2835d9f12122SMartin Sperl */ 2836d9f12122SMartin Sperl *xferp = &xfers[count - 1]; 2837d9f12122SMartin Sperl 2838d9f12122SMartin Sperl /* increment statistics counters */ 28398caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 2840d9f12122SMartin Sperl transfers_split_maxsize); 2841d9f12122SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, 2842d9f12122SMartin Sperl transfers_split_maxsize); 2843d9f12122SMartin Sperl 2844d9f12122SMartin Sperl return 0; 2845d9f12122SMartin Sperl } 2846d9f12122SMartin Sperl 2847d9f12122SMartin Sperl /** 2848d9f12122SMartin Sperl * spi_split_tranfers_maxsize - split spi transfers into multiple transfers 2849d9f12122SMartin Sperl * when an individual transfer exceeds a 2850d9f12122SMartin Sperl * certain size 28518caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller for this transfer 28523700ce95SMasanari Iida * @msg: the @spi_message to transform 28533700ce95SMasanari Iida * @maxsize: the maximum when to apply this 285410f11a22SJavier Martinez Canillas * @gfp: GFP allocation flags 2855d9f12122SMartin Sperl * 2856d9f12122SMartin Sperl * Return: status of transformation 2857d9f12122SMartin Sperl */ 28588caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr, 2859d9f12122SMartin Sperl struct spi_message *msg, 2860d9f12122SMartin Sperl size_t maxsize, 2861d9f12122SMartin Sperl gfp_t gfp) 2862d9f12122SMartin Sperl { 2863d9f12122SMartin Sperl struct spi_transfer *xfer; 2864d9f12122SMartin Sperl int ret; 2865d9f12122SMartin Sperl 2866d9f12122SMartin Sperl /* iterate over the transfer_list, 2867d9f12122SMartin Sperl * but note that xfer is advanced to the last transfer inserted 2868d9f12122SMartin Sperl * to avoid checking sizes again unnecessarily (also xfer does 2869d9f12122SMartin Sperl * potentiall belong to a different list by the time the 2870d9f12122SMartin Sperl * replacement has happened 2871d9f12122SMartin Sperl */ 2872d9f12122SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 2873d9f12122SMartin Sperl if (xfer->len > maxsize) { 28748caab75fSGeert Uytterhoeven ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 28758caab75fSGeert Uytterhoeven maxsize, gfp); 2876d9f12122SMartin Sperl if (ret) 2877d9f12122SMartin Sperl return ret; 2878d9f12122SMartin Sperl } 2879d9f12122SMartin Sperl } 2880d9f12122SMartin Sperl 2881d9f12122SMartin Sperl return 0; 2882d9f12122SMartin Sperl } 2883d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 28848ae12a0dSDavid Brownell 28858ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 28868ae12a0dSDavid Brownell 28878caab75fSGeert Uytterhoeven /* Core methods for SPI controller protocol drivers. Some of the 28887d077197SDavid Brownell * other core methods are currently defined as inline functions. 28897d077197SDavid Brownell */ 28907d077197SDavid Brownell 28918caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 28928caab75fSGeert Uytterhoeven u8 bits_per_word) 289363ab645fSStefan Brüns { 28948caab75fSGeert Uytterhoeven if (ctlr->bits_per_word_mask) { 289563ab645fSStefan Brüns /* Only 32 bits fit in the mask */ 289663ab645fSStefan Brüns if (bits_per_word > 32) 289763ab645fSStefan Brüns return -EINVAL; 28988caab75fSGeert Uytterhoeven if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 289963ab645fSStefan Brüns return -EINVAL; 290063ab645fSStefan Brüns } 290163ab645fSStefan Brüns 290263ab645fSStefan Brüns return 0; 290363ab645fSStefan Brüns } 290463ab645fSStefan Brüns 29057d077197SDavid Brownell /** 29067d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 29077d077197SDavid Brownell * @spi: the device whose settings are being modified 29087d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 29097d077197SDavid Brownell * 29107d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 29117d077197SDavid Brownell * device doesn't work with its default. They may likewise need 29127d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 29137d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 29147d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 29157d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 29167d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 29177d077197SDavid Brownell * 29187d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 29197d077197SDavid Brownell * that the underlying controller or its driver does not support. For 29207d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 29217d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 292297d56dc6SJavier Martinez Canillas * 292397d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 29247d077197SDavid Brownell */ 29257d077197SDavid Brownell int spi_setup(struct spi_device *spi) 29267d077197SDavid Brownell { 292783596fbeSGeert Uytterhoeven unsigned bad_bits, ugly_bits; 29285ab8d262SAndy Shevchenko int status; 29297d077197SDavid Brownell 2930f477b7fbSwangyuhang /* check mode to prevent that DUAL and QUAD set at the same time 2931f477b7fbSwangyuhang */ 2932f477b7fbSwangyuhang if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || 2933f477b7fbSwangyuhang ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { 2934f477b7fbSwangyuhang dev_err(&spi->dev, 2935f477b7fbSwangyuhang "setup: can not select dual and quad at the same time\n"); 2936f477b7fbSwangyuhang return -EINVAL; 2937f477b7fbSwangyuhang } 2938f477b7fbSwangyuhang /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden 2939f477b7fbSwangyuhang */ 2940f477b7fbSwangyuhang if ((spi->mode & SPI_3WIRE) && (spi->mode & 29416b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 29426b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 2943f477b7fbSwangyuhang return -EINVAL; 2944e7db06b5SDavid Brownell /* help drivers fail *cleanly* when they need options 29458caab75fSGeert Uytterhoeven * that aren't supported with their current controller 2946cbaa62e0SDavid Lechner * SPI_CS_WORD has a fallback software implementation, 2947cbaa62e0SDavid Lechner * so it is ignored here. 2948e7db06b5SDavid Brownell */ 2949cbaa62e0SDavid Lechner bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); 295083596fbeSGeert Uytterhoeven ugly_bits = bad_bits & 29516b03061fSYogesh Narayan Gaur (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 29526b03061fSYogesh Narayan Gaur SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL); 295383596fbeSGeert Uytterhoeven if (ugly_bits) { 295483596fbeSGeert Uytterhoeven dev_warn(&spi->dev, 295583596fbeSGeert Uytterhoeven "setup: ignoring unsupported mode bits %x\n", 295683596fbeSGeert Uytterhoeven ugly_bits); 295783596fbeSGeert Uytterhoeven spi->mode &= ~ugly_bits; 295883596fbeSGeert Uytterhoeven bad_bits &= ~ugly_bits; 295983596fbeSGeert Uytterhoeven } 2960e7db06b5SDavid Brownell if (bad_bits) { 2961eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 2962e7db06b5SDavid Brownell bad_bits); 2963e7db06b5SDavid Brownell return -EINVAL; 2964e7db06b5SDavid Brownell } 2965e7db06b5SDavid Brownell 29667d077197SDavid Brownell if (!spi->bits_per_word) 29677d077197SDavid Brownell spi->bits_per_word = 8; 29687d077197SDavid Brownell 29698caab75fSGeert Uytterhoeven status = __spi_validate_bits_per_word(spi->controller, 29708caab75fSGeert Uytterhoeven spi->bits_per_word); 29715ab8d262SAndy Shevchenko if (status) 29725ab8d262SAndy Shevchenko return status; 297363ab645fSStefan Brüns 2974052eb2d4SAxel Lin if (!spi->max_speed_hz) 29758caab75fSGeert Uytterhoeven spi->max_speed_hz = spi->controller->max_speed_hz; 2976052eb2d4SAxel Lin 29778caab75fSGeert Uytterhoeven if (spi->controller->setup) 29788caab75fSGeert Uytterhoeven status = spi->controller->setup(spi); 29797d077197SDavid Brownell 2980abeedb01SFranklin S Cooper Jr spi_set_cs(spi, false); 2981abeedb01SFranklin S Cooper Jr 29825fe5f05eSJingoo Han dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 29837d077197SDavid Brownell (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), 29847d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 29857d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 29867d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 29877d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 29887d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 29897d077197SDavid Brownell status); 29907d077197SDavid Brownell 29917d077197SDavid Brownell return status; 29927d077197SDavid Brownell } 29937d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 29947d077197SDavid Brownell 299590808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message) 2996cf32b71eSErnst Schwab { 29978caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 2998e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 29996ea31293SAtsushi Nemoto int w_size; 3000cf32b71eSErnst Schwab 300124a0013aSMark Brown if (list_empty(&message->transfers)) 300224a0013aSMark Brown return -EINVAL; 300324a0013aSMark Brown 3004cbaa62e0SDavid Lechner /* If an SPI controller does not support toggling the CS line on each 300571388b21SDavid Lechner * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO 300671388b21SDavid Lechner * for the CS line, we can emulate the CS-per-word hardware function by 3007cbaa62e0SDavid Lechner * splitting transfers into one-word transfers and ensuring that 3008cbaa62e0SDavid Lechner * cs_change is set for each transfer. 3009cbaa62e0SDavid Lechner */ 301071388b21SDavid Lechner if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || 3011f3186dd8SLinus Walleij spi->cs_gpiod || 301271388b21SDavid Lechner gpio_is_valid(spi->cs_gpio))) { 3013cbaa62e0SDavid Lechner size_t maxsize; 3014cbaa62e0SDavid Lechner int ret; 3015cbaa62e0SDavid Lechner 3016cbaa62e0SDavid Lechner maxsize = (spi->bits_per_word + 7) / 8; 3017cbaa62e0SDavid Lechner 3018cbaa62e0SDavid Lechner /* spi_split_transfers_maxsize() requires message->spi */ 3019cbaa62e0SDavid Lechner message->spi = spi; 3020cbaa62e0SDavid Lechner 3021cbaa62e0SDavid Lechner ret = spi_split_transfers_maxsize(ctlr, message, maxsize, 3022cbaa62e0SDavid Lechner GFP_KERNEL); 3023cbaa62e0SDavid Lechner if (ret) 3024cbaa62e0SDavid Lechner return ret; 3025cbaa62e0SDavid Lechner 3026cbaa62e0SDavid Lechner list_for_each_entry(xfer, &message->transfers, transfer_list) { 3027cbaa62e0SDavid Lechner /* don't change cs_change on the last entry in the list */ 3028cbaa62e0SDavid Lechner if (list_is_last(&xfer->transfer_list, &message->transfers)) 3029cbaa62e0SDavid Lechner break; 3030cbaa62e0SDavid Lechner xfer->cs_change = 1; 3031cbaa62e0SDavid Lechner } 3032cbaa62e0SDavid Lechner } 3033cbaa62e0SDavid Lechner 3034cf32b71eSErnst Schwab /* Half-duplex links include original MicroWire, and ones with 3035cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 3036cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 3037cf32b71eSErnst Schwab * software limitations. 3038cf32b71eSErnst Schwab */ 30398caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 30408caab75fSGeert Uytterhoeven (spi->mode & SPI_3WIRE)) { 30418caab75fSGeert Uytterhoeven unsigned flags = ctlr->flags; 3042cf32b71eSErnst Schwab 3043cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 3044cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 3045cf32b71eSErnst Schwab return -EINVAL; 30468caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 3047cf32b71eSErnst Schwab return -EINVAL; 30488caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 3049cf32b71eSErnst Schwab return -EINVAL; 3050cf32b71eSErnst Schwab } 3051cf32b71eSErnst Schwab } 3052cf32b71eSErnst Schwab 3053e6811d1dSLaxman Dewangan /** 3054059b8ffeSLaxman Dewangan * Set transfer bits_per_word and max speed as spi device default if 3055059b8ffeSLaxman Dewangan * it is not set for this transfer. 3056f477b7fbSwangyuhang * Set transfer tx_nbits and rx_nbits as single transfer default 3057f477b7fbSwangyuhang * (SPI_NBITS_SINGLE) if it is not set for this transfer. 3058b7bb367aSJonas Bonn * Ensure transfer word_delay is at least as long as that required by 3059b7bb367aSJonas Bonn * device itself. 3060e6811d1dSLaxman Dewangan */ 306177e80588SMartin Sperl message->frame_length = 0; 3062e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 3063078726ceSSourav Poddar message->frame_length += xfer->len; 3064e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 3065e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 3066a6f87fadSAxel Lin 3067a6f87fadSAxel Lin if (!xfer->speed_hz) 3068059b8ffeSLaxman Dewangan xfer->speed_hz = spi->max_speed_hz; 30697dc9fbc3SMark Brown if (!xfer->speed_hz) 30708caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 3071a6f87fadSAxel Lin 30728caab75fSGeert Uytterhoeven if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 30738caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 307456ede94aSGabor Juhos 30758caab75fSGeert Uytterhoeven if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 3076543bb255SStephen Warren return -EINVAL; 3077a2fd4f9fSMark Brown 30784d94bd21SIvan T. Ivanov /* 30794d94bd21SIvan T. Ivanov * SPI transfer length should be multiple of SPI word size 30804d94bd21SIvan T. Ivanov * where SPI word size should be power-of-two multiple 30814d94bd21SIvan T. Ivanov */ 30824d94bd21SIvan T. Ivanov if (xfer->bits_per_word <= 8) 30834d94bd21SIvan T. Ivanov w_size = 1; 30844d94bd21SIvan T. Ivanov else if (xfer->bits_per_word <= 16) 30854d94bd21SIvan T. Ivanov w_size = 2; 30864d94bd21SIvan T. Ivanov else 30874d94bd21SIvan T. Ivanov w_size = 4; 30884d94bd21SIvan T. Ivanov 30894d94bd21SIvan T. Ivanov /* No partial transfers accepted */ 30906ea31293SAtsushi Nemoto if (xfer->len % w_size) 30914d94bd21SIvan T. Ivanov return -EINVAL; 30924d94bd21SIvan T. Ivanov 30938caab75fSGeert Uytterhoeven if (xfer->speed_hz && ctlr->min_speed_hz && 30948caab75fSGeert Uytterhoeven xfer->speed_hz < ctlr->min_speed_hz) 3095a2fd4f9fSMark Brown return -EINVAL; 3096f477b7fbSwangyuhang 3097f477b7fbSwangyuhang if (xfer->tx_buf && !xfer->tx_nbits) 3098f477b7fbSwangyuhang xfer->tx_nbits = SPI_NBITS_SINGLE; 3099f477b7fbSwangyuhang if (xfer->rx_buf && !xfer->rx_nbits) 3100f477b7fbSwangyuhang xfer->rx_nbits = SPI_NBITS_SINGLE; 3101f477b7fbSwangyuhang /* check transfer tx/rx_nbits: 31021afd9989SGeert Uytterhoeven * 1. check the value matches one of single, dual and quad 31031afd9989SGeert Uytterhoeven * 2. check tx/rx_nbits match the mode in spi_device 3104f477b7fbSwangyuhang */ 3105db90a441SSourav Poddar if (xfer->tx_buf) { 3106f477b7fbSwangyuhang if (xfer->tx_nbits != SPI_NBITS_SINGLE && 3107f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_DUAL && 3108f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_QUAD) 3109a2fd4f9fSMark Brown return -EINVAL; 3110f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 3111f477b7fbSwangyuhang !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3112f477b7fbSwangyuhang return -EINVAL; 3113f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 3114f477b7fbSwangyuhang !(spi->mode & SPI_TX_QUAD)) 3115f477b7fbSwangyuhang return -EINVAL; 3116db90a441SSourav Poddar } 3117f477b7fbSwangyuhang /* check transfer rx_nbits */ 3118db90a441SSourav Poddar if (xfer->rx_buf) { 3119f477b7fbSwangyuhang if (xfer->rx_nbits != SPI_NBITS_SINGLE && 3120f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_DUAL && 3121f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_QUAD) 3122f477b7fbSwangyuhang return -EINVAL; 3123f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 3124f477b7fbSwangyuhang !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 3125f477b7fbSwangyuhang return -EINVAL; 3126f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 3127f477b7fbSwangyuhang !(spi->mode & SPI_RX_QUAD)) 3128f477b7fbSwangyuhang return -EINVAL; 3129e6811d1dSLaxman Dewangan } 3130b7bb367aSJonas Bonn 3131b7bb367aSJonas Bonn if (xfer->word_delay_usecs < spi->word_delay_usecs) 3132b7bb367aSJonas Bonn xfer->word_delay_usecs = spi->word_delay_usecs; 3133e6811d1dSLaxman Dewangan } 3134e6811d1dSLaxman Dewangan 3135cf32b71eSErnst Schwab message->status = -EINPROGRESS; 313690808738SMark Brown 313790808738SMark Brown return 0; 313890808738SMark Brown } 313990808738SMark Brown 314090808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message) 314190808738SMark Brown { 31428caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 314390808738SMark Brown 3144b5932f5cSBoris Brezillon /* 3145b5932f5cSBoris Brezillon * Some controllers do not support doing regular SPI transfers. Return 3146b5932f5cSBoris Brezillon * ENOTSUPP when this is the case. 3147b5932f5cSBoris Brezillon */ 3148b5932f5cSBoris Brezillon if (!ctlr->transfer) 3149b5932f5cSBoris Brezillon return -ENOTSUPP; 3150b5932f5cSBoris Brezillon 315190808738SMark Brown message->spi = spi; 315290808738SMark Brown 31538caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); 3154eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); 3155eca2ebc7SMartin Sperl 315690808738SMark Brown trace_spi_message_submit(message); 315790808738SMark Brown 31588caab75fSGeert Uytterhoeven return ctlr->transfer(spi, message); 3159cf32b71eSErnst Schwab } 3160cf32b71eSErnst Schwab 3161568d0697SDavid Brownell /** 3162568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 3163568d0697SDavid Brownell * @spi: device with which data will be exchanged 3164568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 3165568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 3166568d0697SDavid Brownell * 3167568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 3168568d0697SDavid Brownell * as well as from task contexts which can sleep. 3169568d0697SDavid Brownell * 3170568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 3171568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 3172568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 3173568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 3174568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 3175568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 3176568d0697SDavid Brownell * core or controller driver code. 3177568d0697SDavid Brownell * 3178568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 3179568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 3180568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 3181568d0697SDavid Brownell * time requirements, for example. 3182568d0697SDavid Brownell * 3183568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 3184568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 3185568d0697SDavid Brownell * Until returning from the associated message completion callback, 3186568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 3187568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 3188568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 318997d56dc6SJavier Martinez Canillas * 319097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3191568d0697SDavid Brownell */ 3192568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 3193568d0697SDavid Brownell { 31948caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3195cf32b71eSErnst Schwab int ret; 3196cf32b71eSErnst Schwab unsigned long flags; 3197568d0697SDavid Brownell 319890808738SMark Brown ret = __spi_validate(spi, message); 319990808738SMark Brown if (ret != 0) 320090808738SMark Brown return ret; 320190808738SMark Brown 32028caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3203568d0697SDavid Brownell 32048caab75fSGeert Uytterhoeven if (ctlr->bus_lock_flag) 3205cf32b71eSErnst Schwab ret = -EBUSY; 3206cf32b71eSErnst Schwab else 3207cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3208568d0697SDavid Brownell 32098caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3210cf32b71eSErnst Schwab 3211cf32b71eSErnst Schwab return ret; 3212568d0697SDavid Brownell } 3213568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 3214568d0697SDavid Brownell 3215cf32b71eSErnst Schwab /** 3216cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 3217cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3218cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 3219cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 3220cf32b71eSErnst Schwab * 3221cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 3222cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 3223cf32b71eSErnst Schwab * 3224cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 3225cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 3226cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 3227cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 3228cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 3229cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 3230cf32b71eSErnst Schwab * core or controller driver code. 3231cf32b71eSErnst Schwab * 3232cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 3233cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 3234cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 3235cf32b71eSErnst Schwab * time requirements, for example. 3236cf32b71eSErnst Schwab * 3237cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 3238cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 3239cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 3240cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 3241cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 3242cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 324397d56dc6SJavier Martinez Canillas * 324497d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3245cf32b71eSErnst Schwab */ 3246cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message) 3247cf32b71eSErnst Schwab { 32488caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3249cf32b71eSErnst Schwab int ret; 3250cf32b71eSErnst Schwab unsigned long flags; 3251cf32b71eSErnst Schwab 325290808738SMark Brown ret = __spi_validate(spi, message); 325390808738SMark Brown if (ret != 0) 325490808738SMark Brown return ret; 325590808738SMark Brown 32568caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3257cf32b71eSErnst Schwab 3258cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3259cf32b71eSErnst Schwab 32608caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3261cf32b71eSErnst Schwab 3262cf32b71eSErnst Schwab return ret; 3263cf32b71eSErnst Schwab 3264cf32b71eSErnst Schwab } 3265cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked); 3266cf32b71eSErnst Schwab 32677d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 32687d077197SDavid Brownell 32698caab75fSGeert Uytterhoeven /* Utility methods for SPI protocol drivers, layered on 32707d077197SDavid Brownell * top of the core. Some other utility methods are defined as 32717d077197SDavid Brownell * inline functions. 32727d077197SDavid Brownell */ 32737d077197SDavid Brownell 32745d870c8eSAndrew Morton static void spi_complete(void *arg) 32755d870c8eSAndrew Morton { 32765d870c8eSAndrew Morton complete(arg); 32775d870c8eSAndrew Morton } 32785d870c8eSAndrew Morton 3279ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message) 3280cf32b71eSErnst Schwab { 3281cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 3282cf32b71eSErnst Schwab int status; 32838caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 32840461a414SMark Brown unsigned long flags; 32850461a414SMark Brown 32860461a414SMark Brown status = __spi_validate(spi, message); 32870461a414SMark Brown if (status != 0) 32880461a414SMark Brown return status; 3289cf32b71eSErnst Schwab 3290cf32b71eSErnst Schwab message->complete = spi_complete; 3291cf32b71eSErnst Schwab message->context = &done; 32920461a414SMark Brown message->spi = spi; 3293cf32b71eSErnst Schwab 32948caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); 3295eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); 3296eca2ebc7SMartin Sperl 32970461a414SMark Brown /* If we're not using the legacy transfer method then we will 32980461a414SMark Brown * try to transfer in the calling context so special case. 32990461a414SMark Brown * This code would be less tricky if we could remove the 33000461a414SMark Brown * support for driver implemented message queues. 33010461a414SMark Brown */ 33028caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 33038caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 33040461a414SMark Brown 33050461a414SMark Brown trace_spi_message_submit(message); 33060461a414SMark Brown 33070461a414SMark Brown status = __spi_queued_transfer(spi, message, false); 33080461a414SMark Brown 33098caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 33100461a414SMark Brown } else { 3311cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 33120461a414SMark Brown } 3313cf32b71eSErnst Schwab 3314cf32b71eSErnst Schwab if (status == 0) { 33150461a414SMark Brown /* Push out the messages in the calling context if we 33160461a414SMark Brown * can. 33170461a414SMark Brown */ 33188caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 33198caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3320eca2ebc7SMartin Sperl spi_sync_immediate); 3321eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, 3322eca2ebc7SMartin Sperl spi_sync_immediate); 33238caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, false); 3324eca2ebc7SMartin Sperl } 33250461a414SMark Brown 3326cf32b71eSErnst Schwab wait_for_completion(&done); 3327cf32b71eSErnst Schwab status = message->status; 3328cf32b71eSErnst Schwab } 3329cf32b71eSErnst Schwab message->context = NULL; 3330cf32b71eSErnst Schwab return status; 3331cf32b71eSErnst Schwab } 3332cf32b71eSErnst Schwab 33338ae12a0dSDavid Brownell /** 33348ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 33358ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 33368ae12a0dSDavid Brownell * @message: describes the data transfers 333733e34dc6SDavid Brownell * Context: can sleep 33388ae12a0dSDavid Brownell * 33398ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 33408ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 33418ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 33428ae12a0dSDavid Brownell * 33438ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 33448ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 33458ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 33468ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 33478ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 33488ae12a0dSDavid Brownell * 33490c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 33500c868461SDavid Brownell * message will not be freed before this call returns. 33510c868461SDavid Brownell * 335297d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 33538ae12a0dSDavid Brownell */ 33548ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 33558ae12a0dSDavid Brownell { 3356ef4d96ecSMark Brown int ret; 3357ef4d96ecSMark Brown 33588caab75fSGeert Uytterhoeven mutex_lock(&spi->controller->bus_lock_mutex); 3359ef4d96ecSMark Brown ret = __spi_sync(spi, message); 33608caab75fSGeert Uytterhoeven mutex_unlock(&spi->controller->bus_lock_mutex); 3361ef4d96ecSMark Brown 3362ef4d96ecSMark Brown return ret; 33638ae12a0dSDavid Brownell } 33648ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 33658ae12a0dSDavid Brownell 3366cf32b71eSErnst Schwab /** 3367cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 3368cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3369cf32b71eSErnst Schwab * @message: describes the data transfers 3370cf32b71eSErnst Schwab * Context: can sleep 3371cf32b71eSErnst Schwab * 3372cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3373cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 3374cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 3375cf32b71eSErnst Schwab * 3376cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 337725985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 3378cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 3379cf32b71eSErnst Schwab * 338097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3381cf32b71eSErnst Schwab */ 3382cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 3383cf32b71eSErnst Schwab { 3384ef4d96ecSMark Brown return __spi_sync(spi, message); 3385cf32b71eSErnst Schwab } 3386cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 3387cf32b71eSErnst Schwab 3388cf32b71eSErnst Schwab /** 3389cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 33908caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that should be locked for exclusive bus access 3391cf32b71eSErnst Schwab * Context: can sleep 3392cf32b71eSErnst Schwab * 3393cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3394cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3395cf32b71eSErnst Schwab * 3396cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 3397cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 3398cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 3399cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 3400cf32b71eSErnst Schwab * 340197d56dc6SJavier Martinez Canillas * Return: always zero. 3402cf32b71eSErnst Schwab */ 34038caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr) 3404cf32b71eSErnst Schwab { 3405cf32b71eSErnst Schwab unsigned long flags; 3406cf32b71eSErnst Schwab 34078caab75fSGeert Uytterhoeven mutex_lock(&ctlr->bus_lock_mutex); 3408cf32b71eSErnst Schwab 34098caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 34108caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 1; 34118caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3412cf32b71eSErnst Schwab 3413cf32b71eSErnst Schwab /* mutex remains locked until spi_bus_unlock is called */ 3414cf32b71eSErnst Schwab 3415cf32b71eSErnst Schwab return 0; 3416cf32b71eSErnst Schwab } 3417cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 3418cf32b71eSErnst Schwab 3419cf32b71eSErnst Schwab /** 3420cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 34218caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that was locked for exclusive bus access 3422cf32b71eSErnst Schwab * Context: can sleep 3423cf32b71eSErnst Schwab * 3424cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3425cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3426cf32b71eSErnst Schwab * 3427cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 3428cf32b71eSErnst Schwab * call. 3429cf32b71eSErnst Schwab * 343097d56dc6SJavier Martinez Canillas * Return: always zero. 3431cf32b71eSErnst Schwab */ 34328caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr) 3433cf32b71eSErnst Schwab { 34348caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 3435cf32b71eSErnst Schwab 34368caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->bus_lock_mutex); 3437cf32b71eSErnst Schwab 3438cf32b71eSErnst Schwab return 0; 3439cf32b71eSErnst Schwab } 3440cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 3441cf32b71eSErnst Schwab 3442a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 3443a9948b61SDavid Brownell #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 34448ae12a0dSDavid Brownell 34458ae12a0dSDavid Brownell static u8 *buf; 34468ae12a0dSDavid Brownell 34478ae12a0dSDavid Brownell /** 34488ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 34498ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 34508ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 34518ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 345227570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 345327570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 345433e34dc6SDavid Brownell * Context: can sleep 34558ae12a0dSDavid Brownell * 34568ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 34578ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 34588ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 3459b885244eSDavid Brownell * This call may only be used from a context that may sleep. 34608ae12a0dSDavid Brownell * 34610c868461SDavid Brownell * Parameters to this routine are always copied using a small buffer; 346233e34dc6SDavid Brownell * portable code should never use this for more than 32 bytes. 346333e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 34640c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 346597d56dc6SJavier Martinez Canillas * 346697d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 34678ae12a0dSDavid Brownell */ 34688ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 34690c4a1590SMark Brown const void *txbuf, unsigned n_tx, 34700c4a1590SMark Brown void *rxbuf, unsigned n_rx) 34718ae12a0dSDavid Brownell { 3472068f4070SDavid Brownell static DEFINE_MUTEX(lock); 34738ae12a0dSDavid Brownell 34748ae12a0dSDavid Brownell int status; 34758ae12a0dSDavid Brownell struct spi_message message; 3476bdff549eSDavid Brownell struct spi_transfer x[2]; 34778ae12a0dSDavid Brownell u8 *local_buf; 34788ae12a0dSDavid Brownell 3479b3a223eeSMark Brown /* Use preallocated DMA-safe buffer if we can. We can't avoid 3480b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 3481b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 3482b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 34838ae12a0dSDavid Brownell */ 3484b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 34852cd94c8aSMark Brown local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 34862cd94c8aSMark Brown GFP_KERNEL | GFP_DMA); 3487b3a223eeSMark Brown if (!local_buf) 3488b3a223eeSMark Brown return -ENOMEM; 3489b3a223eeSMark Brown } else { 3490b3a223eeSMark Brown local_buf = buf; 3491b3a223eeSMark Brown } 34928ae12a0dSDavid Brownell 34938275c642SVitaly Wool spi_message_init(&message); 34945fe5f05eSJingoo Han memset(x, 0, sizeof(x)); 3495bdff549eSDavid Brownell if (n_tx) { 3496bdff549eSDavid Brownell x[0].len = n_tx; 3497bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 3498bdff549eSDavid Brownell } 3499bdff549eSDavid Brownell if (n_rx) { 3500bdff549eSDavid Brownell x[1].len = n_rx; 3501bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 3502bdff549eSDavid Brownell } 35038275c642SVitaly Wool 35048ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 3505bdff549eSDavid Brownell x[0].tx_buf = local_buf; 3506bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 35078ae12a0dSDavid Brownell 35088ae12a0dSDavid Brownell /* do the i/o */ 35098ae12a0dSDavid Brownell status = spi_sync(spi, &message); 35109b938b74SMarc Pignat if (status == 0) 3511bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 35128ae12a0dSDavid Brownell 3513bdff549eSDavid Brownell if (x[0].tx_buf == buf) 3514068f4070SDavid Brownell mutex_unlock(&lock); 35158ae12a0dSDavid Brownell else 35168ae12a0dSDavid Brownell kfree(local_buf); 35178ae12a0dSDavid Brownell 35188ae12a0dSDavid Brownell return status; 35198ae12a0dSDavid Brownell } 35208ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 35218ae12a0dSDavid Brownell 35228ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 35238ae12a0dSDavid Brownell 35245f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF) 3525ce79d54aSPantelis Antoniou static int __spi_of_device_match(struct device *dev, void *data) 3526ce79d54aSPantelis Antoniou { 3527ce79d54aSPantelis Antoniou return dev->of_node == data; 3528ce79d54aSPantelis Antoniou } 3529ce79d54aSPantelis Antoniou 3530ce79d54aSPantelis Antoniou /* must call put_device() when done with returned spi_device device */ 35315f143af7SMarco Felsch struct spi_device *of_find_spi_device_by_node(struct device_node *node) 3532ce79d54aSPantelis Antoniou { 3533ce79d54aSPantelis Antoniou struct device *dev = bus_find_device(&spi_bus_type, NULL, node, 3534ce79d54aSPantelis Antoniou __spi_of_device_match); 3535ce79d54aSPantelis Antoniou return dev ? to_spi_device(dev) : NULL; 3536ce79d54aSPantelis Antoniou } 35375f143af7SMarco Felsch EXPORT_SYMBOL_GPL(of_find_spi_device_by_node); 35385f143af7SMarco Felsch #endif /* IS_ENABLED(CONFIG_OF) */ 3539ce79d54aSPantelis Antoniou 35405f143af7SMarco Felsch #if IS_ENABLED(CONFIG_OF_DYNAMIC) 35418caab75fSGeert Uytterhoeven static int __spi_of_controller_match(struct device *dev, const void *data) 3542ce79d54aSPantelis Antoniou { 3543ce79d54aSPantelis Antoniou return dev->of_node == data; 3544ce79d54aSPantelis Antoniou } 3545ce79d54aSPantelis Antoniou 35468caab75fSGeert Uytterhoeven /* the spi controllers are not using spi_bus, so we find it with another way */ 35478caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 3548ce79d54aSPantelis Antoniou { 3549ce79d54aSPantelis Antoniou struct device *dev; 3550ce79d54aSPantelis Antoniou 3551ce79d54aSPantelis Antoniou dev = class_find_device(&spi_master_class, NULL, node, 35528caab75fSGeert Uytterhoeven __spi_of_controller_match); 35536c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 35546c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, node, 35558caab75fSGeert Uytterhoeven __spi_of_controller_match); 3556ce79d54aSPantelis Antoniou if (!dev) 3557ce79d54aSPantelis Antoniou return NULL; 3558ce79d54aSPantelis Antoniou 3559ce79d54aSPantelis Antoniou /* reference got in class_find_device */ 35608caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 3561ce79d54aSPantelis Antoniou } 3562ce79d54aSPantelis Antoniou 3563ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action, 3564ce79d54aSPantelis Antoniou void *arg) 3565ce79d54aSPantelis Antoniou { 3566ce79d54aSPantelis Antoniou struct of_reconfig_data *rd = arg; 35678caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 3568ce79d54aSPantelis Antoniou struct spi_device *spi; 3569ce79d54aSPantelis Antoniou 3570ce79d54aSPantelis Antoniou switch (of_reconfig_get_state_change(action, arg)) { 3571ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 35728caab75fSGeert Uytterhoeven ctlr = of_find_spi_controller_by_node(rd->dn->parent); 35738caab75fSGeert Uytterhoeven if (ctlr == NULL) 3574ce79d54aSPantelis Antoniou return NOTIFY_OK; /* not for us */ 3575ce79d54aSPantelis Antoniou 3576bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 35778caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 3578bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 3579bd6c1644SGeert Uytterhoeven } 3580bd6c1644SGeert Uytterhoeven 35818caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, rd->dn); 35828caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 3583ce79d54aSPantelis Antoniou 3584ce79d54aSPantelis Antoniou if (IS_ERR(spi)) { 358525c56c88SRob Herring pr_err("%s: failed to create for '%pOF'\n", 358625c56c88SRob Herring __func__, rd->dn); 3587e0af98a7SRalf Ramsauer of_node_clear_flag(rd->dn, OF_POPULATED); 3588ce79d54aSPantelis Antoniou return notifier_from_errno(PTR_ERR(spi)); 3589ce79d54aSPantelis Antoniou } 3590ce79d54aSPantelis Antoniou break; 3591ce79d54aSPantelis Antoniou 3592ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 3593bd6c1644SGeert Uytterhoeven /* already depopulated? */ 3594bd6c1644SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 3595bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 3596bd6c1644SGeert Uytterhoeven 3597ce79d54aSPantelis Antoniou /* find our device by node */ 3598ce79d54aSPantelis Antoniou spi = of_find_spi_device_by_node(rd->dn); 3599ce79d54aSPantelis Antoniou if (spi == NULL) 3600ce79d54aSPantelis Antoniou return NOTIFY_OK; /* no? not meant for us */ 3601ce79d54aSPantelis Antoniou 3602ce79d54aSPantelis Antoniou /* unregister takes one ref away */ 3603ce79d54aSPantelis Antoniou spi_unregister_device(spi); 3604ce79d54aSPantelis Antoniou 3605ce79d54aSPantelis Antoniou /* and put the reference of the find */ 3606ce79d54aSPantelis Antoniou put_device(&spi->dev); 3607ce79d54aSPantelis Antoniou break; 3608ce79d54aSPantelis Antoniou } 3609ce79d54aSPantelis Antoniou 3610ce79d54aSPantelis Antoniou return NOTIFY_OK; 3611ce79d54aSPantelis Antoniou } 3612ce79d54aSPantelis Antoniou 3613ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = { 3614ce79d54aSPantelis Antoniou .notifier_call = of_spi_notify, 3615ce79d54aSPantelis Antoniou }; 3616ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 3617ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier; 3618ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 3619ce79d54aSPantelis Antoniou 36207f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI) 36218caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data) 36227f24467fSOctavian Purdila { 36237f24467fSOctavian Purdila return ACPI_COMPANION(dev->parent) == data; 36247f24467fSOctavian Purdila } 36257f24467fSOctavian Purdila 36267f24467fSOctavian Purdila static int spi_acpi_device_match(struct device *dev, void *data) 36277f24467fSOctavian Purdila { 36287f24467fSOctavian Purdila return ACPI_COMPANION(dev) == data; 36297f24467fSOctavian Purdila } 36307f24467fSOctavian Purdila 36318caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 36327f24467fSOctavian Purdila { 36337f24467fSOctavian Purdila struct device *dev; 36347f24467fSOctavian Purdila 36357f24467fSOctavian Purdila dev = class_find_device(&spi_master_class, NULL, adev, 36368caab75fSGeert Uytterhoeven spi_acpi_controller_match); 36376c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 36386c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, adev, 36398caab75fSGeert Uytterhoeven spi_acpi_controller_match); 36407f24467fSOctavian Purdila if (!dev) 36417f24467fSOctavian Purdila return NULL; 36427f24467fSOctavian Purdila 36438caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 36447f24467fSOctavian Purdila } 36457f24467fSOctavian Purdila 36467f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 36477f24467fSOctavian Purdila { 36487f24467fSOctavian Purdila struct device *dev; 36497f24467fSOctavian Purdila 36507f24467fSOctavian Purdila dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match); 36517f24467fSOctavian Purdila 36527f24467fSOctavian Purdila return dev ? to_spi_device(dev) : NULL; 36537f24467fSOctavian Purdila } 36547f24467fSOctavian Purdila 36557f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 36567f24467fSOctavian Purdila void *arg) 36577f24467fSOctavian Purdila { 36587f24467fSOctavian Purdila struct acpi_device *adev = arg; 36598caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 36607f24467fSOctavian Purdila struct spi_device *spi; 36617f24467fSOctavian Purdila 36627f24467fSOctavian Purdila switch (value) { 36637f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_ADD: 36648caab75fSGeert Uytterhoeven ctlr = acpi_spi_find_controller_by_adev(adev->parent); 36658caab75fSGeert Uytterhoeven if (!ctlr) 36667f24467fSOctavian Purdila break; 36677f24467fSOctavian Purdila 36688caab75fSGeert Uytterhoeven acpi_register_spi_device(ctlr, adev); 36698caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 36707f24467fSOctavian Purdila break; 36717f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_REMOVE: 36727f24467fSOctavian Purdila if (!acpi_device_enumerated(adev)) 36737f24467fSOctavian Purdila break; 36747f24467fSOctavian Purdila 36757f24467fSOctavian Purdila spi = acpi_spi_find_device_by_adev(adev); 36767f24467fSOctavian Purdila if (!spi) 36777f24467fSOctavian Purdila break; 36787f24467fSOctavian Purdila 36797f24467fSOctavian Purdila spi_unregister_device(spi); 36807f24467fSOctavian Purdila put_device(&spi->dev); 36817f24467fSOctavian Purdila break; 36827f24467fSOctavian Purdila } 36837f24467fSOctavian Purdila 36847f24467fSOctavian Purdila return NOTIFY_OK; 36857f24467fSOctavian Purdila } 36867f24467fSOctavian Purdila 36877f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = { 36887f24467fSOctavian Purdila .notifier_call = acpi_spi_notify, 36897f24467fSOctavian Purdila }; 36907f24467fSOctavian Purdila #else 36917f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier; 36927f24467fSOctavian Purdila #endif 36937f24467fSOctavian Purdila 36948ae12a0dSDavid Brownell static int __init spi_init(void) 36958ae12a0dSDavid Brownell { 3696b885244eSDavid Brownell int status; 36978ae12a0dSDavid Brownell 3698e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 3699b885244eSDavid Brownell if (!buf) { 3700b885244eSDavid Brownell status = -ENOMEM; 3701b885244eSDavid Brownell goto err0; 37028ae12a0dSDavid Brownell } 3703b885244eSDavid Brownell 3704b885244eSDavid Brownell status = bus_register(&spi_bus_type); 3705b885244eSDavid Brownell if (status < 0) 3706b885244eSDavid Brownell goto err1; 3707b885244eSDavid Brownell 3708b885244eSDavid Brownell status = class_register(&spi_master_class); 3709b885244eSDavid Brownell if (status < 0) 3710b885244eSDavid Brownell goto err2; 3711ce79d54aSPantelis Antoniou 37126c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 37136c364062SGeert Uytterhoeven status = class_register(&spi_slave_class); 37146c364062SGeert Uytterhoeven if (status < 0) 37156c364062SGeert Uytterhoeven goto err3; 37166c364062SGeert Uytterhoeven } 37176c364062SGeert Uytterhoeven 37185267720eSFabio Estevam if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 3719ce79d54aSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 37207f24467fSOctavian Purdila if (IS_ENABLED(CONFIG_ACPI)) 37217f24467fSOctavian Purdila WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 3722ce79d54aSPantelis Antoniou 3723b885244eSDavid Brownell return 0; 3724b885244eSDavid Brownell 37256c364062SGeert Uytterhoeven err3: 37266c364062SGeert Uytterhoeven class_unregister(&spi_master_class); 3727b885244eSDavid Brownell err2: 3728b885244eSDavid Brownell bus_unregister(&spi_bus_type); 3729b885244eSDavid Brownell err1: 3730b885244eSDavid Brownell kfree(buf); 3731b885244eSDavid Brownell buf = NULL; 3732b885244eSDavid Brownell err0: 3733b885244eSDavid Brownell return status; 3734b885244eSDavid Brownell } 3735b885244eSDavid Brownell 37368ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(), 37378ae12a0dSDavid Brownell * but even essential drivers wait till later 3738b885244eSDavid Brownell * 3739b885244eSDavid Brownell * REVISIT only boardinfo really needs static linking. the rest (device and 3740b885244eSDavid Brownell * driver registration) _could_ be dynamically linked (modular) ... costs 3741b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 37428ae12a0dSDavid Brownell */ 3743673c0c00SDavid Brownell postcore_initcall(spi_init); 3744f0125f1aSMark Brown 3745