From patchwork Sun Apr 22 15:56:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10355629 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 221C160231 for ; Sun, 22 Apr 2018 15:57:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1137A2893D for ; Sun, 22 Apr 2018 15:57:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 060B32897A; Sun, 22 Apr 2018 15:57:04 +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=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, URIBL_RHS_DOB 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 920782893D for ; Sun, 22 Apr 2018 15:57:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754677AbeDVP5C (ORCPT ); Sun, 22 Apr 2018 11:57:02 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:39617 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754289AbeDVP46 (ORCPT ); Sun, 22 Apr 2018 11:56:58 -0400 Received: by mail-pf0-f194.google.com with SMTP id z9so7224417pfe.6; Sun, 22 Apr 2018 08:56:57 -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=sWde9dllD+xgt7FGpKkn0QPIIis+NM68hB6myh2PrBo=; b=ewLjHuD5NLNLNBOJmLqxSlifWQM1dKskA2pPrSGQdsRK/87i3adGdttdpiWnbtqsKv dqjyVel3205rEOLUuiOR5akhiB5nH/YaKJckYTKsFk/ZLMSNDKzlyFRY04/1q5ksp4iR /R6bQE9VgFWO5xBj+2s6eu4w2FKdj7NQTczhfUAL3aL/NpKRJQG+RL8RQ10tSjIbPSKg 2V6kVnsxGOaEP9uUSJ4jlHl16TFuq5wFsQutKfxJkghzC0EGkqUKsULDl7aFGNXY6A0O 91+uKwV/PDmbe/yLIU+sMLJ/5v66uGOLv8PtB7xWWXvrFOu2FyDz8O0WNW0qxu+M8Eg6 NSiQ== 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=sWde9dllD+xgt7FGpKkn0QPIIis+NM68hB6myh2PrBo=; b=BWGR3IMDaTt4WLtTv6Vpf25tOvmlJWJymn8ycx0kC8UrPC/1wAz3BlpCLagaWm8rlo vbpgekFP6mzCWljhCapiyPDv7tSkJ6O0sJP4hpSnw113+JYm9dpksKsrxnYoa5o6UsOE U+brC75ky8PL5MRyTK0QBvfajVQKN3/z+5XoGQuVMrKywBx0NMNIaP/ppfOH8idizQC7 1azmDhVZGoOzz6xovPl9oCvJ7432TScOBi++MPjD61YSGjY5JgJQpku0kAGid5QEOBkW EkYCRDxac9VcATAMsazKkvNAy88RhkDTSRo+Hmh5YMN/n/dUJsavRgeEojFwaURKop6k AV5g== X-Gm-Message-State: ALQs6tCQHqmAjvu1ju8W6tr/mgzA1Er9Ww5vRgksNAkbd8YMHnmUmqfm VGbckC8NfBn7TLn39517p8D05w== X-Google-Smtp-Source: AIpwx48u9KxUD3SX2KFh2dFp9KyeD6VrBJveUpqO67Cb+OgULb2B4EM73MErN5383YNBii2/CuZWtQ== X-Received: by 10.98.71.76 with SMTP id u73mr16944670pfa.4.1524412617448; Sun, 22 Apr 2018 08:56:57 -0700 (PDT) Received: from localhost.localdomain ([240f:4:c2bc:1:a9a1:96f3:29de:6e47]) by smtp.gmail.com with ESMTPSA id y29sm255461pff.42.2018.04.22.08.56.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 22 Apr 2018 08:56:56 -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 v3 07/11] media: ov772x: handle nested s_power() calls Date: Mon, 23 Apr 2018 00:56:13 +0900 Message-Id: <1524412577-14419-8-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1524412577-14419-1-git-send-email-akinobu.mita@gmail.com> References: <1524412577-14419-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 Signed-off-by: Akinobu Mita Reviewed-by: Jacopo Mondi --- * v3 - Rename mutex name from power_lock to lock - Add warning for duplicated s_power call 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 95c1c95d..8c0b850 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, "xclk"); 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; }