blob: 5193faf594bf42eea8b02aff369510e5eaaea124 [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 +010042static inline u16 ep93xx_pwm_read_tc(struct ep93xx_pwm *pwm)
43{
H Hartley Sweetena39ca272013-05-24 16:22:30 -070044 return readl(pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
Hartley Sweetenef123792009-07-29 22:41:06 +010045}
46
Hartley Sweetenef123792009-07-29 22:41:06 +010047static inline int ep93xx_pwm_is_enabled(struct ep93xx_pwm *pwm)
48{
H Hartley Sweetena39ca272013-05-24 16:22:30 -070049 return readl(pwm->mmio_base + EP93XX_PWMx_ENABLE) & 0x1;
Hartley Sweetenef123792009-07-29 22:41:06 +010050}
51
Hartley Sweetenef123792009-07-29 22:41:06 +010052static inline void ep93xx_pwm_normal(struct ep93xx_pwm *pwm)
53{
H Hartley Sweetena39ca272013-05-24 16:22:30 -070054 writel(0x0, pwm->mmio_base + EP93XX_PWMx_INVERT);
Hartley Sweetenef123792009-07-29 22:41:06 +010055}
56
57static inline int ep93xx_pwm_is_inverted(struct ep93xx_pwm *pwm)
58{
H Hartley Sweetena39ca272013-05-24 16:22:30 -070059 return readl(pwm->mmio_base + EP93XX_PWMx_INVERT) & 0x1;
Hartley Sweetenef123792009-07-29 22:41:06 +010060}
61
62/*
63 * /sys/devices/platform/ep93xx-pwm.N
64 * /min_freq read-only minimum pwm output frequency
65 * /max_req read-only maximum pwm output frequency
66 * /freq read-write pwm output frequency (0 = disable output)
67 * /duty_percent read-write pwm duty cycle percent (1..99)
68 * /invert read-write invert pwm output
69 */
70
71static ssize_t ep93xx_pwm_get_min_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 unsigned long rate = clk_get_rate(pwm->clk);
77
78 return sprintf(buf, "%ld\n", rate / (EP93XX_PWM_MAX_COUNT + 1));
79}
80
81static ssize_t ep93xx_pwm_get_max_freq(struct device *dev,
82 struct device_attribute *attr, char *buf)
83{
84 struct platform_device *pdev = to_platform_device(dev);
85 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
86 unsigned long rate = clk_get_rate(pwm->clk);
87
88 return sprintf(buf, "%ld\n", rate / 2);
89}
90
91static ssize_t ep93xx_pwm_get_freq(struct device *dev,
92 struct device_attribute *attr, char *buf)
93{
94 struct platform_device *pdev = to_platform_device(dev);
95 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
96
97 if (ep93xx_pwm_is_enabled(pwm)) {
98 unsigned long rate = clk_get_rate(pwm->clk);
99 u16 term = ep93xx_pwm_read_tc(pwm);
100
101 return sprintf(buf, "%ld\n", rate / (term + 1));
102 } else {
103 return sprintf(buf, "disabled\n");
104 }
105}
106
107static ssize_t ep93xx_pwm_set_freq(struct device *dev,
108 struct device_attribute *attr, const char *buf, size_t count)
109{
110 struct platform_device *pdev = to_platform_device(dev);
111 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
112 long val;
113 int err;
114
115 err = strict_strtol(buf, 10, &val);
116 if (err)
117 return -EINVAL;
118
119 if (val == 0) {
H Hartley Sweeten02846b92013-05-24 16:24:51 -0700120 writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100121 } else if (val <= (clk_get_rate(pwm->clk) / 2)) {
122 u32 term, duty;
123
124 val = (clk_get_rate(pwm->clk) / val) - 1;
125 if (val > EP93XX_PWM_MAX_COUNT)
126 val = EP93XX_PWM_MAX_COUNT;
127 if (val < 1)
128 val = 1;
129
130 term = ep93xx_pwm_read_tc(pwm);
131 duty = ((val + 1) * pwm->duty_percent / 100) - 1;
132
133 /* If pwm is running, order is important */
134 if (val > term) {
H Hartley Sweeten53e2e382013-05-24 16:23:00 -0700135 writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700136 writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100137 } else {
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700138 writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
H Hartley Sweeten53e2e382013-05-24 16:23:00 -0700139 writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
Hartley Sweetenef123792009-07-29 22:41:06 +0100140 }
141
142 if (!ep93xx_pwm_is_enabled(pwm))
H Hartley Sweetenac91b962013-05-24 16:24:16 -0700143 writel(0x1, pwm->mmio_base + EP93XX_PWMx_ENABLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100144 } else {
145 return -EINVAL;
146 }
147
148 return count;
149}
150
151static ssize_t ep93xx_pwm_get_duty_percent(struct device *dev,
152 struct device_attribute *attr, char *buf)
153{
154 struct platform_device *pdev = to_platform_device(dev);
155 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
156
157 return sprintf(buf, "%d\n", pwm->duty_percent);
158}
159
160static ssize_t ep93xx_pwm_set_duty_percent(struct device *dev,
161 struct device_attribute *attr, const char *buf, size_t count)
162{
163 struct platform_device *pdev = to_platform_device(dev);
164 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
165 long val;
166 int err;
167
168 err = strict_strtol(buf, 10, &val);
169 if (err)
170 return -EINVAL;
171
172 if (val > 0 && val < 100) {
173 u32 term = ep93xx_pwm_read_tc(pwm);
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700174 u32 duty = ((term + 1) * val / 100) - 1;
175
176 writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100177 pwm->duty_percent = val;
178 return count;
179 }
180
181 return -EINVAL;
182}
183
184static ssize_t ep93xx_pwm_get_invert(struct device *dev,
185 struct device_attribute *attr, char *buf)
186{
187 struct platform_device *pdev = to_platform_device(dev);
188 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
189
190 return sprintf(buf, "%d\n", ep93xx_pwm_is_inverted(pwm));
191}
192
193static ssize_t ep93xx_pwm_set_invert(struct device *dev,
194 struct device_attribute *attr, const char *buf, size_t count)
195{
196 struct platform_device *pdev = to_platform_device(dev);
197 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
198 long val;
199 int err;
200
201 err = strict_strtol(buf, 10, &val);
202 if (err)
203 return -EINVAL;
204
205 if (val == 0)
206 ep93xx_pwm_normal(pwm);
207 else if (val == 1)
H Hartley Sweeten47a36ee2013-05-24 16:25:23 -0700208 writel(0x1, pwm->mmio_base + EP93XX_PWMx_INVERT);
Hartley Sweetenef123792009-07-29 22:41:06 +0100209 else
210 return -EINVAL;
211
212 return count;
213}
214
215static DEVICE_ATTR(min_freq, S_IRUGO, ep93xx_pwm_get_min_freq, NULL);
216static DEVICE_ATTR(max_freq, S_IRUGO, ep93xx_pwm_get_max_freq, NULL);
Vasiliy Kulikovdeb187e2011-03-22 16:34:01 -0700217static DEVICE_ATTR(freq, S_IWUSR | S_IRUGO,
Hartley Sweetenef123792009-07-29 22:41:06 +0100218 ep93xx_pwm_get_freq, ep93xx_pwm_set_freq);
Vasiliy Kulikovdeb187e2011-03-22 16:34:01 -0700219static DEVICE_ATTR(duty_percent, S_IWUSR | S_IRUGO,
Hartley Sweetenef123792009-07-29 22:41:06 +0100220 ep93xx_pwm_get_duty_percent, ep93xx_pwm_set_duty_percent);
Vasiliy Kulikovdeb187e2011-03-22 16:34:01 -0700221static DEVICE_ATTR(invert, S_IWUSR | S_IRUGO,
Hartley Sweetenef123792009-07-29 22:41:06 +0100222 ep93xx_pwm_get_invert, ep93xx_pwm_set_invert);
223
224static struct attribute *ep93xx_pwm_attrs[] = {
225 &dev_attr_min_freq.attr,
226 &dev_attr_max_freq.attr,
227 &dev_attr_freq.attr,
228 &dev_attr_duty_percent.attr,
229 &dev_attr_invert.attr,
230 NULL
231};
232
233static const struct attribute_group ep93xx_pwm_sysfs_files = {
234 .attrs = ep93xx_pwm_attrs,
235};
236
237static int __init ep93xx_pwm_probe(struct platform_device *pdev)
238{
239 struct ep93xx_pwm *pwm;
240 struct resource *res;
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700241 int ret;
Hartley Sweetenef123792009-07-29 22:41:06 +0100242
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700243 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
244 if (!pwm)
245 return -ENOMEM;
Hartley Sweetenef123792009-07-29 22:41:06 +0100246
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700247 pwm->clk = devm_clk_get(&pdev->dev, "pwm_clk");
248 if (IS_ERR(pwm->clk))
249 return PTR_ERR(pwm->clk);
Hartley Sweetenef123792009-07-29 22:41:06 +0100250
251 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700252 pwm->mmio_base = devm_ioremap_resource(&pdev->dev, res);
253 if (IS_ERR(pwm->mmio_base))
254 return PTR_ERR(pwm->mmio_base);
Hartley Sweetenef123792009-07-29 22:41:06 +0100255
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700256 ret = ep93xx_pwm_acquire_gpio(pdev);
257 if (ret)
258 return ret;
Hartley Sweetenef123792009-07-29 22:41:06 +0100259
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700260 ret = sysfs_create_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
261 if (ret) {
262 ep93xx_pwm_release_gpio(pdev);
263 return ret;
Hartley Sweetenef123792009-07-29 22:41:06 +0100264 }
265
266 pwm->duty_percent = 50;
267
Hartley Sweetenef123792009-07-29 22:41:06 +0100268 /* disable pwm at startup. Avoids zero value. */
H Hartley Sweeten02846b92013-05-24 16:24:51 -0700269 writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
H Hartley Sweeten53e2e382013-05-24 16:23:00 -0700270 writel(EP93XX_PWM_MAX_COUNT, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700271 writel(EP93XX_PWM_MAX_COUNT/2, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100272
273 clk_enable(pwm->clk);
274
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700275 platform_set_drvdata(pdev, pwm);
Hartley Sweetenef123792009-07-29 22:41:06 +0100276 return 0;
Hartley Sweetenef123792009-07-29 22:41:06 +0100277}
278
279static int __exit ep93xx_pwm_remove(struct platform_device *pdev)
280{
281 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
Hartley Sweetenef123792009-07-29 22:41:06 +0100282
H Hartley Sweeten02846b92013-05-24 16:24:51 -0700283 writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100284 clk_disable(pwm->clk);
Hartley Sweetenef123792009-07-29 22:41:06 +0100285 sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
Hartley Sweetenef123792009-07-29 22:41:06 +0100286 ep93xx_pwm_release_gpio(pdev);
287
288 return 0;
289}
290
291static struct platform_driver ep93xx_pwm_driver = {
292 .driver = {
293 .name = "ep93xx-pwm",
294 .owner = THIS_MODULE,
295 },
296 .remove = __exit_p(ep93xx_pwm_remove),
297};
298
Jingoo Hanead050d2013-03-05 11:04:07 +0900299module_platform_driver_probe(ep93xx_pwm_driver, ep93xx_pwm_probe);
Hartley Sweetenef123792009-07-29 22:41:06 +0100300
301MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>, "
302 "H Hartley Sweeten <hsweeten@visionengravers.com>");
303MODULE_DESCRIPTION("EP93xx PWM driver");
304MODULE_LICENSE("GPL");
305MODULE_ALIAS("platform:ep93xx-pwm");