Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2806806imu; Sun, 23 Dec 2018 08:13:15 -0800 (PST) X-Google-Smtp-Source: ALg8bN469qQbNSalIWdozWojlAPJdyea3+8wncm4meSEnHlMroN7EoaNSWpgTPBEWw3KanlKZ7bl X-Received: by 2002:a17:902:8541:: with SMTP id d1mr10249787plo.205.1545581595454; Sun, 23 Dec 2018 08:13:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545581595; cv=none; d=google.com; s=arc-20160816; b=GC+S2i6vUGi0RNSZQW/Wmq4CLeZL6CXMgN/BklDcUXHxennFuxOH6sBm3U38hoeL5V VuEU6JgmUS635GwYlfuynZkwmOXyNPlYYE8zZOk2D5ltLLs8nc1GkAN+Oj7DJqCqwgzy ArG4MTHZZw6JDvkIefhLc7b2weamYWiU2SoQurnuaGtvxNbL+v7q0s/vKM956i/BeG56 8B8ofMNMZyGCHt4g173uXVCRE5L5gQd4k41Z8WkjHII5160cppCtxYKw24SXaRhhLd6f SXSv1iH7txujQrKU5Oo+idbHYWRr/Mb50DVHcmUASF0RigerM25QkuuETJczPC1VSdH9 a4HQ== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=lkwJDuEoVHL3xgsx0Wumi9STG5f0RnFMDb5BD4+DIx4=; b=Gf4/3ELHSpU6rD+KEcQVJXl8pDkZDvRpofjoxNjEM7ZUBWAkL1hI7fQWvzBIHkUXQx Y3+4641zZzhXCwt3p84eJ2VhSKntSnTyd0u90dccpxTbh9J1BeCo/CArlf48H2+X77qu +avhJ8S7kP9PU2YAQsPUgchhynmuvUCBHioSBorDwS5Vc6/W6kPsfVWT3maWeINK/0/p wn1HY5saa6UoPk3ta4Fihzy3gY4h66JAmm46Zpaa053ZofW7TaxEacLJJaKhOl8/lVcn uad0lOAn7nV4XhiyZ1pJWvV5DPzrR/LIdgEu+TO0Qn9H+vxwiv6+wD2QlI3OqZ7EuGWB 9Uog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ux69gp4Y; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n59si11231896plb.388.2018.12.23.08.13.00; Sun, 23 Dec 2018 08:13:15 -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; dkim=pass header.i=@kernel.org header.s=default header.b=Ux69gp4Y; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391091AbeLVROl (ORCPT + 99 others); Sat, 22 Dec 2018 12:14:41 -0500 Received: from mail.kernel.org ([198.145.29.99]:51204 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725846AbeLVROl (ORCPT ); Sat, 22 Dec 2018 12:14:41 -0500 Received: from archlinux (cpc91196-cmbg18-2-0-cust659.5-4.cable.virginm.net [81.96.234.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2B2DB21A48; Sat, 22 Dec 2018 17:14:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1545498879; bh=SlwRFVr8o1+pHSQuVQJyZat0WNfmHfVXNZY+I3zGvTo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Ux69gp4YuA7BsGGSUaF0N7mLVo7lkDx4yunoP2u8kb2Praz4/W3jn9ESl4F1Nt56p P7WDjOnmOMXZCj0sujKdQjqu3xhj20ajZt8I6DPiYbuWadHroequUjapT2zBSHO7LJ obq2pX/9gCGlIZQEfP2R6qryyVwV32vVhO31wEG4= Date: Sat, 22 Dec 2018 17:14:34 +0000 From: Jonathan Cameron To: Anson Huang Cc: "knaack.h@gmx.de" , "lars@metafoo.de" , "pmeerw@pmeerw.net" , "linux-iio@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "festevam@gmail.com" , "preid@electromag.com.au" , dl-linux-imx Subject: Re: [PATCH V7] iio: light: isl29018: add vcc regulator operation support Message-ID: <20181222171434.4eca4ea4@archlinux> In-Reply-To: <1545016752-9865-1-git-send-email-Anson.Huang@nxp.com> References: <1545016752-9865-1-git-send-email-Anson.Huang@nxp.com> X-Mailer: Claws Mail 3.17.2 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 17 Dec 2018 03:25:17 +0000 Anson Huang wrote: > The light sensor's power supply could be controllable by regulator > on some platforms, such as i.MX6Q-SABRESD board, the light sensor > isl29023's power supply is controlled by a GPIO fixed regulator, > need to make sure the regulator is enabled before any operation of > sensor, this patch adds vcc regulator operation support. > > Signed-off-by: Anson Huang Hi Anson See below. > --- > ChangeLog since V6 > - using devm_regulator_get() instead of devm_regulator_get_optional() since the regulator is > there anyway, if dtb does NOT specify one, regulator framework will assign dummy regulator for it; > - Setup devm action for cleaning up regulator resource for error handling. > --- > drivers/iio/light/isl29018.c | 58 ++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 58 insertions(+) > > diff --git a/drivers/iio/light/isl29018.c b/drivers/iio/light/isl29018.c > index b45400f..63f7b9d 100644 > --- a/drivers/iio/light/isl29018.c > +++ b/drivers/iio/light/isl29018.c > @@ -23,6 +23,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -95,6 +96,7 @@ struct isl29018_chip { > struct isl29018_scale scale; > int prox_scheme; > bool suspended; > + struct regulator *vcc_reg; > }; > > static int isl29018_set_integration_time(struct isl29018_chip *chip, > @@ -708,6 +710,17 @@ static const char *isl29018_match_acpi_device(struct device *dev, int *data) > return dev_name(dev); > } > > +static void isl29018_disable_regulator_action(void *_data) > +{ > + struct isl29018_chip *chip = _data; > + int err; > + > + err = regulator_disable(chip->vcc_reg); > + if (err) > + dev_err(regmap_get_device(chip->regmap), > + "failed to disable VCC regulator!\n"); > +} > + > static int isl29018_probe(struct i2c_client *client, > const struct i2c_device_id *id) > { > @@ -742,6 +755,37 @@ static int isl29018_probe(struct i2c_client *client, > chip->scale = isl29018_scales[chip->int_time][0]; > chip->suspended = false; > > + chip->vcc_reg = devm_regulator_get(&client->dev, "vcc"); > + if (IS_ERR(chip->vcc_reg)) { > + err = PTR_ERR(chip->vcc_reg); > + if (err != -EPROBE_DEFER) > + dev_err(&client->dev, "failed to get VCC regulator!\n"); > + return err; > + } > + > + err = regulator_enable(chip->vcc_reg); > + if (err) { > + dev_err(&client->dev, "failed to enable VCC regulator!\n"); > + return err; > + } > + > + chip->regmap = devm_regmap_init_i2c(client, > + isl29018_chip_info_tbl[dev_id].regmap_cfg); > + if (IS_ERR(chip->regmap)) { > + err = PTR_ERR(chip->regmap); > + dev_err(&client->dev, "regmap initialization fails: %d\n", err); > + regulator_disable(chip->vcc_reg); > + return err; > + } > + > + err = devm_add_action(&client->dev, isl29018_disable_regulator_action, > + chip); > + if (err) { I'm a little confused, why not do this before devm_regmap_init_i2c. That way you won't have to disable the regulator in that one error path. Also, devm_add_action_or_reset will call isl29018_disable_regulator_action for you on error. > + isl29018_disable_regulator_action(chip); > + dev_err(&client->dev, "failed to setup regulator cleanup action!\n"); > + return err; > + } > + > chip->regmap = devm_regmap_init_i2c(client, > isl29018_chip_info_tbl[dev_id].regmap_cfg); > if (IS_ERR(chip->regmap)) { > @@ -768,6 +812,7 @@ static int isl29018_probe(struct i2c_client *client, > static int isl29018_suspend(struct device *dev) > { > struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev)); > + int ret; > > mutex_lock(&chip->lock); > > @@ -777,6 +822,12 @@ static int isl29018_suspend(struct device *dev) > * So we do not have much to do here. > */ > chip->suspended = true; > + ret = regulator_disable(chip->vcc_reg); > + if (ret) { > + dev_err(dev, "failed to disable VCC regulator\n"); > + mutex_unlock(&chip->lock); > + return ret; Given you are about to unlock anyway a common pattern is to not check ret until after the unlock, thus simplifying the code. > + } > > mutex_unlock(&chip->lock); > > @@ -790,6 +841,13 @@ static int isl29018_resume(struct device *dev) > > mutex_lock(&chip->lock); > > + err = regulator_enable(chip->vcc_reg); > + if (err) { > + dev_err(dev, "failed to enable VCC regulator\n"); > + mutex_unlock(&chip->lock); > + return err; > + } > + > err = isl29018_chip_init(chip); > if (!err) > chip->suspended = false;