Commit 29693248 authored by Sascha Hauer's avatar Sascha Hauer Committed by Thierry Reding

ARM i.MX: Move i.MX 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>
[eric@eukrea.com: set chip.dev to prevent probe failure]
[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 17b2b478
...@@ -47,12 +47,6 @@ config MXC_TZIC ...@@ -47,12 +47,6 @@ config MXC_TZIC
config MXC_AVIC config MXC_AVIC
bool bool
config MXC_PWM
tristate "Enable PWM driver"
select HAVE_PWM
help
Enable support for the i.MX PWM controller(s).
config MXC_DEBUG_BOARD config MXC_DEBUG_BOARD
bool "Enable MXC debug board(for 3-stack)" bool "Enable MXC debug board(for 3-stack)"
help help
......
...@@ -11,7 +11,6 @@ obj-$(CONFIG_MXC_AVIC) += avic.o ...@@ -11,7 +11,6 @@ obj-$(CONFIG_MXC_AVIC) += avic.o
obj-$(CONFIG_IMX_HAVE_IOMUX_V1) += iomux-v1.o obj-$(CONFIG_IMX_HAVE_IOMUX_V1) += iomux-v1.o
obj-$(CONFIG_ARCH_MXC_IOMUX_V3) += iomux-v3.o obj-$(CONFIG_ARCH_MXC_IOMUX_V3) += iomux-v3.o
obj-$(CONFIG_IRAM_ALLOC) += iram_alloc.o obj-$(CONFIG_IRAM_ALLOC) += iram_alloc.o
obj-$(CONFIG_MXC_PWM) += pwm.o
obj-$(CONFIG_MXC_ULPI) += ulpi.o obj-$(CONFIG_MXC_ULPI) += ulpi.o
obj-$(CONFIG_MXC_USE_EPIT) += epit.o obj-$(CONFIG_MXC_USE_EPIT) += epit.o
obj-$(CONFIG_MXC_DEBUG_BOARD) += 3ds_debugboard.o obj-$(CONFIG_MXC_DEBUG_BOARD) += 3ds_debugboard.o
......
...@@ -18,6 +18,15 @@ config PWM_BFIN ...@@ -18,6 +18,15 @@ config PWM_BFIN
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-bfin. will be called pwm-bfin.
config PWM_IMX
tristate "i.MX pwm support"
depends on ARCH_MXC
help
Generic PWM framework driver for i.MX.
To compile this driver as a module, choose M here: the module
will be called pwm-imx.
config PWM_PXA config PWM_PXA
tristate "PXA PWM support" tristate "PXA PWM support"
depends on ARCH_PXA depends on ARCH_PXA
......
obj-$(CONFIG_PWM) += core.o 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_PXA) += pwm-pxa.o obj-$(CONFIG_PWM_PXA) += pwm-pxa.o
obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
...@@ -39,33 +39,28 @@ ...@@ -39,33 +39,28 @@
#define MX3_PWMCR_CLKSRC_IPG (1 << 16) #define MX3_PWMCR_CLKSRC_IPG (1 << 16)
#define MX3_PWMCR_EN (1 << 0) #define MX3_PWMCR_EN (1 << 0)
struct imx_chip {
struct pwm_device {
struct list_head node;
struct platform_device *pdev;
const char *label;
struct clk *clk; struct clk *clk;
int clk_enabled; int clk_enabled;
void __iomem *mmio_base; void __iomem *mmio_base;
unsigned int use_count; struct pwm_chip chip;
unsigned int pwm_id;
}; };
int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) #define to_imx_chip(chip) container_of(chip, struct imx_chip, chip)
static int imx_pwm_config(struct pwm_chip *chip,
struct pwm_device *pwm, int duty_ns, int period_ns)
{ {
if (pwm == NULL || period_ns == 0 || duty_ns > period_ns) struct imx_chip *imx = to_imx_chip(chip);
return -EINVAL;
if (!(cpu_is_mx1() || cpu_is_mx21())) { if (!(cpu_is_mx1() || cpu_is_mx21())) {
unsigned long long c; unsigned long long c;
unsigned long period_cycles, duty_cycles, prescale; unsigned long period_cycles, duty_cycles, prescale;
u32 cr; u32 cr;
c = clk_get_rate(pwm->clk); c = clk_get_rate(imx->clk);
c = c * period_ns; c = c * period_ns;
do_div(c, 1000000000); do_div(c, 1000000000);
period_cycles = c; period_cycles = c;
...@@ -86,8 +81,8 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) ...@@ -86,8 +81,8 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
else else
period_cycles = 0; period_cycles = 0;
writel(duty_cycles, pwm->mmio_base + MX3_PWMSAR); writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
writel(period_cycles, pwm->mmio_base + MX3_PWMPR); writel(period_cycles, imx->mmio_base + MX3_PWMPR);
cr = MX3_PWMCR_PRESCALER(prescale) | cr = MX3_PWMCR_PRESCALER(prescale) |
MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN | MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
...@@ -98,7 +93,7 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) ...@@ -98,7 +93,7 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
else else
cr |= MX3_PWMCR_CLKSRC_IPG_HIGH; cr |= MX3_PWMCR_CLKSRC_IPG_HIGH;
writel(cr, pwm->mmio_base + MX3_PWMCR); writel(cr, imx->mmio_base + MX3_PWMCR);
} else if (cpu_is_mx1() || cpu_is_mx21()) { } else if (cpu_is_mx1() || cpu_is_mx21()) {
/* The PWM subsystem allows for exact frequencies. However, /* The PWM subsystem allows for exact frequencies. However,
* I cannot connect a scope on my device to the PWM line and * I cannot connect a scope on my device to the PWM line and
...@@ -116,110 +111,73 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) ...@@ -116,110 +111,73 @@ int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
* both the prescaler (/1 .. /128) and then by CLKSEL * both the prescaler (/1 .. /128) and then by CLKSEL
* (/2 .. /16). * (/2 .. /16).
*/ */
u32 max = readl(pwm->mmio_base + MX1_PWMP); u32 max = readl(imx->mmio_base + MX1_PWMP);
u32 p = max * duty_ns / period_ns; u32 p = max * duty_ns / period_ns;
writel(max - p, pwm->mmio_base + MX1_PWMS); writel(max - p, imx->mmio_base + MX1_PWMS);
} else { } else {
BUG(); BUG();
} }
return 0; return 0;
} }
EXPORT_SYMBOL(pwm_config);
int pwm_enable(struct pwm_device *pwm) static int imx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
{ {
struct imx_chip *imx = to_imx_chip(chip);
int rc = 0; int rc = 0;
if (!pwm->clk_enabled) { if (!imx->clk_enabled) {
rc = clk_prepare_enable(pwm->clk); rc = clk_prepare_enable(imx->clk);
if (!rc) if (!rc)
pwm->clk_enabled = 1; imx->clk_enabled = 1;
} }
return rc; return rc;
} }
EXPORT_SYMBOL(pwm_enable);
void pwm_disable(struct pwm_device *pwm)
{
writel(0, pwm->mmio_base + MX3_PWMCR);
if (pwm->clk_enabled) {
clk_disable_unprepare(pwm->clk);
pwm->clk_enabled = 0;
}
}
EXPORT_SYMBOL(pwm_disable);
static DEFINE_MUTEX(pwm_lock); static void imx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
static LIST_HEAD(pwm_list);
struct pwm_device *pwm_request(int pwm_id, const char *label)
{ {
struct pwm_device *pwm; struct imx_chip *imx = to_imx_chip(chip);
int found = 0;
mutex_lock(&pwm_lock); writel(0, imx->mmio_base + MX3_PWMCR);
list_for_each_entry(pwm, &pwm_list, node) { if (imx->clk_enabled) {
if (pwm->pwm_id == pwm_id) { clk_disable_unprepare(imx->clk);
found = 1; imx->clk_enabled = 0;
break;
}
} }
if (found) {
if (pwm->use_count == 0) {
pwm->use_count++;
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);
if (pwm->use_count) { static struct pwm_ops imx_pwm_ops = {
pwm->use_count--; .enable = imx_pwm_enable,
pwm->label = NULL; .disable = imx_pwm_disable,
} else .config = imx_pwm_config,
pr_warning("PWM device already freed\n"); .owner = THIS_MODULE,
};
mutex_unlock(&pwm_lock);
}
EXPORT_SYMBOL(pwm_free);
static int __devinit mxc_pwm_probe(struct platform_device *pdev) static int __devinit imx_pwm_probe(struct platform_device *pdev)
{ {
struct pwm_device *pwm; struct imx_chip *imx;
struct resource *r; struct resource *r;
int ret = 0; int ret = 0;
pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL); imx = kzalloc(sizeof(*imx), GFP_KERNEL);
if (pwm == NULL) { if (imx == NULL) {
dev_err(&pdev->dev, "failed to allocate memory\n"); dev_err(&pdev->dev, "failed to allocate memory\n");
return -ENOMEM; return -ENOMEM;
} }
pwm->clk = clk_get(&pdev->dev, "pwm"); imx->clk = clk_get(&pdev->dev, "pwm");
if (IS_ERR(pwm->clk)) { if (IS_ERR(imx->clk)) {
ret = PTR_ERR(pwm->clk); ret = PTR_ERR(imx->clk);
goto err_free; goto err_free;
} }
pwm->clk_enabled = 0; imx->chip.ops = &imx_pwm_ops;
imx->chip.dev = &pdev->dev;
imx->chip.base = -1;
imx->chip.npwm = 1;
pwm->use_count = 0; imx->clk_enabled = 0;
pwm->pwm_id = pdev->id;
pwm->pdev = pdev;
r = platform_get_resource(pdev, IORESOURCE_MEM, 0); r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (r == NULL) { if (r == NULL) {
...@@ -235,72 +193,75 @@ static int __devinit mxc_pwm_probe(struct platform_device *pdev) ...@@ -235,72 +193,75 @@ static int __devinit mxc_pwm_probe(struct platform_device *pdev)
goto err_free_clk; goto err_free_clk;
} }
pwm->mmio_base = ioremap(r->start, resource_size(r)); imx->mmio_base = ioremap(r->start, resource_size(r));
if (pwm->mmio_base == NULL) { if (imx->mmio_base == NULL) {
dev_err(&pdev->dev, "failed to ioremap() registers\n"); dev_err(&pdev->dev, "failed to ioremap() registers\n");
ret = -ENODEV; ret = -ENODEV;
goto err_free_mem; goto err_free_mem;
} }
mutex_lock(&pwm_lock); ret = pwmchip_add(&imx->chip);
list_add_tail(&pwm->node, &pwm_list); if (ret < 0)
mutex_unlock(&pwm_lock); goto err_iounmap;
platform_set_drvdata(pdev, pwm); platform_set_drvdata(pdev, imx);
return 0; return 0;
err_iounmap:
iounmap(imx->mmio_base);
err_free_mem: err_free_mem:
release_mem_region(r->start, resource_size(r)); release_mem_region(r->start, resource_size(r));
err_free_clk: err_free_clk:
clk_put(pwm->clk); clk_put(imx->clk);
err_free: err_free:
kfree(pwm); kfree(imx);
return ret; return ret;
} }
static int __devexit mxc_pwm_remove(struct platform_device *pdev) static int __devexit imx_pwm_remove(struct platform_device *pdev)
{ {
struct pwm_device *pwm; struct imx_chip *imx;
struct resource *r; struct resource *r;
int ret;
pwm = platform_get_drvdata(pdev); imx = platform_get_drvdata(pdev);
if (pwm == NULL) if (imx == NULL)
return -ENODEV; return -ENODEV;
mutex_lock(&pwm_lock); ret = pwmchip_remove(&imx->chip);
list_del(&pwm->node); if (ret)
mutex_unlock(&pwm_lock); return ret;
iounmap(pwm->mmio_base); iounmap(imx->mmio_base);
r = platform_get_resource(pdev, IORESOURCE_MEM, 0); r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
release_mem_region(r->start, resource_size(r)); release_mem_region(r->start, resource_size(r));
clk_put(pwm->clk); clk_put(imx->clk);
kfree(pwm); kfree(imx);
return 0; return 0;
} }
static struct platform_driver mxc_pwm_driver = { static struct platform_driver imx_pwm_driver = {
.driver = { .driver = {
.name = "mxc_pwm", .name = "mxc_pwm",
}, },
.probe = mxc_pwm_probe, .probe = imx_pwm_probe,
.remove = __devexit_p(mxc_pwm_remove), .remove = __devexit_p(imx_pwm_remove),
}; };
static int __init mxc_pwm_init(void) static int __init imx_pwm_init(void)
{ {
return platform_driver_register(&mxc_pwm_driver); return platform_driver_register(&imx_pwm_driver);
} }
arch_initcall(mxc_pwm_init); arch_initcall(imx_pwm_init);
static void __exit mxc_pwm_exit(void) static void __exit imx_pwm_exit(void)
{ {
platform_driver_unregister(&mxc_pwm_driver); platform_driver_unregister(&imx_pwm_driver);
} }
module_exit(mxc_pwm_exit); module_exit(imx_pwm_exit);
MODULE_LICENSE("GPL v2"); MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
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