[OpenWrt-Devel] [PATCH] ath79: use gpio_hog instead of gpio-export

Birger Koblitz mail at birger-koblitz.de
Sun Aug 11 16:06:00 EDT 2019


Hi Adrian,

On 11.08.19 21:15, mail at adrianschmutzler.de wrote:
>> -----Original Message-----
>> From: openwrt-devel [mailto:openwrt-devel-bounces at lists.openwrt.org]
>> On Behalf Of Birger Koblitz
>> Sent: Sonntag, 11. August 2019 13:11
>> To: mail at adrianschmutzler.de
>> Cc: 'OpenWrt Development List' <openwrt-devel at lists.openwrt.org>
>> Subject: Re: [OpenWrt-Devel] [PATCH] ath79: use gpio_hog instead of gpio-
>> export
>>
>> Dear Adrian,
>>
>> I'll resubmit a patch taking your comments into account. I am using a script
>> that parses the DTS ...
> So that means that duplicate &gpio is also treated with automatically (as I've seen with some devices)?

The script is able to catch some cases, others are not so easy. I
believe there is a case where the original .dts already has duplicates.

Also the idea was to keep the sequence of the gpios definitions in the
original .dts.

If the original dts moves from chip 0 to chip 1 and back to chip 0 with
the definition of gpios being exported, then this will give duplicates
for gpio0 at the moment.

I could sort the gpios so that it is guaranteed that there are no
duplicate gpio definitions but this would destroy the original authors
intention as to which gpio belongs to which other one. 

>
>> This should also prevent the double naming of the nodes. I am actually
>> surprised the DTS compiler did not complain... Things like
>>
>> -    compatible = "yuncore,a770", "qca,qca9531";
>> +    compatible = "yuncore,a770", "qca,qca9533";
>>
>> are probably due to trailing white-space in the original, I'll stop the script from
>> touching that.
> Well, in this particular case it was not only whitespace, but the qca changing from 9531 to 9533...

Ouch, this was an accident with commit
5e9086b7b11976ba75014b257139ddc4b2885a8a

don't know how I messed that up...

>
>
> Adrian

Thanks for the comments,

  Birger


As an afterthought, here comes the current script, I know that it is
ugly, but it shows what is done and how:

#!/usr/bin/ruby
unless ARGV.length == 1
  puts "Usage: convertHog.rb <input.dts>"
  exit
end

line_num = 0
in_root = 0
in_export = false
in_node = false
in_comment = false
last_empty = false
in_node_comment = false
node = -1
node_name = Array.new
node_output = Array.new
node_is_output = Array.new
node_chip = Array.new
node_num = Array.new
node_active = Array.new
node_comment = Array.new
node_prefix = Array.new
gpio_found = Array.new
gpio_stack = ""
remove_next_empty = false
DEBUG = false

f = File.open(ARGV[0])
f.each do |line|
  line_num += 1
  if line.match(/\/\s*\{/) then
    print "Found beginning\n" if DEBUG
    in_root += 1
    print "\n" if last_empty
    print "#{line}"
    last_empty = false
    next
  end
 
  if line.match(/{/) then
    in_root +=1
  end
 
  if line.match(/}/) then
    in_root -=1
  end
 
  if line.match(/^\s*$/) then
    print "last empty\n" if DEBUG
    if not remove_next_empty then
      last_empty = true
      next
    end
  end
   
 
  print "#{line_num} root #{in_root} " if DEBUG
 
  if (in_root > 0) and line.match(/gpio[_\-]export\s*\{/) then
    print "Found export\n" if DEBUG
    in_export = true
    next
  end
 
  if in_export then
    last_empty = false
   
    if not in_node then
      if line.match(/^\s*\/\*\s*$/) then
        in_comment = true;
      end
      if line.match(/^\s*\*\/\s*$/) then
        in_comment = false;
        print "#{line}"
        next
      end
    end
   
    if in_comment then
      print "#{line}"
      next
    end
   
    if line.match(/\w+\s*\{/)
      print "Found node\n" if DEBUG
      in_node = true;
      node += 1
    end
 
    if line.match(/\}\s*;/) then
      if in_node
        in_node = false
      else
        in_export = false
      end
    end
   
    if in_node then
      line.match(/gpio\-export,name\s*=\s*"([\w\d\-:]+:)?([\w\d\-]+)"/)
{ |m|
        if m.length == 2 then
          print "Found export #{m[1]}\n" if DEBUG
          node_name[node] = m[1]
        else
          print "Found export #{m[2]}\n"  if DEBUG
          node_name[node] = m[2]
          node_prefix[node] = m[1]
        end
      }
      line.match(/gpio\-export,output\s*=\s*<(\d)>\s*;/) { |m|
        print "Found output #{m[1]}\n" if DEBUG
        node_output[node] = m[1]
        node_is_output[node] = true
      }
     
line.match(/gpios\s*\=\s*\<\&gpio(\d*)\s+(\d+)\s+GPIO_ACTIVE_(\w+)\s*\>\s*;/i)
{ |m|
        print "Found gpio #{m[1]}, #{m[2]}, #{m[3]}\n" if DEBUG
        node_chip[node] = m[1]
        node_num[node] = m[2]
        node_active[node] = m[3]
        gpio_found[m[1].to_i] = true
      }
      line.match(/\/\*(.*)\*\//){ |m|
        print "Found comment #{m[1]}\n"
        node_comment[node] = m[0]
      }
      if line.match(/^\s*\/\*\s*$/) then
        print "In Node COMMENT\n" if DEBUG
        in_node_comment = true;
        node_comment[node] = ""
      end
      if line.match(/^\s*\*\/\s*$/) then
        print "End Node COMMENT\n" if DEBUG
        node_comment[node] += line
        in_node_comment = false;
      end
      node_comment[node] += line if in_node_comment
     
    end
    next
  else
    print "Outside root " if DEBUG
    line.match(/^\&gpio(\d*).*\{/) { |m|
        print "Found gpio #{m[1]}\n" if DEBUG
        if gpio_found[m[1].to_i] then
          gpio_stack = line
          print "stacking\n" if DEBUG
          line = ""
          remove_next_empty = true
          next
        end
    }
    print "Here\n" if DEBUG
    if gpio_stack != "" then
      if line.match(/status\s*=\s*"okay"/) then
        next
      end
      if line.match(/^\};/) then
        line = "\n" + gpio_stack + line
        if not gpio_stack.match(/;/) then
          line = ""
          print "more\n" if DEBUG
        end
        gpio_stack = ""
      else
        gpio_stack += line
        next
      end
    end 
  end
 
  print "#{line_num} " if DEBUG
  print "\n" if last_empty and not remove_next_empty
  print "#{line}"
  last_empty = false
  remove_next_empty = false
 
  if in_root == 0 then
    last_chip = -1
   
    i = 0
    while i <= node
      if node_chip[i] != last_chip then
        print "\n&gpio#{node_chip[i]} {\n"
        print "\tstatus = \"okay\";\n\n"
      else
        print "\n"
      end
      print "\t#{node_name[i]} {\n"
      comment = node_comment[i]
      if node_comment[i] then
        comment.gsub!(/^\/\*/, "\t\t/*")
        comment.gsub!(/\t\t\t/, "\t\t")
        print "#{comment}\n"
      end
      print "\t\tgpio-hog;\n"
      if node_prefix[i] then
        print "\t\tline-name = \"#{node_prefix[i]}#{node_name[i]}\";\n";
      else
        print "\t\tline-name = \"#{node_name[i]}\";\n";
      end
      print "\t\tgpios = <#{node_num[i]} GPIO_ACTIVE_#{node_active[i]}>;\n"
      if node_is_output[i] then
        if node_output[i].to_i == 1 then
          print "\t\toutput-high;\n"
        else
          print "\t\toutput-low;\n"
        end
      else
        print "\t\tinput;\n"
      end
      print "\t};\n"
      if i == node or node_chip[i] != node_chip[i+1] then
        print "};\n"
      end
      last_chip = node_chip[i]
      i = i+1
    end
    node = -1
  end   
end


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list