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
7 When streaming with Unicam, the VPU must have a clock frequency of at
8 least 250Mhz. Otherwise, the input fifos could overrun, causing
11 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
13 arch/arm/boot/dts/bcm270x.dtsi | 10 ++--
14 .../media/platform/bcm2835/bcm2835-unicam.c | 49 +++++++++++++++++--
15 2 files changed, 50 insertions(+), 9 deletions(-)
17 --- a/arch/arm/boot/dts/bcm270x.dtsi
18 +++ b/arch/arm/boot/dts/bcm270x.dtsi
20 reg = <0x7e800000 0x800>,
23 - clocks = <&clocks BCM2835_CLOCK_CAM0>;
25 + clocks = <&clocks BCM2835_CLOCK_CAM0>,
26 + <&firmware_clocks 4>;
27 + clock-names = "lp", "vpu";
28 power-domains = <&power RPI_POWER_DOMAIN_UNICAM0>;
32 reg = <0x7e801000 0x800>,
35 - clocks = <&clocks BCM2835_CLOCK_CAM1>;
37 + clocks = <&clocks BCM2835_CLOCK_CAM1>,
38 + <&firmware_clocks 4>;
39 + clock-names = "lp", "vpu";
40 power-domains = <&power RPI_POWER_DOMAIN_UNICAM1>;
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)
49 + * Unicam must request a minimum of 250Mhz from the VPU clock.
50 + * Otherwise the input FIFOs overrun and cause image corruption.
52 +#define MIN_VPU_CLOCK_RATE (250 * 1000 * 1000)
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.
57 @@ -417,8 +422,10 @@ struct unicam_device {
59 /* clock gating base address */
60 void __iomem *clk_gate_base;
62 + /* lp clock handle */
64 + /* vpu clock handle */
65 + struct clk *vpu_clock;
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);
73 - ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
74 + ret = clk_set_min_rate(dev->vpu_clock, MIN_VPU_CLOCK_RATE);
76 + unicam_err(dev, "failed to set up VPU clock\n");
80 + ret = clk_prepare_enable(dev->vpu_clock);
82 - unicam_err(dev, "failed to set up clock\n");
83 + unicam_err(dev, "Failed to enable VPU clock: %d\n", ret);
87 + ret = clk_set_rate(dev->clock, 100 * 1000 * 1000);
89 + unicam_err(dev, "failed to set up CSI clock\n");
93 ret = clk_prepare_enable(dev->clock);
95 unicam_err(dev, "Failed to enable CSI clock: %d\n", ret);
100 for (i = 0; i < ARRAY_SIZE(dev->node); i++) {
101 @@ -1717,6 +1736,11 @@ static int unicam_start_streaming(struct
104 clk_disable_unprepare(dev->clock);
106 + ret = clk_set_min_rate(dev->vpu_clock, 0);
108 + unicam_err(dev, "failed to reset the VPU clock\n");
109 + clk_disable_unprepare(dev->vpu_clock);
111 unicam_runtime_put(dev);
113 @@ -1734,6 +1758,8 @@ static void unicam_stop_streaming(struct
114 node->streaming = false;
116 if (node->pad_id == IMAGE_PAD) {
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");
127 + ret = clk_set_min_rate(dev->vpu_clock, 0);
129 + unicam_err(dev, "failed to reset the min VPU clock\n");
131 + clk_disable_unprepare(dev->vpu_clock);
132 clk_disable_unprepare(dev->clock);
133 unicam_runtime_put(dev);
135 @@ -2742,11 +2774,18 @@ static int unicam_probe(struct platform_
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);
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;
152 ret = platform_get_irq(pdev, 0);
154 dev_err(&pdev->dev, "No IRQ resource\n");