blob: b78988255aeeb3538105515a684837bdba4f9b32 [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
26#define PWM_ID_BASE(d) ((d) & 0xf)
27
28static const struct platform_device_id pwm_id_table[] = {
29 /* PWM has_secondary_pwm? */
30 { "pxa25x-pwm", 0 },
31 { "pxa27x-pwm", 0 | HAS_SECONDARY_PWM },
32 { "pxa168-pwm", 1 },
33 { "pxa910-pwm", 1 },
34 { },
35};
36MODULE_DEVICE_TABLE(platform, pwm_id_table);
37
38/* PWM registers and bits definitions */
39#define PWMCR (0x00)
40#define PWMDCR (0x04)
41#define PWMPCR (0x08)
42
43#define PWMCR_SD (1 << 6)
44#define PWMDCR_FD (1 << 10)
45
46struct pxa_pwm_chip {
47 struct pwm_chip chip;
48 struct device *dev;
49
50 struct clk *clk;
Thierry Reding17b2b472012-01-02 21:22:38 +010051 void __iomem *mmio_base;
52};
53
54static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
55{
56 return container_of(chip, struct pxa_pwm_chip, chip);
57}
58
59/*
60 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
61 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
62 */
63static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
64 int duty_ns, int period_ns)
65{
66 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
67 unsigned long long c;
68 unsigned long period_cycles, prescale, pv, dc;
69 unsigned long offset;
70 int rc;
71
Thierry Reding17b2b472012-01-02 21:22:38 +010072 offset = pwm->hwpwm ? 0x10 : 0;
73
74 c = clk_get_rate(pc->clk);
75 c = c * period_ns;
76 do_div(c, 1000000000);
77 period_cycles = c;
78
79 if (period_cycles < 1)
80 period_cycles = 1;
81 prescale = (period_cycles - 1) / 1024;
82 pv = period_cycles / (prescale + 1) - 1;
83
84 if (prescale > 63)
85 return -EINVAL;
86
87 if (duty_ns == period_ns)
88 dc = PWMDCR_FD;
89 else
90 dc = (pv + 1) * duty_ns / period_ns;
91
92 /* NOTE: the clock to PWM has to be enabled first
93 * before writing to the registers
94 */
95 rc = clk_prepare_enable(pc->clk);
96 if (rc < 0)
97 return rc;
98
99 writel(prescale, pc->mmio_base + offset + PWMCR);
100 writel(dc, pc->mmio_base + offset + PWMDCR);
101 writel(pv, pc->mmio_base + offset + PWMPCR);
102
103 clk_disable_unprepare(pc->clk);
104 return 0;
105}
106
107static int pxa_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
108{
109 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
Thierry Reding17b2b472012-01-02 21:22:38 +0100110
Axel Linb014a302013-03-31 23:04:31 +0800111 return clk_prepare_enable(pc->clk);
Thierry Reding17b2b472012-01-02 21:22:38 +0100112}
113
114static void pxa_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
115{
116 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
117
Axel Linb014a302013-03-31 23:04:31 +0800118 clk_disable_unprepare(pc->clk);
Thierry Reding17b2b472012-01-02 21:22:38 +0100119}
120
121static struct pwm_ops pxa_pwm_ops = {
122 .config = pxa_pwm_config,
123 .enable = pxa_pwm_enable,
124 .disable = pxa_pwm_disable,
125 .owner = THIS_MODULE,
126};
127
Bill Pemberton3e9fe832012-11-19 13:23:14 -0500128static int pwm_probe(struct platform_device *pdev)
Thierry Reding17b2b472012-01-02 21:22:38 +0100129{
130 const struct platform_device_id *id = platform_get_device_id(pdev);
131 struct pxa_pwm_chip *pwm;
132 struct resource *r;
133 int ret = 0;
134
Axel Lin45b301d2012-07-01 18:02:14 +0800135 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
Thierry Reding17b2b472012-01-02 21:22:38 +0100136 if (pwm == NULL) {
137 dev_err(&pdev->dev, "failed to allocate memory\n");
138 return -ENOMEM;
139 }
140
Axel Lin45b301d2012-07-01 18:02:14 +0800141 pwm->clk = devm_clk_get(&pdev->dev, NULL);
142 if (IS_ERR(pwm->clk))
143 return PTR_ERR(pwm->clk);
144
Thierry Reding17b2b472012-01-02 21:22:38 +0100145 pwm->chip.dev = &pdev->dev;
146 pwm->chip.ops = &pxa_pwm_ops;
147 pwm->chip.base = -1;
148 pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1;
149
150 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
151 if (r == NULL) {
152 dev_err(&pdev->dev, "no memory resource defined\n");
Axel Lin45b301d2012-07-01 18:02:14 +0800153 return -ENODEV;
Thierry Reding17b2b472012-01-02 21:22:38 +0100154 }
155
Thierry Reding6d4294d2013-01-21 11:09:16 +0100156 pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r);
157 if (IS_ERR(pwm->mmio_base))
158 return PTR_ERR(pwm->mmio_base);
Thierry Reding17b2b472012-01-02 21:22:38 +0100159
160 ret = pwmchip_add(&pwm->chip);
161 if (ret < 0) {
162 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
163 return ret;
164 }
165
166 platform_set_drvdata(pdev, pwm);
167 return 0;
Thierry Reding17b2b472012-01-02 21:22:38 +0100168}
169
Bill Pemberton77f37912012-11-19 13:26:09 -0500170static int pwm_remove(struct platform_device *pdev)
Thierry Reding17b2b472012-01-02 21:22:38 +0100171{
172 struct pxa_pwm_chip *chip;
Thierry Reding17b2b472012-01-02 21:22:38 +0100173
174 chip = platform_get_drvdata(pdev);
175 if (chip == NULL)
176 return -ENODEV;
177
Thierry Redingabeaf752012-07-02 21:32:33 +0200178 return pwmchip_remove(&chip->chip);
Thierry Reding17b2b472012-01-02 21:22:38 +0100179}
180
181static struct platform_driver pwm_driver = {
182 .driver = {
183 .name = "pxa25x-pwm",
184 .owner = THIS_MODULE,
185 },
186 .probe = pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500187 .remove = pwm_remove,
Thierry Reding17b2b472012-01-02 21:22:38 +0100188 .id_table = pwm_id_table,
189};
190
191static int __init pwm_init(void)
192{
193 return platform_driver_register(&pwm_driver);
194}
195arch_initcall(pwm_init);
196
197static void __exit pwm_exit(void)
198{
199 platform_driver_unregister(&pwm_driver);
200}
201module_exit(pwm_exit);
202
203MODULE_LICENSE("GPL v2");