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/font.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <media/media-device.h>
13 #include <media/tpg/v4l2-tpg.h>
14 #include <media/v4l2-device.h>
16 #include "vimc-common.h"
18 #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
20 #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) { \
24 .sink_pad = sinkpad, \
25 .flags = link_flags, \
28 /* Structure which describes links between entities */
29 struct vimc_ent_link
{
32 unsigned int sink_ent
;
37 /* Structure which describes the whole topology */
38 struct vimc_pipeline_config
{
39 const struct vimc_ent_config
*ents
;
41 const struct vimc_ent_link
*links
;
45 /* --------------------------------------------------------------------------
46 * Topology Configuration
49 static struct vimc_ent_config ent_config
[] = {
52 .type
= &vimc_sen_type
56 .type
= &vimc_sen_type
60 .type
= &vimc_deb_type
64 .type
= &vimc_deb_type
67 .name
= "Raw Capture 0",
68 .type
= &vimc_cap_type
71 .name
= "Raw Capture 1",
72 .type
= &vimc_cap_type
75 /* TODO: change this to vimc-input when it is implemented */
76 .name
= "RGB/YUV Input",
77 .type
= &vimc_sen_type
81 .type
= &vimc_sca_type
84 .name
= "RGB/YUV Capture",
85 .type
= &vimc_cap_type
89 static const struct vimc_ent_link ent_links
[] = {
90 /* Link: Sensor A (Pad 0)->(Pad 0) Debayer A */
91 VIMC_ENT_LINK(0, 0, 2, 0, MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
),
92 /* Link: Sensor A (Pad 0)->(Pad 0) Raw Capture 0 */
93 VIMC_ENT_LINK(0, 0, 4, 0, MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
),
94 /* Link: Sensor B (Pad 0)->(Pad 0) Debayer B */
95 VIMC_ENT_LINK(1, 0, 3, 0, MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
),
96 /* Link: Sensor B (Pad 0)->(Pad 0) Raw Capture 1 */
97 VIMC_ENT_LINK(1, 0, 5, 0, MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
),
98 /* Link: Debayer A (Pad 1)->(Pad 0) Scaler */
99 VIMC_ENT_LINK(2, 1, 7, 0, MEDIA_LNK_FL_ENABLED
),
100 /* Link: Debayer B (Pad 1)->(Pad 0) Scaler */
101 VIMC_ENT_LINK(3, 1, 7, 0, 0),
102 /* Link: RGB/YUV Input (Pad 0)->(Pad 0) Scaler */
103 VIMC_ENT_LINK(6, 0, 7, 0, 0),
104 /* Link: Scaler (Pad 1)->(Pad 0) RGB/YUV Capture */
105 VIMC_ENT_LINK(7, 1, 8, 0, MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
),
108 static struct vimc_pipeline_config pipe_cfg
= {
110 .num_ents
= ARRAY_SIZE(ent_config
),
112 .num_links
= ARRAY_SIZE(ent_links
)
115 /* -------------------------------------------------------------------------- */
117 static void vimc_rm_links(struct vimc_device
*vimc
)
121 for (i
= 0; i
< vimc
->pipe_cfg
->num_ents
; i
++)
122 media_entity_remove_links(vimc
->ent_devs
[i
]->ent
);
125 static int vimc_create_links(struct vimc_device
*vimc
)
130 /* Initialize the links between entities */
131 for (i
= 0; i
< vimc
->pipe_cfg
->num_links
; i
++) {
132 const struct vimc_ent_link
*link
= &vimc
->pipe_cfg
->links
[i
];
134 struct vimc_ent_device
*ved_src
=
135 vimc
->ent_devs
[link
->src_ent
];
136 struct vimc_ent_device
*ved_sink
=
137 vimc
->ent_devs
[link
->sink_ent
];
139 ret
= media_create_pad_link(ved_src
->ent
, link
->src_pad
,
140 ved_sink
->ent
, link
->sink_pad
,
153 static void vimc_release_subdevs(struct vimc_device
*vimc
)
157 for (i
= 0; i
< vimc
->pipe_cfg
->num_ents
; i
++)
158 if (vimc
->ent_devs
[i
])
159 vimc
->pipe_cfg
->ents
[i
].type
->release(vimc
->ent_devs
[i
]);
162 static void vimc_unregister_subdevs(struct vimc_device
*vimc
)
166 for (i
= 0; i
< vimc
->pipe_cfg
->num_ents
; i
++)
167 if (vimc
->ent_devs
[i
] && vimc
->pipe_cfg
->ents
[i
].type
->unregister
)
168 vimc
->pipe_cfg
->ents
[i
].type
->unregister(vimc
->ent_devs
[i
]);
171 static int vimc_add_subdevs(struct vimc_device
*vimc
)
175 for (i
= 0; i
< vimc
->pipe_cfg
->num_ents
; i
++) {
176 dev_dbg(vimc
->mdev
.dev
, "new entity for %s\n",
177 vimc
->pipe_cfg
->ents
[i
].name
);
178 vimc
->ent_devs
[i
] = vimc
->pipe_cfg
->ents
[i
].type
->add(vimc
,
179 vimc
->pipe_cfg
->ents
[i
].name
);
180 if (IS_ERR(vimc
->ent_devs
[i
])) {
181 int err
= PTR_ERR(vimc
->ent_devs
[i
]);
183 dev_err(vimc
->mdev
.dev
, "adding entity %s failed (%d)\n",
184 vimc
->pipe_cfg
->ents
[i
].name
, err
);
185 vimc
->ent_devs
[i
] = NULL
;
186 vimc_unregister_subdevs(vimc
);
187 vimc_release_subdevs(vimc
);
194 static void vimc_v4l2_dev_release(struct v4l2_device
*v4l2_dev
)
196 struct vimc_device
*vimc
=
197 container_of(v4l2_dev
, struct vimc_device
, v4l2_dev
);
199 vimc_release_subdevs(vimc
);
200 media_device_cleanup(&vimc
->mdev
);
201 kfree(vimc
->ent_devs
);
205 static int vimc_register_devices(struct vimc_device
*vimc
)
209 /* Register the v4l2 struct */
210 ret
= v4l2_device_register(vimc
->mdev
.dev
, &vimc
->v4l2_dev
);
212 dev_err(vimc
->mdev
.dev
,
213 "v4l2 device register failed (err=%d)\n", ret
);
216 /* allocate ent_devs */
217 vimc
->ent_devs
= kcalloc(vimc
->pipe_cfg
->num_ents
,
218 sizeof(*vimc
->ent_devs
), GFP_KERNEL
);
219 if (!vimc
->ent_devs
) {
221 goto err_v4l2_unregister
;
224 /* Invoke entity config hooks to initialize and register subdevs */
225 ret
= vimc_add_subdevs(vimc
);
227 goto err_free_ent_devs
;
229 /* Initialize links */
230 ret
= vimc_create_links(vimc
);
234 /* Register the media device */
235 ret
= media_device_register(&vimc
->mdev
);
237 dev_err(vimc
->mdev
.dev
,
238 "media device register failed (err=%d)\n", ret
);
242 /* Expose all subdev's nodes*/
243 ret
= v4l2_device_register_subdev_nodes(&vimc
->v4l2_dev
);
245 dev_err(vimc
->mdev
.dev
,
246 "vimc subdev nodes registration failed (err=%d)\n",
248 goto err_mdev_unregister
;
254 media_device_unregister(&vimc
->mdev
);
256 vimc_unregister_subdevs(vimc
);
257 vimc_release_subdevs(vimc
);
259 kfree(vimc
->ent_devs
);
261 v4l2_device_unregister(&vimc
->v4l2_dev
);
266 static int vimc_probe(struct platform_device
*pdev
)
268 const struct font_desc
*font
= find_font("VGA8x16");
269 struct vimc_device
*vimc
;
272 dev_dbg(&pdev
->dev
, "probe");
275 dev_err(&pdev
->dev
, "could not find font\n");
279 tpg_set_font(font
->data
);
281 vimc
= kzalloc(sizeof(*vimc
), GFP_KERNEL
);
285 vimc
->pipe_cfg
= &pipe_cfg
;
287 /* Link the media device within the v4l2_device */
288 vimc
->v4l2_dev
.mdev
= &vimc
->mdev
;
290 /* Initialize media device */
291 strscpy(vimc
->mdev
.model
, VIMC_MDEV_MODEL_NAME
,
292 sizeof(vimc
->mdev
.model
));
293 snprintf(vimc
->mdev
.bus_info
, sizeof(vimc
->mdev
.bus_info
),
294 "platform:%s", VIMC_PDEV_NAME
);
295 vimc
->mdev
.dev
= &pdev
->dev
;
296 media_device_init(&vimc
->mdev
);
298 ret
= vimc_register_devices(vimc
);
300 media_device_cleanup(&vimc
->mdev
);
305 * the release cb is set only after successful registration.
306 * if the registration fails, we release directly from probe
309 vimc
->v4l2_dev
.release
= vimc_v4l2_dev_release
;
310 platform_set_drvdata(pdev
, vimc
);
314 static int vimc_remove(struct platform_device
*pdev
)
316 struct vimc_device
*vimc
= platform_get_drvdata(pdev
);
318 dev_dbg(&pdev
->dev
, "remove");
320 vimc_unregister_subdevs(vimc
);
321 media_device_unregister(&vimc
->mdev
);
322 v4l2_device_unregister(&vimc
->v4l2_dev
);
323 v4l2_device_put(&vimc
->v4l2_dev
);
328 static void vimc_dev_release(struct device
*dev
)
332 static struct platform_device vimc_pdev
= {
333 .name
= VIMC_PDEV_NAME
,
334 .dev
.release
= vimc_dev_release
,
337 static struct platform_driver vimc_pdrv
= {
339 .remove
= vimc_remove
,
341 .name
= VIMC_PDEV_NAME
,
345 static int __init
vimc_init(void)
349 ret
= platform_device_register(&vimc_pdev
);
351 dev_err(&vimc_pdev
.dev
,
352 "platform device registration failed (err=%d)\n", ret
);
356 ret
= platform_driver_register(&vimc_pdrv
);
358 dev_err(&vimc_pdev
.dev
,
359 "platform driver registration failed (err=%d)\n", ret
);
360 platform_driver_unregister(&vimc_pdrv
);
367 static void __exit
vimc_exit(void)
369 platform_driver_unregister(&vimc_pdrv
);
371 platform_device_unregister(&vimc_pdev
);
374 module_init(vimc_init
);
375 module_exit(vimc_exit
);
377 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC)");
378 MODULE_AUTHOR("Helen Fornazier <helen.fornazier@gmail.com>");
379 MODULE_LICENSE("GPL");