1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics sensors spi library driver
5 * Copyright 2012-2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/iio/iio.h>
13 #include <linux/property.h>
14 #include <linux/regmap.h>
15 #include <linux/spi/spi.h>
17 #include <linux/iio/common/st_sensors_spi.h>
19 #define ST_SENSORS_SPI_MULTIREAD 0xc0
21 static const struct regmap_config st_sensors_spi_regmap_config
= {
26 static const struct regmap_config st_sensors_spi_regmap_multiread_bit_config
= {
29 .read_flag_mask
= ST_SENSORS_SPI_MULTIREAD
,
33 * st_sensors_is_spi_3_wire() - check if SPI 3-wire mode has been selected
34 * @spi: spi device reference.
36 * Return: true if SPI 3-wire mode is selected, false otherwise.
38 static bool st_sensors_is_spi_3_wire(struct spi_device
*spi
)
40 struct st_sensors_platform_data
*pdata
;
41 struct device
*dev
= &spi
->dev
;
43 if (device_property_read_bool(dev
, "spi-3wire"))
46 pdata
= dev_get_platdata(dev
);
47 if (pdata
&& pdata
->spi_3wire
)
54 * st_sensors_configure_spi_3_wire() - configure SPI 3-wire if needed
55 * @spi: spi device reference.
56 * @settings: sensor specific settings reference.
58 * Return: 0 on success, else a negative error code.
60 static int st_sensors_configure_spi_3_wire(struct spi_device
*spi
,
61 struct st_sensor_settings
*settings
)
63 if (settings
->sim
.addr
) {
69 return spi_write(spi
, buffer
, 2);
76 * st_sensors_spi_configure() - configure SPI interface
77 * @indio_dev: IIO device reference.
78 * @spi: spi device reference.
80 * Return: 0 on success, else a negative error code.
82 int st_sensors_spi_configure(struct iio_dev
*indio_dev
,
83 struct spi_device
*spi
)
85 struct st_sensor_data
*sdata
= iio_priv(indio_dev
);
86 const struct regmap_config
*config
;
89 if (st_sensors_is_spi_3_wire(spi
)) {
90 err
= st_sensors_configure_spi_3_wire(spi
,
91 sdata
->sensor_settings
);
96 if (sdata
->sensor_settings
->multi_read_bit
)
97 config
= &st_sensors_spi_regmap_multiread_bit_config
;
99 config
= &st_sensors_spi_regmap_config
;
101 sdata
->regmap
= devm_regmap_init_spi(spi
, config
);
102 if (IS_ERR(sdata
->regmap
)) {
103 dev_err(&spi
->dev
, "Failed to register spi regmap (%ld)\n",
104 PTR_ERR(sdata
->regmap
));
105 return PTR_ERR(sdata
->regmap
);
108 spi_set_drvdata(spi
, indio_dev
);
110 indio_dev
->name
= spi
->modalias
;
112 sdata
->irq
= spi
->irq
;
116 EXPORT_SYMBOL_NS(st_sensors_spi_configure
, "IIO_ST_SENSORS");
118 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
119 MODULE_DESCRIPTION("STMicroelectronics ST-sensors spi driver");
120 MODULE_LICENSE("GPL v2");