3a31ade70a43542c731f3a38bcbabf6ebdf902d6
[linux-gpio-pinout] / gpio.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5 use Data::Dump qw(dump);
6 use Getopt::Long;
7
8 my $opt_svg = 0;
9 my $opt_alt = 0;
10 my $opt_invert = 0;
11 my $opt_vertical = 0;
12 my $opt_horizontal = 0;
13 my $opt_edge = 0;
14 my $opt_middle = 0;
15 my $opt_zebra = 0;
16 my $opt_lines = 0;
17 my $opt_read = '';
18 my $opt_pins = '';
19 my $opt_color = 0;
20 my $opt_pinmux = 0;
21 GetOptions(
22         'svg!' => \$opt_svg,
23         'alt!' => \$opt_alt,
24         'invert!' => \$opt_invert,
25         'vertical-flip!' => \$opt_vertical,
26         'horizontal-flip!' => \$opt_horizontal,
27         'edge-pins!' => \$opt_edge,
28         'middle-pins!' => \$opt_middle,
29         'zebra!' => \$opt_zebra,
30         'lines!' => \$opt_lines,
31         'read=s' => \$opt_read,
32         'pins=s' => \$opt_pins,
33         'color' => \$opt_color,
34         'pinmux' => \$opt_pinmux,
35 );
36
37 # svg font hints
38 my $font_w = 1.67; # < 2.54, font is not perfect square
39 my $font_b = 2.10; # font baseline position
40
41 $opt_read .= '/' unless $opt_read =~ m/\/$/;
42
43 sub slurp {
44         open(my $fh, '<', $opt_read . shift);
45         local $/ = undef;
46         <$fh>;
47 }
48
49 my $pins;
50
51 my $model = slurp('/proc/device-tree/model');
52 $model =~ s/\x00$//; # strip kernel NULL
53 warn "# model [$model]";
54
55 OPEN_PINS_AGAIN:
56 open(DATA, '<', $opt_pins) if $opt_pins;
57
58 my @lines;
59 my $line_i = 0;
60
61 my $include = 0;
62 while(<DATA>) {
63         chomp;
64         if ( m/^#\s(.+)/ ) {
65                 warn "MODEL [$1] == [$model] ?\n";
66                 if ( $model =~ m/$1/ ) {
67                         $include = 1;
68                 } else {
69                         $include = 0;
70                 }
71         } elsif ( $include || $opt_pins ) {
72                 push @{ $pins->{$1} }, $line_i while ( m/\t\s*(\w+\d+)/g );
73
74                 push @lines, $_;
75
76                 $line_i++;
77         } else {
78                 warn "IGNORE: [$_]\n";
79         }
80 }
81
82 if ( ! $opt_pins && ! $pins ) {
83         my $glob = $model;
84         $glob =~ s/^(\w+).*$/$1/;
85         my @pins = glob "pins/${glob}*";
86         warn "# possible pins: ",dump( \@pins );
87         $opt_pins = $pins[0];
88         goto OPEN_PINS_AGAIN;
89 }
90
91 die "add pin definition for # $model" unless $pins;
92
93 #warn "# lines ",dump( \@lines );
94 warn "# pins ",dump($pins);
95
96 my $serial_tty;
97 foreach (
98         glob($opt_read . '/sys/devices/platform/soc*/*.serial/tty/tty*'),       # 4.x
99         glob(            '/sys/devices/soc.*/*.uart/tty/tty*')                  # 3.10
100 ) {
101         my @v = split(/\//, $_);
102         $serial_tty->{ $v[-3] } = $v[-1];
103 }
104 warn "# serial_tty = ",dump($serial_tty);
105
106
107 my $pin_function;
108 my $device;
109 my $pin;
110 my $function;
111
112 sub annotate_pin {
113         my ($pin, $note) = @_;
114         if ( $pins->{$pin} ) {
115                 foreach my $line ( @{$pins->{$pin}} ) {
116                         my $t = $lines[$line];
117                         if ( $opt_svg ) {
118                                 $t =~ s/$pin/$note/;
119                         } else {
120                                 $t =~ s/$pin/$pin $note/ || warn "can't find $pin in [$t]";
121                         }
122                         $lines[$line] = $t;
123                         warn "# $line: $lines[$line]\n";
124                 }
125         } else {
126                 warn "IGNORED: pin $pin function $note\n";
127         }
128 }
129
130 open(my $fh, '<', $opt_read . '/sys/kernel/debug/pinctrl/pinctrl-maps');
131 while(<$fh>) {
132         chomp;
133         if ( m/^device (\S+)/ ) {
134                 $device = $1;
135                 if ( my $replace = $serial_tty->{$device} ) {
136                         $device = $replace; # replace serial hex with kernel name
137                 } else {
138                         $device =~ s/^[0-9a-f]*\.//; # remove hex address
139                 }
140         } elsif ( m/^group (\w+\d+)/ ) {
141                 $pin = $1;
142
143         } elsif ( m/^function (\S+)/ ) {
144                 $function = $1;
145         } elsif ( m/^$/ ) {
146                 if ( $device && $pin && $function ) {
147                         push @{ $pin_function->{$pin} }, "$device $function";
148
149                         annotate_pin $pin, "[$device $function]";
150                 } else {
151                         warn "missing one of ",dump( $device, $pin, $function );
152                 }
153
154                 $device = undef;
155                 $pin = undef;
156                 $function = undef;
157
158         }
159 }
160
161 warn "# pin_function = ",dump($pin_function);
162
163
164 # insert kernel gpio info
165 my $linux_gpio_name;
166 open(my $pins_fh, '<', (glob "/sys/kernel/debug/pinctrl/*/pins")[0]);
167 while(<$pins_fh>) {
168         if ( m/^pin (\d+) \(([^\)]+)\)/ ) {
169                 $linux_gpio_name->{$1} = $2;
170         }
171 }
172 warn "# linux_gpio_name = ",dump( $linux_gpio_name );
173
174
175 my $gpio_debug;
176 open(my $gpio_fh, '<', '/sys/kernel/debug/gpio');
177 while(<$gpio_fh>) {
178         if (m/|/ ) {
179                 s/^\s+//;
180                 s/\s+$//;
181                 my @l = split(/\s*[\(\|\)]\s*/, $_);
182                 warn "XXX ", dump( \@l );
183                 if ( $l[0] =~ m/gpio-(\d+)/ ) {
184                         if ( my $pin = $linux_gpio_name->{$1} ) {
185                                 $gpio_debug->{ $pin } = $l[2];
186                                 $l[3] =~ s/\s\s+/ /g;
187                                 annotate_pin $pin, qq{"$l[2]" $l[3]};
188                         } else {
189                                 warn "FIXME can't find $1 in ",dump( $linux_gpio_name );
190                         }
191                 }
192         }
193
194 }
195 warn "# gpio_debug = ",dump( $gpio_debug );
196
197
198
199 my $have_sunxi_pio = `which sunxi-pio`;
200 if ( $have_sunxi_pio ) {
201
202 open(my $pio, '-|', 'sunxi-pio -m print');
203 while(<$pio>) {
204         chomp;
205         s/[<>]+/ /g;
206         my @p = split(/\s+/,$_);
207         warn "# pio ",dump(\@p);
208         # annotate input 0 and output 1 pins
209 #       annotate_pin $p[0], ( $p[1] ? 'O' : 'I' ) . ':' . $p[4] if $p[1] == 0 || $p[1] == 1;
210         my $pin = shift @p;
211         annotate_pin $pin, join(' ',@p) if ! $opt_svg;
212 }
213 close($pio);
214
215 } # have_sunxi_pio
216
217
218 my $pinmux;
219 my $pinmux_path = (glob("/sys/kernel/debug/pinctrl/*/pinmux-functions"))[0];
220 if ( $opt_pinmux && -e $pinmux_path ) {
221         open(my $mux, '<', $pinmux_path);
222         while(<$mux>) {
223                 chomp;
224                 if ( m/function: (\w+), groups = \[ (.*) \]/ ) {
225                         my ( $func, $pins ) = ( $1, $2 );
226                         foreach ( split(/\s+/,$pins) ) {
227                                 push @{ $pinmux->{$_} }, $func;
228                         }
229                 } else {
230                         warn "IGNORED [$pinmux_path] [$_]\n";
231                 }
232         }
233
234         foreach my $pin ( keys %$pinmux ) {
235                 if ( exists $pins->{$pin} ) {
236                         annotate_pin $pin, '{' . join(' ', @{$pinmux->{$pin}}) . '}';
237                 } else {
238                         warn "IGNORED mux on $pin\n";
239                 }
240         }
241
242         warn "# pinmux = ",dump( $pinmux );
243 }
244
245
246
247 my @max_len = ( 0,0,0,0 );
248 my @line_parts;
249
250 shift(@lines) while ( ! $lines[0] );    # remove empty at beginning
251 pop(@lines) while ( ! $lines[-1] );     # remove empty at end
252
253 foreach my $line (@lines) {
254         if ( $line =~ m/^#/ ) {
255                 push @line_parts, [ $line ] unless $opt_svg && $line =~ m/^###+/; # SVG doesn't display 3rd level comments
256                 next;
257         }
258         $line =~ s/\[(\w+)\s+(\w+)\] \[\1\s+(\w+)\]/[$1 $2 $3]/g; # compress kernel annotation with same prefix
259         $line =~ s/(\[(?:uart\d*|serial|tty\w+))([^\t]*\]\s[^\t]*(rx|tx)d?)/$1 $3$2/gi;
260         $line =~ s/(\[i2c)([^\t]*\]\s[^\t]*(scl?k?|sda))/$1 $3$2/gi;
261         $line =~ s/(\[spi)([^\t]*\]\s[^\t]*(miso|mosi|s?clk|c[se]\d*))/$1 $3$2/gi;
262         $line =~ s/\s*\([^\)]+\)//g if ! $opt_alt;
263
264         # shorten duplicate kernel device/function
265         $line =~ s/\[serial (\w+) (uart\d+)\]/[$2 $1]/g;
266         $line =~ s/\[(\w+) (\w+) \1(\d+)\]/[$1$3 $2]/g;
267
268         $line =~ s/\[(\w+)\s+([^\]]+)\s+\1\]/[$1 $2]/g; # duplicate
269
270         my @v = split(/\s*\t+\s*/,$line,4);
271         @v = ( $v[2], $v[3], $v[0], $v[1] ) if $opt_horizontal && $v[2];
272
273         push @line_parts, [ @v ];
274         foreach my $i ( 0 .. 3 ) {
275                 next unless exists $v[$i];
276                 next if $v[$i] =~ m/^#/; # don't calculate comments into max length
277                 my $l = length($v[$i]);
278                 $max_len[$i] = $l if $l > $max_len[$i];
279         }
280 }
281
282 warn "# max_len = ",dump( \@max_len );
283 warn "# line_parts = ",dump( \@line_parts );
284
285 #print "$_\n" foreach @lines;
286
287 my $x = 20.00; # mm
288 my $y = 20.00; # mm
289
290 if ( $opt_svg ) {
291         print qq{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
292 <svg
293    xmlns:dc="http://purl.org/dc/elements/1.1/"
294    xmlns:cc="http://creativecommons.org/ns#"
295    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
296    xmlns:svg="http://www.w3.org/2000/svg"
297    xmlns="http://www.w3.org/2000/svg"
298    xmlns:xlink="http://www.w3.org/1999/xlink"
299    id="svg8"
300    version="1.1"
301    viewBox="0 0 210 297"
302    height="297mm"
303    width="210mm">
304
305
306 <g id="layer1">
307
308         }; # svg, insert rest of rect
309
310         print qq{<rect x="0" y="0" width="210" height="297" style="fill:#000000" id="high-contrast"/>} if $opt_invert;
311 }
312
313 my @later;
314
315 my $cols = {    # foreground background
316         txt  => [ '#000000', '#ffffff' ],
317         pins => [ '#ffffff', '#ff00ff' ],
318         vcc  => [ '#ff0000', '#ffff00' ],
319         gnd  => [ '#000000', '#00ffff' ],
320         i2c  => [ '#008800', '#ffcccc' ],
321         serial=>[ '#000088', '#ccffcc' ],
322         spi  => [ '#880000', '#ccccff' ],
323 };
324
325 sub swap_cols {
326         my $swap = shift;
327         die "$swap not found in ",dump($cols) unless $cols->{$swap};
328         my ( $c1, $c2 ) = @{ $cols->{$swap} };
329         $cols->{$swap} = [ $c2, $c1 ];
330 }
331
332 swap_cols 'txt' if $opt_invert;
333         
334
335 sub svg_style {
336         my ($name,$x,$y,$col) = @_;
337
338         return '' unless $opt_color;
339
340         $y -= $font_b; # shift box overlay to right vertical position based on font baseline
341
342         sub rect {
343                 my ($x,$y,$col,$fill) = @_;
344                 print qq{<rect x="$x" y="$y" height="2.54" width="}, $max_len[$col] * $font_w, qq{" style="fill:$fill;stroke:#ffffff;stroke-width:0.10" />\n};
345
346         }
347
348         if ( $name =~ m/^(\d+)$/ ) { # pins
349                 my $pin = $1;
350                 my ( $fg, $bg ) = @{ $cols->{pins} };
351                 if ( $pin == 1 ) {
352                         my $w  = $max_len[$col]*$font_w - 0.1;
353                         my $cx = $x + $w;
354                         my $cy = $y + 2.54;
355                         #print qq{<polygon points="$x,$y $cx,$y $x,$cy $x,$y" stroke="$fg" stroke-width="0.25" fill="$bg" />};
356                         #print qq{<polygon points="$x,$cy $cx,$cy $cx,$y $x,$cy" stroke="$bg" stroke-width="0.25" fill="$fg" />};
357                         print qq{<rect x="$x" y="$y" width="$w" height="2.54" stroke="$fg" stroke-width="0.3" fill="$bg" />};
358                         my ( $fg, $bg ) = @{ $cols->{txt} };
359                         print qq{<rect x="$x" y="$y" width="$w" height="2.54" rx="1" ry="1" stroke="$fg" stroke-width="0.3" fill="$bg" />};
360                 } else {
361                         rect $x,$y,$col,$fg;
362                 }
363                 return qq{ style="fill:$bg"};
364         }
365
366         if ( $name =~ m/(VCC|3V3|3.3V|5v)/i ) {
367                 my ($fg,$bg) = @{ $cols->{vcc} };
368                 rect $x,$y,$col,$bg;
369                 return qq{ style="fill:$fg"};
370         } elsif ( $name =~ m/(G(ND|Round)|VSS|0v)/i ) {
371                 my ($fg,$bg) = @{ $cols->{gnd} };
372                 rect $x,$y,$col,$bg;
373                 return qq{ style="fill:$fg"};
374         } elsif ( $name =~ m/\[(\w+)/ ) { # kernel
375                 my $dev = $1;
376                 my ($fg,$bg) = @{ $cols->{txt} };
377                 $dev = 'serial' if $dev =~ m/^tty/;
378                 ($fg,$bg) = @{ $cols->{$dev} } if exists $cols->{$dev};
379                 rect $x,$y,$col,$bg;
380                 return qq{ style="fill:$fg"};
381         } else {
382                 my ( $fg, $bg ) = @{ $cols->{txt} };
383                 rect $x,$y,$col,$bg;
384                 #return qq{ style="fill:$fg"};
385                 return '';
386         }
387 }
388
389 my $alt_col = 0;
390
391 my @cols_order = ( 0,1,2,3 );
392 my @cols_align = ( '','-','','-' ); # sprintf prefix
393
394 my @cols_shuffle = @cols_order;
395
396 if ( $opt_edge ) {
397         # pins outside on the right
398         @cols_shuffle = ( 0,1,3,2 ) if $opt_edge;
399         @cols_align = ( '-','-','','' );
400 } elsif ( $opt_middle ) {
401         # pins in middle
402         @cols_shuffle = ( 1,0,2,3 );
403         @cols_align = ( '','','-','-' );
404 }
405
406 sub cols_shuffle {
407         my ( $what, $order ) = @_;
408         my $new = [];
409         foreach my $i ( 0 .. $#$what ) {
410                 $new->[$i] = $what->[ $order->[$i] ];
411         }
412         warn "# cols_shuffle what=",dump($what)," order=",dump($order)," new=",dump($new);
413         return @$new;
414 }
415
416 @cols_order = cols_shuffle( \@cols_order, \@cols_shuffle );
417 @max_len    = cols_shuffle( \@max_len,    \@cols_shuffle );
418
419 warn "# cols_order = ",dump( \@cols_order );
420 warn "# cols_align = ",dump( \@cols_align );
421
422 my $fmt = "%$cols_align[0]$max_len[0]s %$cols_align[1]$max_len[1]s %$cols_align[2]$max_len[2]s %$cols_align[3]$max_len[3]s\n";
423
424
425 # cut marks
426 my ($fg,$bg) = @{ $cols->{txt} };
427 my $line_fmt = qq{<line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke:$fg;stroke-width:0.10;fill:$bg" />\n};
428
429 my @cut_marks;
430 sub cut_mark {
431         my ($x,$y) = @_;
432         return unless $opt_svg;
433         push @cut_marks, sprintf($line_fmt, $x-5, $y-$font_b,   $x+5, $y-$font_b);
434         push @cut_marks, sprintf($line_fmt, $x,   $y-$font_b-5, $x,   $y-$font_b+5);
435 }
436 #cut_mark $x, $y;
437 my $max_x = $x;
438 $max_x += $max_len[$_] * $font_w foreach ( 0 .. 3 );
439 #cut_mark $max_x, $y;
440
441 sub line {
442         my ($x,$y,$max_x) = @_;
443         push @cut_marks, sprintf($line_fmt, $x, $y-$font_b, $max_x, $y-$font_b);
444 }
445
446
447 my $last_cut_mark = 0;
448
449 sub connector {
450         my ( $from, $to ) = @_;
451         warn "# connector $from - $to ",dump( $line_parts[$from], $line_parts[$to] );
452         if ( $opt_vertical ) {
453                 foreach my $i ( 0 .. int(($to-$from)/2) ) {
454                         my $t = $line_parts[$from + $i];
455                                 $line_parts[$from + $i] = $line_parts[$to - $i];
456                                                           $line_parts[$to - $i] = $t;
457                 }
458         }
459 }
460
461 my $from;
462 my $to;
463 foreach my $i ( 0 .. $#line_parts ) {
464         next if $line_parts[$i]->[0] =~ m/^###/;
465         if (exists $line_parts[$i]->[1]) {
466                 if (! $from) {
467                         $from = $i;
468                 } else {
469                         $to = $i;
470                 }
471         } elsif ($from && $to) {
472                 connector $from => $to;
473                 $from = $to = undef;
474         }
475 }
476 connector $from => $to if $from && $to;
477
478 foreach my $i ( 0 .. $#line_parts ) {
479 #       $i = $#line_parts - $i if $opt_vertical;
480         my $line = $line_parts[$i];
481
482         if ( $opt_svg ) {
483
484                 # not a minimal two column pin description
485                 if ( ! exists $line->[1] ) {
486                         $last_cut_mark = 1 if $line->[0] =~ m/^##/; # skip comments
487
488                         # before first empty line
489                         if ( $last_cut_mark == 0 ) {
490                                 cut_mark $x, $y;
491                                 cut_mark $max_x, $y;
492                                 $last_cut_mark = 1;
493                                 line $x, $y, $max_x if $opt_lines;
494                                 $y += 15; # make spacing between pinouts
495                         }
496                 } elsif ( $last_cut_mark ) {
497                         # first full line
498                         cut_mark $x, $y;
499                         cut_mark $max_x, $y;
500                         $last_cut_mark = 0;
501                 } else {
502                         #warn "CUTMARK no magic";
503                 }
504
505                 line $x, $y, $max_x if $opt_lines && exists $line->[1];
506
507                 my ($fg,$bg) = @{ $cols->{txt} };
508                 my $tspan = qq{<tspan x="$x" y="$y" style="line-height:2.54;fill:$fg;stroke:none;">\n};
509
510                 my $x_pos = $x;
511                 foreach my $i ( 0 .. $#cols_order ) {
512                         my $order = $cols_order[$i];
513                         next unless $line->[$order];
514
515                         my $text_anchor = 'middle';
516                         my $len = $max_len[$i];
517                         my $x2 = $x_pos + ( $len * $font_w ) / 2;
518                         # is this comment?
519                         if ( $#$line == 0 && $line->[$order] =~ s/^#+s*// ) {
520                                 # comment, center over whole width
521                                 $len = length($line->[$order]);
522                                 $x2 = $x + (($max_x-$x)/2); # middle
523                                 $tspan .= qq{\t<tspan x="$x2" text-anchor="$text_anchor"}.sprintf( '>%' . $cols_align[$i] . $len . 's</tspan>', $line->[0]);
524                         } else {
525                                 $tspan .= qq{\t<tspan x="$x2" text-anchor="$text_anchor"}.svg_style($line->[$order],$x_pos,$y,$i).sprintf( '>%' . $cols_align[$i] . $len . 's</tspan>', $line->[$order]);
526                         }
527                         $x_pos += $len * $font_w;
528                 }
529
530                 $tspan .= qq{\n</tspan>\n};
531                 push @later,sprintf $tspan, @$line;
532                 $y += 2.54;
533
534                 # swap pin colors for line stripe
535                 if ( $opt_zebra ) {
536                         swap_cols $_ foreach qw( pins txt );
537                 } else {
538                         swap_cols 'pins';
539                 }
540
541         } else {
542
543                 if ( $#$line == 0 ) {
544                         print $line->[0], "\n";
545                 } else {
546                         push @$line, '' while ($#$line < 3); # fill-in single row header
547                         printf $fmt, map { $line->[$_] } @cols_order;
548                 }
549
550         }
551 }
552
553 if ( $opt_svg ) {
554         cut_mark $x,$y;
555         cut_mark $max_x,$y;
556         line $x, $y, $max_x if $opt_lines;
557
558         print qq{
559     <text
560        id="text4506"
561        y="$x"
562        x="$y"
563        style="font-size:2.34px;line-height:2.54px;font-family:'Andale Mono';stroke:none"
564        xml:space="preserve">
565
566         }; #svg
567
568         print @later, qq{</text>\n}, @cut_marks, qq{</g>\n</svg>};
569
570 }
571
572 # you can add pin definitions below, but they should go into pins/
573 __DATA__
574