1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/pwm/pwm-pxa.c
5 * simple driver for PWM (Pulse Width Modulator) controller
7 * 2008-02-13 initial version
8 * eric miao <eric.miao@marvell.com>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/clk.h>
18 #include <linux/pwm.h>
19 #include <linux/of_device.h>
21 #include <asm/div64.h>
23 #define HAS_SECONDARY_PWM 0x10
25 static const struct platform_device_id pwm_id_table[] = {
26 /* PWM has_secondary_pwm? */
28 { "pxa27x-pwm", HAS_SECONDARY_PWM },
33 MODULE_DEVICE_TABLE(platform, pwm_id_table);
35 /* PWM registers and bits definitions */
40 #define PWMCR_SD (1 << 6)
41 #define PWMDCR_FD (1 << 10)
48 void __iomem *mmio_base;
51 static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
53 return container_of(chip, struct pxa_pwm_chip, chip);
57 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
58 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
60 static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
61 u64 duty_ns, u64 period_ns)
63 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
65 unsigned long period_cycles, prescale, pv, dc;
69 offset = pwm->hwpwm ? 0x10 : 0;
71 c = clk_get_rate(pc->clk);
73 do_div(c, 1000000000);
76 if (period_cycles < 1)
78 prescale = (period_cycles - 1) / 1024;
79 pv = period_cycles / (prescale + 1) - 1;
84 if (duty_ns == period_ns)
87 dc = mul_u64_u64_div_u64(pv + 1, duty_ns, period_ns);
89 /* NOTE: the clock to PWM has to be enabled first
90 * before writing to the registers
92 rc = clk_prepare_enable(pc->clk);
96 writel(prescale, pc->mmio_base + offset + PWMCR);
97 writel(dc, pc->mmio_base + offset + PWMDCR);
98 writel(pv, pc->mmio_base + offset + PWMPCR);
100 clk_disable_unprepare(pc->clk);
104 static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
106 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
108 return clk_prepare_enable(pc->clk);
111 static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
113 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
115 clk_disable_unprepare(pc->clk);
118 static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
119 const struct pwm_state *state)
123 if (state->polarity != PWM_POLARITY_NORMAL)
126 if (!state->enabled) {
127 if (pwm->state.enabled)
128 pxa_pwm_disable(chip, pwm);
133 err = pxa_pwm_config(chip, pwm, state->duty_cycle, state->period);
137 if (!pwm->state.enabled)
138 return pxa_pwm_enable(chip, pwm);
143 static const struct pwm_ops pxa_pwm_ops = {
144 .apply = pxa_pwm_apply,
145 .owner = THIS_MODULE,
150 * Device tree users must create one device instance for each PWM channel.
151 * Hence we dispense with the HAS_SECONDARY_PWM and "tell" the original driver
152 * code that this is a single channel pxa25x-pwm. Currently all devices are
153 * supported identically.
155 static const struct of_device_id pwm_of_match[] = {
156 { .compatible = "marvell,pxa250-pwm", .data = &pwm_id_table[0]},
157 { .compatible = "marvell,pxa270-pwm", .data = &pwm_id_table[0]},
158 { .compatible = "marvell,pxa168-pwm", .data = &pwm_id_table[0]},
159 { .compatible = "marvell,pxa910-pwm", .data = &pwm_id_table[0]},
162 MODULE_DEVICE_TABLE(of, pwm_of_match);
164 #define pwm_of_match NULL
167 static const struct platform_device_id *pxa_pwm_get_id_dt(struct device *dev)
169 const struct of_device_id *id = of_match_device(pwm_of_match, dev);
171 return id ? id->data : NULL;
174 static int pwm_probe(struct platform_device *pdev)
176 const struct platform_device_id *id = platform_get_device_id(pdev);
177 struct pxa_pwm_chip *pc;
180 if (IS_ENABLED(CONFIG_OF) && id == NULL)
181 id = pxa_pwm_get_id_dt(&pdev->dev);
186 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
190 pc->clk = devm_clk_get(&pdev->dev, NULL);
192 return PTR_ERR(pc->clk);
194 pc->chip.dev = &pdev->dev;
195 pc->chip.ops = &pxa_pwm_ops;
196 pc->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
198 if (IS_ENABLED(CONFIG_OF)) {
199 pc->chip.of_xlate = of_pwm_single_xlate;
200 pc->chip.of_pwm_n_cells = 1;
203 pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
204 if (IS_ERR(pc->mmio_base))
205 return PTR_ERR(pc->mmio_base);
207 ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
209 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
216 static struct platform_driver pwm_driver = {
218 .name = "pxa25x-pwm",
219 .of_match_table = pwm_of_match,
222 .id_table = pwm_id_table,
225 module_platform_driver(pwm_driver);
227 MODULE_LICENSE("GPL v2");