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