blob: a4d2164aaf55836cc662784f1e9a6f071e6d8be5 [file] [log] [blame]
Thierry Reding17b2b472012-01-02 21:22:38 +01001/*
Axel Lin45b301d2012-07-01 18:02:14 +08002 * drivers/pwm/pwm-pxa.c
Thierry Reding17b2b472012-01-02 21:22:38 +01003 *
4 * simple driver for PWM (Pulse Width Modulator) controller
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * 2008-02-13 initial version
11 * eric miao <eric.miao@marvell.com>
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/platform_device.h>
17#include <linux/slab.h>
18#include <linux/err.h>
19#include <linux/clk.h>
20#include <linux/io.h>
21#include <linux/pwm.h>
22
23#include <asm/div64.h>
24
25#define HAS_SECONDARY_PWM 0x10
Thierry Reding17b2b472012-01-02 21:22:38 +010026
27static const struct platform_device_id pwm_id_table[] = {
28 /* PWM has_secondary_pwm? */
29 { "pxa25x-pwm", 0 },
Axel Lin22976a52013-04-01 15:41:22 +080030 { "pxa27x-pwm", HAS_SECONDARY_PWM },
31 { "pxa168-pwm", 0 },
32 { "pxa910-pwm", 0 },
Thierry Reding17b2b472012-01-02 21:22:38 +010033 { },
34};
35MODULE_DEVICE_TABLE(platform, pwm_id_table);
36
37/* PWM registers and bits definitions */
38#define PWMCR (0x00)
39#define PWMDCR (0x04)
40#define PWMPCR (0x08)
41
42#define PWMCR_SD (1 << 6)
43#define PWMDCR_FD (1 << 10)
44
45struct pxa_pwm_chip {
46 struct pwm_chip chip;
47 struct device *dev;
48
49 struct clk *clk;
Thierry Reding17b2b472012-01-02 21:22:38 +010050 void __iomem *mmio_base;
51};
52
53static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
54{
55 return container_of(chip, struct pxa_pwm_chip, chip);
56}
57
58/*
59 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
60 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
61 */
62static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
63 int duty_ns, int period_ns)
64{
65 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
66 unsigned long long c;
67 unsigned long period_cycles, prescale, pv, dc;
68 unsigned long offset;
69 int rc;
70
Thierry Reding17b2b472012-01-02 21:22:38 +010071 offset = pwm->hwpwm ? 0x10 : 0;
72
73 c = clk_get_rate(pc->clk);
74 c = c * period_ns;
75 do_div(c, 1000000000);
76 period_cycles = c;
77
78 if (period_cycles < 1)
79 period_cycles = 1;
80 prescale = (period_cycles - 1) / 1024;
81 pv = period_cycles / (prescale + 1) - 1;
82
83 if (prescale > 63)
84 return -EINVAL;
85
86 if (duty_ns == period_ns)
87 dc = PWMDCR_FD;
88 else
89 dc = (pv + 1) * duty_ns / period_ns;
90
91 /* NOTE: the clock to PWM has to be enabled first
92 * before writing to the registers
93 */
94 rc = clk_prepare_enable(pc->clk);
95 if (rc < 0)
96 return rc;
97
98 writel(prescale, pc->mmio_base + offset + PWMCR);
99 writel(dc, pc->mmio_base + offset + PWMDCR);
100 writel(pv, pc->mmio_base + offset + PWMPCR);
101
102 clk_disable_unprepare(pc->clk);
103 return 0;
104}
105
106static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
107{
108 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
Thierry Reding17b2b472012-01-02 21:22:38 +0100109
Axel Linb014a302013-03-31 23:04:31 +0800110 return clk_prepare_enable(pc->clk);
Thierry Reding17b2b472012-01-02 21:22:38 +0100111}
112
113static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
114{
115 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
116
Axel Linb014a302013-03-31 23:04:31 +0800117 clk_disable_unprepare(pc->clk);
Thierry Reding17b2b472012-01-02 21:22:38 +0100118}
119
120static struct pwm_ops pxa_pwm_ops = {
121 .config = pxa_pwm_config,
122 .enable = pxa_pwm_enable,
123 .disable = pxa_pwm_disable,
124 .owner = THIS_MODULE,
125};
126
Bill Pemberton3e9fe832012-11-19 13:23:14 -0500127static int pwm_probe(struct platform_device *pdev)
Thierry Reding17b2b472012-01-02 21:22:38 +0100128{
129 const struct platform_device_id *id = platform_get_device_id(pdev);
130 struct pxa_pwm_chip *pwm;
131 struct resource *r;
132 int ret = 0;
133
Axel Lin45b301d2012-07-01 18:02:14 +0800134 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
Thierry Reding17b2b472012-01-02 21:22:38 +0100135 if (pwm == NULL) {
136 dev_err(&pdev->dev, "failed to allocate memory\n");
137 return -ENOMEM;
138 }
139
Axel Lin45b301d2012-07-01 18:02:14 +0800140 pwm->clk = devm_clk_get(&pdev->dev, NULL);
141 if (IS_ERR(pwm->clk))
142 return PTR_ERR(pwm->clk);
143
Thierry Reding17b2b472012-01-02 21:22:38 +0100144 pwm->chip.dev = &pdev->dev;
145 pwm->chip.ops = &pxa_pwm_ops;
146 pwm->chip.base = -1;
147 pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
148
149 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding6d4294d2013-01-21 11:09:16 +0100150 pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r);
151 if (IS_ERR(pwm->mmio_base))
152 return PTR_ERR(pwm->mmio_base);
Thierry Reding17b2b472012-01-02 21:22:38 +0100153
154 ret = pwmchip_add(&pwm->chip);
155 if (ret < 0) {
156 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
157 return ret;
158 }
159
160 platform_set_drvdata(pdev, pwm);
161 return 0;
Thierry Reding17b2b472012-01-02 21:22:38 +0100162}
163
Bill Pemberton77f37912012-11-19 13:26:09 -0500164static int pwm_remove(struct platform_device *pdev)
Thierry Reding17b2b472012-01-02 21:22:38 +0100165{
166 struct pxa_pwm_chip *chip;
Thierry Reding17b2b472012-01-02 21:22:38 +0100167
168 chip = platform_get_drvdata(pdev);
169 if (chip == NULL)
170 return -ENODEV;
171
Thierry Redingabeaf752012-07-02 21:32:33 +0200172 return pwmchip_remove(&chip->chip);
Thierry Reding17b2b472012-01-02 21:22:38 +0100173}
174
175static struct platform_driver pwm_driver = {
176 .driver = {
177 .name = "pxa25x-pwm",
178 .owner = THIS_MODULE,
179 },
180 .probe = pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500181 .remove = pwm_remove,
Thierry Reding17b2b472012-01-02 21:22:38 +0100182 .id_table = pwm_id_table,
183};
184
Mike Dunn1e185c72013-08-08 10:00:37 -0700185module_platform_driver(pwm_driver);
Thierry Reding17b2b472012-01-02 21:22:38 +0100186
187MODULE_LICENSE("GPL v2");