ARM vt8500: Move vt8500 pwm driver to pwm framework
authorSascha Hauer <s.hauer@pengutronix.de>
Thu, 15 Mar 2012 09:04:37 +0000 (10:04 +0100)
committerThierry Reding <thierry.reding@avionic-design.de>
Mon, 2 Jul 2012 20:06:33 +0000 (22:06 +0200)
Move the driver to drivers/pwm/ and convert it to use the framework.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Alexey Charkov <alchark@gmail.com>
Signed-off-by: Thierry Reding <thierry.reding@avionic-design.de>
arch/arm/Kconfig
arch/arm/mach-vt8500/Makefile
arch/arm/mach-vt8500/pwm.c [deleted file]
drivers/pwm/Kconfig
drivers/pwm/Makefile
drivers/pwm/pwm-vt8500.c [new file with mode: 0644]

index 84449dd8f031084f84990e0b5395bbabb0e42ce9..8da8f82f6fe346b9c96bc7e6eeb69f2d222a8134 100644 (file)
@@ -962,7 +962,6 @@ config ARCH_VT8500
        select ARCH_HAS_CPUFREQ
        select GENERIC_CLOCKEVENTS
        select ARCH_REQUIRE_GPIOLIB
-       select HAVE_PWM
        help
          Support for VIA/WonderMedia VT8500/WM85xx System-on-Chip.
 
index 81aedb7c893c24d94814f1c74e32b40cad08e543..8df9e4abab6dcc84a0323abad5a566dc9c2a2e55 100644 (file)
@@ -5,5 +5,3 @@ obj-$(CONFIG_VTWM_VERSION_WM8505) += devices-wm8505.o
 
 obj-$(CONFIG_MACH_BV07) += bv07.o
 obj-$(CONFIG_MACH_WM8505_7IN_NETBOOK) += wm8505_7in.o
