Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions drivers/pwm/pwm-apple-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ static void apple_pwm_test_enable_holds_power(struct kunit *test)
KUNIT_ASSERT_EQ(test, apple_pwm_apply(ctx->chip, ctx->pwm, &off), 0);
KUNIT_EXPECT_EQ(test, apple_pwm_test_usage(ctx), usage);

KUNIT_ASSERT_EQ(test, apple_pwm_test_apply(ctx, 1000000, 250000, true), 0);
KUNIT_EXPECT_EQ(test, apple_pwm_test_usage(ctx), usage + 1);
KUNIT_ASSERT_EQ(test, apple_pwm_test_apply(ctx, 1000000, 0, true), 0);
KUNIT_EXPECT_EQ(test, apple_pwm_test_usage(ctx), usage);
KUNIT_EXPECT_FALSE(test, ctx->fpwm->enabled);

KUNIT_ASSERT_EQ(test, apple_pwm_test_apply(ctx, 1000000, 250000, true), 0);
KUNIT_EXPECT_EQ(test, apple_pwm_test_usage(ctx), usage + 1);
KUNIT_ASSERT_EQ(test, apple_pwm_test_apply(ctx, 1000000, 250000, false), 0);
Expand Down Expand Up @@ -504,6 +510,44 @@ static void apple_pwm_test_runtime_pm_keeps_registers(struct kunit *test)
KUNIT_EXPECT_EQ(test, apple_pwm_test_reg(ctx, APPLE_PWM_OFF_CYCLES), 0x9abcdef0);
}

/*
* An enabled zero-duty state (or a duty that rounds down to zero cycles)
* releases the output power hold while preserving readback across register loss.
*/
static void apple_pwm_test_zero_duty_runtime_pm_readback(struct kunit *test)
{
struct apple_pwm_test *ctx = test->priv;
struct pwm_state hw;
int usage = apple_pwm_test_usage(ctx);

if (!IS_ENABLED(CONFIG_PM))
kunit_skip(test, "needs CONFIG_PM");

/* Explicit zero duty */
KUNIT_ASSERT_EQ(test, apple_pwm_test_apply(ctx, 1000000, 0, true), 0);
KUNIT_EXPECT_EQ(test, apple_pwm_test_usage(ctx), usage);
KUNIT_EXPECT_FALSE(test, ctx->fpwm->enabled);
KUNIT_ASSERT_EQ(test, apple_pwm_runtime_suspend(ctx->dev), 0);
apple_pwm_test_set_regs(ctx, 0, 0, 0);
KUNIT_ASSERT_EQ(test, apple_pwm_runtime_resume(ctx->dev), 0);
KUNIT_ASSERT_EQ(test, pwm_get_state_hw(ctx->pwm, &hw), 0);
KUNIT_EXPECT_TRUE(test, hw.enabled);
KUNIT_EXPECT_EQ(test, hw.duty_cycle, 0);
KUNIT_EXPECT_EQ(test, hw.period, 1000000);

/* Non-zero duty that rounds down to 0 cycles at 24 MHz (1 cycle = 41.67 ns) */
KUNIT_ASSERT_EQ(test, apple_pwm_test_apply(ctx, 1000000, 10, true), 0);
KUNIT_EXPECT_EQ(test, apple_pwm_test_usage(ctx), usage);
KUNIT_EXPECT_FALSE(test, ctx->fpwm->enabled);
KUNIT_ASSERT_EQ(test, apple_pwm_runtime_suspend(ctx->dev), 0);
apple_pwm_test_set_regs(ctx, 0, 0, 0);
KUNIT_ASSERT_EQ(test, apple_pwm_runtime_resume(ctx->dev), 0);
KUNIT_ASSERT_EQ(test, pwm_get_state_hw(ctx->pwm, &hw), 0);
KUNIT_EXPECT_TRUE(test, hw.enabled);
KUNIT_EXPECT_EQ(test, hw.duty_cycle, 0);
KUNIT_EXPECT_EQ(test, hw.period, 1000000);
}

