diff options
author | Lorenzo Bianconi <lorenzo.bianconi@redhat.com> | 2018-01-01 19:54:44 +0100 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2018-01-08 16:03:43 +0100 |
commit | 51a8b707627b437f17a153812f049daf3da6729d (patch) | |
tree | 937647f5a989dceb5e1493e656e8a2cf23928b16 /drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_spi.c | |
parent | iio: imu: st_lsm6dsx: introduce conf_lock mutex (diff) | |
download | linux-dev-51a8b707627b437f17a153812f049daf3da6729d.tar.xz linux-dev-51a8b707627b437f17a153812f049daf3da6729d.zip |
iio: imu: st_lsm6dsx: add regmap API support
Introduce regmap API support to access to i2c/spi bus instead of
using a custom support. Set max bulk read to
(32 / SAMPLE_SIZE) * SAMPLE_SIZE since spi_write_then_read() used in
regmap_spi indicates that is the max buffer length to use in order to
avoid a kmalloc for each bus access.
Remove lock mutex since concurrency is already managed by regmap API
Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_spi.c')
-rw-r--r-- | drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_spi.c | 70 |
1 files changed, 14 insertions, 56 deletions
diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_spi.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_spi.c index 95472f153ad2..2c8135834479 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_spi.c +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_spi.c @@ -14,72 +14,30 @@ #include <linux/spi/spi.h> #include <linux/slab.h> #include <linux/of.h> +#include <linux/regmap.h> #include "st_lsm6dsx.h" -#define SENSORS_SPI_READ BIT(7) - -static int st_lsm6dsx_spi_read(struct device *dev, u8 addr, int len, - u8 *data) -{ - struct spi_device *spi = to_spi_device(dev); - struct st_lsm6dsx_hw *hw = spi_get_drvdata(spi); - int err; - - struct spi_transfer xfers[] = { - { - .tx_buf = hw->tb.tx_buf, - .bits_per_word = 8, - .len = 1, - }, - { - .rx_buf = hw->tb.rx_buf, - .bits_per_word = 8, - .len = len, - } - }; - - hw->tb.tx_buf[0] = addr | SENSORS_SPI_READ; - - err = spi_sync_transfer(spi, xfers, ARRAY_SIZE(xfers)); - if (err < 0) - return err; - - memcpy(data, hw->tb.rx_buf, len * sizeof(u8)); - - return len; -} - -static int st_lsm6dsx_spi_write(struct device *dev, u8 addr, int len, - u8 *data) -{ - struct st_lsm6dsx_hw *hw; - struct spi_device *spi; - - if (len >= ST_LSM6DSX_TX_MAX_LENGTH) - return -ENOMEM; - - spi = to_spi_device(dev); - hw = spi_get_drvdata(spi); - - hw->tb.tx_buf[0] = addr; - memcpy(&hw->tb.tx_buf[1], data, len); - - return spi_write(spi, hw->tb.tx_buf, len + 1); -} - -static const struct st_lsm6dsx_transfer_function st_lsm6dsx_transfer_fn = { - .read = st_lsm6dsx_spi_read, - .write = st_lsm6dsx_spi_write, +static const struct regmap_config st_lsm6dsx_spi_regmap_config = { + .reg_bits = 8, + .val_bits = 8, }; static int st_lsm6dsx_spi_probe(struct spi_device *spi) { const struct spi_device_id *id = spi_get_device_id(spi); + int hw_id = id->driver_data; + struct regmap *regmap; + + regmap = devm_regmap_init_spi(spi, &st_lsm6dsx_spi_regmap_config); + if (IS_ERR(regmap)) { + dev_err(&spi->dev, "Failed to register spi regmap %d\n", + (int)PTR_ERR(regmap)); + return PTR_ERR(regmap); + } return st_lsm6dsx_probe(&spi->dev, spi->irq, - (int)id->driver_data, id->name, - &st_lsm6dsx_transfer_fn); + hw_id, id->name, regmap); } static const struct of_device_id st_lsm6dsx_spi_of_match[] = { |