si5338_register_map_dts.py 3.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#!/usr/bin/env python
#***************************************************************************
# FILE NAME  : si5338_register_map_dts.py
# DESCRIPTION: convert si5338 register map file generated by Silicon Labs
# ClockBuilder(tm) Desktop Software into a device tree fragment, compatible
# with drivers/misc/si5338.c
# AUTHOR: Oleg Dzhimiev <oleg@elphel.com>
# Copyright (C) 2013 Elphel, Inc
# -----------------------------------------------------------------------------**
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  The four essential freedoms with GNU GPL software:
#  * to run the program for any purpose
#  * to study how the program works and change it to make it do what you wish
#  * to redistribute copies so you can help your neighbor
#  * to distribute copies of your modified versions to others
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------**

__author__ = "Oleg Dzhimiev"
__copyright__ = "Copyright 2013, Elphel, Inc."
__license__ = "GPL"
__version__ = "3.0+"
__maintainer__ = "Oleg Dzhimiev"
__email__ = "oleg@elphel.com"
__status__ = "Development"

import sys

try:
    File = sys.argv[1]
except IndexError:
    print '''
Usage - terminal:
  a) ./register_map_dts.py <file>
  b) python register_map_dts.py <file>
  where <file> is a *.h generated by Si5338 ClockBuilder
Example:
  ./register_map_dts.py register_map.h
Output:
  <file>.dts
'''
    sys.exit()

dts_record_start = '''ps7_axi_interconnect_0: amba@0 {
	ps7_i2c_0: ps7-i2c@e0004000 {
		si5338@70 {
			compatible = "sil,si5338";
			reg = <0x70>;
			si5338,init="always"; /* initialize PLL, wait for lock. Other option is 'if off'*/
			si5338,configuration_data=<
			'''
dts_record_end = '''
			>;
		};
	};
};
'''

class RegisterMapParser:
    def __init__(self,File):
	self.File = File

    def generate_dts(self):
        print "Parsing "+self.File+".\n"
        i,page,tmpstr = 0,0,""
        with open(self.File,'r') as f_r:
            for line in f_r:
                if line[0]=='{':
#                    i+=1
                    line = line[line.find('{')+1:line.find('}')]
                    values = line.split(',')
                    if values[0]=='255':
                        page = int(values[1],16)
                    else:
                        if int(values[2],16) != 0 :
                            result = hex(((256*page+int(values[0]))<<16)+(int(values[1],16)<<8)+int(values[2],16))
                            tmpstr += result+" "
                            i+=1
                            if i%8==0 : tmpstr += "\n\t\t\t"
        return tmpstr

Parser = RegisterMapParser(File)

with open(File+".dts",'w') as f_w:
  f_w.write(dts_record_start+Parser.generate_dts()+dts_record_end)