18ae12a0dSDavid Brownell /* 2ca632f55SGrant Likely * SPI init/core code 38ae12a0dSDavid Brownell * 48ae12a0dSDavid Brownell * Copyright (C) 2005 David Brownell 5d57a4282SGrant Likely * Copyright (C) 2008 Secret Lab Technologies Ltd. 68ae12a0dSDavid Brownell * 78ae12a0dSDavid Brownell * This program is free software; you can redistribute it and/or modify 88ae12a0dSDavid Brownell * it under the terms of the GNU General Public License as published by 98ae12a0dSDavid Brownell * the Free Software Foundation; either version 2 of the License, or 108ae12a0dSDavid Brownell * (at your option) any later version. 118ae12a0dSDavid Brownell * 128ae12a0dSDavid Brownell * This program is distributed in the hope that it will be useful, 138ae12a0dSDavid Brownell * but WITHOUT ANY WARRANTY; without even the implied warranty of 148ae12a0dSDavid Brownell * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 158ae12a0dSDavid Brownell * GNU General Public License for more details. 168ae12a0dSDavid Brownell */ 178ae12a0dSDavid Brownell 188ae12a0dSDavid Brownell #include <linux/kernel.h> 198ae12a0dSDavid Brownell #include <linux/device.h> 208ae12a0dSDavid Brownell #include <linux/init.h> 218ae12a0dSDavid Brownell #include <linux/cache.h> 2299adef31SMark Brown #include <linux/dma-mapping.h> 2399adef31SMark Brown #include <linux/dmaengine.h> 2494040828SMatthias Kaehlcke #include <linux/mutex.h> 252b7a32f7SSinan Akman #include <linux/of_device.h> 26d57a4282SGrant Likely #include <linux/of_irq.h> 2786be408bSSylwester Nawrocki #include <linux/clk/clk-conf.h> 285a0e3ad6STejun Heo #include <linux/slab.h> 29e0626e38SAnton Vorontsov #include <linux/mod_devicetable.h> 308ae12a0dSDavid Brownell #include <linux/spi/spi.h> 31*b5932f5cSBoris Brezillon #include <linux/spi/spi-mem.h> 3274317984SJean-Christophe PLAGNIOL-VILLARD #include <linux/of_gpio.h> 333ae22e8cSMark Brown #include <linux/pm_runtime.h> 34f48c767cSUlf Hansson #include <linux/pm_domain.h> 35826cf175SDmitry Torokhov #include <linux/property.h> 36025ed130SPaul Gortmaker #include <linux/export.h> 378bd75c77SClark Williams #include <linux/sched/rt.h> 38ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h> 39ffbbdd21SLinus Walleij #include <linux/delay.h> 40ffbbdd21SLinus Walleij #include <linux/kthread.h> 4164bee4d2SMika Westerberg #include <linux/ioport.h> 4264bee4d2SMika Westerberg #include <linux/acpi.h> 43b1b8153cSVignesh R #include <linux/highmem.h> 449b61e302SSuniel Mahesh #include <linux/idr.h> 458a2e487eSLukas Wunner #include <linux/platform_data/x86/apple.h> 468ae12a0dSDavid Brownell 4756ec1978SMark Brown #define CREATE_TRACE_POINTS 4856ec1978SMark Brown #include <trace/events/spi.h> 499b61e302SSuniel Mahesh 5046336966SBoris Brezillon #include "internals.h" 5146336966SBoris Brezillon 529b61e302SSuniel Mahesh static DEFINE_IDR(spi_master_idr); 5356ec1978SMark Brown 548ae12a0dSDavid Brownell static void spidev_release(struct device *dev) 558ae12a0dSDavid Brownell { 560ffa0285SHans-Peter Nilsson struct spi_device *spi = to_spi_device(dev); 578ae12a0dSDavid Brownell 588caab75fSGeert Uytterhoeven /* spi controllers may cleanup for released devices */ 598caab75fSGeert Uytterhoeven if (spi->controller->cleanup) 608caab75fSGeert Uytterhoeven spi->controller->cleanup(spi); 618ae12a0dSDavid Brownell 628caab75fSGeert Uytterhoeven spi_controller_put(spi->controller); 6307a389feSRoman Tereshonkov kfree(spi); 648ae12a0dSDavid Brownell } 658ae12a0dSDavid Brownell 668ae12a0dSDavid Brownell static ssize_t 678ae12a0dSDavid Brownell modalias_show(struct device *dev, struct device_attribute *a, char *buf) 688ae12a0dSDavid Brownell { 698ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 708c4ff6d0SZhang Rui int len; 718c4ff6d0SZhang Rui 728c4ff6d0SZhang Rui len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); 738c4ff6d0SZhang Rui if (len != -ENODEV) 748c4ff6d0SZhang Rui return len; 758ae12a0dSDavid Brownell 76d8e328b3SGrant Likely return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); 778ae12a0dSDavid Brownell } 78aa7da564SGreg Kroah-Hartman static DEVICE_ATTR_RO(modalias); 798ae12a0dSDavid Brownell 80eca2ebc7SMartin Sperl #define SPI_STATISTICS_ATTRS(field, file) \ 818caab75fSGeert Uytterhoeven static ssize_t spi_controller_##field##_show(struct device *dev, \ 82eca2ebc7SMartin Sperl struct device_attribute *attr, \ 83eca2ebc7SMartin Sperl char *buf) \ 84eca2ebc7SMartin Sperl { \ 858caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, \ 868caab75fSGeert Uytterhoeven struct spi_controller, dev); \ 878caab75fSGeert Uytterhoeven return spi_statistics_##field##_show(&ctlr->statistics, buf); \ 88eca2ebc7SMartin Sperl } \ 898caab75fSGeert Uytterhoeven static struct device_attribute dev_attr_spi_controller_##field = { \ 90ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 918caab75fSGeert Uytterhoeven .show = spi_controller_##field##_show, \ 92eca2ebc7SMartin Sperl }; \ 93eca2ebc7SMartin Sperl static ssize_t spi_device_##field##_show(struct device *dev, \ 94eca2ebc7SMartin Sperl struct device_attribute *attr, \ 95eca2ebc7SMartin Sperl char *buf) \ 96eca2ebc7SMartin Sperl { \ 97d1eba93bSGeliang Tang struct spi_device *spi = to_spi_device(dev); \ 98eca2ebc7SMartin Sperl return spi_statistics_##field##_show(&spi->statistics, buf); \ 99eca2ebc7SMartin Sperl } \ 100eca2ebc7SMartin Sperl static struct device_attribute dev_attr_spi_device_##field = { \ 101ad25c92eSGeert Uytterhoeven .attr = { .name = file, .mode = 0444 }, \ 102eca2ebc7SMartin Sperl .show = spi_device_##field##_show, \ 103eca2ebc7SMartin Sperl } 104eca2ebc7SMartin Sperl 105eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string) \ 106eca2ebc7SMartin Sperl static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \ 107eca2ebc7SMartin Sperl char *buf) \ 108eca2ebc7SMartin Sperl { \ 109eca2ebc7SMartin Sperl unsigned long flags; \ 110eca2ebc7SMartin Sperl ssize_t len; \ 111eca2ebc7SMartin Sperl spin_lock_irqsave(&stat->lock, flags); \ 112eca2ebc7SMartin Sperl len = sprintf(buf, format_string, stat->field); \ 113eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stat->lock, flags); \ 114eca2ebc7SMartin Sperl return len; \ 115eca2ebc7SMartin Sperl } \ 116eca2ebc7SMartin Sperl SPI_STATISTICS_ATTRS(name, file) 117eca2ebc7SMartin Sperl 118eca2ebc7SMartin Sperl #define SPI_STATISTICS_SHOW(field, format_string) \ 119eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW_NAME(field, __stringify(field), \ 120eca2ebc7SMartin Sperl field, format_string) 121eca2ebc7SMartin Sperl 122eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(messages, "%lu"); 123eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(transfers, "%lu"); 124eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(errors, "%lu"); 125eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(timedout, "%lu"); 126eca2ebc7SMartin Sperl 127eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync, "%lu"); 128eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu"); 129eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(spi_async, "%lu"); 130eca2ebc7SMartin Sperl 131eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes, "%llu"); 132eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_rx, "%llu"); 133eca2ebc7SMartin Sperl SPI_STATISTICS_SHOW(bytes_tx, "%llu"); 134eca2ebc7SMartin Sperl 1356b7bc061SMartin Sperl #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number) \ 1366b7bc061SMartin Sperl SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index, \ 1376b7bc061SMartin Sperl "transfer_bytes_histo_" number, \ 1386b7bc061SMartin Sperl transfer_bytes_histo[index], "%lu") 1396b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(0, "0-1"); 1406b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(1, "2-3"); 1416b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(2, "4-7"); 1426b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(3, "8-15"); 1436b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(4, "16-31"); 1446b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(5, "32-63"); 1456b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(6, "64-127"); 1466b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(7, "128-255"); 1476b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(8, "256-511"); 1486b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(9, "512-1023"); 1496b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047"); 1506b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095"); 1516b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191"); 1526b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383"); 1536b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767"); 1546b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535"); 1556b7bc061SMartin Sperl SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+"); 1566b7bc061SMartin Sperl 157d9f12122SMartin Sperl SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu"); 158d9f12122SMartin Sperl 159aa7da564SGreg Kroah-Hartman static struct attribute *spi_dev_attrs[] = { 160aa7da564SGreg Kroah-Hartman &dev_attr_modalias.attr, 161aa7da564SGreg Kroah-Hartman NULL, 1628ae12a0dSDavid Brownell }; 163eca2ebc7SMartin Sperl 164eca2ebc7SMartin Sperl static const struct attribute_group spi_dev_group = { 165eca2ebc7SMartin Sperl .attrs = spi_dev_attrs, 166eca2ebc7SMartin Sperl }; 167eca2ebc7SMartin Sperl 168eca2ebc7SMartin Sperl static struct attribute *spi_device_statistics_attrs[] = { 169eca2ebc7SMartin Sperl &dev_attr_spi_device_messages.attr, 170eca2ebc7SMartin Sperl &dev_attr_spi_device_transfers.attr, 171eca2ebc7SMartin Sperl &dev_attr_spi_device_errors.attr, 172eca2ebc7SMartin Sperl &dev_attr_spi_device_timedout.attr, 173eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync.attr, 174eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_sync_immediate.attr, 175eca2ebc7SMartin Sperl &dev_attr_spi_device_spi_async.attr, 176eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes.attr, 177eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_rx.attr, 178eca2ebc7SMartin Sperl &dev_attr_spi_device_bytes_tx.attr, 1796b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo0.attr, 1806b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo1.attr, 1816b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo2.attr, 1826b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo3.attr, 1836b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo4.attr, 1846b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo5.attr, 1856b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo6.attr, 1866b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo7.attr, 1876b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo8.attr, 1886b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo9.attr, 1896b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo10.attr, 1906b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo11.attr, 1916b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo12.attr, 1926b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo13.attr, 1936b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo14.attr, 1946b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo15.attr, 1956b7bc061SMartin Sperl &dev_attr_spi_device_transfer_bytes_histo16.attr, 196d9f12122SMartin Sperl &dev_attr_spi_device_transfers_split_maxsize.attr, 197eca2ebc7SMartin Sperl NULL, 198eca2ebc7SMartin Sperl }; 199eca2ebc7SMartin Sperl 200eca2ebc7SMartin Sperl static const struct attribute_group spi_device_statistics_group = { 201eca2ebc7SMartin Sperl .name = "statistics", 202eca2ebc7SMartin Sperl .attrs = spi_device_statistics_attrs, 203eca2ebc7SMartin Sperl }; 204eca2ebc7SMartin Sperl 205eca2ebc7SMartin Sperl static const struct attribute_group *spi_dev_groups[] = { 206eca2ebc7SMartin Sperl &spi_dev_group, 207eca2ebc7SMartin Sperl &spi_device_statistics_group, 208eca2ebc7SMartin Sperl NULL, 209eca2ebc7SMartin Sperl }; 210eca2ebc7SMartin Sperl 2118caab75fSGeert Uytterhoeven static struct attribute *spi_controller_statistics_attrs[] = { 2128caab75fSGeert Uytterhoeven &dev_attr_spi_controller_messages.attr, 2138caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers.attr, 2148caab75fSGeert Uytterhoeven &dev_attr_spi_controller_errors.attr, 2158caab75fSGeert Uytterhoeven &dev_attr_spi_controller_timedout.attr, 2168caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync.attr, 2178caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_sync_immediate.attr, 2188caab75fSGeert Uytterhoeven &dev_attr_spi_controller_spi_async.attr, 2198caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes.attr, 2208caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_rx.attr, 2218caab75fSGeert Uytterhoeven &dev_attr_spi_controller_bytes_tx.attr, 2228caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo0.attr, 2238caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo1.attr, 2248caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo2.attr, 2258caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo3.attr, 2268caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo4.attr, 2278caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo5.attr, 2288caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo6.attr, 2298caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo7.attr, 2308caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo8.attr, 2318caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo9.attr, 2328caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo10.attr, 2338caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo11.attr, 2348caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo12.attr, 2358caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo13.attr, 2368caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo14.attr, 2378caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo15.attr, 2388caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfer_bytes_histo16.attr, 2398caab75fSGeert Uytterhoeven &dev_attr_spi_controller_transfers_split_maxsize.attr, 240eca2ebc7SMartin Sperl NULL, 241eca2ebc7SMartin Sperl }; 242eca2ebc7SMartin Sperl 2438caab75fSGeert Uytterhoeven static const struct attribute_group spi_controller_statistics_group = { 244eca2ebc7SMartin Sperl .name = "statistics", 2458caab75fSGeert Uytterhoeven .attrs = spi_controller_statistics_attrs, 246eca2ebc7SMartin Sperl }; 247eca2ebc7SMartin Sperl 248eca2ebc7SMartin Sperl static const struct attribute_group *spi_master_groups[] = { 2498caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 250eca2ebc7SMartin Sperl NULL, 251eca2ebc7SMartin Sperl }; 252eca2ebc7SMartin Sperl 253eca2ebc7SMartin Sperl void spi_statistics_add_transfer_stats(struct spi_statistics *stats, 254eca2ebc7SMartin Sperl struct spi_transfer *xfer, 2558caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 256eca2ebc7SMartin Sperl { 257eca2ebc7SMartin Sperl unsigned long flags; 2586b7bc061SMartin Sperl int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1; 2596b7bc061SMartin Sperl 2606b7bc061SMartin Sperl if (l2len < 0) 2616b7bc061SMartin Sperl l2len = 0; 262eca2ebc7SMartin Sperl 263eca2ebc7SMartin Sperl spin_lock_irqsave(&stats->lock, flags); 264eca2ebc7SMartin Sperl 265eca2ebc7SMartin Sperl stats->transfers++; 2666b7bc061SMartin Sperl stats->transfer_bytes_histo[l2len]++; 267eca2ebc7SMartin Sperl 268eca2ebc7SMartin Sperl stats->bytes += xfer->len; 269eca2ebc7SMartin Sperl if ((xfer->tx_buf) && 2708caab75fSGeert Uytterhoeven (xfer->tx_buf != ctlr->dummy_tx)) 271eca2ebc7SMartin Sperl stats->bytes_tx += xfer->len; 272eca2ebc7SMartin Sperl if ((xfer->rx_buf) && 2738caab75fSGeert Uytterhoeven (xfer->rx_buf != ctlr->dummy_rx)) 274eca2ebc7SMartin Sperl stats->bytes_rx += xfer->len; 275eca2ebc7SMartin Sperl 276eca2ebc7SMartin Sperl spin_unlock_irqrestore(&stats->lock, flags); 277eca2ebc7SMartin Sperl } 278eca2ebc7SMartin Sperl EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats); 2798ae12a0dSDavid Brownell 2808ae12a0dSDavid Brownell /* modalias support makes "modprobe $MODALIAS" new-style hotplug work, 2818ae12a0dSDavid Brownell * and the sysfs version makes coldplug work too. 2828ae12a0dSDavid Brownell */ 2838ae12a0dSDavid Brownell 28475368bf6SAnton Vorontsov static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, 28575368bf6SAnton Vorontsov const struct spi_device *sdev) 28675368bf6SAnton Vorontsov { 28775368bf6SAnton Vorontsov while (id->name[0]) { 28875368bf6SAnton Vorontsov if (!strcmp(sdev->modalias, id->name)) 28975368bf6SAnton Vorontsov return id; 29075368bf6SAnton Vorontsov id++; 29175368bf6SAnton Vorontsov } 29275368bf6SAnton Vorontsov return NULL; 29375368bf6SAnton Vorontsov } 29475368bf6SAnton Vorontsov 29575368bf6SAnton Vorontsov const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 29675368bf6SAnton Vorontsov { 29775368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver); 29875368bf6SAnton Vorontsov 29975368bf6SAnton Vorontsov return spi_match_id(sdrv->id_table, sdev); 30075368bf6SAnton Vorontsov } 30175368bf6SAnton Vorontsov EXPORT_SYMBOL_GPL(spi_get_device_id); 30275368bf6SAnton Vorontsov 3038ae12a0dSDavid Brownell static int spi_match_device(struct device *dev, struct device_driver *drv) 3048ae12a0dSDavid Brownell { 3058ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 30675368bf6SAnton Vorontsov const struct spi_driver *sdrv = to_spi_driver(drv); 30775368bf6SAnton Vorontsov 3082b7a32f7SSinan Akman /* Attempt an OF style match */ 3092b7a32f7SSinan Akman if (of_driver_match_device(dev, drv)) 3102b7a32f7SSinan Akman return 1; 3112b7a32f7SSinan Akman 31264bee4d2SMika Westerberg /* Then try ACPI */ 31364bee4d2SMika Westerberg if (acpi_driver_match_device(dev, drv)) 31464bee4d2SMika Westerberg return 1; 31564bee4d2SMika Westerberg 31675368bf6SAnton Vorontsov if (sdrv->id_table) 31775368bf6SAnton Vorontsov return !!spi_match_id(sdrv->id_table, spi); 3188ae12a0dSDavid Brownell 31935f74fcaSKay Sievers return strcmp(spi->modalias, drv->name) == 0; 3208ae12a0dSDavid Brownell } 3218ae12a0dSDavid Brownell 3227eff2e7aSKay Sievers static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 3238ae12a0dSDavid Brownell { 3248ae12a0dSDavid Brownell const struct spi_device *spi = to_spi_device(dev); 3258c4ff6d0SZhang Rui int rc; 3268c4ff6d0SZhang Rui 3278c4ff6d0SZhang Rui rc = acpi_device_uevent_modalias(dev, env); 3288c4ff6d0SZhang Rui if (rc != -ENODEV) 3298c4ff6d0SZhang Rui return rc; 3308ae12a0dSDavid Brownell 3312856670fSAndy Shevchenko return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); 3328ae12a0dSDavid Brownell } 3338ae12a0dSDavid Brownell 3348ae12a0dSDavid Brownell struct bus_type spi_bus_type = { 3358ae12a0dSDavid Brownell .name = "spi", 336aa7da564SGreg Kroah-Hartman .dev_groups = spi_dev_groups, 3378ae12a0dSDavid Brownell .match = spi_match_device, 3388ae12a0dSDavid Brownell .uevent = spi_uevent, 3398ae12a0dSDavid Brownell }; 3408ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_bus_type); 3418ae12a0dSDavid Brownell 342b885244eSDavid Brownell 343b885244eSDavid Brownell static int spi_drv_probe(struct device *dev) 344b885244eSDavid Brownell { 345b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 34644af7927SJon Hunter struct spi_device *spi = to_spi_device(dev); 34733cf00e5SMika Westerberg int ret; 348b885244eSDavid Brownell 34986be408bSSylwester Nawrocki ret = of_clk_set_defaults(dev->of_node, false); 35086be408bSSylwester Nawrocki if (ret) 35186be408bSSylwester Nawrocki return ret; 35286be408bSSylwester Nawrocki 35344af7927SJon Hunter if (dev->of_node) { 35444af7927SJon Hunter spi->irq = of_irq_get(dev->of_node, 0); 35544af7927SJon Hunter if (spi->irq == -EPROBE_DEFER) 35644af7927SJon Hunter return -EPROBE_DEFER; 35744af7927SJon Hunter if (spi->irq < 0) 35844af7927SJon Hunter spi->irq = 0; 35944af7927SJon Hunter } 36044af7927SJon Hunter 361676e7c25SUlf Hansson ret = dev_pm_domain_attach(dev, true); 362676e7c25SUlf Hansson if (ret != -EPROBE_DEFER) { 36344af7927SJon Hunter ret = sdrv->probe(spi); 36433cf00e5SMika Westerberg if (ret) 365676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 366676e7c25SUlf Hansson } 36733cf00e5SMika Westerberg 36833cf00e5SMika Westerberg return ret; 369b885244eSDavid Brownell } 370b885244eSDavid Brownell 371b885244eSDavid Brownell static int spi_drv_remove(struct device *dev) 372b885244eSDavid Brownell { 373b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 37433cf00e5SMika Westerberg int ret; 375b885244eSDavid Brownell 376aec35f4eSJean Delvare ret = sdrv->remove(to_spi_device(dev)); 377676e7c25SUlf Hansson dev_pm_domain_detach(dev, true); 37833cf00e5SMika Westerberg 37933cf00e5SMika Westerberg return ret; 380b885244eSDavid Brownell } 381b885244eSDavid Brownell 382b885244eSDavid Brownell static void spi_drv_shutdown(struct device *dev) 383b885244eSDavid Brownell { 384b885244eSDavid Brownell const struct spi_driver *sdrv = to_spi_driver(dev->driver); 385b885244eSDavid Brownell 386b885244eSDavid Brownell sdrv->shutdown(to_spi_device(dev)); 387b885244eSDavid Brownell } 388b885244eSDavid Brownell 38933e34dc6SDavid Brownell /** 390ca5d2485SAndrew F. Davis * __spi_register_driver - register a SPI driver 39188c9321dSThierry Reding * @owner: owner module of the driver to register 39233e34dc6SDavid Brownell * @sdrv: the driver to register 39333e34dc6SDavid Brownell * Context: can sleep 39497d56dc6SJavier Martinez Canillas * 39597d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 39633e34dc6SDavid Brownell */ 397ca5d2485SAndrew F. Davis int __spi_register_driver(struct module *owner, struct spi_driver *sdrv) 398b885244eSDavid Brownell { 399ca5d2485SAndrew F. Davis sdrv->driver.owner = owner; 400b885244eSDavid Brownell sdrv->driver.bus = &spi_bus_type; 401b885244eSDavid Brownell if (sdrv->probe) 402b885244eSDavid Brownell sdrv->driver.probe = spi_drv_probe; 403b885244eSDavid Brownell if (sdrv->remove) 404b885244eSDavid Brownell sdrv->driver.remove = spi_drv_remove; 405b885244eSDavid Brownell if (sdrv->shutdown) 406b885244eSDavid Brownell sdrv->driver.shutdown = spi_drv_shutdown; 407b885244eSDavid Brownell return driver_register(&sdrv->driver); 408b885244eSDavid Brownell } 409ca5d2485SAndrew F. Davis EXPORT_SYMBOL_GPL(__spi_register_driver); 410b885244eSDavid Brownell 4118ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 4128ae12a0dSDavid Brownell 4138ae12a0dSDavid Brownell /* SPI devices should normally not be created by SPI device drivers; that 4148caab75fSGeert Uytterhoeven * would make them board-specific. Similarly with SPI controller drivers. 4158ae12a0dSDavid Brownell * Device registration normally goes into like arch/.../mach.../board-YYY.c 4168ae12a0dSDavid Brownell * with other readonly (flashable) information about mainboard devices. 4178ae12a0dSDavid Brownell */ 4188ae12a0dSDavid Brownell 4198ae12a0dSDavid Brownell struct boardinfo { 4208ae12a0dSDavid Brownell struct list_head list; 4212b9603a0SFeng Tang struct spi_board_info board_info; 4228ae12a0dSDavid Brownell }; 4238ae12a0dSDavid Brownell 4248ae12a0dSDavid Brownell static LIST_HEAD(board_list); 4258caab75fSGeert Uytterhoeven static LIST_HEAD(spi_controller_list); 4262b9603a0SFeng Tang 4272b9603a0SFeng Tang /* 4282b9603a0SFeng Tang * Used to protect add/del opertion for board_info list and 4298caab75fSGeert Uytterhoeven * spi_controller list, and their matching process 4309b61e302SSuniel Mahesh * also used to protect object of type struct idr 4312b9603a0SFeng Tang */ 43294040828SMatthias Kaehlcke static DEFINE_MUTEX(board_lock); 4338ae12a0dSDavid Brownell 434dc87c98eSGrant Likely /** 435dc87c98eSGrant Likely * spi_alloc_device - Allocate a new SPI device 4368caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 437dc87c98eSGrant Likely * Context: can sleep 438dc87c98eSGrant Likely * 439dc87c98eSGrant Likely * Allows a driver to allocate and initialize a spi_device without 440dc87c98eSGrant Likely * registering it immediately. This allows a driver to directly 441dc87c98eSGrant Likely * fill the spi_device with device parameters before calling 442dc87c98eSGrant Likely * spi_add_device() on it. 443dc87c98eSGrant Likely * 444dc87c98eSGrant Likely * Caller is responsible to call spi_add_device() on the returned 4458caab75fSGeert Uytterhoeven * spi_device structure to add it to the SPI controller. If the caller 446dc87c98eSGrant Likely * needs to discard the spi_device without adding it, then it should 447dc87c98eSGrant Likely * call spi_dev_put() on it. 448dc87c98eSGrant Likely * 44997d56dc6SJavier Martinez Canillas * Return: a pointer to the new device, or NULL. 450dc87c98eSGrant Likely */ 4518caab75fSGeert Uytterhoeven struct spi_device *spi_alloc_device(struct spi_controller *ctlr) 452dc87c98eSGrant Likely { 453dc87c98eSGrant Likely struct spi_device *spi; 454dc87c98eSGrant Likely 4558caab75fSGeert Uytterhoeven if (!spi_controller_get(ctlr)) 456dc87c98eSGrant Likely return NULL; 457dc87c98eSGrant Likely 4585fe5f05eSJingoo Han spi = kzalloc(sizeof(*spi), GFP_KERNEL); 459dc87c98eSGrant Likely if (!spi) { 4608caab75fSGeert Uytterhoeven spi_controller_put(ctlr); 461dc87c98eSGrant Likely return NULL; 462dc87c98eSGrant Likely } 463dc87c98eSGrant Likely 4648caab75fSGeert Uytterhoeven spi->master = spi->controller = ctlr; 4658caab75fSGeert Uytterhoeven spi->dev.parent = &ctlr->dev; 466dc87c98eSGrant Likely spi->dev.bus = &spi_bus_type; 467dc87c98eSGrant Likely spi->dev.release = spidev_release; 468446411e1SAndreas Larsson spi->cs_gpio = -ENOENT; 469eca2ebc7SMartin Sperl 470eca2ebc7SMartin Sperl spin_lock_init(&spi->statistics.lock); 471eca2ebc7SMartin Sperl 472dc87c98eSGrant Likely device_initialize(&spi->dev); 473dc87c98eSGrant Likely return spi; 474dc87c98eSGrant Likely } 475dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_alloc_device); 476dc87c98eSGrant Likely 477e13ac47bSJarkko Nikula static void spi_dev_set_name(struct spi_device *spi) 478e13ac47bSJarkko Nikula { 479e13ac47bSJarkko Nikula struct acpi_device *adev = ACPI_COMPANION(&spi->dev); 480e13ac47bSJarkko Nikula 481e13ac47bSJarkko Nikula if (adev) { 482e13ac47bSJarkko Nikula dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev)); 483e13ac47bSJarkko Nikula return; 484e13ac47bSJarkko Nikula } 485e13ac47bSJarkko Nikula 4868caab75fSGeert Uytterhoeven dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev), 487e13ac47bSJarkko Nikula spi->chip_select); 488e13ac47bSJarkko Nikula } 489e13ac47bSJarkko Nikula 490b6fb8d3aSMika Westerberg static int spi_dev_check(struct device *dev, void *data) 491b6fb8d3aSMika Westerberg { 492b6fb8d3aSMika Westerberg struct spi_device *spi = to_spi_device(dev); 493b6fb8d3aSMika Westerberg struct spi_device *new_spi = data; 494b6fb8d3aSMika Westerberg 4958caab75fSGeert Uytterhoeven if (spi->controller == new_spi->controller && 496b6fb8d3aSMika Westerberg spi->chip_select == new_spi->chip_select) 497b6fb8d3aSMika Westerberg return -EBUSY; 498b6fb8d3aSMika Westerberg return 0; 499b6fb8d3aSMika Westerberg } 500b6fb8d3aSMika Westerberg 501dc87c98eSGrant Likely /** 502dc87c98eSGrant Likely * spi_add_device - Add spi_device allocated with spi_alloc_device 503dc87c98eSGrant Likely * @spi: spi_device to register 504dc87c98eSGrant Likely * 505dc87c98eSGrant Likely * Companion function to spi_alloc_device. Devices allocated with 506dc87c98eSGrant Likely * spi_alloc_device can be added onto the spi bus with this function. 507dc87c98eSGrant Likely * 50897d56dc6SJavier Martinez Canillas * Return: 0 on success; negative errno on failure 509dc87c98eSGrant Likely */ 510dc87c98eSGrant Likely int spi_add_device(struct spi_device *spi) 511dc87c98eSGrant Likely { 512e48880e0SDavid Brownell static DEFINE_MUTEX(spi_add_lock); 5138caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 5148caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 515dc87c98eSGrant Likely int status; 516dc87c98eSGrant Likely 517dc87c98eSGrant Likely /* Chipselects are numbered 0..max; validate. */ 5188caab75fSGeert Uytterhoeven if (spi->chip_select >= ctlr->num_chipselect) { 5198caab75fSGeert Uytterhoeven dev_err(dev, "cs%d >= max %d\n", spi->chip_select, 5208caab75fSGeert Uytterhoeven ctlr->num_chipselect); 521dc87c98eSGrant Likely return -EINVAL; 522dc87c98eSGrant Likely } 523dc87c98eSGrant Likely 524dc87c98eSGrant Likely /* Set the bus ID string */ 525e13ac47bSJarkko Nikula spi_dev_set_name(spi); 526e48880e0SDavid Brownell 527e48880e0SDavid Brownell /* We need to make sure there's no other device with this 528e48880e0SDavid Brownell * chipselect **BEFORE** we call setup(), else we'll trash 529e48880e0SDavid Brownell * its configuration. Lock against concurrent add() calls. 530e48880e0SDavid Brownell */ 531e48880e0SDavid Brownell mutex_lock(&spi_add_lock); 532e48880e0SDavid Brownell 533b6fb8d3aSMika Westerberg status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); 534b6fb8d3aSMika Westerberg if (status) { 535e48880e0SDavid Brownell dev_err(dev, "chipselect %d already in use\n", 536e48880e0SDavid Brownell spi->chip_select); 537e48880e0SDavid Brownell goto done; 538e48880e0SDavid Brownell } 539e48880e0SDavid Brownell 5408caab75fSGeert Uytterhoeven if (ctlr->cs_gpios) 5418caab75fSGeert Uytterhoeven spi->cs_gpio = ctlr->cs_gpios[spi->chip_select]; 54274317984SJean-Christophe PLAGNIOL-VILLARD 543e48880e0SDavid Brownell /* Drivers may modify this initial i/o setup, but will 544e48880e0SDavid Brownell * normally rely on the device being setup. Devices 545e48880e0SDavid Brownell * using SPI_CS_HIGH can't coexist well otherwise... 546e48880e0SDavid Brownell */ 5477d077197SDavid Brownell status = spi_setup(spi); 548dc87c98eSGrant Likely if (status < 0) { 549eb288a1fSLinus Walleij dev_err(dev, "can't setup %s, status %d\n", 550eb288a1fSLinus Walleij dev_name(&spi->dev), status); 551e48880e0SDavid Brownell goto done; 552dc87c98eSGrant Likely } 553dc87c98eSGrant Likely 554e48880e0SDavid Brownell /* Device may be bound to an active driver when this returns */ 555dc87c98eSGrant Likely status = device_add(&spi->dev); 556e48880e0SDavid Brownell if (status < 0) 557eb288a1fSLinus Walleij dev_err(dev, "can't add %s, status %d\n", 558eb288a1fSLinus Walleij dev_name(&spi->dev), status); 559e48880e0SDavid Brownell else 56035f74fcaSKay Sievers dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev)); 561e48880e0SDavid Brownell 562e48880e0SDavid Brownell done: 563e48880e0SDavid Brownell mutex_unlock(&spi_add_lock); 564e48880e0SDavid Brownell return status; 565dc87c98eSGrant Likely } 566dc87c98eSGrant Likely EXPORT_SYMBOL_GPL(spi_add_device); 5678ae12a0dSDavid Brownell 56833e34dc6SDavid Brownell /** 56933e34dc6SDavid Brownell * spi_new_device - instantiate one new SPI device 5708caab75fSGeert Uytterhoeven * @ctlr: Controller to which device is connected 57133e34dc6SDavid Brownell * @chip: Describes the SPI device 57233e34dc6SDavid Brownell * Context: can sleep 57333e34dc6SDavid Brownell * 57433e34dc6SDavid Brownell * On typical mainboards, this is purely internal; and it's not needed 5758ae12a0dSDavid Brownell * after board init creates the hard-wired devices. Some development 5768ae12a0dSDavid Brownell * platforms may not be able to use spi_register_board_info though, and 5778ae12a0dSDavid Brownell * this is exported so that for example a USB or parport based adapter 5788ae12a0dSDavid Brownell * driver could add devices (which it would learn about out-of-band). 579082c8cb4SDavid Brownell * 58097d56dc6SJavier Martinez Canillas * Return: the new device, or NULL. 5818ae12a0dSDavid Brownell */ 5828caab75fSGeert Uytterhoeven struct spi_device *spi_new_device(struct spi_controller *ctlr, 583e9d5a461SAdrian Bunk struct spi_board_info *chip) 5848ae12a0dSDavid Brownell { 5858ae12a0dSDavid Brownell struct spi_device *proxy; 5868ae12a0dSDavid Brownell int status; 5878ae12a0dSDavid Brownell 588082c8cb4SDavid Brownell /* NOTE: caller did any chip->bus_num checks necessary. 589082c8cb4SDavid Brownell * 590082c8cb4SDavid Brownell * Also, unless we change the return value convention to use 591082c8cb4SDavid Brownell * error-or-pointer (not NULL-or-pointer), troubleshootability 592082c8cb4SDavid Brownell * suggests syslogged diagnostics are best here (ugh). 593082c8cb4SDavid Brownell */ 594082c8cb4SDavid Brownell 5958caab75fSGeert Uytterhoeven proxy = spi_alloc_device(ctlr); 596dc87c98eSGrant Likely if (!proxy) 5978ae12a0dSDavid Brownell return NULL; 5988ae12a0dSDavid Brownell 599102eb975SGrant Likely WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 600102eb975SGrant Likely 6018ae12a0dSDavid Brownell proxy->chip_select = chip->chip_select; 6028ae12a0dSDavid Brownell proxy->max_speed_hz = chip->max_speed_hz; 603980a01c9SDavid Brownell proxy->mode = chip->mode; 6048ae12a0dSDavid Brownell proxy->irq = chip->irq; 605102eb975SGrant Likely strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 6068ae12a0dSDavid Brownell proxy->dev.platform_data = (void *) chip->platform_data; 6078ae12a0dSDavid Brownell proxy->controller_data = chip->controller_data; 6088ae12a0dSDavid Brownell proxy->controller_state = NULL; 6098ae12a0dSDavid Brownell 610826cf175SDmitry Torokhov if (chip->properties) { 611826cf175SDmitry Torokhov status = device_add_properties(&proxy->dev, chip->properties); 612826cf175SDmitry Torokhov if (status) { 6138caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 614826cf175SDmitry Torokhov "failed to add properties to '%s': %d\n", 615826cf175SDmitry Torokhov chip->modalias, status); 616826cf175SDmitry Torokhov goto err_dev_put; 617826cf175SDmitry Torokhov } 6188ae12a0dSDavid Brownell } 619dc87c98eSGrant Likely 620826cf175SDmitry Torokhov status = spi_add_device(proxy); 621826cf175SDmitry Torokhov if (status < 0) 622826cf175SDmitry Torokhov goto err_remove_props; 623826cf175SDmitry Torokhov 624dc87c98eSGrant Likely return proxy; 625826cf175SDmitry Torokhov 626826cf175SDmitry Torokhov err_remove_props: 627826cf175SDmitry Torokhov if (chip->properties) 628826cf175SDmitry Torokhov device_remove_properties(&proxy->dev); 629826cf175SDmitry Torokhov err_dev_put: 630826cf175SDmitry Torokhov spi_dev_put(proxy); 631826cf175SDmitry Torokhov return NULL; 632dc87c98eSGrant Likely } 6338ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_new_device); 6348ae12a0dSDavid Brownell 6353b1884c2SGeert Uytterhoeven /** 6363b1884c2SGeert Uytterhoeven * spi_unregister_device - unregister a single SPI device 6373b1884c2SGeert Uytterhoeven * @spi: spi_device to unregister 6383b1884c2SGeert Uytterhoeven * 6393b1884c2SGeert Uytterhoeven * Start making the passed SPI device vanish. Normally this would be handled 6408caab75fSGeert Uytterhoeven * by spi_unregister_controller(). 6413b1884c2SGeert Uytterhoeven */ 6423b1884c2SGeert Uytterhoeven void spi_unregister_device(struct spi_device *spi) 6433b1884c2SGeert Uytterhoeven { 644bd6c1644SGeert Uytterhoeven if (!spi) 645bd6c1644SGeert Uytterhoeven return; 646bd6c1644SGeert Uytterhoeven 6478324147fSJohan Hovold if (spi->dev.of_node) { 648bd6c1644SGeert Uytterhoeven of_node_clear_flag(spi->dev.of_node, OF_POPULATED); 6498324147fSJohan Hovold of_node_put(spi->dev.of_node); 6508324147fSJohan Hovold } 6517f24467fSOctavian Purdila if (ACPI_COMPANION(&spi->dev)) 6527f24467fSOctavian Purdila acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev)); 6533b1884c2SGeert Uytterhoeven device_unregister(&spi->dev); 6543b1884c2SGeert Uytterhoeven } 6553b1884c2SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_device); 6563b1884c2SGeert Uytterhoeven 6578caab75fSGeert Uytterhoeven static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr, 6582b9603a0SFeng Tang struct spi_board_info *bi) 6592b9603a0SFeng Tang { 6602b9603a0SFeng Tang struct spi_device *dev; 6612b9603a0SFeng Tang 6628caab75fSGeert Uytterhoeven if (ctlr->bus_num != bi->bus_num) 6632b9603a0SFeng Tang return; 6642b9603a0SFeng Tang 6658caab75fSGeert Uytterhoeven dev = spi_new_device(ctlr, bi); 6662b9603a0SFeng Tang if (!dev) 6678caab75fSGeert Uytterhoeven dev_err(ctlr->dev.parent, "can't create new device for %s\n", 6682b9603a0SFeng Tang bi->modalias); 6692b9603a0SFeng Tang } 6702b9603a0SFeng Tang 67133e34dc6SDavid Brownell /** 67233e34dc6SDavid Brownell * spi_register_board_info - register SPI devices for a given board 67333e34dc6SDavid Brownell * @info: array of chip descriptors 67433e34dc6SDavid Brownell * @n: how many descriptors are provided 67533e34dc6SDavid Brownell * Context: can sleep 67633e34dc6SDavid Brownell * 6778ae12a0dSDavid Brownell * Board-specific early init code calls this (probably during arch_initcall) 6788ae12a0dSDavid Brownell * with segments of the SPI device table. Any device nodes are created later, 6798ae12a0dSDavid Brownell * after the relevant parent SPI controller (bus_num) is defined. We keep 6808ae12a0dSDavid Brownell * this table of devices forever, so that reloading a controller driver will 6818ae12a0dSDavid Brownell * not make Linux forget about these hard-wired devices. 6828ae12a0dSDavid Brownell * 6838ae12a0dSDavid Brownell * Other code can also call this, e.g. a particular add-on board might provide 6848ae12a0dSDavid Brownell * SPI devices through its expansion connector, so code initializing that board 6858ae12a0dSDavid Brownell * would naturally declare its SPI devices. 6868ae12a0dSDavid Brownell * 6878ae12a0dSDavid Brownell * The board info passed can safely be __initdata ... but be careful of 6888ae12a0dSDavid Brownell * any embedded pointers (platform_data, etc), they're copied as-is. 689826cf175SDmitry Torokhov * Device properties are deep-copied though. 69097d56dc6SJavier Martinez Canillas * 69197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 6928ae12a0dSDavid Brownell */ 693fd4a319bSGrant Likely int spi_register_board_info(struct spi_board_info const *info, unsigned n) 6948ae12a0dSDavid Brownell { 6958ae12a0dSDavid Brownell struct boardinfo *bi; 6962b9603a0SFeng Tang int i; 6978ae12a0dSDavid Brownell 698c7908a37SXiubo Li if (!n) 699f974cf57SDmitry Torokhov return 0; 700c7908a37SXiubo Li 701f9bdb7fdSMarkus Elfring bi = kcalloc(n, sizeof(*bi), GFP_KERNEL); 7028ae12a0dSDavid Brownell if (!bi) 7038ae12a0dSDavid Brownell return -ENOMEM; 7048ae12a0dSDavid Brownell 7052b9603a0SFeng Tang for (i = 0; i < n; i++, bi++, info++) { 7068caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 7072b9603a0SFeng Tang 7082b9603a0SFeng Tang memcpy(&bi->board_info, info, sizeof(*info)); 709826cf175SDmitry Torokhov if (info->properties) { 710826cf175SDmitry Torokhov bi->board_info.properties = 711826cf175SDmitry Torokhov property_entries_dup(info->properties); 712826cf175SDmitry Torokhov if (IS_ERR(bi->board_info.properties)) 713826cf175SDmitry Torokhov return PTR_ERR(bi->board_info.properties); 714826cf175SDmitry Torokhov } 715826cf175SDmitry Torokhov 71694040828SMatthias Kaehlcke mutex_lock(&board_lock); 7178ae12a0dSDavid Brownell list_add_tail(&bi->list, &board_list); 7188caab75fSGeert Uytterhoeven list_for_each_entry(ctlr, &spi_controller_list, list) 7198caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, 7208caab75fSGeert Uytterhoeven &bi->board_info); 72194040828SMatthias Kaehlcke mutex_unlock(&board_lock); 7222b9603a0SFeng Tang } 7232b9603a0SFeng Tang 7248ae12a0dSDavid Brownell return 0; 7258ae12a0dSDavid Brownell } 7268ae12a0dSDavid Brownell 7278ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 7288ae12a0dSDavid Brownell 729b158935fSMark Brown static void spi_set_cs(struct spi_device *spi, bool enable) 730b158935fSMark Brown { 731b158935fSMark Brown if (spi->mode & SPI_CS_HIGH) 732b158935fSMark Brown enable = !enable; 733b158935fSMark Brown 7348eee6b9dSThor Thayer if (gpio_is_valid(spi->cs_gpio)) { 735b158935fSMark Brown gpio_set_value(spi->cs_gpio, !enable); 7368eee6b9dSThor Thayer /* Some SPI masters need both GPIO CS & slave_select */ 7378caab75fSGeert Uytterhoeven if ((spi->controller->flags & SPI_MASTER_GPIO_SS) && 7388caab75fSGeert Uytterhoeven spi->controller->set_cs) 7398caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 7408caab75fSGeert Uytterhoeven } else if (spi->controller->set_cs) { 7418caab75fSGeert Uytterhoeven spi->controller->set_cs(spi, !enable); 7428eee6b9dSThor Thayer } 743b158935fSMark Brown } 744b158935fSMark Brown 7452de440f5SGeert Uytterhoeven #ifdef CONFIG_HAS_DMA 74646336966SBoris Brezillon int spi_map_buf(struct spi_controller *ctlr, struct device *dev, 7476ad45a27SMark Brown struct sg_table *sgt, void *buf, size_t len, 7486ad45a27SMark Brown enum dma_data_direction dir) 7496ad45a27SMark Brown { 7506ad45a27SMark Brown const bool vmalloced_buf = is_vmalloc_addr(buf); 751df88e91bSAndy Shevchenko unsigned int max_seg_size = dma_get_max_seg_size(dev); 752b1b8153cSVignesh R #ifdef CONFIG_HIGHMEM 753b1b8153cSVignesh R const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE && 754b1b8153cSVignesh R (unsigned long)buf < (PKMAP_BASE + 755b1b8153cSVignesh R (LAST_PKMAP * PAGE_SIZE))); 756b1b8153cSVignesh R #else 757b1b8153cSVignesh R const bool kmap_buf = false; 758b1b8153cSVignesh R #endif 75965598c13SAndrew Gabbasov int desc_len; 76065598c13SAndrew Gabbasov int sgs; 7616ad45a27SMark Brown struct page *vm_page; 7628dd4a016SJuan Gutierrez struct scatterlist *sg; 7636ad45a27SMark Brown void *sg_buf; 7646ad45a27SMark Brown size_t min; 7656ad45a27SMark Brown int i, ret; 7666ad45a27SMark Brown 767b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 768df88e91bSAndy Shevchenko desc_len = min_t(int, max_seg_size, PAGE_SIZE); 76965598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len); 7700569a88fSVignesh R } else if (virt_addr_valid(buf)) { 7718caab75fSGeert Uytterhoeven desc_len = min_t(int, max_seg_size, ctlr->max_dma_len); 77265598c13SAndrew Gabbasov sgs = DIV_ROUND_UP(len, desc_len); 7730569a88fSVignesh R } else { 7740569a88fSVignesh R return -EINVAL; 77565598c13SAndrew Gabbasov } 77665598c13SAndrew Gabbasov 7776ad45a27SMark Brown ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); 7786ad45a27SMark Brown if (ret != 0) 7796ad45a27SMark Brown return ret; 7806ad45a27SMark Brown 7818dd4a016SJuan Gutierrez sg = &sgt->sgl[0]; 7826ad45a27SMark Brown for (i = 0; i < sgs; i++) { 7836ad45a27SMark Brown 784b1b8153cSVignesh R if (vmalloced_buf || kmap_buf) { 785ce99319aSMaxime Chevallier /* 786ce99319aSMaxime Chevallier * Next scatterlist entry size is the minimum between 787ce99319aSMaxime Chevallier * the desc_len and the remaining buffer length that 788ce99319aSMaxime Chevallier * fits in a page. 789ce99319aSMaxime Chevallier */ 790ce99319aSMaxime Chevallier min = min_t(size_t, desc_len, 791ce99319aSMaxime Chevallier min_t(size_t, len, 792ce99319aSMaxime Chevallier PAGE_SIZE - offset_in_page(buf))); 793b1b8153cSVignesh R if (vmalloced_buf) 7946ad45a27SMark Brown vm_page = vmalloc_to_page(buf); 795b1b8153cSVignesh R else 796b1b8153cSVignesh R vm_page = kmap_to_page(buf); 7976ad45a27SMark Brown if (!vm_page) { 7986ad45a27SMark Brown sg_free_table(sgt); 7996ad45a27SMark Brown return -ENOMEM; 8006ad45a27SMark Brown } 8018dd4a016SJuan Gutierrez sg_set_page(sg, vm_page, 802c1aefbddSCharles Keepax min, offset_in_page(buf)); 8036ad45a27SMark Brown } else { 80465598c13SAndrew Gabbasov min = min_t(size_t, len, desc_len); 8056ad45a27SMark Brown sg_buf = buf; 8068dd4a016SJuan Gutierrez sg_set_buf(sg, sg_buf, min); 8076ad45a27SMark Brown } 8086ad45a27SMark Brown 8096ad45a27SMark Brown buf += min; 8106ad45a27SMark Brown len -= min; 8118dd4a016SJuan Gutierrez sg = sg_next(sg); 8126ad45a27SMark Brown } 8136ad45a27SMark Brown 8146ad45a27SMark Brown ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); 81589e4b66aSGeert Uytterhoeven if (!ret) 81689e4b66aSGeert Uytterhoeven ret = -ENOMEM; 8176ad45a27SMark Brown if (ret < 0) { 8186ad45a27SMark Brown sg_free_table(sgt); 8196ad45a27SMark Brown return ret; 8206ad45a27SMark Brown } 8216ad45a27SMark Brown 8226ad45a27SMark Brown sgt->nents = ret; 8236ad45a27SMark Brown 8246ad45a27SMark Brown return 0; 8256ad45a27SMark Brown } 8266ad45a27SMark Brown 82746336966SBoris Brezillon void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev, 8286ad45a27SMark Brown struct sg_table *sgt, enum dma_data_direction dir) 8296ad45a27SMark Brown { 8306ad45a27SMark Brown if (sgt->orig_nents) { 8316ad45a27SMark Brown dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); 8326ad45a27SMark Brown sg_free_table(sgt); 8336ad45a27SMark Brown } 8346ad45a27SMark Brown } 8356ad45a27SMark Brown 8368caab75fSGeert Uytterhoeven static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 83799adef31SMark Brown { 83899adef31SMark Brown struct device *tx_dev, *rx_dev; 83999adef31SMark Brown struct spi_transfer *xfer; 8406ad45a27SMark Brown int ret; 8413a2eba9bSMark Brown 8428caab75fSGeert Uytterhoeven if (!ctlr->can_dma) 84399adef31SMark Brown return 0; 84499adef31SMark Brown 8458caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 8468caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 847c37f45b5SLeilk Liu else 8488caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 849c37f45b5SLeilk Liu 8508caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 8518caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 852c37f45b5SLeilk Liu else 8538caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 85499adef31SMark Brown 85599adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 8568caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 85799adef31SMark Brown continue; 85899adef31SMark Brown 85999adef31SMark Brown if (xfer->tx_buf != NULL) { 8608caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg, 8616ad45a27SMark Brown (void *)xfer->tx_buf, xfer->len, 86299adef31SMark Brown DMA_TO_DEVICE); 8636ad45a27SMark Brown if (ret != 0) 8646ad45a27SMark Brown return ret; 86599adef31SMark Brown } 86699adef31SMark Brown 86799adef31SMark Brown if (xfer->rx_buf != NULL) { 8688caab75fSGeert Uytterhoeven ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg, 86999adef31SMark Brown xfer->rx_buf, xfer->len, 87099adef31SMark Brown DMA_FROM_DEVICE); 8716ad45a27SMark Brown if (ret != 0) { 8728caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, 8736ad45a27SMark Brown DMA_TO_DEVICE); 8746ad45a27SMark Brown return ret; 87599adef31SMark Brown } 87699adef31SMark Brown } 87799adef31SMark Brown } 87899adef31SMark Brown 8798caab75fSGeert Uytterhoeven ctlr->cur_msg_mapped = true; 88099adef31SMark Brown 88199adef31SMark Brown return 0; 88299adef31SMark Brown } 88399adef31SMark Brown 8848caab75fSGeert Uytterhoeven static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg) 88599adef31SMark Brown { 88699adef31SMark Brown struct spi_transfer *xfer; 88799adef31SMark Brown struct device *tx_dev, *rx_dev; 88899adef31SMark Brown 8898caab75fSGeert Uytterhoeven if (!ctlr->cur_msg_mapped || !ctlr->can_dma) 89099adef31SMark Brown return 0; 89199adef31SMark Brown 8928caab75fSGeert Uytterhoeven if (ctlr->dma_tx) 8938caab75fSGeert Uytterhoeven tx_dev = ctlr->dma_tx->device->dev; 894c37f45b5SLeilk Liu else 8958caab75fSGeert Uytterhoeven tx_dev = ctlr->dev.parent; 896c37f45b5SLeilk Liu 8978caab75fSGeert Uytterhoeven if (ctlr->dma_rx) 8988caab75fSGeert Uytterhoeven rx_dev = ctlr->dma_rx->device->dev; 899c37f45b5SLeilk Liu else 9008caab75fSGeert Uytterhoeven rx_dev = ctlr->dev.parent; 90199adef31SMark Brown 90299adef31SMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9038caab75fSGeert Uytterhoeven if (!ctlr->can_dma(ctlr, msg->spi, xfer)) 90499adef31SMark Brown continue; 90599adef31SMark Brown 9068caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 9078caab75fSGeert Uytterhoeven spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 90899adef31SMark Brown } 90999adef31SMark Brown 91099adef31SMark Brown return 0; 91199adef31SMark Brown } 9122de440f5SGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */ 9138caab75fSGeert Uytterhoeven static inline int __spi_map_msg(struct spi_controller *ctlr, 9142de440f5SGeert Uytterhoeven struct spi_message *msg) 9152de440f5SGeert Uytterhoeven { 9162de440f5SGeert Uytterhoeven return 0; 9172de440f5SGeert Uytterhoeven } 9182de440f5SGeert Uytterhoeven 9198caab75fSGeert Uytterhoeven static inline int __spi_unmap_msg(struct spi_controller *ctlr, 9202de440f5SGeert Uytterhoeven struct spi_message *msg) 9212de440f5SGeert Uytterhoeven { 9222de440f5SGeert Uytterhoeven return 0; 9232de440f5SGeert Uytterhoeven } 9242de440f5SGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */ 9252de440f5SGeert Uytterhoeven 9268caab75fSGeert Uytterhoeven static inline int spi_unmap_msg(struct spi_controller *ctlr, 9274b786458SMartin Sperl struct spi_message *msg) 9284b786458SMartin Sperl { 9294b786458SMartin Sperl struct spi_transfer *xfer; 9304b786458SMartin Sperl 9314b786458SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9324b786458SMartin Sperl /* 9334b786458SMartin Sperl * Restore the original value of tx_buf or rx_buf if they are 9344b786458SMartin Sperl * NULL. 9354b786458SMartin Sperl */ 9368caab75fSGeert Uytterhoeven if (xfer->tx_buf == ctlr->dummy_tx) 9374b786458SMartin Sperl xfer->tx_buf = NULL; 9388caab75fSGeert Uytterhoeven if (xfer->rx_buf == ctlr->dummy_rx) 9394b786458SMartin Sperl xfer->rx_buf = NULL; 9404b786458SMartin Sperl } 9414b786458SMartin Sperl 9428caab75fSGeert Uytterhoeven return __spi_unmap_msg(ctlr, msg); 9434b786458SMartin Sperl } 9444b786458SMartin Sperl 9458caab75fSGeert Uytterhoeven static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg) 9462de440f5SGeert Uytterhoeven { 9472de440f5SGeert Uytterhoeven struct spi_transfer *xfer; 9482de440f5SGeert Uytterhoeven void *tmp; 9492de440f5SGeert Uytterhoeven unsigned int max_tx, max_rx; 9502de440f5SGeert Uytterhoeven 9518caab75fSGeert Uytterhoeven if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) { 9522de440f5SGeert Uytterhoeven max_tx = 0; 9532de440f5SGeert Uytterhoeven max_rx = 0; 9542de440f5SGeert Uytterhoeven 9552de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, transfer_list) { 9568caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) && 9572de440f5SGeert Uytterhoeven !xfer->tx_buf) 9582de440f5SGeert Uytterhoeven max_tx = max(xfer->len, max_tx); 9598caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) && 9602de440f5SGeert Uytterhoeven !xfer->rx_buf) 9612de440f5SGeert Uytterhoeven max_rx = max(xfer->len, max_rx); 9622de440f5SGeert Uytterhoeven } 9632de440f5SGeert Uytterhoeven 9642de440f5SGeert Uytterhoeven if (max_tx) { 9658caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_tx, max_tx, 9662de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 9672de440f5SGeert Uytterhoeven if (!tmp) 9682de440f5SGeert Uytterhoeven return -ENOMEM; 9698caab75fSGeert Uytterhoeven ctlr->dummy_tx = tmp; 9702de440f5SGeert Uytterhoeven memset(tmp, 0, max_tx); 9712de440f5SGeert Uytterhoeven } 9722de440f5SGeert Uytterhoeven 9732de440f5SGeert Uytterhoeven if (max_rx) { 9748caab75fSGeert Uytterhoeven tmp = krealloc(ctlr->dummy_rx, max_rx, 9752de440f5SGeert Uytterhoeven GFP_KERNEL | GFP_DMA); 9762de440f5SGeert Uytterhoeven if (!tmp) 9772de440f5SGeert Uytterhoeven return -ENOMEM; 9788caab75fSGeert Uytterhoeven ctlr->dummy_rx = tmp; 9792de440f5SGeert Uytterhoeven } 9802de440f5SGeert Uytterhoeven 9812de440f5SGeert Uytterhoeven if (max_tx || max_rx) { 9822de440f5SGeert Uytterhoeven list_for_each_entry(xfer, &msg->transfers, 9832de440f5SGeert Uytterhoeven transfer_list) { 9842de440f5SGeert Uytterhoeven if (!xfer->tx_buf) 9858caab75fSGeert Uytterhoeven xfer->tx_buf = ctlr->dummy_tx; 9862de440f5SGeert Uytterhoeven if (!xfer->rx_buf) 9878caab75fSGeert Uytterhoeven xfer->rx_buf = ctlr->dummy_rx; 9882de440f5SGeert Uytterhoeven } 9892de440f5SGeert Uytterhoeven } 9902de440f5SGeert Uytterhoeven } 9912de440f5SGeert Uytterhoeven 9928caab75fSGeert Uytterhoeven return __spi_map_msg(ctlr, msg); 9932de440f5SGeert Uytterhoeven } 99499adef31SMark Brown 995b158935fSMark Brown /* 996b158935fSMark Brown * spi_transfer_one_message - Default implementation of transfer_one_message() 997b158935fSMark Brown * 998b158935fSMark Brown * This is a standard implementation of transfer_one_message() for 9998ba811a7SMoritz Fischer * drivers which implement a transfer_one() operation. It provides 1000b158935fSMark Brown * standard handling of delays and chip select management. 1001b158935fSMark Brown */ 10028caab75fSGeert Uytterhoeven static int spi_transfer_one_message(struct spi_controller *ctlr, 1003b158935fSMark Brown struct spi_message *msg) 1004b158935fSMark Brown { 1005b158935fSMark Brown struct spi_transfer *xfer; 1006b158935fSMark Brown bool keep_cs = false; 1007b158935fSMark Brown int ret = 0; 1008d0716ddeSSien Wu unsigned long long ms = 1; 10098caab75fSGeert Uytterhoeven struct spi_statistics *statm = &ctlr->statistics; 1010eca2ebc7SMartin Sperl struct spi_statistics *stats = &msg->spi->statistics; 1011b158935fSMark Brown 1012b158935fSMark Brown spi_set_cs(msg->spi, true); 1013b158935fSMark Brown 1014eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, messages); 1015eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, messages); 1016eca2ebc7SMartin Sperl 1017b158935fSMark Brown list_for_each_entry(xfer, &msg->transfers, transfer_list) { 1018b158935fSMark Brown trace_spi_transfer_start(msg, xfer); 1019b158935fSMark Brown 10208caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(statm, xfer, ctlr); 10218caab75fSGeert Uytterhoeven spi_statistics_add_transfer_stats(stats, xfer, ctlr); 1022eca2ebc7SMartin Sperl 102338ec10f6SMark Brown if (xfer->tx_buf || xfer->rx_buf) { 10248caab75fSGeert Uytterhoeven reinit_completion(&ctlr->xfer_completion); 1025b158935fSMark Brown 10268caab75fSGeert Uytterhoeven ret = ctlr->transfer_one(ctlr, msg->spi, xfer); 1027b158935fSMark Brown if (ret < 0) { 1028eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, 1029eca2ebc7SMartin Sperl errors); 1030eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, 1031eca2ebc7SMartin Sperl errors); 1032b158935fSMark Brown dev_err(&msg->spi->dev, 1033b158935fSMark Brown "SPI transfer failed: %d\n", ret); 1034b158935fSMark Brown goto out; 1035b158935fSMark Brown } 1036b158935fSMark Brown 103713a42798SAxel Lin if (ret > 0) { 103813a42798SAxel Lin ret = 0; 1039d0716ddeSSien Wu ms = 8LL * 1000LL * xfer->len; 1040d0716ddeSSien Wu do_div(ms, xfer->speed_hz); 1041833bfadeSHauke Mehrtens ms += ms + 200; /* some tolerance */ 104216a0ce4eSMark Brown 1043d0716ddeSSien Wu if (ms > UINT_MAX) 1044d0716ddeSSien Wu ms = UINT_MAX; 1045d0716ddeSSien Wu 10468caab75fSGeert Uytterhoeven ms = wait_for_completion_timeout(&ctlr->xfer_completion, 104716a0ce4eSMark Brown msecs_to_jiffies(ms)); 104816a0ce4eSMark Brown } 104916a0ce4eSMark Brown 105016a0ce4eSMark Brown if (ms == 0) { 1051eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(statm, 1052eca2ebc7SMartin Sperl timedout); 1053eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(stats, 1054eca2ebc7SMartin Sperl timedout); 105538ec10f6SMark Brown dev_err(&msg->spi->dev, 105638ec10f6SMark Brown "SPI transfer timed out\n"); 105716a0ce4eSMark Brown msg->status = -ETIMEDOUT; 105813a42798SAxel Lin } 105938ec10f6SMark Brown } else { 106038ec10f6SMark Brown if (xfer->len) 106138ec10f6SMark Brown dev_err(&msg->spi->dev, 106238ec10f6SMark Brown "Bufferless transfer has length %u\n", 106338ec10f6SMark Brown xfer->len); 106438ec10f6SMark Brown } 1065b158935fSMark Brown 1066b158935fSMark Brown trace_spi_transfer_stop(msg, xfer); 1067b158935fSMark Brown 1068b158935fSMark Brown if (msg->status != -EINPROGRESS) 1069b158935fSMark Brown goto out; 1070b158935fSMark Brown 10718244bd3aSDaniel Kurtz if (xfer->delay_usecs) { 10728244bd3aSDaniel Kurtz u16 us = xfer->delay_usecs; 10738244bd3aSDaniel Kurtz 10748244bd3aSDaniel Kurtz if (us <= 10) 10758244bd3aSDaniel Kurtz udelay(us); 10768244bd3aSDaniel Kurtz else 10778244bd3aSDaniel Kurtz usleep_range(us, us + DIV_ROUND_UP(us, 10)); 10788244bd3aSDaniel Kurtz } 1079b158935fSMark Brown 1080b158935fSMark Brown if (xfer->cs_change) { 1081b158935fSMark Brown if (list_is_last(&xfer->transfer_list, 1082b158935fSMark Brown &msg->transfers)) { 1083b158935fSMark Brown keep_cs = true; 1084b158935fSMark Brown } else { 10850b73aa63SMark Brown spi_set_cs(msg->spi, false); 10860b73aa63SMark Brown udelay(10); 10870b73aa63SMark Brown spi_set_cs(msg->spi, true); 1088b158935fSMark Brown } 1089b158935fSMark Brown } 1090b158935fSMark Brown 1091b158935fSMark Brown msg->actual_length += xfer->len; 1092b158935fSMark Brown } 1093b158935fSMark Brown 1094b158935fSMark Brown out: 1095b158935fSMark Brown if (ret != 0 || !keep_cs) 1096b158935fSMark Brown spi_set_cs(msg->spi, false); 1097b158935fSMark Brown 1098b158935fSMark Brown if (msg->status == -EINPROGRESS) 1099b158935fSMark Brown msg->status = ret; 1100b158935fSMark Brown 11018caab75fSGeert Uytterhoeven if (msg->status && ctlr->handle_err) 11028caab75fSGeert Uytterhoeven ctlr->handle_err(ctlr, msg); 1103b716c4ffSAndy Shevchenko 11048caab75fSGeert Uytterhoeven spi_res_release(ctlr, msg); 1105d780c371SMartin Sperl 11068caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 1107b158935fSMark Brown 1108b158935fSMark Brown return ret; 1109b158935fSMark Brown } 1110b158935fSMark Brown 1111b158935fSMark Brown /** 1112b158935fSMark Brown * spi_finalize_current_transfer - report completion of a transfer 11138caab75fSGeert Uytterhoeven * @ctlr: the controller reporting completion 1114b158935fSMark Brown * 1115b158935fSMark Brown * Called by SPI drivers using the core transfer_one_message() 1116b158935fSMark Brown * implementation to notify it that the current interrupt driven 11179e8f4882SGeert Uytterhoeven * transfer has finished and the next one may be scheduled. 1118b158935fSMark Brown */ 11198caab75fSGeert Uytterhoeven void spi_finalize_current_transfer(struct spi_controller *ctlr) 1120b158935fSMark Brown { 11218caab75fSGeert Uytterhoeven complete(&ctlr->xfer_completion); 1122b158935fSMark Brown } 1123b158935fSMark Brown EXPORT_SYMBOL_GPL(spi_finalize_current_transfer); 1124b158935fSMark Brown 1125ffbbdd21SLinus Walleij /** 1126fc9e0f71SMark Brown * __spi_pump_messages - function which processes spi message queue 11278caab75fSGeert Uytterhoeven * @ctlr: controller to process queue for 1128fc9e0f71SMark Brown * @in_kthread: true if we are in the context of the message pump thread 1129ffbbdd21SLinus Walleij * 1130ffbbdd21SLinus Walleij * This function checks if there is any spi message in the queue that 1131ffbbdd21SLinus Walleij * needs processing and if so call out to the driver to initialize hardware 1132ffbbdd21SLinus Walleij * and transfer each message. 1133ffbbdd21SLinus Walleij * 11340461a414SMark Brown * Note that it is called both from the kthread itself and also from 11350461a414SMark Brown * inside spi_sync(); the queue extraction handling at the top of the 11360461a414SMark Brown * function should deal with this safely. 1137ffbbdd21SLinus Walleij */ 11388caab75fSGeert Uytterhoeven static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread) 1139ffbbdd21SLinus Walleij { 1140ffbbdd21SLinus Walleij unsigned long flags; 1141ffbbdd21SLinus Walleij bool was_busy = false; 1142ffbbdd21SLinus Walleij int ret; 1143ffbbdd21SLinus Walleij 1144983aee5dSMark Brown /* Lock queue */ 11458caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1146983aee5dSMark Brown 1147983aee5dSMark Brown /* Make sure we are not already running a message */ 11488caab75fSGeert Uytterhoeven if (ctlr->cur_msg) { 11498caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1150983aee5dSMark Brown return; 1151983aee5dSMark Brown } 1152983aee5dSMark Brown 11530461a414SMark Brown /* If another context is idling the device then defer */ 11548caab75fSGeert Uytterhoeven if (ctlr->idling) { 11558caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 11568caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 11570461a414SMark Brown return; 11580461a414SMark Brown } 11590461a414SMark Brown 1160983aee5dSMark Brown /* Check if the queue is idle */ 11618caab75fSGeert Uytterhoeven if (list_empty(&ctlr->queue) || !ctlr->running) { 11628caab75fSGeert Uytterhoeven if (!ctlr->busy) { 11638caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1164ffbbdd21SLinus Walleij return; 1165ffbbdd21SLinus Walleij } 1166fc9e0f71SMark Brown 1167fc9e0f71SMark Brown /* Only do teardown in the thread */ 1168fc9e0f71SMark Brown if (!in_kthread) { 11698caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, 11708caab75fSGeert Uytterhoeven &ctlr->pump_messages); 11718caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1172fc9e0f71SMark Brown return; 1173fc9e0f71SMark Brown } 1174fc9e0f71SMark Brown 11758caab75fSGeert Uytterhoeven ctlr->busy = false; 11768caab75fSGeert Uytterhoeven ctlr->idling = true; 11778caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 11780461a414SMark Brown 11798caab75fSGeert Uytterhoeven kfree(ctlr->dummy_rx); 11808caab75fSGeert Uytterhoeven ctlr->dummy_rx = NULL; 11818caab75fSGeert Uytterhoeven kfree(ctlr->dummy_tx); 11828caab75fSGeert Uytterhoeven ctlr->dummy_tx = NULL; 11838caab75fSGeert Uytterhoeven if (ctlr->unprepare_transfer_hardware && 11848caab75fSGeert Uytterhoeven ctlr->unprepare_transfer_hardware(ctlr)) 11858caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 1186b0b36b86SBryan Freed "failed to unprepare transfer hardware\n"); 11878caab75fSGeert Uytterhoeven if (ctlr->auto_runtime_pm) { 11888caab75fSGeert Uytterhoeven pm_runtime_mark_last_busy(ctlr->dev.parent); 11898caab75fSGeert Uytterhoeven pm_runtime_put_autosuspend(ctlr->dev.parent); 119049834de2SMark Brown } 11918caab75fSGeert Uytterhoeven trace_spi_controller_idle(ctlr); 1192ffbbdd21SLinus Walleij 11938caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 11948caab75fSGeert Uytterhoeven ctlr->idling = false; 11958caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1196ffbbdd21SLinus Walleij return; 1197ffbbdd21SLinus Walleij } 1198ffbbdd21SLinus Walleij 1199ffbbdd21SLinus Walleij /* Extract head of queue */ 12008caab75fSGeert Uytterhoeven ctlr->cur_msg = 12018caab75fSGeert Uytterhoeven list_first_entry(&ctlr->queue, struct spi_message, queue); 1202ffbbdd21SLinus Walleij 12038caab75fSGeert Uytterhoeven list_del_init(&ctlr->cur_msg->queue); 12048caab75fSGeert Uytterhoeven if (ctlr->busy) 1205ffbbdd21SLinus Walleij was_busy = true; 1206ffbbdd21SLinus Walleij else 12078caab75fSGeert Uytterhoeven ctlr->busy = true; 12088caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1209ffbbdd21SLinus Walleij 12108caab75fSGeert Uytterhoeven mutex_lock(&ctlr->io_mutex); 1211ef4d96ecSMark Brown 12128caab75fSGeert Uytterhoeven if (!was_busy && ctlr->auto_runtime_pm) { 12138caab75fSGeert Uytterhoeven ret = pm_runtime_get_sync(ctlr->dev.parent); 121449834de2SMark Brown if (ret < 0) { 12158caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "Failed to power device: %d\n", 121649834de2SMark Brown ret); 12178caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 121849834de2SMark Brown return; 121949834de2SMark Brown } 122049834de2SMark Brown } 122149834de2SMark Brown 122256ec1978SMark Brown if (!was_busy) 12238caab75fSGeert Uytterhoeven trace_spi_controller_busy(ctlr); 122456ec1978SMark Brown 12258caab75fSGeert Uytterhoeven if (!was_busy && ctlr->prepare_transfer_hardware) { 12268caab75fSGeert Uytterhoeven ret = ctlr->prepare_transfer_hardware(ctlr); 1227ffbbdd21SLinus Walleij if (ret) { 12288caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 1229ffbbdd21SLinus Walleij "failed to prepare transfer hardware\n"); 123049834de2SMark Brown 12318caab75fSGeert Uytterhoeven if (ctlr->auto_runtime_pm) 12328caab75fSGeert Uytterhoeven pm_runtime_put(ctlr->dev.parent); 12338caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 1234ffbbdd21SLinus Walleij return; 1235ffbbdd21SLinus Walleij } 1236ffbbdd21SLinus Walleij } 1237ffbbdd21SLinus Walleij 12388caab75fSGeert Uytterhoeven trace_spi_message_start(ctlr->cur_msg); 123956ec1978SMark Brown 12408caab75fSGeert Uytterhoeven if (ctlr->prepare_message) { 12418caab75fSGeert Uytterhoeven ret = ctlr->prepare_message(ctlr, ctlr->cur_msg); 12422841a5fcSMark Brown if (ret) { 12438caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to prepare message: %d\n", 12448caab75fSGeert Uytterhoeven ret); 12458caab75fSGeert Uytterhoeven ctlr->cur_msg->status = ret; 12468caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 124749023d2eSJon Hunter goto out; 12482841a5fcSMark Brown } 12498caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = true; 12502841a5fcSMark Brown } 12512841a5fcSMark Brown 12528caab75fSGeert Uytterhoeven ret = spi_map_msg(ctlr, ctlr->cur_msg); 125399adef31SMark Brown if (ret) { 12548caab75fSGeert Uytterhoeven ctlr->cur_msg->status = ret; 12558caab75fSGeert Uytterhoeven spi_finalize_current_message(ctlr); 125649023d2eSJon Hunter goto out; 125799adef31SMark Brown } 125899adef31SMark Brown 12598caab75fSGeert Uytterhoeven ret = ctlr->transfer_one_message(ctlr, ctlr->cur_msg); 1260ffbbdd21SLinus Walleij if (ret) { 12618caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 12621f802f82SGeert Uytterhoeven "failed to transfer one message from queue\n"); 126349023d2eSJon Hunter goto out; 1264ffbbdd21SLinus Walleij } 126549023d2eSJon Hunter 126649023d2eSJon Hunter out: 12678caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->io_mutex); 126862826970SMark Brown 126962826970SMark Brown /* Prod the scheduler in case transfer_one() was busy waiting */ 127049023d2eSJon Hunter if (!ret) 127162826970SMark Brown cond_resched(); 1272ffbbdd21SLinus Walleij } 1273ffbbdd21SLinus Walleij 1274fc9e0f71SMark Brown /** 1275fc9e0f71SMark Brown * spi_pump_messages - kthread work function which processes spi message queue 12768caab75fSGeert Uytterhoeven * @work: pointer to kthread work struct contained in the controller struct 1277fc9e0f71SMark Brown */ 1278fc9e0f71SMark Brown static void spi_pump_messages(struct kthread_work *work) 1279fc9e0f71SMark Brown { 12808caab75fSGeert Uytterhoeven struct spi_controller *ctlr = 12818caab75fSGeert Uytterhoeven container_of(work, struct spi_controller, pump_messages); 1282fc9e0f71SMark Brown 12838caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, true); 1284fc9e0f71SMark Brown } 1285fc9e0f71SMark Brown 12868caab75fSGeert Uytterhoeven static int spi_init_queue(struct spi_controller *ctlr) 1287ffbbdd21SLinus Walleij { 1288ffbbdd21SLinus Walleij struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; 1289ffbbdd21SLinus Walleij 12908caab75fSGeert Uytterhoeven ctlr->running = false; 12918caab75fSGeert Uytterhoeven ctlr->busy = false; 1292ffbbdd21SLinus Walleij 12938caab75fSGeert Uytterhoeven kthread_init_worker(&ctlr->kworker); 12948caab75fSGeert Uytterhoeven ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker, 12958caab75fSGeert Uytterhoeven "%s", dev_name(&ctlr->dev)); 12968caab75fSGeert Uytterhoeven if (IS_ERR(ctlr->kworker_task)) { 12978caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to create message pump task\n"); 12988caab75fSGeert Uytterhoeven return PTR_ERR(ctlr->kworker_task); 1299ffbbdd21SLinus Walleij } 13008caab75fSGeert Uytterhoeven kthread_init_work(&ctlr->pump_messages, spi_pump_messages); 1301ffbbdd21SLinus Walleij 1302ffbbdd21SLinus Walleij /* 13038caab75fSGeert Uytterhoeven * Controller config will indicate if this controller should run the 1304ffbbdd21SLinus Walleij * message pump with high (realtime) priority to reduce the transfer 1305ffbbdd21SLinus Walleij * latency on the bus by minimising the delay between a transfer 1306ffbbdd21SLinus Walleij * request and the scheduling of the message pump thread. Without this 1307ffbbdd21SLinus Walleij * setting the message pump thread will remain at default priority. 1308ffbbdd21SLinus Walleij */ 13098caab75fSGeert Uytterhoeven if (ctlr->rt) { 13108caab75fSGeert Uytterhoeven dev_info(&ctlr->dev, 1311ffbbdd21SLinus Walleij "will run message pump with realtime priority\n"); 13128caab75fSGeert Uytterhoeven sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, ¶m); 1313ffbbdd21SLinus Walleij } 1314ffbbdd21SLinus Walleij 1315ffbbdd21SLinus Walleij return 0; 1316ffbbdd21SLinus Walleij } 1317ffbbdd21SLinus Walleij 1318ffbbdd21SLinus Walleij /** 1319ffbbdd21SLinus Walleij * spi_get_next_queued_message() - called by driver to check for queued 1320ffbbdd21SLinus Walleij * messages 13218caab75fSGeert Uytterhoeven * @ctlr: the controller to check for queued messages 1322ffbbdd21SLinus Walleij * 1323ffbbdd21SLinus Walleij * If there are more messages in the queue, the next message is returned from 1324ffbbdd21SLinus Walleij * this call. 132597d56dc6SJavier Martinez Canillas * 132697d56dc6SJavier Martinez Canillas * Return: the next message in the queue, else NULL if the queue is empty. 1327ffbbdd21SLinus Walleij */ 13288caab75fSGeert Uytterhoeven struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) 1329ffbbdd21SLinus Walleij { 1330ffbbdd21SLinus Walleij struct spi_message *next; 1331ffbbdd21SLinus Walleij unsigned long flags; 1332ffbbdd21SLinus Walleij 1333ffbbdd21SLinus Walleij /* get a pointer to the next message, if any */ 13348caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 13358caab75fSGeert Uytterhoeven next = list_first_entry_or_null(&ctlr->queue, struct spi_message, 13361cfd97f9SAxel Lin queue); 13378caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1338ffbbdd21SLinus Walleij 1339ffbbdd21SLinus Walleij return next; 1340ffbbdd21SLinus Walleij } 1341ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_get_next_queued_message); 1342ffbbdd21SLinus Walleij 1343ffbbdd21SLinus Walleij /** 1344ffbbdd21SLinus Walleij * spi_finalize_current_message() - the current message is complete 13458caab75fSGeert Uytterhoeven * @ctlr: the controller to return the message to 1346ffbbdd21SLinus Walleij * 1347ffbbdd21SLinus Walleij * Called by the driver to notify the core that the message in the front of the 1348ffbbdd21SLinus Walleij * queue is complete and can be removed from the queue. 1349ffbbdd21SLinus Walleij */ 13508caab75fSGeert Uytterhoeven void spi_finalize_current_message(struct spi_controller *ctlr) 1351ffbbdd21SLinus Walleij { 1352ffbbdd21SLinus Walleij struct spi_message *mesg; 1353ffbbdd21SLinus Walleij unsigned long flags; 13542841a5fcSMark Brown int ret; 1355ffbbdd21SLinus Walleij 13568caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 13578caab75fSGeert Uytterhoeven mesg = ctlr->cur_msg; 13588caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1359ffbbdd21SLinus Walleij 13608caab75fSGeert Uytterhoeven spi_unmap_msg(ctlr, mesg); 136199adef31SMark Brown 13628caab75fSGeert Uytterhoeven if (ctlr->cur_msg_prepared && ctlr->unprepare_message) { 13638caab75fSGeert Uytterhoeven ret = ctlr->unprepare_message(ctlr, mesg); 13642841a5fcSMark Brown if (ret) { 13658caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to unprepare message: %d\n", 13668caab75fSGeert Uytterhoeven ret); 13672841a5fcSMark Brown } 13682841a5fcSMark Brown } 1369391949b6SUwe Kleine-König 13708caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 13718caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 13728caab75fSGeert Uytterhoeven ctlr->cur_msg_prepared = false; 13738caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 13748caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 13758e76ef88SMartin Sperl 13768e76ef88SMartin Sperl trace_spi_message_done(mesg); 13772841a5fcSMark Brown 1378ffbbdd21SLinus Walleij mesg->state = NULL; 1379ffbbdd21SLinus Walleij if (mesg->complete) 1380ffbbdd21SLinus Walleij mesg->complete(mesg->context); 1381ffbbdd21SLinus Walleij } 1382ffbbdd21SLinus Walleij EXPORT_SYMBOL_GPL(spi_finalize_current_message); 1383ffbbdd21SLinus Walleij 13848caab75fSGeert Uytterhoeven static int spi_start_queue(struct spi_controller *ctlr) 1385ffbbdd21SLinus Walleij { 1386ffbbdd21SLinus Walleij unsigned long flags; 1387ffbbdd21SLinus Walleij 13888caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1389ffbbdd21SLinus Walleij 13908caab75fSGeert Uytterhoeven if (ctlr->running || ctlr->busy) { 13918caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1392ffbbdd21SLinus Walleij return -EBUSY; 1393ffbbdd21SLinus Walleij } 1394ffbbdd21SLinus Walleij 13958caab75fSGeert Uytterhoeven ctlr->running = true; 13968caab75fSGeert Uytterhoeven ctlr->cur_msg = NULL; 13978caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1398ffbbdd21SLinus Walleij 13998caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 1400ffbbdd21SLinus Walleij 1401ffbbdd21SLinus Walleij return 0; 1402ffbbdd21SLinus Walleij } 1403ffbbdd21SLinus Walleij 14048caab75fSGeert Uytterhoeven static int spi_stop_queue(struct spi_controller *ctlr) 1405ffbbdd21SLinus Walleij { 1406ffbbdd21SLinus Walleij unsigned long flags; 1407ffbbdd21SLinus Walleij unsigned limit = 500; 1408ffbbdd21SLinus Walleij int ret = 0; 1409ffbbdd21SLinus Walleij 14108caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1411ffbbdd21SLinus Walleij 1412ffbbdd21SLinus Walleij /* 1413ffbbdd21SLinus Walleij * This is a bit lame, but is optimized for the common execution path. 14148caab75fSGeert Uytterhoeven * A wait_queue on the ctlr->busy could be used, but then the common 1415ffbbdd21SLinus Walleij * execution path (pump_messages) would be required to call wake_up or 1416ffbbdd21SLinus Walleij * friends on every SPI message. Do this instead. 1417ffbbdd21SLinus Walleij */ 14188caab75fSGeert Uytterhoeven while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) { 14198caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1420f97b26b0SAxel Lin usleep_range(10000, 11000); 14218caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1422ffbbdd21SLinus Walleij } 1423ffbbdd21SLinus Walleij 14248caab75fSGeert Uytterhoeven if (!list_empty(&ctlr->queue) || ctlr->busy) 1425ffbbdd21SLinus Walleij ret = -EBUSY; 1426ffbbdd21SLinus Walleij else 14278caab75fSGeert Uytterhoeven ctlr->running = false; 1428ffbbdd21SLinus Walleij 14298caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1430ffbbdd21SLinus Walleij 1431ffbbdd21SLinus Walleij if (ret) { 14328caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "could not stop message queue\n"); 1433ffbbdd21SLinus Walleij return ret; 1434ffbbdd21SLinus Walleij } 1435ffbbdd21SLinus Walleij return ret; 1436ffbbdd21SLinus Walleij } 1437ffbbdd21SLinus Walleij 14388caab75fSGeert Uytterhoeven static int spi_destroy_queue(struct spi_controller *ctlr) 1439ffbbdd21SLinus Walleij { 1440ffbbdd21SLinus Walleij int ret; 1441ffbbdd21SLinus Walleij 14428caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 1443ffbbdd21SLinus Walleij 1444ffbbdd21SLinus Walleij /* 14453989144fSPetr Mladek * kthread_flush_worker will block until all work is done. 1446ffbbdd21SLinus Walleij * If the reason that stop_queue timed out is that the work will never 1447ffbbdd21SLinus Walleij * finish, then it does no good to call flush/stop thread, so 1448ffbbdd21SLinus Walleij * return anyway. 1449ffbbdd21SLinus Walleij */ 1450ffbbdd21SLinus Walleij if (ret) { 14518caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem destroying queue\n"); 1452ffbbdd21SLinus Walleij return ret; 1453ffbbdd21SLinus Walleij } 1454ffbbdd21SLinus Walleij 14558caab75fSGeert Uytterhoeven kthread_flush_worker(&ctlr->kworker); 14568caab75fSGeert Uytterhoeven kthread_stop(ctlr->kworker_task); 1457ffbbdd21SLinus Walleij 1458ffbbdd21SLinus Walleij return 0; 1459ffbbdd21SLinus Walleij } 1460ffbbdd21SLinus Walleij 14610461a414SMark Brown static int __spi_queued_transfer(struct spi_device *spi, 14620461a414SMark Brown struct spi_message *msg, 14630461a414SMark Brown bool need_pump) 1464ffbbdd21SLinus Walleij { 14658caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 1466ffbbdd21SLinus Walleij unsigned long flags; 1467ffbbdd21SLinus Walleij 14688caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->queue_lock, flags); 1469ffbbdd21SLinus Walleij 14708caab75fSGeert Uytterhoeven if (!ctlr->running) { 14718caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1472ffbbdd21SLinus Walleij return -ESHUTDOWN; 1473ffbbdd21SLinus Walleij } 1474ffbbdd21SLinus Walleij msg->actual_length = 0; 1475ffbbdd21SLinus Walleij msg->status = -EINPROGRESS; 1476ffbbdd21SLinus Walleij 14778caab75fSGeert Uytterhoeven list_add_tail(&msg->queue, &ctlr->queue); 14788caab75fSGeert Uytterhoeven if (!ctlr->busy && need_pump) 14798caab75fSGeert Uytterhoeven kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages); 1480ffbbdd21SLinus Walleij 14818caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->queue_lock, flags); 1482ffbbdd21SLinus Walleij return 0; 1483ffbbdd21SLinus Walleij } 1484ffbbdd21SLinus Walleij 14850461a414SMark Brown /** 14860461a414SMark Brown * spi_queued_transfer - transfer function for queued transfers 14870461a414SMark Brown * @spi: spi device which is requesting transfer 14880461a414SMark Brown * @msg: spi message which is to handled is queued to driver queue 148997d56dc6SJavier Martinez Canillas * 149097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 14910461a414SMark Brown */ 14920461a414SMark Brown static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg) 14930461a414SMark Brown { 14940461a414SMark Brown return __spi_queued_transfer(spi, msg, true); 14950461a414SMark Brown } 14960461a414SMark Brown 14978caab75fSGeert Uytterhoeven static int spi_controller_initialize_queue(struct spi_controller *ctlr) 1498ffbbdd21SLinus Walleij { 1499ffbbdd21SLinus Walleij int ret; 1500ffbbdd21SLinus Walleij 15018caab75fSGeert Uytterhoeven ctlr->transfer = spi_queued_transfer; 15028caab75fSGeert Uytterhoeven if (!ctlr->transfer_one_message) 15038caab75fSGeert Uytterhoeven ctlr->transfer_one_message = spi_transfer_one_message; 1504ffbbdd21SLinus Walleij 1505ffbbdd21SLinus Walleij /* Initialize and start queue */ 15068caab75fSGeert Uytterhoeven ret = spi_init_queue(ctlr); 1507ffbbdd21SLinus Walleij if (ret) { 15088caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem initializing queue\n"); 1509ffbbdd21SLinus Walleij goto err_init_queue; 1510ffbbdd21SLinus Walleij } 15118caab75fSGeert Uytterhoeven ctlr->queued = true; 15128caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 1513ffbbdd21SLinus Walleij if (ret) { 15148caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "problem starting queue\n"); 1515ffbbdd21SLinus Walleij goto err_start_queue; 1516ffbbdd21SLinus Walleij } 1517ffbbdd21SLinus Walleij 1518ffbbdd21SLinus Walleij return 0; 1519ffbbdd21SLinus Walleij 1520ffbbdd21SLinus Walleij err_start_queue: 15218caab75fSGeert Uytterhoeven spi_destroy_queue(ctlr); 1522c3676d5cSMark Brown err_init_queue: 1523ffbbdd21SLinus Walleij return ret; 1524ffbbdd21SLinus Walleij } 1525ffbbdd21SLinus Walleij 1526988f259bSBoris Brezillon /** 1527988f259bSBoris Brezillon * spi_flush_queue - Send all pending messages in the queue from the callers' 1528988f259bSBoris Brezillon * context 1529988f259bSBoris Brezillon * @ctlr: controller to process queue for 1530988f259bSBoris Brezillon * 1531988f259bSBoris Brezillon * This should be used when one wants to ensure all pending messages have been 1532988f259bSBoris Brezillon * sent before doing something. Is used by the spi-mem code to make sure SPI 1533988f259bSBoris Brezillon * memory operations do not preempt regular SPI transfers that have been queued 1534988f259bSBoris Brezillon * before the spi-mem operation. 1535988f259bSBoris Brezillon */ 1536988f259bSBoris Brezillon void spi_flush_queue(struct spi_controller *ctlr) 1537988f259bSBoris Brezillon { 1538988f259bSBoris Brezillon if (ctlr->transfer == spi_queued_transfer) 1539988f259bSBoris Brezillon __spi_pump_messages(ctlr, false); 1540988f259bSBoris Brezillon } 1541988f259bSBoris Brezillon 1542ffbbdd21SLinus Walleij /*-------------------------------------------------------------------------*/ 1543ffbbdd21SLinus Walleij 15447cb94361SAndreas Larsson #if defined(CONFIG_OF) 15458caab75fSGeert Uytterhoeven static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi, 1546c2e51ac3SGeert Uytterhoeven struct device_node *nc) 1547d57a4282SGrant Likely { 154889da4293STrent Piepho u32 value; 1549c2e51ac3SGeert Uytterhoeven int rc; 1550d57a4282SGrant Likely 1551d57a4282SGrant Likely /* Mode (clock phase/polarity/etc.) */ 1552e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpha")) 1553d57a4282SGrant Likely spi->mode |= SPI_CPHA; 1554e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cpol")) 1555d57a4282SGrant Likely spi->mode |= SPI_CPOL; 1556e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-cs-high")) 1557d57a4282SGrant Likely spi->mode |= SPI_CS_HIGH; 1558e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-3wire")) 1559c20151dfSLars-Peter Clausen spi->mode |= SPI_3WIRE; 1560e0bcb680SSergei Shtylyov if (of_property_read_bool(nc, "spi-lsb-first")) 1561cd6339e6SZhao Qiang spi->mode |= SPI_LSB_FIRST; 1562d57a4282SGrant Likely 1563f477b7fbSwangyuhang /* Device DUAL/QUAD mode */ 156489da4293STrent Piepho if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) { 156589da4293STrent Piepho switch (value) { 156689da4293STrent Piepho case 1: 1567f477b7fbSwangyuhang break; 156889da4293STrent Piepho case 2: 1569f477b7fbSwangyuhang spi->mode |= SPI_TX_DUAL; 1570f477b7fbSwangyuhang break; 157189da4293STrent Piepho case 4: 1572f477b7fbSwangyuhang spi->mode |= SPI_TX_QUAD; 1573f477b7fbSwangyuhang break; 1574f477b7fbSwangyuhang default: 15758caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 1576a110f93dSwangyuhang "spi-tx-bus-width %d not supported\n", 157789da4293STrent Piepho value); 157880874d8cSGeert Uytterhoeven break; 1579f477b7fbSwangyuhang } 1580a822e99cSMark Brown } 1581f477b7fbSwangyuhang 158289da4293STrent Piepho if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) { 158389da4293STrent Piepho switch (value) { 158489da4293STrent Piepho case 1: 1585f477b7fbSwangyuhang break; 158689da4293STrent Piepho case 2: 1587f477b7fbSwangyuhang spi->mode |= SPI_RX_DUAL; 1588f477b7fbSwangyuhang break; 158989da4293STrent Piepho case 4: 1590f477b7fbSwangyuhang spi->mode |= SPI_RX_QUAD; 1591f477b7fbSwangyuhang break; 1592f477b7fbSwangyuhang default: 15938caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 1594a110f93dSwangyuhang "spi-rx-bus-width %d not supported\n", 159589da4293STrent Piepho value); 159680874d8cSGeert Uytterhoeven break; 1597f477b7fbSwangyuhang } 1598a822e99cSMark Brown } 1599f477b7fbSwangyuhang 16008caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr)) { 16016c364062SGeert Uytterhoeven if (strcmp(nc->name, "slave")) { 160225c56c88SRob Herring dev_err(&ctlr->dev, "%pOF is not called 'slave'\n", 160325c56c88SRob Herring nc); 16046c364062SGeert Uytterhoeven return -EINVAL; 16056c364062SGeert Uytterhoeven } 16066c364062SGeert Uytterhoeven return 0; 16076c364062SGeert Uytterhoeven } 16086c364062SGeert Uytterhoeven 16096c364062SGeert Uytterhoeven /* Device address */ 16106c364062SGeert Uytterhoeven rc = of_property_read_u32(nc, "reg", &value); 16116c364062SGeert Uytterhoeven if (rc) { 161225c56c88SRob Herring dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n", 161325c56c88SRob Herring nc, rc); 16146c364062SGeert Uytterhoeven return rc; 16156c364062SGeert Uytterhoeven } 16166c364062SGeert Uytterhoeven spi->chip_select = value; 16176c364062SGeert Uytterhoeven 1618d57a4282SGrant Likely /* Device speed */ 161989da4293STrent Piepho rc = of_property_read_u32(nc, "spi-max-frequency", &value); 162089da4293STrent Piepho if (rc) { 16218caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, 162225c56c88SRob Herring "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc); 1623c2e51ac3SGeert Uytterhoeven return rc; 1624d57a4282SGrant Likely } 162589da4293STrent Piepho spi->max_speed_hz = value; 1626d57a4282SGrant Likely 1627c2e51ac3SGeert Uytterhoeven return 0; 1628c2e51ac3SGeert Uytterhoeven } 1629c2e51ac3SGeert Uytterhoeven 1630c2e51ac3SGeert Uytterhoeven static struct spi_device * 16318caab75fSGeert Uytterhoeven of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc) 1632c2e51ac3SGeert Uytterhoeven { 1633c2e51ac3SGeert Uytterhoeven struct spi_device *spi; 1634c2e51ac3SGeert Uytterhoeven int rc; 1635c2e51ac3SGeert Uytterhoeven 1636c2e51ac3SGeert Uytterhoeven /* Alloc an spi_device */ 16378caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 1638c2e51ac3SGeert Uytterhoeven if (!spi) { 163925c56c88SRob Herring dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc); 1640c2e51ac3SGeert Uytterhoeven rc = -ENOMEM; 1641c2e51ac3SGeert Uytterhoeven goto err_out; 1642c2e51ac3SGeert Uytterhoeven } 1643c2e51ac3SGeert Uytterhoeven 1644c2e51ac3SGeert Uytterhoeven /* Select device driver */ 1645c2e51ac3SGeert Uytterhoeven rc = of_modalias_node(nc, spi->modalias, 1646c2e51ac3SGeert Uytterhoeven sizeof(spi->modalias)); 1647c2e51ac3SGeert Uytterhoeven if (rc < 0) { 164825c56c88SRob Herring dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc); 1649c2e51ac3SGeert Uytterhoeven goto err_out; 1650c2e51ac3SGeert Uytterhoeven } 1651c2e51ac3SGeert Uytterhoeven 16528caab75fSGeert Uytterhoeven rc = of_spi_parse_dt(ctlr, spi, nc); 1653c2e51ac3SGeert Uytterhoeven if (rc) 1654c2e51ac3SGeert Uytterhoeven goto err_out; 1655c2e51ac3SGeert Uytterhoeven 1656d57a4282SGrant Likely /* Store a pointer to the node in the device structure */ 1657d57a4282SGrant Likely of_node_get(nc); 1658d57a4282SGrant Likely spi->dev.of_node = nc; 1659d57a4282SGrant Likely 1660d57a4282SGrant Likely /* Register the new device */ 1661d57a4282SGrant Likely rc = spi_add_device(spi); 1662d57a4282SGrant Likely if (rc) { 166325c56c88SRob Herring dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc); 16648324147fSJohan Hovold goto err_of_node_put; 1665d57a4282SGrant Likely } 1666d57a4282SGrant Likely 1667aff5e3f8SPantelis Antoniou return spi; 1668aff5e3f8SPantelis Antoniou 16698324147fSJohan Hovold err_of_node_put: 16708324147fSJohan Hovold of_node_put(nc); 1671aff5e3f8SPantelis Antoniou err_out: 1672aff5e3f8SPantelis Antoniou spi_dev_put(spi); 1673aff5e3f8SPantelis Antoniou return ERR_PTR(rc); 1674aff5e3f8SPantelis Antoniou } 1675aff5e3f8SPantelis Antoniou 1676aff5e3f8SPantelis Antoniou /** 1677aff5e3f8SPantelis Antoniou * of_register_spi_devices() - Register child devices onto the SPI bus 16788caab75fSGeert Uytterhoeven * @ctlr: Pointer to spi_controller device 1679aff5e3f8SPantelis Antoniou * 16806c364062SGeert Uytterhoeven * Registers an spi_device for each child node of controller node which 16816c364062SGeert Uytterhoeven * represents a valid SPI slave. 1682aff5e3f8SPantelis Antoniou */ 16838caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) 1684aff5e3f8SPantelis Antoniou { 1685aff5e3f8SPantelis Antoniou struct spi_device *spi; 1686aff5e3f8SPantelis Antoniou struct device_node *nc; 1687aff5e3f8SPantelis Antoniou 16888caab75fSGeert Uytterhoeven if (!ctlr->dev.of_node) 1689aff5e3f8SPantelis Antoniou return; 1690aff5e3f8SPantelis Antoniou 16918caab75fSGeert Uytterhoeven for_each_available_child_of_node(ctlr->dev.of_node, nc) { 1692bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(nc, OF_POPULATED)) 1693bd6c1644SGeert Uytterhoeven continue; 16948caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, nc); 1695e0af98a7SRalf Ramsauer if (IS_ERR(spi)) { 16968caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, 169725c56c88SRob Herring "Failed to create SPI device for %pOF\n", nc); 1698e0af98a7SRalf Ramsauer of_node_clear_flag(nc, OF_POPULATED); 1699e0af98a7SRalf Ramsauer } 1700d57a4282SGrant Likely } 1701d57a4282SGrant Likely } 1702d57a4282SGrant Likely #else 17038caab75fSGeert Uytterhoeven static void of_register_spi_devices(struct spi_controller *ctlr) { } 1704d57a4282SGrant Likely #endif 1705d57a4282SGrant Likely 170664bee4d2SMika Westerberg #ifdef CONFIG_ACPI 17078a2e487eSLukas Wunner static void acpi_spi_parse_apple_properties(struct spi_device *spi) 17088a2e487eSLukas Wunner { 17098a2e487eSLukas Wunner struct acpi_device *dev = ACPI_COMPANION(&spi->dev); 17108a2e487eSLukas Wunner const union acpi_object *obj; 17118a2e487eSLukas Wunner 17128a2e487eSLukas Wunner if (!x86_apple_machine) 17138a2e487eSLukas Wunner return; 17148a2e487eSLukas Wunner 17158a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj) 17168a2e487eSLukas Wunner && obj->buffer.length >= 4) 17178a2e487eSLukas Wunner spi->max_speed_hz = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer; 17188a2e487eSLukas Wunner 17198a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj) 17208a2e487eSLukas Wunner && obj->buffer.length == 8) 17218a2e487eSLukas Wunner spi->bits_per_word = *(u64 *)obj->buffer.pointer; 17228a2e487eSLukas Wunner 17238a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj) 17248a2e487eSLukas Wunner && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer) 17258a2e487eSLukas Wunner spi->mode |= SPI_LSB_FIRST; 17268a2e487eSLukas Wunner 17278a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj) 17288a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 17298a2e487eSLukas Wunner spi->mode |= SPI_CPOL; 17308a2e487eSLukas Wunner 17318a2e487eSLukas Wunner if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj) 17328a2e487eSLukas Wunner && obj->buffer.length == 8 && *(u64 *)obj->buffer.pointer) 17338a2e487eSLukas Wunner spi->mode |= SPI_CPHA; 17348a2e487eSLukas Wunner } 17358a2e487eSLukas Wunner 173664bee4d2SMika Westerberg static int acpi_spi_add_resource(struct acpi_resource *ares, void *data) 173764bee4d2SMika Westerberg { 173864bee4d2SMika Westerberg struct spi_device *spi = data; 17398caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 174064bee4d2SMika Westerberg 174164bee4d2SMika Westerberg if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { 174264bee4d2SMika Westerberg struct acpi_resource_spi_serialbus *sb; 174364bee4d2SMika Westerberg 174464bee4d2SMika Westerberg sb = &ares->data.spi_serial_bus; 174564bee4d2SMika Westerberg if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) { 1746a0a90718SMika Westerberg /* 1747a0a90718SMika Westerberg * ACPI DeviceSelection numbering is handled by the 1748a0a90718SMika Westerberg * host controller driver in Windows and can vary 1749a0a90718SMika Westerberg * from driver to driver. In Linux we always expect 1750a0a90718SMika Westerberg * 0 .. max - 1 so we need to ask the driver to 1751a0a90718SMika Westerberg * translate between the two schemes. 1752a0a90718SMika Westerberg */ 17538caab75fSGeert Uytterhoeven if (ctlr->fw_translate_cs) { 17548caab75fSGeert Uytterhoeven int cs = ctlr->fw_translate_cs(ctlr, 1755a0a90718SMika Westerberg sb->device_selection); 1756a0a90718SMika Westerberg if (cs < 0) 1757a0a90718SMika Westerberg return cs; 1758a0a90718SMika Westerberg spi->chip_select = cs; 1759a0a90718SMika Westerberg } else { 176064bee4d2SMika Westerberg spi->chip_select = sb->device_selection; 1761a0a90718SMika Westerberg } 1762a0a90718SMika Westerberg 176364bee4d2SMika Westerberg spi->max_speed_hz = sb->connection_speed; 176464bee4d2SMika Westerberg 176564bee4d2SMika Westerberg if (sb->clock_phase == ACPI_SPI_SECOND_PHASE) 176664bee4d2SMika Westerberg spi->mode |= SPI_CPHA; 176764bee4d2SMika Westerberg if (sb->clock_polarity == ACPI_SPI_START_HIGH) 176864bee4d2SMika Westerberg spi->mode |= SPI_CPOL; 176964bee4d2SMika Westerberg if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH) 177064bee4d2SMika Westerberg spi->mode |= SPI_CS_HIGH; 177164bee4d2SMika Westerberg } 177264bee4d2SMika Westerberg } else if (spi->irq < 0) { 177364bee4d2SMika Westerberg struct resource r; 177464bee4d2SMika Westerberg 177564bee4d2SMika Westerberg if (acpi_dev_resource_interrupt(ares, 0, &r)) 177664bee4d2SMika Westerberg spi->irq = r.start; 177764bee4d2SMika Westerberg } 177864bee4d2SMika Westerberg 177964bee4d2SMika Westerberg /* Always tell the ACPI core to skip this resource */ 178064bee4d2SMika Westerberg return 1; 178164bee4d2SMika Westerberg } 178264bee4d2SMika Westerberg 17838caab75fSGeert Uytterhoeven static acpi_status acpi_register_spi_device(struct spi_controller *ctlr, 17847f24467fSOctavian Purdila struct acpi_device *adev) 178564bee4d2SMika Westerberg { 178664bee4d2SMika Westerberg struct list_head resource_list; 178764bee4d2SMika Westerberg struct spi_device *spi; 178864bee4d2SMika Westerberg int ret; 178964bee4d2SMika Westerberg 17907f24467fSOctavian Purdila if (acpi_bus_get_status(adev) || !adev->status.present || 17917f24467fSOctavian Purdila acpi_device_enumerated(adev)) 179264bee4d2SMika Westerberg return AE_OK; 179364bee4d2SMika Westerberg 17948caab75fSGeert Uytterhoeven spi = spi_alloc_device(ctlr); 179564bee4d2SMika Westerberg if (!spi) { 17968caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n", 179764bee4d2SMika Westerberg dev_name(&adev->dev)); 179864bee4d2SMika Westerberg return AE_NO_MEMORY; 179964bee4d2SMika Westerberg } 180064bee4d2SMika Westerberg 18017b199811SRafael J. Wysocki ACPI_COMPANION_SET(&spi->dev, adev); 180264bee4d2SMika Westerberg spi->irq = -1; 180364bee4d2SMika Westerberg 180464bee4d2SMika Westerberg INIT_LIST_HEAD(&resource_list); 180564bee4d2SMika Westerberg ret = acpi_dev_get_resources(adev, &resource_list, 180664bee4d2SMika Westerberg acpi_spi_add_resource, spi); 180764bee4d2SMika Westerberg acpi_dev_free_resource_list(&resource_list); 180864bee4d2SMika Westerberg 18098a2e487eSLukas Wunner acpi_spi_parse_apple_properties(spi); 18108a2e487eSLukas Wunner 181164bee4d2SMika Westerberg if (ret < 0 || !spi->max_speed_hz) { 181264bee4d2SMika Westerberg spi_dev_put(spi); 181364bee4d2SMika Westerberg return AE_OK; 181464bee4d2SMika Westerberg } 181564bee4d2SMika Westerberg 18160c6543f6SDan O'Donovan acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias, 18170c6543f6SDan O'Donovan sizeof(spi->modalias)); 18180c6543f6SDan O'Donovan 181933ada67dSChristophe RICARD if (spi->irq < 0) 182033ada67dSChristophe RICARD spi->irq = acpi_dev_gpio_irq_get(adev, 0); 182133ada67dSChristophe RICARD 18227f24467fSOctavian Purdila acpi_device_set_enumerated(adev); 18237f24467fSOctavian Purdila 182433cf00e5SMika Westerberg adev->power.flags.ignore_parent = true; 182564bee4d2SMika Westerberg if (spi_add_device(spi)) { 182633cf00e5SMika Westerberg adev->power.flags.ignore_parent = false; 18278caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n", 182864bee4d2SMika Westerberg dev_name(&adev->dev)); 182964bee4d2SMika Westerberg spi_dev_put(spi); 183064bee4d2SMika Westerberg } 183164bee4d2SMika Westerberg 183264bee4d2SMika Westerberg return AE_OK; 183364bee4d2SMika Westerberg } 183464bee4d2SMika Westerberg 18357f24467fSOctavian Purdila static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level, 18367f24467fSOctavian Purdila void *data, void **return_value) 18377f24467fSOctavian Purdila { 18388caab75fSGeert Uytterhoeven struct spi_controller *ctlr = data; 18397f24467fSOctavian Purdila struct acpi_device *adev; 18407f24467fSOctavian Purdila 18417f24467fSOctavian Purdila if (acpi_bus_get_device(handle, &adev)) 18427f24467fSOctavian Purdila return AE_OK; 18437f24467fSOctavian Purdila 18448caab75fSGeert Uytterhoeven return acpi_register_spi_device(ctlr, adev); 18457f24467fSOctavian Purdila } 18467f24467fSOctavian Purdila 18478caab75fSGeert Uytterhoeven static void acpi_register_spi_devices(struct spi_controller *ctlr) 184864bee4d2SMika Westerberg { 184964bee4d2SMika Westerberg acpi_status status; 185064bee4d2SMika Westerberg acpi_handle handle; 185164bee4d2SMika Westerberg 18528caab75fSGeert Uytterhoeven handle = ACPI_HANDLE(ctlr->dev.parent); 185364bee4d2SMika Westerberg if (!handle) 185464bee4d2SMika Westerberg return; 185564bee4d2SMika Westerberg 185664bee4d2SMika Westerberg status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, 18578caab75fSGeert Uytterhoeven acpi_spi_add_device, NULL, ctlr, NULL); 185864bee4d2SMika Westerberg if (ACPI_FAILURE(status)) 18598caab75fSGeert Uytterhoeven dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n"); 186064bee4d2SMika Westerberg } 186164bee4d2SMika Westerberg #else 18628caab75fSGeert Uytterhoeven static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {} 186364bee4d2SMika Westerberg #endif /* CONFIG_ACPI */ 186464bee4d2SMika Westerberg 18658caab75fSGeert Uytterhoeven static void spi_controller_release(struct device *dev) 18668ae12a0dSDavid Brownell { 18678caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 18688ae12a0dSDavid Brownell 18698caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 18708caab75fSGeert Uytterhoeven kfree(ctlr); 18718ae12a0dSDavid Brownell } 18728ae12a0dSDavid Brownell 18738ae12a0dSDavid Brownell static struct class spi_master_class = { 18748ae12a0dSDavid Brownell .name = "spi_master", 18758ae12a0dSDavid Brownell .owner = THIS_MODULE, 18768caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 1877eca2ebc7SMartin Sperl .dev_groups = spi_master_groups, 18788ae12a0dSDavid Brownell }; 18798ae12a0dSDavid Brownell 18806c364062SGeert Uytterhoeven #ifdef CONFIG_SPI_SLAVE 18816c364062SGeert Uytterhoeven /** 18826c364062SGeert Uytterhoeven * spi_slave_abort - abort the ongoing transfer request on an SPI slave 18836c364062SGeert Uytterhoeven * controller 18846c364062SGeert Uytterhoeven * @spi: device used for the current transfer 18856c364062SGeert Uytterhoeven */ 18866c364062SGeert Uytterhoeven int spi_slave_abort(struct spi_device *spi) 18876c364062SGeert Uytterhoeven { 18888caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 18896c364062SGeert Uytterhoeven 18908caab75fSGeert Uytterhoeven if (spi_controller_is_slave(ctlr) && ctlr->slave_abort) 18918caab75fSGeert Uytterhoeven return ctlr->slave_abort(ctlr); 18926c364062SGeert Uytterhoeven 18936c364062SGeert Uytterhoeven return -ENOTSUPP; 18946c364062SGeert Uytterhoeven } 18956c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_slave_abort); 18966c364062SGeert Uytterhoeven 18976c364062SGeert Uytterhoeven static int match_true(struct device *dev, void *data) 18986c364062SGeert Uytterhoeven { 18996c364062SGeert Uytterhoeven return 1; 19006c364062SGeert Uytterhoeven } 19016c364062SGeert Uytterhoeven 19026c364062SGeert Uytterhoeven static ssize_t spi_slave_show(struct device *dev, 19036c364062SGeert Uytterhoeven struct device_attribute *attr, char *buf) 19046c364062SGeert Uytterhoeven { 19058caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 19068caab75fSGeert Uytterhoeven dev); 19076c364062SGeert Uytterhoeven struct device *child; 19086c364062SGeert Uytterhoeven 19096c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 19106c364062SGeert Uytterhoeven return sprintf(buf, "%s\n", 19116c364062SGeert Uytterhoeven child ? to_spi_device(child)->modalias : NULL); 19126c364062SGeert Uytterhoeven } 19136c364062SGeert Uytterhoeven 19146c364062SGeert Uytterhoeven static ssize_t spi_slave_store(struct device *dev, 19156c364062SGeert Uytterhoeven struct device_attribute *attr, const char *buf, 19166c364062SGeert Uytterhoeven size_t count) 19176c364062SGeert Uytterhoeven { 19188caab75fSGeert Uytterhoeven struct spi_controller *ctlr = container_of(dev, struct spi_controller, 19198caab75fSGeert Uytterhoeven dev); 19206c364062SGeert Uytterhoeven struct spi_device *spi; 19216c364062SGeert Uytterhoeven struct device *child; 19226c364062SGeert Uytterhoeven char name[32]; 19236c364062SGeert Uytterhoeven int rc; 19246c364062SGeert Uytterhoeven 19256c364062SGeert Uytterhoeven rc = sscanf(buf, "%31s", name); 19266c364062SGeert Uytterhoeven if (rc != 1 || !name[0]) 19276c364062SGeert Uytterhoeven return -EINVAL; 19286c364062SGeert Uytterhoeven 19296c364062SGeert Uytterhoeven child = device_find_child(&ctlr->dev, NULL, match_true); 19306c364062SGeert Uytterhoeven if (child) { 19316c364062SGeert Uytterhoeven /* Remove registered slave */ 19326c364062SGeert Uytterhoeven device_unregister(child); 19336c364062SGeert Uytterhoeven put_device(child); 19346c364062SGeert Uytterhoeven } 19356c364062SGeert Uytterhoeven 19366c364062SGeert Uytterhoeven if (strcmp(name, "(null)")) { 19376c364062SGeert Uytterhoeven /* Register new slave */ 19386c364062SGeert Uytterhoeven spi = spi_alloc_device(ctlr); 19396c364062SGeert Uytterhoeven if (!spi) 19406c364062SGeert Uytterhoeven return -ENOMEM; 19416c364062SGeert Uytterhoeven 19426c364062SGeert Uytterhoeven strlcpy(spi->modalias, name, sizeof(spi->modalias)); 19436c364062SGeert Uytterhoeven 19446c364062SGeert Uytterhoeven rc = spi_add_device(spi); 19456c364062SGeert Uytterhoeven if (rc) { 19466c364062SGeert Uytterhoeven spi_dev_put(spi); 19476c364062SGeert Uytterhoeven return rc; 19486c364062SGeert Uytterhoeven } 19496c364062SGeert Uytterhoeven } 19506c364062SGeert Uytterhoeven 19516c364062SGeert Uytterhoeven return count; 19526c364062SGeert Uytterhoeven } 19536c364062SGeert Uytterhoeven 19546c364062SGeert Uytterhoeven static DEVICE_ATTR(slave, 0644, spi_slave_show, spi_slave_store); 19556c364062SGeert Uytterhoeven 19566c364062SGeert Uytterhoeven static struct attribute *spi_slave_attrs[] = { 19576c364062SGeert Uytterhoeven &dev_attr_slave.attr, 19586c364062SGeert Uytterhoeven NULL, 19596c364062SGeert Uytterhoeven }; 19606c364062SGeert Uytterhoeven 19616c364062SGeert Uytterhoeven static const struct attribute_group spi_slave_group = { 19626c364062SGeert Uytterhoeven .attrs = spi_slave_attrs, 19636c364062SGeert Uytterhoeven }; 19646c364062SGeert Uytterhoeven 19656c364062SGeert Uytterhoeven static const struct attribute_group *spi_slave_groups[] = { 19668caab75fSGeert Uytterhoeven &spi_controller_statistics_group, 19676c364062SGeert Uytterhoeven &spi_slave_group, 19686c364062SGeert Uytterhoeven NULL, 19696c364062SGeert Uytterhoeven }; 19706c364062SGeert Uytterhoeven 19716c364062SGeert Uytterhoeven static struct class spi_slave_class = { 19726c364062SGeert Uytterhoeven .name = "spi_slave", 19736c364062SGeert Uytterhoeven .owner = THIS_MODULE, 19748caab75fSGeert Uytterhoeven .dev_release = spi_controller_release, 19756c364062SGeert Uytterhoeven .dev_groups = spi_slave_groups, 19766c364062SGeert Uytterhoeven }; 19776c364062SGeert Uytterhoeven #else 19786c364062SGeert Uytterhoeven extern struct class spi_slave_class; /* dummy */ 19796c364062SGeert Uytterhoeven #endif 19808ae12a0dSDavid Brownell 19818ae12a0dSDavid Brownell /** 19826c364062SGeert Uytterhoeven * __spi_alloc_controller - allocate an SPI master or slave controller 19838ae12a0dSDavid Brownell * @dev: the controller, possibly using the platform_bus 198433e34dc6SDavid Brownell * @size: how much zeroed driver-private data to allocate; the pointer to this 198549dce689STony Jones * memory is in the driver_data field of the returned device, 19868caab75fSGeert Uytterhoeven * accessible with spi_controller_get_devdata(). 19876c364062SGeert Uytterhoeven * @slave: flag indicating whether to allocate an SPI master (false) or SPI 19886c364062SGeert Uytterhoeven * slave (true) controller 198933e34dc6SDavid Brownell * Context: can sleep 19908ae12a0dSDavid Brownell * 19916c364062SGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 19928ae12a0dSDavid Brownell * only ones directly touching chip registers. It's how they allocate 19938caab75fSGeert Uytterhoeven * an spi_controller structure, prior to calling spi_register_controller(). 19948ae12a0dSDavid Brownell * 199597d56dc6SJavier Martinez Canillas * This must be called from context that can sleep. 19968ae12a0dSDavid Brownell * 19976c364062SGeert Uytterhoeven * The caller is responsible for assigning the bus number and initializing the 19988caab75fSGeert Uytterhoeven * controller's methods before calling spi_register_controller(); and (after 19998caab75fSGeert Uytterhoeven * errors adding the device) calling spi_controller_put() to prevent a memory 20008caab75fSGeert Uytterhoeven * leak. 200197d56dc6SJavier Martinez Canillas * 20026c364062SGeert Uytterhoeven * Return: the SPI controller structure on success, else NULL. 20038ae12a0dSDavid Brownell */ 20048caab75fSGeert Uytterhoeven struct spi_controller *__spi_alloc_controller(struct device *dev, 20056c364062SGeert Uytterhoeven unsigned int size, bool slave) 20068ae12a0dSDavid Brownell { 20078caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 20088ae12a0dSDavid Brownell 20090c868461SDavid Brownell if (!dev) 20100c868461SDavid Brownell return NULL; 20110c868461SDavid Brownell 20128caab75fSGeert Uytterhoeven ctlr = kzalloc(size + sizeof(*ctlr), GFP_KERNEL); 20138caab75fSGeert Uytterhoeven if (!ctlr) 20148ae12a0dSDavid Brownell return NULL; 20158ae12a0dSDavid Brownell 20168caab75fSGeert Uytterhoeven device_initialize(&ctlr->dev); 20178caab75fSGeert Uytterhoeven ctlr->bus_num = -1; 20188caab75fSGeert Uytterhoeven ctlr->num_chipselect = 1; 20198caab75fSGeert Uytterhoeven ctlr->slave = slave; 20206c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave) 20218caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_slave_class; 20226c364062SGeert Uytterhoeven else 20238caab75fSGeert Uytterhoeven ctlr->dev.class = &spi_master_class; 20248caab75fSGeert Uytterhoeven ctlr->dev.parent = dev; 20258caab75fSGeert Uytterhoeven pm_suspend_ignore_children(&ctlr->dev, true); 20268caab75fSGeert Uytterhoeven spi_controller_set_devdata(ctlr, &ctlr[1]); 20278ae12a0dSDavid Brownell 20288caab75fSGeert Uytterhoeven return ctlr; 20298ae12a0dSDavid Brownell } 20306c364062SGeert Uytterhoeven EXPORT_SYMBOL_GPL(__spi_alloc_controller); 20318ae12a0dSDavid Brownell 203274317984SJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_OF 20338caab75fSGeert Uytterhoeven static int of_spi_register_master(struct spi_controller *ctlr) 203474317984SJean-Christophe PLAGNIOL-VILLARD { 2035e80beb27SGrant Likely int nb, i, *cs; 20368caab75fSGeert Uytterhoeven struct device_node *np = ctlr->dev.of_node; 203774317984SJean-Christophe PLAGNIOL-VILLARD 203874317984SJean-Christophe PLAGNIOL-VILLARD if (!np) 203974317984SJean-Christophe PLAGNIOL-VILLARD return 0; 204074317984SJean-Christophe PLAGNIOL-VILLARD 204174317984SJean-Christophe PLAGNIOL-VILLARD nb = of_gpio_named_count(np, "cs-gpios"); 20428caab75fSGeert Uytterhoeven ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect); 204374317984SJean-Christophe PLAGNIOL-VILLARD 20448ec5d84eSAndreas Larsson /* Return error only for an incorrectly formed cs-gpios property */ 20458ec5d84eSAndreas Larsson if (nb == 0 || nb == -ENOENT) 204674317984SJean-Christophe PLAGNIOL-VILLARD return 0; 20478ec5d84eSAndreas Larsson else if (nb < 0) 20488ec5d84eSAndreas Larsson return nb; 204974317984SJean-Christophe PLAGNIOL-VILLARD 20508caab75fSGeert Uytterhoeven cs = devm_kzalloc(&ctlr->dev, sizeof(int) * ctlr->num_chipselect, 205174317984SJean-Christophe PLAGNIOL-VILLARD GFP_KERNEL); 20528caab75fSGeert Uytterhoeven ctlr->cs_gpios = cs; 205374317984SJean-Christophe PLAGNIOL-VILLARD 20548caab75fSGeert Uytterhoeven if (!ctlr->cs_gpios) 205574317984SJean-Christophe PLAGNIOL-VILLARD return -ENOMEM; 205674317984SJean-Christophe PLAGNIOL-VILLARD 20578caab75fSGeert Uytterhoeven for (i = 0; i < ctlr->num_chipselect; i++) 2058446411e1SAndreas Larsson cs[i] = -ENOENT; 205974317984SJean-Christophe PLAGNIOL-VILLARD 206074317984SJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < nb; i++) 206174317984SJean-Christophe PLAGNIOL-VILLARD cs[i] = of_get_named_gpio(np, "cs-gpios", i); 206274317984SJean-Christophe PLAGNIOL-VILLARD 206374317984SJean-Christophe PLAGNIOL-VILLARD return 0; 206474317984SJean-Christophe PLAGNIOL-VILLARD } 206574317984SJean-Christophe PLAGNIOL-VILLARD #else 20668caab75fSGeert Uytterhoeven static int of_spi_register_master(struct spi_controller *ctlr) 206774317984SJean-Christophe PLAGNIOL-VILLARD { 206874317984SJean-Christophe PLAGNIOL-VILLARD return 0; 206974317984SJean-Christophe PLAGNIOL-VILLARD } 207074317984SJean-Christophe PLAGNIOL-VILLARD #endif 207174317984SJean-Christophe PLAGNIOL-VILLARD 2072bdf3a3b5SBoris Brezillon static int spi_controller_check_ops(struct spi_controller *ctlr) 2073bdf3a3b5SBoris Brezillon { 2074bdf3a3b5SBoris Brezillon /* 2075*b5932f5cSBoris Brezillon * The controller may implement only the high-level SPI-memory like 2076*b5932f5cSBoris Brezillon * operations if it does not support regular SPI transfers, and this is 2077*b5932f5cSBoris Brezillon * valid use case. 2078*b5932f5cSBoris Brezillon * If ->mem_ops is NULL, we request that at least one of the 2079*b5932f5cSBoris Brezillon * ->transfer_xxx() method be implemented. 2080bdf3a3b5SBoris Brezillon */ 2081*b5932f5cSBoris Brezillon if (ctlr->mem_ops) { 2082*b5932f5cSBoris Brezillon if (!ctlr->mem_ops->exec_op) 2083bdf3a3b5SBoris Brezillon return -EINVAL; 2084*b5932f5cSBoris Brezillon } else if (!ctlr->transfer && !ctlr->transfer_one && 2085*b5932f5cSBoris Brezillon !ctlr->transfer_one_message) { 2086*b5932f5cSBoris Brezillon return -EINVAL; 2087*b5932f5cSBoris Brezillon } 2088bdf3a3b5SBoris Brezillon 2089bdf3a3b5SBoris Brezillon return 0; 2090bdf3a3b5SBoris Brezillon } 2091bdf3a3b5SBoris Brezillon 20928ae12a0dSDavid Brownell /** 20938caab75fSGeert Uytterhoeven * spi_register_controller - register SPI master or slave controller 20948caab75fSGeert Uytterhoeven * @ctlr: initialized master, originally from spi_alloc_master() or 20958caab75fSGeert Uytterhoeven * spi_alloc_slave() 209633e34dc6SDavid Brownell * Context: can sleep 20978ae12a0dSDavid Brownell * 20988caab75fSGeert Uytterhoeven * SPI controllers connect to their drivers using some non-SPI bus, 20998ae12a0dSDavid Brownell * such as the platform bus. The final stage of probe() in that code 21008caab75fSGeert Uytterhoeven * includes calling spi_register_controller() to hook up to this SPI bus glue. 21018ae12a0dSDavid Brownell * 21028ae12a0dSDavid Brownell * SPI controllers use board specific (often SOC specific) bus numbers, 21038ae12a0dSDavid Brownell * and board-specific addressing for SPI devices combines those numbers 21048ae12a0dSDavid Brownell * with chip select numbers. Since SPI does not directly support dynamic 21058ae12a0dSDavid Brownell * device identification, boards need configuration tables telling which 21068ae12a0dSDavid Brownell * chip is at which address. 21078ae12a0dSDavid Brownell * 21088ae12a0dSDavid Brownell * This must be called from context that can sleep. It returns zero on 21098caab75fSGeert Uytterhoeven * success, else a negative error code (dropping the controller's refcount). 21100c868461SDavid Brownell * After a successful return, the caller is responsible for calling 21118caab75fSGeert Uytterhoeven * spi_unregister_controller(). 211297d56dc6SJavier Martinez Canillas * 211397d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 21148ae12a0dSDavid Brownell */ 21158caab75fSGeert Uytterhoeven int spi_register_controller(struct spi_controller *ctlr) 21168ae12a0dSDavid Brownell { 21178caab75fSGeert Uytterhoeven struct device *dev = ctlr->dev.parent; 21182b9603a0SFeng Tang struct boardinfo *bi; 21198ae12a0dSDavid Brownell int status = -ENODEV; 212042bdd706SLucas Stach int id, first_dynamic; 21218ae12a0dSDavid Brownell 21220c868461SDavid Brownell if (!dev) 21230c868461SDavid Brownell return -ENODEV; 21240c868461SDavid Brownell 2125bdf3a3b5SBoris Brezillon /* 2126bdf3a3b5SBoris Brezillon * Make sure all necessary hooks are implemented before registering 2127bdf3a3b5SBoris Brezillon * the SPI controller. 2128bdf3a3b5SBoris Brezillon */ 2129bdf3a3b5SBoris Brezillon status = spi_controller_check_ops(ctlr); 2130bdf3a3b5SBoris Brezillon if (status) 2131bdf3a3b5SBoris Brezillon return status; 2132bdf3a3b5SBoris Brezillon 21338caab75fSGeert Uytterhoeven if (!spi_controller_is_slave(ctlr)) { 21348caab75fSGeert Uytterhoeven status = of_spi_register_master(ctlr); 213574317984SJean-Christophe PLAGNIOL-VILLARD if (status) 213674317984SJean-Christophe PLAGNIOL-VILLARD return status; 21376c364062SGeert Uytterhoeven } 213874317984SJean-Christophe PLAGNIOL-VILLARD 2139082c8cb4SDavid Brownell /* even if it's just one always-selected device, there must 2140082c8cb4SDavid Brownell * be at least one chipselect 2141082c8cb4SDavid Brownell */ 21428caab75fSGeert Uytterhoeven if (ctlr->num_chipselect == 0) 2143082c8cb4SDavid Brownell return -EINVAL; 21449b61e302SSuniel Mahesh /* allocate dynamic bus number using Linux idr */ 21459b61e302SSuniel Mahesh if ((ctlr->bus_num < 0) && ctlr->dev.of_node) { 21469b61e302SSuniel Mahesh id = of_alias_get_id(ctlr->dev.of_node, "spi"); 21479b61e302SSuniel Mahesh if (id >= 0) { 21489b61e302SSuniel Mahesh ctlr->bus_num = id; 21499b61e302SSuniel Mahesh mutex_lock(&board_lock); 21509b61e302SSuniel Mahesh id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num, 21519b61e302SSuniel Mahesh ctlr->bus_num + 1, GFP_KERNEL); 21529b61e302SSuniel Mahesh mutex_unlock(&board_lock); 21539b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 21549b61e302SSuniel Mahesh return id == -ENOSPC ? -EBUSY : id; 21559b61e302SSuniel Mahesh } 21569b61e302SSuniel Mahesh } 21578caab75fSGeert Uytterhoeven if (ctlr->bus_num < 0) { 215842bdd706SLucas Stach first_dynamic = of_alias_get_highest_id("spi"); 215942bdd706SLucas Stach if (first_dynamic < 0) 216042bdd706SLucas Stach first_dynamic = 0; 216142bdd706SLucas Stach else 216242bdd706SLucas Stach first_dynamic++; 216342bdd706SLucas Stach 21649b61e302SSuniel Mahesh mutex_lock(&board_lock); 216542bdd706SLucas Stach id = idr_alloc(&spi_master_idr, ctlr, first_dynamic, 216642bdd706SLucas Stach 0, GFP_KERNEL); 21679b61e302SSuniel Mahesh mutex_unlock(&board_lock); 21689b61e302SSuniel Mahesh if (WARN(id < 0, "couldn't get idr")) 21699b61e302SSuniel Mahesh return id; 21709b61e302SSuniel Mahesh ctlr->bus_num = id; 21718ae12a0dSDavid Brownell } 21728caab75fSGeert Uytterhoeven INIT_LIST_HEAD(&ctlr->queue); 21738caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->queue_lock); 21748caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->bus_lock_spinlock); 21758caab75fSGeert Uytterhoeven mutex_init(&ctlr->bus_lock_mutex); 21768caab75fSGeert Uytterhoeven mutex_init(&ctlr->io_mutex); 21778caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 21788caab75fSGeert Uytterhoeven init_completion(&ctlr->xfer_completion); 21798caab75fSGeert Uytterhoeven if (!ctlr->max_dma_len) 21808caab75fSGeert Uytterhoeven ctlr->max_dma_len = INT_MAX; 2181cf32b71eSErnst Schwab 21828ae12a0dSDavid Brownell /* register the device, then userspace will see it. 21838ae12a0dSDavid Brownell * registration fails if the bus ID is in use. 21848ae12a0dSDavid Brownell */ 21858caab75fSGeert Uytterhoeven dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num); 21868caab75fSGeert Uytterhoeven status = device_add(&ctlr->dev); 21879b61e302SSuniel Mahesh if (status < 0) { 21889b61e302SSuniel Mahesh /* free bus id */ 21899b61e302SSuniel Mahesh mutex_lock(&board_lock); 21909b61e302SSuniel Mahesh idr_remove(&spi_master_idr, ctlr->bus_num); 21919b61e302SSuniel Mahesh mutex_unlock(&board_lock); 21928ae12a0dSDavid Brownell goto done; 21939b61e302SSuniel Mahesh } 21949b61e302SSuniel Mahesh dev_dbg(dev, "registered %s %s\n", 21958caab75fSGeert Uytterhoeven spi_controller_is_slave(ctlr) ? "slave" : "master", 21969b61e302SSuniel Mahesh dev_name(&ctlr->dev)); 21978ae12a0dSDavid Brownell 2198*b5932f5cSBoris Brezillon /* 2199*b5932f5cSBoris Brezillon * If we're using a queued driver, start the queue. Note that we don't 2200*b5932f5cSBoris Brezillon * need the queueing logic if the driver is only supporting high-level 2201*b5932f5cSBoris Brezillon * memory operations. 2202*b5932f5cSBoris Brezillon */ 2203*b5932f5cSBoris Brezillon if (ctlr->transfer) { 22048caab75fSGeert Uytterhoeven dev_info(dev, "controller is unqueued, this is deprecated\n"); 2205*b5932f5cSBoris Brezillon } else if (ctlr->transfer_one || ctlr->transfer_one_message) { 22068caab75fSGeert Uytterhoeven status = spi_controller_initialize_queue(ctlr); 2207ffbbdd21SLinus Walleij if (status) { 22088caab75fSGeert Uytterhoeven device_del(&ctlr->dev); 22099b61e302SSuniel Mahesh /* free bus id */ 22109b61e302SSuniel Mahesh mutex_lock(&board_lock); 22119b61e302SSuniel Mahesh idr_remove(&spi_master_idr, ctlr->bus_num); 22129b61e302SSuniel Mahesh mutex_unlock(&board_lock); 2213ffbbdd21SLinus Walleij goto done; 2214ffbbdd21SLinus Walleij } 2215ffbbdd21SLinus Walleij } 2216eca2ebc7SMartin Sperl /* add statistics */ 22178caab75fSGeert Uytterhoeven spin_lock_init(&ctlr->statistics.lock); 2218ffbbdd21SLinus Walleij 22192b9603a0SFeng Tang mutex_lock(&board_lock); 22208caab75fSGeert Uytterhoeven list_add_tail(&ctlr->list, &spi_controller_list); 22212b9603a0SFeng Tang list_for_each_entry(bi, &board_list, list) 22228caab75fSGeert Uytterhoeven spi_match_controller_to_boardinfo(ctlr, &bi->board_info); 22232b9603a0SFeng Tang mutex_unlock(&board_lock); 22242b9603a0SFeng Tang 222564bee4d2SMika Westerberg /* Register devices from the device tree and ACPI */ 22268caab75fSGeert Uytterhoeven of_register_spi_devices(ctlr); 22278caab75fSGeert Uytterhoeven acpi_register_spi_devices(ctlr); 22288ae12a0dSDavid Brownell done: 22298ae12a0dSDavid Brownell return status; 22308ae12a0dSDavid Brownell } 22318caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_register_controller); 22328ae12a0dSDavid Brownell 2233666d5b4cSMark Brown static void devm_spi_unregister(struct device *dev, void *res) 2234666d5b4cSMark Brown { 22358caab75fSGeert Uytterhoeven spi_unregister_controller(*(struct spi_controller **)res); 2236666d5b4cSMark Brown } 2237666d5b4cSMark Brown 2238666d5b4cSMark Brown /** 22398caab75fSGeert Uytterhoeven * devm_spi_register_controller - register managed SPI master or slave 22408caab75fSGeert Uytterhoeven * controller 22418caab75fSGeert Uytterhoeven * @dev: device managing SPI controller 22428caab75fSGeert Uytterhoeven * @ctlr: initialized controller, originally from spi_alloc_master() or 22438caab75fSGeert Uytterhoeven * spi_alloc_slave() 2244666d5b4cSMark Brown * Context: can sleep 2245666d5b4cSMark Brown * 22468caab75fSGeert Uytterhoeven * Register a SPI device as with spi_register_controller() which will 224768b892f1SJohan Hovold * automatically be unregistered and freed. 224897d56dc6SJavier Martinez Canillas * 224997d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 2250666d5b4cSMark Brown */ 22518caab75fSGeert Uytterhoeven int devm_spi_register_controller(struct device *dev, 22528caab75fSGeert Uytterhoeven struct spi_controller *ctlr) 2253666d5b4cSMark Brown { 22548caab75fSGeert Uytterhoeven struct spi_controller **ptr; 2255666d5b4cSMark Brown int ret; 2256666d5b4cSMark Brown 2257666d5b4cSMark Brown ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); 2258666d5b4cSMark Brown if (!ptr) 2259666d5b4cSMark Brown return -ENOMEM; 2260666d5b4cSMark Brown 22618caab75fSGeert Uytterhoeven ret = spi_register_controller(ctlr); 22624b92894eSStephen Warren if (!ret) { 22638caab75fSGeert Uytterhoeven *ptr = ctlr; 2264666d5b4cSMark Brown devres_add(dev, ptr); 2265666d5b4cSMark Brown } else { 2266666d5b4cSMark Brown devres_free(ptr); 2267666d5b4cSMark Brown } 2268666d5b4cSMark Brown 2269666d5b4cSMark Brown return ret; 2270666d5b4cSMark Brown } 22718caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(devm_spi_register_controller); 2272666d5b4cSMark Brown 227334860089SDavid Lamparter static int __unregister(struct device *dev, void *null) 22748ae12a0dSDavid Brownell { 22750c868461SDavid Brownell spi_unregister_device(to_spi_device(dev)); 22768ae12a0dSDavid Brownell return 0; 22778ae12a0dSDavid Brownell } 22788ae12a0dSDavid Brownell 22798ae12a0dSDavid Brownell /** 22808caab75fSGeert Uytterhoeven * spi_unregister_controller - unregister SPI master or slave controller 22818caab75fSGeert Uytterhoeven * @ctlr: the controller being unregistered 228233e34dc6SDavid Brownell * Context: can sleep 22838ae12a0dSDavid Brownell * 22848caab75fSGeert Uytterhoeven * This call is used only by SPI controller drivers, which are the 22858ae12a0dSDavid Brownell * only ones directly touching chip registers. 22868ae12a0dSDavid Brownell * 22878ae12a0dSDavid Brownell * This must be called from context that can sleep. 228868b892f1SJohan Hovold * 228968b892f1SJohan Hovold * Note that this function also drops a reference to the controller. 22908ae12a0dSDavid Brownell */ 22918caab75fSGeert Uytterhoeven void spi_unregister_controller(struct spi_controller *ctlr) 22928ae12a0dSDavid Brownell { 22939b61e302SSuniel Mahesh struct spi_controller *found; 229467f7b278SJohan Hovold int id = ctlr->bus_num; 229589fc9a1aSJeff Garzik int dummy; 229689fc9a1aSJeff Garzik 22979b61e302SSuniel Mahesh /* First make sure that this controller was ever added */ 22989b61e302SSuniel Mahesh mutex_lock(&board_lock); 229967f7b278SJohan Hovold found = idr_find(&spi_master_idr, id); 23009b61e302SSuniel Mahesh mutex_unlock(&board_lock); 23018caab75fSGeert Uytterhoeven if (ctlr->queued) { 23028caab75fSGeert Uytterhoeven if (spi_destroy_queue(ctlr)) 23038caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue remove failed\n"); 2304ffbbdd21SLinus Walleij } 23052b9603a0SFeng Tang mutex_lock(&board_lock); 23068caab75fSGeert Uytterhoeven list_del(&ctlr->list); 23072b9603a0SFeng Tang mutex_unlock(&board_lock); 23082b9603a0SFeng Tang 23098caab75fSGeert Uytterhoeven dummy = device_for_each_child(&ctlr->dev, NULL, __unregister); 23108caab75fSGeert Uytterhoeven device_unregister(&ctlr->dev); 23119b61e302SSuniel Mahesh /* free bus id */ 23129b61e302SSuniel Mahesh mutex_lock(&board_lock); 2313613bd1eaSJarkko Nikula if (found == ctlr) 231467f7b278SJohan Hovold idr_remove(&spi_master_idr, id); 23159b61e302SSuniel Mahesh mutex_unlock(&board_lock); 23168ae12a0dSDavid Brownell } 23178caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_unregister_controller); 23188ae12a0dSDavid Brownell 23198caab75fSGeert Uytterhoeven int spi_controller_suspend(struct spi_controller *ctlr) 2320ffbbdd21SLinus Walleij { 2321ffbbdd21SLinus Walleij int ret; 2322ffbbdd21SLinus Walleij 23238caab75fSGeert Uytterhoeven /* Basically no-ops for non-queued controllers */ 23248caab75fSGeert Uytterhoeven if (!ctlr->queued) 2325ffbbdd21SLinus Walleij return 0; 2326ffbbdd21SLinus Walleij 23278caab75fSGeert Uytterhoeven ret = spi_stop_queue(ctlr); 2328ffbbdd21SLinus Walleij if (ret) 23298caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue stop failed\n"); 2330ffbbdd21SLinus Walleij 2331ffbbdd21SLinus Walleij return ret; 2332ffbbdd21SLinus Walleij } 23338caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_suspend); 2334ffbbdd21SLinus Walleij 23358caab75fSGeert Uytterhoeven int spi_controller_resume(struct spi_controller *ctlr) 2336ffbbdd21SLinus Walleij { 2337ffbbdd21SLinus Walleij int ret; 2338ffbbdd21SLinus Walleij 23398caab75fSGeert Uytterhoeven if (!ctlr->queued) 2340ffbbdd21SLinus Walleij return 0; 2341ffbbdd21SLinus Walleij 23428caab75fSGeert Uytterhoeven ret = spi_start_queue(ctlr); 2343ffbbdd21SLinus Walleij if (ret) 23448caab75fSGeert Uytterhoeven dev_err(&ctlr->dev, "queue restart failed\n"); 2345ffbbdd21SLinus Walleij 2346ffbbdd21SLinus Walleij return ret; 2347ffbbdd21SLinus Walleij } 23488caab75fSGeert Uytterhoeven EXPORT_SYMBOL_GPL(spi_controller_resume); 2349ffbbdd21SLinus Walleij 23508caab75fSGeert Uytterhoeven static int __spi_controller_match(struct device *dev, const void *data) 23515ed2c832SDave Young { 23528caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 23539f3b795aSMichał Mirosław const u16 *bus_num = data; 23545ed2c832SDave Young 23558caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 23568caab75fSGeert Uytterhoeven return ctlr->bus_num == *bus_num; 23575ed2c832SDave Young } 23585ed2c832SDave Young 23598ae12a0dSDavid Brownell /** 23608ae12a0dSDavid Brownell * spi_busnum_to_master - look up master associated with bus_num 23618ae12a0dSDavid Brownell * @bus_num: the master's bus number 236233e34dc6SDavid Brownell * Context: can sleep 23638ae12a0dSDavid Brownell * 23648ae12a0dSDavid Brownell * This call may be used with devices that are registered after 23658ae12a0dSDavid Brownell * arch init time. It returns a refcounted pointer to the relevant 23668caab75fSGeert Uytterhoeven * spi_controller (which the caller must release), or NULL if there is 23678ae12a0dSDavid Brownell * no such master registered. 236897d56dc6SJavier Martinez Canillas * 236997d56dc6SJavier Martinez Canillas * Return: the SPI master structure on success, else NULL. 23708ae12a0dSDavid Brownell */ 23718caab75fSGeert Uytterhoeven struct spi_controller *spi_busnum_to_master(u16 bus_num) 23728ae12a0dSDavid Brownell { 237349dce689STony Jones struct device *dev; 23748caab75fSGeert Uytterhoeven struct spi_controller *ctlr = NULL; 23758ae12a0dSDavid Brownell 2376695794aeSGreg Kroah-Hartman dev = class_find_device(&spi_master_class, NULL, &bus_num, 23778caab75fSGeert Uytterhoeven __spi_controller_match); 23785ed2c832SDave Young if (dev) 23798caab75fSGeert Uytterhoeven ctlr = container_of(dev, struct spi_controller, dev); 23805ed2c832SDave Young /* reference got in class_find_device */ 23818caab75fSGeert Uytterhoeven return ctlr; 23828ae12a0dSDavid Brownell } 23838ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_busnum_to_master); 23848ae12a0dSDavid Brownell 2385d780c371SMartin Sperl /*-------------------------------------------------------------------------*/ 2386d780c371SMartin Sperl 2387d780c371SMartin Sperl /* Core methods for SPI resource management */ 2388d780c371SMartin Sperl 2389d780c371SMartin Sperl /** 2390d780c371SMartin Sperl * spi_res_alloc - allocate a spi resource that is life-cycle managed 2391d780c371SMartin Sperl * during the processing of a spi_message while using 2392d780c371SMartin Sperl * spi_transfer_one 2393d780c371SMartin Sperl * @spi: the spi device for which we allocate memory 2394d780c371SMartin Sperl * @release: the release code to execute for this resource 2395d780c371SMartin Sperl * @size: size to alloc and return 2396d780c371SMartin Sperl * @gfp: GFP allocation flags 2397d780c371SMartin Sperl * 2398d780c371SMartin Sperl * Return: the pointer to the allocated data 2399d780c371SMartin Sperl * 2400d780c371SMartin Sperl * This may get enhanced in the future to allocate from a memory pool 24018caab75fSGeert Uytterhoeven * of the @spi_device or @spi_controller to avoid repeated allocations. 2402d780c371SMartin Sperl */ 2403d780c371SMartin Sperl void *spi_res_alloc(struct spi_device *spi, 2404d780c371SMartin Sperl spi_res_release_t release, 2405d780c371SMartin Sperl size_t size, gfp_t gfp) 2406d780c371SMartin Sperl { 2407d780c371SMartin Sperl struct spi_res *sres; 2408d780c371SMartin Sperl 2409d780c371SMartin Sperl sres = kzalloc(sizeof(*sres) + size, gfp); 2410d780c371SMartin Sperl if (!sres) 2411d780c371SMartin Sperl return NULL; 2412d780c371SMartin Sperl 2413d780c371SMartin Sperl INIT_LIST_HEAD(&sres->entry); 2414d780c371SMartin Sperl sres->release = release; 2415d780c371SMartin Sperl 2416d780c371SMartin Sperl return sres->data; 2417d780c371SMartin Sperl } 2418d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_alloc); 2419d780c371SMartin Sperl 2420d780c371SMartin Sperl /** 2421d780c371SMartin Sperl * spi_res_free - free an spi resource 2422d780c371SMartin Sperl * @res: pointer to the custom data of a resource 2423d780c371SMartin Sperl * 2424d780c371SMartin Sperl */ 2425d780c371SMartin Sperl void spi_res_free(void *res) 2426d780c371SMartin Sperl { 2427d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 2428d780c371SMartin Sperl 2429d780c371SMartin Sperl if (!res) 2430d780c371SMartin Sperl return; 2431d780c371SMartin Sperl 2432d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 2433d780c371SMartin Sperl kfree(sres); 2434d780c371SMartin Sperl } 2435d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_free); 2436d780c371SMartin Sperl 2437d780c371SMartin Sperl /** 2438d780c371SMartin Sperl * spi_res_add - add a spi_res to the spi_message 2439d780c371SMartin Sperl * @message: the spi message 2440d780c371SMartin Sperl * @res: the spi_resource 2441d780c371SMartin Sperl */ 2442d780c371SMartin Sperl void spi_res_add(struct spi_message *message, void *res) 2443d780c371SMartin Sperl { 2444d780c371SMartin Sperl struct spi_res *sres = container_of(res, struct spi_res, data); 2445d780c371SMartin Sperl 2446d780c371SMartin Sperl WARN_ON(!list_empty(&sres->entry)); 2447d780c371SMartin Sperl list_add_tail(&sres->entry, &message->resources); 2448d780c371SMartin Sperl } 2449d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_add); 2450d780c371SMartin Sperl 2451d780c371SMartin Sperl /** 2452d780c371SMartin Sperl * spi_res_release - release all spi resources for this message 24538caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller 2454d780c371SMartin Sperl * @message: the @spi_message 2455d780c371SMartin Sperl */ 24568caab75fSGeert Uytterhoeven void spi_res_release(struct spi_controller *ctlr, struct spi_message *message) 2457d780c371SMartin Sperl { 2458d780c371SMartin Sperl struct spi_res *res; 2459d780c371SMartin Sperl 2460d780c371SMartin Sperl while (!list_empty(&message->resources)) { 2461d780c371SMartin Sperl res = list_last_entry(&message->resources, 2462d780c371SMartin Sperl struct spi_res, entry); 2463d780c371SMartin Sperl 2464d780c371SMartin Sperl if (res->release) 24658caab75fSGeert Uytterhoeven res->release(ctlr, message, res->data); 2466d780c371SMartin Sperl 2467d780c371SMartin Sperl list_del(&res->entry); 2468d780c371SMartin Sperl 2469d780c371SMartin Sperl kfree(res); 2470d780c371SMartin Sperl } 2471d780c371SMartin Sperl } 2472d780c371SMartin Sperl EXPORT_SYMBOL_GPL(spi_res_release); 24738ae12a0dSDavid Brownell 24748ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 24758ae12a0dSDavid Brownell 2476523baf5aSMartin Sperl /* Core methods for spi_message alterations */ 2477523baf5aSMartin Sperl 24788caab75fSGeert Uytterhoeven static void __spi_replace_transfers_release(struct spi_controller *ctlr, 2479523baf5aSMartin Sperl struct spi_message *msg, 2480523baf5aSMartin Sperl void *res) 2481523baf5aSMartin Sperl { 2482523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer = res; 2483523baf5aSMartin Sperl size_t i; 2484523baf5aSMartin Sperl 2485523baf5aSMartin Sperl /* call extra callback if requested */ 2486523baf5aSMartin Sperl if (rxfer->release) 24878caab75fSGeert Uytterhoeven rxfer->release(ctlr, msg, res); 2488523baf5aSMartin Sperl 2489523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 2490523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, rxfer->replaced_after); 2491523baf5aSMartin Sperl 2492523baf5aSMartin Sperl /* remove the formerly inserted entries */ 2493523baf5aSMartin Sperl for (i = 0; i < rxfer->inserted; i++) 2494523baf5aSMartin Sperl list_del(&rxfer->inserted_transfers[i].transfer_list); 2495523baf5aSMartin Sperl } 2496523baf5aSMartin Sperl 2497523baf5aSMartin Sperl /** 2498523baf5aSMartin Sperl * spi_replace_transfers - replace transfers with several transfers 2499523baf5aSMartin Sperl * and register change with spi_message.resources 2500523baf5aSMartin Sperl * @msg: the spi_message we work upon 2501523baf5aSMartin Sperl * @xfer_first: the first spi_transfer we want to replace 2502523baf5aSMartin Sperl * @remove: number of transfers to remove 2503523baf5aSMartin Sperl * @insert: the number of transfers we want to insert instead 2504523baf5aSMartin Sperl * @release: extra release code necessary in some circumstances 2505523baf5aSMartin Sperl * @extradatasize: extra data to allocate (with alignment guarantees 2506523baf5aSMartin Sperl * of struct @spi_transfer) 250705885397SMartin Sperl * @gfp: gfp flags 2508523baf5aSMartin Sperl * 2509523baf5aSMartin Sperl * Returns: pointer to @spi_replaced_transfers, 2510523baf5aSMartin Sperl * PTR_ERR(...) in case of errors. 2511523baf5aSMartin Sperl */ 2512523baf5aSMartin Sperl struct spi_replaced_transfers *spi_replace_transfers( 2513523baf5aSMartin Sperl struct spi_message *msg, 2514523baf5aSMartin Sperl struct spi_transfer *xfer_first, 2515523baf5aSMartin Sperl size_t remove, 2516523baf5aSMartin Sperl size_t insert, 2517523baf5aSMartin Sperl spi_replaced_release_t release, 2518523baf5aSMartin Sperl size_t extradatasize, 2519523baf5aSMartin Sperl gfp_t gfp) 2520523baf5aSMartin Sperl { 2521523baf5aSMartin Sperl struct spi_replaced_transfers *rxfer; 2522523baf5aSMartin Sperl struct spi_transfer *xfer; 2523523baf5aSMartin Sperl size_t i; 2524523baf5aSMartin Sperl 2525523baf5aSMartin Sperl /* allocate the structure using spi_res */ 2526523baf5aSMartin Sperl rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release, 2527523baf5aSMartin Sperl insert * sizeof(struct spi_transfer) 2528523baf5aSMartin Sperl + sizeof(struct spi_replaced_transfers) 2529523baf5aSMartin Sperl + extradatasize, 2530523baf5aSMartin Sperl gfp); 2531523baf5aSMartin Sperl if (!rxfer) 2532523baf5aSMartin Sperl return ERR_PTR(-ENOMEM); 2533523baf5aSMartin Sperl 2534523baf5aSMartin Sperl /* the release code to invoke before running the generic release */ 2535523baf5aSMartin Sperl rxfer->release = release; 2536523baf5aSMartin Sperl 2537523baf5aSMartin Sperl /* assign extradata */ 2538523baf5aSMartin Sperl if (extradatasize) 2539523baf5aSMartin Sperl rxfer->extradata = 2540523baf5aSMartin Sperl &rxfer->inserted_transfers[insert]; 2541523baf5aSMartin Sperl 2542523baf5aSMartin Sperl /* init the replaced_transfers list */ 2543523baf5aSMartin Sperl INIT_LIST_HEAD(&rxfer->replaced_transfers); 2544523baf5aSMartin Sperl 2545523baf5aSMartin Sperl /* assign the list_entry after which we should reinsert 2546523baf5aSMartin Sperl * the @replaced_transfers - it may be spi_message.messages! 2547523baf5aSMartin Sperl */ 2548523baf5aSMartin Sperl rxfer->replaced_after = xfer_first->transfer_list.prev; 2549523baf5aSMartin Sperl 2550523baf5aSMartin Sperl /* remove the requested number of transfers */ 2551523baf5aSMartin Sperl for (i = 0; i < remove; i++) { 2552523baf5aSMartin Sperl /* if the entry after replaced_after it is msg->transfers 2553523baf5aSMartin Sperl * then we have been requested to remove more transfers 2554523baf5aSMartin Sperl * than are in the list 2555523baf5aSMartin Sperl */ 2556523baf5aSMartin Sperl if (rxfer->replaced_after->next == &msg->transfers) { 2557523baf5aSMartin Sperl dev_err(&msg->spi->dev, 2558523baf5aSMartin Sperl "requested to remove more spi_transfers than are available\n"); 2559523baf5aSMartin Sperl /* insert replaced transfers back into the message */ 2560523baf5aSMartin Sperl list_splice(&rxfer->replaced_transfers, 2561523baf5aSMartin Sperl rxfer->replaced_after); 2562523baf5aSMartin Sperl 2563523baf5aSMartin Sperl /* free the spi_replace_transfer structure */ 2564523baf5aSMartin Sperl spi_res_free(rxfer); 2565523baf5aSMartin Sperl 2566523baf5aSMartin Sperl /* and return with an error */ 2567523baf5aSMartin Sperl return ERR_PTR(-EINVAL); 2568523baf5aSMartin Sperl } 2569523baf5aSMartin Sperl 2570523baf5aSMartin Sperl /* remove the entry after replaced_after from list of 2571523baf5aSMartin Sperl * transfers and add it to list of replaced_transfers 2572523baf5aSMartin Sperl */ 2573523baf5aSMartin Sperl list_move_tail(rxfer->replaced_after->next, 2574523baf5aSMartin Sperl &rxfer->replaced_transfers); 2575523baf5aSMartin Sperl } 2576523baf5aSMartin Sperl 2577523baf5aSMartin Sperl /* create copy of the given xfer with identical settings 2578523baf5aSMartin Sperl * based on the first transfer to get removed 2579523baf5aSMartin Sperl */ 2580523baf5aSMartin Sperl for (i = 0; i < insert; i++) { 2581523baf5aSMartin Sperl /* we need to run in reverse order */ 2582523baf5aSMartin Sperl xfer = &rxfer->inserted_transfers[insert - 1 - i]; 2583523baf5aSMartin Sperl 2584523baf5aSMartin Sperl /* copy all spi_transfer data */ 2585523baf5aSMartin Sperl memcpy(xfer, xfer_first, sizeof(*xfer)); 2586523baf5aSMartin Sperl 2587523baf5aSMartin Sperl /* add to list */ 2588523baf5aSMartin Sperl list_add(&xfer->transfer_list, rxfer->replaced_after); 2589523baf5aSMartin Sperl 2590523baf5aSMartin Sperl /* clear cs_change and delay_usecs for all but the last */ 2591523baf5aSMartin Sperl if (i) { 2592523baf5aSMartin Sperl xfer->cs_change = false; 2593523baf5aSMartin Sperl xfer->delay_usecs = 0; 2594523baf5aSMartin Sperl } 2595523baf5aSMartin Sperl } 2596523baf5aSMartin Sperl 2597523baf5aSMartin Sperl /* set up inserted */ 2598523baf5aSMartin Sperl rxfer->inserted = insert; 2599523baf5aSMartin Sperl 2600523baf5aSMartin Sperl /* and register it with spi_res/spi_message */ 2601523baf5aSMartin Sperl spi_res_add(msg, rxfer); 2602523baf5aSMartin Sperl 2603523baf5aSMartin Sperl return rxfer; 2604523baf5aSMartin Sperl } 2605523baf5aSMartin Sperl EXPORT_SYMBOL_GPL(spi_replace_transfers); 2606523baf5aSMartin Sperl 26078caab75fSGeert Uytterhoeven static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, 2608d9f12122SMartin Sperl struct spi_message *msg, 2609d9f12122SMartin Sperl struct spi_transfer **xferp, 2610d9f12122SMartin Sperl size_t maxsize, 2611d9f12122SMartin Sperl gfp_t gfp) 2612d9f12122SMartin Sperl { 2613d9f12122SMartin Sperl struct spi_transfer *xfer = *xferp, *xfers; 2614d9f12122SMartin Sperl struct spi_replaced_transfers *srt; 2615d9f12122SMartin Sperl size_t offset; 2616d9f12122SMartin Sperl size_t count, i; 2617d9f12122SMartin Sperl 2618d9f12122SMartin Sperl /* warn once about this fact that we are splitting a transfer */ 2619d9f12122SMartin Sperl dev_warn_once(&msg->spi->dev, 26207d62f51eSFabio Estevam "spi_transfer of length %i exceed max length of %zu - needed to split transfers\n", 2621d9f12122SMartin Sperl xfer->len, maxsize); 2622d9f12122SMartin Sperl 2623d9f12122SMartin Sperl /* calculate how many we have to replace */ 2624d9f12122SMartin Sperl count = DIV_ROUND_UP(xfer->len, maxsize); 2625d9f12122SMartin Sperl 2626d9f12122SMartin Sperl /* create replacement */ 2627d9f12122SMartin Sperl srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp); 2628657d32efSDan Carpenter if (IS_ERR(srt)) 2629657d32efSDan Carpenter return PTR_ERR(srt); 2630d9f12122SMartin Sperl xfers = srt->inserted_transfers; 2631d9f12122SMartin Sperl 2632d9f12122SMartin Sperl /* now handle each of those newly inserted spi_transfers 2633d9f12122SMartin Sperl * note that the replacements spi_transfers all are preset 2634d9f12122SMartin Sperl * to the same values as *xferp, so tx_buf, rx_buf and len 2635d9f12122SMartin Sperl * are all identical (as well as most others) 2636d9f12122SMartin Sperl * so we just have to fix up len and the pointers. 2637d9f12122SMartin Sperl * 2638d9f12122SMartin Sperl * this also includes support for the depreciated 2639d9f12122SMartin Sperl * spi_message.is_dma_mapped interface 2640d9f12122SMartin Sperl */ 2641d9f12122SMartin Sperl 2642d9f12122SMartin Sperl /* the first transfer just needs the length modified, so we 2643d9f12122SMartin Sperl * run it outside the loop 2644d9f12122SMartin Sperl */ 2645c8dab77aSFabio Estevam xfers[0].len = min_t(size_t, maxsize, xfer[0].len); 2646d9f12122SMartin Sperl 2647d9f12122SMartin Sperl /* all the others need rx_buf/tx_buf also set */ 2648d9f12122SMartin Sperl for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) { 2649d9f12122SMartin Sperl /* update rx_buf, tx_buf and dma */ 2650d9f12122SMartin Sperl if (xfers[i].rx_buf) 2651d9f12122SMartin Sperl xfers[i].rx_buf += offset; 2652d9f12122SMartin Sperl if (xfers[i].rx_dma) 2653d9f12122SMartin Sperl xfers[i].rx_dma += offset; 2654d9f12122SMartin Sperl if (xfers[i].tx_buf) 2655d9f12122SMartin Sperl xfers[i].tx_buf += offset; 2656d9f12122SMartin Sperl if (xfers[i].tx_dma) 2657d9f12122SMartin Sperl xfers[i].tx_dma += offset; 2658d9f12122SMartin Sperl 2659d9f12122SMartin Sperl /* update length */ 2660d9f12122SMartin Sperl xfers[i].len = min(maxsize, xfers[i].len - offset); 2661d9f12122SMartin Sperl } 2662d9f12122SMartin Sperl 2663d9f12122SMartin Sperl /* we set up xferp to the last entry we have inserted, 2664d9f12122SMartin Sperl * so that we skip those already split transfers 2665d9f12122SMartin Sperl */ 2666d9f12122SMartin Sperl *xferp = &xfers[count - 1]; 2667d9f12122SMartin Sperl 2668d9f12122SMartin Sperl /* increment statistics counters */ 26698caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 2670d9f12122SMartin Sperl transfers_split_maxsize); 2671d9f12122SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics, 2672d9f12122SMartin Sperl transfers_split_maxsize); 2673d9f12122SMartin Sperl 2674d9f12122SMartin Sperl return 0; 2675d9f12122SMartin Sperl } 2676d9f12122SMartin Sperl 2677d9f12122SMartin Sperl /** 2678d9f12122SMartin Sperl * spi_split_tranfers_maxsize - split spi transfers into multiple transfers 2679d9f12122SMartin Sperl * when an individual transfer exceeds a 2680d9f12122SMartin Sperl * certain size 26818caab75fSGeert Uytterhoeven * @ctlr: the @spi_controller for this transfer 26823700ce95SMasanari Iida * @msg: the @spi_message to transform 26833700ce95SMasanari Iida * @maxsize: the maximum when to apply this 268410f11a22SJavier Martinez Canillas * @gfp: GFP allocation flags 2685d9f12122SMartin Sperl * 2686d9f12122SMartin Sperl * Return: status of transformation 2687d9f12122SMartin Sperl */ 26888caab75fSGeert Uytterhoeven int spi_split_transfers_maxsize(struct spi_controller *ctlr, 2689d9f12122SMartin Sperl struct spi_message *msg, 2690d9f12122SMartin Sperl size_t maxsize, 2691d9f12122SMartin Sperl gfp_t gfp) 2692d9f12122SMartin Sperl { 2693d9f12122SMartin Sperl struct spi_transfer *xfer; 2694d9f12122SMartin Sperl int ret; 2695d9f12122SMartin Sperl 2696d9f12122SMartin Sperl /* iterate over the transfer_list, 2697d9f12122SMartin Sperl * but note that xfer is advanced to the last transfer inserted 2698d9f12122SMartin Sperl * to avoid checking sizes again unnecessarily (also xfer does 2699d9f12122SMartin Sperl * potentiall belong to a different list by the time the 2700d9f12122SMartin Sperl * replacement has happened 2701d9f12122SMartin Sperl */ 2702d9f12122SMartin Sperl list_for_each_entry(xfer, &msg->transfers, transfer_list) { 2703d9f12122SMartin Sperl if (xfer->len > maxsize) { 27048caab75fSGeert Uytterhoeven ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer, 27058caab75fSGeert Uytterhoeven maxsize, gfp); 2706d9f12122SMartin Sperl if (ret) 2707d9f12122SMartin Sperl return ret; 2708d9f12122SMartin Sperl } 2709d9f12122SMartin Sperl } 2710d9f12122SMartin Sperl 2711d9f12122SMartin Sperl return 0; 2712d9f12122SMartin Sperl } 2713d9f12122SMartin Sperl EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); 27148ae12a0dSDavid Brownell 27158ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 27168ae12a0dSDavid Brownell 27178caab75fSGeert Uytterhoeven /* Core methods for SPI controller protocol drivers. Some of the 27187d077197SDavid Brownell * other core methods are currently defined as inline functions. 27197d077197SDavid Brownell */ 27207d077197SDavid Brownell 27218caab75fSGeert Uytterhoeven static int __spi_validate_bits_per_word(struct spi_controller *ctlr, 27228caab75fSGeert Uytterhoeven u8 bits_per_word) 272363ab645fSStefan Brüns { 27248caab75fSGeert Uytterhoeven if (ctlr->bits_per_word_mask) { 272563ab645fSStefan Brüns /* Only 32 bits fit in the mask */ 272663ab645fSStefan Brüns if (bits_per_word > 32) 272763ab645fSStefan Brüns return -EINVAL; 27288caab75fSGeert Uytterhoeven if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word))) 272963ab645fSStefan Brüns return -EINVAL; 273063ab645fSStefan Brüns } 273163ab645fSStefan Brüns 273263ab645fSStefan Brüns return 0; 273363ab645fSStefan Brüns } 273463ab645fSStefan Brüns 27357d077197SDavid Brownell /** 27367d077197SDavid Brownell * spi_setup - setup SPI mode and clock rate 27377d077197SDavid Brownell * @spi: the device whose settings are being modified 27387d077197SDavid Brownell * Context: can sleep, and no requests are queued to the device 27397d077197SDavid Brownell * 27407d077197SDavid Brownell * SPI protocol drivers may need to update the transfer mode if the 27417d077197SDavid Brownell * device doesn't work with its default. They may likewise need 27427d077197SDavid Brownell * to update clock rates or word sizes from initial values. This function 27437d077197SDavid Brownell * changes those settings, and must be called from a context that can sleep. 27447d077197SDavid Brownell * Except for SPI_CS_HIGH, which takes effect immediately, the changes take 27457d077197SDavid Brownell * effect the next time the device is selected and data is transferred to 27467d077197SDavid Brownell * or from it. When this function returns, the spi device is deselected. 27477d077197SDavid Brownell * 27487d077197SDavid Brownell * Note that this call will fail if the protocol driver specifies an option 27497d077197SDavid Brownell * that the underlying controller or its driver does not support. For 27507d077197SDavid Brownell * example, not all hardware supports wire transfers using nine bit words, 27517d077197SDavid Brownell * LSB-first wire encoding, or active-high chipselects. 275297d56dc6SJavier Martinez Canillas * 275397d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 27547d077197SDavid Brownell */ 27557d077197SDavid Brownell int spi_setup(struct spi_device *spi) 27567d077197SDavid Brownell { 275783596fbeSGeert Uytterhoeven unsigned bad_bits, ugly_bits; 27585ab8d262SAndy Shevchenko int status; 27597d077197SDavid Brownell 2760f477b7fbSwangyuhang /* check mode to prevent that DUAL and QUAD set at the same time 2761f477b7fbSwangyuhang */ 2762f477b7fbSwangyuhang if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) || 2763f477b7fbSwangyuhang ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) { 2764f477b7fbSwangyuhang dev_err(&spi->dev, 2765f477b7fbSwangyuhang "setup: can not select dual and quad at the same time\n"); 2766f477b7fbSwangyuhang return -EINVAL; 2767f477b7fbSwangyuhang } 2768f477b7fbSwangyuhang /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden 2769f477b7fbSwangyuhang */ 2770f477b7fbSwangyuhang if ((spi->mode & SPI_3WIRE) && (spi->mode & 2771f477b7fbSwangyuhang (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD))) 2772f477b7fbSwangyuhang return -EINVAL; 2773e7db06b5SDavid Brownell /* help drivers fail *cleanly* when they need options 27748caab75fSGeert Uytterhoeven * that aren't supported with their current controller 2775e7db06b5SDavid Brownell */ 27768caab75fSGeert Uytterhoeven bad_bits = spi->mode & ~spi->controller->mode_bits; 277783596fbeSGeert Uytterhoeven ugly_bits = bad_bits & 277883596fbeSGeert Uytterhoeven (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD); 277983596fbeSGeert Uytterhoeven if (ugly_bits) { 278083596fbeSGeert Uytterhoeven dev_warn(&spi->dev, 278183596fbeSGeert Uytterhoeven "setup: ignoring unsupported mode bits %x\n", 278283596fbeSGeert Uytterhoeven ugly_bits); 278383596fbeSGeert Uytterhoeven spi->mode &= ~ugly_bits; 278483596fbeSGeert Uytterhoeven bad_bits &= ~ugly_bits; 278583596fbeSGeert Uytterhoeven } 2786e7db06b5SDavid Brownell if (bad_bits) { 2787eb288a1fSLinus Walleij dev_err(&spi->dev, "setup: unsupported mode bits %x\n", 2788e7db06b5SDavid Brownell bad_bits); 2789e7db06b5SDavid Brownell return -EINVAL; 2790e7db06b5SDavid Brownell } 2791e7db06b5SDavid Brownell 27927d077197SDavid Brownell if (!spi->bits_per_word) 27937d077197SDavid Brownell spi->bits_per_word = 8; 27947d077197SDavid Brownell 27958caab75fSGeert Uytterhoeven status = __spi_validate_bits_per_word(spi->controller, 27968caab75fSGeert Uytterhoeven spi->bits_per_word); 27975ab8d262SAndy Shevchenko if (status) 27985ab8d262SAndy Shevchenko return status; 279963ab645fSStefan Brüns 2800052eb2d4SAxel Lin if (!spi->max_speed_hz) 28018caab75fSGeert Uytterhoeven spi->max_speed_hz = spi->controller->max_speed_hz; 2802052eb2d4SAxel Lin 28038caab75fSGeert Uytterhoeven if (spi->controller->setup) 28048caab75fSGeert Uytterhoeven status = spi->controller->setup(spi); 28057d077197SDavid Brownell 2806abeedb01SFranklin S Cooper Jr spi_set_cs(spi, false); 2807abeedb01SFranklin S Cooper Jr 28085fe5f05eSJingoo Han dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n", 28097d077197SDavid Brownell (int) (spi->mode & (SPI_CPOL | SPI_CPHA)), 28107d077197SDavid Brownell (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "", 28117d077197SDavid Brownell (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "", 28127d077197SDavid Brownell (spi->mode & SPI_3WIRE) ? "3wire, " : "", 28137d077197SDavid Brownell (spi->mode & SPI_LOOP) ? "loopback, " : "", 28147d077197SDavid Brownell spi->bits_per_word, spi->max_speed_hz, 28157d077197SDavid Brownell status); 28167d077197SDavid Brownell 28177d077197SDavid Brownell return status; 28187d077197SDavid Brownell } 28197d077197SDavid Brownell EXPORT_SYMBOL_GPL(spi_setup); 28207d077197SDavid Brownell 282190808738SMark Brown static int __spi_validate(struct spi_device *spi, struct spi_message *message) 2822cf32b71eSErnst Schwab { 28238caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 2824e6811d1dSLaxman Dewangan struct spi_transfer *xfer; 28256ea31293SAtsushi Nemoto int w_size; 2826cf32b71eSErnst Schwab 282724a0013aSMark Brown if (list_empty(&message->transfers)) 282824a0013aSMark Brown return -EINVAL; 282924a0013aSMark Brown 2830cf32b71eSErnst Schwab /* Half-duplex links include original MicroWire, and ones with 2831cf32b71eSErnst Schwab * only one data pin like SPI_3WIRE (switches direction) or where 2832cf32b71eSErnst Schwab * either MOSI or MISO is missing. They can also be caused by 2833cf32b71eSErnst Schwab * software limitations. 2834cf32b71eSErnst Schwab */ 28358caab75fSGeert Uytterhoeven if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) || 28368caab75fSGeert Uytterhoeven (spi->mode & SPI_3WIRE)) { 28378caab75fSGeert Uytterhoeven unsigned flags = ctlr->flags; 2838cf32b71eSErnst Schwab 2839cf32b71eSErnst Schwab list_for_each_entry(xfer, &message->transfers, transfer_list) { 2840cf32b71eSErnst Schwab if (xfer->rx_buf && xfer->tx_buf) 2841cf32b71eSErnst Schwab return -EINVAL; 28428caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf) 2843cf32b71eSErnst Schwab return -EINVAL; 28448caab75fSGeert Uytterhoeven if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf) 2845cf32b71eSErnst Schwab return -EINVAL; 2846cf32b71eSErnst Schwab } 2847cf32b71eSErnst Schwab } 2848cf32b71eSErnst Schwab 2849e6811d1dSLaxman Dewangan /** 2850059b8ffeSLaxman Dewangan * Set transfer bits_per_word and max speed as spi device default if 2851059b8ffeSLaxman Dewangan * it is not set for this transfer. 2852f477b7fbSwangyuhang * Set transfer tx_nbits and rx_nbits as single transfer default 2853f477b7fbSwangyuhang * (SPI_NBITS_SINGLE) if it is not set for this transfer. 2854e6811d1dSLaxman Dewangan */ 285577e80588SMartin Sperl message->frame_length = 0; 2856e6811d1dSLaxman Dewangan list_for_each_entry(xfer, &message->transfers, transfer_list) { 2857078726ceSSourav Poddar message->frame_length += xfer->len; 2858e6811d1dSLaxman Dewangan if (!xfer->bits_per_word) 2859e6811d1dSLaxman Dewangan xfer->bits_per_word = spi->bits_per_word; 2860a6f87fadSAxel Lin 2861a6f87fadSAxel Lin if (!xfer->speed_hz) 2862059b8ffeSLaxman Dewangan xfer->speed_hz = spi->max_speed_hz; 28637dc9fbc3SMark Brown if (!xfer->speed_hz) 28648caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 2865a6f87fadSAxel Lin 28668caab75fSGeert Uytterhoeven if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz) 28678caab75fSGeert Uytterhoeven xfer->speed_hz = ctlr->max_speed_hz; 286856ede94aSGabor Juhos 28698caab75fSGeert Uytterhoeven if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word)) 2870543bb255SStephen Warren return -EINVAL; 2871a2fd4f9fSMark Brown 28724d94bd21SIvan T. Ivanov /* 28734d94bd21SIvan T. Ivanov * SPI transfer length should be multiple of SPI word size 28744d94bd21SIvan T. Ivanov * where SPI word size should be power-of-two multiple 28754d94bd21SIvan T. Ivanov */ 28764d94bd21SIvan T. Ivanov if (xfer->bits_per_word <= 8) 28774d94bd21SIvan T. Ivanov w_size = 1; 28784d94bd21SIvan T. Ivanov else if (xfer->bits_per_word <= 16) 28794d94bd21SIvan T. Ivanov w_size = 2; 28804d94bd21SIvan T. Ivanov else 28814d94bd21SIvan T. Ivanov w_size = 4; 28824d94bd21SIvan T. Ivanov 28834d94bd21SIvan T. Ivanov /* No partial transfers accepted */ 28846ea31293SAtsushi Nemoto if (xfer->len % w_size) 28854d94bd21SIvan T. Ivanov return -EINVAL; 28864d94bd21SIvan T. Ivanov 28878caab75fSGeert Uytterhoeven if (xfer->speed_hz && ctlr->min_speed_hz && 28888caab75fSGeert Uytterhoeven xfer->speed_hz < ctlr->min_speed_hz) 2889a2fd4f9fSMark Brown return -EINVAL; 2890f477b7fbSwangyuhang 2891f477b7fbSwangyuhang if (xfer->tx_buf && !xfer->tx_nbits) 2892f477b7fbSwangyuhang xfer->tx_nbits = SPI_NBITS_SINGLE; 2893f477b7fbSwangyuhang if (xfer->rx_buf && !xfer->rx_nbits) 2894f477b7fbSwangyuhang xfer->rx_nbits = SPI_NBITS_SINGLE; 2895f477b7fbSwangyuhang /* check transfer tx/rx_nbits: 28961afd9989SGeert Uytterhoeven * 1. check the value matches one of single, dual and quad 28971afd9989SGeert Uytterhoeven * 2. check tx/rx_nbits match the mode in spi_device 2898f477b7fbSwangyuhang */ 2899db90a441SSourav Poddar if (xfer->tx_buf) { 2900f477b7fbSwangyuhang if (xfer->tx_nbits != SPI_NBITS_SINGLE && 2901f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_DUAL && 2902f477b7fbSwangyuhang xfer->tx_nbits != SPI_NBITS_QUAD) 2903a2fd4f9fSMark Brown return -EINVAL; 2904f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_DUAL) && 2905f477b7fbSwangyuhang !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 2906f477b7fbSwangyuhang return -EINVAL; 2907f477b7fbSwangyuhang if ((xfer->tx_nbits == SPI_NBITS_QUAD) && 2908f477b7fbSwangyuhang !(spi->mode & SPI_TX_QUAD)) 2909f477b7fbSwangyuhang return -EINVAL; 2910db90a441SSourav Poddar } 2911f477b7fbSwangyuhang /* check transfer rx_nbits */ 2912db90a441SSourav Poddar if (xfer->rx_buf) { 2913f477b7fbSwangyuhang if (xfer->rx_nbits != SPI_NBITS_SINGLE && 2914f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_DUAL && 2915f477b7fbSwangyuhang xfer->rx_nbits != SPI_NBITS_QUAD) 2916f477b7fbSwangyuhang return -EINVAL; 2917f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_DUAL) && 2918f477b7fbSwangyuhang !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 2919f477b7fbSwangyuhang return -EINVAL; 2920f477b7fbSwangyuhang if ((xfer->rx_nbits == SPI_NBITS_QUAD) && 2921f477b7fbSwangyuhang !(spi->mode & SPI_RX_QUAD)) 2922f477b7fbSwangyuhang return -EINVAL; 2923e6811d1dSLaxman Dewangan } 2924e6811d1dSLaxman Dewangan } 2925e6811d1dSLaxman Dewangan 2926cf32b71eSErnst Schwab message->status = -EINPROGRESS; 292790808738SMark Brown 292890808738SMark Brown return 0; 292990808738SMark Brown } 293090808738SMark Brown 293190808738SMark Brown static int __spi_async(struct spi_device *spi, struct spi_message *message) 293290808738SMark Brown { 29338caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 293490808738SMark Brown 2935*b5932f5cSBoris Brezillon /* 2936*b5932f5cSBoris Brezillon * Some controllers do not support doing regular SPI transfers. Return 2937*b5932f5cSBoris Brezillon * ENOTSUPP when this is the case. 2938*b5932f5cSBoris Brezillon */ 2939*b5932f5cSBoris Brezillon if (!ctlr->transfer) 2940*b5932f5cSBoris Brezillon return -ENOTSUPP; 2941*b5932f5cSBoris Brezillon 294290808738SMark Brown message->spi = spi; 294390808738SMark Brown 29448caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async); 2945eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async); 2946eca2ebc7SMartin Sperl 294790808738SMark Brown trace_spi_message_submit(message); 294890808738SMark Brown 29498caab75fSGeert Uytterhoeven return ctlr->transfer(spi, message); 2950cf32b71eSErnst Schwab } 2951cf32b71eSErnst Schwab 2952568d0697SDavid Brownell /** 2953568d0697SDavid Brownell * spi_async - asynchronous SPI transfer 2954568d0697SDavid Brownell * @spi: device with which data will be exchanged 2955568d0697SDavid Brownell * @message: describes the data transfers, including completion callback 2956568d0697SDavid Brownell * Context: any (irqs may be blocked, etc) 2957568d0697SDavid Brownell * 2958568d0697SDavid Brownell * This call may be used in_irq and other contexts which can't sleep, 2959568d0697SDavid Brownell * as well as from task contexts which can sleep. 2960568d0697SDavid Brownell * 2961568d0697SDavid Brownell * The completion callback is invoked in a context which can't sleep. 2962568d0697SDavid Brownell * Before that invocation, the value of message->status is undefined. 2963568d0697SDavid Brownell * When the callback is issued, message->status holds either zero (to 2964568d0697SDavid Brownell * indicate complete success) or a negative error code. After that 2965568d0697SDavid Brownell * callback returns, the driver which issued the transfer request may 2966568d0697SDavid Brownell * deallocate the associated memory; it's no longer in use by any SPI 2967568d0697SDavid Brownell * core or controller driver code. 2968568d0697SDavid Brownell * 2969568d0697SDavid Brownell * Note that although all messages to a spi_device are handled in 2970568d0697SDavid Brownell * FIFO order, messages may go to different devices in other orders. 2971568d0697SDavid Brownell * Some device might be higher priority, or have various "hard" access 2972568d0697SDavid Brownell * time requirements, for example. 2973568d0697SDavid Brownell * 2974568d0697SDavid Brownell * On detection of any fault during the transfer, processing of 2975568d0697SDavid Brownell * the entire message is aborted, and the device is deselected. 2976568d0697SDavid Brownell * Until returning from the associated message completion callback, 2977568d0697SDavid Brownell * no other spi_message queued to that device will be processed. 2978568d0697SDavid Brownell * (This rule applies equally to all the synchronous transfer calls, 2979568d0697SDavid Brownell * which are wrappers around this core asynchronous primitive.) 298097d56dc6SJavier Martinez Canillas * 298197d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 2982568d0697SDavid Brownell */ 2983568d0697SDavid Brownell int spi_async(struct spi_device *spi, struct spi_message *message) 2984568d0697SDavid Brownell { 29858caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 2986cf32b71eSErnst Schwab int ret; 2987cf32b71eSErnst Schwab unsigned long flags; 2988568d0697SDavid Brownell 298990808738SMark Brown ret = __spi_validate(spi, message); 299090808738SMark Brown if (ret != 0) 299190808738SMark Brown return ret; 299290808738SMark Brown 29938caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 2994568d0697SDavid Brownell 29958caab75fSGeert Uytterhoeven if (ctlr->bus_lock_flag) 2996cf32b71eSErnst Schwab ret = -EBUSY; 2997cf32b71eSErnst Schwab else 2998cf32b71eSErnst Schwab ret = __spi_async(spi, message); 2999568d0697SDavid Brownell 30008caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3001cf32b71eSErnst Schwab 3002cf32b71eSErnst Schwab return ret; 3003568d0697SDavid Brownell } 3004568d0697SDavid Brownell EXPORT_SYMBOL_GPL(spi_async); 3005568d0697SDavid Brownell 3006cf32b71eSErnst Schwab /** 3007cf32b71eSErnst Schwab * spi_async_locked - version of spi_async with exclusive bus usage 3008cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3009cf32b71eSErnst Schwab * @message: describes the data transfers, including completion callback 3010cf32b71eSErnst Schwab * Context: any (irqs may be blocked, etc) 3011cf32b71eSErnst Schwab * 3012cf32b71eSErnst Schwab * This call may be used in_irq and other contexts which can't sleep, 3013cf32b71eSErnst Schwab * as well as from task contexts which can sleep. 3014cf32b71eSErnst Schwab * 3015cf32b71eSErnst Schwab * The completion callback is invoked in a context which can't sleep. 3016cf32b71eSErnst Schwab * Before that invocation, the value of message->status is undefined. 3017cf32b71eSErnst Schwab * When the callback is issued, message->status holds either zero (to 3018cf32b71eSErnst Schwab * indicate complete success) or a negative error code. After that 3019cf32b71eSErnst Schwab * callback returns, the driver which issued the transfer request may 3020cf32b71eSErnst Schwab * deallocate the associated memory; it's no longer in use by any SPI 3021cf32b71eSErnst Schwab * core or controller driver code. 3022cf32b71eSErnst Schwab * 3023cf32b71eSErnst Schwab * Note that although all messages to a spi_device are handled in 3024cf32b71eSErnst Schwab * FIFO order, messages may go to different devices in other orders. 3025cf32b71eSErnst Schwab * Some device might be higher priority, or have various "hard" access 3026cf32b71eSErnst Schwab * time requirements, for example. 3027cf32b71eSErnst Schwab * 3028cf32b71eSErnst Schwab * On detection of any fault during the transfer, processing of 3029cf32b71eSErnst Schwab * the entire message is aborted, and the device is deselected. 3030cf32b71eSErnst Schwab * Until returning from the associated message completion callback, 3031cf32b71eSErnst Schwab * no other spi_message queued to that device will be processed. 3032cf32b71eSErnst Schwab * (This rule applies equally to all the synchronous transfer calls, 3033cf32b71eSErnst Schwab * which are wrappers around this core asynchronous primitive.) 303497d56dc6SJavier Martinez Canillas * 303597d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3036cf32b71eSErnst Schwab */ 3037cf32b71eSErnst Schwab int spi_async_locked(struct spi_device *spi, struct spi_message *message) 3038cf32b71eSErnst Schwab { 30398caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 3040cf32b71eSErnst Schwab int ret; 3041cf32b71eSErnst Schwab unsigned long flags; 3042cf32b71eSErnst Schwab 304390808738SMark Brown ret = __spi_validate(spi, message); 304490808738SMark Brown if (ret != 0) 304590808738SMark Brown return ret; 304690808738SMark Brown 30478caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 3048cf32b71eSErnst Schwab 3049cf32b71eSErnst Schwab ret = __spi_async(spi, message); 3050cf32b71eSErnst Schwab 30518caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3052cf32b71eSErnst Schwab 3053cf32b71eSErnst Schwab return ret; 3054cf32b71eSErnst Schwab 3055cf32b71eSErnst Schwab } 3056cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_async_locked); 3057cf32b71eSErnst Schwab 30587d077197SDavid Brownell 3059556351f1SVignesh R int spi_flash_read(struct spi_device *spi, 3060556351f1SVignesh R struct spi_flash_read_message *msg) 3061556351f1SVignesh R 3062556351f1SVignesh R { 30638caab75fSGeert Uytterhoeven struct spi_controller *master = spi->controller; 3064f4502dd1SVignesh R struct device *rx_dev = NULL; 3065556351f1SVignesh R int ret; 3066556351f1SVignesh R 3067556351f1SVignesh R if ((msg->opcode_nbits == SPI_NBITS_DUAL || 3068556351f1SVignesh R msg->addr_nbits == SPI_NBITS_DUAL) && 3069556351f1SVignesh R !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD))) 3070556351f1SVignesh R return -EINVAL; 3071556351f1SVignesh R if ((msg->opcode_nbits == SPI_NBITS_QUAD || 3072556351f1SVignesh R msg->addr_nbits == SPI_NBITS_QUAD) && 3073556351f1SVignesh R !(spi->mode & SPI_TX_QUAD)) 3074556351f1SVignesh R return -EINVAL; 3075556351f1SVignesh R if (msg->data_nbits == SPI_NBITS_DUAL && 3076556351f1SVignesh R !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD))) 3077556351f1SVignesh R return -EINVAL; 3078556351f1SVignesh R if (msg->data_nbits == SPI_NBITS_QUAD && 3079556351f1SVignesh R !(spi->mode & SPI_RX_QUAD)) 3080556351f1SVignesh R return -EINVAL; 3081556351f1SVignesh R 3082556351f1SVignesh R if (master->auto_runtime_pm) { 3083556351f1SVignesh R ret = pm_runtime_get_sync(master->dev.parent); 3084556351f1SVignesh R if (ret < 0) { 3085556351f1SVignesh R dev_err(&master->dev, "Failed to power device: %d\n", 3086556351f1SVignesh R ret); 3087556351f1SVignesh R return ret; 3088556351f1SVignesh R } 3089556351f1SVignesh R } 3090f4502dd1SVignesh R 3091556351f1SVignesh R mutex_lock(&master->bus_lock_mutex); 3092ef4d96ecSMark Brown mutex_lock(&master->io_mutex); 30932bca3445SVignesh R if (master->dma_rx && master->spi_flash_can_dma(spi, msg)) { 3094f4502dd1SVignesh R rx_dev = master->dma_rx->device->dev; 3095f4502dd1SVignesh R ret = spi_map_buf(master, rx_dev, &msg->rx_sg, 3096f4502dd1SVignesh R msg->buf, msg->len, 3097f4502dd1SVignesh R DMA_FROM_DEVICE); 3098f4502dd1SVignesh R if (!ret) 3099f4502dd1SVignesh R msg->cur_msg_mapped = true; 3100f4502dd1SVignesh R } 3101556351f1SVignesh R ret = master->spi_flash_read(spi, msg); 3102f4502dd1SVignesh R if (msg->cur_msg_mapped) 3103f4502dd1SVignesh R spi_unmap_buf(master, rx_dev, &msg->rx_sg, 3104f4502dd1SVignesh R DMA_FROM_DEVICE); 3105ef4d96ecSMark Brown mutex_unlock(&master->io_mutex); 3106556351f1SVignesh R mutex_unlock(&master->bus_lock_mutex); 3107f4502dd1SVignesh R 3108556351f1SVignesh R if (master->auto_runtime_pm) 3109556351f1SVignesh R pm_runtime_put(master->dev.parent); 3110556351f1SVignesh R 3111556351f1SVignesh R return ret; 3112556351f1SVignesh R } 3113556351f1SVignesh R EXPORT_SYMBOL_GPL(spi_flash_read); 3114556351f1SVignesh R 31157d077197SDavid Brownell /*-------------------------------------------------------------------------*/ 31167d077197SDavid Brownell 31178caab75fSGeert Uytterhoeven /* Utility methods for SPI protocol drivers, layered on 31187d077197SDavid Brownell * top of the core. Some other utility methods are defined as 31197d077197SDavid Brownell * inline functions. 31207d077197SDavid Brownell */ 31217d077197SDavid Brownell 31225d870c8eSAndrew Morton static void spi_complete(void *arg) 31235d870c8eSAndrew Morton { 31245d870c8eSAndrew Morton complete(arg); 31255d870c8eSAndrew Morton } 31265d870c8eSAndrew Morton 3127ef4d96ecSMark Brown static int __spi_sync(struct spi_device *spi, struct spi_message *message) 3128cf32b71eSErnst Schwab { 3129cf32b71eSErnst Schwab DECLARE_COMPLETION_ONSTACK(done); 3130cf32b71eSErnst Schwab int status; 31318caab75fSGeert Uytterhoeven struct spi_controller *ctlr = spi->controller; 31320461a414SMark Brown unsigned long flags; 31330461a414SMark Brown 31340461a414SMark Brown status = __spi_validate(spi, message); 31350461a414SMark Brown if (status != 0) 31360461a414SMark Brown return status; 3137cf32b71eSErnst Schwab 3138cf32b71eSErnst Schwab message->complete = spi_complete; 3139cf32b71eSErnst Schwab message->context = &done; 31400461a414SMark Brown message->spi = spi; 3141cf32b71eSErnst Schwab 31428caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync); 3143eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync); 3144eca2ebc7SMartin Sperl 31450461a414SMark Brown /* If we're not using the legacy transfer method then we will 31460461a414SMark Brown * try to transfer in the calling context so special case. 31470461a414SMark Brown * This code would be less tricky if we could remove the 31480461a414SMark Brown * support for driver implemented message queues. 31490461a414SMark Brown */ 31508caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 31518caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 31520461a414SMark Brown 31530461a414SMark Brown trace_spi_message_submit(message); 31540461a414SMark Brown 31550461a414SMark Brown status = __spi_queued_transfer(spi, message, false); 31560461a414SMark Brown 31578caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 31580461a414SMark Brown } else { 3159cf32b71eSErnst Schwab status = spi_async_locked(spi, message); 31600461a414SMark Brown } 3161cf32b71eSErnst Schwab 3162cf32b71eSErnst Schwab if (status == 0) { 31630461a414SMark Brown /* Push out the messages in the calling context if we 31640461a414SMark Brown * can. 31650461a414SMark Brown */ 31668caab75fSGeert Uytterhoeven if (ctlr->transfer == spi_queued_transfer) { 31678caab75fSGeert Uytterhoeven SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, 3168eca2ebc7SMartin Sperl spi_sync_immediate); 3169eca2ebc7SMartin Sperl SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, 3170eca2ebc7SMartin Sperl spi_sync_immediate); 31718caab75fSGeert Uytterhoeven __spi_pump_messages(ctlr, false); 3172eca2ebc7SMartin Sperl } 31730461a414SMark Brown 3174cf32b71eSErnst Schwab wait_for_completion(&done); 3175cf32b71eSErnst Schwab status = message->status; 3176cf32b71eSErnst Schwab } 3177cf32b71eSErnst Schwab message->context = NULL; 3178cf32b71eSErnst Schwab return status; 3179cf32b71eSErnst Schwab } 3180cf32b71eSErnst Schwab 31818ae12a0dSDavid Brownell /** 31828ae12a0dSDavid Brownell * spi_sync - blocking/synchronous SPI data transfers 31838ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 31848ae12a0dSDavid Brownell * @message: describes the data transfers 318533e34dc6SDavid Brownell * Context: can sleep 31868ae12a0dSDavid Brownell * 31878ae12a0dSDavid Brownell * This call may only be used from a context that may sleep. The sleep 31888ae12a0dSDavid Brownell * is non-interruptible, and has no timeout. Low-overhead controller 31898ae12a0dSDavid Brownell * drivers may DMA directly into and out of the message buffers. 31908ae12a0dSDavid Brownell * 31918ae12a0dSDavid Brownell * Note that the SPI device's chip select is active during the message, 31928ae12a0dSDavid Brownell * and then is normally disabled between messages. Drivers for some 31938ae12a0dSDavid Brownell * frequently-used devices may want to minimize costs of selecting a chip, 31948ae12a0dSDavid Brownell * by leaving it selected in anticipation that the next message will go 31958ae12a0dSDavid Brownell * to the same chip. (That may increase power usage.) 31968ae12a0dSDavid Brownell * 31970c868461SDavid Brownell * Also, the caller is guaranteeing that the memory associated with the 31980c868461SDavid Brownell * message will not be freed before this call returns. 31990c868461SDavid Brownell * 320097d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 32018ae12a0dSDavid Brownell */ 32028ae12a0dSDavid Brownell int spi_sync(struct spi_device *spi, struct spi_message *message) 32038ae12a0dSDavid Brownell { 3204ef4d96ecSMark Brown int ret; 3205ef4d96ecSMark Brown 32068caab75fSGeert Uytterhoeven mutex_lock(&spi->controller->bus_lock_mutex); 3207ef4d96ecSMark Brown ret = __spi_sync(spi, message); 32088caab75fSGeert Uytterhoeven mutex_unlock(&spi->controller->bus_lock_mutex); 3209ef4d96ecSMark Brown 3210ef4d96ecSMark Brown return ret; 32118ae12a0dSDavid Brownell } 32128ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_sync); 32138ae12a0dSDavid Brownell 3214cf32b71eSErnst Schwab /** 3215cf32b71eSErnst Schwab * spi_sync_locked - version of spi_sync with exclusive bus usage 3216cf32b71eSErnst Schwab * @spi: device with which data will be exchanged 3217cf32b71eSErnst Schwab * @message: describes the data transfers 3218cf32b71eSErnst Schwab * Context: can sleep 3219cf32b71eSErnst Schwab * 3220cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3221cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. Low-overhead controller 3222cf32b71eSErnst Schwab * drivers may DMA directly into and out of the message buffers. 3223cf32b71eSErnst Schwab * 3224cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 322525985edcSLucas De Marchi * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must 3226cf32b71eSErnst Schwab * be released by a spi_bus_unlock call when the exclusive access is over. 3227cf32b71eSErnst Schwab * 322897d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 3229cf32b71eSErnst Schwab */ 3230cf32b71eSErnst Schwab int spi_sync_locked(struct spi_device *spi, struct spi_message *message) 3231cf32b71eSErnst Schwab { 3232ef4d96ecSMark Brown return __spi_sync(spi, message); 3233cf32b71eSErnst Schwab } 3234cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_sync_locked); 3235cf32b71eSErnst Schwab 3236cf32b71eSErnst Schwab /** 3237cf32b71eSErnst Schwab * spi_bus_lock - obtain a lock for exclusive SPI bus usage 32388caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that should be locked for exclusive bus access 3239cf32b71eSErnst Schwab * Context: can sleep 3240cf32b71eSErnst Schwab * 3241cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3242cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3243cf32b71eSErnst Schwab * 3244cf32b71eSErnst Schwab * This call should be used by drivers that require exclusive access to the 3245cf32b71eSErnst Schwab * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the 3246cf32b71eSErnst Schwab * exclusive access is over. Data transfer must be done by spi_sync_locked 3247cf32b71eSErnst Schwab * and spi_async_locked calls when the SPI bus lock is held. 3248cf32b71eSErnst Schwab * 324997d56dc6SJavier Martinez Canillas * Return: always zero. 3250cf32b71eSErnst Schwab */ 32518caab75fSGeert Uytterhoeven int spi_bus_lock(struct spi_controller *ctlr) 3252cf32b71eSErnst Schwab { 3253cf32b71eSErnst Schwab unsigned long flags; 3254cf32b71eSErnst Schwab 32558caab75fSGeert Uytterhoeven mutex_lock(&ctlr->bus_lock_mutex); 3256cf32b71eSErnst Schwab 32578caab75fSGeert Uytterhoeven spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); 32588caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 1; 32598caab75fSGeert Uytterhoeven spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); 3260cf32b71eSErnst Schwab 3261cf32b71eSErnst Schwab /* mutex remains locked until spi_bus_unlock is called */ 3262cf32b71eSErnst Schwab 3263cf32b71eSErnst Schwab return 0; 3264cf32b71eSErnst Schwab } 3265cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_lock); 3266cf32b71eSErnst Schwab 3267cf32b71eSErnst Schwab /** 3268cf32b71eSErnst Schwab * spi_bus_unlock - release the lock for exclusive SPI bus usage 32698caab75fSGeert Uytterhoeven * @ctlr: SPI bus master that was locked for exclusive bus access 3270cf32b71eSErnst Schwab * Context: can sleep 3271cf32b71eSErnst Schwab * 3272cf32b71eSErnst Schwab * This call may only be used from a context that may sleep. The sleep 3273cf32b71eSErnst Schwab * is non-interruptible, and has no timeout. 3274cf32b71eSErnst Schwab * 3275cf32b71eSErnst Schwab * This call releases an SPI bus lock previously obtained by an spi_bus_lock 3276cf32b71eSErnst Schwab * call. 3277cf32b71eSErnst Schwab * 327897d56dc6SJavier Martinez Canillas * Return: always zero. 3279cf32b71eSErnst Schwab */ 32808caab75fSGeert Uytterhoeven int spi_bus_unlock(struct spi_controller *ctlr) 3281cf32b71eSErnst Schwab { 32828caab75fSGeert Uytterhoeven ctlr->bus_lock_flag = 0; 3283cf32b71eSErnst Schwab 32848caab75fSGeert Uytterhoeven mutex_unlock(&ctlr->bus_lock_mutex); 3285cf32b71eSErnst Schwab 3286cf32b71eSErnst Schwab return 0; 3287cf32b71eSErnst Schwab } 3288cf32b71eSErnst Schwab EXPORT_SYMBOL_GPL(spi_bus_unlock); 3289cf32b71eSErnst Schwab 3290a9948b61SDavid Brownell /* portable code must never pass more than 32 bytes */ 3291a9948b61SDavid Brownell #define SPI_BUFSIZ max(32, SMP_CACHE_BYTES) 32928ae12a0dSDavid Brownell 32938ae12a0dSDavid Brownell static u8 *buf; 32948ae12a0dSDavid Brownell 32958ae12a0dSDavid Brownell /** 32968ae12a0dSDavid Brownell * spi_write_then_read - SPI synchronous write followed by read 32978ae12a0dSDavid Brownell * @spi: device with which data will be exchanged 32988ae12a0dSDavid Brownell * @txbuf: data to be written (need not be dma-safe) 32998ae12a0dSDavid Brownell * @n_tx: size of txbuf, in bytes 330027570497SJiri Pirko * @rxbuf: buffer into which data will be read (need not be dma-safe) 330127570497SJiri Pirko * @n_rx: size of rxbuf, in bytes 330233e34dc6SDavid Brownell * Context: can sleep 33038ae12a0dSDavid Brownell * 33048ae12a0dSDavid Brownell * This performs a half duplex MicroWire style transaction with the 33058ae12a0dSDavid Brownell * device, sending txbuf and then reading rxbuf. The return value 33068ae12a0dSDavid Brownell * is zero for success, else a negative errno status code. 3307b885244eSDavid Brownell * This call may only be used from a context that may sleep. 33088ae12a0dSDavid Brownell * 33090c868461SDavid Brownell * Parameters to this routine are always copied using a small buffer; 331033e34dc6SDavid Brownell * portable code should never use this for more than 32 bytes. 331133e34dc6SDavid Brownell * Performance-sensitive or bulk transfer code should instead use 33120c868461SDavid Brownell * spi_{async,sync}() calls with dma-safe buffers. 331397d56dc6SJavier Martinez Canillas * 331497d56dc6SJavier Martinez Canillas * Return: zero on success, else a negative error code. 33158ae12a0dSDavid Brownell */ 33168ae12a0dSDavid Brownell int spi_write_then_read(struct spi_device *spi, 33170c4a1590SMark Brown const void *txbuf, unsigned n_tx, 33180c4a1590SMark Brown void *rxbuf, unsigned n_rx) 33198ae12a0dSDavid Brownell { 3320068f4070SDavid Brownell static DEFINE_MUTEX(lock); 33218ae12a0dSDavid Brownell 33228ae12a0dSDavid Brownell int status; 33238ae12a0dSDavid Brownell struct spi_message message; 3324bdff549eSDavid Brownell struct spi_transfer x[2]; 33258ae12a0dSDavid Brownell u8 *local_buf; 33268ae12a0dSDavid Brownell 3327b3a223eeSMark Brown /* Use preallocated DMA-safe buffer if we can. We can't avoid 3328b3a223eeSMark Brown * copying here, (as a pure convenience thing), but we can 3329b3a223eeSMark Brown * keep heap costs out of the hot path unless someone else is 3330b3a223eeSMark Brown * using the pre-allocated buffer or the transfer is too large. 33318ae12a0dSDavid Brownell */ 3332b3a223eeSMark Brown if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) { 33332cd94c8aSMark Brown local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx), 33342cd94c8aSMark Brown GFP_KERNEL | GFP_DMA); 3335b3a223eeSMark Brown if (!local_buf) 3336b3a223eeSMark Brown return -ENOMEM; 3337b3a223eeSMark Brown } else { 3338b3a223eeSMark Brown local_buf = buf; 3339b3a223eeSMark Brown } 33408ae12a0dSDavid Brownell 33418275c642SVitaly Wool spi_message_init(&message); 33425fe5f05eSJingoo Han memset(x, 0, sizeof(x)); 3343bdff549eSDavid Brownell if (n_tx) { 3344bdff549eSDavid Brownell x[0].len = n_tx; 3345bdff549eSDavid Brownell spi_message_add_tail(&x[0], &message); 3346bdff549eSDavid Brownell } 3347bdff549eSDavid Brownell if (n_rx) { 3348bdff549eSDavid Brownell x[1].len = n_rx; 3349bdff549eSDavid Brownell spi_message_add_tail(&x[1], &message); 3350bdff549eSDavid Brownell } 33518275c642SVitaly Wool 33528ae12a0dSDavid Brownell memcpy(local_buf, txbuf, n_tx); 3353bdff549eSDavid Brownell x[0].tx_buf = local_buf; 3354bdff549eSDavid Brownell x[1].rx_buf = local_buf + n_tx; 33558ae12a0dSDavid Brownell 33568ae12a0dSDavid Brownell /* do the i/o */ 33578ae12a0dSDavid Brownell status = spi_sync(spi, &message); 33589b938b74SMarc Pignat if (status == 0) 3359bdff549eSDavid Brownell memcpy(rxbuf, x[1].rx_buf, n_rx); 33608ae12a0dSDavid Brownell 3361bdff549eSDavid Brownell if (x[0].tx_buf == buf) 3362068f4070SDavid Brownell mutex_unlock(&lock); 33638ae12a0dSDavid Brownell else 33648ae12a0dSDavid Brownell kfree(local_buf); 33658ae12a0dSDavid Brownell 33668ae12a0dSDavid Brownell return status; 33678ae12a0dSDavid Brownell } 33688ae12a0dSDavid Brownell EXPORT_SYMBOL_GPL(spi_write_then_read); 33698ae12a0dSDavid Brownell 33708ae12a0dSDavid Brownell /*-------------------------------------------------------------------------*/ 33718ae12a0dSDavid Brownell 3372ce79d54aSPantelis Antoniou #if IS_ENABLED(CONFIG_OF_DYNAMIC) 3373ce79d54aSPantelis Antoniou static int __spi_of_device_match(struct device *dev, void *data) 3374ce79d54aSPantelis Antoniou { 3375ce79d54aSPantelis Antoniou return dev->of_node == data; 3376ce79d54aSPantelis Antoniou } 3377ce79d54aSPantelis Antoniou 3378ce79d54aSPantelis Antoniou /* must call put_device() when done with returned spi_device device */ 3379ce79d54aSPantelis Antoniou static struct spi_device *of_find_spi_device_by_node(struct device_node *node) 3380ce79d54aSPantelis Antoniou { 3381ce79d54aSPantelis Antoniou struct device *dev = bus_find_device(&spi_bus_type, NULL, node, 3382ce79d54aSPantelis Antoniou __spi_of_device_match); 3383ce79d54aSPantelis Antoniou return dev ? to_spi_device(dev) : NULL; 3384ce79d54aSPantelis Antoniou } 3385ce79d54aSPantelis Antoniou 33868caab75fSGeert Uytterhoeven static int __spi_of_controller_match(struct device *dev, const void *data) 3387ce79d54aSPantelis Antoniou { 3388ce79d54aSPantelis Antoniou return dev->of_node == data; 3389ce79d54aSPantelis Antoniou } 3390ce79d54aSPantelis Antoniou 33918caab75fSGeert Uytterhoeven /* the spi controllers are not using spi_bus, so we find it with another way */ 33928caab75fSGeert Uytterhoeven static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node) 3393ce79d54aSPantelis Antoniou { 3394ce79d54aSPantelis Antoniou struct device *dev; 3395ce79d54aSPantelis Antoniou 3396ce79d54aSPantelis Antoniou dev = class_find_device(&spi_master_class, NULL, node, 33978caab75fSGeert Uytterhoeven __spi_of_controller_match); 33986c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 33996c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, node, 34008caab75fSGeert Uytterhoeven __spi_of_controller_match); 3401ce79d54aSPantelis Antoniou if (!dev) 3402ce79d54aSPantelis Antoniou return NULL; 3403ce79d54aSPantelis Antoniou 3404ce79d54aSPantelis Antoniou /* reference got in class_find_device */ 34058caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 3406ce79d54aSPantelis Antoniou } 3407ce79d54aSPantelis Antoniou 3408ce79d54aSPantelis Antoniou static int of_spi_notify(struct notifier_block *nb, unsigned long action, 3409ce79d54aSPantelis Antoniou void *arg) 3410ce79d54aSPantelis Antoniou { 3411ce79d54aSPantelis Antoniou struct of_reconfig_data *rd = arg; 34128caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 3413ce79d54aSPantelis Antoniou struct spi_device *spi; 3414ce79d54aSPantelis Antoniou 3415ce79d54aSPantelis Antoniou switch (of_reconfig_get_state_change(action, arg)) { 3416ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 34178caab75fSGeert Uytterhoeven ctlr = of_find_spi_controller_by_node(rd->dn->parent); 34188caab75fSGeert Uytterhoeven if (ctlr == NULL) 3419ce79d54aSPantelis Antoniou return NOTIFY_OK; /* not for us */ 3420ce79d54aSPantelis Antoniou 3421bd6c1644SGeert Uytterhoeven if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) { 34228caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 3423bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 3424bd6c1644SGeert Uytterhoeven } 3425bd6c1644SGeert Uytterhoeven 34268caab75fSGeert Uytterhoeven spi = of_register_spi_device(ctlr, rd->dn); 34278caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 3428ce79d54aSPantelis Antoniou 3429ce79d54aSPantelis Antoniou if (IS_ERR(spi)) { 343025c56c88SRob Herring pr_err("%s: failed to create for '%pOF'\n", 343125c56c88SRob Herring __func__, rd->dn); 3432e0af98a7SRalf Ramsauer of_node_clear_flag(rd->dn, OF_POPULATED); 3433ce79d54aSPantelis Antoniou return notifier_from_errno(PTR_ERR(spi)); 3434ce79d54aSPantelis Antoniou } 3435ce79d54aSPantelis Antoniou break; 3436ce79d54aSPantelis Antoniou 3437ce79d54aSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 3438bd6c1644SGeert Uytterhoeven /* already depopulated? */ 3439bd6c1644SGeert Uytterhoeven if (!of_node_check_flag(rd->dn, OF_POPULATED)) 3440bd6c1644SGeert Uytterhoeven return NOTIFY_OK; 3441bd6c1644SGeert Uytterhoeven 3442ce79d54aSPantelis Antoniou /* find our device by node */ 3443ce79d54aSPantelis Antoniou spi = of_find_spi_device_by_node(rd->dn); 3444ce79d54aSPantelis Antoniou if (spi == NULL) 3445ce79d54aSPantelis Antoniou return NOTIFY_OK; /* no? not meant for us */ 3446ce79d54aSPantelis Antoniou 3447ce79d54aSPantelis Antoniou /* unregister takes one ref away */ 3448ce79d54aSPantelis Antoniou spi_unregister_device(spi); 3449ce79d54aSPantelis Antoniou 3450ce79d54aSPantelis Antoniou /* and put the reference of the find */ 3451ce79d54aSPantelis Antoniou put_device(&spi->dev); 3452ce79d54aSPantelis Antoniou break; 3453ce79d54aSPantelis Antoniou } 3454ce79d54aSPantelis Antoniou 3455ce79d54aSPantelis Antoniou return NOTIFY_OK; 3456ce79d54aSPantelis Antoniou } 3457ce79d54aSPantelis Antoniou 3458ce79d54aSPantelis Antoniou static struct notifier_block spi_of_notifier = { 3459ce79d54aSPantelis Antoniou .notifier_call = of_spi_notify, 3460ce79d54aSPantelis Antoniou }; 3461ce79d54aSPantelis Antoniou #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 3462ce79d54aSPantelis Antoniou extern struct notifier_block spi_of_notifier; 3463ce79d54aSPantelis Antoniou #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */ 3464ce79d54aSPantelis Antoniou 34657f24467fSOctavian Purdila #if IS_ENABLED(CONFIG_ACPI) 34668caab75fSGeert Uytterhoeven static int spi_acpi_controller_match(struct device *dev, const void *data) 34677f24467fSOctavian Purdila { 34687f24467fSOctavian Purdila return ACPI_COMPANION(dev->parent) == data; 34697f24467fSOctavian Purdila } 34707f24467fSOctavian Purdila 34717f24467fSOctavian Purdila static int spi_acpi_device_match(struct device *dev, void *data) 34727f24467fSOctavian Purdila { 34737f24467fSOctavian Purdila return ACPI_COMPANION(dev) == data; 34747f24467fSOctavian Purdila } 34757f24467fSOctavian Purdila 34768caab75fSGeert Uytterhoeven static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev) 34777f24467fSOctavian Purdila { 34787f24467fSOctavian Purdila struct device *dev; 34797f24467fSOctavian Purdila 34807f24467fSOctavian Purdila dev = class_find_device(&spi_master_class, NULL, adev, 34818caab75fSGeert Uytterhoeven spi_acpi_controller_match); 34826c364062SGeert Uytterhoeven if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE)) 34836c364062SGeert Uytterhoeven dev = class_find_device(&spi_slave_class, NULL, adev, 34848caab75fSGeert Uytterhoeven spi_acpi_controller_match); 34857f24467fSOctavian Purdila if (!dev) 34867f24467fSOctavian Purdila return NULL; 34877f24467fSOctavian Purdila 34888caab75fSGeert Uytterhoeven return container_of(dev, struct spi_controller, dev); 34897f24467fSOctavian Purdila } 34907f24467fSOctavian Purdila 34917f24467fSOctavian Purdila static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev) 34927f24467fSOctavian Purdila { 34937f24467fSOctavian Purdila struct device *dev; 34947f24467fSOctavian Purdila 34957f24467fSOctavian Purdila dev = bus_find_device(&spi_bus_type, NULL, adev, spi_acpi_device_match); 34967f24467fSOctavian Purdila 34977f24467fSOctavian Purdila return dev ? to_spi_device(dev) : NULL; 34987f24467fSOctavian Purdila } 34997f24467fSOctavian Purdila 35007f24467fSOctavian Purdila static int acpi_spi_notify(struct notifier_block *nb, unsigned long value, 35017f24467fSOctavian Purdila void *arg) 35027f24467fSOctavian Purdila { 35037f24467fSOctavian Purdila struct acpi_device *adev = arg; 35048caab75fSGeert Uytterhoeven struct spi_controller *ctlr; 35057f24467fSOctavian Purdila struct spi_device *spi; 35067f24467fSOctavian Purdila 35077f24467fSOctavian Purdila switch (value) { 35087f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_ADD: 35098caab75fSGeert Uytterhoeven ctlr = acpi_spi_find_controller_by_adev(adev->parent); 35108caab75fSGeert Uytterhoeven if (!ctlr) 35117f24467fSOctavian Purdila break; 35127f24467fSOctavian Purdila 35138caab75fSGeert Uytterhoeven acpi_register_spi_device(ctlr, adev); 35148caab75fSGeert Uytterhoeven put_device(&ctlr->dev); 35157f24467fSOctavian Purdila break; 35167f24467fSOctavian Purdila case ACPI_RECONFIG_DEVICE_REMOVE: 35177f24467fSOctavian Purdila if (!acpi_device_enumerated(adev)) 35187f24467fSOctavian Purdila break; 35197f24467fSOctavian Purdila 35207f24467fSOctavian Purdila spi = acpi_spi_find_device_by_adev(adev); 35217f24467fSOctavian Purdila if (!spi) 35227f24467fSOctavian Purdila break; 35237f24467fSOctavian Purdila 35247f24467fSOctavian Purdila spi_unregister_device(spi); 35257f24467fSOctavian Purdila put_device(&spi->dev); 35267f24467fSOctavian Purdila break; 35277f24467fSOctavian Purdila } 35287f24467fSOctavian Purdila 35297f24467fSOctavian Purdila return NOTIFY_OK; 35307f24467fSOctavian Purdila } 35317f24467fSOctavian Purdila 35327f24467fSOctavian Purdila static struct notifier_block spi_acpi_notifier = { 35337f24467fSOctavian Purdila .notifier_call = acpi_spi_notify, 35347f24467fSOctavian Purdila }; 35357f24467fSOctavian Purdila #else 35367f24467fSOctavian Purdila extern struct notifier_block spi_acpi_notifier; 35377f24467fSOctavian Purdila #endif 35387f24467fSOctavian Purdila 35398ae12a0dSDavid Brownell static int __init spi_init(void) 35408ae12a0dSDavid Brownell { 3541b885244eSDavid Brownell int status; 35428ae12a0dSDavid Brownell 3543e94b1766SChristoph Lameter buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 3544b885244eSDavid Brownell if (!buf) { 3545b885244eSDavid Brownell status = -ENOMEM; 3546b885244eSDavid Brownell goto err0; 35478ae12a0dSDavid Brownell } 3548b885244eSDavid Brownell 3549b885244eSDavid Brownell status = bus_register(&spi_bus_type); 3550b885244eSDavid Brownell if (status < 0) 3551b885244eSDavid Brownell goto err1; 3552b885244eSDavid Brownell 3553b885244eSDavid Brownell status = class_register(&spi_master_class); 3554b885244eSDavid Brownell if (status < 0) 3555b885244eSDavid Brownell goto err2; 3556ce79d54aSPantelis Antoniou 35576c364062SGeert Uytterhoeven if (IS_ENABLED(CONFIG_SPI_SLAVE)) { 35586c364062SGeert Uytterhoeven status = class_register(&spi_slave_class); 35596c364062SGeert Uytterhoeven if (status < 0) 35606c364062SGeert Uytterhoeven goto err3; 35616c364062SGeert Uytterhoeven } 35626c364062SGeert Uytterhoeven 35635267720eSFabio Estevam if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 3564ce79d54aSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&spi_of_notifier)); 35657f24467fSOctavian Purdila if (IS_ENABLED(CONFIG_ACPI)) 35667f24467fSOctavian Purdila WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier)); 3567ce79d54aSPantelis Antoniou 3568b885244eSDavid Brownell return 0; 3569b885244eSDavid Brownell 35706c364062SGeert Uytterhoeven err3: 35716c364062SGeert Uytterhoeven class_unregister(&spi_master_class); 3572b885244eSDavid Brownell err2: 3573b885244eSDavid Brownell bus_unregister(&spi_bus_type); 3574b885244eSDavid Brownell err1: 3575b885244eSDavid Brownell kfree(buf); 3576b885244eSDavid Brownell buf = NULL; 3577b885244eSDavid Brownell err0: 3578b885244eSDavid Brownell return status; 3579b885244eSDavid Brownell } 3580b885244eSDavid Brownell 35818ae12a0dSDavid Brownell /* board_info is normally registered in arch_initcall(), 35828ae12a0dSDavid Brownell * but even essential drivers wait till later 3583b885244eSDavid Brownell * 3584b885244eSDavid Brownell * REVISIT only boardinfo really needs static linking. the rest (device and 3585b885244eSDavid Brownell * driver registration) _could_ be dynamically linked (modular) ... costs 3586b885244eSDavid Brownell * include needing to have boardinfo data structures be much more public. 35878ae12a0dSDavid Brownell */ 3588673c0c00SDavid Brownell postcore_initcall(spi_init); 35898ae12a0dSDavid Brownell 3590