xref: /openbmc/skeleton/pytools/gpioutil (revision 3045be31)
140a360c2SBrad Bishop#!/usr/bin/env python
240a360c2SBrad Bishop
340a360c2SBrad Bishopimport sys
440a360c2SBrad Bishopimport os
540a360c2SBrad Bishopimport getopt
60b380f7bSBrad Bishopimport obmc_system_config as System
7a7ac805bSBrad Bishopimport obmc.system
840a360c2SBrad Bishop
940a360c2SBrad Bishop
1040a360c2SBrad Bishopdef printUsage():
1140a360c2SBrad Bishop	print '\nUsage:'
120b380f7bSBrad Bishop	print 'gpioutil -n GPIO_NAME  [-v value]'
130b380f7bSBrad Bishop	print 'gpioutil -i GPIO_NUM  -d <DIRECTION = in,out,falling,rising,both> [-v value]'
140b380f7bSBrad Bishop	print 'gpioutil -p PIN_NAME  -d <DIRECTION = in,out,falling,rising,both> [-v value]'
150b380f7bSBrad Bishop	print 'gpioutil -l PIN_NAME     (lookup pin name only)'
1640a360c2SBrad Bishop	exit(1)
1740a360c2SBrad Bishop
1840a360c2SBrad Bishop
1940a360c2SBrad Bishop
200b380f7bSBrad Bishopif (len(sys.argv) < 2):
2140a360c2SBrad Bishop	printUsage()
2240a360c2SBrad Bishop
23ff47af29SAdriana Kobylak# Pop the command name and point to the args
24ff47af29SAdriana Kobylaksys.argv.pop(0)
25ff47af29SAdriana Kobylak
2640a360c2SBrad BishopGPIO_SYSFS = '/sys/class/gpio/'
2740a360c2SBrad Bishop
2840a360c2SBrad Bishopclass Gpio:
2940a360c2SBrad Bishop	def __init__(self,gpio_num):
3040a360c2SBrad Bishop		self.gpio_num = str(gpio_num)
3140a360c2SBrad Bishop		self.direction = ''
3240a360c2SBrad Bishop		self.interrupt = ''
3340a360c2SBrad Bishop		self.exported = False
3440a360c2SBrad Bishop
3540a360c2SBrad Bishop	def getPath(self,name):
3640a360c2SBrad Bishop		return GPIO_SYSFS+'gpio'+self.gpio_num+'/'+name
3740a360c2SBrad Bishop
3840a360c2SBrad Bishop	def export(self):
3940a360c2SBrad Bishop		if (os.path.exists(GPIO_SYSFS+'export') == False):
4040a360c2SBrad Bishop			raise Exception("ERROR - GPIO_SYSFS path does not exist.  Does this platform support GPIOS?")
4140a360c2SBrad Bishop		if (os.path.exists(GPIO_SYSFS+'gpio'+self.gpio_num) == False):
4240a360c2SBrad Bishop			self.write(GPIO_SYSFS+'export',self.gpio_num)
4340a360c2SBrad Bishop
4440a360c2SBrad Bishop		self.exported = True
4540a360c2SBrad Bishop
4640a360c2SBrad Bishop	def setDirection(self,dir):
4740a360c2SBrad Bishop		if (self.exported == False):
4840a360c2SBrad Bishop			raise Exception("ERROR - Not exported: "+self.getPath())
4940a360c2SBrad Bishop
5040a360c2SBrad Bishop		self.direction = ''
5140a360c2SBrad Bishop		self.interrupt = ''
5240a360c2SBrad Bishop		if (dir == 'in' or dir == 'out'):
5340a360c2SBrad Bishop			self.direction = dir
5440a360c2SBrad Bishop		elif (dir == 'rising' or
5540a360c2SBrad Bishop		      dir == 'falling' or
5640a360c2SBrad Bishop		      dir == 'both'):
5740a360c2SBrad Bishop			self.direction = 'in'
5840a360c2SBrad Bishop			self.interrupt = dir
5940a360c2SBrad Bishop			self.write(self.getPath('edge'),self.interrupt)
6040a360c2SBrad Bishop		else:
6140a360c2SBrad Bishop			raise Exception("ERROR - Invalid Direction: "+dir)
6240a360c2SBrad Bishop
635d4a54e8SXo Wang		current_direction = self.read(self.getPath('direction'))
645d4a54e8SXo Wang		if current_direction != self.direction:
6540a360c2SBrad Bishop			self.write(self.getPath('direction'),self.direction)
6640a360c2SBrad Bishop
6740a360c2SBrad Bishop	def setValue(self,value):
6840a360c2SBrad Bishop		if (value == '0'):
6940a360c2SBrad Bishop			self.write(self.getPath('value'),'0')
7040a360c2SBrad Bishop		elif (value == '1'):
7140a360c2SBrad Bishop			self.write(self.getPath('value'),'1')
7240a360c2SBrad Bishop		else:
7340a360c2SBrad Bishop			raise Exception("ERROR - Invalid value: "+value)
7440a360c2SBrad Bishop
7540a360c2SBrad Bishop	def getValue(self):
7640a360c2SBrad Bishop		return self.read(self.getPath('value'))
7740a360c2SBrad Bishop
7840a360c2SBrad Bishop	def write(self,path,data):
7940a360c2SBrad Bishop		f = open(path,'w')
8040a360c2SBrad Bishop		f.write(data)
8140a360c2SBrad Bishop		f.close()
8240a360c2SBrad Bishop
8340a360c2SBrad Bishop
8440a360c2SBrad Bishop	def read(self,path):
8540a360c2SBrad Bishop		f = open(path,'r')
86*3045be31SXo Wang		data = f.readline().strip()
8740a360c2SBrad Bishop		f.close()
8840a360c2SBrad Bishop		return data
8940a360c2SBrad Bishop
9040a360c2SBrad Bishop
9140a360c2SBrad Bishop
9240a360c2SBrad Bishopif __name__ == '__main__':
9340a360c2SBrad Bishop
9440a360c2SBrad Bishop	try:
9540a360c2SBrad Bishop		opts, args = getopt.getopt(sys.argv,"hn:i:d:v:p:l:")
9640a360c2SBrad Bishop 	except getopt.GetoptError:
9740a360c2SBrad Bishop 		printUsage()
9840a360c2SBrad Bishop
9940a360c2SBrad Bishop
10040a360c2SBrad Bishop
10140a360c2SBrad Bishop	lookup = False
10240a360c2SBrad Bishop	gpio_name = ""
10340a360c2SBrad Bishop	value = ""
10440a360c2SBrad Bishop	direction = ""
10540a360c2SBrad Bishop	for opt, arg in opts:
10640a360c2SBrad Bishop 		if opt == '-h':
10740a360c2SBrad Bishop			printUsage()
10840a360c2SBrad Bishop
10940a360c2SBrad Bishop 		elif opt in ("-n"):
11040a360c2SBrad Bishop 			gpio_name = arg
11140a360c2SBrad Bishop			lookup = True
11240a360c2SBrad Bishop 		elif opt in ("-i"):
11340a360c2SBrad Bishop 			gpio_name = arg
11440a360c2SBrad Bishop 		elif opt in ("-d"):
11540a360c2SBrad Bishop			direction = arg
11640a360c2SBrad Bishop 		elif opt in ("-v"):
11740a360c2SBrad Bishop			value = arg
11840a360c2SBrad Bishop		elif opt in ("-p"):
119a7ac805bSBrad Bishop			gpio_name = obmc.system.convertGpio(arg)
12040a360c2SBrad Bishop		elif opt in ("-l"):
121a7ac805bSBrad Bishop			gpio_name = obmc.system.convertGpio(arg)
12240a360c2SBrad Bishop			print gpio_name
12340a360c2SBrad Bishop			exit(0)
12440a360c2SBrad Bishop
12540a360c2SBrad Bishop	gpio_info = {}
12640a360c2SBrad Bishop	if (lookup == True):
12740a360c2SBrad Bishop		if (System.GPIO_CONFIG.has_key(gpio_name) == False):
12840a360c2SBrad Bishop			print "ERROR - GPIO Name doesn't exist"
12940a360c2SBrad Bishop			print "Valid names: "
13040a360c2SBrad Bishop			for n in System.GPIO_CONFIG:
13140a360c2SBrad Bishop				print "\t"+n
13240a360c2SBrad Bishop			exit(0)
13340a360c2SBrad Bishop		gpio_info = System.GPIO_CONFIG[gpio_name]
13440a360c2SBrad Bishop		direction = gpio_info['direction']
13540a360c2SBrad Bishop		if (gpio_info.has_key('gpio_num')):
13640a360c2SBrad Bishop			gpio_name = str(gpio_info['gpio_num'])
13740a360c2SBrad Bishop		else:
138a7ac805bSBrad Bishop			gpio_name = str(obmc.system.convertGpio(gpio_info['gpio_pin']))
13940a360c2SBrad Bishop		print "GPIO ID: "+gpio_name+"; DIRECTION: "+direction
14040a360c2SBrad Bishop
14140a360c2SBrad Bishop
14240a360c2SBrad Bishop	## Rules
14340a360c2SBrad Bishop	if (gpio_name == ""):
14440a360c2SBrad Bishop		print "ERROR - Gpio not specified"
14540a360c2SBrad Bishop		printUsage()
14640a360c2SBrad Bishop
14740a360c2SBrad Bishop	if (direction == "in" and value != ""):
14840a360c2SBrad Bishop		print "ERROR - Value cannot be specified when direction = in"
14940a360c2SBrad Bishop		printUsage()
15040a360c2SBrad Bishop
15140a360c2SBrad Bishop	gpio = Gpio(gpio_name)
15240a360c2SBrad Bishop	try:
15340a360c2SBrad Bishop		gpio.export()
15440a360c2SBrad Bishop		if (direction != ""):
15540a360c2SBrad Bishop			gpio.setDirection(direction)
15640a360c2SBrad Bishop
15740a360c2SBrad Bishop		if (value == ""):
15840a360c2SBrad Bishop			print gpio.getValue()
15940a360c2SBrad Bishop		else:
16040a360c2SBrad Bishop			gpio.setValue(value)
16140a360c2SBrad Bishop
16240a360c2SBrad Bishop	except Exception as e:
16340a360c2SBrad Bishop		print e
16440a360c2SBrad Bishop
165