1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PWM framework driver for Cirrus Logic EP93xx
5 * Copyright (c) 2009 Matthieu Crapet <mcrapet@gmail.com>
6 * Copyright (c) 2009, 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
8 * EP9301/02 have only one channel:
9 * platform device ep93xx-pwm.1 - PWMOUT1 (EGPIO14)
11 * EP9307 has only one channel:
12 * platform device ep93xx-pwm.0 - PWMOUT
14 * EP9312/15 have two channels:
15 * platform device ep93xx-pwm.0 - PWMOUT
16 * platform device ep93xx-pwm.1 - PWMOUT1 (EGPIO14)
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/clk.h>
23 #include <linux/err.h>
25 #include <linux/pwm.h>
27 #include <asm/div64.h>
29 #include <linux/soc/cirrus/ep93xx.h> /* for ep93xx_pwm_{acquire,release}_gpio() */
31 #define EP93XX_PWMx_TERM_COUNT 0x00
32 #define EP93XX_PWMx_DUTY_CYCLE 0x04
33 #define EP93XX_PWMx_ENABLE 0x08
34 #define EP93XX_PWMx_INVERT 0x0c
42 static inline struct ep93xx_pwm *to_ep93xx_pwm(struct pwm_chip *chip)
44 return container_of(chip, struct ep93xx_pwm, chip);
47 static int ep93xx_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
49 struct platform_device *pdev = to_platform_device(chip->dev);
51 return ep93xx_pwm_acquire_gpio(pdev);
54 static void ep93xx_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
56 struct platform_device *pdev = to_platform_device(chip->dev);
58 ep93xx_pwm_release_gpio(pdev);
61 static int ep93xx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
62 const struct pwm_state *state)
65 struct ep93xx_pwm *ep93xx_pwm = to_ep93xx_pwm(chip);
66 bool enabled = state->enabled;
67 void __iomem *base = ep93xx_pwm->base;
69 unsigned long period_cycles;
70 unsigned long duty_cycles;
73 if (state->polarity != pwm->state.polarity) {
75 writew(0x0, ep93xx_pwm->base + EP93XX_PWMx_ENABLE);
76 clk_disable_unprepare(ep93xx_pwm->clk);
81 * The clock needs to be enabled to access the PWM registers.
82 * Polarity can only be changed when the PWM is disabled.
84 ret = clk_prepare_enable(ep93xx_pwm->clk);
88 if (state->polarity == PWM_POLARITY_INVERSED)
89 writew(0x1, ep93xx_pwm->base + EP93XX_PWMx_INVERT);
91 writew(0x0, ep93xx_pwm->base + EP93XX_PWMx_INVERT);
93 clk_disable_unprepare(ep93xx_pwm->clk);
96 if (!state->enabled) {
98 writew(0x0, ep93xx_pwm->base + EP93XX_PWMx_ENABLE);
99 clk_disable_unprepare(ep93xx_pwm->clk);
106 * The clock needs to be enabled to access the PWM registers.
107 * Configuration can be changed at any time.
109 if (!pwm_is_enabled(pwm)) {
110 ret = clk_prepare_enable(ep93xx_pwm->clk);
115 c = clk_get_rate(ep93xx_pwm->clk);
117 do_div(c, 1000000000);
121 c *= state->duty_cycle;
122 do_div(c, state->period);
125 if (period_cycles < 0x10000 && duty_cycles < 0x10000) {
126 term = readw(base + EP93XX_PWMx_TERM_COUNT);
128 /* Order is important if PWM is running */
129 if (period_cycles > term) {
130 writew(period_cycles, base + EP93XX_PWMx_TERM_COUNT);
131 writew(duty_cycles, base + EP93XX_PWMx_DUTY_CYCLE);
133 writew(duty_cycles, base + EP93XX_PWMx_DUTY_CYCLE);
134 writew(period_cycles, base + EP93XX_PWMx_TERM_COUNT);
141 if (!pwm_is_enabled(pwm))
142 clk_disable_unprepare(ep93xx_pwm->clk);
148 ret = clk_prepare_enable(ep93xx_pwm->clk);
152 writew(0x1, ep93xx_pwm->base + EP93XX_PWMx_ENABLE);
158 static const struct pwm_ops ep93xx_pwm_ops = {
159 .request = ep93xx_pwm_request,
160 .free = ep93xx_pwm_free,
161 .apply = ep93xx_pwm_apply,
162 .owner = THIS_MODULE,
165 static int ep93xx_pwm_probe(struct platform_device *pdev)
167 struct ep93xx_pwm *ep93xx_pwm;
170 ep93xx_pwm = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_pwm), GFP_KERNEL);
174 ep93xx_pwm->base = devm_platform_ioremap_resource(pdev, 0);
175 if (IS_ERR(ep93xx_pwm->base))
176 return PTR_ERR(ep93xx_pwm->base);
178 ep93xx_pwm->clk = devm_clk_get(&pdev->dev, "pwm_clk");
179 if (IS_ERR(ep93xx_pwm->clk))
180 return PTR_ERR(ep93xx_pwm->clk);
182 ep93xx_pwm->chip.dev = &pdev->dev;
183 ep93xx_pwm->chip.ops = &ep93xx_pwm_ops;
184 ep93xx_pwm->chip.npwm = 1;
186 ret = devm_pwmchip_add(&pdev->dev, &ep93xx_pwm->chip);
193 static struct platform_driver ep93xx_pwm_driver = {
195 .name = "ep93xx-pwm",
197 .probe = ep93xx_pwm_probe,
199 module_platform_driver(ep93xx_pwm_driver);
201 MODULE_DESCRIPTION("Cirrus Logic EP93xx PWM driver");
202 MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>");
203 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
204 MODULE_ALIAS("platform:ep93xx-pwm");
205 MODULE_LICENSE("GPL");