sx_common.h 4.8 KB

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