2 * S5P/EXYNOS4 SoC series camera host interface media device driver
4 * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd.
5 * 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 * fimc_pipeline_prepare - update pipeline information with subdevice pointers
44 * @me: media entity terminating the pipeline
46 * Caller holds the graph mutex.
48 static void fimc_pipeline_prepare(struct fimc_pipeline
*p
,
49 struct media_entity
*me
)
51 struct v4l2_subdev
*sd
;
54 for (i
= 0; i
< IDX_MAX
; i
++)
58 struct media_pad
*pad
= NULL
;
60 /* Find remote source pad */
61 for (i
= 0; i
< me
->num_pads
; i
++) {
62 struct media_pad
*spad
= &me
->pads
[i
];
63 if (!(spad
->flags
& MEDIA_PAD_FL_SINK
))
65 pad
= media_entity_remote_source(spad
);
71 media_entity_type(pad
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
73 sd
= media_entity_to_v4l2_subdev(pad
->entity
);
76 case GRP_ID_FIMC_IS_SENSOR
:
78 p
->subdevs
[IDX_SENSOR
] = sd
;
81 p
->subdevs
[IDX_CSIS
] = sd
;
84 p
->subdevs
[IDX_FLITE
] = sd
;
87 /* No need to control FIMC subdev through subdev ops */
90 p
->subdevs
[IDX_IS_ISP
] = sd
;
96 if (me
->num_pads
== 1)
102 * __subdev_set_power - change power state of a single subdev
103 * @sd: subdevice to change power state for
104 * @on: 1 to enable power or 0 to disable
106 * Return result of s_power subdev operation or -ENXIO if sd argument
107 * is NULL. Return 0 if the subdevice does not implement s_power.
109 static int __subdev_set_power(struct v4l2_subdev
*sd
, int on
)
117 use_count
= &sd
->entity
.use_count
;
118 if (on
&& (*use_count
)++ > 0)
120 else if (!on
&& (*use_count
== 0 || --(*use_count
) > 0))
122 ret
= v4l2_subdev_call(sd
, core
, s_power
, on
);
124 return ret
!= -ENOIOCTLCMD
? ret
: 0;
128 * fimc_pipeline_s_power - change power state of all pipeline subdevs
129 * @fimc: fimc device terminating the pipeline
130 * @state: true to power on, false to power off
132 * Needs to be called with the graph mutex held.
134 static int fimc_pipeline_s_power(struct fimc_pipeline
*p
, bool on
)
136 static const u8 seq
[2][IDX_MAX
- 1] = {
137 { IDX_IS_ISP
, IDX_SENSOR
, IDX_CSIS
, IDX_FLITE
},
138 { IDX_CSIS
, IDX_FLITE
, IDX_SENSOR
, IDX_IS_ISP
},
142 if (p
->subdevs
[IDX_SENSOR
] == NULL
)
145 for (i
= 0; i
< IDX_MAX
- 1; i
++) {
146 unsigned int idx
= seq
[on
][i
];
148 ret
= __subdev_set_power(p
->subdevs
[idx
], on
);
151 if (ret
< 0 && ret
!= -ENXIO
)
156 for (; i
>= 0; i
--) {
157 unsigned int idx
= seq
[on
][i
];
158 __subdev_set_power(p
->subdevs
[idx
], !on
);
164 * __fimc_pipeline_open - update the pipeline information, enable power
165 * of all pipeline subdevs and the sensor clock
166 * @me: media entity to start graph walk with
167 * @prepare: true to walk the current pipeline and acquire all subdevs
169 * Called with the graph mutex held.
171 static int __fimc_pipeline_open(struct fimc_pipeline
*p
,
172 struct media_entity
*me
, bool prepare
)
174 struct fimc_md
*fmd
= entity_to_fimc_mdev(me
);
175 struct v4l2_subdev
*sd
;
178 if (WARN_ON(p
== NULL
|| me
== NULL
))
182 fimc_pipeline_prepare(p
, me
);
184 sd
= p
->subdevs
[IDX_SENSOR
];
188 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
189 if (!IS_ERR(fmd
->wbclk
[CLK_IDX_WB_B
]) && p
->subdevs
[IDX_IS_ISP
]) {
190 ret
= clk_prepare_enable(fmd
->wbclk
[CLK_IDX_WB_B
]);
194 ret
= fimc_md_set_camclk(sd
, true);
198 ret
= fimc_pipeline_s_power(p
, 1);
202 fimc_md_set_camclk(sd
, false);
205 if (!IS_ERR(fmd
->wbclk
[CLK_IDX_WB_B
]) && p
->subdevs
[IDX_IS_ISP
])
206 clk_disable_unprepare(fmd
->wbclk
[CLK_IDX_WB_B
]);
212 * __fimc_pipeline_close - disable the sensor clock and pipeline power
213 * @fimc: fimc device terminating the pipeline
215 * Disable power of all subdevs and turn the external sensor clock off.
217 static int __fimc_pipeline_close(struct fimc_pipeline
*p
)
219 struct v4l2_subdev
*sd
= p
? p
->subdevs
[IDX_SENSOR
] : NULL
;
223 if (WARN_ON(sd
== NULL
))
226 if (p
->subdevs
[IDX_SENSOR
]) {
227 ret
= fimc_pipeline_s_power(p
, 0);
228 fimc_md_set_camclk(sd
, false);
231 fmd
= entity_to_fimc_mdev(&sd
->entity
);
233 /* Disable PXLASYNC clock if this pipeline includes FIMC-IS */
234 if (!IS_ERR(fmd
->wbclk
[CLK_IDX_WB_B
]) && p
->subdevs
[IDX_IS_ISP
])
235 clk_disable_unprepare(fmd
->wbclk
[CLK_IDX_WB_B
]);
237 return ret
== -ENXIO
? 0 : ret
;
241 * __fimc_pipeline_s_stream - call s_stream() on pipeline subdevs
242 * @pipeline: video pipeline structure
243 * @on: passed as the s_stream() callback argument
245 static int __fimc_pipeline_s_stream(struct fimc_pipeline
*p
, bool on
)
247 static const u8 seq
[2][IDX_MAX
] = {
248 { IDX_FIMC
, IDX_SENSOR
, IDX_IS_ISP
, IDX_CSIS
, IDX_FLITE
},
249 { IDX_CSIS
, IDX_FLITE
, IDX_FIMC
, IDX_SENSOR
, IDX_IS_ISP
},
253 if (p
->subdevs
[IDX_SENSOR
] == NULL
)
256 for (i
= 0; i
< IDX_MAX
; i
++) {
257 unsigned int idx
= seq
[on
][i
];
259 ret
= v4l2_subdev_call(p
->subdevs
[idx
], video
, s_stream
, on
);
261 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
266 for (; i
>= 0; i
--) {
267 unsigned int idx
= seq
[on
][i
];
268 v4l2_subdev_call(p
->subdevs
[idx
], video
, s_stream
, !on
);
273 /* Media pipeline operations for the FIMC/FIMC-LITE video device driver */
274 static const struct fimc_pipeline_ops fimc_pipeline_ops
= {
275 .open
= __fimc_pipeline_open
,
276 .close
= __fimc_pipeline_close
,
277 .set_stream
= __fimc_pipeline_s_stream
,
281 * Sensor subdevice helper functions
283 static struct v4l2_subdev
*fimc_md_register_sensor(struct fimc_md
*fmd
,
284 struct fimc_source_info
*si
)
286 struct i2c_adapter
*adapter
;
287 struct v4l2_subdev
*sd
= NULL
;
292 * If FIMC bus type is not Writeback FIFO assume it is same
293 * as sensor_bus_type.
295 si
->fimc_bus_type
= si
->sensor_bus_type
;
297 adapter
= i2c_get_adapter(si
->i2c_bus_num
);
299 v4l2_warn(&fmd
->v4l2_dev
,
300 "Failed to get I2C adapter %d, deferring probe\n",
302 return ERR_PTR(-EPROBE_DEFER
);
304 sd
= v4l2_i2c_new_subdev_board(&fmd
->v4l2_dev
, adapter
,
305 si
->board_info
, NULL
);
306 if (IS_ERR_OR_NULL(sd
)) {
307 i2c_put_adapter(adapter
);
308 v4l2_warn(&fmd
->v4l2_dev
,
309 "Failed to acquire subdev %s, deferring probe\n",
310 si
->board_info
->type
);
311 return ERR_PTR(-EPROBE_DEFER
);
313 v4l2_set_subdev_hostdata(sd
, si
);
314 sd
->grp_id
= GRP_ID_SENSOR
;
316 v4l2_info(&fmd
->v4l2_dev
, "Registered sensor subdevice %s\n",
321 static void fimc_md_unregister_sensor(struct v4l2_subdev
*sd
)
323 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
324 struct i2c_adapter
*adapter
;
329 v4l2_device_unregister_subdev(sd
);
331 if (!client
->dev
.of_node
) {
332 adapter
= client
->adapter
;
333 i2c_unregister_device(client
);
335 i2c_put_adapter(adapter
);
340 /* Register I2C client subdev associated with @node. */
341 static int fimc_md_of_add_sensor(struct fimc_md
*fmd
,
342 struct device_node
*node
, int index
)
344 struct fimc_sensor_info
*si
;
345 struct i2c_client
*client
;
346 struct v4l2_subdev
*sd
;
349 if (WARN_ON(index
>= ARRAY_SIZE(fmd
->sensor
)))
351 si
= &fmd
->sensor
[index
];
353 client
= of_find_i2c_device_by_node(node
);
355 return -EPROBE_DEFER
;
357 device_lock(&client
->dev
);
359 if (!client
->driver
||
360 !try_module_get(client
->driver
->driver
.owner
)) {
362 v4l2_info(&fmd
->v4l2_dev
, "No driver found for %s\n",
367 /* Enable sensor's master clock */
368 ret
= __fimc_md_set_camclk(fmd
, &si
->pdata
, true);
371 sd
= i2c_get_clientdata(client
);
373 ret
= v4l2_device_register_subdev(&fmd
->v4l2_dev
, sd
);
374 __fimc_md_set_camclk(fmd
, &si
->pdata
, false);
378 v4l2_set_subdev_hostdata(sd
, &si
->pdata
);
379 if (si
->pdata
.fimc_bus_type
== FIMC_BUS_TYPE_ISP_WRITEBACK
)
380 sd
->grp_id
= GRP_ID_FIMC_IS_SENSOR
;
382 sd
->grp_id
= GRP_ID_SENSOR
;
385 v4l2_info(&fmd
->v4l2_dev
, "Registered sensor subdevice: %s (%d)\n",
386 sd
->name
, fmd
->num_sensors
);
390 module_put(client
->driver
->driver
.owner
);
392 device_unlock(&client
->dev
);
393 put_device(&client
->dev
);
397 /* Parse port node and register as a sub-device any sensor specified there. */
398 static int fimc_md_parse_port_node(struct fimc_md
*fmd
,
399 struct device_node
*port
,
402 struct device_node
*rem
, *ep
, *np
;
403 struct fimc_source_info
*pd
;
404 struct v4l2_of_endpoint endpoint
;
408 pd
= &fmd
->sensor
[index
].pdata
;
410 /* Assume here a port node can have only one endpoint node. */
411 ep
= of_get_next_child(port
, NULL
);
415 v4l2_of_parse_endpoint(ep
, &endpoint
);
416 if (WARN_ON(endpoint
.port
== 0) || index
>= FIMC_MAX_SENSORS
)
419 pd
->mux_id
= (endpoint
.port
- 1) & 0x1;
421 rem
= v4l2_of_get_remote_port_parent(ep
);
424 v4l2_info(&fmd
->v4l2_dev
, "Remote device at %s not found\n",
428 if (!of_property_read_u32(rem
, "samsung,camclk-out", &val
))
431 if (!of_property_read_u32(rem
, "clock-frequency", &val
))
432 pd
->clk_frequency
= val
;
434 if (pd
->clk_frequency
== 0) {
435 v4l2_err(&fmd
->v4l2_dev
, "Wrong clock frequency at node %s\n",
441 if (fimc_input_is_parallel(endpoint
.port
)) {
442 if (endpoint
.bus_type
== V4L2_MBUS_PARALLEL
)
443 pd
->sensor_bus_type
= FIMC_BUS_TYPE_ITU_601
;
445 pd
->sensor_bus_type
= FIMC_BUS_TYPE_ITU_656
;
446 pd
->flags
= endpoint
.bus
.parallel
.flags
;
447 } else if (fimc_input_is_mipi_csi(endpoint
.port
)) {
449 * MIPI CSI-2: only input mux selection and
450 * the sensor's clock frequency is needed.
452 pd
->sensor_bus_type
= FIMC_BUS_TYPE_MIPI_CSI2
;
454 v4l2_err(&fmd
->v4l2_dev
, "Wrong port id (%u) at node %s\n",
455 endpoint
.port
, rem
->full_name
);
458 * For FIMC-IS handled sensors, that are placed under i2c-isp device
459 * node, FIMC is connected to the FIMC-IS through its ISP Writeback
460 * input. Sensors are attached to the FIMC-LITE hostdata interface
461 * directly or through MIPI-CSIS, depending on the external media bus
462 * used. This needs to be handled in a more reliable way, not by just
463 * checking parent's node name.
465 np
= of_get_parent(rem
);
467 if (np
&& !of_node_cmp(np
->name
, "i2c-isp"))
468 pd
->fimc_bus_type
= FIMC_BUS_TYPE_ISP_WRITEBACK
;
470 pd
->fimc_bus_type
= pd
->sensor_bus_type
;
472 ret
= fimc_md_of_add_sensor(fmd
, rem
, index
);
478 /* Register all SoC external sub-devices */
479 static int fimc_md_of_sensors_register(struct fimc_md
*fmd
,
480 struct device_node
*np
)
482 struct device_node
*parent
= fmd
->pdev
->dev
.of_node
;
483 struct device_node
*node
, *ports
;
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
);
504 /* Attach sensors listed in the parallel-ports node */
505 ports
= of_get_child_by_name(parent
, "parallel-ports");
509 for_each_child_of_node(ports
, node
) {
510 ret
= fimc_md_parse_port_node(fmd
, node
, index
);
519 static int __of_get_csis_id(struct device_node
*np
)
523 np
= of_get_child_by_name(np
, "port");
526 of_property_read_u32(np
, "reg", ®
);
527 return reg
- FIMC_INPUT_MIPI_CSI2_0
;
530 #define fimc_md_of_sensors_register(fmd, np) (-ENOSYS)
531 #define __of_get_csis_id(np) (-ENOSYS)
534 static int fimc_md_register_sensor_entities(struct fimc_md
*fmd
)
536 struct s5p_platform_fimc
*pdata
= fmd
->pdev
->dev
.platform_data
;
537 struct device_node
*of_node
= fmd
->pdev
->dev
.of_node
;
542 * Runtime resume one of the FIMC entities to make sure
543 * the sclk_cam clocks are not globally disabled.
548 ret
= pm_runtime_get_sync(fmd
->pmf
);
553 fmd
->num_sensors
= 0;
554 ret
= fimc_md_of_sensors_register(fmd
, of_node
);
556 WARN_ON(pdata
->num_clients
> ARRAY_SIZE(fmd
->sensor
));
557 num_clients
= min_t(u32
, pdata
->num_clients
,
558 ARRAY_SIZE(fmd
->sensor
));
559 fmd
->num_sensors
= num_clients
;
561 for (i
= 0; i
< num_clients
; i
++) {
562 struct fimc_sensor_info
*si
= &fmd
->sensor
[i
];
563 struct v4l2_subdev
*sd
;
565 si
->pdata
= pdata
->source_info
[i
];
566 ret
= __fimc_md_set_camclk(fmd
, &si
->pdata
, true);
569 sd
= fimc_md_register_sensor(fmd
, &si
->pdata
);
570 ret
= __fimc_md_set_camclk(fmd
, &si
->pdata
, false);
583 pm_runtime_put(fmd
->pmf
);
588 * MIPI-CSIS, FIMC and FIMC-LITE platform devices registration.
591 static int register_fimc_lite_entity(struct fimc_md
*fmd
,
592 struct fimc_lite
*fimc_lite
)
594 struct v4l2_subdev
*sd
;
597 if (WARN_ON(fimc_lite
->index
>= FIMC_LITE_MAX_DEVS
||
598 fmd
->fimc_lite
[fimc_lite
->index
]))
601 sd
= &fimc_lite
->subdev
;
602 sd
->grp_id
= GRP_ID_FLITE
;
603 v4l2_set_subdev_hostdata(sd
, (void *)&fimc_pipeline_ops
);
605 ret
= v4l2_device_register_subdev(&fmd
->v4l2_dev
, sd
);
607 fmd
->fimc_lite
[fimc_lite
->index
] = fimc_lite
;
609 v4l2_err(&fmd
->v4l2_dev
, "Failed to register FIMC.LITE%d\n",
614 static int register_fimc_entity(struct fimc_md
*fmd
, struct fimc_dev
*fimc
)
616 struct v4l2_subdev
*sd
;
619 if (WARN_ON(fimc
->id
>= FIMC_MAX_DEVS
|| fmd
->fimc
[fimc
->id
]))
622 sd
= &fimc
->vid_cap
.subdev
;
623 sd
->grp_id
= GRP_ID_FIMC
;
624 v4l2_set_subdev_hostdata(sd
, (void *)&fimc_pipeline_ops
);
626 ret
= v4l2_device_register_subdev(&fmd
->v4l2_dev
, sd
);
628 if (!fmd
->pmf
&& fimc
->pdev
)
629 fmd
->pmf
= &fimc
->pdev
->dev
;
630 fmd
->fimc
[fimc
->id
] = fimc
;
631 fimc
->vid_cap
.user_subdev_api
= fmd
->user_subdev_api
;
633 v4l2_err(&fmd
->v4l2_dev
, "Failed to register FIMC.%d (%d)\n",
639 static int register_csis_entity(struct fimc_md
*fmd
,
640 struct platform_device
*pdev
,
641 struct v4l2_subdev
*sd
)
643 struct device_node
*node
= pdev
->dev
.of_node
;
646 id
= node
? __of_get_csis_id(node
) : max(0, pdev
->id
);
648 if (WARN_ON(id
< 0 || id
>= CSIS_MAX_ENTITIES
))
651 if (WARN_ON(fmd
->csis
[id
].sd
))
654 sd
->grp_id
= GRP_ID_CSIS
;
655 ret
= v4l2_device_register_subdev(&fmd
->v4l2_dev
, sd
);
657 fmd
->csis
[id
].sd
= sd
;
659 v4l2_err(&fmd
->v4l2_dev
,
660 "Failed to register MIPI-CSIS.%d (%d)\n", id
, ret
);
664 static int register_fimc_is_entity(struct fimc_md
*fmd
, struct fimc_is
*is
)
666 struct v4l2_subdev
*sd
= &is
->isp
.subdev
;
669 ret
= v4l2_device_register_subdev(&fmd
->v4l2_dev
, sd
);
671 v4l2_err(&fmd
->v4l2_dev
,
672 "Failed to register FIMC-ISP (%d)\n", ret
);
680 static int fimc_md_register_platform_entity(struct fimc_md
*fmd
,
681 struct platform_device
*pdev
,
684 struct device
*dev
= &pdev
->dev
;
685 int ret
= -EPROBE_DEFER
;
688 /* Lock to ensure dev->driver won't change. */
691 if (!dev
->driver
|| !try_module_get(dev
->driver
->owner
))
694 drvdata
= dev_get_drvdata(dev
);
695 /* Some subdev didn't probe succesfully id drvdata is NULL */
697 switch (plat_entity
) {
699 ret
= register_fimc_entity(fmd
, drvdata
);
702 ret
= register_fimc_lite_entity(fmd
, drvdata
);
705 ret
= register_csis_entity(fmd
, pdev
, drvdata
);
708 ret
= register_fimc_is_entity(fmd
, drvdata
);
715 module_put(dev
->driver
->owner
);
718 if (ret
== -EPROBE_DEFER
)
719 dev_info(&fmd
->pdev
->dev
, "deferring %s device registration\n",
722 dev_err(&fmd
->pdev
->dev
, "%s device registration failed (%d)\n",
727 static int fimc_md_pdev_match(struct device
*dev
, void *data
)
729 struct platform_device
*pdev
= to_platform_device(dev
);
730 int plat_entity
= -1;
734 if (!get_device(dev
))
737 if (!strcmp(pdev
->name
, CSIS_DRIVER_NAME
)) {
738 plat_entity
= IDX_CSIS
;
739 } else if (!strcmp(pdev
->name
, FIMC_LITE_DRV_NAME
)) {
740 plat_entity
= IDX_FLITE
;
742 p
= strstr(pdev
->name
, "fimc");
743 if (p
&& *(p
+ 4) == 0)
744 plat_entity
= IDX_FIMC
;
747 if (plat_entity
>= 0)
748 ret
= fimc_md_register_platform_entity(data
, pdev
,
754 /* Register FIMC, FIMC-LITE and CSIS media entities */
756 static int fimc_md_register_of_platform_entities(struct fimc_md
*fmd
,
757 struct device_node
*parent
)
759 struct device_node
*node
;
762 for_each_available_child_of_node(parent
, node
) {
763 struct platform_device
*pdev
;
764 int plat_entity
= -1;
766 pdev
= of_find_device_by_node(node
);
770 /* If driver of any entity isn't ready try all again later. */
771 if (!strcmp(node
->name
, CSIS_OF_NODE_NAME
))
772 plat_entity
= IDX_CSIS
;
773 else if (!strcmp(node
->name
, FIMC_IS_OF_NODE_NAME
))
774 plat_entity
= IDX_IS_ISP
;
775 else if (!strcmp(node
->name
, FIMC_LITE_OF_NODE_NAME
))
776 plat_entity
= IDX_FLITE
;
777 else if (!strcmp(node
->name
, FIMC_OF_NODE_NAME
) &&
778 !of_property_read_bool(node
, "samsung,lcd-wb"))
779 plat_entity
= IDX_FIMC
;
781 if (plat_entity
>= 0)
782 ret
= fimc_md_register_platform_entity(fmd
, pdev
,
784 put_device(&pdev
->dev
);
792 #define fimc_md_register_of_platform_entities(fmd, node) (-ENOSYS)
795 static void fimc_md_unregister_entities(struct fimc_md
*fmd
)
799 for (i
= 0; i
< FIMC_MAX_DEVS
; i
++) {
800 if (fmd
->fimc
[i
] == NULL
)
802 v4l2_device_unregister_subdev(&fmd
->fimc
[i
]->vid_cap
.subdev
);
803 fmd
->fimc
[i
]->pipeline_ops
= NULL
;
806 for (i
= 0; i
< FIMC_LITE_MAX_DEVS
; i
++) {
807 if (fmd
->fimc_lite
[i
] == NULL
)
809 v4l2_device_unregister_subdev(&fmd
->fimc_lite
[i
]->subdev
);
810 fmd
->fimc_lite
[i
]->pipeline_ops
= NULL
;
811 fmd
->fimc_lite
[i
] = NULL
;
813 for (i
= 0; i
< CSIS_MAX_ENTITIES
; i
++) {
814 if (fmd
->csis
[i
].sd
== NULL
)
816 v4l2_device_unregister_subdev(fmd
->csis
[i
].sd
);
817 fmd
->csis
[i
].sd
= NULL
;
819 for (i
= 0; i
< fmd
->num_sensors
; i
++) {
820 if (fmd
->sensor
[i
].subdev
== NULL
)
822 fimc_md_unregister_sensor(fmd
->sensor
[i
].subdev
);
823 fmd
->sensor
[i
].subdev
= NULL
;
827 v4l2_device_unregister_subdev(&fmd
->fimc_is
->isp
.subdev
);
829 v4l2_info(&fmd
->v4l2_dev
, "Unregistered all entities\n");
833 * __fimc_md_create_fimc_links - create links to all FIMC entities
834 * @fmd: fimc media device
835 * @source: the source entity to create links to all fimc entities from
836 * @sensor: sensor subdev linked to FIMC[fimc_id] entity, may be null
837 * @pad: the source entity pad index
838 * @link_mask: bitmask of the fimc devices for which link should be enabled
840 static int __fimc_md_create_fimc_sink_links(struct fimc_md
*fmd
,
841 struct media_entity
*source
,
842 struct v4l2_subdev
*sensor
,
843 int pad
, int link_mask
)
845 struct fimc_source_info
*si
= NULL
;
846 struct media_entity
*sink
;
847 unsigned int flags
= 0;
851 si
= v4l2_get_subdev_hostdata(sensor
);
852 /* Skip direct FIMC links in the logical FIMC-IS sensor path */
853 if (si
&& si
->fimc_bus_type
== FIMC_BUS_TYPE_ISP_WRITEBACK
)
857 for (i
= 0; !ret
&& i
< FIMC_MAX_DEVS
; i
++) {
861 * Some FIMC variants are not fitted with camera capture
862 * interface. Skip creating a link from sensor for those.
864 if (!fmd
->fimc
[i
]->variant
->has_cam_if
)
867 flags
= ((1 << i
) & link_mask
) ? MEDIA_LNK_FL_ENABLED
: 0;
869 sink
= &fmd
->fimc
[i
]->vid_cap
.subdev
.entity
;
870 ret
= media_entity_create_link(source
, pad
, sink
,
871 FIMC_SD_PAD_SINK_CAM
, flags
);
875 /* Notify FIMC capture subdev entity */
876 ret
= media_entity_call(sink
, link_setup
, &sink
->pads
[0],
877 &source
->pads
[pad
], flags
);
881 v4l2_info(&fmd
->v4l2_dev
, "created link [%s] %c> [%s]\n",
882 source
->name
, flags
? '=' : '-', sink
->name
);
884 if (flags
== 0 || sensor
== NULL
)
887 if (!WARN_ON(si
== NULL
)) {
888 unsigned long irq_flags
;
889 struct fimc_sensor_info
*inf
= source_to_sensor_info(si
);
891 spin_lock_irqsave(&fmd
->slock
, irq_flags
);
892 inf
->host
= fmd
->fimc
[i
];
893 spin_unlock_irqrestore(&fmd
->slock
, irq_flags
);
897 for (i
= 0; i
< FIMC_LITE_MAX_DEVS
; i
++) {
898 if (!fmd
->fimc_lite
[i
])
901 sink
= &fmd
->fimc_lite
[i
]->subdev
.entity
;
902 ret
= media_entity_create_link(source
, pad
, sink
,
903 FLITE_SD_PAD_SINK
, 0);
907 /* Notify FIMC-LITE subdev entity */
908 ret
= media_entity_call(sink
, link_setup
, &sink
->pads
[0],
909 &source
->pads
[pad
], 0);
913 v4l2_info(&fmd
->v4l2_dev
, "created link [%s] -> [%s]\n",
914 source
->name
, sink
->name
);
919 /* Create links from FIMC-LITE source pads to other entities */
920 static int __fimc_md_create_flite_source_links(struct fimc_md
*fmd
)
922 struct media_entity
*source
, *sink
;
925 for (i
= 0; i
< FIMC_LITE_MAX_DEVS
; i
++) {
926 struct fimc_lite
*fimc
= fmd
->fimc_lite
[i
];
931 source
= &fimc
->subdev
.entity
;
932 sink
= &fimc
->vfd
.entity
;
933 /* FIMC-LITE's subdev and video node */
934 ret
= media_entity_create_link(source
, FLITE_SD_PAD_SOURCE_DMA
,
938 /* Link from FIMC-LITE to IS-ISP subdev */
939 sink
= &fmd
->fimc_is
->isp
.subdev
.entity
;
940 ret
= media_entity_create_link(source
, FLITE_SD_PAD_SOURCE_ISP
,
949 /* Create FIMC-IS links */
950 static int __fimc_md_create_fimc_is_links(struct fimc_md
*fmd
)
952 struct media_entity
*source
, *sink
;
955 source
= &fmd
->fimc_is
->isp
.subdev
.entity
;
957 for (i
= 0; i
< FIMC_MAX_DEVS
; i
++) {
958 if (fmd
->fimc
[i
] == NULL
)
961 /* Link from IS-ISP subdev to FIMC */
962 sink
= &fmd
->fimc
[i
]->vid_cap
.subdev
.entity
;
963 ret
= media_entity_create_link(source
, FIMC_ISP_SD_PAD_SRC_FIFO
,
964 sink
, FIMC_SD_PAD_SINK_FIFO
, 0);
973 * fimc_md_create_links - create default links between registered entities
975 * Parallel interface sensor entities are connected directly to FIMC capture
976 * entities. The sensors using MIPI CSIS bus are connected through immutable
977 * link with CSI receiver entity specified by mux_id. Any registered CSIS
978 * entity has a link to each registered FIMC capture entity. Enabled links
979 * are created by default between each subsequent registered sensor and
980 * subsequent FIMC capture entity. The number of default active links is
981 * determined by the number of available sensors or FIMC entities,
984 static int fimc_md_create_links(struct fimc_md
*fmd
)
986 struct v4l2_subdev
*csi_sensors
[CSIS_MAX_ENTITIES
] = { NULL
};
987 struct v4l2_subdev
*sensor
, *csis
;
988 struct fimc_source_info
*pdata
;
989 struct media_entity
*source
, *sink
;
990 int i
, pad
, fimc_id
= 0, ret
= 0;
991 u32 flags
, link_mask
= 0;
993 for (i
= 0; i
< fmd
->num_sensors
; i
++) {
994 if (fmd
->sensor
[i
].subdev
== NULL
)
997 sensor
= fmd
->sensor
[i
].subdev
;
998 pdata
= v4l2_get_subdev_hostdata(sensor
);
1004 switch (pdata
->sensor_bus_type
) {
1005 case FIMC_BUS_TYPE_MIPI_CSI2
:
1006 if (WARN(pdata
->mux_id
>= CSIS_MAX_ENTITIES
,
1007 "Wrong CSI channel id: %d\n", pdata
->mux_id
))
1010 csis
= fmd
->csis
[pdata
->mux_id
].sd
;
1011 if (WARN(csis
== NULL
,
1012 "MIPI-CSI interface specified "
1013 "but s5p-csis module is not loaded!\n"))
1016 pad
= sensor
->entity
.num_pads
- 1;
1017 ret
= media_entity_create_link(&sensor
->entity
, pad
,
1018 &csis
->entity
, CSIS_PAD_SINK
,
1019 MEDIA_LNK_FL_IMMUTABLE
|
1020 MEDIA_LNK_FL_ENABLED
);
1024 v4l2_info(&fmd
->v4l2_dev
, "created link [%s] => [%s]\n",
1025 sensor
->entity
.name
, csis
->entity
.name
);
1028 csi_sensors
[pdata
->mux_id
] = sensor
;
1031 case FIMC_BUS_TYPE_ITU_601
...FIMC_BUS_TYPE_ITU_656
:
1032 source
= &sensor
->entity
;
1037 v4l2_err(&fmd
->v4l2_dev
, "Wrong bus_type: %x\n",
1038 pdata
->sensor_bus_type
);
1044 link_mask
= 1 << fimc_id
++;
1045 ret
= __fimc_md_create_fimc_sink_links(fmd
, source
, sensor
,
1049 for (i
= 0; i
< CSIS_MAX_ENTITIES
; i
++) {
1050 if (fmd
->csis
[i
].sd
== NULL
)
1053 source
= &fmd
->csis
[i
].sd
->entity
;
1054 pad
= CSIS_PAD_SOURCE
;
1055 sensor
= csi_sensors
[i
];
1057 link_mask
= 1 << fimc_id
++;
1058 ret
= __fimc_md_create_fimc_sink_links(fmd
, source
, sensor
,
1062 /* Create immutable links between each FIMC's subdev and video node */
1063 flags
= MEDIA_LNK_FL_IMMUTABLE
| MEDIA_LNK_FL_ENABLED
;
1064 for (i
= 0; i
< FIMC_MAX_DEVS
; i
++) {
1068 source
= &fmd
->fimc
[i
]->vid_cap
.subdev
.entity
;
1069 sink
= &fmd
->fimc
[i
]->vid_cap
.vfd
.entity
;
1071 ret
= media_entity_create_link(source
, FIMC_SD_PAD_SOURCE
,
1077 ret
= __fimc_md_create_flite_source_links(fmd
);
1082 ret
= __fimc_md_create_fimc_is_links(fmd
);
1088 * The peripheral sensor and CAM_BLK (PIXELASYNCMx) clocks management.
1090 static void fimc_md_put_clocks(struct fimc_md
*fmd
)
1092 int i
= FIMC_MAX_CAMCLKS
;
1095 if (IS_ERR(fmd
->camclk
[i
].clock
))
1097 clk_unprepare(fmd
->camclk
[i
].clock
);
1098 clk_put(fmd
->camclk
[i
].clock
);
1099 fmd
->camclk
[i
].clock
= ERR_PTR(-EINVAL
);
1102 /* Writeback (PIXELASYNCMx) clocks */
1103 for (i
= 0; i
< FIMC_MAX_WBCLKS
; i
++) {
1104 if (IS_ERR(fmd
->wbclk
[i
]))
1106 clk_put(fmd
->wbclk
[i
]);
1107 fmd
->wbclk
[i
] = ERR_PTR(-EINVAL
);
1111 static int fimc_md_get_clocks(struct fimc_md
*fmd
)
1113 struct device
*dev
= NULL
;
1118 for (i
= 0; i
< FIMC_MAX_CAMCLKS
; i
++)
1119 fmd
->camclk
[i
].clock
= ERR_PTR(-EINVAL
);
1121 if (fmd
->pdev
->dev
.of_node
)
1122 dev
= &fmd
->pdev
->dev
;
1124 for (i
= 0; i
< FIMC_MAX_CAMCLKS
; i
++) {
1125 snprintf(clk_name
, sizeof(clk_name
), "sclk_cam%u", i
);
1126 clock
= clk_get(dev
, clk_name
);
1128 if (IS_ERR(clock
)) {
1129 dev_err(&fmd
->pdev
->dev
, "Failed to get clock: %s\n",
1131 ret
= PTR_ERR(clock
);
1134 ret
= clk_prepare(clock
);
1137 fmd
->camclk
[i
].clock
= ERR_PTR(-EINVAL
);
1140 fmd
->camclk
[i
].clock
= clock
;
1143 fimc_md_put_clocks(fmd
);
1148 * For now get only PIXELASYNCM1 clock (Writeback B/ISP),
1149 * leave PIXELASYNCM0 out for the LCD Writeback driver.
1151 fmd
->wbclk
[CLK_IDX_WB_A
] = ERR_PTR(-EINVAL
);
1153 for (i
= CLK_IDX_WB_B
; i
< FIMC_MAX_WBCLKS
; i
++) {
1154 snprintf(clk_name
, sizeof(clk_name
), "pxl_async%u", i
);
1155 clock
= clk_get(dev
, clk_name
);
1156 if (IS_ERR(clock
)) {
1157 v4l2_err(&fmd
->v4l2_dev
, "Failed to get clock: %s\n",
1159 ret
= PTR_ERR(clock
);
1162 fmd
->wbclk
[i
] = clock
;
1165 fimc_md_put_clocks(fmd
);
1170 static int __fimc_md_set_camclk(struct fimc_md
*fmd
,
1171 struct fimc_source_info
*si
,
1174 struct fimc_camclk_info
*camclk
;
1177 if (WARN_ON(si
->clk_id
>= FIMC_MAX_CAMCLKS
) || !fmd
|| !fmd
->pmf
)
1180 camclk
= &fmd
->camclk
[si
->clk_id
];
1182 dbg("camclk %d, f: %lu, use_count: %d, on: %d",
1183 si
->clk_id
, si
->clk_frequency
, camclk
->use_count
, on
);
1186 if (camclk
->use_count
> 0 &&
1187 camclk
->frequency
!= si
->clk_frequency
)
1190 if (camclk
->use_count
++ == 0) {
1191 clk_set_rate(camclk
->clock
, si
->clk_frequency
);
1192 camclk
->frequency
= si
->clk_frequency
;
1193 ret
= pm_runtime_get_sync(fmd
->pmf
);
1196 ret
= clk_enable(camclk
->clock
);
1197 dbg("Enabled camclk %d: f: %lu", si
->clk_id
,
1198 clk_get_rate(camclk
->clock
));
1203 if (WARN_ON(camclk
->use_count
== 0))
1206 if (--camclk
->use_count
== 0) {
1207 clk_disable(camclk
->clock
);
1208 pm_runtime_put(fmd
->pmf
);
1209 dbg("Disabled camclk %d", si
->clk_id
);
1215 * fimc_md_set_camclk - peripheral sensor clock setup
1216 * @sd: sensor subdev to configure sclk_cam clock for
1217 * @on: 1 to enable or 0 to disable the clock
1219 * There are 2 separate clock outputs available in the SoC for external
1220 * image processors. These clocks are shared between all registered FIMC
1221 * devices to which sensors can be attached, either directly or through
1222 * the MIPI CSI receiver. The clock is allowed here to be used by
1223 * multiple sensors concurrently if they use same frequency.
1224 * This function should only be called when the graph mutex is held.
1226 int fimc_md_set_camclk(struct v4l2_subdev
*sd
, bool on
)
1228 struct fimc_source_info
*si
= v4l2_get_subdev_hostdata(sd
);
1229 struct fimc_md
*fmd
= entity_to_fimc_mdev(&sd
->entity
);
1231 return __fimc_md_set_camclk(fmd
, si
, on
);
1234 static int fimc_md_link_notify(struct media_pad
*source
,
1235 struct media_pad
*sink
, u32 flags
)
1237 struct fimc_lite
*fimc_lite
= NULL
;
1238 struct fimc_dev
*fimc
= NULL
;
1239 struct fimc_pipeline
*pipeline
;
1240 struct v4l2_subdev
*sd
;
1245 if (media_entity_type(sink
->entity
) != MEDIA_ENT_T_V4L2_SUBDEV
)
1248 sd
= media_entity_to_v4l2_subdev(sink
->entity
);
1250 switch (sd
->grp_id
) {
1252 fimc_lite
= v4l2_get_subdevdata(sd
);
1253 if (WARN_ON(fimc_lite
== NULL
))
1255 pipeline
= &fimc_lite
->pipeline
;
1256 lock
= &fimc_lite
->lock
;
1259 fimc
= v4l2_get_subdevdata(sd
);
1260 if (WARN_ON(fimc
== NULL
))
1262 pipeline
= &fimc
->pipeline
;
1270 ref_count
= fimc
? fimc
->vid_cap
.refcnt
: fimc_lite
->ref_count
;
1272 if (!(flags
& MEDIA_LNK_FL_ENABLED
)) {
1273 if (ref_count
> 0) {
1274 ret
= __fimc_pipeline_close(pipeline
);
1276 fimc_ctrls_delete(fimc
->vid_cap
.ctx
);
1278 for (i
= 0; i
< IDX_MAX
; i
++)
1279 pipeline
->subdevs
[i
] = NULL
;
1280 } else if (ref_count
> 0) {
1282 * Link activation. Enable power of pipeline elements only if
1283 * the pipeline is already in use, i.e. its video node is open.
1284 * Recreate the controls destroyed during the link deactivation.
1286 ret
= __fimc_pipeline_open(pipeline
,
1287 source
->entity
, true);
1289 ret
= fimc_capture_ctrls_create(fimc
);
1293 return ret
? -EPIPE
: ret
;
1296 static ssize_t
fimc_md_sysfs_show(struct device
*dev
,
1297 struct device_attribute
*attr
, char *buf
)
1299 struct platform_device
*pdev
= to_platform_device(dev
);
1300 struct fimc_md
*fmd
= platform_get_drvdata(pdev
);
1302 if (fmd
->user_subdev_api
)
1303 return strlcpy(buf
, "Sub-device API (sub-dev)\n", PAGE_SIZE
);
1305 return strlcpy(buf
, "V4L2 video node only API (vid-dev)\n", PAGE_SIZE
);
1308 static ssize_t
fimc_md_sysfs_store(struct device
*dev
,
1309 struct device_attribute
*attr
,
1310 const char *buf
, size_t count
)
1312 struct platform_device
*pdev
= to_platform_device(dev
);
1313 struct fimc_md
*fmd
= platform_get_drvdata(pdev
);
1317 if (!strcmp(buf
, "vid-dev\n"))
1319 else if (!strcmp(buf
, "sub-dev\n"))
1324 fmd
->user_subdev_api
= subdev_api
;
1325 for (i
= 0; i
< FIMC_MAX_DEVS
; i
++)
1327 fmd
->fimc
[i
]->vid_cap
.user_subdev_api
= subdev_api
;
1331 * This device attribute is to select video pipeline configuration method.
1332 * There are following valid values:
1333 * vid-dev - for V4L2 video node API only, subdevice will be configured
1334 * by the host driver.
1335 * sub-dev - for media controller API, subdevs must be configured in user
1336 * space before starting streaming.
1338 static DEVICE_ATTR(subdev_conf_mode
, S_IWUSR
| S_IRUGO
,
1339 fimc_md_sysfs_show
, fimc_md_sysfs_store
);
1341 static int fimc_md_get_pinctrl(struct fimc_md
*fmd
)
1343 struct device
*dev
= &fmd
->pdev
->dev
;
1344 struct fimc_pinctrl
*pctl
= &fmd
->pinctl
;
1346 pctl
->pinctrl
= devm_pinctrl_get(dev
);
1347 if (IS_ERR(pctl
->pinctrl
))
1348 return PTR_ERR(pctl
->pinctrl
);
1350 pctl
->state_default
= pinctrl_lookup_state(pctl
->pinctrl
,
1351 PINCTRL_STATE_DEFAULT
);
1352 if (IS_ERR(pctl
->state_default
))
1353 return PTR_ERR(pctl
->state_default
);
1355 pctl
->state_idle
= pinctrl_lookup_state(pctl
->pinctrl
,
1356 PINCTRL_STATE_IDLE
);
1360 static int fimc_md_probe(struct platform_device
*pdev
)
1362 struct device
*dev
= &pdev
->dev
;
1363 struct v4l2_device
*v4l2_dev
;
1364 struct fimc_md
*fmd
;
1367 fmd
= devm_kzalloc(dev
, sizeof(*fmd
), GFP_KERNEL
);
1371 spin_lock_init(&fmd
->slock
);
1374 strlcpy(fmd
->media_dev
.model
, "SAMSUNG S5P FIMC",
1375 sizeof(fmd
->media_dev
.model
));
1376 fmd
->media_dev
.link_notify
= fimc_md_link_notify
;
1377 fmd
->media_dev
.dev
= dev
;
1379 v4l2_dev
= &fmd
->v4l2_dev
;
1380 v4l2_dev
->mdev
= &fmd
->media_dev
;
1381 v4l2_dev
->notify
= fimc_sensor_notify
;
1382 strlcpy(v4l2_dev
->name
, "s5p-fimc-md", sizeof(v4l2_dev
->name
));
1384 fmd
->use_isp
= fimc_md_is_isp_available(dev
->of_node
);
1386 ret
= v4l2_device_register(dev
, &fmd
->v4l2_dev
);
1388 v4l2_err(v4l2_dev
, "Failed to register v4l2_device: %d\n", ret
);
1391 ret
= media_device_register(&fmd
->media_dev
);
1393 v4l2_err(v4l2_dev
, "Failed to register media device: %d\n", ret
);
1396 ret
= fimc_md_get_clocks(fmd
);
1400 fmd
->user_subdev_api
= (dev
->of_node
!= NULL
);
1402 /* Protect the media graph while we're registering entities */
1403 mutex_lock(&fmd
->media_dev
.graph_mutex
);
1405 ret
= fimc_md_get_pinctrl(fmd
);
1407 if (ret
!= EPROBE_DEFER
)
1408 dev_err(dev
, "Failed to get pinctrl: %d\n", ret
);
1413 ret
= fimc_md_register_of_platform_entities(fmd
, dev
->of_node
);
1415 ret
= bus_for_each_dev(&platform_bus_type
, NULL
, fmd
,
1416 fimc_md_pdev_match
);
1420 if (dev
->platform_data
|| dev
->of_node
) {
1421 ret
= fimc_md_register_sensor_entities(fmd
);
1426 ret
= fimc_md_create_links(fmd
);
1429 ret
= v4l2_device_register_subdev_nodes(&fmd
->v4l2_dev
);
1433 ret
= device_create_file(&pdev
->dev
, &dev_attr_subdev_conf_mode
);
1437 platform_set_drvdata(pdev
, fmd
);
1438 mutex_unlock(&fmd
->media_dev
.graph_mutex
);
1442 mutex_unlock(&fmd
->media_dev
.graph_mutex
);
1444 media_device_unregister(&fmd
->media_dev
);
1445 fimc_md_put_clocks(fmd
);
1446 fimc_md_unregister_entities(fmd
);
1448 v4l2_device_unregister(&fmd
->v4l2_dev
);
1452 static int fimc_md_remove(struct platform_device
*pdev
)
1454 struct fimc_md
*fmd
= platform_get_drvdata(pdev
);
1458 device_remove_file(&pdev
->dev
, &dev_attr_subdev_conf_mode
);
1459 fimc_md_unregister_entities(fmd
);
1460 media_device_unregister(&fmd
->media_dev
);
1461 fimc_md_put_clocks(fmd
);
1465 static struct platform_device_id fimc_driver_ids
[] __always_unused
= {
1466 { .name
= "s5p-fimc-md" },
1469 MODULE_DEVICE_TABLE(platform
, fimc_driver_ids
);
1471 static const struct of_device_id fimc_md_of_match
[] = {
1472 { .compatible
= "samsung,fimc" },
1475 MODULE_DEVICE_TABLE(of
, fimc_md_of_match
);
1477 static struct platform_driver fimc_md_driver
= {
1478 .probe
= fimc_md_probe
,
1479 .remove
= fimc_md_remove
,
1481 .of_match_table
= of_match_ptr(fimc_md_of_match
),
1482 .name
= "s5p-fimc-md",
1483 .owner
= THIS_MODULE
,
1487 static int __init
fimc_md_init(void)
1491 request_module("s5p-csis");
1492 ret
= fimc_register_driver();
1496 return platform_driver_register(&fimc_md_driver
);
1499 static void __exit
fimc_md_exit(void)
1501 platform_driver_unregister(&fimc_md_driver
);
1502 fimc_unregister_driver();
1505 module_init(fimc_md_init
);
1506 module_exit(fimc_md_exit
);
1508 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
1509 MODULE_DESCRIPTION("S5P FIMC camera host interface/video postprocessor driver");
1510 MODULE_LICENSE("GPL");
1511 MODULE_VERSION("2.0.1");