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