Skip to content

Commit c5b8425

Browse files
fgasnier63Jonathan Cameron
authored andcommitted
counter: stm32-timer-cnt: add power management support
Add suspend/resume PM sleep ops. When going to low power, enforce the counter isn't active. Gracefully restore its state upon resume in case it's been left enabled prior to suspend. Acked-by: William Breathitt Gray <vilhelm.gray@gmail.com> Signed-off-by: Fabrice Gasnier <fabrice.gasnier@st.com> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent b9cd7be commit c5b8425

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

‎drivers/counter/stm32-timer-cnt.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/iio/types.h>
1313
#include <linux/mfd/stm32-timers.h>
1414
#include <linux/module.h>
15+
#include <linux/pinctrl/consumer.h>
1516
#include <linux/platform_device.h>
1617

1718
#define TIM_CCMR_CCXS (BIT(8) | BIT(0))
@@ -20,11 +21,20 @@
2021
#define TIM_CCER_MASK (TIM_CCER_CC1P | TIM_CCER_CC1NP | \
2122
TIM_CCER_CC2P | TIM_CCER_CC2NP)
2223

24+
struct stm32_timer_regs {
25+
u32 cr1;
26+
u32 cnt;
27+
u32 smcr;
28+
u32 arr;
29+
};
30+
2331
struct stm32_timer_cnt {
2432
struct counter_device counter;
2533
struct regmap *regmap;
2634
struct clk *clk;
2735
u32 ceiling;
36+
bool enabled;
37+
struct stm32_timer_regs bak;
2838
};
2939

3040
/**
@@ -224,6 +234,9 @@ static ssize_t stm32_count_enable_write(struct counter_device *counter,
224234
clk_disable(priv->clk);
225235
}
226236

237+
/* Keep enabled state to properly handle low power states */
238+
priv->enabled = enable;
239+
227240
return len;
228241
}
229242

@@ -358,10 +371,59 @@ static int stm32_timer_cnt_probe(struct platform_device *pdev)
358371
priv->counter.num_signals = ARRAY_SIZE(stm32_signals);
359372
priv->counter.priv = priv;
360373

374+
platform_set_drvdata(pdev, priv);
375+
361376
/* Register Counter device */
362377
return devm_counter_register(dev, &priv->counter);
363378
}
364379

380+
static int __maybe_unused stm32_timer_cnt_suspend(struct device *dev)
381+
{
382+
struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
383+
384+
/* Only take care of enabled counter: don't disturb other MFD child */
385+
if (priv->enabled) {
386+
/* Backup registers that may get lost in low power mode */
387+
regmap_read(priv->regmap, TIM_SMCR, &priv->bak.smcr);
388+
regmap_read(priv->regmap, TIM_ARR, &priv->bak.arr);
389+
regmap_read(priv->regmap, TIM_CNT, &priv->bak.cnt);
390+
regmap_read(priv->regmap, TIM_CR1, &priv->bak.cr1);
391+
392+
/* Disable the counter */
393+
regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
394+
clk_disable(priv->clk);
395+
}
396+
397+
return pinctrl_pm_select_sleep_state(dev);
398+
}
399+
400+
static int __maybe_unused stm32_timer_cnt_resume(struct device *dev)
401+
{
402+
struct stm32_timer_cnt *priv = dev_get_drvdata(dev);
403+
int ret;
404+
405+
ret = pinctrl_pm_select_default_state(dev);
406+
if (ret)
407+
return ret;
408+
409+
if (priv->enabled) {
410+
clk_enable(priv->clk);
411+
412+
/* Restore registers that may have been lost */
413+
regmap_write(priv->regmap, TIM_SMCR, priv->bak.smcr);
414+
regmap_write(priv->regmap, TIM_ARR, priv->bak.arr);
415+
regmap_write(priv->regmap, TIM_CNT, priv->bak.cnt);
416+
417+
/* Also re-enables the counter */
418+
regmap_write(priv->regmap, TIM_CR1, priv->bak.cr1);
419+
}
420+
421+
return 0;
422+
}
423+
424+
static SIMPLE_DEV_PM_OPS(stm32_timer_cnt_pm_ops, stm32_timer_cnt_suspend,
425+
stm32_timer_cnt_resume);
426+
365427
static const struct of_device_id stm32_timer_cnt_of_match[] = {
366428
{ .compatible = "st,stm32-timer-counter", },
367429
{},
@@ -373,6 +435,7 @@ static struct platform_driver stm32_timer_cnt_driver = {
373435
.driver = {
374436
.name = "stm32-timer-counter",
375437
.of_match_table = stm32_timer_cnt_of_match,
438+
.pm = &stm32_timer_cnt_pm_ops,
376439
},
377440
};
378441
module_platform_driver(stm32_timer_cnt_driver);

0 commit comments

Comments
 (0)