From patchwork Fri Jan 26 15:19:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Rossak X-Patchwork-Id: 10186319 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DE24E60383 for ; Fri, 26 Jan 2018 15:21:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CF89E29C19 for ; Fri, 26 Jan 2018 15:21:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C3C7A29C26; Fri, 26 Jan 2018 15:21:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1917C29C19 for ; Fri, 26 Jan 2018 15:21:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=cUAR3YqcsNtJ4Fq+O2KWcMeshdXOeflgeFdDN3TzBFs=; b=LoygwQB6ASaXlcO95hIsEa+nE3 iFojgbduvgJ0e3BhL2u0sxgRuTTP3qJ3QIKdn2hr1sBnCiuJz7eSpB8LgprJHezI0NvZTDCwhToJM EvvGpSxKRr9kmYL+JmMApyeppPKgTT94AoGFd361zf1MduWeTZI9rafd1tFtGzTaQov7VaoHOBUpD SAJxWjUeueGboyhc/IbXBK1dvG6n30bsx2w+xpsxloR3bh9Z/SW5cZkJJ3hmNY5wWyGeGFYyXaBZA tUV/Nnvjji8exPilRBgFQl5aX5v/mNjSfbZxqtfV0FBpqXzHWn39TymV+mkW4X3VOY71pxD7popTF 7KoH8FPg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1ef5p9-0003X0-Ch; Fri, 26 Jan 2018 15:21:51 +0000 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1ef5nV-0000Y0-AM for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2018 15:20:13 +0000 Received: by mail-wr0-x242.google.com with SMTP id s5so849131wra.0 for ; Fri, 26 Jan 2018 07:19:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=eB66xYg9QIvt7PRjfIosl8LiI4+nxxjOxGtn8BlsUr8=; b=MCvetZTYCpl5QRePal45aqiP0PDpgt3eqwXQOrDcAdXC9ZX8QHszy+4SF8Se3yIyeO 4gYF5AlkWq6ZDP7EGa4qkvhzYVGw3IWaa83v8f/wG8CfDBxnjLGPNgcPrLYPhiNlKgvX zzdTVeargxUVc7B8b2IEQCXKii+1WlfOXo7Md3nU2n0SqZSf4Sw2Yy505oefdLiRLetk c4NR7QCtjaSGlyLsjnfCtXez6x5J6cP3yBYgU4G7zQrtuuBLXTAyEdCK2y0/A+0OkHJT Bc4rCqe2ja4fmb92JRO9OXWAPVDjL5m8N5JbZp3bSmSNmKBvid3zQAZgGNz9Htmvb40f gGJQ== 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:in-reply-to :references; bh=eB66xYg9QIvt7PRjfIosl8LiI4+nxxjOxGtn8BlsUr8=; b=nrssqsFOquV5KJPs6+cpPtr9tpxH4Yu2PFjo0GXpsKXv3lt7wQ3wxHH2bucNzUU3vU BTe3FA2W9YEnE6Zq7VhiWXkCI19yo8Mp6rxb9FuU2CcCp1bm06Xc773eXggSzuZS0xSl 4yI5CNdVXr6JCkBf8XIHsZtQgEpgFUA8VdVUvnT0394Ozscu+SghJ+P2XjJfAqIe5dIB WF2WbvgM8TJ8aQ4Kv8sEpCK714pEHANl1+Qy4FJU6npBACNv2uCM9US7y8RKOmVmzE8w YqWk9zghL3kg3D5p5quB3d2m2CRaBOxqolY/8G9RMrSl/IKabVm0JnMOeJbywCoXiXYE tD0g== X-Gm-Message-State: AKwxytf8ccwuxmoArqywnn+86SGKksZ/3qseQsH8p8ohis3xkKLKz8iU woTOJbsoCD31oBukngA3vnQ= X-Google-Smtp-Source: AH8x225o4FX8z8vPuv4wr/hj2SyLx8YAP/KJVLsDN/RDSxY7NUGVbTrRzq/jN13wx1Q7y0vIrwJWnA== X-Received: by 10.223.152.109 with SMTP id v100mr11611872wrb.222.1516979997988; Fri, 26 Jan 2018 07:19:57 -0800 (PST) Received: from debian-laptop.fritz.box (p578F04D2.dip0.t-ipconnect.de. [87.143.4.210]) by smtp.gmail.com with ESMTPSA id e15sm4586130wmd.26.2018.01.26.07.19.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Jan 2018 07:19:57 -0800 (PST) From: Philipp Rossak To: lee.jones@linaro.org, robh+dt@kernel.org, mark.rutland@arm.com, maxime.ripard@free-electrons.com, wens@csie.org, linux@armlinux.org.uk, jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, davem@davemloft.net, hans.verkuil@cisco.com, mchehab@kernel.org, rask@formelder.dk, clabbe.montjoie@gmail.com, sean@mess.org, krzk@kernel.org, quentin.schulz@free-electrons.com, icenowy@aosc.io, edu.molinas@gmail.com, singhalsimran0@gmail.com Subject: [PATCH 07/16] iio: adc: sun4i-gpadc-iio: rework: support nvmem calibration data Date: Fri, 26 Jan 2018 16:19:32 +0100 Message-Id: <20180126151941.12183-8-embed3d@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180126151941.12183-1-embed3d@gmail.com> References: <20180126151941.12183-1-embed3d@gmail.com> X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-sunxi@googlegroups.com, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP This patch reworks the driver to support nvmem calibration cells. The driver checks if the nvmem calibration is supported and reads out the nvmem. At the beginning of the startup process the calibration data is written to the related registers. Signed-off-by: Philipp Rossak --- drivers/iio/adc/sun4i-gpadc-iio.c | 52 +++++++++++++++++++++++++++++++++++++++ include/linux/mfd/sun4i-gpadc.h | 2 ++ 2 files changed, 54 insertions(+) diff --git a/drivers/iio/adc/sun4i-gpadc-iio.c b/drivers/iio/adc/sun4i-gpadc-iio.c index bff06f2798e8..7b12666cdd9e 100644 --- a/drivers/iio/adc/sun4i-gpadc-iio.c +++ b/drivers/iio/adc/sun4i-gpadc-iio.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -81,6 +82,7 @@ struct gpadc_data { bool has_bus_rst; bool has_mod_clk; int sensor_count; + bool supports_nvmem; }; static const struct gpadc_data sun4i_gpadc_data = { @@ -94,6 +96,7 @@ static const struct gpadc_data sun4i_gpadc_data = { .sample_start = sun4i_gpadc_sample_start, .sample_end = sun4i_gpadc_sample_end, .sensor_count = 1, + .supports_nvmem = false, }; static const struct gpadc_data sun5i_gpadc_data = { @@ -107,6 +110,7 @@ static const struct gpadc_data sun5i_gpadc_data = { .sample_start = sun4i_gpadc_sample_start, .sample_end = sun4i_gpadc_sample_end, .sensor_count = 1, + .supports_nvmem = false, }; static const struct gpadc_data sun6i_gpadc_data = { @@ -120,6 +124,7 @@ static const struct gpadc_data sun6i_gpadc_data = { .sample_start = sun4i_gpadc_sample_start, .sample_end = sun4i_gpadc_sample_end, .sensor_count = 1, + .supports_nvmem = false, }; static const struct gpadc_data sun8i_a33_gpadc_data = { @@ -130,6 +135,7 @@ static const struct gpadc_data sun8i_a33_gpadc_data = { .sample_start = sun4i_gpadc_sample_start, .sample_end = sun4i_gpadc_sample_end, .sensor_count = 1, + .supports_nvmem = false, }; struct sun4i_gpadc_iio { @@ -148,6 +154,8 @@ struct sun4i_gpadc_iio { struct clk *mod_clk; struct reset_control *reset; int sensor_id; + u32 calibration_data[2]; + bool has_calibration_data[2]; /* prevents concurrent reads of temperature and ADC */ struct mutex mutex; struct thermal_zone_device *tzd; @@ -459,6 +467,17 @@ static int sun4i_gpadc_runtime_suspend(struct device *dev) return info->data->sample_end(info); } +static void sunxi_calibrate(struct sun4i_gpadc_iio *info) +{ + if (info->has_calibration_data[0]) + regmap_write(info->regmap, SUNXI_THS_CDATA_0_1, + info->calibration_data[0]); + + if (info->has_calibration_data[1]) + regmap_write(info->regmap, SUNXI_THS_CDATA_2_3, + info->calibration_data[1]); +} + static int sun4i_gpadc_sample_start(struct sun4i_gpadc_iio *info) { /* clkin = 6MHz */ @@ -481,6 +500,7 @@ static int sun4i_gpadc_sample_start(struct sun4i_gpadc_iio *info) static int sunxi_ths_sample_start(struct sun4i_gpadc_iio *info) { u32 value; + sunxi_calibrate(info); if (info->data->ctrl0_map) regmap_write(info->regmap, SUNXI_THS_CTRL0, @@ -602,6 +622,9 @@ static int sun4i_gpadc_probe_dt(struct platform_device *pdev, struct resource *mem; void __iomem *base; int ret; + struct nvmem_cell *cell; + ssize_t cell_size; + u64 *cell_data; info->data = of_device_get_match_data(&pdev->dev); if (!info->data) @@ -616,6 +639,35 @@ static int sun4i_gpadc_probe_dt(struct platform_device *pdev, if (IS_ERR(base)) return PTR_ERR(base); + info->has_calibration_data[0] = false; + info->has_calibration_data[1] = false; + + if (!info->data->supports_nvmem) + goto no_nvmem; + + cell = devm_nvmem_cell_get(&pdev->dev, "calibration"); + if (IS_ERR(cell)) { + if (PTR_ERR(cell) == -EPROBE_DEFER) + return PTR_ERR(cell); + } else { + cell_data = (u64 *)nvmem_cell_read(cell, &cell_size); + devm_nvmem_cell_put(&pdev->dev, cell); + if (cell_size <= 4) { + info->has_calibration_data[0] = true; + info->calibration_data[0] = be32_to_cpu(cell_data[0] & + GENMASK(31, 0)); + } else if (cell_size <= 8) { + info->has_calibration_data[0] = true; + info->calibration_data[0] = be32_to_cpu(cell_data[0] & + GENMASK(31, 0)); + info->has_calibration_data[1] = true; + info->calibration_data[1] = be32_to_cpu( + (cell_data[0] >> 32) & GENMASK(31, 0)); + } + } + +no_nvmem: + info->regmap = devm_regmap_init_mmio(&pdev->dev, base, &sun4i_gpadc_regmap_config); if (IS_ERR(info->regmap)) { diff --git a/include/linux/mfd/sun4i-gpadc.h b/include/linux/mfd/sun4i-gpadc.h index 40b4dd9d2405..c251002431bd 100644 --- a/include/linux/mfd/sun4i-gpadc.h +++ b/include/linux/mfd/sun4i-gpadc.h @@ -90,6 +90,8 @@ #define SUNXI_THS_CTRL0 0x00 #define SUNXI_THS_CTRL2 0x40 #define SUNXI_THS_FILTER 0x70 +#define SUNXI_THS_CDATA_0_1 0x74 +#define SUNXI_THS_CDATA_2_3 0x78 #define SUNXI_THS_TDATA0 0x80 #define SUNXI_THS_TDATA1 0x84 #define SUNXI_THS_TDATA2 0x88