Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp6139006imm; Mon, 27 Aug 2018 10:19:43 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbD73LdU8IRrGJSwbl+I4c6T2Ny0YuUMTDo/P8ShTML2GqXulS2NwBOHyJdHk5kNMFxy1AC X-Received: by 2002:a17:902:d213:: with SMTP id t19-v6mr14020474ply.63.1535390383339; Mon, 27 Aug 2018 10:19:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535390383; cv=none; d=google.com; s=arc-20160816; b=p8btClpnfxus1LASdLNFK1Ym/dVRBnECiQSujOZfitl0V7EmDVen809besp1bGLZyL 4kIWcnqkQm45/mXqgEU9Dl9Aj5Z8DXoMZLEhyDurQtMtio877U/gsbfavpBLbcVpuK6g j4j6YWrbntpeU6mdAnCci/6CHfFySiYmrWAS3QBVxewORfRdzlqZG9S1UgRQ/yFcRB4t hccxC4ifsuozWWSfMs9aNrhaHkpFN7Mmxs/S98LKPXX4R61hNXHGU430hrC+JT4LcVvx CU1AfCjCLJKqliE/6Q0FJPWaRQjKMMmoyXAXAfNBMg/PM1Q8+2eA4gKg9HrzSW60Wvx7 bFBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature:arc-authentication-results; bh=kzt/naUVCnslyq1QL+KVDoPfLLPGiIVTweTD8OyT2Ro=; b=M4sdJhkBfnxembjkGVqVp+dOMV1FF8WtgoaF/uuUUlknaQONIrMtontEtO/Wnb0PuW OKjjICEPCk9KrbGkpkSOCtNTqXjCZ2bvh8qQK4aK9cOAmf2VwK9BL2TJc9yWR5v6zKRI wolZrgF3uvDnpHsyfGqqyKk6p0kEiGqU1jWpean8ll/vWEn2vSpR06N1OxIgt7pqRYJx fFN/PBTu6Te8hF3lqyp3SdHUxP9RSZhZsKoaJN5EiWrn+lLUgOp5skEMz90Q4LYX/YrQ 9dzeJ313s2ZTJE2rpfI0PnO15ORP8mkHfZwdTgLarS7e9KKQK+/D7TkRQ/EifIH0WrlL P00A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fHPV0yGA; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c5-v6si13186667pll.275.2018.08.27.10.19.27; Mon, 27 Aug 2018 10:19:43 -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=@kernel.org header.s=default header.b=fHPV0yGA; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727110AbeH0VFn (ORCPT + 99 others); Mon, 27 Aug 2018 17:05:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:53616 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726895AbeH0VFn (ORCPT ); Mon, 27 Aug 2018 17:05:43 -0400 Received: from archlinux (cpc91196-cmbg18-2-0-cust659.5-4.cable.virginm.net [81.96.234.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0B9B3208D5; Mon, 27 Aug 2018 17:18:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1535390294; bh=hS6HTvCqrNq5Jt+hPYRegCMKldWcjKjscRwbmW7QMow=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=fHPV0yGAz/8tunjH5i7TIIBH0kA5rFO8Xp1qITATRkxknajC0rTfsiS0wC8oTftbB DwXQKjj2yJfWk1VfqqstL21qG94rlT3auyK1ziNDG+twfx7IQHr4mHBjeL2FXQXGxk nMJOuScm+Di/n/cP3CiGDJzJqV/urzyHp+S6v9hY= Date: Mon, 27 Aug 2018 18:18:10 +0100 From: Jonathan Cameron To: Afonso Bordado Cc: knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Subject: Re: [PATCH 3/4] iio: fxas21002c: add ODR/Scale support Message-ID: <20180827181810.6b4ee31d@archlinux> In-Reply-To: <20180825211910.22929-3-afonsobordado@az8.co> References: <20180825211910.22929-1-afonsobordado@az8.co> <20180825211910.22929-3-afonsobordado@az8.co> X-Mailer: Claws Mail 3.16.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 25 Aug 2018 22:19:09 +0100 Afonso Bordado wrote: > This patch adds support for reading/writing ODR/Scale > > We don't support the scale boost modes. > > Signed-off-by: Afonso Bordado A few trivial bits in here. Jonathan > --- > drivers/iio/gyro/fxas21002c.c | 162 +++++++++++++++++++++++++++++++--- > 1 file changed, 149 insertions(+), 13 deletions(-) > > diff --git a/drivers/iio/gyro/fxas21002c.c b/drivers/iio/gyro/fxas21002c.c > index 6fef210630e0..dc0cb9848386 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 > * LowPass/HighPass Filters > * Buffers > @@ -40,7 +40,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 (BIT(0) | BIT(1)) This is a 2 bit mask, not a direct combination of two one bit different things, so better as GENMASK(1, 0) to make that implicit. > + > #define FXAS21002C_REG_RT_CFG 0x0E > #define FXAS21002C_REG_RT_SRC 0x0F > #define FXAS21002C_REG_RT_THS 0x10 > @@ -52,13 +55,12 @@ > #define FXAS21002C_ACTIVE_BIT BIT(1) > #define FXAS21002C_READY_BIT BIT(0) > > -#define FXAS21002C_REG_CTRL_REG2 0x14 > -#define FXAS21002C_REG_CTRL_REG3 0x15 > +#define FXAS21002C_ODR_SHIFT 2 > +#define FXAS21002C_ODR_MASK (BIT(2) | BIT(3) | BIT(4)) > > -#define FXAS21002C_DEFAULT_ODR_HZ 800 > > -// 0.0625 deg/s > -#define FXAS21002C_DEFAULT_SENSITIVITY IIO_DEGREE_TO_RAD(62500) > +#define FXAS21002C_REG_CTRL_REG2 0x14 > +#define FXAS21002C_REG_CTRL_REG3 0x15 > > enum fxas21002c_id { > ID_FXAS21002C, > @@ -76,6 +78,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), > @@ -242,6 +278,47 @@ static int fxas21002c_read_oneshot(struct fxas21002c_data *data, > return IIO_VAL_INT; > } > > +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 /* * We... * also.. */ The kernel style is very fussy about comment syntax. It may seem silly but when you read a lot of code these little thing being consistent help. > + 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) > @@ -255,24 +332,83 @@ static int fxas21002c_read_raw(struct iio_dev *indio_dev, > if (chan->type != IIO_ANGL_VEL) > return -EINVAL; > > - *val = 0; > - *val2 = FXAS21002C_DEFAULT_SENSITIVITY; > - > - return IIO_VAL_INT_PLUS_MICRO; > + return fxas21002c_scale_read(data, val, val2); > case IIO_CHAN_INFO_SAMP_FREQ: > 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; > + > + ret = regmap_update_bits(data->regmap, FXAS21002C_REG_CTRL_REG1, return regmap_update_bits... > + FXAS21002C_ODR_MASK, > + i << FXAS21002C_ODR_SHIFT); > + > + break; > + 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; > + > + ret = regmap_update_bits(data->regmap, FXAS21002C_REG_CTRL_REG0, return regmap_update_bits. > + FXAS21002C_SCALE_MASK, i); > + > + break; > + } > + > + 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,