blob: 40833fa69efee56bda528bf866e6eaa238980262 [file] [log] [blame]
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +08001/*
2 * GPIO Greybus driver.
3 *
4 * Copyright 2014 Google Inc.
5 *
6 * Released under the GPLv2 only.
7 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/slab.h>
12#include <linux/gpio.h>
13#include "greybus.h"
14
Alex Elderbb2e1c92014-10-16 06:35:39 -050015struct gb_gpio_line {
16 /* The following has to be an array of line_max entries */
17 /* --> make them just a flags field */
18 u8 active: 1,
19 direction: 1, /* 0 = output, 1 = input */
20 value: 1; /* 0 = low, 1 = high */
21 u16 debounce_usec;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +080022};
23
Alex Elderbb2e1c92014-10-16 06:35:39 -050024struct gb_gpio_controller {
25 struct gb_connection *connection;
26 u8 version_major;
27 u8 version_minor;
28 u8 line_max; /* max line number */
29 struct gb_gpio_line *lines;
30
31 struct gpio_chip chip;
Alex Elderbb2e1c92014-10-16 06:35:39 -050032};
33#define gpio_chip_to_gb_gpio_controller(chip) \
34 container_of(chip, struct gb_gpio_controller, chip)
35
36/* Version of the Greybus GPIO protocol we support */
37#define GB_GPIO_VERSION_MAJOR 0x00
38#define GB_GPIO_VERSION_MINOR 0x01
39
40/* Greybus GPIO request types */
41#define GB_GPIO_TYPE_INVALID 0x00
42#define GB_GPIO_TYPE_PROTOCOL_VERSION 0x01
43#define GB_GPIO_TYPE_LINE_COUNT 0x02
44#define GB_GPIO_TYPE_ACTIVATE 0x03
45#define GB_GPIO_TYPE_DEACTIVATE 0x04
46#define GB_GPIO_TYPE_GET_DIRECTION 0x05
47#define GB_GPIO_TYPE_DIRECTION_IN 0x06
48#define GB_GPIO_TYPE_DIRECTION_OUT 0x07
49#define GB_GPIO_TYPE_GET_VALUE 0x08
50#define GB_GPIO_TYPE_SET_VALUE 0x09
51#define GB_GPIO_TYPE_SET_DEBOUNCE 0x0a
52#define GB_GPIO_TYPE_RESPONSE 0x80 /* OR'd with rest */
53
54#define GB_GPIO_DEBOUNCE_USEC_DEFAULT 0 /* microseconds */
55
56/* version request has no payload */
57struct gb_gpio_proto_version_response {
58 __u8 status;
59 __u8 major;
60 __u8 minor;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +080061};
62
Alex Elderbb2e1c92014-10-16 06:35:39 -050063/* line count request has no payload */
64struct gb_gpio_line_count_response {
65 __u8 status;
66 __u8 count;
67};
68
69struct gb_gpio_activate_request {
70 __u8 which;
71};
72struct gb_gpio_activate_response {
73 __u8 status;
74};
75
76struct gb_gpio_deactivate_request {
77 __u8 which;
78};
79struct gb_gpio_deactivate_response {
80 __u8 status;
81};
82
83struct gb_gpio_get_direction_request {
84 __u8 which;
85};
86struct gb_gpio_get_direction_response {
87 __u8 status;
88 __u8 direction;
89};
90
91struct gb_gpio_direction_in_request {
92 __u8 which;
93};
94struct gb_gpio_direction_in_response {
95 __u8 status;
96};
97
98struct gb_gpio_direction_out_request {
99 __u8 which;
100 __u8 value;
101};
102struct gb_gpio_direction_out_response {
103 __u8 status;
104};
105
106struct gb_gpio_get_value_request {
Alex Elderbb2e1c92014-10-16 06:35:39 -0500107 __u8 which;
108};
109struct gb_gpio_get_value_response {
110 __u8 status;
111 __u8 value;
112};
113
114struct gb_gpio_set_value_request {
115 __u8 which;
116 __u8 value;
117};
118struct gb_gpio_set_value_response {
119 __u8 status;
120};
121
122struct gb_gpio_set_debounce_request {
123 __u8 which;
124 __le16 usec;
125};
126struct gb_gpio_set_debounce_response {
127 __u8 status;
128};
129
130
131/*
132 * This request only uses the connection field, and if successful,
133 * fills in the major and minor protocol version of the target.
134 */
135static int gb_gpio_proto_version_operation(struct gb_gpio_controller *gb_gpio_controller)
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800136{
Alex Elderbb2e1c92014-10-16 06:35:39 -0500137 struct gb_connection *connection = gb_gpio_controller->connection;
138 struct gb_operation *operation;
139 struct gb_gpio_proto_version_response *response;
140 int ret;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800141
Alex Elderbb2e1c92014-10-16 06:35:39 -0500142 /* protocol version request has no payload */
143 operation = gb_operation_create(connection,
144 GB_GPIO_TYPE_PROTOCOL_VERSION,
145 0, sizeof(*response));
146 if (!operation)
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800147 return -ENOMEM;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800148
Alex Elderbb2e1c92014-10-16 06:35:39 -0500149 /* Synchronous operation--no callback */
150 ret = gb_operation_request_send(operation, NULL);
151 if (ret) {
152 pr_err("version operation failed (%d)\n", ret);
153 goto out;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800154 }
155
Alex Elderbb2e1c92014-10-16 06:35:39 -0500156 response = operation->response_payload;
157 if (response->status) {
158 gb_connection_err(connection, "version response %hhu",
159 response->status);
160 ret = -EIO;
161 } else {
162 if (response->major > GB_GPIO_VERSION_MAJOR) {
163 pr_err("unsupported major version (%hhu > %hhu)\n",
164 response->major, GB_GPIO_VERSION_MAJOR);
165 ret = -ENOTSUPP;
166 goto out;
167 }
168 gb_gpio_controller->version_major = response->major;
169 gb_gpio_controller->version_minor = response->minor;
170printk("%s: version_major = %u version_minor = %u\n", __func__,
171 gb_gpio_controller->version_major, gb_gpio_controller->version_minor);
172 }
173out:
174 gb_operation_destroy(operation);
175
176 return ret;
177}
178
179static int gb_gpio_line_count_operation(struct gb_gpio_controller *gb_gpio_controller)
180{
181 struct gb_connection *connection = gb_gpio_controller->connection;
182 struct gb_operation *operation;
183 struct gb_gpio_line_count_response *response;
184 int ret;
185
186 /* line count request has no payload */
187 operation = gb_operation_create(connection,
188 GB_GPIO_TYPE_LINE_COUNT,
189 0, sizeof(*response));
190 if (!operation)
191 return -ENOMEM;
192
193 /* Synchronous operation--no callback */
194 ret = gb_operation_request_send(operation, NULL);
195 if (ret) {
196 pr_err("line count operation failed (%d)\n", ret);
197 goto out;
198 }
199
200 response = operation->response_payload;
201 if (response->status) {
202 gb_connection_err(connection, "line count response %hhu",
203 response->status);
204 ret = -EIO;
205 } else {
206 gb_gpio_controller->line_max = response->count;
207printk("%s: count = %u\n", __func__,
208 gb_gpio_controller->line_max + 1);
209 }
210out:
211 gb_operation_destroy(operation);
212
213 return ret;
214}
215
216static int gb_gpio_activate_operation(struct gb_gpio_controller *gb_gpio_controller,
217 u8 which)
218{
219 struct gb_connection *connection = gb_gpio_controller->connection;
220 struct gb_operation *operation;
221 struct gb_gpio_activate_request *request;
222 struct gb_gpio_activate_response *response;
223 int ret;
224
225 if (which > gb_gpio_controller->line_max)
226 return -EINVAL;
227
228 /* activate response has no payload */
229 operation = gb_operation_create(connection,
230 GB_GPIO_TYPE_ACTIVATE,
231 sizeof(*request), sizeof(*response));
232 if (!operation)
233 return -ENOMEM;
234 request = operation->request_payload;
235 request->which = which;
236
237 /* Synchronous operation--no callback */
238 ret = gb_operation_request_send(operation, NULL);
239 if (ret) {
240 pr_err("activate operation failed (%d)\n", ret);
241 goto out;
242 }
243
244 response = operation->response_payload;
245 if (response->status) {
246 gb_connection_err(connection, "activate response %hhu",
247 response->status);
248 ret = -EIO;
249 } else {
250 gb_gpio_controller->lines[which].active = true;
251printk("%s: %u is now active\n", __func__, which);
252 }
253out:
254 gb_operation_destroy(operation);
255
256 return ret;
257}
258
259static int gb_gpio_deactivate_operation(struct gb_gpio_controller *gb_gpio_controller,
260 u8 which)
261{
262 struct gb_connection *connection = gb_gpio_controller->connection;
263 struct gb_operation *operation;
264 struct gb_gpio_deactivate_request *request;
265 struct gb_gpio_deactivate_response *response;
266 int ret;
267
268 if (which > gb_gpio_controller->line_max)
269 return -EINVAL;
270
271 /* deactivate response has no payload */
272 operation = gb_operation_create(connection,
273 GB_GPIO_TYPE_DEACTIVATE,
274 sizeof(*request), sizeof(*response));
275 if (!operation)
276 return -ENOMEM;
277 request = operation->request_payload;
278 request->which = which;
279
280 /* Synchronous operation--no callback */
281 ret = gb_operation_request_send(operation, NULL);
282 if (ret) {
283 pr_err("deactivate operation failed (%d)\n", ret);
284 goto out;
285 }
286
287 response = operation->response_payload;
288 if (response->status) {
289 gb_connection_err(connection, "deactivate response %hhu",
290 response->status);
291 ret = -EIO;
292 } else {
293 gb_gpio_controller->lines[which].active = false;
294printk("%s: %u is now inactive\n", __func__, which);
295 }
296out:
297 gb_operation_destroy(operation);
298
299 return ret;
300}
301
302static int gb_gpio_get_direction_operation(struct gb_gpio_controller *gb_gpio_controller,
303 u8 which)
304{
305 struct gb_connection *connection = gb_gpio_controller->connection;
306 struct gb_operation *operation;
307 struct gb_gpio_get_direction_request *request;
308 struct gb_gpio_get_direction_response *response;
309 int ret;
310
311 if (which > gb_gpio_controller->line_max)
312 return -EINVAL;
313
314 operation = gb_operation_create(connection,
315 GB_GPIO_TYPE_GET_DIRECTION,
316 sizeof(*request), sizeof(*response));
317 if (!operation)
318 return -ENOMEM;
319 request = operation->request_payload;
320 request->which = which;
321
322 /* Synchronous operation--no callback */
323 ret = gb_operation_request_send(operation, NULL);
324 if (ret) {
325 pr_err("get direction operation failed (%d)\n", ret);
326 goto out;
327 }
328
329 response = operation->response_payload;
330 if (response->status) {
331 gb_connection_err(connection, "get direction response %hhu",
332 response->status);
333 ret = -EIO;
334 } else {
335 u8 direction = response->direction;
336
337 if (direction && direction != 1)
338 pr_warn("gpio %u direction was %u (should be 0 or 1)\n",
339 which, direction);
340 gb_gpio_controller->lines[which].direction = direction ? 1 : 0;
341printk("%s: direction of %u is %s\n", __func__, which,
342 direction ? "in" : "out");
343 }
344out:
345 gb_operation_destroy(operation);
346
347 return ret;
348}
349
350static int gb_gpio_direction_in_operation(struct gb_gpio_controller *gb_gpio_controller,
351 u8 which)
352{
353 struct gb_connection *connection = gb_gpio_controller->connection;
354 struct gb_operation *operation;
355 struct gb_gpio_direction_in_request *request;
356 struct gb_gpio_direction_in_response *response;
357 int ret;
358
359 if (which > gb_gpio_controller->line_max)
360 return -EINVAL;
361
362 /* direction_in response has no payload */
363 operation = gb_operation_create(connection,
364 GB_GPIO_TYPE_DIRECTION_IN,
365 sizeof(*request), sizeof(*response));
366 if (!operation)
367 return -ENOMEM;
368 request = operation->request_payload;
369 request->which = which;
370
371 /* Synchronous operation--no callback */
372 ret = gb_operation_request_send(operation, NULL);
373 if (ret) {
374 pr_err("direction in operation failed (%d)\n", ret);
375 goto out;
376 }
377
378 response = operation->response_payload;
379 if (response->status) {
380 gb_connection_err(connection, "direction in response %hhu",
381 response->status);
382 ret = -EIO;
383 } else {
384 gb_gpio_controller->lines[which].direction = 1;
385printk("%s: direction of %u is now in\n", __func__, which);
386 }
387out:
388 gb_operation_destroy(operation);
389
390 return ret;
391}
392
393static int gb_gpio_direction_out_operation(struct gb_gpio_controller *gb_gpio_controller,
394 u8 which, bool value_high)
395{
396 struct gb_connection *connection = gb_gpio_controller->connection;
397 struct gb_operation *operation;
398 struct gb_gpio_direction_out_request *request;
399 struct gb_gpio_direction_out_response *response;
400 int ret;
401
402 if (which > gb_gpio_controller->line_max)
403 return -EINVAL;
404
405 /* direction_out response has no payload */
406 operation = gb_operation_create(connection,
407 GB_GPIO_TYPE_DIRECTION_OUT,
408 sizeof(*request), sizeof(*response));
409 if (!operation)
410 return -ENOMEM;
411 request = operation->request_payload;
412 request->which = which;
413 request->value = value_high ? 1 : 0;
414
415 /* Synchronous operation--no callback */
416 ret = gb_operation_request_send(operation, NULL);
417 if (ret) {
418 pr_err("direction out operation failed (%d)\n", ret);
419 goto out;
420 }
421
422 response = operation->response_payload;
423 if (response->status) {
424 gb_connection_err(connection, "direction out response %hhu",
425 response->status);
426 ret = -EIO;
427 } else {
428 gb_gpio_controller->lines[which].direction = 0;
429printk("%s: direction of %u is now out, value %s\n", __func__,
430 which, value_high ? "high" : "low");
431 }
432out:
433 gb_operation_destroy(operation);
434
435 return ret;
436}
437
438static int gb_gpio_get_value_operation(struct gb_gpio_controller *gb_gpio_controller,
439 u8 which)
440{
441 struct gb_connection *connection = gb_gpio_controller->connection;
442 struct gb_operation *operation;
443 struct gb_gpio_get_value_request *request;
444 struct gb_gpio_get_value_response *response;
445 int ret;
446
447 if (which > gb_gpio_controller->line_max)
448 return -EINVAL;
449
450 operation = gb_operation_create(connection,
451 GB_GPIO_TYPE_GET_VALUE,
452 sizeof(*request), sizeof(*response));
453 if (!operation)
454 return -ENOMEM;
455 request = operation->request_payload;
456 request->which = which;
457
458 /* Synchronous operation--no callback */
459 ret = gb_operation_request_send(operation, NULL);
460 if (ret) {
461 pr_err("get value operation failed (%d)\n", ret);
462 goto out;
463 }
464
465 response = operation->response_payload;
466 if (response->status) {
467 gb_connection_err(connection, "get value response %hhu",
468 response->status);
469 ret = -EIO;
470 } else {
471 u8 value = response->value;
472
473 if (value && value != 1)
474 pr_warn("gpio %u value was %u (should be 0 or 1)\n",
475 which, value);
476 gb_gpio_controller->lines[which].value = value ? 1 : 0;
477 /* XXX should this set direction to out? */
478printk("%s: value of %u is %s\n", __func__,
479 which, gb_gpio_controller->lines[which].value ? "high" : "low");
480 }
481out:
482 gb_operation_destroy(operation);
483
484 return ret;
485}
486
487static int gb_gpio_set_value_operation(struct gb_gpio_controller *gb_gpio_controller,
488 u8 which, bool value_high)
489{
490 struct gb_connection *connection = gb_gpio_controller->connection;
491 struct gb_operation *operation;
492 struct gb_gpio_set_value_request *request;
493 struct gb_gpio_set_value_response *response;
494 int ret;
495
496 if (which > gb_gpio_controller->line_max)
497 return -EINVAL;
498
499 /* set_value response has no payload */
500 operation = gb_operation_create(connection,
501 GB_GPIO_TYPE_SET_VALUE,
502 sizeof(*request), sizeof(*response));
503 if (!operation)
504 return -ENOMEM;
505 request = operation->request_payload;
506 request->which = which;
507 request->value = value_high ? 1 : 0;
508
509 /* Synchronous operation--no callback */
510 ret = gb_operation_request_send(operation, NULL);
511 if (ret) {
512 pr_err("set value operation failed (%d)\n", ret);
513 goto out;
514 }
515
516 response = operation->response_payload;
517 if (response->status) {
518 gb_connection_err(connection, "set value response %hhu",
519 response->status);
520 ret = -EIO;
521 } else {
522 /* XXX should this set direction to out? */
523 gb_gpio_controller->lines[which].value = request->value;
524printk("%s: out value of %u is now %s\n", __func__,
525 which, gb_gpio_controller->lines[which].value ? "high" : "low");
526 }
527out:
528 gb_operation_destroy(operation);
529
530 return ret;
531}
532
533static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *gb_gpio_controller,
534 u8 which, u16 debounce_usec)
535{
536 struct gb_connection *connection = gb_gpio_controller->connection;
537 struct gb_operation *operation;
538 struct gb_gpio_set_debounce_request *request;
539 struct gb_gpio_set_debounce_response *response;
540 int ret;
541
542 if (which > gb_gpio_controller->line_max)
543 return -EINVAL;
544
545 /* set_debounce response has no payload */
546 operation = gb_operation_create(connection,
547 GB_GPIO_TYPE_SET_DEBOUNCE,
548 sizeof(*request), sizeof(*response));
549 if (!operation)
550 return -ENOMEM;
551 request = operation->request_payload;
552 request->which = which;
Greg Kroah-Hartmand4c82472014-10-27 12:33:47 +0800553 request->usec = cpu_to_le16(debounce_usec);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500554
555 /* Synchronous operation--no callback */
556 ret = gb_operation_request_send(operation, NULL);
557 if (ret) {
558 pr_err("set debounce operation failed (%d)\n", ret);
559 goto out;
560 }
561
562 response = operation->response_payload;
563 if (response->status) {
564 gb_connection_err(connection, "set debounce response %hhu",
565 response->status);
566 ret = -EIO;
567 } else {
Greg Kroah-Hartmand4c82472014-10-27 12:33:47 +0800568 gb_gpio_controller->lines[which].debounce_usec = le16_to_cpu(request->usec);
569 printk("%s: debounce of %u is now %hu usec\n", __func__, which,
570 gb_gpio_controller->lines[which].debounce_usec);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500571 }
572out:
573 gb_operation_destroy(operation);
574
575 return ret;
576}
577
578static int gb_gpio_request(struct gpio_chip *chip, unsigned offset)
579{
580 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500581 int ret;
582
Matt Porterff6e0b92014-10-20 06:39:45 -0400583 if (offset < 0 || offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500584 return -EINVAL;
Matt Porterff6e0b92014-10-20 06:39:45 -0400585 printk("passed check\n");
586 ret = gb_gpio_activate_operation(gb_gpio_controller, (u8)offset);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500587 if (ret)
588 ; /* return ret; */
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800589 return 0;
590}
591
Alex Elderbb2e1c92014-10-16 06:35:39 -0500592static void gb_gpio_free(struct gpio_chip *chip, unsigned offset)
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800593{
Alex Elderbb2e1c92014-10-16 06:35:39 -0500594 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500595 int ret;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800596
Matt Porterff6e0b92014-10-20 06:39:45 -0400597 if (offset < 0 || offset >= chip->ngpio) {
598 pr_err("bad offset %u supplied (must be 0..%u)\n",
599 offset, chip->ngpio - 1);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500600 return;
601 }
Matt Porterff6e0b92014-10-20 06:39:45 -0400602 ret = gb_gpio_deactivate_operation(gb_gpio_controller, (u8)offset);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500603 if (ret)
604 ; /* return ret; */
605}
606
607static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
608{
609 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
610 u8 which;
611 int ret;
612
Matt Porterff6e0b92014-10-20 06:39:45 -0400613 if (offset < 0 || offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500614 return -EINVAL;
Matt Porterff6e0b92014-10-20 06:39:45 -0400615 which = (u8)offset;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500616 ret = gb_gpio_get_direction_operation(gb_gpio_controller, which);
617 if (ret)
618 ; /* return ret; */
619 return gb_gpio_controller->lines[which].direction ? 1 : 0;
620}
621
622static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
623{
624 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500625 int ret;
626
Matt Porterff6e0b92014-10-20 06:39:45 -0400627 if (offset < 0 || offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500628 return -EINVAL;
Matt Porterff6e0b92014-10-20 06:39:45 -0400629 ret = gb_gpio_direction_in_operation(gb_gpio_controller, (u8)offset);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500630 if (ret)
631 ; /* return ret; */
632 return 0;
633}
634
635static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
636 int value)
637{
638 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500639 int ret;
640
Matt Porterff6e0b92014-10-20 06:39:45 -0400641 if (offset < 0 || offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500642 return -EINVAL;
Matt Porterff6e0b92014-10-20 06:39:45 -0400643 ret = gb_gpio_direction_out_operation(gb_gpio_controller, (u8)offset, !!value);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500644 if (ret)
645 ; /* return ret; */
646 return 0;
647}
648
649static int gb_gpio_get(struct gpio_chip *chip, unsigned offset)
650{
651 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
652 u8 which;
653 int ret;
654
Matt Porterff6e0b92014-10-20 06:39:45 -0400655 if (offset < 0 || offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500656 return -EINVAL;
Matt Porterff6e0b92014-10-20 06:39:45 -0400657 which = (u8)offset;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500658 ret = gb_gpio_get_value_operation(gb_gpio_controller, which);
659 if (ret)
660 return ret;
661 return (int)gb_gpio_controller->lines[which].value;
662}
663
664static void gb_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
665{
666 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500667 int ret;
668
Matt Porterff6e0b92014-10-20 06:39:45 -0400669 if (offset < 0 || offset >= chip->ngpio) {
670 pr_err("bad offset %u supplied (must be 0..%u)\n",
671 offset, chip->ngpio - 1);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500672 return;
673 }
Matt Porterff6e0b92014-10-20 06:39:45 -0400674 ret = gb_gpio_set_value_operation(gb_gpio_controller, (u8)offset, !!value);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500675 if (ret)
676 ; /* return ret; */
677}
678
679static int gb_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
680 unsigned debounce)
681{
682 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
683 u16 usec;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500684 int ret;
685
Matt Porterff6e0b92014-10-20 06:39:45 -0400686 if (offset < 0 || offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500687 return -EINVAL;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500688 if (debounce > (unsigned int)U16_MAX)
689 return -EINVAL;
690 usec = (u8)debounce;
Matt Porterff6e0b92014-10-20 06:39:45 -0400691 ret = gb_gpio_set_debounce_operation(gb_gpio_controller, (u8)offset, usec);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500692 if (ret)
693 ; /* return ret; */
694
695 return 0; /* XXX */
696}
697
698static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
699{
Matt Porterff6e0b92014-10-20 06:39:45 -0400700 if (offset < 0 || offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500701 return -EINVAL;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500702
703 return 0; /* XXX */
704}
705
706static void gb_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
707{
708 return; /* XXX */
709}
710
Greg Kroah-Hartmand4c82472014-10-27 12:33:47 +0800711static int gb_gpio_controller_setup(struct gb_gpio_controller *gb_gpio_controller)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500712{
713 u32 line_count;
714 size_t size;
715 int ret;
716
717 /* First thing we need to do is check the version */
718 ret = gb_gpio_proto_version_operation(gb_gpio_controller);
719 if (ret)
720 ; /* return ret; */
721
722 /* Now find out how many lines there are */
723 ret = gb_gpio_line_count_operation(gb_gpio_controller);
724 if (ret)
725 ; /* return ret; */
726 line_count = (u32)gb_gpio_controller->line_max + 1;
727 size = line_count * sizeof(*gb_gpio_controller->lines);
728 gb_gpio_controller->lines = kzalloc(size, GFP_KERNEL);
729 if (!gb_gpio_controller->lines)
730 return -ENOMEM;
731
732 return ret;
733}
734
Alex Elder3689f972014-10-27 06:04:30 -0500735static int gb_gpio_connection_init(struct gb_connection *connection)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500736{
737 struct gb_gpio_controller *gb_gpio_controller;
738 struct gpio_chip *gpio;
739 int ret;
740
741 gb_gpio_controller = kzalloc(sizeof(*gb_gpio_controller), GFP_KERNEL);
742 if (!gb_gpio_controller)
743 return -ENOMEM;
744 gb_gpio_controller->connection = connection;
745
746 ret = gb_gpio_controller_setup(gb_gpio_controller);
747 if (ret)
748 goto out_err;
749
750 gpio = &gb_gpio_controller->chip;
751
752 gpio->label = "greybus_gpio";
753 gpio->owner = THIS_MODULE; /* XXX Module get? */
754
755 gpio->request = gb_gpio_request;
756 gpio->free = gb_gpio_free;
757 gpio->get_direction = gb_gpio_get_direction;
758 gpio->direction_input = gb_gpio_direction_input;
759 gpio->direction_output = gb_gpio_direction_output;
760 gpio->get = gb_gpio_get;
761 gpio->set = gb_gpio_set;
762 gpio->set_debounce = gb_gpio_set_debounce;
763 gpio->to_irq = gb_gpio_to_irq;
764 gpio->dbg_show = gb_gpio_dbg_show;
765
766 gpio->base = -1; /* Allocate base dynamically */
767 gpio->ngpio = gb_gpio_controller->line_max + 1;
768 gpio->can_sleep = true; /* XXX */
769
770 ret = gpiochip_add(gpio);
771 if (ret) {
772 pr_err("Failed to register GPIO\n");
773 return ret;
774 }
775 connection->private = gb_gpio_controller;
776
777 return 0;
778out_err:
779 kfree(gb_gpio_controller);
780 return ret;
781}
782
Alex Elder3689f972014-10-27 06:04:30 -0500783static void gb_gpio_connection_exit(struct gb_connection *connection)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500784{
785 struct gb_gpio_controller *gb_gpio_controller = connection->private;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500786
787 if (!gb_gpio_controller)
Alex Elder051fb042014-10-16 06:35:24 -0500788 return;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800789
Greg Kroah-Hartman213aefe2014-10-20 13:40:02 +0800790 gb_gpiochip_remove(&gb_gpio_controller->chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500791 /* kref_put(gb_gpio_controller->connection) */
792 kfree(gb_gpio_controller);
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800793}
794
Alex Elder19d03de2014-11-05 16:12:53 -0600795static struct gb_protocol gpio_protocol = {
796 .id = GREYBUS_PROTOCOL_GPIO,
797 .major = 0,
798 .minor = 1,
Alex Elder5d9fd7e2014-11-05 16:12:54 -0600799 .connection_init = gb_gpio_connection_init,
800 .connection_exit = gb_gpio_connection_exit,
Alex Elderf8fb05e2014-11-05 16:12:55 -0600801 .request_recv = NULL, /* no incoming requests */
Alex Elder19d03de2014-11-05 16:12:53 -0600802};
803
804bool gb_gpio_protocol_init(void)
805{
806 return gb_protocol_register(&gpio_protocol);
807}
808
809void gb_gpio_protocol_exit(void)
810{
811 gb_protocol_deregister(&gpio_protocol);
812}