1 From e9db29d62cbcef7974099c3a4deac69ad2a72190 Mon Sep 17 00:00:00 2001
2 From: mokopatches <mokopatches@openmoko.org>
3 Date: Fri, 4 Apr 2008 11:31:44 +0100
4 Subject: [PATCH] s3c2410-pwm.patch
5 This patch adds a PWM api abstraction for the S3C2410 SoC
7 Signed-off-by: Javi Roman <javiroman@kernel-labs.org>
8 Signed-off-by: Harald Welte <laforge@openmoko.org>
10 arch/arm/mach-s3c2410/Kconfig | 6 +
11 arch/arm/mach-s3c2410/Makefile | 1 +
12 arch/arm/mach-s3c2410/pwm.c | 214 ++++++++++++++++++++++++++++++++++++
13 include/asm-arm/arch-s3c2410/pwm.h | 40 +++++++
14 4 files changed, 261 insertions(+), 0 deletions(-)
15 create mode 100644 arch/arm/mach-s3c2410/pwm.c
16 create mode 100644 include/asm-arm/arch-s3c2410/pwm.h
18 diff --git a/arch/arm/mach-s3c2410/Kconfig b/arch/arm/mach-s3c2410/Kconfig
19 index ff339e1..e42195f 100644
20 --- a/arch/arm/mach-s3c2410/Kconfig
21 +++ b/arch/arm/mach-s3c2410/Kconfig
22 @@ -9,6 +9,7 @@ config CPU_S3C2410
23 depends on ARCH_S3C2410
27 select CPU_LLSERIAL_S3C2410
28 select S3C2410_PM if PM
30 @@ -37,6 +38,11 @@ config S3C2410_CLOCK
32 Clock code for the S3C2410, and similar processors
37 + PWM timer code for the S3C2410, and similar processors
40 menu "S3C2410 Machines"
42 diff --git a/arch/arm/mach-s3c2410/Makefile b/arch/arm/mach-s3c2410/Makefile
43 index 8952a8d..bb577f4 100644
44 --- a/arch/arm/mach-s3c2410/Makefile
45 +++ b/arch/arm/mach-s3c2410/Makefile
46 @@ -16,6 +16,7 @@ obj-$(CONFIG_CPU_S3C2410_DMA) += dma.o
47 obj-$(CONFIG_S3C2410_PM) += pm.o sleep.o
48 obj-$(CONFIG_S3C2410_GPIO) += gpio.o
49 obj-$(CONFIG_S3C2410_CLOCK) += clock.o
50 +obj-$(CONFIG_S3C2410_PWM) += pwm.o
54 diff --git a/arch/arm/mach-s3c2410/pwm.c b/arch/arm/mach-s3c2410/pwm.c
56 index 0000000..8a07359
58 +++ b/arch/arm/mach-s3c2410/pwm.c
61 + * arch/arm/mach-s3c2410/3c2410-pwm.c
63 + * Copyright (c) by Javi Roman <javiroman@kernel-labs.org>
64 + * for the OpenMoko Project.
66 + * S3C2410A SoC PWM support
68 + * This program is free software; you can redistribute it and/or modify
69 + * it under the terms of the GNU General Public License as published by
70 + * the Free Software Foundation; either version 2 of the License, or
71 + * (at your option) any later version.
73 + * You should have received a copy of the GNU General Public License
74 + * along with this program; if not, write to the Free Software
75 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
79 +#include <linux/kernel.h>
80 +#include <linux/init.h>
81 +#include <linux/clk.h>
82 +#include <asm/hardware.h>
83 +#include <asm/plat-s3c/regs-timer.h>
84 +#include <asm/arch/pwm.h>
86 +int s3c2410_pwm_disable(struct s3c2410_pwm *pwm)
91 + tcon = __raw_readl(S3C2410_TCON);
93 + __raw_writel(tcon, S3C2410_TCON);
95 + clk_disable(pwm->pclk);
100 +EXPORT_SYMBOL_GPL(s3c2410_pwm_disable);
102 +int s3c2410_pwm_init(struct s3c2410_pwm *pwm)
104 + pwm->pclk = clk_get(NULL, "timers");
105 + if (IS_ERR(pwm->pclk))
106 + return PTR_ERR(pwm->pclk);
108 + clk_enable(pwm->pclk);
109 + pwm->pclk_rate = clk_get_rate(pwm->pclk);
112 +EXPORT_SYMBOL_GPL(s3c2410_pwm_init);
114 +int s3c2410_pwm_enable(struct s3c2410_pwm *pwm)
116 + unsigned long tcfg0, tcfg1, tcnt, tcmp;
118 + /* control registers bits */
119 + tcfg1 = __raw_readl(S3C2410_TCFG1);
120 + tcfg0 = __raw_readl(S3C2410_TCFG0);
122 + /* divider & scaler slection */
123 + switch (pwm->timerid) {
125 + tcfg1 &= ~S3C2410_TCFG1_MUX0_MASK;
126 + tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
129 + tcfg1 &= ~S3C2410_TCFG1_MUX1_MASK;
130 + tcfg0 &= ~S3C2410_TCFG_PRESCALER0_MASK;
133 + tcfg1 &= ~S3C2410_TCFG1_MUX2_MASK;
134 + tcfg0 &= ~S3C2410_TCFG_PRESCALER1_MASK;
137 + tcfg1 &= ~S3C2410_TCFG1_MUX3_MASK;
138 + tcfg0 &= ~S3C2410_TCFG_PRESCALER1_MASK;
141 + /* timer four is not capable of doing PWM */
144 + clk_disable(pwm->pclk);
145 + clk_put(pwm->pclk);
149 + /* divider & scaler values */
150 + tcfg1 |= pwm->divider;
151 + __raw_writel(tcfg1, S3C2410_TCFG1);
153 + switch (pwm->timerid) {
156 + tcfg0 |= pwm->prescaler;
157 + __raw_writel(tcfg0, S3C2410_TCFG0);
160 + if ((tcfg0 | pwm->prescaler) != tcfg0) {
161 + printk(KERN_WARNING "not changing prescaler of PWM %u,"
162 + " since it's shared with timer4 (clock tick)\n",
168 + /* timer count and compare buffer initial values */
169 + tcnt = pwm->counter;
170 + tcmp = pwm->comparer;
172 + __raw_writel(tcnt, S3C2410_TCNTB(pwm->timerid));
173 + __raw_writel(tcmp, S3C2410_TCMPB(pwm->timerid));
175 + /* ensure timer is stopped */
176 + s3c2410_pwm_stop(pwm);
180 +EXPORT_SYMBOL_GPL(s3c2410_pwm_enable);
182 +int s3c2410_pwm_start(struct s3c2410_pwm *pwm)
184 + unsigned long tcon;
186 + tcon = __raw_readl(S3C2410_TCON);
188 + switch (pwm->timerid) {
190 + tcon |= S3C2410_TCON_T0START;
191 + tcon &= ~S3C2410_TCON_T0MANUALUPD;
194 + tcon |= S3C2410_TCON_T1START;
195 + tcon &= ~S3C2410_TCON_T1MANUALUPD;
198 + tcon |= S3C2410_TCON_T2START;
199 + tcon &= ~S3C2410_TCON_T2MANUALUPD;
202 + tcon |= S3C2410_TCON_T3START;
203 + tcon &= ~S3C2410_TCON_T3MANUALUPD;
206 + /* timer four is not capable of doing PWM */
211 + __raw_writel(tcon, S3C2410_TCON);
215 +EXPORT_SYMBOL_GPL(s3c2410_pwm_start);
217 +int s3c2410_pwm_stop(struct s3c2410_pwm *pwm)
219 + unsigned long tcon;
221 + tcon = __raw_readl(S3C2410_TCON);
223 + switch (pwm->timerid) {
225 + tcon &= ~0x00000000;
226 + tcon |= S3C2410_TCON_T0RELOAD;
227 + tcon |= S3C2410_TCON_T0MANUALUPD;
230 + tcon &= ~0x00000080;
231 + tcon |= S3C2410_TCON_T1RELOAD;
232 + tcon |= S3C2410_TCON_T1MANUALUPD;
235 + tcon &= ~0x00000800;
236 + tcon |= S3C2410_TCON_T2RELOAD;
237 + tcon |= S3C2410_TCON_T2MANUALUPD;
240 + tcon &= ~0x00008000;
241 + tcon |= S3C2410_TCON_T3RELOAD;
242 + tcon |= S3C2410_TCON_T3MANUALUPD;
245 + /* timer four is not capable of doing PWM */
250 + __raw_writel(tcon, S3C2410_TCON);
254 +EXPORT_SYMBOL_GPL(s3c2410_pwm_stop);
256 +int s3c2410_pwm_duty_cycle(int reg_value, struct s3c2410_pwm *pwm)
258 + __raw_writel(reg_value, S3C2410_TCMPB(pwm->timerid));
262 +EXPORT_SYMBOL_GPL(s3c2410_pwm_duty_cycle);
264 +int s3c2410_pwm_dumpregs(void)
266 + printk(KERN_INFO "TCON: %08lx, TCFG0: %08lx, TCFG1: %08lx\n",
267 + (unsigned long) __raw_readl(S3C2410_TCON),
268 + (unsigned long) __raw_readl(S3C2410_TCFG0),
269 + (unsigned long) __raw_readl(S3C2410_TCFG1));
273 +EXPORT_SYMBOL_GPL(s3c2410_pwm_dumpregs);
274 diff --git a/include/asm-arm/arch-s3c2410/pwm.h b/include/asm-arm/arch-s3c2410/pwm.h
276 index 0000000..a797ec3
278 +++ b/include/asm-arm/arch-s3c2410/pwm.h
280 +#ifndef __S3C2410_PWM_H
281 +#define __S3C2410_PWM_H
283 +#include <linux/err.h>
284 +#include <linux/platform_device.h>
285 +#include <linux/clk.h>
287 +#include <asm-arm/io.h>
288 +#include <asm/arch/hardware.h>
289 +#include <asm/mach-types.h>
290 +#include <asm/plat-s3c/regs-timer.h>
291 +#include <asm/arch/gta01.h>
301 +struct s3c2410_pwm {
302 + enum pwm_timer timerid;
304 + unsigned long pclk_rate;
305 + unsigned long prescaler;
306 + unsigned long divider;
307 + unsigned long counter;
308 + unsigned long comparer;
311 +int s3c2410_pwm_init(struct s3c2410_pwm *s3c2410_pwm);
312 +int s3c2410_pwm_enable(struct s3c2410_pwm *s3c2410_pwm);
313 +int s3c2410_pwm_disable(struct s3c2410_pwm *s3c2410_pwm);
314 +int s3c2410_pwm_start(struct s3c2410_pwm *s3c2410_pwm);
315 +int s3c2410_pwm_stop(struct s3c2410_pwm *s3c2410_pwm);
316 +int s3c2410_pwm_duty_cycle(int reg_value, struct s3c2410_pwm *s3c2410_pwm);
317 +int s3c2410_pwm_dumpregs(void);
319 +#endif /* __S3C2410_PWM_H */