blob: 6a493d4758b4d817cb3f585fe110f85d13b9d5ee [file] [log] [blame]
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +08001/*
2 * GPIO Greybus driver.
3 *
4 * Copyright 2014 Google Inc.
Alex Eldera46e9672014-12-12 12:08:42 -06005 * Copyright 2014 Linaro Ltd.
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +08006 *
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>
Matt Porter036aad92015-02-17 10:48:23 -050014#include <linux/irq.h>
15#include <linux/irqdomain.h>
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +080016#include "greybus.h"
17
Alex Elderbb2e1c92014-10-16 06:35:39 -050018struct gb_gpio_line {
19 /* The following has to be an array of line_max entries */
20 /* --> make them just a flags field */
21 u8 active: 1,
22 direction: 1, /* 0 = output, 1 = input */
23 value: 1; /* 0 = low, 1 = high */
24 u16 debounce_usec;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +080025};
26
Alex Elderbb2e1c92014-10-16 06:35:39 -050027struct gb_gpio_controller {
28 struct gb_connection *connection;
29 u8 version_major;
30 u8 version_minor;
31 u8 line_max; /* max line number */
32 struct gb_gpio_line *lines;
33
34 struct gpio_chip chip;
Matt Porter036aad92015-02-17 10:48:23 -050035 struct irq_chip irqc;
36 struct irq_chip *irqchip;
37 struct irq_domain *irqdomain;
38 unsigned int irq_base;
39 irq_flow_handler_t irq_handler;
40 unsigned int irq_default_type;
Alex Elderbb2e1c92014-10-16 06:35:39 -050041};
42#define gpio_chip_to_gb_gpio_controller(chip) \
43 container_of(chip, struct gb_gpio_controller, chip)
Matt Porter036aad92015-02-17 10:48:23 -050044#define irq_data_to_gpio_chip(d) (d->domain->host_data)
Alex Elderbb2e1c92014-10-16 06:35:39 -050045
46/* Version of the Greybus GPIO protocol we support */
47#define GB_GPIO_VERSION_MAJOR 0x00
48#define GB_GPIO_VERSION_MINOR 0x01
49
50/* Greybus GPIO request types */
51#define GB_GPIO_TYPE_INVALID 0x00
52#define GB_GPIO_TYPE_PROTOCOL_VERSION 0x01
53#define GB_GPIO_TYPE_LINE_COUNT 0x02
54#define GB_GPIO_TYPE_ACTIVATE 0x03
55#define GB_GPIO_TYPE_DEACTIVATE 0x04
56#define GB_GPIO_TYPE_GET_DIRECTION 0x05
57#define GB_GPIO_TYPE_DIRECTION_IN 0x06
58#define GB_GPIO_TYPE_DIRECTION_OUT 0x07
59#define GB_GPIO_TYPE_GET_VALUE 0x08
60#define GB_GPIO_TYPE_SET_VALUE 0x09
61#define GB_GPIO_TYPE_SET_DEBOUNCE 0x0a
Matt Porter036aad92015-02-17 10:48:23 -050062#define GB_GPIO_TYPE_IRQ_TYPE 0x0b
63#define GB_GPIO_TYPE_IRQ_ACK 0x0c
64#define GB_GPIO_TYPE_IRQ_MASK 0x0d
65#define GB_GPIO_TYPE_IRQ_UNMASK 0x0e
66#define GB_GPIO_TYPE_IRQ_EVENT 0x0f
Alex Elderbb2e1c92014-10-16 06:35:39 -050067#define GB_GPIO_TYPE_RESPONSE 0x80 /* OR'd with rest */
68
69#define GB_GPIO_DEBOUNCE_USEC_DEFAULT 0 /* microseconds */
70
Alex Elderbb2e1c92014-10-16 06:35:39 -050071/* line count request has no payload */
72struct gb_gpio_line_count_response {
Alex Elderbb2e1c92014-10-16 06:35:39 -050073 __u8 count;
74};
75
76struct gb_gpio_activate_request {
77 __u8 which;
78};
Alex Elder5e689952014-11-20 15:37:05 -060079/* activate response has no payload */
Alex Elderbb2e1c92014-10-16 06:35:39 -050080
81struct gb_gpio_deactivate_request {
82 __u8 which;
83};
Alex Elder25d0f812014-11-19 17:55:05 -060084/* deactivate response has no payload */
Alex Elderbb2e1c92014-10-16 06:35:39 -050085
86struct gb_gpio_get_direction_request {
87 __u8 which;
88};
89struct gb_gpio_get_direction_response {
Alex Elderbb2e1c92014-10-16 06:35:39 -050090 __u8 direction;
91};
92
93struct gb_gpio_direction_in_request {
94 __u8 which;
95};
Alex Elder25d0f812014-11-19 17:55:05 -060096/* direction in response has no payload */
Alex Elderbb2e1c92014-10-16 06:35:39 -050097
98struct gb_gpio_direction_out_request {
99 __u8 which;
100 __u8 value;
101};
Alex Elder25d0f812014-11-19 17:55:05 -0600102/* direction out response has no payload */
Alex Elderbb2e1c92014-10-16 06:35:39 -0500103
104struct gb_gpio_get_value_request {
Alex Elderbb2e1c92014-10-16 06:35:39 -0500105 __u8 which;
106};
107struct gb_gpio_get_value_response {
Alex Elderbb2e1c92014-10-16 06:35:39 -0500108 __u8 value;
109};
110
111struct gb_gpio_set_value_request {
112 __u8 which;
113 __u8 value;
114};
Alex Elder25d0f812014-11-19 17:55:05 -0600115/* set value response has no payload */
Alex Elderbb2e1c92014-10-16 06:35:39 -0500116
117struct gb_gpio_set_debounce_request {
118 __u8 which;
Johan Hovoldfcc43562015-03-17 18:24:28 +0100119 __le16 usec __packed;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500120};
Alex Elder25d0f812014-11-19 17:55:05 -0600121/* debounce response has no payload */
Alex Elderbb2e1c92014-10-16 06:35:39 -0500122
Matt Porter036aad92015-02-17 10:48:23 -0500123struct gb_gpio_irq_type_request {
124 __u8 which;
125 __u8 type;
126};
127/* irq type response has no payload */
128
129struct gb_gpio_irq_mask_request {
130 __u8 which;
131};
132/* irq mask response has no payload */
133
134struct gb_gpio_irq_unmask_request {
135 __u8 which;
136};
137/* irq unmask response has no payload */
138
139struct gb_gpio_irq_ack_request {
140 __u8 which;
141};
142/* irq ack response has no payload */
143
144/* irq event requests originate on another module and are handled on the AP */
145struct gb_gpio_irq_event_request {
146 __u8 which;
147};
148/* irq event response has no payload */
Alex Elderbb2e1c92014-10-16 06:35:39 -0500149
Viresh Kumar36e79de2015-01-21 18:12:36 +0530150/* Define get_version() routine */
151define_get_version(gb_gpio_controller, GPIO);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500152
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800153static int gb_gpio_line_count_operation(struct gb_gpio_controller *ggc)
154{
155 struct gb_gpio_line_count_response response;
156 int ret;
157
158 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_LINE_COUNT,
159 NULL, 0, &response, sizeof(response));
160 if (!ret)
161 ggc->line_max = response.count;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500162 return ret;
163}
164
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800165static int gb_gpio_activate_operation(struct gb_gpio_controller *ggc, u8 which)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500166{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800167 struct gb_gpio_activate_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500168 int ret;
169
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800170 if (which > ggc->line_max)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500171 return -EINVAL;
172
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800173 request.which = which;
174 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE,
175 &request, sizeof(request), NULL, 0);
176 if (!ret)
177 ggc->lines[which].active = true;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500178 return ret;
179}
180
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800181static int gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500182 u8 which)
183{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800184 struct gb_gpio_deactivate_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500185 int ret;
186
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800187 if (which > ggc->line_max)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500188 return -EINVAL;
189
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800190 request.which = which;
191 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE,
192 &request, sizeof(request), NULL, 0);
193 if (!ret)
194 ggc->lines[which].active = false;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500195 return ret;
196}
197
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800198static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500199 u8 which)
200{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800201 struct gb_gpio_get_direction_request request;
202 struct gb_gpio_get_direction_response response;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500203 int ret;
Alex Elder23383de2014-11-21 19:29:12 -0600204 u8 direction;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500205
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800206 if (which > ggc->line_max)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500207 return -EINVAL;
208
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800209 request.which = which;
210 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION,
211 &request, sizeof(request),
212 &response, sizeof(response));
213 if (ret)
214 return ret;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500215
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800216 direction = response.direction;
Alex Elder23383de2014-11-21 19:29:12 -0600217 if (direction && direction != 1)
218 pr_warn("gpio %u direction was %u (should be 0 or 1)\n",
219 which, direction);
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800220 ggc->lines[which].direction = direction ? 1 : 0;
221 return 0;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500222}
223
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800224static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500225 u8 which)
226{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800227 struct gb_gpio_direction_in_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500228 int ret;
229
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800230 if (which > ggc->line_max)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500231 return -EINVAL;
232
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800233 request.which = which;
234 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN,
235 &request, sizeof(request), NULL, 0);
236 if (!ret)
237 ggc->lines[which].direction = 1;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500238 return ret;
239}
240
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800241static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500242 u8 which, bool value_high)
243{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800244 struct gb_gpio_direction_out_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500245 int ret;
246
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800247 if (which > ggc->line_max)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500248 return -EINVAL;
249
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800250 request.which = which;
251 request.value = value_high ? 1 : 0;
252 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT,
253 &request, sizeof(request), NULL, 0);
254 if (!ret)
255 ggc->lines[which].direction = 0;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500256 return ret;
257}
258
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800259static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500260 u8 which)
261{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800262 struct gb_gpio_get_value_request request;
263 struct gb_gpio_get_value_response response;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500264 int ret;
Alex Elder23383de2014-11-21 19:29:12 -0600265 u8 value;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500266
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800267 if (which > ggc->line_max)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500268 return -EINVAL;
269
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800270 request.which = which;
271 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE,
272 &request, sizeof(request),
273 &response, sizeof(response));
274 if (ret)
275 return ret;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500276
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800277 value = response.value;
Alex Elder23383de2014-11-21 19:29:12 -0600278 if (value && value != 1)
279 pr_warn("gpio %u value was %u (should be 0 or 1)\n",
280 which, value);
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800281 ggc->lines[which].value = value ? 1 : 0;
282 return 0;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500283}
284
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800285static int gb_gpio_set_value_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500286 u8 which, bool value_high)
287{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800288 struct gb_gpio_set_value_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500289 int ret;
290
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800291 if (which > ggc->line_max)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500292 return -EINVAL;
293
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800294 request.which = which;
295 request.value = value_high ? 1 : 0;
296 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE,
297 &request, sizeof(request), NULL, 0);
298 if (!ret) {
299 /* XXX should this set direction to out? */
300 ggc->lines[which].value = request.value;
301 }
Alex Elderbb2e1c92014-10-16 06:35:39 -0500302 return ret;
303}
304
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800305static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500306 u8 which, u16 debounce_usec)
307{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800308 struct gb_gpio_set_debounce_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500309 int ret;
310
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800311 if (which > ggc->line_max)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500312 return -EINVAL;
313
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800314 request.which = which;
315 request.usec = cpu_to_le16(debounce_usec);
316 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE,
317 &request, sizeof(request), NULL, 0);
318 if (!ret)
319 ggc->lines[which].debounce_usec = debounce_usec;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500320 return ret;
321}
322
Matt Porter036aad92015-02-17 10:48:23 -0500323static void gb_gpio_ack_irq(struct irq_data *d)
324{
325 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
326 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
327 struct gb_gpio_irq_ack_request request;
328 int ret;
329
330 request.which = d->hwirq;
331 ret = gb_operation_sync(ggc->connection,
332 GB_GPIO_TYPE_IRQ_ACK,
333 &request, sizeof(request), NULL, 0);
334 if (ret)
335 pr_err("irq ack operation failed (%d)\n", ret);
336}
337
338static void gb_gpio_mask_irq(struct irq_data *d)
339{
340 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
341 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
342 struct gb_gpio_irq_mask_request request;
343 int ret;
344
345 request.which = d->hwirq;
346 ret = gb_operation_sync(ggc->connection,
347 GB_GPIO_TYPE_IRQ_MASK,
348 &request, sizeof(request), NULL, 0);
349 if (ret)
350 pr_err("irq mask operation failed (%d)\n", ret);
351}
352
353static void gb_gpio_unmask_irq(struct irq_data *d)
354{
355 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
356 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
357 struct gb_gpio_irq_unmask_request request;
358 int ret;
359
360 request.which = d->hwirq;
361 ret = gb_operation_sync(ggc->connection,
362 GB_GPIO_TYPE_IRQ_UNMASK,
363 &request, sizeof(request), NULL, 0);
364 if (ret)
365 pr_err("irq unmask operation failed (%d)\n", ret);
366}
367
368static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
369{
370 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
371 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
372 struct gb_gpio_irq_type_request request;
373 int ret = 0;
374
375 request.which = d->hwirq;
376 request.type = type;
377
378 switch (type) {
379 case IRQ_TYPE_NONE:
380 break;
381 case IRQ_TYPE_EDGE_RISING:
382 case IRQ_TYPE_EDGE_FALLING:
383 case IRQ_TYPE_EDGE_BOTH:
384 case IRQ_TYPE_LEVEL_LOW:
385 case IRQ_TYPE_LEVEL_HIGH:
386 ret = gb_operation_sync(ggc->connection,
387 GB_GPIO_TYPE_IRQ_TYPE,
388 &request, sizeof(request), NULL, 0);
389 if (ret)
390 pr_err("irq type operation failed (%d)\n", ret);
391 break;
392 default:
393 pr_err("No such irq type %d", type);
394 ret = -EINVAL;
395 }
396
397 return ret;
398}
399
400static void gb_gpio_request_recv(u8 type, struct gb_operation *op)
401{
402 struct gb_gpio_controller *ggc;
403 struct gb_connection *connection;
404 struct gb_message *request;
405 struct gb_gpio_irq_event_request *event;
406 int irq;
407 struct irq_desc *desc;
408 int ret;
409
410 if (type != GB_GPIO_TYPE_IRQ_EVENT) {
411 pr_err("unsupported unsolicited request\n");
412 return;
413 }
414
415 connection = op->connection;
416 ggc = connection->private;
417
418 request = op->request;
419 event = request->payload;
420 if (event->which > ggc->line_max) {
421 pr_err("Unsupported hw irq %d\n", event->which);
422 return;
423 }
Alexandre Bailond1b20d72015-03-02 17:32:43 +0100424 irq = gpio_to_irq(ggc->chip.base + event->which);
Matt Porter036aad92015-02-17 10:48:23 -0500425 desc = irq_to_desc(irq);
426
427 /* Dispatch interrupt */
428 local_irq_disable();
429 handle_simple_irq(irq, desc);
430 local_irq_enable();
431
432 ret = gb_operation_response_send(op, 0);
433 if (ret)
434 pr_err("error %d sending response status %d\n", ret, 0);
435}
436
Alex Elderbb2e1c92014-10-16 06:35:39 -0500437static int gb_gpio_request(struct gpio_chip *chip, unsigned offset)
438{
439 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500440 int ret;
441
Bill Pemberton76590b12015-01-16 13:57:32 -0500442 if (offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500443 return -EINVAL;
Matt Porterff6e0b92014-10-20 06:39:45 -0400444 ret = gb_gpio_activate_operation(gb_gpio_controller, (u8)offset);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500445 if (ret)
446 ; /* return ret; */
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800447 return 0;
448}
449
Alex Elderbb2e1c92014-10-16 06:35:39 -0500450static void gb_gpio_free(struct gpio_chip *chip, unsigned offset)
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800451{
Alex Elderbb2e1c92014-10-16 06:35:39 -0500452 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500453 int ret;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800454
Bill Pemberton76590b12015-01-16 13:57:32 -0500455 if (offset >= chip->ngpio) {
Matt Porterff6e0b92014-10-20 06:39:45 -0400456 pr_err("bad offset %u supplied (must be 0..%u)\n",
457 offset, chip->ngpio - 1);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500458 return;
459 }
Matt Porterff6e0b92014-10-20 06:39:45 -0400460 ret = gb_gpio_deactivate_operation(gb_gpio_controller, (u8)offset);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500461 if (ret)
462 ; /* return ret; */
463}
464
465static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
466{
467 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
468 u8 which;
469 int ret;
470
Bill Pemberton76590b12015-01-16 13:57:32 -0500471 if (offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500472 return -EINVAL;
Matt Porterff6e0b92014-10-20 06:39:45 -0400473 which = (u8)offset;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500474 ret = gb_gpio_get_direction_operation(gb_gpio_controller, which);
475 if (ret)
476 ; /* return ret; */
477 return gb_gpio_controller->lines[which].direction ? 1 : 0;
478}
479
480static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
481{
482 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500483 int ret;
484
Bill Pemberton76590b12015-01-16 13:57:32 -0500485 if (offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500486 return -EINVAL;
Matt Porterff6e0b92014-10-20 06:39:45 -0400487 ret = gb_gpio_direction_in_operation(gb_gpio_controller, (u8)offset);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500488 if (ret)
489 ; /* return ret; */
490 return 0;
491}
492
493static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
494 int value)
495{
496 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500497 int ret;
498
Bill Pemberton76590b12015-01-16 13:57:32 -0500499 if (offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500500 return -EINVAL;
Matt Porterff6e0b92014-10-20 06:39:45 -0400501 ret = gb_gpio_direction_out_operation(gb_gpio_controller, (u8)offset, !!value);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500502 if (ret)
503 ; /* return ret; */
504 return 0;
505}
506
507static int gb_gpio_get(struct gpio_chip *chip, unsigned offset)
508{
509 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
510 u8 which;
511 int ret;
512
Bill Pemberton76590b12015-01-16 13:57:32 -0500513 if (offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500514 return -EINVAL;
Matt Porterff6e0b92014-10-20 06:39:45 -0400515 which = (u8)offset;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500516 ret = gb_gpio_get_value_operation(gb_gpio_controller, which);
517 if (ret)
518 return ret;
519 return (int)gb_gpio_controller->lines[which].value;
520}
521
522static void gb_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
523{
524 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500525 int ret;
526
Matt Porterff6e0b92014-10-20 06:39:45 -0400527 if (offset < 0 || offset >= chip->ngpio) {
528 pr_err("bad offset %u supplied (must be 0..%u)\n",
529 offset, chip->ngpio - 1);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500530 return;
531 }
Matt Porterff6e0b92014-10-20 06:39:45 -0400532 ret = gb_gpio_set_value_operation(gb_gpio_controller, (u8)offset, !!value);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500533 if (ret)
534 ; /* return ret; */
535}
536
537static int gb_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
538 unsigned debounce)
539{
540 struct gb_gpio_controller *gb_gpio_controller = gpio_chip_to_gb_gpio_controller(chip);
541 u16 usec;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500542 int ret;
543
Bill Pemberton76590b12015-01-16 13:57:32 -0500544 if (offset >= chip->ngpio)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500545 return -EINVAL;
Johan Hovoldc2a66102015-03-19 16:51:09 +0100546 if (debounce > U16_MAX)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500547 return -EINVAL;
Johan Hovoldc2a66102015-03-19 16:51:09 +0100548 usec = (u16)debounce;
Matt Porterff6e0b92014-10-20 06:39:45 -0400549 ret = gb_gpio_set_debounce_operation(gb_gpio_controller, (u8)offset, usec);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500550 if (ret)
551 ; /* return ret; */
552
553 return 0; /* XXX */
554}
555
Alex Elderbb2e1c92014-10-16 06:35:39 -0500556static void gb_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
557{
558 return; /* XXX */
559}
560
Greg Kroah-Hartmand4c82472014-10-27 12:33:47 +0800561static int gb_gpio_controller_setup(struct gb_gpio_controller *gb_gpio_controller)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500562{
563 u32 line_count;
564 size_t size;
565 int ret;
566
567 /* First thing we need to do is check the version */
Viresh Kumar36e79de2015-01-21 18:12:36 +0530568 ret = get_version(gb_gpio_controller);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500569 if (ret)
570 ; /* return ret; */
571
572 /* Now find out how many lines there are */
573 ret = gb_gpio_line_count_operation(gb_gpio_controller);
574 if (ret)
575 ; /* return ret; */
576 line_count = (u32)gb_gpio_controller->line_max + 1;
577 size = line_count * sizeof(*gb_gpio_controller->lines);
578 gb_gpio_controller->lines = kzalloc(size, GFP_KERNEL);
579 if (!gb_gpio_controller->lines)
580 return -ENOMEM;
581
582 return ret;
583}
584
Matt Porter036aad92015-02-17 10:48:23 -0500585/**
586 * gb_gpio_irq_map() - maps an IRQ into a GB gpio irqchip
587 * @d: the irqdomain used by this irqchip
588 * @irq: the global irq number used by this GB gpio irqchip irq
589 * @hwirq: the local IRQ/GPIO line offset on this GB gpio
590 *
591 * This function will set up the mapping for a certain IRQ line on a
592 * GB gpio by assigning the GB gpio as chip data, and using the irqchip
593 * stored inside the GB gpio.
594 */
595static int gb_gpio_irq_map(struct irq_domain *domain, unsigned int irq,
596 irq_hw_number_t hwirq)
597{
598 struct gpio_chip *chip = domain->host_data;
599 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
600
601 irq_set_chip_data(irq, ggc);
602 irq_set_chip_and_handler(irq, ggc->irqchip, ggc->irq_handler);
603#ifdef CONFIG_ARM
604 set_irq_flags(irq, IRQF_VALID);
605#else
606 irq_set_noprobe(irq);
607#endif
608 /*
609 * No set-up of the hardware will happen if IRQ_TYPE_NONE
610 * is passed as default type.
611 */
612 if (ggc->irq_default_type != IRQ_TYPE_NONE)
613 irq_set_irq_type(irq, ggc->irq_default_type);
614
615 return 0;
616}
617
618static void gb_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
619{
620#ifdef CONFIG_ARM
621 set_irq_flags(irq, 0);
622#endif
623 irq_set_chip_and_handler(irq, NULL, NULL);
624 irq_set_chip_data(irq, NULL);
625}
626
627static const struct irq_domain_ops gb_gpio_domain_ops = {
628 .map = gb_gpio_irq_map,
629 .unmap = gb_gpio_irq_unmap,
630};
631
632/**
633 * gb_gpio_irqchip_remove() - removes an irqchip added to a gb_gpio_controller
634 * @ggc: the gb_gpio_controller to remove the irqchip from
635 *
636 * This is called only from gb_gpio_remove()
637 */
638static void gb_gpio_irqchip_remove(struct gb_gpio_controller *ggc)
639{
640 unsigned int offset;
641
642 /* Remove all IRQ mappings and delete the domain */
643 if (ggc->irqdomain) {
644 for (offset = 0; offset < (ggc->line_max + 1); offset++)
645 irq_dispose_mapping(irq_find_mapping(ggc->irqdomain, offset));
646 irq_domain_remove(ggc->irqdomain);
647 }
648
649 if (ggc->irqchip) {
650 ggc->irqchip = NULL;
651 }
652}
653
654
655/**
656 * gb_gpio_irqchip_add() - adds an irqchip to a gpio chip
657 * @chip: the gpio chip to add the irqchip to
658 * @irqchip: the irqchip to add to the adapter
659 * @first_irq: if not dynamically assigned, the base (first) IRQ to
660 * allocate gpio irqs from
661 * @handler: the irq handler to use (often a predefined irq core function)
662 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
663 * to have the core avoid setting up any default type in the hardware.
664 *
665 * This function closely associates a certain irqchip with a certain
666 * gpio chip, providing an irq domain to translate the local IRQs to
667 * global irqs, and making sure that the gpio chip
668 * is passed as chip data to all related functions. Driver callbacks
669 * need to use container_of() to get their local state containers back
670 * from the gpio chip passed as chip data. An irqdomain will be stored
671 * in the gpio chip that shall be used by the driver to handle IRQ number
672 * translation. The gpio chip will need to be initialized and registered
673 * before calling this function.
674 */
675static int gb_gpio_irqchip_add(struct gpio_chip *chip,
676 struct irq_chip *irqchip,
677 unsigned int first_irq,
678 irq_flow_handler_t handler,
679 unsigned int type)
680{
681 struct gb_gpio_controller *ggc;
682 unsigned int offset;
683 unsigned irq_base;
684
685 if (!chip || !irqchip)
686 return -EINVAL;
687
688 ggc = gpio_chip_to_gb_gpio_controller(chip);
689
690 ggc->irqchip = irqchip;
691 ggc->irq_handler = handler;
692 ggc->irq_default_type = type;
693 ggc->irqdomain = irq_domain_add_simple(NULL,
694 ggc->line_max + 1, first_irq,
695 &gb_gpio_domain_ops, chip);
696 if (!ggc->irqdomain) {
697 ggc->irqchip = NULL;
698 return -EINVAL;
699 }
700
701 /*
702 * Prepare the mapping since the irqchip shall be orthogonal to
703 * any gpio calls. If the first_irq was zero, this is
704 * necessary to allocate descriptors for all IRQs.
705 */
706 for (offset = 0; offset < (ggc->line_max + 1); offset++) {
707 irq_base = irq_create_mapping(ggc->irqdomain, offset);
708 if (offset == 0)
709 ggc->irq_base = irq_base;
710 }
711
712 return 0;
713}
714
715static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
716{
717 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
718
719 return irq_find_mapping(ggc->irqdomain, offset);
720}
721
Alex Elder3689f972014-10-27 06:04:30 -0500722static int gb_gpio_connection_init(struct gb_connection *connection)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500723{
724 struct gb_gpio_controller *gb_gpio_controller;
725 struct gpio_chip *gpio;
Matt Porter036aad92015-02-17 10:48:23 -0500726 struct irq_chip *irqc;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500727 int ret;
728
729 gb_gpio_controller = kzalloc(sizeof(*gb_gpio_controller), GFP_KERNEL);
730 if (!gb_gpio_controller)
731 return -ENOMEM;
732 gb_gpio_controller->connection = connection;
Alex Elder93bbe852014-12-03 12:27:42 -0600733 connection->private = gb_gpio_controller;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500734
735 ret = gb_gpio_controller_setup(gb_gpio_controller);
736 if (ret)
Johan Hovold35a64f22015-02-13 14:58:04 +0800737 goto err_free_controller;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500738
Matt Porter036aad92015-02-17 10:48:23 -0500739 irqc = &gb_gpio_controller->irqc;
740 irqc->irq_ack = gb_gpio_ack_irq;
741 irqc->irq_mask = gb_gpio_mask_irq;
742 irqc->irq_unmask = gb_gpio_unmask_irq;
743 irqc->irq_set_type = gb_gpio_irq_set_type;
744 irqc->name = "greybus_gpio";
745
Alex Elderbb2e1c92014-10-16 06:35:39 -0500746 gpio = &gb_gpio_controller->chip;
747
748 gpio->label = "greybus_gpio";
Matt Porter036aad92015-02-17 10:48:23 -0500749 gpio->dev = &connection->dev;
Johan Hovold56c2da12015-03-19 16:51:10 +0100750 gpio->owner = THIS_MODULE;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500751
752 gpio->request = gb_gpio_request;
753 gpio->free = gb_gpio_free;
754 gpio->get_direction = gb_gpio_get_direction;
755 gpio->direction_input = gb_gpio_direction_input;
756 gpio->direction_output = gb_gpio_direction_output;
757 gpio->get = gb_gpio_get;
758 gpio->set = gb_gpio_set;
759 gpio->set_debounce = gb_gpio_set_debounce;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500760 gpio->dbg_show = gb_gpio_dbg_show;
Matt Porter036aad92015-02-17 10:48:23 -0500761 gpio->to_irq = gb_gpio_to_irq;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500762 gpio->base = -1; /* Allocate base dynamically */
763 gpio->ngpio = gb_gpio_controller->line_max + 1;
Johan Hovold56c2da12015-03-19 16:51:10 +0100764 gpio->can_sleep = true;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500765
766 ret = gpiochip_add(gpio);
767 if (ret) {
768 pr_err("Failed to register GPIO\n");
Johan Hovold35a64f22015-02-13 14:58:04 +0800769 goto err_free_lines;
Matt Porter036aad92015-02-17 10:48:23 -0500770 }
771
772 ret = gb_gpio_irqchip_add(gpio, irqc, 0,
773 handle_simple_irq, IRQ_TYPE_NONE);
774 if (ret) {
775 pr_err("Couldn't add irqchip to Greybus GPIO controller %d\n", ret);
776 goto irqchip_err;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500777 }
Alex Elderbb2e1c92014-10-16 06:35:39 -0500778
779 return 0;
Matt Porter036aad92015-02-17 10:48:23 -0500780
781irqchip_err:
782 gb_gpiochip_remove(gpio);
Johan Hovold35a64f22015-02-13 14:58:04 +0800783err_free_lines:
784 kfree(gb_gpio_controller->lines);
785err_free_controller:
Alex Elderbb2e1c92014-10-16 06:35:39 -0500786 kfree(gb_gpio_controller);
787 return ret;
788}
789
Alex Elder3689f972014-10-27 06:04:30 -0500790static void gb_gpio_connection_exit(struct gb_connection *connection)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500791{
792 struct gb_gpio_controller *gb_gpio_controller = connection->private;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500793
794 if (!gb_gpio_controller)
Alex Elder051fb042014-10-16 06:35:24 -0500795 return;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800796
Matt Porter036aad92015-02-17 10:48:23 -0500797 gb_gpio_irqchip_remove(gb_gpio_controller);
Greg Kroah-Hartman213aefe2014-10-20 13:40:02 +0800798 gb_gpiochip_remove(&gb_gpio_controller->chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500799 /* kref_put(gb_gpio_controller->connection) */
Johan Hovold35a64f22015-02-13 14:58:04 +0800800 kfree(gb_gpio_controller->lines);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500801 kfree(gb_gpio_controller);
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800802}
803
Alex Elder19d03de2014-11-05 16:12:53 -0600804static struct gb_protocol gpio_protocol = {
Greg Kroah-Hartman7422a1e2014-12-24 13:01:45 -0800805 .name = "gpio",
Alex Elder19d03de2014-11-05 16:12:53 -0600806 .id = GREYBUS_PROTOCOL_GPIO,
807 .major = 0,
808 .minor = 1,
Alex Elder5d9fd7e2014-11-05 16:12:54 -0600809 .connection_init = gb_gpio_connection_init,
810 .connection_exit = gb_gpio_connection_exit,
Matt Porter036aad92015-02-17 10:48:23 -0500811 .request_recv = gb_gpio_request_recv,
Alex Elder19d03de2014-11-05 16:12:53 -0600812};
813
Greg Kroah-Hartman7c7d5b92014-12-23 15:16:51 -0800814int gb_gpio_protocol_init(void)
Alex Elder19d03de2014-11-05 16:12:53 -0600815{
816 return gb_protocol_register(&gpio_protocol);
817}
818
819void gb_gpio_protocol_exit(void)
820{
821 gb_protocol_deregister(&gpio_protocol);
822}