summaryrefslogtreecommitdiffstats
path: root/drivers/pwm/pwm-bfin.c
blob: 5da8e185e838c111676fb40a9d159619a846cdc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Blackfin Pulse Width Modulation (PWM) core
 *
 * Copyright (c) 2011 Analog Devices Inc.
 *
 * Licensed under the GPL-2 or later.
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <linux/slab.h>

#include <asm/gptimers.h>
#include <asm/portmux.h>

struct bfin_pwm_chip {
	struct pwm_chip chip;
};

struct bfin_pwm {
	unsigned short pin;
};

static const unsigned short pwm_to_gptimer_per[] = {
	P_TMR0, P_TMR1, P_TMR2, P_TMR3, P_TMR4, P_TMR5,
	P_TMR6, P_TMR7, P_TMR8, P_TMR9, P_TMR10, P_TMR11,
};

static int bfin_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct bfin_pwm *priv;
	int ret;

	if (pwm->hwpwm >= ARRAY_SIZE(pwm_to_gptimer_per))
		return -EINVAL;

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->pin = pwm_to_gptimer_per[pwm->hwpwm];

	ret = peripheral_request(priv->pin, NULL);
	if (ret) {
		kfree(priv);
		return ret;
	}

	pwm_set_chip_data(pwm, priv);

	return 0;
}

static void bfin_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct bfin_pwm *priv = pwm_get_chip_data(pwm);

	if (priv) {
		peripheral_free(priv->pin);
		kfree(priv);
	}
}

static int bfin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
		int duty_ns, int period_ns)
{
	struct bfin_pwm *priv = pwm_get_chip_data(pwm);
	unsigned long period, duty;
	unsigned long long val;

	val = (unsigned long long)get_sclk() * period_ns;
	do_div(val, NSEC_PER_SEC);
	period = val;

	val = (unsigned long long)period * duty_ns;
	do_div(val, period_ns);
	duty = period - val;

	if (duty >= period)
		duty = period - 1;

	set_gptimer_config(priv->pin, TIMER_MODE_PWM | TIMER_PERIOD_CNT);
	set_gptimer_pwidth(priv->pin, duty);
	set_gptimer_period(priv->pin, period);

	return 0;
}

static int bfin_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct bfin_pwm *priv = pwm_get_chip_data(pwm);

	enable_gptimer(priv->pin);

	return 0;
}

static void bfin_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
{
	struct bfin_pwm *priv = pwm_get_chip_data(pwm);

	disable_gptimer(priv->pin);
}

static struct pwm_ops bfin_pwm_ops = {
	.request = bfin_pwm_request,
	.free = bfin_pwm_free,
	.config = bfin_pwm_config,
	.enable = bfin_pwm_enable,
	.disable = bfin_pwm_disable,
	.owner = THIS_MODULE,
};

static int bfin_pwm_probe(struct platform_device *pdev)
{
	struct bfin_pwm_chip *pwm;
	int ret;

	pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
	if (!pwm) {
		dev_err(&pdev->dev, "failed to allocate memory\n");
		return -ENOMEM;
	}

	platform_set_drvdata(pdev, pwm);

	pwm->chip.dev = &pdev->dev;
	pwm->chip.ops = &bfin_pwm_ops;
	pwm->chip.base = -1;
	pwm->chip.npwm = 12;

	ret = pwmchip_add(&pwm->chip);
	if (ret < 0) {
		dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
		return ret;
	}

	return 0;
}

static int __devexit bfin_pwm_remove(struct platform_device *pdev)
{
	struct bfin_pwm_chip *pwm = platform_get_drvdata(pdev);

	return pwmchip_remove(&pwm->chip);
}

static struct platform_driver bfin_pwm_driver = {
	.driver = {
		.name = "bfin-pwm",
	},
	.probe = bfin_pwm_probe,
	.remove = __devexit_p(bfin_pwm_remove),
};

module_platform_driver(bfin_pwm_driver);

MODULE_LICENSE("GPL");