linux/include/linux/iio/common/st_sensors.h
<<
>>
Prefs
   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 * @dev: Pointer to instance of struct device (I2C or SPI).
 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 * @vdd: Pointer to sensor's Vdd power supply
 229 * @vdd_io: Pointer to sensor's Vdd-IO power supply
 230 * @regmap: Pointer to specific sensor regmap configuration.
 231 * @enabled: Status of the sensor (false->off, true->on).
 232 * @odr: Output data rate of the sensor [Hz].
 233 * num_data_channels: Number of data channels used in buffer.
 234 * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
 235 * @int_pin_open_drain: Set the interrupt/DRDY to open drain.
 236 * @irq: the IRQ number.
 237 * @edge_irq: the IRQ triggers on edges and need special handling.
 238 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
 239 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
 240 * @buffer_data: Data used by buffer part.
 241 */
 242struct st_sensor_data {
 243        struct device *dev;
 244        struct iio_trigger *trig;
 245        struct iio_mount_matrix mount_matrix;
 246        struct st_sensor_settings *sensor_settings;
 247        struct st_sensor_fullscale_avl *current_fullscale;
 248        struct regulator *vdd;
 249        struct regulator *vdd_io;
 250        struct regmap *regmap;
 251
 252        bool enabled;
 253
 254        unsigned int odr;
 255        unsigned int num_data_channels;
 256
 257        u8 drdy_int_pin;
 258        bool int_pin_open_drain;
 259        int irq;
 260
 261        bool edge_irq;
 262        bool hw_irq_trigger;
 263        s64 hw_timestamp;
 264
 265        char buffer_data[ST_SENSORS_MAX_BUFFER_SIZE] ____cacheline_aligned;
 266};
 267
 268#ifdef CONFIG_IIO_BUFFER
 269irqreturn_t st_sensors_trigger_handler(int irq, void *p);
 270#endif
 271
 272#ifdef CONFIG_IIO_TRIGGER
 273int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
 274                                const struct iio_trigger_ops *trigger_ops);
 275
 276void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
 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}
 285static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
 286{
 287        return;
 288}
 289#define st_sensors_validate_device NULL
 290#endif
 291
 292int st_sensors_init_sensor(struct iio_dev *indio_dev,
 293                                        struct st_sensors_platform_data *pdata);
 294
 295int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
 296
 297int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
 298
 299int st_sensors_power_enable(struct iio_dev *indio_dev);
 300
 301void st_sensors_power_disable(struct iio_dev *indio_dev);
 302
 303int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
 304                                  unsigned reg, unsigned writeval,
 305                                  unsigned *readval);
 306
 307int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
 308
 309int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
 310
 311int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
 312
 313int st_sensors_read_info_raw(struct iio_dev *indio_dev,
 314                                struct iio_chan_spec const *ch, int *val);
 315
 316int st_sensors_get_settings_index(const char *name,
 317                                  const struct st_sensor_settings *list,
 318                                  const int list_length);
 319
 320int st_sensors_verify_id(struct iio_dev *indio_dev);
 321
 322ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
 323                                struct device_attribute *attr, char *buf);
 324
 325ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
 326                                struct device_attribute *attr, char *buf);
 327
 328void st_sensors_dev_name_probe(struct device *dev, char *name, int len);
 329
 330/* Accelerometer */
 331const struct st_sensor_settings *st_accel_get_settings(const char *name);
 332int st_accel_common_probe(struct iio_dev *indio_dev);
 333void st_accel_common_remove(struct iio_dev *indio_dev);
 334
 335/* Gyroscope */
 336const struct st_sensor_settings *st_gyro_get_settings(const char *name);
 337int st_gyro_common_probe(struct iio_dev *indio_dev);
 338void st_gyro_common_remove(struct iio_dev *indio_dev);
 339
 340/* Magnetometer */
 341const struct st_sensor_settings *st_magn_get_settings(const char *name);
 342int st_magn_common_probe(struct iio_dev *indio_dev);
 343void st_magn_common_remove(struct iio_dev *indio_dev);
 344
 345/* Pressure */
 346const struct st_sensor_settings *st_press_get_settings(const char *name);
 347int st_press_common_probe(struct iio_dev *indio_dev);
 348void st_press_common_remove(struct iio_dev *indio_dev);
 349
 350#endif /* ST_SENSORS_H */
 351