2009-07-29 23:41:06 +02:00
|
|
|
/*
|
|
|
|
* Simple PWM driver for EP93XX
|
|
|
|
*
|
|
|
|
* (c) Copyright 2009 Matthieu Crapet <mcrapet@gmail.com>
|
|
|
|
* (c) Copyright 2009 H Hartley Sweeten <hsweeten@visionengravers.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* EP9307 has only one channel:
|
|
|
|
* - PWMOUT
|
|
|
|
*
|
|
|
|
* EP9301/02/12/15 have two channels:
|
|
|
|
* - PWMOUT
|
|
|
|
* - PWMOUT1 (alternate function for EGPIO14)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 09:04:11 +01:00
|
|
|
#include <linux/slab.h>
|
2009-07-29 23:41:06 +02:00
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
|
|
|
|
#include <mach/platform.h>
|
|
|
|
|
|
|
|
#define EP93XX_PWMx_TERM_COUNT 0x00
|
|
|
|
#define EP93XX_PWMx_DUTY_CYCLE 0x04
|
|
|
|
#define EP93XX_PWMx_ENABLE 0x08
|
|
|
|
#define EP93XX_PWMx_INVERT 0x0C
|
|
|
|
|
|
|
|
#define EP93XX_PWM_MAX_COUNT 0xFFFF
|
|
|
|
|
|
|
|
struct ep93xx_pwm {
|
|
|
|
void __iomem *mmio_base;
|
|
|
|
struct clk *clk;
|
|
|
|
u32 duty_percent;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline u16 ep93xx_pwm_read_tc(struct ep93xx_pwm *pwm)
|
|
|
|
{
|
2013-05-25 01:22:30 +02:00
|
|
|
return readl(pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
|
2009-07-29 23:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ep93xx_pwm_is_enabled(struct ep93xx_pwm *pwm)
|
|
|
|
{
|
2013-05-25 01:22:30 +02:00
|
|
|
return readl(pwm->mmio_base + EP93XX_PWMx_ENABLE) & 0x1;
|
2009-07-29 23:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void ep93xx_pwm_normal(struct ep93xx_pwm *pwm)
|
|
|
|
{
|
2013-05-25 01:22:30 +02:00
|
|
|
writel(0x0, pwm->mmio_base + EP93XX_PWMx_INVERT);
|
2009-07-29 23:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ep93xx_pwm_is_inverted(struct ep93xx_pwm *pwm)
|
|
|
|
{
|
2013-05-25 01:22:30 +02:00
|
|
|
return readl(pwm->mmio_base + EP93XX_PWMx_INVERT) & 0x1;
|
2009-07-29 23:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* /sys/devices/platform/ep93xx-pwm.N
|
|
|
|
* /min_freq read-only minimum pwm output frequency
|
|
|
|
* /max_req read-only maximum pwm output frequency
|
|
|
|
* /freq read-write pwm output frequency (0 = disable output)
|
|
|
|
* /duty_percent read-write pwm duty cycle percent (1..99)
|
|
|
|
* /invert read-write invert pwm output
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ssize_t ep93xx_pwm_get_min_freq(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
|
|
|
|
unsigned long rate = clk_get_rate(pwm->clk);
|
|
|
|
|
|
|
|
return sprintf(buf, "%ld\n", rate / (EP93XX_PWM_MAX_COUNT + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ep93xx_pwm_get_max_freq(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
|
|
|
|
unsigned long rate = clk_get_rate(pwm->clk);
|
|
|
|
|
|
|
|
return sprintf(buf, "%ld\n", rate / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ep93xx_pwm_get_freq(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
if (ep93xx_pwm_is_enabled(pwm)) {
|
|
|
|
unsigned long rate = clk_get_rate(pwm->clk);
|
|
|
|
u16 term = ep93xx_pwm_read_tc(pwm);
|
|
|
|
|
|
|
|
return sprintf(buf, "%ld\n", rate / (term + 1));
|
|
|
|
} else {
|
|
|
|
return sprintf(buf, "disabled\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ep93xx_pwm_set_freq(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
|
|
|
|
long val;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = strict_strtol(buf, 10, &val);
|
|
|
|
if (err)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (val == 0) {
|
2013-05-25 01:24:51 +02:00
|
|
|
writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
|
2009-07-29 23:41:06 +02:00
|
|
|
} else if (val <= (clk_get_rate(pwm->clk) / 2)) {
|
|
|
|
u32 term, duty;
|
|
|
|
|
|
|
|
val = (clk_get_rate(pwm->clk) / val) - 1;
|
|
|
|
if (val > EP93XX_PWM_MAX_COUNT)
|
|
|
|
val = EP93XX_PWM_MAX_COUNT;
|
|
|
|
if (val < 1)
|
|
|
|
val = 1;
|
|
|
|
|
|
|
|
term = ep93xx_pwm_read_tc(pwm);
|
|
|
|
duty = ((val + 1) * pwm->duty_percent / 100) - 1;
|
|
|
|
|
|
|
|
/* If pwm is running, order is important */
|
|
|
|
if (val > term) {
|
2013-05-25 01:23:00 +02:00
|
|
|
writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
|
2013-05-25 01:23:38 +02:00
|
|
|
writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
|
2009-07-29 23:41:06 +02:00
|
|
|
} else {
|
2013-05-25 01:23:38 +02:00
|
|
|
writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
|
2013-05-25 01:23:00 +02:00
|
|
|
writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
|
2009-07-29 23:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!ep93xx_pwm_is_enabled(pwm))
|
2013-05-25 01:24:16 +02:00
|
|
|
writel(0x1, pwm->mmio_base + EP93XX_PWMx_ENABLE);
|
2009-07-29 23:41:06 +02:00
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ep93xx_pwm_get_duty_percent(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%d\n", pwm->duty_percent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ep93xx_pwm_set_duty_percent(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
|
|
|
|
long val;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = strict_strtol(buf, 10, &val);
|
|
|
|
if (err)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (val > 0 && val < 100) {
|
|
|
|
u32 term = ep93xx_pwm_read_tc(pwm);
|
2013-05-25 01:23:38 +02:00
|
|
|
u32 duty = ((term + 1) * val / 100) - 1;
|
|
|
|
|
|
|
|
writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
|
2009-07-29 23:41:06 +02:00
|
|
|
pwm->duty_percent = val;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ep93xx_pwm_get_invert(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%d\n", ep93xx_pwm_is_inverted(pwm));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ep93xx_pwm_set_invert(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
|
|
|
|
long val;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = strict_strtol(buf, 10, &val);
|
|
|
|
if (err)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (val == 0)
|
|
|
|
ep93xx_pwm_normal(pwm);
|
|
|
|
else if (val == 1)
|
2013-05-25 01:25:23 +02:00
|
|
|
writel(0x1, pwm->mmio_base + EP93XX_PWMx_INVERT);
|
2009-07-29 23:41:06 +02:00
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DEVICE_ATTR(min_freq, S_IRUGO, ep93xx_pwm_get_min_freq, NULL);
|
|
|
|
static DEVICE_ATTR(max_freq, S_IRUGO, ep93xx_pwm_get_max_freq, NULL);
|
2011-03-23 00:34:01 +01:00
|
|
|
static DEVICE_ATTR(freq, S_IWUSR | S_IRUGO,
|
2009-07-29 23:41:06 +02:00
|
|
|
ep93xx_pwm_get_freq, ep93xx_pwm_set_freq);
|
2011-03-23 00:34:01 +01:00
|
|
|
static DEVICE_ATTR(duty_percent, S_IWUSR | S_IRUGO,
|
2009-07-29 23:41:06 +02:00
|
|
|
ep93xx_pwm_get_duty_percent, ep93xx_pwm_set_duty_percent);
|
2011-03-23 00:34:01 +01:00
|
|
|
static DEVICE_ATTR(invert, S_IWUSR | S_IRUGO,
|
2009-07-29 23:41:06 +02:00
|
|
|
ep93xx_pwm_get_invert, ep93xx_pwm_set_invert);
|
|
|
|
|
|
|
|
static struct attribute *ep93xx_pwm_attrs[] = {
|
|
|
|
&dev_attr_min_freq.attr,
|
|
|
|
&dev_attr_max_freq.attr,
|
|
|
|
&dev_attr_freq.attr,
|
|
|
|
&dev_attr_duty_percent.attr,
|
|
|
|
&dev_attr_invert.attr,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct attribute_group ep93xx_pwm_sysfs_files = {
|
|
|
|
.attrs = ep93xx_pwm_attrs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init ep93xx_pwm_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct ep93xx_pwm *pwm;
|
|
|
|
struct resource *res;
|
2013-05-25 01:21:01 +02:00
|
|
|
int ret;
|
2009-07-29 23:41:06 +02:00
|
|
|
|
2013-05-25 01:21:01 +02:00
|
|
|
pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
|
|
|
|
if (!pwm)
|
|
|
|
return -ENOMEM;
|
2009-07-29 23:41:06 +02:00
|
|
|
|
2013-05-25 01:21:01 +02:00
|
|
|
pwm->clk = devm_clk_get(&pdev->dev, "pwm_clk");
|
|
|
|
if (IS_ERR(pwm->clk))
|
|
|
|
return PTR_ERR(pwm->clk);
|
2009-07-29 23:41:06 +02:00
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2013-05-25 01:21:01 +02:00
|
|
|
pwm->mmio_base = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(pwm->mmio_base))
|
|
|
|
return PTR_ERR(pwm->mmio_base);
|
|
|
|
|
|
|
|
ret = ep93xx_pwm_acquire_gpio(pdev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = sysfs_create_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
|
|
|
|
if (ret) {
|
|
|
|
ep93xx_pwm_release_gpio(pdev);
|
|
|
|
return ret;
|
2009-07-29 23:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pwm->duty_percent = 50;
|
|
|
|
|
|
|
|
/* disable pwm at startup. Avoids zero value. */
|
2013-05-25 01:24:51 +02:00
|
|
|
writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
|
2013-05-25 01:23:00 +02:00
|
|
|
writel(EP93XX_PWM_MAX_COUNT, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
|
2013-05-25 01:23:38 +02:00
|
|
|
writel(EP93XX_PWM_MAX_COUNT/2, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
|
2009-07-29 23:41:06 +02:00
|
|
|
|
|
|
|
clk_enable(pwm->clk);
|
|
|
|
|
2013-05-25 01:21:01 +02:00
|
|
|
platform_set_drvdata(pdev, pwm);
|
2009-07-29 23:41:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __exit ep93xx_pwm_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
|
|
|
|
|
2013-05-25 01:24:51 +02:00
|
|
|
writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
|
2009-07-29 23:41:06 +02:00
|
|
|
clk_disable(pwm->clk);
|
|
|
|
sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
|
|
|
|
ep93xx_pwm_release_gpio(pdev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver ep93xx_pwm_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "ep93xx-pwm",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
.remove = __exit_p(ep93xx_pwm_remove),
|
|
|
|
};
|
|
|
|
|
2013-03-05 03:04:07 +01:00
|
|
|
module_platform_driver_probe(ep93xx_pwm_driver, ep93xx_pwm_probe);
|
2009-07-29 23:41:06 +02:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>, "
|
|
|
|
"H Hartley Sweeten <hsweeten@visionengravers.com>");
|
|
|
|
MODULE_DESCRIPTION("EP93xx PWM driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS("platform:ep93xx-pwm");
|