1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * V4L2 Media Controller Driver for Freescale i.MX5/6 SOC
4  *
5  * Copyright (c) 2016-2019 Mentor Graphics Inc.
6  */
7 #include <linux/fs.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <media/v4l2-async.h>
11 #include <media/v4l2-event.h>
12 #include <media/imx.h>
13 #include "imx-media.h"
14 
15 static inline struct imx_media_dev *notifier2dev(struct v4l2_async_notifier *n)
16 {
17 	return container_of(n, struct imx_media_dev, notifier);
18 }
19 
20 /* async subdev bound notifier */
21 static int imx_media_subdev_bound(struct v4l2_async_notifier *notifier,
22 				  struct v4l2_subdev *sd,
23 				  struct v4l2_async_subdev *asd)
24 {
25 	struct imx_media_dev *imxmd = notifier2dev(notifier);
26 	int ret;
27 
28 	if (sd->grp_id & IMX_MEDIA_GRP_ID_IPU_CSI) {
29 		/* register the IPU internal subdevs */
30 		ret = imx_media_register_ipu_internal_subdevs(imxmd, sd);
31 		if (ret)
32 			return ret;
33 	}
34 
35 	dev_dbg(imxmd->md.dev, "subdev %s bound\n", sd->name);
36 
37 	return 0;
38 }
39 
40 /* async subdev complete notifier */
41 static int imx6_media_probe_complete(struct v4l2_async_notifier *notifier)
42 {
43 	struct imx_media_dev *imxmd = notifier2dev(notifier);
44 	int ret;
45 
46 	/* call the imx5/6/7 common probe completion handler */
47 	ret = imx_media_probe_complete(notifier);
48 	if (ret)
49 		return ret;
50 
51 	mutex_lock(&imxmd->mutex);
52 
53 	imxmd->m2m_vdev = imx_media_csc_scaler_device_init(imxmd);
54 	if (IS_ERR(imxmd->m2m_vdev)) {
55 		ret = PTR_ERR(imxmd->m2m_vdev);
56 		goto unlock;
57 	}
58 
59 	ret = imx_media_csc_scaler_device_register(imxmd->m2m_vdev);
60 unlock:
61 	mutex_unlock(&imxmd->mutex);
62 	return ret;
63 }
64 
65 /* async subdev complete notifier */
66 static const struct v4l2_async_notifier_operations imx_media_notifier_ops = {
67 	.bound = imx_media_subdev_bound,
68 	.complete = imx6_media_probe_complete,
69 };
70 
71 static int imx_media_probe(struct platform_device *pdev)
72 {
73 	struct device *dev = &pdev->dev;
74 	struct device_node *node = dev->of_node;
75 	struct imx_media_dev *imxmd;
76 	int ret;
77 
78 	imxmd = imx_media_dev_init(dev, NULL);
79 	if (IS_ERR(imxmd))
80 		return PTR_ERR(imxmd);
81 
82 	ret = imx_media_add_of_subdevs(imxmd, node);
83 	if (ret) {
84 		v4l2_err(&imxmd->v4l2_dev,
85 			 "add_of_subdevs failed with %d\n", ret);
86 		goto cleanup;
87 	}
88 
89 	ret = imx_media_dev_notifier_register(imxmd, &imx_media_notifier_ops);
90 	if (ret)
91 		goto cleanup;
92 
93 	return 0;
94 
95 cleanup:
96 	v4l2_async_notifier_cleanup(&imxmd->notifier);
97 	v4l2_device_unregister(&imxmd->v4l2_dev);
98 	media_device_cleanup(&imxmd->md);
99 
100 	return ret;
101 }
102 
103 static int imx_media_remove(struct platform_device *pdev)
104 {
105 	struct imx_media_dev *imxmd =
106 		(struct imx_media_dev *)platform_get_drvdata(pdev);
107 
108 	v4l2_info(&imxmd->v4l2_dev, "Removing imx-media\n");
109 
110 	v4l2_async_notifier_unregister(&imxmd->notifier);
111 	imx_media_unregister_ipu_internal_subdevs(imxmd);
112 	v4l2_async_notifier_cleanup(&imxmd->notifier);
113 	imx_media_csc_scaler_device_unregister(imxmd->m2m_vdev);
114 	media_device_unregister(&imxmd->md);
115 	v4l2_device_unregister(&imxmd->v4l2_dev);
116 	media_device_cleanup(&imxmd->md);
117 
118 	return 0;
119 }
120 
121 static const struct of_device_id imx_media_dt_ids[] = {
122 	{ .compatible = "fsl,imx-capture-subsystem" },
123 	{ /* sentinel */ }
124 };
125 MODULE_DEVICE_TABLE(of, imx_media_dt_ids);
126 
127 static struct platform_driver imx_media_pdrv = {
128 	.probe		= imx_media_probe,
129 	.remove		= imx_media_remove,
130 	.driver		= {
131 		.name	= "imx-media",
132 		.of_match_table	= imx_media_dt_ids,
133 	},
134 };
135 
136 module_platform_driver(imx_media_pdrv);
137 
138 MODULE_DESCRIPTION("i.MX5/6 v4l2 media controller driver");
139 MODULE_AUTHOR("Steve Longerbeam <steve_longerbeam@mentor.com>");
140 MODULE_LICENSE("GPL");
141