st_sensors.h (9760B)
1/* SPDX-License-Identifier: GPL-2.0-only */ 2/* 3 * STMicroelectronics sensors library driver 4 * 5 * Copyright 2012-2013 STMicroelectronics Inc. 6 * 7 * Denis Ciocca <denis.ciocca@st.com> 8 */ 9 10#ifndef ST_SENSORS_H 11#define ST_SENSORS_H 12 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> 21 22#include <linux/platform_data/st_sensors_pdata.h> 23 24#define LSM9DS0_IMU_DEV_NAME "lsm9ds0" 25 26/* 27 * Buffer size max case: 2bytes per channel, 3 channels in total + 28 * 8bytes timestamp channel (s64) 29 */ 30#define ST_SENSORS_MAX_BUFFER_SIZE (ALIGN(2 * 3, sizeof(s64)) + \ 31 sizeof(s64)) 32 33#define ST_SENSORS_ODR_LIST_MAX 10 34#define ST_SENSORS_FULLSCALE_AVL_MAX 10 35 36#define ST_SENSORS_NUMBER_ALL_CHANNELS 4 37#define ST_SENSORS_ENABLE_ALL_AXIS 0x07 38#define ST_SENSORS_SCAN_X 0 39#define ST_SENSORS_SCAN_Y 1 40#define ST_SENSORS_SCAN_Z 2 41#define ST_SENSORS_DEFAULT_POWER_ON_VALUE 0x01 42#define ST_SENSORS_DEFAULT_POWER_OFF_VALUE 0x00 43#define ST_SENSORS_DEFAULT_WAI_ADDRESS 0x0f 44#define ST_SENSORS_DEFAULT_AXIS_ADDR 0x20 45#define ST_SENSORS_DEFAULT_AXIS_MASK 0x07 46#define ST_SENSORS_DEFAULT_AXIS_N_BIT 3 47#define ST_SENSORS_DEFAULT_STAT_ADDR 0x27 48 49#define ST_SENSORS_MAX_NAME 17 50#define ST_SENSORS_MAX_4WAI 8 51 52#define ST_SENSORS_LSM_CHANNELS_EXT(device_type, mask, index, mod, \ 53 ch2, s, endian, rbits, sbits, addr, ext) \ 54{ \ 55 .type = device_type, \ 56 .modified = mod, \ 57 .info_mask_separate = mask, \ 58 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 59 .scan_index = index, \ 60 .channel2 = ch2, \ 61 .address = addr, \ 62 .scan_type = { \ 63 .sign = s, \ 64 .realbits = rbits, \ 65 .shift = sbits - rbits, \ 66 .storagebits = sbits, \ 67 .endianness = endian, \ 68 }, \ 69 .ext_info = ext, \ 70} 71 72#define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \ 73 ch2, s, endian, rbits, sbits, addr) \ 74 ST_SENSORS_LSM_CHANNELS_EXT(device_type, mask, index, mod, \ 75 ch2, s, endian, rbits, sbits, addr, NULL) 76 77#define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \ 78 IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \ 79 st_sensors_sysfs_sampling_frequency_avail) 80 81#define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \ 82 IIO_DEVICE_ATTR(name, S_IRUGO, \ 83 st_sensors_sysfs_scale_avail, NULL , 0); 84 85struct st_sensor_odr_avl { 86 unsigned int hz; 87 u8 value; 88}; 89 90struct st_sensor_odr { 91 u8 addr; 92 u8 mask; 93 struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX]; 94}; 95 96struct st_sensor_power { 97 u8 addr; 98 u8 mask; 99 u8 value_off; 100 u8 value_on; 101}; 102 103struct st_sensor_axis { 104 u8 addr; 105 u8 mask; 106}; 107 108struct st_sensor_fullscale_avl { 109 unsigned int num; 110 u8 value; 111 unsigned int gain; 112 unsigned int gain2; 113}; 114 115struct st_sensor_fullscale { 116 u8 addr; 117 u8 mask; 118 struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX]; 119}; 120 121struct st_sensor_sim { 122 u8 addr; 123 u8 value; 124}; 125 126/** 127 * struct st_sensor_bdu - ST sensor device block data update 128 * @addr: address of the register. 129 * @mask: mask to write the block data update flag. 130 */ 131struct st_sensor_bdu { 132 u8 addr; 133 u8 mask; 134}; 135 136/** 137 * struct st_sensor_das - ST sensor device data alignment selection 138 * @addr: address of the register. 139 * @mask: mask to write the das flag for left alignment. 140 */ 141struct st_sensor_das { 142 u8 addr; 143 u8 mask; 144}; 145 146/** 147 * struct st_sensor_int_drdy - ST sensor device drdy line parameters 148 * @addr: address of INT drdy register. 149 * @mask: mask to enable drdy line. 150 * @addr_od: address to enable/disable Open Drain on the INT line. 151 * @mask_od: mask to enable/disable Open Drain on the INT line. 152 */ 153struct st_sensor_int_drdy { 154 u8 addr; 155 u8 mask; 156 u8 addr_od; 157 u8 mask_od; 158}; 159 160/** 161 * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt 162 * struct int1 - data-ready configuration register for INT1 pin. 163 * struct int2 - data-ready configuration register for INT2 pin. 164 * @addr_ihl: address to enable/disable active low on the INT lines. 165 * @mask_ihl: mask to enable/disable active low on the INT lines. 166 * struct stat_drdy - status register of DRDY (data ready) interrupt. 167 * struct ig1 - represents the Interrupt Generator 1 of sensors. 168 * @en_addr: address of the enable ig1 register. 169 * @en_mask: mask to write the on/off value for enable. 170 */ 171struct st_sensor_data_ready_irq { 172 struct st_sensor_int_drdy int1; 173 struct st_sensor_int_drdy int2; 174 u8 addr_ihl; 175 u8 mask_ihl; 176 struct { 177 u8 addr; 178 u8 mask; 179 } stat_drdy; 180 struct { 181 u8 en_addr; 182 u8 en_mask; 183 } ig1; 184}; 185 186/** 187 * struct st_sensor_settings - ST specific sensor settings 188 * @wai: Contents of WhoAmI register. 189 * @wai_addr: The address of WhoAmI register. 190 * @sensors_supported: List of supported sensors by struct itself. 191 * @ch: IIO channels for the sensor. 192 * @odr: Output data rate register and ODR list available. 193 * @pw: Power register of the sensor. 194 * @enable_axis: Enable one or more axis of the sensor. 195 * @fs: Full scale register and full scale list available. 196 * @bdu: Block data update register. 197 * @das: Data Alignment Selection register. 198 * @drdy_irq: Data ready register of the sensor. 199 * @sim: SPI serial interface mode register of the sensor. 200 * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read. 201 * @bootime: samples to discard when sensor passing from power-down to power-up. 202 */ 203struct st_sensor_settings { 204 u8 wai; 205 u8 wai_addr; 206 char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME]; 207 struct iio_chan_spec *ch; 208 int num_ch; 209 struct st_sensor_odr odr; 210 struct st_sensor_power pw; 211 struct st_sensor_axis enable_axis; 212 struct st_sensor_fullscale fs; 213 struct st_sensor_bdu bdu; 214 struct st_sensor_das das; 215 struct st_sensor_data_ready_irq drdy_irq; 216 struct st_sensor_sim sim; 217 bool multi_read_bit; 218 unsigned int bootime; 219}; 220 221/** 222 * struct st_sensor_data - ST sensor device status 223 * @trig: The trigger in use by the core driver. 224 * @mount_matrix: The mounting matrix of the sensor. 225 * @sensor_settings: Pointer to the specific sensor settings in use. 226 * @current_fullscale: Maximum range of measure by the sensor. 227 * @vdd: Pointer to sensor's Vdd power supply 228 * @vdd_io: Pointer to sensor's Vdd-IO power supply 229 * @regmap: Pointer to specific sensor regmap configuration. 230 * @enabled: Status of the sensor (false->off, true->on). 231 * @odr: Output data rate of the sensor [Hz]. 232 * num_data_channels: Number of data channels used in buffer. 233 * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2). 234 * @int_pin_open_drain: Set the interrupt/DRDY to open drain. 235 * @irq: the IRQ number. 236 * @edge_irq: the IRQ triggers on edges and need special handling. 237 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor. 238 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use. 239 * @buffer_data: Data used by buffer part. 240 * @odr_lock: Local lock for preventing concurrent ODR accesses/changes 241 */ 242struct st_sensor_data { 243 struct iio_trigger *trig; 244 struct iio_mount_matrix mount_matrix; 245 struct st_sensor_settings *sensor_settings; 246 struct st_sensor_fullscale_avl *current_fullscale; 247 struct regulator *vdd; 248 struct regulator *vdd_io; 249 struct regmap *regmap; 250 251 bool enabled; 252 253 unsigned int odr; 254 unsigned int num_data_channels; 255 256 u8 drdy_int_pin; 257 bool int_pin_open_drain; 258 int irq; 259 260 bool edge_irq; 261 bool hw_irq_trigger; 262 s64 hw_timestamp; 263 264 char buffer_data[ST_SENSORS_MAX_BUFFER_SIZE] ____cacheline_aligned; 265 266 struct mutex odr_lock; 267}; 268 269#ifdef CONFIG_IIO_BUFFER 270irqreturn_t st_sensors_trigger_handler(int irq, void *p); 271#endif 272 273#ifdef CONFIG_IIO_TRIGGER 274int st_sensors_allocate_trigger(struct iio_dev *indio_dev, 275 const struct iio_trigger_ops *trigger_ops); 276 277int st_sensors_validate_device(struct iio_trigger *trig, 278 struct iio_dev *indio_dev); 279#else 280static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev, 281 const struct iio_trigger_ops *trigger_ops) 282{ 283 return 0; 284} 285#define st_sensors_validate_device NULL 286#endif 287 288int st_sensors_init_sensor(struct iio_dev *indio_dev, 289 struct st_sensors_platform_data *pdata); 290 291int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable); 292 293int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable); 294 295int st_sensors_power_enable(struct iio_dev *indio_dev); 296 297int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev, 298 unsigned reg, unsigned writeval, 299 unsigned *readval); 300 301int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr); 302 303int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable); 304 305int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale); 306 307int st_sensors_read_info_raw(struct iio_dev *indio_dev, 308 struct iio_chan_spec const *ch, int *val); 309 310int st_sensors_get_settings_index(const char *name, 311 const struct st_sensor_settings *list, 312 const int list_length); 313 314int st_sensors_verify_id(struct iio_dev *indio_dev); 315 316ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev, 317 struct device_attribute *attr, char *buf); 318 319ssize_t st_sensors_sysfs_scale_avail(struct device *dev, 320 struct device_attribute *attr, char *buf); 321 322void st_sensors_dev_name_probe(struct device *dev, char *name, int len); 323 324/* Accelerometer */ 325const struct st_sensor_settings *st_accel_get_settings(const char *name); 326int st_accel_common_probe(struct iio_dev *indio_dev); 327 328/* Gyroscope */ 329const struct st_sensor_settings *st_gyro_get_settings(const char *name); 330int st_gyro_common_probe(struct iio_dev *indio_dev); 331 332/* Magnetometer */ 333const struct st_sensor_settings *st_magn_get_settings(const char *name); 334int st_magn_common_probe(struct iio_dev *indio_dev); 335 336/* Pressure */ 337const struct st_sensor_settings *st_press_get_settings(const char *name); 338int st_press_common_probe(struct iio_dev *indio_dev); 339 340#endif /* ST_SENSORS_H */