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