117b2b478SThierry Reding /* 245b301d2SAxel Lin * drivers/pwm/pwm-pxa.c 317b2b478SThierry Reding * 417b2b478SThierry Reding * simple driver for PWM (Pulse Width Modulator) controller 517b2b478SThierry Reding * 617b2b478SThierry Reding * This program is free software; you can redistribute it and/or modify 717b2b478SThierry Reding * it under the terms of the GNU General Public License version 2 as 817b2b478SThierry Reding * published by the Free Software Foundation. 917b2b478SThierry Reding * 1017b2b478SThierry Reding * 2008-02-13 initial version 1117b2b478SThierry Reding * eric miao <eric.miao@marvell.com> 1217b2b478SThierry Reding */ 1317b2b478SThierry Reding 1417b2b478SThierry Reding #include <linux/module.h> 1517b2b478SThierry Reding #include <linux/kernel.h> 1617b2b478SThierry Reding #include <linux/platform_device.h> 1717b2b478SThierry Reding #include <linux/slab.h> 1817b2b478SThierry Reding #include <linux/err.h> 1917b2b478SThierry Reding #include <linux/clk.h> 2017b2b478SThierry Reding #include <linux/io.h> 2117b2b478SThierry Reding #include <linux/pwm.h> 22b52fa7bcSMike Dunn #include <linux/of_device.h> 2317b2b478SThierry Reding 2417b2b478SThierry Reding #include <asm/div64.h> 2517b2b478SThierry Reding 2617b2b478SThierry Reding #define HAS_SECONDARY_PWM 0x10 2717b2b478SThierry Reding 2817b2b478SThierry Reding static const struct platform_device_id pwm_id_table[] = { 2917b2b478SThierry Reding /* PWM has_secondary_pwm? */ 3017b2b478SThierry Reding { "pxa25x-pwm", 0 }, 3122976a5dSAxel Lin { "pxa27x-pwm", HAS_SECONDARY_PWM }, 3222976a5dSAxel Lin { "pxa168-pwm", 0 }, 3322976a5dSAxel Lin { "pxa910-pwm", 0 }, 3417b2b478SThierry Reding { }, 3517b2b478SThierry Reding }; 3617b2b478SThierry Reding MODULE_DEVICE_TABLE(platform, pwm_id_table); 3717b2b478SThierry Reding 3817b2b478SThierry Reding /* PWM registers and bits definitions */ 3917b2b478SThierry Reding #define PWMCR (0x00) 4017b2b478SThierry Reding #define PWMDCR (0x04) 4117b2b478SThierry Reding #define PWMPCR (0x08) 4217b2b478SThierry Reding 4317b2b478SThierry Reding #define PWMCR_SD (1 << 6) 4417b2b478SThierry Reding #define PWMDCR_FD (1 << 10) 4517b2b478SThierry Reding 4617b2b478SThierry Reding struct pxa_pwm_chip { 4717b2b478SThierry Reding struct pwm_chip chip; 4817b2b478SThierry Reding struct device *dev; 4917b2b478SThierry Reding 5017b2b478SThierry Reding struct clk *clk; 5117b2b478SThierry Reding void __iomem *mmio_base; 5217b2b478SThierry Reding }; 5317b2b478SThierry Reding 5417b2b478SThierry Reding static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip) 5517b2b478SThierry Reding { 5617b2b478SThierry Reding return container_of(chip, struct pxa_pwm_chip, chip); 5717b2b478SThierry Reding } 5817b2b478SThierry Reding 5917b2b478SThierry Reding /* 6017b2b478SThierry Reding * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE 6117b2b478SThierry Reding * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE 6217b2b478SThierry Reding */ 6317b2b478SThierry Reding static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 6417b2b478SThierry Reding int duty_ns, int period_ns) 6517b2b478SThierry Reding { 6617b2b478SThierry Reding struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip); 6717b2b478SThierry Reding unsigned long long c; 6817b2b478SThierry Reding unsigned long period_cycles, prescale, pv, dc; 6917b2b478SThierry Reding unsigned long offset; 7017b2b478SThierry Reding int rc; 7117b2b478SThierry Reding 7217b2b478SThierry Reding offset = pwm->hwpwm ? 0x10 : 0; 7317b2b478SThierry Reding 7417b2b478SThierry Reding c = clk_get_rate(pc->clk); 7517b2b478SThierry Reding c = c * period_ns; 7617b2b478SThierry Reding do_div(c, 1000000000); 7717b2b478SThierry Reding period_cycles = c; 7817b2b478SThierry Reding 7917b2b478SThierry Reding if (period_cycles < 1) 8017b2b478SThierry Reding period_cycles = 1; 8117b2b478SThierry Reding prescale = (period_cycles - 1) / 1024; 8217b2b478SThierry Reding pv = period_cycles / (prescale + 1) - 1; 8317b2b478SThierry Reding 8417b2b478SThierry Reding if (prescale > 63) 8517b2b478SThierry Reding return -EINVAL; 8617b2b478SThierry Reding 8717b2b478SThierry Reding if (duty_ns == period_ns) 8817b2b478SThierry Reding dc = PWMDCR_FD; 8917b2b478SThierry Reding else 9017b2b478SThierry Reding dc = (pv + 1) * duty_ns / period_ns; 9117b2b478SThierry Reding 9217b2b478SThierry Reding /* NOTE: the clock to PWM has to be enabled first 9317b2b478SThierry Reding * before writing to the registers 9417b2b478SThierry Reding */ 9517b2b478SThierry Reding rc = clk_prepare_enable(pc->clk); 9617b2b478SThierry Reding if (rc < 0) 9717b2b478SThierry Reding return rc; 9817b2b478SThierry Reding 9917b2b478SThierry Reding writel(prescale, pc->mmio_base + offset + PWMCR); 10017b2b478SThierry Reding writel(dc, pc->mmio_base + offset + PWMDCR); 10117b2b478SThierry Reding writel(pv, pc->mmio_base + offset + PWMPCR); 10217b2b478SThierry Reding 10317b2b478SThierry Reding clk_disable_unprepare(pc->clk); 10417b2b478SThierry Reding return 0; 10517b2b478SThierry Reding } 10617b2b478SThierry Reding 10717b2b478SThierry Reding static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 10817b2b478SThierry Reding { 10917b2b478SThierry Reding struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip); 11017b2b478SThierry Reding 111b014a30cSAxel Lin return clk_prepare_enable(pc->clk); 11217b2b478SThierry Reding } 11317b2b478SThierry Reding 11417b2b478SThierry Reding static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 11517b2b478SThierry Reding { 11617b2b478SThierry Reding struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip); 11717b2b478SThierry Reding 11817b2b478SThierry Reding clk_disable_unprepare(pc->clk); 11917b2b478SThierry Reding } 12017b2b478SThierry Reding 12117b2b478SThierry Reding static struct pwm_ops pxa_pwm_ops = { 12217b2b478SThierry Reding .config = pxa_pwm_config, 12317b2b478SThierry Reding .enable = pxa_pwm_enable, 12417b2b478SThierry Reding .disable = pxa_pwm_disable, 12517b2b478SThierry Reding .owner = THIS_MODULE, 12617b2b478SThierry Reding }; 12717b2b478SThierry Reding 128b52fa7bcSMike Dunn #ifdef CONFIG_OF 129b52fa7bcSMike Dunn /* 130*fdec4f72SThierry Reding * Device tree users must create one device instance for each PWM channel. 131b52fa7bcSMike Dunn * Hence we dispense with the HAS_SECONDARY_PWM and "tell" the original driver 132b52fa7bcSMike Dunn * code that this is a single channel pxa25x-pwm. Currently all devices are 133b52fa7bcSMike Dunn * supported identically. 134b52fa7bcSMike Dunn */ 135b52fa7bcSMike Dunn static struct of_device_id pwm_of_match[] = { 136b52fa7bcSMike Dunn { .compatible = "marvell,pxa250-pwm", .data = &pwm_id_table[0]}, 137b52fa7bcSMike Dunn { .compatible = "marvell,pxa270-pwm", .data = &pwm_id_table[0]}, 138b52fa7bcSMike Dunn { .compatible = "marvell,pxa168-pwm", .data = &pwm_id_table[0]}, 139b52fa7bcSMike Dunn { .compatible = "marvell,pxa910-pwm", .data = &pwm_id_table[0]}, 140b52fa7bcSMike Dunn { } 141b52fa7bcSMike Dunn }; 142b52fa7bcSMike Dunn MODULE_DEVICE_TABLE(of, pwm_of_match); 143f409cd38SThierry Reding #else 144f409cd38SThierry Reding #define pwm_of_match NULL 145b52fa7bcSMike Dunn #endif 146b52fa7bcSMike Dunn 147b52fa7bcSMike Dunn static const struct platform_device_id *pxa_pwm_get_id_dt(struct device *dev) 148b52fa7bcSMike Dunn { 149b52fa7bcSMike Dunn const struct of_device_id *id = of_match_device(pwm_of_match, dev); 150b52fa7bcSMike Dunn 151b52fa7bcSMike Dunn return id ? id->data : NULL; 152b52fa7bcSMike Dunn } 153b52fa7bcSMike Dunn 154b52fa7bcSMike Dunn static struct pwm_device * 155b52fa7bcSMike Dunn pxa_pwm_of_xlate(struct pwm_chip *pc, const struct of_phandle_args *args) 156b52fa7bcSMike Dunn { 157b52fa7bcSMike Dunn struct pwm_device *pwm; 158b52fa7bcSMike Dunn 159b52fa7bcSMike Dunn pwm = pwm_request_from_chip(pc, 0, NULL); 160b52fa7bcSMike Dunn if (IS_ERR(pwm)) 161b52fa7bcSMike Dunn return pwm; 162b52fa7bcSMike Dunn 163b52fa7bcSMike Dunn pwm_set_period(pwm, args->args[0]); 164b52fa7bcSMike Dunn 165b52fa7bcSMike Dunn return pwm; 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); 17117b2b478SThierry Reding struct pxa_pwm_chip *pwm; 17217b2b478SThierry Reding struct resource *r; 17317b2b478SThierry Reding int ret = 0; 17417b2b478SThierry Reding 175b52fa7bcSMike Dunn if (IS_ENABLED(CONFIG_OF) && id == NULL) 176b52fa7bcSMike Dunn id = pxa_pwm_get_id_dt(&pdev->dev); 177b52fa7bcSMike Dunn 178b52fa7bcSMike Dunn if (id == NULL) 179b52fa7bcSMike Dunn return -EINVAL; 180b52fa7bcSMike Dunn 18145b301d2SAxel Lin pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); 18217b2b478SThierry Reding if (pwm == NULL) { 18317b2b478SThierry Reding dev_err(&pdev->dev, "failed to allocate memory\n"); 18417b2b478SThierry Reding return -ENOMEM; 18517b2b478SThierry Reding } 18617b2b478SThierry Reding 18745b301d2SAxel Lin pwm->clk = devm_clk_get(&pdev->dev, NULL); 18845b301d2SAxel Lin if (IS_ERR(pwm->clk)) 18945b301d2SAxel Lin return PTR_ERR(pwm->clk); 19045b301d2SAxel Lin 19117b2b478SThierry Reding pwm->chip.dev = &pdev->dev; 19217b2b478SThierry Reding pwm->chip.ops = &pxa_pwm_ops; 19317b2b478SThierry Reding pwm->chip.base = -1; 19417b2b478SThierry Reding pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1; 19517b2b478SThierry Reding 196b52fa7bcSMike Dunn if (IS_ENABLED(CONFIG_OF)) { 197b52fa7bcSMike Dunn pwm->chip.of_xlate = pxa_pwm_of_xlate; 198b52fa7bcSMike Dunn pwm->chip.of_pwm_n_cells = 1; 199b52fa7bcSMike Dunn } 200b52fa7bcSMike Dunn 20117b2b478SThierry Reding r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2026d4294d1SThierry Reding pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r); 2036d4294d1SThierry Reding if (IS_ERR(pwm->mmio_base)) 2046d4294d1SThierry Reding return PTR_ERR(pwm->mmio_base); 20517b2b478SThierry Reding 20617b2b478SThierry Reding ret = pwmchip_add(&pwm->chip); 20717b2b478SThierry Reding if (ret < 0) { 20817b2b478SThierry Reding dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); 20917b2b478SThierry Reding return ret; 21017b2b478SThierry Reding } 21117b2b478SThierry Reding 21217b2b478SThierry Reding platform_set_drvdata(pdev, pwm); 21317b2b478SThierry Reding return 0; 21417b2b478SThierry Reding } 21517b2b478SThierry Reding 21677f37917SBill Pemberton static int pwm_remove(struct platform_device *pdev) 21717b2b478SThierry Reding { 21817b2b478SThierry Reding struct pxa_pwm_chip *chip; 21917b2b478SThierry Reding 22017b2b478SThierry Reding chip = platform_get_drvdata(pdev); 22117b2b478SThierry Reding if (chip == NULL) 22217b2b478SThierry Reding return -ENODEV; 22317b2b478SThierry Reding 224abeaf755SThierry Reding return pwmchip_remove(&chip->chip); 22517b2b478SThierry Reding } 22617b2b478SThierry Reding 22717b2b478SThierry Reding static struct platform_driver pwm_driver = { 22817b2b478SThierry Reding .driver = { 22917b2b478SThierry Reding .name = "pxa25x-pwm", 23017b2b478SThierry Reding .owner = THIS_MODULE, 231f409cd38SThierry Reding .of_match_table = pwm_of_match, 23217b2b478SThierry Reding }, 23317b2b478SThierry Reding .probe = pwm_probe, 234fd109112SBill Pemberton .remove = pwm_remove, 23517b2b478SThierry Reding .id_table = pwm_id_table, 23617b2b478SThierry Reding }; 23717b2b478SThierry Reding 2381e185c7aSMike Dunn module_platform_driver(pwm_driver); 23917b2b478SThierry Reding 24017b2b478SThierry Reding MODULE_LICENSE("GPL v2"); 241