blob: cdc2ffc08ab986f9fe7dc995ce6a618928ede23f [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 int ep93xx_pwm_is_inverted(struct ep93xx_pwm *pwm)
53{
H Hartley Sweetena39ca272013-05-24 16:22:30 -070054 return readl(pwm->mmio_base + EP93XX_PWMx_INVERT) & 0x1;
Hartley Sweetenef123792009-07-29 22:41:06 +010055}
56
57/*
58 * /sys/devices/platform/ep93xx-pwm.N
59 * /min_freq read-only minimum pwm output frequency
60 * /max_req read-only maximum pwm output frequency
61 * /freq read-write pwm output frequency (0 = disable output)
62 * /duty_percent read-write pwm duty cycle percent (1..99)
63 * /invert read-write invert pwm output
64 */
65
66static ssize_t ep93xx_pwm_get_min_freq(struct device *dev,
67 struct device_attribute *attr, char *buf)
68{
69 struct platform_device *pdev = to_platform_device(dev);
70 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
71 unsigned long rate = clk_get_rate(pwm->clk);
72
73 return sprintf(buf, "%ld\n", rate / (EP93XX_PWM_MAX_COUNT + 1));
74}
75
76static ssize_t ep93xx_pwm_get_max_freq(struct device *dev,
77 struct device_attribute *attr, char *buf)
78{
79 struct platform_device *pdev = to_platform_device(dev);
80 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
81 unsigned long rate = clk_get_rate(pwm->clk);
82
83 return sprintf(buf, "%ld\n", rate / 2);
84}
85
86static ssize_t ep93xx_pwm_get_freq(struct device *dev,
87 struct device_attribute *attr, char *buf)
88{
89 struct platform_device *pdev = to_platform_device(dev);
90 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
91
92 if (ep93xx_pwm_is_enabled(pwm)) {
93 unsigned long rate = clk_get_rate(pwm->clk);
94 u16 term = ep93xx_pwm_read_tc(pwm);
95
96 return sprintf(buf, "%ld\n", rate / (term + 1));
97 } else {
98 return sprintf(buf, "disabled\n");
99 }
100}
101
102static ssize_t ep93xx_pwm_set_freq(struct device *dev,
103 struct device_attribute *attr, const char *buf, size_t count)
104{
105 struct platform_device *pdev = to_platform_device(dev);
106 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
107 long val;
108 int err;
109
110 err = strict_strtol(buf, 10, &val);
111 if (err)
112 return -EINVAL;
113
114 if (val == 0) {
H Hartley Sweeten02846b92013-05-24 16:24:51 -0700115 writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100116 } else if (val <= (clk_get_rate(pwm->clk) / 2)) {
117 u32 term, duty;
118
119 val = (clk_get_rate(pwm->clk) / val) - 1;
120 if (val > EP93XX_PWM_MAX_COUNT)
121 val = EP93XX_PWM_MAX_COUNT;
122 if (val < 1)
123 val = 1;
124
125 term = ep93xx_pwm_read_tc(pwm);
126 duty = ((val + 1) * pwm->duty_percent / 100) - 1;
127
128 /* If pwm is running, order is important */
129 if (val > term) {
H Hartley Sweeten53e2e382013-05-24 16:23:00 -0700130 writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700131 writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100132 } else {
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700133 writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
H Hartley Sweeten53e2e382013-05-24 16:23:00 -0700134 writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
Hartley Sweetenef123792009-07-29 22:41:06 +0100135 }
136
137 if (!ep93xx_pwm_is_enabled(pwm))
H Hartley Sweetenac91b962013-05-24 16:24:16 -0700138 writel(0x1, pwm->mmio_base + EP93XX_PWMx_ENABLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100139 } else {
140 return -EINVAL;
141 }
142
143 return count;
144}
145
146static ssize_t ep93xx_pwm_get_duty_percent(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
149 struct platform_device *pdev = to_platform_device(dev);
150 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
151
152 return sprintf(buf, "%d\n", pwm->duty_percent);
153}
154
155static ssize_t ep93xx_pwm_set_duty_percent(struct device *dev,
156 struct device_attribute *attr, const char *buf, size_t count)
157{
158 struct platform_device *pdev = to_platform_device(dev);
159 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
160 long val;
161 int err;
162
163 err = strict_strtol(buf, 10, &val);
164 if (err)
165 return -EINVAL;
166
167 if (val > 0 && val < 100) {
168 u32 term = ep93xx_pwm_read_tc(pwm);
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700169 u32 duty = ((term + 1) * val / 100) - 1;
170
171 writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100172 pwm->duty_percent = val;
173 return count;
174 }
175
176 return -EINVAL;
177}
178
179static ssize_t ep93xx_pwm_get_invert(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
182 struct platform_device *pdev = to_platform_device(dev);
183 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
184
185 return sprintf(buf, "%d\n", ep93xx_pwm_is_inverted(pwm));
186}
187
188static ssize_t ep93xx_pwm_set_invert(struct device *dev,
189 struct device_attribute *attr, const char *buf, size_t count)
190{
191 struct platform_device *pdev = to_platform_device(dev);
192 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
193 long val;
194 int err;
195
196 err = strict_strtol(buf, 10, &val);
197 if (err)
198 return -EINVAL;
199
200 if (val == 0)
H Hartley Sweetend98d7902013-05-24 16:26:11 -0700201 writel(0x0, pwm->mmio_base + EP93XX_PWMx_INVERT);
Hartley Sweetenef123792009-07-29 22:41:06 +0100202 else if (val == 1)
H Hartley Sweeten47a36ee2013-05-24 16:25:23 -0700203 writel(0x1, pwm->mmio_base + EP93XX_PWMx_INVERT);
Hartley Sweetenef123792009-07-29 22:41:06 +0100204 else
205 return -EINVAL;
206
207 return count;
208}
209
210static DEVICE_ATTR(min_freq, S_IRUGO, ep93xx_pwm_get_min_freq, NULL);
211static DEVICE_ATTR(max_freq, S_IRUGO, ep93xx_pwm_get_max_freq, NULL);
Vasiliy Kulikovdeb187e2011-03-22 16:34:01 -0700212static DEVICE_ATTR(freq, S_IWUSR | S_IRUGO,
Hartley Sweetenef123792009-07-29 22:41:06 +0100213 ep93xx_pwm_get_freq, ep93xx_pwm_set_freq);
Vasiliy Kulikovdeb187e2011-03-22 16:34:01 -0700214static DEVICE_ATTR(duty_percent, S_IWUSR | S_IRUGO,
Hartley Sweetenef123792009-07-29 22:41:06 +0100215 ep93xx_pwm_get_duty_percent, ep93xx_pwm_set_duty_percent);
Vasiliy Kulikovdeb187e2011-03-22 16:34:01 -0700216static DEVICE_ATTR(invert, S_IWUSR | S_IRUGO,
Hartley Sweetenef123792009-07-29 22:41:06 +0100217 ep93xx_pwm_get_invert, ep93xx_pwm_set_invert);
218
219static struct attribute *ep93xx_pwm_attrs[] = {
220 &dev_attr_min_freq.attr,
221 &dev_attr_max_freq.attr,
222 &dev_attr_freq.attr,
223 &dev_attr_duty_percent.attr,
224 &dev_attr_invert.attr,
225 NULL
226};
227
228static const struct attribute_group ep93xx_pwm_sysfs_files = {
229 .attrs = ep93xx_pwm_attrs,
230};
231
232static int __init ep93xx_pwm_probe(struct platform_device *pdev)
233{
234 struct ep93xx_pwm *pwm;
235 struct resource *res;
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700236 int ret;
Hartley Sweetenef123792009-07-29 22:41:06 +0100237
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700238 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
239 if (!pwm)
240 return -ENOMEM;
Hartley Sweetenef123792009-07-29 22:41:06 +0100241
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700242 pwm->clk = devm_clk_get(&pdev->dev, "pwm_clk");
243 if (IS_ERR(pwm->clk))
244 return PTR_ERR(pwm->clk);
Hartley Sweetenef123792009-07-29 22:41:06 +0100245
246 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700247 pwm->mmio_base = devm_ioremap_resource(&pdev->dev, res);
248 if (IS_ERR(pwm->mmio_base))
249 return PTR_ERR(pwm->mmio_base);
Hartley Sweetenef123792009-07-29 22:41:06 +0100250
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700251 ret = ep93xx_pwm_acquire_gpio(pdev);
252 if (ret)
253 return ret;
Hartley Sweetenef123792009-07-29 22:41:06 +0100254
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700255 ret = sysfs_create_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
256 if (ret) {
257 ep93xx_pwm_release_gpio(pdev);
258 return ret;
Hartley Sweetenef123792009-07-29 22:41:06 +0100259 }
260
261 pwm->duty_percent = 50;
262
Hartley Sweetenef123792009-07-29 22:41:06 +0100263 /* disable pwm at startup. Avoids zero value. */
H Hartley Sweeten02846b92013-05-24 16:24:51 -0700264 writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
H Hartley Sweeten53e2e382013-05-24 16:23:00 -0700265 writel(EP93XX_PWM_MAX_COUNT, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
H Hartley Sweetenaa919762013-05-24 16:23:38 -0700266 writel(EP93XX_PWM_MAX_COUNT/2, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100267
268 clk_enable(pwm->clk);
269
H Hartley Sweeten6c7dd642013-05-24 16:21:01 -0700270 platform_set_drvdata(pdev, pwm);
Hartley Sweetenef123792009-07-29 22:41:06 +0100271 return 0;
Hartley Sweetenef123792009-07-29 22:41:06 +0100272}
273
274static int __exit ep93xx_pwm_remove(struct platform_device *pdev)
275{
276 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
Hartley Sweetenef123792009-07-29 22:41:06 +0100277
H Hartley Sweeten02846b92013-05-24 16:24:51 -0700278 writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
Hartley Sweetenef123792009-07-29 22:41:06 +0100279 clk_disable(pwm->clk);
Hartley Sweetenef123792009-07-29 22:41:06 +0100280 sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
Hartley Sweetenef123792009-07-29 22:41:06 +0100281 ep93xx_pwm_release_gpio(pdev);
282
283 return 0;
284}
285
286static struct platform_driver ep93xx_pwm_driver = {
287 .driver = {
288 .name = "ep93xx-pwm",
289 .owner = THIS_MODULE,
290 },
291 .remove = __exit_p(ep93xx_pwm_remove),
292};
293
Jingoo Hanead050d2013-03-05 11:04:07 +0900294module_platform_driver_probe(ep93xx_pwm_driver, ep93xx_pwm_probe);
Hartley Sweetenef123792009-07-29 22:41:06 +0100295
296MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>, "
297 "H Hartley Sweeten <hsweeten@visionengravers.com>");
298MODULE_DESCRIPTION("EP93xx PWM driver");
299MODULE_LICENSE("GPL");
300MODULE_ALIAS("platform:ep93xx-pwm");