xref: /openbmc/linux/drivers/pwm/pwm-pxa.c (revision 3ab7b6ac5d829e60c3b89d415811ff1c9f358c8e)
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,
6117b2b478SThierry Reding 			  int duty_ns, int 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
8717b2b478SThierry Reding 		dc = (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 
10417b2b478SThierry Reding static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
10517b2b478SThierry Reding {
10617b2b478SThierry Reding 	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
10717b2b478SThierry Reding 
108b014a30cSAxel Lin 	return clk_prepare_enable(pc->clk);
10917b2b478SThierry Reding }
11017b2b478SThierry Reding 
11117b2b478SThierry Reding static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
11217b2b478SThierry Reding {
11317b2b478SThierry Reding 	struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
11417b2b478SThierry Reding 
11517b2b478SThierry Reding 	clk_disable_unprepare(pc->clk);
11617b2b478SThierry Reding }
11717b2b478SThierry Reding 
118b2ec9efcSBhumika Goyal static const struct pwm_ops pxa_pwm_ops = {
11917b2b478SThierry Reding 	.config = pxa_pwm_config,
12017b2b478SThierry Reding 	.enable = pxa_pwm_enable,
12117b2b478SThierry Reding 	.disable = pxa_pwm_disable,
12217b2b478SThierry Reding 	.owner = THIS_MODULE,
12317b2b478SThierry Reding };
12417b2b478SThierry Reding 
125b52fa7bcSMike Dunn #ifdef CONFIG_OF
126b52fa7bcSMike Dunn /*
127fdec4f72SThierry Reding  * Device tree users must create one device instance for each PWM channel.
128b52fa7bcSMike Dunn  * Hence we dispense with the HAS_SECONDARY_PWM and "tell" the original driver
129b52fa7bcSMike Dunn  * code that this is a single channel pxa25x-pwm.  Currently all devices are
130b52fa7bcSMike Dunn  * supported identically.
131b52fa7bcSMike Dunn  */
1322ae69a46SThierry Reding static const struct of_device_id pwm_of_match[] = {
133b52fa7bcSMike Dunn 	{ .compatible = "marvell,pxa250-pwm", .data = &pwm_id_table[0]},
134b52fa7bcSMike Dunn 	{ .compatible = "marvell,pxa270-pwm", .data = &pwm_id_table[0]},
135b52fa7bcSMike Dunn 	{ .compatible = "marvell,pxa168-pwm", .data = &pwm_id_table[0]},
136b52fa7bcSMike Dunn 	{ .compatible = "marvell,pxa910-pwm", .data = &pwm_id_table[0]},
137b52fa7bcSMike Dunn 	{ }
138b52fa7bcSMike Dunn };
139b52fa7bcSMike Dunn MODULE_DEVICE_TABLE(of, pwm_of_match);
140f409cd38SThierry Reding #else
141f409cd38SThierry Reding #define pwm_of_match NULL
142b52fa7bcSMike Dunn #endif
143b52fa7bcSMike Dunn 
144b52fa7bcSMike Dunn static const struct platform_device_id *pxa_pwm_get_id_dt(struct device *dev)
145b52fa7bcSMike Dunn {
146b52fa7bcSMike Dunn 	const struct of_device_id *id = of_match_device(pwm_of_match, dev);
147b52fa7bcSMike Dunn 
148b52fa7bcSMike Dunn 	return id ? id->data : NULL;
149b52fa7bcSMike Dunn }
150b52fa7bcSMike Dunn 
1513e9fe83dSBill Pemberton static int pwm_probe(struct platform_device *pdev)
15217b2b478SThierry Reding {
15317b2b478SThierry Reding 	const struct platform_device_id *id = platform_get_device_id(pdev);
154b63d60b2SUwe Kleine-König 	struct pxa_pwm_chip *pc;
15517b2b478SThierry Reding 	int ret = 0;
15617b2b478SThierry Reding 
157b52fa7bcSMike Dunn 	if (IS_ENABLED(CONFIG_OF) && id == NULL)
158b52fa7bcSMike Dunn 		id = pxa_pwm_get_id_dt(&pdev->dev);
159b52fa7bcSMike Dunn 
160b52fa7bcSMike Dunn 	if (id == NULL)
161b52fa7bcSMike Dunn 		return -EINVAL;
162b52fa7bcSMike Dunn 
163b63d60b2SUwe Kleine-König 	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
164b63d60b2SUwe Kleine-König 	if (pc == NULL)
16517b2b478SThierry Reding 		return -ENOMEM;
16617b2b478SThierry Reding 
167b63d60b2SUwe Kleine-König 	pc->clk = devm_clk_get(&pdev->dev, NULL);
168b63d60b2SUwe Kleine-König 	if (IS_ERR(pc->clk))
169b63d60b2SUwe Kleine-König 		return PTR_ERR(pc->clk);
17045b301d2SAxel Lin 
171b63d60b2SUwe Kleine-König 	pc->chip.dev = &pdev->dev;
172b63d60b2SUwe Kleine-König 	pc->chip.ops = &pxa_pwm_ops;
173b63d60b2SUwe Kleine-König 	pc->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
17417b2b478SThierry Reding 
175b52fa7bcSMike Dunn 	if (IS_ENABLED(CONFIG_OF)) {
176*3ab7b6acSBjorn Andersson 		pc->chip.of_xlate = of_pwm_single_xlate;
177b63d60b2SUwe Kleine-König 		pc->chip.of_pwm_n_cells = 1;
178b52fa7bcSMike Dunn 	}
179b52fa7bcSMike Dunn 
180b63d60b2SUwe Kleine-König 	pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
181b63d60b2SUwe Kleine-König 	if (IS_ERR(pc->mmio_base))
182b63d60b2SUwe Kleine-König 		return PTR_ERR(pc->mmio_base);
18317b2b478SThierry Reding 
18497f29035SUwe Kleine-König 	ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
18517b2b478SThierry Reding 	if (ret < 0) {
18617b2b478SThierry Reding 		dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
18717b2b478SThierry Reding 		return ret;
18817b2b478SThierry Reding 	}
18917b2b478SThierry Reding 
19017b2b478SThierry Reding 	return 0;
19117b2b478SThierry Reding }
19217b2b478SThierry Reding 
19317b2b478SThierry Reding static struct platform_driver pwm_driver = {
19417b2b478SThierry Reding 	.driver		= {
19517b2b478SThierry Reding 		.name	= "pxa25x-pwm",
196f409cd38SThierry Reding 		.of_match_table = pwm_of_match,
19717b2b478SThierry Reding 	},
19817b2b478SThierry Reding 	.probe		= pwm_probe,
19917b2b478SThierry Reding 	.id_table	= pwm_id_table,
20017b2b478SThierry Reding };
20117b2b478SThierry Reding 
2021e185c7aSMike Dunn module_platform_driver(pwm_driver);
20317b2b478SThierry Reding 
20417b2b478SThierry Reding MODULE_LICENSE("GPL v2");
205