Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / platform / exynos4-is / media-dev.c
blobb5993532831da6b3aada06390b8338f3b32f66d4
1 /*
2 * S5P/EXYNOS4 SoC series camera host interface media device driver
4 * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
5 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 2 of the License,
10 * or (at your option) any later version.
13 #include <linux/bug.h>
14 #include <linux/clk.h>
15 #include <linux/clk-provider.h>
16 #include <linux/device.h>
17 #include <linux/errno.h>
18 #include <linux/i2c.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_platform.h>
24 #include <linux/of_device.h>
25 #include <linux/of_graph.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <media/v4l2-async.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-fwnode.h>
33 #include <media/media-device.h>
34 #include <media/drv-intf/exynos-fimc.h>
36 #include "media-dev.h"
37 #include "fimc-core.h"
38 #include "fimc-is.h"
39 #include "fimc-lite.h"
40 #include "mipi-csis.h"
42 /* Set up image sensor subdev -> FIMC capture node notifications. */
43 static void __setup_sensor_notification(struct fimc_md *fmd,
44 struct v4l2_subdev *sensor,
45 struct v4l2_subdev *fimc_sd)
47 struct fimc_source_info *src_inf;
48 struct fimc_sensor_info *md_si;
49 unsigned long flags;
51 src_inf = v4l2_get_subdev_hostdata(sensor);
52 if (!src_inf || WARN_ON(fmd == NULL))
53 return;
55 md_si = source_to_sensor_info(src_inf);
56 spin_lock_irqsave(&fmd->slock, flags);
57 md_si->host = v4l2_get_subdevdata(fimc_sd);
58 spin_unlock_irqrestore(&fmd->slock, flags);
61 /**
62 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
63 * @p: fimc pipeline
64 * @me: media entity terminating the pipeline
66 * Caller holds the graph mutex.
68 static void fimc_pipeline_prepare(struct fimc_pipeline *p,
69 struct media_entity *me)
71 struct fimc_md *fmd = entity_to_fimc_mdev(me);
72 struct v4l2_subdev *sd;
73 struct v4l2_subdev *sensor = NULL;
74 int i;
76 for (i = 0; i < IDX_MAX; i++)
77 p->subdevs[i] = NULL;
79 while (1) {
80 struct media_pad *pad = NULL;
82 /* Find remote source pad */
83 for (i = 0; i < me->num_pads; i++) {
84 struct media_pad *spad = &me->pads[i];
85 if (!(spad->flags & MEDIA_PAD_FL_SINK))
86 continue;
87 pad = media_entity_remote_pad(spad);
88 if (pad)
89 break;
92 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
93 break;
94 sd = media_entity_to_v4l2_subdev(pad->entity);
96 switch (sd->grp_id) {
97 case GRP_ID_SENSOR:
98 sensor = sd;
99 /* fall through */
100 case GRP_ID_FIMC_IS_SENSOR:
101 p->subdevs[IDX_SENSOR] = sd;
102 break;
103 case GRP_ID_CSIS:
104 p->subdevs[IDX_CSIS] = sd;
105 break;
106 case GRP_ID_FLITE:
107 p->subdevs[IDX_FLITE] = sd;
108 break;
109 case GRP_ID_FIMC:
110 p->subdevs[IDX_FIMC] = sd;
111 break;
112 case GRP_ID_FIMC_IS:
113 p->subdevs[IDX_IS_ISP] = sd;
114 break;
115 default:
116 break;
118 me = &sd->entity;
119 if (me->num_pads == 1)
120 break;
123 if (sensor && p->subdevs[IDX_FIMC])
124 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
128 * __subdev_set_power - change power state of a single subdev
129 * @sd: subdevice to change power state for
130 * @on: 1 to enable power or 0 to disable
132 * Return result of s_power subdev operation or -ENXIO if sd argument
133 * is NULL. Return 0 if the subdevice does not implement s_power.
135 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
137 int *use_count;
138 int ret;
140 if (sd == NULL)
141 return -ENXIO;
143 use_count = &sd->entity.use_count;
144 if (on && (*use_count)++ > 0)
145 return 0;
146 else if (!on && (*use_count == 0 || --(*use_count) > 0))
147 return 0;
148 ret = v4l2_subdev_call(sd, core, s_power, on);
150 return ret != -ENOIOCTLCMD ? ret : 0;
154 * fimc_pipeline_s_power - change power state of all pipeline subdevs
155 * @p: fimc device terminating the pipeline
156 * @on: true to power on, false to power off
158 * Needs to be called with the graph mutex held.
160 static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
162 static const u8 seq[2][IDX_MAX - 1] = {
163 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
164 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
166 int i, ret = 0;
168 if (p->subdevs[IDX_SENSOR] == NULL)
169 return -ENXIO;
171 for (i = 0; i < IDX_MAX - 1; i++) {
172 unsigned int idx = seq[on][i];
174 ret = __subdev_set_power(p->subdevs[idx], on);
177 if (ret < 0 && ret != -ENXIO)
178 goto error;
180 return 0;
181 error:
182 for (; i >= 0; i--) {
183 unsigned int idx = seq[on][i];
184 __subdev_set_power(p->subdevs[idx], !on);
186 return ret;
190 * __fimc_pipeline_enable - enable power of all pipeline subdevs
191 * and the sensor clock
192 * @ep: video pipeline structure
193 * @fmd: fimc media device
195 * Called with the graph mutex held.
197 static int __fimc_pipeline_enable(struct exynos_media_pipeline *ep,
198 struct fimc_md *fmd)
200 struct fimc_pipeline *p = to_fimc_pipeline(ep);
201 int ret;
203 /* Enable PXLASYNC clock if this pipeline includes FIMC-IS */
204 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
205 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
206 if (ret < 0)
207 return ret;
210 ret = fimc_pipeline_s_power(p, 1);
211 if (!ret)
212 return 0;
214 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
215 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
217 return ret;
221 * __fimc_pipeline_open - update the pipeline information, enable power
222 * of all pipeline subdevs and the sensor clock
223 * @ep: fimc device terminating the pipeline
224 * @me: media entity to start graph walk with
225 * @prepare: true to walk the current pipeline and acquire all subdevs
227 * Called with the graph mutex held.
229 static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
230 struct media_entity *me, bool prepare)
232 struct fimc_md *fmd = entity_to_fimc_mdev(me);
233 struct fimc_pipeline *p = to_fimc_pipeline(ep);
234 struct v4l2_subdev *sd;
236 if (WARN_ON(p == NULL || me == NULL))
237 return -EINVAL;
239 if (prepare)
240 fimc_pipeline_prepare(p, me);
242 sd = p->subdevs[IDX_SENSOR];
243 if (sd == NULL) {
244 pr_warn("%s(): No sensor subdev\n", __func__);
246 * Pipeline open cannot fail so as to make it possible
247 * for the user space to configure the pipeline.
249 return 0;
252 return __fimc_pipeline_enable(ep, fmd);
256 * __fimc_pipeline_close - disable the sensor clock and pipeline power
257 * @ep: fimc device terminating the pipeline
259 * Disable power of all subdevs and turn the external sensor clock off.
261 static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
263 struct fimc_pipeline *p = to_fimc_pipeline(ep);
264 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
265 struct fimc_md *fmd;
266 int ret;
268 if (sd == NULL) {
269 pr_warn("%s(): No sensor subdev\n", __func__);
270 return 0;
273 ret = fimc_pipeline_s_power(p, 0);
275 fmd = entity_to_fimc_mdev(&sd->entity);
277 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
278 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
279 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
281 return ret == -ENXIO ? 0 : ret;
285 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
286 * @ep: video pipeline structure
287 * @on: passed as the s_stream() callback argument
289 static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
291 static const u8 seq[2][IDX_MAX] = {
292 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
293 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
295 struct fimc_pipeline *p = to_fimc_pipeline(ep);
296 struct fimc_md *fmd = entity_to_fimc_mdev(&p->subdevs[IDX_CSIS]->entity);
297 enum fimc_subdev_index sd_id;
298 int i, ret = 0;
300 if (p->subdevs[IDX_SENSOR] == NULL) {
301 if (!fmd->user_subdev_api) {
303 * Sensor must be already discovered if we
304 * aren't in the user_subdev_api mode
306 return -ENODEV;
309 /* Get pipeline sink entity */
310 if (p->subdevs[IDX_FIMC])
311 sd_id = IDX_FIMC;
312 else if (p->subdevs[IDX_IS_ISP])
313 sd_id = IDX_IS_ISP;
314 else if (p->subdevs[IDX_FLITE])
315 sd_id = IDX_FLITE;
316 else
317 return -ENODEV;
320 * Sensor could have been linked between open and STREAMON -
321 * check if this is the case.
323 fimc_pipeline_prepare(p, &p->subdevs[sd_id]->entity);
325 if (p->subdevs[IDX_SENSOR] == NULL)
326 return -ENODEV;
328 ret = __fimc_pipeline_enable(ep, fmd);
329 if (ret < 0)
330 return ret;
334 for (i = 0; i < IDX_MAX; i++) {
335 unsigned int idx = seq[on][i];
337 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
339 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
340 goto error;
343 return 0;
344 error:
345 fimc_pipeline_s_power(p, !on);
346 for (; i >= 0; i--) {
347 unsigned int idx = seq[on][i];
348 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
350 return ret;
353 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
354 static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
355 .open = __fimc_pipeline_open,
356 .close = __fimc_pipeline_close,
357 .set_stream = __fimc_pipeline_s_stream,
360 static struct exynos_media_pipeline *fimc_md_pipeline_create(
361 struct fimc_md *fmd)
363 struct fimc_pipeline *p;
365 p = kzalloc(sizeof(*p), GFP_KERNEL);
366 if (!p)
367 return NULL;
369 list_add_tail(&p->list, &fmd->pipelines);
371 p->ep.ops = &fimc_pipeline_ops;
372 return &p->ep;
375 static void fimc_md_pipelines_free(struct fimc_md *fmd)
377 while (!list_empty(&fmd->pipelines)) {
378 struct fimc_pipeline *p;
380 p = list_entry(fmd->pipelines.next, typeof(*p), list);
381 list_del(&p->list);
382 kfree(p);
386 /* Parse port node and register as a sub-device any sensor specified there. */
387 static int fimc_md_parse_port_node(struct fimc_md *fmd,
388 struct device_node *port,
389 unsigned int index)
391 struct fimc_source_info *pd = &fmd->sensor[index].pdata;
392 struct device_node *rem, *ep, *np;
393 struct v4l2_fwnode_endpoint endpoint;
394 int ret;
396 /* Assume here a port node can have only one endpoint node. */
397 ep = of_get_next_child(port, NULL);
398 if (!ep)
399 return 0;
401 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &endpoint);
402 if (ret) {
403 of_node_put(ep);
404 return ret;
407 if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS) {
408 of_node_put(ep);
409 return -EINVAL;
412 pd->mux_id = (endpoint.base.port - 1) & 0x1;
414 rem = of_graph_get_remote_port_parent(ep);
415 of_node_put(ep);
416 if (rem == NULL) {
417 v4l2_info(&fmd->v4l2_dev, "Remote device at %pOF not found\n",
418 ep);
419 return 0;
422 if (fimc_input_is_parallel(endpoint.base.port)) {
423 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
424 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
425 else
426 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
427 pd->flags = endpoint.bus.parallel.flags;
428 } else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
430 * MIPI CSI-2: only input mux selection and
431 * the sensor's clock frequency is needed.
433 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
434 } else {
435 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %pOF\n",
436 endpoint.base.port, rem);
439 * For FIMC-IS handled sensors, that are placed under i2c-isp device
440 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
441 * input. Sensors are attached to the FIMC-LITE hostdata interface
442 * directly or through MIPI-CSIS, depending on the external media bus
443 * used. This needs to be handled in a more reliable way, not by just
444 * checking parent's node name.
446 np = of_get_parent(rem);
448 if (np && !of_node_cmp(np->name, "i2c-isp"))
449 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
450 else
451 pd->fimc_bus_type = pd->sensor_bus_type;
453 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) {
454 of_node_put(rem);
455 return -EINVAL;
458 fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_FWNODE;
459 fmd->sensor[index].asd.match.fwnode = of_fwnode_handle(rem);
460 fmd->async_subdevs[index] = &fmd->sensor[index].asd;
462 fmd->num_sensors++;
464 of_node_put(rem);
465 return 0;
468 /* Register all SoC external sub-devices */
469 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
471 struct device_node *parent = fmd->pdev->dev.of_node;
472 struct device_node *node, *ports;
473 int index = 0;
474 int ret;
477 * Runtime resume one of the FIMC entities to make sure
478 * the sclk_cam clocks are not globally disabled.
480 if (!fmd->pmf)
481 return -ENXIO;
483 ret = pm_runtime_get_sync(fmd->pmf);
484 if (ret < 0)
485 return ret;
487 fmd->num_sensors = 0;
489 /* Attach sensors linked to MIPI CSI-2 receivers */
490 for_each_available_child_of_node(parent, node) {
491 struct device_node *port;
493 if (of_node_cmp(node->name, "csis"))
494 continue;
495 /* The csis node can have only port subnode. */
496 port = of_get_next_child(node, NULL);
497 if (!port)
498 continue;
500 ret = fimc_md_parse_port_node(fmd, port, index);
501 of_node_put(port);
502 if (ret < 0) {
503 of_node_put(node);
504 goto rpm_put;
506 index++;
509 /* Attach sensors listed in the parallel-ports node */
510 ports = of_get_child_by_name(parent, "parallel-ports");
511 if (!ports)
512 goto rpm_put;
514 for_each_child_of_node(ports, node) {
515 ret = fimc_md_parse_port_node(fmd, node, index);
516 if (ret < 0) {
517 of_node_put(node);
518 break;
520 index++;
522 rpm_put:
523 pm_runtime_put(fmd->pmf);
524 return ret;
527 static int __of_get_csis_id(struct device_node *np)
529 u32 reg = 0;
531 np = of_get_child_by_name(np, "port");
532 if (!np)
533 return -EINVAL;
534 of_property_read_u32(np, "reg", &reg);
535 of_node_put(np);
536 return reg - FIMC_INPUT_MIPI_CSI2_0;
540 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
542 static int register_fimc_lite_entity(struct fimc_md *fmd,
543 struct fimc_lite *fimc_lite)
545 struct v4l2_subdev *sd;
546 struct exynos_media_pipeline *ep;
547 int ret;
549 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
550 fmd->fimc_lite[fimc_lite->index]))
551 return -EBUSY;
553 sd = &fimc_lite->subdev;
554 sd->grp_id = GRP_ID_FLITE;
556 ep = fimc_md_pipeline_create(fmd);
557 if (!ep)
558 return -ENOMEM;
560 v4l2_set_subdev_hostdata(sd, ep);
562 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
563 if (!ret)
564 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
565 else
566 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
567 fimc_lite->index);
568 return ret;
571 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
573 struct v4l2_subdev *sd;
574 struct exynos_media_pipeline *ep;
575 int ret;
577 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
578 return -EBUSY;
580 sd = &fimc->vid_cap.subdev;
581 sd->grp_id = GRP_ID_FIMC;
583 ep = fimc_md_pipeline_create(fmd);
584 if (!ep)
585 return -ENOMEM;
587 v4l2_set_subdev_hostdata(sd, ep);
589 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
590 if (!ret) {
591 if (!fmd->pmf && fimc->pdev)
592 fmd->pmf = &fimc->pdev->dev;
593 fmd->fimc[fimc->id] = fimc;
594 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
595 } else {
596 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
597 fimc->id, ret);
599 return ret;
602 static int register_csis_entity(struct fimc_md *fmd,
603 struct platform_device *pdev,
604 struct v4l2_subdev *sd)
606 struct device_node *node = pdev->dev.of_node;
607 int id, ret;
609 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
611 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
612 return -ENOENT;
614 if (WARN_ON(fmd->csis[id].sd))
615 return -EBUSY;
617 sd->grp_id = GRP_ID_CSIS;
618 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
619 if (!ret)
620 fmd->csis[id].sd = sd;
621 else
622 v4l2_err(&fmd->v4l2_dev,
623 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
624 return ret;
627 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
629 struct v4l2_subdev *sd = &is->isp.subdev;
630 struct exynos_media_pipeline *ep;
631 int ret;
633 /* Allocate pipeline object for the ISP capture video node. */
634 ep = fimc_md_pipeline_create(fmd);
635 if (!ep)
636 return -ENOMEM;
638 v4l2_set_subdev_hostdata(sd, ep);
640 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
641 if (ret) {
642 v4l2_err(&fmd->v4l2_dev,
643 "Failed to register FIMC-ISP (%d)\n", ret);
644 return ret;
647 fmd->fimc_is = is;
648 return 0;
651 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
652 struct platform_device *pdev,
653 int plat_entity)
655 struct device *dev = &pdev->dev;
656 int ret = -EPROBE_DEFER;
657 void *drvdata;
659 /* Lock to ensure dev->driver won't change. */
660 device_lock(dev);
662 if (!dev->driver || !try_module_get(dev->driver->owner))
663 goto dev_unlock;
665 drvdata = dev_get_drvdata(dev);
666 /* Some subdev didn't probe successfully id drvdata is NULL */
667 if (drvdata) {
668 switch (plat_entity) {
669 case IDX_FIMC:
670 ret = register_fimc_entity(fmd, drvdata);
671 break;
672 case IDX_FLITE:
673 ret = register_fimc_lite_entity(fmd, drvdata);
674 break;
675 case IDX_CSIS:
676 ret = register_csis_entity(fmd, pdev, drvdata);
677 break;
678 case IDX_IS_ISP:
679 ret = register_fimc_is_entity(fmd, drvdata);
680 break;
681 default:
682 ret = -ENODEV;
686 module_put(dev->driver->owner);
687 dev_unlock:
688 device_unlock(dev);
689 if (ret == -EPROBE_DEFER)
690 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
691 dev_name(dev));
692 else if (ret < 0)
693 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
694 dev_name(dev), ret);
695 return ret;
698 /* Register FIMC, FIMC-LITE and CSIS media entities */
699 static int fimc_md_register_platform_entities(struct fimc_md *fmd,
700 struct device_node *parent)
702 struct device_node *node;
703 int ret = 0;
705 for_each_available_child_of_node(parent, node) {
706 struct platform_device *pdev;
707 int plat_entity = -1;
709 pdev = of_find_device_by_node(node);
710 if (!pdev)
711 continue;
713 /* If driver of any entity isn't ready try all again later. */
714 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
715 plat_entity = IDX_CSIS;
716 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
717 plat_entity = IDX_IS_ISP;
718 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
719 plat_entity = IDX_FLITE;
720 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
721 !of_property_read_bool(node, "samsung,lcd-wb"))
722 plat_entity = IDX_FIMC;
724 if (plat_entity >= 0)
725 ret = fimc_md_register_platform_entity(fmd, pdev,
726 plat_entity);
727 put_device(&pdev->dev);
728 if (ret < 0) {
729 of_node_put(node);
730 break;
734 return ret;
737 static void fimc_md_unregister_entities(struct fimc_md *fmd)
739 int i;
741 for (i = 0; i < FIMC_MAX_DEVS; i++) {
742 struct fimc_dev *dev = fmd->fimc[i];
743 if (dev == NULL)
744 continue;
745 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
746 dev->vid_cap.ve.pipe = NULL;
747 fmd->fimc[i] = NULL;
749 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
750 struct fimc_lite *dev = fmd->fimc_lite[i];
751 if (dev == NULL)
752 continue;
753 v4l2_device_unregister_subdev(&dev->subdev);
754 dev->ve.pipe = NULL;
755 fmd->fimc_lite[i] = NULL;
757 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
758 if (fmd->csis[i].sd == NULL)
759 continue;
760 v4l2_device_unregister_subdev(fmd->csis[i].sd);
761 fmd->csis[i].sd = NULL;
764 if (fmd->fimc_is)
765 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
767 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
771 * __fimc_md_create_fimc_links - create links to all FIMC entities
772 * @fmd: fimc media device
773 * @source: the source entity to create links to all fimc entities from
774 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
775 * @pad: the source entity pad index
776 * @link_mask: bitmask of the fimc devices for which link should be enabled
778 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
779 struct media_entity *source,
780 struct v4l2_subdev *sensor,
781 int pad, int link_mask)
783 struct fimc_source_info *si = NULL;
784 struct media_entity *sink;
785 unsigned int flags = 0;
786 int i, ret = 0;
788 if (sensor) {
789 si = v4l2_get_subdev_hostdata(sensor);
790 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
791 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
792 ret = 1;
795 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
796 if (!fmd->fimc[i])
797 continue;
799 * Some FIMC variants are not fitted with camera capture
800 * interface. Skip creating a link from sensor for those.
802 if (!fmd->fimc[i]->variant->has_cam_if)
803 continue;
805 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
807 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
808 ret = media_create_pad_link(source, pad, sink,
809 FIMC_SD_PAD_SINK_CAM, flags);
810 if (ret)
811 return ret;
813 /* Notify FIMC capture subdev entity */
814 ret = media_entity_call(sink, link_setup, &sink->pads[0],
815 &source->pads[pad], flags);
816 if (ret)
817 break;
819 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
820 source->name, flags ? '=' : '-', sink->name);
823 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
824 if (!fmd->fimc_lite[i])
825 continue;
827 sink = &fmd->fimc_lite[i]->subdev.entity;
828 ret = media_create_pad_link(source, pad, sink,
829 FLITE_SD_PAD_SINK, 0);
830 if (ret)
831 return ret;
833 /* Notify FIMC-LITE subdev entity */
834 ret = media_entity_call(sink, link_setup, &sink->pads[0],
835 &source->pads[pad], 0);
836 if (ret)
837 break;
839 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
840 source->name, sink->name);
842 return 0;
845 /* Create links from FIMC-LITE source pads to other entities */
846 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
848 struct media_entity *source, *sink;
849 int i, ret = 0;
851 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
852 struct fimc_lite *fimc = fmd->fimc_lite[i];
854 if (fimc == NULL)
855 continue;
857 source = &fimc->subdev.entity;
858 sink = &fimc->ve.vdev.entity;
859 /* FIMC-LITE's subdev and video node */
860 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
861 sink, 0, 0);
862 if (ret)
863 break;
864 /* Link from FIMC-LITE to IS-ISP subdev */
865 sink = &fmd->fimc_is->isp.subdev.entity;
866 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
867 sink, 0, 0);
868 if (ret)
869 break;
872 return ret;
875 /* Create FIMC-IS links */
876 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
878 struct fimc_isp *isp = &fmd->fimc_is->isp;
879 struct media_entity *source, *sink;
880 int i, ret;
882 source = &isp->subdev.entity;
884 for (i = 0; i < FIMC_MAX_DEVS; i++) {
885 if (fmd->fimc[i] == NULL)
886 continue;
888 /* Link from FIMC-IS-ISP subdev to FIMC */
889 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
890 ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
891 sink, FIMC_SD_PAD_SINK_FIFO, 0);
892 if (ret)
893 return ret;
896 /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
897 sink = &isp->video_capture.ve.vdev.entity;
899 /* Skip this link if the fimc-is-isp video node driver isn't built-in */
900 if (sink->num_pads == 0)
901 return 0;
903 return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
904 sink, 0, 0);
908 * fimc_md_create_links - create default links between registered entities
909 * @fmd: fimc media device
911 * Parallel interface sensor entities are connected directly to FIMC capture
912 * entities. The sensors using MIPI CSIS bus are connected through immutable
913 * link with CSI receiver entity specified by mux_id. Any registered CSIS
914 * entity has a link to each registered FIMC capture entity. Enabled links
915 * are created by default between each subsequent registered sensor and
916 * subsequent FIMC capture entity. The number of default active links is
917 * determined by the number of available sensors or FIMC entities,
918 * whichever is less.
920 static int fimc_md_create_links(struct fimc_md *fmd)
922 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
923 struct v4l2_subdev *sensor, *csis;
924 struct fimc_source_info *pdata;
925 struct media_entity *source, *sink;
926 int i, pad, fimc_id = 0, ret = 0;
927 u32 flags, link_mask = 0;
929 for (i = 0; i < fmd->num_sensors; i++) {
930 if (fmd->sensor[i].subdev == NULL)
931 continue;
933 sensor = fmd->sensor[i].subdev;
934 pdata = v4l2_get_subdev_hostdata(sensor);
935 if (!pdata)
936 continue;
938 source = NULL;
940 switch (pdata->sensor_bus_type) {
941 case FIMC_BUS_TYPE_MIPI_CSI2:
942 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
943 "Wrong CSI channel id: %d\n", pdata->mux_id))
944 return -EINVAL;
946 csis = fmd->csis[pdata->mux_id].sd;
947 if (WARN(csis == NULL,
948 "MIPI-CSI interface specified but s5p-csis module is not loaded!\n"))
949 return -EINVAL;
951 pad = sensor->entity.num_pads - 1;
952 ret = media_create_pad_link(&sensor->entity, pad,
953 &csis->entity, CSIS_PAD_SINK,
954 MEDIA_LNK_FL_IMMUTABLE |
955 MEDIA_LNK_FL_ENABLED);
956 if (ret)
957 return ret;
959 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
960 sensor->entity.name, csis->entity.name);
962 source = NULL;
963 csi_sensors[pdata->mux_id] = sensor;
964 break;
966 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
967 source = &sensor->entity;
968 pad = 0;
969 break;
971 default:
972 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
973 pdata->sensor_bus_type);
974 return -EINVAL;
976 if (source == NULL)
977 continue;
979 link_mask = 1 << fimc_id++;
980 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
981 pad, link_mask);
984 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
985 if (fmd->csis[i].sd == NULL)
986 continue;
988 source = &fmd->csis[i].sd->entity;
989 pad = CSIS_PAD_SOURCE;
990 sensor = csi_sensors[i];
992 link_mask = 1 << fimc_id++;
993 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
994 pad, link_mask);
997 /* Create immutable links between each FIMC's subdev and video node */
998 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
999 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1000 if (!fmd->fimc[i])
1001 continue;
1003 source = &fmd->fimc[i]->vid_cap.subdev.entity;
1004 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1006 ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
1007 sink, 0, flags);
1008 if (ret)
1009 break;
1012 ret = __fimc_md_create_flite_source_links(fmd);
1013 if (ret < 0)
1014 return ret;
1016 if (fmd->use_isp)
1017 ret = __fimc_md_create_fimc_is_links(fmd);
1019 return ret;
1023 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1025 static void fimc_md_put_clocks(struct fimc_md *fmd)
1027 int i = FIMC_MAX_CAMCLKS;
1029 while (--i >= 0) {
1030 if (IS_ERR(fmd->camclk[i].clock))
1031 continue;
1032 clk_put(fmd->camclk[i].clock);
1033 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1036 /* Writeback (PIXELASYNCMx) clocks */
1037 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1038 if (IS_ERR(fmd->wbclk[i]))
1039 continue;
1040 clk_put(fmd->wbclk[i]);
1041 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1045 static int fimc_md_get_clocks(struct fimc_md *fmd)
1047 struct device *dev = &fmd->pdev->dev;
1048 char clk_name[32];
1049 struct clk *clock;
1050 int i, ret = 0;
1052 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1053 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1055 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1056 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1057 clock = clk_get(dev, clk_name);
1059 if (IS_ERR(clock)) {
1060 dev_err(dev, "Failed to get clock: %s\n", clk_name);
1061 ret = PTR_ERR(clock);
1062 break;
1064 fmd->camclk[i].clock = clock;
1066 if (ret)
1067 fimc_md_put_clocks(fmd);
1069 if (!fmd->use_isp)
1070 return 0;
1072 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1073 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1075 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1077 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1078 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1079 clock = clk_get(dev, clk_name);
1080 if (IS_ERR(clock)) {
1081 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1082 clk_name);
1083 ret = PTR_ERR(clock);
1084 break;
1086 fmd->wbclk[i] = clock;
1088 if (ret)
1089 fimc_md_put_clocks(fmd);
1091 return ret;
1094 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1096 struct exynos_video_entity *ve;
1097 struct fimc_pipeline *p;
1098 struct video_device *vdev;
1099 int ret;
1101 vdev = media_entity_to_video_device(entity);
1102 if (vdev->entity.use_count == 0)
1103 return 0;
1105 ve = vdev_to_exynos_video_entity(vdev);
1106 p = to_fimc_pipeline(ve->pipe);
1108 * Nothing to do if we are disabling the pipeline, some link
1109 * has been disconnected and p->subdevs array is cleared now.
1111 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1112 return 0;
1114 if (enable)
1115 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1116 else
1117 ret = __fimc_pipeline_close(ve->pipe);
1119 if (ret == 0 && !enable)
1120 memset(p->subdevs, 0, sizeof(p->subdevs));
1122 return ret;
1125 /* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
1126 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1127 struct media_graph *graph)
1129 struct media_entity *entity_err = entity;
1130 int ret;
1133 * Walk current graph and call the pipeline open/close routine for each
1134 * opened video node that belongs to the graph of entities connected
1135 * through active links. This is needed as we cannot power on/off the
1136 * subdevs in random order.
1138 media_graph_walk_start(graph, entity);
1140 while ((entity = media_graph_walk_next(graph))) {
1141 if (!is_media_entity_v4l2_video_device(entity))
1142 continue;
1144 ret = __fimc_md_modify_pipeline(entity, enable);
1146 if (ret < 0)
1147 goto err;
1150 return 0;
1152 err:
1153 media_graph_walk_start(graph, entity_err);
1155 while ((entity_err = media_graph_walk_next(graph))) {
1156 if (!is_media_entity_v4l2_video_device(entity_err))
1157 continue;
1159 __fimc_md_modify_pipeline(entity_err, !enable);
1161 if (entity_err == entity)
1162 break;
1165 return ret;
1168 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1169 unsigned int notification)
1171 struct media_graph *graph =
1172 &container_of(link->graph_obj.mdev, struct fimc_md,
1173 media_dev)->link_setup_graph;
1174 struct media_entity *sink = link->sink->entity;
1175 int ret = 0;
1177 /* Before link disconnection */
1178 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1179 ret = media_graph_walk_init(graph,
1180 link->graph_obj.mdev);
1181 if (ret)
1182 return ret;
1183 if (!(flags & MEDIA_LNK_FL_ENABLED))
1184 ret = __fimc_md_modify_pipelines(sink, false, graph);
1185 #if 0
1186 else
1187 /* TODO: Link state change validation */
1188 #endif
1189 /* After link activation */
1190 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1191 if (link->flags & MEDIA_LNK_FL_ENABLED)
1192 ret = __fimc_md_modify_pipelines(sink, true, graph);
1193 media_graph_walk_cleanup(graph);
1196 return ret ? -EPIPE : 0;
1199 static const struct media_device_ops fimc_md_ops = {
1200 .link_notify = fimc_md_link_notify,
1203 static ssize_t fimc_md_sysfs_show(struct device *dev,
1204 struct device_attribute *attr, char *buf)
1206 struct fimc_md *fmd = dev_get_drvdata(dev);
1208 if (fmd->user_subdev_api)
1209 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1211 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1214 static ssize_t fimc_md_sysfs_store(struct device *dev,
1215 struct device_attribute *attr,
1216 const char *buf, size_t count)
1218 struct fimc_md *fmd = dev_get_drvdata(dev);
1219 bool subdev_api;
1220 int i;
1222 if (!strcmp(buf, "vid-dev\n"))
1223 subdev_api = false;
1224 else if (!strcmp(buf, "sub-dev\n"))
1225 subdev_api = true;
1226 else
1227 return count;
1229 fmd->user_subdev_api = subdev_api;
1230 for (i = 0; i < FIMC_MAX_DEVS; i++)
1231 if (fmd->fimc[i])
1232 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1233 return count;
1236 * This device attribute is to select video pipeline configuration method.
1237 * There are following valid values:
1238 * vid-dev - for V4L2 video node API only, subdevice will be configured
1239 * by the host driver.
1240 * sub-dev - for media controller API, subdevs must be configured in user
1241 * space before starting streaming.
1243 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1244 fimc_md_sysfs_show, fimc_md_sysfs_store);
1246 static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1248 struct device *dev = &fmd->pdev->dev;
1249 struct fimc_pinctrl *pctl = &fmd->pinctl;
1251 pctl->pinctrl = devm_pinctrl_get(dev);
1252 if (IS_ERR(pctl->pinctrl))
1253 return PTR_ERR(pctl->pinctrl);
1255 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1256 PINCTRL_STATE_DEFAULT);
1257 if (IS_ERR(pctl->state_default))
1258 return PTR_ERR(pctl->state_default);
1260 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1261 PINCTRL_STATE_IDLE);
1262 return 0;
1265 static int cam_clk_prepare(struct clk_hw *hw)
1267 struct cam_clk *camclk = to_cam_clk(hw);
1268 int ret;
1270 if (camclk->fmd->pmf == NULL)
1271 return -ENODEV;
1273 ret = pm_runtime_get_sync(camclk->fmd->pmf);
1274 return ret < 0 ? ret : 0;
1277 static void cam_clk_unprepare(struct clk_hw *hw)
1279 struct cam_clk *camclk = to_cam_clk(hw);
1281 if (camclk->fmd->pmf == NULL)
1282 return;
1284 pm_runtime_put_sync(camclk->fmd->pmf);
1287 static const struct clk_ops cam_clk_ops = {
1288 .prepare = cam_clk_prepare,
1289 .unprepare = cam_clk_unprepare,
1292 static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1294 struct cam_clk_provider *cp = &fmd->clk_provider;
1295 unsigned int i;
1297 if (cp->of_node)
1298 of_clk_del_provider(cp->of_node);
1300 for (i = 0; i < cp->num_clocks; i++)
1301 clk_unregister(cp->clks[i]);
1304 static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1306 struct cam_clk_provider *cp = &fmd->clk_provider;
1307 struct device *dev = &fmd->pdev->dev;
1308 int i, ret;
1310 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1311 struct cam_clk *camclk = &cp->camclk[i];
1312 struct clk_init_data init;
1313 const char *p_name;
1315 ret = of_property_read_string_index(dev->of_node,
1316 "clock-output-names", i, &init.name);
1317 if (ret < 0)
1318 break;
1320 p_name = __clk_get_name(fmd->camclk[i].clock);
1322 /* It's safe since clk_register() will duplicate the string. */
1323 init.parent_names = &p_name;
1324 init.num_parents = 1;
1325 init.ops = &cam_clk_ops;
1326 init.flags = CLK_SET_RATE_PARENT;
1327 camclk->hw.init = &init;
1328 camclk->fmd = fmd;
1330 cp->clks[i] = clk_register(NULL, &camclk->hw);
1331 if (IS_ERR(cp->clks[i])) {
1332 dev_err(dev, "failed to register clock: %s (%ld)\n",
1333 init.name, PTR_ERR(cp->clks[i]));
1334 ret = PTR_ERR(cp->clks[i]);
1335 goto err;
1337 cp->num_clocks++;
1340 if (cp->num_clocks == 0) {
1341 dev_warn(dev, "clk provider not registered\n");
1342 return 0;
1345 cp->clk_data.clks = cp->clks;
1346 cp->clk_data.clk_num = cp->num_clocks;
1347 cp->of_node = dev->of_node;
1348 ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1349 &cp->clk_data);
1350 if (ret == 0)
1351 return 0;
1352 err:
1353 fimc_md_unregister_clk_provider(fmd);
1354 return ret;
1357 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1358 struct v4l2_subdev *subdev,
1359 struct v4l2_async_subdev *asd)
1361 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1362 struct fimc_sensor_info *si = NULL;
1363 int i;
1365 /* Find platform data for this sensor subdev */
1366 for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1367 if (fmd->sensor[i].asd.match.fwnode ==
1368 of_fwnode_handle(subdev->dev->of_node))
1369 si = &fmd->sensor[i];
1371 if (si == NULL)
1372 return -EINVAL;
1374 v4l2_set_subdev_hostdata(subdev, &si->pdata);
1376 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1377 subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1378 else
1379 subdev->grp_id = GRP_ID_SENSOR;
1381 si->subdev = subdev;
1383 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1384 subdev->name, fmd->num_sensors);
1386 fmd->num_sensors++;
1388 return 0;
1391 static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1393 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1394 int ret;
1396 mutex_lock(&fmd->media_dev.graph_mutex);
1398 ret = fimc_md_create_links(fmd);
1399 if (ret < 0)
1400 goto unlock;
1402 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1403 unlock:
1404 mutex_unlock(&fmd->media_dev.graph_mutex);
1405 if (ret < 0)
1406 return ret;
1408 return media_device_register(&fmd->media_dev);
1411 static const struct v4l2_async_notifier_operations subdev_notifier_ops = {
1412 .bound = subdev_notifier_bound,
1413 .complete = subdev_notifier_complete,
1416 static int fimc_md_probe(struct platform_device *pdev)
1418 struct device *dev = &pdev->dev;
1419 struct v4l2_device *v4l2_dev;
1420 struct fimc_md *fmd;
1421 int ret;
1423 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1424 if (!fmd)
1425 return -ENOMEM;
1427 spin_lock_init(&fmd->slock);
1428 INIT_LIST_HEAD(&fmd->pipelines);
1429 fmd->pdev = pdev;
1431 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1432 sizeof(fmd->media_dev.model));
1433 fmd->media_dev.ops = &fimc_md_ops;
1434 fmd->media_dev.dev = dev;
1436 v4l2_dev = &fmd->v4l2_dev;
1437 v4l2_dev->mdev = &fmd->media_dev;
1438 v4l2_dev->notify = fimc_sensor_notify;
1439 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1441 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1442 fmd->user_subdev_api = true;
1444 media_device_init(&fmd->media_dev);
1446 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1447 if (ret < 0) {
1448 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1449 return ret;
1452 ret = fimc_md_get_clocks(fmd);
1453 if (ret)
1454 goto err_md;
1456 ret = fimc_md_get_pinctrl(fmd);
1457 if (ret < 0) {
1458 if (ret != EPROBE_DEFER)
1459 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1460 goto err_clk;
1463 platform_set_drvdata(pdev, fmd);
1465 ret = fimc_md_register_platform_entities(fmd, dev->of_node);
1466 if (ret)
1467 goto err_clk;
1469 ret = fimc_md_register_sensor_entities(fmd);
1470 if (ret)
1471 goto err_m_ent;
1473 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1474 if (ret)
1475 goto err_m_ent;
1477 * FIMC platform devices need to be registered before the sclk_cam
1478 * clocks provider, as one of these devices needs to be activated
1479 * to enable the clock.
1481 ret = fimc_md_register_clk_provider(fmd);
1482 if (ret < 0) {
1483 v4l2_err(v4l2_dev, "clock provider registration failed\n");
1484 goto err_attr;
1487 if (fmd->num_sensors > 0) {
1488 fmd->subdev_notifier.subdevs = fmd->async_subdevs;
1489 fmd->subdev_notifier.num_subdevs = fmd->num_sensors;
1490 fmd->subdev_notifier.ops = &subdev_notifier_ops;
1491 fmd->num_sensors = 0;
1493 ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
1494 &fmd->subdev_notifier);
1495 if (ret)
1496 goto err_clk_p;
1499 return 0;
1501 err_clk_p:
1502 fimc_md_unregister_clk_provider(fmd);
1503 err_attr:
1504 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1505 err_clk:
1506 fimc_md_put_clocks(fmd);
1507 err_m_ent:
1508 fimc_md_unregister_entities(fmd);
1509 err_md:
1510 media_device_cleanup(&fmd->media_dev);
1511 v4l2_device_unregister(&fmd->v4l2_dev);
1512 return ret;
1515 static int fimc_md_remove(struct platform_device *pdev)
1517 struct fimc_md *fmd = platform_get_drvdata(pdev);
1519 if (!fmd)
1520 return 0;
1522 fimc_md_unregister_clk_provider(fmd);
1523 v4l2_async_notifier_unregister(&fmd->subdev_notifier);
1525 v4l2_device_unregister(&fmd->v4l2_dev);
1526 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1527 fimc_md_unregister_entities(fmd);
1528 fimc_md_pipelines_free(fmd);
1529 media_device_unregister(&fmd->media_dev);
1530 media_device_cleanup(&fmd->media_dev);
1531 fimc_md_put_clocks(fmd);
1533 return 0;
1536 static const struct platform_device_id fimc_driver_ids[] __always_unused = {
1537 { .name = "s5p-fimc-md" },
1538 { },
1540 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1542 static const struct of_device_id fimc_md_of_match[] = {
1543 { .compatible = "samsung,fimc" },
1544 { },
1546 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1548 static struct platform_driver fimc_md_driver = {
1549 .probe = fimc_md_probe,
1550 .remove = fimc_md_remove,
1551 .driver = {
1552 .of_match_table = of_match_ptr(fimc_md_of_match),
1553 .name = "s5p-fimc-md",
1557 static int __init fimc_md_init(void)
1559 int ret;
1561 request_module("s5p-csis");
1562 ret = fimc_register_driver();
1563 if (ret)
1564 return ret;
1566 return platform_driver_register(&fimc_md_driver);
1569 static void __exit fimc_md_exit(void)
1571 platform_driver_unregister(&fimc_md_driver);
1572 fimc_unregister_driver();
1575 module_init(fimc_md_init);
1576 module_exit(fimc_md_exit);
1578 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1579 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1580 MODULE_LICENSE("GPL");
1581 MODULE_VERSION("2.0.1");