Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp519077rbb; Sat, 24 Feb 2024 10:22:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUP/YCxVpi0fzCZwVjI/Oc3TFLXFq8i1PdJCrgyg7+Mt8qS2yl3ohExf1eFiz+z7lOon13E0XpojanQVQRvondbVBKAD7k8hS02x8ezWA== X-Google-Smtp-Source: AGHT+IGoyClwWH7ZvsJxD7XhzlpDQII30X0HMF8k9eXNsP9TNjCCjHsey59Ms5Ftx8T5k5uxSl2w X-Received: by 2002:a05:620a:f81:b0:787:2009:600b with SMTP id b1-20020a05620a0f8100b007872009600bmr3023612qkn.47.1708798961739; Sat, 24 Feb 2024 10:22:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708798961; cv=pass; d=google.com; s=arc-20160816; b=onem2xNOkC8aU3NyoH8b90yTHzJq80YP3vr0YVLYC2Sri1173YqO8UHxcJNcDqOs/6 +l+lQ+64npVxVAueJcvYWPfYgQcoS+sWm3Xn9pYHppDLgk+g8fVSuNkgBe1oCjZAhUMR NJV94xu75PnDi90Wx1G3Bhm54ZhyUQWATnqnJ6Rs+FEP+eGkSsE0k+ukiTMwiJvqTdoJ 8YSTHCJ096IWG1SwgPmME0PLMVbL5faGSL4JKKBW5Dn/fWIoZkkg/PGNDUDNGG/ykysv dDnfJuAB2rOfogmiazI29cG9xF/vox9Walf+JHcG6Y8ImazC4AaKZE0eXUiYFhWvPznF s97w== 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=TL3QS5B9ejfnDaqexAX8VOOdbHaxjokRsG9ocvxPoxE=; fh=FLETEDSF9CXyd3uJ2KywfhUDwUVP7OgflJIoG5QJn6M=; b=KvvnC5gJZ+tn46PH8bPNr6xggNFG2QCvMMftZyIETWguCEYrhhl1unCo1u3MYH3+Ds 4fYDLQj9M2klYimj9EmIZxBEl9BHFyXvdFmGW8Q+bJAdOmgZ6BIBD3LnSoceAi4uO0tU h2mP2YcGawdShPpapGZSSkt/Bi3ZWlz4fYiQWK/XJsAuXJtzbksLwl4ZKn3mjipsNQp5 Cu9xcv60hYaABj4VEAj5ztlBmVUkwJHaHQ2zDL05EvrDEPB1lPIT2opJOXq+RAOgNKXs mYrt0ufAzwD2wqUYV5zmKP9dmzNr9BoVgKbtkyCFVHiKQ2P+90cdm4f/wkE2K1JF+NyB L1/w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=puNCuUNB; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79790-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79790-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id bj38-20020a05620a192600b00787ca9de39esi289116qkb.431.2024.02.24.10.22.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Feb 2024 10:22:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-79790-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=puNCuUNB; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-79790-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79790-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 4896D1C21189 for ; Sat, 24 Feb 2024 18:22:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 759284C3DE; Sat, 24 Feb 2024 18:22:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="puNCuUNB" 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 84D08481D0; Sat, 24 Feb 2024 18:22:31 +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=1708798951; cv=none; b=pR8wbUAU+wE6R4VwtaGGJKwwW2H7V9CnuLyAVxRBbypDeqM0pP1HrnnWnsDi/tzsMN2b0JMMbtHh2/YDtXP5FyEA+0fnL9TN9x2Q7fSZB5D3gSYaMr6Izjt/S4abE8poslx+U4JDI2zqo3Jc77VvPUKljUWRqK50LseELojWTzQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708798951; c=relaxed/simple; bh=vITQDNHGejyqlMVNnPYBxyzzJU5RBoH076vJprzwKp0=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PvbN7fBXWYUu3FqDH0evUmDTG9QoGVa54ohxhBzdTRGTxLjLJLjUk7/1jHWIAith+apRccSGVHwJiu+03N0bbXgoQe33QQbsnJXX1rsi8Wac0MXfRlJPHneIu+XCpbT5V83Igkde8VwxU5zKtresSwDKnjvfNDMSBBZ6TTlSEJY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=puNCuUNB; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7DECAC433C7; Sat, 24 Feb 2024 18:22:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708798951; bh=vITQDNHGejyqlMVNnPYBxyzzJU5RBoH076vJprzwKp0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=puNCuUNBtjbmwNa9S5Zz8I5sBpVY8EFbGj0mYnM1BxHYs4giFO8FK62F1eZFQ66OF fulO2ZdQr09QEzLrFY+p/Fves3PROh2Xz2zMxVmxvkTuC9O3KJ/BIlELPhXgicJtid 0RFLCBYkt2zr/lDcDwLwLRSEeOoQuzB3UxBKrFwFngxKxUWSu92ZKNkaxmZENkRI0Z fK86TDfWjLztOK59S4N1AYib1OyitZvZYSkry6LcfOkjFK2ZVN6fbt/Thw2HPgC9e0 TEpkvTQ8RlIaYue5s/LNyHTfNNs7Xn2JyCE8Wj8bkg6RegiQF+W6J6CS0bnTcyBl2N YsgrW2b8+heIw== Date: Sat, 24 Feb 2024 18:22:15 +0000 From: Jonathan Cameron To: Javier Carrasco Cc: Lars-Peter Clausen , Li peiyu <579lpy@gmail.com>, Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Cameron , linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org Subject: Re: [PATCH 2/4] iio: humidity: hdc3020: add power management Message-ID: <20240224182215.058aaecb@jic23-huawei> In-Reply-To: <20240220-hdc3020-pm-v1-2-d8e60dbe79e9@gmail.com> References: <20240220-hdc3020-pm-v1-0-d8e60dbe79e9@gmail.com> <20240220-hdc3020-pm-v1-2-d8e60dbe79e9@gmail.com> 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, 20 Feb 2024 22:14:56 +0100 Javier Carrasco wrote: > The HDC3020 sensor carries out periodic measurements during normal > operation, but as long as the power supply is enabled, it will carry on > in low-power modes. In order to avoid that and reduce power consumption, > the device can be switched to Trigger-on Demand mode, and if possible, > turn off its regulator. > > According to the datasheet, the maximum "Power Up Ready" is 5 ms. > > Add resume/suspend pm operations to manage measurement mode and > regulator state. > > Signed-off-by: Javier Carrasco Hi Javier, Comments inline. Mainly that you should not have side effects if the power up fails and you should fail probe. Thanks, Jonathan > --- > drivers/iio/humidity/hdc3020.c | 81 +++++++++++++++++++++++++++++++++--------- > 1 file changed, 65 insertions(+), 16 deletions(-) > > diff --git a/drivers/iio/humidity/hdc3020.c b/drivers/iio/humidity/hdc3020.c > index 11ede97a31d7..0da5c5c41cd2 100644 > --- a/drivers/iio/humidity/hdc3020.c > +++ b/drivers/iio/humidity/hdc3020.c > @@ -20,6 +20,8 @@ > #include > #include > #include > +#include > +#include > #include > > #include > @@ -68,6 +70,7 @@ > > struct hdc3020_data { > struct i2c_client *client; > + struct regulator *vdd_supply; > /* > * Ensure that the sensor configuration (currently only heater is > * supported) will not be changed during the process of reading > @@ -551,9 +554,39 @@ static const struct iio_info hdc3020_info = { > .write_event_value = hdc3020_write_thresh, > }; > > -static void hdc3020_stop(void *data) > +static int hdc3020_power_on(struct hdc3020_data *data) > { > - hdc3020_exec_cmd((struct hdc3020_data *)data, HDC3020_EXIT_AUTO); > + int ret; > + > + ret = regulator_enable(data->vdd_supply); > + if (ret) > + return ret; > + > + fsleep(5000); > + > + if (data->client->irq) { > + /* > + * The alert output is activated by default upon power up, > + * hardware reset, and soft reset. Clear the status register. > + */ > + ret = hdc3020_exec_cmd(data, HDC3020_S_STATUS); > + if (ret) > + return ret; > + } > + > + return hdc3020_exec_cmd(data, HDC3020_S_AUTO_10HZ_MOD0); Expectation of a power on fail, in probe at least is it should cleanup after itself. It's messier in resume because there isn't anything sensible to do about it, but we should keep to the convention of no side effects on failure. As such if either of the later parts of this fail, you should power down the regulator before returning. > +} > + > +static int hdc3020_power_off(struct hdc3020_data *data) > +{ > + hdc3020_exec_cmd(data, HDC3020_EXIT_AUTO); > + > + return regulator_disable(data->vdd_supply); > +} > + > +static void hdc3020_exit(void *data) > +{ > + hdc3020_power_off((struct hdc3020_data *)data); Trivial but no need to cast a void * to anything as the C standard says this is fine as implicit. > } > > static int hdc3020_probe(struct i2c_client *client) > @@ -569,6 +602,8 @@ static int hdc3020_probe(struct i2c_client *client) > if (!indio_dev) > return -ENOMEM; > > + dev_set_drvdata(&client->dev, (void *)indio_dev); > + > data = iio_priv(indio_dev); > data->client = client; > mutex_init(&data->lock); > @@ -580,6 +615,14 @@ static int hdc3020_probe(struct i2c_client *client) > indio_dev->info = &hdc3020_info; > indio_dev->channels = hdc3020_channels; > indio_dev->num_channels = ARRAY_SIZE(hdc3020_channels); > + > + data->vdd_supply = devm_regulator_get(&client->dev, "vdd"); > + if (IS_ERR(data->vdd_supply)) > + return dev_err_probe(&client->dev, PTR_ERR(data->vdd_supply), > + "Unable to get VDD regulator\n"); > + > + hdc3020_power_on(data) Check return value. We want to fail probe if the power up didn't work! > + > if (client->irq) { > ret = devm_request_threaded_irq(&client->dev, client->irq, > NULL, hdc3020_interrupt_handler, > @@ -588,22 +631,9 @@ static int hdc3020_probe(struct i2c_client *client) > if (ret) > return dev_err_probe(&client->dev, ret, > "Failed to request IRQ\n"); > - > - /* > - * The alert output is activated by default upon power up, > - * hardware reset, and soft reset. Clear the status register. > - */ > - ret = hdc3020_exec_cmd(data, HDC3020_S_STATUS); > - if (ret) > - return ret; > } > > - ret = hdc3020_exec_cmd(data, HDC3020_S_AUTO_10HZ_MOD0); > - if (ret) > - return dev_err_probe(&client->dev, ret, > - "Unable to set up measurement\n"); > - > - ret = devm_add_action_or_reset(&data->client->dev, hdc3020_stop, data); > + ret = devm_add_action_or_reset(&data->client->dev, hdc3020_exit, data); > if (ret) > return ret; > > @@ -614,6 +644,24 @@ static int hdc3020_probe(struct i2c_client *client) > return 0; > } >