-
-obj-$(CONFIG_HAVE_PWM) += pwm.o
diff --git a/arch/arm/mach-vt8500/pwm.c b/arch/arm/mach-vt8500/pwm.c
deleted file mode 100644 (file)
index 8ad825e..0000000
+++ /dev/null
@@ -1,265 +0,0 @@
-/*
- * arch/arm/mach-vt8500/pwm.c
- *
- *  Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
- *
- * This software is licensed under the terms of the GNU General Public
- * License version 2, as published by the Free Software Foundation, and
- * may be copied, distributed, and modified under those terms.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
-
-#include <linux/module.h>
-#include <linux/kernel.h>
-#include <linux/platform_device.h>
-#include <linux/slab.h>
-#include <linux/err.h>
-#include <linux/io.h>
-#include <linux/pwm.h>
-#include <linux/delay.h>
-
-#include <asm/div64.h>
-
-#define VT8500_NR_PWMS 4
-
-static DEFINE_MUTEX(pwm_lock);
-static LIST_HEAD(pwm_list);
-
-struct pwm_device {
-       struct list_head        node;
-       struct platform_device  *pdev;
-
-       const char      *label;
-
-       void __iomem    *regbase;
-
-       unsigned int    use_count;
-       unsigned int    pwm_id;
-};
-
-#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
-static inline void pwm_busy_wait(void __iomem *reg, u8 bitmask)
-{
-       int loops = msecs_to_loops(10);
-       while ((readb(reg) & bitmask) && --loops)
-               cpu_relax();
-
-       if (unlikely(!loops))
-               pr_warning("Waiting for status bits 0x%x to clear timed out\n",
-                          bitmask);
-}
-
-int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
-{
-       unsigned long long c;
-       unsigned long period_cycles, prescale, pv, dc;
-
-       if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
-               return -EINVAL;
-
-       c = 25000000/2; /* wild guess --- need to implement clocks */
-       c = c * period_ns;
-       do_div(c, 1000000000);
-       period_cycles = c;
-
-       if (period_cycles < 1)
-               period_cycles = 1;
-       prescale = (period_cycles - 1) / 4096;
-       pv = period_cycles / (prescale + 1) - 1;
-       if (pv > 4095)
-               pv = 4095;
-
-       if (prescale > 1023)
-               return -EINVAL;
-
-       c = (unsigned long long)pv * duty_ns;
-       do_div(c, period_ns);
-       dc = c;
-
-       pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 1));
-       writel(prescale, pwm->regbase + 0x4 + (pwm->pwm_id << 4));
-
-       pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 2));
-       writel(pv, pwm->regbase + 0x8 + (pwm->pwm_id << 4));
-
-       pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 3));
-       writel(dc, pwm->regbase + 0xc + (pwm->pwm_id << 4));
-
-       return 0;
-}
-EXPORT_SYMBOL(pwm_config);
-
-int pwm_enable(struct pwm_device *pwm)
-{
-       pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 0));
-       writel(5, pwm->regbase + (pwm->pwm_id << 4));
-       return 0;
-}
-EXPORT_SYMBOL(pwm_enable);
-
-void pwm_disable(struct pwm_device *pwm)
-{
-       pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 0));
-       writel(0, pwm->regbase + (pwm->pwm_id << 4));
-}
-EXPORT_SYMBOL(pwm_disable);
-
-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, node) {
-               if (pwm->pwm_id == pwm_id) {
-                       found = 1;
-                       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) {
-               pwm->use_count--;
-               pwm->label = NULL;
-       } else {
-               pr_warning("PWM device already freed\n");
-       }
-
-       mutex_unlock(&pwm_lock);
-}
-EXPORT_SYMBOL(pwm_free);
-
-static inline void __add_pwm(struct pwm_device *pwm)
-{
-       mutex_lock(&pwm_lock);
-       list_add_tail(&pwm->node, &pwm_list);
-       mutex_unlock(&pwm_lock);
-}
-
-static int __devinit pwm_probe(struct platform_device *pdev)
-{
-       struct pwm_device *pwms;
-       struct resource *r;
-       int ret = 0;
-       int i;
-
-       pwms = kzalloc(sizeof(struct pwm_device) * VT8500_NR_PWMS, GFP_KERNEL);
-       if (pwms == NULL) {
-               dev_err(&pdev->dev, "failed to allocate memory\n");
-               return -ENOMEM;
-       }
-
-       for (i = 0; i < VT8500_NR_PWMS; i++) {
-               pwms[i].use_count = 0;
-               pwms[i].pwm_id = i;
-               pwms[i].pdev = pdev;
-       }
-
-       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-       if (r == NULL) {
-               dev_err(&pdev->dev, "no memory resource defined\n");
-               ret = -ENODEV;
-               goto err_free;
-       }
-
-       r = request_mem_region(r->start, resource_size(r), pdev->name);
-       if (r == NULL) {
-               dev_err(&pdev->dev, "failed to request memory resource\n");
-               ret = -EBUSY;
-               goto err_free;
-       }
-
-       pwms[0].regbase = ioremap(r->start, resource_size(r));
-       if (pwms[0].regbase == NULL) {
-               dev_err(&pdev->dev, "failed to ioremap() registers\n");
-               ret = -ENODEV;
-               goto err_free_mem;
-       }
-
-       for (i = 1; i < VT8500_NR_PWMS; i++)
-               pwms[i].regbase = pwms[0].regbase;
-
-       for (i = 0; i < VT8500_NR_PWMS; i++)
-               __add_pwm(&pwms[i]);
-
-       platform_set_drvdata(pdev, pwms);
-       return 0;
-
-err_free_mem:
-       release_mem_region(r->start, resource_size(r));
-err_free:
-       kfree(pwms);
-       return ret;
-}
-
-static int __devexit pwm_remove(struct platform_device *pdev)
-{
-       struct pwm_device *pwms;
-       struct resource *r;
-       int i;
-
-       pwms = platform_get_drvdata(pdev);
-       if (pwms == NULL)
-               return -ENODEV;
-
-       mutex_lock(&pwm_lock);
-
-       for (i = 0; i < VT8500_NR_PWMS; i++)
-               list_del(&pwms[i].node);
-       mutex_unlock(&pwm_lock);
-
-       iounmap(pwms[0].regbase);
-
-       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-       release_mem_region(r->start, resource_size(r));
-
-       kfree(pwms);
-       return 0;
-}
-
-static struct platform_driver pwm_driver = {
-       .driver         = {
-               .name   = "vt8500-pwm",
-               .owner  = THIS_MODULE,
-       },
-       .probe          = pwm_probe,
-       .remove         = __devexit_p(pwm_remove),
-};
-
-static int __init pwm_init(void)
-{
-       return platform_driver_register(&pwm_driver);
-}
-arch_initcall(pwm_init);
-
-static void __exit pwm_exit(void)
-{
-       platform_driver_unregister(&pwm_driver);
-}
-module_exit(pwm_exit);
-
-MODULE_LICENSE("GPL");
index 0d9aa92636c3cc0a1f754e2315f2819c46e1254b..a93feffbc36a8c63059c91cc4d5f0bb3965023c7 100644 (file)
@@ -55,4 +55,13 @@ config PWM_TEGRA
          To compile this driver as a module, choose M here: the module
          will be called pwm-tegra.
 
+config PWM_VT8500
+       tristate "vt8500 pwm support"
+       depends on ARCH_VT8500
+       help
+         Generic PWM framework driver for vt8500.
+
+         To compile this driver as a module, choose M here: the module
+         will be called pwm-vt8500.
+
 endif
