Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright 2021 Google LLC.
0004  *
0005  * Code shared between most Semtech SAR sensor driver.
0006  */
0007 
0008 #ifndef IIO_SX_COMMON_H
0009 #define IIO_SX_COMMON_H
0010 
0011 #include <linux/iio/iio.h>
0012 #include <linux/iio/types.h>
0013 #include <linux/regulator/consumer.h>
0014 #include <linux/types.h>
0015 
0016 struct device;
0017 struct i2c_client;
0018 struct regmap_config;
0019 struct sx_common_data;
0020 
0021 #define SX_COMMON_REG_IRQ_SRC               0x00
0022 
0023 #define SX_COMMON_MAX_NUM_CHANNELS  4
0024 static_assert(SX_COMMON_MAX_NUM_CHANNELS < BITS_PER_LONG);
0025 
0026 struct sx_common_reg_default {
0027     u8 reg;
0028     u8 def;
0029 };
0030 
0031 /**
0032  * struct sx_common_ops: function pointers needed by common code
0033  *
0034  * List functions needed by common code to gather information or configure
0035  * the sensor.
0036  *
0037  * @read_prox_data: Function to read raw proximity data.
0038  * @check_whoami:   Set device name based on whoami register.
0039  * @init_compensation:  Function to set initial compensation.
0040  * @wait_for_sample:    When there are no physical IRQ, function to wait for a
0041  *          sample to be ready.
0042  * @get_default_reg:    Populate the initial value for a given register.
0043  */
0044 struct sx_common_ops {
0045     int (*read_prox_data)(struct sx_common_data *data,
0046                   const struct iio_chan_spec *chan, __be16 *val);
0047     int (*check_whoami)(struct device *dev, struct iio_dev *indio_dev);
0048     int (*init_compensation)(struct iio_dev *indio_dev);
0049     int (*wait_for_sample)(struct sx_common_data *data);
0050     const struct sx_common_reg_default  *
0051         (*get_default_reg)(struct device *dev, int idx,
0052                    struct sx_common_reg_default *reg_def);
0053 };
0054 
0055 /**
0056  * struct sx_common_chip_info: Semtech Sensor private chip information
0057  *
0058  * @reg_stat:       Main status register address.
0059  * @reg_irq_msk:    IRQ mask register address.
0060  * @reg_enable_chan:    Address to enable/disable channels.
0061  *          Each phase presented by the sensor is an IIO channel..
0062  * @reg_reset:      Reset register address.
0063  * @mask_enable_chan:   Mask over the channels bits in the enable channel
0064  *          register.
0065  * @stat_offset:    Offset to check phase status.
0066  * @irq_msk_offset: Offset to enable interrupt in the IRQ mask
0067  *          register.
0068  * @num_channels:   Number of channels.
0069  * @num_default_regs:   Number of internal registers that can be configured.
0070  *
0071  * @ops:        Private functions pointers.
0072  * @iio_channels:   Description of exposed iio channels.
0073  * @num_iio_channels:   Number of iio_channels.
0074  * @iio_info:       iio_info structure for this driver.
0075  */
0076 struct sx_common_chip_info {
0077     unsigned int reg_stat;
0078     unsigned int reg_irq_msk;
0079     unsigned int reg_enable_chan;
0080     unsigned int reg_reset;
0081 
0082     unsigned int mask_enable_chan;
0083     unsigned int stat_offset;
0084     unsigned int irq_msk_offset;
0085     unsigned int num_channels;
0086     int num_default_regs;
0087 
0088     struct sx_common_ops ops;
0089 
0090     const struct iio_chan_spec *iio_channels;
0091     int num_iio_channels;
0092     struct iio_info iio_info;
0093 };
0094 
0095 /**
0096  * struct sx_common_data: Semtech Sensor private data structure.
0097  *
0098  * @chip_info:      Structure defining sensor internals.
0099  * @mutex:      Serialize access to registers and channel configuration.
0100  * @completion:     completion object to wait for data acquisition.
0101  * @client:     I2C client structure.
0102  * @trig:       IIO trigger object.
0103  * @regmap:     Register map.
0104  * @num_default_regs:   Number of default registers to set at init.
0105  * @supplies:       Power supplies object.
0106  * @chan_prox_stat: Last reading of the proximity status for each channel.
0107  *          We only send an event to user space when this changes.
0108  * @trigger_enabled:    True when the device trigger is enabled.
0109  * @buffer:     Buffer to store raw samples.
0110  * @suspend_ctrl:   Remember enabled channels and sample rate during suspend.
0111  * @chan_read:      Bit field for each raw channel enabled.
0112  * @chan_event:     Bit field for each event enabled.
0113  */
0114 struct sx_common_data {
0115     const struct sx_common_chip_info *chip_info;
0116 
0117     struct mutex mutex;
0118     struct completion completion;
0119     struct i2c_client *client;
0120     struct iio_trigger *trig;
0121     struct regmap *regmap;
0122 
0123     struct regulator_bulk_data supplies[2];
0124     unsigned long chan_prox_stat;
0125     bool trigger_enabled;
0126 
0127     /* Ensure correct alignment of timestamp when present. */
0128     struct {
0129         __be16 channels[SX_COMMON_MAX_NUM_CHANNELS];
0130         s64 ts __aligned(8);
0131     } buffer;
0132 
0133     unsigned int suspend_ctrl;
0134     unsigned long chan_read;
0135     unsigned long chan_event;
0136 };
0137 
0138 int sx_common_read_proximity(struct sx_common_data *data,
0139                  const struct iio_chan_spec *chan, int *val);
0140 
0141 int sx_common_read_event_config(struct iio_dev *indio_dev,
0142                 const struct iio_chan_spec *chan,
0143                 enum iio_event_type type,
0144                 enum iio_event_direction dir);
0145 int sx_common_write_event_config(struct iio_dev *indio_dev,
0146                  const struct iio_chan_spec *chan,
0147                  enum iio_event_type type,
0148                  enum iio_event_direction dir, int state);
0149 
0150 int sx_common_probe(struct i2c_client *client,
0151             const struct sx_common_chip_info *chip_info,
0152             const struct regmap_config *regmap_config);
0153 
0154 /* 3 is the number of events defined by a single phase. */
0155 extern const struct iio_event_spec sx_common_events[3];
0156 
0157 #endif  /* IIO_SX_COMMON_H */