Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/power/regulator_5v.c
1476 views
1
/*
2
* Copyright (c) 2019-2023 CTCaer
3
*
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms and conditions of the GNU General Public License,
6
* version 2, as published by the Free Software Foundation.
7
*
8
* This program is distributed in the hope it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11
* more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15
*/
16
17
#include <soc/fuse.h>
18
#include <soc/gpio.h>
19
#include <soc/hw_init.h>
20
#include <soc/pinmux.h>
21
#include <soc/pmc.h>
22
#include <soc/t210.h>
23
#include <utils/types.h>
24
25
static u8 reg_5v_dev = 0;
26
static bool usb_src = false;
27
28
void regulator_5v_enable(u8 dev)
29
{
30
bool tegra_t210 = hw_get_chip_id() == GP_HIDREV_MAJOR_T210;
31
32
// The power supply selection from battery or USB is automatic.
33
if (!reg_5v_dev)
34
{
35
// Fan and Rail power from battery 5V regulator EN.
36
PINMUX_AUX(PINMUX_AUX_SATA_LED_ACTIVE) = 1;
37
gpio_direction_output(GPIO_PORT_A, GPIO_PIN_5, GPIO_HIGH);
38
39
// Only Icosa has USB 5V VBUS rails.
40
if (tegra_t210)
41
{
42
// Fan and Rail power from USB 5V VBUS EN.
43
PINMUX_AUX(PINMUX_AUX_USB_VBUS_EN0) = PINMUX_LPDR | 1;
44
gpio_direction_output(GPIO_PORT_CC, GPIO_PIN_4, GPIO_LOW);
45
}
46
47
// Make sure GPIO IO power is enabled.
48
PMC(APBDEV_PMC_NO_IOPOWER) &= ~PMC_NO_IOPOWER_GPIO;
49
(void)PMC(APBDEV_PMC_NO_IOPOWER); // Commit write.
50
51
// Inform GPIO IO pads that we switched to 1.8V.
52
PMC(APBDEV_PMC_PWR_DET_VAL) &= ~PMC_PWR_DET_33V_GPIO;
53
(void)PMC(APBDEV_PMC_PWR_DET_VAL); // Commit write.
54
55
usb_src = false;
56
}
57
reg_5v_dev |= dev;
58
}
59
60
void regulator_5v_disable(u8 dev)
61
{
62
bool tegra_t210 = hw_get_chip_id() == GP_HIDREV_MAJOR_T210;
63
64
reg_5v_dev &= ~dev;
65
66
if (!reg_5v_dev)
67
{
68
// Rail power from battery 5V regulator.
69
gpio_write(GPIO_PORT_A, GPIO_PIN_5, GPIO_LOW);
70
71
// Only Icosa has USB 5V VBUS rails.
72
if (tegra_t210)
73
{
74
// Rail power from USB 5V VBUS.
75
gpio_write(GPIO_PORT_CC, GPIO_PIN_4, GPIO_LOW);
76
usb_src = false;
77
78
}
79
}
80
}
81
82
bool regulator_5v_get_dev_enabled(u8 dev)
83
{
84
return (reg_5v_dev & dev);
85
}
86
87
void regulator_5v_usb_src_enable(bool enable)
88
{
89
// Only for Icosa.
90
if (hw_get_chip_id() != GP_HIDREV_MAJOR_T210)
91
return;
92
93
if (enable && !usb_src)
94
{
95
gpio_write(GPIO_PORT_CC, GPIO_PIN_4, GPIO_HIGH);
96
usb_src = true;
97
}
98
else if (!enable && usb_src)
99
{
100
gpio_write(GPIO_PORT_CC, GPIO_PIN_4, GPIO_LOW);
101
usb_src = false;
102
}
103
}
104
105