index a1d87b1733125ea8793ad12886c718ddb1b57743..b7c0fcf4de164305e7a3190eee6f76280b98e9ea 100644 (file)
@@ -4,3 +4,4 @@ obj-$(CONFIG_PWM_IMX)           += pwm-imx.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_VT8500)       += pwm-vt8500.o
diff --git a/drivers/pwm/pwm-vt8500.c b/drivers/pwm/pwm-vt8500.c
new file mode 100644 (file)
index 0000000..3db0746
--- /dev/null
@@ -0,0 +1,208 @@
+/*
+ * arch/arm/mach-vt8500/pwm.c
+ *
+ *  Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/pwm.h>
+#include <linux/delay.h>
+
+#include <asm/div64.h>
+
+#define VT8500_NR_PWMS 4
+
+struct vt8500_chip {
+       struct pwm_chip chip;
+       void __iomem *base;
+};
+
+#define to_vt8500_chip(chip)   container_of(chip, struct vt8500_chip, chip)
+
+#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
+static inline void pwm_busy_wait(void __iomem *reg, u8 bitmask)
+{
+       int loops = msecs_to_loops(10);
+       while ((readb(reg) & bitmask) && --loops)
+               cpu_relax();
+
+       if (unlikely(!loops))
+               pr_warning("Waiting for status bits 0x%x to clear timed out\n",
+                          bitmask);
+}
+
+static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
+               int duty_ns, int period_ns)
+{
+       struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
+       unsigned long long c;
+       unsigned long period_cycles, prescale, pv, dc;
+
+       c = 25000000/2; /* wild guess --- need to implement clocks */
+       c = c * period_ns;
+       do_div(c, 1000000000);
+       period_cycles = c;
+
+       if (period_cycles < 1)
+               period_cycles = 1;
+       prescale = (period_cycles - 1) / 4096;
+       pv = period_cycles / (prescale + 1) - 1;
+       if (pv > 4095)
+               pv = 4095;
+
+       if (prescale > 1023)
+               return -EINVAL;
+
+       c = (unsigned long long)pv * duty_ns;
+       do_div(c, period_ns);
+       dc = c;
+
+       pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 1));
+       writel(prescale, vt8500->base + 0x4 + (pwm->hwpwm << 4));
+
+       pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 2));
+       writel(pv, vt8500->base + 0x8 + (pwm->hwpwm << 4));
+
+       pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 3));
+       writel(dc, vt8500->base + 0xc + (pwm->hwpwm << 4));
+
+       return 0;
+}
+
+static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+       struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
+
+       pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
+       writel(5, vt8500->base + (pwm->hwpwm << 4));
+       return 0;
+}
+
+static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+       struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
+
+       pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
+       writel(0, vt8500->base + (pwm->hwpwm << 4));
+}
+
+static struct pwm_ops vt8500_pwm_ops = {
+       .enable = vt8500_pwm_enable,
+       .disable = vt8500_pwm_disable,
+       .config = vt8500_pwm_config,
+       .owner = THIS_MODULE,
+};
+
+static int __devinit pwm_probe(struct platform_device *pdev)
+{
+       struct vt8500_chip *chip;
+       struct resource *r;
+       int ret;
+
+       chip = kzalloc(sizeof(*chip), GFP_KERNEL);
+       if (chip == NULL) {
+               dev_err(&pdev->dev, "failed to allocate memory\n");
+               return -ENOMEM;
+       }
+
+       chip->chip.dev = &pdev->dev;
+       chip->chip.ops = &vt8500_pwm_ops;
+       chip->chip.base = -1;
+       chip->chip.npwm = VT8500_NR_PWMS;
+
+       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       if (r == NULL) {
+               dev_err(&pdev->dev, "no memory resource defined\n");
+               ret = -ENODEV;
+               goto err_free;
+       }
+
+       r = request_mem_region(r->start, resource_size(r), pdev->name);
+       if (r == NULL) {
+               dev_err(&pdev->dev, "failed to request memory resource\n");
+               ret = -EBUSY;
+               goto err_free;
+       }
+
+       chip->base = ioremap(r->start, resource_size(r));
+       if (chip->base == NULL) {
+               dev_err(&pdev->dev, "failed to ioremap() registers\n");
+               ret = -ENODEV;
+               goto err_free_mem;
+       }
+
+       ret = pwmchip_add(&chip->chip);
+       if (ret < 0)
+               goto err_unmap;
+
+       platform_set_drvdata(pdev, chip);
+       return ret;
+
+err_unmap:
+       iounmap(chip->base);
+err_free_mem:
+       release_mem_region(r->start, resource_size(r));
+err_free:
+       kfree(chip);
+       return ret;
+}
+
+static int __devexit pwm_remove(struct platform_device *pdev)
+{
+       struct vt8500_chip *chip;
+       struct resource *r;
+       int err;
+
+       chip = platform_get_drvdata(pdev);
+       if (chip == NULL)
+               return -ENODEV;
+
+       err = pwmchip_remove(&chip->chip);
+       if (err < 0)
+               return err;
+
+       iounmap(chip->base);
+
+       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       release_mem_region(r->start, resource_size(r));
+
+       kfree(chip);
+       return 0;
+}
+
+static struct platform_driver pwm_driver = {
+       .driver         = {
+               .name   = "vt8500-pwm",
+               .owner  = THIS_MODULE,
+       },
+       .probe          = pwm_probe,
+       .remove         = __devexit_p(pwm_remove),
+};
+
+static int __init pwm_init(void)
+{
+       return platform_driver_register(&pwm_driver);
+}
+arch_initcall(pwm_init);
+
+static void __exit pwm_exit(void)
+{
+       platform_driver_unregister(&pwm_driver);
+}
+module_exit(pwm_exit);
+
+MODULE_LICENSE("GPL");