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/component.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/v4l2-device.h>
15 #include "vimc-common.h"
17 #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
19 #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) { \
23 .sink_pad = sinkpad, \
24 .flags = link_flags, \
28 /* The platform device */
29 struct platform_device pdev
;
31 /* The pipeline configuration */
32 const struct vimc_pipeline_config
*pipe_cfg
;
34 /* The Associated media_device parent */
35 struct media_device mdev
;
37 /* Internal v4l2 parent device*/
38 struct v4l2_device v4l2_dev
;
41 struct platform_device
**subdevs
;
44 /* Structure which describes individual configuration for each entity */
45 struct vimc_ent_config
{
50 /* Structure which describes links between entities */
51 struct vimc_ent_link
{
54 unsigned int sink_ent
;
59 /* Structure which describes the whole topology */
60 struct vimc_pipeline_config
{
61 const struct vimc_ent_config
*ents
;
63 const struct vimc_ent_link
*links
;
67 /* --------------------------------------------------------------------------
68 * Topology Configuration
71 static const struct vimc_ent_config ent_config
[] = {
82 .drv
= "vimc-debayer",
86 .drv
= "vimc-debayer",
89 .name
= "Raw Capture 0",
90 .drv
= "vimc-capture",
93 .name
= "Raw Capture 1",
94 .drv
= "vimc-capture",
97 .name
= "RGB/YUV Input",
98 /* TODO: change this to vimc-input when it is implemented */
103 .drv
= "vimc-scaler",
106 .name
= "RGB/YUV Capture",
107 .drv
= "vimc-capture",
111 static const struct vimc_ent_link ent_links
[] = {
112 /* Link: Sensor A (Pad 0)->(Pad 0) Debayer A */
113 VIMC_ENT_LINK(0, 0, 2, 0, MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
),
114 /* Link: Sensor A (Pad 0)->(Pad 0) Raw Capture 0 */
115 VIMC_ENT_LINK(0, 0, 4, 0, MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
),
116 /* Link: Sensor B (Pad 0)->(Pad 0) Debayer B */
117 VIMC_ENT_LINK(1, 0, 3, 0, MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
),
118 /* Link: Sensor B (Pad 0)->(Pad 0) Raw Capture 1 */
119 VIMC_ENT_LINK(1, 0, 5, 0, MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
),
120 /* Link: Debayer A (Pad 1)->(Pad 0) Scaler */
121 VIMC_ENT_LINK(2, 1, 7, 0, MEDIA_LNK_FL_ENABLED
),
122 /* Link: Debayer B (Pad 1)->(Pad 0) Scaler */
123 VIMC_ENT_LINK(3, 1, 7, 0, 0),
124 /* Link: RGB/YUV Input (Pad 0)->(Pad 0) Scaler */
125 VIMC_ENT_LINK(6, 0, 7, 0, 0),
126 /* Link: Scaler (Pad 1)->(Pad 0) RGB/YUV Capture */
127 VIMC_ENT_LINK(7, 1, 8, 0, MEDIA_LNK_FL_ENABLED
| MEDIA_LNK_FL_IMMUTABLE
),
130 static const struct vimc_pipeline_config pipe_cfg
= {
132 .num_ents
= ARRAY_SIZE(ent_config
),
134 .num_links
= ARRAY_SIZE(ent_links
)
137 /* -------------------------------------------------------------------------- */
139 static int vimc_create_links(struct vimc_device
*vimc
)
144 /* Initialize the links between entities */
145 for (i
= 0; i
< vimc
->pipe_cfg
->num_links
; i
++) {
146 const struct vimc_ent_link
*link
= &vimc
->pipe_cfg
->links
[i
];
148 * TODO: Check another way of retrieving ved struct without
149 * relying on platform_get_drvdata
151 struct vimc_ent_device
*ved_src
=
152 platform_get_drvdata(vimc
->subdevs
[link
->src_ent
]);
153 struct vimc_ent_device
*ved_sink
=
154 platform_get_drvdata(vimc
->subdevs
[link
->sink_ent
]);
156 ret
= media_create_pad_link(ved_src
->ent
, link
->src_pad
,
157 ved_sink
->ent
, link
->sink_pad
,
166 static int vimc_comp_bind(struct device
*master
)
168 struct vimc_device
*vimc
= container_of(to_platform_device(master
),
169 struct vimc_device
, pdev
);
172 dev_dbg(master
, "bind");
174 /* Register the v4l2 struct */
175 ret
= v4l2_device_register(vimc
->mdev
.dev
, &vimc
->v4l2_dev
);
177 dev_err(vimc
->mdev
.dev
,
178 "v4l2 device register failed (err=%d)\n", ret
);
182 /* Bind subdevices */
183 ret
= component_bind_all(master
, &vimc
->v4l2_dev
);
185 goto err_v4l2_unregister
;
187 /* Initialize links */
188 ret
= vimc_create_links(vimc
);
190 goto err_comp_unbind_all
;
192 /* Register the media device */
193 ret
= media_device_register(&vimc
->mdev
);
195 dev_err(vimc
->mdev
.dev
,
196 "media device register failed (err=%d)\n", ret
);
197 goto err_comp_unbind_all
;
200 /* Expose all subdev's nodes*/
201 ret
= v4l2_device_register_subdev_nodes(&vimc
->v4l2_dev
);
203 dev_err(vimc
->mdev
.dev
,
204 "vimc subdev nodes registration failed (err=%d)\n",
206 goto err_mdev_unregister
;
212 media_device_unregister(&vimc
->mdev
);
213 media_device_cleanup(&vimc
->mdev
);
215 component_unbind_all(master
, NULL
);
217 v4l2_device_unregister(&vimc
->v4l2_dev
);
222 static void vimc_comp_unbind(struct device
*master
)
224 struct vimc_device
*vimc
= container_of(to_platform_device(master
),
225 struct vimc_device
, pdev
);
227 dev_dbg(master
, "unbind");
229 media_device_unregister(&vimc
->mdev
);
230 media_device_cleanup(&vimc
->mdev
);
231 component_unbind_all(master
, NULL
);
232 v4l2_device_unregister(&vimc
->v4l2_dev
);
235 static int vimc_comp_compare(struct device
*comp
, void *data
)
237 const struct platform_device
*pdev
= to_platform_device(comp
);
238 const char *name
= data
;
240 return !strcmp(pdev
->dev
.platform_data
, name
);
243 static struct component_match
*vimc_add_subdevs(struct vimc_device
*vimc
)
245 struct component_match
*match
= NULL
;
246 struct vimc_platform_data pdata
;
249 for (i
= 0; i
< vimc
->pipe_cfg
->num_ents
; i
++) {
250 dev_dbg(&vimc
->pdev
.dev
, "new pdev for %s\n",
251 vimc
->pipe_cfg
->ents
[i
].drv
);
253 strscpy(pdata
.entity_name
, vimc
->pipe_cfg
->ents
[i
].name
,
254 sizeof(pdata
.entity_name
));
256 vimc
->subdevs
[i
] = platform_device_register_data(&vimc
->pdev
.dev
,
257 vimc
->pipe_cfg
->ents
[i
].drv
,
261 if (IS_ERR(vimc
->subdevs
[i
])) {
262 match
= ERR_CAST(vimc
->subdevs
[i
]);
264 platform_device_unregister(vimc
->subdevs
[i
]);
269 component_match_add(&vimc
->pdev
.dev
, &match
, vimc_comp_compare
,
270 (void *)vimc
->pipe_cfg
->ents
[i
].name
);
276 static void vimc_rm_subdevs(struct vimc_device
*vimc
)
280 for (i
= 0; i
< vimc
->pipe_cfg
->num_ents
; i
++)
281 platform_device_unregister(vimc
->subdevs
[i
]);
284 static const struct component_master_ops vimc_comp_ops
= {
285 .bind
= vimc_comp_bind
,
286 .unbind
= vimc_comp_unbind
,
289 static int vimc_probe(struct platform_device
*pdev
)
291 struct vimc_device
*vimc
= container_of(pdev
, struct vimc_device
, pdev
);
292 struct component_match
*match
= NULL
;
295 dev_dbg(&pdev
->dev
, "probe");
297 memset(&vimc
->mdev
, 0, sizeof(vimc
->mdev
));
299 /* Create platform_device for each entity in the topology*/
300 vimc
->subdevs
= devm_kcalloc(&vimc
->pdev
.dev
, vimc
->pipe_cfg
->num_ents
,
301 sizeof(*vimc
->subdevs
), GFP_KERNEL
);
305 match
= vimc_add_subdevs(vimc
);
307 return PTR_ERR(match
);
309 /* Link the media device within the v4l2_device */
310 vimc
->v4l2_dev
.mdev
= &vimc
->mdev
;
312 /* Initialize media device */
313 strscpy(vimc
->mdev
.model
, VIMC_MDEV_MODEL_NAME
,
314 sizeof(vimc
->mdev
.model
));
315 snprintf(vimc
->mdev
.bus_info
, sizeof(vimc
->mdev
.bus_info
),
316 "platform:%s", VIMC_PDEV_NAME
);
317 vimc
->mdev
.dev
= &pdev
->dev
;
318 media_device_init(&vimc
->mdev
);
320 /* Add self to the component system */
321 ret
= component_master_add_with_match(&pdev
->dev
, &vimc_comp_ops
,
324 media_device_cleanup(&vimc
->mdev
);
325 vimc_rm_subdevs(vimc
);
332 static int vimc_remove(struct platform_device
*pdev
)
334 struct vimc_device
*vimc
= container_of(pdev
, struct vimc_device
, pdev
);
336 dev_dbg(&pdev
->dev
, "remove");
338 component_master_del(&pdev
->dev
, &vimc_comp_ops
);
339 vimc_rm_subdevs(vimc
);
344 static void vimc_dev_release(struct device
*dev
)
348 static struct vimc_device vimc_dev
= {
349 .pipe_cfg
= &pipe_cfg
,
351 .name
= VIMC_PDEV_NAME
,
352 .dev
.release
= vimc_dev_release
,
356 static struct platform_driver vimc_pdrv
= {
358 .remove
= vimc_remove
,
360 .name
= VIMC_PDEV_NAME
,
364 static int __init
vimc_init(void)
368 ret
= platform_device_register(&vimc_dev
.pdev
);
370 dev_err(&vimc_dev
.pdev
.dev
,
371 "platform device registration failed (err=%d)\n", ret
);
375 ret
= platform_driver_register(&vimc_pdrv
);
377 dev_err(&vimc_dev
.pdev
.dev
,
378 "platform driver registration failed (err=%d)\n", ret
);
379 platform_driver_unregister(&vimc_pdrv
);
386 static void __exit
vimc_exit(void)
388 platform_driver_unregister(&vimc_pdrv
);
390 platform_device_unregister(&vimc_dev
.pdev
);
393 module_init(vimc_init
);
394 module_exit(vimc_exit
);
396 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC)");
397 MODULE_AUTHOR("Helen Fornazier <helen.fornazier@gmail.com>");
398 MODULE_LICENSE("GPL");