From patchwork Tue Jul 17 08:41:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brian Masney X-Patchwork-Id: 10528401 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D1A876020A for ; Tue, 17 Jul 2018 08:42:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B812028C4E for ; Tue, 17 Jul 2018 08:42:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AC01428C61; Tue, 17 Jul 2018 08:42:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3AFA428C4E for ; Tue, 17 Jul 2018 08:42:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728983AbeGQJNx (ORCPT ); Tue, 17 Jul 2018 05:13:53 -0400 Received: from onstation.org ([52.200.56.107]:58286 "EHLO onstation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729690AbeGQJNh (ORCPT ); Tue, 17 Jul 2018 05:13:37 -0400 Received: from xilitla.hsd1.wv.comcast.net (c-98-236-77-125.hsd1.wv.comcast.net [98.236.77.125]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: masneyb) by onstation.org (Postfix) with ESMTPSA id 386A8191E; Tue, 17 Jul 2018 08:42:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=onstation.org; s=default; t=1531816926; bh=llz7OUyRLuWWmD0PPR2Xx+5uzsbcw1USJhCmcYYjbvw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dXBFxMJuDH0/5wMHDv2VLbv6coqK6NFT2Nx4de95uoFVGdAh0DlNiO5sZqI2PzbVF R5dJ5cXmOoXoFxeyUwYH5YHdFuj6K7M132YsXiFrX3ySEPZf2qx2tCCexbf+ZjgXjQ W7DH46TBndKiszUDmqKDW1jA/gkKzn1REEsSp02k= From: Brian Masney To: jic23@kernel.org, robh+dt@kernel.org, mark.rutland@arm.com, andy.gross@linaro.org, david.brown@linaro.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-soc@vger.kernel.org Cc: jonathan@marek.ca, jmaneyrol@invensense.com, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, mkelly@xevo.com, fischerdouglasc@gmail.com, bshah@kde.org, ctatlor97@gmail.com, drew.paterson@ams.com Subject: [PATCH v2 5/7] iio: tsl2772: add support for regulator framework Date: Tue, 17 Jul 2018 04:41:56 -0400 Message-Id: <20180717084158.23532-6-masneyb@onstation.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180717084158.23532-1-masneyb@onstation.org> References: <20180717084158.23532-1-masneyb@onstation.org> Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds support for the regulator framework to the tsl2772 driver. Driver was tested using a LG Nexus 5 (hammerhead) phone with the two regulators and on a Raspberry Pi 2 without any regulators controlling the power to the sensor. Signed-off-by: Brian Masney --- .../devicetree/bindings/iio/light/tsl2772.txt | 4 + drivers/iio/light/tsl2772.c | 82 ++++++++++++++++++- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/iio/light/tsl2772.txt b/Documentation/devicetree/bindings/iio/light/tsl2772.txt index ab553d52b9fc..bfde8b2c8790 100644 --- a/Documentation/devicetree/bindings/iio/light/tsl2772.txt +++ b/Documentation/devicetree/bindings/iio/light/tsl2772.txt @@ -21,6 +21,8 @@ Optional properties: TSL2772_DIODE_BOTH. - amstaos,prox_power - must be TSL2772_100_mA, TSL2772_50_mA, TSL2772_25_mA, or TSL2772_13_mA. + - vdd-supply: phandle to the regulator that provides power to the sensor. + - vddio-supply: phandle to the regulator that provides power to the bus. - interrupt-parent: should be the phandle for the interrupt controller - interrupts: the sole interrupt generated by the device @@ -35,5 +37,7 @@ tsl2772@39 { compatible = "amstaos,tsl2772"; reg = <0x39>; interrupts-extended = <&msmgpio 61 IRQ_TYPE_EDGE_FALLING>; + vdd-supply = <&pm8941_l17>; + vddio-supply = <&pm8941_lvs1>; amstaos,prox_diode = ; }; diff --git a/drivers/iio/light/tsl2772.c b/drivers/iio/light/tsl2772.c index 0be57f2ecb02..f6a27f8b3ca7 100644 --- a/drivers/iio/light/tsl2772.c +++ b/drivers/iio/light/tsl2772.c @@ -20,6 +20,7 @@ #include #include #include +#include /* Cal defs */ #define PROX_STAT_CAL 0 @@ -146,6 +147,9 @@ struct tsl2772_chip { struct mutex prox_mutex; struct mutex als_mutex; struct i2c_client *client; + struct regulator *vdd_supply; + struct regulator *vddio_supply; + bool regulators_enabled; u16 prox_data; struct tsl2772_als_info als_cur_info; struct tsl2772_settings settings; @@ -609,6 +613,46 @@ static int tsl2772_als_calibrate(struct iio_dev *indio_dev) return ret; } +static int tsl2772_enable_regulators(struct tsl2772_chip *chip) +{ + int ret; + + ret = regulator_enable(chip->vddio_supply); + if (ret < 0) { + dev_err(&chip->client->dev, "Failed to enable regulator: %d\n", + ret); + return ret; + } + + ret = regulator_enable(chip->vdd_supply); + if (ret < 0) { + regulator_disable(chip->vddio_supply); + dev_err(&chip->client->dev, "Failed to enable regulator: %d\n", + ret); + return ret; + } + + chip->regulators_enabled = true; + + return 0; +} + +static void tsl2772_disable_regulators(struct tsl2772_chip *chip) +{ + if (!chip->regulators_enabled) + return; + + regulator_disable(chip->vdd_supply); + regulator_disable(chip->vddio_supply); + + chip->regulators_enabled = false; +} + +static void tsl2772_disable_regulators_action(void *_data) +{ + tsl2772_disable_regulators(_data); +} + static int tsl2772_chip_on(struct iio_dev *indio_dev) { struct tsl2772_chip *chip = iio_priv(indio_dev); @@ -666,6 +710,10 @@ static int tsl2772_chip_on(struct iio_dev *indio_dev) chip->als_gain_time_scale = als_time_us * tsl2772_als_gain[chip->settings.als_gain]; + ret = tsl2772_enable_regulators(chip); + if (ret < 0) + return ret; + /* * TSL2772 Specific power-on / adc enable sequence * Power on the device 1st. @@ -724,10 +772,13 @@ static int tsl2772_chip_on(struct iio_dev *indio_dev) static int tsl2772_chip_off(struct iio_dev *indio_dev) { struct tsl2772_chip *chip = iio_priv(indio_dev); + int ret; /* turn device off */ chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED; - return tsl2772_write_control_reg(chip, 0x00); + ret = tsl2772_write_control_reg(chip, 0x00); + tsl2772_disable_regulators(chip); + return ret; } /** @@ -1666,6 +1717,35 @@ static int tsl2772_probe(struct i2c_client *clientp, chip->client = clientp; i2c_set_clientdata(clientp, indio_dev); + chip->vddio_supply = devm_regulator_get(&clientp->dev, "vddio"); + if (IS_ERR(chip->vddio_supply)) { + if (PTR_ERR(chip->vddio_supply) != -EPROBE_DEFER) + dev_err(&clientp->dev, + "Failed to get vddio regulator %d\n", + (int)PTR_ERR(chip->vddio_supply)); + + return PTR_ERR(chip->vddio_supply); + } + + chip->vdd_supply = devm_regulator_get(&clientp->dev, "vdd"); + if (IS_ERR(chip->vdd_supply)) { + if (PTR_ERR(chip->vdd_supply) != -EPROBE_DEFER) + dev_err(&clientp->dev, + "Failed to get vdd regulator %d\n", + (int)PTR_ERR(chip->vdd_supply)); + + return PTR_ERR(chip->vdd_supply); + } + + ret = devm_add_action(&clientp->dev, tsl2772_disable_regulators_action, + chip); + if (ret < 0) { + tsl2772_disable_regulators_action(chip); + dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n", + ret); + return ret; + } + ret = i2c_smbus_read_byte_data(chip->client, TSL2772_CMD_REG | TSL2772_CHIPID); if (ret < 0)