1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * V4L2 Image Converter Subdev for Freescale i.MX5/6 SOC
4  *
5  * Copyright (c) 2014-2016 Mentor Graphics Inc.
6  */
7 #include <media/v4l2-device.h>
8 #include <media/v4l2-subdev.h>
9 #include "imx-media.h"
10 #include "imx-ic.h"
11 
12 #define IC_TASK_PRP IC_NUM_TASKS
13 #define IC_NUM_OPS  (IC_NUM_TASKS + 1)
14 
15 static struct imx_ic_ops *ic_ops[IC_NUM_OPS] = {
16 	[IC_TASK_PRP]            = &imx_ic_prp_ops,
17 	[IC_TASK_ENCODER]        = &imx_ic_prpencvf_ops,
18 	[IC_TASK_VIEWFINDER]     = &imx_ic_prpencvf_ops,
19 };
20 
imx_media_ic_register(struct v4l2_device * v4l2_dev,struct device * ipu_dev,struct ipu_soc * ipu,u32 grp_id)21 struct v4l2_subdev *imx_media_ic_register(struct v4l2_device *v4l2_dev,
22 					  struct device *ipu_dev,
23 					  struct ipu_soc *ipu,
24 					  u32 grp_id)
25 {
26 	struct imx_ic_priv *priv;
27 	int ret;
28 
29 	priv = devm_kzalloc(ipu_dev, sizeof(*priv), GFP_KERNEL);
30 	if (!priv)
31 		return ERR_PTR(-ENOMEM);
32 
33 	priv->ipu_dev = ipu_dev;
34 	priv->ipu = ipu;
35 
36 	/* get our IC task id */
37 	switch (grp_id) {
38 	case IMX_MEDIA_GRP_ID_IPU_IC_PRP:
39 		priv->task_id = IC_TASK_PRP;
40 		break;
41 	case IMX_MEDIA_GRP_ID_IPU_IC_PRPENC:
42 		priv->task_id = IC_TASK_ENCODER;
43 		break;
44 	case IMX_MEDIA_GRP_ID_IPU_IC_PRPVF:
45 		priv->task_id = IC_TASK_VIEWFINDER;
46 		break;
47 	default:
48 		return ERR_PTR(-EINVAL);
49 	}
50 
51 	v4l2_subdev_init(&priv->sd, ic_ops[priv->task_id]->subdev_ops);
52 	v4l2_set_subdevdata(&priv->sd, priv);
53 	priv->sd.internal_ops = ic_ops[priv->task_id]->internal_ops;
54 	priv->sd.entity.ops = ic_ops[priv->task_id]->entity_ops;
55 	priv->sd.entity.function = MEDIA_ENT_F_PROC_VIDEO_SCALER;
56 	priv->sd.owner = ipu_dev->driver->owner;
57 	priv->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
58 	priv->sd.grp_id = grp_id;
59 	imx_media_grp_id_to_sd_name(priv->sd.name, sizeof(priv->sd.name),
60 				    priv->sd.grp_id, ipu_get_num(ipu));
61 
62 	ret = ic_ops[priv->task_id]->init(priv);
63 	if (ret)
64 		return ERR_PTR(ret);
65 
66 	ret = v4l2_device_register_subdev(v4l2_dev, &priv->sd);
67 	if (ret) {
68 		ic_ops[priv->task_id]->remove(priv);
69 		return ERR_PTR(ret);
70 	}
71 
72 	return &priv->sd;
73 }
74 
imx_media_ic_unregister(struct v4l2_subdev * sd)75 int imx_media_ic_unregister(struct v4l2_subdev *sd)
76 {
77 	struct imx_ic_priv *priv = container_of(sd, struct imx_ic_priv, sd);
78 
79 	v4l2_info(sd, "Removing\n");
80 
81 	ic_ops[priv->task_id]->remove(priv);
82 
83 	v4l2_device_unregister_subdev(sd);
84 	media_entity_cleanup(&sd->entity);
85 
86 	return 0;
87 }
88