aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pwm
diff options
context:
space:
mode:
authorThierry Reding <thierry.reding@avionic-design.de>2012-01-02 14:53:50 -0500
committerThierry Reding <thierry.reding@avionic-design.de>2012-07-02 15:38:59 -0400
commita4315e3c11f97ea0fa602184fa310161309657df (patch)
tree859df4b9b0733be80f20ecf42c8430584215a1ca /drivers/pwm
parent140fd977dc46bc750258f082cdf1cfea79dc1d14 (diff)
pwm: Move Blackfin PWM driver to PWM framework
This commit moves the Blackfin PWM driver to the drivers/pwm sub- directory and converts it to register with the new PWM framework. Signed-off-by: Thierry Reding <thierry.reding@avionic-design.de>
Diffstat (limited to 'drivers/pwm')
-rw-r--r--drivers/pwm/Kconfig9
-rw-r--r--drivers/pwm/Makefile1
-rw-r--r--drivers/pwm/pwm-bfin.c164
3 files changed, 174 insertions, 0 deletions
diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index bda6f23af532..eb5404268135 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -9,6 +9,15 @@ menuconfig PWM
9 9
10if PWM 10if PWM
11 11
12config PWM_BFIN
13 tristate "Blackfin PWM support"
14 depends on BFIN_GPTIMERS
15 help
16 Generic PWM framework driver for Blackfin.
17
18 To compile this driver as a module, choose M here: the module
19 will be called pwm-bfin.
20
12config PWM_TEGRA 21config PWM_TEGRA
13 tristate "NVIDIA Tegra PWM support" 22 tristate "NVIDIA Tegra PWM support"
14 depends on ARCH_TEGRA 23 depends on ARCH_TEGRA
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 12300f5e9016..251b8d22435f 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -1,2 +1,3 @@
1obj-$(CONFIG_PWM) += core.o 1obj-$(CONFIG_PWM) += core.o
2obj-$(CONFIG_PWM_BFIN) += pwm-bfin.o
2obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o 3obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
diff --git a/drivers/pwm/pwm-bfin.c b/drivers/pwm/pwm-bfin.c
new file mode 100644
index 000000000000..a0c6bf9070ea
--- /dev/null
+++ b/drivers/pwm/pwm-bfin.c
@@ -0,0 +1,164 @@
1/*
2 * Blackfin Pulse Width Modulation (PWM) core
3 *
4 * Copyright (c) 2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/module.h>
10#include <linux/platform_device.h>
11#include <linux/pwm.h>
12#include <linux/slab.h>
13
14#include <asm/gptimers.h>
15#include <asm/portmux.h>
16
17struct bfin_pwm_chip {
18 struct pwm_chip chip;
19};
20
21struct bfin_pwm {
22 unsigned short pin;
23};
24
25static const unsigned short pwm_to_gptimer_per[] = {
26 P_TMR0, P_TMR1, P_TMR2, P_TMR3, P_TMR4, P_TMR5,
27 P_TMR6, P_TMR7, P_TMR8, P_TMR9, P_TMR10, P_TMR11,
28};
29
30static int bfin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
31{
32 struct bfin_pwm *priv;
33 int ret;
34
35 if (pwm->hwpwm >= ARRAY_SIZE(pwm_to_gptimer_per))
36 return -EINVAL;
37
38 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
39 if (!priv)
40 return -ENOMEM;
41
42 priv->pin = pwm_to_gptimer_per[pwm->hwpwm];
43
44 ret = peripheral_request(priv->pin, NULL);
45 if (ret) {
46 kfree(priv);
47 return ret;
48 }
49
50 pwm_set_chip_data(pwm, priv);
51
52 return 0;
53}
54
55static void bfin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
56{
57 struct bfin_pwm *priv = pwm_get_chip_data(pwm);
58
59 if (priv) {
60 peripheral_free(priv->pin);
61 kfree(priv);
62 }
63}
64
65static int bfin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
66 int duty_ns, int period_ns)
67{
68 struct bfin_pwm *priv = pwm_get_chip_data(pwm);
69 unsigned long period, duty;
70 unsigned long long val;
71
72 if (duty_ns < 0 || duty_ns > period_ns)
73 return -EINVAL;
74
75 val = (unsigned long long)get_sclk() * period_ns;
76 do_div(val, NSEC_PER_SEC);
77 period = val;
78
79 val = (unsigned long long)period * duty_ns;
80 do_div(val, period_ns);
81 duty = period - val;
82
83 if (duty >= period)
84 duty = period - 1;
85
86 set_gptimer_config(priv->pin, TIMER_MODE_PWM | TIMER_PERIOD_CNT);
87 set_gptimer_pwidth(priv->pin, duty);
88 set_gptimer_period(priv->pin, period);
89
90 return 0;
91}
92
93static int bfin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
94{
95 struct bfin_pwm *priv = pwm_get_chip_data(pwm);
96
97 enable_gptimer(priv->pin);
98
99 return 0;
100}
101
102static void bfin_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
103{
104 struct bfin_pwm *priv = pwm_get_chip_data(pwm);
105
106 disable_gptimer(priv->pin);
107}
108
109static struct pwm_ops bfin_pwm_ops = {
110 .request = bfin_pwm_request,
111 .free = bfin_pwm_free,
112 .config = bfin_pwm_config,
113 .enable = bfin_pwm_enable,
114 .disable = bfin_pwm_disable,
115 .owner = THIS_MODULE,
116};
117
118static int bfin_pwm_probe(struct platform_device *pdev)
119{
120 struct bfin_pwm_chip *pwm;
121 int ret;
122
123 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
124 if (!pwm) {
125 dev_err(&pdev->dev, "failed to allocate memory\n");
126 return -ENOMEM;
127 }
128
129 platform_set_drvdata(pdev, pwm);
130
131 pwm->chip.dev = &pdev->dev;
132 pwm->chip.ops = &bfin_pwm_ops;
133 pwm->chip.base = -1;
134 pwm->chip.npwm = 12;
135
136 ret = pwmchip_add(&pwm->chip);
137 if (ret < 0) {
138 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
139 return ret;
140 }
141
142 return 0;
143}
144
145static int __devexit bfin_pwm_remove(struct platform_device *pdev)
146{
147 struct bfin_pwm_chip *pwm = platform_get_drvdata(pdev);
148
149 pwmchip_remove(&pwm->chip);
150
151 return 0;
152}
153
154static struct platform_driver bfin_pwm_driver = {
155 .driver = {
156 .name = "bfin-pwm",
157 },
158 .probe = bfin_pwm_probe,
159 .remove = __devexit_p(bfin_pwm_remove),
160};
161
162module_platform_driver(bfin_pwm_driver);
163
164MODULE_LICENSE("GPL");