static struct kunit_case apple_pwm_test_cases[] = {
KUNIT_CASE_PARAM(apple_pwm_test_apply_cycles, apple_pwm_test_conv_gen_params),
KUNIT_CASE_PARAM(apple_pwm_test_readback, apple_pwm_test_conv_gen_params),
Expand All @@ -518,6 +562,7 @@ static struct kunit_case apple_pwm_test_cases[] = {
KUNIT_CASE(apple_pwm_test_enable_holds_power),
KUNIT_CASE(apple_pwm_test_get_state_power_balanced),
KUNIT_CASE(apple_pwm_test_runtime_pm_keeps_registers),
KUNIT_CASE(apple_pwm_test_zero_duty_runtime_pm_readback),
{}
};

Expand Down
17 changes: 11 additions & 6 deletions drivers/pwm/pwm-apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ static int apple_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
if (ret)
return ret;

bool driving = false;

if (state->enabled) {
u64 on_cycles, off_cycles;

Expand All @@ -78,16 +80,17 @@ static int apple_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
writel(off_cycles, fpwm->base + APPLE_PWM_OFF_CYCLES);
writel(APPLE_PWM_CTRL_ENABLE | APPLE_PWM_CTRL_OUTPUT_ENABLE | APPLE_PWM_CTRL_UPDATE,
fpwm->base + APPLE_PWM_CTRL);
driving = on_cycles > 0;
} else {
writel(0, fpwm->base + APPLE_PWM_CTRL);
}

/* Keep the controller powered for as long as it drives an output. */
if (state->enabled && !fpwm->enabled)
/* Keep the controller powered only while it actively drives a non-zero output. */
if (driving && !fpwm->enabled)
pm_runtime_get_noresume(dev);
else if (!state->enabled && fpwm->enabled)
else if (!driving && fpwm->enabled)
pm_runtime_put_noidle(dev);
fpwm->enabled = state->enabled;
fpwm->enabled = driving;
pm_runtime_put(dev);
return 0;
}
Expand Down Expand Up @@ -212,10 +215,12 @@ static int apple_pwm_probe(struct platform_device *pdev)
return ret;
}

/* Preserve an output that the bootloader left running. */
/* Preserve a non-zero output that the bootloader left running. */
fpwm->ctrl = readl(fpwm->base + APPLE_PWM_CTRL);
fpwm->on_cycles = readl(fpwm->base + APPLE_PWM_ON_CYCLES);
fpwm->enabled = (fpwm->ctrl & APPLE_PWM_CTRL_ENABLE) &&
(fpwm->ctrl & APPLE_PWM_CTRL_OUTPUT_ENABLE);
(fpwm->ctrl & APPLE_PWM_CTRL_OUTPUT_ENABLE) &&
fpwm->on_cycles > 0;
pm_runtime_get_noresume(dev); /* probe reference */
if (fpwm->enabled)
pm_runtime_get_noresume(dev); /* output reference */
Expand Down
10 changes: 6 additions & 4 deletions tools/asahi/apple-pwm-pm-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ def function(source, name):
puts("PASS enabled output pins power; repeated apply/get_state do not leak");
state.duty_cycle = 0;
assert(!apple_pwm_apply(&chip, &pwm, &state));
assert(pdev.dev.active && pdev.dev.refs == 1);
assert(!pdev.dev.active && !pdev.dev.refs && !clock_model.refs);
assert(!apple_pwm_get_state(&chip, &pwm, &got) && got.enabled && !got.duty_cycle);
assert(!pdev.dev.active && !pdev.dev.refs && !clock_model.refs);
state.duty_cycle = 10000;
assert(!apple_pwm_apply(&chip, &pwm, &state));
puts("PASS enabled zero-duty output remains powered");
assert(pdev.dev.active && pdev.dev.refs == 1 && clock_model.refs == 1);
puts("PASS enabled zero-duty output suspends and resumes cleanly");
state.enabled = false;
assert(!apple_pwm_apply(&chip, &pwm, &state));
assert(!pdev.dev.active && !pdev.dev.refs && !clock_model.refs);
Expand Down Expand Up @@ -216,9 +218,9 @@ def main():
args = parser.parse_args()
source = (ROOT / 'drivers/pwm/pwm-apple.c').read_text()
if args.negative_control:
old = '\tif (state->enabled && !fpwm->enabled)\n\t\tpm_runtime_get_noresume(dev);'
old = '\tif (driving && !fpwm->enabled)\n\t\tpm_runtime_get_noresume(dev);'
assert old in source
source = source.replace(old, '\tif (state->enabled && !fpwm->enabled)\n\t\t; /* missing output reference */', 1)
source = source.replace(old, '\tif (driving && !fpwm->enabled)\n\t\t; /* missing output reference */', 1)
defines = '\n'.join(re.findall(r'^#define APPLE_PWM_.*$', source, re.M))
struct = re.search(r'struct apple_pwm \{.*?\n\};', source, re.S).group()
callbacks = '\n'.join(function(source, name) for name in (
Expand Down
Loading