From patchwork Sat Oct 20 14:26:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10650543 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CFA1490 for ; Sat, 20 Oct 2018 14:26:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BEE7828622 for ; Sat, 20 Oct 2018 14:26:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B280028630; Sat, 20 Oct 2018 14:26:46 +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 5A54D28622 for ; Sat, 20 Oct 2018 14:26:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727413AbeJTWhY (ORCPT ); Sat, 20 Oct 2018 18:37:24 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:34209 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727373AbeJTWhY (ORCPT ); Sat, 20 Oct 2018 18:37:24 -0400 Received: by mail-pf1-f193.google.com with SMTP id f78-v6so12039027pfe.1 for ; Sat, 20 Oct 2018 07:26:44 -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=ed53biS0Z1WkaZmk97GMEJDJhEcCDxqfC9gtj4Eukkw=; b=UAkf5erO9j2M6Nm2//oLZ2xXvTZcxXsds+O/+OSeA3eD0xhWRAumCMarF3gPX3sm65 GhZQ0m62TFm61A7en7/lgzy9scosFPIF1saDIeKLk+M8WEoutCup4S6UFfTwZYC3b6z/ KIKkcA8ejbYc/U4g01Kg6NYekxqm93vyA6m6MCpkO/9rEmayijootHhbYGOBOr31XaTK kKR5z63kFhoRyOxylfP+SbhC6G+ODHEGmPsQnhJEOpstHhEJY7XeQwP3fdx7Rzr70ahO 7jN8IIaF5UCAHj+fxsvwCO0gbHWaBxCrfvp4Fs3NuTUPihVilBcgYxkAs2u6nqGdYc07 UR+Q== 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=ed53biS0Z1WkaZmk97GMEJDJhEcCDxqfC9gtj4Eukkw=; b=SndadUzn5PQDJhQlp5K9Zrd5QdbxM1HMqzsS24g2AsukreiWc+V0oJBf2FIsmi7WTh hBvqQB65d5wqtVON7P1FI416vsrBqy0zGfaLQdOHgXQSW4JSgzH65IjcnlMTLE1iWxw5 PHM84EceewG0noZAzjTK/CPB2r3TKlc2imMR2cu6NYEj9pxwul92M12u8AALGO+I9jaL k19SZo7FyfRXU2Ze6DhvFTJPR2awYO2T9NECG6LHycNlxUrjfk7DEb2xXI5YGfRS/J/Q 74QklyeUGHVyOpnJkuthbW/eweXzJqSrGJ+eGWGX/SKIgzYKsYMYhIdy+Blql8PV5qSn B7gw== X-Gm-Message-State: ABuFfohh+HfRZbUuw23ZCoyeZuCt49s1v47egx7uG5gNzBDWKX+tmlAc 6q4+MHNIKb5WUaiGY0Qk1wLjEZOD X-Google-Smtp-Source: ACcGV60CjOSxFjmxZdE9rzXhjje+AvvMCI7kVKkHz3Hmvc4Xna0y/ZRfv0kJbi1z9yAE8IN6eKESOw== X-Received: by 2002:a62:178f:: with SMTP id 137-v6mr37725488pfx.215.1540045604080; Sat, 20 Oct 2018 07:26:44 -0700 (PDT) Received: from mita-MS-7A45.lan ([240f:34:212d:1:28d8:ef69:4040:67dc]) by smtp.gmail.com with ESMTPSA id q68-v6sm28624799pfb.160.2018.10.20.07.26.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 20 Oct 2018 07:26:43 -0700 (PDT) From: Akinobu Mita To: linux-media@vger.kernel.org Cc: Akinobu Mita , Matt Ranostay , Sakari Ailus , Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH v4 1/6] media: video-i2c: avoid accessing released memory area when removing driver Date: Sat, 20 Oct 2018 23:26:23 +0900 Message-Id: <1540045588-9091-2-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1540045588-9091-1-git-send-email-akinobu.mita@gmail.com> References: <1540045588-9091-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 The video device release() callback for video-i2c driver frees the whole struct video_i2c_data. If there is no user left for the video device when video_unregister_device() is called, the release callback is executed. However, in video_i2c_remove() some fields (v4l2_dev, lock, and queue_lock) in struct video_i2c_data are still accessed after video_unregister_device() is called. This fixes the use after free by moving the code from video_i2c_remove() to the release() callback. Fixes: 5cebaac60974 ("media: video-i2c: add video-i2c driver") Cc: Matt Ranostay Cc: Sakari Ailus Cc: Hans Verkuil Cc: Mauro Carvalho Chehab Reviewed-by: Matt Ranostay Signed-off-by: Akinobu Mita Acked-by: Sakari Ailus --- * v4 - Add Reviewed-by line drivers/media/i2c/video-i2c.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/media/i2c/video-i2c.c b/drivers/media/i2c/video-i2c.c index 06d29d8..f27d294 100644 --- a/drivers/media/i2c/video-i2c.c +++ b/drivers/media/i2c/video-i2c.c @@ -510,7 +510,12 @@ static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = { static void video_i2c_release(struct video_device *vdev) { - kfree(video_get_drvdata(vdev)); + struct video_i2c_data *data = video_get_drvdata(vdev); + + v4l2_device_unregister(&data->v4l2_dev); + mutex_destroy(&data->lock); + mutex_destroy(&data->queue_lock); + kfree(data); } static int video_i2c_probe(struct i2c_client *client, @@ -608,10 +613,6 @@ static int video_i2c_remove(struct i2c_client *client) struct video_i2c_data *data = i2c_get_clientdata(client); video_unregister_device(&data->vdev); - v4l2_device_unregister(&data->v4l2_dev); - - mutex_destroy(&data->lock); - mutex_destroy(&data->queue_lock); return 0; } From patchwork Sat Oct 20 14:26: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: 10650545 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BAC0317EF for ; Sat, 20 Oct 2018 14:26:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AA2432862C for ; Sat, 20 Oct 2018 14:26:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9CFE928622; Sat, 20 Oct 2018 14:26:49 +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 7B4B428622 for ; Sat, 20 Oct 2018 14:26:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727449AbeJTWh0 (ORCPT ); Sat, 20 Oct 2018 18:37:26 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:43341 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727373AbeJTWh0 (ORCPT ); Sat, 20 Oct 2018 18:37:26 -0400 Received: by mail-pl1-f194.google.com with SMTP id 30-v6so17102067plb.10 for ; Sat, 20 Oct 2018 07:26:47 -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=o+XKrUS+r4XIO2dmBUj37+gk1EabrnL57CS+GLzB7lU=; b=lctNV80lCqdANjtHE0TLESsPnj9MkrYVpSynPk4s+OSj/0qQWJc23FATlihb4Z23mY e4ux96i3PI/Kw4XK3zyUp0gfM3B82Kl4FGV+nhD1cNUvo8tT9Krr1PKFTuEM+0TKtiTX axOLytmS8U5aslBvDVu1mM/fotSHeMuqW9ZWnoKs67nil2zTQ2yu64lsp3mw6TJHDp5a 6RbAUH+PWp2xBZrc7BnykmeZCGt6988icblXO6Rd7uUZd9ERlsAXbcVafGWXrYCYvwTO KZwKS6uf4mloHQevxcrpLrBXU64NDM6wPEqlqY6nh1NoU9XGI32xqH7WwQVGS3laUe1r 6BvA== 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=o+XKrUS+r4XIO2dmBUj37+gk1EabrnL57CS+GLzB7lU=; b=Ch7vaEdVvNHLq3MGUr2iutbqK98K0OnmlIrRxgggn5Z82CCeZqubwZ1KdLmMzdWj4S ZClWtdpSY1rEMOKD1aE41V6FcQnFFab0jwxp2dFTKFyZf8Dwl5xvBTg2m9zjkapj+sgj 52laLB3AnYvqFL8ycHz50FVz1apufuSemXaMPdjpgsKw0tUPZDFwlvdnzPAVGNhU8Hf8 ke5Qsg21frw/Fn9kgfQSI39taKSIoFGCWcWLj9gQU5saFfwJwMClrJQftmAEdcvv9y4E PwuBHL2q3hpFGl1QjtkiEAIL+4fdof+MxtgkfnQs/nLtg/Mrr14MBPsF7EwjWfKyeHAh yyzg== X-Gm-Message-State: AGRZ1gK6aTrHPVo0sGqoxilD7g9cGN0orpLvZA/0m/Z8EFYMyvl+V9km odztPp6UUuhB8DK9h4a0ya6NzrdN X-Google-Smtp-Source: AJdET5dsQ5r9AMNTTB61ND0q48+XAQHi8MzPuXNEHTaQhiUj6CZ1zoDkTusxYIqKm+fXMM6M0jRrKQ== X-Received: by 2002:a17:902:9a44:: with SMTP id x4-v6mr830245plv.121.1540045606636; Sat, 20 Oct 2018 07:26:46 -0700 (PDT) Received: from mita-MS-7A45.lan ([240f:34:212d:1:28d8:ef69:4040:67dc]) by smtp.gmail.com with ESMTPSA id q68-v6sm28624799pfb.160.2018.10.20.07.26.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 20 Oct 2018 07:26:45 -0700 (PDT) From: Akinobu Mita To: linux-media@vger.kernel.org Cc: Akinobu Mita , Matt Ranostay , Sakari Ailus , Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH v4 2/6] media: video-i2c: use i2c regmap Date: Sat, 20 Oct 2018 23:26:24 +0900 Message-Id: <1540045588-9091-3-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1540045588-9091-1-git-send-email-akinobu.mita@gmail.com> References: <1540045588-9091-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 Use regmap for i2c register access. This simplifies register accesses and chooses suitable access commands based on the functionality that the adapter supports. Cc: Matt Ranostay Cc: Sakari Ailus Cc: Hans Verkuil Cc: Mauro Carvalho Chehab Acked-by: Matt Ranostay Acked-by: Sakari Ailus Signed-off-by: Akinobu Mita --- * v4 - No changes from v3 drivers/media/i2c/video-i2c.c | 68 ++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/drivers/media/i2c/video-i2c.c b/drivers/media/i2c/video-i2c.c index f27d294..f23cb91 100644 --- a/drivers/media/i2c/video-i2c.c +++ b/drivers/media/i2c/video-i2c.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -38,7 +39,7 @@ struct video_i2c_buffer { }; struct video_i2c_data { - struct i2c_client *client; + struct regmap *regmap; const struct video_i2c_chip *chip; struct mutex lock; spinlock_t slock; @@ -62,6 +63,12 @@ static const struct v4l2_frmsize_discrete amg88xx_size = { .height = 8, }; +static const struct regmap_config amg88xx_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = 0xff +}; + struct video_i2c_chip { /* video dimensions */ const struct v4l2_fmtdesc *format; @@ -76,6 +83,8 @@ struct video_i2c_chip { /* pixel size in bits */ unsigned int bpp; + const struct regmap_config *regmap_config; + /* xfer function */ int (*xfer)(struct video_i2c_data *data, char *buf); @@ -83,26 +92,16 @@ struct video_i2c_chip { int (*hwmon_init)(struct video_i2c_data *data); }; -static int amg88xx_xfer(struct video_i2c_data *data, char *buf) -{ - struct i2c_client *client = data->client; - struct i2c_msg msg[2]; - u8 reg = 0x80; - int ret; - - msg[0].addr = client->addr; - msg[0].flags = 0; - msg[0].len = 1; - msg[0].buf = (char *)® +/* Thermistor register */ +#define AMG88XX_REG_TTHL 0x0e - msg[1].addr = client->addr; - msg[1].flags = I2C_M_RD; - msg[1].len = data->chip->buffer_size; - msg[1].buf = (char *)buf; +/* Temperature register */ +#define AMG88XX_REG_T01L 0x80 - ret = i2c_transfer(client->adapter, msg, 2); - - return (ret == 2) ? 0 : -EIO; +static int amg88xx_xfer(struct video_i2c_data *data, char *buf) +{ + return regmap_bulk_read(data->regmap, AMG88XX_REG_T01L, buf, + data->chip->buffer_size); } #if IS_ENABLED(CONFIG_HWMON) @@ -133,12 +132,15 @@ static int amg88xx_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { struct video_i2c_data *data = dev_get_drvdata(dev); - struct i2c_client *client = data->client; - int tmp = i2c_smbus_read_word_data(client, 0x0e); + __le16 buf; + int tmp; - if (tmp < 0) + tmp = regmap_bulk_read(data->regmap, AMG88XX_REG_TTHL, &buf, 2); + if (tmp) return tmp; + tmp = le16_to_cpu(buf); + /* * Check for sign bit, this isn't a two's complement value but an * absolute temperature that needs to be inverted in the case of being @@ -164,8 +166,9 @@ static const struct hwmon_chip_info amg88xx_chip_info = { static int amg88xx_hwmon_init(struct video_i2c_data *data) { - void *hwmon = devm_hwmon_device_register_with_info(&data->client->dev, - "amg88xx", data, &amg88xx_chip_info, NULL); + struct device *dev = regmap_get_device(data->regmap); + void *hwmon = devm_hwmon_device_register_with_info(dev, "amg88xx", data, + &amg88xx_chip_info, NULL); return PTR_ERR_OR_ZERO(hwmon); } @@ -182,6 +185,7 @@ static const struct video_i2c_chip video_i2c_chip[] = { .max_fps = 10, .buffer_size = 128, .bpp = 16, + .regmap_config = &amg88xx_regmap_config, .xfer = &amg88xx_xfer, .hwmon_init = amg88xx_hwmon_init, }, @@ -350,7 +354,8 @@ static int video_i2c_querycap(struct file *file, void *priv, struct v4l2_capability *vcap) { struct video_i2c_data *data = video_drvdata(file); - struct i2c_client *client = data->client; + struct device *dev = regmap_get_device(data->regmap); + struct i2c_client *client = to_i2c_client(dev); strlcpy(vcap->driver, data->v4l2_dev.name, sizeof(vcap->driver)); strlcpy(vcap->card, data->vdev.name, sizeof(vcap->card)); @@ -515,6 +520,7 @@ static void video_i2c_release(struct video_device *vdev) v4l2_device_unregister(&data->v4l2_dev); mutex_destroy(&data->lock); mutex_destroy(&data->queue_lock); + regmap_exit(data->regmap); kfree(data); } @@ -537,13 +543,18 @@ static int video_i2c_probe(struct i2c_client *client, else goto error_free_device; - data->client = client; + data->regmap = regmap_init_i2c(client, data->chip->regmap_config); + if (IS_ERR(data->regmap)) { + ret = PTR_ERR(data->regmap); + goto error_free_device; + } + v4l2_dev = &data->v4l2_dev; strlcpy(v4l2_dev->name, VIDEO_I2C_DRIVER, sizeof(v4l2_dev->name)); ret = v4l2_device_register(&client->dev, v4l2_dev); if (ret < 0) - goto error_free_device; + goto error_regmap_exit; mutex_init(&data->lock); mutex_init(&data->queue_lock); @@ -602,6 +613,9 @@ static int video_i2c_probe(struct i2c_client *client, mutex_destroy(&data->lock); mutex_destroy(&data->queue_lock); +error_regmap_exit: + regmap_exit(data->regmap); + error_free_device: kfree(data); From patchwork Sat Oct 20 14:26:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10650547 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 368DD17EF for ; Sat, 20 Oct 2018 14:26:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 25CB328622 for ; Sat, 20 Oct 2018 14:26:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1769628632; Sat, 20 Oct 2018 14:26:51 +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 AF42628622 for ; Sat, 20 Oct 2018 14:26:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727453AbeJTWh3 (ORCPT ); Sat, 20 Oct 2018 18:37:29 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:46003 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727373AbeJTWh2 (ORCPT ); Sat, 20 Oct 2018 18:37:28 -0400 Received: by mail-pf1-f193.google.com with SMTP id u12-v6so17783133pfn.12 for ; Sat, 20 Oct 2018 07:26:49 -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=Yw+6seVTs2UOoFuM8wOlZYuvyboxKHwd+Nb+BLXfwIY=; b=s9nl4H/1rDN7xsO3L3Oi0BPhdzavFl+AYfNNevEGgKpLLHK8AjfIezDQtU77gYn3sn 8elfMfYMWKe3kyYHkwBEwZPU2QDu/lb4iaPie4He9LaMTykg70J9AldUweH3JLGnDpcp IFZidZuZCXoRr9n7OV1h9y9uujMYIL4v2zKi7PW24MkuqIBW2Why+AfBUNRROFbtYcCn W8CyC+Jtd0gMkhVtqqoyOnq7gBQzFktOHS/SZBFWnVOsntP2z+LpBVgVQOP/jz2Jbr6H DFKDR2EJLP6+XMl0cPTliDe8PuVJB+Gjj3OZnByk0Z+T253JljCmF7Aov41QhalinjGF JjTw== 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=Yw+6seVTs2UOoFuM8wOlZYuvyboxKHwd+Nb+BLXfwIY=; b=eB6qEz2C1I8WFeHpAvmX+kTZaiLEpDUtt7lp/YlIfzk50kyzbo/zT4yz3oYxS411IP DTnDCyKwfa2Prs1Yox/b0xyCyI+YtKEeLqh6S/XaVBl5xQoEyQjBvG1u5mS45uUcp5A/ kR+V4OOghU0yuNZaS3zZh6RTuawuGTTSaDNhzfjDgFtm8oGzHEmTqEsz1apW/bw7stDv u3MTDrI649SYx6vipMy2MrRP5IXJTJzfXF5uEcTXIW65mPAGKUu/JvyVa991jW6DJm/P cPge4fV843KnXmSR01i+PkRWM+wCtI2yVkLKRy98OWWxLp9j8ahfHm7EPqPrLTbRxoD7 BgQg== X-Gm-Message-State: ABuFfojuwm+AELOL8LqWA6OhO6ChvhXyiGZq+LEG3CjK8jqHCTECl714 shfU8KwPcp8YjX+rnqm0Nz+UHC4p X-Google-Smtp-Source: ACcGV62+RQJFjlHl4Pqm2+Uzkn9Glo+lyr9eE2+Nc9ed9VZ9LdXSnVjbG6frXlw0iGt7IIT+ZXV2ew== X-Received: by 2002:a62:4681:: with SMTP id o1-v6mr38496615pfi.108.1540045609055; Sat, 20 Oct 2018 07:26:49 -0700 (PDT) Received: from mita-MS-7A45.lan ([240f:34:212d:1:28d8:ef69:4040:67dc]) by smtp.gmail.com with ESMTPSA id q68-v6sm28624799pfb.160.2018.10.20.07.26.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 20 Oct 2018 07:26:48 -0700 (PDT) From: Akinobu Mita To: linux-media@vger.kernel.org Cc: Akinobu Mita , Matt Ranostay , Sakari Ailus , Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH v4 3/6] media: v4l2-common: add V4L2_FRACT_COMPARE Date: Sat, 20 Oct 2018 23:26:25 +0900 Message-Id: <1540045588-9091-4-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1540045588-9091-1-git-send-email-akinobu.mita@gmail.com> References: <1540045588-9091-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 Add macro to compare two v4l2_fract values in v4l2 common internal API. The same macro FRACT_CMP() is used by vivid and bcm2835-camera. This just renames it to V4L2_FRACT_COMPARE in order to avoid namespace collision. Cc: Matt Ranostay Cc: Sakari Ailus Cc: Hans Verkuil Cc: Mauro Carvalho Chehab Acked-by: Sakari Ailus Signed-off-by: Akinobu Mita --- * v4 - No changes from v3 include/media/v4l2-common.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h index cdc87ec..eafb8a3 100644 --- a/include/media/v4l2-common.h +++ b/include/media/v4l2-common.h @@ -384,4 +384,9 @@ int v4l2_g_parm_cap(struct video_device *vdev, int v4l2_s_parm_cap(struct video_device *vdev, struct v4l2_subdev *sd, struct v4l2_streamparm *a); +/* Compare two v4l2_fract structs */ +#define V4L2_FRACT_COMPARE(a, OP, b) \ + ((u64)(a).numerator * (b).denominator OP \ + (u64)(b).numerator * (a).denominator) + #endif /* V4L2_COMMON_H_ */ From patchwork Sat Oct 20 14:26:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10650549 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0649590 for ; Sat, 20 Oct 2018 14:26:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DEAB72862C for ; Sat, 20 Oct 2018 14:26:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D359528632; Sat, 20 Oct 2018 14:26:53 +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 7D98A2862C for ; Sat, 20 Oct 2018 14:26:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727470AbeJTWhc (ORCPT ); Sat, 20 Oct 2018 18:37:32 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:44148 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727373AbeJTWhb (ORCPT ); Sat, 20 Oct 2018 18:37:31 -0400 Received: by mail-pg1-f195.google.com with SMTP id w3-v6so1475308pgs.11 for ; Sat, 20 Oct 2018 07:26:52 -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=OmqeoU0XuCmw5YovbS2vtReQ4AfstqagcRGgJk+aRVQ=; b=EWUuxYWihwNq1bFfZYjv7ez1XgX57rHm9NEyEF21ocrDo7tV0CyUd7R25UYwyMmbcl iFv60MkNeDI4MYeHWsQfDrB1+IsBC1oj/KM6toy+DXp0dOE845ic8cMr6Jd1kkdOPfvj U3TlADgCgPoaj4RN1km4UKrK5OYmu0dnrRG6RrCkGBOtSbd77fOqP6TaJJh664Du7agn mNF/SBwZeyqVbIKkqj+RXHMsuuBRP6Qo8Op79NwDhEmQaCKPOS3VkxCWTI79BGAUMgvV sJKlab8WNcu38eoTnUnKh1nk/1Ul9D4daNl0YzFFlbQ232gRZfTEQ9RuNgvSsg6zLCCP jt6g== 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=OmqeoU0XuCmw5YovbS2vtReQ4AfstqagcRGgJk+aRVQ=; b=qTrd9R4xNUNIq85OjkzpvSWuJZTdV4HYbg1NyoNfAD+HjQrZUQe0o0xRTaMGQUdAIB ppvpX6afuwCy5E52qU4XxZZE5aKVqMdoIvFGwoabSBLcjFsX0wf5a9lICN3Ld+1uVXGM 47S4WspFTenAXQv60JRFi8MWmovzbi4GJls+hor+QzF9eXASkYdLugx09e7jda9kr2uz RZwzUyRjONreUWNeBb7lFn2aiDQo1LiQYbmiEU7AmL2rGFMesnCq8WYl4XEd/e2iudvT YexFPg0Eafa3AEkMzwfdBcqZuTCpHdygGZGDnY3L2nfoLRcgsdT4MhKMAUzcLNpsebxz lx3w== X-Gm-Message-State: ABuFfoh7dfBIGNzK9j2P5J9i84VTDyqVSNZqmBlY5+9tsOq6B5P3AEdy QMMIFQ2UgdWwp5tYVRuGEGA5WmQT X-Google-Smtp-Source: ACcGV62L+1Hr3bsYJJoocV+kgwPzx21HXQRYwbt3mjJCP0nzT6Z4Swd/oh3rpbsLpR29nL6hPJPegw== X-Received: by 2002:a63:2447:: with SMTP id k68-v6mr7135948pgk.156.1540045611542; Sat, 20 Oct 2018 07:26:51 -0700 (PDT) Received: from mita-MS-7A45.lan ([240f:34:212d:1:28d8:ef69:4040:67dc]) by smtp.gmail.com with ESMTPSA id q68-v6sm28624799pfb.160.2018.10.20.07.26.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 20 Oct 2018 07:26:50 -0700 (PDT) From: Akinobu Mita To: linux-media@vger.kernel.org Cc: Akinobu Mita , Matt Ranostay , Sakari Ailus , Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH v4 4/6] media: vivid: use V4L2_FRACT_COMPARE Date: Sat, 20 Oct 2018 23:26:26 +0900 Message-Id: <1540045588-9091-5-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1540045588-9091-1-git-send-email-akinobu.mita@gmail.com> References: <1540045588-9091-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 Now the equivalent of FRACT_CMP() is added in v4l2 common internal API header. Cc: Matt Ranostay Cc: Sakari Ailus Cc: Hans Verkuil Cc: Mauro Carvalho Chehab Acked-by: Sakari Ailus Signed-off-by: Akinobu Mita --- * v4 - No changes from v3 drivers/media/platform/vivid/vivid-vid-cap.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/drivers/media/platform/vivid/vivid-vid-cap.c b/drivers/media/platform/vivid/vivid-vid-cap.c index 1599159..f19c701 100644 --- a/drivers/media/platform/vivid/vivid-vid-cap.c +++ b/drivers/media/platform/vivid/vivid-vid-cap.c @@ -1824,9 +1824,6 @@ int vivid_vid_cap_g_parm(struct file *file, void *priv, return 0; } -#define FRACT_CMP(a, OP, b) \ - ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator) - int vivid_vid_cap_s_parm(struct file *file, void *priv, struct v4l2_streamparm *parm) { @@ -1847,14 +1844,14 @@ int vivid_vid_cap_s_parm(struct file *file, void *priv, if (tpf.denominator == 0) tpf = webcam_intervals[ival_sz - 1]; for (i = 0; i < ival_sz; i++) - if (FRACT_CMP(tpf, >=, webcam_intervals[i])) + if (V4L2_FRACT_COMPARE(tpf, >=, webcam_intervals[i])) break; if (i == ival_sz) i = ival_sz - 1; dev->webcam_ival_idx = i; tpf = webcam_intervals[dev->webcam_ival_idx]; - tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf; - tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf; + tpf = V4L2_FRACT_COMPARE(tpf, <, tpf_min) ? tpf_min : tpf; + tpf = V4L2_FRACT_COMPARE(tpf, >, tpf_max) ? tpf_max : tpf; /* resync the thread's timings */ dev->cap_seq_resync = true; From patchwork Sat Oct 20 14:26:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10650551 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 16C0617EF for ; Sat, 20 Oct 2018 14:26:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 02EED28622 for ; Sat, 20 Oct 2018 14:26:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E88D428630; Sat, 20 Oct 2018 14:26:56 +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 4A4D228622 for ; Sat, 20 Oct 2018 14:26:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727512AbeJTWhe (ORCPT ); Sat, 20 Oct 2018 18:37:34 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:32808 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727373AbeJTWhe (ORCPT ); Sat, 20 Oct 2018 18:37:34 -0400 Received: by mail-pf1-f194.google.com with SMTP id 78-v6so15424891pfq.0 for ; Sat, 20 Oct 2018 07:26:54 -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=HZwVvC2i6b50w0GgXukABc35ksKNc7wXmTxXmgnFQhc=; b=ktWUviAoNsc0btAeL8fNJKTX6U2DSVU4OxDDSrevqcTfm6nSZVpKwpI0Ue595ksBcz mh9AjbaWSqx3MhmC1DRMtOdzU8DqQ7Zv3eWwx+/bTS8Nr6zwsdDAwUPs4fjX1xX0b+HU FmX0l3ONfvq9Ii+rXR6o3YmoefSVgVNMIT2IU0bT8nSliK1xo5EfGT4URZvLsv9nTWHW 0ZdaItwq2O9bDN0nbS/gft0naaoDaMge5tgBC5SpCU/ISVeV41jOilzgv91Zg/W5x43q 2ZUipFAdRzqL0o0lAU19SrkbOys5dUAOpVjS9lFcQxYgiiH0v6TdK0rDCsD3Q13G6PJ4 SgHA== 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=HZwVvC2i6b50w0GgXukABc35ksKNc7wXmTxXmgnFQhc=; b=AePagMmVMhw5W2681TIgDap2iJPkHYa357wOABw2cyBbU0sb2n0GWSI7CWB115aVdA k3mVq1aIemaGBsiDV+eyut8sOcO9eOy28EtuNuc8NlZ+MCk7Ygs/rbC3Rw6Ccpn6LRGt zlVTWW80lHig1WL8DF+BHf5FZVgJypsr+ciySdn3ZxTDjOJGC8+j2DbTTFY/RaI0C2nv 0RltQdwmXdpyZ4UFx+KYb5YH+ZMQNb07HpWoM3iL6GguIVsjUjohn/1BOPYr33H2oG0f YCDPMVuOUYSpbtv5p4ZA73gu0dN1/Z/mzHtuzvTtsAr1c+ovSmtoM9E6toBq5IW2vmfN MJKg== X-Gm-Message-State: ABuFfojmjtRJnagJk8kAGsiej4kkWvxbqTD4M8powFmIuKqYFsZA+pgi TXcj/c/vvZfQSsWkT7OEoKvD+RL4 X-Google-Smtp-Source: ACcGV61vjQD02beE+4IDicOxEmC0QY0oP3fiBlrfjOuSCNcZBUcqryKGxH2CAroNAYicRGtQa2R95g== X-Received: by 2002:a63:9612:: with SMTP id c18-v6mr35348483pge.21.1540045614123; Sat, 20 Oct 2018 07:26:54 -0700 (PDT) Received: from mita-MS-7A45.lan ([240f:34:212d:1:28d8:ef69:4040:67dc]) by smtp.gmail.com with ESMTPSA id q68-v6sm28624799pfb.160.2018.10.20.07.26.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 20 Oct 2018 07:26:53 -0700 (PDT) From: Akinobu Mita To: linux-media@vger.kernel.org Cc: Akinobu Mita , Matt Ranostay , Sakari Ailus , Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH v4 5/6] media: video-i2c: support changing frame interval Date: Sat, 20 Oct 2018 23:26:27 +0900 Message-Id: <1540045588-9091-6-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1540045588-9091-1-git-send-email-akinobu.mita@gmail.com> References: <1540045588-9091-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 AMG88xx has a register for setting frame rate 1 or 10 FPS. This adds support changing frame interval. Reference specifications: https://docid81hrs3j1.cloudfront.net/medialibrary/2017/11/PANA-S-A0002141979-1.pdf Cc: Matt Ranostay Cc: Sakari Ailus Cc: Hans Verkuil Cc: Mauro Carvalho Chehab Acked-by: Matt Ranostay Acked-by: Sakari Ailus Signed-off-by: Akinobu Mita --- * v4 - No changes from v3 drivers/media/i2c/video-i2c.c | 78 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/drivers/media/i2c/video-i2c.c b/drivers/media/i2c/video-i2c.c index f23cb91..3334fc2 100644 --- a/drivers/media/i2c/video-i2c.c +++ b/drivers/media/i2c/video-i2c.c @@ -52,6 +52,8 @@ struct video_i2c_data { struct task_struct *kthread_vid_cap; struct list_head vid_cap_active; + + struct v4l2_fract frame_interval; }; static const struct v4l2_fmtdesc amg88xx_format = { @@ -74,8 +76,9 @@ struct video_i2c_chip { const struct v4l2_fmtdesc *format; const struct v4l2_frmsize_discrete *size; - /* max frames per second */ - unsigned int max_fps; + /* available frame intervals */ + const struct v4l2_fract *frame_intervals; + unsigned int num_frame_intervals; /* pixel buffer size */ unsigned int buffer_size; @@ -85,6 +88,9 @@ struct video_i2c_chip { const struct regmap_config *regmap_config; + /* setup function */ + int (*setup)(struct video_i2c_data *data); + /* xfer function */ int (*xfer)(struct video_i2c_data *data, char *buf); @@ -92,6 +98,10 @@ struct video_i2c_chip { int (*hwmon_init)(struct video_i2c_data *data); }; +/* Frame rate register */ +#define AMG88XX_REG_FPSC 0x02 +#define AMG88XX_FPSC_1FPS BIT(0) + /* Thermistor register */ #define AMG88XX_REG_TTHL 0x0e @@ -104,6 +114,19 @@ static int amg88xx_xfer(struct video_i2c_data *data, char *buf) data->chip->buffer_size); } +static int amg88xx_setup(struct video_i2c_data *data) +{ + unsigned int mask = AMG88XX_FPSC_1FPS; + unsigned int val; + + if (data->frame_interval.numerator == data->frame_interval.denominator) + val = mask; + else + val = 0; + + return regmap_update_bits(data->regmap, AMG88XX_REG_FPSC, mask, val); +} + #if IS_ENABLED(CONFIG_HWMON) static const u32 amg88xx_temp_config[] = { @@ -178,14 +201,21 @@ static int amg88xx_hwmon_init(struct video_i2c_data *data) #define AMG88XX 0 +static const struct v4l2_fract amg88xx_frame_intervals[] = { + { 1, 10 }, + { 1, 1 }, +}; + static const struct video_i2c_chip video_i2c_chip[] = { [AMG88XX] = { .size = &amg88xx_size, .format = &amg88xx_format, - .max_fps = 10, + .frame_intervals = amg88xx_frame_intervals, + .num_frame_intervals = ARRAY_SIZE(amg88xx_frame_intervals), .buffer_size = 128, .bpp = 16, .regmap_config = &amg88xx_regmap_config, + .setup = &amg88xx_setup, .xfer = &amg88xx_xfer, .hwmon_init = amg88xx_hwmon_init, }, @@ -250,7 +280,8 @@ static void buffer_queue(struct vb2_buffer *vb) static int video_i2c_thread_vid_cap(void *priv) { struct video_i2c_data *data = priv; - unsigned int delay = msecs_to_jiffies(1000 / data->chip->max_fps); + unsigned int delay = mult_frac(HZ, data->frame_interval.numerator, + data->frame_interval.denominator); set_freezable(); @@ -312,19 +343,26 @@ static void video_i2c_del_list(struct vb2_queue *vq, enum vb2_buffer_state state static int start_streaming(struct vb2_queue *vq, unsigned int count) { struct video_i2c_data *data = vb2_get_drv_priv(vq); + int ret; if (data->kthread_vid_cap) return 0; + ret = data->chip->setup(data); + if (ret) + goto error_del_list; + data->sequence = 0; data->kthread_vid_cap = kthread_run(video_i2c_thread_vid_cap, data, "%s-vid-cap", data->v4l2_dev.name); - if (!IS_ERR(data->kthread_vid_cap)) + ret = PTR_ERR_OR_ZERO(data->kthread_vid_cap); + if (!ret) return 0; +error_del_list: video_i2c_del_list(vq, VB2_BUF_STATE_QUEUED); - return PTR_ERR(data->kthread_vid_cap); + return ret; } static void stop_streaming(struct vb2_queue *vq) @@ -431,15 +469,14 @@ static int video_i2c_enum_frameintervals(struct file *file, void *priv, const struct video_i2c_data *data = video_drvdata(file); const struct v4l2_frmsize_discrete *size = data->chip->size; - if (fe->index > 0) + if (fe->index >= data->chip->num_frame_intervals) return -EINVAL; if (fe->width != size->width || fe->height != size->height) return -EINVAL; fe->type = V4L2_FRMIVAL_TYPE_DISCRETE; - fe->discrete.numerator = 1; - fe->discrete.denominator = data->chip->max_fps; + fe->discrete = data->chip->frame_intervals[fe->index]; return 0; } @@ -484,12 +521,27 @@ static int video_i2c_g_parm(struct file *filp, void *priv, parm->parm.capture.readbuffers = 1; parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; - parm->parm.capture.timeperframe.numerator = 1; - parm->parm.capture.timeperframe.denominator = data->chip->max_fps; + parm->parm.capture.timeperframe = data->frame_interval; return 0; } +static int video_i2c_s_parm(struct file *filp, void *priv, + struct v4l2_streamparm *parm) +{ + struct video_i2c_data *data = video_drvdata(filp); + int i; + + for (i = 0; i < data->chip->num_frame_intervals - 1; i++) { + if (V4L2_FRACT_COMPARE(parm->parm.capture.timeperframe, <=, + data->chip->frame_intervals[i])) + break; + } + data->frame_interval = data->chip->frame_intervals[i]; + + return video_i2c_g_parm(filp, priv, parm); +} + static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = { .vidioc_querycap = video_i2c_querycap, .vidioc_g_input = video_i2c_g_input, @@ -501,7 +553,7 @@ static const struct v4l2_ioctl_ops video_i2c_ioctl_ops = { .vidioc_g_fmt_vid_cap = video_i2c_try_fmt_vid_cap, .vidioc_s_fmt_vid_cap = video_i2c_s_fmt_vid_cap, .vidioc_g_parm = video_i2c_g_parm, - .vidioc_s_parm = video_i2c_g_parm, + .vidioc_s_parm = video_i2c_s_parm, .vidioc_try_fmt_vid_cap = video_i2c_try_fmt_vid_cap, .vidioc_reqbufs = vb2_ioctl_reqbufs, .vidioc_create_bufs = vb2_ioctl_create_bufs, @@ -591,6 +643,8 @@ static int video_i2c_probe(struct i2c_client *client, spin_lock_init(&data->slock); INIT_LIST_HEAD(&data->vid_cap_active); + data->frame_interval = data->chip->frame_intervals[0]; + video_set_drvdata(&data->vdev, data); i2c_set_clientdata(client, data); From patchwork Sat Oct 20 14:26:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10650553 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B310E17EF for ; Sat, 20 Oct 2018 14:26:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A1DD628622 for ; Sat, 20 Oct 2018 14:26:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 967D028630; Sat, 20 Oct 2018 14:26:58 +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 EB14628622 for ; Sat, 20 Oct 2018 14:26:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727516AbeJTWhg (ORCPT ); Sat, 20 Oct 2018 18:37:36 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:44417 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727373AbeJTWhg (ORCPT ); Sat, 20 Oct 2018 18:37:36 -0400 Received: by mail-pl1-f195.google.com with SMTP id d23-v6so3673835pls.11 for ; Sat, 20 Oct 2018 07:26: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=Id8CeEw8me9XiidL3SJiTnO941/iX86/RupQPN+eWTY=; b=MDqsUUDp1RBfNW5p4rzIz8VeZHQ1R18RZLL986k2EK1zlWvjSQHFHWhfRF6K9rE/rS x6Zirqka7rh8aeIT73HxWm0iDMbihMy9mefM1+yZ5lFvxOE0BE+9g1CqAE1bnMn3c0mz V0ui0cQ+U1z/0GR1YfXaWghKPGDKWvisDPja23NkwPnGLxV//zd5y+zTgoxAoZnmVvcc F3mul2e9p5D1omBDT4r8qhMHAZgApqUq5rn9PpEI/GL/7grx/XOGkJ+IzC2lMFZDdNv5 zTYlox+3n5PlTe5tSEi0a8jyIB28XEpJwqxXhgi6EIz53+OmizgJlwc9gssur5G3LI+6 pH9w== 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=Id8CeEw8me9XiidL3SJiTnO941/iX86/RupQPN+eWTY=; b=nu6Z1svDM/lXCFBo3LQIyBw1cUfqKNzgkXVYFYY73D4MsqZK6JGDnyaHbGBzCuQ3/n QeZADjLppe+Nm/gmj4cgf//0fzBzNR1s/rLYviwaLM3fqp5q4ZUhlE2J9MnzwzVHhdJ1 hUUE1tY+2KPi1VBEaBVe97t7X5T9Mz5p2WZPOYtieKHat86YYBNvusIVUreCFOLZT4lX 68S4bYVJPWj8E+ue+eiHak2GOGJeQato+prFdaNlKTjU9pkEYqX3+1N6RYSslZh9gJWf oTWuVjaMjZ/NTd0HLYZRa16/BDATPJeOfkrKRdFTUHacUX6SFkwPZOiuhIVULdlUm/MF fUdA== X-Gm-Message-State: ABuFfoi1jRPy+VjsM5yOCz8lK3xg6aKvz3Juah6SouJBFLpXFSEbUHm0 Um870GbiF+Ykz/cTwiKQB3iCIVgC X-Google-Smtp-Source: ACcGV60IbClUVTnIXc4XJl/MxKzuzbL7ege6MLZljLnI+mx0t9Ia+5+bPyWFRu8neZ4+NByi0tLaPw== X-Received: by 2002:a17:902:bf44:: with SMTP id u4-v6mr9983650pls.48.1540045616628; Sat, 20 Oct 2018 07:26:56 -0700 (PDT) Received: from mita-MS-7A45.lan ([240f:34:212d:1:28d8:ef69:4040:67dc]) by smtp.gmail.com with ESMTPSA id q68-v6sm28624799pfb.160.2018.10.20.07.26.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 20 Oct 2018 07:26:56 -0700 (PDT) From: Akinobu Mita To: linux-media@vger.kernel.org Cc: Akinobu Mita , Matt Ranostay , Sakari Ailus , Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH v4 6/6] media: video-i2c: support runtime PM Date: Sat, 20 Oct 2018 23:26:28 +0900 Message-Id: <1540045588-9091-7-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1540045588-9091-1-git-send-email-akinobu.mita@gmail.com> References: <1540045588-9091-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 AMG88xx has a register for setting operating mode. This adds support runtime PM by changing the operating mode. The instruction for changing sleep mode to normal mode is from the reference specifications. https://docid81hrs3j1.cloudfront.net/medialibrary/2017/11/PANA-S-A0002141979-1.pdf Cc: Matt Ranostay Cc: Sakari Ailus Cc: Hans Verkuil Cc: Mauro Carvalho Chehab Reviewed-by: Matt Ranostay Acked-by: Sakari Ailus Signed-off-by: Akinobu Mita --- * v4 - Move set_power() call into release() callback drivers/media/i2c/video-i2c.c | 141 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 2 deletions(-) diff --git a/drivers/media/i2c/video-i2c.c b/drivers/media/i2c/video-i2c.c index 3334fc2..384a046 100644 --- a/drivers/media/i2c/video-i2c.c +++ b/drivers/media/i2c/video-i2c.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -94,10 +95,23 @@ struct video_i2c_chip { /* xfer function */ int (*xfer)(struct video_i2c_data *data, char *buf); + /* power control function */ + int (*set_power)(struct video_i2c_data *data, bool on); + /* hwmon init function */ int (*hwmon_init)(struct video_i2c_data *data); }; +/* Power control register */ +#define AMG88XX_REG_PCTL 0x00 +#define AMG88XX_PCTL_NORMAL 0x00 +#define AMG88XX_PCTL_SLEEP 0x10 + +/* Reset register */ +#define AMG88XX_REG_RST 0x01 +#define AMG88XX_RST_FLAG 0x30 +#define AMG88XX_RST_INIT 0x3f + /* Frame rate register */ #define AMG88XX_REG_FPSC 0x02 #define AMG88XX_FPSC_1FPS BIT(0) @@ -127,6 +141,59 @@ static int amg88xx_setup(struct video_i2c_data *data) return regmap_update_bits(data->regmap, AMG88XX_REG_FPSC, mask, val); } +static int amg88xx_set_power_on(struct video_i2c_data *data) +{ + int ret; + + ret = regmap_write(data->regmap, AMG88XX_REG_PCTL, AMG88XX_PCTL_NORMAL); + if (ret) + return ret; + + msleep(50); + + ret = regmap_write(data->regmap, AMG88XX_REG_RST, AMG88XX_RST_INIT); + if (ret) + return ret; + + msleep(2); + + ret = regmap_write(data->regmap, AMG88XX_REG_RST, AMG88XX_RST_FLAG); + if (ret) + return ret; + + /* + * Wait two frames before reading thermistor and temperature registers + */ + msleep(200); + + return 0; +} + +static int amg88xx_set_power_off(struct video_i2c_data *data) +{ + int ret; + + ret = regmap_write(data->regmap, AMG88XX_REG_PCTL, AMG88XX_PCTL_SLEEP); + if (ret) + return ret; + /* + * Wait for a while to avoid resuming normal mode immediately after + * entering sleep mode, otherwise the device occasionally goes wrong + * (thermistor and temperature registers are not updated at all) + */ + msleep(100); + + return 0; +} + +static int amg88xx_set_power(struct video_i2c_data *data, bool on) +{ + if (on) + return amg88xx_set_power_on(data); + + return amg88xx_set_power_off(data); +} + #if IS_ENABLED(CONFIG_HWMON) static const u32 amg88xx_temp_config[] = { @@ -158,7 +225,15 @@ static int amg88xx_read(struct device *dev, enum hwmon_sensor_types type, __le16 buf; int tmp; + tmp = pm_runtime_get_sync(regmap_get_device(data->regmap)); + if (tmp < 0) { + pm_runtime_put_noidle(regmap_get_device(data->regmap)); + return tmp; + } + tmp = regmap_bulk_read(data->regmap, AMG88XX_REG_TTHL, &buf, 2); + pm_runtime_mark_last_busy(regmap_get_device(data->regmap)); + pm_runtime_put_autosuspend(regmap_get_device(data->regmap)); if (tmp) return tmp; @@ -217,6 +292,7 @@ static const struct video_i2c_chip video_i2c_chip[] = { .regmap_config = &amg88xx_regmap_config, .setup = &amg88xx_setup, .xfer = &amg88xx_xfer, + .set_power = amg88xx_set_power, .hwmon_init = amg88xx_hwmon_init, }, }; @@ -343,14 +419,21 @@ static void video_i2c_del_list(struct vb2_queue *vq, enum vb2_buffer_state state static int start_streaming(struct vb2_queue *vq, unsigned int count) { struct video_i2c_data *data = vb2_get_drv_priv(vq); + struct device *dev = regmap_get_device(data->regmap); int ret; if (data->kthread_vid_cap) return 0; + ret = pm_runtime_get_sync(dev); + if (ret < 0) { + pm_runtime_put_noidle(dev); + goto error_del_list; + } + ret = data->chip->setup(data); if (ret) - goto error_del_list; + goto error_rpm_put; data->sequence = 0; data->kthread_vid_cap = kthread_run(video_i2c_thread_vid_cap, data, @@ -359,6 +442,9 @@ static int start_streaming(struct vb2_queue *vq, unsigned int count) if (!ret) return 0; +error_rpm_put: + pm_runtime_mark_last_busy(dev); + pm_runtime_put_autosuspend(dev); error_del_list: video_i2c_del_list(vq, VB2_BUF_STATE_QUEUED); @@ -374,6 +460,8 @@ static void stop_streaming(struct vb2_queue *vq) kthread_stop(data->kthread_vid_cap); data->kthread_vid_cap = NULL; + pm_runtime_mark_last_busy(regmap_get_device(data->regmap)); + pm_runtime_put_autosuspend(regmap_get_device(data->regmap)); video_i2c_del_list(vq, VB2_BUF_STATE_ERROR); } @@ -648,6 +736,16 @@ static int video_i2c_probe(struct i2c_client *client, video_set_drvdata(&data->vdev, data); i2c_set_clientdata(client, data); + ret = data->chip->set_power(data, true); + if (ret) + goto error_unregister_device; + + pm_runtime_get_noresume(&client->dev); + pm_runtime_set_active(&client->dev); + pm_runtime_enable(&client->dev); + pm_runtime_set_autosuspend_delay(&client->dev, 2000); + pm_runtime_use_autosuspend(&client->dev); + if (data->chip->hwmon_init) { ret = data->chip->hwmon_init(data); if (ret < 0) { @@ -658,10 +756,19 @@ static int video_i2c_probe(struct i2c_client *client, ret = video_register_device(&data->vdev, VFL_TYPE_GRABBER, -1); if (ret < 0) - goto error_unregister_device; + goto error_pm_disable; + + pm_runtime_mark_last_busy(&client->dev); + pm_runtime_put_autosuspend(&client->dev); return 0; +error_pm_disable: + pm_runtime_disable(&client->dev); + pm_runtime_set_suspended(&client->dev); + pm_runtime_put_noidle(&client->dev); + data->chip->set_power(data, false); + error_unregister_device: v4l2_device_unregister(v4l2_dev); mutex_destroy(&data->lock); @@ -680,11 +787,40 @@ static int video_i2c_remove(struct i2c_client *client) { struct video_i2c_data *data = i2c_get_clientdata(client); + pm_runtime_get_sync(&client->dev); + pm_runtime_disable(&client->dev); + pm_runtime_set_suspended(&client->dev); + pm_runtime_put_noidle(&client->dev); + data->chip->set_power(data, false); + video_unregister_device(&data->vdev); return 0; } +#ifdef CONFIG_PM + +static int video_i2c_pm_runtime_suspend(struct device *dev) +{ + struct video_i2c_data *data = i2c_get_clientdata(to_i2c_client(dev)); + + return data->chip->set_power(data, false); +} + +static int video_i2c_pm_runtime_resume(struct device *dev) +{ + struct video_i2c_data *data = i2c_get_clientdata(to_i2c_client(dev)); + + return data->chip->set_power(data, true); +} + +#endif + +static const struct dev_pm_ops video_i2c_pm_ops = { + SET_RUNTIME_PM_OPS(video_i2c_pm_runtime_suspend, + video_i2c_pm_runtime_resume, NULL) +}; + static const struct i2c_device_id video_i2c_id_table[] = { { "amg88xx", AMG88XX }, {} @@ -701,6 +837,7 @@ static struct i2c_driver video_i2c_driver = { .driver = { .name = VIDEO_I2C_DRIVER, .of_match_table = video_i2c_of_match, + .pm = &video_i2c_pm_ops, }, .probe = video_i2c_probe, .remove = video_i2c_remove,