summaryrefslogtreecommitdiffstats
path: root/drivers/iio/adc/meson_saradc.c
diff options
context:
space:
mode:
authorGreg Kroah-Hartman2017-04-03 15:29:12 +0200
committerGreg Kroah-Hartman2017-04-03 15:29:12 +0200
commitedf5e79422a9dd98fb896606124edd8407d97d70 (patch)
treef7d2921071b0e1858a484c66bbc643b40e3b62e2 /drivers/iio/adc/meson_saradc.c
parentStaging: lustre: lnet: code style fix (diff)
parentiio: imu: st_lsm6dsx: do not apply ODR configuration in write_raw handler (diff)
downloadkernel-qcow2-linux-edf5e79422a9dd98fb896606124edd8407d97d70.tar.gz
kernel-qcow2-linux-edf5e79422a9dd98fb896606124edd8407d97d70.tar.xz
kernel-qcow2-linux-edf5e79422a9dd98fb896606124edd8407d97d70.zip
Merge tag 'iio-for-4.12c' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-next
Third set of new device support, cleanups and features for IIO in the 4.12 cycle Somewhat dominated in patch numbers of last of the outreachy application window related patches (they are still coming, despite window being closed which is good to see!) Good set of new drivers as well. New device support * ASPEED ADC - new driver * cpcap PMIC ADC - new driver * hid-humidity - driver for HID compatible humidity sensors. * ltc2497 ADC - new driver * mpu6050 - bring bindings up to date and add trivial support for 9250 * rockchip-saradc - update bindings to cover rk3328 * vl6180 light, proximity and time of flight sensor. - new driver Features * meson-saradc - add calibration Cleanup and minor fixes * ad5504 - constify attribute_group structure - drop casting of void * * ad7150 - replace some shifts of 1 by BIT macro usage * ad7152 - blank lines between function definitions * ad7280a - octal permissions. * ad7606 - replace use of core mlock mutex with a local lock * ad7746 - replace some shifts of 1 by BIT macro usage - function parameter alignment - drop some excessive brackets (introduced in last pull request) * ad7753 - white space cleanup * ad7754 - includes in alphabetical order and groupped appropriately. - change from missuse of internal mlock mutex to using the buffer lock to also protect values during frequency update. * ad779x - constify attribute_group structures * ad9832 - octal permissions * adis16060 - remove use of core mlock mutex in favour of adding a local _spi_write_then_read which can use the local buffer protection lock. - fix naming of above function. * adis16203 - remove locking during reads of calibbias that doesn't protect anything not protected elsewhere. * adis16209 - remove unnecessary braces in single statement if * adis16240 - remove unnecessary braces in single statement if * adt7136 - drop excess blank lines and put some in between functions. * ams-iaq - replace comma with semi colon. Not actual bug, just unusual syntax. * apds9960 - constify attribute group structure * as3935 - constify attribute group structure * bm1750 - constify attribute group structure * cros_ec - devm version of triggered buffer setup to simplify code. * exynos - drop casting of void * * hdc100x - constify attribute_group structure * hid-accel - fix wrong scale for newly introduced gravity sensor. * hts221 - drop casting of void * * hx711 - constify attribute_group structure * imx7d_adc - drop casting of void * * lm35333 - constify attribute_group structure * lsm6dsx - drop casting of void * - hold ODR configuration until enabling to avoid a race condition. * max1027 - drop casting of void * * max11100 - fix a comma where semicolon was intended (no actual bug, just odd) * max1363 - constify attribute_group structure * ms sensors - drop casting of void * * rockchip_saradc - drop casting of void * * sun4i-gpadc - fix missing dependency on THERMAL or presence of stubs (issue only introduced in pervious set) - drop casting of void * * tsl2x7x - fix wrong standard deviation calc. Note these aren't actually used for anything at the moment so bug didn't really matter. - constify attribute group structure. * vf610adc - drop casting of void * * vz89x - replace comma with semicolon. Not actual bug, just odd syntax. * zpa2326 - drop casting of void *
Diffstat (limited to 'drivers/iio/adc/meson_saradc.c')
-rw-r--r--drivers/iio/adc/meson_saradc.c77
1 files changed, 75 insertions, 2 deletions
diff --git a/drivers/iio/adc/meson_saradc.c b/drivers/iio/adc/meson_saradc.c
index cde9ca7a01b8..dd4190b50df6 100644
--- a/drivers/iio/adc/meson_saradc.c
+++ b/drivers/iio/adc/meson_saradc.c
@@ -166,6 +166,8 @@
#define MESON_SAR_ADC_MAX_FIFO_SIZE 32
#define MESON_SAR_ADC_TIMEOUT 100 /* ms */
+/* for use with IIO_VAL_INT_PLUS_MICRO */
+#define MILLION 1000000
#define MESON_SAR_ADC_CHAN(_chan) { \
.type = IIO_VOLTAGE, \
@@ -173,7 +175,9 @@
.channel = _chan, \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
- .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
+ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
+ BIT(IIO_CHAN_INFO_CALIBBIAS) | \
+ BIT(IIO_CHAN_INFO_CALIBSCALE), \
.datasheet_name = "SAR_ADC_CH"#_chan, \
}
@@ -233,6 +237,8 @@ struct meson_sar_adc_priv {
struct clk *adc_div_clk;
struct clk_divider clk_div;
struct completion done;
+ int calibbias;
+ int calibscale;
};
static const struct regmap_config meson_sar_adc_regmap_config = {
@@ -252,6 +258,17 @@ static unsigned int meson_sar_adc_get_fifo_count(struct iio_dev *indio_dev)
return FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval);
}
+static int meson_sar_adc_calib_val(struct iio_dev *indio_dev, int val)
+{
+ struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
+ int tmp;
+
+ /* use val_calib = scale * val_raw + offset calibration function */
+ tmp = div_s64((s64)val * priv->calibscale, MILLION) + priv->calibbias;
+
+ return clamp(tmp, 0, (1 << priv->data->resolution) - 1);
+}
+
static int meson_sar_adc_wait_busy_clear(struct iio_dev *indio_dev)
{
struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
@@ -302,7 +319,7 @@ static int meson_sar_adc_read_raw_sample(struct iio_dev *indio_dev,
fifo_val = FIELD_GET(MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK, regval);
fifo_val &= GENMASK(priv->data->resolution - 1, 0);
- *val = fifo_val;
+ *val = meson_sar_adc_calib_val(indio_dev, fifo_val);
return 0;
}
@@ -527,6 +544,15 @@ static int meson_sar_adc_iio_info_read_raw(struct iio_dev *indio_dev,
*val2 = priv->data->resolution;
return IIO_VAL_FRACTIONAL_LOG2;
+ case IIO_CHAN_INFO_CALIBBIAS:
+ *val = priv->calibbias;
+ return IIO_VAL_INT;
+
+ case IIO_CHAN_INFO_CALIBSCALE:
+ *val = priv->calibscale / MILLION;
+ *val2 = priv->calibscale % MILLION;
+ return IIO_VAL_INT_PLUS_MICRO;
+
default:
return -EINVAL;
}
@@ -762,6 +788,47 @@ static irqreturn_t meson_sar_adc_irq(int irq, void *data)
return IRQ_HANDLED;
}
+static int meson_sar_adc_calib(struct iio_dev *indio_dev)
+{
+ struct meson_sar_adc_priv *priv = iio_priv(indio_dev);
+ int ret, nominal0, nominal1, value0, value1;
+
+ /* use points 25% and 75% for calibration */
+ nominal0 = (1 << priv->data->resolution) / 4;
+ nominal1 = (1 << priv->data->resolution) * 3 / 4;
+
+ meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_VDD_DIV4);
+ usleep_range(10, 20);
+ ret = meson_sar_adc_get_sample(indio_dev,
+ &meson_sar_adc_iio_channels[7],
+ MEAN_AVERAGING, EIGHT_SAMPLES, &value0);
+ if (ret < 0)
+ goto out;
+
+ meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_VDD_MUL3_DIV4);
+ usleep_range(10, 20);
+ ret = meson_sar_adc_get_sample(indio_dev,
+ &meson_sar_adc_iio_channels[7],
+ MEAN_AVERAGING, EIGHT_SAMPLES, &value1);
+ if (ret < 0)
+ goto out;
+
+ if (value1 <= value0) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ priv->calibscale = div_s64((nominal1 - nominal0) * (s64)MILLION,
+ value1 - value0);
+ priv->calibbias = nominal0 - div_s64((s64)value0 * priv->calibscale,
+ MILLION);
+ ret = 0;
+out:
+ meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_CH7_INPUT);
+
+ return ret;
+}
+
static const struct iio_info meson_sar_adc_iio_info = {
.read_raw = meson_sar_adc_iio_info_read_raw,
.driver_module = THIS_MODULE,
@@ -901,6 +968,8 @@ static int meson_sar_adc_probe(struct platform_device *pdev)
return PTR_ERR(priv->vref);
}
+ priv->calibscale = MILLION;
+
ret = meson_sar_adc_init(indio_dev);
if (ret)
goto err;
@@ -909,6 +978,10 @@ static int meson_sar_adc_probe(struct platform_device *pdev)
if (ret)
goto err;
+ ret = meson_sar_adc_calib(indio_dev);
+ if (ret)
+ dev_warn(&pdev->dev, "calibration failed\n");
+
platform_set_drvdata(pdev, indio_dev);
ret = iio_device_register(indio_dev);