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/platform-defs.h>
8 #include <ddktl/mmio.h>
9 #include <ddktl/pdev.h>
10 #include <ddktl/protocol/clk.h>
11 #include <hwreg/mmio.h>
12 #include <lib/zx/bti.h>
13 #include <soc/aml-s905d2/s905d2-hiu.h>
14 
15 #include <optional>
16 
17 namespace thermal {
18 // This class handles the dynamic changing of
19 // CPU frequency.
20 class AmlCpuFrequency {
21 
22 public:
23     DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(AmlCpuFrequency);
AmlCpuFrequency()24     AmlCpuFrequency(){};
25     ~AmlCpuFrequency() = default;
26     zx_status_t SetFrequency(uint32_t rate);
27     zx_status_t Init(zx_device_t* parent);
28     uint32_t GetFrequency();
29 
30 private:
31     zx_status_t WaitForBusy();
32     zx_status_t ConfigureSysPLL(uint32_t new_rate);
33     zx_status_t ConfigureFixedPLL(uint32_t new_rate);
34 
35     ddk::PDev pdev_;
36     // Initialize platform stuff.
37     zx_status_t InitPdev(zx_device_t* parent);
38     // Protocols.
39     ddk::ClkProtocolClient clk_;
40     // MMIOS.
41     std::optional<ddk::MmioBuffer> hiu_mmio_;
42     // BTI handle.
43     zx::bti bti_;
44     // HIU Handle.
45     aml_hiu_dev_t hiu_;
46     // Sys PLL.
47     aml_pll_dev_t sys_pll_;
48     // Current Frequency, default is 1.2GHz,
49     // which is set by u-boot while booting up.
50     uint32_t current_rate_ = 1200000000;
51 };
52 } // namespace thermal
53