1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
217b2b478SThierry Reding /*
345b301d2SAxel Lin * drivers/pwm/pwm-pxa.c
417b2b478SThierry Reding *
517b2b478SThierry Reding * simple driver for PWM (Pulse Width Modulator) controller
617b2b478SThierry Reding *
717b2b478SThierry Reding * 2008-02-13 initial version
817b2b478SThierry Reding * eric miao <eric.miao@marvell.com>
98ba2725fSDoug Brown *
108ba2725fSDoug Brown * Links to reference manuals for some of the supported PWM chips can be found
11173cb655SJonathan Corbet * in Documentation/arch/arm/marvell.rst.
128ba2725fSDoug Brown *
138ba2725fSDoug Brown * Limitations:
148ba2725fSDoug Brown * - When PWM is stopped, the current PWM period stops abruptly at the next
158ba2725fSDoug Brown * input clock (PWMCR_SD is set) and the output is driven to inactive.
1617b2b478SThierry Reding */
1717b2b478SThierry Reding
180a41b0c5SRob Herring #include <linux/mod_devicetable.h>
1917b2b478SThierry Reding #include <linux/module.h>
2017b2b478SThierry Reding #include <linux/kernel.h>
2117b2b478SThierry Reding #include <linux/platform_device.h>
2217b2b478SThierry Reding #include <linux/slab.h>
2317b2b478SThierry Reding #include <linux/err.h>
2417b2b478SThierry Reding #include <linux/clk.h>
2517b2b478SThierry Reding #include <linux/io.h>
2617b2b478SThierry Reding #include <linux/pwm.h>
27b52fa7bcSMike Dunn #include <linux/of_device.h>
2817b2b478SThierry Reding
2917b2b478SThierry Reding #include <asm/div64.h>
3017b2b478SThierry Reding
3117b2b478SThierry Reding #define HAS_SECONDARY_PWM 0x10
3217b2b478SThierry Reding
3317b2b478SThierry Reding static const struct platform_device_id pwm_id_table[] = {
3417b2b478SThierry Reding /* PWM has_secondary_pwm? */
3517b2b478SThierry Reding { "pxa25x-pwm", 0 },
3622976a5dSAxel Lin { "pxa27x-pwm", HAS_SECONDARY_PWM },
3722976a5dSAxel Lin { "pxa168-pwm", 0 },
3822976a5dSAxel Lin { "pxa910-pwm", 0 },
3917b2b478SThierry Reding { },
4017b2b478SThierry Reding };
4117b2b478SThierry Reding MODULE_DEVICE_TABLE(platform, pwm_id_table);
4217b2b478SThierry Reding
4317b2b478SThierry Reding /* PWM registers and bits definitions */
4417b2b478SThierry Reding #define PWMCR (0x00)
4517b2b478SThierry Reding #define PWMDCR (0x04)
4617b2b478SThierry Reding #define PWMPCR (0x08)
4717b2b478SThierry Reding
4817b2b478SThierry Reding #define PWMCR_SD (1 << 6)
4917b2b478SThierry Reding #define PWMDCR_FD (1 << 10)
5017b2b478SThierry Reding
5117b2b478SThierry Reding struct pxa_pwm_chip {
5217b2b478SThierry Reding struct pwm_chip chip;
5317b2b478SThierry Reding struct device *dev;
5417b2b478SThierry Reding
5517b2b478SThierry Reding struct clk *clk;
5617b2b478SThierry Reding void __iomem *mmio_base;
5717b2b478SThierry Reding };
5817b2b478SThierry Reding
to_pxa_pwm_chip(struct pwm_chip * chip)5917b2b478SThierry Reding static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
6017b2b478SThierry Reding {
6117b2b478SThierry Reding return container_of(chip, struct pxa_pwm_chip, chip);
6217b2b478SThierry Reding }
6317b2b478SThierry Reding
6417b2b478SThierry Reding /*
6517b2b478SThierry Reding * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
6617b2b478SThierry Reding * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
6717b2b478SThierry Reding */
pxa_pwm_config(struct pwm_chip * chip,struct pwm_device * pwm,u64 duty_ns,u64 period_ns)6817b2b478SThierry Reding static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
69657e54e5SUwe Kleine-König u64 duty_ns, u64 period_ns)
7017b2b478SThierry Reding {
7117b2b478SThierry Reding struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
7217b2b478SThierry Reding unsigned long long c;
7317b2b478SThierry Reding unsigned long period_cycles, prescale, pv, dc;
7417b2b478SThierry Reding unsigned long offset;
7517b2b478SThierry Reding
7617b2b478SThierry Reding offset = pwm->hwpwm ? 0x10 : 0;
7717b2b478SThierry Reding
7817b2b478SThierry Reding c = clk_get_rate(pc->clk);
7917b2b478SThierry Reding c = c * period_ns;
8017b2b478SThierry Reding do_div(c, 1000000000);
8117b2b478SThierry Reding period_cycles = c;
8217b2b478SThierry Reding
8317b2b478SThierry Reding if (period_cycles < 1)
8417b2b478SThierry Reding period_cycles = 1;
8517b2b478SThierry Reding prescale = (period_cycles - 1) / 1024;
8617b2b478SThierry Reding pv = period_cycles / (prescale + 1) - 1;
8717b2b478SThierry Reding
8817b2b478SThierry Reding if (prescale > 63)
8917b2b478SThierry Reding return -EINVAL;
9017b2b478SThierry Reding
9117b2b478SThierry Reding if (duty_ns == period_ns)
9217b2b478SThierry Reding dc = PWMDCR_FD;
9317b2b478SThierry Reding else
94657e54e5SUwe Kleine-König dc = mul_u64_u64_div_u64(pv + 1, duty_ns, period_ns);
9517b2b478SThierry Reding
96092c2ef4SDoug Brown writel(prescale | PWMCR_SD, pc->mmio_base + offset + PWMCR);
9717b2b478SThierry Reding writel(dc, pc->mmio_base + offset + PWMDCR);
9817b2b478SThierry Reding writel(pv, pc->mmio_base + offset + PWMPCR);
9917b2b478SThierry Reding
10017b2b478SThierry Reding return 0;
10117b2b478SThierry Reding }
10217b2b478SThierry Reding
pxa_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)103657e54e5SUwe Kleine-König static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
104657e54e5SUwe Kleine-König const struct pwm_state *state)
105657e54e5SUwe Kleine-König {
106f956b838SDoug Brown struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
107152f2d1dSDoug Brown u64 duty_cycle;
108657e54e5SUwe Kleine-König int err;
109657e54e5SUwe Kleine-König
110657e54e5SUwe Kleine-König if (state->polarity != PWM_POLARITY_NORMAL)
111657e54e5SUwe Kleine-König return -EINVAL;
112657e54e5SUwe Kleine-König
113152f2d1dSDoug Brown err = clk_prepare_enable(pc->clk);
114657e54e5SUwe Kleine-König if (err)
115657e54e5SUwe Kleine-König return err;
116657e54e5SUwe Kleine-König
117152f2d1dSDoug Brown duty_cycle = state->enabled ? state->duty_cycle : 0;
118152f2d1dSDoug Brown
119152f2d1dSDoug Brown err = pxa_pwm_config(chip, pwm, duty_cycle, state->period);
120152f2d1dSDoug Brown if (err) {
121152f2d1dSDoug Brown clk_disable_unprepare(pc->clk);
122152f2d1dSDoug Brown return err;
123152f2d1dSDoug Brown }
124152f2d1dSDoug Brown
125152f2d1dSDoug Brown if (state->enabled && !pwm->state.enabled)
126152f2d1dSDoug Brown return 0;
127152f2d1dSDoug Brown
128152f2d1dSDoug Brown clk_disable_unprepare(pc->clk);
129152f2d1dSDoug Brown
130152f2d1dSDoug Brown if (!state->enabled && pwm->state.enabled)
131152f2d1dSDoug Brown clk_disable_unprepare(pc->clk);
132657e54e5SUwe Kleine-König
133657e54e5SUwe Kleine-König return 0;
134657e54e5SUwe Kleine-König }
135657e54e5SUwe Kleine-König
136b2ec9efcSBhumika Goyal static const struct pwm_ops pxa_pwm_ops = {
137657e54e5SUwe Kleine-König .apply = pxa_pwm_apply,
13817b2b478SThierry Reding .owner = THIS_MODULE,
13917b2b478SThierry Reding };
14017b2b478SThierry Reding
141b52fa7bcSMike Dunn #ifdef CONFIG_OF
142b52fa7bcSMike Dunn /*
143fdec4f72SThierry Reding * Device tree users must create one device instance for each PWM channel.
144b52fa7bcSMike Dunn * Hence we dispense with the HAS_SECONDARY_PWM and "tell" the original driver
145b52fa7bcSMike Dunn * code that this is a single channel pxa25x-pwm. Currently all devices are
146b52fa7bcSMike Dunn * supported identically.
147b52fa7bcSMike Dunn */
1482ae69a46SThierry Reding static const struct of_device_id pwm_of_match[] = {
149b52fa7bcSMike Dunn { .compatible = "marvell,pxa250-pwm", .data = &pwm_id_table[0]},
150b52fa7bcSMike Dunn { .compatible = "marvell,pxa270-pwm", .data = &pwm_id_table[0]},
151b52fa7bcSMike Dunn { .compatible = "marvell,pxa168-pwm", .data = &pwm_id_table[0]},
152b52fa7bcSMike Dunn { .compatible = "marvell,pxa910-pwm", .data = &pwm_id_table[0]},
153b52fa7bcSMike Dunn { }
154b52fa7bcSMike Dunn };
155b52fa7bcSMike Dunn MODULE_DEVICE_TABLE(of, pwm_of_match);
156f409cd38SThierry Reding #else
157f409cd38SThierry Reding #define pwm_of_match NULL
158b52fa7bcSMike Dunn #endif
159b52fa7bcSMike Dunn
pwm_probe(struct platform_device * pdev)1603e9fe83dSBill Pemberton static int pwm_probe(struct platform_device *pdev)
16117b2b478SThierry Reding {
16217b2b478SThierry Reding const struct platform_device_id *id = platform_get_device_id(pdev);
163b63d60b2SUwe Kleine-König struct pxa_pwm_chip *pc;
16417b2b478SThierry Reding int ret = 0;
16517b2b478SThierry Reding
166b52fa7bcSMike Dunn if (IS_ENABLED(CONFIG_OF) && id == NULL)
167*63808bbbSUwe Kleine-König id = of_device_get_match_data(&pdev->dev);
168b52fa7bcSMike Dunn
169b52fa7bcSMike Dunn if (id == NULL)
170b52fa7bcSMike Dunn return -EINVAL;
171b52fa7bcSMike Dunn
172b63d60b2SUwe Kleine-König pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
173b63d60b2SUwe Kleine-König if (pc == NULL)
17417b2b478SThierry Reding return -ENOMEM;
17517b2b478SThierry Reding
176b63d60b2SUwe Kleine-König pc->clk = devm_clk_get(&pdev->dev, NULL);
177b63d60b2SUwe Kleine-König if (IS_ERR(pc->clk))
178b63d60b2SUwe Kleine-König return PTR_ERR(pc->clk);
17945b301d2SAxel Lin
180b63d60b2SUwe Kleine-König pc->chip.dev = &pdev->dev;
181b63d60b2SUwe Kleine-König pc->chip.ops = &pxa_pwm_ops;
182b63d60b2SUwe Kleine-König pc->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
18317b2b478SThierry Reding
184b52fa7bcSMike Dunn if (IS_ENABLED(CONFIG_OF)) {
1853ab7b6acSBjorn Andersson pc->chip.of_xlate = of_pwm_single_xlate;
186b63d60b2SUwe Kleine-König pc->chip.of_pwm_n_cells = 1;
187b52fa7bcSMike Dunn }
188b52fa7bcSMike Dunn
189b63d60b2SUwe Kleine-König pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
190b63d60b2SUwe Kleine-König if (IS_ERR(pc->mmio_base))
191b63d60b2SUwe Kleine-König return PTR_ERR(pc->mmio_base);
19217b2b478SThierry Reding
19397f29035SUwe Kleine-König ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
19417b2b478SThierry Reding if (ret < 0) {
19517b2b478SThierry Reding dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
19617b2b478SThierry Reding return ret;
19717b2b478SThierry Reding }
19817b2b478SThierry Reding
19917b2b478SThierry Reding return 0;
20017b2b478SThierry Reding }
20117b2b478SThierry Reding
20217b2b478SThierry Reding static struct platform_driver pwm_driver = {
20317b2b478SThierry Reding .driver = {
20417b2b478SThierry Reding .name = "pxa25x-pwm",
205f409cd38SThierry Reding .of_match_table = pwm_of_match,
20617b2b478SThierry Reding },
20717b2b478SThierry Reding .probe = pwm_probe,
20817b2b478SThierry Reding .id_table = pwm_id_table,
20917b2b478SThierry Reding };
21017b2b478SThierry Reding
2111e185c7aSMike Dunn module_platform_driver(pwm_driver);
21217b2b478SThierry Reding
21317b2b478SThierry Reding MODULE_LICENSE("GPL v2");
214