Revert "hoc-clk: add live vdd2, live boost clock and basic pwm dimming"
This reverts commit 15b7df8ef1.
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 "pwm_impl_pwm_driver_api.hpp"
|
||||
#include "pwm_pwm_driver_impl.hpp"
|
||||
|
||||
namespace ams::pwm::driver::board::nintendo::nx::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr inline const dd::PhysicalAddress PwmRegistersPhysicalAddress = 0x7000A000;
|
||||
constexpr inline size_t PwmRegistersSize = 0x100;
|
||||
|
||||
constexpr const ChannelDefinition SupportedChannels[] = {
|
||||
{ pwm::DeviceCode_LcdBacklight, 0 },
|
||||
{ pwm::DeviceCode_CpuFan, 1 },
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Result InitializePwmDriver() {
|
||||
/* Get the memory resource with which to allocate our driver/devices. */
|
||||
auto *memory_resource = ddsf::GetMemoryResource();
|
||||
|
||||
/* Allocate storage for our driver. */
|
||||
auto *driver_storage = memory_resource->Allocate(sizeof(PwmDriverImpl), alignof(PwmDriverImpl));
|
||||
AMS_ABORT_UNLESS(driver_storage != nullptr);
|
||||
|
||||
/* Create our driver. */
|
||||
auto *driver = std::construct_at(static_cast<PwmDriverImpl *>(driver_storage), PwmRegistersPhysicalAddress, PwmRegistersSize, SupportedChannels, util::size(SupportedChannels));
|
||||
|
||||
/* Register our driver. */
|
||||
pwm::driver::RegisterDriver(driver);
|
||||
|
||||
/* Create our devices. */
|
||||
for (const auto &entry : SupportedChannels) {
|
||||
auto *device_storage = memory_resource->Allocate(sizeof(PwmDriverImpl), alignof(PwmDriverImpl));
|
||||
AMS_ABORT_UNLESS(device_storage != nullptr);
|
||||
|
||||
/* Create our driver. */
|
||||
auto *device = std::construct_at(static_cast<PwmDeviceImpl *>(device_storage), entry.channel_id);
|
||||
|
||||
/* Register the device with our driver. */
|
||||
driver->RegisterDevice(device);
|
||||
|
||||
/* Register the device code with our driver. */
|
||||
R_ABORT_UNLESS(pwm::driver::RegisterDeviceCode(entry.device_code, device));
|
||||
}
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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::pwm::driver::board::nintendo::nx::impl {
|
||||
|
||||
struct ChannelDefinition {
|
||||
DeviceCode device_code;
|
||||
int channel_id;
|
||||
};
|
||||
|
||||
Result InitializePwmDriver();
|
||||
|
||||
}
|
||||
@@ -1,263 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 "pwm_pwm_driver_impl.hpp"
|
||||
|
||||
namespace ams::pwm::driver::board::nintendo::nx::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr inline u32 PwmClockRateHz = 45'333'333;
|
||||
|
||||
constexpr inline TimeSpan DefaultChannelPeriod = TimeSpan::FromMilliSeconds(10);
|
||||
|
||||
constexpr inline int MaxDuty = 0x100;
|
||||
|
||||
template<typename T>
|
||||
T DivideRoundUp(T a, T b) {
|
||||
return (a + (b / 2)) / b;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
PwmDriverImpl::PwmDriverImpl(dd::PhysicalAddress paddr, size_t sz, const ChannelDefinition *c, size_t nc) : m_registers_phys_addr(paddr), m_registers_size(sz), m_channels(c), m_num_channels(nc), m_registers(0) {
|
||||
/* ... */
|
||||
}
|
||||
|
||||
void PwmDriverImpl::PowerOn() {
|
||||
/* Initialize pcv driver. */
|
||||
pcv::Initialize();
|
||||
|
||||
/* Setup clock/power for pwm. */
|
||||
R_ABORT_UNLESS(pcv::SetReset(pcv::Module_Pwm, true));
|
||||
R_ABORT_UNLESS(pcv::SetClockEnabled(pcv::Module_Pwm, true));
|
||||
R_ABORT_UNLESS(pcv::SetClockRate(pcv::Module_Pwm, PwmClockRateHz));
|
||||
R_ABORT_UNLESS(pcv::SetReset(pcv::Module_Pwm, false));
|
||||
}
|
||||
|
||||
void PwmDriverImpl::PowerOff() {
|
||||
/* Disable clock and hold pwm in reset. */
|
||||
/* NOTE: Nintendo does not check this succeeds. */
|
||||
pcv::SetClockEnabled(pcv::Module_Pwm, false);
|
||||
pcv::SetReset(pcv::Module_Pwm, true);
|
||||
|
||||
/* Finalize pcv driver. */
|
||||
pcv::Finalize();
|
||||
}
|
||||
|
||||
void PwmDriverImpl::InitializeDriver() {
|
||||
/* Get the registers virtual address. */
|
||||
m_registers = dd::QueryIoMapping(m_registers_phys_addr, m_registers_size);
|
||||
AMS_ABORT_UNLESS(m_registers != 0);
|
||||
|
||||
/* Setup power to pwm. */
|
||||
this->PowerOn();
|
||||
}
|
||||
|
||||
void PwmDriverImpl::FinalizeDriver() {
|
||||
/* Shut down power to pwm. */
|
||||
this->PowerOff();
|
||||
}
|
||||
|
||||
Result PwmDriverImpl::InitializeDevice(IPwmDevice *device) {
|
||||
/* Validate the device. */
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Configure initial settings. */
|
||||
/* NOTE: None of these results are checked. */
|
||||
this->SetEnabled(device, false);
|
||||
this->SetScale(device, 0.0);
|
||||
this->SetPeriod(device, DefaultChannelPeriod);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void PwmDriverImpl::FinalizeDevice(IPwmDevice *device) {
|
||||
/* Validate the device. */
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Nothing to do here. */
|
||||
AMS_UNUSED(device);
|
||||
}
|
||||
|
||||
Result PwmDriverImpl::SetPeriod(IPwmDevice *device, TimeSpan period) {
|
||||
/* Validate the device. */
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Verify the period is valid. */
|
||||
const auto ns = period.GetNanoSeconds();
|
||||
R_UNLESS(ns > 0, pwm::ResultInvalidArgument());
|
||||
|
||||
/* Convert the ns to a desired frequency (rounding up). */
|
||||
const auto hz = DivideRoundUp(TimeSpan::FromSeconds(1).GetNanoSeconds(), ns);
|
||||
R_UNLESS(hz > 0, pwm::ResultInvalidArgument());
|
||||
|
||||
/* Convert the frequency to a pfm value. */
|
||||
const u32 pfm = std::min<u32>(std::max<u32>(DivideRoundUp<u64>(PwmClockRateHz, hz * 256), 1) - 1, 0x1FFF);
|
||||
|
||||
/* Acquire exclusive access to the device registers. */
|
||||
std::scoped_lock lk(device->SafeCastTo<PwmDeviceImpl>());
|
||||
|
||||
/* Update the period. */
|
||||
reg::ReadWrite(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_VALUE(PWM_CSR_PFM, pfm));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result PwmDriverImpl::GetPeriod(TimeSpan *out, IPwmDevice *device) {
|
||||
/* Validate the device. */
|
||||
AMS_ASSERT(out != nullptr);
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Get the pfm value. */
|
||||
const u32 pfm = reg::GetValue(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_MASK(PWM_CSR_PFM));
|
||||
|
||||
/* Convert it to a frequency. */
|
||||
/* pfm = ((ClockRate / (hz * 256)) - 1) -> hz = (ClockRate / ((pfm + 1) * 256)) */
|
||||
const auto hz = DivideRoundUp<s64>(PwmClockRateHz, (pfm + 1) * 256);
|
||||
|
||||
/* Convert the frequency to a period. */
|
||||
const auto ns = DivideRoundUp(TimeSpan::FromSeconds(1).GetNanoSeconds(), hz);
|
||||
|
||||
/* Set the output. */
|
||||
*out = TimeSpan::FromNanoSeconds(ns);
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result PwmDriverImpl::SetDuty(IPwmDevice *device, int duty) {
|
||||
/* Validate the device. */
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Validate the duty. */
|
||||
R_UNLESS(0 <= duty && duty <= MaxDuty, pwm::ResultInvalidArgument());
|
||||
|
||||
/* Acquire exclusive access to the device registers. */
|
||||
std::scoped_lock lk(device->SafeCastTo<PwmDeviceImpl>());
|
||||
|
||||
/* Update the duty. */
|
||||
reg::ReadWrite(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_VALUE(PWM_CSR_PWM, static_cast<u32>(duty)));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result PwmDriverImpl::GetDuty(int *out, IPwmDevice *device) {
|
||||
/* Validate the device. */
|
||||
AMS_ASSERT(out != nullptr);
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Get the duty. */
|
||||
*out = static_cast<int>(reg::GetValue(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_MASK(PWM_CSR_PWM)));
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result PwmDriverImpl::SetScale(IPwmDevice *device, double scale) {
|
||||
/* Validate the device. */
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Convert the scale to a duty. */
|
||||
const int duty = static_cast<int>(((scale * 256.0) / 100.0) + 0.5);
|
||||
|
||||
/* Validate the duty. */
|
||||
R_UNLESS(0 <= duty && duty <= MaxDuty, pwm::ResultInvalidArgument());
|
||||
|
||||
/* Acquire exclusive access to the device registers. */
|
||||
std::scoped_lock lk(device->SafeCastTo<PwmDeviceImpl>());
|
||||
|
||||
/* Update the duty. */
|
||||
reg::ReadWrite(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_VALUE(PWM_CSR_PWM, static_cast<u32>(duty)));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result PwmDriverImpl::GetScale(double *out, IPwmDevice *device) {
|
||||
/* Validate the device. */
|
||||
AMS_ASSERT(out != nullptr);
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Get the duty. */
|
||||
const int duty = static_cast<int>(reg::GetValue(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_MASK(PWM_CSR_PWM)));
|
||||
|
||||
/* Convert to scale. */
|
||||
*out = (static_cast<double>(duty) * 100.0) / 256.0;
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result PwmDriverImpl::SetEnabled(IPwmDevice *device, bool en) {
|
||||
/* Validate the device. */
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Acquire exclusive access to the device registers. */
|
||||
std::scoped_lock lk(device->SafeCastTo<PwmDeviceImpl>());
|
||||
|
||||
/* Update the enable. */
|
||||
reg::ReadWrite(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_ENUM_SEL(PWM_CSR_ENB, en, ENABLE, DISABLE));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result PwmDriverImpl::GetEnabled(bool *out, IPwmDevice *device) {
|
||||
/* Validate the device. */
|
||||
AMS_ASSERT(out != nullptr);
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Get the enable. */
|
||||
*out = reg::HasValue(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_ENUM(PWM_CSR_ENB, ENABLE));
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result PwmDriverImpl::Suspend() {
|
||||
/* Suspend each device. */
|
||||
this->ForEachDevice([&](ddsf::IDevice &device) -> bool {
|
||||
/* Convert the device to a pwm device. */
|
||||
auto &pwm_device = device.SafeCastTo<PwmDeviceImpl>();
|
||||
|
||||
/* Cache the suspend value. */
|
||||
pwm_device.SetSuspendValue(reg::Read(this->GetRegistersFor(pwm_device) + PWM_CONTROLLER_PWM_CSR));
|
||||
|
||||
/* Acquire exclusive access to the device. */
|
||||
std::scoped_lock lk(pwm_device);
|
||||
|
||||
/* Disable the device. */
|
||||
reg::ReadWrite(this->GetRegistersFor(pwm_device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_ENUM(PWM_CSR_ENB, DISABLE));
|
||||
|
||||
/* Continue to the next device. */
|
||||
return true;
|
||||
});
|
||||
|
||||
/* Disable clock to pwm. */
|
||||
R_RETURN(pcv::SetClockEnabled(pcv::Module_Pwm, false));
|
||||
}
|
||||
|
||||
void PwmDriverImpl::Resume() {
|
||||
/* Power on. */
|
||||
this->PowerOn();
|
||||
|
||||
/* Resume each device. */
|
||||
this->ForEachDevice([&](ddsf::IDevice &device) -> bool {
|
||||
/* Convert the device to a pwm device. */
|
||||
auto &pwm_device = device.SafeCastTo<PwmDeviceImpl>();
|
||||
|
||||
/* Acquire exclusive access to the device. */
|
||||
std::scoped_lock lk(pwm_device);
|
||||
|
||||
/* Write the device's suspend value. */
|
||||
reg::Write(this->GetRegistersFor(pwm_device) + PWM_CONTROLLER_PWM_CSR, pwm_device.GetSuspendValue());
|
||||
|
||||
/* Continue to the next device. */
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 "pwm_impl_pwm_driver_api.hpp"
|
||||
|
||||
namespace ams::pwm::driver::board::nintendo::nx::impl {
|
||||
|
||||
class PwmDeviceImpl : public ::ams::pwm::driver::IPwmDevice {
|
||||
NON_COPYABLE(PwmDeviceImpl);
|
||||
NON_MOVEABLE(PwmDeviceImpl);
|
||||
AMS_DDSF_CASTABLE_TRAITS(ams::pwm::driver::board::nintendo::nx::impl::PwmDeviceImpl, ::ams::pwm::driver::IPwmDevice);
|
||||
private:
|
||||
os::SdkMutex m_suspend_mutex;
|
||||
u32 m_suspend_value;
|
||||
public:
|
||||
PwmDeviceImpl(int channel) : IPwmDevice(channel), m_suspend_mutex(), m_suspend_value() { /* ... */ }
|
||||
|
||||
void SetSuspendValue(u32 v) { m_suspend_value = v; }
|
||||
u32 GetSuspendValue() const { return m_suspend_value; }
|
||||
|
||||
void lock() { return m_suspend_mutex.lock(); }
|
||||
void unlock() { return m_suspend_mutex.unlock(); }
|
||||
};
|
||||
|
||||
class PwmDriverImpl : public ::ams::pwm::driver::IPwmDriver {
|
||||
NON_COPYABLE(PwmDriverImpl);
|
||||
NON_MOVEABLE(PwmDriverImpl);
|
||||
AMS_DDSF_CASTABLE_TRAITS(ams::pwm::driver::board::nintendo::nx::impl::PwmDriverImpl, ::ams::pwm::driver::IPwmDriver);
|
||||
private:
|
||||
dd::PhysicalAddress m_registers_phys_addr;
|
||||
size_t m_registers_size;
|
||||
const ChannelDefinition *m_channels;
|
||||
size_t m_num_channels;
|
||||
uintptr_t m_registers;
|
||||
private:
|
||||
ALWAYS_INLINE uintptr_t GetRegistersFor(IPwmDevice &device) {
|
||||
return m_registers + PWM_CONTROLLER_PWM_CHANNEL_OFFSET(device.GetChannelIndex());
|
||||
}
|
||||
|
||||
ALWAYS_INLINE uintptr_t GetRegistersFor(IPwmDevice *device) {
|
||||
return this->GetRegistersFor(*device);
|
||||
}
|
||||
|
||||
void PowerOn();
|
||||
void PowerOff();
|
||||
public:
|
||||
PwmDriverImpl(dd::PhysicalAddress paddr, size_t sz, const ChannelDefinition *c, size_t nsc);
|
||||
|
||||
virtual void InitializeDriver() override;
|
||||
virtual void FinalizeDriver() override;
|
||||
|
||||
virtual Result InitializeDevice(IPwmDevice *device) override;
|
||||
virtual void FinalizeDevice(IPwmDevice *device) override;
|
||||
|
||||
virtual Result SetPeriod(IPwmDevice *device, TimeSpan period) override;
|
||||
virtual Result GetPeriod(TimeSpan *out, IPwmDevice *device) override;
|
||||
|
||||
virtual Result SetDuty(IPwmDevice *device, int duty) override;
|
||||
virtual Result GetDuty(int *out, IPwmDevice *device) override;
|
||||
|
||||
virtual Result SetScale(IPwmDevice *device, double scale) override;
|
||||
virtual Result GetScale(double *out, IPwmDevice *device) override;
|
||||
|
||||
virtual Result SetEnabled(IPwmDevice *device, bool en) override;
|
||||
virtual Result GetEnabled(bool *out, IPwmDevice *device) override;
|
||||
|
||||
virtual Result Suspend() override;
|
||||
virtual void Resume() override;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 "impl/pwm_impl_pwm_driver_api.hpp"
|
||||
|
||||
namespace ams::pwm::driver::board::nintendo::nx {
|
||||
|
||||
void Initialize() {
|
||||
R_ABORT_UNLESS(impl::InitializePwmDriver());
|
||||
/* TODO: R_ABORT_UNLESS(impl::InitializePmcDriver()); */
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 "pwm_driver_core.hpp"
|
||||
#include "pwm_channel_session_impl.hpp"
|
||||
|
||||
namespace ams::pwm::driver::impl {
|
||||
|
||||
Result ChannelSessionImpl::Open(IPwmDevice *device, ddsf::AccessMode access_mode) {
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Check if we're the device's first session. */
|
||||
const bool first = !device->HasAnyOpenSession();
|
||||
|
||||
/* Open the session. */
|
||||
R_TRY(ddsf::OpenSession(device, this, access_mode));
|
||||
auto guard = SCOPE_GUARD { ddsf::CloseSession(this); };
|
||||
|
||||
/* If we're the first session, initialize the device. */
|
||||
if (first) {
|
||||
R_TRY(device->GetDriver().SafeCastTo<IPwmDriver>().InitializeDevice(device));
|
||||
}
|
||||
|
||||
/* We're opened. */
|
||||
guard.Cancel();
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void ChannelSessionImpl::Close() {
|
||||
/* If we're not open, do nothing. */
|
||||
if (!this->IsOpen()) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get the device. */
|
||||
auto &device = this->GetDevice().SafeCastTo<IPwmDevice>();
|
||||
|
||||
/* Close the session. */
|
||||
ddsf::CloseSession(this);
|
||||
|
||||
/* If there are no remaining sessions, finalize the device. */
|
||||
if (!device.HasAnyOpenSession()) {
|
||||
device.GetDriver().SafeCastTo<IPwmDriver>().FinalizeDevice(std::addressof(device));
|
||||
}
|
||||
}
|
||||
|
||||
Result ChannelSessionImpl::SetPeriod(TimeSpan period) {
|
||||
/* Get the device. */
|
||||
IPwmDevice &device = this->GetDevice().SafeCastTo<IPwmDevice>();
|
||||
|
||||
/* Invoke the driver handler. */
|
||||
R_RETURN(device.GetDriver().SafeCastTo<IPwmDriver>().SetPeriod(std::addressof(device), period));
|
||||
}
|
||||
|
||||
Result ChannelSessionImpl::GetPeriod(TimeSpan *out) {
|
||||
/* Get the device. */
|
||||
IPwmDevice &device = this->GetDevice().SafeCastTo<IPwmDevice>();
|
||||
|
||||
/* Invoke the driver handler. */
|
||||
R_RETURN(device.GetDriver().SafeCastTo<IPwmDriver>().GetPeriod(out, std::addressof(device)));
|
||||
}
|
||||
|
||||
Result ChannelSessionImpl::SetDuty(int duty) {
|
||||
/* Get the device. */
|
||||
IPwmDevice &device = this->GetDevice().SafeCastTo<IPwmDevice>();
|
||||
|
||||
/* Invoke the driver handler. */
|
||||
R_RETURN(device.GetDriver().SafeCastTo<IPwmDriver>().SetDuty(std::addressof(device), duty));
|
||||
}
|
||||
|
||||
Result ChannelSessionImpl::GetDuty(int *out) {
|
||||
/* Get the device. */
|
||||
IPwmDevice &device = this->GetDevice().SafeCastTo<IPwmDevice>();
|
||||
|
||||
/* Invoke the driver handler. */
|
||||
R_RETURN(device.GetDriver().SafeCastTo<IPwmDriver>().GetDuty(out, std::addressof(device)));
|
||||
}
|
||||
|
||||
Result ChannelSessionImpl::SetEnabled(bool en) {
|
||||
/* Get the device. */
|
||||
IPwmDevice &device = this->GetDevice().SafeCastTo<IPwmDevice>();
|
||||
|
||||
/* Invoke the driver handler. */
|
||||
R_RETURN(device.GetDriver().SafeCastTo<IPwmDriver>().SetEnabled(std::addressof(device), en));
|
||||
}
|
||||
|
||||
Result ChannelSessionImpl::GetEnabled(bool *out) {
|
||||
/* Get the device. */
|
||||
IPwmDevice &device = this->GetDevice().SafeCastTo<IPwmDevice>();
|
||||
|
||||
/* Invoke the driver handler. */
|
||||
R_RETURN(device.GetDriver().SafeCastTo<IPwmDriver>().GetEnabled(out, std::addressof(device)));
|
||||
}
|
||||
|
||||
Result ChannelSessionImpl::SetScale(double scale) {
|
||||
/* Get the device. */
|
||||
IPwmDevice &device = this->GetDevice().SafeCastTo<IPwmDevice>();
|
||||
|
||||
/* Invoke the driver handler. */
|
||||
R_RETURN(device.GetDriver().SafeCastTo<IPwmDriver>().SetScale(std::addressof(device), scale));
|
||||
}
|
||||
|
||||
Result ChannelSessionImpl::GetScale(double *out) {
|
||||
/* Get the device. */
|
||||
IPwmDevice &device = this->GetDevice().SafeCastTo<IPwmDevice>();
|
||||
|
||||
/* Invoke the driver handler. */
|
||||
R_RETURN(device.GetDriver().SafeCastTo<IPwmDriver>().GetScale(out, std::addressof(device)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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::pwm::driver::impl {
|
||||
|
||||
class ChannelSessionImpl : public ::ams::ddsf::ISession {
|
||||
NON_COPYABLE(ChannelSessionImpl);
|
||||
NON_MOVEABLE(ChannelSessionImpl);
|
||||
AMS_DDSF_CASTABLE_TRAITS(ams::pwm::driver::impl::ChannelSessionImpl, ::ams::ddsf::ISession);
|
||||
public:
|
||||
ChannelSessionImpl() { /* ... */ }
|
||||
|
||||
~ChannelSessionImpl() {
|
||||
this->Close();
|
||||
}
|
||||
|
||||
Result Open(IPwmDevice *device, ddsf::AccessMode access_mode);
|
||||
void Close();
|
||||
|
||||
Result SetPeriod(TimeSpan period);
|
||||
Result GetPeriod(TimeSpan *out);
|
||||
|
||||
Result SetDuty(int duty);
|
||||
Result GetDuty(int *out);
|
||||
|
||||
Result SetEnabled(bool en);
|
||||
Result GetEnabled(bool *out);
|
||||
|
||||
Result SetScale(double scale);
|
||||
Result GetScale(double *out);
|
||||
};
|
||||
static_assert( sizeof(ChannelSessionImpl) <= ChannelSessionSize);
|
||||
static_assert(alignof(ChannelSessionImpl) <= ChannelSessionAlign);
|
||||
|
||||
struct alignas(ChannelSessionAlign) ChannelSessionImplPadded {
|
||||
ChannelSessionImpl _impl;
|
||||
u8 _padding[ChannelSessionSize - sizeof(ChannelSessionImpl)];
|
||||
};
|
||||
static_assert( sizeof(ChannelSessionImplPadded) == ChannelSessionSize);
|
||||
static_assert(alignof(ChannelSessionImplPadded) == ChannelSessionAlign);
|
||||
|
||||
ALWAYS_INLINE ChannelSessionImpl &GetChannelSessionImpl(ChannelSession &session) {
|
||||
return GetReference(session._impl)._impl;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE const ChannelSessionImpl &GetChannelSessionImpl(const ChannelSession &session) {
|
||||
return GetReference(session._impl)._impl;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE ChannelSessionImpl &GetOpenChannelSessionImpl(ChannelSession &session) {
|
||||
auto &ref = GetReference(session._impl)._impl;
|
||||
AMS_ASSERT(ref.IsOpen());
|
||||
return ref;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE const ChannelSessionImpl &GetOpenChannelSessionImpl(const ChannelSession &session) {
|
||||
const auto &ref = GetReference(session._impl)._impl;
|
||||
AMS_ASSERT(ref.IsOpen());
|
||||
return ref;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 "pwm_driver_core.hpp"
|
||||
|
||||
namespace ams::pwm::driver::impl {
|
||||
|
||||
namespace {
|
||||
|
||||
constinit os::SdkMutex g_init_mutex;
|
||||
constinit int g_init_count = 0;
|
||||
|
||||
pwm::driver::IPwmDriver::List &GetPwmDriverList() {
|
||||
AMS_FUNCTION_LOCAL_STATIC_CONSTINIT(pwm::driver::IPwmDriver::List, s_driver_list);
|
||||
return s_driver_list;
|
||||
}
|
||||
|
||||
ddsf::DeviceCodeEntryManager &GetDeviceCodeEntryManager() {
|
||||
AMS_FUNCTION_LOCAL_STATIC(ddsf::DeviceCodeEntryManager, s_device_code_entry_manager, ddsf::GetDeviceCodeEntryHolderMemoryResource());
|
||||
|
||||
return s_device_code_entry_manager;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void InitializeDrivers() {
|
||||
std::scoped_lock lk(g_init_mutex);
|
||||
|
||||
/* Initialize all registered drivers, if this is our first initialization. */
|
||||
if ((g_init_count++) == 0) {
|
||||
for (auto &driver : GetPwmDriverList()) {
|
||||
driver.SafeCastTo<IPwmDriver>().InitializeDriver();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FinalizeDrivers() {
|
||||
std::scoped_lock lk(g_init_mutex);
|
||||
|
||||
/* If we have no remaining sessions, close. */
|
||||
if ((--g_init_count) == 0) {
|
||||
/* Reset all device code entries. */
|
||||
GetDeviceCodeEntryManager().Reset();
|
||||
|
||||
/* Finalize all drivers. */
|
||||
for (auto &driver : GetPwmDriverList()) {
|
||||
driver.SafeCastTo<IPwmDriver>().FinalizeDriver();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RegisterDriver(IPwmDriver *driver) {
|
||||
AMS_ASSERT(driver != nullptr);
|
||||
GetPwmDriverList().push_back(*driver);
|
||||
}
|
||||
|
||||
void UnregisterDriver(IPwmDriver *driver) {
|
||||
AMS_ASSERT(driver != nullptr);
|
||||
if (driver->IsLinkedToList()) {
|
||||
auto &list = GetPwmDriverList();
|
||||
list.erase(list.iterator_to(*driver));
|
||||
}
|
||||
}
|
||||
|
||||
Result RegisterDeviceCode(DeviceCode device_code, IPwmDevice *device) {
|
||||
AMS_ASSERT(device != nullptr);
|
||||
R_TRY(GetDeviceCodeEntryManager().Add(device_code, device));
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
bool UnregisterDeviceCode(DeviceCode device_code) {
|
||||
return GetDeviceCodeEntryManager().Remove(device_code);
|
||||
}
|
||||
|
||||
Result FindDevice(IPwmDevice **out, DeviceCode device_code) {
|
||||
/* Validate output. */
|
||||
AMS_ASSERT(out != nullptr);
|
||||
|
||||
/* Find the device. */
|
||||
ddsf::IDevice *device;
|
||||
R_TRY(GetDeviceCodeEntryManager().FindDevice(std::addressof(device), device_code));
|
||||
|
||||
/* Set output. */
|
||||
*out = device->SafeCastToPointer<IPwmDevice>();
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
Result FindDeviceByChannelIndex(IPwmDevice **out, int channel) {
|
||||
/* Validate output. */
|
||||
AMS_ASSERT(out != nullptr);
|
||||
|
||||
/* Find the device. */
|
||||
bool found = false;
|
||||
GetDeviceCodeEntryManager().ForEachEntry([&](ddsf::DeviceCodeEntry &entry) -> bool {
|
||||
/* Convert the entry to an IPwmDevice. */
|
||||
auto &device = entry.GetDevice().SafeCastTo<IPwmDevice>();
|
||||
|
||||
/* Check if the device is the one we're looking for. */
|
||||
if (device.GetChannelIndex() == channel) {
|
||||
found = true;
|
||||
*out = std::addressof(device);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
/* Check that we found the pad. */
|
||||
R_UNLESS(found, ddsf::ResultDeviceCodeNotFound());
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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::pwm::driver::impl {
|
||||
|
||||
void InitializeDrivers();
|
||||
void FinalizeDrivers();
|
||||
|
||||
void RegisterDriver(IPwmDriver *driver);
|
||||
void UnregisterDriver(IPwmDriver *driver);
|
||||
|
||||
Result RegisterDeviceCode(DeviceCode device_code, IPwmDevice *device);
|
||||
bool UnregisterDeviceCode(DeviceCode device_code);
|
||||
|
||||
Result FindDevice(IPwmDevice **out, DeviceCode device_code);
|
||||
Result FindDeviceByChannelIndex(IPwmDevice **out, int channel);
|
||||
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 "impl/pwm_driver_core.hpp"
|
||||
#include "impl/pwm_channel_session_impl.hpp"
|
||||
|
||||
namespace ams::pwm::driver {
|
||||
|
||||
namespace {
|
||||
|
||||
Result OpenSessionImpl(ChannelSession *out, IPwmDevice *device) {
|
||||
/* Construct the session. */
|
||||
auto *session = std::construct_at(std::addressof(impl::GetChannelSessionImpl(*out)));
|
||||
ON_RESULT_FAILURE { std::destroy_at(session); };
|
||||
|
||||
/* Open the session. */
|
||||
R_RETURN(session->Open(device, ddsf::AccessMode_ReadWrite));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Result OpenSession(ChannelSession *out, DeviceCode device_code) {
|
||||
AMS_ASSERT(out != nullptr);
|
||||
|
||||
/* Find the device. */
|
||||
IPwmDevice *device = nullptr;
|
||||
R_TRY(impl::FindDevice(std::addressof(device), device_code));
|
||||
AMS_ASSERT(device != nullptr);
|
||||
|
||||
/* Open the session. */
|
||||
R_TRY(OpenSessionImpl(out, device));
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
void CloseSession(ChannelSession &session) {
|
||||
std::destroy_at(std::addressof(impl::GetOpenChannelSessionImpl(session)));
|
||||
}
|
||||
|
||||
void SetPeriod(ChannelSession &session, TimeSpan period) {
|
||||
R_ABORT_UNLESS(impl::GetOpenChannelSessionImpl(session).SetPeriod(period));
|
||||
}
|
||||
|
||||
TimeSpan GetPeriod(ChannelSession &session) {
|
||||
TimeSpan out_val;
|
||||
R_ABORT_UNLESS(impl::GetOpenChannelSessionImpl(session).GetPeriod(std::addressof(out_val)));
|
||||
return out_val;
|
||||
}
|
||||
|
||||
void SetDuty(ChannelSession &session, int duty) {
|
||||
R_ABORT_UNLESS(impl::GetOpenChannelSessionImpl(session).SetDuty(duty));
|
||||
}
|
||||
|
||||
int GetDuty(ChannelSession &session) {
|
||||
int out_val;
|
||||
R_ABORT_UNLESS(impl::GetOpenChannelSessionImpl(session).GetDuty(std::addressof(out_val)));
|
||||
return out_val;
|
||||
}
|
||||
|
||||
void SetEnabled(ChannelSession &session, bool en) {
|
||||
R_ABORT_UNLESS(impl::GetOpenChannelSessionImpl(session).SetEnabled(en));
|
||||
}
|
||||
|
||||
bool GetEnabled(ChannelSession &session) {
|
||||
bool out_val;
|
||||
R_ABORT_UNLESS(impl::GetOpenChannelSessionImpl(session).GetEnabled(std::addressof(out_val)));
|
||||
return out_val;
|
||||
}
|
||||
|
||||
void SetScale(ChannelSession &session, double scale) {
|
||||
R_ABORT_UNLESS(impl::GetOpenChannelSessionImpl(session).SetScale(scale));
|
||||
}
|
||||
|
||||
double GetScale(ChannelSession &session) {
|
||||
double out_val;
|
||||
R_ABORT_UNLESS(impl::GetOpenChannelSessionImpl(session).GetScale(std::addressof(out_val)));
|
||||
return out_val;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 "impl/pwm_driver_core.hpp"
|
||||
|
||||
namespace ams::pwm::driver {
|
||||
|
||||
void Initialize() {
|
||||
return impl::InitializeDrivers();
|
||||
}
|
||||
|
||||
void Finalize() {
|
||||
return impl::FinalizeDrivers();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 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 "impl/pwm_driver_core.hpp"
|
||||
|
||||
namespace ams::pwm::driver {
|
||||
|
||||
void RegisterDriver(IPwmDriver *driver) {
|
||||
return impl::RegisterDriver(driver);
|
||||
}
|
||||
|
||||
void UnregisterDriver(IPwmDriver *driver) {
|
||||
return impl::UnregisterDriver(driver);
|
||||
}
|
||||
|
||||
Result RegisterDeviceCode(DeviceCode device_code, IPwmDevice *device) {
|
||||
R_RETURN(impl::RegisterDeviceCode(device_code, device));
|
||||
}
|
||||
|
||||
bool UnregisterDeviceCode(DeviceCode device_code) {
|
||||
return impl::UnregisterDeviceCode(device_code);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user