Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp922145lqa; Sun, 28 Apr 2024 09:53:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX6pFH7pzDC/ZnfisWWFh+t6227GVNZS1/1kv7+wMWxMUmvomZYMoWooKu1XVbVVQ1TXqBvPEk3tueSSfk8FXnGRIb/lPjkYhe3KznS9A== X-Google-Smtp-Source: AGHT+IGUaeyTCu2285D+2pcB11SqK2neADqAGamewj7Y+sNcCKYa+1xl42cHQW1zcUxBQmHoYERS X-Received: by 2002:a17:902:b08c:b0:1e6:622c:7bb4 with SMTP id p12-20020a170902b08c00b001e6622c7bb4mr9253224plr.19.1714323239586; Sun, 28 Apr 2024 09:53:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714323239; cv=pass; d=google.com; s=arc-20160816; b=ZDzIEIZldQ4n0xvDhMso70ka8U7uYeXB4oLGgkBczWAedoy9E4yk5MnhpgM2jC2OxR kvr8D+bn8BZ1pJ5Q89+PVI17jLKXjv9y7h05wqlOgVqhCNbDt0X/9RbUJSm2QFQjcvKn 6XEruFE0rHWNtyncSqQTK51/6as3KHhmIt2H0slV///Zq6xKZzFYAfH1jw1RjAgGrX7+ ClsrQKI+yLL4mzd5lN6OOx8TliVlENVIlvL5mEJ42qlzu+eT7YZhTqyHzjZKS8zRKxEU 2H++i1UmO+P5ySgPUG2fbyLroB4JiLVHoQsvTQlsK+V5h2+F5ypgmHBwepaig0bSHVJ5 nRAw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=3cVztAxTRLf1U4UfEgZzwFzwwX92RIQa7Ll+8El78m4=; fh=ngaGZcxQlePdtHhUCx84Vk++fWrrAHPh2fx5sSaSC2g=; b=qcCl3jLJcXd4BfeBRHwoYGqibxKihhRukel5RM1rwh9IEIeKCMeXyPZdaEVtPj64KG MZ5UPLblOk3XFncABofhYWG2dxaLmZkEQmQRSWtqvC3+3X+HXRHDCbxDg4N/zGBKqDi5 5KhpOs3Hs9jEdM/+p+2QmuKTCqKzIzIic4RmcHRCSu4ukwWW2UOYZG6Bfu4QH8dpEQdR Y8eRCbzj6+Rjmc9p1FqXBZ16+WjxbpXAQ+z5bpGnhizxMeWZfOimj5emWvtybVoPfONl AsfRp4rWmZpejtNFzhDBOQnv8EWsNnehD+zhNb8vHcEnIacCHdh8Ta9gSvjopQXO6F37 Bmbw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LjR61GuM; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-161506-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161506-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id p3-20020a170902bd0300b001e7a854b118si18361042pls.338.2024.04.28.09.53.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Apr 2024 09:53:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161506-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LjR61GuM; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-161506-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161506-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 2D0D8281686 for ; Sun, 28 Apr 2024 16:53:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 327ED73164; Sun, 28 Apr 2024 16:53:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LjR61GuM" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DE2110F1; Sun, 28 Apr 2024 16:53:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714323231; cv=none; b=VsQ4pxQ1a8AC6kh0BueKaKqqtjAxFDYxcBhQDS+BV5dnP8m51emS9J5PB/yvbfxgwKKscEpEZDDt1KrDUUiF8aeOuq7T6KRiDQUUlA3Yr1ZWtJ597+Tv/Y4G4BlXBYkYId8i3Y1ifbIIb8tY9wjGS6AKogekhZBGxPIpRNzcdn4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714323231; c=relaxed/simple; bh=vKlq852Mkt2LdjN+6R/Og0QA2JGqOCAcs1MaQ4RQDnc=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iq9N/Ne1ywXDDTybXZqiX1MuUj2jg3uKbOtoyn2f58ICtJHnLKGTo5whViKxenyAvqRMb5gSVkhsuLwoB8L6Jr53kfpwEPSHicf1jXrvTQM/M1nwvQNvJ43lxaxy1kN2DTUQfKPolD6j4DLAomzavLdQGYEHKBy7WlkmWQHgck8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LjR61GuM; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A048C113CC; Sun, 28 Apr 2024 16:53:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714323231; bh=vKlq852Mkt2LdjN+6R/Og0QA2JGqOCAcs1MaQ4RQDnc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=LjR61GuMEFGAu419qGiabMclqWpfYwDouS36biVw7/fRhxjf2g4HgGKM8NUteeSZz JLeRT6vNf70R+Uvx7Ke8EzX01G8z5BwsfRDVnblc2Mx5uH/GhL/g+6QK0bVoasYoa3 scxwyecrhm9CLsnpZsVtalzmmNDr0HTP/vpeT8iyiq/cYnKgFfpRj5RF0zdL3LKtty aLWyMXoNDTbCC3DUBkJE3wHHgSX5AlhrwS8J1q3m72uJYJpEbDBFTXJWG93QS0JvDp LcU0xG+a1b0MM8xJepL9Fdlj50PP8nSrQzI1C5o8MtP7Uwg9/CdG78Pmxx5L8YvIau aM85Mca+qEjzw== Date: Sun, 28 Apr 2024 17:53:37 +0100 From: Jonathan Cameron To: Aren Moynihan Cc: Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Liam Girdwood , Mark Brown , Andy Shevchenko , Ondrej Jirman , Uwe =?UTF-8?B?S2xlaW5lLUvDtm5pZw==?= , linux-iio@vger.kernel.org, phone-devel@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, Willow Barraco Subject: Re: [PATCH v2 2/6] iio: light: stk3310: Implement vdd supply and power it off during suspend Message-ID: <20240428175337.61850e2a@jic23-huawei> In-Reply-To: <20240423223309.1468198-4-aren@peacevolution.org> References: <20240423223309.1468198-2-aren@peacevolution.org> <20240423223309.1468198-4-aren@peacevolution.org> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.41; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Tue, 23 Apr 2024 18:33:05 -0400 Aren Moynihan wrote: > From: Ondrej Jirman > > VDD power input can be used to completely power off the chip during > system suspend. Do so if available. > > Signed-off-by: Ondrej Jirman > Signed-off-by: Aren Moynihan Suggestions inline. Key thing is take the whole thing devm_ managed and your life gets much easier. It is mixing the two approaches that causes problems and often the best plan is to do everything in probe/remove with devm_ calls to do the cleanup for you. > --- > > Notes: > Changes in v2: > - always enable / disable regulators and rely on a dummy regulator if > one isn't specified > - replace usleep_range with fsleep > - reorder includes so iio headers are last > - add missing error handling to resume > > drivers/iio/light/stk3310.c | 49 ++++++++++++++++++++++++++++++++++--- > 1 file changed, 46 insertions(+), 3 deletions(-) > > diff --git a/drivers/iio/light/stk3310.c b/drivers/iio/light/stk3310.c > index 7b71ad71d78d..a0547eeca3e3 100644 > --- a/drivers/iio/light/stk3310.c > +++ b/drivers/iio/light/stk3310.c > @@ -13,6 +13,8 @@ > #include > #include > #include > +#include > + > #include > #include > #include > @@ -117,6 +119,7 @@ struct stk3310_data { > struct regmap_field *reg_int_ps; > struct regmap_field *reg_flag_psint; > struct regmap_field *reg_flag_nf; > + struct regulator *vdd_reg; > }; > > static const struct iio_event_spec stk3310_events[] = { > @@ -607,6 +610,10 @@ static int stk3310_probe(struct i2c_client *client) > > mutex_init(&data->lock); > > + data->vdd_reg = devm_regulator_get(&client->dev, "vdd"); > + if (IS_ERR(data->vdd_reg)) > + return dev_err_probe(&client->dev, ret, "get regulator vdd failed\n"); > + > ret = stk3310_regmap_init(data); > if (ret < 0) > return ret; > @@ -617,9 +624,17 @@ static int stk3310_probe(struct i2c_client *client) > indio_dev->channels = stk3310_channels; > indio_dev->num_channels = ARRAY_SIZE(stk3310_channels); > > + ret = regulator_enable(data->vdd_reg); > + if (ret) > + return dev_err_probe(&client->dev, ret, > + "regulator vdd enable failed\n"); > + > + /* we need a short delay to allow the chip time to power on */ > + fsleep(1000); > + > ret = stk3310_init(indio_dev); > if (ret < 0) > - return ret; > + goto err_vdd_disable; > > if (client->irq > 0) { > ret = devm_request_threaded_irq(&client->dev, client->irq, > @@ -645,32 +660,60 @@ static int stk3310_probe(struct i2c_client *client) > > err_standby: > stk3310_set_state(data, STK3310_STATE_STANDBY); Move this handling to a devm_add_action_or_reset() callback in a precursor patch. That will fix the current ordering issue wrt to the irq registration. Then use devm_iio_device_register() in that precursor patch. > +err_vdd_disable: > + regulator_disable(data->vdd_reg); Add a devm_add_action_or_reset() callback to disable this regulator in this patch. Register that just after the enable. That way the ordering will be maintained for all calls. > return ret; > } > > static void stk3310_remove(struct i2c_client *client) > { > struct iio_dev *indio_dev = i2c_get_clientdata(client); > + struct stk3310_data *data = iio_priv(indio_dev); > > iio_device_unregister(indio_dev); > stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY); > + regulator_disable(data->vdd_reg); With above suggested changes, you can drop the remove function entirely. > } > > static int stk3310_suspend(struct device *dev) > { > struct stk3310_data *data; > + int ret; > > data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); > > - return stk3310_set_state(data, STK3310_STATE_STANDBY); > + ret = stk3310_set_state(data, STK3310_STATE_STANDBY); > + if (ret) > + return ret; > + > + regcache_mark_dirty(data->regmap); > + regulator_disable(data->vdd_reg); > + > + return 0; > } > > static int stk3310_resume(struct device *dev) > { > - u8 state = 0; > struct stk3310_data *data; > + u8 state = 0; > + int ret; > > data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); > + > + ret = regulator_enable(data->vdd_reg); > + if (ret) { > + dev_err(dev, "Failed to re-enable regulator vdd\n"); > + return ret; > + } > + > + fsleep(1000); > + > + ret = regcache_sync(data->regmap); > + if (ret) { > + dev_err(dev, "Failed to restore registers: %d\n", ret); > + return ret; > + } > + > if (data->ps_enabled) > state |= STK3310_STATE_EN_PS; > if (data->als_enabled)