drivers/linuxgpiod: release gpio at exit
[fw/openocd] / src / jtag / drivers / linuxgpiod.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Bitbang driver for Linux GPIO descriptors through libgpiod
4  * Copyright (C) 2020 Antonio Borneo <borneo.antonio@gmail.com>
5  *
6  * Largely based on sysfsgpio driver
7  * Copyright (C) 2012 by Creative Product Design, marc @ cpdesign.com.au
8  * Copyright (C) 2014 by Jean-Christian de Rivaz <jc@eclis.ch>
9  * Copyright (C) 2014 by Paul Fertser <fercerpav@gmail.com>
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <gpiod.h>
17 #include <jtag/interface.h>
18 #include <transport/transport.h>
19 #include "bitbang.h"
20
21 /* gpio numbers for each gpio. Negative values are invalid */
22 static int tck_gpio = -1;
23 static int tms_gpio = -1;
24 static int tdi_gpio = -1;
25 static int tdo_gpio = -1;
26 static int trst_gpio = -1;
27 static int srst_gpio = -1;
28 static int swclk_gpio = -1;
29 static int swdio_gpio = -1;
30 static int swdio_dir_gpio = -1;
31 static int led_gpio = -1;
32 static int gpiochip = -1;
33 static int tck_gpiochip = -1;
34 static int tms_gpiochip = -1;
35 static int tdi_gpiochip = -1;
36 static int tdo_gpiochip = -1;
37 static int trst_gpiochip = -1;
38 static int srst_gpiochip = -1;
39 static int swclk_gpiochip = -1;
40 static int swdio_gpiochip = -1;
41 static int swdio_dir_gpiochip = -1;
42 static int led_gpiochip = -1;
43
44 static struct gpiod_chip *gpiod_chip_tck;
45 static struct gpiod_chip *gpiod_chip_tms;
46 static struct gpiod_chip *gpiod_chip_tdi;
47 static struct gpiod_chip *gpiod_chip_tdo;
48 static struct gpiod_chip *gpiod_chip_trst;
49 static struct gpiod_chip *gpiod_chip_srst;
50 static struct gpiod_chip *gpiod_chip_swclk;
51 static struct gpiod_chip *gpiod_chip_swdio;
52 static struct gpiod_chip *gpiod_chip_swdio_dir;
53 static struct gpiod_chip *gpiod_chip_led;
54
55 static struct gpiod_line *gpiod_tck;
56 static struct gpiod_line *gpiod_tms;
57 static struct gpiod_line *gpiod_tdi;
58 static struct gpiod_line *gpiod_tdo;
59 static struct gpiod_line *gpiod_trst;
60 static struct gpiod_line *gpiod_swclk;
61 static struct gpiod_line *gpiod_swdio;
62 static struct gpiod_line *gpiod_swdio_dir;
63 static struct gpiod_line *gpiod_srst;
64 static struct gpiod_line *gpiod_led;
65
66 static int last_swclk;
67 static int last_swdio;
68 static bool last_stored;
69 static bool swdio_input;
70 static bool swdio_dir_is_active_high = true;
71
72 /* Bitbang interface read of TDO */
73 static bb_value_t linuxgpiod_read(void)
74 {
75         int retval;
76
77         retval = gpiod_line_get_value(gpiod_tdo);
78         if (retval < 0) {
79                 LOG_WARNING("reading tdo failed");
80                 return 0;
81         }
82
83         return retval ? BB_HIGH : BB_LOW;
84 }
85
86 /*
87  * Bitbang interface write of TCK, TMS, TDI
88  *
89  * Seeing as this is the only function where the outputs are changed,
90  * we can cache the old value to avoid needlessly writing it.
91  */
92 static int linuxgpiod_write(int tck, int tms, int tdi)
93 {
94         static int last_tck;
95         static int last_tms;
96         static int last_tdi;
97
98         static int first_time;
99
100         int retval;
101
102         if (!first_time) {
103                 last_tck = !tck;
104                 last_tms = !tms;
105                 last_tdi = !tdi;
106                 first_time = 1;
107         }
108
109         if (tdi != last_tdi) {
110                 retval = gpiod_line_set_value(gpiod_tdi, tdi);
111                 if (retval < 0)
112                         LOG_WARNING("writing tdi failed");
113         }
114
115         if (tms != last_tms) {
116                 retval = gpiod_line_set_value(gpiod_tms, tms);
117                 if (retval < 0)
118                         LOG_WARNING("writing tms failed");
119         }
120
121         /* write clk last */
122         if (tck != last_tck) {
123                 retval = gpiod_line_set_value(gpiod_tck, tck);
124                 if (retval < 0)
125                         LOG_WARNING("writing tck failed");
126         }
127
128         last_tdi = tdi;
129         last_tms = tms;
130         last_tck = tck;
131
132         return ERROR_OK;
133 }
134
135 static int linuxgpiod_swdio_read(void)
136 {
137         int retval;
138
139         retval = gpiod_line_get_value(gpiod_swdio);
140         if (retval < 0) {
141                 LOG_WARNING("Fail read swdio");
142                 return 0;
143         }
144
145         return retval;
146 }
147
148 static void linuxgpiod_swdio_drive(bool is_output)
149 {
150         int retval;
151
152         /*
153          * FIXME: change direction requires release and re-require the line
154          * https://stackoverflow.com/questions/58735140/
155          * this would change in future libgpiod
156          */
157         gpiod_line_release(gpiod_swdio);
158
159         if (is_output) {
160                 if (gpiod_swdio_dir) {
161                         retval = gpiod_line_set_value(gpiod_swdio_dir, swdio_dir_is_active_high ? 1 : 0);
162                         if (retval < 0)
163                                 LOG_WARNING("Fail set swdio_dir");
164                 }
165                 retval = gpiod_line_request_output(gpiod_swdio, "OpenOCD", 1);
166                 if (retval < 0)
167                         LOG_WARNING("Fail request_output line swdio");
168         } else {
169                 retval = gpiod_line_request_input(gpiod_swdio, "OpenOCD");
170                 if (retval < 0)
171                         LOG_WARNING("Fail request_input line swdio");
172                 if (gpiod_swdio_dir) {
173                         retval = gpiod_line_set_value(gpiod_swdio_dir, swdio_dir_is_active_high ? 0 : 1);
174                         if (retval < 0)
175                                 LOG_WARNING("Fail set swdio_dir");
176                 }
177         }
178
179         last_stored = false;
180         swdio_input = !is_output;
181 }
182
183 static int linuxgpiod_swd_write(int swclk, int swdio)
184 {
185         int retval;
186
187         if (!swdio_input) {
188                 if (!last_stored || (swdio != last_swdio)) {
189                         retval = gpiod_line_set_value(gpiod_swdio, swdio);
190                         if (retval < 0)
191                                 LOG_WARNING("Fail set swdio");
192                 }
193         }
194
195         /* write swclk last */
196         if (!last_stored || (swclk != last_swclk)) {
197                 retval = gpiod_line_set_value(gpiod_swclk, swclk);
198                 if (retval < 0)
199                         LOG_WARNING("Fail set swclk");
200         }
201
202         last_swdio = swdio;
203         last_swclk = swclk;
204         last_stored = true;
205
206         return ERROR_OK;
207 }
208
209 static int linuxgpiod_blink(int on)
210 {
211         int retval;
212
213         if (!gpiod_led)
214                 return ERROR_OK;
215
216         retval = gpiod_line_set_value(gpiod_led, on);
217         if (retval < 0)
218                 LOG_WARNING("Fail set led");
219         return retval;
220 }
221
222 static struct bitbang_interface linuxgpiod_bitbang = {
223         .read = linuxgpiod_read,
224         .write = linuxgpiod_write,
225         .swdio_read = linuxgpiod_swdio_read,
226         .swdio_drive = linuxgpiod_swdio_drive,
227         .swd_write = linuxgpiod_swd_write,
228         .blink = linuxgpiod_blink,
229 };
230
231 /*
232  * Bitbang interface to manipulate reset lines SRST and TRST
233  *
234  * (1) assert or (0) deassert reset lines
235  */
236 static int linuxgpiod_reset(int trst, int srst)
237 {
238         int retval1 = 0, retval2 = 0;
239
240         LOG_DEBUG("linuxgpiod_reset");
241
242         /* assume active low */
243         if (gpiod_srst) {
244                 retval1 = gpiod_line_set_value(gpiod_srst, srst ? 0 : 1);
245                 if (retval1 < 0)
246                         LOG_WARNING("set srst value failed");
247         }
248
249         /* assume active low */
250         if (gpiod_trst) {
251                 retval2 = gpiod_line_set_value(gpiod_trst, trst ? 0 : 1);
252                 if (retval2 < 0)
253                         LOG_WARNING("set trst value failed");
254         }
255
256         return ((retval1 < 0) || (retval2 < 0)) ? ERROR_FAIL : ERROR_OK;
257 }
258
259 /*
260  * Helper function to determine if gpio number is valid
261  *
262  * Assume here that there will be less than 10000 gpios per gpiochip
263  */
264 static bool is_gpio_valid(int gpio)
265 {
266         return gpio >= 0 && gpio < 10000;
267 }
268
269 static bool linuxgpiod_jtag_mode_possible(void)
270 {
271         if (!is_gpio_valid(tck_gpio))
272                 return false;
273         if (!is_gpio_valid(tms_gpio))
274                 return false;
275         if (!is_gpio_valid(tdi_gpio))
276                 return false;
277         if (!is_gpio_valid(tdo_gpio))
278                 return false;
279         return true;
280 }
281
282 static bool linuxgpiod_swd_mode_possible(void)
283 {
284         if (!is_gpio_valid(swclk_gpio))
285                 return false;
286         if (!is_gpio_valid(swdio_gpio))
287                 return false;
288         return true;
289 }
290
291 static inline void helper_release(struct gpiod_line *line)
292 {
293         if (line)
294                 gpiod_line_release(line);
295 }
296
297 static int linuxgpiod_quit(void)
298 {
299         helper_release(gpiod_led);
300         helper_release(gpiod_srst);
301         helper_release(gpiod_swdio);
302         helper_release(gpiod_swdio_dir);
303         helper_release(gpiod_swclk);
304         helper_release(gpiod_trst);
305         helper_release(gpiod_tms);
306         helper_release(gpiod_tck);
307         helper_release(gpiod_tdi);
308         helper_release(gpiod_tdo);
309
310         if (gpiod_chip_led != NULL)
311                 gpiod_chip_close(gpiod_chip_led);
312         if (gpiod_chip_srst != NULL)
313                 gpiod_chip_close(gpiod_chip_srst);
314         if (gpiod_chip_swdio != NULL)
315                 gpiod_chip_close(gpiod_chip_swdio);
316         if (gpiod_chip_swdio_dir != NULL)
317                 gpiod_chip_close(gpiod_chip_swdio_dir);
318         if (gpiod_chip_swclk != NULL)
319                 gpiod_chip_close(gpiod_chip_swclk);
320         if (gpiod_chip_trst != NULL)
321                 gpiod_chip_close(gpiod_chip_trst);
322         if (gpiod_chip_tms != NULL)
323                 gpiod_chip_close(gpiod_chip_tms);
324         if (gpiod_chip_tck != NULL)
325                 gpiod_chip_close(gpiod_chip_tck);
326         if (gpiod_chip_tdi != NULL)
327                 gpiod_chip_close(gpiod_chip_tdi);
328         if (gpiod_chip_tdo != NULL)
329                 gpiod_chip_close(gpiod_chip_tdo);
330
331         return ERROR_OK;
332 }
333
334 static struct gpiod_line *helper_get_line(const char *label,
335                 struct gpiod_chip *gpiod_chip, unsigned int offset,
336                 int val, int dir, int flags)
337 {
338         struct gpiod_line *line;
339         int retval;
340
341         line = gpiod_chip_get_line(gpiod_chip, offset);
342         if (!line) {
343                 LOG_ERROR("Error get line %s", label);
344                 return NULL;
345         }
346
347         struct gpiod_line_request_config config = {
348                 .consumer = "OpenOCD",
349                 .request_type = dir,
350                 .flags = flags,
351         };
352
353         retval = gpiod_line_request(line, &config, val);
354         if (retval < 0) {
355                 LOG_ERROR("Error requesting gpio line %s", label);
356                 return NULL;
357         }
358
359         return line;
360 }
361
362 static struct gpiod_line *helper_get_input_line(const char *label,
363                 struct gpiod_chip *gpiod_chip, unsigned int offset)
364 {
365         return helper_get_line(label, gpiod_chip, offset, 0,
366                         GPIOD_LINE_REQUEST_DIRECTION_INPUT, 0);
367 }
368
369 static struct gpiod_line *helper_get_output_line(const char *label,
370                 struct gpiod_chip *gpiod_chip, unsigned int offset, int val)
371 {
372         return helper_get_line(label, gpiod_chip, offset, val,
373                         GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, 0);
374 }
375
376 static struct gpiod_line *helper_get_open_drain_output_line(const char *label,
377                 struct gpiod_chip *gpiod_chip, unsigned int offset, int val)
378 {
379         return helper_get_line(label, gpiod_chip, offset, val,
380                         GPIOD_LINE_REQUEST_DIRECTION_OUTPUT, GPIOD_LINE_REQUEST_FLAG_OPEN_DRAIN);
381 }
382
383 static int linuxgpiod_init(void)
384 {
385         LOG_INFO("Linux GPIOD JTAG/SWD bitbang driver");
386
387         bitbang_interface = &linuxgpiod_bitbang;
388
389         /*
390          * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
391          * as outputs.  Drive TDI and TCK low, and TMS/TRST/SRST high.
392          * For SWD, SWCLK and SWDIO are configures as output high.
393          */
394
395         if (transport_is_jtag()) {
396                 if (!linuxgpiod_jtag_mode_possible()) {
397                         LOG_ERROR("Require tck, tms, tdi and tdo gpios for JTAG mode");
398                         goto out_error;
399                 }
400
401                 gpiod_chip_tdo = gpiod_chip_open_by_number(tdo_gpiochip);
402                 if (!gpiod_chip_tdo) {
403                         LOG_ERROR("Cannot open LinuxGPIOD tdo_gpiochip %d", tdo_gpiochip);
404                         goto out_error;
405                 }
406                 gpiod_chip_tdi = gpiod_chip_open_by_number(tdi_gpiochip);
407                 if (!gpiod_chip_tdi) {
408                         LOG_ERROR("Cannot open LinuxGPIOD tdi_gpiochip %d", tdi_gpiochip);
409                         goto out_error;
410                 }
411                 gpiod_chip_tck = gpiod_chip_open_by_number(tck_gpiochip);
412                 if (!gpiod_chip_tck) {
413                         LOG_ERROR("Cannot open LinuxGPIOD tck_gpiochip %d", tck_gpiochip);
414                         goto out_error;
415                 }
416                 gpiod_chip_tms = gpiod_chip_open_by_number(tms_gpiochip);
417                 if (!gpiod_chip_tms) {
418                         LOG_ERROR("Cannot open LinuxGPIOD tms_gpiochip %d", tms_gpiochip);
419                         goto out_error;
420                 }
421
422                 gpiod_tdo = helper_get_input_line("tdo", gpiod_chip_tdo, tdo_gpio);
423                 if (!gpiod_tdo)
424                         goto out_error;
425
426                 gpiod_tdi = helper_get_output_line("tdi", gpiod_chip_tdi, tdi_gpio, 0);
427                 if (!gpiod_tdi)
428                         goto out_error;
429
430                 gpiod_tck = helper_get_output_line("tck", gpiod_chip_tck, tck_gpio, 0);
431                 if (!gpiod_tck)
432                         goto out_error;
433
434                 gpiod_tms = helper_get_output_line("tms", gpiod_chip_tms, tms_gpio, 1);
435                 if (!gpiod_tms)
436                         goto out_error;
437
438                 if (is_gpio_valid(trst_gpio)) {
439                         gpiod_chip_trst = gpiod_chip_open_by_number(trst_gpiochip);
440                         if (!gpiod_chip_trst) {
441                                 LOG_ERROR("Cannot open LinuxGPIOD trst_gpiochip %d", trst_gpiochip);
442                                 goto out_error;
443                         }
444
445                         if (jtag_get_reset_config() & RESET_TRST_OPEN_DRAIN)
446                                 gpiod_trst = helper_get_open_drain_output_line("trst", gpiod_chip_trst, trst_gpio, 1);
447                         else
448                                 gpiod_trst = helper_get_output_line("trst", gpiod_chip_trst, trst_gpio, 1);
449
450                         if (!gpiod_trst)
451                                 goto out_error;
452                 }
453         }
454
455         if (transport_is_swd()) {
456                 if (!linuxgpiod_swd_mode_possible()) {
457                         LOG_ERROR("Require swclk and swdio gpio for SWD mode");
458                         goto out_error;
459                 }
460
461                 gpiod_chip_swclk = gpiod_chip_open_by_number(swclk_gpiochip);
462                 if (!gpiod_chip_swclk) {
463                         LOG_ERROR("Cannot open LinuxGPIOD swclk_gpiochip %d", swclk_gpiochip);
464                         goto out_error;
465                 }
466                 gpiod_chip_swdio = gpiod_chip_open_by_number(swdio_gpiochip);
467                 if (!gpiod_chip_swdio) {
468                         LOG_ERROR("Cannot open LinuxGPIOD swdio_gpiochip %d", swdio_gpiochip);
469                         goto out_error;
470                 }
471
472                 if (is_gpio_valid(swdio_dir_gpio)) {
473                         gpiod_chip_swdio_dir = gpiod_chip_open_by_number(swdio_dir_gpiochip);
474                         if (!gpiod_chip_swdio_dir) {
475                                 LOG_ERROR("Cannot open LinuxGPIOD swdio_dir_gpiochip %d", swdio_dir_gpiochip);
476                                 goto out_error;
477                         }
478                 }
479
480                 gpiod_swclk = helper_get_output_line("swclk", gpiod_chip_swclk, swclk_gpio, 1);
481                 if (!gpiod_swclk)
482                         goto out_error;
483
484                 /* Set buffer direction before making SWDIO an output */
485                 if (is_gpio_valid(swdio_dir_gpio)) {
486                         gpiod_swdio_dir = helper_get_output_line("swdio_dir", gpiod_chip_swdio_dir, swdio_dir_gpio,
487                                         swdio_dir_is_active_high ? 1 : 0);
488                         if (!gpiod_swdio_dir)
489                                 goto out_error;
490                 }
491
492                 gpiod_swdio = helper_get_output_line("swdio", gpiod_chip_swdio, swdio_gpio, 1);
493                 if (!gpiod_swdio)
494                         goto out_error;
495         }
496
497         if (is_gpio_valid(srst_gpio)) {
498                 gpiod_chip_srst = gpiod_chip_open_by_number(srst_gpiochip);
499                 if (!gpiod_chip_srst) {
500                         LOG_ERROR("Cannot open LinuxGPIOD srst_gpiochip %d", srst_gpiochip);
501                         goto out_error;
502                 }
503
504                 if (jtag_get_reset_config() & RESET_SRST_PUSH_PULL)
505                         gpiod_srst = helper_get_output_line("srst", gpiod_chip_srst, srst_gpio, 1);
506                 else
507                         gpiod_srst = helper_get_open_drain_output_line("srst", gpiod_chip_srst, srst_gpio, 1);
508
509                 if (!gpiod_srst)
510                         goto out_error;
511         }
512
513         if (is_gpio_valid(led_gpio)) {
514                 gpiod_chip_led = gpiod_chip_open_by_number(led_gpiochip);
515                 if (!gpiod_chip_led) {
516                         LOG_ERROR("Cannot open LinuxGPIOD led_gpiochip %d", led_gpiochip);
517                         goto out_error;
518                 }
519
520                 gpiod_led = helper_get_output_line("led", gpiod_chip_led, led_gpio, 0);
521                 if (!gpiod_led)
522                         goto out_error;
523         }
524
525         return ERROR_OK;
526
527 out_error:
528         linuxgpiod_quit();
529
530         return ERROR_JTAG_INIT_FAILED;
531 }
532
533 COMMAND_HELPER(linuxgpiod_helper_gpionum, const char *name, int *chip, int *line)
534 {
535         int i = 0;
536         if (CMD_ARGC > 2)
537                 return ERROR_COMMAND_SYNTAX_ERROR;
538         if (CMD_ARGC == 2) {
539                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], *chip);
540                 i = 1;
541         }
542         if (CMD_ARGC > 0)
543                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[i], *line);
544         command_print(CMD, "LinuxGPIOD %s: chip = %d, num = %d", name, *chip, *line);
545         return ERROR_OK;
546 }
547
548 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionums)
549 {
550         if (CMD_ARGC == 4) {
551                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], tck_gpio);
552                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], tms_gpio);
553                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], tdi_gpio);
554                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[3], tdo_gpio);
555         } else if (CMD_ARGC != 0) {
556                 return ERROR_COMMAND_SYNTAX_ERROR;
557         }
558
559         command_print(CMD,
560                         "LinuxGPIOD nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
561                         tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
562
563         return ERROR_OK;
564 }
565
566 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tck)
567 {
568         return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "tck", &tck_gpiochip,
569                         &tck_gpio);
570 }
571
572 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tms)
573 {
574         return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "tms", &tms_gpiochip,
575                         &tms_gpio);
576 }
577
578 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdo)
579 {
580         return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "tdo", &tdo_gpiochip,
581                         &tdo_gpio);
582 }
583
584 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_tdi)
585 {
586         return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "tdi", &tdi_gpiochip,
587                         &tdi_gpio);
588 }
589
590 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_srst)
591 {
592         return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "srst", &srst_gpiochip,
593                         &srst_gpio);
594 }
595
596 COMMAND_HANDLER(linuxgpiod_handle_jtag_gpionum_trst)
597 {
598         return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "trst", &trst_gpiochip,
599                         &trst_gpio);
600 }
601
602 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionums)
603 {
604         if (CMD_ARGC == 2) {
605                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], swclk_gpio);
606                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], swdio_gpio);
607         } else if (CMD_ARGC != 0) {
608                 return ERROR_COMMAND_SYNTAX_ERROR;
609         }
610
611         command_print(CMD,
612                         "LinuxGPIOD nums: swclk = %d, swdio = %d",
613                         swclk_gpio, swdio_gpio);
614
615         return ERROR_OK;
616 }
617
618 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swclk)
619 {
620         return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "swclk", &swclk_gpiochip,
621                         &swclk_gpio);
622 }
623
624 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio)
625 {
626         return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "swdio", &swdio_gpiochip,
627                         &swdio_gpio);
628 }
629
630 COMMAND_HANDLER(linuxgpiod_handle_swd_gpionum_swdio_dir)
631 {
632         return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "swdio_dir", &swdio_dir_gpiochip,
633                         &swdio_dir_gpio);
634 }
635
636 COMMAND_HANDLER(linuxgpiod_handle_gpionum_led)
637 {
638         return CALL_COMMAND_HANDLER(linuxgpiod_helper_gpionum, "led", &led_gpiochip,
639                         &led_gpio);
640 }
641
642 COMMAND_HANDLER(linuxgpiod_handle_gpiochip)
643 {
644         if (CMD_ARGC == 1) {
645                 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], gpiochip);
646                 tck_gpiochip = gpiochip;
647                 tms_gpiochip = gpiochip;
648                 tdi_gpiochip = gpiochip;
649                 tdo_gpiochip = gpiochip;
650                 trst_gpiochip = gpiochip;
651                 srst_gpiochip = gpiochip;
652                 swclk_gpiochip = gpiochip;
653                 swdio_gpiochip = gpiochip;
654                 swdio_dir_gpiochip = gpiochip;
655                 led_gpiochip = gpiochip;
656         }
657
658         command_print(CMD, "LinuxGPIOD gpiochip = %d", gpiochip);
659         return ERROR_OK;
660 }
661
662 static const struct command_registration linuxgpiod_subcommand_handlers[] = {
663         {
664                 .name = "jtag_nums",
665                 .handler = linuxgpiod_handle_jtag_gpionums,
666                 .mode = COMMAND_CONFIG,
667                 .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
668                 .usage = "tck tms tdi tdo",
669         },
670         {
671                 .name = "tck_num",
672                 .handler = linuxgpiod_handle_jtag_gpionum_tck,
673                 .mode = COMMAND_CONFIG,
674                 .help = "gpio chip number (optional) and gpio number for tck.",
675                 .usage = "[chip] tck",
676         },
677         {
678                 .name = "tms_num",
679                 .handler = linuxgpiod_handle_jtag_gpionum_tms,
680                 .mode = COMMAND_CONFIG,
681                 .help = "gpio chip number (optional) and gpio number for tms.",
682                 .usage = "[chip] tms",
683         },
684         {
685                 .name = "tdo_num",
686                 .handler = linuxgpiod_handle_jtag_gpionum_tdo,
687                 .mode = COMMAND_CONFIG,
688                 .help = "gpio chip number (optional) and gpio number for tdo.",
689                 .usage = "[chip] tdo",
690         },
691         {
692                 .name = "tdi_num",
693                 .handler = linuxgpiod_handle_jtag_gpionum_tdi,
694                 .mode = COMMAND_CONFIG,
695                 .help = "gpio chip number (optional) and gpio number for tdi.",
696                 .usage = "[chip] tdi",
697         },
698         {
699                 .name = "srst_num",
700                 .handler = linuxgpiod_handle_jtag_gpionum_srst,
701                 .mode = COMMAND_CONFIG,
702                 .help = "gpio chip number (optional) and gpio number for srst.",
703                 .usage = "[chip] srst",
704         },
705         {
706                 .name = "trst_num",
707                 .handler = linuxgpiod_handle_jtag_gpionum_trst,
708                 .mode = COMMAND_CONFIG,
709                 .help = "gpio chip number (optional) and gpio number for trst.",
710                 .usage = "[chip] trst",
711         },
712         {
713                 .name = "swd_nums",
714                 .handler = linuxgpiod_handle_swd_gpionums,
715                 .mode = COMMAND_CONFIG,
716                 .help = "gpio numbers for swclk, swdio. (in that order)",
717                 .usage = "swclk swdio",
718         },
719         {
720                 .name = "swclk_num",
721                 .handler = linuxgpiod_handle_swd_gpionum_swclk,
722                 .mode = COMMAND_CONFIG,
723                 .help = "gpio chip number (optional) and gpio number for swclk.",
724                 .usage = "[chip] swclk",
725         },
726         {
727                 .name = "swdio_num",
728                 .handler = linuxgpiod_handle_swd_gpionum_swdio,
729                 .mode = COMMAND_CONFIG,
730                 .help = "gpio chip number (optional) and gpio number for swdio.",
731                 .usage = "[chip] swdio",
732         },
733         {
734                 .name = "swdio_dir_num",
735                 .handler = linuxgpiod_handle_swd_gpionum_swdio_dir,
736                 .mode = COMMAND_CONFIG,
737                 .help = "gpio chip number (optional) and gpio number for swdio_dir.",
738                 .usage = "[chip] swdio_dir",
739         },
740         {
741                 .name = "led_num",
742                 .handler = linuxgpiod_handle_gpionum_led,
743                 .mode = COMMAND_CONFIG,
744                 .help = "gpio chip number (optional) and gpio number for LED.",
745                 .usage = "[chip] led",
746         },
747         {
748                 .name = "gpiochip",
749                 .handler = linuxgpiod_handle_gpiochip,
750                 .mode = COMMAND_CONFIG,
751                 .help = "number of the gpiochip.",
752                 .usage = "gpiochip",
753         },
754         COMMAND_REGISTRATION_DONE
755 };
756
757 static const struct command_registration linuxgpiod_command_handlers[] = {
758         {
759                 .name = "linuxgpiod",
760                 .mode = COMMAND_ANY,
761                 .help = "perform linuxgpiod management",
762                 .chain = linuxgpiod_subcommand_handlers,
763                 .usage = "",
764         },
765         COMMAND_REGISTRATION_DONE
766 };
767
768 static const char *const linuxgpiod_transport[] = { "swd", "jtag", NULL };
769
770 static struct jtag_interface linuxgpiod_interface = {
771         .supported = DEBUG_CAP_TMS_SEQ,
772         .execute_queue = bitbang_execute_queue,
773 };
774
775 struct adapter_driver linuxgpiod_adapter_driver = {
776         .name = "linuxgpiod",
777         .transports = linuxgpiod_transport,
778         .commands = linuxgpiod_command_handlers,
779
780         .init = linuxgpiod_init,
781         .quit = linuxgpiod_quit,
782         .reset = linuxgpiod_reset,
783
784         .jtag_ops = &linuxgpiod_interface,
785         .swd_ops = &bitbang_swd,
786 };