Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * simple driver for PWM (Pulse Width Modulator) controller |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/pwm.h> |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 18 | #include <mach/hardware.h> |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 19 | |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 20 | |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 21 | /* i.MX1 and i.MX21 share the same PWM function block: */ |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 22 | |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 23 | #define MX1_PWMC 0x00 /* PWM Control Register */ |
| 24 | #define MX1_PWMS 0x04 /* PWM Sample Register */ |
| 25 | #define MX1_PWMP 0x08 /* PWM Period Register */ |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 26 | |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 27 | |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 28 | /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */ |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 29 | |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 30 | #define MX3_PWMCR 0x00 /* PWM Control Register */ |
| 31 | #define MX3_PWMSAR 0x0C /* PWM Sample Register */ |
| 32 | #define MX3_PWMPR 0x10 /* PWM Period Register */ |
| 33 | #define MX3_PWMCR_PRESCALER(x) (((x - 1) & 0xFFF) << 4) |
| 34 | #define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16) |
Sascha Hauer | a058cbc | 2009-06-19 11:08:07 +0200 | [diff] [blame] | 35 | #define MX3_PWMCR_CLKSRC_IPG (1 << 16) |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 36 | #define MX3_PWMCR_EN (1 << 0) |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 37 | |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 38 | |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 39 | |
| 40 | struct pwm_device { |
| 41 | struct list_head node; |
| 42 | struct platform_device *pdev; |
| 43 | |
| 44 | const char *label; |
| 45 | struct clk *clk; |
| 46 | |
| 47 | int clk_enabled; |
| 48 | void __iomem *mmio_base; |
| 49 | |
| 50 | unsigned int use_count; |
| 51 | unsigned int pwm_id; |
| 52 | }; |
| 53 | |
| 54 | int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) |
| 55 | { |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 56 | if (pwm == NULL || period_ns == 0 || duty_ns > period_ns) |
| 57 | return -EINVAL; |
| 58 | |
Sascha Hauer | a058cbc | 2009-06-19 11:08:07 +0200 | [diff] [blame] | 59 | if (cpu_is_mx27() || cpu_is_mx3() || cpu_is_mx25()) { |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 60 | unsigned long long c; |
| 61 | unsigned long period_cycles, duty_cycles, prescale; |
Sascha Hauer | a058cbc | 2009-06-19 11:08:07 +0200 | [diff] [blame] | 62 | u32 cr; |
| 63 | |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 64 | c = clk_get_rate(pwm->clk); |
| 65 | c = c * period_ns; |
| 66 | do_div(c, 1000000000); |
| 67 | period_cycles = c; |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 68 | |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 69 | prescale = period_cycles / 0x10000 + 1; |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 70 | |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 71 | period_cycles /= prescale; |
| 72 | c = (unsigned long long)period_cycles * duty_ns; |
| 73 | do_div(c, period_ns); |
| 74 | duty_cycles = c; |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 75 | |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 76 | writel(duty_cycles, pwm->mmio_base + MX3_PWMSAR); |
| 77 | writel(period_cycles, pwm->mmio_base + MX3_PWMPR); |
Sascha Hauer | a058cbc | 2009-06-19 11:08:07 +0200 | [diff] [blame] | 78 | |
| 79 | cr = MX3_PWMCR_PRESCALER(prescale) | MX3_PWMCR_EN; |
| 80 | |
| 81 | if (cpu_is_mx25()) |
| 82 | cr |= MX3_PWMCR_CLKSRC_IPG; |
| 83 | else |
| 84 | cr |= MX3_PWMCR_CLKSRC_IPG_HIGH; |
| 85 | |
| 86 | writel(cr, pwm->mmio_base + MX3_PWMCR); |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 87 | } else if (cpu_is_mx1() || cpu_is_mx21()) { |
| 88 | /* The PWM subsystem allows for exact frequencies. However, |
| 89 | * I cannot connect a scope on my device to the PWM line and |
| 90 | * thus cannot provide the program the PWM controller |
| 91 | * exactly. Instead, I'm relying on the fact that the |
| 92 | * Bootloader (u-boot or WinCE+haret) has programmed the PWM |
| 93 | * function group already. So I'll just modify the PWM sample |
| 94 | * register to follow the ratio of duty_ns vs. period_ns |
| 95 | * accordingly. |
| 96 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 97 | * This is good enough for programming the brightness of |
Holger Schurig | c010dba | 2009-04-14 12:50:20 +0200 | [diff] [blame] | 98 | * the LCD backlight. |
| 99 | * |
| 100 | * The real implementation would divide PERCLK[0] first by |
| 101 | * both the prescaler (/1 .. /128) and then by CLKSEL |
| 102 | * (/2 .. /16). |
| 103 | */ |
| 104 | u32 max = readl(pwm->mmio_base + MX1_PWMP); |
| 105 | u32 p = max * duty_ns / period_ns; |
| 106 | writel(max - p, pwm->mmio_base + MX1_PWMS); |
| 107 | } else { |
| 108 | BUG(); |
| 109 | } |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | EXPORT_SYMBOL(pwm_config); |
| 114 | |
| 115 | int pwm_enable(struct pwm_device *pwm) |
| 116 | { |
| 117 | int rc = 0; |
| 118 | |
| 119 | if (!pwm->clk_enabled) { |
| 120 | rc = clk_enable(pwm->clk); |
| 121 | if (!rc) |
| 122 | pwm->clk_enabled = 1; |
| 123 | } |
| 124 | return rc; |
| 125 | } |
| 126 | EXPORT_SYMBOL(pwm_enable); |
| 127 | |
| 128 | void pwm_disable(struct pwm_device *pwm) |
| 129 | { |
Sascha Hauer | 5ef881b | 2009-06-19 11:35:08 +0200 | [diff] [blame] | 130 | writel(0, pwm->mmio_base + MX3_PWMCR); |
| 131 | |
Sascha Hauer | 166091b | 2009-01-16 15:17:16 +0100 | [diff] [blame] | 132 | if (pwm->clk_enabled) { |
| 133 | clk_disable(pwm->clk); |
| 134 | pwm->clk_enabled = 0; |
| 135 | } |
| 136 | } |
| 137 | EXPORT_SYMBOL(pwm_disable); |
| 138 | |
| 139 | static DEFINE_MUTEX(pwm_lock); |
| 140 | static LIST_HEAD(pwm_list); |
| 141 | |
| 142 | struct pwm_device *pwm_request(int pwm_id, const char *label) |
| 143 | { |
| 144 | struct pwm_device *pwm; |
| 145 | int found = 0; |
| 146 | |
| 147 | mutex_lock(&pwm_lock); |
| 148 | |
| 149 | list_for_each_entry(pwm, &pwm_list, node) { |
| 150 | if (pwm->pwm_id == pwm_id) { |
| 151 | found = 1; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (found) { |
| 157 | if (pwm->use_count == 0) { |
| 158 | pwm->use_count++; |
| 159 | pwm->label = label; |
| 160 | } else |
| 161 | pwm = ERR_PTR(-EBUSY); |
| 162 | } else |
| 163 | pwm = ERR_PTR(-ENOENT); |
| 164 | |
| 165 | mutex_unlock(&pwm_lock); |
| 166 | return pwm; |
| 167 | } |
| 168 | EXPORT_SYMBOL(pwm_request); |
| 169 | |
| 170 | void pwm_free(struct pwm_device *pwm) |
| 171 | { |
| 172 | mutex_lock(&pwm_lock); |
| 173 | |
| 174 | if (pwm->use_count) { |
| 175 | pwm->use_count--; |
| 176 | pwm->label = NULL; |
| 177 | } else |
| 178 | pr_warning("PWM device already freed\n"); |
| 179 | |
| 180 | mutex_unlock(&pwm_lock); |
| 181 | } |
| 182 | EXPORT_SYMBOL(pwm_free); |
| 183 | |
| 184 | static int __devinit mxc_pwm_probe(struct platform_device *pdev) |
| 185 | { |
| 186 | struct pwm_device *pwm; |
| 187 | struct resource *r; |
| 188 | int ret = 0; |
| 189 | |
| 190 | pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL); |
| 191 | if (pwm == NULL) { |
| 192 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
| 193 | return -ENOMEM; |
| 194 | } |
| 195 | |
| 196 | pwm->clk = clk_get(&pdev->dev, "pwm"); |
| 197 | |
| 198 | if (IS_ERR(pwm->clk)) { |
| 199 | ret = PTR_ERR(pwm->clk); |
| 200 | goto err_free; |
| 201 | } |
| 202 | |
| 203 | pwm->clk_enabled = 0; |
| 204 | |
| 205 | pwm->use_count = 0; |
| 206 | pwm->pwm_id = pdev->id; |
| 207 | pwm->pdev = pdev; |
| 208 | |
| 209 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 210 | if (r == NULL) { |
| 211 | dev_err(&pdev->dev, "no memory resource defined\n"); |
| 212 | ret = -ENODEV; |
| 213 | goto err_free_clk; |
| 214 | } |
| 215 | |
| 216 | r = request_mem_region(r->start, r->end - r->start + 1, pdev->name); |
| 217 | if (r == NULL) { |
| 218 | dev_err(&pdev->dev, "failed to request memory resource\n"); |
| 219 | ret = -EBUSY; |
| 220 | goto err_free_clk; |
| 221 | } |
| 222 | |
| 223 | pwm->mmio_base = ioremap(r->start, r->end - r->start + 1); |
| 224 | if (pwm->mmio_base == NULL) { |
| 225 | dev_err(&pdev->dev, "failed to ioremap() registers\n"); |
| 226 | ret = -ENODEV; |
| 227 | goto err_free_mem; |
| 228 | } |
| 229 | |
| 230 | mutex_lock(&pwm_lock); |
| 231 | list_add_tail(&pwm->node, &pwm_list); |
| 232 | mutex_unlock(&pwm_lock); |
| 233 | |
| 234 | platform_set_drvdata(pdev, pwm); |
| 235 | return 0; |
| 236 | |
| 237 | err_free_mem: |
| 238 | release_mem_region(r->start, r->end - r->start + 1); |
| 239 | err_free_clk: |
| 240 | clk_put(pwm->clk); |
| 241 | err_free: |
| 242 | kfree(pwm); |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | static int __devexit mxc_pwm_remove(struct platform_device *pdev) |
| 247 | { |
| 248 | struct pwm_device *pwm; |
| 249 | struct resource *r; |
| 250 | |
| 251 | pwm = platform_get_drvdata(pdev); |
| 252 | if (pwm == NULL) |
| 253 | return -ENODEV; |
| 254 | |
| 255 | mutex_lock(&pwm_lock); |
| 256 | list_del(&pwm->node); |
| 257 | mutex_unlock(&pwm_lock); |
| 258 | |
| 259 | iounmap(pwm->mmio_base); |
| 260 | |
| 261 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 262 | release_mem_region(r->start, r->end - r->start + 1); |
| 263 | |
| 264 | clk_put(pwm->clk); |
| 265 | |
| 266 | kfree(pwm); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static struct platform_driver mxc_pwm_driver = { |
| 271 | .driver = { |
| 272 | .name = "mxc_pwm", |
| 273 | }, |
| 274 | .probe = mxc_pwm_probe, |
| 275 | .remove = __devexit_p(mxc_pwm_remove), |
| 276 | }; |
| 277 | |
| 278 | static int __init mxc_pwm_init(void) |
| 279 | { |
| 280 | return platform_driver_register(&mxc_pwm_driver); |
| 281 | } |
| 282 | arch_initcall(mxc_pwm_init); |
| 283 | |
| 284 | static void __exit mxc_pwm_exit(void) |
| 285 | { |
| 286 | platform_driver_unregister(&mxc_pwm_driver); |
| 287 | } |
| 288 | module_exit(mxc_pwm_exit); |
| 289 | |
| 290 | MODULE_LICENSE("GPL v2"); |
| 291 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |