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 int ep93xx_pwm_is_inverted(struct ep93xx_pwm *pwm) |
| 53 | { |
H Hartley Sweeten | a39ca27 | 2013-05-24 16:22:30 -0700 | [diff] [blame] | 54 | return readl(pwm->mmio_base + EP93XX_PWMx_INVERT) & 0x1; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 55 | } |
| 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 | |
| 66 | static 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 | |
| 76 | static 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 | |
| 86 | static 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 | |
| 102 | static 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 Sweeten | 02846b9 | 2013-05-24 16:24:51 -0700 | [diff] [blame] | 115 | writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 116 | } 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 Sweeten | 53e2e38 | 2013-05-24 16:23:00 -0700 | [diff] [blame] | 130 | writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT); |
H Hartley Sweeten | aa91976 | 2013-05-24 16:23:38 -0700 | [diff] [blame] | 131 | writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 132 | } else { |
H Hartley Sweeten | aa91976 | 2013-05-24 16:23:38 -0700 | [diff] [blame] | 133 | writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE); |
H Hartley Sweeten | 53e2e38 | 2013-05-24 16:23:00 -0700 | [diff] [blame] | 134 | writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | if (!ep93xx_pwm_is_enabled(pwm)) |
H Hartley Sweeten | ac91b96 | 2013-05-24 16:24:16 -0700 | [diff] [blame] | 138 | writel(0x1, pwm->mmio_base + EP93XX_PWMx_ENABLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 139 | } else { |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | return count; |
| 144 | } |
| 145 | |
| 146 | static 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 | |
| 155 | static 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 Sweeten | aa91976 | 2013-05-24 16:23:38 -0700 | [diff] [blame] | 169 | u32 duty = ((term + 1) * val / 100) - 1; |
| 170 | |
| 171 | writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 172 | pwm->duty_percent = val; |
| 173 | return count; |
| 174 | } |
| 175 | |
| 176 | return -EINVAL; |
| 177 | } |
| 178 | |
| 179 | static 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 | |
| 188 | static 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 Sweeten | d98d790 | 2013-05-24 16:26:11 -0700 | [diff] [blame^] | 201 | writel(0x0, pwm->mmio_base + EP93XX_PWMx_INVERT); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 202 | else if (val == 1) |
H Hartley Sweeten | 47a36ee | 2013-05-24 16:25:23 -0700 | [diff] [blame] | 203 | writel(0x1, pwm->mmio_base + EP93XX_PWMx_INVERT); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 204 | else |
| 205 | return -EINVAL; |
| 206 | |
| 207 | return count; |
| 208 | } |
| 209 | |
| 210 | static DEVICE_ATTR(min_freq, S_IRUGO, ep93xx_pwm_get_min_freq, NULL); |
| 211 | 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] | 212 | static DEVICE_ATTR(freq, S_IWUSR | S_IRUGO, |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 213 | ep93xx_pwm_get_freq, ep93xx_pwm_set_freq); |
Vasiliy Kulikov | deb187e | 2011-03-22 16:34:01 -0700 | [diff] [blame] | 214 | static DEVICE_ATTR(duty_percent, S_IWUSR | S_IRUGO, |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 215 | ep93xx_pwm_get_duty_percent, ep93xx_pwm_set_duty_percent); |
Vasiliy Kulikov | deb187e | 2011-03-22 16:34:01 -0700 | [diff] [blame] | 216 | static DEVICE_ATTR(invert, S_IWUSR | S_IRUGO, |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 217 | ep93xx_pwm_get_invert, ep93xx_pwm_set_invert); |
| 218 | |
| 219 | static 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 | |
| 228 | static const struct attribute_group ep93xx_pwm_sysfs_files = { |
| 229 | .attrs = ep93xx_pwm_attrs, |
| 230 | }; |
| 231 | |
| 232 | static int __init ep93xx_pwm_probe(struct platform_device *pdev) |
| 233 | { |
| 234 | struct ep93xx_pwm *pwm; |
| 235 | struct resource *res; |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 236 | int ret; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 237 | |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 238 | pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); |
| 239 | if (!pwm) |
| 240 | return -ENOMEM; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 241 | |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 242 | pwm->clk = devm_clk_get(&pdev->dev, "pwm_clk"); |
| 243 | if (IS_ERR(pwm->clk)) |
| 244 | return PTR_ERR(pwm->clk); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 245 | |
| 246 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 247 | pwm->mmio_base = devm_ioremap_resource(&pdev->dev, res); |
| 248 | if (IS_ERR(pwm->mmio_base)) |
| 249 | return PTR_ERR(pwm->mmio_base); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 250 | |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 251 | ret = ep93xx_pwm_acquire_gpio(pdev); |
| 252 | if (ret) |
| 253 | return ret; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 254 | |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 255 | ret = sysfs_create_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files); |
| 256 | if (ret) { |
| 257 | ep93xx_pwm_release_gpio(pdev); |
| 258 | return ret; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | pwm->duty_percent = 50; |
| 262 | |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 263 | /* disable pwm at startup. Avoids zero value. */ |
H Hartley Sweeten | 02846b9 | 2013-05-24 16:24:51 -0700 | [diff] [blame] | 264 | writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE); |
H Hartley Sweeten | 53e2e38 | 2013-05-24 16:23:00 -0700 | [diff] [blame] | 265 | 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] | 266 | 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] | 267 | |
| 268 | clk_enable(pwm->clk); |
| 269 | |
H Hartley Sweeten | 6c7dd64 | 2013-05-24 16:21:01 -0700 | [diff] [blame] | 270 | platform_set_drvdata(pdev, pwm); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 271 | return 0; |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static int __exit ep93xx_pwm_remove(struct platform_device *pdev) |
| 275 | { |
| 276 | struct ep93xx_pwm *pwm = platform_get_drvdata(pdev); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 277 | |
H Hartley Sweeten | 02846b9 | 2013-05-24 16:24:51 -0700 | [diff] [blame] | 278 | writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 279 | clk_disable(pwm->clk); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 280 | sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 281 | ep93xx_pwm_release_gpio(pdev); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static 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 Han | ead050d | 2013-03-05 11:04:07 +0900 | [diff] [blame] | 294 | module_platform_driver_probe(ep93xx_pwm_driver, ep93xx_pwm_probe); |
Hartley Sweeten | ef12379 | 2009-07-29 22:41:06 +0100 | [diff] [blame] | 295 | |
| 296 | MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>, " |
| 297 | "H Hartley Sweeten <hsweeten@visionengravers.com>"); |
| 298 | MODULE_DESCRIPTION("EP93xx PWM driver"); |
| 299 | MODULE_LICENSE("GPL"); |
| 300 | MODULE_ALIAS("platform:ep93xx-pwm"); |