4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/gpio.h>
17 /* The following has to be an array of line_max entries */
18 /* --> make them just a flags field */
20 direction: 1, /* 0 = output, 1 = input */
21 value: 1; /* 0 = low, 1 = high */
25 struct gb_gpio_controller {
26 struct gb_connection *connection;
29 u8 line_max; /* max line number */
30 struct gb_gpio_line *lines;
32 struct gpio_chip chip;
34 #define gpio_chip_to_gb_gpio_controller(chip) \
35 container_of(chip, struct gb_gpio_controller, chip)
37 /* Version of the Greybus GPIO protocol we support */
38 #define GB_GPIO_VERSION_MAJOR 0x00
39 #define GB_GPIO_VERSION_MINOR 0x01
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 */
55 #define GB_GPIO_DEBOUNCE_USEC_DEFAULT 0 /* microseconds */
57 /* version request has no payload */
58 struct gb_gpio_proto_version_response {
63 /* line count request has no payload */
64 struct gb_gpio_line_count_response {
68 struct gb_gpio_activate_request {
71 /* activate response has no payload */
73 struct gb_gpio_deactivate_request {
76 /* deactivate response has no payload */
78 struct gb_gpio_get_direction_request {
81 struct gb_gpio_get_direction_response {
85 struct gb_gpio_direction_in_request {
88 /* direction in response has no payload */
90 struct gb_gpio_direction_out_request {
94 /* direction out response has no payload */
96 struct gb_gpio_get_value_request {
99 struct gb_gpio_get_value_response {
103 struct gb_gpio_set_value_request {
107 /* set value response has no payload */
109 struct gb_gpio_set_debounce_request {
113 /* debounce response has no payload */
117 * This request only uses the connection field, and if successful,
118 * fills in the major and minor protocol version of the target.
120 static int gb_gpio_proto_version_operation(struct gb_gpio_controller *ggc)
122 struct gb_gpio_proto_version_response response;
125 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_PROTOCOL_VERSION,
126 NULL, 0, &response, sizeof(response));
130 if (response.major > GB_GPIO_VERSION_MAJOR) {
131 pr_err("unsupported major version (%hhu > %hhu)\n",
132 response.major, GB_GPIO_VERSION_MAJOR);
135 ggc->version_major = response.major;
136 ggc->version_minor = response.minor;
140 static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc)
142 struct gb_gpio_line_count_response response;
145 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT,
146 NULL, 0, &response, sizeof(response));
148 ggc->line_max = response.count;
152 static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which)
154 struct gb_gpio_activate_request request;
157 if (which > ggc->line_max)
160 request.which = which;
161 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE,
162 &request, sizeof(request), NULL, 0);
164 ggc->lines[which].active = true;
168 static int gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc,
171 struct gb_gpio_deactivate_request request;
174 if (which > ggc->line_max)
177 request.which = which;
178 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE,
179 &request, sizeof(request), NULL, 0);
181 ggc->lines[which].active = false;
185 static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc,
188 struct gb_gpio_get_direction_request request;
189 struct gb_gpio_get_direction_response response;
193 if (which > ggc->line_max)
196 request.which = which;
197 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION,
198 &request, sizeof(request),
199 &response, sizeof(response));
203 direction = response.direction;
204 if (direction && direction != 1)
205 pr_warn("gpio %u direction was %u (should be 0 or 1)\n",
207 ggc->lines[which].direction = direction ? 1 : 0;
211 static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc,
214 struct gb_gpio_direction_in_request request;
217 if (which > ggc->line_max)
220 request.which = which;
221 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN,
222 &request, sizeof(request), NULL, 0);
224 ggc->lines[which].direction = 1;
228 static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc,
229 u8 which, bool value_high)
231 struct gb_gpio_direction_out_request request;
234 if (which > ggc->line_max)
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);
242 ggc->lines[which].direction = 0;
246 static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc,
249 struct gb_gpio_get_value_request request;
250 struct gb_gpio_get_value_response response;
254 if (which > ggc->line_max)
257 request.which = which;
258 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE,
259 &request, sizeof(request),
260 &response, sizeof(response));
264 value = response.value;
265 if (value && value != 1)
266 pr_warn("gpio %u value was %u (should be 0 or 1)\n",
268 ggc->lines[which].value = value ? 1 : 0;
272 static int gb_gpio_set_value_operation(struct gb_gpio_controller *ggc,
273 u8 which, bool value_high)
275 struct gb_gpio_set_value_request request;
278 if (which > ggc->line_max)
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);
286 /* XXX should this set direction to out? */
287 ggc->lines[which].value = request.value;
292 static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc,
293 u8 which, u16 debounce_usec)
295 struct gb_gpio_set_debounce_request request;
298 if (which > ggc->line_max)
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);
306 ggc->lines[which].debounce_usec = debounce_usec;
310 static int gb_gpio_request(struct gpio_chip *chip, unsigned offset)
312 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
315 if (offset < 0 || offset >= chip->ngpio)
317 ret = gb_gpio_activate_operation(gb_gpio_controller, (u8)offset);
323 static void gb_gpio_free(struct gpio_chip *chip, unsigned offset)
325 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
328 if (offset < 0 || offset >= chip->ngpio) {
329 pr_err("bad offset %u supplied (must be 0..%u)\n",
330 offset, chip->ngpio - 1);
333 ret = gb_gpio_deactivate_operation(gb_gpio_controller, (u8)offset);
338 static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
340 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
344 if (offset < 0 || offset >= chip->ngpio)
347 ret = gb_gpio_get_direction_operation(gb_gpio_controller, which);
350 return gb_gpio_controller->lines[which].direction ? 1 : 0;
353 static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
355 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
358 if (offset < 0 || offset >= chip->ngpio)
360 ret = gb_gpio_direction_in_operation(gb_gpio_controller, (u8)offset);
366 static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
369 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
372 if (offset < 0 || offset >= chip->ngpio)
374 ret = gb_gpio_direction_out_operation(gb_gpio_controller, (u8)offset, !!value);
380 static int gb_gpio_get(struct gpio_chip *chip, unsigned offset)
382 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
386 if (offset < 0 || offset >= chip->ngpio)
389 ret = gb_gpio_get_value_operation(gb_gpio_controller, which);
392 return (int)gb_gpio_controller->lines[which].value;
395 static void gb_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
397 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
400 if (offset < 0 || offset >= chip->ngpio) {
401 pr_err("bad offset %u supplied (must be 0..%u)\n",
402 offset, chip->ngpio - 1);
405 ret = gb_gpio_set_value_operation(gb_gpio_controller, (u8)offset, !!value);
410 static int gb_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
413 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
417 if (offset < 0 || offset >= chip->ngpio)
419 if (debounce > (unsigned int)U16_MAX)
422 ret = gb_gpio_set_debounce_operation(gb_gpio_controller, (u8)offset, usec);
429 static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
431 if (offset < 0 || offset >= chip->ngpio)
437 static void gb_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
442 static int gb_gpio_controller_setup(struct gb_gpio_controller *gb_gpio_controller)
448 /* First thing we need to do is check the version */
449 ret = gb_gpio_proto_version_operation(gb_gpio_controller);
453 /* Now find out how many lines there are */
454 ret = gb_gpio_line_count_operation(gb_gpio_controller);
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)
466 static int gb_gpio_connection_init(struct gb_connection *connection)
468 struct gb_gpio_controller *gb_gpio_controller;
469 struct gpio_chip *gpio;
472 gb_gpio_controller = kzalloc(sizeof(*gb_gpio_controller), GFP_KERNEL);
473 if (!gb_gpio_controller)
475 gb_gpio_controller->connection = connection;
476 connection->private = gb_gpio_controller;
478 ret = gb_gpio_controller_setup(gb_gpio_controller);
482 gpio = &gb_gpio_controller->chip;
484 gpio->label = "greybus_gpio";
485 gpio->owner = THIS_MODULE; /* XXX Module get? */
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;
498 gpio->base = -1; /* Allocate base dynamically */
499 gpio->ngpio = gb_gpio_controller->line_max + 1;
500 gpio->can_sleep = true; /* XXX */
502 ret = gpiochip_add(gpio);
504 pr_err("Failed to register GPIO\n");
510 kfree(gb_gpio_controller);
514 static void gb_gpio_connection_exit(struct gb_connection *connection)
516 struct gb_gpio_controller *gb_gpio_controller = connection->private;
518 if (!gb_gpio_controller)
521 gb_gpiochip_remove(&gb_gpio_controller->chip);
522 /* kref_put(gb_gpio_controller->connection) */
523 kfree(gb_gpio_controller);
526 static struct gb_protocol gpio_protocol = {
527 .id = GREYBUS_PROTOCOL_GPIO,
530 .connection_init = gb_gpio_connection_init,
531 .connection_exit = gb_gpio_connection_exit,
532 .request_recv = NULL, /* no incoming requests */
535 int gb_gpio_protocol_init(void)
537 return gb_protocol_register(&gpio_protocol);
540 void gb_gpio_protocol_exit(void)
542 gb_protocol_deregister(&gpio_protocol);