From patchwork Thu Dec 16 17:47:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 525664 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC9FAC433FE for ; Thu, 16 Dec 2021 17:47:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240410AbhLPRrI (ORCPT ); Thu, 16 Dec 2021 12:47:08 -0500 Received: from relay11.mail.gandi.net ([217.70.178.231]:40393 "EHLO relay11.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240276AbhLPRrI (ORCPT ); Thu, 16 Dec 2021 12:47:08 -0500 Received: (Authenticated sender: jacopo@jmondi.org) by relay11.mail.gandi.net (Postfix) with ESMTPSA id E5547100002; Thu, 16 Dec 2021 17:47:03 +0000 (UTC) From: Jacopo Mondi To: tomi.valkeinen@ideasonboard.com, sakari.ailus@linux.intel.com, laurent.pinchart@ideasonboard.com, niklas.soderlund@ragnatech.se, kieran.bingham@ideasonboard.com Cc: Jacopo Mondi , Mauro Carvalho Chehab , Hans Verkuil , linux-media@vger.kernel.org, linux-renesas-soc@vger.kernel.org Subject: [PATCH v4 1/6] media: max9286: Add support for v4l2_subdev_state Date: Thu, 16 Dec 2021 18:47:41 +0100 Message-Id: <20211216174746.147233-2-jacopo+renesas@jmondi.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211216174746.147233-1-jacopo+renesas@jmondi.org> References: <20211216174746.147233-1-jacopo+renesas@jmondi.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Create and initialize the v4l2_subdev_state for the max9286 driver in order to prepare to support routing operations and multiplexed streams. Create the subdevice state with v4l2_subdev_init_finalize() and implement the init_cfg() operation to guarantee the state is initialized correctly with the default device format. Remove the max9286_open() subdev internal operation as the format of the file-handle state is now initialized by init_cfg(). Signed-off-by: Jacopo Mondi --- drivers/media/i2c/max9286.c | 89 +++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 33 deletions(-) diff --git a/drivers/media/i2c/max9286.c b/drivers/media/i2c/max9286.c index 9e204aabf465..1b9ff537d08e 100644 --- a/drivers/media/i2c/max9286.c +++ b/drivers/media/i2c/max9286.c @@ -214,6 +214,17 @@ static inline struct max9286_priv *sd_to_max9286(struct v4l2_subdev *sd) return container_of(sd, struct max9286_priv, sd); } +static const struct v4l2_mbus_framefmt max9286_default_format = { + .width = 1280, + .height = 800, + .code = MEDIA_BUS_FMT_UYVY8_1X16, + .colorspace = V4L2_COLORSPACE_SRGB, + .field = V4L2_FIELD_NONE, + .ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT, + .quantization = V4L2_QUANTIZATION_DEFAULT, + .xfer_func = V4L2_XFER_FUNC_DEFAULT, +}; + /* ----------------------------------------------------------------------------- * I2C IO */ @@ -821,11 +832,45 @@ static int max9286_get_fmt(struct v4l2_subdev *sd, return 0; } +static int max9286_init_cfg(struct v4l2_subdev *sd, + struct v4l2_subdev_state *state) +{ + struct v4l2_subdev_route routes[MAX9286_NUM_GMSL]; + struct max9286_priv *priv = sd_to_max9286(sd); + struct v4l2_subdev_krouting routing; + struct max9286_source *source; + unsigned int num_routes = 0; + int ret; + + /* Create a route for each enable source. */ + for_each_source(priv, source) { + struct v4l2_subdev_route *route = &routes[num_routes++]; + unsigned int idx = to_index(priv, source); + + route->sink_pad = idx; + route->sink_stream = 0; + route->source_pad = MAX9286_SRC_PAD; + route->source_stream = idx; + route->flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE; + } + + routing.num_routes = num_routes; + routing.routes = routes; + + v4l2_subdev_lock_state(state); + ret = v4l2_subdev_set_routing_with_fmt(sd, state, &routing, + &max9286_default_format); + v4l2_subdev_unlock_state(state); + + return ret; +} + static const struct v4l2_subdev_video_ops max9286_video_ops = { .s_stream = max9286_s_stream, }; static const struct v4l2_subdev_pad_ops max9286_pad_ops = { + .init_cfg = max9286_init_cfg, .enum_mbus_code = max9286_enum_mbus_code, .get_fmt = max9286_get_fmt, .set_fmt = max9286_set_fmt, @@ -836,35 +881,6 @@ static const struct v4l2_subdev_ops max9286_subdev_ops = { .pad = &max9286_pad_ops, }; -static void max9286_init_format(struct v4l2_mbus_framefmt *fmt) -{ - fmt->width = 1280; - fmt->height = 800; - fmt->code = MEDIA_BUS_FMT_UYVY8_1X16; - fmt->colorspace = V4L2_COLORSPACE_SRGB; - fmt->field = V4L2_FIELD_NONE; - fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; - fmt->quantization = V4L2_QUANTIZATION_DEFAULT; - fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT; -} - -static int max9286_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh) -{ - struct v4l2_mbus_framefmt *format; - unsigned int i; - - for (i = 0; i < MAX9286_N_SINKS; i++) { - format = v4l2_subdev_get_try_format(subdev, fh->state, i); - max9286_init_format(format); - } - - return 0; -} - -static const struct v4l2_subdev_internal_ops max9286_subdev_internal_ops = { - .open = max9286_open, -}; - static int max9286_s_ctrl(struct v4l2_ctrl *ctrl) { switch (ctrl->id) { @@ -896,11 +912,11 @@ static int max9286_v4l2_register(struct max9286_priv *priv) /* Configure V4L2 for the MAX9286 itself */ for (i = 0; i < MAX9286_N_SINKS; i++) - max9286_init_format(&priv->fmt[i]); + priv->fmt[i] = max9286_default_format; v4l2_i2c_subdev_init(&priv->sd, priv->client, &max9286_subdev_ops); - priv->sd.internal_ops = &max9286_subdev_internal_ops; - priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; + priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | + V4L2_SUBDEV_FL_MULTIPLEXED; v4l2_ctrl_handler_init(&priv->ctrls, 1); priv->pixelrate = v4l2_ctrl_new_std(&priv->ctrls, @@ -932,14 +948,20 @@ static int max9286_v4l2_register(struct max9286_priv *priv) } priv->sd.fwnode = ep; + ret = v4l2_subdev_init_finalize(&priv->sd); + if (ret) + goto err_put_node; + ret = v4l2_async_register_subdev(&priv->sd); if (ret < 0) { dev_err(dev, "Unable to register subdevice\n"); - goto err_put_node; + goto err_state_cleanup; } return 0; +err_state_cleanup: + v4l2_subdev_cleanup(&priv->sd); err_put_node: fwnode_handle_put(ep); err_async: @@ -952,6 +974,7 @@ static void max9286_v4l2_unregister(struct max9286_priv *priv) { fwnode_handle_put(priv->sd.fwnode); v4l2_async_unregister_subdev(&priv->sd); + v4l2_subdev_cleanup(&priv->sd); max9286_v4l2_notifier_unregister(priv); } From patchwork Thu Dec 16 17:47:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 525663 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7F81C433EF for ; Thu, 16 Dec 2021 17:47:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240423AbhLPRrP (ORCPT ); Thu, 16 Dec 2021 12:47:15 -0500 Received: from relay11.mail.gandi.net ([217.70.178.231]:40931 "EHLO relay11.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240421AbhLPRrO (ORCPT ); Thu, 16 Dec 2021 12:47:14 -0500 Received: (Authenticated sender: jacopo@jmondi.org) by relay11.mail.gandi.net (Postfix) with ESMTPSA id A3581100002; Thu, 16 Dec 2021 17:47:10 +0000 (UTC) From: Jacopo Mondi To: tomi.valkeinen@ideasonboard.com, sakari.ailus@linux.intel.com, laurent.pinchart@ideasonboard.com, niklas.soderlund@ragnatech.se, kieran.bingham@ideasonboard.com Cc: Jacopo Mondi , Mauro Carvalho Chehab , Hans Verkuil , linux-media@vger.kernel.org, linux-renesas-soc@vger.kernel.org Subject: [PATCH v4 3/6] media: max9286: Use enabled routes to calculate pixelrate Date: Thu, 16 Dec 2021 18:47:43 +0100 Message-Id: <20211216174746.147233-4-jacopo+renesas@jmondi.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211216174746.147233-1-jacopo+renesas@jmondi.org> References: <20211216174746.147233-1-jacopo+renesas@jmondi.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Now that the device supports routing, use the enabled routes instead of the connected sources to compute the output pixel rate. To do so, update the route_mask after a set_routing() call with the ACTIVE format, and re-calculate the pixel rate just after. At the same time, start and stop only the enabled routes at s_stream() time. Signed-off-by: Jacopo Mondi --- drivers/media/i2c/max9286.c | 63 ++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/drivers/media/i2c/max9286.c b/drivers/media/i2c/max9286.c index eb76acdb2cd9..1395eb783dc0 100644 --- a/drivers/media/i2c/max9286.c +++ b/drivers/media/i2c/max9286.c @@ -188,8 +188,11 @@ struct max9286_priv { struct v4l2_async_notifier notifier; }; +#define to_index(priv, source) ((source) - &(priv)->sources[0]) + static struct max9286_source *next_source(struct max9286_priv *priv, - struct max9286_source *source) + struct max9286_source *source, + bool routed) { if (!source) source = &priv->sources[0]; @@ -197,17 +200,27 @@ static struct max9286_source *next_source(struct max9286_priv *priv, source++; for (; source < &priv->sources[MAX9286_NUM_GMSL]; source++) { - if (source->fwnode) + unsigned int index = to_index(priv, source); + + if (!source->fwnode) + continue; + + /* + * Careful here! A very unfortunate call to set_routing() can + * change priv->route_mask behind our back! + */ + if (!routed || priv->route_mask & BIT(index)) return source; } return NULL; } -#define for_each_source(priv, source) \ - for ((source) = NULL; ((source) = next_source((priv), (source))); ) +#define for_each_route(priv, source) \ + for ((source) = NULL; ((source) = next_source((priv), (source), true)); ) -#define to_index(priv, source) ((source) - &(priv)->sources[0]) +#define for_each_source(priv, source) \ + for ((source) = NULL; ((source) = next_source((priv), (source), false)); ) static inline struct max9286_priv *sd_to_max9286(struct v4l2_subdev *sd) { @@ -409,7 +422,7 @@ static int max9286_check_video_links(struct max9286_priv *priv) if (ret < 0) return -EIO; - if ((ret & MAX9286_VIDEO_DETECT_MASK) == priv->source_mask) + if ((ret & MAX9286_VIDEO_DETECT_MASK) == priv->route_mask) break; usleep_range(350, 500); @@ -493,9 +506,10 @@ static int max9286_check_config_link(struct max9286_priv *priv, static int max9286_set_pixelrate(struct max9286_priv *priv) { struct max9286_source *source = NULL; + unsigned int num_routes = 0; u64 pixelrate = 0; - for_each_source(priv, source) { + for_each_route(priv, source) { struct v4l2_ctrl *ctrl; u64 source_rate = 0; @@ -516,6 +530,8 @@ static int max9286_set_pixelrate(struct max9286_priv *priv) "Unable to calculate pixel rate\n"); return -EINVAL; } + + num_routes++; } if (!pixelrate) { @@ -529,7 +545,7 @@ static int max9286_set_pixelrate(struct max9286_priv *priv) * by the number of available sources. */ return v4l2_ctrl_s_ctrl_int64(priv->pixelrate, - pixelrate * priv->nsources); + pixelrate * num_routes); } static int max9286_notify_bound(struct v4l2_async_notifier *notifier, @@ -691,8 +707,8 @@ static int max9286_s_stream(struct v4l2_subdev *sd, int enable) */ max9286_i2c_mux_open(priv); - /* Start all cameras. */ - for_each_source(priv, source) { + /* Start cameras. */ + for_each_route(priv, source) { ret = v4l2_subdev_call(source->sd, video, s_stream, 1); if (ret) return ret; @@ -733,8 +749,8 @@ static int max9286_s_stream(struct v4l2_subdev *sd, int enable) } else { max9286_write(priv, 0x15, MAX9286_VCTYPE | MAX9286_0X15_RESV); - /* Stop all cameras. */ - for_each_source(priv, source) + /* Stop cameras. */ + for_each_route(priv, source) v4l2_subdev_call(source->sd, video, s_stream, 0); max9286_i2c_mux_close(priv); @@ -912,6 +928,29 @@ static int max9286_set_routing(struct v4l2_subdev *sd, v4l2_subdev_lock_state(state); ret = _max9286_set_routing(sd, state, routing); + if (ret) + goto out; + + if (which == V4L2_SUBDEV_FORMAT_TRY) + goto out; + + /* + * Update the active routes mask and the pixel rate according to the + * routed sources. + */ + priv->route_mask = 0; + for (i = 0; i < routing->num_routes; ++i) { + struct v4l2_subdev_route *route = &routing->routes[i]; + + if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE)) + continue; + + priv->route_mask |= BIT(route->sink_pad); + } + + max9286_set_pixelrate(priv); + +out: v4l2_subdev_unlock_state(state); return ret; From patchwork Thu Dec 16 17:47:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 525662 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39458C433F5 for ; Thu, 16 Dec 2021 17:47:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240443AbhLPRrW (ORCPT ); Thu, 16 Dec 2021 12:47:22 -0500 Received: from relay11.mail.gandi.net ([217.70.178.231]:55579 "EHLO relay11.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240435AbhLPRrV (ORCPT ); Thu, 16 Dec 2021 12:47:21 -0500 Received: (Authenticated sender: jacopo@jmondi.org) by relay11.mail.gandi.net (Postfix) with ESMTPSA id 3B0F9100002; Thu, 16 Dec 2021 17:47:17 +0000 (UTC) From: Jacopo Mondi To: tomi.valkeinen@ideasonboard.com, sakari.ailus@linux.intel.com, laurent.pinchart@ideasonboard.com, niklas.soderlund@ragnatech.se, kieran.bingham@ideasonboard.com Cc: Jacopo Mondi , Mauro Carvalho Chehab , Hans Verkuil , linux-media@vger.kernel.org, linux-renesas-soc@vger.kernel.org Subject: [PATCH v4 5/6] media: max9286: Move format to subdev state Date: Thu, 16 Dec 2021 18:47:45 +0100 Message-Id: <20211216174746.147233-6-jacopo+renesas@jmondi.org> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211216174746.147233-1-jacopo+renesas@jmondi.org> References: <20211216174746.147233-1-jacopo+renesas@jmondi.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Move format handling to the v4l2_subdev state and store it per (pad, stream) combination. Now that the image format is stored in the subdev state, it can be accessed through v4l2_subdev_get_fmt() instead of open-coding it. Signed-off-by: Jacopo Mondi --- drivers/media/i2c/max9286.c | 86 ++++++++++++------------------------- 1 file changed, 27 insertions(+), 59 deletions(-) diff --git a/drivers/media/i2c/max9286.c b/drivers/media/i2c/max9286.c index 5d728fa23f01..aa7cb7c10fc0 100644 --- a/drivers/media/i2c/max9286.c +++ b/drivers/media/i2c/max9286.c @@ -174,8 +174,6 @@ struct max9286_priv { struct v4l2_ctrl_handler ctrls; struct v4l2_ctrl *pixelrate; - struct v4l2_mbus_framefmt fmt[MAX9286_N_SINKS]; - /* Protects controls and fmt structures */ struct mutex mutex; @@ -828,28 +826,17 @@ static int max9286_enum_mbus_code(struct v4l2_subdev *sd, return 0; } -static struct v4l2_mbus_framefmt * -max9286_get_pad_format(struct max9286_priv *priv, - struct v4l2_subdev_state *sd_state, - unsigned int pad, u32 which) -{ - switch (which) { - case V4L2_SUBDEV_FORMAT_TRY: - return v4l2_subdev_get_try_format(&priv->sd, sd_state, pad); - case V4L2_SUBDEV_FORMAT_ACTIVE: - return &priv->fmt[pad]; - default: - return NULL; - } -} - static int max9286_set_fmt(struct v4l2_subdev *sd, - struct v4l2_subdev_state *sd_state, + struct v4l2_subdev_state *state, struct v4l2_subdev_format *format) { - struct max9286_priv *priv = sd_to_max9286(sd); - struct v4l2_mbus_framefmt *cfg_fmt; + struct v4l2_mbus_framefmt *fmt; + int ret = 0; + /* + * Refuse to set format on the multiplexed source pad. + * Format is propagated from sinks streams to source streams. + */ if (format->pad == MAX9286_SRC_PAD) return -EINVAL; @@ -865,44 +852,28 @@ static int max9286_set_fmt(struct v4l2_subdev *sd, break; } - cfg_fmt = max9286_get_pad_format(priv, sd_state, format->pad, - format->which); - if (!cfg_fmt) - return -EINVAL; - - mutex_lock(&priv->mutex); - *cfg_fmt = format->format; - mutex_unlock(&priv->mutex); - - return 0; -} - -static int max9286_get_fmt(struct v4l2_subdev *sd, - struct v4l2_subdev_state *sd_state, - struct v4l2_subdev_format *format) -{ - struct max9286_priv *priv = sd_to_max9286(sd); - struct v4l2_mbus_framefmt *cfg_fmt; - unsigned int pad = format->pad; - - /* - * Multiplexed Stream Support: Support link validation by returning the - * format of the first bound link. All links must have the same format, - * as we do not support mixing and matching of cameras connected to the - * max9286. - */ - if (pad == MAX9286_SRC_PAD) - pad = __ffs(priv->bound_sources); + v4l2_subdev_lock_state(state); + fmt = v4l2_state_get_stream_format(state, format->pad, + format->stream); + if (!fmt) { + ret = -EINVAL; + goto out; + } + *fmt = format->format; - cfg_fmt = max9286_get_pad_format(priv, sd_state, pad, format->which); - if (!cfg_fmt) - return -EINVAL; + /* Propagate format to the other end of the route. */ + fmt = v4l2_subdev_state_get_opposite_stream_format(state, format->pad, + format->stream); + if (!fmt) { + ret = -EINVAL; + goto out; + } + *fmt = format->format; - mutex_lock(&priv->mutex); - format->format = *cfg_fmt; - mutex_unlock(&priv->mutex); +out: + v4l2_subdev_unlock_state(state); - return 0; + return ret; } static int max9286_routing_validate(struct max9286_priv *priv, @@ -1052,7 +1023,7 @@ static const struct v4l2_subdev_video_ops max9286_video_ops = { static const struct v4l2_subdev_pad_ops max9286_pad_ops = { .init_cfg = max9286_init_cfg, .enum_mbus_code = max9286_enum_mbus_code, - .get_fmt = max9286_get_fmt, + .get_fmt = v4l2_subdev_get_fmt, .set_fmt = max9286_set_fmt, .set_routing = max9286_set_routing, }; @@ -1092,9 +1063,6 @@ static int max9286_v4l2_register(struct max9286_priv *priv) /* Configure V4L2 for the MAX9286 itself */ - for (i = 0; i < MAX9286_N_SINKS; i++) - priv->fmt[i] = max9286_default_format; - v4l2_i2c_subdev_init(&priv->sd, priv->client, &max9286_subdev_ops); priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_MULTIPLEXED;