0001
0002
0003
0004
0005
0006
0007
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
0028
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
0128
0129
0130
0131 struct st_sensor_bdu {
0132 u8 addr;
0133 u8 mask;
0134 };
0135
0136
0137
0138
0139
0140
0141 struct st_sensor_das {
0142 u8 addr;
0143 u8 mask;
0144 };
0145
0146
0147
0148
0149
0150
0151
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
0162
0163
0164
0165
0166
0167
0168
0169
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
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
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
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
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
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
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
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
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