powctl: implement client api (needs board-specific impl)

This commit is contained in:
Michael Scire
2020-11-02 18:13:36 -08:00
parent aa63b1eab7
commit cd7d7894f1
37 changed files with 1984 additions and 11 deletions

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2018-2020 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 <stratosphere.hpp>
#include "powctl_board_impl.hpp"
namespace ams::powctl::impl::board::nintendo_nx {
void Initialize(bool use_event_handlers) {
/* TODO */
AMS_ABORT();
}
void Finalize() {
/* TODO */
AMS_ABORT();
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (c) 2018-2020 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "powctl_interrupt_event_handler.hpp"
namespace ams::powctl::impl::board::nintendo_nx {
void Initialize(bool use_event_handlers);
void Finalize();
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2018-2020 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 <stratosphere.hpp>
#include "powctl_interrupt_event_handler.hpp"
namespace ams::powctl::impl::board::nintendo_nx {
void ChargerInterruptEventHandler::SignalEvent(IDevice *device) {
/* TODO */
AMS_ABORT();
}
void BatteryInterruptEventHandler::SignalEvent(IDevice *device) {
/* TODO */
AMS_ABORT();
}
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (c) 2018-2020 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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "../../powctl_i_power_control_driver.hpp"
namespace ams::powctl::impl::board::nintendo_nx {
template<typename Derived>
class InterruptEventHandler : public ddsf::IEventHandler {
private:
IDevice *device;
gpio::GpioPadSession gpio_session;
os::SystemEventType gpio_system_event;
os::SdkMutex mutex;
public:
InterruptEventHandler(IDevice *dv) : IEventHandler(), device(dv), mutex() {
/* Initialize the gpio session. */
Derived::Initialize(std::addressof(this->gpio_session), std::addressof(this->gpio_system_event));
/* Initialize ourselves as an event handler. */
IEventHandler::Initialize(std::addressof(this->gpio_system_event));
}
os::SystemEventType *GetSystemEvent() {
return std::addressof(this->gpio_system_event);
}
virtual void HandleEvent() override final {
/* Acquire exclusive access to ourselves. */
std::scoped_lock lk(this->mutex);
/* Clear our interrupt status. */
gpio::ClearInterruptStatus(std::addressof(this->gpio_session));
/* Clear our system event. */
os::ClearSystemEvent(std::addressof(this->gpio_system_event));
/* Signal the event. */
Derived::SignalEvent(this->device);
}
};
class ChargerInterruptEventHandler : public InterruptEventHandler<ChargerInterruptEventHandler> {
friend class InterruptEventHandler<ChargerInterruptEventHandler>;
private:
static void Initialize(gpio::GpioPadSession *session, os::SystemEventType *event) {
/* Open the gpio session. */
R_ABORT_UNLESS(gpio::OpenSession(session, gpio::DeviceCode_Bq24190Irq));
/* Configure the gpio session. */
gpio::SetDirection(session, gpio::Direction_Input);
gpio::SetInterruptMode(session, gpio::InterruptMode_FallingEdge);
gpio::SetInterruptEnable(session, true);
/* Bind the interrupt event. */
R_ABORT_UNLESS(gpio::BindInterrupt(event, session));
}
void SignalEvent(IDevice *device);
};
class BatteryInterruptEventHandler : public InterruptEventHandler<BatteryInterruptEventHandler> {
friend class InterruptEventHandler<BatteryInterruptEventHandler>;
private:
static void Initialize(gpio::GpioPadSession *session, os::SystemEventType *event) {
/* Open the gpio session. */
R_ABORT_UNLESS(gpio::OpenSession(session, gpio::DeviceCode_BattMgicIrq));
/* Configure the gpio session. */
gpio::SetDirection(session, gpio::Direction_Input);
gpio::SetInterruptMode(session, gpio::InterruptMode_LowLevel);
/* Bind the interrupt event. */
R_ABORT_UNLESS(gpio::BindInterrupt(event, session));
}
void SignalEvent(IDevice *device);
};
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (c) 2018-2020 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::powctl::impl {
constexpr inline const TimeSpan PowerControlRetryTimeout = TimeSpan::FromSeconds(10);
constexpr inline const TimeSpan PowerControlRetryInterval = TimeSpan::FromMilliSeconds(20);
#define AMS_POWCTL_R_TRY_WITH_RETRY(__EXPR__) \
({ \
TimeSpan __powctl_retry_current_time = 0; \
while (true) { \
const Result __powctl_retry_result = (__EXPR__); \
if (R_SUCCEEDED(__powctl_retry_result)) { \
break; \
} \
\
__powctl_retry_current_time += PowerControlRetryInterval; \
R_UNLESS(__powctl_retry_current_time < PowerControlRetryTimeout, __powctl_retry_result); \
\
os::SleepThread(PowerControlRetryInterval); \
} \
})
}