Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Register map access API - SPI support
0004 //
0005 // Copyright 2011 Wolfson Microelectronics plc
0006 //
0007 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
0008 
0009 #include <linux/regmap.h>
0010 #include <linux/spi/spi.h>
0011 #include <linux/module.h>
0012 
0013 #include "internal.h"
0014 
0015 struct regmap_async_spi {
0016     struct regmap_async core;
0017     struct spi_message m;
0018     struct spi_transfer t[2];
0019 };
0020 
0021 static void regmap_spi_complete(void *data)
0022 {
0023     struct regmap_async_spi *async = data;
0024 
0025     regmap_async_complete_cb(&async->core, async->m.status);
0026 }
0027 
0028 static int regmap_spi_write(void *context, const void *data, size_t count)
0029 {
0030     struct device *dev = context;
0031     struct spi_device *spi = to_spi_device(dev);
0032 
0033     return spi_write(spi, data, count);
0034 }
0035 
0036 static int regmap_spi_gather_write(void *context,
0037                    const void *reg, size_t reg_len,
0038                    const void *val, size_t val_len)
0039 {
0040     struct device *dev = context;
0041     struct spi_device *spi = to_spi_device(dev);
0042     struct spi_message m;
0043     struct spi_transfer t[2] = { { .tx_buf = reg, .len = reg_len, },
0044                      { .tx_buf = val, .len = val_len, }, };
0045 
0046     spi_message_init(&m);
0047     spi_message_add_tail(&t[0], &m);
0048     spi_message_add_tail(&t[1], &m);
0049 
0050     return spi_sync(spi, &m);
0051 }
0052 
0053 static int regmap_spi_async_write(void *context,
0054                   const void *reg, size_t reg_len,
0055                   const void *val, size_t val_len,
0056                   struct regmap_async *a)
0057 {
0058     struct regmap_async_spi *async = container_of(a,
0059                               struct regmap_async_spi,
0060                               core);
0061     struct device *dev = context;
0062     struct spi_device *spi = to_spi_device(dev);
0063 
0064     async->t[0].tx_buf = reg;
0065     async->t[0].len = reg_len;
0066     async->t[1].tx_buf = val;
0067     async->t[1].len = val_len;
0068 
0069     spi_message_init(&async->m);
0070     spi_message_add_tail(&async->t[0], &async->m);
0071     if (val)
0072         spi_message_add_tail(&async->t[1], &async->m);
0073 
0074     async->m.complete = regmap_spi_complete;
0075     async->m.context = async;
0076 
0077     return spi_async(spi, &async->m);
0078 }
0079 
0080 static struct regmap_async *regmap_spi_async_alloc(void)
0081 {
0082     struct regmap_async_spi *async_spi;
0083 
0084     async_spi = kzalloc(sizeof(*async_spi), GFP_KERNEL);
0085     if (!async_spi)
0086         return NULL;
0087 
0088     return &async_spi->core;
0089 }
0090 
0091 static int regmap_spi_read(void *context,
0092                const void *reg, size_t reg_size,
0093                void *val, size_t val_size)
0094 {
0095     struct device *dev = context;
0096     struct spi_device *spi = to_spi_device(dev);
0097 
0098     return spi_write_then_read(spi, reg, reg_size, val, val_size);
0099 }
0100 
0101 static const struct regmap_bus regmap_spi = {
0102     .write = regmap_spi_write,
0103     .gather_write = regmap_spi_gather_write,
0104     .async_write = regmap_spi_async_write,
0105     .async_alloc = regmap_spi_async_alloc,
0106     .read = regmap_spi_read,
0107     .read_flag_mask = 0x80,
0108     .reg_format_endian_default = REGMAP_ENDIAN_BIG,
0109     .val_format_endian_default = REGMAP_ENDIAN_BIG,
0110 };
0111 
0112 static const struct regmap_bus *regmap_get_spi_bus(struct spi_device *spi,
0113                            const struct regmap_config *config)
0114 {
0115     size_t max_size = spi_max_transfer_size(spi);
0116     size_t max_msg_size, reg_reserve_size;
0117     struct regmap_bus *bus;
0118 
0119     if (max_size != SIZE_MAX) {
0120         bus = kmemdup(&regmap_spi, sizeof(*bus), GFP_KERNEL);
0121         if (!bus)
0122             return ERR_PTR(-ENOMEM);
0123 
0124         max_msg_size = spi_max_message_size(spi);
0125         reg_reserve_size = config->reg_bits / BITS_PER_BYTE
0126                  + config->pad_bits / BITS_PER_BYTE;
0127         if (max_size + reg_reserve_size > max_msg_size)
0128             max_size -= reg_reserve_size;
0129 
0130         bus->free_on_exit = true;
0131         bus->max_raw_read = max_size;
0132         bus->max_raw_write = max_size;
0133 
0134         return bus;
0135     }
0136 
0137     return &regmap_spi;
0138 }
0139 
0140 struct regmap *__regmap_init_spi(struct spi_device *spi,
0141                  const struct regmap_config *config,
0142                  struct lock_class_key *lock_key,
0143                  const char *lock_name)
0144 {
0145     const struct regmap_bus *bus = regmap_get_spi_bus(spi, config);
0146 
0147     if (IS_ERR(bus))
0148         return ERR_CAST(bus);
0149 
0150     return __regmap_init(&spi->dev, bus, &spi->dev, config, lock_key, lock_name);
0151 }
0152 EXPORT_SYMBOL_GPL(__regmap_init_spi);
0153 
0154 struct regmap *__devm_regmap_init_spi(struct spi_device *spi,
0155                       const struct regmap_config *config,
0156                       struct lock_class_key *lock_key,
0157                       const char *lock_name)
0158 {
0159     const struct regmap_bus *bus = regmap_get_spi_bus(spi, config);
0160 
0161     if (IS_ERR(bus))
0162         return ERR_CAST(bus);
0163 
0164     return __devm_regmap_init(&spi->dev, bus, &spi->dev, config, lock_key, lock_name);
0165 }
0166 EXPORT_SYMBOL_GPL(__devm_regmap_init_spi);
0167 
0168 MODULE_LICENSE("GPL");