Atmosphere/stratosphere/boot/source/i2c/driver/impl/i2c_resource_manager.cpp

179 lines
6.1 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* 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 <switch.h>
#include <stratosphere.hpp>
#include "i2c_pcv.hpp"
#include "i2c_resource_manager.hpp"
namespace sts::i2c::driver::impl {
void ResourceManager::Initialize() {
2019-09-24 10:15:36 +00:00
std::scoped_lock lk(this->initialize_mutex);
this->ref_cnt++;
}
void ResourceManager::Finalize() {
2019-09-24 10:15:36 +00:00
std::scoped_lock lk(this->initialize_mutex);
STS_ASSERT(this->ref_cnt > 0);
this->ref_cnt--;
if (this->ref_cnt > 0) {
return;
}
{
2019-09-24 10:15:36 +00:00
std::scoped_lock sess_lk(this->session_open_mutex);
for (size_t i = 0; i < MaxDriverSessions; i++) {
this->sessions[i].Close();
}
}
}
size_t ResourceManager::GetFreeSessionId() const {
for (size_t i = 0; i < MaxDriverSessions; i++) {
if (!this->sessions[i].IsOpen()) {
return i;
}
}
return InvalidSessionId;
}
void ResourceManager::OpenSession(driver::Session *out_session, Bus bus, u32 slave_address, AddressingMode addressing_mode, SpeedMode speed_mode, u32 max_retries, u64 retry_wait_time) {
bool need_enable_ldo6 = false;
size_t session_id = InvalidSessionId;
/* Get, open session. */
{
2019-09-24 10:15:36 +00:00
std::scoped_lock lk(this->session_open_mutex);
STS_ASSERT(out_session != nullptr);
STS_ASSERT(bus < Bus::Count);
session_id = GetFreeSessionId();
STS_ASSERT(session_id != InvalidSessionId);
if ((bus == Bus::I2C2 || bus == Bus::I2C3) && (this->bus_accessors[ConvertToIndex(Bus::I2C2)].GetOpenSessions() == 0 && this->bus_accessors[ConvertToIndex(Bus::I2C3)].GetOpenSessions() == 0)) {
need_enable_ldo6 = true;
}
out_session->session_id = session_id;
out_session->bus_idx = ConvertToIndex(bus);
this->sessions[session_id].Open(bus, slave_address, addressing_mode, speed_mode, &this->bus_accessors[ConvertToIndex(bus)], max_retries, retry_wait_time);
}
this->sessions[session_id].Start();
if (need_enable_ldo6) {
pcv::Initialize();
R_ASSERT(pcv::SetVoltageValue(10, 2'900'000));
R_ASSERT(pcv::SetVoltageEnabled(10, true));
pcv::Finalize();
svcSleepThread(560'000ul);
}
}
void ResourceManager::CloseSession(const driver::Session &session) {
bool need_disable_ldo6 = false;
/* Get, open session. */
{
2019-09-24 10:15:36 +00:00
std::scoped_lock lk(this->session_open_mutex);
STS_ASSERT(this->sessions[session.session_id].IsOpen());
this->sessions[session.session_id].Close();
if ((ConvertFromIndex(session.bus_idx) == Bus::I2C2 || ConvertFromIndex(session.bus_idx) == Bus::I2C3) &&
(this->bus_accessors[ConvertToIndex(Bus::I2C2)].GetOpenSessions() == 0 && this->bus_accessors[ConvertToIndex(Bus::I2C3)].GetOpenSessions() == 0)) {
need_disable_ldo6 = true;
}
}
if (need_disable_ldo6) {
pcv::Initialize();
R_ASSERT(pcv::SetVoltageEnabled(10, false));
pcv::Finalize();
}
}
void ResourceManager::SuspendBuses() {
STS_ASSERT(this->ref_cnt > 0);
if (!this->suspended) {
{
2019-09-24 10:15:36 +00:00
std::scoped_lock lk(this->session_open_mutex);
this->suspended = true;
for (size_t i = 0; i < ConvertToIndex(Bus::Count); i++) {
if (i != PowerBusId && this->bus_accessors[i].GetOpenSessions() > 0) {
this->bus_accessors[i].Suspend();
}
}
}
pcv::Initialize();
R_ASSERT(pcv::SetVoltageEnabled(10, false));
pcv::Finalize();
}
}
void ResourceManager::ResumeBuses() {
STS_ASSERT(this->ref_cnt > 0);
if (this->suspended) {
if (this->bus_accessors[ConvertToIndex(Bus::I2C2)].GetOpenSessions() > 0 || this->bus_accessors[ConvertToIndex(Bus::I2C3)].GetOpenSessions() > 0) {
pcv::Initialize();
R_ASSERT(pcv::SetVoltageValue(10, 2'900'000));
R_ASSERT(pcv::SetVoltageEnabled(10, true));
pcv::Finalize();
svcSleepThread(1'560'000ul);
}
{
2019-09-24 10:15:36 +00:00
std::scoped_lock lk(this->session_open_mutex);
for (size_t i = 0; i < ConvertToIndex(Bus::Count); i++) {
if (i != PowerBusId && this->bus_accessors[i].GetOpenSessions() > 0) {
this->bus_accessors[i].Resume();
}
}
}
this->suspended = false;
}
}
void ResourceManager::SuspendPowerBus() {
STS_ASSERT(this->ref_cnt > 0);
2019-09-24 10:15:36 +00:00
std::scoped_lock lk(this->session_open_mutex);
if (!this->power_bus_suspended) {
this->power_bus_suspended = true;
if (this->bus_accessors[PowerBusId].GetOpenSessions() > 0) {
this->bus_accessors[PowerBusId].Suspend();
}
}
}
void ResourceManager::ResumePowerBus() {
STS_ASSERT(this->ref_cnt > 0);
2019-09-24 10:15:36 +00:00
std::scoped_lock lk(this->session_open_mutex);
if (this->power_bus_suspended) {
if (this->bus_accessors[PowerBusId].GetOpenSessions() > 0) {
this->bus_accessors[PowerBusId].Resume();
}
this->power_bus_suspended = false;
}
}
}