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 if (period_ns == 0 || duty_ns > period_ns) 7417b2b478SThierry Reding return -EINVAL; 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 9417b2b478SThierry Reding dc = (pv + 1) * duty_ns / period_ns; 9517b2b478SThierry Reding 9617b2b478SThierry Reding /* NOTE: the clock to PWM has to be enabled first 9717b2b478SThierry Reding * before writing to the registers 9817b2b478SThierry Reding */ 9917b2b478SThierry Reding rc = clk_prepare_enable(pc->clk); 10017b2b478SThierry Reding if (rc < 0) 10117b2b478SThierry Reding return rc; 10217b2b478SThierry Reding 10317b2b478SThierry Reding writel(prescale, pc->mmio_base + offset + PWMCR); 10417b2b478SThierry Reding writel(dc, pc->mmio_base + offset + PWMDCR); 10517b2b478SThierry Reding writel(pv, pc->mmio_base + offset + PWMPCR); 10617b2b478SThierry Reding 10717b2b478SThierry Reding clk_disable_unprepare(pc->clk); 10817b2b478SThierry Reding return 0; 10917b2b478SThierry Reding } 11017b2b478SThierry Reding 11117b2b478SThierry Reding static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 11217b2b478SThierry Reding { 11317b2b478SThierry Reding struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip); 11417b2b478SThierry Reding int rc = 0; 11517b2b478SThierry Reding 11617b2b478SThierry Reding if (!pc->clk_enabled) { 11717b2b478SThierry Reding rc = clk_prepare_enable(pc->clk); 11817b2b478SThierry Reding if (!rc) 11917b2b478SThierry Reding pc->clk_enabled++; 12017b2b478SThierry Reding } 12117b2b478SThierry Reding return rc; 12217b2b478SThierry Reding } 12317b2b478SThierry Reding 12417b2b478SThierry Reding static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 12517b2b478SThierry Reding { 12617b2b478SThierry Reding struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip); 12717b2b478SThierry Reding 12817b2b478SThierry Reding if (pc->clk_enabled) { 12917b2b478SThierry Reding clk_disable_unprepare(pc->clk); 13017b2b478SThierry Reding pc->clk_enabled--; 13117b2b478SThierry Reding } 13217b2b478SThierry Reding } 13317b2b478SThierry Reding 13417b2b478SThierry Reding static struct pwm_ops pxa_pwm_ops = { 13517b2b478SThierry Reding .config = pxa_pwm_config, 13617b2b478SThierry Reding .enable = pxa_pwm_enable, 13717b2b478SThierry Reding .disable = pxa_pwm_disable, 13817b2b478SThierry Reding .owner = THIS_MODULE, 13917b2b478SThierry Reding }; 14017b2b478SThierry Reding 14117b2b478SThierry Reding static int __devinit pwm_probe(struct platform_device *pdev) 14217b2b478SThierry Reding { 14317b2b478SThierry Reding const struct platform_device_id *id = platform_get_device_id(pdev); 14417b2b478SThierry Reding struct pxa_pwm_chip *pwm; 14517b2b478SThierry Reding struct resource *r; 14617b2b478SThierry Reding int ret = 0; 14717b2b478SThierry Reding 14845b301d2SAxel Lin pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); 14917b2b478SThierry Reding if (pwm == NULL) { 15017b2b478SThierry Reding dev_err(&pdev->dev, "failed to allocate memory\n"); 15117b2b478SThierry Reding return -ENOMEM; 15217b2b478SThierry Reding } 15317b2b478SThierry Reding 15445b301d2SAxel Lin pwm->clk = devm_clk_get(&pdev->dev, NULL); 15545b301d2SAxel Lin if (IS_ERR(pwm->clk)) 15645b301d2SAxel Lin return PTR_ERR(pwm->clk); 15745b301d2SAxel Lin 15817b2b478SThierry Reding pwm->clk_enabled = 0; 15917b2b478SThierry Reding 16017b2b478SThierry Reding pwm->chip.dev = &pdev->dev; 16117b2b478SThierry Reding pwm->chip.ops = &pxa_pwm_ops; 16217b2b478SThierry Reding pwm->chip.base = -1; 16317b2b478SThierry Reding pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1; 16417b2b478SThierry Reding 16517b2b478SThierry Reding r = platform_get_resource(pdev, IORESOURCE_MEM, 0); 16617b2b478SThierry Reding if (r == NULL) { 16717b2b478SThierry Reding dev_err(&pdev->dev, "no memory resource defined\n"); 16845b301d2SAxel Lin return -ENODEV; 16917b2b478SThierry Reding } 17017b2b478SThierry Reding 17145b301d2SAxel Lin pwm->mmio_base = devm_request_and_ioremap(&pdev->dev, r); 17245b301d2SAxel Lin if (pwm->mmio_base == NULL) 17345b301d2SAxel Lin return -EADDRNOTAVAIL; 17417b2b478SThierry Reding 17517b2b478SThierry Reding ret = pwmchip_add(&pwm->chip); 17617b2b478SThierry Reding if (ret < 0) { 17717b2b478SThierry Reding dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); 17817b2b478SThierry Reding return ret; 17917b2b478SThierry Reding } 18017b2b478SThierry Reding 18117b2b478SThierry Reding platform_set_drvdata(pdev, pwm); 18217b2b478SThierry Reding return 0; 18317b2b478SThierry Reding } 18417b2b478SThierry Reding 18517b2b478SThierry Reding static int __devexit pwm_remove(struct platform_device *pdev) 18617b2b478SThierry Reding { 18717b2b478SThierry Reding struct pxa_pwm_chip *chip; 18817b2b478SThierry Reding 18917b2b478SThierry Reding chip = platform_get_drvdata(pdev); 19017b2b478SThierry Reding if (chip == NULL) 19117b2b478SThierry Reding return -ENODEV; 19217b2b478SThierry Reding 193*abeaf755SThierry Reding return pwmchip_remove(&chip->chip); 19417b2b478SThierry Reding } 19517b2b478SThierry Reding 19617b2b478SThierry Reding static struct platform_driver pwm_driver = { 19717b2b478SThierry Reding .driver = { 19817b2b478SThierry Reding .name = "pxa25x-pwm", 19917b2b478SThierry Reding .owner = THIS_MODULE, 20017b2b478SThierry Reding }, 20117b2b478SThierry Reding .probe = pwm_probe, 20217b2b478SThierry Reding .remove = __devexit_p(pwm_remove), 20317b2b478SThierry Reding .id_table = pwm_id_table, 20417b2b478SThierry Reding }; 20517b2b478SThierry Reding 20617b2b478SThierry Reding static int __init pwm_init(void) 20717b2b478SThierry Reding { 20817b2b478SThierry Reding return platform_driver_register(&pwm_driver); 20917b2b478SThierry Reding } 21017b2b478SThierry Reding arch_initcall(pwm_init); 21117b2b478SThierry Reding 21217b2b478SThierry Reding static void __exit pwm_exit(void) 21317b2b478SThierry Reding { 21417b2b478SThierry Reding platform_driver_unregister(&pwm_driver); 21517b2b478SThierry Reding } 21617b2b478SThierry Reding module_exit(pwm_exit); 21717b2b478SThierry Reding 21817b2b478SThierry Reding MODULE_LICENSE("GPL v2"); 219