2012-07-25 13:28:18 +02:00
|
|
|
/*
|
|
|
|
* ECAP PWM driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Texas Instruments, Inc. - http://www.ti.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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/pm_runtime.h>
|
|
|
|
#include <linux/pwm.h>
|
2012-11-27 09:48:09 +01:00
|
|
|
#include <linux/of_device.h>
|
|
|
|
|
2012-07-25 13:28:18 +02:00
|
|
|
/* ECAP registers and bits definitions */
|
|
|
|
#define CAP1 0x08
|
|
|
|
#define CAP2 0x0C
|
|
|
|
#define CAP3 0x10
|
|
|
|
#define CAP4 0x14
|
|
|
|
#define ECCTL2 0x2A
|
2012-09-06 07:10:02 +02:00
|
|
|
#define ECCTL2_APWM_POL_LOW BIT(10)
|
2012-07-25 13:28:18 +02:00
|
|
|
#define ECCTL2_APWM_MODE BIT(9)
|
|
|
|
#define ECCTL2_SYNC_SEL_DISA (BIT(7) | BIT(6))
|
|
|
|
#define ECCTL2_TSCTR_FREERUN BIT(4)
|
|
|
|
|
2013-01-17 10:20:03 +01:00
|
|
|
struct ecap_context {
|
2017-08-21 08:29:41 +02:00
|
|
|
u32 cap3;
|
|
|
|
u32 cap4;
|
|
|
|
u16 ecctl2;
|
2013-01-17 10:20:03 +01:00
|
|
|
};
|
|
|
|
|
2012-07-25 13:28:18 +02:00
|
|
|
struct ecap_pwm_chip {
|
2017-08-21 08:29:41 +02:00
|
|
|
struct pwm_chip chip;
|
|
|
|
unsigned int clk_rate;
|
|
|
|
void __iomem *mmio_base;
|
2013-01-17 10:20:03 +01:00
|
|
|
struct ecap_context ctx;
|
2012-07-25 13:28:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct ecap_pwm_chip *to_ecap_pwm_chip(struct pwm_chip *chip)
|
|
|
|
{
|
|
|
|
return container_of(chip, struct ecap_pwm_chip, chip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* period_ns = 10^9 * period_cycles / PWM_CLK_RATE
|
|
|
|
* duty_ns = 10^9 * duty_cycles / PWM_CLK_RATE
|
|
|
|
*/
|
|
|
|
static int ecap_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
|
|
|
|
int duty_ns, int period_ns)
|
|
|
|
{
|
|
|
|
struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
|
2017-08-21 08:29:41 +02:00
|
|
|
u32 period_cycles, duty_cycles;
|
2012-07-25 13:28:18 +02:00
|
|
|
unsigned long long c;
|
2017-08-21 08:29:41 +02:00
|
|
|
u16 value;
|
2012-07-25 13:28:18 +02:00
|
|
|
|
2012-09-02 22:13:40 +02:00
|
|
|
if (period_ns > NSEC_PER_SEC)
|
2012-07-25 13:28:18 +02:00
|
|
|
return -ERANGE;
|
|
|
|
|
|
|
|
c = pc->clk_rate;
|
|
|
|
c = c * period_ns;
|
|
|
|
do_div(c, NSEC_PER_SEC);
|
2017-08-21 08:29:41 +02:00
|
|
|
period_cycles = (u32)c;
|
2012-07-25 13:28:18 +02:00
|
|
|
|
|
|
|
if (period_cycles < 1) {
|
|
|
|
period_cycles = 1;
|
|
|
|
duty_cycles = 1;
|
|
|
|
} else {
|
|
|
|
c = pc->clk_rate;
|
|
|
|
c = c * duty_ns;
|
|
|
|
do_div(c, NSEC_PER_SEC);
|
2017-08-21 08:29:41 +02:00
|
|
|
duty_cycles = (u32)c;
|
2012-07-25 13:28:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pm_runtime_get_sync(pc->chip.dev);
|
|
|
|
|
2017-08-21 08:29:41 +02:00
|
|
|
value = readw(pc->mmio_base + ECCTL2);
|
2012-07-25 13:28:18 +02:00
|
|
|
|
|
|
|
/* Configure APWM mode & disable sync option */
|
2017-08-21 08:29:41 +02:00
|
|
|
value |= ECCTL2_APWM_MODE | ECCTL2_SYNC_SEL_DISA;
|
2012-07-25 13:28:18 +02:00
|
|
|
|
2017-08-21 08:29:41 +02:00
|
|
|
writew(value, pc->mmio_base + ECCTL2);
|
2012-07-25 13:28:18 +02:00
|
|
|
|
2015-07-01 10:21:47 +02:00
|
|
|
if (!pwm_is_enabled(pwm)) {
|
2012-07-25 13:28:18 +02:00
|
|
|
/* Update active registers if not running */
|
|
|
|
writel(duty_cycles, pc->mmio_base + CAP2);
|
|
|
|
writel(period_cycles, pc->mmio_base + CAP1);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Update shadow registers to configure period and
|
|
|
|
* compare values. This helps current PWM period to
|
|
|
|
* complete on reconfiguring
|
|
|
|
*/
|
|
|
|
writel(duty_cycles, pc->mmio_base + CAP4);
|
|
|
|
writel(period_cycles, pc->mmio_base + CAP3);
|
|
|
|
}
|
|
|
|
|
2015-07-01 10:21:47 +02:00
|
|
|
if (!pwm_is_enabled(pwm)) {
|
2017-08-21 08:29:41 +02:00
|
|
|
value = readw(pc->mmio_base + ECCTL2);
|
2012-08-23 08:59:46 +02:00
|
|
|
/* Disable APWM mode to put APWM output Low */
|
2017-08-21 08:29:41 +02:00
|
|
|
value &= ~ECCTL2_APWM_MODE;
|
|
|
|
writew(value, pc->mmio_base + ECCTL2);
|
2012-08-23 08:59:46 +02:00
|
|
|
}
|
|
|
|
|
2012-07-25 13:28:18 +02:00
|
|
|
pm_runtime_put_sync(pc->chip.dev);
|
2017-08-21 08:29:41 +02:00
|
|
|
|
2012-07-25 13:28:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-06 07:10:02 +02:00
|
|
|
static int ecap_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
|
2017-08-21 08:29:41 +02:00
|
|
|
enum pwm_polarity polarity)
|
2012-09-06 07:10:02 +02:00
|
|
|
{
|
|
|
|
struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
|
2017-08-21 08:29:41 +02:00
|
|
|
u16 value;
|
2012-09-06 07:10:02 +02:00
|
|
|
|
|
|
|
pm_runtime_get_sync(pc->chip.dev);
|
2017-08-21 08:29:41 +02:00
|
|
|
|
|
|
|
value = readw(pc->mmio_base + ECCTL2);
|
|
|
|
|
2012-09-06 07:10:02 +02:00
|
|
|
if (polarity == PWM_POLARITY_INVERSED)
|
|
|
|
/* Duty cycle defines LOW period of PWM */
|
2017-08-21 08:29:41 +02:00
|
|
|
value |= ECCTL2_APWM_POL_LOW;
|
2012-09-06 07:10:02 +02:00
|
|
|
else
|
|
|
|
/* Duty cycle defines HIGH period of PWM */
|
2017-08-21 08:29:41 +02:00
|
|
|
value &= ~ECCTL2_APWM_POL_LOW;
|
|
|
|
|
|
|
|
writew(value, pc->mmio_base + ECCTL2);
|
2012-09-06 07:10:02 +02:00
|
|
|
|
|
|
|
pm_runtime_put_sync(pc->chip.dev);
|
2017-08-21 08:29:41 +02:00
|
|
|
|
2012-09-06 07:10:02 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-25 13:28:18 +02:00
|
|
|
static int ecap_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
|
2017-08-21 08:29:41 +02:00
|
|
|
u16 value;
|
2012-07-25 13:28:18 +02:00
|
|
|
|
|
|
|
/* Leave clock enabled on enabling PWM */
|
|
|
|
pm_runtime_get_sync(pc->chip.dev);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable 'Free run Time stamp counter mode' to start counter
|
|
|
|
* and 'APWM mode' to enable APWM output
|
|
|
|
*/
|
2017-08-21 08:29:41 +02:00
|
|
|
value = readw(pc->mmio_base + ECCTL2);
|
|
|
|
value |= ECCTL2_TSCTR_FREERUN | ECCTL2_APWM_MODE;
|
|
|
|
writew(value, pc->mmio_base + ECCTL2);
|
|
|
|
|
2012-07-25 13:28:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ecap_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
|
2017-08-21 08:29:41 +02:00
|
|
|
u16 value;
|
2012-07-25 13:28:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable 'Free run Time stamp counter mode' to stop counter
|
|
|
|
* and 'APWM mode' to put APWM output to low
|
|
|
|
*/
|
2017-08-21 08:29:41 +02:00
|
|
|
value = readw(pc->mmio_base + ECCTL2);
|
|
|
|
value &= ~(ECCTL2_TSCTR_FREERUN | ECCTL2_APWM_MODE);
|
|
|
|
writew(value, pc->mmio_base + ECCTL2);
|
2012-07-25 13:28:18 +02:00
|
|
|
|
|
|
|
/* Disable clock on PWM disable */
|
|
|
|
pm_runtime_put_sync(pc->chip.dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ecap_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
2015-07-01 10:21:47 +02:00
|
|
|
if (pwm_is_enabled(pwm)) {
|
2012-07-25 13:28:18 +02:00
|
|
|
dev_warn(chip->dev, "Removing PWM device without disabling\n");
|
|
|
|
pm_runtime_put_sync(chip->dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct pwm_ops ecap_pwm_ops = {
|
2017-08-21 08:29:41 +02:00
|
|
|
.free = ecap_pwm_free,
|
|
|
|
.config = ecap_pwm_config,
|
|
|
|
.set_polarity = ecap_pwm_set_polarity,
|
|
|
|
.enable = ecap_pwm_enable,
|
|
|
|
.disable = ecap_pwm_disable,
|
|
|
|
.owner = THIS_MODULE,
|
2012-07-25 13:28:18 +02:00
|
|
|
};
|
|
|
|
|
2012-11-27 09:48:09 +01:00
|
|
|
static const struct of_device_id ecap_of_match[] = {
|
2016-05-03 17:56:52 +02:00
|
|
|
{ .compatible = "ti,am3352-ecap" },
|
2012-11-27 09:48:09 +01:00
|
|
|
{ .compatible = "ti,am33xx-ecap" },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, ecap_of_match);
|
|
|
|
|
2012-11-19 19:23:14 +01:00
|
|
|
static int ecap_pwm_probe(struct platform_device *pdev)
|
2012-07-25 13:28:18 +02:00
|
|
|
{
|
2016-05-03 17:56:52 +02:00
|
|
|
struct device_node *np = pdev->dev.of_node;
|
2017-08-21 08:29:41 +02:00
|
|
|
struct ecap_pwm_chip *pc;
|
2012-07-25 13:28:18 +02:00
|
|
|
struct resource *r;
|
|
|
|
struct clk *clk;
|
2017-08-21 08:29:41 +02:00
|
|
|
int ret;
|
2012-07-25 13:28:18 +02:00
|
|
|
|
|
|
|
pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
|
2014-04-23 11:41:27 +02:00
|
|
|
if (!pc)
|
2012-07-25 13:28:18 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
clk = devm_clk_get(&pdev->dev, "fck");
|
2016-05-03 17:56:52 +02:00
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
|
|
|
|
dev_warn(&pdev->dev, "Binding is obsolete.\n");
|
|
|
|
clk = devm_clk_get(pdev->dev.parent, "fck");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-25 13:28:18 +02:00
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
dev_err(&pdev->dev, "failed to get clock\n");
|
|
|
|
return PTR_ERR(clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
pc->clk_rate = clk_get_rate(clk);
|
|
|
|
if (!pc->clk_rate) {
|
|
|
|
dev_err(&pdev->dev, "failed to get clock rate\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pc->chip.dev = &pdev->dev;
|
|
|
|
pc->chip.ops = &ecap_pwm_ops;
|
2012-11-27 09:48:09 +01:00
|
|
|
pc->chip.of_xlate = of_pwm_xlate_with_flags;
|
|
|
|
pc->chip.of_pwm_n_cells = 3;
|
2012-07-25 13:28:18 +02:00
|
|
|
pc->chip.base = -1;
|
|
|
|
pc->chip.npwm = 1;
|
|
|
|
|
|
|
|
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2013-01-21 11:09:16 +01:00
|
|
|
pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
|
|
|
|
if (IS_ERR(pc->mmio_base))
|
|
|
|
return PTR_ERR(pc->mmio_base);
|
2012-07-25 13:28:18 +02:00
|
|
|
|
|
|
|
ret = pwmchip_add(&pc->chip);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-21 08:31:37 +02:00
|
|
|
platform_set_drvdata(pdev, pc);
|
2012-07-25 13:28:18 +02:00
|
|
|
pm_runtime_enable(&pdev->dev);
|
2012-11-27 09:48:09 +01:00
|
|
|
|
2012-07-25 13:28:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-19 19:26:09 +01:00
|
|
|
static int ecap_pwm_remove(struct platform_device *pdev)
|
2012-07-25 13:28:18 +02:00
|
|
|
{
|
|
|
|
struct ecap_pwm_chip *pc = platform_get_drvdata(pdev);
|
|
|
|
|
|
|
|
pm_runtime_disable(&pdev->dev);
|
2017-08-21 08:29:41 +02:00
|
|
|
|
2012-07-25 13:28:18 +02:00
|
|
|
return pwmchip_remove(&pc->chip);
|
|
|
|
}
|
|
|
|
|
2013-08-02 08:11:18 +02:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2013-03-26 15:54:58 +01:00
|
|
|
static void ecap_pwm_save_context(struct ecap_pwm_chip *pc)
|
2013-01-17 10:20:03 +01:00
|
|
|
{
|
|
|
|
pm_runtime_get_sync(pc->chip.dev);
|
|
|
|
pc->ctx.ecctl2 = readw(pc->mmio_base + ECCTL2);
|
|
|
|
pc->ctx.cap4 = readl(pc->mmio_base + CAP4);
|
|
|
|
pc->ctx.cap3 = readl(pc->mmio_base + CAP3);
|
|
|
|
pm_runtime_put_sync(pc->chip.dev);
|
|
|
|
}
|
|
|
|
|
2013-03-26 15:54:58 +01:00
|
|
|
static void ecap_pwm_restore_context(struct ecap_pwm_chip *pc)
|
2013-01-17 10:20:03 +01:00
|
|
|
{
|
|
|
|
writel(pc->ctx.cap3, pc->mmio_base + CAP3);
|
|
|
|
writel(pc->ctx.cap4, pc->mmio_base + CAP4);
|
|
|
|
writew(pc->ctx.ecctl2, pc->mmio_base + ECCTL2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ecap_pwm_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct ecap_pwm_chip *pc = dev_get_drvdata(dev);
|
|
|
|
struct pwm_device *pwm = pc->chip.pwms;
|
|
|
|
|
|
|
|
ecap_pwm_save_context(pc);
|
|
|
|
|
|
|
|
/* Disable explicitly if PWM is running */
|
2015-07-01 10:21:47 +02:00
|
|
|
if (pwm_is_enabled(pwm))
|
2013-01-17 10:20:03 +01:00
|
|
|
pm_runtime_put_sync(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ecap_pwm_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct ecap_pwm_chip *pc = dev_get_drvdata(dev);
|
|
|
|
struct pwm_device *pwm = pc->chip.pwms;
|
|
|
|
|
|
|
|
/* Enable explicitly if PWM was running */
|
2015-07-01 10:21:47 +02:00
|
|
|
if (pwm_is_enabled(pwm))
|
2013-01-17 10:20:03 +01:00
|
|
|
pm_runtime_get_sync(dev);
|
|
|
|
|
|
|
|
ecap_pwm_restore_context(pc);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-11 03:12:58 +01:00
|
|
|
#endif
|
2013-01-17 10:20:03 +01:00
|
|
|
|
|
|
|
static SIMPLE_DEV_PM_OPS(ecap_pwm_pm_ops, ecap_pwm_suspend, ecap_pwm_resume);
|
|
|
|
|
2012-07-25 13:28:18 +02:00
|
|
|
static struct platform_driver ecap_pwm_driver = {
|
|
|
|
.driver = {
|
2017-08-21 08:29:41 +02:00
|
|
|
.name = "ecap",
|
2012-11-27 09:48:09 +01:00
|
|
|
.of_match_table = ecap_of_match,
|
2017-08-21 08:29:41 +02:00
|
|
|
.pm = &ecap_pwm_pm_ops,
|
2012-07-25 13:28:18 +02:00
|
|
|
},
|
|
|
|
.probe = ecap_pwm_probe,
|
2012-11-19 19:21:28 +01:00
|
|
|
.remove = ecap_pwm_remove,
|
2012-07-25 13:28:18 +02:00
|
|
|
};
|
|
|
|
module_platform_driver(ecap_pwm_driver);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("ECAP PWM driver");
|
|
|
|
MODULE_AUTHOR("Texas Instruments");
|
|
|
|
MODULE_LICENSE("GPL");
|