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/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/types.h>
26 #include <linux/slab.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-of.h>
29 #include <media/media-device.h>
30 #include <media/s5p_fimc.h>
32 #include "media-dev.h"
33 #include "fimc-core.h"
35 #include "fimc-lite.h"
36 #include "mipi-csis.h"
38 static int __fimc_md_set_camclk(struct fimc_md *fmd,
39 struct fimc_source_info *si,
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);
92 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
94 sd = media_entity_to_v4l2_subdev(pad->entity);
100 case GRP_ID_FIMC_IS_SENSOR:
101 p->subdevs[IDX_SENSOR] = sd;
104 p->subdevs[IDX_CSIS] = sd;
107 p->subdevs[IDX_FLITE] = sd;
110 p->subdevs[IDX_FIMC] = sd;
113 p->subdevs[IDX_IS_ISP] = sd;
119 if (me->num_pads == 1)
123 if (sensor && p->subdevs[IDX_FIMC])
124 __setup_sensor_notification(fmd, sensor, p->subdevs[IDX_FIMC]);
128 * __subdev_set_power - change power state of a single subdev
129 * @sd: subdevice to change power state for
130 * @on: 1 to enable power or 0 to disable
132 * Return result of s_power subdev operation or -ENXIO if sd argument
133 * is NULL. Return 0 if the subdevice does not implement s_power.
135 static int __subdev_set_power(struct v4l2_subdev *sd, int on)
143 use_count = &sd->entity.use_count;
144 if (on && (*use_count)++ > 0)
146 else if (!on && (*use_count == 0 || --(*use_count) > 0))
148 ret = v4l2_subdev_call(sd, core, s_power, on);
150 return ret != -ENOIOCTLCMD ? ret : 0;
154 * fimc_pipeline_s_power - change power state of all pipeline subdevs
155 * @fimc: fimc device terminating the pipeline
156 * @state: true to power on, false to power off
158 * Needs to be called with the graph mutex held.
160 static int fimc_pipeline_s_power(struct fimc_pipeline *p, bool on)
162 static const u8 seq[2][IDX_MAX - 1] = {
163 { IDX_IS_ISP, IDX_SENSOR, IDX_CSIS, IDX_FLITE },
164 { IDX_CSIS, IDX_FLITE, IDX_SENSOR, IDX_IS_ISP },
168 if (p->subdevs[IDX_SENSOR] == NULL)
171 for (i = 0; i < IDX_MAX - 1; i++) {
172 unsigned int idx = seq[on][i];
174 ret = __subdev_set_power(p->subdevs[idx], on);
177 if (ret < 0 && ret != -ENXIO)
182 for (; i >= 0; i--) {
183 unsigned int idx = seq[on][i];
184 __subdev_set_power(p->subdevs[idx], !on);
190 * __fimc_pipeline_open - update the pipeline information, enable power
191 * of all pipeline subdevs and the sensor clock
192 * @me: media entity to start graph walk with
193 * @prepare: true to walk the current pipeline and acquire all subdevs
195 * Called with the graph mutex held.
197 static int __fimc_pipeline_open(struct exynos_media_pipeline *ep,
198 struct media_entity *me, bool prepare)
200 struct fimc_md *fmd = entity_to_fimc_mdev(me);
201 struct fimc_pipeline *p = to_fimc_pipeline(ep);
202 struct v4l2_subdev *sd;
205 if (WARN_ON(p == NULL || me == NULL))
209 fimc_pipeline_prepare(p, me);
211 sd = p->subdevs[IDX_SENSOR];
215 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
216 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP]) {
217 ret = clk_prepare_enable(fmd->wbclk[CLK_IDX_WB_B]);
221 ret = fimc_md_set_camclk(sd, true);
225 ret = fimc_pipeline_s_power(p, 1);
229 fimc_md_set_camclk(sd, false);
232 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
233 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
239 * __fimc_pipeline_close - disable the sensor clock and pipeline power
240 * @fimc: fimc device terminating the pipeline
242 * Disable power of all subdevs and turn the external sensor clock off.
244 static int __fimc_pipeline_close(struct exynos_media_pipeline *ep)
246 struct fimc_pipeline *p = to_fimc_pipeline(ep);
247 struct v4l2_subdev *sd = p ? p->subdevs[IDX_SENSOR] : NULL;
252 pr_warn("%s(): No sensor subdev\n", __func__);
256 ret = fimc_pipeline_s_power(p, 0);
257 fimc_md_set_camclk(sd, false);
259 fmd = entity_to_fimc_mdev(&sd->entity);
261 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
262 if (!IS_ERR(fmd->wbclk[CLK_IDX_WB_B]) && p->subdevs[IDX_IS_ISP])
263 clk_disable_unprepare(fmd->wbclk[CLK_IDX_WB_B]);
265 return ret == -ENXIO ? 0 : ret;
269 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
270 * @pipeline: video pipeline structure
271 * @on: passed as the s_stream() callback argument
273 static int __fimc_pipeline_s_stream(struct exynos_media_pipeline *ep, bool on)
275 static const u8 seq[2][IDX_MAX] = {
276 { IDX_FIMC, IDX_SENSOR, IDX_IS_ISP, IDX_CSIS, IDX_FLITE },
277 { IDX_CSIS, IDX_FLITE, IDX_FIMC, IDX_SENSOR, IDX_IS_ISP },
279 struct fimc_pipeline *p = to_fimc_pipeline(ep);
282 if (p->subdevs[IDX_SENSOR] == NULL)
285 for (i = 0; i < IDX_MAX; i++) {
286 unsigned int idx = seq[on][i];
288 ret = v4l2_subdev_call(p->subdevs[idx], video, s_stream, on);
290 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
295 for (; i >= 0; i--) {
296 unsigned int idx = seq[on][i];
297 v4l2_subdev_call(p->subdevs[idx], video, s_stream, !on);
302 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
303 static const struct exynos_media_pipeline_ops fimc_pipeline_ops = {
304 .open = __fimc_pipeline_open,
305 .close = __fimc_pipeline_close,
306 .set_stream = __fimc_pipeline_s_stream,
309 static struct exynos_media_pipeline *fimc_md_pipeline_create(
312 struct fimc_pipeline *p;
314 p = kzalloc(sizeof(*p), GFP_KERNEL);
318 list_add_tail(&p->list, &fmd->pipelines);
320 p->ep.ops = &fimc_pipeline_ops;
324 static void fimc_md_pipelines_free(struct fimc_md *fmd)
326 while (!list_empty(&fmd->pipelines)) {
327 struct fimc_pipeline *p;
329 p = list_entry(fmd->pipelines.next, typeof(*p), list);
336 * Sensor subdevice helper functions
338 static struct v4l2_subdev *fimc_md_register_sensor(struct fimc_md *fmd,
339 struct fimc_source_info *si)
341 struct i2c_adapter *adapter;
342 struct v4l2_subdev *sd = NULL;
347 * If FIMC bus type is not Writeback FIFO assume it is same
348 * as sensor_bus_type.
350 si->fimc_bus_type = si->sensor_bus_type;
352 adapter = i2c_get_adapter(si->i2c_bus_num);
354 v4l2_warn(&fmd->v4l2_dev,
355 "Failed to get I2C adapter %d, deferring probe\n",
357 return ERR_PTR(-EPROBE_DEFER);
359 sd = v4l2_i2c_new_subdev_board(&fmd->v4l2_dev, adapter,
360 si->board_info, NULL);
361 if (IS_ERR_OR_NULL(sd)) {
362 i2c_put_adapter(adapter);
363 v4l2_warn(&fmd->v4l2_dev,
364 "Failed to acquire subdev %s, deferring probe\n",
365 si->board_info->type);
366 return ERR_PTR(-EPROBE_DEFER);
368 v4l2_set_subdev_hostdata(sd, si);
369 sd->grp_id = GRP_ID_SENSOR;
371 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice %s\n",
376 static void fimc_md_unregister_sensor(struct v4l2_subdev *sd)
378 struct i2c_client *client = v4l2_get_subdevdata(sd);
379 struct i2c_adapter *adapter;
384 v4l2_device_unregister_subdev(sd);
386 if (!client->dev.of_node) {
387 adapter = client->adapter;
388 i2c_unregister_device(client);
390 i2c_put_adapter(adapter);
395 /* Register I2C client subdev associated with @node. */
396 static int fimc_md_of_add_sensor(struct fimc_md *fmd,
397 struct device_node *node, int index)
399 struct fimc_sensor_info *si;
400 struct i2c_client *client;
401 struct v4l2_subdev *sd;
404 if (WARN_ON(index >= ARRAY_SIZE(fmd->sensor)))
406 si = &fmd->sensor[index];
408 client = of_find_i2c_device_by_node(node);
410 return -EPROBE_DEFER;
412 device_lock(&client->dev);
414 if (!client->dev.driver ||
415 !try_module_get(client->dev.driver->owner)) {
417 v4l2_info(&fmd->v4l2_dev, "No driver found for %s\n",
422 /* Enable sensor's master clock */
423 ret = __fimc_md_set_camclk(fmd, &si->pdata, true);
426 sd = i2c_get_clientdata(client);
428 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
429 __fimc_md_set_camclk(fmd, &si->pdata, false);
433 v4l2_set_subdev_hostdata(sd, &si->pdata);
434 if (si->pdata.fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
435 sd->grp_id = GRP_ID_FIMC_IS_SENSOR;
437 sd->grp_id = GRP_ID_SENSOR;
440 v4l2_info(&fmd->v4l2_dev, "Registered sensor subdevice: %s (%d)\n",
441 sd->name, fmd->num_sensors);
445 module_put(client->dev.driver->owner);
447 device_unlock(&client->dev);
448 put_device(&client->dev);
452 /* Parse port node and register as a sub-device any sensor specified there. */
453 static int fimc_md_parse_port_node(struct fimc_md *fmd,
454 struct device_node *port,
457 struct device_node *rem, *ep, *np;
458 struct fimc_source_info *pd;
459 struct v4l2_of_endpoint endpoint;
463 pd = &fmd->sensor[index].pdata;
465 /* Assume here a port node can have only one endpoint node. */
466 ep = of_get_next_child(port, NULL);
470 v4l2_of_parse_endpoint(ep, &endpoint);
471 if (WARN_ON(endpoint.port == 0) || index >= FIMC_MAX_SENSORS)
474 pd->mux_id = (endpoint.port - 1) & 0x1;
476 rem = v4l2_of_get_remote_port_parent(ep);
479 v4l2_info(&fmd->v4l2_dev, "Remote device at %s not found\n",
483 if (!of_property_read_u32(rem, "samsung,camclk-out", &val))
486 if (!of_property_read_u32(rem, "clock-frequency", &val))
487 pd->clk_frequency = val;
489 if (pd->clk_frequency == 0) {
490 v4l2_err(&fmd->v4l2_dev, "Wrong clock frequency at node %s\n",
496 if (fimc_input_is_parallel(endpoint.port)) {
497 if (endpoint.bus_type == V4L2_MBUS_PARALLEL)
498 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_601;
500 pd->sensor_bus_type = FIMC_BUS_TYPE_ITU_656;
501 pd->flags = endpoint.bus.parallel.flags;
502 } else if (fimc_input_is_mipi_csi(endpoint.port)) {
504 * MIPI CSI-2: only input mux selection and
505 * the sensor's clock frequency is needed.
507 pd->sensor_bus_type = FIMC_BUS_TYPE_MIPI_CSI2;
509 v4l2_err(&fmd->v4l2_dev, "Wrong port id (%u) at node %s\n",
510 endpoint.port, rem->full_name);
513 * For FIMC-IS handled sensors, that are placed under i2c-isp device
514 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
515 * input. Sensors are attached to the FIMC-LITE hostdata interface
516 * directly or through MIPI-CSIS, depending on the external media bus
517 * used. This needs to be handled in a more reliable way, not by just
518 * checking parent's node name.
520 np = of_get_parent(rem);
522 if (np && !of_node_cmp(np->name, "i2c-isp"))
523 pd->fimc_bus_type = FIMC_BUS_TYPE_ISP_WRITEBACK;
525 pd->fimc_bus_type = pd->sensor_bus_type;
527 ret = fimc_md_of_add_sensor(fmd, rem, index);
533 /* Register all SoC external sub-devices */
534 static int fimc_md_of_sensors_register(struct fimc_md *fmd,
535 struct device_node *np)
537 struct device_node *parent = fmd->pdev->dev.of_node;
538 struct device_node *node, *ports;
542 /* Attach sensors linked to MIPI CSI-2 receivers */
543 for_each_available_child_of_node(parent, node) {
544 struct device_node *port;
546 if (of_node_cmp(node->name, "csis"))
548 /* The csis node can have only port subnode. */
549 port = of_get_next_child(node, NULL);
553 ret = fimc_md_parse_port_node(fmd, port, index);
559 /* Attach sensors listed in the parallel-ports node */
560 ports = of_get_child_by_name(parent, "parallel-ports");
564 for_each_child_of_node(ports, node) {
565 ret = fimc_md_parse_port_node(fmd, node, index);
574 static int __of_get_csis_id(struct device_node *np)
578 np = of_get_child_by_name(np, "port");
581 of_property_read_u32(np, "reg", ®);
582 return reg - FIMC_INPUT_MIPI_CSI2_0;
585 #define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
586 #define __of_get_csis_id(np) (-ENOSYS)
589 static int fimc_md_register_sensor_entities(struct fimc_md *fmd)
591 struct s5p_platform_fimc *pdata = fmd->pdev->dev.platform_data;
592 struct device_node *of_node = fmd->pdev->dev.of_node;
597 * Runtime resume one of the FIMC entities to make sure
598 * the sclk_cam clocks are not globally disabled.
603 ret = pm_runtime_get_sync(fmd->pmf);
608 fmd->num_sensors = 0;
609 ret = fimc_md_of_sensors_register(fmd, of_node);
611 WARN_ON(pdata->num_clients > ARRAY_SIZE(fmd->sensor));
612 num_clients = min_t(u32, pdata->num_clients,
613 ARRAY_SIZE(fmd->sensor));
614 fmd->num_sensors = num_clients;
616 for (i = 0; i < num_clients; i++) {
617 struct fimc_sensor_info *si = &fmd->sensor[i];
618 struct v4l2_subdev *sd;
620 si->pdata = pdata->source_info[i];
621 ret = __fimc_md_set_camclk(fmd, &si->pdata, true);
624 sd = fimc_md_register_sensor(fmd, &si->pdata);
625 ret = __fimc_md_set_camclk(fmd, &si->pdata, false);
638 pm_runtime_put(fmd->pmf);
643 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
646 static int register_fimc_lite_entity(struct fimc_md *fmd,
647 struct fimc_lite *fimc_lite)
649 struct v4l2_subdev *sd;
650 struct exynos_media_pipeline *ep;
653 if (WARN_ON(fimc_lite->index >= FIMC_LITE_MAX_DEVS ||
654 fmd->fimc_lite[fimc_lite->index]))
657 sd = &fimc_lite->subdev;
658 sd->grp_id = GRP_ID_FLITE;
660 ep = fimc_md_pipeline_create(fmd);
664 v4l2_set_subdev_hostdata(sd, ep);
666 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
668 fmd->fimc_lite[fimc_lite->index] = fimc_lite;
670 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.LITE%d\n",
675 static int register_fimc_entity(struct fimc_md *fmd, struct fimc_dev *fimc)
677 struct v4l2_subdev *sd;
678 struct exynos_media_pipeline *ep;
681 if (WARN_ON(fimc->id >= FIMC_MAX_DEVS || fmd->fimc[fimc->id]))
684 sd = &fimc->vid_cap.subdev;
685 sd->grp_id = GRP_ID_FIMC;
687 ep = fimc_md_pipeline_create(fmd);
691 v4l2_set_subdev_hostdata(sd, ep);
693 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
695 if (!fmd->pmf && fimc->pdev)
696 fmd->pmf = &fimc->pdev->dev;
697 fmd->fimc[fimc->id] = fimc;
698 fimc->vid_cap.user_subdev_api = fmd->user_subdev_api;
700 v4l2_err(&fmd->v4l2_dev, "Failed to register FIMC.%d (%d)\n",
706 static int register_csis_entity(struct fimc_md *fmd,
707 struct platform_device *pdev,
708 struct v4l2_subdev *sd)
710 struct device_node *node = pdev->dev.of_node;
713 id = node ? __of_get_csis_id(node) : max(0, pdev->id);
715 if (WARN_ON(id < 0 || id >= CSIS_MAX_ENTITIES))
718 if (WARN_ON(fmd->csis[id].sd))
721 sd->grp_id = GRP_ID_CSIS;
722 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
724 fmd->csis[id].sd = sd;
726 v4l2_err(&fmd->v4l2_dev,
727 "Failed to register MIPI-CSIS.%d (%d)\n", id, ret);
731 static int register_fimc_is_entity(struct fimc_md *fmd, struct fimc_is *is)
733 struct v4l2_subdev *sd = &is->isp.subdev;
736 ret = v4l2_device_register_subdev(&fmd->v4l2_dev, sd);
738 v4l2_err(&fmd->v4l2_dev,
739 "Failed to register FIMC-ISP (%d)\n", ret);
747 static int fimc_md_register_platform_entity(struct fimc_md *fmd,
748 struct platform_device *pdev,
751 struct device *dev = &pdev->dev;
752 int ret = -EPROBE_DEFER;
755 /* Lock to ensure dev->driver won't change. */
758 if (!dev->driver || !try_module_get(dev->driver->owner))
761 drvdata = dev_get_drvdata(dev);
762 /* Some subdev didn't probe successfully id drvdata is NULL */
764 switch (plat_entity) {
766 ret = register_fimc_entity(fmd, drvdata);
769 ret = register_fimc_lite_entity(fmd, drvdata);
772 ret = register_csis_entity(fmd, pdev, drvdata);
775 ret = register_fimc_is_entity(fmd, drvdata);
782 module_put(dev->driver->owner);
785 if (ret == -EPROBE_DEFER)
786 dev_info(&fmd->pdev->dev, "deferring %s device registration\n",
789 dev_err(&fmd->pdev->dev, "%s device registration failed (%d)\n",
794 static int fimc_md_pdev_match(struct device *dev, void *data)
796 struct platform_device *pdev = to_platform_device(dev);
797 int plat_entity = -1;
801 if (!get_device(dev))
804 if (!strcmp(pdev->name, CSIS_DRIVER_NAME)) {
805 plat_entity = IDX_CSIS;
807 p = strstr(pdev->name, "fimc");
808 if (p && *(p + 4) == 0)
809 plat_entity = IDX_FIMC;
812 if (plat_entity >= 0)
813 ret = fimc_md_register_platform_entity(data, pdev,
819 /* Register FIMC, FIMC-LITE and CSIS media entities */
821 static int fimc_md_register_of_platform_entities(struct fimc_md *fmd,
822 struct device_node *parent)
824 struct device_node *node;
827 for_each_available_child_of_node(parent, node) {
828 struct platform_device *pdev;
829 int plat_entity = -1;
831 pdev = of_find_device_by_node(node);
835 /* If driver of any entity isn't ready try all again later. */
836 if (!strcmp(node->name, CSIS_OF_NODE_NAME))
837 plat_entity = IDX_CSIS;
838 else if (!strcmp(node->name, FIMC_IS_OF_NODE_NAME))
839 plat_entity = IDX_IS_ISP;
840 else if (!strcmp(node->name, FIMC_LITE_OF_NODE_NAME))
841 plat_entity = IDX_FLITE;
842 else if (!strcmp(node->name, FIMC_OF_NODE_NAME) &&
843 !of_property_read_bool(node, "samsung,lcd-wb"))
844 plat_entity = IDX_FIMC;
846 if (plat_entity >= 0)
847 ret = fimc_md_register_platform_entity(fmd, pdev,
849 put_device(&pdev->dev);
857 #define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
860 static void fimc_md_unregister_entities(struct fimc_md *fmd)
864 for (i = 0; i < FIMC_MAX_DEVS; i++) {
865 struct fimc_dev *dev = fmd->fimc[i];
868 v4l2_device_unregister_subdev(&dev->vid_cap.subdev);
869 dev->vid_cap.ve.pipe = NULL;
872 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
873 struct fimc_lite *dev = fmd->fimc_lite[i];
876 v4l2_device_unregister_subdev(&dev->subdev);
878 fmd->fimc_lite[i] = NULL;
880 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
881 if (fmd->csis[i].sd == NULL)
883 v4l2_device_unregister_subdev(fmd->csis[i].sd);
884 fmd->csis[i].sd = NULL;
886 for (i = 0; i < fmd->num_sensors; i++) {
887 if (fmd->sensor[i].subdev == NULL)
889 fimc_md_unregister_sensor(fmd->sensor[i].subdev);
890 fmd->sensor[i].subdev = NULL;
894 v4l2_device_unregister_subdev(&fmd->fimc_is->isp.subdev);
896 v4l2_info(&fmd->v4l2_dev, "Unregistered all entities\n");
900 * __fimc_md_create_fimc_links - create links to all FIMC entities
901 * @fmd: fimc media device
902 * @source: the source entity to create links to all fimc entities from
903 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
904 * @pad: the source entity pad index
905 * @link_mask: bitmask of the fimc devices for which link should be enabled
907 static int __fimc_md_create_fimc_sink_links(struct fimc_md *fmd,
908 struct media_entity *source,
909 struct v4l2_subdev *sensor,
910 int pad, int link_mask)
912 struct fimc_source_info *si = NULL;
913 struct media_entity *sink;
914 unsigned int flags = 0;
918 si = v4l2_get_subdev_hostdata(sensor);
919 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
920 if (si && si->fimc_bus_type == FIMC_BUS_TYPE_ISP_WRITEBACK)
924 for (i = 0; !ret && i < FIMC_MAX_DEVS; i++) {
928 * Some FIMC variants are not fitted with camera capture
929 * interface. Skip creating a link from sensor for those.
931 if (!fmd->fimc[i]->variant->has_cam_if)
934 flags = ((1 << i) & link_mask) ? MEDIA_LNK_FL_ENABLED : 0;
936 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
937 ret = media_entity_create_link(source, pad, sink,
938 FIMC_SD_PAD_SINK_CAM, flags);
942 /* Notify FIMC capture subdev entity */
943 ret = media_entity_call(sink, link_setup, &sink->pads[0],
944 &source->pads[pad], flags);
948 v4l2_info(&fmd->v4l2_dev, "created link [%s] %c> [%s]\n",
949 source->name, flags ? '=' : '-', sink->name);
952 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
953 if (!fmd->fimc_lite[i])
956 sink = &fmd->fimc_lite[i]->subdev.entity;
957 ret = media_entity_create_link(source, pad, sink,
958 FLITE_SD_PAD_SINK, 0);
962 /* Notify FIMC-LITE subdev entity */
963 ret = media_entity_call(sink, link_setup, &sink->pads[0],
964 &source->pads[pad], 0);
968 v4l2_info(&fmd->v4l2_dev, "created link [%s] -> [%s]\n",
969 source->name, sink->name);
974 /* Create links from FIMC-LITE source pads to other entities */
975 static int __fimc_md_create_flite_source_links(struct fimc_md *fmd)
977 struct media_entity *source, *sink;
980 for (i = 0; i < FIMC_LITE_MAX_DEVS; i++) {
981 struct fimc_lite *fimc = fmd->fimc_lite[i];
986 source = &fimc->subdev.entity;
987 sink = &fimc->ve.vdev.entity;
988 /* FIMC-LITE's subdev and video node */
989 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_DMA,
993 /* Link from FIMC-LITE to IS-ISP subdev */
994 sink = &fmd->fimc_is->isp.subdev.entity;
995 ret = media_entity_create_link(source, FLITE_SD_PAD_SOURCE_ISP,
1004 /* Create FIMC-IS links */
1005 static int __fimc_md_create_fimc_is_links(struct fimc_md *fmd)
1007 struct media_entity *source, *sink;
1010 source = &fmd->fimc_is->isp.subdev.entity;
1012 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1013 if (fmd->fimc[i] == NULL)
1016 /* Link from IS-ISP subdev to FIMC */
1017 sink = &fmd->fimc[i]->vid_cap.subdev.entity;
1018 ret = media_entity_create_link(source, FIMC_ISP_SD_PAD_SRC_FIFO,
1019 sink, FIMC_SD_PAD_SINK_FIFO, 0);
1028 * fimc_md_create_links - create default links between registered entities
1030 * Parallel interface sensor entities are connected directly to FIMC capture
1031 * entities. The sensors using MIPI CSIS bus are connected through immutable
1032 * link with CSI receiver entity specified by mux_id. Any registered CSIS
1033 * entity has a link to each registered FIMC capture entity. Enabled links
1034 * are created by default between each subsequent registered sensor and
1035 * subsequent FIMC capture entity. The number of default active links is
1036 * determined by the number of available sensors or FIMC entities,
1037 * whichever is less.
1039 static int fimc_md_create_links(struct fimc_md *fmd)
1041 struct v4l2_subdev *csi_sensors[CSIS_MAX_ENTITIES] = { NULL };
1042 struct v4l2_subdev *sensor, *csis;
1043 struct fimc_source_info *pdata;
1044 struct media_entity *source, *sink;
1045 int i, pad, fimc_id = 0, ret = 0;
1046 u32 flags, link_mask = 0;
1048 for (i = 0; i < fmd->num_sensors; i++) {
1049 if (fmd->sensor[i].subdev == NULL)
1052 sensor = fmd->sensor[i].subdev;
1053 pdata = v4l2_get_subdev_hostdata(sensor);
1059 switch (pdata->sensor_bus_type) {
1060 case FIMC_BUS_TYPE_MIPI_CSI2:
1061 if (WARN(pdata->mux_id >= CSIS_MAX_ENTITIES,
1062 "Wrong CSI channel id: %d\n", pdata->mux_id))
1065 csis = fmd->csis[pdata->mux_id].sd;
1066 if (WARN(csis == NULL,
1067 "MIPI-CSI interface specified "
1068 "but s5p-csis module is not loaded!\n"))
1071 pad = sensor->entity.num_pads - 1;
1072 ret = media_entity_create_link(&sensor->entity, pad,
1073 &csis->entity, CSIS_PAD_SINK,
1074 MEDIA_LNK_FL_IMMUTABLE |
1075 MEDIA_LNK_FL_ENABLED);
1079 v4l2_info(&fmd->v4l2_dev, "created link [%s] => [%s]\n",
1080 sensor->entity.name, csis->entity.name);
1083 csi_sensors[pdata->mux_id] = sensor;
1086 case FIMC_BUS_TYPE_ITU_601...FIMC_BUS_TYPE_ITU_656:
1087 source = &sensor->entity;
1092 v4l2_err(&fmd->v4l2_dev, "Wrong bus_type: %x\n",
1093 pdata->sensor_bus_type);
1099 link_mask = 1 << fimc_id++;
1100 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1104 for (i = 0; i < CSIS_MAX_ENTITIES; i++) {
1105 if (fmd->csis[i].sd == NULL)
1108 source = &fmd->csis[i].sd->entity;
1109 pad = CSIS_PAD_SOURCE;
1110 sensor = csi_sensors[i];
1112 link_mask = 1 << fimc_id++;
1113 ret = __fimc_md_create_fimc_sink_links(fmd, source, sensor,
1117 /* Create immutable links between each FIMC's subdev and video node */
1118 flags = MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED;
1119 for (i = 0; i < FIMC_MAX_DEVS; i++) {
1123 source = &fmd->fimc[i]->vid_cap.subdev.entity;
1124 sink = &fmd->fimc[i]->vid_cap.ve.vdev.entity;
1126 ret = media_entity_create_link(source, FIMC_SD_PAD_SOURCE,
1132 ret = __fimc_md_create_flite_source_links(fmd);
1137 ret = __fimc_md_create_fimc_is_links(fmd);
1143 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1145 static void fimc_md_put_clocks(struct fimc_md *fmd)
1147 int i = FIMC_MAX_CAMCLKS;
1150 if (IS_ERR(fmd->camclk[i].clock))
1152 clk_put(fmd->camclk[i].clock);
1153 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1156 /* Writeback (PIXELASYNCMx) clocks */
1157 for (i = 0; i < FIMC_MAX_WBCLKS; i++) {
1158 if (IS_ERR(fmd->wbclk[i]))
1160 clk_put(fmd->wbclk[i]);
1161 fmd->wbclk[i] = ERR_PTR(-EINVAL);
1165 static int fimc_md_get_clocks(struct fimc_md *fmd)
1167 struct device *dev = NULL;
1172 for (i = 0; i < FIMC_MAX_CAMCLKS; i++)
1173 fmd->camclk[i].clock = ERR_PTR(-EINVAL);
1175 if (fmd->pdev->dev.of_node)
1176 dev = &fmd->pdev->dev;
1178 for (i = 0; i < FIMC_MAX_CAMCLKS; i++) {
1179 snprintf(clk_name, sizeof(clk_name), "sclk_cam%u", i);
1180 clock = clk_get(dev, clk_name);
1182 if (IS_ERR(clock)) {
1183 dev_err(&fmd->pdev->dev, "Failed to get clock: %s\n",
1185 ret = PTR_ERR(clock);
1188 fmd->camclk[i].clock = clock;
1191 fimc_md_put_clocks(fmd);
1196 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1197 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1199 fmd->wbclk[CLK_IDX_WB_A] = ERR_PTR(-EINVAL);
1201 for (i = CLK_IDX_WB_B; i < FIMC_MAX_WBCLKS; i++) {
1202 snprintf(clk_name, sizeof(clk_name), "pxl_async%u", i);
1203 clock = clk_get(dev, clk_name);
1204 if (IS_ERR(clock)) {
1205 v4l2_err(&fmd->v4l2_dev, "Failed to get clock: %s\n",
1207 ret = PTR_ERR(clock);
1210 fmd->wbclk[i] = clock;
1213 fimc_md_put_clocks(fmd);
1218 static int __fimc_md_set_camclk(struct fimc_md *fmd,
1219 struct fimc_source_info *si,
1222 struct fimc_camclk_info *camclk;
1225 if (WARN_ON(si->clk_id >= FIMC_MAX_CAMCLKS) || !fmd || !fmd->pmf)
1228 camclk = &fmd->camclk[si->clk_id];
1230 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
1231 si->clk_id, si->clk_frequency, camclk->use_count, on);
1234 if (camclk->use_count > 0 &&
1235 camclk->frequency != si->clk_frequency)
1238 if (camclk->use_count++ == 0) {
1239 clk_set_rate(camclk->clock, si->clk_frequency);
1240 camclk->frequency = si->clk_frequency;
1241 ret = pm_runtime_get_sync(fmd->pmf);
1244 ret = clk_prepare_enable(camclk->clock);
1245 dbg("Enabled camclk %d: f: %lu", si->clk_id,
1246 clk_get_rate(camclk->clock));
1251 if (WARN_ON(camclk->use_count == 0))
1254 if (--camclk->use_count == 0) {
1255 clk_disable_unprepare(camclk->clock);
1256 pm_runtime_put(fmd->pmf);
1257 dbg("Disabled camclk %d", si->clk_id);
1263 * fimc_md_set_camclk - peripheral sensor clock setup
1264 * @sd: sensor subdev to configure sclk_cam clock for
1265 * @on: 1 to enable or 0 to disable the clock
1267 * There are 2 separate clock outputs available in the SoC for external
1268 * image processors. These clocks are shared between all registered FIMC
1269 * devices to which sensors can be attached, either directly or through
1270 * the MIPI CSI receiver. The clock is allowed here to be used by
1271 * multiple sensors concurrently if they use same frequency.
1272 * This function should only be called when the graph mutex is held.
1274 int fimc_md_set_camclk(struct v4l2_subdev *sd, bool on)
1276 struct fimc_source_info *si = v4l2_get_subdev_hostdata(sd);
1277 struct fimc_md *fmd = entity_to_fimc_mdev(&sd->entity);
1279 return __fimc_md_set_camclk(fmd, si, on);
1282 static int __fimc_md_modify_pipeline(struct media_entity *entity, bool enable)
1284 struct exynos_video_entity *ve;
1285 struct fimc_pipeline *p;
1286 struct video_device *vdev;
1289 vdev = media_entity_to_video_device(entity);
1290 if (vdev->entity.use_count == 0)
1293 ve = vdev_to_exynos_video_entity(vdev);
1294 p = to_fimc_pipeline(ve->pipe);
1296 * Nothing to do if we are disabling the pipeline, some link
1297 * has been disconnected and p->subdevs array is cleared now.
1299 if (!enable && p->subdevs[IDX_SENSOR] == NULL)
1303 ret = __fimc_pipeline_open(ve->pipe, entity, true);
1305 ret = __fimc_pipeline_close(ve->pipe);
1307 if (ret == 0 && !enable)
1308 memset(p->subdevs, 0, sizeof(p->subdevs));
1313 /* Locking: called with entity->parent->graph_mutex mutex held. */
1314 static int __fimc_md_modify_pipelines(struct media_entity *entity, bool enable)
1316 struct media_entity *entity_err = entity;
1317 struct media_entity_graph graph;
1321 * Walk current graph and call the pipeline open/close routine for each
1322 * opened video node that belongs to the graph of entities connected
1323 * through active links. This is needed as we cannot power on/off the
1324 * subdevs in random order.
1326 media_entity_graph_walk_start(&graph, entity);
1328 while ((entity = media_entity_graph_walk_next(&graph))) {
1329 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
1332 ret = __fimc_md_modify_pipeline(entity, enable);
1340 media_entity_graph_walk_start(&graph, entity_err);
1342 while ((entity_err = media_entity_graph_walk_next(&graph))) {
1343 if (media_entity_type(entity_err) != MEDIA_ENT_T_DEVNODE)
1346 __fimc_md_modify_pipeline(entity_err, !enable);
1348 if (entity_err == entity)
1355 static int fimc_md_link_notify(struct media_link *link, unsigned int flags,
1356 unsigned int notification)
1358 struct media_entity *sink = link->sink->entity;
1361 /* Before link disconnection */
1362 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH) {
1363 if (!(flags & MEDIA_LNK_FL_ENABLED))
1364 ret = __fimc_md_modify_pipelines(sink, false);
1366 ; /* TODO: Link state change validation */
1367 /* After link activation */
1368 } else if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
1369 (link->flags & MEDIA_LNK_FL_ENABLED)) {
1370 ret = __fimc_md_modify_pipelines(sink, true);
1373 return ret ? -EPIPE : 0;
1376 static ssize_t fimc_md_sysfs_show(struct device *dev,
1377 struct device_attribute *attr, char *buf)
1379 struct platform_device *pdev = to_platform_device(dev);
1380 struct fimc_md *fmd = platform_get_drvdata(pdev);
1382 if (fmd->user_subdev_api)
1383 return strlcpy(buf, "Sub-device API (sub-dev)\n", PAGE_SIZE);
1385 return strlcpy(buf, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE);
1388 static ssize_t fimc_md_sysfs_store(struct device *dev,
1389 struct device_attribute *attr,
1390 const char *buf, size_t count)
1392 struct platform_device *pdev = to_platform_device(dev);
1393 struct fimc_md *fmd = platform_get_drvdata(pdev);
1397 if (!strcmp(buf, "vid-dev\n"))
1399 else if (!strcmp(buf, "sub-dev\n"))
1404 fmd->user_subdev_api = subdev_api;
1405 for (i = 0; i < FIMC_MAX_DEVS; i++)
1407 fmd->fimc[i]->vid_cap.user_subdev_api = subdev_api;
1411 * This device attribute is to select video pipeline configuration method.
1412 * There are following valid values:
1413 * vid-dev - for V4L2 video node API only, subdevice will be configured
1414 * by the host driver.
1415 * sub-dev - for media controller API, subdevs must be configured in user
1416 * space before starting streaming.
1418 static DEVICE_ATTR(subdev_conf_mode, S_IWUSR | S_IRUGO,
1419 fimc_md_sysfs_show, fimc_md_sysfs_store);
1421 static int fimc_md_get_pinctrl(struct fimc_md *fmd)
1423 struct device *dev = &fmd->pdev->dev;
1424 struct fimc_pinctrl *pctl = &fmd->pinctl;
1426 pctl->pinctrl = devm_pinctrl_get(dev);
1427 if (IS_ERR(pctl->pinctrl))
1428 return PTR_ERR(pctl->pinctrl);
1430 pctl->state_default = pinctrl_lookup_state(pctl->pinctrl,
1431 PINCTRL_STATE_DEFAULT);
1432 if (IS_ERR(pctl->state_default))
1433 return PTR_ERR(pctl->state_default);
1435 pctl->state_idle = pinctrl_lookup_state(pctl->pinctrl,
1436 PINCTRL_STATE_IDLE);
1440 static int fimc_md_probe(struct platform_device *pdev)
1442 struct device *dev = &pdev->dev;
1443 struct v4l2_device *v4l2_dev;
1444 struct fimc_md *fmd;
1447 fmd = devm_kzalloc(dev, sizeof(*fmd), GFP_KERNEL);
1451 spin_lock_init(&fmd->slock);
1453 INIT_LIST_HEAD(&fmd->pipelines);
1455 strlcpy(fmd->media_dev.model, "SAMSUNG S5P FIMC",
1456 sizeof(fmd->media_dev.model));
1457 fmd->media_dev.link_notify = fimc_md_link_notify;
1458 fmd->media_dev.dev = dev;
1460 v4l2_dev = &fmd->v4l2_dev;
1461 v4l2_dev->mdev = &fmd->media_dev;
1462 v4l2_dev->notify = fimc_sensor_notify;
1463 strlcpy(v4l2_dev->name, "s5p-fimc-md", sizeof(v4l2_dev->name));
1465 fmd->use_isp = fimc_md_is_isp_available(dev->of_node);
1467 ret = v4l2_device_register(dev, &fmd->v4l2_dev);
1469 v4l2_err(v4l2_dev, "Failed to register v4l2_device: %d\n", ret);
1472 ret = media_device_register(&fmd->media_dev);
1474 v4l2_err(v4l2_dev, "Failed to register media device: %d\n", ret);
1477 ret = fimc_md_get_clocks(fmd);
1481 fmd->user_subdev_api = (dev->of_node != NULL);
1483 /* Protect the media graph while we're registering entities */
1484 mutex_lock(&fmd->media_dev.graph_mutex);
1486 ret = fimc_md_get_pinctrl(fmd);
1488 if (ret != EPROBE_DEFER)
1489 dev_err(dev, "Failed to get pinctrl: %d\n", ret);
1494 ret = fimc_md_register_of_platform_entities(fmd, dev->of_node);
1496 ret = bus_for_each_dev(&platform_bus_type, NULL, fmd,
1497 fimc_md_pdev_match);
1501 if (dev->platform_data || dev->of_node) {
1502 ret = fimc_md_register_sensor_entities(fmd);
1507 ret = fimc_md_create_links(fmd);
1510 ret = v4l2_device_register_subdev_nodes(&fmd->v4l2_dev);
1514 ret = device_create_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1518 platform_set_drvdata(pdev, fmd);
1519 mutex_unlock(&fmd->media_dev.graph_mutex);
1523 mutex_unlock(&fmd->media_dev.graph_mutex);
1525 fimc_md_put_clocks(fmd);
1526 fimc_md_unregister_entities(fmd);
1527 media_device_unregister(&fmd->media_dev);
1529 v4l2_device_unregister(&fmd->v4l2_dev);
1533 static int fimc_md_remove(struct platform_device *pdev)
1535 struct fimc_md *fmd = platform_get_drvdata(pdev);
1540 v4l2_device_unregister(&fmd->v4l2_dev);
1541 device_remove_file(&pdev->dev, &dev_attr_subdev_conf_mode);
1542 fimc_md_unregister_entities(fmd);
1543 fimc_md_pipelines_free(fmd);
1544 media_device_unregister(&fmd->media_dev);
1545 fimc_md_put_clocks(fmd);
1549 static struct platform_device_id fimc_driver_ids[] __always_unused = {
1550 { .name = "s5p-fimc-md" },
1553 MODULE_DEVICE_TABLE(platform, fimc_driver_ids);
1555 static const struct of_device_id fimc_md_of_match[] = {
1556 { .compatible = "samsung,fimc" },
1559 MODULE_DEVICE_TABLE(of, fimc_md_of_match);
1561 static struct platform_driver fimc_md_driver = {
1562 .probe = fimc_md_probe,
1563 .remove = fimc_md_remove,
1565 .of_match_table = of_match_ptr(fimc_md_of_match),
1566 .name = "s5p-fimc-md",
1567 .owner = THIS_MODULE,
1571 static int __init fimc_md_init(void)
1575 request_module("s5p-csis");
1576 ret = fimc_register_driver();
1580 return platform_driver_register(&fimc_md_driver);
1583 static void __exit fimc_md_exit(void)
1585 platform_driver_unregister(&fimc_md_driver);
1586 fimc_unregister_driver();
1589 module_init(fimc_md_init);
1590 module_exit(fimc_md_exit);
1592 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1593 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1594 MODULE_LICENSE("GPL");
1595 MODULE_VERSION("2.0.1");