Skip to content

Commit 8e02d18

Browse files
dlechbroonie
authored andcommitted
spi: add basic support for SPI offloading
Add the basic infrastructure to support SPI offload providers and consumers. SPI offloading is a feature that allows the SPI controller to perform transfers without any CPU intervention. This is useful, e.g. for high-speed data acquisition. SPI controllers with offload support need to implement the get_offload and put_offload callbacks and can use the devm_spi_offload_alloc() to allocate offload instances. SPI peripheral drivers will call devm_spi_offload_get() to get a reference to the matching offload instance. This offload instance can then be attached to a SPI message to request offloading that message. It is expected that SPI controllers with offload support will check for the offload instance in the SPI message in the ctlr->optimize_message() callback and handle it accordingly. CONFIG_SPI_OFFLOAD is intended to be a select-only option. Both consumer and provider drivers should `select SPI_OFFLOAD` in their Kconfig to ensure that the SPI core is built with offload support. Reviewed-by: Jonathan Cameron <[email protected]> Reviewed-by: Nuno Sa <[email protected]> Signed-off-by: David Lechner <[email protected]> Link: https://patch.msgid.link/20250207-dlech-mainline-spi-engine-offload-2-v8-1-e48a489be48c@baylibre.com Signed-off-by: Mark Brown <[email protected]>
1 parent 2014c95 commit 8e02d18

File tree

8 files changed

+225
-0
lines changed

8 files changed

+225
-0
lines changed

MAINTAINERS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22295,6 +22295,12 @@ F: Documentation/devicetree/bindings/mtd/jedec,spi-nor.yaml
2229522295
F: drivers/mtd/spi-nor/
2229622296
F: include/linux/mtd/spi-nor.h
2229722297

22298+
SPI OFFLOAD
22299+
R: David Lechner <[email protected]>
22300+
F: drivers/spi/spi-offload.c
22301+
F: include/linux/spi/spi-offload.h
22302+
K: spi_offload
22303+
2229822304
SPI SUBSYSTEM
2229922305
M: Mark Brown <[email protected]>
2230022306

drivers/spi/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ config SPI_MEM
5555
This extension is meant to simplify interaction with SPI memories
5656
by providing a high-level interface to send memory-like commands.
5757

58+
config SPI_OFFLOAD
59+
bool
60+
5861
comment "SPI Master Controller Drivers"
5962

6063
config SPI_AIROHA_SNFI

drivers/spi/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ccflags-$(CONFIG_SPI_DEBUG) := -DDEBUG
1010
obj-$(CONFIG_SPI_MASTER) += spi.o
1111
obj-$(CONFIG_SPI_MEM) += spi-mem.o
1212
obj-$(CONFIG_SPI_MUX) += spi-mux.o
13+
obj-$(CONFIG_SPI_OFFLOAD) += spi-offload.o
1314
obj-$(CONFIG_SPI_SPIDEV) += spidev.o
1415
obj-$(CONFIG_SPI_LOOPBACK_TEST) += spi-loopback-test.o
1516

