1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Analog Devices ADV748X CSI-2 Transmitter
5 * Copyright (C) 2017 Renesas Electronics Corp.
8 #include <linux/module.h>
9 #include <linux/mutex.h>
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-device.h>
13 #include <media/v4l2-ioctl.h>
17 static int adv748x_csi2_set_virtual_channel(struct adv748x_csi2
*tx
,
20 return tx_write(tx
, ADV748X_CSI_VC_REF
, vc
<< ADV748X_CSI_VC_REF_SHIFT
);
24 * adv748x_csi2_register_link : Register and link internal entities
26 * @tx: CSI2 private entity
27 * @v4l2_dev: Video registration device
28 * @src: Source subdevice to establish link
29 * @src_pad: Pad number of source to link to this @tx
31 * Ensure that the subdevice is registered against the v4l2_device, and link the
32 * source pad to the sink pad of the CSI2 bus entity.
34 static int adv748x_csi2_register_link(struct adv748x_csi2
*tx
,
35 struct v4l2_device
*v4l2_dev
,
36 struct v4l2_subdev
*src
,
39 int enabled
= MEDIA_LNK_FL_ENABLED
;
43 * Dynamic linking of the AFE is not supported.
44 * Register the links as immutable.
46 enabled
|= MEDIA_LNK_FL_IMMUTABLE
;
49 ret
= v4l2_device_register_subdev(v4l2_dev
, src
);
54 return media_create_pad_link(&src
->entity
, src_pad
,
55 &tx
->sd
.entity
, ADV748X_CSI2_SINK
,
59 /* -----------------------------------------------------------------------------
60 * v4l2_subdev_internal_ops
62 * We use the internal registered operation to be able to ensure that our
63 * incremental subdevices (not connected in the forward path) can be registered
64 * against the resulting video path and media device.
67 static int adv748x_csi2_registered(struct v4l2_subdev
*sd
)
69 struct adv748x_csi2
*tx
= adv748x_sd_to_csi2(sd
);
70 struct adv748x_state
*state
= tx
->state
;
72 adv_dbg(state
, "Registered %s (%s)", is_txa(tx
) ? "TXA":"TXB",
76 * The adv748x hardware allows the AFE to route through the TXA, however
77 * this is not currently supported in this driver.
79 * Link HDMI->TXA, and AFE->TXB directly.
81 if (is_txa(tx
) && is_hdmi_enabled(state
))
82 return adv748x_csi2_register_link(tx
, sd
->v4l2_dev
,
85 if (!is_txa(tx
) && is_afe_enabled(state
))
86 return adv748x_csi2_register_link(tx
, sd
->v4l2_dev
,
92 static const struct v4l2_subdev_internal_ops adv748x_csi2_internal_ops
= {
93 .registered
= adv748x_csi2_registered
,
96 /* -----------------------------------------------------------------------------
97 * v4l2_subdev_video_ops
100 static int adv748x_csi2_s_stream(struct v4l2_subdev
*sd
, int enable
)
102 struct adv748x_csi2
*tx
= adv748x_sd_to_csi2(sd
);
103 struct v4l2_subdev
*src
;
105 src
= adv748x_get_remote_sd(&tx
->pads
[ADV748X_CSI2_SINK
]);
109 return v4l2_subdev_call(src
, video
, s_stream
, enable
);
112 static const struct v4l2_subdev_video_ops adv748x_csi2_video_ops
= {
113 .s_stream
= adv748x_csi2_s_stream
,
116 /* -----------------------------------------------------------------------------
117 * v4l2_subdev_pad_ops
119 * The CSI2 bus pads are ignorant to the data sizes or formats.
120 * But we must support setting the pad formats for format propagation.
123 static struct v4l2_mbus_framefmt
*
124 adv748x_csi2_get_pad_format(struct v4l2_subdev
*sd
,
125 struct v4l2_subdev_pad_config
*cfg
,
126 unsigned int pad
, u32 which
)
128 struct adv748x_csi2
*tx
= adv748x_sd_to_csi2(sd
);
130 if (which
== V4L2_SUBDEV_FORMAT_TRY
)
131 return v4l2_subdev_get_try_format(sd
, cfg
, pad
);
136 static int adv748x_csi2_get_format(struct v4l2_subdev
*sd
,
137 struct v4l2_subdev_pad_config
*cfg
,
138 struct v4l2_subdev_format
*sdformat
)
140 struct adv748x_csi2
*tx
= adv748x_sd_to_csi2(sd
);
141 struct adv748x_state
*state
= tx
->state
;
142 struct v4l2_mbus_framefmt
*mbusformat
;
144 mbusformat
= adv748x_csi2_get_pad_format(sd
, cfg
, sdformat
->pad
,
149 mutex_lock(&state
->mutex
);
151 sdformat
->format
= *mbusformat
;
153 mutex_unlock(&state
->mutex
);
158 static int adv748x_csi2_set_format(struct v4l2_subdev
*sd
,
159 struct v4l2_subdev_pad_config
*cfg
,
160 struct v4l2_subdev_format
*sdformat
)
162 struct adv748x_csi2
*tx
= adv748x_sd_to_csi2(sd
);
163 struct adv748x_state
*state
= tx
->state
;
164 struct v4l2_mbus_framefmt
*mbusformat
;
167 mbusformat
= adv748x_csi2_get_pad_format(sd
, cfg
, sdformat
->pad
,
172 mutex_lock(&state
->mutex
);
174 if (sdformat
->pad
== ADV748X_CSI2_SOURCE
) {
175 const struct v4l2_mbus_framefmt
*sink_fmt
;
177 sink_fmt
= adv748x_csi2_get_pad_format(sd
, cfg
,
186 sdformat
->format
= *sink_fmt
;
189 *mbusformat
= sdformat
->format
;
192 mutex_unlock(&state
->mutex
);
197 static const struct v4l2_subdev_pad_ops adv748x_csi2_pad_ops
= {
198 .get_fmt
= adv748x_csi2_get_format
,
199 .set_fmt
= adv748x_csi2_set_format
,
202 /* -----------------------------------------------------------------------------
206 static const struct v4l2_subdev_ops adv748x_csi2_ops
= {
207 .video
= &adv748x_csi2_video_ops
,
208 .pad
= &adv748x_csi2_pad_ops
,
211 /* -----------------------------------------------------------------------------
212 * Subdev module and controls
215 int adv748x_csi2_set_pixelrate(struct v4l2_subdev
*sd
, s64 rate
)
217 struct adv748x_csi2
*tx
= adv748x_sd_to_csi2(sd
);
222 return v4l2_ctrl_s_ctrl_int64(tx
->pixel_rate
, rate
);
225 static int adv748x_csi2_s_ctrl(struct v4l2_ctrl
*ctrl
)
228 case V4L2_CID_PIXEL_RATE
:
235 static const struct v4l2_ctrl_ops adv748x_csi2_ctrl_ops
= {
236 .s_ctrl
= adv748x_csi2_s_ctrl
,
239 static int adv748x_csi2_init_controls(struct adv748x_csi2
*tx
)
242 v4l2_ctrl_handler_init(&tx
->ctrl_hdl
, 1);
244 tx
->pixel_rate
= v4l2_ctrl_new_std(&tx
->ctrl_hdl
,
245 &adv748x_csi2_ctrl_ops
,
246 V4L2_CID_PIXEL_RATE
, 1, INT_MAX
,
249 tx
->sd
.ctrl_handler
= &tx
->ctrl_hdl
;
250 if (tx
->ctrl_hdl
.error
) {
251 v4l2_ctrl_handler_free(&tx
->ctrl_hdl
);
252 return tx
->ctrl_hdl
.error
;
255 return v4l2_ctrl_handler_setup(&tx
->ctrl_hdl
);
258 int adv748x_csi2_init(struct adv748x_state
*state
, struct adv748x_csi2
*tx
)
262 if (!is_tx_enabled(tx
))
265 /* Initialise the virtual channel */
266 adv748x_csi2_set_virtual_channel(tx
, 0);
268 adv748x_subdev_init(&tx
->sd
, state
, &adv748x_csi2_ops
,
269 MEDIA_ENT_F_VID_IF_BRIDGE
,
270 is_txa(tx
) ? "txa" : "txb");
272 /* Ensure that matching is based upon the endpoint fwnodes */
273 tx
->sd
.fwnode
= of_fwnode_handle(state
->endpoints
[tx
->port
]);
275 /* Register internal ops for incremental subdev registration */
276 tx
->sd
.internal_ops
= &adv748x_csi2_internal_ops
;
278 tx
->pads
[ADV748X_CSI2_SINK
].flags
= MEDIA_PAD_FL_SINK
;
279 tx
->pads
[ADV748X_CSI2_SOURCE
].flags
= MEDIA_PAD_FL_SOURCE
;
281 ret
= media_entity_pads_init(&tx
->sd
.entity
, ADV748X_CSI2_NR_PADS
,
286 ret
= adv748x_csi2_init_controls(tx
);
290 ret
= v4l2_async_register_subdev(&tx
->sd
);
297 v4l2_ctrl_handler_free(&tx
->ctrl_hdl
);
299 media_entity_cleanup(&tx
->sd
.entity
);
304 void adv748x_csi2_cleanup(struct adv748x_csi2
*tx
)
306 if (!is_tx_enabled(tx
))
309 v4l2_async_unregister_subdev(&tx
->sd
);
310 media_entity_cleanup(&tx
->sd
.entity
);
311 v4l2_ctrl_handler_free(&tx
->ctrl_hdl
);