diff --git a/libraries/libstratosphere/include/stratosphere.hpp b/libraries/libstratosphere/include/stratosphere.hpp index 778b186bd..a6eb28d13 100644 --- a/libraries/libstratosphere/include/stratosphere.hpp +++ b/libraries/libstratosphere/include/stratosphere.hpp @@ -61,6 +61,7 @@ #include #include #include +#include #include #include #include diff --git a/libraries/libstratosphere/include/stratosphere/pcv.hpp b/libraries/libstratosphere/include/stratosphere/pcv.hpp new file mode 100644 index 000000000..a7c59ad8c --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pcv.hpp @@ -0,0 +1,20 @@ +/* + * 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 . + */ + +#pragma once + +#include +#include diff --git a/libraries/libstratosphere/include/stratosphere/pcv/pcv_api.hpp b/libraries/libstratosphere/include/stratosphere/pcv/pcv_api.hpp new file mode 100644 index 000000000..01643d09c --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pcv/pcv_api.hpp @@ -0,0 +1,30 @@ +/* + * 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 . + */ +#pragma once +#include +#include + +namespace ams::pcv { + + void Initialize(); + void Finalize(); + + Result SetClockEnabled(Module module, bool en); + Result SetClockRate(Module module, ClockHz hz); + + Result SetReset(Module module, bool en); + +} diff --git a/libraries/libstratosphere/include/stratosphere/pcv/pcv_types.hpp b/libraries/libstratosphere/include/stratosphere/pcv/pcv_types.hpp new file mode 100644 index 000000000..114976da2 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pcv/pcv_types.hpp @@ -0,0 +1,116 @@ +/* + * 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 . + */ +#pragma once +#include + +namespace ams::pcv { + + using ClockHz = u32; + using MicroVolt = s32; + using MilliC = s32; + + /* TODO: Device codes? */ + enum Module { + Module_Cpu = 0, + Module_Gpu = 1, + Module_I2s1 = 2, + Module_I2s2 = 3, + Module_I2s3 = 4, + Module_Pwm = 5, + Module_I2c1 = 6, + Module_I2c2 = 7, + Module_I2c3 = 8, + Module_I2c4 = 9, + Module_I2c5 = 10, + Module_I2c6 = 11, + Module_Spi1 = 12, + Module_Spi2 = 13, + Module_Spi3 = 14, + Module_Spi4 = 15, + Module_Disp1 = 16, + Module_Disp2 = 17, + Module_Isp = 18, + Module_Vi = 19, + Module_Sdmmc1 = 20, + Module_Sdmmc2 = 21, + Module_Sdmmc3 = 22, + Module_Sdmmc4 = 23, + Module_Owr = 24, + Module_Csite = 25, + Module_Tsec = 26, + Module_Mselect = 27, + Module_Hda2codec2x = 28, + Module_Actmon = 29, + Module_I2cSlow = 30, + Module_Sor1 = 31, + Module_Sata = 32, + Module_Hda = 33, + Module_XusbCoreHostSrc = 34, + Module_XusbFalconSrc = 35, + Module_XusbFsSrc = 36, + Module_XusbCoreDevSrc = 37, + Module_XusbSsSrc = 38, + Module_UartA = 39, + Module_UartB = 40, + Module_UartC = 41, + Module_UartD = 42, + Module_Host1x = 43, + Module_Entropy = 44, + Module_SocTherm = 45, + Module_Vic = 46, + Module_Nvenc = 47, + Module_Nvjpg = 48, + Module_Nvdec = 49, + Module_Qspi = 50, + Module_ViI2c = 51, + Module_Tsecb = 52, + Module_Ape = 53, + Module_AudioDsp = 54, + Module_AudioUart = 55, + Module_Emc = 56, + Module_Plle = 57, + Module_PlleHwSeq = 58, + Module_Dsi = 59, + Module_Maud = 60, + Module_Dpaux1 = 61, + Module_MipiCal = 62, + Module_UartFstMipiCal = 63, + Module_Osc = 64, + Module_SysBus = 65, + Module_SorSafe = 66, + Module_XusbSs = 67, + Module_XusbHost = 68, + Module_XusbDevice = 69, + Module_Extperiph1 = 70, + Module_Ahub = 71, + Module_Hda2hdmicodec = 72, + Module_Gpuaux = 73, + Module_UsbD = 74, + Module_Usb2 = 75, + Module_Pcie = 76, + Module_Afi = 77, + Module_PciExClk = 78, + Module_PExUsbPhy = 79, + Module_XUsbPadCtl = 80, + Module_Apbdma = 81, + Module_Usb2TrkClk = 82, + Module_XUsbIoPll = 83, + Module_XUsbIoPllHwSeq = 84, + Module_Cec = 85, + Module_Extperiph2 = 86, + }; + +} diff --git a/libraries/libstratosphere/include/stratosphere/pwm.hpp b/libraries/libstratosphere/include/stratosphere/pwm.hpp index 6446e5b42..99aefb4b3 100644 --- a/libraries/libstratosphere/include/stratosphere/pwm.hpp +++ b/libraries/libstratosphere/include/stratosphere/pwm.hpp @@ -15,6 +15,14 @@ */ #pragma once - #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include \ No newline at end of file diff --git a/libraries/libstratosphere/include/stratosphere/pwm/driver/board/nintendo_nx/pwm_driver_api.hpp b/libraries/libstratosphere/include/stratosphere/pwm/driver/board/nintendo_nx/pwm_driver_api.hpp new file mode 100644 index 000000000..0a9b0030e --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/driver/board/nintendo_nx/pwm_driver_api.hpp @@ -0,0 +1,24 @@ +/* + * 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 . + */ +#pragma once +#include +#include + +namespace ams::pwm::driver::board::nintendo_nx { + + void Initialize(); + +} diff --git a/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_channel_api.hpp b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_channel_api.hpp new file mode 100644 index 000000000..312ecd224 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_channel_api.hpp @@ -0,0 +1,50 @@ +/* + * 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 . + */ +#pragma once +#include +#include + +namespace ams::pwm::driver { + + namespace impl { + + constexpr inline size_t ChannelSessionSize = 0x60; + constexpr inline size_t ChannelSessionAlign = 8; + struct alignas(ChannelSessionAlign) ChannelSessionImplPadded; + + } + + struct ChannelSession { + util::TypedStorage _impl; + }; + + Result OpenSession(ChannelSession *out, DeviceCode device_code); + void CloseSession(ChannelSession &session); + + void SetPeriod(ChannelSession &session, TimeSpan period); + TimeSpan GetPeriod(ChannelSession &session); + + void SetDuty(ChannelSession &session, int duty); + int GetDuty(ChannelSession &session); + + void SetEnabled(ChannelSession &session, bool en); + bool GetEnabled(ChannelSession &session); + + void SetScale(ChannelSession &session, double scale); + double GetScale(ChannelSession &session); + +} + diff --git a/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_driver_client_api.hpp b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_driver_client_api.hpp new file mode 100644 index 000000000..6499bf7b0 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_driver_client_api.hpp @@ -0,0 +1,25 @@ +/* + * 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 . + */ +#pragma once +#include +#include + +namespace ams::pwm::driver { + + void Initialize(); + void Finalize(); + +} diff --git a/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_driver_service_api.hpp b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_driver_service_api.hpp new file mode 100644 index 000000000..715fff353 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_driver_service_api.hpp @@ -0,0 +1,30 @@ +/* + * 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 . + */ +#pragma once +#include +#include +#include +#include + +namespace ams::pwm::driver { + + void RegisterDriver(IPwmDriver *driver); + void UnregisterDriver(IPwmDriver *driver); + + Result RegisterDeviceCode(DeviceCode device_code, IPwmDevice *device); + bool UnregisterDeviceCode(DeviceCode device_code); + +} diff --git a/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_i_pwm_device.hpp b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_i_pwm_device.hpp new file mode 100644 index 000000000..6949c7cfb --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_i_pwm_device.hpp @@ -0,0 +1,36 @@ +/* + * 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 . + */ +#pragma once +#include +#include +#include + +namespace ams::pwm::driver { + + class IPwmDevice : public ::ams::ddsf::IDevice { + NON_COPYABLE(IPwmDevice); + NON_MOVEABLE(IPwmDevice); + AMS_DDSF_CASTABLE_TRAITS(ams::pwm::IPwmDevice, ::ams::ddsf::IDriver); + private: + int channel_index; + public: + IPwmDevice(int id) : IDevice(false), channel_index(id) { /* ... */ } + virtual ~IPwmDevice() { /* ... */ } + + constexpr int GetChannelIndex() const { return this->channel_index; } + }; + +} diff --git a/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_i_pwm_driver.hpp b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_i_pwm_driver.hpp new file mode 100644 index 000000000..11f8dd601 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_i_pwm_driver.hpp @@ -0,0 +1,54 @@ +/* + * 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 . + */ +#pragma once +#include +#include +#include +#include + +namespace ams::pwm::driver { + + class IPwmDriver : public ::ams::ddsf::IDriver { + NON_COPYABLE(IPwmDriver); + NON_MOVEABLE(IPwmDriver); + AMS_DDSF_CASTABLE_TRAITS(ams::pwm::IPwmDriver, ::ams::ddsf::IDriver); + public: + IPwmDriver() : IDriver() { /* ... */ } + virtual ~IPwmDriver() { /* ... */ } + + virtual void InitializeDriver() = 0; + virtual void FinalizeDriver() = 0; + + virtual Result InitializeDevice(IPwmDevice *device) = 0; + virtual void FinalizeDevice(IPwmDevice *device) = 0; + + virtual Result SetPeriod(IPwmDevice *device, TimeSpan period) = 0; + virtual Result GetPeriod(TimeSpan *out, IPwmDevice *device) = 0; + + virtual Result SetDuty(IPwmDevice *device, int duty) = 0; + virtual Result GetDuty(int *out, IPwmDevice *device) = 0; + + virtual Result SetScale(IPwmDevice *device, double scale) = 0; + virtual Result GetScale(double *out, IPwmDevice *device) = 0; + + virtual Result SetEnabled(IPwmDevice *device, bool en) = 0; + virtual Result GetEnabled(bool *out, IPwmDevice *device) = 0; + + virtual Result Suspend() = 0; + virtual void Resume() = 0; + }; + +} diff --git a/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_select_driver_api.hpp b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_select_driver_api.hpp new file mode 100644 index 000000000..c9fbec53f --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/driver/pwm_select_driver_api.hpp @@ -0,0 +1,36 @@ +/* + * 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 . + */ +#pragma once +#include +#include +#include + +#if defined(ATMOSPHERE_BOARD_NINTENDO_NX) + + #include + + namespace ams::pwm::driver::board { + + using namespace ams::pwm::driver::board::nintendo_nx; + + } + +#else + + #error "Unknown board for ams::pwm::driver::" + +#endif + diff --git a/libraries/libstratosphere/include/stratosphere/pwm/pwm_api.hpp b/libraries/libstratosphere/include/stratosphere/pwm/pwm_api.hpp new file mode 100644 index 000000000..44544fbdf --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/pwm_api.hpp @@ -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 . + */ +#pragma once +#include +#include +#include + +namespace ams::pwm { + + void InitializeWith(std::shared_ptr &&sp); + void Finalize(); + +} diff --git a/libraries/libstratosphere/include/stratosphere/pwm/pwm_channel_api.hpp b/libraries/libstratosphere/include/stratosphere/pwm/pwm_channel_api.hpp new file mode 100644 index 000000000..da10659d1 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/pwm_channel_api.hpp @@ -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 . + */ +#pragma once +#include +#include + +namespace ams::pwm { + + struct ChannelSession { + void *_session; + }; + + Result OpenSession(ChannelSession *out, DeviceCode device_code); + void CloseSession(ChannelSession &session); + + void SetPeriod(ChannelSession &session, TimeSpan period); + TimeSpan GetPeriod(ChannelSession &session); + + void SetDuty(ChannelSession &session, int duty); + int GetDuty(ChannelSession &session); + + void SetEnabled(ChannelSession &session, bool en); + bool GetEnabled(ChannelSession &session); + + void SetScale(ChannelSession &session, double scale); + double GetScale(ChannelSession &session); + +} diff --git a/libraries/libstratosphere/include/stratosphere/pwm/server/pwm_server_api.hpp b/libraries/libstratosphere/include/stratosphere/pwm/server/pwm_server_api.hpp new file mode 100644 index 000000000..0024a54a8 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/server/pwm_server_api.hpp @@ -0,0 +1,25 @@ +/* + * 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 . + */ +#pragma once +#include +#include +#include + +namespace ams::pwm::server { + + std::shared_ptr GetServiceObject(); + +} diff --git a/libraries/libstratosphere/include/stratosphere/pwm/sf/pwm_sf_i_channel_session.hpp b/libraries/libstratosphere/include/stratosphere/pwm/sf/pwm_sf_i_channel_session.hpp new file mode 100644 index 000000000..f61c15ea0 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/sf/pwm_sf_i_channel_session.hpp @@ -0,0 +1,35 @@ +/* + * 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 . + */ + +#pragma once +#include +#include + +namespace ams::pwm::sf { + + #define AMS_PWM_I_CHANNEL_SESSION_INTERFACE_INFO(C, H) \ + AMS_SF_METHOD_INFO(C, H, 0, Result, SetPeriod, (TimeSpanType period) ) \ + AMS_SF_METHOD_INFO(C, H, 1, Result, GetPeriod, (ams::sf::Out out) ) \ + AMS_SF_METHOD_INFO(C, H, 2, Result, SetDuty, (int duty) ) \ + AMS_SF_METHOD_INFO(C, H, 3, Result, GetDuty, (ams::sf::Out out) ) \ + AMS_SF_METHOD_INFO(C, H, 4, Result, SetEnabled, (bool enabled) ) \ + AMS_SF_METHOD_INFO(C, H, 5, Result, GetEnabled, (ams::sf::Out out) ) \ + AMS_SF_METHOD_INFO(C, H, 6, Result, SetScale, (double scale), hos::Version_6_0_0) \ + AMS_SF_METHOD_INFO(C, H, 7, Result, GetScale, (ams::sf::Out out), hos::Version_6_0_0) + + AMS_SF_DEFINE_INTERFACE(IChannelSession, AMS_PWM_I_CHANNEL_SESSION_INTERFACE_INFO) + +} diff --git a/libraries/libstratosphere/include/stratosphere/pwm/sf/pwm_sf_i_manager.hpp b/libraries/libstratosphere/include/stratosphere/pwm/sf/pwm_sf_i_manager.hpp new file mode 100644 index 000000000..5c4e47f82 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/pwm/sf/pwm_sf_i_manager.hpp @@ -0,0 +1,32 @@ +/* + * 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 . + */ + +#pragma once +#include +#include +#include +#include + +namespace ams::pwm::sf { + + #define AMS_PWM_I_MANAGER_INTERFACE_INFO(C, H) \ + AMS_SF_METHOD_INFO(C, H, 0, Result, OpenSessionForDev, (ams::sf::Out> out, int channel) ) \ + AMS_SF_METHOD_INFO(C, H, 1, Result, OpenSession, (ams::sf::Out> out, pwm::ChannelName channel_name) ) \ + AMS_SF_METHOD_INFO(C, H, 2, Result, OpenSession2, (ams::sf::Out> out, DeviceCode device_code), hos::Version_6_0_0) + + AMS_SF_DEFINE_INTERFACE(IManager, AMS_PWM_I_MANAGER_INTERFACE_INFO) + +} diff --git a/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_impl_pwm_driver_api.cpp b/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_impl_pwm_driver_api.cpp new file mode 100644 index 000000000..f292236c3 --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_impl_pwm_driver_api.cpp @@ -0,0 +1,66 @@ +/* + * 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 . + */ +#include +#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_CpuFan, 0 }, + { pwm::DeviceCode_LcdBacklight, 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 = new (static_cast(driver_storage)) PwmDriverImpl(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 = new (static_cast(device_storage)) PwmDeviceImpl(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)); + } + + return ResultSuccess(); + } + +} diff --git a/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_impl_pwm_driver_api.hpp b/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_impl_pwm_driver_api.hpp new file mode 100644 index 000000000..9ce8137d8 --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_impl_pwm_driver_api.hpp @@ -0,0 +1,28 @@ +/* + * 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 . + */ +#pragma once +#include + +namespace ams::pwm::driver::board::nintendo_nx::impl { + + struct ChannelDefinition { + DeviceCode device_code; + int channel_id; + }; + + Result InitializePwmDriver(); + +} diff --git a/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_pwm_driver_impl.cpp b/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_pwm_driver_impl.cpp new file mode 100644 index 000000000..0c2e29198 --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_pwm_driver_impl.cpp @@ -0,0 +1,255 @@ +/* + * 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 . + */ +#include +#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 + 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) : registers_phys_addr(paddr), registers_size(sz), channels(c), num_channels(nc), 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. */ + this->registers = dd::QueryIoMapping(this->registers_phys_addr, this->registers_size); + AMS_ABORT_UNLESS(this->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->SetDuty(device, 0); + this->SetPeriod(device, DefaultChannelPeriod); + return ResultSuccess(); + } + + 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(std::max(DivideRoundUp(PwmClockRateHz, hz * 256), 1) - 1, 0x1FFF); + + /* Acquire exclusive access to the device registers. */ + std::scoped_lock lk(device->SafeCastTo()); + + /* Update the period. */ + reg::ReadWrite(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_VALUE(PWM_CSR_PFM, pfm)); + + return ResultSuccess(); + } + + 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(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); + return ResultSuccess(); + } + + 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()); + + /* Update the duty. */ + reg::ReadWrite(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_VALUE(PWM_CSR_PWM, static_cast(duty))); + + return ResultSuccess(); + } + + Result PwmDriverImpl::GetDuty(int *out, IPwmDevice *device) { + /* Validate the device. */ + AMS_ASSERT(out != nullptr); + AMS_ASSERT(device != nullptr); + + /* Get the duty. */ + *out = static_cast(reg::GetValue(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_MASK(PWM_CSR_PWM))); + return ResultSuccess(); + } + + 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(((scale * 256.0) / 100.0) + 0.5); + + /* Set the duty. */ + return this->SetDuty(device, duty); + } + + 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(reg::GetValue(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_MASK(PWM_CSR_PWM))); + + /* Convert to scale. */ + *out = (static_cast(duty) * 100.0) / 256.0; + return ResultSuccess(); + } + + 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()); + + /* Update the enable. */ + reg::ReadWrite(this->GetRegistersFor(device) + PWM_CONTROLLER_PWM_CSR, PWM_REG_BITS_ENUM_SEL(PWM_CSR_ENB, en, ENABLE, DISABLE)); + + return ResultSuccess(); + } + + 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)); + return ResultSuccess(); + } + + Result PwmDriverImpl::Suspend() { + /* Suspend each device. */ + this->ForEachDevice([&](ddsf::IDevice &device) -> bool { + /* Convert the device to a pwm device. */ + auto &pwm_device = device.SafeCastTo(); + + /* 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. */ + 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(); + + /* 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; + }); + } + +} diff --git a/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_pwm_driver_impl.hpp b/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_pwm_driver_impl.hpp new file mode 100644 index 000000000..d04e841bd --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/impl/pwm_pwm_driver_impl.hpp @@ -0,0 +1,85 @@ +/* + * 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 . + */ +#pragma once +#include +#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 suspend_mutex; + u32 suspend_value; + public: + PwmDeviceImpl(int channel) : IPwmDevice(channel), suspend_mutex(), suspend_value() { /* ... */ } + + void SetSuspendValue(u32 v) { this->suspend_value = v; } + u32 GetSuspendValue() const { return this->suspend_value; } + + void lock() { return this->suspend_mutex.lock(); } + void unlock() { return this->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 registers_phys_addr; + size_t registers_size; + const ChannelDefinition *channels; + size_t num_channels; + uintptr_t registers; + private: + ALWAYS_INLINE uintptr_t GetRegistersFor(IPwmDevice &device) { + return 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; + }; + +} diff --git a/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/pwm_driver_api.cpp b/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/pwm_driver_api.cpp new file mode 100644 index 000000000..0b8442e37 --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/board/nintendo_nx/pwm_driver_api.cpp @@ -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 . + */ +#include +#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()); */ + } + +} diff --git a/libraries/libstratosphere/source/pwm/driver/impl/pwm_channel_session_impl.cpp b/libraries/libstratosphere/source/pwm/driver/impl/pwm_channel_session_impl.cpp new file mode 100644 index 000000000..1459246cc --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/impl/pwm_channel_session_impl.cpp @@ -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 . + */ +#include +#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().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(); + + /* Close the session. */ + ddsf::CloseSession(this); + + /* If there are no remaining sessions, finalize the device. */ + if (!device.HasAnyOpenSession()) { + device.GetDriver().SafeCastTo().FinalizeDevice(std::addressof(device)); + } + } + + Result ChannelSessionImpl::SetPeriod(TimeSpan period) { + /* Get the device. */ + IPwmDevice &device = this->GetDevice().SafeCastTo(); + + /* Invoke the driver handler. */ + return device.GetDriver().SafeCastTo().SetPeriod(std::addressof(device), period); + } + + Result ChannelSessionImpl::GetPeriod(TimeSpan *out) { + /* Get the device. */ + IPwmDevice &device = this->GetDevice().SafeCastTo(); + + /* Invoke the driver handler. */ + return device.GetDriver().SafeCastTo().GetPeriod(out, std::addressof(device)); + } + + Result ChannelSessionImpl::SetDuty(int duty) { + /* Get the device. */ + IPwmDevice &device = this->GetDevice().SafeCastTo(); + + /* Invoke the driver handler. */ + return device.GetDriver().SafeCastTo().SetDuty(std::addressof(device), duty); + } + + Result ChannelSessionImpl::GetDuty(int *out) { + /* Get the device. */ + IPwmDevice &device = this->GetDevice().SafeCastTo(); + + /* Invoke the driver handler. */ + return device.GetDriver().SafeCastTo().GetDuty(out, std::addressof(device)); + } + + Result ChannelSessionImpl::SetEnabled(bool en) { + /* Get the device. */ + IPwmDevice &device = this->GetDevice().SafeCastTo(); + + /* Invoke the driver handler. */ + return device.GetDriver().SafeCastTo().SetEnabled(std::addressof(device), en); + } + + Result ChannelSessionImpl::GetEnabled(bool *out) { + /* Get the device. */ + IPwmDevice &device = this->GetDevice().SafeCastTo(); + + /* Invoke the driver handler. */ + return device.GetDriver().SafeCastTo().GetEnabled(out, std::addressof(device)); + } + + Result ChannelSessionImpl::SetScale(double scale) { + /* Get the device. */ + IPwmDevice &device = this->GetDevice().SafeCastTo(); + + /* Invoke the driver handler. */ + return device.GetDriver().SafeCastTo().SetScale(std::addressof(device), scale); + } + + Result ChannelSessionImpl::GetScale(double *out) { + /* Get the device. */ + IPwmDevice &device = this->GetDevice().SafeCastTo(); + + /* Invoke the driver handler. */ + return device.GetDriver().SafeCastTo().GetScale(out, std::addressof(device)); + } + +} diff --git a/libraries/libstratosphere/source/pwm/driver/impl/pwm_channel_session_impl.hpp b/libraries/libstratosphere/source/pwm/driver/impl/pwm_channel_session_impl.hpp new file mode 100644 index 000000000..6b8a75dfb --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/impl/pwm_channel_session_impl.hpp @@ -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 . + */ +#pragma once +#include + +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; + } + +} diff --git a/libraries/libstratosphere/source/pwm/driver/impl/pwm_driver_core.cpp b/libraries/libstratosphere/source/pwm/driver/impl/pwm_driver_core.cpp new file mode 100644 index 000000000..359312dba --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/impl/pwm_driver_core.cpp @@ -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 . + */ +#include +#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().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().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(); + 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(); + + /* 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(); + } + +} diff --git a/libraries/libstratosphere/source/pwm/driver/impl/pwm_driver_core.hpp b/libraries/libstratosphere/source/pwm/driver/impl/pwm_driver_core.hpp new file mode 100644 index 000000000..aca6a821e --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/impl/pwm_driver_core.hpp @@ -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 . + */ +#pragma once +#include + +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); + +} diff --git a/libraries/libstratosphere/source/pwm/driver/pwm_driver_channel_api.cpp b/libraries/libstratosphere/source/pwm/driver/pwm_driver_channel_api.cpp new file mode 100644 index 000000000..27ab69454 --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/pwm_driver_channel_api.cpp @@ -0,0 +1,97 @@ +/* + * 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 . + */ +#include + +#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 = new (std::addressof(impl::GetChannelSessionImpl(*out))) impl::ChannelSessionImpl; + auto session_guard = SCOPE_GUARD { session->~ChannelSessionImpl(); }; + + /* Open the session. */ + R_TRY(session->Open(device, ddsf::AccessMode_ReadWrite)); + + /* We succeeded. */ + session_guard.Cancel(); + return ResultSuccess(); + } + + } + + 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)); + + return ResultSuccess(); + } + + void CloseSession(ChannelSession &session) { + impl::GetOpenChannelSessionImpl(session).~ChannelSessionImpl(); + } + + 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; + } +} diff --git a/libraries/libstratosphere/source/pwm/driver/pwm_driver_client_api.cpp b/libraries/libstratosphere/source/pwm/driver/pwm_driver_client_api.cpp new file mode 100644 index 000000000..7126b7f3f --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/pwm_driver_client_api.cpp @@ -0,0 +1,30 @@ +/* + * 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 . + */ +#include + +#include "impl/pwm_driver_core.hpp" + +namespace ams::pwm::driver { + + void Initialize() { + return impl::InitializeDrivers(); + } + + void Finalize() { + return impl::FinalizeDrivers(); + } + +} diff --git a/libraries/libstratosphere/source/pwm/driver/pwm_driver_service_api.cpp b/libraries/libstratosphere/source/pwm/driver/pwm_driver_service_api.cpp new file mode 100644 index 000000000..5955c009b --- /dev/null +++ b/libraries/libstratosphere/source/pwm/driver/pwm_driver_service_api.cpp @@ -0,0 +1,38 @@ +/* + * 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 . + */ +#include + +#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) { + return impl::RegisterDeviceCode(device_code, device); + } + + bool UnregisterDeviceCode(DeviceCode device_code) { + return impl::UnregisterDeviceCode(device_code); + } + +} diff --git a/libraries/libstratosphere/source/pwm/pwm_api.cpp b/libraries/libstratosphere/source/pwm/pwm_api.cpp new file mode 100644 index 000000000..4b843c993 --- /dev/null +++ b/libraries/libstratosphere/source/pwm/pwm_api.cpp @@ -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 . + */ +#include + +namespace ams::pwm { + + namespace { + + constinit os::SdkMutex g_init_mutex; + constinit int g_initialize_count = 0; + + std::shared_ptr g_pwm_manager; + + using InternalSession = std::shared_ptr; + + InternalSession &GetInterface(const ChannelSession &session) { + AMS_ASSERT(session._session != nullptr); + return *static_cast(session._session); + } + } + + void InitializeWith(std::shared_ptr &&sp) { + std::scoped_lock lk(g_init_mutex); + + AMS_ABORT_UNLESS(g_initialize_count == 0); + + g_pwm_manager = std::move(sp); + + g_initialize_count = 1; + } + + void Finalize() { + std::scoped_lock lk(g_init_mutex); + + AMS_ASSERT(g_initialize_count > 0); + + if ((--g_initialize_count) == 0) { + g_pwm_manager.reset(); + } + } + + Result OpenSession(ChannelSession *out, DeviceCode device_code) { + /* Allocate the session. */ + InternalSession *internal_session = new (std::nothrow) InternalSession; + AMS_ABORT_UNLESS(internal_session != nullptr); + auto session_guard = SCOPE_GUARD { delete internal_session; }; + + /* Get the session. */ + { + ams::sf::cmif::ServiceObjectHolder object_holder; + if (hos::GetVersion() >= hos::Version_6_0_0) { + R_TRY(g_pwm_manager->OpenSession2(std::addressof(object_holder), device_code)); + } else { + R_TRY(g_pwm_manager->OpenSession(std::addressof(object_holder), ConvertToChannelName(device_code))); + } + *internal_session = object_holder.GetServiceObject(); + } + + /* Set output. */ + out->_session = internal_session; + + /* We succeeded. */ + session_guard.Cancel(); + return ResultSuccess(); + } + + void CloseSession(ChannelSession &session) { + /* Close the session. */ + delete std::addressof(GetInterface(session)); + session._session = nullptr; + } + + void SetPeriod(ChannelSession &session, TimeSpan period) { + R_ABORT_UNLESS(GetInterface(session)->SetPeriod(period)); + } + + TimeSpan GetPeriod(ChannelSession &session) { + TimeSpanType out_val; + R_ABORT_UNLESS(GetInterface(session)->GetPeriod(std::addressof(out_val))); + return out_val; + } + + void SetDuty(ChannelSession &session, int duty) { + R_ABORT_UNLESS(GetInterface(session)->SetDuty(duty)); + } + + int GetDuty(ChannelSession &session) { + int out_val; + R_ABORT_UNLESS(GetInterface(session)->GetDuty(std::addressof(out_val))); + return out_val; + } + + void SetEnabled(ChannelSession &session, bool en) { + R_ABORT_UNLESS(GetInterface(session)->SetEnabled(en)); + } + + bool GetEnabled(ChannelSession &session) { + bool out_val; + R_ABORT_UNLESS(GetInterface(session)->GetEnabled(std::addressof(out_val))); + return out_val; + } + + void SetScale(ChannelSession &session, double scale) { + R_ABORT_UNLESS(GetInterface(session)->SetScale(scale)); + } + + double GetScale(ChannelSession &session) { + double out_val; + R_ABORT_UNLESS(GetInterface(session)->GetScale(std::addressof(out_val))); + return out_val; + } + +} diff --git a/libraries/libstratosphere/source/pwm/server/pwm_server_api.cpp b/libraries/libstratosphere/source/pwm/server/pwm_server_api.cpp new file mode 100644 index 000000000..0b9155e04 --- /dev/null +++ b/libraries/libstratosphere/source/pwm/server/pwm_server_api.cpp @@ -0,0 +1,36 @@ +/* + * 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 . + */ +#include +#include "pwm_server_manager_impl.hpp" + +namespace ams::pwm::server { + + namespace { + + ManagerImpl g_manager_impl; + + std::shared_ptr GetManagerServiceObject() { + static std::shared_ptr s_sp = ams::sf::GetSharedPointerTo(g_manager_impl); + return s_sp; + } + + } + + std::shared_ptr GetServiceObject() { + return GetManagerServiceObject(); + } + +} diff --git a/libraries/libstratosphere/source/pwm/server/pwm_server_channel_session_impl.hpp b/libraries/libstratosphere/source/pwm/server/pwm_server_channel_session_impl.hpp new file mode 100644 index 000000000..c70ac4cb0 --- /dev/null +++ b/libraries/libstratosphere/source/pwm/server/pwm_server_channel_session_impl.hpp @@ -0,0 +1,88 @@ +/* + * 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 . + */ +#pragma once +#include + +namespace ams::pwm::server { + + class ManagerImpl; + + class ChannelSessionImpl { + private: + ManagerImpl *parent; /* NOTE: this is an sf::SharedPointer<> in Nintendo's code. */ + pwm::driver::ChannelSession internal_session; + bool has_session; + public: + explicit ChannelSessionImpl(ManagerImpl *p) : parent(p), has_session(false) { /* ... */ } + + ~ChannelSessionImpl() { + if (this->has_session) { + pwm::driver::CloseSession(this->internal_session); + } + } + + Result OpenSession(DeviceCode device_code) { + AMS_ABORT_UNLESS(!this->has_session); + + R_TRY(pwm::driver::OpenSession(std::addressof(this->internal_session), device_code)); + this->has_session = true; + return ResultSuccess(); + } + public: + /* Actual commands. */ + Result SetPeriod(TimeSpanType period) { + pwm::driver::SetPeriod(this->internal_session, period); + return ResultSuccess(); + } + + Result GetPeriod(ams::sf::Out out) { + out.SetValue(pwm::driver::GetPeriod(this->internal_session)); + return ResultSuccess(); + } + + Result SetDuty(int duty) { + pwm::driver::SetDuty(this->internal_session, duty); + return ResultSuccess(); + } + + Result GetDuty(ams::sf::Out out) { + out.SetValue(pwm::driver::GetDuty(this->internal_session)); + return ResultSuccess(); + } + + Result SetEnabled(bool enabled) { + pwm::driver::SetEnabled(this->internal_session, enabled); + return ResultSuccess(); + } + + Result GetEnabled(ams::sf::Out out) { + out.SetValue(pwm::driver::GetEnabled(this->internal_session)); + return ResultSuccess(); + } + + Result SetScale(double scale) { + pwm::driver::SetScale(this->internal_session, scale); + return ResultSuccess(); + } + + Result GetScale(ams::sf::Out out) { + out.SetValue(pwm::driver::GetScale(this->internal_session)); + return ResultSuccess(); + } + }; + static_assert(pwm::sf::IsIChannelSession); + +} diff --git a/libraries/libstratosphere/source/pwm/server/pwm_server_manager_impl.cpp b/libraries/libstratosphere/source/pwm/server/pwm_server_manager_impl.cpp new file mode 100644 index 000000000..afc232984 --- /dev/null +++ b/libraries/libstratosphere/source/pwm/server/pwm_server_manager_impl.cpp @@ -0,0 +1,51 @@ +/* + * 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 . + */ +#include +#include "pwm_server_manager_impl.hpp" + +namespace ams::pwm::server { + + ManagerImpl::ManagerImpl() : session_memory_resource(), allocator(std::addressof(session_memory_resource)) { + this->heap_handle = lmem::CreateExpHeap(this->heap_buffer, sizeof(this->heap_buffer), lmem::CreateOption_None); + this->session_memory_resource.Attach(this->heap_handle); + } + + ManagerImpl::~ManagerImpl() { + lmem::DestroyExpHeap(this->heap_handle); + } + + Result ManagerImpl::OpenSessionForDev(ams::sf::Out> out, int channel) { + /* TODO */ + AMS_ABORT(); + } + + Result ManagerImpl::OpenSession(ams::sf::Out> out, pwm::ChannelName channel_name) { + return this->OpenSession2(out, ConvertToDeviceCode(channel_name)); + } + + Result ManagerImpl::OpenSession2(ams::sf::Out> out, DeviceCode device_code) { + /* Allocate a session. */ + auto session = ams::sf::AllocateShared(this->allocator, this); + + /* Open the session. */ + R_TRY(session->GetImpl().OpenSession(device_code)); + + /* We succeeded. */ + out.SetValue(std::move(session)); + return ResultSuccess(); + } + +} diff --git a/libraries/libstratosphere/source/pwm/server/pwm_server_manager_impl.hpp b/libraries/libstratosphere/source/pwm/server/pwm_server_manager_impl.hpp new file mode 100644 index 000000000..53662153d --- /dev/null +++ b/libraries/libstratosphere/source/pwm/server/pwm_server_manager_impl.hpp @@ -0,0 +1,40 @@ +/* + * 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 . + */ +#pragma once +#include +#include "pwm_server_channel_session_impl.hpp" + +namespace ams::pwm::server { + + class ManagerImpl { + private: + lmem::HeapHandle heap_handle; + ams::sf::ExpHeapMemoryResource session_memory_resource; + typename ams::sf::ServiceObjectAllocator allocator; + u8 heap_buffer[4_KB]; + public: + ManagerImpl(); + + ~ManagerImpl(); + public: + /* Actual commands. */ + Result OpenSessionForDev(ams::sf::Out> out, int channel); + Result OpenSession(ams::sf::Out> out, pwm::ChannelName channel_name); + Result OpenSession2(ams::sf::Out> out, DeviceCode device_code); + }; + static_assert(pwm::sf::IsIManager); + +} diff --git a/libraries/libvapours/include/vapours/results.hpp b/libraries/libvapours/include/vapours/results.hpp index 95eb6442a..21d141c3e 100644 --- a/libraries/libvapours/include/vapours/results.hpp +++ b/libraries/libvapours/include/vapours/results.hpp @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include diff --git a/libraries/libvapours/include/vapours/results/pwm_results.hpp b/libraries/libvapours/include/vapours/results/pwm_results.hpp new file mode 100644 index 000000000..37041beb5 --- /dev/null +++ b/libraries/libvapours/include/vapours/results/pwm_results.hpp @@ -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 . + */ + +#pragma once +#include + +namespace ams::pwm { + + R_DEFINE_NAMESPACE_RESULT_MODULE(189); + + R_DEFINE_ERROR_RESULT(InvalidArgument, 2); + +} diff --git a/libraries/libvapours/include/vapours/tegra.hpp b/libraries/libvapours/include/vapours/tegra.hpp index cb4cb4cee..52a04cbb5 100644 --- a/libraries/libvapours/include/vapours/tegra.hpp +++ b/libraries/libvapours/include/vapours/tegra.hpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include diff --git a/libraries/libvapours/include/vapours/tegra/tegra_pwm.hpp b/libraries/libvapours/include/vapours/tegra/tegra_pwm.hpp new file mode 100644 index 000000000..3beadefe7 --- /dev/null +++ b/libraries/libvapours/include/vapours/tegra/tegra_pwm.hpp @@ -0,0 +1,43 @@ +/* + * 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 . + */ +#pragma once +#include +#include +#include +#include +#include +#include + +#define PWM_CONTROLLER_PWM_CHANNEL_OFFSET(channel) (0x10 * channel) + +#define PWM_CONTROLLER_PWM_CSR (0x000) + + +#define PWM_REG_BITS_MASK(NAME) REG_NAMED_BITS_MASK (PWM_CONTROLLER, NAME) +#define PWM_REG_BITS_VALUE(NAME, VALUE) REG_NAMED_BITS_VALUE (PWM_CONTROLLER, NAME, VALUE) +#define PWM_REG_BITS_ENUM(NAME, ENUM) REG_NAMED_BITS_ENUM (PWM_CONTROLLER, NAME, ENUM) +#define PWM_REG_BITS_ENUM_SEL(NAME, __COND__, TRUE_ENUM, FALSE_ENUM) REG_NAMED_BITS_ENUM_SEL(PWM_CONTROLLER, NAME, __COND__, TRUE_ENUM, FALSE_ENUM) + +#define DEFINE_PWM_REG(NAME, __OFFSET__, __WIDTH__) REG_DEFINE_NAMED_REG (PWM_CONTROLLER, NAME, __OFFSET__, __WIDTH__) +#define DEFINE_PWM_REG_BIT_ENUM(NAME, __OFFSET__, ZERO, ONE) REG_DEFINE_NAMED_BIT_ENUM (PWM_CONTROLLER, NAME, __OFFSET__, ZERO, ONE) +#define DEFINE_PWM_REG_TWO_BIT_ENUM(NAME, __OFFSET__, ZERO, ONE, TWO, THREE) REG_DEFINE_NAMED_TWO_BIT_ENUM (PWM_CONTROLLER, NAME, __OFFSET__, ZERO, ONE, TWO, THREE) +#define DEFINE_PWM_REG_THREE_BIT_ENUM(NAME, __OFFSET__, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN) REG_DEFINE_NAMED_THREE_BIT_ENUM(PWM_CONTROLLER, NAME, __OFFSET__, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN) +#define DEFINE_PWM_REG_FOUR_BIT_ENUM(NAME, __OFFSET__, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, ELEVEN, TWELVE, THIRTEEN, FOURTEEN, FIFTEEN) REG_DEFINE_NAMED_FOUR_BIT_ENUM (PWM_CONTROLLER, NAME, __OFFSET__, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, ELEVEN, TWELVE, THIRTEEN, FOURTEEN, FIFTEEN) + +DEFINE_PWM_REG(PWM_CSR_PFM, 0, 13); +DEFINE_PWM_REG(PWM_CSR_PWM, 16, 15); +DEFINE_PWM_REG_BIT_ENUM(PWM_CSR_ENB, 31, DISABLE, ENABLE); + diff --git a/libraries/libvapours/include/vapours/timespan.hpp b/libraries/libvapours/include/vapours/timespan.hpp index a789a5710..3219bf238 100644 --- a/libraries/libvapours/include/vapours/timespan.hpp +++ b/libraries/libvapours/include/vapours/timespan.hpp @@ -95,6 +95,10 @@ namespace ams { constexpr ALWAYS_INLINE friend TimeSpan operator+(const TimeSpan &lhs, const TimeSpan &rhs) { TimeSpan r(lhs); return r += rhs; } constexpr ALWAYS_INLINE friend TimeSpan operator-(const TimeSpan &lhs, const TimeSpan &rhs) { TimeSpan r(lhs); return r -= rhs; } + + constexpr ALWAYS_INLINE operator TimeSpanType() const { + return this->ts; + } }; } \ No newline at end of file diff --git a/stratosphere/boot/source/api_overrides/clkrst_api_for_boot.board.nintendo_nx.cpp b/stratosphere/boot/source/api_overrides/pcv_clkrst_api_for_boot.board.nintendo_nx.cpp similarity index 52% rename from stratosphere/boot/source/api_overrides/clkrst_api_for_boot.board.nintendo_nx.cpp rename to stratosphere/boot/source/api_overrides/pcv_clkrst_api_for_boot.board.nintendo_nx.cpp index b50c9d8a1..11bb7fd66 100644 --- a/stratosphere/boot/source/api_overrides/clkrst_api_for_boot.board.nintendo_nx.cpp +++ b/stratosphere/boot/source/api_overrides/pcv_clkrst_api_for_boot.board.nintendo_nx.cpp @@ -15,7 +15,7 @@ */ #include -namespace ams::clkrst { +namespace ams { namespace { @@ -47,14 +47,15 @@ namespace ams::clkrst { constexpr inline const struct { const ClkRstDefinition *definition; DeviceCode device_code; + pcv::Module pcv_module; } ClkRstDefinitionMap[] = { - { std::addressof(Definitions[0]), i2c::DeviceCode_I2c1 }, - { std::addressof(Definitions[1]), i2c::DeviceCode_I2c2 }, - { std::addressof(Definitions[2]), i2c::DeviceCode_I2c3 }, - { std::addressof(Definitions[3]), i2c::DeviceCode_I2c4 }, - { std::addressof(Definitions[4]), i2c::DeviceCode_I2c5 }, - { std::addressof(Definitions[5]), i2c::DeviceCode_I2c6 }, - { std::addressof(Definitions[6]), pwm::DeviceCode_LcdBacklight }, + { std::addressof(Definitions[0]), i2c::DeviceCode_I2c1, pcv::Module_I2c1 }, + { std::addressof(Definitions[1]), i2c::DeviceCode_I2c2, pcv::Module_I2c2 }, + { std::addressof(Definitions[2]), i2c::DeviceCode_I2c3, pcv::Module_I2c3 }, + { std::addressof(Definitions[3]), i2c::DeviceCode_I2c4, pcv::Module_I2c4 }, + { std::addressof(Definitions[4]), i2c::DeviceCode_I2c5, pcv::Module_I2c5 }, + { std::addressof(Definitions[5]), i2c::DeviceCode_I2c6, pcv::Module_I2c6 }, + { std::addressof(Definitions[6]), pwm::DeviceCode_LcdBacklight, pcv::Module_Pwm }, }; ALWAYS_INLINE const ClkRstDefinition *GetDefinition(DeviceCode device_code) { @@ -71,7 +72,21 @@ namespace ams::clkrst { return def; } - ALWAYS_INLINE const ClkRstDefinition &GetDefinition(ClkRstSession *session) { + ALWAYS_INLINE const ClkRstDefinition &GetDefinition(pcv::Module module) { + const ClkRstDefinition *def = nullptr; + + for (const auto &entry : ClkRstDefinitionMap) { + if (entry.pcv_module == module) { + def = entry.definition; + break; + } + } + + AMS_ABORT_UNLESS(def != nullptr); + return *def; + } + + ALWAYS_INLINE const ClkRstDefinition &GetDefinition(clkrst::ClkRstSession *session) { const ClkRstDefinition *def = nullptr; for (const auto &entry : ClkRstDefinitionMap) { @@ -85,65 +100,108 @@ namespace ams::clkrst { return *def; } + void SetResetEnabled(const ClkRstDefinition &def, bool en) { + /* Set or clear reset. */ + reg::ReadWrite(g_clkrst_registers + def.rst_ofs, (en ? 1u : 0u) << def.rst_index, 1u << def.rst_index); + } + + void SetClockEnabled(const ClkRstDefinition &def, bool en) { + /* Set or clear reset. */ + reg::ReadWrite(g_clkrst_registers + def.clk_en_ofs, (en ? 1u : 0u) << def.clk_en_index, 1u << def.clk_en_index); + } + + void SetClockRate(const ClkRstDefinition &def, u32 hz) { + /* Enable clock. */ + reg::ReadWrite(g_clkrst_registers + def.clk_en_ofs, 1u << def.clk_en_index, 1u << def.clk_en_index); + + /* Set the clock divisor. */ + reg::ReadWrite(g_clkrst_registers + def.clk_src_ofs, CLK_RST_REG_BITS_VALUE(CLK_SOURCE_CLK_DIVISOR, def.clk_divisor)); + + /* Wait for 2us for clock setting to take. */ + os::SleepThread(TimeSpan::FromMicroSeconds(2)); + + /* Set the clock source. */ + reg::ReadWrite(g_clkrst_registers + def.clk_src_ofs, CLK_RST_REG_BITS_VALUE(CLK_SOURCE_CLK_SOURCE, def.clk_src)); + + /* Wait for 2us for clock setting to take. */ + os::SleepThread(TimeSpan::FromMicroSeconds(2)); + } + } - void Initialize() { - /* ... */ + namespace clkrst { + + void Initialize() { + /* ... */ + } + + void Finalize() { + /* ... */ + } + + Result OpenSession(ClkRstSession *out, DeviceCode device_code) { + /* Get the relevant definition. */ + out->_session = const_cast(GetDefinition(device_code)); + return ResultSuccess(); + } + + void CloseSession(ClkRstSession *session) { + /* Clear the session. */ + session->_session = nullptr; + } + + void SetResetAsserted(ClkRstSession *session) { + /* Assert reset. */ + SetResetEnabled(GetDefinition(session), true); + } + + void SetResetDeasserted(ClkRstSession *session) { + /* Assert reset. */ + SetResetEnabled(GetDefinition(session), false); + } + + void SetClockRate(ClkRstSession *session, u32 hz) { + /* Set the clock rate. */ + SetClockRate(GetDefinition(session), hz); + } + + void SetClockDisabled(ClkRstSession *session) { + AMS_ABORT("SetClockDisabled not implemented for boot system module"); + } + } - void Finalize() { - /* ... */ - } + namespace pcv { - Result OpenSession(ClkRstSession *out, DeviceCode device_code) { - /* Get the relevant definition. */ - out->_session = const_cast(GetDefinition(device_code)); - return ResultSuccess(); - } + void Initialize() { + /* ... */ + } - void CloseSession(ClkRstSession *session) { - /* Clear the session. */ - session->_session = nullptr; - } + void Finalize() { + /* ... */ + } - void SetResetAsserted(ClkRstSession *session) { - /* Get the definition. */ - const auto &def = GetDefinition(session); + Result SetClockEnabled(Module module, bool en) { + /* Set clock. */ + SetClockEnabled(GetDefinition(module), en); - /* Assert reset. */ - reg::ReadWrite(g_clkrst_registers + def.rst_ofs, 1u << def.rst_index, 1u << def.rst_index); - } + return ResultSuccess(); + } - void SetResetDeasserted(ClkRstSession *session) { - /* Get the definition. */ - const auto &def = GetDefinition(session); + Result SetClockRate(Module module, ClockHz hz) { + /* Set the clock rate. */ + SetClockRate(GetDefinition(module), hz); - /* Clear reset. */ - reg::ReadWrite(g_clkrst_registers + def.rst_ofs, 0, 1u << def.rst_index); - } + return ResultSuccess(); + } - void SetClockRate(ClkRstSession *session, u32 hz) { - /* Get the definition. */ - const auto &def = GetDefinition(session); + Result SetReset(Module module, bool en) { + /* Set reset. */ + SetResetEnabled(GetDefinition(module), en); - /* Enable clock. */ - reg::ReadWrite(g_clkrst_registers + def.clk_en_ofs, 1u << def.clk_en_index, 1u << def.clk_en_index); + return ResultSuccess(); + } - /* Set the clock divisor. */ - reg::ReadWrite(g_clkrst_registers + def.clk_src_ofs, CLK_RST_REG_BITS_VALUE(CLK_SOURCE_CLK_DIVISOR, def.clk_divisor)); - - /* Wait for 2us for clock setting to take. */ - os::SleepThread(TimeSpan::FromMicroSeconds(2)); - - /* Set the clock source. */ - reg::ReadWrite(g_clkrst_registers + def.clk_src_ofs, CLK_RST_REG_BITS_VALUE(CLK_SOURCE_CLK_SOURCE, def.clk_src)); - - /* Wait for 2us for clock setting to take. */ - os::SleepThread(TimeSpan::FromMicroSeconds(2)); - } - - void SetClockDisabled(ClkRstSession *session) { - AMS_ABORT("SetClockDisabled not implemented for boot system module"); } } diff --git a/stratosphere/boot/source/boot_driver_management.cpp b/stratosphere/boot/source/boot_driver_management.cpp index 9fe63b017..0bfcb18a6 100644 --- a/stratosphere/boot/source/boot_driver_management.cpp +++ b/stratosphere/boot/source/boot_driver_management.cpp @@ -40,13 +40,13 @@ namespace ams::boot { i2c::driver::Initialize(); /* Initialize the pwm client library with the server manager object. */ - /* TODO: pwm::InitializeWith(pwm::server::GetServiceObject()); */ + pwm::InitializeWith(pwm::server::GetServiceObject()); /* Initialize the pwm board driver. */ - /* TODO: pwm::driver::board::Initialize(); */ + pwm::driver::board::Initialize(); /* Initialize the pwm driver library. */ - /* TODO: pwm::driver::Initialize(); */ + pwm::driver::Initialize(); } }