treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / platform / sunxi / sun4i-csi / sun4i_csi.c
blobeff34ded63055db4d0920f02e7ba0ff338f7ed63
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2016 NextThing Co
4 * Copyright (C) 2016-2019 Bootlin
6 * Author: Maxime Ripard <maxime.ripard@bootlin.com>
7 */
9 #include <linux/clk.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/of.h>
14 #include <linux/of_device.h>
15 #include <linux/of_graph.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/reset.h>
19 #include <linux/videodev2.h>
21 #include <media/v4l2-dev.h>
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-fwnode.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-mediabus.h>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
30 #include "sun4i_csi.h"
32 struct sun4i_csi_traits {
33 unsigned int channels;
34 unsigned int max_width;
35 bool has_isp;
38 static const struct media_entity_operations sun4i_csi_video_entity_ops = {
39 .link_validate = v4l2_subdev_link_validate,
42 static int sun4i_csi_notify_bound(struct v4l2_async_notifier *notifier,
43 struct v4l2_subdev *subdev,
44 struct v4l2_async_subdev *asd)
46 struct sun4i_csi *csi = container_of(notifier, struct sun4i_csi,
47 notifier);
49 csi->src_subdev = subdev;
50 csi->src_pad = media_entity_get_fwnode_pad(&subdev->entity,
51 subdev->fwnode,
52 MEDIA_PAD_FL_SOURCE);
53 if (csi->src_pad < 0) {
54 dev_err(csi->dev, "Couldn't find output pad for subdev %s\n",
55 subdev->name);
56 return csi->src_pad;
59 dev_dbg(csi->dev, "Bound %s pad: %d\n", subdev->name, csi->src_pad);
60 return 0;
63 static int sun4i_csi_notify_complete(struct v4l2_async_notifier *notifier)
65 struct sun4i_csi *csi = container_of(notifier, struct sun4i_csi,
66 notifier);
67 struct v4l2_subdev *subdev = &csi->subdev;
68 struct video_device *vdev = &csi->vdev;
69 int ret;
71 ret = v4l2_device_register_subdev(&csi->v4l, subdev);
72 if (ret < 0)
73 return ret;
75 ret = sun4i_csi_v4l2_register(csi);
76 if (ret < 0)
77 return ret;
79 ret = media_device_register(&csi->mdev);
80 if (ret)
81 return ret;
83 /* Create link from subdev to main device */
84 ret = media_create_pad_link(&subdev->entity, CSI_SUBDEV_SOURCE,
85 &vdev->entity, 0,
86 MEDIA_LNK_FL_ENABLED |
87 MEDIA_LNK_FL_IMMUTABLE);
88 if (ret)
89 goto err_clean_media;
91 ret = media_create_pad_link(&csi->src_subdev->entity, csi->src_pad,
92 &subdev->entity, CSI_SUBDEV_SINK,
93 MEDIA_LNK_FL_ENABLED |
94 MEDIA_LNK_FL_IMMUTABLE);
95 if (ret)
96 goto err_clean_media;
98 ret = v4l2_device_register_subdev_nodes(&csi->v4l);
99 if (ret < 0)
100 goto err_clean_media;
102 return 0;
104 err_clean_media:
105 media_device_unregister(&csi->mdev);
107 return ret;
110 static const struct v4l2_async_notifier_operations sun4i_csi_notify_ops = {
111 .bound = sun4i_csi_notify_bound,
112 .complete = sun4i_csi_notify_complete,
115 static int sun4i_csi_notifier_init(struct sun4i_csi *csi)
117 struct v4l2_fwnode_endpoint vep = {
118 .bus_type = V4L2_MBUS_PARALLEL,
120 struct fwnode_handle *ep;
121 int ret;
123 v4l2_async_notifier_init(&csi->notifier);
125 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csi->dev), 0, 0,
126 FWNODE_GRAPH_ENDPOINT_NEXT);
127 if (!ep)
128 return -EINVAL;
130 ret = v4l2_fwnode_endpoint_parse(ep, &vep);
131 if (ret)
132 goto out;
134 csi->bus = vep.bus.parallel;
136 ret = v4l2_async_notifier_add_fwnode_remote_subdev(&csi->notifier,
137 ep, &csi->asd);
138 if (ret)
139 goto out;
141 csi->notifier.ops = &sun4i_csi_notify_ops;
143 out:
144 fwnode_handle_put(ep);
145 return ret;
148 static int sun4i_csi_probe(struct platform_device *pdev)
150 struct v4l2_subdev *subdev;
151 struct video_device *vdev;
152 struct sun4i_csi *csi;
153 struct resource *res;
154 int ret;
155 int irq;
157 csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL);
158 if (!csi)
159 return -ENOMEM;
160 platform_set_drvdata(pdev, csi);
161 csi->dev = &pdev->dev;
162 subdev = &csi->subdev;
163 vdev = &csi->vdev;
165 csi->traits = of_device_get_match_data(&pdev->dev);
166 if (!csi->traits)
167 return -EINVAL;
170 * On Allwinner SoCs, some high memory bandwidth devices do DMA
171 * directly over the memory bus (called MBUS), instead of the
172 * system bus. The memory bus has a different addressing scheme
173 * without the DRAM starting offset.
175 * In some cases this can be described by an interconnect in
176 * the device tree. In other cases where the hardware is not
177 * fully understood and the interconnect is left out of the
178 * device tree, fall back to a default offset.
180 if (of_find_property(csi->dev->of_node, "interconnects", NULL)) {
181 ret = of_dma_configure(csi->dev, csi->dev->of_node, true);
182 if (ret)
183 return ret;
184 } else {
185 #ifdef PHYS_PFN_OFFSET
186 csi->dev->dma_pfn_offset = PHYS_PFN_OFFSET;
187 #endif
190 csi->mdev.dev = csi->dev;
191 strscpy(csi->mdev.model, "Allwinner Video Capture Device",
192 sizeof(csi->mdev.model));
193 csi->mdev.hw_revision = 0;
194 media_device_init(&csi->mdev);
195 csi->v4l.mdev = &csi->mdev;
197 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
198 csi->regs = devm_ioremap_resource(&pdev->dev, res);
199 if (IS_ERR(csi->regs))
200 return PTR_ERR(csi->regs);
202 irq = platform_get_irq(pdev, 0);
203 if (irq < 0)
204 return irq;
206 csi->bus_clk = devm_clk_get(&pdev->dev, "bus");
207 if (IS_ERR(csi->bus_clk)) {
208 dev_err(&pdev->dev, "Couldn't get our bus clock\n");
209 return PTR_ERR(csi->bus_clk);
212 if (csi->traits->has_isp) {
213 csi->isp_clk = devm_clk_get(&pdev->dev, "isp");
214 if (IS_ERR(csi->isp_clk)) {
215 dev_err(&pdev->dev, "Couldn't get our ISP clock\n");
216 return PTR_ERR(csi->isp_clk);
220 csi->ram_clk = devm_clk_get(&pdev->dev, "ram");
221 if (IS_ERR(csi->ram_clk)) {
222 dev_err(&pdev->dev, "Couldn't get our ram clock\n");
223 return PTR_ERR(csi->ram_clk);
226 csi->rst = devm_reset_control_get(&pdev->dev, NULL);
227 if (IS_ERR(csi->rst)) {
228 dev_err(&pdev->dev, "Couldn't get our reset line\n");
229 return PTR_ERR(csi->rst);
232 /* Initialize subdev */
233 v4l2_subdev_init(subdev, &sun4i_csi_subdev_ops);
234 subdev->flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
235 subdev->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
236 subdev->owner = THIS_MODULE;
237 snprintf(subdev->name, sizeof(subdev->name), "sun4i-csi-0");
238 v4l2_set_subdevdata(subdev, csi);
240 csi->subdev_pads[CSI_SUBDEV_SINK].flags =
241 MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
242 csi->subdev_pads[CSI_SUBDEV_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
243 ret = media_entity_pads_init(&subdev->entity, CSI_SUBDEV_PADS,
244 csi->subdev_pads);
245 if (ret < 0)
246 return ret;
248 csi->vdev_pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
249 vdev->entity.ops = &sun4i_csi_video_entity_ops;
250 ret = media_entity_pads_init(&vdev->entity, 1, &csi->vdev_pad);
251 if (ret < 0)
252 return ret;
254 ret = sun4i_csi_dma_register(csi, irq);
255 if (ret)
256 goto err_clean_pad;
258 ret = sun4i_csi_notifier_init(csi);
259 if (ret)
260 goto err_unregister_media;
262 ret = v4l2_async_notifier_register(&csi->v4l, &csi->notifier);
263 if (ret) {
264 dev_err(csi->dev, "Couldn't register our notifier.\n");
265 goto err_unregister_media;
268 pm_runtime_enable(&pdev->dev);
270 return 0;
272 err_unregister_media:
273 media_device_unregister(&csi->mdev);
274 sun4i_csi_dma_unregister(csi);
276 err_clean_pad:
277 media_device_cleanup(&csi->mdev);
279 return ret;
282 static int sun4i_csi_remove(struct platform_device *pdev)
284 struct sun4i_csi *csi = platform_get_drvdata(pdev);
286 v4l2_async_notifier_unregister(&csi->notifier);
287 v4l2_async_notifier_cleanup(&csi->notifier);
288 media_device_unregister(&csi->mdev);
289 sun4i_csi_dma_unregister(csi);
290 media_device_cleanup(&csi->mdev);
292 return 0;
295 static const struct sun4i_csi_traits sun4i_a10_csi1_traits = {
296 .channels = 1,
297 .max_width = 24,
298 .has_isp = false,
301 static const struct sun4i_csi_traits sun7i_a20_csi0_traits = {
302 .channels = 4,
303 .max_width = 16,
304 .has_isp = true,
307 static const struct of_device_id sun4i_csi_of_match[] = {
308 { .compatible = "allwinner,sun4i-a10-csi1", .data = &sun4i_a10_csi1_traits },
309 { .compatible = "allwinner,sun7i-a20-csi0", .data = &sun7i_a20_csi0_traits },
310 { /* Sentinel */ }
312 MODULE_DEVICE_TABLE(of, sun4i_csi_of_match);
314 static int __maybe_unused sun4i_csi_runtime_resume(struct device *dev)
316 struct sun4i_csi *csi = dev_get_drvdata(dev);
318 reset_control_deassert(csi->rst);
319 clk_prepare_enable(csi->bus_clk);
320 clk_prepare_enable(csi->ram_clk);
321 clk_set_rate(csi->isp_clk, 80000000);
322 clk_prepare_enable(csi->isp_clk);
324 writel(1, csi->regs + CSI_EN_REG);
326 return 0;
329 static int __maybe_unused sun4i_csi_runtime_suspend(struct device *dev)
331 struct sun4i_csi *csi = dev_get_drvdata(dev);
333 clk_disable_unprepare(csi->isp_clk);
334 clk_disable_unprepare(csi->ram_clk);
335 clk_disable_unprepare(csi->bus_clk);
337 reset_control_assert(csi->rst);
339 return 0;
342 static const struct dev_pm_ops sun4i_csi_pm_ops = {
343 SET_RUNTIME_PM_OPS(sun4i_csi_runtime_suspend,
344 sun4i_csi_runtime_resume,
345 NULL)
348 static struct platform_driver sun4i_csi_driver = {
349 .probe = sun4i_csi_probe,
350 .remove = sun4i_csi_remove,
351 .driver = {
352 .name = "sun4i-csi",
353 .of_match_table = sun4i_csi_of_match,
354 .pm = &sun4i_csi_pm_ops,
357 module_platform_driver(sun4i_csi_driver);
359 MODULE_DESCRIPTION("Allwinner A10 Camera Sensor Interface driver");
360 MODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>");
361 MODULE_LICENSE("GPL");