xref: /openbmc/linux/drivers/pwm/pwm-pxa.c (revision 152f2d1def5e4b974947877126ff292a68a8c521)
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>
917b2b478SThierry Reding  */
1017b2b478SThierry Reding 
1117b2b478SThierry Reding #include <linux/module.h>
1217b2b478SThierry Reding #include <linux/kernel.h>
1317b2b478SThierry Reding #include <linux/platform_device.h>
1417b2b478SThierry Reding #include <linux/slab.h>
1517b2b478SThierry Reding #include <linux/err.h>
1617b2b478SThierry Reding #include <linux/clk.h>
1717b2b478SThierry Reding #include <linux/io.h>
1817b2b478SThierry Reding #include <linux/pwm.h>
19b52fa7bcSMike Dunn #include <linux/of_device.h>
2017b2b478SThierry Reding 
2117b2b478SThierry Reding #include <asm/div64.h>
2217b2b478SThierry Reding 
2317b2b478SThierry Reding #define HAS_SECONDARY_PWM	0x10
2417b2b478SThierry Reding 
2517b2b478SThierry Reding static const struct platform_device_id pwm_id_table[] = {
2617b2b478SThierry Reding 	/*   PWM    has_secondary_pwm? */
2717b2b478SThierry Reding 	{ "pxa25x-pwm", 0 },
2822976a5dSAxel Lin 	{ "pxa27x-pwm", HAS_SECONDARY_PWM },
2922976a5dSAxel Lin 	{ "pxa168-pwm", 0 },
3022976a5dSAxel Lin 	{ "pxa910-pwm", 0 },
3117b2b478SThierry Reding 	{ },
3217b2b478SThierry Reding };
3317b2b478SThierry Reding MODULE_DEVICE_TABLE(platform, pwm_id_table);
3417b2b478SThierry Reding 
3517b2b478SThierry Reding /* PWM registers and bits definitions */
3617b2b478SThierry Reding #define PWMCR		(0x00)
3717b2b478SThierry Reding #define PWMDCR		(0x04)
3817b2b478SThierry Reding #define PWMPCR		(0x08)
3917b2b478SThierry Reding 
4017b2b478SThierry Reding #define PWMCR_SD	(1 << 6)
4117b2b478SThierry Reding #define PWMDCR_FD	(1 << 10)
4217b2b478SThierry Reding 
4317b2b478SThierry Reding struct pxa_pwm_chip {
4417b2b478SThierry Reding 	struct pwm_chip	chip;
4517b2b478SThierry Reding 	struct device	*dev;
4617b2b478SThierry Reding 
4717b2b478SThierry Reding 	struct clk	*clk;
4817b2b478SThierry Reding 	void __iomem	*mmio_base;
4917b2b478SThierry Reding };
5017b2b478SThierry Reding 
5117b2b478SThierry Reding static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
5217b2b478SThierry Reding {
5317b2b478SThierry Reding 	return container_of(chip, struct pxa_pwm_chip, chip);
5417b2b478SThierry Reding }
5517b2b478SThierry Reding 
5617b2b478SThierry Reding /*
5717b2b478SThierry Reding  * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
5817b2b478SThierry Reding  * duty_ns   = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
5917b2b478SThierry Reding  */
6017b2b478SThierry Reding static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
61657e54e5SUwe Kleine-König 			  u64 duty_ns, u64 period_ns)
6217b2b478SThierry Reding {
6317b2b478SThierry Reding 	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
6417b2b478SThierry Reding 	unsigned long long c;
6517b2b478SThierry Reding 	unsigned long period_cycles, prescale, pv, dc;
6617b2b478SThierry Reding 	unsigned long offset;
6717b2b478SThierry Reding 	int rc;
6817b2b478SThierry Reding 
6917b2b478SThierry Reding 	offset = pwm->hwpwm ? 0x10 : 0;
7017b2b478SThierry Reding 
7117b2b478SThierry Reding 	c = clk_get_rate(pc->clk);
7217b2b478SThierry Reding 	c = c * period_ns;
7317b2b478SThierry Reding 	do_div(c, 1000000000);
7417b2b478SThierry Reding 	period_cycles = c;
7517b2b478SThierry Reding 
7617b2b478SThierry Reding 	if (period_cycles < 1)
7717b2b478SThierry Reding 		period_cycles = 1;
7817b2b478SThierry Reding 	prescale = (period_cycles - 1) / 1024;
7917b2b478SThierry Reding 	pv = period_cycles / (prescale + 1) - 1;
8017b2b478SThierry Reding 
8117b2b478SThierry Reding 	if (prescale > 63)
8217b2b478SThierry Reding 		return -EINVAL;
8317b2b478SThierry Reding 
8417b2b478SThierry Reding 	if (duty_ns == period_ns)
8517b2b478SThierry Reding 		dc = PWMDCR_FD;
8617b2b478SThierry Reding 	else
87657e54e5SUwe Kleine-König 		dc = mul_u64_u64_div_u64(pv + 1, duty_ns, period_ns);
8817b2b478SThierry Reding 
8917b2b478SThierry Reding 	/* NOTE: the clock to PWM has to be enabled first
9017b2b478SThierry Reding 	 * before writing to the registers
9117b2b478SThierry Reding 	 */
9217b2b478SThierry Reding 	rc = clk_prepare_enable(pc->clk);
9317b2b478SThierry Reding 	if (rc < 0)
9417b2b478SThierry Reding 		return rc;
9517b2b478SThierry Reding 
9617b2b478SThierry Reding 	writel(prescale, 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 	clk_disable_unprepare(pc->clk);
10117b2b478SThierry Reding 	return 0;
10217b2b478SThierry Reding }
10317b2b478SThierry Reding 
104657e54e5SUwe Kleine-König static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
105657e54e5SUwe Kleine-König 			 const struct pwm_state *state)
106657e54e5SUwe Kleine-König {
107f956b838SDoug Brown 	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
108*152f2d1dSDoug Brown 	u64 duty_cycle;
109657e54e5SUwe Kleine-König 	int err;
110657e54e5SUwe Kleine-König 
111657e54e5SUwe Kleine-König 	if (state->polarity != PWM_POLARITY_NORMAL)
112657e54e5SUwe Kleine-König 		return -EINVAL;
113657e54e5SUwe Kleine-König 
114*152f2d1dSDoug Brown 	err = clk_prepare_enable(pc->clk);
115657e54e5SUwe Kleine-König 	if (err)
116657e54e5SUwe Kleine-König 		return err;
117657e54e5SUwe Kleine-König 
118*152f2d1dSDoug Brown 	duty_cycle = state->enabled ? state->duty_cycle : 0;
119*152f2d1dSDoug Brown 
120*152f2d1dSDoug Brown 	err = pxa_pwm_config(chip, pwm, duty_cycle, state->period);
121*152f2d1dSDoug Brown 	if (err) {
122*152f2d1dSDoug Brown 		clk_disable_unprepare(pc->clk);
123*152f2d1dSDoug Brown 		return err;
124*152f2d1dSDoug Brown 	}
125*152f2d1dSDoug Brown 
126*152f2d1dSDoug Brown 	if (state->enabled && !pwm->state.enabled)
127*152f2d1dSDoug Brown 		return 0;
128*152f2d1dSDoug Brown 
129*152f2d1dSDoug Brown 	clk_disable_unprepare(pc->clk);
130*152f2d1dSDoug Brown 
131*152f2d1dSDoug Brown 	if (!state->enabled && pwm->state.enabled)
132*152f2d1dSDoug Brown 		clk_disable_unprepare(pc->clk);
133657e54e5SUwe Kleine-König 
134657e54e5SUwe Kleine-König 	return 0;
135657e54e5SUwe Kleine-König }
136657e54e5SUwe Kleine-König 
137b2ec9efcSBhumika Goyal static const struct pwm_ops pxa_pwm_ops = {
138657e54e5SUwe Kleine-König 	.apply = pxa_pwm_apply,
13917b2b478SThierry Reding 	.owner = THIS_MODULE,
14017b2b478SThierry Reding };
14117b2b478SThierry Reding 
142b52fa7bcSMike Dunn #ifdef CONFIG_OF
143b52fa7bcSMike Dunn /*
144fdec4f72SThierry Reding  * Device tree users must create one device instance for each PWM channel.
145b52fa7bcSMike Dunn  * Hence we dispense with the HAS_SECONDARY_PWM and "tell" the original driver
146b52fa7bcSMike Dunn  * code that this is a single channel pxa25x-pwm.  Currently all devices are
147b52fa7bcSMike Dunn  * supported identically.
148b52fa7bcSMike Dunn  */
1492ae69a46SThierry Reding static const struct of_device_id pwm_of_match[] = {
150b52fa7bcSMike Dunn 	{ .compatible = "marvell,pxa250-pwm", .data = &pwm_id_table[0]},
151b52fa7bcSMike Dunn 	{ .compatible = "marvell,pxa270-pwm", .data = &pwm_id_table[0]},
152b52fa7bcSMike Dunn 	{ .compatible = "marvell,pxa168-pwm", .data = &pwm_id_table[0]},
153b52fa7bcSMike Dunn 	{ .compatible = "marvell,pxa910-pwm", .data = &pwm_id_table[0]},
154b52fa7bcSMike Dunn 	{ }
155b52fa7bcSMike Dunn };
156b52fa7bcSMike Dunn MODULE_DEVICE_TABLE(of, pwm_of_match);
157f409cd38SThierry Reding #else
158f409cd38SThierry Reding #define pwm_of_match NULL
159b52fa7bcSMike Dunn #endif
160b52fa7bcSMike Dunn 
161b52fa7bcSMike Dunn static const struct platform_device_id *pxa_pwm_get_id_dt(struct device *dev)
162b52fa7bcSMike Dunn {
163b52fa7bcSMike Dunn 	const struct of_device_id *id = of_match_device(pwm_of_match, dev);
164b52fa7bcSMike Dunn 
165b52fa7bcSMike Dunn 	return id ? id->data : NULL;
166b52fa7bcSMike Dunn }
167b52fa7bcSMike Dunn 
1683e9fe83dSBill Pemberton static int pwm_probe(struct platform_device *pdev)
16917b2b478SThierry Reding {
17017b2b478SThierry Reding 	const struct platform_device_id *id = platform_get_device_id(pdev);
171b63d60b2SUwe Kleine-König 	struct pxa_pwm_chip *pc;
17217b2b478SThierry Reding 	int ret = 0;
17317b2b478SThierry Reding 
174b52fa7bcSMike Dunn 	if (IS_ENABLED(CONFIG_OF) && id == NULL)
175b52fa7bcSMike Dunn 		id = pxa_pwm_get_id_dt(&pdev->dev);
176b52fa7bcSMike Dunn 
177b52fa7bcSMike Dunn 	if (id == NULL)
178b52fa7bcSMike Dunn 		return -EINVAL;
179b52fa7bcSMike Dunn 
180b63d60b2SUwe Kleine-König 	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
181b63d60b2SUwe Kleine-König 	if (pc == NULL)
18217b2b478SThierry Reding 		return -ENOMEM;
18317b2b478SThierry Reding 
184b63d60b2SUwe Kleine-König 	pc->clk = devm_clk_get(&pdev->dev, NULL);
185b63d60b2SUwe Kleine-König 	if (IS_ERR(pc->clk))
186b63d60b2SUwe Kleine-König 		return PTR_ERR(pc->clk);
18745b301d2SAxel Lin 
188b63d60b2SUwe Kleine-König 	pc->chip.dev = &pdev->dev;
189b63d60b2SUwe Kleine-König 	pc->chip.ops = &pxa_pwm_ops;
190b63d60b2SUwe Kleine-König 	pc->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
19117b2b478SThierry Reding 
192b52fa7bcSMike Dunn 	if (IS_ENABLED(CONFIG_OF)) {
1933ab7b6acSBjorn Andersson 		pc->chip.of_xlate = of_pwm_single_xlate;
194b63d60b2SUwe Kleine-König 		pc->chip.of_pwm_n_cells = 1;
195b52fa7bcSMike Dunn 	}
196b52fa7bcSMike Dunn 
197b63d60b2SUwe Kleine-König 	pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
198b63d60b2SUwe Kleine-König 	if (IS_ERR(pc->mmio_base))
199b63d60b2SUwe Kleine-König 		return PTR_ERR(pc->mmio_base);
20017b2b478SThierry Reding 
20197f29035SUwe Kleine-König 	ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
20217b2b478SThierry Reding 	if (ret < 0) {
20317b2b478SThierry Reding 		dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
20417b2b478SThierry Reding 		return ret;
20517b2b478SThierry Reding 	}
20617b2b478SThierry Reding 
20717b2b478SThierry Reding 	return 0;
20817b2b478SThierry Reding }
20917b2b478SThierry Reding 
21017b2b478SThierry Reding static struct platform_driver pwm_driver = {
21117b2b478SThierry Reding 	.driver		= {
21217b2b478SThierry Reding 		.name	= "pxa25x-pwm",
213f409cd38SThierry Reding 		.of_match_table = pwm_of_match,
21417b2b478SThierry Reding 	},
21517b2b478SThierry Reding 	.probe		= pwm_probe,
21617b2b478SThierry Reding 	.id_table	= pwm_id_table,
21717b2b478SThierry Reding };
21817b2b478SThierry Reding 
2191e185c7aSMike Dunn module_platform_driver(pwm_driver);
22017b2b478SThierry Reding 
22117b2b478SThierry Reding MODULE_LICENSE("GPL v2");
222