aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Ciocca <denis.ciocca@st.com>2019-07-18 18:53:53 -0400
committerJonathan Cameron <Jonathan.Cameron@huawei.com>2019-07-27 17:52:06 -0400
commit062809ef7733209312562e87cefc84a470430929 (patch)
treed8ba6b9866b49d2b445ab56bcfc5607f7a2eb97a
parent1ecd245e0eb23d1c3803474eba75589743d0d1fe (diff)
iio: make st_sensors drivers use regmap
This patch is meant to replace the i2c/spi transfer functions with regmap. SPI framework requires DMA safe buffers so let's add GFP_DMA flag for memory allocation used by bulk_read functions. Signed-off-by: Denis Ciocca <denis.ciocca@st.com> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
-rw-r--r--drivers/iio/accel/st_accel_buffer.c3
-rw-r--r--drivers/iio/accel/st_accel_core.c3
-rw-r--r--drivers/iio/accel/st_accel_i2c.c4
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_buffer.c10
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_core.c39
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_i2c.c73
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_spi.c100
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_trigger.c10
-rw-r--r--drivers/iio/gyro/st_gyro_buffer.c3
-rw-r--r--drivers/iio/gyro/st_gyro_core.c3
-rw-r--r--drivers/iio/gyro/st_gyro_i2c.c4
-rw-r--r--drivers/iio/magnetometer/st_magn_buffer.c3
-rw-r--r--drivers/iio/magnetometer/st_magn_core.c3
-rw-r--r--drivers/iio/magnetometer/st_magn_i2c.c4
-rw-r--r--drivers/iio/pressure/st_pressure_buffer.c3
-rw-r--r--drivers/iio/pressure/st_pressure_core.c3
-rw-r--r--drivers/iio/pressure/st_pressure_i2c.c4
-rw-r--r--include/linux/iio/common/st_sensors.h40
-rw-r--r--include/linux/iio/common/st_sensors_i2c.h4
19 files changed, 104 insertions, 212 deletions
diff --git a/drivers/iio/accel/st_accel_buffer.c b/drivers/iio/accel/st_accel_buffer.c
index 0205c0167cdd..05f9aea431e2 100644
--- a/drivers/iio/accel/st_accel_buffer.c
+++ b/drivers/iio/accel/st_accel_buffer.c
@@ -39,7 +39,8 @@ static int st_accel_buffer_postenable(struct iio_dev *indio_dev)
39 int err; 39 int err;
40 struct st_sensor_data *adata = iio_priv(indio_dev); 40 struct st_sensor_data *adata = iio_priv(indio_dev);
41 41
42 adata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); 42 adata->buffer_data = kmalloc(indio_dev->scan_bytes,
43 GFP_DMA | GFP_KERNEL);
43 if (adata->buffer_data == NULL) { 44 if (adata->buffer_data == NULL) {
44 err = -ENOMEM; 45 err = -ENOMEM;
45 goto allocate_memory_error; 46 goto allocate_memory_error;
diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c
index 630909702a19..0b17004cb12e 100644
--- a/drivers/iio/accel/st_accel_core.c
+++ b/drivers/iio/accel/st_accel_core.c
@@ -13,7 +13,6 @@
13#include <linux/acpi.h> 13#include <linux/acpi.h>
14#include <linux/errno.h> 14#include <linux/errno.h>
15#include <linux/types.h> 15#include <linux/types.h>
16#include <linux/mutex.h>
17#include <linux/interrupt.h> 16#include <linux/interrupt.h>
18#include <linux/i2c.h> 17#include <linux/i2c.h>
19#include <linux/gpio.h> 18#include <linux/gpio.h>
@@ -1177,7 +1176,6 @@ int st_accel_common_probe(struct iio_dev *indio_dev)
1177 1176
1178 indio_dev->modes = INDIO_DIRECT_MODE; 1177 indio_dev->modes = INDIO_DIRECT_MODE;
1179 indio_dev->info = &accel_info; 1178 indio_dev->info = &accel_info;
1180 mutex_init(&adata->tb.buf_lock);
1181 1179
1182 err = st_sensors_power_enable(indio_dev); 1180 err = st_sensors_power_enable(indio_dev);
1183 if (err) 1181 if (err)
@@ -1188,7 +1186,6 @@ int st_accel_common_probe(struct iio_dev *indio_dev)
1188 goto st_accel_power_off; 1186 goto st_accel_power_off;
1189 1187
1190 adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS; 1188 adata->num_data_channels = ST_ACCEL_NUMBER_DATA_CHANNELS;
1191 adata->multiread_bit = adata->sensor_settings->multi_read_bit;
1192 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 1189 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
1193 1190
1194 channels_size = indio_dev->num_channels * sizeof(struct iio_chan_spec); 1191 channels_size = indio_dev->num_channels * sizeof(struct iio_chan_spec);
diff --git a/drivers/iio/accel/st_accel_i2c.c b/drivers/iio/accel/st_accel_i2c.c
index a92cf776031e..50fa0fc32baa 100644
--- a/drivers/iio/accel/st_accel_i2c.c
+++ b/drivers/iio/accel/st_accel_i2c.c
@@ -174,7 +174,9 @@ static int st_accel_i2c_probe(struct i2c_client *client)
174 adata = iio_priv(indio_dev); 174 adata = iio_priv(indio_dev);
175 adata->sensor_settings = (struct st_sensor_settings *)settings; 175 adata->sensor_settings = (struct st_sensor_settings *)settings;
176 176
177 st_sensors_i2c_configure(indio_dev, client, adata); 177 ret = st_sensors_i2c_configure(indio_dev, client);
178 if (ret < 0)
179 return ret;
178 180
179 ret = st_accel_common_probe(indio_dev); 181 ret = st_accel_common_probe(indio_dev);
180 if (ret < 0) 182 if (ret < 0)
diff --git a/drivers/iio/common/st_sensors/st_sensors_buffer.c b/drivers/iio/common/st_sensors/st_sensors_buffer.c
index 4a68669dc555..eee30130ae23 100644
--- a/drivers/iio/common/st_sensors/st_sensors_buffer.c
+++ b/drivers/iio/common/st_sensors/st_sensors_buffer.c
@@ -17,15 +17,16 @@
17#include <linux/iio/trigger_consumer.h> 17#include <linux/iio/trigger_consumer.h>
18#include <linux/iio/triggered_buffer.h> 18#include <linux/iio/triggered_buffer.h>
19#include <linux/irqreturn.h> 19#include <linux/irqreturn.h>
20#include <linux/regmap.h>
20 21
21#include <linux/iio/common/st_sensors.h> 22#include <linux/iio/common/st_sensors.h>
22 23
23 24
24static int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf) 25static int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
25{ 26{
26 int i;
27 struct st_sensor_data *sdata = iio_priv(indio_dev); 27 struct st_sensor_data *sdata = iio_priv(indio_dev);
28 unsigned int num_data_channels = sdata->num_data_channels; 28 unsigned int num_data_channels = sdata->num_data_channels;
29 int i;
29 30
30 for_each_set_bit(i, indio_dev->active_scan_mask, num_data_channels) { 31 for_each_set_bit(i, indio_dev->active_scan_mask, num_data_channels) {
31 const struct iio_chan_spec *channel = &indio_dev->channels[i]; 32 const struct iio_chan_spec *channel = &indio_dev->channels[i];
@@ -36,11 +37,8 @@ static int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
36 channel->scan_type.storagebits >> 3; 37 channel->scan_type.storagebits >> 3;
37 38
38 buf = PTR_ALIGN(buf, storage_bytes); 39 buf = PTR_ALIGN(buf, storage_bytes);
39 if (sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev, 40 if (regmap_bulk_read(sdata->regmap, channel->address,
40 channel->address, 41 buf, bytes_to_read) < 0)
41 bytes_to_read, buf,
42 sdata->multiread_bit) <
43 bytes_to_read)
44 return -EIO; 42 return -EIO;
45 43
46 /* Advance the buffer pointer */ 44 /* Advance the buffer pointer */
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index f05bf7cf0594..4a3064fb6cd9 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -15,6 +15,7 @@
15#include <linux/regulator/consumer.h> 15#include <linux/regulator/consumer.h>
16#include <linux/of.h> 16#include <linux/of.h>
17#include <linux/of_device.h> 17#include <linux/of_device.h>
18#include <linux/regmap.h>
18#include <asm/unaligned.h> 19#include <asm/unaligned.h>
19#include <linux/iio/common/st_sensors.h> 20#include <linux/iio/common/st_sensors.h>
20 21
@@ -28,19 +29,10 @@ static inline u32 st_sensors_get_unaligned_le24(const u8 *p)
28int st_sensors_write_data_with_mask(struct iio_dev *indio_dev, 29int st_sensors_write_data_with_mask(struct iio_dev *indio_dev,
29 u8 reg_addr, u8 mask, u8 data) 30 u8 reg_addr, u8 mask, u8 data)
30{ 31{
31 int err;
32 u8 new_data;
33 struct st_sensor_data *sdata = iio_priv(indio_dev); 32 struct st_sensor_data *sdata = iio_priv(indio_dev);
34 33
35 err = sdata->tf->read_byte(&sdata->tb, sdata->dev, reg_addr, &new_data); 34 return regmap_update_bits(sdata->regmap,
36 if (err < 0) 35 reg_addr, mask, data << __ffs(mask));
37 goto st_sensors_write_data_with_mask_error;
38
39 new_data = ((new_data & (~mask)) | ((data << __ffs(mask)) & mask));
40 err = sdata->tf->write_byte(&sdata->tb, sdata->dev, reg_addr, new_data);
41
42st_sensors_write_data_with_mask_error:
43 return err;
44} 36}
45 37
46int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev, 38int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
@@ -48,19 +40,15 @@ int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
48 unsigned *readval) 40 unsigned *readval)
49{ 41{
50 struct st_sensor_data *sdata = iio_priv(indio_dev); 42 struct st_sensor_data *sdata = iio_priv(indio_dev);
51 u8 readdata;
52 int err; 43 int err;
53 44
54 if (!readval) 45 if (!readval)
55 return sdata->tf->write_byte(&sdata->tb, sdata->dev, 46 return regmap_write(sdata->regmap, reg, writeval);
56 (u8)reg, (u8)writeval);
57 47
58 err = sdata->tf->read_byte(&sdata->tb, sdata->dev, (u8)reg, &readdata); 48 err = regmap_read(sdata->regmap, reg, readval);
59 if (err < 0) 49 if (err < 0)
60 return err; 50 return err;
61 51
62 *readval = (unsigned)readdata;
63
64 return 0; 52 return 0;
65} 53}
66EXPORT_SYMBOL(st_sensors_debugfs_reg_access); 54EXPORT_SYMBOL(st_sensors_debugfs_reg_access);
@@ -545,7 +533,7 @@ st_sensors_match_scale_error:
545EXPORT_SYMBOL(st_sensors_set_fullscale_by_gain); 533EXPORT_SYMBOL(st_sensors_set_fullscale_by_gain);
546 534
547static int st_sensors_read_axis_data(struct iio_dev *indio_dev, 535static int st_sensors_read_axis_data(struct iio_dev *indio_dev,
548 struct iio_chan_spec const *ch, int *data) 536 struct iio_chan_spec const *ch, int *data)
549{ 537{
550 int err; 538 int err;
551 u8 *outdata; 539 u8 *outdata;
@@ -554,13 +542,12 @@ static int st_sensors_read_axis_data(struct iio_dev *indio_dev,
554 542
555 byte_for_channel = DIV_ROUND_UP(ch->scan_type.realbits + 543 byte_for_channel = DIV_ROUND_UP(ch->scan_type.realbits +
556 ch->scan_type.shift, 8); 544 ch->scan_type.shift, 8);
557 outdata = kmalloc(byte_for_channel, GFP_KERNEL); 545 outdata = kmalloc(byte_for_channel, GFP_DMA | GFP_KERNEL);
558 if (!outdata) 546 if (!outdata)
559 return -ENOMEM; 547 return -ENOMEM;
560 548
561 err = sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev, 549 err = regmap_bulk_read(sdata->regmap, ch->address,
562 ch->address, byte_for_channel, 550 outdata, byte_for_channel);
563 outdata, sdata->multiread_bit);
564 if (err < 0) 551 if (err < 0)
565 goto st_sensors_free_memory; 552 goto st_sensors_free_memory;
566 553
@@ -645,13 +632,11 @@ EXPORT_SYMBOL(st_sensors_get_settings_index);
645int st_sensors_verify_id(struct iio_dev *indio_dev) 632int st_sensors_verify_id(struct iio_dev *indio_dev)
646{ 633{
647 struct st_sensor_data *sdata = iio_priv(indio_dev); 634 struct st_sensor_data *sdata = iio_priv(indio_dev);
648 int err; 635 int wai, err;
649 u8 wai;
650 636
651 if (sdata->sensor_settings->wai_addr) { 637 if (sdata->sensor_settings->wai_addr) {
652 err = sdata->tf->read_byte(&sdata->tb, sdata->dev, 638 err = regmap_read(sdata->regmap,
653 sdata->sensor_settings->wai_addr, 639 sdata->sensor_settings->wai_addr, &wai);
654 &wai);
655 if (err < 0) { 640 if (err < 0) {
656 dev_err(&indio_dev->dev, 641 dev_err(&indio_dev->dev,
657 "failed to read Who-Am-I register.\n"); 642 "failed to read Who-Am-I register.\n");
diff --git a/drivers/iio/common/st_sensors/st_sensors_i2c.c b/drivers/iio/common/st_sensors/st_sensors_i2c.c
index b1c9812407e7..9240625534df 100644
--- a/drivers/iio/common/st_sensors/st_sensors_i2c.c
+++ b/drivers/iio/common/st_sensors/st_sensors_i2c.c
@@ -13,6 +13,7 @@
13#include <linux/iio/iio.h> 13#include <linux/iio/iio.h>
14#include <linux/of_device.h> 14#include <linux/of_device.h>
15#include <linux/acpi.h> 15#include <linux/acpi.h>
16#include <linux/regmap.h>
16 17
17#include <linux/iio/common/st_sensors_i2c.h> 18#include <linux/iio/common/st_sensors_i2c.h>
18 19
@@ -26,55 +27,51 @@ static unsigned int st_sensors_i2c_get_irq(struct iio_dev *indio_dev)
26 return to_i2c_client(sdata->dev)->irq; 27 return to_i2c_client(sdata->dev)->irq;
27} 28}
28 29
29static int st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer *tb, 30static const struct regmap_config st_sensors_i2c_regmap_config = {
30 struct device *dev, u8 reg_addr, u8 *res_byte) 31 .reg_bits = 8,
31{ 32 .val_bits = 8,
32 int err; 33};
33
34 err = i2c_smbus_read_byte_data(to_i2c_client(dev), reg_addr);
35 if (err < 0)
36 goto st_accel_i2c_read_byte_error;
37
38 *res_byte = err & 0xff;
39
40st_accel_i2c_read_byte_error:
41 return err < 0 ? err : 0;
42}
43
44static int st_sensors_i2c_read_multiple_byte(
45 struct st_sensor_transfer_buffer *tb, struct device *dev,
46 u8 reg_addr, int len, u8 *data, bool multiread_bit)
47{
48 if (multiread_bit)
49 reg_addr |= ST_SENSORS_I2C_MULTIREAD;
50
51 return i2c_smbus_read_i2c_block_data_or_emulated(to_i2c_client(dev),
52 reg_addr, len, data);
53}
54
55static int st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer *tb,
56 struct device *dev, u8 reg_addr, u8 data)
57{
58 return i2c_smbus_write_byte_data(to_i2c_client(dev), reg_addr, data);
59}
60 34
61static const struct st_sensor_transfer_function st_sensors_tf_i2c = { 35static const struct regmap_config st_sensors_i2c_regmap_multiread_bit_config = {
62 .read_byte = st_sensors_i2c_read_byte, 36 .reg_bits = 8,
63 .write_byte = st_sensors_i2c_write_byte, 37 .val_bits = 8,
64 .read_multiple_byte = st_sensors_i2c_read_multiple_byte, 38 .read_flag_mask = ST_SENSORS_I2C_MULTIREAD,
65}; 39};
66 40
67void st_sensors_i2c_configure(struct iio_dev *indio_dev, 41/*
68 struct i2c_client *client, struct st_sensor_data *sdata) 42 * st_sensors_i2c_configure() - configure I2C interface
43 * @indio_dev: IIO device reference.
44 * @client: i2c client reference.
45 *
46 * Return: 0 on success, else a negative error code.
47 */
48int st_sensors_i2c_configure(struct iio_dev *indio_dev,
49 struct i2c_client *client)
69{ 50{
51 struct st_sensor_data *sdata = iio_priv(indio_dev);
52 const struct regmap_config *config;
53
54 if (sdata->sensor_settings->multi_read_bit)
55 config = &st_sensors_i2c_regmap_multiread_bit_config;
56 else
57 config = &st_sensors_i2c_regmap_config;
58
59 sdata->regmap = devm_regmap_init_i2c(client, config);
60 if (IS_ERR(sdata->regmap)) {
61 dev_err(&client->dev, "Failed to register i2c regmap (%d)\n",
62 (int)PTR_ERR(sdata->regmap));
63 return PTR_ERR(sdata->regmap);
64 }
65
70 i2c_set_clientdata(client, indio_dev); 66 i2c_set_clientdata(client, indio_dev);
71 67
72 indio_dev->dev.parent = &client->dev; 68 indio_dev->dev.parent = &client->dev;
73 indio_dev->name = client->name; 69 indio_dev->name = client->name;
74 70
75 sdata->dev = &client->dev; 71 sdata->dev = &client->dev;
76 sdata->tf = &st_sensors_tf_i2c;
77 sdata->get_irq_data_ready = st_sensors_i2c_get_irq; 72 sdata->get_irq_data_ready = st_sensors_i2c_get_irq;
73
74 return 0;
78} 75}
79EXPORT_SYMBOL(st_sensors_i2c_configure); 76EXPORT_SYMBOL(st_sensors_i2c_configure);
80 77
diff --git a/drivers/iio/common/st_sensors/st_sensors_spi.c b/drivers/iio/common/st_sensors/st_sensors_spi.c
index a57cd648975c..9c0661a283d0 100644
--- a/drivers/iio/common/st_sensors/st_sensors_spi.c
+++ b/drivers/iio/common/st_sensors/st_sensors_spi.c
@@ -11,12 +11,12 @@
11#include <linux/module.h> 11#include <linux/module.h>
12#include <linux/slab.h> 12#include <linux/slab.h>
13#include <linux/iio/iio.h> 13#include <linux/iio/iio.h>
14#include <linux/regmap.h>
14 15
15#include <linux/iio/common/st_sensors_spi.h> 16#include <linux/iio/common/st_sensors_spi.h>
16 17#include "st_sensors_core.h"
17 18
18#define ST_SENSORS_SPI_MULTIREAD 0xc0 19#define ST_SENSORS_SPI_MULTIREAD 0xc0
19#define ST_SENSORS_SPI_READ 0x80
20 20
21static unsigned int st_sensors_spi_get_irq(struct iio_dev *indio_dev) 21static unsigned int st_sensors_spi_get_irq(struct iio_dev *indio_dev)
22{ 22{
@@ -25,81 +25,15 @@ static unsigned int st_sensors_spi_get_irq(struct iio_dev *indio_dev)
25 return to_spi_device(sdata->dev)->irq; 25 return to_spi_device(sdata->dev)->irq;
26} 26}
27 27
28static int st_sensors_spi_read(struct st_sensor_transfer_buffer *tb, 28static const struct regmap_config st_sensors_spi_regmap_config = {
29 struct device *dev, u8 reg_addr, int len, u8 *data, bool multiread_bit) 29 .reg_bits = 8,
30{ 30 .val_bits = 8,
31 int err; 31};
32
33 struct spi_transfer xfers[] = {
34 {
35 .tx_buf = tb->tx_buf,
36 .bits_per_word = 8,
37 .len = 1,
38 },
39 {
40 .rx_buf = tb->rx_buf,
41 .bits_per_word = 8,
42 .len = len,
43 }
44 };
45
46 mutex_lock(&tb->buf_lock);
47 if ((multiread_bit) && (len > 1))
48 tb->tx_buf[0] = reg_addr | ST_SENSORS_SPI_MULTIREAD;
49 else
50 tb->tx_buf[0] = reg_addr | ST_SENSORS_SPI_READ;
51
52 err = spi_sync_transfer(to_spi_device(dev), xfers, ARRAY_SIZE(xfers));
53 if (err)
54 goto acc_spi_read_error;
55
56 memcpy(data, tb->rx_buf, len);
57 mutex_unlock(&tb->buf_lock);
58 return len;
59
60acc_spi_read_error:
61 mutex_unlock(&tb->buf_lock);
62 return err;
63}
64
65static int st_sensors_spi_read_byte(struct st_sensor_transfer_buffer *tb,
66 struct device *dev, u8 reg_addr, u8 *res_byte)
67{
68 return st_sensors_spi_read(tb, dev, reg_addr, 1, res_byte, false);
69}
70
71static int st_sensors_spi_read_multiple_byte(
72 struct st_sensor_transfer_buffer *tb, struct device *dev,
73 u8 reg_addr, int len, u8 *data, bool multiread_bit)
74{
75 return st_sensors_spi_read(tb, dev, reg_addr, len, data, multiread_bit);
76}
77
78static int st_sensors_spi_write_byte(struct st_sensor_transfer_buffer *tb,
79 struct device *dev, u8 reg_addr, u8 data)
80{
81 int err;
82
83 struct spi_transfer xfers = {
84 .tx_buf = tb->tx_buf,
85 .bits_per_word = 8,
86 .len = 2,
87 };
88
89 mutex_lock(&tb->buf_lock);
90 tb->tx_buf[0] = reg_addr;
91 tb->tx_buf[1] = data;
92
93 err = spi_sync_transfer(to_spi_device(dev), &xfers, 1);
94 mutex_unlock(&tb->buf_lock);
95
96 return err;
97}
98 32
99static const struct st_sensor_transfer_function st_sensors_tf_spi = { 33static const struct regmap_config st_sensors_spi_regmap_multiread_bit_config = {
100 .read_byte = st_sensors_spi_read_byte, 34 .reg_bits = 8,
101 .write_byte = st_sensors_spi_write_byte, 35 .val_bits = 8,
102 .read_multiple_byte = st_sensors_spi_read_multiple_byte, 36 .read_flag_mask = ST_SENSORS_SPI_MULTIREAD,
103}; 37};
104 38
105/* 39/*
@@ -155,6 +89,7 @@ int st_sensors_spi_configure(struct iio_dev *indio_dev,
155 struct spi_device *spi) 89 struct spi_device *spi)
156{ 90{
157 struct st_sensor_data *sdata = iio_priv(indio_dev); 91 struct st_sensor_data *sdata = iio_priv(indio_dev);
92 const struct regmap_config *config;
158 int err; 93 int err;
159 94
160 if (st_sensors_is_spi_3_wire(spi)) { 95 if (st_sensors_is_spi_3_wire(spi)) {
@@ -164,13 +99,24 @@ int st_sensors_spi_configure(struct iio_dev *indio_dev,
164 return err; 99 return err;
165 } 100 }
166 101
102 if (sdata->sensor_settings->multi_read_bit)
103 config = &st_sensors_spi_regmap_multiread_bit_config;
104 else
105 config = &st_sensors_spi_regmap_config;
106
107 sdata->regmap = devm_regmap_init_spi(spi, config);
108 if (IS_ERR(sdata->regmap)) {
109 dev_err(&spi->dev, "Failed to register spi regmap (%d)\n",
110 (int)PTR_ERR(sdata->regmap));
111 return PTR_ERR(sdata->regmap);
112 }
113
167 spi_set_drvdata(spi, indio_dev); 114 spi_set_drvdata(spi, indio_dev);
168 115
169 indio_dev->dev.parent = &spi->dev; 116 indio_dev->dev.parent = &spi->dev;
170 indio_dev->name = spi->modalias; 117 indio_dev->name = spi->modalias;
171 118
172 sdata->dev = &spi->dev; 119 sdata->dev = &spi->dev;
173 sdata->tf = &st_sensors_tf_spi;
174 sdata->get_irq_data_ready = st_sensors_spi_get_irq; 120 sdata->get_irq_data_ready = st_sensors_spi_get_irq;
175 121
176 return 0; 122 return 0;
diff --git a/drivers/iio/common/st_sensors/st_sensors_trigger.c b/drivers/iio/common/st_sensors/st_sensors_trigger.c
index 630c8cb35e8b..bed7b8682b17 100644
--- a/drivers/iio/common/st_sensors/st_sensors_trigger.c
+++ b/drivers/iio/common/st_sensors/st_sensors_trigger.c
@@ -13,6 +13,7 @@
13#include <linux/iio/iio.h> 13#include <linux/iio/iio.h>
14#include <linux/iio/trigger.h> 14#include <linux/iio/trigger.h>
15#include <linux/interrupt.h> 15#include <linux/interrupt.h>
16#include <linux/regmap.h>
16#include <linux/iio/common/st_sensors.h> 17#include <linux/iio/common/st_sensors.h>
17#include "st_sensors_core.h" 18#include "st_sensors_core.h"
18 19
@@ -26,8 +27,7 @@
26static int st_sensors_new_samples_available(struct iio_dev *indio_dev, 27static int st_sensors_new_samples_available(struct iio_dev *indio_dev,
27 struct st_sensor_data *sdata) 28 struct st_sensor_data *sdata)
28{ 29{
29 u8 status; 30 int ret, status;
30 int ret;
31 31
32 /* How would I know if I can't check it? */ 32 /* How would I know if I can't check it? */
33 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr) 33 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr)
@@ -37,9 +37,9 @@ static int st_sensors_new_samples_available(struct iio_dev *indio_dev,
37 if (!indio_dev->active_scan_mask) 37 if (!indio_dev->active_scan_mask)
38 return 0; 38 return 0;
39 39
40 ret = sdata->tf->read_byte(&sdata->tb, sdata->dev, 40 ret = regmap_read(sdata->regmap,
41 sdata->sensor_settings->drdy_irq.stat_drdy.addr, 41 sdata->sensor_settings->drdy_irq.stat_drdy.addr,
42 &status); 42 &status);
43 if (ret < 0) { 43 if (ret < 0) {
44 dev_err(sdata->dev, 44 dev_err(sdata->dev,
45 "error checking samples available\n"); 45 "error checking samples available\n");
diff --git a/drivers/iio/gyro/st_gyro_buffer.c b/drivers/iio/gyro/st_gyro_buffer.c
index 6e362f735e92..21360681d4dd 100644
--- a/drivers/iio/gyro/st_gyro_buffer.c
+++ b/drivers/iio/gyro/st_gyro_buffer.c
@@ -39,7 +39,8 @@ static int st_gyro_buffer_postenable(struct iio_dev *indio_dev)
39 int err; 39 int err;
40 struct st_sensor_data *gdata = iio_priv(indio_dev); 40 struct st_sensor_data *gdata = iio_priv(indio_dev);
41 41
42 gdata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); 42 gdata->buffer_data = kmalloc(indio_dev->scan_bytes,
43 GFP_DMA | GFP_KERNEL);
43 if (gdata->buffer_data == NULL) { 44 if (gdata->buffer_data == NULL) {
44 err = -ENOMEM; 45 err = -ENOMEM;
45 goto allocate_memory_error; 46 goto allocate_memory_error;
diff --git a/drivers/iio/gyro/st_gyro_core.c b/drivers/iio/gyro/st_gyro_core.c
index 4b87e79aa744..02e42c945181 100644
--- a/drivers/iio/gyro/st_gyro_core.c
+++ b/drivers/iio/gyro/st_gyro_core.c
@@ -12,7 +12,6 @@
12#include <linux/slab.h> 12#include <linux/slab.h>
13#include <linux/errno.h> 13#include <linux/errno.h>
14#include <linux/types.h> 14#include <linux/types.h>
15#include <linux/mutex.h>
16#include <linux/interrupt.h> 15#include <linux/interrupt.h>
17#include <linux/i2c.h> 16#include <linux/i2c.h>
18#include <linux/gpio.h> 17#include <linux/gpio.h>
@@ -394,7 +393,6 @@ int st_gyro_common_probe(struct iio_dev *indio_dev)
394 393
395 indio_dev->modes = INDIO_DIRECT_MODE; 394 indio_dev->modes = INDIO_DIRECT_MODE;
396 indio_dev->info = &gyro_info; 395 indio_dev->info = &gyro_info;
397 mutex_init(&gdata->tb.buf_lock);
398 396
399 err = st_sensors_power_enable(indio_dev); 397 err = st_sensors_power_enable(indio_dev);
400 if (err) 398 if (err)
@@ -405,7 +403,6 @@ int st_gyro_common_probe(struct iio_dev *indio_dev)
405 goto st_gyro_power_off; 403 goto st_gyro_power_off;
406 404
407 gdata->num_data_channels = ST_GYRO_NUMBER_DATA_CHANNELS; 405 gdata->num_data_channels = ST_GYRO_NUMBER_DATA_CHANNELS;
408 gdata->multiread_bit = gdata->sensor_settings->multi_read_bit;
409 indio_dev->channels = gdata->sensor_settings->ch; 406 indio_dev->channels = gdata->sensor_settings->ch;
410 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 407 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
411 408
diff --git a/drivers/iio/gyro/st_gyro_i2c.c b/drivers/iio/gyro/st_gyro_i2c.c
index fa71e94b76f4..05a1a0874bd5 100644
--- a/drivers/iio/gyro/st_gyro_i2c.c
+++ b/drivers/iio/gyro/st_gyro_i2c.c
@@ -87,7 +87,9 @@ static int st_gyro_i2c_probe(struct i2c_client *client,
87 gdata = iio_priv(indio_dev); 87 gdata = iio_priv(indio_dev);
88 gdata->sensor_settings = (struct st_sensor_settings *)settings; 88 gdata->sensor_settings = (struct st_sensor_settings *)settings;
89 89
90 st_sensors_i2c_configure(indio_dev, client, gdata); 90 err = st_sensors_i2c_configure(indio_dev, client);
91 if (err < 0)
92 return err;
91 93
92 err = st_gyro_common_probe(indio_dev); 94 err = st_gyro_common_probe(indio_dev);
93 if (err < 0) 95 if (err < 0)
diff --git a/drivers/iio/magnetometer/st_magn_buffer.c b/drivers/iio/magnetometer/st_magn_buffer.c
index 11d7806655bc..9dba93539a99 100644
--- a/drivers/iio/magnetometer/st_magn_buffer.c
+++ b/drivers/iio/magnetometer/st_magn_buffer.c
@@ -34,7 +34,8 @@ static int st_magn_buffer_postenable(struct iio_dev *indio_dev)
34 int err; 34 int err;
35 struct st_sensor_data *mdata = iio_priv(indio_dev); 35 struct st_sensor_data *mdata = iio_priv(indio_dev);
36 36
37 mdata->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); 37 mdata->buffer_data = kmalloc(indio_dev->scan_bytes,
38 GFP_DMA | GFP_KERNEL);
38 if (mdata->buffer_data == NULL) { 39 if (mdata->buffer_data == NULL) {
39 err = -ENOMEM; 40 err = -ENOMEM;
40 goto allocate_memory_error; 41 goto allocate_memory_error;
diff --git a/drivers/iio/magnetometer/st_magn_core.c b/drivers/iio/magnetometer/st_magn_core.c
index 3f313aefece6..804353a483c7 100644
--- a/drivers/iio/magnetometer/st_magn_core.c
+++ b/drivers/iio/magnetometer/st_magn_core.c
@@ -12,7 +12,6 @@
12#include <linux/slab.h> 12#include <linux/slab.h>
13#include <linux/errno.h> 13#include <linux/errno.h>
14#include <linux/types.h> 14#include <linux/types.h>
15#include <linux/mutex.h>
16#include <linux/interrupt.h> 15#include <linux/interrupt.h>
17#include <linux/i2c.h> 16#include <linux/i2c.h>
18#include <linux/gpio.h> 17#include <linux/gpio.h>
@@ -496,7 +495,6 @@ int st_magn_common_probe(struct iio_dev *indio_dev)
496 495
497 indio_dev->modes = INDIO_DIRECT_MODE; 496 indio_dev->modes = INDIO_DIRECT_MODE;
498 indio_dev->info = &magn_info; 497 indio_dev->info = &magn_info;
499 mutex_init(&mdata->tb.buf_lock);
500 498
501 err = st_sensors_power_enable(indio_dev); 499 err = st_sensors_power_enable(indio_dev);
502 if (err) 500 if (err)
@@ -507,7 +505,6 @@ int st_magn_common_probe(struct iio_dev *indio_dev)
507 goto st_magn_power_off; 505 goto st_magn_power_off;
508 506
509 mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS; 507 mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS;
510 mdata->multiread_bit = mdata->sensor_settings->multi_read_bit;
511 indio_dev->channels = mdata->sensor_settings->ch; 508 indio_dev->channels = mdata->sensor_settings->ch;
512 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 509 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
513 510
diff --git a/drivers/iio/magnetometer/st_magn_i2c.c b/drivers/iio/magnetometer/st_magn_i2c.c
index d5d565639bed..fdba480a12be 100644
--- a/drivers/iio/magnetometer/st_magn_i2c.c
+++ b/drivers/iio/magnetometer/st_magn_i2c.c
@@ -79,7 +79,9 @@ static int st_magn_i2c_probe(struct i2c_client *client,
79 mdata = iio_priv(indio_dev); 79 mdata = iio_priv(indio_dev);
80 mdata->sensor_settings = (struct st_sensor_settings *)settings; 80 mdata->sensor_settings = (struct st_sensor_settings *)settings;
81 81
82 st_sensors_i2c_configure(indio_dev, client, mdata); 82 err = st_sensors_i2c_configure(indio_dev, client);
83 if (err < 0)
84 return err;
83 85
84 err = st_magn_common_probe(indio_dev); 86 err = st_magn_common_probe(indio_dev);
85 if (err < 0) 87 if (err < 0)
diff --git a/drivers/iio/pressure/st_pressure_buffer.c b/drivers/iio/pressure/st_pressure_buffer.c
index 4566e08a64a1..f21b630abaa0 100644
--- a/drivers/iio/pressure/st_pressure_buffer.c
+++ b/drivers/iio/pressure/st_pressure_buffer.c
@@ -39,7 +39,8 @@ static int st_press_buffer_postenable(struct iio_dev *indio_dev)
39 int err; 39 int err;
40 struct st_sensor_data *press_data = iio_priv(indio_dev); 40 struct st_sensor_data *press_data = iio_priv(indio_dev);
41 41
42 press_data->buffer_data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); 42 press_data->buffer_data = kmalloc(indio_dev->scan_bytes,
43 GFP_DMA | GFP_KERNEL);
43 if (press_data->buffer_data == NULL) { 44 if (press_data->buffer_data == NULL) {
44 err = -ENOMEM; 45 err = -ENOMEM;
45 goto allocate_memory_error; 46 goto allocate_memory_error;
diff --git a/drivers/iio/pressure/st_pressure_core.c b/drivers/iio/pressure/st_pressure_core.c
index a783fc075c26..9ef92a501286 100644
--- a/drivers/iio/pressure/st_pressure_core.c
+++ b/drivers/iio/pressure/st_pressure_core.c
@@ -12,7 +12,6 @@
12#include <linux/slab.h> 12#include <linux/slab.h>
13#include <linux/errno.h> 13#include <linux/errno.h>
14#include <linux/types.h> 14#include <linux/types.h>
15#include <linux/mutex.h>
16#include <linux/interrupt.h> 15#include <linux/interrupt.h>
17#include <linux/i2c.h> 16#include <linux/i2c.h>
18#include <linux/gpio.h> 17#include <linux/gpio.h>
@@ -692,7 +691,6 @@ int st_press_common_probe(struct iio_dev *indio_dev)
692 691
693 indio_dev->modes = INDIO_DIRECT_MODE; 692 indio_dev->modes = INDIO_DIRECT_MODE;
694 indio_dev->info = &press_info; 693 indio_dev->info = &press_info;
695 mutex_init(&press_data->tb.buf_lock);
696 694
697 err = st_sensors_power_enable(indio_dev); 695 err = st_sensors_power_enable(indio_dev);
698 if (err) 696 if (err)
@@ -709,7 +707,6 @@ int st_press_common_probe(struct iio_dev *indio_dev)
709 * element. 707 * element.
710 */ 708 */
711 press_data->num_data_channels = press_data->sensor_settings->num_ch - 1; 709 press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
712 press_data->multiread_bit = press_data->sensor_settings->multi_read_bit;
713 indio_dev->channels = press_data->sensor_settings->ch; 710 indio_dev->channels = press_data->sensor_settings->ch;
714 indio_dev->num_channels = press_data->sensor_settings->num_ch; 711 indio_dev->num_channels = press_data->sensor_settings->num_ch;
715 712
diff --git a/drivers/iio/pressure/st_pressure_i2c.c b/drivers/iio/pressure/st_pressure_i2c.c
index 466e7dde5eae..71d2ed6b4948 100644
--- a/drivers/iio/pressure/st_pressure_i2c.c
+++ b/drivers/iio/pressure/st_pressure_i2c.c
@@ -112,7 +112,9 @@ static int st_press_i2c_probe(struct i2c_client *client,
112 press_data = iio_priv(indio_dev); 112 press_data = iio_priv(indio_dev);
113 press_data->sensor_settings = (struct st_sensor_settings *)settings; 113 press_data->sensor_settings = (struct st_sensor_settings *)settings;
114 114
115 st_sensors_i2c_configure(indio_dev, client, press_data); 115 ret = st_sensors_i2c_configure(indio_dev, client);
116 if (ret < 0)
117 return ret;
116 118
117 ret = st_press_common_probe(indio_dev); 119 ret = st_press_common_probe(indio_dev);
118 if (ret < 0) 120 if (ret < 0)
diff --git a/include/linux/iio/common/st_sensors.h b/include/linux/iio/common/st_sensors.h
index 566b955e2980..28fc1f9fa7d5 100644
--- a/include/linux/iio/common/st_sensors.h
+++ b/include/linux/iio/common/st_sensors.h
@@ -16,6 +16,7 @@
16#include <linux/iio/trigger.h> 16#include <linux/iio/trigger.h>
17#include <linux/bitops.h> 17#include <linux/bitops.h>
18#include <linux/regulator/consumer.h> 18#include <linux/regulator/consumer.h>
19#include <linux/regmap.h>
19 20
20#include <linux/platform_data/st_sensors_pdata.h> 21#include <linux/platform_data/st_sensors_pdata.h>
21 22
@@ -170,36 +171,6 @@ struct st_sensor_data_ready_irq {
170}; 171};
171 172
172/** 173/**
173 * struct st_sensor_transfer_buffer - ST sensor device I/O buffer
174 * @buf_lock: Mutex to protect rx and tx buffers.
175 * @tx_buf: Buffer used by SPI transfer function to send data to the sensors.
176 * This buffer is used to avoid DMA not-aligned issue.
177 * @rx_buf: Buffer used by SPI transfer to receive data from sensors.
178 * This buffer is used to avoid DMA not-aligned issue.
179 */
180struct st_sensor_transfer_buffer {
181 struct mutex buf_lock;
182 u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH];
183 u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned;
184};
185
186/**
187 * struct st_sensor_transfer_function - ST sensor device I/O function
188 * @read_byte: Function used to read one byte.
189 * @write_byte: Function used to write one byte.
190 * @read_multiple_byte: Function used to read multiple byte.
191 */
192struct st_sensor_transfer_function {
193 int (*read_byte) (struct st_sensor_transfer_buffer *tb,
194 struct device *dev, u8 reg_addr, u8 *res_byte);
195 int (*write_byte) (struct st_sensor_transfer_buffer *tb,
196 struct device *dev, u8 reg_addr, u8 data);
197 int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb,
198 struct device *dev, u8 reg_addr, int len, u8 *data,
199 bool multiread_bit);
200};
201
202/**
203 * struct st_sensor_settings - ST specific sensor settings 174 * struct st_sensor_settings - ST specific sensor settings
204 * @wai: Contents of WhoAmI register. 175 * @wai: Contents of WhoAmI register.
205 * @wai_addr: The address of WhoAmI register. 176 * @wai_addr: The address of WhoAmI register.
@@ -242,16 +213,14 @@ struct st_sensor_settings {
242 * @current_fullscale: Maximum range of measure by the sensor. 213 * @current_fullscale: Maximum range of measure by the sensor.
243 * @vdd: Pointer to sensor's Vdd power supply 214 * @vdd: Pointer to sensor's Vdd power supply
244 * @vdd_io: Pointer to sensor's Vdd-IO power supply 215 * @vdd_io: Pointer to sensor's Vdd-IO power supply
216 * @regmap: Pointer to specific sensor regmap configuration.
245 * @enabled: Status of the sensor (false->off, true->on). 217 * @enabled: Status of the sensor (false->off, true->on).
246 * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread.
247 * @buffer_data: Data used by buffer part. 218 * @buffer_data: Data used by buffer part.
248 * @odr: Output data rate of the sensor [Hz]. 219 * @odr: Output data rate of the sensor [Hz].
249 * num_data_channels: Number of data channels used in buffer. 220 * num_data_channels: Number of data channels used in buffer.
250 * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2). 221 * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
251 * @int_pin_open_drain: Set the interrupt/DRDY to open drain. 222 * @int_pin_open_drain: Set the interrupt/DRDY to open drain.
252 * @get_irq_data_ready: Function to get the IRQ used for data ready signal. 223 * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
253 * @tf: Transfer function structure used by I/O operations.
254 * @tb: Transfer buffers and mutex used by I/O operations.
255 * @edge_irq: the IRQ triggers on edges and need special handling. 224 * @edge_irq: the IRQ triggers on edges and need special handling.
256 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor. 225 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
257 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use. 226 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
@@ -264,9 +233,9 @@ struct st_sensor_data {
264 struct st_sensor_fullscale_avl *current_fullscale; 233 struct st_sensor_fullscale_avl *current_fullscale;
265 struct regulator *vdd; 234 struct regulator *vdd;
266 struct regulator *vdd_io; 235 struct regulator *vdd_io;
236 struct regmap *regmap;
267 237
268 bool enabled; 238 bool enabled;
269 bool multiread_bit;
270 239
271 char *buffer_data; 240 char *buffer_data;
272 241
@@ -278,9 +247,6 @@ struct st_sensor_data {
278 247
279 unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev); 248 unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);
280 249
281 const struct st_sensor_transfer_function *tf;
282 struct st_sensor_transfer_buffer tb;
283
284 bool edge_irq; 250 bool edge_irq;
285 bool hw_irq_trigger; 251 bool hw_irq_trigger;
286 s64 hw_timestamp; 252 s64 hw_timestamp;
diff --git a/include/linux/iio/common/st_sensors_i2c.h b/include/linux/iio/common/st_sensors_i2c.h
index 5ada89944698..01e424e2af4f 100644
--- a/include/linux/iio/common/st_sensors_i2c.h
+++ b/include/linux/iio/common/st_sensors_i2c.h
@@ -14,8 +14,8 @@
14#include <linux/iio/common/st_sensors.h> 14#include <linux/iio/common/st_sensors.h>
15#include <linux/of.h> 15#include <linux/of.h>
16 16
17void st_sensors_i2c_configure(struct iio_dev *indio_dev, 17int st_sensors_i2c_configure(struct iio_dev *indio_dev,
18 struct i2c_client *client, struct st_sensor_data *sdata); 18 struct i2c_client *client);
19 19
20#ifdef CONFIG_ACPI 20#ifdef CONFIG_ACPI
21int st_sensors_match_acpi_device(struct device *dev); 21int st_sensors_match_acpi_device(struct device *dev);