forked from manic-3dprint/rpi_SoftPwm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwm.c
416 lines (314 loc) · 9.27 KB
/
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/time.h>
#include <linux/gpio.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/kdev_t.h>
#include <linux/mutex.h>
/* Copyright (C)
* 2014 - Tomasz Wisniewski
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#define DEBUG 0
#define MICRO_SEC 1000000
#define NANO_SEC (MICRO_SEC*1000)
#define DEFAULT_FREQ 1000
#define DEFAULT_DUTY_CYCLE 50
/**
* @brief represents a single pwm channel
*/
struct pwm_channel {
struct hrtimer tm1, tm2;
ktime_t t1;
int freq;
int dc;
int gpio;
// linked list of channels
struct list_head chan_list;
};
/**
* @brief linked list of all the channels in the system
*/
static LIST_HEAD(_channels);
static struct mutex _lock;
/**
* @brief callback for timer 1 - responsible for the rising slope
*
* @param t timer
*
* @return _RESTART
*/
enum hrtimer_restart cb1(struct hrtimer *t) {
struct pwm_channel *ch = container_of(t, struct pwm_channel, tm1);
ktime_t now;
int ovr;
now = hrtimer_cb_get_time(t);
ovr = hrtimer_forward(t, now, ch->t1);
#if DEBUG == 1
printk(KERN_EMERG "up [%d]\n", ovr);
#endif
if (ch->dc) {
gpio_set_value(ch->gpio, 1);
if (ch->dc < 100) {
unsigned long t_ns = ((MICRO_SEC * 10 * ch->dc) / (ch->freq));
ktime_t t2 = ktime_set( 0, t_ns );
hrtimer_start(&ch->tm2, t2, HRTIMER_MODE_REL);
}
}
else {
gpio_set_value(ch->gpio, 0);
}
return HRTIMER_RESTART;
}
/**
* @brief callback for timer 2 - responsible for falling slope
*
* @param t timer
*
* @return _NORESTART
*/
enum hrtimer_restart cb2(struct hrtimer *t) {
struct pwm_channel *ch = container_of(t, struct pwm_channel, tm2);
#if DEBUG == 1
printk(KERN_INFO "down\n");
#endif
gpio_set_value(ch->gpio, 0);
return HRTIMER_NORESTART;
}
void init_channel(struct pwm_channel *a_ch) {
unsigned long t_ns = (NANO_SEC)/a_ch->freq;
hrtimer_init(&a_ch->tm1, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
hrtimer_init(&a_ch->tm2, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
a_ch->t1 = ktime_set( 0, t_ns );
a_ch->tm1.function = &cb1;
a_ch->tm2.function = &cb2;
gpio_request(a_ch->gpio, "soft_pwm_gpio");
gpio_direction_output(a_ch->gpio, 1);
hrtimer_start(&a_ch->tm1, a_ch->t1, HRTIMER_MODE_REL);
}
void deinit_channel(struct pwm_channel *a_ch) {
if (hrtimer_active(&a_ch->tm1) || hrtimer_is_queued(&a_ch->tm1)) {
hrtimer_cancel(&a_ch->tm1);
}
if (hrtimer_active(&a_ch->tm2) || hrtimer_is_queued(&a_ch->tm2)) {
hrtimer_cancel(&a_ch->tm2);
}
gpio_free(a_ch->gpio);
}
/* ========================================================================== */
static ssize_t export_store(struct class *class, struct class_attribute *attr, const char *buf, size_t len);
static ssize_t unexport_store(struct class *class, struct class_attribute *attr, const char *buf, size_t len);
static struct class_attribute soft_pwm_class_attrs[] = {
__ATTR(export, 0222, NULL, export_store),
__ATTR(unexport, 0222, NULL, unexport_store),
__ATTR_NULL,
};
static struct class soft_pwm_class = {
.name = "soft_pwm",
.owner = THIS_MODULE,
.class_attrs = soft_pwm_class_attrs,
};
static ssize_t duty_cycle_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len);
static ssize_t duty_cycle_show(struct device *dev, struct device_attribute *attr, char *buf);
static ssize_t frequency_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len);
static ssize_t frequency_show(struct device *dev, struct device_attribute *attr, char *buf);
static DEVICE_ATTR(duty_cycle, 0644, duty_cycle_show, duty_cycle_store);
static DEVICE_ATTR(frequency, 0644, frequency_show, frequency_store);
static struct attribute *soft_pwm_dev_attrs[] = {
&dev_attr_duty_cycle.attr,
&dev_attr_frequency.attr,
NULL,
};
static struct attribute_group soft_pwm_dev_attr_group = {
.attrs = (struct attribute **)soft_pwm_dev_attrs,
};
/* ========================================================================== */
static ssize_t duty_cycle_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len) {
unsigned long dc = 0;
struct pwm_channel *ch = dev_get_drvdata(dev);
if (!kstrtol(buf, 10, &dc)) {
dc = dc > 100 ? 100 : dc;
ch->dc = dc;
}
return len;
}
static ssize_t duty_cycle_show(struct device *dev,
struct device_attribute *attr, char *buf) {
struct pwm_channel *ch = dev_get_drvdata(dev);
return sprintf(buf, "%d", ch->dc);
}
static ssize_t frequency_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len) {
unsigned long f = 0;
struct pwm_channel *ch = dev_get_drvdata(dev);
if (!kstrtol(buf, 10, &f)) {
unsigned long t_ns = (NANO_SEC)/f;
deinit_channel(ch);
ch->freq = f;
// restart timer1
ch->t1 = ktime_set( 0, t_ns );
hrtimer_start(&ch->tm1, ch->t1, HRTIMER_MODE_REL);
}
return len;
}
static ssize_t frequency_show(struct device *dev,
struct device_attribute *attr, char *buf) {
struct pwm_channel *ch = dev_get_drvdata(dev);
return sprintf(buf, "%d", ch->freq);
}
ssize_t export_store(struct class *class,
struct class_attribute *attr,
const char *buf,
size_t len) {
struct list_head *p = NULL;
struct pwm_channel *ch = NULL;
struct device *d = NULL;
int found = 0;
unsigned long gpio = 0;
int rv = len;
mutex_lock(&_lock);
if (kstrtol(buf, 10, &gpio)) {
printk(KERN_ERR "invalid data [%s]\n", buf);
rv = -EINVAL;
goto label_export_cleanup;
}
list_for_each (p, &_channels) {
ch = list_entry(p, struct pwm_channel, chan_list);
if (ch->gpio == gpio) {
found = 1;
break;
}
}
if (found) {
// channel for the given gpio already exists
// ignore the request
printk(KERN_ERR "channel for the gpio already allocated\n");
goto label_export_cleanup;
}
// create the channel
if (!(ch = kmalloc(sizeof(struct pwm_channel), GFP_KERNEL))) {
printk(KERN_ERR "Unable to allocate memory for the channel\n");
rv = -ENOMEM;
goto label_export_cleanup;
}
// initialize the channel
ch->gpio = gpio;
ch->freq = DEFAULT_FREQ;
ch->dc = DEFAULT_DUTY_CYCLE;
INIT_LIST_HEAD(&ch->chan_list);
// create sysfs entries
if (!(d =
device_create(&soft_pwm_class, NULL, MKDEV(0,0), ch, "pwm-%d", (int)gpio))) {
printk(KERN_ERR "Unable to create the pwm-%d device\n", (int)gpio);
rv = -ENOMEM;
goto label_export_cleanup;
}
if (sysfs_create_group(&d->kobj, &soft_pwm_dev_attr_group)) {
printk(KERN_ERR "Unable to create attributes for channel %d\n", (int)gpio);
rv = -ENODEV;
goto label_export_cleanup;
}
// ... and finally - initialize channel's timers
init_channel(ch);
list_add(&ch->chan_list, &_channels);
goto label_export_exit;
label_export_cleanup:
if (d) device_unregister(d);
if (ch) kfree(ch);
label_export_exit:
mutex_unlock(&_lock);
return rv;
}
static int _match_channel(struct device *dev, const void *data) {
return dev_get_drvdata(dev) == data;
}
ssize_t unexport_store(struct class *class,
struct class_attribute *attr,
const char *buf,
size_t len) {
struct list_head *p = NULL;
struct pwm_channel *ch = NULL;
struct device *d = NULL;
int found = 0;
long gpio = 0;
if (kstrtol(buf, 10, &gpio)) {
printk(KERN_ERR "invalid data [%s]\n", buf);
return -EINVAL;
}
list_for_each (p, &_channels) {
ch = list_entry(p, struct pwm_channel, chan_list);
if (ch->gpio == gpio) {
found = 1;
break;
}
}
if (!found) {
// channel for the given gpio doesn't exists
// ignore the request
return len;
}
if ((d = class_find_device(&soft_pwm_class, NULL, ch, _match_channel))) {
put_device(d);
device_unregister(d);
}
deinit_channel(ch);
list_del(&ch->chan_list);
kfree(ch);
return len;
}
/* ========================================================================== */
static int __init pwm_init(void) {
#if DEBUG == 1
printk(KERN_INFO "installing soft pwm module\n");
#endif
mutex_init(&_lock);
return class_register(&soft_pwm_class);
}
static void __exit pwm_exit(void) {
struct list_head *p = NULL;
struct list_head *q = NULL;
struct pwm_channel *ch = NULL;
struct device *d = NULL;
#if DEBUG == 1
printk(KERN_INFO "deinstalling soft pwm module\n");
#endif
list_for_each_safe(p, q, &_channels) {
ch = list_entry(p, struct pwm_channel, chan_list);
if ((d = class_find_device(&soft_pwm_class,
NULL,
ch,
_match_channel))) {
put_device(d);
device_unregister(d);
}
deinit_channel(ch);
list_del(p);
kfree(ch);
}
class_unregister(&soft_pwm_class);
}
module_init(pwm_init);
module_exit(pwm_exit);
MODULE_AUTHOR("Tomasz Wisniewski");
MODULE_DESCRIPTION("hrtimer based pwm bit banger");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");