media: media/test_drivers: rename to test-drivers
[linux-2.6-microblaze.git] / drivers / media / test-drivers / vimc / vimc-core.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * vimc-core.c Virtual Media Controller Driver
4  *
5  * Copyright (C) 2015-2017 Helen Koike <helen.fornazier@gmail.com>
6  */
7
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>
13
14 #include "vimc-common.h"
15
16 #define VIMC_MDEV_MODEL_NAME "VIMC MDEV"
17
18 #define VIMC_ENT_LINK(src, srcpad, sink, sinkpad, link_flags) { \
19         .src_ent = src,                                         \
20         .src_pad = srcpad,                                      \
21         .sink_ent = sink,                                       \
22         .sink_pad = sinkpad,                                    \
23         .flags = link_flags,                                    \
24 }
25
26 /* Structure which describes links between entities */
27 struct vimc_ent_link {
28         unsigned int src_ent;
29         u16 src_pad;
30         unsigned int sink_ent;
31         u16 sink_pad;
32         u32 flags;
33 };
34
35 /* Structure which describes the whole topology */
36 struct vimc_pipeline_config {
37         const struct vimc_ent_config *ents;
38         size_t num_ents;
39         const struct vimc_ent_link *links;
40         size_t num_links;
41 };
42
43 /* --------------------------------------------------------------------------
44  * Topology Configuration
45  */
46
47 static struct vimc_ent_config ent_config[] = {
48         {
49                 .name = "Sensor A",
50                 .type = &vimc_sen_type
51         },
52         {
53                 .name = "Sensor B",
54                 .type = &vimc_sen_type
55         },
56         {
57                 .name = "Debayer A",
58                 .type = &vimc_deb_type
59         },
60         {
61                 .name = "Debayer B",
62                 .type = &vimc_deb_type
63         },
64         {
65                 .name = "Raw Capture 0",
66                 .type = &vimc_cap_type
67         },
68         {
69                 .name = "Raw Capture 1",
70                 .type = &vimc_cap_type
71         },
72         {
73                 /* TODO: change this to vimc-input when it is implemented */
74                 .name = "RGB/YUV Input",
75                 .type = &vimc_sen_type
76         },
77         {
78                 .name = "Scaler",
79                 .type = &vimc_sca_type
80         },
81         {
82                 .name = "RGB/YUV Capture",
83                 .type = &vimc_cap_type
84         },
85 };
86
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),
104 };
105
106 static struct vimc_pipeline_config pipe_cfg = {
107         .ents           = ent_config,
108         .num_ents       = ARRAY_SIZE(ent_config),
109         .links          = ent_links,
110         .num_links      = ARRAY_SIZE(ent_links)
111 };
112
113 /* -------------------------------------------------------------------------- */
114
115 static void vimc_rm_links(struct vimc_device *vimc)
116 {
117         unsigned int i;
118
119         for (i = 0; i < vimc->pipe_cfg->num_ents; i++)
120                 media_entity_remove_links(vimc->ent_devs[i]->ent);
121 }
122
123 static int vimc_create_links(struct vimc_device *vimc)
124 {
125         unsigned int i;
126         int ret;
127
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];
131
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];
136
137                 ret = media_create_pad_link(ved_src->ent, link->src_pad,
138                                             ved_sink->ent, link->sink_pad,
139                                             link->flags);
140                 if (ret)
141                         goto err_rm_links;
142         }
143
144         return 0;
145
146 err_rm_links:
147         vimc_rm_links(vimc);
148         return ret;
149 }
150
151 static void vimc_release_subdevs(struct vimc_device *vimc)
152 {
153         unsigned int i;
154
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]);
158 }
159
160 static void vimc_unregister_subdevs(struct vimc_device *vimc)
161 {
162         unsigned int i;
163
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]);
167 }
168
169 static int vimc_add_subdevs(struct vimc_device *vimc)
170 {
171         unsigned int i;
172
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]);
180
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);
186                         return err;
187                 }
188         }
189         return 0;
190 }
191
192 static void vimc_v4l2_dev_release(struct v4l2_device *v4l2_dev)
193 {
194         struct vimc_device *vimc =
195                 container_of(v4l2_dev, struct vimc_device, v4l2_dev);
196
197         vimc_release_subdevs(vimc);
198         media_device_cleanup(&vimc->mdev);
199         kfree(vimc->ent_devs);
200         kfree(vimc);
201 }
202
203 static int vimc_register_devices(struct vimc_device *vimc)
204 {
205         int ret;
206
207         /* Register the v4l2 struct */
208         ret = v4l2_device_register(vimc->mdev.dev, &vimc->v4l2_dev);
209         if (ret) {
210                 dev_err(vimc->mdev.dev,
211                         "v4l2 device register failed (err=%d)\n", ret);
212                 return ret;
213         }
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) {
218                 ret = -ENOMEM;
219                 goto err_v4l2_unregister;
220         }
221
222         /* Invoke entity config hooks to initialize and register subdevs */
223         ret = vimc_add_subdevs(vimc);
224         if (ret)
225                 goto err_free_ent_devs;
226
227         /* Initialize links */
228         ret = vimc_create_links(vimc);
229         if (ret)
230                 goto err_rm_subdevs;
231
232         /* Register the media device */
233         ret = media_device_register(&vimc->mdev);
234         if (ret) {
235                 dev_err(vimc->mdev.dev,
236                         "media device register failed (err=%d)\n", ret);
237                 goto err_rm_subdevs;
238         }
239
240         /* Expose all subdev's nodes*/
241         ret = v4l2_device_register_subdev_nodes(&vimc->v4l2_dev);
242         if (ret) {
243                 dev_err(vimc->mdev.dev,
244                         "vimc subdev nodes registration failed (err=%d)\n",
245                         ret);
246                 goto err_mdev_unregister;
247         }
248
249         return 0;
250
251 err_mdev_unregister:
252         media_device_unregister(&vimc->mdev);
253 err_rm_subdevs:
254         vimc_unregister_subdevs(vimc);
255         vimc_release_subdevs(vimc);
256 err_free_ent_devs:
257         kfree(vimc->ent_devs);
258 err_v4l2_unregister:
259         v4l2_device_unregister(&vimc->v4l2_dev);
260
261         return ret;
262 }
263
264 static int vimc_probe(struct platform_device *pdev)
265 {
266         struct vimc_device *vimc;
267         int ret;
268
269         dev_dbg(&pdev->dev, "probe");
270
271         vimc = kzalloc(sizeof(*vimc), GFP_KERNEL);
272         if (!vimc)
273                 return -ENOMEM;
274
275         vimc->pipe_cfg = &pipe_cfg;
276
277         /* Link the media device within the v4l2_device */
278         vimc->v4l2_dev.mdev = &vimc->mdev;
279
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);
287
288         ret = vimc_register_devices(vimc);
289         if (ret) {
290                 media_device_cleanup(&vimc->mdev);
291                 kfree(vimc);
292                 return ret;
293         }
294         /*
295          * the release cb is set only after successful registration.
296          * if the registration fails, we release directly from probe
297          */
298
299         vimc->v4l2_dev.release = vimc_v4l2_dev_release;
300         platform_set_drvdata(pdev, vimc);
301         return 0;
302 }
303
304 static int vimc_remove(struct platform_device *pdev)
305 {
306         struct vimc_device *vimc = platform_get_drvdata(pdev);
307
308         dev_dbg(&pdev->dev, "remove");
309
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);
314
315         return 0;
316 }
317
318 static void vimc_dev_release(struct device *dev)
319 {
320 }
321
322 static struct platform_device vimc_pdev = {
323         .name = VIMC_PDEV_NAME,
324         .dev.release = vimc_dev_release,
325 };
326
327 static struct platform_driver vimc_pdrv = {
328         .probe          = vimc_probe,
329         .remove         = vimc_remove,
330         .driver         = {
331                 .name   = VIMC_PDEV_NAME,
332         },
333 };
334
335 static int __init vimc_init(void)
336 {
337         int ret;
338
339         ret = platform_device_register(&vimc_pdev);
340         if (ret) {
341                 dev_err(&vimc_pdev.dev,
342                         "platform device registration failed (err=%d)\n", ret);
343                 return ret;
344         }
345
346         ret = platform_driver_register(&vimc_pdrv);
347         if (ret) {
348                 dev_err(&vimc_pdev.dev,
349                         "platform driver registration failed (err=%d)\n", ret);
350                 platform_driver_unregister(&vimc_pdrv);
351                 return ret;
352         }
353
354         return 0;
355 }
356
357 static void __exit vimc_exit(void)
358 {
359         platform_driver_unregister(&vimc_pdrv);
360
361         platform_device_unregister(&vimc_pdev);
362 }
363
364 module_init(vimc_init);
365 module_exit(vimc_exit);
366
367 MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC)");
368 MODULE_AUTHOR("Helen Fornazier <helen.fornazier@gmail.com>");
369 MODULE_LICENSE("GPL");