788606cd8be9dcb7c7fc4377a371b7a366bea603
[openwrt/openwrt.git] /
1 From 09c1533f7f5dabd79aa2018083d1b71d26cd7eda Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Mon, 11 May 2020 13:02:22 +0100
4 Subject: [PATCH] media: bcm2835: unicam: Set VPU min clock freq to
5 250Mhz.
6
7 When streaming with Unicam, the VPU must have a clock frequency of at
8 least 250Mhz. Otherwise, the input fifos could overrun, causing
9 image corruption.
10
11 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
12 ---
13 arch/arm/boot/dts/bcm270x.dtsi | 10 ++--
14 .../media/platform/bcm2835/bcm2835-unicam.c | 49 +++++++++++++++++--
15 2 files changed, 50 insertions(+), 9 deletions(-)
16
17 --- a/arch/arm/boot/dts/bcm270x.dtsi
18 +++ b/arch/arm/boot/dts/bcm270x.dtsi
19 @@ -88,8 +88,9 @@
20 reg = <0x7e800000 0x800>,
21 <0x7e802000 0x4>;
22 interrupts = <2 6>;
23 - clocks = <&clocks BCM2835_CLOCK_CAM0>;
24 - clock-names = "lp";
25 + clocks = <&clocks BCM2835_CLOCK_CAM0>,
26 + <&firmware_clocks 4>;
27 + clock-names = "lp", "vpu";
28 power-domains = <&power RPI_POWER_DOMAIN_UNICAM0>;
29 #address-cells = <1>;
30 #size-cells = <0>;
31 @@ -102,8 +103,9 @@
32 reg = <0x7e801000 0x800>,
33 <0x7e802004 0x4>;
34 interrupts = <2 7>;
35 - clocks = <&clocks BCM2835_CLOCK_CAM1>;
36 - clock-names = "lp";
37 + clocks = <&clocks BCM2835_CLOCK_CAM1>,
38 + <&firmware_clocks 4>;
39 + clock-names = "lp", "vpu";
40 power-domains = <&power RPI_POWER_DOMAIN_UNICAM1>;
41 #address-cells = <1>;
42 #size-cells = <0>;
43 --- a/drivers/media/platform/bcm2835/bcm2835-unicam.c
44 +++ b/drivers/media/platform/bcm2835/bcm2835-unicam.c
45 @@ -89,6 +89,11 @@ MODULE_PARM_DESC(debug, "Debug level 0-3
46 v4l2_err(&(dev)->v4l2_dev, fmt, ##arg)
47
48 /*
49 + * Unicam must request a minimum of 250Mhz from the VPU clock.
50 + * Otherwise the input FIFOs overrun and cause image corruption.
51 + */
52 +#define MIN_VPU_CLOCK_RATE (250 * 1000 * 1000)
53 +/*
54 * To protect against a dodgy sensor driver never returning an error from
55 * enum_mbus_code, set a maximum index value to be used.
56 */
57 @@ -417,8 +422,10 @@ struct unicam_device {
58 void __iomem *base;
59 /* clock gating base address */
60 void __iomem *clk_gate_base;
61 - /* clock handle */
62 + /* lp clock handle */
63 struct clk *clock;
64 + /* vpu clock handle */
65 + struct clk *vpu_clock;
66 /* V4l2 device */
67 struct v4l2_device v4l2_dev;
68 struct media_device mdev;
69 @@ -1674,16 +1681,28 @@ static int unicam_start_streaming(struct
70 unicam_dbg(1, dev, "Running with %u data lanes\n",
71 dev->active_data_lanes);
72
73 - ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
74 + ret = clk_set_min_rate(dev->vpu_clock, MIN_VPU_CLOCK_RATE);
75 + if (ret) {
76 + unicam_err(dev, "failed to set up VPU clock\n");
77 + goto err_pm_put;
78 + }
79 +
80 + ret = clk_prepare_enable(dev->vpu_clock);
81 if (ret) {
82 - unicam_err(dev, "failed to set up clock\n");
83 + unicam_err(dev, "Failed to enable VPU clock: %d\n", ret);
84 goto err_pm_put;
85 }
86
87 + ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
88 + if (ret) {
89 + unicam_err(dev, "failed to set up CSI clock\n");
90 + goto err_vpu_clock;
91 + }
92 +
93 ret = clk_prepare_enable(dev->clock);
94 if (ret) {
95 unicam_err(dev, "Failed to enable CSI clock: %d\n", ret);
96 - goto err_pm_put;
97 + goto err_vpu_clock;
98 }
99
100 for (i = 0; i < ARRAY_SIZE(dev->node); i++) {
101 @@ -1717,6 +1736,11 @@ static int unicam_start_streaming(struct
102 err_disable_unicam:
103 unicam_disable(dev);
104 clk_disable_unprepare(dev->clock);
105 +err_vpu_clock:
106 + ret = clk_set_min_rate(dev->vpu_clock, 0);
107 + if (ret)
108 + unicam_err(dev, "failed to reset the VPU clock\n");
109 + clk_disable_unprepare(dev->vpu_clock);
110 err_pm_put:
111 unicam_runtime_put(dev);
112 err_streaming:
113 @@ -1734,6 +1758,8 @@ static void unicam_stop_streaming(struct
114 node->streaming = false;
115
116 if (node->pad_id == IMAGE_PAD) {
117 + int ret;
118 +
119 /*
120 * Stop streaming the sensor and disable the peripheral.
121 * We cannot continue streaming embedded data with the
122 @@ -1743,6 +1769,12 @@ static void unicam_stop_streaming(struct
123 unicam_err(dev, "stream off failed in subdev\n");
124
125 unicam_disable(dev);
126 +
127 + ret = clk_set_min_rate(dev->vpu_clock, 0);
128 + if (ret)
129 + unicam_err(dev, "failed to reset the min VPU clock\n");
130 +
131 + clk_disable_unprepare(dev->vpu_clock);
132 clk_disable_unprepare(dev->clock);
133 unicam_runtime_put(dev);
134
135 @@ -2742,11 +2774,18 @@ static int unicam_probe(struct platform_
136
137 unicam->clock = devm_clk_get(&pdev->dev, "lp");
138 if (IS_ERR(unicam->clock)) {
139 - unicam_err(unicam, "Failed to get clock\n");
140 + unicam_err(unicam, "Failed to get lp clock\n");
141 ret = PTR_ERR(unicam->clock);
142 goto err_unicam_put;
143 }
144
145 + unicam->vpu_clock = devm_clk_get(&pdev->dev, "vpu");
146 + if (IS_ERR(unicam->vpu_clock)) {
147 + unicam_err(unicam, "Failed to get vpu clock\n");
148 + ret = PTR_ERR(unicam->vpu_clock);
149 + goto err_unicam_put;
150 + }
151 +
152 ret = platform_get_irq(pdev, 0);
153 if (ret <= 0) {
154 dev_err(&pdev->dev, "No IRQ resource\n");