greybus: Remove "-gb" suffix from .c files
[cascardo/linux.git] / drivers / staging / greybus / pwm.c
1 /*
2  * PWM Greybus driver.
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/pwm.h>
14 #include "greybus.h"
15
16 struct gb_pwm_chip {
17         struct gb_connection    *connection;
18         u8                      version_major;
19         u8                      version_minor;
20         u8                      pwm_max;        /* max pwm number */
21
22         struct pwm_chip         chip;
23         struct pwm_chip         *pwm;
24 };
25 #define pwm_chip_to_gb_pwm_chip(chip) \
26         container_of(chip, struct gb_pwm_chip, chip)
27
28 /* Version of the Greybus PWM protocol we support */
29 #define GB_PWM_VERSION_MAJOR            0x00
30 #define GB_PWM_VERSION_MINOR            0x01
31
32 /* Greybus PWM request types */
33 #define GB_PWM_TYPE_INVALID             0x00
34 #define GB_PWM_TYPE_PROTOCOL_VERSION    0x01
35 #define GB_PWM_TYPE_PWM_COUNT           0x02
36 #define GB_PWM_TYPE_ACTIVATE            0x03
37 #define GB_PWM_TYPE_DEACTIVATE          0x04
38 #define GB_PWM_TYPE_CONFIG              0x05
39 #define GB_PWM_TYPE_POLARITY            0x06
40 #define GB_PWM_TYPE_ENABLE              0x07
41 #define GB_PWM_TYPE_DISABLE             0x08
42 #define GB_PWM_TYPE_RESPONSE            0x80    /* OR'd with rest */
43
44 /* version request has no payload */
45 struct gb_pwm_proto_version_response {
46         __u8    major;
47         __u8    minor;
48 };
49
50 /* pwm count request has no payload */
51 struct gb_pwm_count_response {
52         __u8    count;
53 };
54
55 struct gb_pwm_activate_request {
56         __u8    which;
57 };
58
59 struct gb_pwm_deactivate_request {
60         __u8    which;
61 };
62
63 struct gb_pwm_config_request {
64         __u8    which;
65         __le32  duty;
66         __le32  period;
67 };
68
69 struct gb_pwm_polarity_request {
70         __u8    which;
71         __u8    polarity;
72 };
73
74 struct gb_pwm_enable_request {
75         __u8    which;
76 };
77
78 struct gb_pwm_disable_request {
79         __u8    which;
80 };
81
82 /*
83  * This request only uses the connection field, and if successful,
84  * fills in the major and minor protocol version of the target.
85  */
86 static int gb_pwm_proto_version_operation(struct gb_pwm_chip *pwmc)
87 {
88         struct gb_pwm_proto_version_response response;
89         int ret;
90
91         ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PROTOCOL_VERSION,
92                                 NULL, 0, &response, sizeof(response));
93
94         if (ret)
95                 return ret;
96
97         if (response.major > GB_PWM_VERSION_MAJOR) {
98                 pr_err("unsupported major version (%hhu > %hhu)\n",
99                         response.major, GB_PWM_VERSION_MAJOR);
100                 return -ENOTSUPP;
101         }
102         pwmc->version_major = response.major;
103         pwmc->version_minor = response.minor;
104         return 0;
105 }
106
107 static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
108 {
109         struct gb_pwm_count_response response;
110         int ret;
111
112         ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
113                                 NULL, 0, &response, sizeof(response));
114         if (ret)
115                 return ret;
116         pwmc->pwm_max = response.count;
117         return 0;
118 }
119
120 static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
121                                      u8 which)
122 {
123         struct gb_pwm_activate_request request;
124
125         if (which > pwmc->pwm_max)
126                 return -EINVAL;
127
128         request.which = which;
129         return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
130                                  &request, sizeof(request), NULL, 0);
131 }
132
133 static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
134                                        u8 which)
135 {
136         struct gb_pwm_deactivate_request request;
137
138         if (which > pwmc->pwm_max)
139                 return -EINVAL;
140
141         request.which = which;
142         return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
143                                  &request, sizeof(request), NULL, 0);
144 }
145
146 static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
147                                    u8 which, u32 duty, u32 period)
148 {
149         struct gb_pwm_config_request request;
150
151         if (which > pwmc->pwm_max)
152                 return -EINVAL;
153
154         request.which = which;
155         request.duty = cpu_to_le32(duty);
156         request.period = cpu_to_le32(period);
157         return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
158                                  &request, sizeof(request), NULL, 0);
159 }
160
161
162 static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
163                                          u8 which, u8 polarity)
164 {
165         struct gb_pwm_polarity_request request;
166
167         if (which > pwmc->pwm_max)
168                 return -EINVAL;
169
170         request.which = which;
171         request.polarity = polarity;
172         return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
173                                  &request, sizeof(request), NULL, 0);
174 }
175
176 static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
177                                    u8 which)
178 {
179         struct gb_pwm_enable_request request;
180
181         if (which > pwmc->pwm_max)
182                 return -EINVAL;
183
184         request.which = which;
185         return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
186                                  &request, sizeof(request), NULL, 0);
187 }
188
189 static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
190                                     u8 which)
191 {
192         struct gb_pwm_disable_request request;
193
194         if (which > pwmc->pwm_max)
195                 return -EINVAL;
196
197         request.which = which;
198         return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
199                                  &request, sizeof(request), NULL, 0);
200 }
201
202 static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
203 {
204         struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
205
206         return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
207 };
208
209 static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
210 {
211         struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
212
213         if (test_bit(PWMF_ENABLED, &pwm->flags))
214                 dev_warn(chip->dev, "freeing PWM device without disabling\n");
215
216         gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
217 }
218
219 static int gb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
220                          int duty_ns, int period_ns)
221 {
222         struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
223
224         return gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_ns, period_ns);
225 };
226
227 static int gb_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
228                                enum pwm_polarity polarity)
229 {
230         struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
231
232         return gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, polarity);
233 };
234
235 static int gb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
236 {
237         struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
238
239         return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
240 };
241
242 static void gb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
243 {
244         struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
245
246         gb_pwm_disable_operation(pwmc, pwm->hwpwm);
247 };
248
249 static const struct pwm_ops gb_pwm_ops = {
250         .request = gb_pwm_request,
251         .free = gb_pwm_free,
252         .config = gb_pwm_config,
253         .set_polarity = gb_pwm_set_polarity,
254         .enable = gb_pwm_enable,
255         .disable = gb_pwm_disable,
256         .owner = THIS_MODULE,
257 };
258
259 static int gb_pwm_connection_init(struct gb_connection *connection)
260 {
261         struct gb_pwm_chip *pwmc;
262         struct pwm_chip *pwm;
263         int ret;
264
265         pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
266         if (!pwmc)
267                 return -ENOMEM;
268         pwmc->connection = connection;
269         connection->private = pwmc;
270
271         /* Check for compatible protocol version */
272         ret = gb_pwm_proto_version_operation(pwmc);
273         if (ret)
274                 goto out_err;
275
276         /* Query number of pwms present */
277         ret = gb_pwm_count_operation(pwmc);
278         if (ret)
279                 goto out_err;
280
281         pwm = &pwmc->chip;
282
283         pwm->dev = &connection->dev;
284         pwm->ops = &gb_pwm_ops;
285         pwm->base = -1;                 /* Allocate base dynamically */
286         pwm->npwm = pwmc->pwm_max + 1;
287         pwm->can_sleep = true;          /* FIXME */
288
289         ret = pwmchip_add(pwm);
290         if (ret) {
291                 pr_err("Failed to register PWM\n");
292                 return ret;
293         }
294
295         return 0;
296 out_err:
297         kfree(pwmc);
298         return ret;
299 }
300
301 static void gb_pwm_connection_exit(struct gb_connection *connection)
302 {
303         struct gb_pwm_chip *pwmc = connection->private;
304
305         if (!pwmc)
306                 return;
307
308         pwmchip_remove(&pwmc->chip);
309         /* kref_put(pwmc->connection) */
310         kfree(pwmc);
311 }
312
313 static struct gb_protocol pwm_protocol = {
314         .name                   = "pwm",
315         .id                     = GREYBUS_PROTOCOL_PWM,
316         .major                  = 0,
317         .minor                  = 1,
318         .connection_init        = gb_pwm_connection_init,
319         .connection_exit        = gb_pwm_connection_exit,
320         .request_recv           = NULL, /* no incoming requests */
321 };
322
323 int gb_pwm_protocol_init(void)
324 {
325         return gb_protocol_register(&pwm_protocol);
326 }
327
328 void gb_pwm_protocol_exit(void)
329 {
330         gb_protocol_deregister(&pwm_protocol);
331 }