pwm: implement driver for boot sysmodule

This commit is contained in:
Michael Scire
2020-11-01 23:04:19 -08:00
parent 4cc4f5fdb0
commit aa63b1eab7
41 changed files with 2107 additions and 59 deletions

View File

@@ -0,0 +1,124 @@
/*
* 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 "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();
return ResultSuccess();
}
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. */
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. */
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. */
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. */
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. */
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. */
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. */
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. */
return device.GetDriver().SafeCastTo<IPwmDriver>().GetScale(out, std::addressof(device));
}
}

View File

@@ -0,0 +1,77 @@
/*
* 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::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;
}
}

View File

@@ -0,0 +1,126 @@
/*
* 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 "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() {
static pwm::driver::IPwmDriver::List s_driver_list;
return s_driver_list;
}
ddsf::DeviceCodeEntryManager &GetDeviceCodeEntryManager() {
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));
return ResultSuccess();
}
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>();
return ResultSuccess();
}
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());
return ResultSuccess();
}
}

View File

@@ -0,0 +1,33 @@
/*
* 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::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);
}