2018-08-21 22:02:21 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-09-30 08:47:53 +00:00
|
|
|
/*
|
|
|
|
* R-Car PWM Timer driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Renesas Electronics Corporation
|
2019-10-24 07:14:10 +00:00
|
|
|
*
|
|
|
|
* Limitations:
|
|
|
|
* - The hardware cannot generate a 0% duty cycle.
|
2015-09-30 08:47:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
2019-01-09 08:19:08 +00:00
|
|
|
#include <linux/log2.h>
|
|
|
|
#include <linux/math64.h>
|
2015-09-30 08:47:53 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/pm_runtime.h>
|
|
|
|
#include <linux/pwm.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
|
|
|
#define RCAR_PWM_MAX_DIVISION 24
|
|
|
|
#define RCAR_PWM_MAX_CYCLE 1023
|
|
|
|
|
|
|
|
#define RCAR_PWMCR 0x00
|
|
|
|
#define RCAR_PWMCR_CC0_MASK 0x000f0000
|
|
|
|
#define RCAR_PWMCR_CC0_SHIFT 16
|
|
|
|
#define RCAR_PWMCR_CCMD BIT(15)
|
|
|
|
#define RCAR_PWMCR_SYNC BIT(11)
|
|
|
|
#define RCAR_PWMCR_SS0 BIT(4)
|
|
|
|
#define RCAR_PWMCR_EN0 BIT(0)
|
|
|
|
|
|
|
|
#define RCAR_PWMCNT 0x04
|
|
|
|
#define RCAR_PWMCNT_CYC0_MASK 0x03ff0000
|
|
|
|
#define RCAR_PWMCNT_CYC0_SHIFT 16
|
|
|
|
#define RCAR_PWMCNT_PH0_MASK 0x000003ff
|
|
|
|
#define RCAR_PWMCNT_PH0_SHIFT 0
|
|
|
|
|
|
|
|
struct rcar_pwm_chip {
|
|
|
|
void __iomem *base;
|
|
|
|
struct clk *clk;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct rcar_pwm_chip *to_rcar_pwm_chip(struct pwm_chip *chip)
|
|
|
|
{
|
2024-02-14 09:32:16 +00:00
|
|
|
return pwmchip_get_drvdata(chip);
|
2015-09-30 08:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rcar_pwm_write(struct rcar_pwm_chip *rp, u32 data,
|
|
|
|
unsigned int offset)
|
|
|
|
{
|
|
|
|
writel(data, rp->base + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 rcar_pwm_read(struct rcar_pwm_chip *rp, unsigned int offset)
|
|
|
|
{
|
|
|
|
return readl(rp->base + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rcar_pwm_update(struct rcar_pwm_chip *rp, u32 mask, u32 data,
|
|
|
|
unsigned int offset)
|
|
|
|
{
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
value = rcar_pwm_read(rp, offset);
|
|
|
|
value &= ~mask;
|
|
|
|
value |= data & mask;
|
|
|
|
rcar_pwm_write(rp, value, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rcar_pwm_get_clock_division(struct rcar_pwm_chip *rp, int period_ns)
|
|
|
|
{
|
|
|
|
unsigned long clk_rate = clk_get_rate(rp->clk);
|
2019-01-09 08:19:08 +00:00
|
|
|
u64 div, tmp;
|
2015-09-30 08:47:53 +00:00
|
|
|
|
|
|
|
if (clk_rate == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2019-01-09 08:19:08 +00:00
|
|
|
div = (u64)NSEC_PER_SEC * RCAR_PWM_MAX_CYCLE;
|
|
|
|
tmp = (u64)period_ns * clk_rate + div - 1;
|
|
|
|
tmp = div64_u64(tmp, div);
|
|
|
|
div = ilog2(tmp - 1) + 1;
|
2015-09-30 08:47:53 +00:00
|
|
|
|
|
|
|
return (div <= RCAR_PWM_MAX_DIVISION) ? div : -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rcar_pwm_set_clock_control(struct rcar_pwm_chip *rp,
|
|
|
|
unsigned int div)
|
|
|
|
{
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
value = rcar_pwm_read(rp, RCAR_PWMCR);
|
|
|
|
value &= ~(RCAR_PWMCR_CCMD | RCAR_PWMCR_CC0_MASK);
|
|
|
|
|
|
|
|
if (div & 1)
|
|
|
|
value |= RCAR_PWMCR_CCMD;
|
|
|
|
|
|
|
|
div >>= 1;
|
|
|
|
|
|
|
|
value |= div << RCAR_PWMCR_CC0_SHIFT;
|
|
|
|
rcar_pwm_write(rp, value, RCAR_PWMCR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rcar_pwm_set_counter(struct rcar_pwm_chip *rp, int div, int duty_ns,
|
|
|
|
int period_ns)
|
|
|
|
{
|
|
|
|
unsigned long long one_cycle, tmp; /* 0.01 nanoseconds */
|
|
|
|
unsigned long clk_rate = clk_get_rate(rp->clk);
|
|
|
|
u32 cyc, ph;
|
|
|
|
|
2022-02-21 16:28:16 +00:00
|
|
|
one_cycle = NSEC_PER_SEC * 100ULL << div;
|
2015-09-30 08:47:53 +00:00
|
|
|
do_div(one_cycle, clk_rate);
|
|
|
|
|
|
|
|
tmp = period_ns * 100ULL;
|
|
|
|
do_div(tmp, one_cycle);
|
|
|
|
cyc = (tmp << RCAR_PWMCNT_CYC0_SHIFT) & RCAR_PWMCNT_CYC0_MASK;
|
|
|
|
|
|
|
|
tmp = duty_ns * 100ULL;
|
|
|
|
do_div(tmp, one_cycle);
|
|
|
|
ph = tmp & RCAR_PWMCNT_PH0_MASK;
|
|
|
|
|
|
|
|
/* Avoid prohibited setting */
|
|
|
|
if (cyc == 0 || ph == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
rcar_pwm_write(rp, cyc | ph, RCAR_PWMCNT);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rcar_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
2024-02-14 09:32:14 +00:00
|
|
|
return pm_runtime_get_sync(pwmchip_parent(chip));
|
2015-09-30 08:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rcar_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
2024-02-14 09:32:14 +00:00
|
|
|
pm_runtime_put(pwmchip_parent(chip));
|
2015-09-30 08:47:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 08:19:07 +00:00
|
|
|
static int rcar_pwm_enable(struct rcar_pwm_chip *rp)
|
2015-09-30 08:47:53 +00:00
|
|
|
{
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
/* Don't enable the PWM device if CYC0 or PH0 is 0 */
|
|
|
|
value = rcar_pwm_read(rp, RCAR_PWMCNT);
|
|
|
|
if ((value & RCAR_PWMCNT_CYC0_MASK) == 0 ||
|
|
|
|
(value & RCAR_PWMCNT_PH0_MASK) == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
rcar_pwm_update(rp, RCAR_PWMCR_EN0, RCAR_PWMCR_EN0, RCAR_PWMCR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-09 08:19:07 +00:00
|
|
|
static void rcar_pwm_disable(struct rcar_pwm_chip *rp)
|
2015-09-30 08:47:53 +00:00
|
|
|
{
|
|
|
|
rcar_pwm_update(rp, RCAR_PWMCR_EN0, 0, RCAR_PWMCR);
|
|
|
|
}
|
|
|
|
|
2019-01-09 08:19:05 +00:00
|
|
|
static int rcar_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
|
2019-08-24 15:37:07 +00:00
|
|
|
const struct pwm_state *state)
|
2019-01-09 08:19:05 +00:00
|
|
|
{
|
|
|
|
struct rcar_pwm_chip *rp = to_rcar_pwm_chip(chip);
|
|
|
|
int div, ret;
|
|
|
|
|
|
|
|
/* This HW/driver only supports normal polarity */
|
|
|
|
if (state->polarity != PWM_POLARITY_NORMAL)
|
2020-11-11 20:18:11 +00:00
|
|
|
return -EINVAL;
|
2019-01-09 08:19:05 +00:00
|
|
|
|
|
|
|
if (!state->enabled) {
|
2019-01-09 08:19:07 +00:00
|
|
|
rcar_pwm_disable(rp);
|
2019-01-09 08:19:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
div = rcar_pwm_get_clock_division(rp, state->period);
|
|
|
|
if (div < 0)
|
|
|
|
return div;
|
|
|
|
|
|
|
|
rcar_pwm_update(rp, RCAR_PWMCR_SYNC, RCAR_PWMCR_SYNC, RCAR_PWMCR);
|
|
|
|
|
|
|
|
ret = rcar_pwm_set_counter(rp, div, state->duty_cycle, state->period);
|
|
|
|
if (!ret)
|
|
|
|
rcar_pwm_set_clock_control(rp, div);
|
|
|
|
|
|
|
|
/* The SYNC should be set to 0 even if rcar_pwm_set_counter failed */
|
|
|
|
rcar_pwm_update(rp, RCAR_PWMCR_SYNC, 0, RCAR_PWMCR);
|
|
|
|
|
2019-08-19 06:20:12 +00:00
|
|
|
if (!ret)
|
2019-01-09 08:19:07 +00:00
|
|
|
ret = rcar_pwm_enable(rp);
|
2019-01-09 08:19:05 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-09-30 08:47:53 +00:00
|
|
|
static const struct pwm_ops rcar_pwm_ops = {
|
|
|
|
.request = rcar_pwm_request,
|
|
|
|
.free = rcar_pwm_free,
|
2019-01-09 08:19:05 +00:00
|
|
|
.apply = rcar_pwm_apply,
|
2015-09-30 08:47:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int rcar_pwm_probe(struct platform_device *pdev)
|
|
|
|
{
|
2024-02-14 09:32:15 +00:00
|
|
|
struct pwm_chip *chip;
|
2015-09-30 08:47:53 +00:00
|
|
|
struct rcar_pwm_chip *rcar_pwm;
|
|
|
|
int ret;
|
|
|
|
|
2024-02-14 09:32:16 +00:00
|
|
|
chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*rcar_pwm));
|
|
|
|
if (IS_ERR(chip))
|
|
|
|
return PTR_ERR(chip);
|
|
|
|
rcar_pwm = to_rcar_pwm_chip(chip);
|
2015-09-30 08:47:53 +00:00
|
|
|
|
2019-12-29 08:05:41 +00:00
|
|
|
rcar_pwm->base = devm_platform_ioremap_resource(pdev, 0);
|
2015-09-30 08:47:53 +00:00
|
|
|
if (IS_ERR(rcar_pwm->base))
|
|
|
|
return PTR_ERR(rcar_pwm->base);
|
|
|
|
|
|
|
|
rcar_pwm->clk = devm_clk_get(&pdev->dev, NULL);
|
|
|
|
if (IS_ERR(rcar_pwm->clk)) {
|
|
|
|
dev_err(&pdev->dev, "cannot get clock\n");
|
|
|
|
return PTR_ERR(rcar_pwm->clk);
|
|
|
|
}
|
|
|
|
|
2024-02-14 09:32:15 +00:00
|
|
|
chip->ops = &rcar_pwm_ops;
|
2015-09-30 08:47:53 +00:00
|
|
|
|
2024-02-14 09:32:15 +00:00
|
|
|
platform_set_drvdata(pdev, chip);
|
2015-09-30 08:47:53 +00:00
|
|
|
|
2020-03-16 10:32:14 +00:00
|
|
|
pm_runtime_enable(&pdev->dev);
|
|
|
|
|
2024-02-14 09:32:15 +00:00
|
|
|
ret = pwmchip_add(chip);
|
2015-09-30 08:47:53 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&pdev->dev, "failed to register PWM chip: %d\n", ret);
|
2020-03-16 10:32:14 +00:00
|
|
|
pm_runtime_disable(&pdev->dev);
|
2015-09-30 08:47:53 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-03 18:54:32 +00:00
|
|
|
static void rcar_pwm_remove(struct platform_device *pdev)
|
2015-09-30 08:47:53 +00:00
|
|
|
{
|
2024-02-14 09:32:15 +00:00
|
|
|
struct pwm_chip *chip = platform_get_drvdata(pdev);
|
2020-03-16 10:32:14 +00:00
|
|
|
|
2024-02-14 09:32:15 +00:00
|
|
|
pwmchip_remove(chip);
|
2015-09-30 08:47:53 +00:00
|
|
|
|
|
|
|
pm_runtime_disable(&pdev->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id rcar_pwm_of_table[] = {
|
|
|
|
{ .compatible = "renesas,pwm-rcar", },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, rcar_pwm_of_table);
|
|
|
|
|
|
|
|
static struct platform_driver rcar_pwm_driver = {
|
|
|
|
.probe = rcar_pwm_probe,
|
2023-03-03 18:54:32 +00:00
|
|
|
.remove_new = rcar_pwm_remove,
|
2015-09-30 08:47:53 +00:00
|
|
|
.driver = {
|
|
|
|
.name = "pwm-rcar",
|
2023-03-12 13:51:19 +00:00
|
|
|
.of_match_table = rcar_pwm_of_table,
|
2015-09-30 08:47:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
module_platform_driver(rcar_pwm_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");
|
|
|
|
MODULE_DESCRIPTION("Renesas PWM Timer Driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
MODULE_ALIAS("platform:pwm-rcar");
|