]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/platform/vimc/vimc-core.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / platform / vimc / vimc-core.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
f2fe8906
HK
2/*
3 * vimc-core.c Virtual Media Controller Driver
4 *
5 * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
f2fe8906
HK
6 */
7
4a29b709 8#include <linux/component.h>
f2fe8906
HK
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>
14
5ba0ae43 15#include "vimc-common.h"
f2fe8906 16
f2fe8906
HK
17#define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
18
19#define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) { \
20 .src_ent = src, \
21 .src_pad = srcpad, \
22 .sink_ent = sink, \
23 .sink_pad = sinkpad, \
24 .flags = link_flags, \
25}
26
27struct vimc_device {
4a29b709
HF
28 /* The platform device */
29 struct platform_device pdev;
30
31 /* The pipeline configuration */
f2fe8906
HK
32 const struct vimc_pipeline_config *pipe_cfg;
33
34 /* The Associated media_device parent */
35 struct media_device mdev;
36
37 /* Internal v4l2 parent device*/
38 struct v4l2_device v4l2_dev;
39
4a29b709
HF
40 /* Subdevices */
41 struct platform_device **subdevs;
f2fe8906
HK
42};
43
44/* Structure which describes individual configuration for each entity */
45struct vimc_ent_config {
46 const char *name;
4a29b709 47 const char *drv;
f2fe8906
HK
48};
49
50/* Structure which describes links between entities */
51struct vimc_ent_link {
52 unsigned int src_ent;
53 u16 src_pad;
54 unsigned int sink_ent;
55 u16 sink_pad;
56 u32 flags;
57};
58
59/* Structure which describes the whole topology */
60struct vimc_pipeline_config {
61 const struct vimc_ent_config *ents;
62 size_t num_ents;
63 const struct vimc_ent_link *links;
64 size_t num_links;
65};
66
67/* --------------------------------------------------------------------------
68 * Topology Configuration
69 */
70
71static const struct vimc_ent_config ent_config[] = {
72 {
73 .name = "Sensor A",
4a29b709 74 .drv = "vimc-sensor",
f2fe8906
HK
75 },
76 {
77 .name = "Sensor B",
4a29b709 78 .drv = "vimc-sensor",
f2fe8906
HK
79 },
80 {
81 .name = "Debayer A",
4a29b709 82 .drv = "vimc-debayer",
f2fe8906
HK
83 },
84 {
85 .name = "Debayer B",
4a29b709 86 .drv = "vimc-debayer",
f2fe8906
HK
87 },
88 {
89 .name = "Raw Capture 0",
4a29b709 90 .drv = "vimc-capture",
f2fe8906
HK
91 },
92 {
93 .name = "Raw Capture 1",
4a29b709 94 .drv = "vimc-capture",
f2fe8906
HK
95 },
96 {
97 .name = "RGB/YUV Input",
4a29b709
HF
98 /* TODO: change this to vimc-input when it is implemented */
99 .drv = "vimc-sensor",
f2fe8906
HK
100 },
101 {
102 .name = "Scaler",
4a29b709 103 .drv = "vimc-scaler",
f2fe8906
HK
104 },
105 {
106 .name = "RGB/YUV Capture",
4a29b709 107 .drv = "vimc-capture",
f2fe8906
HK
108 },
109};
110
111static 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),
128};
129
130static const struct vimc_pipeline_config pipe_cfg = {
131 .ents = ent_config,
132 .num_ents = ARRAY_SIZE(ent_config),
133 .links = ent_links,
134 .num_links = ARRAY_SIZE(ent_links)
135};
136
137/* -------------------------------------------------------------------------- */
138
4a29b709 139static int vimc_create_links(struct vimc_device *vimc)
f2fe8906
HK
140{
141 unsigned int i;
f2fe8906
HK
142 int ret;
143
4a29b709
HF
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];
147 /*
148 * TODO: Check another way of retrieving ved struct without
149 * relying on platform_get_drvdata
150 */
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]);
155
156 ret = media_create_pad_link(ved_src->ent, link->src_pad,
157 ved_sink->ent, link->sink_pad,
158 link->flags);
159 if (ret)
160 return ret;
f2fe8906
HK
161 }
162
4a29b709 163 return 0;
f2fe8906
HK
164}
165
4a29b709 166static int vimc_comp_bind(struct device *master)
f2fe8906 167{
4a29b709
HF
168 struct vimc_device *vimc = container_of(to_platform_device(master),
169 struct vimc_device, pdev);
f2fe8906
HK
170 int ret;
171
4a29b709 172 dev_dbg(master, "bind");
f2fe8906
HK
173
174 /* Register the v4l2 struct */
175 ret = v4l2_device_register(vimc->mdev.dev, &vimc->v4l2_dev);
176 if (ret) {
177 dev_err(vimc->mdev.dev,
178 "v4l2 device register failed (err=%d)\n", ret);
179 return ret;
180 }
181
4a29b709
HF
182 /* Bind subdevices */
183 ret = component_bind_all(master, &vimc->v4l2_dev);
184 if (ret)
185 goto err_v4l2_unregister;
f2fe8906 186
4a29b709
HF
187 /* Initialize links */
188 ret = vimc_create_links(vimc);
189 if (ret)
190 goto err_comp_unbind_all;
f2fe8906
HK
191
192 /* Register the media device */
193 ret = media_device_register(&vimc->mdev);
194 if (ret) {
195 dev_err(vimc->mdev.dev,
196 "media device register failed (err=%d)\n", ret);
4a29b709 197 goto err_comp_unbind_all;
f2fe8906
HK
198 }
199
200 /* Expose all subdev's nodes*/
201 ret = v4l2_device_register_subdev_nodes(&vimc->v4l2_dev);
202 if (ret) {
203 dev_err(vimc->mdev.dev,
204 "vimc subdev nodes registration failed (err=%d)\n",
205 ret);
4a29b709 206 goto err_mdev_unregister;
f2fe8906
HK
207 }
208
209 return 0;
210
4a29b709
HF
211err_mdev_unregister:
212 media_device_unregister(&vimc->mdev);
cdabfa84 213 media_device_cleanup(&vimc->mdev);
4a29b709
HF
214err_comp_unbind_all:
215 component_unbind_all(master, NULL);
216err_v4l2_unregister:
217 v4l2_device_unregister(&vimc->v4l2_dev);
f2fe8906
HK
218
219 return ret;
220}
221
4a29b709
HF
222static void vimc_comp_unbind(struct device *master)
223{
224 struct vimc_device *vimc = container_of(to_platform_device(master),
225 struct vimc_device, pdev);
226
227 dev_dbg(master, "unbind");
228
229 media_device_unregister(&vimc->mdev);
cdabfa84 230 media_device_cleanup(&vimc->mdev);
4a29b709
HF
231 component_unbind_all(master, NULL);
232 v4l2_device_unregister(&vimc->v4l2_dev);
233}
234
235static int vimc_comp_compare(struct device *comp, void *data)
236{
237 const struct platform_device *pdev = to_platform_device(comp);
238 const char *name = data;
239
240 return !strcmp(pdev->dev.platform_data, name);
241}
242
243static struct component_match *vimc_add_subdevs(struct vimc_device *vimc)
244{
245 struct component_match *match = NULL;
246 struct vimc_platform_data pdata;
247 int i;
248
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);
252
c0decac1 253 strscpy(pdata.entity_name, vimc->pipe_cfg->ents[i].name,
4a29b709
HF
254 sizeof(pdata.entity_name));
255
256 vimc->subdevs[i] = platform_device_register_data(&vimc->pdev.dev,
257 vimc->pipe_cfg->ents[i].drv,
258 PLATFORM_DEVID_AUTO,
259 &pdata,
260 sizeof(pdata));
3863360c
WY
261 if (IS_ERR(vimc->subdevs[i])) {
262 match = ERR_CAST(vimc->subdevs[i]);
4a29b709
HF
263 while (--i >= 0)
264 platform_device_unregister(vimc->subdevs[i]);
265
3863360c 266 return match;
4a29b709
HF
267 }
268
269 component_match_add(&vimc->pdev.dev, &match, vimc_comp_compare,
270 (void *)vimc->pipe_cfg->ents[i].name);
271 }
272
273 return match;
274}
275
276static void vimc_rm_subdevs(struct vimc_device *vimc)
277{
278 unsigned int i;
279
280 for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
281 platform_device_unregister(vimc->subdevs[i]);
282}
283
284static const struct component_master_ops vimc_comp_ops = {
285 .bind = vimc_comp_bind,
286 .unbind = vimc_comp_unbind,
287};
288
f2fe8906
HK
289static int vimc_probe(struct platform_device *pdev)
290{
4a29b709
HF
291 struct vimc_device *vimc = container_of(pdev, struct vimc_device, pdev);
292 struct component_match *match = NULL;
f2fe8906
HK
293 int ret;
294
4a29b709 295 dev_dbg(&pdev->dev, "probe");
f2fe8906 296
f74267b5
HV
297 memset(&vimc->mdev, 0, sizeof(vimc->mdev));
298
4a29b709
HF
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);
302 if (!vimc->subdevs)
f2fe8906
HK
303 return -ENOMEM;
304
4a29b709
HF
305 match = vimc_add_subdevs(vimc);
306 if (IS_ERR(match))
307 return PTR_ERR(match);
308
309 /* Link the media device within the v4l2_device */
310 vimc->v4l2_dev.mdev = &vimc->mdev;
f2fe8906
HK
311
312 /* Initialize media device */
c0decac1 313 strscpy(vimc->mdev.model, VIMC_MDEV_MODEL_NAME,
f2fe8906 314 sizeof(vimc->mdev.model));
6fd369dd
HV
315 snprintf(vimc->mdev.bus_info, sizeof(vimc->mdev.bus_info),
316 "platform:%s", VIMC_PDEV_NAME);
f2fe8906
HK
317 vimc->mdev.dev = &pdev->dev;
318 media_device_init(&vimc->mdev);
319
4a29b709
HF
320 /* Add self to the component system */
321 ret = component_master_add_with_match(&pdev->dev, &vimc_comp_ops,
322 match);
f2fe8906 323 if (ret) {
4a29b709
HF
324 media_device_cleanup(&vimc->mdev);
325 vimc_rm_subdevs(vimc);
f2fe8906
HK
326 return ret;
327 }
328
f2fe8906
HK
329 return 0;
330}
331
332static int vimc_remove(struct platform_device *pdev)
333{
4a29b709 334 struct vimc_device *vimc = container_of(pdev, struct vimc_device, pdev);
f2fe8906 335
4a29b709
HF
336 dev_dbg(&pdev->dev, "remove");
337
338 component_master_del(&pdev->dev, &vimc_comp_ops);
339 vimc_rm_subdevs(vimc);
f2fe8906
HK
340
341 return 0;
342}
343
344static void vimc_dev_release(struct device *dev)
345{
346}
347
4a29b709
HF
348static struct vimc_device vimc_dev = {
349 .pipe_cfg = &pipe_cfg,
350 .pdev = {
351 .name = VIMC_PDEV_NAME,
352 .dev.release = vimc_dev_release,
353 }
f2fe8906
HK
354};
355
356static struct platform_driver vimc_pdrv = {
357 .probe = vimc_probe,
358 .remove = vimc_remove,
359 .driver = {
360 .name = VIMC_PDEV_NAME,
361 },
362};
363
364static int __init vimc_init(void)
365{
366 int ret;
367
4a29b709 368 ret = platform_device_register(&vimc_dev.pdev);
f2fe8906 369 if (ret) {
4a29b709 370 dev_err(&vimc_dev.pdev.dev,
f2fe8906
HK
371 "platform device registration failed (err=%d)\n", ret);
372 return ret;
373 }
374
375 ret = platform_driver_register(&vimc_pdrv);
376 if (ret) {
4a29b709 377 dev_err(&vimc_dev.pdev.dev,
f2fe8906 378 "platform driver registration failed (err=%d)\n", ret);
4a29b709
HF
379 platform_driver_unregister(&vimc_pdrv);
380 return ret;
f2fe8906
HK
381 }
382
4a29b709 383 return 0;
f2fe8906
HK
384}
385
386static void __exit vimc_exit(void)
387{
388 platform_driver_unregister(&vimc_pdrv);
389
4a29b709 390 platform_device_unregister(&vimc_dev.pdev);
f2fe8906
HK
391}
392
393module_init(vimc_init);
394module_exit(vimc_exit);
395
396MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC)");
397MODULE_AUTHOR("Helen Fornazier <helen.fornazier@gmail.com>");
398MODULE_LICENSE("GPL");