st_h3lis331dl.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * STMicroelectronics st_h3lis331dl sensor driver
  4. *
  5. * MEMS Software Solutions Team
  6. *
  7. * Copyright 2023 STMicroelectronics Inc.
  8. */
  9. #ifndef ST_H3LIS331DL_H
  10. #define ST_H3LIS331DL_H
  11. #include <linux/bitfield.h>
  12. #include <linux/device.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/delay.h>
  15. #include <linux/regmap.h>
  16. #define ST_H3LIS331DL_DEV_NAME "h3lis331dl"
  17. #define ST_LIS331DLH_DEV_NAME "lis331dlh"
  18. #define ST_H3LIS331DL_REG_WHO_AM_I_ADDR 0x0f
  19. #define ST_H3LIS331DL_WHO_AM_I_VAL 0x32
  20. #define ST_H3LIS331DL_CTRL_REG1_ADDR 0x20
  21. #define ST_H3LIS331DL_EN_MASK GENMASK(2, 0)
  22. #define ST_H3LIS331DL_DR_MASK GENMASK(4, 3)
  23. #define ST_H3LIS331DL_PM_MASK GENMASK(7, 5)
  24. #define ST_H3LIS331DL_CTRL_REG2_ADDR 0x21
  25. #define ST_H3LIS331DL_BOOT_MASK BIT(7)
  26. #define ST_H3LIS331DL_CTRL_REG3_ADDR 0x22
  27. #define ST_H3LIS331DL_I1CFG_MASK GENMASK(1, 0)
  28. #define ST_H3LIS331DL_LIR1_MASK BIT(2)
  29. #define ST_H3LIS331DL_I2CFG_MASK GENMASK(4, 3)
  30. #define ST_H3LIS331DL_LIR2_MASK BIT(5)
  31. #define ST_H3LIS331DL_PP_OD_MASK BIT(6)
  32. #define ST_H3LIS331DL_IHL_MASK BIT(7)
  33. #define ST_H3LIS331DL_CFG_DRDY_VAL 0x02
  34. #define ST_H3LIS331DL_CTRL_REG4_ADDR 0x23
  35. #define ST_H3LIS331DL_FS_MASK GENMASK(5, 4)
  36. #define ST_H3LIS331DL_BLE_MASK BIT(6)
  37. #define ST_H3LIS331DL_BDU_MASK BIT(7)
  38. #define ST_H3LIS331DL_STATUS_REG_ADDR 0x27
  39. #define ST_H3LIS331DL_ZYXDA_MASK BIT(3)
  40. #define ST_H3LIS331DL_REG_OUTX_L_ADDR 0x28
  41. #define ST_H3LIS331DL_REG_OUTY_L_ADDR 0x2a
  42. #define ST_H3LIS331DL_REG_OUTZ_L_ADDR 0x2c
  43. #define ST_H3LIS331DL_SAMPLE_SIZE 6
  44. /* enable reading address with auto increment */
  45. #define ST_H3LIS331DL_AUTO_INCREMENT(_addr) (0x80 | _addr)
  46. #define ST_H3LIS331DL_DATA_CHANNEL(chan_type, addr, mod, ch2, scan_idx, \
  47. rb, sb, sg, ex_info) \
  48. { \
  49. .type = chan_type, \
  50. .address = addr, \
  51. .modified = mod, \
  52. .channel2 = ch2, \
  53. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  54. BIT(IIO_CHAN_INFO_SCALE), \
  55. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  56. .scan_index = scan_idx, \
  57. .scan_type = { \
  58. .sign = sg, \
  59. .realbits = rb, \
  60. .storagebits = sb, \
  61. .endianness = IIO_LE, \
  62. }, \
  63. .ext_info = ex_info, \
  64. }
  65. #define ST_H3LIS331DL_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
  66. #define ST_H3LIS331DL_ACC_SENSOR_ID 0
  67. #define ST_H3LIS331DL_ODR_LIST_SIZE 8
  68. #define ST_H3LIS331DL_FS_LIST_SIZE 3
  69. /**
  70. * struct st_h3lis331dl_reg - Generic sensor register description (addr + mask)
  71. * @addr: Address of register.
  72. * @mask: Bitmask register for proper usage.
  73. */
  74. struct st_h3lis331dl_reg {
  75. u8 addr;
  76. u8 mask;
  77. };
  78. /**
  79. * struct st_h3lis331dl_odr - Single ODR entry
  80. * @hz: Most significant part of the sensor ODR (Hz).
  81. * @val: ODR register value.
  82. */
  83. struct st_h3lis331dl_odr {
  84. int hz;
  85. u8 val;
  86. };
  87. /**
  88. * struct st_h3lis331dl_odr_table_entry - Sensor ODR table
  89. * @size: Size of ODR table.
  90. * @reg: ODR register.
  91. * @odr_avl: Array of supported ODR value.
  92. */
  93. struct st_h3lis331dl_odr_table_entry {
  94. u8 size;
  95. struct st_h3lis331dl_reg reg;
  96. struct st_h3lis331dl_odr odr_avl[ST_H3LIS331DL_ODR_LIST_SIZE];
  97. };
  98. /**
  99. * struct st_h3lis331dl_fs - Full Scale sensor table entry
  100. * @gain: Sensor sensitivity.
  101. * @val: FS register value.
  102. */
  103. struct st_h3lis331dl_fs {
  104. u32 gain;
  105. u8 val;
  106. };
  107. /**
  108. * struct st_h3lis331dl_fs_table_entry - Full Scale sensor table
  109. * @size: Full Scale sensor table size.
  110. * @reg: Register description for FS settings.
  111. * @fs_avl: Full Scale list entries.
  112. */
  113. struct st_h3lis331dl_fs_table_entry {
  114. u8 size;
  115. struct st_h3lis331dl_reg reg;
  116. struct st_h3lis331dl_fs fs_avl[ST_H3LIS331DL_FS_LIST_SIZE];
  117. };
  118. #define ST_H3LIS331DL_ACC_FS_100G_GAIN IIO_G_TO_M_S_2(784000)
  119. #define ST_H3LIS331DL_ACC_FS_200G_GAIN IIO_G_TO_M_S_2(1568000)
  120. #define ST_H3LIS331DL_ACC_FS_400G_GAIN IIO_G_TO_M_S_2(3136000)
  121. /**
  122. * struct st_h3lis331dl_sensor - ST IMU sensor instance
  123. * @is: Sensor id.
  124. * @name: Sensor name.
  125. * @hw: Pointer to instance of struct st_h3lis331dl_hw.
  126. * @trig: Trigger used by IIO event sensors.
  127. * @odr: Sensor odr.
  128. * @gain: Configured sensor sensitivity.
  129. * @offset: Sensor data offset.
  130. * @decimator: Sensor decimator
  131. * @dec_counter: Sensor decimator counter
  132. */
  133. struct st_h3lis331dl_sensor {
  134. int id;
  135. char name[32];
  136. struct st_h3lis331dl_hw *hw;
  137. struct iio_trigger *trig;
  138. int odr;
  139. u32 gain;
  140. u32 offset;
  141. u8 decimator;
  142. u8 dec_counter;
  143. };
  144. /**
  145. * struct st_h3lis331dl_hw - ST IMU MEMS hw instance
  146. * @dev: Pointer to instance of struct device (I2C or SPI).
  147. * @irq: Device interrupt line (I2C or SPI).
  148. * @regmap: Register map of the device.
  149. * @int_pin: Save interrupt pin used by sensor.
  150. * @lock: Mutex to protect read and write operations.
  151. * @enable_mask: Enabled sensor bitmask.
  152. * @ts: hw timestamp value always monotonic where the most
  153. * significant 8byte are incremented at every disable/enable.
  154. * @iio_devs: Pointers to acc/gyro iio_dev instances.
  155. * @vdd_supply: Voltage regulator for VDD.
  156. * @vddio_supply: Voltage regulator for VDDIIO.
  157. * @orientation: Sensor chip orientation relative to main hardware.
  158. * @self_test: Self test start and read result.
  159. * @selftest_available: Reports self test available for this sensor.
  160. */
  161. struct st_h3lis331dl_hw {
  162. struct device *dev;
  163. int irq;
  164. struct regmap *regmap;
  165. int int_pin;
  166. struct mutex lock;
  167. u64 enable_mask;
  168. u64 requested_mask;
  169. s64 ts;
  170. struct iio_dev *iio_devs;
  171. struct regulator *vdd_supply;
  172. struct regulator *vddio_supply;
  173. struct iio_mount_matrix orientation;
  174. };
  175. extern const struct dev_pm_ops st_h3lis331dl_pm_ops;
  176. static inline int __st_h3lis331dl_write_with_mask(struct st_h3lis331dl_hw *hw,
  177. unsigned int addr,
  178. unsigned int mask,
  179. unsigned int data)
  180. {
  181. int err;
  182. unsigned int val = ST_H3LIS331DL_SHIFT_VAL(data, mask);
  183. err = regmap_update_bits(hw->regmap, addr, mask, val);
  184. return err;
  185. }
  186. static inline int
  187. st_h3lis331dl_update_bits_locked(struct st_h3lis331dl_hw *hw, unsigned int addr,
  188. unsigned int mask, unsigned int val)
  189. {
  190. int err;
  191. mutex_lock(&hw->lock);
  192. err = __st_h3lis331dl_write_with_mask(hw, addr, mask, val);
  193. mutex_unlock(&hw->lock);
  194. return err;
  195. }
  196. /* use when mask is constant */
  197. static inline int
  198. st_h3lis331dl_write_with_mask_locked(struct st_h3lis331dl_hw *hw,
  199. unsigned int addr,
  200. unsigned int mask,
  201. unsigned int data)
  202. {
  203. int err;
  204. unsigned int val = ST_H3LIS331DL_SHIFT_VAL(mask, data);
  205. mutex_lock(&hw->lock);
  206. err = regmap_update_bits(hw->regmap, addr, mask, val);
  207. mutex_unlock(&hw->lock);
  208. return err;
  209. }
  210. static inline s64 st_h3lis331dl_get_time_ns(struct iio_dev *iio_dev)
  211. {
  212. return iio_get_time_ns(iio_dev);
  213. }
  214. int st_h3lis331dl_probe(struct device *dev, int irq, int hw_id,
  215. struct regmap *regmap);
  216. int st_h3lis331dl_sensor_set_enable(struct st_h3lis331dl_sensor *sensor,
  217. bool enable);
  218. int st_h3lis331dl_trigger_setup(struct st_h3lis331dl_hw *hw);
  219. int st_h3lis331dl_allocate_triggered_buffer(struct st_h3lis331dl_hw *hw);
  220. #endif /* ST_H3LIS331DL_H */