Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * STMicroelectronics sensors library driver
0004  *
0005  * Copyright 2012-2013 STMicroelectronics Inc.
0006  *
0007  * Denis Ciocca <denis.ciocca@st.com>
0008  */
0009 
0010 #ifndef ST_SENSORS_H
0011 #define ST_SENSORS_H
0012 
0013 #include <linux/i2c.h>
0014 #include <linux/spi/spi.h>
0015 #include <linux/irqreturn.h>
0016 #include <linux/iio/iio.h>
0017 #include <linux/iio/trigger.h>
0018 #include <linux/bitops.h>
0019 #include <linux/regulator/consumer.h>
0020 #include <linux/regmap.h>
0021 
0022 #include <linux/platform_data/st_sensors_pdata.h>
0023 
0024 #define LSM9DS0_IMU_DEV_NAME        "lsm9ds0"
0025 
0026 /*
0027  * Buffer size max case: 2bytes per channel, 3 channels in total +
0028  *           8bytes timestamp channel (s64)
0029  */
0030 #define ST_SENSORS_MAX_BUFFER_SIZE      (ALIGN(2 * 3, sizeof(s64)) + \
0031                          sizeof(s64))
0032 
0033 #define ST_SENSORS_ODR_LIST_MAX         10
0034 #define ST_SENSORS_FULLSCALE_AVL_MAX        10
0035 
0036 #define ST_SENSORS_NUMBER_ALL_CHANNELS      4
0037 #define ST_SENSORS_ENABLE_ALL_AXIS      0x07
0038 #define ST_SENSORS_SCAN_X           0
0039 #define ST_SENSORS_SCAN_Y           1
0040 #define ST_SENSORS_SCAN_Z           2
0041 #define ST_SENSORS_DEFAULT_POWER_ON_VALUE   0x01
0042 #define ST_SENSORS_DEFAULT_POWER_OFF_VALUE  0x00
0043 #define ST_SENSORS_DEFAULT_WAI_ADDRESS      0x0f
0044 #define ST_SENSORS_DEFAULT_AXIS_ADDR        0x20
0045 #define ST_SENSORS_DEFAULT_AXIS_MASK        0x07
0046 #define ST_SENSORS_DEFAULT_AXIS_N_BIT       3
0047 #define ST_SENSORS_DEFAULT_STAT_ADDR        0x27
0048 
0049 #define ST_SENSORS_MAX_NAME         17
0050 #define ST_SENSORS_MAX_4WAI         8
0051 
0052 #define ST_SENSORS_LSM_CHANNELS_EXT(device_type, mask, index, mod, \
0053                     ch2, s, endian, rbits, sbits, addr, ext) \
0054 { \
0055     .type = device_type, \
0056     .modified = mod, \
0057     .info_mask_separate = mask, \
0058     .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
0059     .scan_index = index, \
0060     .channel2 = ch2, \
0061     .address = addr, \
0062     .scan_type = { \
0063         .sign = s, \
0064         .realbits = rbits, \
0065         .shift = sbits - rbits, \
0066         .storagebits = sbits, \
0067         .endianness = endian, \
0068     }, \
0069     .ext_info = ext, \
0070 }
0071 
0072 #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
0073                 ch2, s, endian, rbits, sbits, addr) \
0074     ST_SENSORS_LSM_CHANNELS_EXT(device_type, mask, index, mod,  \
0075                     ch2, s, endian, rbits, sbits, addr, NULL)
0076 
0077 #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
0078         IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
0079             st_sensors_sysfs_sampling_frequency_avail)
0080 
0081 #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
0082         IIO_DEVICE_ATTR(name, S_IRUGO, \
0083             st_sensors_sysfs_scale_avail, NULL , 0);
0084 
0085 struct st_sensor_odr_avl {
0086     unsigned int hz;
0087     u8 value;
0088 };
0089 
0090 struct st_sensor_odr {
0091     u8 addr;
0092     u8 mask;
0093     struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
0094 };
0095 
0096 struct st_sensor_power {
0097     u8 addr;
0098     u8 mask;
0099     u8 value_off;
0100     u8 value_on;
0101 };
0102 
0103 struct st_sensor_axis {
0104     u8 addr;
0105     u8 mask;
0106 };
0107 
0108 struct st_sensor_fullscale_avl {
0109     unsigned int num;
0110     u8 value;
0111     unsigned int gain;
0112     unsigned int gain2;
0113 };
0114 
0115 struct st_sensor_fullscale {
0116     u8 addr;
0117     u8 mask;
0118     struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
0119 };
0120 
0121 struct st_sensor_sim {
0122     u8 addr;
0123     u8 value;
0124 };
0125 
0126 /**
0127  * struct st_sensor_bdu - ST sensor device block data update
0128  * @addr: address of the register.
0129  * @mask: mask to write the block data update flag.
0130  */
0131 struct st_sensor_bdu {
0132     u8 addr;
0133     u8 mask;
0134 };
0135 
0136 /**
0137  * struct st_sensor_das - ST sensor device data alignment selection
0138  * @addr: address of the register.
0139  * @mask: mask to write the das flag for left alignment.
0140  */
0141 struct st_sensor_das {
0142     u8 addr;
0143     u8 mask;
0144 };
0145 
0146 /**
0147  * struct st_sensor_int_drdy - ST sensor device drdy line parameters
0148  * @addr: address of INT drdy register.
0149  * @mask: mask to enable drdy line.
0150  * @addr_od: address to enable/disable Open Drain on the INT line.
0151  * @mask_od: mask to enable/disable Open Drain on the INT line.
0152  */
0153 struct st_sensor_int_drdy {
0154     u8 addr;
0155     u8 mask;
0156     u8 addr_od;
0157     u8 mask_od;
0158 };
0159 
0160 /**
0161  * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
0162  * struct int1 - data-ready configuration register for INT1 pin.
0163  * struct int2 - data-ready configuration register for INT2 pin.
0164  * @addr_ihl: address to enable/disable active low on the INT lines.
0165  * @mask_ihl: mask to enable/disable active low on the INT lines.
0166  * struct stat_drdy - status register of DRDY (data ready) interrupt.
0167  * struct ig1 - represents the Interrupt Generator 1 of sensors.
0168  * @en_addr: address of the enable ig1 register.
0169  * @en_mask: mask to write the on/off value for enable.
0170  */
0171 struct st_sensor_data_ready_irq {
0172     struct st_sensor_int_drdy int1;
0173     struct st_sensor_int_drdy int2;
0174     u8 addr_ihl;
0175     u8 mask_ihl;
0176     struct {
0177         u8 addr;
0178         u8 mask;
0179     } stat_drdy;
0180     struct {
0181         u8 en_addr;
0182         u8 en_mask;
0183     } ig1;
0184 };
0185 
0186 /**
0187  * struct st_sensor_settings - ST specific sensor settings
0188  * @wai: Contents of WhoAmI register.
0189  * @wai_addr: The address of WhoAmI register.
0190  * @sensors_supported: List of supported sensors by struct itself.
0191  * @ch: IIO channels for the sensor.
0192  * @odr: Output data rate register and ODR list available.
0193  * @pw: Power register of the sensor.
0194  * @enable_axis: Enable one or more axis of the sensor.
0195  * @fs: Full scale register and full scale list available.
0196  * @bdu: Block data update register.
0197  * @das: Data Alignment Selection register.
0198  * @drdy_irq: Data ready register of the sensor.
0199  * @sim: SPI serial interface mode register of the sensor.
0200  * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
0201  * @bootime: samples to discard when sensor passing from power-down to power-up.
0202  */
0203 struct st_sensor_settings {
0204     u8 wai;
0205     u8 wai_addr;
0206     char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
0207     struct iio_chan_spec *ch;
0208     int num_ch;
0209     struct st_sensor_odr odr;
0210     struct st_sensor_power pw;
0211     struct st_sensor_axis enable_axis;
0212     struct st_sensor_fullscale fs;
0213     struct st_sensor_bdu bdu;
0214     struct st_sensor_das das;
0215     struct st_sensor_data_ready_irq drdy_irq;
0216     struct st_sensor_sim sim;
0217     bool multi_read_bit;
0218     unsigned int bootime;
0219 };
0220 
0221 /**
0222  * struct st_sensor_data - ST sensor device status
0223  * @trig: The trigger in use by the core driver.
0224  * @mount_matrix: The mounting matrix of the sensor.
0225  * @sensor_settings: Pointer to the specific sensor settings in use.
0226  * @current_fullscale: Maximum range of measure by the sensor.
0227  * @vdd: Pointer to sensor's Vdd power supply
0228  * @vdd_io: Pointer to sensor's Vdd-IO power supply
0229  * @regmap: Pointer to specific sensor regmap configuration.
0230  * @enabled: Status of the sensor (false->off, true->on).
0231  * @odr: Output data rate of the sensor [Hz].
0232  * num_data_channels: Number of data channels used in buffer.
0233  * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
0234  * @int_pin_open_drain: Set the interrupt/DRDY to open drain.
0235  * @irq: the IRQ number.
0236  * @edge_irq: the IRQ triggers on edges and need special handling.
0237  * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
0238  * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
0239  * @buffer_data: Data used by buffer part.
0240  * @odr_lock: Local lock for preventing concurrent ODR accesses/changes
0241  */
0242 struct st_sensor_data {
0243     struct iio_trigger *trig;
0244     struct iio_mount_matrix mount_matrix;
0245     struct st_sensor_settings *sensor_settings;
0246     struct st_sensor_fullscale_avl *current_fullscale;
0247     struct regulator *vdd;
0248     struct regulator *vdd_io;
0249     struct regmap *regmap;
0250 
0251     bool enabled;
0252 
0253     unsigned int odr;
0254     unsigned int num_data_channels;
0255 
0256     u8 drdy_int_pin;
0257     bool int_pin_open_drain;
0258     int irq;
0259 
0260     bool edge_irq;
0261     bool hw_irq_trigger;
0262     s64 hw_timestamp;
0263 
0264     char buffer_data[ST_SENSORS_MAX_BUFFER_SIZE] ____cacheline_aligned;
0265 
0266     struct mutex odr_lock;
0267 };
0268 
0269 #ifdef CONFIG_IIO_BUFFER
0270 irqreturn_t st_sensors_trigger_handler(int irq, void *p);
0271 #endif
0272 
0273 #ifdef CONFIG_IIO_TRIGGER
0274 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
0275                 const struct iio_trigger_ops *trigger_ops);
0276 
0277 int st_sensors_validate_device(struct iio_trigger *trig,
0278                    struct iio_dev *indio_dev);
0279 #else
0280 static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
0281                 const struct iio_trigger_ops *trigger_ops)
0282 {
0283     return 0;
0284 }
0285 #define st_sensors_validate_device NULL
0286 #endif
0287 
0288 int st_sensors_init_sensor(struct iio_dev *indio_dev,
0289                     struct st_sensors_platform_data *pdata);
0290 
0291 int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
0292 
0293 int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
0294 
0295 int st_sensors_power_enable(struct iio_dev *indio_dev);
0296 
0297 int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
0298                   unsigned reg, unsigned writeval,
0299                   unsigned *readval);
0300 
0301 int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
0302 
0303 int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
0304 
0305 int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
0306 
0307 int st_sensors_read_info_raw(struct iio_dev *indio_dev,
0308                 struct iio_chan_spec const *ch, int *val);
0309 
0310 int st_sensors_get_settings_index(const char *name,
0311                   const struct st_sensor_settings *list,
0312                   const int list_length);
0313 
0314 int st_sensors_verify_id(struct iio_dev *indio_dev);
0315 
0316 ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
0317                 struct device_attribute *attr, char *buf);
0318 
0319 ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
0320                 struct device_attribute *attr, char *buf);
0321 
0322 void st_sensors_dev_name_probe(struct device *dev, char *name, int len);
0323 
0324 /* Accelerometer */
0325 const struct st_sensor_settings *st_accel_get_settings(const char *name);
0326 int st_accel_common_probe(struct iio_dev *indio_dev);
0327 
0328 /* Gyroscope */
0329 const struct st_sensor_settings *st_gyro_get_settings(const char *name);
0330 int st_gyro_common_probe(struct iio_dev *indio_dev);
0331 
0332 /* Magnetometer */
0333 const struct st_sensor_settings *st_magn_get_settings(const char *name);
0334 int st_magn_common_probe(struct iio_dev *indio_dev);
0335 
0336 /* Pressure */
0337 const struct st_sensor_settings *st_press_get_settings(const char *name);
0338 int st_press_common_probe(struct iio_dev *indio_dev);
0339 
0340 #endif /* ST_SENSORS_H */