-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm-mockup.c
180 lines (148 loc) · 4.27 KB
/
pwm-mockup.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
/*
* pwm-mockup driver
*
* Copyright (C) Paulo Neves
*
* Author: Paulo Neves <[email protected]>
*
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <linux/slab.h>
#define MODULE_NAME "gpio-fake"
#define pwm_mockup_err(...) pr_err(MODULE_NAME ": " __VA_ARGS__)
#define pwm_mockup_info(...) pr_info(MODULE_NAME ": " __VA_ARGS__)
#define pwm_mockup_MAX_DEVICES 1
struct pwm_mockup_pwm_chip {
struct pwm_chip chip;
};
static inline struct pwm_mockup_pwm_chip *to_pwm_mockup_pwm_chip(struct pwm_chip *chip)
{
return container_of(chip, struct pwm_mockup_pwm_chip, chip);
}
static int pwm_mockup_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
{
return 0;
}
static void pwm_mockup_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
{
pwm_set_chip_data(pwm, NULL);
}
static int pwm_mockup_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev,
int duty_ns, int period_ns)
{
pwm_mockup_info("pin:%d;duty_ns:%d,period_ns:%d", pwm_dev->hwpwm, duty_ns, period_ns);
return 0;
}
static int pwm_mockup_pwm_set_polarity(struct pwm_chip *chip,
struct pwm_device *pwm_dev,
enum pwm_polarity polarity)
{
pwm_mockup_info("pin:%d;polarity:1", pwm_dev->hwpwm);
return 0;
}
static int pwm_mockup_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm_dev)
{
pwm_mockup_info("pin:%d;enable:1", pwm_dev->hwpwm);
return 0;
}
static void pwm_mockup_pwm_disable(struct pwm_chip *chip,
struct pwm_device *pwm_dev)
{
pwm_mockup_info("pin:%d;enable:0", pwm_dev->hwpwm);
}
static const struct pwm_ops pwm_mockup_pwm_ops = {
.request = pwm_mockup_pwm_request,
.free = pwm_mockup_pwm_free,
.config = pwm_mockup_pwm_config,
.set_polarity = pwm_mockup_pwm_set_polarity,
.enable = pwm_mockup_pwm_enable,
.disable = pwm_mockup_pwm_disable,
.owner = THIS_MODULE,
};
static int pwm_mockup_pwm_probe(struct platform_device *pdev)
{
struct pwm_mockup_pwm_chip *pwm;
int ret;
pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
if (!pwm)
return -ENOMEM;
pwm->chip.base = -1;
pwm->chip.dev = &pdev->dev;
pwm->chip.ops = &pwm_mockup_pwm_ops;
pwm->chip.npwm = 4;
pwm->chip.of_pwm_n_cells = 3;
ret = pwmchip_add(&pwm->chip);
if (ret < 0) {
pwm_mockup_err("failed to add PWM chip: %d\n", ret);
return ret;
}
platform_set_drvdata(pdev, pwm);
pwm_mockup_info("Probed");
return 0;
}
static int pwm_mockup_pwm_remove(struct platform_device *pdev)
{
struct pwm_mockup_pwm_chip *pwm = platform_get_drvdata(pdev);
int ret;
ret = pwmchip_remove(&pwm->chip);
return ret;
}
static struct platform_driver pwm_mockup_pwm_driver = {
.probe = pwm_mockup_pwm_probe,
.remove = pwm_mockup_pwm_remove,
.driver = {
.name = MODULE_NAME,
},
};
static struct platform_device *pwm_mockup_pdevs[pwm_mockup_MAX_DEVICES];
static void pwm_mockup_unregister_pdevs(void)
{
struct platform_device *pdev;
int i;
for (i = 0; i < pwm_mockup_MAX_DEVICES; i++) {
pdev = pwm_mockup_pdevs[i];
if (pdev)
platform_device_unregister(pdev);
}
}
static int __init pwm_mockup_init(void) {
int err = 0;
int i = 0;
struct platform_device *pdev = NULL;
err = platform_driver_register(&pwm_mockup_pwm_driver);
if (err) {
pwm_mockup_err("pwm_mockup: could not register driver");
return err;
}
for (; i < pwm_mockup_MAX_DEVICES; i++) {
pdev = platform_device_register_simple(MODULE_NAME, i, NULL, 0);
if (IS_ERR(pdev)) {
pwm_mockup_err("pwm_mockup: error registring device");
platform_driver_unregister(&pwm_mockup_pwm_driver);
if (pdev) {
platform_device_unregister(pdev);
}
return PTR_ERR(pdev);
}
pwm_mockup_pdevs[i] = pdev;
}
return 0;
}
static void __exit pwm_mockup_exit(void)
{
platform_driver_unregister(&pwm_mockup_pwm_driver);
pwm_mockup_unregister_pdevs();
}
module_init(pwm_mockup_init);
module_exit(pwm_mockup_exit);
MODULE_AUTHOR("Paulo Neves <[email protected]>");
MODULE_DESCRIPTION("Mockup PWM driver");
MODULE_LICENSE("GPL v2");