1 // SPDX-License-Identifier: GPL-2.0
5 * Qualcomm MSM Camera Subsystem - Core
7 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
8 * Copyright (C) 2015-2018 Linaro Ltd.
10 #include <linux/clk.h>
11 #include <linux/interconnect.h>
12 #include <linux/media-bus-format.h>
13 #include <linux/media.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
17 #include <linux/of_graph.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/pm_domain.h>
20 #include <linux/slab.h>
21 #include <linux/videodev2.h>
23 #include <media/media-device.h>
24 #include <media/v4l2-async.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-mc.h>
27 #include <media/v4l2-fwnode.h>
31 #define CAMSS_CLOCK_MARGIN_NUMERATOR 105
32 #define CAMSS_CLOCK_MARGIN_DENOMINATOR 100
34 static const struct resources csiphy_res_8x16[] = {
38 .clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy0_timer" },
39 .clock_rate = { { 0 },
42 { 100000000, 200000000 } },
43 .reg = { "csiphy0", "csiphy0_clk_mux" },
44 .interrupt = { "csiphy0" }
50 .clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy1_timer" },
51 .clock_rate = { { 0 },
54 { 100000000, 200000000 } },
55 .reg = { "csiphy1", "csiphy1_clk_mux" },
56 .interrupt = { "csiphy1" }
60 static const struct resources csid_res_8x16[] = {
63 .regulators = { "vdda" },
64 .clock = { "top_ahb", "ispif_ahb", "csi0_ahb", "ahb",
65 "csi0", "csi0_phy", "csi0_pix", "csi0_rdi" },
66 .clock_rate = { { 0 },
70 { 100000000, 200000000 },
75 .interrupt = { "csid0" }
80 .regulators = { "vdda" },
81 .clock = { "top_ahb", "ispif_ahb", "csi1_ahb", "ahb",
82 "csi1", "csi1_phy", "csi1_pix", "csi1_rdi" },
83 .clock_rate = { { 0 },
87 { 100000000, 200000000 },
92 .interrupt = { "csid1" }
96 static const struct resources_ispif ispif_res_8x16 = {
98 .clock = { "top_ahb", "ahb", "ispif_ahb",
99 "csi0", "csi0_pix", "csi0_rdi",
100 "csi1", "csi1_pix", "csi1_rdi" },
101 .clock_for_reset = { "vfe0", "csi_vfe0" },
102 .reg = { "ispif", "csi_clk_mux" },
107 static const struct resources vfe_res_8x16[] = {
111 .clock = { "top_ahb", "vfe0", "csi_vfe0",
112 "vfe_ahb", "vfe_axi", "ahb" },
113 .clock_rate = { { 0 },
114 { 50000000, 80000000, 100000000, 160000000,
115 177780000, 200000000, 266670000, 320000000,
116 400000000, 465000000 },
125 .interrupt = { "vfe0" }
129 static const struct resources csiphy_res_8x96[] = {
133 .clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy0_timer" },
134 .clock_rate = { { 0 },
137 { 100000000, 200000000, 266666667 } },
138 .reg = { "csiphy0", "csiphy0_clk_mux" },
139 .interrupt = { "csiphy0" }
145 .clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy1_timer" },
146 .clock_rate = { { 0 },
149 { 100000000, 200000000, 266666667 } },
150 .reg = { "csiphy1", "csiphy1_clk_mux" },
151 .interrupt = { "csiphy1" }
157 .clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy2_timer" },
158 .clock_rate = { { 0 },
161 { 100000000, 200000000, 266666667 } },
162 .reg = { "csiphy2", "csiphy2_clk_mux" },
163 .interrupt = { "csiphy2" }
167 static const struct resources csid_res_8x96[] = {
170 .regulators = { "vdda" },
171 .clock = { "top_ahb", "ispif_ahb", "csi0_ahb", "ahb",
172 "csi0", "csi0_phy", "csi0_pix", "csi0_rdi" },
173 .clock_rate = { { 0 },
177 { 100000000, 200000000, 266666667 },
182 .interrupt = { "csid0" }
187 .regulators = { "vdda" },
188 .clock = { "top_ahb", "ispif_ahb", "csi1_ahb", "ahb",
189 "csi1", "csi1_phy", "csi1_pix", "csi1_rdi" },
190 .clock_rate = { { 0 },
194 { 100000000, 200000000, 266666667 },
199 .interrupt = { "csid1" }
204 .regulators = { "vdda" },
205 .clock = { "top_ahb", "ispif_ahb", "csi2_ahb", "ahb",
206 "csi2", "csi2_phy", "csi2_pix", "csi2_rdi" },
207 .clock_rate = { { 0 },
211 { 100000000, 200000000, 266666667 },
216 .interrupt = { "csid2" }
221 .regulators = { "vdda" },
222 .clock = { "top_ahb", "ispif_ahb", "csi3_ahb", "ahb",
223 "csi3", "csi3_phy", "csi3_pix", "csi3_rdi" },
224 .clock_rate = { { 0 },
228 { 100000000, 200000000, 266666667 },
233 .interrupt = { "csid3" }
237 static const struct resources_ispif ispif_res_8x96 = {
239 .clock = { "top_ahb", "ahb", "ispif_ahb",
240 "csi0", "csi0_pix", "csi0_rdi",
241 "csi1", "csi1_pix", "csi1_rdi",
242 "csi2", "csi2_pix", "csi2_rdi",
243 "csi3", "csi3_pix", "csi3_rdi" },
244 .clock_for_reset = { "vfe0", "csi_vfe0", "vfe1", "csi_vfe1" },
245 .reg = { "ispif", "csi_clk_mux" },
249 static const struct resources vfe_res_8x96[] = {
253 .clock = { "top_ahb", "ahb", "vfe0", "csi_vfe0", "vfe_ahb",
254 "vfe0_ahb", "vfe_axi", "vfe0_stream"},
255 .clock_rate = { { 0 },
257 { 75000000, 100000000, 300000000,
258 320000000, 480000000, 600000000 },
265 .interrupt = { "vfe0" }
271 .clock = { "top_ahb", "ahb", "vfe1", "csi_vfe1", "vfe_ahb",
272 "vfe1_ahb", "vfe_axi", "vfe1_stream"},
273 .clock_rate = { { 0 },
275 { 75000000, 100000000, 300000000,
276 320000000, 480000000, 600000000 },
283 .interrupt = { "vfe1" }
287 static const struct resources csiphy_res_660[] = {
291 .clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy0_timer",
292 "csi0_phy", "csiphy_ahb2crif" },
293 .clock_rate = { { 0 },
296 { 100000000, 200000000, 269333333 },
298 .reg = { "csiphy0", "csiphy0_clk_mux" },
299 .interrupt = { "csiphy0" }
305 .clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy1_timer",
306 "csi1_phy", "csiphy_ahb2crif" },
307 .clock_rate = { { 0 },
310 { 100000000, 200000000, 269333333 },
312 .reg = { "csiphy1", "csiphy1_clk_mux" },
313 .interrupt = { "csiphy1" }
319 .clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy2_timer",
320 "csi2_phy", "csiphy_ahb2crif" },
321 .clock_rate = { { 0 },
324 { 100000000, 200000000, 269333333 },
326 .reg = { "csiphy2", "csiphy2_clk_mux" },
327 .interrupt = { "csiphy2" }
331 static const struct resources csid_res_660[] = {
334 .regulators = { "vdda", "vdd_sec" },
335 .clock = { "top_ahb", "ispif_ahb", "csi0_ahb", "ahb",
336 "csi0", "csi0_phy", "csi0_pix", "csi0_rdi",
338 .clock_rate = { { 0 },
342 { 100000000, 200000000, 310000000,
343 404000000, 465000000 },
349 .interrupt = { "csid0" }
354 .regulators = { "vdda", "vdd_sec" },
355 .clock = { "top_ahb", "ispif_ahb", "csi1_ahb", "ahb",
356 "csi1", "csi1_phy", "csi1_pix", "csi1_rdi",
358 .clock_rate = { { 0 },
362 { 100000000, 200000000, 310000000,
363 404000000, 465000000 },
369 .interrupt = { "csid1" }
374 .regulators = { "vdda", "vdd_sec" },
375 .clock = { "top_ahb", "ispif_ahb", "csi2_ahb", "ahb",
376 "csi2", "csi2_phy", "csi2_pix", "csi2_rdi",
378 .clock_rate = { { 0 },
382 { 100000000, 200000000, 310000000,
383 404000000, 465000000 },
389 .interrupt = { "csid2" }
394 .regulators = { "vdda", "vdd_sec" },
395 .clock = { "top_ahb", "ispif_ahb", "csi3_ahb", "ahb",
396 "csi3", "csi3_phy", "csi3_pix", "csi3_rdi",
398 .clock_rate = { { 0 },
402 { 100000000, 200000000, 310000000,
403 404000000, 465000000 },
409 .interrupt = { "csid3" }
413 static const struct resources_ispif ispif_res_660 = {
415 .clock = { "top_ahb", "ahb", "ispif_ahb",
416 "csi0", "csi0_pix", "csi0_rdi",
417 "csi1", "csi1_pix", "csi1_rdi",
418 "csi2", "csi2_pix", "csi2_rdi",
419 "csi3", "csi3_pix", "csi3_rdi" },
420 .clock_for_reset = { "vfe0", "csi_vfe0", "vfe1", "csi_vfe1" },
421 .reg = { "ispif", "csi_clk_mux" },
425 static const struct resources vfe_res_660[] = {
429 .clock = { "throttle_axi", "top_ahb", "ahb", "vfe0",
430 "csi_vfe0", "vfe_ahb", "vfe0_ahb", "vfe_axi",
432 .clock_rate = { { 0 },
435 { 120000000, 200000000, 256000000,
436 300000000, 404000000, 480000000,
437 540000000, 576000000 },
444 .interrupt = { "vfe0" }
450 .clock = { "throttle_axi", "top_ahb", "ahb", "vfe1",
451 "csi_vfe1", "vfe_ahb", "vfe1_ahb", "vfe_axi",
453 .clock_rate = { { 0 },
456 { 120000000, 200000000, 256000000,
457 300000000, 404000000, 480000000,
458 540000000, 576000000 },
465 .interrupt = { "vfe1" }
469 static const struct resources csiphy_res_845[] = {
473 .clock = { "camnoc_axi", "soc_ahb", "slow_ahb_src",
474 "cpas_ahb", "cphy_rx_src", "csiphy0",
475 "csiphy0_timer_src", "csiphy0_timer" },
476 .clock_rate = { { 0 },
483 { 19200000, 240000000, 269333333 } },
484 .reg = { "csiphy0" },
485 .interrupt = { "csiphy0" }
491 .clock = { "camnoc_axi", "soc_ahb", "slow_ahb_src",
492 "cpas_ahb", "cphy_rx_src", "csiphy1",
493 "csiphy1_timer_src", "csiphy1_timer" },
494 .clock_rate = { { 0 },
501 { 19200000, 240000000, 269333333 } },
502 .reg = { "csiphy1" },
503 .interrupt = { "csiphy1" }
509 .clock = { "camnoc_axi", "soc_ahb", "slow_ahb_src",
510 "cpas_ahb", "cphy_rx_src", "csiphy2",
511 "csiphy2_timer_src", "csiphy2_timer" },
512 .clock_rate = { { 0 },
519 { 19200000, 240000000, 269333333 } },
520 .reg = { "csiphy2" },
521 .interrupt = { "csiphy2" }
527 .clock = { "camnoc_axi", "soc_ahb", "slow_ahb_src",
528 "cpas_ahb", "cphy_rx_src", "csiphy3",
529 "csiphy3_timer_src", "csiphy3_timer" },
530 .clock_rate = { { 0 },
537 { 19200000, 240000000, 269333333 } },
538 .reg = { "csiphy3" },
539 .interrupt = { "csiphy3" }
543 static const struct resources csid_res_845[] = {
546 .regulators = { "vdda-phy", "vdda-pll" },
547 .clock = { "cpas_ahb", "cphy_rx_src", "slow_ahb_src",
548 "soc_ahb", "vfe0", "vfe0_src",
549 "vfe0_cphy_rx", "csi0",
551 .clock_rate = { { 0 },
555 { 19200000, 100000000, 320000000, 404000000, 480000000, 600000000 },
558 { 19200000, 75000000, 384000000, 538666667 },
561 .interrupt = { "csid0" }
566 .regulators = { "vdda-phy", "vdda-pll" },
567 .clock = { "cpas_ahb", "cphy_rx_src", "slow_ahb_src",
568 "soc_ahb", "vfe1", "vfe1_src",
569 "vfe1_cphy_rx", "csi1",
571 .clock_rate = { { 0 },
575 { 19200000, 100000000, 320000000, 404000000, 480000000, 600000000 },
578 { 19200000, 75000000, 384000000, 538666667 },
581 .interrupt = { "csid1" }
586 .regulators = { "vdda-phy", "vdda-pll" },
587 .clock = { "cpas_ahb", "cphy_rx_src", "slow_ahb_src",
588 "soc_ahb", "vfe_lite", "vfe_lite_src",
589 "vfe_lite_cphy_rx", "csi2",
591 .clock_rate = { { 0 },
595 { 19200000, 100000000, 320000000, 404000000, 480000000, 600000000 },
598 { 19200000, 75000000, 384000000, 538666667 },
601 .interrupt = { "csid2" }
605 static const struct resources vfe_res_845[] = {
609 .clock = { "camnoc_axi", "cpas_ahb", "slow_ahb_src",
610 "soc_ahb", "vfe0", "vfe0_axi",
613 .clock_rate = { { 0 },
617 { 19200000, 100000000, 320000000, 404000000, 480000000, 600000000 },
620 { 19200000, 75000000, 384000000, 538666667 },
623 .interrupt = { "vfe0" }
629 .clock = { "camnoc_axi", "cpas_ahb", "slow_ahb_src",
630 "soc_ahb", "vfe1", "vfe1_axi",
633 .clock_rate = { { 0 },
637 { 19200000, 100000000, 320000000, 404000000, 480000000, 600000000 },
640 { 19200000, 75000000, 384000000, 538666667 },
643 .interrupt = { "vfe1" }
649 .clock = { "camnoc_axi", "cpas_ahb", "slow_ahb_src",
650 "soc_ahb", "vfe_lite",
651 "vfe_lite_src", "csi2",
653 .clock_rate = { { 0 },
657 { 19200000, 100000000, 320000000, 404000000, 480000000, 600000000 },
659 { 19200000, 75000000, 384000000, 538666667 },
661 .reg = { "vfe_lite" },
662 .interrupt = { "vfe_lite" }
666 static const struct resources csiphy_res_8250[] = {
670 .clock = { "csiphy0", "csiphy0_timer" },
671 .clock_rate = { { 400000000 },
673 .reg = { "csiphy0" },
674 .interrupt = { "csiphy0" }
679 .clock = { "csiphy1", "csiphy1_timer" },
680 .clock_rate = { { 400000000 },
682 .reg = { "csiphy1" },
683 .interrupt = { "csiphy1" }
688 .clock = { "csiphy2", "csiphy2_timer" },
689 .clock_rate = { { 400000000 },
691 .reg = { "csiphy2" },
692 .interrupt = { "csiphy2" }
697 .clock = { "csiphy3", "csiphy3_timer" },
698 .clock_rate = { { 400000000 },
700 .reg = { "csiphy3" },
701 .interrupt = { "csiphy3" }
706 .clock = { "csiphy4", "csiphy4_timer" },
707 .clock_rate = { { 400000000 },
709 .reg = { "csiphy4" },
710 .interrupt = { "csiphy4" }
715 .clock = { "csiphy5", "csiphy5_timer" },
716 .clock_rate = { { 400000000 },
718 .reg = { "csiphy5" },
719 .interrupt = { "csiphy5" }
723 static const struct resources csid_res_8250[] = {
726 .regulators = { "vdda-phy", "vdda-pll" },
727 .clock = { "vfe0_csid", "vfe0_cphy_rx", "vfe0", "vfe0_areg", "vfe0_ahb" },
728 .clock_rate = { { 400000000 },
730 { 350000000, 475000000, 576000000, 720000000 },
731 { 100000000, 200000000, 300000000, 400000000 },
734 .interrupt = { "csid0" }
738 .regulators = { "vdda-phy", "vdda-pll" },
739 .clock = { "vfe1_csid", "vfe1_cphy_rx", "vfe1", "vfe1_areg", "vfe1_ahb" },
740 .clock_rate = { { 400000000 },
742 { 350000000, 475000000, 576000000, 720000000 },
743 { 100000000, 200000000, 300000000, 400000000 },
746 .interrupt = { "csid1" }
750 .regulators = { "vdda-phy", "vdda-pll" },
751 .clock = { "vfe_lite_csid", "vfe_lite_cphy_rx", "vfe_lite", "vfe_lite_ahb" },
752 .clock_rate = { { 400000000 },
754 { 400000000, 480000000 },
757 .interrupt = { "csid2" }
761 .regulators = { "vdda-phy", "vdda-pll" },
762 .clock = { "vfe_lite_csid", "vfe_lite_cphy_rx", "vfe_lite", "vfe_lite_ahb" },
763 .clock_rate = { { 400000000 },
765 { 400000000, 480000000 },
768 .interrupt = { "csid3" }
772 static const struct resources vfe_res_8250[] = {
776 .clock = { "camnoc_axi_src", "slow_ahb_src", "cpas_ahb",
777 "camnoc_axi", "vfe0_ahb", "vfe0_areg", "vfe0",
778 "vfe0_axi", "cam_hf_axi" },
779 .clock_rate = { { 19200000, 300000000, 400000000, 480000000 },
780 { 19200000, 80000000 },
784 { 100000000, 200000000, 300000000, 400000000 },
785 { 350000000, 475000000, 576000000, 720000000 },
789 .interrupt = { "vfe0" }
794 .clock = { "camnoc_axi_src", "slow_ahb_src", "cpas_ahb",
795 "camnoc_axi", "vfe1_ahb", "vfe1_areg", "vfe1",
796 "vfe1_axi", "cam_hf_axi" },
797 .clock_rate = { { 19200000, 300000000, 400000000, 480000000 },
798 { 19200000, 80000000 },
802 { 100000000, 200000000, 300000000, 400000000 },
803 { 350000000, 475000000, 576000000, 720000000 },
807 .interrupt = { "vfe1" }
812 .clock = { "camnoc_axi_src", "slow_ahb_src", "cpas_ahb",
813 "camnoc_axi", "vfe_lite_ahb", "vfe_lite_axi",
814 "vfe_lite", "cam_hf_axi" },
815 .clock_rate = { { 19200000, 300000000, 400000000, 480000000 },
816 { 19200000, 80000000 },
821 { 400000000, 480000000 },
823 .reg = { "vfe_lite0" },
824 .interrupt = { "vfe_lite0" }
829 .clock = { "camnoc_axi_src", "slow_ahb_src", "cpas_ahb",
830 "camnoc_axi", "vfe_lite_ahb", "vfe_lite_axi",
831 "vfe_lite", "cam_hf_axi" },
832 .clock_rate = { { 19200000, 300000000, 400000000, 480000000 },
833 { 19200000, 80000000 },
838 { 400000000, 480000000 },
840 .reg = { "vfe_lite1" },
841 .interrupt = { "vfe_lite1" }
845 static const struct resources_icc icc_res_sm8250[] = {
848 .icc_bw_tbl.avg = 38400,
849 .icc_bw_tbl.peak = 76800,
852 .name = "cam_hf_0_mnoc",
853 .icc_bw_tbl.avg = 2097152,
854 .icc_bw_tbl.peak = 2097152,
857 .name = "cam_sf_0_mnoc",
859 .icc_bw_tbl.peak = 2097152,
862 .name = "cam_sf_icp_mnoc",
863 .icc_bw_tbl.avg = 2097152,
864 .icc_bw_tbl.peak = 2097152,
869 * camss_add_clock_margin - Add margin to clock frequency rate
870 * @rate: Clock frequency rate
872 * When making calculations with physical clock frequency values
873 * some safety margin must be added. Add it.
875 inline void camss_add_clock_margin(u64 *rate)
877 *rate *= CAMSS_CLOCK_MARGIN_NUMERATOR;
878 *rate = div_u64(*rate, CAMSS_CLOCK_MARGIN_DENOMINATOR);
882 * camss_enable_clocks - Enable multiple clocks
883 * @nclocks: Number of clocks in clock array
884 * @clock: Clock array
887 * Return 0 on success or a negative error code otherwise
889 int camss_enable_clocks(int nclocks, struct camss_clock *clock,
895 for (i = 0; i < nclocks; i++) {
896 ret = clk_prepare_enable(clock[i].clk);
898 dev_err(dev, "clock enable failed: %d\n", ret);
906 for (i--; i >= 0; i--)
907 clk_disable_unprepare(clock[i].clk);
913 * camss_disable_clocks - Disable multiple clocks
914 * @nclocks: Number of clocks in clock array
915 * @clock: Clock array
917 void camss_disable_clocks(int nclocks, struct camss_clock *clock)
921 for (i = nclocks - 1; i >= 0; i--)
922 clk_disable_unprepare(clock[i].clk);
926 * camss_find_sensor - Find a linked media entity which represents a sensor
927 * @entity: Media entity to start searching from
929 * Return a pointer to sensor media entity or NULL if not found
931 struct media_entity *camss_find_sensor(struct media_entity *entity)
933 struct media_pad *pad;
936 pad = &entity->pads[0];
937 if (!(pad->flags & MEDIA_PAD_FL_SINK))
940 pad = media_pad_remote_pad_first(pad);
941 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
944 entity = pad->entity;
946 if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
952 * camss_get_link_freq - Get link frequency from sensor
953 * @entity: Media entity in the current pipeline
954 * @bpp: Number of bits per pixel for the current format
955 * @lanes: Number of lanes in the link to the sensor
957 * Return link frequency on success or a negative error code otherwise
959 s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
962 struct media_entity *sensor;
963 struct v4l2_subdev *subdev;
965 sensor = camss_find_sensor(entity);
969 subdev = media_entity_to_v4l2_subdev(sensor);
971 return v4l2_get_link_freq(subdev->ctrl_handler, bpp, 2 * lanes);
975 * camss_get_pixel_clock - Get pixel clock rate from sensor
976 * @entity: Media entity in the current pipeline
977 * @pixel_clock: Received pixel clock value
979 * Return 0 on success or a negative error code otherwise
981 int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
983 struct media_entity *sensor;
984 struct v4l2_subdev *subdev;
985 struct v4l2_ctrl *ctrl;
987 sensor = camss_find_sensor(entity);
991 subdev = media_entity_to_v4l2_subdev(sensor);
993 ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
998 *pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);
1003 int camss_pm_domain_on(struct camss *camss, int id)
1007 if (id < camss->vfe_num) {
1008 struct vfe_device *vfe = &camss->vfe[id];
1010 ret = vfe->ops->pm_domain_on(vfe);
1016 void camss_pm_domain_off(struct camss *camss, int id)
1018 if (id < camss->vfe_num) {
1019 struct vfe_device *vfe = &camss->vfe[id];
1021 vfe->ops->pm_domain_off(vfe);
1026 * camss_of_parse_endpoint_node - Parse port endpoint node
1028 * @node: Device node to be parsed
1029 * @csd: Parsed data from port endpoint node
1031 * Return 0 on success or a negative error code on failure
1033 static int camss_of_parse_endpoint_node(struct device *dev,
1034 struct device_node *node,
1035 struct camss_async_subdev *csd)
1037 struct csiphy_lanes_cfg *lncfg = &csd->interface.csi2.lane_cfg;
1038 struct v4l2_mbus_config_mipi_csi2 *mipi_csi2;
1039 struct v4l2_fwnode_endpoint vep = { { 0 } };
1042 v4l2_fwnode_endpoint_parse(of_fwnode_handle(node), &vep);
1044 csd->interface.csiphy_id = vep.base.port;
1046 mipi_csi2 = &vep.bus.mipi_csi2;
1047 lncfg->clk.pos = mipi_csi2->clock_lane;
1048 lncfg->clk.pol = mipi_csi2->lane_polarities[0];
1049 lncfg->num_data = mipi_csi2->num_data_lanes;
1051 lncfg->data = devm_kcalloc(dev,
1052 lncfg->num_data, sizeof(*lncfg->data),
1057 for (i = 0; i < lncfg->num_data; i++) {
1058 lncfg->data[i].pos = mipi_csi2->data_lanes[i];
1059 lncfg->data[i].pol = mipi_csi2->lane_polarities[i + 1];
1066 * camss_of_parse_ports - Parse ports node
1068 * @notifier: v4l2_device notifier data
1070 * Return number of "port" nodes found in "ports" node
1072 static int camss_of_parse_ports(struct camss *camss)
1074 struct device *dev = camss->dev;
1075 struct device_node *node = NULL;
1076 struct device_node *remote = NULL;
1077 int ret, num_subdevs = 0;
1079 for_each_endpoint_of_node(dev->of_node, node) {
1080 struct camss_async_subdev *csd;
1082 if (!of_device_is_available(node))
1085 remote = of_graph_get_remote_port_parent(node);
1087 dev_err(dev, "Cannot get remote parent\n");
1092 csd = v4l2_async_nf_add_fwnode(&camss->notifier,
1093 of_fwnode_handle(remote),
1094 struct camss_async_subdev);
1095 of_node_put(remote);
1101 ret = camss_of_parse_endpoint_node(dev, node, csd);
1116 * camss_init_subdevices - Initialize subdev structures and resources
1117 * @camss: CAMSS device
1119 * Return 0 on success or a negative error code on failure
1121 static int camss_init_subdevices(struct camss *camss)
1123 const struct resources *csiphy_res;
1124 const struct resources *csid_res;
1125 const struct resources_ispif *ispif_res;
1126 const struct resources *vfe_res;
1130 if (camss->version == CAMSS_8x16) {
1131 csiphy_res = csiphy_res_8x16;
1132 csid_res = csid_res_8x16;
1133 ispif_res = &ispif_res_8x16;
1134 vfe_res = vfe_res_8x16;
1135 } else if (camss->version == CAMSS_8x96) {
1136 csiphy_res = csiphy_res_8x96;
1137 csid_res = csid_res_8x96;
1138 ispif_res = &ispif_res_8x96;
1139 vfe_res = vfe_res_8x96;
1140 } else if (camss->version == CAMSS_660) {
1141 csiphy_res = csiphy_res_660;
1142 csid_res = csid_res_660;
1143 ispif_res = &ispif_res_660;
1144 vfe_res = vfe_res_660;
1145 } else if (camss->version == CAMSS_845) {
1146 csiphy_res = csiphy_res_845;
1147 csid_res = csid_res_845;
1148 /* Titan VFEs don't have an ISPIF */
1150 vfe_res = vfe_res_845;
1151 } else if (camss->version == CAMSS_8250) {
1152 csiphy_res = csiphy_res_8250;
1153 csid_res = csid_res_8250;
1154 /* Titan VFEs don't have an ISPIF */
1156 vfe_res = vfe_res_8250;
1161 for (i = 0; i < camss->csiphy_num; i++) {
1162 ret = msm_csiphy_subdev_init(camss, &camss->csiphy[i],
1166 "Failed to init csiphy%d sub-device: %d\n",
1172 /* note: SM8250 requires VFE to be initialized before CSID */
1173 for (i = 0; i < camss->vfe_num + camss->vfe_lite_num; i++) {
1174 ret = msm_vfe_subdev_init(camss, &camss->vfe[i],
1178 "Fail to init vfe%d sub-device: %d\n", i, ret);
1183 for (i = 0; i < camss->csid_num; i++) {
1184 ret = msm_csid_subdev_init(camss, &camss->csid[i],
1188 "Failed to init csid%d sub-device: %d\n",
1194 ret = msm_ispif_subdev_init(camss, ispif_res);
1196 dev_err(camss->dev, "Failed to init ispif sub-device: %d\n",
1205 * camss_register_entities - Register subdev nodes and create links
1206 * @camss: CAMSS device
1208 * Return 0 on success or a negative error code on failure
1210 static int camss_register_entities(struct camss *camss)
1215 for (i = 0; i < camss->csiphy_num; i++) {
1216 ret = msm_csiphy_register_entity(&camss->csiphy[i],
1220 "Failed to register csiphy%d entity: %d\n",
1222 goto err_reg_csiphy;
1226 for (i = 0; i < camss->csid_num; i++) {
1227 ret = msm_csid_register_entity(&camss->csid[i],
1231 "Failed to register csid%d entity: %d\n",
1237 ret = msm_ispif_register_entities(camss->ispif,
1240 dev_err(camss->dev, "Failed to register ispif entities: %d\n",
1245 for (i = 0; i < camss->vfe_num + camss->vfe_lite_num; i++) {
1246 ret = msm_vfe_register_entities(&camss->vfe[i],
1250 "Failed to register vfe%d entities: %d\n",
1256 for (i = 0; i < camss->csiphy_num; i++) {
1257 for (j = 0; j < camss->csid_num; j++) {
1258 ret = media_create_pad_link(
1259 &camss->csiphy[i].subdev.entity,
1261 &camss->csid[j].subdev.entity,
1266 "Failed to link %s->%s entities: %d\n",
1267 camss->csiphy[i].subdev.entity.name,
1268 camss->csid[j].subdev.entity.name,
1276 for (i = 0; i < camss->csid_num; i++) {
1277 for (j = 0; j < camss->ispif->line_num; j++) {
1278 ret = media_create_pad_link(
1279 &camss->csid[i].subdev.entity,
1281 &camss->ispif->line[j].subdev.entity,
1286 "Failed to link %s->%s entities: %d\n",
1287 camss->csid[i].subdev.entity.name,
1288 camss->ispif->line[j].subdev.entity.name,
1295 for (i = 0; i < camss->ispif->line_num; i++)
1296 for (k = 0; k < camss->vfe_num; k++)
1297 for (j = 0; j < camss->vfe[k].line_num; j++) {
1298 struct v4l2_subdev *ispif = &camss->ispif->line[i].subdev;
1299 struct v4l2_subdev *vfe = &camss->vfe[k].line[j].subdev;
1301 ret = media_create_pad_link(&ispif->entity,
1308 "Failed to link %s->%s entities: %d\n",
1316 for (i = 0; i < camss->csid_num; i++)
1317 for (k = 0; k < camss->vfe_num + camss->vfe_lite_num; k++)
1318 for (j = 0; j < camss->vfe[k].line_num; j++) {
1319 struct v4l2_subdev *csid = &camss->csid[i].subdev;
1320 struct v4l2_subdev *vfe = &camss->vfe[k].line[j].subdev;
1322 ret = media_create_pad_link(&csid->entity,
1323 MSM_CSID_PAD_FIRST_SRC + j,
1329 "Failed to link %s->%s entities: %d\n",
1341 i = camss->vfe_num + camss->vfe_lite_num;
1343 for (i--; i >= 0; i--)
1344 msm_vfe_unregister_entities(&camss->vfe[i]);
1347 msm_ispif_unregister_entities(camss->ispif);
1349 i = camss->csid_num;
1351 for (i--; i >= 0; i--)
1352 msm_csid_unregister_entity(&camss->csid[i]);
1354 i = camss->csiphy_num;
1356 for (i--; i >= 0; i--)
1357 msm_csiphy_unregister_entity(&camss->csiphy[i]);
1363 * camss_unregister_entities - Unregister subdev nodes
1364 * @camss: CAMSS device
1366 * Return 0 on success or a negative error code on failure
1368 static void camss_unregister_entities(struct camss *camss)
1372 for (i = 0; i < camss->csiphy_num; i++)
1373 msm_csiphy_unregister_entity(&camss->csiphy[i]);
1375 for (i = 0; i < camss->csid_num; i++)
1376 msm_csid_unregister_entity(&camss->csid[i]);
1378 msm_ispif_unregister_entities(camss->ispif);
1380 for (i = 0; i < camss->vfe_num + camss->vfe_lite_num; i++)
1381 msm_vfe_unregister_entities(&camss->vfe[i]);
1384 static int camss_subdev_notifier_bound(struct v4l2_async_notifier *async,
1385 struct v4l2_subdev *subdev,
1386 struct v4l2_async_connection *asd)
1388 struct camss *camss = container_of(async, struct camss, notifier);
1389 struct camss_async_subdev *csd =
1390 container_of(asd, struct camss_async_subdev, asd);
1391 u8 id = csd->interface.csiphy_id;
1392 struct csiphy_device *csiphy = &camss->csiphy[id];
1394 csiphy->cfg.csi2 = &csd->interface.csi2;
1395 subdev->host_priv = csiphy;
1400 static int camss_subdev_notifier_complete(struct v4l2_async_notifier *async)
1402 struct camss *camss = container_of(async, struct camss, notifier);
1403 struct v4l2_device *v4l2_dev = &camss->v4l2_dev;
1404 struct v4l2_subdev *sd;
1407 list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
1408 if (sd->host_priv) {
1409 struct media_entity *sensor = &sd->entity;
1410 struct csiphy_device *csiphy =
1411 (struct csiphy_device *) sd->host_priv;
1412 struct media_entity *input = &csiphy->subdev.entity;
1415 for (i = 0; i < sensor->num_pads; i++) {
1416 if (sensor->pads[i].flags & MEDIA_PAD_FL_SOURCE)
1419 if (i == sensor->num_pads) {
1421 "No source pad in external entity\n");
1425 ret = media_create_pad_link(sensor, i,
1426 input, MSM_CSIPHY_PAD_SINK,
1427 MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
1430 "Failed to link %s->%s entities: %d\n",
1431 sensor->name, input->name, ret);
1437 ret = v4l2_device_register_subdev_nodes(&camss->v4l2_dev);
1441 return media_device_register(&camss->media_dev);
1444 static const struct v4l2_async_notifier_operations camss_subdev_notifier_ops = {
1445 .bound = camss_subdev_notifier_bound,
1446 .complete = camss_subdev_notifier_complete,
1449 static const struct media_device_ops camss_media_ops = {
1450 .link_notify = v4l2_pipeline_link_notify,
1453 static int camss_configure_pd(struct camss *camss)
1455 struct device *dev = camss->dev;
1459 camss->genpd_num = of_count_phandle_with_args(dev->of_node,
1461 "#power-domain-cells");
1462 if (camss->genpd_num < 0) {
1463 dev_err(dev, "Power domains are not defined for camss\n");
1464 return camss->genpd_num;
1468 * If a platform device has just one power domain, then it is attached
1469 * at platform_probe() level, thus there shall be no need and even no
1470 * option to attach it again, this is the case for CAMSS on MSM8916.
1472 if (camss->genpd_num == 1)
1475 camss->genpd = devm_kmalloc_array(dev, camss->genpd_num,
1476 sizeof(*camss->genpd), GFP_KERNEL);
1480 camss->genpd_link = devm_kmalloc_array(dev, camss->genpd_num,
1481 sizeof(*camss->genpd_link),
1483 if (!camss->genpd_link)
1487 * VFE power domains are in the beginning of the list, and while all
1488 * power domains should be attached, only if TITAN_TOP power domain is
1489 * found in the list, it should be linked over here.
1491 for (i = 0; i < camss->genpd_num; i++) {
1492 camss->genpd[i] = dev_pm_domain_attach_by_id(camss->dev, i);
1493 if (IS_ERR(camss->genpd[i])) {
1494 ret = PTR_ERR(camss->genpd[i]);
1499 if (i > camss->vfe_num) {
1500 camss->genpd_link[i - 1] = device_link_add(camss->dev, camss->genpd[i - 1],
1501 DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME |
1502 DL_FLAG_RPM_ACTIVE);
1503 if (!camss->genpd_link[i - 1]) {
1512 for (--i ; i >= 0; i--)
1513 dev_pm_domain_detach(camss->genpd[i], true);
1518 static int camss_icc_get(struct camss *camss)
1520 const struct resources_icc *icc_res;
1521 int nbr_icc_paths = 0;
1524 if (camss->version == CAMSS_8250) {
1525 icc_res = &icc_res_sm8250[0];
1526 nbr_icc_paths = ICC_SM8250_COUNT;
1529 for (i = 0; i < nbr_icc_paths; i++) {
1530 camss->icc_path[i] = devm_of_icc_get(camss->dev,
1532 if (IS_ERR(camss->icc_path[i]))
1533 return PTR_ERR(camss->icc_path[i]);
1535 camss->icc_bw_tbl[i] = icc_res[i].icc_bw_tbl;
1542 * camss_probe - Probe CAMSS platform device
1543 * @pdev: Pointer to CAMSS platform device
1545 * Return 0 on success or a negative error code on failure
1547 static int camss_probe(struct platform_device *pdev)
1549 struct device *dev = &pdev->dev;
1550 struct camss *camss;
1551 int num_subdevs, ret;
1553 camss = devm_kzalloc(dev, sizeof(*camss), GFP_KERNEL);
1557 atomic_set(&camss->ref_count, 0);
1559 platform_set_drvdata(pdev, camss);
1561 if (of_device_is_compatible(dev->of_node, "qcom,msm8916-camss")) {
1562 camss->version = CAMSS_8x16;
1563 camss->csiphy_num = 2;
1564 camss->csid_num = 2;
1566 } else if (of_device_is_compatible(dev->of_node,
1567 "qcom,msm8996-camss")) {
1568 camss->version = CAMSS_8x96;
1569 camss->csiphy_num = 3;
1570 camss->csid_num = 4;
1572 } else if (of_device_is_compatible(dev->of_node,
1573 "qcom,sdm660-camss")) {
1574 camss->version = CAMSS_660;
1575 camss->csiphy_num = 3;
1576 camss->csid_num = 4;
1578 } else if (of_device_is_compatible(dev->of_node,
1579 "qcom,sdm845-camss")) {
1580 camss->version = CAMSS_845;
1581 camss->csiphy_num = 4;
1582 camss->csid_num = 3;
1584 camss->vfe_lite_num = 1;
1585 } else if (of_device_is_compatible(dev->of_node,
1586 "qcom,sm8250-camss")) {
1587 camss->version = CAMSS_8250;
1588 camss->csiphy_num = 6;
1589 camss->csid_num = 4;
1591 camss->vfe_lite_num = 2;
1596 camss->csiphy = devm_kcalloc(dev, camss->csiphy_num,
1597 sizeof(*camss->csiphy), GFP_KERNEL);
1601 camss->csid = devm_kcalloc(dev, camss->csid_num, sizeof(*camss->csid),
1606 if (camss->version == CAMSS_8x16 ||
1607 camss->version == CAMSS_8x96) {
1608 camss->ispif = devm_kcalloc(dev, 1, sizeof(*camss->ispif), GFP_KERNEL);
1613 camss->vfe = devm_kcalloc(dev, camss->vfe_num + camss->vfe_lite_num,
1614 sizeof(*camss->vfe), GFP_KERNEL);
1618 ret = camss_icc_get(camss);
1622 ret = camss_init_subdevices(camss);
1626 ret = dma_set_mask_and_coherent(dev, 0xffffffff);
1630 camss->media_dev.dev = camss->dev;
1631 strscpy(camss->media_dev.model, "Qualcomm Camera Subsystem",
1632 sizeof(camss->media_dev.model));
1633 camss->media_dev.ops = &camss_media_ops;
1634 media_device_init(&camss->media_dev);
1636 camss->v4l2_dev.mdev = &camss->media_dev;
1637 ret = v4l2_device_register(camss->dev, &camss->v4l2_dev);
1639 dev_err(dev, "Failed to register V4L2 device: %d\n", ret);
1643 v4l2_async_nf_init(&camss->notifier, &camss->v4l2_dev);
1645 num_subdevs = camss_of_parse_ports(camss);
1646 if (num_subdevs < 0) {
1651 ret = camss_register_entities(camss);
1656 camss->notifier.ops = &camss_subdev_notifier_ops;
1658 ret = v4l2_async_nf_register(&camss->notifier);
1661 "Failed to register async subdev nodes: %d\n",
1663 goto err_register_subdevs;
1666 ret = v4l2_device_register_subdev_nodes(&camss->v4l2_dev);
1668 dev_err(dev, "Failed to register subdev nodes: %d\n",
1670 goto err_register_subdevs;
1673 ret = media_device_register(&camss->media_dev);
1675 dev_err(dev, "Failed to register media device: %d\n",
1677 goto err_register_subdevs;
1681 ret = camss_configure_pd(camss);
1683 dev_err(dev, "Failed to configure power domains: %d\n", ret);
1687 pm_runtime_enable(dev);
1691 err_register_subdevs:
1692 camss_unregister_entities(camss);
1694 v4l2_device_unregister(&camss->v4l2_dev);
1695 v4l2_async_nf_cleanup(&camss->notifier);
1700 void camss_delete(struct camss *camss)
1704 v4l2_device_unregister(&camss->v4l2_dev);
1705 media_device_unregister(&camss->media_dev);
1706 media_device_cleanup(&camss->media_dev);
1708 pm_runtime_disable(camss->dev);
1710 if (camss->genpd_num == 1)
1713 if (camss->genpd_num > camss->vfe_num)
1714 device_link_del(camss->genpd_link[camss->genpd_num - 1]);
1716 for (i = 0; i < camss->genpd_num; i++)
1717 dev_pm_domain_detach(camss->genpd[i], true);
1721 * camss_remove - Remove CAMSS platform device
1722 * @pdev: Pointer to CAMSS platform device
1726 static void camss_remove(struct platform_device *pdev)
1728 struct camss *camss = platform_get_drvdata(pdev);
1730 v4l2_async_nf_unregister(&camss->notifier);
1731 v4l2_async_nf_cleanup(&camss->notifier);
1732 camss_unregister_entities(camss);
1734 if (atomic_read(&camss->ref_count) == 0)
1735 camss_delete(camss);
1738 static const struct of_device_id camss_dt_match[] = {
1739 { .compatible = "qcom,msm8916-camss" },
1740 { .compatible = "qcom,msm8996-camss" },
1741 { .compatible = "qcom,sdm660-camss" },
1742 { .compatible = "qcom,sdm845-camss" },
1743 { .compatible = "qcom,sm8250-camss" },
1747 MODULE_DEVICE_TABLE(of, camss_dt_match);
1749 static int __maybe_unused camss_runtime_suspend(struct device *dev)
1751 struct camss *camss = dev_get_drvdata(dev);
1752 int nbr_icc_paths = 0;
1756 if (camss->version == CAMSS_8250)
1757 nbr_icc_paths = ICC_SM8250_COUNT;
1759 for (i = 0; i < nbr_icc_paths; i++) {
1760 ret = icc_set_bw(camss->icc_path[i], 0, 0);
1768 static int __maybe_unused camss_runtime_resume(struct device *dev)
1770 struct camss *camss = dev_get_drvdata(dev);
1771 int nbr_icc_paths = 0;
1775 if (camss->version == CAMSS_8250)
1776 nbr_icc_paths = ICC_SM8250_COUNT;
1778 for (i = 0; i < nbr_icc_paths; i++) {
1779 ret = icc_set_bw(camss->icc_path[i],
1780 camss->icc_bw_tbl[i].avg,
1781 camss->icc_bw_tbl[i].peak);
1789 static const struct dev_pm_ops camss_pm_ops = {
1790 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1791 pm_runtime_force_resume)
1792 SET_RUNTIME_PM_OPS(camss_runtime_suspend, camss_runtime_resume, NULL)
1795 static struct platform_driver qcom_camss_driver = {
1796 .probe = camss_probe,
1797 .remove_new = camss_remove,
1799 .name = "qcom-camss",
1800 .of_match_table = camss_dt_match,
1801 .pm = &camss_pm_ops,
1805 module_platform_driver(qcom_camss_driver);
1807 MODULE_ALIAS("platform:qcom-camss");
1808 MODULE_DESCRIPTION("Qualcomm Camera Subsystem driver");
1809 MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1810 MODULE_LICENSE("GPL v2");