From 8b66ff518e93ba8e8a17679e5c67e097b2d2dc6d Mon Sep 17 00:00:00 2001 From: Dhina17 Date: Thu, 6 Feb 2025 03:00:25 +0800 Subject: [PATCH] redwood: Implement torch light control Signed-off-by: rahul Co-Authored-By: rahul Co-Authored-By: John Galt <7730016+realjohngalt@users.noreply.github.com> --- BoardConfig.mk | 1 + camera/Android.bp | 22 +++++++++ camera/CameraProviderExtension.cpp | 75 ++++++++++++++++++++++++++++++ rootdir/etc/ueventd.rc | 4 ++ sepolicy/vendor/cameraserver.te | 1 + sepolicy/vendor/genfs_contexts | 5 ++ sepolicy/vendor/torch.te | 1 + 7 files changed, 109 insertions(+) create mode 100644 camera/Android.bp create mode 100644 camera/CameraProviderExtension.cpp create mode 100644 sepolicy/vendor/cameraserver.te create mode 100644 sepolicy/vendor/torch.te diff --git a/BoardConfig.mk b/BoardConfig.mk index 86a2bf0..9938ce4 100644 --- a/BoardConfig.mk +++ b/BoardConfig.mk @@ -50,6 +50,7 @@ TARGET_BOOTLOADER_BOARD_NAME := redwood TARGET_NO_BOOTLOADER := true # Camera +TARGET_CAMERA_SERVICE_EXT_LIB := //$(DEVICE_PATH):libcameraservice_extension.xiaomi_redwood TARGET_CAMERA_OVERRIDE_FORMAT_FROM_RESERVED := true TARGET_CAMERA_PACKAGE_NAME := com.android.camera diff --git a/camera/Android.bp b/camera/Android.bp new file mode 100644 index 0000000..713ae7e --- /dev/null +++ b/camera/Android.bp @@ -0,0 +1,22 @@ +// +// Copyright (C) 2022 The LineageOS Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +cc_library_static { + name: "libcameraservice_extension.xiaomi_redwood", + srcs: ["CameraProviderExtension.cpp"], + include_dirs: [ + "frameworks/av/services/camera/libcameraservice/common" + ], +} diff --git a/camera/CameraProviderExtension.cpp b/camera/CameraProviderExtension.cpp new file mode 100644 index 0000000..1bc56b3 --- /dev/null +++ b/camera/CameraProviderExtension.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2024 LibreMobileOS Foundation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "CameraProviderExtension.h" + +#include + +#define TORCH_BRIGHTNESS "brightness" +#define TORCH_MAX_BRIGHTNESS "max_brightness" +#define TOGGLE_SWITCH "/sys/devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-02/c440000.qcom,spmi:qcom,pm8350c@2:qcom,flash_led@ee00/leds/led:switch_0/brightness" +static std::string kTorchLedPath = "/sys/devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-02/c440000.qcom,spmi:qcom,pm8350c@2:qcom,flash_led@ee00/leds/led:torch_0"; + +/** + * Write value to path and close file. + */ +template +static void set(const std::string& path, const T& value) { + std::ofstream file(path); + file << value; +} + +/** + * Read value from the path and close file. + */ +template +static T get(const std::string& path, const T& def) { + std::ifstream file(path); + T result; + + file >> result; + return file.fail() ? def : result; +} + +bool supportsTorchStrengthControlExt() { + return true; +} + +bool supportsSetTorchModeExt() { + return false; +} + +int32_t getTorchDefaultStrengthLevelExt() { + // Our default value is 75. This corresponds to 15%. + // As we have changed the maximum value, 59% now corresponds to 75. + return 59; +} + +int32_t getTorchMaxStrengthLevelExt() { + // 255 out of 500 is a sane brightness. + // Let's cap it to 255 as max, we can go much higher, but I don't want to test this. + return 255; +} + +int32_t getTorchStrengthLevelExt() { + // We write same value in the both LEDs, + // so get from one. + auto node = kTorchLedPath + "/" + TORCH_BRIGHTNESS; + return get(node, 0); +} + +void setTorchStrengthLevelExt(int32_t torchStrength, bool enabled) { + set(TOGGLE_SWITCH, 0); + auto node = kTorchLedPath + "/" + TORCH_BRIGHTNESS; + set(node, torchStrength); + if (enabled) + set(TOGGLE_SWITCH, 255); +} + +void setTorchModeExt(bool enabled) { + int32_t strength = getTorchDefaultStrengthLevelExt(); + setTorchStrengthLevelExt(enabled ? strength : 0, enabled); +} diff --git a/rootdir/etc/ueventd.rc b/rootdir/etc/ueventd.rc index 869f822..0349d02 100644 --- a/rootdir/etc/ueventd.rc +++ b/rootdir/etc/ueventd.rc @@ -143,3 +143,7 @@ firmware_directories /vendor/firmware_mnt/image/ # Fingerprint /dev/goodix_fp 0660 system system + +# Torch control +/sys/devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-02/c440000.qcom,spmi:qcom,pm8350c@2:qcom,flash_led@ee00/leds/led:torch_0 brightness 0660 cameraserver camera +/sys/devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-02/c440000.qcom,spmi:qcom,pm8350c@2:qcom,flash_led@ee00/leds/led:switch_0 0660 cameraserver camera diff --git a/sepolicy/vendor/cameraserver.te b/sepolicy/vendor/cameraserver.te new file mode 100644 index 0000000..438be33 --- /dev/null +++ b/sepolicy/vendor/cameraserver.te @@ -0,0 +1 @@ +allow cameraserver sysfs_torch:file rw_file_perms; diff --git a/sepolicy/vendor/genfs_contexts b/sepolicy/vendor/genfs_contexts index 809b461..940990c 100644 --- a/sepolicy/vendor/genfs_contexts +++ b/sepolicy/vendor/genfs_contexts @@ -4,6 +4,11 @@ genfscon sysfs /devices/platform/soc/soc:qcom,dsi-display-primary u:object_r:ven # USB genfscon sysfs /devices/platform/soc/soc:qcom,pmic_glink/soc:qcom,pmic_glink:qcom,ucsi/typec u:object_r:vendor_sysfs_usb_c:s0 +# Torch control +genfscon sysfs /devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-02/c440000.qcom,spmi:qcom,pm8350c@2:qcom,flash_led@ee00/leds/led:torch_0/brightness u:object_r:sysfs_torch:s0 +genfscon sysfs /devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-02/c440000.qcom,spmi:qcom,pm8350c@2:qcom,flash_led@ee00/leds/led:torch_0/max_brightness u:object_r:sysfs_torch:s0 +genfscon sysfs /devices/platform/soc/c440000.qcom,spmi/spmi-0/spmi0-02/c440000.qcom,spmi:qcom,pm8350c@2:qcom,flash_led@ee00/leds/led:switch_0/brightness u:object_r:sysfs_torch:s0 + # Wakeup nodes genfscon sysfs /devices/platform/goodix_ts.0/wakeup u:object_r:sysfs_wakeup:s0 genfscon sysfs /devices/platform/soc/17300000.qcom,lpass/subsys6/wakeup u:object_r:sysfs_wakeup:s0 diff --git a/sepolicy/vendor/torch.te b/sepolicy/vendor/torch.te new file mode 100644 index 0000000..b8b6b42 --- /dev/null +++ b/sepolicy/vendor/torch.te @@ -0,0 +1 @@ +type sysfs_torch, fs_type, sysfs_type;