Commit 215c29d3 authored by Sascha Hauer's avatar Sascha Hauer Committed by Thierry Reding

ARM Samsung: Move s3c pwm driver to pwm framework

Move the driver to drivers/pwm/ and convert it to use the framework.
Signed-off-by: default avatarSascha Hauer <s.hauer@pengutronix.de>
Cc: Ben Dooks <ben-linux@fluff.org>
Cc: Kukjin Kim <kgene.kim@samsung.com>
[eric@eukrea.com: fix pwmchip_add return code test]
Signed-off-by: default avatarEric Bénard <eric@eukrea.com>
Signed-off-by: default avatarThierry Reding <thierry.reding@avionic-design.de>
parent 29693248
...@@ -63,7 +63,3 @@ obj-$(CONFIG_S5P_SLEEP) += s5p-sleep.o ...@@ -63,7 +63,3 @@ obj-$(CONFIG_S5P_SLEEP) += s5p-sleep.o
# PD support # PD support
obj-$(CONFIG_SAMSUNG_PD) += pd.o obj-$(CONFIG_SAMSUNG_PD) += pd.o
# PWM support
obj-$(CONFIG_HAVE_PWM) += pwm.o
...@@ -36,6 +36,15 @@ config PWM_PXA ...@@ -36,6 +36,15 @@ config PWM_PXA
To compile this driver as a module, choose M here: the module To compile this driver as a module, choose M here: the module
will be called pwm-pxa. will be called pwm-pxa.
config PWM_SAMSUNG
tristate "Samsung pwm support"
depends on PLAT_SAMSUNG
help
Generic PWM framework driver for Samsung.
To compile this driver as a module, choose M here: the module
will be called pwm-samsung.
config PWM_TEGRA config PWM_TEGRA
tristate "NVIDIA Tegra PWM support" tristate "NVIDIA Tegra PWM support"
depends on ARCH_TEGRA depends on ARCH_TEGRA
......
...@@ -2,4 +2,5 @@ obj-$(CONFIG_PWM) += core.o ...@@ -2,4 +2,5 @@ obj-$(CONFIG_PWM) += core.o
obj-$(CONFIG_PWM_BFIN) += pwm-bfin.o obj-$(CONFIG_PWM_BFIN) += pwm-bfin.o
obj-$(CONFIG_PWM_IMX) += pwm-imx.o obj-$(CONFIG_PWM_IMX) += pwm-imx.o
obj-$(CONFIG_PWM_PXA) += pwm-pxa.o obj-$(CONFIG_PWM_PXA) += pwm-pxa.o
obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o
obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
...@@ -24,8 +24,7 @@ ...@@ -24,8 +24,7 @@
#include <plat/regs-timer.h> #include <plat/regs-timer.h>
struct pwm_device { struct s3c_chip {
struct list_head list;
struct platform_device *pdev; struct platform_device *pdev;
struct clk *clk_div; struct clk *clk_div;
...@@ -36,117 +35,65 @@ struct pwm_device { ...@@ -36,117 +35,65 @@ struct pwm_device {
unsigned int duty_ns; unsigned int duty_ns;
unsigned char tcon_base; unsigned char tcon_base;
unsigned char running;
unsigned char use_count;
unsigned char pwm_id; unsigned char pwm_id;
struct pwm_chip chip;
}; };
#define to_s3c_chip(chip) container_of(chip, struct s3c_chip, chip)
#define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg) #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
static struct clk *clk_scaler[2]; static struct clk *clk_scaler[2];
static inline int pwm_is_tdiv(struct pwm_device *pwm) static inline int pwm_is_tdiv(struct s3c_chip *chip)
{
return clk_get_parent(pwm->clk) == pwm->clk_div;
}
static DEFINE_MUTEX(pwm_lock);
static LIST_HEAD(pwm_list);
struct pwm_device *pwm_request(int pwm_id, const char *label)
{
struct pwm_device *pwm;
int found = 0;
mutex_lock(&pwm_lock);
list_for_each_entry(pwm, &pwm_list, list) {
if (pwm->pwm_id == pwm_id) {
found = 1;
break;
}
}
if (found) {
if (pwm->use_count == 0) {
pwm->use_count = 1;
pwm->label = label;
} else
pwm = ERR_PTR(-EBUSY);
} else
pwm = ERR_PTR(-ENOENT);
mutex_unlock(&pwm_lock);
return pwm;
}
EXPORT_SYMBOL(pwm_request);
void pwm_free(struct pwm_device *pwm)
{ {
mutex_lock(&pwm_lock); return clk_get_parent(chip->clk) == chip->clk_div;
if (pwm->use_count) {
pwm->use_count--;
pwm->label = NULL;
} else
printk(KERN_ERR "PWM%d device already freed\n", pwm->pwm_id);
mutex_unlock(&pwm_lock);
} }
EXPORT_SYMBOL(pwm_free);
#define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0)) #define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0))
#define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2)) #define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2))
#define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3)) #define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3))
#define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1)) #define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1))
int pwm_enable(struct pwm_device *pwm) static int s3c_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
{ {
struct s3c_chip *s3c = to_s3c_chip(chip);
unsigned long flags; unsigned long flags;
unsigned long tcon; unsigned long tcon;
local_irq_save(flags); local_irq_save(flags);
tcon = __raw_readl(S3C2410_TCON); tcon = __raw_readl(S3C2410_TCON);
tcon |= pwm_tcon_start(pwm); tcon |= pwm_tcon_start(s3c);
__raw_writel(tcon, S3C2410_TCON); __raw_writel(tcon, S3C2410_TCON);
local_irq_restore(flags); local_irq_restore(flags);
pwm->running = 1;
return 0; return 0;
} }
EXPORT_SYMBOL(pwm_enable); static void s3c_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
void pwm_disable(struct pwm_device *pwm)
{ {
struct s3c_chip *s3c = to_s3c_chip(chip);
unsigned long flags; unsigned long flags;
unsigned long tcon; unsigned long tcon;
local_irq_save(flags); local_irq_save(flags);
tcon = __raw_readl(S3C2410_TCON); tcon = __raw_readl(S3C2410_TCON);
tcon &= ~pwm_tcon_start(pwm); tcon &= ~pwm_tcon_start(s3c);
__raw_writel(tcon, S3C2410_TCON); __raw_writel(tcon, S3C2410_TCON);
local_irq_restore(flags); local_irq_restore(flags);
pwm->running = 0;
} }
EXPORT_SYMBOL(pwm_disable); static unsigned long pwm_calc_tin(struct s3c_chip *s3c, unsigned long freq)
static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
{ {
unsigned long tin_parent_rate; unsigned long tin_parent_rate;
unsigned int div; unsigned int div;
tin_parent_rate = clk_get_rate(clk_get_parent(pwm->clk_div)); tin_parent_rate = clk_get_rate(clk_get_parent(s3c->clk_div));
pwm_dbg(pwm, "tin parent at %lu\n", tin_parent_rate); pwm_dbg(s3c, "tin parent at %lu\n", tin_parent_rate);
for (div = 2; div <= 16; div *= 2) { for (div = 2; div <= 16; div *= 2) {
if ((tin_parent_rate / (div << 16)) < freq) if ((tin_parent_rate / (div << 16)) < freq)
...@@ -158,8 +105,10 @@ static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq) ...@@ -158,8 +105,10 @@ static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
#define NS_IN_HZ (1000000000UL) #define NS_IN_HZ (1000000000UL)
int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) static int s3c_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
int duty_ns, int period_ns)
{ {
struct s3c_chip *s3c = to_s3c_chip(chip);
unsigned long tin_rate; unsigned long tin_rate;
unsigned long tin_ns; unsigned long tin_ns;
unsigned long period; unsigned long period;
...@@ -178,38 +127,38 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) ...@@ -178,38 +127,38 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
if (duty_ns > period_ns) if (duty_ns > period_ns)
return -EINVAL; return -EINVAL;
if (period_ns == pwm->period_ns && if (period_ns == s3c->period_ns &&
duty_ns == pwm->duty_ns) duty_ns == s3c->duty_ns)
return 0; return 0;
/* The TCMP and TCNT can be read without a lock, they're not /* The TCMP and TCNT can be read without a lock, they're not
* shared between the timers. */ * shared between the timers. */
tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id)); tcmp = __raw_readl(S3C2410_TCMPB(s3c->pwm_id));
tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id)); tcnt = __raw_readl(S3C2410_TCNTB(s3c->pwm_id));
period = NS_IN_HZ / period_ns; period = NS_IN_HZ / period_ns;
pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n", pwm_dbg(s3c, "duty_ns=%d, period_ns=%d (%lu)\n",
duty_ns, period_ns, period); duty_ns, period_ns, period);
/* Check to see if we are changing the clock rate of the PWM */ /* Check to see if we are changing the clock rate of the PWM */
if (pwm->period_ns != period_ns) { if (s3c->period_ns != period_ns) {
if (pwm_is_tdiv(pwm)) { if (pwm_is_tdiv(s3c)) {
tin_rate = pwm_calc_tin(pwm, period); tin_rate = pwm_calc_tin(s3c, period);
clk_set_rate(pwm->clk_div, tin_rate); clk_set_rate(s3c->clk_div, tin_rate);
} else } else
tin_rate = clk_get_rate(pwm->clk); tin_rate = clk_get_rate(s3c->clk);
pwm->period_ns = period_ns; s3c->period_ns = period_ns;
pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate); pwm_dbg(s3c, "tin_rate=%lu\n", tin_rate);
tin_ns = NS_IN_HZ / tin_rate; tin_ns = NS_IN_HZ / tin_rate;
tcnt = period_ns / tin_ns; tcnt = period_ns / tin_ns;
} else } else
tin_ns = NS_IN_HZ / clk_get_rate(pwm->clk); tin_ns = NS_IN_HZ / clk_get_rate(s3c->clk);
/* Note, counters count down */ /* Note, counters count down */
...@@ -220,7 +169,7 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) ...@@ -220,7 +169,7 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
if (tcmp == tcnt) if (tcmp == tcnt)
tcmp--; tcmp--;
pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt); pwm_dbg(s3c, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
if (tcmp < 0) if (tcmp < 0)
tcmp = 0; tcmp = 0;
...@@ -229,15 +178,15 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) ...@@ -229,15 +178,15 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
local_irq_save(flags); local_irq_save(flags);
__raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id)); __raw_writel(tcmp, S3C2410_TCMPB(s3c->pwm_id));
__raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id)); __raw_writel(tcnt, S3C2410_TCNTB(s3c->pwm_id));
tcon = __raw_readl(S3C2410_TCON); tcon = __raw_readl(S3C2410_TCON);
tcon |= pwm_tcon_manulupdate(pwm); tcon |= pwm_tcon_manulupdate(s3c);
tcon |= pwm_tcon_autoreload(pwm); tcon |= pwm_tcon_autoreload(s3c);
__raw_writel(tcon, S3C2410_TCON); __raw_writel(tcon, S3C2410_TCON);
tcon &= ~pwm_tcon_manulupdate(pwm); tcon &= ~pwm_tcon_manulupdate(s3c);
__raw_writel(tcon, S3C2410_TCON); __raw_writel(tcon, S3C2410_TCON);
local_irq_restore(flags); local_irq_restore(flags);
...@@ -245,24 +194,17 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) ...@@ -245,24 +194,17 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
return 0; return 0;
} }
EXPORT_SYMBOL(pwm_config); static struct pwm_ops s3c_pwm_ops = {
.enable = s3c_pwm_enable,
static int pwm_register(struct pwm_device *pwm) .disable = s3c_pwm_disable,
{ .config = s3c_pwm_config,
pwm->duty_ns = -1; .owner = THIS_MODULE,
pwm->period_ns = -1; };
mutex_lock(&pwm_lock);
list_add_tail(&pwm->list, &pwm_list);
mutex_unlock(&pwm_lock);
return 0;
}
static int s3c_pwm_probe(struct platform_device *pdev) static int s3c_pwm_probe(struct platform_device *pdev)
{ {
struct device *dev = &pdev->dev; struct device *dev = &pdev->dev;
struct pwm_device *pwm; struct s3c_chip *s3c;
unsigned long flags; unsigned long flags;
unsigned long tcon; unsigned long tcon;
unsigned int id = pdev->id; unsigned int id = pdev->id;
...@@ -273,83 +215,87 @@ static int s3c_pwm_probe(struct platform_device *pdev) ...@@ -273,83 +215,87 @@ static int s3c_pwm_probe(struct platform_device *pdev)
return -ENXIO; return -ENXIO;
} }
pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL); s3c = kzalloc(sizeof(*s3c), GFP_KERNEL);
if (pwm == NULL) { if (s3c == NULL) {
dev_err(dev, "failed to allocate pwm_device\n"); dev_err(dev, "failed to allocate pwm_device\n");
return -ENOMEM; return -ENOMEM;
} }
pwm->pdev = pdev;
pwm->pwm_id = id;
/* calculate base of control bits in TCON */ /* calculate base of control bits in TCON */
pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4; s3c->tcon_base = id == 0 ? 0 : (id * 4) + 4;
s3c->chip.ops = &s3c_pwm_ops;
s3c->chip.base = -1;
s3c->chip.npwm = 1;
pwm->clk = clk_get(dev, "pwm-tin"); s3c->clk = clk_get(dev, "pwm-tin");
if (IS_ERR(pwm->clk)) { if (IS_ERR(s3c->clk)) {
dev_err(dev, "failed to get pwm tin clk\n"); dev_err(dev, "failed to get pwm tin clk\n");
ret = PTR_ERR(pwm->clk); ret = PTR_ERR(s3c->clk);
goto err_alloc; goto err_alloc;
} }
pwm->clk_div = clk_get(dev, "pwm-tdiv"); s3c->clk_div = clk_get(dev, "pwm-tdiv");
if (IS_ERR(pwm->clk_div)) { if (IS_ERR(s3c->clk_div)) {
dev_err(dev, "failed to get pwm tdiv clk\n"); dev_err(dev, "failed to get pwm tdiv clk\n");
ret = PTR_ERR(pwm->clk_div); ret = PTR_ERR(s3c->clk_div);
goto err_clk_tin; goto err_clk_tin;
} }
clk_enable(pwm->clk); clk_enable(s3c->clk);
clk_enable(pwm->clk_div); clk_enable(s3c->clk_div);
local_irq_save(flags); local_irq_save(flags);
tcon = __raw_readl(S3C2410_TCON); tcon = __raw_readl(S3C2410_TCON);
tcon |= pwm_tcon_invert(pwm); tcon |= pwm_tcon_invert(s3c);
__raw_writel(tcon, S3C2410_TCON); __raw_writel(tcon, S3C2410_TCON);
local_irq_restore(flags); local_irq_restore(flags);
ret = pwmchip_add(&s3c->chip);
ret = pwm_register(pwm); if (ret < 0) {
if (ret) {
dev_err(dev, "failed to register pwm\n"); dev_err(dev, "failed to register pwm\n");
goto err_clk_tdiv; goto err_clk_tdiv;
} }
pwm_dbg(pwm, "config bits %02x\n", pwm_dbg(s3c, "config bits %02x\n",
(__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f); (__raw_readl(S3C2410_TCON) >> s3c->tcon_base) & 0x0f);
dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n", dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
clk_get_rate(pwm->clk), clk_get_rate(s3c->clk),
clk_get_rate(pwm->clk_div), clk_get_rate(s3c->clk_div),
pwm_is_tdiv(pwm) ? "div" : "ext", pwm->tcon_base); pwm_is_tdiv(s3c) ? "div" : "ext", s3c->tcon_base);
platform_set_drvdata(pdev, pwm); platform_set_drvdata(pdev, s3c);
return 0; return 0;
err_clk_tdiv: err_clk_tdiv:
clk_disable(pwm->clk_div); clk_disable(s3c->clk_div);
clk_disable(pwm->clk); clk_disable(s3c->clk);
clk_put(pwm->clk_div); clk_put(s3c->clk_div);
err_clk_tin: err_clk_tin:
clk_put(pwm->clk); clk_put(s3c->clk);
err_alloc: err_alloc:
kfree(pwm); kfree(s3c);
return ret; return ret;
} }
static int __devexit s3c_pwm_remove(struct platform_device *pdev) static int __devexit s3c_pwm_remove(struct platform_device *pdev)
{ {
struct pwm_device *pwm = platform_get_drvdata(pdev); struct s3c_chip *s3c = platform_get_drvdata(pdev);
int err;
err = pwmchip_remove(&s3c->chip);
if (err < 0)
return err;
clk_disable(pwm->clk_div); clk_disable(s3c->clk_div);
clk_disable(pwm->clk); clk_disable(s3c->clk);
clk_put(pwm->clk_div); clk_put(s3c->clk_div);
clk_put(pwm->clk); clk_put(s3c->clk);
kfree(pwm); kfree(s3c);
return 0; return 0;
} }
...@@ -357,26 +303,26 @@ static int __devexit s3c_pwm_remove(struct platform_device *pdev) ...@@ -357,26 +303,26 @@ static int __devexit s3c_pwm_remove(struct platform_device *pdev)
#ifdef CONFIG_PM #ifdef CONFIG_PM
static int s3c_pwm_suspend(struct platform_device *pdev, pm_message_t state) static int s3c_pwm_suspend(struct platform_device *pdev, pm_message_t state)
{ {
struct pwm_device *pwm = platform_get_drvdata(pdev); struct s3c_chip *s3c = platform_get_drvdata(pdev);
/* No one preserve these values during suspend so reset them /* No one preserve these values during suspend so reset them
* Otherwise driver leaves PWM unconfigured if same values * Otherwise driver leaves PWM unconfigured if same values
* passed to pwm_config * passed to pwm_config
*/ */
pwm->period_ns = 0; s3c->period_ns = 0;
pwm->duty_ns = 0; s3c->duty_ns = 0;
return 0; return 0;
} }
static int s3c_pwm_resume(struct platform_device *pdev) static int s3c_pwm_resume(struct platform_device *pdev)
{ {
struct pwm_device *pwm = platform_get_drvdata(pdev); struct s3c_chip *s3c = platform_get_drvdata(pdev);
unsigned long tcon; unsigned long tcon;
/* Restore invertion */ /* Restore invertion */
tcon = __raw_readl(S3C2410_TCON); tcon = __raw_readl(S3C2410_TCON);
tcon |= pwm_tcon_invert(pwm); tcon |= pwm_tcon_invert(s3c);
__raw_writel(tcon, S3C2410_TCON); __raw_writel(tcon, S3C2410_TCON);
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment