Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2918656imm; Tue, 4 Sep 2018 12:06:41 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYOQzJ4lhI56cABTTdTbQVnUvbCMl468VtWai4GrWx1mXjv2LYtMgrFVbTE5Q5Sunh0Vj6W X-Received: by 2002:a63:e841:: with SMTP id a1-v6mr17842707pgk.126.1536088001015; Tue, 04 Sep 2018 12:06:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1536088000; cv=pass; d=google.com; s=arc-20160816; b=dNOHPFicHG+lC5gt1lBNYY5kWA8z5NmEWTeVXlMbXMkZKSkkNaxnKg8ZGgaLM5TE7A Uv3H2vXS/cIDC9qRwNbptXTm0YCw5JTcDnCKfVV3DeY5f+pxyykO2g296DFqXYnloI6Z WMXlr3QmnU1ttYb2DYU/aj2UCTJpbBrO5SI37jOMJMbwdfNTevj98KNoxqK+Rwe5ZTSE jHrYYSxYQo1Ts/K/RlxLPRgLJ5Ou688CUcbIK36GTdUNQp2GkCompttS1kQq2tsoFV+0 WTciWm54/oSSzIp25wESuryzEiIxhBKaqh1xM4Fc9XkfvMSEPBeqeHH4yD0mb3vu6ZYr wdJA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results :arc-message-signature:arc-authentication-results; bh=GITcJ6S9bZMcJRvgf15Mt/ubxrNrL/QwsCliVzZpygk=; b=B8ClXUDcmrVpP+LEM924mwqh0kp6nD69Ncy9E2xBsF09qu1j1FDzYv1YbenjL0b/WF gcADy5VlE34mfMbXqJYAqPd/72YCdxqVTAcjIAPkh27YGdmzf1Gt/9gjh7kx8wbQISBn Ox1QlaPeKhn62UTYHfJuB/s7BzBDU7lvUb/+9gdZAKHj4phJjfISHyDLLvxdpv4UIu6P 5AOAiS0b8Q2fnOmhyLBbye6sSvC2E6C4jKTOUCj10ta4f9LGzG/oyuWWa8hWq9g3Cves lrycuqGsGMXribtbcMeAsmxgTsedDFOMPr4TL0RXawFZOGnqs198Bsl9x4z9y3var8KF mIHA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@az8.co header.s=dkimmail header.b=LKCBk9TW; arc=pass (i=1 spf=pass spfdomain=az8.co dkim=pass dkdomain=az8.co dmarc=pass fromdomain=az8.co>); 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=az8.co Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n19-v6si22325016pfi.360.2018.09.04.12.06.25; Tue, 04 Sep 2018 12:06:40 -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=@az8.co header.s=dkimmail header.b=LKCBk9TW; arc=pass (i=1 spf=pass spfdomain=az8.co dkim=pass dkdomain=az8.co dmarc=pass fromdomain=az8.co>); 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=az8.co Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728030AbeIDXbm (ORCPT + 99 others); Tue, 4 Sep 2018 19:31:42 -0400 Received: from sender-of-o52.zoho.com ([135.84.80.217]:21315 "EHLO sender-of-o52.zoho.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726507AbeIDXbl (ORCPT ); Tue, 4 Sep 2018 19:31:41 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1536087890; cv=none; d=zoho.com; s=zohoarc; b=bMLr2icKf0o46ZhT4LFc6R0cbn+l6VXPgZB+tHdggyt2rPmRJcpYT6lr1mESHa81KK+Ea9SbU3g7eu6Gu2tqTgAPXIem5WbIycsRGzIvaFbbNaJ7j26v50utz9uGhv6G3cCuRIziRIQnPBd4VMLfuiKkyeQEF1z2WRbs0WyHE7Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1536087890; h=Cc:Date:From:In-Reply-To:Message-ID:References:Subject:To:ARC-Authentication-Results; bh=GITcJ6S9bZMcJRvgf15Mt/ubxrNrL/QwsCliVzZpygk=; b=iDPdAMtCjWUsOLU28O9hZpKYAYzIXiwWCpiIktoKjBPFj4hbFtZRWacevw6q8G9EsDquuplZovGlmI0fjWabxScYyVUN2MyXRihR2sJUSG4Rx/c3pN0NZlVOLy1+S62JCVuu6o9sga4grc8F6cIDQsKRg2/ZC6PlSCarobrFSSc= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass header.i=az8.co; spf=pass smtp.mailfrom=afonsobordado@az8.co; dmarc=pass header.from= header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1536087889; s=dkimmail; d=az8.co; i=afonsobordado@az8.co; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; l=6540; bh=GITcJ6S9bZMcJRvgf15Mt/ubxrNrL/QwsCliVzZpygk=; b=LKCBk9TWH8VB0vXuyKcn0GKnFWU9XDbhoo4rVyEJQSqy0lgVBujhPlWPIEvD8epK lO82KiONbuioEiQSUd3/D24Bc+/hESgfDlCIvEdwqbGUKwfjE2tbq1MylljJ5/kIS4J sov8oYUBWq24vDQZGR+AK1ieiHUqRSYLM40OjQ8E= Received: from localhost (bl9-77-228.dsl.telepac.pt [85.242.77.228]) by mx.zohomail.com with SMTPS id 1536087889539281.276072519198; Tue, 4 Sep 2018 12:04:49 -0700 (PDT) From: Afonso Bordado To: jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Subject: [PATCH v3 3/4] iio: fxas21002c: add ODR/Scale support Date: Tue, 4 Sep 2018 21:04:33 +0100 Message-Id: <20180904200434.10442-3-afonsobordado@az8.co> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180904200434.10442-1-afonsobordado@az8.co> References: <20180904200434.10442-1-afonsobordado@az8.co> X-ZohoMailClient: External Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds support for reading/writing ODR/Scale We don't support the scale boost modes. Signed-off-by: Afonso Bordado --- drivers/iio/gyro/fxas21002c.c | 161 +++++++++++++++++++++++++++++++--- 1 file changed, 148 insertions(+), 13 deletions(-) diff --git a/drivers/iio/gyro/fxas21002c.c b/drivers/iio/gyro/fxas21002c.c index 18b1f500dd3e..7efef3156fa0 100644 --- a/drivers/iio/gyro/fxas21002c.c +++ b/drivers/iio/gyro/fxas21002c.c @@ -7,7 +7,7 @@ * IIO driver for FXAS21002C (7-bit I2C slave address 0x20 or 0x21). * Datasheet: https://www.nxp.com/docs/en/data-sheet/FXAS21002.pdf * TODO: - * ODR / Scale Support + * Scale boost mode * Power management * GPIO Reset * Power supplies @@ -44,7 +44,10 @@ #define FXAS21002C_REG_F_EVENT 0x0A #define FXAS21002C_REG_INT_SRC_FLAG 0x0B #define FXAS21002C_REG_WHO_AM_I 0x0C + #define FXAS21002C_REG_CTRL_REG0 0x0D +#define FXAS21002C_SCALE_MASK GENMASK(1, 0) + #define FXAS21002C_REG_RT_CFG 0x0E #define FXAS21002C_REG_RT_SRC 0x0F #define FXAS21002C_REG_RT_THS 0x10 @@ -56,14 +59,12 @@ #define FXAS21002C_ACTIVE_BIT BIT(1) #define FXAS21002C_READY_BIT BIT(0) +#define FXAS21002C_ODR_SHIFT 2 +#define FXAS21002C_ODR_MASK GENMASK(4, 2) + #define FXAS21002C_REG_CTRL_REG2 0x14 #define FXAS21002C_REG_CTRL_REG3 0x15 -#define FXAS21002C_DEFAULT_ODR_HZ 800 - -/* 0.0625 deg/s */ -#define FXAS21002C_DEFAULT_SENSITIVITY IIO_DEGREE_TO_RAD(62500) - #define FXAS21002C_TEMP_SCALE 1000 enum { @@ -82,6 +83,40 @@ struct fxas21002c_data { struct regmap *regmap; }; +enum fxas21002c_scale { + FXAS21002C_SCALE_62MDPS, + FXAS21002C_SCALE_31MDPS, + FXAS21002C_SCALE_15MDPS, + FXAS21002C_SCALE_7MDPS, +}; + +static const int fxas21002c_anglevel_scale_avail[4][2] = { + [FXAS21002C_SCALE_62MDPS] = { 0, IIO_DEGREE_TO_RAD(62500) }, + [FXAS21002C_SCALE_31MDPS] = { 0, IIO_DEGREE_TO_RAD(31250) }, + [FXAS21002C_SCALE_15MDPS] = { 0, IIO_DEGREE_TO_RAD(15625) }, + [FXAS21002C_SCALE_7MDPS] = { 0, IIO_DEGREE_TO_RAD(7812) }, +}; + +enum fxas21002c_odr { + FXAS21002C_ODR_800, + FXAS21002C_ODR_400, + FXAS21002C_ODR_200, + FXAS21002C_ODR_100, + FXAS21002C_ODR_50, + FXAS21002C_ODR_25, + FXAS21002C_ODR_12_5, +}; + +static const int fxas21002c_sample_freq_avail[7][2] = { + [FXAS21002C_ODR_800] = { 800, 0 }, + [FXAS21002C_ODR_400] = { 400, 0 }, + [FXAS21002C_ODR_200] = { 200, 0 }, + [FXAS21002C_ODR_100] = { 100, 0 }, + [FXAS21002C_ODR_50] = { 50, 0 }, + [FXAS21002C_ODR_25] = { 25, 0 }, + [FXAS21002C_ODR_12_5] = { 12, 500000 }, +}; + static const struct regmap_range fxas21002c_writable_ranges[] = { regmap_reg_range(FXAS21002C_REG_F_SETUP, FXAS21002C_REG_F_SETUP), regmap_reg_range(FXAS21002C_REG_CTRL_REG0, FXAS21002C_REG_RT_CFG), @@ -254,6 +289,49 @@ static int fxas21002c_read_oneshot(struct fxas21002c_data *data, } } +static int fxas21002c_scale_read(struct fxas21002c_data *data, int *val, + int *val2) +{ + int ret; + unsigned int raw; + + ret = regmap_read(data->regmap, FXAS21002C_REG_CTRL_REG0, &raw); + if (ret) + return ret; + + raw &= FXAS21002C_SCALE_MASK; + + *val = fxas21002c_anglevel_scale_avail[raw][0]; + *val2 = fxas21002c_anglevel_scale_avail[raw][1]; + + return IIO_VAL_INT_PLUS_MICRO; +} + +static int fxas21002c_odr_read(struct fxas21002c_data *data, int *val, + int *val2) +{ + int ret; + unsigned int raw; + + ret = regmap_read(data->regmap, FXAS21002C_REG_CTRL_REG1, &raw); + if (ret) + return ret; + + raw = (raw & FXAS21002C_ODR_MASK) >> FXAS21002C_ODR_SHIFT; + + /* + * We don't use this mode but according to the datasheet its + * also a 12.5Hz + */ + if (raw == 7) + raw = FXAS21002C_ODR_12_5; + + *val = fxas21002c_sample_freq_avail[raw][0]; + *val2 = fxas21002c_sample_freq_avail[raw][1]; + + return IIO_VAL_INT_PLUS_MICRO; +} + static int fxas21002c_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask) @@ -266,10 +344,7 @@ static int fxas21002c_read_raw(struct iio_dev *indio_dev, case IIO_CHAN_INFO_SCALE: switch (chan->type) { case IIO_ANGL_VEL: - *val = 0; - *val2 = FXAS21002C_DEFAULT_SENSITIVITY; - - return IIO_VAL_INT_PLUS_MICRO; + return fxas21002c_scale_read(data, val, val2); case IIO_TEMP: *val = FXAS21002C_TEMP_SCALE; @@ -281,16 +356,76 @@ static int fxas21002c_read_raw(struct iio_dev *indio_dev, if (chan->type != IIO_ANGL_VEL) return -EINVAL; - *val = FXAS21002C_DEFAULT_ODR_HZ; - - return IIO_VAL_INT; + return fxas21002c_odr_read(data, val, val2); } return -EINVAL; } +static int fxas21002c_write_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, int val, + int val2, long mask) +{ + struct fxas21002c_data *data = iio_priv(indio_dev); + int ret = -EINVAL; + int i; + + switch (mask) { + case IIO_CHAN_INFO_SAMP_FREQ: + for (i = 0; i < ARRAY_SIZE(fxas21002c_sample_freq_avail); i++) { + if (fxas21002c_sample_freq_avail[i][0] == val && + fxas21002c_sample_freq_avail[i][1] == val2) + break; + } + + if (i == ARRAY_SIZE(fxas21002c_sample_freq_avail)) + break; + + return regmap_update_bits(data->regmap, + FXAS21002C_REG_CTRL_REG1, + FXAS21002C_ODR_MASK, + i << FXAS21002C_ODR_SHIFT); + case IIO_CHAN_INFO_SCALE: + for (i = 0; i < ARRAY_SIZE(fxas21002c_anglevel_scale_avail); + i++) { + if (fxas21002c_anglevel_scale_avail[i][0] == val && + fxas21002c_anglevel_scale_avail[i][1] == val2) + break; + } + + if (i == ARRAY_SIZE(fxas21002c_anglevel_scale_avail)) + break; + + return regmap_update_bits(data->regmap, + FXAS21002C_REG_CTRL_REG0, + FXAS21002C_SCALE_MASK, i); + } + + return ret; +} + +static IIO_CONST_ATTR(anglevel_scale_available, + "0.001090831 " /* 62.5 mdps */ + "0.000545415 " /* 31.25 mdps */ + "0.000272708 " /* 15.625 mdps */ + "0.000136354"); /* 7.8125 mdps */ + +static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("800 400 200 100 50 25 12.5"); + +static struct attribute *fxas21002c_attributes[] = { + &iio_const_attr_anglevel_scale_available.dev_attr.attr, + &iio_const_attr_sampling_frequency_available.dev_attr.attr, + NULL +}; + +static const struct attribute_group fxas21002c_attribute_group = { + .attrs = fxas21002c_attributes, +}; + static const struct iio_info fxas21002c_info = { .read_raw = fxas21002c_read_raw, + .write_raw = fxas21002c_write_raw, + .attrs = &fxas21002c_attribute_group, }; static int fxas21002c_probe(struct i2c_client *client, -- 2.18.0