drivers/spi/spi-offload.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2024 Analog Devices Inc.
4+
* Copyright (C) 2024 BayLibre, SAS
5+
*/
6+
7+
/*
8+
* SPI Offloading support.
9+
*
10+
* Some SPI controllers support offloading of SPI transfers. Essentially, this
11+
* is the ability for a SPI controller to perform SPI transfers with minimal
12+
* or even no CPU intervention, e.g. via a specialized SPI controller with a
13+
* hardware trigger or via a conventional SPI controller using a non-Linux MCU
14+
* processor core to offload the work.
15+
*/
16+
17+
#define DEFAULT_SYMBOL_NAMESPACE "SPI_OFFLOAD"
18+
19+
#include <linux/cleanup.h>
20+
#include <linux/device.h>
21+
#include <linux/export.h>
22+
#include <linux/mutex.h>
23+
#include <linux/spi/offload/consumer.h>
24+
#include <linux/spi/offload/provider.h>
25+
#include <linux/spi/offload/types.h>
26+
#include <linux/spi/spi.h>
27+
#include <linux/types.h>
28+
29+
struct spi_controller_and_offload {
30+
struct spi_controller *controller;
31+
struct spi_offload *offload;
32+
};
33+
34+
/**
35+
* devm_spi_offload_alloc() - Allocate offload instance
36+
* @dev: Device for devm purposes and assigned to &struct spi_offload.provider_dev
37+
* @priv_size: Size of private data to allocate
38+
*
39+
* Offload providers should use this to allocate offload instances.
40+
*
41+
* Return: Pointer to new offload instance or error on failure.
42+
*/
43+
struct spi_offload *devm_spi_offload_alloc(struct device *dev,
44+
size_t priv_size)
45+
{
46+
struct spi_offload *offload;
47+
void *priv;
48+
49+
offload = devm_kzalloc(dev, sizeof(*offload), GFP_KERNEL);
50+
if (!offload)
51+
return ERR_PTR(-ENOMEM);
52+
53+
priv = devm_kzalloc(dev, priv_size, GFP_KERNEL);
54+
if (!priv)
55+
return ERR_PTR(-ENOMEM);
56+
57+
offload->provider_dev = dev;
58+
offload->priv = priv;
59+
60+
return offload;
61+
}
62+
EXPORT_SYMBOL_GPL(devm_spi_offload_alloc);
63+
64+
static void spi_offload_put(void *data)
65+
{
66+
struct spi_controller_and_offload *resource = data;
67+
68+
resource->controller->put_offload(resource->offload);
69+
kfree(resource);
70+
}
71+
72+
/**
73+
* devm_spi_offload_get() - Get an offload instance
74+
* @dev: Device for devm purposes
75+
* @spi: SPI device to use for the transfers
76+
* @config: Offload configuration
77+
*
78+
* Peripheral drivers call this function to get an offload instance that meets
79+
* the requirements specified in @config. If no suitable offload instance is
80+
* available, -ENODEV is returned.
81+
*
82+
* Return: Offload instance or error on failure.
83+
*/
84+
struct spi_offload *devm_spi_offload_get(struct device *dev,
85+
struct spi_device *spi,
86+
const struct spi_offload_config *config)
87+
{
88+
struct spi_controller_and_offload *resource;
89+
int ret;
90+
91+
if (!spi || !config)
92+
return ERR_PTR(-EINVAL);
93+
94+
if (!spi->controller->get_offload)
95+
return ERR_PTR(-ENODEV);
96+
97+
resource = kzalloc(sizeof(*resource), GFP_KERNEL);
98+
if (!resource)
99+
return ERR_PTR(-ENOMEM);
100+
101+
resource->controller = spi->controller;
102+
resource->offload = spi->controller->get_offload(spi, config);
103+
if (IS_ERR(resource->offload)) {
104+
kfree(resource);
105+
return resource->offload;
106+
}
107+
108+
ret = devm_add_action_or_reset(dev, spi_offload_put, resource);
109+
if (ret)
110+
return ERR_PTR(ret);
111+
112+
return resource->offload;
113+
}
114+
EXPORT_SYMBOL_GPL(devm_spi_offload_get);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2024 Analog Devices Inc.
4+
* Copyright (C) 2024 BayLibre, SAS
5+
*/
6+
7+
#ifndef __LINUX_SPI_OFFLOAD_CONSUMER_H
8+
#define __LINUX_SPI_OFFLOAD_CONSUMER_H
9+
10+
#include <linux/module.h>
11+
#include <linux/spi/offload/types.h>
12+
#include <linux/types.h>
13+
14+
MODULE_IMPORT_NS("SPI_OFFLOAD");
15+
16+
struct device;
17+
struct spi_device;
18+
19+
struct spi_offload *devm_spi_offload_get(struct device *dev, struct spi_device *spi,
20+
const struct spi_offload_config *config);
21+
22+
#endif /* __LINUX_SPI_OFFLOAD_CONSUMER_H */
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2024 Analog Devices Inc.
4+
* Copyright (C) 2024 BayLibre, SAS
5+
*/
6+
7+
#ifndef __LINUX_SPI_OFFLOAD_PROVIDER_H
8+
#define __LINUX_SPI_OFFLOAD_PROVIDER_H
9+
10+
#include <linux/module.h>
11+
#include <linux/types.h>
12+
13+
MODULE_IMPORT_NS("SPI_OFFLOAD");
14+
15+
struct device;
16+
17+
struct spi_offload *devm_spi_offload_alloc(struct device *dev, size_t priv_size);
18+
19+
#endif /* __LINUX_SPI_OFFLOAD_PROVIDER_H */

