forked from arduino/portentax8-x8h7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x8h7_pwm.c
223 lines (174 loc) · 5.44 KB
/
x8h7_pwm.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* X8H7 PWM
* driver
*/
#include <linux/err.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <linux/slab.h>
#include "x8h7.h"
#define DRIVER_NAME "x8h7_pwm"
//#define DEBUG
#include "debug.h"
// Peripheral code
#define X8H7_PWM_PERIPH 0x02
struct __attribute__((packed, aligned(4))) pwmPacket {
uint8_t enable : 1;
uint8_t polarity: 1;
uint32_t duty : 30;
uint32_t period : 32;
};
struct x8h7_pwm_chip {
struct pwm_chip chip;
struct pwmPacket pkt;
wait_queue_head_t wait;
int cnt;
};
#define to_x8h7_pwm_chip(_chip) container_of(_chip, struct x8h7_pwm_chip, chip)
static int x8h7_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
int duty_ns, int period_ns)
{
struct x8h7_pwm_chip *x8h7 = to_x8h7_pwm_chip(chip);
DBG_PRINT("duty_ns: %d, period_ns: %d, enabled: %d\n", duty_ns, period_ns, x8h7->pkt.enable);
//@TODO: period_ns must be greater than 953
x8h7->pkt.duty = duty_ns;
x8h7->pkt.period = period_ns;
x8h7_pkt_send_sync(X8H7_PWM_PERIPH, pwm->hwpwm, sizeof(x8h7->pkt), &x8h7->pkt);
return 0;
}
static void x8h7_pwm_hook(void *priv, x8h7_pkt_t *pkt)
{
struct x8h7_pwm_chip *pwm = (struct x8h7_pwm_chip*)priv;
uint8_t ch;
ch = pkt->opcode & 0xF;
if (ch < 10) {
struct pwmPacket* packet = (struct pwmPacket*)(pkt->data);
pwm->pkt.duty = packet->duty;
pwm->pkt.period = packet->period;
pwm->cnt++;
wake_up_interruptible(&pwm->wait);
}
}
static int x8h7_pwm_pkt_get(struct x8h7_pwm_chip *pwm, unsigned int timeout)
{
long ret;
ret = wait_event_interruptible_timeout(pwm->wait,
pwm->cnt != 0,
timeout);
if (!ret) {
DBG_ERROR("timeout expired");
return -1;
}
pwm->cnt--;
return 0;
}
static int x8h7_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
{
struct x8h7_pwm_chip *x8h7 = to_x8h7_pwm_chip(chip);
DBG_PRINT("\n");
x8h7->pkt.enable = 1;
x8h7_pkt_send_sync(X8H7_PWM_PERIPH, pwm->hwpwm, sizeof(x8h7->pkt), &x8h7->pkt);
return 0;
}
static void x8h7_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
{
struct x8h7_pwm_chip *x8h7 = to_x8h7_pwm_chip(chip);
DBG_PRINT("\n");
x8h7->pkt.enable = 0;
x8h7_pkt_send_sync(X8H7_PWM_PERIPH, pwm->hwpwm, sizeof(x8h7->pkt), &x8h7->pkt);
}
static int x8h7_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
{
return x8h7_pwm_enable(chip, pwm);
}
static void x8h7_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
{
x8h7_pwm_disable(chip, pwm);
}
static int x8h7_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm,
struct pwm_capture *result, unsigned long timeout)
{
struct x8h7_pwm_chip *x8h7 = to_x8h7_pwm_chip(chip);
//@TODO: period_ns must be greater than 953
x8h7_pkt_send_sync(X8H7_PWM_PERIPH, pwm->hwpwm | 0x60, sizeof(x8h7->pkt), &x8h7->pkt);
x8h7->pkt.duty = 0;
x8h7->pkt.period = 0;
if (x8h7_pwm_pkt_get(x8h7, timeout) < 0)
return -ETIMEDOUT;
result->duty_cycle = x8h7->pkt.duty;
result->period = x8h7->pkt.period;
DBG_PRINT("duty_ns: %d, period_ns: %d\n", result->duty_cycle, result->period);
return 0;
}
static int x8h7_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
return x8h7_pwm_config(chip, pwm, state->duty_cycle, state->period);
}
static int x8h7_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
struct pwm_state *state)
{
struct x8h7_pwm_chip *x8h7 = to_x8h7_pwm_chip(chip);
state->period = x8h7->pkt.period;
state->polarity = x8h7->pkt.polarity;
state->duty_cycle = x8h7->pkt.duty;
state->enabled = x8h7->pkt.enable;
return 0;
}
static const struct pwm_ops x8h7_pwm_ops = {
.request = x8h7_pwm_request,
.free = x8h7_pwm_free,
.capture = x8h7_pwm_capture,
.apply = x8h7_pwm_apply,
.get_state = x8h7_pwm_get_state,
.owner = THIS_MODULE,
};
static const struct of_device_id x8h7_pwm_dt_ids[] = {
{ .compatible = "portenta,x8h7_pwm", },
{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, x8h7_pwm_dt_ids);
static int x8h7_pwm_probe(struct platform_device *pdev)
{
struct x8h7_pwm_chip *x8h7_pwm;
int ret;
x8h7_pwm = devm_kzalloc(&pdev->dev, sizeof(*x8h7_pwm), GFP_KERNEL);
if (!x8h7_pwm) {
return -ENOMEM;
}
x8h7_pwm->chip.dev = &pdev->dev;
x8h7_pwm->chip.ops = &x8h7_pwm_ops;
x8h7_pwm->chip.base = -1;
x8h7_pwm->chip.npwm = 10;
init_waitqueue_head(&x8h7_pwm->wait);
ret = pwmchip_add(&x8h7_pwm->chip);
if (ret < 0) {
dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
return ret;
}
platform_set_drvdata(pdev, x8h7_pwm);
x8h7_hook_set(X8H7_PWM_PERIPH, x8h7_pwm_hook, x8h7_pwm);
return ret;
}
static int x8h7_pwm_remove(struct platform_device *pdev)
{
struct x8h7_pwm_chip *x8h7_pwm = platform_get_drvdata(pdev);
pwmchip_remove(&x8h7_pwm->chip);
return 0;
}
static struct platform_driver x8h7_pwm_driver = {
.driver = {
.name = DRIVER_NAME,
.of_match_table = of_match_ptr(x8h7_pwm_dt_ids),
},
.probe = x8h7_pwm_probe,
.remove = x8h7_pwm_remove,
};
module_platform_driver(x8h7_pwm_driver);
MODULE_ALIAS("platform:x8h7-pwm");
MODULE_AUTHOR("Massimiliano Agneni <[email protected]>");
MODULE_DESCRIPTION("Aeduino portenta X8 PWM driver");
MODULE_LICENSE("GPL v2");