1 // SPDX-License-Identifier: GPL-2.0-only
3 * Parallel video capture module (VIP) for the Tegra VI.
5 * This file implements the VIP-specific infrastructure.
7 * Copyright (C) 2023 SKIDATA GmbH
8 * Author: Luca Ceresoli <luca.ceresoli@bootlin.com>
11 #include <linux/device.h>
12 #include <linux/host1x.h>
13 #include <linux/module.h>
15 #include <linux/of_graph.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
20 #include <media/v4l2-fwnode.h>
24 static inline struct tegra_vip *host1x_client_to_vip(struct host1x_client *client)
26 return container_of(client, struct tegra_vip, client);
29 static inline struct tegra_vip_channel *subdev_to_vip_channel(struct v4l2_subdev *subdev)
31 return container_of(subdev, struct tegra_vip_channel, subdev);
34 static inline struct tegra_vip *vip_channel_to_vip(struct tegra_vip_channel *chan)
36 return container_of(chan, struct tegra_vip, chan);
39 /* Find the previous subdev in the pipeline (i.e. the one connected to our sink pad) */
40 static struct v4l2_subdev *tegra_vip_channel_get_prev_subdev(struct tegra_vip_channel *chan)
42 struct media_pad *remote_pad;
44 remote_pad = media_pad_remote_pad_first(&chan->pads[TEGRA_VIP_PAD_SINK]);
48 return media_entity_to_v4l2_subdev(remote_pad->entity);
51 static int tegra_vip_enable_stream(struct v4l2_subdev *subdev)
53 struct tegra_vip_channel *vip_chan = subdev_to_vip_channel(subdev);
54 struct tegra_vip *vip = vip_channel_to_vip(vip_chan);
55 struct v4l2_subdev *prev_subdev = tegra_vip_channel_get_prev_subdev(vip_chan);
58 err = pm_runtime_resume_and_get(vip->dev);
60 return dev_err_probe(vip->dev, err, "failed to get runtime PM\n");
62 err = vip->soc->ops->vip_start_streaming(vip_chan);
64 goto err_start_streaming;
66 err = v4l2_subdev_call(prev_subdev, video, s_stream, true);
67 if (err < 0 && err != -ENOIOCTLCMD)
68 goto err_prev_subdev_start_stream;
72 err_prev_subdev_start_stream:
74 pm_runtime_put(vip->dev);
78 static int tegra_vip_disable_stream(struct v4l2_subdev *subdev)
80 struct tegra_vip_channel *vip_chan = subdev_to_vip_channel(subdev);
81 struct tegra_vip *vip = vip_channel_to_vip(vip_chan);
82 struct v4l2_subdev *prev_subdev = tegra_vip_channel_get_prev_subdev(vip_chan);
84 v4l2_subdev_call(prev_subdev, video, s_stream, false);
86 pm_runtime_put(vip->dev);
91 static int tegra_vip_s_stream(struct v4l2_subdev *subdev, int enable)
96 err = tegra_vip_enable_stream(subdev);
98 err = tegra_vip_disable_stream(subdev);
103 static const struct v4l2_subdev_video_ops tegra_vip_video_ops = {
104 .s_stream = tegra_vip_s_stream,
107 static const struct v4l2_subdev_ops tegra_vip_ops = {
108 .video = &tegra_vip_video_ops,
111 static int tegra_vip_channel_of_parse(struct tegra_vip *vip)
113 struct device *dev = vip->dev;
114 struct device_node *np = dev->of_node;
115 struct v4l2_fwnode_endpoint v4l2_ep = {
116 .bus_type = V4L2_MBUS_PARALLEL
118 struct fwnode_handle *fwh;
119 struct device_node *ep;
120 unsigned int num_pads;
123 dev_dbg(dev, "Parsing %pOF", np);
125 ep = of_graph_get_endpoint_by_regs(np, 0, 0);
128 dev_err_probe(dev, err, "%pOF: error getting endpoint node\n", np);
132 fwh = of_fwnode_handle(ep);
133 err = v4l2_fwnode_endpoint_parse(fwh, &v4l2_ep);
136 dev_err_probe(dev, err, "%pOF: failed to parse v4l2 endpoint\n", np);
140 num_pads = of_graph_get_endpoint_count(np);
141 if (num_pads != TEGRA_VIP_PADS_NUM) {
143 dev_err_probe(dev, err, "%pOF: need 2 pads, got %d\n", np, num_pads);
147 vip->chan.of_node = of_node_get(np);
148 vip->chan.pads[TEGRA_VIP_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
149 vip->chan.pads[TEGRA_VIP_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
158 static int tegra_vip_channel_init(struct tegra_vip *vip)
160 struct v4l2_subdev *subdev;
163 subdev = &vip->chan.subdev;
164 v4l2_subdev_init(subdev, &tegra_vip_ops);
165 subdev->dev = vip->dev;
166 snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "%s",
167 kbasename(vip->chan.of_node->full_name));
169 v4l2_set_subdevdata(subdev, &vip->chan);
170 subdev->fwnode = of_fwnode_handle(vip->chan.of_node);
171 subdev->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
173 err = media_entity_pads_init(&subdev->entity, TEGRA_VIP_PADS_NUM, vip->chan.pads);
175 return dev_err_probe(vip->dev, err, "failed to initialize media entity\n");
177 err = v4l2_async_register_subdev(subdev);
179 dev_err_probe(vip->dev, err, "failed to register subdev\n");
180 goto err_register_subdev;
186 media_entity_cleanup(&subdev->entity);
190 static int tegra_vip_init(struct host1x_client *client)
192 struct tegra_vip *vip = host1x_client_to_vip(client);
195 err = tegra_vip_channel_of_parse(vip);
199 err = tegra_vip_channel_init(vip);
206 of_node_put(vip->chan.of_node);
210 static int tegra_vip_exit(struct host1x_client *client)
212 struct tegra_vip *vip = host1x_client_to_vip(client);
213 struct v4l2_subdev *subdev = &vip->chan.subdev;
215 v4l2_async_unregister_subdev(subdev);
216 media_entity_cleanup(&subdev->entity);
217 of_node_put(vip->chan.of_node);
222 static const struct host1x_client_ops vip_client_ops = {
223 .init = tegra_vip_init,
224 .exit = tegra_vip_exit,
227 static int tegra_vip_probe(struct platform_device *pdev)
229 struct tegra_vip *vip;
232 dev_dbg(&pdev->dev, "Probing VIP \"%s\" from %pOF\n", pdev->name, pdev->dev.of_node);
234 vip = devm_kzalloc(&pdev->dev, sizeof(*vip), GFP_KERNEL);
238 vip->soc = of_device_get_match_data(&pdev->dev);
240 vip->dev = &pdev->dev;
241 platform_set_drvdata(pdev, vip);
243 /* initialize host1x interface */
244 INIT_LIST_HEAD(&vip->client.list);
245 vip->client.ops = &vip_client_ops;
246 vip->client.dev = &pdev->dev;
248 err = host1x_client_register(&vip->client);
250 return dev_err_probe(&pdev->dev, err, "failed to register host1x client\n");
252 pm_runtime_enable(&pdev->dev);
257 static int tegra_vip_remove(struct platform_device *pdev)
259 struct tegra_vip *vip = platform_get_drvdata(pdev);
261 host1x_client_unregister(&vip->client);
263 pm_runtime_disable(&pdev->dev);
268 #if defined(CONFIG_ARCH_TEGRA_2x_SOC)
269 extern const struct tegra_vip_soc tegra20_vip_soc;
272 static const struct of_device_id tegra_vip_of_id_table[] = {
273 #if defined(CONFIG_ARCH_TEGRA_2x_SOC)
274 { .compatible = "nvidia,tegra20-vip", .data = &tegra20_vip_soc },
278 MODULE_DEVICE_TABLE(of, tegra_vip_of_id_table);
280 struct platform_driver tegra_vip_driver = {
283 .of_match_table = tegra_vip_of_id_table,
285 .probe = tegra_vip_probe,
286 .remove = tegra_vip_remove,