include/linux/spi/offload/types.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2024 Analog Devices Inc.
4+
* Copyright (C) 2024 BayLibre, SAS
5+
*/
6+
7+
#ifndef __LINUX_SPI_OFFLOAD_TYPES_H
8+
#define __LINUX_SPI_OFFLOAD_TYPES_H
9+
10+
#include <linux/types.h>
11+
12+
struct device;
13+
14+
/* Offload can be triggered by external hardware event. */
15+
#define SPI_OFFLOAD_CAP_TRIGGER BIT(0)
16+
/* Offload can record and then play back TX data when triggered. */
17+
#define SPI_OFFLOAD_CAP_TX_STATIC_DATA BIT(1)
18+
/* Offload can get TX data from an external stream source. */
19+
#define SPI_OFFLOAD_CAP_TX_STREAM_DMA BIT(2)
20+
/* Offload can send RX data to an external stream sink. */
21+
#define SPI_OFFLOAD_CAP_RX_STREAM_DMA BIT(3)
22+
23+
/**
24+
* struct spi_offload_config - offload configuration
25+
*
26+
* This is used to request an offload with specific configuration.
27+
*/
28+
struct spi_offload_config {
29+
/** @capability_flags: required capabilities. See %SPI_OFFLOAD_CAP_* */
30+
u32 capability_flags;
31+
};
32+
33+
/**
34+
* struct spi_offload - offload instance
35+
*/
36+
struct spi_offload {
37+
/** @provider_dev: for get/put reference counting */
38+
struct device *provider_dev;
39+
/** @priv: provider driver private data */
40+
void *priv;
41+
};
42+
43+
#endif /* __LINUX_SPI_OFFLOAD_TYPES_H */

include/linux/spi/spi.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct spi_transfer;
3131
struct spi_controller_mem_ops;
3232
struct spi_controller_mem_caps;
3333
struct spi_message;
34+
struct spi_offload;
35+
struct spi_offload_config;
3436

3537
/*
3638
* INTERFACES between SPI master-side drivers and SPI slave protocol handlers,
@@ -496,6 +498,10 @@ extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 ch
496498
* @mem_ops: optimized/dedicated operations for interactions with SPI memory.
497499
* This field is optional and should only be implemented if the
498500
* controller has native support for memory like operations.
501+
* @get_offload: callback for controllers with offload support to get matching
502+
* offload instance. Implementations should return -ENODEV if no match is
503+
* found.
504+
* @put_offload: release the offload instance acquired by @get_offload.
499505
* @mem_caps: controller capabilities for the handling of memory operations.
500506
* @unprepare_message: undo any work done by prepare_message().
501507
* @target_abort: abort the ongoing transfer request on an SPI target controller
@@ -740,6 +746,10 @@ struct spi_controller {
740746
const struct spi_controller_mem_ops *mem_ops;
741747
const struct spi_controller_mem_caps *mem_caps;
742748

749+
struct spi_offload *(*get_offload)(struct spi_device *spi,
750+
const struct spi_offload_config *config);
751+
void (*put_offload)(struct spi_offload *offload);
752+
743753
/* GPIO chip select */
744754
struct gpio_desc **cs_gpiods;
745755
bool use_gpio_descriptors;
@@ -1108,6 +1118,7 @@ struct spi_transfer {
11081118
* @state: for use by whichever driver currently owns the message
11091119
* @opt_state: for use by whichever driver currently owns the message
11101120
* @resources: for resource management when the SPI message is processed
1121+
* @offload: (optional) offload instance used by this message
11111122
*
11121123
* A @spi_message is used to execute an atomic sequence of data transfers,
11131124
* each represented by a struct spi_transfer. The sequence is "atomic"
@@ -1168,6 +1179,12 @@ struct spi_message {
11681179
*/
11691180
void *opt_state;
11701181

1182+
/*
1183+
* Optional offload instance used by this message. This must be set
1184+
* by the peripheral driver before calling spi_optimize_message().
1185+
*/
1186+
struct spi_offload *offload;
1187+
11711188
/* List of spi_res resources when the SPI message is processed */
11721189
struct list_head resources;
11731190
};

0 commit comments

Comments
 (0)