blob: b18fb7e12a5b5a82649afb5f442a5aed6239d187 [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 request.which = which;
171 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_ACTIVATE,
172 &request, sizeof(request), NULL, 0);
173 if (!ret)
174 ggc->lines[which].active = true;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500175 return ret;
176}
177
Johan Hovold86c68162015-03-19 16:51:15 +0100178static void gb_gpio_deactivate_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500179 u8 which)
180{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800181 struct gb_gpio_deactivate_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500182 int ret;
183
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800184 request.which = which;
185 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DEACTIVATE,
186 &request, sizeof(request), NULL, 0);
187 if (!ret)
188 ggc->lines[which].active = false;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500189}
190
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800191static int gb_gpio_get_direction_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500192 u8 which)
193{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800194 struct gb_gpio_get_direction_request request;
195 struct gb_gpio_get_direction_response response;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500196 int ret;
Alex Elder23383de2014-11-21 19:29:12 -0600197 u8 direction;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500198
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800199 request.which = which;
200 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_DIRECTION,
201 &request, sizeof(request),
202 &response, sizeof(response));
203 if (ret)
204 return ret;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500205
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800206 direction = response.direction;
Johan Hovolde5032d82015-03-19 16:51:16 +0100207 if (direction && direction != 1) {
208 dev_warn(ggc->chip.dev,
209 "gpio %u direction was %u (should be 0 or 1)\n",
210 which, direction);
211 }
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800212 ggc->lines[which].direction = direction ? 1 : 0;
213 return 0;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500214}
215
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800216static int gb_gpio_direction_in_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500217 u8 which)
218{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800219 struct gb_gpio_direction_in_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500220 int ret;
221
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800222 request.which = which;
223 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_IN,
224 &request, sizeof(request), NULL, 0);
225 if (!ret)
226 ggc->lines[which].direction = 1;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500227 return ret;
228}
229
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800230static int gb_gpio_direction_out_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500231 u8 which, bool value_high)
232{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800233 struct gb_gpio_direction_out_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500234 int ret;
235
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800236 request.which = which;
237 request.value = value_high ? 1 : 0;
238 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_DIRECTION_OUT,
239 &request, sizeof(request), NULL, 0);
240 if (!ret)
241 ggc->lines[which].direction = 0;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500242 return ret;
243}
244
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800245static int gb_gpio_get_value_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500246 u8 which)
247{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800248 struct gb_gpio_get_value_request request;
249 struct gb_gpio_get_value_response response;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500250 int ret;
Alex Elder23383de2014-11-21 19:29:12 -0600251 u8 value;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500252
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800253 request.which = which;
254 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_GET_VALUE,
255 &request, sizeof(request),
256 &response, sizeof(response));
257 if (ret)
258 return ret;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500259
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800260 value = response.value;
Johan Hovolde5032d82015-03-19 16:51:16 +0100261 if (value && value != 1) {
262 dev_warn(ggc->chip.dev,
263 "gpio %u value was %u (should be 0 or 1)\n",
264 which, value);
265 }
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800266 ggc->lines[which].value = value ? 1 : 0;
267 return 0;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500268}
269
Johan Hovold86c68162015-03-19 16:51:15 +0100270static void gb_gpio_set_value_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500271 u8 which, bool value_high)
272{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800273 struct gb_gpio_set_value_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500274 int ret;
275
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800276 request.which = which;
277 request.value = value_high ? 1 : 0;
278 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_VALUE,
279 &request, sizeof(request), NULL, 0);
280 if (!ret) {
281 /* XXX should this set direction to out? */
282 ggc->lines[which].value = request.value;
283 }
Alex Elderbb2e1c92014-10-16 06:35:39 -0500284}
285
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800286static int gb_gpio_set_debounce_operation(struct gb_gpio_controller *ggc,
Alex Elderbb2e1c92014-10-16 06:35:39 -0500287 u8 which, u16 debounce_usec)
288{
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800289 struct gb_gpio_set_debounce_request request;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500290 int ret;
291
Greg Kroah-Hartman7d5bbb12014-11-23 17:45:22 -0800292 request.which = which;
293 request.usec = cpu_to_le16(debounce_usec);
294 ret = gb_operation_sync(ggc->connection, GB_GPIO_TYPE_SET_DEBOUNCE,
295 &request, sizeof(request), NULL, 0);
296 if (!ret)
297 ggc->lines[which].debounce_usec = debounce_usec;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500298 return ret;
299}
300
Matt Porter036aad92015-02-17 10:48:23 -0500301static void gb_gpio_ack_irq(struct irq_data *d)
302{
303 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
304 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
305 struct gb_gpio_irq_ack_request request;
306 int ret;
307
308 request.which = d->hwirq;
309 ret = gb_operation_sync(ggc->connection,
310 GB_GPIO_TYPE_IRQ_ACK,
311 &request, sizeof(request), NULL, 0);
312 if (ret)
Johan Hovolde5032d82015-03-19 16:51:16 +0100313 dev_err(chip->dev, "failed to ack irq: %d\n", ret);
Matt Porter036aad92015-02-17 10:48:23 -0500314}
315
316static void gb_gpio_mask_irq(struct irq_data *d)
317{
318 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
319 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
320 struct gb_gpio_irq_mask_request request;
321 int ret;
322
323 request.which = d->hwirq;
324 ret = gb_operation_sync(ggc->connection,
325 GB_GPIO_TYPE_IRQ_MASK,
326 &request, sizeof(request), NULL, 0);
327 if (ret)
Johan Hovolde5032d82015-03-19 16:51:16 +0100328 dev_err(chip->dev, "failed to mask irq: %d\n", ret);
Matt Porter036aad92015-02-17 10:48:23 -0500329}
330
331static void gb_gpio_unmask_irq(struct irq_data *d)
332{
333 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
334 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
335 struct gb_gpio_irq_unmask_request request;
336 int ret;
337
338 request.which = d->hwirq;
339 ret = gb_operation_sync(ggc->connection,
340 GB_GPIO_TYPE_IRQ_UNMASK,
341 &request, sizeof(request), NULL, 0);
342 if (ret)
Johan Hovolde5032d82015-03-19 16:51:16 +0100343 dev_err(chip->dev, "failed to unmask irq: %d\n", ret);
Matt Porter036aad92015-02-17 10:48:23 -0500344}
345
346static int gb_gpio_irq_set_type(struct irq_data *d, unsigned int type)
347{
348 struct gpio_chip *chip = irq_data_to_gpio_chip(d);
349 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
350 struct gb_gpio_irq_type_request request;
351 int ret = 0;
352
353 request.which = d->hwirq;
354 request.type = type;
355
356 switch (type) {
357 case IRQ_TYPE_NONE:
358 break;
359 case IRQ_TYPE_EDGE_RISING:
360 case IRQ_TYPE_EDGE_FALLING:
361 case IRQ_TYPE_EDGE_BOTH:
362 case IRQ_TYPE_LEVEL_LOW:
363 case IRQ_TYPE_LEVEL_HIGH:
364 ret = gb_operation_sync(ggc->connection,
365 GB_GPIO_TYPE_IRQ_TYPE,
366 &request, sizeof(request), NULL, 0);
Johan Hovolde5032d82015-03-19 16:51:16 +0100367 if (ret) {
368 dev_err(chip->dev, "failed to set irq type: %d\n",
369 ret);
370 }
Matt Porter036aad92015-02-17 10:48:23 -0500371 break;
372 default:
Johan Hovolde5032d82015-03-19 16:51:16 +0100373 dev_err(chip->dev, "unsupported irq type: %u\n", type);
Matt Porter036aad92015-02-17 10:48:23 -0500374 ret = -EINVAL;
375 }
376
377 return ret;
378}
379
380static void gb_gpio_request_recv(u8 type, struct gb_operation *op)
381{
382 struct gb_gpio_controller *ggc;
Johan Hovolde5032d82015-03-19 16:51:16 +0100383 struct gb_connection *connection = op->connection;
Matt Porter036aad92015-02-17 10:48:23 -0500384 struct gb_message *request;
385 struct gb_gpio_irq_event_request *event;
386 int irq;
387 struct irq_desc *desc;
388 int ret;
389
390 if (type != GB_GPIO_TYPE_IRQ_EVENT) {
Johan Hovolde5032d82015-03-19 16:51:16 +0100391 dev_err(&connection->dev,
392 "unsupported unsolicited request: %u\n", type);
Matt Porter036aad92015-02-17 10:48:23 -0500393 return;
394 }
395
Matt Porter036aad92015-02-17 10:48:23 -0500396 ggc = connection->private;
397
398 request = op->request;
399 event = request->payload;
400 if (event->which > ggc->line_max) {
Johan Hovolde5032d82015-03-19 16:51:16 +0100401 dev_err(ggc->chip.dev, "invalid hw irq: %d\n", event->which);
Matt Porter036aad92015-02-17 10:48:23 -0500402 return;
403 }
Alexandre Bailond1b20d72015-03-02 17:32:43 +0100404 irq = gpio_to_irq(ggc->chip.base + event->which);
Matt Porter036aad92015-02-17 10:48:23 -0500405 desc = irq_to_desc(irq);
406
407 /* Dispatch interrupt */
408 local_irq_disable();
409 handle_simple_irq(irq, desc);
410 local_irq_enable();
411
412 ret = gb_operation_response_send(op, 0);
Johan Hovolde5032d82015-03-19 16:51:16 +0100413 if (ret) {
414 dev_err(ggc->chip.dev,
415 "failed to send response status %d: %d\n", 0, ret);
416 }
Matt Porter036aad92015-02-17 10:48:23 -0500417}
418
Alex Elderbb2e1c92014-10-16 06:35:39 -0500419static int gb_gpio_request(struct gpio_chip *chip, unsigned offset)
420{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100421 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500422
Johan Hovold86c68162015-03-19 16:51:15 +0100423 return gb_gpio_activate_operation(ggc, (u8)offset);
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800424}
425
Alex Elderbb2e1c92014-10-16 06:35:39 -0500426static void gb_gpio_free(struct gpio_chip *chip, unsigned offset)
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800427{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100428 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800429
Johan Hovold86c68162015-03-19 16:51:15 +0100430 gb_gpio_deactivate_operation(ggc, (u8)offset);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500431}
432
433static int gb_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
434{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100435 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500436 u8 which;
437 int ret;
438
Matt Porterff6e0b92014-10-20 06:39:45 -0400439 which = (u8)offset;
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100440 ret = gb_gpio_get_direction_operation(ggc, which);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500441 if (ret)
Johan Hovold86c68162015-03-19 16:51:15 +0100442 return ret;
443
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100444 return ggc->lines[which].direction ? 1 : 0;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500445}
446
447static int gb_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
448{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100449 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500450
Johan Hovold86c68162015-03-19 16:51:15 +0100451 return gb_gpio_direction_in_operation(ggc, (u8)offset);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500452}
453
454static int gb_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
455 int value)
456{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100457 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500458
Johan Hovold86c68162015-03-19 16:51:15 +0100459 return gb_gpio_direction_out_operation(ggc, (u8)offset, !!value);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500460}
461
462static int gb_gpio_get(struct gpio_chip *chip, unsigned offset)
463{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100464 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500465 u8 which;
466 int ret;
467
Matt Porterff6e0b92014-10-20 06:39:45 -0400468 which = (u8)offset;
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100469 ret = gb_gpio_get_value_operation(ggc, which);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500470 if (ret)
471 return ret;
Johan Hovold86c68162015-03-19 16:51:15 +0100472
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100473 return ggc->lines[which].value;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500474}
475
476static void gb_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
477{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100478 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500479
Johan Hovold86c68162015-03-19 16:51:15 +0100480 gb_gpio_set_value_operation(ggc, (u8)offset, !!value);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500481}
482
483static int gb_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
484 unsigned debounce)
485{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100486 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500487 u16 usec;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500488
Johan Hovoldc2a66102015-03-19 16:51:09 +0100489 if (debounce > U16_MAX)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500490 return -EINVAL;
Johan Hovoldc2a66102015-03-19 16:51:09 +0100491 usec = (u16)debounce;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500492
Johan Hovold86c68162015-03-19 16:51:15 +0100493 return gb_gpio_set_debounce_operation(ggc, (u8)offset, usec);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500494}
495
Alex Elderbb2e1c92014-10-16 06:35:39 -0500496static void gb_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
497{
498 return; /* XXX */
499}
500
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100501static int gb_gpio_controller_setup(struct gb_gpio_controller *ggc)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500502{
Alex Elderbb2e1c92014-10-16 06:35:39 -0500503 int ret;
504
505 /* First thing we need to do is check the version */
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100506 ret = get_version(ggc);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500507 if (ret)
Johan Hovold86c68162015-03-19 16:51:15 +0100508 return ret;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500509
510 /* Now find out how many lines there are */
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100511 ret = gb_gpio_line_count_operation(ggc);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500512 if (ret)
Johan Hovold86c68162015-03-19 16:51:15 +0100513 return ret;
514
Johan Hovold64d2f4e2015-03-19 16:51:17 +0100515 ggc->lines = kcalloc(ggc->line_max + 1, sizeof(*ggc->lines),
516 GFP_KERNEL);
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100517 if (!ggc->lines)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500518 return -ENOMEM;
519
520 return ret;
521}
522
Matt Porter036aad92015-02-17 10:48:23 -0500523/**
524 * gb_gpio_irq_map() - maps an IRQ into a GB gpio irqchip
525 * @d: the irqdomain used by this irqchip
526 * @irq: the global irq number used by this GB gpio irqchip irq
527 * @hwirq: the local IRQ/GPIO line offset on this GB gpio
528 *
529 * This function will set up the mapping for a certain IRQ line on a
530 * GB gpio by assigning the GB gpio as chip data, and using the irqchip
531 * stored inside the GB gpio.
532 */
533static int gb_gpio_irq_map(struct irq_domain *domain, unsigned int irq,
534 irq_hw_number_t hwirq)
535{
536 struct gpio_chip *chip = domain->host_data;
537 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
538
539 irq_set_chip_data(irq, ggc);
540 irq_set_chip_and_handler(irq, ggc->irqchip, ggc->irq_handler);
541#ifdef CONFIG_ARM
542 set_irq_flags(irq, IRQF_VALID);
543#else
544 irq_set_noprobe(irq);
545#endif
546 /*
547 * No set-up of the hardware will happen if IRQ_TYPE_NONE
548 * is passed as default type.
549 */
550 if (ggc->irq_default_type != IRQ_TYPE_NONE)
551 irq_set_irq_type(irq, ggc->irq_default_type);
552
553 return 0;
554}
555
556static void gb_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
557{
558#ifdef CONFIG_ARM
559 set_irq_flags(irq, 0);
560#endif
561 irq_set_chip_and_handler(irq, NULL, NULL);
562 irq_set_chip_data(irq, NULL);
563}
564
565static const struct irq_domain_ops gb_gpio_domain_ops = {
566 .map = gb_gpio_irq_map,
567 .unmap = gb_gpio_irq_unmap,
568};
569
570/**
571 * gb_gpio_irqchip_remove() - removes an irqchip added to a gb_gpio_controller
572 * @ggc: the gb_gpio_controller to remove the irqchip from
573 *
574 * This is called only from gb_gpio_remove()
575 */
576static void gb_gpio_irqchip_remove(struct gb_gpio_controller *ggc)
577{
578 unsigned int offset;
579
580 /* Remove all IRQ mappings and delete the domain */
581 if (ggc->irqdomain) {
582 for (offset = 0; offset < (ggc->line_max + 1); offset++)
583 irq_dispose_mapping(irq_find_mapping(ggc->irqdomain, offset));
584 irq_domain_remove(ggc->irqdomain);
585 }
586
587 if (ggc->irqchip) {
588 ggc->irqchip = NULL;
589 }
590}
591
592
593/**
594 * gb_gpio_irqchip_add() - adds an irqchip to a gpio chip
595 * @chip: the gpio chip to add the irqchip to
596 * @irqchip: the irqchip to add to the adapter
597 * @first_irq: if not dynamically assigned, the base (first) IRQ to
598 * allocate gpio irqs from
599 * @handler: the irq handler to use (often a predefined irq core function)
600 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
601 * to have the core avoid setting up any default type in the hardware.
602 *
603 * This function closely associates a certain irqchip with a certain
604 * gpio chip, providing an irq domain to translate the local IRQs to
605 * global irqs, and making sure that the gpio chip
606 * is passed as chip data to all related functions. Driver callbacks
607 * need to use container_of() to get their local state containers back
608 * from the gpio chip passed as chip data. An irqdomain will be stored
609 * in the gpio chip that shall be used by the driver to handle IRQ number
610 * translation. The gpio chip will need to be initialized and registered
611 * before calling this function.
612 */
613static int gb_gpio_irqchip_add(struct gpio_chip *chip,
614 struct irq_chip *irqchip,
615 unsigned int first_irq,
616 irq_flow_handler_t handler,
617 unsigned int type)
618{
619 struct gb_gpio_controller *ggc;
620 unsigned int offset;
621 unsigned irq_base;
622
623 if (!chip || !irqchip)
624 return -EINVAL;
625
626 ggc = gpio_chip_to_gb_gpio_controller(chip);
627
628 ggc->irqchip = irqchip;
629 ggc->irq_handler = handler;
630 ggc->irq_default_type = type;
631 ggc->irqdomain = irq_domain_add_simple(NULL,
632 ggc->line_max + 1, first_irq,
633 &gb_gpio_domain_ops, chip);
634 if (!ggc->irqdomain) {
635 ggc->irqchip = NULL;
636 return -EINVAL;
637 }
638
639 /*
640 * Prepare the mapping since the irqchip shall be orthogonal to
641 * any gpio calls. If the first_irq was zero, this is
642 * necessary to allocate descriptors for all IRQs.
643 */
644 for (offset = 0; offset < (ggc->line_max + 1); offset++) {
645 irq_base = irq_create_mapping(ggc->irqdomain, offset);
646 if (offset == 0)
647 ggc->irq_base = irq_base;
648 }
649
650 return 0;
651}
652
653static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
654{
655 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
656
657 return irq_find_mapping(ggc->irqdomain, offset);
658}
659
Alex Elder3689f972014-10-27 06:04:30 -0500660static int gb_gpio_connection_init(struct gb_connection *connection)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500661{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100662 struct gb_gpio_controller *ggc;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500663 struct gpio_chip *gpio;
Matt Porter036aad92015-02-17 10:48:23 -0500664 struct irq_chip *irqc;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500665 int ret;
666
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100667 ggc = kzalloc(sizeof(*ggc), GFP_KERNEL);
668 if (!ggc)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500669 return -ENOMEM;
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100670 ggc->connection = connection;
671 connection->private = ggc;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500672
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100673 ret = gb_gpio_controller_setup(ggc);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500674 if (ret)
Johan Hovold35a64f22015-02-13 14:58:04 +0800675 goto err_free_controller;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500676
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100677 irqc = &ggc->irqc;
Matt Porter036aad92015-02-17 10:48:23 -0500678 irqc->irq_ack = gb_gpio_ack_irq;
679 irqc->irq_mask = gb_gpio_mask_irq;
680 irqc->irq_unmask = gb_gpio_unmask_irq;
681 irqc->irq_set_type = gb_gpio_irq_set_type;
682 irqc->name = "greybus_gpio";
683
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100684 gpio = &ggc->chip;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500685
686 gpio->label = "greybus_gpio";
Matt Porter036aad92015-02-17 10:48:23 -0500687 gpio->dev = &connection->dev;
Johan Hovold56c2da12015-03-19 16:51:10 +0100688 gpio->owner = THIS_MODULE;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500689
690 gpio->request = gb_gpio_request;
691 gpio->free = gb_gpio_free;
692 gpio->get_direction = gb_gpio_get_direction;
693 gpio->direction_input = gb_gpio_direction_input;
694 gpio->direction_output = gb_gpio_direction_output;
695 gpio->get = gb_gpio_get;
696 gpio->set = gb_gpio_set;
697 gpio->set_debounce = gb_gpio_set_debounce;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500698 gpio->dbg_show = gb_gpio_dbg_show;
Matt Porter036aad92015-02-17 10:48:23 -0500699 gpio->to_irq = gb_gpio_to_irq;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500700 gpio->base = -1; /* Allocate base dynamically */
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100701 gpio->ngpio = ggc->line_max + 1;
Johan Hovold56c2da12015-03-19 16:51:10 +0100702 gpio->can_sleep = true;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500703
704 ret = gpiochip_add(gpio);
705 if (ret) {
Johan Hovolde5032d82015-03-19 16:51:16 +0100706 dev_err(&connection->dev, "failed to add gpio chip: %d\n",
707 ret);
Johan Hovold35a64f22015-02-13 14:58:04 +0800708 goto err_free_lines;
Matt Porter036aad92015-02-17 10:48:23 -0500709 }
710
711 ret = gb_gpio_irqchip_add(gpio, irqc, 0,
712 handle_simple_irq, IRQ_TYPE_NONE);
713 if (ret) {
Johan Hovolde5032d82015-03-19 16:51:16 +0100714 dev_err(&connection->dev, "failed to add irq chip: %d\n", ret);
Matt Porter036aad92015-02-17 10:48:23 -0500715 goto irqchip_err;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500716 }
Alex Elderbb2e1c92014-10-16 06:35:39 -0500717
718 return 0;
Matt Porter036aad92015-02-17 10:48:23 -0500719
720irqchip_err:
721 gb_gpiochip_remove(gpio);
Johan Hovold35a64f22015-02-13 14:58:04 +0800722err_free_lines:
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100723 kfree(ggc->lines);
Johan Hovold35a64f22015-02-13 14:58:04 +0800724err_free_controller:
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100725 kfree(ggc);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500726 return ret;
727}
728
Alex Elder3689f972014-10-27 06:04:30 -0500729static void gb_gpio_connection_exit(struct gb_connection *connection)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500730{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100731 struct gb_gpio_controller *ggc = connection->private;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500732
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100733 if (!ggc)
Alex Elder051fb042014-10-16 06:35:24 -0500734 return;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800735
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100736 gb_gpio_irqchip_remove(ggc);
737 gb_gpiochip_remove(&ggc->chip);
738 /* kref_put(ggc->connection) */
739 kfree(ggc->lines);
740 kfree(ggc);
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800741}
742
Alex Elder19d03de2014-11-05 16:12:53 -0600743static struct gb_protocol gpio_protocol = {
Greg Kroah-Hartman7422a1e2014-12-24 13:01:45 -0800744 .name = "gpio",
Alex Elder19d03de2014-11-05 16:12:53 -0600745 .id = GREYBUS_PROTOCOL_GPIO,
746 .major = 0,
747 .minor = 1,
Alex Elder5d9fd7e2014-11-05 16:12:54 -0600748 .connection_init = gb_gpio_connection_init,
749 .connection_exit = gb_gpio_connection_exit,
Matt Porter036aad92015-02-17 10:48:23 -0500750 .request_recv = gb_gpio_request_recv,
Alex Elder19d03de2014-11-05 16:12:53 -0600751};
752
Greg Kroah-Hartman7c7d5b92014-12-23 15:16:51 -0800753int gb_gpio_protocol_init(void)
Alex Elder19d03de2014-11-05 16:12:53 -0600754{
755 return gb_protocol_register(&gpio_protocol);
756}
757
758void gb_gpio_protocol_exit(void)
759{
760 gb_protocol_deregister(&gpio_protocol);
761}