-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservo.c
197 lines (157 loc) · 4.42 KB
/
servo.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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <linux/pinctrl/consumer.h>
#define SERVO_PWM_PERIOD 20000000
#define SERVO_MAX_DUTY 2500000
#define SERVO_MIN_DUTY 700000
#define SERVO_DEGREE ((SERVO_MAX_DUTY - SERVO_MIN_DUTY) / 180)
struct pwm_servo_data {
struct pwm_device *pwm;
unsigned int angle;
unsigned int angle_duty;
unsigned int standby;
};
static ssize_t pwm_servo_show_angle(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct pwm_servo_data *servo =
platform_get_drvdata(to_platform_device(dev));
return sprintf(buf, "%u\n", servo->angle);
}
static ssize_t pwm_servo_store_angle(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
unsigned long tmp;
int error;
struct pwm_servo_data *servo =
platform_get_drvdata(to_platform_device(dev));
error = kstrtoul(buf, 10, &tmp);
if (error < 0)
return error;
if (tmp < 0 || tmp > 180)
return -EINVAL;
error = pwm_config(servo->pwm, SERVO_MIN_DUTY +
SERVO_DEGREE * tmp, SERVO_PWM_PERIOD);
if (error < 0)
return error;
servo->angle = tmp;
servo->angle_duty = SERVO_MIN_DUTY + SERVO_DEGREE * tmp;
return count;
}
static ssize_t pwm_servo_show_standby(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct pwm_servo_data *servo =
platform_get_drvdata(to_platform_device(dev));
return sprintf(buf, "%u\n", servo->standby);
}
static ssize_t pwm_servo_store_standby(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
unsigned long tmp;
int error;
struct pwm_servo_data *servo =
platform_get_drvdata(to_platform_device(dev));
error = kstrtoul(buf, 10, &tmp);
if (error < 0)
return error;
if (tmp != 0 && tmp != 1)
return -EINVAL;
if (tmp == 1 && servo->standby != 1)
pwm_disable(servo->pwm);
else if (tmp == 0 && servo->standby != 0) {
error = pwm_enable(servo->pwm);
if (error)
return error;
}
servo->standby = tmp;
return count;
}
static DEVICE_ATTR(angle, S_IWUSR | S_IRUGO, pwm_servo_show_angle,
pwm_servo_store_angle);
static DEVICE_ATTR(standby, S_IWUSR | S_IRUGO, pwm_servo_show_standby,
pwm_servo_store_standby);
static struct attribute *pwm_servo_attributes[] = {
&dev_attr_angle.attr,
&dev_attr_standby.attr,
NULL,
};
static const struct attribute_group pwm_servo_group = {
.attrs = pwm_servo_attributes,
};
static int pwm_servo_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct pinctrl *pinctrl;
struct device_node *node = dev->of_node;
struct pwm_servo_data *servo;
int error = 0;
if (node == NULL) {
dev_err(dev, "Non DT platforms not supported\n");
return -EINVAL;
}
pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
if (IS_ERR(pinctrl))
dev_warn(&pdev->dev, "Unable to select pin group\n");
servo = devm_kzalloc(&pdev->dev, sizeof(*servo), GFP_KERNEL);
if (!servo)
return -ENOMEM;
servo->pwm = devm_pwm_get(&pdev->dev, NULL);
if (IS_ERR(servo->pwm)) {
dev_err(&pdev->dev, "devm_pwm_get() failed\n");
return error;
}
servo->angle = 0;
servo->angle_duty = SERVO_MIN_DUTY;
servo->standby = 1;
error = pwm_config(servo->pwm, servo->angle_duty, SERVO_PWM_PERIOD);
if (error) {
dev_err(&pdev->dev, "pwm_config() failed\n");
return error;
}
error = pwm_set_polarity(servo->pwm, PWM_POLARITY_NORMAL);
if (error) {
dev_err(&pdev->dev, "pwm_set_polarity() failed\n");
return error;
}
platform_set_drvdata(pdev, servo);
error = sysfs_create_group(&pdev->dev.kobj, &pwm_servo_group);
if (error) {
dev_err(&pdev->dev, "sysfs_create_group() failed (%d)\n",
error);
return error;
}
return 0;
}
static int pwm_servo_remove(struct platform_device *pdev)
{
struct pwm_servo_data *servo = platform_get_drvdata(pdev);
pwm_disable(servo->pwm);
return 0;
}
#ifdef CONFIG_OF
static const struct of_device_id pwm_servo_match[] = {
{ .compatible = "pwm_servo", },
{ },
};
MODULE_DEVICE_TABLE(of, pwm_servo_match);
#endif
static struct platform_driver pwm_servo_driver = {
.probe = pwm_servo_probe,
.remove = pwm_servo_remove,
.driver = {
.name = "pwm-servo",
#ifdef CONFIG_OF
.of_match_table = of_match_ptr(pwm_servo_match),
#endif
},
};
module_platform_driver(pwm_servo_driver);
MODULE_AUTHOR("Adam Olek, Maciej Sobkowski <maciejjo@maciejjo.pl>");
MODULE_DESCRIPTION("PWM Servo driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:pwm-servo");