Skip to content

Commit

Permalink
Merge "vibrator: Add a temperature awareness mechanism" into qt-qpr1-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaseTeng authored and Android (Google) Code Review committed Feb 25, 2020
2 parents a5aac13 + 9658b5d commit 5e04973
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion device.mk
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ PRODUCT_PRODUCT_PROPERTIES +=\
ro.vibrator.hal.heavyclick.duration=6 \
ro.vibrator.hal.short.voltage=161 \
ro.vibrator.hal.long.voltage=161 \
ro.vibrator.hal.long.frequency.shift=0 \
ro.vibrator.hal.long.frequency.shift=10 \
ro.vibrator.hal.steady.shape=1

# Thermal HAL
Expand Down
8 changes: 8 additions & 0 deletions vibrator/common/HardwareBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class HwApiBase {
protected:
template <typename T>
void open(const std::string &name, T *stream);
template <typename T>
void openFull(const std::string &name, T *stream);
bool has(const std::ios &stream);
template <typename T>
bool get(T *value, std::istream *stream);
Expand All @@ -93,6 +95,12 @@ void HwApiBase::open(const std::string &name, T *stream) {
utils::openNoCreate(mPathPrefix + name, stream);
}

template <typename T>
void HwApiBase::openFull(const std::string &name, T *stream) {
mNames[stream] = name;
utils::openNoCreate(name, stream);
}

template <typename T>
bool HwApiBase::get(T *value, std::istream *stream) {
ATRACE_NAME("HwApi::get");
Expand Down
6 changes: 5 additions & 1 deletion vibrator/drv2624/Hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#ifndef ANDROID_HARDWARE_VIBRATOR_HARDWARE_H
#define ANDROID_HARDWARE_VIBRATOR_HARDWARE_H

#include "HardwareBase.h"
#include "../common/HardwareBase.h"
#include "Vibrator.h"

namespace android {
Expand Down Expand Up @@ -54,6 +54,7 @@ class HwApi : public Vibrator::HwApi, private HwApiBase {
bool setLpTriggerEffect(uint32_t value) override { return set(value, &mLpTrigger); }
bool setLraWaveShape(uint32_t value) override { return set(value, &mLraWaveShape); }
bool setOdClamp(uint32_t value) override { return set(value, &mOdClamp); }
bool getUsbTemp(int32_t *value) override { return get(value, &mUsbTemp); }
void debug(int fd) override { HwApiBase::debug(fd); }

private:
Expand All @@ -71,6 +72,8 @@ class HwApi : public Vibrator::HwApi, private HwApiBase {
open("device/lp_trigger_effect", &mLpTrigger);
open("device/lra_wave_shape", &mLraWaveShape);
open("device/od_clamp", &mOdClamp);
// TODO: for future new architecture: b/149610125
openFull("/sys/devices/virtual/thermal/tz-by-name/usbc-therm-monitor/temp", &mUsbTemp);
}

private:
Expand All @@ -87,6 +90,7 @@ class HwApi : public Vibrator::HwApi, private HwApiBase {
std::ofstream mLpTrigger;
std::ofstream mLraWaveShape;
std::ofstream mOdClamp;
std::ifstream mUsbTemp;
};

class HwCal : public Vibrator::HwCal, private HwCalBase {
Expand Down
24 changes: 20 additions & 4 deletions vibrator/drv2624/Vibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ static constexpr std::array<float, 3> STEADY_TARGET_G = {1.2, 1.145, 0.905};

#define FLOAT_EPS 1e-6

// Temperature protection upper bound 10°C and lower bound 5°C
static constexpr int32_t TEMP_UPPER_BOUND = 10000;
static constexpr int32_t TEMP_LOWER_BOUND = 5000;
// Steady vibration's voltage in lower bound guarantee
static uint32_t STEADY_VOLTAGE_LOWER_BOUND = 90; // 1.8 Vpeak

Expand Down Expand Up @@ -249,11 +252,14 @@ Vibrator::Vibrator(std::unique_ptr<HwApi> hwapi, std::unique_ptr<HwCal> hwcal)
mSteadyConfig.reset(new VibrationConfig({
.shape = (shape == UINT32_MAX) ? WaveShape::SQUARE : static_cast<WaveShape>(shape),
.odClamp = &mSteadyTargetOdClamp,
// 1. Change long lra period to frequency
// 2. Get frequency': subtract the frequency shift from the frequency
// 3. Get final long lra period after put the frequency' to formula
.olLraPeriod = freqPeriodFormula(freqPeriodFormula(lraPeriod) - longFreqencyShift),
.olLraPeriod = lraPeriod,
}));
mSteadyOlLraPeriod = lraPeriod;
// 1. Change long lra period to frequency
// 2. Get frequency': subtract the frequency shift from the frequency
// 3. Get final long lra period after put the frequency' to formula
mSteadyOlLraPeriodShift =
freqPeriodFormula(freqPeriodFormula(lraPeriod) - longFreqencyShift);
} else {
mHwApi->setOlLraPeriod(lraPeriod);
}
Expand Down Expand Up @@ -304,7 +310,17 @@ Return<Status> Vibrator::on(uint32_t timeoutMs, const char mode[],

// Methods from ::android::hardware::vibrator::V1_2::IVibrator follow.
Return<Status> Vibrator::on(uint32_t timeoutMs) {
int usbTemp;
ATRACE_NAME("Vibrator::on");
mHwApi->getUsbTemp(&usbTemp);
if (usbTemp > TEMP_UPPER_BOUND) {
mSteadyConfig->odClamp = &mSteadyTargetOdClamp;
mSteadyConfig->olLraPeriod = mSteadyOlLraPeriod;
} else if (usbTemp < TEMP_LOWER_BOUND) {
mSteadyConfig->odClamp = &STEADY_VOLTAGE_LOWER_BOUND;
mSteadyConfig->olLraPeriod = mSteadyOlLraPeriodShift;
}

return on(timeoutMs, RTP_MODE, mSteadyConfig, 0);
}

Expand Down
4 changes: 4 additions & 0 deletions vibrator/drv2624/Vibrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class Vibrator : public IVibrator {
// Specifies the maximum voltage for automatic overdrive and automatic
// braking periods.
virtual bool setOdClamp(uint32_t value) = 0;
// Get battery temperature sensor value
virtual bool getUsbTemp(int32_t *value) = 0;
// Emit diagnostic information to the given file.
virtual void debug(int fd) = 0;
};
Expand Down Expand Up @@ -190,6 +192,8 @@ class Vibrator : public IVibrator {
uint32_t mHeavyClickDuration;
std::array<uint32_t, 5> mEffectTargetOdClamp;
uint32_t mSteadyTargetOdClamp;
uint32_t mSteadyOlLraPeriod;
uint32_t mSteadyOlLraPeriodShift;
};

} // namespace implementation
Expand Down
1 change: 1 addition & 0 deletions vibrator/drv2624/tests/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class MockApi : public ::android::hardware::vibrator::V1_3::implementation::Vibr
MOCK_METHOD1(setLpTriggerEffect, bool(uint32_t value));
MOCK_METHOD1(setLraWaveShape, bool(uint32_t value));
MOCK_METHOD1(setOdClamp, bool(uint32_t value));
MOCK_METHOD1(getUsbTemp, bool(int32_t *value));
MOCK_METHOD1(debug, void(int fd));

~MockApi() override { destructor(); };
Expand Down

0 comments on commit 5e04973

Please sign in to comment.