Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1171777pxv; Fri, 25 Jun 2021 07:02:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXhpXa2LEPA7pLrrz3Sbixwgh/+RsYFhA58xVs12H+lgTp0tTtvpsTe0tbzsRuRfD8uJoi X-Received: by 2002:a02:5d0a:: with SMTP id w10mr9486336jaa.82.1624629768657; Fri, 25 Jun 2021 07:02:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624629768; cv=none; d=google.com; s=arc-20160816; b=qRbenU7IOO3oCDffiP7L2SCrulJpXBTTBmlErCyyoL1EjsDNwqVJLWC6vdZDik1HkY MCKCeSKZpQOep+oRb7H4XE978Cu49sSl0L64ODMgoX3ekBOxr0mHB3Ufn9G9640Z0h/P jkuAVlrNyK1MMuj260lvwBx+L419u5LzbGh5e2vmUTFdHvj5uOLvOg4RW5IMkLKI/m/y 5LVs2rlT+A9/pNOWHd+s6qgBrTQk8PPlwP0HF+XwVR7tVmOLtcWXBKBwfqqDJdC7Rrqi RRfFo2odzxrR6BNQ9w8HAXxIdbwbteys0u2a9hePIWJZYSWTqeQQztJQr3JnWDXwu5jR EslQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=D1tsMvDAeuRYczhDgVUkekdSjLDiyIzg0pQFMOQgYwE=; b=DZJQvmI8W7GKX/HxM1AvDXrIyK4WdR0U9RIJs+xacoOu9FJZUI9FW43obgSApzx4Xs RWJ0H2yaa0mnLDmnV3Usq6aFsZVID7hEdInfOBHNp01NPN/zj7K2ORt+2oYIMunQKic4 50Px0M8mBguZl4qSJ03d7w3LkXGHhH1uiGugGHX/eDmwWZRsqf/CPZn41RRkk1tGsIh6 60hbOiiF8DATHgGsHmBLRHZjTvBL+sWHdwJ3w8DB/L0NSRVi2yH94YnLEsBPzYdExi4S lPpOZRKuVFJYFpe5lGsy/mcR0JNZS6ejkSpO0ldd9m/dE9KQWTysXS9CBJo3lhYUo8yt QHfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@deviqon.com header.s=google header.b=USDJkxqN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=deviqon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z11si8060687jat.113.2021.06.25.07.02.35; Fri, 25 Jun 2021 07:02:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@deviqon.com header.s=google header.b=USDJkxqN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=deviqon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231617AbhFYOEY (ORCPT + 99 others); Fri, 25 Jun 2021 10:04:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229890AbhFYOEX (ORCPT ); Fri, 25 Jun 2021 10:04:23 -0400 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBEEDC061767 for ; Fri, 25 Jun 2021 07:02:01 -0700 (PDT) Received: by mail-ej1-x62f.google.com with SMTP id hq39so15285871ejc.5 for ; Fri, 25 Jun 2021 07:02:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=deviqon.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=D1tsMvDAeuRYczhDgVUkekdSjLDiyIzg0pQFMOQgYwE=; b=USDJkxqNofnXE1O9aVxZ2a0KZfneORrPKVeXjdKXYnGUruc9otkm3oF7JI+punj3Dr PbD5yqeoc5mwE6fxs2HShcb4dNHkt6hDqvI6KBv76WUWDf5Q6KV5J+j1JQSRlKzfaa75 9UKuh524GKV40adPXM5eKskCd8H1/UFjltCW46l1UawkrSas2EpYPSGUUBxqc/v4086A sNxyGbNNRxU/omf83drSmMmMN55S2tV5VTG1rUIYQ15aEpFRhtHD6sIVC5K1yKPvBfk3 gxUvIXrbr/Vh5Szu9f8k0jTdp4fvkwc1ZHTP9hYxC+S3Xun2jwUosazOdAjxDPURe79x NXEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=D1tsMvDAeuRYczhDgVUkekdSjLDiyIzg0pQFMOQgYwE=; b=Jb3yIMRAnZLxQiIRVwKK2sJhVlJINPLSyeb1DF5bwPYwaaaofmTB4qe3zgpPsgpo/7 JPBSQCyLLj5adwiZnJgyjONg9nLO+aP6lI2TJGPVyqXayS5DLFz4OI169yAhBLWUk9Sc qYJN5PjZxUqqgKPTSJpJgjA7XKQoDSw9U5PyI0CvO+5PMSk/4xgR4apOHDq/c85SLLsz S4oGWJL5WshghqdamZ9vWg+JQE6KvtfDHX1shbRe6v/nJpPYhivy58B1Y2HTh16Hz3ga ZLd5XMUJQ8Em8AtaLkGNLrrolGgBXBf/3PSDcEBVL6X6attGxwxlAfYcjDMAAqTwkmX2 tYpw== X-Gm-Message-State: AOAM533h7r9i6MaQEUh4F31x7tNRx4DTVMFMjK7ivuJtbTGxKTlni5I0 QPgWq18uWX5Kmx4Wjdnp7BhCcg== X-Received: by 2002:a17:907:9813:: with SMTP id ji19mr11118539ejc.318.1624629720334; Fri, 25 Jun 2021 07:02:00 -0700 (PDT) Received: from neptune.. ([5.2.193.191]) by smtp.gmail.com with ESMTPSA id g23sm2767853ejh.116.2021.06.25.07.01.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Jun 2021 07:01:59 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: jic23@kernel.org, Alexandru Ardelean Subject: [PATCH 2/2] iio: accel: bma220: make suspend state setting more robust Date: Fri, 25 Jun 2021 17:01:37 +0300 Message-Id: <20210625140137.362282-2-aardelean@deviqon.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210625140137.362282-1-aardelean@deviqon.com> References: <20210625140137.362282-1-aardelean@deviqon.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The datasheet mentions that the suspend mode is toggled by reading the suspend register. The reading returns value 0xFF if the system was in suspend mode, otherwise it returns value 0x00. The bma220_deinit() function does up to 2 reads, in case the device was in suspend mode, which suggests a level of paranoia that makes the logic in bma220_suspend() and bma220_resume() look insufficient. This change implements a bma220_power() function which does up to 2 reads of the suspend register to make sure that the chip enters a desired (suspended or normal) mode. If the transition fails, then -EBUSY is returned. Since only a reference to SPI device is required, we can remove the spi_set_drvdata() call and get the SPI device object from the base device object in the suspend/resume routines. Signed-off-by: Alexandru Ardelean --- drivers/iio/accel/bma220_spi.c | 41 ++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/drivers/iio/accel/bma220_spi.c b/drivers/iio/accel/bma220_spi.c index 0095931a11f8..bc4c626e454d 100644 --- a/drivers/iio/accel/bma220_spi.c +++ b/drivers/iio/accel/bma220_spi.c @@ -218,14 +218,33 @@ static int bma220_init(struct spi_device *spi) return 0; } -static void bma220_deinit(void *spi) +static int bma220_power(struct spi_device *spi, bool up) { - int ret; + int i, ret; + + /** + * The chip can be suspended/woken up by a simple register read. + * So, we need up to 2 register reads of the suspend register + * to make sure that the device is in the desired state. + */ + for (i = 0; i < 2; i++) { + ret = bma220_read_reg(spi, BMA220_REG_SUSPEND); + if (ret < 0) + return ret; - /* Make sure the chip is powered off */ - ret = bma220_read_reg(spi, BMA220_REG_SUSPEND); - if (ret == BMA220_SUSPEND_SLEEP) - bma220_read_reg(spi, BMA220_REG_SUSPEND); + if (up && ret == BMA220_SUSPEND_SLEEP) + return 0; + + if (!up && ret == BMA220_SUSPEND_WAKE) + return 0; + } + + return -EBUSY; +} + +static void bma220_deinit(void *spi) +{ + bma220_power(spi, false); } static int bma220_probe(struct spi_device *spi) @@ -242,7 +261,6 @@ static int bma220_probe(struct spi_device *spi) data = iio_priv(indio_dev); data->spi_device = spi; - spi_set_drvdata(spi, indio_dev); mutex_init(&data->lock); indio_dev->info = &bma220_info; @@ -273,17 +291,16 @@ static int bma220_probe(struct spi_device *spi) static __maybe_unused int bma220_suspend(struct device *dev) { - struct bma220_data *data = iio_priv(dev_get_drvdata(dev)); + struct spi_device *spi = to_spi_device(dev); - /* The chip can be suspended/woken up by a simple register read. */ - return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND); + return bma220_power(spi, false); } static __maybe_unused int bma220_resume(struct device *dev) { - struct bma220_data *data = iio_priv(dev_get_drvdata(dev)); + struct spi_device *spi = to_spi_device(dev); - return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND); + return bma220_power(spi, true); } static SIMPLE_DEV_PM_OPS(bma220_pm_ops, bma220_suspend, bma220_resume); -- 2.31.1