Picklock_RCM/bdk/thermal/fan.c

116 lines
2.9 KiB
C
Raw Permalink Normal View History

2020-06-26 22:17:06 +02:00
/*
* Fan driver for Nintendo Switch
*
2021-11-30 19:39:35 +01:00
* Copyright (c) 2018-2021 CTCaer
2020-06-26 22:17:06 +02: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/>.
*/
#include <thermal/fan.h>
#include <power/regulator_5v.h>
2021-11-30 19:39:35 +01:00
#include <soc/fuse.h>
2020-06-26 22:17:06 +02:00
#include <soc/gpio.h>
#include <soc/pinmux.h>
#include <soc/t210.h>
#include <utils/util.h>
void set_fan_duty(u32 duty)
{
static bool fan_init = false;
2021-05-12 23:38:34 +02:00
static u16 curr_duty = -1;
2020-06-26 22:17:06 +02:00
2021-11-30 19:39:35 +01:00
if (duty > 236)
duty = 236;
2020-06-26 22:17:06 +02:00
if (curr_duty == duty)
return;
2021-11-30 19:39:35 +01:00
curr_duty = duty;
//! TODO: Add HOAG/AULA support.
u32 hw_type = fuse_read_hw_type();
if (hw_type != FUSE_NX_HW_TYPE_ICOSA &&
hw_type != FUSE_NX_HW_TYPE_IOWA)
return;
2020-06-26 22:17:06 +02:00
if (!fan_init)
{
// Fan tachometer.
PINMUX_AUX(PINMUX_AUX_CAM1_PWDN) = PINMUX_TRISTATE | PINMUX_INPUT_ENABLE | PINMUX_PULL_UP | 1;
gpio_config(GPIO_PORT_S, GPIO_PIN_7, GPIO_MODE_GPIO);
gpio_output_enable(GPIO_PORT_S, GPIO_PIN_7, GPIO_OUTPUT_DISABLE);
PWM(PWM_CONTROLLER_PWM_CSR_1) = PWM_CSR_EN | (0x100 << 16); // Max PWM to disable fan.
2020-06-26 22:17:06 +02:00
PINMUX_AUX(PINMUX_AUX_LCD_GPIO2) = 1; // Set source to PWM1.
gpio_config(GPIO_PORT_V, GPIO_PIN_4, GPIO_MODE_SPIO); // Fan power mode.
fan_init = true;
}
// Inverted polarity.
u32 inv_duty = 236 - duty;
// If disabled send a 0 duty.
if (inv_duty == 236)
{
PWM(PWM_CONTROLLER_PWM_CSR_1) = PWM_CSR_EN | (0x100 << 16); // Bit 24 is absolute 0%.
2021-05-12 23:38:34 +02:00
regulator_5v_disable(REGULATOR_5V_FAN);
2020-06-26 22:17:06 +02:00
// Disable fan.
PINMUX_AUX(PINMUX_AUX_LCD_GPIO2) =
PINMUX_INPUT_ENABLE | PINMUX_PARKED | PINMUX_TRISTATE | PINMUX_PULL_DOWN; // Set source to PWM1.
}
else // Set PWM duty.
{
// Fan power supply.
2021-05-12 23:38:34 +02:00
regulator_5v_enable(REGULATOR_5V_FAN);
2020-06-26 22:17:06 +02:00
PWM(PWM_CONTROLLER_PWM_CSR_1) = PWM_CSR_EN | (inv_duty << 16);
// Enable fan.
PINMUX_AUX(PINMUX_AUX_LCD_GPIO2) = 1; // Set source to PWM1.
}
}
void get_fan_speed(u32 *duty, u32 *rpm)
{
if (rpm)
{
2021-05-12 23:38:34 +02:00
u32 irq_count = 0;
2020-06-26 22:17:06 +02:00
bool should_read = true;
2021-05-12 23:38:34 +02:00
// Poll irqs for 2 seconds. (5 seconds for accurate count).
int timer = get_tmr_us() + 2000000;
while ((timer - get_tmr_us()) > 0)
2020-06-26 22:17:06 +02:00
{
2021-05-12 23:38:34 +02:00
bool irq_val = gpio_read(GPIO_PORT_S, GPIO_PIN_7);
2020-06-26 22:17:06 +02:00
if (irq_val && should_read)
{
irq_count++;
should_read = false;
}
else if (!irq_val)
should_read = true;
}
2021-05-12 23:38:34 +02:00
// Halve the irq count.
irq_count /= 2;
2020-06-26 22:17:06 +02:00
// Calculate rpm based on triggered interrupts.
2021-05-12 23:38:34 +02:00
*rpm = irq_count * (60 / 2);
2020-06-26 22:17:06 +02:00
}
if (duty)
*duty = 236 - ((PWM(PWM_CONTROLLER_PWM_CSR_1) >> 16) & 0xFF);
}