linux/arch/arm/plat-s3c24xx/pwm.c
<<
>>
Prefs
   1/* arch/arm/plat-s3c24xx/pwm.c
   2 *
   3 * Copyright (c) 2007 Ben Dooks
   4 * Copyright (c) 2008 Simtec Electronics
   5 *      Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
   6 *
   7 * S3C24XX PWM device core
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; either version 2 of the License.
  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 <mach/irqs.h>
  23
  24#include <plat/devs.h>
  25#include <plat/regs-timer.h>
  26
  27struct pwm_device {
  28        struct list_head         list;
  29        struct platform_device  *pdev;
  30
  31        struct clk              *clk_div;
  32        struct clk              *clk;
  33        const char              *label;
  34
  35        unsigned int             period_ns;
  36        unsigned int             duty_ns;
  37
  38        unsigned char            tcon_base;
  39        unsigned char            running;
  40        unsigned char            use_count;
  41        unsigned char            pwm_id;
  42};
  43
  44#define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
  45
  46static struct clk *clk_scaler[2];
  47
  48/* Standard setup for a timer block. */
  49
  50#define TIMER_RESOURCE_SIZE (1)
  51
  52#define TIMER_RESOURCE(_tmr, _irq)                      \
  53        (struct resource [TIMER_RESOURCE_SIZE]) {       \
  54                [0] = {                                 \
  55                        .start  = _irq,                 \
  56                        .end    = _irq,                 \
  57                        .flags  = IORESOURCE_IRQ        \
  58                }                                       \
  59        }
  60
  61#define DEFINE_S3C_TIMER(_tmr_no, _irq)                 \
  62        .name           = "s3c24xx-pwm",                \
  63        .id             = _tmr_no,                      \
  64        .num_resources  = TIMER_RESOURCE_SIZE,          \
  65        .resource       = TIMER_RESOURCE(_tmr_no, _irq),        \
  66
  67/* since we already have an static mapping for the timer, we do not
  68 * bother setting any IO resource for the base.
  69 */
  70
  71struct platform_device s3c_device_timer[] = {
  72        [0] = { DEFINE_S3C_TIMER(0, IRQ_TIMER0) },
  73        [1] = { DEFINE_S3C_TIMER(1, IRQ_TIMER1) },
  74        [2] = { DEFINE_S3C_TIMER(2, IRQ_TIMER2) },
  75        [3] = { DEFINE_S3C_TIMER(3, IRQ_TIMER3) },
  76        [4] = { DEFINE_S3C_TIMER(4, IRQ_TIMER4) },
  77};
  78
  79static inline int pwm_is_tdiv(struct pwm_device *pwm)
  80{
  81        return clk_get_parent(pwm->clk) == pwm->clk_div;
  82}
  83
  84static DEFINE_MUTEX(pwm_lock);
  85static LIST_HEAD(pwm_list);
  86
  87struct pwm_device *pwm_request(int pwm_id, const char *label)
  88{
  89        struct pwm_device *pwm;
  90        int found = 0;
  91
  92        mutex_lock(&pwm_lock);
  93
  94        list_for_each_entry(pwm, &pwm_list, list) {
  95                if (pwm->pwm_id == pwm_id) {
  96                        found = 1;
  97                        break;
  98                }
  99        }
 100
 101        if (found) {
 102                if (pwm->use_count == 0) {
 103                        pwm->use_count = 1;
 104                        pwm->label = label;
 105                } else
 106                        pwm = ERR_PTR(-EBUSY);
 107        } else
 108                pwm = ERR_PTR(-ENOENT);
 109
 110        mutex_unlock(&pwm_lock);
 111        return pwm;
 112}
 113
 114EXPORT_SYMBOL(pwm_request);
 115
 116
 117void pwm_free(struct pwm_device *pwm)
 118{
 119        mutex_lock(&pwm_lock);
 120
 121        if (pwm->use_count) {
 122                pwm->use_count--;
 123                pwm->label = NULL;
 124        } else
 125                printk(KERN_ERR "PWM%d device already freed\n", pwm->pwm_id);
 126
 127        mutex_unlock(&pwm_lock);
 128}
 129
 130EXPORT_SYMBOL(pwm_free);
 131
 132#define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0))
 133#define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2))
 134#define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3))
 135#define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1))
 136
 137int pwm_enable(struct pwm_device *pwm)
 138{
 139        unsigned long flags;
 140        unsigned long tcon;
 141
 142        local_irq_save(flags);
 143
 144        tcon = __raw_readl(S3C2410_TCON);
 145        tcon |= pwm_tcon_start(pwm);
 146        __raw_writel(tcon, S3C2410_TCON);
 147
 148        local_irq_restore(flags);
 149
 150        pwm->running = 1;
 151        return 0;
 152}
 153
 154EXPORT_SYMBOL(pwm_enable);
 155
 156void pwm_disable(struct pwm_device *pwm)
 157{
 158        unsigned long flags;
 159        unsigned long tcon;
 160
 161        local_irq_save(flags);
 162
 163        tcon = __raw_readl(S3C2410_TCON);
 164        tcon &= ~pwm_tcon_start(pwm);
 165        __raw_writel(tcon, S3C2410_TCON);
 166
 167        local_irq_restore(flags);
 168
 169        pwm->running = 0;
 170}
 171
 172EXPORT_SYMBOL(pwm_disable);
 173
 174static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
 175{
 176        unsigned long tin_parent_rate;
 177        unsigned int div;
 178
 179        tin_parent_rate = clk_get_rate(clk_get_parent(pwm->clk_div));
 180        pwm_dbg(pwm, "tin parent at %lu\n", tin_parent_rate);
 181
 182        for (div = 2; div <= 16; div *= 2) {
 183                if ((tin_parent_rate / (div << 16)) < freq)
 184                        return tin_parent_rate / div;
 185        }
 186
 187        return tin_parent_rate / 16;
 188}
 189
 190#define NS_IN_HZ (1000000000UL)
 191
 192int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
 193{
 194        unsigned long tin_rate;
 195        unsigned long tin_ns;
 196        unsigned long period;
 197        unsigned long flags;
 198        unsigned long tcon;
 199        unsigned long tcnt;
 200        long tcmp;
 201
 202        /* We currently avoid using 64bit arithmetic by using the
 203         * fact that anything faster than 1Hz is easily representable
 204         * by 32bits. */
 205
 206        if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
 207                return -ERANGE;
 208
 209        if (duty_ns > period_ns)
 210                return -EINVAL;
 211
 212        if (period_ns == pwm->period_ns &&
 213            duty_ns == pwm->duty_ns)
 214                return 0;
 215
 216        /* The TCMP and TCNT can be read without a lock, they're not
 217         * shared between the timers. */
 218
 219        tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id));
 220        tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id));
 221
 222        period = NS_IN_HZ / period_ns;
 223
 224        pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n",
 225                duty_ns, period_ns, period);
 226
 227        /* Check to see if we are changing the clock rate of the PWM */
 228
 229        if (pwm->period_ns != period_ns) {
 230                if (pwm_is_tdiv(pwm)) {
 231                        tin_rate = pwm_calc_tin(pwm, period);
 232                        clk_set_rate(pwm->clk_div, tin_rate);
 233                } else
 234                        tin_rate = clk_get_rate(pwm->clk);
 235
 236                pwm->period_ns = period_ns;
 237
 238                pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate);
 239
 240                tin_ns = NS_IN_HZ / tin_rate;
 241                tcnt = period_ns / tin_ns;
 242        } else
 243                tin_ns = NS_IN_HZ / clk_get_rate(pwm->clk);
 244
 245        /* Note, counters count down */
 246
 247        tcmp = duty_ns / tin_ns;
 248        tcmp = tcnt - tcmp;
 249
 250        pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
 251
 252        if (tcmp < 0)
 253                tcmp = 0;
 254
 255        /* Update the PWM register block. */
 256
 257        local_irq_save(flags);
 258
 259        __raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id));
 260        __raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id));
 261
 262        tcon = __raw_readl(S3C2410_TCON);
 263        tcon |= pwm_tcon_manulupdate(pwm);
 264        tcon |= pwm_tcon_autoreload(pwm);
 265        __raw_writel(tcon, S3C2410_TCON);
 266
 267        tcon &= ~pwm_tcon_manulupdate(pwm);
 268        __raw_writel(tcon, S3C2410_TCON);
 269
 270        local_irq_restore(flags);
 271
 272        return 0;
 273}
 274
 275EXPORT_SYMBOL(pwm_config);
 276
 277static int pwm_register(struct pwm_device *pwm)
 278{
 279        pwm->duty_ns = -1;
 280        pwm->period_ns = -1;
 281
 282        mutex_lock(&pwm_lock);
 283        list_add_tail(&pwm->list, &pwm_list);
 284        mutex_unlock(&pwm_lock);
 285
 286        return 0;
 287}
 288
 289static int s3c_pwm_probe(struct platform_device *pdev)
 290{
 291        struct device *dev = &pdev->dev;
 292        struct pwm_device *pwm;
 293        unsigned long flags;
 294        unsigned long tcon;
 295        unsigned int id = pdev->id;
 296        int ret;
 297
 298        if (id == 4) {
 299                dev_err(dev, "TIMER4 is currently not supported\n");
 300                return -ENXIO;
 301        }
 302
 303        pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
 304        if (pwm == NULL) {
 305                dev_err(dev, "failed to allocate pwm_device\n");
 306                return -ENOMEM;
 307        }
 308
 309        pwm->pdev = pdev;
 310        pwm->pwm_id = id;
 311
 312        /* calculate base of control bits in TCON */
 313        pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
 314
 315        pwm->clk = clk_get(dev, "pwm-tin");
 316        if (IS_ERR(pwm->clk)) {
 317                dev_err(dev, "failed to get pwm tin clk\n");
 318                ret = PTR_ERR(pwm->clk);
 319                goto err_alloc;
 320        }
 321
 322        pwm->clk_div = clk_get(dev, "pwm-tdiv");
 323        if (IS_ERR(pwm->clk_div)) {
 324                dev_err(dev, "failed to get pwm tdiv clk\n");
 325                ret = PTR_ERR(pwm->clk_div);
 326                goto err_clk_tin;
 327        }
 328
 329        local_irq_save(flags);
 330
 331        tcon = __raw_readl(S3C2410_TCON);
 332        tcon |= pwm_tcon_invert(pwm);
 333        __raw_writel(tcon, S3C2410_TCON);
 334
 335        local_irq_restore(flags);
 336
 337
 338        ret = pwm_register(pwm);
 339        if (ret) {
 340                dev_err(dev, "failed to register pwm\n");
 341                goto err_clk_tdiv;
 342        }
 343
 344        pwm_dbg(pwm, "config bits %02x\n",
 345                (__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f);
 346
 347        dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
 348                 clk_get_rate(pwm->clk),
 349                 clk_get_rate(pwm->clk_div),
 350                 pwm_is_tdiv(pwm) ? "div" : "ext", pwm->tcon_base);
 351
 352        platform_set_drvdata(pdev, pwm);
 353        return 0;
 354
 355 err_clk_tdiv:
 356        clk_put(pwm->clk_div);
 357
 358 err_clk_tin:
 359        clk_put(pwm->clk);
 360
 361 err_alloc:
 362        kfree(pwm);
 363        return ret;
 364}
 365
 366static int s3c_pwm_remove(struct platform_device *pdev)
 367{
 368        struct pwm_device *pwm = platform_get_drvdata(pdev);
 369
 370        clk_put(pwm->clk_div);
 371        clk_put(pwm->clk);
 372        kfree(pwm);
 373
 374        return 0;
 375}
 376
 377static struct platform_driver s3c_pwm_driver = {
 378        .driver         = {
 379                .name   = "s3c24xx-pwm",
 380                .owner  = THIS_MODULE,
 381        },
 382        .probe          = s3c_pwm_probe,
 383        .remove         = __devexit_p(s3c_pwm_remove),
 384};
 385
 386static int __init pwm_init(void)
 387{
 388        int ret;
 389
 390        clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
 391        clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
 392
 393        if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
 394                printk(KERN_ERR "%s: failed to get scaler clocks\n", __func__);
 395                return -EINVAL;
 396        }
 397
 398        ret = platform_driver_register(&s3c_pwm_driver);
 399        if (ret)
 400                printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
 401
 402        return ret;
 403}
 404
 405arch_initcall(pwm_init);
 406
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.