1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * S5P/EXYNOS4 SoC series camera host interface media device driver
5 * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
6 * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
19 #include <linux/of_platform.h>
20 #include <linux/of_device.h>
21 #include <linux/of_graph.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/types.h>
26 #include <linux/slab.h>
27 #include <media/v4l2-async.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-fwnode.h>
30 #include <media/media-device.h>
31 #include <media/drv-intf/exynos-fimc.h>
33 #include "media-dev.h"
34 #include "fimc-core.h"
36 #include "fimc-lite.h"
37 #include "mipi-csis.h"
39 /* Set up image sensor subdev -> FIMC capture node notifications. */
40 static void __setup_sensor_notification(struct fimc_md *fmd,
41 struct v4l2_subdev *sensor,
42 struct v4l2_subdev *fimc_sd)
44 struct fimc_source_info *src_inf;
45 struct fimc_sensor_info *md_si;
48 src_inf = v4l2_get_subdev_hostdata(sensor);
49 if (!src_inf || WARN_ON(fmd == NULL))
52 md_si = source_to_sensor_info(src_inf);
53 spin_lock_irqsave(&fmd->slock, flags);
54 md_si->host = v4l2_get_subdevdata(fimc_sd);
55 spin_unlock_irqrestore(&fmd->slock, flags);
59 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
61 * @me: media entity terminating the pipeline
63 * Caller holds the graph mutex.
65 static void fimc_pipeline_prepare(struct fimc_pipeline *p,
66 struct media_entity *me)
68 struct fimc_md *fmd = entity_to_fimc_mdev(me);
69 struct v4l2_subdev *sd;
70 struct v4l2_subdev *sensor = NULL;
73 for (i = 0; i < IDX_MAX; i++)
77 struct media_pad *pad = NULL;
79 /* Find remote source pad */
80 for (i = 0; i < me->num_pads; i++) {
81 struct media_pad *spad = &me->pads[i];
82 if (!(spad->flags & MEDIA_PAD_FL_SINK))
84 pad = media_entity_remote_pad(spad);
89 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
91 sd = media_entity_to_v4l2_subdev(pad->entity);
97 case GRP_ID_FIMC_IS_SENSOR:
98 p->subdevs[IDX_SENSOR] = sd;
101 p->subdevs[IDX_CSIS] = sd;
104 p->subdevs[IDX_FLITE] = sd;
107 p->subdevs[IDX_FIMC] = sd;
110 p->subdevs[IDX_IS_ISP] = sd;
116 if (me->num_pads == 1)
120 if (sensor && p->subdevs[IDX_FIMC])
121 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
125 * __subdev_set_power - change power state of a single subdev
126 * @sd: subdevice to change power state for
127 * @on: 1 to enable power or 0 to disable
129 * Return result of s_power subdev operation or -ENXIO if sd argument
130 * is NULL. Return 0 if the subdevice does not implement s_power.
132 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
140 use_count = &sd->entity.use_count;
141 if (on && (*use_count)++ > 0)
143 else if (!on && (*use_count == 0 || --(*use_count) > 0))
145 ret = v4l2_subdev_call(sd, core, s_power, on);
147 return ret != -ENOIOCTLCMD ? ret : 0;
151 * fimc_pipeline_s_power - change power state of all pipeline subdevs
152 * @p: fimc device terminating the pipeline
153 * @on: true to power on, false to power off
155 * Needs to be called with the graph mutex held.
157 static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
159 static const u8 seq[2][IDX_MAX - 1] = {
160 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
161 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
165 if (p->subdevs[IDX_SENSOR] == NULL)
168 for (i = 0; i < IDX_MAX - 1; i++) {
169 unsigned int idx = seq[on][i];
171 ret = __subdev_set_power(p->subdevs[idx], on);
174 if (ret < 0 && ret != -ENXIO)
179 for (; i >= 0; i--) {
180 unsigned int idx = seq[on][i];
181 __subdev_set_power(p->subdevs[idx], !on);
187 * __fimc_pipeline_enable - enable power of all pipeline subdevs
188 * and the sensor clock
189 * @ep: video pipeline structure
190 * @fmd: fimc media device
192 * Called with the graph mutex held.
194 static int __fimc_pipeline_enable(struct exynos_media_pipeline *ep,
197 struct fimc_pipeline *p = to_fimc_pipeline(ep);
200 /* Enable PXLASYNC clock if this pipeline includes FIMC-IS */
201 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
202 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
207 ret = fimc_pipeline_s_power(p, 1);
211 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
212 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
218 * __fimc_pipeline_open - update the pipeline information, enable power
219 * of all pipeline subdevs and the sensor clock
220 * @ep: fimc device terminating the pipeline
221 * @me: media entity to start graph walk with
222 * @prepare: true to walk the current pipeline and acquire all subdevs
224 * Called with the graph mutex held.
226 static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
227 struct media_entity *me, bool prepare)
229 struct fimc_md *fmd = entity_to_fimc_mdev(me);
230 struct fimc_pipeline *p = to_fimc_pipeline(ep);
231 struct v4l2_subdev *sd;
233 if (WARN_ON(p == NULL || me == NULL))
237 fimc_pipeline_prepare(p, me);
239 sd = p->subdevs[IDX_SENSOR];
241 pr_warn("%s(): No sensor subdev\n", __func__);
243 * Pipeline open cannot fail so as to make it possible
244 * for the user space to configure the pipeline.
249 return __fimc_pipeline_enable(ep, fmd);
253 * __fimc_pipeline_close - disable the sensor clock and pipeline power
254 * @ep: fimc device terminating the pipeline
256 * Disable power of all subdevs and turn the external sensor clock off.
258 static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
260 struct fimc_pipeline *p = to_fimc_pipeline(ep);
261 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
266 pr_warn("%s(): No sensor subdev\n", __func__);
270 ret = fimc_pipeline_s_power(p, 0);
272 fmd = entity_to_fimc_mdev(&sd->entity);
274 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
275 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
276 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
278 return ret == -ENXIO ? 0 : ret;
282 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
283 * @ep: video pipeline structure
284 * @on: passed as the s_stream() callback argument
286 static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
288 static const u8 seq[2][IDX_MAX] = {
289 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
290 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
292 struct fimc_pipeline *p = to_fimc_pipeline(ep);
293 enum fimc_subdev_index sd_id;
296 if (p->subdevs[IDX_SENSOR] == NULL) {
298 struct v4l2_subdev *sd = p->subdevs[IDX_CSIS];
301 sd = p->subdevs[IDX_FIMC];
305 * If neither CSIS nor FIMC was set up,
306 * it's impossible to have any sensors
311 fmd = entity_to_fimc_mdev(&sd->entity);
313 if (!fmd->user_subdev_api) {
315 * Sensor must be already discovered if we
316 * aren't in the user_subdev_api mode
321 /* Get pipeline sink entity */
322 if (p->subdevs[IDX_FIMC])
324 else if (p->subdevs[IDX_IS_ISP])
326 else if (p->subdevs[IDX_FLITE])
332 * Sensor could have been linked between open and STREAMON -
333 * check if this is the case.
335 fimc_pipeline_prepare(p, &p->subdevs[sd_id]->entity);
337 if (p->subdevs[IDX_SENSOR] == NULL)
340 ret = __fimc_pipeline_enable(ep, fmd);
346 for (i = 0; i < IDX_MAX; i++) {
347 unsigned int idx = seq[on][i];
349 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
351 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
357 fimc_pipeline_s_power(p, !on);
358 for (; i >= 0; i--) {
359 unsigned int idx = seq[on][i];
360 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
365 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
366 static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
367 .open = __fimc_pipeline_open,
368 .close = __fimc_pipeline_close,
369 .set_stream = __fimc_pipeline_s_stream,
372 static struct exynos_media_pipeline *fimc_md_pipeline_create(
375 struct fimc_pipeline *p;
377 p = kzalloc(sizeof(*p), GFP_KERNEL);
381 list_add_tail(&p->list, &fmd->pipelines);
383 p->ep.ops = &fimc_pipeline_ops;
387 static void fimc_md_pipelines_free(struct fimc_md *fmd)
389 while (!list_empty(&fmd->pipelines)) {
390 struct fimc_pipeline *p;
392 p = list_entry(fmd->pipelines.next, typeof(*p), list);
398 static int fimc_md_parse_one_endpoint(struct fimc_md *fmd,
399 struct device_node *ep)
401 int index = fmd->num_sensors;
402 struct fimc_source_info *pd = &fmd->sensor[index].pdata;
403 struct device_node *rem, *np;
404 struct v4l2_async_subdev *asd;
405 struct v4l2_fwnode_endpoint endpoint = { .bus_type = 0 };
408 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &endpoint);
414 if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS) {
419 pd->mux_id = (endpoint.base.port - 1) & 0x1;
421 rem = of_graph_get_remote_port_parent(ep);
423 v4l2_info(&fmd->v4l2_dev, "Remote device at %pOF not found\n",
429 if (fimc_input_is_parallel(endpoint.base.port)) {
430 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
431 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
433 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
434 pd->flags = endpoint.bus.parallel.flags;
435 } else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
437 * MIPI CSI-2: only input mux selection and
438 * the sensor's clock frequency is needed.
440 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
442 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %pOF\n",
443 endpoint.base.port, rem);
446 * For FIMC-IS handled sensors, that are placed under i2c-isp device
447 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
448 * input. Sensors are attached to the FIMC-LITE hostdata interface
449 * directly or through MIPI-CSIS, depending on the external media bus
450 * used. This needs to be handled in a more reliable way, not by just
451 * checking parent's node name.
453 np = of_get_parent(rem);
456 if (of_node_name_eq(np, "i2c-isp"))
457 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
459 pd->fimc_bus_type = pd->sensor_bus_type;
462 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) {
467 asd = v4l2_async_notifier_add_fwnode_remote_subdev(
468 &fmd->subdev_notifier, of_fwnode_handle(ep), sizeof(*asd));
475 fmd->sensor[index].asd = asd;
481 /* Parse port node and register as a sub-device any sensor specified there. */
482 static int fimc_md_parse_port_node(struct fimc_md *fmd,
483 struct device_node *port)
485 struct device_node *ep;
488 for_each_child_of_node(port, ep) {
489 ret = fimc_md_parse_one_endpoint(fmd, ep);
497 /* Register all SoC external sub-devices */
498 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
500 struct device_node *parent = fmd->pdev->dev.of_node;
501 struct device_node *ports = NULL;
502 struct device_node *node;
506 * Runtime resume one of the FIMC entities to make sure
507 * the sclk_cam clocks are not globally disabled.
512 ret = pm_runtime_resume_and_get(fmd->pmf);
516 fmd->num_sensors = 0;
518 /* Attach sensors linked to MIPI CSI-2 receivers */
519 for_each_available_child_of_node(parent, node) {
520 struct device_node *port;
522 if (!of_node_name_eq(node, "csis"))
524 /* The csis node can have only port subnode. */
525 port = of_get_next_child(node, NULL);
529 ret = fimc_md_parse_port_node(fmd, port);
537 /* Attach sensors listed in the parallel-ports node */
538 ports = of_get_child_by_name(parent, "parallel-ports");
542 for_each_child_of_node(ports, node) {
543 ret = fimc_md_parse_port_node(fmd, node);
552 pm_runtime_put(fmd->pmf);
557 v4l2_async_notifier_cleanup(&fmd->subdev_notifier);
558 pm_runtime_put(fmd->pmf);
562 static int __of_get_csis_id(struct device_node *np)
566 np = of_get_child_by_name(np, "port");
569 of_property_read_u32(np, "reg", ®);
571 return reg - FIMC_INPUT_MIPI_CSI2_0;
575 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
577 static int register_fimc_lite_entity(struct fimc_md *fmd,
578 struct fimc_lite *fimc_lite)
580 struct v4l2_subdev *sd;
581 struct exynos_media_pipeline *ep;
584 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
585 fmd->fimc_lite[fimc_lite->index]))
588 sd = &fimc_lite->subdev;
589 sd->grp_id = GRP_ID_FLITE;
591 ep = fimc_md_pipeline_create(fmd);
595 v4l2_set_subdev_hostdata(sd, ep);
597 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
599 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
601 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
606 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
608 struct v4l2_subdev *sd;
609 struct exynos_media_pipeline *ep;
612 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
615 sd = &fimc->vid_cap.subdev;
616 sd->grp_id = GRP_ID_FIMC;
618 ep = fimc_md_pipeline_create(fmd);
622 v4l2_set_subdev_hostdata(sd, ep);
624 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
626 if (!fmd->pmf && fimc->pdev)
627 fmd->pmf = &fimc->pdev->dev;
628 fmd->fimc[fimc->id] = fimc;
629 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
631 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
637 static int register_csis_entity(struct fimc_md *fmd,
638 struct platform_device *pdev,
639 struct v4l2_subdev *sd)
641 struct device_node *node = pdev->dev.of_node;
644 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
646 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
649 if (WARN_ON(fmd->csis[id].sd))
652 sd->grp_id = GRP_ID_CSIS;
653 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
655 fmd->csis[id].sd = sd;
657 v4l2_err(&fmd->v4l2_dev,
658 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
662 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
664 struct v4l2_subdev *sd = &is->isp.subdev;
665 struct exynos_media_pipeline *ep;
668 /* Allocate pipeline object for the ISP capture video node. */
669 ep = fimc_md_pipeline_create(fmd);
673 v4l2_set_subdev_hostdata(sd, ep);
675 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
677 v4l2_err(&fmd->v4l2_dev,
678 "Failed to register FIMC-ISP (%d)\n", ret);
686 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
687 struct platform_device *pdev,
690 struct device *dev = &pdev->dev;
691 int ret = -EPROBE_DEFER;
694 /* Lock to ensure dev->driver won't change. */
697 if (!dev->driver || !try_module_get(dev->driver->owner))
700 drvdata = dev_get_drvdata(dev);
701 /* Some subdev didn't probe successfully id drvdata is NULL */
703 switch (plat_entity) {
705 ret = register_fimc_entity(fmd, drvdata);
708 ret = register_fimc_lite_entity(fmd, drvdata);
711 ret = register_csis_entity(fmd, pdev, drvdata);
714 ret = register_fimc_is_entity(fmd, drvdata);
721 module_put(dev->driver->owner);
724 if (ret == -EPROBE_DEFER)
725 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
728 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
733 /* Register FIMC, FIMC-LITE and CSIS media entities */
734 static int fimc_md_register_platform_entities(struct fimc_md *fmd,
735 struct device_node *parent)
737 struct device_node *node;
740 for_each_available_child_of_node(parent, node) {
741 struct platform_device *pdev;
742 int plat_entity = -1;
744 pdev = of_find_device_by_node(node);
748 /* If driver of any entity isn't ready try all again later. */
749 if (of_node_name_eq(node, CSIS_OF_NODE_NAME))
750 plat_entity = IDX_CSIS;
751 else if (of_node_name_eq(node, FIMC_IS_OF_NODE_NAME))
752 plat_entity = IDX_IS_ISP;
753 else if (of_node_name_eq(node, FIMC_LITE_OF_NODE_NAME))
754 plat_entity = IDX_FLITE;
755 else if (of_node_name_eq(node, FIMC_OF_NODE_NAME) &&
756 !of_property_read_bool(node, "samsung,lcd-wb"))
757 plat_entity = IDX_FIMC;
759 if (plat_entity >= 0)
760 ret = fimc_md_register_platform_entity(fmd, pdev,
762 put_device(&pdev->dev);
772 static void fimc_md_unregister_entities(struct fimc_md *fmd)
776 for (i = 0; i < FIMC_MAX_DEVS; i++) {
777 struct fimc_dev *dev = fmd->fimc[i];
780 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
781 dev->vid_cap.ve.pipe = NULL;
784 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
785 struct fimc_lite *dev = fmd->fimc_lite[i];
788 v4l2_device_unregister_subdev(&dev->subdev);
790 fmd->fimc_lite[i] = NULL;
792 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
793 if (fmd->csis[i].sd == NULL)
795 v4l2_device_unregister_subdev(fmd->csis[i].sd);
796 fmd->csis[i].sd = NULL;
800 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
802 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
806 * __fimc_md_create_fimc_links - create links to all FIMC entities
807 * @fmd: fimc media device
808 * @source: the source entity to create links to all fimc entities from
809 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
810 * @pad: the source entity pad index
811 * @link_mask: bitmask of the fimc devices for which link should be enabled
813 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
814 struct media_entity *source,
815 struct v4l2_subdev *sensor,
816 int pad, int link_mask)
818 struct fimc_source_info *si = NULL;
819 struct media_entity *sink;
820 unsigned int flags = 0;
824 si = v4l2_get_subdev_hostdata(sensor);
825 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
826 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
830 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
834 * Some FIMC variants are not fitted with camera capture
835 * interface. Skip creating a link from sensor for those.
837 if (!fmd->fimc[i]->variant->has_cam_if)
840 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
842 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
843 ret = media_create_pad_link(source, pad, sink,
844 FIMC_SD_PAD_SINK_CAM, flags);
848 /* Notify FIMC capture subdev entity */
849 ret = media_entity_call(sink, link_setup, &sink->pads[0],
850 &source->pads[pad], flags);
854 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
855 source->name, flags ? '=' : '-', sink->name);
858 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
859 if (!fmd->fimc_lite[i])
862 sink = &fmd->fimc_lite[i]->subdev.entity;
863 ret = media_create_pad_link(source, pad, sink,
864 FLITE_SD_PAD_SINK, 0);
868 /* Notify FIMC-LITE subdev entity */
869 ret = media_entity_call(sink, link_setup, &sink->pads[0],
870 &source->pads[pad], 0);
874 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
875 source->name, sink->name);
880 /* Create links from FIMC-LITE source pads to other entities */
881 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
883 struct media_entity *source, *sink;
886 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
887 struct fimc_lite *fimc = fmd->fimc_lite[i];
892 source = &fimc->subdev.entity;
893 sink = &fimc->ve.vdev.entity;
894 /* FIMC-LITE's subdev and video node */
895 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
899 /* Link from FIMC-LITE to IS-ISP subdev */
900 sink = &fmd->fimc_is->isp.subdev.entity;
901 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
910 /* Create FIMC-IS links */
911 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
913 struct fimc_isp *isp = &fmd->fimc_is->isp;
914 struct media_entity *source, *sink;
917 source = &isp->subdev.entity;
919 for (i = 0; i < FIMC_MAX_DEVS; i++) {
920 if (fmd->fimc[i] == NULL)
923 /* Link from FIMC-IS-ISP subdev to FIMC */
924 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
925 ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
926 sink, FIMC_SD_PAD_SINK_FIFO, 0);
931 /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
932 sink = &isp->video_capture.ve.vdev.entity;
934 /* Skip this link if the fimc-is-isp video node driver isn't built-in */
935 if (sink->num_pads == 0)
938 return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
943 * fimc_md_create_links - create default links between registered entities
944 * @fmd: fimc media device
946 * Parallel interface sensor entities are connected directly to FIMC capture
947 * entities. The sensors using MIPI CSIS bus are connected through immutable
948 * link with CSI receiver entity specified by mux_id. Any registered CSIS
949 * entity has a link to each registered FIMC capture entity. Enabled links
950 * are created by default between each subsequent registered sensor and
951 * subsequent FIMC capture entity. The number of default active links is
952 * determined by the number of available sensors or FIMC entities,
955 static int fimc_md_create_links(struct fimc_md *fmd)
957 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
958 struct v4l2_subdev *sensor, *csis;
959 struct fimc_source_info *pdata;
960 struct media_entity *source, *sink;
961 int i, pad, fimc_id = 0, ret = 0;
962 u32 flags, link_mask = 0;
964 for (i = 0; i < fmd->num_sensors; i++) {
965 if (fmd->sensor[i].subdev == NULL)
968 sensor = fmd->sensor[i].subdev;
969 pdata = v4l2_get_subdev_hostdata(sensor);
975 switch (pdata->sensor_bus_type) {
976 case FIMC_BUS_TYPE_MIPI_CSI2:
977 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
978 "Wrong CSI channel id: %d\n", pdata->mux_id))
981 csis = fmd->csis[pdata->mux_id].sd;
982 if (WARN(csis == NULL,
983 "MIPI-CSI interface specified but s5p-csis module is not loaded!\n"))
986 pad = sensor->entity.num_pads - 1;
987 ret = media_create_pad_link(&sensor->entity, pad,
988 &csis->entity, CSIS_PAD_SINK,
989 MEDIA_LNK_FL_IMMUTABLE |
990 MEDIA_LNK_FL_ENABLED);
994 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
995 sensor->entity.name, csis->entity.name);
998 csi_sensors[pdata->mux_id] = sensor;
1001 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
1002 source = &sensor->entity;
1007 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
1008 pdata->sensor_bus_type);
1014 link_mask = 1 << fimc_id++;
1015 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1019 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
1020 if (fmd->csis[i].sd == NULL)
1023 source = &fmd->csis[i].sd->entity;
1024 pad = CSIS_PAD_SOURCE;
1025 sensor = csi_sensors[i];
1027 link_mask = 1 << fimc_id++;
1028 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1032 /* Create immutable links between each FIMC's subdev and video node */
1033 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1034 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1038 source = &fmd->fimc[i]->vid_cap.subdev.entity;
1039 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1041 ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
1047 ret = __fimc_md_create_flite_source_links(fmd);
1052 ret = __fimc_md_create_fimc_is_links(fmd);
1058 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1060 static void fimc_md_put_clocks(struct fimc_md *fmd)
1062 int i = FIMC_MAX_CAMCLKS;
1065 if (IS_ERR(fmd->camclk[i].clock))
1067 clk_put(fmd->camclk[i].clock);
1068 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1071 /* Writeback (PIXELASYNCMx) clocks */
1072 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1073 if (IS_ERR(fmd->wbclk[i]))
1075 clk_put(fmd->wbclk[i]);
1076 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1080 static int fimc_md_get_clocks(struct fimc_md *fmd)
1082 struct device *dev = &fmd->pdev->dev;
1087 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1088 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1090 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1091 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1092 clock = clk_get(dev, clk_name);
1094 if (IS_ERR(clock)) {
1095 dev_err(dev, "Failed to get clock: %s\n", clk_name);
1096 ret = PTR_ERR(clock);
1099 fmd->camclk[i].clock = clock;
1102 fimc_md_put_clocks(fmd);
1107 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1108 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1110 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1112 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1113 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1114 clock = clk_get(dev, clk_name);
1115 if (IS_ERR(clock)) {
1116 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1118 ret = PTR_ERR(clock);
1121 fmd->wbclk[i] = clock;
1124 fimc_md_put_clocks(fmd);
1129 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1131 struct exynos_video_entity *ve;
1132 struct fimc_pipeline *p;
1133 struct video_device *vdev;
1136 vdev = media_entity_to_video_device(entity);
1137 if (vdev->entity.use_count == 0)
1140 ve = vdev_to_exynos_video_entity(vdev);
1141 p = to_fimc_pipeline(ve->pipe);
1143 * Nothing to do if we are disabling the pipeline, some link
1144 * has been disconnected and p->subdevs array is cleared now.
1146 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1150 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1152 ret = __fimc_pipeline_close(ve->pipe);
1154 if (ret == 0 && !enable)
1155 memset(p->subdevs, 0, sizeof(p->subdevs));
1160 /* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
1161 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1162 struct media_graph *graph)
1164 struct media_entity *entity_err = entity;
1168 * Walk current graph and call the pipeline open/close routine for each
1169 * opened video node that belongs to the graph of entities connected
1170 * through active links. This is needed as we cannot power on/off the
1171 * subdevs in random order.
1173 media_graph_walk_start(graph, entity);
1175 while ((entity = media_graph_walk_next(graph))) {
1176 if (!is_media_entity_v4l2_video_device(entity))
1179 ret = __fimc_md_modify_pipeline(entity, enable);
1188 media_graph_walk_start(graph, entity_err);
1190 while ((entity_err = media_graph_walk_next(graph))) {
1191 if (!is_media_entity_v4l2_video_device(entity_err))
1194 __fimc_md_modify_pipeline(entity_err, !enable);
1196 if (entity_err == entity)
1203 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1204 unsigned int notification)
1206 struct media_graph *graph =
1207 &container_of(link->graph_obj.mdev, struct fimc_md,
1208 media_dev)->link_setup_graph;
1209 struct media_entity *sink = link->sink->entity;
1212 /* Before link disconnection */
1213 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1214 ret = media_graph_walk_init(graph,
1215 link->graph_obj.mdev);
1218 if (!(flags & MEDIA_LNK_FL_ENABLED))
1219 ret = __fimc_md_modify_pipelines(sink, false, graph);
1222 /* TODO: Link state change validation */
1224 /* After link activation */
1225 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1226 if (link->flags & MEDIA_LNK_FL_ENABLED)
1227 ret = __fimc_md_modify_pipelines(sink, true, graph);
1228 media_graph_walk_cleanup(graph);
1231 return ret ? -EPIPE : 0;
1234 static const struct media_device_ops fimc_md_ops = {
1235 .link_notify = fimc_md_link_notify,
1238 static ssize_t fimc_md_sysfs_show(struct device *dev,
1239 struct device_attribute *attr, char *buf)
1241 struct fimc_md *fmd = dev_get_drvdata(dev);
1243 if (fmd->user_subdev_api)
1244 return strscpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1246 return strscpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1249 static ssize_t fimc_md_sysfs_store(struct device *dev,
1250 struct device_attribute *attr,
1251 const char *buf, size_t count)
1253 struct fimc_md *fmd = dev_get_drvdata(dev);
1257 if (!strcmp(buf, "vid-dev\n"))
1259 else if (!strcmp(buf, "sub-dev\n"))
1264 fmd->user_subdev_api = subdev_api;
1265 for (i = 0; i < FIMC_MAX_DEVS; i++)
1267 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1271 * This device attribute is to select video pipeline configuration method.
1272 * There are following valid values:
1273 * vid-dev - for V4L2 video node API only, subdevice will be configured
1274 * by the host driver.
1275 * sub-dev - for media controller API, subdevs must be configured in user
1276 * space before starting streaming.
1278 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1279 fimc_md_sysfs_show, fimc_md_sysfs_store);
1281 static int cam_clk_prepare(struct clk_hw *hw)
1283 struct cam_clk *camclk = to_cam_clk(hw);
1285 if (camclk->fmd->pmf == NULL)
1288 return pm_runtime_resume_and_get(camclk->fmd->pmf);
1291 static void cam_clk_unprepare(struct clk_hw *hw)
1293 struct cam_clk *camclk = to_cam_clk(hw);
1295 if (camclk->fmd->pmf == NULL)
1298 pm_runtime_put_sync(camclk->fmd->pmf);
1301 static const struct clk_ops cam_clk_ops = {
1302 .prepare = cam_clk_prepare,
1303 .unprepare = cam_clk_unprepare,
1306 static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1308 struct cam_clk_provider *cp = &fmd->clk_provider;
1312 of_clk_del_provider(cp->of_node);
1314 for (i = 0; i < cp->num_clocks; i++)
1315 clk_unregister(cp->clks[i]);
1318 static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1320 struct cam_clk_provider *cp = &fmd->clk_provider;
1321 struct device *dev = &fmd->pdev->dev;
1324 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1325 struct cam_clk *camclk = &cp->camclk[i];
1326 struct clk_init_data init;
1329 ret = of_property_read_string_index(dev->of_node,
1330 "clock-output-names", i, &init.name);
1334 p_name = __clk_get_name(fmd->camclk[i].clock);
1336 /* It's safe since clk_register() will duplicate the string. */
1337 init.parent_names = &p_name;
1338 init.num_parents = 1;
1339 init.ops = &cam_clk_ops;
1340 init.flags = CLK_SET_RATE_PARENT;
1341 camclk->hw.init = &init;
1344 cp->clks[i] = clk_register(NULL, &camclk->hw);
1345 if (IS_ERR(cp->clks[i])) {
1346 dev_err(dev, "failed to register clock: %s (%ld)\n",
1347 init.name, PTR_ERR(cp->clks[i]));
1348 ret = PTR_ERR(cp->clks[i]);
1354 if (cp->num_clocks == 0) {
1355 dev_warn(dev, "clk provider not registered\n");
1359 cp->clk_data.clks = cp->clks;
1360 cp->clk_data.clk_num = cp->num_clocks;
1361 cp->of_node = dev->of_node;
1362 ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1367 fimc_md_unregister_clk_provider(fmd);
1371 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1372 struct v4l2_subdev *subdev,
1373 struct v4l2_async_subdev *asd)
1375 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1376 struct fimc_sensor_info *si = NULL;
1379 /* Find platform data for this sensor subdev */
1380 for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1381 if (fmd->sensor[i].asd == asd)
1382 si = &fmd->sensor[i];
1387 v4l2_set_subdev_hostdata(subdev, &si->pdata);
1389 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
1390 subdev->grp_id = GRP_ID_FIMC_IS_SENSOR;
1392 subdev->grp_id = GRP_ID_SENSOR;
1394 si->subdev = subdev;
1396 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
1397 subdev->name, fmd->num_sensors);
1404 static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1406 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1409 mutex_lock(&fmd->media_dev.graph_mutex);
1411 ret = fimc_md_create_links(fmd);
1415 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1417 mutex_unlock(&fmd->media_dev.graph_mutex);
1421 return media_device_register(&fmd->media_dev);
1424 static const struct v4l2_async_notifier_operations subdev_notifier_ops = {
1425 .bound = subdev_notifier_bound,
1426 .complete = subdev_notifier_complete,
1429 static int fimc_md_probe(struct platform_device *pdev)
1431 struct device *dev = &pdev->dev;
1432 struct v4l2_device *v4l2_dev;
1433 struct pinctrl *pinctrl;
1434 struct fimc_md *fmd;
1437 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1441 spin_lock_init(&fmd->slock);
1442 INIT_LIST_HEAD(&fmd->pipelines);
1445 strscpy(fmd->media_dev.model, "Samsung S5P FIMC",
1446 sizeof(fmd->media_dev.model));
1447 fmd->media_dev.ops = &fimc_md_ops;
1448 fmd->media_dev.dev = dev;
1450 v4l2_dev = &fmd->v4l2_dev;
1451 v4l2_dev->mdev = &fmd->media_dev;
1452 v4l2_dev->notify = fimc_sensor_notify;
1453 strscpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1455 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1456 fmd->user_subdev_api = true;
1458 media_device_init(&fmd->media_dev);
1460 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1462 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1466 ret = fimc_md_get_clocks(fmd);
1470 pinctrl = devm_pinctrl_get(dev);
1471 if (IS_ERR(pinctrl)) {
1472 ret = PTR_ERR(pinctrl);
1473 if (ret != -EPROBE_DEFER)
1474 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1478 platform_set_drvdata(pdev, fmd);
1480 v4l2_async_notifier_init(&fmd->subdev_notifier);
1482 ret = fimc_md_register_platform_entities(fmd, dev->of_node);
1486 ret = fimc_md_register_sensor_entities(fmd);
1490 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1494 * FIMC platform devices need to be registered before the sclk_cam
1495 * clocks provider, as one of these devices needs to be activated
1496 * to enable the clock.
1498 ret = fimc_md_register_clk_provider(fmd);
1500 v4l2_err(v4l2_dev, "clock provider registration failed\n");
1504 if (fmd->num_sensors > 0) {
1505 fmd->subdev_notifier.ops = &subdev_notifier_ops;
1506 fmd->num_sensors = 0;
1508 ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
1509 &fmd->subdev_notifier);
1517 fimc_md_unregister_clk_provider(fmd);
1519 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1521 v4l2_async_notifier_cleanup(&fmd->subdev_notifier);
1523 fimc_md_unregister_entities(fmd);
1525 fimc_md_put_clocks(fmd);
1527 v4l2_device_unregister(&fmd->v4l2_dev);
1529 media_device_cleanup(&fmd->media_dev);
1533 static int fimc_md_remove(struct platform_device *pdev)
1535 struct fimc_md *fmd = platform_get_drvdata(pdev);
1540 fimc_md_unregister_clk_provider(fmd);
1541 v4l2_async_notifier_unregister(&fmd->subdev_notifier);
1542 v4l2_async_notifier_cleanup(&fmd->subdev_notifier);
1544 v4l2_device_unregister(&fmd->v4l2_dev);
1545 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1546 fimc_md_unregister_entities(fmd);
1547 fimc_md_pipelines_free(fmd);
1548 media_device_unregister(&fmd->media_dev);
1549 media_device_cleanup(&fmd->media_dev);
1550 fimc_md_put_clocks(fmd);
1555 static const struct platform_device_id fimc_driver_ids[] __always_unused = {
1556 { .name = "s5p-fimc-md" },
1559 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1561 static const struct of_device_id fimc_md_of_match[] = {
1562 { .compatible = "samsung,fimc" },
1565 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1567 static struct platform_driver fimc_md_driver = {
1568 .probe = fimc_md_probe,
1569 .remove = fimc_md_remove,
1571 .of_match_table = of_match_ptr(fimc_md_of_match),
1572 .name = "s5p-fimc-md",
1576 static int __init fimc_md_init(void)
1580 request_module("s5p-csis");
1581 ret = fimc_register_driver();
1585 ret = platform_driver_register(&fimc_md_driver);
1587 fimc_unregister_driver();
1592 static void __exit fimc_md_exit(void)
1594 platform_driver_unregister(&fimc_md_driver);
1595 fimc_unregister_driver();
1598 module_init(fimc_md_init);
1599 module_exit(fimc_md_exit);
1601 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1602 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1603 MODULE_LICENSE("GPL");
1604 MODULE_VERSION("2.0.1");