2018-11-10 16:22:58 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-10-08 10:14:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Bart Tanghe <bart.tanghe@thomasmore.be>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/pwm.h>
|
|
|
|
|
|
|
|
#define PWM_CONTROL 0x000
|
|
|
|
#define PWM_CONTROL_SHIFT(x) ((x) * 8)
|
|
|
|
#define PWM_CONTROL_MASK 0xff
|
|
|
|
#define PWM_MODE 0x80 /* set timer in PWM mode */
|
|
|
|
#define PWM_ENABLE (1 << 0)
|
|
|
|
#define PWM_POLARITY (1 << 4)
|
|
|
|
|
|
|
|
#define PERIOD(x) (((x) * 0x10) + 0x10)
|
|
|
|
#define DUTY(x) (((x) * 0x10) + 0x14)
|
|
|
|
|
2019-08-24 14:09:47 +00:00
|
|
|
#define PERIOD_MIN 0x2
|
2014-10-08 10:14:32 +00:00
|
|
|
|
|
|
|
struct bcm2835_pwm {
|
|
|
|
void __iomem *base;
|
|
|
|
struct clk *clk;
|
2023-12-20 14:24:25 +00:00
|
|
|
unsigned long rate;
|
2014-10-08 10:14:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip)
|
|
|
|
{
|
2024-02-14 09:31:03 +00:00
|
|
|
return pwmchip_get_drvdata(chip);
|
2014-10-08 10:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm2835_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
value = readl(pc->base + PWM_CONTROL);
|
|
|
|
value &= ~(PWM_CONTROL_MASK << PWM_CONTROL_SHIFT(pwm->hwpwm));
|
|
|
|
value |= (PWM_MODE << PWM_CONTROL_SHIFT(pwm->hwpwm));
|
|
|
|
writel(value, pc->base + PWM_CONTROL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bcm2835_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
|
|
|
|
{
|
|
|
|
struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
value = readl(pc->base + PWM_CONTROL);
|
|
|
|
value &= ~(PWM_CONTROL_MASK << PWM_CONTROL_SHIFT(pwm->hwpwm));
|
|
|
|
writel(value, pc->base + PWM_CONTROL);
|
|
|
|
}
|
|
|
|
|
2020-12-09 20:48:25 +00:00
|
|
|
static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
|
|
|
|
const struct pwm_state *state)
|
2014-10-08 10:14:32 +00:00
|
|
|
{
|
2020-12-09 20:48:25 +00:00
|
|
|
|
2014-10-08 10:14:32 +00:00
|
|
|
struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
|
2021-01-14 20:48:04 +00:00
|
|
|
unsigned long long period_cycles;
|
|
|
|
u64 max_period;
|
|
|
|
|
2020-12-09 20:48:25 +00:00
|
|
|
u32 val;
|
2015-12-01 22:55:40 +00:00
|
|
|
|
2021-01-14 20:48:04 +00:00
|
|
|
/*
|
|
|
|
* period_cycles must be a 32 bit value, so period * rate / NSEC_PER_SEC
|
|
|
|
* must be <= U32_MAX. As U32_MAX * NSEC_PER_SEC < U64_MAX the
|
|
|
|
* multiplication period * rate doesn't overflow.
|
|
|
|
* To calculate the maximal possible period that guarantees the
|
|
|
|
* above inequality:
|
|
|
|
*
|
|
|
|
* round(period * rate / NSEC_PER_SEC) <= U32_MAX
|
|
|
|
* <=> period * rate / NSEC_PER_SEC < U32_MAX + 0.5
|
|
|
|
* <=> period * rate < (U32_MAX + 0.5) * NSEC_PER_SEC
|
|
|
|
* <=> period < ((U32_MAX + 0.5) * NSEC_PER_SEC) / rate
|
|
|
|
* <=> period < ((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate
|
|
|
|
* <=> period <= ceil((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate) - 1
|
|
|
|
*/
|
2023-12-20 14:24:25 +00:00
|
|
|
max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, pc->rate) - 1;
|
2021-01-14 20:48:04 +00:00
|
|
|
|
|
|
|
if (state->period > max_period)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-12-09 20:48:25 +00:00
|
|
|
/* set period */
|
2023-12-20 14:24:25 +00:00
|
|
|
period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * pc->rate, NSEC_PER_SEC);
|
2014-10-08 10:14:32 +00:00
|
|
|
|
2021-01-14 20:48:04 +00:00
|
|
|
/* don't accept a period that is too small */
|
|
|
|
if (period_cycles < PERIOD_MIN)
|
2014-10-08 10:14:32 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2021-01-14 20:48:04 +00:00
|
|
|
writel(period_cycles, pc->base + PERIOD(pwm->hwpwm));
|
2014-10-08 10:14:32 +00:00
|
|
|
|
2020-12-09 20:48:25 +00:00
|
|
|
/* set duty cycle */
|
2023-12-20 14:24:25 +00:00
|
|
|
val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * pc->rate, NSEC_PER_SEC);
|
2020-12-09 20:48:25 +00:00
|
|
|
writel(val, pc->base + DUTY(pwm->hwpwm));
|
2014-10-08 10:14:32 +00:00
|
|
|
|
2020-12-09 20:48:25 +00:00
|
|
|
/* set polarity */
|
|
|
|
val = readl(pc->base + PWM_CONTROL);
|
2014-10-08 10:14:32 +00:00
|
|
|
|
2020-12-09 20:48:25 +00:00
|
|
|
if (state->polarity == PWM_POLARITY_NORMAL)
|
|
|
|
val &= ~(PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm));
|
|
|
|
else
|
|
|
|
val |= PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm);
|
2014-10-08 10:14:32 +00:00
|
|
|
|
2020-12-09 20:48:25 +00:00
|
|
|
/* enable/disable */
|
|
|
|
if (state->enabled)
|
|
|
|
val |= PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm);
|
2014-10-08 10:14:32 +00:00
|
|
|
else
|
2020-12-09 20:48:25 +00:00
|
|
|
val &= ~(PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm));
|
2014-10-08 10:14:32 +00:00
|
|
|
|
2020-12-09 20:48:25 +00:00
|
|
|
writel(val, pc->base + PWM_CONTROL);
|
2014-10-08 10:14:32 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct pwm_ops bcm2835_pwm_ops = {
|
|
|
|
.request = bcm2835_pwm_request,
|
|
|
|
.free = bcm2835_pwm_free,
|
2020-12-09 20:48:25 +00:00
|
|
|
.apply = bcm2835_pwm_apply,
|
2014-10-08 10:14:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int bcm2835_pwm_probe(struct platform_device *pdev)
|
|
|
|
{
|
2024-03-10 13:47:19 +00:00
|
|
|
struct device *dev = &pdev->dev;
|
2024-02-14 09:31:03 +00:00
|
|
|
struct pwm_chip *chip;
|
2014-10-08 10:14:32 +00:00
|
|
|
struct bcm2835_pwm *pc;
|
|
|
|
int ret;
|
|
|
|
|
2024-03-10 13:47:19 +00:00
|
|
|
chip = devm_pwmchip_alloc(dev, 2, sizeof(*pc));
|
2024-02-14 09:31:03 +00:00
|
|
|
if (IS_ERR(chip))
|
|
|
|
return PTR_ERR(chip);
|
|
|
|
pc = to_bcm2835_pwm(chip);
|
2014-10-08 10:14:32 +00:00
|
|
|
|
2019-12-29 08:06:00 +00:00
|
|
|
pc->base = devm_platform_ioremap_resource(pdev, 0);
|
2014-10-08 10:14:32 +00:00
|
|
|
if (IS_ERR(pc->base))
|
|
|
|
return PTR_ERR(pc->base);
|
|
|
|
|
2024-03-10 13:47:19 +00:00
|
|
|
pc->clk = devm_clk_get_enabled(dev, NULL);
|
2020-08-26 14:47:42 +00:00
|
|
|
if (IS_ERR(pc->clk))
|
2024-03-10 13:47:19 +00:00
|
|
|
return dev_err_probe(dev, PTR_ERR(pc->clk),
|
2020-08-26 14:47:42 +00:00
|
|
|
"clock not found\n");
|
2014-10-08 10:14:32 +00:00
|
|
|
|
2024-03-10 13:47:20 +00:00
|
|
|
ret = devm_clk_rate_exclusive_get(dev, pc->clk);
|
2023-12-20 14:24:25 +00:00
|
|
|
if (ret)
|
2024-03-10 13:47:19 +00:00
|
|
|
return dev_err_probe(dev, ret,
|
2023-12-20 14:24:25 +00:00
|
|
|
"fail to get exclusive rate\n");
|
|
|
|
|
|
|
|
pc->rate = clk_get_rate(pc->clk);
|
|
|
|
if (!pc->rate)
|
2024-03-10 13:47:19 +00:00
|
|
|
return dev_err_probe(dev, -EINVAL,
|
2023-12-20 14:24:25 +00:00
|
|
|
"failed to get clock rate\n");
|
|
|
|
|
2024-02-14 09:31:03 +00:00
|
|
|
chip->ops = &bcm2835_pwm_ops;
|
|
|
|
chip->atomic = true;
|
2014-10-08 10:14:32 +00:00
|
|
|
|
2023-11-13 16:46:32 +00:00
|
|
|
platform_set_drvdata(pdev, pc);
|
|
|
|
|
2024-03-10 13:47:19 +00:00
|
|
|
ret = devm_pwmchip_add(dev, chip);
|
2014-10-08 10:14:32 +00:00
|
|
|
if (ret < 0)
|
2024-03-10 13:47:19 +00:00
|
|
|
return dev_err_probe(dev, ret, "failed to add pwmchip\n");
|
2014-10-08 10:14:32 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-11 17:07:17 +00:00
|
|
|
static int bcm2835_pwm_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct bcm2835_pwm *pc = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
clk_disable_unprepare(pc->clk);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm2835_pwm_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct bcm2835_pwm *pc = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
return clk_prepare_enable(pc->clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DEFINE_SIMPLE_DEV_PM_OPS(bcm2835_pwm_pm_ops, bcm2835_pwm_suspend,
|
|
|
|
bcm2835_pwm_resume);
|
|
|
|
|
2014-10-08 10:14:32 +00:00
|
|
|
static const struct of_device_id bcm2835_pwm_of_match[] = {
|
|
|
|
{ .compatible = "brcm,bcm2835-pwm", },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, bcm2835_pwm_of_match);
|
|
|
|
|
|
|
|
static struct platform_driver bcm2835_pwm_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "bcm2835-pwm",
|
|
|
|
.of_match_table = bcm2835_pwm_of_match,
|
2023-10-11 17:07:17 +00:00
|
|
|
.pm = pm_ptr(&bcm2835_pwm_pm_ops),
|
2014-10-08 10:14:32 +00:00
|
|
|
},
|
|
|
|
.probe = bcm2835_pwm_probe,
|
|
|
|
};
|
|
|
|
module_platform_driver(bcm2835_pwm_driver);
|
|
|
|
|
2015-12-01 22:55:41 +00:00
|
|
|
MODULE_AUTHOR("Bart Tanghe <bart.tanghe@thomasmore.be>");
|
2014-10-08 10:14:32 +00:00
|
|
|
MODULE_DESCRIPTION("Broadcom BCM2835 PWM driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|