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 18*0a41b0c5SRob 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 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 */ 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 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 160b52fa7bcSMike Dunn static const struct platform_device_id *pxa_pwm_get_id_dt(struct device *dev) 161b52fa7bcSMike Dunn { 162b52fa7bcSMike Dunn const struct of_device_id *id = of_match_device(pwm_of_match, dev); 163b52fa7bcSMike Dunn 164b52fa7bcSMike Dunn return id ? id->data : NULL; 165b52fa7bcSMike Dunn } 166b52fa7bcSMike Dunn 1673e9fe83dSBill Pemberton static int pwm_probe(struct platform_device *pdev) 16817b2b478SThierry Reding { 16917b2b478SThierry Reding const struct platform_device_id *id = platform_get_device_id(pdev); 170b63d60b2SUwe Kleine-König struct pxa_pwm_chip *pc; 17117b2b478SThierry Reding int ret = 0; 17217b2b478SThierry Reding 173b52fa7bcSMike Dunn if (IS_ENABLED(CONFIG_OF) && id == NULL) 174b52fa7bcSMike Dunn id = pxa_pwm_get_id_dt(&pdev->dev); 175b52fa7bcSMike Dunn 176b52fa7bcSMike Dunn if (id == NULL) 177b52fa7bcSMike Dunn return -EINVAL; 178b52fa7bcSMike Dunn 179b63d60b2SUwe Kleine-König pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); 180b63d60b2SUwe Kleine-König if (pc == NULL) 18117b2b478SThierry Reding return -ENOMEM; 18217b2b478SThierry Reding 183b63d60b2SUwe Kleine-König pc->clk = devm_clk_get(&pdev->dev, NULL); 184b63d60b2SUwe Kleine-König if (IS_ERR(pc->clk)) 185b63d60b2SUwe Kleine-König return PTR_ERR(pc->clk); 18645b301d2SAxel Lin 187b63d60b2SUwe Kleine-König pc->chip.dev = &pdev->dev; 188b63d60b2SUwe Kleine-König pc->chip.ops = &pxa_pwm_ops; 189b63d60b2SUwe Kleine-König pc->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1; 19017b2b478SThierry Reding 191b52fa7bcSMike Dunn if (IS_ENABLED(CONFIG_OF)) { 1923ab7b6acSBjorn Andersson pc->chip.of_xlate = of_pwm_single_xlate; 193b63d60b2SUwe Kleine-König pc->chip.of_pwm_n_cells = 1; 194b52fa7bcSMike Dunn } 195b52fa7bcSMike Dunn 196b63d60b2SUwe Kleine-König pc->mmio_base = devm_platform_ioremap_resource(pdev, 0); 197b63d60b2SUwe Kleine-König if (IS_ERR(pc->mmio_base)) 198b63d60b2SUwe Kleine-König return PTR_ERR(pc->mmio_base); 19917b2b478SThierry Reding 20097f29035SUwe Kleine-König ret = devm_pwmchip_add(&pdev->dev, &pc->chip); 20117b2b478SThierry Reding if (ret < 0) { 20217b2b478SThierry Reding dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); 20317b2b478SThierry Reding return ret; 20417b2b478SThierry Reding } 20517b2b478SThierry Reding 20617b2b478SThierry Reding return 0; 20717b2b478SThierry Reding } 20817b2b478SThierry Reding 20917b2b478SThierry Reding static struct platform_driver pwm_driver = { 21017b2b478SThierry Reding .driver = { 21117b2b478SThierry Reding .name = "pxa25x-pwm", 212f409cd38SThierry Reding .of_match_table = pwm_of_match, 21317b2b478SThierry Reding }, 21417b2b478SThierry Reding .probe = pwm_probe, 21517b2b478SThierry Reding .id_table = pwm_id_table, 21617b2b478SThierry Reding }; 21717b2b478SThierry Reding 2181e185c7aSMike Dunn module_platform_driver(pwm_driver); 21917b2b478SThierry Reding 22017b2b478SThierry Reding MODULE_LICENSE("GPL v2"); 221