Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/thermal/tmp451.c
1476 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
u16 tmp451_get_soc_temp(bool intenger)
25
{
26
u8 val;
27
u16 temp = 0;
28
29
val = i2c_recv_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TEMP_REG);
30
if (intenger)
31
return val;
32
33
temp = val << 8;
34
val = i2c_recv_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TMP_DEC_REG);
35
temp |= ((val >> 4) * 625) / 100;
36
37
return temp;
38
}
39
40
u16 tmp451_get_pcb_temp(bool intenger)
41
{
42
u8 val;
43
u16 temp = 0;
44
45
val = i2c_recv_byte(I2C_1, TMP451_I2C_ADDR, TMP451_PCB_TEMP_REG);
46
if (intenger)
47
return val;
48
49
temp = val << 8;
50
val = i2c_recv_byte(I2C_1, TMP451_I2C_ADDR, TMP451_PCB_TMP_DEC_REG);
51
temp |= ((val >> 4) * 625) / 100;
52
53
return temp;
54
}
55
56
void tmp451_init()
57
{
58
// Disable ALARM and Range to 0 - 127 oC.
59
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_CONFIG_REG, 0x80);
60
61
// Set remote sensor offsets based on SoC.
62
if (hw_get_chip_id() == GP_HIDREV_MAJOR_T210)
63
{
64
// Set offset to 0 oC for Erista.
65
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TMP_OFH_REG, 0);
66
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TMP_OFL_REG, 0);
67
}
68
else
69
{
70
// Set offset to -12.5 oC for Mariko.
71
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TMP_OFH_REG, 0xF3); // - 13 oC.
72
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_SOC_TMP_OFL_REG, 0x80); // + 0.5 oC.
73
}
74
75
// Set conversion rate to 32/s and make a read to update the reg.
76
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_CNV_RATE_REG, 9);
77
tmp451_get_soc_temp(false);
78
79
// Set rate to every 4 seconds.
80
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_CNV_RATE_REG, 2);
81
}
82
83
void tmp451_end()
84
{
85
// Place into shutdown mode to conserve power.
86
i2c_send_byte(I2C_1, TMP451_I2C_ADDR, TMP451_CONFIG_REG, 0xC0);
87
}
88
89