From patchwork Thu Dec 15 22:48:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 88239 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1071137qgi; Thu, 15 Dec 2016 14:48:37 -0800 (PST) X-Received: by 10.84.143.68 with SMTP id 62mr6996237ply.63.1481842117674; Thu, 15 Dec 2016 14:48:37 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g65si4317743pfk.269.2016.12.15.14.48.37; Thu, 15 Dec 2016 14:48:37 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-arm-msm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-arm-msm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-arm-msm-owner@vger.kernel.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756688AbcLOWsg (ORCPT + 9 others); Thu, 15 Dec 2016 17:48:36 -0500 Received: from mail-wm0-f51.google.com ([74.125.82.51]:36542 "EHLO mail-wm0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756684AbcLOWsf (ORCPT ); Thu, 15 Dec 2016 17:48:35 -0500 Received: by mail-wm0-f51.google.com with SMTP id g23so7475840wme.1 for ; Thu, 15 Dec 2016 14:48:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=AiQUp+/9VZkYFygYybdQEVzhuAgvzHOtcDG1A5uUjJE=; b=ZZOZNr4S/tlPreCAYDVSZwvGd2Jdo/G7B83QFpX4kzuUn7sbwk86ukCeCV9v4O4yqG rUoqIJrrIbespLo3uYLT4uy1GNE+Ldyai56Sx8GtC8QETY35d7LUADYtJQPkSanK4zv/ G/pWGrZIlbcwKjJHYpqkSurrh177cmGe67Hq8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=AiQUp+/9VZkYFygYybdQEVzhuAgvzHOtcDG1A5uUjJE=; b=Y5R3FJ+7RUob18CDi7KGCiEPE42JszFCZrA4qCGhZSLMH4vq8VuoNII3u+3n1xC1YB 2rXuWLbag9jIwN3i6r3+2cc+fqeqQLCQ3VSDsy3fnBG+Lt2qHvgJm4ErkpikNSCRKyGk Oe6nJmXKv8a9BsxKHKXO+qboBD6PJ4jUYUMC1gE//uR7DQtZNjd96aDrGpL4lr39Doye pi9ah9GEt1f6GDXDLsGMQ4K1JHUwMSLPUR/wZMzsLW/Q9vXmRTouFek3uuSe7jRwhxyF Se/tmlntIENIp6/cqnlOAzDow54bOhaCaxlJHufa1mVxqLoFRcOwt9ttzXo6KiUwLFMG uxzg== X-Gm-Message-State: AKaTC02xhJU3DhvSeCEcKN8+xV3aP+SbFxfS3h9QiwLSpdukBV4zgOkYNSj39z2165NLET1Q X-Received: by 10.25.151.69 with SMTP id z66mr1188687lfd.98.1481842113470; Thu, 15 Dec 2016 14:48:33 -0800 (PST) Received: from localhost.localdomain.localdomain (c-357171d5.014-348-6c756e10.cust.bredbandsbolaget.se. [213.113.113.53]) by smtp.gmail.com with ESMTPSA id o103sm831222lfi.44.2016.12.15.14.48.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Dec 2016 14:48:32 -0800 (PST) From: Linus Walleij To: Jonathan Cameron , linux-iio@vger.kernel.org Cc: Linus Walleij , linux-arm-kernel@lists.infradead.org, linux-arm-msm@vger.kernel.org, "Ivan T . Ivanov" , Andy Gross , Bjorn Andersson , Stephen Boyd , Srinivas Kandagatla Subject: [PATCH 2/3 v2] iio: adc: break out common code from SPMI VADC Date: Thu, 15 Dec 2016 23:48:25 +0100 Message-Id: <1481842105-14047-1-git-send-email-linus.walleij@linaro.org> X-Mailer: git-send-email 2.7.4 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org The SPMI VADC and the earlier XOADC share a subset of common code, so to be able to use the same code in both drivers, we break out a separate file with the common code, prefix exported functions that are no longer static with qcom_* and bake an object qcom-vadc.o that contains both files: qcom-vadc-common.o and qcom-spmi-vadc.o. Cc: linux-arm-kernel@lists.infradead.org Cc: linux-arm-msm@vger.kernel.org Cc: Ivan T. Ivanov Cc: Andy Gross Cc: Bjorn Andersson Cc: Stephen Boyd Cc: Srinivas Kandagatla Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - No changes just reposting --- drivers/iio/adc/Makefile | 3 +- drivers/iio/adc/qcom-spmi-vadc.c | 95 +++----------------------------------- drivers/iio/adc/qcom-vadc-common.c | 38 +++++++++++++++ drivers/iio/adc/qcom-vadc-common.h | 69 +++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 89 deletions(-) create mode 100644 drivers/iio/adc/qcom-vadc-common.c create mode 100644 drivers/iio/adc/qcom-vadc-common.h -- 2.7.4 -- To unsubscribe from this list: send the line "unsubscribe linux-arm-msm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile index 7a40c04c311f..f9468d228b1e 100644 --- a/drivers/iio/adc/Makefile +++ b/drivers/iio/adc/Makefile @@ -38,7 +38,8 @@ obj-$(CONFIG_MXS_LRADC) += mxs-lradc.o obj-$(CONFIG_NAU7802) += nau7802.o obj-$(CONFIG_PALMAS_GPADC) += palmas_gpadc.o obj-$(CONFIG_QCOM_SPMI_IADC) += qcom-spmi-iadc.o -obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o +qcom-vadc-y := qcom-vadc-common.o +obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-vadc.o qcom-spmi-vadc.o obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o obj-$(CONFIG_STX104) += stx104.o obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o diff --git a/drivers/iio/adc/qcom-spmi-vadc.c b/drivers/iio/adc/qcom-spmi-vadc.c index c2babe50a0d8..74d21afa34a9 100644 --- a/drivers/iio/adc/qcom-spmi-vadc.c +++ b/drivers/iio/adc/qcom-spmi-vadc.c @@ -28,6 +28,8 @@ #include +#include "qcom-vadc-common.h" + /* VADC register and bit definitions */ #define VADC_REVISION2 0x1 #define VADC_REVISION2_SUPPORTED_VADC 1 @@ -75,69 +77,9 @@ #define VADC_DATA 0x60 /* 16 bits */ -#define VADC_CONV_TIME_MIN_US 2000 -#define VADC_CONV_TIME_MAX_US 2100 - -/* Min ADC code represents 0V */ -#define VADC_MIN_ADC_CODE 0x6000 -/* Max ADC code represents full-scale range of 1.8V */ -#define VADC_MAX_ADC_CODE 0xa800 - -#define VADC_ABSOLUTE_RANGE_UV 625000 -#define VADC_RATIOMETRIC_RANGE_UV 1800000 - -#define VADC_DEF_PRESCALING 0 /* 1:1 */ -#define VADC_DEF_DECIMATION 0 /* 512 */ -#define VADC_DEF_HW_SETTLE_TIME 0 /* 0 us */ -#define VADC_DEF_AVG_SAMPLES 0 /* 1 sample */ -#define VADC_DEF_CALIB_TYPE VADC_CALIB_ABSOLUTE - -#define VADC_DECIMATION_MIN 512 -#define VADC_DECIMATION_MAX 4096 - -#define VADC_HW_SETTLE_DELAY_MAX 10000 -#define VADC_AVG_SAMPLES_MAX 512 - -#define KELVINMIL_CELSIUSMIL 273150 - #define VADC_CHAN_MIN VADC_USBIN #define VADC_CHAN_MAX VADC_LR_MUX3_BUF_PU1_PU2_XO_THERM -/* - * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels. - * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for - * calibration. - */ -enum vadc_calibration { - VADC_CALIB_ABSOLUTE = 0, - VADC_CALIB_RATIOMETRIC -}; - -/** - * struct vadc_linear_graph - Represent ADC characteristics. - * @dy: numerator slope to calculate the gain. - * @dx: denominator slope to calculate the gain. - * @gnd: A/D word of the ground reference used for the channel. - * - * Each ADC device has different offset and gain parameters which are - * computed to calibrate the device. - */ -struct vadc_linear_graph { - s32 dy; - s32 dx; - s32 gnd; -}; - -/** - * struct vadc_prescale_ratio - Represent scaling ratio for ADC input. - * @num: the inverse numerator of the gain applied to the input channel. - * @den: the inverse denominator of the gain applied to the input channel. - */ -struct vadc_prescale_ratio { - u32 num; - u32 den; -}; - /** * struct vadc_channel_prop - VADC channel property. * @channel: channel number, refer to the channel list. @@ -471,33 +413,10 @@ static int vadc_measure_ref_points(struct vadc_priv *vadc) static s32 vadc_calibrate(struct vadc_priv *vadc, const struct vadc_channel_prop *prop, u16 adc_code) { - const struct vadc_prescale_ratio *prescale; - s64 voltage; - - voltage = adc_code - vadc->graph[prop->calibration].gnd; - voltage *= vadc->graph[prop->calibration].dx; - voltage = div64_s64(voltage, vadc->graph[prop->calibration].dy); - - if (prop->calibration == VADC_CALIB_ABSOLUTE) - voltage += vadc->graph[prop->calibration].dx; - - if (voltage < 0) - voltage = 0; - - prescale = &vadc_prescale_ratios[prop->prescale]; - - voltage = voltage * prescale->den; - - return div64_s64(voltage, prescale->num); -} - -static int vadc_decimation_from_dt(u32 value) -{ - if (!is_power_of_2(value) || value < VADC_DECIMATION_MIN || - value > VADC_DECIMATION_MAX) - return -EINVAL; - - return __ffs64(value / VADC_DECIMATION_MIN); + return qcom_vadc_calibrate(&vadc_prescale_ratios[prop->prescale], + &vadc->graph[prop->calibration], + (prop->calibration == VADC_CALIB_ABSOLUTE), + adc_code); } static int vadc_prescaling_from_dt(u32 num, u32 den) @@ -752,7 +671,7 @@ static int vadc_get_dt_channel_data(struct device *dev, ret = of_property_read_u32(node, "qcom,decimation", &value); if (!ret) { - ret = vadc_decimation_from_dt(value); + ret = qcom_vadc_decimation_from_dt(value); if (ret < 0) { dev_err(dev, "%02x invalid decimation %d\n", chan, value); diff --git a/drivers/iio/adc/qcom-vadc-common.c b/drivers/iio/adc/qcom-vadc-common.c new file mode 100644 index 000000000000..f67fc5e2a702 --- /dev/null +++ b/drivers/iio/adc/qcom-vadc-common.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include + +#include "qcom-vadc-common.h" + +s32 qcom_vadc_calibrate(const struct vadc_prescale_ratio *prescale, + const struct vadc_linear_graph *graph, + bool absolute, + u16 adc_code) +{ + s64 voltage; + + voltage = adc_code - graph->gnd; + voltage *= graph->dx; + voltage = div64_s64(voltage, graph->dy); + + if (absolute) + voltage += graph->dx; + + if (voltage < 0) + voltage = 0; + + voltage = voltage * prescale->den; + + return div64_s64(voltage, prescale->num); +} + +int qcom_vadc_decimation_from_dt(u32 value) +{ + if (!is_power_of_2(value) || value < VADC_DECIMATION_MIN || + value > VADC_DECIMATION_MAX) + return -EINVAL; + + return __ffs64(value / VADC_DECIMATION_MIN); +} diff --git a/drivers/iio/adc/qcom-vadc-common.h b/drivers/iio/adc/qcom-vadc-common.h new file mode 100644 index 000000000000..b41cb501eef8 --- /dev/null +++ b/drivers/iio/adc/qcom-vadc-common.h @@ -0,0 +1,69 @@ +/* + * Code shared between the different Qualcomm PMIC voltage ADCs + */ + +#define VADC_CONV_TIME_MIN_US 2000 +#define VADC_CONV_TIME_MAX_US 2100 + +/* Min ADC code represents 0V */ +#define VADC_MIN_ADC_CODE 0x6000 +/* Max ADC code represents full-scale range of 1.8V */ +#define VADC_MAX_ADC_CODE 0xa800 + +#define VADC_ABSOLUTE_RANGE_UV 625000 +#define VADC_RATIOMETRIC_RANGE_UV 1800000 + +#define VADC_DEF_PRESCALING 0 /* 1:1 */ +#define VADC_DEF_DECIMATION 0 /* 512 */ +#define VADC_DEF_HW_SETTLE_TIME 0 /* 0 us */ +#define VADC_DEF_AVG_SAMPLES 0 /* 1 sample */ +#define VADC_DEF_CALIB_TYPE VADC_CALIB_ABSOLUTE + +#define VADC_DECIMATION_MIN 512 +#define VADC_DECIMATION_MAX 4096 + +#define VADC_HW_SETTLE_DELAY_MAX 10000 +#define VADC_AVG_SAMPLES_MAX 512 + +#define KELVINMIL_CELSIUSMIL 273150 + +/* + * VADC_CALIB_ABSOLUTE: uses the 625mV and 1.25V as reference channels. + * VADC_CALIB_RATIOMETRIC: uses the reference voltage (1.8V) and GND for + * calibration. + */ +enum vadc_calibration { + VADC_CALIB_ABSOLUTE = 0, + VADC_CALIB_RATIOMETRIC +}; + +/** + * struct vadc_linear_graph - Represent ADC characteristics. + * @dy: numerator slope to calculate the gain. + * @dx: denominator slope to calculate the gain. + * @gnd: A/D word of the ground reference used for the channel. + * + * Each ADC device has different offset and gain parameters which are + * computed to calibrate the device. + */ +struct vadc_linear_graph { + s32 dy; + s32 dx; + s32 gnd; +}; + +/** + * struct vadc_prescale_ratio - Represent scaling ratio for ADC input. + * @num: the inverse numerator of the gain applied to the input channel. + * @den: the inverse denominator of the gain applied to the input channel. + */ +struct vadc_prescale_ratio { + u32 num; + u32 den; +}; + +s32 qcom_vadc_calibrate(const struct vadc_prescale_ratio *prescale, + const struct vadc_linear_graph *graph, + bool absolute, + u16 adc_code); +int qcom_vadc_decimation_from_dt(u32 value);