Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp697359yba; Thu, 16 May 2019 07:33:57 -0700 (PDT) X-Google-Smtp-Source: APXvYqx2QFbL+z3a92tZVYJwHZfj7ygZZIZHeJkymM9xgsLZpKhM4K9sW4SkXIlKuDGf0D9QEZjA X-Received: by 2002:a17:902:521:: with SMTP id 30mr11730967plf.62.1558017237093; Thu, 16 May 2019 07:33:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558017237; cv=none; d=google.com; s=arc-20160816; b=RIp9TM4VFHd8190BnivTIy/YNbkpIDHfLwJZ/6fm1wqSaA9L/0+syXZEsD6Y0O7FsP L3jHbRebq4Oo2HQnyR1ngKDhdw+3SLAP5kSTXX3Knk+WP/X3tSolV5xVZqVmkOPW1gu8 2SzbWpQQ5zYrwHAMOrN97NRdL+92I7ORwYGiCtYEopQKs/G+jZ8BeqOQrRL3tPNmCz7l mDbJXegGRv6SgcDVoWidcHNXkzP6j6nRC0hJRdIAoc0ufjNeE4dEa5LhTFKYYSfgNKcu Vyx1s0LzkXQJHgZVIX0V7EgT/t0vh42jR3DQvp7/Dvp4dEThcb6Fp2Uy3aWBmEq99HOQ ZPQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=lRnjRH5C6G1TZttsN0EG0HqtgPjxumxx5mgT8eucyTE=; b=ZtEBUj0qIWBOogXxwTFTjDX/CuUgLB134JDDCwbHizE4EmrI/Wrv1Oti2EFITffXGr 1AjWKUyADZ5oX+nzJDBEi6/f8RQ0WxSoXbyEZcK65Xb8g4aR0ngdMnxGvAV4/jYZq6iN xtGtYW6DGYDY2KMvfhRLS/Fci+GUC/VCDRWaWFusNQkUY0y9DZCwi/yVLMgZqWOqKmkU YnWatM+lCsNfExvF07dV8IMoB2SfB+I6+aPvQV1xYVQg2hvfmrgm4f/Kq8GnoT6daX8G 5PFR7PirlCsbM0po7y20za5dKCn8qV9eWTK+9c6Gu1EnzrIrn0dfCLjLWqeebANQynLe 6w5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@analog.onmicrosoft.com header.s=selector1-analog-com header.b=rmOzmF+p; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s205si4895634pgs.467.2019.05.16.07.33.39; Thu, 16 May 2019 07:33:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@analog.onmicrosoft.com header.s=selector1-analog-com header.b=rmOzmF+p; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727358AbfEPOb7 (ORCPT + 99 others); Thu, 16 May 2019 10:31:59 -0400 Received: from mail-eopbgr810070.outbound.protection.outlook.com ([40.107.81.70]:11607 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727302AbfEPOb4 (ORCPT ); Thu, 16 May 2019 10:31:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=analog.onmicrosoft.com; s=selector1-analog-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lRnjRH5C6G1TZttsN0EG0HqtgPjxumxx5mgT8eucyTE=; b=rmOzmF+p9Py065wALvVPJ+skuhLqWFZ1dM9kegQ+a/3p/7NQ5LJcDHqHwz0Fu5pHN5CmMhkrmFZcTa+A0iKC7O5W/NNqhnENDjK+b0KMcjemerpkM5HCsOhWywErbgFZ6Y86sU0RLTlLim4hjtA5nVTnbhhGM2R8U/Na4KuNt2k= Received: from DM6PR03CA0005.namprd03.prod.outlook.com (2603:10b6:5:40::18) by BY2PR03MB554.namprd03.prod.outlook.com (2a01:111:e400:2c38::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1878.22; Thu, 16 May 2019 14:31:52 +0000 Received: from SN1NAM02FT022.eop-nam02.prod.protection.outlook.com (2a01:111:f400:7e44::200) by DM6PR03CA0005.outlook.office365.com (2603:10b6:5:40::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1900.16 via Frontend Transport; Thu, 16 May 2019 14:31:51 +0000 Authentication-Results: spf=pass (sender IP is 137.71.25.55) smtp.mailfrom=analog.com; gmx.de; dkim=none (message not signed) header.d=none;gmx.de; dmarc=bestguesspass action=none header.from=analog.com; Received-SPF: Pass (protection.outlook.com: domain of analog.com designates 137.71.25.55 as permitted sender) receiver=protection.outlook.com; client-ip=137.71.25.55; helo=nwd2mta1.analog.com; Received: from nwd2mta1.analog.com (137.71.25.55) by SN1NAM02FT022.mail.protection.outlook.com (10.152.72.148) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.1900.16 via Frontend Transport; Thu, 16 May 2019 14:31:50 +0000 Received: from NWD2HUBCAS7.ad.analog.com (nwd2hubcas7.ad.analog.com [10.64.69.107]) by nwd2mta1.analog.com (8.13.8/8.13.8) with ESMTP id x4GEVo5q027602 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=OK); Thu, 16 May 2019 07:31:50 -0700 Received: from ben-Latitude-E6540.analog.com (10.50.1.133) by NWD2HUBCAS7.ad.analog.com (10.64.69.107) with Microsoft SMTP Server id 14.3.408.0; Thu, 16 May 2019 10:31:49 -0400 From: Beniamin Bia To: CC: , , , , , , , , , , , , Beniamin Bia Subject: [PATCH 3/5] iio: adc: ad7606: Make SPI register calculation generic and add spi support Date: Thu, 16 May 2019 17:32:06 +0300 Message-ID: <20190516143208.19294-3-beniamin.bia@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190516143208.19294-1-beniamin.bia@analog.com> References: <20190516143208.19294-1-beniamin.bia@analog.com> MIME-Version: 1.0 Content-Type: text/plain X-ADIRoutedOnPrem: True X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:137.71.25.55;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(376002)(396003)(39860400002)(136003)(346002)(2980300002)(189003)(199004)(4326008)(356004)(36756003)(54906003)(6666004)(8936002)(126002)(51416003)(44832011)(106002)(246002)(446003)(7696005)(2616005)(486006)(5660300002)(11346002)(16586007)(77096007)(70586007)(186003)(76176011)(8676002)(476003)(26005)(70206006)(7416002)(2906002)(50226002)(336012)(426003)(7636002)(305945005)(6916009)(316002)(1076003)(48376002)(47776003)(53416004)(50466002)(478600001)(86362001)(2351001)(107886003)(14444005)(72206003)(41533002);DIR:OUT;SFP:1101;SCL:1;SRVR:BY2PR03MB554;H:nwd2mta1.analog.com;FPR:;SPF:Pass;LANG:en;PTR:nwd2mail10.analog.com;MX:1;A:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6eda2412-6e24-4e1a-55df-08d6da0b3c76 X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600141)(711020)(4605104)(4709054)(2017052603328);SRVR:BY2PR03MB554; X-MS-TrafficTypeDiagnostic: BY2PR03MB554: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-Forefront-PRVS: 0039C6E5C5 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: jKqzlErXezhdWF/e/B+BG+u8ays2mlrFqba4BxuVFLnOg4FltHPwUVRzncr/HInC6OLcgp+TlMKMRK+ID22geY4kuWoo9jK6HMlosQZ7ycU4Uk/ld8y+QjPZ9C2DXWGWN5RGXkQncC8e80S3UFfNtXsuL2Wg2mjlcwyAmCn6EfGjpQy4FEMNSzIT52HLvuy538+XyN3x9xoCBRdGAUgWpfZvCeHzdfB/Je03jnbfCcG4dhPCyvaoPZSHPMFDP/eitMqQpyQlPb1xX53f6ZAKg8Ap9jSZ2z1ekEXgo8Ac/97KTw9M8bAb+eg0h0eFG+b8dOIVJm78HZnsA9xe78B7hFnogWI0yqcFYpvGlZp1QOeSCx5WqvA29i9zaYDzQj6cm4j91T5/hiDjrCl4umO6/pJv7jDK7fRtMB/7Q4yHMtA= X-OriginatorOrg: analog.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2019 14:31:50.8333 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6eda2412-6e24-4e1a-55df-08d6da0b3c76 X-MS-Exchange-CrossTenant-Id: eaa689b4-8f87-40e0-9c6f-7228de4d754a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=eaa689b4-8f87-40e0-9c6f-7228de4d754a;Ip=[137.71.25.55];Helo=[nwd2mta1.analog.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR03MB554 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In order to support AD7616 software mode, the spi register access must be added and the calculation of registers address must be generic. The length of address and bit which specifies the read/write operation is different for every device, that is why it was made generic. Signed-off-by: Beniamin Bia --- drivers/iio/adc/ad7606.c | 60 ++++++++++++++++++++++++++++++++++++++++ drivers/iio/adc/ad7606.h | 2 ++ 2 files changed, 62 insertions(+) diff --git a/drivers/iio/adc/ad7606.c b/drivers/iio/adc/ad7606.c index aba0fd123a51..6df81117cacc 100644 --- a/drivers/iio/adc/ad7606.c +++ b/drivers/iio/adc/ad7606.c @@ -25,6 +25,8 @@ #include #include +#include + #include "ad7606.h" /* @@ -43,6 +45,11 @@ static const unsigned int ad7616_oversampling_avail[8] = { 1, 2, 4, 8, 16, 32, 64, 128, }; +static int ad7616_spi_rd_wr_cmd(int addr, char isWriteOp) +{ + return ((addr & 0x7F) << 1) | ((isWriteOp & 0x1) << 7); +} + static int ad7606_reset(struct ad7606_state *st) { if (st->gpio_reset) { @@ -55,6 +62,59 @@ static int ad7606_reset(struct ad7606_state *st) return -ENODEV; } +static int ad7606_spi_reg_read(struct ad7606_state *st, unsigned int addr) +{ + struct spi_device *spi = to_spi_device(st->dev); + struct spi_transfer t[] = { + { + .tx_buf = &st->data[0], + .len = 2, + .cs_change = 0, + }, { + .rx_buf = &st->data[1], + .len = 2, + }, + }; + int ret; + + st->data[0] = cpu_to_be16(st->chip_info->spi_rd_wr_cmd(addr, 0) << 8); + + ret = spi_sync_transfer(spi, t, ARRAY_SIZE(t)); + if (ret < 0) + return ret; + + return be16_to_cpu(st->data[1]); +} + +static int ad7606_spi_reg_write(struct ad7606_state *st, + unsigned int addr, + unsigned int val) +{ + struct spi_device *spi = to_spi_device(st->dev); + + st->data[0] = cpu_to_be16((st->chip_info->spi_rd_wr_cmd(addr, 1) << 8) | + (val & 0x1FF)); + + return spi_write(spi, &st->data[0], sizeof(st->data[0])); +} + +static int ad7606_spi_write_mask(struct ad7606_state *st, + unsigned int addr, + unsigned long mask, + unsigned int val) +{ + int readval; + + readval = ad7606_spi_reg_read(st, addr); + if (readval < 0) + return readval; + + readval &= ~mask; + readval |= val; + + return ad7606_spi_reg_write(st, addr, readval); +} + static int ad7606_read_samples(struct ad7606_state *st) { unsigned int num = st->chip_info->num_channels; diff --git a/drivers/iio/adc/ad7606.h b/drivers/iio/adc/ad7606.h index d8a509c2c428..dfc60af9b8ac 100644 --- a/drivers/iio/adc/ad7606.h +++ b/drivers/iio/adc/ad7606.h @@ -16,6 +16,7 @@ * oversampling ratios. * @oversampling_num number of elements stored in oversampling_avail array * @os_req_reset some devices require a reset to update oversampling + * @spi_rd_wr_cmd pointer to the function which calculates the spi address * @write_scale_sw pointer to the function which writes the scale via spi in software mode * @write_os_sw pointer to the function which writes the os via spi @@ -29,6 +30,7 @@ struct ad7606_chip_info { const unsigned int *oversampling_avail; unsigned int oversampling_num; bool os_req_reset; + int (*spi_rd_wr_cmd)(int addr, char isWriteOp); int (*write_scale_sw)(struct iio_dev *indio_dev, int ch, int val); int (*write_os_sw)(struct iio_dev *indio_dev, int val); int (*sw_mode_config)(struct iio_dev *indio_dev); -- 2.17.1