forked from ant9000/soft_pwm
-
Notifications
You must be signed in to change notification settings - Fork 5
/
soft_pwm.c
351 lines (304 loc) · 10.1 KB
/
soft_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* Copyright (C) 2010 Antonio Galea
*
* May be copied or modified under the terms of the GNU General Public
* License. See linux/COPYING for more information.
*
* Generic software-only driver for generating PWM signals via high
* resolution timers and GPIO lib interface.
*/
/* Modified by Sergio Tanzilli
http://www.acmesystems.it/soft_pwm
http://www.acmesystems.it/DAISY-2
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/device.h>
#include <linux/kdev_t.h>
#include <linux/gpio.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Antonio Galea");
MODULE_DESCRIPTION("Driver for kernel-generated PWM signals");
static struct hrtimer hr_timer;
/* pwm_desc
*
* This structure maintains the information regarding a
* single PWM signal: its wave period and pulse length
* are user-definable via sysfs. The counter is also
* shown in sysfs as a debug helper.
*/
struct pwm_desc {
unsigned int pulse; // pulse width, in microseconds
unsigned int period; // wave period, in microseconds
unsigned int pulses; // number of pwm pulses before stopping; -1 never stops, 0 stops immediately
unsigned long counter; // "interrupt" counter - counts each value toggle
int value; // current GPIO pin value (0 or 1 only)
ktime_t next_tick; // timer tick at which next toggling should happen
unsigned long flags; // only FLAG_SOFTPWM is used, for synchronizing inside module
#define FLAG_SOFTPWM 0
};
/* pwm_table
*
* The table will hold a description for any GPIO pin available
* on the system. It's wasteful to preallocate the entire table,
* but avoiding race conditions is so much easier this way ;-)
*/
static struct pwm_desc pwm_table[ARCH_NR_GPIOS];
/* lock protects against pwm_unexport() being called while
* sysfs files are active.
*/
static DEFINE_MUTEX(sysfs_lock);
int pwm_export(unsigned gpio); // forward definition
int pwm_unexport(unsigned gpio); // forward definition
/* Show attribute values for PWMs */
static ssize_t pwm_show(struct device *dev, struct device_attribute *attr, char *buf){
const struct pwm_desc *desc = dev_get_drvdata(dev);
ssize_t status;
mutex_lock(&sysfs_lock);
if(!test_bit(FLAG_SOFTPWM, &desc->flags)){
status = -EIO;
}else{
if(strcmp(attr->attr.name, "pulse")==0){
status = sprintf(buf, "%d usec\n", desc->pulse);
}else if(strcmp(attr->attr.name, "period")==0){
status = sprintf(buf, "%d usec\n", desc->period);
}else if(strcmp(attr->attr.name, "pulses")==0){
status = sprintf(buf, "%d usec\n", desc->pulses);
}else if(strcmp(attr->attr.name, "counter")==0){
status = sprintf(buf, "%lu\n", desc->counter);
}else{
status = -EIO;
}
}
mutex_unlock(&sysfs_lock);
return status;
}
/* Store attribute values for PWMs */
static ssize_t pwm_store(
struct device *dev, struct device_attribute *attr, const char *buf, size_t size
){
struct pwm_desc *desc = dev_get_drvdata(dev);
ssize_t status;
mutex_lock(&sysfs_lock);
if(!test_bit(FLAG_SOFTPWM, &desc->flags)){
status = -EIO;
}else{
unsigned long value;
status = strict_strtoul(buf, 0, &value);
if(status==0){
if(strcmp(attr->attr.name, "pulse")==0){
if(value<=desc->period){ desc->pulse = (unsigned int)value; }
}else if(strcmp(attr->attr.name, "period")==0){
desc->period = (unsigned int)value;
}else if(strcmp(attr->attr.name, "pulses")==0){
if (value>0)
desc->pulses = (unsigned int)value*2;
else
desc->pulses = (unsigned int)value;
}
desc->next_tick = ktime_get();
//printk(KERN_INFO "Starting timer (%s).\n", attr->attr.name);
hrtimer_start(&hr_timer, ktime_set(0,1), HRTIMER_MODE_REL);
}
}
mutex_unlock(&sysfs_lock);
return status ? : size;
}
/* Sysfs attributes definition for PWMs */
static DEVICE_ATTR(pulse, 0644, pwm_show, pwm_store);
static DEVICE_ATTR(period, 0644, pwm_show, pwm_store);
static DEVICE_ATTR(pulses, 0644, pwm_show, pwm_store);
static DEVICE_ATTR(counter, 0444, pwm_show, NULL);
static const struct attribute *soft_pwm_dev_attrs[] = {
&dev_attr_pulse.attr,
&dev_attr_period.attr,
&dev_attr_pulses.attr,
&dev_attr_counter.attr,
NULL,
};
static const struct attribute_group soft_pwm_dev_attr_group = {
.attrs = (struct attribute **) soft_pwm_dev_attrs,
};
/* Export a GPIO pin to sysfs, and claim it for PWM usage.
* See the equivalent function in drivers/gpio/gpiolib.c
*/
static ssize_t export_store(struct class *class, struct class_attribute *attr, const char *buf, size_t len){
long gpio;
int status;
status = strict_strtol(buf, 0, &gpio);
if(status<0){ goto done; }
status = gpio_request(gpio, "soft_pwm");
if(status<0){ goto done; }
status = gpio_direction_output(gpio,0);
if(status<0){ goto done; }
status = pwm_export(gpio);
if(status<0){ goto done; }
set_bit(FLAG_SOFTPWM, &pwm_table[gpio].flags);
done:
if(status){
gpio_free(gpio);
pr_debug("%s: status %d\n", __func__, status);
}
return status ? : len;
}
/* Unexport a PWM GPIO pin from sysfs, and unreclaim it.
* See the equivalent function in drivers/gpio/gpiolib.c
*/
static ssize_t unexport_store(struct class *class, struct class_attribute *attr, const char *buf, size_t len){
long gpio;
int status;
status = strict_strtol(buf, 0, &gpio);
if(status<0){ goto done; }
status = -EINVAL;
if(!gpio_is_valid(gpio)){ goto done; }
if(test_and_clear_bit(FLAG_SOFTPWM, &pwm_table[gpio].flags)){
status = pwm_unexport(gpio);
if(status==0){ gpio_free(gpio); }
}
done:
if(status){ pr_debug("%s: status %d\n", __func__, status); }
return status ? : len;
}
/* Sysfs definitions for soft_pwm class */
static struct class_attribute soft_pwm_class_attrs[] = {
__ATTR(export, 0200, NULL, export_store),
__ATTR(unexport, 0200, NULL, unexport_store),
__ATTR_NULL,
};
static struct class soft_pwm_class = {
.name = "soft_pwm",
.owner = THIS_MODULE,
.class_attrs = soft_pwm_class_attrs,
};
/* Setup the sysfs directory for a claimed PWM device */
int pwm_export(unsigned gpio){
struct pwm_desc *desc;
struct device *dev;
int status;
mutex_lock(&sysfs_lock);
desc = &pwm_table[gpio];
desc->value = 0;
desc->pulses = -1;
dev = device_create(&soft_pwm_class, NULL, MKDEV(0, 0), desc, "pwm%d", gpio);
if(dev){
status = sysfs_create_group(&dev->kobj, &soft_pwm_dev_attr_group);
if(status==0){
printk(KERN_INFO "Registered device pwm%d\n", gpio);
}else{
device_unregister(dev);
}
}else{
status = -ENODEV;
}
mutex_unlock(&sysfs_lock);
if(status){ pr_debug("%s: pwm%d status %d\n", __func__, gpio, status); }
return status;
}
/* Used by pwm_unexport below to find the device which should be freed */
static int match_export(struct device *dev, void *data){
return dev_get_drvdata(dev) == data;
}
/* Free a claimed PWM device and unregister the sysfs directory */
int pwm_unexport(unsigned gpio){
struct pwm_desc *desc;
struct device *dev;
int status;
mutex_lock(&sysfs_lock);
desc = &pwm_table[gpio];
dev = class_find_device(&soft_pwm_class, NULL, desc, match_export);
if(dev){
put_device(dev);
device_unregister(dev);
printk(KERN_INFO "Unregistered device pwm%d\n", gpio);
status = 0;
}else{
status = -ENODEV;
}
mutex_unlock(&sysfs_lock);
if(status){ pr_debug("%s: pwm%d status %d\n", __func__, gpio, status); }
return status;
}
/* The timer callback is called only when needed (which is to
* say, at the earliest PWM signal toggling time) in order to
* maintain the pressure on system latency as low as possible
*/
enum hrtimer_restart soft_pwm_hrtimer_callback(struct hrtimer *timer){
unsigned gpio;
struct pwm_desc *desc;
ktime_t now = ktime_get();
ktime_t next_tick = ktime_set(0,0);
now = ktime_get();
for(gpio=0;gpio<ARCH_NR_GPIOS;gpio++){
desc = &pwm_table[gpio];
if(
test_bit(FLAG_SOFTPWM,&desc->flags) &&
(desc->period>0) &&
(desc->pulse<=desc->period) &&
(desc->pulses!=0)
){
if(desc->next_tick.tv64<=now.tv64){
desc->value = 1-desc->value;
__gpio_set_value(gpio,desc->value);
desc->counter++;
if(desc->pulses>0){ desc->pulses--; }
if((desc->pulse==0)||(desc->pulse==desc->period)||(desc->pulses==0)){
desc->next_tick.tv64 = KTIME_MAX;
}else{
desc->next_tick=ktime_add_ns(
desc->next_tick,
(desc->value? desc->pulse : desc->period-desc->pulse)*1000
);
}
}
if((next_tick.tv64==0)||(desc->next_tick.tv64<next_tick.tv64)){
next_tick.tv64 = desc->next_tick.tv64;
}
}
}
if(next_tick.tv64>0){
hrtimer_start(&hr_timer, next_tick, HRTIMER_MODE_ABS);
}else{
//printk(KERN_INFO "Stopping timer.\n");
}
return HRTIMER_NORESTART;
}
/* module initialization: init the hr-timer and register a driver class */
static int __init soft_pwm_init(void){
struct timespec tp;
int status;
printk(KERN_INFO "SoftPWM v0.2-acme initializing.\n");
hrtimer_get_res(CLOCK_MONOTONIC, &tp);
printk(KERN_INFO "Clock resolution is %ldns\n", tp.tv_nsec);
hrtimer_init(&hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
hr_timer.function = &soft_pwm_hrtimer_callback;
status = class_register(&soft_pwm_class);
if(status<0){ goto fail_no_class; }
printk(KERN_INFO "SoftPWM initialized.\n");
return 0;
fail_no_class:
return status;
}
/* module finalization: cancel the hr-timer, switch off any PWM
* signal and give back to GPIO the pin, then deregister our class
*/
static void __exit soft_pwm_exit(void){
unsigned gpio;
int status;
hrtimer_cancel(&hr_timer);
for(gpio=0;gpio<ARCH_NR_GPIOS;gpio++){
struct pwm_desc *desc;
desc = &pwm_table[gpio];
if(test_bit(FLAG_SOFTPWM,&desc->flags)){
__gpio_set_value(gpio,0);
status = pwm_unexport(gpio);
if(status==0){ gpio_free(gpio); }
}
}
class_unregister(&soft_pwm_class);
printk(KERN_INFO "SoftPWM disabled.\n");
}
module_init(soft_pwm_init);
module_exit(soft_pwm_exit);