Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 23 | #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 | |
| 36 | struct ep93xx_pwm { |
| 37 | void __iomem *mmio_base; |
| 38 | struct clk *clk; |
| 39 | u32 duty_percent; |
| 40 | }; |
| 41 | |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 42 | static inline u16 ep93xx_pwm_read_tc(struct ep93xx_pwm *pwm) |
| 43 | { |
H Hartley Sweeten | a39ca27 | 2013-05-24 16:22:30 -0700 | [diff] [blame] | 44 | return readl(pwm->mmio_base + EP93XX_PWMx_TERM_COUNT); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 47 | static inline int ep93xx_pwm_is_enabled(struct ep93xx_pwm *pwm) |
| 48 | { |
H Hartley Sweeten | a39ca27 | 2013-05-24 16:22:30 -0700 | [diff] [blame] | 49 | return readl(pwm->mmio_base + EP93XX_PWMx_ENABLE) & 0x1; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 52 | static inline void ep93xx_pwm_normal(struct ep93xx_pwm *pwm) |
| 53 | { |
H Hartley Sweeten | a39ca27 | 2013-05-24 16:22:30 -0700 | [diff] [blame] | 54 | writel(0x0, pwm->mmio_base + EP93XX_PWMx_INVERT); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static inline int ep93xx_pwm_is_inverted(struct ep93xx_pwm *pwm) |
| 58 | { |
H Hartley Sweeten | a39ca27 | 2013-05-24 16:22:30 -0700 | [diff] [blame] | 59 | return readl(pwm->mmio_base + EP93XX_PWMx_INVERT) & 0x1; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 60 | } |
| 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 | |
| 71 | static 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 | |
| 81 | static 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 | |
| 91 | static 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 | |
| 107 | static 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 Sweeten | 02846b9 | 2013-05-24 16:24:51 -0700 | [diff] [blame] | 120 | writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 121 | } 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 Sweeten | 53e2e38 | 2013-05-24 16:23:00 -0700 | [diff] [blame] | 135 | writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT); |
H Hartley Sweeten | aa91976 | 2013-05-24 16:23:38 -0700 | [diff] [blame] | 136 | writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 137 | } else { |
H Hartley Sweeten | aa91976 | 2013-05-24 16:23:38 -0700 | [diff] [blame] | 138 | writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE); |
H Hartley Sweeten | 53e2e38 | 2013-05-24 16:23:00 -0700 | [diff] [blame] | 139 | writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | if (!ep93xx_pwm_is_enabled(pwm)) |
H Hartley Sweeten | ac91b96 | 2013-05-24 16:24:16 -0700 | [diff] [blame] | 143 | writel(0x1, pwm->mmio_base + EP93XX_PWMx_ENABLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 144 | } else { |
| 145 | return -EINVAL; |
| 146 | } |
| 147 | |
| 148 | return count; |
| 149 | } |
| 150 | |
| 151 | static 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 | |
| 160 | static 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 Sweeten | aa91976 | 2013-05-24 16:23:38 -0700 | [diff] [blame] | 174 | u32 duty = ((term + 1) * val / 100) - 1; |
| 175 | |
| 176 | writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 177 | pwm->duty_percent = val; |
| 178 | return count; |
| 179 | } |
| 180 | |
| 181 | return -EINVAL; |
| 182 | } |
| 183 | |
| 184 | static 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 | |
| 193 | static 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 Sweeten | 47a36ee | 2013-05-24 16:25:23 -0700 | [diff] [blame^] | 208 | writel(0x1, pwm->mmio_base + EP93XX_PWMx_INVERT); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 209 | else |
| 210 | return -EINVAL; |
| 211 | |
| 212 | return count; |
| 213 | } |
| 214 | |
| 215 | static DEVICE_ATTR(min_freq, S_IRUGO, ep93xx_pwm_get_min_freq, NULL); |
| 216 | static DEVICE_ATTR(max_freq, S_IRUGO, ep93xx_pwm_get_max_freq, NULL); |
Vasiliy Kulikov | deb187e | 2011-03-22 16:34:01 -0700 | [diff] [blame] | 217 | static DEVICE_ATTR(freq, S_IWUSR | S_IRUGO, |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 218 | ep93xx_pwm_get_freq, ep93xx_pwm_set_freq); |
Vasiliy Kulikov | deb187e | 2011-03-22 16:34:01 -0700 | [diff] [blame] | 219 | static DEVICE_ATTR(duty_percent, S_IWUSR | S_IRUGO, |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 220 | ep93xx_pwm_get_duty_percent, ep93xx_pwm_set_duty_percent); |
Vasiliy Kulikov | deb187e | 2011-03-22 16:34:01 -0700 | [diff] [blame] | 221 | static DEVICE_ATTR(invert, S_IWUSR | S_IRUGO, |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 222 | ep93xx_pwm_get_invert, ep93xx_pwm_set_invert); |
| 223 | |
| 224 | static 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 | |
| 233 | static const struct attribute_group ep93xx_pwm_sysfs_files = { |
| 234 | .attrs = ep93xx_pwm_attrs, |
| 235 | }; |
| 236 | |
| 237 | static int __init ep93xx_pwm_probe(struct platform_device *pdev) |
| 238 | { |
| 239 | struct ep93xx_pwm *pwm; |
| 240 | struct resource *res; |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 241 | int ret; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 242 | |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 243 | pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); |
| 244 | if (!pwm) |
| 245 | return -ENOMEM; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 246 | |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 247 | pwm->clk = devm_clk_get(&pdev->dev, "pwm_clk"); |
| 248 | if (IS_ERR(pwm->clk)) |
| 249 | return PTR_ERR(pwm->clk); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 250 | |
| 251 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 252 | pwm->mmio_base = devm_ioremap_resource(&pdev->dev, res); |
| 253 | if (IS_ERR(pwm->mmio_base)) |
| 254 | return PTR_ERR(pwm->mmio_base); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 255 | |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 256 | ret = ep93xx_pwm_acquire_gpio(pdev); |
| 257 | if (ret) |
| 258 | return ret; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 259 | |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 260 | ret = sysfs_create_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files); |
| 261 | if (ret) { |
| 262 | ep93xx_pwm_release_gpio(pdev); |
| 263 | return ret; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | pwm->duty_percent = 50; |
| 267 | |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 268 | /* disable pwm at startup. Avoids zero value. */ |
H Hartley Sweeten | 02846b9 | 2013-05-24 16:24:51 -0700 | [diff] [blame] | 269 | writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE); |
H Hartley Sweeten | 53e2e38 | 2013-05-24 16:23:00 -0700 | [diff] [blame] | 270 | writel(EP93XX_PWM_MAX_COUNT, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT); |
H Hartley Sweeten | aa91976 | 2013-05-24 16:23:38 -0700 | [diff] [blame] | 271 | writel(EP93XX_PWM_MAX_COUNT/2, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 272 | |
| 273 | clk_enable(pwm->clk); |
| 274 | |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 275 | platform_set_drvdata(pdev, pwm); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 276 | return 0; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static int __exit ep93xx_pwm_remove(struct platform_device *pdev) |
| 280 | { |
| 281 | struct ep93xx_pwm *pwm = platform_get_drvdata(pdev); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 282 | |
H Hartley Sweeten | 02846b9 | 2013-05-24 16:24:51 -0700 | [diff] [blame] | 283 | writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 284 | clk_disable(pwm->clk); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 285 | sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 286 | ep93xx_pwm_release_gpio(pdev); |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static 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 Han | ead050d | 2013-03-05 11:04:07 +0900 | [diff] [blame] | 299 | module_platform_driver_probe(ep93xx_pwm_driver, ep93xx_pwm_probe); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 300 | |
| 301 | MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>, " |
| 302 | "H Hartley Sweeten <hsweeten@visionengravers.com>"); |
| 303 | MODULE_DESCRIPTION("EP93xx PWM driver"); |
| 304 | MODULE_LICENSE("GPL"); |
| 305 | MODULE_ALIAS("platform:ep93xx-pwm"); |