Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Support code for Analog Devices Sigma-Delta ADCs
0004  *
0005  * Copyright 2012 Analog Devices Inc.
0006  *  Author: Lars-Peter Clausen <lars@metafoo.de>
0007  */
0008 #ifndef __AD_SIGMA_DELTA_H__
0009 #define __AD_SIGMA_DELTA_H__
0010 
0011 enum ad_sigma_delta_mode {
0012     AD_SD_MODE_CONTINUOUS = 0,
0013     AD_SD_MODE_SINGLE = 1,
0014     AD_SD_MODE_IDLE = 2,
0015     AD_SD_MODE_POWERDOWN = 3,
0016 };
0017 
0018 /**
0019  * struct ad_sigma_delta_calib_data - Calibration data for Sigma Delta devices
0020  * @mode: Calibration mode.
0021  * @channel: Calibration channel.
0022  */
0023 struct ad_sd_calib_data {
0024     unsigned int mode;
0025     unsigned int channel;
0026 };
0027 
0028 struct ad_sigma_delta;
0029 struct device;
0030 struct iio_dev;
0031 
0032 /**
0033  * struct ad_sigma_delta_info - Sigma Delta driver specific callbacks and options
0034  * @set_channel: Will be called to select the current channel, may be NULL.
0035  * @append_status: Will be called to enable status append at the end of the sample, may be NULL.
0036  * @set_mode: Will be called to select the current mode, may be NULL.
0037  * @disable_all: Will be called to disable all channels, may be NULL.
0038  * @postprocess_sample: Is called for each sampled data word, can be used to
0039  *      modify or drop the sample data, it, may be NULL.
0040  * @has_registers: true if the device has writable and readable registers, false
0041  *      if there is just one read-only sample data shift register.
0042  * @addr_shift: Shift of the register address in the communications register.
0043  * @read_mask: Mask for the communications register having the read bit set.
0044  * @status_ch_mask: Mask for the channel number stored in status register.
0045  * @data_reg: Address of the data register, if 0 the default address of 0x3 will
0046  *   be used.
0047  * @irq_flags: flags for the interrupt used by the triggered buffer
0048  * @num_slots: Number of sequencer slots
0049  */
0050 struct ad_sigma_delta_info {
0051     int (*set_channel)(struct ad_sigma_delta *, unsigned int channel);
0052     int (*append_status)(struct ad_sigma_delta *, bool append);
0053     int (*set_mode)(struct ad_sigma_delta *, enum ad_sigma_delta_mode mode);
0054     int (*disable_all)(struct ad_sigma_delta *);
0055     int (*postprocess_sample)(struct ad_sigma_delta *, unsigned int raw_sample);
0056     bool has_registers;
0057     unsigned int addr_shift;
0058     unsigned int read_mask;
0059     unsigned int status_ch_mask;
0060     unsigned int data_reg;
0061     unsigned long irq_flags;
0062     unsigned int num_slots;
0063 };
0064 
0065 /**
0066  * struct ad_sigma_delta - Sigma Delta device struct
0067  * @spi: The spi device associated with the Sigma Delta device.
0068  * @trig: The IIO trigger associated with the Sigma Delta device.
0069  *
0070  * Most of the fields are private to the sigma delta library code and should not
0071  * be accessed by individual drivers.
0072  */
0073 struct ad_sigma_delta {
0074     struct spi_device   *spi;
0075     struct iio_trigger  *trig;
0076 
0077 /* private: */
0078     struct completion   completion;
0079     bool            irq_dis;
0080 
0081     bool            bus_locked;
0082     bool            keep_cs_asserted;
0083 
0084     uint8_t         comm;
0085 
0086     const struct ad_sigma_delta_info *info;
0087     unsigned int        active_slots;
0088     unsigned int        current_slot;
0089     unsigned int        num_slots;
0090     bool            status_appended;
0091     /* map slots to channels in order to know what to expect from devices */
0092     unsigned int        *slots;
0093     uint8_t         *samples_buf;
0094 
0095     /*
0096      * DMA (thus cache coherency maintenance) requires the
0097      * transfer buffers to live in their own cache lines.
0098      * 'tx_buf' is up to 32 bits.
0099      * 'rx_buf' is up to 32 bits per sample + 64 bit timestamp,
0100      * rounded to 16 bytes to take into account padding.
0101      */
0102     uint8_t             tx_buf[4] ____cacheline_aligned;
0103     uint8_t             rx_buf[16] __aligned(8);
0104 };
0105 
0106 static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,
0107     unsigned int channel)
0108 {
0109     if (sd->info->set_channel)
0110         return sd->info->set_channel(sd, channel);
0111 
0112     return 0;
0113 }
0114 
0115 static inline int ad_sigma_delta_append_status(struct ad_sigma_delta *sd, bool append)
0116 {
0117     int ret;
0118 
0119     if (sd->info->append_status) {
0120         ret = sd->info->append_status(sd, append);
0121         if (ret < 0)
0122             return ret;
0123 
0124         sd->status_appended = append;
0125     }
0126 
0127     return 0;
0128 }
0129 
0130 static inline int ad_sigma_delta_disable_all(struct ad_sigma_delta *sd)
0131 {
0132     if (sd->info->disable_all)
0133         return sd->info->disable_all(sd);
0134 
0135     return 0;
0136 }
0137 
0138 static inline int ad_sigma_delta_set_mode(struct ad_sigma_delta *sd,
0139     unsigned int mode)
0140 {
0141     if (sd->info->set_mode)
0142         return sd->info->set_mode(sd, mode);
0143 
0144     return 0;
0145 }
0146 
0147 static inline int ad_sigma_delta_postprocess_sample(struct ad_sigma_delta *sd,
0148     unsigned int raw_sample)
0149 {
0150     if (sd->info->postprocess_sample)
0151         return sd->info->postprocess_sample(sd, raw_sample);
0152 
0153     return 0;
0154 }
0155 
0156 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm);
0157 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
0158     unsigned int size, unsigned int val);
0159 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
0160     unsigned int size, unsigned int *val);
0161 
0162 int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
0163     unsigned int reset_length);
0164 
0165 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
0166     const struct iio_chan_spec *chan, int *val);
0167 int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
0168     unsigned int mode, unsigned int channel);
0169 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
0170     const struct ad_sd_calib_data *cd, unsigned int n);
0171 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
0172     struct spi_device *spi, const struct ad_sigma_delta_info *info);
0173 
0174 int devm_ad_sd_setup_buffer_and_trigger(struct device *dev, struct iio_dev *indio_dev);
0175 
0176 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig);
0177 
0178 #endif