From patchwork Sun Apr 29 17:13:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10370619 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 6359260384 for ; Sun, 29 Apr 2018 17:14:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5361A28A18 for ; Sun, 29 Apr 2018 17:14:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 486BD28A25; Sun, 29 Apr 2018 17:14:05 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham 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 D151128A18 for ; Sun, 29 Apr 2018 17:14:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754036AbeD2ROC (ORCPT ); Sun, 29 Apr 2018 13:14:02 -0400 Received: from mail-pg0-f67.google.com ([74.125.83.67]:41733 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754033AbeD2RN7 (ORCPT ); Sun, 29 Apr 2018 13:13:59 -0400 Received: by mail-pg0-f67.google.com with SMTP id m21-v6so4797004pgv.8; Sun, 29 Apr 2018 10:13:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=W8kee6OeTqU1V8ytp25FQ3WA6kCVrYzmk4LJ/KIEkuI=; b=Xs+KpQ2RHNaeTReRpQoveHaif2KJ+Mkdou9EkQvvXPWVqxjDfU4cvJJj3HbHg0mdcw +NJwQJIHD0P4AhsyQ1F9pWCwN9DTW+rbURxIqbpUrQUi2lqBNyBsSD03Uw1LCvGOQG0u rU6TXm509fY8peaNfknOW/GkMbMxacD9fBE88GOlNuiAmES/Gjwb6EGZPbJ+1Bam9aaE hjAlbNDoNgAq4g1LdRWcpK0YyYorsL8szw5lw8Rd145uSDgCePfGD45aCRUUm62/AhxO 6PiXAZ4l/53kN81a11uIwCmZi4iVSsWRQWy2dKyUd+vTIFR3b/Ru4DgHCqpyWBzG8s4m eIbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=W8kee6OeTqU1V8ytp25FQ3WA6kCVrYzmk4LJ/KIEkuI=; b=T3KDX5Eotenf+jBSjrUGlPAlG2KmCKuxEmddrFzxIa4hl5UPINiXm2WrmZUsYXw30u 7LWGqZa8BuZ7kgTQwsnGP2T5HSLD4O98ao4SD1G3Su/43xfem7CDVZAV9/1woQdn2vkz XEAnDB2nR0UN1yHY+4Nn5E5sKWLDBF/U2rW0AIQFw2FU42cTv3YvknyVdR8+/TB3RJhV UkLNQvnSRDuQW37ylAELCLeRLtNngzOU28OZPKQhlbLbc9jXMsbbPlwLlOcQsPAwKIJk DtCSbM0Vt9cT9XYb5ZhQKAJIRdhzkBNrlAZOcYYzFBu+dhv67nVTdks0NayfAy3aR+Y/ 0DHA== X-Gm-Message-State: ALQs6tDGFQmPEK23a19x7PZ2dyHss7WiC8GUELbzy0qU9syB+8lmCpuH PrSrQYA5qNFHjT80/5Fqy561tg== X-Google-Smtp-Source: AB8JxZrUHhxlnQ/naHTcIXlmSkRf2L48dRHS3A5C2jv7UwiIazUCc69/wGt53+SwJ8Ird2vUpbZ7Aw== X-Received: by 2002:a17:902:8c91:: with SMTP id t17-v6mr9892953plo.182.1525022038457; Sun, 29 Apr 2018 10:13:58 -0700 (PDT) Received: from localhost.localdomain ([240f:4:c2bc:1:754e:5a21:c35f:52a1]) by smtp.gmail.com with ESMTPSA id e10sm10168022pfn.67.2018.04.29.10.13.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 29 Apr 2018 10:13:57 -0700 (PDT) From: Akinobu Mita To: linux-media@vger.kernel.org, devicetree@vger.kernel.org Cc: Akinobu Mita , Jacopo Mondi , Laurent Pinchart , Hans Verkuil , Sakari Ailus , Mauro Carvalho Chehab Subject: [PATCH v4 09/14] media: ov772x: handle nested s_power() calls Date: Mon, 30 Apr 2018 02:13:08 +0900 Message-Id: <1525021993-17789-10-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1525021993-17789-1-git-send-email-akinobu.mita@gmail.com> References: <1525021993-17789-1-git-send-email-akinobu.mita@gmail.com> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Depending on the v4l2 driver, calling s_power() could be nested. So the actual transitions between power saving mode and normal operation mode should only happen at the first power on and the last power off. This adds an s_power() nesting counter and updates the power state if the counter is modified from 0 to != 0 or from != 0 to 0. Cc: Jacopo Mondi Cc: Laurent Pinchart Cc: Hans Verkuil Cc: Sakari Ailus Cc: Mauro Carvalho Chehab Reviewed-by: Jacopo Mondi Signed-off-by: Akinobu Mita --- * v4 - Add Reviewed-by: line drivers/media/i2c/ov772x.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/drivers/media/i2c/ov772x.c b/drivers/media/i2c/ov772x.c index 621149a..edc013d 100644 --- a/drivers/media/i2c/ov772x.c +++ b/drivers/media/i2c/ov772x.c @@ -424,6 +424,9 @@ struct ov772x_priv { /* band_filter = COM8[5] ? 256 - BDBASE : 0 */ unsigned short band_filter; unsigned int fps; + /* lock to protect power_count */ + struct mutex lock; + int power_count; #ifdef CONFIG_MEDIA_CONTROLLER struct media_pad pad; #endif @@ -871,9 +874,26 @@ static int ov772x_power_off(struct ov772x_priv *priv) static int ov772x_s_power(struct v4l2_subdev *sd, int on) { struct ov772x_priv *priv = to_ov772x(sd); + int ret = 0; + + mutex_lock(&priv->lock); + + /* If the power count is modified from 0 to != 0 or from != 0 to 0, + * update the power state. + */ + if (priv->power_count == !on) + ret = on ? ov772x_power_on(priv) : ov772x_power_off(priv); + + if (!ret) { + /* Update the power count. */ + priv->power_count += on ? 1 : -1; + WARN(priv->power_count < 0, "Unbalanced power count\n"); + WARN(priv->power_count > 1, "Duplicated s_power call\n"); + } + + mutex_unlock(&priv->lock); - return on ? ov772x_power_on(priv) : - ov772x_power_off(priv); + return ret; } static const struct ov772x_win_size *ov772x_select_win(u32 width, u32 height) @@ -1303,6 +1323,7 @@ static int ov772x_probe(struct i2c_client *client, return -ENOMEM; priv->info = client->dev.platform_data; + mutex_init(&priv->lock); v4l2_i2c_subdev_init(&priv->subdev, client, &ov772x_subdev_ops); v4l2_ctrl_handler_init(&priv->hdl, 3); @@ -1313,8 +1334,10 @@ static int ov772x_probe(struct i2c_client *client, v4l2_ctrl_new_std(&priv->hdl, &ov772x_ctrl_ops, V4L2_CID_BAND_STOP_FILTER, 0, 256, 1, 0); priv->subdev.ctrl_handler = &priv->hdl; - if (priv->hdl.error) - return priv->hdl.error; + if (priv->hdl.error) { + ret = priv->hdl.error; + goto error_mutex_destroy; + } priv->clk = clk_get(&client->dev, NULL); if (IS_ERR(priv->clk)) { @@ -1362,6 +1385,8 @@ static int ov772x_probe(struct i2c_client *client, clk_put(priv->clk); error_ctrl_free: v4l2_ctrl_handler_free(&priv->hdl); +error_mutex_destroy: + mutex_destroy(&priv->lock); return ret; } @@ -1376,6 +1401,7 @@ static int ov772x_remove(struct i2c_client *client) gpiod_put(priv->pwdn_gpio); v4l2_async_unregister_subdev(&priv->subdev); v4l2_ctrl_handler_free(&priv->hdl); + mutex_destroy(&priv->lock); return 0; }