]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/gpio-gb.c
greybus: gpb: Create a "GP Bridge" kernel module
[karo-tx-linux.git] / drivers / staging / greybus / gpio-gb.c
1 /*
2  * GPIO 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/gpio.h>
14 #include "greybus.h"
15
16 struct gb_gpio_line {
17         /* The following has to be an array of line_max entries */
18         /* --> make them just a flags field */
19         u8                      active:    1,
20                                 direction: 1,   /* 0 = output, 1 = input */
21                                 value:     1;   /* 0 = low, 1 = high */
22         u16                     debounce_usec;
23 };
24
25 struct gb_gpio_controller {
26         struct gb_connection    *connection;
27         u8                      version_major;
28         u8                      version_minor;
29         u8                      line_max;       /* max line number */
30         struct gb_gpio_line     *lines;
31
32         struct gpio_chip        chip;
33 };
34 #define gpio_chip_to_gb_gpio_controller(chip) \
35         container_of(chip, struct gb_gpio_controller, chip)
36
37 /* Version of the Greybus GPIO protocol we support */
38 #define GB_GPIO_VERSION_MAJOR           0x00
39 #define GB_GPIO_VERSION_MINOR           0x01
40
41 /* Greybus GPIO request types */
42 #define GB_GPIO_TYPE_INVALID            0x00
43 #define GB_GPIO_TYPE_PROTOCOL_VERSION   0x01
44 #define GB_GPIO_TYPE_LINE_COUNT         0x02
45 #define GB_GPIO_TYPE_ACTIVATE           0x03
46 #define GB_GPIO_TYPE_DEACTIVATE         0x04
47 #define GB_GPIO_TYPE_GET_DIRECTION      0x05
48 #define GB_GPIO_TYPE_DIRECTION_IN       0x06
49 #define GB_GPIO_TYPE_DIRECTION_OUT      0x07
50 #define GB_GPIO_TYPE_GET_VALUE          0x08
51 #define GB_GPIO_TYPE_SET_VALUE          0x09
52 #define GB_GPIO_TYPE_SET_DEBOUNCE       0x0a
53 #define GB_GPIO_TYPE_RESPONSE           0x80    /* OR'd with rest */
54
55 #define GB_GPIO_DEBOUNCE_USEC_DEFAULT   0       /* microseconds */
56
57 /* version request has no payload */
58 struct gb_gpio_proto_version_response {
59         __u8    major;
60         __u8    minor;
61 };
62
63 /* line count request has no payload */
64 struct gb_gpio_line_count_response {
65         __u8    count;
66 };
67
68 struct gb_gpio_activate_request {
69         __u8    which;
70 };
71 /* activate response has no payload */
72
73 struct gb_gpio_deactivate_request {
74         __u8    which;
75 };
76 /* deactivate response has no payload */
77
78 struct gb_gpio_get_direction_request {
79         __u8    which;
80 };
81 struct gb_gpio_get_direction_response {
82         __u8    direction;
83 };
84
85 struct gb_gpio_direction_in_request {
86         __u8    which;
87 };
88 /* direction in response has no payload */
89
90 struct gb_gpio_direction_out_request {
91         __u8    which;
92         __u8    value;
93 };
94 /* direction out response has no payload */
95
96 struct gb_gpio_get_value_request {
97         __u8    which;
98 };
99 struct gb_gpio_get_value_response {
100         __u8    value;
101 };
102
103 struct gb_gpio_set_value_request {
104         __u8    which;
105         __u8    value;
106 };
107 /* set value response has no payload */
108
109 struct gb_gpio_set_debounce_request {
110         __u8    which;
111         __le16  usec;
112 };
113 /* debounce response has no payload */
114
115
116 /*
117  * This request only uses the connection field, and if successful,
118  * fills in the major and minor protocol version of the target.
119  */
120 static int gb_gpio_proto_version_operation(struct gb_gpio_controller *ggc)
121 {
122         struct gb_gpio_proto_version_response response;
123         int ret;
124
125         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_PROTOCOL_VERSION,
126                                 NULL, 0, &response, sizeof(response));
127         if (ret)
128                 return ret;
129
130         if (response.major > GB_GPIO_VERSION_MAJOR) {
131                 pr_err("unsupported major version (%hhu > %hhu)\n",
132                         response.major, GB_GPIO_VERSION_MAJOR);
133                 return -ENOTSUPP;
134         }
135         ggc->version_major = response.major;
136         ggc->version_minor = response.minor;
137         return 0;
138 }
139
140 static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc)
141 {
142         struct gb_gpio_line_count_response response;
143         int ret;
144
145         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT,
146                                 NULL, 0, &response, sizeof(response));
147         if (!ret)
148                 ggc->line_max = response.count;
149         return ret;
150 }
151
152 static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which)
153 {
154         struct gb_gpio_activate_request request;
155         int ret;
156
157         if (which > ggc->line_max)
158                 return -EINVAL;
159
160         request.which = which;
161         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE,
162                                  &request, sizeof(request), NULL, 0);
163         if (!ret)
164                 ggc->lines[which].active = true;
165         return ret;
166 }
167
168 static int gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc,
169                                         u8 which)
170 {
171         struct gb_gpio_deactivate_request request;
172         int ret;
173
174         if (which > ggc->line_max)
175                 return -EINVAL;
176
177         request.which = which;
178         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE,
179                                  &request, sizeof(request), NULL, 0);
180         if (!ret)
181                 ggc->lines[which].active = false;
182         return ret;
183 }
184
185 static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc,
186                                         u8 which)
187 {
188         struct gb_gpio_get_direction_request request;
189         struct gb_gpio_get_direction_response response;
190         int ret;
191         u8 direction;
192
193         if (which > ggc->line_max)
194                 return -EINVAL;
195
196         request.which = which;
197         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION,
198                                 &request, sizeof(request),
199                                 &response, sizeof(response));
200         if (ret)
201                 return ret;
202
203         direction = response.direction;
204         if (direction && direction != 1)
205                 pr_warn("gpio %u direction was %u (should be 0 or 1)\n",
206                         which, direction);
207         ggc->lines[which].direction = direction ? 1 : 0;
208         return 0;
209 }
210
211 static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc,
212                                         u8 which)
213 {
214         struct gb_gpio_direction_in_request request;
215         int ret;
216
217         if (which > ggc->line_max)
218                 return -EINVAL;
219
220         request.which = which;
221         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN,
222                                 &request, sizeof(request), NULL, 0);
223         if (!ret)
224                 ggc->lines[which].direction = 1;
225         return ret;
226 }
227
228 static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc,
229                                         u8 which, bool value_high)
230 {
231         struct gb_gpio_direction_out_request request;
232         int ret;
233
234         if (which > ggc->line_max)
235                 return -EINVAL;
236
237         request.which = which;
238         request.value = value_high ? 1 : 0;
239         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT,
240                                 &request, sizeof(request), NULL, 0);
241         if (!ret)
242                 ggc->lines[which].direction = 0;
243         return ret;
244 }
245
246 static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc,
247                                         u8 which)
248 {
249         struct gb_gpio_get_value_request request;
250         struct gb_gpio_get_value_response response;
251         int ret;
252         u8 value;
253
254         if (which > ggc->line_max)
255                 return -EINVAL;
256
257         request.which = which;
258         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE,
259                                 &request, sizeof(request),
260                                 &response, sizeof(response));
261         if (ret)
262                 return ret;
263
264         value = response.value;
265         if (value && value != 1)
266                 pr_warn("gpio %u value was %u (should be 0 or 1)\n",
267                         which, value);
268         ggc->lines[which].value = value ? 1 : 0;
269         return 0;
270 }
271
272 static int gb_gpio_set_value_operation(struct gb_gpio_controller *ggc,
273                                         u8 which, bool value_high)
274 {
275         struct gb_gpio_set_value_request request;
276         int ret;
277
278         if (which > ggc->line_max)
279                 return -EINVAL;
280
281         request.which = which;
282         request.value = value_high ? 1 : 0;
283         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE,
284                                 &request, sizeof(request), NULL, 0);
285         if (!ret) {
286                 /* XXX should this set direction to out? */
287                 ggc->lines[which].value = request.value;
288         }
289         return ret;
290 }
291
292 static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc,
293                                         u8 which, u16 debounce_usec)
294 {
295         struct gb_gpio_set_debounce_request request;
296         int ret;
297
298         if (which > ggc->line_max)
299                 return -EINVAL;
300
301         request.which = which;
302         request.usec = cpu_to_le16(debounce_usec);
303         ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE,
304                                 &request, sizeof(request), NULL, 0);
305         if (!ret)
306                 ggc->lines[which].debounce_usec = debounce_usec;
307         return ret;
308 }
309
310 static int gb_gpio_request(struct gpio_chip *chip, unsigned offset)
311 {
312         struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
313         int ret;
314
315         if (offset < 0 || offset >= chip->ngpio)
316                 return -EINVAL;
317         ret = gb_gpio_activate_operation(gb_gpio_controller, (u8)offset);
318         if (ret)
319                 ;       /* return ret; */
320         return 0;
321 }
322
323 static void gb_gpio_free(struct gpio_chip *chip, unsigned offset)
324 {
325         struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
326         int ret;
327
328         if (offset < 0 || offset >= chip->ngpio) {
329                 pr_err("bad offset %u supplied (must be 0..%u)\n",
330                         offset, chip->ngpio - 1);
331                 return;
332         }
333         ret = gb_gpio_deactivate_operation(gb_gpio_controller, (u8)offset);
334         if (ret)
335                 ;       /* return ret; */
336 }
337
338 static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
339 {
340         struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
341         u8 which;
342         int ret;
343
344         if (offset < 0 || offset >= chip->ngpio)
345                 return -EINVAL;
346         which = (u8)offset;
347         ret = gb_gpio_get_direction_operation(gb_gpio_controller, which);
348         if (ret)
349                 ;       /* return ret; */
350         return gb_gpio_controller->lines[which].direction ? 1 : 0;
351 }
352
353 static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
354 {
355         struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
356         int ret;
357
358         if (offset < 0 || offset >= chip->ngpio)
359                 return -EINVAL;
360         ret = gb_gpio_direction_in_operation(gb_gpio_controller, (u8)offset);
361         if (ret)
362                 ;       /* return ret; */
363         return 0;
364 }
365
366 static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
367                                         int value)
368 {
369         struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
370         int ret;
371
372         if (offset < 0 || offset >= chip->ngpio)
373                 return -EINVAL;
374         ret = gb_gpio_direction_out_operation(gb_gpio_controller, (u8)offset, !!value);
375         if (ret)
376                 ;       /* return ret; */
377         return 0;
378 }
379
380 static int gb_gpio_get(struct gpio_chip *chip, unsigned offset)
381 {
382         struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
383         u8 which;
384         int ret;
385
386         if (offset < 0 || offset >= chip->ngpio)
387                 return -EINVAL;
388         which = (u8)offset;
389         ret = gb_gpio_get_value_operation(gb_gpio_controller, which);
390         if (ret)
391                 return ret;
392         return (int)gb_gpio_controller->lines[which].value;
393 }
394
395 static void gb_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
396 {
397         struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
398         int ret;
399
400         if (offset < 0 || offset >= chip->ngpio) {
401                 pr_err("bad offset %u supplied (must be 0..%u)\n",
402                         offset, chip->ngpio - 1);
403                 return;
404         }
405         ret = gb_gpio_set_value_operation(gb_gpio_controller, (u8)offset, !!value);
406         if (ret)
407                 ;       /* return ret; */
408 }
409
410 static int gb_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
411                                         unsigned debounce)
412 {
413         struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
414         u16 usec;
415         int ret;
416
417         if (offset < 0 || offset >= chip->ngpio)
418                 return -EINVAL;
419         if (debounce > (unsigned int)U16_MAX)
420                 return -EINVAL;
421         usec = (u8)debounce;
422         ret = gb_gpio_set_debounce_operation(gb_gpio_controller, (u8)offset, usec);
423         if (ret)
424                 ;       /* return ret; */
425
426         return 0;       /* XXX */
427 }
428
429 static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
430 {
431         if (offset < 0 || offset >= chip->ngpio)
432                 return -EINVAL;
433
434         return 0;       /* XXX */
435 }
436
437 static void gb_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
438 {
439         return; /* XXX */
440 }
441
442 static int gb_gpio_controller_setup(struct gb_gpio_controller *gb_gpio_controller)
443 {
444         u32 line_count;
445         size_t size;
446         int ret;
447
448         /* First thing we need to do is check the version */
449         ret = gb_gpio_proto_version_operation(gb_gpio_controller);
450         if (ret)
451                 ;       /* return ret; */
452
453         /* Now find out how many lines there are */
454         ret = gb_gpio_line_count_operation(gb_gpio_controller);
455         if (ret)
456                 ;       /* return ret; */
457         line_count = (u32)gb_gpio_controller->line_max + 1;
458         size = line_count * sizeof(*gb_gpio_controller->lines);
459         gb_gpio_controller->lines = kzalloc(size, GFP_KERNEL);
460         if (!gb_gpio_controller->lines)
461                 return -ENOMEM;
462
463         return ret;
464 }
465
466 static int gb_gpio_connection_init(struct gb_connection *connection)
467 {
468         struct gb_gpio_controller *gb_gpio_controller;
469         struct gpio_chip *gpio;
470         int ret;
471
472         gb_gpio_controller = kzalloc(sizeof(*gb_gpio_controller), GFP_KERNEL);
473         if (!gb_gpio_controller)
474                 return -ENOMEM;
475         gb_gpio_controller->connection = connection;
476         connection->private = gb_gpio_controller;
477
478         ret = gb_gpio_controller_setup(gb_gpio_controller);
479         if (ret)
480                 goto out_err;
481
482         gpio = &gb_gpio_controller->chip;
483
484         gpio->label = "greybus_gpio";
485         gpio->owner = THIS_MODULE;      /* XXX Module get? */
486
487         gpio->request = gb_gpio_request;
488         gpio->free = gb_gpio_free;
489         gpio->get_direction = gb_gpio_get_direction;
490         gpio->direction_input = gb_gpio_direction_input;
491         gpio->direction_output = gb_gpio_direction_output;
492         gpio->get = gb_gpio_get;
493         gpio->set = gb_gpio_set;
494         gpio->set_debounce = gb_gpio_set_debounce;
495         gpio->to_irq = gb_gpio_to_irq;
496         gpio->dbg_show = gb_gpio_dbg_show;
497
498         gpio->base = -1;                /* Allocate base dynamically */
499         gpio->ngpio = gb_gpio_controller->line_max + 1;
500         gpio->can_sleep = true;         /* XXX */
501
502         ret = gpiochip_add(gpio);
503         if (ret) {
504                 pr_err("Failed to register GPIO\n");
505                 return ret;
506         }
507
508         return 0;
509 out_err:
510         kfree(gb_gpio_controller);
511         return ret;
512 }
513
514 static void gb_gpio_connection_exit(struct gb_connection *connection)
515 {
516         struct gb_gpio_controller *gb_gpio_controller = connection->private;
517
518         if (!gb_gpio_controller)
519                 return;
520
521         gb_gpiochip_remove(&gb_gpio_controller->chip);
522         /* kref_put(gb_gpio_controller->connection) */
523         kfree(gb_gpio_controller);
524 }
525
526 static struct gb_protocol gpio_protocol = {
527         .id                     = GREYBUS_PROTOCOL_GPIO,
528         .major                  = 0,
529         .minor                  = 1,
530         .connection_init        = gb_gpio_connection_init,
531         .connection_exit        = gb_gpio_connection_exit,
532         .request_recv           = NULL, /* no incoming requests */
533 };
534
535 int gb_gpio_protocol_init(void)
536 {
537         return gb_protocol_register(&gpio_protocol);
538 }
539
540 void gb_gpio_protocol_exit(void)
541 {
542         gb_protocol_deregister(&gpio_protocol);
543 }