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> 2217b2b478SThierry Reding 2317b2b478SThierry Reding #include <asm/div64.h> 2417b2b478SThierry Reding 2517b2b478SThierry Reding #define HAS_SECONDARY_PWM 0x10 2617b2b478SThierry Reding #define PWM_ID_BASE(d) ((d) & 0xf) 2717b2b478SThierry Reding 2817b2b478SThierry Reding static const struct platform_device_id pwm_id_table[] = { 2917b2b478SThierry Reding /* PWM has_secondary_pwm? */ 3017b2b478SThierry Reding { "pxa25x-pwm", 0 }, 3117b2b478SThierry Reding { "pxa27x-pwm", 0 | HAS_SECONDARY_PWM }, 3217b2b478SThierry Reding { "pxa168-pwm", 1 }, 3317b2b478SThierry Reding { "pxa910-pwm", 1 }, 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 int clk_enabled; 5217b2b478SThierry Reding void __iomem *mmio_base; 5317b2b478SThierry Reding }; 5417b2b478SThierry Reding 5517b2b478SThierry Reding static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip) 5617b2b478SThierry Reding { 5717b2b478SThierry Reding return container_of(chip, struct pxa_pwm_chip, chip); 5817b2b478SThierry Reding } 5917b2b478SThierry Reding 6017b2b478SThierry Reding /* 6117b2b478SThierry Reding * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE 6217b2b478SThierry Reding * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE 6317b2b478SThierry Reding */ 6417b2b478SThierry Reding static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 6517b2b478SThierry Reding int duty_ns, int period_ns) 6617b2b478SThierry Reding { 6717b2b478SThierry Reding struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip); 6817b2b478SThierry Reding unsigned long long c; 6917b2b478SThierry Reding unsigned long period_cycles, prescale, pv, dc; 7017b2b478SThierry Reding unsigned long offset; 7117b2b478SThierry Reding int rc; 7217b2b478SThierry Reding 7317b2b478SThierry Reding offset = pwm->hwpwm ? 0x10 : 0; 7417b2b478SThierry Reding 7517b2b478SThierry Reding c = clk_get_rate(pc->clk); 7617b2b478SThierry Reding c = c * period_ns; 7717b2b478SThierry Reding do_div(c, 1000000000); 7817b2b478SThierry Reding period_cycles = c; 7917b2b478SThierry Reding 8017b2b478SThierry Reding if (period_cycles < 1) 8117b2b478SThierry Reding period_cycles = 1; 8217b2b478SThierry Reding prescale = (period_cycles - 1) / 1024; 8317b2b478SThierry Reding pv = period_cycles / (prescale + 1) - 1; 8417b2b478SThierry Reding 8517b2b478SThierry Reding if (prescale > 63) 8617b2b478SThierry Reding return -EINVAL; 8717b2b478SThierry Reding 8817b2b478SThierry Reding if (duty_ns == period_ns) 8917b2b478SThierry Reding dc = PWMDCR_FD; 9017b2b478SThierry Reding else 9117b2b478SThierry Reding dc = (pv + 1) * duty_ns / period_ns; 9217b2b478SThierry Reding 9317b2b478SThierry Reding /* NOTE: the clock to PWM has to be enabled first 9417b2b478SThierry Reding * before writing to the registers 9517b2b478SThierry Reding */ 9617b2b478SThierry Reding rc = clk_prepare_enable(pc->clk); 9717b2b478SThierry Reding if (rc < 0) 9817b2b478SThierry Reding return rc; 9917b2b478SThierry Reding 10017b2b478SThierry Reding writel(prescale, pc->mmio_base + offset + PWMCR); 10117b2b478SThierry Reding writel(dc, pc->mmio_base + offset + PWMDCR); 10217b2b478SThierry Reding writel(pv, pc->mmio_base + offset + PWMPCR); 10317b2b478SThierry Reding 10417b2b478SThierry Reding clk_disable_unprepare(pc->clk); 10517b2b478SThierry Reding return 0; 10617b2b478SThierry Reding } 10717b2b478SThierry Reding 10817b2b478SThierry Reding static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 10917b2b478SThierry Reding { 11017b2b478SThierry Reding struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip); 11117b2b478SThierry Reding int rc = 0; 11217b2b478SThierry Reding 11317b2b478SThierry Reding if (!pc->clk_enabled) { 11417b2b478SThierry Reding rc = clk_prepare_enable(pc->clk); 11517b2b478SThierry Reding if (!rc) 11617b2b478SThierry Reding pc->clk_enabled++; 11717b2b478SThierry Reding } 11817b2b478SThierry Reding return rc; 11917b2b478SThierry Reding } 12017b2b478SThierry Reding 12117b2b478SThierry Reding static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 12217b2b478SThierry Reding { 12317b2b478SThierry Reding struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip); 12417b2b478SThierry Reding 12517b2b478SThierry Reding if (pc->clk_enabled) { 12617b2b478SThierry Reding clk_disable_unprepare(pc->clk); 12717b2b478SThierry Reding pc->clk_enabled--; 12817b2b478SThierry Reding } 12917b2b478SThierry Reding } 13017b2b478SThierry Reding 13117b2b478SThierry Reding static struct pwm_ops pxa_pwm_ops = { 13217b2b478SThierry Reding .config = pxa_pwm_config, 13317b2b478SThierry Reding .enable = pxa_pwm_enable, 13417b2b478SThierry Reding .disable = pxa_pwm_disable, 13517b2b478SThierry Reding .owner = THIS_MODULE, 13617b2b478SThierry Reding }; 13717b2b478SThierry Reding 1383e9fe83dSBill Pemberton static int pwm_probe(struct platform_device *pdev) 13917b2b478SThierry Reding { 14017b2b478SThierry Reding const struct platform_device_id *id = platform_get_device_id(pdev); 14117b2b478SThierry Reding struct pxa_pwm_chip *pwm; 14217b2b478SThierry Reding struct resource *r; 14317b2b478SThierry Reding int ret = 0; 14417b2b478SThierry Reding 14545b301d2SAxel Lin pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); 14617b2b478SThierry Reding if (pwm == NULL) { 14717b2b478SThierry Reding dev_err(&pdev->dev, "failed to allocate memory\n"); 14817b2b478SThierry Reding return -ENOMEM; 14917b2b478SThierry Reding } 15017b2b478SThierry Reding 15145b301d2SAxel Lin pwm->clk = devm_clk_get(&pdev->dev, NULL); 15245b301d2SAxel Lin if (IS_ERR(pwm->clk)) 15345b301d2SAxel Lin return PTR_ERR(pwm->clk); 15445b301d2SAxel Lin 15517b2b478SThierry Reding pwm->clk_enabled = 0; 15617b2b478SThierry Reding 15717b2b478SThierry Reding pwm->chip.dev = &pdev->dev; 15817b2b478SThierry Reding pwm->chip.ops = &pxa_pwm_ops; 15917b2b478SThierry Reding pwm->chip.base = -1; 16017b2b478SThierry Reding pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1; 16117b2b478SThierry Reding 16217b2b478SThierry Reding r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 16317b2b478SThierry Reding if (r == NULL) { 16417b2b478SThierry Reding dev_err(&pdev->dev, "no memory resource defined\n"); 16545b301d2SAxel Lin return -ENODEV; 16617b2b478SThierry Reding } 16717b2b478SThierry Reding 168*6d4294d1SThierry Reding pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r); 169*6d4294d1SThierry Reding if (IS_ERR(pwm->mmio_base)) 170*6d4294d1SThierry Reding return PTR_ERR(pwm->mmio_base); 17117b2b478SThierry Reding 17217b2b478SThierry Reding ret = pwmchip_add(&pwm->chip); 17317b2b478SThierry Reding if (ret < 0) { 17417b2b478SThierry Reding dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); 17517b2b478SThierry Reding return ret; 17617b2b478SThierry Reding } 17717b2b478SThierry Reding 17817b2b478SThierry Reding platform_set_drvdata(pdev, pwm); 17917b2b478SThierry Reding return 0; 18017b2b478SThierry Reding } 18117b2b478SThierry Reding 18277f37917SBill Pemberton static int pwm_remove(struct platform_device *pdev) 18317b2b478SThierry Reding { 18417b2b478SThierry Reding struct pxa_pwm_chip *chip; 18517b2b478SThierry Reding 18617b2b478SThierry Reding chip = platform_get_drvdata(pdev); 18717b2b478SThierry Reding if (chip == NULL) 18817b2b478SThierry Reding return -ENODEV; 18917b2b478SThierry Reding 190abeaf755SThierry Reding return pwmchip_remove(&chip->chip); 19117b2b478SThierry Reding } 19217b2b478SThierry Reding 19317b2b478SThierry Reding static struct platform_driver pwm_driver = { 19417b2b478SThierry Reding .driver = { 19517b2b478SThierry Reding .name = "pxa25x-pwm", 19617b2b478SThierry Reding .owner = THIS_MODULE, 19717b2b478SThierry Reding }, 19817b2b478SThierry Reding .probe = pwm_probe, 199fd109112SBill Pemberton .remove = pwm_remove, 20017b2b478SThierry Reding .id_table = pwm_id_table, 20117b2b478SThierry Reding }; 20217b2b478SThierry Reding 20317b2b478SThierry Reding static int __init pwm_init(void) 20417b2b478SThierry Reding { 20517b2b478SThierry Reding return platform_driver_register(&pwm_driver); 20617b2b478SThierry Reding } 20717b2b478SThierry Reding arch_initcall(pwm_init); 20817b2b478SThierry Reding 20917b2b478SThierry Reding static void __exit pwm_exit(void) 21017b2b478SThierry Reding { 21117b2b478SThierry Reding platform_driver_unregister(&pwm_driver); 21217b2b478SThierry Reding } 21317b2b478SThierry Reding module_exit(pwm_exit); 21417b2b478SThierry Reding 21517b2b478SThierry Reding MODULE_LICENSE("GPL v2"); 216