From patchwork Thu Mar 3 16:09:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ludovic Desroches X-Patchwork-Id: 8493701 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B516EC0553 for ; Thu, 3 Mar 2016 16:11:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A9C1F200ED for ; Thu, 3 Mar 2016 16:11:50 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9AB23200DB for ; Thu, 3 Mar 2016 16:11:49 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1abVpW-0001RZ-Eb; Thu, 03 Mar 2016 16:10:22 +0000 Received: from eusmtp01.atmel.com ([212.144.249.243]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1abVpS-0008Tq-V0 for linux-arm-kernel@lists.infradead.org; Thu, 03 Mar 2016 16:10:20 +0000 Received: from ibiza.corp.atmel.com (10.161.101.13) by eusmtp01.atmel.com (10.161.101.31) with Microsoft SMTP Server id 14.3.235.1; Thu, 3 Mar 2016 17:09:51 +0100 From: Ludovic Desroches To: Subject: [PATCH 3/3] iio:adc:at91-sama5d2: add support for signed conversion Date: Thu, 3 Mar 2016 17:09:15 +0100 Message-ID: <1457021355-23898-4-git-send-email-ludovic.desroches@atmel.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1457021355-23898-1-git-send-email-ludovic.desroches@atmel.com> References: <1457021355-23898-1-git-send-email-ludovic.desroches@atmel.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160303_081019_379624_96209D66 X-CRM114-Status: GOOD ( 13.15 ) X-Spam-Score: -4.2 (----) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-iio@vger.kernel.org, Ludovic Desroches , nicolas.ferre@atmel.com, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The at91-sama5d2 ADC controller can achieve unsigned and signed conversion. For each channel, a signed and an unsigned variant are created. We can't set the sign mode for each channel. For that reason, the controller has to be configured upon conversion requests. Signed-off-by: Ludovic Desroches --- drivers/iio/adc/at91-sama5d2_adc.c | 81 +++++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 14 deletions(-) diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c index 5bc038f..d4bf73f 100644 --- a/drivers/iio/adc/at91-sama5d2_adc.c +++ b/drivers/iio/adc/at91-sama5d2_adc.c @@ -105,8 +105,26 @@ #define AT91_SAMA5D2_LCCWR 0x38 /* Overrun Status Register */ #define AT91_SAMA5D2_OVER 0x3c + /* Extended Mode Register */ #define AT91_SAMA5D2_EMR 0x40 +/* Sign Mode */ +#define AT91_SAMA5D2_EMR_SIGNMODE(v) ((v) << 25) +/* + * Single-Ended channels: Unsigned conversions. + * Differential channels: Signed conversions. + */ +#define AT91_SAMA5D2_EMR_SE_UNSG_DF_SIGN 0 +/* + * Single-Ended channels: Signed conversions. + * Differential channels: Unsigned conversions. + */ +#define AT91_SAMA5D2_EMR_SE_SIGN_DF_UNSG 1 +/* All channels: Unsigned conversions */ +#define AT91_SAMA5D2_EMR_ALL_UNSIGNED 2 +/* All channels: Signed conversions */ +#define AT91_SAMA5D2_EMR_ALL_SIGNED 3 + /* Compare Window Register */ #define AT91_SAMA5D2_CWR 0x44 /* Channel Gain Register */ @@ -140,13 +158,14 @@ /* Version Register */ #define AT91_SAMA5D2_VERSION 0xfc -#define AT91_SAMA5D2_CHAN(num, addr) \ +#define AT91_SAMA5D2_VOLTAGE_CHANNEL(num, addr, sign_mode) \ { \ .type = IIO_VOLTAGE, \ .channel = num, \ .address = addr, \ + .extend_name = (sign_mode == 's') ? "signed" : "unsigned",\ .scan_type = { \ - .sign = 'u', \ + .sign = sign_mode, \ .realbits = 12, \ }, \ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ @@ -156,6 +175,12 @@ .indexed = 1, \ } +#define AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(num, addr) \ + AT91_SAMA5D2_VOLTAGE_CHANNEL(num, addr, 's') + +#define AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(num, addr) \ + AT91_SAMA5D2_VOLTAGE_CHANNEL(num, addr, 'u') + #define at91_adc_readl(st, reg) readl_relaxed(st->base + reg) #define at91_adc_writel(st, reg, val) writel_relaxed(val, st->base + reg) @@ -185,18 +210,30 @@ struct at91_adc_state { }; static const struct iio_chan_spec at91_adc_channels[] = { - AT91_SAMA5D2_CHAN(0, 0x50), - AT91_SAMA5D2_CHAN(1, 0x54), - AT91_SAMA5D2_CHAN(2, 0x58), - AT91_SAMA5D2_CHAN(3, 0x5c), - AT91_SAMA5D2_CHAN(4, 0x60), - AT91_SAMA5D2_CHAN(5, 0x64), - AT91_SAMA5D2_CHAN(6, 0x68), - AT91_SAMA5D2_CHAN(7, 0x6c), - AT91_SAMA5D2_CHAN(8, 0x70), - AT91_SAMA5D2_CHAN(9, 0x74), - AT91_SAMA5D2_CHAN(10, 0x78), - AT91_SAMA5D2_CHAN(11, 0x7c), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(0, 0x50), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(1, 0x54), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(2, 0x58), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(3, 0x5c), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(4, 0x60), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(5, 0x64), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(6, 0x68), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(7, 0x6c), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(8, 0x70), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(9, 0x74), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(10, 0x78), + AT91_SAMA5D2_UNSIGNED_VOLTAGE_CHANNEL(11, 0x7c), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(0, 0x50), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(1, 0x54), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(2, 0x58), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(3, 0x5c), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(4, 0x60), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(5, 0x64), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(6, 0x68), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(7, 0x6c), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(8, 0x70), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(9, 0x74), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(10, 0x78), + AT91_SAMA5D2_SIGNED_VOLTAGE_CHANNEL(11, 0x7c), }; static unsigned at91_adc_startup_time(unsigned startup_time_min, @@ -278,6 +315,7 @@ static int at91_adc_read_raw(struct iio_dev *indio_dev, int *val, int *val2, long mask) { struct at91_adc_state *st = iio_priv(indio_dev); + u32 emr; int ret; switch (mask) { @@ -286,6 +324,19 @@ static int at91_adc_read_raw(struct iio_dev *indio_dev, st->chan = chan; + /* Read EMR register and clear 'sign mode' field */ + emr = at91_adc_readl(st, AT91_SAMA5D2_EMR) + & AT91_SAMA5D2_EMR_SIGNMODE(0); + /* + * Check if the user requested a conversion on a signed or + * unsigned channel. + */ + if (chan->scan_type.sign == 's') + emr |= AT91_SAMA5D2_EMR_SIGNMODE(AT91_SAMA5D2_EMR_ALL_SIGNED); + else + emr |= AT91_SAMA5D2_EMR_SIGNMODE(AT91_SAMA5D2_EMR_ALL_UNSIGNED); + + at91_adc_writel(st, AT91_SAMA5D2_EMR, emr); at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel)); at91_adc_writel(st, AT91_SAMA5D2_IER, BIT(chan->channel)); at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START); @@ -298,6 +349,8 @@ static int at91_adc_read_raw(struct iio_dev *indio_dev, if (ret > 0) { *val = st->conversion_value; + if (chan->scan_type.sign == 's') + *val = sign_extend32(*val, 11); ret = IIO_VAL_INT; st->conversion_done = false; }