blob: cdb67a9c1959241aa23852edcabcd2c8d97e7e16 [file] [log] [blame]
Hartley Sweetenef123792009-07-29 22:41:06 +01001/*
2 * Simple PWM driver for EP93XX
3 *
4 * (c) Copyright 2009 Matthieu Crapet <mcrapet@gmail.com>
5 * (c) Copyright 2009 H Hartley Sweeten <hsweeten@visionengravers.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * EP9307 has only one channel:
13 * - PWMOUT
14 *
15 * EP9301/02/12/15 have two channels:
16 * - PWMOUT
17 * - PWMOUT1 (alternate function for EGPIO14)
18 */
19
20#include <linux/module.h>
21#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Hartley Sweetenef123792009-07-29 22:41:06 +010023#include <linux/clk.h>
24#include <linux/err.h>
25#include <linux/io.h>
26
27#include <mach/platform.h>
28
29#define EP93XX_PWMx_TERM_COUNT 0x00
30#define EP93XX_PWMx_DUTY_CYCLE 0x04
31#define EP93XX_PWMx_ENABLE 0x08
32#define EP93XX_PWMx_INVERT 0x0C
33
34#define EP93XX_PWM_MAX_COUNT 0xFFFF
35
36struct ep93xx_pwm {
37 void __iomem *mmio_base;
38 struct clk *clk;
39 u32 duty_percent;
40};
41
Hartley Sweetenef123792009-07-29 22:41:06 +010042/*
43 * /sys/devices/platform/ep93xx-pwm.N
44 * /min_freq read-only minimum pwm output frequency
45 * /max_req read-only maximum pwm output frequency
46 * /freq read-write pwm output frequency (0 = disable output)
47 * /duty_percent read-write pwm duty cycle percent (1..99)
48 * /invert read-write invert pwm output
49 */
50
51static ssize_t ep93xx_pwm_get_min_freq(struct device *dev,
52 struct device_attribute *attr, char *buf)
53{
54 struct platform_device *pdev = to_platform_device(dev);
55 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
56 unsigned long rate = clk_get_rate(pwm->clk);
57
58 return sprintf(buf, "%ld\n", rate / (EP93XX_PWM_MAX_COUNT + 1));
59}
60
61static ssize_t ep93xx_pwm_get_max_freq(struct device *dev,
62 struct device_attribute *attr, char *buf)
63{
64 struct platform_device *pdev = to_platform_device(dev);
65 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
66 unsigned long rate = clk_get_rate(pwm->clk);
67
68 return sprintf(buf, "%ld\n", rate / 2);
69}
70
71static ssize_t ep93xx_pwm_get_freq(struct device *dev,
72 struct device_attribute *attr, char *buf)
73{
74 struct platform_device *pdev = to_platform_device(dev);
75 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
76
H Hartley Sweeten5b2cd8f2013-05-24 16:27:08 -070077 if (readl(pwm->mmio_base + EP93XX_PWMx_ENABLE) & 0x1) {
Hartley Sweetenef123792009-07-29 22:41:06 +010078 unsigned long rate = clk_get_rate(pwm->clk);
H Hartley Sweeten7501ba362013-05-24 16:26:41 -070079 u16 term = readl(pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
Hartley Sweetenef123792009-07-29 22:41:06 +010080
81 return sprintf(buf, "%ld\n", rate / (term + 1));
82 } else {
83 return sprintf(buf, "disabled\n");
84 }
85}
86
87static ssize_t ep93xx_pwm_set_freq(struct device *dev,
88 struct device_attribute *attr, const char *buf, size_t count)
89{
90 struct platform_device *pdev = to_platform_device(dev);
91 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
92 long val;
93 int err;
94
H Hartley Sweetena4b05d12013-05-24 16:28:44 -070095 err = kstrtol(buf, 10, &val);
Hartley Sweetenef123792009-07-29 22:41:06 +010096 if (err)
97 return -EINVAL;
98
99 if (val == 0) {
H Hartley Sweeten02846b92013-05-24 16:24:51 -0700100 writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100101 } else if (val <= (clk_get_rate(pwm->clk) / 2)) {
102 u32 term, duty;
103
104 val = (clk_get_rate(pwm->clk) / val) - 1;
105 if (val > EP93XX_PWM_MAX_COUNT)
106 val = EP93XX_PWM_MAX_COUNT;
107 if (val < 1)
108 val = 1;
109
H Hartley Sweeten7501ba362013-05-24 16:26:41 -0700110 term = readl(pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
Hartley Sweetenef123792009-07-29 22:41:06 +0100111 duty = ((val + 1) * pwm->duty_percent / 100) - 1;
112
113 /* If pwm is running, order is important */
114 if (val > term) {
H Hartley Sweeten53e2e382013-05-24 16:23:00 -0700115 writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700116 writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100117 } else {
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700118 writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
H Hartley Sweeten53e2e382013-05-24 16:23:00 -0700119 writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
Hartley Sweetenef123792009-07-29 22:41:06 +0100120 }
121
H Hartley Sweeten5b2cd8f2013-05-24 16:27:08 -0700122 if (!readl(pwm->mmio_base + EP93XX_PWMx_ENABLE) & 0x1)
H Hartley Sweetenac91b962013-05-24 16:24:16 -0700123 writel(0x1, pwm->mmio_base + EP93XX_PWMx_ENABLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100124 } else {
125 return -EINVAL;
126 }
127
128 return count;
129}
130
131static ssize_t ep93xx_pwm_get_duty_percent(struct device *dev,
132 struct device_attribute *attr, char *buf)
133{
134 struct platform_device *pdev = to_platform_device(dev);
135 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
136
137 return sprintf(buf, "%d\n", pwm->duty_percent);
138}
139
140static ssize_t ep93xx_pwm_set_duty_percent(struct device *dev,
141 struct device_attribute *attr, const char *buf, size_t count)
142{
143 struct platform_device *pdev = to_platform_device(dev);
144 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
145 long val;
146 int err;
147
H Hartley Sweetena4b05d12013-05-24 16:28:44 -0700148 err = kstrtol(buf, 10, &val);
Hartley Sweetenef123792009-07-29 22:41:06 +0100149 if (err)
150 return -EINVAL;
151
152 if (val > 0 && val < 100) {
H Hartley Sweeten7501ba362013-05-24 16:26:41 -0700153 u32 term = readl(pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700154 u32 duty = ((term + 1) * val / 100) - 1;
155
156 writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100157 pwm->duty_percent = val;
158 return count;
159 }
160
161 return -EINVAL;
162}
163
164static ssize_t ep93xx_pwm_get_invert(struct device *dev,
165 struct device_attribute *attr, char *buf)
166{
167 struct platform_device *pdev = to_platform_device(dev);
168 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
H Hartley Sweetenddfd6892013-05-24 16:27:38 -0700169 int inverted = readl(pwm->mmio_base + EP93XX_PWMx_INVERT) & 0x1;
Hartley Sweetenef123792009-07-29 22:41:06 +0100170
H Hartley Sweetenddfd6892013-05-24 16:27:38 -0700171 return sprintf(buf, "%d\n", inverted);
Hartley Sweetenef123792009-07-29 22:41:06 +0100172}
173
174static ssize_t ep93xx_pwm_set_invert(struct device *dev,
175 struct device_attribute *attr, const char *buf, size_t count)
176{
177 struct platform_device *pdev = to_platform_device(dev);
178 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
179 long val;
180 int err;
181
H Hartley Sweetena4b05d12013-05-24 16:28:44 -0700182 err = kstrtol(buf, 10, &val);
Hartley Sweetenef123792009-07-29 22:41:06 +0100183 if (err)
184 return -EINVAL;
185
186 if (val == 0)
H Hartley Sweetend98d7902013-05-24 16:26:11 -0700187 writel(0x0, pwm->mmio_base + EP93XX_PWMx_INVERT);
Hartley Sweetenef123792009-07-29 22:41:06 +0100188 else if (val == 1)
H Hartley Sweeten47a36ee2013-05-24 16:25:23 -0700189 writel(0x1, pwm->mmio_base + EP93XX_PWMx_INVERT);
Hartley Sweetenef123792009-07-29 22:41:06 +0100190 else
191 return -EINVAL;
192
193 return count;
194}
195
196static DEVICE_ATTR(min_freq, S_IRUGO, ep93xx_pwm_get_min_freq, NULL);
197static DEVICE_ATTR(max_freq, S_IRUGO, ep93xx_pwm_get_max_freq, NULL);
Vasiliy Kulikovdeb187e2011-03-22 16:34:01 -0700198static DEVICE_ATTR(freq, S_IWUSR | S_IRUGO,
Hartley Sweetenef123792009-07-29 22:41:06 +0100199 ep93xx_pwm_get_freq, ep93xx_pwm_set_freq);
Vasiliy Kulikovdeb187e2011-03-22 16:34:01 -0700200static DEVICE_ATTR(duty_percent, S_IWUSR | S_IRUGO,
Hartley Sweetenef123792009-07-29 22:41:06 +0100201 ep93xx_pwm_get_duty_percent, ep93xx_pwm_set_duty_percent);
Vasiliy Kulikovdeb187e2011-03-22 16:34:01 -0700202static DEVICE_ATTR(invert, S_IWUSR | S_IRUGO,
Hartley Sweetenef123792009-07-29 22:41:06 +0100203 ep93xx_pwm_get_invert, ep93xx_pwm_set_invert);
204
205static struct attribute *ep93xx_pwm_attrs[] = {
206 &dev_attr_min_freq.attr,
207 &dev_attr_max_freq.attr,
208 &dev_attr_freq.attr,
209 &dev_attr_duty_percent.attr,
210 &dev_attr_invert.attr,
211 NULL
212};
213
214static const struct attribute_group ep93xx_pwm_sysfs_files = {
215 .attrs = ep93xx_pwm_attrs,
216};
217
H Hartley Sweeten6e1cf66e2013-05-24 16:28:14 -0700218static int ep93xx_pwm_probe(struct platform_device *pdev)
Hartley Sweetenef123792009-07-29 22:41:06 +0100219{
220 struct ep93xx_pwm *pwm;
221 struct resource *res;
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700222 int ret;
Hartley Sweetenef123792009-07-29 22:41:06 +0100223
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700224 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
225 if (!pwm)
226 return -ENOMEM;
Hartley Sweetenef123792009-07-29 22:41:06 +0100227
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700228 pwm->clk = devm_clk_get(&pdev->dev, "pwm_clk");
229 if (IS_ERR(pwm->clk))
230 return PTR_ERR(pwm->clk);
Hartley Sweetenef123792009-07-29 22:41:06 +0100231
232 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700233 pwm->mmio_base = devm_ioremap_resource(&pdev->dev, res);
234 if (IS_ERR(pwm->mmio_base))
235 return PTR_ERR(pwm->mmio_base);
Hartley Sweetenef123792009-07-29 22:41:06 +0100236
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700237 ret = ep93xx_pwm_acquire_gpio(pdev);
238 if (ret)
239 return ret;
Hartley Sweetenef123792009-07-29 22:41:06 +0100240
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700241 ret = sysfs_create_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
242 if (ret) {
243 ep93xx_pwm_release_gpio(pdev);
244 return ret;
Hartley Sweetenef123792009-07-29 22:41:06 +0100245 }
246
247 pwm->duty_percent = 50;
248
Hartley Sweetenef123792009-07-29 22:41:06 +0100249 /* disable pwm at startup. Avoids zero value. */
H Hartley Sweeten02846b92013-05-24 16:24:51 -0700250 writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
H Hartley Sweeten53e2e382013-05-24 16:23:00 -0700251 writel(EP93XX_PWM_MAX_COUNT, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700252 writel(EP93XX_PWM_MAX_COUNT/2, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100253
254 clk_enable(pwm->clk);
255
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700256 platform_set_drvdata(pdev, pwm);
Hartley Sweetenef123792009-07-29 22:41:06 +0100257 return 0;
Hartley Sweetenef123792009-07-29 22:41:06 +0100258}
259
H Hartley Sweeten6e1cf66e2013-05-24 16:28:14 -0700260static int ep93xx_pwm_remove(struct platform_device *pdev)
Hartley Sweetenef123792009-07-29 22:41:06 +0100261{
262 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
Hartley Sweetenef123792009-07-29 22:41:06 +0100263
H Hartley Sweeten02846b92013-05-24 16:24:51 -0700264 writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100265 clk_disable(pwm->clk);
Hartley Sweetenef123792009-07-29 22:41:06 +0100266 sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
Hartley Sweetenef123792009-07-29 22:41:06 +0100267 ep93xx_pwm_release_gpio(pdev);
268
269 return 0;
270}
271
272static struct platform_driver ep93xx_pwm_driver = {
273 .driver = {
274 .name = "ep93xx-pwm",
275 .owner = THIS_MODULE,
276 },
H Hartley Sweeten6e1cf66e2013-05-24 16:28:14 -0700277 .probe = ep93xx_pwm_probe,
278 .remove = ep93xx_pwm_remove,
Hartley Sweetenef123792009-07-29 22:41:06 +0100279};
H Hartley Sweeten6e1cf66e2013-05-24 16:28:14 -0700280module_platform_driver(ep93xx_pwm_driver);
Hartley Sweetenef123792009-07-29 22:41:06 +0100281
282MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>, "
283 "H Hartley Sweeten <hsweeten@visionengravers.com>");
284MODULE_DESCRIPTION("EP93xx PWM driver");
285MODULE_LICENSE("GPL");
286MODULE_ALIAS("platform:ep93xx-pwm");