c2af8ac9cebc683c6eba655ca1ec33bfa5f92a2e
[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 my $have_sunxi_pio = `which sunxi-pio`;
164 if ( $have_sunxi_pio ) {
165
166 open(my $pio, '-|', 'sunxi-pio -m print');
167 while(<$pio>) {
168         chomp;
169         s/[<>]+/ /g;
170         my @p = split(/\s+/,$_);
171         warn "# pio ",dump(\@p);
172         # annotate input 0 and output 1 pins
173 #       annotate_pin $p[0], ( $p[1] ? 'O' : 'I' ) . ':' . $p[4] if $p[1] == 0 || $p[1] == 1;
174         my $pin = shift @p;
175         annotate_pin $pin, join(' ',@p) if ! $opt_svg;
176 }
177 close($pio);
178
179 } # have_sunxi_pio
180
181
182 my $pinmux;
183 my $pinmux_path = (glob("/sys/kernel/debug/pinctrl/*/pinmux-functions"))[0];
184 if ( $opt_pinmux && -e $pinmux_path ) {
185         open(my $mux, '<', $pinmux_path);
186         while(<$mux>) {
187                 chomp;
188                 if ( m/function: (\w+), groups = \[ (.*) \]/ ) {
189                         my ( $func, $pins ) = ( $1, $2 );
190                         foreach ( split(/\s+/,$pins) ) {
191                                 push @{ $pinmux->{$_} }, $func;
192                         }
193                 } else {
194                         warn "IGNORED [$pinmux_path] [$_]\n";
195                 }
196         }
197
198         foreach my $pin ( keys %$pinmux ) {
199                 if ( exists $pins->{$pin} ) {
200                         annotate_pin $pin, '{' . join(' ', @{$pinmux->{$pin}}) . '}';
201                 } else {
202                         warn "IGNORED mux on $pin\n";
203                 }
204         }
205
206         warn "# pinmux = ",dump( $pinmux );
207 }
208
209
210
211 my @max_len = ( 0,0,0,0 );
212 my @line_parts;
213
214 shift(@lines) while ( ! $lines[0] );    # remove empty at beginning
215 pop(@lines) while ( ! $lines[-1] );     # remove empty at end
216
217 foreach my $line (@lines) {
218         if ( $line =~ m/^#/ ) {
219                 push @line_parts, [ $line ] unless $opt_svg && $line =~ m/^###+/; # SVG doesn't display 3rd level comments
220                 next;
221         }
222         $line =~ s/\[(\w+)\s+(\w+)\] \[\1\s+(\w+)\]/[$1 $2 $3]/g; # compress kernel annotation with same prefix
223         $line =~ s/(\[(?:uart\d*|serial|tty\w+))([^\t]*\]\s[^\t]*(rx|tx)d?)/$1 $3$2/gi;
224         $line =~ s/(\[i2c)([^\t]*\]\s[^\t]*(scl?k?|sda))/$1 $3$2/gi;
225         $line =~ s/(\[spi)([^\t]*\]\s[^\t]*(miso|mosi|s?clk|c[se]\d*))/$1 $3$2/gi;
226         $line =~ s/\s*\([^\)]+\)//g if ! $opt_alt;
227
228         # shorten duplicate kernel device/function
229         $line =~ s/\[serial (\w+) (uart\d+)\]/[$2 $1]/g;
230         $line =~ s/\[(\w+) (\w+) \1(\d+)\]/[$1$3 $2]/g;
231
232         $line =~ s/\[(\w+)\s+([^\]]+)\s+\1\]/[$1 $2]/g; # duplicate
233
234         my @v = split(/\s*\t+\s*/,$line,4);
235         @v = ( $v[2], $v[3], $v[0], $v[1] ) if $opt_horizontal && $v[2];
236
237         push @line_parts, [ @v ];
238         foreach my $i ( 0 .. 3 ) {
239                 next unless exists $v[$i];
240                 next if $v[$i] =~ m/^#/; # don't calculate comments into max length
241                 my $l = length($v[$i]);
242                 $max_len[$i] = $l if $l > $max_len[$i];
243         }
244 }
245
246 warn "# max_len = ",dump( \@max_len );
247 warn "# line_parts = ",dump( \@line_parts );
248
249 #print "$_\n" foreach @lines;
250
251 my $x = 20.00; # mm
252 my $y = 20.00; # mm
253
254 if ( $opt_svg ) {
255         print qq{<?xml version="1.0" encoding="UTF-8" standalone="no"?>
256 <svg
257    xmlns:dc="http://purl.org/dc/elements/1.1/"
258    xmlns:cc="http://creativecommons.org/ns#"
259    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
260    xmlns:svg="http://www.w3.org/2000/svg"
261    xmlns="http://www.w3.org/2000/svg"
262    xmlns:xlink="http://www.w3.org/1999/xlink"
263    id="svg8"
264    version="1.1"
265    viewBox="0 0 210 297"
266    height="297mm"
267    width="210mm">
268
269
270 <g id="layer1">
271
272         }; # svg, insert rest of rect
273
274         print qq{<rect x="0" y="0" width="210" height="297" style="fill:#000000" id="high-contrast"/>} if $opt_invert;
275 }
276
277 my @later;
278
279 my $cols = {    # foreground background
280         txt  => [ '#000000', '#ffffff' ],
281         pins => [ '#ffffff', '#ff00ff' ],
282         vcc  => [ '#ff0000', '#ffff00' ],
283         gnd  => [ '#000000', '#00ffff' ],
284         i2c  => [ '#008800', '#ffcccc' ],
285         serial=>[ '#000088', '#ccffcc' ],
286         spi  => [ '#880000', '#ccccff' ],
287 };
288
289 sub swap_cols {
290         my $swap = shift;
291         die "$swap not found in ",dump($cols) unless $cols->{$swap};
292         my ( $c1, $c2 ) = @{ $cols->{$swap} };
293         $cols->{$swap} = [ $c2, $c1 ];
294 }
295
296 swap_cols 'txt' if $opt_invert;
297         
298
299 sub svg_style {
300         my ($name,$x,$y,$col) = @_;
301
302         return '' unless $opt_color;
303
304         $y -= $font_b; # shift box overlay to right vertical position based on font baseline
305
306         sub rect {
307                 my ($x,$y,$col,$fill) = @_;
308                 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};
309
310         }
311
312         if ( $name =~ m/^(\d+)$/ ) { # pins
313                 my $pin = $1;
314                 my ( $fg, $bg ) = @{ $cols->{pins} };
315                 if ( $pin == 1 ) {
316                         my $w  = $max_len[$col]*$font_w - 0.1;
317                         my $cx = $x + $w;
318                         my $cy = $y + 2.54;
319                         #print qq{<polygon points="$x,$y $cx,$y $x,$cy $x,$y" stroke="$fg" stroke-width="0.25" fill="$bg" />};
320                         #print qq{<polygon points="$x,$cy $cx,$cy $cx,$y $x,$cy" stroke="$bg" stroke-width="0.25" fill="$fg" />};
321                         print qq{<rect x="$x" y="$y" width="$w" height="2.54" stroke="$fg" stroke-width="0.3" fill="$bg" />};
322                         my ( $fg, $bg ) = @{ $cols->{txt} };
323                         print qq{<rect x="$x" y="$y" width="$w" height="2.54" rx="1" ry="1" stroke="$fg" stroke-width="0.3" fill="$bg" />};
324                 } else {
325                         rect $x,$y,$col,$fg;
326                 }
327                 return qq{ style="fill:$bg"};
328         }
329
330         if ( $name =~ m/(VCC|3V3|3.3V|5v)/i ) {
331                 my ($fg,$bg) = @{ $cols->{vcc} };
332                 rect $x,$y,$col,$bg;
333                 return qq{ style="fill:$fg"};
334         } elsif ( $name =~ m/(G(ND|Round)|VSS|0v)/i ) {
335                 my ($fg,$bg) = @{ $cols->{gnd} };
336                 rect $x,$y,$col,$bg;
337                 return qq{ style="fill:$fg"};
338         } elsif ( $name =~ m/\[(\w+)/ ) { # kernel
339                 my $dev = $1;
340                 my ($fg,$bg) = @{ $cols->{txt} };
341                 $dev = 'serial' if $dev =~ m/^tty/;
342                 ($fg,$bg) = @{ $cols->{$dev} } if exists $cols->{$dev};
343                 rect $x,$y,$col,$bg;
344                 return qq{ style="fill:$fg"};
345         } else {
346                 my ( $fg, $bg ) = @{ $cols->{txt} };
347                 rect $x,$y,$col,$bg;
348                 #return qq{ style="fill:$fg"};
349                 return '';
350         }
351 }
352
353 my $alt_col = 0;
354
355 my @cols_order = ( 0,1,2,3 );
356 my @cols_align = ( '','-','','-' ); # sprintf prefix
357
358 my @cols_shuffle = @cols_order;
359
360 if ( $opt_edge ) {
361         # pins outside on the right
362         @cols_shuffle = ( 0,1,3,2 ) if $opt_edge;
363         @cols_align = ( '-','-','','' );
364 } elsif ( $opt_middle ) {
365         # pins in middle
366         @cols_shuffle = ( 1,0,2,3 );
367         @cols_align = ( '','','-','-' );
368 }
369
370 sub cols_shuffle {
371         my ( $what, $order ) = @_;
372         my $new = [];
373         foreach my $i ( 0 .. $#$what ) {
374                 $new->[$i] = $what->[ $order->[$i] ];
375         }
376         warn "# cols_shuffle what=",dump($what)," order=",dump($order)," new=",dump($new);
377         return @$new;
378 }
379
380 @cols_order = cols_shuffle( \@cols_order, \@cols_shuffle );
381 @max_len    = cols_shuffle( \@max_len,    \@cols_shuffle );
382
383 warn "# cols_order = ",dump( \@cols_order );
384 warn "# cols_align = ",dump( \@cols_align );
385
386 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";
387
388
389 # cut marks
390 my ($fg,$bg) = @{ $cols->{txt} };
391 my $line_fmt = qq{<line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke:$fg;stroke-width:0.10;fill:$bg" />\n};
392
393 my @cut_marks;
394 sub cut_mark {
395         my ($x,$y) = @_;
396         return unless $opt_svg;
397         push @cut_marks, sprintf($line_fmt, $x-5, $y-$font_b,   $x+5, $y-$font_b);
398         push @cut_marks, sprintf($line_fmt, $x,   $y-$font_b-5, $x,   $y-$font_b+5);
399 }
400 #cut_mark $x, $y;
401 my $max_x = $x;
402 $max_x += $max_len[$_] * $font_w foreach ( 0 .. 3 );
403 #cut_mark $max_x, $y;
404
405 sub line {
406         my ($x,$y,$max_x) = @_;
407         push @cut_marks, sprintf($line_fmt, $x, $y-$font_b, $max_x, $y-$font_b);
408 }
409
410
411 my $last_cut_mark = 0;
412
413 sub connector {
414         my ( $from, $to ) = @_;
415         warn "# connector $from - $to ",dump( $line_parts[$from], $line_parts[$to] );
416         if ( $opt_vertical ) {
417                 foreach my $i ( 0 .. int(($to-$from)/2) ) {
418                         my $t = $line_parts[$from + $i];
419                                 $line_parts[$from + $i] = $line_parts[$to - $i];
420                                                           $line_parts[$to - $i] = $t;
421                 }
422         }
423 }
424
425 my $from;
426 my $to;
427 foreach my $i ( 0 .. $#line_parts ) {
428         next if $line_parts[$i]->[0] =~ m/^###/;
429         if (exists $line_parts[$i]->[1]) {
430                 if (! $from) {
431                         $from = $i;
432                 } else {
433                         $to = $i;
434                 }
435         } elsif ($from && $to) {
436                 connector $from => $to;
437                 $from = $to = undef;
438         }
439 }
440 connector $from => $to if $from && $to;
441
442 foreach my $i ( 0 .. $#line_parts ) {
443 #       $i = $#line_parts - $i if $opt_vertical;
444         my $line = $line_parts[$i];
445
446         if ( $opt_svg ) {
447
448                 # not a minimal two column pin description
449                 if ( ! exists $line->[1] ) {
450                         $last_cut_mark = 1 if $line->[0] =~ m/^##/; # skip comments
451
452                         # before first empty line
453                         if ( $last_cut_mark == 0 ) {
454                                 cut_mark $x, $y;
455                                 cut_mark $max_x, $y;
456                                 $last_cut_mark = 1;
457                                 line $x, $y, $max_x if $opt_lines;
458                                 $y += 15; # make spacing between pinouts
459                         }
460                 } elsif ( $last_cut_mark ) {
461                         # first full line
462                         cut_mark $x, $y;
463                         cut_mark $max_x, $y;
464                         $last_cut_mark = 0;
465                 } else {
466                         #warn "CUTMARK no magic";
467                 }
468
469                 line $x, $y, $max_x if $opt_lines && exists $line->[1];
470
471                 my ($fg,$bg) = @{ $cols->{txt} };
472                 my $tspan = qq{<tspan x="$x" y="$y" style="line-height:2.54;fill:$fg;stroke:none;">\n};
473
474                 my $x_pos = $x;
475                 foreach my $i ( 0 .. $#cols_order ) {
476                         my $order = $cols_order[$i];
477                         next unless $line->[$order];
478
479                         my $text_anchor = 'middle';
480                         my $len = $max_len[$i];
481                         my $x2 = $x_pos + ( $len * $font_w ) / 2;
482                         # is this comment?
483                         if ( $#$line == 0 && $line->[$order] =~ s/^#+s*// ) {
484                                 # comment, center over whole width
485                                 $len = length($line->[$order]);
486                                 $x2 = $x + (($max_x-$x)/2); # middle
487                                 $tspan .= qq{\t<tspan x="$x2" text-anchor="$text_anchor"}.sprintf( '>%' . $cols_align[$i] . $len . 's</tspan>', $line->[0]);
488                         } else {
489                                 $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]);
490                         }
491                         $x_pos += $len * $font_w;
492                 }
493
494                 $tspan .= qq{\n</tspan>\n};
495                 push @later,sprintf $tspan, @$line;
496                 $y += 2.54;
497
498                 # swap pin colors for line stripe
499                 if ( $opt_zebra ) {
500                         swap_cols $_ foreach qw( pins txt );
501                 } else {
502                         swap_cols 'pins';
503                 }
504
505         } else {
506
507                 if ( $#$line == 0 ) {
508                         print $line->[0], "\n";
509                 } else {
510                         push @$line, '' while ($#$line < 3); # fill-in single row header
511                         printf $fmt, map { $line->[$_] } @cols_order;
512                 }
513
514         }
515 }
516
517 if ( $opt_svg ) {
518         cut_mark $x,$y;
519         cut_mark $max_x,$y;
520         line $x, $y, $max_x if $opt_lines;
521
522         print qq{
523     <text
524        id="text4506"
525        y="$x"
526        x="$y"
527        style="font-size:2.34px;line-height:2.54px;font-family:'Andale Mono';stroke:none"
528        xml:space="preserve">
529
530         }; #svg
531
532         print @later, qq{</text>\n}, @cut_marks, qq{</g>\n</svg>};
533
534 }
535
536 # you can add pin definitions below, but they should go into pins/
537 __DATA__
538