This source file includes following definitions.
- st_sensors_is_spi_3_wire
- st_sensors_configure_spi_3_wire
- st_sensors_spi_configure
1
2
3
4
5
6
7
8
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/iio/iio.h>
14 #include <linux/regmap.h>
15
16 #include <linux/iio/common/st_sensors_spi.h>
17 #include "st_sensors_core.h"
18
19 #define ST_SENSORS_SPI_MULTIREAD 0xc0
20
21 static const struct regmap_config st_sensors_spi_regmap_config = {
22 .reg_bits = 8,
23 .val_bits = 8,
24 };
25
26 static const struct regmap_config st_sensors_spi_regmap_multiread_bit_config = {
27 .reg_bits = 8,
28 .val_bits = 8,
29 .read_flag_mask = ST_SENSORS_SPI_MULTIREAD,
30 };
31
32
33
34
35
36
37
38 static bool st_sensors_is_spi_3_wire(struct spi_device *spi)
39 {
40 struct device_node *np = spi->dev.of_node;
41 struct st_sensors_platform_data *pdata;
42
43 pdata = (struct st_sensors_platform_data *)spi->dev.platform_data;
44 if ((np && of_property_read_bool(np, "spi-3wire")) ||
45 (pdata && pdata->spi_3wire)) {
46 return true;
47 }
48
49 return false;
50 }
51
52
53
54
55
56
57
58
59 static int st_sensors_configure_spi_3_wire(struct spi_device *spi,
60 struct st_sensor_settings *settings)
61 {
62 if (settings->sim.addr) {
63 u8 buffer[] = {
64 settings->sim.addr,
65 settings->sim.value
66 };
67
68 return spi_write(spi, buffer, 2);
69 }
70
71 return 0;
72 }
73
74
75
76
77
78
79
80
81 int st_sensors_spi_configure(struct iio_dev *indio_dev,
82 struct spi_device *spi)
83 {
84 struct st_sensor_data *sdata = iio_priv(indio_dev);
85 const struct regmap_config *config;
86 int err;
87
88 if (st_sensors_is_spi_3_wire(spi)) {
89 err = st_sensors_configure_spi_3_wire(spi,
90 sdata->sensor_settings);
91 if (err < 0)
92 return err;
93 }
94
95 if (sdata->sensor_settings->multi_read_bit)
96 config = &st_sensors_spi_regmap_multiread_bit_config;
97 else
98 config = &st_sensors_spi_regmap_config;
99
100 sdata->regmap = devm_regmap_init_spi(spi, config);
101 if (IS_ERR(sdata->regmap)) {
102 dev_err(&spi->dev, "Failed to register spi regmap (%d)\n",
103 (int)PTR_ERR(sdata->regmap));
104 return PTR_ERR(sdata->regmap);
105 }
106
107 spi_set_drvdata(spi, indio_dev);
108
109 indio_dev->dev.parent = &spi->dev;
110 indio_dev->name = spi->modalias;
111
112 sdata->dev = &spi->dev;
113 sdata->irq = spi->irq;
114
115 return 0;
116 }
117 EXPORT_SYMBOL(st_sensors_spi_configure);
118
119 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
120 MODULE_DESCRIPTION("STMicroelectronics ST-sensors spi driver");
121 MODULE_LICENSE("GPL v2");