at v5.2-rc7 398 lines 9.6 kB view raw
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/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> 14 15#include "vimc-common.h" 16 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 { 28 /* The platform device */ 29 struct platform_device pdev; 30 31 /* The pipeline configuration */ 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 40 /* Subdevices */ 41 struct platform_device **subdevs; 42}; 43 44/* Structure which describes individual configuration for each entity */ 45struct vimc_ent_config { 46 const char *name; 47 const char *drv; 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", 74 .drv = "vimc-sensor", 75 }, 76 { 77 .name = "Sensor B", 78 .drv = "vimc-sensor", 79 }, 80 { 81 .name = "Debayer A", 82 .drv = "vimc-debayer", 83 }, 84 { 85 .name = "Debayer B", 86 .drv = "vimc-debayer", 87 }, 88 { 89 .name = "Raw Capture 0", 90 .drv = "vimc-capture", 91 }, 92 { 93 .name = "Raw Capture 1", 94 .drv = "vimc-capture", 95 }, 96 { 97 .name = "RGB/YUV Input", 98 /* TODO: change this to vimc-input when it is implemented */ 99 .drv = "vimc-sensor", 100 }, 101 { 102 .name = "Scaler", 103 .drv = "vimc-scaler", 104 }, 105 { 106 .name = "RGB/YUV Capture", 107 .drv = "vimc-capture", 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 139static int vimc_create_links(struct vimc_device *vimc) 140{ 141 unsigned int i; 142 int ret; 143 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; 161 } 162 163 return 0; 164} 165 166static int vimc_comp_bind(struct device *master) 167{ 168 struct vimc_device *vimc = container_of(to_platform_device(master), 169 struct vimc_device, pdev); 170 int ret; 171 172 dev_dbg(master, "bind"); 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 182 /* Bind subdevices */ 183 ret = component_bind_all(master, &vimc->v4l2_dev); 184 if (ret) 185 goto err_v4l2_unregister; 186 187 /* Initialize links */ 188 ret = vimc_create_links(vimc); 189 if (ret) 190 goto err_comp_unbind_all; 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); 197 goto err_comp_unbind_all; 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); 206 goto err_mdev_unregister; 207 } 208 209 return 0; 210 211err_mdev_unregister: 212 media_device_unregister(&vimc->mdev); 213 media_device_cleanup(&vimc->mdev); 214err_comp_unbind_all: 215 component_unbind_all(master, NULL); 216err_v4l2_unregister: 217 v4l2_device_unregister(&vimc->v4l2_dev); 218 219 return ret; 220} 221 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); 230 media_device_cleanup(&vimc->mdev); 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 253 strscpy(pdata.entity_name, vimc->pipe_cfg->ents[i].name, 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)); 261 if (IS_ERR(vimc->subdevs[i])) { 262 match = ERR_CAST(vimc->subdevs[i]); 263 while (--i >= 0) 264 platform_device_unregister(vimc->subdevs[i]); 265 266 return match; 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 289static int vimc_probe(struct platform_device *pdev) 290{ 291 struct vimc_device *vimc = container_of(pdev, struct vimc_device, pdev); 292 struct component_match *match = NULL; 293 int ret; 294 295 dev_dbg(&pdev->dev, "probe"); 296 297 memset(&vimc->mdev, 0, sizeof(vimc->mdev)); 298 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) 303 return -ENOMEM; 304 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; 311 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); 319 320 /* Add self to the component system */ 321 ret = component_master_add_with_match(&pdev->dev, &vimc_comp_ops, 322 match); 323 if (ret) { 324 media_device_cleanup(&vimc->mdev); 325 vimc_rm_subdevs(vimc); 326 return ret; 327 } 328 329 return 0; 330} 331 332static int vimc_remove(struct platform_device *pdev) 333{ 334 struct vimc_device *vimc = container_of(pdev, struct vimc_device, pdev); 335 336 dev_dbg(&pdev->dev, "remove"); 337 338 component_master_del(&pdev->dev, &vimc_comp_ops); 339 vimc_rm_subdevs(vimc); 340 341 return 0; 342} 343 344static void vimc_dev_release(struct device *dev) 345{ 346} 347 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 } 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 368 ret = platform_device_register(&vimc_dev.pdev); 369 if (ret) { 370 dev_err(&vimc_dev.pdev.dev, 371 "platform device registration failed (err=%d)\n", ret); 372 return ret; 373 } 374 375 ret = platform_driver_register(&vimc_pdrv); 376 if (ret) { 377 dev_err(&vimc_dev.pdev.dev, 378 "platform driver registration failed (err=%d)\n", ret); 379 platform_driver_unregister(&vimc_pdrv); 380 return ret; 381 } 382 383 return 0; 384} 385 386static void __exit vimc_exit(void) 387{ 388 platform_driver_unregister(&vimc_pdrv); 389 390 platform_device_unregister(&vimc_dev.pdev); 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");