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>
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-of.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"
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;
51 src_inf = v4l2_get_subdev_hostdata(sensor);
52 if (!src_inf || WARN_ON(fmd == NULL))
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);
62 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
63 * @me: media entity terminating the pipeline
65 * Caller holds the graph mutex.
67 static void fimc_pipeline_prepare(struct fimc_pipeline *p,
68 struct media_entity *me)
70 struct fimc_md *fmd = entity_to_fimc_mdev(me);
71 struct v4l2_subdev *sd;
72 struct v4l2_subdev *sensor = NULL;
75 for (i = 0; i < IDX_MAX; i++)
79 struct media_pad *pad = NULL;
81 /* Find remote source pad */
82 for (i = 0; i < me->num_pads; i++) {
83 struct media_pad *spad = &me->pads[i];
84 if (!(spad->flags & MEDIA_PAD_FL_SINK))
86 pad = media_entity_remote_pad(spad);
91 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
93 sd = media_entity_to_v4l2_subdev(pad->entity);
99 case GRP_ID_FIMC_IS_SENSOR:
100 p->subdevs[IDX_SENSOR] = sd;
103 p->subdevs[IDX_CSIS] = sd;
106 p->subdevs[IDX_FLITE] = sd;
109 p->subdevs[IDX_FIMC] = sd;
112 p->subdevs[IDX_IS_ISP] = sd;
118 if (me->num_pads == 1)
122 if (sensor && p->subdevs[IDX_FIMC])
123 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
127 * __subdev_set_power - change power state of a single subdev
128 * @sd: subdevice to change power state for
129 * @on: 1 to enable power or 0 to disable
131 * Return result of s_power subdev operation or -ENXIO if sd argument
132 * is NULL. Return 0 if the subdevice does not implement s_power.
134 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
142 use_count = &sd->entity.use_count;
143 if (on && (*use_count)++ > 0)
145 else if (!on && (*use_count == 0 || --(*use_count) > 0))
147 ret = v4l2_subdev_call(sd, core, s_power, on);
149 return ret != -ENOIOCTLCMD ? ret : 0;
153 * fimc_pipeline_s_power - change power state of all pipeline subdevs
154 * @fimc: fimc device terminating the pipeline
155 * @state: true to power on, false to power off
157 * Needs to be called with the graph mutex held.
159 static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
161 static const u8 seq[2][IDX_MAX - 1] = {
162 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
163 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
167 if (p->subdevs[IDX_SENSOR] == NULL)
170 for (i = 0; i < IDX_MAX - 1; i++) {
171 unsigned int idx = seq[on][i];
173 ret = __subdev_set_power(p->subdevs[idx], on);
176 if (ret < 0 && ret != -ENXIO)
181 for (; i >= 0; i--) {
182 unsigned int idx = seq[on][i];
183 __subdev_set_power(p->subdevs[idx], !on);
189 * __fimc_pipeline_enable - enable power of all pipeline subdevs
190 * and the sensor clock
191 * @ep: video pipeline structure
192 * @fmd: fimc media device
194 * Called with the graph mutex held.
196 static int __fimc_pipeline_enable(struct exynos_media_pipeline *ep,
199 struct fimc_pipeline *p = to_fimc_pipeline(ep);
202 /* Enable PXLASYNC clock if this pipeline includes FIMC-IS */
203 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
204 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
209 ret = fimc_pipeline_s_power(p, 1);
213 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
214 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
220 * __fimc_pipeline_open - update the pipeline information, enable power
221 * of all pipeline subdevs and the sensor clock
222 * @me: media entity to start graph walk with
223 * @prepare: true to walk the current pipeline and acquire all subdevs
225 * Called with the graph mutex held.
227 static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
228 struct media_entity *me, bool prepare)
230 struct fimc_md *fmd = entity_to_fimc_mdev(me);
231 struct fimc_pipeline *p = to_fimc_pipeline(ep);
232 struct v4l2_subdev *sd;
234 if (WARN_ON(p == NULL || me == NULL))
238 fimc_pipeline_prepare(p, me);
240 sd = p->subdevs[IDX_SENSOR];
242 pr_warn("%s(): No sensor subdev\n", __func__);
244 * Pipeline open cannot fail so as to make it possible
245 * for the user space to configure the pipeline.
250 return __fimc_pipeline_enable(ep, fmd);
254 * __fimc_pipeline_close - disable the sensor clock and pipeline power
255 * @fimc: fimc device terminating the pipeline
257 * Disable power of all subdevs and turn the external sensor clock off.
259 static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
261 struct fimc_pipeline *p = to_fimc_pipeline(ep);
262 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
267 pr_warn("%s(): No sensor subdev\n", __func__);
271 ret = fimc_pipeline_s_power(p, 0);
273 fmd = entity_to_fimc_mdev(&sd->entity);
275 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
276 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
277 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
279 return ret == -ENXIO ? 0 : ret;
283 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
284 * @pipeline: video pipeline structure
285 * @on: passed as the s_stream() callback argument
287 static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
289 static const u8 seq[2][IDX_MAX] = {
290 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
291 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
293 struct fimc_pipeline *p = to_fimc_pipeline(ep);
294 struct fimc_md *fmd = entity_to_fimc_mdev(&p->subdevs[IDX_CSIS]->entity);
295 enum fimc_subdev_index sd_id;
298 if (p->subdevs[IDX_SENSOR] == NULL) {
299 if (!fmd->user_subdev_api) {
301 * Sensor must be already discovered if we
302 * aren't in the user_subdev_api mode
307 /* Get pipeline sink entity */
308 if (p->subdevs[IDX_FIMC])
310 else if (p->subdevs[IDX_IS_ISP])
312 else if (p->subdevs[IDX_FLITE])
318 * Sensor could have been linked between open and STREAMON -
319 * check if this is the case.
321 fimc_pipeline_prepare(p, &p->subdevs[sd_id]->entity);
323 if (p->subdevs[IDX_SENSOR] == NULL)
326 ret = __fimc_pipeline_enable(ep, fmd);
332 for (i = 0; i < IDX_MAX; i++) {
333 unsigned int idx = seq[on][i];
335 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
337 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
343 fimc_pipeline_s_power(p, !on);
344 for (; i >= 0; i--) {
345 unsigned int idx = seq[on][i];
346 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
351 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
352 static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
353 .open = __fimc_pipeline_open,
354 .close = __fimc_pipeline_close,
355 .set_stream = __fimc_pipeline_s_stream,
358 static struct exynos_media_pipeline *fimc_md_pipeline_create(
361 struct fimc_pipeline *p;
363 p = kzalloc(sizeof(*p), GFP_KERNEL);
367 list_add_tail(&p->list, &fmd->pipelines);
369 p->ep.ops = &fimc_pipeline_ops;
373 static void fimc_md_pipelines_free(struct fimc_md *fmd)
375 while (!list_empty(&fmd->pipelines)) {
376 struct fimc_pipeline *p;
378 p = list_entry(fmd->pipelines.next, typeof(*p), list);
384 /* Parse port node and register as a sub-device any sensor specified there. */
385 static int fimc_md_parse_port_node(struct fimc_md *fmd,
386 struct device_node *port,
389 struct fimc_source_info *pd = &fmd->sensor[index].pdata;
390 struct device_node *rem, *ep, *np;
391 struct v4l2_of_endpoint endpoint;
394 /* Assume here a port node can have only one endpoint node. */
395 ep = of_get_next_child(port, NULL);
399 ret = v4l2_of_parse_endpoint(ep, &endpoint);
405 if (WARN_ON(endpoint.base.port == 0) || index >= FIMC_MAX_SENSORS)
408 pd->mux_id = (endpoint.base.port - 1) & 0x1;
410 rem = of_graph_get_remote_port_parent(ep);
413 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
418 if (fimc_input_is_parallel(endpoint.base.port)) {
419 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
420 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
422 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
423 pd->flags = endpoint.bus.parallel.flags;
424 } else if (fimc_input_is_mipi_csi(endpoint.base.port)) {
426 * MIPI CSI-2: only input mux selection and
427 * the sensor's clock frequency is needed.
429 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
431 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
432 endpoint.base.port, rem->full_name);
435 * For FIMC-IS handled sensors, that are placed under i2c-isp device
436 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
437 * input. Sensors are attached to the FIMC-LITE hostdata interface
438 * directly or through MIPI-CSIS, depending on the external media bus
439 * used. This needs to be handled in a more reliable way, not by just
440 * checking parent's node name.
442 np = of_get_parent(rem);
444 if (np && !of_node_cmp(np->name, "i2c-isp"))
445 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
447 pd->fimc_bus_type = pd->sensor_bus_type;
449 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor))) {
454 fmd->sensor[index].asd.match_type = V4L2_ASYNC_MATCH_OF;
455 fmd->sensor[index].asd.match.of.node = rem;
456 fmd->async_subdevs[index] = &fmd->sensor[index].asd;
464 /* Register all SoC external sub-devices */
465 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
467 struct device_node *parent = fmd->pdev->dev.of_node;
468 struct device_node *node, *ports;
473 * Runtime resume one of the FIMC entities to make sure
474 * the sclk_cam clocks are not globally disabled.
479 ret = pm_runtime_get_sync(fmd->pmf);
481 pm_runtime_put(fmd->pmf);
485 fmd->num_sensors = 0;
487 /* Attach sensors linked to MIPI CSI-2 receivers */
488 for_each_available_child_of_node(parent, node) {
489 struct device_node *port;
491 if (of_node_cmp(node->name, "csis"))
493 /* The csis node can have only port subnode. */
494 port = of_get_next_child(node, NULL);
498 ret = fimc_md_parse_port_node(fmd, port, index);
507 /* Attach sensors listed in the parallel-ports node */
508 ports = of_get_child_by_name(parent, "parallel-ports");
512 for_each_child_of_node(ports, node) {
513 ret = fimc_md_parse_port_node(fmd, node, index);
521 pm_runtime_put(fmd->pmf);
525 static int __of_get_csis_id(struct device_node *np)
529 np = of_get_child_by_name(np, "port");
532 of_property_read_u32(np, "reg", ®);
534 return reg - FIMC_INPUT_MIPI_CSI2_0;
538 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
540 static int register_fimc_lite_entity(struct fimc_md *fmd,
541 struct fimc_lite *fimc_lite)
543 struct v4l2_subdev *sd;
544 struct exynos_media_pipeline *ep;
547 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
548 fmd->fimc_lite[fimc_lite->index]))
551 sd = &fimc_lite->subdev;
552 sd->grp_id = GRP_ID_FLITE;
554 ep = fimc_md_pipeline_create(fmd);
558 v4l2_set_subdev_hostdata(sd, ep);
560 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
562 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
564 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
569 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
571 struct v4l2_subdev *sd;
572 struct exynos_media_pipeline *ep;
575 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
578 sd = &fimc->vid_cap.subdev;
579 sd->grp_id = GRP_ID_FIMC;
581 ep = fimc_md_pipeline_create(fmd);
585 v4l2_set_subdev_hostdata(sd, ep);
587 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
589 if (!fmd->pmf && fimc->pdev)
590 fmd->pmf = &fimc->pdev->dev;
591 fmd->fimc[fimc->id] = fimc;
592 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
594 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
600 static int register_csis_entity(struct fimc_md *fmd,
601 struct platform_device *pdev,
602 struct v4l2_subdev *sd)
604 struct device_node *node = pdev->dev.of_node;
607 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
609 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
612 if (WARN_ON(fmd->csis[id].sd))
615 sd->grp_id = GRP_ID_CSIS;
616 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
618 fmd->csis[id].sd = sd;
620 v4l2_err(&fmd->v4l2_dev,
621 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
625 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
627 struct v4l2_subdev *sd = &is->isp.subdev;
628 struct exynos_media_pipeline *ep;
631 /* Allocate pipeline object for the ISP capture video node. */
632 ep = fimc_md_pipeline_create(fmd);
636 v4l2_set_subdev_hostdata(sd, ep);
638 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
640 v4l2_err(&fmd->v4l2_dev,
641 "Failed to register FIMC-ISP (%d)\n", ret);
649 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
650 struct platform_device *pdev,
653 struct device *dev = &pdev->dev;
654 int ret = -EPROBE_DEFER;
657 /* Lock to ensure dev->driver won't change. */
660 if (!dev->driver || !try_module_get(dev->driver->owner))
663 drvdata = dev_get_drvdata(dev);
664 /* Some subdev didn't probe successfully id drvdata is NULL */
666 switch (plat_entity) {
668 ret = register_fimc_entity(fmd, drvdata);
671 ret = register_fimc_lite_entity(fmd, drvdata);
674 ret = register_csis_entity(fmd, pdev, drvdata);
677 ret = register_fimc_is_entity(fmd, drvdata);
684 module_put(dev->driver->owner);
687 if (ret == -EPROBE_DEFER)
688 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
691 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
696 /* Register FIMC, FIMC-LITE and CSIS media entities */
697 static int fimc_md_register_platform_entities(struct fimc_md *fmd,
698 struct device_node *parent)
700 struct device_node *node;
703 for_each_available_child_of_node(parent, node) {
704 struct platform_device *pdev;
705 int plat_entity = -1;
707 pdev = of_find_device_by_node(node);
711 /* If driver of any entity isn't ready try all again later. */
712 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
713 plat_entity = IDX_CSIS;
714 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
715 plat_entity = IDX_IS_ISP;
716 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
717 plat_entity = IDX_FLITE;
718 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
719 !of_property_read_bool(node, "samsung,lcd-wb"))
720 plat_entity = IDX_FIMC;
722 if (plat_entity >= 0)
723 ret = fimc_md_register_platform_entity(fmd, pdev,
725 put_device(&pdev->dev);
735 static void fimc_md_unregister_entities(struct fimc_md *fmd)
739 for (i = 0; i < FIMC_MAX_DEVS; i++) {
740 struct fimc_dev *dev = fmd->fimc[i];
743 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
744 dev->vid_cap.ve.pipe = NULL;
747 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
748 struct fimc_lite *dev = fmd->fimc_lite[i];
751 v4l2_device_unregister_subdev(&dev->subdev);
753 fmd->fimc_lite[i] = NULL;
755 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
756 if (fmd->csis[i].sd == NULL)
758 v4l2_device_unregister_subdev(fmd->csis[i].sd);
759 fmd->csis[i].sd = NULL;
763 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
765 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
769 * __fimc_md_create_fimc_links - create links to all FIMC entities
770 * @fmd: fimc media device
771 * @source: the source entity to create links to all fimc entities from
772 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
773 * @pad: the source entity pad index
774 * @link_mask: bitmask of the fimc devices for which link should be enabled
776 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
777 struct media_entity *source,
778 struct v4l2_subdev *sensor,
779 int pad, int link_mask)
781 struct fimc_source_info *si = NULL;
782 struct media_entity *sink;
783 unsigned int flags = 0;
787 si = v4l2_get_subdev_hostdata(sensor);
788 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
789 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
793 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
797 * Some FIMC variants are not fitted with camera capture
798 * interface. Skip creating a link from sensor for those.
800 if (!fmd->fimc[i]->variant->has_cam_if)
803 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
805 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
806 ret = media_create_pad_link(source, pad, sink,
807 FIMC_SD_PAD_SINK_CAM, flags);
811 /* Notify FIMC capture subdev entity */
812 ret = media_entity_call(sink, link_setup, &sink->pads[0],
813 &source->pads[pad], flags);
817 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
818 source->name, flags ? '=' : '-', sink->name);
821 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
822 if (!fmd->fimc_lite[i])
825 sink = &fmd->fimc_lite[i]->subdev.entity;
826 ret = media_create_pad_link(source, pad, sink,
827 FLITE_SD_PAD_SINK, 0);
831 /* Notify FIMC-LITE subdev entity */
832 ret = media_entity_call(sink, link_setup, &sink->pads[0],
833 &source->pads[pad], 0);
837 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
838 source->name, sink->name);
843 /* Create links from FIMC-LITE source pads to other entities */
844 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
846 struct media_entity *source, *sink;
849 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
850 struct fimc_lite *fimc = fmd->fimc_lite[i];
855 source = &fimc->subdev.entity;
856 sink = &fimc->ve.vdev.entity;
857 /* FIMC-LITE's subdev and video node */
858 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_DMA,
862 /* Link from FIMC-LITE to IS-ISP subdev */
863 sink = &fmd->fimc_is->isp.subdev.entity;
864 ret = media_create_pad_link(source, FLITE_SD_PAD_SOURCE_ISP,
873 /* Create FIMC-IS links */
874 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
876 struct fimc_isp *isp = &fmd->fimc_is->isp;
877 struct media_entity *source, *sink;
880 source = &isp->subdev.entity;
882 for (i = 0; i < FIMC_MAX_DEVS; i++) {
883 if (fmd->fimc[i] == NULL)
886 /* Link from FIMC-IS-ISP subdev to FIMC */
887 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
888 ret = media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
889 sink, FIMC_SD_PAD_SINK_FIFO, 0);
894 /* Link from FIMC-IS-ISP subdev to fimc-is-isp.capture video node */
895 sink = &isp->video_capture.ve.vdev.entity;
897 /* Skip this link if the fimc-is-isp video node driver isn't built-in */
898 if (sink->num_pads == 0)
901 return media_create_pad_link(source, FIMC_ISP_SD_PAD_SRC_DMA,
906 * fimc_md_create_links - create default links between registered entities
908 * Parallel interface sensor entities are connected directly to FIMC capture
909 * entities. The sensors using MIPI CSIS bus are connected through immutable
910 * link with CSI receiver entity specified by mux_id. Any registered CSIS
911 * entity has a link to each registered FIMC capture entity. Enabled links
912 * are created by default between each subsequent registered sensor and
913 * subsequent FIMC capture entity. The number of default active links is
914 * determined by the number of available sensors or FIMC entities,
917 static int fimc_md_create_links(struct fimc_md *fmd)
919 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
920 struct v4l2_subdev *sensor, *csis;
921 struct fimc_source_info *pdata;
922 struct media_entity *source, *sink;
923 int i, pad, fimc_id = 0, ret = 0;
924 u32 flags, link_mask = 0;
926 for (i = 0; i < fmd->num_sensors; i++) {
927 if (fmd->sensor[i].subdev == NULL)
930 sensor = fmd->sensor[i].subdev;
931 pdata = v4l2_get_subdev_hostdata(sensor);
937 switch (pdata->sensor_bus_type) {
938 case FIMC_BUS_TYPE_MIPI_CSI2:
939 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
940 "Wrong CSI channel id: %d\n", pdata->mux_id))
943 csis = fmd->csis[pdata->mux_id].sd;
944 if (WARN(csis == NULL,
945 "MIPI-CSI interface specified "
946 "but s5p-csis module is not loaded!\n"))
949 pad = sensor->entity.num_pads - 1;
950 ret = media_create_pad_link(&sensor->entity, pad,
951 &csis->entity, CSIS_PAD_SINK,
952 MEDIA_LNK_FL_IMMUTABLE |
953 MEDIA_LNK_FL_ENABLED);
957 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
958 sensor->entity.name, csis->entity.name);
961 csi_sensors[pdata->mux_id] = sensor;
964 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
965 source = &sensor->entity;
970 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
971 pdata->sensor_bus_type);
977 link_mask = 1 << fimc_id++;
978 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
982 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
983 if (fmd->csis[i].sd == NULL)
986 source = &fmd->csis[i].sd->entity;
987 pad = CSIS_PAD_SOURCE;
988 sensor = csi_sensors[i];
990 link_mask = 1 << fimc_id++;
991 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
995 /* Create immutable links between each FIMC's subdev and video node */
996 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
997 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1001 source = &fmd->fimc[i]->vid_cap.subdev.entity;
1002 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1004 ret = media_create_pad_link(source, FIMC_SD_PAD_SOURCE,
1010 ret = __fimc_md_create_flite_source_links(fmd);
1015 ret = __fimc_md_create_fimc_is_links(fmd);
1021 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1023 static void fimc_md_put_clocks(struct fimc_md *fmd)
1025 int i = FIMC_MAX_CAMCLKS;
1028 if (IS_ERR(fmd->camclk[i].clock))
1030 clk_put(fmd->camclk[i].clock);
1031 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1034 /* Writeback (PIXELASYNCMx) clocks */
1035 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1036 if (IS_ERR(fmd->wbclk[i]))
1038 clk_put(fmd->wbclk[i]);
1039 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1043 static int fimc_md_get_clocks(struct fimc_md *fmd)
1045 struct device *dev = &fmd->pdev->dev;
1050 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1051 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1053 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1054 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1055 clock = clk_get(dev, clk_name);
1057 if (IS_ERR(clock)) {
1058 dev_err(dev, "Failed to get clock: %s\n", clk_name);
1059 ret = PTR_ERR(clock);
1062 fmd->camclk[i].clock = clock;
1065 fimc_md_put_clocks(fmd);
1070 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1071 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1073 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1075 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1076 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1077 clock = clk_get(dev, clk_name);
1078 if (IS_ERR(clock)) {
1079 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1081 ret = PTR_ERR(clock);
1084 fmd->wbclk[i] = clock;
1087 fimc_md_put_clocks(fmd);
1092 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1094 struct exynos_video_entity *ve;
1095 struct fimc_pipeline *p;
1096 struct video_device *vdev;
1099 vdev = media_entity_to_video_device(entity);
1100 if (vdev->entity.use_count == 0)
1103 ve = vdev_to_exynos_video_entity(vdev);
1104 p = to_fimc_pipeline(ve->pipe);
1106 * Nothing to do if we are disabling the pipeline, some link
1107 * has been disconnected and p->subdevs array is cleared now.
1109 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1113 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1115 ret = __fimc_pipeline_close(ve->pipe);
1117 if (ret == 0 && !enable)
1118 memset(p->subdevs, 0, sizeof(p->subdevs));
1123 /* Locking: called with entity->graph_obj.mdev->graph_mutex mutex held. */
1124 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable,
1125 struct media_entity_graph *graph)
1127 struct media_entity *entity_err = entity;
1131 * Walk current graph and call the pipeline open/close routine for each
1132 * opened video node that belongs to the graph of entities connected
1133 * through active links. This is needed as we cannot power on/off the
1134 * subdevs in random order.
1136 media_entity_graph_walk_start(graph, entity);
1138 while ((entity = media_entity_graph_walk_next(graph))) {
1139 if (!is_media_entity_v4l2_video_device(entity))
1142 ret = __fimc_md_modify_pipeline(entity, enable);
1151 media_entity_graph_walk_start(graph, entity_err);
1153 while ((entity_err = media_entity_graph_walk_next(graph))) {
1154 if (!is_media_entity_v4l2_video_device(entity_err))
1157 __fimc_md_modify_pipeline(entity_err, !enable);
1159 if (entity_err == entity)
1166 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1167 unsigned int notification)
1169 struct media_entity_graph *graph =
1170 &container_of(link->graph_obj.mdev, struct fimc_md,
1171 media_dev)->link_setup_graph;
1172 struct media_entity *sink = link->sink->entity;
1175 /* Before link disconnection */
1176 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1177 ret = media_entity_graph_walk_init(graph,
1178 link->graph_obj.mdev);
1181 if (!(flags & MEDIA_LNK_FL_ENABLED))
1182 ret = __fimc_md_modify_pipelines(sink, false, graph);
1185 /* TODO: Link state change validation */
1187 /* After link activation */
1188 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH) {
1189 if (link->flags & MEDIA_LNK_FL_ENABLED)
1190 ret = __fimc_md_modify_pipelines(sink, true, graph);
1191 media_entity_graph_walk_cleanup(graph);
1194 return ret ? -EPIPE : 0;
1197 static const struct media_device_ops fimc_md_ops = {
1198 .link_notify = fimc_md_link_notify,
1201 static ssize_t fimc_md_sysfs_show(struct device *dev,
1202 struct device_attribute *attr, char *buf)
1204 struct platform_device *pdev = to_platform_device(dev);
1205 struct fimc_md *fmd = platform_get_drvdata(pdev);
1207 if (fmd->user_subdev_api)
1208 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1210 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1213 static ssize_t fimc_md_sysfs_store(struct device *dev,
1214 struct device_attribute *attr,
1215 const char *buf, size_t count)
1217 struct platform_device *pdev = to_platform_device(dev);
1218 struct fimc_md *fmd = platform_get_drvdata(pdev);
1222 if (!strcmp(buf, "vid-dev\n"))
1224 else if (!strcmp(buf, "sub-dev\n"))
1229 fmd->user_subdev_api = subdev_api;
1230 for (i = 0; i < FIMC_MAX_DEVS; i++)
1232 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
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 /* PINCTRL_STATE_IDLE is optional */
1261 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1262 PINCTRL_STATE_IDLE);
1266 static int cam_clk_prepare(struct clk_hw *hw)
1268 struct cam_clk *camclk = to_cam_clk(hw);
1271 if (camclk->fmd->pmf == NULL)
1274 ret = pm_runtime_get_sync(camclk->fmd->pmf);
1275 return ret < 0 ? ret : 0;
1278 static void cam_clk_unprepare(struct clk_hw *hw)
1280 struct cam_clk *camclk = to_cam_clk(hw);
1282 if (camclk->fmd->pmf == NULL)
1285 pm_runtime_put_sync(camclk->fmd->pmf);
1288 static const struct clk_ops cam_clk_ops = {
1289 .prepare = cam_clk_prepare,
1290 .unprepare = cam_clk_unprepare,
1293 static void fimc_md_unregister_clk_provider(struct fimc_md *fmd)
1295 struct cam_clk_provider *cp = &fmd->clk_provider;
1299 of_clk_del_provider(cp->of_node);
1301 for (i = 0; i < cp->num_clocks; i++)
1302 clk_unregister(cp->clks[i]);
1305 static int fimc_md_register_clk_provider(struct fimc_md *fmd)
1307 struct cam_clk_provider *cp = &fmd->clk_provider;
1308 struct device *dev = &fmd->pdev->dev;
1311 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1312 struct cam_clk *camclk = &cp->camclk[i];
1313 struct clk_init_data init;
1316 ret = of_property_read_string_index(dev->of_node,
1317 "clock-output-names", i, &init.name);
1321 p_name = __clk_get_name(fmd->camclk[i].clock);
1323 /* It's safe since clk_register() will duplicate the string. */
1324 init.parent_names = &p_name;
1325 init.num_parents = 1;
1326 init.ops = &cam_clk_ops;
1327 init.flags = CLK_SET_RATE_PARENT;
1328 camclk->hw.init = &init;
1331 cp->clks[i] = clk_register(NULL, &camclk->hw);
1332 if (IS_ERR(cp->clks[i])) {
1333 dev_err(dev, "failed to register clock: %s (%ld)\n",
1334 init.name, PTR_ERR(cp->clks[i]));
1335 ret = PTR_ERR(cp->clks[i]);
1341 if (cp->num_clocks == 0) {
1342 dev_warn(dev, "clk provider not registered\n");
1346 cp->clk_data.clks = cp->clks;
1347 cp->clk_data.clk_num = cp->num_clocks;
1348 cp->of_node = dev->of_node;
1349 ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1354 fimc_md_unregister_clk_provider(fmd);
1358 static int subdev_notifier_bound(struct v4l2_async_notifier *notifier,
1359 struct v4l2_subdev *subdev,
1360 struct v4l2_async_subdev *asd)
1362 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1363 struct fimc_sensor_info *si = NULL;
1366 /* Find platform data for this sensor subdev */
1367 for (i = 0; i < ARRAY_SIZE(fmd->sensor); i++)
1368 if (fmd->sensor[i].asd.match.of.node == subdev->dev->of_node)
1369 si = &fmd->sensor[i];
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;
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);
1391 static int subdev_notifier_complete(struct v4l2_async_notifier *notifier)
1393 struct fimc_md *fmd = notifier_to_fimc_md(notifier);
1396 mutex_lock(&fmd->media_dev.graph_mutex);
1398 ret = fimc_md_create_links(fmd);
1402 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1404 mutex_unlock(&fmd->media_dev.graph_mutex);
1408 return media_device_register(&fmd->media_dev);
1411 static int fimc_md_probe(struct platform_device *pdev)
1413 struct device *dev = &pdev->dev;
1414 struct v4l2_device *v4l2_dev;
1415 struct fimc_md *fmd;
1418 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1422 spin_lock_init(&fmd->slock);
1423 INIT_LIST_HEAD(&fmd->pipelines);
1426 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1427 sizeof(fmd->media_dev.model));
1428 fmd->media_dev.ops = &fimc_md_ops;
1429 fmd->media_dev.dev = dev;
1431 v4l2_dev = &fmd->v4l2_dev;
1432 v4l2_dev->mdev = &fmd->media_dev;
1433 v4l2_dev->notify = fimc_sensor_notify;
1434 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1436 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1437 fmd->user_subdev_api = true;
1439 media_device_init(&fmd->media_dev);
1441 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1443 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1447 ret = fimc_md_get_clocks(fmd);
1451 ret = fimc_md_get_pinctrl(fmd);
1453 if (ret != EPROBE_DEFER)
1454 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1458 platform_set_drvdata(pdev, fmd);
1460 ret = fimc_md_register_platform_entities(fmd, dev->of_node);
1464 ret = fimc_md_register_sensor_entities(fmd);
1468 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1472 * FIMC platform devices need to be registered before the sclk_cam
1473 * clocks provider, as one of these devices needs to be activated
1474 * to enable the clock.
1476 ret = fimc_md_register_clk_provider(fmd);
1478 v4l2_err(v4l2_dev, "clock provider registration failed\n");
1482 if (fmd->num_sensors > 0) {
1483 fmd->subdev_notifier.subdevs = fmd->async_subdevs;
1484 fmd->subdev_notifier.num_subdevs = fmd->num_sensors;
1485 fmd->subdev_notifier.bound = subdev_notifier_bound;
1486 fmd->subdev_notifier.complete = subdev_notifier_complete;
1487 fmd->num_sensors = 0;
1489 ret = v4l2_async_notifier_register(&fmd->v4l2_dev,
1490 &fmd->subdev_notifier);
1498 fimc_md_unregister_clk_provider(fmd);
1500 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1502 fimc_md_put_clocks(fmd);
1504 fimc_md_unregister_entities(fmd);
1506 media_device_cleanup(&fmd->media_dev);
1507 v4l2_device_unregister(&fmd->v4l2_dev);
1511 static int fimc_md_remove(struct platform_device *pdev)
1513 struct fimc_md *fmd = platform_get_drvdata(pdev);
1518 fimc_md_unregister_clk_provider(fmd);
1519 v4l2_async_notifier_unregister(&fmd->subdev_notifier);
1521 v4l2_device_unregister(&fmd->v4l2_dev);
1522 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1523 fimc_md_unregister_entities(fmd);
1524 fimc_md_pipelines_free(fmd);
1525 media_device_unregister(&fmd->media_dev);
1526 media_device_cleanup(&fmd->media_dev);
1527 fimc_md_put_clocks(fmd);
1532 static const struct platform_device_id fimc_driver_ids[] __always_unused = {
1533 { .name = "s5p-fimc-md" },
1536 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1538 static const struct of_device_id fimc_md_of_match[] = {
1539 { .compatible = "samsung,fimc" },
1542 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1544 static struct platform_driver fimc_md_driver = {
1545 .probe = fimc_md_probe,
1546 .remove = fimc_md_remove,
1548 .of_match_table = of_match_ptr(fimc_md_of_match),
1549 .name = "s5p-fimc-md",
1553 static int __init fimc_md_init(void)
1557 request_module("s5p-csis");
1558 ret = fimc_register_driver();
1562 return platform_driver_register(&fimc_md_driver);
1565 static void __exit fimc_md_exit(void)
1567 platform_driver_unregister(&fimc_md_driver);
1568 fimc_unregister_driver();
1571 module_init(fimc_md_init);
1572 module_exit(fimc_md_exit);
1574 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1575 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1576 MODULE_LICENSE("GPL");
1577 MODULE_VERSION("2.0.1");