1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * STMicroelectronics sensors library driver
5 * Copyright 2012-2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
13 #include <linux/i2c.h>
14 #include <linux/spi/spi.h>
15 #include <linux/irqreturn.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/trigger.h>
18 #include <linux/bitops.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/regmap.h>
22 #include <linux/platform_data/st_sensors_pdata.h>
24 #define LSM9DS0_IMU_DEV_NAME "lsm9ds0"
25 #define LSM303D_IMU_DEV_NAME "lsm303d"
28 * Buffer size max case: 2bytes per channel, 3 channels in total +
29 * 8bytes timestamp channel (s64)
31 #define ST_SENSORS_MAX_BUFFER_SIZE (ALIGN(2 * 3, sizeof(s64)) + \
34 #define ST_SENSORS_ODR_LIST_MAX 10
35 #define ST_SENSORS_FULLSCALE_AVL_MAX 10
37 #define ST_SENSORS_NUMBER_ALL_CHANNELS 4
38 #define ST_SENSORS_ENABLE_ALL_AXIS 0x07
39 #define ST_SENSORS_SCAN_X 0
40 #define ST_SENSORS_SCAN_Y 1
41 #define ST_SENSORS_SCAN_Z 2
42 #define ST_SENSORS_DEFAULT_POWER_ON_VALUE 0x01
43 #define ST_SENSORS_DEFAULT_POWER_OFF_VALUE 0x00
44 #define ST_SENSORS_DEFAULT_WAI_ADDRESS 0x0f
45 #define ST_SENSORS_DEFAULT_AXIS_ADDR 0x20
46 #define ST_SENSORS_DEFAULT_AXIS_MASK 0x07
47 #define ST_SENSORS_DEFAULT_AXIS_N_BIT 3
48 #define ST_SENSORS_DEFAULT_STAT_ADDR 0x27
50 #define ST_SENSORS_MAX_NAME 17
51 #define ST_SENSORS_MAX_4WAI 8
53 #define ST_SENSORS_LSM_CHANNELS_EXT(device_type, mask, index, mod, \
54 ch2, s, endian, rbits, sbits, addr, ext) \
56 .type = device_type, \
58 .info_mask_separate = mask, \
59 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
60 .scan_index = index, \
66 .shift = sbits - rbits, \
67 .storagebits = sbits, \
68 .endianness = endian, \
73 #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
74 ch2, s, endian, rbits, sbits, addr) \
75 ST_SENSORS_LSM_CHANNELS_EXT(device_type, mask, index, mod, \
76 ch2, s, endian, rbits, sbits, addr, NULL)
78 #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
79 IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
80 st_sensors_sysfs_sampling_frequency_avail)
82 #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
83 IIO_DEVICE_ATTR(name, S_IRUGO, \
84 st_sensors_sysfs_scale_avail, NULL , 0);
86 struct st_sensor_odr_avl {
91 struct st_sensor_odr {
94 struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
97 struct st_sensor_power {
104 struct st_sensor_axis {
109 struct st_sensor_fullscale_avl {
116 struct st_sensor_fullscale {
119 struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
122 struct st_sensor_sim {
128 * struct st_sensor_bdu - ST sensor device block data update
129 * @addr: address of the register.
130 * @mask: mask to write the block data update flag.
132 struct st_sensor_bdu {
138 * struct st_sensor_das - ST sensor device data alignment selection
139 * @addr: address of the register.
140 * @mask: mask to write the das flag for left alignment.
142 struct st_sensor_das {
148 * struct st_sensor_int_drdy - ST sensor device drdy line parameters
149 * @addr: address of INT drdy register.
150 * @mask: mask to enable drdy line.
151 * @addr_od: address to enable/disable Open Drain on the INT line.
152 * @mask_od: mask to enable/disable Open Drain on the INT line.
154 struct st_sensor_int_drdy {
162 * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
163 * struct int1 - data-ready configuration register for INT1 pin.
164 * struct int2 - data-ready configuration register for INT2 pin.
165 * @addr_ihl: address to enable/disable active low on the INT lines.
166 * @mask_ihl: mask to enable/disable active low on the INT lines.
167 * struct stat_drdy - status register of DRDY (data ready) interrupt.
168 * struct ig1 - represents the Interrupt Generator 1 of sensors.
169 * @en_addr: address of the enable ig1 register.
170 * @en_mask: mask to write the on/off value for enable.
172 struct st_sensor_data_ready_irq {
173 struct st_sensor_int_drdy int1;
174 struct st_sensor_int_drdy int2;
188 * struct st_sensor_settings - ST specific sensor settings
189 * @wai: Contents of WhoAmI register.
190 * @wai_addr: The address of WhoAmI register.
191 * @sensors_supported: List of supported sensors by struct itself.
192 * @ch: IIO channels for the sensor.
193 * @odr: Output data rate register and ODR list available.
194 * @pw: Power register of the sensor.
195 * @enable_axis: Enable one or more axis of the sensor.
196 * @fs: Full scale register and full scale list available.
197 * @bdu: Block data update register.
198 * @das: Data Alignment Selection register.
199 * @drdy_irq: Data ready register of the sensor.
200 * @sim: SPI serial interface mode register of the sensor.
201 * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
202 * @bootime: samples to discard when sensor passing from power-down to power-up.
204 struct st_sensor_settings {
207 char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
208 struct iio_chan_spec *ch;
210 struct st_sensor_odr odr;
211 struct st_sensor_power pw;
212 struct st_sensor_axis enable_axis;
213 struct st_sensor_fullscale fs;
214 struct st_sensor_bdu bdu;
215 struct st_sensor_das das;
216 struct st_sensor_data_ready_irq drdy_irq;
217 struct st_sensor_sim sim;
219 unsigned int bootime;
223 * struct st_sensor_data - ST sensor device status
224 * @trig: The trigger in use by the core driver.
225 * @mount_matrix: The mounting matrix of the sensor.
226 * @sensor_settings: Pointer to the specific sensor settings in use.
227 * @current_fullscale: Maximum range of measure by the sensor.
228 * @regmap: Pointer to specific sensor regmap configuration.
229 * @enabled: Status of the sensor (false->off, true->on).
230 * @odr: Output data rate of the sensor [Hz].
231 * num_data_channels: Number of data channels used in buffer.
232 * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
233 * @int_pin_open_drain: Set the interrupt/DRDY to open drain.
234 * @irq: the IRQ number.
235 * @edge_irq: the IRQ triggers on edges and need special handling.
236 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
237 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
238 * @buffer_data: Data used by buffer part.
239 * @odr_lock: Local lock for preventing concurrent ODR accesses/changes
241 struct st_sensor_data {
242 struct iio_trigger *trig;
243 struct iio_mount_matrix mount_matrix;
244 struct st_sensor_settings *sensor_settings;
245 struct st_sensor_fullscale_avl *current_fullscale;
246 struct regmap *regmap;
251 unsigned int num_data_channels;
254 bool int_pin_open_drain;
261 struct mutex odr_lock;
263 char buffer_data[ST_SENSORS_MAX_BUFFER_SIZE] __aligned(IIO_DMA_MINALIGN);
266 #ifdef CONFIG_IIO_BUFFER
267 irqreturn_t st_sensors_trigger_handler(int irq, void *p);
270 #ifdef CONFIG_IIO_TRIGGER
271 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
272 const struct iio_trigger_ops *trigger_ops);
274 int st_sensors_validate_device(struct iio_trigger *trig,
275 struct iio_dev *indio_dev);
277 static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
278 const struct iio_trigger_ops *trigger_ops)
282 #define st_sensors_validate_device NULL
285 int st_sensors_init_sensor(struct iio_dev *indio_dev,
286 struct st_sensors_platform_data *pdata);
288 int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
290 int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
292 int st_sensors_power_enable(struct iio_dev *indio_dev);
294 int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
295 unsigned reg, unsigned writeval,
298 int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
300 int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
302 int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
304 int st_sensors_read_info_raw(struct iio_dev *indio_dev,
305 struct iio_chan_spec const *ch, int *val);
307 int st_sensors_get_settings_index(const char *name,
308 const struct st_sensor_settings *list,
309 const int list_length);
311 int st_sensors_verify_id(struct iio_dev *indio_dev);
313 ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
314 struct device_attribute *attr, char *buf);
316 ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
317 struct device_attribute *attr, char *buf);
319 void st_sensors_dev_name_probe(struct device *dev, char *name, int len);
322 const struct st_sensor_settings *st_accel_get_settings(const char *name);
323 int st_accel_common_probe(struct iio_dev *indio_dev);
326 const struct st_sensor_settings *st_gyro_get_settings(const char *name);
327 int st_gyro_common_probe(struct iio_dev *indio_dev);
330 const struct st_sensor_settings *st_magn_get_settings(const char *name);
331 int st_magn_common_probe(struct iio_dev *indio_dev);
334 const struct st_sensor_settings *st_press_get_settings(const char *name);
335 int st_press_common_probe(struct iio_dev *indio_dev);
337 #endif /* ST_SENSORS_H */