sx_common.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright 2021 Google LLC.
  4. *
  5. * Code shared between most Semtech SAR sensor driver.
  6. */
  7. #ifndef IIO_SX_COMMON_H
  8. #define IIO_SX_COMMON_H
  9. #include <linux/iio/iio.h>
  10. #include <linux/iio/types.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/types.h>
  13. struct device;
  14. struct i2c_client;
  15. struct regmap_config;
  16. struct sx_common_data;
  17. #define SX_COMMON_REG_IRQ_SRC 0x00
  18. #define SX_COMMON_MAX_NUM_CHANNELS 4
  19. static_assert(SX_COMMON_MAX_NUM_CHANNELS < BITS_PER_LONG);
  20. struct sx_common_reg_default {
  21. u8 reg;
  22. u8 def;
  23. const char *property;
  24. };
  25. /**
  26. * struct sx_common_ops: function pointers needed by common code
  27. *
  28. * List functions needed by common code to gather information or configure
  29. * the sensor.
  30. *
  31. * @read_prox_data: Function to read raw proximity data.
  32. * @check_whoami: Set device name based on whoami register.
  33. * @init_compensation: Function to set initial compensation.
  34. * @wait_for_sample: When there are no physical IRQ, function to wait for a
  35. * sample to be ready.
  36. * @get_default_reg: Populate the initial value for a given register.
  37. */
  38. struct sx_common_ops {
  39. int (*read_prox_data)(struct sx_common_data *data,
  40. const struct iio_chan_spec *chan, __be16 *val);
  41. int (*check_whoami)(struct device *dev, struct iio_dev *indio_dev);
  42. int (*init_compensation)(struct iio_dev *indio_dev);
  43. int (*wait_for_sample)(struct sx_common_data *data);
  44. const struct sx_common_reg_default *
  45. (*get_default_reg)(struct device *dev, int idx,
  46. struct sx_common_reg_default *reg_def);
  47. };
  48. /**
  49. * struct sx_common_chip_info: Semtech Sensor private chip information
  50. *
  51. * @reg_stat: Main status register address.
  52. * @reg_irq_msk: IRQ mask register address.
  53. * @reg_enable_chan: Address to enable/disable channels.
  54. * Each phase presented by the sensor is an IIO channel..
  55. * @reg_reset: Reset register address.
  56. * @mask_enable_chan: Mask over the channels bits in the enable channel
  57. * register.
  58. * @stat_offset: Offset to check phase status.
  59. * @irq_msk_offset: Offset to enable interrupt in the IRQ mask
  60. * register.
  61. * @num_channels: Number of channels.
  62. * @num_default_regs: Number of internal registers that can be configured.
  63. *
  64. * @ops: Private functions pointers.
  65. * @iio_channels: Description of exposed iio channels.
  66. * @num_iio_channels: Number of iio_channels.
  67. * @iio_info: iio_info structure for this driver.
  68. */
  69. struct sx_common_chip_info {
  70. unsigned int reg_stat;
  71. unsigned int reg_irq_msk;
  72. unsigned int reg_enable_chan;
  73. unsigned int reg_reset;
  74. unsigned int mask_enable_chan;
  75. unsigned int stat_offset;
  76. unsigned int irq_msk_offset;
  77. unsigned int num_channels;
  78. int num_default_regs;
  79. struct sx_common_ops ops;
  80. const struct iio_chan_spec *iio_channels;
  81. int num_iio_channels;
  82. struct iio_info iio_info;
  83. };
  84. /**
  85. * struct sx_common_data: Semtech Sensor private data structure.
  86. *
  87. * @chip_info: Structure defining sensor internals.
  88. * @mutex: Serialize access to registers and channel configuration.
  89. * @completion: completion object to wait for data acquisition.
  90. * @client: I2C client structure.
  91. * @trig: IIO trigger object.
  92. * @regmap: Register map.
  93. * @chan_prox_stat: Last reading of the proximity status for each channel.
  94. * We only send an event to user space when this changes.
  95. * @trigger_enabled: True when the device trigger is enabled.
  96. * @buffer: Buffer to store raw samples.
  97. * @suspend_ctrl: Remember enabled channels and sample rate during suspend.
  98. * @chan_read: Bit field for each raw channel enabled.
  99. * @chan_event: Bit field for each event enabled.
  100. */
  101. struct sx_common_data {
  102. const struct sx_common_chip_info *chip_info;
  103. struct mutex mutex;
  104. struct completion completion;
  105. struct i2c_client *client;
  106. struct iio_trigger *trig;
  107. struct regmap *regmap;
  108. unsigned long chan_prox_stat;
  109. bool trigger_enabled;
  110. /* Ensure correct alignment of timestamp when present. */
  111. struct {
  112. __be16 channels[SX_COMMON_MAX_NUM_CHANNELS];
  113. aligned_s64 ts;
  114. } buffer;
  115. unsigned int suspend_ctrl;
  116. unsigned long chan_read;
  117. unsigned long chan_event;
  118. };
  119. int sx_common_read_proximity(struct sx_common_data *data,
  120. const struct iio_chan_spec *chan, int *val);
  121. int sx_common_read_event_config(struct iio_dev *indio_dev,
  122. const struct iio_chan_spec *chan,
  123. enum iio_event_type type,
  124. enum iio_event_direction dir);
  125. int sx_common_write_event_config(struct iio_dev *indio_dev,
  126. const struct iio_chan_spec *chan,
  127. enum iio_event_type type,
  128. enum iio_event_direction dir, bool state);
  129. int sx_common_probe(struct i2c_client *client,
  130. const struct sx_common_chip_info *chip_info,
  131. const struct regmap_config *regmap_config);
  132. /* 3 is the number of events defined by a single phase. */
  133. extern const struct iio_event_spec sx_common_events[3];
  134. #endif /* IIO_SX_COMMON_H */