Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/thermal/tmp451.c
3694 views
1
/*
2
* SOC/PCB Temperature driver for Nintendo Switch's TI TMP451
3
*
4
* Copyright (c) 2018-2020 CTCaer
5
*
6
* This program is free software; you can redistribute it and/or modify it
7
* under the terms and conditions of the GNU General Public License,
8
* version 2, as published by the Free Software Foundation.
9
*
10
* This program is distributed in the hope it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13
* more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include <soc/hw_init.h>
20
#include <soc/i2c.h>
21
#include <soc/t210.h>
22
#include <thermal/tmp451.h>
23
24
// Remote Sensor.
25
u16 tmp451_get_soc_temp(bool intenger)
26
{
27
u8 val;
28
u16 temp = 0;
29
30
val = i2c_recv_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TEMP_REG);
31
if (intenger)
32
return val;
33
34
temp = val << 8;
35
val = i2c_recv_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TMP_DEC_REG);
36
temp |= ((val >> 4) * 625) / 100;
37
38
return temp;
39
}
40
41
// Local Sensor.
42
u16 tmp451_get_pcb_temp(bool intenger)
43
{
44
u8 val;
45
u16 temp = 0;
46
47
val = i2c_recv_byte(I2C_1, TMP451_I2C_ADDR, TMP451_PCB_TEMP_REG);
48
if (intenger)
49
return val;
50
51
temp = val << 8;
52
val = i2c_recv_byte(I2C_1, TMP451_I2C_ADDR, TMP451_PCB_TMP_DEC_REG);
53
temp |= ((val >> 4) * 625) / 100;
54
55
return temp;
56
}
57
58
void tmp451_init()
59
{
60
// Disable ALARM and Range to 0 - 127 oC.
61
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_CONFIG_REG, 0x80);
62
63
// Set remote sensor offsets based on SoC.
64
if (hw_get_chip_id() == GP_HIDREV_MAJOR_T210)
65
{
66
// Set offset to 0 oC for Erista.
67
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TMP_OFH_REG, 0);
68
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TMP_OFL_REG, 0);
69
}
70
else
71
{
72
// Set offset to -12.5 oC for Mariko.
73
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TMP_OFH_REG, 0xF3); // - 13 oC.
74
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TMP_OFL_REG, 0x80); // + 0.5 oC.
75
}
76
77
// Set conversion rate to 31 ms and make a read to update the reg.
78
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_CNV_RATE_REG, 9);
79
tmp451_get_soc_temp(false);
80
81
// Set rate to every 4 seconds.
82
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_CNV_RATE_REG, 2);
83
}
84
85
void tmp451_end()
86
{
87
// Place into shutdown mode to conserve power.
88
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_CONFIG_REG, 0xC0);
89
}
90
91