gsensor.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Memsic MXC400xXC Gsensor driver for screen orientation
  4. *
  5. */
  6. #ifndef __GSENSOR_H
  7. #define __GSENSOR_H
  8. #include <linux/types.h>
  9. #include <linux/iio/iio.h>
  10. #include <linux/i2c.h>
  11. #include <linux/iio/trigger.h>
  12. #include <linux/iio/triggered_buffer.h>
  13. /* Gsensor orientation values (hex per API spec) */
  14. enum gsensor_orientation_hex
  15. {
  16. GSENSOR_ORIENT_UNKNOWN_HEX = 0x00,
  17. GSENSOR_ORIENT_PORTRAIT_HEX = 0x14,
  18. GSENSOR_ORIENT_LANDSCAPE_HEX = 0x15,
  19. GSENSOR_ORIENT_PORTRAIT_FLIP_HEX = 0x16,
  20. GSENSOR_ORIENT_LANDSCAPE_FLIP_HEX = 0x17,
  21. };
  22. /* Gsensor orientation strings */
  23. #define GSENSOR_ORIENT_PORTRAIT_STR "portrait"
  24. #define GSENSOR_ORIENT_LANDSCAPE_STR "landscape"
  25. #define GSENSOR_ORIENT_PORTRAIT_FLIP_STR "portrait_flip"
  26. #define GSENSOR_ORIENT_LANDSCAPE_FLIP_STR "landscape_flip"
  27. /* Gsensor mode bits (per API spec) */
  28. #define GSENSOR_MODE_SOFT_RESET BIT(2) /* EC17: soft reset */
  29. #define GSENSOR_MODE_INITIALIZED BIT(1) /* EC16: 0=not init, 1=init */
  30. #define GSENSOR_MODE_INTERRUPT BIT(0) /* 0=poll, 1=interrupt */
  31. /* Memsic MXC400xXC Register Definitions */
  32. #define MXC400X_REG_WHO_AM_I 0x00
  33. #define MXC400X_WHO_AM_I_VALUE 0x20
  34. #define MXC400X_REG_XOUT_U 0x04
  35. #define MXC400X_REG_XOUT_L 0x05
  36. #define MXC400X_REG_YOUT_U 0x06
  37. #define MXC400X_REG_YOUT_L 0x07
  38. #define MXC400X_REG_ZOUT_U 0x08
  39. #define MXC400X_REG_ZOUT_L 0x09
  40. #define MXC400X_REG_CONTROL 0x0A
  41. #define MXC400X_CONTROL_MODE_MASK 0x03
  42. #define MXC400X_CONTROL_MODE_STANDBY 0x00
  43. #define MXC400X_CONTROL_MODE_ACTIVE 0x01
  44. #define MXC400X_CONTROL_MODE_RESET 0x02
  45. #define MXC400X_REG_INT_ENABLE 0x0B
  46. #define MXC400X_INT_ENABLE_DRDY BIT(0)
  47. #define MXC400X_REG_INT_STATUS 0x0C
  48. /* ST LIS2DH12 / SC7A20 register */
  49. #define ST_ACCEL_WHO_AM_I_ADDR 0x0F
  50. #define ST_ACCEL_WHO_AM_I_VALUE 0x33 /* LIS2DH12 / SC7A20 */
  51. #define ST_ACCEL_CTRL_REG1_ADDR 0x20
  52. #define ST_ACCEL_CTRL_REG4_ADDR 0x23
  53. #define ST_ACCEL_OUT_X_L_ADDR 0x28
  54. #define ST_ACCEL_OUT_X_H_ADDR 0x29
  55. #define ST_ACCEL_OUT_Y_L_ADDR 0x2A
  56. #define ST_ACCEL_OUT_Y_H_ADDR 0x2B
  57. #define ST_ACCEL_OUT_Z_L_ADDR 0x2C
  58. #define ST_ACCEL_OUT_Z_H_ADDR 0x2D
  59. #define ST_ACCEL_STATUS_REG_ADDR 0x27
  60. #define ST_ACCEL_STATUS_DRDY 0x07
  61. /* Driver Constants */
  62. #define GSENSOR_NUMBER_DATA_CHANNELS 3
  63. #define GSENSOR_DEBOUNCE_MS 100
  64. #define MXC400X_SCALE_FACTOR 1000 /* mg per count at 2g range */
  65. /* Default I2C address */
  66. #define MXC400X_DEFAULT_I2C_ADDR 0x15
  67. /* Sensor Settings Structure */
  68. struct gsensor_fs_avl
  69. {
  70. unsigned int num; /* Full scale value in g */
  71. unsigned int value; /* Register value */
  72. unsigned int gain; /* Gain in micro g per LSB */
  73. };
  74. struct gsensor_odr_avl
  75. {
  76. unsigned int hz; /* Output data rate in Hz */
  77. unsigned int value; /* Register value */
  78. };
  79. struct gsensor_settings
  80. {
  81. u8 wai; /* WHO_AM_I expected value */
  82. u8 wai_addr; /* WHO_AM_I register address */
  83. const char **sensors_supported; /* Supported device names */
  84. /* Channel configuration */
  85. const struct iio_chan_spec *ch;
  86. int num_channels;
  87. /* ODR configuration */
  88. struct
  89. {
  90. u8 addr;
  91. u8 mask;
  92. const struct gsensor_odr_avl *odr_avl;
  93. int num_odr;
  94. } odr;
  95. /* Power mode configuration */
  96. struct
  97. {
  98. u8 addr;
  99. u8 mask;
  100. u8 value_on;
  101. u8 value_off;
  102. } pw;
  103. /* Full scale configuration */
  104. struct
  105. {
  106. u8 addr;
  107. u8 mask;
  108. const struct gsensor_fs_avl *fs_avl;
  109. int num_fs;
  110. } fs;
  111. /* Block Data Update */
  112. struct
  113. {
  114. u8 addr;
  115. u8 mask;
  116. } bdu;
  117. /* Data ready interrupt */
  118. struct
  119. {
  120. struct
  121. {
  122. u8 addr;
  123. u8 mask;
  124. } int1;
  125. u8 addr_ihl;
  126. u8 mask_ihl;
  127. struct
  128. {
  129. u8 addr;
  130. u8 mask;
  131. } stat_drdy;
  132. } drdy_irq;
  133. /* SPI/I2C interface mode */
  134. struct
  135. {
  136. u8 addr;
  137. u8 value;
  138. } sim;
  139. bool multi_read_bit;
  140. unsigned int bootime;
  141. };
  142. /* Driver Data Structure */
  143. struct gsensor_data
  144. {
  145. struct i2c_client *client;
  146. struct device *dev;
  147. struct mutex lock; /* Protects data access */
  148. /* Sensor settings */
  149. const struct gsensor_settings *sensor_settings;
  150. /* Current configuration */
  151. const struct gsensor_fs_avl *current_fullscale;
  152. unsigned int odr;
  153. /* Orientation data */
  154. enum gsensor_orientation_hex orientation_hex;
  155. char orientation_str[32];
  156. struct delayed_work debounce_work;
  157. enum gsensor_orientation_hex pending_orientation;
  158. unsigned long last_change_jiffies;
  159. /* State */
  160. bool enabled;
  161. bool initialized;
  162. bool interrupt_mode;
  163. bool irq_enabled;
  164. /* IIO related */
  165. struct iio_dev *indio_dev;
  166. /* Verifone sysfs */
  167. struct kobject *verifone_kobj;
  168. /* Regulators (optional) */
  169. struct regulator *vdd;
  170. struct regulator *vdd_io;
  171. };
  172. /* Core functions */
  173. int gsensor_common_probe(struct iio_dev *indio_dev);
  174. void gsensor_common_remove(struct iio_dev *indio_dev);
  175. const struct gsensor_settings *gsensor_get_settings(const char *name);
  176. /* Buffer functions */
  177. int gsensor_allocate_ring(struct iio_dev *indio_dev);
  178. int gsensor_trig_set_state(struct iio_trigger *trig, bool state);
  179. int gsensor_allocate_trigger(struct iio_dev *indio_dev);
  180. extern const struct iio_trigger_ops gsensor_trigger_ops;
  181. /* Power management */
  182. int gsensor_power_enable(struct iio_dev *indio_dev);
  183. int gsensor_power_disable(struct iio_dev *indio_dev);
  184. /* I2C functions */
  185. int gsensor_i2c_read(struct device *dev, u8 reg, u8 *data, size_t len);
  186. int gsensor_i2c_write(struct device *dev, u8 reg, u8 value);
  187. int gsensor_init_main(void);
  188. void gsensor_exit_main(void);
  189. int gsensor_read_raw_data(struct gsensor_data *gs, int *x, int *y, int *z);
  190. int gsensor_set_enable(struct gsensor_data *gs, bool enable);
  191. int gsensor_set_dataready_irq(struct gsensor_data *gs, bool enable);
  192. /* Helper macros */
  193. #define GSENSOR_DEV_ATTR_SAMP_FREQ_AVAIL() \
  194. IIO_DEV_ATTR_SAMP_FREQ_AVAIL(gsensor_show_available_odr)
  195. #define GSENSOR_DEV_ATTR_SCALE_AVAIL() \
  196. IIO_DEVICE_ATTR(in_accel_scale_available, 0444, \
  197. gsensor_show_available_scale, NULL, 0)
  198. #endif /* __GSENSOR_H */