1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <ddk/driver.h>
8 #include <ddk/protocol/platform-device-lib.h>
9 #include <ddk/protocol/platform/device.h>
10 #include <ddktl/device.h>
11 #include <ddktl/mmio.h>
12 #include <hwreg/mmio.h>
13 #include <unistd.h>
14 #include <zircon/compiler.h>
15 
16 #include <optional>
17 
18 #include "aml-dsi.h"
19 #include "hhi-regs.h"
20 #include "vpu-regs.h"
21 #include "common.h"
22 
23 namespace astro_display {
24 
25 class AstroDisplayClock {
26 public:
AstroDisplayClock()27     AstroDisplayClock() {}
28     zx_status_t Init(zx_device_t* parent);
29     zx_status_t Enable(const DisplaySetting& d);
30     void Disable();
31     void Dump();
32 
GetBitrate()33     uint32_t GetBitrate() {
34         return pll_cfg_.bitrate;
35     }
36 
37 private:
38     void CalculateLcdTiming(const DisplaySetting& disp_setting);
39 
40     // This function wait for hdmi_pll to lock. The retry algorithm is
41     // undocumented and comes from U-Boot.
42     zx_status_t PllLockWait();
43 
44     // This function calculates the required pll configurations needed to generate
45     // the desired lcd clock
46     zx_status_t GenerateHPLL(const DisplaySetting& disp_setting);
47 
48     std::optional<ddk::MmioBuffer>          vpu_mmio_;
49     std::optional<ddk::MmioBuffer>          hhi_mmio_;
50     pdev_protocol_t              pdev_ = {nullptr, nullptr};
51 
52     PllConfig                               pll_cfg_;
53     LcdTiming                               lcd_timing_;
54 
55     bool                                    initialized_ = false;
56     bool                                    clock_enabled_ = false;
57 };
58 
59 } // namespace astro_display
60