2014-02-27 16:39:49 +07:00
|
|
|
/*
|
|
|
|
* Freescale FlexTimer Module (FTM) PWM Driver
|
|
|
|
*
|
|
|
|
* Copyright 2012-2013 Freescale Semiconductor, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/of_address.h>
|
2018-06-09 02:22:35 +07:00
|
|
|
#include <linux/of_device.h>
|
2014-02-27 16:39:49 +07:00
|
|
|
#include <linux/platform_device.h>
|
2014-10-15 12:21:35 +07:00
|
|
|
#include <linux/pm.h>
|
2014-02-27 16:39:49 +07:00
|
|
|
#include <linux/pwm.h>
|
2014-08-19 11:38:02 +07:00
|
|
|
#include <linux/regmap.h>
|
2014-02-27 16:39:49 +07:00
|
|
|
#include <linux/slab.h>
|
2019-04-02 13:30:48 +07:00
|
|
|
#include <linux/fsl/ftm.h>
|
2014-02-27 16:39:49 +07:00
|
|
|
|
2014-08-19 11:38:01 +07:00
|
|
|
#define FTM_SC_CLK(c) (((c) + 1) << FTM_SC_CLK_MASK_SHIFT)
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
enum fsl_pwm_clk {
|
|
|
|
FSL_PWM_CLK_SYS,
|
|
|
|
FSL_PWM_CLK_FIX,
|
|
|
|
FSL_PWM_CLK_EXT,
|
|
|
|
FSL_PWM_CLK_CNTEN,
|
|
|
|
FSL_PWM_CLK_MAX
|
|
|
|
};
|
|
|
|
|
2018-06-09 02:22:35 +07:00
|
|
|
struct fsl_ftm_soc {
|
|
|
|
bool has_enable_bits;
|
|
|
|
};
|
|
|
|
|
2014-02-27 16:39:49 +07:00
|
|
|
struct fsl_pwm_chip {
|
|
|
|
struct pwm_chip chip;
|
|
|
|
|
|
|
|
struct mutex lock;
|
|
|
|
|
|
|
|
unsigned int cnt_select;
|
|
|
|
unsigned int clk_ps;
|
|
|
|
|
2014-08-19 11:38:02 +07:00
|
|
|
struct regmap *regmap;
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
int period_ns;
|
|
|
|
|
2018-06-09 02:22:34 +07:00
|
|
|
struct clk *ipg_clk;
|
2014-02-27 16:39:49 +07:00
|
|
|
struct clk *clk[FSL_PWM_CLK_MAX];
|
2018-06-09 02:22:35 +07:00
|
|
|
|
|
|
|
const struct fsl_ftm_soc *soc;
|
2014-02-27 16:39:49 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct fsl_pwm_chip *to_fsl_chip(struct pwm_chip *chip)
|
|
|
|
{
|
|
|
|
return container_of(chip, struct fsl_pwm_chip, chip);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fsl_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
2018-06-09 02:22:35 +07:00
|
|
|
int ret;
|
2014-02-27 16:39:49 +07:00
|
|
|
struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
|
|
|
|
|
2018-06-09 02:22:35 +07:00
|
|
|
ret = clk_prepare_enable(fpc->ipg_clk);
|
|
|
|
if (!ret && fpc->soc->has_enable_bits) {
|
|
|
|
mutex_lock(&fpc->lock);
|
|
|
|
regmap_update_bits(fpc->regmap, FTM_SC, BIT(pwm->hwpwm + 16),
|
|
|
|
BIT(pwm->hwpwm + 16));
|
|
|
|
mutex_unlock(&fpc->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2014-02-27 16:39:49 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fsl_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
|
|
|
|
|
2018-06-09 02:22:35 +07:00
|
|
|
if (fpc->soc->has_enable_bits) {
|
|
|
|
mutex_lock(&fpc->lock);
|
|
|
|
regmap_update_bits(fpc->regmap, FTM_SC, BIT(pwm->hwpwm + 16),
|
|
|
|
0);
|
|
|
|
mutex_unlock(&fpc->lock);
|
|
|
|
}
|
|
|
|
|
2018-06-09 02:22:34 +07:00
|
|
|
clk_disable_unprepare(fpc->ipg_clk);
|
2014-02-27 16:39:49 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fsl_pwm_calculate_default_ps(struct fsl_pwm_chip *fpc,
|
|
|
|
enum fsl_pwm_clk index)
|
|
|
|
{
|
|
|
|
unsigned long sys_rate, cnt_rate;
|
|
|
|
unsigned long long ratio;
|
|
|
|
|
|
|
|
sys_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_SYS]);
|
|
|
|
if (!sys_rate)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
cnt_rate = clk_get_rate(fpc->clk[fpc->cnt_select]);
|
|
|
|
if (!cnt_rate)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (index) {
|
|
|
|
case FSL_PWM_CLK_SYS:
|
|
|
|
fpc->clk_ps = 1;
|
|
|
|
break;
|
|
|
|
case FSL_PWM_CLK_FIX:
|
|
|
|
ratio = 2 * cnt_rate - 1;
|
|
|
|
do_div(ratio, sys_rate);
|
|
|
|
fpc->clk_ps = ratio;
|
|
|
|
break;
|
|
|
|
case FSL_PWM_CLK_EXT:
|
|
|
|
ratio = 4 * cnt_rate - 1;
|
|
|
|
do_div(ratio, sys_rate);
|
|
|
|
fpc->clk_ps = ratio;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long fsl_pwm_calculate_cycles(struct fsl_pwm_chip *fpc,
|
|
|
|
unsigned long period_ns)
|
|
|
|
{
|
|
|
|
unsigned long long c, c0;
|
|
|
|
|
|
|
|
c = clk_get_rate(fpc->clk[fpc->cnt_select]);
|
|
|
|
c = c * period_ns;
|
|
|
|
do_div(c, 1000000000UL);
|
|
|
|
|
|
|
|
do {
|
|
|
|
c0 = c;
|
|
|
|
do_div(c0, (1 << fpc->clk_ps));
|
|
|
|
if (c0 <= 0xFFFF)
|
|
|
|
return (unsigned long)c0;
|
|
|
|
} while (++fpc->clk_ps < 8);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long fsl_pwm_calculate_period_cycles(struct fsl_pwm_chip *fpc,
|
|
|
|
unsigned long period_ns,
|
|
|
|
enum fsl_pwm_clk index)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = fsl_pwm_calculate_default_ps(fpc, index);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(fpc->chip.dev,
|
|
|
|
"failed to calculate default prescaler: %d\n",
|
|
|
|
ret);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fsl_pwm_calculate_cycles(fpc, period_ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc,
|
|
|
|
unsigned long period_ns)
|
|
|
|
{
|
|
|
|
enum fsl_pwm_clk m0, m1;
|
|
|
|
unsigned long fix_rate, ext_rate, cycles;
|
|
|
|
|
|
|
|
cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns,
|
|
|
|
FSL_PWM_CLK_SYS);
|
|
|
|
if (cycles) {
|
|
|
|
fpc->cnt_select = FSL_PWM_CLK_SYS;
|
|
|
|
return cycles;
|
|
|
|
}
|
|
|
|
|
|
|
|
fix_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_FIX]);
|
|
|
|
ext_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_EXT]);
|
|
|
|
|
|
|
|
if (fix_rate > ext_rate) {
|
|
|
|
m0 = FSL_PWM_CLK_FIX;
|
|
|
|
m1 = FSL_PWM_CLK_EXT;
|
|
|
|
} else {
|
|
|
|
m0 = FSL_PWM_CLK_EXT;
|
|
|
|
m1 = FSL_PWM_CLK_FIX;
|
|
|
|
}
|
|
|
|
|
|
|
|
cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns, m0);
|
|
|
|
if (cycles) {
|
|
|
|
fpc->cnt_select = m0;
|
|
|
|
return cycles;
|
|
|
|
}
|
|
|
|
|
|
|
|
fpc->cnt_select = m1;
|
|
|
|
|
|
|
|
return fsl_pwm_calculate_period_cycles(fpc, period_ns, m1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc,
|
|
|
|
unsigned long period_ns,
|
|
|
|
unsigned long duty_ns)
|
|
|
|
{
|
2014-08-19 11:38:02 +07:00
|
|
|
unsigned long long duty;
|
|
|
|
u32 val;
|
2014-02-27 16:39:49 +07:00
|
|
|
|
2014-08-19 11:38:02 +07:00
|
|
|
regmap_read(fpc->regmap, FTM_MOD, &val);
|
|
|
|
duty = (unsigned long long)duty_ns * (val + 1);
|
2014-02-27 16:39:49 +07:00
|
|
|
do_div(duty, period_ns);
|
|
|
|
|
|
|
|
return (unsigned long)duty;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fsl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
|
|
|
|
int duty_ns, int period_ns)
|
|
|
|
{
|
|
|
|
struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
|
2014-08-19 11:38:02 +07:00
|
|
|
u32 period, duty;
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
mutex_lock(&fpc->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Freescale FTM controller supports only a single period for
|
|
|
|
* all PWM channels, therefore incompatible changes need to be
|
|
|
|
* refused.
|
|
|
|
*/
|
|
|
|
if (fpc->period_ns && fpc->period_ns != period_ns) {
|
|
|
|
dev_err(fpc->chip.dev,
|
|
|
|
"conflicting period requested for PWM %u\n",
|
|
|
|
pwm->hwpwm);
|
|
|
|
mutex_unlock(&fpc->lock);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fpc->period_ns && duty_ns) {
|
|
|
|
period = fsl_pwm_calculate_period(fpc, period_ns);
|
|
|
|
if (!period) {
|
|
|
|
dev_err(fpc->chip.dev, "failed to calculate period\n");
|
|
|
|
mutex_unlock(&fpc->lock);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-08-19 11:38:02 +07:00
|
|
|
regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_PS_MASK,
|
|
|
|
fpc->clk_ps);
|
|
|
|
regmap_write(fpc->regmap, FTM_MOD, period - 1);
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
fpc->period_ns = period_ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&fpc->lock);
|
|
|
|
|
|
|
|
duty = fsl_pwm_calculate_duty(fpc, period_ns, duty_ns);
|
|
|
|
|
2014-08-19 11:38:02 +07:00
|
|
|
regmap_write(fpc->regmap, FTM_CSC(pwm->hwpwm),
|
|
|
|
FTM_CSC_MSB | FTM_CSC_ELSB);
|
|
|
|
regmap_write(fpc->regmap, FTM_CV(pwm->hwpwm), duty);
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fsl_pwm_set_polarity(struct pwm_chip *chip,
|
|
|
|
struct pwm_device *pwm,
|
|
|
|
enum pwm_polarity polarity)
|
|
|
|
{
|
|
|
|
struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
|
|
|
|
u32 val;
|
|
|
|
|
2014-08-19 11:38:02 +07:00
|
|
|
regmap_read(fpc->regmap, FTM_POL, &val);
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
if (polarity == PWM_POLARITY_INVERSED)
|
|
|
|
val |= BIT(pwm->hwpwm);
|
|
|
|
else
|
|
|
|
val &= ~BIT(pwm->hwpwm);
|
|
|
|
|
2014-08-19 11:38:02 +07:00
|
|
|
regmap_write(fpc->regmap, FTM_POL, val);
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fsl_counter_clock_enable(struct fsl_pwm_chip *fpc)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* select counter clock source */
|
2014-08-19 11:38:02 +07:00
|
|
|
regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_CLK_MASK,
|
|
|
|
FTM_SC_CLK(fpc->cnt_select));
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
ret = clk_prepare_enable(fpc->clk[fpc->cnt_select]);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
|
|
|
|
if (ret) {
|
|
|
|
clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fsl_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&fpc->lock);
|
2014-08-19 11:38:02 +07:00
|
|
|
regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm), 0);
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
ret = fsl_counter_clock_enable(fpc);
|
|
|
|
mutex_unlock(&fpc->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fsl_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct fsl_pwm_chip *fpc = to_fsl_chip(chip);
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
mutex_lock(&fpc->lock);
|
2014-08-19 11:38:02 +07:00
|
|
|
regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm),
|
|
|
|
BIT(pwm->hwpwm));
|
2014-02-27 16:39:49 +07:00
|
|
|
|
pwm: fsl-ftm: Fix clock enable/disable when using PM
A FTM PWM instance enables/disables three clocks: The bus clock, the
counter clock and the PWM clock. The bus clock gets enabled on
pwm_request, whereas the counter and PWM clocks will be enabled upon
pwm_enable.
The driver has three closesly related issues when enabling/disabling
clocks during suspend/resume:
- The three clocks are not treated differently in regards to the
individual PWM state enabled/requested. This can lead to clocks
getting disabled which have not been enabled in the first place
(a PWM channel which only has been requested going through
suspend/resume).
- When entering suspend, the current behavior relies on the
FTM_OUTMASK register: If a PWM output is unmasked, the driver
assumes the clocks are enabled. However, some PWM instances
have only 2 channels connected (e.g. Vybrid's FTM1). In that case,
the FTM_OUTMASK reads 0x3 if all channels are disabled, even if
the code wrote 0xff to it before. For those PWM instances, the
current approach to detect enabled PWM signals does not work.
- A third issue applies to the bus clock only, which can get enabled
multiple times (once for each PWM channel of a PWM chip). This is
fine, however when entering suspend mode, the clock only gets
disabled once.
This change introduces a different approach by relying on the enable
and prepared counters of the clock framework and using the frameworks
PWM signal states to address all three issues.
Clocks get disabled during suspend and back enabled on resume
regarding to the PWM channels individual state (requested/enabled).
Since we do not count the clock enables in the driver, this change no
longer clears the Status and Control registers Clock Source Selection
(FTM_SC[CLKS]). However, since we disable the selected clock anyway,
and we explicitly select the clock source on reenabling a PWM channel
this approach should not make a difference in practice.
Signed-off-by: Stefan Agner <stefan@agner.ch>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2015-11-24 05:45:07 +07:00
|
|
|
clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
|
|
|
|
clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
|
2014-02-27 16:39:49 +07:00
|
|
|
|
2014-08-19 11:38:02 +07:00
|
|
|
regmap_read(fpc->regmap, FTM_OUTMASK, &val);
|
2014-02-27 16:39:49 +07:00
|
|
|
if ((val & 0xFF) == 0xFF)
|
|
|
|
fpc->period_ns = 0;
|
|
|
|
|
|
|
|
mutex_unlock(&fpc->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct pwm_ops fsl_pwm_ops = {
|
|
|
|
.request = fsl_pwm_request,
|
|
|
|
.free = fsl_pwm_free,
|
|
|
|
.config = fsl_pwm_config,
|
|
|
|
.set_polarity = fsl_pwm_set_polarity,
|
|
|
|
.enable = fsl_pwm_enable,
|
|
|
|
.disable = fsl_pwm_disable,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int fsl_pwm_init(struct fsl_pwm_chip *fpc)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-06-09 02:22:34 +07:00
|
|
|
ret = clk_prepare_enable(fpc->ipg_clk);
|
2014-02-27 16:39:49 +07:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2014-08-19 11:38:02 +07:00
|
|
|
regmap_write(fpc->regmap, FTM_CNTIN, 0x00);
|
|
|
|
regmap_write(fpc->regmap, FTM_OUTINIT, 0x00);
|
|
|
|
regmap_write(fpc->regmap, FTM_OUTMASK, 0xFF);
|
2014-02-27 16:39:49 +07:00
|
|
|
|
2018-06-09 02:22:34 +07:00
|
|
|
clk_disable_unprepare(fpc->ipg_clk);
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-15 12:21:34 +07:00
|
|
|
static bool fsl_pwm_volatile_reg(struct device *dev, unsigned int reg)
|
|
|
|
{
|
|
|
|
switch (reg) {
|
|
|
|
case FTM_CNT:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-19 11:38:02 +07:00
|
|
|
static const struct regmap_config fsl_pwm_regmap_config = {
|
|
|
|
.reg_bits = 32,
|
|
|
|
.reg_stride = 4,
|
|
|
|
.val_bits = 32,
|
|
|
|
|
|
|
|
.max_register = FTM_PWMLOAD,
|
2014-10-15 12:21:34 +07:00
|
|
|
.volatile_reg = fsl_pwm_volatile_reg,
|
2016-01-21 09:56:22 +07:00
|
|
|
.cache_type = REGCACHE_FLAT,
|
2014-08-19 11:38:02 +07:00
|
|
|
};
|
|
|
|
|
2014-02-27 16:39:49 +07:00
|
|
|
static int fsl_pwm_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct fsl_pwm_chip *fpc;
|
|
|
|
struct resource *res;
|
2014-08-19 11:38:02 +07:00
|
|
|
void __iomem *base;
|
2014-02-27 16:39:49 +07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
fpc = devm_kzalloc(&pdev->dev, sizeof(*fpc), GFP_KERNEL);
|
|
|
|
if (!fpc)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mutex_init(&fpc->lock);
|
|
|
|
|
2018-06-09 02:22:35 +07:00
|
|
|
fpc->soc = of_device_get_match_data(&pdev->dev);
|
2014-02-27 16:39:49 +07:00
|
|
|
fpc->chip.dev = &pdev->dev;
|
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2014-08-19 11:38:02 +07:00
|
|
|
base = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(base))
|
|
|
|
return PTR_ERR(base);
|
|
|
|
|
2014-10-15 12:21:35 +07:00
|
|
|
fpc->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "ftm_sys", base,
|
2014-08-19 11:38:02 +07:00
|
|
|
&fsl_pwm_regmap_config);
|
|
|
|
if (IS_ERR(fpc->regmap)) {
|
|
|
|
dev_err(&pdev->dev, "regmap init failed\n");
|
|
|
|
return PTR_ERR(fpc->regmap);
|
|
|
|
}
|
2014-02-27 16:39:49 +07:00
|
|
|
|
|
|
|
fpc->clk[FSL_PWM_CLK_SYS] = devm_clk_get(&pdev->dev, "ftm_sys");
|
|
|
|
if (IS_ERR(fpc->clk[FSL_PWM_CLK_SYS])) {
|
|
|
|
dev_err(&pdev->dev, "failed to get \"ftm_sys\" clock\n");
|
|
|
|
return PTR_ERR(fpc->clk[FSL_PWM_CLK_SYS]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fpc->clk[FSL_PWM_CLK_FIX] = devm_clk_get(fpc->chip.dev, "ftm_fix");
|
|
|
|
if (IS_ERR(fpc->clk[FSL_PWM_CLK_FIX]))
|
|
|
|
return PTR_ERR(fpc->clk[FSL_PWM_CLK_FIX]);
|
|
|
|
|
|
|
|
fpc->clk[FSL_PWM_CLK_EXT] = devm_clk_get(fpc->chip.dev, "ftm_ext");
|
|
|
|
if (IS_ERR(fpc->clk[FSL_PWM_CLK_EXT]))
|
|
|
|
return PTR_ERR(fpc->clk[FSL_PWM_CLK_EXT]);
|
|
|
|
|
|
|
|
fpc->clk[FSL_PWM_CLK_CNTEN] =
|
|
|
|
devm_clk_get(fpc->chip.dev, "ftm_cnt_clk_en");
|
|
|
|
if (IS_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]))
|
|
|
|
return PTR_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]);
|
|
|
|
|
2018-06-09 02:22:34 +07:00
|
|
|
/*
|
|
|
|
* ipg_clk is the interface clock for the IP. If not provided, use the
|
|
|
|
* ftm_sys clock as the default.
|
|
|
|
*/
|
|
|
|
fpc->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
|
|
|
|
if (IS_ERR(fpc->ipg_clk))
|
|
|
|
fpc->ipg_clk = fpc->clk[FSL_PWM_CLK_SYS];
|
|
|
|
|
|
|
|
|
2014-02-27 16:39:49 +07:00
|
|
|
fpc->chip.ops = &fsl_pwm_ops;
|
|
|
|
fpc->chip.of_xlate = of_pwm_xlate_with_flags;
|
|
|
|
fpc->chip.of_pwm_n_cells = 3;
|
|
|
|
fpc->chip.base = -1;
|
|
|
|
fpc->chip.npwm = 8;
|
|
|
|
|
|
|
|
ret = pwmchip_add(&fpc->chip);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, fpc);
|
|
|
|
|
|
|
|
return fsl_pwm_init(fpc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fsl_pwm_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct fsl_pwm_chip *fpc = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
return pwmchip_remove(&fpc->chip);
|
|
|
|
}
|
|
|
|
|
2014-10-15 12:21:35 +07:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
|
|
|
static int fsl_pwm_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
|
pwm: fsl-ftm: Fix clock enable/disable when using PM
A FTM PWM instance enables/disables three clocks: The bus clock, the
counter clock and the PWM clock. The bus clock gets enabled on
pwm_request, whereas the counter and PWM clocks will be enabled upon
pwm_enable.
The driver has three closesly related issues when enabling/disabling
clocks during suspend/resume:
- The three clocks are not treated differently in regards to the
individual PWM state enabled/requested. This can lead to clocks
getting disabled which have not been enabled in the first place
(a PWM channel which only has been requested going through
suspend/resume).
- When entering suspend, the current behavior relies on the
FTM_OUTMASK register: If a PWM output is unmasked, the driver
assumes the clocks are enabled. However, some PWM instances
have only 2 channels connected (e.g. Vybrid's FTM1). In that case,
the FTM_OUTMASK reads 0x3 if all channels are disabled, even if
the code wrote 0xff to it before. For those PWM instances, the
current approach to detect enabled PWM signals does not work.
- A third issue applies to the bus clock only, which can get enabled
multiple times (once for each PWM channel of a PWM chip). This is
fine, however when entering suspend mode, the clock only gets
disabled once.
This change introduces a different approach by relying on the enable
and prepared counters of the clock framework and using the frameworks
PWM signal states to address all three issues.
Clocks get disabled during suspend and back enabled on resume
regarding to the PWM channels individual state (requested/enabled).
Since we do not count the clock enables in the driver, this change no
longer clears the Status and Control registers Clock Source Selection
(FTM_SC[CLKS]). However, since we disable the selected clock anyway,
and we explicitly select the clock source on reenabling a PWM channel
this approach should not make a difference in practice.
Signed-off-by: Stefan Agner <stefan@agner.ch>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2015-11-24 05:45:07 +07:00
|
|
|
int i;
|
2014-10-15 12:21:35 +07:00
|
|
|
|
|
|
|
regcache_cache_only(fpc->regmap, true);
|
|
|
|
regcache_mark_dirty(fpc->regmap);
|
|
|
|
|
pwm: fsl-ftm: Fix clock enable/disable when using PM
A FTM PWM instance enables/disables three clocks: The bus clock, the
counter clock and the PWM clock. The bus clock gets enabled on
pwm_request, whereas the counter and PWM clocks will be enabled upon
pwm_enable.
The driver has three closesly related issues when enabling/disabling
clocks during suspend/resume:
- The three clocks are not treated differently in regards to the
individual PWM state enabled/requested. This can lead to clocks
getting disabled which have not been enabled in the first place
(a PWM channel which only has been requested going through
suspend/resume).
- When entering suspend, the current behavior relies on the
FTM_OUTMASK register: If a PWM output is unmasked, the driver
assumes the clocks are enabled. However, some PWM instances
have only 2 channels connected (e.g. Vybrid's FTM1). In that case,
the FTM_OUTMASK reads 0x3 if all channels are disabled, even if
the code wrote 0xff to it before. For those PWM instances, the
current approach to detect enabled PWM signals does not work.
- A third issue applies to the bus clock only, which can get enabled
multiple times (once for each PWM channel of a PWM chip). This is
fine, however when entering suspend mode, the clock only gets
disabled once.
This change introduces a different approach by relying on the enable
and prepared counters of the clock framework and using the frameworks
PWM signal states to address all three issues.
Clocks get disabled during suspend and back enabled on resume
regarding to the PWM channels individual state (requested/enabled).
Since we do not count the clock enables in the driver, this change no
longer clears the Status and Control registers Clock Source Selection
(FTM_SC[CLKS]). However, since we disable the selected clock anyway,
and we explicitly select the clock source on reenabling a PWM channel
this approach should not make a difference in practice.
Signed-off-by: Stefan Agner <stefan@agner.ch>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2015-11-24 05:45:07 +07:00
|
|
|
for (i = 0; i < fpc->chip.npwm; i++) {
|
|
|
|
struct pwm_device *pwm = &fpc->chip.pwms[i];
|
|
|
|
|
|
|
|
if (!test_bit(PWMF_REQUESTED, &pwm->flags))
|
|
|
|
continue;
|
|
|
|
|
2018-06-09 02:22:34 +07:00
|
|
|
clk_disable_unprepare(fpc->ipg_clk);
|
pwm: fsl-ftm: Fix clock enable/disable when using PM
A FTM PWM instance enables/disables three clocks: The bus clock, the
counter clock and the PWM clock. The bus clock gets enabled on
pwm_request, whereas the counter and PWM clocks will be enabled upon
pwm_enable.
The driver has three closesly related issues when enabling/disabling
clocks during suspend/resume:
- The three clocks are not treated differently in regards to the
individual PWM state enabled/requested. This can lead to clocks
getting disabled which have not been enabled in the first place
(a PWM channel which only has been requested going through
suspend/resume).
- When entering suspend, the current behavior relies on the
FTM_OUTMASK register: If a PWM output is unmasked, the driver
assumes the clocks are enabled. However, some PWM instances
have only 2 channels connected (e.g. Vybrid's FTM1). In that case,
the FTM_OUTMASK reads 0x3 if all channels are disabled, even if
the code wrote 0xff to it before. For those PWM instances, the
current approach to detect enabled PWM signals does not work.
- A third issue applies to the bus clock only, which can get enabled
multiple times (once for each PWM channel of a PWM chip). This is
fine, however when entering suspend mode, the clock only gets
disabled once.
This change introduces a different approach by relying on the enable
and prepared counters of the clock framework and using the frameworks
PWM signal states to address all three issues.
Clocks get disabled during suspend and back enabled on resume
regarding to the PWM channels individual state (requested/enabled).
Since we do not count the clock enables in the driver, this change no
longer clears the Status and Control registers Clock Source Selection
(FTM_SC[CLKS]). However, since we disable the selected clock anyway,
and we explicitly select the clock source on reenabling a PWM channel
this approach should not make a difference in practice.
Signed-off-by: Stefan Agner <stefan@agner.ch>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2015-11-24 05:45:07 +07:00
|
|
|
|
|
|
|
if (!pwm_is_enabled(pwm))
|
|
|
|
continue;
|
|
|
|
|
2014-10-15 12:21:35 +07:00
|
|
|
clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]);
|
|
|
|
clk_disable_unprepare(fpc->clk[fpc->cnt_select]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fsl_pwm_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct fsl_pwm_chip *fpc = dev_get_drvdata(dev);
|
pwm: fsl-ftm: Fix clock enable/disable when using PM
A FTM PWM instance enables/disables three clocks: The bus clock, the
counter clock and the PWM clock. The bus clock gets enabled on
pwm_request, whereas the counter and PWM clocks will be enabled upon
pwm_enable.
The driver has three closesly related issues when enabling/disabling
clocks during suspend/resume:
- The three clocks are not treated differently in regards to the
individual PWM state enabled/requested. This can lead to clocks
getting disabled which have not been enabled in the first place
(a PWM channel which only has been requested going through
suspend/resume).
- When entering suspend, the current behavior relies on the
FTM_OUTMASK register: If a PWM output is unmasked, the driver
assumes the clocks are enabled. However, some PWM instances
have only 2 channels connected (e.g. Vybrid's FTM1). In that case,
the FTM_OUTMASK reads 0x3 if all channels are disabled, even if
the code wrote 0xff to it before. For those PWM instances, the
current approach to detect enabled PWM signals does not work.
- A third issue applies to the bus clock only, which can get enabled
multiple times (once for each PWM channel of a PWM chip). This is
fine, however when entering suspend mode, the clock only gets
disabled once.
This change introduces a different approach by relying on the enable
and prepared counters of the clock framework and using the frameworks
PWM signal states to address all three issues.
Clocks get disabled during suspend and back enabled on resume
regarding to the PWM channels individual state (requested/enabled).
Since we do not count the clock enables in the driver, this change no
longer clears the Status and Control registers Clock Source Selection
(FTM_SC[CLKS]). However, since we disable the selected clock anyway,
and we explicitly select the clock source on reenabling a PWM channel
this approach should not make a difference in practice.
Signed-off-by: Stefan Agner <stefan@agner.ch>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2015-11-24 05:45:07 +07:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < fpc->chip.npwm; i++) {
|
|
|
|
struct pwm_device *pwm = &fpc->chip.pwms[i];
|
|
|
|
|
|
|
|
if (!test_bit(PWMF_REQUESTED, &pwm->flags))
|
|
|
|
continue;
|
2014-10-15 12:21:35 +07:00
|
|
|
|
2018-06-09 02:22:34 +07:00
|
|
|
clk_prepare_enable(fpc->ipg_clk);
|
pwm: fsl-ftm: Fix clock enable/disable when using PM
A FTM PWM instance enables/disables three clocks: The bus clock, the
counter clock and the PWM clock. The bus clock gets enabled on
pwm_request, whereas the counter and PWM clocks will be enabled upon
pwm_enable.
The driver has three closesly related issues when enabling/disabling
clocks during suspend/resume:
- The three clocks are not treated differently in regards to the
individual PWM state enabled/requested. This can lead to clocks
getting disabled which have not been enabled in the first place
(a PWM channel which only has been requested going through
suspend/resume).
- When entering suspend, the current behavior relies on the
FTM_OUTMASK register: If a PWM output is unmasked, the driver
assumes the clocks are enabled. However, some PWM instances
have only 2 channels connected (e.g. Vybrid's FTM1). In that case,
the FTM_OUTMASK reads 0x3 if all channels are disabled, even if
the code wrote 0xff to it before. For those PWM instances, the
current approach to detect enabled PWM signals does not work.
- A third issue applies to the bus clock only, which can get enabled
multiple times (once for each PWM channel of a PWM chip). This is
fine, however when entering suspend mode, the clock only gets
disabled once.
This change introduces a different approach by relying on the enable
and prepared counters of the clock framework and using the frameworks
PWM signal states to address all three issues.
Clocks get disabled during suspend and back enabled on resume
regarding to the PWM channels individual state (requested/enabled).
Since we do not count the clock enables in the driver, this change no
longer clears the Status and Control registers Clock Source Selection
(FTM_SC[CLKS]). However, since we disable the selected clock anyway,
and we explicitly select the clock source on reenabling a PWM channel
this approach should not make a difference in practice.
Signed-off-by: Stefan Agner <stefan@agner.ch>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
2015-11-24 05:45:07 +07:00
|
|
|
|
|
|
|
if (!pwm_is_enabled(pwm))
|
|
|
|
continue;
|
|
|
|
|
2014-10-15 12:21:35 +07:00
|
|
|
clk_prepare_enable(fpc->clk[fpc->cnt_select]);
|
|
|
|
clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore all registers from cache */
|
|
|
|
regcache_cache_only(fpc->regmap, false);
|
|
|
|
regcache_sync(fpc->regmap);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const struct dev_pm_ops fsl_pwm_pm_ops = {
|
|
|
|
SET_SYSTEM_SLEEP_PM_OPS(fsl_pwm_suspend, fsl_pwm_resume)
|
|
|
|
};
|
|
|
|
|
2018-06-09 02:22:35 +07:00
|
|
|
static const struct fsl_ftm_soc vf610_ftm_pwm = {
|
|
|
|
.has_enable_bits = false,
|
|
|
|
};
|
|
|
|
|
2018-06-09 02:22:36 +07:00
|
|
|
static const struct fsl_ftm_soc imx8qm_ftm_pwm = {
|
|
|
|
.has_enable_bits = true,
|
|
|
|
};
|
|
|
|
|
2014-02-27 16:39:49 +07:00
|
|
|
static const struct of_device_id fsl_pwm_dt_ids[] = {
|
2018-06-09 02:22:35 +07:00
|
|
|
{ .compatible = "fsl,vf610-ftm-pwm", .data = &vf610_ftm_pwm },
|
2018-06-09 02:22:36 +07:00
|
|
|
{ .compatible = "fsl,imx8qm-ftm-pwm", .data = &imx8qm_ftm_pwm },
|
2014-02-27 16:39:49 +07:00
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids);
|
|
|
|
|
|
|
|
static struct platform_driver fsl_pwm_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "fsl-ftm-pwm",
|
|
|
|
.of_match_table = fsl_pwm_dt_ids,
|
2014-10-15 12:21:35 +07:00
|
|
|
.pm = &fsl_pwm_pm_ops,
|
2014-02-27 16:39:49 +07:00
|
|
|
},
|
|
|
|
.probe = fsl_pwm_probe,
|
|
|
|
.remove = fsl_pwm_remove,
|
|
|
|
};
|
|
|
|
module_platform_driver(fsl_pwm_driver);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Freescale FlexTimer Module PWM Driver");
|
|
|
|
MODULE_AUTHOR("Xiubo Li <Li.Xiubo@freescale.com>");
|
|
|
|
MODULE_ALIAS("platform:fsl-ftm-pwm");
|
|
|
|
MODULE_LICENSE("GPL");
|