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 <atomic>
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 <lib/zx/interrupt.h>
13 #include <optional>
14 #include <threads.h>
15 #include <zircon/device/thermal.h>
16 
17 namespace thermal {
18 
19 // This class represents a temperature sensor
20 // which is on the S905D2 core.
21 class AmlTSensor {
22 
23 public:
24     DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(AmlTSensor);
AmlTSensor()25     AmlTSensor(){};
26     uint32_t ReadTemperature();
27     zx_status_t InitSensor(zx_device_t* parent, thermal_device_info_t thermal_config);
28     zx_status_t GetStateChangePort(zx_handle_t* port);
29     ~AmlTSensor();
30 
31 private:
32     int TripPointIrqHandler();
33     zx_status_t InitPdev(zx_device_t* parent);
34     uint32_t TempToCode(uint32_t temp, bool trend);
35     uint32_t CodeToTemp(uint32_t temp_code);
36     void SetRebootTemperature(uint32_t temp);
37     zx_status_t InitTripPoints();
38     zx_status_t NotifyThermalDaemon();
39     void UpdateFallThresholdIrq(uint32_t irq);
40     void UpdateRiseThresholdIrq(uint32_t irq);
41     uint32_t trim_info_;
42     pdev_protocol_t pdev_;
43     std::optional<ddk::MmioBuffer> pll_mmio_;
44     std::optional<ddk::MmioBuffer> ao_mmio_;
45     std::optional<ddk::MmioBuffer> hiu_mmio_;
46     zx::interrupt tsensor_irq_;
47     thrd_t irq_thread_;
48     std::atomic<bool> running_;
49     zx_handle_t port_;
50     thermal_device_info_t thermal_config_;
51     uint32_t current_trip_idx_ = 0;
52 };
53 } // namespace thermal
54