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