x86: xen: size struct xen_spinlock to always fit in arch_spinlock_t
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / blackfin / kernel / pwm.c
blob33f5942733bdd103559ff82e0540061345629bc7
1 /*
2 * Blackfin Pulse Width Modulation (PWM) core
4 * Copyright (c) 2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/module.h>
10 #include <linux/pwm.h>
11 #include <linux/slab.h>
13 #include <asm/gptimers.h>
14 #include <asm/portmux.h>
16 struct pwm_device {
17 unsigned id;
18 unsigned short pin;
21 static const unsigned short pwm_to_gptimer_per[] = {
22 P_TMR0, P_TMR1, P_TMR2, P_TMR3, P_TMR4, P_TMR5,
23 P_TMR6, P_TMR7, P_TMR8, P_TMR9, P_TMR10, P_TMR11,
26 struct pwm_device *pwm_request(int pwm_id, const char *label)
28 struct pwm_device *pwm;
29 int ret;
31 /* XXX: pwm_id really should be unsigned */
32 if (pwm_id < 0)
33 return NULL;
35 pwm = kzalloc(sizeof(*pwm), GFP_KERNEL);
36 if (!pwm)
37 return pwm;
39 pwm->id = pwm_id;
40 if (pwm->id >= ARRAY_SIZE(pwm_to_gptimer_per))
41 goto err;
43 pwm->pin = pwm_to_gptimer_per[pwm->id];
44 ret = peripheral_request(pwm->pin, label);
45 if (ret)
46 goto err;
48 return pwm;
49 err:
50 kfree(pwm);
51 return NULL;
53 EXPORT_SYMBOL(pwm_request);
55 void pwm_free(struct pwm_device *pwm)
57 peripheral_free(pwm->pin);
58 kfree(pwm);
60 EXPORT_SYMBOL(pwm_free);
62 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
64 unsigned long period, duty;
65 unsigned long long val;
67 if (duty_ns < 0 || duty_ns > period_ns)
68 return -EINVAL;
70 val = (unsigned long long)get_sclk() * period_ns;
71 do_div(val, NSEC_PER_SEC);
72 period = val;
74 val = (unsigned long long)period * duty_ns;
75 do_div(val, period_ns);
76 duty = period - val;
78 if (duty >= period)
79 duty = period - 1;
81 set_gptimer_config(pwm->id, TIMER_MODE_PWM | TIMER_PERIOD_CNT);
82 set_gptimer_pwidth(pwm->id, duty);
83 set_gptimer_period(pwm->id, period);
85 return 0;
87 EXPORT_SYMBOL(pwm_config);
89 int pwm_enable(struct pwm_device *pwm)
91 enable_gptimer(pwm->id);
92 return 0;
94 EXPORT_SYMBOL(pwm_enable);
96 void pwm_disable(struct pwm_device *pwm)
98 disable_gptimer(pwm->id);
100 EXPORT_SYMBOL(pwm_disable);