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 "aml-cpufreq.h"
8 #include "aml-pwm.h"
9 #include "aml-tsensor.h"
10 #include "aml-voltage.h"
11 #include <ddk/device.h>
12 #include <ddktl/device.h>
13 #include <ddktl/protocol/empty-protocol.h>
14 #include <fbl/unique_ptr.h>
15 #include <threads.h>
16 
17 #include <utility>
18 
19 namespace thermal {
20 
21 class AmlThermal;
22 using DeviceType = ddk::Device<AmlThermal,
23                                ddk::Unbindable,
24                                ddk::Ioctlable>;
25 
26 class AmlThermal : public DeviceType,
27                    public ddk::EmptyProtocol<ZX_PROTOCOL_THERMAL> {
28 
29 public:
30     DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(AmlThermal);
AmlThermal(zx_device_t * device,fbl::unique_ptr<thermal::AmlTSensor> tsensor,fbl::unique_ptr<thermal::AmlVoltageRegulator> voltage_regulator,fbl::unique_ptr<thermal::AmlCpuFrequency> cpufreq_scaling,opp_info_t opp_info,thermal_device_info_t thermal_config)31     AmlThermal(zx_device_t* device, fbl::unique_ptr<thermal::AmlTSensor> tsensor,
32                fbl::unique_ptr<thermal::AmlVoltageRegulator> voltage_regulator,
33                fbl::unique_ptr<thermal::AmlCpuFrequency> cpufreq_scaling,
34                opp_info_t opp_info,
35                thermal_device_info_t thermal_config)
36         : DeviceType(device), tsensor_(std::move(tsensor)),
37           voltage_regulator_(std::move(voltage_regulator)),
38           cpufreq_scaling_(std::move(cpufreq_scaling)),
39           opp_info_(std::move(opp_info)),
40           thermal_config_(std::move(thermal_config)) {}
41 
42     static zx_status_t Create(zx_device_t* device);
43 
44     // Ddk Hooks
45     void DdkUnbind();
46     void DdkRelease();
47     zx_status_t DdkIoctl(uint32_t op, const void* in_buf, size_t in_len,
48                          void* out_buf, size_t out_len, size_t* out_actual);
49 
50 private:
51     int ThermalNotificationThread();
52     zx_status_t NotifyThermalDaemon();
53     zx_status_t SetTarget(uint32_t opp_idx);
54 
55     fbl::unique_ptr<thermal::AmlTSensor> tsensor_;
56     fbl::unique_ptr<thermal::AmlVoltageRegulator> voltage_regulator_;
57     fbl::unique_ptr<thermal::AmlCpuFrequency> cpufreq_scaling_;
58     opp_info_t opp_info_;
59     thermal_device_info_t thermal_config_;
60 };
61 } // namespace thermal
62