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/device.h>
15 #include <linux/errno.h>
16 #include <linux/i2c.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
21 #include <linux/of_platform.h>
22 #include <linux/of_device.h>
23 #include <linux/of_i2c.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-of.h>
30 #include <media/media-device.h>
31 #include <media/s5p_fimc.h>
33 #include "media-dev.h"
34 #include "fimc-core.h"
36 #include "fimc-lite.h"
37 #include "mipi-csis.h"
39 static int __fimc_md_set_camclk(struct fimc_md *fmd,
40 struct fimc_source_info *si,
43 /* Set up image sensor subdev -> FIMC capture node notifications. */
44 static void __setup_sensor_notification(struct fimc_md *fmd,
45 struct v4l2_subdev *sensor,
46 struct v4l2_subdev *fimc_sd)
48 struct fimc_source_info *src_inf;
49 struct fimc_sensor_info *md_si;
52 src_inf = v4l2_get_subdev_hostdata(sensor);
53 if (!src_inf || WARN_ON(fmd == NULL))
56 md_si = source_to_sensor_info(src_inf);
57 spin_lock_irqsave(&fmd->slock, flags);
58 md_si->host = v4l2_get_subdevdata(fimc_sd);
59 spin_unlock_irqrestore(&fmd->slock, flags);
63 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
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;
76 for (i = 0; i < IDX_MAX; i++)
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))
87 pad = media_entity_remote_pad(spad);
93 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
95 sd = media_entity_to_v4l2_subdev(pad->entity);
101 case GRP_ID_FIMC_IS_SENSOR:
102 p->subdevs[IDX_SENSOR] = sd;
105 p->subdevs[IDX_CSIS] = sd;
108 p->subdevs[IDX_FLITE] = sd;
111 p->subdevs[IDX_FIMC] = sd;
114 p->subdevs[IDX_IS_ISP] = sd;
120 if (me->num_pads == 1)
124 if (sensor && p->subdevs[IDX_FIMC])
125 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
129 * __subdev_set_power - change power state of a single subdev
130 * @sd: subdevice to change power state for
131 * @on: 1 to enable power or 0 to disable
133 * Return result of s_power subdev operation or -ENXIO if sd argument
134 * is NULL. Return 0 if the subdevice does not implement s_power.
136 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
144 use_count = &sd->entity.use_count;
145 if (on && (*use_count)++ > 0)
147 else if (!on && (*use_count == 0 || --(*use_count) > 0))
149 ret = v4l2_subdev_call(sd, core, s_power, on);
151 return ret != -ENOIOCTLCMD ? ret : 0;
155 * fimc_pipeline_s_power - change power state of all pipeline subdevs
156 * @fimc: fimc device terminating the pipeline
157 * @state: true to power on, false to power off
159 * Needs to be called with the graph mutex held.
161 static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
163 static const u8 seq[2][IDX_MAX - 1] = {
164 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
165 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
169 if (p->subdevs[IDX_SENSOR] == NULL)
172 for (i = 0; i < IDX_MAX - 1; i++) {
173 unsigned int idx = seq[on][i];
175 ret = __subdev_set_power(p->subdevs[idx], on);
178 if (ret < 0 && ret != -ENXIO)
183 for (; i >= 0; i--) {
184 unsigned int idx = seq[on][i];
185 __subdev_set_power(p->subdevs[idx], !on);
191 * __fimc_pipeline_open - update the pipeline information, enable power
192 * of all pipeline subdevs and the sensor clock
193 * @me: media entity to start graph walk with
194 * @prepare: true to walk the current pipeline and acquire all subdevs
196 * Called with the graph mutex held.
198 static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
199 struct media_entity *me, bool prepare)
201 struct fimc_md *fmd = entity_to_fimc_mdev(me);
202 struct fimc_pipeline *p = to_fimc_pipeline(ep);
203 struct v4l2_subdev *sd;
206 if (WARN_ON(p == NULL || me == NULL))
210 fimc_pipeline_prepare(p, me);
212 sd = p->subdevs[IDX_SENSOR];
216 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
217 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
218 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
222 ret = fimc_md_set_camclk(sd, true);
226 ret = fimc_pipeline_s_power(p, 1);
230 fimc_md_set_camclk(sd, false);
233 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
234 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
240 * __fimc_pipeline_close - disable the sensor clock and pipeline power
241 * @fimc: fimc device terminating the pipeline
243 * Disable power of all subdevs and turn the external sensor clock off.
245 static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
247 struct fimc_pipeline *p = to_fimc_pipeline(ep);
248 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
253 pr_warn("%s(): No sensor subdev\n", __func__);
257 ret = fimc_pipeline_s_power(p, 0);
258 fimc_md_set_camclk(sd, false);
260 fmd = entity_to_fimc_mdev(&sd->entity);
262 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
263 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
264 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
266 return ret == -ENXIO ? 0 : ret;
270 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
271 * @pipeline: video pipeline structure
272 * @on: passed as the s_stream() callback argument
274 static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
276 static const u8 seq[2][IDX_MAX] = {
277 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
278 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
280 struct fimc_pipeline *p = to_fimc_pipeline(ep);
283 if (p->subdevs[IDX_SENSOR] == NULL)
286 for (i = 0; i < IDX_MAX; i++) {
287 unsigned int idx = seq[on][i];
289 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
291 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
296 for (; i >= 0; i--) {
297 unsigned int idx = seq[on][i];
298 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
303 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
304 static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
305 .open = __fimc_pipeline_open,
306 .close = __fimc_pipeline_close,
307 .set_stream = __fimc_pipeline_s_stream,
310 static struct exynos_media_pipeline *fimc_md_pipeline_create(
313 struct fimc_pipeline *p;
315 p = kzalloc(sizeof(*p), GFP_KERNEL);
319 list_add_tail(&p->list, &fmd->pipelines);
321 p->ep.ops = &fimc_pipeline_ops;
325 static void fimc_md_pipelines_free(struct fimc_md *fmd)
327 while (!list_empty(&fmd->pipelines)) {
328 struct fimc_pipeline *p;
330 p = list_entry(fmd->pipelines.next, typeof(*p), list);
337 * Sensor subdevice helper functions
339 static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
340 struct fimc_source_info *si)
342 struct i2c_adapter *adapter;
343 struct v4l2_subdev *sd = NULL;
348 * If FIMC bus type is not Writeback FIFO assume it is same
349 * as sensor_bus_type.
351 si->fimc_bus_type = si->sensor_bus_type;
353 adapter = i2c_get_adapter(si->i2c_bus_num);
355 v4l2_warn(&fmd->v4l2_dev,
356 "Failed to get I2C adapter %d, deferring probe\n",
358 return ERR_PTR(-EPROBE_DEFER);
360 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
361 si->board_info, NULL);
362 if (IS_ERR_OR_NULL(sd)) {
363 i2c_put_adapter(adapter);
364 v4l2_warn(&fmd->v4l2_dev,
365 "Failed to acquire subdev %s, deferring probe\n",
366 si->board_info->type);
367 return ERR_PTR(-EPROBE_DEFER);
369 v4l2_set_subdev_hostdata(sd, si);
370 sd->grp_id = GRP_ID_SENSOR;
372 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
377 static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
379 struct i2c_client *client = v4l2_get_subdevdata(sd);
380 struct i2c_adapter *adapter;
385 v4l2_device_unregister_subdev(sd);
387 if (!client->dev.of_node) {
388 adapter = client->adapter;
389 i2c_unregister_device(client);
391 i2c_put_adapter(adapter);
396 /* Register I2C client subdev associated with @node. */
397 static int fimc_md_of_add_sensor(struct fimc_md *fmd,
398 struct device_node *node, int index)
400 struct fimc_sensor_info *si;
401 struct i2c_client *client;
402 struct v4l2_subdev *sd;
405 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
407 si = &fmd->sensor[index];
409 client = of_find_i2c_device_by_node(node);
411 return -EPROBE_DEFER;
413 device_lock(&client->dev);
415 if (!client->driver ||
416 !try_module_get(client->driver->driver.owner)) {
418 v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n",
423 /* Enable sensor's master clock */
424 ret = __fimc_md_set_camclk(fmd, &si->pdata, true);
427 sd = i2c_get_clientdata(client);
429 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
430 __fimc_md_set_camclk(fmd, &si->pdata, false);
434 v4l2_set_subdev_hostdata(sd, &si->pdata);
435 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
436 sd->grp_id = GRP_ID_FIMC_IS_SENSOR;
438 sd->grp_id = GRP_ID_SENSOR;
441 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
442 sd->name, fmd->num_sensors);
446 module_put(client->driver->driver.owner);
448 device_unlock(&client->dev);
449 put_device(&client->dev);
453 /* Parse port node and register as a sub-device any sensor specified there. */
454 static int fimc_md_parse_port_node(struct fimc_md *fmd,
455 struct device_node *port,
458 struct device_node *rem, *ep, *np;
459 struct fimc_source_info *pd;
460 struct v4l2_of_endpoint endpoint;
464 pd = &fmd->sensor[index].pdata;
466 /* Assume here a port node can have only one endpoint node. */
467 ep = of_get_next_child(port, NULL);
471 v4l2_of_parse_endpoint(ep, &endpoint);
472 if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS)
475 pd->mux_id = (endpoint.port - 1) & 0x1;
477 rem = v4l2_of_get_remote_port_parent(ep);
480 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
484 if (!of_property_read_u32(rem, "samsung,camclk-out", &val))
487 if (!of_property_read_u32(rem, "clock-frequency", &val))
488 pd->clk_frequency = val;
490 if (pd->clk_frequency == 0) {
491 v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n",
497 if (fimc_input_is_parallel(endpoint.port)) {
498 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
499 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
501 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
502 pd->flags = endpoint.bus.parallel.flags;
503 } else if (fimc_input_is_mipi_csi(endpoint.port)) {
505 * MIPI CSI-2: only input mux selection and
506 * the sensor's clock frequency is needed.
508 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
510 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
511 endpoint.port, rem->full_name);
514 * For FIMC-IS handled sensors, that are placed under i2c-isp device
515 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
516 * input. Sensors are attached to the FIMC-LITE hostdata interface
517 * directly or through MIPI-CSIS, depending on the external media bus
518 * used. This needs to be handled in a more reliable way, not by just
519 * checking parent's node name.
521 np = of_get_parent(rem);
523 if (np && !of_node_cmp(np->name, "i2c-isp"))
524 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
526 pd->fimc_bus_type = pd->sensor_bus_type;
528 ret = fimc_md_of_add_sensor(fmd, rem, index);
534 /* Register all SoC external sub-devices */
535 static int fimc_md_of_sensors_register(struct fimc_md *fmd,
536 struct device_node *np)
538 struct device_node *parent = fmd->pdev->dev.of_node;
539 struct device_node *node, *ports;
543 /* Attach sensors linked to MIPI CSI-2 receivers */
544 for_each_available_child_of_node(parent, node) {
545 struct device_node *port;
547 if (of_node_cmp(node->name, "csis"))
549 /* The csis node can have only port subnode. */
550 port = of_get_next_child(node, NULL);
554 ret = fimc_md_parse_port_node(fmd, port, index);
560 /* Attach sensors listed in the parallel-ports node */
561 ports = of_get_child_by_name(parent, "parallel-ports");
565 for_each_child_of_node(ports, node) {
566 ret = fimc_md_parse_port_node(fmd, node, index);
575 static int __of_get_csis_id(struct device_node *np)
579 np = of_get_child_by_name(np, "port");
582 of_property_read_u32(np, "reg", ®);
583 return reg - FIMC_INPUT_MIPI_CSI2_0;
586 #define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
587 #define __of_get_csis_id(np) (-ENOSYS)
590 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
592 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
593 struct device_node *of_node = fmd->pdev->dev.of_node;
598 * Runtime resume one of the FIMC entities to make sure
599 * the sclk_cam clocks are not globally disabled.
604 ret = pm_runtime_get_sync(fmd->pmf);
609 fmd->num_sensors = 0;
610 ret = fimc_md_of_sensors_register(fmd, of_node);
612 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
613 num_clients = min_t(u32, pdata->num_clients,
614 ARRAY_SIZE(fmd->sensor));
615 fmd->num_sensors = num_clients;
617 for (i = 0; i < num_clients; i++) {
618 struct fimc_sensor_info *si = &fmd->sensor[i];
619 struct v4l2_subdev *sd;
621 si->pdata = pdata->source_info[i];
622 ret = __fimc_md_set_camclk(fmd, &si->pdata, true);
625 sd = fimc_md_register_sensor(fmd, &si->pdata);
626 ret = __fimc_md_set_camclk(fmd, &si->pdata, false);
639 pm_runtime_put(fmd->pmf);
644 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
647 static int register_fimc_lite_entity(struct fimc_md *fmd,
648 struct fimc_lite *fimc_lite)
650 struct v4l2_subdev *sd;
651 struct exynos_media_pipeline *ep;
654 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
655 fmd->fimc_lite[fimc_lite->index]))
658 sd = &fimc_lite->subdev;
659 sd->grp_id = GRP_ID_FLITE;
661 ep = fimc_md_pipeline_create(fmd);
665 v4l2_set_subdev_hostdata(sd, ep);
667 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
669 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
671 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
676 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
678 struct v4l2_subdev *sd;
679 struct exynos_media_pipeline *ep;
682 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
685 sd = &fimc->vid_cap.subdev;
686 sd->grp_id = GRP_ID_FIMC;
688 ep = fimc_md_pipeline_create(fmd);
692 v4l2_set_subdev_hostdata(sd, ep);
694 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
696 if (!fmd->pmf && fimc->pdev)
697 fmd->pmf = &fimc->pdev->dev;
698 fmd->fimc[fimc->id] = fimc;
699 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
701 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
707 static int register_csis_entity(struct fimc_md *fmd,
708 struct platform_device *pdev,
709 struct v4l2_subdev *sd)
711 struct device_node *node = pdev->dev.of_node;
714 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
716 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
719 if (WARN_ON(fmd->csis[id].sd))
722 sd->grp_id = GRP_ID_CSIS;
723 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
725 fmd->csis[id].sd = sd;
727 v4l2_err(&fmd->v4l2_dev,
728 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
732 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
734 struct v4l2_subdev *sd = &is->isp.subdev;
737 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
739 v4l2_err(&fmd->v4l2_dev,
740 "Failed to register FIMC-ISP (%d)\n", ret);
748 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
749 struct platform_device *pdev,
752 struct device *dev = &pdev->dev;
753 int ret = -EPROBE_DEFER;
756 /* Lock to ensure dev->driver won't change. */
759 if (!dev->driver || !try_module_get(dev->driver->owner))
762 drvdata = dev_get_drvdata(dev);
763 /* Some subdev didn't probe succesfully id drvdata is NULL */
765 switch (plat_entity) {
767 ret = register_fimc_entity(fmd, drvdata);
770 ret = register_fimc_lite_entity(fmd, drvdata);
773 ret = register_csis_entity(fmd, pdev, drvdata);
776 ret = register_fimc_is_entity(fmd, drvdata);
783 module_put(dev->driver->owner);
786 if (ret == -EPROBE_DEFER)
787 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
790 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
795 static int fimc_md_pdev_match(struct device *dev, void *data)
797 struct platform_device *pdev = to_platform_device(dev);
798 int plat_entity = -1;
802 if (!get_device(dev))
805 if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) {
806 plat_entity = IDX_CSIS;
808 p = strstr(pdev->name, "fimc");
809 if (p && *(p + 4) == 0)
810 plat_entity = IDX_FIMC;
813 if (plat_entity >= 0)
814 ret = fimc_md_register_platform_entity(data, pdev,
820 /* Register FIMC, FIMC-LITE and CSIS media entities */
822 static int fimc_md_register_of_platform_entities(struct fimc_md *fmd,
823 struct device_node *parent)
825 struct device_node *node;
828 for_each_available_child_of_node(parent, node) {
829 struct platform_device *pdev;
830 int plat_entity = -1;
832 pdev = of_find_device_by_node(node);
836 /* If driver of any entity isn't ready try all again later. */
837 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
838 plat_entity = IDX_CSIS;
839 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
840 plat_entity = IDX_IS_ISP;
841 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
842 plat_entity = IDX_FLITE;
843 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
844 !of_property_read_bool(node, "samsung,lcd-wb"))
845 plat_entity = IDX_FIMC;
847 if (plat_entity >= 0)
848 ret = fimc_md_register_platform_entity(fmd, pdev,
850 put_device(&pdev->dev);
858 #define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
861 static void fimc_md_unregister_entities(struct fimc_md *fmd)
865 for (i = 0; i < FIMC_MAX_DEVS; i++) {
866 struct fimc_dev *dev = fmd->fimc[i];
869 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
870 dev->vid_cap.ve.pipe = NULL;
873 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
874 struct fimc_lite *dev = fmd->fimc_lite[i];
877 v4l2_device_unregister_subdev(&dev->subdev);
879 fmd->fimc_lite[i] = NULL;
881 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
882 if (fmd->csis[i].sd == NULL)
884 v4l2_device_unregister_subdev(fmd->csis[i].sd);
885 fmd->csis[i].sd = NULL;
887 for (i = 0; i < fmd->num_sensors; i++) {
888 if (fmd->sensor[i].subdev == NULL)
890 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
891 fmd->sensor[i].subdev = NULL;
895 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
897 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
901 * __fimc_md_create_fimc_links - create links to all FIMC entities
902 * @fmd: fimc media device
903 * @source: the source entity to create links to all fimc entities from
904 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
905 * @pad: the source entity pad index
906 * @link_mask: bitmask of the fimc devices for which link should be enabled
908 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
909 struct media_entity *source,
910 struct v4l2_subdev *sensor,
911 int pad, int link_mask)
913 struct fimc_source_info *si = NULL;
914 struct media_entity *sink;
915 unsigned int flags = 0;
919 si = v4l2_get_subdev_hostdata(sensor);
920 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
921 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
925 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
929 * Some FIMC variants are not fitted with camera capture
930 * interface. Skip creating a link from sensor for those.
932 if (!fmd->fimc[i]->variant->has_cam_if)
935 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
937 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
938 ret = media_entity_create_link(source, pad, sink,
939 FIMC_SD_PAD_SINK_CAM, flags);
943 /* Notify FIMC capture subdev entity */
944 ret = media_entity_call(sink, link_setup, &sink->pads[0],
945 &source->pads[pad], flags);
949 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
950 source->name, flags ? '=' : '-', sink->name);
953 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
954 if (!fmd->fimc_lite[i])
957 sink = &fmd->fimc_lite[i]->subdev.entity;
958 ret = media_entity_create_link(source, pad, sink,
959 FLITE_SD_PAD_SINK, 0);
963 /* Notify FIMC-LITE subdev entity */
964 ret = media_entity_call(sink, link_setup, &sink->pads[0],
965 &source->pads[pad], 0);
969 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
970 source->name, sink->name);
975 /* Create links from FIMC-LITE source pads to other entities */
976 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
978 struct media_entity *source, *sink;
981 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
982 struct fimc_lite *fimc = fmd->fimc_lite[i];
987 source = &fimc->subdev.entity;
988 sink = &fimc->ve.vdev.entity;
989 /* FIMC-LITE's subdev and video node */
990 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
994 /* Link from FIMC-LITE to IS-ISP subdev */
995 sink = &fmd->fimc_is->isp.subdev.entity;
996 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_ISP,
1005 /* Create FIMC-IS links */
1006 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
1008 struct media_entity *source, *sink;
1011 source = &fmd->fimc_is->isp.subdev.entity;
1013 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1014 if (fmd->fimc[i] == NULL)
1017 /* Link from IS-ISP subdev to FIMC */
1018 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
1019 ret = media_entity_create_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
1020 sink, FIMC_SD_PAD_SINK_FIFO, 0);
1029 * fimc_md_create_links - create default links between registered entities
1031 * Parallel interface sensor entities are connected directly to FIMC capture
1032 * entities. The sensors using MIPI CSIS bus are connected through immutable
1033 * link with CSI receiver entity specified by mux_id. Any registered CSIS
1034 * entity has a link to each registered FIMC capture entity. Enabled links
1035 * are created by default between each subsequent registered sensor and
1036 * subsequent FIMC capture entity. The number of default active links is
1037 * determined by the number of available sensors or FIMC entities,
1038 * whichever is less.
1040 static int fimc_md_create_links(struct fimc_md *fmd)
1042 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
1043 struct v4l2_subdev *sensor, *csis;
1044 struct fimc_source_info *pdata;
1045 struct media_entity *source, *sink;
1046 int i, pad, fimc_id = 0, ret = 0;
1047 u32 flags, link_mask = 0;
1049 for (i = 0; i < fmd->num_sensors; i++) {
1050 if (fmd->sensor[i].subdev == NULL)
1053 sensor = fmd->sensor[i].subdev;
1054 pdata = v4l2_get_subdev_hostdata(sensor);
1060 switch (pdata->sensor_bus_type) {
1061 case FIMC_BUS_TYPE_MIPI_CSI2:
1062 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
1063 "Wrong CSI channel id: %d\n", pdata->mux_id))
1066 csis = fmd->csis[pdata->mux_id].sd;
1067 if (WARN(csis == NULL,
1068 "MIPI-CSI interface specified "
1069 "but s5p-csis module is not loaded!\n"))
1072 pad = sensor->entity.num_pads - 1;
1073 ret = media_entity_create_link(&sensor->entity, pad,
1074 &csis->entity, CSIS_PAD_SINK,
1075 MEDIA_LNK_FL_IMMUTABLE |
1076 MEDIA_LNK_FL_ENABLED);
1080 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
1081 sensor->entity.name, csis->entity.name);
1084 csi_sensors[pdata->mux_id] = sensor;
1087 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
1088 source = &sensor->entity;
1093 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
1094 pdata->sensor_bus_type);
1100 link_mask = 1 << fimc_id++;
1101 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1105 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
1106 if (fmd->csis[i].sd == NULL)
1109 source = &fmd->csis[i].sd->entity;
1110 pad = CSIS_PAD_SOURCE;
1111 sensor = csi_sensors[i];
1113 link_mask = 1 << fimc_id++;
1114 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1118 /* Create immutable links between each FIMC's subdev and video node */
1119 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1120 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1124 source = &fmd->fimc[i]->vid_cap.subdev.entity;
1125 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1127 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
1133 ret = __fimc_md_create_flite_source_links(fmd);
1138 ret = __fimc_md_create_fimc_is_links(fmd);
1144 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1146 static void fimc_md_put_clocks(struct fimc_md *fmd)
1148 int i = FIMC_MAX_CAMCLKS;
1151 if (IS_ERR(fmd->camclk[i].clock))
1153 clk_put(fmd->camclk[i].clock);
1154 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1157 /* Writeback (PIXELASYNCMx) clocks */
1158 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1159 if (IS_ERR(fmd->wbclk[i]))
1161 clk_put(fmd->wbclk[i]);
1162 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1166 static int fimc_md_get_clocks(struct fimc_md *fmd)
1168 struct device *dev = NULL;
1173 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1174 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1176 if (fmd->pdev->dev.of_node)
1177 dev = &fmd->pdev->dev;
1179 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1180 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1181 clock = clk_get(dev, clk_name);
1183 if (IS_ERR(clock)) {
1184 dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n",
1186 ret = PTR_ERR(clock);
1189 fmd->camclk[i].clock = clock;
1192 fimc_md_put_clocks(fmd);
1197 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1198 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1200 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1202 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1203 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1204 clock = clk_get(dev, clk_name);
1205 if (IS_ERR(clock)) {
1206 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1208 ret = PTR_ERR(clock);
1211 fmd->wbclk[i] = clock;
1214 fimc_md_put_clocks(fmd);
1219 static int __fimc_md_set_camclk(struct fimc_md *fmd,
1220 struct fimc_source_info *si,
1223 struct fimc_camclk_info *camclk;
1226 if (WARN_ON(si->clk_id >= FIMC_MAX_CAMCLKS) || !fmd || !fmd->pmf)
1229 camclk = &fmd->camclk[si->clk_id];
1231 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
1232 si->clk_id, si->clk_frequency, camclk->use_count, on);
1235 if (camclk->use_count > 0 &&
1236 camclk->frequency != si->clk_frequency)
1239 if (camclk->use_count++ == 0) {
1240 clk_set_rate(camclk->clock, si->clk_frequency);
1241 camclk->frequency = si->clk_frequency;
1242 ret = pm_runtime_get_sync(fmd->pmf);
1245 ret = clk_prepare_enable(camclk->clock);
1246 dbg("Enabled camclk %d: f: %lu", si->clk_id,
1247 clk_get_rate(camclk->clock));
1252 if (WARN_ON(camclk->use_count == 0))
1255 if (--camclk->use_count == 0) {
1256 clk_disable_unprepare(camclk->clock);
1257 pm_runtime_put(fmd->pmf);
1258 dbg("Disabled camclk %d", si->clk_id);
1264 * fimc_md_set_camclk - peripheral sensor clock setup
1265 * @sd: sensor subdev to configure sclk_cam clock for
1266 * @on: 1 to enable or 0 to disable the clock
1268 * There are 2 separate clock outputs available in the SoC for external
1269 * image processors. These clocks are shared between all registered FIMC
1270 * devices to which sensors can be attached, either directly or through
1271 * the MIPI CSI receiver. The clock is allowed here to be used by
1272 * multiple sensors concurrently if they use same frequency.
1273 * This function should only be called when the graph mutex is held.
1275 int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
1277 struct fimc_source_info *si = v4l2_get_subdev_hostdata(sd);
1278 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
1280 return __fimc_md_set_camclk(fmd, si, on);
1283 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1285 struct exynos_video_entity *ve;
1286 struct fimc_pipeline *p;
1287 struct video_device *vdev;
1290 vdev = media_entity_to_video_device(entity);
1291 if (vdev->entity.use_count == 0)
1294 ve = vdev_to_exynos_video_entity(vdev);
1295 p = to_fimc_pipeline(ve->pipe);
1297 * Nothing to do if we are disabling the pipeline, some link
1298 * has been disconnected and p->subdevs array is cleared now.
1300 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1304 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1306 ret = __fimc_pipeline_close(ve->pipe);
1308 if (ret == 0 && !enable)
1309 memset(p->subdevs, 0, sizeof(p->subdevs));
1314 /* Locking: called with entity->parent->graph_mutex mutex held. */
1315 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable)
1317 struct media_entity *entity_err = entity;
1318 struct media_entity_graph graph;
1322 * Walk current graph and call the pipeline open/close routine for each
1323 * opened video node that belongs to the graph of entities connected
1324 * through active links. This is needed as we cannot power on/off the
1325 * subdevs in random order.
1327 media_entity_graph_walk_start(&graph, entity);
1329 while ((entity = media_entity_graph_walk_next(&graph))) {
1330 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
1333 ret = __fimc_md_modify_pipeline(entity, enable);
1341 media_entity_graph_walk_start(&graph, entity_err);
1343 while ((entity_err = media_entity_graph_walk_next(&graph))) {
1344 if (media_entity_type(entity_err) != MEDIA_ENT_T_DEVNODE)
1347 __fimc_md_modify_pipeline(entity_err, !enable);
1349 if (entity_err == entity)
1356 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1357 unsigned int notification)
1359 struct media_entity *sink = link->sink->entity;
1362 /* Before link disconnection */
1363 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1364 if (!(flags & MEDIA_LNK_FL_ENABLED))
1365 ret = __fimc_md_modify_pipelines(sink, false);
1367 ; /* TODO: Link state change validation */
1368 /* After link activation */
1369 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
1370 (link->flags & MEDIA_LNK_FL_ENABLED)) {
1371 ret = __fimc_md_modify_pipelines(sink, true);
1374 return ret ? -EPIPE : 0;
1377 static ssize_t fimc_md_sysfs_show(struct device *dev,
1378 struct device_attribute *attr, char *buf)
1380 struct platform_device *pdev = to_platform_device(dev);
1381 struct fimc_md *fmd = platform_get_drvdata(pdev);
1383 if (fmd->user_subdev_api)
1384 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1386 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1389 static ssize_t fimc_md_sysfs_store(struct device *dev,
1390 struct device_attribute *attr,
1391 const char *buf, size_t count)
1393 struct platform_device *pdev = to_platform_device(dev);
1394 struct fimc_md *fmd = platform_get_drvdata(pdev);
1398 if (!strcmp(buf, "vid-dev\n"))
1400 else if (!strcmp(buf, "sub-dev\n"))
1405 fmd->user_subdev_api = subdev_api;
1406 for (i = 0; i < FIMC_MAX_DEVS; i++)
1408 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1412 * This device attribute is to select video pipeline configuration method.
1413 * There are following valid values:
1414 * vid-dev - for V4L2 video node API only, subdevice will be configured
1415 * by the host driver.
1416 * sub-dev - for media controller API, subdevs must be configured in user
1417 * space before starting streaming.
1419 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1420 fimc_md_sysfs_show, fimc_md_sysfs_store);
1422 static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1424 struct device *dev = &fmd->pdev->dev;
1425 struct fimc_pinctrl *pctl = &fmd->pinctl;
1427 pctl->pinctrl = devm_pinctrl_get(dev);
1428 if (IS_ERR(pctl->pinctrl))
1429 return PTR_ERR(pctl->pinctrl);
1431 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1432 PINCTRL_STATE_DEFAULT);
1433 if (IS_ERR(pctl->state_default))
1434 return PTR_ERR(pctl->state_default);
1436 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1437 PINCTRL_STATE_IDLE);
1441 static int fimc_md_probe(struct platform_device *pdev)
1443 struct device *dev = &pdev->dev;
1444 struct v4l2_device *v4l2_dev;
1445 struct fimc_md *fmd;
1448 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1452 spin_lock_init(&fmd->slock);
1454 INIT_LIST_HEAD(&fmd->pipelines);
1456 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1457 sizeof(fmd->media_dev.model));
1458 fmd->media_dev.link_notify = fimc_md_link_notify;
1459 fmd->media_dev.dev = dev;
1461 v4l2_dev = &fmd->v4l2_dev;
1462 v4l2_dev->mdev = &fmd->media_dev;
1463 v4l2_dev->notify = fimc_sensor_notify;
1464 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1466 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1468 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1470 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1473 ret = media_device_register(&fmd->media_dev);
1475 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
1478 ret = fimc_md_get_clocks(fmd);
1482 fmd->user_subdev_api = (dev->of_node != NULL);
1484 /* Protect the media graph while we're registering entities */
1485 mutex_lock(&fmd->media_dev.graph_mutex);
1487 ret = fimc_md_get_pinctrl(fmd);
1489 if (ret != EPROBE_DEFER)
1490 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1495 ret = fimc_md_register_of_platform_entities(fmd, dev->of_node);
1497 ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
1498 fimc_md_pdev_match);
1502 if (dev->platform_data || dev->of_node) {
1503 ret = fimc_md_register_sensor_entities(fmd);
1508 ret = fimc_md_create_links(fmd);
1511 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1515 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1519 platform_set_drvdata(pdev, fmd);
1520 mutex_unlock(&fmd->media_dev.graph_mutex);
1524 mutex_unlock(&fmd->media_dev.graph_mutex);
1526 media_device_unregister(&fmd->media_dev);
1527 fimc_md_put_clocks(fmd);
1528 fimc_md_unregister_entities(fmd);
1530 v4l2_device_unregister(&fmd->v4l2_dev);
1534 static int fimc_md_remove(struct platform_device *pdev)
1536 struct fimc_md *fmd = platform_get_drvdata(pdev);
1541 v4l2_device_unregister(&fmd->v4l2_dev);
1542 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1543 fimc_md_unregister_entities(fmd);
1544 fimc_md_pipelines_free(fmd);
1545 media_device_unregister(&fmd->media_dev);
1546 fimc_md_put_clocks(fmd);
1550 static struct platform_device_id fimc_driver_ids[] __always_unused = {
1551 { .name = "s5p-fimc-md" },
1554 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1556 static const struct of_device_id fimc_md_of_match[] = {
1557 { .compatible = "samsung,fimc" },
1560 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1562 static struct platform_driver fimc_md_driver = {
1563 .probe = fimc_md_probe,
1564 .remove = fimc_md_remove,
1566 .of_match_table = of_match_ptr(fimc_md_of_match),
1567 .name = "s5p-fimc-md",
1568 .owner = THIS_MODULE,
1572 static int __init fimc_md_init(void)
1576 request_module("s5p-csis");
1577 ret = fimc_register_driver();
1581 return platform_driver_register(&fimc_md_driver);
1584 static void __exit fimc_md_exit(void)
1586 platform_driver_unregister(&fimc_md_driver);
1587 fimc_unregister_driver();
1590 module_init(fimc_md_init);
1591 module_exit(fimc_md_exit);
1593 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1594 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1595 MODULE_LICENSE("GPL");
1596 MODULE_VERSION("2.0.1");