|
| 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); |
0 commit comments