1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-11-29 15:22:09 +00:00
TegraExplorer/bdk/power/regulator_5v.c

126 lines
3.7 KiB
C
Raw Normal View History

2020-05-01 21:42:49 +01:00
/*
2023-07-22 17:37:52 +01:00
* Copyright (c) 2019-2021 CTCaer
2020-05-01 21:42:49 +01:00
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2023-07-22 17:37:52 +01:00
#include <soc/fuse.h>
#include <soc/gpio.h>
2023-07-22 17:37:52 +01:00
#include <soc/hw_init.h>
#include <soc/pinmux.h>
#include <soc/pmc.h>
#include <soc/t210.h>
#include <utils/types.h>
2020-05-01 21:42:49 +01:00
static u8 reg_5v_dev = 0;
2023-07-22 17:37:52 +01:00
static bool usb_src = false;
2020-05-01 21:42:49 +01:00
2021-01-11 23:30:14 +00:00
void regulator_5v_enable(u8 dev)
2020-05-01 21:42:49 +01:00
{
// The power supply selection from battery or USB is automatic.
if (!reg_5v_dev)
{
2023-07-22 17:37:52 +01:00
// Fan and Rail power from battery 5V regulator.
2020-05-01 21:42:49 +01:00
PINMUX_AUX(PINMUX_AUX_SATA_LED_ACTIVE) = 1;
gpio_config(GPIO_PORT_A, GPIO_PIN_5, GPIO_MODE_GPIO);
gpio_output_enable(GPIO_PORT_A, GPIO_PIN_5, GPIO_OUTPUT_ENABLE);
gpio_write(GPIO_PORT_A, GPIO_PIN_5, GPIO_HIGH);
2023-07-22 17:37:52 +01:00
// Only Icosa and Iowa have USB 5V VBUS rails. Skip on Hoag/Aula.
u32 hw_type = fuse_read_hw_type();
if (hw_type == FUSE_NX_HW_TYPE_ICOSA ||
hw_type == FUSE_NX_HW_TYPE_IOWA)
{
// Fan and Rail power from USB 5V VBUS.
PINMUX_AUX(PINMUX_AUX_USB_VBUS_EN0) = PINMUX_LPDR | 1;
gpio_config(GPIO_PORT_CC, GPIO_PIN_4, GPIO_MODE_GPIO);
gpio_output_enable(GPIO_PORT_CC, GPIO_PIN_4, GPIO_OUTPUT_ENABLE);
gpio_write(GPIO_PORT_CC, GPIO_PIN_4, GPIO_LOW);
}
// Enable GPIO AO IO rail for T210.
if (hw_get_chip_id() == GP_HIDREV_MAJOR_T210)
{
// Make sure GPIO power is enabled.
PMC(APBDEV_PMC_NO_IOPOWER) &= ~PMC_NO_IOPOWER_GPIO_IO_EN;
(void)PMC(APBDEV_PMC_NO_IOPOWER); // Commit write.
2020-05-01 21:42:49 +01:00
2023-07-22 17:37:52 +01:00
// Override power detect for GPIO AO IO rails.
PMC(APBDEV_PMC_PWR_DET_VAL) &= ~PMC_PWR_DET_GPIO_IO_EN;
(void)PMC(APBDEV_PMC_PWR_DET_VAL); // Commit write.
}
usb_src = false;
2020-05-01 21:42:49 +01:00
}
reg_5v_dev |= dev;
}
2021-01-11 23:30:14 +00:00
void regulator_5v_disable(u8 dev)
2020-05-01 21:42:49 +01:00
{
reg_5v_dev &= ~dev;
if (!reg_5v_dev)
{
2023-07-22 17:37:52 +01:00
// Rail power from battery 5V regulator.
2020-05-01 21:42:49 +01:00
gpio_write(GPIO_PORT_A, GPIO_PIN_5, GPIO_LOW);
gpio_output_enable(GPIO_PORT_A, GPIO_PIN_5, GPIO_OUTPUT_DISABLE);
gpio_config(GPIO_PORT_A, GPIO_PIN_5, GPIO_MODE_SPIO);
PINMUX_AUX(PINMUX_AUX_SATA_LED_ACTIVE) = PINMUX_PARKED | PINMUX_INPUT_ENABLE;
2023-07-22 17:37:52 +01:00
// Only Icosa and Iowa have USB 5V VBUS rails. Skip on Hoag/Aula.
u32 hw_type = fuse_read_hw_type();
if (hw_type == FUSE_NX_HW_TYPE_ICOSA ||
hw_type == FUSE_NX_HW_TYPE_IOWA)
{
// Rail power from USB 5V VBUS.
gpio_write(GPIO_PORT_CC, GPIO_PIN_4, GPIO_LOW);
gpio_output_enable(GPIO_PORT_CC, GPIO_PIN_4, GPIO_OUTPUT_DISABLE);
gpio_config(GPIO_PORT_CC, GPIO_PIN_4, GPIO_MODE_SPIO);
PINMUX_AUX(PINMUX_AUX_USB_VBUS_EN0) = PINMUX_IO_HV | PINMUX_LPDR | PINMUX_PARKED | PINMUX_INPUT_ENABLE;
usb_src = false;
}
2020-05-01 21:42:49 +01:00
// GPIO AO IO rails.
2023-07-22 17:37:52 +01:00
if (hw_get_chip_id() == GP_HIDREV_MAJOR_T210)
{
PMC(APBDEV_PMC_PWR_DET_VAL) |= PMC_PWR_DET_GPIO_IO_EN;
(void)PMC(APBDEV_PMC_PWR_DET_VAL); // Commit write.
}
2020-05-01 21:42:49 +01:00
}
}
2021-01-11 23:30:14 +00:00
bool regulator_5v_get_dev_enabled(u8 dev)
2020-05-01 21:42:49 +01:00
{
return (reg_5v_dev & dev);
}
2021-01-11 23:30:14 +00:00
2023-07-22 17:37:52 +01:00
void regulator_5v_usb_src_enable(bool enable)
2021-01-11 23:30:14 +00:00
{
2023-07-22 17:37:52 +01:00
// Only for Icosa/Iowa. Skip on Hoag/Aula.
u32 hw_type = fuse_read_hw_type();
if (hw_type != FUSE_NX_HW_TYPE_ICOSA &&
hw_type != FUSE_NX_HW_TYPE_IOWA)
return;
if (enable && !usb_src)
2021-06-24 20:44:59 +01:00
{
2023-07-22 17:37:52 +01:00
gpio_write(GPIO_PORT_CC, GPIO_PIN_4, GPIO_HIGH);
usb_src = true;
2021-06-24 20:44:59 +01:00
}
2023-07-22 17:37:52 +01:00
else if (!enable && usb_src)
2021-06-24 20:44:59 +01:00
{
2023-07-22 17:37:52 +01:00
gpio_write(GPIO_PORT_CC, GPIO_PIN_4, GPIO_LOW);
usb_src = false;
2021-06-24 20:44:59 +01:00
}
2021-01-11 23:30:14 +00:00
}