blob: 5d426ffe6a3174dfd8733d1d6134930030e731f0 [file] [log] [blame]
Sascha Hauer29693242012-03-15 10:04:35 +01001/*
2 * simple driver for PWM (Pulse Width Modulator) controller
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
9 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/platform_device.h>
14#include <linux/slab.h>
15#include <linux/err.h>
16#include <linux/clk.h>
17#include <linux/io.h>
18#include <linux/pwm.h>
19#include <mach/hardware.h>
20
21
22/* i.MX1 and i.MX21 share the same PWM function block: */
23
24#define MX1_PWMC 0x00 /* PWM Control Register */
25#define MX1_PWMS 0x04 /* PWM Sample Register */
26#define MX1_PWMP 0x08 /* PWM Period Register */
27
28
29/* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
30
31#define MX3_PWMCR 0x00 /* PWM Control Register */
32#define MX3_PWMSAR 0x0C /* PWM Sample Register */
33#define MX3_PWMPR 0x10 /* PWM Period Register */
34#define MX3_PWMCR_PRESCALER(x) (((x - 1) & 0xFFF) << 4)
35#define MX3_PWMCR_DOZEEN (1 << 24)
36#define MX3_PWMCR_WAITEN (1 << 23)
37#define MX3_PWMCR_DBGEN (1 << 22)
38#define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16)
39#define MX3_PWMCR_CLKSRC_IPG (1 << 16)
40#define MX3_PWMCR_EN (1 << 0)
41
42struct imx_chip {
43 struct clk *clk;
44
Sascha Hauer140827c2012-08-28 09:12:01 +020045 int enabled;
Sascha Hauer29693242012-03-15 10:04:35 +010046 void __iomem *mmio_base;
47
48 struct pwm_chip chip;
Sascha Hauer19e73332012-07-03 17:28:14 +020049
50 int (*config)(struct pwm_chip *chip,
51 struct pwm_device *pwm, int duty_ns, int period_ns);
Sascha Hauer29693242012-03-15 10:04:35 +010052};
53
54#define to_imx_chip(chip) container_of(chip, struct imx_chip, chip)
55
Sascha Hauer19e73332012-07-03 17:28:14 +020056static int imx_pwm_config_v1(struct pwm_chip *chip,
57 struct pwm_device *pwm, int duty_ns, int period_ns)
58{
59 struct imx_chip *imx = to_imx_chip(chip);
60
61 /*
62 * The PWM subsystem allows for exact frequencies. However,
63 * I cannot connect a scope on my device to the PWM line and
64 * thus cannot provide the program the PWM controller
65 * exactly. Instead, I'm relying on the fact that the
66 * Bootloader (u-boot or WinCE+haret) has programmed the PWM
67 * function group already. So I'll just modify the PWM sample
68 * register to follow the ratio of duty_ns vs. period_ns
69 * accordingly.
70 *
71 * This is good enough for programming the brightness of
72 * the LCD backlight.
73 *
74 * The real implementation would divide PERCLK[0] first by
75 * both the prescaler (/1 .. /128) and then by CLKSEL
76 * (/2 .. /16).
77 */
78 u32 max = readl(imx->mmio_base + MX1_PWMP);
79 u32 p = max * duty_ns / period_ns;
80 writel(max - p, imx->mmio_base + MX1_PWMS);
81
82 return 0;
83}
84
85static int imx_pwm_config_v2(struct pwm_chip *chip,
86 struct pwm_device *pwm, int duty_ns, int period_ns)
87{
88 struct imx_chip *imx = to_imx_chip(chip);
89 unsigned long long c;
90 unsigned long period_cycles, duty_cycles, prescale;
91 u32 cr;
92
93 c = clk_get_rate(imx->clk);
94 c = c * period_ns;
95 do_div(c, 1000000000);
96 period_cycles = c;
97
98 prescale = period_cycles / 0x10000 + 1;
99
100 period_cycles /= prescale;
101 c = (unsigned long long)period_cycles * duty_ns;
102 do_div(c, period_ns);
103 duty_cycles = c;
104
105 /*
106 * according to imx pwm RM, the real period value should be
107 * PERIOD value in PWMPR plus 2.
108 */
109 if (period_cycles > 2)
110 period_cycles -= 2;
111 else
112 period_cycles = 0;
113
114 writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
115 writel(period_cycles, imx->mmio_base + MX3_PWMPR);
116
117 cr = MX3_PWMCR_PRESCALER(prescale) |
118 MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
119 MX3_PWMCR_DBGEN | MX3_PWMCR_EN;
120
121 if (cpu_is_mx25())
122 cr |= MX3_PWMCR_CLKSRC_IPG;
123 else
124 cr |= MX3_PWMCR_CLKSRC_IPG_HIGH;
125
126 writel(cr, imx->mmio_base + MX3_PWMCR);
127
128 return 0;
129}
130
Sascha Hauer29693242012-03-15 10:04:35 +0100131static int imx_pwm_config(struct pwm_chip *chip,
132 struct pwm_device *pwm, int duty_ns, int period_ns)
133{
134 struct imx_chip *imx = to_imx_chip(chip);
135
Sascha Hauer19e73332012-07-03 17:28:14 +0200136 return imx->config(chip, pwm, duty_ns, period_ns);
Sascha Hauer29693242012-03-15 10:04:35 +0100137}
138
139static int imx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
140{
141 struct imx_chip *imx = to_imx_chip(chip);
Sascha Hauer140827c2012-08-28 09:12:01 +0200142 int ret;
Sascha Hauer29693242012-03-15 10:04:35 +0100143
Sascha Hauer140827c2012-08-28 09:12:01 +0200144 ret = clk_prepare_enable(imx->clk);
145 if (ret)
146 return ret;
147
148 imx->enabled = 1;
149
150 return 0;
Sascha Hauer29693242012-03-15 10:04:35 +0100151}
152
153static void imx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
154{
155 struct imx_chip *imx = to_imx_chip(chip);
156
157 writel(0, imx->mmio_base + MX3_PWMCR);
158
Sascha Hauer140827c2012-08-28 09:12:01 +0200159 clk_disable_unprepare(imx->clk);
160 imx->enabled = 0;
Sascha Hauer29693242012-03-15 10:04:35 +0100161}
162
163static struct pwm_ops imx_pwm_ops = {
164 .enable = imx_pwm_enable,
165 .disable = imx_pwm_disable,
166 .config = imx_pwm_config,
167 .owner = THIS_MODULE,
168};
169
170static int __devinit imx_pwm_probe(struct platform_device *pdev)
171{
172 struct imx_chip *imx;
173 struct resource *r;
174 int ret = 0;
175
Axel Lina9970e32012-07-01 08:27:23 +0800176 imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
Sascha Hauer29693242012-03-15 10:04:35 +0100177 if (imx == NULL) {
178 dev_err(&pdev->dev, "failed to allocate memory\n");
179 return -ENOMEM;
180 }
181
Axel Lina9970e32012-07-01 08:27:23 +0800182 imx->clk = devm_clk_get(&pdev->dev, "pwm");
Sascha Hauer29693242012-03-15 10:04:35 +0100183
Axel Lina9970e32012-07-01 08:27:23 +0800184 if (IS_ERR(imx->clk))
185 return PTR_ERR(imx->clk);
Sascha Hauer29693242012-03-15 10:04:35 +0100186
187 imx->chip.ops = &imx_pwm_ops;
188 imx->chip.dev = &pdev->dev;
189 imx->chip.base = -1;
190 imx->chip.npwm = 1;
191
Sascha Hauer29693242012-03-15 10:04:35 +0100192 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 if (r == NULL) {
194 dev_err(&pdev->dev, "no memory resource defined\n");
Axel Lina9970e32012-07-01 08:27:23 +0800195 return -ENODEV;
Sascha Hauer29693242012-03-15 10:04:35 +0100196 }
197
Axel Lina9970e32012-07-01 08:27:23 +0800198 imx->mmio_base = devm_request_and_ioremap(&pdev->dev, r);
199 if (imx->mmio_base == NULL)
200 return -EADDRNOTAVAIL;
Sascha Hauer29693242012-03-15 10:04:35 +0100201
Sascha Hauer19e73332012-07-03 17:28:14 +0200202 if (cpu_is_mx1() || cpu_is_mx21())
203 imx->config = imx_pwm_config_v1;
204 else
205 imx->config = imx_pwm_config_v2;
206
Sascha Hauer29693242012-03-15 10:04:35 +0100207 ret = pwmchip_add(&imx->chip);
208 if (ret < 0)
Axel Lina9970e32012-07-01 08:27:23 +0800209 return ret;
Sascha Hauer29693242012-03-15 10:04:35 +0100210
211 platform_set_drvdata(pdev, imx);
212 return 0;
Sascha Hauer29693242012-03-15 10:04:35 +0100213}
214
215static int __devexit imx_pwm_remove(struct platform_device *pdev)
216{
217 struct imx_chip *imx;
Sascha Hauer29693242012-03-15 10:04:35 +0100218
219 imx = platform_get_drvdata(pdev);
220 if (imx == NULL)
221 return -ENODEV;
222
Axel Lina9970e32012-07-01 08:27:23 +0800223 return pwmchip_remove(&imx->chip);
Sascha Hauer29693242012-03-15 10:04:35 +0100224}
225
226static struct platform_driver imx_pwm_driver = {
227 .driver = {
228 .name = "mxc_pwm",
229 },
230 .probe = imx_pwm_probe,
231 .remove = __devexit_p(imx_pwm_remove),
232};
233
234static int __init imx_pwm_init(void)
235{
236 return platform_driver_register(&imx_pwm_driver);
237}
238arch_initcall(imx_pwm_init);
239
240static void __exit imx_pwm_exit(void)
241{
242 platform_driver_unregister(&imx_pwm_driver);
243}
244module_exit(imx_pwm_exit);
245
246MODULE_LICENSE("GPL v2");
247MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");