1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * vimc-core.c Virtual Media Controller Driver
5 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <media/media-device.h>
12 #include <media/v4l2-device.h>
14 #include "vimc-common.h"
16 #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
18 #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) { \
22 .sink_pad = sinkpad, \
23 .flags = link_flags, \
26 /* Structure which describes links between entities */
27 struct vimc_ent_link {
30 unsigned int sink_ent;
35 /* Structure which describes the whole topology */
36 struct vimc_pipeline_config {
37 const struct vimc_ent_config *ents;
39 const struct vimc_ent_link *links;
43 /* --------------------------------------------------------------------------
44 * Topology Configuration
47 static struct vimc_ent_config ent_config[] = {
69 .name = "Raw Capture 0",
74 .name = "Raw Capture 1",
79 /* TODO: change this to vimc-input when it is implemented */
80 .name = "RGB/YUV Input",
90 .name = "RGB/YUV Capture",
96 static const struct vimc_ent_link ent_links[] = {
97 /* Link: Sensor A (Pad 0)->(Pad 0) Debayer A */
98 VIMC_ENT_LINK(0, 0, 2, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
99 /* Link: Sensor A (Pad 0)->(Pad 0) Raw Capture 0 */
100 VIMC_ENT_LINK(0, 0, 4, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
101 /* Link: Sensor B (Pad 0)->(Pad 0) Debayer B */
102 VIMC_ENT_LINK(1, 0, 3, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
103 /* Link: Sensor B (Pad 0)->(Pad 0) Raw Capture 1 */
104 VIMC_ENT_LINK(1, 0, 5, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
105 /* Link: Debayer A (Pad 1)->(Pad 0) Scaler */
106 VIMC_ENT_LINK(2, 1, 7, 0, MEDIA_LNK_FL_ENABLED),
107 /* Link: Debayer B (Pad 1)->(Pad 0) Scaler */
108 VIMC_ENT_LINK(3, 1, 7, 0, 0),
109 /* Link: RGB/YUV Input (Pad 0)->(Pad 0) Scaler */
110 VIMC_ENT_LINK(6, 0, 7, 0, 0),
111 /* Link: Scaler (Pad 1)->(Pad 0) RGB/YUV Capture */
112 VIMC_ENT_LINK(7, 1, 8, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
115 static struct vimc_pipeline_config pipe_cfg = {
117 .num_ents = ARRAY_SIZE(ent_config),
119 .num_links = ARRAY_SIZE(ent_links)
122 /* -------------------------------------------------------------------------- */
124 static void vimc_rm_links(struct vimc_device *vimc)
128 for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
129 media_entity_remove_links(vimc->ent_devs[i]->ent);
132 static int vimc_create_links(struct vimc_device *vimc)
137 /* Initialize the links between entities */
138 for (i = 0; i < vimc->pipe_cfg->num_links; i++) {
139 const struct vimc_ent_link *link = &vimc->pipe_cfg->links[i];
141 struct vimc_ent_device *ved_src =
142 vimc->ent_devs[link->src_ent];
143 struct vimc_ent_device *ved_sink =
144 vimc->ent_devs[link->sink_ent];
146 ret = media_create_pad_link(ved_src->ent, link->src_pad,
147 ved_sink->ent, link->sink_pad,
160 static int vimc_add_subdevs(struct vimc_device *vimc)
163 struct vimc_ent_device *ved;
165 for (i = 0; i < vimc->pipe_cfg->num_ents; i++) {
166 dev_dbg(&vimc->pdev.dev, "new entity for %s\n",
167 vimc->pipe_cfg->ents[i].name);
168 ved = vimc->pipe_cfg->ents[i].add(vimc,
169 vimc->pipe_cfg->ents[i].name);
171 dev_err(&vimc->pdev.dev, "add new entity for %s\n",
172 vimc->pipe_cfg->ents[i].name);
175 vimc->ent_devs[i] = ved;
180 static void vimc_rm_subdevs(struct vimc_device *vimc)
184 for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
185 if (vimc->ent_devs[i])
186 vimc->pipe_cfg->ents[i].rm(vimc, vimc->ent_devs[i]);
189 static int vimc_register_devices(struct vimc_device *vimc)
193 /* Register the v4l2 struct */
194 ret = v4l2_device_register(vimc->mdev.dev, &vimc->v4l2_dev);
196 dev_err(vimc->mdev.dev,
197 "v4l2 device register failed (err=%d)\n", ret);
201 /* allocate ent_devs */
202 vimc->ent_devs = kmalloc_array(vimc->pipe_cfg->num_ents,
203 sizeof(*vimc->ent_devs),
206 goto err_v4l2_unregister;
208 /* Invoke entity config hooks to initialize and register subdevs */
209 ret = vimc_add_subdevs(vimc);
211 /* remove sundevs that got added */
214 /* Initialize links */
215 ret = vimc_create_links(vimc);
219 /* Register the media device */
220 ret = media_device_register(&vimc->mdev);
222 dev_err(vimc->mdev.dev,
223 "media device register failed (err=%d)\n", ret);
227 /* Expose all subdev's nodes*/
228 ret = v4l2_device_register_subdev_nodes(&vimc->v4l2_dev);
230 dev_err(vimc->mdev.dev,
231 "vimc subdev nodes registration failed (err=%d)\n",
233 goto err_mdev_unregister;
239 media_device_unregister(&vimc->mdev);
240 media_device_cleanup(&vimc->mdev);
242 vimc_rm_subdevs(vimc);
243 kfree(vimc->ent_devs);
245 v4l2_device_unregister(&vimc->v4l2_dev);
250 static void vimc_unregister(struct vimc_device *vimc)
252 media_device_unregister(&vimc->mdev);
253 media_device_cleanup(&vimc->mdev);
254 v4l2_device_unregister(&vimc->v4l2_dev);
255 kfree(vimc->ent_devs);
258 static int vimc_probe(struct platform_device *pdev)
260 struct vimc_device *vimc = container_of(pdev, struct vimc_device, pdev);
263 dev_dbg(&pdev->dev, "probe");
265 memset(&vimc->mdev, 0, sizeof(vimc->mdev));
267 /* Link the media device within the v4l2_device */
268 vimc->v4l2_dev.mdev = &vimc->mdev;
270 /* Initialize media device */
271 strscpy(vimc->mdev.model, VIMC_MDEV_MODEL_NAME,
272 sizeof(vimc->mdev.model));
273 snprintf(vimc->mdev.bus_info, sizeof(vimc->mdev.bus_info),
274 "platform:%s", VIMC_PDEV_NAME);
275 vimc->mdev.dev = &pdev->dev;
276 media_device_init(&vimc->mdev);
278 ret = vimc_register_devices(vimc);
280 media_device_cleanup(&vimc->mdev);
287 static int vimc_remove(struct platform_device *pdev)
289 struct vimc_device *vimc = container_of(pdev, struct vimc_device, pdev);
291 dev_dbg(&pdev->dev, "remove");
293 vimc_rm_subdevs(vimc);
294 vimc_unregister(vimc);
299 static void vimc_dev_release(struct device *dev)
303 static struct vimc_device vimc_dev = {
304 .pipe_cfg = &pipe_cfg,
306 .name = VIMC_PDEV_NAME,
307 .dev.release = vimc_dev_release,
311 static struct platform_driver vimc_pdrv = {
313 .remove = vimc_remove,
315 .name = VIMC_PDEV_NAME,
319 static int __init vimc_init(void)
323 ret = platform_device_register(&vimc_dev.pdev);
325 dev_err(&vimc_dev.pdev.dev,
326 "platform device registration failed (err=%d)\n", ret);
330 ret = platform_driver_register(&vimc_pdrv);
332 dev_err(&vimc_dev.pdev.dev,
333 "platform driver registration failed (err=%d)\n", ret);
334 platform_driver_unregister(&vimc_pdrv);
341 static void __exit vimc_exit(void)
343 platform_driver_unregister(&vimc_pdrv);
345 platform_device_unregister(&vimc_dev.pdev);
348 module_init(vimc_init);
349 module_exit(vimc_exit);
351 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC)");
352 MODULE_AUTHOR("Helen Fornazier <helen.fornazier@gmail.com>");
353 MODULE_LICENSE("GPL");