summaryrefslogtreecommitdiff
path: root/drivers/spi/spi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r--drivers/spi/spi.c592
1 files changed, 447 insertions, 145 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 61f7bde8c7fb..5b5b3bc5f0d8 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -4,6 +4,7 @@
// Copyright (C) 2005 David Brownell
// Copyright (C) 2008 Secret Lab Technologies Ltd.
+#include <kunit/visibility.h>
#include <linux/acpi.h>
#include <linux/cache.h>
#include <linux/clk/clk-conf.h>
@@ -19,7 +20,6 @@
#include <linux/ioport.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
-#include <linux/mod_devicetable.h>
#include <linux/mutex.h>
#include <linux/of_device.h>
#include <linux/of_irq.h>
@@ -43,6 +43,9 @@ EXPORT_TRACEPOINT_SYMBOL(spi_transfer_stop);
#include "internals.h"
+static int __spi_setup(struct spi_device *spi, bool initial_setup);
+static int __spi_add_device(struct spi_device *spi, struct spi_device *parent);
+
static DEFINE_IDR(spi_controller_idr);
static void spidev_release(struct device *dev)
@@ -50,7 +53,6 @@ static void spidev_release(struct device *dev)
struct spi_device *spi = to_spi_device(dev);
spi_controller_put(spi->controller);
- kfree(spi->driver_override);
free_percpu(spi->pcpu_statistics);
kfree(spi);
}
@@ -73,10 +75,9 @@ static ssize_t driver_override_store(struct device *dev,
struct device_attribute *a,
const char *buf, size_t count)
{
- struct spi_device *spi = to_spi_device(dev);
int ret;
- ret = driver_set_override(dev, &spi->driver_override, buf, count);
+ ret = __device_set_driver_override(dev, buf, count);
if (ret)
return ret;
@@ -86,35 +87,27 @@ static ssize_t driver_override_store(struct device *dev,
static ssize_t driver_override_show(struct device *dev,
struct device_attribute *a, char *buf)
{
- const struct spi_device *spi = to_spi_device(dev);
- ssize_t len;
-
- device_lock(dev);
- len = sysfs_emit(buf, "%s\n", spi->driver_override ? : "");
- device_unlock(dev);
- return len;
+ guard(spinlock)(&dev->driver_override.lock);
+ return sysfs_emit(buf, "%s\n", dev->driver_override.name ?: "");
}
static DEVICE_ATTR_RW(driver_override);
-static struct spi_statistics __percpu *spi_alloc_pcpu_stats(struct device *dev)
+static struct spi_statistics __percpu *spi_alloc_pcpu_stats(void)
{
struct spi_statistics __percpu *pcpu_stats;
+ int cpu;
- if (dev)
- pcpu_stats = devm_alloc_percpu(dev, struct spi_statistics);
- else
- pcpu_stats = alloc_percpu_gfp(struct spi_statistics, GFP_KERNEL);
-
- if (pcpu_stats) {
- int cpu;
+ pcpu_stats = alloc_percpu_gfp(struct spi_statistics, GFP_KERNEL);
+ if (!pcpu_stats)
+ return NULL;
- for_each_possible_cpu(cpu) {
- struct spi_statistics *stat;
+ for_each_possible_cpu(cpu) {
+ struct spi_statistics *stat;
- stat = per_cpu_ptr(pcpu_stats, cpu);
- u64_stats_init(&stat->syncp);
- }
+ stat = per_cpu_ptr(pcpu_stats, cpu);
+ u64_stats_init(&stat->syncp);
}
+
return pcpu_stats;
}
@@ -306,6 +299,192 @@ static const struct attribute_group spi_controller_statistics_group = {
.attrs = spi_controller_statistics_attrs,
};
+#if IS_ENABLED(CONFIG_SPI_DYNAMIC)
+
+/*
+ * new_device_store - instantiate a new SPI device from userspace
+ *
+ * Takes parameters: <modalias> <chip_select> [<max_speed_hz> [<mode>]]
+ *
+ * Examples:
+ * echo spidev 0 > new_device
+ * echo spidev 0 10000000 > new_device
+ * echo spidev 0 10000000 3 > new_device
+ */
+static ssize_t
+new_device_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct spi_controller *ctlr = container_of(dev, struct spi_controller,
+ dev);
+ struct spi_device *spi;
+ char modalias[SPI_NAME_SIZE];
+ unsigned int chip_select;
+ u32 max_speed_hz = 0;
+ u32 mode = 0;
+ char *blank;
+ int status;
+
+ blank = strchr(buf, ' ');
+ if (!blank) {
+ dev_err(dev, "new_device: Missing parameters\n");
+ return -EINVAL;
+ }
+
+ if (blank == buf || blank - buf > SPI_NAME_SIZE - 1) {
+ dev_err(dev, "new_device: Invalid device name\n");
+ return -EINVAL;
+ }
+
+ memset(modalias, 0, sizeof(modalias));
+ memcpy(modalias, buf, blank - buf);
+
+ /*
+ * sscanf fills only the fields it matches; unmatched optional
+ * fields (max_speed_hz, mode) stay zero from initialisation above.
+ * max_speed_hz == 0 is clamped to the controller max by spi_setup().
+ * mode == 0 selects SPI mode 0 (CPOL=0, CPHA=0).
+ */
+ if (sscanf(++blank, "%u %u %u", &chip_select, &max_speed_hz, &mode) < 1) {
+ dev_err(dev, "new_device: Can't parse chip select\n");
+ return -EINVAL;
+ }
+
+ /*
+ * spi_device.chip_select[] is u8, so cap at U8_MAX independently of
+ * ctlr->num_chipselect (which is u16 and may exceed 255). Without
+ * this, values in (U8_MAX, num_chipselect) would silently truncate
+ * inside spi_set_chipselect() and select the wrong CS.
+ */
+ if (chip_select > U8_MAX || chip_select >= ctlr->num_chipselect) {
+ dev_err(dev, "new_device: Chip select %u out of range (num_chipselect=%u)\n",
+ chip_select, ctlr->num_chipselect);
+ return -EINVAL;
+ }
+
+ /*
+ * Reject kernel-internal mode bits (SPI_NO_TX, SPI_NO_RX,
+ * SPI_TPM_HW_FLOW, ...). These are set only by in-kernel drivers
+ * that know they are safe on their controller/device pair and must
+ * not be settable through a userspace-writable sysfs. Matches
+ * spidev's SPI_IOC_WR_MODE32 handling (drivers/spi/spidev.c).
+ */
+ if (mode & ~(u32)SPI_MODE_USER_MASK) {
+ dev_err(dev, "new_device: Invalid mode bits 0x%x\n",
+ mode & ~(u32)SPI_MODE_USER_MASK);
+ return -EINVAL;
+ }
+
+ spi = spi_alloc_device(ctlr);
+ if (!spi)
+ return -ENOMEM;
+
+ spi_set_chipselect(spi, 0, chip_select);
+ spi->max_speed_hz = max_speed_hz;
+ spi->mode = mode;
+ spi->cs_index_mask = BIT(0);
+ strscpy(spi->modalias, modalias, sizeof(spi->modalias));
+
+ /*
+ * Set driver_override so that the device binds to the driver
+ * named by modalias regardless of whether that driver's
+ * id_table contains a matching entry. This is needed because
+ * some drivers (e.g. spidev) deliberately omit generic names
+ * from their id_table.
+ */
+ status = device_set_driver_override(&spi->dev, modalias);
+ if (status) {
+ spi_dev_put(spi);
+ return status;
+ }
+
+ /*
+ * spi_unregister_controller() removes the new_device/delete_device
+ * sysfs group before taking add_lock, so kernfs_drain() has already
+ * completed by the time we get here and we cannot be racing with
+ * teardown. Take add_lock to serialise the __spi_add_device() and
+ * list insertion with respect to non-sysfs callers of
+ * __spi_add_device() (DT/ACPI, ancillary), which check
+ * device_is_registered(&ctlr->dev) under the same lock.
+ */
+ mutex_lock(&ctlr->add_lock);
+
+ status = __spi_add_device(spi, NULL);
+ if (status) {
+ mutex_unlock(&ctlr->add_lock);
+ spi_dev_put(spi);
+ return status;
+ }
+
+ list_add_tail(&spi->userspace_node, &ctlr->userspace_clients);
+ mutex_unlock(&ctlr->add_lock);
+
+ dev_info(dev, "new_device: Instantiated device %s at CS%u\n",
+ modalias, chip_select);
+ return count;
+}
+static DEVICE_ATTR_IGNORE_LOCKDEP(new_device, 0200, NULL, new_device_store);
+
+static ssize_t
+delete_device_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct spi_controller *ctlr = container_of(dev, struct spi_controller,
+ dev);
+ struct spi_device *spi, *next;
+ unsigned short cs;
+ char end;
+ int res;
+
+ res = sscanf(buf, "%hu%c", &cs, &end);
+ if (res < 1) {
+ dev_err(dev, "delete_device: Can't parse chip select\n");
+ return -EINVAL;
+ }
+ if (res > 1 && end != '\n') {
+ dev_err(dev, "delete_device: Unexpected parameters\n");
+ return -EINVAL;
+ }
+
+ res = -ENOENT;
+ mutex_lock(&ctlr->add_lock);
+ list_for_each_entry_safe(spi, next, &ctlr->userspace_clients,
+ userspace_node) {
+ if (spi_get_chipselect(spi, 0) == cs) {
+ dev_info(dev, "delete_device: Deleting device %s at CS%u\n",
+ spi->modalias, cs);
+ list_del(&spi->userspace_node);
+ spi_unregister_device(spi);
+ res = count;
+ break;
+ }
+ }
+ mutex_unlock(&ctlr->add_lock);
+
+ if (res < 0)
+ dev_err(dev, "delete_device: Can't find device in list\n");
+ return res;
+}
+static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, 0200, NULL,
+ delete_device_store);
+
+static struct attribute *spi_controller_userspace_attrs[] = {
+ &dev_attr_new_device.attr,
+ &dev_attr_delete_device.attr,
+ NULL,
+};
+
+static const struct attribute_group spi_controller_userspace_group = {
+ .attrs = spi_controller_userspace_attrs,
+};
+
+#endif /* CONFIG_SPI_DYNAMIC */
+
+/*
+ * spi_controller_userspace_group is registered manually for host controllers
+ * at the end of spi_register_controller() so new_device/delete_device only
+ * appear after DT/ACPI children and the queue are set up.
+ */
static const struct attribute_group *spi_controller_groups[] = {
&spi_controller_statistics_group,
NULL,
@@ -363,12 +542,16 @@ EXPORT_SYMBOL_GPL(spi_get_device_id);
const void *spi_get_device_match_data(const struct spi_device *sdev)
{
const void *match;
+ const struct spi_device_id *id;
match = device_get_match_data(&sdev->dev);
if (match)
return match;
- return (const void *)spi_get_device_id(sdev)->driver_data;
+ id = spi_get_device_id(sdev);
+ if (!id)
+ return NULL;
+ return (const void *)id->driver_data;
}
EXPORT_SYMBOL_GPL(spi_get_device_match_data);
@@ -376,10 +559,12 @@ static int spi_match_device(struct device *dev, const struct device_driver *drv)
{
const struct spi_device *spi = to_spi_device(dev);
const struct spi_driver *sdrv = to_spi_driver(drv);
+ int ret;
/* Check override first, and if set, only use the named driver */
- if (spi->driver_override)
- return strcmp(spi->driver_override, drv->name) == 0;
+ ret = device_match_driver_override(dev, drv);
+ if (ret >= 0)
+ return ret;
/* Attempt an OF style match */
if (of_driver_match_device(dev, drv))
@@ -574,7 +759,7 @@ struct spi_device *spi_alloc_device(struct spi_controller *ctlr)
return NULL;
}
- spi->pcpu_statistics = spi_alloc_pcpu_stats(NULL);
+ spi->pcpu_statistics = spi_alloc_pcpu_stats();
if (!spi->pcpu_statistics) {
kfree(spi);
spi_controller_put(ctlr);
@@ -641,12 +826,26 @@ static inline int spi_dev_check_cs(struct device *dev,
return 0;
}
+struct spi_dev_check_info {
+ struct spi_device *new_spi;
+ struct spi_device *parent; /* set for ancillary devices */
+};
+
static int spi_dev_check(struct device *dev, void *data)
{
struct spi_device *spi = to_spi_device(dev);
- struct spi_device *new_spi = data;
+ struct spi_dev_check_info *info = data;
+ struct spi_device *new_spi = info->new_spi;
int status, idx;
+ /*
+ * When registering an ancillary device, skip checking against the
+ * parent device since the ancillary is intentionally using one of
+ * the parent's chip selects.
+ */
+ if (info->parent && spi == info->parent)
+ return 0;
+
if (spi->controller == new_spi->controller) {
for (idx = 0; idx < spi->num_chipselect; idx++) {
status = spi_dev_check_cs(dev, spi, idx, new_spi, 0);
@@ -663,10 +862,11 @@ static void spi_cleanup(struct spi_device *spi)
spi->controller->cleanup(spi);
}
-static int __spi_add_device(struct spi_device *spi)
+static int __spi_add_device(struct spi_device *spi, struct spi_device *parent)
{
struct spi_controller *ctlr = spi->controller;
struct device *dev = ctlr->dev.parent;
+ struct spi_dev_check_info check_info;
int status, idx;
u8 cs;
@@ -710,7 +910,9 @@ static int __spi_add_device(struct spi_device *spi)
* chipselect **BEFORE** we call setup(), else we'll trash
* its configuration.
*/
- status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check);
+ check_info.new_spi = spi;
+ check_info.parent = parent;
+ status = bus_for_each_dev(&spi_bus_type, NULL, &check_info, spi_dev_check);
if (status)
return status;
@@ -721,8 +923,6 @@ static int __spi_add_device(struct spi_device *spi)
}
if (ctlr->cs_gpiods) {
- u8 cs;
-
for (idx = 0; idx < spi->num_chipselect; idx++) {
cs = spi_get_chipselect(spi, idx);
spi_set_csgpiod(spi, idx, ctlr->cs_gpiods[cs]);
@@ -734,7 +934,7 @@ static int __spi_add_device(struct spi_device *spi)
* normally rely on the device being setup. Devices
* using SPI_CS_HIGH can't coexist well otherwise...
*/
- status = spi_setup(spi);
+ status = __spi_setup(spi, true);
if (status < 0) {
dev_err(dev, "can't setup %s, status %d\n",
dev_name(&spi->dev), status);
@@ -772,7 +972,7 @@ int spi_add_device(struct spi_device *spi)
spi_dev_set_name(spi);
mutex_lock(&ctlr->add_lock);
- status = __spi_add_device(spi);
+ status = __spi_add_device(spi, NULL);
mutex_unlock(&ctlr->add_lock);
return status;
}
@@ -1219,7 +1419,34 @@ void spi_unmap_buf(struct spi_controller *ctlr, struct device *dev,
spi_unmap_buf_attrs(ctlr, dev, sgt, dir, 0);
}
-static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
+VISIBLE_IF_KUNIT
+int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
+{
+ struct device *rx_dev = ctlr->cur_rx_dma_dev;
+ struct device *tx_dev = ctlr->cur_tx_dma_dev;
+ struct spi_transfer *xfer;
+
+ list_for_each_entry(xfer, &msg->transfers, transfer_list) {
+ /* The sync has already been done after each transfer. */
+ unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
+
+ if (xfer->rx_sg_mapped)
+ spi_unmap_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
+ DMA_FROM_DEVICE, attrs);
+ xfer->rx_sg_mapped = false;
+
+ if (xfer->tx_sg_mapped)
+ spi_unmap_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
+ DMA_TO_DEVICE, attrs);
+ xfer->tx_sg_mapped = false;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_IF_KUNIT(__spi_unmap_msg);
+
+VISIBLE_IF_KUNIT
+int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
{
struct device *tx_dev, *rx_dev;
struct spi_transfer *xfer;
@@ -1242,7 +1469,13 @@ static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
else
rx_dev = ctlr->dev.parent;
- ret = -ENOMSG;
+ /*
+ * Store the devices before mapping so partial failures can be unwound
+ * with the device that created each mapping.
+ */
+ ctlr->cur_tx_dma_dev = tx_dev;
+ ctlr->cur_rx_dma_dev = rx_dev;
+
list_for_each_entry(xfer, &msg->transfers, transfer_list) {
/* The sync is done before each transfer. */
unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
@@ -1255,8 +1488,8 @@ static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
(void *)xfer->tx_buf,
xfer->len, DMA_TO_DEVICE,
attrs);
- if (ret != 0)
- return ret;
+ if (ret)
+ goto unwind;
xfer->tx_sg_mapped = true;
}
@@ -1265,50 +1498,21 @@ static int __spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
ret = spi_map_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
xfer->rx_buf, xfer->len,
DMA_FROM_DEVICE, attrs);
- if (ret != 0) {
- spi_unmap_buf_attrs(ctlr, tx_dev,
- &xfer->tx_sg, DMA_TO_DEVICE,
- attrs);
-
- return ret;
- }
+ if (ret)
+ goto unwind;
xfer->rx_sg_mapped = true;
}
}
- /* No transfer has been mapped, bail out with success */
- if (ret)
- return 0;
-
- ctlr->cur_rx_dma_dev = rx_dev;
- ctlr->cur_tx_dma_dev = tx_dev;
return 0;
-}
-
-static int __spi_unmap_msg(struct spi_controller *ctlr, struct spi_message *msg)
-{
- struct device *rx_dev = ctlr->cur_rx_dma_dev;
- struct device *tx_dev = ctlr->cur_tx_dma_dev;
- struct spi_transfer *xfer;
-
- list_for_each_entry(xfer, &msg->transfers, transfer_list) {
- /* The sync has already been done after each transfer. */
- unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
-
- if (xfer->rx_sg_mapped)
- spi_unmap_buf_attrs(ctlr, rx_dev, &xfer->rx_sg,
- DMA_FROM_DEVICE, attrs);
- xfer->rx_sg_mapped = false;
- if (xfer->tx_sg_mapped)
- spi_unmap_buf_attrs(ctlr, tx_dev, &xfer->tx_sg,
- DMA_TO_DEVICE, attrs);
- xfer->tx_sg_mapped = false;
- }
+unwind:
+ __spi_unmap_msg(ctlr, msg);
- return 0;
+ return ret;
}
+EXPORT_SYMBOL_IF_KUNIT(__spi_map_msg);
static void spi_dma_sync_for_device(struct spi_controller *ctlr,
struct spi_transfer *xfer)
@@ -1361,6 +1565,7 @@ static inline int spi_unmap_msg(struct spi_controller *ctlr,
struct spi_message *msg)
{
struct spi_transfer *xfer;
+ int ret;
list_for_each_entry(xfer, &msg->transfers, transfer_list) {
/*
@@ -1373,7 +1578,12 @@ static inline int spi_unmap_msg(struct spi_controller *ctlr,
xfer->rx_buf = NULL;
}
- return __spi_unmap_msg(ctlr, msg);
+ ret = __spi_unmap_msg(ctlr, msg);
+
+ ctlr->cur_rx_dma_dev = NULL;
+ ctlr->cur_tx_dma_dev = NULL;
+
+ return ret;
}
static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
@@ -2715,8 +2925,8 @@ struct spi_device *spi_new_ancillary_device(struct spi_device *spi,
WARN_ON(!mutex_is_locked(&ctlr->add_lock));
- /* Register the new device */
- rc = __spi_add_device(ancillary);
+ /* Register the new device, passing the parent to skip CS conflict check */
+ rc = __spi_add_device(ancillary, spi);
if (rc) {
dev_err(&spi->dev, "failed to register ancillary device\n");
goto err_out;
@@ -2730,6 +2940,46 @@ err_out:
}
EXPORT_SYMBOL_GPL(spi_new_ancillary_device);
+static void devm_spi_unregister_device(void *spi)
+{
+ spi_unregister_device(spi);
+}
+
+/**
+ * devm_spi_new_ancillary_device() - Register managed ancillary SPI device
+ * @spi: Pointer to the main SPI device registering the ancillary device
+ * @chip_select: Chip Select of the ancillary device
+ *
+ * Register an ancillary SPI device; for example some chips have a chip-select
+ * for normal device usage and another one for setup/firmware upload.
+ *
+ * This is the managed version of spi_new_ancillary_device(). The ancillary
+ * device will be unregistered automatically when the parent SPI device is
+ * unregistered.
+ *
+ * This may only be called from main SPI device's probe routine.
+ *
+ * Return: Pointer to new ancillary device on success; ERR_PTR on failure
+ */
+struct spi_device *devm_spi_new_ancillary_device(struct spi_device *spi,
+ u8 chip_select)
+{
+ struct spi_device *ancillary;
+ int ret;
+
+ ancillary = spi_new_ancillary_device(spi, chip_select);
+ if (IS_ERR(ancillary))
+ return ancillary;
+
+ ret = devm_add_action_or_reset(&spi->dev, devm_spi_unregister_device,
+ ancillary);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return ancillary;
+}
+EXPORT_SYMBOL_GPL(devm_spi_new_ancillary_device);
+
#ifdef CONFIG_ACPI
struct acpi_spi_lookup {
struct spi_controller *ctlr;
@@ -2926,12 +3176,12 @@ struct spi_device *acpi_spi_device_alloc(struct spi_controller *ctlr,
INIT_LIST_HEAD(&resource_list);
ret = acpi_dev_get_resources(adev, &resource_list,
acpi_spi_add_resource, &lookup);
- acpi_dev_free_resource_list(&resource_list);
-
if (ret < 0)
/* Found SPI in _CRS but it points to another controller */
return ERR_PTR(ret);
+ acpi_dev_free_resource_list(&resource_list);
+
if (!lookup.max_speed_hz &&
ACPI_SUCCESS(acpi_get_parent(adev->handle, &parent_handle)) &&
device_match_acpi_handle(lookup.ctlr->dev.parent, parent_handle)) {
@@ -3049,6 +3299,8 @@ static void spi_controller_release(struct device *dev)
struct spi_controller *ctlr;
ctlr = container_of(dev, struct spi_controller, dev);
+
+ free_percpu(ctlr->pcpu_statistics);
kfree(ctlr);
}
@@ -3173,9 +3425,9 @@ extern struct class spi_target_class; /* dummy */
* This must be called from context that can sleep.
*
* The caller is responsible for assigning the bus number and initializing the
- * controller's methods before calling spi_register_controller(); and (after
- * errors adding the device) calling spi_controller_put() to prevent a memory
- * leak.
+ * controller's methods before calling spi_register_controller(); and calling
+ * spi_controller_put() to prevent a memory leak when done with the
+ * controller.
*
* Return: the SPI controller structure on success, else NULL.
*/
@@ -3192,6 +3444,12 @@ struct spi_controller *__spi_alloc_controller(struct device *dev,
if (!ctlr)
return NULL;
+ ctlr->pcpu_statistics = spi_alloc_pcpu_stats();
+ if (!ctlr->pcpu_statistics) {
+ kfree(ctlr);
+ return NULL;
+ }
+
device_initialize(&ctlr->dev);
INIT_LIST_HEAD(&ctlr->queue);
spin_lock_init(&ctlr->queue_lock);
@@ -3199,6 +3457,9 @@ struct spi_controller *__spi_alloc_controller(struct device *dev,
mutex_init(&ctlr->bus_lock_mutex);
mutex_init(&ctlr->io_mutex);
mutex_init(&ctlr->add_lock);
+#if IS_ENABLED(CONFIG_SPI_DYNAMIC)
+ INIT_LIST_HEAD(&ctlr->userspace_clients);
+#endif
ctlr->bus_num = -1;
ctlr->num_chipselect = 1;
ctlr->num_data_lanes = 1;
@@ -3253,8 +3514,6 @@ struct spi_controller *__devm_spi_alloc_controller(struct device *dev,
if (ret)
return NULL;
- ctlr->devm_allocated = true;
-
return ctlr;
}
EXPORT_SYMBOL_GPL(__devm_spi_alloc_controller);
@@ -3383,8 +3642,8 @@ static int spi_controller_id_alloc(struct spi_controller *ctlr, int start, int e
* device identification, boards need configuration tables telling which
* chip is at which address.
*
- * This must be called from context that can sleep. It returns zero on
- * success, else a negative error code (dropping the controller's refcount).
+ * This must be called from context that can sleep.
+ *
* After a successful return, the caller is responsible for calling
* spi_unregister_controller().
*
@@ -3480,17 +3739,8 @@ int spi_register_controller(struct spi_controller *ctlr)
dev_info(dev, "controller is unqueued, this is deprecated\n");
} else if (ctlr->transfer_one || ctlr->transfer_one_message) {
status = spi_controller_initialize_queue(ctlr);
- if (status) {
- device_del(&ctlr->dev);
- goto free_bus_id;
- }
- }
- /* Add statistics */
- ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev);
- if (!ctlr->pcpu_statistics) {
- dev_err(dev, "Error allocating per-cpu statistics\n");
- status = -ENOMEM;
- goto destroy_queue;
+ if (status)
+ goto del_ctrl;
}
mutex_lock(&board_lock);
@@ -3502,14 +3752,39 @@ int spi_register_controller(struct spi_controller *ctlr)
/* Register devices from the device tree and ACPI */
of_register_spi_devices(ctlr);
acpi_register_spi_devices(ctlr);
- return status;
-destroy_queue:
- spi_destroy_queue(ctlr);
+#if IS_ENABLED(CONFIG_SPI_DYNAMIC)
+ /*
+ * Register the new_device/delete_device sysfs interface as the
+ * final step of host controller bringup, only after the queue,
+ * boardinfo matching and DT/ACPI enumeration have all completed.
+ * If this fails, the controller is otherwise usable, so log and
+ * carry on rather than tearing everything down.
+ */
+ if (!spi_controller_is_target(ctlr)) {
+ status = sysfs_create_group(&ctlr->dev.kobj,
+ &spi_controller_userspace_group);
+ if (status) {
+ dev_warn(&ctlr->dev,
+ "Failed to create userspace client interface: %d\n",
+ status);
+ } else {
+ ctlr->userspace_registered = true;
+ /* Notify userspace that the new attributes are available. */
+ kobject_uevent(&ctlr->dev.kobj, KOBJ_CHANGE);
+ }
+ }
+#endif
+
+ return 0;
+
+del_ctrl:
+ device_del(&ctlr->dev);
free_bus_id:
mutex_lock(&board_lock);
idr_remove(&spi_controller_idr, ctlr->bus_num);
mutex_unlock(&board_lock);
+
return status;
}
EXPORT_SYMBOL_GPL(spi_register_controller);
@@ -3527,7 +3802,7 @@ static void devm_spi_unregister_controller(void *ctlr)
* Context: can sleep
*
* Register a SPI device as with spi_register_controller() which will
- * automatically be unregistered and freed.
+ * automatically be unregistered.
*
* Return: zero on success, else a negative error code.
*/
@@ -3541,7 +3816,6 @@ int devm_spi_register_controller(struct device *dev,
return ret;
return devm_add_action_or_reset(dev, devm_spi_unregister_controller, ctlr);
-
}
EXPORT_SYMBOL_GPL(devm_spi_register_controller);
@@ -3560,18 +3834,49 @@ static int __unregister(struct device *dev, void *null)
* only ones directly touching chip registers.
*
* This must be called from context that can sleep.
- *
- * Note that this function also drops a reference to the controller.
*/
void spi_unregister_controller(struct spi_controller *ctlr)
{
struct spi_controller *found;
int id = ctlr->bus_num;
+ /*
+ * Drain in-flight new_device/delete_device sysfs stores and
+ * prevent new ones from starting. Must happen before we take
+ * add_lock so kernfs_drain doesn't wait on a store that is
+ * itself blocked on add_lock.
+ */
+#if IS_ENABLED(CONFIG_SPI_DYNAMIC)
+ if (ctlr->userspace_registered) {
+ sysfs_remove_group(&ctlr->dev.kobj,
+ &spi_controller_userspace_group);
+ ctlr->userspace_registered = false;
+ }
+#endif
+
/* Prevent addition of new devices, unregister existing ones */
if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
mutex_lock(&ctlr->add_lock);
+#if IS_ENABLED(CONFIG_SPI_DYNAMIC)
+ /*
+ * Drain userspace_clients before __unregister since
+ * spi_unregister_device() doesn't do list_del() itself. The
+ * userspace sysfs group has already been removed above and
+ * kernfs_drain() has completed, so no new entries can appear
+ * here.
+ */
+ while (!list_empty(&ctlr->userspace_clients)) {
+ struct spi_device *spi;
+
+ spi = list_first_entry(&ctlr->userspace_clients,
+ struct spi_device,
+ userspace_node);
+ list_del(&spi->userspace_node);
+ spi_unregister_device(spi);
+ }
+#endif
+
device_for_each_child(&ctlr->dev, NULL, __unregister);
/* First make sure that this controller was ever added */
@@ -3596,13 +3901,6 @@ void spi_unregister_controller(struct spi_controller *ctlr)
if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
mutex_unlock(&ctlr->add_lock);
-
- /*
- * Release the last reference on the controller if its driver
- * has not yet been converted to devm_spi_alloc_host/target().
- */
- if (!ctlr->devm_allocated)
- put_device(&ctlr->dev);
}
EXPORT_SYMBOL_GPL(spi_unregister_controller);
@@ -3629,6 +3927,9 @@ int spi_controller_suspend(struct spi_controller *ctlr)
{
int ret = 0;
+ if (ctlr->cur_msg && spi_controller_is_target(ctlr) && ctlr->target_abort)
+ ctlr->target_abort(ctlr);
+
/* Basically no-ops for non-queued controllers */
if (ctlr->queued) {
ret = spi_stop_queue(ctlr);
@@ -3988,27 +4289,7 @@ static int spi_set_cs_timing(struct spi_device *spi)
return status;
}
-/**
- * spi_setup - setup SPI mode and clock rate
- * @spi: the device whose settings are being modified
- * Context: can sleep, and no requests are queued to the device
- *
- * SPI protocol drivers may need to update the transfer mode if the
- * device doesn't work with its default. They may likewise need
- * to update clock rates or word sizes from initial values. This function
- * changes those settings, and must be called from a context that can sleep.
- * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
- * effect the next time the device is selected and data is transferred to
- * or from it. When this function returns, the SPI device is deselected.
- *
- * Note that this call will fail if the protocol driver specifies an option
- * that the underlying controller or its driver does not support. For
- * example, not all hardware supports wire transfers using nine bit words,
- * LSB-first wire encoding, or active-high chipselects.
- *
- * Return: zero on success, else a negative error code.
- */
-int spi_setup(struct spi_device *spi)
+static int __spi_setup(struct spi_device *spi, bool initial_setup)
{
unsigned bad_bits, ugly_bits;
int status;
@@ -4093,7 +4374,7 @@ int spi_setup(struct spi_device *spi)
status = spi_set_cs_timing(spi);
if (status) {
mutex_unlock(&spi->controller->io_mutex);
- return status;
+ goto err_cleanup;
}
if (spi->controller->auto_runtime_pm && spi->controller->set_cs) {
@@ -4102,7 +4383,7 @@ int spi_setup(struct spi_device *spi)
mutex_unlock(&spi->controller->io_mutex);
dev_err(&spi->controller->dev, "Failed to power device: %d\n",
status);
- return status;
+ goto err_cleanup;
}
/*
@@ -4138,6 +4419,37 @@ int spi_setup(struct spi_device *spi)
status);
return status;
+
+err_cleanup:
+ if (initial_setup)
+ spi_cleanup(spi);
+
+ return status;
+}
+
+/**
+ * spi_setup - setup SPI mode and clock rate
+ * @spi: the device whose settings are being modified
+ * Context: can sleep, and no requests are queued to the device
+ *
+ * SPI protocol drivers may need to update the transfer mode if the
+ * device doesn't work with its default. They may likewise need
+ * to update clock rates or word sizes from initial values. This function
+ * changes those settings, and must be called from a context that can sleep.
+ * Except for SPI_CS_HIGH, which takes effect immediately, the changes take
+ * effect the next time the device is selected and data is transferred to
+ * or from it. When this function returns, the SPI device is deselected.
+ *
+ * Note that this call will fail if the protocol driver specifies an option
+ * that the underlying controller or its driver does not support. For
+ * example, not all hardware supports wire transfers using nine bit words,
+ * LSB-first wire encoding, or active-high chipselects.
+ *
+ * Return: zero on success, else a negative error code.
+ */
+int spi_setup(struct spi_device *spi)
+{
+ return __spi_setup(spi, false);
}
EXPORT_SYMBOL_GPL(spi_setup);
@@ -4227,12 +4539,7 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
* SPI transfer length should be multiple of SPI word size
* where SPI word size should be power-of-two multiple.
*/
- if (xfer->bits_per_word <= 8)
- w_size = 1;
- else if (xfer->bits_per_word <= 16)
- w_size = 2;
- else
- w_size = 4;
+ w_size = spi_bpw_to_bytes(xfer->bits_per_word);
/* No partial transfers accepted */
if (xfer->len % w_size)
@@ -4934,11 +5241,6 @@ static int of_spi_notify(struct notifier_block *nb, unsigned long action,
return NOTIFY_OK;
}
- /*
- * Clear the flag before adding the device so that fw_devlink
- * doesn't skip adding consumers to this device.
- */
- rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
spi = of_register_spi_device(ctlr, rd->dn);
put_device(&ctlr->dev);