eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mach-pxa/pwm.c |
| 3 | * |
| 4 | * simple driver for PWM (Pulse Width Modulator) controller |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * 2008-02-13 initial version |
| 11 | * eric miao <eric.miao@marvell.com> |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 18 | #include <linux/err.h> |
| 19 | #include <linux/clk.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/pwm.h> |
| 22 | |
| 23 | #include <asm/div64.h> |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 24 | |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 25 | #define HAS_SECONDARY_PWM 0x10 |
Eric Miao | a757ad8 | 2009-04-13 18:51:31 +0800 | [diff] [blame] | 26 | #define PWM_ID_BASE(d) ((d) & 0xf) |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 27 | |
| 28 | static const struct platform_device_id pwm_id_table[] = { |
| 29 | /* PWM has_secondary_pwm? */ |
| 30 | { "pxa25x-pwm", 0 }, |
Eric Miao | a757ad8 | 2009-04-13 18:51:31 +0800 | [diff] [blame] | 31 | { "pxa27x-pwm", 0 | HAS_SECONDARY_PWM }, |
Eric Miao | a27ba76 | 2009-04-13 18:29:52 +0800 | [diff] [blame] | 32 | { "pxa168-pwm", 1 }, |
| 33 | { "pxa910-pwm", 1 }, |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 34 | { }, |
| 35 | }; |
| 36 | MODULE_DEVICE_TABLE(platform, pwm_id_table); |
| 37 | |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 38 | /* PWM registers and bits definitions */ |
| 39 | #define PWMCR (0x00) |
| 40 | #define PWMDCR (0x04) |
| 41 | #define PWMPCR (0x08) |
| 42 | |
| 43 | #define PWMCR_SD (1 << 6) |
| 44 | #define PWMDCR_FD (1 << 10) |
| 45 | |
| 46 | struct pwm_device { |
| 47 | struct list_head node; |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 48 | struct pwm_device *secondary; |
| 49 | struct platform_device *pdev; |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 50 | |
| 51 | const char *label; |
| 52 | struct clk *clk; |
Robert Jarzmik | c860d70 | 2008-06-10 23:02:31 +0100 | [diff] [blame] | 53 | int clk_enabled; |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 54 | void __iomem *mmio_base; |
| 55 | |
| 56 | unsigned int use_count; |
| 57 | unsigned int pwm_id; |
| 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE |
| 62 | * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE |
| 63 | */ |
| 64 | int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) |
| 65 | { |
| 66 | unsigned long long c; |
| 67 | unsigned long period_cycles, prescale, pv, dc; |
| 68 | |
| 69 | if (pwm == NULL || period_ns == 0 || duty_ns > period_ns) |
| 70 | return -EINVAL; |
| 71 | |
| 72 | c = clk_get_rate(pwm->clk); |
| 73 | c = c * period_ns; |
| 74 | do_div(c, 1000000000); |
| 75 | period_cycles = c; |
| 76 | |
roelkluin | 71a35d7 | 2008-10-14 21:28:51 +0100 | [diff] [blame] | 77 | if (period_cycles < 1) |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 78 | period_cycles = 1; |
| 79 | prescale = (period_cycles - 1) / 1024; |
| 80 | pv = period_cycles / (prescale + 1) - 1; |
| 81 | |
| 82 | if (prescale > 63) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | if (duty_ns == period_ns) |
| 86 | dc = PWMDCR_FD; |
| 87 | else |
| 88 | dc = (pv + 1) * duty_ns / period_ns; |
| 89 | |
| 90 | /* NOTE: the clock to PWM has to be enabled first |
| 91 | * before writing to the registers |
| 92 | */ |
| 93 | clk_enable(pwm->clk); |
| 94 | __raw_writel(prescale, pwm->mmio_base + PWMCR); |
| 95 | __raw_writel(dc, pwm->mmio_base + PWMDCR); |
| 96 | __raw_writel(pv, pwm->mmio_base + PWMPCR); |
| 97 | clk_disable(pwm->clk); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | EXPORT_SYMBOL(pwm_config); |
| 102 | |
| 103 | int pwm_enable(struct pwm_device *pwm) |
| 104 | { |
Robert Jarzmik | c860d70 | 2008-06-10 23:02:31 +0100 | [diff] [blame] | 105 | int rc = 0; |
| 106 | |
| 107 | if (!pwm->clk_enabled) { |
| 108 | rc = clk_enable(pwm->clk); |
| 109 | if (!rc) |
| 110 | pwm->clk_enabled = 1; |
| 111 | } |
| 112 | return rc; |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 113 | } |
| 114 | EXPORT_SYMBOL(pwm_enable); |
| 115 | |
| 116 | void pwm_disable(struct pwm_device *pwm) |
| 117 | { |
Robert Jarzmik | c860d70 | 2008-06-10 23:02:31 +0100 | [diff] [blame] | 118 | if (pwm->clk_enabled) { |
| 119 | clk_disable(pwm->clk); |
| 120 | pwm->clk_enabled = 0; |
| 121 | } |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 122 | } |
| 123 | EXPORT_SYMBOL(pwm_disable); |
| 124 | |
| 125 | static DEFINE_MUTEX(pwm_lock); |
| 126 | static LIST_HEAD(pwm_list); |
| 127 | |
| 128 | struct pwm_device *pwm_request(int pwm_id, const char *label) |
| 129 | { |
| 130 | struct pwm_device *pwm; |
| 131 | int found = 0; |
| 132 | |
| 133 | mutex_lock(&pwm_lock); |
| 134 | |
| 135 | list_for_each_entry(pwm, &pwm_list, node) { |
Ben Dooks | 43bda1a | 2008-07-01 14:18:27 +0100 | [diff] [blame] | 136 | if (pwm->pwm_id == pwm_id) { |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 137 | found = 1; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
Ben Dooks | 43bda1a | 2008-07-01 14:18:27 +0100 | [diff] [blame] | 142 | if (found) { |
| 143 | if (pwm->use_count == 0) { |
| 144 | pwm->use_count++; |
| 145 | pwm->label = label; |
| 146 | } else |
| 147 | pwm = ERR_PTR(-EBUSY); |
| 148 | } else |
| 149 | pwm = ERR_PTR(-ENOENT); |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 150 | |
Ben Dooks | 43bda1a | 2008-07-01 14:18:27 +0100 | [diff] [blame] | 151 | mutex_unlock(&pwm_lock); |
| 152 | return pwm; |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 153 | } |
| 154 | EXPORT_SYMBOL(pwm_request); |
| 155 | |
| 156 | void pwm_free(struct pwm_device *pwm) |
| 157 | { |
| 158 | mutex_lock(&pwm_lock); |
| 159 | |
| 160 | if (pwm->use_count) { |
| 161 | pwm->use_count--; |
| 162 | pwm->label = NULL; |
| 163 | } else |
| 164 | pr_warning("PWM device already freed\n"); |
| 165 | |
| 166 | mutex_unlock(&pwm_lock); |
| 167 | } |
| 168 | EXPORT_SYMBOL(pwm_free); |
| 169 | |
| 170 | static inline void __add_pwm(struct pwm_device *pwm) |
| 171 | { |
| 172 | mutex_lock(&pwm_lock); |
| 173 | list_add_tail(&pwm->node, &pwm_list); |
| 174 | mutex_unlock(&pwm_lock); |
| 175 | } |
| 176 | |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 177 | static int __devinit pwm_probe(struct platform_device *pdev) |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 178 | { |
Uwe Kleine-König | b3282ab | 2010-08-12 08:18:28 +0200 | [diff] [blame] | 179 | const struct platform_device_id *id = platform_get_device_id(pdev); |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 180 | struct pwm_device *pwm, *secondary = NULL; |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 181 | struct resource *r; |
| 182 | int ret = 0; |
| 183 | |
| 184 | pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL); |
| 185 | if (pwm == NULL) { |
| 186 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 187 | return -ENOMEM; |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Russell King | e0d8b13 | 2008-11-11 17:52:32 +0000 | [diff] [blame] | 190 | pwm->clk = clk_get(&pdev->dev, NULL); |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 191 | if (IS_ERR(pwm->clk)) { |
| 192 | ret = PTR_ERR(pwm->clk); |
| 193 | goto err_free; |
| 194 | } |
Robert Jarzmik | c860d70 | 2008-06-10 23:02:31 +0100 | [diff] [blame] | 195 | pwm->clk_enabled = 0; |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 196 | |
| 197 | pwm->use_count = 0; |
Eric Miao | a757ad8 | 2009-04-13 18:51:31 +0800 | [diff] [blame] | 198 | pwm->pwm_id = PWM_ID_BASE(id->driver_data) + pdev->id; |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 199 | pwm->pdev = pdev; |
| 200 | |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 201 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 202 | if (r == NULL) { |
| 203 | dev_err(&pdev->dev, "no memory resource defined\n"); |
| 204 | ret = -ENODEV; |
| 205 | goto err_free_clk; |
| 206 | } |
| 207 | |
H Hartley Sweeten | 1692231 | 2009-11-23 12:24:32 -0500 | [diff] [blame] | 208 | r = request_mem_region(r->start, resource_size(r), pdev->name); |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 209 | if (r == NULL) { |
| 210 | dev_err(&pdev->dev, "failed to request memory resource\n"); |
| 211 | ret = -EBUSY; |
| 212 | goto err_free_clk; |
| 213 | } |
| 214 | |
H Hartley Sweeten | 1692231 | 2009-11-23 12:24:32 -0500 | [diff] [blame] | 215 | pwm->mmio_base = ioremap(r->start, resource_size(r)); |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 216 | if (pwm->mmio_base == NULL) { |
| 217 | dev_err(&pdev->dev, "failed to ioremap() registers\n"); |
| 218 | ret = -ENODEV; |
| 219 | goto err_free_mem; |
| 220 | } |
| 221 | |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 222 | if (id->driver_data & HAS_SECONDARY_PWM) { |
| 223 | secondary = kzalloc(sizeof(struct pwm_device), GFP_KERNEL); |
| 224 | if (secondary == NULL) { |
| 225 | ret = -ENOMEM; |
| 226 | goto err_free_mem; |
| 227 | } |
| 228 | |
| 229 | *secondary = *pwm; |
| 230 | pwm->secondary = secondary; |
| 231 | |
| 232 | /* registers for the second PWM has offset of 0x10 */ |
| 233 | secondary->mmio_base = pwm->mmio_base + 0x10; |
| 234 | secondary->pwm_id = pdev->id + 2; |
| 235 | } |
| 236 | |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 237 | __add_pwm(pwm); |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 238 | if (secondary) |
| 239 | __add_pwm(secondary); |
| 240 | |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 241 | platform_set_drvdata(pdev, pwm); |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 242 | return 0; |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 243 | |
| 244 | err_free_mem: |
H Hartley Sweeten | 1692231 | 2009-11-23 12:24:32 -0500 | [diff] [blame] | 245 | release_mem_region(r->start, resource_size(r)); |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 246 | err_free_clk: |
| 247 | clk_put(pwm->clk); |
| 248 | err_free: |
| 249 | kfree(pwm); |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 250 | return ret; |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static int __devexit pwm_remove(struct platform_device *pdev) |
| 254 | { |
| 255 | struct pwm_device *pwm; |
| 256 | struct resource *r; |
| 257 | |
| 258 | pwm = platform_get_drvdata(pdev); |
| 259 | if (pwm == NULL) |
| 260 | return -ENODEV; |
| 261 | |
| 262 | mutex_lock(&pwm_lock); |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 263 | |
| 264 | if (pwm->secondary) { |
| 265 | list_del(&pwm->secondary->node); |
| 266 | kfree(pwm->secondary); |
| 267 | } |
| 268 | |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 269 | list_del(&pwm->node); |
| 270 | mutex_unlock(&pwm_lock); |
| 271 | |
| 272 | iounmap(pwm->mmio_base); |
| 273 | |
| 274 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
H Hartley Sweeten | 1692231 | 2009-11-23 12:24:32 -0500 | [diff] [blame] | 275 | release_mem_region(r->start, resource_size(r)); |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 276 | |
| 277 | clk_put(pwm->clk); |
| 278 | kfree(pwm); |
| 279 | return 0; |
| 280 | } |
| 281 | |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 282 | static struct platform_driver pwm_driver = { |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 283 | .driver = { |
| 284 | .name = "pxa25x-pwm", |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 285 | .owner = THIS_MODULE, |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 286 | }, |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 287 | .probe = pwm_probe, |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 288 | .remove = __devexit_p(pwm_remove), |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 289 | .id_table = pwm_id_table, |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | static int __init pwm_init(void) |
| 293 | { |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 294 | return platform_driver_register(&pwm_driver); |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 295 | } |
| 296 | arch_initcall(pwm_init); |
| 297 | |
| 298 | static void __exit pwm_exit(void) |
| 299 | { |
Eric Miao | 3d2a98c | 2009-04-13 15:59:03 +0800 | [diff] [blame] | 300 | platform_driver_unregister(&pwm_driver); |
eric miao | 75540c1 | 2008-04-13 21:44:04 +0100 | [diff] [blame] | 301 | } |
| 302 | module_exit(pwm_exit); |
Guennadi Liakhovetski | b5f0228 | 2008-06-05 10:45:02 +0100 | [diff] [blame] | 303 | |
| 304 | MODULE_LICENSE("GPL v2"); |