2009-01-16 22:17:16 +08:00
|
|
|
/*
|
|
|
|
* simple driver for PWM (Pulse Width Modulator) controller
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.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 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2009-01-16 22:17:16 +08:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/pwm.h>
|
2009-04-14 18:50:20 +08:00
|
|
|
#include <mach/hardware.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* i.MX1 and i.MX21 share the same PWM function block: */
|
|
|
|
|
|
|
|
#define MX1_PWMC 0x00 /* PWM Control Register */
|
|
|
|
#define MX1_PWMS 0x04 /* PWM Sample Register */
|
|
|
|
#define MX1_PWMP 0x08 /* PWM Period Register */
|
|
|
|
|
|
|
|
|
|
|
|
/* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
|
|
|
|
|
|
|
|
#define MX3_PWMCR 0x00 /* PWM Control Register */
|
|
|
|
#define MX3_PWMSAR 0x0C /* PWM Sample Register */
|
|
|
|
#define MX3_PWMPR 0x10 /* PWM Period Register */
|
|
|
|
#define MX3_PWMCR_PRESCALER(x) (((x - 1) & 0xFFF) << 4)
|
2011-11-30 11:34:27 +08:00
|
|
|
#define MX3_PWMCR_DOZEEN (1 << 24)
|
|
|
|
#define MX3_PWMCR_WAITEN (1 << 23)
|
|
|
|
#define MX3_PWMCR_DBGEN (1 << 22)
|
2009-04-14 18:50:20 +08:00
|
|
|
#define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16)
|
2009-06-19 17:08:07 +08:00
|
|
|
#define MX3_PWMCR_CLKSRC_IPG (1 << 16)
|
2009-04-14 18:50:20 +08:00
|
|
|
#define MX3_PWMCR_EN (1 << 0)
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
struct imx_chip {
|
2009-01-16 22:17:16 +08:00
|
|
|
struct clk *clk;
|
|
|
|
|
|
|
|
int clk_enabled;
|
|
|
|
void __iomem *mmio_base;
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
struct pwm_chip chip;
|
2009-01-16 22:17:16 +08:00
|
|
|
};
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
#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)
|
2009-01-16 22:17:16 +08:00
|
|
|
{
|
2012-03-15 17:04:35 +08:00
|
|
|
struct imx_chip *imx = to_imx_chip(chip);
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2011-08-24 00:26:38 +08:00
|
|
|
if (!(cpu_is_mx1() || cpu_is_mx21())) {
|
2009-04-14 18:50:20 +08:00
|
|
|
unsigned long long c;
|
|
|
|
unsigned long period_cycles, duty_cycles, prescale;
|
2009-06-19 17:08:07 +08:00
|
|
|
u32 cr;
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
c = clk_get_rate(imx->clk);
|
2009-04-14 18:50:20 +08:00
|
|
|
c = c * period_ns;
|
|
|
|
do_div(c, 1000000000);
|
|
|
|
period_cycles = c;
|
|
|
|
|
|
|
|
prescale = period_cycles / 0x10000 + 1;
|
|
|
|
|
|
|
|
period_cycles /= prescale;
|
|
|
|
c = (unsigned long long)period_cycles * duty_ns;
|
|
|
|
do_div(c, period_ns);
|
|
|
|
duty_cycles = c;
|
|
|
|
|
2011-12-19 11:23:28 +08:00
|
|
|
/*
|
|
|
|
* according to imx pwm RM, the real period value should be
|
|
|
|
* PERIOD value in PWMPR plus 2.
|
|
|
|
*/
|
|
|
|
if (period_cycles > 2)
|
|
|
|
period_cycles -= 2;
|
|
|
|
else
|
|
|
|
period_cycles = 0;
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
|
|
|
|
writel(period_cycles, imx->mmio_base + MX3_PWMPR);
|
2009-06-19 17:08:07 +08:00
|
|
|
|
2011-11-30 11:34:27 +08:00
|
|
|
cr = MX3_PWMCR_PRESCALER(prescale) |
|
|
|
|
MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
|
|
|
|
MX3_PWMCR_DBGEN | MX3_PWMCR_EN;
|
2009-06-19 17:08:07 +08:00
|
|
|
|
|
|
|
if (cpu_is_mx25())
|
|
|
|
cr |= MX3_PWMCR_CLKSRC_IPG;
|
|
|
|
else
|
|
|
|
cr |= MX3_PWMCR_CLKSRC_IPG_HIGH;
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
writel(cr, imx->mmio_base + MX3_PWMCR);
|
2009-04-14 18:50:20 +08:00
|
|
|
} else if (cpu_is_mx1() || cpu_is_mx21()) {
|
|
|
|
/* The PWM subsystem allows for exact frequencies. However,
|
|
|
|
* I cannot connect a scope on my device to the PWM line and
|
|
|
|
* thus cannot provide the program the PWM controller
|
|
|
|
* exactly. Instead, I'm relying on the fact that the
|
|
|
|
* Bootloader (u-boot or WinCE+haret) has programmed the PWM
|
|
|
|
* function group already. So I'll just modify the PWM sample
|
|
|
|
* register to follow the ratio of duty_ns vs. period_ns
|
|
|
|
* accordingly.
|
|
|
|
*
|
tree-wide: fix assorted typos all over the place
That is "success", "unknown", "through", "performance", "[re|un]mapping"
, "access", "default", "reasonable", "[con]currently", "temperature"
, "channel", "[un]used", "application", "example","hierarchy", "therefore"
, "[over|under]flow", "contiguous", "threshold", "enough" and others.
Signed-off-by: André Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2009-11-14 23:09:05 +08:00
|
|
|
* This is good enough for programming the brightness of
|
2009-04-14 18:50:20 +08:00
|
|
|
* the LCD backlight.
|
|
|
|
*
|
|
|
|
* The real implementation would divide PERCLK[0] first by
|
|
|
|
* both the prescaler (/1 .. /128) and then by CLKSEL
|
|
|
|
* (/2 .. /16).
|
|
|
|
*/
|
2012-03-15 17:04:35 +08:00
|
|
|
u32 max = readl(imx->mmio_base + MX1_PWMP);
|
2009-04-14 18:50:20 +08:00
|
|
|
u32 p = max * duty_ns / period_ns;
|
2012-03-15 17:04:35 +08:00
|
|
|
writel(max - p, imx->mmio_base + MX1_PWMS);
|
2009-04-14 18:50:20 +08:00
|
|
|
} else {
|
|
|
|
BUG();
|
|
|
|
}
|
2009-01-16 22:17:16 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
static int imx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
|
2009-01-16 22:17:16 +08:00
|
|
|
{
|
2012-03-15 17:04:35 +08:00
|
|
|
struct imx_chip *imx = to_imx_chip(chip);
|
2009-01-16 22:17:16 +08:00
|
|
|
int rc = 0;
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
if (!imx->clk_enabled) {
|
|
|
|
rc = clk_prepare_enable(imx->clk);
|
2009-01-16 22:17:16 +08:00
|
|
|
if (!rc)
|
2012-03-15 17:04:35 +08:00
|
|
|
imx->clk_enabled = 1;
|
2009-01-16 22:17:16 +08:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
static void imx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
|
2009-01-16 22:17:16 +08:00
|
|
|
{
|
2012-03-15 17:04:35 +08:00
|
|
|
struct imx_chip *imx = to_imx_chip(chip);
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
writel(0, imx->mmio_base + MX3_PWMCR);
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
if (imx->clk_enabled) {
|
|
|
|
clk_disable_unprepare(imx->clk);
|
|
|
|
imx->clk_enabled = 0;
|
2009-01-16 22:17:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
static struct pwm_ops imx_pwm_ops = {
|
|
|
|
.enable = imx_pwm_enable,
|
|
|
|
.disable = imx_pwm_disable,
|
|
|
|
.config = imx_pwm_config,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
static int __devinit imx_pwm_probe(struct platform_device *pdev)
|
2009-01-16 22:17:16 +08:00
|
|
|
{
|
2012-03-15 17:04:35 +08:00
|
|
|
struct imx_chip *imx;
|
2009-01-16 22:17:16 +08:00
|
|
|
struct resource *r;
|
|
|
|
int ret = 0;
|
|
|
|
|
2012-07-01 08:27:23 +08:00
|
|
|
imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
|
2012-03-15 17:04:35 +08:00
|
|
|
if (imx == NULL) {
|
2009-01-16 22:17:16 +08:00
|
|
|
dev_err(&pdev->dev, "failed to allocate memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2012-07-01 08:27:23 +08:00
|
|
|
imx->clk = devm_clk_get(&pdev->dev, "pwm");
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2012-07-01 08:27:23 +08:00
|
|
|
if (IS_ERR(imx->clk))
|
|
|
|
return PTR_ERR(imx->clk);
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
imx->chip.ops = &imx_pwm_ops;
|
|
|
|
imx->chip.dev = &pdev->dev;
|
|
|
|
imx->chip.base = -1;
|
|
|
|
imx->chip.npwm = 1;
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
imx->clk_enabled = 0;
|
2009-01-16 22:17:16 +08:00
|
|
|
|
|
|
|
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
if (r == NULL) {
|
|
|
|
dev_err(&pdev->dev, "no memory resource defined\n");
|
2012-07-01 08:27:23 +08:00
|
|
|
return -ENODEV;
|
2009-01-16 22:17:16 +08:00
|
|
|
}
|
|
|
|
|
2012-07-01 08:27:23 +08:00
|
|
|
imx->mmio_base = devm_request_and_ioremap(&pdev->dev, r);
|
|
|
|
if (imx->mmio_base == NULL)
|
|
|
|
return -EADDRNOTAVAIL;
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
ret = pwmchip_add(&imx->chip);
|
|
|
|
if (ret < 0)
|
2012-07-01 08:27:23 +08:00
|
|
|
return ret;
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
platform_set_drvdata(pdev, imx);
|
2009-01-16 22:17:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
static int __devexit imx_pwm_remove(struct platform_device *pdev)
|
2009-01-16 22:17:16 +08:00
|
|
|
{
|
2012-03-15 17:04:35 +08:00
|
|
|
struct imx_chip *imx;
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
imx = platform_get_drvdata(pdev);
|
|
|
|
if (imx == NULL)
|
2009-01-16 22:17:16 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
2012-07-01 08:27:23 +08:00
|
|
|
return pwmchip_remove(&imx->chip);
|
2009-01-16 22:17:16 +08:00
|
|
|
}
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
static struct platform_driver imx_pwm_driver = {
|
2009-01-16 22:17:16 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "mxc_pwm",
|
|
|
|
},
|
2012-03-15 17:04:35 +08:00
|
|
|
.probe = imx_pwm_probe,
|
|
|
|
.remove = __devexit_p(imx_pwm_remove),
|
2009-01-16 22:17:16 +08:00
|
|
|
};
|
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
static int __init imx_pwm_init(void)
|
2009-01-16 22:17:16 +08:00
|
|
|
{
|
2012-03-15 17:04:35 +08:00
|
|
|
return platform_driver_register(&imx_pwm_driver);
|
2009-01-16 22:17:16 +08:00
|
|
|
}
|
2012-03-15 17:04:35 +08:00
|
|
|
arch_initcall(imx_pwm_init);
|
2009-01-16 22:17:16 +08:00
|
|
|
|
2012-03-15 17:04:35 +08:00
|
|
|
static void __exit imx_pwm_exit(void)
|
2009-01-16 22:17:16 +08:00
|
|
|
{
|
2012-03-15 17:04:35 +08:00
|
|
|
platform_driver_unregister(&imx_pwm_driver);
|
2009-01-16 22:17:16 +08:00
|
|
|
}
|
2012-03-15 17:04:35 +08:00
|
|
|
module_exit(imx_pwm_exit);
|
2009-01-16 22:17:16 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
|