2 * Simple PWM driver for EP93XX
4 * (c) Copyright 2009 Matthieu Crapet <mcrapet@gmail.com>
5 * (c) Copyright 2009 H Hartley Sweeten <hsweeten@visionengravers.com>
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.
12 * EP9307 has only one channel:
15 * EP9301/02/12/15 have two channels:
17 * - PWMOUT1 (alternate function for EGPIO14)
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/clk.h>
24 #include <linux/err.h>
27 #include <mach/platform.h>
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
34 #define EP93XX_PWM_MAX_COUNT 0xFFFF
37 void __iomem *mmio_base;
42 static inline void ep93xx_pwm_writel(struct ep93xx_pwm *pwm,
43 unsigned int val, unsigned int off)
45 __raw_writel(val, pwm->mmio_base + off);
48 static inline unsigned int ep93xx_pwm_readl(struct ep93xx_pwm *pwm,
51 return __raw_readl(pwm->mmio_base + off);
54 static inline void ep93xx_pwm_write_tc(struct ep93xx_pwm *pwm, u16 value)
56 ep93xx_pwm_writel(pwm, value, EP93XX_PWMx_TERM_COUNT);
59 static inline u16 ep93xx_pwm_read_tc(struct ep93xx_pwm *pwm)
61 return ep93xx_pwm_readl(pwm, EP93XX_PWMx_TERM_COUNT);
64 static inline void ep93xx_pwm_write_dc(struct ep93xx_pwm *pwm, u16 value)
66 ep93xx_pwm_writel(pwm, value, EP93XX_PWMx_DUTY_CYCLE);
69 static inline void ep93xx_pwm_enable(struct ep93xx_pwm *pwm)
71 ep93xx_pwm_writel(pwm, 0x1, EP93XX_PWMx_ENABLE);
74 static inline void ep93xx_pwm_disable(struct ep93xx_pwm *pwm)
76 ep93xx_pwm_writel(pwm, 0x0, EP93XX_PWMx_ENABLE);
79 static inline int ep93xx_pwm_is_enabled(struct ep93xx_pwm *pwm)
81 return ep93xx_pwm_readl(pwm, EP93XX_PWMx_ENABLE) & 0x1;
84 static inline void ep93xx_pwm_invert(struct ep93xx_pwm *pwm)
86 ep93xx_pwm_writel(pwm, 0x1, EP93XX_PWMx_INVERT);
89 static inline void ep93xx_pwm_normal(struct ep93xx_pwm *pwm)
91 ep93xx_pwm_writel(pwm, 0x0, EP93XX_PWMx_INVERT);
94 static inline int ep93xx_pwm_is_inverted(struct ep93xx_pwm *pwm)
96 return ep93xx_pwm_readl(pwm, EP93XX_PWMx_INVERT) & 0x1;
100 * /sys/devices/platform/ep93xx-pwm.N
101 * /min_freq read-only minimum pwm output frequency
102 * /max_req read-only maximum pwm output frequency
103 * /freq read-write pwm output frequency (0 = disable output)
104 * /duty_percent read-write pwm duty cycle percent (1..99)
105 * /invert read-write invert pwm output
108 static ssize_t ep93xx_pwm_get_min_freq(struct device *dev,
109 struct device_attribute *attr, char *buf)
111 struct platform_device *pdev = to_platform_device(dev);
112 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
113 unsigned long rate = clk_get_rate(pwm->clk);
115 return sprintf(buf, "%ld\n", rate / (EP93XX_PWM_MAX_COUNT + 1));
118 static ssize_t ep93xx_pwm_get_max_freq(struct device *dev,
119 struct device_attribute *attr, char *buf)
121 struct platform_device *pdev = to_platform_device(dev);
122 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
123 unsigned long rate = clk_get_rate(pwm->clk);
125 return sprintf(buf, "%ld\n", rate / 2);
128 static ssize_t ep93xx_pwm_get_freq(struct device *dev,
129 struct device_attribute *attr, char *buf)
131 struct platform_device *pdev = to_platform_device(dev);
132 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
134 if (ep93xx_pwm_is_enabled(pwm)) {
135 unsigned long rate = clk_get_rate(pwm->clk);
136 u16 term = ep93xx_pwm_read_tc(pwm);
138 return sprintf(buf, "%ld\n", rate / (term + 1));
140 return sprintf(buf, "disabled\n");
144 static ssize_t ep93xx_pwm_set_freq(struct device *dev,
145 struct device_attribute *attr, const char *buf, size_t count)
147 struct platform_device *pdev = to_platform_device(dev);
148 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
152 err = strict_strtol(buf, 10, &val);
157 ep93xx_pwm_disable(pwm);
158 } else if (val <= (clk_get_rate(pwm->clk) / 2)) {
161 val = (clk_get_rate(pwm->clk) / val) - 1;
162 if (val > EP93XX_PWM_MAX_COUNT)
163 val = EP93XX_PWM_MAX_COUNT;
167 term = ep93xx_pwm_read_tc(pwm);
168 duty = ((val + 1) * pwm->duty_percent / 100) - 1;
170 /* If pwm is running, order is important */
172 ep93xx_pwm_write_tc(pwm, val);
173 ep93xx_pwm_write_dc(pwm, duty);
175 ep93xx_pwm_write_dc(pwm, duty);
176 ep93xx_pwm_write_tc(pwm, val);
179 if (!ep93xx_pwm_is_enabled(pwm))
180 ep93xx_pwm_enable(pwm);
188 static ssize_t ep93xx_pwm_get_duty_percent(struct device *dev,
189 struct device_attribute *attr, char *buf)
191 struct platform_device *pdev = to_platform_device(dev);
192 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
194 return sprintf(buf, "%d\n", pwm->duty_percent);
197 static ssize_t ep93xx_pwm_set_duty_percent(struct device *dev,
198 struct device_attribute *attr, const char *buf, size_t count)
200 struct platform_device *pdev = to_platform_device(dev);
201 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
205 err = strict_strtol(buf, 10, &val);
209 if (val > 0 && val < 100) {
210 u32 term = ep93xx_pwm_read_tc(pwm);
211 ep93xx_pwm_write_dc(pwm, ((term + 1) * val / 100) - 1);
212 pwm->duty_percent = val;
219 static ssize_t ep93xx_pwm_get_invert(struct device *dev,
220 struct device_attribute *attr, char *buf)
222 struct platform_device *pdev = to_platform_device(dev);
223 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
225 return sprintf(buf, "%d\n", ep93xx_pwm_is_inverted(pwm));
228 static ssize_t ep93xx_pwm_set_invert(struct device *dev,
229 struct device_attribute *attr, const char *buf, size_t count)
231 struct platform_device *pdev = to_platform_device(dev);
232 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
236 err = strict_strtol(buf, 10, &val);
241 ep93xx_pwm_normal(pwm);
243 ep93xx_pwm_invert(pwm);
250 static DEVICE_ATTR(min_freq, S_IRUGO, ep93xx_pwm_get_min_freq, NULL);
251 static DEVICE_ATTR(max_freq, S_IRUGO, ep93xx_pwm_get_max_freq, NULL);
252 static DEVICE_ATTR(freq, S_IWUGO | S_IRUGO,
253 ep93xx_pwm_get_freq, ep93xx_pwm_set_freq);
254 static DEVICE_ATTR(duty_percent, S_IWUGO | S_IRUGO,
255 ep93xx_pwm_get_duty_percent, ep93xx_pwm_set_duty_percent);
256 static DEVICE_ATTR(invert, S_IWUGO | S_IRUGO,
257 ep93xx_pwm_get_invert, ep93xx_pwm_set_invert);
259 static struct attribute *ep93xx_pwm_attrs[] = {
260 &dev_attr_min_freq.attr,
261 &dev_attr_max_freq.attr,
263 &dev_attr_duty_percent.attr,
264 &dev_attr_invert.attr,
268 static const struct attribute_group ep93xx_pwm_sysfs_files = {
269 .attrs = ep93xx_pwm_attrs,
272 static int __init ep93xx_pwm_probe(struct platform_device *pdev)
274 struct ep93xx_pwm *pwm;
275 struct resource *res;
278 err = ep93xx_pwm_acquire_gpio(pdev);
282 pwm = kzalloc(sizeof(struct ep93xx_pwm), GFP_KERNEL);
288 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
291 goto fail_no_mem_resource;
294 res = request_mem_region(res->start, resource_size(res), pdev->name);
297 goto fail_no_mem_resource;
300 pwm->mmio_base = ioremap(res->start, resource_size(res));
301 if (pwm->mmio_base == NULL) {
303 goto fail_no_ioremap;
306 err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
310 pwm->clk = clk_get(&pdev->dev, "pwm_clk");
311 if (IS_ERR(pwm->clk)) {
312 err = PTR_ERR(pwm->clk);
316 pwm->duty_percent = 50;
318 platform_set_drvdata(pdev, pwm);
320 /* disable pwm at startup. Avoids zero value. */
321 ep93xx_pwm_disable(pwm);
322 ep93xx_pwm_write_tc(pwm, EP93XX_PWM_MAX_COUNT);
323 ep93xx_pwm_write_dc(pwm, EP93XX_PWM_MAX_COUNT / 2);
325 clk_enable(pwm->clk);
330 sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
332 iounmap(pwm->mmio_base);
334 release_mem_region(res->start, resource_size(res));
335 fail_no_mem_resource:
338 ep93xx_pwm_release_gpio(pdev);
342 static int __exit ep93xx_pwm_remove(struct platform_device *pdev)
344 struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
345 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
347 ep93xx_pwm_disable(pwm);
348 clk_disable(pwm->clk);
350 platform_set_drvdata(pdev, NULL);
351 sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
352 iounmap(pwm->mmio_base);
353 release_mem_region(res->start, resource_size(res));
355 ep93xx_pwm_release_gpio(pdev);
360 static struct platform_driver ep93xx_pwm_driver = {
362 .name = "ep93xx-pwm",
363 .owner = THIS_MODULE,
365 .remove = __exit_p(ep93xx_pwm_remove),
368 static int __init ep93xx_pwm_init(void)
370 return platform_driver_probe(&ep93xx_pwm_driver, ep93xx_pwm_probe);
373 static void __exit ep93xx_pwm_exit(void)
375 platform_driver_unregister(&ep93xx_pwm_driver);
378 module_init(ep93xx_pwm_init);
379 module_exit(ep93xx_pwm_exit);
381 MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>, "
382 "H Hartley Sweeten <hsweeten@visionengravers.com>");
383 MODULE_DESCRIPTION("EP93xx PWM driver");
384 MODULE_LICENSE("GPL");
385 MODULE_ALIAS("platform:ep93xx-pwm");