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