LCOV - code coverage report
Current view: top level - drivers/spi - spi.c (source / functions) Hit Total Coverage
Test: Real Lines: 19 1153 1.6 %
Date: 2020-10-17 15:46:16 Functions: 0 175 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-or-later
       2                 :            : // SPI init/core code
       3                 :            : //
       4                 :            : // Copyright (C) 2005 David Brownell
       5                 :            : // Copyright (C) 2008 Secret Lab Technologies Ltd.
       6                 :            : 
       7                 :            : #include <linux/kernel.h>
       8                 :            : #include <linux/device.h>
       9                 :            : #include <linux/init.h>
      10                 :            : #include <linux/cache.h>
      11                 :            : #include <linux/dma-mapping.h>
      12                 :            : #include <linux/dmaengine.h>
      13                 :            : #include <linux/mutex.h>
      14                 :            : #include <linux/of_device.h>
      15                 :            : #include <linux/of_irq.h>
      16                 :            : #include <linux/clk/clk-conf.h>
      17                 :            : #include <linux/slab.h>
      18                 :            : #include <linux/mod_devicetable.h>
      19                 :            : #include <linux/spi/spi.h>
      20                 :            : #include <linux/spi/spi-mem.h>
      21                 :            : #include <linux/of_gpio.h>
      22                 :            : #include <linux/gpio/consumer.h>
      23                 :            : #include <linux/pm_runtime.h>
      24                 :            : #include <linux/pm_domain.h>
      25                 :            : #include <linux/property.h>
      26                 :            : #include <linux/export.h>
      27                 :            : #include <linux/sched/rt.h>
      28                 :            : #include <uapi/linux/sched/types.h>
      29                 :            : #include <linux/delay.h>
      30                 :            : #include <linux/kthread.h>
      31                 :            : #include <linux/ioport.h>
      32                 :            : #include <linux/acpi.h>
      33                 :            : #include <linux/highmem.h>
      34                 :            : #include <linux/idr.h>
      35                 :            : #include <linux/platform_data/x86/apple.h>
      36                 :            : 
      37                 :            : #define CREATE_TRACE_POINTS
      38                 :            : #include <trace/events/spi.h>
      39                 :            : EXPORT_TRACEPOINT_SYMBOL(spi_transfer_start);
      40                 :            : EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
      41                 :            : 
      42                 :            : #include "internals.h"
      43                 :            : 
      44                 :            : static DEFINE_IDR(spi_master_idr);
      45                 :            : 
      46                 :          0 : static void spidev_release(struct device *dev)
      47                 :            : {
      48                 :            :         struct spi_device       *spi = to_spi_device(dev);
      49                 :            : 
      50                 :            :         /* spi controllers may cleanup for released devices */
      51                 :          0 :         if (spi->controller->cleanup)
      52                 :          0 :                 spi->controller->cleanup(spi);
      53                 :            : 
      54                 :          0 :         spi_controller_put(spi->controller);
      55                 :          0 :         kfree(spi->driver_override);
      56                 :          0 :         kfree(spi);
      57                 :          0 : }
      58                 :            : 
      59                 :            : static ssize_t
      60                 :          0 : modalias_show(struct device *dev, struct device_attribute *a, char *buf)
      61                 :            : {
      62                 :            :         const struct spi_device *spi = to_spi_device(dev);
      63                 :            :         int len;
      64                 :            : 
      65                 :            :         len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
      66                 :            :         if (len != -ENODEV)
      67                 :            :                 return len;
      68                 :            : 
      69                 :          0 :         return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias);
      70                 :            : }
      71                 :            : static DEVICE_ATTR_RO(modalias);
      72                 :            : 
      73                 :          0 : static ssize_t driver_override_store(struct device *dev,
      74                 :            :                                      struct device_attribute *a,
      75                 :            :                                      const char *buf, size_t count)
      76                 :            : {
      77                 :            :         struct spi_device *spi = to_spi_device(dev);
      78                 :          0 :         const char *end = memchr(buf, '\n', count);
      79                 :          0 :         const size_t len = end ? end - buf : count;
      80                 :            :         const char *driver_override, *old;
      81                 :            : 
      82                 :            :         /* We need to keep extra room for a newline when displaying value */
      83                 :          0 :         if (len >= (PAGE_SIZE - 1))
      84                 :            :                 return -EINVAL;
      85                 :            : 
      86                 :          0 :         driver_override = kstrndup(buf, len, GFP_KERNEL);
      87                 :          0 :         if (!driver_override)
      88                 :            :                 return -ENOMEM;
      89                 :            : 
      90                 :            :         device_lock(dev);
      91                 :          0 :         old = spi->driver_override;
      92                 :          0 :         if (len) {
      93                 :          0 :                 spi->driver_override = driver_override;
      94                 :            :         } else {
      95                 :            :                 /* Emptry string, disable driver override */
      96                 :          0 :                 spi->driver_override = NULL;
      97                 :          0 :                 kfree(driver_override);
      98                 :            :         }
      99                 :            :         device_unlock(dev);
     100                 :          0 :         kfree(old);
     101                 :            : 
     102                 :          0 :         return count;
     103                 :            : }
     104                 :            : 
     105                 :          0 : static ssize_t driver_override_show(struct device *dev,
     106                 :            :                                     struct device_attribute *a, char *buf)
     107                 :            : {
     108                 :            :         const struct spi_device *spi = to_spi_device(dev);
     109                 :            :         ssize_t len;
     110                 :            : 
     111                 :            :         device_lock(dev);
     112                 :          0 :         len = snprintf(buf, PAGE_SIZE, "%s\n", spi->driver_override ? : "");
     113                 :            :         device_unlock(dev);
     114                 :          0 :         return len;
     115                 :            : }
     116                 :            : static DEVICE_ATTR_RW(driver_override);
     117                 :            : 
     118                 :            : #define SPI_STATISTICS_ATTRS(field, file)                               \
     119                 :            : static ssize_t spi_controller_##field##_show(struct device *dev,        \
     120                 :            :                                              struct device_attribute *attr, \
     121                 :            :                                              char *buf)                 \
     122                 :            : {                                                                       \
     123                 :            :         struct spi_controller *ctlr = container_of(dev,                 \
     124                 :            :                                          struct spi_controller, dev);   \
     125                 :            :         return spi_statistics_##field##_show(&ctlr->statistics, buf);    \
     126                 :            : }                                                                       \
     127                 :            : static struct device_attribute dev_attr_spi_controller_##field = {      \
     128                 :            :         .attr = { .name = file, .mode = 0444 },                         \
     129                 :            :         .show = spi_controller_##field##_show,                          \
     130                 :            : };                                                                      \
     131                 :            : static ssize_t spi_device_##field##_show(struct device *dev,            \
     132                 :            :                                          struct device_attribute *attr, \
     133                 :            :                                         char *buf)                      \
     134                 :            : {                                                                       \
     135                 :            :         struct spi_device *spi = to_spi_device(dev);                    \
     136                 :            :         return spi_statistics_##field##_show(&spi->statistics, buf);     \
     137                 :            : }                                                                       \
     138                 :            : static struct device_attribute dev_attr_spi_device_##field = {          \
     139                 :            :         .attr = { .name = file, .mode = 0444 },                         \
     140                 :            :         .show = spi_device_##field##_show,                              \
     141                 :            : }
     142                 :            : 
     143                 :            : #define SPI_STATISTICS_SHOW_NAME(name, file, field, format_string)      \
     144                 :            : static ssize_t spi_statistics_##name##_show(struct spi_statistics *stat, \
     145                 :            :                                             char *buf)                  \
     146                 :            : {                                                                       \
     147                 :            :         unsigned long flags;                                            \
     148                 :            :         ssize_t len;                                                    \
     149                 :            :         spin_lock_irqsave(&stat->lock, flags);                           \
     150                 :            :         len = sprintf(buf, format_string, stat->field);                      \
     151                 :            :         spin_unlock_irqrestore(&stat->lock, flags);                      \
     152                 :            :         return len;                                                     \
     153                 :            : }                                                                       \
     154                 :            : SPI_STATISTICS_ATTRS(name, file)
     155                 :            : 
     156                 :            : #define SPI_STATISTICS_SHOW(field, format_string)                       \
     157                 :            :         SPI_STATISTICS_SHOW_NAME(field, __stringify(field),             \
     158                 :            :                                  field, format_string)
     159                 :            : 
     160                 :          0 : SPI_STATISTICS_SHOW(messages, "%lu");
     161                 :          0 : SPI_STATISTICS_SHOW(transfers, "%lu");
     162                 :          0 : SPI_STATISTICS_SHOW(errors, "%lu");
     163                 :          0 : SPI_STATISTICS_SHOW(timedout, "%lu");
     164                 :            : 
     165                 :          0 : SPI_STATISTICS_SHOW(spi_sync, "%lu");
     166                 :          0 : SPI_STATISTICS_SHOW(spi_sync_immediate, "%lu");
     167                 :          0 : SPI_STATISTICS_SHOW(spi_async, "%lu");
     168                 :            : 
     169                 :          0 : SPI_STATISTICS_SHOW(bytes, "%llu");
     170                 :          0 : SPI_STATISTICS_SHOW(bytes_rx, "%llu");
     171                 :          0 : SPI_STATISTICS_SHOW(bytes_tx, "%llu");
     172                 :            : 
     173                 :            : #define SPI_STATISTICS_TRANSFER_BYTES_HISTO(index, number)              \
     174                 :            :         SPI_STATISTICS_SHOW_NAME(transfer_bytes_histo##index,           \
     175                 :            :                                  "transfer_bytes_histo_" number,      \
     176                 :            :                                  transfer_bytes_histo[index],  "%lu")
     177                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(0,  "0-1");
     178                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(1,  "2-3");
     179                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(2,  "4-7");
     180                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(3,  "8-15");
     181                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(4,  "16-31");
     182                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(5,  "32-63");
     183                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(6,  "64-127");
     184                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(7,  "128-255");
     185                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(8,  "256-511");
     186                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(9,  "512-1023");
     187                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(10, "1024-2047");
     188                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(11, "2048-4095");
     189                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(12, "4096-8191");
     190                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(13, "8192-16383");
     191                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(14, "16384-32767");
     192                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(15, "32768-65535");
     193                 :          0 : SPI_STATISTICS_TRANSFER_BYTES_HISTO(16, "65536+");
     194                 :            : 
     195                 :          0 : SPI_STATISTICS_SHOW(transfers_split_maxsize, "%lu");
     196                 :            : 
     197                 :            : static struct attribute *spi_dev_attrs[] = {
     198                 :            :         &dev_attr_modalias.attr,
     199                 :            :         &dev_attr_driver_override.attr,
     200                 :            :         NULL,
     201                 :            : };
     202                 :            : 
     203                 :            : static const struct attribute_group spi_dev_group = {
     204                 :            :         .attrs  = spi_dev_attrs,
     205                 :            : };
     206                 :            : 
     207                 :            : static struct attribute *spi_device_statistics_attrs[] = {
     208                 :            :         &dev_attr_spi_device_messages.attr,
     209                 :            :         &dev_attr_spi_device_transfers.attr,
     210                 :            :         &dev_attr_spi_device_errors.attr,
     211                 :            :         &dev_attr_spi_device_timedout.attr,
     212                 :            :         &dev_attr_spi_device_spi_sync.attr,
     213                 :            :         &dev_attr_spi_device_spi_sync_immediate.attr,
     214                 :            :         &dev_attr_spi_device_spi_async.attr,
     215                 :            :         &dev_attr_spi_device_bytes.attr,
     216                 :            :         &dev_attr_spi_device_bytes_rx.attr,
     217                 :            :         &dev_attr_spi_device_bytes_tx.attr,
     218                 :            :         &dev_attr_spi_device_transfer_bytes_histo0.attr,
     219                 :            :         &dev_attr_spi_device_transfer_bytes_histo1.attr,
     220                 :            :         &dev_attr_spi_device_transfer_bytes_histo2.attr,
     221                 :            :         &dev_attr_spi_device_transfer_bytes_histo3.attr,
     222                 :            :         &dev_attr_spi_device_transfer_bytes_histo4.attr,
     223                 :            :         &dev_attr_spi_device_transfer_bytes_histo5.attr,
     224                 :            :         &dev_attr_spi_device_transfer_bytes_histo6.attr,
     225                 :            :         &dev_attr_spi_device_transfer_bytes_histo7.attr,
     226                 :            :         &dev_attr_spi_device_transfer_bytes_histo8.attr,
     227                 :            :         &dev_attr_spi_device_transfer_bytes_histo9.attr,
     228                 :            :         &dev_attr_spi_device_transfer_bytes_histo10.attr,
     229                 :            :         &dev_attr_spi_device_transfer_bytes_histo11.attr,
     230                 :            :         &dev_attr_spi_device_transfer_bytes_histo12.attr,
     231                 :            :         &dev_attr_spi_device_transfer_bytes_histo13.attr,
     232                 :            :         &dev_attr_spi_device_transfer_bytes_histo14.attr,
     233                 :            :         &dev_attr_spi_device_transfer_bytes_histo15.attr,
     234                 :            :         &dev_attr_spi_device_transfer_bytes_histo16.attr,
     235                 :            :         &dev_attr_spi_device_transfers_split_maxsize.attr,
     236                 :            :         NULL,
     237                 :            : };
     238                 :            : 
     239                 :            : static const struct attribute_group spi_device_statistics_group = {
     240                 :            :         .name  = "statistics",
     241                 :            :         .attrs  = spi_device_statistics_attrs,
     242                 :            : };
     243                 :            : 
     244                 :            : static const struct attribute_group *spi_dev_groups[] = {
     245                 :            :         &spi_dev_group,
     246                 :            :         &spi_device_statistics_group,
     247                 :            :         NULL,
     248                 :            : };
     249                 :            : 
     250                 :            : static struct attribute *spi_controller_statistics_attrs[] = {
     251                 :            :         &dev_attr_spi_controller_messages.attr,
     252                 :            :         &dev_attr_spi_controller_transfers.attr,
     253                 :            :         &dev_attr_spi_controller_errors.attr,
     254                 :            :         &dev_attr_spi_controller_timedout.attr,
     255                 :            :         &dev_attr_spi_controller_spi_sync.attr,
     256                 :            :         &dev_attr_spi_controller_spi_sync_immediate.attr,
     257                 :            :         &dev_attr_spi_controller_spi_async.attr,
     258                 :            :         &dev_attr_spi_controller_bytes.attr,
     259                 :            :         &dev_attr_spi_controller_bytes_rx.attr,
     260                 :            :         &dev_attr_spi_controller_bytes_tx.attr,
     261                 :            :         &dev_attr_spi_controller_transfer_bytes_histo0.attr,
     262                 :            :         &dev_attr_spi_controller_transfer_bytes_histo1.attr,
     263                 :            :         &dev_attr_spi_controller_transfer_bytes_histo2.attr,
     264                 :            :         &dev_attr_spi_controller_transfer_bytes_histo3.attr,
     265                 :            :         &dev_attr_spi_controller_transfer_bytes_histo4.attr,
     266                 :            :         &dev_attr_spi_controller_transfer_bytes_histo5.attr,
     267                 :            :         &dev_attr_spi_controller_transfer_bytes_histo6.attr,
     268                 :            :         &dev_attr_spi_controller_transfer_bytes_histo7.attr,
     269                 :            :         &dev_attr_spi_controller_transfer_bytes_histo8.attr,
     270                 :            :         &dev_attr_spi_controller_transfer_bytes_histo9.attr,
     271                 :            :         &dev_attr_spi_controller_transfer_bytes_histo10.attr,
     272                 :            :         &dev_attr_spi_controller_transfer_bytes_histo11.attr,
     273                 :            :         &dev_attr_spi_controller_transfer_bytes_histo12.attr,
     274                 :            :         &dev_attr_spi_controller_transfer_bytes_histo13.attr,
     275                 :            :         &dev_attr_spi_controller_transfer_bytes_histo14.attr,
     276                 :            :         &dev_attr_spi_controller_transfer_bytes_histo15.attr,
     277                 :            :         &dev_attr_spi_controller_transfer_bytes_histo16.attr,
     278                 :            :         &dev_attr_spi_controller_transfers_split_maxsize.attr,
     279                 :            :         NULL,
     280                 :            : };
     281                 :            : 
     282                 :            : static const struct attribute_group spi_controller_statistics_group = {
     283                 :            :         .name  = "statistics",
     284                 :            :         .attrs  = spi_controller_statistics_attrs,
     285                 :            : };
     286                 :            : 
     287                 :            : static const struct attribute_group *spi_master_groups[] = {
     288                 :            :         &spi_controller_statistics_group,
     289                 :            :         NULL,
     290                 :            : };
     291                 :            : 
     292                 :          0 : void spi_statistics_add_transfer_stats(struct spi_statistics *stats,
     293                 :            :                                        struct spi_transfer *xfer,
     294                 :            :                                        struct spi_controller *ctlr)
     295                 :            : {
     296                 :            :         unsigned long flags;
     297                 :          0 :         int l2len = min(fls(xfer->len), SPI_STATISTICS_HISTO_SIZE) - 1;
     298                 :            : 
     299                 :          0 :         if (l2len < 0)
     300                 :            :                 l2len = 0;
     301                 :            : 
     302                 :          0 :         spin_lock_irqsave(&stats->lock, flags);
     303                 :            : 
     304                 :          0 :         stats->transfers++;
     305                 :          0 :         stats->transfer_bytes_histo[l2len]++;
     306                 :            : 
     307                 :          0 :         stats->bytes += xfer->len;
     308                 :          0 :         if ((xfer->tx_buf) &&
     309                 :          0 :             (xfer->tx_buf != ctlr->dummy_tx))
     310                 :          0 :                 stats->bytes_tx += xfer->len;
     311                 :          0 :         if ((xfer->rx_buf) &&
     312                 :          0 :             (xfer->rx_buf != ctlr->dummy_rx))
     313                 :          0 :                 stats->bytes_rx += xfer->len;
     314                 :            : 
     315                 :            :         spin_unlock_irqrestore(&stats->lock, flags);
     316                 :          0 : }
     317                 :            : EXPORT_SYMBOL_GPL(spi_statistics_add_transfer_stats);
     318                 :            : 
     319                 :            : /* modalias support makes "modprobe $MODALIAS" new-style hotplug work,
     320                 :            :  * and the sysfs version makes coldplug work too.
     321                 :            :  */
     322                 :            : 
     323                 :          0 : static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
     324                 :            :                                                 const struct spi_device *sdev)
     325                 :            : {
     326                 :          0 :         while (id->name[0]) {
     327                 :          0 :                 if (!strcmp(sdev->modalias, id->name))
     328                 :          0 :                         return id;
     329                 :          0 :                 id++;
     330                 :            :         }
     331                 :            :         return NULL;
     332                 :            : }
     333                 :            : 
     334                 :          0 : const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
     335                 :            : {
     336                 :          0 :         const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
     337                 :            : 
     338                 :          0 :         return spi_match_id(sdrv->id_table, sdev);
     339                 :            : }
     340                 :            : EXPORT_SYMBOL_GPL(spi_get_device_id);
     341                 :            : 
     342                 :          0 : static int spi_match_device(struct device *dev, struct device_driver *drv)
     343                 :            : {
     344                 :            :         const struct spi_device *spi = to_spi_device(dev);
     345                 :            :         const struct spi_driver *sdrv = to_spi_driver(drv);
     346                 :            : 
     347                 :            :         /* Check override first, and if set, only use the named driver */
     348                 :          0 :         if (spi->driver_override)
     349                 :          0 :                 return strcmp(spi->driver_override, drv->name) == 0;
     350                 :            : 
     351                 :            :         /* Attempt an OF style match */
     352                 :          0 :         if (of_driver_match_device(dev, drv))
     353                 :            :                 return 1;
     354                 :            : 
     355                 :            :         /* Then try ACPI */
     356                 :            :         if (acpi_driver_match_device(dev, drv))
     357                 :            :                 return 1;
     358                 :            : 
     359                 :          0 :         if (sdrv->id_table)
     360                 :          0 :                 return !!spi_match_id(sdrv->id_table, spi);
     361                 :            : 
     362                 :          0 :         return strcmp(spi->modalias, drv->name) == 0;
     363                 :            : }
     364                 :            : 
     365                 :          0 : static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
     366                 :            : {
     367                 :            :         const struct spi_device         *spi = to_spi_device(dev);
     368                 :            :         int rc;
     369                 :            : 
     370                 :            :         rc = acpi_device_uevent_modalias(dev, env);
     371                 :            :         if (rc != -ENODEV)
     372                 :            :                 return rc;
     373                 :            : 
     374                 :          0 :         return add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
     375                 :            : }
     376                 :            : 
     377                 :            : struct bus_type spi_bus_type = {
     378                 :            :         .name           = "spi",
     379                 :            :         .dev_groups     = spi_dev_groups,
     380                 :            :         .match          = spi_match_device,
     381                 :            :         .uevent         = spi_uevent,
     382                 :            : };
     383                 :            : EXPORT_SYMBOL_GPL(spi_bus_type);
     384                 :            : 
     385                 :            : 
     386                 :          0 : static int spi_drv_probe(struct device *dev)
     387                 :            : {
     388                 :          0 :         const struct spi_driver         *sdrv = to_spi_driver(dev->driver);
     389                 :            :         struct spi_device               *spi = to_spi_device(dev);
     390                 :            :         int ret;
     391                 :            : 
     392                 :          0 :         ret = of_clk_set_defaults(dev->of_node, false);
     393                 :          0 :         if (ret)
     394                 :            :                 return ret;
     395                 :            : 
     396                 :          0 :         if (dev->of_node) {
     397                 :          0 :                 spi->irq = of_irq_get(dev->of_node, 0);
     398                 :          0 :                 if (spi->irq == -EPROBE_DEFER)
     399                 :            :                         return -EPROBE_DEFER;
     400                 :          0 :                 if (spi->irq < 0)
     401                 :          0 :                         spi->irq = 0;
     402                 :            :         }
     403                 :            : 
     404                 :          0 :         ret = dev_pm_domain_attach(dev, true);
     405                 :          0 :         if (ret)
     406                 :            :                 return ret;
     407                 :            : 
     408                 :          0 :         ret = sdrv->probe(spi);
     409                 :          0 :         if (ret)
     410                 :          0 :                 dev_pm_domain_detach(dev, true);
     411                 :            : 
     412                 :          0 :         return ret;
     413                 :            : }
     414                 :            : 
     415                 :          0 : static int spi_drv_remove(struct device *dev)
     416                 :            : {
     417                 :          0 :         const struct spi_driver         *sdrv = to_spi_driver(dev->driver);
     418                 :            :         int ret;
     419                 :            : 
     420                 :          0 :         ret = sdrv->remove(to_spi_device(dev));
     421                 :          0 :         dev_pm_domain_detach(dev, true);
     422                 :            : 
     423                 :          0 :         return ret;
     424                 :            : }
     425                 :            : 
     426                 :          0 : static void spi_drv_shutdown(struct device *dev)
     427                 :            : {
     428                 :          0 :         const struct spi_driver         *sdrv = to_spi_driver(dev->driver);
     429                 :            : 
     430                 :          0 :         sdrv->shutdown(to_spi_device(dev));
     431                 :          0 : }
     432                 :            : 
     433                 :            : /**
     434                 :            :  * __spi_register_driver - register a SPI driver
     435                 :            :  * @owner: owner module of the driver to register
     436                 :            :  * @sdrv: the driver to register
     437                 :            :  * Context: can sleep
     438                 :            :  *
     439                 :            :  * Return: zero on success, else a negative error code.
     440                 :            :  */
     441                 :          3 : int __spi_register_driver(struct module *owner, struct spi_driver *sdrv)
     442                 :            : {
     443                 :          3 :         sdrv->driver.owner = owner;
     444                 :          3 :         sdrv->driver.bus = &spi_bus_type;
     445                 :          3 :         if (sdrv->probe)
     446                 :          3 :                 sdrv->driver.probe = spi_drv_probe;
     447                 :          3 :         if (sdrv->remove)
     448                 :          3 :                 sdrv->driver.remove = spi_drv_remove;
     449                 :          3 :         if (sdrv->shutdown)
     450                 :          0 :                 sdrv->driver.shutdown = spi_drv_shutdown;
     451                 :          3 :         return driver_register(&sdrv->driver);
     452                 :            : }
     453                 :            : EXPORT_SYMBOL_GPL(__spi_register_driver);
     454                 :            : 
     455                 :            : /*-------------------------------------------------------------------------*/
     456                 :            : 
     457                 :            : /* SPI devices should normally not be created by SPI device drivers; that
     458                 :            :  * would make them board-specific.  Similarly with SPI controller drivers.
     459                 :            :  * Device registration normally goes into like arch/.../mach.../board-YYY.c
     460                 :            :  * with other readonly (flashable) information about mainboard devices.
     461                 :            :  */
     462                 :            : 
     463                 :            : struct boardinfo {
     464                 :            :         struct list_head        list;
     465                 :            :         struct spi_board_info   board_info;
     466                 :            : };
     467                 :            : 
     468                 :            : static LIST_HEAD(board_list);
     469                 :            : static LIST_HEAD(spi_controller_list);
     470                 :            : 
     471                 :            : /*
     472                 :            :  * Used to protect add/del opertion for board_info list and
     473                 :            :  * spi_controller list, and their matching process
     474                 :            :  * also used to protect object of type struct idr
     475                 :            :  */
     476                 :            : static DEFINE_MUTEX(board_lock);
     477                 :            : 
     478                 :            : /*
     479                 :            :  * Prevents addition of devices with same chip select and
     480                 :            :  * addition of devices below an unregistering controller.
     481                 :            :  */
     482                 :            : static DEFINE_MUTEX(spi_add_lock);
     483                 :            : 
     484                 :            : /**
     485                 :            :  * spi_alloc_device - Allocate a new SPI device
     486                 :            :  * @ctlr: Controller to which device is connected
     487                 :            :  * Context: can sleep
     488                 :            :  *
     489                 :            :  * Allows a driver to allocate and initialize a spi_device without
     490                 :            :  * registering it immediately.  This allows a driver to directly
     491                 :            :  * fill the spi_device with device parameters before calling
     492                 :            :  * spi_add_device() on it.
     493                 :            :  *
     494                 :            :  * Caller is responsible to call spi_add_device() on the returned
     495                 :            :  * spi_device structure to add it to the SPI controller.  If the caller
     496                 :            :  * needs to discard the spi_device without adding it, then it should
     497                 :            :  * call spi_dev_put() on it.
     498                 :            :  *
     499                 :            :  * Return: a pointer to the new device, or NULL.
     500                 :            :  */
     501                 :          0 : struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
     502                 :            : {
     503                 :            :         struct spi_device       *spi;
     504                 :            : 
     505                 :          0 :         if (!spi_controller_get(ctlr))
     506                 :            :                 return NULL;
     507                 :            : 
     508                 :          0 :         spi = kzalloc(sizeof(*spi), GFP_KERNEL);
     509                 :          0 :         if (!spi) {
     510                 :            :                 spi_controller_put(ctlr);
     511                 :            :                 return NULL;
     512                 :            :         }
     513                 :            : 
     514                 :          0 :         spi->master = spi->controller = ctlr;
     515                 :          0 :         spi->dev.parent = &ctlr->dev;
     516                 :          0 :         spi->dev.bus = &spi_bus_type;
     517                 :          0 :         spi->dev.release = spidev_release;
     518                 :          0 :         spi->cs_gpio = -ENOENT;
     519                 :            : 
     520                 :          0 :         spin_lock_init(&spi->statistics.lock);
     521                 :            : 
     522                 :          0 :         device_initialize(&spi->dev);
     523                 :          0 :         return spi;
     524                 :            : }
     525                 :            : EXPORT_SYMBOL_GPL(spi_alloc_device);
     526                 :            : 
     527                 :          0 : static void spi_dev_set_name(struct spi_device *spi)
     528                 :            : {
     529                 :            :         struct acpi_device *adev = ACPI_COMPANION(&spi->dev);
     530                 :            : 
     531                 :            :         if (adev) {
     532                 :            :                 dev_set_name(&spi->dev, "spi-%s", acpi_dev_name(adev));
     533                 :          0 :                 return;
     534                 :            :         }
     535                 :            : 
     536                 :          0 :         dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->controller->dev),
     537                 :          0 :                      spi->chip_select);
     538                 :            : }
     539                 :            : 
     540                 :          0 : static int spi_dev_check(struct device *dev, void *data)
     541                 :            : {
     542                 :            :         struct spi_device *spi = to_spi_device(dev);
     543                 :            :         struct spi_device *new_spi = data;
     544                 :            : 
     545                 :          0 :         if (spi->controller == new_spi->controller &&
     546                 :          0 :             spi->chip_select == new_spi->chip_select)
     547                 :            :                 return -EBUSY;
     548                 :          0 :         return 0;
     549                 :            : }
     550                 :            : 
     551                 :            : /**
     552                 :            :  * spi_add_device - Add spi_device allocated with spi_alloc_device
     553                 :            :  * @spi: spi_device to register
     554                 :            :  *
     555                 :            :  * Companion function to spi_alloc_device.  Devices allocated with
     556                 :            :  * spi_alloc_device can be added onto the spi bus with this function.
     557                 :            :  *
     558                 :            :  * Return: 0 on success; negative errno on failure
     559                 :            :  */
     560                 :          0 : int spi_add_device(struct spi_device *spi)
     561                 :            : {
     562                 :          0 :         struct spi_controller *ctlr = spi->controller;
     563                 :          0 :         struct device *dev = ctlr->dev.parent;
     564                 :            :         int status;
     565                 :            : 
     566                 :            :         /* Chipselects are numbered 0..max; validate. */
     567                 :          0 :         if (spi->chip_select >= ctlr->num_chipselect) {
     568                 :          0 :                 dev_err(dev, "cs%d >= max %d\n", spi->chip_select,
     569                 :            :                         ctlr->num_chipselect);
     570                 :          0 :                 return -EINVAL;
     571                 :            :         }
     572                 :            : 
     573                 :            :         /* Set the bus ID string */
     574                 :          0 :         spi_dev_set_name(spi);
     575                 :            : 
     576                 :            :         /* We need to make sure there's no other device with this
     577                 :            :          * chipselect **BEFORE** we call setup(), else we'll trash
     578                 :            :          * its configuration.  Lock against concurrent add() calls.
     579                 :            :          */
     580                 :          0 :         mutex_lock(&spi_add_lock);
     581                 :            : 
     582                 :          0 :         status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
     583                 :          0 :         if (status) {
     584                 :          0 :                 dev_err(dev, "chipselect %d already in use\n",
     585                 :            :                                 spi->chip_select);
     586                 :          0 :                 goto done;
     587                 :            :         }
     588                 :            : 
     589                 :            :         /* Controller may unregister concurrently */
     590                 :          0 :         if (IS_ENABLED(CONFIG_SPI_DYNAMIC) &&
     591                 :            :             !device_is_registered(&ctlr->dev)) {
     592                 :            :                 status = -ENODEV;
     593                 :            :                 goto done;
     594                 :            :         }
     595                 :            : 
     596                 :            :         /* Descriptors take precedence */
     597                 :          0 :         if (ctlr->cs_gpiods)
     598                 :          0 :                 spi->cs_gpiod = ctlr->cs_gpiods[spi->chip_select];
     599                 :          0 :         else if (ctlr->cs_gpios)
     600                 :          0 :                 spi->cs_gpio = ctlr->cs_gpios[spi->chip_select];
     601                 :            : 
     602                 :            :         /* Drivers may modify this initial i/o setup, but will
     603                 :            :          * normally rely on the device being setup.  Devices
     604                 :            :          * using SPI_CS_HIGH can't coexist well otherwise...
     605                 :            :          */
     606                 :          0 :         status = spi_setup(spi);
     607                 :          0 :         if (status < 0) {
     608                 :          0 :                 dev_err(dev, "can't setup %s, status %d\n",
     609                 :            :                                 dev_name(&spi->dev), status);
     610                 :          0 :                 goto done;
     611                 :            :         }
     612                 :            : 
     613                 :            :         /* Device may be bound to an active driver when this returns */
     614                 :          0 :         status = device_add(&spi->dev);
     615                 :          0 :         if (status < 0)
     616                 :          0 :                 dev_err(dev, "can't add %s, status %d\n",
     617                 :            :                                 dev_name(&spi->dev), status);
     618                 :            :         else
     619                 :            :                 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
     620                 :            : 
     621                 :            : done:
     622                 :          0 :         mutex_unlock(&spi_add_lock);
     623                 :          0 :         return status;
     624                 :            : }
     625                 :            : EXPORT_SYMBOL_GPL(spi_add_device);
     626                 :            : 
     627                 :            : /**
     628                 :            :  * spi_new_device - instantiate one new SPI device
     629                 :            :  * @ctlr: Controller to which device is connected
     630                 :            :  * @chip: Describes the SPI device
     631                 :            :  * Context: can sleep
     632                 :            :  *
     633                 :            :  * On typical mainboards, this is purely internal; and it's not needed
     634                 :            :  * after board init creates the hard-wired devices.  Some development
     635                 :            :  * platforms may not be able to use spi_register_board_info though, and
     636                 :            :  * this is exported so that for example a USB or parport based adapter
     637                 :            :  * driver could add devices (which it would learn about out-of-band).
     638                 :            :  *
     639                 :            :  * Return: the new device, or NULL.
     640                 :            :  */
     641                 :          0 : struct spi_device *spi_new_device(struct spi_controller *ctlr,
     642                 :            :                                   struct spi_board_info *chip)
     643                 :            : {
     644                 :            :         struct spi_device       *proxy;
     645                 :            :         int                     status;
     646                 :            : 
     647                 :            :         /* NOTE:  caller did any chip->bus_num checks necessary.
     648                 :            :          *
     649                 :            :          * Also, unless we change the return value convention to use
     650                 :            :          * error-or-pointer (not NULL-or-pointer), troubleshootability
     651                 :            :          * suggests syslogged diagnostics are best here (ugh).
     652                 :            :          */
     653                 :            : 
     654                 :          0 :         proxy = spi_alloc_device(ctlr);
     655                 :          0 :         if (!proxy)
     656                 :            :                 return NULL;
     657                 :            : 
     658                 :          0 :         WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
     659                 :            : 
     660                 :          0 :         proxy->chip_select = chip->chip_select;
     661                 :          0 :         proxy->max_speed_hz = chip->max_speed_hz;
     662                 :          0 :         proxy->mode = chip->mode;
     663                 :          0 :         proxy->irq = chip->irq;
     664                 :          0 :         strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
     665                 :          0 :         proxy->dev.platform_data = (void *) chip->platform_data;
     666                 :          0 :         proxy->controller_data = chip->controller_data;
     667                 :          0 :         proxy->controller_state = NULL;
     668                 :            : 
     669                 :          0 :         if (chip->properties) {
     670                 :          0 :                 status = device_add_properties(&proxy->dev, chip->properties);
     671                 :          0 :                 if (status) {
     672                 :          0 :                         dev_err(&ctlr->dev,
     673                 :            :                                 "failed to add properties to '%s': %d\n",
     674                 :            :                                 chip->modalias, status);
     675                 :          0 :                         goto err_dev_put;
     676                 :            :                 }
     677                 :            :         }
     678                 :            : 
     679                 :          0 :         status = spi_add_device(proxy);
     680                 :          0 :         if (status < 0)
     681                 :            :                 goto err_remove_props;
     682                 :            : 
     683                 :            :         return proxy;
     684                 :            : 
     685                 :            : err_remove_props:
     686                 :          0 :         if (chip->properties)
     687                 :          0 :                 device_remove_properties(&proxy->dev);
     688                 :            : err_dev_put:
     689                 :            :         spi_dev_put(proxy);
     690                 :            :         return NULL;
     691                 :            : }
     692                 :            : EXPORT_SYMBOL_GPL(spi_new_device);
     693                 :            : 
     694                 :            : /**
     695                 :            :  * spi_unregister_device - unregister a single SPI device
     696                 :            :  * @spi: spi_device to unregister
     697                 :            :  *
     698                 :            :  * Start making the passed SPI device vanish. Normally this would be handled
     699                 :            :  * by spi_unregister_controller().
     700                 :            :  */
     701                 :          0 : void spi_unregister_device(struct spi_device *spi)
     702                 :            : {
     703                 :          0 :         if (!spi)
     704                 :          0 :                 return;
     705                 :            : 
     706                 :          0 :         if (spi->dev.of_node) {
     707                 :            :                 of_node_clear_flag(spi->dev.of_node, OF_POPULATED);
     708                 :          0 :                 of_node_put(spi->dev.of_node);
     709                 :            :         }
     710                 :            :         if (ACPI_COMPANION(&spi->dev))
     711                 :            :                 acpi_device_clear_enumerated(ACPI_COMPANION(&spi->dev));
     712                 :          0 :         device_unregister(&spi->dev);
     713                 :            : }
     714                 :            : EXPORT_SYMBOL_GPL(spi_unregister_device);
     715                 :            : 
     716                 :          0 : static void spi_match_controller_to_boardinfo(struct spi_controller *ctlr,
     717                 :            :                                               struct spi_board_info *bi)
     718                 :            : {
     719                 :            :         struct spi_device *dev;
     720                 :            : 
     721                 :          0 :         if (ctlr->bus_num != bi->bus_num)
     722                 :          0 :                 return;
     723                 :            : 
     724                 :          0 :         dev = spi_new_device(ctlr, bi);
     725                 :          0 :         if (!dev)
     726                 :          0 :                 dev_err(ctlr->dev.parent, "can't create new device for %s\n",
     727                 :            :                         bi->modalias);
     728                 :            : }
     729                 :            : 
     730                 :            : /**
     731                 :            :  * spi_register_board_info - register SPI devices for a given board
     732                 :            :  * @info: array of chip descriptors
     733                 :            :  * @n: how many descriptors are provided
     734                 :            :  * Context: can sleep
     735                 :            :  *
     736                 :            :  * Board-specific early init code calls this (probably during arch_initcall)
     737                 :            :  * with segments of the SPI device table.  Any device nodes are created later,
     738                 :            :  * after the relevant parent SPI controller (bus_num) is defined.  We keep
     739                 :            :  * this table of devices forever, so that reloading a controller driver will
     740                 :            :  * not make Linux forget about these hard-wired devices.
     741                 :            :  *
     742                 :            :  * Other code can also call this, e.g. a particular add-on board might provide
     743                 :            :  * SPI devices through its expansion connector, so code initializing that board
     744                 :            :  * would naturally declare its SPI devices.
     745                 :            :  *
     746                 :            :  * The board info passed can safely be __initdata ... but be careful of
     747                 :            :  * any embedded pointers (platform_data, etc), they're copied as-is.
     748                 :            :  * Device properties are deep-copied though.
     749                 :            :  *
     750                 :            :  * Return: zero on success, else a negative error code.
     751                 :            :  */
     752                 :          0 : int spi_register_board_info(struct spi_board_info const *info, unsigned n)
     753                 :            : {
     754                 :            :         struct boardinfo *bi;
     755                 :            :         int i;
     756                 :            : 
     757                 :          0 :         if (!n)
     758                 :            :                 return 0;
     759                 :            : 
     760                 :            :         bi = kcalloc(n, sizeof(*bi), GFP_KERNEL);
     761                 :          0 :         if (!bi)
     762                 :            :                 return -ENOMEM;
     763                 :            : 
     764                 :          0 :         for (i = 0; i < n; i++, bi++, info++) {
     765                 :            :                 struct spi_controller *ctlr;
     766                 :            : 
     767                 :          0 :                 memcpy(&bi->board_info, info, sizeof(*info));
     768                 :          0 :                 if (info->properties) {
     769                 :          0 :                         bi->board_info.properties =
     770                 :          0 :                                         property_entries_dup(info->properties);
     771                 :          0 :                         if (IS_ERR(bi->board_info.properties))
     772                 :          0 :                                 return PTR_ERR(bi->board_info.properties);
     773                 :            :                 }
     774                 :            : 
     775                 :          0 :                 mutex_lock(&board_lock);
     776                 :          0 :                 list_add_tail(&bi->list, &board_list);
     777                 :          0 :                 list_for_each_entry(ctlr, &spi_controller_list, list)
     778                 :          0 :                         spi_match_controller_to_boardinfo(ctlr,
     779                 :            :                                                           &bi->board_info);
     780                 :          0 :                 mutex_unlock(&board_lock);
     781                 :            :         }
     782                 :            : 
     783                 :            :         return 0;
     784                 :            : }
     785                 :            : 
     786                 :            : /*-------------------------------------------------------------------------*/
     787                 :            : 
     788                 :          0 : static void spi_set_cs(struct spi_device *spi, bool enable)
     789                 :            : {
     790                 :          0 :         if (spi->mode & SPI_CS_HIGH)
     791                 :          0 :                 enable = !enable;
     792                 :            : 
     793                 :          0 :         if (spi->cs_gpiod || gpio_is_valid(spi->cs_gpio)) {
     794                 :            :                 /*
     795                 :            :                  * Honour the SPI_NO_CS flag and invert the enable line, as
     796                 :            :                  * active low is default for SPI. Execution paths that handle
     797                 :            :                  * polarity inversion in gpiolib (such as device tree) will
     798                 :            :                  * enforce active high using the SPI_CS_HIGH resulting in a
     799                 :            :                  * double inversion through the code above.
     800                 :            :                  */
     801                 :          0 :                 if (!(spi->mode & SPI_NO_CS)) {
     802                 :          0 :                         if (spi->cs_gpiod)
     803                 :          0 :                                 gpiod_set_value_cansleep(spi->cs_gpiod,
     804                 :          0 :                                                          !enable);
     805                 :            :                         else
     806                 :          0 :                                 gpio_set_value_cansleep(spi->cs_gpio, !enable);
     807                 :            :                 }
     808                 :            :                 /* Some SPI masters need both GPIO CS & slave_select */
     809                 :          0 :                 if ((spi->controller->flags & SPI_MASTER_GPIO_SS) &&
     810                 :          0 :                     spi->controller->set_cs)
     811                 :          0 :                         spi->controller->set_cs(spi, !enable);
     812                 :          0 :         } else if (spi->controller->set_cs) {
     813                 :          0 :                 spi->controller->set_cs(spi, !enable);
     814                 :            :         }
     815                 :          0 : }
     816                 :            : 
     817                 :            : #ifdef CONFIG_HAS_DMA
     818                 :          0 : int spi_map_buf(struct spi_controller *ctlr, struct device *dev,
     819                 :            :                 struct sg_table *sgt, void *buf, size_t len,
     820                 :            :                 enum dma_data_direction dir)
     821                 :            : {
     822                 :            :         const bool vmalloced_buf = is_vmalloc_addr(buf);
     823                 :            :         unsigned int max_seg_size = dma_get_max_seg_size(dev);
     824                 :            : #ifdef CONFIG_HIGHMEM
     825                 :            :         const bool kmap_buf = ((unsigned long)buf >= PKMAP_BASE &&
     826                 :            :                                 (unsigned long)buf < (PKMAP_BASE +
     827                 :            :                                         (LAST_PKMAP * PAGE_SIZE)));
     828                 :            : #else
     829                 :            :         const bool kmap_buf = false;
     830                 :            : #endif
     831                 :            :         int desc_len;
     832                 :            :         int sgs;
     833                 :            :         struct page *vm_page;
     834                 :            :         struct scatterlist *sg;
     835                 :            :         void *sg_buf;
     836                 :            :         size_t min;
     837                 :            :         int i, ret;
     838                 :            : 
     839                 :          0 :         if (vmalloced_buf || kmap_buf) {
     840                 :          0 :                 desc_len = min_t(int, max_seg_size, PAGE_SIZE);
     841                 :          0 :                 sgs = DIV_ROUND_UP(len + offset_in_page(buf), desc_len);
     842                 :          0 :         } else if (virt_addr_valid(buf)) {
     843                 :          0 :                 desc_len = min_t(int, max_seg_size, ctlr->max_dma_len);
     844                 :          0 :                 sgs = DIV_ROUND_UP(len, desc_len);
     845                 :            :         } else {
     846                 :            :                 return -EINVAL;
     847                 :            :         }
     848                 :            : 
     849                 :          0 :         ret = sg_alloc_table(sgt, sgs, GFP_KERNEL);
     850                 :          0 :         if (ret != 0)
     851                 :            :                 return ret;
     852                 :            : 
     853                 :          0 :         sg = &sgt->sgl[0];
     854                 :          0 :         for (i = 0; i < sgs; i++) {
     855                 :            : 
     856                 :          0 :                 if (vmalloced_buf || kmap_buf) {
     857                 :            :                         /*
     858                 :            :                          * Next scatterlist entry size is the minimum between
     859                 :            :                          * the desc_len and the remaining buffer length that
     860                 :            :                          * fits in a page.
     861                 :            :                          */
     862                 :          0 :                         min = min_t(size_t, desc_len,
     863                 :            :                                     min_t(size_t, len,
     864                 :            :                                           PAGE_SIZE - offset_in_page(buf)));
     865                 :          0 :                         if (vmalloced_buf)
     866                 :          0 :                                 vm_page = vmalloc_to_page(buf);
     867                 :            :                         else
     868                 :            :                                 vm_page = kmap_to_page(buf);
     869                 :          0 :                         if (!vm_page) {
     870                 :          0 :                                 sg_free_table(sgt);
     871                 :          0 :                                 return -ENOMEM;
     872                 :            :                         }
     873                 :            :                         sg_set_page(sg, vm_page,
     874                 :            :                                     min, offset_in_page(buf));
     875                 :            :                 } else {
     876                 :          0 :                         min = min_t(size_t, len, desc_len);
     877                 :            :                         sg_buf = buf;
     878                 :          0 :                         sg_set_buf(sg, sg_buf, min);
     879                 :            :                 }
     880                 :            : 
     881                 :          0 :                 buf += min;
     882                 :          0 :                 len -= min;
     883                 :          0 :                 sg = sg_next(sg);
     884                 :            :         }
     885                 :            : 
     886                 :          0 :         ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir);
     887                 :          0 :         if (!ret)
     888                 :            :                 ret = -ENOMEM;
     889                 :          0 :         if (ret < 0) {
     890                 :          0 :                 sg_free_table(sgt);
     891                 :          0 :                 return ret;
     892                 :            :         }
     893                 :            : 
     894                 :          0 :         sgt->nents = ret;
     895                 :            : 
     896                 :          0 :         return 0;
     897                 :            : }
     898                 :            : 
     899                 :          0 : void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
     900                 :            :                    struct sg_table *sgt, enum dma_data_direction dir)
     901                 :            : {
     902                 :          0 :         if (sgt->orig_nents) {
     903                 :          0 :                 dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir);
     904                 :          0 :                 sg_free_table(sgt);
     905                 :            :         }
     906                 :          0 : }
     907                 :            : 
     908                 :          0 : static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
     909                 :            : {
     910                 :            :         struct device *tx_dev, *rx_dev;
     911                 :            :         struct spi_transfer *xfer;
     912                 :            :         int ret;
     913                 :            : 
     914                 :          0 :         if (!ctlr->can_dma)
     915                 :            :                 return 0;
     916                 :            : 
     917                 :          0 :         if (ctlr->dma_tx)
     918                 :          0 :                 tx_dev = ctlr->dma_tx->device->dev;
     919                 :            :         else
     920                 :          0 :                 tx_dev = ctlr->dev.parent;
     921                 :            : 
     922                 :          0 :         if (ctlr->dma_rx)
     923                 :          0 :                 rx_dev = ctlr->dma_rx->device->dev;
     924                 :            :         else
     925                 :          0 :                 rx_dev = ctlr->dev.parent;
     926                 :            : 
     927                 :          0 :         list_for_each_entry(xfer, &msg->transfers, transfer_list) {
     928                 :          0 :                 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
     929                 :          0 :                         continue;
     930                 :            : 
     931                 :          0 :                 if (xfer->tx_buf != NULL) {
     932                 :          0 :                         ret = spi_map_buf(ctlr, tx_dev, &xfer->tx_sg,
     933                 :            :                                           (void *)xfer->tx_buf, xfer->len,
     934                 :            :                                           DMA_TO_DEVICE);
     935                 :          0 :                         if (ret != 0)
     936                 :          0 :                                 return ret;
     937                 :            :                 }
     938                 :            : 
     939                 :          0 :                 if (xfer->rx_buf != NULL) {
     940                 :          0 :                         ret = spi_map_buf(ctlr, rx_dev, &xfer->rx_sg,
     941                 :            :                                           xfer->rx_buf, xfer->len,
     942                 :            :                                           DMA_FROM_DEVICE);
     943                 :          0 :                         if (ret != 0) {
     944                 :          0 :                                 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg,
     945                 :            :                                               DMA_TO_DEVICE);
     946                 :          0 :                                 return ret;
     947                 :            :                         }
     948                 :            :                 }
     949                 :            :         }
     950                 :            : 
     951                 :          0 :         ctlr->cur_msg_mapped = true;
     952                 :            : 
     953                 :          0 :         return 0;
     954                 :            : }
     955                 :            : 
     956                 :          0 : static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
     957                 :            : {
     958                 :            :         struct spi_transfer *xfer;
     959                 :            :         struct device *tx_dev, *rx_dev;
     960                 :            : 
     961                 :          0 :         if (!ctlr->cur_msg_mapped || !ctlr->can_dma)
     962                 :            :                 return 0;
     963                 :            : 
     964                 :          0 :         if (ctlr->dma_tx)
     965                 :          0 :                 tx_dev = ctlr->dma_tx->device->dev;
     966                 :            :         else
     967                 :          0 :                 tx_dev = ctlr->dev.parent;
     968                 :            : 
     969                 :          0 :         if (ctlr->dma_rx)
     970                 :          0 :                 rx_dev = ctlr->dma_rx->device->dev;
     971                 :            :         else
     972                 :          0 :                 rx_dev = ctlr->dev.parent;
     973                 :            : 
     974                 :          0 :         list_for_each_entry(xfer, &msg->transfers, transfer_list) {
     975                 :          0 :                 if (!ctlr->can_dma(ctlr, msg->spi, xfer))
     976                 :          0 :                         continue;
     977                 :            : 
     978                 :          0 :                 spi_unmap_buf(ctlr, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE);
     979                 :          0 :                 spi_unmap_buf(ctlr, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE);
     980                 :            :         }
     981                 :            : 
     982                 :            :         return 0;
     983                 :            : }
     984                 :            : #else /* !CONFIG_HAS_DMA */
     985                 :            : static inline int __spi_map_msg(struct spi_controller *ctlr,
     986                 :            :                                 struct spi_message *msg)
     987                 :            : {
     988                 :            :         return 0;
     989                 :            : }
     990                 :            : 
     991                 :            : static inline int __spi_unmap_msg(struct spi_controller *ctlr,
     992                 :            :                                   struct spi_message *msg)
     993                 :            : {
     994                 :            :         return 0;
     995                 :            : }
     996                 :            : #endif /* !CONFIG_HAS_DMA */
     997                 :            : 
     998                 :          0 : static inline int spi_unmap_msg(struct spi_controller *ctlr,
     999                 :            :                                 struct spi_message *msg)
    1000                 :            : {
    1001                 :            :         struct spi_transfer *xfer;
    1002                 :            : 
    1003                 :          0 :         list_for_each_entry(xfer, &msg->transfers, transfer_list) {
    1004                 :            :                 /*
    1005                 :            :                  * Restore the original value of tx_buf or rx_buf if they are
    1006                 :            :                  * NULL.
    1007                 :            :                  */
    1008                 :          0 :                 if (xfer->tx_buf == ctlr->dummy_tx)
    1009                 :          0 :                         xfer->tx_buf = NULL;
    1010                 :          0 :                 if (xfer->rx_buf == ctlr->dummy_rx)
    1011                 :          0 :                         xfer->rx_buf = NULL;
    1012                 :            :         }
    1013                 :            : 
    1014                 :          0 :         return __spi_unmap_msg(ctlr, msg);
    1015                 :            : }
    1016                 :            : 
    1017                 :          0 : static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
    1018                 :            : {
    1019                 :            :         struct spi_transfer *xfer;
    1020                 :            :         void *tmp;
    1021                 :            :         unsigned int max_tx, max_rx;
    1022                 :            : 
    1023                 :          0 :         if (ctlr->flags & (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX)) {
    1024                 :            :                 max_tx = 0;
    1025                 :            :                 max_rx = 0;
    1026                 :            : 
    1027                 :          0 :                 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
    1028                 :          0 :                         if ((ctlr->flags & SPI_CONTROLLER_MUST_TX) &&
    1029                 :          0 :                             !xfer->tx_buf)
    1030                 :          0 :                                 max_tx = max(xfer->len, max_tx);
    1031                 :          0 :                         if ((ctlr->flags & SPI_CONTROLLER_MUST_RX) &&
    1032                 :          0 :                             !xfer->rx_buf)
    1033                 :          0 :                                 max_rx = max(xfer->len, max_rx);
    1034                 :            :                 }
    1035                 :            : 
    1036                 :          0 :                 if (max_tx) {
    1037                 :          0 :                         tmp = krealloc(ctlr->dummy_tx, max_tx,
    1038                 :            :                                        GFP_KERNEL | GFP_DMA);
    1039                 :          0 :                         if (!tmp)
    1040                 :            :                                 return -ENOMEM;
    1041                 :          0 :                         ctlr->dummy_tx = tmp;
    1042                 :          0 :                         memset(tmp, 0, max_tx);
    1043                 :            :                 }
    1044                 :            : 
    1045                 :          0 :                 if (max_rx) {
    1046                 :          0 :                         tmp = krealloc(ctlr->dummy_rx, max_rx,
    1047                 :            :                                        GFP_KERNEL | GFP_DMA);
    1048                 :          0 :                         if (!tmp)
    1049                 :            :                                 return -ENOMEM;
    1050                 :          0 :                         ctlr->dummy_rx = tmp;
    1051                 :            :                 }
    1052                 :            : 
    1053                 :          0 :                 if (max_tx || max_rx) {
    1054                 :          0 :                         list_for_each_entry(xfer, &msg->transfers,
    1055                 :            :                                             transfer_list) {
    1056                 :          0 :                                 if (!xfer->len)
    1057                 :          0 :                                         continue;
    1058                 :          0 :                                 if (!xfer->tx_buf)
    1059                 :          0 :                                         xfer->tx_buf = ctlr->dummy_tx;
    1060                 :          0 :                                 if (!xfer->rx_buf)
    1061                 :          0 :                                         xfer->rx_buf = ctlr->dummy_rx;
    1062                 :            :                         }
    1063                 :            :                 }
    1064                 :            :         }
    1065                 :            : 
    1066                 :          0 :         return __spi_map_msg(ctlr, msg);
    1067                 :            : }
    1068                 :            : 
    1069                 :          0 : static int spi_transfer_wait(struct spi_controller *ctlr,
    1070                 :            :                              struct spi_message *msg,
    1071                 :            :                              struct spi_transfer *xfer)
    1072                 :            : {
    1073                 :            :         struct spi_statistics *statm = &ctlr->statistics;
    1074                 :          0 :         struct spi_statistics *stats = &msg->spi->statistics;
    1075                 :          0 :         unsigned long long ms = 1;
    1076                 :            : 
    1077                 :          0 :         if (spi_controller_is_slave(ctlr)) {
    1078                 :          0 :                 if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
    1079                 :            :                         dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
    1080                 :            :                         return -EINTR;
    1081                 :            :                 }
    1082                 :            :         } else {
    1083                 :          0 :                 ms = 8LL * 1000LL * xfer->len;
    1084                 :          0 :                 do_div(ms, xfer->speed_hz);
    1085                 :          0 :                 ms += ms + 200; /* some tolerance */
    1086                 :            : 
    1087                 :          0 :                 if (ms > UINT_MAX)
    1088                 :          0 :                         ms = UINT_MAX;
    1089                 :            : 
    1090                 :          0 :                 ms = wait_for_completion_timeout(&ctlr->xfer_completion,
    1091                 :            :                                                  msecs_to_jiffies(ms));
    1092                 :            : 
    1093                 :          0 :                 if (ms == 0) {
    1094                 :          0 :                         SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
    1095                 :          0 :                         SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
    1096                 :          0 :                         dev_err(&msg->spi->dev,
    1097                 :            :                                 "SPI transfer timed out\n");
    1098                 :          0 :                         return -ETIMEDOUT;
    1099                 :            :                 }
    1100                 :            :         }
    1101                 :            : 
    1102                 :            :         return 0;
    1103                 :            : }
    1104                 :            : 
    1105                 :          0 : static void _spi_transfer_delay_ns(u32 ns)
    1106                 :            : {
    1107                 :          0 :         if (!ns)
    1108                 :          0 :                 return;
    1109                 :          0 :         if (ns <= 1000) {
    1110                 :          0 :                 ndelay(ns);
    1111                 :            :         } else {
    1112                 :          0 :                 u32 us = DIV_ROUND_UP(ns, 1000);
    1113                 :            : 
    1114                 :          0 :                 if (us <= 10)
    1115                 :          0 :                         udelay(us);
    1116                 :            :                 else
    1117                 :          0 :                         usleep_range(us, us + DIV_ROUND_UP(us, 10));
    1118                 :            :         }
    1119                 :            : }
    1120                 :            : 
    1121                 :          0 : static void _spi_transfer_cs_change_delay(struct spi_message *msg,
    1122                 :            :                                           struct spi_transfer *xfer)
    1123                 :            : {
    1124                 :          0 :         u32 delay = xfer->cs_change_delay;
    1125                 :          0 :         u32 unit = xfer->cs_change_delay_unit;
    1126                 :            :         u32 hz;
    1127                 :            : 
    1128                 :            :         /* return early on "fast" mode - for everything but USECS */
    1129                 :          0 :         if (!delay && unit != SPI_DELAY_UNIT_USECS)
    1130                 :          0 :                 return;
    1131                 :            : 
    1132                 :          0 :         switch (unit) {
    1133                 :            :         case SPI_DELAY_UNIT_USECS:
    1134                 :            :                 /* for compatibility use default of 10us */
    1135                 :          0 :                 if (!delay)
    1136                 :            :                         delay = 10000;
    1137                 :            :                 else
    1138                 :          0 :                         delay *= 1000;
    1139                 :            :                 break;
    1140                 :            :         case SPI_DELAY_UNIT_NSECS: /* nothing to do here */
    1141                 :            :                 break;
    1142                 :            :         case SPI_DELAY_UNIT_SCK:
    1143                 :            :                 /* if there is no effective speed know, then approximate
    1144                 :            :                  * by underestimating with half the requested hz
    1145                 :            :                  */
    1146                 :          0 :                 hz = xfer->effective_speed_hz ?: xfer->speed_hz / 2;
    1147                 :          0 :                 delay *= DIV_ROUND_UP(1000000000, hz);
    1148                 :          0 :                 break;
    1149                 :            :         default:
    1150                 :          0 :                 dev_err_once(&msg->spi->dev,
    1151                 :            :                              "Use of unsupported delay unit %i, using default of 10us\n",
    1152                 :            :                              xfer->cs_change_delay_unit);
    1153                 :            :                 delay = 10000;
    1154                 :            :         }
    1155                 :            :         /* now sleep for the requested amount of time */
    1156                 :          0 :         _spi_transfer_delay_ns(delay);
    1157                 :            : }
    1158                 :            : 
    1159                 :            : /*
    1160                 :            :  * spi_transfer_one_message - Default implementation of transfer_one_message()
    1161                 :            :  *
    1162                 :            :  * This is a standard implementation of transfer_one_message() for
    1163                 :            :  * drivers which implement a transfer_one() operation.  It provides
    1164                 :            :  * standard handling of delays and chip select management.
    1165                 :            :  */
    1166                 :          0 : static int spi_transfer_one_message(struct spi_controller *ctlr,
    1167                 :            :                                     struct spi_message *msg)
    1168                 :            : {
    1169                 :            :         struct spi_transfer *xfer;
    1170                 :            :         bool keep_cs = false;
    1171                 :            :         int ret = 0;
    1172                 :          0 :         struct spi_statistics *statm = &ctlr->statistics;
    1173                 :          0 :         struct spi_statistics *stats = &msg->spi->statistics;
    1174                 :            : 
    1175                 :          0 :         spi_set_cs(msg->spi, true);
    1176                 :            : 
    1177                 :          0 :         SPI_STATISTICS_INCREMENT_FIELD(statm, messages);
    1178                 :          0 :         SPI_STATISTICS_INCREMENT_FIELD(stats, messages);
    1179                 :            : 
    1180                 :          0 :         list_for_each_entry(xfer, &msg->transfers, transfer_list) {
    1181                 :          0 :                 trace_spi_transfer_start(msg, xfer);
    1182                 :            : 
    1183                 :          0 :                 spi_statistics_add_transfer_stats(statm, xfer, ctlr);
    1184                 :          0 :                 spi_statistics_add_transfer_stats(stats, xfer, ctlr);
    1185                 :            : 
    1186                 :          0 :                 if (xfer->tx_buf || xfer->rx_buf) {
    1187                 :            :                         reinit_completion(&ctlr->xfer_completion);
    1188                 :            : 
    1189                 :          0 :                         ret = ctlr->transfer_one(ctlr, msg->spi, xfer);
    1190                 :          0 :                         if (ret < 0) {
    1191                 :          0 :                                 SPI_STATISTICS_INCREMENT_FIELD(statm,
    1192                 :            :                                                                errors);
    1193                 :          0 :                                 SPI_STATISTICS_INCREMENT_FIELD(stats,
    1194                 :            :                                                                errors);
    1195                 :          0 :                                 dev_err(&msg->spi->dev,
    1196                 :            :                                         "SPI transfer failed: %d\n", ret);
    1197                 :          0 :                                 goto out;
    1198                 :            :                         }
    1199                 :            : 
    1200                 :          0 :                         if (ret > 0) {
    1201                 :          0 :                                 ret = spi_transfer_wait(ctlr, msg, xfer);
    1202                 :          0 :                                 if (ret < 0)
    1203                 :          0 :                                         msg->status = ret;
    1204                 :            :                         }
    1205                 :            :                 } else {
    1206                 :          0 :                         if (xfer->len)
    1207                 :          0 :                                 dev_err(&msg->spi->dev,
    1208                 :            :                                         "Bufferless transfer has length %u\n",
    1209                 :            :                                         xfer->len);
    1210                 :            :                 }
    1211                 :            : 
    1212                 :          0 :                 trace_spi_transfer_stop(msg, xfer);
    1213                 :            : 
    1214                 :          0 :                 if (msg->status != -EINPROGRESS)
    1215                 :            :                         goto out;
    1216                 :            : 
    1217                 :          0 :                 if (xfer->delay_usecs)
    1218                 :          0 :                         _spi_transfer_delay_ns(xfer->delay_usecs * 1000);
    1219                 :            : 
    1220                 :          0 :                 if (xfer->cs_change) {
    1221                 :          0 :                         if (list_is_last(&xfer->transfer_list,
    1222                 :            :                                          &msg->transfers)) {
    1223                 :            :                                 keep_cs = true;
    1224                 :            :                         } else {
    1225                 :          0 :                                 spi_set_cs(msg->spi, false);
    1226                 :          0 :                                 _spi_transfer_cs_change_delay(msg, xfer);
    1227                 :          0 :                                 spi_set_cs(msg->spi, true);
    1228                 :            :                         }
    1229                 :            :                 }
    1230                 :            : 
    1231                 :          0 :                 msg->actual_length += xfer->len;
    1232                 :            :         }
    1233                 :            : 
    1234                 :            : out:
    1235                 :          0 :         if (ret != 0 || !keep_cs)
    1236                 :          0 :                 spi_set_cs(msg->spi, false);
    1237                 :            : 
    1238                 :          0 :         if (msg->status == -EINPROGRESS)
    1239                 :          0 :                 msg->status = ret;
    1240                 :            : 
    1241                 :          0 :         if (msg->status && ctlr->handle_err)
    1242                 :          0 :                 ctlr->handle_err(ctlr, msg);
    1243                 :            : 
    1244                 :          0 :         spi_res_release(ctlr, msg);
    1245                 :            : 
    1246                 :          0 :         spi_finalize_current_message(ctlr);
    1247                 :            : 
    1248                 :          0 :         return ret;
    1249                 :            : }
    1250                 :            : 
    1251                 :            : /**
    1252                 :            :  * spi_finalize_current_transfer - report completion of a transfer
    1253                 :            :  * @ctlr: the controller reporting completion
    1254                 :            :  *
    1255                 :            :  * Called by SPI drivers using the core transfer_one_message()
    1256                 :            :  * implementation to notify it that the current interrupt driven
    1257                 :            :  * transfer has finished and the next one may be scheduled.
    1258                 :            :  */
    1259                 :          0 : void spi_finalize_current_transfer(struct spi_controller *ctlr)
    1260                 :            : {
    1261                 :          0 :         complete(&ctlr->xfer_completion);
    1262                 :          0 : }
    1263                 :            : EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
    1264                 :            : 
    1265                 :            : /**
    1266                 :            :  * __spi_pump_messages - function which processes spi message queue
    1267                 :            :  * @ctlr: controller to process queue for
    1268                 :            :  * @in_kthread: true if we are in the context of the message pump thread
    1269                 :            :  *
    1270                 :            :  * This function checks if there is any spi message in the queue that
    1271                 :            :  * needs processing and if so call out to the driver to initialize hardware
    1272                 :            :  * and transfer each message.
    1273                 :            :  *
    1274                 :            :  * Note that it is called both from the kthread itself and also from
    1275                 :            :  * inside spi_sync(); the queue extraction handling at the top of the
    1276                 :            :  * function should deal with this safely.
    1277                 :            :  */
    1278                 :          0 : static void __spi_pump_messages(struct spi_controller *ctlr, bool in_kthread)
    1279                 :            : {
    1280                 :            :         struct spi_message *msg;
    1281                 :            :         bool was_busy = false;
    1282                 :            :         unsigned long flags;
    1283                 :            :         int ret;
    1284                 :            : 
    1285                 :            :         /* Lock queue */
    1286                 :          0 :         spin_lock_irqsave(&ctlr->queue_lock, flags);
    1287                 :            : 
    1288                 :            :         /* Make sure we are not already running a message */
    1289                 :          0 :         if (ctlr->cur_msg) {
    1290                 :            :                 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1291                 :            :                 return;
    1292                 :            :         }
    1293                 :            : 
    1294                 :            :         /* If another context is idling the device then defer */
    1295                 :          0 :         if (ctlr->idling) {
    1296                 :          0 :                 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
    1297                 :            :                 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1298                 :            :                 return;
    1299                 :            :         }
    1300                 :            : 
    1301                 :            :         /* Check if the queue is idle */
    1302                 :          0 :         if (list_empty(&ctlr->queue) || !ctlr->running) {
    1303                 :          0 :                 if (!ctlr->busy) {
    1304                 :            :                         spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1305                 :            :                         return;
    1306                 :            :                 }
    1307                 :            : 
    1308                 :            :                 /* Only do teardown in the thread */
    1309                 :          0 :                 if (!in_kthread) {
    1310                 :          0 :                         kthread_queue_work(&ctlr->kworker,
    1311                 :            :                                            &ctlr->pump_messages);
    1312                 :            :                         spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1313                 :            :                         return;
    1314                 :            :                 }
    1315                 :            : 
    1316                 :          0 :                 ctlr->busy = false;
    1317                 :          0 :                 ctlr->idling = true;
    1318                 :            :                 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1319                 :            : 
    1320                 :          0 :                 kfree(ctlr->dummy_rx);
    1321                 :          0 :                 ctlr->dummy_rx = NULL;
    1322                 :          0 :                 kfree(ctlr->dummy_tx);
    1323                 :          0 :                 ctlr->dummy_tx = NULL;
    1324                 :          0 :                 if (ctlr->unprepare_transfer_hardware &&
    1325                 :          0 :                     ctlr->unprepare_transfer_hardware(ctlr))
    1326                 :          0 :                         dev_err(&ctlr->dev,
    1327                 :            :                                 "failed to unprepare transfer hardware\n");
    1328                 :          0 :                 if (ctlr->auto_runtime_pm) {
    1329                 :          0 :                         pm_runtime_mark_last_busy(ctlr->dev.parent);
    1330                 :          0 :                         pm_runtime_put_autosuspend(ctlr->dev.parent);
    1331                 :            :                 }
    1332                 :          0 :                 trace_spi_controller_idle(ctlr);
    1333                 :            : 
    1334                 :          0 :                 spin_lock_irqsave(&ctlr->queue_lock, flags);
    1335                 :          0 :                 ctlr->idling = false;
    1336                 :            :                 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1337                 :            :                 return;
    1338                 :            :         }
    1339                 :            : 
    1340                 :            :         /* Extract head of queue */
    1341                 :          0 :         msg = list_first_entry(&ctlr->queue, struct spi_message, queue);
    1342                 :          0 :         ctlr->cur_msg = msg;
    1343                 :            : 
    1344                 :          0 :         list_del_init(&msg->queue);
    1345                 :          0 :         if (ctlr->busy)
    1346                 :            :                 was_busy = true;
    1347                 :            :         else
    1348                 :          0 :                 ctlr->busy = true;
    1349                 :            :         spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1350                 :            : 
    1351                 :          0 :         mutex_lock(&ctlr->io_mutex);
    1352                 :            : 
    1353                 :          0 :         if (!was_busy && ctlr->auto_runtime_pm) {
    1354                 :          0 :                 ret = pm_runtime_get_sync(ctlr->dev.parent);
    1355                 :          0 :                 if (ret < 0) {
    1356                 :          0 :                         pm_runtime_put_noidle(ctlr->dev.parent);
    1357                 :          0 :                         dev_err(&ctlr->dev, "Failed to power device: %d\n",
    1358                 :            :                                 ret);
    1359                 :          0 :                         mutex_unlock(&ctlr->io_mutex);
    1360                 :          0 :                         return;
    1361                 :            :                 }
    1362                 :            :         }
    1363                 :            : 
    1364                 :          0 :         if (!was_busy)
    1365                 :          0 :                 trace_spi_controller_busy(ctlr);
    1366                 :            : 
    1367                 :          0 :         if (!was_busy && ctlr->prepare_transfer_hardware) {
    1368                 :          0 :                 ret = ctlr->prepare_transfer_hardware(ctlr);
    1369                 :          0 :                 if (ret) {
    1370                 :          0 :                         dev_err(&ctlr->dev,
    1371                 :            :                                 "failed to prepare transfer hardware: %d\n",
    1372                 :            :                                 ret);
    1373                 :            : 
    1374                 :          0 :                         if (ctlr->auto_runtime_pm)
    1375                 :          0 :                                 pm_runtime_put(ctlr->dev.parent);
    1376                 :            : 
    1377                 :          0 :                         msg->status = ret;
    1378                 :          0 :                         spi_finalize_current_message(ctlr);
    1379                 :            : 
    1380                 :          0 :                         mutex_unlock(&ctlr->io_mutex);
    1381                 :          0 :                         return;
    1382                 :            :                 }
    1383                 :            :         }
    1384                 :            : 
    1385                 :          0 :         trace_spi_message_start(msg);
    1386                 :            : 
    1387                 :          0 :         if (ctlr->prepare_message) {
    1388                 :          0 :                 ret = ctlr->prepare_message(ctlr, msg);
    1389                 :          0 :                 if (ret) {
    1390                 :          0 :                         dev_err(&ctlr->dev, "failed to prepare message: %d\n",
    1391                 :            :                                 ret);
    1392                 :          0 :                         msg->status = ret;
    1393                 :          0 :                         spi_finalize_current_message(ctlr);
    1394                 :          0 :                         goto out;
    1395                 :            :                 }
    1396                 :          0 :                 ctlr->cur_msg_prepared = true;
    1397                 :            :         }
    1398                 :            : 
    1399                 :          0 :         ret = spi_map_msg(ctlr, msg);
    1400                 :          0 :         if (ret) {
    1401                 :          0 :                 msg->status = ret;
    1402                 :          0 :                 spi_finalize_current_message(ctlr);
    1403                 :          0 :                 goto out;
    1404                 :            :         }
    1405                 :            : 
    1406                 :          0 :         ret = ctlr->transfer_one_message(ctlr, msg);
    1407                 :          0 :         if (ret) {
    1408                 :          0 :                 dev_err(&ctlr->dev,
    1409                 :            :                         "failed to transfer one message from queue\n");
    1410                 :          0 :                 goto out;
    1411                 :            :         }
    1412                 :            : 
    1413                 :            : out:
    1414                 :          0 :         mutex_unlock(&ctlr->io_mutex);
    1415                 :            : 
    1416                 :            :         /* Prod the scheduler in case transfer_one() was busy waiting */
    1417                 :          0 :         if (!ret)
    1418                 :          0 :                 cond_resched();
    1419                 :            : }
    1420                 :            : 
    1421                 :            : /**
    1422                 :            :  * spi_pump_messages - kthread work function which processes spi message queue
    1423                 :            :  * @work: pointer to kthread work struct contained in the controller struct
    1424                 :            :  */
    1425                 :          0 : static void spi_pump_messages(struct kthread_work *work)
    1426                 :            : {
    1427                 :            :         struct spi_controller *ctlr =
    1428                 :          0 :                 container_of(work, struct spi_controller, pump_messages);
    1429                 :            : 
    1430                 :          0 :         __spi_pump_messages(ctlr, true);
    1431                 :          0 : }
    1432                 :            : 
    1433                 :            : /**
    1434                 :            :  * spi_set_thread_rt - set the controller to pump at realtime priority
    1435                 :            :  * @ctlr: controller to boost priority of
    1436                 :            :  *
    1437                 :            :  * This can be called because the controller requested realtime priority
    1438                 :            :  * (by setting the ->rt value before calling spi_register_controller()) or
    1439                 :            :  * because a device on the bus said that its transfers needed realtime
    1440                 :            :  * priority.
    1441                 :            :  *
    1442                 :            :  * NOTE: at the moment if any device on a bus says it needs realtime then
    1443                 :            :  * the thread will be at realtime priority for all transfers on that
    1444                 :            :  * controller.  If this eventually becomes a problem we may see if we can
    1445                 :            :  * find a way to boost the priority only temporarily during relevant
    1446                 :            :  * transfers.
    1447                 :            :  */
    1448                 :          0 : static void spi_set_thread_rt(struct spi_controller *ctlr)
    1449                 :            : {
    1450                 :          0 :         struct sched_param param = { .sched_priority = MAX_RT_PRIO / 2 };
    1451                 :            : 
    1452                 :          0 :         dev_info(&ctlr->dev,
    1453                 :            :                 "will run message pump with realtime priority\n");
    1454                 :          0 :         sched_setscheduler(ctlr->kworker_task, SCHED_FIFO, &param);
    1455                 :          0 : }
    1456                 :            : 
    1457                 :          0 : static int spi_init_queue(struct spi_controller *ctlr)
    1458                 :            : {
    1459                 :          0 :         ctlr->running = false;
    1460                 :          0 :         ctlr->busy = false;
    1461                 :            : 
    1462                 :          0 :         kthread_init_worker(&ctlr->kworker);
    1463                 :          0 :         ctlr->kworker_task = kthread_run(kthread_worker_fn, &ctlr->kworker,
    1464                 :            :                                          "%s", dev_name(&ctlr->dev));
    1465                 :          0 :         if (IS_ERR(ctlr->kworker_task)) {
    1466                 :          0 :                 dev_err(&ctlr->dev, "failed to create message pump task\n");
    1467                 :          0 :                 return PTR_ERR(ctlr->kworker_task);
    1468                 :            :         }
    1469                 :          0 :         kthread_init_work(&ctlr->pump_messages, spi_pump_messages);
    1470                 :            : 
    1471                 :            :         /*
    1472                 :            :          * Controller config will indicate if this controller should run the
    1473                 :            :          * message pump with high (realtime) priority to reduce the transfer
    1474                 :            :          * latency on the bus by minimising the delay between a transfer
    1475                 :            :          * request and the scheduling of the message pump thread. Without this
    1476                 :            :          * setting the message pump thread will remain at default priority.
    1477                 :            :          */
    1478                 :          0 :         if (ctlr->rt)
    1479                 :          0 :                 spi_set_thread_rt(ctlr);
    1480                 :            : 
    1481                 :            :         return 0;
    1482                 :            : }
    1483                 :            : 
    1484                 :            : /**
    1485                 :            :  * spi_get_next_queued_message() - called by driver to check for queued
    1486                 :            :  * messages
    1487                 :            :  * @ctlr: the controller to check for queued messages
    1488                 :            :  *
    1489                 :            :  * If there are more messages in the queue, the next message is returned from
    1490                 :            :  * this call.
    1491                 :            :  *
    1492                 :            :  * Return: the next message in the queue, else NULL if the queue is empty.
    1493                 :            :  */
    1494                 :          0 : struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr)
    1495                 :            : {
    1496                 :            :         struct spi_message *next;
    1497                 :            :         unsigned long flags;
    1498                 :            : 
    1499                 :            :         /* get a pointer to the next message, if any */
    1500                 :          0 :         spin_lock_irqsave(&ctlr->queue_lock, flags);
    1501                 :          0 :         next = list_first_entry_or_null(&ctlr->queue, struct spi_message,
    1502                 :            :                                         queue);
    1503                 :            :         spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1504                 :            : 
    1505                 :          0 :         return next;
    1506                 :            : }
    1507                 :            : EXPORT_SYMBOL_GPL(spi_get_next_queued_message);
    1508                 :            : 
    1509                 :            : /**
    1510                 :            :  * spi_finalize_current_message() - the current message is complete
    1511                 :            :  * @ctlr: the controller to return the message to
    1512                 :            :  *
    1513                 :            :  * Called by the driver to notify the core that the message in the front of the
    1514                 :            :  * queue is complete and can be removed from the queue.
    1515                 :            :  */
    1516                 :          0 : void spi_finalize_current_message(struct spi_controller *ctlr)
    1517                 :            : {
    1518                 :            :         struct spi_message *mesg;
    1519                 :            :         unsigned long flags;
    1520                 :            :         int ret;
    1521                 :            : 
    1522                 :          0 :         spin_lock_irqsave(&ctlr->queue_lock, flags);
    1523                 :          0 :         mesg = ctlr->cur_msg;
    1524                 :            :         spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1525                 :            : 
    1526                 :          0 :         spi_unmap_msg(ctlr, mesg);
    1527                 :            : 
    1528                 :          0 :         if (ctlr->cur_msg_prepared && ctlr->unprepare_message) {
    1529                 :          0 :                 ret = ctlr->unprepare_message(ctlr, mesg);
    1530                 :          0 :                 if (ret) {
    1531                 :          0 :                         dev_err(&ctlr->dev, "failed to unprepare message: %d\n",
    1532                 :            :                                 ret);
    1533                 :            :                 }
    1534                 :            :         }
    1535                 :            : 
    1536                 :          0 :         spin_lock_irqsave(&ctlr->queue_lock, flags);
    1537                 :          0 :         ctlr->cur_msg = NULL;
    1538                 :          0 :         ctlr->cur_msg_prepared = false;
    1539                 :          0 :         kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
    1540                 :            :         spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1541                 :            : 
    1542                 :          0 :         trace_spi_message_done(mesg);
    1543                 :            : 
    1544                 :          0 :         mesg->state = NULL;
    1545                 :          0 :         if (mesg->complete)
    1546                 :          0 :                 mesg->complete(mesg->context);
    1547                 :          0 : }
    1548                 :            : EXPORT_SYMBOL_GPL(spi_finalize_current_message);
    1549                 :            : 
    1550                 :          0 : static int spi_start_queue(struct spi_controller *ctlr)
    1551                 :            : {
    1552                 :            :         unsigned long flags;
    1553                 :            : 
    1554                 :          0 :         spin_lock_irqsave(&ctlr->queue_lock, flags);
    1555                 :            : 
    1556                 :          0 :         if (ctlr->running || ctlr->busy) {
    1557                 :            :                 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1558                 :          0 :                 return -EBUSY;
    1559                 :            :         }
    1560                 :            : 
    1561                 :          0 :         ctlr->running = true;
    1562                 :          0 :         ctlr->cur_msg = NULL;
    1563                 :            :         spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1564                 :            : 
    1565                 :          0 :         kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
    1566                 :            : 
    1567                 :          0 :         return 0;
    1568                 :            : }
    1569                 :            : 
    1570                 :          0 : static int spi_stop_queue(struct spi_controller *ctlr)
    1571                 :            : {
    1572                 :            :         unsigned long flags;
    1573                 :            :         unsigned limit = 500;
    1574                 :            :         int ret = 0;
    1575                 :            : 
    1576                 :          0 :         spin_lock_irqsave(&ctlr->queue_lock, flags);
    1577                 :            : 
    1578                 :            :         /*
    1579                 :            :          * This is a bit lame, but is optimized for the common execution path.
    1580                 :            :          * A wait_queue on the ctlr->busy could be used, but then the common
    1581                 :            :          * execution path (pump_messages) would be required to call wake_up or
    1582                 :            :          * friends on every SPI message. Do this instead.
    1583                 :            :          */
    1584                 :          0 :         while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
    1585                 :            :                 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1586                 :          0 :                 usleep_range(10000, 11000);
    1587                 :          0 :                 spin_lock_irqsave(&ctlr->queue_lock, flags);
    1588                 :            :         }
    1589                 :            : 
    1590                 :          0 :         if (!list_empty(&ctlr->queue) || ctlr->busy)
    1591                 :            :                 ret = -EBUSY;
    1592                 :            :         else
    1593                 :          0 :                 ctlr->running = false;
    1594                 :            : 
    1595                 :            :         spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1596                 :            : 
    1597                 :          0 :         if (ret) {
    1598                 :          0 :                 dev_warn(&ctlr->dev, "could not stop message queue\n");
    1599                 :          0 :                 return ret;
    1600                 :            :         }
    1601                 :            :         return ret;
    1602                 :            : }
    1603                 :            : 
    1604                 :          0 : static int spi_destroy_queue(struct spi_controller *ctlr)
    1605                 :            : {
    1606                 :            :         int ret;
    1607                 :            : 
    1608                 :          0 :         ret = spi_stop_queue(ctlr);
    1609                 :            : 
    1610                 :            :         /*
    1611                 :            :          * kthread_flush_worker will block until all work is done.
    1612                 :            :          * If the reason that stop_queue timed out is that the work will never
    1613                 :            :          * finish, then it does no good to call flush/stop thread, so
    1614                 :            :          * return anyway.
    1615                 :            :          */
    1616                 :          0 :         if (ret) {
    1617                 :          0 :                 dev_err(&ctlr->dev, "problem destroying queue\n");
    1618                 :          0 :                 return ret;
    1619                 :            :         }
    1620                 :            : 
    1621                 :          0 :         kthread_flush_worker(&ctlr->kworker);
    1622                 :          0 :         kthread_stop(ctlr->kworker_task);
    1623                 :            : 
    1624                 :          0 :         return 0;
    1625                 :            : }
    1626                 :            : 
    1627                 :          0 : static int __spi_queued_transfer(struct spi_device *spi,
    1628                 :            :                                  struct spi_message *msg,
    1629                 :            :                                  bool need_pump)
    1630                 :            : {
    1631                 :          0 :         struct spi_controller *ctlr = spi->controller;
    1632                 :            :         unsigned long flags;
    1633                 :            : 
    1634                 :          0 :         spin_lock_irqsave(&ctlr->queue_lock, flags);
    1635                 :            : 
    1636                 :          0 :         if (!ctlr->running) {
    1637                 :            :                 spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1638                 :          0 :                 return -ESHUTDOWN;
    1639                 :            :         }
    1640                 :          0 :         msg->actual_length = 0;
    1641                 :          0 :         msg->status = -EINPROGRESS;
    1642                 :            : 
    1643                 :          0 :         list_add_tail(&msg->queue, &ctlr->queue);
    1644                 :          0 :         if (!ctlr->busy && need_pump)
    1645                 :          0 :                 kthread_queue_work(&ctlr->kworker, &ctlr->pump_messages);
    1646                 :            : 
    1647                 :            :         spin_unlock_irqrestore(&ctlr->queue_lock, flags);
    1648                 :          0 :         return 0;
    1649                 :            : }
    1650                 :            : 
    1651                 :            : /**
    1652                 :            :  * spi_queued_transfer - transfer function for queued transfers
    1653                 :            :  * @spi: spi device which is requesting transfer
    1654                 :            :  * @msg: spi message which is to handled is queued to driver queue
    1655                 :            :  *
    1656                 :            :  * Return: zero on success, else a negative error code.
    1657                 :            :  */
    1658                 :          0 : static int spi_queued_transfer(struct spi_device *spi, struct spi_message *msg)
    1659                 :            : {
    1660                 :          0 :         return __spi_queued_transfer(spi, msg, true);
    1661                 :            : }
    1662                 :            : 
    1663                 :          0 : static int spi_controller_initialize_queue(struct spi_controller *ctlr)
    1664                 :            : {
    1665                 :            :         int ret;
    1666                 :            : 
    1667                 :          0 :         ctlr->transfer = spi_queued_transfer;
    1668                 :          0 :         if (!ctlr->transfer_one_message)
    1669                 :          0 :                 ctlr->transfer_one_message = spi_transfer_one_message;
    1670                 :            : 
    1671                 :            :         /* Initialize and start queue */
    1672                 :          0 :         ret = spi_init_queue(ctlr);
    1673                 :          0 :         if (ret) {
    1674                 :          0 :                 dev_err(&ctlr->dev, "problem initializing queue\n");
    1675                 :          0 :                 goto err_init_queue;
    1676                 :            :         }
    1677                 :          0 :         ctlr->queued = true;
    1678                 :          0 :         ret = spi_start_queue(ctlr);
    1679                 :          0 :         if (ret) {
    1680                 :          0 :                 dev_err(&ctlr->dev, "problem starting queue\n");
    1681                 :            :                 goto err_start_queue;
    1682                 :            :         }
    1683                 :            : 
    1684                 :            :         return 0;
    1685                 :            : 
    1686                 :            : err_start_queue:
    1687                 :          0 :         spi_destroy_queue(ctlr);
    1688                 :            : err_init_queue:
    1689                 :          0 :         return ret;
    1690                 :            : }
    1691                 :            : 
    1692                 :            : /**
    1693                 :            :  * spi_flush_queue - Send all pending messages in the queue from the callers'
    1694                 :            :  *                   context
    1695                 :            :  * @ctlr: controller to process queue for
    1696                 :            :  *
    1697                 :            :  * This should be used when one wants to ensure all pending messages have been
    1698                 :            :  * sent before doing something. Is used by the spi-mem code to make sure SPI
    1699                 :            :  * memory operations do not preempt regular SPI transfers that have been queued
    1700                 :            :  * before the spi-mem operation.
    1701                 :            :  */
    1702                 :          0 : void spi_flush_queue(struct spi_controller *ctlr)
    1703                 :            : {
    1704                 :          0 :         if (ctlr->transfer == spi_queued_transfer)
    1705                 :          0 :                 __spi_pump_messages(ctlr, false);
    1706                 :          0 : }
    1707                 :            : 
    1708                 :            : /*-------------------------------------------------------------------------*/
    1709                 :            : 
    1710                 :            : #if defined(CONFIG_OF)
    1711                 :          0 : static int of_spi_parse_dt(struct spi_controller *ctlr, struct spi_device *spi,
    1712                 :            :                            struct device_node *nc)
    1713                 :            : {
    1714                 :            :         u32 value;
    1715                 :            :         int rc;
    1716                 :            : 
    1717                 :            :         /* Mode (clock phase/polarity/etc.) */
    1718                 :          0 :         if (of_property_read_bool(nc, "spi-cpha"))
    1719                 :          0 :                 spi->mode |= SPI_CPHA;
    1720                 :          0 :         if (of_property_read_bool(nc, "spi-cpol"))
    1721                 :          0 :                 spi->mode |= SPI_CPOL;
    1722                 :          0 :         if (of_property_read_bool(nc, "spi-3wire"))
    1723                 :          0 :                 spi->mode |= SPI_3WIRE;
    1724                 :          0 :         if (of_property_read_bool(nc, "spi-lsb-first"))
    1725                 :          0 :                 spi->mode |= SPI_LSB_FIRST;
    1726                 :          0 :         if (of_property_read_bool(nc, "spi-cs-high"))
    1727                 :          0 :                 spi->mode |= SPI_CS_HIGH;
    1728                 :            : 
    1729                 :            :         /* Device DUAL/QUAD mode */
    1730                 :          0 :         if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
    1731                 :          0 :                 switch (value) {
    1732                 :            :                 case 1:
    1733                 :            :                         break;
    1734                 :            :                 case 2:
    1735                 :          0 :                         spi->mode |= SPI_TX_DUAL;
    1736                 :          0 :                         break;
    1737                 :            :                 case 4:
    1738                 :          0 :                         spi->mode |= SPI_TX_QUAD;
    1739                 :          0 :                         break;
    1740                 :            :                 case 8:
    1741                 :          0 :                         spi->mode |= SPI_TX_OCTAL;
    1742                 :          0 :                         break;
    1743                 :            :                 default:
    1744                 :          0 :                         dev_warn(&ctlr->dev,
    1745                 :            :                                 "spi-tx-bus-width %d not supported\n",
    1746                 :            :                                 value);
    1747                 :          0 :                         break;
    1748                 :            :                 }
    1749                 :            :         }
    1750                 :            : 
    1751                 :          0 :         if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
    1752                 :          0 :                 switch (value) {
    1753                 :            :                 case 1:
    1754                 :            :                         break;
    1755                 :            :                 case 2:
    1756                 :          0 :                         spi->mode |= SPI_RX_DUAL;
    1757                 :          0 :                         break;
    1758                 :            :                 case 4:
    1759                 :          0 :                         spi->mode |= SPI_RX_QUAD;
    1760                 :          0 :                         break;
    1761                 :            :                 case 8:
    1762                 :          0 :                         spi->mode |= SPI_RX_OCTAL;
    1763                 :          0 :                         break;
    1764                 :            :                 default:
    1765                 :          0 :                         dev_warn(&ctlr->dev,
    1766                 :            :                                 "spi-rx-bus-width %d not supported\n",
    1767                 :            :                                 value);
    1768                 :          0 :                         break;
    1769                 :            :                 }
    1770                 :            :         }
    1771                 :            : 
    1772                 :          0 :         if (spi_controller_is_slave(ctlr)) {
    1773                 :          0 :                 if (!of_node_name_eq(nc, "slave")) {
    1774                 :          0 :                         dev_err(&ctlr->dev, "%pOF is not called 'slave'\n",
    1775                 :            :                                 nc);
    1776                 :          0 :                         return -EINVAL;
    1777                 :            :                 }
    1778                 :            :                 return 0;
    1779                 :            :         }
    1780                 :            : 
    1781                 :            :         /* Device address */
    1782                 :            :         rc = of_property_read_u32(nc, "reg", &value);
    1783                 :          0 :         if (rc) {
    1784                 :          0 :                 dev_err(&ctlr->dev, "%pOF has no valid 'reg' property (%d)\n",
    1785                 :            :                         nc, rc);
    1786                 :          0 :                 return rc;
    1787                 :            :         }
    1788                 :          0 :         spi->chip_select = value;
    1789                 :            : 
    1790                 :            :         /* Device speed */
    1791                 :            :         rc = of_property_read_u32(nc, "spi-max-frequency", &value);
    1792                 :          0 :         if (rc) {
    1793                 :          0 :                 dev_err(&ctlr->dev,
    1794                 :            :                         "%pOF has no valid 'spi-max-frequency' property (%d)\n", nc, rc);
    1795                 :          0 :                 return rc;
    1796                 :            :         }
    1797                 :          0 :         spi->max_speed_hz = value;
    1798                 :            : 
    1799                 :          0 :         return 0;
    1800                 :            : }
    1801                 :            : 
    1802                 :            : static struct spi_device *
    1803                 :          0 : of_register_spi_device(struct spi_controller *ctlr, struct device_node *nc)
    1804                 :            : {
    1805                 :            :         struct spi_device *spi;
    1806                 :            :         int rc;
    1807                 :            : 
    1808                 :            :         /* Alloc an spi_device */
    1809                 :          0 :         spi = spi_alloc_device(ctlr);
    1810                 :          0 :         if (!spi) {
    1811                 :          0 :                 dev_err(&ctlr->dev, "spi_device alloc error for %pOF\n", nc);
    1812                 :            :                 rc = -ENOMEM;
    1813                 :          0 :                 goto err_out;
    1814                 :            :         }
    1815                 :            : 
    1816                 :            :         /* Select device driver */
    1817                 :          0 :         rc = of_modalias_node(nc, spi->modalias,
    1818                 :            :                                 sizeof(spi->modalias));
    1819                 :          0 :         if (rc < 0) {
    1820                 :          0 :                 dev_err(&ctlr->dev, "cannot find modalias for %pOF\n", nc);
    1821                 :          0 :                 goto err_out;
    1822                 :            :         }
    1823                 :            : 
    1824                 :          0 :         rc = of_spi_parse_dt(ctlr, spi, nc);
    1825                 :          0 :         if (rc)
    1826                 :            :                 goto err_out;
    1827                 :            : 
    1828                 :            :         /* Store a pointer to the node in the device structure */
    1829                 :          0 :         of_node_get(nc);
    1830                 :          0 :         spi->dev.of_node = nc;
    1831                 :            : 
    1832                 :            :         /* Register the new device */
    1833                 :          0 :         rc = spi_add_device(spi);
    1834                 :          0 :         if (rc) {
    1835                 :          0 :                 dev_err(&ctlr->dev, "spi_device register error %pOF\n", nc);
    1836                 :            :                 goto err_of_node_put;
    1837                 :            :         }
    1838                 :            : 
    1839                 :            :         return spi;
    1840                 :            : 
    1841                 :            : err_of_node_put:
    1842                 :          0 :         of_node_put(nc);
    1843                 :            : err_out:
    1844                 :            :         spi_dev_put(spi);
    1845                 :          0 :         return ERR_PTR(rc);
    1846                 :            : }
    1847                 :            : 
    1848                 :            : /**
    1849                 :            :  * of_register_spi_devices() - Register child devices onto the SPI bus
    1850                 :            :  * @ctlr:       Pointer to spi_controller device
    1851                 :            :  *
    1852                 :            :  * Registers an spi_device for each child node of controller node which
    1853                 :            :  * represents a valid SPI slave.
    1854                 :            :  */
    1855                 :          0 : static void of_register_spi_devices(struct spi_controller *ctlr)
    1856                 :            : {
    1857                 :            :         struct spi_device *spi;
    1858                 :            :         struct device_node *nc;
    1859                 :            : 
    1860                 :          0 :         if (!ctlr->dev.of_node)
    1861                 :          0 :                 return;
    1862                 :            : 
    1863                 :          0 :         for_each_available_child_of_node(ctlr->dev.of_node, nc) {
    1864                 :          0 :                 if (of_node_test_and_set_flag(nc, OF_POPULATED))
    1865                 :          0 :                         continue;
    1866                 :          0 :                 spi = of_register_spi_device(ctlr, nc);
    1867                 :          0 :                 if (IS_ERR(spi)) {
    1868                 :          0 :                         dev_warn(&ctlr->dev,
    1869                 :            :                                  "Failed to create SPI device for %pOF\n", nc);
    1870                 :            :                         of_node_clear_flag(nc, OF_POPULATED);
    1871                 :            :                 }
    1872                 :            :         }
    1873                 :            : }
    1874                 :            : #else
    1875                 :            : static void of_register_spi_devices(struct spi_controller *ctlr) { }
    1876                 :            : #endif
    1877                 :            : 
    1878                 :            : #ifdef CONFIG_ACPI
    1879                 :            : struct acpi_spi_lookup {
    1880                 :            :         struct spi_controller   *ctlr;
    1881                 :            :         u32                     max_speed_hz;
    1882                 :            :         u32                     mode;
    1883                 :            :         int                     irq;
    1884                 :            :         u8                      bits_per_word;
    1885                 :            :         u8                      chip_select;
    1886                 :            : };
    1887                 :            : 
    1888                 :            : static void acpi_spi_parse_apple_properties(struct acpi_device *dev,
    1889                 :            :                                             struct acpi_spi_lookup *lookup)
    1890                 :            : {
    1891                 :            :         const union acpi_object *obj;
    1892                 :            : 
    1893                 :            :         if (!x86_apple_machine)
    1894                 :            :                 return;
    1895                 :            : 
    1896                 :            :         if (!acpi_dev_get_property(dev, "spiSclkPeriod", ACPI_TYPE_BUFFER, &obj)
    1897                 :            :             && obj->buffer.length >= 4)
    1898                 :            :                 lookup->max_speed_hz  = NSEC_PER_SEC / *(u32 *)obj->buffer.pointer;
    1899                 :            : 
    1900                 :            :         if (!acpi_dev_get_property(dev, "spiWordSize", ACPI_TYPE_BUFFER, &obj)
    1901                 :            :             && obj->buffer.length == 8)
    1902                 :            :                 lookup->bits_per_word = *(u64 *)obj->buffer.pointer;
    1903                 :            : 
    1904                 :            :         if (!acpi_dev_get_property(dev, "spiBitOrder", ACPI_TYPE_BUFFER, &obj)
    1905                 :            :             && obj->buffer.length == 8 && !*(u64 *)obj->buffer.pointer)
    1906                 :            :                 lookup->mode |= SPI_LSB_FIRST;
    1907                 :            : 
    1908                 :            :         if (!acpi_dev_get_property(dev, "spiSPO", ACPI_TYPE_BUFFER, &obj)
    1909                 :            :             && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
    1910                 :            :                 lookup->mode |= SPI_CPOL;
    1911                 :            : 
    1912                 :            :         if (!acpi_dev_get_property(dev, "spiSPH", ACPI_TYPE_BUFFER, &obj)
    1913                 :            :             && obj->buffer.length == 8 &&  *(u64 *)obj->buffer.pointer)
    1914                 :            :                 lookup->mode |= SPI_CPHA;
    1915                 :            : }
    1916                 :            : 
    1917                 :            : static int acpi_spi_add_resource(struct acpi_resource *ares, void *data)
    1918                 :            : {
    1919                 :            :         struct acpi_spi_lookup *lookup = data;
    1920                 :            :         struct spi_controller *ctlr = lookup->ctlr;
    1921                 :            : 
    1922                 :            :         if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
    1923                 :            :                 struct acpi_resource_spi_serialbus *sb;
    1924                 :            :                 acpi_handle parent_handle;
    1925                 :            :                 acpi_status status;
    1926                 :            : 
    1927                 :            :                 sb = &ares->data.spi_serial_bus;
    1928                 :            :                 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_SPI) {
    1929                 :            : 
    1930                 :            :                         status = acpi_get_handle(NULL,
    1931                 :            :                                                  sb->resource_source.string_ptr,
    1932                 :            :                                                  &parent_handle);
    1933                 :            : 
    1934                 :            :                         if (ACPI_FAILURE(status) ||
    1935                 :            :                             ACPI_HANDLE(ctlr->dev.parent) != parent_handle)
    1936                 :            :                                 return -ENODEV;
    1937                 :            : 
    1938                 :            :                         /*
    1939                 :            :                          * ACPI DeviceSelection numbering is handled by the
    1940                 :            :                          * host controller driver in Windows and can vary
    1941                 :            :                          * from driver to driver. In Linux we always expect
    1942                 :            :                          * 0 .. max - 1 so we need to ask the driver to
    1943                 :            :                          * translate between the two schemes.
    1944                 :            :                          */
    1945                 :            :                         if (ctlr->fw_translate_cs) {
    1946                 :            :                                 int cs = ctlr->fw_translate_cs(ctlr,
    1947                 :            :                                                 sb->device_selection);
    1948                 :            :                                 if (cs < 0)
    1949                 :            :                                         return cs;
    1950                 :            :                                 lookup->chip_select = cs;
    1951                 :            :                         } else {
    1952                 :            :                                 lookup->chip_select = sb->device_selection;
    1953                 :            :                         }
    1954                 :            : 
    1955                 :            :                         lookup->max_speed_hz = sb->connection_speed;
    1956                 :            :                         lookup->bits_per_word = sb->data_bit_length;
    1957                 :            : 
    1958                 :            :                         if (sb->clock_phase == ACPI_SPI_SECOND_PHASE)
    1959                 :            :                                 lookup->mode |= SPI_CPHA;
    1960                 :            :                         if (sb->clock_polarity == ACPI_SPI_START_HIGH)
    1961                 :            :                                 lookup->mode |= SPI_CPOL;
    1962                 :            :                         if (sb->device_polarity == ACPI_SPI_ACTIVE_HIGH)
    1963                 :            :                                 lookup->mode |= SPI_CS_HIGH;
    1964                 :            :                 }
    1965                 :            :         } else if (lookup->irq < 0) {
    1966                 :            :                 struct resource r;
    1967                 :            : 
    1968                 :            :                 if (acpi_dev_resource_interrupt(ares, 0, &r))
    1969                 :            :                         lookup->irq = r.start;
    1970                 :            :         }
    1971                 :            : 
    1972                 :            :         /* Always tell the ACPI core to skip this resource */
    1973                 :            :         return 1;
    1974                 :            : }
    1975                 :            : 
    1976                 :            : static acpi_status acpi_register_spi_device(struct spi_controller *ctlr,
    1977                 :            :                                             struct acpi_device *adev)
    1978                 :            : {
    1979                 :            :         acpi_handle parent_handle = NULL;
    1980                 :            :         struct list_head resource_list;
    1981                 :            :         struct acpi_spi_lookup lookup = {};
    1982                 :            :         struct spi_device *spi;
    1983                 :            :         int ret;
    1984                 :            : 
    1985                 :            :         if (acpi_bus_get_status(adev) || !adev->status.present ||
    1986                 :            :             acpi_device_enumerated(adev))
    1987                 :            :                 return AE_OK;
    1988                 :            : 
    1989                 :            :         lookup.ctlr             = ctlr;
    1990                 :            :         lookup.irq              = -1;
    1991                 :            : 
    1992                 :            :         INIT_LIST_HEAD(&resource_list);
    1993                 :            :         ret = acpi_dev_get_resources(adev, &resource_list,
    1994                 :            :                                      acpi_spi_add_resource, &lookup);
    1995                 :            :         acpi_dev_free_resource_list(&resource_list);
    1996                 :            : 
    1997                 :            :         if (ret < 0)
    1998                 :            :                 /* found SPI in _CRS but it points to another controller */
    1999                 :            :                 return AE_OK;
    2000                 :            : 
    2001                 :            :         if (!lookup.max_speed_hz &&
    2002                 :            :             !ACPI_FAILURE(acpi_get_parent(adev->handle, &parent_handle)) &&
    2003                 :            :             ACPI_HANDLE(ctlr->dev.parent) == parent_handle) {
    2004                 :            :                 /* Apple does not use _CRS but nested devices for SPI slaves */
    2005                 :            :                 acpi_spi_parse_apple_properties(adev, &lookup);
    2006                 :            :         }
    2007                 :            : 
    2008                 :            :         if (!lookup.max_speed_hz)
    2009                 :            :                 return AE_OK;
    2010                 :            : 
    2011                 :            :         spi = spi_alloc_device(ctlr);
    2012                 :            :         if (!spi) {
    2013                 :            :                 dev_err(&ctlr->dev, "failed to allocate SPI device for %s\n",
    2014                 :            :                         dev_name(&adev->dev));
    2015                 :            :                 return AE_NO_MEMORY;
    2016                 :            :         }
    2017                 :            : 
    2018                 :            :         ACPI_COMPANION_SET(&spi->dev, adev);
    2019                 :            :         spi->max_speed_hz    = lookup.max_speed_hz;
    2020                 :            :         spi->mode            = lookup.mode;
    2021                 :            :         spi->irq             = lookup.irq;
    2022                 :            :         spi->bits_per_word   = lookup.bits_per_word;
    2023                 :            :         spi->chip_select     = lookup.chip_select;
    2024                 :            : 
    2025                 :            :         acpi_set_modalias(adev, acpi_device_hid(adev), spi->modalias,
    2026                 :            :                           sizeof(spi->modalias));
    2027                 :            : 
    2028                 :            :         if (spi->irq < 0)
    2029                 :            :                 spi->irq = acpi_dev_gpio_irq_get(adev, 0);
    2030                 :            : 
    2031                 :            :         acpi_device_set_enumerated(adev);
    2032                 :            : 
    2033                 :            :         adev->power.flags.ignore_parent = true;
    2034                 :            :         if (spi_add_device(spi)) {
    2035                 :            :                 adev->power.flags.ignore_parent = false;
    2036                 :            :                 dev_err(&ctlr->dev, "failed to add SPI device %s from ACPI\n",
    2037                 :            :                         dev_name(&adev->dev));
    2038                 :            :                 spi_dev_put(spi);
    2039                 :            :         }
    2040                 :            : 
    2041                 :            :         return AE_OK;
    2042                 :            : }
    2043                 :            : 
    2044                 :            : static acpi_status acpi_spi_add_device(acpi_handle handle, u32 level,
    2045                 :            :                                        void *data, void **return_value)
    2046                 :            : {
    2047                 :            :         struct spi_controller *ctlr = data;
    2048                 :            :         struct acpi_device *adev;
    2049                 :            : 
    2050                 :            :         if (acpi_bus_get_device(handle, &adev))
    2051                 :            :                 return AE_OK;
    2052                 :            : 
    2053                 :            :         return acpi_register_spi_device(ctlr, adev);
    2054                 :            : }
    2055                 :            : 
    2056                 :            : #define SPI_ACPI_ENUMERATE_MAX_DEPTH            32
    2057                 :            : 
    2058                 :            : static void acpi_register_spi_devices(struct spi_controller *ctlr)
    2059                 :            : {
    2060                 :            :         acpi_status status;
    2061                 :            :         acpi_handle handle;
    2062                 :            : 
    2063                 :            :         handle = ACPI_HANDLE(ctlr->dev.parent);
    2064                 :            :         if (!handle)
    2065                 :            :                 return;
    2066                 :            : 
    2067                 :            :         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
    2068                 :            :                                      SPI_ACPI_ENUMERATE_MAX_DEPTH,
    2069                 :            :                                      acpi_spi_add_device, NULL, ctlr, NULL);
    2070                 :            :         if (ACPI_FAILURE(status))
    2071                 :            :                 dev_warn(&ctlr->dev, "failed to enumerate SPI slaves\n");
    2072                 :            : }
    2073                 :            : #else
    2074                 :            : static inline void acpi_register_spi_devices(struct spi_controller *ctlr) {}
    2075                 :            : #endif /* CONFIG_ACPI */
    2076                 :            : 
    2077                 :          0 : static void spi_controller_release(struct device *dev)
    2078                 :            : {
    2079                 :            :         struct spi_controller *ctlr;
    2080                 :            : 
    2081                 :            :         ctlr = container_of(dev, struct spi_controller, dev);
    2082                 :          0 :         kfree(ctlr);
    2083                 :          0 : }
    2084                 :            : 
    2085                 :            : static struct class spi_master_class = {
    2086                 :            :         .name           = "spi_master",
    2087                 :            :         .owner          = THIS_MODULE,
    2088                 :            :         .dev_release    = spi_controller_release,
    2089                 :            :         .dev_groups     = spi_master_groups,
    2090                 :            : };
    2091                 :            : 
    2092                 :            : #ifdef CONFIG_SPI_SLAVE
    2093                 :            : /**
    2094                 :            :  * spi_slave_abort - abort the ongoing transfer request on an SPI slave
    2095                 :            :  *                   controller
    2096                 :            :  * @spi: device used for the current transfer
    2097                 :            :  */
    2098                 :          0 : int spi_slave_abort(struct spi_device *spi)
    2099                 :            : {
    2100                 :          0 :         struct spi_controller *ctlr = spi->controller;
    2101                 :            : 
    2102                 :          0 :         if (spi_controller_is_slave(ctlr) && ctlr->slave_abort)
    2103                 :          0 :                 return ctlr->slave_abort(ctlr);
    2104                 :            : 
    2105                 :            :         return -ENOTSUPP;
    2106                 :            : }
    2107                 :            : EXPORT_SYMBOL_GPL(spi_slave_abort);
    2108                 :            : 
    2109                 :          0 : static int match_true(struct device *dev, void *data)
    2110                 :            : {
    2111                 :          0 :         return 1;
    2112                 :            : }
    2113                 :            : 
    2114                 :          0 : static ssize_t slave_show(struct device *dev, struct device_attribute *attr,
    2115                 :            :                           char *buf)
    2116                 :            : {
    2117                 :            :         struct spi_controller *ctlr = container_of(dev, struct spi_controller,
    2118                 :            :                                                    dev);
    2119                 :            :         struct device *child;
    2120                 :            : 
    2121                 :          0 :         child = device_find_child(&ctlr->dev, NULL, match_true);
    2122                 :          0 :         return sprintf(buf, "%s\n",
    2123                 :            :                        child ? to_spi_device(child)->modalias : NULL);
    2124                 :            : }
    2125                 :            : 
    2126                 :          0 : static ssize_t slave_store(struct device *dev, struct device_attribute *attr,
    2127                 :            :                            const char *buf, size_t count)
    2128                 :            : {
    2129                 :            :         struct spi_controller *ctlr = container_of(dev, struct spi_controller,
    2130                 :            :                                                    dev);
    2131                 :            :         struct spi_device *spi;
    2132                 :            :         struct device *child;
    2133                 :            :         char name[32];
    2134                 :            :         int rc;
    2135                 :            : 
    2136                 :          0 :         rc = sscanf(buf, "%31s", name);
    2137                 :          0 :         if (rc != 1 || !name[0])
    2138                 :            :                 return -EINVAL;
    2139                 :            : 
    2140                 :          0 :         child = device_find_child(&ctlr->dev, NULL, match_true);
    2141                 :          0 :         if (child) {
    2142                 :            :                 /* Remove registered slave */
    2143                 :          0 :                 device_unregister(child);
    2144                 :          0 :                 put_device(child);
    2145                 :            :         }
    2146                 :            : 
    2147                 :          0 :         if (strcmp(name, "(null)")) {
    2148                 :            :                 /* Register new slave */
    2149                 :          0 :                 spi = spi_alloc_device(ctlr);
    2150                 :          0 :                 if (!spi)
    2151                 :            :                         return -ENOMEM;
    2152                 :            : 
    2153                 :          0 :                 strlcpy(spi->modalias, name, sizeof(spi->modalias));
    2154                 :            : 
    2155                 :          0 :                 rc = spi_add_device(spi);
    2156                 :          0 :                 if (rc) {
    2157                 :            :                         spi_dev_put(spi);
    2158                 :          0 :                         return rc;
    2159                 :            :                 }
    2160                 :            :         }
    2161                 :            : 
    2162                 :          0 :         return count;
    2163                 :            : }
    2164                 :            : 
    2165                 :            : static DEVICE_ATTR_RW(slave);
    2166                 :            : 
    2167                 :            : static struct attribute *spi_slave_attrs[] = {
    2168                 :            :         &dev_attr_slave.attr,
    2169                 :            :         NULL,
    2170                 :            : };
    2171                 :            : 
    2172                 :            : static const struct attribute_group spi_slave_group = {
    2173                 :            :         .attrs = spi_slave_attrs,
    2174                 :            : };
    2175                 :            : 
    2176                 :            : static const struct attribute_group *spi_slave_groups[] = {
    2177                 :            :         &spi_controller_statistics_group,
    2178                 :            :         &spi_slave_group,
    2179                 :            :         NULL,
    2180                 :            : };
    2181                 :            : 
    2182                 :            : static struct class spi_slave_class = {
    2183                 :            :         .name           = "spi_slave",
    2184                 :            :         .owner          = THIS_MODULE,
    2185                 :            :         .dev_release    = spi_controller_release,
    2186                 :            :         .dev_groups     = spi_slave_groups,
    2187                 :            : };
    2188                 :            : #else
    2189                 :            : extern struct class spi_slave_class;    /* dummy */
    2190                 :            : #endif
    2191                 :            : 
    2192                 :            : /**
    2193                 :            :  * __spi_alloc_controller - allocate an SPI master or slave controller
    2194                 :            :  * @dev: the controller, possibly using the platform_bus
    2195                 :            :  * @size: how much zeroed driver-private data to allocate; the pointer to this
    2196                 :            :  *      memory is in the driver_data field of the returned device, accessible
    2197                 :            :  *      with spi_controller_get_devdata(); the memory is cacheline aligned;
    2198                 :            :  *      drivers granting DMA access to portions of their private data need to
    2199                 :            :  *      round up @size using ALIGN(size, dma_get_cache_alignment()).
    2200                 :            :  * @slave: flag indicating whether to allocate an SPI master (false) or SPI
    2201                 :            :  *      slave (true) controller
    2202                 :            :  * Context: can sleep
    2203                 :            :  *
    2204                 :            :  * This call is used only by SPI controller drivers, which are the
    2205                 :            :  * only ones directly touching chip registers.  It's how they allocate
    2206                 :            :  * an spi_controller structure, prior to calling spi_register_controller().
    2207                 :            :  *
    2208                 :            :  * This must be called from context that can sleep.
    2209                 :            :  *
    2210                 :            :  * The caller is responsible for assigning the bus number and initializing the
    2211                 :            :  * controller's methods before calling spi_register_controller(); and (after
    2212                 :            :  * errors adding the device) calling spi_controller_put() to prevent a memory
    2213                 :            :  * leak.
    2214                 :            :  *
    2215                 :            :  * Return: the SPI controller structure on success, else NULL.
    2216                 :            :  */
    2217                 :          0 : struct spi_controller *__spi_alloc_controller(struct device *dev,
    2218                 :            :                                               unsigned int size, bool slave)
    2219                 :            : {
    2220                 :            :         struct spi_controller   *ctlr;
    2221                 :            :         size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment());
    2222                 :            : 
    2223                 :          0 :         if (!dev)
    2224                 :            :                 return NULL;
    2225                 :            : 
    2226                 :          0 :         ctlr = kzalloc(size + ctlr_size, GFP_KERNEL);
    2227                 :          0 :         if (!ctlr)
    2228                 :            :                 return NULL;
    2229                 :            : 
    2230                 :          0 :         device_initialize(&ctlr->dev);
    2231                 :          0 :         ctlr->bus_num = -1;
    2232                 :          0 :         ctlr->num_chipselect = 1;
    2233                 :          0 :         ctlr->slave = slave;
    2234                 :          0 :         if (IS_ENABLED(CONFIG_SPI_SLAVE) && slave)
    2235                 :          0 :                 ctlr->dev.class = &spi_slave_class;
    2236                 :            :         else
    2237                 :          0 :                 ctlr->dev.class = &spi_master_class;
    2238                 :          0 :         ctlr->dev.parent = dev;
    2239                 :            :         pm_suspend_ignore_children(&ctlr->dev, true);
    2240                 :          0 :         spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size);
    2241                 :            : 
    2242                 :          0 :         return ctlr;
    2243                 :            : }
    2244                 :            : EXPORT_SYMBOL_GPL(__spi_alloc_controller);
    2245                 :            : 
    2246                 :            : #ifdef CONFIG_OF
    2247                 :          0 : static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
    2248                 :            : {
    2249                 :            :         int nb, i, *cs;
    2250                 :          0 :         struct device_node *np = ctlr->dev.of_node;
    2251                 :            : 
    2252                 :          0 :         if (!np)
    2253                 :            :                 return 0;
    2254                 :            : 
    2255                 :            :         nb = of_gpio_named_count(np, "cs-gpios");
    2256                 :          0 :         ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
    2257                 :            : 
    2258                 :            :         /* Return error only for an incorrectly formed cs-gpios property */
    2259                 :          0 :         if (nb == 0 || nb == -ENOENT)
    2260                 :            :                 return 0;
    2261                 :          0 :         else if (nb < 0)
    2262                 :            :                 return nb;
    2263                 :            : 
    2264                 :          0 :         cs = devm_kcalloc(&ctlr->dev, ctlr->num_chipselect, sizeof(int),
    2265                 :            :                           GFP_KERNEL);
    2266                 :          0 :         ctlr->cs_gpios = cs;
    2267                 :            : 
    2268                 :          0 :         if (!ctlr->cs_gpios)
    2269                 :            :                 return -ENOMEM;
    2270                 :            : 
    2271                 :          0 :         for (i = 0; i < ctlr->num_chipselect; i++)
    2272                 :          0 :                 cs[i] = -ENOENT;
    2273                 :            : 
    2274                 :          0 :         for (i = 0; i < nb; i++)
    2275                 :          0 :                 cs[i] = of_get_named_gpio(np, "cs-gpios", i);
    2276                 :            : 
    2277                 :            :         return 0;
    2278                 :            : }
    2279                 :            : #else
    2280                 :            : static int of_spi_get_gpio_numbers(struct spi_controller *ctlr)
    2281                 :            : {
    2282                 :            :         return 0;
    2283                 :            : }
    2284                 :            : #endif
    2285                 :            : 
    2286                 :            : /**
    2287                 :            :  * spi_get_gpio_descs() - grab chip select GPIOs for the master
    2288                 :            :  * @ctlr: The SPI master to grab GPIO descriptors for
    2289                 :            :  */
    2290                 :          0 : static int spi_get_gpio_descs(struct spi_controller *ctlr)
    2291                 :            : {
    2292                 :            :         int nb, i;
    2293                 :            :         struct gpio_desc **cs;
    2294                 :          0 :         struct device *dev = &ctlr->dev;
    2295                 :            : 
    2296                 :          0 :         nb = gpiod_count(dev, "cs");
    2297                 :          0 :         ctlr->num_chipselect = max_t(int, nb, ctlr->num_chipselect);
    2298                 :            : 
    2299                 :            :         /* No GPIOs at all is fine, else return the error */
    2300                 :          0 :         if (nb == 0 || nb == -ENOENT)
    2301                 :            :                 return 0;
    2302                 :          0 :         else if (nb < 0)
    2303                 :            :                 return nb;
    2304                 :            : 
    2305                 :          0 :         cs = devm_kcalloc(dev, ctlr->num_chipselect, sizeof(*cs),
    2306                 :            :                           GFP_KERNEL);
    2307                 :          0 :         if (!cs)
    2308                 :            :                 return -ENOMEM;
    2309                 :          0 :         ctlr->cs_gpiods = cs;
    2310                 :            : 
    2311                 :          0 :         for (i = 0; i < nb; i++) {
    2312                 :            :                 /*
    2313                 :            :                  * Most chipselects are active low, the inverted
    2314                 :            :                  * semantics are handled by special quirks in gpiolib,
    2315                 :            :                  * so initializing them GPIOD_OUT_LOW here means
    2316                 :            :                  * "unasserted", in most cases this will drive the physical
    2317                 :            :                  * line high.
    2318                 :            :                  */
    2319                 :          0 :                 cs[i] = devm_gpiod_get_index_optional(dev, "cs", i,
    2320                 :            :                                                       GPIOD_OUT_LOW);
    2321                 :          0 :                 if (IS_ERR(cs[i]))
    2322                 :          0 :                         return PTR_ERR(cs[i]);
    2323                 :            : 
    2324                 :          0 :                 if (cs[i]) {
    2325                 :            :                         /*
    2326                 :            :                          * If we find a CS GPIO, name it after the device and
    2327                 :            :                          * chip select line.
    2328                 :            :                          */
    2329                 :            :                         char *gpioname;
    2330                 :            : 
    2331                 :          0 :                         gpioname = devm_kasprintf(dev, GFP_KERNEL, "%s CS%d",
    2332                 :            :                                                   dev_name(dev), i);
    2333                 :          0 :                         if (!gpioname)
    2334                 :            :                                 return -ENOMEM;
    2335                 :          0 :                         gpiod_set_consumer_name(cs[i], gpioname);
    2336                 :            :                 }
    2337                 :            :         }
    2338                 :            : 
    2339                 :            :         return 0;
    2340                 :            : }
    2341                 :            : 
    2342                 :            : static int spi_controller_check_ops(struct spi_controller *ctlr)
    2343                 :            : {
    2344                 :            :         /*
    2345                 :            :          * The controller may implement only the high-level SPI-memory like
    2346                 :            :          * operations if it does not support regular SPI transfers, and this is
    2347                 :            :          * valid use case.
    2348                 :            :          * If ->mem_ops is NULL, we request that at least one of the
    2349                 :            :          * ->transfer_xxx() method be implemented.
    2350                 :            :          */
    2351                 :          0 :         if (ctlr->mem_ops) {
    2352                 :          0 :                 if (!ctlr->mem_ops->exec_op)
    2353                 :            :                         return -EINVAL;
    2354                 :          0 :         } else if (!ctlr->transfer && !ctlr->transfer_one &&
    2355                 :          0 :                    !ctlr->transfer_one_message) {
    2356                 :            :                 return -EINVAL;
    2357                 :            :         }
    2358                 :            : 
    2359                 :            :         return 0;
    2360                 :            : }
    2361                 :            : 
    2362                 :            : /**
    2363                 :            :  * spi_register_controller - register SPI master or slave controller
    2364                 :            :  * @ctlr: initialized master, originally from spi_alloc_master() or
    2365                 :            :  *      spi_alloc_slave()
    2366                 :            :  * Context: can sleep
    2367                 :            :  *
    2368                 :            :  * SPI controllers connect to their drivers using some non-SPI bus,
    2369                 :            :  * such as the platform bus.  The final stage of probe() in that code
    2370                 :            :  * includes calling spi_register_controller() to hook up to this SPI bus glue.
    2371                 :            :  *
    2372                 :            :  * SPI controllers use board specific (often SOC specific) bus numbers,
    2373                 :            :  * and board-specific addressing for SPI devices combines those numbers
    2374                 :            :  * with chip select numbers.  Since SPI does not directly support dynamic
    2375                 :            :  * device identification, boards need configuration tables telling which
    2376                 :            :  * chip is at which address.
    2377                 :            :  *
    2378                 :            :  * This must be called from context that can sleep.  It returns zero on
    2379                 :            :  * success, else a negative error code (dropping the controller's refcount).
    2380                 :            :  * After a successful return, the caller is responsible for calling
    2381                 :            :  * spi_unregister_controller().
    2382                 :            :  *
    2383                 :            :  * Return: zero on success, else a negative error code.
    2384                 :            :  */
    2385                 :          0 : int spi_register_controller(struct spi_controller *ctlr)
    2386                 :            : {
    2387                 :          0 :         struct device           *dev = ctlr->dev.parent;
    2388                 :            :         struct boardinfo        *bi;
    2389                 :            :         int                     status;
    2390                 :            :         int                     id, first_dynamic;
    2391                 :            : 
    2392                 :          0 :         if (!dev)
    2393                 :            :                 return -ENODEV;
    2394                 :            : 
    2395                 :            :         /*
    2396                 :            :          * Make sure all necessary hooks are implemented before registering
    2397                 :            :          * the SPI controller.
    2398                 :            :          */
    2399                 :            :         status = spi_controller_check_ops(ctlr);
    2400                 :          0 :         if (status)
    2401                 :            :                 return status;
    2402                 :            : 
    2403                 :          0 :         if (ctlr->bus_num >= 0) {
    2404                 :            :                 /* devices with a fixed bus num must check-in with the num */
    2405                 :          0 :                 mutex_lock(&board_lock);
    2406                 :          0 :                 id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
    2407                 :          0 :                         ctlr->bus_num + 1, GFP_KERNEL);
    2408                 :          0 :                 mutex_unlock(&board_lock);
    2409                 :          0 :                 if (WARN(id < 0, "couldn't get idr"))
    2410                 :          0 :                         return id == -ENOSPC ? -EBUSY : id;
    2411                 :          0 :                 ctlr->bus_num = id;
    2412                 :          0 :         } else if (ctlr->dev.of_node) {
    2413                 :            :                 /* allocate dynamic bus number using Linux idr */
    2414                 :          0 :                 id = of_alias_get_id(ctlr->dev.of_node, "spi");
    2415                 :          0 :                 if (id >= 0) {
    2416                 :          0 :                         ctlr->bus_num = id;
    2417                 :          0 :                         mutex_lock(&board_lock);
    2418                 :          0 :                         id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
    2419                 :          0 :                                        ctlr->bus_num + 1, GFP_KERNEL);
    2420                 :          0 :                         mutex_unlock(&board_lock);
    2421                 :          0 :                         if (WARN(id < 0, "couldn't get idr"))
    2422                 :          0 :                                 return id == -ENOSPC ? -EBUSY : id;
    2423                 :            :                 }
    2424                 :            :         }
    2425                 :          0 :         if (ctlr->bus_num < 0) {
    2426                 :          0 :                 first_dynamic = of_alias_get_highest_id("spi");
    2427                 :          0 :                 if (first_dynamic < 0)
    2428                 :            :                         first_dynamic = 0;
    2429                 :            :                 else
    2430                 :          0 :                         first_dynamic++;
    2431                 :            : 
    2432                 :          0 :                 mutex_lock(&board_lock);
    2433                 :          0 :                 id = idr_alloc(&spi_master_idr, ctlr, first_dynamic,
    2434                 :            :                                0, GFP_KERNEL);
    2435                 :          0 :                 mutex_unlock(&board_lock);
    2436                 :          0 :                 if (WARN(id < 0, "couldn't get idr"))
    2437                 :            :                         return id;
    2438                 :          0 :                 ctlr->bus_num = id;
    2439                 :            :         }
    2440                 :          0 :         INIT_LIST_HEAD(&ctlr->queue);
    2441                 :          0 :         spin_lock_init(&ctlr->queue_lock);
    2442                 :          0 :         spin_lock_init(&ctlr->bus_lock_spinlock);
    2443                 :          0 :         mutex_init(&ctlr->bus_lock_mutex);
    2444                 :          0 :         mutex_init(&ctlr->io_mutex);
    2445                 :          0 :         ctlr->bus_lock_flag = 0;
    2446                 :            :         init_completion(&ctlr->xfer_completion);
    2447                 :          0 :         if (!ctlr->max_dma_len)
    2448                 :          0 :                 ctlr->max_dma_len = INT_MAX;
    2449                 :            : 
    2450                 :            :         /* register the device, then userspace will see it.
    2451                 :            :          * registration fails if the bus ID is in use.
    2452                 :            :          */
    2453                 :          0 :         dev_set_name(&ctlr->dev, "spi%u", ctlr->bus_num);
    2454                 :            : 
    2455                 :          0 :         if (!spi_controller_is_slave(ctlr)) {
    2456                 :          0 :                 if (ctlr->use_gpio_descriptors) {
    2457                 :          0 :                         status = spi_get_gpio_descs(ctlr);
    2458                 :          0 :                         if (status)
    2459                 :            :                                 goto free_bus_id;
    2460                 :            :                         /*
    2461                 :            :                          * A controller using GPIO descriptors always
    2462                 :            :                          * supports SPI_CS_HIGH if need be.
    2463                 :            :                          */
    2464                 :          0 :                         ctlr->mode_bits |= SPI_CS_HIGH;
    2465                 :            :                 } else {
    2466                 :            :                         /* Legacy code path for GPIOs from DT */
    2467                 :          0 :                         status = of_spi_get_gpio_numbers(ctlr);
    2468                 :          0 :                         if (status)
    2469                 :            :                                 goto free_bus_id;
    2470                 :            :                 }
    2471                 :            :         }
    2472                 :            : 
    2473                 :            :         /*
    2474                 :            :          * Even if it's just one always-selected device, there must
    2475                 :            :          * be at least one chipselect.
    2476                 :            :          */
    2477                 :          0 :         if (!ctlr->num_chipselect) {
    2478                 :            :                 status = -EINVAL;
    2479                 :            :                 goto free_bus_id;
    2480                 :            :         }
    2481                 :            : 
    2482                 :          0 :         status = device_add(&ctlr->dev);
    2483                 :          0 :         if (status < 0)
    2484                 :            :                 goto free_bus_id;
    2485                 :            :         dev_dbg(dev, "registered %s %s\n",
    2486                 :            :                         spi_controller_is_slave(ctlr) ? "slave" : "master",
    2487                 :            :                         dev_name(&ctlr->dev));
    2488                 :            : 
    2489                 :            :         /*
    2490                 :            :          * If we're using a queued driver, start the queue. Note that we don't
    2491                 :            :          * need the queueing logic if the driver is only supporting high-level
    2492                 :            :          * memory operations.
    2493                 :            :          */
    2494                 :          0 :         if (ctlr->transfer) {
    2495                 :          0 :                 dev_info(dev, "controller is unqueued, this is deprecated\n");
    2496                 :          0 :         } else if (ctlr->transfer_one || ctlr->transfer_one_message) {
    2497                 :          0 :                 status = spi_controller_initialize_queue(ctlr);
    2498                 :          0 :                 if (status) {
    2499                 :          0 :                         device_del(&ctlr->dev);
    2500                 :          0 :                         goto free_bus_id;
    2501                 :            :                 }
    2502                 :            :         }
    2503                 :            :         /* add statistics */
    2504                 :          0 :         spin_lock_init(&ctlr->statistics.lock);
    2505                 :            : 
    2506                 :          0 :         mutex_lock(&board_lock);
    2507                 :          0 :         list_add_tail(&ctlr->list, &spi_controller_list);
    2508                 :          0 :         list_for_each_entry(bi, &board_list, list)
    2509                 :          0 :                 spi_match_controller_to_boardinfo(ctlr, &bi->board_info);
    2510                 :          0 :         mutex_unlock(&board_lock);
    2511                 :            : 
    2512                 :            :         /* Register devices from the device tree and ACPI */
    2513                 :          0 :         of_register_spi_devices(ctlr);
    2514                 :            :         acpi_register_spi_devices(ctlr);
    2515                 :          0 :         return status;
    2516                 :            : 
    2517                 :            : free_bus_id:
    2518                 :          0 :         mutex_lock(&board_lock);
    2519                 :          0 :         idr_remove(&spi_master_idr, ctlr->bus_num);
    2520                 :          0 :         mutex_unlock(&board_lock);
    2521                 :          0 :         return status;
    2522                 :            : }
    2523                 :            : EXPORT_SYMBOL_GPL(spi_register_controller);
    2524                 :            : 
    2525                 :          0 : static void devm_spi_unregister(struct device *dev, void *res)
    2526                 :            : {
    2527                 :          0 :         spi_unregister_controller(*(struct spi_controller **)res);
    2528                 :          0 : }
    2529                 :            : 
    2530                 :            : /**
    2531                 :            :  * devm_spi_register_controller - register managed SPI master or slave
    2532                 :            :  *      controller
    2533                 :            :  * @dev:    device managing SPI controller
    2534                 :            :  * @ctlr: initialized controller, originally from spi_alloc_master() or
    2535                 :            :  *      spi_alloc_slave()
    2536                 :            :  * Context: can sleep
    2537                 :            :  *
    2538                 :            :  * Register a SPI device as with spi_register_controller() which will
    2539                 :            :  * automatically be unregistered and freed.
    2540                 :            :  *
    2541                 :            :  * Return: zero on success, else a negative error code.
    2542                 :            :  */
    2543                 :          0 : int devm_spi_register_controller(struct device *dev,
    2544                 :            :                                  struct spi_controller *ctlr)
    2545                 :            : {
    2546                 :            :         struct spi_controller **ptr;
    2547                 :            :         int ret;
    2548                 :            : 
    2549                 :            :         ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL);
    2550                 :          0 :         if (!ptr)
    2551                 :            :                 return -ENOMEM;
    2552                 :            : 
    2553                 :          0 :         ret = spi_register_controller(ctlr);
    2554                 :          0 :         if (!ret) {
    2555                 :          0 :                 *ptr = ctlr;
    2556                 :          0 :                 devres_add(dev, ptr);
    2557                 :            :         } else {
    2558                 :          0 :                 devres_free(ptr);
    2559                 :            :         }
    2560                 :            : 
    2561                 :          0 :         return ret;
    2562                 :            : }
    2563                 :            : EXPORT_SYMBOL_GPL(devm_spi_register_controller);
    2564                 :            : 
    2565                 :          0 : static int __unregister(struct device *dev, void *null)
    2566                 :            : {
    2567                 :          0 :         spi_unregister_device(to_spi_device(dev));
    2568                 :          0 :         return 0;
    2569                 :            : }
    2570                 :            : 
    2571                 :            : /**
    2572                 :            :  * spi_unregister_controller - unregister SPI master or slave controller
    2573                 :            :  * @ctlr: the controller being unregistered
    2574                 :            :  * Context: can sleep
    2575                 :            :  *
    2576                 :            :  * This call is used only by SPI controller drivers, which are the
    2577                 :            :  * only ones directly touching chip registers.
    2578                 :            :  *
    2579                 :            :  * This must be called from context that can sleep.
    2580                 :            :  *
    2581                 :            :  * Note that this function also drops a reference to the controller.
    2582                 :            :  */
    2583                 :          0 : void spi_unregister_controller(struct spi_controller *ctlr)
    2584                 :            : {
    2585                 :            :         struct spi_controller *found;
    2586                 :          0 :         int id = ctlr->bus_num;
    2587                 :            : 
    2588                 :            :         /* Prevent addition of new devices, unregister existing ones */
    2589                 :            :         if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
    2590                 :          0 :                 mutex_lock(&spi_add_lock);
    2591                 :            : 
    2592                 :          0 :         device_for_each_child(&ctlr->dev, NULL, __unregister);
    2593                 :            : 
    2594                 :            :         /* First make sure that this controller was ever added */
    2595                 :          0 :         mutex_lock(&board_lock);
    2596                 :          0 :         found = idr_find(&spi_master_idr, id);
    2597                 :          0 :         mutex_unlock(&board_lock);
    2598                 :          0 :         if (ctlr->queued) {
    2599                 :          0 :                 if (spi_destroy_queue(ctlr))
    2600                 :          0 :                         dev_err(&ctlr->dev, "queue remove failed\n");
    2601                 :            :         }
    2602                 :          0 :         mutex_lock(&board_lock);
    2603                 :            :         list_del(&ctlr->list);
    2604                 :          0 :         mutex_unlock(&board_lock);
    2605                 :            : 
    2606                 :          0 :         device_unregister(&ctlr->dev);
    2607                 :            :         /* free bus id */
    2608                 :          0 :         mutex_lock(&board_lock);
    2609                 :          0 :         if (found == ctlr)
    2610                 :          0 :                 idr_remove(&spi_master_idr, id);
    2611                 :          0 :         mutex_unlock(&board_lock);
    2612                 :            : 
    2613                 :            :         if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
    2614                 :          0 :                 mutex_unlock(&spi_add_lock);
    2615                 :          0 : }
    2616                 :            : EXPORT_SYMBOL_GPL(spi_unregister_controller);
    2617                 :            : 
    2618                 :          0 : int spi_controller_suspend(struct spi_controller *ctlr)
    2619                 :            : {
    2620                 :            :         int ret;
    2621                 :            : 
    2622                 :            :         /* Basically no-ops for non-queued controllers */
    2623                 :          0 :         if (!ctlr->queued)
    2624                 :            :                 return 0;
    2625                 :            : 
    2626                 :          0 :         ret = spi_stop_queue(ctlr);
    2627                 :          0 :         if (ret)
    2628                 :          0 :                 dev_err(&ctlr->dev, "queue stop failed\n");
    2629                 :            : 
    2630                 :          0 :         return ret;
    2631                 :            : }
    2632                 :            : EXPORT_SYMBOL_GPL(spi_controller_suspend);
    2633                 :            : 
    2634                 :          0 : int spi_controller_resume(struct spi_controller *ctlr)
    2635                 :            : {
    2636                 :            :         int ret;
    2637                 :            : 
    2638                 :          0 :         if (!ctlr->queued)
    2639                 :            :                 return 0;
    2640                 :            : 
    2641                 :          0 :         ret = spi_start_queue(ctlr);
    2642                 :          0 :         if (ret)
    2643                 :          0 :                 dev_err(&ctlr->dev, "queue restart failed\n");
    2644                 :            : 
    2645                 :          0 :         return ret;
    2646                 :            : }
    2647                 :            : EXPORT_SYMBOL_GPL(spi_controller_resume);
    2648                 :            : 
    2649                 :          0 : static int __spi_controller_match(struct device *dev, const void *data)
    2650                 :            : {
    2651                 :            :         struct spi_controller *ctlr;
    2652                 :            :         const u16 *bus_num = data;
    2653                 :            : 
    2654                 :            :         ctlr = container_of(dev, struct spi_controller, dev);
    2655                 :          0 :         return ctlr->bus_num == *bus_num;
    2656                 :            : }
    2657                 :            : 
    2658                 :            : /**
    2659                 :            :  * spi_busnum_to_master - look up master associated with bus_num
    2660                 :            :  * @bus_num: the master's bus number
    2661                 :            :  * Context: can sleep
    2662                 :            :  *
    2663                 :            :  * This call may be used with devices that are registered after
    2664                 :            :  * arch init time.  It returns a refcounted pointer to the relevant
    2665                 :            :  * spi_controller (which the caller must release), or NULL if there is
    2666                 :            :  * no such master registered.
    2667                 :            :  *
    2668                 :            :  * Return: the SPI master structure on success, else NULL.
    2669                 :            :  */
    2670                 :          0 : struct spi_controller *spi_busnum_to_master(u16 bus_num)
    2671                 :            : {
    2672                 :            :         struct device           *dev;
    2673                 :            :         struct spi_controller   *ctlr = NULL;
    2674                 :            : 
    2675                 :          0 :         dev = class_find_device(&spi_master_class, NULL, &bus_num,
    2676                 :            :                                 __spi_controller_match);
    2677                 :          0 :         if (dev)
    2678                 :            :                 ctlr = container_of(dev, struct spi_controller, dev);
    2679                 :            :         /* reference got in class_find_device */
    2680                 :          0 :         return ctlr;
    2681                 :            : }
    2682                 :            : EXPORT_SYMBOL_GPL(spi_busnum_to_master);
    2683                 :            : 
    2684                 :            : /*-------------------------------------------------------------------------*/
    2685                 :            : 
    2686                 :            : /* Core methods for SPI resource management */
    2687                 :            : 
    2688                 :            : /**
    2689                 :            :  * spi_res_alloc - allocate a spi resource that is life-cycle managed
    2690                 :            :  *                 during the processing of a spi_message while using
    2691                 :            :  *                 spi_transfer_one
    2692                 :            :  * @spi:     the spi device for which we allocate memory
    2693                 :            :  * @release: the release code to execute for this resource
    2694                 :            :  * @size:    size to alloc and return
    2695                 :            :  * @gfp:     GFP allocation flags
    2696                 :            :  *
    2697                 :            :  * Return: the pointer to the allocated data
    2698                 :            :  *
    2699                 :            :  * This may get enhanced in the future to allocate from a memory pool
    2700                 :            :  * of the @spi_device or @spi_controller to avoid repeated allocations.
    2701                 :            :  */
    2702                 :          0 : void *spi_res_alloc(struct spi_device *spi,
    2703                 :            :                     spi_res_release_t release,
    2704                 :            :                     size_t size, gfp_t gfp)
    2705                 :            : {
    2706                 :            :         struct spi_res *sres;
    2707                 :            : 
    2708                 :          0 :         sres = kzalloc(sizeof(*sres) + size, gfp);
    2709                 :          0 :         if (!sres)
    2710                 :            :                 return NULL;
    2711                 :            : 
    2712                 :          0 :         INIT_LIST_HEAD(&sres->entry);
    2713                 :          0 :         sres->release = release;
    2714                 :            : 
    2715                 :          0 :         return sres->data;
    2716                 :            : }
    2717                 :            : EXPORT_SYMBOL_GPL(spi_res_alloc);
    2718                 :            : 
    2719                 :            : /**
    2720                 :            :  * spi_res_free - free an spi resource
    2721                 :            :  * @res: pointer to the custom data of a resource
    2722                 :            :  *
    2723                 :            :  */
    2724                 :          0 : void spi_res_free(void *res)
    2725                 :            : {
    2726                 :          0 :         struct spi_res *sres = container_of(res, struct spi_res, data);
    2727                 :            : 
    2728                 :          0 :         if (!res)
    2729                 :          0 :                 return;
    2730                 :            : 
    2731                 :          0 :         WARN_ON(!list_empty(&sres->entry));
    2732                 :          0 :         kfree(sres);
    2733                 :            : }
    2734                 :            : EXPORT_SYMBOL_GPL(spi_res_free);
    2735                 :            : 
    2736                 :            : /**
    2737                 :            :  * spi_res_add - add a spi_res to the spi_message
    2738                 :            :  * @message: the spi message
    2739                 :            :  * @res:     the spi_resource
    2740                 :            :  */
    2741                 :          0 : void spi_res_add(struct spi_message *message, void *res)
    2742                 :            : {
    2743                 :            :         struct spi_res *sres = container_of(res, struct spi_res, data);
    2744                 :            : 
    2745                 :          0 :         WARN_ON(!list_empty(&sres->entry));
    2746                 :          0 :         list_add_tail(&sres->entry, &message->resources);
    2747                 :          0 : }
    2748                 :            : EXPORT_SYMBOL_GPL(spi_res_add);
    2749                 :            : 
    2750                 :            : /**
    2751                 :            :  * spi_res_release - release all spi resources for this message
    2752                 :            :  * @ctlr:  the @spi_controller
    2753                 :            :  * @message: the @spi_message
    2754                 :            :  */
    2755                 :          0 : void spi_res_release(struct spi_controller *ctlr, struct spi_message *message)
    2756                 :            : {
    2757                 :            :         struct spi_res *res, *tmp;
    2758                 :            : 
    2759                 :          0 :         list_for_each_entry_safe_reverse(res, tmp, &message->resources, entry) {
    2760                 :          0 :                 if (res->release)
    2761                 :          0 :                         res->release(ctlr, message, res->data);
    2762                 :            : 
    2763                 :            :                 list_del(&res->entry);
    2764                 :            : 
    2765                 :          0 :                 kfree(res);
    2766                 :            :         }
    2767                 :          0 : }
    2768                 :            : EXPORT_SYMBOL_GPL(spi_res_release);
    2769                 :            : 
    2770                 :            : /*-------------------------------------------------------------------------*/
    2771                 :            : 
    2772                 :            : /* Core methods for spi_message alterations */
    2773                 :            : 
    2774                 :          0 : static void __spi_replace_transfers_release(struct spi_controller *ctlr,
    2775                 :            :                                             struct spi_message *msg,
    2776                 :            :                                             void *res)
    2777                 :            : {
    2778                 :            :         struct spi_replaced_transfers *rxfer = res;
    2779                 :            :         size_t i;
    2780                 :            : 
    2781                 :            :         /* call extra callback if requested */
    2782                 :          0 :         if (rxfer->release)
    2783                 :          0 :                 rxfer->release(ctlr, msg, res);
    2784                 :            : 
    2785                 :            :         /* insert replaced transfers back into the message */
    2786                 :          0 :         list_splice(&rxfer->replaced_transfers, rxfer->replaced_after);
    2787                 :            : 
    2788                 :            :         /* remove the formerly inserted entries */
    2789                 :          0 :         for (i = 0; i < rxfer->inserted; i++)
    2790                 :            :                 list_del(&rxfer->inserted_transfers[i].transfer_list);
    2791                 :          0 : }
    2792                 :            : 
    2793                 :            : /**
    2794                 :            :  * spi_replace_transfers - replace transfers with several transfers
    2795                 :            :  *                         and register change with spi_message.resources
    2796                 :            :  * @msg:           the spi_message we work upon
    2797                 :            :  * @xfer_first:    the first spi_transfer we want to replace
    2798                 :            :  * @remove:        number of transfers to remove
    2799                 :            :  * @insert:        the number of transfers we want to insert instead
    2800                 :            :  * @release:       extra release code necessary in some circumstances
    2801                 :            :  * @extradatasize: extra data to allocate (with alignment guarantees
    2802                 :            :  *                 of struct @spi_transfer)
    2803                 :            :  * @gfp:           gfp flags
    2804                 :            :  *
    2805                 :            :  * Returns: pointer to @spi_replaced_transfers,
    2806                 :            :  *          PTR_ERR(...) in case of errors.
    2807                 :            :  */
    2808                 :          0 : struct spi_replaced_transfers *spi_replace_transfers(
    2809                 :            :         struct spi_message *msg,
    2810                 :            :         struct spi_transfer *xfer_first,
    2811                 :            :         size_t remove,
    2812                 :            :         size_t insert,
    2813                 :            :         spi_replaced_release_t release,
    2814                 :            :         size_t extradatasize,
    2815                 :            :         gfp_t gfp)
    2816                 :            : {
    2817                 :            :         struct spi_replaced_transfers *rxfer;
    2818                 :            :         struct spi_transfer *xfer;
    2819                 :            :         size_t i;
    2820                 :            : 
    2821                 :            :         /* allocate the structure using spi_res */
    2822                 :          0 :         rxfer = spi_res_alloc(msg->spi, __spi_replace_transfers_release,
    2823                 :          0 :                               struct_size(rxfer, inserted_transfers, insert)
    2824                 :            :                               + extradatasize,
    2825                 :            :                               gfp);
    2826                 :          0 :         if (!rxfer)
    2827                 :            :                 return ERR_PTR(-ENOMEM);
    2828                 :            : 
    2829                 :            :         /* the release code to invoke before running the generic release */
    2830                 :          0 :         rxfer->release = release;
    2831                 :            : 
    2832                 :            :         /* assign extradata */
    2833                 :          0 :         if (extradatasize)
    2834                 :          0 :                 rxfer->extradata =
    2835                 :          0 :                         &rxfer->inserted_transfers[insert];
    2836                 :            : 
    2837                 :            :         /* init the replaced_transfers list */
    2838                 :          0 :         INIT_LIST_HEAD(&rxfer->replaced_transfers);
    2839                 :            : 
    2840                 :            :         /* assign the list_entry after which we should reinsert
    2841                 :            :          * the @replaced_transfers - it may be spi_message.messages!
    2842                 :            :          */
    2843                 :          0 :         rxfer->replaced_after = xfer_first->transfer_list.prev;
    2844                 :            : 
    2845                 :            :         /* remove the requested number of transfers */
    2846                 :          0 :         for (i = 0; i < remove; i++) {
    2847                 :            :                 /* if the entry after replaced_after it is msg->transfers
    2848                 :            :                  * then we have been requested to remove more transfers
    2849                 :            :                  * than are in the list
    2850                 :            :                  */
    2851                 :          0 :                 if (rxfer->replaced_after->next == &msg->transfers) {
    2852                 :          0 :                         dev_err(&msg->spi->dev,
    2853                 :            :                                 "requested to remove more spi_transfers than are available\n");
    2854                 :            :                         /* insert replaced transfers back into the message */
    2855                 :          0 :                         list_splice(&rxfer->replaced_transfers,
    2856                 :            :                                     rxfer->replaced_after);
    2857                 :            : 
    2858                 :            :                         /* free the spi_replace_transfer structure */
    2859                 :          0 :                         spi_res_free(rxfer);
    2860                 :            : 
    2861                 :            :                         /* and return with an error */
    2862                 :          0 :                         return ERR_PTR(-EINVAL);
    2863                 :            :                 }
    2864                 :            : 
    2865                 :            :                 /* remove the entry after replaced_after from list of
    2866                 :            :                  * transfers and add it to list of replaced_transfers
    2867                 :            :                  */
    2868                 :            :                 list_move_tail(rxfer->replaced_after->next,
    2869                 :            :                                &rxfer->replaced_transfers);
    2870                 :            :         }
    2871                 :            : 
    2872                 :            :         /* create copy of the given xfer with identical settings
    2873                 :            :          * based on the first transfer to get removed
    2874                 :            :          */
    2875                 :          0 :         for (i = 0; i < insert; i++) {
    2876                 :            :                 /* we need to run in reverse order */
    2877                 :          0 :                 xfer = &rxfer->inserted_transfers[insert - 1 - i];
    2878                 :            : 
    2879                 :            :                 /* copy all spi_transfer data */
    2880                 :          0 :                 memcpy(xfer, xfer_first, sizeof(*xfer));
    2881                 :            : 
    2882                 :            :                 /* add to list */
    2883                 :          0 :                 list_add(&xfer->transfer_list, rxfer->replaced_after);
    2884                 :            : 
    2885                 :            :                 /* clear cs_change and delay_usecs for all but the last */
    2886                 :          0 :                 if (i) {
    2887                 :          0 :                         xfer->cs_change = false;
    2888                 :          0 :                         xfer->delay_usecs = 0;
    2889                 :            :                 }
    2890                 :            :         }
    2891                 :            : 
    2892                 :            :         /* set up inserted */
    2893                 :          0 :         rxfer->inserted = insert;
    2894                 :            : 
    2895                 :            :         /* and register it with spi_res/spi_message */
    2896                 :          0 :         spi_res_add(msg, rxfer);
    2897                 :            : 
    2898                 :          0 :         return rxfer;
    2899                 :            : }
    2900                 :            : EXPORT_SYMBOL_GPL(spi_replace_transfers);
    2901                 :            : 
    2902                 :          0 : static int __spi_split_transfer_maxsize(struct spi_controller *ctlr,
    2903                 :            :                                         struct spi_message *msg,
    2904                 :            :                                         struct spi_transfer **xferp,
    2905                 :            :                                         size_t maxsize,
    2906                 :            :                                         gfp_t gfp)
    2907                 :            : {
    2908                 :          0 :         struct spi_transfer *xfer = *xferp, *xfers;
    2909                 :            :         struct spi_replaced_transfers *srt;
    2910                 :            :         size_t offset;
    2911                 :            :         size_t count, i;
    2912                 :            : 
    2913                 :            :         /* calculate how many we have to replace */
    2914                 :          0 :         count = DIV_ROUND_UP(xfer->len, maxsize);
    2915                 :            : 
    2916                 :            :         /* create replacement */
    2917                 :          0 :         srt = spi_replace_transfers(msg, xfer, 1, count, NULL, 0, gfp);
    2918                 :          0 :         if (IS_ERR(srt))
    2919                 :          0 :                 return PTR_ERR(srt);
    2920                 :          0 :         xfers = srt->inserted_transfers;
    2921                 :            : 
    2922                 :            :         /* now handle each of those newly inserted spi_transfers
    2923                 :            :          * note that the replacements spi_transfers all are preset
    2924                 :            :          * to the same values as *xferp, so tx_buf, rx_buf and len
    2925                 :            :          * are all identical (as well as most others)
    2926                 :            :          * so we just have to fix up len and the pointers.
    2927                 :            :          *
    2928                 :            :          * this also includes support for the depreciated
    2929                 :            :          * spi_message.is_dma_mapped interface
    2930                 :            :          */
    2931                 :            : 
    2932                 :            :         /* the first transfer just needs the length modified, so we
    2933                 :            :          * run it outside the loop
    2934                 :            :          */
    2935                 :          0 :         xfers[0].len = min_t(size_t, maxsize, xfer[0].len);
    2936                 :            : 
    2937                 :            :         /* all the others need rx_buf/tx_buf also set */
    2938                 :          0 :         for (i = 1, offset = maxsize; i < count; offset += maxsize, i++) {
    2939                 :            :                 /* update rx_buf, tx_buf and dma */
    2940                 :          0 :                 if (xfers[i].rx_buf)
    2941                 :          0 :                         xfers[i].rx_buf += offset;
    2942                 :          0 :                 if (xfers[i].rx_dma)
    2943                 :          0 :                         xfers[i].rx_dma += offset;
    2944                 :          0 :                 if (xfers[i].tx_buf)
    2945                 :          0 :                         xfers[i].tx_buf += offset;
    2946                 :          0 :                 if (xfers[i].tx_dma)
    2947                 :          0 :                         xfers[i].tx_dma += offset;
    2948                 :            : 
    2949                 :            :                 /* update length */
    2950                 :          0 :                 xfers[i].len = min(maxsize, xfers[i].len - offset);
    2951                 :            :         }
    2952                 :            : 
    2953                 :            :         /* we set up xferp to the last entry we have inserted,
    2954                 :            :          * so that we skip those already split transfers
    2955                 :            :          */
    2956                 :          0 :         *xferp = &xfers[count - 1];
    2957                 :            : 
    2958                 :            :         /* increment statistics counters */
    2959                 :          0 :         SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
    2960                 :            :                                        transfers_split_maxsize);
    2961                 :          0 :         SPI_STATISTICS_INCREMENT_FIELD(&msg->spi->statistics,
    2962                 :            :                                        transfers_split_maxsize);
    2963                 :            : 
    2964                 :          0 :         return 0;
    2965                 :            : }
    2966                 :            : 
    2967                 :            : /**
    2968                 :            :  * spi_split_tranfers_maxsize - split spi transfers into multiple transfers
    2969                 :            :  *                              when an individual transfer exceeds a
    2970                 :            :  *                              certain size
    2971                 :            :  * @ctlr:    the @spi_controller for this transfer
    2972                 :            :  * @msg:   the @spi_message to transform
    2973                 :            :  * @maxsize:  the maximum when to apply this
    2974                 :            :  * @gfp: GFP allocation flags
    2975                 :            :  *
    2976                 :            :  * Return: status of transformation
    2977                 :            :  */
    2978                 :          0 : int spi_split_transfers_maxsize(struct spi_controller *ctlr,
    2979                 :            :                                 struct spi_message *msg,
    2980                 :            :                                 size_t maxsize,
    2981                 :            :                                 gfp_t gfp)
    2982                 :            : {
    2983                 :            :         struct spi_transfer *xfer;
    2984                 :            :         int ret;
    2985                 :            : 
    2986                 :            :         /* iterate over the transfer_list,
    2987                 :            :          * but note that xfer is advanced to the last transfer inserted
    2988                 :            :          * to avoid checking sizes again unnecessarily (also xfer does
    2989                 :            :          * potentiall belong to a different list by the time the
    2990                 :            :          * replacement has happened
    2991                 :            :          */
    2992                 :          0 :         list_for_each_entry(xfer, &msg->transfers, transfer_list) {
    2993                 :          0 :                 if (xfer->len > maxsize) {
    2994                 :          0 :                         ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
    2995                 :            :                                                            maxsize, gfp);
    2996                 :          0 :                         if (ret)
    2997                 :          0 :                                 return ret;
    2998                 :            :                 }
    2999                 :            :         }
    3000                 :            : 
    3001                 :            :         return 0;
    3002                 :            : }
    3003                 :            : EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
    3004                 :            : 
    3005                 :            : /*-------------------------------------------------------------------------*/
    3006                 :            : 
    3007                 :            : /* Core methods for SPI controller protocol drivers.  Some of the
    3008                 :            :  * other core methods are currently defined as inline functions.
    3009                 :            :  */
    3010                 :            : 
    3011                 :            : static int __spi_validate_bits_per_word(struct spi_controller *ctlr,
    3012                 :            :                                         u8 bits_per_word)
    3013                 :            : {
    3014                 :          0 :         if (ctlr->bits_per_word_mask) {
    3015                 :            :                 /* Only 32 bits fit in the mask */
    3016                 :          0 :                 if (bits_per_word > 32)
    3017                 :            :                         return -EINVAL;
    3018                 :          0 :                 if (!(ctlr->bits_per_word_mask & SPI_BPW_MASK(bits_per_word)))
    3019                 :            :                         return -EINVAL;
    3020                 :            :         }
    3021                 :            : 
    3022                 :            :         return 0;
    3023                 :            : }
    3024                 :            : 
    3025                 :            : /**
    3026                 :            :  * spi_setup - setup SPI mode and clock rate
    3027                 :            :  * @spi: the device whose settings are being modified
    3028                 :            :  * Context: can sleep, and no requests are queued to the device
    3029                 :            :  *
    3030                 :            :  * SPI protocol drivers may need to update the transfer mode if the
    3031                 :            :  * device doesn't work with its default.  They may likewise need
    3032                 :            :  * to update clock rates or word sizes from initial values.  This function
    3033                 :            :  * changes those settings, and must be called from a context that can sleep.
    3034                 :            :  * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
    3035                 :            :  * effect the next time the device is selected and data is transferred to
    3036                 :            :  * or from it.  When this function returns, the spi device is deselected.
    3037                 :            :  *
    3038                 :            :  * Note that this call will fail if the protocol driver specifies an option
    3039                 :            :  * that the underlying controller or its driver does not support.  For
    3040                 :            :  * example, not all hardware supports wire transfers using nine bit words,
    3041                 :            :  * LSB-first wire encoding, or active-high chipselects.
    3042                 :            :  *
    3043                 :            :  * Return: zero on success, else a negative error code.
    3044                 :            :  */
    3045                 :          0 : int spi_setup(struct spi_device *spi)
    3046                 :            : {
    3047                 :          0 :         struct spi_controller *ctlr = spi->controller;
    3048                 :            :         unsigned        bad_bits, ugly_bits;
    3049                 :            :         int             status;
    3050                 :            : 
    3051                 :            :         /* check mode to prevent that DUAL and QUAD set at the same time
    3052                 :            :          */
    3053                 :          0 :         if (((spi->mode & SPI_TX_DUAL) && (spi->mode & SPI_TX_QUAD)) ||
    3054                 :          0 :                 ((spi->mode & SPI_RX_DUAL) && (spi->mode & SPI_RX_QUAD))) {
    3055                 :          0 :                 dev_err(&spi->dev,
    3056                 :            :                 "setup: can not select dual and quad at the same time\n");
    3057                 :          0 :                 return -EINVAL;
    3058                 :            :         }
    3059                 :            :         /* if it is SPI_3WIRE mode, DUAL and QUAD should be forbidden
    3060                 :            :          */
    3061                 :          0 :         if ((spi->mode & SPI_3WIRE) && (spi->mode &
    3062                 :            :                 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
    3063                 :            :                  SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL)))
    3064                 :            :                 return -EINVAL;
    3065                 :            : 
    3066                 :          0 :         if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
    3067                 :          0 :             ctlr->cs_gpiods[spi->chip_select] && !(spi->mode & SPI_CS_HIGH)) {
    3068                 :            :                 dev_dbg(&spi->dev,
    3069                 :            :                         "setup: forcing CS_HIGH (use_gpio_descriptors)\n");
    3070                 :          0 :                 spi->mode |= SPI_CS_HIGH;
    3071                 :            :         }
    3072                 :            : 
    3073                 :            :         /* help drivers fail *cleanly* when they need options
    3074                 :            :          * that aren't supported with their current controller
    3075                 :            :          * SPI_CS_WORD has a fallback software implementation,
    3076                 :            :          * so it is ignored here.
    3077                 :            :          */
    3078                 :          0 :         bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD);
    3079                 :            :         /* nothing prevents from working with active-high CS in case if it
    3080                 :            :          * is driven by GPIO.
    3081                 :            :          */
    3082                 :          0 :         if (gpio_is_valid(spi->cs_gpio))
    3083                 :          0 :                 bad_bits &= ~SPI_CS_HIGH;
    3084                 :          0 :         ugly_bits = bad_bits &
    3085                 :            :                     (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL |
    3086                 :            :                      SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL);
    3087                 :          0 :         if (ugly_bits) {
    3088                 :          0 :                 dev_warn(&spi->dev,
    3089                 :            :                          "setup: ignoring unsupported mode bits %x\n",
    3090                 :            :                          ugly_bits);
    3091                 :          0 :                 spi->mode &= ~ugly_bits;
    3092                 :          0 :                 bad_bits &= ~ugly_bits;
    3093                 :            :         }
    3094                 :          0 :         if (bad_bits) {
    3095                 :          0 :                 dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
    3096                 :            :                         bad_bits);
    3097                 :          0 :                 return -EINVAL;
    3098                 :            :         }
    3099                 :            : 
    3100                 :          0 :         if (!spi->bits_per_word)
    3101                 :          0 :                 spi->bits_per_word = 8;
    3102                 :            : 
    3103                 :          0 :         status = __spi_validate_bits_per_word(spi->controller,
    3104                 :            :                                               spi->bits_per_word);
    3105                 :          0 :         if (status)
    3106                 :            :                 return status;
    3107                 :            : 
    3108                 :          0 :         if (!spi->max_speed_hz)
    3109                 :          0 :                 spi->max_speed_hz = spi->controller->max_speed_hz;
    3110                 :            : 
    3111                 :          0 :         if (spi->controller->setup)
    3112                 :          0 :                 status = spi->controller->setup(spi);
    3113                 :            : 
    3114                 :          0 :         spi_set_cs(spi, false);
    3115                 :            : 
    3116                 :          0 :         if (spi->rt && !spi->controller->rt) {
    3117                 :          0 :                 spi->controller->rt = true;
    3118                 :          0 :                 spi_set_thread_rt(spi->controller);
    3119                 :            :         }
    3120                 :            : 
    3121                 :            :         dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s%u bits/w, %u Hz max --> %d\n",
    3122                 :            :                         (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
    3123                 :            :                         (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
    3124                 :            :                         (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
    3125                 :            :                         (spi->mode & SPI_3WIRE) ? "3wire, " : "",
    3126                 :            :                         (spi->mode & SPI_LOOP) ? "loopback, " : "",
    3127                 :            :                         spi->bits_per_word, spi->max_speed_hz,
    3128                 :            :                         status);
    3129                 :            : 
    3130                 :          0 :         return status;
    3131                 :            : }
    3132                 :            : EXPORT_SYMBOL_GPL(spi_setup);
    3133                 :            : 
    3134                 :            : /**
    3135                 :            :  * spi_set_cs_timing - configure CS setup, hold, and inactive delays
    3136                 :            :  * @spi: the device that requires specific CS timing configuration
    3137                 :            :  * @setup: CS setup time in terms of clock count
    3138                 :            :  * @hold: CS hold time in terms of clock count
    3139                 :            :  * @inactive_dly: CS inactive delay between transfers in terms of clock count
    3140                 :            :  */
    3141                 :          0 : void spi_set_cs_timing(struct spi_device *spi, u8 setup, u8 hold,
    3142                 :            :                        u8 inactive_dly)
    3143                 :            : {
    3144                 :          0 :         if (spi->controller->set_cs_timing)
    3145                 :          0 :                 spi->controller->set_cs_timing(spi, setup, hold, inactive_dly);
    3146                 :          0 : }
    3147                 :            : EXPORT_SYMBOL_GPL(spi_set_cs_timing);
    3148                 :            : 
    3149                 :          0 : static int __spi_validate(struct spi_device *spi, struct spi_message *message)
    3150                 :            : {
    3151                 :          0 :         struct spi_controller *ctlr = spi->controller;
    3152                 :            :         struct spi_transfer *xfer;
    3153                 :            :         int w_size;
    3154                 :            : 
    3155                 :          0 :         if (list_empty(&message->transfers))
    3156                 :            :                 return -EINVAL;
    3157                 :            : 
    3158                 :            :         /* If an SPI controller does not support toggling the CS line on each
    3159                 :            :          * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO
    3160                 :            :          * for the CS line, we can emulate the CS-per-word hardware function by
    3161                 :            :          * splitting transfers into one-word transfers and ensuring that
    3162                 :            :          * cs_change is set for each transfer.
    3163                 :            :          */
    3164                 :          0 :         if ((spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) ||
    3165                 :          0 :                                           spi->cs_gpiod ||
    3166                 :          0 :                                           gpio_is_valid(spi->cs_gpio))) {
    3167                 :            :                 size_t maxsize;
    3168                 :            :                 int ret;
    3169                 :            : 
    3170                 :          0 :                 maxsize = (spi->bits_per_word + 7) / 8;
    3171                 :            : 
    3172                 :            :                 /* spi_split_transfers_maxsize() requires message->spi */
    3173                 :          0 :                 message->spi = spi;
    3174                 :            : 
    3175                 :          0 :                 ret = spi_split_transfers_maxsize(ctlr, message, maxsize,
    3176                 :            :                                                   GFP_KERNEL);
    3177                 :          0 :                 if (ret)
    3178                 :            :                         return ret;
    3179                 :            : 
    3180                 :          0 :                 list_for_each_entry(xfer, &message->transfers, transfer_list) {
    3181                 :            :                         /* don't change cs_change on the last entry in the list */
    3182                 :          0 :                         if (list_is_last(&xfer->transfer_list, &message->transfers))
    3183                 :            :                                 break;
    3184                 :          0 :                         xfer->cs_change = 1;
    3185                 :            :                 }
    3186                 :            :         }
    3187                 :            : 
    3188                 :            :         /* Half-duplex links include original MicroWire, and ones with
    3189                 :            :          * only one data pin like SPI_3WIRE (switches direction) or where
    3190                 :            :          * either MOSI or MISO is missing.  They can also be caused by
    3191                 :            :          * software limitations.
    3192                 :            :          */
    3193                 :          0 :         if ((ctlr->flags & SPI_CONTROLLER_HALF_DUPLEX) ||
    3194                 :          0 :             (spi->mode & SPI_3WIRE)) {
    3195                 :            :                 unsigned flags = ctlr->flags;
    3196                 :            : 
    3197                 :          0 :                 list_for_each_entry(xfer, &message->transfers, transfer_list) {
    3198                 :          0 :                         if (xfer->rx_buf && xfer->tx_buf)
    3199                 :            :                                 return -EINVAL;
    3200                 :          0 :                         if ((flags & SPI_CONTROLLER_NO_TX) && xfer->tx_buf)
    3201                 :            :                                 return -EINVAL;
    3202                 :          0 :                         if ((flags & SPI_CONTROLLER_NO_RX) && xfer->rx_buf)
    3203                 :            :                                 return -EINVAL;
    3204                 :            :                 }
    3205                 :            :         }
    3206                 :            : 
    3207                 :            :         /**
    3208                 :            :          * Set transfer bits_per_word and max speed as spi device default if
    3209                 :            :          * it is not set for this transfer.
    3210                 :            :          * Set transfer tx_nbits and rx_nbits as single transfer default
    3211                 :            :          * (SPI_NBITS_SINGLE) if it is not set for this transfer.
    3212                 :            :          * Ensure transfer word_delay is at least as long as that required by
    3213                 :            :          * device itself.
    3214                 :            :          */
    3215                 :          0 :         message->frame_length = 0;
    3216                 :          0 :         list_for_each_entry(xfer, &message->transfers, transfer_list) {
    3217                 :          0 :                 xfer->effective_speed_hz = 0;
    3218                 :          0 :                 message->frame_length += xfer->len;
    3219                 :          0 :                 if (!xfer->bits_per_word)
    3220                 :          0 :                         xfer->bits_per_word = spi->bits_per_word;
    3221                 :            : 
    3222                 :          0 :                 if (!xfer->speed_hz)
    3223                 :          0 :                         xfer->speed_hz = spi->max_speed_hz;
    3224                 :            : 
    3225                 :          0 :                 if (ctlr->max_speed_hz && xfer->speed_hz > ctlr->max_speed_hz)
    3226                 :          0 :                         xfer->speed_hz = ctlr->max_speed_hz;
    3227                 :            : 
    3228                 :          0 :                 if (__spi_validate_bits_per_word(ctlr, xfer->bits_per_word))
    3229                 :            :                         return -EINVAL;
    3230                 :            : 
    3231                 :            :                 /*
    3232                 :            :                  * SPI transfer length should be multiple of SPI word size
    3233                 :            :                  * where SPI word size should be power-of-two multiple
    3234                 :            :                  */
    3235                 :          0 :                 if (xfer->bits_per_word <= 8)
    3236                 :            :                         w_size = 1;
    3237                 :          0 :                 else if (xfer->bits_per_word <= 16)
    3238                 :            :                         w_size = 2;
    3239                 :            :                 else
    3240                 :            :                         w_size = 4;
    3241                 :            : 
    3242                 :            :                 /* No partial transfers accepted */
    3243                 :          0 :                 if (xfer->len % w_size)
    3244                 :            :                         return -EINVAL;
    3245                 :            : 
    3246                 :          0 :                 if (xfer->speed_hz && ctlr->min_speed_hz &&
    3247                 :            :                     xfer->speed_hz < ctlr->min_speed_hz)
    3248                 :            :                         return -EINVAL;
    3249                 :            : 
    3250                 :          0 :                 if (xfer->tx_buf && !xfer->tx_nbits)
    3251                 :          0 :                         xfer->tx_nbits = SPI_NBITS_SINGLE;
    3252                 :          0 :                 if (xfer->rx_buf && !xfer->rx_nbits)
    3253                 :          0 :                         xfer->rx_nbits = SPI_NBITS_SINGLE;
    3254                 :            :                 /* check transfer tx/rx_nbits:
    3255                 :            :                  * 1. check the value matches one of single, dual and quad
    3256                 :            :                  * 2. check tx/rx_nbits match the mode in spi_device
    3257                 :            :                  */
    3258                 :          0 :                 if (xfer->tx_buf) {
    3259                 :          0 :                         if (xfer->tx_nbits != SPI_NBITS_SINGLE &&
    3260                 :          0 :                                 xfer->tx_nbits != SPI_NBITS_DUAL &&
    3261                 :            :                                 xfer->tx_nbits != SPI_NBITS_QUAD)
    3262                 :            :                                 return -EINVAL;
    3263                 :          0 :                         if ((xfer->tx_nbits == SPI_NBITS_DUAL) &&
    3264                 :          0 :                                 !(spi->mode & (SPI_TX_DUAL | SPI_TX_QUAD)))
    3265                 :            :                                 return -EINVAL;
    3266                 :          0 :                         if ((xfer->tx_nbits == SPI_NBITS_QUAD) &&
    3267                 :          0 :                                 !(spi->mode & SPI_TX_QUAD))
    3268                 :            :                                 return -EINVAL;
    3269                 :            :                 }
    3270                 :            :                 /* check transfer rx_nbits */
    3271                 :          0 :                 if (xfer->rx_buf) {
    3272                 :          0 :                         if (xfer->rx_nbits != SPI_NBITS_SINGLE &&
    3273                 :          0 :                                 xfer->rx_nbits != SPI_NBITS_DUAL &&
    3274                 :            :                                 xfer->rx_nbits != SPI_NBITS_QUAD)
    3275                 :            :                                 return -EINVAL;
    3276                 :          0 :                         if ((xfer->rx_nbits == SPI_NBITS_DUAL) &&
    3277                 :          0 :                                 !(spi->mode & (SPI_RX_DUAL | SPI_RX_QUAD)))
    3278                 :            :                                 return -EINVAL;
    3279                 :          0 :                         if ((xfer->rx_nbits == SPI_NBITS_QUAD) &&
    3280                 :          0 :                                 !(spi->mode & SPI_RX_QUAD))
    3281                 :            :                                 return -EINVAL;
    3282                 :            :                 }
    3283                 :            : 
    3284                 :          0 :                 if (xfer->word_delay_usecs < spi->word_delay_usecs)
    3285                 :          0 :                         xfer->word_delay_usecs = spi->word_delay_usecs;
    3286                 :            :         }
    3287                 :            : 
    3288                 :          0 :         message->status = -EINPROGRESS;
    3289                 :            : 
    3290                 :          0 :         return 0;
    3291                 :            : }
    3292                 :            : 
    3293                 :          0 : static int __spi_async(struct spi_device *spi, struct spi_message *message)
    3294                 :            : {
    3295                 :          0 :         struct spi_controller *ctlr = spi->controller;
    3296                 :            : 
    3297                 :            :         /*
    3298                 :            :          * Some controllers do not support doing regular SPI transfers. Return
    3299                 :            :          * ENOTSUPP when this is the case.
    3300                 :            :          */
    3301                 :          0 :         if (!ctlr->transfer)
    3302                 :            :                 return -ENOTSUPP;
    3303                 :            : 
    3304                 :          0 :         message->spi = spi;
    3305                 :            : 
    3306                 :          0 :         SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_async);
    3307                 :          0 :         SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_async);
    3308                 :            : 
    3309                 :          0 :         trace_spi_message_submit(message);
    3310                 :            : 
    3311                 :          0 :         return ctlr->transfer(spi, message);
    3312                 :            : }
    3313                 :            : 
    3314                 :            : /**
    3315                 :            :  * spi_async - asynchronous SPI transfer
    3316                 :            :  * @spi: device with which data will be exchanged
    3317                 :            :  * @message: describes the data transfers, including completion callback
    3318                 :            :  * Context: any (irqs may be blocked, etc)
    3319                 :            :  *
    3320                 :            :  * This call may be used in_irq and other contexts which can't sleep,
    3321                 :            :  * as well as from task contexts which can sleep.
    3322                 :            :  *
    3323                 :            :  * The completion callback is invoked in a context which can't sleep.
    3324                 :            :  * Before that invocation, the value of message->status is undefined.
    3325                 :            :  * When the callback is issued, message->status holds either zero (to
    3326                 :            :  * indicate complete success) or a negative error code.  After that
    3327                 :            :  * callback returns, the driver which issued the transfer request may
    3328                 :            :  * deallocate the associated memory; it's no longer in use by any SPI
    3329                 :            :  * core or controller driver code.
    3330                 :            :  *
    3331                 :            :  * Note that although all messages to a spi_device are handled in
    3332                 :            :  * FIFO order, messages may go to different devices in other orders.
    3333                 :            :  * Some device might be higher priority, or have various "hard" access
    3334                 :            :  * time requirements, for example.
    3335                 :            :  *
    3336                 :            :  * On detection of any fault during the transfer, processing of
    3337                 :            :  * the entire message is aborted, and the device is deselected.
    3338                 :            :  * Until returning from the associated message completion callback,
    3339                 :            :  * no other spi_message queued to that device will be processed.
    3340                 :            :  * (This rule applies equally to all the synchronous transfer calls,
    3341                 :            :  * which are wrappers around this core asynchronous primitive.)
    3342                 :            :  *
    3343                 :            :  * Return: zero on success, else a negative error code.
    3344                 :            :  */
    3345                 :          0 : int spi_async(struct spi_device *spi, struct spi_message *message)
    3346                 :            : {
    3347                 :          0 :         struct spi_controller *ctlr = spi->controller;
    3348                 :            :         int ret;
    3349                 :            :         unsigned long flags;
    3350                 :            : 
    3351                 :          0 :         ret = __spi_validate(spi, message);
    3352                 :          0 :         if (ret != 0)
    3353                 :            :                 return ret;
    3354                 :            : 
    3355                 :          0 :         spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
    3356                 :            : 
    3357                 :          0 :         if (ctlr->bus_lock_flag)
    3358                 :            :                 ret = -EBUSY;
    3359                 :            :         else
    3360                 :          0 :                 ret = __spi_async(spi, message);
    3361                 :            : 
    3362                 :            :         spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
    3363                 :            : 
    3364                 :          0 :         return ret;
    3365                 :            : }
    3366                 :            : EXPORT_SYMBOL_GPL(spi_async);
    3367                 :            : 
    3368                 :            : /**
    3369                 :            :  * spi_async_locked - version of spi_async with exclusive bus usage
    3370                 :            :  * @spi: device with which data will be exchanged
    3371                 :            :  * @message: describes the data transfers, including completion callback
    3372                 :            :  * Context: any (irqs may be blocked, etc)
    3373                 :            :  *
    3374                 :            :  * This call may be used in_irq and other contexts which can't sleep,
    3375                 :            :  * as well as from task contexts which can sleep.
    3376                 :            :  *
    3377                 :            :  * The completion callback is invoked in a context which can't sleep.
    3378                 :            :  * Before that invocation, the value of message->status is undefined.
    3379                 :            :  * When the callback is issued, message->status holds either zero (to
    3380                 :            :  * indicate complete success) or a negative error code.  After that
    3381                 :            :  * callback returns, the driver which issued the transfer request may
    3382                 :            :  * deallocate the associated memory; it's no longer in use by any SPI
    3383                 :            :  * core or controller driver code.
    3384                 :            :  *
    3385                 :            :  * Note that although all messages to a spi_device are handled in
    3386                 :            :  * FIFO order, messages may go to different devices in other orders.
    3387                 :            :  * Some device might be higher priority, or have various "hard" access
    3388                 :            :  * time requirements, for example.
    3389                 :            :  *
    3390                 :            :  * On detection of any fault during the transfer, processing of
    3391                 :            :  * the entire message is aborted, and the device is deselected.
    3392                 :            :  * Until returning from the associated message completion callback,
    3393                 :            :  * no other spi_message queued to that device will be processed.
    3394                 :            :  * (This rule applies equally to all the synchronous transfer calls,
    3395                 :            :  * which are wrappers around this core asynchronous primitive.)
    3396                 :            :  *
    3397                 :            :  * Return: zero on success, else a negative error code.
    3398                 :            :  */
    3399                 :          0 : int spi_async_locked(struct spi_device *spi, struct spi_message *message)
    3400                 :            : {
    3401                 :          0 :         struct spi_controller *ctlr = spi->controller;
    3402                 :            :         int ret;
    3403                 :            :         unsigned long flags;
    3404                 :            : 
    3405                 :          0 :         ret = __spi_validate(spi, message);
    3406                 :          0 :         if (ret != 0)
    3407                 :            :                 return ret;
    3408                 :            : 
    3409                 :          0 :         spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
    3410                 :            : 
    3411                 :          0 :         ret = __spi_async(spi, message);
    3412                 :            : 
    3413                 :            :         spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
    3414                 :            : 
    3415                 :          0 :         return ret;
    3416                 :            : 
    3417                 :            : }
    3418                 :            : EXPORT_SYMBOL_GPL(spi_async_locked);
    3419                 :            : 
    3420                 :            : /*-------------------------------------------------------------------------*/
    3421                 :            : 
    3422                 :            : /* Utility methods for SPI protocol drivers, layered on
    3423                 :            :  * top of the core.  Some other utility methods are defined as
    3424                 :            :  * inline functions.
    3425                 :            :  */
    3426                 :            : 
    3427                 :          0 : static void spi_complete(void *arg)
    3428                 :            : {
    3429                 :          0 :         complete(arg);
    3430                 :          0 : }
    3431                 :            : 
    3432                 :          0 : static int __spi_sync(struct spi_device *spi, struct spi_message *message)
    3433                 :            : {
    3434                 :          0 :         DECLARE_COMPLETION_ONSTACK(done);
    3435                 :            :         int status;
    3436                 :          0 :         struct spi_controller *ctlr = spi->controller;
    3437                 :            :         unsigned long flags;
    3438                 :            : 
    3439                 :          0 :         status = __spi_validate(spi, message);
    3440                 :          0 :         if (status != 0)
    3441                 :            :                 return status;
    3442                 :            : 
    3443                 :          0 :         message->complete = spi_complete;
    3444                 :          0 :         message->context = &done;
    3445                 :          0 :         message->spi = spi;
    3446                 :            : 
    3447                 :          0 :         SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics, spi_sync);
    3448                 :          0 :         SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics, spi_sync);
    3449                 :            : 
    3450                 :            :         /* If we're not using the legacy transfer method then we will
    3451                 :            :          * try to transfer in the calling context so special case.
    3452                 :            :          * This code would be less tricky if we could remove the
    3453                 :            :          * support for driver implemented message queues.
    3454                 :            :          */
    3455                 :          0 :         if (ctlr->transfer == spi_queued_transfer) {
    3456                 :          0 :                 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
    3457                 :            : 
    3458                 :          0 :                 trace_spi_message_submit(message);
    3459                 :            : 
    3460                 :          0 :                 status = __spi_queued_transfer(spi, message, false);
    3461                 :            : 
    3462                 :            :                 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
    3463                 :            :         } else {
    3464                 :          0 :                 status = spi_async_locked(spi, message);
    3465                 :            :         }
    3466                 :            : 
    3467                 :          0 :         if (status == 0) {
    3468                 :            :                 /* Push out the messages in the calling context if we
    3469                 :            :                  * can.
    3470                 :            :                  */
    3471                 :          0 :                 if (ctlr->transfer == spi_queued_transfer) {
    3472                 :          0 :                         SPI_STATISTICS_INCREMENT_FIELD(&ctlr->statistics,
    3473                 :            :                                                        spi_sync_immediate);
    3474                 :          0 :                         SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
    3475                 :            :                                                        spi_sync_immediate);
    3476                 :          0 :                         __spi_pump_messages(ctlr, false);
    3477                 :            :                 }
    3478                 :            : 
    3479                 :          0 :                 wait_for_completion(&done);
    3480                 :          0 :                 status = message->status;
    3481                 :            :         }
    3482                 :          0 :         message->context = NULL;
    3483                 :          0 :         return status;
    3484                 :            : }
    3485                 :            : 
    3486                 :            : /**
    3487                 :            :  * spi_sync - blocking/synchronous SPI data transfers
    3488                 :            :  * @spi: device with which data will be exchanged
    3489                 :            :  * @message: describes the data transfers
    3490                 :            :  * Context: can sleep
    3491                 :            :  *
    3492                 :            :  * This call may only be used from a context that may sleep.  The sleep
    3493                 :            :  * is non-interruptible, and has no timeout.  Low-overhead controller
    3494                 :            :  * drivers may DMA directly into and out of the message buffers.
    3495                 :            :  *
    3496                 :            :  * Note that the SPI device's chip select is active during the message,
    3497                 :            :  * and then is normally disabled between messages.  Drivers for some
    3498                 :            :  * frequently-used devices may want to minimize costs of selecting a chip,
    3499                 :            :  * by leaving it selected in anticipation that the next message will go
    3500                 :            :  * to the same chip.  (That may increase power usage.)
    3501                 :            :  *
    3502                 :            :  * Also, the caller is guaranteeing that the memory associated with the
    3503                 :            :  * message will not be freed before this call returns.
    3504                 :            :  *
    3505                 :            :  * Return: zero on success, else a negative error code.
    3506                 :            :  */
    3507                 :          0 : int spi_sync(struct spi_device *spi, struct spi_message *message)
    3508                 :            : {
    3509                 :            :         int ret;
    3510                 :            : 
    3511                 :          0 :         mutex_lock(&spi->controller->bus_lock_mutex);
    3512                 :          0 :         ret = __spi_sync(spi, message);
    3513                 :          0 :         mutex_unlock(&spi->controller->bus_lock_mutex);
    3514                 :            : 
    3515                 :          0 :         return ret;
    3516                 :            : }
    3517                 :            : EXPORT_SYMBOL_GPL(spi_sync);
    3518                 :            : 
    3519                 :            : /**
    3520                 :            :  * spi_sync_locked - version of spi_sync with exclusive bus usage
    3521                 :            :  * @spi: device with which data will be exchanged
    3522                 :            :  * @message: describes the data transfers
    3523                 :            :  * Context: can sleep
    3524                 :            :  *
    3525                 :            :  * This call may only be used from a context that may sleep.  The sleep
    3526                 :            :  * is non-interruptible, and has no timeout.  Low-overhead controller
    3527                 :            :  * drivers may DMA directly into and out of the message buffers.
    3528                 :            :  *
    3529                 :            :  * This call should be used by drivers that require exclusive access to the
    3530                 :            :  * SPI bus. It has to be preceded by a spi_bus_lock call. The SPI bus must
    3531                 :            :  * be released by a spi_bus_unlock call when the exclusive access is over.
    3532                 :            :  *
    3533                 :            :  * Return: zero on success, else a negative error code.
    3534                 :            :  */
    3535                 :          0 : int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
    3536                 :            : {
    3537                 :          0 :         return __spi_sync(spi, message);
    3538                 :            : }
    3539                 :            : EXPORT_SYMBOL_GPL(spi_sync_locked);
    3540                 :            : 
    3541                 :            : /**
    3542                 :            :  * spi_bus_lock - obtain a lock for exclusive SPI bus usage
    3543                 :            :  * @ctlr: SPI bus master that should be locked for exclusive bus access
    3544                 :            :  * Context: can sleep
    3545                 :            :  *
    3546                 :            :  * This call may only be used from a context that may sleep.  The sleep
    3547                 :            :  * is non-interruptible, and has no timeout.
    3548                 :            :  *
    3549                 :            :  * This call should be used by drivers that require exclusive access to the
    3550                 :            :  * SPI bus. The SPI bus must be released by a spi_bus_unlock call when the
    3551                 :            :  * exclusive access is over. Data transfer must be done by spi_sync_locked
    3552                 :            :  * and spi_async_locked calls when the SPI bus lock is held.
    3553                 :            :  *
    3554                 :            :  * Return: always zero.
    3555                 :            :  */
    3556                 :          0 : int spi_bus_lock(struct spi_controller *ctlr)
    3557                 :            : {
    3558                 :            :         unsigned long flags;
    3559                 :            : 
    3560                 :          0 :         mutex_lock(&ctlr->bus_lock_mutex);
    3561                 :            : 
    3562                 :          0 :         spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
    3563                 :          0 :         ctlr->bus_lock_flag = 1;
    3564                 :            :         spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
    3565                 :            : 
    3566                 :            :         /* mutex remains locked until spi_bus_unlock is called */
    3567                 :            : 
    3568                 :          0 :         return 0;
    3569                 :            : }
    3570                 :            : EXPORT_SYMBOL_GPL(spi_bus_lock);
    3571                 :            : 
    3572                 :            : /**
    3573                 :            :  * spi_bus_unlock - release the lock for exclusive SPI bus usage
    3574                 :            :  * @ctlr: SPI bus master that was locked for exclusive bus access
    3575                 :            :  * Context: can sleep
    3576                 :            :  *
    3577                 :            :  * This call may only be used from a context that may sleep.  The sleep
    3578                 :            :  * is non-interruptible, and has no timeout.
    3579                 :            :  *
    3580                 :            :  * This call releases an SPI bus lock previously obtained by an spi_bus_lock
    3581                 :            :  * call.
    3582                 :            :  *
    3583                 :            :  * Return: always zero.
    3584                 :            :  */
    3585                 :          0 : int spi_bus_unlock(struct spi_controller *ctlr)
    3586                 :            : {
    3587                 :          0 :         ctlr->bus_lock_flag = 0;
    3588                 :            : 
    3589                 :          0 :         mutex_unlock(&ctlr->bus_lock_mutex);
    3590                 :            : 
    3591                 :          0 :         return 0;
    3592                 :            : }
    3593                 :            : EXPORT_SYMBOL_GPL(spi_bus_unlock);
    3594                 :            : 
    3595                 :            : /* portable code must never pass more than 32 bytes */
    3596                 :            : #define SPI_BUFSIZ      max(32, SMP_CACHE_BYTES)
    3597                 :            : 
    3598                 :            : static u8       *buf;
    3599                 :            : 
    3600                 :            : /**
    3601                 :            :  * spi_write_then_read - SPI synchronous write followed by read
    3602                 :            :  * @spi: device with which data will be exchanged
    3603                 :            :  * @txbuf: data to be written (need not be dma-safe)
    3604                 :            :  * @n_tx: size of txbuf, in bytes
    3605                 :            :  * @rxbuf: buffer into which data will be read (need not be dma-safe)
    3606                 :            :  * @n_rx: size of rxbuf, in bytes
    3607                 :            :  * Context: can sleep
    3608                 :            :  *
    3609                 :            :  * This performs a half duplex MicroWire style transaction with the
    3610                 :            :  * device, sending txbuf and then reading rxbuf.  The return value
    3611                 :            :  * is zero for success, else a negative errno status code.
    3612                 :            :  * This call may only be used from a context that may sleep.
    3613                 :            :  *
    3614                 :            :  * Parameters to this routine are always copied using a small buffer;
    3615                 :            :  * portable code should never use this for more than 32 bytes.
    3616                 :            :  * Performance-sensitive or bulk transfer code should instead use
    3617                 :            :  * spi_{async,sync}() calls with dma-safe buffers.
    3618                 :            :  *
    3619                 :            :  * Return: zero on success, else a negative error code.
    3620                 :            :  */
    3621                 :          0 : int spi_write_then_read(struct spi_device *spi,
    3622                 :            :                 const void *txbuf, unsigned n_tx,
    3623                 :            :                 void *rxbuf, unsigned n_rx)
    3624                 :            : {
    3625                 :            :         static DEFINE_MUTEX(lock);
    3626                 :            : 
    3627                 :            :         int                     status;
    3628                 :            :         struct spi_message      message;
    3629                 :            :         struct spi_transfer     x[2];
    3630                 :            :         u8                      *local_buf;
    3631                 :            : 
    3632                 :            :         /* Use preallocated DMA-safe buffer if we can.  We can't avoid
    3633                 :            :          * copying here, (as a pure convenience thing), but we can
    3634                 :            :          * keep heap costs out of the hot path unless someone else is
    3635                 :            :          * using the pre-allocated buffer or the transfer is too large.
    3636                 :            :          */
    3637                 :          0 :         if ((n_tx + n_rx) > SPI_BUFSIZ || !mutex_trylock(&lock)) {
    3638                 :          0 :                 local_buf = kmalloc(max((unsigned)SPI_BUFSIZ, n_tx + n_rx),
    3639                 :            :                                     GFP_KERNEL | GFP_DMA);
    3640                 :          0 :                 if (!local_buf)
    3641                 :            :                         return -ENOMEM;
    3642                 :            :         } else {
    3643                 :          0 :                 local_buf = buf;
    3644                 :            :         }
    3645                 :            : 
    3646                 :            :         spi_message_init(&message);
    3647                 :          0 :         memset(x, 0, sizeof(x));
    3648                 :          0 :         if (n_tx) {
    3649                 :          0 :                 x[0].len = n_tx;
    3650                 :            :                 spi_message_add_tail(&x[0], &message);
    3651                 :            :         }
    3652                 :          0 :         if (n_rx) {
    3653                 :          0 :                 x[1].len = n_rx;
    3654                 :            :                 spi_message_add_tail(&x[1], &message);
    3655                 :            :         }
    3656                 :            : 
    3657                 :          0 :         memcpy(local_buf, txbuf, n_tx);
    3658                 :          0 :         x[0].tx_buf = local_buf;
    3659                 :          0 :         x[1].rx_buf = local_buf + n_tx;
    3660                 :            : 
    3661                 :            :         /* do the i/o */
    3662                 :          0 :         status = spi_sync(spi, &message);
    3663                 :          0 :         if (status == 0)
    3664                 :          0 :                 memcpy(rxbuf, x[1].rx_buf, n_rx);
    3665                 :            : 
    3666                 :          0 :         if (x[0].tx_buf == buf)
    3667                 :          0 :                 mutex_unlock(&lock);
    3668                 :            :         else
    3669                 :          0 :                 kfree(local_buf);
    3670                 :            : 
    3671                 :          0 :         return status;
    3672                 :            : }
    3673                 :            : EXPORT_SYMBOL_GPL(spi_write_then_read);
    3674                 :            : 
    3675                 :            : /*-------------------------------------------------------------------------*/
    3676                 :            : 
    3677                 :            : #if IS_ENABLED(CONFIG_OF)
    3678                 :            : /* must call put_device() when done with returned spi_device device */
    3679                 :          0 : struct spi_device *of_find_spi_device_by_node(struct device_node *node)
    3680                 :            : {
    3681                 :            :         struct device *dev = bus_find_device_by_of_node(&spi_bus_type, node);
    3682                 :            : 
    3683                 :          0 :         return dev ? to_spi_device(dev) : NULL;
    3684                 :            : }
    3685                 :            : EXPORT_SYMBOL_GPL(of_find_spi_device_by_node);
    3686                 :            : #endif /* IS_ENABLED(CONFIG_OF) */
    3687                 :            : 
    3688                 :            : #if IS_ENABLED(CONFIG_OF_DYNAMIC)
    3689                 :            : /* the spi controllers are not using spi_bus, so we find it with another way */
    3690                 :          0 : static struct spi_controller *of_find_spi_controller_by_node(struct device_node *node)
    3691                 :            : {
    3692                 :            :         struct device *dev;
    3693                 :            : 
    3694                 :            :         dev = class_find_device_by_of_node(&spi_master_class, node);
    3695                 :          0 :         if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
    3696                 :            :                 dev = class_find_device_by_of_node(&spi_slave_class, node);
    3697                 :          0 :         if (!dev)
    3698                 :            :                 return NULL;
    3699                 :            : 
    3700                 :            :         /* reference got in class_find_device */
    3701                 :          0 :         return container_of(dev, struct spi_controller, dev);
    3702                 :            : }
    3703                 :            : 
    3704                 :          0 : static int of_spi_notify(struct notifier_block *nb, unsigned long action,
    3705                 :            :                          void *arg)
    3706                 :            : {
    3707                 :            :         struct of_reconfig_data *rd = arg;
    3708                 :            :         struct spi_controller *ctlr;
    3709                 :            :         struct spi_device *spi;
    3710                 :            : 
    3711                 :          0 :         switch (of_reconfig_get_state_change(action, arg)) {
    3712                 :            :         case OF_RECONFIG_CHANGE_ADD:
    3713                 :          0 :                 ctlr = of_find_spi_controller_by_node(rd->dn->parent);
    3714                 :          0 :                 if (ctlr == NULL)
    3715                 :            :                         return NOTIFY_OK;       /* not for us */
    3716                 :            : 
    3717                 :          0 :                 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
    3718                 :          0 :                         put_device(&ctlr->dev);
    3719                 :          0 :                         return NOTIFY_OK;
    3720                 :            :                 }
    3721                 :            : 
    3722                 :          0 :                 spi = of_register_spi_device(ctlr, rd->dn);
    3723                 :          0 :                 put_device(&ctlr->dev);
    3724                 :            : 
    3725                 :          0 :                 if (IS_ERR(spi)) {
    3726                 :          0 :                         pr_err("%s: failed to create for '%pOF'\n",
    3727                 :            :                                         __func__, rd->dn);
    3728                 :          0 :                         of_node_clear_flag(rd->dn, OF_POPULATED);
    3729                 :          0 :                         return notifier_from_errno(PTR_ERR(spi));
    3730                 :            :                 }
    3731                 :            :                 break;
    3732                 :            : 
    3733                 :            :         case OF_RECONFIG_CHANGE_REMOVE:
    3734                 :            :                 /* already depopulated? */
    3735                 :          0 :                 if (!of_node_check_flag(rd->dn, OF_POPULATED))
    3736                 :            :                         return NOTIFY_OK;
    3737                 :            : 
    3738                 :            :                 /* find our device by node */
    3739                 :          0 :                 spi = of_find_spi_device_by_node(rd->dn);
    3740                 :          0 :                 if (spi == NULL)
    3741                 :            :                         return NOTIFY_OK;       /* no? not meant for us */
    3742                 :            : 
    3743                 :            :                 /* unregister takes one ref away */
    3744                 :          0 :                 spi_unregister_device(spi);
    3745                 :            : 
    3746                 :            :                 /* and put the reference of the find */
    3747                 :          0 :                 put_device(&spi->dev);
    3748                 :          0 :                 break;
    3749                 :            :         }
    3750                 :            : 
    3751                 :            :         return NOTIFY_OK;
    3752                 :            : }
    3753                 :            : 
    3754                 :            : static struct notifier_block spi_of_notifier = {
    3755                 :            :         .notifier_call = of_spi_notify,
    3756                 :            : };
    3757                 :            : #else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
    3758                 :            : extern struct notifier_block spi_of_notifier;
    3759                 :            : #endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
    3760                 :            : 
    3761                 :            : #if IS_ENABLED(CONFIG_ACPI)
    3762                 :            : static int spi_acpi_controller_match(struct device *dev, const void *data)
    3763                 :            : {
    3764                 :            :         return ACPI_COMPANION(dev->parent) == data;
    3765                 :            : }
    3766                 :            : 
    3767                 :            : static struct spi_controller *acpi_spi_find_controller_by_adev(struct acpi_device *adev)
    3768                 :            : {
    3769                 :            :         struct device *dev;
    3770                 :            : 
    3771                 :            :         dev = class_find_device(&spi_master_class, NULL, adev,
    3772                 :            :                                 spi_acpi_controller_match);
    3773                 :            :         if (!dev && IS_ENABLED(CONFIG_SPI_SLAVE))
    3774                 :            :                 dev = class_find_device(&spi_slave_class, NULL, adev,
    3775                 :            :                                         spi_acpi_controller_match);
    3776                 :            :         if (!dev)
    3777                 :            :                 return NULL;
    3778                 :            : 
    3779                 :            :         return container_of(dev, struct spi_controller, dev);
    3780                 :            : }
    3781                 :            : 
    3782                 :            : static struct spi_device *acpi_spi_find_device_by_adev(struct acpi_device *adev)
    3783                 :            : {
    3784                 :            :         struct device *dev;
    3785                 :            : 
    3786                 :            :         dev = bus_find_device_by_acpi_dev(&spi_bus_type, adev);
    3787                 :            :         return dev ? to_spi_device(dev) : NULL;
    3788                 :            : }
    3789                 :            : 
    3790                 :            : static int acpi_spi_notify(struct notifier_block *nb, unsigned long value,
    3791                 :            :                            void *arg)
    3792                 :            : {
    3793                 :            :         struct acpi_device *adev = arg;
    3794                 :            :         struct spi_controller *ctlr;
    3795                 :            :         struct spi_device *spi;
    3796                 :            : 
    3797                 :            :         switch (value) {
    3798                 :            :         case ACPI_RECONFIG_DEVICE_ADD:
    3799                 :            :                 ctlr = acpi_spi_find_controller_by_adev(adev->parent);
    3800                 :            :                 if (!ctlr)
    3801                 :            :                         break;
    3802                 :            : 
    3803                 :            :                 acpi_register_spi_device(ctlr, adev);
    3804                 :            :                 put_device(&ctlr->dev);
    3805                 :            :                 break;
    3806                 :            :         case ACPI_RECONFIG_DEVICE_REMOVE:
    3807                 :            :                 if (!acpi_device_enumerated(adev))
    3808                 :            :                         break;
    3809                 :            : 
    3810                 :            :                 spi = acpi_spi_find_device_by_adev(adev);
    3811                 :            :                 if (!spi)
    3812                 :            :                         break;
    3813                 :            : 
    3814                 :            :                 spi_unregister_device(spi);
    3815                 :            :                 put_device(&spi->dev);
    3816                 :            :                 break;
    3817                 :            :         }
    3818                 :            : 
    3819                 :            :         return NOTIFY_OK;
    3820                 :            : }
    3821                 :            : 
    3822                 :            : static struct notifier_block spi_acpi_notifier = {
    3823                 :            :         .notifier_call = acpi_spi_notify,
    3824                 :            : };
    3825                 :            : #else
    3826                 :            : extern struct notifier_block spi_acpi_notifier;
    3827                 :            : #endif
    3828                 :            : 
    3829                 :          3 : static int __init spi_init(void)
    3830                 :            : {
    3831                 :            :         int     status;
    3832                 :            : 
    3833                 :          3 :         buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
    3834                 :          3 :         if (!buf) {
    3835                 :            :                 status = -ENOMEM;
    3836                 :            :                 goto err0;
    3837                 :            :         }
    3838                 :            : 
    3839                 :          3 :         status = bus_register(&spi_bus_type);
    3840                 :          3 :         if (status < 0)
    3841                 :            :                 goto err1;
    3842                 :            : 
    3843                 :          3 :         status = class_register(&spi_master_class);
    3844                 :          3 :         if (status < 0)
    3845                 :            :                 goto err2;
    3846                 :            : 
    3847                 :            :         if (IS_ENABLED(CONFIG_SPI_SLAVE)) {
    3848                 :          3 :                 status = class_register(&spi_slave_class);
    3849                 :          3 :                 if (status < 0)
    3850                 :            :                         goto err3;
    3851                 :            :         }
    3852                 :            : 
    3853                 :            :         if (IS_ENABLED(CONFIG_OF_DYNAMIC))
    3854                 :          3 :                 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
    3855                 :            :         if (IS_ENABLED(CONFIG_ACPI))
    3856                 :            :                 WARN_ON(acpi_reconfig_notifier_register(&spi_acpi_notifier));
    3857                 :            : 
    3858                 :            :         return 0;
    3859                 :            : 
    3860                 :            : err3:
    3861                 :          0 :         class_unregister(&spi_master_class);
    3862                 :            : err2:
    3863                 :          0 :         bus_unregister(&spi_bus_type);
    3864                 :            : err1:
    3865                 :          0 :         kfree(buf);
    3866                 :          0 :         buf = NULL;
    3867                 :            : err0:
    3868                 :          0 :         return status;
    3869                 :            : }
    3870                 :            : 
    3871                 :            : /* board_info is normally registered in arch_initcall(),
    3872                 :            :  * but even essential drivers wait till later
    3873                 :            :  *
    3874                 :            :  * REVISIT only boardinfo really needs static linking. the rest (device and
    3875                 :            :  * driver registration) _could_ be dynamically linked (modular) ... costs
    3876                 :            :  * include needing to have boardinfo data structures be much more public.
    3877                 :            :  */
    3878                 :            : postcore_initcall(spi_init);
    

Generated by: LCOV version 1.14