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 #define LSM303D_IMU_DEV_NAME "lsm303d" 26 27 /* 28 * Buffer size max case: 2bytes per channel, 3 channels in total + 29 * 8bytes timestamp channel (s64) 30 */ 31 #define ST_SENSORS_MAX_BUFFER_SIZE (ALIGN(2 * 3, sizeof(s64)) + \ 32 sizeof(s64)) 33 34 #define ST_SENSORS_ODR_LIST_MAX 10 35 #define ST_SENSORS_FULLSCALE_AVL_MAX 10 36 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 49 50 #define ST_SENSORS_MAX_NAME 17 51 #define ST_SENSORS_MAX_4WAI 8 52 53 #define ST_SENSORS_LSM_CHANNELS_EXT(device_type, mask, index, mod, \ 54 ch2, s, endian, rbits, sbits, addr, ext) \ 55 { \ 56 .type = device_type, \ 57 .modified = mod, \ 58 .info_mask_separate = mask, \ 59 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 60 .scan_index = index, \ 61 .channel2 = ch2, \ 62 .address = addr, \ 63 .scan_type = { \ 64 .sign = s, \ 65 .realbits = rbits, \ 66 .shift = sbits - rbits, \ 67 .storagebits = sbits, \ 68 .endianness = endian, \ 69 }, \ 70 .ext_info = ext, \ 71 } 72 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) 77 78 #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \ 79 IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \ 80 st_sensors_sysfs_sampling_frequency_avail) 81 82 #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \ 83 IIO_DEVICE_ATTR(name, S_IRUGO, \ 84 st_sensors_sysfs_scale_avail, NULL , 0); 85 86 struct st_sensor_odr_avl { 87 unsigned int hz; 88 u8 value; 89 }; 90 91 struct st_sensor_odr { 92 u8 addr; 93 u8 mask; 94 struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX]; 95 }; 96 97 struct st_sensor_power { 98 u8 addr; 99 u8 mask; 100 u8 value_off; 101 u8 value_on; 102 }; 103 104 struct st_sensor_axis { 105 u8 addr; 106 u8 mask; 107 }; 108 109 struct st_sensor_fullscale_avl { 110 unsigned int num; 111 u8 value; 112 unsigned int gain; 113 unsigned int gain2; 114 }; 115 116 struct st_sensor_fullscale { 117 u8 addr; 118 u8 mask; 119 struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX]; 120 }; 121 122 struct st_sensor_sim { 123 u8 addr; 124 u8 value; 125 }; 126 127 /** 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. 131 */ 132 struct st_sensor_bdu { 133 u8 addr; 134 u8 mask; 135 }; 136 137 /** 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. 141 */ 142 struct st_sensor_das { 143 u8 addr; 144 u8 mask; 145 }; 146 147 /** 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. 153 */ 154 struct st_sensor_int_drdy { 155 u8 addr; 156 u8 mask; 157 u8 addr_od; 158 u8 mask_od; 159 }; 160 161 /** 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. 171 */ 172 struct st_sensor_data_ready_irq { 173 struct st_sensor_int_drdy int1; 174 struct st_sensor_int_drdy int2; 175 u8 addr_ihl; 176 u8 mask_ihl; 177 struct { 178 u8 addr; 179 u8 mask; 180 } stat_drdy; 181 struct { 182 u8 en_addr; 183 u8 en_mask; 184 } ig1; 185 }; 186 187 /** 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. 203 */ 204 struct st_sensor_settings { 205 u8 wai; 206 u8 wai_addr; 207 char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME]; 208 struct iio_chan_spec *ch; 209 int num_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; 218 bool multi_read_bit; 219 unsigned int bootime; 220 }; 221 222 /** 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 240 */ 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; 247 248 bool enabled; 249 250 unsigned int odr; 251 unsigned int num_data_channels; 252 253 u8 drdy_int_pin; 254 bool int_pin_open_drain; 255 int irq; 256 257 bool edge_irq; 258 bool hw_irq_trigger; 259 s64 hw_timestamp; 260 261 struct mutex odr_lock; 262 263 char buffer_data[ST_SENSORS_MAX_BUFFER_SIZE] __aligned(IIO_DMA_MINALIGN); 264 }; 265 266 #ifdef CONFIG_IIO_BUFFER 267 irqreturn_t st_sensors_trigger_handler(int irq, void *p); 268 #endif 269 270 #ifdef CONFIG_IIO_TRIGGER 271 int st_sensors_allocate_trigger(struct iio_dev *indio_dev, 272 const struct iio_trigger_ops *trigger_ops); 273 274 int st_sensors_validate_device(struct iio_trigger *trig, 275 struct iio_dev *indio_dev); 276 #else 277 static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev, 278 const struct iio_trigger_ops *trigger_ops) 279 { 280 return 0; 281 } 282 #define st_sensors_validate_device NULL 283 #endif 284 285 int st_sensors_init_sensor(struct iio_dev *indio_dev, 286 struct st_sensors_platform_data *pdata); 287 288 int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable); 289 290 int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable); 291 292 int st_sensors_power_enable(struct iio_dev *indio_dev); 293 294 int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev, 295 unsigned reg, unsigned writeval, 296 unsigned *readval); 297 298 int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr); 299 300 int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable); 301 302 int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale); 303 304 int st_sensors_read_info_raw(struct iio_dev *indio_dev, 305 struct iio_chan_spec const *ch, int *val); 306 307 int st_sensors_get_settings_index(const char *name, 308 const struct st_sensor_settings *list, 309 const int list_length); 310 311 int st_sensors_verify_id(struct iio_dev *indio_dev); 312 313 ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev, 314 struct device_attribute *attr, char *buf); 315 316 ssize_t st_sensors_sysfs_scale_avail(struct device *dev, 317 struct device_attribute *attr, char *buf); 318 319 void st_sensors_dev_name_probe(struct device *dev, char *name, int len); 320 321 /* Accelerometer */ 322 const struct st_sensor_settings *st_accel_get_settings(const char *name); 323 int st_accel_common_probe(struct iio_dev *indio_dev); 324 325 /* Gyroscope */ 326 const struct st_sensor_settings *st_gyro_get_settings(const char *name); 327 int st_gyro_common_probe(struct iio_dev *indio_dev); 328 329 /* Magnetometer */ 330 const struct st_sensor_settings *st_magn_get_settings(const char *name); 331 int st_magn_common_probe(struct iio_dev *indio_dev); 332 333 /* Pressure */ 334 const struct st_sensor_settings *st_press_get_settings(const char *name); 335 int st_press_common_probe(struct iio_dev *indio_dev); 336 337 #endif /* ST_SENSORS_H */ 338
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.