blob: a9153a85b01123d6df8580a86a386c4019ebbdec [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{
503 u32 line_count;
504 size_t size;
505 int ret;
506
507 /* First thing we need to do is check the version */
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100508 ret = get_version(ggc);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500509 if (ret)
Johan Hovold86c68162015-03-19 16:51:15 +0100510 return ret;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500511
512 /* Now find out how many lines there are */
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100513 ret = gb_gpio_line_count_operation(ggc);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500514 if (ret)
Johan Hovold86c68162015-03-19 16:51:15 +0100515 return ret;
516
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100517 line_count = (u32)ggc->line_max + 1;
518 size = line_count * sizeof(*ggc->lines);
519 ggc->lines = kzalloc(size, GFP_KERNEL);
520 if (!ggc->lines)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500521 return -ENOMEM;
522
523 return ret;
524}
525
Matt Porter036aad92015-02-17 10:48:23 -0500526/**
527 * gb_gpio_irq_map() - maps an IRQ into a GB gpio irqchip
528 * @d: the irqdomain used by this irqchip
529 * @irq: the global irq number used by this GB gpio irqchip irq
530 * @hwirq: the local IRQ/GPIO line offset on this GB gpio
531 *
532 * This function will set up the mapping for a certain IRQ line on a
533 * GB gpio by assigning the GB gpio as chip data, and using the irqchip
534 * stored inside the GB gpio.
535 */
536static int gb_gpio_irq_map(struct irq_domain *domain, unsigned int irq,
537 irq_hw_number_t hwirq)
538{
539 struct gpio_chip *chip = domain->host_data;
540 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
541
542 irq_set_chip_data(irq, ggc);
543 irq_set_chip_and_handler(irq, ggc->irqchip, ggc->irq_handler);
544#ifdef CONFIG_ARM
545 set_irq_flags(irq, IRQF_VALID);
546#else
547 irq_set_noprobe(irq);
548#endif
549 /*
550 * No set-up of the hardware will happen if IRQ_TYPE_NONE
551 * is passed as default type.
552 */
553 if (ggc->irq_default_type != IRQ_TYPE_NONE)
554 irq_set_irq_type(irq, ggc->irq_default_type);
555
556 return 0;
557}
558
559static void gb_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
560{
561#ifdef CONFIG_ARM
562 set_irq_flags(irq, 0);
563#endif
564 irq_set_chip_and_handler(irq, NULL, NULL);
565 irq_set_chip_data(irq, NULL);
566}
567
568static const struct irq_domain_ops gb_gpio_domain_ops = {
569 .map = gb_gpio_irq_map,
570 .unmap = gb_gpio_irq_unmap,
571};
572
573/**
574 * gb_gpio_irqchip_remove() - removes an irqchip added to a gb_gpio_controller
575 * @ggc: the gb_gpio_controller to remove the irqchip from
576 *
577 * This is called only from gb_gpio_remove()
578 */
579static void gb_gpio_irqchip_remove(struct gb_gpio_controller *ggc)
580{
581 unsigned int offset;
582
583 /* Remove all IRQ mappings and delete the domain */
584 if (ggc->irqdomain) {
585 for (offset = 0; offset < (ggc->line_max + 1); offset++)
586 irq_dispose_mapping(irq_find_mapping(ggc->irqdomain, offset));
587 irq_domain_remove(ggc->irqdomain);
588 }
589
590 if (ggc->irqchip) {
591 ggc->irqchip = NULL;
592 }
593}
594
595
596/**
597 * gb_gpio_irqchip_add() - adds an irqchip to a gpio chip
598 * @chip: the gpio chip to add the irqchip to
599 * @irqchip: the irqchip to add to the adapter
600 * @first_irq: if not dynamically assigned, the base (first) IRQ to
601 * allocate gpio irqs from
602 * @handler: the irq handler to use (often a predefined irq core function)
603 * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
604 * to have the core avoid setting up any default type in the hardware.
605 *
606 * This function closely associates a certain irqchip with a certain
607 * gpio chip, providing an irq domain to translate the local IRQs to
608 * global irqs, and making sure that the gpio chip
609 * is passed as chip data to all related functions. Driver callbacks
610 * need to use container_of() to get their local state containers back
611 * from the gpio chip passed as chip data. An irqdomain will be stored
612 * in the gpio chip that shall be used by the driver to handle IRQ number
613 * translation. The gpio chip will need to be initialized and registered
614 * before calling this function.
615 */
616static int gb_gpio_irqchip_add(struct gpio_chip *chip,
617 struct irq_chip *irqchip,
618 unsigned int first_irq,
619 irq_flow_handler_t handler,
620 unsigned int type)
621{
622 struct gb_gpio_controller *ggc;
623 unsigned int offset;
624 unsigned irq_base;
625
626 if (!chip || !irqchip)
627 return -EINVAL;
628
629 ggc = gpio_chip_to_gb_gpio_controller(chip);
630
631 ggc->irqchip = irqchip;
632 ggc->irq_handler = handler;
633 ggc->irq_default_type = type;
634 ggc->irqdomain = irq_domain_add_simple(NULL,
635 ggc->line_max + 1, first_irq,
636 &gb_gpio_domain_ops, chip);
637 if (!ggc->irqdomain) {
638 ggc->irqchip = NULL;
639 return -EINVAL;
640 }
641
642 /*
643 * Prepare the mapping since the irqchip shall be orthogonal to
644 * any gpio calls. If the first_irq was zero, this is
645 * necessary to allocate descriptors for all IRQs.
646 */
647 for (offset = 0; offset < (ggc->line_max + 1); offset++) {
648 irq_base = irq_create_mapping(ggc->irqdomain, offset);
649 if (offset == 0)
650 ggc->irq_base = irq_base;
651 }
652
653 return 0;
654}
655
656static int gb_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
657{
658 struct gb_gpio_controller *ggc = gpio_chip_to_gb_gpio_controller(chip);
659
660 return irq_find_mapping(ggc->irqdomain, offset);
661}
662
Alex Elder3689f972014-10-27 06:04:30 -0500663static int gb_gpio_connection_init(struct gb_connection *connection)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500664{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100665 struct gb_gpio_controller *ggc;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500666 struct gpio_chip *gpio;
Matt Porter036aad92015-02-17 10:48:23 -0500667 struct irq_chip *irqc;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500668 int ret;
669
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100670 ggc = kzalloc(sizeof(*ggc), GFP_KERNEL);
671 if (!ggc)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500672 return -ENOMEM;
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100673 ggc->connection = connection;
674 connection->private = ggc;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500675
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100676 ret = gb_gpio_controller_setup(ggc);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500677 if (ret)
Johan Hovold35a64f22015-02-13 14:58:04 +0800678 goto err_free_controller;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500679
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100680 irqc = &ggc->irqc;
Matt Porter036aad92015-02-17 10:48:23 -0500681 irqc->irq_ack = gb_gpio_ack_irq;
682 irqc->irq_mask = gb_gpio_mask_irq;
683 irqc->irq_unmask = gb_gpio_unmask_irq;
684 irqc->irq_set_type = gb_gpio_irq_set_type;
685 irqc->name = "greybus_gpio";
686
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100687 gpio = &ggc->chip;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500688
689 gpio->label = "greybus_gpio";
Matt Porter036aad92015-02-17 10:48:23 -0500690 gpio->dev = &connection->dev;
Johan Hovold56c2da12015-03-19 16:51:10 +0100691 gpio->owner = THIS_MODULE;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500692
693 gpio->request = gb_gpio_request;
694 gpio->free = gb_gpio_free;
695 gpio->get_direction = gb_gpio_get_direction;
696 gpio->direction_input = gb_gpio_direction_input;
697 gpio->direction_output = gb_gpio_direction_output;
698 gpio->get = gb_gpio_get;
699 gpio->set = gb_gpio_set;
700 gpio->set_debounce = gb_gpio_set_debounce;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500701 gpio->dbg_show = gb_gpio_dbg_show;
Matt Porter036aad92015-02-17 10:48:23 -0500702 gpio->to_irq = gb_gpio_to_irq;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500703 gpio->base = -1; /* Allocate base dynamically */
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100704 gpio->ngpio = ggc->line_max + 1;
Johan Hovold56c2da12015-03-19 16:51:10 +0100705 gpio->can_sleep = true;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500706
707 ret = gpiochip_add(gpio);
708 if (ret) {
Johan Hovolde5032d82015-03-19 16:51:16 +0100709 dev_err(&connection->dev, "failed to add gpio chip: %d\n",
710 ret);
Johan Hovold35a64f22015-02-13 14:58:04 +0800711 goto err_free_lines;
Matt Porter036aad92015-02-17 10:48:23 -0500712 }
713
714 ret = gb_gpio_irqchip_add(gpio, irqc, 0,
715 handle_simple_irq, IRQ_TYPE_NONE);
716 if (ret) {
Johan Hovolde5032d82015-03-19 16:51:16 +0100717 dev_err(&connection->dev, "failed to add irq chip: %d\n", ret);
Matt Porter036aad92015-02-17 10:48:23 -0500718 goto irqchip_err;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500719 }
Alex Elderbb2e1c92014-10-16 06:35:39 -0500720
721 return 0;
Matt Porter036aad92015-02-17 10:48:23 -0500722
723irqchip_err:
724 gb_gpiochip_remove(gpio);
Johan Hovold35a64f22015-02-13 14:58:04 +0800725err_free_lines:
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100726 kfree(ggc->lines);
Johan Hovold35a64f22015-02-13 14:58:04 +0800727err_free_controller:
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100728 kfree(ggc);
Alex Elderbb2e1c92014-10-16 06:35:39 -0500729 return ret;
730}
731
Alex Elder3689f972014-10-27 06:04:30 -0500732static void gb_gpio_connection_exit(struct gb_connection *connection)
Alex Elderbb2e1c92014-10-16 06:35:39 -0500733{
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100734 struct gb_gpio_controller *ggc = connection->private;
Alex Elderbb2e1c92014-10-16 06:35:39 -0500735
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100736 if (!ggc)
Alex Elder051fb042014-10-16 06:35:24 -0500737 return;
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800738
Johan Hovold8aff1ac2015-03-19 16:51:14 +0100739 gb_gpio_irqchip_remove(ggc);
740 gb_gpiochip_remove(&ggc->chip);
741 /* kref_put(ggc->connection) */
742 kfree(ggc->lines);
743 kfree(ggc);
Greg Kroah-Hartmanc16854c2014-08-12 12:00:16 +0800744}
745
Alex Elder19d03de2014-11-05 16:12:53 -0600746static struct gb_protocol gpio_protocol = {
Greg Kroah-Hartman7422a1e2014-12-24 13:01:45 -0800747 .name = "gpio",
Alex Elder19d03de2014-11-05 16:12:53 -0600748 .id = GREYBUS_PROTOCOL_GPIO,
749 .major = 0,
750 .minor = 1,
Alex Elder5d9fd7e2014-11-05 16:12:54 -0600751 .connection_init = gb_gpio_connection_init,
752 .connection_exit = gb_gpio_connection_exit,
Matt Porter036aad92015-02-17 10:48:23 -0500753 .request_recv = gb_gpio_request_recv,
Alex Elder19d03de2014-11-05 16:12:53 -0600754};
755
Greg Kroah-Hartman7c7d5b92014-12-23 15:16:51 -0800756int gb_gpio_protocol_init(void)
Alex Elder19d03de2014-11-05 16:12:53 -0600757{
758 return gb_protocol_register(&gpio_protocol);
759}
760
761void gb_gpio_protocol_exit(void)
762{
763 gb_protocol_deregister(&gpio_protocol);
764}