Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1256581pxb; Fri, 21 Jan 2022 13:33:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJz0/Ln9rD95RXOOsTJ/m6gFT+31REy5ZcgOoRLYyB5QolkMrKUvvMJRJ3zN3u1gWz9oB8DZ X-Received: by 2002:a63:7d0e:: with SMTP id y14mr4211906pgc.356.1642800831185; Fri, 21 Jan 2022 13:33:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642800831; cv=none; d=google.com; s=arc-20160816; b=PsR0gLKOCjr3WDQ/8UvZpKnsU3N1C/3lUNKu6ocq4dNsFXd6bkj0kowze6mSA8T5fE u3cDRAB51O1Zh0+FmNwKwWHSrIZl9ADGj3YT01WL88p2MVA6qStjxbxUmxSQuveRnYWL miFxskNdrcQFQylOZBtcTvqLObBXecE2lp1eoT/2WyV6AWRewyNbVs7o00+yHCf/MZL7 WfeSXhkm8kzGK85HU8vVAeEfUIyxl/C1AkFVizT8OSuzPfBOwh9X9Hr9juOp94nb5Pss fylHGLQmeIAxI1a++d2dUxrOs6FPO2ZrwG2DdwT/AcJncFIKqgeerROcbd4Pib6ASVGh zQBg== 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 :message-id:date:subject:cc:to:from; bh=1tRpiI0MtLzKNVGPeVmZW1bswPk/vNrShN5EP/DJ9XM=; b=jtWVsQD/uhUQlfnqnSUXWZGgiDm0B7H/4o6gUIckP4jO79Nbq7Fns98BaIPYl51V2M Mwr8rRE/wE5NzTIup0wOjPa2fijBLk2liZdPjwnscsGuG4FUHCcK+JdYHoBtJI+lQfOl QtVRquDeO4q/R/TzLmhJzkVDzQgj/9JBqJqHW6TnJAF7hg9jmq34cNtEMSKd9vRvr36Y TKrL3Mm7io57fKctJdLch/oUuX7OeT8kvxAkMKR6kKrRT2x0qLOOkOy8wAwNrAcR4FiK 4vxttXvMxOxWbUUcwb1/3vjsB3PIDKwZxKc1Mya5fBO0AErHo8HIvxvXeKzFg3xbBNH4 NYFw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=puri.sm Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u5si7451018plg.310.2022.01.21.13.33.39; Fri, 21 Jan 2022 13:33:51 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=puri.sm Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230442AbiATLPt (ORCPT + 99 others); Thu, 20 Jan 2022 06:15:49 -0500 Received: from comms.puri.sm ([159.203.221.185]:45242 "EHLO comms.puri.sm" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230345AbiATLPb (ORCPT ); Thu, 20 Jan 2022 06:15:31 -0500 Received: from localhost (localhost [127.0.0.1]) by comms.puri.sm (Postfix) with ESMTP id B5734DF770; Thu, 20 Jan 2022 03:15:29 -0800 (PST) Received: from comms.puri.sm ([127.0.0.1]) by localhost (comms.puri.sm [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Bkfp2F8h1pmr; Thu, 20 Jan 2022 03:15:29 -0800 (PST) From: Martin Kepplinger To: martin.kepplinger@puri.sm, mchehab@kernel.org, broonie@kernel.org, sakari.ailus@linux.intel.com Cc: kernel@puri.sm, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linux-pm@vger.kernel.org, Angus Ainslie Subject: [PATCH v4] media: i2c: dw9714: add optional regulator support Date: Thu, 20 Jan 2022 12:14:53 +0100 Message-Id: <20220120111453.2244905-1-martin.kepplinger@puri.sm> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Angus Ainslie Allow the dw9714 to control a regulator and adjust suspend() and resume() to support both runtime and system pm. Signed-off-by: Angus Ainslie Signed-off-by: Martin Kepplinger --- v4: (thank you Sakari) * ensure suspend / power off in module remove() v3: (thank you Mark and Sakari) * use regulator_get() instead of regulator_get_optional() https://lore.kernel.org/linux-media/20211129120754.1766570-1-martin.kepplinger@puri.sm/ v2: (thank you Mark) * simplify the regulator_get_optional() error path * fix regulator usage during probe() https://lore.kernel.org/linux-media/20211126090107.1243558-1-martin.kepplinger@puri.sm/ v1: https://lore.kernel.org/linux-media/20211125080922.978583-1-martin.kepplinger@puri.sm/ drivers/media/i2c/dw9714.c | 42 +++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/drivers/media/i2c/dw9714.c b/drivers/media/i2c/dw9714.c index 3863dfeb8293..cd7008ad8f2f 100644 --- a/drivers/media/i2c/dw9714.c +++ b/drivers/media/i2c/dw9714.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,7 @@ struct dw9714_device { struct v4l2_ctrl_handler ctrls_vcm; struct v4l2_subdev sd; u16 current_val; + struct regulator *vcc; }; static inline struct dw9714_device *to_dw9714_vcm(struct v4l2_ctrl *ctrl) @@ -145,6 +147,16 @@ static int dw9714_probe(struct i2c_client *client) if (dw9714_dev == NULL) return -ENOMEM; + dw9714_dev->vcc = devm_regulator_get(&client->dev, "vcc"); + if (IS_ERR(dw9714_dev->vcc)) + return PTR_ERR(dw9714_dev->vcc); + + rval = regulator_enable(dw9714_dev->vcc); + if (rval < 0) { + dev_err(&client->dev, "failed to enable vcc: %d\n", rval); + return rval; + } + v4l2_i2c_subdev_init(&dw9714_dev->sd, client, &dw9714_ops); dw9714_dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; @@ -181,8 +193,18 @@ static int dw9714_remove(struct i2c_client *client) { struct v4l2_subdev *sd = i2c_get_clientdata(client); struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); + int ret; pm_runtime_disable(&client->dev); + if (!pm_runtime_status_suspended(&client->dev)) { + ret = regulator_disable(dw9714_dev->vcc); + if (ret) { + dev_err(&client->dev, + "Failed to disable vcc: %d\n", ret); + return ret; + } + } + pm_runtime_set_suspended(&client->dev); dw9714_subdev_cleanup(dw9714_dev); return 0; @@ -200,6 +222,9 @@ static int __maybe_unused dw9714_vcm_suspend(struct device *dev) struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); int ret, val; + if (pm_runtime_suspended(&client->dev)) + return 0; + for (val = dw9714_dev->current_val & ~(DW9714_CTRL_STEPS - 1); val >= 0; val -= DW9714_CTRL_STEPS) { ret = dw9714_i2c_write(client, @@ -208,7 +233,12 @@ static int __maybe_unused dw9714_vcm_suspend(struct device *dev) dev_err_once(dev, "%s I2C failure: %d", __func__, ret); usleep_range(DW9714_CTRL_DELAY_US, DW9714_CTRL_DELAY_US + 10); } - return 0; + + ret = regulator_disable(dw9714_dev->vcc); + if (ret) + dev_err(dev, "Failed to disable vcc: %d\n", ret); + + return ret; } /* @@ -224,6 +254,16 @@ static int __maybe_unused dw9714_vcm_resume(struct device *dev) struct dw9714_device *dw9714_dev = sd_to_dw9714_vcm(sd); int ret, val; + if (pm_runtime_suspended(&client->dev)) + return 0; + + ret = regulator_enable(dw9714_dev->vcc); + if (ret) { + dev_err(dev, "Failed to enable vcc: %d\n", ret); + return ret; + } + usleep_range(1000, 2000); + for (val = dw9714_dev->current_val % DW9714_CTRL_STEPS; val < dw9714_dev->current_val + DW9714_CTRL_STEPS - 1; val += DW9714_CTRL_STEPS) { -- 2.30.2