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[] = {
50 .type = &vimc_sen_type
54 .type = &vimc_sen_type
58 .type = &vimc_deb_type
62 .type = &vimc_deb_type
65 .name = "Raw Capture 0",
66 .type = &vimc_cap_type
69 .name = "Raw Capture 1",
70 .type = &vimc_cap_type
73 /* TODO: change this to vimc-input when it is implemented */
74 .name = "RGB/YUV Input",
75 .type = &vimc_sen_type
79 .type = &vimc_sca_type
82 .name = "RGB/YUV Capture",
83 .type = &vimc_cap_type
87 static const struct vimc_ent_link ent_links[] = {
88 /* Link: Sensor A (Pad 0)->(Pad 0) Debayer A */
89 VIMC_ENT_LINK(0, 0, 2, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
90 /* Link: Sensor A (Pad 0)->(Pad 0) Raw Capture 0 */
91 VIMC_ENT_LINK(0, 0, 4, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
92 /* Link: Sensor B (Pad 0)->(Pad 0) Debayer B */
93 VIMC_ENT_LINK(1, 0, 3, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
94 /* Link: Sensor B (Pad 0)->(Pad 0) Raw Capture 1 */
95 VIMC_ENT_LINK(1, 0, 5, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
96 /* Link: Debayer A (Pad 1)->(Pad 0) Scaler */
97 VIMC_ENT_LINK(2, 1, 7, 0, MEDIA_LNK_FL_ENABLED),
98 /* Link: Debayer B (Pad 1)->(Pad 0) Scaler */
99 VIMC_ENT_LINK(3, 1, 7, 0, 0),
100 /* Link: RGB/YUV Input (Pad 0)->(Pad 0) Scaler */
101 VIMC_ENT_LINK(6, 0, 7, 0, 0),
102 /* Link: Scaler (Pad 1)->(Pad 0) RGB/YUV Capture */
103 VIMC_ENT_LINK(7, 1, 8, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
106 static struct vimc_pipeline_config pipe_cfg = {
108 .num_ents = ARRAY_SIZE(ent_config),
110 .num_links = ARRAY_SIZE(ent_links)
113 /* -------------------------------------------------------------------------- */
115 static void vimc_rm_links(struct vimc_device *vimc)
119 for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
120 media_entity_remove_links(vimc->ent_devs[i]->ent);
123 static int vimc_create_links(struct vimc_device *vimc)
128 /* Initialize the links between entities */
129 for (i = 0; i < vimc->pipe_cfg->num_links; i++) {
130 const struct vimc_ent_link *link = &vimc->pipe_cfg->links[i];
132 struct vimc_ent_device *ved_src =
133 vimc->ent_devs[link->src_ent];
134 struct vimc_ent_device *ved_sink =
135 vimc->ent_devs[link->sink_ent];
137 ret = media_create_pad_link(ved_src->ent, link->src_pad,
138 ved_sink->ent, link->sink_pad,
151 static void vimc_release_subdevs(struct vimc_device *vimc)
155 for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
156 if (vimc->ent_devs[i])
157 vimc->pipe_cfg->ents[i].type->release(vimc->ent_devs[i]);
160 static void vimc_unregister_subdevs(struct vimc_device *vimc)
164 for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
165 if (vimc->ent_devs[i] && vimc->pipe_cfg->ents[i].type->unregister)
166 vimc->pipe_cfg->ents[i].type->unregister(vimc->ent_devs[i]);
169 static int vimc_add_subdevs(struct vimc_device *vimc)
173 for (i = 0; i < vimc->pipe_cfg->num_ents; i++) {
174 dev_dbg(vimc->mdev.dev, "new entity for %s\n",
175 vimc->pipe_cfg->ents[i].name);
176 vimc->ent_devs[i] = vimc->pipe_cfg->ents[i].type->add(vimc,
177 vimc->pipe_cfg->ents[i].name);
178 if (IS_ERR(vimc->ent_devs[i])) {
179 int err = PTR_ERR(vimc->ent_devs[i]);
181 dev_err(vimc->mdev.dev, "adding entity %s failed (%d)\n",
182 vimc->pipe_cfg->ents[i].name, err);
183 vimc->ent_devs[i] = NULL;
184 vimc_unregister_subdevs(vimc);
185 vimc_release_subdevs(vimc);
192 static void vimc_v4l2_dev_release(struct v4l2_device *v4l2_dev)
194 struct vimc_device *vimc =
195 container_of(v4l2_dev, struct vimc_device, v4l2_dev);
197 vimc_release_subdevs(vimc);
198 media_device_cleanup(&vimc->mdev);
199 kfree(vimc->ent_devs);
203 static int vimc_register_devices(struct vimc_device *vimc)
207 /* Register the v4l2 struct */
208 ret = v4l2_device_register(vimc->mdev.dev, &vimc->v4l2_dev);
210 dev_err(vimc->mdev.dev,
211 "v4l2 device register failed (err=%d)\n", ret);
214 /* allocate ent_devs */
215 vimc->ent_devs = kcalloc(vimc->pipe_cfg->num_ents,
216 sizeof(*vimc->ent_devs), GFP_KERNEL);
217 if (!vimc->ent_devs) {
219 goto err_v4l2_unregister;
222 /* Invoke entity config hooks to initialize and register subdevs */
223 ret = vimc_add_subdevs(vimc);
225 goto err_free_ent_devs;
227 /* Initialize links */
228 ret = vimc_create_links(vimc);
232 /* Register the media device */
233 ret = media_device_register(&vimc->mdev);
235 dev_err(vimc->mdev.dev,
236 "media device register failed (err=%d)\n", ret);
240 /* Expose all subdev's nodes*/
241 ret = v4l2_device_register_subdev_nodes(&vimc->v4l2_dev);
243 dev_err(vimc->mdev.dev,
244 "vimc subdev nodes registration failed (err=%d)\n",
246 goto err_mdev_unregister;
252 media_device_unregister(&vimc->mdev);
254 vimc_unregister_subdevs(vimc);
255 vimc_release_subdevs(vimc);
257 kfree(vimc->ent_devs);
259 v4l2_device_unregister(&vimc->v4l2_dev);
264 static int vimc_probe(struct platform_device *pdev)
266 struct vimc_device *vimc;
269 dev_dbg(&pdev->dev, "probe");
271 vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
275 vimc->pipe_cfg = &pipe_cfg;
277 /* Link the media device within the v4l2_device */
278 vimc->v4l2_dev.mdev = &vimc->mdev;
280 /* Initialize media device */
281 strscpy(vimc->mdev.model, VIMC_MDEV_MODEL_NAME,
282 sizeof(vimc->mdev.model));
283 snprintf(vimc->mdev.bus_info, sizeof(vimc->mdev.bus_info),
284 "platform:%s", VIMC_PDEV_NAME);
285 vimc->mdev.dev = &pdev->dev;
286 media_device_init(&vimc->mdev);
288 ret = vimc_register_devices(vimc);
290 media_device_cleanup(&vimc->mdev);
295 * the release cb is set only after successful registration.
296 * if the registration fails, we release directly from probe
299 vimc->v4l2_dev.release = vimc_v4l2_dev_release;
300 platform_set_drvdata(pdev, vimc);
304 static int vimc_remove(struct platform_device *pdev)
306 struct vimc_device *vimc = platform_get_drvdata(pdev);
308 dev_dbg(&pdev->dev, "remove");
310 vimc_unregister_subdevs(vimc);
311 media_device_unregister(&vimc->mdev);
312 v4l2_device_unregister(&vimc->v4l2_dev);
313 v4l2_device_put(&vimc->v4l2_dev);
318 static void vimc_dev_release(struct device *dev)
322 static struct platform_device vimc_pdev = {
323 .name = VIMC_PDEV_NAME,
324 .dev.release = vimc_dev_release,
327 static struct platform_driver vimc_pdrv = {
329 .remove = vimc_remove,
331 .name = VIMC_PDEV_NAME,
335 static int __init vimc_init(void)
339 ret = platform_device_register(&vimc_pdev);
341 dev_err(&vimc_pdev.dev,
342 "platform device registration failed (err=%d)\n", ret);
346 ret = platform_driver_register(&vimc_pdrv);
348 dev_err(&vimc_pdev.dev,
349 "platform driver registration failed (err=%d)\n", ret);
350 platform_driver_unregister(&vimc_pdrv);
357 static void __exit vimc_exit(void)
359 platform_driver_unregister(&vimc_pdrv);
361 platform_device_unregister(&vimc_pdev);
364 module_init(vimc_init);
365 module_exit(vimc_exit);
367 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC)");
368 MODULE_AUTHOR("Helen Fornazier <helen.fornazier@gmail.com>");
369 MODULE_LICENSE("GPL");