xref: /openbmc/skeleton/pytools/gpioutil (revision 7e48038a)
1#!/usr/bin/env python
2
3import sys
4import os
5import getopt
6from glob import glob
7from os.path import join
8import obmc_system_config as System
9
10
11def printUsage():
12	print '\nUsage:'
13	print 'gpioutil -n GPIO_NAME  [-v value]'
14	print 'gpioutil -i GPIO_NUM  -d <DIRECTION = in,out,falling,rising,both> [-v value]'
15	print 'gpioutil -p PIN_NAME  -d <DIRECTION = in,out,falling,rising,both> [-v value]'
16	print 'gpioutil -l PIN_NAME     (lookup pin name only)'
17	exit(1)
18
19
20
21if (len(sys.argv) < 2):
22	printUsage()
23
24# Pop the command name and point to the args
25sys.argv.pop(0)
26
27GPIO_SYSFS = '/sys/class/gpio/'
28
29
30def find_gpio_base(path="/sys/class/gpio/"):
31	pattern = "gpiochip*"
32	for gc in glob(join(path, pattern)):
33		with open(join(gc, "label")) as f:
34			label = f.readline().strip()
35		if label == "1e780000.gpio":
36			with open(join(gc, "base")) as f:
37				return int(f.readline().strip())
38	# trigger a file not found exception
39	open(join(path, "gpiochip"))
40
41
42GPIO_BASE = find_gpio_base()
43
44
45def convertGpio(name):
46	offset = int(''.join(list(filter(str.isdigit, name))))
47	port = list(filter(str.isalpha, name.upper()))
48	a = ord(port[-1]) - ord('A')
49	if len(port) > 1:
50		a += 26
51	base = a * 8 + GPIO_BASE
52	return base + offset
53
54
55class Gpio:
56	def __init__(self,gpio_num):
57		self.gpio_num = str(gpio_num)
58		self.direction = ''
59		self.interrupt = ''
60		self.exported = False
61
62	def getPath(self,name):
63		return GPIO_SYSFS+'gpio'+self.gpio_num+'/'+name
64
65	def export(self):
66		if (os.path.exists(GPIO_SYSFS+'export') == False):
67			raise Exception("ERROR - GPIO_SYSFS path does not exist.  Does this platform support GPIOS?")
68		if (os.path.exists(GPIO_SYSFS+'gpio'+self.gpio_num) == False):
69			self.write(GPIO_SYSFS+'export',self.gpio_num)
70
71		self.exported = True
72
73	def setDirection(self,dir):
74		if (self.exported == False):
75			raise Exception("ERROR - Not exported: "+self.getPath())
76
77		self.direction = ''
78		self.interrupt = ''
79		if (dir == 'in' or dir == 'out'):
80			self.direction = dir
81		elif (dir == 'rising' or
82		      dir == 'falling' or
83		      dir == 'both'):
84			self.direction = 'in'
85			self.interrupt = dir
86			self.write(self.getPath('edge'),self.interrupt)
87		else:
88			raise Exception("ERROR - Invalid Direction: "+dir)
89
90		current_direction = self.read(self.getPath('direction'))
91		if current_direction != self.direction:
92			self.write(self.getPath('direction'),self.direction)
93
94	def setValue(self,value):
95		if (value == '0'):
96			self.write(self.getPath('value'),'0')
97		elif (value == '1'):
98			self.write(self.getPath('value'),'1')
99		else:
100			raise Exception("ERROR - Invalid value: "+value)
101
102	def getValue(self):
103		return self.read(self.getPath('value'))
104
105	def write(self,path,data):
106		f = open(path,'w')
107		f.write(data)
108		f.close()
109
110
111	def read(self,path):
112		f = open(path,'r')
113		data = f.readline().strip()
114		f.close()
115		return data
116
117
118
119if __name__ == '__main__':
120
121	try:
122		opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:")
123 	except getopt.GetoptError:
124 		printUsage()
125
126
127
128	lookup = False
129	gpio_name = ""
130	value = ""
131	direction = ""
132	for opt, arg in opts:
133 		if opt == '-h':
134			printUsage()
135
136 		elif opt in ("-n"):
137 			gpio_name = arg
138			lookup = True
139 		elif opt in ("-i"):
140 			gpio_name = arg
141 		elif opt in ("-d"):
142			direction = arg
143 		elif opt in ("-v"):
144			value = arg
145		elif opt in ("-p"):
146			gpio_name = convertGpio(arg)
147		elif opt in ("-l"):
148			gpio_name = convertGpio(arg)
149			print gpio_name
150			exit(0)
151
152	gpio_info = {}
153	if (lookup == True):
154		if (System.GPIO_CONFIG.has_key(gpio_name) == False):
155			print "ERROR - GPIO Name doesn't exist"
156			print "Valid names: "
157			for n in System.GPIO_CONFIG:
158				print "\t"+n
159			exit(0)
160		gpio_info = System.GPIO_CONFIG[gpio_name]
161		direction = gpio_info['direction']
162		if (gpio_info.has_key('gpio_num')):
163			gpio_name = str(gpio_info['gpio_num'])
164		else:
165			gpio_name = str(convertGpio(gpio_info['gpio_pin']))
166		print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction
167
168
169	## Rules
170	if (gpio_name == ""):
171		print "ERROR - Gpio not specified"
172		printUsage()
173
174	if (direction == "in" and value != ""):
175		print "ERROR - Value cannot be specified when direction = in"
176		printUsage()
177
178	gpio = Gpio(gpio_name)
179	try:
180		gpio.export()
181		if (direction != ""):
182			gpio.setDirection(direction)
183
184		if (value == ""):
185			print gpio.getValue()
186		else:
187			gpio.setValue(value)
188
189	except Exception as e:
190		print e
191
192