From patchwork Sun May 6 14:19:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10382821 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 D678860236 for ; Sun, 6 May 2018 14:20:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C47F62892B for ; Sun, 6 May 2018 14:20:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B801128BD9; Sun, 6 May 2018 14:20:29 +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 C9D1D2892B for ; Sun, 6 May 2018 14:20:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751811AbeEFOUN (ORCPT ); Sun, 6 May 2018 10:20:13 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:41128 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751777AbeEFOUL (ORCPT ); Sun, 6 May 2018 10:20:11 -0400 Received: by mail-pf0-f193.google.com with SMTP id v63so20814704pfk.8; Sun, 06 May 2018 07:20:11 -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=Da77aP5XXSaXgVty8ObOhlLc6h79W4edyfnbrjZwbPk=; b=NtCWVEu9+pucBG3V4+TmhjX5OCG2yluqdVmzcwbsI3UU/bHtEPnBN7Ms+qEhCsMWe5 FD2PRlPFNORdQiTFoR9swHKzul2+SB24h55Cjrz/frmG0WZGsuaL/pHY1GJy83YXKbYx TrSdmY8KxfdBA663s8eY1BM+teVd1Ct4mtKQOY3TuVaauC5u8yVA2fXNVKmv8cLOopZf ZKk5+05qBEdhY84o8qZ213iO3NyL8NH3k6GvdtbA7Z+zlFpELvI/Y+xo+W6bi/t+0cOl 88GqNDJqPCy00UASL3HVWjKDCekSZ7B2MyFBRFQgOxzs554h79t9ChYiG7GNJ/mO6PpQ BzXg== 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=Da77aP5XXSaXgVty8ObOhlLc6h79W4edyfnbrjZwbPk=; b=jO7BshK7fHgmwYW/wuQY8+bP2nUVfe4RbH6ZwzITQAWFM7qpkw8Ub/FvElUxt88cMa gKeToRhrHht0NEaEjOjudsoJTLrAbc979DlX7tTIru/qWJPcF2BuohYtyFTTAbNpa5Yf bNGUBgdfNrzPmZNHikX/2KxEeSxIh6UjH1+EBtnZuPuUCAN4frr96NISX8MSE/tPbQmE 6EGe/ZHVlWVWP20b6SGEyX2zPpX9IpmWfkgmd8sxTkYUfgspOs76uhGleHH/qFxeDFw4 /RbWzVEEqxXPqq3Bp+YQ8h8CvMvt4M+LLYxBs+GNR2vk/StFggPIjplfrLGsvYY4HAuN hbig== X-Gm-Message-State: ALQs6tBMN4tVQKm0J2NMpz0VuumFuFLf3wkRKLfviRF3tJLVfhnL/2cU sWQYrlAethyKewo23IwcggjEqg== X-Google-Smtp-Source: AB8JxZpchzhUUEwAmgx97Y/TN6wovFE5MaN6RS4YLa5P1oTfP7HsA848MCifejz9RNX4vhhJ1te0Rg== X-Received: by 2002:a17:902:9a04:: with SMTP id v4-v6mr34835104plp.21.1525616411256; Sun, 06 May 2018 07:20:11 -0700 (PDT) Received: from localhost.localdomain ([240f:4:c2bc:1:141a:15e1:5268:11ce]) by smtp.gmail.com with ESMTPSA id s88sm58766771pfe.43.2018.05.06.07.20.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 06 May 2018 07:20:10 -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 v5 09/14] media: ov772x: handle nested s_power() calls Date: Sun, 6 May 2018 23:19:24 +0900 Message-Id: <1525616369-8843-10-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1525616369-8843-1-git-send-email-akinobu.mita@gmail.com> References: <1525616369-8843-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 --- * v5 - No changes 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 2b02411..6c0c792 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; }