Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3757734ybl; Mon, 3 Feb 2020 06:03:34 -0800 (PST) X-Google-Smtp-Source: APXvYqxA7gECLMksTWYQwT4ve0cJJDgIM6KbEOJPQs7d2y7drBer7RAloPlAnQJBHssoCbKU6bJX X-Received: by 2002:aca:dd05:: with SMTP id u5mr4781822oig.91.1580738614641; Mon, 03 Feb 2020 06:03:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580738614; cv=none; d=google.com; s=arc-20160816; b=yR2E+BqHVzbTGNWRIiir1ejqOhC3D9GNYAxqdUjARhpcdCfITD8I6beDSeZ8LAZl2t ojdR7kkqpyHCNdWAhyt8ZMIVvEqJiZg4HODA4ODssaCtynRPg9bs2y9bNPLC1hKlYfls DFsnQVMtQAf+L9iHcoawJTOUl1wetCFtw31npQkj+V6wpQF7Yk2gRRX5ogRad//vHbsA Zopgscft9+3py1M7Rzv63Qf5U1XULBuJcuBUsnsVJw8cRjnNdLmSdewatRs6g+6kLzTR 4jnnfwKrgoUXJjgOsHJqLH1EY4lyQieD0wraxUKtDDR+mY3yAPDyWDN2OBJokpdlWKu/ 1EWg== 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 :organization:references:in-reply-to:message-id:subject:cc:to:from :date; bh=DoHvEnUjFzrZY+scF8RxRzg2YVG7gDPyvzS8tdc4OOE=; b=eJcAMLG6tOU8B6/hL+wKhI+MstffSYkRfav+ichHdu6xUgghtaI5ahLiyUqNuW3TTe OsYONU87Qjgn0DgonzLpvJk1U6szDNiqJG7u//bMp5C3YPRGz4769iGdsny4KM4cQidI KWMHV5thEh9B343tL8kEhe5FbyQK40FDDJE2yCHbI6ZNImqTaDgZfu0XgfPUILaSHjZC 2MItklC5qb/6L+tpwDQ2yTnF/XGa4bAG5fke9JSPmm0r329KOGxZ2TuUVoK2K8c/Xgzm 2O5dLoSdwZ9ZYyqH5nKUpbfGYl4oK8lguJppz176Yu9zUXIH2XToZ6uC5q22Gz9QxBxO tySg== ARC-Authentication-Results: i=1; mx.google.com; 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 l6si9160019otk.134.2020.02.03.06.03.18; Mon, 03 Feb 2020 06:03:34 -0800 (PST) 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; 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 S1727696AbgBCMDo convert rfc822-to-8bit (ORCPT + 99 others); Mon, 3 Feb 2020 07:03:44 -0500 Received: from lhrrgout.huawei.com ([185.176.76.210]:2349 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727102AbgBCMDn (ORCPT ); Mon, 3 Feb 2020 07:03:43 -0500 Received: from LHREML710-CAH.china.huawei.com (unknown [172.18.7.107]) by Forcepoint Email with ESMTP id 3CF4667FA7CBC00D936B; Mon, 3 Feb 2020 12:03:40 +0000 (GMT) Received: from lhreml710-chm.china.huawei.com (10.201.108.61) by LHREML710-CAH.china.huawei.com (10.201.108.33) with Microsoft SMTP Server (TLS) id 14.3.408.0; Mon, 3 Feb 2020 12:03:39 +0000 Received: from localhost (10.202.226.57) by lhreml710-chm.china.huawei.com (10.201.108.61) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1713.5; Mon, 3 Feb 2020 12:03:39 +0000 Date: Mon, 3 Feb 2020 12:03:38 +0000 From: Jonathan Cameron To: Nuno =?ISO-8859-1?Q?S=E1?= CC: Jonathan Cameron , Alexandru Ardelean , , , , Subject: Re: [PATCH 2/4] iio: imu: adis: Refactor adis_initial_startup Message-ID: <20200203120338.000044c1@Huawei.com> In-Reply-To: References: <20200120142051.28533-1-alexandru.ardelean@analog.com> <20200120142051.28533-2-alexandru.ardelean@analog.com> <20200201170839.4ab98d8e@archlinux> Organization: Huawei Technologies Research and Development (UK) Ltd. X-Mailer: Claws Mail 3.17.4 (GTK+ 2.24.32; i686-w64-mingw32) MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8BIT X-Originating-IP: [10.202.226.57] X-ClientProxiedBy: lhreml745-chm.china.huawei.com (10.201.108.195) To lhreml710-chm.china.huawei.com (10.201.108.61) X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 3 Feb 2020 10:31:30 +0100 Nuno S? wrote: > Hi Jonathan, > > > On Sat, 2020-02-01 at 17:08 +0000, Jonathan Cameron wrote: > > On Mon, 20 Jan 2020 16:20:49 +0200 > > Alexandru Ardelean wrote: > > > > > From: Nuno S? > > > > > > All the ADIS devices perform, at the beginning, a self test to make > > > sure > > > the device is in a sane state. Furthermore, some drivers also do a > > > call > > > to `adis_reset()` before the test which is also a good practice. > > > This > > > patch unifies all those operation so that, there's no need for code > > > duplication. Furthermore, the rst pin is also checked to make sure > > > the > > > device is not in HW reset. On top of this, some drivers also read > > > the > > > device product id and compare it with the device being probed to > > > make > > > sure the correct device is being handled. This can also be passed > > > to the > > > library by introducing a variable holding the PROD_ID register of > > > the > > > device. > > > > > > Signed-off-by: Nuno S? > > > Signed-off-by: Alexandru Ardelean > > > --- > > > drivers/iio/imu/Kconfig | 1 + > > > drivers/iio/imu/adis.c | 63 ++++++++++++++++++++++++++---- > > > ------ > > > include/linux/iio/imu/adis.h | 15 ++++++++- > > > 3 files changed, 61 insertions(+), 18 deletions(-) > > > > > > diff --git a/drivers/iio/imu/Kconfig b/drivers/iio/imu/Kconfig > > > index 60bb1029e759..63036cf473c7 100644 > > > --- a/drivers/iio/imu/Kconfig > > > +++ b/drivers/iio/imu/Kconfig > > > @@ -85,6 +85,7 @@ endmenu > > > > > > config IIO_ADIS_LIB > > > tristate > > > + depends on GPIOLIB > > > help > > > A set of IO helper functions for the Analog Devices ADIS* > > > device family. > > > > > > diff --git a/drivers/iio/imu/adis.c b/drivers/iio/imu/adis.c > > > index d02b1911b0f2..1eca5271380e 100644 > > > --- a/drivers/iio/imu/adis.c > > > +++ b/drivers/iio/imu/adis.c > > > @@ -7,6 +7,7 @@ > > > */ > > > > > > #include > > > +#include > > > #include > > > #include > > > #include > > > @@ -365,36 +366,64 @@ static int adis_self_test(struct adis *adis) > > > } > > > > > > /** > > > - * adis_inital_startup() - Performs device self-test > > > + * __adis_initial_startup() - Device initial setup > > > * @adis: The adis device > > > * > > > + * This functions makes sure the device is not in reset, via rst > > > pin. > > > + * Furthermore it performs a SW reset (only in the case we are not > > > coming from > > > + * reset already) and a self test. It also compares the product id > > > with the > > > + * device id if the prod_id_reg variable is set. > > > + * > > > * Returns 0 if the device is operational, a negative error code > > > otherwise. > > > * > > > * This function should be called early on in the device > > > initialization sequence > > > * to ensure that the device is in a sane and known state and that > > > it is usable. > > > */ > > > -int adis_initial_startup(struct adis *adis) > > > +int __adis_initial_startup(struct adis *adis) > > > { > > > int ret; > > > - > > > - mutex_lock(&adis->state_lock); > > > + struct gpio_desc *gpio; > > > + const struct adis_timeout *timeouts = adis->data->timeouts; > > > + const char *iio_name = spi_get_device_id(adis->spi)->name; > > > + u16 prod_id, dev_id; > > > + > > > + /* check if the device has rst pin low */ > > > + gpio = devm_gpiod_get_optional(&adis->spi->dev, "reset", > > > GPIOD_ASIS); > > > + if (IS_ERR(gpio)) { > > > + return PTR_ERR(gpio); > > > > Given you are returning here, no need for else to follow > > > > if (gpio... > > > > Definitely... > > > > + } else if (gpio && gpiod_get_value_cansleep(gpio)) { > > > + /* bring device out of reset */ > > > + gpiod_set_value_cansleep(gpio, 0); > > > > Hmm. So is a software reset the best option if we have a hardware > > reset > > line but it's not currently in the reset mode? > > > > Hmm, that's a fair question. Now that I think about it, if we do have a > gpio we should just assume it's in reset and call > `gpiod_set_value_cansleep`. So, I guess we could just ditch the > `gpiod_get_value_cansleep(gpio)` part. Not sure I agree. For example the driver may well have been unbound and rebound for some reason. I would argue you should just do a set / reset cycle with appropriate sleep in between. If it's already set then no harm done, if it isn't you force a hardware reset. > > > > + msleep(timeouts->reset_ms); > > > + } else { > > > + ret = __adis_reset(adis); > > > + if (ret) > > > + return ret; > > > + } > > > > > > ret = adis_self_test(adis); > > > - if (ret) { > > > - dev_err(&adis->spi->dev, "Self-test failed, trying > > > reset.\n"); > > > - __adis_reset(adis); > > > - ret = adis_self_test(adis); > > > - if (ret) { > > > - dev_err(&adis->spi->dev, "Second self-test > > > failed, giving up.\n"); > > > - goto out_unlock; > > > - } > > > - } > > > + if (ret) > > > + return ret; > > > > > > -out_unlock: > > > - mutex_unlock(&adis->state_lock); > > > - return ret; > > > + if (!adis->data->prod_id_reg) > > > + return 0; > > > + > > > + ret = adis_read_reg_16(adis, adis->data->prod_id_reg, > > > &prod_id); > > > + if (ret) > > > + return ret; > > > + > > > + ret = sscanf(iio_name, "adis%hu\n", &dev_id); > > > > Hmm. I have a general dislike of pulling part name strings apart to > > get > > IDs. It tends to break when someone comes along and adds a part with > > new > > branding. Perhaps just put it in the relevant device part specific > > structures > > directly? > > > > I'll admit that this to orientated to ADI devices and I basically just > took what all the drivers were doing and placed it inside the > library... > > So, you mean passing this to each `chip_info` and then passing it to > the library through `adis_data`? Yes. People don't tend to expect strings to need to take a particular form, so pulling them apart in a library can give unexpected results... > > > + if (ret != 1) > > > + return -EINVAL; > > > + > > > + if (prod_id != dev_id) > > > + dev_warn(&adis->spi->dev, > > > + "Device ID(%u) and product ID(%u) do not > > > match.", > > > + dev_id, prod_id); > > > + > > > + return 0; > > > } > > > -EXPORT_SYMBOL_GPL(adis_initial_startup); > > > +EXPORT_SYMBOL_GPL(__adis_initial_startup); > > > > > > /** > > > * adis_single_conversion() - Performs a single sample conversion > > > diff --git a/include/linux/iio/imu/adis.h > > > b/include/linux/iio/imu/adis.h > > > index d21a013d1122..c43e7922ab32 100644 > > > --- a/include/linux/iio/imu/adis.h > > > +++ b/include/linux/iio/imu/adis.h > > > @@ -41,6 +41,7 @@ struct adis_timeout { > > > * @glob_cmd_reg: Register address of the GLOB_CMD register > > > * @msc_ctrl_reg: Register address of the MSC_CTRL register > > > * @diag_stat_reg: Register address of the DIAG_STAT register > > > + * @prod_id_reg: Register address of the PROD_ID register > > > * @self_test_reg: Register address to request self test command > > > * @status_error_msgs: Array of error messgaes > > > * @status_error_mask: > > > @@ -54,6 +55,7 @@ struct adis_data { > > > unsigned int glob_cmd_reg; > > > unsigned int msc_ctrl_reg; > > > unsigned int diag_stat_reg; > > > + unsigned int prod_id_reg; > > > > > > unsigned int self_test_mask; > > > unsigned int self_test_reg; > > > @@ -299,6 +301,7 @@ static inline int adis_read_reg_32(struct adis > > > *adis, unsigned int reg, > > > > > > int adis_enable_irq(struct adis *adis, bool enable); > > > int __adis_check_status(struct adis *adis); > > > +int __adis_initial_startup(struct adis *adis); > > > > > > static inline int adis_check_status(struct adis *adis) > > > { > > > @@ -311,7 +314,17 @@ static inline int adis_check_status(struct > > > adis *adis) > > > return ret; > > > } > > > > > > -int adis_initial_startup(struct adis *adis); > > > +/* locked version of __adis_initial_startup() */ > > > +static inline int adis_initial_startup(struct adis *adis) > > > +{ > > > + int ret; > > > + > > > + mutex_lock(&adis->state_lock); > > > + ret = __adis_initial_startup(adis); > > > + mutex_unlock(&adis->state_lock); > > > + > > > + return ret; > > > +} > > > > > > int adis_single_conversion(struct iio_dev *indio_dev, > > > const struct iio_chan_spec *chan, unsigned int error_mask, > > > > >