st_ilps22qs.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * STMicroelectronics ilps22qs driver
  4. *
  5. * Copyright 2023 STMicroelectronics Inc.
  6. *
  7. * MEMS Software Solutions Team
  8. */
  9. #ifndef __ST_ILPS22QS_H
  10. #define __ST_ILPS22QS_H
  11. #include <linux/bitfield.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of.h>
  16. #include <linux/regmap.h>
  17. #include <linux/types.h>
  18. #include <linux/workqueue.h>
  19. #define ST_ILPS22QS_DEV_NAME "ilps22qs"
  20. #define ST_ILPS28QWS_DEV_NAME "ilps28qws"
  21. #define ST_ILPS22QS_WHO_AM_I_ADDR 0x0f
  22. #define ST_ILPS22QS_WHOAMI_VAL 0xb4
  23. #define ST_ILPS22QS_CTRL1_ADDR 0x10
  24. #define ST_ILPS22QS_ODR_MASK GENMASK(6, 3)
  25. #define ST_ILPS22QS_CTRL2_ADDR 0x11
  26. #define ST_ILPS22QS_SOFT_RESET_MASK BIT(2)
  27. #define ST_ILPS22QS_BDU_MASK BIT(3)
  28. #define ST_ILPS22QS_CTRL3_ADDR 0x12
  29. #define ST_ILPS22QS_AH_QVAR_EN_MASK BIT(7)
  30. #define ST_ILPS22QS_AH_QVAR_P_AUTO_EN_MASK BIT(5)
  31. #define ST_ILPS22QS_PRESS_OUT_XL_ADDR 0x28
  32. #define ST_ILPS22QS_TEMP_OUT_L_ADDR 0x2b
  33. #define ST_ILPS22QS_PRESS_FS_AVL_GAIN (1000000000UL / 4096UL)
  34. #define ST_ILPS22QS_TEMP_FS_AVL_GAIN 100
  35. #define ST_ILPS22QS_QVAR_FS_AVL_GAIN 438000
  36. #define ST_ILPS22QS_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
  37. #define ST_ILPS22QS_ODR_LIST_NUM 8
  38. enum st_ilps22qs_sensor_id {
  39. ST_ILPS22QS_PRESS = 0,
  40. ST_ILPS22QS_TEMP,
  41. ST_ILPS22QS_QVAR,
  42. ST_ILPS22QS_SENSORS_NUM,
  43. };
  44. struct st_ilps22qs_odr_t {
  45. u8 hz;
  46. u8 val;
  47. };
  48. struct st_ilps22qs_reg {
  49. u8 addr;
  50. u8 mask;
  51. };
  52. struct st_ilps22qs_odr_table_t {
  53. u8 size;
  54. struct st_ilps22qs_reg reg;
  55. struct st_ilps22qs_odr_t odr_avl[ST_ILPS22QS_ODR_LIST_NUM];
  56. };
  57. struct st_ilps22qs_hw {
  58. struct iio_dev *iio_devs[ST_ILPS22QS_SENSORS_NUM];
  59. struct workqueue_struct *workqueue;
  60. struct regulator *vddio_supply;
  61. struct regulator *vdd_supply;
  62. struct regmap *regmap;
  63. struct device *dev;
  64. struct mutex lock;
  65. bool interleave;
  66. u8 enable_mask;
  67. u8 odr;
  68. };
  69. struct st_ilps22qs_sensor {
  70. enum st_ilps22qs_sensor_id id;
  71. struct work_struct iio_work;
  72. struct st_ilps22qs_hw *hw;
  73. struct hrtimer hr_timer;
  74. ktime_t ktime;
  75. int64_t timestamp;
  76. char name[32];
  77. u32 gain;
  78. u8 odr;
  79. };
  80. extern const struct dev_pm_ops st_ilps22qs_pm_ops;
  81. static inline int st_ilps22qs_update_locked(struct st_ilps22qs_hw *hw,
  82. unsigned int addr,
  83. unsigned int mask,
  84. unsigned int data)
  85. {
  86. unsigned int val = ST_ILPS22QS_SHIFT_VAL(data, mask);
  87. int err;
  88. mutex_lock(&hw->lock);
  89. err = regmap_update_bits(hw->regmap, addr, mask, val);
  90. mutex_unlock(&hw->lock);
  91. return err;
  92. }
  93. static inline int st_ilps22qs_read_locked(struct st_ilps22qs_hw *hw,
  94. unsigned int addr, void *val,
  95. unsigned int len)
  96. {
  97. int err;
  98. mutex_lock(&hw->lock);
  99. err = regmap_bulk_read(hw->regmap, addr, val, len);
  100. mutex_unlock(&hw->lock);
  101. return err;
  102. }
  103. static inline void st_ilps22qs_flush_works(struct st_ilps22qs_hw *hw)
  104. {
  105. flush_workqueue(hw->workqueue);
  106. }
  107. static inline int st_ilps22qs_destroy_workqueue(struct st_ilps22qs_hw *hw)
  108. {
  109. if (hw->workqueue)
  110. destroy_workqueue(hw->workqueue);
  111. return 0;
  112. }
  113. static inline int st_ilps22qs_allocate_workqueue(struct st_ilps22qs_hw *hw)
  114. {
  115. if (!hw->workqueue)
  116. hw->workqueue = create_workqueue(ST_ILPS22QS_DEV_NAME);
  117. return !hw->workqueue ? -ENOMEM : 0;
  118. }
  119. static inline s64 st_ilps22qs_get_time_ns(struct st_ilps22qs_hw *hw)
  120. {
  121. return iio_get_time_ns(hw->iio_devs[ST_ILPS22QS_PRESS]);
  122. }
  123. int st_ilps22qs_probe(struct device *dev, struct regmap *regmap);
  124. int st_ilps22qs_remove(struct device *dev);
  125. #endif /* __ST_ILPS22QS_H */