Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2932259imm; Tue, 4 Sep 2018 12:20:39 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaSbI7LzhqeRiwvLp1pWlKNozwd98XEiC6Wjn/Nyh6PC/2kxR1/0Ks/hqBrIihxGe0gIrEk X-Received: by 2002:a17:902:8b8b:: with SMTP id ay11-v6mr34451103plb.1.1536088839370; Tue, 04 Sep 2018 12:20:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536088839; cv=none; d=google.com; s=arc-20160816; b=WGI8rKw93Op3XND/pxnciShpzu8XEFlJmj2ISKQRNVpCsgKf7Pqtq97LangP4tCpE7 hBbYRdFd+FZrgc9gN/aDY0da767OAniWxs/Z66l0aWJ2QQ3aQCjGZBedG+VfVXcDyeMh 5xQ+UlC7WJHm8aUxrvd9HqIUytDj3Zx3mAfCjcELlko3+ZdfhJCRXv/FgUQ8E5un/roO SRV38rTKyNDlgiez105TQGfROYlaHOpTniu99Q9B8DE2vpjCpnAAMlxQ8SAU0pXUb2Zx jHZyP5Y0KmtV/0vyJ0Ch2kOs1PQM13xUOVyYtdSUNapM411opW8hb7GU67WWsQb7VDhF SX3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature :arc-authentication-results; bh=fhC2Od440nvVHXmlVNaEVfbbJXKBg7giCn++uIJH7Ds=; b=P6Jq/U3NlTdp/3F+RuPogQRcANo4vJIqceSqiRviEGJyzE9hvH3NXhF1HJ/owJx9k/ EMftZNY6A/qqM1HOx/sbpWdKkxMr/57t79KgU4h60cLO/F3xLoX4iT/q4Mbt2y4Nh0vy FLJW2b2ajjGx1dXAZFOq4By9icc65/4TFFxmhJ0YrcxejfMhIYyV0f0HtakQy5b/U8rm yyxAdd7MVpK32wg714iOnktpvY/sOaz6MX9gSq8/NSZH5oh1VAPu+LkvzjLY3y97cEMo s8D13mp31ALedjtbXD59HjhVXousHh+Lx2c4/1VJ86f9OwBlRkJ0ix6fZ8GgkEZ9BNYB +Okw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=BxihehYX; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u18-v6si23431965pfa.28.2018.09.04.12.20.23; Tue, 04 Sep 2018 12:20:39 -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=@gmail.com header.s=20161025 header.b=BxihehYX; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728021AbeIDXpe (ORCPT + 99 others); Tue, 4 Sep 2018 19:45:34 -0400 Received: from mail-qt0-f196.google.com ([209.85.216.196]:44745 "EHLO mail-qt0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727765AbeIDXpe (ORCPT ); Tue, 4 Sep 2018 19:45:34 -0400 Received: by mail-qt0-f196.google.com with SMTP id k38-v6so5298072qtk.11; Tue, 04 Sep 2018 12:19:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=fhC2Od440nvVHXmlVNaEVfbbJXKBg7giCn++uIJH7Ds=; b=BxihehYX2cBZ3mvzWfVaG1N0nuUFvNc9pmc0283txTVjDgX1PpUi9+cfghsTvm1seK 8WLKOoQioosCjBjj1uKCnlVTm7uqXz+Z7Sc7WGTDgUThVWWUu0f2m8u6hOqINKQBMeVE 8gAcyCTlIJ+UPQQake7+zTGVtDPwVr/LpBgehgGNMZWu5BEKzE/NvnV7ivvfEFAhlk7j V5IRhvZWEg4z+c79YItjH9rfxJV9GbFvXH9Ea6TRbJAaDEBJiZn/0STaSGzGbAVMXFPn m+Se6CxRuN1Opm/Sy7nCpaBW3J/kiHPaiqPQ1mpXi/qRBnd2fVEaFdY1fcXiWLVhNzdK TF+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=fhC2Od440nvVHXmlVNaEVfbbJXKBg7giCn++uIJH7Ds=; b=X5gJhG25Lm0TzXkn9q68CdJPucCeRcvQOEEmRTDToTv/34aImfos8TC9JC3Qpf0a8H YT1ONxBxo3xIP3RxgZyLhsxCibg/5bj1zOJJJn+dDIwwo7JZTiVWIB84FMMFI16c1Ou3 zY/Or9CeOyV5xtoICLqhg0pmWFJKjTa3+k3aqtrEseQGUZK4ATODjvnUIBba3/UJXdKT jYguRMdSYot7WbUND1mQfyJLbtyzZiDlbbR6rdkEEkWUt2gAvNMci0D1kWDwp7VqFvRF /jqVhDVVOAcrC+a7PqdML11pc4yFSrvcdr8Ryb2qt3kkiHDPu7q8joKet7wcmrcYCScg TEHw== X-Gm-Message-State: APzg51C7UWcOPbsSLVlouQKPtzQmet0DvwKebF/HWDbcjibDlQR3FKkd NAYPX98B/Euo7Q1g8In/J4TVvp8op1/gznukCA4= X-Received: by 2002:ac8:1a04:: with SMTP id v4-v6mr32445961qtj.183.1536088743380; Tue, 04 Sep 2018 12:19:03 -0700 (PDT) MIME-Version: 1.0 References: <20180904200434.10442-1-afonsobordado@az8.co> In-Reply-To: <20180904200434.10442-1-afonsobordado@az8.co> From: Andy Shevchenko Date: Tue, 4 Sep 2018 22:18:51 +0300 Message-ID: Subject: Re: [PATCH v3 1/4] iio: gyro: add support for fxas21002c To: afonsobordado@az8.co Cc: Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald , linux-iio , Linux Kernel Mailing List , devicetree Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Sep 4, 2018 at 10:08 PM Afonso Bordado wrote: > > FXAS21002C is a 3 axis gyroscope with integrated temperature sensor > > Signed-off-by: Afonso Bordado > --- > Changes in v3 > - Use unsigned int on regmap functions > - Remove the export of the regmap config > - Fix undefined behaviour on dev_err call > - Minor cleanups > > Changes in v2 > - Use ANSI C Comments > - Minor cleanups > - More dscriptive devicetree bindings > > drivers/iio/gyro/Kconfig | 11 + > drivers/iio/gyro/Makefile | 1 + > drivers/iio/gyro/fxas21002c.c | 372 ++++++++++++++++++++++++++++++++++ > 3 files changed, 384 insertions(+) > create mode 100644 drivers/iio/gyro/fxas21002c.c > > diff --git a/drivers/iio/gyro/Kconfig b/drivers/iio/gyro/Kconfig > index 3126cf05e6b9..d71e33ea9fa4 100644 > --- a/drivers/iio/gyro/Kconfig > +++ b/drivers/iio/gyro/Kconfig > @@ -73,6 +73,17 @@ config BMG160_SPI > tristate > select REGMAP_SPI > > +config FXAS21002C > + tristate "Freescale FXAS21002C Gyroscope" > + depends on I2C > + select REGMAP_I2C > + help > + Say yes here to build support for the Freescale FXAS21002C Gyroscope > + driver connected via I2C. > + > + This driver can also be built as a module. If so, the module > + will be called fxas21002c. > + > config HID_SENSOR_GYRO_3D > depends on HID_SENSOR_HUB > select IIO_BUFFER > diff --git a/drivers/iio/gyro/Makefile b/drivers/iio/gyro/Makefile > index 295ec780c4eb..ec3e2aeae92a 100644 > --- a/drivers/iio/gyro/Makefile > +++ b/drivers/iio/gyro/Makefile > @@ -12,6 +12,7 @@ obj-$(CONFIG_ADXRS450) += adxrs450.o > obj-$(CONFIG_BMG160) += bmg160_core.o > obj-$(CONFIG_BMG160_I2C) += bmg160_i2c.o > obj-$(CONFIG_BMG160_SPI) += bmg160_spi.o > +obj-$(CONFIG_FXAS21002C) += fxas21002c.o > > obj-$(CONFIG_HID_SENSOR_GYRO_3D) += hid-sensor-gyro-3d.o > > diff --git a/drivers/iio/gyro/fxas21002c.c b/drivers/iio/gyro/fxas21002c.c > new file mode 100644 > index 000000000000..1668e6f3fe11 > --- /dev/null > +++ b/drivers/iio/gyro/fxas21002c.c > @@ -0,0 +1,372 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * FXAS21002C - Digital Angular Rate Gyroscope driver > + * > + * Copyright (c) 2018, Afonso Bordado > + * > + * 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 > + * Devicetree > + * Power management > + * GPIO Reset > + * Power supplies > + * Mount Matrix > + * LowPass/HighPass Filters > + * Buffers > + * Interrupts > + * Alarms > + * SPI Support > + */ With this rather long TODO list it sounds like a staging candidate, though if it allows us to use it in the shape provided by the change, code looks good Reviewed-by: Andy Shevchenko > + > +#include > +#include > +#include > +#include > +#include > + > +#define FXAS21002C_DRV_NAME "fxas21002c" > + > +#define FXAS21002C_MAX_TRANSITION_TIME_MS 61 > + > +#define FXAS21002C_CHIP_ID 0xD7 > + > +#define FXAS21002C_REG_STATUS 0x00 > +#define FXAS21002C_REG_OUT_X_MSB 0x01 > +#define FXAS21002C_REG_OUT_X_LSB 0x02 > +#define FXAS21002C_REG_OUT_Y_MSB 0x03 > +#define FXAS21002C_REG_OUT_Y_LSB 0x04 > +#define FXAS21002C_REG_OUT_Z_MSB 0x05 > +#define FXAS21002C_REG_OUT_Z_LSB 0x06 > +#define FXAS21002C_REG_DR_STATUS 0x07 > +#define FXAS21002C_REG_F_STATUS 0x08 > +#define FXAS21002C_REG_F_SETUP 0x09 > +#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_REG_RT_CFG 0x0E > +#define FXAS21002C_REG_RT_SRC 0x0F > +#define FXAS21002C_REG_RT_THS 0x10 > +#define FXAS21002C_REG_RT_COUNT 0x11 > +#define FXAS21002C_REG_TEMP 0x12 > + > +#define FXAS21002C_REG_CTRL_REG1 0x13 > +#define FXAS21002C_RST_BIT BIT(6) > +#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_DEFAULT_ODR_HZ 800 > + > +/* 0.0625 deg/s */ > +#define FXAS21002C_DEFAULT_SENSITIVITY IIO_DEGREE_TO_RAD(62500) > + > +#define FXAS21002C_TEMP_SCALE 1000 > + > +enum { > + ID_FXAS21002C, > +}; > + > +enum fxas21002c_operating_mode { > + FXAS21002C_OM_BOOT, > + FXAS21002C_OM_STANDBY, > + FXAS21002C_OM_READY, > + FXAS21002C_OM_ACTIVE, > +}; > + > +struct fxas21002c_data { > + struct i2c_client *client; > + struct regmap *regmap; > +}; > + > +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), > + regmap_reg_range(FXAS21002C_REG_RT_THS, FXAS21002C_REG_RT_COUNT), > + regmap_reg_range(FXAS21002C_REG_CTRL_REG1, FXAS21002C_REG_CTRL_REG3), > +}; > + > +static const struct regmap_access_table fxas21002c_writable_table = { > + .yes_ranges = fxas21002c_writable_ranges, > + .n_yes_ranges = ARRAY_SIZE(fxas21002c_writable_ranges), > +}; > + > +static const struct regmap_range fxas21002c_volatile_ranges[] = { > + regmap_reg_range(FXAS21002C_REG_STATUS, FXAS21002C_REG_F_STATUS), > + regmap_reg_range(FXAS21002C_REG_F_EVENT, FXAS21002C_REG_INT_SRC_FLAG), > + regmap_reg_range(FXAS21002C_REG_RT_COUNT, FXAS21002C_REG_CTRL_REG1), > +}; > + > +static const struct regmap_access_table fxas21002c_volatile_table = { > + .yes_ranges = fxas21002c_volatile_ranges, > + .n_yes_ranges = ARRAY_SIZE(fxas21002c_volatile_ranges), > +}; > + > +const struct regmap_config fxas21002c_regmap_config = { > + .reg_bits = 8, > + .val_bits = 8, > + > + .max_register = FXAS21002C_REG_CTRL_REG3, > + /* We don't specify a .rd_table because everything is readable */ > + .wr_table = &fxas21002c_writable_table, > + .volatile_table = &fxas21002c_volatile_table, > +}; > + > +#define FXAS21002C_GYRO_CHAN(_axis) { \ > + .type = IIO_ANGL_VEL, \ > + .modified = 1, \ > + .channel2 = IIO_MOD_ ## _axis, \ > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ > + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ > + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ > + .address = FXAS21002C_REG_OUT_ ## _axis ## _MSB, \ > +} > + > +static const struct iio_chan_spec fxas21002c_channels[] = { > + { > + .type = IIO_TEMP, > + .address = FXAS21002C_REG_TEMP, > + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | > + BIT(IIO_CHAN_INFO_SCALE), > + }, > + FXAS21002C_GYRO_CHAN(X), > + FXAS21002C_GYRO_CHAN(Y), > + FXAS21002C_GYRO_CHAN(Z), > + IIO_CHAN_SOFT_TIMESTAMP(3), > +}; > + > +static int fxas21002c_set_operating_mode(struct fxas21002c_data *data, > + enum fxas21002c_operating_mode om) > +{ > + int ret; > + int mask; > + > + switch (om) { > + case FXAS21002C_OM_STANDBY: > + mask = 0; > + break; > + case FXAS21002C_OM_READY: > + mask = FXAS21002C_READY_BIT; > + break; > + case FXAS21002C_OM_ACTIVE: > + mask = FXAS21002C_ACTIVE_BIT; > + break; > + > + default: > + return -EINVAL; > + } > + > + ret = regmap_write(data->regmap, FXAS21002C_REG_CTRL_REG1, mask); > + if (ret) { > + dev_err(&data->client->dev, > + "could not switch operating mode\n"); > + return ret; > + } > + > + msleep(FXAS21002C_MAX_TRANSITION_TIME_MS); > + > + return 0; > +} > + > +static void fxas21002c_standby(void *_data) > +{ > + struct fxas21002c_data *data = _data; > + > + fxas21002c_set_operating_mode(data, FXAS21002C_OM_STANDBY); > +} > + > +static int fxas21002c_reset(struct fxas21002c_data *data) > +{ > + int ret; > + > + /* > + * On issuing a Software Reset command over an I2C interface, > + * the device immediately resets and does not send any > + * acknowledgment (ACK) of the written byte to the Master. > + * > + * This is documented in table 46 on the datasheet. Due to this > + * the write will fail with EREMOTEIO. > + */ > + ret = regmap_write(data->regmap, > + FXAS21002C_REG_CTRL_REG1, FXAS21002C_RST_BIT); > + > + if (ret != -EREMOTEIO) { > + dev_err(&data->client->dev, "could not reset device\n"); > + return ret; > + } > + > + regcache_mark_dirty(data->regmap); > + > + /* Wait for device to boot up */ > + msleep(FXAS21002C_MAX_TRANSITION_TIME_MS); > + > + return 0; > +} > + > +static int fxas21002c_verify_chip(struct fxas21002c_data *data) > +{ > + int ret; > + unsigned int chip_id; > + > + ret = regmap_read(data->regmap, FXAS21002C_REG_WHO_AM_I, &chip_id); > + if (ret) { > + dev_err(&data->client->dev, "could not read device id\n"); > + return ret; > + } > + > + if (chip_id != FXAS21002C_CHIP_ID) { > + dev_err(&data->client->dev, > + "unsupported chip id %02x\n", chip_id); > + return -ENODEV; > + } > + > + return 0; > +} > + > +static int fxas21002c_read_oneshot(struct fxas21002c_data *data, > + struct iio_chan_spec const *chan, int *val) > +{ > + int ret; > + __be16 bulk_raw; > + unsigned int uval; > + > + switch (chan->type) { > + case IIO_ANGL_VEL: > + ret = regmap_bulk_read(data->regmap, chan->address, > + &bulk_raw, sizeof(bulk_raw)); > + if (ret) > + return ret; > + > + *val = sign_extend32(be16_to_cpu(bulk_raw), 15); > + return IIO_VAL_INT; > + case IIO_TEMP: > + ret = regmap_read(data->regmap, chan->address, &uval); > + if (ret) > + return ret; > + > + *val = sign_extend32(uval, 7); > + return IIO_VAL_INT; > + default: > + return -EINVAL; > + } > +} > + > +static int fxas21002c_read_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); > + > + switch (mask) { > + case IIO_CHAN_INFO_RAW: > + return fxas21002c_read_oneshot(data, chan, val); > + case IIO_CHAN_INFO_SCALE: > + switch (chan->type) { > + case IIO_ANGL_VEL: > + *val = 0; > + *val2 = FXAS21002C_DEFAULT_SENSITIVITY; > + > + return IIO_VAL_INT_PLUS_MICRO; > + case IIO_TEMP: > + *val = FXAS21002C_TEMP_SCALE; > + > + return IIO_VAL_INT; > + default: > + return -EINVAL; > + } > + case IIO_CHAN_INFO_SAMP_FREQ: > + if (chan->type != IIO_ANGL_VEL) > + return -EINVAL; > + > + *val = FXAS21002C_DEFAULT_ODR_HZ; > + > + return IIO_VAL_INT; > + } > + > + return -EINVAL; > +} > + > +static const struct iio_info fxas21002c_info = { > + .read_raw = fxas21002c_read_raw, > +}; > + > +static int fxas21002c_probe(struct i2c_client *client, > + const struct i2c_device_id *id) > +{ > + int ret; > + struct iio_dev *indio_dev; > + struct fxas21002c_data *data; > + > + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); > + if (!indio_dev) > + return -ENOMEM; > + > + i2c_set_clientdata(client, indio_dev); > + data = iio_priv(indio_dev); > + data->client = client; > + > + data->regmap = devm_regmap_init_i2c(client, &fxas21002c_regmap_config); > + if (IS_ERR(data->regmap)) { > + ret = PTR_ERR(data->regmap); > + dev_err(&client->dev, > + "Failed to allocate regmap, err: %d\n", ret); > + return ret; > + } > + > + indio_dev->dev.parent = &client->dev; > + indio_dev->channels = fxas21002c_channels; > + indio_dev->num_channels = ARRAY_SIZE(fxas21002c_channels); > + indio_dev->name = id->name; > + indio_dev->modes = INDIO_DIRECT_MODE; > + indio_dev->info = &fxas21002c_info; > + > + ret = fxas21002c_verify_chip(data); > + if (ret < 0) > + return ret; > + > + ret = fxas21002c_reset(data); > + if (ret < 0) > + return ret; > + > + ret = fxas21002c_set_operating_mode(data, FXAS21002C_OM_ACTIVE); > + if (ret < 0) > + return ret; > + > + ret = devm_add_action(&client->dev, fxas21002c_standby, data); > + if (ret < 0) { > + fxas21002c_standby(data); > + dev_err(&client->dev, "failed to add standby action\n"); > + return ret; > + } > + > + ret = iio_device_register(indio_dev); > + if (ret < 0) > + dev_err(&client->dev, "failed to register iio device\n"); > + > + return ret; > +} > + > +static const struct i2c_device_id fxas21002c_id[] = { > + {"fxas21002c", ID_FXAS21002C}, > + {} > +}; > + > +MODULE_DEVICE_TABLE(i2c, fxas21002c_id); > + > +static struct i2c_driver fxas21002c_driver = { > + .driver = { > + .name = FXAS21002C_DRV_NAME, > + }, > + .probe = fxas21002c_probe, > + .id_table = fxas21002c_id, > +}; > + > +module_i2c_driver(fxas21002c_driver); > + > +MODULE_AUTHOR("Afonso Bordado "); > +MODULE_DESCRIPTION("FXAS21002C Digital Angular Rate Gyroscope driver"); > +MODULE_LICENSE("GPL v2"); > -- > 2.18.0 > > -- With Best Regards, Andy Shevchenko