aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Ciocca <denis.ciocca@st.com>2019-07-18 18:53:52 -0400
committerJonathan Cameron <Jonathan.Cameron@huawei.com>2019-07-27 17:52:06 -0400
commit1ecd245e0eb23d1c3803474eba75589743d0d1fe (patch)
tree4bb5bd28226e91b1a69bcb8a6b556dd29a245697
parent570c2c55ef364ef6aed531e554be4c989fb1a49c (diff)
iio: move 3-wire spi initialization to st_sensors_spi
Some devices need to be configured with special bit in order to use spi 3-wire. This was done during device identification phase. Instead, let's move this part as spi specific. Doing this the check_device_support function becomes a simple device id check, so let's rename it. 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_core.c4
-rw-r--r--drivers/iio/accel/st_accel_spi.c4
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_core.c64
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_spi.c65
-rw-r--r--drivers/iio/gyro/st_gyro_core.c4
-rw-r--r--drivers/iio/gyro/st_gyro_spi.c4
-rw-r--r--drivers/iio/magnetometer/st_magn_core.c4
-rw-r--r--drivers/iio/magnetometer/st_magn_spi.c4
-rw-r--r--drivers/iio/pressure/st_pressure_core.c4
-rw-r--r--drivers/iio/pressure/st_pressure_spi.c4
-rw-r--r--include/linux/iio/common/st_sensors.h3
-rw-r--r--include/linux/iio/common/st_sensors_spi.h4
12 files changed, 97 insertions, 71 deletions
diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c
index 6fc490ffef7e..630909702a19 100644
--- a/drivers/iio/accel/st_accel_core.c
+++ b/drivers/iio/accel/st_accel_core.c
@@ -1183,9 +1183,7 @@ int st_accel_common_probe(struct iio_dev *indio_dev)
1183 if (err) 1183 if (err)
1184 return err; 1184 return err;
1185 1185
1186 err = st_sensors_check_device_support(indio_dev, 1186 err = st_sensors_verify_id(indio_dev);
1187 ARRAY_SIZE(st_accel_sensors_settings),
1188 st_accel_sensors_settings);
1189 if (err < 0) 1187 if (err < 0)
1190 goto st_accel_power_off; 1188 goto st_accel_power_off;
1191 1189
diff --git a/drivers/iio/accel/st_accel_spi.c b/drivers/iio/accel/st_accel_spi.c
index c0556db9d60a..8af7027d5598 100644
--- a/drivers/iio/accel/st_accel_spi.c
+++ b/drivers/iio/accel/st_accel_spi.c
@@ -124,7 +124,9 @@ static int st_accel_spi_probe(struct spi_device *spi)
124 adata = iio_priv(indio_dev); 124 adata = iio_priv(indio_dev);
125 adata->sensor_settings = (struct st_sensor_settings *)settings; 125 adata->sensor_settings = (struct st_sensor_settings *)settings;
126 126
127 st_sensors_spi_configure(indio_dev, spi, adata); 127 err = st_sensors_spi_configure(indio_dev, spi);
128 if (err < 0)
129 return err;
128 130
129 err = st_accel_common_probe(indio_dev); 131 err = st_accel_common_probe(indio_dev);
130 if (err < 0) 132 if (err < 0)
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index 3610ca9eaa87..f05bf7cf0594 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -608,31 +608,6 @@ out:
608} 608}
609EXPORT_SYMBOL(st_sensors_read_info_raw); 609EXPORT_SYMBOL(st_sensors_read_info_raw);
610 610
611static int st_sensors_init_interface_mode(struct iio_dev *indio_dev,
612 const struct st_sensor_settings *sensor_settings)
613{
614 struct st_sensor_data *sdata = iio_priv(indio_dev);
615 struct device_node *np = sdata->dev->of_node;
616 struct st_sensors_platform_data *pdata;
617
618 pdata = (struct st_sensors_platform_data *)sdata->dev->platform_data;
619 if (((np && of_property_read_bool(np, "spi-3wire")) ||
620 (pdata && pdata->spi_3wire)) && sensor_settings->sim.addr) {
621 int err;
622
623 err = sdata->tf->write_byte(&sdata->tb, sdata->dev,
624 sensor_settings->sim.addr,
625 sensor_settings->sim.value);
626 if (err < 0) {
627 dev_err(&indio_dev->dev,
628 "failed to init interface mode\n");
629 return err;
630 }
631 }
632
633 return 0;
634}
635
636/* 611/*
637 * st_sensors_get_settings_index() - get index of the sensor settings for a 612 * st_sensors_get_settings_index() - get index of the sensor settings for a
638 * specific device from list of settings 613 * specific device from list of settings
@@ -660,36 +635,30 @@ int st_sensors_get_settings_index(const char *name,
660} 635}
661EXPORT_SYMBOL(st_sensors_get_settings_index); 636EXPORT_SYMBOL(st_sensors_get_settings_index);
662 637
663int st_sensors_check_device_support(struct iio_dev *indio_dev, 638/*
664 int num_sensors_list, 639 * st_sensors_verify_id() - verify sensor ID (WhoAmI) is matching with the
665 const struct st_sensor_settings *sensor_settings) 640 * expected value
641 * @indio_dev: IIO device reference.
642 *
643 * Return: 0 on success (valid sensor ID), else a negative error code.
644 */
645int st_sensors_verify_id(struct iio_dev *indio_dev)
666{ 646{
667 struct st_sensor_data *sdata = iio_priv(indio_dev); 647 struct st_sensor_data *sdata = iio_priv(indio_dev);
668 int i, err; 648 int err;
669 u8 wai; 649 u8 wai;
670 650
671 i = st_sensors_get_settings_index(indio_dev->name, 651 if (sdata->sensor_settings->wai_addr) {
672 sensor_settings, num_sensors_list);
673 if (i < 0) {
674 dev_err(&indio_dev->dev, "device name %s not recognized.\n",
675 indio_dev->name);
676 return i;
677 }
678
679 err = st_sensors_init_interface_mode(indio_dev, &sensor_settings[i]);
680 if (err < 0)
681 return err;
682
683 if (sensor_settings[i].wai_addr) {
684 err = sdata->tf->read_byte(&sdata->tb, sdata->dev, 652 err = sdata->tf->read_byte(&sdata->tb, sdata->dev,
685 sensor_settings[i].wai_addr, &wai); 653 sdata->sensor_settings->wai_addr,
654 &wai);
686 if (err < 0) { 655 if (err < 0) {
687 dev_err(&indio_dev->dev, 656 dev_err(&indio_dev->dev,
688 "failed to read Who-Am-I register.\n"); 657 "failed to read Who-Am-I register.\n");
689 return err; 658 return err;
690 } 659 }
691 660
692 if (sensor_settings[i].wai != wai) { 661 if (sdata->sensor_settings->wai != wai) {
693 dev_err(&indio_dev->dev, 662 dev_err(&indio_dev->dev,
694 "%s: WhoAmI mismatch (0x%x).\n", 663 "%s: WhoAmI mismatch (0x%x).\n",
695 indio_dev->name, wai); 664 indio_dev->name, wai);
@@ -697,12 +666,9 @@ int st_sensors_check_device_support(struct iio_dev *indio_dev,
697 } 666 }
698 } 667 }
699 668
700 sdata->sensor_settings = 669 return 0;
701 (struct st_sensor_settings *)&sensor_settings[i];
702
703 return i;
704} 670}
705EXPORT_SYMBOL(st_sensors_check_device_support); 671EXPORT_SYMBOL(st_sensors_verify_id);
706 672
707ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev, 673ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
708 struct device_attribute *attr, char *buf) 674 struct device_attribute *attr, char *buf)
diff --git a/drivers/iio/common/st_sensors/st_sensors_spi.c b/drivers/iio/common/st_sensors/st_sensors_spi.c
index 2213843f02cb..a57cd648975c 100644
--- a/drivers/iio/common/st_sensors/st_sensors_spi.c
+++ b/drivers/iio/common/st_sensors/st_sensors_spi.c
@@ -102,9 +102,68 @@ static const struct st_sensor_transfer_function st_sensors_tf_spi = {
102 .read_multiple_byte = st_sensors_spi_read_multiple_byte, 102 .read_multiple_byte = st_sensors_spi_read_multiple_byte,
103}; 103};
104 104
105void st_sensors_spi_configure(struct iio_dev *indio_dev, 105/*
106 struct spi_device *spi, struct st_sensor_data *sdata) 106 * st_sensors_is_spi_3_wire() - check if SPI 3-wire mode has been selected
107 * @spi: spi device reference.
108 *
109 * Return: true if SPI 3-wire mode is selected, false otherwise.
110 */
111static bool st_sensors_is_spi_3_wire(struct spi_device *spi)
112{
113 struct device_node *np = spi->dev.of_node;
114 struct st_sensors_platform_data *pdata;
115
116 pdata = (struct st_sensors_platform_data *)spi->dev.platform_data;
117 if ((np && of_property_read_bool(np, "spi-3wire")) ||
118 (pdata && pdata->spi_3wire)) {
119 return true;
120 }
121
122 return false;
123}
124
125/*
126 * st_sensors_configure_spi_3_wire() - configure SPI 3-wire if needed
127 * @spi: spi device reference.
128 * @settings: sensor specific settings reference.
129 *
130 * Return: 0 on success, else a negative error code.
131 */
132static int st_sensors_configure_spi_3_wire(struct spi_device *spi,
133 struct st_sensor_settings *settings)
134{
135 if (settings->sim.addr) {
136 u8 buffer[] = {
137 settings->sim.addr,
138 settings->sim.value
139 };
140
141 return spi_write(spi, buffer, 2);
142 }
143
144 return 0;
145}
146
147/*
148 * st_sensors_spi_configure() - configure SPI interface
149 * @indio_dev: IIO device reference.
150 * @spi: spi device reference.
151 *
152 * Return: 0 on success, else a negative error code.
153 */
154int st_sensors_spi_configure(struct iio_dev *indio_dev,
155 struct spi_device *spi)
107{ 156{
157 struct st_sensor_data *sdata = iio_priv(indio_dev);
158 int err;
159
160 if (st_sensors_is_spi_3_wire(spi)) {
161 err = st_sensors_configure_spi_3_wire(spi,
162 sdata->sensor_settings);
163 if (err < 0)
164 return err;
165 }
166
108 spi_set_drvdata(spi, indio_dev); 167 spi_set_drvdata(spi, indio_dev);
109 168
110 indio_dev->dev.parent = &spi->dev; 169 indio_dev->dev.parent = &spi->dev;
@@ -113,6 +172,8 @@ void st_sensors_spi_configure(struct iio_dev *indio_dev,
113 sdata->dev = &spi->dev; 172 sdata->dev = &spi->dev;
114 sdata->tf = &st_sensors_tf_spi; 173 sdata->tf = &st_sensors_tf_spi;
115 sdata->get_irq_data_ready = st_sensors_spi_get_irq; 174 sdata->get_irq_data_ready = st_sensors_spi_get_irq;
175
176 return 0;
116} 177}
117EXPORT_SYMBOL(st_sensors_spi_configure); 178EXPORT_SYMBOL(st_sensors_spi_configure);
118 179
diff --git a/drivers/iio/gyro/st_gyro_core.c b/drivers/iio/gyro/st_gyro_core.c
index 5cc63d41d855..4b87e79aa744 100644
--- a/drivers/iio/gyro/st_gyro_core.c
+++ b/drivers/iio/gyro/st_gyro_core.c
@@ -400,9 +400,7 @@ int st_gyro_common_probe(struct iio_dev *indio_dev)
400 if (err) 400 if (err)
401 return err; 401 return err;
402 402
403 err = st_sensors_check_device_support(indio_dev, 403 err = st_sensors_verify_id(indio_dev);
404 ARRAY_SIZE(st_gyro_sensors_settings),
405 st_gyro_sensors_settings);
406 if (err < 0) 404 if (err < 0)
407 goto st_gyro_power_off; 405 goto st_gyro_power_off;
408 406
diff --git a/drivers/iio/gyro/st_gyro_spi.c b/drivers/iio/gyro/st_gyro_spi.c
index bb7082055f85..b5c624251231 100644
--- a/drivers/iio/gyro/st_gyro_spi.c
+++ b/drivers/iio/gyro/st_gyro_spi.c
@@ -91,7 +91,9 @@ static int st_gyro_spi_probe(struct spi_device *spi)
91 gdata = iio_priv(indio_dev); 91 gdata = iio_priv(indio_dev);
92 gdata->sensor_settings = (struct st_sensor_settings *)settings; 92 gdata->sensor_settings = (struct st_sensor_settings *)settings;
93 93
94 st_sensors_spi_configure(indio_dev, spi, gdata); 94 err = st_sensors_spi_configure(indio_dev, spi);
95 if (err < 0)
96 return err;
95 97
96 err = st_gyro_common_probe(indio_dev); 98 err = st_gyro_common_probe(indio_dev);
97 if (err < 0) 99 if (err < 0)
diff --git a/drivers/iio/magnetometer/st_magn_core.c b/drivers/iio/magnetometer/st_magn_core.c
index 43a49a52c81a..3f313aefece6 100644
--- a/drivers/iio/magnetometer/st_magn_core.c
+++ b/drivers/iio/magnetometer/st_magn_core.c
@@ -502,9 +502,7 @@ int st_magn_common_probe(struct iio_dev *indio_dev)
502 if (err) 502 if (err)
503 return err; 503 return err;
504 504
505 err = st_sensors_check_device_support(indio_dev, 505 err = st_sensors_verify_id(indio_dev);
506 ARRAY_SIZE(st_magn_sensors_settings),
507 st_magn_sensors_settings);
508 if (err < 0) 506 if (err < 0)
509 goto st_magn_power_off; 507 goto st_magn_power_off;
510 508
diff --git a/drivers/iio/magnetometer/st_magn_spi.c b/drivers/iio/magnetometer/st_magn_spi.c
index a3045afc6b53..fbf909bde841 100644
--- a/drivers/iio/magnetometer/st_magn_spi.c
+++ b/drivers/iio/magnetometer/st_magn_spi.c
@@ -73,7 +73,9 @@ static int st_magn_spi_probe(struct spi_device *spi)
73 mdata = iio_priv(indio_dev); 73 mdata = iio_priv(indio_dev);
74 mdata->sensor_settings = (struct st_sensor_settings *)settings; 74 mdata->sensor_settings = (struct st_sensor_settings *)settings;
75 75
76 st_sensors_spi_configure(indio_dev, spi, mdata); 76 err = st_sensors_spi_configure(indio_dev, spi);
77 if (err < 0)
78 return err;
77 79
78 err = st_magn_common_probe(indio_dev); 80 err = st_magn_common_probe(indio_dev);
79 if (err < 0) 81 if (err < 0)
diff --git a/drivers/iio/pressure/st_pressure_core.c b/drivers/iio/pressure/st_pressure_core.c
index 35d80ff27464..a783fc075c26 100644
--- a/drivers/iio/pressure/st_pressure_core.c
+++ b/drivers/iio/pressure/st_pressure_core.c
@@ -698,9 +698,7 @@ int st_press_common_probe(struct iio_dev *indio_dev)
698 if (err) 698 if (err)
699 return err; 699 return err;
700 700
701 err = st_sensors_check_device_support(indio_dev, 701 err = st_sensors_verify_id(indio_dev);
702 ARRAY_SIZE(st_press_sensors_settings),
703 st_press_sensors_settings);
704 if (err < 0) 702 if (err < 0)
705 goto st_press_power_off; 703 goto st_press_power_off;
706 704
diff --git a/drivers/iio/pressure/st_pressure_spi.c b/drivers/iio/pressure/st_pressure_spi.c
index 3e8c1ffe001e..7c8b70221e70 100644
--- a/drivers/iio/pressure/st_pressure_spi.c
+++ b/drivers/iio/pressure/st_pressure_spi.c
@@ -83,7 +83,9 @@ static int st_press_spi_probe(struct spi_device *spi)
83 press_data = iio_priv(indio_dev); 83 press_data = iio_priv(indio_dev);
84 press_data->sensor_settings = (struct st_sensor_settings *)settings; 84 press_data->sensor_settings = (struct st_sensor_settings *)settings;
85 85
86 st_sensors_spi_configure(indio_dev, spi, press_data); 86 err = st_sensors_spi_configure(indio_dev, spi);
87 if (err < 0)
88 return err;
87 89
88 err = st_press_common_probe(indio_dev); 90 err = st_press_common_probe(indio_dev);
89 if (err < 0) 91 if (err < 0)
diff --git a/include/linux/iio/common/st_sensors.h b/include/linux/iio/common/st_sensors.h
index 17fbf3e9b013..566b955e2980 100644
--- a/include/linux/iio/common/st_sensors.h
+++ b/include/linux/iio/common/st_sensors.h
@@ -338,8 +338,7 @@ int st_sensors_get_settings_index(const char *name,
338 const struct st_sensor_settings *list, 338 const struct st_sensor_settings *list,
339 const int list_length); 339 const int list_length);
340 340
341int st_sensors_check_device_support(struct iio_dev *indio_dev, 341int st_sensors_verify_id(struct iio_dev *indio_dev);
342 int num_sensors_list, const struct st_sensor_settings *sensor_settings);
343 342
344ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev, 343ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
345 struct device_attribute *attr, char *buf); 344 struct device_attribute *attr, char *buf);
diff --git a/include/linux/iio/common/st_sensors_spi.h b/include/linux/iio/common/st_sensors_spi.h
index 6020f7167859..90b25f087f06 100644
--- a/include/linux/iio/common/st_sensors_spi.h
+++ b/include/linux/iio/common/st_sensors_spi.h
@@ -13,7 +13,7 @@
13#include <linux/spi/spi.h> 13#include <linux/spi/spi.h>
14#include <linux/iio/common/st_sensors.h> 14#include <linux/iio/common/st_sensors.h>
15 15
16void st_sensors_spi_configure(struct iio_dev *indio_dev, 16int st_sensors_spi_configure(struct iio_dev *indio_dev,
17 struct spi_device *spi, struct st_sensor_data *sdata); 17 struct spi_device *spi);
18 18
19#endif /* ST_SENSORS_SPI_H */ 19#endif /* ST_SENSORS_SPI_H */