#include "ad7606.h"
 
+#define AD7606_CALIB_GAIN_MIN  0
+#define AD7606_CALIB_GAIN_STEP 1024
+#define AD7606_CALIB_GAIN_MAX  (63 * AD7606_CALIB_GAIN_STEP)
+
 /*
  * Scales are computed as 5000/32768 and 10000/32768 respectively,
  * so that when applied to the raw values they provide mV values.
        .scale_setup_cb = ad7606_16bit_chan_scale_setup,
        .sw_setup_cb = ad7606b_sw_mode_setup,
        .offload_storagebits = 32,
+       .calib_gain_avail = true,
        .calib_offset_avail = ad7606_calib_offset_avail,
        .calib_phase_avail = ad7606b_calib_phase_avail,
 };
        .scale_setup_cb = ad7606c_16bit_chan_scale_setup,
        .sw_setup_cb = ad7606b_sw_mode_setup,
        .offload_storagebits = 32,
+       .calib_gain_avail = true,
        .calib_offset_avail = ad7606_calib_offset_avail,
        .calib_phase_avail = ad7606c_calib_phase_avail,
 };
        .scale_setup_cb = ad7606c_18bit_chan_scale_setup,
        .sw_setup_cb = ad7606b_sw_mode_setup,
        .offload_storagebits = 32,
+       .calib_gain_avail = true,
        .calib_offset_avail = ad7606c_18bit_calib_offset_avail,
        .calib_phase_avail = ad7606c_calib_phase_avail,
 };
                                  bool *bipolar, bool *differential)
 {
        struct ad7606_state *st = iio_priv(indio_dev);
+       struct ad7606_chan_info *ci;
        unsigned int num_channels = st->chip_info->num_adc_channels;
        struct device *dev = st->dev;
        int ret;
                        return -EINVAL;
                }
 
+               ci = &st->chan_info[reg - 1];
+
+               ci->r_gain = 0;
+               ret = fwnode_property_read_u32(child, "adi,rfilter-ohms",
+                                              &ci->r_gain);
+               if (ret == 0 && ci->r_gain > AD7606_CALIB_GAIN_MAX)
+                       return -EINVAL;
+
                return 0;
        }
 
        return st->bops->sw_mode_config(indio_dev);
 }
 
+static int ad7606_set_gain_calib(struct ad7606_state *st)
+{
+       struct ad7606_chan_info *ci;
+       int i, ret;
+
+       for (i = 0; i < st->chip_info->num_adc_channels; i++) {
+               ci = &st->chan_info[i];
+               ret = st->bops->reg_write(st, AD7606_CALIB_GAIN(i),
+                                         DIV_ROUND_CLOSEST(ci->r_gain,
+                                               AD7606_CALIB_GAIN_STEP));
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
 static int ad7606_probe_channels(struct iio_dev *indio_dev)
 {
        struct ad7606_state *st = iio_priv(indio_dev);
                st->chip_info->sw_setup_cb(indio_dev);
        }
 
+       if (st->sw_mode_en && st->chip_info->calib_gain_avail) {
+               ret = ad7606_set_gain_calib(st);
+               if (ret)
+                       return ret;
+       }
+
        return devm_iio_device_register(dev, indio_dev);
 }
 EXPORT_SYMBOL_NS_GPL(ad7606_probe, "IIO_AD7606");
 
  * @init_delay_ms:     required delay in milliseconds for initialization
  *                     after a restart
  * @offload_storagebits: storage bits used by the offload hw implementation
+ * @calib_gain_avail:   chip supports gain calibration
  * @calib_offset_avail: pointer to offset calibration range/limits array
  * @calib_phase_avail:  pointer to phase calibration range/limits array
  */
        bool                            os_req_reset;
        unsigned long                   init_delay_ms;
        u8                              offload_storagebits;
+       bool                            calib_gain_avail;
        const int                       *calib_offset_avail;
        const int                       (*calib_phase_avail)[2];
 };
  * @range:             voltage range selection, selects which scale to apply
  * @reg_offset:                offset for the register value, to be applied when
  *                     writing the value of 'range' to the register value
+ * @r_gain:            gain resistor value in ohms, to be set to match the
+ *                      external r_filter value
  */
 struct ad7606_chan_info {
 #define AD760X_MAX_SCALES              16
        unsigned int                    num_scales;
        unsigned int                    range;
        unsigned int                    reg_offset;
+       unsigned int                    r_gain;
 };
 
 /**