12b62c894SBinbin Zhou // SPDX-License-Identifier: GPL-2.0-only
22b62c894SBinbin Zhou /*
32b62c894SBinbin Zhou * Copyright (C) 2017-2025 Loongson Technology Corporation Limited.
42b62c894SBinbin Zhou *
52b62c894SBinbin Zhou * Loongson PWM driver
62b62c894SBinbin Zhou *
72b62c894SBinbin Zhou * For Loongson's PWM IP block documentation please refer Chapter 11 of
82b62c894SBinbin Zhou * Reference Manual: https://loongson.github.io/LoongArch-Documentation/Loongson-7A1000-usermanual-EN.pdf
92b62c894SBinbin Zhou *
102b62c894SBinbin Zhou * Author: Juxin Gao <gaojuxin@loongson.cn>
112b62c894SBinbin Zhou * Further cleanup and restructuring by:
122b62c894SBinbin Zhou * Binbin Zhou <zhoubinbin@loongson.cn>
132b62c894SBinbin Zhou *
142b62c894SBinbin Zhou * Limitations:
152b62c894SBinbin Zhou * - If both DUTY and PERIOD are set to 0, the output is a constant low signal.
162b62c894SBinbin Zhou * - When disabled the output is driven to 0 independent of the configured
172b62c894SBinbin Zhou * polarity.
182b62c894SBinbin Zhou * - If the register is reconfigured while PWM is running, it does not complete
192b62c894SBinbin Zhou * the currently running period.
202b62c894SBinbin Zhou * - Disabling the PWM stops the output immediately (without waiting for current
212b62c894SBinbin Zhou * period to complete first).
222b62c894SBinbin Zhou */
232b62c894SBinbin Zhou
242b62c894SBinbin Zhou #include <linux/acpi.h>
252b62c894SBinbin Zhou #include <linux/clk.h>
262b62c894SBinbin Zhou #include <linux/device.h>
272b62c894SBinbin Zhou #include <linux/init.h>
282b62c894SBinbin Zhou #include <linux/io.h>
292b62c894SBinbin Zhou #include <linux/kernel.h>
302b62c894SBinbin Zhou #include <linux/module.h>
312b62c894SBinbin Zhou #include <linux/platform_device.h>
322b62c894SBinbin Zhou #include <linux/pwm.h>
332b62c894SBinbin Zhou #include <linux/units.h>
342b62c894SBinbin Zhou
352b62c894SBinbin Zhou /* Loongson PWM registers */
362b62c894SBinbin Zhou #define LOONGSON_PWM_REG_DUTY 0x4 /* Low Pulse Buffer Register */
372b62c894SBinbin Zhou #define LOONGSON_PWM_REG_PERIOD 0x8 /* Pulse Period Buffer Register */
382b62c894SBinbin Zhou #define LOONGSON_PWM_REG_CTRL 0xc /* Control Register */
392b62c894SBinbin Zhou
402b62c894SBinbin Zhou /* Control register bits */
412b62c894SBinbin Zhou #define LOONGSON_PWM_CTRL_REG_EN BIT(0) /* Counter Enable Bit */
422b62c894SBinbin Zhou #define LOONGSON_PWM_CTRL_REG_OE BIT(3) /* Pulse Output Enable Control Bit, Valid Low */
432b62c894SBinbin Zhou #define LOONGSON_PWM_CTRL_REG_SINGLE BIT(4) /* Single Pulse Control Bit */
442b62c894SBinbin Zhou #define LOONGSON_PWM_CTRL_REG_INTE BIT(5) /* Interrupt Enable Bit */
452b62c894SBinbin Zhou #define LOONGSON_PWM_CTRL_REG_INT BIT(6) /* Interrupt Bit */
462b62c894SBinbin Zhou #define LOONGSON_PWM_CTRL_REG_RST BIT(7) /* Counter Reset Bit */
472b62c894SBinbin Zhou #define LOONGSON_PWM_CTRL_REG_CAPTE BIT(8) /* Measurement Pulse Enable Bit */
482b62c894SBinbin Zhou #define LOONGSON_PWM_CTRL_REG_INVERT BIT(9) /* Output flip-flop Enable Bit */
492b62c894SBinbin Zhou #define LOONGSON_PWM_CTRL_REG_DZONE BIT(10) /* Anti-dead Zone Enable Bit */
502b62c894SBinbin Zhou
512b62c894SBinbin Zhou /* default input clk frequency for the ACPI case */
522b62c894SBinbin Zhou #define LOONGSON_PWM_FREQ_DEFAULT 50000 /* Hz */
532b62c894SBinbin Zhou
542b62c894SBinbin Zhou struct pwm_loongson_ddata {
552b62c894SBinbin Zhou struct clk *clk;
562b62c894SBinbin Zhou void __iomem *base;
572b62c894SBinbin Zhou u64 clk_rate;
582b62c894SBinbin Zhou };
592b62c894SBinbin Zhou
to_pwm_loongson_ddata(struct pwm_chip * chip)602b62c894SBinbin Zhou static inline __pure struct pwm_loongson_ddata *to_pwm_loongson_ddata(struct pwm_chip *chip)
612b62c894SBinbin Zhou {
622b62c894SBinbin Zhou return pwmchip_get_drvdata(chip);
632b62c894SBinbin Zhou }
642b62c894SBinbin Zhou
pwm_loongson_readl(struct pwm_loongson_ddata * ddata,u32 offset)652b62c894SBinbin Zhou static inline u32 pwm_loongson_readl(struct pwm_loongson_ddata *ddata, u32 offset)
662b62c894SBinbin Zhou {
672b62c894SBinbin Zhou return readl(ddata->base + offset);
682b62c894SBinbin Zhou }
692b62c894SBinbin Zhou
pwm_loongson_writel(struct pwm_loongson_ddata * ddata,u32 val,u32 offset)702b62c894SBinbin Zhou static inline void pwm_loongson_writel(struct pwm_loongson_ddata *ddata,
712b62c894SBinbin Zhou u32 val, u32 offset)
722b62c894SBinbin Zhou {
732b62c894SBinbin Zhou writel(val, ddata->base + offset);
742b62c894SBinbin Zhou }
752b62c894SBinbin Zhou
pwm_loongson_set_polarity(struct pwm_chip * chip,struct pwm_device * pwm,enum pwm_polarity polarity)762b62c894SBinbin Zhou static int pwm_loongson_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
772b62c894SBinbin Zhou enum pwm_polarity polarity)
782b62c894SBinbin Zhou {
792b62c894SBinbin Zhou u16 val;
802b62c894SBinbin Zhou struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
812b62c894SBinbin Zhou
822b62c894SBinbin Zhou val = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_CTRL);
832b62c894SBinbin Zhou
842b62c894SBinbin Zhou if (polarity == PWM_POLARITY_INVERSED)
852b62c894SBinbin Zhou /* Duty cycle defines LOW period of PWM */
862b62c894SBinbin Zhou val |= LOONGSON_PWM_CTRL_REG_INVERT;
872b62c894SBinbin Zhou else
882b62c894SBinbin Zhou /* Duty cycle defines HIGH period of PWM */
892b62c894SBinbin Zhou val &= ~LOONGSON_PWM_CTRL_REG_INVERT;
902b62c894SBinbin Zhou
912b62c894SBinbin Zhou pwm_loongson_writel(ddata, val, LOONGSON_PWM_REG_CTRL);
922b62c894SBinbin Zhou
932b62c894SBinbin Zhou return 0;
942b62c894SBinbin Zhou }
952b62c894SBinbin Zhou
pwm_loongson_disable(struct pwm_chip * chip,struct pwm_device * pwm)962b62c894SBinbin Zhou static void pwm_loongson_disable(struct pwm_chip *chip, struct pwm_device *pwm)
972b62c894SBinbin Zhou {
982b62c894SBinbin Zhou u32 val;
992b62c894SBinbin Zhou struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
1002b62c894SBinbin Zhou
1012b62c894SBinbin Zhou val = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_CTRL);
1022b62c894SBinbin Zhou val &= ~LOONGSON_PWM_CTRL_REG_EN;
1032b62c894SBinbin Zhou pwm_loongson_writel(ddata, val, LOONGSON_PWM_REG_CTRL);
1042b62c894SBinbin Zhou }
1052b62c894SBinbin Zhou
pwm_loongson_enable(struct pwm_chip * chip,struct pwm_device * pwm)1062b62c894SBinbin Zhou static int pwm_loongson_enable(struct pwm_chip *chip, struct pwm_device *pwm)
1072b62c894SBinbin Zhou {
1082b62c894SBinbin Zhou u32 val;
1092b62c894SBinbin Zhou struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
1102b62c894SBinbin Zhou
1112b62c894SBinbin Zhou val = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_CTRL);
1122b62c894SBinbin Zhou val |= LOONGSON_PWM_CTRL_REG_EN;
1132b62c894SBinbin Zhou pwm_loongson_writel(ddata, val, LOONGSON_PWM_REG_CTRL);
1142b62c894SBinbin Zhou
1152b62c894SBinbin Zhou return 0;
1162b62c894SBinbin Zhou }
1172b62c894SBinbin Zhou
pwm_loongson_config(struct pwm_chip * chip,struct pwm_device * pwm,u64 duty_ns,u64 period_ns)1182b62c894SBinbin Zhou static int pwm_loongson_config(struct pwm_chip *chip, struct pwm_device *pwm,
1192b62c894SBinbin Zhou u64 duty_ns, u64 period_ns)
1202b62c894SBinbin Zhou {
121dcb882bdSUwe Kleine-König u64 duty, period;
1222b62c894SBinbin Zhou struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
1232b62c894SBinbin Zhou
1242b62c894SBinbin Zhou /* duty = duty_ns * ddata->clk_rate / NSEC_PER_SEC */
1252b62c894SBinbin Zhou duty = mul_u64_u64_div_u64(duty_ns, ddata->clk_rate, NSEC_PER_SEC);
1262b62c894SBinbin Zhou if (duty > U32_MAX)
1272b62c894SBinbin Zhou duty = U32_MAX;
1282b62c894SBinbin Zhou
1292b62c894SBinbin Zhou /* period = period_ns * ddata->clk_rate / NSEC_PER_SEC */
1302b62c894SBinbin Zhou period = mul_u64_u64_div_u64(period_ns, ddata->clk_rate, NSEC_PER_SEC);
1312b62c894SBinbin Zhou if (period > U32_MAX)
1322b62c894SBinbin Zhou period = U32_MAX;
1332b62c894SBinbin Zhou
1342b62c894SBinbin Zhou pwm_loongson_writel(ddata, duty, LOONGSON_PWM_REG_DUTY);
1352b62c894SBinbin Zhou pwm_loongson_writel(ddata, period, LOONGSON_PWM_REG_PERIOD);
1362b62c894SBinbin Zhou
1372b62c894SBinbin Zhou return 0;
1382b62c894SBinbin Zhou }
1392b62c894SBinbin Zhou
pwm_loongson_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)1402b62c894SBinbin Zhou static int pwm_loongson_apply(struct pwm_chip *chip, struct pwm_device *pwm,
1412b62c894SBinbin Zhou const struct pwm_state *state)
1422b62c894SBinbin Zhou {
1432b62c894SBinbin Zhou int ret;
1442b62c894SBinbin Zhou bool enabled = pwm->state.enabled;
1452b62c894SBinbin Zhou
1462b62c894SBinbin Zhou if (!state->enabled) {
1472b62c894SBinbin Zhou if (enabled)
1482b62c894SBinbin Zhou pwm_loongson_disable(chip, pwm);
1492b62c894SBinbin Zhou return 0;
1502b62c894SBinbin Zhou }
1512b62c894SBinbin Zhou
1522b62c894SBinbin Zhou ret = pwm_loongson_set_polarity(chip, pwm, state->polarity);
1532b62c894SBinbin Zhou if (ret)
1542b62c894SBinbin Zhou return ret;
1552b62c894SBinbin Zhou
1562b62c894SBinbin Zhou ret = pwm_loongson_config(chip, pwm, state->duty_cycle, state->period);
1572b62c894SBinbin Zhou if (ret)
1582b62c894SBinbin Zhou return ret;
1592b62c894SBinbin Zhou
1602b62c894SBinbin Zhou if (!enabled && state->enabled)
1612b62c894SBinbin Zhou ret = pwm_loongson_enable(chip, pwm);
1622b62c894SBinbin Zhou
1632b62c894SBinbin Zhou return ret;
1642b62c894SBinbin Zhou }
1652b62c894SBinbin Zhou
pwm_loongson_get_state(struct pwm_chip * chip,struct pwm_device * pwm,struct pwm_state * state)1662b62c894SBinbin Zhou static int pwm_loongson_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
1672b62c894SBinbin Zhou struct pwm_state *state)
1682b62c894SBinbin Zhou {
1692b62c894SBinbin Zhou u32 duty, period, ctrl;
1702b62c894SBinbin Zhou struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
1712b62c894SBinbin Zhou
1722b62c894SBinbin Zhou duty = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_DUTY);
1732b62c894SBinbin Zhou period = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_PERIOD);
1742b62c894SBinbin Zhou ctrl = pwm_loongson_readl(ddata, LOONGSON_PWM_REG_CTRL);
1752b62c894SBinbin Zhou
1762b62c894SBinbin Zhou /* duty & period have a max of 2^32, so we can't overflow */
1772b62c894SBinbin Zhou state->duty_cycle = DIV64_U64_ROUND_UP((u64)duty * NSEC_PER_SEC, ddata->clk_rate);
1782b62c894SBinbin Zhou state->period = DIV64_U64_ROUND_UP((u64)period * NSEC_PER_SEC, ddata->clk_rate);
1792b62c894SBinbin Zhou state->polarity = (ctrl & LOONGSON_PWM_CTRL_REG_INVERT) ? PWM_POLARITY_INVERSED :
1802b62c894SBinbin Zhou PWM_POLARITY_NORMAL;
1812b62c894SBinbin Zhou state->enabled = (ctrl & LOONGSON_PWM_CTRL_REG_EN) ? true : false;
1822b62c894SBinbin Zhou
1832b62c894SBinbin Zhou return 0;
1842b62c894SBinbin Zhou }
1852b62c894SBinbin Zhou
1862b62c894SBinbin Zhou static const struct pwm_ops pwm_loongson_ops = {
1872b62c894SBinbin Zhou .apply = pwm_loongson_apply,
1882b62c894SBinbin Zhou .get_state = pwm_loongson_get_state,
1892b62c894SBinbin Zhou };
1902b62c894SBinbin Zhou
pwm_loongson_probe(struct platform_device * pdev)1912b62c894SBinbin Zhou static int pwm_loongson_probe(struct platform_device *pdev)
1922b62c894SBinbin Zhou {
1932b62c894SBinbin Zhou int ret;
1942b62c894SBinbin Zhou struct pwm_chip *chip;
1952b62c894SBinbin Zhou struct pwm_loongson_ddata *ddata;
1962b62c894SBinbin Zhou struct device *dev = &pdev->dev;
1972b62c894SBinbin Zhou
1982b62c894SBinbin Zhou chip = devm_pwmchip_alloc(dev, 1, sizeof(*ddata));
1992b62c894SBinbin Zhou if (IS_ERR(chip))
2002b62c894SBinbin Zhou return PTR_ERR(chip);
2012b62c894SBinbin Zhou ddata = to_pwm_loongson_ddata(chip);
2022b62c894SBinbin Zhou
2032b62c894SBinbin Zhou ddata->base = devm_platform_ioremap_resource(pdev, 0);
2042b62c894SBinbin Zhou if (IS_ERR(ddata->base))
2052b62c894SBinbin Zhou return PTR_ERR(ddata->base);
2062b62c894SBinbin Zhou
2072b62c894SBinbin Zhou ddata->clk = devm_clk_get_optional_enabled(dev, NULL);
2082b62c894SBinbin Zhou if (IS_ERR(ddata->clk))
2092b62c894SBinbin Zhou return dev_err_probe(dev, PTR_ERR(ddata->clk),
2102b62c894SBinbin Zhou "Failed to get pwm clock\n");
2112b62c894SBinbin Zhou if (ddata->clk) {
2122b62c894SBinbin Zhou ret = devm_clk_rate_exclusive_get(dev, ddata->clk);
2132b62c894SBinbin Zhou if (ret)
214*bd897149SDan Carpenter return dev_err_probe(dev, ret,
2152b62c894SBinbin Zhou "Failed to get exclusive rate\n");
2162b62c894SBinbin Zhou
2172b62c894SBinbin Zhou ddata->clk_rate = clk_get_rate(ddata->clk);
2182b62c894SBinbin Zhou if (!ddata->clk_rate)
2192b62c894SBinbin Zhou return dev_err_probe(dev, -EINVAL,
2202b62c894SBinbin Zhou "Failed to get frequency\n");
2212b62c894SBinbin Zhou } else {
2222b62c894SBinbin Zhou ddata->clk_rate = LOONGSON_PWM_FREQ_DEFAULT;
2232b62c894SBinbin Zhou }
2242b62c894SBinbin Zhou
2252b62c894SBinbin Zhou /* This check is done to prevent an overflow in .apply */
2262b62c894SBinbin Zhou if (ddata->clk_rate > NSEC_PER_SEC)
2272b62c894SBinbin Zhou return dev_err_probe(dev, -EINVAL, "PWM clock out of range\n");
2282b62c894SBinbin Zhou
2292b62c894SBinbin Zhou chip->ops = &pwm_loongson_ops;
2302b62c894SBinbin Zhou chip->atomic = true;
2312b62c894SBinbin Zhou dev_set_drvdata(dev, chip);
2322b62c894SBinbin Zhou
2332b62c894SBinbin Zhou ret = devm_pwmchip_add(dev, chip);
2342b62c894SBinbin Zhou if (ret < 0)
2352b62c894SBinbin Zhou return dev_err_probe(dev, ret, "Failed to add PWM chip\n");
2362b62c894SBinbin Zhou
2372b62c894SBinbin Zhou return 0;
2382b62c894SBinbin Zhou }
2392b62c894SBinbin Zhou
pwm_loongson_suspend(struct device * dev)2402b62c894SBinbin Zhou static int pwm_loongson_suspend(struct device *dev)
2412b62c894SBinbin Zhou {
2422b62c894SBinbin Zhou struct pwm_chip *chip = dev_get_drvdata(dev);
2432b62c894SBinbin Zhou struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
2442b62c894SBinbin Zhou struct pwm_device *pwm = &chip->pwms[0];
2452b62c894SBinbin Zhou
2462b62c894SBinbin Zhou if (pwm->state.enabled)
2472b62c894SBinbin Zhou return -EBUSY;
2482b62c894SBinbin Zhou
2492b62c894SBinbin Zhou clk_disable_unprepare(ddata->clk);
2502b62c894SBinbin Zhou
2512b62c894SBinbin Zhou return 0;
2522b62c894SBinbin Zhou }
2532b62c894SBinbin Zhou
pwm_loongson_resume(struct device * dev)2542b62c894SBinbin Zhou static int pwm_loongson_resume(struct device *dev)
2552b62c894SBinbin Zhou {
2562b62c894SBinbin Zhou struct pwm_chip *chip = dev_get_drvdata(dev);
2572b62c894SBinbin Zhou struct pwm_loongson_ddata *ddata = to_pwm_loongson_ddata(chip);
2582b62c894SBinbin Zhou
2592b62c894SBinbin Zhou return clk_prepare_enable(ddata->clk);
2602b62c894SBinbin Zhou }
2612b62c894SBinbin Zhou
2622b62c894SBinbin Zhou static DEFINE_SIMPLE_DEV_PM_OPS(pwm_loongson_pm_ops, pwm_loongson_suspend,
2632b62c894SBinbin Zhou pwm_loongson_resume);
2642b62c894SBinbin Zhou
2652b62c894SBinbin Zhou static const struct of_device_id pwm_loongson_of_ids[] = {
2662b62c894SBinbin Zhou { .compatible = "loongson,ls7a-pwm" },
2672b62c894SBinbin Zhou { /* sentinel */ },
2682b62c894SBinbin Zhou };
2692b62c894SBinbin Zhou MODULE_DEVICE_TABLE(of, pwm_loongson_of_ids);
2702b62c894SBinbin Zhou
2712b62c894SBinbin Zhou static const struct acpi_device_id pwm_loongson_acpi_ids[] = {
2722b62c894SBinbin Zhou { "LOON0006" },
2732b62c894SBinbin Zhou { }
2742b62c894SBinbin Zhou };
2752b62c894SBinbin Zhou MODULE_DEVICE_TABLE(acpi, pwm_loongson_acpi_ids);
2762b62c894SBinbin Zhou
2772b62c894SBinbin Zhou static struct platform_driver pwm_loongson_driver = {
2782b62c894SBinbin Zhou .probe = pwm_loongson_probe,
2792b62c894SBinbin Zhou .driver = {
2802b62c894SBinbin Zhou .name = "loongson-pwm",
2812b62c894SBinbin Zhou .pm = pm_ptr(&pwm_loongson_pm_ops),
2822b62c894SBinbin Zhou .of_match_table = pwm_loongson_of_ids,
2832b62c894SBinbin Zhou .acpi_match_table = pwm_loongson_acpi_ids,
2842b62c894SBinbin Zhou },
2852b62c894SBinbin Zhou };
2862b62c894SBinbin Zhou module_platform_driver(pwm_loongson_driver);
2872b62c894SBinbin Zhou
2882b62c894SBinbin Zhou MODULE_DESCRIPTION("Loongson PWM driver");
2892b62c894SBinbin Zhou MODULE_AUTHOR("Loongson Technology Corporation Limited.");
2902b62c894SBinbin Zhou MODULE_LICENSE("GPL");
291