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